Search in sources :

Example 6 with Condition

use of org.graalvm.compiler.core.common.calc.Condition in project graal by oracle.

the class SPARCNodeMatchRules method ifCompareLogicCas.

@MatchRule("(If (ObjectEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (PointerEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (FloatEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (IntegerEquals=compare value LogicCompareAndSwap=cas))")
public ComplexMatchResult ifCompareLogicCas(IfNode root, CompareNode compare, ValueNode value, LogicCompareAndSwapNode cas) {
    JavaConstant constant = value.asJavaConstant();
    assert compare.condition() == CanonicalCondition.EQ;
    if (constant != null && cas.usages().count() == 1) {
        long constantValue = constant.asLong();
        boolean successIsTrue;
        if (constantValue == 0) {
            successIsTrue = false;
        } else if (constantValue == 1) {
            successIsTrue = true;
        } else {
            return null;
        }
        return builder -> {
            LIRKind kind = getLirKind(cas);
            LabelRef trueLabel = getLIRBlock(root.trueSuccessor());
            LabelRef falseLabel = getLIRBlock(root.falseSuccessor());
            double trueLabelProbability = root.probability(root.trueSuccessor());
            Value expectedValue = operand(cas.getExpectedValue());
            Value newValue = operand(cas.getNewValue());
            SPARCAddressValue address = (SPARCAddressValue) operand(cas.getAddress());
            Condition condition = successIsTrue ? Condition.EQ : Condition.NE;
            Value result = getLIRGeneratorTool().emitValueCompareAndSwap(address, expectedValue, newValue);
            getLIRGeneratorTool().emitCompareBranch(kind.getPlatformKind(), result, expectedValue, condition, false, trueLabel, falseLabel, trueLabelProbability);
            return null;
        };
    }
    return null;
}
Also used : CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) LIRFrameState(org.graalvm.compiler.lir.LIRFrameState) XWORD(jdk.vm.ci.sparc.SPARCKind.XWORD) LabelRef(org.graalvm.compiler.lir.LabelRef) SPARCKind(jdk.vm.ci.sparc.SPARCKind) WORD(jdk.vm.ci.sparc.SPARCKind.WORD) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) Condition(org.graalvm.compiler.core.common.calc.Condition) HWORD(jdk.vm.ci.sparc.SPARCKind.HWORD) IfNode(org.graalvm.compiler.nodes.IfNode) LIRKind(org.graalvm.compiler.core.common.LIRKind) LIRLowerableAccess(org.graalvm.compiler.nodes.memory.LIRLowerableAccess) BYTE(jdk.vm.ci.sparc.SPARCKind.BYTE) DeoptimizingNode(org.graalvm.compiler.nodes.DeoptimizingNode) CanonicalCondition(org.graalvm.compiler.core.common.calc.CanonicalCondition) JavaConstant(jdk.vm.ci.meta.JavaConstant) ValueNode(org.graalvm.compiler.nodes.ValueNode) Value(jdk.vm.ci.meta.Value) ComplexMatchResult(org.graalvm.compiler.core.match.ComplexMatchResult) Access(org.graalvm.compiler.nodes.memory.Access) NodeMatchRules(org.graalvm.compiler.core.gen.NodeMatchRules) GraalError(org.graalvm.compiler.debug.GraalError) LIRGeneratorTool(org.graalvm.compiler.lir.gen.LIRGeneratorTool) SPARCAddressValue(org.graalvm.compiler.lir.sparc.SPARCAddressValue) MatchRule(org.graalvm.compiler.core.match.MatchRule) LogicCompareAndSwapNode(org.graalvm.compiler.nodes.java.LogicCompareAndSwapNode) Condition(org.graalvm.compiler.core.common.calc.Condition) CanonicalCondition(org.graalvm.compiler.core.common.calc.CanonicalCondition) Value(jdk.vm.ci.meta.Value) SPARCAddressValue(org.graalvm.compiler.lir.sparc.SPARCAddressValue) JavaConstant(jdk.vm.ci.meta.JavaConstant) SPARCAddressValue(org.graalvm.compiler.lir.sparc.SPARCAddressValue) LIRKind(org.graalvm.compiler.core.common.LIRKind) LabelRef(org.graalvm.compiler.lir.LabelRef) MatchRule(org.graalvm.compiler.core.match.MatchRule)

