[llvm] [DA] Refactor the signature of the Exact RDIV test (NFCI) (PR #189535)

Ruoyu Qiu via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 00:14:51 PDT 2026


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

>From 83a8987a53bbe44f7ec4dde6172985efefa702f2 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Tue, 31 Mar 2026 04:33:23 +0000
Subject: [PATCH 1/2] [DA] Refactor the signature of the Exact RDIV test (NFCI)

Change the signature of `exactSIVtest` to directly pass addrecs instead
of passing their operands separately. This change is not mandatory, but
it will simplify the code, especially because we will be checking the
presence of nsw flags on the addrecs.
---
 .../llvm/Analysis/DependenceAnalysis.h        |  4 +--
 llvm/lib/Analysis/DependenceAnalysis.cpp      | 33 ++++++++-----------
 2 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h
index 7ca7dde467085..ac819ec271b07 100644
--- a/llvm/include/llvm/Analysis/DependenceAnalysis.h
+++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h
@@ -583,9 +583,7 @@ class DependenceInfo {
   /// Returns true if any possible dependence is disproved.
   /// Works in some cases that symbolicRDIVtest doesn't,
   /// and vice versa.
-  bool exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
-                     const SCEV *SrcConst, const SCEV *DstConst,
-                     const Loop *SrcLoop, const Loop *DstLoop,
+  bool exactRDIVtest(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
                      FullDependence &Result) const;
 
   /// gcdMIVtest - Tests an MIV subscript pair for dependence.
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 959299bed2521..d9dc6beeca284 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1945,13 +1945,16 @@ bool DependenceInfo::weakZeroDstSIVtest(const SCEVAddRecExpr *Src,
 // and a and b are constants.
 // Returns true if any possible dependence is disproved.
 // Works in some cases that symbolicRDIVtest doesn't, and vice versa.
-bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
-                                   const SCEV *SrcConst, const SCEV *DstConst,
-                                   const Loop *SrcLoop, const Loop *DstLoop,
+bool DependenceInfo::exactRDIVtest(const SCEVAddRecExpr *Src,
+                                   const SCEVAddRecExpr *Dst,
                                    FullDependence &Result) const {
   if (!isDependenceTestEnabled(DependenceTestType::ExactRDIV))
     return false;
 
+  const SCEV *SrcCoeff = Src->getStepRecurrence(*SE);
+  const SCEV *SrcConst = Src->getStart();
+  const SCEV *DstCoeff = Dst->getStepRecurrence(*SE);
+  const SCEV *DstConst = Dst->getStart();
   LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
   LLVM_DEBUG(dbgs() << "\t    SrcCoeff = " << *SrcCoeff << " = AM\n");
   LLVM_DEBUG(dbgs() << "\t    DstCoeff = " << *DstCoeff << " = BM\n");
@@ -1982,12 +1985,12 @@ bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
 
   // since SCEV construction seems to normalize, LM = 0
   std::optional<APInt> SrcUM =
-      collectNonNegativeConstantUpperBound(SrcLoop, Delta->getType());
+      collectNonNegativeConstantUpperBound(Src->getLoop(), Delta->getType());
   if (SrcUM)
     LLVM_DEBUG(dbgs() << "\t    SrcUM = " << *SrcUM << "\n");
 
   std::optional<APInt> DstUM =
-      collectNonNegativeConstantUpperBound(DstLoop, Delta->getType());
+      collectNonNegativeConstantUpperBound(Dst->getLoop(), Delta->getType());
   if (DstUM)
     LLVM_DEBUG(dbgs() << "\t    DstUM = " << *DstUM << "\n");
 
@@ -2103,26 +2106,16 @@ bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
 // Return true if dependence disproved.
 bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
                               FullDependence &Result) const {
-  const SCEV *SrcConst, *DstConst;
-  const SCEV *SrcCoeff, *DstCoeff;
-  const Loop *SrcLoop, *DstLoop;
-
   LLVM_DEBUG(dbgs() << "    src = " << *Src << "\n");
   LLVM_DEBUG(dbgs() << "    dst = " << *Dst << "\n");
   const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
   const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
-  if (SrcAddRec && DstAddRec) {
-    SrcConst = SrcAddRec->getStart();
-    SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
-    SrcLoop = SrcAddRec->getLoop();
-    DstConst = DstAddRec->getStart();
-    DstCoeff = DstAddRec->getStepRecurrence(*SE);
-    DstLoop = DstAddRec->getLoop();
-  } else
+  bool disproven = false;
+  if (SrcAddRec && DstAddRec)
+    disproven = exactRDIVtest(SrcAddRec, DstAddRec, Result);
+  else
     llvm_unreachable("RDIV expected at least one AddRec");
-  return exactRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop, DstLoop,
-                       Result) ||
-         gcdMIVtest(Src, Dst, Result);
+  return disproven || gcdMIVtest(Src, Dst, Result);
 }
 
 // Tests the single-subscript MIV pair (Src and Dst) for dependence.

>From 32ad66206f6a2343e01596270e13e583b9941ed7 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Tue, 31 Mar 2026 07:14:12 +0000
Subject: [PATCH 2/2] Update

---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index d9dc6beeca284..34178b3f7c5d2 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2110,12 +2110,8 @@ bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
   LLVM_DEBUG(dbgs() << "    dst = " << *Dst << "\n");
   const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
   const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
-  bool disproven = false;
-  if (SrcAddRec && DstAddRec)
-    disproven = exactRDIVtest(SrcAddRec, DstAddRec, Result);
-  else
-    llvm_unreachable("RDIV expected at least one AddRec");
-  return disproven || gcdMIVtest(Src, Dst, Result);
+  assert(SrcAddRec && DstAddRec && "Unexpected non-addrec input");
+  return exactRDIVtest(SrcAddRec, DstAddRec, Result) || gcdMIVtest(Src, Dst, Result);
 }
 
 // Tests the single-subscript MIV pair (Src and Dst) for dependence.



More information about the llvm-commits mailing list