]> git.ipfire.org Git - thirdparty/gcc.git/commit
tree-ssa-math-opts: popcount (X) == 1 to (X ^ (X - 1)) > (X - 1) optimization [PR90693]
authorJakub Jelinek <jakub@redhat.com>
Mon, 20 Nov 2023 09:00:09 +0000 (10:00 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 20 Nov 2023 09:00:09 +0000 (10:00 +0100)
commitd0b6b7f8a6b8115b033441590a6304fb088d193c
tree59a53cfc6bdaae051b44e913d0b6d06b45a19893
parent99fad213d5d457162be00d873cd655fe402ca91a
tree-ssa-math-opts: popcount (X) == 1 to (X ^ (X - 1)) > (X - 1) optimization [PR90693]

Per the earlier discussions on this PR, the following patch folds
popcount (x) == 1 (and != 1) into (x ^ (x - 1)) > x - 1 (or <=)
if the corresponding popcount optab isn't implemented (I think any
double-word popcount or call will be necessarily slower than the
above cheap 3 op check and even for -Os larger or same size).

I've noticed e.g. C++ aligned new starts with std::has_single_bit
which does popcount (x) == 1.

As a follow-up, I'm considering changing in this routine the popcount
call to IFN_POPCOUNT with 2 arguments and during expansion test costs.

2023-11-20  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/90693
* tree-ssa-math-opts.cc (match_single_bit_test): New function.
(math_opts_dom_walker::after_dom_children): Call it for EQ_EXPR
and NE_EXPR assignments and GIMPLE_CONDs.

* gcc.target/i386/pr90693.c: New test.
gcc/testsuite/gcc.target/i386/pr90693.c [new file with mode: 0644]
gcc/tree-ssa-math-opts.cc