]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect: Relax known iteration number constraint
authorVictor Do Nascimento <victor.donascimento@arm.com>
Thu, 8 May 2025 14:28:05 +0000 (15:28 +0100)
committerVictor Do Nascimento <victor.donascimento@arm.com>
Mon, 15 Dec 2025 15:27:40 +0000 (15:27 +0000)
At present we reject uncounted loops outright when doing initial loop
analysis in `vect_analyze_loop_form'.

We have the following gating condition that causes rejection of a
given loop:

 if (integer_zerop (info->assumptions)
      || !info->number_of_iterations
      || chrec_contains_undetermined (info->number_of_iterations))

We can do away with this check altogether, but not without problems,
allowing many malformed loops through which ought to be rejected as
early as possible.

We observe that a common thread running through these malformed loops
is the absence of any scalar evolution between iterations.

We have therefore adjusted the analysis replacing the checks on
`niters' for a test for the presence of scalar evolution in the loop,
which can be detected via the presence of phi nodes in the loop.

gcc/ChangeLog:

* tree-vect-loop.cc (vect_analyze_loop_form): Relax niters
condition.

gcc/tree-vect-loop.cc

index 9b435c78c21b1305944e954d6ff442263df84162..00b21ecfc9586501d0e68e2ecacdfb5c013df0fa 100644 (file)
@@ -1473,6 +1473,20 @@ vect_analyze_loop_form (class loop *loop, gimple *loop_vectorized_call,
                                       "not vectorized:"
                                       " unsupported control flow in loop.\n");
       }
+
+  /* Check if we have any control flow that doesn't leave the loop.  */
+  bool has_phi = false;
+  for (unsigned i = 0; i < loop->num_nodes; i++)
+    if (!gimple_seq_empty_p (phi_nodes (bbs[i])))
+      {
+       has_phi = true;
+       break;
+      }
+  if (!has_phi)
+    return opt_result::failure_at (vect_location,
+                                  "not vectorized:"
+                                  " no scalar evolution detected in loop.\n");
+
   free (bbs);
 
   /* Different restrictions apply when we are considering an inner-most loop,
@@ -1597,9 +1611,20 @@ vect_analyze_loop_form (class loop *loop, gimple *loop_vectorized_call,
        std::swap (info->conds[0], info->conds[i]);
     }
 
-  if (integer_zerop (info->assumptions)
-      || !info->number_of_iterations
-      || chrec_contains_undetermined (info->number_of_iterations))
+  if (chrec_contains_undetermined (info->number_of_iterations))
+    {
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_NOTE, vect_location,
+                        "Loop being analyzed as uncounted.\n");
+      if (loop->inner)
+       return opt_result::failure_at
+         (vect_location,
+          "not vectorized: outer loop vectorization of uncounted loops"
+          " is unsupported.\n");
+      return opt_result::success ();
+    }
+
+  if (integer_zerop (info->assumptions))
     return opt_result::failure_at
       (info->conds[0],
        "not vectorized: number of iterations cannot be computed.\n");