Hi! Since my r15-7223 the niter analysis can recognize one loop during bootstrap as being ctz like. The patch just turned @@ -2173,7 +2173,7 @@ PROC m2pim_NumberIO_BinToStr (CARDINAL x _T535_44 = &buf[i.40_2]{lb: 1 sz: 4}; _T536_45 = x_21 & 1; *_T535_44 = _T536_45; - _T537_47 = x_21 / 2; + _T537_47 = x_21 >> 1; x_48 = _T537_47; # DEBUG x => x_48 if (x_48 != 0) which is not a big deal for the number_of_iterations_cltz optimization, it recognizes both right shift by 1 and unsigned division by 2 (and similarly for clz left shift by 1 or multiplication by 2). But starting with forwprop1 that change also resulted in @@ -1875,9 +1875,9 @@ PROC m2pim_NumberIO_BinToStr (CARDINAL x i.40_2 = (INTEGER) _T530_34; _T536_45 = x_21 & 1; MEM <CARDINAL[1:64]> [(CARDINAL *)&buf][i.40_2]{lb: 1 sz: 4} = _T536_45; - _T537_47 = x_21 / 2; + _T537_47 = x_21 >> 1; # DEBUG x => _T537_47 - if (x_21 > 1) + if (_T537_47 != 0) goto <bb 3>; [INV] else goto <bb 8>; [INV] and apparently it is only the latter form that number_of_iterations_cltz pattern matches, not the former (after all, that was the exact reason for r15-7223). The problem is that build_cltz_expr assumes if IFN_C[LT]Z can't be used it can use the __builtin_c[lt]z{,l,ll} builtins, and while most of the FEs do create them, modula 2 does not.
The following patch just let us punt if the FE doesn't build those builtins. I've filed a PR against modula2 so that they add the builtins too. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2025-01-31 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/118689 PR modula2/115032 * tree-ssa-loop-niter.cc (build_cltz_expr): Return NULL_TREE if fn is NULL and use_ifn is false. --- gcc/tree-ssa-loop-niter.cc.jj 2025-01-17 11:29:34.080683133 +0100 +++ gcc/tree-ssa-loop-niter.cc 2025-01-30 14:51:57.528933620 +0100 @@ -2238,6 +2238,8 @@ build_cltz_expr (tree src, bool leading, build_int_cst (integer_type_node, prec)); } } + else if (fn == NULL_TREE) + return NULL_TREE; else if (prec == 2 * lli_prec) { tree src1 = fold_convert (long_long_unsigned_type_node, Jakub