Example 7 with Condition

use of org.graalvm.compiler.core.common.calc.Condition in project graal by oracle.

the class ConditionTest method testMeet.

@Test
public void testMeet() {
    Random rand = new Random(13);
    for (Condition c1 : Condition.values()) {
        for (Condition c2 : Condition.values()) {
            Condition meet = c1.meet(c2);
            assertEquals(meet, c2.meet(c1));
            if (meet != null) {
                for (int i = 0; i < 1000; i++) {
                    JavaConstant a = JavaConstant.forInt(rand.nextInt());
                    JavaConstant b = JavaConstant.forInt(i < 100 ? a.asInt() : rand.nextInt());
                    boolean result1 = c1.foldCondition(a, b, null, false);
                    boolean result2 = c2.foldCondition(a, b, null, false);
                    boolean resultMeet = meet.foldCondition(a, b, null, false);
                    if (result1 || result2) {
                        assertTrue(resultMeet);
                    } else {
                        assertFalse(resultMeet);
                    }
                }
            }
        }
    }
}
Also used : Condition(org.graalvm.compiler.core.common.calc.Condition) Random(java.util.Random) JavaConstant(jdk.vm.ci.meta.JavaConstant) Test(org.junit.Test)

Example 8 with Condition

use of org.graalvm.compiler.core.common.calc.Condition in project graal by oracle.

the class LoopEx method detectCounted.

