From 0a1673bc7efedef7a6d734946bcb17f4b296dcd9 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Fri, 27 Jun 2025 19:32:17 +0100 Subject: [PATCH 1/2] [DA] Let getConstantPart return optional APInt (NFC) --- llvm/lib/Analysis/DependenceAnalysis.cpp | 55 +++++++++++------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp index c1b1d002c9979..1cc756d367aea 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -2375,17 +2375,15 @@ bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst, // Given a product, e.g., 10*X*Y, returns the first constant operand, // in this case 10. If there is no constant part, returns NULL. -static -const SCEVConstant *getConstantPart(const SCEV *Expr) { +static std::optional getConstantPart(const SCEV *Expr) { if (const auto *Constant = dyn_cast(Expr)) - return Constant; - else if (const auto *Product = dyn_cast(Expr)) + return Constant->getAPInt(); + if (const auto *Product = dyn_cast(Expr)) if (const auto *Constant = dyn_cast(Product->getOperand(0))) - return Constant; - return nullptr; + return Constant->getAPInt(); + return std::nullopt; } - //===----------------------------------------------------------------------===// // gcdMIVtest - // Tests an MIV subscript pair for dependence. @@ -2421,11 +2419,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, 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. - const auto *Constant = getConstantPart(Coeff); - if (!Constant) + std::optional ConstCoeff = getConstantPart(Coeff); + if (!ConstCoeff) return false; - APInt ConstCoeff = Constant->getAPInt(); - RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); + RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); Coefficients = AddRec->getStart(); } const SCEV *SrcConst = Coefficients; @@ -2440,11 +2437,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, 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. - const auto *Constant = getConstantPart(Coeff); - if (!Constant) + std::optional ConstCoeff = getConstantPart(Coeff); + if (!ConstCoeff) return false; - APInt ConstCoeff = Constant->getAPInt(); - RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); + RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); Coefficients = AddRec->getStart(); } const SCEV *DstConst = Coefficients; @@ -2463,12 +2459,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, else if (const SCEVMulExpr *Product = dyn_cast(Operand)) { // Search for constant operand to participate in GCD; // If none found; return false. - const SCEVConstant *ConstOp = getConstantPart(Product); + std::optional ConstOp = getConstantPart(Product); if (!ConstOp) return false; - APInt ConstOpValue = ConstOp->getAPInt(); - ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, - ConstOpValue.abs()); + ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, ConstOp->abs()); } else return false; @@ -2520,11 +2514,11 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, else { // If the coefficient is the product of a constant and other stuff, // we can use the constant in the GCD computation. - Constant = getConstantPart(Coeff); - if (!Constant) + std::optional ConstCoeff = getConstantPart(Coeff); + if (!ConstCoeff) return false; - APInt ConstCoeff = Constant->getAPInt(); - RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); + RunningGCD = + APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); } Inner = AddRec->getStart(); } @@ -2537,24 +2531,23 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst, else { // If the coefficient is the product of a constant and other stuff, // we can use the constant in the GCD computation. - Constant = getConstantPart(Coeff); - if (!Constant) + std::optional ConstCoeff = getConstantPart(Coeff); + if (!ConstCoeff) return false; - APInt ConstCoeff = Constant->getAPInt(); - RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); + RunningGCD = + APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); } Inner = AddRec->getStart(); } Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff); // If the coefficient is the product of a constant and other stuff, // we can use the constant in the GCD computation. - Constant = getConstantPart(Delta); - if (!Constant) + std::optional ConstCoeff = getConstantPart(Delta); + if (!ConstCoeff) // The difference of the two coefficients might not be a product // or constant, in which case we give up on this direction. continue; - APInt ConstCoeff = Constant->getAPInt(); - RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); + RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n"); if (RunningGCD != 0) { Remainder = ConstDelta.srem(RunningGCD); From c8ed6b691cdbcb3be26bc3255ce9d4116e57be34 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Fri, 27 Jun 2025 20:27:40 +0100 Subject: [PATCH 2/2] [DA] Update comment --- llvm/lib/Analysis/DependenceAnalysis.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp index 1cc756d367aea..b93f70004fb26 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -2372,9 +2372,8 @@ bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst, banerjeeMIVtest(Src, Dst, Loops, Result); } - // Given a product, e.g., 10*X*Y, returns the first constant operand, -// in this case 10. If there is no constant part, returns NULL. +// in this case 10. If there is no constant part, returns std::nullopt. static std::optional getConstantPart(const SCEV *Expr) { if (const auto *Constant = dyn_cast(Expr)) return Constant->getAPInt();