1 /*
   2  * Copyright (c) 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.nodes.profiling;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  27 
  28 import org.graalvm.compiler.core.common.type.StampFactory;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.iterators.NodeIterable;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.spi.Lowerable;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 import org.graalvm.compiler.options.Option;
  38 import org.graalvm.compiler.options.OptionKey;
  39 import org.graalvm.compiler.options.OptionType;
  40 
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 @NodeInfo(cycles = CYCLES_IGNORED, cyclesRationale = "profiling should be ignored", size = SIZE_IGNORED, sizeRationale = "profiling should be ignored")
  44 public class ProfileNode extends DeoptimizingFixedWithNextNode implements Lowerable {
  45     public static class Options {
  46         @Option(help = "Control probabilistic profiling on AMD64", type = OptionType.Expert)//
  47         public static final OptionKey<Boolean> ProbabilisticProfiling = new OptionKey<>(true);
  48     }
  49 
  50     public static final NodeClass<ProfileNode> TYPE = NodeClass.create(ProfileNode.class);
  51 
  52     protected ResolvedJavaMethod method;
  53 
  54     // Only used if ProbabilisticProfiling == true and may be ignored by lowerer.
  55     @OptionalInput protected ValueNode random;
  56 
  57     // logarithm base 2 of the profile probability
  58     protected int probabilityLog;
  59 
  60     protected ProfileNode(NodeClass<? extends DeoptimizingFixedWithNextNode> c, ResolvedJavaMethod method, int probabilityLog) {
  61         super(c, StampFactory.forVoid());
  62         this.method = method;
  63         this.probabilityLog = probabilityLog;
  64     }
  65 
  66     public ProfileNode(ResolvedJavaMethod method, int probabilityLog) {
  67         super(TYPE, StampFactory.forVoid());
  68         this.method = method;
  69         this.probabilityLog = probabilityLog;
  70     }
  71 
  72     @Override
  73     public boolean canDeoptimize() {
  74         return true;
  75     }
  76 
  77     @Override
  78     public void lower(LoweringTool tool) {
  79         tool.getLowerer().lower(this, tool);
  80     }
  81 
  82     public ResolvedJavaMethod getProfiledMethod() {
  83         return method;
  84     }
  85 
  86     public ValueNode getRandom() {
  87         return random;
  88     }
  89 
  90     public void setRandom(ValueNode r) {
  91         updateUsages(random, r);
  92         this.random = r;
  93     }
  94 
  95     /**
  96      * Get the logarithm base 2 of the profile probability.
  97      */
  98     public int getProbabilityLog() {
  99         return probabilityLog;
 100     }
 101 
 102     /**
 103      * Gathers all the {@link ProfileNode}s that are inputs to the
 104      * {@linkplain StructuredGraph#getNodes() live nodes} in a given graph.
 105      */
 106     public static NodeIterable<ProfileNode> getProfileNodes(StructuredGraph graph) {
 107         return graph.getNodes().filter(ProfileNode.class);
 108     }
 109 }