@@ -15523,7 +15523,8 @@ static bool collectDivisibilityInformation(
1552315523 const SCEV *Multiple =
1552415524 SE.getMulExpr(SE.getUDivExpr(URemLHS, URemRHS), URemRHS);
1552515525 DivInfo[URemLHS] = Multiple;
15526- Multiples[URemLHS] = cast<SCEVConstant>(URemRHS)->getAPInt();
15526+ if (auto *C = dyn_cast<SCEVConstant>(URemRHS))
15527+ Multiples[URemLHS] = C->getAPInt();
1552715528 return true;
1552815529}
1552915530
@@ -15538,7 +15539,7 @@ static bool isDivisibilityGuard(const SCEV *LHS, const SCEV *RHS,
1553815539// recursively. This is done by aligning up/down the constant value to the
1553915540// Divisor.
1554015541static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
15541- const SCEV * Divisor,
15542+ APInt Divisor,
1554215543 ScalarEvolution &SE) {
1554315544 // Return true if \p Expr is a MinMax SCEV expression with a non-negative
1554415545 // constant operand. If so, return in \p SCTy the SCEV type and in \p RHS
@@ -15569,10 +15570,8 @@ static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
1556915570 auto IsMin = isa<SCEVSMinExpr>(MinMaxExpr) || isa<SCEVUMinExpr>(MinMaxExpr);
1557015571 assert(SE.isKnownNonNegative(MinMaxLHS) && "Expected non-negative operand!");
1557115572 auto *DivisibleExpr =
15572- IsMin ? getPreviousSCEVDivisibleByDivisor(
15573- MinMaxLHS, cast<SCEVConstant>(Divisor)->getAPInt(), SE)
15574- : getNextSCEVDivisibleByDivisor(
15575- MinMaxLHS, cast<SCEVConstant>(Divisor)->getAPInt(), SE);
15573+ IsMin ? getPreviousSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE)
15574+ : getNextSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE);
1557615575 SmallVector<const SCEV *> Ops = {
1557715576 applyDivisibilityOnMinMaxExpr(MinMaxRHS, Divisor, SE), DivisibleExpr};
1557815577 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15631,6 +15630,12 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1563115630 if (isa<SCEVConstant>(LHS) || SE.containsAddRecurrence(RHS))
1563215631 return;
1563315632
15633+ // If RHS is SCEVUnknown, make sure the information is applied to it.
15634+ if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15635+ std::swap(LHS, RHS);
15636+ Predicate = CmpInst::getSwappedPredicate(Predicate);
15637+ }
15638+
1563415639 // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
1563515640 // and \p FromRewritten are the same (i.e. there has been no rewrite
1563615641 // registered for \p From), then puts this value in the list of rewritten
@@ -15862,10 +15867,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1586215867 if (isa<SCEVConstant>(LHS)) {
1586315868 std::swap(LHS, RHS);
1586415869 Predicate = CmpInst::getSwappedPredicate(Predicate);
15865- } else if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15866- // If RHS is SCEVUnknown, make sure the information is applied to it.
15867- std::swap(LHS, RHS);
15868- Predicate = CmpInst::getSwappedPredicate(Predicate);
1586915870 }
1587015871 GuardsToProcess.emplace_back(Predicate, LHS, RHS);
1587115872 continue;
@@ -15895,11 +15896,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1589515896
1589615897 // Apply divisibility information last. This ensures it is applied to the
1589715898 // outermost expression after other rewrites for the given value.
15898- for (const auto &[K, V ] : Multiples) {
15899- const SCEV *DivisorSCEV = SE.getConstant(V );
15899+ for (const auto &[K, Divisor ] : Multiples) {
15900+ const SCEV *DivisorSCEV = SE.getConstant(Divisor );
1590015901 Guards.RewriteMap[K] =
1590115902 SE.getMulExpr(SE.getUDivExpr(applyDivisibilityOnMinMaxExpr(
15902- Guards.rewrite(K), DivisorSCEV , SE),
15903+ Guards.rewrite(K), Divisor , SE),
1590315904 DivisorSCEV),
1590415905 DivisorSCEV);
1590515906 ExprsToRewrite.push_back(K);
0 commit comments