From 63522b1998289e694385a45a719313c95f3350cf Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 12 Dec 2012 20:48:54 +0000 Subject: [PATCH] Simplify negated bit test git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170020 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineCompares.cpp | 18 +++++++++++++ test/Transforms/InstCombine/icmp.ll | 26 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 1b96c3cca4e..b0fc82f6f66 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2034,6 +2034,15 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { CI->countTrailingZeros())); } + // Turn x&~y == 0 into x&y != 0 if x is a power of 2. + Value *X = 0, *Y = 0; + if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) && + match(Op1, m_Zero()) && isPowerOfTwo(X, TD)) { + return new ICmpInst(ICmpInst::ICMP_NE, + Builder->CreateAnd(X, Y), + Op1); + } + break; } case ICmpInst::ICMP_NE: { @@ -2071,6 +2080,15 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { CI->countTrailingZeros())); } + // Turn x&~y != 0 into x&y == 0 if x is a power of 2. + Value *X = 0, *Y = 0; + if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) && + match(Op1, m_Zero()) && isPowerOfTwo(X, TD)) { + return new ICmpInst(ICmpInst::ICMP_EQ, + Builder->CreateAnd(X, Y), + Op1); + } + break; } case ICmpInst::ICMP_ULT: diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index 8e064a4f2fc..d11b6cc756f 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -677,3 +677,29 @@ define i1 @test66(i64 %A, i64 %B) { ; CHECK-NEXT: ret i1 true ret i1 %cmp } + +define i1 @test67(i32 %A, i32 %B) { + %neg = xor i32 %A, -1 + %shl = shl i32 1, %B + %and = and i32 %shl, %neg + %cmp = icmp ne i32 %and, 0 +; CHECK: @test67 +; CHECK-NEXT: %shl = shl i32 1, %B +; CHECK-NEXT: %1 = and i32 %shl, %A +; CHECK-NEXT: %cmp = icmp eq i32 %1, 0 +; CHECK-NEXT: ret i1 %cmp + ret i1 %cmp +} + +define i1 @test68(i32 %A, i32 %B) { + %neg = xor i32 %A, -1 + %shl = shl i32 1, %B + %and = and i32 %shl, %neg + %cmp = icmp eq i32 %and, 0 +; CHECK: @test68 +; CHECK-NEXT: %shl = shl i32 1, %B +; CHECK-NEXT: %1 = and i32 %shl, %A +; CHECK-NEXT: %cmp = icmp ne i32 %1, 0 +; CHECK-NEXT: ret i1 %cmp + ret i1 %cmp +}