[PATCH] D67153: [InstCombine]Fold (sub (or A, B) (and A, B) to (xor A, B)

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 04:18:52 PDT 2019


xbolva00 created this revision.
xbolva00 added reviewers: spatel, lebedev.ri.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Name: sub or and to xor
%or = or i32 %y, %x
%and = and i32 %x, %y
%sub = sub i32 %or, %and

  =>

%sub = xor i32 %x, %y

Optimization: sub or and to xor
Done: 1
Optimization is correct!

https://rise4fun.com/Alive/eJu


Repository:
  rL LLVM

https://reviews.llvm.org/D67153

Files:
  /home/xbolva00/LLVM/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
  test/Transforms/InstCombine/sub-or-and-xor.ll


Index: test/Transforms/InstCombine/sub-or-and-xor.ll
===================================================================
--- test/Transforms/InstCombine/sub-or-and-xor.ll
+++ test/Transforms/InstCombine/sub-or-and-xor.ll
@@ -5,9 +5,7 @@
 
 define i32 @sub_to_xor(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_xor(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -18,9 +16,7 @@
 
 define i32 @sub_to_xor_extra_use_sub(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_xor_extra_use_sub(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[SUB]])
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
@@ -33,10 +29,9 @@
 
 define i32 @sub_to_xor_extra_use_and(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_xor_extra_use_and(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[AND]])
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X]], [[Y]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -50,8 +45,7 @@
 ; CHECK-LABEL: @sub_to_xor_extra_use_or(
 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[OR]])
-; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X]], [[Y]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -63,9 +57,7 @@
 
 define i32 @sub_to_xor_or_commuted(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_xor_or_commuted(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %y, %x
@@ -76,9 +68,7 @@
 
 define <2 x i32> @sub_to_xor_vec(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @sub_to_xor_vec(
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i32> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[Y]], [[X]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub <2 x i32> [[OR]], [[AND]]
+; CHECK-NEXT:    [[SUB:%.*]] = xor <2 x i32> [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    ret <2 x i32> [[SUB]]
 ;
   %or = or <2 x i32> %x, %y
Index: /home/xbolva00/LLVM/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- /home/xbolva00/LLVM/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ /home/xbolva00/LLVM/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1717,6 +1717,14 @@
       return BinaryOperator::CreateNeg(Y);
   }
 
+  // (sub (or A, B) (and A, B)) --> (xor A, B)
+  {
+    Value *A, *B;
+    if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
+        (match(Op0, m_c_Or(m_Specific(A), m_Specific(B)))))
+      return BinaryOperator::CreateXor(A, B);
+  }
+
   // (sub (or A, B), (xor A, B)) --> (and A, B)
   {
     Value *A, *B;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67153.218642.patch
Type: text/x-patch
Size: 3463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190904/80282a9d/attachment.bin>


More information about the llvm-commits mailing list