https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92539
--- Comment #14 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Richard Biener <rgue...@gcc.gnu.org>: https://gcc.gnu.org/g:44d21551362f9076617200595f49d4260d1f40a9 commit r15-6990-g44d21551362f9076617200595f49d4260d1f40a9 Author: Richard Biener <rguent...@suse.de> Date: Mon Jan 13 13:24:06 2025 +0100 tree-optimization/92539 - missed optimization leads to bogus -Warray-bounds The following makes niter analysis recognize a loop with an exit condition scanning over a STRING_CST. This is done via enhancing the force evaluation code rather than recognizing for example strlen (s) as number of iterations because it allows to handle some more cases. STRING_CSTs are easy to handle since nothing can write to them, also processing those should be cheap. I've refrained from handling anything besides char8_t. Note to avoid the -Warray-bound dianostic we have to either early unroll the loop (there's no final value replacement done, there's a PR for doing this as part of CD-DCE when possibly eliding a loop), or create a canonical IV so we can DCE the loads. The latter is what the patch does, also avoiding to repeatedly force-evaluate niters. This also makes final value replacement work again since now ivcanon is after it. There are some testsuite adjustments needed, in particular we now unroll some loops early, causing messages to appear in different passes but also vectorization to now no longer happening on outer loops. The changes mitigate that. PR tree-optimization/92539 * tree-ssa-loop-ivcanon.cc (tree_unroll_loops_completely_1): Also try force-evaluation if ivcanon did not yet run. (canonicalize_loop_induction_variables): When niter was computed constant by force evaluation add a canonical IV if we didn't unroll. * tree-ssa-loop-niter.cc (loop_niter_by_eval): When we don't find a proper PHI try if the exit condition scans over a STRING_CST and simulate that. * g++.dg/warn/Warray-bounds-pr92539.C: New testcase. * gcc.dg/tree-ssa/sccp-16.c: New testcase. * g++.dg/vect/pr87621.cc: Use larger power to avoid inner loop unrolling. * gcc.dg/vect/pr89440.c: Use larger loop bound to avoid inner loop unrolling. * gcc.dg/pr77975.c: Scan cunrolli dump and adjust.