[llvm] [InstCombine] Fold `(add X, (sext/zext (icmp eq X, C)))` (PR #93840)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu May 30 10:33:10 PDT 2024


================
@@ -1694,6 +1694,26 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
     return BinaryOperator::CreateOr(LHS, Zext);
   }
 
+  {
+    Value *Cond, *Ext;
+    Constant *C;
+    // (add X, (sext/zext (icmp eq X, C)))
+    //    -> (select (icmp eq X, C), (add C, (sext/zext 1)), X)
+    auto CondMatcher = m_CombineAnd(
+        m_Value(Cond), m_ICmp(Pred, m_Deferred(A), m_ImmConstant(C)));
+
+    if (match(&I,
+              m_c_Add(m_Value(A),
+                      m_CombineAnd(m_Value(Ext), m_ZExtOrSExt(CondMatcher)))) &&
+        Pred == ICmpInst::ICMP_EQ && Ext->hasOneUse()) {
+      Constant *ExtC = match(Ext, m_ZExt(m_Value()))
+                           ? ConstantInt::get(C->getType(), 1)
+                           : ConstantInt::getAllOnesValue(C->getType());
+      return replaceInstUsesWith(
+          I, Builder.CreateSelect(Cond, Builder.CreateAdd(C, ExtC), A));
----------------
dtcxzyw wrote:

Use `InstCombiner::AddOne/SubOne`?


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


More information about the llvm-commits mailing list