@@ -44,8 +44,6 @@ using namespace llvm;
44
44
// TODO: Should these be here or in LoopUnroll?
45
45
STATISTIC (NumCompletelyUnrolled, " Number of loops completely unrolled" );
46
46
STATISTIC (NumUnrolled, " Number of loops unrolled (completely or otherwise)" );
47
- STATISTIC (NumUnrolledWithHeader, " Number of loops unrolled without a "
48
- " conditional latch (completely or otherwise)" );
49
47
50
48
static cl::opt<bool >
51
49
UnrollRuntimeEpilog (" unroll-runtime-epilog" , cl::init(false ), cl::Hidden,
@@ -297,46 +295,28 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
297
295
return LoopUnrollResult::Unmodified;
298
296
}
299
297
300
- // The current loop unroll pass can unroll loops with a single latch or header
298
+ // The current loop unroll pass can only unroll loops with a single latch
301
299
// that's a conditional branch exiting the loop.
302
300
// FIXME: The implementation can be extended to work with more complicated
303
301
// cases, e.g. loops with multiple latches.
304
302
BasicBlock *Header = L->getHeader ();
305
- BranchInst *HeaderBI = dyn_cast<BranchInst>(Header->getTerminator ());
306
303
BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator ());
307
304
308
- // FIXME: Support loops without conditional latch and multiple exiting blocks.
309
- if (!BI ||
310
- (BI->isUnconditional () && (!HeaderBI || HeaderBI->isUnconditional () ||
311
- L->getExitingBlock () != Header))) {
312
- LLVM_DEBUG (dbgs () << " Can't unroll; loop not terminated by a conditional "
313
- " branch in the latch or header.\n " );
314
- return LoopUnrollResult::Unmodified;
315
- }
316
-
317
- auto CheckLatchSuccessors = [&](unsigned S1, unsigned S2) {
318
- return BI->isConditional () && BI->getSuccessor (S1) == Header &&
319
- !L->contains (BI->getSuccessor (S2));
320
- };
321
-
322
- // If we have a conditional latch, it must exit the loop.
323
- if (BI && BI->isConditional () && !CheckLatchSuccessors (0 , 1 ) &&
324
- !CheckLatchSuccessors (1 , 0 )) {
305
+ if (!BI || BI->isUnconditional ()) {
306
+ // The loop-rotate pass can be helpful to avoid this in many cases.
325
307
LLVM_DEBUG (
326
- dbgs () << " Can't unroll; a conditional latch must exit the loop" );
308
+ dbgs ()
309
+ << " Can't unroll; loop not terminated by a conditional branch.\n " );
327
310
return LoopUnrollResult::Unmodified;
328
311
}
329
312
330
- auto CheckHeaderSuccessors = [&](unsigned S1, unsigned S2) {
331
- return HeaderBI && HeaderBI->isConditional () &&
332
- L->contains (HeaderBI->getSuccessor (S1)) &&
333
- !L->contains (HeaderBI->getSuccessor (S2));
313
+ auto CheckSuccessors = [&](unsigned S1, unsigned S2) {
314
+ return BI->getSuccessor (S1) == Header && !L->contains (BI->getSuccessor (S2));
334
315
};
335
316
336
- // If we do not have a conditional latch, the header must exit the loop.
337
- if (BI && !BI->isConditional () && HeaderBI && HeaderBI->isConditional () &&
338
- !CheckHeaderSuccessors (0 , 1 ) && !CheckHeaderSuccessors (1 , 0 )) {
339
- LLVM_DEBUG (dbgs () << " Can't unroll; conditional header must exit the loop" );
317
+ if (!CheckSuccessors (0 , 1 ) && !CheckSuccessors (1 , 0 )) {
318
+ LLVM_DEBUG (dbgs () << " Can't unroll; only loops with one conditional latch"
319
+ " exiting the loop can be unrolled\n " );
340
320
return LoopUnrollResult::Unmodified;
341
321
}
342
322
@@ -523,17 +503,8 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
523
503
SE->forgetTopmostLoop (L);
524
504
}
525
505
526
- bool ContinueOnTrue;
527
- bool LatchIsExiting = BI->isConditional ();
528
- BasicBlock *LoopExit = nullptr ;
529
- if (LatchIsExiting) {
530
- ContinueOnTrue = L->contains (BI->getSuccessor (0 ));
531
- LoopExit = BI->getSuccessor (ContinueOnTrue);
532
- } else {
533
- NumUnrolledWithHeader++;
534
- ContinueOnTrue = L->contains (HeaderBI->getSuccessor (0 ));
535
- LoopExit = HeaderBI->getSuccessor (ContinueOnTrue);
536
- }
506
+ bool ContinueOnTrue = L->contains (BI->getSuccessor (0 ));
507
+ BasicBlock *LoopExit = BI->getSuccessor (ContinueOnTrue);
537
508
538
509
// For the first iteration of the loop, we should use the precloned values for
539
510
// PHI nodes. Insert associations now.
@@ -543,23 +514,11 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
543
514
OrigPHINode.push_back (cast<PHINode>(I));
544
515
}
545
516
546
- std::vector<BasicBlock *> Headers;
547
- std::vector<BasicBlock *> HeaderSucc;
548
- std::vector<BasicBlock *> Latches;
517
+ std::vector<BasicBlock*> Headers;
518
+ std::vector<BasicBlock*> Latches;
549
519
Headers.push_back (Header);
550
520
Latches.push_back (LatchBlock);
551
521
552
- if (!LatchIsExiting) {
553
- auto *Term = cast<BranchInst>(Header->getTerminator ());
554
- if (Term->isUnconditional () || L->contains (Term->getSuccessor (0 ))) {
555
- assert (L->contains (Term->getSuccessor (0 )));
556
- HeaderSucc.push_back (Term->getSuccessor (0 ));
557
- } else {
558
- assert (L->contains (Term->getSuccessor (1 )));
559
- HeaderSucc.push_back (Term->getSuccessor (1 ));
560
- }
561
- }
562
-
563
522
// The current on-the-fly SSA update requires blocks to be processed in
564
523
// reverse postorder so that LastValueMap contains the correct value at each
565
524
// exit.
@@ -649,13 +608,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
649
608
if (*BB == LatchBlock)
650
609
Latches.push_back (New);
651
610
652
- // Keep track of the successor of the new header in the current iteration.
653
- for (auto *Pred : predecessors (*BB))
654
- if (Pred == Header) {
655
- HeaderSucc.push_back (New);
656
- break ;
657
- }
658
-
659
611
NewBlocks.push_back (New);
660
612
UnrolledLoopBlocks.push_back (New);
661
613
@@ -705,21 +657,51 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
705
657
}
706
658
}
707
659
708
- auto setDest = [LoopExit, ContinueOnTrue](BasicBlock *Src, BasicBlock *Dest,
709
- ArrayRef<BasicBlock *> NextBlocks,
710
- BasicBlock *CurrentHeader,
711
- bool NeedConditional) {
712
- auto *Term = cast<BranchInst>(Src->getTerminator ());
660
+ // Now that all the basic blocks for the unrolled iterations are in place,
661
+ // set up the branches to connect them.
662
+ for (unsigned i = 0 , e = Latches.size (); i != e; ++i) {
663
+ // The original branch was replicated in each unrolled iteration.
664
+ BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator ());
665
+
666
+ // The branch destination.
667
+ unsigned j = (i + 1 ) % e;
668
+ BasicBlock *Dest = Headers[j];
669
+ bool NeedConditional = true ;
670
+
671
+ if (RuntimeTripCount && j != 0 ) {
672
+ NeedConditional = false ;
673
+ }
674
+
675
+ // For a complete unroll, make the last iteration end with a branch
676
+ // to the exit block.
677
+ if (CompletelyUnroll) {
678
+ if (j == 0 )
679
+ Dest = LoopExit;
680
+ // If using trip count upper bound to completely unroll, we need to keep
681
+ // the conditional branch except the last one because the loop may exit
682
+ // after any iteration.
683
+ assert (NeedConditional &&
684
+ " NeedCondition cannot be modified by both complete "
685
+ " unrolling and runtime unrolling" );
686
+ NeedConditional =
687
+ (ULO.PreserveCondBr && j && !(ULO.PreserveOnlyFirst && i != 0 ));
688
+ } else if (j != BreakoutTrip &&
689
+ (ULO.TripMultiple == 0 || j % ULO.TripMultiple != 0 )) {
690
+ // If we know the trip count or a multiple of it, we can safely use an
691
+ // unconditional branch for some iterations.
692
+ NeedConditional = false ;
693
+ }
694
+
713
695
if (NeedConditional) {
714
696
// Update the conditional branch's successor for the following
715
697
// iteration.
716
698
Term->setSuccessor (!ContinueOnTrue, Dest);
717
699
} else {
718
700
// Remove phi operands at this loop exit
719
701
if (Dest != LoopExit) {
720
- BasicBlock *BB = Src ;
721
- for (BasicBlock *Succ : successors (BB)) {
722
- if (Succ == CurrentHeader )
702
+ BasicBlock *BB = Latches[i] ;
703
+ for (BasicBlock *Succ: successors (BB)) {
704
+ if (Succ == Headers[i] )
723
705
continue ;
724
706
for (PHINode &Phi : Succ->phis ())
725
707
Phi.removeIncomingValue (BB, false );
@@ -729,90 +711,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
729
711
BranchInst::Create (Dest, Term);
730
712
Term->eraseFromParent ();
731
713
}
732
- };
733
-
734
- // Now that all the basic blocks for the unrolled iterations are in place,
735
- // set up the branches to connect them.
736
- if (LatchIsExiting) {
737
- // Set up latches to branch to the new header in the unrolled iterations or
738
- // the loop exit for the last latch in a fully unrolled loop.
739
- for (unsigned i = 0 , e = Latches.size (); i != e; ++i) {
740
- // The branch destination.
741
- unsigned j = (i + 1 ) % e;
742
- BasicBlock *Dest = Headers[j];
743
- bool NeedConditional = true ;
744
-
745
- if (RuntimeTripCount && j != 0 ) {
746
- NeedConditional = false ;
747
- }
748
-
749
- // For a complete unroll, make the last iteration end with a branch
750
- // to the exit block.
751
- if (CompletelyUnroll) {
752
- if (j == 0 )
753
- Dest = LoopExit;
754
- // If using trip count upper bound to completely unroll, we need to keep
755
- // the conditional branch except the last one because the loop may exit
756
- // after any iteration.
757
- assert (NeedConditional &&
758
- " NeedCondition cannot be modified by both complete "
759
- " unrolling and runtime unrolling" );
760
- NeedConditional =
761
- (ULO.PreserveCondBr && j && !(ULO.PreserveOnlyFirst && i != 0 ));
762
- } else if (j != BreakoutTrip &&
763
- (ULO.TripMultiple == 0 || j % ULO.TripMultiple != 0 )) {
764
- // If we know the trip count or a multiple of it, we can safely use an
765
- // unconditional branch for some iterations.
766
- NeedConditional = false ;
767
- }
768
-
769
- setDest (Latches[i], Dest, Headers, Headers[i], NeedConditional);
770
- }
771
- } else {
772
- // Setup headers to branch to their new successors in the unrolled
773
- // iterations.
774
- for (unsigned i = 0 , e = Headers.size (); i != e; ++i) {
775
- // The branch destination.
776
- unsigned j = (i + 1 ) % e;
777
- BasicBlock *Dest = HeaderSucc[i];
778
- bool NeedConditional = true ;
779
-
780
- if (RuntimeTripCount && j != 0 )
781
- NeedConditional = false ;
782
-
783
- if (CompletelyUnroll)
784
- // We cannot drop the conditional branch for the last condition, as we
785
- // may have to execute the loop body depending on the condition.
786
- NeedConditional = j == 0 || ULO.PreserveCondBr ;
787
- else if (j != BreakoutTrip &&
788
- (ULO.TripMultiple == 0 || j % ULO.TripMultiple != 0 ))
789
- // If we know the trip count or a multiple of it, we can safely use an
790
- // unconditional branch for some iterations.
791
- NeedConditional = false ;
792
-
793
- setDest (Headers[i], Dest, Headers, Headers[i], NeedConditional);
794
- }
795
-
796
- // Set up latches to branch to the new header in the unrolled iterations or
797
- // the loop exit for the last latch in a fully unrolled loop.
798
-
799
- for (unsigned i = 0 , e = Latches.size (); i != e; ++i) {
800
- // The original branch was replicated in each unrolled iteration.
801
- BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator ());
802
-
803
- // The branch destination.
804
- unsigned j = (i + 1 ) % e;
805
- BasicBlock *Dest = Headers[j];
806
-
807
- // When completely unrolling, the last latch becomes unreachable.
808
- if (CompletelyUnroll && j == 0 )
809
- new UnreachableInst (Term->getContext (), Term);
810
- else
811
- // Replace the conditional branch with an unconditional one.
812
- BranchInst::Create (Dest, Term);
813
-
814
- Term->eraseFromParent ();
815
- }
816
714
}
817
715
818
716
// Update dominators of blocks we might reach through exits.
@@ -829,23 +727,19 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
829
727
ChildrenToUpdate.push_back (ChildBB);
830
728
}
831
729
BasicBlock *NewIDom;
832
- BasicBlock *&TermBlock = LatchIsExiting ? LatchBlock : Header;
833
- auto &TermBlocks = LatchIsExiting ? Latches : Headers;
834
- if (BB == TermBlock) {
730
+ if (BB == LatchBlock) {
835
731
// The latch is special because we emit unconditional branches in
836
732
// some cases where the original loop contained a conditional branch.
837
733
// Since the latch is always at the bottom of the loop, if the latch
838
734
// dominated an exit before unrolling, the new dominator of that exit
839
735
// must also be a latch. Specifically, the dominator is the first
840
736
// latch which ends in a conditional branch, or the last latch if
841
737
// there is no such latch.
842
- // For loops exiting from the header, we limit the supported loops
843
- // to have a single exiting block.
844
- NewIDom = TermBlocks.back ();
845
- for (BasicBlock *Iter : TermBlocks) {
846
- Instruction *Term = Iter->getTerminator ();
738
+ NewIDom = Latches.back ();
739
+ for (BasicBlock *IterLatch : Latches) {
740
+ Instruction *Term = IterLatch->getTerminator ();
847
741
if (isa<BranchInst>(Term) && cast<BranchInst>(Term)->isConditional ()) {
848
- NewIDom = Iter ;
742
+ NewIDom = IterLatch ;
849
743
break ;
850
744
}
851
745
}
@@ -862,17 +756,13 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
862
756
}
863
757
864
758
assert (!DT || !UnrollVerifyDomtree ||
865
- DT->verify (DominatorTree::VerificationLevel::Fast));
759
+ DT->verify (DominatorTree::VerificationLevel::Fast));
866
760
867
761
DomTreeUpdater DTU (DT, DomTreeUpdater::UpdateStrategy::Eager);
868
762
// Merge adjacent basic blocks, if possible.
869
763
for (BasicBlock *Latch : Latches) {
870
- BranchInst *Term = dyn_cast<BranchInst>(Latch->getTerminator ());
871
- assert ((Term ||
872
- (CompletelyUnroll && !LatchIsExiting && Latch == Latches.back ())) &&
873
- " Need a branch as terminator, except when fully unrolling with "
874
- " unconditional latch" );
875
- if (Term && Term->isUnconditional ()) {
764
+ BranchInst *Term = cast<BranchInst>(Latch->getTerminator ());
765
+ if (Term->isUnconditional ()) {
876
766
BasicBlock *Dest = Term->getSuccessor (0 );
877
767
BasicBlock *Fold = Dest->getUniquePredecessor ();
878
768
if (MergeBlockIntoPredecessor (Dest, &DTU, LI)) {
0 commit comments