]> git.ipfire.org Git - thirdparty/gcc.git/commit
[PATCH v3] match.pd: popcount(X & -X) -> -X != 0 [PR102486]
authorDaniel Barboza <daniel.barboza@oss.qualcomm.com>
Wed, 7 Jan 2026 14:17:26 +0000 (07:17 -0700)
committerJeff Law <jeffrey.law@oss.qualcomm.com>
Wed, 7 Jan 2026 14:20:22 +0000 (07:20 -0700)
commitf26f5e3a4c42e50a34cb2d3e5f02a70a8faa596f
treec8d0e2da775d05246a4d120aba14da1cf3554228
parent3ee0e36f81af4ca2355430c910ada014c66aab3a
[PATCH v3] match.pd: popcount(X & -X) -> -X != 0 [PR102486]

The initial idea of this optimization was to reduce it to "X != 0",
checking for either X being an unsigned or a truncating conversion.
Then we discussed reducing it to "(X & -X) != 0" instead. This form
would avoid the potential trapping problems (like -ftrapv) that might
happen in case X is not an unsigned type.

Then, as suggested by Roger Sayle in bugzilla, we could reduce to just
"-X != 0". Keeping the negated value in the pattern preserves any trapping
or UBs to be handled by other match.pd patterns that are more able to do
the conversion to "X != 0" when applicable. This would also spare us from
a TYPE_UNSIGNED check.

PR tree-optimization/102486

gcc/ChangeLog:

* match.pd (`popcount (X & -X) -> -X != 0`): New pattern.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Daniel Barboza <daniel.barboza@oss.qualcomm.com>
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr102486.c [new file with mode: 0644]