InstCombine: Combine (X | Y) - X to (~X & Y)

This implements the transformation from (X | Y) - X to (~X & Y).

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221129 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer
2014-11-03 05:53:55 +00:00
parent c70aecad29
commit 9808f11b09
2 changed files with 25 additions and 6 deletions

View File

@ -1630,6 +1630,15 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
return BinaryOperator::CreateAnd(A, B);
}
if (Op0->hasOneUse()) {
Value *Y = nullptr;
// ((X | Y) - X) --> (~X & Y)
if (match(Op0, m_Or(m_Value(Y), m_Specific(Op1))) ||
match(Op0, m_Or(m_Specific(Op1), m_Value(Y))))
return BinaryOperator::CreateAnd(
Y, Builder->CreateNot(Op1, Op1->getName() + ".not"));
}
if (Op1->hasOneUse()) {
Value *X = nullptr, *Y = nullptr, *Z = nullptr;
Constant *C = nullptr;

View File

@ -540,3 +540,13 @@ define i32 @test45(i32 %x, i32 %y) {
; CHECK-NEXT: %sub = and i32 %x, %y
; CHECK: ret i32 %sub
}
define i32 @test46(i32 %x, i32 %y) {
%or = or i32 %x, %y
%sub = sub i32 %or, %x
ret i32 %sub
; CHECK-LABEL: @test46(
; CHECK-NEXT: %x.not = xor i32 %x, -1
; CHECK-NEXT: %sub = and i32 %y, %x.not
; CHECK: ret i32 %sub
}