https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102486
--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:f26f5e3a4c42e50a34cb2d3e5f02a70a8faa596f commit r16-6549-gf26f5e3a4c42e50a34cb2d3e5f02a70a8faa596f Author: Daniel Barboza <[email protected]> Date: Wed Jan 7 07:17:26 2026 -0700 [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 <[email protected]>
