From a04fb96d9be10d393fd218bd4aacdc19b98d7d6e Mon Sep 17 00:00:00 2001 From: James Molloy Date: Thu, 8 Oct 2015 12:40:06 +0000 Subject: [PATCH] Compute demanded bits for icmp instructions Instead of bailing out when we see an icmp, we can instead at least say that if the upper bits of both operands are known zero, they are not demanded. This doesn't help with signed comparisons, but it's at least better than bailing out. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249687 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/DemandedBits.cpp | 7 +++++++ test/Analysis/DemandedBits/basic.ll | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Analysis/DemandedBits.cpp b/lib/Analysis/DemandedBits.cpp index 6f92ba6289a..912c5ceb754 100644 --- a/lib/Analysis/DemandedBits.cpp +++ b/lib/Analysis/DemandedBits.cpp @@ -242,6 +242,13 @@ void DemandedBits::determineLiveOperandBits( if (OperandNo != 0) AB = AOut; break; + case Instruction::ICmp: + // Count the number of leading zeroes in each operand. + ComputeKnownBits(BitWidth, I, UserI->getOperand(1)); + auto NumLeadingZeroes = std::min(KnownZero.countLeadingOnes(), + KnownZero2.countLeadingOnes()); + AB = ~APInt::getHighBitsSet(BitWidth, NumLeadingZeroes); + break; } } diff --git a/test/Analysis/DemandedBits/basic.ll b/test/Analysis/DemandedBits/basic.ll index 3fd1b321288..487e522e9db 100644 --- a/test/Analysis/DemandedBits/basic.ll +++ b/test/Analysis/DemandedBits/basic.ll @@ -10,3 +10,25 @@ define i8 @test_mul(i32 %a, i32 %b) { %3 = trunc i32 %2 to i8 ret i8 %3 } + +; CHECK-LABEL: 'test_icmp1' +; CHECK-DAG: DemandedBits: 0x1 for %3 = icmp eq i32 %1, %2 +; CHECK-DAG: DemandedBits: 0xFFF for %1 = and i32 %a, 255 +; CHECK-DAG: DemandedBits: 0xFFF for %2 = shl i32 %1, 4 +define i1 @test_icmp1(i32 %a, i32 %b) { + %1 = and i32 %a, 255 + %2 = shl i32 %1, 4 + %3 = icmp eq i32 %1, %2 + ret i1 %3 +} + +; CHECK-LABEL: 'test_icmp2' +; CHECK-DAG: DemandedBits: 0x1 for %3 = icmp eq i32 %1, %2 +; CHECK-DAG: DemandedBits: 0xFF for %1 = and i32 %a, 255 +; CHECK-DAG: DemandedBits: 0xF for %2 = ashr i32 %1, 4 +define i1 @test_icmp2(i32 %a, i32 %b) { + %1 = and i32 %a, 255 + %2 = ashr i32 %1, 4 + %3 = icmp eq i32 %1, %2 + ret i1 %3 +} -- 2.34.1