[llvm] [IndVars] Fix high-cost-expand check in LFTR (PR #125828)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 1 02:05:26 PDT 2025
================
@@ -901,73 +902,38 @@ static PHINode *FindLoopCounter(Loop *L, BasicBlock *ExitingBB,
return BestPhi;
}
-/// Insert an IR expression which computes the value held by the IV IndVar
-/// (which must be an loop counter w/unit stride) after the backedge of loop L
-/// is taken ExitCount times.
-static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
- const SCEV *ExitCount, bool UsePostInc, Loop *L,
- SCEVExpander &Rewriter, ScalarEvolution *SE) {
- assert(isLoopCounter(IndVar, L, SE));
- assert(ExitCount->getType()->isIntegerTy() && "exit count must be integer");
- const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
- assert(AR->getStepRecurrence(*SE)->isOne() && "only handles unit stride");
-
+static const SCEV *getIVLimit(PHINode *IndVar, const SCEV *ExitCount,
+ bool UsePostInc, ScalarEvolution *SE) {
// For integer IVs, truncate the IV before computing the limit unless we
// know apriori that the limit must be a constant when evaluated in the
// bitwidth of the IV. We prefer (potentially) keeping a truncate of the
// IV in the loop over a (potentially) expensive expansion of the widened
// exit count add(zext(add)) expression.
+ const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
+ assert(AR->getStepRecurrence(*SE)->isOne() && "only handles unit stride");
if (IndVar->getType()->isIntegerTy() &&
SE->getTypeSizeInBits(AR->getType()) >
- SE->getTypeSizeInBits(ExitCount->getType())) {
+ SE->getTypeSizeInBits(ExitCount->getType())) {
const SCEV *IVInit = AR->getStart();
if (!isa<SCEVConstant>(IVInit) || !isa<SCEVConstant>(ExitCount))
AR = cast<SCEVAddRecExpr>(SE->getTruncateExpr(AR, ExitCount->getType()));
}
-
- const SCEVAddRecExpr *ARBase = UsePostInc ? AR->getPostIncExpr(*SE) : AR;
- const SCEV *IVLimit = ARBase->evaluateAtIteration(ExitCount, *SE);
- assert(SE->isLoopInvariant(IVLimit, L) &&
- "Computed iteration count is not loop invariant!");
- return Rewriter.expandCodeFor(IVLimit, ARBase->getType(),
- ExitingBB->getTerminator());
+ AR = UsePostInc ? AR->getPostIncExpr(*SE) : AR;
+ return AR->evaluateAtIteration(ExitCount, *SE);
}
/// This method rewrites the exit condition of the loop to be a canonical !=
/// comparison against the incremented loop induction variable. This pass is
/// able to rewrite the exit tests of any loop where the SCEV analysis can
/// determine a loop-invariant trip count of the loop, which is actually a much
/// broader range than just linear tests.
-bool IndVarSimplify::
-linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
- const SCEV *ExitCount,
- PHINode *IndVar, SCEVExpander &Rewriter) {
- assert(L->getLoopLatch() && "Loop no longer in simplified form?");
+bool IndVarSimplify::linearFunctionTestReplace(
+ Loop *L, BasicBlock *ExitingBB, const SCEV *ExitCount, PHINode *IndVar,
----------------
artagnon wrote:
Yes, I think it is okay to strip non-critical usages.
https://github.com/llvm/llvm-project/pull/125828
More information about the llvm-commits
mailing list