Search in sources :

Example 1 with FixedGuardNode

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

the class NewArrayNode method simplify.

@Override
public void simplify(SimplifierTool tool) {
    if (hasNoUsages()) {
        NodeView view = NodeView.from(tool);
        Stamp lengthStamp = length().stamp(view);
        if (lengthStamp instanceof IntegerStamp) {
            IntegerStamp lengthIntegerStamp = (IntegerStamp) lengthStamp;
            if (lengthIntegerStamp.isPositive()) {
                GraphUtil.removeFixedWithUnusedInputs(this);
                return;
            }
        }
        // RuntimeConstraint
        if (graph().getGuardsStage().allowsFloatingGuards()) {
            LogicNode lengthNegativeCondition = CompareNode.createCompareNode(graph(), CanonicalCondition.LT, length(), ConstantNode.forInt(0, graph()), tool.getConstantReflection(), view);
            // we do not have a non-deopting path for that at the moment so action=None.
            FixedGuardNode guard = graph().add(new FixedGuardNode(lengthNegativeCondition, DeoptimizationReason.RuntimeConstraint, DeoptimizationAction.None, true));
            graph().replaceFixedWithFixed(this, guard);
        }
    }
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) LogicNode(org.graalvm.compiler.nodes.LogicNode) NodeView(org.graalvm.compiler.nodes.NodeView)

Example 2 with FixedGuardNode

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

the class TruffleGraphBuilderPlugins method registerCompilerDirectivesPlugins.

