[llvm-branch-commits] [llvm] ae69fa9 - [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 06:09:01 PST 2020


Author: Dávid Bolvanský
Date: 2020-12-31T15:04:32+01:00
New Revision: ae69fa9b9f65f59cc0ca8c47f23748a53c8dbdc5

URL: https://github.com/llvm/llvm-project/commit/ae69fa9b9f65f59cc0ca8c47f23748a53c8dbdc5
DIFF: https://github.com/llvm/llvm-project/commit/ae69fa9b9f65f59cc0ca8c47f23748a53c8dbdc5.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 = and i32 %x, %y
  %r = sub i32 %a, %o
  ret i32 %r
}
=>
define i32 @tgt(i32 %x, i32 %y) {
%0:
  %b = or i32 %x, %y
  ret i32 %b
}
Transformation seems to be correct!

https://alive2.llvm.org/ce/z/2fhW6r

Added: 
    

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

Removed: 
    


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

diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index aaac3f23f71f..649d2e8c2848 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -1621,9 +1621,7 @@ define <2 x i8> @and_vec(<2 x i8> %X, <2 x i8> %Y) {
 
 define i32 @or_test(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_test(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[B]]
+; CHECK-NEXT:    [[R:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %x, %y
@@ -1634,9 +1632,7 @@ define i32 @or_test(i32 %x, i32 %y) {
 
 define i32 @or_test2(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_test2(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[Y]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[B]]
+; CHECK-NEXT:    [[R:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %x, %y
@@ -1647,9 +1643,7 @@ define i32 @or_test2(i32 %x, i32 %y) {
 
 define i32 @or_test3(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_test3(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub i32 [[A]], [[B]]
+; CHECK-NEXT:    [[R:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %y, %x
@@ -1660,9 +1654,7 @@ define i32 @or_test3(i32 %x, i32 %y) {
 
 define <2 x i8> @or_vec(<2 x i8> %X, <2 x i8> %Y) {
 ; CHECK-LABEL: @or_vec(
-; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[B:%.*]] = and <2 x i8> [[X]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = sub <2 x i8> [[A]], [[B]]
+; CHECK-NEXT:    [[R:%.*]] = or <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