Skip to content

Commit 78db4e9

Browse files
authored
[NFC][IRCE] Don't require LoopStructure to determine IRCE profitability (#116384)
This refactoring hoists the profitability check earlier in the pipeline, so that for loops that are not profitable to transform there is no iteration over the basic blocks or LoopStructure computation. Motivated by PR #104659 that tweaks how the profitability of individual branches is evaluated.
1 parent 2202f0e commit 78db4e9

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

253253
public:
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

Comments
 (0)