`vect_set_loop_condition_partial_vectors_avx512' currently relies
on a known scalar iteration count to generate partial-vector
masks.  It creates a decrementing induction variable to track
the remaining iterations and uses this to generate the masks
and the loop exit condition.

For uncounted loops, the scalar iteration count is unavailable
and termination is instead controlled by the vectorized
early-break conditions.

Extend the function to handle uncounted loops without creating
a decrementing induction variable.  Generate full masks for
subsequent iterations, while retaining the initial masking
needed when peeling for alignment.

Preserve the existing loop exit condition rather than replacing
it with an iteration-count-based condition.  Also avoid
calculating the loop iteration count or updating the final
induction variable for uncounted loops.

Retain the existing behavior for counted loops.

gcc/ChangeLog:

        * tree-vect-loop-manip.cc
        (vect_set_loop_condition_partial_vectors_avx512): Support
        uncounted loops without generating a decrementing induction
        variable.  Generate full masks for subsequent iterations,
        preserve initial alignment masking and the existing exit
        condition, and skip iteration-count and final-IV updates.
---
 gcc/tree-vect-loop-manip.cc | 221 +++++++++++++++++++++---------------
 1 file changed, 128 insertions(+), 93 deletions(-)

diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 5e3f27578f3..07092e8f762 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -1120,6 +1120,7 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
   poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
   tree orig_niters = niters;
   gimple_seq preheader_seq = NULL;
+  bool uncounted_p = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo);
 
   /* Create an IV that counts down from niters and whose step
      is the number of iterations processed in the current iteration.
@@ -1137,46 +1138,53 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
      repetitions according to max_nscalars_per_iter which is guaranteed
      to be the same within a group.  */
 
-  /* Convert NITERS to the determined IV type.  */
-  if (TYPE_PRECISION (iv_type) > TYPE_PRECISION (TREE_TYPE (niters))
-      && niters_maybe_zero)
+  tree iv_step = NULL_TREE;
+  tree index_before_incr = NULL_TREE;
+  tree index_after_incr = NULL_TREE;
+  gimple_stmt_iterator incr_gsi = {};
+
+  if (!uncounted_p)
     {
-      /* We know that there is always at least one iteration, so if the
-        count is zero then it must have wrapped.  Cope with this by
-        subtracting 1 before the conversion and adding 1 to the result.  */
-      gcc_assert (TYPE_UNSIGNED (TREE_TYPE (niters)));
-      niters = gimple_build (&preheader_seq, PLUS_EXPR, TREE_TYPE (niters),
-                            niters, build_minus_one_cst (TREE_TYPE (niters)));
-      niters = gimple_convert (&preheader_seq, iv_type, niters);
-      niters = gimple_build (&preheader_seq, PLUS_EXPR, iv_type,
-                            niters, build_one_cst (iv_type));
+      /* Convert NITERS to the determined IV type.  */
+      if (TYPE_PRECISION (iv_type) > TYPE_PRECISION (TREE_TYPE (niters))
+         && niters_maybe_zero)
+       {
+         /* We know that there is always at least one iteration, so if the
+            count is zero then it must have wrapped.  Cope with this by
+            subtracting 1 before the conversion and adding 1 to the result.  */
+         gcc_assert (TYPE_UNSIGNED (TREE_TYPE (niters)));
+         niters = gimple_build (&preheader_seq, PLUS_EXPR, TREE_TYPE (niters),
+                                niters,
+                                build_minus_one_cst (TREE_TYPE (niters)));
+         niters = gimple_convert (&preheader_seq, iv_type, niters);
+         niters = gimple_build (&preheader_seq, PLUS_EXPR, iv_type,
+                                niters, build_one_cst (iv_type));
+       }
+      else
+       niters = gimple_convert (&preheader_seq, iv_type, niters);
+
+      /* Bias the initial value of the IV in case we need to skip iterations
+        at the beginning.  */
+      tree niters_adj = niters;
+      if (niters_skip)
+       {
+         tree skip = gimple_convert (&preheader_seq, iv_type, niters_skip);
+         niters_adj = gimple_build (&preheader_seq, PLUS_EXPR,
+                                    iv_type, niters, skip);
+       }
+
+      bool insert_after;
+      standard_iv_increment_position (loop, &incr_gsi, &insert_after);
+
+      /* The iteration step is the vectorization factor.  */
+      iv_step = gimple_convert (&preheader_seq, iv_type,
+                               LOOP_VINFO_IV_INCREMENT (loop_vinfo));
+
+      /* Create the decrement IV.  */
+      create_iv (niters_adj, MINUS_EXPR, iv_step, NULL_TREE, loop,
+                &incr_gsi, insert_after, &index_before_incr,
+                &index_after_incr);
     }
-  else
-    niters = gimple_convert (&preheader_seq, iv_type, niters);
-
-  /* Bias the initial value of the IV in case we need to skip iterations
-     at the beginning.  */
-  tree niters_adj = niters;
-  if (niters_skip)
-    {
-      tree skip = gimple_convert (&preheader_seq, iv_type, niters_skip);
-      niters_adj = gimple_build (&preheader_seq, PLUS_EXPR,
-                                iv_type, niters, skip);
-    }
-
-  gimple_stmt_iterator incr_gsi;
-  tree index_before_incr, index_after_incr;
-  bool insert_after;
-  standard_iv_increment_position (loop, &incr_gsi, &insert_after);
-
-  /* The iteration step is the vectorization factor.  */
-  tree iv_step = gimple_convert (&preheader_seq, iv_type,
-                                LOOP_VINFO_IV_INCREMENT (loop_vinfo));
-
-  /* Create the decrement IV.  */
-  create_iv (niters_adj, MINUS_EXPR, iv_step, NULL_TREE, loop,
-            &incr_gsi, insert_after, &index_before_incr,
-            &index_after_incr);
 
   /* Iterate over all the rgroups and fill in their controls.  */
   for (auto &rgc : LOOP_VINFO_MASKS (loop_vinfo).rgc_vec)
@@ -1189,23 +1197,30 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
 
       tree vectype = rgc.compare_type;
 
-      /* index_after_incr is the IV specifying the remaining iterations in
-        the next iteration.  */
-      tree rem = index_after_incr;
-      /* When the data type for the compare to produce the mask is
-        smaller than the IV type we need to saturate.  Saturate to
-        the smallest possible value (IV_TYPE) so we only have to
-        saturate once (CSE will catch redundant ones we add).  */
-      if (TYPE_PRECISION (TREE_TYPE (vectype)) < TYPE_PRECISION (iv_type))
-       rem = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
-                           UNKNOWN_LOCATION,
-                           MIN_EXPR, TREE_TYPE (rem), rem, iv_step);
-      rem = gimple_convert (&incr_gsi, false, GSI_CONTINUE_LINKING,
-                           UNKNOWN_LOCATION, TREE_TYPE (vectype), rem);
+      tree rem = NULL_TREE;
 
-      /* Build a data vector composed of the remaining iterations.  */
-      rem = gimple_build_vector_from_val (&incr_gsi, false, 
GSI_CONTINUE_LINKING,
-                                         UNKNOWN_LOCATION, vectype, rem);
+      if (!uncounted_p)
+       {
+         /* index_after_incr is the IV specifying the remaining iterations in
+            the next iteration.  */
+         rem = index_after_incr;
+
+         /* When the data type for the compare to produce the mask is
+            smaller than the IV type we need to saturate.  Saturate to
+            the smallest possible value (IV_TYPE) so we only have to
+            saturate once (CSE will catch redundant ones we add).  */
+         if (TYPE_PRECISION (TREE_TYPE (vectype)) < TYPE_PRECISION (iv_type))
+           rem = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
+                               UNKNOWN_LOCATION,
+                               MIN_EXPR, TREE_TYPE (rem), rem, iv_step);
+         rem = gimple_convert (&incr_gsi, false, GSI_CONTINUE_LINKING,
+                               UNKNOWN_LOCATION, TREE_TYPE (vectype), rem);
+
+         /* Build a data vector composed of the remaining iterations.  */
+         rem = gimple_build_vector_from_val (&incr_gsi, false,
+                                             GSI_CONTINUE_LINKING,
+                                             UNKNOWN_LOCATION, vectype, rem);
+       }
 
       /* Provide a definition of each vector in the control group.  */
       tree next_ctrl = NULL_TREE;
@@ -1217,20 +1232,25 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
          /* Previous controls will cover BIAS items.  This control covers the
             next batch.  */
          poly_uint64 bias = nitems_per_ctrl * i;
+         tree cmp_series = NULL_TREE;
 
-         /* Build the constant to compare the remaining iters against,
-            this is sth like { 0, 0, 1, 1, 2, 2, 3, 3, ... } appropriately
-            split into pieces.  */
-         unsigned n = TYPE_VECTOR_SUBPARTS (ctrl_type).to_constant ();
-         tree_vector_builder builder (vectype, n, 1);
-         for (unsigned i = 0; i < n; ++i)
+         if (!uncounted_p)
            {
-             unsigned HOST_WIDE_INT val
-               = (i + bias.to_constant ()) / rgc.max_nscalars_per_iter;
-             gcc_assert (val < vf.to_constant ());
-             builder.quick_push (build_int_cst (TREE_TYPE (vectype), val));
+             /* Build the constant to compare the remaining iters against,
+                this is sth like { 0, 0, 1, 1, 2, 2, 3, 3, ... } appropriately
+                split into pieces.  */
+             unsigned n = TYPE_VECTOR_SUBPARTS (ctrl_type).to_constant ();
+             tree_vector_builder builder (vectype, n, 1);
+             for (unsigned i = 0; i < n; ++i)
+               {
+                 unsigned HOST_WIDE_INT val
+                   = (i + bias.to_constant ()) / rgc.max_nscalars_per_iter;
+                 gcc_assert (val < vf.to_constant ());
+                 builder.quick_push (build_int_cst (TREE_TYPE (vectype), val));
+               }
+
+             cmp_series = builder.build ();
            }
-         tree cmp_series = builder.build ();
 
          /* Create the initial control.  First include all items that
             are within the loop limit.  */
@@ -1238,8 +1258,9 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
          poly_uint64 const_limit;
          /* See whether the first iteration of the vector loop is known
             to have a full control.  */
-         if (poly_int_tree_p (niters, &const_limit)
-             && known_ge (const_limit, (i + 1) * nitems_per_ctrl))
+         if (uncounted_p || (poly_int_tree_p (niters, &const_limit)
+                             && known_ge (const_limit, (i + 1)
+                                          * nitems_per_ctrl)))
            init_ctrl = build_minus_one_cst (ctrl_type);
          else
            {
@@ -1294,10 +1315,13 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
                                        ctrl_type, init_ctrl);
            }
 
-         /* Get the control value for the next iteration of the loop.  */
-         next_ctrl = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
-                                   UNKNOWN_LOCATION,
-                                   LT_EXPR, ctrl_type, cmp_series, rem);
+         if (!uncounted_p)
+           /* Get the control value for the next iteration of the loop.  */
+           next_ctrl = gimple_build (&incr_gsi, false, GSI_CONTINUE_LINKING,
+                                     UNKNOWN_LOCATION,
+                                     LT_EXPR, ctrl_type, cmp_series, rem);
+         else
+           next_ctrl = build_minus_one_cst (ctrl_type);
 
          vect_set_loop_control (loop, ctrl, init_ctrl, next_ctrl);
        }
@@ -1306,29 +1330,40 @@ vect_set_loop_condition_partial_vectors_avx512 (class 
loop *loop,
   /* Emit all accumulated statements.  */
   add_preheader_seq (loop, preheader_seq);
 
-  /* Adjust the exit test using the decrementing IV.  */
-  tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR : GT_EXPR;
-  /* When we peel for alignment with niter_skip != 0 this can
-     cause niter + niter_skip to wrap and since we are comparing the
-     value before the decrement here we get a false early exit.
-     We can't compare the value after decrement either because that
-     decrement could wrap as well as we're not doing a saturating
-     decrement.  To avoid this situation we force a larger
-     iv_type.  */
-  gcond *cond_stmt = gimple_build_cond (code, index_before_incr, iv_step,
-                                       NULL_TREE, NULL_TREE);
-  /* The loop iterates (NITERS - 1 + NITERS_SKIP) / VF + 1 times.
-     Subtract one from this to get the latch count.  */
-  tree niters_minus_one
-    = fold_build2 (PLUS_EXPR, TREE_TYPE (orig_niters), orig_niters,
-                  build_minus_one_cst (TREE_TYPE (orig_niters)));
-  tree niters_adj2 = fold_convert (iv_type, niters_minus_one);
-  if (niters_skip)
-    niters_adj2 = fold_build2 (PLUS_EXPR, iv_type, niters_minus_one,
-                              fold_convert (iv_type, niters_skip));
-  loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, iv_type,
-                                    niters_adj2, iv_step);
+  /* For uncounted loops, no counting-IV exit condition is generated.
+     Preserve the existing exit condition.  */
+  gcond *cond_stmt = nullptr;
 
+  if (!uncounted_p)
+    {
+      /* Adjust the exit test using the decrementing IV.  */
+      tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR : 
GT_EXPR;
+      /* When we peel for alignment with niter_skip != 0 this can
+        cause niter + niter_skip to wrap and since we are comparing the
+        value before the decrement here we get a false early exit.
+        We can't compare the value after decrement either because that
+        decrement could wrap as well as we're not doing a saturating
+        decrement.  To avoid this situation we force a larger
+        iv_type.  */
+      cond_stmt = gimple_build_cond (code, index_before_incr, iv_step,
+                                           NULL_TREE, NULL_TREE);
+      /* The loop iterates (NITERS - 1 + NITERS_SKIP) / VF + 1 times.
+        Subtract one from this to get the latch count.  */
+      tree niters_minus_one
+       = fold_build2 (PLUS_EXPR, TREE_TYPE (orig_niters), orig_niters,
+                      build_minus_one_cst (TREE_TYPE (orig_niters)));
+      tree niters_adj2 = fold_convert (iv_type, niters_minus_one);
+      if (niters_skip)
+       niters_adj2 = fold_build2 (PLUS_EXPR, iv_type, niters_minus_one,
+                                  fold_convert (iv_type, niters_skip));
+      loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, iv_type,
+                                        niters_adj2, iv_step);
+    }
+
+  if (uncounted_p && dump_enabled_p ())
+    dump_printf_loc (MSG_NOTE, vect_location,
+                    "using mask-based partial-vector controls for "
+                    "uncounted loop\n");
   return cond_stmt;
 }
 
-- 
2.43.0

Reply via email to