@@ -248,7 +248,7 @@ class InductiveRangeCheckElimination {
248248
249249 // Returns true if it is profitable to do a transform basing on estimation of
250250 // number of iterations.
251- bool isProfitableToTransform (const Loop &L, LoopStructure &LS );
251+ bool isProfitableToTransform (const Loop &L);
252252
253253public:
254254 InductiveRangeCheckElimination (ScalarEvolution &SE,
@@ -938,14 +938,12 @@ PreservedAnalyses IRCEPass::run(Function &F, FunctionAnalysisManager &AM) {
938938 return getLoopPassPreservedAnalyses ();
939939}
940940
941- bool
942- InductiveRangeCheckElimination::isProfitableToTransform (const Loop &L,
943- LoopStructure &LS) {
941+ bool InductiveRangeCheckElimination::isProfitableToTransform (const Loop &L) {
944942 if (SkipProfitabilityChecks)
945943 return true ;
946944 if (GetBFI) {
947945 BlockFrequencyInfo &BFI = (*GetBFI)();
948- uint64_t hFreq = BFI.getBlockFreq (LS. Header ).getFrequency ();
946+ uint64_t hFreq = BFI.getBlockFreq (L. getHeader () ).getFrequency ();
949947 uint64_t phFreq = BFI.getBlockFreq (L.getLoopPreheader ()).getFrequency ();
950948 if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
951949 LLVM_DEBUG (dbgs () << " irce: could not prove profitability: "
@@ -958,8 +956,17 @@ InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,
958956
959957 if (!BPI)
960958 return true ;
959+
960+ auto *Latch = L.getLoopLatch ();
961+ if (!Latch)
962+ return true ;
963+ auto *LatchBr = dyn_cast<BranchInst>(Latch->getTerminator ());
964+ if (!LatchBr)
965+ return true ;
966+ auto LatchBrExitIdx = LatchBr->getSuccessor (0 ) == L.getHeader () ? 1 : 0 ;
967+
961968 BranchProbability ExitProbability =
962- BPI->getEdgeProbability (LS. Latch , LS. LatchBrExitIdx );
969+ BPI->getEdgeProbability (Latch, LatchBrExitIdx);
963970 if (ExitProbability > BranchProbability (1 , MinRuntimeIterations)) {
964971 LLVM_DEBUG (dbgs () << " irce: could not prove profitability: "
965972 << " the exit probability is too big " << ExitProbability
@@ -982,6 +989,9 @@ bool InductiveRangeCheckElimination::run(
982989 return false ;
983990 }
984991
992+ if (!isProfitableToTransform (*L))
993+ return false ;
994+
985995 LLVMContext &Context = Preheader->getContext ();
986996 SmallVector<InductiveRangeCheck, 16 > RangeChecks;
987997 bool Changed = false ;
@@ -1017,8 +1027,6 @@ bool InductiveRangeCheckElimination::run(
10171027 return Changed;
10181028 }
10191029 LoopStructure LS = *MaybeLoopStructure;
1020- if (!isProfitableToTransform (*L, LS))
1021- return Changed;
10221030 const SCEVAddRecExpr *IndVar =
10231031 cast<SCEVAddRecExpr>(SE.getMinusSCEV (SE.getSCEV (LS.IndVarBase ), SE.getSCEV (LS.IndVarStep )));
10241032
0 commit comments