Skip to content

Commit 5ea29f7

Browse files
authored
[DA] Let getConstantPart return optional APInt (NFC) (llvm#146135)
To use the result of an SCEVConstant, we need to extract the APInt, which callers anyway do.
1 parent 04cd0f2 commit 5ea29f7

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,20 +2372,17 @@ bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
23722372
banerjeeMIVtest(Src, Dst, Loops, Result);
23732373
}
23742374

2375-
23762375
// Given a product, e.g., 10*X*Y, returns the first constant operand,
2377-
// in this case 10. If there is no constant part, returns NULL.
2378-
static
2379-
const SCEVConstant *getConstantPart(const SCEV *Expr) {
2376+
// in this case 10. If there is no constant part, returns std::nullopt.
2377+
static std::optional<APInt> getConstantPart(const SCEV *Expr) {
23802378
if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2381-
return Constant;
2382-
else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2379+
return Constant->getAPInt();
2380+
if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
23832381
if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
2384-
return Constant;
2385-
return nullptr;
2382+
return Constant->getAPInt();
2383+
return std::nullopt;
23862384
}
23872385

2388-
23892386
//===----------------------------------------------------------------------===//
23902387
// gcdMIVtest -
23912388
// Tests an MIV subscript pair for dependence.
@@ -2421,11 +2418,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
24212418
const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
24222419
// If the coefficient is the product of a constant and other stuff,
24232420
// we can use the constant in the GCD computation.
2424-
const auto *Constant = getConstantPart(Coeff);
2425-
if (!Constant)
2421+
std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2422+
if (!ConstCoeff)
24262423
return false;
2427-
APInt ConstCoeff = Constant->getAPInt();
2428-
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2424+
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
24292425
Coefficients = AddRec->getStart();
24302426
}
24312427
const SCEV *SrcConst = Coefficients;
@@ -2440,11 +2436,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
24402436
const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
24412437
// If the coefficient is the product of a constant and other stuff,
24422438
// we can use the constant in the GCD computation.
2443-
const auto *Constant = getConstantPart(Coeff);
2444-
if (!Constant)
2439+
std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2440+
if (!ConstCoeff)
24452441
return false;
2446-
APInt ConstCoeff = Constant->getAPInt();
2447-
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2442+
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
24482443
Coefficients = AddRec->getStart();
24492444
}
24502445
const SCEV *DstConst = Coefficients;
@@ -2463,12 +2458,10 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
24632458
else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
24642459
// Search for constant operand to participate in GCD;
24652460
// If none found; return false.
2466-
const SCEVConstant *ConstOp = getConstantPart(Product);
2461+
std::optional<APInt> ConstOp = getConstantPart(Product);
24672462
if (!ConstOp)
24682463
return false;
2469-
APInt ConstOpValue = ConstOp->getAPInt();
2470-
ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2471-
ConstOpValue.abs());
2464+
ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, ConstOp->abs());
24722465
}
24732466
else
24742467
return false;
@@ -2520,11 +2513,11 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
25202513
else {
25212514
// If the coefficient is the product of a constant and other stuff,
25222515
// we can use the constant in the GCD computation.
2523-
Constant = getConstantPart(Coeff);
2524-
if (!Constant)
2516+
std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2517+
if (!ConstCoeff)
25252518
return false;
2526-
APInt ConstCoeff = Constant->getAPInt();
2527-
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2519+
RunningGCD =
2520+
APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
25282521
}
25292522
Inner = AddRec->getStart();
25302523
}
@@ -2537,24 +2530,23 @@ bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
25372530
else {
25382531
// If the coefficient is the product of a constant and other stuff,
25392532
// we can use the constant in the GCD computation.
2540-
Constant = getConstantPart(Coeff);
2541-
if (!Constant)
2533+
std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2534+
if (!ConstCoeff)
25422535
return false;
2543-
APInt ConstCoeff = Constant->getAPInt();
2544-
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2536+
RunningGCD =
2537+
APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
25452538
}
25462539
Inner = AddRec->getStart();
25472540
}
25482541
Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
25492542
// If the coefficient is the product of a constant and other stuff,
25502543
// we can use the constant in the GCD computation.
2551-
Constant = getConstantPart(Delta);
2552-
if (!Constant)
2544+
std::optional<APInt> ConstCoeff = getConstantPart(Delta);
2545+
if (!ConstCoeff)
25532546
// The difference of the two coefficients might not be a product
25542547
// or constant, in which case we give up on this direction.
25552548
continue;
2556-
APInt ConstCoeff = Constant->getAPInt();
2557-
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2549+
RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
25582550
LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
25592551
if (RunningGCD != 0) {
25602552
Remainder = ConstDelta.srem(RunningGCD);

0 commit comments

Comments
 (0)