llvm.org GIT mirror llvm / d406311
[IndVars] Use exit count reasoning to discharge obviously untaken exits Continue in the spirit of D63618, and use exit count reasoning to prove away loop exits which can not be taken since the backedge taken count of the loop as a whole is provably less than the minimal BE count required to take this particular loop exit. As demonstrated in the newly added tests, this triggers in a number of cases where IndVars was previously unable to discharge obviously redundant exit tests. And some not so obvious ones. Differential Revision: https://reviews.llvm.org/D63733 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365920 91177308-0d34-0410-b5e6-96231b3b80d8 Philip Reames 2 months ago
2 changed file(s) with 113 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
143143 bool rewriteNonIntegerIVs(Loop *L);
144144
145145 bool simplifyAndExtend(Loop *L, SCEVExpander &Rewriter, LoopInfo *LI);
146 bool optimizeLoopExits(Loop *L);
146147
147148 bool canLoopBeDeleted(Loop *L, SmallVector &RewritePhiSet);
148149 bool rewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
26222623 return MadeAnyChanges;
26232624 }
26242625
2626 bool IndVarSimplify::optimizeLoopExits(Loop *L) {
2627 SmallVector ExitingBlocks;
2628 L->getExitingBlocks(ExitingBlocks);
2629 BasicBlock * const Latch = L->getLoopLatch();
2630
2631 // Form an expression for the maximum exit count possible for this loop. We
2632 // merge the max and exact information to approximate a version of
2633 // getMaxBackedgeTakenInfo which isn't restricted to just constants.
2634 // TODO: factor this out as a version of getMaxBackedgeTakenCount which
2635 // isn't guaranteed to return a constant.
2636 SmallVector ExitCounts;
2637 const SCEV *MaxConstEC = SE->getMaxBackedgeTakenCount(L);
2638 if (!isa(MaxConstEC))
2639 ExitCounts.push_back(MaxConstEC);
2640 for (BasicBlock *ExitingBB : ExitingBlocks) {
2641 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
2642 if (!isa(ExitCount)) {
2643 assert(DT->dominates(ExitingBB, Latch) &&
2644 "We should only have known counts for exiting blocks that "
2645 "dominate latch!");
2646 ExitCounts.push_back(ExitCount);
2647 }
2648 }
2649 if (ExitCounts.empty())
2650 return false;
2651 const SCEV *MaxExitCount = SE->getUMinFromMismatchedTypes(ExitCounts);
2652
2653 bool Changed = false;
2654 for (BasicBlock *ExitingBB : ExitingBlocks) {
2655 // If our exitting block exits multiple loops, we can only rewrite the
2656 // innermost one. Otherwise, we're changing how many times the innermost
2657 // loop runs before it exits.
2658 if (LI->getLoopFor(ExitingBB) != L)
2659 continue;
2660
2661 // Can't rewrite non-branch yet.
2662 BranchInst *BI = dyn_cast(ExitingBB->getTerminator());
2663 if (!BI)
2664 continue;
2665
2666 // If already constant, nothing to do.
2667 if (isa(BI->getCondition()))
2668 continue;
2669
2670 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
2671 if (isa(ExitCount))
2672 continue;
2673
2674 // If we know we'd exit on the first iteration, rewrite the exit to
2675 // reflect this. This does not imply the loop must exit through this
2676 // exit; there may be an earlier one taken on the first iteration.
2677 // TODO: Given we know the backedge can't be taken, we should go ahead
2678 // and break it. Or at least, kill all the header phis and simplify.
2679 if (ExitCount->isZero()) {
2680 bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
2681 auto *OldCond = BI->getCondition();
2682 auto *NewCond = ExitIfTrue ? ConstantInt::getTrue(OldCond->getType()) :
2683 ConstantInt::getFalse(OldCond->getType());
2684 BI->setCondition(NewCond);
2685 if (OldCond->use_empty())
2686 DeadInsts.push_back(OldCond);
2687 Changed = true;
2688 continue;
2689 }
2690
2691 // If we end up with a pointer exit count, bail.
2692 if (!ExitCount->getType()->isIntegerTy() ||
2693 !MaxExitCount->getType()->isIntegerTy())
2694 return false;
2695
2696 Type *WiderType =
2697 SE->getWiderType(MaxExitCount->getType(), ExitCount->getType());
2698 ExitCount = SE->getNoopOrZeroExtend(ExitCount, WiderType);
2699 MaxExitCount = SE->getNoopOrZeroExtend(MaxExitCount, WiderType);
2700 assert(MaxExitCount->getType() == ExitCount->getType());
2701
2702 // Can we prove that some other exit must be taken strictly before this
2703 // one? TODO: handle cases where ule is known, and equality is covered
2704 // by a dominating exit
2705 if (SE->isLoopEntryGuardedByCond(L, CmpInst::ICMP_ULT,
2706 MaxExitCount, ExitCount)) {
2707 bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
2708 auto *OldCond = BI->getCondition();
2709 auto *NewCond = ExitIfTrue ? ConstantInt::getFalse(OldCond->getType()) :
2710 ConstantInt::getTrue(OldCond->getType());
2711 BI->setCondition(NewCond);
2712 if (OldCond->use_empty())
2713 DeadInsts.push_back(OldCond);
2714 Changed = true;
2715 continue;
2716 }
2717
2718 // TODO: If we can prove that the exiting iteration is equal to the exit
2719 // count for this exit and that no previous exit oppurtunities exist within
2720 // the loop, then we can discharge all other exits. (May fall out of
2721 // previous TODO.)
2722
2723 // TODO: If we can't prove any relation between our exit count and the
2724 // loops exit count, but taking this exit doesn't require actually running
2725 // the loop (i.e. no side effects, no computed values used in exit), then
2726 // we can replace the exit test with a loop invariant test which exits on
2727 // the first iteration.
2728 }
2729 return Changed;
2730 }
2731
26252732 //===----------------------------------------------------------------------===//
26262733 // IndVarSimplify driver. Manage several subpasses of IV simplification.
26272734 //===----------------------------------------------------------------------===//
26782785 // Eliminate redundant IV cycles.
26792786 NumElimIV += Rewriter.replaceCongruentIVs(L, DT, DeadInsts);
26802787
2788 Changed |= optimizeLoopExits(L);
2789
26812790 // If we have a trip count expression, rewrite the loop's exit condition
26822791 // using it.
26832792 if (!DisableLFTR) {
27012810 if (isa(ExitCount))
27022811 continue;
27032812
2704 // If we know we'd exit on the first iteration, rewrite the exit to
2705 // reflect this. This does not imply the loop must exit through this
2706 // exit; there may be an earlier one taken on the first iteration.
2707 // TODO: Given we know the backedge can't be taken, we should go ahead
2708 // and break it. Or at least, kill all the header phis and simplify.
2709 if (ExitCount->isZero()) {
2710 auto *BI = cast(ExitingBB->getTerminator());
2711 bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
2712 auto *OldCond = BI->getCondition();
2713 auto *NewCond = ExitIfTrue ? ConstantInt::getTrue(OldCond->getType()) :
2714 ConstantInt::getFalse(OldCond->getType());
2715 BI->setCondition(NewCond);
2716 if (OldCond->use_empty())
2717 DeadInsts.push_back(OldCond);
2718 Changed = true;
2719 continue;
2720 }
2813 assert(!ExitCount->isZero() && "Should have been folded above");
27212814
27222815 PHINode *IndVar = FindLoopCounter(L, ExitingBB, ExitCount, SE, DT);
27232816 if (!IndVar)
1414 ; CHECK-NEXT: br i1 [[CMP1]], label [[LATCH]], label [[EXIT_LOOPEXIT:%.*]]
1515 ; CHECK: latch:
1616 ; CHECK-NEXT: call void @side_effect()
17 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i64 [[IV]], [[M]]
18 ; CHECK-NEXT: br i1 [[CMP2]], label [[LOOP]], label [[EXIT_LOOPEXIT]]
17 ; CHECK-NEXT: br i1 true, label [[LOOP]], label [[EXIT_LOOPEXIT]]
1918 ; CHECK: exit.loopexit:
2019 ; CHECK-NEXT: br label [[EXIT]]
2120 ; CHECK: exit:
4746 ; CHECK: loop:
4847 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[LATCH:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
4948 ; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
50 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i64 [[IV]], [[N]]
51 ; CHECK-NEXT: br i1 [[CMP1]], label [[LATCH]], label [[EXIT_LOOPEXIT:%.*]]
49 ; CHECK-NEXT: br i1 true, label [[LATCH]], label [[EXIT_LOOPEXIT:%.*]]
5250 ; CHECK: latch:
5351 ; CHECK-NEXT: call void @side_effect()
5452 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i64 [[IV]], [[M]]
159157 ; CHECK: loop:
160158 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[LATCH:%.*]] ], [ 0, [[LOOP_PREHEADER]] ]
161159 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
162 ; CHECK-NEXT: [[UDIV:%.*]] = udiv i64 [[IV]], 10
163 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i64 [[UDIV]], 2
164 ; CHECK-NEXT: br i1 [[CMP1]], label [[LATCH]], label [[EXIT_LOOPEXIT:%.*]]
160 ; CHECK-NEXT: br i1 true, label [[LATCH]], label [[EXIT_LOOPEXIT:%.*]]
165161 ; CHECK: latch:
166162 ; CHECK-NEXT: call void @side_effect()
167163 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i64 [[IV]], [[N]]