43972
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
|
43972
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.loop.phases;
|
|
26 |
|
46807
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.MaximumDesiredSize;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Iterator;
|
|
31 |
import java.util.List;
|
|
32 |
|
52578
|
33 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
46640
|
34 |
import org.graalvm.compiler.core.common.RetryableBailoutException;
|
48861
|
35 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
46640
|
36 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
37 |
import org.graalvm.compiler.graph.Graph.Mark;
|
46640
|
38 |
import org.graalvm.compiler.graph.Node;
|
43972
|
39 |
import org.graalvm.compiler.graph.Position;
|
46640
|
40 |
import org.graalvm.compiler.loop.CountedLoopInfo;
|
|
41 |
import org.graalvm.compiler.loop.InductionVariable.Direction;
|
43972
|
42 |
import org.graalvm.compiler.loop.LoopEx;
|
46640
|
43 |
import org.graalvm.compiler.loop.LoopFragmentInside;
|
43972
|
44 |
import org.graalvm.compiler.loop.LoopFragmentWhole;
|
|
45 |
import org.graalvm.compiler.nodeinfo.InputType;
|
|
46 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
46640
|
47 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
48 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
43972
|
49 |
import org.graalvm.compiler.nodes.BeginNode;
|
|
50 |
import org.graalvm.compiler.nodes.ControlSplitNode;
|
46640
|
51 |
import org.graalvm.compiler.nodes.EndNode;
|
|
52 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
53 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
43972
|
54 |
import org.graalvm.compiler.nodes.IfNode;
|
46640
|
55 |
import org.graalvm.compiler.nodes.LogicNode;
|
43972
|
56 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
52578
|
57 |
import org.graalvm.compiler.nodes.NodeView;
|
46640
|
58 |
import org.graalvm.compiler.nodes.PhiNode;
|
46762
|
59 |
import org.graalvm.compiler.nodes.SafepointNode;
|
43972
|
60 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
61 |
import org.graalvm.compiler.nodes.ValueNode;
|
52578
|
62 |
import org.graalvm.compiler.nodes.calc.AddNode;
|
46640
|
63 |
import org.graalvm.compiler.nodes.calc.CompareNode;
|
|
64 |
import org.graalvm.compiler.nodes.calc.ConditionalNode;
|
52578
|
65 |
import org.graalvm.compiler.nodes.extended.OpaqueNode;
|
43972
|
66 |
import org.graalvm.compiler.nodes.extended.SwitchNode;
|
55509
|
67 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
58299
|
68 |
import org.graalvm.compiler.nodes.util.IntegerHelper;
|
43972
|
69 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
70 |
|
|
71 |
public abstract class LoopTransformations {
|
|
72 |
|
|
73 |
private LoopTransformations() {
|
|
74 |
// does not need to be instantiated
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void peel(LoopEx loop) {
|
|
78 |
loop.inside().duplicate().insertBefore(loop);
|
|
79 |
loop.loopBegin().setLoopFrequency(Math.max(0.0, loop.loopBegin().loopFrequency() - 1));
|
|
80 |
}
|
|
81 |
|
55509
|
82 |
public static void fullUnroll(LoopEx loop, CoreProviders context, CanonicalizerPhase canonicalizer) {
|
46640
|
83 |
// assert loop.isCounted(); //TODO (gd) strengthen : counted with known trip count
|
43972
|
84 |
LoopBeginNode loopBegin = loop.loopBegin();
|
|
85 |
StructuredGraph graph = loopBegin.graph();
|
|
86 |
int initialNodeCount = graph.getNodeCount();
|
|
87 |
while (!loopBegin.isDeleted()) {
|
|
88 |
Mark mark = graph.getMark();
|
|
89 |
peel(loop);
|
|
90 |
canonicalizer.applyIncremental(graph, context, mark);
|
|
91 |
loop.invalidateFragments();
|
46344
|
92 |
if (graph.getNodeCount() > initialNodeCount + MaximumDesiredSize.getValue(graph.getOptions()) * 2) {
|
43972
|
93 |
throw new RetryableBailoutException("FullUnroll : Graph seems to grow out of proportion");
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
public static void unswitch(LoopEx loop, List<ControlSplitNode> controlSplitNodeSet) {
|
|
99 |
ControlSplitNode firstNode = controlSplitNodeSet.iterator().next();
|
|
100 |
LoopFragmentWhole originalLoop = loop.whole();
|
|
101 |
StructuredGraph graph = firstNode.graph();
|
|
102 |
|
|
103 |
loop.loopBegin().incrementUnswitches();
|
|
104 |
|
|
105 |
// create new control split out of loop
|
|
106 |
ControlSplitNode newControlSplit = (ControlSplitNode) firstNode.copyWithInputs();
|
|
107 |
originalLoop.entryPoint().replaceAtPredecessor(newControlSplit);
|
|
108 |
|
|
109 |
/*
|
|
110 |
* The code below assumes that all of the control split nodes have the same successor
|
|
111 |
* structure, which should have been enforced by findUnswitchable.
|
|
112 |
*/
|
|
113 |
Iterator<Position> successors = firstNode.successorPositions().iterator();
|
|
114 |
assert successors.hasNext();
|
|
115 |
// original loop is used as first successor
|
|
116 |
Position firstPosition = successors.next();
|
|
117 |
AbstractBeginNode originalLoopBegin = BeginNode.begin(originalLoop.entryPoint());
|
|
118 |
firstPosition.set(newControlSplit, originalLoopBegin);
|
50330
|
119 |
originalLoopBegin.setNodeSourcePosition(firstPosition.get(firstNode).getNodeSourcePosition());
|
43972
|
120 |
|
|
121 |
while (successors.hasNext()) {
|
|
122 |
Position position = successors.next();
|
|
123 |
// create a new loop duplicate and connect it.
|
|
124 |
LoopFragmentWhole duplicateLoop = originalLoop.duplicate();
|
|
125 |
AbstractBeginNode newBegin = BeginNode.begin(duplicateLoop.entryPoint());
|
50330
|
126 |
newBegin.setNodeSourcePosition(position.get(firstNode).getNodeSourcePosition());
|
43972
|
127 |
position.set(newControlSplit, newBegin);
|
|
128 |
|
|
129 |
// For each cloned ControlSplitNode, simplify the proper path
|
|
130 |
for (ControlSplitNode controlSplitNode : controlSplitNodeSet) {
|
|
131 |
ControlSplitNode duplicatedControlSplit = duplicateLoop.getDuplicatedNode(controlSplitNode);
|
|
132 |
if (duplicatedControlSplit.isAlive()) {
|
|
133 |
AbstractBeginNode survivingSuccessor = (AbstractBeginNode) position.get(duplicatedControlSplit);
|
|
134 |
survivingSuccessor.replaceAtUsages(InputType.Guard, newBegin);
|
|
135 |
graph.removeSplitPropagate(duplicatedControlSplit, survivingSuccessor);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
}
|
|
139 |
// original loop is simplified last to avoid deleting controlSplitNode too early
|
|
140 |
for (ControlSplitNode controlSplitNode : controlSplitNodeSet) {
|
|
141 |
if (controlSplitNode.isAlive()) {
|
|
142 |
AbstractBeginNode survivingSuccessor = (AbstractBeginNode) firstPosition.get(controlSplitNode);
|
|
143 |
survivingSuccessor.replaceAtUsages(InputType.Guard, originalLoopBegin);
|
|
144 |
graph.removeSplitPropagate(controlSplitNode, survivingSuccessor);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
// TODO (gd) probabilities need some amount of fixup.. (probably also in other transforms)
|
|
149 |
}
|
|
150 |
|
52578
|
151 |
public static void partialUnroll(LoopEx loop, EconomicMap<LoopBeginNode, OpaqueNode> opaqueUnrolledStrides) {
|
46640
|
152 |
assert loop.loopBegin().isMainLoop();
|
46963
|
153 |
loop.loopBegin().graph().getDebug().log("LoopPartialUnroll %s", loop);
|
46762
|
154 |
|
46640
|
155 |
LoopFragmentInside newSegment = loop.inside().duplicate();
|
52578
|
156 |
newSegment.insertWithinAfter(loop, opaqueUnrolledStrides);
|
46640
|
157 |
|
|
158 |
}
|
|
159 |
|
|
160 |
// This function splits candidate loops into pre, main and post loops,
|
|
161 |
// dividing the iteration space to facilitate the majority of iterations
|
|
162 |
// being executed in a main loop, which will have RCE implemented upon it.
|
|
163 |
// The initial loop form is constrained to single entry/exit, but can have
|
|
164 |
// flow. The translation looks like:
|
|
165 |
//
|
|
166 |
// @formatter:off
|
|
167 |
//
|
|
168 |
// (Simple Loop entry) (Pre Loop Entry)
|
|
169 |
// | |
|
|
170 |
// (LoopBeginNode) (LoopBeginNode)
|
|
171 |
// | |
|
|
172 |
// (Loop Control Test)<------ ==> (Loop control Test)<------
|
|
173 |
// / \ \ / \ \
|
|
174 |
// (Loop Exit) (Loop Body) | (Loop Exit) (Loop Body) |
|
|
175 |
// | | | | | |
|
|
176 |
// (continue code) (Loop End) | if (M < length)* (Loop End) |
|
|
177 |
// \ / / \ \ /
|
|
178 |
// -----> / | ----->
|
|
179 |
// / if ( ... )*
|
|
180 |
// / / \
|
|
181 |
// / / \
|
|
182 |
// / / \
|
|
183 |
// | / (Main Loop Entry)
|
|
184 |
// | | |
|
|
185 |
// | | (LoopBeginNode)
|
|
186 |
// | | |
|
|
187 |
// | | (Loop Control Test)<------
|
|
188 |
// | | / \ \
|
|
189 |
// | | (Loop Exit) (Loop Body) |
|
|
190 |
// \ \ | | |
|
|
191 |
// \ \ | (Loop End) |
|
|
192 |
// \ \ | \ /
|
|
193 |
// \ \ | ------>
|
|
194 |
// \ \ |
|
|
195 |
// (Main Loop Merge)*
|
|
196 |
// |
|
|
197 |
// (Post Loop Entry)
|
|
198 |
// |
|
|
199 |
// (LoopBeginNode)
|
|
200 |
// |
|
|
201 |
// (Loop Control Test)<-----
|
|
202 |
// / \ \
|
|
203 |
// (Loop Exit) (Loop Body) |
|
|
204 |
// | | |
|
|
205 |
// (continue code) (Loop End) |
|
|
206 |
// \ /
|
|
207 |
// ----->
|
|
208 |
//
|
|
209 |
// Key: "*" = optional.
|
|
210 |
// @formatter:on
|
|
211 |
//
|
|
212 |
// The value "M" is the maximal value of the loop trip for the original
|
|
213 |
// loop. The value of "length" is applicable to the number of arrays found
|
|
214 |
// in the loop but is reduced if some or all of the arrays are known to be
|
|
215 |
// the same length as "M". The maximum number of tests can be equal to the
|
|
216 |
// number of arrays in the loop, where multiple instances of an array are
|
|
217 |
// subsumed into a single test for that arrays length.
|
|
218 |
//
|
|
219 |
// If the optional main loop entry tests are absent, the Pre Loop exit
|
|
220 |
// connects to the Main loops entry and there is no merge hanging off the
|
|
221 |
// main loops exit to converge flow from said tests. All split use data
|
|
222 |
// flow is mitigated through phi(s) in the main merge if present and
|
|
223 |
// passed through the main and post loop phi(s) from the originating pre
|
|
224 |
// loop with final phi(s) and data flow patched to the "continue code".
|
|
225 |
// The pre loop is constrained to one iteration for now and will likely
|
|
226 |
// be updated to produce vector alignment if applicable.
|
|
227 |
|
46963
|
228 |
public static LoopBeginNode insertPrePostLoops(LoopEx loop) {
|
|
229 |
StructuredGraph graph = loop.loopBegin().graph();
|
46640
|
230 |
graph.getDebug().log("LoopTransformations.insertPrePostLoops %s", loop);
|
|
231 |
LoopFragmentWhole preLoop = loop.whole();
|
46762
|
232 |
CountedLoopInfo preCounted = loop.counted();
|
46963
|
233 |
LoopBeginNode preLoopBegin = loop.loopBegin();
|
54084
|
234 |
AbstractBeginNode preLoopExitNode = preCounted.getCountedExit();
|
|
235 |
|
|
236 |
assert preLoop.nodes().contains(preLoopBegin);
|
|
237 |
assert preLoop.nodes().contains(preLoopExitNode);
|
46640
|
238 |
|
46963
|
239 |
// Each duplication is inserted after the original, ergo create the post loop first
|
|
240 |
LoopFragmentWhole mainLoop = preLoop.duplicate();
|
|
241 |
LoopFragmentWhole postLoop = preLoop.duplicate();
|
|
242 |
preLoopBegin.incrementSplits();
|
|
243 |
preLoopBegin.incrementSplits();
|
|
244 |
preLoopBegin.setPreLoop();
|
|
245 |
graph.getDebug().dump(DebugContext.VERBOSE_LEVEL, graph, "After duplication");
|
|
246 |
LoopBeginNode mainLoopBegin = mainLoop.getDuplicatedNode(preLoopBegin);
|
|
247 |
mainLoopBegin.setMainLoop();
|
|
248 |
LoopBeginNode postLoopBegin = postLoop.getDuplicatedNode(preLoopBegin);
|
|
249 |
postLoopBegin.setPostLoop();
|
46640
|
250 |
|
54084
|
251 |
AbstractBeginNode postLoopExitNode = postLoop.getDuplicatedNode(preLoopExitNode);
|
|
252 |
EndNode postEndNode = getBlockEndAfterLoopExit(postLoopExitNode);
|
46963
|
253 |
AbstractMergeNode postMergeNode = postEndNode.merge();
|
46640
|
254 |
|
46963
|
255 |
// Update the main loop phi initialization to carry from the pre loop
|
|
256 |
for (PhiNode prePhiNode : preLoopBegin.phis()) {
|
|
257 |
PhiNode mainPhiNode = mainLoop.getDuplicatedNode(prePhiNode);
|
|
258 |
mainPhiNode.setValueAt(0, prePhiNode);
|
|
259 |
}
|
46640
|
260 |
|
54084
|
261 |
AbstractBeginNode mainLoopExitNode = mainLoop.getDuplicatedNode(preLoopExitNode);
|
|
262 |
EndNode mainEndNode = getBlockEndAfterLoopExit(mainLoopExitNode);
|
46963
|
263 |
AbstractMergeNode mainMergeNode = mainEndNode.merge();
|
|
264 |
AbstractEndNode postEntryNode = postLoopBegin.forwardEnd();
|
46640
|
265 |
|
54084
|
266 |
// Exits have been merged, find the continuation below the merge
|
|
267 |
FixedNode continuationNode = mainMergeNode.next();
|
|
268 |
|
46963
|
269 |
// In the case of no Bounds tests, we just flow right into the main loop
|
|
270 |
AbstractBeginNode mainLandingNode = BeginNode.begin(postEntryNode);
|
|
271 |
mainLoopExitNode.setNext(mainLandingNode);
|
|
272 |
preLoopExitNode.setNext(mainLoopBegin.forwardEnd());
|
46640
|
273 |
|
46963
|
274 |
// Add and update any phi edges as per merge usage as needed and update usages
|
|
275 |
processPreLoopPhis(loop, mainLoop, postLoop);
|
|
276 |
continuationNode.predecessor().clearSuccessors();
|
|
277 |
postLoopExitNode.setNext(continuationNode);
|
|
278 |
cleanupMerge(postMergeNode, postLoopExitNode);
|
|
279 |
cleanupMerge(mainMergeNode, mainLandingNode);
|
46640
|
280 |
|
46963
|
281 |
// Change the preLoop to execute one iteration for now
|
52578
|
282 |
updatePreLoopLimit(preCounted);
|
46963
|
283 |
preLoopBegin.setLoopFrequency(1);
|
|
284 |
mainLoopBegin.setLoopFrequency(Math.max(0.0, mainLoopBegin.loopFrequency() - 2));
|
|
285 |
postLoopBegin.setLoopFrequency(Math.max(0.0, postLoopBegin.loopFrequency() - 1));
|
46762
|
286 |
|
46963
|
287 |
// The pre and post loops don't require safepoints at all
|
|
288 |
for (SafepointNode safepoint : preLoop.nodes().filter(SafepointNode.class)) {
|
|
289 |
graph.removeFixed(safepoint);
|
|
290 |
}
|
|
291 |
for (SafepointNode safepoint : postLoop.nodes().filter(SafepointNode.class)) {
|
|
292 |
graph.removeFixed(safepoint);
|
46640
|
293 |
}
|
|
294 |
graph.getDebug().dump(DebugContext.DETAILED_LEVEL, graph, "InsertPrePostLoops %s", loop);
|
46963
|
295 |
return mainLoopBegin;
|
46640
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* Cleanup the merge and remove the predecessors too.
|
|
300 |
*/
|
|
301 |
private static void cleanupMerge(AbstractMergeNode mergeNode, AbstractBeginNode landingNode) {
|
|
302 |
for (EndNode end : mergeNode.cfgPredecessors().snapshot()) {
|
|
303 |
mergeNode.removeEnd(end);
|
|
304 |
end.safeDelete();
|
|
305 |
}
|
|
306 |
mergeNode.prepareDelete(landingNode);
|
|
307 |
mergeNode.safeDelete();
|
|
308 |
}
|
|
309 |
|
|
310 |
private static void processPreLoopPhis(LoopEx preLoop, LoopFragmentWhole mainLoop, LoopFragmentWhole postLoop) {
|
|
311 |
// process phis for the post loop
|
|
312 |
LoopBeginNode preLoopBegin = preLoop.loopBegin();
|
|
313 |
for (PhiNode prePhiNode : preLoopBegin.phis()) {
|
|
314 |
PhiNode postPhiNode = postLoop.getDuplicatedNode(prePhiNode);
|
|
315 |
PhiNode mainPhiNode = mainLoop.getDuplicatedNode(prePhiNode);
|
|
316 |
postPhiNode.setValueAt(0, mainPhiNode);
|
|
317 |
|
|
318 |
// Build a work list to update the pre loop phis to the post loops phis
|
|
319 |
for (Node usage : prePhiNode.usages().snapshot()) {
|
|
320 |
if (usage == mainPhiNode) {
|
|
321 |
continue;
|
|
322 |
}
|
|
323 |
if (preLoop.isOutsideLoop(usage)) {
|
|
324 |
usage.replaceFirstInput(prePhiNode, postPhiNode);
|
|
325 |
}
|
|
326 |
}
|
|
327 |
}
|
|
328 |
for (Node node : preLoop.inside().nodes()) {
|
|
329 |
for (Node externalUsage : node.usages().snapshot()) {
|
|
330 |
if (preLoop.isOutsideLoop(externalUsage)) {
|
|
331 |
Node postUsage = postLoop.getDuplicatedNode(node);
|
|
332 |
assert postUsage != null;
|
|
333 |
externalUsage.replaceFirstInput(node, postUsage);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Find the end of the block following the LoopExit.
|
|
341 |
*/
|
54084
|
342 |
private static EndNode getBlockEndAfterLoopExit(AbstractBeginNode exit) {
|
|
343 |
FixedNode node = exit.next();
|
46640
|
344 |
// Find the last node after the exit blocks starts
|
|
345 |
return getBlockEnd(node);
|
|
346 |
}
|
|
347 |
|
|
348 |
private static EndNode getBlockEnd(FixedNode node) {
|
|
349 |
FixedNode curNode = node;
|
|
350 |
while (curNode instanceof FixedWithNextNode) {
|
|
351 |
curNode = ((FixedWithNextNode) curNode).next();
|
|
352 |
}
|
|
353 |
return (EndNode) curNode;
|
|
354 |
}
|
|
355 |
|
52578
|
356 |
private static void updatePreLoopLimit(CountedLoopInfo preCounted) {
|
|
357 |
// Update the pre loops limit test
|
|
358 |
// Make new limit one iteration
|
|
359 |
ValueNode newLimit = AddNode.add(preCounted.getStart(), preCounted.getCounter().strideNode(), NodeView.DEFAULT);
|
|
360 |
// Fetch the variable we are not replacing and configure the one we are
|
|
361 |
ValueNode ub = preCounted.getLimit();
|
58299
|
362 |
IntegerHelper helper = preCounted.getCounterIntegerHelper();
|
52578
|
363 |
LogicNode entryCheck;
|
|
364 |
if (preCounted.getDirection() == Direction.Up) {
|
58299
|
365 |
entryCheck = helper.createCompareNode(newLimit, ub, NodeView.DEFAULT);
|
46640
|
366 |
} else {
|
58299
|
367 |
entryCheck = helper.createCompareNode(ub, newLimit, NodeView.DEFAULT);
|
46640
|
368 |
}
|
52578
|
369 |
newLimit = ConditionalNode.create(entryCheck, newLimit, ub, NodeView.DEFAULT);
|
46640
|
370 |
// Re-wire the condition with the new limit
|
52578
|
371 |
CompareNode compareNode = (CompareNode) preCounted.getLimitTest().condition();
|
|
372 |
compareNode.replaceFirstInput(ub, compareNode.graph().addOrUniqueWithInputs(newLimit));
|
46640
|
373 |
}
|
|
374 |
|
43972
|
375 |
public static List<ControlSplitNode> findUnswitchable(LoopEx loop) {
|
|
376 |
List<ControlSplitNode> controls = null;
|
|
377 |
ValueNode invariantValue = null;
|
|
378 |
for (IfNode ifNode : loop.whole().nodes().filter(IfNode.class)) {
|
|
379 |
if (loop.isOutsideLoop(ifNode.condition())) {
|
|
380 |
if (controls == null) {
|
|
381 |
invariantValue = ifNode.condition();
|
|
382 |
controls = new ArrayList<>();
|
|
383 |
controls.add(ifNode);
|
|
384 |
} else if (ifNode.condition() == invariantValue) {
|
|
385 |
controls.add(ifNode);
|
|
386 |
}
|
|
387 |
}
|
|
388 |
}
|
|
389 |
if (controls == null) {
|
|
390 |
SwitchNode firstSwitch = null;
|
|
391 |
for (SwitchNode switchNode : loop.whole().nodes().filter(SwitchNode.class)) {
|
|
392 |
if (switchNode.successors().count() > 1 && loop.isOutsideLoop(switchNode.value())) {
|
|
393 |
if (controls == null) {
|
|
394 |
firstSwitch = switchNode;
|
|
395 |
invariantValue = switchNode.value();
|
|
396 |
controls = new ArrayList<>();
|
|
397 |
controls.add(switchNode);
|
54601
|
398 |
} else if (switchNode.value() == invariantValue) {
|
|
399 |
// Fortify: Suppress Null Dereference false positive
|
|
400 |
assert firstSwitch != null;
|
|
401 |
|
|
402 |
if (firstSwitch.structureEquals(switchNode)) {
|
|
403 |
// Only collect switches which test the same values in the same order
|
|
404 |
controls.add(switchNode);
|
|
405 |
}
|
43972
|
406 |
}
|
|
407 |
}
|
|
408 |
}
|
|
409 |
}
|
|
410 |
return controls;
|
|
411 |
}
|
46640
|
412 |
|
|
413 |
public static boolean isUnrollableLoop(LoopEx loop) {
|
46807
|
414 |
if (!loop.isCounted() || !loop.counted().getCounter().isConstantStride() || !loop.loop().getChildren().isEmpty()) {
|
46640
|
415 |
return false;
|
|
416 |
}
|
52578
|
417 |
assert loop.counted().getDirection() != null;
|
46640
|
418 |
LoopBeginNode loopBegin = loop.loopBegin();
|
46963
|
419 |
LogicNode condition = loop.counted().getLimitTest().condition();
|
|
420 |
if (!(condition instanceof CompareNode)) {
|
|
421 |
return false;
|
|
422 |
}
|
48861
|
423 |
if (((CompareNode) condition).condition() == CanonicalCondition.EQ) {
|
46963
|
424 |
condition.getDebug().log(DebugContext.VERBOSE_LEVEL, "isUnrollableLoop %s condition unsupported %s ", loopBegin, ((CompareNode) condition).condition());
|
|
425 |
return false;
|
|
426 |
}
|
52578
|
427 |
long stride = loop.counted().getCounter().constantStride();
|
|
428 |
try {
|
|
429 |
Math.addExact(stride, stride);
|
|
430 |
} catch (ArithmeticException ae) {
|
|
431 |
condition.getDebug().log(DebugContext.VERBOSE_LEVEL, "isUnrollableLoop %s doubling the stride overflows %d", loopBegin, stride);
|
|
432 |
return false;
|
|
433 |
}
|
54084
|
434 |
if (!loop.canDuplicateLoop()) {
|
|
435 |
return false;
|
|
436 |
}
|
46640
|
437 |
if (loopBegin.isMainLoop() || loopBegin.isSimpleLoop()) {
|
|
438 |
// Flow-less loops to partial unroll for now. 3 blocks corresponds to an if that either
|
|
439 |
// exits or continues the loop. There might be fixed and floating work within the loop
|
|
440 |
// as well.
|
|
441 |
if (loop.loop().getBlocks().size() < 3) {
|
46762
|
442 |
return true;
|
46640
|
443 |
}
|
46963
|
444 |
condition.getDebug().log(DebugContext.VERBOSE_LEVEL, "isUnrollableLoop %s too large to unroll %s ", loopBegin, loop.loop().getBlocks().size());
|
46640
|
445 |
}
|
46762
|
446 |
return false;
|
46640
|
447 |
}
|
43972
|
448 |
}
|