Search in sources :

Example 11 with IntegerEqualsNode

use of org.graalvm.compiler.nodes.calc.IntegerEqualsNode 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;
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) NormalizeCompareNode(org.graalvm.compiler.nodes.calc.NormalizeCompareNode)

Example 12 with IntegerEqualsNode

use of org.graalvm.compiler.nodes.calc.IntegerEqualsNode in project graal by oracle.

the class IfNode method tryEliminateBoxedReferenceEquals.

/**
 * Attempts to replace the following pattern:
 *
 * <pre>
 * Integer x = ...;
 * Integer y = ...;
 * if ((x == y) || x.equals(y)) { ... }
 * </pre>
 *
 * with:
 *
 * <pre>
 * Integer x = ...;
 * Integer y = ...;
 * if (x.equals(y)) { ... }
 * </pre>
 *
 * whenever the probability that the reference check will pass is relatively small.
 *
 * See GR-1315 for more information.
 */
private boolean tryEliminateBoxedReferenceEquals(SimplifierTool tool) {
    if (!(condition instanceof ObjectEqualsNode)) {
        return false;
    }
    MetaAccessProvider meta = tool.getMetaAccess();
    ObjectEqualsNode equalsCondition = (ObjectEqualsNode) condition;
    ValueNode x = equalsCondition.getX();
    ValueNode y = equalsCondition.getY();
    ResolvedJavaType integerType = meta.lookupJavaType(Integer.class);
    // At least one argument for reference equal must be a boxed primitive.
    NodeView view = NodeView.from(tool);
    if (!x.stamp(view).javaType(meta).equals(integerType) && !y.stamp(view).javaType(meta).equals(integerType)) {
        return false;
    }
    // no sense to eliminate it.
    if (getTrueSuccessorProbability() > 0.4) {
        return false;
    }
    // True branch must be empty.
    if (trueSuccessor instanceof BeginNode || trueSuccessor instanceof LoopExitNode) {
        if (trueSuccessor.next() instanceof EndNode) {
        // Empty true branch.
        } else {
            return false;
        }
    } else {
        return false;
    }
    // False branch must only check the unboxed values.
    UnboxNode unbox = null;
    FixedGuardNode unboxCheck = null;
    for (FixedNode node : falseSuccessor.getBlockNodes()) {
        if (!(node instanceof BeginNode || node instanceof UnboxNode || node instanceof FixedGuardNode || node instanceof EndNode || node instanceof LoadFieldNode || node instanceof LoopExitNode)) {
            return false;
        }
        if (node instanceof UnboxNode) {
            if (unbox == null) {
                unbox = (UnboxNode) node;
            } else {
                return false;
            }
        }
        if (!(node instanceof FixedGuardNode)) {
            continue;
        }
        FixedGuardNode fixed = (FixedGuardNode) node;
        if (!(fixed.condition() instanceof IntegerEqualsNode)) {
            continue;
        }
        IntegerEqualsNode equals = (IntegerEqualsNode) fixed.condition();
        if ((isUnboxedFrom(meta, view, equals.getX(), x) && isUnboxedFrom(meta, view, equals.getY(), y)) || (isUnboxedFrom(meta, view, equals.getX(), y) && isUnboxedFrom(meta, view, equals.getY(), x))) {
            unboxCheck = fixed;
        }
    }
    if (unbox == null || unboxCheck == null) {
        return false;
    }
    // Falsify the reference check.
    setCondition(graph().addOrUniqueWithInputs(LogicConstantNode.contradiction()));
    return true;
}
Also used : IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) ObjectEqualsNode(org.graalvm.compiler.nodes.calc.ObjectEqualsNode) LoadFieldNode(org.graalvm.compiler.nodes.java.LoadFieldNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) UnboxNode(org.graalvm.compiler.nodes.extended.UnboxNode) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider)

Aggregations

IntegerEqualsNode (org.graalvm.compiler.nodes.calc.IntegerEqualsNode)12 ValueNode (org.graalvm.compiler.nodes.ValueNode)8 ConditionalNode (org.graalvm.compiler.nodes.calc.ConditionalNode)7 LogicNode (org.graalvm.compiler.nodes.LogicNode)6 IntegerLessThanNode (org.graalvm.compiler.nodes.calc.IntegerLessThanNode)3 CanonicalizedCondition (org.graalvm.compiler.core.common.calc.Condition.CanonicalizedCondition)2 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)2 IfNode (org.graalvm.compiler.nodes.IfNode)2 CompareNode (org.graalvm.compiler.nodes.calc.CompareNode)2 IntegerBelowNode (org.graalvm.compiler.nodes.calc.IntegerBelowNode)2 VirtualObjectNode (org.graalvm.compiler.nodes.virtual.VirtualObjectNode)2 JavaKind (jdk.vm.ci.meta.JavaKind)1 MetaAccessProvider (jdk.vm.ci.meta.MetaAccessProvider)1 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)1 Condition (org.graalvm.compiler.core.common.calc.Condition)1 FloatConvert (org.graalvm.compiler.core.common.calc.FloatConvert)1 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)1 GraalError (org.graalvm.compiler.debug.GraalError)1 BciBlock (org.graalvm.compiler.java.BciBlockMapping.BciBlock)1