43972
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2011, 2018, 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.hotspot.phases;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
|
|
28 |
|
46344
|
29 |
import org.graalvm.compiler.core.common.PermanentBailoutException;
|
43972
|
30 |
import org.graalvm.compiler.core.common.cfg.Loop;
|
47798
|
31 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
46344
|
32 |
import org.graalvm.compiler.core.common.type.Stamp;
|
46640
|
33 |
import org.graalvm.compiler.debug.CounterKey;
|
49873
|
34 |
import org.graalvm.compiler.debug.DebugCloseable;
|
46640
|
35 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
36 |
import org.graalvm.compiler.debug.GraalError;
|
|
37 |
import org.graalvm.compiler.graph.Node;
|
|
38 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
54084
|
39 |
import org.graalvm.compiler.loop.LoopEx;
|
43972
|
40 |
import org.graalvm.compiler.loop.LoopsData;
|
|
41 |
import org.graalvm.compiler.loop.phases.LoopTransformations;
|
|
42 |
import org.graalvm.compiler.nodeinfo.InputType;
|
|
43 |
import org.graalvm.compiler.nodeinfo.Verbosity;
|
|
44 |
import org.graalvm.compiler.nodes.EntryMarkerNode;
|
|
45 |
import org.graalvm.compiler.nodes.EntryProxyNode;
|
47798
|
46 |
import org.graalvm.compiler.nodes.FixedGuardNode;
|
43972
|
47 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
48 |
import org.graalvm.compiler.nodes.FrameState;
|
47798
|
49 |
import org.graalvm.compiler.nodes.LogicNode;
|
46344
|
50 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
48190
|
51 |
import org.graalvm.compiler.nodes.NodeView;
|
46344
|
52 |
import org.graalvm.compiler.nodes.ParameterNode;
|
47798
|
53 |
import org.graalvm.compiler.nodes.PiNode;
|
43972
|
54 |
import org.graalvm.compiler.nodes.StartNode;
|
|
55 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
56 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
57 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
58 |
import org.graalvm.compiler.nodes.extended.OSRLocalNode;
|
46344
|
59 |
import org.graalvm.compiler.nodes.extended.OSRLockNode;
|
|
60 |
import org.graalvm.compiler.nodes.extended.OSRMonitorEnterNode;
|
43972
|
61 |
import org.graalvm.compiler.nodes.extended.OSRStartNode;
|
46344
|
62 |
import org.graalvm.compiler.nodes.java.AccessMonitorNode;
|
47798
|
63 |
import org.graalvm.compiler.nodes.java.InstanceOfNode;
|
46344
|
64 |
import org.graalvm.compiler.nodes.java.MonitorEnterNode;
|
|
65 |
import org.graalvm.compiler.nodes.java.MonitorExitNode;
|
|
66 |
import org.graalvm.compiler.nodes.java.MonitorIdNode;
|
43972
|
67 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
46344
|
68 |
import org.graalvm.compiler.options.Option;
|
|
69 |
import org.graalvm.compiler.options.OptionKey;
|
|
70 |
import org.graalvm.compiler.options.OptionType;
|
|
71 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
72 |
import org.graalvm.compiler.phases.Phase;
|
|
73 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
54084
|
74 |
import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup;
|
43972
|
75 |
|
49873
|
76 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
77 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
78 |
import jdk.vm.ci.meta.JavaKind;
|
50858
|
79 |
import jdk.vm.ci.meta.SpeculationLog;
|
52956
|
80 |
import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
|
43972
|
81 |
import jdk.vm.ci.runtime.JVMCICompiler;
|
|
82 |
|
|
83 |
public class OnStackReplacementPhase extends Phase {
|
|
84 |
|
46344
|
85 |
public static class Options {
|
|
86 |
// @formatter:off
|
|
87 |
@Option(help = "Deoptimize OSR compiled code when the OSR entry loop is finished " +
|
|
88 |
"if there is no mature profile available for the rest of the method.", type = OptionType.Debug)
|
|
89 |
public static final OptionKey<Boolean> DeoptAfterOSR = new OptionKey<>(true);
|
|
90 |
@Option(help = "Support OSR compilations with locks. If DeoptAfterOSR is true we can per definition not have " +
|
54084
|
91 |
"unbalanced enter/exits mappings. If DeoptAfterOSR is false insert artificial monitor enters after " +
|
46344
|
92 |
"the OSRStart to have balanced enter/exits in the graph.", type = OptionType.Debug)
|
|
93 |
public static final OptionKey<Boolean> SupportOSRWithLocks = new OptionKey<>(true);
|
|
94 |
// @formatter:on
|
|
95 |
}
|
|
96 |
|
46640
|
97 |
private static final CounterKey OsrWithLocksCount = DebugContext.counter("OSRWithLocks");
|
46344
|
98 |
|
|
99 |
private static boolean supportOSRWithLocks(OptionValues options) {
|
|
100 |
return Options.SupportOSRWithLocks.getValue(options);
|
|
101 |
}
|
|
102 |
|
54084
|
103 |
private static final SpeculationReasonGroup OSR_LOCAL_SPECULATIONS = new SpeculationReasonGroup("OSRLocal", int.class, Stamp.class, int.class);
|
|
104 |
|
43972
|
105 |
@Override
|
49873
|
106 |
@SuppressWarnings("try")
|
43972
|
107 |
protected void run(StructuredGraph graph) {
|
46640
|
108 |
DebugContext debug = graph.getDebug();
|
43972
|
109 |
if (graph.getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI) {
|
|
110 |
// This happens during inlining in a OSR method, because the same phase plan will be
|
|
111 |
// used.
|
|
112 |
assert graph.getNodes(EntryMarkerNode.TYPE).isEmpty();
|
|
113 |
return;
|
|
114 |
}
|
46640
|
115 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement initial at bci %d", graph.getEntryBCI());
|
46344
|
116 |
|
43972
|
117 |
EntryMarkerNode osr;
|
|
118 |
int maxIterations = -1;
|
|
119 |
int iterations = 0;
|
46344
|
120 |
|
|
121 |
final EntryMarkerNode originalOSRNode = getEntryMarker(graph);
|
|
122 |
final LoopBeginNode originalOSRLoop = osrLoop(originalOSRNode);
|
|
123 |
final boolean currentOSRWithLocks = osrWithLocks(originalOSRNode);
|
|
124 |
|
|
125 |
if (originalOSRLoop == null) {
|
|
126 |
/*
|
|
127 |
* OSR with Locks: We do not have an OSR loop for the original OSR bci. Therefore we
|
|
128 |
* cannot decide where to deopt and which framestate will be used. In the worst case the
|
|
129 |
* framestate of the OSR entry would be used.
|
|
130 |
*/
|
|
131 |
throw new PermanentBailoutException("OSR compilation without OSR entry loop.");
|
|
132 |
}
|
|
133 |
|
|
134 |
if (!supportOSRWithLocks(graph.getOptions()) && currentOSRWithLocks) {
|
|
135 |
throw new PermanentBailoutException("OSR with locks disabled.");
|
|
136 |
}
|
|
137 |
|
43972
|
138 |
do {
|
|
139 |
osr = getEntryMarker(graph);
|
|
140 |
LoopsData loops = new LoopsData(graph);
|
|
141 |
// Find the loop that contains the EntryMarker
|
|
142 |
Loop<Block> l = loops.getCFG().getNodeToBlock().get(osr).getLoop();
|
|
143 |
if (l == null) {
|
|
144 |
break;
|
|
145 |
}
|
46344
|
146 |
|
43972
|
147 |
iterations++;
|
|
148 |
if (maxIterations == -1) {
|
|
149 |
maxIterations = l.getDepth();
|
|
150 |
} else if (iterations > maxIterations) {
|
|
151 |
throw GraalError.shouldNotReachHere();
|
|
152 |
}
|
|
153 |
// Peel the outermost loop first
|
|
154 |
while (l.getParent() != null) {
|
|
155 |
l = l.getParent();
|
|
156 |
}
|
|
157 |
|
54084
|
158 |
LoopEx loop = loops.loop(l);
|
|
159 |
loop.loopBegin().markOsrLoop();
|
|
160 |
LoopTransformations.peel(loop);
|
57537
|
161 |
|
|
162 |
osr.prepareDelete();
|
43972
|
163 |
GraphUtil.removeFixedWithUnusedInputs(osr);
|
46640
|
164 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement loop peeling result");
|
43972
|
165 |
} while (true);
|
|
166 |
|
49873
|
167 |
StartNode start = graph.start();
|
43972
|
168 |
FrameState osrState = osr.stateAfter();
|
49873
|
169 |
OSRStartNode osrStart;
|
|
170 |
try (DebugCloseable context = osr.withNodeSourcePosition()) {
|
|
171 |
osr.setStateAfter(null);
|
|
172 |
osrStart = graph.add(new OSRStartNode());
|
|
173 |
FixedNode next = osr.next();
|
|
174 |
osr.setNext(null);
|
|
175 |
osrStart.setNext(next);
|
|
176 |
graph.setStart(osrStart);
|
|
177 |
osrStart.setStateAfter(osrState);
|
|
178 |
|
|
179 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after setting OSR start");
|
|
180 |
final int localsSize = osrState.localsSize();
|
|
181 |
final int locksSize = osrState.locksSize();
|
43972
|
182 |
|
49873
|
183 |
for (int i = 0; i < localsSize + locksSize; i++) {
|
54084
|
184 |
ValueNode value;
|
49873
|
185 |
if (i >= localsSize) {
|
|
186 |
value = osrState.lockAt(i - localsSize);
|
|
187 |
} else {
|
|
188 |
value = osrState.localAt(i);
|
|
189 |
}
|
|
190 |
if (value instanceof EntryProxyNode) {
|
|
191 |
EntryProxyNode proxy = (EntryProxyNode) value;
|
|
192 |
/*
|
|
193 |
* We need to drop the stamp since the types we see during OSR may be too
|
|
194 |
* precise (if a branch was not parsed for example). In cases when this is
|
|
195 |
* possible, we insert a guard and narrow the OSRLocal stamp at its usages.
|
|
196 |
*/
|
|
197 |
Stamp narrowedStamp = proxy.value().stamp(NodeView.DEFAULT);
|
|
198 |
Stamp unrestrictedStamp = proxy.stamp(NodeView.DEFAULT).unrestricted();
|
|
199 |
ValueNode osrLocal;
|
|
200 |
if (i >= localsSize) {
|
|
201 |
osrLocal = graph.addOrUnique(new OSRLockNode(i - localsSize, unrestrictedStamp));
|
|
202 |
} else {
|
|
203 |
osrLocal = graph.addOrUnique(new OSRLocalNode(i, unrestrictedStamp));
|
|
204 |
}
|
|
205 |
// Speculate on the OSRLocal stamps that could be more precise.
|
54084
|
206 |
SpeculationReason reason = OSR_LOCAL_SPECULATIONS.createSpeculationReason(osrState.bci, narrowedStamp, i);
|
49873
|
207 |
if (graph.getSpeculationLog().maySpeculate(reason) && osrLocal instanceof OSRLocalNode && value.getStackKind().equals(JavaKind.Object) && !narrowedStamp.isUnrestricted()) {
|
|
208 |
// Add guard.
|
|
209 |
LogicNode check = graph.addOrUniqueWithInputs(InstanceOfNode.createHelper((ObjectStamp) narrowedStamp, osrLocal, null, null));
|
50858
|
210 |
SpeculationLog.Speculation constant = graph.getSpeculationLog().speculate(reason);
|
49873
|
211 |
FixedGuardNode guard = graph.add(new FixedGuardNode(check, DeoptimizationReason.OptimizedTypeCheckViolated, DeoptimizationAction.InvalidateRecompile, constant, false));
|
|
212 |
graph.addAfterFixed(osrStart, guard);
|
46344
|
213 |
|
49873
|
214 |
// Replace with a more specific type at usages.
|
|
215 |
// We know that we are at the root,
|
|
216 |
// so we need to replace the proxy in the state.
|
|
217 |
proxy.replaceAtMatchingUsages(osrLocal, n -> n == osrState);
|
|
218 |
osrLocal = graph.addOrUnique(new PiNode(osrLocal, narrowedStamp, guard));
|
|
219 |
}
|
|
220 |
proxy.replaceAndDelete(osrLocal);
|
|
221 |
} else {
|
|
222 |
assert value == null || value instanceof OSRLocalNode;
|
|
223 |
}
|
46344
|
224 |
}
|
47798
|
225 |
|
49873
|
226 |
osr.replaceAtUsages(InputType.Guard, osrStart);
|
57537
|
227 |
osr.replaceAtUsages(InputType.Anchor, osrStart);
|
43972
|
228 |
}
|
46640
|
229 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement after replacing entry proxies");
|
43972
|
230 |
GraphUtil.killCFG(start);
|
46640
|
231 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
|
43972
|
232 |
new DeadCodeEliminationPhase(Required).apply(graph);
|
46344
|
233 |
|
|
234 |
if (currentOSRWithLocks) {
|
46640
|
235 |
OsrWithLocksCount.increment(debug);
|
49873
|
236 |
try (DebugCloseable context = osrStart.withNodeSourcePosition()) {
|
|
237 |
for (int i = osrState.monitorIdCount() - 1; i >= 0; --i) {
|
|
238 |
MonitorIdNode id = osrState.monitorIdAt(i);
|
|
239 |
ValueNode lockedObject = osrState.lockAt(i);
|
|
240 |
OSRMonitorEnterNode osrMonitorEnter = graph.add(new OSRMonitorEnterNode(lockedObject, id));
|
|
241 |
for (Node usage : id.usages()) {
|
|
242 |
if (usage instanceof AccessMonitorNode) {
|
|
243 |
AccessMonitorNode access = (AccessMonitorNode) usage;
|
|
244 |
access.setObject(lockedObject);
|
|
245 |
}
|
46344
|
246 |
}
|
49873
|
247 |
FixedNode oldNext = osrStart.next();
|
|
248 |
oldNext.replaceAtPredecessor(null);
|
|
249 |
osrMonitorEnter.setNext(oldNext);
|
|
250 |
osrStart.setNext(osrMonitorEnter);
|
46344
|
251 |
}
|
|
252 |
}
|
49873
|
253 |
|
46640
|
254 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After inserting OSR monitor enters");
|
46344
|
255 |
/*
|
|
256 |
* Ensure balanced monitorenter - monitorexit
|
|
257 |
*
|
|
258 |
* Ensure that there is no monitor exit without a monitor enter in the graph. If there
|
|
259 |
* is one this can only be done by bytecode as we have the monitor enter before the OSR
|
|
260 |
* loop but the exit in a path of the loop that must be under a condition, else it will
|
|
261 |
* throw an IllegalStateException anyway in the 2.iteration
|
|
262 |
*/
|
|
263 |
for (MonitorExitNode exit : graph.getNodes(MonitorExitNode.TYPE)) {
|
|
264 |
MonitorIdNode id = exit.getMonitorId();
|
|
265 |
if (id.usages().filter(MonitorEnterNode.class).count() != 1) {
|
|
266 |
throw new PermanentBailoutException("Unbalanced monitor enter-exit in OSR compilation with locks. Object is locked before the loop but released inside the loop.");
|
|
267 |
}
|
|
268 |
}
|
|
269 |
}
|
46640
|
270 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "OnStackReplacement result");
|
46344
|
271 |
new DeadCodeEliminationPhase(Required).apply(graph);
|
|
272 |
/*
|
|
273 |
* There must not be any parameter nodes left after OSR compilation.
|
|
274 |
*/
|
|
275 |
assert graph.getNodes(ParameterNode.TYPE).count() == 0 : "OSR Compilation contains references to parameters.";
|
43972
|
276 |
}
|
|
277 |
|
|
278 |
private static EntryMarkerNode getEntryMarker(StructuredGraph graph) {
|
|
279 |
NodeIterable<EntryMarkerNode> osrNodes = graph.getNodes(EntryMarkerNode.TYPE);
|
|
280 |
EntryMarkerNode osr = osrNodes.first();
|
|
281 |
if (osr == null) {
|
55509
|
282 |
throw new GraalError("No OnStackReplacementNode generated");
|
43972
|
283 |
}
|
|
284 |
if (osrNodes.count() > 1) {
|
|
285 |
throw new GraalError("Multiple OnStackReplacementNodes generated");
|
|
286 |
}
|
|
287 |
if (osr.stateAfter().stackSize() != 0) {
|
|
288 |
throw new PermanentBailoutException("OSR with stack entries not supported: %s", osr.stateAfter().toString(Verbosity.Debugger));
|
|
289 |
}
|
|
290 |
return osr;
|
|
291 |
}
|
|
292 |
|
46344
|
293 |
private static LoopBeginNode osrLoop(EntryMarkerNode osr) {
|
|
294 |
// Check that there is an OSR loop for the OSR begin
|
|
295 |
LoopsData loops = new LoopsData(osr.graph());
|
|
296 |
Loop<Block> l = loops.getCFG().getNodeToBlock().get(osr).getLoop();
|
|
297 |
if (l == null) {
|
|
298 |
return null;
|
|
299 |
}
|
|
300 |
return (LoopBeginNode) l.getHeader().getBeginNode();
|
|
301 |
}
|
|
302 |
|
|
303 |
private static boolean osrWithLocks(EntryMarkerNode osr) {
|
|
304 |
return osr.stateAfter().locksSize() != 0;
|
|
305 |
}
|
|
306 |
|
43972
|
307 |
@Override
|
|
308 |
public float codeSizeIncrease() {
|
|
309 |
return 5.0f;
|
|
310 |
}
|
|
311 |
}
|