Skip to content

Commit a4a4db9

Browse files
committed
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. llvm-svn: 215524
1 parent b216ca5 commit a4a4db9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,18 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
25082508
if ((A == C && B == D) || (A == D && B == C))
25092509
return BinaryOperator::CreateXor(A, B);
25102510
}
2511+
// (A ^ B)^(A | B) -> A & B
2512+
if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
2513+
match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
2514+
if ((A == C && B == D) || (A == D && B == C))
2515+
return BinaryOperator::CreateAnd(A, B);
2516+
}
2517+
// (A | B)^(A ^ B) -> A & B
2518+
if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2519+
match(Op1I, m_Xor(m_Value(C), m_Value(D)))) {
2520+
if ((A == C && B == D) || (A == D && B == C))
2521+
return BinaryOperator::CreateAnd(A, B);
2522+
}
25112523
// (A & B) ^ (A ^ B) -> (A | B)
25122524
if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
25132525
match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))

llvm/test/Transforms/InstCombine/or-xor.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,14 @@ define i32 @test13(i32 %x, i32 %y) {
136136
; CHECK-NEXT: %and = and i32 %x, %y
137137
; CHECK-NEXT: ret i32 %and
138138
}
139+
140+
; ((x | y) ^ (x ^ y)) -> (x & y)
141+
define i32 @test15(i32 %x, i32 %y) {
142+
%1 = xor i32 %y, %x
143+
%2 = or i32 %y, %x
144+
%3 = xor i32 %2, %1
145+
ret i32 %3
146+
; CHECK-LABEL: @test15(
147+
; CHECK-NEXT: %1 = and i32 %y, %x
148+
; CHECK-NEXT: ret i32 %1
149+
}

0 commit comments

Comments
 (0)