43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.replacements;
|
|
26 |
|
54601
|
27 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
|
|
28 |
import static org.graalvm.compiler.core.common.GraalOptions.UseEncodedGraphs;
|
43972
|
29 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
|
|
30 |
|
49873
|
31 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
43972
|
32 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
46640
|
33 |
import org.graalvm.compiler.debug.DebugContext;
|
49451
|
34 |
import org.graalvm.compiler.graph.SourceLanguagePositionProvider;
|
43972
|
35 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
54328
|
36 |
import org.graalvm.compiler.loop.phases.ConvertDeoptimizeToGuardPhase;
|
43972
|
37 |
import org.graalvm.compiler.nodes.EncodedGraph;
|
|
38 |
import org.graalvm.compiler.nodes.GraphEncoder;
|
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
40 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
41 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
46371
|
42 |
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
|
43972
|
43 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
46371
|
44 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
|
45 |
import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin;
|
54601
|
46 |
import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
|
46393
|
47 |
import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
|
46371
|
48 |
import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
|
55509
|
49 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
|
50 |
import org.graalvm.compiler.phases.BasePhase;
|
43972
|
51 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
52 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
53 |
import org.graalvm.compiler.phases.util.Providers;
|
|
54 |
|
|
55 |
import jdk.vm.ci.code.Architecture;
|
|
56 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* A graph decoder that provides all necessary encoded graphs on-the-fly (by parsing the methods and
|
|
60 |
* encoding the graphs).
|
|
61 |
*/
|
|
62 |
public class CachingPEGraphDecoder extends PEGraphDecoder {
|
|
63 |
|
|
64 |
protected final Providers providers;
|
|
65 |
protected final GraphBuilderConfiguration graphBuilderConfig;
|
|
66 |
protected final OptimisticOptimizations optimisticOpts;
|
|
67 |
private final AllowAssumptions allowAssumptions;
|
46344
|
68 |
private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache;
|
55509
|
69 |
private final BasePhase<? super CoreProviders> postParsingPhase;
|
43972
|
70 |
|
46371
|
71 |
public CachingPEGraphDecoder(Architecture architecture, StructuredGraph graph, Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
|
46640
|
72 |
AllowAssumptions allowAssumptions, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins, InlineInvokePlugin[] inlineInvokePlugins,
|
|
73 |
ParameterPlugin parameterPlugin,
|
58299
|
74 |
NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod, ResolvedJavaMethod callInlinedAgnosticMethod, SourceLanguagePositionProvider sourceLanguagePositionProvider,
|
|
75 |
BasePhase<? super CoreProviders> postParsingPhase, EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache) {
|
54328
|
76 |
super(architecture, graph, providers, loopExplosionPlugin,
|
58299
|
77 |
invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins, callInlinedMethod, callInlinedAgnosticMethod, sourceLanguagePositionProvider);
|
43972
|
78 |
|
|
79 |
this.providers = providers;
|
|
80 |
this.graphBuilderConfig = graphBuilderConfig;
|
|
81 |
this.optimisticOpts = optimisticOpts;
|
|
82 |
this.allowAssumptions = allowAssumptions;
|
58299
|
83 |
this.graphCache = graphCache;
|
55509
|
84 |
this.postParsingPhase = postParsingPhase;
|
43972
|
85 |
}
|
|
86 |
|
|
87 |
protected GraphBuilderPhase.Instance createGraphBuilderPhaseInstance(IntrinsicContext initialIntrinsicContext) {
|
54328
|
88 |
return new GraphBuilderPhase.Instance(providers, graphBuilderConfig, optimisticOpts, initialIntrinsicContext);
|
43972
|
89 |
}
|
|
90 |
|
|
91 |
@SuppressWarnings("try")
|
54601
|
92 |
private EncodedGraph createGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution) {
|
|
93 |
StructuredGraph graphToEncode;
|
|
94 |
if (isSubstitution && (UseEncodedGraphs.getValue(options) || IS_IN_NATIVE_IMAGE)) {
|
|
95 |
// These must go through Replacements to find the graph to use.
|
|
96 |
graphToEncode = providers.getReplacements().getMethodSubstitution(plugin, method, INLINE_AFTER_PARSING, allowAssumptions,
|
55509
|
97 |
null, options);
|
54601
|
98 |
} else {
|
|
99 |
graphToEncode = buildGraph(method, plugin, intrinsicBytecodeProvider, isSubstitution);
|
|
100 |
}
|
|
101 |
|
|
102 |
/*
|
|
103 |
* ConvertDeoptimizeToGuardPhase reduces the number of merges in the graph, so that fewer
|
|
104 |
* frame states will be created. This significantly reduces the number of nodes in the
|
|
105 |
* initial graph.
|
|
106 |
*/
|
|
107 |
try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
|
55509
|
108 |
new ConvertDeoptimizeToGuardPhase().apply(graphToEncode, providers);
|
54601
|
109 |
} catch (Throwable t) {
|
|
110 |
throw debug.handle(t);
|
|
111 |
}
|
|
112 |
|
|
113 |
EncodedGraph encodedGraph = GraphEncoder.encodeSingleGraph(graphToEncode, architecture);
|
|
114 |
graphCache.put(method, encodedGraph);
|
|
115 |
return encodedGraph;
|
|
116 |
}
|
|
117 |
|
|
118 |
@SuppressWarnings("try")
|
|
119 |
private StructuredGraph buildGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution) {
|
|
120 |
StructuredGraph graphToEncode;// @formatter:off
|
|
121 |
graphToEncode = new StructuredGraph.Builder(options, debug, allowAssumptions).
|
|
122 |
useProfilingInfo(false).
|
|
123 |
trackNodeSourcePosition(graphBuilderConfig.trackNodeSourcePosition()).
|
|
124 |
method(plugin != null ? plugin.getSubstitute(providers.getMetaAccess()) : method).
|
|
125 |
setIsSubstitution(isSubstitution).
|
|
126 |
cancellable(graph.getCancellable()).
|
|
127 |
build();
|
51736
|
128 |
// @formatter:on
|
46640
|
129 |
try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
|
54601
|
130 |
IntrinsicContext initialIntrinsicContext = intrinsicBytecodeProvider != null
|
55509
|
131 |
? new IntrinsicContext(method, plugin.getSubstitute(providers.getMetaAccess()), intrinsicBytecodeProvider, INLINE_AFTER_PARSING)
|
|
132 |
: null;
|
43972
|
133 |
GraphBuilderPhase.Instance graphBuilderPhaseInstance = createGraphBuilderPhaseInstance(initialIntrinsicContext);
|
46371
|
134 |
graphBuilderPhaseInstance.apply(graphToEncode);
|
55509
|
135 |
new CanonicalizerPhase().apply(graphToEncode, providers);
|
|
136 |
if (postParsingPhase != null) {
|
|
137 |
postParsingPhase.apply(graphToEncode, providers);
|
|
138 |
}
|
43972
|
139 |
} catch (Throwable ex) {
|
46640
|
140 |
throw debug.handle(ex);
|
43972
|
141 |
}
|
54601
|
142 |
return graphToEncode;
|
43972
|
143 |
}
|
|
144 |
|
|
145 |
@Override
|
54601
|
146 |
protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution,
|
51736
|
147 |
boolean trackNodeSourcePosition) {
|
43972
|
148 |
EncodedGraph result = graphCache.get(method);
|
|
149 |
if (result == null && method.hasBytecodes()) {
|
54601
|
150 |
result = createGraph(method, plugin, intrinsicBytecodeProvider, isSubstitution);
|
43972
|
151 |
}
|
|
152 |
return result;
|
|
153 |
}
|
|
154 |
}
|