[llvm] [InstCombine] fold `(a == 1 && b != 0) || (a != 0 && b == 0))` to `(a ==0) != (b == 0)` (PR #94915)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 14:42:15 PDT 2024


================
@@ -3421,6 +3421,29 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
   return foldAndOrOfICmpsUsingRanges(LHS, RHS, IsAnd);
 }
 
+Value *foldAorBZero(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
+  Value *Op0 = I.getOperand(0);
+  Value *Op1 = I.getOperand(1);
+  if (!Op0->hasOneUse() || !Op1->hasOneUse())
+    return nullptr;
+
+  // match each operand of I with and
+  Value *A, *B;
+  CmpInst::Predicate Pred = CmpInst::ICMP_EQ;
+  CmpInst::Predicate InPred = CmpInst::ICMP_EQ;
+  bool IsOp0 = match(Op0, m_c_And(m_Cmp(Pred, m_Value(A), m_ZeroInt()),
+                                  m_Cmp(InPred, m_Value(B), m_ZeroInt())));
+  bool IsOp1 = match(Op1, m_c_And(m_Cmp(InPred, m_Specific(A), m_ZeroInt()),
+                                  m_Cmp(Pred, m_Specific(B), m_ZeroInt())));
+  if (!IsOp0 || !IsOp1)
+    return nullptr;
+
+  Constant *Zero = ConstantInt::getNullValue(A->getType());
+  auto *LHS = Builder.CreateICmpEQ(A, Zero);
+  auto *RHS = Builder.CreateICmpEQ(B, Zero);
+  return Builder.CreateICmpNE(LHS, RHS);
----------------
goldsteinn wrote:

Oh I see, you swap preds.

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


More information about the llvm-commits mailing list