[llvm] [DA] Fix overflow of findBoundsALL in BanerjeeTest (PR #191232)

Ruoyu Qiu via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 09:29:17 PDT 2026


https://github.com/cabbaken updated https://github.com/llvm/llvm-project/pull/191232

>From e468e0af772165bb343a39e1676be0c14aa01c68 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Thu, 9 Apr 2026 16:02:42 +0000
Subject: [PATCH] [DA] Fix overflow of findBoundsALL in BanerjeeTest

Fix signed overflow handling in `findBoundsALL` for the Banerjee test.
The previous implementation computed bounds using `getMinusSCEV` and
`getMulExpr` without checking for signed overflow, which could produce
incorrect bounds when coefficients have extreme values.

- Add `mulSCEVNoSignedOverflow` helper function that checks for
multiplication overflow before computing the result
- Use `minusSCEVNoSignedOverflow` and `mulSCEVNoSignedOverflow` in
`findBoundsALL` to safely compute bounds, returning `nullptr`
when overflow would occur
---
 llvm/lib/Analysis/DependenceAnalysis.cpp      | 23 +++++++++++++---
 .../DependenceAnalysis/gcd-miv-overflow.ll    | 26 +++++--------------
 2 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 3191651dc5b8a..fa030597c56e3 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1162,6 +1162,15 @@ static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
   return nullptr;
 }
 
+/// Returns \p A * \p B if it guaranteed not to signed wrap. Otherwise returns
+/// nullptr. \p A and \p B must have the same integer type.
+static const SCEV *mulSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
+                                           ScalarEvolution &SE) {
+  if (SE.willNotOverflow(Instruction::Mul, /*Signed=*/true, A, B))
+    return SE.getMulExpr(A, B);
+  return nullptr;
+}
+
 /// Returns true iff \p Test is enabled.
 static bool isDependenceTestEnabled(DependenceTestType Test) {
   if (EnableDependenceTest == DependenceTestType::All)
@@ -2467,10 +2476,16 @@ void DependenceInfo::findBoundsALL(ArrayRef<CoefficientInfo> A,
   Bound[K].Upper[Dependence::DVEntry::ALL] =
       nullptr; // Default value = +infinity.
   if (Bound[K].Iterations) {
-    Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
-        SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
-    Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
-        SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
+    const SCEV *LowerDiff =
+        minusSCEVNoSignedOverflow(A[K].NegPart, B[K].PosPart, *SE);
+    const SCEV *UpperDiff =
+        minusSCEVNoSignedOverflow(A[K].PosPart, B[K].NegPart, *SE);
+    if (LowerDiff)
+      Bound[K].Lower[Dependence::DVEntry::ALL] =
+          mulSCEVNoSignedOverflow(LowerDiff, Bound[K].Iterations, *SE);
+    if (UpperDiff)
+      Bound[K].Upper[Dependence::DVEntry::ALL] =
+          mulSCEVNoSignedOverflow(UpperDiff, Bound[K].Iterations, *SE);
   } else {
     // If the difference is 0, we won't need to know the number of iterations.
     if (SE->isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
diff --git a/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll b/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
index 1d1ba010abe82..b8a35975e34ab 100644
--- a/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/gcd-miv-overflow.ll
@@ -189,29 +189,19 @@ exit:
 ;     if (offset1 == -5) A[offset1] = 0;
 ;   }
 ;
-; FIXME: DependenceAnalysis fails to detect dependency between two stores.
-;
 ; memory accesses                     | (i,j) == (1844674407370955160,1)
 ; ------------------------------------|----------------------------------
 ; A[ 5*i - 9223372036854775805*j]     | A[-5]
 ; A[-5*i + 9223372036854775796*j - 1] | A[-5]
 ;
 define void @gcdmiv_delta_ovfl2(ptr %A) {
-; CHECK-ALL-LABEL: 'gcdmiv_delta_ovfl2'
-; 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 0, ptr %gep.1, align 1
-; CHECK-ALL-NEXT:    da analyze - none!
-; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.1, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-ALL-NEXT:    da analyze - none!
-;
-; CHECK-GCD-MIV-LABEL: 'gcdmiv_delta_ovfl2'
-; CHECK-GCD-MIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
-; CHECK-GCD-MIV-NEXT:    da analyze - output [* *]!
-; CHECK-GCD-MIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-GCD-MIV-NEXT:    da analyze - output [* *|<]!
-; CHECK-GCD-MIV-NEXT:  Src: store i8 0, ptr %gep.1, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
-; CHECK-GCD-MIV-NEXT:    da analyze - output [* *]!
+; CHECK-LABEL: 'gcdmiv_delta_ovfl2'
+; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-NEXT:    da analyze - output [* *]!
+; CHECK-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
+; CHECK-NEXT:    da analyze - output [* *|<]!
+; CHECK-NEXT:  Src: store i8 0, ptr %gep.1, align 1 --> Dst: store i8 0, ptr %gep.1, align 1
+; CHECK-NEXT:    da analyze - output [* *]!
 ;
 entry:
   br label %loop.i.header
@@ -261,5 +251,3 @@ exit:
   ret void
 
 }
-;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; CHECK: {{.*}}



More information about the llvm-commits mailing list