[llvm] [SCEV] Apply loop guards to Distance + 1 in howFarToZero. (PR #219944)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 04:17:35 PDT 2026


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/219944

Apply loop guards on on `Distance + 1` before computing the unsinged
range. This improves results in a number of cases in practice, mostly
additional unrolling and some runtime check simplifications:
https://github.com/dtcxzyw/llvm-opt-benchmark-nightly/pull/1138

Guards have already been collected on this code path, so applying them is cheap.
Compile-time impact is in the noise: https://llvm-compile-time-tracker.com/compare.php?from=918aa38d750cd40d4265c39817fb31efb628f7e1&to=33c51ea1c4748883dfef71bdd1007bd72ec9095f&stat=instructions:u

>From 10613bb3056db86d73be1c4fb5f72d2a13fcf521 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 30 Aug 2026 22:13:43 +0100
Subject: [PATCH 1/2] Add test

---
 .../max-backedge-taken-count-guard-info.ll    | 194 ++++++++++++++++++
 1 file changed, 194 insertions(+)

diff --git a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
index 85cb371f6a3d3..6c2c707496af9 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
@@ -1848,3 +1848,197 @@ loop:
 exit:
   ret void
 }
+
+; The backedge-taken count is (%n - %start - 1), whose guarded range wraps.
+; The bound has to be computed via (%n - %start), which the assume limits to 7.
+define void @guarded_distance_plus_one(ptr %dst, i64 %n, i64 %start) {
+; CHECK-LABEL: 'guarded_distance_plus_one'
+; CHECK-NEXT:  Classifying expressions for: @guarded_distance_plus_one
+; CHECK-NEXT:    %sub = sub i64 %n, %start
+; CHECK-NEXT:    --> ((-1 * %start) + %n) U: full-set S: full-set
+; CHECK-NEXT:    %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT:    --> {%start,+,1}<nuw><%loop> U: full-set S: full-set Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %gep = getelementptr i8, ptr %dst, i64 %iv
+; CHECK-NEXT:    --> {(%start + %dst),+,1}<nw><%loop> U: full-set S: full-set Exits: (-1 + %n + %dst) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %iv.next = add nuw i64 %iv, 1
+; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:  Determining loop execution counts for: @guarded_distance_plus_one
+; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -2
+; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: Trip multiple is 1
+;
+entry:
+  %sub = sub i64 %n, %start
+  %a = icmp ult i64 %sub, 8
+  call void @llvm.assume(i1 %a)
+  %c = icmp ult i64 %start, %n
+  br i1 %c, label %loop, label %exit
+
+loop:
+  %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i8, ptr %dst, i64 %iv
+  store i8 0, ptr %gep
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+; (%start u< %n) and (%n u< %start + 8) bound the difference (%n - %start),
+; which cannot wrap, by 7.
+define void @two_sided_guard_bounds_difference(ptr %dst, i64 %n, i64 %start) {
+; CHECK-LABEL: 'two_sided_guard_bounds_difference'
+; CHECK-NEXT:  Classifying expressions for: @two_sided_guard_bounds_difference
+; CHECK-NEXT:    %add = add i64 %start, 8
+; CHECK-NEXT:    --> (8 + %start) U: full-set S: full-set
+; CHECK-NEXT:    %and = and i1 %c.0, %c.1
+; CHECK-NEXT:    --> (%c.1 umin %c.0) U: full-set S: full-set
+; CHECK-NEXT:    %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT:    --> {%start,+,1}<nuw><%loop> U: full-set S: full-set Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %gep = getelementptr i8, ptr %dst, i64 %iv
+; CHECK-NEXT:    --> {(%start + %dst),+,1}<nw><%loop> U: full-set S: full-set Exits: (-1 + %n + %dst) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %iv.next = add nuw i64 %iv, 1
+; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:  Determining loop execution counts for: @two_sided_guard_bounds_difference
+; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -2
+; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: Trip multiple is 1
+;
+entry:
+  %c.0 = icmp ult i64 %start, %n
+  %add = add i64 %start, 8
+  %c.1 = icmp ult i64 %n, %add
+  %and = and i1 %c.0, %c.1
+  br i1 %and, label %loop, label %exit
+
+loop:
+  %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i8, ptr %dst, i64 %iv
+  store i8 0, ptr %gep
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+; Same, with a non-strict upper bound: (%n u<= %start + 8) bounds the
+; difference by 8.
+define void @two_sided_guard_bounds_difference_ule(ptr %dst, i64 %n, i64 %start) {
+; CHECK-LABEL: 'two_sided_guard_bounds_difference_ule'
+; CHECK-NEXT:  Classifying expressions for: @two_sided_guard_bounds_difference_ule
+; CHECK-NEXT:    %add = add i64 %start, 8
+; CHECK-NEXT:    --> (8 + %start) U: full-set S: full-set
+; CHECK-NEXT:    %and = and i1 %c.0, %c.1
+; CHECK-NEXT:    --> (%c.1 umin %c.0) U: full-set S: full-set
+; CHECK-NEXT:    %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT:    --> {%start,+,1}<nuw><%loop> U: full-set S: full-set Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %gep = getelementptr i8, ptr %dst, i64 %iv
+; CHECK-NEXT:    --> {(%start + %dst),+,1}<nw><%loop> U: full-set S: full-set Exits: (-1 + %n + %dst) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %iv.next = add nuw i64 %iv, 1
+; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:  Determining loop execution counts for: @two_sided_guard_bounds_difference_ule
+; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -2
+; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: Trip multiple is 1
+;
+entry:
+  %c.0 = icmp ult i64 %start, %n
+  %add = add i64 %start, 8
+  %c.1 = icmp ule i64 %n, %add
+  %and = and i1 %c.0, %c.1
+  br i1 %and, label %loop, label %exit
+
+loop:
+  %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i8, ptr %dst, i64 %iv
+  store i8 0, ptr %gep
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+; Without the (%start u< %n) guard, (%n - %start) may wrap and cannot be
+; bounded.
+define void @one_sided_guard_does_not_bound_difference(ptr %dst, i64 %n, i64 %start) {
+; CHECK-LABEL: 'one_sided_guard_does_not_bound_difference'
+; CHECK-NEXT:  Classifying expressions for: @one_sided_guard_does_not_bound_difference
+; CHECK-NEXT:    %add = add i64 %start, 8
+; CHECK-NEXT:    --> (8 + %start) U: full-set S: full-set
+; CHECK-NEXT:    %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT:    --> {%start,+,1}<nuw><%loop> U: full-set S: full-set Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %gep = getelementptr i8, ptr %dst, i64 %iv
+; CHECK-NEXT:    --> {(%start + %dst),+,1}<nw><%loop> U: full-set S: full-set Exits: (-1 + %n + %dst) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %iv.next = add nuw i64 %iv, 1
+; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:  Determining loop execution counts for: @one_sided_guard_does_not_bound_difference
+; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -1
+; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: Trip multiple is 1
+;
+entry:
+  %add = add i64 %start, 8
+  %c = icmp ult i64 %n, %add
+  br i1 %c, label %loop, label %exit
+
+loop:
+  %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i8, ptr %dst, i64 %iv
+  store i8 0, ptr %gep
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+; The upper bound is not (%start + constant), so nothing can be derived about
+; (%n - %start).
+define void @two_sided_guard_non_constant_offset(ptr %dst, i64 %n, i64 %start, i64 %off) {
+; CHECK-LABEL: 'two_sided_guard_non_constant_offset'
+; CHECK-NEXT:  Classifying expressions for: @two_sided_guard_non_constant_offset
+; CHECK-NEXT:    %add = add i64 %start, %off
+; CHECK-NEXT:    --> (%start + %off) U: full-set S: full-set
+; CHECK-NEXT:    %and = and i1 %c.0, %c.1
+; CHECK-NEXT:    --> (%c.1 umin %c.0) U: full-set S: full-set
+; CHECK-NEXT:    %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT:    --> {%start,+,1}<nuw><%loop> U: full-set S: full-set Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %gep = getelementptr i8, ptr %dst, i64 %iv
+; CHECK-NEXT:    --> {(%start + %dst),+,1}<nw><%loop> U: full-set S: full-set Exits: (-1 + %n + %dst) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:    %iv.next = add nuw i64 %iv, 1
+; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
+; CHECK-NEXT:  Determining loop execution counts for: @two_sided_guard_non_constant_offset
+; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -2
+; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
+; CHECK-NEXT:  Loop %loop: Trip multiple is 1
+;
+entry:
+  %c.0 = icmp ult i64 %start, %n
+  %add = add i64 %start, %off
+  %c.1 = icmp ult i64 %n, %add
+  %and = and i1 %c.0, %c.1
+  br i1 %and, label %loop, label %exit
+
+loop:
+  %iv = phi i64 [ %start, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i8, ptr %dst, i64 %iv
+  store i8 0, ptr %gep
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}

>From 6292a8fb4ccee38362d17b715a7d586a0e91ee26 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 30 Aug 2026 18:54:14 +0100
Subject: [PATCH 2/2] [SCEV] Apply loop guards to Distance + 1 in howFarToZero.

For a unit-step exit, the maximum backedge-taken count is refined using
unsigned_max(Distance + 1) - 1 when the loop entry guarantees
Distance + 1 != 0. That refinement used the plain unsigned range of
Distance + 1, while the bound computed from Distance itself already
takes the loop guards into account.

Applying the guards to Distance + 1 as well recovers bounds that are
lost on Distance: guards rewrite a trip count of the form (n - i - 1)
to (-1 + umin(n - i, C)), whose unsigned range is a wrapped set, so its
maximum is -1 even though (n - i) is bounded by C.
---
 llvm/lib/Analysis/ScalarEvolution.cpp                  | 10 +++++++---
 .../max-backedge-taken-count-guard-info.ll             |  2 +-
 .../constraint-eliminiation-interactions.ll            |  2 +-
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 4df043b434b01..64b2e8c291fc3 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -10799,9 +10799,13 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
     const SCEV *DistancePlusOne = getAddExpr(Distance, One);
     if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
       // If Distance + 1 doesn't overflow, we can compute the maximum distance
-      // as "unsigned_max(Distance + 1) - 1".
-      ConstantRange CR = getUnsignedRange(DistancePlusOne);
-      MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
+      // as "unsigned_max(Distance + 1) - 1". Also apply the loop guards to
+      // Distance + 1; the range of Distance itself may be a wrapped set even
+      // when the guards bound Distance + 1 tightly.
+      APInt Max = APIntOps::umin(
+          getUnsignedRangeMax(applyLoopGuards(DistancePlusOne, Guards)),
+          getUnsignedRangeMax(DistancePlusOne));
+      MaxBECount = APIntOps::umin(MaxBECount, Max - 1);
     }
     return ExitLimit(Distance, getConstant(MaxBECount), Distance, false,
                      Predicates);
diff --git a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
index 6c2c707496af9..18aff4f73830b 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
@@ -1864,7 +1864,7 @@ define void @guarded_distance_plus_one(ptr %dst, i64 %n, i64 %start) {
 ; CHECK-NEXT:    --> {(1 + %start),+,1}<nuw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
 ; CHECK-NEXT:  Determining loop execution counts for: @guarded_distance_plus_one
 ; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (-1 * %start) + %n)
-; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 -2
+; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i64 6
 ; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (-1 * %start) + %n)
 ; CHECK-NEXT:  Loop %loop: Trip multiple is 1
 ;
diff --git a/llvm/test/Transforms/PhaseOrdering/constraint-eliminiation-interactions.ll b/llvm/test/Transforms/PhaseOrdering/constraint-eliminiation-interactions.ll
index 4b5e0b278f350..766903a604ec0 100644
--- a/llvm/test/Transforms/PhaseOrdering/constraint-eliminiation-interactions.ll
+++ b/llvm/test/Transforms/PhaseOrdering/constraint-eliminiation-interactions.ll
@@ -16,7 +16,7 @@ define void @sum_of_induction_and_guard(i8 %n, i8 %j) mustprogress {
 ; CHECK:       [[BODY]]:
 ; CHECK-NEXT:    [[IV_NEXT2:%.*]] = phi i8 [ [[IV_NEXT:%.*]], %[[BODY]] ], [ 11, %[[ENTRY]] ]
 ; CHECK-NEXT:    tail call void @use(i32 1)
-; CHECK-NEXT:    [[IV_NEXT]] = add i8 [[IV_NEXT2]], 1
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i8 [[IV_NEXT2]], 1
 ; CHECK-NEXT:    [[C_NOT:%.*]] = icmp eq i8 [[IV_NEXT]], [[N]]
 ; CHECK-NEXT:    br i1 [[C_NOT]], label %[[EXIT]], label %[[BODY]]
 ; CHECK:       [[EXIT]]:



More information about the llvm-commits mailing list