diff options
author | Aldy Hernandez <aldyh@redhat.com> | 2022-09-27 10:30:00 +0200 |
---|---|---|
committer | Aldy Hernandez <aldyh@redhat.com> | 2022-09-27 17:02:11 +0200 |
commit | 001c60ccfeaf9a480a56203418af804ad42e88b9 (patch) | |
tree | 6063fd6a3ed81175459c4f1a848f1fc625da8017 /gcc | |
parent | 971bc0aae9cf52abe9a6fcab3b7c25d1fa94ad1e (diff) | |
download | gcc-001c60ccfeaf9a480a56203418af804ad42e88b9.zip gcc-001c60ccfeaf9a480a56203418af804ad42e88b9.tar.gz gcc-001c60ccfeaf9a480a56203418af804ad42e88b9.tar.bz2 |
range-ops: Calculate the popcount of a singleton.
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/gimple-range-op.cc | 8 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index d7c6dfa..3f5e585 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -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 index 0000000..90336ec --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c @@ -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" } } |