[llvm] [InstCombine] Fold umax(nuw_mul(x, C0), x + 1) into (x == 0 ? 1 : nuw_mul(x, C0)) (PR #123468)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 18 11:07:48 PST 2025


================
@@ -1847,6 +1847,37 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         return CastInst::Create(Instruction::ZExt, NarrowMaxMin, II->getType());
       }
     }
+    // If C0 is not 0:
+    //   umax(nuw_shl(x, C0), x + 1) -> x == 0 ? 1 : nuw_shl(x, C0)
+    // If C0 is not 0 or 1:
+    //   umax(nuw_mul(x, C0), x + 1) -> x == 0 ? 1 : nuw_mul(x, C0)
+    ConstantInt *C0;
+    bool isShl = false;
+    BinaryOperator *Op = nullptr;
+    auto matchShiftOrMul = [&](Value *I) {
+      if (match(I, m_OneUse(m_NUWShl(m_Value(X), m_ConstantInt(C0))))) {
+        isShl = true;
+        return true;
+      } else if (match(I, m_OneUse(m_NUWMul(m_Value(X), m_ConstantInt(C0)))) &&
+                 C0 && !C0->isOne()) {
+        isShl = false;
+        return true;
+      }
+      return false;
+    };
+    if (((matchShiftOrMul(I0) &&
+          match(I1, m_OneUse(m_Add(m_Specific(X), m_One())))) ||
+         (matchShiftOrMul(I1) &&
+          match(I0, m_OneUse(m_Add(m_Specific(X), m_One()))))) &&
+        C0 && !C0->isZero()) {
----------------
Ruhung wrote:

Removed the check.

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


More information about the llvm-commits mailing list