Search in sources :

Example 31 with ValueNode

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

the class InliningUtil method nonNullReceiver.

/**
 * Gets the receiver for an invoke, adding a guard if necessary to ensure it is non-null, and
 * ensuring that the resulting type is compatible with the method being invoked.
 */
@SuppressWarnings("try")
public static ValueNode nonNullReceiver(Invoke invoke) {
    try (DebugCloseable position = invoke.asNode().withNodeSourcePosition()) {
        MethodCallTargetNode callTarget = (MethodCallTargetNode) invoke.callTarget();
        assert !callTarget.isStatic() : callTarget.targetMethod();
        StructuredGraph graph = callTarget.graph();
        ValueNode oldReceiver = callTarget.arguments().get(0);
        ValueNode newReceiver = oldReceiver;
        if (newReceiver.getStackKind() == JavaKind.Object) {
            if (invoke.getInvokeKind() == InvokeKind.Special) {
                Stamp paramStamp = newReceiver.stamp(NodeView.DEFAULT);
                Stamp stamp = paramStamp.join(StampFactory.object(TypeReference.create(graph.getAssumptions(), callTarget.targetMethod().getDeclaringClass())));
                if (!stamp.equals(paramStamp)) {
                    // The verifier and previous optimizations guarantee unconditionally that
                    // the
                    // receiver is at least of the type of the method holder for a special
                    // invoke.
                    newReceiver = graph.unique(new PiNode(newReceiver, stamp));
                }
            }
            if (!StampTool.isPointerNonNull(newReceiver)) {
                LogicNode condition = graph.unique(IsNullNode.create(newReceiver));
                FixedGuardNode fixedGuard = graph.add(new FixedGuardNode(condition, NullCheckException, InvalidateReprofile, true));
                PiNode nonNullReceiver = graph.unique(new PiNode(newReceiver, StampFactory.objectNonNull(), fixedGuard));
                graph.addBeforeFixed(invoke.asNode(), fixedGuard);
                newReceiver = nonNullReceiver;
            }
        }
        if (newReceiver != oldReceiver) {
            callTarget.replaceFirstInput(oldReceiver, newReceiver);
        }
        return newReceiver;
    }
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) Stamp(org.graalvm.compiler.core.common.type.Stamp) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) DebugCloseable(org.graalvm.compiler.debug.DebugCloseable) PiNode(org.graalvm.compiler.nodes.PiNode)

Example 32 with ValueNode

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

the class MultiTypeGuardInlineInfo method inlineMultipleMethods.

