Re: [PATCH] match: Fix incorrect identification of POPCOUNT pattern [PR 126466]

Andrea Pinski <[email protected]> Sun, 2 Aug 2026 01:51:37 -0700
Newsgroups gmane.comp.gcc.patches
Message-ID <CALvbMcBX0ZTz0u+AyV4eohzsDtSMQdASbM_pqESuQ4xPCmKkxA@mail.gmail.com>
On Thu, Jul 30, 2026 at 3:23 AM <[email protected]> wrote:
>
> From: Reshma Roy <[email protected]>
>
> This fixes r17-489-g8ca1e887847e2f, which added a third 32-bit
> Hacker's Delight popcount matcher.  Its predicate checked
> compare_tree_int (@5, 0x0F0F0F0F) twice and never validated the
> final outer AND constant (@7), so any mask was accepted once the
> earlier constants matched.
>
> Require compare_tree_int (@7, 0x0000003F) so only the intended
> popcount idiom is folded to IFN_POPCOUNT.
>
>
> PR tree-optimization/PR126466
> gcc/ChangeLog:
>
>         * match.pd: Fix incorrect POPCOUNT identification in the third
>           32-bit Hacker's Delight matcher.

Ok.

>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/popcount10.c: New test.
>
> ---
> Hi Richard,
>    This patch fixes the bug reported in
>    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126466 on incorrect
>    identification of POPCOUNT pattern.
>
>    Bootstrapped and tested on x86_64-linux.
>
> Thanks,
> Reshma
>
>
>  gcc/match.pd                               |  2 +-
>  gcc/testsuite/gcc.dg/tree-ssa/popcount10.c | 20 ++++++++++++++++++++
>  2 files changed, 21 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/popcount10.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 4b2a360966c..adcb8085f0e 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -11201,7 +11201,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>         && compare_tree_int (@1, 0x55555555) == 0
>         && compare_tree_int (@3, 0x33333333) == 0
>         && compare_tree_int (@5, 0x0F0F0F0F) == 0
> -       && compare_tree_int (@5, 0x0F0F0F0F) == 0)
> +       && compare_tree_int (@7, 0x0000003F) == 0)
>       (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
>                                           OPTIMIZE_FOR_BOTH))
>         (convert (IFN_POPCOUNT:type @0))))))
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount10.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount10.c
> new file mode 100644
> index 00000000000..ed894618690
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount10.c
> @@ -0,0 +1,20 @@
> +/* { dg-do compile } */
> +/* { dg-require-effective-target popcount } */
> +/* { dg-require-effective-target int32plus } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +const unsigned m1  = 0x55555555UL;
> +const unsigned m2  = 0x33333333UL;
> +const unsigned m3  = 0x0F0F0F0FUL;
> +const unsigned m4  = 0x0000FFFFUL;
> +
> +int popc(unsigned x) {
> +  x = x - ((x >> 1) & m1);
> +  x = x - 3*((x >> 2) & m2);
> +  x = (x + (x >> 4)) & m3;
> +  x = x + (x >> 8);
> +  x = x + (x >> 16);
> +  return x & m4;
> +}
> +
> +/* { dg-final { scan-tree-dump-not "\.POPCOUNT" "optimized" } } */
> --
> 2.34.1
>