Don't move cold code out of loop by checking bb count
authorXionghu Luo <luoxhu@linux.ibm.com>
Wed, 8 Dec 2021 01:24:35 +0000 (19:24 -0600)
committerXionghu Luo <luoxhu@linux.ibm.com>
Tue, 21 Dec 2021 03:12:50 +0000 (21:12 -0600)
commit51a24e4a984536371eaf146a922124af4a6982da
tree6fc2b374b66619b3c50106ed2c6fe4c486f7a57c
parentcd5ae148c47c6dee05adb19acd6a523f7187be7f
Don't move cold code out of loop by checking bb count

v8 changes:
1. Use hotter_than_inner_loop instead of colder to store a hotter loop
nearest to loop.
2. Update the logic in fill_coldest_and_hotter_out_loop and
get_coldest_out_loop to make common case O(1).
3. Update function argument bb_colder_than_loop_preheader.
4. Make cached array to vec<class *loop> for index checking.

v7 changes:
1. Refine get_coldest_out_loop to replace loop with checking
pre-computed coldest_outermost_loop and colder_than_inner_loop.
2. Add function fill_cold_out_loop, compute coldest_outermost_loop and
colder_than_inner_loop recursively without loop.

v6 changes:
1. Add function fill_coldest_out_loop to pre compute the coldest
outermost loop for each loop.
2. Rename find_coldest_out_loop to get_coldest_out_loop.
3. Add testcase ssa-lim-22.c to differentiate with ssa-lim-19.c.

v5 changes:
1. Refine comments for new functions.
2. Use basic_block instead of count in bb_colder_than_loop_preheader
to align with function name.
3. Refine with simpler implementation for get_coldest_out_loop and
ref_in_loop_hot_body::operator for better understanding.

v4 changes:
1. Sort out profile_count comparision to function bb_cold_than_loop_preheader.
2. Update ref_in_loop_hot_body::operator () to find cold_loop before compare.
3. Split RTL invariant motion part out.
4. Remove aux changes.

v3 changes:
1. Handle max_loop in determine_max_movement instead of outermost_invariant_loop.
2. Remove unnecessary changes.
3. Add for_all_locs_in_loop (loop, ref, ref_in_loop_hot_body) in can_sm_ref_p.
4. "gsi_next (&bsi);" in move_computations_worker is kept since it caused
infinite loop when implementing v1 and the iteration is missed to be
updated actually.

v1: https://gcc.gnu.org/pipermail/gcc-patches/2021-August/576488.html
v2: https://gcc.gnu.org/pipermail/gcc-patches/2021-September/579086.html
v3: https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580211.html
v4: https://gcc.gnu.org/pipermail/gcc-patches/2021-October/581231.html
v5: https://gcc.gnu.org/pipermail/gcc-patches/2021-October/581961.html
...
v8: https://gcc.gnu.org/pipermail/gcc-patches/2021-December/586209.html

There was a patch trying to avoid move cold block out of loop:

https://gcc.gnu.org/pipermail/gcc/2014-November/215551.html

Richard suggested to "never hoist anything from a bb with lower execution
frequency to a bb with higher one in LIM invariantness_dom_walker
before_dom_children".

In gimple LIM analysis, add get_coldest_out_loop to move invariants to
expected target loop, if profile count of the loop bb is colder
than target loop preheader, it won't be hoisted out of loop.
Likely for store motion, if all locations of the REF in loop is cold,
don't do store motion of it.

SPEC2017 performance evaluation shows 1% performance improvement for
intrate GEOMEAN and no obvious regression for others.  Especially,
500.perlbench_r +7.52% (Perf shows function S_regtry of perlbench is
largely improved.), and 548.exchange2_r+1.98%, 526.blender_r +1.00%
on P8LE.

gcc/ChangeLog:

2021-12-21  Xionghu Luo  <luoxhu@linux.ibm.com>

* tree-ssa-loop-im.c (bb_colder_than_loop_preheader): New
function.
(get_coldest_out_loop): New function.
(determine_max_movement): Use get_coldest_out_loop.
(move_computations_worker): Adjust and fix iteration udpate.
(class ref_in_loop_hot_body): New functor.
(ref_in_loop_hot_body::operator): New.
(can_sm_ref_p): Use for_all_locs_in_loop.
(fill_coldest_and_hotter_out_loop): New.
(tree_ssa_lim_finalize): Free coldest_outermost_loop and
hotter_than_inner_loop.
(loop_invariant_motion_in_fun): Call fill_coldest_and_hotter_out_loop.

gcc/testsuite/ChangeLog:

2021-12-21  Xionghu Luo  <luoxhu@linux.ibm.com>

* gcc.dg/tree-ssa/recip-3.c: Adjust.
* gcc.dg/tree-ssa/ssa-lim-19.c: New test.
* gcc.dg/tree-ssa/ssa-lim-20.c: New test.
* gcc.dg/tree-ssa/ssa-lim-21.c: New test.
* gcc.dg/tree-ssa/ssa-lim-22.c: New test.
* gcc.dg/tree-ssa/ssa-lim-23.c: New test.
gcc/testsuite/gcc.dg/tree-ssa/recip-3.c
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-19.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-20.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-21.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-22.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-23.c [new file with mode: 0644]
gcc/tree-ssa-loop-im.c