private EconomicSet<Node> inlineMultipleMethods(StructuredGraph graph, Providers providers) {
    int numberOfMethods = concretes.size();
    FixedNode continuation = invoke.next();
    // setup merge and phi nodes for results and exceptions
    AbstractMergeNode returnMerge = graph.add(new MergeNode());
    returnMerge.setStateAfter(invoke.stateAfter());
    PhiNode returnValuePhi = null;
    if (invoke.asNode().getStackKind() != JavaKind.Void) {
        returnValuePhi = graph.addWithoutUnique(new ValuePhiNode(invoke.asNode().stamp(NodeView.DEFAULT).unrestricted(), returnMerge));
    }
    AbstractMergeNode exceptionMerge = null;
    PhiNode exceptionObjectPhi = null;
    if (invoke instanceof InvokeWithExceptionNode) {
        InvokeWithExceptionNode invokeWithException = (InvokeWithExceptionNode) invoke;
        ExceptionObjectNode exceptionEdge = (ExceptionObjectNode) invokeWithException.exceptionEdge();
        exceptionMerge = graph.add(new MergeNode());
        FixedNode exceptionSux = exceptionEdge.next();
        graph.addBeforeFixed(exceptionSux, exceptionMerge);
        exceptionObjectPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(JavaKind.Object), exceptionMerge));
        exceptionMerge.setStateAfter(exceptionEdge.stateAfter().duplicateModified(invoke.stateAfter().bci, true, JavaKind.Object, new JavaKind[] { JavaKind.Object }, new ValueNode[] { exceptionObjectPhi }));
    }
    // create one separate block for each invoked method
    AbstractBeginNode[] successors = new AbstractBeginNode[numberOfMethods + 1];
    for (int i = 0; i < numberOfMethods; i++) {
        successors[i] = createInvocationBlock(graph, invoke, returnMerge, returnValuePhi, exceptionMerge, exceptionObjectPhi, true);
    }
    // create the successor for an unknown type
    FixedNode unknownTypeSux;
    if (shouldFallbackToInvoke()) {
        unknownTypeSux = createInvocationBlock(graph, invoke, returnMerge, returnValuePhi, exceptionMerge, exceptionObjectPhi, false);
    } else {
        unknownTypeSux = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TypeCheckedInliningViolated));
    }
    successors[successors.length - 1] = BeginNode.begin(unknownTypeSux);
    // replace the invoke exception edge
    if (invoke instanceof InvokeWithExceptionNode) {
        InvokeWithExceptionNode invokeWithExceptionNode = (InvokeWithExceptionNode) invoke;
        ExceptionObjectNode exceptionEdge = (ExceptionObjectNode) invokeWithExceptionNode.exceptionEdge();
        exceptionEdge.replaceAtUsages(exceptionObjectPhi);
        exceptionEdge.setNext(null);
        GraphUtil.killCFG(invokeWithExceptionNode.exceptionEdge());
    }
    assert invoke.asNode().isAlive();
    // replace the invoke with a switch on the type of the actual receiver
    boolean methodDispatch = createDispatchOnTypeBeforeInvoke(graph, successors, false, providers.getStampProvider(), providers.getConstantReflection());
    assert invoke.next() == continuation;
    invoke.setNext(null);
    returnMerge.setNext(continuation);
    if (returnValuePhi != null) {
        invoke.asNode().replaceAtUsages(returnValuePhi);
    }
    invoke.asNode().safeDelete();
    ArrayList<PiNode> replacementNodes = new ArrayList<>();
    // prepare the anchors for the invokes
    for (int i = 0; i < numberOfMethods; i++) {
        AbstractBeginNode node = successors[i];
        Invoke invokeForInlining = (Invoke) node.next();
        ResolvedJavaType commonType;
        if (methodDispatch) {
            commonType = concretes.get(i).getDeclaringClass();
        } else {
            commonType = getLeastCommonType(i);
        }
        ValueNode receiver = ((MethodCallTargetNode) invokeForInlining.callTarget()).receiver();
        boolean exact = (getTypeCount(i) == 1 && !methodDispatch);
        PiNode anchoredReceiver = InliningUtil.createAnchoredReceiver(graph, node, commonType, receiver, exact);
        invokeForInlining.callTarget().replaceFirstInput(receiver, anchoredReceiver);
        assert !anchoredReceiver.isDeleted() : anchoredReceiver;
        replacementNodes.add(anchoredReceiver);
    }
    if (shouldFallbackToInvoke()) {
        replacementNodes.add(null);
    }
    EconomicSet<Node> canonicalizeNodes = EconomicSet.create(Equivalence.DEFAULT);
    // do the actual inlining for every invoke
    for (int i = 0; i < numberOfMethods; i++) {
        Invoke invokeForInlining = (Invoke) successors[i].next();
        canonicalizeNodes.addAll(doInline(i, invokeForInlining));
    }
    if (returnValuePhi != null) {
        canonicalizeNodes.add(returnValuePhi);
    }
    return canonicalizeNodes;
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) ExceptionObjectNode(org.graalvm.compiler.nodes.java.ExceptionObjectNode) LoadHubNode(org.graalvm.compiler.nodes.extended.LoadHubNode) BeginNode(org.graalvm.compiler.nodes.BeginNode) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) PiNode(org.graalvm.compiler.nodes.PiNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) TypeSwitchNode(org.graalvm.compiler.nodes.java.TypeSwitchNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ExceptionObjectNode(org.graalvm.compiler.nodes.java.ExceptionObjectNode) ArrayList(java.util.ArrayList) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) PiNode(org.graalvm.compiler.nodes.PiNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) Invoke(org.graalvm.compiler.nodes.Invoke) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 33 with ValueNode

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

the class MultiTypeGuardInlineInfo method createDispatchOnTypeBeforeInvoke.

