[llvm] [InstCombine] Fold `(x == A) || (x & -Pow2) == A + 1` into range check (PR #153842)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 16 05:04:13 PDT 2025


================
@@ -1338,16 +1338,46 @@ Value *InstCombinerImpl::foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1,
       V2 = X;
   }
 
+  // Look through and with a negative power of 2 mask on V1 or V2. This
+  // detects idioms of the form `(x == A) || ((x & Mask) == A + 1)` where A + 1
+  // is aligned to the mask and A + 1 >= |Mask|. This pattern corresponds to a
+  // contiguous range check, which can be folded into an addition and compare.
+  const APInt *Mask1 = nullptr, *Mask2 = nullptr;
+  bool matchedAnd1 = false, matchedAnd2 = false;
----------------
dtcxzyw wrote:

```suggestion
  bool MatchedAnd1 = false, MatchedAnd2 = false;
```

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


More information about the llvm-commits mailing list