public boolean detectCounted() {
    LoopBeginNode loopBegin = loopBegin();
    FixedNode next = loopBegin.next();
    while (next instanceof FixedGuardNode || next instanceof ValueAnchorNode || next instanceof FullInfopointNode) {
        next = ((FixedWithNextNode) next).next();
    }
    if (next instanceof IfNode) {
        IfNode ifNode = (IfNode) next;
        boolean negated = false;
        if (!loopBegin.isLoopExit(ifNode.falseSuccessor())) {
            if (!loopBegin.isLoopExit(ifNode.trueSuccessor())) {
                return false;
            }
            negated = true;
        }
        LogicNode ifTest = ifNode.condition();
        if (!(ifTest instanceof IntegerLessThanNode) && !(ifTest instanceof IntegerEqualsNode)) {
            if (ifTest instanceof IntegerBelowNode) {
                ifTest.getDebug().log("Ignored potential Counted loop at %s with |<|", loopBegin);
            }
            return false;
        }
        CompareNode lessThan = (CompareNode) ifTest;
        Condition condition = null;
        InductionVariable iv = null;
        ValueNode limit = null;
        if (isOutsideLoop(lessThan.getX())) {
            iv = getInductionVariables().get(lessThan.getY());
            if (iv != null) {
                condition = lessThan.condition().asCondition().mirror();
                limit = lessThan.getX();
            }
        } else if (isOutsideLoop(lessThan.getY())) {
            iv = getInductionVariables().get(lessThan.getX());
            if (iv != null) {
                condition = lessThan.condition().asCondition();
                limit = lessThan.getY();
            }
        }
        if (condition == null) {
            return false;
        }
        if (negated) {
            condition = condition.negate();
        }
        boolean oneOff = false;
        switch(condition) {
            case EQ:
                return false;
            case NE:
                {
                    if (!iv.isConstantStride() || Math.abs(iv.constantStride()) != 1) {
                        return false;
                    }
                    IntegerStamp initStamp = (IntegerStamp) iv.initNode().stamp(NodeView.DEFAULT);
                    IntegerStamp limitStamp = (IntegerStamp) limit.stamp(NodeView.DEFAULT);
                    if (iv.direction() == Direction.Up) {
                        if (initStamp.upperBound() > limitStamp.lowerBound()) {
                            return false;
                        }
                    } else if (iv.direction() == Direction.Down) {
                        if (initStamp.lowerBound() < limitStamp.upperBound()) {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    break;
                }
            case LE:
                oneOff = true;
                if (iv.direction() != Direction.Up) {
                    return false;
                }
                break;
            case LT:
                if (iv.direction() != Direction.Up) {
                    return false;
                }
                break;
            case GE:
                oneOff = true;
                if (iv.direction() != Direction.Down) {
                    return false;
                }
                break;
            case GT:
                if (iv.direction() != Direction.Down) {
                    return false;
                }
                break;
            default:
                throw GraalError.shouldNotReachHere();
        }
        counted = new CountedLoopInfo(this, iv, ifNode, limit, oneOff, negated ? ifNode.falseSuccessor() : ifNode.trueSuccessor());
        return true;
    }
    return false;
}
Also used : Condition(org.graalvm.compiler.core.common.calc.Condition) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) IfNode(org.graalvm.compiler.nodes.IfNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) ValueAnchorNode(org.graalvm.compiler.nodes.extended.ValueAnchorNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode)

Example 9 with Condition

use of org.graalvm.compiler.core.common.calc.Condition in project graal by oracle.

the class AMD64LIRGenerator method emitConditionalMove.

@Override
public Variable emitConditionalMove(PlatformKind cmpKind, Value left, Value right, Condition cond, boolean unorderedIsTrue, Value trueValue, Value falseValue) {
    boolean isFloatComparison = cmpKind == AMD64Kind.SINGLE || cmpKind == AMD64Kind.DOUBLE;
    Condition finalCondition = cond;
    Value finalTrueValue = trueValue;
    Value finalFalseValue = falseValue;
    if (isFloatComparison) {
        // eliminate the parity check in case of a float comparison
        Value finalLeft = left;
        Value finalRight = right;
        if (unorderedIsTrue != AMD64ControlFlow.trueOnUnordered(finalCondition)) {
            if (unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.mirror())) {
                finalCondition = finalCondition.mirror();
                finalLeft = right;
                finalRight = left;
            } else if (finalCondition != Condition.EQ && finalCondition != Condition.NE) {
                // NaN semantics)
                assert unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.negate());
                finalCondition = finalCondition.negate();
                finalTrueValue = falseValue;
                finalFalseValue = trueValue;
            }
        }
        emitRawCompare(cmpKind, finalLeft, finalRight);
    } else {
        finalCondition = emitCompare(cmpKind, left, right, cond);
    }
    boolean isParityCheckNecessary = isFloatComparison && unorderedIsTrue != AMD64ControlFlow.trueOnUnordered(finalCondition);
    Variable result = newVariable(finalTrueValue.getValueKind());
    if (!isParityCheckNecessary && isIntConstant(finalTrueValue, 1) && isIntConstant(finalFalseValue, 0)) {
        if (isFloatComparison) {
            append(new FloatCondSetOp(result, finalCondition));
        } else {
            append(new CondSetOp(result, finalCondition));
        }
    } else if (!isParityCheckNecessary && isIntConstant(finalTrueValue, 0) && isIntConstant(finalFalseValue, 1)) {
        if (isFloatComparison) {
            if (unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.negate())) {
                append(new FloatCondSetOp(result, finalCondition.negate()));
            } else {
                append(new FloatCondSetOp(result, finalCondition));
                Variable negatedResult = newVariable(result.getValueKind());
                append(new AMD64Binary.ConstOp(AMD64BinaryArithmetic.XOR, OperandSize.get(result.getPlatformKind()), negatedResult, result, 1));
                result = negatedResult;
            }
        } else {
            append(new CondSetOp(result, finalCondition.negate()));
        }
    } else if (isFloatComparison) {
        append(new FloatCondMoveOp(result, finalCondition, unorderedIsTrue, load(finalTrueValue), load(finalFalseValue)));
    } else {
        append(new CondMoveOp(result, finalCondition, load(finalTrueValue), loadNonConst(finalFalseValue)));
    }
    return result;
}
Also used : Condition(org.graalvm.compiler.core.common.calc.Condition) Variable(org.graalvm.compiler.lir.Variable) FloatCondSetOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondSetOp) LIRValueUtil.asConstantValue(org.graalvm.compiler.lir.LIRValueUtil.asConstantValue) ConstantValue(org.graalvm.compiler.lir.ConstantValue) LIRValueUtil.isConstantValue(org.graalvm.compiler.lir.LIRValueUtil.isConstantValue) Value(jdk.vm.ci.meta.Value) ValueUtil.isAllocatableValue(jdk.vm.ci.code.ValueUtil.isAllocatableValue) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) RegisterValue(jdk.vm.ci.code.RegisterValue) AMD64AddressValue(org.graalvm.compiler.lir.amd64.AMD64AddressValue) CondMoveOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.CondMoveOp) FloatCondMoveOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondMoveOp) FloatCondMoveOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondMoveOp) CondSetOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.CondSetOp) FloatCondSetOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondSetOp)

