Skip to content

Commit 960507b

Browse files
author
Mayur Pandey
committedAug 19, 2014
InstCombine: ((A & ~B) ^ (~A & B)) to A ^ B
Proof using CVC3 follows: $ cat t.cvc A, B : BITVECTOR(32); QUERY BVXOR((A & ~B),(~A & B)) = BVXOR(A,B); $ cvc3 t.cvc Valid. Differential Revision: http://reviews.llvm.org/D4898 llvm-svn: 215974
1 parent 97ebe53 commit 960507b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,16 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
25222522
match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) {
25232523
return BinaryOperator::CreateXor(A, B);
25242524
}
2525+
// (A & ~B) ^ (~A & B) -> A ^ B
2526+
if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2527+
match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B)))) {
2528+
return BinaryOperator::CreateXor(A, B);
2529+
}
2530+
// (~A & B) ^ (A & ~B) -> A ^ B
2531+
if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2532+
match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) {
2533+
return BinaryOperator::CreateXor(A, B);
2534+
}
25252535
// (A ^ B)^(A | B) -> A & B
25262536
if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
25272537
match(Op1I, m_Or(m_Value(C), m_Value(D)))) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,16 @@ define i32 @test16(i32 %x, i32 %y) {
160160
; CHECK-NEXT: %xor = xor i32 %x, %y
161161
; CHECK-NEXT: ret i32 %xor
162162
}
163+
164+
; ((x & ~y) ^ (~x & y)) -> x ^ y
165+
define i32 @test17(i32 %x, i32 %y) {
166+
%noty = xor i32 %y, -1
167+
%notx = xor i32 %x, -1
168+
%and1 = and i32 %x, %noty
169+
%and2 = and i32 %notx, %y
170+
%xor = xor i32 %and1, %and2
171+
ret i32 %xor
172+
; CHECK-LABEL: @test17(
173+
; CHECK-NEXT: %xor = xor i32 %x, %y
174+
; CHECK-NEXT: ret i32 %xor
175+
}

0 commit comments

Comments
 (0)
Please sign in to comment.