1 /* 2 * Copyright (c) 2012, 2014, 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.nodes.extended; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 27 28 import org.graalvm.compiler.core.common.calc.Condition; 29 import org.graalvm.compiler.debug.GraalError; 30 import org.graalvm.compiler.graph.NodeClass; 31 import org.graalvm.compiler.graph.iterators.NodePredicates; 32 import org.graalvm.compiler.graph.spi.Simplifiable; 33 import org.graalvm.compiler.graph.spi.SimplifierTool; 34 import org.graalvm.compiler.nodeinfo.NodeInfo; 35 import org.graalvm.compiler.nodes.FixedGuardNode; 36 import org.graalvm.compiler.nodes.IfNode; 37 import org.graalvm.compiler.nodes.ReturnNode; 38 import org.graalvm.compiler.nodes.ValueNode; 39 import org.graalvm.compiler.nodes.calc.ConditionalNode; 40 import org.graalvm.compiler.nodes.calc.FloatingNode; 41 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 42 import org.graalvm.compiler.nodes.spi.Lowerable; 43 import org.graalvm.compiler.nodes.spi.LoweringTool; 44 45 /** 46 * Instances of this node class will look for a preceding if node and put the given probability into 47 * the if node's taken probability. Then the branch probability node will be removed. This node is 48 * intended primarily for snippets, so that they can define their fast and slow paths. 49 */ 50 @NodeInfo(cycles = CYCLES_0, cyclesRationale = "Artificial Node", size = SIZE_0) 51 public final class BranchProbabilityNode extends FloatingNode implements Simplifiable, Lowerable { 52 53 public static final NodeClass<BranchProbabilityNode> TYPE = NodeClass.create(BranchProbabilityNode.class); 54 public static final double LIKELY_PROBABILITY = 0.6; 55 public static final double NOT_LIKELY_PROBABILITY = 1 - LIKELY_PROBABILITY; 56 57 public static final double FREQUENT_PROBABILITY = 0.9; 58 public static final double NOT_FREQUENT_PROBABILITY = 1 - FREQUENT_PROBABILITY; 59 60 public static final double FAST_PATH_PROBABILITY = 0.99; 61 public static final double SLOW_PATH_PROBABILITY = 1 - FAST_PATH_PROBABILITY; 62 63 public static final double VERY_FAST_PATH_PROBABILITY = 0.999; 64 public static final double VERY_SLOW_PATH_PROBABILITY = 1 - VERY_FAST_PATH_PROBABILITY; 65 66 @Input ValueNode probability; 67 @Input ValueNode condition; 68 69 public BranchProbabilityNode(ValueNode probability, ValueNode condition) { 70 super(TYPE, condition.stamp()); 71 this.probability = probability; 72 this.condition = condition; 73 } 74 75 public ValueNode getProbability() { 76 return probability; 77 } 78 79 public ValueNode getCondition() { 80 return condition; 81 } 82 83 @Override 84 public void simplify(SimplifierTool tool) { 85 if (probability.isConstant()) { 86 double probabilityValue = probability.asJavaConstant().asDouble(); 87 if (probabilityValue < 0.0) { 88 throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!"); 89 } else if (probabilityValue > 1.0) { 90 throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!"); 91 } else if (Double.isNaN(probabilityValue)) { 92 /* 93 * We allow NaN if the node is in unreachable code that will eventually fall away, 94 * or else an error will be thrown during lowering since we keep the node around. 95 */ 96 return; 97 } 98 boolean usageFound = false; 99 for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) { 100 if (node.condition() == Condition.EQ) { 101 ValueNode other = node.getX(); 102 if (node.getX() == this) { 103 other = node.getY(); 104 } 105 if (other.isConstant()) { 106 double probabilityToSet = probabilityValue; 107 if (other.asJavaConstant().asInt() == 0) { 108 probabilityToSet = 1.0 - probabilityToSet; 109 } 110 for (IfNode ifNodeUsages : node.usages().filter(IfNode.class)) { 111 usageFound = true; 112 ifNodeUsages.setTrueSuccessorProbability(probabilityToSet); 113 } 114 if (!usageFound) { 115 usageFound = node.usages().filter(NodePredicates.isA(FixedGuardNode.class).or(ConditionalNode.class)).isNotEmpty(); 116 } 117 } 118 } 119 } 120 if (usageFound) { 121 ValueNode currentCondition = condition; 122 replaceAndDelete(currentCondition); 123 if (tool != null) { 124 tool.addToWorkList(currentCondition.usages()); 125 } 126 } else { 127 if (!isSubstitutionGraph()) { 128 throw new GraalError("Wrong usage of branch probability injection!"); 129 } 130 } 131 } 132 } 133 134 private boolean isSubstitutionGraph() { 135 return getUsageCount() == 1 && usages().first() instanceof ReturnNode; 136 } 137 138 /** 139 * This intrinsic should only be used for the condition of an if statement. The parameter 140 * condition should also only denote a simple condition and not a combined condition involving 141 * && or || operators. It injects the probability of the condition into the if 142 * statement. 143 * 144 * @param probability the probability that the given condition is true as a double value between 145 * 0.0 and 1.0. 146 * @param condition the simple condition without any && or || operators 147 * @return the condition 148 */ 149 @NodeIntrinsic 150 public static native boolean probability(double probability, boolean condition); 151 152 @Override 153 public void lower(LoweringTool tool) { 154 throw new GraalError("Branch probability could not be injected, because the probability value did not reduce to a constant value."); 155 } 156 }