Example 10 with Condition

use of org.graalvm.compiler.core.common.calc.Condition in project graal by oracle.

the class AMD64NodeMatchRules method ifCompareLogicCas.

@MatchRule("(If (ObjectEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (PointerEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (FloatEquals=compare value LogicCompareAndSwap=cas))")
@MatchRule("(If (IntegerEquals=compare value LogicCompareAndSwap=cas))")
public ComplexMatchResult ifCompareLogicCas(IfNode root, CompareNode compare, ValueNode value, LogicCompareAndSwapNode cas) {
    JavaConstant constant = value.asJavaConstant();
    assert compare.condition() == CanonicalCondition.EQ;
    if (constant != null && cas.usages().count() == 1) {
        long constantValue = constant.asLong();
        boolean successIsTrue;
        if (constantValue == 0) {
            successIsTrue = false;
        } else if (constantValue == 1) {
            successIsTrue = true;
        } else {
            return null;
        }
        return builder -> {
            LIRKind kind = getLirKind(cas);
            LabelRef trueLabel = getLIRBlock(root.trueSuccessor());
            LabelRef falseLabel = getLIRBlock(root.falseSuccessor());
            double trueLabelProbability = root.probability(root.trueSuccessor());
            Value expectedValue = operand(cas.getExpectedValue());
            Value newValue = operand(cas.getNewValue());
            AMD64AddressValue address = (AMD64AddressValue) operand(cas.getAddress());
            Condition condition = successIsTrue ? Condition.EQ : Condition.NE;
            getLIRGeneratorTool().emitCompareAndSwapBranch(kind, address, expectedValue, newValue, condition, trueLabel, falseLabel, trueLabelProbability);
            return null;
        };
    }
    return null;
}
Also used : OperandSize(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize) AMD64RMOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp) AVXOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AVXOp) AMD64BinaryConsumer(org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer) NarrowNode(org.graalvm.compiler.nodes.calc.NarrowNode) LabelRef(org.graalvm.compiler.lir.LabelRef) UnsignedRightShiftNode(org.graalvm.compiler.nodes.calc.UnsignedRightShiftNode) SUB(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.SUB) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) FloatConvertNode(org.graalvm.compiler.nodes.calc.FloatConvertNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) AMD64Kind(jdk.vm.ci.amd64.AMD64Kind) NumUtil(org.graalvm.compiler.core.common.NumUtil) IfNode(org.graalvm.compiler.nodes.IfNode) GraphUtil(org.graalvm.compiler.nodes.util.GraphUtil) NodeView(org.graalvm.compiler.nodes.NodeView) LIRLowerableAccess(org.graalvm.compiler.nodes.memory.LIRLowerableAccess) BranchOp(org.graalvm.compiler.lir.amd64.AMD64ControlFlow.BranchOp) MOVSX(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp.MOVSX) NodeLIRBuilder(org.graalvm.compiler.core.gen.NodeLIRBuilder) DeoptimizingNode(org.graalvm.compiler.nodes.DeoptimizingNode) TargetDescription(jdk.vm.ci.code.TargetDescription) JavaConstant(jdk.vm.ci.meta.JavaConstant) PlatformKind(jdk.vm.ci.meta.PlatformKind) ValueNode(org.graalvm.compiler.nodes.ValueNode) Value(jdk.vm.ci.meta.Value) ComplexMatchResult(org.graalvm.compiler.core.match.ComplexMatchResult) Access(org.graalvm.compiler.nodes.memory.Access) ADD(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.ADD) SS(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize.SS) DWORD(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize.DWORD) GraalError(org.graalvm.compiler.debug.GraalError) ValueKind(jdk.vm.ci.meta.ValueKind) MatchRule(org.graalvm.compiler.core.match.MatchRule) LogicCompareAndSwapNode(org.graalvm.compiler.nodes.java.LogicCompareAndSwapNode) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) LeftShiftNode(org.graalvm.compiler.nodes.calc.LeftShiftNode) MOVSXB(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp.MOVSXB) WriteNode(org.graalvm.compiler.nodes.memory.WriteNode) LIRFrameState(org.graalvm.compiler.lir.LIRFrameState) SD(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize.SD) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) ReinterpretNode(org.graalvm.compiler.nodes.calc.ReinterpretNode) AMD64RRMOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RRMOp) AMD64(jdk.vm.ci.amd64.AMD64) AMD64MIOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64MIOp) ValueCompareAndSwapNode(org.graalvm.compiler.nodes.java.ValueCompareAndSwapNode) MOVSXD(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp.MOVSXD) Condition(org.graalvm.compiler.core.common.calc.Condition) CPUFeature(jdk.vm.ci.amd64.AMD64.CPUFeature) LIRKind(org.graalvm.compiler.core.common.LIRKind) CanonicalCondition(org.graalvm.compiler.core.common.calc.CanonicalCondition) SSEOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.SSEOp) OR(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.OR) LIRValueUtil(org.graalvm.compiler.lir.LIRValueUtil) NodeMatchRules(org.graalvm.compiler.core.gen.NodeMatchRules) XOR(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.XOR) LIRGeneratorTool(org.graalvm.compiler.lir.gen.LIRGeneratorTool) AND(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.AND) AMD64AddressValue(org.graalvm.compiler.lir.amd64.AMD64AddressValue) QWORD(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize.QWORD) AMD64AddressValue(org.graalvm.compiler.lir.amd64.AMD64AddressValue) Condition(org.graalvm.compiler.core.common.calc.Condition) CanonicalCondition(org.graalvm.compiler.core.common.calc.CanonicalCondition) Value(jdk.vm.ci.meta.Value) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) AMD64AddressValue(org.graalvm.compiler.lir.amd64.AMD64AddressValue) JavaConstant(jdk.vm.ci.meta.JavaConstant) LIRKind(org.graalvm.compiler.core.common.LIRKind) LabelRef(org.graalvm.compiler.lir.LabelRef) MatchRule(org.graalvm.compiler.core.match.MatchRule)

