On 7/6/2026 8:14 AM, [email protected] wrote:
From: Kyrylo Tkachov <[email protected]>

A double-word count of trailing or leading zeros written as

   lo ? ctz (lo) : 32 + ctz (hi)

with lo and hi the low and high halves of X equals a single wide ctzll (X),
and likewise for clz, but GCC kept the branch and two narrow counts.
Recognize the idiom in phiopt diamond handling and emit one .CTZ/.CLZ on the
double-word value.  As __builtin_c[lt]z is undefined at zero, so is the
result, so a one-argument .CTZ/.CLZ is used.

Based on a prototype by Andrea, refactored into helpers and extended
to handle clz as well as ctz, with testcases added.

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

        PR tree-optimization/126035

gcc/ChangeLog:

        * tree-ssa-phiopt.cc (clzctz_match_arm): New function.
        (clzctz_decode_half): New function.
        (clzctz_combine_replacement): New function.
        (pass_phiopt::execute): Call it for the diamond case.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/pr126035.c: New test.
        * gcc.target/aarch64/pr126035-clz.c: New test.
        * gcc.target/aarch64/pr126035-run.c: New test.
        * gcc.target/aarch64/pr126035-debug.c: New test.
        * gcc.target/aarch64/pr126035-extra-pred.c: New test.
        * gcc.target/aarch64/pr126035-narrow.c: New test.

Co-authored-by: Andrew Pinski <[email protected]>
Signed-off-by: Andrew Pinski <[email protected]>
Signed-off-by: Kyrylo Tkachov <[email protected]>
So do we need review on this, or are you and Andrea iterating on a newer version?  (Andrea mentioned this problem in another context)?

jeff

Reply via email to