[llvm-branch-commits] [llvm] [LoopUtils] Factor out the same-step check of runtime check bounds (NFC) (PR #243835)
Valeriy Savchenko via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 18 04:52:09 PST 2026
https://github.com/SavchenkoValeriy created https://github.com/llvm/llvm-project/pull/243835
The follow-up change uses four SCEV expressions for this type of change
so it also generalizes it to take an array.
---
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
>From aeac36898fe21ca371103843901ecd1d530e55ac Mon Sep 17 00:00:00 2001
From: Valeriy Savchenko <vsavchenko at apple.com>
Date: Sun, 15 Nov 2026 11:39:49 +0000
Subject: [PATCH] [LoopUtils] Factor out the same-step check of runtime check
bounds (NFC)
The follow-up change uses four SCEV expressions for this type of change
so it also generalizes it to take an array.
---
llvm/lib/Transforms/Utils/LoopUtils.cpp | 65 ++++++++++++++-----------
1 file changed, 37 insertions(+), 28 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 3a7c7f8825f38..cefd05ce4413e 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -2169,6 +2169,21 @@ struct PointerBounds {
Value *StrideToCheck;
};
+/// Return true if all of \p Exprs are AddRecs of \p L with the same step.
+static bool haveSameStepIn(ArrayRef<const SCEV *> Exprs, const Loop *L,
+ ScalarEvolution &SE) {
+ const SCEV *Step = nullptr;
+ return all_of(Exprs, [&](const SCEV *S) {
+ auto *AR = dyn_cast<SCEVAddRecExpr>(S);
+ if (!AR || AR->getLoop() != L)
+ return false;
+ const SCEV *ARStep = AR->getStepRecurrence(SE);
+ if (!Step)
+ Step = ARStep;
+ return ARStep == Step;
+ });
+}
+
/// Return the bounds of \p CG in \p TheLoop. If \p HoistRuntimeChecks is true,
/// bounds varying in the parent loop of \p TheLoop may be widened to cover the
/// whole parent loop, so that checks using them can be hoisted out of it.
@@ -2187,34 +2202,28 @@ getGroupBounds(const RuntimeCheckingPtrGroup *CG, const Loop *TheLoop,
// whereas using a restricted range check could have allowed us to enter at
// least once. This is why the behaviour is not currently the default and is
// controlled by the parameter 'HoistRuntimeChecks'.
- if (HoistRuntimeChecks && TheLoop->getParentLoop() &&
- isa<SCEVAddRecExpr>(High) && isa<SCEVAddRecExpr>(Low)) {
- auto *HighAR = cast<SCEVAddRecExpr>(High);
- auto *LowAR = cast<SCEVAddRecExpr>(Low);
- const Loop *OuterLoop = TheLoop->getParentLoop();
- const SCEV *Recur = LowAR->getStepRecurrence(SE);
- if (Recur == HighAR->getStepRecurrence(SE) &&
- HighAR->getLoop() == OuterLoop && LowAR->getLoop() == OuterLoop) {
- BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
- const SCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
- if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
- OuterExitCount->getType()->isIntegerTy()) {
- const SCEV *NewHigh =
- cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
- if (!isa<SCEVCouldNotCompute>(NewHigh)) {
- LLVM_DEBUG(dbgs() << "LAA: Expanded RT check for range to include "
- "outer loop in order to permit hoisting\n");
- High = NewHigh;
- Low = cast<SCEVAddRecExpr>(Low)->getStart();
- // If there is a possibility that the stride is negative then we have
- // to generate extra checks to ensure the stride is positive.
- if (!SE.isKnownNonNegative(
- SE.applyLoopGuards(Recur, HighAR->getLoop()))) {
- Stride = Recur;
- LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
- "positive: "
- << *Stride << '\n');
- }
+ const Loop *OuterLoop = TheLoop->getParentLoop();
+ if (HoistRuntimeChecks && OuterLoop &&
+ haveSameStepIn({Low, High}, OuterLoop, SE)) {
+ const SCEV *Recur = cast<SCEVAddRecExpr>(Low)->getStepRecurrence(SE);
+ BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
+ const SCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
+ if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
+ OuterExitCount->getType()->isIntegerTy()) {
+ const SCEV *NewHigh =
+ cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
+ if (!isa<SCEVCouldNotCompute>(NewHigh)) {
+ LLVM_DEBUG(dbgs() << "LAA: Expanded RT check for range to include "
+ "outer loop in order to permit hoisting\n");
+ High = NewHigh;
+ Low = cast<SCEVAddRecExpr>(Low)->getStart();
+ // If there is a possibility that the stride is negative then we have
+ // to generate extra checks to ensure the stride is positive.
+ if (!SE.isKnownNonNegative(SE.applyLoopGuards(Recur, OuterLoop))) {
+ Stride = Recur;
+ LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
+ "positive: "
+ << *Stride << '\n');
}
}
}
More information about the llvm-branch-commits
mailing list