@@ -15633,47 +15633,34 @@ 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
15650- // \p Divisor and greater or equal than Expr.
15651- // For now, only handle constant Expr and Divisor .
15637+ // \p Divisor and greater or equal than Expr. For now, only handle constant
15638+ // 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() ||
15643+ DivisorVal.isNonPositive( ))
1565715644 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 ;
15645+ APInt Rem = ExprVal-> urem(DivisorVal);
15646+ if (Rem.isZero())
15647+ return Expr;
15648+ // return the SCEV: Expr + Divisor - Expr % Divisor
15649+ return SE.getConstant(*ExprVal + DivisorVal - Rem) ;
1566315650 };
1566415651
1566515652 // Return a new SCEV that modifies \p Expr to the closest number divides by
15666- // \p Divisor and less or equal than Expr.
15667- // For now, only handle constant Expr and Divisor .
15653+ // \p Divisor and less or equal than Expr. For now, only handle constant
15654+ // Expr.
1566815655 auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr,
15669- const SCEV *Divisor ) {
15670- APInt ExprVal;
15671- APInt DivisorVal;
15672- if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15656+ const APInt &DivisorVal ) {
15657+ const APInt * ExprVal;
15658+ if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15659+ DivisorVal.isNonPositive( ))
1567315660 return Expr;
15674- APInt Rem = ExprVal. urem(DivisorVal);
15661+ APInt Rem = ExprVal-> urem(DivisorVal);
1567515662 // return the SCEV: Expr - Expr % Divisor
15676- return SE.getConstant(ExprVal - Rem);
15663+ return SE.getConstant(* ExprVal - Rem);
1567715664 };
1567815665
1567915666 // Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
@@ -15682,6 +15669,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1568215669 std::function<const SCEV *(const SCEV *, const SCEV *)>
1568315670 ApplyDivisibiltyOnMinMaxExpr = [&](const SCEV *MinMaxExpr,
1568415671 const SCEV *Divisor) {
15672+ auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15673+ if (!ConstDivisor)
15674+ return MinMaxExpr;
15675+ const APInt &DivisorVal = ConstDivisor->getAPInt();
15676+
1568515677 const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr;
1568615678 SCEVTypes SCTy;
1568715679 if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
@@ -15692,8 +15684,8 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1569215684 assert(SE.isKnownNonNegative(MinMaxLHS) &&
1569315685 "Expected non-negative operand!");
1569415686 auto *DivisibleExpr =
15695- IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor )
15696- : GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor );
15687+ IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, DivisorVal )
15688+ : GetNextSCEVDividesByDivisor(MinMaxLHS, DivisorVal );
1569715689 SmallVector<const SCEV *> Ops = {
1569815690 ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
1569915691 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15750,10 +15742,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1575015742 };
1575115743
1575215744 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);
15745+ const APInt &DividesBy = SE.getConstantMultiple(RewrittenLHS);
1575715746
1575815747 // Collect rewrites for LHS and its transitive operands based on the
1575915748 // condition.
@@ -15775,21 +15764,21 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1577515764 [[fallthrough]];
1577615765 case CmpInst::ICMP_SLT: {
1577715766 RHS = SE.getMinusSCEV(RHS, One);
15778- RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15767+ RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1577915768 break;
1578015769 }
1578115770 case CmpInst::ICMP_UGT:
1578215771 case CmpInst::ICMP_SGT:
1578315772 RHS = SE.getAddExpr(RHS, One);
15784- RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15773+ RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1578515774 break;
1578615775 case CmpInst::ICMP_ULE:
1578715776 case CmpInst::ICMP_SLE:
15788- RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15777+ RHS = GetPreviousSCEVDividesByDivisor(RHS, DividesBy);
1578915778 break;
1579015779 case CmpInst::ICMP_UGE:
1579115780 case CmpInst::ICMP_SGE:
15792- RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS ;
15781+ RHS = GetNextSCEVDividesByDivisor(RHS, DividesBy);
1579315782 break;
1579415783 default:
1579515784 break;
@@ -15843,7 +15832,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1584315832 case CmpInst::ICMP_NE:
1584415833 if (match(RHS, m_scev_Zero())) {
1584515834 const SCEV *OneAlignedUp =
15846- DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One ;
15835+ GetNextSCEVDividesByDivisor(One, DividesBy);
1584715836 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
1584815837 }
1584915838 break;
0 commit comments