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..aa5ae814c934f 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -1770,15 +1770,15 @@ static std::optional 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(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(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 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,13 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, unsigned BitWidth = SE->getTypeSizeInBits(Src->getType()); APInt RunningGCD = APInt::getZero(BitWidth); - // Examine Src and dst coefficients. - const SCEV *SrcConst = analyzeCoefficientsForGCD(Src, RunningGCD, SE); + const SCEV *Dummy = nullptr; + 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;