Aggregations

Condition (org.graalvm.compiler.core.common.calc.Condition)12 JavaConstant (jdk.vm.ci.meta.JavaConstant)6 Value (jdk.vm.ci.meta.Value)5 AllocatableValue (jdk.vm.ci.meta.AllocatableValue)4 Random (java.util.Random)3 CanonicalCondition (org.graalvm.compiler.core.common.calc.CanonicalCondition)3 AMD64AddressValue (org.graalvm.compiler.lir.amd64.AMD64AddressValue)3 IfNode (org.graalvm.compiler.nodes.IfNode)3 ValueNode (org.graalvm.compiler.nodes.ValueNode)3 CompareNode (org.graalvm.compiler.nodes.calc.CompareNode)3 AMD64Kind (jdk.vm.ci.amd64.AMD64Kind)2 SPARCKind (jdk.vm.ci.sparc.SPARCKind)2 ConditionFlag (org.graalvm.compiler.asm.aarch64.AArch64Assembler.ConditionFlag)2 LIRKind (org.graalvm.compiler.core.common.LIRKind)2 ComplexMatchResult (org.graalvm.compiler.core.match.ComplexMatchResult)2 LabelRef (org.graalvm.compiler.lir.LabelRef)2 SPARCAddressValue (org.graalvm.compiler.lir.sparc.SPARCAddressValue)2 Test (org.junit.Test)2 AMD64 (jdk.vm.ci.amd64.AMD64)1 CPUFeature (jdk.vm.ci.amd64.AMD64.CPUFeature)1