Skip to content

[NFC][IRCE] Don't require LoopStructure to determine IRCE profitability #116384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class InductiveRangeCheckElimination {

// Returns true if it is profitable to do a transform basing on estimation of
// number of iterations.
bool isProfitableToTransform(const Loop &L, LoopStructure &LS);
bool isProfitableToTransform(const Loop &L);

public:
InductiveRangeCheckElimination(ScalarEvolution &SE,
Expand Down Expand Up @@ -938,14 +938,12 @@ PreservedAnalyses IRCEPass::run(Function &F, FunctionAnalysisManager &AM) {
return getLoopPassPreservedAnalyses();
}

bool
InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,
LoopStructure &LS) {
bool InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L) {
if (SkipProfitabilityChecks)
return true;
if (GetBFI) {
BlockFrequencyInfo &BFI = (*GetBFI)();
uint64_t hFreq = BFI.getBlockFreq(LS.Header).getFrequency();
uint64_t hFreq = BFI.getBlockFreq(L.getHeader()).getFrequency();
uint64_t phFreq = BFI.getBlockFreq(L.getLoopPreheader()).getFrequency();
if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
Expand All @@ -958,8 +956,17 @@ InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,

if (!BPI)
return true;

auto *Latch = L.getLoopLatch();
if (!Latch)
return true;
auto *LatchBr = dyn_cast<BranchInst>(Latch->getTerminator());
if (!LatchBr)
return true;
auto LatchBrExitIdx = LatchBr->getSuccessor(0) == L.getHeader() ? 1 : 0;

BranchProbability ExitProbability =
BPI->getEdgeProbability(LS.Latch, LS.LatchBrExitIdx);
BPI->getEdgeProbability(Latch, LatchBrExitIdx);
if (ExitProbability > BranchProbability(1, MinRuntimeIterations)) {
LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
<< "the exit probability is too big " << ExitProbability
Expand All @@ -982,6 +989,9 @@ bool InductiveRangeCheckElimination::run(
return false;
}

if (!isProfitableToTransform(*L))
return false;

LLVMContext &Context = Preheader->getContext();
SmallVector<InductiveRangeCheck, 16> RangeChecks;
bool Changed = false;
Expand Down Expand Up @@ -1017,8 +1027,6 @@ bool InductiveRangeCheckElimination::run(
return Changed;
}
LoopStructure LS = *MaybeLoopStructure;
if (!isProfitableToTransform(*L, LS))
return Changed;
const SCEVAddRecExpr *IndVar =
cast<SCEVAddRecExpr>(SE.getMinusSCEV(SE.getSCEV(LS.IndVarBase), SE.getSCEV(LS.IndVarStep)));

Expand Down
Loading