[llvm-branch-commits] [llvm] 742ea77 - [InstCombine] Transform (A + B) - (A | B) to A & B (PR48604)

Dávid Bolvanský via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 31 05:08:20 PST 2020


Author: Dávid Bolvanský
Date: 2020-12-31T14:03:20+01:00
New Revision: 742ea77ca4c0ea10d8ccd160c7d7f4257d214ed0

URL: https://github.com/llvm/llvm-project/commit/742ea77ca4c0ea10d8ccd160c7d7f4257d214ed0
DIFF: https://github.com/llvm/llvm-project/commit/742ea77ca4c0ea10d8ccd160c7d7f4257d214ed0.diff

LOG: [InstCombine] Transform (A + B) - (A | B) to A & B (PR48604)

define i32 @src(i32 %x, i32 %y) {
%0:
  %a = add i32 %x, %y
  %o = or i32 %x, %y
  %r = sub i32 %a, %o
  ret i32 %r
}
=>
define i32 @tgt(i32 %x, i32 %y) {
%0:
  %b = and i32 %x, %y
  ret i32 %b
}
Transformation seems to be correct!

https://alive2.llvm.org/ce/z/aQRh2j

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/and.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index c20861f20f07..be145615a241 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1873,6 +1873,14 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
       return BinaryOperator::CreateXor(A, B);
   }
 
+  // (sub (add A, B) (or A, B)) --> (and A, B)
+  {
+    Value *A, *B;
+    if (match(Op0, m_Add(m_Value(A), m_Value(B))) &&
+        match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
+      return BinaryOperator::CreateAnd(A, B);
+  }
+
   // (sub (and A, B) (or A, B)) --> neg (xor A, B)
   {
     Value *A, *B;

diff  --git a/llvm/test/Transforms/InstCombine/and.ll b/llvm/test/Transforms/InstCombine/and.ll
index 4f054c05889a..a3d5932ff140 100644
--- a/llvm/test/Transforms/InstCombine/and.ll
+++ b/llvm/test/Transforms/InstCombine/and.ll
@@ -1221,9 +1221,7 @@ define <2 x i8> @flip_masked_bit_nonuniform(<2 x i8> %A) {
 
 define i32 @and_test(i32 %x, i32 %y) {
 ; CHECK-LABEL: @and_test(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[O:%.*]] = or i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[O]]
+; CHECK-NEXT:    [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %x, %y
@@ -1234,9 +1232,7 @@ define i32 @and_test(i32 %x, i32 %y) {
 
 define i32 @and_test2(i32 %x, i32 %y) {
 ; CHECK-LABEL: @and_test2(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[O:%.*]] = or i32 [[Y]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[O]]
+; CHECK-NEXT:    [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %x, %y
@@ -1247,9 +1243,7 @@ define i32 @and_test2(i32 %x, i32 %y) {
 
 define i32 @and_test3(i32 %x, i32 %y) {
 ; CHECK-LABEL: @and_test3(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[O:%.*]] = or i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[O]]
+; CHECK-NEXT:    [[R:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %y, %x
@@ -1261,9 +1255,7 @@ define i32 @and_test3(i32 %x, i32 %y) {
 
 define <2 x i8> @and_vec(<2 x i8> %X, <2 x i8> %Y) {
 ; CHECK-LABEL: @and_vec(
-; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[O:%.*]] = or <2 x i8> [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub <2 x i8> [[A]], [[O]]
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret <2 x i8> [[R]]
 ;
   %a = add <2 x i8> %X, %Y


        


More information about the llvm-branch-commits mailing list