@@ -15528,7 +15528,8 @@ static bool collectDivisibilityInformation(
1552815528 const SCEV *Multiple =
1552915529 SE.getMulExpr(SE.getUDivExpr(URemLHS, URemRHS), URemRHS);
1553015530 DivInfo[URemLHS] = Multiple;
15531- Multiples[URemLHS] = cast<SCEVConstant>(URemRHS)->getAPInt();
15531+ if (auto *C = dyn_cast<SCEVConstant>(URemRHS))
15532+ Multiples[URemLHS] = C->getAPInt();
1553215533 return true;
1553315534}
1553415535
@@ -15543,7 +15544,7 @@ static bool isDivisibilityGuard(const SCEV *LHS, const SCEV *RHS,
1554315544// recursively. This is done by aligning up/down the constant value to the
1554415545// Divisor.
1554515546static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
15546- const SCEV * Divisor,
15547+ APInt Divisor,
1554715548 ScalarEvolution &SE) {
1554815549 // Return true if \p Expr is a MinMax SCEV expression with a non-negative
1554915550 // constant operand. If so, return in \p SCTy the SCEV type and in \p RHS
@@ -15574,10 +15575,8 @@ static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
1557415575 auto IsMin = isa<SCEVSMinExpr>(MinMaxExpr) || isa<SCEVUMinExpr>(MinMaxExpr);
1557515576 assert(SE.isKnownNonNegative(MinMaxLHS) && "Expected non-negative operand!");
1557615577 auto *DivisibleExpr =
15577- IsMin ? getPreviousSCEVDivisibleByDivisor(
15578- MinMaxLHS, cast<SCEVConstant>(Divisor)->getAPInt(), SE)
15579- : getNextSCEVDivisibleByDivisor(
15580- MinMaxLHS, cast<SCEVConstant>(Divisor)->getAPInt(), SE);
15578+ IsMin ? getPreviousSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE)
15579+ : getNextSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE);
1558115580 SmallVector<const SCEV *> Ops = {
1558215581 applyDivisibilityOnMinMaxExpr(MinMaxRHS, Divisor, SE), DivisibleExpr};
1558315582 return SE.getMinMaxExpr(SCTy, Ops);
@@ -15636,6 +15635,12 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1563615635 if (isa<SCEVConstant>(LHS) || SE.containsAddRecurrence(RHS))
1563715636 return;
1563815637
15638+ // If RHS is SCEVUnknown, make sure the information is applied to it.
15639+ if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15640+ std::swap(LHS, RHS);
15641+ Predicate = CmpInst::getSwappedPredicate(Predicate);
15642+ }
15643+
1563915644 // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
1564015645 // and \p FromRewritten are the same (i.e. there has been no rewrite
1564115646 // registered for \p From), then puts this value in the list of rewritten
@@ -15867,10 +15872,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1586715872 if (isa<SCEVConstant>(LHS)) {
1586815873 std::swap(LHS, RHS);
1586915874 Predicate = CmpInst::getSwappedPredicate(Predicate);
15870- } else if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15871- // If RHS is SCEVUnknown, make sure the information is applied to it.
15872- std::swap(LHS, RHS);
15873- Predicate = CmpInst::getSwappedPredicate(Predicate);
1587415875 }
1587515876 GuardsToProcess.emplace_back(Predicate, LHS, RHS);
1587615877 continue;
@@ -15900,11 +15901,11 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1590015901
1590115902 // Apply divisibility information last. This ensures it is applied to the
1590215903 // outermost expression after other rewrites for the given value.
15903- for (const auto &[K, V ] : Multiples) {
15904- const SCEV *DivisorSCEV = SE.getConstant(V );
15904+ for (const auto &[K, Divisor ] : Multiples) {
15905+ const SCEV *DivisorSCEV = SE.getConstant(Divisor );
1590515906 Guards.RewriteMap[K] =
1590615907 SE.getMulExpr(SE.getUDivExpr(applyDivisibilityOnMinMaxExpr(
15907- Guards.rewrite(K), DivisorSCEV , SE),
15908+ Guards.rewrite(K), Divisor , SE),
1590815909 DivisorSCEV),
1590915910 DivisorSCEV);
1591015911 ExprsToRewrite.push_back(K);
0 commit comments