[Bug tree-optimization/125734] clz loop not detected for signed integer types where it is always non-negative

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Sep 10 01:20:16 GMT 2026


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

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrea Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:e90e0f66051d338c1950d2cc9e36967552d75c3a

commit r17-4047-ge90e0f66051d338c1950d2cc9e36967552d75c3a
Author: Shivam Gupta <shivam98.tkg@gmail.com>
Date:   Mon Sep 7 06:36:15 2026 +0530

    tree-optimization: Recognize CLZ loop for signed non-negative values

    is_rshift_by_1 previously checked TYPE_UNSIGNED on the shift statement,
    preventing signed arithmetic right shifts from being optimized into CLZ
    calls. However, an arithmetic right shift behaves identically to a logical
    right shift when the initial operand entering the loop is non-negative.

    This patch removes the strict TYPE_UNSIGNED requirement from is_rshift_by_1
    and adds ranger range_on_edge non-negativity checks in
number_of_iterations_cltz
    and number_of_iterations_cltz_complement on the loop preheader edge.

    Regression tested on arch64-unknown-linux-gnu.

            PR tree-optimization/125734

    gcc/ChangeLog:

            * tree-ssa-loop-niter.cc (is_rshift_by_1): Remove TYPE_UNSIGNED
restriction.
            (number_of_iterations_cltz): Use range_on_edge to check
non-negativity of
            signed preheader input.
            (number_of_iterations_cltz_complement): Likewise.

    gcc/testsuite/ChangeLog:

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

    Signed-off-by: Shivam Gupta <shivam98.tkg@gmail.com>


More information about the Gcc-bugs mailing list