@@ -327,19 +327,62 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
327
327
else
328
328
NewOuter->addChildLoop (L->removeChildLoop (SubLoops.begin () + I));
329
329
330
+ SmallVector<BasicBlock *, 8 > OuterLoopBlocks;
331
+ OuterLoopBlocks.push_back (NewBB);
330
332
// Now that we know which blocks are in L and which need to be moved to
331
333
// OuterLoop, move any blocks that need it.
332
334
for (unsigned i = 0 ; i != L->getBlocks ().size (); ++i) {
333
335
BasicBlock *BB = L->getBlocks ()[i];
334
336
if (!BlocksInL.count (BB)) {
335
337
// Move this block to the parent, updating the exit blocks sets
336
338
L->removeBlockFromLoop (BB);
337
- if ((*LI)[BB] == L)
339
+ if ((*LI)[BB] == L) {
338
340
LI->changeLoopFor (BB, NewOuter);
341
+ OuterLoopBlocks.push_back (BB);
342
+ }
339
343
--i;
340
344
}
341
345
}
342
346
347
+ // Split edges to exit blocks from the inner loop, if they emerged in the
348
+ // process of separating the outer one.
349
+ SmallVector<BasicBlock *, 8 > ExitBlocks;
350
+ L->getExitBlocks (ExitBlocks);
351
+ SmallSetVector<BasicBlock *, 8 > ExitBlockSet (ExitBlocks.begin (),
352
+ ExitBlocks.end ());
353
+ for (BasicBlock *ExitBlock : ExitBlockSet) {
354
+ if (any_of (predecessors (ExitBlock),
355
+ [L](BasicBlock *BB) { return !L->contains (BB); })) {
356
+ rewriteLoopExitBlock (L, ExitBlock, DT, LI, PreserveLCSSA);
357
+ }
358
+ }
359
+
360
+ if (PreserveLCSSA) {
361
+ // Fix LCSSA form for L. Some values, which previously were only used inside
362
+ // L, can now be used in NewOuter loop. We need to insert phi-nodes for them
363
+ // in corresponding exit blocks.
364
+
365
+ // Go through all instructions in OuterLoopBlocks and check if they are
366
+ // using operands from the inner loop. In this case we'll need to fix LCSSA
367
+ // for these instructions.
368
+ SmallSetVector<Instruction *, 8 > WorklistSet;
369
+ for (BasicBlock *OuterBB: OuterLoopBlocks) {
370
+ for (Instruction &I : *OuterBB) {
371
+ for (Value *Op : I.operands ()) {
372
+ Instruction *OpI = dyn_cast<Instruction>(Op);
373
+ if (!OpI || !L->contains (OpI))
374
+ continue ;
375
+ WorklistSet.insert (OpI);
376
+ }
377
+ }
378
+ }
379
+ SmallVector<Instruction *, 8 > Worklist (WorklistSet.begin (),
380
+ WorklistSet.end ());
381
+ formLCSSAForInstructions (Worklist, *DT, *LI);
382
+ assert (NewOuter->isRecursivelyLCSSAForm (*DT) &&
383
+ " LCSSA is broken after separating nested loops!" );
384
+ }
385
+
343
386
return NewOuter;
344
387
}
345
388
@@ -541,17 +584,12 @@ static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,
541
584
SmallSetVector<BasicBlock *, 8 > ExitBlockSet (ExitBlocks.begin (),
542
585
ExitBlocks.end ());
543
586
for (BasicBlock *ExitBlock : ExitBlockSet) {
544
- for (pred_iterator PI = pred_begin (ExitBlock), PE = pred_end (ExitBlock);
545
- PI != PE; ++PI)
546
- // Must be exactly this loop: no subloops, parent loops, or non-loop preds
547
- // allowed.
548
- if (!L->contains (*PI)) {
549
- if (rewriteLoopExitBlock (L, ExitBlock, DT, LI, PreserveLCSSA)) {
550
- ++NumInserted;
551
- Changed = true ;
552
- }
553
- break ;
554
- }
587
+ if (any_of (predecessors (ExitBlock),
588
+ [L](BasicBlock *BB) { return !L->contains (BB); })) {
589
+ rewriteLoopExitBlock (L, ExitBlock, DT, LI, PreserveLCSSA);
590
+ ++NumInserted;
591
+ Changed = true ;
592
+ }
555
593
}
556
594
557
595
// If the header has more than two predecessors at this point (from the
0 commit comments