[InstCombine] Fold sub (and A, B) (or A, B)) to neg (xor A, B)
Summary:
```
Name: sub(and(x, y), or(x, y)) -> neg(xor(x, y))
%or = or i32 %y, %x
%and = and i32 %x, %y
%sub = sub i32 %and, %or
=>
%sub1 = xor i32 %x, %y
%sub = sub i32 0, %sub1
Optimization: sub(and(x, y), or(x, y)) -> neg(xor(x, y))
Done: 1
Optimization is correct!
```
https://rise4fun.com/Alive/VI6
Found by @lebedev.ri. Also author of the proof.
Reviewers: lebedev.ri, spatel
Reviewed By: lebedev.ri
Subscribers: llvm-commits, lebedev.ri
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67155
llvm-svn: 370934
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 776bef3..fbd0165 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1725,6 +1725,15 @@
return BinaryOperator::CreateXor(A, B);
}
+ // (sub (and A, B) (or A, B)) --> neg (xor A, B)
+ {
+ Value *A, *B;
+ if (match(Op0, m_And(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.CreateXor(A, B));
+ }
+
// (sub (or A, B), (xor A, B)) --> (and A, B)
{
Value *A, *B;
diff --git a/llvm/test/Transforms/InstCombine/sub-and-or-not-xor.ll b/llvm/test/Transforms/InstCombine/sub-and-or-not-xor.ll
index dc56ba7..980433e 100644
--- a/llvm/test/Transforms/InstCombine/sub-and-or-not-xor.ll
+++ b/llvm/test/Transforms/InstCombine/sub-and-or-not-xor.ll
@@ -5,9 +5,8 @@
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 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor 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_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 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor 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_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 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[X]], [[Y]]
+; CHECK-NEXT: [[SUB:%.*]] = sub i32 0, [[TMP1]]
; CHECK-NEXT: ret i32 [[SUB]]
;
%or = or i32 %x, %y
@@ -50,8 +48,8 @@
; 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 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor 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_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 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor 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_xor_and_commuted(i32 %x, i32 %y) {
; CHECK-LABEL: @sub_to_xor_and_commuted(
-; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[AND:%.*]] = and i32 [[Y]], [[X]]
-; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor 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 <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> [[AND]], [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor <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