[llvm] [InstCombine] Generalize `(A + 1) + ~B` fold to any constant (PR #188271)

Piotr Fusik via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 08:19:21 PDT 2026


================
@@ -1604,13 +1604,20 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
   if (Value *V = checkForNegativeOperand(I, Builder))
     return replaceInstUsesWith(I, V);
 
-  // (A + 1) + ~B --> A - B
-  // ~B + (A + 1) --> A - B
-  // (~B + A) + 1 --> A - B
-  // (A + ~B) + 1 --> A - B
-  if (match(&I, m_c_BinOp(m_Add(m_Value(A), m_One()), m_Not(m_Value(B)))) ||
-      match(&I, m_BinOp(m_c_Add(m_Not(m_Value(B)), m_Value(A)), m_One())))
-    return BinaryOperator::CreateSub(A, B);
+  {
+    // (A + C) + ~B --> A - B + (C-1)
+    // ~B + (A + C) --> A - B + (C-1)
+    // (~B + A) + C --> A - B + (C-1)
+    // (A + ~B) + C --> A - B + (C-1)
+    const APInt *C;
+    if (match(&I, m_c_BinOp(m_Add(m_Value(A), m_APIntAllowPoison(C)),
----------------
pfusik wrote:

We are in the `visitAdd` function, therefore `I` is an add.

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


More information about the llvm-commits mailing list