[llvm] [InstCombine] Fold commuted add of udiv/urem by two (#206272) (PR #207249)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 2 11:56:52 PDT 2026


https://github.com/igor-ishevlyakov updated https://github.com/llvm/llvm-project/pull/207249

>From c8d2c69eeda3fdfe586c06e996aed3c771b2b6e6 Mon Sep 17 00:00:00 2001
From: Igor Shevlyakov <igor at tachyum.com>
Date: Thu, 2 Jul 2026 11:40:06 -0700
Subject: [PATCH] [InstCombine] Fold commuted add of udiv/urem by two (#206272)

SimplifyAddWithRemainder recognizes `and X, lowmask` as a remainder and
`lshr X, N` as a division, folding e.g. `(x >> 1) + (x & 1)` into
`x - (x >> 1)`. When the operands are commuted, the code relies on a swap
that only triggered for a real `urem`/`srem` (m_IRem), so the remainder
spelled as `and X, C` on the LHS was never moved into the Rem slot and the
fold was missed for `add (and X, C), (lshr X, N)`.

Extend the swap condition to also match the `and X, lowmask` remainder form.
MatchDiv and MatchRem accept disjoint opcodes, so this cannot misfire on a
genuine division.

Proved with Alive2 and Z3 (all widths); brute-forced exhaustively for i8/i16.

Fixes #206272

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply at anthropic.com>
---
 .../InstCombine/InstCombineAddSub.cpp         |  8 +++++-
 llvm/test/Transforms/InstCombine/add4.ll      | 26 +++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 8c0dcc8029a1e..e897277671811 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1199,7 +1199,13 @@ Value *InstCombinerImpl::SimplifyAddWithRemainder(BinaryOperator &I) {
     Div = LHS, C1 = APInt(I.getType()->getScalarSizeInBits(), 1);
   if (!RHS->hasOneUse() || !MatchMul(RHS, Rem, C2))
     Rem = RHS, C2 = APInt(I.getType()->getScalarSizeInBits(), 1);
-  if (match(Div, m_IRem(m_Value(), m_Value()))) {
+  // The remainder may be spelled `and X, lowmask` (rem by a power of two), not
+  // just urem/srem, so m_IRem alone misses the commuted form
+  // `add (and X, C), (lshr X, N)`. Detect any remainder form here so it lands
+  // in Rem. MatchDiv and MatchRem accept disjoint opcodes, so this cannot
+  // misfire on a genuine division.
+  if (match(Div, m_IRem(m_Value(), m_Value())) ||
+      match(Div, m_And(m_Value(), m_LowBitMask()))) {
     std::swap(Div, Rem);
     std::swap(C1, C2);
   }
diff --git a/llvm/test/Transforms/InstCombine/add4.ll b/llvm/test/Transforms/InstCombine/add4.ll
index 15feb8a093e99..8fdb11e0b7547 100644
--- a/llvm/test/Transforms/InstCombine/add4.ll
+++ b/llvm/test/Transforms/InstCombine/add4.ll
@@ -412,3 +412,29 @@ define i32 @fold_add_sdiv_srem_by_two_no_mul(i32 noundef %arg) {
   %add = add i32 %div, %rem
   ret i32 %add
 }
+
+; The remainder may be `and X, 1` and appear on the LHS of the add. This
+; commuted form should fold identically to fold_add_udiv_urem_by_two_no_mul.
+define i32 @fold_add_udiv_urem_by_two_no_mul_commuted(i32 noundef %arg) {
+; CHECK-LABEL: @fold_add_udiv_urem_by_two_no_mul_commuted(
+; CHECK-NEXT:    [[LSHR:%.*]] = lshr i32 [[ARG:%.*]], 1
+; CHECK-NEXT:    [[ADD:%.*]] = sub i32 [[ARG]], [[LSHR]]
+; CHECK-NEXT:    ret i32 [[ADD]]
+;
+  %lshr = lshr i32 %arg, 1
+  %and = and i32 %arg, 1
+  %add = add i32 %and, %lshr
+  ret i32 %add
+}
+
+define <2 x i32> @fold_add_udiv_urem_by_two_no_mul_commuted_vec(<2 x i32> noundef %arg) {
+; CHECK-LABEL: @fold_add_udiv_urem_by_two_no_mul_commuted_vec(
+; CHECK-NEXT:    [[LSHR:%.*]] = lshr <2 x i32> [[ARG:%.*]], splat (i32 1)
+; CHECK-NEXT:    [[ADD:%.*]] = sub <2 x i32> [[ARG]], [[LSHR]]
+; CHECK-NEXT:    ret <2 x i32> [[ADD]]
+;
+  %lshr = lshr <2 x i32> %arg, splat (i32 1)
+  %and = and <2 x i32> %arg, splat (i32 1)
+  %add = add <2 x i32> %and, %lshr
+  ret <2 x i32> %add
+}



More information about the llvm-commits mailing list