@@ -15473,12 +15473,27 @@ void ScalarEvolution::LoopGuards::collectFromPHI(
1547315473 }
1547415474}
1547515475
15476+ // Return a new SCEV that modifies \p Expr to the closest number divides by
15477+ // \p Divisor and less or equal than Expr. For now, only handle constant
15478+ // Expr.
15479+ static const SCEV *getPreviousSCEVDivisibleByDivisor(const SCEV *Expr,
15480+ const APInt &DivisorVal,
15481+ ScalarEvolution &SE) {
15482+ const APInt *ExprVal;
15483+ if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15484+ DivisorVal.isNonPositive())
15485+ return Expr;
15486+ APInt Rem = ExprVal->urem(DivisorVal);
15487+ // return the SCEV: Expr - Expr % Divisor
15488+ return SE.getConstant(*ExprVal - Rem);
15489+ }
15490+
1547615491// Return a new SCEV that modifies \p Expr to the closest number divides by
1547715492// \p Divisor and greater or equal than Expr. For now, only handle constant
1547815493// Expr.
15479- static const SCEV *getNextSCEVDividesByDivisor (const SCEV *Expr,
15480- const APInt &DivisorVal,
15481- ScalarEvolution &SE) {
15494+ static const SCEV *getNextSCEVDivisibleByDivisor (const SCEV *Expr,
15495+ const APInt &DivisorVal,
15496+ ScalarEvolution &SE) {
1548215497 const APInt *ExprVal;
1548315498 if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
1548415499 DivisorVal.isNonPositive())
@@ -15557,20 +15572,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1555715572 match(LHS, m_scev_APInt(C)) && C->isNonNegative();
1555815573 };
1555915574
15560- // Return a new SCEV that modifies \p Expr to the closest number divides by
15561- // \p Divisor and less or equal than Expr. For now, only handle constant
15562- // Expr.
15563- auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr,
15564- const APInt &DivisorVal) {
15565- const APInt *ExprVal;
15566- if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15567- DivisorVal.isNonPositive())
15568- return Expr;
15569- APInt Rem = ExprVal->urem(DivisorVal);
15570- // return the SCEV: Expr - Expr % Divisor
15571- return SE.getConstant(*ExprVal - Rem);
15572- };
15573-
1557415575 // Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
1557515576 // recursively. This is done by aligning up/down the constant value to the
1557615577 // Divisor.
@@ -15592,8 +15593,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1559215593 assert(SE.isKnownNonNegative(MinMaxLHS) &&
1559315594 "Expected non-negative operand!");
1559415595 auto *DivisibleExpr =
15595- IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, DivisorVal)
15596- : getNextSCEVDividesByDivisor(MinMaxLHS, DivisorVal, SE);
15596+ IsMin
15597+ ? getPreviousSCEVDivisibleByDivisor(MinMaxLHS, DivisorVal, SE)
15598+ : getNextSCEVDivisibleByDivisor(MinMaxLHS, DivisorVal, SE);
1559715599 SmallVector<const SCEV *> Ops = {
1559815600 ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
1559915601 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15670,21 +15672,21 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1567015672 [[fallthrough]];
1567115673 case CmpInst::ICMP_SLT: {
1567215674 RHS = SE.getMinusSCEV(RHS, One);
15673- RHS = GetPreviousSCEVDividesByDivisor (RHS, DividesBy);
15675+ RHS = getPreviousSCEVDivisibleByDivisor (RHS, DividesBy, SE );
1567415676 break;
1567515677 }
1567615678 case CmpInst::ICMP_UGT:
1567715679 case CmpInst::ICMP_SGT:
1567815680 RHS = SE.getAddExpr(RHS, One);
15679- RHS = getNextSCEVDividesByDivisor (RHS, DividesBy, SE);
15681+ RHS = getNextSCEVDivisibleByDivisor (RHS, DividesBy, SE);
1568015682 break;
1568115683 case CmpInst::ICMP_ULE:
1568215684 case CmpInst::ICMP_SLE:
15683- RHS = GetPreviousSCEVDividesByDivisor (RHS, DividesBy);
15685+ RHS = getPreviousSCEVDivisibleByDivisor (RHS, DividesBy, SE );
1568415686 break;
1568515687 case CmpInst::ICMP_UGE:
1568615688 case CmpInst::ICMP_SGE:
15687- RHS = getNextSCEVDividesByDivisor (RHS, DividesBy, SE);
15689+ RHS = getNextSCEVDivisibleByDivisor (RHS, DividesBy, SE);
1568815690 break;
1568915691 default:
1569015692 break;
@@ -15738,7 +15740,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1573815740 case CmpInst::ICMP_NE:
1573915741 if (match(RHS, m_scev_Zero())) {
1574015742 const SCEV *OneAlignedUp =
15741- getNextSCEVDividesByDivisor (One, DividesBy, SE);
15743+ getNextSCEVDivisibleByDivisor (One, DividesBy, SE);
1574215744 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
1574315745 } else {
1574415746 // LHS != RHS can be rewritten as (LHS - RHS) = UMax(1, LHS - RHS),
@@ -15965,7 +15967,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1596515967 if (LHS > RHS)
1596615968 std::swap(LHS, RHS);
1596715969 if (NotEqual.contains({LHS, RHS})) {
15968- const SCEV *OneAlignedUp = getNextSCEVDividesByDivisor (
15970+ const SCEV *OneAlignedUp = getNextSCEVDivisibleByDivisor (
1596915971 SE.getOne(S->getType()), SE.getConstantMultiple(S), SE);
1597015972 return SE.getUMaxExpr(OneAlignedUp, S);
1597115973 }
0 commit comments