[llvm] Fixies missed optimization (x + y) & (2^C) -> x & 2^C when y % 2^(C+1) == 0 (PR #157072)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 7 07:31:41 PDT 2025


================
@@ -2476,6 +2476,20 @@ 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
+  if (match(Op0, m_Add(m_Value(X), m_Value(Y)))) {
+    const APInt *PowerC;
+    if (match(Op1, m_Power2(PowerC)) && !PowerC->isOne()) {
+      KnownBits YKnown = computeKnownBits(Y, &I);
+      unsigned ShiftAmount = PowerC->logBase2() + 1;
+
+      APInt YMod = YKnown.Zero;
+      if (YMod.getLoBits(ShiftAmount).isZero()) {
----------------
dtcxzyw wrote:

Use `YMod.countMinTrailingZeros() > PowerC->logBase2()`.

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


More information about the llvm-commits mailing list