`vect_set_loop_condition_partial_vectors' currently assumes that
the loop has a scalar iteration count.  It uses this count to
initialize the partial-vector controls, generate a loop exit
condition and calculate the number of vector iterations.

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

Allow uncounted loops to use this function without accessing
the scalar iteration count.  Skip the conversion of NITERS and
avoid generating a replacement loop exit condition or updating
`loop->nb_iterations' for these loops.

Retain the existing behavior for counted loops, including the
handling of potentially wrapping iteration counts and the
generation of loop exit conditions.

gcc/ChangeLog:

        * tree-vect-loop-manip.cc
        (vect_set_loop_condition_partial_vectors): Handle uncounted
        loops without converting NITERS, generating a replacement
        exit condition or calculating the loop iteration count.
---
 gcc/tree-vect-loop-manip.cc | 85 ++++++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 38 deletions(-)

diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 69b1b962e67..99ad5a88bb2 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -972,33 +972,36 @@ vect_set_loop_condition_partial_vectors (class loop 
*loop, edge exit_edge,
   gimple_seq preheader_seq = NULL;
   gimple_seq header_seq = NULL;
 
+  bool uncounted_p = LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo);
   bool use_masks_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
   tree compare_type = LOOP_VINFO_RGROUP_COMPARE_TYPE (loop_vinfo);
   unsigned int compare_precision = TYPE_PRECISION (compare_type);
 
   /* Type of the initial value of NITERS.  */
-  tree ni_actual_type = TREE_TYPE (niters);
+  tree ni_actual_type = uncounted_p ? sizetype : TREE_TYPE (niters);
   unsigned int ni_actual_precision = TYPE_PRECISION (ni_actual_type);
   tree niters_skip = LOOP_VINFO_MASK_SKIP_NITERS (loop_vinfo);
   if (niters_skip)
     niters_skip = gimple_convert (&preheader_seq, compare_type, niters_skip);
-
-  /* Convert NITERS to the same size as the compare.  */
-  if (compare_precision > ni_actual_precision
-      && niters_maybe_zero)
+  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 (ni_actual_type));
-      niters = gimple_build (&preheader_seq, PLUS_EXPR, ni_actual_type,
-                            niters, build_minus_one_cst (ni_actual_type));
-      niters = gimple_convert (&preheader_seq, compare_type, niters);
-      niters = gimple_build (&preheader_seq, PLUS_EXPR, compare_type,
-                            niters, build_one_cst (compare_type));
+      /* Convert NITERS to the same size as the compare.  */
+      if (compare_precision > ni_actual_precision
+         && 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 (ni_actual_type));
+         niters = gimple_build (&preheader_seq, PLUS_EXPR, ni_actual_type,
+                                niters, build_minus_one_cst (ni_actual_type));
+         niters = gimple_convert (&preheader_seq, compare_type, niters);
+         niters = gimple_build (&preheader_seq, PLUS_EXPR, compare_type,
+                                niters, build_one_cst (compare_type));
+       }
+      else
+       niters = gimple_convert (&preheader_seq, compare_type, niters);
     }
-  else
-    niters = gimple_convert (&preheader_seq, compare_type, niters);
 
   /* Iterate over all the rgroups and fill in their controls.  We could use
      the first control from any rgroup for the loop condition; here we
@@ -1064,30 +1067,36 @@ vect_set_loop_condition_partial_vectors (class loop 
*loop, edge exit_edge,
   add_header_seq (loop, header_seq);
 
   /* Get a boolean result that tells us whether to iterate.  */
-  gcond *cond_stmt;
-  if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)
-      && !LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
+  gcond *cond_stmt = nullptr;
+  if (!uncounted_p)
     {
-      gcc_assert (compare_step);
-      tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR : 
GT_EXPR;
-      cond_stmt = gimple_build_cond (code, test_ctrl, compare_step, NULL_TREE,
-                                    NULL_TREE);
+      if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)
+         && !LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
+       {
+         gcc_assert (compare_step);
+         tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? LE_EXPR
+                                                               : GT_EXPR;
+         cond_stmt = gimple_build_cond (code, test_ctrl, compare_step,
+                                        NULL_TREE, NULL_TREE);
+       }
+      else
+       {
+         tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? EQ_EXPR
+                                                               : NE_EXPR;
+         tree zero_ctrl = build_zero_cst (TREE_TYPE (test_ctrl));
+         cond_stmt
+           = gimple_build_cond (code, test_ctrl, zero_ctrl, NULL_TREE,
+                                NULL_TREE);
+       }
+      /* The loop iterates (NITERS - 1) / VF + 1 times.
+        Subtract one from this to get the latch count.  */
+      tree step = build_int_cst (compare_type,
+                                LOOP_VINFO_VECT_FACTOR (loop_vinfo));
+      tree niters_minus_one = fold_build2 (PLUS_EXPR, compare_type, niters,
+                                          build_minus_one_cst (compare_type));
+      loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, compare_type,
+                                        niters_minus_one, step);
     }
-  else
-    {
-      tree_code code = (exit_edge->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : 
NE_EXPR;
-      tree zero_ctrl = build_zero_cst (TREE_TYPE (test_ctrl));
-      cond_stmt
-       = gimple_build_cond (code, test_ctrl, zero_ctrl, NULL_TREE, NULL_TREE);
-    }
-  /* The loop iterates (NITERS - 1) / VF + 1 times.
-     Subtract one from this to get the latch count.  */
-  tree step = build_int_cst (compare_type,
-                            LOOP_VINFO_VECT_FACTOR (loop_vinfo));
-  tree niters_minus_one = fold_build2 (PLUS_EXPR, compare_type, niters,
-                                      build_minus_one_cst (compare_type));
-  loop->nb_iterations = fold_build2 (TRUNC_DIV_EXPR, compare_type,
-                                    niters_minus_one, step);
 
   return cond_stmt;
 }
-- 
2.43.0

Reply via email to