@@ -248,7 +248,7 @@ class InductiveRangeCheckElimination {
248
248
249
249
// Returns true if it is profitable to do a transform basing on estimation of
250
250
// number of iterations.
251
- bool isProfitableToTransform (const Loop &L, LoopStructure &LS );
251
+ bool isProfitableToTransform (const Loop &L);
252
252
253
253
public:
254
254
InductiveRangeCheckElimination (ScalarEvolution &SE,
@@ -938,14 +938,12 @@ PreservedAnalyses IRCEPass::run(Function &F, FunctionAnalysisManager &AM) {
938
938
return getLoopPassPreservedAnalyses ();
939
939
}
940
940
941
- bool
942
- InductiveRangeCheckElimination::isProfitableToTransform (const Loop &L,
943
- LoopStructure &LS) {
941
+ bool InductiveRangeCheckElimination::isProfitableToTransform (const Loop &L) {
944
942
if (SkipProfitabilityChecks)
945
943
return true ;
946
944
if (GetBFI) {
947
945
BlockFrequencyInfo &BFI = (*GetBFI)();
948
- uint64_t hFreq = BFI.getBlockFreq (LS. Header ).getFrequency ();
946
+ uint64_t hFreq = BFI.getBlockFreq (L. getHeader () ).getFrequency ();
949
947
uint64_t phFreq = BFI.getBlockFreq (L.getLoopPreheader ()).getFrequency ();
950
948
if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
951
949
LLVM_DEBUG (dbgs () << " irce: could not prove profitability: "
@@ -958,8 +956,17 @@ InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,
958
956
959
957
if (!BPI)
960
958
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
+
961
968
BranchProbability ExitProbability =
962
- BPI->getEdgeProbability (LS. Latch , LS. LatchBrExitIdx );
969
+ BPI->getEdgeProbability (Latch, LatchBrExitIdx);
963
970
if (ExitProbability > BranchProbability (1 , MinRuntimeIterations)) {
964
971
LLVM_DEBUG (dbgs () << " irce: could not prove profitability: "
965
972
<< " the exit probability is too big " << ExitProbability
@@ -982,6 +989,9 @@ bool InductiveRangeCheckElimination::run(
982
989
return false ;
983
990
}
984
991
992
+ if (!isProfitableToTransform (*L))
993
+ return false ;
994
+
985
995
LLVMContext &Context = Preheader->getContext ();
986
996
SmallVector<InductiveRangeCheck, 16 > RangeChecks;
987
997
bool Changed = false ;
@@ -1017,8 +1027,6 @@ bool InductiveRangeCheckElimination::run(
1017
1027
return Changed;
1018
1028
}
1019
1029
LoopStructure LS = *MaybeLoopStructure;
1020
- if (!isProfitableToTransform (*L, LS))
1021
- return Changed;
1022
1030
const SCEVAddRecExpr *IndVar =
1023
1031
cast<SCEVAddRecExpr>(SE.getMinusSCEV (SE.getSCEV (LS.IndVarBase ), SE.getSCEV (LS.IndVarStep )));
1024
1032
0 commit comments