[llvm] 272ebd6 - [LSR] Inline getAlternateIVEnd and simplify [nfc]
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 20 11:22:30 PDT 2023
Author: Philip Reames
Date: 2023-03-20T11:22:21-07:00
New Revision: 272ebd6957ef7bd39a6c6d2aaf7249d86e09791b
URL: https://github.com/llvm/llvm-project/commit/272ebd6957ef7bd39a6c6d2aaf7249d86e09791b
DIFF: https://github.com/llvm/llvm-project/commit/272ebd6957ef7bd39a6c6d2aaf7249d86e09791b.diff
LOG: [LSR] Inline getAlternateIVEnd and simplify [nfc]
Also, add a comment to highlight that the "good" result on this test is accidental, and not based on a principled decision. I matched the original behavior to make this nfc, but selecting the last legal IV is not well motivated here.
Added:
Modified:
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold-negative-testcase.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 5d8e822eaddf..e76ba2da2212 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6737,35 +6737,12 @@ canFoldTermCondOfLoop(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
if (!isAlmostDeadIV(ToFold, LoopLatch, TermCond))
return std::nullopt;
- // If this is an IV which we could replace the terminating condition, return
- // the final value of the alternative IV on the last iteration.
- auto getAlternateIVEnd = [&](PHINode &PN) -> const SCEV * {
- // FIXME: This does not properly account for overflow.
- const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(SE.getSCEV(&PN));
- const SCEV *BECount = SE.getBackedgeTakenCount(L);
- const SCEV *TermValueS = SE.getAddExpr(
- AddRec->getOperand(0),
- SE.getTruncateOrZeroExtend(
- SE.getMulExpr(
- AddRec->getOperand(1),
- SE.getTruncateOrZeroExtend(
- SE.getAddExpr(BECount, SE.getOne(BECount->getType())),
- AddRec->getOperand(1)->getType())),
- AddRec->getOperand(0)->getType()));
- const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
- SCEVExpander Expander(SE, DL, "lsr_fold_term_cond");
- if (!Expander.isSafeToExpand(TermValueS)) {
- LLVM_DEBUG(
- dbgs() << "Is not safe to expand terminating value for phi node" << PN
- << "\n");
- return nullptr;
- }
- return TermValueS;
- };
+ const SCEV *BECount = SE.getBackedgeTakenCount(L);
+ const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
+ SCEVExpander Expander(SE, DL, "lsr_fold_term_cond");
PHINode *ToHelpFold = nullptr;
const SCEV *TermValueS = nullptr;
-
for (PHINode &PN : L->getHeader()->phis()) {
if (ToFold == &PN)
continue;
@@ -6785,10 +6762,26 @@ canFoldTermCondOfLoop(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
continue;
}
- if (auto P = getAlternateIVEnd(PN)) {
- ToHelpFold = &PN;
- TermValueS = P;
+ // FIXME: This does not properly account for overflow.
+ const SCEV *TermValueSLocal = SE.getAddExpr(
+ AddRec->getOperand(0),
+ SE.getTruncateOrZeroExtend(
+ SE.getMulExpr(
+ AddRec->getOperand(1),
+ SE.getTruncateOrZeroExtend(
+ SE.getAddExpr(BECount, SE.getOne(BECount->getType())),
+ AddRec->getOperand(1)->getType())),
+ AddRec->getOperand(0)->getType()));
+ if (!Expander.isSafeToExpand(TermValueSLocal)) {
+ LLVM_DEBUG(
+ dbgs() << "Is not safe to expand terminating value for phi node" << PN
+ << "\n");
+ continue;
}
+ // We pick the last legal alternate IV. We could expore choosing an optimal
+ // alternate IV if we had a decent heuristic to do so.
+ ToHelpFold = &PN;
+ TermValueS = TermValueSLocal;
}
LLVM_DEBUG(if (ToFold && !ToHelpFold) dbgs()
diff --git a/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold-negative-testcase.ll b/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold-negative-testcase.ll
index 1b9b58f79b48..e7a8acb82c20 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold-negative-testcase.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold-negative-testcase.ll
@@ -211,6 +211,9 @@ for.end: ; preds = %for.body
ret void
}
+; After LSR, there are three IVs in this loop. As a result, we have two
+; alternate IVs to chose from. At the moment, we chose the last, but this
+; is somewhat arbitrary.
define void @TermCondMoreThanOneUse(ptr %a) {
; CHECK-LABEL: define void @TermCondMoreThanOneUse
; CHECK-SAME: (ptr [[A:%.*]]) {
More information about the llvm-commits
mailing list