@@ -15426,12 +15426,27 @@ void ScalarEvolution::LoopGuards::collectFromPHI(
1542615426 }
1542715427}
1542815428
15429+ // Return a new SCEV that modifies \p Expr to the closest number divides by
15430+ // \p Divisor and less or equal than Expr. For now, only handle constant
15431+ // Expr.
15432+ static const SCEV *getPreviousSCEVDivisibleByDivisor(const SCEV *Expr,
15433+ const APInt &DivisorVal,
15434+ ScalarEvolution &SE) {
15435+ const APInt *ExprVal;
15436+ if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15437+ DivisorVal.isNonPositive())
15438+ return Expr;
15439+ APInt Rem = ExprVal->urem(DivisorVal);
15440+ // return the SCEV: Expr - Expr % Divisor
15441+ return SE.getConstant(*ExprVal - Rem);
15442+ }
15443+
1542915444// Return a new SCEV that modifies \p Expr to the closest number divides by
1543015445// \p Divisor and greater or equal than Expr. For now, only handle constant
1543115446// Expr.
15432- static const SCEV *getNextSCEVDividesByDivisor (const SCEV *Expr,
15433- const APInt &DivisorVal,
15434- ScalarEvolution &SE) {
15447+ static const SCEV *getNextSCEVDivisibleByDivisor (const SCEV *Expr,
15448+ const APInt &DivisorVal,
15449+ ScalarEvolution &SE) {
1543515450 const APInt *ExprVal;
1543615451 if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
1543715452 DivisorVal.isNonPositive())
@@ -15516,20 +15531,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1551615531 return false;
1551715532 };
1551815533
15519- // Return a new SCEV that modifies \p Expr to the closest number divides by
15520- // \p Divisor and less or equal than Expr. For now, only handle constant
15521- // Expr.
15522- auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr,
15523- const APInt &DivisorVal) {
15524- const APInt *ExprVal;
15525- if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15526- DivisorVal.isNonPositive())
15527- return Expr;
15528- APInt Rem = ExprVal->urem(DivisorVal);
15529- // return the SCEV: Expr - Expr % Divisor
15530- return SE.getConstant(*ExprVal - Rem);
15531- };
15532-
1553315534 // Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
1553415535 // recursively. This is done by aligning up/down the constant value to the
1553515536 // Divisor.
@@ -15551,8 +15552,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1555115552 assert(SE.isKnownNonNegative(MinMaxLHS) &&
1555215553 "Expected non-negative operand!");
1555315554 auto *DivisibleExpr =
15554- IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, DivisorVal)
15555- : getNextSCEVDividesByDivisor(MinMaxLHS, DivisorVal, SE);
15555+ IsMin
15556+ ? getPreviousSCEVDivisibleByDivisor(MinMaxLHS, DivisorVal, SE)
15557+ : getNextSCEVDivisibleByDivisor(MinMaxLHS, DivisorVal, SE);
1555615558 SmallVector<const SCEV *> Ops = {
1555715559 ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
1555815560 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15629,21 +15631,21 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1562915631 [[fallthrough]];
1563015632 case CmpInst::ICMP_SLT: {
1563115633 RHS = SE.getMinusSCEV(RHS, One);
15632- RHS = GetPreviousSCEVDividesByDivisor (RHS, DividesBy);
15634+ RHS = getPreviousSCEVDivisibleByDivisor (RHS, DividesBy, SE );
1563315635 break;
1563415636 }
1563515637 case CmpInst::ICMP_UGT:
1563615638 case CmpInst::ICMP_SGT:
1563715639 RHS = SE.getAddExpr(RHS, One);
15638- RHS = getNextSCEVDividesByDivisor (RHS, DividesBy, SE);
15640+ RHS = getNextSCEVDivisibleByDivisor (RHS, DividesBy, SE);
1563915641 break;
1564015642 case CmpInst::ICMP_ULE:
1564115643 case CmpInst::ICMP_SLE:
15642- RHS = GetPreviousSCEVDividesByDivisor (RHS, DividesBy);
15644+ RHS = getPreviousSCEVDivisibleByDivisor (RHS, DividesBy, SE );
1564315645 break;
1564415646 case CmpInst::ICMP_UGE:
1564515647 case CmpInst::ICMP_SGE:
15646- RHS = getNextSCEVDividesByDivisor (RHS, DividesBy, SE);
15648+ RHS = getNextSCEVDivisibleByDivisor (RHS, DividesBy, SE);
1564715649 break;
1564815650 default:
1564915651 break;
@@ -15697,7 +15699,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1569715699 case CmpInst::ICMP_NE:
1569815700 if (match(RHS, m_scev_Zero())) {
1569915701 const SCEV *OneAlignedUp =
15700- getNextSCEVDividesByDivisor (One, DividesBy, SE);
15702+ getNextSCEVDivisibleByDivisor (One, DividesBy, SE);
1570115703 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
1570215704 } else {
1570315705 // LHS != RHS can be rewritten as (LHS - RHS) = UMax(1, LHS - RHS),
@@ -15924,7 +15926,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1592415926 if (LHS > RHS)
1592515927 std::swap(LHS, RHS);
1592615928 if (NotEqual.contains({LHS, RHS})) {
15927- const SCEV *OneAlignedUp = getNextSCEVDividesByDivisor (
15929+ const SCEV *OneAlignedUp = getNextSCEVDivisibleByDivisor (
1592815930 SE.getOne(S->getType()), SE.getConstantMultiple(S), SE);
1592915931 return SE.getUMaxExpr(OneAlignedUp, S);
1593015932 }
0 commit comments