@@ -15567,47 +15567,34 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1556715567 return false;
1556815568 };
1556915569
15570- // Checks whether Expr is a non-negative constant, and Divisor is a positive
15571- // constant, and returns their APInt in ExprVal and in DivisorVal.
15572- auto GetNonNegExprAndPosDivisor = [&](const SCEV *Expr, const SCEV *Divisor,
15573- APInt &ExprVal, APInt &DivisorVal) {
15574- auto *ConstExpr = dyn_cast<SCEVConstant>(Expr);
15575- auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15576- if (!ConstExpr || !ConstDivisor)
15577- return false;
15578- ExprVal = ConstExpr->getAPInt();
15579- DivisorVal = ConstDivisor->getAPInt();
15580- return ExprVal.isNonNegative() && !DivisorVal.isNonPositive();
15581- };
15582-
1558315570 // Return a new SCEV that modifies \p Expr to the closest number divides by
15584- // \p Divisor and greater or equal than Expr.
15585- // For now, only handle constant Expr and Divisor .
15571+ // \p Divisor and greater or equal than Expr. For now, only handle constant
15572+ // Expr.
1558615573 auto GetNextSCEVDividesByDivisor = [&](const SCEV *Expr,
15587- const SCEV *Divisor ) {
15588- APInt ExprVal;
15589- APInt DivisorVal;
15590- if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15574+ const APInt &DivisorVal ) {
15575+ const APInt * ExprVal;
15576+ if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15577+ DivisorVal.isNonPositive( ))
1559115578 return Expr;
15592- APInt Rem = ExprVal. urem(DivisorVal);
15593- if (! Rem.isZero())
15594- // return the SCEV: Expr + Divisor - Expr % Divisor
15595- return SE.getConstant(ExprVal + DivisorVal - Rem);
15596- return Expr ;
15579+ APInt Rem = ExprVal-> urem(DivisorVal);
15580+ if (Rem.isZero())
15581+ return Expr;
15582+ // return the SCEV: Expr + Divisor - Expr % Divisor
15583+ return SE.getConstant(*ExprVal + DivisorVal - Rem) ;
1559715584 };
1559815585
1559915586 // Return a new SCEV that modifies \p Expr to the closest number divides by
15600- // \p Divisor and less or equal than Expr.
15601- // For now, only handle constant Expr and Divisor .
15587+ // \p Divisor and less or equal than Expr. For now, only handle constant
15588+ // Expr.
1560215589 auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr,
15603- const SCEV *Divisor ) {
15604- APInt ExprVal;
15605- APInt DivisorVal;
15606- if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15590+ const APInt &DivisorVal ) {
15591+ const APInt * ExprVal;
15592+ if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15593+ DivisorVal.isNonPositive( ))
1560715594 return Expr;
15608- APInt Rem = ExprVal. urem(DivisorVal);
15595+ APInt Rem = ExprVal-> urem(DivisorVal);
1560915596 // return the SCEV: Expr - Expr % Divisor
15610- return SE.getConstant(ExprVal - Rem);
15597+ return SE.getConstant(* ExprVal - Rem);
1561115598 };
1561215599
1561315600 // Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
@@ -15616,6 +15603,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1561615603 std::function<const SCEV *(const SCEV *, const SCEV *)>
1561715604 ApplyDivisibiltyOnMinMaxExpr = [&](const SCEV *MinMaxExpr,
1561815605 const SCEV *Divisor) {
15606+ auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15607+ if (!ConstDivisor)
15608+ return MinMaxExpr;
15609+ const APInt &DivisorVal = ConstDivisor->getAPInt();
15610+
1561915611 const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr;
1562015612 SCEVTypes SCTy;
1562115613 if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
@@ -15626,8 +15618,8 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1562615618 assert(SE.isKnownNonNegative(MinMaxLHS) &&
1562715619 "Expected non-negative operand!");
1562815620 auto *DivisibleExpr =
15629- IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor )
15630- : GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor );
15621+ IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, DivisorVal )
15622+ : GetNextSCEVDividesByDivisor(MinMaxLHS, DivisorVal );
1563115623 SmallVector<const SCEV *> Ops = {
1563215624 ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
1563315625 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15684,10 +15676,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1568415676 };
1568515677
1568615678 const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
15687- const SCEV *DividesBy = nullptr;
15688- const APInt &Multiple = SE.getConstantMultiple(RewrittenLHS);
15689- if (!Multiple.isOne())
15690- DividesBy = SE.getConstant(Multiple);
15679+ const APInt &DividesBy = SE.getConstantMultiple(RewrittenLHS);
1569115680
1569215681 // Collect rewrites for LHS and its transitive operands based on the
1569315682 // condition.
@@ -15709,21 +15698,21 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1570915698 [[fallthrough]];
1571015699 case CmpInst::ICMP_SLT: {
1571115700 RHS = SE.getMinusSCEV(RHS, One);
15712- RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15701+ RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1571315702 break;
1571415703 }
1571515704 case CmpInst::ICMP_UGT:
1571615705 case CmpInst::ICMP_SGT:
1571715706 RHS = SE.getAddExpr(RHS, One);
15718- RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15707+ RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1571915708 break;
1572015709 case CmpInst::ICMP_ULE:
1572115710 case CmpInst::ICMP_SLE:
15722- RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15711+ RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1572315712 break;
1572415713 case CmpInst::ICMP_UGE:
1572515714 case CmpInst::ICMP_SGE:
15726- RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15715+ RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1572715716 break;
1572815717 default:
1572915718 break;
@@ -15777,7 +15766,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1577715766 case CmpInst::ICMP_NE:
1577815767 if (match(RHS, m_scev_Zero())) {
1577915768 const SCEV *OneAlignedUp =
15780- DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One ;
15769+ GetNextSCEVDividesByDivisor(One, DividesBy);
1578115770 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
1578215771 }
1578315772 break;
0 commit comments