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.hotspot.stubs;
24
25 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
26
27 import java.lang.reflect.Method;
28
29 import org.graalvm.compiler.api.replacements.Snippet;
30 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
31 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter;
32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
33 import org.graalvm.compiler.bytecode.BytecodeProvider;
34 import org.graalvm.compiler.core.common.CompilationIdentifier;
35 import org.graalvm.compiler.core.common.type.StampFactory;
36 import org.graalvm.compiler.debug.Debug;
37 import org.graalvm.compiler.debug.Debug.Scope;
38 import org.graalvm.compiler.debug.GraalError;
39 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
41 import org.graalvm.compiler.java.GraphBuilderPhase;
42 import org.graalvm.compiler.nodes.ParameterNode;
43 import org.graalvm.compiler.nodes.StructuredGraph;
44 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
45 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
46 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
47 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
48 import org.graalvm.compiler.nodes.spi.LoweringTool;
49 import org.graalvm.compiler.options.OptionValues;
50 import org.graalvm.compiler.phases.OptimisticOptimizations;
51 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
52 import org.graalvm.compiler.phases.common.LoweringPhase;
53 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase;
54 import org.graalvm.compiler.phases.tiers.PhaseContext;
55 import org.graalvm.compiler.replacements.ConstantBindingParameterPlugin;
56 import org.graalvm.compiler.replacements.SnippetTemplate;
57 import org.graalvm.compiler.replacements.Snippets;
79 this(null, snippetMethodName, options, providers, linkage);
80 }
81
82 /**
83 * Creates a new snippet stub.
84 *
85 * @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is
86 * declared. If {@code null}, this the class of this object is used.
87 * @param snippetMethodName name of the single {@link Snippet} annotated method in
88 * {@code snippetDeclaringClass}
89 * @param linkage linkage details for a call to the stub
90 */
91 public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
92 super(options, providers, linkage);
93 Method javaMethod = SnippetTemplate.AbstractTemplates.findMethod(snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName, null);
94 this.method = providers.getMetaAccess().lookupJavaMethod(javaMethod);
95 }
96
97 @Override
98 @SuppressWarnings("try")
99 protected StructuredGraph getGraph(CompilationIdentifier compilationId) {
100 Plugins defaultPlugins = providers.getGraphBuilderPlugins();
101 MetaAccessProvider metaAccess = providers.getMetaAccess();
102 SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
103
104 Plugins plugins = new Plugins(defaultPlugins);
105 plugins.prependParameterPlugin(new ConstantBindingParameterPlugin(makeConstArgs(), metaAccess, snippetReflection));
106 GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
107
108 // Stubs cannot have optimistic assumptions since they have
109 // to be valid for the entire run of the VM.
110 final StructuredGraph graph = new StructuredGraph.Builder(options).method(method).compilationId(compilationId).build();
111 try (Scope outer = Debug.scope("SnippetStub", graph)) {
112 graph.disableUnsafeAccessTracking();
113
114 IntrinsicContext initialIntrinsicContext = new IntrinsicContext(method, method, getReplacementsBytecodeProvider(), INLINE_AFTER_PARSING);
115 GraphBuilderPhase.Instance instance = new GraphBuilderPhase.Instance(metaAccess, providers.getStampProvider(),
116 providers.getConstantReflection(), providers.getConstantFieldProvider(),
117 config, OptimisticOptimizations.NONE,
118 initialIntrinsicContext);
119 instance.apply(graph);
120
121 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
122 int index = param.index();
123 if (method.getParameterAnnotation(NonNullParameter.class, index) != null) {
124 param.setStamp(param.stamp().join(StampFactory.objectNonNull()));
125 }
126 }
127
128 new RemoveValueProxyPhase().apply(graph);
129 graph.setGuardsStage(GuardsStage.FLOATING_GUARDS);
130 CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
131 PhaseContext context = new PhaseContext(providers);
132 canonicalizer.apply(graph, context);
133 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
134 } catch (Throwable e) {
135 throw Debug.handle(e);
136 }
137
138 return graph;
139 }
140
141 protected BytecodeProvider getReplacementsBytecodeProvider() {
142 return providers.getReplacements().getDefaultReplacementBytecodeProvider();
143 }
144
145 protected boolean checkConstArg(int index, String expectedName) {
146 assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)"));
147 LocalVariableTable lvt = method.getLocalVariableTable();
148 if (lvt != null) {
149 Local local = lvt.getLocal(index, 0);
150 assert local != null;
151 String actualName = local.getName();
152 assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName);
153 }
154 return true;
155 }
|
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.hotspot.stubs;
24
25 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
26
27 import java.lang.reflect.Method;
28
29 import org.graalvm.compiler.api.replacements.Snippet;
30 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
31 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter;
32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
33 import org.graalvm.compiler.bytecode.BytecodeProvider;
34 import org.graalvm.compiler.core.common.CompilationIdentifier;
35 import org.graalvm.compiler.core.common.type.StampFactory;
36 import org.graalvm.compiler.debug.DebugContext;
37 import org.graalvm.compiler.debug.GraalError;
38 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
39 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
40 import org.graalvm.compiler.java.GraphBuilderPhase;
41 import org.graalvm.compiler.nodes.ParameterNode;
42 import org.graalvm.compiler.nodes.StructuredGraph;
43 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
44 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
45 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
46 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
47 import org.graalvm.compiler.nodes.spi.LoweringTool;
48 import org.graalvm.compiler.options.OptionValues;
49 import org.graalvm.compiler.phases.OptimisticOptimizations;
50 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
51 import org.graalvm.compiler.phases.common.LoweringPhase;
52 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase;
53 import org.graalvm.compiler.phases.tiers.PhaseContext;
54 import org.graalvm.compiler.replacements.ConstantBindingParameterPlugin;
55 import org.graalvm.compiler.replacements.SnippetTemplate;
56 import org.graalvm.compiler.replacements.Snippets;
78 this(null, snippetMethodName, options, providers, linkage);
79 }
80
81 /**
82 * Creates a new snippet stub.
83 *
84 * @param snippetDeclaringClass this class in which the {@link Snippet} annotated method is
85 * declared. If {@code null}, this the class of this object is used.
86 * @param snippetMethodName name of the single {@link Snippet} annotated method in
87 * {@code snippetDeclaringClass}
88 * @param linkage linkage details for a call to the stub
89 */
90 public SnippetStub(Class<? extends Snippets> snippetDeclaringClass, String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
91 super(options, providers, linkage);
92 Method javaMethod = SnippetTemplate.AbstractTemplates.findMethod(snippetDeclaringClass == null ? getClass() : snippetDeclaringClass, snippetMethodName, null);
93 this.method = providers.getMetaAccess().lookupJavaMethod(javaMethod);
94 }
95
96 @Override
97 @SuppressWarnings("try")
98 protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {
99 Plugins defaultPlugins = providers.getGraphBuilderPlugins();
100 MetaAccessProvider metaAccess = providers.getMetaAccess();
101 SnippetReflectionProvider snippetReflection = providers.getSnippetReflection();
102
103 Plugins plugins = new Plugins(defaultPlugins);
104 plugins.prependParameterPlugin(new ConstantBindingParameterPlugin(makeConstArgs(), metaAccess, snippetReflection));
105 GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
106
107 // Stubs cannot have optimistic assumptions since they have
108 // to be valid for the entire run of the VM.
109 final StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).compilationId(compilationId).build();
110 try (DebugContext.Scope outer = debug.scope("SnippetStub", graph)) {
111 graph.disableUnsafeAccessTracking();
112
113 IntrinsicContext initialIntrinsicContext = new IntrinsicContext(method, method, getReplacementsBytecodeProvider(), INLINE_AFTER_PARSING);
114 GraphBuilderPhase.Instance instance = new GraphBuilderPhase.Instance(metaAccess, providers.getStampProvider(),
115 providers.getConstantReflection(), providers.getConstantFieldProvider(),
116 config, OptimisticOptimizations.NONE,
117 initialIntrinsicContext);
118 instance.apply(graph);
119
120 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
121 int index = param.index();
122 if (method.getParameterAnnotation(NonNullParameter.class, index) != null) {
123 param.setStamp(param.stamp().join(StampFactory.objectNonNull()));
124 }
125 }
126
127 new RemoveValueProxyPhase().apply(graph);
128 graph.setGuardsStage(GuardsStage.FLOATING_GUARDS);
129 CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
130 PhaseContext context = new PhaseContext(providers);
131 canonicalizer.apply(graph, context);
132 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
133 } catch (Throwable e) {
134 throw debug.handle(e);
135 }
136
137 return graph;
138 }
139
140 protected BytecodeProvider getReplacementsBytecodeProvider() {
141 return providers.getReplacements().getDefaultReplacementBytecodeProvider();
142 }
143
144 protected boolean checkConstArg(int index, String expectedName) {
145 assert method.getParameterAnnotation(ConstantParameter.class, index) != null : String.format("parameter %d of %s is expected to be constant", index, method.format("%H.%n(%p)"));
146 LocalVariableTable lvt = method.getLocalVariableTable();
147 if (lvt != null) {
148 Local local = lvt.getLocal(index, 0);
149 assert local != null;
150 String actualName = local.getName();
151 assert actualName.equals(expectedName) : String.format("parameter %d of %s is expected to be named %s, not %s", index, method.format("%H.%n(%p)"), expectedName, actualName);
152 }
153 return true;
154 }
|