Search in sources :

Example 1 with NormalizeCompareNode

use of org.graalvm.compiler.nodes.calc.NormalizeCompareNode 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)

Aggregations

ConditionalNode (org.graalvm.compiler.nodes.calc.ConditionalNode)1 IntegerEqualsNode (org.graalvm.compiler.nodes.calc.IntegerEqualsNode)1 IntegerLessThanNode (org.graalvm.compiler.nodes.calc.IntegerLessThanNode)1 NormalizeCompareNode (org.graalvm.compiler.nodes.calc.NormalizeCompareNode)1