diff options
author | David Bolvansky <david.bolvansky@gmail.com> | 2019-09-04 12:00:33 +0000 |
---|---|---|
committer | David Bolvansky <david.bolvansky@gmail.com> | 2019-09-04 12:00:33 +0000 |
commit | 358b80b34017ddabe6eabe8224408d58e7780a88 (patch) | |
tree | 180e46c84bd2104bba0e2293f7305e5547c52a6e | |
parent | 98634c2e11d837b7519b93ed922b05259762e3c8 (diff) | |
download | bcm5719-llvm-358b80b34017ddabe6eabe8224408d58e7780a88.tar.gz bcm5719-llvm-358b80b34017ddabe6eabe8224408d58e7780a88.zip |
[InstCombine] Fold sub (or A, B) (and A, B) to (xor A, B)
Summary:
```
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
Reviewers: spatel, lebedev.ri
Reviewed By: lebedev.ri
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67153
llvm-svn: 370883
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 8 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/sub-or-and-xor.ll | 28 |
2 files changed, 16 insertions, 20 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 36a776618f5..776bef36abf 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1717,6 +1717,14 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { 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; diff --git a/llvm/test/Transforms/InstCombine/sub-or-and-xor.ll b/llvm/test/Transforms/InstCombine/sub-or-and-xor.ll index 59fe79550a7..b1f163dad15 100644 --- a/llvm/test/Transforms/InstCombine/sub-or-and-xor.ll +++ b/llvm/test/Transforms/InstCombine/sub-or-and-xor.ll @@ -5,9 +5,7 @@ declare void @use(i32) 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(i32 %x, i32 %y) { 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_sub(i32 %x, i32 %y) { 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 @@ define i32 @sub_to_xor_extra_use_or(i32 %x, i32 %y) { ; 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_extra_use_or(i32 %x, i32 %y) { 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 i32 @sub_to_xor_or_commuted(i32 %x, i32 %y) { 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 [[OR]], [[AND]] +; CHECK-NEXT: [[SUB:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]] ; CHECK-NEXT: ret i32 [[SUB]] ; %or = or i32 %x, %y @@ -89,9 +79,7 @@ define i32 @sub_to_xor_and_commuted(i32 %x, i32 %y) { 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 |