@@ -291,68 +291,26 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter,
291
291
Function *F = Header->getParent ();
292
292
LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO ();
293
293
LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO ();
294
+ Loop *NewLoop = nullptr ;
294
295
Loop *ParentLoop = L->getParentLoop ();
295
-
296
- // The map from original loops to their cloned copies.
297
- SmallDenseMap<const Loop *, Loop *, 4 > NewLoops;
296
+ if (CreateRemainderLoop) {
297
+ NewLoop = new Loop ();
298
+ if (ParentLoop)
299
+ ParentLoop->addChildLoop (NewLoop);
300
+ else
301
+ LI->addTopLevelLoop (NewLoop);
302
+ }
298
303
299
304
// For each block in the original loop, create a new copy,
300
305
// and update the value map with the newly created values.
301
306
for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
302
307
BasicBlock *NewBB = CloneBasicBlock (*BB, VMap, " ." + suffix, F);
303
308
NewBlocks.push_back (NewBB);
304
309
305
- // Figure out which loop NewBB is in.
306
- auto findClonedLoop = [&](const Loop *OldLoop) {
307
- Loop *&NewLoop = NewLoops[OldLoop];
308
- // If we've encountered this loop before, return it right away.
309
- if (NewLoop)
310
- return NewLoop;
311
-
312
- // If BB is from L, and we're not creating a remainder, the loop for
313
- // NewBB will be ParentLoop, which might be null. Update NewLoops map and
314
- // return ParentLoop.
315
- if (OldLoop == L && !CreateRemainderLoop)
316
- return (NewLoop = ParentLoop);
317
-
318
- // Now we know that there should be a cloned counterpart for OldLoop, but
319
- // we haven't seen it yet. Note that OldLoop might be L if we're
320
- // generating a remainder loop, or it can be an inner loop of L - in this
321
- // case we'll recreate the loop structure of L in its clone.
322
-
323
- // This is a first block belonging to OldLoop encountered in our RPO
324
- // traversal.
325
- assert (*BB == OldLoop->getHeader () && " Header should be first in RPO" );
326
-
327
- NewLoop = new Loop;
328
- Loop *OldLoopParent = OldLoop->getParentLoop ();
329
- // If OldLoop has a parent loop, we have two options:
330
- // 1. ParentLoop is the parent of L. It won't be cloned, and it will
331
- // be a parent for NewLoop too.
332
- // 2. ParentLoop is not a parent of L. In this case, it should be one
333
- // of the cloned loops and we should be able to find it in our map.
334
- //
335
- // If OldLoop doesn't have a parent, then NewLoop should be yet another
336
- // top-level loop.
337
- if (OldLoopParent) {
338
- Loop *NewLoopParent = ParentLoop == OldLoopParent
339
- ? ParentLoop
340
- : NewLoops.lookup (OldLoopParent);
341
- assert (NewLoopParent && " Expected parent loop before sub-loop in RPO" );
342
- NewLoopParent->addChildLoop (NewLoop);
343
- } else
344
- LI->addTopLevelLoop (NewLoop);
345
- return NewLoop;
346
- };
347
-
348
- Loop *NewLoop = findClonedLoop (LI->getLoopFor (*BB));
349
-
350
- assert (NewLoop ||
351
- (!CreateRemainderLoop && !ParentLoop) &&
352
- " NewLoop can only be null if we are cloning top-level loop "
353
- " without creating a remainder loop." );
354
310
if (NewLoop)
355
311
NewLoop->addBasicBlockToLoop (NewBB, *LI);
312
+ else if (ParentLoop)
313
+ ParentLoop->addBasicBlockToLoop (NewBB, *LI);
356
314
357
315
VMap[*BB] = NewBB;
358
316
if (Header == *BB) {
@@ -411,8 +369,7 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter,
411
369
NewPHI->setIncomingValue (idx, V);
412
370
}
413
371
}
414
- if (CreateRemainderLoop) {
415
- Loop *NewLoop = NewLoops[L];
372
+ if (NewLoop) {
416
373
// Add unroll disable metadata to disable future unrolling for this loop.
417
374
SmallVector<Metadata *, 4 > MDs;
418
375
// Reserve first location for self reference to the LoopID metadata node.
0 commit comments