use of org.graalvm.compiler.nodes.calc.IntegerLessThanNode in project graal by oracle.
the class LoopTransformations method updatePreLoopLimit.
private static void updatePreLoopLimit(IfNode preLimit, InductionVariable preIv, CountedLoopInfo preCounted) {
// Update the pre loops limit test
StructuredGraph graph = preLimit.graph();
LogicNode ifTest = preLimit.condition();
CompareNode compareNode = (CompareNode) ifTest;
ValueNode prePhi = preIv.valueNode();
// Make new limit one iteration
ValueNode initIv = preCounted.getStart();
ValueNode newLimit = add(graph, initIv, preIv.strideNode());
// Fetch the variable we are not replacing and configure the one we are
ValueNode ub;
if (compareNode.getX() == prePhi) {
ub = compareNode.getY();
} else if (compareNode.getY() == prePhi) {
ub = compareNode.getX();
} else {
throw GraalError.shouldNotReachHere();
}
// Re-wire the condition with the new limit
if (preIv.direction() == Direction.Up) {
compareNode.replaceFirstInput(ub, graph.unique(new ConditionalNode(graph.unique(new IntegerLessThanNode(newLimit, ub)), newLimit, ub)));
} else {
compareNode.replaceFirstInput(ub, graph.unique(new ConditionalNode(graph.unique(new IntegerLessThanNode(ub, newLimit)), newLimit, ub)));
}
}
use of org.graalvm.compiler.nodes.calc.IntegerLessThanNode in project graal by oracle.
the class IfNode method checkForUnsignedCompare.
/**
* Recognize a couple patterns that can be merged into an unsigned compare.
*
* @param tool
* @return true if a replacement was done.
*/
private boolean checkForUnsignedCompare(SimplifierTool tool) {
assert trueSuccessor().hasNoUsages() && falseSuccessor().hasNoUsages();
if (condition() instanceof IntegerLessThanNode) {
NodeView view = NodeView.from(tool);
IntegerLessThanNode lessThan = (IntegerLessThanNode) condition();
Constant y = lessThan.getY().stamp(view).asConstant();
if (y instanceof PrimitiveConstant && ((PrimitiveConstant) y).asLong() == 0 && falseSuccessor().next() instanceof IfNode) {
IfNode ifNode2 = (IfNode) falseSuccessor().next();
if (ifNode2.condition() instanceof IntegerLessThanNode) {
IntegerLessThanNode lessThan2 = (IntegerLessThanNode) ifNode2.condition();
AbstractBeginNode falseSucc = ifNode2.falseSuccessor();
AbstractBeginNode trueSucc = ifNode2.trueSuccessor();
IntegerBelowNode below = null;
/*
* Convert x >= 0 && x < positive which is represented as !(x < 0) && x <
* <positive> into an unsigned compare.
*/
if (lessThan2.getX() == lessThan.getX() && lessThan2.getY().stamp(view) instanceof IntegerStamp && ((IntegerStamp) lessThan2.getY().stamp(view)).isPositive() && sameDestination(trueSuccessor(), ifNode2.falseSuccessor)) {
below = graph().unique(new IntegerBelowNode(lessThan2.getX(), lessThan2.getY()));
// swap direction
AbstractBeginNode tmp = falseSucc;
falseSucc = trueSucc;
trueSucc = tmp;
} else if (lessThan2.getY() == lessThan.getX() && sameDestination(trueSuccessor(), ifNode2.trueSuccessor)) {
/*
* Convert x >= 0 && x <= positive which is represented as !(x < 0) &&
* !(<positive> > x), into x <| positive + 1. This can only be done for
* constants since there isn't a IntegerBelowEqualThanNode but that doesn't
* appear to be interesting.
*/
JavaConstant positive = lessThan2.getX().asJavaConstant();
if (positive != null && positive.asLong() > 0 && positive.asLong() < positive.getJavaKind().getMaxValue()) {
ConstantNode newLimit = ConstantNode.forIntegerStamp(lessThan2.getX().stamp(view), positive.asLong() + 1, graph());
below = graph().unique(new IntegerBelowNode(lessThan.getX(), newLimit));
}
}
if (below != null) {
ifNode2.setTrueSuccessor(null);
ifNode2.setFalseSuccessor(null);
IfNode newIfNode = graph().add(new IfNode(below, falseSucc, trueSucc, 1 - trueSuccessorProbability));
// Remove the < 0 test.
tool.deleteBranch(trueSuccessor);
graph().removeSplit(this, falseSuccessor);
// Replace the second test with the new one.
ifNode2.predecessor().replaceFirstSuccessor(ifNode2, newIfNode);
ifNode2.safeDelete();
return true;
}
}
}
}
return false;
}
use of org.graalvm.compiler.nodes.calc.IntegerLessThanNode in project graal by oracle.
the class IfNode method canonicalizeConditionalCascade.
private ValueNode canonicalizeConditionalCascade(SimplifierTool tool, ValueNode trueValue, ValueNode falseValue) {
if (trueValue.getStackKind() != falseValue.getStackKind()) {
return null;
}
if (trueValue.getStackKind() != JavaKind.Int && trueValue.getStackKind() != JavaKind.Long) {
return null;
}
if (trueValue.isConstant() && falseValue.isConstant()) {
return graph().unique(new ConditionalNode(condition(), trueValue, falseValue));
} else if (!graph().isAfterExpandLogic()) {
ConditionalNode conditional = null;
ValueNode constant = null;
boolean negateCondition;
if (trueValue instanceof ConditionalNode && falseValue.isConstant()) {
conditional = (ConditionalNode) trueValue;
constant = falseValue;
negateCondition = true;
} else if (falseValue instanceof ConditionalNode && trueValue.isConstant()) {
conditional = (ConditionalNode) falseValue;
constant = trueValue;
negateCondition = false;
} else {
return null;
}
boolean negateConditionalCondition = false;
ValueNode otherValue = null;
if (constant == conditional.trueValue()) {
otherValue = conditional.falseValue();
negateConditionalCondition = false;
} else if (constant == conditional.falseValue()) {
otherValue = conditional.trueValue();
negateConditionalCondition = true;
}
if (otherValue != null && otherValue.isConstant()) {
double shortCutProbability = probability(trueSuccessor());
LogicNode newCondition = LogicNode.or(condition(), negateCondition, conditional.condition(), negateConditionalCondition, shortCutProbability);
return graph().unique(new ConditionalNode(newCondition, constant, otherValue));
} else if (!negateCondition && constant.isJavaConstant() && conditional.trueValue().isJavaConstant() && conditional.falseValue().isJavaConstant()) {
IntegerLessThanNode lessThan = null;
IntegerEqualsNode equals = null;
if (condition() instanceof IntegerLessThanNode && conditional.condition() instanceof IntegerEqualsNode && constant.asJavaConstant().asLong() == -1 && conditional.trueValue().asJavaConstant().asLong() == 0 && conditional.falseValue().asJavaConstant().asLong() == 1) {
lessThan = (IntegerLessThanNode) condition();
equals = (IntegerEqualsNode) conditional.condition();
} else if (condition() instanceof IntegerEqualsNode && conditional.condition() instanceof IntegerLessThanNode && constant.asJavaConstant().asLong() == 0 && conditional.trueValue().asJavaConstant().asLong() == -1 && conditional.falseValue().asJavaConstant().asLong() == 1) {
lessThan = (IntegerLessThanNode) conditional.condition();
equals = (IntegerEqualsNode) condition();
}
if (lessThan != null) {
assert equals != null;
NodeView view = NodeView.from(tool);
if ((lessThan.getX() == equals.getX() && lessThan.getY() == equals.getY()) || (lessThan.getX() == equals.getY() && lessThan.getY() == equals.getX())) {
return graph().unique(new NormalizeCompareNode(lessThan.getX(), lessThan.getY(), conditional.trueValue().stamp(view).getStackKind(), false));
}
}
}
}
return null;
}
Aggregations