[llvm] 6b3bdfe - [SCEV] Collect loop guards at most once in howManyLessThans (NFC). (#228687)

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 04:11:00 PDT 2026


Author: Florian Hahn
Date: 2026-10-03T11:10:53Z
New Revision: 6b3bdfea98b4dc21a45eb4f2b7b782d57f1c078a

URL: https://github.com/llvm/llvm-project/commit/6b3bdfea98b4dc21a45eb4f2b7b782d57f1c078a
DIFF: https://github.com/llvm/llvm-project/commit/6b3bdfea98b4dc21a45eb4f2b7b782d57f1c078a.diff

LOG: [SCEV] Collect loop guards at most once in howManyLessThans (NFC). (#228687)

Add helper to collect loop guards once in howManyLessThans to avoid
repeated collections to limit compile-time increases if collection gets
more expensive (https://github.com/llvm/llvm-project/pull/228135) and
additional uses of loop guards are added to howManyLessThans (follow up)

Compile-time impact currently is in the noise

https://llvm-compile-time-tracker.com/compare.php?from=f81414dbe2ba478fa489730079263dbd546d18c0&to=171115fa4fa837b7ad9f9a6398e929a1a5866350&stat=instructions:u

PR: https://github.com/llvm/llvm-project/pull/228687

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 530b1400ae69e..0cdfd4f5c8933 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13369,6 +13369,14 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
                                   bool ControlsOnlyExit, bool AllowPredicates) {
   SmallVector<const SCEVPredicate *> Predicates;
 
+  // Loop guards for L, collected on demand.
+  std::optional<LoopGuards> CachedGuards;
+  auto getGuards = [&]() -> const LoopGuards & {
+    if (!CachedGuards)
+      CachedGuards.emplace(LoopGuards::collect(L, *this));
+    return *CachedGuards;
+  };
+
   // FIXME: Extend the non-invariant RHS analysis to greater-than comparisons.
   if (Invert && !isLoopInvariant(RHS, L))
     return getCouldNotCompute();
@@ -13405,7 +13413,8 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
           APInt StrideMax = getUnsignedRangeMax(AR->getStepRecurrence(*this));
           APInt Limit = APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1);
           Limit = Limit.zext(OuterBitWidth);
-          return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit);
+          return getUnsignedRangeMax(applyLoopGuards(RHS, getGuards()))
+              .ule(Limit);
         };
         auto Flags = AR->getNoWrapFlags();
         if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW())
@@ -13468,7 +13477,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
   bool PositiveStride = isKnownPositive(Stride);
   // A dominating guard may prove the stride positive.
   if (!PositiveStride) {
-    const SCEV *LoopGuardedStride = applyLoopGuards(Stride, L);
+    const SCEV *LoopGuardedStride = applyLoopGuards(Stride, getGuards());
     if (isKnownPositive(LoopGuardedStride)) {
       GuardedStride = LoopGuardedStride;
       PositiveStride = true;
@@ -13796,8 +13805,8 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
         };
 
         auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
-        const SCEV *GuardedRHS = applyLoopGuards(OrigRHS, L);
-        const SCEV *GuardedStart = applyLoopGuards(OrigStart, L);
+        const SCEV *GuardedRHS = applyLoopGuards(OrigRHS, getGuards());
+        const SCEV *GuardedStart = applyLoopGuards(OrigStart, getGuards());
         if (Invert)
           std::swap(GuardedRHS, GuardedStart);
 


        


More information about the llvm-commits mailing list