Search in sources :

Example 6 with IntegerLessThanNode

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)));
    }
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) LogicNode(org.graalvm.compiler.nodes.LogicNode)

Example 7 with IntegerLessThanNode

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;
}
Also used : IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) Constant(jdk.vm.ci.meta.Constant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) JavaConstant(jdk.vm.ci.meta.JavaConstant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) JavaConstant(jdk.vm.ci.meta.JavaConstant)

Example 8 with IntegerLessThanNode

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;
}
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

IntegerLessThanNode (org.graalvm.compiler.nodes.calc.IntegerLessThanNode)8 ValueNode (org.graalvm.compiler.nodes.ValueNode)6 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)4 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)4 CompareNode (org.graalvm.compiler.nodes.calc.CompareNode)4 ConditionalNode (org.graalvm.compiler.nodes.calc.ConditionalNode)4 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)3 IntegerBelowNode (org.graalvm.compiler.nodes.calc.IntegerBelowNode)3 IntegerEqualsNode (org.graalvm.compiler.nodes.calc.IntegerEqualsNode)3 Stamp (org.graalvm.compiler.core.common.type.Stamp)2 LogicNode (org.graalvm.compiler.nodes.LogicNode)2 JNIObjectHandle (com.oracle.svm.jni.nativeapi.JNIObjectHandle)1 Constant (jdk.vm.ci.meta.Constant)1 JavaConstant (jdk.vm.ci.meta.JavaConstant)1 PrimitiveConstant (jdk.vm.ci.meta.PrimitiveConstant)1 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)1 Condition (org.graalvm.compiler.core.common.calc.Condition)1 CanonicalizedCondition (org.graalvm.compiler.core.common.calc.Condition.CanonicalizedCondition)1 DebugCloseable (org.graalvm.compiler.debug.DebugCloseable)1 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)1