[llvm] r370945 - [InstCombine] sub(xor(x, y), or(x, y)) -> neg(and(x, y))

David Bolvansky via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 11:03:21 PDT 2019


Author: xbolva00
Date: Wed Sep  4 11:03:21 2019
New Revision: 370945

URL: http://llvm.org/viewvc/llvm-project?rev=370945&view=rev
Log:
[InstCombine] sub(xor(x, y), or(x, y)) -> neg(and(x, y))

Summary:
```
Name: sub(xor(x, y), or(x, y)) -> neg(and(x, y))
%or = or i32 %y, %x
%xor = xor i32 %x, %y
%sub = sub i32 %xor, %or
  =>
%sub1 = and i32 %x, %y
%sub = sub i32 0, %sub1

Optimization: sub(xor(x, y), or(x, y)) -> neg(and(x, y))
Done: 1
Optimization is correct!
```

https://rise4fun.com/Alive/8OI

Reviewers: lebedev.ri

Reviewed By: lebedev.ri

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D67188

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/trunk/test/Transforms/InstCombine/sub-xor-or-neg-and.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=370945&r1=370944&r2=370945&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Wed Sep  4 11:03:21 2019
@@ -1742,6 +1742,15 @@ Instruction *InstCombiner::visitSub(Bina
       return BinaryOperator::CreateAnd(A, B);
   }
 
+  // (sub (xor A, B) (or A, B)) --> neg (and A, B)
+  {
+    Value *A, *B;
+    if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
+        match(Op1, m_c_Or(m_Specific(A), m_Specific(B))) &&
+        (Op0->hasOneUse() || Op1->hasOneUse()))
+      return BinaryOperator::CreateNeg(Builder.CreateAnd(A, B));
+  }
+
   {
     Value *Y;
     // ((X | Y) - X) --> (~X & Y)

Modified: llvm/trunk/test/Transforms/InstCombine/sub-xor-or-neg-and.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sub-xor-or-neg-and.ll?rev=370945&r1=370944&r2=370945&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sub-xor-or-neg-and.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/sub-xor-or-neg-and.ll Wed Sep  4 11:03:21 2019
@@ -5,9 +5,8 @@ declare void @use(i32)
 
 define i32 @sub_to_and(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_and(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -18,9 +17,8 @@ define i32 @sub_to_and(i32 %x, i32 %y) {
 
 define i32 @sub_to_and_extra_use_sub(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_and_extra_use_sub(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    call void @use(i32 [[SUB]])
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
@@ -33,10 +31,10 @@ define i32 @sub_to_and_extra_use_sub(i32
 
 define i32 @sub_to_and_extra_use_and(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_and_extra_use_and(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[XOR]])
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -50,8 +48,8 @@ define i32 @sub_to_and_extra_use_or(i32
 ; CHECK-LABEL: @sub_to_and_extra_use_or(
 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i32 [[OR]])
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -63,9 +61,8 @@ define i32 @sub_to_and_extra_use_or(i32
 
 define i32 @sub_to_and_or_commuted(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_and_or_commuted(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %y, %x
@@ -76,9 +73,8 @@ define i32 @sub_to_and_or_commuted(i32 %
 
 define i32 @sub_to_and_and_commuted(i32 %x, i32 %y) {
 ; CHECK-LABEL: @sub_to_and_and_commuted(
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[Y]], [[X]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %or = or i32 %x, %y
@@ -89,9 +85,8 @@ define i32 @sub_to_and_and_commuted(i32
 
 define <2 x i32> @sub_to_and_vec(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @sub_to_and_vec(
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i32> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor <2 x i32> [[Y]], [[X]]
-; CHECK-NEXT:    [[SUB:%.*]] = sub <2 x i32> [[XOR]], [[OR]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub <2 x i32> zeroinitializer, [[TMP1]]
 ; CHECK-NEXT:    ret <2 x i32> [[SUB]]
 ;
   %or = or <2 x i32> %x, %y




More information about the llvm-commits mailing list