001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.nodes;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.common.*;
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.graph.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.CallTargetNode.InvokeKind;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.java.*;
034import com.oracle.graal.nodes.memory.*;
035
036/**
037 * This is an extension of {@link MacroNode} that is a {@link StateSplit} and a
038 * {@link MemoryCheckpoint}.
039 */
040@NodeInfo
041public abstract class MacroStateSplitNode extends MacroNode implements StateSplit, MemoryCheckpoint.Single {
042
043    public static final NodeClass<MacroStateSplitNode> TYPE = NodeClass.create(MacroStateSplitNode.class);
044    @OptionalInput(InputType.State) protected FrameState stateAfter;
045
046    protected MacroStateSplitNode(NodeClass<? extends MacroNode> c, InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, JavaType returnType, ValueNode... arguments) {
047        super(c, invokeKind, targetMethod, bci, returnType, arguments);
048    }
049
050    @Override
051    public FrameState stateAfter() {
052        return stateAfter;
053    }
054
055    public void setStateAfter(FrameState x) {
056        assert x == null || x.isAlive() : "frame state must be in a graph";
057        updateUsages(stateAfter, x);
058        stateAfter = x;
059    }
060
061    public boolean hasSideEffect() {
062        return true;
063    }
064
065    public LocationIdentity getLocationIdentity() {
066        return LocationIdentity.any();
067    }
068
069    protected void replaceSnippetInvokes(StructuredGraph snippetGraph) {
070        for (MethodCallTargetNode call : snippetGraph.getNodes(MethodCallTargetNode.TYPE)) {
071            Invoke invoke = call.invoke();
072            if (!call.targetMethod().equals(getTargetMethod())) {
073                throw new JVMCIError("unexpected invoke %s in snippet", getClass().getSimpleName());
074            }
075            assert invoke.stateAfter().bci == BytecodeFrame.AFTER_BCI;
076            // Here we need to fix the bci of the invoke
077            InvokeNode newInvoke = snippetGraph.add(new InvokeNode(invoke.callTarget(), getBci()));
078            newInvoke.setStateAfter(invoke.stateAfter());
079            snippetGraph.replaceFixedWithFixed((InvokeNode) invoke.asNode(), newInvoke);
080        }
081    }
082}