Skip to content

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

Merged
amehsan merged 6 commits intollvm:mainfrom
amehsan:gcd-refactor
Apr 1, 2026
Merged

[DA] factor out repetitive code in GCD test (NFCI)#189461
amehsan merged 6 commits intollvm:mainfrom
amehsan:gcd-refactor

Conversation

@amehsan
Copy link
Copy Markdown
Contributor

@amehsan amehsan commented Mar 30, 2026

The logic for recursively investigating the source and destination AddRecs in GCD test is the same and can be factored out.

The logic for recursively investigating the source and destination AddRecs
in GCD test is the same and can be factored out.
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Mar 30, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Mar 30, 2026

@llvm/pr-subscribers-llvm-analysis

Author: Ehsan Amiri (amehsan)

Changes

The logic for recursively investigating the source and destination AddRecs in GCD test is the same and can be factored out.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (+29-36)
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();

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 30, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@amehsan amehsan changed the title [DA] factor our repetitive code in GCD test (NFCI) [DA] factor out repetitive code in GCD test (NFCI) Mar 30, 2026
return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
}

// Compute running GCD and record source constant.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment ("source" should be deleted now) needs to be refined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment Revised

Comment on lines +2215 to +2217
// 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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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 record source constant.
/// Because we're looking for the constant at the end of the chain,
/// we can't quit the loop just because \p RunningGCD == 1.

Use Doxygen‑style comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

@cabbaken cabbaken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

/// 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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that there is an almost identical function DependenceInfo::accumulateCoefficientsGCD right above. Consider extending it rather than creating a new one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that there is an almost identical function DependenceInfo::accumulateCoefficientsGCD right above. Consider extending it rather than creating a new one.

I will check that in the next step to see if more code can be commoned.

Copy link
Copy Markdown
Contributor

@kasuga-fj kasuga-fj Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take care of it myself... I took a closer look and it was a bit off, so never mind.

@amehsan amehsan merged commit 26a5a49 into llvm:main Apr 1, 2026
10 checks passed
joaovam pushed a commit to joaovam/llvm-project that referenced this pull request Apr 2, 2026
The logic for recursively investigating the source and destination
AddRecs in GCD test is the same and can be factored out.
@amehsan amehsan deleted the gcd-refactor branch April 2, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants