From 87569413b074f9b2a2a67b279ec1e254179fcfcc Mon Sep 17 00:00:00 2001 From: Suyog Sarda Date: Fri, 1 Aug 2014 04:41:43 +0000 Subject: [PATCH] This patch implements transform for pattern "(A & B) | ((~A) ^ B) -> (~A ^ B)". Patch Credit to Ankit Jain ! Differential Revision: http://reviews.llvm.org/D4655 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214476 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineAndOrXor.cpp | 10 +++++++++ test/Transforms/InstCombine/or.ll | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 51564088e67..60e81ebfd0c 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2167,6 +2167,16 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { return BinaryOperator::CreateOr(Not, Op0); } + // (A & B) | ((~A) ^ B) -> (~A ^ B) + if (match(Op0, m_And(m_Value(A), m_Value(B))) && + match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B)))) + return BinaryOperator::CreateXor(Builder->CreateNot(A), B); + + // ((~A) ^ B) | (A & B) -> (~A ^ B) + if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) && + match(Op1, m_And(m_Specific(A), m_Specific(B)))) + return BinaryOperator::CreateXor(Builder->CreateNot(A), B); + if (SwappedForXor) std::swap(Op0, Op1); diff --git a/test/Transforms/InstCombine/or.ll b/test/Transforms/InstCombine/or.ll index 1a493df6153..8e2e6ad9677 100644 --- a/test/Transforms/InstCombine/or.ll +++ b/test/Transforms/InstCombine/or.ll @@ -427,3 +427,25 @@ define i32 @test40(i32 %a, i32 %b) { %or = or i32 %and, %xor ret i32 %or } + +define i32 @test41(i32 %a, i32 %b) { +; CHECK-LABEL: test41( +; CHECK-NEXT: %1 = xor i32 %a, -1 +; CHECK-NEXT: %or = xor i32 %1, %b + %and = and i32 %a, %b + %nega = xor i32 %a, -1 + %xor = xor i32 %nega, %b + %or = or i32 %and, %xor + ret i32 %or +} + +define i32 @test42(i32 %a, i32 %b) { +; CHECK-LABEL: test42( +; CHECK-NEXT: %1 = xor i32 %a, -1 +; CHECK-NEXT: %or = xor i32 %1, %b + %nega = xor i32 %a, -1 + %xor = xor i32 %nega, %b + %and = and i32 %a, %b + %or = or i32 %xor, %and + ret i32 %or +}