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