[llvm] [SCEV] Update comment for check BE formula in howManyLT (NFC) (PR #219170)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 03:35:58 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

Update outdate comments around the code for picking the check BE formula. It includes the following updates:

* define End = max(RHS,Start), as used later, use instead of incorrect max(End,Start)
* precondition for the code use check min(RHS,Start) > Start - Stride
* update remaining test to be consistent
* Stride = umax(1, Stride) instead of umin, which matches the code above; umin would not avoid divide by 0.


Preparation for https://github.com/llvm/llvm-project/pull/218694

---
Full diff: https://github.com/llvm/llvm-project/pull/219170.diff


1 Files Affected:

- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+14-15) 


``````````diff
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 6b0951491a88a..2f4d0fc8ce58c 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13560,35 +13560,34 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
                        MaxBECount, false /*MaxOrZero*/, Predicates);
     }
   } else {
-    // We use the expression (max(End,Start)-Start)/Stride to describe the
-    // backedge count, as if the backedge is taken at least once
-    // max(End,Start) is End and so the result is as above, and if not
-    // max(End,Start) is Start so we get a backedge count of zero.
+    // Let End = max(RHS,Start).  We use the expression (End-Start)/Stride to
+    // describe the backedge count: if the backedge is taken at least once then
+    // End is RHS, and if not End is Start so we get a backedge count of zero.
     auto *OrigStartMinusStride = getMinusSCEV(OrigStart, Stride);
     assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!");
     assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!");
     assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!");
-    // Can we prove (max(RHS,Start) > Start - Stride?
+    // Can we prove Start - Stride < Start and Start - Stride < RHS?
     if (isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigStart) &&
         isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigRHS)) {
       // In this case, we can use a refined formula for computing backedge
       // taken count.  The general formula remains:
-      //   "End-Start /uceiling Stride" where "End = max(RHS,Start)"
+      //   "End-Start /uceiling Stride"
       // We want to use the alternate formula:
-      //   "((End - 1) - (Start - Stride)) /u Stride"
+      //   "((RHS - 1) - (Start - Stride)) /u Stride"
       // Let's do a quick case analysis to show these are equivalent under
-      // our precondition that max(RHS,Start) > Start - Stride.
-      // * For RHS <= Start, the backedge-taken count must be zero.
-      //   "((End - 1) - (Start - Stride)) /u Stride" reduces to
+      // our preconditions that Start - Stride is below both Start and RHS.
+      // * For RHS <= Start (End is Start), the backedge-taken count must be
+      //   zero.
+      //   "((End - 1) - (Start - Stride)) /u Stride" is <=
       //   "((Start - 1) - (Start - Stride)) /u Stride" which simplies to
       //   "Stride - 1 /u Stride" which is indeed zero for all non-zero values
-      //     of Stride.  For 0 stride, we've use umin(1,Stride) above,
+      //     of Stride.  For 0 stride, we've used umax(Stride,1) above,
       //     reducing this to the stride of 1 case.
-      // * For RHS >= Start, the backedge count must be "RHS-Start /uceil
-      // Stride".
-      //   "((End - 1) - (Start - Stride)) /u Stride" reduces to
+      // * For RHS >= Start (End is RHS), the backedge count must be
+      //   "RHS-Start /uceil Stride".
       //   "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to
-      //   "((RHS - (Start - Stride) - 1) /u Stride".
+      //   "(RHS - (Start - Stride) - 1) /u Stride".
       //   Our preconditions trivially imply no overflow in that form.
       const SCEV *MinusOne = getMinusOne(Stride->getType());
       const SCEV *Numerator =

``````````

</details>


https://github.com/llvm/llvm-project/pull/219170


More information about the llvm-commits mailing list