@@ -15611,8 +15611,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1561115611 auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS,
1561215612 const SCEV *RHS,
1561315613 DenseMap<const SCEV *, const SCEV *> &RewriteMap,
15614- const DenseMap<const SCEV *, const SCEV *>
15615- &DivInfo) {
15614+ const LoopGuards &DivGuards) {
1561615615 // WARNING: It is generally unsound to apply any wrap flags to the proposed
1561715616 // replacement SCEV which isn't directly implied by the structure of that
1561815617 // SCEV. In particular, using contextual facts to imply flags is *NOT*
@@ -15654,12 +15653,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1565415653 if (isa<SCEVConstant>(LHS) || SE.containsAddRecurrence(RHS))
1565515654 return;
1565615655
15657- // If RHS is SCEVUnknown, make sure the information is applied to it.
15658- if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15659- std::swap(LHS, RHS);
15660- Predicate = CmpInst::getSwappedPredicate(Predicate);
15661- }
15662-
1566315656 // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
1566415657 // and \p FromRewritten are the same (i.e. there has been no rewrite
1566515658 // registered for \p From), then puts this value in the list of rewritten
@@ -15680,8 +15673,6 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1568015673
1568115674 const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
1568215675 // Apply divisibility information when computing the constant multiple.
15683- LoopGuards DivGuards(SE);
15684- DivGuards.RewriteMap = DivInfo;
1568515676 const APInt &DividesBy =
1568615677 SE.getConstantMultiple(DivGuards.rewrite(RewrittenLHS));
1568715678
@@ -15881,6 +15872,8 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1588115872 const auto *LHS = SE.getSCEV(Cmp->getOperand(0));
1588215873 const auto *RHS = SE.getSCEV(Cmp->getOperand(1));
1588315874 // If LHS is a constant, apply information to the other expression.
15875+ // TODO: If LHS is not a constant, check if using CompareSCEVComplexity
15876+ // can improve results.
1588415877 if (isa<SCEVConstant>(LHS)) {
1588515878 std::swap(LHS, RHS);
1588615879 Predicate = CmpInst::getSwappedPredicate(Predicate);
@@ -15898,17 +15891,18 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1589815891 }
1589915892 }
1590015893
15901- // Process divisibility guards in reverse order to populate DivInfo early.
15894+ // Process divisibility guards in reverse order to populate DivGuards early.
1590215895 DenseMap<const SCEV *, APInt> Multiples;
15903- DenseMap<const SCEV *, const SCEV *> DivInfo ;
15896+ LoopGuards DivGuards(SE) ;
1590415897 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess) {
1590515898 if (!isDivisibilityGuard(LHS, RHS, SE))
1590615899 continue;
15907- collectDivisibilityInformation(Predicate, LHS, RHS, DivInfo, Multiples, SE);
15900+ collectDivisibilityInformation(Predicate, LHS, RHS, DivGuards.RewriteMap,
15901+ Multiples, SE);
1590815902 }
1590915903
1591015904 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess)
15911- CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap, DivInfo );
15905+ CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap, DivGuards );
1591215906
1591315907 // Apply divisibility information last. This ensures it is applied to the
1591415908 // outermost expression after other rewrites for the given value.
0 commit comments