[llvm] r249687 - Compute demanded bits for icmp instructions

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 05:40:06 PDT 2015


Author: jamesm
Date: Thu Oct  8 07:40:06 2015
New Revision: 249687

URL: http://llvm.org/viewvc/llvm-project?rev=249687&view=rev
Log:
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.

Modified:
    llvm/trunk/lib/Analysis/DemandedBits.cpp
    llvm/trunk/test/Analysis/DemandedBits/basic.ll

Modified: llvm/trunk/lib/Analysis/DemandedBits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/DemandedBits.cpp?rev=249687&r1=249686&r2=249687&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/DemandedBits.cpp (original)
+++ llvm/trunk/lib/Analysis/DemandedBits.cpp Thu Oct  8 07:40:06 2015
@@ -242,6 +242,13 @@ void DemandedBits::determineLiveOperandB
     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;
   }
 }
 

Modified: llvm/trunk/test/Analysis/DemandedBits/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/DemandedBits/basic.ll?rev=249687&r1=249686&r2=249687&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/DemandedBits/basic.ll (original)
+++ llvm/trunk/test/Analysis/DemandedBits/basic.ll Thu Oct  8 07:40:06 2015
@@ -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
+}




More information about the llvm-commits mailing list