43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2015, 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.nodes;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.debug.GraalError.shouldNotReachHere;
|
|
26 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
|
|
28 |
|
|
29 |
import java.util.ArrayDeque;
|
|
30 |
import java.util.ArrayList;
|
|
31 |
import java.util.Arrays;
|
|
32 |
import java.util.BitSet;
|
|
33 |
import java.util.Deque;
|
|
34 |
import java.util.Iterator;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Map;
|
|
37 |
import java.util.SortedMap;
|
|
38 |
import java.util.TreeMap;
|
|
39 |
|
48861
|
40 |
import org.graalvm.collections.EconomicMap;
|
|
41 |
import org.graalvm.collections.EconomicSet;
|
|
42 |
import org.graalvm.collections.Equivalence;
|
46371
|
43 |
import org.graalvm.compiler.core.common.Fields;
|
46344
|
44 |
import org.graalvm.compiler.core.common.PermanentBailoutException;
|
43972
|
45 |
import org.graalvm.compiler.core.common.util.TypeReader;
|
|
46 |
import org.graalvm.compiler.core.common.util.UnsafeArrayTypeReader;
|
46640
|
47 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
48 |
import org.graalvm.compiler.debug.GraalError;
|
|
49 |
import org.graalvm.compiler.graph.Edges;
|
|
50 |
import org.graalvm.compiler.graph.Graph;
|
|
51 |
import org.graalvm.compiler.graph.Node;
|
|
52 |
import org.graalvm.compiler.graph.NodeBitMap;
|
|
53 |
import org.graalvm.compiler.graph.NodeClass;
|
|
54 |
import org.graalvm.compiler.graph.NodeInputList;
|
|
55 |
import org.graalvm.compiler.graph.NodeList;
|
|
56 |
import org.graalvm.compiler.graph.NodeSourcePosition;
|
|
57 |
import org.graalvm.compiler.graph.NodeSuccessorList;
|
|
58 |
import org.graalvm.compiler.graph.spi.Canonicalizable;
|
|
59 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
60 |
import org.graalvm.compiler.nodeinfo.InputType;
|
|
61 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
62 |
import org.graalvm.compiler.nodes.GraphDecoder.MethodScope;
|
|
63 |
import org.graalvm.compiler.nodes.GraphDecoder.ProxyPlaceholder;
|
|
64 |
import org.graalvm.compiler.nodes.calc.FloatingNode;
|
|
65 |
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode;
|
|
66 |
import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin.LoopExplosionKind;
|
46640
|
67 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
68 |
|
|
69 |
import jdk.vm.ci.code.Architecture;
|
|
70 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
71 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
72 |
import jdk.vm.ci.meta.JavaConstant;
|
|
73 |
import jdk.vm.ci.meta.JavaKind;
|
|
74 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
75 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Decoder for {@link EncodedGraph encoded graphs} produced by {@link GraphEncoder}. Support for
|
|
79 |
* loop explosion during decoding is built into this class, because it requires many interactions
|
|
80 |
* with the decoding process. Subclasses can provide canonicalization and simplification of nodes
|
|
81 |
* during decoding, as well as method inlining during decoding.
|
|
82 |
*/
|
|
83 |
public class GraphDecoder {
|
|
84 |
|
|
85 |
/** Decoding state maintained for each encoded graph. */
|
|
86 |
protected class MethodScope {
|
|
87 |
/** The loop that contains the call. Only non-null during method inlining. */
|
|
88 |
public final LoopScope callerLoopScope;
|
|
89 |
/**
|
|
90 |
* Mark for nodes that were present before the decoding of this method started. Note that
|
|
91 |
* nodes that were decoded after the mark can still be part of an outer method, since
|
|
92 |
* floating nodes of outer methods are decoded lazily.
|
|
93 |
*/
|
|
94 |
public final Graph.Mark methodStartMark;
|
|
95 |
/** The encode graph that is decoded. */
|
|
96 |
public final EncodedGraph encodedGraph;
|
46371
|
97 |
/** The highest node order id that a fixed node has in the EncodedGraph. */
|
|
98 |
public final int maxFixedNodeOrderId;
|
43972
|
99 |
/** Access to the encoded graph. */
|
|
100 |
public final TypeReader reader;
|
|
101 |
/** The kind of loop explosion to be performed during decoding. */
|
|
102 |
public final LoopExplosionKind loopExplosion;
|
|
103 |
|
|
104 |
/** All return nodes encountered during decoding. */
|
46371
|
105 |
public final List<ControlSinkNode> returnAndUnwindNodes;
|
43972
|
106 |
|
|
107 |
/** All merges created during loop explosion. */
|
46344
|
108 |
public final EconomicSet<Node> loopExplosionMerges;
|
46371
|
109 |
|
43972
|
110 |
/**
|
|
111 |
* The start of explosion, and the merge point for when irreducible loops are detected. Only
|
|
112 |
* used when {@link MethodScope#loopExplosion} is {@link LoopExplosionKind#MERGE_EXPLODE}.
|
|
113 |
*/
|
|
114 |
public MergeNode loopExplosionHead;
|
|
115 |
|
|
116 |
protected MethodScope(LoopScope callerLoopScope, StructuredGraph graph, EncodedGraph encodedGraph, LoopExplosionKind loopExplosion) {
|
|
117 |
this.callerLoopScope = callerLoopScope;
|
|
118 |
this.methodStartMark = graph.getMark();
|
|
119 |
this.encodedGraph = encodedGraph;
|
|
120 |
this.loopExplosion = loopExplosion;
|
46371
|
121 |
this.returnAndUnwindNodes = new ArrayList<>(2);
|
43972
|
122 |
|
|
123 |
if (encodedGraph != null) {
|
|
124 |
reader = UnsafeArrayTypeReader.create(encodedGraph.getEncoding(), encodedGraph.getStartOffset(), architecture.supportsUnalignedMemoryAccess());
|
46371
|
125 |
maxFixedNodeOrderId = reader.getUVInt();
|
43972
|
126 |
if (encodedGraph.nodeStartOffsets == null) {
|
|
127 |
int nodeCount = reader.getUVInt();
|
46344
|
128 |
int[] nodeStartOffsets = new int[nodeCount];
|
43972
|
129 |
for (int i = 0; i < nodeCount; i++) {
|
46344
|
130 |
nodeStartOffsets[i] = encodedGraph.getStartOffset() - reader.getUVInt();
|
43972
|
131 |
}
|
|
132 |
encodedGraph.nodeStartOffsets = nodeStartOffsets;
|
|
133 |
}
|
|
134 |
} else {
|
|
135 |
reader = null;
|
46371
|
136 |
maxFixedNodeOrderId = 0;
|
43972
|
137 |
}
|
|
138 |
|
|
139 |
if (loopExplosion != LoopExplosionKind.NONE) {
|
46344
|
140 |
loopExplosionMerges = EconomicSet.create(Equivalence.IDENTITY);
|
43972
|
141 |
} else {
|
|
142 |
loopExplosionMerges = null;
|
|
143 |
}
|
|
144 |
}
|
46371
|
145 |
|
|
146 |
public boolean isInlinedMethod() {
|
|
147 |
return false;
|
|
148 |
}
|
43972
|
149 |
}
|
|
150 |
|
|
151 |
/** Decoding state maintained for each loop in the encoded graph. */
|
|
152 |
protected static class LoopScope {
|
|
153 |
public final MethodScope methodScope;
|
|
154 |
public final LoopScope outer;
|
|
155 |
public final int loopDepth;
|
|
156 |
public final int loopIteration;
|
|
157 |
/**
|
|
158 |
* Upcoming loop iterations during loop explosions that have not been processed yet. Only
|
|
159 |
* used when {@link MethodScope#loopExplosion} is not {@link LoopExplosionKind#NONE}.
|
|
160 |
*/
|
|
161 |
public Deque<LoopScope> nextIterations;
|
|
162 |
/**
|
|
163 |
* Information about already processed loop iterations for state merging during loop
|
|
164 |
* explosion. Only used when {@link MethodScope#loopExplosion} is
|
|
165 |
* {@link LoopExplosionKind#MERGE_EXPLODE}.
|
|
166 |
*/
|
46344
|
167 |
public final EconomicMap<LoopExplosionState, LoopExplosionState> iterationStates;
|
43972
|
168 |
public final int loopBeginOrderId;
|
|
169 |
/**
|
|
170 |
* The worklist of fixed nodes to process. Since we already the correct processing order
|
|
171 |
* from the orderId, we just set the orderId bit in the bitset when a node is ready for
|
|
172 |
* processing. The lowest set bit is the next node to process.
|
|
173 |
*/
|
|
174 |
public final BitSet nodesToProcess;
|
|
175 |
/** Nodes that have been created, indexed by the orderId. */
|
|
176 |
public final Node[] createdNodes;
|
|
177 |
/**
|
|
178 |
* Nodes that have been created in outer loop scopes and existed before starting to process
|
46371
|
179 |
* this loop, indexed by the orderId. Only used when {@link MethodScope#loopExplosion} is
|
|
180 |
* not {@link LoopExplosionKind#NONE}.
|
43972
|
181 |
*/
|
|
182 |
public final Node[] initialCreatedNodes;
|
|
183 |
|
|
184 |
protected LoopScope(MethodScope methodScope) {
|
|
185 |
this.methodScope = methodScope;
|
|
186 |
this.outer = null;
|
46371
|
187 |
this.nextIterations = methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN ? new ArrayDeque<>(2) : null;
|
43972
|
188 |
this.loopDepth = 0;
|
|
189 |
this.loopIteration = 0;
|
|
190 |
this.iterationStates = null;
|
|
191 |
this.loopBeginOrderId = -1;
|
|
192 |
|
|
193 |
int nodeCount = methodScope.encodedGraph.nodeStartOffsets.length;
|
46371
|
194 |
this.nodesToProcess = new BitSet(methodScope.maxFixedNodeOrderId);
|
43972
|
195 |
this.createdNodes = new Node[nodeCount];
|
46371
|
196 |
this.initialCreatedNodes = null;
|
43972
|
197 |
}
|
|
198 |
|
|
199 |
protected LoopScope(MethodScope methodScope, LoopScope outer, int loopDepth, int loopIteration, int loopBeginOrderId, Node[] initialCreatedNodes, Node[] createdNodes,
|
46344
|
200 |
Deque<LoopScope> nextIterations, EconomicMap<LoopExplosionState, LoopExplosionState> iterationStates) {
|
43972
|
201 |
this.methodScope = methodScope;
|
|
202 |
this.outer = outer;
|
|
203 |
this.loopDepth = loopDepth;
|
|
204 |
this.loopIteration = loopIteration;
|
|
205 |
this.nextIterations = nextIterations;
|
|
206 |
this.iterationStates = iterationStates;
|
|
207 |
this.loopBeginOrderId = loopBeginOrderId;
|
46371
|
208 |
this.nodesToProcess = new BitSet(methodScope.maxFixedNodeOrderId);
|
43972
|
209 |
this.initialCreatedNodes = initialCreatedNodes;
|
46371
|
210 |
this.createdNodes = createdNodes;
|
43972
|
211 |
}
|
|
212 |
|
|
213 |
@Override
|
|
214 |
public String toString() {
|
|
215 |
return loopDepth + "," + loopIteration + (loopBeginOrderId == -1 ? "" : "#" + loopBeginOrderId);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
protected static class LoopExplosionState {
|
|
220 |
public final FrameState state;
|
|
221 |
public final MergeNode merge;
|
|
222 |
public final int hashCode;
|
|
223 |
|
|
224 |
protected LoopExplosionState(FrameState state, MergeNode merge) {
|
|
225 |
this.state = state;
|
|
226 |
this.merge = merge;
|
|
227 |
|
|
228 |
int h = 0;
|
|
229 |
for (ValueNode value : state.values()) {
|
|
230 |
if (value == null) {
|
|
231 |
h = h * 31 + 1234;
|
|
232 |
} else {
|
|
233 |
h = h * 31 + ProxyPlaceholder.unwrap(value).hashCode();
|
|
234 |
}
|
|
235 |
}
|
|
236 |
this.hashCode = h;
|
|
237 |
}
|
|
238 |
|
|
239 |
@Override
|
|
240 |
public boolean equals(Object obj) {
|
|
241 |
if (!(obj instanceof LoopExplosionState)) {
|
|
242 |
return false;
|
|
243 |
}
|
|
244 |
|
|
245 |
FrameState otherState = ((LoopExplosionState) obj).state;
|
|
246 |
FrameState thisState = state;
|
|
247 |
assert thisState.outerFrameState() == otherState.outerFrameState();
|
|
248 |
|
|
249 |
Iterator<ValueNode> thisIter = thisState.values().iterator();
|
|
250 |
Iterator<ValueNode> otherIter = otherState.values().iterator();
|
|
251 |
while (thisIter.hasNext() && otherIter.hasNext()) {
|
|
252 |
ValueNode thisValue = ProxyPlaceholder.unwrap(thisIter.next());
|
|
253 |
ValueNode otherValue = ProxyPlaceholder.unwrap(otherIter.next());
|
|
254 |
if (thisValue != otherValue) {
|
|
255 |
return false;
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return thisIter.hasNext() == otherIter.hasNext();
|
|
259 |
}
|
|
260 |
|
|
261 |
@Override
|
|
262 |
public int hashCode() {
|
|
263 |
return hashCode;
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
/**
|
|
268 |
* Additional information encoded for {@link Invoke} nodes to allow method inlining without
|
|
269 |
* decoding the frame state and successors beforehand.
|
|
270 |
*/
|
|
271 |
protected static class InvokeData {
|
|
272 |
public final Invoke invoke;
|
|
273 |
public final ResolvedJavaType contextType;
|
|
274 |
public final int invokeOrderId;
|
|
275 |
public final int callTargetOrderId;
|
|
276 |
public final int stateAfterOrderId;
|
|
277 |
public final int nextOrderId;
|
|
278 |
|
|
279 |
public final int nextNextOrderId;
|
|
280 |
public final int exceptionOrderId;
|
|
281 |
public final int exceptionStateOrderId;
|
|
282 |
public final int exceptionNextOrderId;
|
|
283 |
public JavaConstant constantReceiver;
|
|
284 |
|
|
285 |
protected InvokeData(Invoke invoke, ResolvedJavaType contextType, int invokeOrderId, int callTargetOrderId, int stateAfterOrderId, int nextOrderId, int nextNextOrderId, int exceptionOrderId,
|
|
286 |
int exceptionStateOrderId, int exceptionNextOrderId) {
|
|
287 |
this.invoke = invoke;
|
|
288 |
this.contextType = contextType;
|
|
289 |
this.invokeOrderId = invokeOrderId;
|
|
290 |
this.callTargetOrderId = callTargetOrderId;
|
|
291 |
this.stateAfterOrderId = stateAfterOrderId;
|
|
292 |
this.nextOrderId = nextOrderId;
|
|
293 |
this.nextNextOrderId = nextNextOrderId;
|
|
294 |
this.exceptionOrderId = exceptionOrderId;
|
|
295 |
this.exceptionStateOrderId = exceptionStateOrderId;
|
|
296 |
this.exceptionNextOrderId = exceptionNextOrderId;
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* A node that is created during {@link LoopExplosionKind#MERGE_EXPLODE loop explosion} that can
|
|
302 |
* later be replaced by a ProxyNode if {@link LoopDetector loop detection} finds out that the
|
|
303 |
* value is defined in the loop, but used outside the loop.
|
|
304 |
*/
|
|
305 |
@NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
|
|
306 |
protected static final class ProxyPlaceholder extends FloatingNode implements Canonicalizable {
|
|
307 |
public static final NodeClass<ProxyPlaceholder> TYPE = NodeClass.create(ProxyPlaceholder.class);
|
|
308 |
|
|
309 |
@Input ValueNode value;
|
|
310 |
@Input(InputType.Unchecked) Node proxyPoint;
|
|
311 |
|
|
312 |
public ProxyPlaceholder(ValueNode value, MergeNode proxyPoint) {
|
48190
|
313 |
super(TYPE, value.stamp(NodeView.DEFAULT));
|
43972
|
314 |
this.value = value;
|
|
315 |
this.proxyPoint = proxyPoint;
|
|
316 |
}
|
|
317 |
|
|
318 |
void setValue(ValueNode value) {
|
|
319 |
updateUsages(this.value, value);
|
|
320 |
this.value = value;
|
|
321 |
}
|
|
322 |
|
|
323 |
@Override
|
|
324 |
public Node canonical(CanonicalizerTool tool) {
|
|
325 |
if (tool.allUsagesAvailable()) {
|
|
326 |
/* The node is always unnecessary after graph decoding. */
|
|
327 |
return value;
|
|
328 |
} else {
|
|
329 |
return this;
|
|
330 |
}
|
|
331 |
}
|
|
332 |
|
|
333 |
public static ValueNode unwrap(ValueNode value) {
|
|
334 |
ValueNode result = value;
|
|
335 |
while (result instanceof ProxyPlaceholder) {
|
|
336 |
result = ((ProxyPlaceholder) result).value;
|
|
337 |
}
|
|
338 |
return result;
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
protected final Architecture architecture;
|
46371
|
343 |
/** The target graph where decoded nodes are added to. */
|
|
344 |
protected final StructuredGraph graph;
|
46640
|
345 |
protected final OptionValues options;
|
|
346 |
protected final DebugContext debug;
|
|
347 |
|
46371
|
348 |
private final EconomicMap<NodeClass<?>, ArrayDeque<Node>> reusableFloatingNodes;
|
43972
|
349 |
|
46371
|
350 |
public GraphDecoder(Architecture architecture, StructuredGraph graph) {
|
43972
|
351 |
this.architecture = architecture;
|
46371
|
352 |
this.graph = graph;
|
46640
|
353 |
this.options = graph.getOptions();
|
|
354 |
this.debug = graph.getDebug();
|
46371
|
355 |
reusableFloatingNodes = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
356 |
}
|
|
357 |
|
|
358 |
@SuppressWarnings("try")
|
46371
|
359 |
public final void decode(EncodedGraph encodedGraph) {
|
46640
|
360 |
try (DebugContext.Scope scope = debug.scope("GraphDecoder", graph)) {
|
43972
|
361 |
MethodScope methodScope = new MethodScope(null, graph, encodedGraph, LoopExplosionKind.NONE);
|
|
362 |
decode(createInitialLoopScope(methodScope, null));
|
|
363 |
cleanupGraph(methodScope);
|
46371
|
364 |
assert graph.verify();
|
43972
|
365 |
} catch (Throwable ex) {
|
46640
|
366 |
debug.handle(ex);
|
43972
|
367 |
}
|
|
368 |
}
|
|
369 |
|
|
370 |
protected final LoopScope createInitialLoopScope(MethodScope methodScope, FixedWithNextNode startNode) {
|
|
371 |
LoopScope loopScope = new LoopScope(methodScope);
|
|
372 |
FixedNode firstNode;
|
|
373 |
if (startNode != null) {
|
|
374 |
/*
|
|
375 |
* The start node of a graph can be referenced as the guard for a GuardedNode. We
|
|
376 |
* register the previous block node, so that such guards are correctly anchored when
|
|
377 |
* doing inlining during graph decoding.
|
|
378 |
*/
|
|
379 |
registerNode(loopScope, GraphEncoder.START_NODE_ORDER_ID, AbstractBeginNode.prevBegin(startNode), false, false);
|
|
380 |
|
|
381 |
firstNode = makeStubNode(methodScope, loopScope, GraphEncoder.FIRST_NODE_ORDER_ID);
|
|
382 |
startNode.setNext(firstNode);
|
|
383 |
loopScope.nodesToProcess.set(GraphEncoder.FIRST_NODE_ORDER_ID);
|
|
384 |
} else {
|
46371
|
385 |
firstNode = graph.start();
|
43972
|
386 |
registerNode(loopScope, GraphEncoder.START_NODE_ORDER_ID, firstNode, false, false);
|
|
387 |
loopScope.nodesToProcess.set(GraphEncoder.START_NODE_ORDER_ID);
|
|
388 |
}
|
|
389 |
return loopScope;
|
|
390 |
}
|
|
391 |
|
|
392 |
protected final void decode(LoopScope initialLoopScope) {
|
|
393 |
LoopScope loopScope = initialLoopScope;
|
46344
|
394 |
/* Process (inlined) methods. */
|
43972
|
395 |
while (loopScope != null) {
|
|
396 |
MethodScope methodScope = loopScope.methodScope;
|
|
397 |
|
|
398 |
/* Process loops of method. */
|
|
399 |
while (loopScope != null) {
|
|
400 |
|
|
401 |
/* Process nodes of loop. */
|
|
402 |
while (!loopScope.nodesToProcess.isEmpty()) {
|
|
403 |
loopScope = processNextNode(methodScope, loopScope);
|
|
404 |
methodScope = loopScope.methodScope;
|
|
405 |
/*
|
|
406 |
* We can have entered a new loop, and we can have entered a new inlined method.
|
|
407 |
*/
|
|
408 |
}
|
|
409 |
|
|
410 |
/* Finished with a loop. */
|
|
411 |
if (loopScope.nextIterations != null && !loopScope.nextIterations.isEmpty()) {
|
|
412 |
/* Loop explosion: process the loop iteration. */
|
|
413 |
assert loopScope.nextIterations.peekFirst().loopIteration == loopScope.loopIteration + 1;
|
|
414 |
loopScope = loopScope.nextIterations.removeFirst();
|
|
415 |
} else {
|
|
416 |
propagateCreatedNodes(loopScope);
|
|
417 |
loopScope = loopScope.outer;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
/*
|
46371
|
422 |
* Finished with an inlined method. Perform end-of-method cleanup tasks.
|
43972
|
423 |
*/
|
46371
|
424 |
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
|
|
425 |
LoopDetector loopDetector = new LoopDetector(graph, methodScope);
|
|
426 |
loopDetector.run();
|
43972
|
427 |
}
|
46371
|
428 |
if (methodScope.isInlinedMethod()) {
|
|
429 |
finishInlining(methodScope);
|
|
430 |
}
|
|
431 |
|
|
432 |
/* continue with the caller */
|
43972
|
433 |
loopScope = methodScope.callerLoopScope;
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
46371
|
437 |
protected void finishInlining(@SuppressWarnings("unused") MethodScope inlineScope) {
|
|
438 |
}
|
|
439 |
|
43972
|
440 |
private static void propagateCreatedNodes(LoopScope loopScope) {
|
46371
|
441 |
if (loopScope.outer == null || loopScope.createdNodes != loopScope.outer.createdNodes) {
|
43972
|
442 |
return;
|
|
443 |
}
|
|
444 |
|
|
445 |
/* Register nodes that were created while decoding the loop to the outside scope. */
|
|
446 |
for (int i = 0; i < loopScope.createdNodes.length; i++) {
|
|
447 |
if (loopScope.outer.createdNodes[i] == null) {
|
|
448 |
loopScope.outer.createdNodes[i] = loopScope.createdNodes[i];
|
|
449 |
}
|
|
450 |
}
|
|
451 |
}
|
|
452 |
|
|
453 |
protected LoopScope processNextNode(MethodScope methodScope, LoopScope loopScope) {
|
|
454 |
int nodeOrderId = loopScope.nodesToProcess.nextSetBit(0);
|
|
455 |
loopScope.nodesToProcess.clear(nodeOrderId);
|
|
456 |
|
|
457 |
FixedNode node = (FixedNode) lookupNode(loopScope, nodeOrderId);
|
|
458 |
if (node.isDeleted()) {
|
|
459 |
return loopScope;
|
|
460 |
}
|
|
461 |
|
|
462 |
if ((node instanceof MergeNode ||
|
|
463 |
(node instanceof LoopBeginNode && (methodScope.loopExplosion == LoopExplosionKind.FULL_UNROLL || methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE ||
|
|
464 |
methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN))) &&
|
|
465 |
((AbstractMergeNode) node).forwardEndCount() == 1) {
|
|
466 |
AbstractMergeNode merge = (AbstractMergeNode) node;
|
|
467 |
EndNode singleEnd = merge.forwardEndAt(0);
|
|
468 |
|
|
469 |
/* Nodes that would use this merge as the guard need to use the previous block. */
|
|
470 |
registerNode(loopScope, nodeOrderId, AbstractBeginNode.prevBegin(singleEnd), true, false);
|
|
471 |
|
|
472 |
FixedNode next = makeStubNode(methodScope, loopScope, nodeOrderId + GraphEncoder.BEGIN_NEXT_ORDER_ID_OFFSET);
|
|
473 |
singleEnd.replaceAtPredecessor(next);
|
|
474 |
|
|
475 |
merge.safeDelete();
|
|
476 |
singleEnd.safeDelete();
|
|
477 |
return loopScope;
|
|
478 |
}
|
|
479 |
|
|
480 |
LoopScope successorAddScope = loopScope;
|
|
481 |
boolean updatePredecessors = true;
|
|
482 |
if (node instanceof LoopExitNode) {
|
|
483 |
if (methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN || (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE && loopScope.loopDepth > 1)) {
|
|
484 |
/*
|
|
485 |
* We do not want to merge loop exits of inner loops. Instead, we want to keep
|
|
486 |
* exploding the outer loop separately for every loop exit and then merge the outer
|
|
487 |
* loop. Therefore, we create a new LoopScope of the outer loop for every loop exit
|
|
488 |
* of the inner loop.
|
|
489 |
*/
|
|
490 |
LoopScope outerScope = loopScope.outer;
|
|
491 |
int nextIterationNumber = outerScope.nextIterations.isEmpty() ? outerScope.loopIteration + 1 : outerScope.nextIterations.getLast().loopIteration + 1;
|
48861
|
492 |
successorAddScope = new LoopScope(methodScope, outerScope.outer, outerScope.loopDepth, nextIterationNumber, outerScope.loopBeginOrderId,
|
|
493 |
outerScope.initialCreatedNodes == null ? null : Arrays.copyOf(outerScope.initialCreatedNodes, outerScope.initialCreatedNodes.length),
|
46371
|
494 |
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length), outerScope.nextIterations, outerScope.iterationStates);
|
43972
|
495 |
checkLoopExplosionIteration(methodScope, successorAddScope);
|
|
496 |
|
|
497 |
/*
|
|
498 |
* Nodes that are still unprocessed in the outer scope might be merge nodes that are
|
|
499 |
* also reachable from the new exploded scope. Clearing them ensures that we do not
|
|
500 |
* merge, but instead keep exploding.
|
|
501 |
*/
|
|
502 |
for (int id = outerScope.nodesToProcess.nextSetBit(0); id >= 0; id = outerScope.nodesToProcess.nextSetBit(id + 1)) {
|
|
503 |
successorAddScope.createdNodes[id] = null;
|
|
504 |
}
|
|
505 |
|
|
506 |
outerScope.nextIterations.addLast(successorAddScope);
|
|
507 |
} else {
|
|
508 |
successorAddScope = loopScope.outer;
|
|
509 |
}
|
|
510 |
updatePredecessors = methodScope.loopExplosion == LoopExplosionKind.NONE;
|
|
511 |
}
|
|
512 |
|
|
513 |
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
|
|
514 |
int typeId = methodScope.reader.getUVInt();
|
|
515 |
assert node.getNodeClass() == methodScope.encodedGraph.getNodeClasses()[typeId];
|
46371
|
516 |
makeFixedNodeInputs(methodScope, loopScope, node);
|
43972
|
517 |
readProperties(methodScope, node);
|
|
518 |
makeSuccessorStubs(methodScope, successorAddScope, node, updatePredecessors);
|
|
519 |
|
|
520 |
LoopScope resultScope = loopScope;
|
|
521 |
if (node instanceof LoopBeginNode) {
|
|
522 |
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
|
|
523 |
handleLoopExplosionBegin(methodScope, loopScope, (LoopBeginNode) node);
|
|
524 |
}
|
|
525 |
|
|
526 |
} else if (node instanceof LoopExitNode) {
|
|
527 |
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
|
|
528 |
handleLoopExplosionProxyNodes(methodScope, loopScope, successorAddScope, (LoopExitNode) node, nodeOrderId);
|
|
529 |
} else {
|
|
530 |
handleProxyNodes(methodScope, loopScope, (LoopExitNode) node);
|
|
531 |
}
|
|
532 |
|
|
533 |
} else if (node instanceof MergeNode) {
|
|
534 |
handleMergeNode(((MergeNode) node));
|
|
535 |
|
|
536 |
} else if (node instanceof AbstractEndNode) {
|
|
537 |
LoopScope phiInputScope = loopScope;
|
|
538 |
LoopScope phiNodeScope = loopScope;
|
|
539 |
|
|
540 |
if (methodScope.loopExplosion != LoopExplosionKind.NONE && node instanceof LoopEndNode) {
|
|
541 |
node = handleLoopExplosionEnd(methodScope, loopScope, (LoopEndNode) node);
|
|
542 |
phiNodeScope = loopScope.nextIterations.getLast();
|
|
543 |
}
|
|
544 |
|
|
545 |
int mergeOrderId = readOrderId(methodScope);
|
|
546 |
AbstractMergeNode merge = (AbstractMergeNode) lookupNode(phiNodeScope, mergeOrderId);
|
|
547 |
if (merge == null) {
|
|
548 |
merge = (AbstractMergeNode) makeStubNode(methodScope, phiNodeScope, mergeOrderId);
|
|
549 |
|
|
550 |
if (merge instanceof LoopBeginNode) {
|
|
551 |
assert phiNodeScope == phiInputScope && phiNodeScope == loopScope;
|
|
552 |
resultScope = new LoopScope(methodScope, loopScope, loopScope.loopDepth + 1, 0, mergeOrderId,
|
46371
|
553 |
methodScope.loopExplosion != LoopExplosionKind.NONE ? Arrays.copyOf(loopScope.createdNodes, loopScope.createdNodes.length) : null,
|
|
554 |
methodScope.loopExplosion != LoopExplosionKind.NONE ? Arrays.copyOf(loopScope.createdNodes, loopScope.createdNodes.length) : loopScope.createdNodes, //
|
|
555 |
methodScope.loopExplosion != LoopExplosionKind.NONE ? new ArrayDeque<>(2) : null, //
|
46344
|
556 |
methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE ? EconomicMap.create(Equivalence.DEFAULT) : null);
|
43972
|
557 |
phiInputScope = resultScope;
|
|
558 |
phiNodeScope = resultScope;
|
|
559 |
|
46371
|
560 |
if (methodScope.loopExplosion != LoopExplosionKind.NONE) {
|
|
561 |
registerNode(loopScope, mergeOrderId, null, true, true);
|
|
562 |
}
|
43972
|
563 |
loopScope.nodesToProcess.clear(mergeOrderId);
|
|
564 |
resultScope.nodesToProcess.set(mergeOrderId);
|
|
565 |
}
|
|
566 |
}
|
|
567 |
|
|
568 |
handlePhiFunctions(methodScope, phiInputScope, phiNodeScope, (AbstractEndNode) node, merge);
|
|
569 |
|
|
570 |
} else if (node instanceof Invoke) {
|
|
571 |
InvokeData invokeData = readInvokeData(methodScope, nodeOrderId, (Invoke) node);
|
|
572 |
resultScope = handleInvoke(methodScope, loopScope, invokeData);
|
|
573 |
|
46371
|
574 |
} else if (node instanceof ReturnNode || node instanceof UnwindNode) {
|
|
575 |
methodScope.returnAndUnwindNodes.add((ControlSinkNode) node);
|
43972
|
576 |
} else {
|
|
577 |
handleFixedNode(methodScope, loopScope, nodeOrderId, node);
|
|
578 |
}
|
|
579 |
|
|
580 |
return resultScope;
|
|
581 |
}
|
|
582 |
|
46393
|
583 |
protected InvokeData readInvokeData(MethodScope methodScope, int invokeOrderId, Invoke invoke) {
|
43972
|
584 |
ResolvedJavaType contextType = (ResolvedJavaType) readObject(methodScope);
|
|
585 |
int callTargetOrderId = readOrderId(methodScope);
|
|
586 |
int stateAfterOrderId = readOrderId(methodScope);
|
|
587 |
int nextOrderId = readOrderId(methodScope);
|
|
588 |
|
|
589 |
if (invoke instanceof InvokeWithExceptionNode) {
|
|
590 |
int nextNextOrderId = readOrderId(methodScope);
|
|
591 |
int exceptionOrderId = readOrderId(methodScope);
|
|
592 |
int exceptionStateOrderId = readOrderId(methodScope);
|
|
593 |
int exceptionNextOrderId = readOrderId(methodScope);
|
|
594 |
return new InvokeData(invoke, contextType, invokeOrderId, callTargetOrderId, stateAfterOrderId, nextOrderId, nextNextOrderId, exceptionOrderId, exceptionStateOrderId,
|
|
595 |
exceptionNextOrderId);
|
|
596 |
} else {
|
|
597 |
return new InvokeData(invoke, contextType, invokeOrderId, callTargetOrderId, stateAfterOrderId, nextOrderId, -1, -1, -1, -1);
|
|
598 |
}
|
|
599 |
}
|
|
600 |
|
|
601 |
/**
|
|
602 |
* {@link Invoke} nodes do not have the {@link CallTargetNode}, {@link FrameState}, and
|
|
603 |
* successors encoded. Instead, this information is provided separately to allow method inlining
|
|
604 |
* without decoding and adding them to the graph upfront. For non-inlined methods, this method
|
|
605 |
* restores the normal state. Subclasses can override it to perform method inlining.
|
|
606 |
*
|
|
607 |
* The return value is the loop scope where decoding should continue. When method inlining
|
|
608 |
* should be performed, the returned loop scope must be a new loop scope for the inlined method.
|
|
609 |
* Without inlining, the original loop scope must be returned.
|
|
610 |
*/
|
|
611 |
protected LoopScope handleInvoke(MethodScope methodScope, LoopScope loopScope, InvokeData invokeData) {
|
|
612 |
assert invokeData.invoke.callTarget() == null : "callTarget edge is ignored during decoding of Invoke";
|
|
613 |
CallTargetNode callTarget = (CallTargetNode) ensureNodeCreated(methodScope, loopScope, invokeData.callTargetOrderId);
|
46344
|
614 |
appendInvoke(methodScope, loopScope, invokeData, callTarget);
|
|
615 |
return loopScope;
|
|
616 |
}
|
|
617 |
|
|
618 |
protected void appendInvoke(MethodScope methodScope, LoopScope loopScope, InvokeData invokeData, CallTargetNode callTarget) {
|
43972
|
619 |
if (invokeData.invoke instanceof InvokeWithExceptionNode) {
|
|
620 |
((InvokeWithExceptionNode) invokeData.invoke).setCallTarget(callTarget);
|
|
621 |
} else {
|
|
622 |
((InvokeNode) invokeData.invoke).setCallTarget(callTarget);
|
|
623 |
}
|
|
624 |
|
|
625 |
assert invokeData.invoke.stateAfter() == null && invokeData.invoke.stateDuring() == null : "FrameState edges are ignored during decoding of Invoke";
|
|
626 |
invokeData.invoke.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, invokeData.stateAfterOrderId));
|
|
627 |
|
|
628 |
invokeData.invoke.setNext(makeStubNode(methodScope, loopScope, invokeData.nextOrderId));
|
|
629 |
if (invokeData.invoke instanceof InvokeWithExceptionNode) {
|
|
630 |
((InvokeWithExceptionNode) invokeData.invoke).setExceptionEdge((AbstractBeginNode) makeStubNode(methodScope, loopScope, invokeData.exceptionOrderId));
|
|
631 |
}
|
|
632 |
}
|
|
633 |
|
|
634 |
/**
|
|
635 |
* Hook for subclasses to perform simplifications for a non-loop-header control flow merge.
|
|
636 |
*
|
|
637 |
* @param merge The control flow merge.
|
|
638 |
*/
|
|
639 |
protected void handleMergeNode(MergeNode merge) {
|
|
640 |
}
|
|
641 |
|
|
642 |
protected void handleLoopExplosionBegin(MethodScope methodScope, LoopScope loopScope, LoopBeginNode loopBegin) {
|
|
643 |
checkLoopExplosionIteration(methodScope, loopScope);
|
|
644 |
|
|
645 |
List<EndNode> predecessors = loopBegin.forwardEnds().snapshot();
|
|
646 |
FixedNode successor = loopBegin.next();
|
|
647 |
FrameState frameState = loopBegin.stateAfter();
|
|
648 |
|
|
649 |
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
|
|
650 |
LoopExplosionState queryState = new LoopExplosionState(frameState, null);
|
|
651 |
LoopExplosionState existingState = loopScope.iterationStates.get(queryState);
|
|
652 |
if (existingState != null) {
|
|
653 |
loopBegin.replaceAtUsagesAndDelete(existingState.merge);
|
|
654 |
successor.safeDelete();
|
|
655 |
for (EndNode predecessor : predecessors) {
|
|
656 |
existingState.merge.addForwardEnd(predecessor);
|
|
657 |
}
|
|
658 |
return;
|
|
659 |
}
|
|
660 |
}
|
|
661 |
|
46371
|
662 |
MergeNode merge = graph.add(new MergeNode());
|
46344
|
663 |
methodScope.loopExplosionMerges.add(merge);
|
43972
|
664 |
|
|
665 |
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
|
|
666 |
if (loopScope.iterationStates.size() == 0 && loopScope.loopDepth == 1) {
|
|
667 |
if (methodScope.loopExplosionHead != null) {
|
|
668 |
throw new PermanentBailoutException("Graal implementation restriction: Method with %s loop explosion must not have more than one top-level loop", LoopExplosionKind.MERGE_EXPLODE);
|
|
669 |
}
|
|
670 |
methodScope.loopExplosionHead = merge;
|
|
671 |
}
|
|
672 |
|
|
673 |
List<ValueNode> newFrameStateValues = new ArrayList<>();
|
|
674 |
for (ValueNode frameStateValue : frameState.values) {
|
46371
|
675 |
if (frameStateValue == null || frameStateValue.isConstant() || !graph.isNew(methodScope.methodStartMark, frameStateValue)) {
|
43972
|
676 |
newFrameStateValues.add(frameStateValue);
|
|
677 |
|
|
678 |
} else {
|
46371
|
679 |
ProxyPlaceholder newFrameStateValue = graph.unique(new ProxyPlaceholder(frameStateValue, merge));
|
43972
|
680 |
newFrameStateValues.add(newFrameStateValue);
|
|
681 |
|
|
682 |
/*
|
|
683 |
* We do not have the orderID of the value anymore, so we need to search through
|
|
684 |
* the complete list of nodes to find a match.
|
|
685 |
*/
|
|
686 |
for (int i = 0; i < loopScope.createdNodes.length; i++) {
|
|
687 |
if (loopScope.createdNodes[i] == frameStateValue) {
|
|
688 |
loopScope.createdNodes[i] = newFrameStateValue;
|
|
689 |
}
|
46371
|
690 |
}
|
|
691 |
|
|
692 |
if (loopScope.initialCreatedNodes != null) {
|
|
693 |
for (int i = 0; i < loopScope.initialCreatedNodes.length; i++) {
|
|
694 |
if (loopScope.initialCreatedNodes[i] == frameStateValue) {
|
|
695 |
loopScope.initialCreatedNodes[i] = newFrameStateValue;
|
|
696 |
}
|
43972
|
697 |
}
|
|
698 |
}
|
|
699 |
}
|
|
700 |
}
|
|
701 |
|
46371
|
702 |
FrameState newFrameState = graph.add(new FrameState(frameState.outerFrameState(), frameState.getCode(), frameState.bci, newFrameStateValues, frameState.localsSize(),
|
43972
|
703 |
frameState.stackSize(), frameState.rethrowException(), frameState.duringCall(), frameState.monitorIds(), frameState.virtualObjectMappings()));
|
|
704 |
|
46344
|
705 |
frameState.replaceAtUsagesAndDelete(newFrameState);
|
43972
|
706 |
frameState = newFrameState;
|
|
707 |
}
|
|
708 |
|
|
709 |
loopBegin.replaceAtUsagesAndDelete(merge);
|
|
710 |
merge.setStateAfter(frameState);
|
|
711 |
merge.setNext(successor);
|
|
712 |
for (EndNode predecessor : predecessors) {
|
|
713 |
merge.addForwardEnd(predecessor);
|
|
714 |
}
|
|
715 |
|
|
716 |
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE) {
|
|
717 |
LoopExplosionState explosionState = new LoopExplosionState(frameState, merge);
|
|
718 |
loopScope.iterationStates.put(explosionState, explosionState);
|
|
719 |
}
|
|
720 |
}
|
|
721 |
|
|
722 |
/**
|
|
723 |
* Hook for subclasses.
|
|
724 |
*
|
|
725 |
* @param methodScope The current method.
|
|
726 |
* @param loopScope The current loop.
|
|
727 |
*/
|
|
728 |
protected void checkLoopExplosionIteration(MethodScope methodScope, LoopScope loopScope) {
|
|
729 |
throw shouldNotReachHere("when subclass uses loop explosion, it needs to implement this method");
|
|
730 |
}
|
|
731 |
|
|
732 |
protected FixedNode handleLoopExplosionEnd(MethodScope methodScope, LoopScope loopScope, LoopEndNode loopEnd) {
|
46371
|
733 |
EndNode replacementNode = graph.add(new EndNode());
|
43972
|
734 |
loopEnd.replaceAtPredecessor(replacementNode);
|
|
735 |
loopEnd.safeDelete();
|
|
736 |
|
|
737 |
assert methodScope.loopExplosion != LoopExplosionKind.NONE;
|
|
738 |
if (methodScope.loopExplosion != LoopExplosionKind.FULL_UNROLL || loopScope.nextIterations.isEmpty()) {
|
|
739 |
int nextIterationNumber = loopScope.nextIterations.isEmpty() ? loopScope.loopIteration + 1 : loopScope.nextIterations.getLast().loopIteration + 1;
|
48861
|
740 |
LoopScope nextIterationScope = new LoopScope(methodScope, loopScope.outer, loopScope.loopDepth, nextIterationNumber, loopScope.loopBeginOrderId,
|
|
741 |
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length),
|
46371
|
742 |
Arrays.copyOf(loopScope.initialCreatedNodes, loopScope.initialCreatedNodes.length), loopScope.nextIterations, loopScope.iterationStates);
|
43972
|
743 |
checkLoopExplosionIteration(methodScope, nextIterationScope);
|
|
744 |
loopScope.nextIterations.addLast(nextIterationScope);
|
|
745 |
registerNode(nextIterationScope, loopScope.loopBeginOrderId, null, true, true);
|
|
746 |
makeStubNode(methodScope, nextIterationScope, loopScope.loopBeginOrderId);
|
|
747 |
}
|
|
748 |
return replacementNode;
|
|
749 |
}
|
|
750 |
|
|
751 |
/**
|
|
752 |
* Hook for subclasses.
|
|
753 |
*
|
|
754 |
* @param methodScope The current method.
|
|
755 |
* @param loopScope The current loop.
|
|
756 |
* @param nodeOrderId The orderId of the node.
|
|
757 |
* @param node The node to be simplified.
|
|
758 |
*/
|
|
759 |
protected void handleFixedNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId, FixedNode node) {
|
|
760 |
}
|
|
761 |
|
|
762 |
protected void handleProxyNodes(MethodScope methodScope, LoopScope loopScope, LoopExitNode loopExit) {
|
|
763 |
assert loopExit.stateAfter() == null;
|
|
764 |
int stateAfterOrderId = readOrderId(methodScope);
|
|
765 |
loopExit.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, stateAfterOrderId));
|
|
766 |
|
|
767 |
int numProxies = methodScope.reader.getUVInt();
|
|
768 |
for (int i = 0; i < numProxies; i++) {
|
|
769 |
int proxyOrderId = readOrderId(methodScope);
|
|
770 |
ProxyNode proxy = (ProxyNode) ensureNodeCreated(methodScope, loopScope, proxyOrderId);
|
|
771 |
/*
|
|
772 |
* The ProxyNode transports a value from the loop to the outer scope. We therefore
|
|
773 |
* register it in the outer scope.
|
|
774 |
*/
|
46371
|
775 |
if (loopScope.outer.createdNodes != loopScope.createdNodes) {
|
|
776 |
registerNode(loopScope.outer, proxyOrderId, proxy, false, false);
|
|
777 |
}
|
43972
|
778 |
}
|
|
779 |
}
|
|
780 |
|
|
781 |
protected void handleLoopExplosionProxyNodes(MethodScope methodScope, LoopScope loopScope, LoopScope outerScope, LoopExitNode loopExit, int loopExitOrderId) {
|
|
782 |
assert loopExit.stateAfter() == null;
|
|
783 |
int stateAfterOrderId = readOrderId(methodScope);
|
|
784 |
|
46371
|
785 |
BeginNode begin = graph.add(new BeginNode());
|
43972
|
786 |
|
|
787 |
FixedNode loopExitSuccessor = loopExit.next();
|
|
788 |
loopExit.replaceAtPredecessor(begin);
|
|
789 |
|
|
790 |
MergeNode loopExitPlaceholder = null;
|
|
791 |
if (methodScope.loopExplosion == LoopExplosionKind.MERGE_EXPLODE && loopScope.loopDepth == 1) {
|
|
792 |
/*
|
|
793 |
* This exit might end up as a loop exit of a loop detected after partial evaluation. We
|
|
794 |
* need to be able to create a FrameState and the necessary proxy nodes in this case.
|
|
795 |
*/
|
46371
|
796 |
loopExitPlaceholder = graph.add(new MergeNode());
|
46344
|
797 |
methodScope.loopExplosionMerges.add(loopExitPlaceholder);
|
43972
|
798 |
|
46371
|
799 |
EndNode end = graph.add(new EndNode());
|
43972
|
800 |
begin.setNext(end);
|
|
801 |
loopExitPlaceholder.addForwardEnd(end);
|
|
802 |
|
46371
|
803 |
begin = graph.add(new BeginNode());
|
43972
|
804 |
loopExitPlaceholder.setNext(begin);
|
|
805 |
}
|
|
806 |
|
|
807 |
/*
|
|
808 |
* In the original graph, the loop exit is not a merge node. Multiple exploded loop
|
|
809 |
* iterations now take the same loop exit, so we have to introduce a new merge node to
|
|
810 |
* handle the merge.
|
|
811 |
*/
|
|
812 |
MergeNode merge = null;
|
|
813 |
Node existingExit = lookupNode(outerScope, loopExitOrderId);
|
|
814 |
if (existingExit == null) {
|
|
815 |
/* First loop iteration that exits. No merge necessary yet. */
|
|
816 |
registerNode(outerScope, loopExitOrderId, begin, false, false);
|
|
817 |
begin.setNext(loopExitSuccessor);
|
|
818 |
|
|
819 |
} else if (existingExit instanceof BeginNode) {
|
|
820 |
/* Second loop iteration that exits. Create the merge. */
|
46371
|
821 |
merge = graph.add(new MergeNode());
|
43972
|
822 |
registerNode(outerScope, loopExitOrderId, merge, true, false);
|
|
823 |
/* Add the first iteration. */
|
46371
|
824 |
EndNode firstEnd = graph.add(new EndNode());
|
43972
|
825 |
((BeginNode) existingExit).setNext(firstEnd);
|
|
826 |
merge.addForwardEnd(firstEnd);
|
|
827 |
merge.setNext(loopExitSuccessor);
|
|
828 |
|
|
829 |
} else {
|
|
830 |
/* Subsequent loop iteration. Merge already created. */
|
|
831 |
merge = (MergeNode) existingExit;
|
|
832 |
}
|
|
833 |
|
|
834 |
if (merge != null) {
|
46371
|
835 |
EndNode end = graph.add(new EndNode());
|
43972
|
836 |
begin.setNext(end);
|
|
837 |
merge.addForwardEnd(end);
|
|
838 |
}
|
|
839 |
|
|
840 |
/*
|
|
841 |
* Possibly create phi nodes for the original proxy nodes that flow out of the loop. Note
|
|
842 |
* that we definitely do not need a proxy node itself anymore, since the loop was exploded
|
|
843 |
* and is no longer present.
|
|
844 |
*/
|
|
845 |
int numProxies = methodScope.reader.getUVInt();
|
|
846 |
boolean phiCreated = false;
|
|
847 |
for (int i = 0; i < numProxies; i++) {
|
|
848 |
int proxyOrderId = readOrderId(methodScope);
|
|
849 |
ProxyNode proxy = (ProxyNode) ensureNodeCreated(methodScope, loopScope, proxyOrderId);
|
|
850 |
ValueNode phiInput = proxy.value();
|
|
851 |
|
|
852 |
if (loopExitPlaceholder != null) {
|
|
853 |
if (!phiInput.isConstant()) {
|
46371
|
854 |
phiInput = graph.unique(new ProxyPlaceholder(phiInput, loopExitPlaceholder));
|
43972
|
855 |
}
|
|
856 |
registerNode(loopScope, proxyOrderId, phiInput, true, false);
|
|
857 |
}
|
|
858 |
|
|
859 |
ValueNode replacement;
|
|
860 |
ValueNode existing = (ValueNode) outerScope.createdNodes[proxyOrderId];
|
|
861 |
if (existing == null || existing == phiInput) {
|
|
862 |
/*
|
|
863 |
* We are at the first loop exit, or the proxy carries the same value for all exits.
|
|
864 |
* We do not need a phi node yet.
|
|
865 |
*/
|
|
866 |
registerNode(outerScope, proxyOrderId, phiInput, true, false);
|
|
867 |
replacement = phiInput;
|
|
868 |
|
|
869 |
} else if (!merge.isPhiAtMerge(existing)) {
|
|
870 |
/* Now we have two different values, so we need to create a phi node. */
|
46371
|
871 |
PhiNode phi;
|
|
872 |
if (proxy instanceof ValueProxyNode) {
|
48190
|
873 |
phi = graph.addWithoutUnique(new ValuePhiNode(proxy.stamp(NodeView.DEFAULT), merge));
|
46371
|
874 |
} else if (proxy instanceof GuardProxyNode) {
|
|
875 |
phi = graph.addWithoutUnique(new GuardPhiNode(merge));
|
|
876 |
} else {
|
|
877 |
throw GraalError.shouldNotReachHere();
|
|
878 |
}
|
43972
|
879 |
/* Add the inputs from all previous exits. */
|
|
880 |
for (int j = 0; j < merge.phiPredecessorCount() - 1; j++) {
|
|
881 |
phi.addInput(existing);
|
|
882 |
}
|
|
883 |
/* Add the input from this exit. */
|
|
884 |
phi.addInput(phiInput);
|
|
885 |
registerNode(outerScope, proxyOrderId, phi, true, false);
|
|
886 |
replacement = phi;
|
|
887 |
phiCreated = true;
|
|
888 |
|
|
889 |
} else {
|
|
890 |
/* Phi node has been created before, so just add the new input. */
|
|
891 |
PhiNode phi = (PhiNode) existing;
|
|
892 |
phi.addInput(phiInput);
|
|
893 |
replacement = phi;
|
|
894 |
}
|
|
895 |
|
|
896 |
proxy.replaceAtUsagesAndDelete(replacement);
|
|
897 |
}
|
|
898 |
|
|
899 |
if (loopExitPlaceholder != null) {
|
|
900 |
registerNode(loopScope, stateAfterOrderId, null, true, true);
|
|
901 |
loopExitPlaceholder.setStateAfter((FrameState) ensureNodeCreated(methodScope, loopScope, stateAfterOrderId));
|
|
902 |
}
|
|
903 |
|
|
904 |
if (merge != null && (merge.stateAfter() == null || phiCreated)) {
|
|
905 |
FrameState oldStateAfter = merge.stateAfter();
|
|
906 |
registerNode(outerScope, stateAfterOrderId, null, true, true);
|
|
907 |
merge.setStateAfter((FrameState) ensureNodeCreated(methodScope, outerScope, stateAfterOrderId));
|
|
908 |
if (oldStateAfter != null) {
|
|
909 |
oldStateAfter.safeDelete();
|
|
910 |
}
|
|
911 |
}
|
|
912 |
loopExit.safeDelete();
|
|
913 |
assert loopExitSuccessor.predecessor() == null;
|
|
914 |
if (merge != null) {
|
|
915 |
merge.getNodeClass().getSuccessorEdges().update(merge, null, loopExitSuccessor);
|
|
916 |
} else {
|
|
917 |
begin.getNodeClass().getSuccessorEdges().update(begin, null, loopExitSuccessor);
|
|
918 |
}
|
|
919 |
}
|
|
920 |
|
|
921 |
protected void handlePhiFunctions(MethodScope methodScope, LoopScope phiInputScope, LoopScope phiNodeScope, AbstractEndNode end, AbstractMergeNode merge) {
|
|
922 |
|
|
923 |
if (end instanceof LoopEndNode) {
|
|
924 |
/*
|
|
925 |
* Fix the loop end index and the number of loop ends. When we do canonicalization
|
|
926 |
* during decoding, we can end up with fewer ends than the encoded graph had. And the
|
|
927 |
* order of loop ends can be different.
|
|
928 |
*/
|
|
929 |
int numEnds = ((LoopBeginNode) merge).loopEnds().count();
|
|
930 |
((LoopBeginNode) merge).nextEndIndex = numEnds;
|
|
931 |
((LoopEndNode) end).endIndex = numEnds - 1;
|
|
932 |
|
|
933 |
} else {
|
|
934 |
if (merge.ends == null) {
|
|
935 |
merge.ends = new NodeInputList<>(merge);
|
|
936 |
}
|
|
937 |
merge.addForwardEnd((EndNode) end);
|
|
938 |
}
|
|
939 |
|
|
940 |
/*
|
|
941 |
* We create most phi functions lazily. Canonicalization and simplification during decoding
|
|
942 |
* can lead to dead branches that are not decoded, so we might not need all phi functions
|
|
943 |
* that the original graph contained. Since we process all predecessors before actually
|
|
944 |
* processing the merge node, we have the final phi function when processing the merge node.
|
|
945 |
* The only exception are loop headers of non-exploded loops: since backward branches are
|
|
946 |
* not processed yet when processing the loop body, we need to create all phi functions
|
|
947 |
* upfront.
|
|
948 |
*/
|
|
949 |
boolean lazyPhi = allowLazyPhis() && (!(merge instanceof LoopBeginNode) || methodScope.loopExplosion != LoopExplosionKind.NONE);
|
|
950 |
int numPhis = methodScope.reader.getUVInt();
|
|
951 |
for (int i = 0; i < numPhis; i++) {
|
|
952 |
int phiInputOrderId = readOrderId(methodScope);
|
|
953 |
int phiNodeOrderId = readOrderId(methodScope);
|
|
954 |
|
|
955 |
ValueNode phiInput = (ValueNode) ensureNodeCreated(methodScope, phiInputScope, phiInputOrderId);
|
47667
|
956 |
ValueNode existing = (ValueNode) lookupNode(phiNodeScope, phiNodeOrderId);
|
43972
|
957 |
|
47667
|
958 |
if (existing != null && merge.phiPredecessorCount() == 1) {
|
|
959 |
/*
|
|
960 |
* When exploding loops and the code after the loop (FULL_EXPLODE_UNTIL_RETURN),
|
|
961 |
* then an existing value can already be registered: Parsing of the code before the
|
|
962 |
* loop registers it when preparing for the later merge. The code after the loop,
|
|
963 |
* which starts with a clone of the values that were created before the loop, sees
|
|
964 |
* the stale value when processing the merge the first time. We can safely ignore
|
|
965 |
* the stale value because it will never be needed to be merged (we are exploding
|
|
966 |
* until we hit a return).
|
|
967 |
*/
|
|
968 |
assert methodScope.loopExplosion == LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN && phiNodeScope.loopIteration > 0;
|
|
969 |
existing = null;
|
|
970 |
}
|
|
971 |
|
43972
|
972 |
if (lazyPhi && (existing == null || existing == phiInput)) {
|
|
973 |
/* Phi function not yet necessary. */
|
|
974 |
registerNode(phiNodeScope, phiNodeOrderId, phiInput, true, false);
|
|
975 |
|
|
976 |
} else if (!merge.isPhiAtMerge(existing)) {
|
|
977 |
/*
|
|
978 |
* Phi function is necessary. Create it and fill it with existing inputs as well as
|
|
979 |
* the new input.
|
|
980 |
*/
|
|
981 |
registerNode(phiNodeScope, phiNodeOrderId, null, true, true);
|
|
982 |
PhiNode phi = (PhiNode) ensureNodeCreated(methodScope, phiNodeScope, phiNodeOrderId);
|
|
983 |
|
|
984 |
phi.setMerge(merge);
|
|
985 |
for (int j = 0; j < merge.phiPredecessorCount() - 1; j++) {
|
|
986 |
phi.addInput(existing);
|
|
987 |
}
|
|
988 |
phi.addInput(phiInput);
|
|
989 |
|
|
990 |
} else {
|
|
991 |
/* Phi node has been created before, so just add the new input. */
|
|
992 |
PhiNode phi = (PhiNode) existing;
|
|
993 |
phi.addInput(phiInput);
|
|
994 |
}
|
|
995 |
}
|
|
996 |
}
|
|
997 |
|
|
998 |
protected boolean allowLazyPhis() {
|
|
999 |
/* We need to exactly reproduce the encoded graph, including unnecessary phi functions. */
|
|
1000 |
return false;
|
|
1001 |
}
|
|
1002 |
|
|
1003 |
protected void readProperties(MethodScope methodScope, Node node) {
|
|
1004 |
node.setNodeSourcePosition((NodeSourcePosition) readObject(methodScope));
|
|
1005 |
Fields fields = node.getNodeClass().getData();
|
|
1006 |
for (int pos = 0; pos < fields.getCount(); pos++) {
|
|
1007 |
if (fields.getType(pos).isPrimitive()) {
|
|
1008 |
long primitive = methodScope.reader.getSV();
|
|
1009 |
fields.setRawPrimitive(node, pos, primitive);
|
|
1010 |
} else {
|
|
1011 |
Object value = readObject(methodScope);
|
46344
|
1012 |
fields.putObject(node, pos, value);
|
43972
|
1013 |
}
|
|
1014 |
}
|
|
1015 |
}
|
|
1016 |
|
|
1017 |
/**
|
|
1018 |
* Process the input edges of a node. Input nodes that have not yet been created must be
|
|
1019 |
* non-fixed nodes (because fixed nodes are processed in reverse postorder. Such non-fixed nodes
|
|
1020 |
* are created on demand (recursively since they can themselves reference not yet created
|
|
1021 |
* nodes).
|
|
1022 |
*/
|
46371
|
1023 |
protected void makeFixedNodeInputs(MethodScope methodScope, LoopScope loopScope, Node node) {
|
46344
|
1024 |
Edges edges = node.getNodeClass().getInputEdges();
|
43972
|
1025 |
for (int index = 0; index < edges.getDirectCount(); index++) {
|
46344
|
1026 |
if (skipDirectEdge(node, edges, index)) {
|
43972
|
1027 |
continue;
|
|
1028 |
}
|
|
1029 |
int orderId = readOrderId(methodScope);
|
|
1030 |
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
|
|
1031 |
edges.initializeNode(node, index, value);
|
46371
|
1032 |
if (value != null && !value.isDeleted()) {
|
43972
|
1033 |
edges.update(node, null, value);
|
|
1034 |
|
|
1035 |
}
|
|
1036 |
}
|
46371
|
1037 |
|
|
1038 |
if (node instanceof AbstractMergeNode) {
|
|
1039 |
/* The ends of merge nodes are filled manually when the ends are processed. */
|
|
1040 |
assert edges.getCount() - edges.getDirectCount() == 1 : "MergeNode has one variable size input (the ends)";
|
|
1041 |
assert Edges.getNodeList(node, edges.getOffsets(), edges.getDirectCount()) != null : "Input list must have been already created";
|
|
1042 |
} else {
|
|
1043 |
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
|
|
1044 |
int size = methodScope.reader.getSVInt();
|
|
1045 |
if (size != -1) {
|
|
1046 |
NodeList<Node> nodeList = new NodeInputList<>(node, size);
|
|
1047 |
edges.initializeList(node, index, nodeList);
|
|
1048 |
for (int idx = 0; idx < size; idx++) {
|
|
1049 |
int orderId = readOrderId(methodScope);
|
|
1050 |
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
|
|
1051 |
nodeList.initialize(idx, value);
|
|
1052 |
if (value != null && !value.isDeleted()) {
|
|
1053 |
edges.update(node, null, value);
|
|
1054 |
}
|
|
1055 |
}
|
|
1056 |
}
|
43972
|
1057 |
}
|
46371
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
protected void makeFloatingNodeInputs(MethodScope methodScope, LoopScope loopScope, Node node) {
|
|
1062 |
Edges edges = node.getNodeClass().getInputEdges();
|
|
1063 |
if (node instanceof PhiNode) {
|
|
1064 |
/*
|
|
1065 |
* The inputs of phi functions are filled manually when the end nodes are processed.
|
|
1066 |
* However, the values must not be null, so initialize them with an empty list.
|
|
1067 |
*/
|
|
1068 |
assert edges.getDirectCount() == 1 : "PhiNode has one direct input (the MergeNode)";
|
|
1069 |
assert edges.getCount() - edges.getDirectCount() == 1 : "PhiNode has one variable size input (the values)";
|
|
1070 |
edges.initializeList(node, edges.getDirectCount(), new NodeInputList<>(node));
|
|
1071 |
} else {
|
|
1072 |
for (int index = 0; index < edges.getDirectCount(); index++) {
|
|
1073 |
int orderId = readOrderId(methodScope);
|
|
1074 |
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
|
|
1075 |
edges.initializeNode(node, index, value);
|
|
1076 |
}
|
|
1077 |
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
|
|
1078 |
int size = methodScope.reader.getSVInt();
|
|
1079 |
if (size != -1) {
|
|
1080 |
NodeList<Node> nodeList = new NodeInputList<>(node, size);
|
|
1081 |
edges.initializeList(node, index, nodeList);
|
|
1082 |
for (int idx = 0; idx < size; idx++) {
|
|
1083 |
int orderId = readOrderId(methodScope);
|
|
1084 |
Node value = ensureNodeCreated(methodScope, loopScope, orderId);
|
|
1085 |
nodeList.initialize(idx, value);
|
43972
|
1086 |
}
|
|
1087 |
}
|
|
1088 |
}
|
|
1089 |
}
|
|
1090 |
}
|
|
1091 |
|
|
1092 |
protected Node ensureNodeCreated(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
|
|
1093 |
if (nodeOrderId == GraphEncoder.NULL_ORDER_ID) {
|
|
1094 |
return null;
|
|
1095 |
}
|
|
1096 |
Node node = lookupNode(loopScope, nodeOrderId);
|
|
1097 |
if (node != null) {
|
|
1098 |
return node;
|
|
1099 |
}
|
|
1100 |
|
|
1101 |
node = decodeFloatingNode(methodScope, loopScope, nodeOrderId);
|
|
1102 |
if (node instanceof ProxyNode || node instanceof PhiNode) {
|
|
1103 |
/*
|
|
1104 |
* We need these nodes as they were in the original graph, without any canonicalization
|
|
1105 |
* or value numbering.
|
|
1106 |
*/
|
46371
|
1107 |
node = graph.addWithoutUnique(node);
|
43972
|
1108 |
} else {
|
|
1109 |
/* Allow subclasses to canonicalize and intercept nodes. */
|
46371
|
1110 |
Node newNode = handleFloatingNodeBeforeAdd(methodScope, loopScope, node);
|
|
1111 |
if (newNode != node) {
|
|
1112 |
releaseFloatingNode(node);
|
43972
|
1113 |
}
|
46371
|
1114 |
|
|
1115 |
if (!newNode.isAlive()) {
|
|
1116 |
newNode = addFloatingNode(methodScope, newNode);
|
|
1117 |
}
|
|
1118 |
node = handleFloatingNodeAfterAdd(methodScope, loopScope, newNode);
|
43972
|
1119 |
}
|
|
1120 |
registerNode(loopScope, nodeOrderId, node, false, false);
|
|
1121 |
return node;
|
|
1122 |
}
|
|
1123 |
|
46371
|
1124 |
protected Node addFloatingNode(@SuppressWarnings("unused") MethodScope methodScope, Node node) {
|
43972
|
1125 |
/*
|
|
1126 |
* We want to exactly reproduce the encoded graph. Even though nodes should be unique in the
|
|
1127 |
* encoded graph, this is not always guaranteed.
|
|
1128 |
*/
|
46371
|
1129 |
return graph.addWithoutUnique(node);
|
43972
|
1130 |
}
|
|
1131 |
|
|
1132 |
/**
|
|
1133 |
* Decodes a non-fixed node, but does not do any post-processing and does not register it.
|
|
1134 |
*/
|
|
1135 |
protected Node decodeFloatingNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
|
|
1136 |
long readerByteIndex = methodScope.reader.getByteIndex();
|
46371
|
1137 |
|
|
1138 |
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
|
|
1139 |
NodeClass<?> nodeClass = methodScope.encodedGraph.getNodeClasses()[methodScope.reader.getUVInt()];
|
|
1140 |
Node node = allocateFloatingNode(nodeClass);
|
43972
|
1141 |
if (node instanceof FixedNode) {
|
|
1142 |
/*
|
|
1143 |
* This is a severe error that will lead to a corrupted graph, so it is better not to
|
|
1144 |
* continue decoding at all.
|
|
1145 |
*/
|
|
1146 |
throw shouldNotReachHere("Not a floating node: " + node.getClass().getName());
|
|
1147 |
}
|
|
1148 |
|
46371
|
1149 |
/* Read the inputs of the node, possibly creating them recursively. */
|
|
1150 |
makeFloatingNodeInputs(methodScope, loopScope, node);
|
|
1151 |
|
43972
|
1152 |
/* Read the properties of the node. */
|
|
1153 |
readProperties(methodScope, node);
|
|
1154 |
/* There must not be any successors to read, since it is a non-fixed node. */
|
|
1155 |
assert node.getNodeClass().getEdges(Edges.Type.Successors).getCount() == 0;
|
46371
|
1156 |
|
43972
|
1157 |
methodScope.reader.setByteIndex(readerByteIndex);
|
|
1158 |
return node;
|
|
1159 |
}
|
|
1160 |
|
46371
|
1161 |
private Node allocateFloatingNode(NodeClass<?> nodeClass) {
|
|
1162 |
ArrayDeque<? extends Node> cachedNodes = reusableFloatingNodes.get(nodeClass);
|
|
1163 |
if (cachedNodes != null) {
|
|
1164 |
Node node = cachedNodes.poll();
|
|
1165 |
if (node != null) {
|
|
1166 |
return node;
|
|
1167 |
}
|
|
1168 |
}
|
|
1169 |
return nodeClass.allocateInstance();
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
private void releaseFloatingNode(Node node) {
|
|
1173 |
ArrayDeque<Node> cachedNodes = reusableFloatingNodes.get(node.getNodeClass());
|
|
1174 |
if (cachedNodes == null) {
|
|
1175 |
cachedNodes = new ArrayDeque<>(2);
|
|
1176 |
reusableFloatingNodes.put(node.getNodeClass(), cachedNodes);
|
|
1177 |
}
|
|
1178 |
cachedNodes.push(node);
|
|
1179 |
}
|
|
1180 |
|
43972
|
1181 |
/**
|
|
1182 |
* Hook for subclasses to process a non-fixed node before it is added to the graph.
|
|
1183 |
*
|
|
1184 |
* @param methodScope The current method.
|
|
1185 |
* @param loopScope The current loop.
|
|
1186 |
* @param node The node to be canonicalized.
|
|
1187 |
* @return The replacement for the node, or the node itself.
|
|
1188 |
*/
|
|
1189 |
protected Node handleFloatingNodeBeforeAdd(MethodScope methodScope, LoopScope loopScope, Node node) {
|
|
1190 |
return node;
|
|
1191 |
}
|
|
1192 |
|
|
1193 |
/**
|
|
1194 |
* Hook for subclasses to process a non-fixed node after it is added to the graph.
|
|
1195 |
*
|
|
1196 |
* If this method replaces a node with another node, it must update its source position if the
|
|
1197 |
* original node has the source position set.
|
|
1198 |
*
|
|
1199 |
* @param methodScope The current method.
|
|
1200 |
* @param loopScope The current loop.
|
|
1201 |
* @param node The node to be canonicalized.
|
|
1202 |
* @return The replacement for the node, or the node itself.
|
|
1203 |
*/
|
|
1204 |
protected Node handleFloatingNodeAfterAdd(MethodScope methodScope, LoopScope loopScope, Node node) {
|
|
1205 |
return node;
|
|
1206 |
}
|
|
1207 |
|
|
1208 |
/**
|
|
1209 |
* Process successor edges of a node. We create the successor nodes so that we can fill the
|
|
1210 |
* successor list, but no properties or edges are loaded yet. That is done when the successor is
|
|
1211 |
* on top of the worklist in {@link #processNextNode}.
|
|
1212 |
*/
|
|
1213 |
protected void makeSuccessorStubs(MethodScope methodScope, LoopScope loopScope, Node node, boolean updatePredecessors) {
|
46344
|
1214 |
Edges edges = node.getNodeClass().getSuccessorEdges();
|
43972
|
1215 |
for (int index = 0; index < edges.getDirectCount(); index++) {
|
46344
|
1216 |
if (skipDirectEdge(node, edges, index)) {
|
43972
|
1217 |
continue;
|
|
1218 |
}
|
|
1219 |
int orderId = readOrderId(methodScope);
|
|
1220 |
Node value = makeStubNode(methodScope, loopScope, orderId);
|
|
1221 |
edges.initializeNode(node, index, value);
|
|
1222 |
if (updatePredecessors && value != null) {
|
|
1223 |
edges.update(node, null, value);
|
|
1224 |
}
|
|
1225 |
}
|
|
1226 |
for (int index = edges.getDirectCount(); index < edges.getCount(); index++) {
|
|
1227 |
int size = methodScope.reader.getSVInt();
|
|
1228 |
if (size != -1) {
|
|
1229 |
NodeList<Node> nodeList = new NodeSuccessorList<>(node, size);
|
|
1230 |
edges.initializeList(node, index, nodeList);
|
|
1231 |
for (int idx = 0; idx < size; idx++) {
|
|
1232 |
int orderId = readOrderId(methodScope);
|
|
1233 |
Node value = makeStubNode(methodScope, loopScope, orderId);
|
|
1234 |
nodeList.initialize(idx, value);
|
|
1235 |
if (updatePredecessors && value != null) {
|
|
1236 |
edges.update(node, null, value);
|
|
1237 |
}
|
|
1238 |
}
|
|
1239 |
}
|
|
1240 |
}
|
|
1241 |
}
|
|
1242 |
|
|
1243 |
protected FixedNode makeStubNode(MethodScope methodScope, LoopScope loopScope, int nodeOrderId) {
|
|
1244 |
if (nodeOrderId == GraphEncoder.NULL_ORDER_ID) {
|
|
1245 |
return null;
|
|
1246 |
}
|
|
1247 |
FixedNode node = (FixedNode) lookupNode(loopScope, nodeOrderId);
|
|
1248 |
if (node != null) {
|
|
1249 |
return node;
|
|
1250 |
}
|
|
1251 |
|
|
1252 |
long readerByteIndex = methodScope.reader.getByteIndex();
|
46371
|
1253 |
methodScope.reader.setByteIndex(methodScope.encodedGraph.nodeStartOffsets[nodeOrderId]);
|
|
1254 |
NodeClass<?> nodeClass = methodScope.encodedGraph.getNodeClasses()[methodScope.reader.getUVInt()];
|
|
1255 |
node = (FixedNode) graph.add(nodeClass.allocateInstance());
|
43972
|
1256 |
/* Properties and edges are not filled yet, the node remains uninitialized. */
|
|
1257 |
methodScope.reader.setByteIndex(readerByteIndex);
|
|
1258 |
|
|
1259 |
registerNode(loopScope, nodeOrderId, node, false, false);
|
|
1260 |
loopScope.nodesToProcess.set(nodeOrderId);
|
|
1261 |
return node;
|
|
1262 |
}
|
|
1263 |
|
46344
|
1264 |
protected static boolean skipDirectEdge(Node node, Edges edges, int index) {
|
|
1265 |
if (node instanceof Invoke) {
|
43972
|
1266 |
assert node instanceof InvokeNode || node instanceof InvokeWithExceptionNode : "The only two Invoke node classes. Got " + node.getClass();
|
|
1267 |
if (edges.type() == Edges.Type.Successors) {
|
|
1268 |
assert edges.getCount() == (node instanceof InvokeWithExceptionNode ? 2 : 1) : "InvokeNode has one successor (next); InvokeWithExceptionNode has two successors (next, exceptionEdge)";
|
|
1269 |
return true;
|
|
1270 |
} else {
|
|
1271 |
assert edges.type() == Edges.Type.Inputs;
|
|
1272 |
if (edges.getType(index) == CallTargetNode.class) {
|
|
1273 |
return true;
|
|
1274 |
} else if (edges.getType(index) == FrameState.class) {
|
|
1275 |
assert edges.get(node, index) == null || edges.get(node, index) == ((Invoke) node).stateAfter() : "Only stateAfter can be a FrameState during encoding";
|
|
1276 |
return true;
|
|
1277 |
}
|
|
1278 |
}
|
46344
|
1279 |
} else if (node instanceof LoopExitNode && edges.type() == Edges.Type.Inputs && edges.getType(index) == FrameState.class) {
|
|
1280 |
/* The stateAfter of the loop exit is filled manually. */
|
|
1281 |
return true;
|
|
1282 |
|
|
1283 |
}
|
|
1284 |
return false;
|
|
1285 |
}
|
|
1286 |
|
43972
|
1287 |
protected Node lookupNode(LoopScope loopScope, int nodeOrderId) {
|
|
1288 |
return loopScope.createdNodes[nodeOrderId];
|
|
1289 |
}
|
|
1290 |
|
|
1291 |
protected void registerNode(LoopScope loopScope, int nodeOrderId, Node node, boolean allowOverwrite, boolean allowNull) {
|
|
1292 |
assert node == null || node.isAlive();
|
|
1293 |
assert allowNull || node != null;
|
|
1294 |
assert allowOverwrite || lookupNode(loopScope, nodeOrderId) == null;
|
|
1295 |
loopScope.createdNodes[nodeOrderId] = node;
|
|
1296 |
}
|
|
1297 |
|
|
1298 |
protected int readOrderId(MethodScope methodScope) {
|
|
1299 |
return methodScope.reader.getUVInt();
|
|
1300 |
}
|
|
1301 |
|
|
1302 |
protected Object readObject(MethodScope methodScope) {
|
|
1303 |
return methodScope.encodedGraph.getObjects()[methodScope.reader.getUVInt()];
|
|
1304 |
}
|
|
1305 |
|
|
1306 |
/**
|
|
1307 |
* Removes unnecessary nodes from the graph after decoding.
|
|
1308 |
*
|
|
1309 |
* @param methodScope The current method.
|
|
1310 |
*/
|
|
1311 |
protected void cleanupGraph(MethodScope methodScope) {
|
46371
|
1312 |
assert verifyEdges();
|
43972
|
1313 |
}
|
|
1314 |
|
46371
|
1315 |
protected boolean verifyEdges() {
|
|
1316 |
for (Node node : graph.getNodes()) {
|
43972
|
1317 |
assert node.isAlive();
|
|
1318 |
for (Node i : node.inputs()) {
|
|
1319 |
assert i.isAlive();
|
|
1320 |
assert i.usages().contains(node);
|
|
1321 |
}
|
|
1322 |
for (Node s : node.successors()) {
|
|
1323 |
assert s.isAlive();
|
|
1324 |
assert s.predecessor() == node;
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
for (Node usage : node.usages()) {
|
|
1328 |
assert usage.isAlive();
|
|
1329 |
assert usage.inputs().contains(node) : node + " / " + usage + " / " + usage.inputs().count();
|
|
1330 |
}
|
|
1331 |
if (node.predecessor() != null) {
|
|
1332 |
assert node.predecessor().isAlive();
|
|
1333 |
assert node.predecessor().successors().contains(node);
|
|
1334 |
}
|
|
1335 |
}
|
|
1336 |
return true;
|
|
1337 |
}
|
|
1338 |
}
|
|
1339 |
|
|
1340 |
class LoopDetector implements Runnable {
|
|
1341 |
|
|
1342 |
/**
|
|
1343 |
* Information about loops before the actual loop nodes are inserted.
|
|
1344 |
*/
|
|
1345 |
static class Loop {
|
|
1346 |
/**
|
|
1347 |
* The header, i.e., the target of backward branches.
|
|
1348 |
*/
|
|
1349 |
MergeNode header;
|
|
1350 |
/**
|
|
1351 |
* The ends, i.e., the source of backward branches. The {@link EndNode#successors successor}
|
|
1352 |
* is the {@link #header loop header}.
|
|
1353 |
*/
|
46371
|
1354 |
List<EndNode> ends = new ArrayList<>(2);
|
43972
|
1355 |
/**
|
|
1356 |
* Exits of the loop. The successor is a {@link MergeNode} marked in
|
|
1357 |
* {@link MethodScope#loopExplosionMerges}.
|
|
1358 |
*/
|
|
1359 |
List<AbstractEndNode> exits = new ArrayList<>();
|
|
1360 |
/**
|
|
1361 |
* Set to true when the loop is irreducible, i.e., has multiple entries. See
|
|
1362 |
* {@link #handleIrreducibleLoop} for details on the handling.
|
|
1363 |
*/
|
|
1364 |
boolean irreducible;
|
|
1365 |
}
|
|
1366 |
|
46371
|
1367 |
private final StructuredGraph graph;
|
43972
|
1368 |
private final MethodScope methodScope;
|
|
1369 |
|
|
1370 |
private Loop irreducibleLoopHandler;
|
|
1371 |
private IntegerSwitchNode irreducibleLoopSwitch;
|
|
1372 |
|
46371
|
1373 |
protected LoopDetector(StructuredGraph graph, MethodScope methodScope) {
|
|
1374 |
this.graph = graph;
|
43972
|
1375 |
this.methodScope = methodScope;
|
|
1376 |
}
|
|
1377 |
|
|
1378 |
@Override
|
|
1379 |
public void run() {
|
46640
|
1380 |
DebugContext debug = graph.getDebug();
|
|
1381 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "Before loop detection");
|
43972
|
1382 |
|
|
1383 |
List<Loop> orderedLoops = findLoops();
|
|
1384 |
assert orderedLoops.get(orderedLoops.size() - 1) == irreducibleLoopHandler : "outermost loop must be the last element in the list";
|
|
1385 |
|
|
1386 |
for (Loop loop : orderedLoops) {
|
|
1387 |
if (loop.ends.isEmpty()) {
|
|
1388 |
assert loop == irreducibleLoopHandler;
|
|
1389 |
continue;
|
|
1390 |
}
|
|
1391 |
|
|
1392 |
/*
|
|
1393 |
* The algorithm to find loop exits requires that inner loops have already been
|
|
1394 |
* processed. Therefore, we need to iterate the loops in order (inner loops before outer
|
|
1395 |
* loops), and we cannot find the exits for all loops before we start inserting nodes.
|
|
1396 |
*/
|
|
1397 |
findLoopExits(loop);
|
|
1398 |
|
|
1399 |
if (loop.irreducible) {
|
|
1400 |
handleIrreducibleLoop(loop);
|
|
1401 |
} else {
|
|
1402 |
insertLoopNodes(loop);
|
|
1403 |
}
|
46640
|
1404 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After handling of loop %s", loop.header);
|
43972
|
1405 |
}
|
|
1406 |
|
|
1407 |
logIrreducibleLoops();
|
46640
|
1408 |
debug.dump(DebugContext.DETAILED_LEVEL, graph, "After loop detection");
|
43972
|
1409 |
}
|
|
1410 |
|
|
1411 |
private List<Loop> findLoops() {
|
|
1412 |
/* Mapping from the loop header node to additional loop information. */
|
46344
|
1413 |
EconomicMap<MergeNode, Loop> unorderedLoops = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
1414 |
/* Loops in reverse order of, i.e., inner loops before outer loops. */
|
|
1415 |
List<Loop> orderedLoops = new ArrayList<>();
|
|
1416 |
|
|
1417 |
/*
|
|
1418 |
* Ensure we have an outermost loop that we can use to eliminate irreducible loops. This
|
|
1419 |
* loop can remain empty (no ends), in which case it is ignored.
|
|
1420 |
*/
|
|
1421 |
irreducibleLoopHandler = findOrCreateLoop(unorderedLoops, methodScope.loopExplosionHead);
|
|
1422 |
|
46371
|
1423 |
NodeBitMap visited = graph.createNodeBitMap();
|
|
1424 |
NodeBitMap active = graph.createNodeBitMap();
|
43972
|
1425 |
Deque<Node> stack = new ArrayDeque<>();
|
46371
|
1426 |
visited.mark(methodScope.loopExplosionHead);
|
|
1427 |
stack.push(methodScope.loopExplosionHead);
|
43972
|
1428 |
|
|
1429 |
while (!stack.isEmpty()) {
|
|
1430 |
Node current = stack.peek();
|
|
1431 |
assert visited.isMarked(current);
|
|
1432 |
|
|
1433 |
if (active.isMarked(current)) {
|
|
1434 |
/* We are back-tracking, i.e., all successor nodes have been processed. */
|
|
1435 |
stack.pop();
|
|
1436 |
active.clear(current);
|
|
1437 |
|
46344
|
1438 |
if (current instanceof MergeNode) {
|
|
1439 |
Loop loop = unorderedLoops.get((MergeNode) current);
|
|
1440 |
if (loop != null) {
|
|
1441 |
/*
|
|
1442 |
* Since nodes are popped in reverse order that they were pushed, we add
|
|
1443 |
* inner loops before outer loops here.
|
|
1444 |
*/
|
|
1445 |
assert !orderedLoops.contains(loop);
|
|
1446 |
orderedLoops.add(loop);
|
|
1447 |
}
|
43972
|
1448 |
}
|
|
1449 |
|
|
1450 |
} else {
|
|
1451 |
/*
|
|
1452 |
* Process the node. Note that we do not remove the node from the stack, i.e., we
|
|
1453 |
* will peek it again. But the next time the node is marked as active, so we do not
|
|
1454 |
* execute this code again.
|
|
1455 |
*/
|
|
1456 |
active.mark(current);
|
|
1457 |
for (Node successor : current.cfgSuccessors()) {
|
|
1458 |
if (active.isMarked(successor)) {
|
|
1459 |
/* Detected a cycle, i.e., a backward branch of a loop. */
|
|
1460 |
Loop loop = findOrCreateLoop(unorderedLoops, (MergeNode) successor);
|
|
1461 |
assert !loop.ends.contains(current);
|
|
1462 |
loop.ends.add((EndNode) current);
|
|
1463 |
|
|
1464 |
} else if (visited.isMarked(successor)) {
|
|
1465 |
/* Forward merge into a branch we are already exploring. */
|
|
1466 |
|
|
1467 |
} else {
|
|
1468 |
/* Forward branch to a node we have not seen yet. */
|
|
1469 |
visited.mark(successor);
|
|
1470 |
stack.push(successor);
|
|
1471 |
}
|
|
1472 |
}
|
|
1473 |
}
|
|
1474 |
}
|
|
1475 |
return orderedLoops;
|
|
1476 |
}
|
|
1477 |
|
46344
|
1478 |
private Loop findOrCreateLoop(EconomicMap<MergeNode, Loop> unorderedLoops, MergeNode loopHeader) {
|
|
1479 |
assert methodScope.loopExplosionMerges.contains(loopHeader) : loopHeader;
|
43972
|
1480 |
Loop loop = unorderedLoops.get(loopHeader);
|
|
1481 |
if (loop == null) {
|
|
1482 |
loop = new Loop();
|
|
1483 |
loop.header = loopHeader;
|
|
1484 |
unorderedLoops.put(loopHeader, loop);
|
|
1485 |
}
|
|
1486 |
return loop;
|
|
1487 |
}
|
|
1488 |
|
|
1489 |
private void findLoopExits(Loop loop) {
|
|
1490 |
/*
|
|
1491 |
* Backward marking of loop nodes: Starting with the known loop ends, we mark all nodes that
|
|
1492 |
* are reachable until we hit the loop begin. All successors of loop nodes that are not
|
|
1493 |
* marked as loop nodes themselves are exits of the loop. We mark all successors, and then
|
|
1494 |
* subtract the loop nodes, to find the exits.
|
|
1495 |
*/
|
|
1496 |
|
46344
|
1497 |
List<Node> possibleExits = new ArrayList<>();
|
46371
|
1498 |
NodeBitMap visited = graph.createNodeBitMap();
|
43972
|
1499 |
Deque<Node> stack = new ArrayDeque<>();
|
|
1500 |
for (EndNode loopEnd : loop.ends) {
|
|
1501 |
stack.push(loopEnd);
|
|
1502 |
visited.mark(loopEnd);
|
|
1503 |
}
|
|
1504 |
|
|
1505 |
while (!stack.isEmpty()) {
|
|
1506 |
Node current = stack.pop();
|
|
1507 |
if (current == loop.header) {
|
|
1508 |
continue;
|
|
1509 |
}
|
46371
|
1510 |
if (!graph.isNew(methodScope.methodStartMark, current)) {
|
43972
|
1511 |
/*
|
|
1512 |
* The current node is before the method that contains the exploded loop. The loop
|
|
1513 |
* must have a second entry point, i.e., it is an irreducible loop.
|
|
1514 |
*/
|
|
1515 |
loop.irreducible = true;
|
|
1516 |
return;
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
for (Node predecessor : current.cfgPredecessors()) {
|
|
1520 |
if (predecessor instanceof LoopExitNode) {
|
|
1521 |
/*
|
|
1522 |
* Inner loop. We do not need to mark every node of it, instead we just continue
|
|
1523 |
* marking at the loop header.
|
|
1524 |
*/
|
|
1525 |
LoopBeginNode innerLoopBegin = ((LoopExitNode) predecessor).loopBegin();
|
|
1526 |
if (!visited.isMarked(innerLoopBegin)) {
|
|
1527 |
stack.push(innerLoopBegin);
|
|
1528 |
visited.mark(innerLoopBegin);
|
|
1529 |
|
|
1530 |
/*
|
|
1531 |
* All loop exits of the inner loop possibly need a LoopExit of our loop.
|
|
1532 |
* Because we are processing inner loops first, we are guaranteed to already
|
|
1533 |
* have all exits of the inner loop.
|
|
1534 |
*/
|
|
1535 |
for (LoopExitNode exit : innerLoopBegin.loopExits()) {
|
46344
|
1536 |
possibleExits.add(exit);
|
43972
|
1537 |
}
|
|
1538 |
}
|
|
1539 |
|
|
1540 |
} else if (!visited.isMarked(predecessor)) {
|
|
1541 |
stack.push(predecessor);
|
|
1542 |
visited.mark(predecessor);
|
|
1543 |
|
|
1544 |
if (predecessor instanceof ControlSplitNode) {
|
|
1545 |
for (Node succ : predecessor.cfgSuccessors()) {
|
|
1546 |
/*
|
|
1547 |
* We would not need to mark the current node, and would not need to
|
|
1548 |
* mark visited nodes. But it is easier to just mark everything, since
|
|
1549 |
* we subtract all visited nodes in the end anyway. Note that at this
|
|
1550 |
* point we do not have the complete visited information, so we would
|
|
1551 |
* always mark too many possible exits.
|
|
1552 |
*/
|
46344
|
1553 |
possibleExits.add(succ);
|
43972
|
1554 |
}
|
|
1555 |
}
|
|
1556 |
}
|
|
1557 |
}
|
|
1558 |
}
|
|
1559 |
|
|
1560 |
/*
|
|
1561 |
* Now we know all the actual loop exits. Ideally, we would insert LoopExit nodes for them.
|
|
1562 |
* However, a LoopExit needs a valid FrameState that captures the state at the point where
|
|
1563 |
* we exit the loop. During graph decoding, we create a FrameState for every exploded loop
|
|
1564 |
* iteration. We need to do a forward marking until we hit the next such point. This puts
|
|
1565 |
* some nodes into the loop that are actually not part of the loop.
|
|
1566 |
*
|
|
1567 |
* In some cases, we did not create a FrameState during graph decoding: when there was no
|
|
1568 |
* LoopExit in the original loop that we exploded. This happens for code paths that lead
|
|
1569 |
* immediately to a DeoptimizeNode.
|
|
1570 |
*
|
|
1571 |
* Both cases mimic the behavior of the BytecodeParser, which also puts more nodes than
|
|
1572 |
* necessary into a loop because it computes loop information based on bytecodes, before the
|
|
1573 |
* actual parsing.
|
|
1574 |
*/
|
|
1575 |
for (Node succ : possibleExits) {
|
46344
|
1576 |
if (!visited.contains(succ)) {
|
|
1577 |
stack.push(succ);
|
|
1578 |
visited.mark(succ);
|
|
1579 |
assert !methodScope.loopExplosionMerges.contains(succ);
|
|
1580 |
}
|
43972
|
1581 |
}
|
|
1582 |
|
|
1583 |
while (!stack.isEmpty()) {
|
|
1584 |
Node current = stack.pop();
|
|
1585 |
assert visited.isMarked(current);
|
|
1586 |
assert current instanceof ControlSinkNode || current instanceof LoopEndNode || current.cfgSuccessors().iterator().hasNext() : "Must not reach a node that has not been decoded yet";
|
|
1587 |
|
|
1588 |
for (Node successor : current.cfgSuccessors()) {
|
|
1589 |
if (visited.isMarked(successor)) {
|
|
1590 |
/* Already processed this successor. */
|
|
1591 |
|
46344
|
1592 |
} else if (methodScope.loopExplosionMerges.contains(successor)) {
|
43972
|
1593 |
/*
|
|
1594 |
* We have a FrameState for the successor. The LoopExit will be inserted between
|
|
1595 |
* the current node and the successor node. Since the successor node is a
|
|
1596 |
* MergeNode, the current node mus be a AbstractEndNode with only that MergeNode
|
|
1597 |
* as the successor.
|
|
1598 |
*/
|
|
1599 |
assert successor instanceof MergeNode;
|
|
1600 |
assert !loop.exits.contains(current);
|
|
1601 |
loop.exits.add((AbstractEndNode) current);
|
|
1602 |
|
|
1603 |
} else {
|
|
1604 |
/* Node we have not seen yet. */
|
|
1605 |
visited.mark(successor);
|
|
1606 |
stack.push(successor);
|
|
1607 |
}
|
|
1608 |
}
|
|
1609 |
}
|
|
1610 |
}
|
|
1611 |
|
|
1612 |
private void insertLoopNodes(Loop loop) {
|
|
1613 |
MergeNode merge = loop.header;
|
|
1614 |
FrameState stateAfter = merge.stateAfter().duplicate();
|
|
1615 |
FixedNode afterMerge = merge.next();
|
|
1616 |
merge.setNext(null);
|
46371
|
1617 |
EndNode preLoopEnd = graph.add(new EndNode());
|
|
1618 |
LoopBeginNode loopBegin = graph.add(new LoopBeginNode());
|
43972
|
1619 |
|
|
1620 |
merge.setNext(preLoopEnd);
|
|
1621 |
/* Add the single non-loop predecessor of the loop header. */
|
|
1622 |
loopBegin.addForwardEnd(preLoopEnd);
|
|
1623 |
loopBegin.setNext(afterMerge);
|
|
1624 |
loopBegin.setStateAfter(stateAfter);
|
|
1625 |
|
|
1626 |
/*
|
|
1627 |
* Phi functions of the original merge need to be split: inputs that come from forward edges
|
|
1628 |
* remain with the original phi function; inputs that come from backward edges are added to
|
|
1629 |
* new phi functions.
|
|
1630 |
*/
|
|
1631 |
List<PhiNode> mergePhis = merge.phis().snapshot();
|
|
1632 |
List<PhiNode> loopBeginPhis = new ArrayList<>(mergePhis.size());
|
|
1633 |
for (int i = 0; i < mergePhis.size(); i++) {
|
|
1634 |
PhiNode mergePhi = mergePhis.get(i);
|
48190
|
1635 |
PhiNode loopBeginPhi = graph.addWithoutUnique(new ValuePhiNode(mergePhi.stamp(NodeView.DEFAULT), loopBegin));
|
43972
|
1636 |
mergePhi.replaceAtUsages(loopBeginPhi);
|
|
1637 |
/*
|
|
1638 |
* The first input of the new phi function is the original phi function, for the one
|
|
1639 |
* forward edge of the LoopBeginNode.
|
|
1640 |
*/
|
|
1641 |
loopBeginPhi.addInput(mergePhi);
|
|
1642 |
loopBeginPhis.add(loopBeginPhi);
|
|
1643 |
}
|
|
1644 |
|
|
1645 |
for (EndNode endNode : loop.ends) {
|
|
1646 |
for (int i = 0; i < mergePhis.size(); i++) {
|
|
1647 |
PhiNode mergePhi = mergePhis.get(i);
|
|
1648 |
PhiNode loopBeginPhi = loopBeginPhis.get(i);
|
|
1649 |
loopBeginPhi.addInput(mergePhi.valueAt(endNode));
|
|
1650 |
}
|
|
1651 |
|
|
1652 |
merge.removeEnd(endNode);
|
46371
|
1653 |
LoopEndNode loopEnd = graph.add(new LoopEndNode(loopBegin));
|
43972
|
1654 |
endNode.replaceAndDelete(loopEnd);
|
|
1655 |
}
|
|
1656 |
|
|
1657 |
/*
|
|
1658 |
* Insert the LoopExit nodes (the easy part) and compute the FrameState for the new exits
|
|
1659 |
* (the difficult part).
|
|
1660 |
*/
|
|
1661 |
for (AbstractEndNode exit : loop.exits) {
|
|
1662 |
AbstractMergeNode loopExplosionMerge = exit.merge();
|
46344
|
1663 |
assert methodScope.loopExplosionMerges.contains(loopExplosionMerge);
|
43972
|
1664 |
|
46371
|
1665 |
LoopExitNode loopExit = graph.add(new LoopExitNode(loopBegin));
|
43972
|
1666 |
exit.replaceAtPredecessor(loopExit);
|
|
1667 |
loopExit.setNext(exit);
|
|
1668 |
assignLoopExitState(loopExit, loopExplosionMerge, exit);
|
|
1669 |
}
|
|
1670 |
}
|
|
1671 |
|
|
1672 |
/**
|
|
1673 |
* During graph decoding, we create a FrameState for every exploded loop iteration. This is
|
|
1674 |
* mostly the state that we want, we only need to tweak it a little bit: we need to insert the
|
|
1675 |
* appropriate ProxyNodes for all values that are created inside the loop and that flow out of
|
|
1676 |
* the loop.
|
|
1677 |
*/
|
|
1678 |
private void assignLoopExitState(LoopExitNode loopExit, AbstractMergeNode loopExplosionMerge, AbstractEndNode loopExplosionEnd) {
|
|
1679 |
FrameState oldState = loopExplosionMerge.stateAfter();
|
|
1680 |
|
|
1681 |
/* Collect all nodes that are in the FrameState at the LoopBegin. */
|
46344
|
1682 |
EconomicSet<Node> loopBeginValues = EconomicSet.create(Equivalence.IDENTITY);
|
43972
|
1683 |
for (FrameState state = loopExit.loopBegin().stateAfter(); state != null; state = state.outerFrameState()) {
|
|
1684 |
for (ValueNode value : state.values()) {
|
|
1685 |
if (value != null && !value.isConstant() && !loopExit.loopBegin().isPhiAtMerge(value)) {
|
46344
|
1686 |
loopBeginValues.add(ProxyPlaceholder.unwrap(value));
|
43972
|
1687 |
}
|
|
1688 |
}
|
|
1689 |
}
|
|
1690 |
|
|
1691 |
List<ValueNode> newValues = new ArrayList<>(oldState.values().size());
|
|
1692 |
for (ValueNode v : oldState.values()) {
|
|
1693 |
ValueNode value = v;
|
|
1694 |
ValueNode realValue = ProxyPlaceholder.unwrap(value);
|
|
1695 |
|
|
1696 |
/*
|
|
1697 |
* The LoopExit is inserted before the existing merge, i.e., separately for every branch
|
|
1698 |
* that leads to the merge. So for phi functions of the merge, we need to take the input
|
|
1699 |
* that corresponds to our branch.
|
|
1700 |
*/
|
|
1701 |
if (realValue instanceof PhiNode && loopExplosionMerge.isPhiAtMerge(realValue)) {
|
|
1702 |
value = ((PhiNode) realValue).valueAt(loopExplosionEnd);
|
|
1703 |
realValue = ProxyPlaceholder.unwrap(value);
|
|
1704 |
}
|
|
1705 |
|
46371
|
1706 |
if (realValue == null || realValue.isConstant() || loopBeginValues.contains(realValue) || !graph.isNew(methodScope.methodStartMark, realValue)) {
|
43972
|
1707 |
newValues.add(realValue);
|
|
1708 |
} else {
|
|
1709 |
/*
|
|
1710 |
* The node is not in the FrameState of the LoopBegin, i.e., it is a value computed
|
|
1711 |
* inside the loop.
|
|
1712 |
*/
|
|
1713 |
GraalError.guarantee(value instanceof ProxyPlaceholder && ((ProxyPlaceholder) value).proxyPoint == loopExplosionMerge,
|
|
1714 |
"Value flowing out of loop, but we are not prepared to insert a ProxyNode");
|
|
1715 |
|
|
1716 |
ProxyPlaceholder proxyPlaceholder = (ProxyPlaceholder) value;
|
46371
|
1717 |
ValueProxyNode proxy = ProxyNode.forValue(proxyPlaceholder.value, loopExit, graph);
|
43972
|
1718 |
proxyPlaceholder.setValue(proxy);
|
|
1719 |
newValues.add(proxy);
|
|
1720 |
}
|
|
1721 |
}
|
|
1722 |
|
|
1723 |
FrameState newState = new FrameState(oldState.outerFrameState(), oldState.getCode(), oldState.bci, newValues, oldState.localsSize(), oldState.stackSize(), oldState.rethrowException(),
|
|
1724 |
oldState.duringCall(), oldState.monitorIds(), oldState.virtualObjectMappings());
|
|
1725 |
|
|
1726 |
assert loopExit.stateAfter() == null;
|
46371
|
1727 |
loopExit.setStateAfter(graph.add(newState));
|
43972
|
1728 |
}
|
|
1729 |
|
|
1730 |
/**
|
|
1731 |
* Graal does not support irreducible loops (loops with more than one entry point). There are
|
|
1732 |
* two ways to make them reducible: 1) duplicate nodes (peel a loop iteration starting at the
|
|
1733 |
* second entry point until we reach the first entry point), or 2) insert a big outer loop
|
|
1734 |
* covering the whole method and build a state machine for the different loop entry points.
|
|
1735 |
* Since node duplication can lead to an exponential explosion of nodes in the worst case, we
|
|
1736 |
* use the second approach.
|
|
1737 |
*
|
|
1738 |
* We already did some preparations to insert a big outer loop:
|
|
1739 |
* {@link MethodScope#loopExplosionHead} is the loop header for the outer loop, and we ensured
|
|
1740 |
* that we have a {@link Loop} data object for it in {@link #irreducibleLoopHandler}.
|
|
1741 |
*
|
|
1742 |
* Now we need to insert the state machine. We have several implementation restrictions to make
|
|
1743 |
* that efficient:
|
|
1744 |
* <ul>
|
|
1745 |
* <li>There must be only one loop variable, i.e., one value that is different in the
|
|
1746 |
* {@link FrameState} of the different loop headers.</li>
|
|
1747 |
* <li>The loop variable must use the primitive {@code int} type, because Graal only has a
|
|
1748 |
* {@link IntegerSwitchNode switch node} for {@code int}.</li>
|
|
1749 |
* <li>The values of the loop variable that are merged are {@link PrimitiveConstant compile time
|
|
1750 |
* constants}.</li>
|
|
1751 |
* </ul>
|
|
1752 |
*/
|
|
1753 |
private void handleIrreducibleLoop(Loop loop) {
|
|
1754 |
assert loop != irreducibleLoopHandler;
|
|
1755 |
|
|
1756 |
FrameState loopState = loop.header.stateAfter();
|
|
1757 |
FrameState explosionHeadState = irreducibleLoopHandler.header.stateAfter();
|
|
1758 |
assert loopState.outerFrameState() == explosionHeadState.outerFrameState();
|
|
1759 |
NodeInputList<ValueNode> loopValues = loopState.values();
|
|
1760 |
NodeInputList<ValueNode> explosionHeadValues = explosionHeadState.values();
|
|
1761 |
assert loopValues.size() == explosionHeadValues.size();
|
|
1762 |
|
|
1763 |
/*
|
|
1764 |
* Find the loop variable, and the value of the loop variable for our loop and the outermost
|
|
1765 |
* loop. There must be exactly one loop variable.
|
|
1766 |
*/
|
|
1767 |
int loopVariableIndex = -1;
|
|
1768 |
ValueNode loopValue = null;
|
|
1769 |
ValueNode explosionHeadValue = null;
|
|
1770 |
for (int i = 0; i < loopValues.size(); i++) {
|
|
1771 |
ValueNode curLoopValue = loopValues.get(i);
|
|
1772 |
ValueNode curExplosionHeadValue = explosionHeadValues.get(i);
|
|
1773 |
|
|
1774 |
if (curLoopValue != curExplosionHeadValue) {
|
|
1775 |
if (loopVariableIndex != -1) {
|
|
1776 |
throw bailout("must have only one variable that is changed in loop. " + loopValue + " != " + explosionHeadValue + " and " + curLoopValue + " != " + curExplosionHeadValue);
|
|
1777 |
}
|
|
1778 |
|
|
1779 |
loopVariableIndex = i;
|
|
1780 |
loopValue = curLoopValue;
|
|
1781 |
explosionHeadValue = curExplosionHeadValue;
|
|
1782 |
}
|
|
1783 |
}
|
|
1784 |
assert loopVariableIndex != -1;
|
|
1785 |
|
|
1786 |
ValuePhiNode loopVariablePhi;
|
|
1787 |
SortedMap<Integer, AbstractBeginNode> dispatchTable = new TreeMap<>();
|
|
1788 |
AbstractBeginNode unreachableDefaultSuccessor;
|
|
1789 |
if (irreducibleLoopSwitch == null) {
|
|
1790 |
/*
|
|
1791 |
* This is the first irreducible loop. We need to build the initial state machine
|
|
1792 |
* (dispatch for the loop header of the outermost loop).
|
|
1793 |
*/
|
|
1794 |
assert !irreducibleLoopHandler.header.isPhiAtMerge(explosionHeadValue);
|
|
1795 |
assert irreducibleLoopHandler.header.phis().isEmpty();
|
|
1796 |
|
|
1797 |
/* The new phi function for the loop variable. */
|
48190
|
1798 |
loopVariablePhi = graph.addWithoutUnique(new ValuePhiNode(explosionHeadValue.stamp(NodeView.DEFAULT).unrestricted(), irreducibleLoopHandler.header));
|
43972
|
1799 |
for (int i = 0; i < irreducibleLoopHandler.header.phiPredecessorCount(); i++) {
|
|
1800 |
loopVariablePhi.addInput(explosionHeadValue);
|
|
1801 |
}
|
|
1802 |
|
|
1803 |
/*
|
|
1804 |
* Build the new FrameState for the loop header. There is only once change in comparison
|
|
1805 |
* to the old FrameState: the loop variable is replaced with the phi function.
|
|
1806 |
*/
|
|
1807 |
FrameState oldFrameState = explosionHeadState;
|
46371
|
1808 |
List<ValueNode> newFrameStateValues = new ArrayList<>(explosionHeadValues.size());
|
43972
|
1809 |
for (int i = 0; i < explosionHeadValues.size(); i++) {
|
|
1810 |
if (i == loopVariableIndex) {
|
|
1811 |
newFrameStateValues.add(loopVariablePhi);
|
|
1812 |
} else {
|
|
1813 |
newFrameStateValues.add(explosionHeadValues.get(i));
|
|
1814 |
}
|
|
1815 |
}
|
46371
|
1816 |
|
|
1817 |
FrameState newFrameState = graph.add(
|
43972
|
1818 |
new FrameState(oldFrameState.outerFrameState(), oldFrameState.getCode(), oldFrameState.bci, newFrameStateValues, oldFrameState.localsSize(),
|
|
1819 |
oldFrameState.stackSize(), oldFrameState.rethrowException(), oldFrameState.duringCall(), oldFrameState.monitorIds(),
|
|
1820 |
oldFrameState.virtualObjectMappings()));
|
|
1821 |
oldFrameState.replaceAtUsages(newFrameState);
|
|
1822 |
|
|
1823 |
/*
|
|
1824 |
* Disconnect the outermost loop header from its loop body, so that we can later on
|
|
1825 |
* insert the switch node. Collect dispatch information for the outermost loop.
|
|
1826 |
*/
|
|
1827 |
FixedNode handlerNext = irreducibleLoopHandler.header.next();
|
|
1828 |
irreducibleLoopHandler.header.setNext(null);
|
46371
|
1829 |
BeginNode handlerBegin = graph.add(new BeginNode());
|
43972
|
1830 |
handlerBegin.setNext(handlerNext);
|
|
1831 |
dispatchTable.put(asInt(explosionHeadValue), handlerBegin);
|
|
1832 |
|
|
1833 |
/*
|
|
1834 |
* We know that there will always be a matching key in the switch. But Graal always
|
|
1835 |
* wants a default successor, so we build a dummy block that just deoptimizes.
|
|
1836 |
*/
|
46371
|
1837 |
unreachableDefaultSuccessor = graph.add(new BeginNode());
|
|
1838 |
DeoptimizeNode deopt = graph.add(new DeoptimizeNode(DeoptimizationAction.InvalidateRecompile, DeoptimizationReason.UnreachedCode));
|
43972
|
1839 |
unreachableDefaultSuccessor.setNext(deopt);
|
|
1840 |
|
|
1841 |
} else {
|
|
1842 |
/*
|
|
1843 |
* This is the second or a subsequent irreducible loop, i.e., we already inserted a
|
|
1844 |
* switch node before. We re-create the dispatch state machine of that switch, so that
|
|
1845 |
* we can extend it with one more branch.
|
|
1846 |
*/
|
|
1847 |
assert irreducibleLoopHandler.header.isPhiAtMerge(explosionHeadValue);
|
|
1848 |
assert irreducibleLoopHandler.header.phis().count() == 1 && irreducibleLoopHandler.header.phis().first() == explosionHeadValue;
|
|
1849 |
assert irreducibleLoopSwitch.value() == explosionHeadValue;
|
|
1850 |
|
|
1851 |
/* We can modify the phi function used by the old switch node. */
|
|
1852 |
loopVariablePhi = (ValuePhiNode) explosionHeadValue;
|
|
1853 |
|
|
1854 |
/*
|
|
1855 |
* We cannot modify the old switch node. Insert all information from the old switch node
|
|
1856 |
* into our temporary data structures for the new, larger, switch node.
|
|
1857 |
*/
|
|
1858 |
for (int i = 0; i < irreducibleLoopSwitch.keyCount(); i++) {
|
|
1859 |
int key = irreducibleLoopSwitch.keyAt(i).asInt();
|
|
1860 |
dispatchTable.put(key, irreducibleLoopSwitch.successorAtKey(key));
|
|
1861 |
}
|
|
1862 |
unreachableDefaultSuccessor = irreducibleLoopSwitch.defaultSuccessor();
|
|
1863 |
|
|
1864 |
/* Unlink and delete the old switch node, we do not need it anymore. */
|
|
1865 |
assert irreducibleLoopHandler.header.next() == irreducibleLoopSwitch;
|
|
1866 |
irreducibleLoopHandler.header.setNext(null);
|
|
1867 |
irreducibleLoopSwitch.clearSuccessors();
|
|
1868 |
irreducibleLoopSwitch.safeDelete();
|
|
1869 |
}
|
|
1870 |
|
|
1871 |
/* Insert our loop into the dispatch state machine. */
|
|
1872 |
assert loop.header.phis().isEmpty();
|
46371
|
1873 |
BeginNode dispatchBegin = graph.add(new BeginNode());
|
|
1874 |
EndNode dispatchEnd = graph.add(new EndNode());
|
43972
|
1875 |
dispatchBegin.setNext(dispatchEnd);
|
|
1876 |
loop.header.addForwardEnd(dispatchEnd);
|
|
1877 |
int intLoopValue = asInt(loopValue);
|
|
1878 |
assert !dispatchTable.containsKey(intLoopValue);
|
|
1879 |
dispatchTable.put(intLoopValue, dispatchBegin);
|
|
1880 |
|
|
1881 |
/* Disconnect the ends of our loop and re-connect them to the outermost loop header. */
|
|
1882 |
for (EndNode end : loop.ends) {
|
|
1883 |
loop.header.removeEnd(end);
|
|
1884 |
irreducibleLoopHandler.ends.add(end);
|
|
1885 |
irreducibleLoopHandler.header.addForwardEnd(end);
|
|
1886 |
loopVariablePhi.addInput(loopValue);
|
|
1887 |
}
|
|
1888 |
|
|
1889 |
/* Build and insert the switch node. */
|
46371
|
1890 |
irreducibleLoopSwitch = graph.add(createSwitch(loopVariablePhi, dispatchTable, unreachableDefaultSuccessor));
|
43972
|
1891 |
irreducibleLoopHandler.header.setNext(irreducibleLoopSwitch);
|
|
1892 |
}
|
|
1893 |
|
|
1894 |
private static int asInt(ValueNode node) {
|
|
1895 |
if (!node.isConstant() || node.asJavaConstant().getJavaKind() != JavaKind.Int) {
|
|
1896 |
throw bailout("must have a loop variable of type int. " + node);
|
|
1897 |
}
|
|
1898 |
return node.asJavaConstant().asInt();
|
|
1899 |
}
|
|
1900 |
|
|
1901 |
private static RuntimeException bailout(String msg) {
|
|
1902 |
throw new PermanentBailoutException("Graal implementation restriction: Method with %s loop explosion %s", LoopExplosionKind.MERGE_EXPLODE, msg);
|
|
1903 |
}
|
|
1904 |
|
|
1905 |
private static IntegerSwitchNode createSwitch(ValuePhiNode switchedValue, SortedMap<Integer, AbstractBeginNode> dispatchTable, AbstractBeginNode defaultSuccessor) {
|
|
1906 |
int numKeys = dispatchTable.size();
|
|
1907 |
int numSuccessors = numKeys + 1;
|
|
1908 |
|
|
1909 |
AbstractBeginNode[] switchSuccessors = new AbstractBeginNode[numSuccessors];
|
|
1910 |
int[] switchKeys = new int[numKeys];
|
|
1911 |
double[] switchKeyProbabilities = new double[numSuccessors];
|
|
1912 |
int[] switchKeySuccessors = new int[numSuccessors];
|
|
1913 |
|
|
1914 |
int idx = 0;
|
|
1915 |
for (Map.Entry<Integer, AbstractBeginNode> entry : dispatchTable.entrySet()) {
|
|
1916 |
switchSuccessors[idx] = entry.getValue();
|
|
1917 |
switchKeys[idx] = entry.getKey();
|
|
1918 |
switchKeyProbabilities[idx] = 1d / numKeys;
|
|
1919 |
switchKeySuccessors[idx] = idx;
|
|
1920 |
idx++;
|
|
1921 |
}
|
|
1922 |
switchSuccessors[idx] = defaultSuccessor;
|
|
1923 |
/* We know the default branch is never going to be executed. */
|
|
1924 |
switchKeyProbabilities[idx] = 0;
|
|
1925 |
switchKeySuccessors[idx] = idx;
|
|
1926 |
|
|
1927 |
return new IntegerSwitchNode(switchedValue, switchSuccessors, switchKeys, switchKeyProbabilities, switchKeySuccessors);
|
|
1928 |
}
|
|
1929 |
|
|
1930 |
/**
|
|
1931 |
* Print information about irreducible loops, when enabled with -Dgraal.Log=IrreducibleLoops.
|
|
1932 |
*/
|
|
1933 |
@SuppressWarnings("try")
|
|
1934 |
private void logIrreducibleLoops() {
|
46640
|
1935 |
DebugContext debug = graph.getDebug();
|
|
1936 |
try (DebugContext.Scope s = debug.scope("IrreducibleLoops")) {
|
|
1937 |
if (debug.isLogEnabled(DebugContext.BASIC_LEVEL) && irreducibleLoopSwitch != null) {
|
43972
|
1938 |
StringBuilder msg = new StringBuilder("Inserted state machine to remove irreducible loops. Dispatching to the following states: ");
|
|
1939 |
String sep = "";
|
|
1940 |
for (int i = 0; i < irreducibleLoopSwitch.keyCount(); i++) {
|
|
1941 |
msg.append(sep).append(irreducibleLoopSwitch.keyAt(i).asInt());
|
|
1942 |
sep = ", ";
|
|
1943 |
}
|
46640
|
1944 |
debug.log(DebugContext.BASIC_LEVEL, "%s", msg);
|
43972
|
1945 |
}
|
|
1946 |
}
|
|
1947 |
}
|
|
1948 |
}
|