@@ -149,7 +149,11 @@ class IndVarSimplify {
149
149
bool rewriteNonIntegerIVs (Loop *L);
150
150
151
151
bool simplifyAndExtend (Loop *L, SCEVExpander &Rewriter, LoopInfo *LI);
152
+ // / Try to eliminate loop exits based on analyzeable exit counts
152
153
bool optimizeLoopExits (Loop *L, SCEVExpander &Rewriter);
154
+ // / Try to form loop invariant tests for loop exits by changing how many
155
+ // / iterations of the loop run when that is unobservable.
156
+ bool predicateLoopExits (Loop *L, SCEVExpander &Rewriter);
153
157
154
158
bool canLoopBeDeleted (Loop *L, SmallVector<RewritePhi, 8 > &RewritePhiSet);
155
159
bool rewriteLoopExitValues (Loop *L, SCEVExpander &Rewriter);
@@ -2680,32 +2684,46 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
2680
2684
SmallVector<BasicBlock*, 16 > ExitingBlocks;
2681
2685
L->getExitingBlocks (ExitingBlocks);
2682
2686
2683
- // Get a symbolic upper bound on the loop backedge taken count.
2684
- const SCEV *MaxExitCount = getMaxBackedgeTakenCount (*SE, *DT, L);
2685
- if (isa<SCEVCouldNotCompute>(MaxExitCount))
2686
- return false ;
2687
-
2688
- bool Changed = false ;
2689
- for (BasicBlock *ExitingBB : ExitingBlocks) {
2687
+ // Remove all exits which aren't both rewriteable and analyzeable.
2688
+ auto NewEnd = llvm::remove_if (ExitingBlocks,
2689
+ [&](BasicBlock *ExitingBB) {
2690
2690
// If our exitting block exits multiple loops, we can only rewrite the
2691
2691
// innermost one. Otherwise, we're changing how many times the innermost
2692
2692
// loop runs before it exits.
2693
2693
if (LI->getLoopFor (ExitingBB) != L)
2694
- continue ;
2694
+ return true ;
2695
2695
2696
2696
// Can't rewrite non-branch yet.
2697
2697
BranchInst *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator ());
2698
2698
if (!BI)
2699
- continue ;
2699
+ return true ;
2700
2700
2701
2701
// If already constant, nothing to do.
2702
2702
if (isa<Constant>(BI->getCondition ()))
2703
- continue ;
2703
+ return true ;
2704
2704
2705
2705
const SCEV *ExitCount = SE->getExitCount (L, ExitingBB);
2706
2706
if (isa<SCEVCouldNotCompute>(ExitCount))
2707
- continue ;
2707
+ return true ;
2708
+ return false ;
2709
+ });
2710
+ ExitingBlocks.erase (NewEnd, ExitingBlocks.end ());
2711
+
2712
+ if (ExitingBlocks.empty ())
2713
+ return false ;
2714
+
2715
+ // Get a symbolic upper bound on the loop backedge taken count.
2716
+ const SCEV *MaxExitCount = getMaxBackedgeTakenCount (*SE, *DT, L);
2717
+ if (isa<SCEVCouldNotCompute>(MaxExitCount))
2718
+ return false ;
2708
2719
2720
+ bool Changed = false ;
2721
+ for (BasicBlock *ExitingBB : ExitingBlocks) {
2722
+ BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator ());
2723
+
2724
+ const SCEV *ExitCount = SE->getExitCount (L, ExitingBB);
2725
+ assert (!isa<SCEVCouldNotCompute>(ExitCount) && " checked above" );
2726
+
2709
2727
// If we know we'd exit on the first iteration, rewrite the exit to
2710
2728
// reflect this. This does not imply the loop must exit through this
2711
2729
// exit; there may be an earlier one taken on the first iteration.
@@ -2756,6 +2774,14 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
2756
2774
// the loop, then we can discharge all other exits. (May fall out of
2757
2775
// previous TODO.)
2758
2776
}
2777
+ return Changed;
2778
+ }
2779
+
2780
+ bool IndVarSimplify::predicateLoopExits (Loop *L, SCEVExpander &Rewriter) {
2781
+ SmallVector<BasicBlock*, 16 > ExitingBlocks;
2782
+ L->getExitingBlocks (ExitingBlocks);
2783
+
2784
+ bool Changed = false ;
2759
2785
2760
2786
// Finally, see if we can rewrite our exit conditions into a loop invariant
2761
2787
// form. If we have a read-only loop, and we can tell that we must exit down
@@ -2971,7 +2997,12 @@ bool IndVarSimplify::run(Loop *L) {
2971
2997
// Eliminate redundant IV cycles.
2972
2998
NumElimIV += Rewriter.replaceCongruentIVs (L, DT, DeadInsts);
2973
2999
3000
+ // Try to eliminate loop exits based on analyzeable exit counts
2974
3001
Changed |= optimizeLoopExits (L, Rewriter);
3002
+
3003
+ // Try to form loop invariant tests for loop exits by changing how many
3004
+ // iterations of the loop run when that is unobservable.
3005
+ Changed |= predicateLoopExits (L, Rewriter);
2975
3006
2976
3007
// If we have a trip count expression, rewrite the loop's exit condition
2977
3008
// using it.
0 commit comments