1 /*
2  * Copyright (c) 2015, 2018, 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 
24 
25 package org.graalvm.compiler.nodes.graphbuilderconf;
26 
27 import org.graalvm.compiler.bytecode.BytecodeProvider;
28 import org.graalvm.compiler.nodes.Invoke;
29 import org.graalvm.compiler.nodes.ValueNode;
30 
31 import jdk.vm.ci.meta.ResolvedJavaMethod;
32 
33 /**
34  * Plugin for specifying what is inlined during graph parsing. This plugin is also notified
35  * {@link #notifyBeforeInline before} and {@link #notifyAfterInline} the inlining, as well as of
36  * {@link #notifyNotInlined non-inlined} invocations (i.e., those for which an {@link Invoke} node
37  * is created).
38  */
39 public interface InlineInvokePlugin extends GraphBuilderPlugin {
40 
41     /**
42      * Result of a {@link #shouldInlineInvoke inlining decision}.
43      */
44     final class InlineInfo {
45 
46         /**
47          * Denotes a call site that must not be inlined and should be implemented by a node that
48          * does not speculate on the call not raising an exception.
49          */
50         public static final InlineInfo DO_NOT_INLINE_WITH_EXCEPTION = new InlineInfo(null, null, null);
51 
52         /**
53          * Denotes a call site must not be inlined and can be implemented by a node that speculates
54          * the call will not throw an exception.
55          */
56         public static final InlineInfo DO_NOT_INLINE_NO_EXCEPTION = new InlineInfo(null, null, null);
57 
58         /**
59          * Denotes a call site must not be inlined and the execution should be transferred to
60          * interpreter in case of an exception.
61          */
62         public static final InlineInfo DO_NOT_INLINE_DEOPTIMIZE_ON_EXCEPTION = new InlineInfo(null, null, null);
63 
64         private final ResolvedJavaMethod methodToInline;
65         private final ResolvedJavaMethod originalMethod;
66         private final BytecodeProvider intrinsicBytecodeProvider;
67 
createStandardInlineInfo(ResolvedJavaMethod methodToInline)68         public static InlineInfo createStandardInlineInfo(ResolvedJavaMethod methodToInline) {
69             return new InlineInfo(methodToInline, null, null);
70         }
71 
createIntrinsicInlineInfo(ResolvedJavaMethod methodToInline, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider)72         public static InlineInfo createIntrinsicInlineInfo(ResolvedJavaMethod methodToInline, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider) {
73             return new InlineInfo(methodToInline, originalMethod, intrinsicBytecodeProvider);
74         }
75 
InlineInfo(ResolvedJavaMethod methodToInline, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider)76         private InlineInfo(ResolvedJavaMethod methodToInline, ResolvedJavaMethod originalMethod, BytecodeProvider intrinsicBytecodeProvider) {
77             this.methodToInline = methodToInline;
78             this.originalMethod = originalMethod;
79             this.intrinsicBytecodeProvider = intrinsicBytecodeProvider;
80         }
81 
82         /**
83          * Returns the method to be inlined, or {@code null} if the call site must not be inlined.
84          */
getMethodToInline()85         public ResolvedJavaMethod getMethodToInline() {
86             return methodToInline;
87         }
88 
allowsInlining()89         public boolean allowsInlining() {
90             return methodToInline != null;
91         }
92 
93         /**
94          * Returns the original method if this is an inline of an intrinsic, or {@code null} if the
95          * call site must not be inlined.
96          */
getOriginalMethod()97         public ResolvedJavaMethod getOriginalMethod() {
98             return originalMethod;
99         }
100 
101         /**
102          * Gets the provider of bytecode to be parsed for {@link #getMethodToInline()} if is is an
103          * intrinsic for the original method (i.e., the {@code method} passed to
104          * {@link InlineInvokePlugin#shouldInlineInvoke}). A {@code null} return value indicates
105          * that this is not an intrinsic inlining.
106          */
getIntrinsicBytecodeProvider()107         public BytecodeProvider getIntrinsicBytecodeProvider() {
108             return intrinsicBytecodeProvider;
109         }
110     }
111 
112     /**
113      * Determines whether a call to a given method is to be inlined. The return value is a
114      * tri-state:
115      * <p>
116      * Non-null return value with a non-null {@link InlineInfo#getMethodToInline method}: That
117      * {@link InlineInfo#getMethodToInline method} is inlined. Note that it can be a different
118      * method than the one specified here as the parameter, which allows method substitutions.
119      * <p>
120      * Non-null return value with a null {@link InlineInfo#getMethodToInline method}, e.g.,
121      * {@link InlineInfo#DO_NOT_INLINE_WITH_EXCEPTION}: The method is not inlined, and other plugins
122      * with a lower priority cannot overwrite this decision.
123      * <p>
124      * Null return value: This plugin made no decision, other plugins with a lower priority are
125      * asked.
126      *
127      * @param b the context
128      * @param method the target method of an invoke
129      * @param args the arguments to the invoke
130      */
shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args)131     default InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
132         return null;
133     }
134 
135     /**
136      * Notification that a method is about to be inlined.
137      *
138      * @param methodToInline the inlined method
139      */
notifyBeforeInline(ResolvedJavaMethod methodToInline)140     default void notifyBeforeInline(ResolvedJavaMethod methodToInline) {
141     }
142 
143     /**
144      * Notification that a method was inlined.
145      *
146      * @param methodToInline the inlined method
147      */
notifyAfterInline(ResolvedJavaMethod methodToInline)148     default void notifyAfterInline(ResolvedJavaMethod methodToInline) {
149     }
150 
151     /**
152      * Notifies this plugin of the {@link Invoke} node created for a method that was not inlined per
153      * {@link #shouldInlineInvoke}.
154      *
155      * @param b the context
156      * @param method the method that was not inlined
157      * @param invoke the invoke node created for the call to {@code method}
158      */
notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke)159     default void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
160     }
161 }
162