1 /* 2 * Copyright (c) 2012, 2018, 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 24 25 package org.graalvm.compiler.nodes.extended; 26 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 29 30 import jdk.vm.ci.meta.JavaKind; 31 import org.graalvm.compiler.core.common.calc.CanonicalCondition; 32 import org.graalvm.compiler.core.common.type.IntegerStamp; 33 import org.graalvm.compiler.core.common.type.StampFactory; 34 import org.graalvm.compiler.debug.GraalError; 35 import org.graalvm.compiler.graph.Node; 36 import org.graalvm.compiler.graph.NodeClass; 37 import org.graalvm.compiler.graph.iterators.NodePredicates; 38 import org.graalvm.compiler.graph.spi.Canonicalizable; 39 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 40 import org.graalvm.compiler.graph.spi.Simplifiable; 41 import org.graalvm.compiler.graph.spi.SimplifierTool; 42 import org.graalvm.compiler.nodeinfo.NodeInfo; 43 import org.graalvm.compiler.nodes.FixedGuardNode; 44 import org.graalvm.compiler.nodes.IfNode; 45 import org.graalvm.compiler.nodes.NodeView; 46 import org.graalvm.compiler.nodes.ReturnNode; 47 import org.graalvm.compiler.nodes.ValueNode; 48 import org.graalvm.compiler.nodes.calc.ConditionalNode; 49 import org.graalvm.compiler.nodes.calc.FloatingNode; 50 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 51 import org.graalvm.compiler.nodes.calc.NarrowNode; 52 import org.graalvm.compiler.nodes.calc.ZeroExtendNode; 53 import org.graalvm.compiler.nodes.spi.Lowerable; 54 import org.graalvm.compiler.nodes.spi.LoweringTool; 55 56 /** 57 * Instances of this node class will look for a preceding if node and put the given probability into 58 * the if node's taken probability. Then the branch probability node will be removed. This node is 59 * intended primarily for snippets, so that they can define their fast and slow paths. 60 */ 61 @NodeInfo(cycles = CYCLES_0, cyclesRationale = "Artificial Node", size = SIZE_0) 62 public final class BranchProbabilityNode extends FloatingNode implements Simplifiable, Lowerable, Canonicalizable { 63 64 public static final NodeClass<BranchProbabilityNode> TYPE = NodeClass.create(BranchProbabilityNode.class); 65 public static final double LIKELY_PROBABILITY = 0.6; 66 public static final double NOT_LIKELY_PROBABILITY = 1 - LIKELY_PROBABILITY; 67 68 public static final double FREQUENT_PROBABILITY = 0.9; 69 public static final double NOT_FREQUENT_PROBABILITY = 1 - FREQUENT_PROBABILITY; 70 71 public static final double FAST_PATH_PROBABILITY = 0.99; 72 public static final double SLOW_PATH_PROBABILITY = 1 - FAST_PATH_PROBABILITY; 73 74 public static final double VERY_FAST_PATH_PROBABILITY = 0.999; 75 public static final double VERY_SLOW_PATH_PROBABILITY = 1 - VERY_FAST_PATH_PROBABILITY; 76 77 /* 78 * This probability may seem excessive, but it makes a difference in long running loops. Lets 79 * say a loop is executed 100k times and it has a few null checks with probability 0.999. As 80 * these probabilities multiply for every loop iteration, the overall loop frequency will be 81 * calculated as approximately 30 while it should be 100k. 82 */ 83 public static final double LUDICROUSLY_FAST_PATH_PROBABILITY = 0.999999; 84 public static final double LUDICROUSLY_SLOW_PATH_PROBABILITY = 1 - LUDICROUSLY_FAST_PATH_PROBABILITY; 85 86 @Input ValueNode probability; 87 @Input ValueNode condition; 88 89 public BranchProbabilityNode(ValueNode probability, ValueNode condition) { 90 super(TYPE, StampFactory.forKind(JavaKind.Boolean)); 91 this.probability = probability; 92 this.condition = condition; 93 } 94 95 public ValueNode getProbability() { 96 return probability; 97 } 98 99 public ValueNode getCondition() { 100 return condition; 101 } 102 103 @Override 104 public Node canonical(CanonicalizerTool tool) { 105 if (condition.isConstant()) { 106 // fold constant conditions early during PE 107 return condition; 108 } 109 return this; 110 } 111 112 @Override 113 public void simplify(SimplifierTool tool) { 114 if (!hasUsages()) { 115 return; 116 } 117 if (probability.isConstant()) { 118 double probabilityValue = probability.asJavaConstant().asDouble(); 119 if (probabilityValue < 0.0) { 120 throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!"); 121 } else if (probabilityValue > 1.0) { 122 throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!"); 123 } else if (Double.isNaN(probabilityValue)) { 124 /* 125 * We allow NaN if the node is in unreachable code that will eventually fall away, 126 * or else an error will be thrown during lowering since we keep the node around. 127 */ 128 return; 129 } 130 boolean usageFound = false; 131 for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) { 132 assert node.condition() == CanonicalCondition.EQ; 133 ValueNode other = node.getX(); 134 if (node.getX() == this) { 135 other = node.getY(); 136 } 137 if (other.isConstant()) { 138 double probabilityToSet = probabilityValue; 139 if (other.asJavaConstant().asInt() == 0) { 140 probabilityToSet = 1.0 - probabilityToSet; 141 } 142 for (IfNode ifNodeUsages : node.usages().filter(IfNode.class)) { 143 usageFound = true; 144 ifNodeUsages.setTrueSuccessorProbability(probabilityToSet); 145 } 146 if (!usageFound) { 147 usageFound = node.usages().filter(NodePredicates.isA(FixedGuardNode.class).or(ConditionalNode.class)).isNotEmpty(); 148 } 149 } 150 } 151 if (usageFound) { 152 ValueNode currentCondition = condition; 153 IntegerStamp currentStamp = (IntegerStamp) currentCondition.stamp(NodeView.DEFAULT); 154 if (currentStamp.lowerBound() < 0 || 1 < currentStamp.upperBound()) { 155 ValueNode narrow = graph().maybeAddOrUnique(NarrowNode.create(currentCondition, 1, NodeView.DEFAULT)); 156 currentCondition = graph().maybeAddOrUnique(ZeroExtendNode.create(narrow, 32, NodeView.DEFAULT)); 157 } 158 replaceAndDelete(currentCondition); 159 if (tool != null) { 160 tool.addToWorkList(currentCondition.usages()); 161 } 162 } else { 163 if (!isSubstitutionGraph()) { 164 throw new GraalError("Wrong usage of branch probability injection!"); 165 } 166 } 167 } 168 } 169 170 private boolean isSubstitutionGraph() { 171 return hasExactlyOneUsage() && usages().first() instanceof ReturnNode; 172 } 173 174 /** 175 * This intrinsic should only be used for the condition of an if statement. The parameter 176 * condition should also only denote a simple condition and not a combined condition involving 177 * && or || operators. It injects the probability of the condition into the if 178 * statement. 179 * 180 * @param probability the probability that the given condition is true as a double value between 181 * 0.0 and 1.0. 182 * @param condition the simple condition without any && or || operators 183 * @return the condition 184 */ 185 @NodeIntrinsic 186 public static native boolean probability(double probability, boolean condition); 187 188 @Override 189 public void lower(LoweringTool tool) { 190 throw new GraalError("Branch probability could not be injected, because the probability value did not reduce to a constant value."); 191 } 192 }