]> git.ipfire.org Git - thirdparty/gcc.git/commit
tree-optimization/92539 - missed optimization leads to bogus -Warray-bounds
authorRichard Biener <rguenther@suse.de>
Mon, 13 Jan 2025 12:24:06 +0000 (13:24 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Fri, 17 Jan 2025 07:38:51 +0000 (08:38 +0100)
commit44d21551362f9076617200595f49d4260d1f40a9
treefa7223819bd05e178c0a5446490027ead8f585e2
parentc6f9f53d6eb5a2568d231ffa56fbec3e2e4c626d
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.
gcc/testsuite/g++.dg/vect/pr87621.cc
gcc/testsuite/g++.dg/warn/Warray-bounds-pr92539.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr77975.c
gcc/testsuite/gcc.dg/tree-ssa/sccp-16.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vect/pr89440.c
gcc/tree-ssa-loop-ivcanon.cc
gcc/tree-ssa-loop-niter.cc