< prev index next >
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/ConditionalNode.java
Print this page
@@ -20,11 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.nodes.calc;
-import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
+import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
import static org.graalvm.compiler.nodes.calc.CompareNode.createCompareNode;
import org.graalvm.compiler.core.common.calc.Condition;
import org.graalvm.compiler.core.common.type.IntegerStamp;
@@ -45,14 +45,14 @@
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.vm.ci.meta.JavaConstant;
/**
- * The {@code ConditionalNode} class represents a comparison that yields one of two values. Note
- * that these nodes are not built directly from the bytecode but are introduced by canonicalization.
+ * The {@code ConditionalNode} class represents a comparison that yields one of two (eagerly
+ * evaluated) values.
*/
-@NodeInfo(cycles = CYCLES_0, size = SIZE_2)
+@NodeInfo(cycles = CYCLES_1, size = SIZE_2)
public final class ConditionalNode extends FloatingNode implements Canonicalizable, LIRLowerable {
public static final NodeClass<ConditionalNode> TYPE = NodeClass.create(ConditionalNode.class);
@Input(InputType.Condition) LogicNode condition;
@Input ValueNode trueValue;
@@ -114,11 +114,10 @@
if (constant != null) {
IntegerStamp bounds = StampFactory.forInteger(constant.getJavaKind(), constant.asLong(), constant.getJavaKind().getMaxValue());
valueStamp = valueStamp.join(bounds);
}
}
-
}
return updateStamp(valueStamp);
}
public ValueNode trueValue() {
@@ -143,12 +142,44 @@
return this;
}
public static ValueNode canonicalizeConditional(LogicNode condition, ValueNode trueValue, ValueNode falseValue, Stamp stamp) {
- // this optimizes the case where a value from the range 0 - 1 is mapped to the range 0 - 1
- if (trueValue.isConstant() && falseValue.isConstant() && trueValue.stamp() instanceof IntegerStamp && falseValue.stamp() instanceof IntegerStamp) {
+ if (trueValue == falseValue) {
+ return trueValue;
+ }
+
+ if (condition instanceof CompareNode && ((CompareNode) condition).isIdentityComparison()) {
+ // optimize the pattern (x == y) ? x : y
+ CompareNode compare = (CompareNode) condition;
+ if ((compare.getX() == trueValue && compare.getY() == falseValue) || (compare.getX() == falseValue && compare.getY() == trueValue)) {
+ return falseValue;
+ }
+ }
+
+ if (trueValue.stamp() instanceof IntegerStamp) {
+ // check if the conditional is redundant
+ if (condition instanceof IntegerLessThanNode) {
+ IntegerLessThanNode lessThan = (IntegerLessThanNode) condition;
+ IntegerStamp falseValueStamp = (IntegerStamp) falseValue.stamp();
+ IntegerStamp trueValueStamp = (IntegerStamp) trueValue.stamp();
+ if (lessThan.getX() == trueValue && lessThan.getY() == falseValue) {
+ // return "x" for "x < y ? x : y" in case that we know "x <= y"
+ if (trueValueStamp.upperBound() <= falseValueStamp.lowerBound()) {
+ return trueValue;
+ }
+ } else if (lessThan.getX() == falseValue && lessThan.getY() == trueValue) {
+ // return "x" for "x < y ? y : x" in case that we know "x <= y"
+ if (falseValueStamp.upperBound() <= trueValueStamp.lowerBound()) {
+ return falseValue;
+ }
+ }
+ }
+
+ // this optimizes the case where a value from the range 0 - 1 is mapped to the
+ // range 0 - 1
+ if (trueValue.isConstant() && falseValue.isConstant()) {
long constTrueValue = trueValue.asJavaConstant().asLong();
long constFalseValue = falseValue.asJavaConstant().asLong();
if (condition instanceof IntegerEqualsNode) {
IntegerEqualsNode equals = (IntegerEqualsNode) condition;
if (equals.getY().isConstant() && equals.getX().stamp() instanceof IntegerStamp) {
@@ -186,25 +217,15 @@
return IntegerConvertNode.convertUnsigned(AndNode.create(integerTestNode.getX(), integerTestNode.getY()), stamp);
}
}
}
}
- if (condition instanceof CompareNode && ((CompareNode) condition).isIdentityComparison()) {
- // optimize the pattern (x == y) ? x : y
- CompareNode compare = (CompareNode) condition;
- if ((compare.getX() == trueValue && compare.getY() == falseValue) || (compare.getX() == falseValue && compare.getY() == trueValue)) {
- return falseValue;
- }
- }
- if (trueValue == falseValue) {
- return trueValue;
- }
- if (condition instanceof IntegerLessThanNode && trueValue.stamp() instanceof IntegerStamp) {
+ if (condition instanceof IntegerLessThanNode) {
/*
- * Convert a conditional add ((x < 0) ? (x + y) : x) into (x + (y & (x >> (bits - 1))))
- * to avoid the test.
+ * Convert a conditional add ((x < 0) ? (x + y) : x) into (x + (y & (x >> (bits -
+ * 1)))) to avoid the test.
*/
IntegerLessThanNode lt = (IntegerLessThanNode) condition;
if (lt.getY().isConstant() && lt.getY().asConstant().isDefaultForKind()) {
if (falseValue == lt.getX()) {
if (trueValue instanceof AddNode) {
@@ -217,10 +238,11 @@
}
}
}
}
}
+ }
return null;
}
private static ValueNode findSynonym(ValueNode condition, ValueNode trueValue, ValueNode falseValue) {
< prev index next >