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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 12:47:57 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) {
----------------
artagnon wrote:

```suggestion
    auto MatchRangeCheckIdiom = [&SE, &RewriteMap, &ExprsToRewrite, &AddRewrite](ICmpInst::Predicate Predicate,
                                    const SCEV *LHS, const SCEV *RHS) {
```

A bit unfortunate, but probably best not to overshadow the lambda capture with arguments? Either that, or revert to no-arguments (which isn't pretty either?)

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


More information about the llvm-commits mailing list