On 10/13/26 2:23 PM, Jakub Jelinek wrote:
On Tue, Oct 13, 2026 at 08:33:38AM +0530, Vineet Gupta wrote:
On x86 without BMI, fwprop (simplify_count_zeroes) can build a .CTZ,
claiming ctx(0) = 32, although target specifies ctz(0) is undefined
and the instruction it emits leaves zero undefined (or destination
unchanged on AMD). A later match.pd (cond (ne x 0) (IFN_C?Z x C) C)
can fold that 32 with user's "x ? clz (x) : 32" giving undefined
result for the 0 input.

Follow the convention documented in fold_builtin_bit_query and only pass
the second argument when the macro returns 2; the existing COND_EXPR
already supplies the table's value for zero.

Bootstrapped and regression tested on x86_64-linux-gnu and aarch64-linux-gnu
(Full disclosure: the testing was with combined fixes for 3 other PRs).

        PR tree-optimization/127853

gcc/ChangeLog:

        * tree-ssa-forwprop.cc (simplify_count_zeroes): Only pass the
        value at zero to .CLZ/.CTZ when the target defines it.

gcc/testsuite/ChangeLog:

        * gcc.dg/torture/clz-table-zero-1.c: New test.
        * gcc.dg/torture/ctz-table-zero-1.c: New test.
        * gcc.target/aarch64/clz-ctz-table-zero.c: New test.
        * gcc.target/i386/clz-ctz-table-nonzero.c: New test.
        * gcc.target/i386/clz-ctz-table-zero.c: New test.

Signed-off-by: Vineet Gupta <[email protected]>
LGTM.

        Jakub

Commited !

Thx,
-Vineet

Reply via email to