[Bug tree-optimization/124630] popcnt (a ^ (a - 1)) -1 is ctz when a is known non-zero

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 25 01:30:34 GMT 2026


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124630

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by hongtao Liu <liuhongt@gcc.gnu.org>:

https://gcc.gnu.org/g:421f09e24c9c82a9ff6a61dd0b65687175c90bff

commit r17-709-g421f09e24c9c82a9ff6a61dd0b65687175c90bff
Author: liuhongt <hongtao.liu@intel.com>
Date:   Thu May 14 02:06:43 2026 -0700

    match.pd: Fold popcount(x ^ (x - 1)) to ctz(x) + 1 [PR124630]

    When X is nonzero, X ^ (X - 1) produces a mask of trailing zeros plus
    the lowest set bit, so popcount of that expression equals ctz(X) + 1.

    gcc/ChangeLog:

            PR middle-end/124630
            * match.pd (popcount (x ^ (x - 1))): Fold to ctz (x) + 1 when
            x is nonzero and CTZ is directly supported.

    gcc/testsuite/ChangeLog:

            PR middle-end/124630
            * gcc.dg/pr124630.c: New test.
            * gcc.target/i386/pr124630.c: New test.


More information about the Gcc-bugs mailing list