1 /*
2  * Copyright (c) 2013, 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.spi;
26 
27 import org.graalvm.compiler.api.replacements.MethodSubstitution;
28 import org.graalvm.compiler.api.replacements.SnippetTemplateCache;
29 import org.graalvm.compiler.bytecode.BytecodeProvider;
30 import org.graalvm.compiler.core.common.CompilationIdentifier;
31 import org.graalvm.compiler.debug.DebugContext;
32 import org.graalvm.compiler.graph.NodeSourcePosition;
33 import org.graalvm.compiler.nodes.StructuredGraph;
34 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
35 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderPlugin;
36 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
37 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
38 import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
39 import org.graalvm.compiler.options.OptionValues;
40 
41 import jdk.vm.ci.meta.ResolvedJavaMethod;
42 
43 /**
44  * Interface for managing replacements.
45  */
46 public interface Replacements {
47 
getProviders()48     CoreProviders getProviders();
49 
50     /**
51      * Gets the object managing the various graph builder plugins used by this object when parsing
52      * bytecode into a graph.
53      */
getGraphBuilderPlugins()54     GraphBuilderConfiguration.Plugins getGraphBuilderPlugins();
55 
56     /**
57      * Gets the plugin type that intrinsifies calls to {@code method}.
58      */
getIntrinsifyingPlugin(ResolvedJavaMethod method)59     Class<? extends GraphBuilderPlugin> getIntrinsifyingPlugin(ResolvedJavaMethod method);
60 
61     /**
62      * Gets the snippet graph derived from a given method.
63      *
64      * @param recursiveEntry if the snippet contains a call to this method, it's considered as
65      *            recursive call and won't be processed for {@linkplain MethodSubstitution
66      *            substitutions}.
67      * @param args arguments to the snippet if available, otherwise {@code null}
68      * @param trackNodeSourcePosition
69      * @param options
70      * @return the snippet graph, if any, that is derived from {@code method}
71      */
getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, OptionValues options)72     StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition,
73                     OptionValues options);
74 
75     /**
76      * Registers a method as snippet.
77      */
registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition, OptionValues options)78     void registerSnippet(ResolvedJavaMethod method, ResolvedJavaMethod original, Object receiver, boolean trackNodeSourcePosition, OptionValues options);
79 
80     /**
81      * Gets a graph that is a substitution for a given {@link MethodSubstitutionPlugin plugin} in
82      * the {@link org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext
83      * context}.
84      *
85      * @param plugin the plugin being substituted
86      * @param original the method being substituted
87      * @param context the kind of inlining to be performed for the substitution
88      * @param allowAssumptions
89      * @param options
90      * @return the method substitution graph, if any, that is derived from {@code method}
91      */
getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options)92     StructuredGraph getMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context,
93                     StructuredGraph.AllowAssumptions allowAssumptions, OptionValues options);
94 
95     /**
96      * Registers a plugin as a substitution.
97      */
registerMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, OptionValues options)98     void registerMethodSubstitution(MethodSubstitutionPlugin plugin, ResolvedJavaMethod original, IntrinsicContext.CompilationContext context, OptionValues options);
99 
100     /**
101      * Gets a graph that is a substitution for a given method.
102      *
103      * @param invokeBci the call site BCI if this request is made for inlining a substitute
104      *            otherwise {@code -1}
105      * @param trackNodeSourcePosition
106      * @param options
107      * @return the graph, if any, that is a substitution for {@code method}
108      */
getSubstitution(ResolvedJavaMethod method, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, OptionValues options)109     StructuredGraph getSubstitution(ResolvedJavaMethod method, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition, OptionValues options);
110 
111     /**
112      * Gets a graph produced from the intrinsic for a given method that can be compiled and
113      * installed for the method.
114      *
115      * @param method
116      * @param compilationId
117      * @param debug
118      * @return an intrinsic graph that can be compiled and installed for {@code method} or null
119      */
getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug)120     StructuredGraph getIntrinsicGraph(ResolvedJavaMethod method, CompilationIdentifier compilationId, DebugContext debug);
121 
122     /**
123      * Determines if there may be a
124      * {@linkplain #getSubstitution(ResolvedJavaMethod, int, boolean, NodeSourcePosition, OptionValues)
125      * substitution graph} for a given method.
126      *
127      * A call to {@link #getSubstitution} may still return {@code null} for {@code method} and
128      * {@code invokeBci}. A substitution may be based on an {@link InvocationPlugin} that returns
129      * {@code false} for {@link InvocationPlugin#execute} making it impossible to create a
130      * substitute graph.
131      *
132      * @param invokeBci the call site BCI if this request is made for inlining a substitute
133      *            otherwise {@code -1}
134      * @return true iff there may be a substitution graph available for {@code method}
135      */
hasSubstitution(ResolvedJavaMethod method, int invokeBci)136     boolean hasSubstitution(ResolvedJavaMethod method, int invokeBci);
137 
138     /**
139      * Gets the provider for accessing the bytecode of a substitution method if no other provider is
140      * associated with the substitution method.
141      */
getDefaultReplacementBytecodeProvider()142     BytecodeProvider getDefaultReplacementBytecodeProvider();
143 
144     /**
145      * Register snippet templates.
146      */
registerSnippetTemplateCache(SnippetTemplateCache snippetTemplates)147     void registerSnippetTemplateCache(SnippetTemplateCache snippetTemplates);
148 
149     /**
150      * Get snippet templates that were registered with
151      * {@link Replacements#registerSnippetTemplateCache(SnippetTemplateCache)}.
152      */
getSnippetTemplateCache(Class<T> templatesClass)153     <T extends SnippetTemplateCache> T getSnippetTemplateCache(Class<T> templatesClass);
154 
155     /**
156      * Notifies this method that no further snippets will be registered via {@link #registerSnippet}
157      * or {@link #registerSnippetTemplateCache}.
158      *
159      * This is a hook for an implementation to check for or forbid late registration.
160      */
closeSnippetRegistration()161     default void closeSnippetRegistration() {
162     }
163 }
164