43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
|
|
26 |
|
48861
|
27 |
import org.graalvm.collections.EconomicMap;
|
43972
|
28 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
46640
|
29 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
30 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
|
31 |
import org.graalvm.compiler.nodes.EncodedGraph;
|
|
32 |
import org.graalvm.compiler.nodes.GraphEncoder;
|
|
33 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
34 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
35 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
46371
|
36 |
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
|
43972
|
37 |
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
|
46371
|
38 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
|
39 |
import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin;
|
46393
|
40 |
import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
|
46371
|
41 |
import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
|
43972
|
42 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
43 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
44 |
import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase;
|
|
45 |
import org.graalvm.compiler.phases.tiers.PhaseContext;
|
|
46 |
import org.graalvm.compiler.phases.util.Providers;
|
|
47 |
|
|
48 |
import jdk.vm.ci.code.Architecture;
|
|
49 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* A graph decoder that provides all necessary encoded graphs on-the-fly (by parsing the methods and
|
|
53 |
* encoding the graphs).
|
|
54 |
*/
|
|
55 |
public class CachingPEGraphDecoder extends PEGraphDecoder {
|
|
56 |
|
|
57 |
protected final Providers providers;
|
|
58 |
protected final GraphBuilderConfiguration graphBuilderConfig;
|
|
59 |
protected final OptimisticOptimizations optimisticOpts;
|
|
60 |
private final AllowAssumptions allowAssumptions;
|
46344
|
61 |
private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache;
|
43972
|
62 |
|
46371
|
63 |
public CachingPEGraphDecoder(Architecture architecture, StructuredGraph graph, Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
|
46640
|
64 |
AllowAssumptions allowAssumptions, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins, InlineInvokePlugin[] inlineInvokePlugins,
|
|
65 |
ParameterPlugin parameterPlugin,
|
47667
|
66 |
NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod) {
|
46640
|
67 |
super(architecture, graph, providers.getMetaAccess(), providers.getConstantReflection(), providers.getConstantFieldProvider(), providers.getStampProvider(), loopExplosionPlugin,
|
47667
|
68 |
invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins, callInlinedMethod);
|
43972
|
69 |
|
|
70 |
this.providers = providers;
|
|
71 |
this.graphBuilderConfig = graphBuilderConfig;
|
|
72 |
this.optimisticOpts = optimisticOpts;
|
|
73 |
this.allowAssumptions = allowAssumptions;
|
46344
|
74 |
this.graphCache = EconomicMap.create();
|
43972
|
75 |
}
|
|
76 |
|
|
77 |
protected GraphBuilderPhase.Instance createGraphBuilderPhaseInstance(IntrinsicContext initialIntrinsicContext) {
|
|
78 |
return new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), graphBuilderConfig,
|
|
79 |
optimisticOpts, initialIntrinsicContext);
|
|
80 |
}
|
|
81 |
|
|
82 |
@SuppressWarnings("try")
|
|
83 |
private EncodedGraph createGraph(ResolvedJavaMethod method, BytecodeProvider intrinsicBytecodeProvider) {
|
46640
|
84 |
StructuredGraph graphToEncode = new StructuredGraph.Builder(options, debug, allowAssumptions).useProfilingInfo(false).method(method).build();
|
|
85 |
try (DebugContext.Scope scope = debug.scope("createGraph", graphToEncode)) {
|
43972
|
86 |
IntrinsicContext initialIntrinsicContext = intrinsicBytecodeProvider != null ? new IntrinsicContext(method, method, intrinsicBytecodeProvider, INLINE_AFTER_PARSING) : null;
|
|
87 |
GraphBuilderPhase.Instance graphBuilderPhaseInstance = createGraphBuilderPhaseInstance(initialIntrinsicContext);
|
46371
|
88 |
graphBuilderPhaseInstance.apply(graphToEncode);
|
43972
|
89 |
|
|
90 |
PhaseContext context = new PhaseContext(providers);
|
46371
|
91 |
new CanonicalizerPhase().apply(graphToEncode, context);
|
43972
|
92 |
/*
|
|
93 |
* ConvertDeoptimizeToGuardPhase reduces the number of merges in the graph, so that
|
|
94 |
* fewer frame states will be created. This significantly reduces the number of nodes in
|
|
95 |
* the initial graph.
|
|
96 |
*/
|
46371
|
97 |
new ConvertDeoptimizeToGuardPhase().apply(graphToEncode, context);
|
43972
|
98 |
|
46371
|
99 |
EncodedGraph encodedGraph = GraphEncoder.encodeSingleGraph(graphToEncode, architecture);
|
43972
|
100 |
graphCache.put(method, encodedGraph);
|
|
101 |
return encodedGraph;
|
|
102 |
|
|
103 |
} catch (Throwable ex) {
|
46640
|
104 |
throw debug.handle(ex);
|
43972
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, BytecodeProvider intrinsicBytecodeProvider) {
|
|
110 |
EncodedGraph result = graphCache.get(method);
|
|
111 |
if (result == null && method.hasBytecodes()) {
|
|
112 |
result = createGraph(method, intrinsicBytecodeProvider);
|
|
113 |
}
|
|
114 |
return result;
|
|
115 |
}
|
|
116 |
}
|