[PATCH] D23139: Add popcount(n) == bitsize(n) -> n == -1 transformation.

Amaury SECHET via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 3 14:25:09 PDT 2016


deadalnix created this revision.
deadalnix added reviewers: majnemer, spatel.
deadalnix added a subscriber: llvm-commits.

As per title.

https://reviews.llvm.org/D23139

Files:
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  test/Transforms/InstCombine/intrinsics.ll

Index: test/Transforms/InstCombine/intrinsics.ll
===================================================================
--- test/Transforms/InstCombine/intrinsics.ll
+++ test/Transforms/InstCombine/intrinsics.ll
@@ -302,18 +302,23 @@
   %tz = tail call i32 @llvm.cttz.i32(i32 %a, i1 false) nounwind readnone
   %tz.cmp = icmp ne i32 %tz, 32
   store volatile i1 %tz.cmp, i1* %c
-  %pop = tail call i32 @llvm.ctpop.i32(i32 %b) nounwind readnone
-  %pop.cmp = icmp eq i32 %pop, 0
-  store volatile i1 %pop.cmp, i1* %c
+  %pop0 = tail call i32 @llvm.ctpop.i32(i32 %b) nounwind readnone
+  %pop0.cmp = icmp eq i32 %pop0, 0
+  store volatile i1 %pop0.cmp, i1* %c
+  %pop1 = tail call i32 @llvm.ctpop.i32(i32 %b) nounwind readnone
+  %pop1.cmp = icmp eq i32 %pop1, 32
+  store volatile i1 %pop1.cmp, i1* %c
   ret void
 ; CHECK: @cmp.simplify
 ; CHECK-NEXT: entry:
 ; CHECK-NEXT: %lz.cmp = icmp eq i32 %a, 0
 ; CHECK-NEXT: store volatile i1 %lz.cmp, i1* %c
 ; CHECK-NEXT: %tz.cmp = icmp ne i32 %a, 0
 ; CHECK-NEXT: store volatile i1 %tz.cmp, i1* %c
-; CHECK-NEXT: %pop.cmp = icmp eq i32 %b, 0
-; CHECK-NEXT: store volatile i1 %pop.cmp, i1* %c
+; CHECK-NEXT: %pop0.cmp = icmp eq i32 %b, 0
+; CHECK-NEXT: store volatile i1 %pop0.cmp, i1* %c
+; CHECK-NEXT: %pop1.cmp = icmp eq i32 %b, -1
+; CHECK-NEXT: store volatile i1 %pop1.cmp, i1* %c
 }
 
 define <2 x i1> @ctlz_cmp_vec(<2 x i32> %a) {
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2378,14 +2378,20 @@
       return &ICI;
     }
     break;
-  case Intrinsic::ctpop:
+  case Intrinsic::ctpop: {
     // popcount(A) == 0  ->  A == 0 and likewise for !=
-    if (*Op1C == 0) {
+    // popcount(A) == bitwidth(a)  ->  A == -1 and likewise for !=
+    bool IsZero = *Op1C == 0;
+    if (IsZero || *Op1C == Op1C->getBitWidth()) {
       Worklist.Add(II);
       ICI.setOperand(0, II->getArgOperand(0));
-      ICI.setOperand(1, ConstantInt::getNullValue(II->getType()));
+      auto NewOp = IsZero
+        ? ConstantInt::getNullValue(II->getType())
+        : ConstantInt::getAllOnesValue(II->getType());
+      ICI.setOperand(1, NewOp);
       return &ICI;
     }
+    }
     break;
   default:
     break;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23139.66722.patch
Type: text/x-patch
Size: 2341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160803/dbaab2da/attachment.bin>


More information about the llvm-commits mailing list