https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128904
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Kyrylo Tkachov <[email protected]>: https://gcc.gnu.org/g:0b62a155214b56665b31aa5b5eaa4d1d85de57df commit r17-6732-g0b62a155214b56665b31aa5b5eaa4d1d85de57df Author: Kyrylo Tkachov <[email protected]> Date: Tue Nov 24 03:14:30 2026 -0800 Make CLZ and CTZ defined for zero when executed unconditionally [PR128904] __builtin_ctzg (c, -1) is lowered to c != 0 ? __builtin_ctz (c) : -1. If-conversion executes the call unconditionally: _1 = __builtin_ctz (c_8); _2 = c_8 != 0; prephitmp_15 = _2 ? _1 : 1; The range of a one-argument CTZ call is [0, prec - 1] because the result is undefined for a zero argument. VRP then folds the select using that range, which is wrong for c == 0 and the test aborts at -O2. The same applies to a one-argument CLZ call, and to any other transformation that speculates such a call (phiopt, loop invariant motion). Extend gimple_needing_rewrite_undefined and rewrite_to_defined_unconditional, which already rewrite statements that need a defined result when executed unconditionally, to cover one-argument CLZ and CTZ calls on integral types. The rewrite is to .CLZ (x, val) or .CTZ (x, val), where val is the value the target defines for a zero argument, or the precision of the argument if the target defines none or the type has no scalar integer mode (large _BitInt). Vector calls, such as the AArch64 vclz intrinsics folded to a vector .CLZ, are left alone. If-conversion keeps the calls convertible and rewrites them in the predicated blocks, so the vectorizer still sees a CLZ or CTZ call. The two-argument call expands through the unary optab only if the target has an instruction for the mode. Otherwise expand_clz_ctz now uses the guarded expansion, which also handles widening and libcalls, instead of asserting that an insn exists. Bootstrapped adn tested on aarch64-none-linux-gnu. gcc/ChangeLog: PR tree-optimization/128904 * gimple-fold.cc: Include case-cfn-macros.h. (gimple_needing_rewrite_undefined): Return true for one-argument CLZ and CTZ calls with an integral argument. (rewrite_to_defined_unconditional): Rewrite such calls to the two-argument IFN_CLZ or IFN_CTZ with the value of the target for a zero argument, or the precision of the argument, as the result for zero. * internal-fn.cc (expand_clz_ctz): Use the unary optab expansion for a matching two-argument call only if the target has an insn for the mode. * tree-if-conv.cc (if_convertible_stmt_p): Set need_to_rewrite_undefined for calls needing a rewrite. (predicate_statements): Visit calls needing a rewrite. * tree-ssa-loop-im.cc (shift_needs_mask_p): Return false for statements that are not assignments. gcc/testsuite/ChangeLog: PR tree-optimization/128904 * gcc.dg/torture/pr128904.c: New test. * gcc.dg/torture/pr128904-2.c: New test. * gcc.dg/torture/pr128904-3.c: New test. * gcc.target/aarch64/simd/pr128904.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]>
