Add instcombine patterns for the following transformations:

(x & y) | (x ^ y) -> x | y 
 (x & y) + (x ^ y) -> x | y 

Patch by Manman Ren.
rdar://10770603

llvm-svn: 155674
This commit is contained in:
Chad Rosier 2012-04-26 23:29:14 +00:00
parent 4be3b28946
commit f3d4646377
3 changed files with 43 additions and 0 deletions

View File

@ -329,6 +329,20 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
}
}
// Check for (x & y) + (x ^ y)
{
Value *A = 0, *B = 0;
if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
(match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
match(LHS, m_And(m_Specific(B), m_Specific(A)))))
return BinaryOperator::CreateOr(A, B);
if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
(match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
match(RHS, m_And(m_Specific(B), m_Specific(A)))))
return BinaryOperator::CreateOr(A, B);
}
return Changed ? &I : 0;
}

View File

@ -1932,10 +1932,15 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
// A | ( A ^ B) -> A | B
// A | (~A ^ B) -> A | ~B
// (A & B) | (A ^ B)
if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
if (Op0 == A || Op0 == B)
return BinaryOperator::CreateOr(A, B);
if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
match(Op0, m_And(m_Specific(B), m_Specific(A))))
return BinaryOperator::CreateOr(A, B);
if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
Value *Not = Builder->CreateNot(B, B->getName()+".not");
return BinaryOperator::CreateOr(Not, Op0);

View File

@ -0,0 +1,24 @@
; RUN: opt < %s -instcombine -S | FileCheck %s
; rdar://10770603
; (x & y) | (x ^ y) -> x | y
define i64 @or(i64 %x, i64 %y) nounwind uwtable readnone ssp {
%1 = and i64 %y, %x
%2 = xor i64 %y, %x
%3 = add i64 %1, %2
ret i64 %3
; CHECK: @or
; CHECK: or i64
; CHECK-NEXT: ret
}
; (x & y) + (x ^ y) -> x | y
define i64 @or2(i64 %x, i64 %y) nounwind uwtable readnone ssp {
%1 = and i64 %y, %x
%2 = xor i64 %y, %x
%3 = or i64 %1, %2
ret i64 %3
; CHECK: @or2
; CHECK: or i64
; CHECK-NEXT: ret
}