[llvm] [InstCombine] Fold (A & B) - (A & ~B) => B - (A ^ B) (PR #79717)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 17:27:13 PST 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/79717

>From 551defe8ca9db4b630cebc79ea450f0844131800 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 27 Jan 2024 16:40:49 -0500
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests (NFC)

---
 llvm/test/Transforms/InstCombine/sub.ll | 30 +++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 494cdf62c7975ee..720d86a3159b270 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2626,3 +2626,33 @@ define i8 @sub_of_adds_2xc(i8 %x, i8 %y) {
   %r = sub i8 %xc, %yc
   ret i8 %r
 }
+
+define i32 @sub_and_xor(i32 %A, i32 %B) {
+; CHECK-LABEL: @sub_and_xor(
+; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], -1
+; CHECK-NEXT:    [[AND1:%.*]] = and i32 [[XOR]], [[A:%.*]]
+; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[B]], [[A]]
+; CHECK-NEXT:    [[RES:%.*]] = sub nsw i32 [[AND1]], [[AND2]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %xor = xor i32 %B, -1
+  %and1 = and i32 %xor, %A
+  %and2 = and i32 %B, %A
+  %res = sub nsw i32 %and1, %and2
+  ret i32 %res
+}
+
+define i32 @sub_xor_and(i32 %A, i32 %B) {
+; CHECK-LABEL: @sub_xor_and(
+; CHECK-NEXT:    [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
+; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B]], -1
+; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[XOR]], [[A]]
+; CHECK-NEXT:    [[RES:%.*]] = sub nsw i32 [[AND1]], [[AND2]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %and1 = and i32 %B, %A
+  %xor = xor i32 %B, -1
+  %and2 = and i32 %xor, %A
+  %res = sub nsw i32 %and1, %and2
+  ret i32 %res
+}

>From ff6f516750192739e5ef7f59dbaab1a53c9a2889 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 27 Jan 2024 19:44:23 -0500
Subject: [PATCH 2/2] [InstCombine] Fold (A & B) - (A & ~B) => B - (A ^ B)

(A & ~B) - (A & B) => (A ^ B) - B, and its counterpart when operands are swapped.
---
 .../InstCombine/InstCombineAddSub.cpp          | 18 ++++++++++++++++++
 llvm/test/Transforms/InstCombine/sub.ll        | 12 ++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 8a00b75a1f74042..aceb178263811ce 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2371,6 +2371,24 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
       return BinaryOperator::CreateNeg(Y);
   }
 
+  {
+    Value *A, *B;
+    // (A & ~B) - (A & B) => (A ^ B) - B
+    if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
+        match(Op1, m_c_And(m_Specific(A), m_Specific(B)))) {
+      return BinaryOperator::CreateSub(Builder.CreateXor(A, B), B);
+    }
+  }
+
+  {
+    Value *A, *B;
+    // (A & B) - (A & ~B) => B - (A ^ B)
+    if (match(Op1, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
+        match(Op0, m_c_And(m_Specific(A), m_Specific(B)))) {
+      return BinaryOperator::CreateSub(Builder.CreateXor(A, B), B);
+    }
+  }
+
   // (sub (or A, B) (and A, B)) --> (xor A, B)
   {
     Value *A, *B;
diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 720d86a3159b270..65142d4e2006332 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2629,10 +2629,8 @@ define i8 @sub_of_adds_2xc(i8 %x, i8 %y) {
 
 define i32 @sub_and_xor(i32 %A, i32 %B) {
 ; CHECK-LABEL: @sub_and_xor(
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], -1
-; CHECK-NEXT:    [[AND1:%.*]] = and i32 [[XOR]], [[A:%.*]]
-; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[B]], [[A]]
-; CHECK-NEXT:    [[RES:%.*]] = sub nsw i32 [[AND1]], [[AND2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[RES:%.*]] = sub i32 [[TMP1]], [[B]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %xor = xor i32 %B, -1
@@ -2644,10 +2642,8 @@ define i32 @sub_and_xor(i32 %A, i32 %B) {
 
 define i32 @sub_xor_and(i32 %A, i32 %B) {
 ; CHECK-LABEL: @sub_xor_and(
-; CHECK-NEXT:    [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B]], -1
-; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[XOR]], [[A]]
-; CHECK-NEXT:    [[RES:%.*]] = sub nsw i32 [[AND1]], [[AND2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[RES:%.*]] = sub i32 [[TMP1]], [[B]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %and1 = and i32 %B, %A



More information about the llvm-commits mailing list