Skip to content

Commit

Permalink
InstCombine: (sub (or A B) (xor A B)) --> (and A B)
Browse files Browse the repository at this point in the history
The following implements the transformation:
(sub (or A B) (xor A B)) --> (and A B).

Patch by Ankur Garg!

Differential Revision: http://reviews.llvm.org/D5719

llvm-svn: 220163
  • Loading branch information
majnemer committed Oct 19, 2014
1 parent 59939ac commit 312c3e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Expand Up @@ -1621,6 +1621,15 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
return BinaryOperator::CreateNeg(Y);
}

// (sub (or A, B) (xor A, B)) --> (and A, B)
{
Value *A = nullptr, *B = nullptr;
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
(match(Op0, m_Or(m_Specific(A), m_Specific(B))) ||
match(Op0, m_Or(m_Specific(B), m_Specific(A)))))
return BinaryOperator::CreateAnd(A, B);
}

if (Op1->hasOneUse()) {
Value *X = nullptr, *Y = nullptr, *Z = nullptr;
Constant *C = nullptr;
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/Transforms/InstCombine/sub.ll
Expand Up @@ -530,3 +530,13 @@ define i32 @test44(i32 %x) {
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 %x, -32768
; CHECK: ret i32 [[ADD]]
}

define i32 @test45(i32 %x, i32 %y) {
%or = or i32 %x, %y
%xor = xor i32 %x, %y
%sub = sub i32 %or, %xor
ret i32 %sub
; CHECK-LABEL: @test45(
; CHECK-NEXT: %sub = and i32 %x, %y
; CHECK: ret i32 %sub
}

0 comments on commit 312c3e5

Please sign in to comment.