From 98a8206e58d32adebaba501d40266eefb42cb06a Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 12 Jul 2010 13:44:00 +0000 Subject: [PATCH] Reapply 108136 with an ugly pasto fixed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@108141 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineAndOrXor.cpp | 17 +++++++++++++++++ test/Transforms/InstCombine/or.ll | 13 +++++++++++++ test/Transforms/InstCombine/xor2.ll | 14 ++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 3f4a857c41a..ccf5cd8581d 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1597,6 +1597,14 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D); if (Ret) return Ret; } + + // (A & ~C) | (B & C) -> ((B ^ A) & C) ^ A + if (Op0->hasOneUse() && Op1->hasOneUse() && + match(C, m_Not(m_Specific(D)))) { + Value *Xor = Builder->CreateXor(B, A, "xor"); + Value *And = Builder->CreateAnd(Xor, D, "and"); + return BinaryOperator::CreateXor(And, A); + } } // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. @@ -1920,6 +1928,15 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { return BinaryOperator::CreateAnd(NewOp, X); } } + + // (A & ~C) ^ (B & C) -> ((B ^ A) & C) ^ A + if (Op0->hasOneUse() && Op1->hasOneUse() && + match(Op0I, m_And(m_Value(A), m_Not(m_Value(D)))) && + match(Op1I, m_And(m_Value(B), m_Specific(D)))) { + Value *Xor = Builder->CreateXor(B, A, "xor"); + Value *And = Builder->CreateAnd(Xor, D, "and"); + return BinaryOperator::CreateXor(And, A); + } } // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) diff --git a/test/Transforms/InstCombine/or.ll b/test/Transforms/InstCombine/or.ll index c3526b77f6a..0246925bd82 100644 --- a/test/Transforms/InstCombine/or.ll +++ b/test/Transforms/InstCombine/or.ll @@ -350,3 +350,16 @@ define <4 x i32> @test32(<4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32 ; CHECK: or <4 x i32> %and.i, %and.i129 } +; PR6773 +define i32 @test33(i32 %x, i32 %y, i32 %z) nounwind readnone { + %and = and i32 %y, %x + %not = xor i32 %x, -1 + %and2 = and i32 %z, %not + %or = or i32 %and2, %and + ret i32 %or +; CHECK: @test33 +; CHECK-NEXT: xor i32 +; CHECK-NEXT: and i32 +; CHECK-NEXT: xor i32 +; CHECK-NEXT: ret i32 +} diff --git a/test/Transforms/InstCombine/xor2.ll b/test/Transforms/InstCombine/xor2.ll index 67f05efa23d..19804c19488 100644 --- a/test/Transforms/InstCombine/xor2.ll +++ b/test/Transforms/InstCombine/xor2.ll @@ -51,3 +51,17 @@ define i32 @test4(i32 %A, i32 %B) { ; CHECK: %1 = ashr i32 %A, %B ; CHECK: ret i32 %1 } + +; PR6773 +define i32 @test5(i32 %x, i32 %y, i32 %z) nounwind readnone { + %and = and i32 %y, %x + %not = xor i32 %x, -1 + %and2 = and i32 %z, %not + %xor = xor i32 %and2, %and + ret i32 %xor +; CHECK: @test5 +; CHECK-NEXT: xor i32 +; CHECK-NEXT: and i32 +; CHECK-NEXT: xor i32 +; CHECK-NEXT: ret i32 +}