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.OptionKey;
  37 import org.graalvm.compiler.options.OptionType;
  38 import org.graalvm.compiler.options.OptionValues;
  39 
  40 import jdk.vm.ci.meta.ResolvedJavaMethod;
  41 
  42 public abstract class HotSpotProfilingPlugin implements ProfilingPlugin {
  43     public static class Options {
  44         @Option(help = "Emit profiling of invokes", type = OptionType.Expert)//
  45         public static final OptionKey<Boolean> ProfileInvokes = new OptionKey<>(true);
  46         @Option(help = "Emit profiling of backedges", type = OptionType.Expert)//
  47         public static final OptionKey<Boolean> ProfileBackedges = new OptionKey<>(true);
  48     }
  49 
  50     public abstract int invokeNotifyFreqLog(OptionValues options);
  51 
  52     public abstract int invokeInlineeNotifyFreqLog(OptionValues options);
  53 
  54     public abstract int invokeProfilePobabilityLog(OptionValues options);
  55 
  56     public abstract int backedgeNotifyFreqLog(OptionValues options);
  57 
  58     public abstract int backedgeProfilePobabilityLog(OptionValues options);
  59 
  60     @Override
  61     public boolean shouldProfile(GraphBuilderContext builder, ResolvedJavaMethod method) {
  62         return !builder.parsingIntrinsic();
  63     }
  64 
  65     @Override
  66     public void profileInvoke(GraphBuilderContext builder, ResolvedJavaMethod method, FrameState frameState) {
  67         assert shouldProfile(builder, method);
  68         OptionValues options = builder.getOptions();
  69         if (Options.ProfileInvokes.getValue(options) && !method.isClassInitializer()) {
  70             ProfileNode p = builder.append(new ProfileInvokeNode(method, invokeNotifyFreqLog(options), invokeProfilePobabilityLog(options)));
  71             p.setStateBefore(frameState);
  72         }
  73     }
  74 
  75     @Override
  76     public void profileGoto(GraphBuilderContext builder, ResolvedJavaMethod method, int bci, int targetBci, FrameState frameState) {
  77         assert shouldProfile(builder, method);
  78         OptionValues options = builder.getOptions();
  79         if (Options.ProfileBackedges.getValue(options) && targetBci <= bci) {
  80             ProfileNode p = builder.append(new ProfileBranchNode(method, backedgeNotifyFreqLog(options), backedgeProfilePobabilityLog(options), bci, targetBci));
  81             p.setStateBefore(frameState);
  82         }
  83     }
  84 
  85     @Override
  86     public void profileIf(GraphBuilderContext builder, ResolvedJavaMethod method, int bci, LogicNode condition, int trueBranchBci, int falseBranchBci, FrameState frameState) {
  87         assert shouldProfile(builder, method);
  88         OptionValues options = builder.getOptions();
  89         if (Options.ProfileBackedges.getValue(options) && (falseBranchBci <= bci || trueBranchBci <= bci)) {
  90             boolean negate = false;
  91             int targetBci = trueBranchBci;
  92             if (falseBranchBci <= bci) {
  93                 assert trueBranchBci > bci;
  94                 negate = true;
  95                 targetBci = falseBranchBci;
  96             } else {
  97                 assert trueBranchBci <= bci && falseBranchBci > bci;
  98             }
  99             ValueNode trueValue = builder.append(ConstantNode.forBoolean(!negate));
 100             ValueNode falseValue = builder.append(ConstantNode.forBoolean(negate));
 101             ConditionalNode branchCondition = builder.append(new ConditionalNode(condition, trueValue, falseValue));
 102             ProfileNode p = builder.append(new ProfileBranchNode(method, backedgeNotifyFreqLog(options), backedgeProfilePobabilityLog(options), branchCondition, bci, targetBci));
 103             p.setStateBefore(frameState);
 104         }
 105     }
 106 }