Search in sources :

Example 1 with ValuePhiNode

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

the class FinalizeProfileNodesPhase method assignRandomSources.

private static void assignRandomSources(StructuredGraph graph) {
    ValueNode seed = graph.unique(new RandomSeedNode());
    ControlFlowGraph cfg = ControlFlowGraph.compute(graph, false, true, false, false);
    Map<LoopBeginNode, ValueNode> loopRandomValueCache = new HashMap<>();
    for (ProfileNode node : getProfileNodes(graph)) {
        ValueNode random;
        Block block = cfg.blockFor(node);
        Loop<Block> loop = block.getLoop();
        // pseudo-random number generator into the loop
        if (loop != null) {
            LoopBeginNode loopBegin = (LoopBeginNode) loop.getHeader().getBeginNode();
            random = loopRandomValueCache.get(loopBegin);
            if (random == null) {
                PhiNode phi = graph.addWithoutUnique(new ValuePhiNode(seed.stamp(NodeView.DEFAULT), loopBegin));
                phi.addInput(seed);
                // X_{n+1} = a*X_n + c, using glibc-like constants
                ValueNode a = ConstantNode.forInt(1103515245, graph);
                ValueNode c = ConstantNode.forInt(12345, graph);
                ValueNode next = graph.addOrUniqueWithInputs(new AddNode(c, new MulNode(phi, a)));
                for (int i = 0; i < loopBegin.getLoopEndCount(); i++) {
                    phi.addInput(next);
                }
                random = phi;
                loopRandomValueCache.put(loopBegin, random);
            }
        } else {
            // Graal doesn't compile methods with irreducible loops. So all profile nodes that
            // are not in a loop are guaranteed to be executed at most once. We feed the seed
            // value to such nodes directly.
            random = seed;
        }
        node.setRandom(random);
    }
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) HashMap(java.util.HashMap) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) ProfileNode(org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) RandomSeedNode(org.graalvm.compiler.hotspot.nodes.profiling.RandomSeedNode) MulNode(org.graalvm.compiler.nodes.calc.MulNode) AddNode(org.graalvm.compiler.nodes.calc.AddNode)

Example 2 with ValuePhiNode

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

the class DeoptimizationGroupingPhase method run.

@Override
protected void run(StructuredGraph graph, MidTierContext context) {
    ControlFlowGraph cfg = null;
    for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
        FixedNode target = null;
        PhiNode reasonActionPhi = null;
        PhiNode speculationPhi = null;
        List<AbstractDeoptimizeNode> obsoletes = null;
        for (AbstractDeoptimizeNode deopt : fs.usages().filter(AbstractDeoptimizeNode.class)) {
            if (target == null) {
                target = deopt;
            } else {
                if (cfg == null) {
                    cfg = ControlFlowGraph.compute(graph, true, true, false, false);
                }
                AbstractMergeNode merge;
                if (target instanceof AbstractDeoptimizeNode) {
                    merge = graph.add(new MergeNode());
                    EndNode firstEnd = graph.add(new EndNode());
                    ValueNode actionAndReason = ((AbstractDeoptimizeNode) target).getActionAndReason(context.getMetaAccess());
                    ValueNode speculation = ((AbstractDeoptimizeNode) target).getSpeculation(context.getMetaAccess());
                    reasonActionPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(actionAndReason.getStackKind()), merge));
                    speculationPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(speculation.getStackKind()), merge));
                    merge.addForwardEnd(firstEnd);
                    reasonActionPhi.addInput(actionAndReason);
                    speculationPhi.addInput(speculation);
                    target.replaceAtPredecessor(firstEnd);
                    exitLoops((AbstractDeoptimizeNode) target, firstEnd, cfg);
                    merge.setNext(graph.add(new DynamicDeoptimizeNode(reasonActionPhi, speculationPhi)));
                    obsoletes = new LinkedList<>();
                    obsoletes.add((AbstractDeoptimizeNode) target);
                    target = merge;
                } else {
                    merge = (AbstractMergeNode) target;
                }
                EndNode newEnd = graph.add(new EndNode());
                merge.addForwardEnd(newEnd);
                reasonActionPhi.addInput(deopt.getActionAndReason(context.getMetaAccess()));
                speculationPhi.addInput(deopt.getSpeculation(context.getMetaAccess()));
                deopt.replaceAtPredecessor(newEnd);
                exitLoops(deopt, newEnd, cfg);
                obsoletes.add(deopt);
            }
        }
        if (obsoletes != null) {
            ((DynamicDeoptimizeNode) ((AbstractMergeNode) target).next()).setStateBefore(fs);
            for (AbstractDeoptimizeNode obsolete : obsoletes) {
                obsolete.safeDelete();
            }
        }
    }
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) DynamicDeoptimizeNode(org.graalvm.compiler.nodes.DynamicDeoptimizeNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) FrameState(org.graalvm.compiler.nodes.FrameState) AbstractDeoptimizeNode(org.graalvm.compiler.nodes.AbstractDeoptimizeNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) MergeNode(org.graalvm.compiler.nodes.MergeNode) EndNode(org.graalvm.compiler.nodes.EndNode) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Example 3 with ValuePhiNode

use of org.graalvm.compiler.nodes.ValuePhiNode 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 4 with ValuePhiNode

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

the class UnsafeEATest method testMergedDouble.

@Test
public void testMergedDouble() {
    testEscapeAnalysis("testMergedDoubleSnippet", null, false);
    Assert.assertEquals(1, returnNodes.size());
    Assert.assertTrue(returnNodes.get(0).result() instanceof ValuePhiNode);
    PhiNode phi = (PhiNode) returnNodes.get(0).result();
    Assert.assertTrue(phi.valueAt(0) instanceof LoadFieldNode);
    Assert.assertTrue(phi.valueAt(1) instanceof LoadFieldNode);
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) LoadFieldNode(org.graalvm.compiler.nodes.java.LoadFieldNode) Test(org.junit.Test)

Example 5 with ValuePhiNode

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

the class EAMergingTest method testSimpleMerge.

@Test
public void testSimpleMerge() {
    testEscapeAnalysis("simpleMergeSnippet", null, false);
    assertDeepEquals(1, returnNodes.size());
    assertTrue(returnNodes.get(0).result() instanceof ValuePhiNode);
    PhiNode phi = (PhiNode) returnNodes.get(0).result();
    assertTrue(phi.valueAt(0) instanceof ParameterNode);
    assertTrue(phi.valueAt(1) instanceof ParameterNode);
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) Test(org.junit.Test)

Aggregations

ValuePhiNode (org.graalvm.compiler.nodes.ValuePhiNode)24 ValueNode (org.graalvm.compiler.nodes.ValueNode)18 PhiNode (org.graalvm.compiler.nodes.PhiNode)12 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)11 Node (org.graalvm.compiler.graph.Node)9 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)8 FixedNode (org.graalvm.compiler.nodes.FixedNode)8 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)7 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)6 LogicNode (org.graalvm.compiler.nodes.LogicNode)6 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)5 AbstractEndNode (org.graalvm.compiler.nodes.AbstractEndNode)5 EndNode (org.graalvm.compiler.nodes.EndNode)5 JavaKind (jdk.vm.ci.meta.JavaKind)4 IfNode (org.graalvm.compiler.nodes.IfNode)4 ArrayList (java.util.ArrayList)3 DeoptimizeNode (org.graalvm.compiler.nodes.DeoptimizeNode)3 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)3 LogicConstantNode (org.graalvm.compiler.nodes.LogicConstantNode)3 MergeNode (org.graalvm.compiler.nodes.MergeNode)3