Search in sources :

Example 1 with IntegerEqualsNode

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

the class BranchProbabilityNode method simplify.

@Override
public void simplify(SimplifierTool tool) {
    if (!hasUsages()) {
        return;
    }
    if (probability.isConstant()) {
        double probabilityValue = probability.asJavaConstant().asDouble();
        if (probabilityValue < 0.0) {
            throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!");
        } else if (probabilityValue > 1.0) {
            throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!");
        } else if (Double.isNaN(probabilityValue)) {
            /*
                 * We allow NaN if the node is in unreachable code that will eventually fall away,
                 * or else an error will be thrown during lowering since we keep the node around.
                 */
            return;
        }
        boolean usageFound = false;
        for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) {
            assert node.condition() == CanonicalCondition.EQ;
            ValueNode other = node.getX();
            if (node.getX() == this) {
                other = node.getY();
            }
            if (other.isConstant()) {
                double probabilityToSet = probabilityValue;
                if (other.asJavaConstant().asInt() == 0) {
                    probabilityToSet = 1.0 - probabilityToSet;
                }
                for (IfNode ifNodeUsages : node.usages().filter(IfNode.class)) {
                    usageFound = true;
                    ifNodeUsages.setTrueSuccessorProbability(probabilityToSet);
                }
                if (!usageFound) {
                    usageFound = node.usages().filter(NodePredicates.isA(FixedGuardNode.class).or(ConditionalNode.class)).isNotEmpty();
                }
            }
        }
        if (usageFound) {
            ValueNode currentCondition = condition;
            replaceAndDelete(currentCondition);
            if (tool != null) {
                tool.addToWorkList(currentCondition.usages());
            }
        } else {
            if (!isSubstitutionGraph()) {
                throw new GraalError("Wrong usage of branch probability injection!");
            }
        }
    }
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) GraalError(org.graalvm.compiler.debug.GraalError) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) IfNode(org.graalvm.compiler.nodes.IfNode)

Example 2 with IntegerEqualsNode

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

the class BytecodeParser method genIf.

protected void genIf(ValueNode x, Condition cond, ValueNode y) {
    assert x.getStackKind() == y.getStackKind();
    assert currentBlock.getSuccessorCount() == 2;
    BciBlock trueBlock = currentBlock.getSuccessor(0);
    BciBlock falseBlock = currentBlock.getSuccessor(1);
    if (trueBlock == falseBlock) {
        // The target block is the same independent of the condition.
        appendGoto(trueBlock);
        return;
    }
    ValueNode a = x;
    ValueNode b = y;
    BciBlock trueSuccessor = trueBlock;
    BciBlock falseSuccessor = falseBlock;
    CanonicalizedCondition canonicalizedCondition = cond.canonicalize();
    // Check whether the condition needs to mirror the operands.
    if (canonicalizedCondition.mustMirror()) {
        a = y;
        b = x;
    }
    if (canonicalizedCondition.mustNegate()) {
        trueSuccessor = falseBlock;
        falseSuccessor = trueBlock;
    }
    // Create the logic node for the condition.
    LogicNode condition = createLogicNode(canonicalizedCondition.getCanonicalCondition(), a, b);
    double probability = -1;
    if (condition instanceof IntegerEqualsNode) {
        probability = extractInjectedProbability((IntegerEqualsNode) condition);
    // the probability coming from here is about the actual condition
    }
    if (probability == -1) {
        probability = getProfileProbability(canonicalizedCondition.mustNegate());
    }
    probability = clampProbability(probability);
    genIf(condition, trueSuccessor, falseSuccessor, probability);
}
Also used : CanonicalizedCondition(org.graalvm.compiler.core.common.calc.Condition.CanonicalizedCondition) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) BciBlock(org.graalvm.compiler.java.BciBlockMapping.BciBlock)

Example 3 with IntegerEqualsNode

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

the class WordOperationPlugin method comparisonOp.

