43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2016, 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 |
package org.graalvm.compiler.replacements;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.core.common.GraalOptions.UseSnippetGraphCache;
|
46640
|
26 |
import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
|
43972
|
27 |
import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing;
|
|
28 |
import static org.graalvm.compiler.java.BytecodeParserOptions.InlineIntrinsicsDuringParsing;
|
|
29 |
import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createIntrinsicInlineInfo;
|
|
30 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
|
|
31 |
import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
|
|
32 |
|
46640
|
33 |
import java.util.Collections;
|
|
34 |
import java.util.List;
|
43972
|
35 |
import java.util.concurrent.ConcurrentHashMap;
|
|
36 |
import java.util.concurrent.ConcurrentMap;
|
46640
|
37 |
import java.util.concurrent.atomic.AtomicInteger;
|
43972
|
38 |
|
48861
|
39 |
import org.graalvm.collections.EconomicMap;
|
|
40 |
import org.graalvm.collections.Equivalence;
|
43972
|
41 |
import org.graalvm.compiler.api.replacements.Fold;
|
|
42 |
import org.graalvm.compiler.api.replacements.MethodSubstitution;
|
|
43 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
44 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
45 |
import org.graalvm.compiler.api.replacements.SnippetTemplateCache;
|
46459
|
46 |
import org.graalvm.compiler.bytecode.Bytecode;
|
43972
|
47 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
|
48 |
import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
|
|
49 |
import org.graalvm.compiler.core.common.GraalOptions;
|
|
50 |
import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
|
|
51 |
import org.graalvm.compiler.debug.DebugCloseable;
|
46640
|
52 |
import org.graalvm.compiler.debug.DebugContext;
|
|
53 |
import org.graalvm.compiler.debug.DebugContext.Description;
|
48861
|
54 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
43972
|
55 |
import org.graalvm.compiler.debug.GraalError;
|
46640
|
56 |
import org.graalvm.compiler.debug.TimerKey;
|
43972
|
57 |
import org.graalvm.compiler.graph.Node;
|
|
58 |
import org.graalvm.compiler.graph.Node.NodeIntrinsic;
|
|
59 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
|
60 |
import org.graalvm.compiler.java.GraphBuilderPhase.Instance;
|
|
61 |
import org.graalvm.compiler.nodes.CallTargetNode;
|
|
62 |
import org.graalvm.compiler.nodes.Invoke;
|
|
63 |
import org.graalvm.compiler.nodes.StateSplit;
|
|
64 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
65 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
66 |
import org.graalvm.compiler.nodes.graphbuilderconf.GeneratedInvocationPlugin;
|
|
67 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
|
68 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
|
|
69 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
70 |
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
|
|
71 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
|
72 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
73 |
import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
|
|
74 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
|
|
75 |
import org.graalvm.compiler.nodes.spi.Replacements;
|
|
76 |
import org.graalvm.compiler.nodes.spi.StampProvider;
|
46344
|
77 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
78 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
79 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
80 |
import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase;
|
|
81 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
|
82 |
import org.graalvm.compiler.phases.tiers.PhaseContext;
|
|
83 |
import org.graalvm.compiler.phases.util.Providers;
|
|
84 |
import org.graalvm.compiler.word.Word;
|
46459
|
85 |
import org.graalvm.compiler.word.WordOperationPlugin;
|
43972
|
86 |
|
|
87 |
import jdk.vm.ci.code.TargetDescription;
|
|
88 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
89 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
90 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
91 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
92 |
|
|
93 |
public class ReplacementsImpl implements Replacements, InlineInvokePlugin {
|
|
94 |
|
46344
|
95 |
protected final OptionValues options;
|
43972
|
96 |
public final Providers providers;
|
|
97 |
public final SnippetReflectionProvider snippetReflection;
|
|
98 |
public final TargetDescription target;
|
|
99 |
private GraphBuilderConfiguration.Plugins graphBuilderPlugins;
|
46640
|
100 |
private final DebugHandlersFactory debugHandlersFactory;
|
43972
|
101 |
|
46344
|
102 |
@Override
|
|
103 |
public OptionValues getOptions() {
|
|
104 |
return options;
|
|
105 |
}
|
|
106 |
|
43972
|
107 |
/**
|
|
108 |
* The preprocessed replacement graphs.
|
|
109 |
*/
|
|
110 |
protected final ConcurrentMap<ResolvedJavaMethod, StructuredGraph> graphs;
|
|
111 |
|
46459
|
112 |
/**
|
|
113 |
* The default {@link BytecodeProvider} to use for accessing the bytecode of a replacement if
|
|
114 |
* the replacement doesn't provide another {@link BytecodeProvider}.
|
|
115 |
*/
|
|
116 |
protected final BytecodeProvider defaultBytecodeProvider;
|
43972
|
117 |
|
|
118 |
public void setGraphBuilderPlugins(GraphBuilderConfiguration.Plugins plugins) {
|
|
119 |
assert this.graphBuilderPlugins == null;
|
|
120 |
this.graphBuilderPlugins = plugins;
|
|
121 |
}
|
|
122 |
|
46459
|
123 |
@Override
|
43972
|
124 |
public GraphBuilderConfiguration.Plugins getGraphBuilderPlugins() {
|
|
125 |
return graphBuilderPlugins;
|
|
126 |
}
|
|
127 |
|
|
128 |
protected boolean hasGeneratedInvocationPluginAnnotation(ResolvedJavaMethod method) {
|
|
129 |
return method.getAnnotation(Node.NodeIntrinsic.class) != null || method.getAnnotation(Fold.class) != null;
|
|
130 |
}
|
|
131 |
|
|
132 |
protected boolean hasGenericInvocationPluginAnnotation(ResolvedJavaMethod method) {
|
|
133 |
return method.getAnnotation(Word.Operation.class) != null;
|
|
134 |
}
|
|
135 |
|
|
136 |
private static final int MAX_GRAPH_INLINING_DEPTH = 100; // more than enough
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Determines whether a given method should be inlined based on whether it has a substitution or
|
|
140 |
* whether the inlining context is already within a substitution.
|
|
141 |
*
|
|
142 |
* @return an object specifying how {@code method} is to be inlined or null if it should not be
|
|
143 |
* inlined based on substitution related criteria
|
|
144 |
*/
|
|
145 |
@Override
|
|
146 |
public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
|
46459
|
147 |
Bytecode subst = getSubstitutionBytecode(method);
|
43972
|
148 |
if (subst != null) {
|
46344
|
149 |
if (b.parsingIntrinsic() || InlineDuringParsing.getValue(b.getOptions()) || InlineIntrinsicsDuringParsing.getValue(b.getOptions())) {
|
43972
|
150 |
// Forced inlining of intrinsics
|
46459
|
151 |
return createIntrinsicInlineInfo(subst.getMethod(), subst.getOrigin());
|
43972
|
152 |
}
|
|
153 |
return null;
|
|
154 |
}
|
|
155 |
if (b.parsingIntrinsic()) {
|
|
156 |
assert b.getDepth() < MAX_GRAPH_INLINING_DEPTH : "inlining limit exceeded";
|
|
157 |
|
|
158 |
// Force inlining when parsing replacements
|
46459
|
159 |
return createIntrinsicInlineInfo(method, defaultBytecodeProvider);
|
43972
|
160 |
} else {
|
|
161 |
assert method.getAnnotation(NodeIntrinsic.class) == null : String.format("@%s method %s must only be called from within a replacement%n%s", NodeIntrinsic.class.getSimpleName(),
|
|
162 |
method.format("%h.%n"), b);
|
|
163 |
}
|
|
164 |
return null;
|
|
165 |
}
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public void notifyNotInlined(GraphBuilderContext b, ResolvedJavaMethod method, Invoke invoke) {
|
|
169 |
if (b.parsingIntrinsic()) {
|
|
170 |
IntrinsicContext intrinsic = b.getIntrinsic();
|
|
171 |
if (!intrinsic.isCallToOriginal(method)) {
|
46344
|
172 |
if (hasGeneratedInvocationPluginAnnotation(method)) {
|
|
173 |
throw new GraalError("%s should have been handled by a %s", method.format("%H.%n(%p)"), GeneratedInvocationPlugin.class.getSimpleName());
|
|
174 |
}
|
|
175 |
if (hasGenericInvocationPluginAnnotation(method)) {
|
|
176 |
throw new GraalError("%s should have been handled by %s", method.format("%H.%n(%p)"), WordOperationPlugin.class.getSimpleName());
|
|
177 |
}
|
|
178 |
|
43972
|
179 |
throw new GraalError("All non-recursive calls in the intrinsic %s must be inlined or intrinsified: found call to %s",
|
|
180 |
intrinsic.getIntrinsicMethod().format("%H.%n(%p)"), method.format("%h.%n(%p)"));
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
// This map is key'ed by a class name instead of a Class object so that
|
|
186 |
// it is stable across VM executions (in support of replay compilation).
|
46344
|
187 |
private final EconomicMap<String, SnippetTemplateCache> snippetTemplateCache;
|
43972
|
188 |
|
46640
|
189 |
public ReplacementsImpl(OptionValues options, DebugHandlersFactory debugHandlersFactory, Providers providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider,
|
|
190 |
TargetDescription target) {
|
46344
|
191 |
this.options = options;
|
43972
|
192 |
this.providers = providers.copyWith(this);
|
|
193 |
this.snippetReflection = snippetReflection;
|
|
194 |
this.target = target;
|
|
195 |
this.graphs = new ConcurrentHashMap<>();
|
46344
|
196 |
this.snippetTemplateCache = EconomicMap.create(Equivalence.DEFAULT);
|
46459
|
197 |
this.defaultBytecodeProvider = bytecodeProvider;
|
46640
|
198 |
this.debugHandlersFactory = debugHandlersFactory;
|
|
199 |
|
43972
|
200 |
}
|
|
201 |
|
46640
|
202 |
private static final TimerKey SnippetPreparationTime = DebugContext.timer("SnippetPreparationTime");
|
43972
|
203 |
|
|
204 |
@Override
|
|
205 |
public StructuredGraph getSnippet(ResolvedJavaMethod method, Object[] args) {
|
|
206 |
return getSnippet(method, null, args);
|
|
207 |
}
|
|
208 |
|
46640
|
209 |
private static final AtomicInteger nextDebugContextId = new AtomicInteger();
|
|
210 |
|
|
211 |
protected DebugContext openDebugContext(String idPrefix, ResolvedJavaMethod method) {
|
|
212 |
DebugContext outer = DebugContext.forCurrentThread();
|
|
213 |
Description description = new Description(method, idPrefix + nextDebugContextId.incrementAndGet());
|
|
214 |
List<DebugHandlersFactory> factories = debugHandlersFactory == null ? Collections.emptyList() : Collections.singletonList(debugHandlersFactory);
|
|
215 |
return DebugContext.create(options, description, outer.getGlobalMetrics(), DEFAULT_LOG_STREAM, factories);
|
|
216 |
}
|
|
217 |
|
43972
|
218 |
@Override
|
|
219 |
@SuppressWarnings("try")
|
|
220 |
public StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args) {
|
|
221 |
assert method.getAnnotation(Snippet.class) != null : "Snippet must be annotated with @" + Snippet.class.getSimpleName();
|
|
222 |
assert method.hasBytecodes() : "Snippet must not be abstract or native";
|
|
223 |
|
46344
|
224 |
StructuredGraph graph = UseSnippetGraphCache.getValue(options) ? graphs.get(method) : null;
|
43972
|
225 |
if (graph == null) {
|
46640
|
226 |
try (DebugContext debug = openDebugContext("Snippet_", method);
|
|
227 |
DebugCloseable a = SnippetPreparationTime.start(debug)) {
|
|
228 |
StructuredGraph newGraph = makeGraph(debug, defaultBytecodeProvider, method, args, recursiveEntry);
|
|
229 |
DebugContext.counter("SnippetNodeCount[%#s]", method).add(newGraph.getDebug(), newGraph.getNodeCount());
|
46344
|
230 |
if (!UseSnippetGraphCache.getValue(options) || args != null) {
|
43972
|
231 |
return newGraph;
|
|
232 |
}
|
46640
|
233 |
newGraph.freeze();
|
43972
|
234 |
graphs.putIfAbsent(method, newGraph);
|
|
235 |
graph = graphs.get(method);
|
|
236 |
}
|
|
237 |
}
|
|
238 |
return graph;
|
|
239 |
}
|
|
240 |
|
|
241 |
@Override
|
|
242 |
public void registerSnippet(ResolvedJavaMethod method) {
|
|
243 |
// No initialization needed as snippet graphs are created on demand in getSnippet
|
|
244 |
}
|
|
245 |
|
|
246 |
@Override
|
|
247 |
public boolean hasSubstitution(ResolvedJavaMethod method, int invokeBci) {
|
|
248 |
InvocationPlugin plugin = graphBuilderPlugins.getInvocationPlugins().lookupInvocation(method);
|
|
249 |
return plugin != null && (!plugin.inlineOnly() || invokeBci >= 0);
|
|
250 |
}
|
|
251 |
|
|
252 |
@Override
|
46459
|
253 |
public BytecodeProvider getDefaultReplacementBytecodeProvider() {
|
|
254 |
return defaultBytecodeProvider;
|
43972
|
255 |
}
|
|
256 |
|
|
257 |
@Override
|
46459
|
258 |
public Bytecode getSubstitutionBytecode(ResolvedJavaMethod method) {
|
43972
|
259 |
InvocationPlugin plugin = graphBuilderPlugins.getInvocationPlugins().lookupInvocation(method);
|
|
260 |
if (plugin instanceof MethodSubstitutionPlugin) {
|
|
261 |
MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
|
46459
|
262 |
ResolvedJavaMethod substitute = msPlugin.getSubstitute(providers.getMetaAccess());
|
|
263 |
return msPlugin.getBytecodeProvider().getBytecode(substitute);
|
43972
|
264 |
}
|
|
265 |
return null;
|
|
266 |
}
|
|
267 |
|
|
268 |
@Override
|
|
269 |
public StructuredGraph getSubstitution(ResolvedJavaMethod method, int invokeBci) {
|
|
270 |
StructuredGraph result;
|
|
271 |
InvocationPlugin plugin = graphBuilderPlugins.getInvocationPlugins().lookupInvocation(method);
|
|
272 |
if (plugin != null && (!plugin.inlineOnly() || invokeBci >= 0)) {
|
|
273 |
MetaAccessProvider metaAccess = providers.getMetaAccess();
|
|
274 |
if (plugin instanceof MethodSubstitutionPlugin) {
|
|
275 |
MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
|
|
276 |
ResolvedJavaMethod substitute = msPlugin.getSubstitute(metaAccess);
|
48861
|
277 |
StructuredGraph graph = UseSnippetGraphCache.getValue(options) ? graphs.get(substitute) : null;
|
43972
|
278 |
if (graph == null) {
|
46640
|
279 |
try (DebugContext debug = openDebugContext("Substitution_", method)) {
|
|
280 |
graph = makeGraph(debug, msPlugin.getBytecodeProvider(), substitute, null, method);
|
48861
|
281 |
if (!UseSnippetGraphCache.getValue(options)) {
|
|
282 |
return graph;
|
|
283 |
}
|
46640
|
284 |
graph.freeze();
|
|
285 |
graphs.putIfAbsent(substitute, graph);
|
|
286 |
graph = graphs.get(substitute);
|
|
287 |
}
|
43972
|
288 |
}
|
|
289 |
assert graph.isFrozen();
|
|
290 |
result = graph;
|
|
291 |
} else {
|
46459
|
292 |
Bytecode code = new ResolvedJavaMethodBytecode(method);
|
43972
|
293 |
ConstantReflectionProvider constantReflection = providers.getConstantReflection();
|
|
294 |
ConstantFieldProvider constantFieldProvider = providers.getConstantFieldProvider();
|
|
295 |
StampProvider stampProvider = providers.getStampProvider();
|
46640
|
296 |
try (DebugContext debug = openDebugContext("Substitution_", method)) {
|
|
297 |
result = new IntrinsicGraphBuilder(options, debug, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci).buildGraph(plugin);
|
|
298 |
}
|
43972
|
299 |
}
|
|
300 |
} else {
|
|
301 |
result = null;
|
|
302 |
}
|
|
303 |
return result;
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
|
307 |
* Creates a preprocessed graph for a snippet or method substitution.
|
|
308 |
*
|
46459
|
309 |
* @param bytecodeProvider how to access the bytecode of {@code method}
|
43972
|
310 |
* @param method the snippet or method substitution for which a graph will be created
|
|
311 |
* @param args
|
|
312 |
* @param original the original method if {@code method} is a {@linkplain MethodSubstitution
|
|
313 |
* substitution} otherwise null
|
|
314 |
*/
|
46640
|
315 |
public StructuredGraph makeGraph(DebugContext debug, BytecodeProvider bytecodeProvider, ResolvedJavaMethod method, Object[] args, ResolvedJavaMethod original) {
|
|
316 |
return createGraphMaker(method, original).makeGraph(debug, bytecodeProvider, args);
|
43972
|
317 |
}
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Can be overridden to return an object that specializes various parts of graph preprocessing.
|
|
321 |
*/
|
|
322 |
protected GraphMaker createGraphMaker(ResolvedJavaMethod substitute, ResolvedJavaMethod original) {
|
|
323 |
return new GraphMaker(this, substitute, original);
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Creates and preprocesses a graph for a replacement.
|
|
328 |
*/
|
|
329 |
public static class GraphMaker {
|
|
330 |
|
|
331 |
/** The replacements object that the graphs are created for. */
|
|
332 |
protected final ReplacementsImpl replacements;
|
|
333 |
|
|
334 |
/**
|
|
335 |
* The method for which a graph is being created.
|
|
336 |
*/
|
|
337 |
protected final ResolvedJavaMethod method;
|
|
338 |
|
|
339 |
/**
|
|
340 |
* The original method which {@link #method} is substituting. Calls to {@link #method} or
|
|
341 |
* {@link #substitutedMethod} will be replaced with a forced inline of
|
|
342 |
* {@link #substitutedMethod}.
|
|
343 |
*/
|
|
344 |
protected final ResolvedJavaMethod substitutedMethod;
|
|
345 |
|
|
346 |
protected GraphMaker(ReplacementsImpl replacements, ResolvedJavaMethod substitute, ResolvedJavaMethod substitutedMethod) {
|
|
347 |
this.replacements = replacements;
|
|
348 |
this.method = substitute;
|
|
349 |
this.substitutedMethod = substitutedMethod;
|
|
350 |
}
|
|
351 |
|
|
352 |
@SuppressWarnings("try")
|
46640
|
353 |
public StructuredGraph makeGraph(DebugContext debug, BytecodeProvider bytecodeProvider, Object[] args) {
|
|
354 |
try (DebugContext.Scope s = debug.scope("BuildSnippetGraph", method)) {
|
43972
|
355 |
assert method.hasBytecodes() : method;
|
46640
|
356 |
StructuredGraph graph = buildInitialGraph(debug, bytecodeProvider, method, args);
|
43972
|
357 |
|
|
358 |
finalizeGraph(graph);
|
|
359 |
|
46640
|
360 |
debug.dump(DebugContext.INFO_LEVEL, graph, "%s: Final", method.getName());
|
43972
|
361 |
|
|
362 |
return graph;
|
|
363 |
} catch (Throwable e) {
|
46640
|
364 |
throw debug.handle(e);
|
43972
|
365 |
}
|
|
366 |
}
|
|
367 |
|
|
368 |
/**
|
|
369 |
* Does final processing of a snippet graph.
|
|
370 |
*/
|
|
371 |
protected void finalizeGraph(StructuredGraph graph) {
|
46344
|
372 |
if (!GraalOptions.SnippetCounters.getValue(replacements.options) || graph.getNodes().filter(SnippetCounterNode.class).isEmpty()) {
|
43972
|
373 |
int sideEffectCount = 0;
|
|
374 |
assert (sideEffectCount = graph.getNodes().filter(e -> hasSideEffect(e)).count()) >= 0;
|
|
375 |
new ConvertDeoptimizeToGuardPhase().apply(graph, null);
|
|
376 |
assert sideEffectCount == graph.getNodes().filter(e -> hasSideEffect(e)).count() : "deleted side effecting node";
|
|
377 |
|
|
378 |
new DeadCodeEliminationPhase(Required).apply(graph);
|
|
379 |
} else {
|
|
380 |
// ConvertDeoptimizeToGuardPhase will eliminate snippet counters on paths
|
|
381 |
// that terminate in a deopt so we disable it if the graph contains
|
|
382 |
// snippet counters. The trade off is that we miss out on guard
|
|
383 |
// coalescing opportunities.
|
|
384 |
}
|
|
385 |
}
|
|
386 |
|
|
387 |
/**
|
|
388 |
* Filter nodes which have side effects and shouldn't be deleted from snippets when
|
|
389 |
* converting deoptimizations to guards. Currently this only allows exception constructors
|
|
390 |
* to be eliminated to cover the case when Java assertions are in the inlined code.
|
|
391 |
*
|
|
392 |
* @param node
|
|
393 |
* @return true for nodes that have side effects and are unsafe to delete
|
|
394 |
*/
|
|
395 |
private boolean hasSideEffect(Node node) {
|
|
396 |
if (node instanceof StateSplit) {
|
|
397 |
if (((StateSplit) node).hasSideEffect()) {
|
|
398 |
if (node instanceof Invoke) {
|
|
399 |
CallTargetNode callTarget = ((Invoke) node).callTarget();
|
|
400 |
if (callTarget instanceof MethodCallTargetNode) {
|
|
401 |
ResolvedJavaMethod targetMethod = ((MethodCallTargetNode) callTarget).targetMethod();
|
|
402 |
if (targetMethod.isConstructor()) {
|
|
403 |
ResolvedJavaType throwableType = replacements.providers.getMetaAccess().lookupJavaType(Throwable.class);
|
|
404 |
return !throwableType.isAssignableFrom(targetMethod.getDeclaringClass());
|
|
405 |
}
|
|
406 |
}
|
|
407 |
}
|
|
408 |
// Not an exception constructor call
|
|
409 |
return true;
|
|
410 |
}
|
|
411 |
}
|
|
412 |
// Not a StateSplit
|
|
413 |
return false;
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
46459
|
417 |
* Builds the initial graph for a replacement.
|
43972
|
418 |
*/
|
|
419 |
@SuppressWarnings("try")
|
46640
|
420 |
protected StructuredGraph buildInitialGraph(DebugContext debug, BytecodeProvider bytecodeProvider, final ResolvedJavaMethod methodToParse, Object[] args) {
|
43972
|
421 |
// Replacements cannot have optimistic assumptions since they have
|
|
422 |
// to be valid for the entire run of the VM.
|
46640
|
423 |
final StructuredGraph graph = new StructuredGraph.Builder(replacements.options, debug).method(methodToParse).build();
|
43972
|
424 |
|
46640
|
425 |
// Replacements are not user code so they do not participate in unsafe access
|
|
426 |
// tracking
|
43972
|
427 |
graph.disableUnsafeAccessTracking();
|
|
428 |
|
46640
|
429 |
try (DebugContext.Scope s = debug.scope("buildInitialGraph", graph)) {
|
43972
|
430 |
MetaAccessProvider metaAccess = replacements.providers.getMetaAccess();
|
|
431 |
|
|
432 |
Plugins plugins = new Plugins(replacements.graphBuilderPlugins);
|
|
433 |
GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
|
|
434 |
if (args != null) {
|
|
435 |
plugins.prependParameterPlugin(new ConstantBindingParameterPlugin(args, metaAccess, replacements.snippetReflection));
|
|
436 |
}
|
|
437 |
|
|
438 |
IntrinsicContext initialIntrinsicContext = null;
|
47798
|
439 |
Snippet snippetAnnotation = method.getAnnotation(Snippet.class);
|
|
440 |
if (snippetAnnotation == null) {
|
43972
|
441 |
// Post-parse inlined intrinsic
|
46459
|
442 |
initialIntrinsicContext = new IntrinsicContext(substitutedMethod, method, bytecodeProvider, INLINE_AFTER_PARSING);
|
43972
|
443 |
} else {
|
|
444 |
// Snippet
|
|
445 |
ResolvedJavaMethod original = substitutedMethod != null ? substitutedMethod : method;
|
47798
|
446 |
initialIntrinsicContext = new IntrinsicContext(original, method, bytecodeProvider, INLINE_AFTER_PARSING, snippetAnnotation.allowPartialIntrinsicArgumentMismatch());
|
43972
|
447 |
}
|
|
448 |
|
|
449 |
createGraphBuilder(metaAccess, replacements.providers.getStampProvider(), replacements.providers.getConstantReflection(), replacements.providers.getConstantFieldProvider(), config,
|
|
450 |
OptimisticOptimizations.NONE, initialIntrinsicContext).apply(graph);
|
|
451 |
|
|
452 |
new CanonicalizerPhase().apply(graph, new PhaseContext(replacements.providers));
|
|
453 |
} catch (Throwable e) {
|
46640
|
454 |
throw debug.handle(e);
|
43972
|
455 |
}
|
|
456 |
return graph;
|
|
457 |
}
|
|
458 |
|
|
459 |
protected Instance createGraphBuilder(MetaAccessProvider metaAccess, StampProvider stampProvider, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
|
|
460 |
GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) {
|
|
461 |
return new GraphBuilderPhase.Instance(metaAccess, stampProvider, constantReflection, constantFieldProvider, graphBuilderConfig, optimisticOpts,
|
|
462 |
initialIntrinsicContext);
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
@Override
|
|
467 |
public void registerSnippetTemplateCache(SnippetTemplateCache templates) {
|
|
468 |
assert snippetTemplateCache.get(templates.getClass().getName()) == null;
|
|
469 |
snippetTemplateCache.put(templates.getClass().getName(), templates);
|
|
470 |
}
|
|
471 |
|
|
472 |
@Override
|
|
473 |
public <T extends SnippetTemplateCache> T getSnippetTemplateCache(Class<T> templatesClass) {
|
|
474 |
SnippetTemplateCache ret = snippetTemplateCache.get(templatesClass.getName());
|
|
475 |
return templatesClass.cast(ret);
|
|
476 |
}
|
|
477 |
}
|