[llvm] [DA] factor out repetitive code in GCD test (NFCI) (PR #189461)

Ehsan Amiri via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 09:46:19 PDT 2026


https://github.com/amehsan updated https://github.com/llvm/llvm-project/pull/189461

>From 81dcb781a924ba91134e100ae76d65ba10e46fb6 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Mon, 30 Mar 2026 11:25:35 -0400
Subject: [PATCH 1/5] [DA] factor our repetitive code in GCD test (NFCI)

The logic for recursively investigating the source and destination AddRecs
in GCD test is the same and can be factored out.
---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 65 +++++++++++-------------
 1 file changed, 29 insertions(+), 36 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 76f514e4c9327..e6d717f0a5b74 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2212,6 +2212,27 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
   return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
 }
 
+// Compute running GCD and record source constant.
+// Because we're looking for the constant at the end of the chain,
+// we can't quit the loop just because the GCD == 1.
+const SCEV* analyzeCoefficientsForGCD(const SCEV *Coefficients, 
+                                      APInt &RunningGCD,
+                                      ScalarEvolution *SE)
+{
+  while (const SCEVAddRecExpr *AddRec =
+             dyn_cast<SCEVAddRecExpr>(Coefficients)) {
+    const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
+    // If the coefficient is the product of a constant and other stuff,
+    // we can use the constant in the GCD computation.
+    std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
+    if (!ConstCoeff)
+      return nullptr;
+    RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
+    Coefficients = AddRec->getStart();
+  }
+  return Coefficients;
+}
+
 //===----------------------------------------------------------------------===//
 // gcdMIVtest -
 // Tests an MIV subscript pair for dependence.
@@ -2239,41 +2260,13 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
   unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
   APInt RunningGCD = APInt::getZero(BitWidth);
 
-  // Examine Src coefficients.
-  // Compute running GCD and record source constant.
-  // Because we're looking for the constant at the end of the chain,
-  // we can't quit the loop just because the GCD == 1.
-  const SCEV *Coefficients = Src;
-  while (const SCEVAddRecExpr *AddRec =
-             dyn_cast<SCEVAddRecExpr>(Coefficients)) {
-    const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
-    // If the coefficient is the product of a constant and other stuff,
-    // we can use the constant in the GCD computation.
-    std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
-    if (!ConstCoeff)
-      return false;
-    RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
-    Coefficients = AddRec->getStart();
-  }
-  const SCEV *SrcConst = Coefficients;
-
-  // Examine Dst coefficients.
-  // Compute running GCD and record destination constant.
-  // Because we're looking for the constant at the end of the chain,
-  // we can't quit the loop just because the GCD == 1.
-  Coefficients = Dst;
-  while (const SCEVAddRecExpr *AddRec =
-             dyn_cast<SCEVAddRecExpr>(Coefficients)) {
-    const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
-    // If the coefficient is the product of a constant and other stuff,
-    // we can use the constant in the GCD computation.
-    std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
-    if (!ConstCoeff)
-      return false;
-    RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
-    Coefficients = AddRec->getStart();
-  }
-  const SCEV *DstConst = Coefficients;
+  // Examine Src and dst coefficients.
+  const SCEV *SrcConst = analyzeCoefficientsForGCD(Src, RunningGCD, SE);
+  if (!SrcConst)
+    return false;
+  const SCEV *DstConst = analyzeCoefficientsForGCD(Dst, RunningGCD, SE);
+  if (!DstConst)
+    return false;
 
   APInt ExtraGCD = APInt::getZero(BitWidth);
   const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
@@ -2309,7 +2302,7 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
   LLVM_DEBUG(dbgs() << "    ExtraGCD = " << ExtraGCD << '\n');
 
   bool Improved = false;
-  Coefficients = Src;
+  const SCEV *Coefficients = Src;
   while (const SCEVAddRecExpr *AddRec =
              dyn_cast<SCEVAddRecExpr>(Coefficients)) {
     Coefficients = AddRec->getStart();

>From e5bd7669e88079fdbe712a1e07e507f7f6a7dcb5 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Mon, 30 Mar 2026 15:41:07 -0400
Subject: [PATCH 2/5] fix code style issue

---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index e6d717f0a5b74..cca187aa63e40 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2216,9 +2216,7 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
 // Because we're looking for the constant at the end of the chain,
 // we can't quit the loop just because the GCD == 1.
 const SCEV* analyzeCoefficientsForGCD(const SCEV *Coefficients, 
-                                      APInt &RunningGCD,
-                                      ScalarEvolution *SE)
-{
+                                      APInt &RunningGCD, ScalarEvolution *SE) {
   while (const SCEVAddRecExpr *AddRec =
              dyn_cast<SCEVAddRecExpr>(Coefficients)) {
     const SCEV *Coeff = AddRec->getStepRecurrence(*SE);

>From eb9d55b9910275612b8e091a6daa2a3c60467518 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Mon, 30 Mar 2026 15:43:47 -0400
Subject: [PATCH 3/5] Remove trailing space

---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index cca187aa63e40..ae441193d2fe6 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2215,7 +2215,7 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
 // Compute running GCD and record source constant.
 // Because we're looking for the constant at the end of the chain,
 // we can't quit the loop just because the GCD == 1.
-const SCEV* analyzeCoefficientsForGCD(const SCEV *Coefficients, 
+const SCEV* analyzeCoefficientsForGCD(const SCEV *Coefficients,
                                       APInt &RunningGCD, ScalarEvolution *SE) {
   while (const SCEVAddRecExpr *AddRec =
              dyn_cast<SCEVAddRecExpr>(Coefficients)) {

>From 58661b9aba85467210a3949fb0fa05ffaaf65fe7 Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Mon, 30 Mar 2026 15:49:28 -0400
Subject: [PATCH 4/5] another issue with code style

---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index ae441193d2fe6..05ba021771bc2 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2215,7 +2215,7 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
 // Compute running GCD and record source constant.
 // Because we're looking for the constant at the end of the chain,
 // we can't quit the loop just because the GCD == 1.
-const SCEV* analyzeCoefficientsForGCD(const SCEV *Coefficients,
+const SCEV *analyzeCoefficientsForGCD(const SCEV *Coefficients,
                                       APInt &RunningGCD, ScalarEvolution *SE) {
   while (const SCEVAddRecExpr *AddRec =
              dyn_cast<SCEVAddRecExpr>(Coefficients)) {

>From 39d59a16272e450fa919d4282f1dc2df0da80d5c Mon Sep 17 00:00:00 2001
From: Ehsan Amiri <ehsan.amiri at huawei.com>
Date: Tue, 31 Mar 2026 13:08:06 -0400
Subject: [PATCH 5/5] Revise the comment

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

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 05ba021771bc2..c37b9304ebefb 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -2212,9 +2212,9 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
   return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
 }
 
-// Compute running GCD and record source constant.
-// Because we're looking for the constant at the end of the chain,
-// we can't quit the loop just because the GCD == 1.
+/// Compute \p RunningGCD and return the start value of the innermost
+/// \p SCEVAddRecExpr. In order to calculate the return value we do not
+/// return immediately if it is proved that \p RunningGCD = 1.
 const SCEV *analyzeCoefficientsForGCD(const SCEV *Coefficients,
                                       APInt &RunningGCD, ScalarEvolution *SE) {
   while (const SCEVAddRecExpr *AddRec =



More information about the llvm-commits mailing list