]> git.ipfire.org Git - thirdparty/gcc.git/commit
Update loop estimate after header duplication
authorJan Hubicka <jh@suse.cz>
Sat, 22 Apr 2023 07:20:45 +0000 (09:20 +0200)
committerJan Hubicka <jh@suse.cz>
Sat, 22 Apr 2023 07:20:45 +0000 (09:20 +0200)
commitcda246f8b421ba855a9e5f9d7bfcd0fc49b7bd4b
treed5b45ff75c65468a25940bf3da1a14c6187ca96d
parent6d7f9ebfb827d238543ce81a096ee0ad369715ef
Update loop estimate after header duplication

Loop header copying implements partial loop peelng.  If all exits of the loop
are peeled (which is the common case) the number of iterations decreases by 1.
Without noting this, for loops iterating zero times, we end up re-peeling them
later in the loop peeling pass which is wasteful.

This patch commonizes the code for esitmate update and adds logic to detect
when all (likely) exits were peeled by loop-ch.

We are still wrong about update of estimate however: if the exits behave
randomly with given probability, loop peeling does not decrease expected
iteration counts, just decreases probability that loop will be executed.
In this case we thus incorrectly decrease any_estimate. Doing so however
at least help us to not peel or optimize hard the lop later.

If the loop iterates precisely the estimated nuner of iterations. the estimate
decreases, but we are wrong about decreasing the header frequncy.  We already
have logic that tries to prove that loop exit will not be taken in peeled out
iterations and it may make sense to special case this.

I also fixed problem where we had off by one error in iteration count updating.
It makes perfect sense to expect loop to have 0 iterations.  However if bounds
drops to negative, we lose info about the loop behaviour (since we have no
profile data reaching the loop body).

Bootstrapped/regtested x86_64-linux, comitted.
Honza

gcc/ChangeLog:

2023-04-22  Jan Hubicka  <hubicka@ucw.cz>
    Ondrej Kubanek  <kubanek0ondrej@gmail.com>

* cfgloopmanip.h (adjust_loop_info_after_peeling): Declare.
* tree-ssa-loop-ch.cc (ch_base::copy_headers): Fix updating of
loop profile and bounds after header duplication.
* tree-ssa-loop-ivcanon.cc (adjust_loop_info_after_peeling):
Break out from try_peel_loop; fix handling of 0 iterations.
(try_peel_loop): Use adjust_loop_info_after_peeling.

gcc/testsuite/ChangeLog:

2023-04-22  Jan Hubicka  <hubicka@ucw.cz>
    Ondrej Kubanek  <kubanek0ondrej@gmail.com>

* gcc.dg/tree-ssa/peel1.c: Decrease number of peels by 1.
* gcc.dg/unroll-8.c: Decrease loop iteration estimate.
* gcc.dg/tree-prof/peel-2.c: New test.
gcc/cfgloopmanip.h
gcc/testsuite/gcc.dg/tree-prof/peel-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/peel1.c
gcc/testsuite/gcc.dg/unroll-8.c
gcc/tree-ssa-loop-ch.cc
gcc/tree-ssa-loop-ivcanon.cc