[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 02:02:05 PDT 2024


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

>From 3ec3b326289315ef158256904837a24828759faf Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Sun, 9 Jun 2024 21:53:46 +0100
Subject: [PATCH 1/4] [InstCombine] fold `(a == 1 && b != 0) || (a != 0 && b ==
 0))` to `(a == 0) != (b == 0)`

---
 .../InstCombine/InstCombineAndOrXor.cpp       |  27 +++++
 .../InstCombine/fold-a-or-b-zero.ll           | 104 ++++++++++++++++++
 2 files changed, 131 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 8695e9e69df20..e873a86f3332f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -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);
+}
+
 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
 // here. We should standardize that construct where it is needed or choose some
 // other way to ensure that commutated variants of patterns are not missed.
@@ -3450,6 +3473,10 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
   if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
     return X;
 
+  // (A == 0 & B != 0) | (A != 0 & B == 0)) -> (A == 0) != (B == 0)
+  if (Value *V = foldAorBZero(I, Builder))
+    return replaceInstUsesWith(I, V);
+
   // (A&B)|(A&C) -> A&(B|C) etc
   if (Value *V = foldUsingDistributiveLaws(I))
     return replaceInstUsesWith(I, V);
diff --git a/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
new file mode 100644
index 0000000000000..5a68927e879a7
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
@@ -0,0 +1,104 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -S -passes=instcombine | FileCheck %s
+
+declare void @use(i1)
+
+define void @a_or_b(i32 %a, i32 %b)  {
+; CHECK-LABEL: define void @src(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
+; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
+; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    ret void
+;
+  %a_eq_zero = icmp eq i32 %a, 0
+  %b_ne_zero = icmp ne i32 %b, 0
+  %and.1 = and i1 %a_eq_zero, %b_ne_zero
+  %a_ne_zero = icmp ne i32 %a, 0
+  %b_eq_zero = icmp eq i32 %b, 0
+  %and.2 = and i1 %a_ne_zero, %b_eq_zero
+  %or = or i1 %and.1, %and.2
+  call void @use(i1 %or)
+  ret void
+}
+
+
+define void @a_or_b_zero(i32 %a, i32 %b)  {
+; CHECK-LABEL: define void @src(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
+; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
+; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    ret void
+;
+  %a_eq_zero = icmp eq i32 %a, 0
+  %b_ne_zero = icmp ne i32 %b, 0
+  %and.1 = and i1 %a_eq_zero, %b_ne_zero
+  %a_ne_zero = icmp ne i32 %a, 0
+  %b_eq_zero = icmp eq i32 %b, 0
+  %and.2 = and i1 %a_ne_zero, %b_eq_zero
+  %or = or i1 %and.1, %and.2
+  call void @use(i1 %or)
+  ret void
+}
+
+define void @a_or_b_multiple_uses(i32 %a, i32 %b)  {
+; CHECK-LABEL: define void @src(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
+; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
+; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    ret void
+;
+  %a_eq_zero = icmp eq i32 %a, 0
+  %b_ne_zero = icmp ne i32 %b, 0
+  %and.1 = and i1 %a_eq_zero, %b_ne_zero
+  %a_ne_zero = icmp ne i32 %a, 0
+  %b_eq_zero = icmp eq i32 %b, 0
+  %and.2 = and i1 %a_ne_zero, %b_eq_zero
+  call void @use(i1 %and.2)
+  %or = or i1 %and.1, %and.2
+  ret void
+}
+
+define void @a_or_b_multiple_uses_2(i32 %a, i32 %b)  {
+; CHECK-LABEL: define void @src(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
+; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
+; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    ret void
+;
+  %a_eq_zero = icmp eq i32 %a, 0
+  %b_ne_zero = icmp ne i32 %b, 0
+  call void @use(i1 %b_ne_zero)
+  %and.1 = and i1 %a_eq_zero, %b_ne_zero
+  %a_ne_zero = icmp ne i32 %a, 0
+  %b_eq_zero = icmp eq i32 %b, 0
+  %and.2 = and i1 %a_ne_zero, %b_eq_zero
+  call void @use(i1 %and.1)
+  %or = or i1 %and.1, %and.2
+  ret void
+}
+
+

>From c2b0197da058224bda067b1d43e28f1554615a35 Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Wed, 19 Jun 2024 08:46:31 +0100
Subject: [PATCH 2/4] generalise compare

---
 .../InstCombine/InstCombineAndOrXor.cpp       | 35 +++++++++++++------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index e873a86f3332f..4d8fc87ce0b84 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3427,20 +3427,33 @@ Value *foldAorBZero(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
   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())));
+  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;
 
-  Constant *Zero = ConstantInt::getNullValue(A->getType());
-  auto *LHS = Builder.CreateICmpEQ(A, Zero);
-  auto *RHS = Builder.CreateICmpEQ(B, Zero);
+  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
+  auto *AInst = cast<Instruction>(Cmp1);
+  auto *BInst = cast<Instruction>(Cmp2);
+  if (AInst->getOperand(1) != BInst->getOperand(1))
+    return nullptr;
+  A = AInst->getOperand(0);
+  B = BInst->getOperand(0);
+  CmpInvariant = AInst->getOperand(1);
+  auto *LHS = Builder.CreateICmpEQ(A, CmpInvariant);
+  auto *RHS = Builder.CreateICmpEQ(B, CmpInvariant);
   return Builder.CreateICmpNE(LHS, RHS);
 }
 

>From 371770c76e2e1287e070d88d7ddcd62b62e79176 Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Wed, 19 Jun 2024 08:46:38 +0100
Subject: [PATCH 3/4] update test cases

---
 .../InstCombine/fold-a-or-b-zero.ll           | 76 ++++++++++---------
 1 file changed, 40 insertions(+), 36 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
index 5a68927e879a7..dd044d09fc79b 100644
--- a/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
+++ b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
@@ -4,15 +4,11 @@
 declare void @use(i1)
 
 define void @a_or_b(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @src(
+; CHECK-LABEL: define void @a_or_b(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
-; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
-; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
-; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
-; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
-; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
-; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    call void @use(i1 [[OR]])
 ; CHECK-NEXT:    ret void
 ;
@@ -27,42 +23,53 @@ define void @a_or_b(i32 %a, i32 %b)  {
   ret void
 }
 
+define void @a_or_b_const(i32 %a, i32 %b, i32 %c)  {
+; CHECK-LABEL: define void @a_or_b_const(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A]], [[C]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[B]], [[C]]
+; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    ret void
+;
+  %a_eq_c = icmp eq i32 %a, %c
+  %b_ne_c = icmp ne i32 %b, %c
+  %and.1 = and i1 %a_eq_c, %b_ne_c
+  %a_ne_c = icmp ne i32 %a, %c
+  %b_eq_c = icmp eq i32 %b, %c
+  %and.2 = and i1 %a_ne_c, %b_eq_c
+  %or = or i1 %and.1, %and.2
+  call void @use(i1 %or)
+  ret void
+}
 
-define void @a_or_b_zero(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @src(
-; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
-; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
-; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
-; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
-; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
-; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
-; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+define void @a_or_b_nullptr(ptr %a, ptr %b) {
+; CHECK-LABEL: define void @a_or_b_nullptr(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq ptr [[A]], null
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq ptr [[B]], null
+; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    call void @use(i1 [[OR]])
 ; CHECK-NEXT:    ret void
 ;
-  %a_eq_zero = icmp eq i32 %a, 0
-  %b_ne_zero = icmp ne i32 %b, 0
-  %and.1 = and i1 %a_eq_zero, %b_ne_zero
-  %a_ne_zero = icmp ne i32 %a, 0
-  %b_eq_zero = icmp eq i32 %b, 0
-  %and.2 = and i1 %a_ne_zero, %b_eq_zero
+  %a_null = icmp eq ptr %a, null
+  %b_null = icmp eq ptr %b, null
+  %a_not_null = icmp ne ptr %a, null
+  %b_not_null = icmp ne ptr %b, null
+  %and.1 = and i1 %a_null, %b_not_null
+  %and.2 = and i1 %a_not_null, %b_null
   %or = or i1 %and.1, %and.2
   call void @use(i1 %or)
   ret void
 }
 
 define void @a_or_b_multiple_uses(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @src(
+; CHECK-LABEL: define void @a_or_b_multiple_uses(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
-; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
-; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
-; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
 ; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
 ; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
 ; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
-; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    call void @use(i1 [[AND_2]])
 ; CHECK-NEXT:    ret void
 ;
   %a_eq_zero = icmp eq i32 %a, 0
@@ -77,16 +84,13 @@ define void @a_or_b_multiple_uses(i32 %a, i32 %b)  {
 }
 
 define void @a_or_b_multiple_uses_2(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @src(
+; CHECK-LABEL: define void @a_or_b_multiple_uses_2(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
 ; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
 ; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    call void @use(i1 [[B_NE_ZERO]])
 ; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
-; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
-; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
-; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
-; CHECK-NEXT:    call void @use(i1 [[OR]])
+; CHECK-NEXT:    call void @use(i1 [[AND_1]])
 ; CHECK-NEXT:    ret void
 ;
   %a_eq_zero = icmp eq i32 %a, 0

>From 1c0318d44078bc20c9a787a8958c0237f14b37ef Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Wed, 19 Jun 2024 10:01:48 +0100
Subject: [PATCH 4/4] simplify the compare

---
 .../InstCombine/InstCombineAndOrXor.cpp       | 29 ++--------
 .../InstCombine/fold-a-or-b-zero.ll           | 54 ++++++++++---------
 2 files changed, 33 insertions(+), 50 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 4d8fc87ce0b84..0deb0aa47a177 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3424,37 +3424,18 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
 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)
+  if (match(Op0, m_OneUse(m_And(m_Value(Cmp1), m_Value(Cmp2)))) ||
+      match(Op1, m_OneUse(m_And(m_Value(Cmp3), m_Value(Cmp4)))))
     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)
+  if ((isKnownInversion(Cmp1, Cmp3) && isKnownInversion(Cmp2, Cmp4)) ||
+      (isKnownInversion(Cmp1, Cmp4) && isKnownInversion(Cmp2, Cmp3)))
     return nullptr;
 
-  // given both compares are invertions we need to check if the second operand
-  // is the same for both invertion pairs
-  auto *AInst = cast<Instruction>(Cmp1);
-  auto *BInst = cast<Instruction>(Cmp2);
-  if (AInst->getOperand(1) != BInst->getOperand(1))
-    return nullptr;
-  A = AInst->getOperand(0);
-  B = BInst->getOperand(0);
-  CmpInvariant = AInst->getOperand(1);
-  auto *LHS = Builder.CreateICmpEQ(A, CmpInvariant);
-  auto *RHS = Builder.CreateICmpEQ(B, CmpInvariant);
-  return Builder.CreateICmpNE(LHS, RHS);
+  return Builder.CreateICmpEQ(Cmp1, Cmp2);
 }
 
 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
diff --git a/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
index dd044d09fc79b..a2c08c5fc7069 100644
--- a/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
+++ b/llvm/test/Transforms/InstCombine/fold-a-or-b-zero.ll
@@ -3,14 +3,13 @@
 
 declare void @use(i1)
 
-define void @a_or_b(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @a_or_b(
+define i1 @a_or_b(i32 %a, i32 %b)  {
+; CHECK-LABEL: define i1 @a_or_b(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A]], 0
 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[B]], 0
 ; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    call void @use(i1 [[OR]])
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %a_eq_zero = icmp eq i32 %a, 0
   %b_ne_zero = icmp ne i32 %b, 0
@@ -19,18 +18,16 @@ define void @a_or_b(i32 %a, i32 %b)  {
   %b_eq_zero = icmp eq i32 %b, 0
   %and.2 = and i1 %a_ne_zero, %b_eq_zero
   %or = or i1 %and.1, %and.2
-  call void @use(i1 %or)
-  ret void
+  ret i1 %or
 }
 
-define void @a_or_b_const(i32 %a, i32 %b, i32 %c)  {
-; CHECK-LABEL: define void @a_or_b_const(
+define i1 @a_or_b_const(i32 %a, i32 %b, i32 %c)  {
+; CHECK-LABEL: define i1 @a_or_b_const(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A]], [[C]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[B]], [[C]]
 ; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    call void @use(i1 [[OR]])
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %a_eq_c = icmp eq i32 %a, %c
   %b_ne_c = icmp ne i32 %b, %c
@@ -39,18 +36,16 @@ define void @a_or_b_const(i32 %a, i32 %b, i32 %c)  {
   %b_eq_c = icmp eq i32 %b, %c
   %and.2 = and i1 %a_ne_c, %b_eq_c
   %or = or i1 %and.1, %and.2
-  call void @use(i1 %or)
-  ret void
+  ret i1 %or
 }
 
-define void @a_or_b_nullptr(ptr %a, ptr %b) {
-; CHECK-LABEL: define void @a_or_b_nullptr(
+define i1 @a_or_b_nullptr(ptr %a, ptr %b) {
+; CHECK-LABEL: define i1 @a_or_b_nullptr(
 ; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq ptr [[A]], null
 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq ptr [[B]], null
 ; CHECK-NEXT:    [[OR:%.*]] = xor i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    call void @use(i1 [[OR]])
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %a_null = icmp eq ptr %a, null
   %b_null = icmp eq ptr %b, null
@@ -59,18 +54,21 @@ define void @a_or_b_nullptr(ptr %a, ptr %b) {
   %and.1 = and i1 %a_null, %b_not_null
   %and.2 = and i1 %a_not_null, %b_null
   %or = or i1 %and.1, %and.2
-  call void @use(i1 %or)
-  ret void
+  ret i1 %or
 }
 
-define void @a_or_b_multiple_uses(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @a_or_b_multiple_uses(
+define i1 @a_or_b_multiple_uses(i32 %a, i32 %b)  {
+; CHECK-LABEL: define i1 @a_or_b_multiple_uses(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
 ; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
 ; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
 ; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
 ; CHECK-NEXT:    call void @use(i1 [[AND_2]])
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %a_eq_zero = icmp eq i32 %a, 0
   %b_ne_zero = icmp ne i32 %b, 0
@@ -80,18 +78,22 @@ define void @a_or_b_multiple_uses(i32 %a, i32 %b)  {
   %and.2 = and i1 %a_ne_zero, %b_eq_zero
   call void @use(i1 %and.2)
   %or = or i1 %and.1, %and.2
-  ret void
+  ret i1 %or
 }
 
-define void @a_or_b_multiple_uses_2(i32 %a, i32 %b)  {
-; CHECK-LABEL: define void @a_or_b_multiple_uses_2(
+define i1 @a_or_b_multiple_uses_2(i32 %a, i32 %b)  {
+; CHECK-LABEL: define i1 @a_or_b_multiple_uses_2(
 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
 ; CHECK-NEXT:    [[A_EQ_ZERO:%.*]] = icmp eq i32 [[A]], 0
 ; CHECK-NEXT:    [[B_NE_ZERO:%.*]] = icmp ne i32 [[B]], 0
 ; CHECK-NEXT:    call void @use(i1 [[B_NE_ZERO]])
 ; CHECK-NEXT:    [[AND_1:%.*]] = and i1 [[A_EQ_ZERO]], [[B_NE_ZERO]]
+; CHECK-NEXT:    [[A_NE_ZERO:%.*]] = icmp ne i32 [[A]], 0
+; CHECK-NEXT:    [[B_EQ_ZERO:%.*]] = icmp eq i32 [[B]], 0
+; CHECK-NEXT:    [[AND_2:%.*]] = and i1 [[A_NE_ZERO]], [[B_EQ_ZERO]]
 ; CHECK-NEXT:    call void @use(i1 [[AND_1]])
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND_1]], [[AND_2]]
+; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %a_eq_zero = icmp eq i32 %a, 0
   %b_ne_zero = icmp ne i32 %b, 0
@@ -102,7 +104,7 @@ define void @a_or_b_multiple_uses_2(i32 %a, i32 %b)  {
   %and.2 = and i1 %a_ne_zero, %b_eq_zero
   call void @use(i1 %and.1)
   %or = or i1 %and.1, %and.2
-  ret void
+  ret i1 %or
 }
 
 



More information about the llvm-commits mailing list