@@ -15581,6 +15581,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1558115581 Predicate = CmpInst::getSwappedPredicate(Predicate);
1558215582 }
1558315583
15584+ unsigned StartSize = RewriteMap.size();
1558415585 // Check for a condition of the form (-C1 + X < C2). InstCombine will
1558515586 // create this form when combining two checks of the form (X u< C2 + C1) and
1558615587 // (X >=u C1).
@@ -15931,6 +15932,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1593115932 // Guards.RewriteMap. Conditions are processed in reverse order, so the
1593215933 // earliest conditions is processed first. This ensures the SCEVs with the
1593315934 // shortest dependency chains are constructed first.
15935+ SmallVector<std::tuple<CmpInst::Predicate, const SCEV *, const SCEV *>> ToProcess;
1593415936 for (auto [Term, EnterIfTrue] : reverse(Terms)) {
1593515937 SmallVector<Value *, 8> Worklist;
1593615938 SmallPtrSet<Value *, 8> Visited;
@@ -15945,7 +15947,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1594515947 EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate();
1594615948 const auto *LHS = SE.getSCEV(Cmp->getOperand(0));
1594715949 const auto *RHS = SE.getSCEV(Cmp->getOperand(1));
15948- CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);
15950+ // dbgs() << "\n" << *Cmp << "\n";
15951+
15952+ ToProcess.emplace_back(Predicate, LHS, RHS);
1594915953 continue;
1595015954 }
1595115955
@@ -15958,6 +15962,31 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1595815962 }
1595915963 }
1596015964
15965+ ToProcess = to_vector(ToProcess);
15966+ sort(ToProcess, [&](const auto &A, const auto &B) {
15967+
15968+ const SCEV *X, *Y;
15969+ const auto &[APred, ALHS , ARHS] = A;
15970+ const auto &[BPred, BLHS , BRHS] = B;
15971+ bool AIsDiv = SE.matchURem(ALHS, X, Y) && ARHS->isZero();
15972+ bool BIsDiv = SE.matchURem(BLHS, X, Y) && BRHS->isZero();
15973+ return BIsDiv < AIsDiv;
15974+ /*bool AConst = isa<SCEVConstant>(ARHS);*/
15975+ /*// bool AEq = !(APred == CmpInst::ICMP_EQ || APred == CmpInst::ICMP_NE);*/
15976+ /*bool BConst = isa<SCEVConstant>(BRHS);*/
15977+ /*//bool BEq = !(BPred == CmpInst::ICMP_EQ || BPred == CmpInst::ICMP_NE);*/
15978+ /*return std::tie(AConst) < std::tie(BConst);*/
15979+ });
15980+ for (const auto &[Predicate, LHS, RHS]: ToProcess) {
15981+ CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);
15982+ }
15983+
15984+ /* for (const auto &[Predicate, LHS, RHS]: reverse(ToProcess)) {*/
15985+ /*CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);*/
15986+ /*}*/
15987+
15988+
15989+
1596115990 // Let the rewriter preserve NUW/NSW flags if the unsigned/signed ranges of
1596215991 // the replacement expressions are contained in the ranges of the replaced
1596315992 // expressions.
@@ -16096,6 +16125,11 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1609616125 if (RewriteMap.empty())
1609716126 return Expr;
1609816127
16128+ /* dbgs() << "RWM\n";*/
16129+ /*for (const auto &[From, To] : RewriteMap) {*/
16130+ /*dbgs() << "From " << *From << " to " << *To << "\n";*/
16131+
16132+ /*}*/
1609916133 SCEVLoopGuardRewriter Rewriter(SE, *this);
1610016134 return Rewriter.visit(Expr);
1610116135}
0 commit comments