package org.graalvm.compiler.nodes.extended;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
import org.graalvm.compiler.core.common.calc.Condition;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.iterators.NodePredicates;
import org.graalvm.compiler.graph.spi.Simplifiable;
import org.graalvm.compiler.graph.spi.SimplifierTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.FixedGuardNode;
import org.graalvm.compiler.nodes.IfNode;
import org.graalvm.compiler.nodes.ReturnNode;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.calc.ConditionalNode;
import org.graalvm.compiler.nodes.calc.FloatingNode;
import org.graalvm.compiler.nodes.calc.IntegerEqualsNode;
import org.graalvm.compiler.nodes.spi.Lowerable;
import org.graalvm.compiler.nodes.spi.LoweringTool;
@NodeInfo(cycles = CYCLES_0, cyclesRationale = "Artificial Node", size = SIZE_0)
public final class BranchProbabilityNode extends FloatingNode implements Simplifiable, Lowerable {
public static final NodeClass<BranchProbabilityNode> TYPE = NodeClass.create(BranchProbabilityNode.class);
public static final double LIKELY_PROBABILITY = 0.6;
public static final double NOT_LIKELY_PROBABILITY = 1 - LIKELY_PROBABILITY;
public static final double FREQUENT_PROBABILITY = 0.9;
public static final double NOT_FREQUENT_PROBABILITY = 1 - FREQUENT_PROBABILITY;
public static final double FAST_PATH_PROBABILITY = 0.99;
public static final double SLOW_PATH_PROBABILITY = 1 - FAST_PATH_PROBABILITY;
public static final double VERY_FAST_PATH_PROBABILITY = 0.999;
public static final double VERY_SLOW_PATH_PROBABILITY = 1 - VERY_FAST_PATH_PROBABILITY;
@Input ValueNode probability;
@Input ValueNode condition;
public BranchProbabilityNode(ValueNode probability, ValueNode condition) {
super(TYPE, condition.stamp());
this.probability = probability;
this.condition = condition;
}
public ValueNode getProbability() {
return probability;
}
public ValueNode getCondition() {
return condition;
}
@Override
public void simplify(SimplifierTool tool) {
if (probability.isConstant()) {
double probabilityValue = probability.asJavaConstant().asDouble();
if (probabilityValue < 0.0) {
throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!");
} else if (probabilityValue > 1.0) {
throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!");
} else if (Double.isNaN(probabilityValue)) {
return;
}
boolean usageFound = false;
for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) {
if (node.condition() == Condition.EQ) {
ValueNode other = node.getX();
if (node.getX() == this) {
other = node.getY();
}
if (other.isConstant()) {
double probabilityToSet = probabilityValue;
if (other.asJavaConstant().asInt() == 0) {
probabilityToSet = 1.0 - probabilityToSet;
}
for (IfNode ifNodeUsages : node.usages().filter(IfNode.class)) {
usageFound = true;
ifNodeUsages.setTrueSuccessorProbability(probabilityToSet);
}
if (!usageFound) {
usageFound = node.usages().filter(NodePredicates.isA(FixedGuardNode.class).or(ConditionalNode.class)).isNotEmpty();
}
}
}
}
if (usageFound) {
ValueNode currentCondition = condition;
replaceAndDelete(currentCondition);
if (tool != null) {
tool.addToWorkList(currentCondition.usages());
}
} else {
if (!isSubstitutionGraph()) {
throw new GraalError("Wrong usage of branch probability injection!");
}
}
}
}
private boolean isSubstitutionGraph() {
return getUsageCount() == 1 && usages().first() instanceof ReturnNode;
}
@NodeIntrinsic
public static native boolean probability(double probability, boolean condition);
@Override
public void lower(LoweringTool tool) {
throw new GraalError("Branch probability could not be injected, because the probability value did not reduce to a constant value.");
}
}