@@ -172,6 +172,36 @@ static bool needToInsertPhisForLCSSA(Loop *L, std::vector<BasicBlock *> Blocks,
172
172
return false ;
173
173
}
174
174
175
+ // / Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary
176
+ // / and adds a mapping from the original loop to the new loop to NewLoops.
177
+ // / Returns nullptr if no new loop was created and a pointer to the
178
+ // / original loop OriginalBB was part of otherwise.
179
+ const Loop* llvm::addClonedBlockToLoopInfo (BasicBlock *OriginalBB,
180
+ BasicBlock *ClonedBB, LoopInfo *LI,
181
+ NewLoopsMap &NewLoops) {
182
+ // Figure out which loop New is in.
183
+ const Loop *OldLoop = LI->getLoopFor (OriginalBB);
184
+ assert (OldLoop && " Should (at least) be in the loop being unrolled!" );
185
+
186
+ Loop *&NewLoop = NewLoops[OldLoop];
187
+ if (!NewLoop) {
188
+ // Found a new sub-loop.
189
+ assert (OriginalBB == OldLoop->getHeader () &&
190
+ " Header should be first in RPO" );
191
+
192
+ Loop *NewLoopParent = NewLoops.lookup (OldLoop->getParentLoop ());
193
+ assert (NewLoopParent &&
194
+ " Expected parent loop before sub-loop in RPO" );
195
+ NewLoop = new Loop;
196
+ NewLoopParent->addChildLoop (NewLoop);
197
+ NewLoop->addBasicBlockToLoop (ClonedBB, *LI);
198
+ return OldLoop;
199
+ } else {
200
+ NewLoop->addBasicBlockToLoop (ClonedBB, *LI);
201
+ return nullptr ;
202
+ }
203
+ }
204
+
175
205
// / Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
176
206
// / if unrolling was successful, or false if the loop was unmodified. Unrolling
177
207
// / can only fail when the loop's latch block is not terminated by a conditional
@@ -428,28 +458,14 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool Force,
428
458
assert (LI->getLoopFor (*BB) == L && " Header should not be in a sub-loop" );
429
459
L->addBasicBlockToLoop (New, *LI);
430
460
} else {
431
- // Figure out which loop New is in.
432
- const Loop *OldLoop = LI->getLoopFor (*BB);
433
- assert (OldLoop && " Should (at least) be in the loop being unrolled!" );
434
-
435
- Loop *&NewLoop = NewLoops[OldLoop];
436
- if (!NewLoop) {
437
- // Found a new sub-loop.
438
- assert (*BB == OldLoop->getHeader () &&
439
- " Header should be first in RPO" );
440
-
441
- Loop *NewLoopParent = NewLoops.lookup (OldLoop->getParentLoop ());
442
- assert (NewLoopParent &&
443
- " Expected parent loop before sub-loop in RPO" );
444
- NewLoop = new Loop;
445
- NewLoopParent->addChildLoop (NewLoop);
446
- LoopsToSimplify.insert (NewLoop);
461
+ const Loop *OldLoop = addClonedBlockToLoopInfo (*BB, New, LI, NewLoops);
462
+ if (OldLoop) {
463
+ LoopsToSimplify.insert (NewLoops[OldLoop]);
447
464
448
465
// Forget the old loop, since its inputs may have changed.
449
466
if (SE)
450
467
SE->forgetLoop (OldLoop);
451
468
}
452
- NewLoop->addBasicBlockToLoop (New, *LI);
453
469
}
454
470
455
471
if (*BB == Header)
0 commit comments