[llvm] [InstCombine] Fold (x + y) & (2^C) -> x & 2^C when y % 2^(C+1) == 0 (PR #166935)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 8 02:25:23 PST 2025


================
@@ -0,0 +1,60 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @addition_and_bitwise1(ptr %0) {
----------------
dtcxzyw wrote:

Don't include unrelated instructions.
You can use `y = arg & (2^(C+1) - 1)` to make sure the low bits of y are zeros.
Here is an example:
```
define i32 @test(i32 %x, i32 %y) {
  %y_masked = and i32 %y, -4
  %add = add i32 %x, %y_masked
  %and = and i32 %add, 2
  ret i32 %and
}
```

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


More information about the llvm-commits mailing list