[llvm] [SCEV][NFC] Refactor LoopGuards range check matching (PR #210362)

Aleksandr Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 13:08:00 PDT 2026


================
@@ -16024,11 +16024,29 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
     // SCEV.  In particular, using contextual facts to imply flags is *NOT*
     // legal.  See the scoping rules for flags in the header to understand why.
 
+    // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
+    // and \p FromRewritten are the same (i.e. there has been no rewrite
+    // registered for \p From), then puts this value in the list of rewritten
+    // expressions.
+    auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten,
+                          const SCEV *To) {
+      if (From == FromRewritten)
+        ExprsToRewrite.push_back(From);
+      RewriteMap[From] = To;
+    };
+
+    // Checks whether \p S has already been rewritten. In that case returns the
+    // existing rewrite because we want to chain further rewrites onto the
+    // already rewritten value. Otherwise returns \p S.
+    auto GetMaybeRewritten = [&](const SCEV *S) {
+      return RewriteMap.lookup_or(S, S);
+    };
+
     // Check for a condition of the form (-C1 + X < C2).  InstCombine will
     // create this form when combining two checks of the form (X u< C2 + C1) and
     // (X >=u C1).
-    auto MatchRangeCheckIdiom = [&SE, Predicate, LHS, RHS, &RewriteMap,
-                                 &ExprsToRewrite]() {
+    auto MatchRangeCheckIdiom = [&](ICmpInst::Predicate Predicate,
+                                    const SCEV *LHS, const SCEV *RHS) {
----------------
aleks-tmb wrote:

Good catch, thanks. Renamed to Pred / MatchLHS / MatchRHS. 

I’d prefer to keep them as parameters. A follow-up patch, which is in progress, applies the matcher to subexpressions of the original condition that match the idiom. That is the motivation for this refactoring.

https://github.com/llvm/llvm-project/pull/210362


More information about the llvm-commits mailing list