001/* 002 * Copyright (c) 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; 024 025import jdk.internal.jvmci.meta.*; 026import jdk.internal.jvmci.meta.MethodHandleAccessProvider.*; 027 028import com.oracle.graal.graph.*; 029import com.oracle.graal.graphbuilderconf.*; 030import com.oracle.graal.nodes.*; 031import com.oracle.graal.nodes.CallTargetNode.InvokeKind; 032import com.oracle.graal.replacements.nodes.*; 033 034public class MethodHandlePlugin implements NodePlugin { 035 private final MethodHandleAccessProvider methodHandleAccess; 036 037 public MethodHandlePlugin(MethodHandleAccessProvider methodHandleAccess) { 038 this.methodHandleAccess = methodHandleAccess; 039 } 040 041 @Override 042 public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) { 043 IntrinsicMethod intrinsicMethod = methodHandleAccess.lookupMethodHandleIntrinsic(method); 044 if (intrinsicMethod != null) { 045 InvokeKind invokeKind = b.getInvokeKind(); 046 if (invokeKind != InvokeKind.Static) { 047 args[0] = b.nullCheckedValue(args[0]); 048 } 049 JavaType invokeReturnType = b.getInvokeReturnType(); 050 InvokeNode invoke = MethodHandleNode.tryResolveTargetInvoke(b.getAssumptions(), b.getConstantReflection().getMethodHandleAccess(), intrinsicMethod, method, b.bci(), invokeReturnType, args); 051 if (invoke == null) { 052 MethodHandleNode methodHandleNode = new MethodHandleNode(intrinsicMethod, invokeKind, method, b.bci(), invokeReturnType, args); 053 if (invokeReturnType.getKind() == Kind.Void) { 054 b.add(methodHandleNode); 055 } else { 056 b.addPush(invokeReturnType.getKind(), methodHandleNode); 057 } 058 } else { 059 CallTargetNode callTarget = invoke.callTarget(); 060 NodeInputList<ValueNode> argumentsList = callTarget.arguments(); 061 for (int i = 0; i < argumentsList.size(); ++i) { 062 argumentsList.initialize(i, b.recursiveAppend(argumentsList.get(i))); 063 } 064 065 // If a MemberName suffix argument is dropped, the replaced call cannot 066 // deoptimized since the necessary frame state cannot be reconstructed. 067 // As such, it needs to recursively inline everything. 068 boolean inlineEverything = args.length != argumentsList.size(); 069 b.handleReplacedInvoke(invoke.getInvokeKind(), callTarget.targetMethod(), argumentsList.toArray(new ValueNode[argumentsList.size()]), inlineEverything); 070 } 071 return true; 072 } 073 return false; 074 } 075}