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