]> git.ipfire.org Git - thirdparty/gcc.git/commit
aarch64: Improve popcountti2 with SVE
authorKyrylo Tkachov <ktkachov@nvidia.com>
Fri, 4 Jul 2025 13:49:15 +0000 (06:49 -0700)
committerKyrylo Tkachov <ktkachov@nvidia.com>
Mon, 7 Jul 2025 14:17:56 +0000 (16:17 +0200)
commit0c73f2f1f92c135f50dcc2ab76d5e53236262a4e
tree4b128a9c1bbf92e6f70dc8ef761baf1541edfd2e
parent8ad5968a8dcb472cbff8e4c48217fd65e125b2f2
aarch64: Improve popcountti2 with SVE

The TImode popcount sequence can be slightly improved with SVE.
If we generate:
        ldr     q31, [x0]
        ptrue   p7.b, vl16
        cnt     z31.d, p7/m, z31.d
        addp    d31, v31.2d
        fmov    x0, d31
        ret

instead of:
h128:
        ldr     q31, [x0]
        cnt     v31.16b, v31.16b
        addv    b31, v31.16b
        fmov    w0, s31
        ret

we use the ADDP instruction for reduction, which is cheaper on all CPUs AFAIK,
as it is only a single 64-bit addition vs the tree of additions for ADDV.
For example, on a CPU like Grace we get a latency and throughput of 2,4 vs 4,1
for ADDV.
We do generate one more instruction due to the PTRUE being materialised, but that
is cheap itself and can be scheduled away from the critical path or even CSE'd
with other PTRUE constants.
As this sequence is larger code size-wise it is avoided for -Os.

Bootstrapped and tested on aarch64-none-linux-gnu.

Signed-off-by: Kyrylo Tkachov <ktkachov@nvidia.com>
gcc/

* config/aarch64/aarch64.md (popcountti2): Add TARGET_SVE path.

gcc/testsuite/

* gcc.target/aarch64/popcnt9.c: Add +nosve to target pragma.
* gcc.target/aarch64/popcnt13.c: New test.
gcc/config/aarch64/aarch64.md
gcc/testsuite/gcc.target/aarch64/popcnt13.c [new file with mode: 0644]
gcc/testsuite/gcc.target/aarch64/popcnt9.c