[llvm] [InstCombine] Fold X + umax(X, 1) -> umax(X << 1, 1) (PR #222727)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 09:10:14 PDT 2026


================
@@ -1984,6 +1984,21 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
     I.setHasNoUnsignedWrap(true);
   }
 
+  // X + umax(X, 1) --> umax(X << 1, 1) if X + X does not overflow.
+  if (match(&I,
+            m_c_Add(m_Value(A), m_OneUse(m_UMax(m_Deferred(A), m_One())))) &&
+      (I.hasNoUnsignedWrap() || I.hasNoSignedWrap())) {
+    return replaceInstUsesWith(
+        I,
+        Builder.CreateIntrinsic(
+            Intrinsic::umax, {Ty},
+            {
+                Builder.CreateShl(A, ConstantInt::get(Ty, 1), "mul2",
+                                  I.hasNoUnsignedWrap(), I.hasNoSignedWrap()),
+                ConstantInt::get(Ty, 1),
+            }));
----------------
nikic wrote:

nit: Use CreateBinaryIntrinsic instead, which will simplify this a bit.

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


More information about the llvm-commits mailing list