`vect_set_loop_controls_directly' currently assumes that the
number of scalar iterations is available when generating
partial-vector controls. This is not the case for uncounted
loops, whose termination is determined by early-break exits.
Extend the function to handle uncounted loops separately from
counted loops. Avoid generating iteration-count-based limits
and induction variables for uncounted loops, while preserving
the existing control generation for counted loops.
For uncounted loops, initialize the controls to their full
values, except where alignment requires elements to be skipped
during the first iteration. Use full masks or maximum lengths
for subsequent iterations, leaving the vectorized early-break
conditions responsible for terminating the loop.
Also assert that uncounted loops do not use decrementing
induction variables, since these require a known iteration
count.
gcc/ChangeLog:
* tree-vect-loop-manip.cc (vect_set_loop_controls_directly):
Handle uncounted loops without generating iteration-count-based
induction variables or limits. Generate full controls for
subsequent iterations and preserve initial alignment masking.
---
gcc/tree-vect-loop-manip.cc | 444 +++++++++++++++++++++---------------
1 file changed, 257 insertions(+), 187 deletions(-)
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index f5af874a210..69b1b962e67 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -558,6 +558,11 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
tree compare_type = LOOP_VINFO_RGROUP_COMPARE_TYPE (loop_vinfo);
tree iv_type = LOOP_VINFO_RGROUP_IV_TYPE (loop_vinfo);
bool use_masks_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
+ bool uncounted_p = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo);
+
+ /* Uncounted loops cannot use decrementing induction variables. */
+ gcc_assert (!uncounted_p
+ || !LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo));
tree ctrl_type = rgc->type;
unsigned int nitems_per_iter = rgc->max_nscalars_per_iter * rgc->factor;
@@ -572,191 +577,222 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
of the vector loop, and the number that it should skip during the
first iteration of the vector loop. */
tree nitems_total = niters;
- tree nitems_vf
- = build_int_cst (iv_type, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
- tree nitems_step
- = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
- ? gimple_convert (preheader_seq, iv_type,
- LOOP_VINFO_IV_INCREMENT (loop_vinfo))
- : gimple_convert (&loop_cond_gsi, true, GSI_SAME_STMT, UNKNOWN_LOCATION,
- iv_type, LOOP_VINFO_IV_INCREMENT (loop_vinfo));
-
+ tree nitems_vf = NULL_TREE;
+ tree nitems_step = NULL_TREE;
tree nitems_skip = niters_skip;
+
+ if (!uncounted_p)
+ {
+ nitems_vf
+ = build_int_cst (iv_type, LOOP_VINFO_VECT_FACTOR (loop_vinfo));
+ nitems_step
+ = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
+ ? gimple_convert (preheader_seq, iv_type,
+ LOOP_VINFO_IV_INCREMENT (loop_vinfo))
+ : gimple_convert (&loop_cond_gsi, true, GSI_SAME_STMT,
+ UNKNOWN_LOCATION, iv_type,
+ LOOP_VINFO_IV_INCREMENT (loop_vinfo));
+ }
+
if (nitems_per_iter != 1)
{
- /* We checked before setting LOOP_VINFO_USING_PARTIAL_VECTORS_P that
- these multiplications don't overflow. */
tree compare_factor = build_int_cst (compare_type, nitems_per_iter);
- tree iv_factor = build_int_cst (iv_type, nitems_per_iter);
- nitems_total = gimple_build (preheader_seq, MULT_EXPR, compare_type,
- nitems_total, compare_factor);
- nitems_vf = gimple_build (preheader_seq, MULT_EXPR, iv_type,
- nitems_vf, iv_factor);
- nitems_step = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
- ? gimple_build (preheader_seq, MULT_EXPR, iv_type,
- nitems_step, iv_factor)
- : gimple_build (&loop_cond_gsi, true, GSI_SAME_STMT,
- UNKNOWN_LOCATION, MULT_EXPR, iv_type,
- nitems_step, iv_factor);
+ if (!uncounted_p)
+ {
+ /* We checked before setting LOOP_VINFO_USING_PARTIAL_VECTORS_P that
+ these multiplications don't overflow. */
+ tree iv_factor = build_int_cst (iv_type, nitems_per_iter);
+ nitems_total = gimple_build (preheader_seq, MULT_EXPR, compare_type,
+ nitems_total, compare_factor);
+
+ nitems_vf = gimple_build (preheader_seq, MULT_EXPR, iv_type,
+ nitems_vf, iv_factor);
+
+ nitems_step = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
+ ? gimple_build (preheader_seq, MULT_EXPR, iv_type,
+ nitems_step, iv_factor)
+ : gimple_build (&loop_cond_gsi, true, GSI_SAME_STMT,
+ UNKNOWN_LOCATION, MULT_EXPR, iv_type,
+ nitems_step, iv_factor);
+ }
+
+ /* The number of skipped items must be scaled for both counted
+ and uncounted loops. */
if (nitems_skip)
nitems_skip = gimple_build (preheader_seq, MULT_EXPR, compare_type,
nitems_skip, compare_factor);
}
- /* Create an induction variable that counts the number of items
- processed. */
- tree index_before_incr, index_after_incr;
+ /* These values are only meaningful for counted loops. */
+ tree test_index = NULL_TREE;
+ tree test_limit = NULL_TREE;
+ tree first_limit = NULL_TREE;
+
gimple_stmt_iterator incr_gsi;
bool insert_after;
+
standard_iv_increment_position (loop, &incr_gsi, &insert_after);
- if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo))
+ if (!uncounted_p)
{
- /* Create an IV that counts down from niters_total and whose step
- is the (variable) amount processed in the current iteration:
- ...
- _10 = (unsigned long) count_12(D);
- ...
- # ivtmp_9 = PHI <ivtmp_35(6), _10(5)>
- _36 = (MIN_EXPR | SELECT_VL) <ivtmp_9, POLY_INT_CST [4, 4]>;
- ...
- vect__4.8_28 = .LEN_LOAD (_17, 32B, _36, 0);
- ...
- ivtmp_35 = ivtmp_9 - POLY_INT_CST [4, 4];
- ...
- if (ivtmp_9 > POLY_INT_CST [4, 4])
- goto <bb 4>; [83.33%]
- else
- goto <bb 5>; [16.67%]
- */
- nitems_total = gimple_convert (preheader_seq, iv_type, nitems_total);
- tree step = rgc->controls.length () == 1 ? rgc->controls[0]
- : make_ssa_name (iv_type);
- /* Create decrement IV. */
- if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
+ /* Create an induction variable that counts the number of items
+ processed. */
+ tree index_before_incr, index_after_incr;
+
+ if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo))
{
- create_iv (nitems_total, MINUS_EXPR, step, NULL_TREE, loop, &incr_gsi,
- insert_after, &index_before_incr, &index_after_incr);
- tree vectype = build_zero_cst (rgc->type);
- tree len = gimple_build (header_seq, IFN_SELECT_VL, iv_type,
- index_before_incr, nitems_vf,
- vectype);
- gimple_seq_add_stmt (header_seq, gimple_build_assign (step, len));
- len = gimple_convert (header_seq, sizetype, len);
+ /* Create an IV that counts down from niters_total and whose step
+ is the (variable) amount processed in the current iteration:
+ ...
+ _10 = (unsigned long) count_12(D);
+ ...
+ # ivtmp_9 = PHI <ivtmp_35(6), _10(5)>
+ _36 = (MIN_EXPR | SELECT_VL) <ivtmp_9, POLY_INT_CST [4, 4]>;
+ ...
+ vect__4.8_28 = .LEN_LOAD (_17, 32B, _36, 0);
+ ...
+ ivtmp_35 = ivtmp_9 - POLY_INT_CST [4, 4];
+ ...
+ if (ivtmp_9 > POLY_INT_CST [4, 4])
+ goto <bb 4>; [83.33%]
+ else
+ goto <bb 5>; [16.67%]
+ */
+ nitems_total = gimple_convert (preheader_seq, iv_type, nitems_total);
- /* Remove the previous initialization of IV_INCREMENT to VARYING. */
- gimple *varying_def
- = SSA_NAME_DEF_STMT (LOOP_VINFO_IV_INCREMENT (loop_vinfo));
- auto def_gsi = gsi_for_stmt (varying_def);
- gsi_remove (&def_gsi, true);
+ tree step = rgc->controls.length () == 1 ? rgc->controls[0]
+ : make_ssa_name (iv_type);
- /* Set the LOOP_VINFO_IV_INCREMENT to be len. */
- gassign* assign_iv_increment
- = gimple_build_assign (LOOP_VINFO_IV_INCREMENT (loop_vinfo), len);
- gimple_seq_add_stmt (header_seq, assign_iv_increment);
+ /* Create decrement IV. */
+ if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
+ {
+ create_iv (nitems_total, MINUS_EXPR, step, NULL_TREE, loop,
+ &incr_gsi, insert_after, &index_before_incr,
+ &index_after_incr);
+
+ tree vectype = build_zero_cst (rgc->type);
+ tree len = gimple_build (header_seq, IFN_SELECT_VL, iv_type,
+ index_before_incr, nitems_vf,
+ vectype);
+ gimple_seq_add_stmt (header_seq,
+ gimple_build_assign (step, len));
+ len = gimple_convert (header_seq, sizetype, len);
+
+ /* Remove the previous initialization of IV_INCREMENT to
+ VARYING. */
+ gimple *varying_def
+ = SSA_NAME_DEF_STMT (LOOP_VINFO_IV_INCREMENT (loop_vinfo));
+ auto def_gsi = gsi_for_stmt (varying_def);
+ gsi_remove (&def_gsi, true);
+
+ /* Set the LOOP_VINFO_IV_INCREMENT to be len. */
+ gassign* assign_iv_increment
+ = gimple_build_assign (LOOP_VINFO_IV_INCREMENT (loop_vinfo),
+ len);
+ gimple_seq_add_stmt (header_seq, assign_iv_increment);
+ }
+ else
+ {
+ create_iv (nitems_total, MINUS_EXPR, nitems_step, NULL_TREE, loop,
+ &incr_gsi, insert_after, &index_before_incr,
+ &index_after_incr);
+ gimple_seq_add_stmt (header_seq,
+ gimple_build_assign (step, MIN_EXPR,
+ index_before_incr,
+ nitems_step));
+ }
+ *iv_step = step;
+ *compare_step = nitems_vf;
+ return LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo) ? index_after_incr
+ : index_before_incr;
+ }
+
+ /* Create increment IV. */
+ create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
+ loop, &incr_gsi, insert_after, &index_before_incr,
+ &index_after_incr,
+ LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
+
+ if (might_wrap_p)
+ {
+ /* In principle the loop should stop iterating once the incremented
+ IV reaches a value greater than or equal to:
+
+ NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP
+
+ However, there's no guarantee that this addition doesn't overflow
+ the comparison type, or that the IV hits a value above it before
+ wrapping around. We therefore adjust the limit down by one
+ IV step:
+
+ (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
+ -[infinite-prec] NITEMS_STEP
+
+ and compare the IV against this limit _before_ incrementing it.
+ Since the comparison type is unsigned, we actually want the
+ subtraction to saturate at zero:
+
+ (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
+ -[sat] NITEMS_STEP
+
+ And since NITEMS_SKIP < NITEMS_STEP, we can reassociate this as:
+
+ NITEMS_TOTAL -[sat] (NITEMS_STEP - NITEMS_SKIP)
+
+ where the rightmost subtraction can be done directly in
+ COMPARE_TYPE. */
+ test_index = index_before_incr;
+ tree adjust = gimple_convert (preheader_seq, compare_type,
+ nitems_vf);
+ if (nitems_skip)
+ adjust = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
+ adjust, nitems_skip);
+ test_limit = gimple_build (preheader_seq, MAX_EXPR, compare_type,
+ nitems_total, adjust);
+ test_limit = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
+ test_limit, adjust);
+
+ /* Get a safe limit for the first iteration. */
+ if (nitems_skip)
+ {
+ /* The first vector iteration can handle at most NITEMS_STEP
+ items. NITEMS_STEP <= CONST_LIMIT, and adding
+ NITEMS_SKIP to that cannot overflow. */
+ tree const_limit = build_int_cst (compare_type,
+ LOOP_VINFO_VECT_FACTOR
+ (loop_vinfo) * nitems_per_iter);
+ first_limit = gimple_build (preheader_seq, MIN_EXPR, compare_type,
+ nitems_total, const_limit);
+ first_limit = gimple_build (preheader_seq, PLUS_EXPR,
+ compare_type, first_limit,
+ nitems_skip);
+ }
+ else
+ /* For the first iteration it doesn't matter whether the IV hits
+ a value above NITEMS_TOTAL. That only matters for the latch
+ condition. */
+ first_limit = nitems_total;
}
else
{
- create_iv (nitems_total, MINUS_EXPR, nitems_step, NULL_TREE, loop,
- &incr_gsi, insert_after, &index_before_incr,
- &index_after_incr);
- gimple_seq_add_stmt (header_seq,
- gimple_build_assign (step, MIN_EXPR,
- index_before_incr,
- nitems_step));
+ /* Test the incremented IV, which will always hit a value above
+ the bound before wrapping. */
+ test_index = index_after_incr;
+ test_limit = nitems_total;
+ if (nitems_skip)
+ test_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
+ test_limit, nitems_skip);
+
+ first_limit = test_limit;
}
- *iv_step = step;
- *compare_step = nitems_vf;
- return LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo) ? index_after_incr
- : index_before_incr;
+
+ /* Convert the IV value to the comparison type (either a no-op or
+ a demotion). */
+ gimple_seq test_seq = NULL;
+ test_index = gimple_convert (&test_seq, compare_type, test_index);
+ gsi_insert_seq_after (&incr_gsi, test_seq, GSI_NEW_STMT);
}
- /* Create increment IV. */
- create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
- loop, &incr_gsi, insert_after, &index_before_incr,
- &index_after_incr,
- LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
-
- tree zero_index = build_int_cst (compare_type, 0);
- tree test_index, test_limit, first_limit;
- if (might_wrap_p)
- {
- /* In principle the loop should stop iterating once the incremented
- IV reaches a value greater than or equal to:
-
- NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP
-
- However, there's no guarantee that this addition doesn't overflow
- the comparison type, or that the IV hits a value above it before
- wrapping around. We therefore adjust the limit down by one
- IV step:
-
- (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
- -[infinite-prec] NITEMS_STEP
-
- and compare the IV against this limit _before_ incrementing it.
- Since the comparison type is unsigned, we actually want the
- subtraction to saturate at zero:
-
- (NITEMS_TOTAL +[infinite-prec] NITEMS_SKIP)
- -[sat] NITEMS_STEP
-
- And since NITEMS_SKIP < NITEMS_STEP, we can reassociate this as:
-
- NITEMS_TOTAL -[sat] (NITEMS_STEP - NITEMS_SKIP)
-
- where the rightmost subtraction can be done directly in
- COMPARE_TYPE. */
- test_index = index_before_incr;
- tree adjust = gimple_convert (preheader_seq, compare_type,
- nitems_vf);
- if (nitems_skip)
- adjust = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
- adjust, nitems_skip);
- test_limit = gimple_build (preheader_seq, MAX_EXPR, compare_type,
- nitems_total, adjust);
- test_limit = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
- test_limit, adjust);
-
- /* Get a safe limit for the first iteration. */
- if (nitems_skip)
- {
- /* The first vector iteration can handle at most NITEMS_STEP
- items. NITEMS_STEP <= CONST_LIMIT, and adding
- NITEMS_SKIP to that cannot overflow. */
- tree const_limit = build_int_cst (compare_type,
- LOOP_VINFO_VECT_FACTOR (loop_vinfo)
- * nitems_per_iter);
- first_limit = gimple_build (preheader_seq, MIN_EXPR, compare_type,
- nitems_total, const_limit);
- first_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
- first_limit, nitems_skip);
- }
- else
- /* For the first iteration it doesn't matter whether the IV hits
- a value above NITEMS_TOTAL. That only matters for the latch
- condition. */
- first_limit = nitems_total;
- }
- else
- {
- /* Test the incremented IV, which will always hit a value above
- the bound before wrapping. */
- test_index = index_after_incr;
- test_limit = nitems_total;
- if (nitems_skip)
- test_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
- test_limit, nitems_skip);
-
- first_limit = test_limit;
- }
-
- /* Convert the IV value to the comparison type (either a no-op or
- a demotion). */
- gimple_seq test_seq = NULL;
- test_index = gimple_convert (&test_seq, compare_type, test_index);
- gsi_insert_seq_after (&incr_gsi, test_seq, GSI_NEW_STMT);
-
/* Provide a definition of each control in the group. */
tree next_ctrl = NULL_TREE;
tree ctrl;
@@ -768,25 +804,44 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
poly_uint64 bias = nitems_per_ctrl * i;
tree bias_tree = build_int_cst (compare_type, bias);
- /* See whether the first iteration of the vector loop is known
- to have a full control. */
- poly_uint64 const_limit;
- bool first_iteration_full
- = (poly_int_tree_p (first_limit, &const_limit)
- && known_ge (const_limit, (i + 1) * nitems_per_ctrl));
+ /* In an uncounted loop, the first iteration is assumed to
+ have a full control, except for any items that must be skipped
+ for alignment.
- /* Rather than have a new IV that starts at BIAS and goes up to
- TEST_LIMIT, prefer to use the same 0-based IV for each control
- and adjust the bound down by BIAS. */
- tree this_test_limit = test_limit;
- if (i != 0)
+ For counted loops, determine whether the first iteration
+ is known to have a full control. */
+ bool first_iteration_full = uncounted_p;
+ tree this_test_limit = NULL_TREE;
+
+ if (!uncounted_p)
{
- this_test_limit = gimple_build (preheader_seq, MAX_EXPR,
- compare_type, this_test_limit,
- bias_tree);
- this_test_limit = gimple_build (preheader_seq, MINUS_EXPR,
- compare_type, this_test_limit,
- bias_tree);
+ /* See whether the first iteration of the vector loop is known
+ to have a full control. */
+ poly_uint64 const_limit;
+ first_iteration_full
+ = (poly_int_tree_p (first_limit, &const_limit)
+ && known_ge (const_limit, (i + 1) * nitems_per_ctrl));
+
+ /* Rather than have a new IV that starts at BIAS and goes up to
+ TEST_LIMIT, prefer to use the same 0-based IV for each control
+ and adjust the bound down by BIAS. */
+ this_test_limit = test_limit;
+ if (i != 0)
+ {
+ this_test_limit = gimple_build (preheader_seq, MAX_EXPR,
+ compare_type, this_test_limit,
+ bias_tree);
+ this_test_limit = gimple_build (preheader_seq, MINUS_EXPR,
+ compare_type, this_test_limit,
+ bias_tree);
+ }
+
+ /* A do-while loop always executes the body once, as such the limit
+ the end counter should be lowered by 1 iteration. */
+ if (LOOP_VINFO_EARLY_BREAKS_VECT_PEELED (loop_vinfo))
+ this_test_limit = gimple_build (preheader_seq, MINUS_EXPR,
+ compare_type, this_test_limit,
+ build_one_cst (compare_type));
}
/* Create the initial control. First include all items that
@@ -801,7 +856,7 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
and the loop limit. The "else" block would be valid too,
but this choice can avoid the need to load BIAS_TREE into
a register. */
- start = zero_index;
+ start = build_int_cst (compare_type, 0);
end = this_test_limit;
}
else
@@ -844,15 +899,25 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
if (!init_ctrl)
{
- /* First iteration is full. */
- if (use_masks_p)
- init_ctrl = build_minus_one_cst (ctrl_type);
- else
- init_ctrl = length_limit;
+ /* First iteration is full if not using masks. */
+ init_ctrl = use_masks_p ? build_minus_one_cst (ctrl_type)
+ : length_limit;
+
}
- /* Get the control value for the next iteration of the loop. */
- if (use_masks_p)
+ /* Get the control value for subsequent iterations.
+
+ Uncounted loops have no known iteration bound, so their
+ subsequent masks must be all-true. The actual loop exits
+ determine termination. */
+ if (uncounted_p)
+ {
+ if (use_masks_p)
+ next_ctrl = build_minus_one_cst (ctrl_type);
+ else
+ next_ctrl = length_limit;
+ }
+ else if (use_masks_p)
{
gimple_seq stmts = NULL;
next_ctrl = vect_gen_while (&stmts, ctrl_type, test_index,
@@ -882,6 +947,11 @@ vect_set_loop_controls_directly (class loop *loop,
loop_vec_info loop_vinfo,
gimple_seq_add_stmt (header_seq, minus);
}
+ if (uncounted_p && dump_enabled_p ())
+ dump_printf_loc (MSG_NOTE, vect_location,
+ "using %s partial-vector controls for uncounted loop\n",
+ use_masks_p ? "mask-based" : "length-based");
+
return next_ctrl;
}
--
2.43.0