public static void registerCompilerDirectivesPlugins(InvocationPlugins plugins, MetaAccessProvider metaAccess, boolean canDelayIntrinsification) {
    final ResolvedJavaType compilerDirectivesType = getRuntime().resolveType(metaAccess, "com.oracle.truffle.api.CompilerDirectives");
    Registration r = new Registration(plugins, new ResolvedJavaSymbol(compilerDirectivesType));
    r.register0("inInterpreter", new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(false));
            return true;
        }
    });
    r.register0("inCompiledCode", new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true));
            return true;
        }
    });
    r.register0("inCompilationRoot", new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            GraphBuilderContext.ExternalInliningContext inliningContext = b.getExternalInliningContext();
            if (inliningContext != null) {
                b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(inliningContext.getInlinedDepth() == 0));
                return true;
            }
            return false;
        }
    });
    r.register0("transferToInterpreter", new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            b.add(new DeoptimizeNode(DeoptimizationAction.None, DeoptimizationReason.TransferToInterpreter));
            return true;
        }
    });
    r.register0("transferToInterpreterAndInvalidate", new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            b.add(new DeoptimizeNode(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.TransferToInterpreter));
            return true;
        }
    });
    r.register1("interpreterOnly", Runnable.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
            return true;
        }
    });
    r.register1("interpreterOnly", Callable.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg) {
            return true;
        }
    });
    r.register2("injectBranchProbability", double.class, boolean.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode probability, ValueNode condition) {
            b.addPush(JavaKind.Boolean, new BranchProbabilityNode(probability, condition));
            return true;
        }
    });
    r.register1("bailout", String.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode message) {
            if (canDelayIntrinsification) {
                /*
                     * We do not want to bailout yet, since we are still parsing individual methods
                     * and constant folding could still eliminate the call to bailout(). However, we
                     * also want to stop parsing, since we are sure that we will never need the
                     * graph beyond the bailout point.
                     *
                     * Therefore, we manually emit the call to bailout, which will be intrinsified
                     * later when intrinsifications can no longer be delayed. The call is followed
                     * by a NeverPartOfCompilationNode, which is a control sink and therefore stops
                     * any further parsing.
                     */
                StampPair returnStamp = b.getInvokeReturnStamp(b.getAssumptions());
                CallTargetNode callTarget = b.add(new MethodCallTargetNode(InvokeKind.Static, targetMethod, new ValueNode[] { message }, returnStamp, null));
                b.add(new InvokeNode(callTarget, b.bci()));
                b.add(new NeverPartOfCompilationNode("intrinsification of call to bailout() will abort entire compilation"));
                return true;
            }
            if (message.isConstant()) {
                throw b.bailout(message.asConstant().toValueString());
            }
            throw b.bailout("bailout (message is not compile-time constant, so no additional information is available)");
        }
    });
    r.register1("isCompilationConstant", Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
            if ((value instanceof BoxNode ? ((BoxNode) value).getValue() : value).isConstant()) {
                b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true));
            } else {
                b.addPush(JavaKind.Boolean, new IsCompilationConstantNode(value));
            }
            return true;
        }
    });
    r.register1("isPartialEvaluationConstant", Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
            if ((value instanceof BoxNode ? ((BoxNode) value).getValue() : value).isConstant()) {
                b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(true));
            } else if (canDelayIntrinsification) {
                return false;
            } else {
                b.addPush(JavaKind.Boolean, ConstantNode.forBoolean(false));
            }
            return true;
        }
    });
    r.register1("materialize", Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
            AllowMaterializeNode materializedValue = b.append(new AllowMaterializeNode(value));
            b.add(new ForceMaterializeNode(materializedValue));
            return true;
        }
    });
    r.register1("ensureVirtualized", Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
            b.add(new EnsureVirtualizedNode(object, false));
            return true;
        }
    });
    r.register1("ensureVirtualizedHere", Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
            b.add(new EnsureVirtualizedNode(object, true));
            return true;
        }
    });
    r.register2("castExact", Object.class, Class.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object, ValueNode javaClass) {
            ValueNode nullCheckedClass = b.addNonNullCast(javaClass);
            LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), nullCheckedClass, object, true, true));
            if (condition.isTautology()) {
                b.addPush(JavaKind.Object, object);
            } else {
                FixedGuardNode fixedGuard = b.add(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false));
                b.addPush(JavaKind.Object, DynamicPiNode.create(b.getAssumptions(), b.getConstantReflection(), object, fixedGuard, nullCheckedClass, true));
            }
            return true;
        }
    });
}
Also used : EnsureVirtualizedNode(org.graalvm.compiler.nodes.virtual.EnsureVirtualizedNode) BoxNode(org.graalvm.compiler.nodes.extended.BoxNode) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) BranchProbabilityNode(org.graalvm.compiler.nodes.extended.BranchProbabilityNode) AllowMaterializeNode(org.graalvm.compiler.truffle.compiler.nodes.frame.AllowMaterializeNode) ResolvedJavaSymbol(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.ResolvedJavaSymbol) NeverPartOfCompilationNode(org.graalvm.compiler.truffle.compiler.nodes.asserts.NeverPartOfCompilationNode) ForceMaterializeNode(org.graalvm.compiler.truffle.compiler.nodes.frame.ForceMaterializeNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) IsCompilationConstantNode(org.graalvm.compiler.truffle.compiler.nodes.IsCompilationConstantNode) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) StampPair(org.graalvm.compiler.core.common.type.StampPair) InvokeNode(org.graalvm.compiler.nodes.InvokeNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) DeoptimizeNode(org.graalvm.compiler.nodes.DeoptimizeNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) CallTargetNode(org.graalvm.compiler.nodes.CallTargetNode)

Example 3 with FixedGuardNode

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

the class VirtualFrameAccessorNode method insertDeoptimization.

protected void insertDeoptimization(VirtualizerTool tool) {
    /*
         * Escape analysis does not allow insertion of a DeoptimizeNode. We work around this
         * restriction by inserting an always-failing guard, which will be canonicalized to a
         * DeoptimizeNode later on.
         */
    LogicNode condition = LogicConstantNode.contradiction();
    tool.addNode(condition);
    JavaConstant speculation = graph().getSpeculationLog().speculate(frame.getIntrinsifyAccessorsSpeculation());
    tool.addNode(new FixedGuardNode(condition, DeoptimizationReason.RuntimeConstraint, DeoptimizationAction.InvalidateRecompile, speculation, false));
    if (getStackKind() == JavaKind.Void) {
        tool.delete();
    } else {
        /*
             * Even though all usages will be eventually dead, we need to provide a valid
             * replacement value for now.
             */
        ConstantNode unusedValue = ConstantNode.forConstant(JavaConstant.defaultForKind(getStackKind()), tool.getMetaAccessProvider());
        tool.addNode(unusedValue);
        tool.replaceWith(unusedValue);
    }
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) JavaConstant(jdk.vm.ci.meta.JavaConstant)

