[llvm-branch-commits] [llvm] [DA] Add precondition `0 <=s UB` to function `inferAffineDomain` (PR #186390)

Ryotaro Kasuga via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Mar 13 06:07:15 PDT 2026


https://github.com/kasuga-fj created https://github.com/llvm/llvm-project/pull/186390

None

>From 2cbefc9ced870026c9c07b8a14f42b96c7ae59b1 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Fri, 13 Mar 2026 12:44:45 +0000
Subject: [PATCH] [DA] Add precondition `0 <=s UB` to function
 `inferAffineDomain`

---
 llvm/lib/Analysis/DependenceAnalysis.cpp      | 35 ++++++++++++-------
 .../DependenceAnalysis/exact-siv-large-btc.ll | 25 ++++++++-----
 .../DependenceAnalysis/rdiv-large-btc.ll      |  2 +-
 3 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 581f4849ae315..3c08449ba87a2 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1561,11 +1561,11 @@ ceilingOfQuotient(const OverflowSafeSignedAPInt &OA,
 /// integer, infer the possible range of k based on the known range of the
 /// affine expression. If we know A*k + B is non-negative, i.e.,
 ///
-///   A*k + B >= 0
+///   A*k + B >=s 0
 ///
 /// we can derive the following inequalities for k when A is positive:
 ///
-///   k >= -B / A
+///   k >=s -B / A
 ///
 /// Since k is an integer, it means k is greater than or equal to the
 /// ceil(-B / A).
@@ -1573,11 +1573,11 @@ ceilingOfQuotient(const OverflowSafeSignedAPInt &OA,
 /// If the upper bound of the affine expression \p UB is passed, the following
 /// inequality can be derived as well:
 ///
-///   A*k + B <= UB
+///   A*k + B <=s UB
 ///
 /// which leads to:
 ///
-///   k <= (UB - B) / A
+///   k <=s (UB - B) / A
 ///
 /// Again, as k is an integer, it means k is less than or equal to the
 /// floor((UB - B) / A).
@@ -1585,12 +1585,14 @@ ceilingOfQuotient(const OverflowSafeSignedAPInt &OA,
 /// The similar logic applies when A is negative, but the inequalities sign flip
 /// while working with them.
 ///
-/// Preconditions: \p A is non-zero, and we know A*k + B is non-negative.
+/// Preconditions: \p A is non-zero, and we know A*k + B and \p UB are
+/// non-negative.
 static std::pair<OverflowSafeSignedAPInt, OverflowSafeSignedAPInt>
 inferDomainOfAffine(OverflowSafeSignedAPInt A, OverflowSafeSignedAPInt B,
                     OverflowSafeSignedAPInt UB) {
   assert(A && B && "A and B must be available");
   assert(*A != 0 && "A must be non-zero");
+  assert((!UB || UB->isNonNegative()) && "UB must be non-negative");
   OverflowSafeSignedAPInt TL, TU;
   if (A->sgt(0)) {
     TL = ceilingOfQuotient(-B, A);
@@ -1681,8 +1683,11 @@ bool DependenceInfo::exactSIVtest(const SCEVAddRecExpr *Src,
   // UM is perhaps unavailable, let's check
   if (const SCEVConstant *CUB =
           collectConstantUpperBound(Src->getLoop(), Delta->getType())) {
-    UM = CUB->getAPInt();
-    LLVM_DEBUG(dbgs() << "\t    UM = " << *UM << "\n");
+    APInt Tmp = CUB->getAPInt();
+    if (Tmp.isNonNegative()) {
+      UM = CUB->getAPInt();
+      LLVM_DEBUG(dbgs() << "\t    UM = " << *UM << "\n");
+    }
   }
 
   APInt TU(APInt::getSignedMaxValue(Bits));
@@ -2056,16 +2061,22 @@ bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
   // SrcUM is perhaps unavailable, let's check
   if (const SCEVConstant *UpperBound =
           collectConstantUpperBound(SrcLoop, Delta->getType())) {
-    SrcUM = UpperBound->getAPInt();
-    LLVM_DEBUG(dbgs() << "\t    SrcUM = " << *SrcUM << "\n");
+    APInt Tmp = UpperBound->getAPInt();
+    if (Tmp.isNonNegative()) {
+      SrcUM = UpperBound->getAPInt();
+      LLVM_DEBUG(dbgs() << "\t    SrcUM = " << *SrcUM << "\n");
+    }
   }
 
   std::optional<APInt> DstUM;
-  // UM is perhaps unavailable, let's check
+  // DstUM is perhaps unavailable, let's check
   if (const SCEVConstant *UpperBound =
           collectConstantUpperBound(DstLoop, Delta->getType())) {
-    DstUM = UpperBound->getAPInt();
-    LLVM_DEBUG(dbgs() << "\t    DstUM = " << *DstUM << "\n");
+    APInt Tmp = UpperBound->getAPInt();
+    if (Tmp.isNonNegative()) {
+      DstUM = UpperBound->getAPInt();
+      LLVM_DEBUG(dbgs() << "\t    DstUM = " << *DstUM << "\n");
+    }
   }
 
   APInt TU(APInt::getSignedMaxValue(Bits));
diff --git a/llvm/test/Analysis/DependenceAnalysis/exact-siv-large-btc.ll b/llvm/test/Analysis/DependenceAnalysis/exact-siv-large-btc.ll
index aa15d474f5e60..6982983967e62 100644
--- a/llvm/test/Analysis/DependenceAnalysis/exact-siv-large-btc.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/exact-siv-large-btc.ll
@@ -15,13 +15,21 @@
 ; FIXME: There is a dependency between the two stores.
 ;
 define void @exact_siv_large_btc(ptr %A) {
-; CHECK-LABEL: 'exact_siv_large_btc'
-; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
-; CHECK-NEXT:    da analyze - none!
-; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
-; CHECK-NEXT:    da analyze - none!
-; CHECK-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
-; CHECK-NEXT:    da analyze - none!
+; CHECK-ALL-LABEL: 'exact_siv_large_btc'
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - none!
+;
+; CHECK-EXACT-SIV-LABEL: 'exact_siv_large_btc'
+; CHECK-EXACT-SIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-EXACT-SIV-NEXT:    da analyze - output [*]!
+; CHECK-EXACT-SIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-EXACT-SIV-NEXT:    da analyze - output [*|<]!
+; CHECK-EXACT-SIV-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-EXACT-SIV-NEXT:    da analyze - output [*]!
 ;
 entry:
   br label %loop.header
@@ -49,5 +57,4 @@ exit:
   ret void
 }
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; CHECK-ALL: {{.*}}
-; CHECK-EXACT-SIV: {{.*}}
+; CHECK: {{.*}}
diff --git a/llvm/test/Analysis/DependenceAnalysis/rdiv-large-btc.ll b/llvm/test/Analysis/DependenceAnalysis/rdiv-large-btc.ll
index 96b2006a1cb0b..45648f306a6f3 100644
--- a/llvm/test/Analysis/DependenceAnalysis/rdiv-large-btc.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/rdiv-large-btc.ll
@@ -31,7 +31,7 @@ define void @rdiv_large_btc(ptr %A) {
 ; CHECK-EXACT-RDIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
 ; CHECK-EXACT-RDIV-NEXT:    da analyze - output [*]!
 ; CHECK-EXACT-RDIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-EXACT-RDIV-NEXT:    da analyze - none!
+; CHECK-EXACT-RDIV-NEXT:    da analyze - output [|<]!
 ; CHECK-EXACT-RDIV-NEXT:  Src: store i8 0, ptr %gep.1, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
 ; CHECK-EXACT-RDIV-NEXT:    da analyze - output [*]!
 ;



More information about the llvm-branch-commits mailing list