1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.replacements.nodes;
  24 
  25 import org.graalvm.compiler.core.common.LocationIdentity;
  26 import org.graalvm.compiler.core.common.type.StampPair;
  27 import org.graalvm.compiler.debug.GraalError;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.nodeinfo.InputType;
  30 import org.graalvm.compiler.nodeinfo.NodeInfo;
  31 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  32 import org.graalvm.compiler.nodes.FrameState;
  33 import org.graalvm.compiler.nodes.Invoke;
  34 import org.graalvm.compiler.nodes.InvokeNode;
  35 import org.graalvm.compiler.nodes.StateSplit;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  39 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  40 
  41 import jdk.vm.ci.code.BytecodeFrame;
  42 import jdk.vm.ci.meta.ResolvedJavaMethod;
  43 
  44 /**
  45  * This is an extension of {@link MacroNode} that is a {@link StateSplit} and a
  46  * {@link MemoryCheckpoint}.
  47  */
  48 @NodeInfo
  49 public abstract class MacroStateSplitNode extends MacroNode implements StateSplit, MemoryCheckpoint.Single {
  50 
  51     public static final NodeClass<MacroStateSplitNode> TYPE = NodeClass.create(MacroStateSplitNode.class);
  52     @OptionalInput(InputType.State) protected FrameState stateAfter;
  53 
  54     protected MacroStateSplitNode(NodeClass<? extends MacroNode> c, InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) {
  55         super(c, invokeKind, targetMethod, bci, returnStamp, arguments);
  56     }
  57 
  58     @Override
  59     public FrameState stateAfter() {
  60         return stateAfter;
  61     }
  62 
  63     @Override
  64     public void setStateAfter(FrameState x) {
  65         assert x == null || x.isAlive() : "frame state must be in a graph";
  66         updateUsages(stateAfter, x);
  67         stateAfter = x;
  68     }
  69 
  70     @Override
  71     public boolean hasSideEffect() {
  72         return true;
  73     }
  74 
  75     @Override
  76     public LocationIdentity getLocationIdentity() {
  77         return LocationIdentity.any();
  78     }
  79 
  80     protected void replaceSnippetInvokes(StructuredGraph snippetGraph) {
  81         for (MethodCallTargetNode call : snippetGraph.getNodes(MethodCallTargetNode.TYPE)) {
  82             Invoke invoke = call.invoke();
  83             if (!call.targetMethod().equals(getTargetMethod())) {
  84                 throw new GraalError("unexpected invoke %s in snippet", getClass().getSimpleName());
  85             }
  86             assert invoke.stateAfter().bci == BytecodeFrame.AFTER_BCI;
  87             // Here we need to fix the bci of the invoke
  88             InvokeNode newInvoke = snippetGraph.add(new InvokeNode(invoke.callTarget(), getBci()));
  89             newInvoke.setStateAfter(invoke.stateAfter());
  90             snippetGraph.replaceFixedWithFixed((InvokeNode) invoke.asNode(), newInvoke);
  91         }
  92     }
  93 }