From eb323b2b3c7d72ae00bf6d9998fcb8b027102965 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 14 Aug 2014 06:46:25 +0000 Subject: [PATCH] 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. Patch by Mayur Pandey! Differential Revision: http://reviews.llvm.org/D4883 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215621 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 10 ++++++++++ test/Transforms/InstCombine/or-xor.ll | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 64708c4d4f7..063d4a799e4 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2512,6 +2512,16 @@ 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_Or(m_Value(A), m_Not(m_Value(B)))) && + match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B)))) { + return BinaryOperator::CreateXor(A, B); + } + // (~A | B) ^ (A | ~B) -> A ^ B + if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) && + match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) { + 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)))) { diff --git a/test/Transforms/InstCombine/or-xor.ll b/test/Transforms/InstCombine/or-xor.ll index 6984c44d9a3..984178aeb34 100644 --- a/test/Transforms/InstCombine/or-xor.ll +++ b/test/Transforms/InstCombine/or-xor.ll @@ -147,3 +147,16 @@ define i32 @test15(i32 %x, i32 %y) { ; CHECK-NEXT: %1 = and i32 %y, %x ; CHECK-NEXT: ret i32 %1 } + +; ((x | ~y) ^ (~x | y)) -> x ^ y +define i32 @test16(i32 %x, i32 %y) { + %noty = xor i32 %y, -1 + %notx = xor i32 %x, -1 + %or1 = or i32 %x, %noty + %or2 = or i32 %notx, %y + %xor = xor i32 %or1, %or2 + ret i32 %xor +; CHECK-LABEL: @test16( +; CHECK-NEXT: %xor = xor i32 %x, %y +; CHECK-NEXT: ret i32 %xor +} -- 2.11.0