1 /*
   2  * Copyright (c) 2013, 2015, 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 java.lang.invoke.MethodHandle;
  26 
  27 import org.graalvm.compiler.core.common.type.StampPair;
  28 import org.graalvm.compiler.debug.GraalError;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.NodeInputList;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  34 import org.graalvm.compiler.nodes.spi.Lowerable;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  37 
  38 import jdk.vm.ci.meta.ResolvedJavaMethod;
  39 
  40 /**
  41  * A call target that replaces itself in the graph when being lowered by restoring the original
  42  * {@link MethodHandle} invocation target. Prior to
  43  * https://bugs.openjdk.java.net/browse/JDK-8072008, this is required for when a
  44  * {@link MethodHandle} call is resolved to a constant target but the target was not inlined. In
  45  * that case, the original invocation must be restored with all of its original arguments. Why?
  46  * HotSpot linkage for {@link MethodHandle} intrinsics (see
  47  * {@code MethodHandles::generate_method_handle_dispatch}) expects certain implicit arguments to be
  48  * on the stack such as the MemberName suffix argument for a call to one of the MethodHandle.linkTo*
  49  * methods. An {@linkplain MethodHandleNode#tryResolveTargetInvoke resolved} {@link MethodHandle}
  50  * invocation drops these arguments which means the interpreter won't find them.
  51  */
  52 @NodeInfo
  53 public final class ResolvedMethodHandleCallTargetNode extends MethodCallTargetNode implements Lowerable {
  54 
  55     public static final NodeClass<ResolvedMethodHandleCallTargetNode> TYPE = NodeClass.create(ResolvedMethodHandleCallTargetNode.class);
  56 
  57     /**
  58      * Creates a call target for an invocation on a direct target derived by resolving a constant
  59      * {@link MethodHandle}.
  60      */
  61     public static MethodCallTargetNode create(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] arguments, StampPair returnStamp,
  62                     ResolvedJavaMethod originalTargetMethod,
  63                     ValueNode[] originalArguments, StampPair originalReturnStamp) {
  64         return new ResolvedMethodHandleCallTargetNode(invokeKind, targetMethod, arguments, returnStamp, originalTargetMethod, originalArguments, originalReturnStamp);
  65     }
  66 
  67     protected final ResolvedJavaMethod originalTargetMethod;
  68     protected final StampPair originalReturnStamp;
  69     @Input NodeInputList<ValueNode> originalArguments;
  70 
  71     protected ResolvedMethodHandleCallTargetNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] arguments, StampPair returnStamp,
  72                     ResolvedJavaMethod originalTargetMethod,
  73                     ValueNode[] originalArguments, StampPair originalReturnStamp) {
  74         super(TYPE, invokeKind, targetMethod, arguments, returnStamp, null);
  75         this.originalTargetMethod = originalTargetMethod;
  76         this.originalReturnStamp = originalReturnStamp;
  77         this.originalArguments = new NodeInputList<>(this, originalArguments);
  78     }
  79 
  80     @Override
  81     public void lower(LoweringTool tool) {
  82         InvokeKind replacementInvokeKind = originalTargetMethod.isStatic() ? InvokeKind.Static : InvokeKind.Special;
  83         MethodCallTargetNode replacement = graph().add(
  84                         new MethodCallTargetNode(replacementInvokeKind, originalTargetMethod, originalArguments.toArray(new ValueNode[originalArguments.size()]), originalReturnStamp, null));
  85 
  86         // Replace myself...
  87         this.replaceAndDelete(replacement);
  88     }
  89 
  90     @Override
  91     public void generate(NodeLIRBuilderTool gen) {
  92         throw GraalError.shouldNotReachHere("should have replaced itself");
  93     }
  94 }