Example 4 with FixedGuardNode

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

the class OnStackReplacementPhase method run.

@Override
@SuppressWarnings("try")
protected void run(StructuredGraph graph) {
    DebugContext debug = graph.getDebug();
    if (graph.getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI) {
        // used.
        assert graph.getNodes(EntryMarkerNode.TYPE).isEmpty();
        return;
    }
    debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement initial at bci %d", graph.getEntryBCI());
    EntryMarkerNode osr;
    int maxIterations = -1;
    int iterations = 0;
    final EntryMarkerNode originalOSRNode = getEntryMarker(graph);
    final LoopBeginNode originalOSRLoop = osrLoop(originalOSRNode);
    final boolean currentOSRWithLocks = osrWithLocks(originalOSRNode);
    if (originalOSRLoop == null) {
        /*
             * OSR with Locks: We do not have an OSR loop for the original OSR bci. Therefore we
             * cannot decide where to deopt and which framestate will be used. In the worst case the
             * framestate of the OSR entry would be used.
             */
        throw new PermanentBailoutException("OSR compilation without OSR entry loop.");
    }
    if (!supportOSRWithLocks(graph.getOptions()) && currentOSRWithLocks) {
        throw new PermanentBailoutException("OSR with locks disabled.");
    }
    do {
        osr = getEntryMarker(graph);
        LoopsData loops = new LoopsData(graph);
        // Find the loop that contains the EntryMarker
        Loop<Block> l = loops.getCFG().getNodeToBlock().get(osr).getLoop();
        if (l == null) {
            break;
        }
        iterations++;
        if (maxIterations == -1) {
            maxIterations = l.getDepth();
        } else if (iterations > maxIterations) {
            throw GraalError.shouldNotReachHere();
        }
        // Peel the outermost loop first
        while (l.getParent() != null) {
            l = l.getParent();
        }
        LoopTransformations.peel(loops.loop(l));
        osr.replaceAtUsages(InputType.Guard, AbstractBeginNode.prevBegin((FixedNode) osr.predecessor()));
        for (Node usage : osr.usages().snapshot()) {
            EntryProxyNode proxy = (EntryProxyNode) usage;
            proxy.replaceAndDelete(proxy.value());
        }
        GraphUtil.removeFixedWithUnusedInputs(osr);
        debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement loop peeling result");
    } while (true);
    StartNode start = graph.start();
    FrameState osrState = osr.stateAfter();
    OSRStartNode osrStart;
    try (DebugCloseable context = osr.withNodeSourcePosition()) {
        osr.setStateAfter(null);
        osrStart = graph.add(new OSRStartNode());
        FixedNode next = osr.next();
        osr.setNext(null);
        osrStart.setNext(next);
        graph.setStart(osrStart);
        osrStart.setStateAfter(osrState);
        debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after setting OSR start");
        final int localsSize = osrState.localsSize();
        final int locksSize = osrState.locksSize();
        for (int i = 0; i < localsSize + locksSize; i++) {
            ValueNode value = null;
            if (i >= localsSize) {
                value = osrState.lockAt(i - localsSize);
            } else {
                value = osrState.localAt(i);
            }
            if (value instanceof EntryProxyNode) {
                EntryProxyNode proxy = (EntryProxyNode) value;
                /*
                     * We need to drop the stamp since the types we see during OSR may be too
                     * precise (if a branch was not parsed for example). In cases when this is
                     * possible, we insert a guard and narrow the OSRLocal stamp at its usages.
                     */
                Stamp narrowedStamp = proxy.value().stamp(NodeView.DEFAULT);
                Stamp unrestrictedStamp = proxy.stamp(NodeView.DEFAULT).unrestricted();
                ValueNode osrLocal;
                if (i >= localsSize) {
                    osrLocal = graph.addOrUnique(new OSRLockNode(i - localsSize, unrestrictedStamp));
                } else {
                    osrLocal = graph.addOrUnique(new OSRLocalNode(i, unrestrictedStamp));
                }
                // Speculate on the OSRLocal stamps that could be more precise.
                OSRLocalSpeculationReason reason = new OSRLocalSpeculationReason(osrState.bci, narrowedStamp, i);
                if (graph.getSpeculationLog().maySpeculate(reason) && osrLocal instanceof OSRLocalNode && value.getStackKind().equals(JavaKind.Object) && !narrowedStamp.isUnrestricted()) {
                    // Add guard.
                    LogicNode check = graph.addOrUniqueWithInputs(InstanceOfNode.createHelper((ObjectStamp) narrowedStamp, osrLocal, null, null));
                    JavaConstant constant = graph.getSpeculationLog().speculate(reason);
                    FixedGuardNode guard = graph.add(new FixedGuardNode(check, DeoptimizationReason.OptimizedTypeCheckViolated, DeoptimizationAction.InvalidateRecompile, constant, false));
                    graph.addAfterFixed(osrStart, guard);
                    // Replace with a more specific type at usages.
                    // We know that we are at the root,
                    // so we need to replace the proxy in the state.
                    proxy.replaceAtMatchingUsages(osrLocal, n -> n == osrState);
                    osrLocal = graph.addOrUnique(new PiNode(osrLocal, narrowedStamp, guard));
                }
                proxy.replaceAndDelete(osrLocal);
            } else {
                assert value == null || value instanceof OSRLocalNode;
            }
        }
        osr.replaceAtUsages(InputType.Guard, osrStart);
    }
    debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after replacing entry proxies");
    GraphUtil.killCFG(start);
    debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
    new DeadCodeEliminationPhase(Required).apply(graph);
    if (currentOSRWithLocks) {
        OsrWithLocksCount.increment(debug);
        try (DebugCloseable context = osrStart.withNodeSourcePosition()) {
            for (int i = osrState.monitorIdCount() - 1; i >= 0; --i) {
                MonitorIdNode id = osrState.monitorIdAt(i);
                ValueNode lockedObject = osrState.lockAt(i);
                OSRMonitorEnterNode osrMonitorEnter = graph.add(new OSRMonitorEnterNode(lockedObject, id));
                for (Node usage : id.usages()) {
                    if (usage instanceof AccessMonitorNode) {
                        AccessMonitorNode access = (AccessMonitorNode) usage;
                        access.setObject(lockedObject);
                    }
                }
                FixedNode oldNext = osrStart.next();
                oldNext.replaceAtPredecessor(null);
                osrMonitorEnter.setNext(oldNext);
                osrStart.setNext(osrMonitorEnter);
            }
        }
        debug.dump(DebugContext.DETAILED_LEVEL, graph, "After inserting OSR monitor enters");
        /*
             * Ensure balanced monitorenter - monitorexit
             *
             * Ensure that there is no monitor exit without a monitor enter in the graph. If there
             * is one this can only be done by bytecode as we have the monitor enter before the OSR
             * loop but the exit in a path of the loop that must be under a condition, else it will
             * throw an IllegalStateException anyway in the 2.iteration
             */
        for (MonitorExitNode exit : graph.getNodes(MonitorExitNode.TYPE)) {
            MonitorIdNode id = exit.getMonitorId();
            if (id.usages().filter(MonitorEnterNode.class).count() != 1) {
                throw new PermanentBailoutException("Unbalanced monitor enter-exit in OSR compilation with locks. Object is locked before the loop but released inside the loop.");
            }
        }
    }
    debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
    new DeadCodeEliminationPhase(Required).apply(graph);
    /*
         * There must not be any parameter nodes left after OSR compilation.
         */
    assert graph.getNodes(ParameterNode.TYPE).count() == 0 : "OSR Compilation contains references to parameters.";
}
Also used : EntryMarkerNode(org.graalvm.compiler.nodes.EntryMarkerNode) AccessMonitorNode(org.graalvm.compiler.nodes.java.AccessMonitorNode) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) OSRLockNode(org.graalvm.compiler.nodes.extended.OSRLockNode) MonitorIdNode(org.graalvm.compiler.nodes.java.MonitorIdNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) OSRLocalNode(org.graalvm.compiler.nodes.extended.OSRLocalNode) EntryMarkerNode(org.graalvm.compiler.nodes.EntryMarkerNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) EntryProxyNode(org.graalvm.compiler.nodes.EntryProxyNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) PiNode(org.graalvm.compiler.nodes.PiNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) OSRMonitorEnterNode(org.graalvm.compiler.nodes.extended.OSRMonitorEnterNode) MonitorExitNode(org.graalvm.compiler.nodes.java.MonitorExitNode) StartNode(org.graalvm.compiler.nodes.StartNode) InstanceOfNode(org.graalvm.compiler.nodes.java.InstanceOfNode) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) AccessMonitorNode(org.graalvm.compiler.nodes.java.AccessMonitorNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) OSRStartNode(org.graalvm.compiler.nodes.extended.OSRStartNode) MonitorEnterNode(org.graalvm.compiler.nodes.java.MonitorEnterNode) Node(org.graalvm.compiler.graph.Node) JavaConstant(jdk.vm.ci.meta.JavaConstant) FixedNode(org.graalvm.compiler.nodes.FixedNode) PiNode(org.graalvm.compiler.nodes.PiNode) FrameState(org.graalvm.compiler.nodes.FrameState) MonitorExitNode(org.graalvm.compiler.nodes.java.MonitorExitNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) DeadCodeEliminationPhase(org.graalvm.compiler.phases.common.DeadCodeEliminationPhase) PermanentBailoutException(org.graalvm.compiler.core.common.PermanentBailoutException) MonitorIdNode(org.graalvm.compiler.nodes.java.MonitorIdNode) StartNode(org.graalvm.compiler.nodes.StartNode) OSRStartNode(org.graalvm.compiler.nodes.extended.OSRStartNode) LoopsData(org.graalvm.compiler.loop.LoopsData) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) OSRLocalNode(org.graalvm.compiler.nodes.extended.OSRLocalNode) DebugContext(org.graalvm.compiler.debug.DebugContext) EntryProxyNode(org.graalvm.compiler.nodes.EntryProxyNode) OSRLockNode(org.graalvm.compiler.nodes.extended.OSRLockNode) OSRMonitorEnterNode(org.graalvm.compiler.nodes.extended.OSRMonitorEnterNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) OSRStartNode(org.graalvm.compiler.nodes.extended.OSRStartNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) Block(org.graalvm.compiler.nodes.cfg.Block) LogicNode(org.graalvm.compiler.nodes.LogicNode) DebugCloseable(org.graalvm.compiler.debug.DebugCloseable)

Example 5 with FixedGuardNode

use of org.graalvm.compiler.nodes.FixedGuardNode 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)

Aggregations

FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)31 LogicNode (org.graalvm.compiler.nodes.LogicNode)25 ValueNode (org.graalvm.compiler.nodes.ValueNode)24 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)10 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)8 TypeReference (org.graalvm.compiler.core.common.type.TypeReference)8 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)8 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)7 Stamp (org.graalvm.compiler.core.common.type.Stamp)7 JavaConstant (jdk.vm.ci.meta.JavaConstant)6 DebugCloseable (org.graalvm.compiler.debug.DebugCloseable)5 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)5 RuntimeConstraint (jdk.vm.ci.meta.DeoptimizationReason.RuntimeConstraint)4 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)4 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)4 FixedNode (org.graalvm.compiler.nodes.FixedNode)4 IfNode (org.graalvm.compiler.nodes.IfNode)4 LogicConstantNode (org.graalvm.compiler.nodes.LogicConstantNode)4 PiNode (org.graalvm.compiler.nodes.PiNode)4 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)4