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

Zain Jaffal via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 01:49:15 PDT 2024


================
@@ -3421,6 +3421,42 @@ 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;
+
+  Value *Cmp1, *Cmp2, *Cmp3, *Cmp4;
+  bool IsOp0 = match(Op0, m_And(m_Value(Cmp1), m_Value(Cmp2)));
+  bool IsOp1 = match(Op1, m_And(m_Value(Cmp3), m_Value(Cmp4)));
+  if (!IsOp0 || !IsOp1)
+    return nullptr;
+
+  Value *A;
+  Value *B;
+  Value *CmpInvariant;
+  // check if any two pairs of the and operations are invertions of each other.
+  bool CheckInvertion =
+      (isKnownInversion(Cmp1, Cmp3) && isKnownInversion(Cmp2, Cmp4)) ||
+      (isKnownInversion(Cmp1, Cmp4) && isKnownInversion(Cmp2, Cmp3));
+  if (!CheckInvertion)
+    return nullptr;
+
+  // given both compares are invertions we need to check if the second operand
+  // is the same for both invertion pairs
----------------
zjaffal wrote:

yeah i get it 

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


More information about the llvm-commits mailing list