Skip to content

Commit 7b597b4

Browse files
committed
[SCEV] Use APInt for DividesBy when collecting loop guard info (NFC).
Follow-up as suggested in llvm#162617. Just use an APInt for DividesBy, as the existing code already operates on APInt and thus handles the case of DividesBy being 1.
1 parent ae7b15f commit 7b597b4

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15633,47 +15633,32 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1563315633
return false;
1563415634
};
1563515635

15636-
// Checks whether Expr is a non-negative constant, and Divisor is a positive
15637-
// constant, and returns their APInt in ExprVal and in DivisorVal.
15638-
auto GetNonNegExprAndPosDivisor = [&](const SCEV *Expr, const SCEV *Divisor,
15639-
APInt &ExprVal, APInt &DivisorVal) {
15640-
auto *ConstExpr = dyn_cast<SCEVConstant>(Expr);
15641-
auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15642-
if (!ConstExpr || !ConstDivisor)
15643-
return false;
15644-
ExprVal = ConstExpr->getAPInt();
15645-
DivisorVal = ConstDivisor->getAPInt();
15646-
return ExprVal.isNonNegative() && !DivisorVal.isNonPositive();
15647-
};
15648-
1564915636
// Return a new SCEV that modifies \p Expr to the closest number divides by
1565015637
// \p Divisor and greater or equal than Expr.
15651-
// For now, only handle constant Expr and Divisor.
15638+
// For now, only handle constant Expr.
1565215639
auto GetNextSCEVDividesByDivisor = [&](const SCEV *Expr,
15653-
const SCEV *Divisor) {
15654-
APInt ExprVal;
15655-
APInt DivisorVal;
15656-
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15640+
const APInt &DivisorVal) {
15641+
const APInt *ExprVal;
15642+
if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative())
1565715643
return Expr;
15658-
APInt Rem = ExprVal.urem(DivisorVal);
15659-
if (!Rem.isZero())
15660-
// return the SCEV: Expr + Divisor - Expr % Divisor
15661-
return SE.getConstant(ExprVal + DivisorVal - Rem);
15662-
return Expr;
15644+
APInt Rem = ExprVal->urem(DivisorVal);
15645+
if (Rem.isZero())
15646+
return Expr;
15647+
// return the SCEV: Expr + Divisor - Expr % Divisor
15648+
return SE.getConstant(*ExprVal + DivisorVal - Rem);
1566315649
};
1566415650

1566515651
// Return a new SCEV that modifies \p Expr to the closest number divides by
1566615652
// \p Divisor and less or equal than Expr.
15667-
// For now, only handle constant Expr and Divisor.
15653+
// For now, only handle constant Expr.
1566815654
auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr,
15669-
const SCEV *Divisor) {
15670-
APInt ExprVal;
15671-
APInt DivisorVal;
15672-
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15655+
const APInt &DivisorVal) {
15656+
const APInt *ExprVal;
15657+
if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative())
1567315658
return Expr;
15674-
APInt Rem = ExprVal.urem(DivisorVal);
15659+
APInt Rem = ExprVal->urem(DivisorVal);
1567515660
// return the SCEV: Expr - Expr % Divisor
15676-
return SE.getConstant(ExprVal - Rem);
15661+
return SE.getConstant(*ExprVal - Rem);
1567715662
};
1567815663

1567915664
// Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
@@ -15682,6 +15667,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1568215667
std::function<const SCEV *(const SCEV *, const SCEV *)>
1568315668
ApplyDivisibiltyOnMinMaxExpr = [&](const SCEV *MinMaxExpr,
1568415669
const SCEV *Divisor) {
15670+
auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15671+
if (!ConstDivisor)
15672+
return MinMaxExpr;
15673+
const APInt &DivisorVal = ConstDivisor->getAPInt();
15674+
1568515675
const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr;
1568615676
SCEVTypes SCTy;
1568715677
if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
@@ -15692,8 +15682,8 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1569215682
assert(SE.isKnownNonNegative(MinMaxLHS) &&
1569315683
"Expected non-negative operand!");
1569415684
auto *DivisibleExpr =
15695-
IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor)
15696-
: GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor);
15685+
IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, DivisorVal)
15686+
: GetNextSCEVDividesByDivisor(MinMaxLHS, DivisorVal);
1569715687
SmallVector<const SCEV *> Ops = {
1569815688
ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
1569915689
return SE.getMinMaxExpr(SCTy, Ops);
@@ -15750,10 +15740,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1575015740
};
1575115741

1575215742
const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
15753-
const SCEV *DividesBy = nullptr;
15754-
const APInt &Multiple = SE.getConstantMultiple(RewrittenLHS);
15755-
if (!Multiple.isOne())
15756-
DividesBy = SE.getConstant(Multiple);
15743+
const APInt &DividesBy = SE.getConstantMultiple(RewrittenLHS);
1575715744

1575815745
// Collect rewrites for LHS and its transitive operands based on the
1575915746
// condition.
@@ -15775,21 +15762,21 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1577515762
[[fallthrough]];
1577615763
case CmpInst::ICMP_SLT: {
1577715764
RHS = SE.getMinusSCEV(RHS, One);
15778-
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS;
15765+
RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1577915766
break;
1578015767
}
1578115768
case CmpInst::ICMP_UGT:
1578215769
case CmpInst::ICMP_SGT:
1578315770
RHS = SE.getAddExpr(RHS, One);
15784-
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS;
15771+
RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1578515772
break;
1578615773
case CmpInst::ICMP_ULE:
1578715774
case CmpInst::ICMP_SLE:
15788-
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS;
15775+
RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1578915776
break;
1579015777
case CmpInst::ICMP_UGE:
1579115778
case CmpInst::ICMP_SGE:
15792-
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS;
15779+
RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1579315780
break;
1579415781
default:
1579515782
break;
@@ -15843,7 +15830,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1584315830
case CmpInst::ICMP_NE:
1584415831
if (match(RHS, m_scev_Zero())) {
1584515832
const SCEV *OneAlignedUp =
15846-
DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One;
15833+
GetNextSCEVDividesByDivisor(One, DividesBy);
1584715834
To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
1584815835
}
1584915836
break;

0 commit comments

Comments
 (0)