1 /* 2 * Copyright (c) 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; 24 25 import org.graalvm.compiler.core.common.type.StampPair; 26 import org.graalvm.compiler.graph.NodeInputList; 27 import org.graalvm.compiler.nodes.CallTargetNode; 28 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 29 import org.graalvm.compiler.nodes.InvokeNode; 30 import org.graalvm.compiler.nodes.ValueNode; 31 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 32 import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin; 33 import org.graalvm.compiler.replacements.nodes.MethodHandleNode; 34 35 import jdk.vm.ci.meta.JavaKind; 36 import jdk.vm.ci.meta.MethodHandleAccessProvider; 37 import jdk.vm.ci.meta.MethodHandleAccessProvider.IntrinsicMethod; 38 import jdk.vm.ci.meta.ResolvedJavaMethod; 39 40 public class MethodHandlePlugin implements NodePlugin { 41 private final MethodHandleAccessProvider methodHandleAccess; 42 private final boolean safeForDeoptimization; 43 44 public MethodHandlePlugin(MethodHandleAccessProvider methodHandleAccess, boolean safeForDeoptimization) { 45 this.methodHandleAccess = methodHandleAccess; 46 this.safeForDeoptimization = safeForDeoptimization; 47 } 48 49 @Override 50 public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) { 51 IntrinsicMethod intrinsicMethod = methodHandleAccess.lookupMethodHandleIntrinsic(method); 52 if (intrinsicMethod != null) { 53 InvokeKind invokeKind = b.getInvokeKind(); 54 if (invokeKind != InvokeKind.Static) { 55 args[0] = b.nullCheckedValue(args[0]); 56 } 57 StampPair invokeReturnStamp = b.getInvokeReturnStamp(b.getAssumptions()); 58 InvokeNode invoke = MethodHandleNode.tryResolveTargetInvoke(b.getAssumptions(), b.getConstantReflection().getMethodHandleAccess(), intrinsicMethod, method, b.bci(), invokeReturnStamp, 59 args); 60 if (invoke == null) { 61 MethodHandleNode methodHandleNode = new MethodHandleNode(intrinsicMethod, invokeKind, method, b.bci(), invokeReturnStamp, args); 62 if (invokeReturnStamp.getTrustedStamp().getStackKind() == JavaKind.Void) { 63 b.add(methodHandleNode); 64 } else { 65 b.addPush(invokeReturnStamp.getTrustedStamp().getStackKind(), methodHandleNode); 66 } 67 } else { 68 CallTargetNode callTarget = invoke.callTarget(); 69 NodeInputList<ValueNode> argumentsList = callTarget.arguments(); 70 for (int i = 0; i < argumentsList.size(); ++i) { 71 argumentsList.initialize(i, b.recursiveAppend(argumentsList.get(i))); 72 } 73 74 boolean inlineEverything = false; 75 if (safeForDeoptimization) { 76 // If a MemberName suffix argument is dropped, the replaced call cannot 77 // deoptimized since the necessary frame state cannot be reconstructed. 78 // As such, it needs to recursively inline everything. 79 inlineEverything = args.length != argumentsList.size(); 80 } 81 b.handleReplacedInvoke(invoke.getInvokeKind(), callTarget.targetMethod(), argumentsList.toArray(new ValueNode[argumentsList.size()]), inlineEverything); 82 } 83 return true; 84 } 85 return false; 86 } 87 }