1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot.meta;
  24 
  25 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileBranchNode;
  26 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileInvokeNode;
  27 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
  28 import org.graalvm.compiler.nodes.ConstantNode;
  29 import org.graalvm.compiler.nodes.FrameState;
  30 import org.graalvm.compiler.nodes.LogicNode;
  31 import org.graalvm.compiler.nodes.ValueNode;
  32 import org.graalvm.compiler.nodes.calc.ConditionalNode;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  34 import org.graalvm.compiler.nodes.graphbuilderconf.ProfilingPlugin;
  35 import org.graalvm.compiler.options.Option;
  36 import org.graalvm.compiler.options.OptionType;
  37 import org.graalvm.compiler.options.OptionValue;
  38 
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 
  41 public abstract class HotSpotProfilingPlugin implements ProfilingPlugin {
  42     public static class Options {
  43         @Option(help = "Emit profiling of invokes", type = OptionType.Expert)//
  44         public static final OptionValue<Boolean> ProfileInvokes = new OptionValue<>(true);
  45         @Option(help = "Emit profiling of backedges", type = OptionType.Expert)//
  46         public static final OptionValue<Boolean> ProfileBackedges = new OptionValue<>(true);
  47     }
  48 
  49     public abstract int invokeNotifyFreqLog();
  50 
  51     public abstract int invokeInlineeNotifyFreqLog();
  52 
  53     public abstract int invokeProfilePobabilityLog();
  54 
  55     public abstract int backedgeNotifyFreqLog();
  56 
  57     public abstract int backedgeProfilePobabilityLog();
  58 
  59     @Override
  60     public boolean shouldProfile(GraphBuilderContext builder, ResolvedJavaMethod method) {
  61         return !builder.parsingIntrinsic();
  62     }
  63 
  64     @Override
  65     public void profileInvoke(GraphBuilderContext builder, ResolvedJavaMethod method, FrameState frameState) {
  66         assert shouldProfile(builder, method);
  67         if (Options.ProfileInvokes.getValue() && !method.isClassInitializer()) {
  68             ProfileNode p = builder.append(new ProfileInvokeNode(method, invokeNotifyFreqLog(), invokeProfilePobabilityLog()));
  69             p.setStateBefore(frameState);
  70         }
  71     }
  72 
  73     @Override
  74     public void profileGoto(GraphBuilderContext builder, ResolvedJavaMethod method, int bci, int targetBci, FrameState frameState) {
  75         assert shouldProfile(builder, method);
  76         if (Options.ProfileBackedges.getValue() && targetBci <= bci) {
  77             ProfileNode p = builder.append(new ProfileBranchNode(method, backedgeNotifyFreqLog(), backedgeProfilePobabilityLog(), bci, targetBci));
  78             p.setStateBefore(frameState);
  79         }
  80     }
  81 
  82     @Override
  83     public void profileIf(GraphBuilderContext builder, ResolvedJavaMethod method, int bci, LogicNode condition, int trueBranchBci, int falseBranchBci, FrameState frameState) {
  84         assert shouldProfile(builder, method);
  85         if (Options.ProfileBackedges.getValue() && (falseBranchBci <= bci || trueBranchBci <= bci)) {
  86             boolean negate = false;
  87             int targetBci = trueBranchBci;
  88             if (falseBranchBci <= bci) {
  89                 assert trueBranchBci > bci;
  90                 negate = true;
  91                 targetBci = falseBranchBci;
  92             } else {
  93                 assert trueBranchBci <= bci && falseBranchBci > bci;
  94             }
  95             ValueNode trueValue = builder.append(ConstantNode.forBoolean(!negate));
  96             ValueNode falseValue = builder.append(ConstantNode.forBoolean(negate));
  97             ConditionalNode branchCondition = builder.append(new ConditionalNode(condition, trueValue, falseValue));
  98             ProfileNode p = builder.append(new ProfileBranchNode(method, backedgeNotifyFreqLog(), backedgeProfilePobabilityLog(), branchCondition, bci, targetBci));
  99             p.setStateBefore(frameState);
 100         }
 101     }
 102 }