[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:22 PST 2025


================
@@ -2470,6 +2470,19 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
     return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Y);
   }
 
+  // (x + y) & (2^C) -> x & 2^C when y % 2^(C+1) == 0
----------------
dtcxzyw wrote:

It reminds me that we already support this pattern in https://github.com/llvm/llvm-project/blob/8d950d27d686543e877c5d64df8cec287381556c/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L608-L613

Can you dig out the reason why SimplifyDemandedBits fails to eliminate the add? Does it reach the max depth limit? Or some context information gets lost so we cannot use `load ... align 4` to infer the alignment.


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


More information about the llvm-commits mailing list