InstCombine: Combine (xor (or %a, %b) (xor %a, %b)) to (add %a, %b)

Correctness proof of the transform using CVC3-

$ cat t.cvc
A, B : BITVECTOR(32);
QUERY BVXOR(A | B, BVXOR(A,B) ) = A & B;

$ cvc3 t.cvc
Valid.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215524 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Karthik Bhat 2014-08-13 05:13:14 +00:00
parent 59ee88d574
commit 7ef167ae1f
2 changed files with 23 additions and 0 deletions

View File

@ -2508,6 +2508,18 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
// (A ^ B)^(A | B) -> A & B
if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateAnd(A, B);
}
// (A | B)^(A ^ B) -> A & B
if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
match(Op1I, m_Xor(m_Value(C), m_Value(D)))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateAnd(A, B);
}
// (A & B) ^ (A ^ B) -> (A | B)
if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))

View File

@ -136,3 +136,14 @@ define i32 @test13(i32 %x, i32 %y) {
; CHECK-NEXT: %and = and i32 %x, %y
; CHECK-NEXT: ret i32 %and
}
; ((x | y) ^ (x ^ y)) -> (x & y)
define i32 @test15(i32 %x, i32 %y) {
%1 = xor i32 %y, %x
%2 = or i32 %y, %x
%3 = xor i32 %2, %1
ret i32 %3
; CHECK-LABEL: @test15(
; CHECK-NEXT: %1 = and i32 %y, %x
; CHECK-NEXT: ret i32 %1
}