[llvm-branch-commits] [llvm] [DA] Consolidate accumulating GCD functions (NFCI) (PR #197936)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri May 15 07:03:04 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Ryotaro Kasuga (kasuga-fj)

<details>
<summary>Changes</summary>

This patch consolidates two functions `accumulateCoefficientsGCD` and `analyzeCoefficientsForGCD` by merging the latter into the former. These two functions are very similar, and keeping both of them does not make much sense.

---
Full diff: https://github.com/llvm/llvm-project/pull/197936.diff


2 Files Affected:

- (modified) llvm/include/llvm/Analysis/DependenceAnalysis.h (+5-5) 
- (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (+11-28) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h
index 4a58b3dac8882..490fd4520746f 100644
--- a/llvm/include/llvm/Analysis/DependenceAnalysis.h
+++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h
@@ -628,12 +628,12 @@ class DependenceInfo {
   /// in \p RunningGCD. Also, the initial value of \p RunningGCD affects the
   /// result. If we find a term like (c_k * X_k * i_k), where i_k is the
   /// induction variable of \p CurLoop, c_k is stored in \p CurLoopCoeff and not
-  /// included in the GCD computation. Returns false if we fail to find a
+  /// included in the GCD computation. Returns nullptr if we fail to find a
   /// constant coefficient for some loop, e.g., when a term like (X+Y)*i is
-  /// present. Otherwise returns true.
-  bool accumulateCoefficientsGCD(const SCEV *Expr, const Loop *CurLoop,
-                                 const SCEV *&CurLoopCoeff,
-                                 APInt &RunningGCD) const;
+  /// present. Otherwise returns the remaining constant term C.
+  const SCEV *accumulateCoefficientsGCD(const SCEV *Expr, const Loop *CurLoop,
+                                        const SCEV *&CurLoopCoeff,
+                                        APInt &RunningGCD) const;
 
   /// getPositivePart - X^+ = max(X, 0).
   const SCEV *getPositivePart(const SCEV *X) const;
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 9f5ca62cd70c6..d028bfb4ad059 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1770,15 +1770,15 @@ static std::optional<APInt> getConstantCoefficient(const SCEV *Expr) {
   return std::nullopt;
 }
 
-bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
-                                               const Loop *CurLoop,
-                                               const SCEV *&CurLoopCoeff,
-                                               APInt &RunningGCD) const {
+const SCEV *DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
+                                                      const Loop *CurLoop,
+                                                      const SCEV *&CurLoopCoeff,
+                                                      APInt &RunningGCD) const {
   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
   if (!AddRec) {
     assert(isLoopInvariant(Expr, CurLoop) &&
            "Expected loop invariant expression");
-    return true;
+    return Expr;
   }
 
   assert(AddRec->isAffine() && "Unexpected Expr");
@@ -1792,7 +1792,7 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
     // If the coefficient is the product of a constant and other stuff, we can
     // use the constant in the GCD computation.
     if (!ConstCoeff)
-      return false;
+      return nullptr;
 
     // TODO: What happens if ConstCoeff is the "most negative" signed number
     // (e.g. -128 for 8 bit wide APInt)?
@@ -1802,26 +1802,6 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
   return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
 }
 
-/// 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.
-static 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.
@@ -1849,11 +1829,14 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
   unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
   APInt RunningGCD = APInt::getZero(BitWidth);
 
+  const SCEV *Dummy = nullptr;
   // Examine Src and dst coefficients.
-  const SCEV *SrcConst = analyzeCoefficientsForGCD(Src, RunningGCD, SE);
+  const SCEV *SrcConst =
+      accumulateCoefficientsGCD(Src, nullptr, Dummy, RunningGCD);
   if (!SrcConst)
     return false;
-  const SCEV *DstConst = analyzeCoefficientsForGCD(Dst, RunningGCD, SE);
+  const SCEV *DstConst =
+      accumulateCoefficientsGCD(Dst, nullptr, Dummy, RunningGCD);
   if (!DstConst)
     return false;
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/197936


More information about the llvm-branch-commits mailing list