]> gcc.gnu.org Git - gcc.git/commitdiff
range-ops: Calculate the popcount of a singleton.
authorAldy Hernandez <aldyh@redhat.com>
Tue, 27 Sep 2022 08:30:00 +0000 (10:30 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Tue, 27 Sep 2022 15:02:11 +0000 (17:02 +0200)
The legacy popcount folding didn't actually fold singleton ranges.
I don't think anyone noticed because there are match.pd patterns that
fold __builtin_popcount using the global nonzero bits set by CCP.

It's good form to handle this, even without CCP's help.

Tested on x86-64 Linux.

gcc/ChangeLog:

* gimple-range-op.cc (cfn_popcount): Calculate the popcount of a
singleton.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/popcount6b.c: New test.

gcc/gimple-range-op.cc
gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c [new file with mode: 0644]

index d7c6dfa933d1f63192953807815778b38956f3df..3f5e5852e5a21329193e18d9eef9653bd90e2749 100644 (file)
@@ -397,6 +397,14 @@ public:
   {
     if (lh.undefined_p ())
       return false;
+    // Calculating the popcount of a singleton is trivial.
+    if (lh.singleton_p ())
+      {
+       wide_int nz = lh.get_nonzero_bits ();
+       wide_int pop = wi::shwi (wi::popcount (nz), TYPE_PRECISION (type));
+       r.set (type, pop, pop);
+       return true;
+      }
     // __builtin_ffs* and __builtin_popcount* return [0, prec].
     int prec = TYPE_PRECISION (lh.type ());
     // If arg is non-zero, then ffs or popcount are non-zero.
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c
new file mode 100644 (file)
index 0000000..90336ec
--- /dev/null
@@ -0,0 +1,6 @@
+// { dg-do compile }
+// { dg-options "-O2 -fdump-tree-evrp -fno-tree-ccp" }
+
+#include "popcount6.c"
+
+// { dg-final { scan-tree-dump "return 1;" "evrp" } }
This page took 0.064479 seconds and 5 git commands to generate.