private boolean createDispatchOnTypeBeforeInvoke(StructuredGraph graph, AbstractBeginNode[] successors, boolean invokeIsOnlySuccessor, StampProvider stampProvider, ConstantReflectionProvider constantReflection) {
    assert ptypes.size() >= 1;
    ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke);
    LoadHubNode hub = graph.unique(new LoadHubNode(stampProvider, nonNullReceiver));
    graph.getDebug().log("Type switch with %d types", concretes.size());
    ResolvedJavaType[] keys = new ResolvedJavaType[ptypes.size()];
    double[] keyProbabilities = new double[ptypes.size() + 1];
    int[] keySuccessors = new int[ptypes.size() + 1];
    double totalProbability = notRecordedTypeProbability;
    for (int i = 0; i < ptypes.size(); i++) {
        keys[i] = ptypes.get(i).getType();
        keyProbabilities[i] = ptypes.get(i).getProbability();
        totalProbability += keyProbabilities[i];
        keySuccessors[i] = invokeIsOnlySuccessor ? 0 : typesToConcretes.get(i);
        assert keySuccessors[i] < successors.length - 1 : "last successor is the unknownTypeSux";
    }
    keyProbabilities[keyProbabilities.length - 1] = notRecordedTypeProbability;
    keySuccessors[keySuccessors.length - 1] = successors.length - 1;
    // Normalize the probabilities.
    for (int i = 0; i < keyProbabilities.length; i++) {
        keyProbabilities[i] /= totalProbability;
    }
    TypeSwitchNode typeSwitch = graph.add(new TypeSwitchNode(hub, successors, keys, keyProbabilities, keySuccessors, constantReflection));
    FixedWithNextNode pred = (FixedWithNextNode) invoke.asNode().predecessor();
    pred.setNext(typeSwitch);
    return false;
}
Also used : FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) LoadHubNode(org.graalvm.compiler.nodes.extended.LoadHubNode) TypeSwitchNode(org.graalvm.compiler.nodes.java.TypeSwitchNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 34 with ValueNode

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

the class StampTool method meetOrNull.

/**
 * Meet a collection of {@link ValueNode}s optionally excluding {@code selfValue}. If no values
 * are encountered then return {@code null}.
 */
public static Stamp meetOrNull(Iterable<? extends ValueNode> values, ValueNode selfValue) {
    Iterator<? extends ValueNode> iterator = values.iterator();
    Stamp stamp = null;
    while (iterator.hasNext()) {
        ValueNode nextValue = iterator.next();
        if (nextValue != selfValue) {
            if (stamp == null) {
                stamp = nextValue.stamp(NodeView.DEFAULT);
            } else {
                stamp = stamp.meet(nextValue.stamp(NodeView.DEFAULT));
            }
        }
    }
    return stamp;
}
Also used : AbstractObjectStamp(org.graalvm.compiler.core.common.type.AbstractObjectStamp) AbstractPointerStamp(org.graalvm.compiler.core.common.type.AbstractPointerStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Example 35 with ValueNode

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

the class GraphUtil method checkRedundantPhi.

public static void checkRedundantPhi(PhiNode phiNode) {
    if (phiNode.isDeleted() || phiNode.valueCount() == 1) {
        return;
    }
    ValueNode singleValue = phiNode.singleValueOrThis();
    if (singleValue != phiNode) {
        Collection<PhiNode> phiUsages = phiNode.usages().filter(PhiNode.class).snapshot();
        Collection<ProxyNode> proxyUsages = phiNode.usages().filter(ProxyNode.class).snapshot();
        phiNode.replaceAtUsagesAndDelete(singleValue);
        for (PhiNode phi : phiUsages) {
            checkRedundantPhi(phi);
        }
        for (ProxyNode proxy : proxyUsages) {
            checkRedundantProxy(proxy);
        }
    }
}
Also used : ProxyNode(org.graalvm.compiler.nodes.ProxyNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Aggregations

ValueNode (org.graalvm.compiler.nodes.ValueNode)482 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)104 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)77 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)76 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)69 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)67 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)66 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)60 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)52 Node (org.graalvm.compiler.graph.Node)50 JavaKind (jdk.vm.ci.meta.JavaKind)48 Stamp (org.graalvm.compiler.core.common.type.Stamp)48 LogicNode (org.graalvm.compiler.nodes.LogicNode)48 VirtualObjectNode (org.graalvm.compiler.nodes.virtual.VirtualObjectNode)42 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)38 FixedNode (org.graalvm.compiler.nodes.FixedNode)37 AddressNode (org.graalvm.compiler.nodes.memory.address.AddressNode)37 NodeView (org.graalvm.compiler.nodes.NodeView)36 PhiNode (org.graalvm.compiler.nodes.PhiNode)36 Test (org.junit.Test)36