From 1a1b1f708ddaba3834bf22a19cc9eed764093660 Mon Sep 17 00:00:00 2001 From: Suyog Sarda Date: Tue, 22 Jul 2014 18:09:41 +0000 Subject: [PATCH] Added InstCombine Transform for patterns: "((~A & B) | A) -> (A | B)" and "((A & B) | ~A) -> (~A | B)" Original Patch credit to Ankit Jain !! Differential Revision: http://reviews.llvm.org/D4591 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213676 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineAndOrXor.cpp | 10 ++++++++++ test/Transforms/InstCombine/or.ll | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 431f7323511..ef6a848357e 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1988,6 +1988,16 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { return BinaryOperator::CreateXor(NOr, C1); } + // ((~A & B) | A) -> (A | B) + if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) && + match(Op1, m_Specific(A))) + return BinaryOperator::CreateOr(A, B); + + // ((A & B) | ~A) -> (~A | B) + if (match(Op0, m_And(m_Value(A), m_Value(B))) && + match(Op1, m_Not(m_Specific(A)))) + return BinaryOperator::CreateOr(Builder->CreateNot(A), B); + // (A & C)|(B & D) Value *C = nullptr, *D = nullptr; if (match(Op0, m_And(m_Value(A), m_Value(C))) && diff --git a/test/Transforms/InstCombine/or.ll b/test/Transforms/InstCombine/or.ll index 1cd897ee90c..1a493df6153 100644 --- a/test/Transforms/InstCombine/or.ll +++ b/test/Transforms/InstCombine/or.ll @@ -408,3 +408,22 @@ define i32 @test38(i32* %xp, i32 %y) { %or = or i32 %x, %sext ret i32 %or } + +define i32 @test39(i32 %a, i32 %b) { +; CHECK-LABEL: test39( +; CHECK-NEXT: %or = or i32 %a, %b + %xor = xor i32 %a, -1 + %and = and i32 %xor, %b + %or = or i32 %and, %a + ret i32 %or +} + +define i32 @test40(i32 %a, i32 %b) { +; CHECK-LABEL: test40( +; CHECK-NEXT: %1 = xor i32 %a, -1 +; CHECK-NEXT: %or = or i32 %1, %b + %and = and i32 %a, %b + %xor = xor i32 %a, -1 + %or = or i32 %and, %xor + ret i32 %or +} -- 2.34.1