Simplify control-flow structure generated when GCC vectorizes an
uncounted loop with multiple exits.
Given that in the uncounted vectorization scheme under consideration,
there is no meaningful distinction between the main exit and the
alternative early-break exits in terms of how execution resumes in the
scalar epilogue, keeping distinct exit blocks for main and early break
exits adds unnecessary complexity.
This patch therefore modifies the way
`slpeel_tree-duplicate_loop_to_edge_cfg' connects the loop under
vectorization to its epilogue copy, such that it no longer generates
separate exit paths and merge blocks for the main and early-break
exits, rather arranging the CFG such that all exits reach a common
destination.
This works given that in this context, every exit from the vector loop
can continue through the same scalar-replay entry, using values
representing the start of the iteration being replayed.
This also addresses a problem exposed by the subsequent work on
masking for alignment in uncounted loops.
When masking off elements preceding the first valid scalar element,
the vector-loop induction can start at a negative logical offset.
The scalar epilogue must therefore clamp the restart index to zero:
restart_index = MAX (iteration_start, 0)
Here, iteration_start is the logical index of the first element of
the vector iteration in which an exit was encountered.
With separate exit destinations, the adjustment is inserted into the
early-break exit block even though its result is also needed along the
main-exit path. Since the early-break exit block does not dominate the
main-exit path, this produces an SSA definition that does not
dominate all its uses.
Thus, the present schematic:
| loop_exit | alt1 | altN
v v ... v
main_loop_exit_block: alt_loop_exit_block:
| /
| |
| |-----------------
v v
new_preheader:
is changed in uncounted loops to:
| loop_exit | alt1 | altN
v v ... v
common_loop_exit_block:
|
v
new_preheader:
Whether this transformation is carried out is governed by the new
predicate we define:
bool merge_uncounted_exits_p = at_exit && flow_loops && uncounted_p
&& redirect_multiple_exits_p;
The conditions restrict the change to uncounted loops with multiple
exits, when exit redirection is requested during the relevant
epilogue-duplication path. Prologue duplication and other
loop-duplication scenarios retain their existing behaviour.
gcc/ChangeLog:
* tree-vect-loop-manip.cc
(slpeel_tree_duplicate_loop_to_edge_cfg): Redirect all exits
of uncounted loops to a common exit block.
* tree-vect-loop.cc (vect_create_epilog_for_reduction):
Handle multiple incoming edges to the shared exit block
when constructing epilogue PHI arguments.
---
gcc/tree-vect-loop-manip.cc | 189 ++++++++++++++++++++++++++++--------
gcc/tree-vect-loop.cc | 15 ++-
2 files changed, 161 insertions(+), 43 deletions(-)
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 886f27b5fa1..07a75400174 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -1650,6 +1650,13 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop
*loop, edge loop_exit,
/* If REDIRECT_EXITS is false we leave the alternative exits untouched and
treat the duplication as if the loop only had the main exit. */
bool redirect_multiple_exits_p = redirect_exits && has_multiple_exits_p;
+
+ /* If dealing with uncounted loops, no semantic difference between main and
+ early-break exits. Avoid extra complication of directing them to distinct
+ exit blocks. */
+ bool merge_uncounted_exits_p = uncounted_p && has_multiple_exits_p
+ && flow_loops && at_exit;
+
auto_vec<basic_block> doms;
if (at_exit) /* Add the loop copy at exit. */
@@ -1662,6 +1669,12 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop
*loop, edge loop_exit,
bool need_virtual_phi = get_virtual_phi (loop->header);
+ tree main_exit_vphi_arg = NULL_TREE;
+
+ if (merge_uncounted_exits_p)
+ if (gphi *vphi = get_virtual_phi (loop_exit->dest))
+ main_exit_vphi_arg = PHI_ARG_DEF_FROM_EDGE (vphi, loop_exit);
+
/* For the main loop exit preserve the LC PHI nodes. For vectorization
we need them to continue or finalize reductions. Since we do not
copy the loop exit blocks we have to materialize PHIs at the
@@ -1670,12 +1683,36 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop
*loop, edge loop_exit,
!gsi_end_p (gsi_from); gsi_next (&gsi_from))
{
tree res = gimple_phi_result (*gsi_from);
+ /* For uncounted loops, all scalar continuation values are
+ reconstructed later, but we still need the existing virtual exit
+ PHIs, so we skip everything other than VPHIs. */
+ if (merge_uncounted_exits_p && !virtual_operand_p (res))
+ continue;
+
create_phi_node (copy_ssa_name (res), new_preheader);
}
+
edge e = redirect_edge_and_branch (loop_exit, new_preheader);
gcc_assert (e == loop_exit);
- flush_pending_stmts (loop_exit);
- set_immediate_dominator (CDI_DOMINATORS, new_preheader, loop_exit->src);
+
+ if (merge_uncounted_exits_p)
+ {
+ /* All incoming edges represent early break-type exits, so all PHI
+ node arguments will be reconstructed from loop-header PHIs rather
+ than copied from the main exit destination. */
+ redirect_edge_var_map_clear (loop_exit);
+ if (gphi *vphi = get_virtual_phi (new_preheader))
+ {
+ gcc_checking_assert (main_exit_vphi_arg != NULL_TREE);
+ add_phi_arg (vphi, main_exit_vphi_arg, loop_exit,
+ UNKNOWN_LOCATION);
+ }
+ }
+ else
+ flush_pending_stmts (loop_exit);
+
+ set_immediate_dominator (CDI_DOMINATORS, new_preheader,
+ loop_exit->src);
/* If we ended up choosing an exit leading to a path not using memory
we can end up without a virtual LC PHI. Create it when it is
@@ -1691,61 +1728,119 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop
*loop, edge loop_exit,
basic_block main_loop_exit_block = new_preheader;
basic_block alt_loop_exit_block = new_preheader;
- /* When we redirect the other exits create the CFG
- below to funnel everything through the merge block:
+ /* When we redirect the other exits, two scenarios are possible:
+ 1. For counted loops, create the CFG below to funnel everything
+ through the merge block:
+
| loop_exit | alt1 | altN
v v ... v
main_loop_exit_block: alt_loop_exit_block:
| /
v v
new_preheader:
- where in the new preheader we need merge PHIs for
- the continuation values into the epilogue header.
- Do not bother with exit PHIs for the early exits but
- their live virtual operand. We'll fix up things below. */
+
+ where in the new preheader we need merge PHIs for the continuation
+ values into the epilogue header.
+ Do not bother with exit PHIs for the early exits but their live
+ virtual operand. We'll fix up things below.
+
+ 2. For uncounted loops, the "main" exit behaves just as other
+ early-break exits. Enforcing any artificial distinction has the
+ potential to complicate things later on, so all exits are directed to
+ the main_lop_exit_block:
+
+ | loop_exit | alt1 | altN
+ v v ... v
+ common_loop_exit_block:
+ |
+ v
+ new_preheader:
+
+ For this case, do not bother with exit PHIs for any exits but their
+ live virtual operand. As in the previous case, We'll fix up things
+ below. */
if (redirect_multiple_exits_p || uncounted_p)
{
edge loop_e = single_succ_edge (new_preheader);
new_preheader = split_edge (loop_e);
- if (redirect_exits)
- {
- gphi *vphi = NULL;
- alt_loop_exit_block = new_preheader;
- for (auto exit : loop_exits)
- if (exit != loop_exit)
- {
- tree vphi_def = NULL_TREE;
- if (gphi *evphi = get_virtual_phi (exit->dest))
- vphi_def = gimple_phi_arg_def_from_edge (evphi, exit);
- edge res
- = redirect_edge_and_branch (exit, alt_loop_exit_block);
- gcc_assert (res == exit);
- redirect_edge_var_map_clear (exit);
+ if (redirect_exits)
+ {
+ gphi *vphi = merge_uncounted_exits_p
+ ? get_virtual_phi (main_loop_exit_block)
+ : NULL;
- if (alt_loop_exit_block == new_preheader)
- alt_loop_exit_block = split_edge (exit);
- if (!need_virtual_phi)
- continue;
+ /* When merging uncounted exits, the alternative loop exits are
+ redirected to the main exit, so under merging they become
+ one and the same. */
+ alt_loop_exit_block = merge_uncounted_exits_p
+ ? main_loop_exit_block
+ : new_preheader;
- /* When the edge has no virtual LC PHI get at the live
- virtual operand by other means. */
- if (!vphi_def)
- vphi_def = get_live_virtual_operand_on_edge (exit);
+ for (auto exit : loop_exits)
+ if (exit != loop_exit)
+ {
+ /* For the non-merged case, NEED_VIRTUAL_PHI means we need
+ to create a VPHI node, for the merging case, we might
+ have already created one, in which case it needs args
+ for all incoming edges. */
+ bool need_exit_vphi
+ = merge_uncounted_exits_p
+ ? get_virtual_phi (exit_dest) != NULL
+ : need_virtual_phi;
- if (!vphi)
- vphi = create_phi_node (copy_ssa_name (vphi_def),
- alt_loop_exit_block);
- else
- /* Edge redirection might re-allocate the PHI node
- so we have to rediscover it. */
- vphi = get_virtual_phi (alt_loop_exit_block);
- add_phi_arg (vphi, vphi_def, exit, UNKNOWN_LOCATION);
- }
- }
+ tree vphi_def = NULL_TREE;
+
+ if (need_exit_vphi)
+ {
+ if (gphi *evphi = get_virtual_phi (exit->dest))
+ vphi_def = gimple_phi_arg_def_from_edge (evphi, exit);
+ /* When the edge has no virtual LC PHI get at the live
+ virtual operand by other means. */
+ if (!vphi_def)
+ vphi_def = get_live_virtual_operand_on_edge (exit);
+ }
+
+ edge res
+ = redirect_edge_and_branch (exit, alt_loop_exit_block);
+
+ gcc_assert (res == exit);
+ redirect_edge_var_map_clear (exit);
+
+ /* This is run once when not merging uncounted exits. */
+ if (alt_loop_exit_block == new_preheader)
+ alt_loop_exit_block = split_edge (exit);
+
+
+ if (!need_exit_vphi)
+ continue;
+
+ if (!vphi)
+ {
+ gcc_checking_assert (!merge_uncounted_exits_p);
+ vphi = create_phi_node (copy_ssa_name (vphi_def),
+ alt_loop_exit_block);
+ }
+ else
+ /* Edge redirection might re-allocate the PHI node
+ so we have to rediscover it. */
+ vphi = get_virtual_phi (alt_loop_exit_block);
+ add_phi_arg (vphi, vphi_def, exit, UNKNOWN_LOCATION);
+
+ }
+ }
+
+ if (merge_uncounted_exits_p)
+ {
+ set_immediate_dominator (CDI_DOMINATORS, main_loop_exit_block,
+ loop->header);
set_immediate_dominator (CDI_DOMINATORS, new_preheader,
- loop->header);
+ main_loop_exit_block);
+ main_loop_exit_block->count = preheader->count;
+ }
+ else
+ set_immediate_dominator (CDI_DOMINATORS, new_preheader, loop->header);
/* Fix up the profile counts of the new exit blocks.
main_loop_exit_block was created by duplicating the
@@ -1834,7 +1929,17 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop
*loop, edge loop_exit,
tree new_def = copy_ssa_name (new_arg);
gphi *lc_phi
= create_phi_node (new_def, main_loop_exit_block);
- SET_PHI_ARG_DEF (lc_phi, 0, new_arg);
+ if (merge_uncounted_exits_p)
+ {
+ /* All loop exits point to the same exit block which
+ contains LC_PHI, so it can be reused for every
+ iteration. PEELED_ITERS ensures all exits will
+ provide args taken from GIMPLE_PHI_RESULT. */
+ for (edge exit: loop_exits)
+ SET_PHI_ARG_DEF_ON_EDGE (lc_phi, exit, new_arg);
+ }
+ else
+ SET_PHI_ARG_DEF (lc_phi, 0, new_arg);
new_arg = new_def;
}
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 26563e61dc1..779318a7b99 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -5631,7 +5631,20 @@ vect_create_epilog_for_reduction (loop_vec_info
loop_vinfo,
tree new_def = copy_ssa_name (def);
phi = create_phi_node (new_def, exit_bb);
if (LOOP_VINFO_MAIN_EXIT (loop_vinfo) == loop_exit)
- SET_PHI_ARG_DEF (phi, loop_exit->dest_idx, def);
+ {
+ if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
+ && EDGE_COUNT (exit_bb->preds) > 1)
+ {
+ /* In uncounted loops with multiple exits, loop_exit->dest will
+ contain several incoming edges. Preserve reduction state
+ throughout. */
+ for (edge exit : get_loop_exit_edges (loop))
+ if (exit->dest == exit_bb)
+ SET_PHI_ARG_DEF (phi, exit->dest_idx, def);
+ }
+ else
+ SET_PHI_ARG_DEF (phi, loop_exit->dest_idx, def);
+ }
else
{
for (unsigned k = 0; k < gimple_phi_num_args (phi); k++)
--
2.43.0