https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127853
--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Vineet Gupta <[email protected]>: https://gcc.gnu.org/g:e2d1ca54a787da164e748075abfb35d5b789a8b6 commit r17-5112-ge2d1ca54a787da164e748075abfb35d5b789a8b6 Author: Vineet Gupta <[email protected]> Date: Tue Oct 13 18:15:49 2026 +0530 forwprop: Only pass the value at zero to .CLZ/.CTZ if the target defines it [PR127853] 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]>
