use of org.graalvm.compiler.nodes.IfNode in project graal by oracle.
the class BranchProbabilityNode method simplify.
@Override
public void simplify(SimplifierTool tool) {
if (!hasUsages()) {
return;
}
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)) {
/*
* We allow NaN if the node is in unreachable code that will eventually fall away,
* or else an error will be thrown during lowering since we keep the node around.
*/
return;
}
boolean usageFound = false;
for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) {
assert node.condition() == CanonicalCondition.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!");
}
}
}
}
use of org.graalvm.compiler.nodes.IfNode in project graal by oracle.
the class OptimizeExceptionCallsPhase method setBranchProbability.
/**
* Sets the branch probability of the guarding IfNode to a small value. The effect is that the
* exception call block is put at the end of the method. The other block (= the regular path)
* gets the fall-through block of the IfNode. This should give a better performance - and it
* looks nicer in the disassembly.
*/
private static void setBranchProbability(Node endNode) {
Node node = endNode;
Node predecessor = node.predecessor();
// Go "up" the graph until we find an IfNode
while (predecessor != null) {
if (predecessor instanceof IfNode && node instanceof BeginNode) {
// We found an IfNode which branches to our runtime exception call
IfNode ifNode = (IfNode) predecessor;
ifNode.setTrueSuccessorProbability(node == ifNode.trueSuccessor() ? 0.00001 : 0.99999);
return;
}
if (predecessor instanceof MergeNode || predecessor instanceof ControlSplitNode) {
// Any other split or merge is suspicious: we abort
return;
}
node = predecessor;
predecessor = node.predecessor();
}
}
use of org.graalvm.compiler.nodes.IfNode in project graal by oracle.
the class AMD64NodeMatchRules method emitIntegerTestBranchMemory.
private ComplexMatchResult emitIntegerTestBranchMemory(IfNode x, ValueNode value, LIRLowerableAccess access) {
LabelRef trueLabel = getLIRBlock(x.trueSuccessor());
LabelRef falseLabel = getLIRBlock(x.falseSuccessor());
double trueLabelProbability = x.probability(x.trueSuccessor());
AMD64Kind kind = getMemoryKind(access);
OperandSize size = kind == AMD64Kind.QWORD ? QWORD : DWORD;
if (value.isConstant()) {
JavaConstant constant = value.asJavaConstant();
if (constant != null && kind == AMD64Kind.QWORD && !NumUtil.isInt(constant.asLong())) {
// Only imm32 as long
return null;
}
return builder -> {
AMD64AddressValue address = (AMD64AddressValue) operand(access.getAddress());
gen.append(new AMD64BinaryConsumer.MemoryConstOp(AMD64MIOp.TEST, size, address, (int) constant.asLong(), getState(access)));
gen.append(new BranchOp(Condition.EQ, trueLabel, falseLabel, trueLabelProbability));
return null;
};
} else {
return builder -> {
AMD64AddressValue address = (AMD64AddressValue) operand(access.getAddress());
gen.append(new AMD64BinaryConsumer.MemoryRMOp(AMD64RMOp.TEST, size, gen.asAllocatable(operand(value)), address, getState(access)));
gen.append(new BranchOp(Condition.EQ, trueLabel, falseLabel, trueLabelProbability));
return null;
};
}
}
use of org.graalvm.compiler.nodes.IfNode in project graal by oracle.
the class SimpleCFGTest method testImplies.
@Test
public void testImplies() {
OptionValues options = getInitialOptions();
DebugContext debug = DebugContext.create(options, new GraalDebugHandlersFactory(getSnippetReflection()));
StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).build();
EndNode trueEnd = graph.add(new EndNode());
EndNode falseEnd = graph.add(new EndNode());
AbstractBeginNode trueBegin = graph.add(new BeginNode());
trueBegin.setNext(trueEnd);
AbstractBeginNode falseBegin = graph.add(new BeginNode());
falseBegin.setNext(falseEnd);
IfNode ifNode = graph.add(new IfNode(null, trueBegin, falseBegin, 0.5));
graph.start().setNext(ifNode);
AbstractMergeNode merge = graph.add(new MergeNode());
merge.addForwardEnd(trueEnd);
merge.addForwardEnd(falseEnd);
ReturnNode returnNode = graph.add(new ReturnNode(null));
merge.setNext(returnNode);
dumpGraph(graph);
ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true);
Block[] blocks = cfg.getBlocks();
// check number of blocks
assertDeepEquals(4, blocks.length);
// check block - node assignment
assertDeepEquals(blocks[0], cfg.blockFor(graph.start()));
assertDeepEquals(blocks[0], cfg.blockFor(ifNode));
assertDeepEquals(blocks[1], cfg.blockFor(trueBegin));
assertDeepEquals(blocks[1], cfg.blockFor(trueEnd));
assertDeepEquals(blocks[2], cfg.blockFor(falseBegin));
assertDeepEquals(blocks[2], cfg.blockFor(falseEnd));
assertDeepEquals(blocks[3], cfg.blockFor(merge));
assertDeepEquals(blocks[3], cfg.blockFor(returnNode));
// check dominators
assertDominator(blocks[0], null);
assertDominator(blocks[1], blocks[0]);
assertDominator(blocks[2], blocks[0]);
assertDominator(blocks[3], blocks[0]);
// check dominated
assertDominatedSize(blocks[0], 3);
assertDominatedSize(blocks[1], 0);
assertDominatedSize(blocks[2], 0);
assertDominatedSize(blocks[3], 0);
// check postdominators
assertPostdominator(blocks[0], blocks[3]);
assertPostdominator(blocks[1], blocks[3]);
assertPostdominator(blocks[2], blocks[3]);
assertPostdominator(blocks[3], null);
}
use of org.graalvm.compiler.nodes.IfNode in project graal by oracle.
the class HashMapGetTest method hashMapTest.
@Test
public void hashMapTest() {
HashMap<Integer, Integer> map = new HashMap<>();
ResolvedJavaMethod get = getResolvedJavaMethod(HashMapGetTest.class, "mapGet");
for (int i = 0; i < 5000; i++) {
mapGet(map, i);
map.put(i, i);
mapGet(map, i);
}
test(get, null, map, new Integer(0));
for (IfNode ifNode : lastCompiledGraph.getNodes(IfNode.TYPE)) {
LogicNode condition = ifNode.condition();
if (ifNode.getTrueSuccessorProbability() < 0.4 && condition instanceof ObjectEqualsNode) {
assertTrue(ifNode.trueSuccessor().next() instanceof ReturnNode, "Expected return.", ifNode.trueSuccessor(), ifNode.trueSuccessor().next());
}
}
}
Aggregations