@@ -328,6 +328,37 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
328
328
if (MaxTripCount && ULO.Count > MaxTripCount)
329
329
ULO.Count = MaxTripCount;
330
330
331
+ struct ExitInfo {
332
+ unsigned TripCount;
333
+ unsigned TripMultiple;
334
+ unsigned BreakoutTrip;
335
+ bool ExitOnTrue;
336
+ SmallVector<BasicBlock *> ExitingBlocks;
337
+ };
338
+ DenseMap<BasicBlock *, ExitInfo> ExitInfos;
339
+ SmallVector<BasicBlock *, 4 > ExitingBlocks;
340
+ L->getExitingBlocks (ExitingBlocks);
341
+ for (auto *ExitingBlock : ExitingBlocks) {
342
+ // The folding code is not prepared to deal with non-branch instructions
343
+ // right now.
344
+ auto *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator ());
345
+ if (!BI)
346
+ continue ;
347
+
348
+ ExitInfo &Info = ExitInfos.try_emplace (ExitingBlock).first ->second ;
349
+ Info.TripCount = SE->getSmallConstantTripCount (L, ExitingBlock);
350
+ Info.TripMultiple = SE->getSmallConstantTripMultiple (L, ExitingBlock);
351
+ if (Info.TripCount != 0 ) {
352
+ Info.BreakoutTrip = Info.TripCount % ULO.Count ;
353
+ Info.TripMultiple = 0 ;
354
+ } else {
355
+ Info.BreakoutTrip = Info.TripMultiple =
356
+ (unsigned )GreatestCommonDivisor64 (ULO.Count , Info.TripMultiple );
357
+ }
358
+ Info.ExitOnTrue = !L->contains (BI->getSuccessor (0 ));
359
+ Info.ExitingBlocks .push_back (ExitingBlock);
360
+ }
361
+
331
362
// Are we eliminating the loop control altogether? Note that we can know
332
363
// we're eliminating the backedge without knowing exactly which iteration
333
364
// of the unrolled body exits.
@@ -362,31 +393,12 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
362
393
363
394
// A conditional branch which exits the loop, which can be optimized to an
364
395
// unconditional branch in the unrolled loop in some cases.
365
- BranchInst *ExitingBI = nullptr ;
366
396
bool LatchIsExiting = L->isLoopExiting (LatchBlock);
367
- if (LatchIsExiting)
368
- ExitingBI = LatchBI;
369
- else if (BasicBlock *ExitingBlock = L->getExitingBlock ())
370
- ExitingBI = dyn_cast<BranchInst>(ExitingBlock->getTerminator ());
371
397
if (!LatchBI || (LatchBI->isConditional () && !LatchIsExiting)) {
372
398
LLVM_DEBUG (
373
399
dbgs () << " Can't unroll; a conditional latch must exit the loop" );
374
400
return LoopUnrollResult::Unmodified;
375
401
}
376
- LLVM_DEBUG ({
377
- if (ExitingBI)
378
- dbgs () << " Exiting Block = " << ExitingBI->getParent ()->getName ()
379
- << " \n " ;
380
- else
381
- dbgs () << " No single exiting block\n " ;
382
- });
383
-
384
- // Warning: ExactTripCount is the exact trip count for the block ending in
385
- // ExitingBI, not neccessarily an exact exit count *for the loop*. The
386
- // distinction comes when we have an exiting latch, but the loop exits
387
- // through another exit first.
388
- const unsigned ExactTripCount = ExitingBI ?
389
- SE->getSmallConstantTripCount (L,ExitingBI->getParent ()) : 0 ;
390
402
391
403
// Loops containing convergent instructions must have a count that divides
392
404
// their TripMultiple.
@@ -421,6 +433,7 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
421
433
}
422
434
423
435
// If we know the trip count, we know the multiple...
436
+ // TODO: This is only used for the ORE code, remove it.
424
437
unsigned BreakoutTrip = 0 ;
425
438
if (ULO.TripCount != 0 ) {
426
439
BreakoutTrip = ULO.TripCount % ULO.Count ;
@@ -504,12 +517,9 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
504
517
}
505
518
506
519
std::vector<BasicBlock *> Headers;
507
- std::vector<BasicBlock *> ExitingBlocks;
508
520
std::vector<BasicBlock *> Latches;
509
521
Headers.push_back (Header);
510
522
Latches.push_back (LatchBlock);
511
- if (ExitingBI)
512
- ExitingBlocks.push_back (ExitingBI->getParent ());
513
523
514
524
// The current on-the-fly SSA update requires blocks to be processed in
515
525
// reverse postorder so that LastValueMap contains the correct value at each
@@ -609,9 +619,9 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
609
619
610
620
// Keep track of the exiting block and its successor block contained in
611
621
// the loop for the current iteration.
612
- if (ExitingBI)
613
- if (*BB == ExitingBlocks[ 0 ] )
614
- ExitingBlocks.push_back (New);
622
+ auto ExitInfoIt = ExitInfos. find (*BB);
623
+ if (ExitInfoIt != ExitInfos. end () )
624
+ ExitInfoIt-> second . ExitingBlocks .push_back (New);
615
625
616
626
NewBlocks.push_back (New);
617
627
UnrolledLoopBlocks.push_back (New);
@@ -701,71 +711,79 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
701
711
702
712
DomTreeUpdater DTU (DT, DomTreeUpdater::UpdateStrategy::Lazy);
703
713
704
- if (ExitingBI) {
705
- auto SetDest = [&](BasicBlock *Src, bool WillExit, bool ExitOnTrue) {
706
- auto *Term = cast<BranchInst>(Src->getTerminator ());
707
- const unsigned Idx = ExitOnTrue ^ WillExit;
708
- BasicBlock *Dest = Term->getSuccessor (Idx);
709
- BasicBlock *DeadSucc = Term->getSuccessor (1 -Idx);
714
+ auto SetDest = [&](BasicBlock *Src, bool WillExit, bool ExitOnTrue) {
715
+ auto *Term = cast<BranchInst>(Src->getTerminator ());
716
+ const unsigned Idx = ExitOnTrue ^ WillExit;
717
+ BasicBlock *Dest = Term->getSuccessor (Idx);
718
+ BasicBlock *DeadSucc = Term->getSuccessor (1 -Idx);
710
719
711
- // Remove predecessors from all non-Dest successors.
712
- DeadSucc->removePredecessor (Src, /* KeepOneInputPHIs */ true );
720
+ // Remove predecessors from all non-Dest successors.
721
+ DeadSucc->removePredecessor (Src, /* KeepOneInputPHIs */ true );
713
722
714
- // Replace the conditional branch with an unconditional one.
715
- BranchInst::Create (Dest, Term);
716
- Term->eraseFromParent ();
723
+ // Replace the conditional branch with an unconditional one.
724
+ BranchInst::Create (Dest, Term);
725
+ Term->eraseFromParent ();
717
726
718
- DTU.applyUpdates ({{DominatorTree::Delete, Src, DeadSucc}});
719
- };
727
+ DTU.applyUpdates ({{DominatorTree::Delete, Src, DeadSucc}});
728
+ };
720
729
721
- auto WillExit = [&](unsigned i, unsigned j) -> Optional<bool > {
722
- if (CompletelyUnroll) {
723
- if (PreserveOnlyFirst) {
724
- if (i == 0 )
725
- return None;
726
- return j == 0 ;
727
- }
728
- // Complete (but possibly inexact) unrolling
729
- if (j == 0 )
730
- return true ;
731
- // Warning: ExactTripCount is the trip count of the exiting
732
- // block which ends in ExitingBI, not neccessarily the loop.
733
- if (ExactTripCount && j != ExactTripCount)
734
- return false ;
735
- return None;
730
+ auto WillExit = [&](const ExitInfo &Info, unsigned i, unsigned j,
731
+ bool IsLatch) -> Optional<bool > {
732
+ if (CompletelyUnroll) {
733
+ if (PreserveOnlyFirst) {
734
+ if (i == 0 )
735
+ return None;
736
+ return j == 0 ;
736
737
}
737
-
738
- if (RuntimeTripCount && j != 0 )
738
+ // Complete (but possibly inexact) unrolling
739
+ if (j == 0 )
740
+ return true ;
741
+ if (Info.TripCount && j != Info.TripCount )
739
742
return false ;
743
+ return None;
744
+ }
740
745
741
- if (j != BreakoutTrip &&
742
- (ULO. TripMultiple == 0 || j % ULO. TripMultiple != 0 )) {
743
- // If we know the trip count or a multiple of it, we can safely use an
744
- // unconditional branch for some iterations.
746
+ if (RuntimeTripCount) {
747
+ // If runtime unrolling inserts a prologue, information about non-latch
748
+ // exits may be stale.
749
+ if (IsLatch && j != 0 )
745
750
return false ;
746
- }
747
751
return None;
748
- };
752
+ }
753
+
754
+ if (j != Info.BreakoutTrip &&
755
+ (Info.TripMultiple == 0 || j % Info.TripMultiple != 0 )) {
756
+ // If we know the trip count or a multiple of it, we can safely use an
757
+ // unconditional branch for some iterations.
758
+ return false ;
759
+ }
760
+ return None;
761
+ };
749
762
750
- // Fold branches for iterations where we know that they will exit or not
751
- // exit.
752
- bool ExitOnTrue = !L->contains (ExitingBI->getSuccessor (0 ));
753
- for (unsigned i = 0 , e = ExitingBlocks.size (); i != e; ++i) {
763
+ // Fold branches for iterations where we know that they will exit or not
764
+ // exit.
765
+ for (const auto &Pair : ExitInfos) {
766
+ const ExitInfo &Info = Pair.second ;
767
+ for (unsigned i = 0 , e = Info.ExitingBlocks .size (); i != e; ++i) {
754
768
// The branch destination.
755
769
unsigned j = (i + 1 ) % e;
756
- Optional<bool > KnownWillExit = WillExit (i, j);
770
+ bool IsLatch = Pair.first == LatchBlock;
771
+ Optional<bool > KnownWillExit = WillExit (Info, i, j, IsLatch);
757
772
if (!KnownWillExit)
758
773
continue ;
759
774
760
- // TODO: Also fold known-exiting branches for non-latch exits.
761
- if (*KnownWillExit && !LatchIsExiting)
775
+ // We don't fold known-exiting branches for non-latch exits here,
776
+ // because this ensures that both all loop blocks and all exit blocks
777
+ // remain reachable in the CFG.
778
+ // TODO: We could fold these branches, but it would require much more
779
+ // sophisticated updates to LoopInfo.
780
+ if (*KnownWillExit && !IsLatch)
762
781
continue ;
763
782
764
- SetDest (ExitingBlocks[i], *KnownWillExit, ExitOnTrue);
783
+ SetDest (Info. ExitingBlocks [i], *KnownWillExit, Info. ExitOnTrue );
765
784
}
766
785
}
767
786
768
-
769
787
// When completely unrolling, the last latch becomes unreachable.
770
788
if (!LatchIsExiting && CompletelyUnroll)
771
789
changeToUnreachable (Latches.back ()->getTerminator (), /* UseTrap */ false ,
0 commit comments