private ValueNode comparisonOp(GraphBuilderContext graph, Condition condition, ValueNode left, ValueNode right) {
    assert left.getStackKind() == wordKind && right.getStackKind() == wordKind;
    CanonicalizedCondition canonical = condition.canonicalize();
    ValueNode a = canonical.mustMirror() ? right : left;
    ValueNode b = canonical.mustMirror() ? left : right;
    CompareNode comparison;
    if (canonical.getCanonicalCondition() == CanonicalCondition.EQ) {
        comparison = new IntegerEqualsNode(a, b);
    } else if (canonical.getCanonicalCondition() == CanonicalCondition.BT) {
        comparison = new IntegerBelowNode(a, b);
    } else {
        assert canonical.getCanonicalCondition() == CanonicalCondition.LT;
        comparison = new IntegerLessThanNode(a, b);
    }
    ConstantNode trueValue = graph.add(forInt(1));
    ConstantNode falseValue = graph.add(forInt(0));
    if (canonical.mustNegate()) {
        ConstantNode temp = trueValue;
        trueValue = falseValue;
        falseValue = temp;
    }
    return graph.add(new ConditionalNode(graph.add(comparison), trueValue, falseValue));
}
Also used : CanonicalizedCondition(org.graalvm.compiler.core.common.calc.Condition.CanonicalizedCondition) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode)

Example 4 with IntegerEqualsNode

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

the class CInterfaceInvocationPlugin method adaptPrimitiveType.

static ValueNode adaptPrimitiveType(StructuredGraph graph, ValueNode value, JavaKind fromKind, JavaKind toKind, boolean isUnsigned) {
    if (fromKind == toKind) {
        return value;
    }
    assert fromKind.isNumericFloat() == toKind.isNumericFloat();
    int fromBits = fromKind.getBitCount();
    int toBits = toKind.getBitCount();
    if (fromBits == toBits) {
        return value;
    } else if (fromKind.isNumericFloat()) {
        FloatConvert op;
        if (fromKind == JavaKind.Float && toKind == JavaKind.Double) {
            op = FloatConvert.F2D;
        } else if (fromKind == JavaKind.Double && toKind == JavaKind.Float) {
            op = FloatConvert.D2F;
        } else {
            throw shouldNotReachHere();
        }
        return graph.unique(new FloatConvertNode(op, value));
    } else if (toKind == JavaKind.Boolean) {
        JavaKind computeKind = fromKind == JavaKind.Long ? JavaKind.Long : JavaKind.Int;
        LogicNode comparison = graph.unique(new IntegerEqualsNode(adaptPrimitiveType(graph, value, fromKind, computeKind, true), ConstantNode.forIntegerKind(computeKind, 0, graph)));
        return graph.unique(new ConditionalNode(comparison, ConstantNode.forBoolean(false, graph), ConstantNode.forBoolean(true, graph)));
    } else if (fromBits > toBits) {
        return graph.unique(new NarrowNode(value, toBits));
    } else if (isUnsigned) {
        return graph.unique(new ZeroExtendNode(value, toBits));
    } else {
        return graph.unique(new SignExtendNode(value, toBits));
    }
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) FloatConvertNode(org.graalvm.compiler.nodes.calc.FloatConvertNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) FloatConvert(org.graalvm.compiler.core.common.calc.FloatConvert) LogicNode(org.graalvm.compiler.nodes.LogicNode) NarrowNode(org.graalvm.compiler.nodes.calc.NarrowNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 5 with IntegerEqualsNode

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

the class ShortCircuitOrNodeTest method registerInvocationPlugins.

@Override
protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
    Registration r = new Registration(invocationPlugins, ShortCircuitOrNodeTest.class);
    r.register2("shortCircuitOr", boolean.class, boolean.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode b1, ValueNode b2) {
            LogicNode x = b.add(new IntegerEqualsNode(b1, b.add(ConstantNode.forInt(1))));
            LogicNode y = b.add(new IntegerEqualsNode(b2, b.add(ConstantNode.forInt(1))));
            ShortCircuitOrNode compare = b.add(new ShortCircuitOrNode(x, false, y, false, 0.5));
            b.addPush(JavaKind.Boolean, new ConditionalNode(compare, b.add(ConstantNode.forBoolean(true)), b.add(ConstantNode.forBoolean(false))));
            return true;
        }
    });
    super.registerInvocationPlugins(invocationPlugins);
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) ShortCircuitOrNode(org.graalvm.compiler.nodes.ShortCircuitOrNode) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) LogicNode(org.graalvm.compiler.nodes.LogicNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

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