diff options
Diffstat (limited to 'llvm/lib/Analysis/DependenceAnalysis.cpp')
| -rw-r--r-- | llvm/lib/Analysis/DependenceAnalysis.cpp | 22 | 
1 files changed, 13 insertions, 9 deletions
| diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp index 84ee8c0..11d8294 100644 --- a/llvm/lib/Analysis/DependenceAnalysis.cpp +++ b/llvm/lib/Analysis/DependenceAnalysis.cpp @@ -2854,14 +2854,18 @@ 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 std::nullopt. -static std::optional<APInt> getConstantPart(const SCEV *Expr) { +/// Given a SCEVMulExpr, returns its first operand if its first operand is a +/// constant and the product doesn't overflow in a signed sense. Otherwise, +/// returns std::nullopt. For example, given (10 * X * Y)<nsw>, it returns 10. +/// Notably, if it doesn't have nsw, the multiplication may overflow, and if +/// so, it may not a multiple of 10. +static std::optional<APInt> getConstanCoefficient(const SCEV *Expr) {    if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))      return Constant->getAPInt();    if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))      if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0))) -      return Constant->getAPInt(); +      if (Product->hasNoSignedWrap()) +        return Constant->getAPInt();    return std::nullopt;  } @@ -2887,7 +2891,7 @@ bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,    if (AddRec->getLoop() == CurLoop) {      CurLoopCoeff = Step;    } else { -    std::optional<APInt> ConstCoeff = getConstantPart(Step); +    std::optional<APInt> ConstCoeff = getConstanCoefficient(Step);      // If the coefficient is the product of a constant and other stuff, we can      // use the constant in the GCD computation. @@ -2940,7 +2944,7 @@ 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. -    std::optional<APInt> ConstCoeff = getConstantPart(Coeff); +    std::optional<APInt> ConstCoeff = getConstanCoefficient(Coeff);      if (!ConstCoeff)        return false;      RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); @@ -2958,7 +2962,7 @@ 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. -    std::optional<APInt> ConstCoeff = getConstantPart(Coeff); +    std::optional<APInt> ConstCoeff = getConstanCoefficient(Coeff);      if (!ConstCoeff)        return false;      RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs()); @@ -2979,7 +2983,7 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,        } else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {          // Search for constant operand to participate in GCD;          // If none found; return false. -        std::optional<APInt> ConstOp = getConstantPart(Product); +        std::optional<APInt> ConstOp = getConstanCoefficient(Product);          if (!ConstOp)            return false;          ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, ConstOp->abs()); @@ -3032,7 +3036,7 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,      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. -    std::optional<APInt> ConstCoeff = getConstantPart(Delta); +    std::optional<APInt> ConstCoeff = getConstanCoefficient(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. | 
