43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.loop;
|
|
24 |
|
|
25 |
import java.util.LinkedList;
|
|
26 |
import java.util.List;
|
|
27 |
|
|
28 |
import org.graalvm.compiler.debug.GraalError;
|
|
29 |
import org.graalvm.compiler.graph.Graph.DuplicationReplacement;
|
|
30 |
import org.graalvm.compiler.graph.Node;
|
|
31 |
import org.graalvm.compiler.graph.NodeBitMap;
|
|
32 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
|
33 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
34 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
35 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
36 |
import org.graalvm.compiler.nodes.BeginNode;
|
|
37 |
import org.graalvm.compiler.nodes.EndNode;
|
|
38 |
import org.graalvm.compiler.nodes.FrameState;
|
|
39 |
import org.graalvm.compiler.nodes.GuardPhiNode;
|
|
40 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
41 |
import org.graalvm.compiler.nodes.LoopEndNode;
|
|
42 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
43 |
import org.graalvm.compiler.nodes.MergeNode;
|
|
44 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
45 |
import org.graalvm.compiler.nodes.ProxyNode;
|
|
46 |
import org.graalvm.compiler.nodes.StateSplit;
|
|
47 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
48 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
49 |
import org.graalvm.compiler.nodes.ValuePhiNode;
|
|
50 |
import org.graalvm.compiler.nodes.VirtualState.NodeClosure;
|
|
51 |
import org.graalvm.compiler.nodes.memory.MemoryPhiNode;
|
|
52 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
46640
|
53 |
import org.graalvm.util.EconomicMap;
|
46344
|
54 |
import org.graalvm.util.Equivalence;
|
43972
|
55 |
|
|
56 |
public class LoopFragmentInside extends LoopFragment {
|
|
57 |
|
|
58 |
/**
|
|
59 |
* mergedInitializers. When an inside fragment's (loop)ends are merged to create a unique exit
|
|
60 |
* point, some phis must be created : they phis together all the back-values of the loop-phis
|
|
61 |
* These can then be used to update the loop-phis' forward edge value ('initializer') in the
|
|
62 |
* peeling case. In the unrolling case they will be used as the value that replace the loop-phis
|
|
63 |
* of the duplicated inside fragment
|
|
64 |
*/
|
46344
|
65 |
private EconomicMap<PhiNode, ValueNode> mergedInitializers;
|
43972
|
66 |
private final DuplicationReplacement dataFixBefore = new DuplicationReplacement() {
|
|
67 |
|
|
68 |
@Override
|
|
69 |
public Node replacement(Node oriInput) {
|
|
70 |
if (!(oriInput instanceof ValueNode)) {
|
|
71 |
return oriInput;
|
|
72 |
}
|
|
73 |
return prim((ValueNode) oriInput);
|
|
74 |
}
|
|
75 |
};
|
|
76 |
|
46640
|
77 |
private final DuplicationReplacement dataFixWithinAfter = new DuplicationReplacement() {
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public Node replacement(Node oriInput) {
|
|
81 |
if (!(oriInput instanceof ValueNode)) {
|
|
82 |
return oriInput;
|
|
83 |
}
|
|
84 |
return primAfter((ValueNode) oriInput);
|
|
85 |
}
|
|
86 |
};
|
|
87 |
|
43972
|
88 |
public LoopFragmentInside(LoopEx loop) {
|
|
89 |
super(loop);
|
|
90 |
}
|
|
91 |
|
|
92 |
public LoopFragmentInside(LoopFragmentInside original) {
|
|
93 |
super(null, original);
|
|
94 |
}
|
|
95 |
|
|
96 |
@Override
|
|
97 |
public LoopFragmentInside duplicate() {
|
|
98 |
assert !isDuplicate();
|
|
99 |
return new LoopFragmentInside(this);
|
|
100 |
}
|
|
101 |
|
|
102 |
@Override
|
|
103 |
public LoopFragmentInside original() {
|
|
104 |
return (LoopFragmentInside) super.original();
|
|
105 |
}
|
|
106 |
|
|
107 |
@SuppressWarnings("unused")
|
|
108 |
public void appendInside(LoopEx loop) {
|
|
109 |
// TODO (gd)
|
|
110 |
}
|
|
111 |
|
|
112 |
@Override
|
|
113 |
public LoopEx loop() {
|
|
114 |
assert !this.isDuplicate();
|
|
115 |
return super.loop();
|
|
116 |
}
|
|
117 |
|
|
118 |
@Override
|
|
119 |
public void insertBefore(LoopEx loop) {
|
|
120 |
assert this.isDuplicate() && this.original().loop() == loop;
|
|
121 |
|
|
122 |
patchNodes(dataFixBefore);
|
|
123 |
|
|
124 |
AbstractBeginNode end = mergeEnds();
|
|
125 |
|
|
126 |
mergeEarlyExits();
|
|
127 |
|
|
128 |
original().patchPeeling(this);
|
|
129 |
|
|
130 |
AbstractBeginNode entry = getDuplicatedNode(loop.loopBegin());
|
|
131 |
loop.entryPoint().replaceAtPredecessor(entry);
|
|
132 |
end.setNext(loop.entryPoint());
|
|
133 |
}
|
|
134 |
|
46640
|
135 |
public void insertWithinAfter(LoopEx loop) {
|
|
136 |
assert this.isDuplicate() && this.original().loop() == loop;
|
|
137 |
|
|
138 |
patchNodes(dataFixWithinAfter);
|
|
139 |
}
|
|
140 |
|
43972
|
141 |
@Override
|
|
142 |
public NodeBitMap nodes() {
|
|
143 |
if (nodes == null) {
|
|
144 |
LoopFragmentWhole whole = loop().whole();
|
|
145 |
whole.nodes(); // init nodes bitmap in whole
|
|
146 |
nodes = whole.nodes.copy();
|
|
147 |
// remove the phis
|
|
148 |
LoopBeginNode loopBegin = loop().loopBegin();
|
|
149 |
for (PhiNode phi : loopBegin.phis()) {
|
|
150 |
nodes.clear(phi);
|
|
151 |
}
|
|
152 |
clearStateNodes(loopBegin);
|
|
153 |
for (LoopExitNode exit : exits()) {
|
|
154 |
clearStateNodes(exit);
|
|
155 |
for (ProxyNode proxy : exit.proxies()) {
|
|
156 |
nodes.clear(proxy);
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
return nodes;
|
|
161 |
}
|
|
162 |
|
|
163 |
private void clearStateNodes(StateSplit stateSplit) {
|
|
164 |
FrameState loopState = stateSplit.stateAfter();
|
|
165 |
if (loopState != null) {
|
|
166 |
loopState.applyToVirtual(v -> {
|
|
167 |
if (v.usages().filter(n -> nodes.isMarked(n) && n != stateSplit).isEmpty()) {
|
|
168 |
nodes.clear(v);
|
|
169 |
}
|
|
170 |
});
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
public NodeIterable<LoopExitNode> exits() {
|
|
175 |
return loop().loopBegin().loopExits();
|
|
176 |
}
|
|
177 |
|
|
178 |
@Override
|
|
179 |
protected DuplicationReplacement getDuplicationReplacement() {
|
|
180 |
final LoopBeginNode loopBegin = loop().loopBegin();
|
|
181 |
final StructuredGraph graph = graph();
|
|
182 |
return new DuplicationReplacement() {
|
|
183 |
|
46344
|
184 |
private EconomicMap<Node, Node> seenNode = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
185 |
|
|
186 |
@Override
|
|
187 |
public Node replacement(Node original) {
|
|
188 |
if (original == loopBegin) {
|
|
189 |
Node value = seenNode.get(original);
|
|
190 |
if (value != null) {
|
|
191 |
return value;
|
|
192 |
}
|
|
193 |
AbstractBeginNode newValue = graph.add(new BeginNode());
|
|
194 |
seenNode.put(original, newValue);
|
|
195 |
return newValue;
|
|
196 |
}
|
|
197 |
if (original instanceof LoopExitNode && ((LoopExitNode) original).loopBegin() == loopBegin) {
|
|
198 |
Node value = seenNode.get(original);
|
|
199 |
if (value != null) {
|
|
200 |
return value;
|
|
201 |
}
|
|
202 |
AbstractBeginNode newValue = graph.add(new BeginNode());
|
|
203 |
seenNode.put(original, newValue);
|
|
204 |
return newValue;
|
|
205 |
}
|
|
206 |
if (original instanceof LoopEndNode && ((LoopEndNode) original).loopBegin() == loopBegin) {
|
|
207 |
Node value = seenNode.get(original);
|
|
208 |
if (value != null) {
|
|
209 |
return value;
|
|
210 |
}
|
|
211 |
EndNode newValue = graph.add(new EndNode());
|
|
212 |
seenNode.put(original, newValue);
|
|
213 |
return newValue;
|
|
214 |
}
|
|
215 |
return original;
|
|
216 |
}
|
|
217 |
};
|
|
218 |
}
|
|
219 |
|
|
220 |
@Override
|
|
221 |
protected void finishDuplication() {
|
|
222 |
// TODO (gd) ?
|
|
223 |
}
|
|
224 |
|
46640
|
225 |
@Override
|
|
226 |
protected void beforeDuplication() {
|
|
227 |
// Nothing to do
|
|
228 |
}
|
|
229 |
|
43972
|
230 |
private static PhiNode patchPhi(StructuredGraph graph, PhiNode phi, AbstractMergeNode merge) {
|
|
231 |
PhiNode ret;
|
|
232 |
if (phi instanceof ValuePhiNode) {
|
|
233 |
ret = new ValuePhiNode(phi.stamp(), merge);
|
|
234 |
} else if (phi instanceof GuardPhiNode) {
|
|
235 |
ret = new GuardPhiNode(merge);
|
|
236 |
} else if (phi instanceof MemoryPhiNode) {
|
|
237 |
ret = new MemoryPhiNode(merge, ((MemoryPhiNode) phi).getLocationIdentity());
|
|
238 |
} else {
|
|
239 |
throw GraalError.shouldNotReachHere();
|
|
240 |
}
|
|
241 |
return graph.addWithoutUnique(ret);
|
|
242 |
}
|
|
243 |
|
|
244 |
private void patchPeeling(LoopFragmentInside peel) {
|
|
245 |
LoopBeginNode loopBegin = loop().loopBegin();
|
|
246 |
StructuredGraph graph = loopBegin.graph();
|
|
247 |
List<PhiNode> newPhis = new LinkedList<>();
|
|
248 |
|
|
249 |
NodeBitMap usagesToPatch = nodes.copy();
|
|
250 |
for (LoopExitNode exit : exits()) {
|
|
251 |
markStateNodes(exit, usagesToPatch);
|
|
252 |
for (ProxyNode proxy : exit.proxies()) {
|
|
253 |
usagesToPatch.markAndGrow(proxy);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
markStateNodes(loopBegin, usagesToPatch);
|
|
257 |
|
|
258 |
List<PhiNode> oldPhis = loopBegin.phis().snapshot();
|
|
259 |
for (PhiNode phi : oldPhis) {
|
|
260 |
if (phi.hasNoUsages()) {
|
|
261 |
continue;
|
|
262 |
}
|
|
263 |
ValueNode first;
|
|
264 |
if (loopBegin.loopEnds().count() == 1) {
|
|
265 |
ValueNode b = phi.valueAt(loopBegin.loopEnds().first()); // back edge value
|
|
266 |
first = peel.prim(b); // corresponding value in the peel
|
|
267 |
} else {
|
|
268 |
first = peel.mergedInitializers.get(phi);
|
|
269 |
}
|
|
270 |
// create a new phi (we don't patch the old one since some usages of the old one may
|
|
271 |
// still be valid)
|
|
272 |
PhiNode newPhi = patchPhi(graph, phi, loopBegin);
|
|
273 |
newPhi.addInput(first);
|
|
274 |
for (LoopEndNode end : loopBegin.orderedLoopEnds()) {
|
|
275 |
newPhi.addInput(phi.valueAt(end));
|
|
276 |
}
|
|
277 |
peel.putDuplicatedNode(phi, newPhi);
|
|
278 |
newPhis.add(newPhi);
|
|
279 |
for (Node usage : phi.usages().snapshot()) {
|
|
280 |
// patch only usages that should use the new phi ie usages that were peeled
|
|
281 |
if (usagesToPatch.isMarkedAndGrow(usage)) {
|
|
282 |
usage.replaceFirstInput(phi, newPhi);
|
|
283 |
}
|
|
284 |
}
|
|
285 |
}
|
|
286 |
// check new phis to see if they have as input some old phis, replace those inputs with the
|
|
287 |
// new corresponding phis
|
|
288 |
for (PhiNode phi : newPhis) {
|
|
289 |
for (int i = 0; i < phi.valueCount(); i++) {
|
|
290 |
ValueNode v = phi.valueAt(i);
|
|
291 |
if (loopBegin.isPhiAtMerge(v)) {
|
|
292 |
PhiNode newV = peel.getDuplicatedNode((ValuePhiNode) v);
|
|
293 |
if (newV != null) {
|
|
294 |
phi.setValueAt(i, newV);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
boolean progress = true;
|
|
301 |
while (progress) {
|
|
302 |
progress = false;
|
|
303 |
int i = 0;
|
|
304 |
outer: while (i < oldPhis.size()) {
|
|
305 |
PhiNode oldPhi = oldPhis.get(i);
|
|
306 |
for (Node usage : oldPhi.usages()) {
|
|
307 |
if (usage instanceof PhiNode && oldPhis.contains(usage)) {
|
|
308 |
// Do not mark.
|
|
309 |
} else {
|
|
310 |
// Mark alive by removing from delete set.
|
|
311 |
oldPhis.remove(i);
|
|
312 |
progress = true;
|
|
313 |
continue outer;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
i++;
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
for (PhiNode deadPhi : oldPhis) {
|
|
321 |
deadPhi.clearInputs();
|
|
322 |
}
|
|
323 |
|
|
324 |
for (PhiNode deadPhi : oldPhis) {
|
|
325 |
if (deadPhi.isAlive()) {
|
|
326 |
GraphUtil.killWithUnusedFloatingInputs(deadPhi);
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
private static void markStateNodes(StateSplit stateSplit, NodeBitMap marks) {
|
|
332 |
FrameState exitState = stateSplit.stateAfter();
|
|
333 |
if (exitState != null) {
|
|
334 |
exitState.applyToVirtual(v -> marks.markAndGrow(v));
|
|
335 |
}
|
|
336 |
}
|
|
337 |
|
|
338 |
/**
|
|
339 |
* Gets the corresponding value in this fragment.
|
|
340 |
*
|
|
341 |
* @param b original value
|
|
342 |
* @return corresponding value in the peel
|
|
343 |
*/
|
|
344 |
@Override
|
|
345 |
protected ValueNode prim(ValueNode b) {
|
|
346 |
assert isDuplicate();
|
|
347 |
LoopBeginNode loopBegin = original().loop().loopBegin();
|
|
348 |
if (loopBegin.isPhiAtMerge(b)) {
|
|
349 |
PhiNode phi = (PhiNode) b;
|
|
350 |
return phi.valueAt(loopBegin.forwardEnd());
|
|
351 |
} else if (nodesReady) {
|
|
352 |
ValueNode v = getDuplicatedNode(b);
|
|
353 |
if (v == null) {
|
|
354 |
return b;
|
|
355 |
}
|
|
356 |
return v;
|
|
357 |
} else {
|
|
358 |
return b;
|
|
359 |
}
|
|
360 |
}
|
|
361 |
|
46640
|
362 |
protected ValueNode primAfter(ValueNode b) {
|
|
363 |
assert isDuplicate();
|
|
364 |
LoopBeginNode loopBegin = original().loop().loopBegin();
|
|
365 |
if (loopBegin.isPhiAtMerge(b)) {
|
|
366 |
PhiNode phi = (PhiNode) b;
|
|
367 |
assert phi.valueCount() == 2;
|
|
368 |
return phi.valueAt(1);
|
|
369 |
} else if (nodesReady) {
|
|
370 |
ValueNode v = getDuplicatedNode(b);
|
|
371 |
if (v == null) {
|
|
372 |
return b;
|
|
373 |
}
|
|
374 |
return v;
|
|
375 |
} else {
|
|
376 |
return b;
|
|
377 |
}
|
|
378 |
}
|
|
379 |
|
43972
|
380 |
private AbstractBeginNode mergeEnds() {
|
|
381 |
assert isDuplicate();
|
|
382 |
List<EndNode> endsToMerge = new LinkedList<>();
|
|
383 |
// map peel exits to the corresponding loop exits
|
46344
|
384 |
EconomicMap<AbstractEndNode, LoopEndNode> reverseEnds = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
385 |
LoopBeginNode loopBegin = original().loop().loopBegin();
|
|
386 |
for (LoopEndNode le : loopBegin.loopEnds()) {
|
|
387 |
AbstractEndNode duplicate = getDuplicatedNode(le);
|
|
388 |
if (duplicate != null) {
|
|
389 |
endsToMerge.add((EndNode) duplicate);
|
|
390 |
reverseEnds.put(duplicate, le);
|
|
391 |
}
|
|
392 |
}
|
46344
|
393 |
mergedInitializers = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
394 |
AbstractBeginNode newExit;
|
|
395 |
StructuredGraph graph = graph();
|
|
396 |
if (endsToMerge.size() == 1) {
|
|
397 |
AbstractEndNode end = endsToMerge.get(0);
|
|
398 |
assert end.hasNoUsages();
|
|
399 |
newExit = graph.add(new BeginNode());
|
|
400 |
end.replaceAtPredecessor(newExit);
|
|
401 |
end.safeDelete();
|
|
402 |
} else {
|
|
403 |
assert endsToMerge.size() > 1;
|
|
404 |
AbstractMergeNode newExitMerge = graph.add(new MergeNode());
|
|
405 |
newExit = newExitMerge;
|
|
406 |
FrameState state = loopBegin.stateAfter();
|
|
407 |
FrameState duplicateState = null;
|
|
408 |
if (state != null) {
|
|
409 |
duplicateState = state.duplicateWithVirtualState();
|
|
410 |
newExitMerge.setStateAfter(duplicateState);
|
|
411 |
}
|
|
412 |
for (EndNode end : endsToMerge) {
|
|
413 |
newExitMerge.addForwardEnd(end);
|
|
414 |
}
|
|
415 |
|
|
416 |
for (final PhiNode phi : loopBegin.phis().snapshot()) {
|
|
417 |
if (phi.hasNoUsages()) {
|
|
418 |
continue;
|
|
419 |
}
|
|
420 |
final PhiNode firstPhi = patchPhi(graph, phi, newExitMerge);
|
|
421 |
for (AbstractEndNode end : newExitMerge.forwardEnds()) {
|
|
422 |
LoopEndNode loopEnd = reverseEnds.get(end);
|
|
423 |
ValueNode prim = prim(phi.valueAt(loopEnd));
|
|
424 |
assert prim != null;
|
|
425 |
firstPhi.addInput(prim);
|
|
426 |
}
|
|
427 |
ValueNode initializer = firstPhi;
|
|
428 |
if (duplicateState != null) {
|
|
429 |
// fix the merge's state after
|
|
430 |
duplicateState.applyToNonVirtual(new NodeClosure<ValueNode>() {
|
|
431 |
|
|
432 |
@Override
|
|
433 |
public void apply(Node from, ValueNode node) {
|
|
434 |
if (node == phi) {
|
|
435 |
from.replaceFirstInput(phi, firstPhi);
|
|
436 |
}
|
|
437 |
}
|
|
438 |
});
|
|
439 |
}
|
46344
|
440 |
mergedInitializers.put(phi, initializer);
|
43972
|
441 |
}
|
|
442 |
}
|
|
443 |
return newExit;
|
|
444 |
}
|
|
445 |
}
|