]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect: Fix outer loop vectorization for nested uncounted loops [PR123657]
authorVictor Do Nascimento <victor.donascimento@arm.com>
Mon, 19 Jan 2026 16:28:26 +0000 (16:28 +0000)
committerVictor Do Nascimento <victor.donascimento@arm.com>
Mon, 26 Jan 2026 10:59:53 +0000 (10:59 +0000)
Given the inability of `expr_invariant_in_loop_p (loop, expr)' to
handle the `scev_not_known' node as the expression, an unknown loop
bound in the inner loop in a nested set of loops led to
`vect_analyze_loop_form' to erroneously consider the outer loop as
suitable for vectorization.  This introduces the necessary unknown
loop iteration count check to ensure correct handling of counted loops
with an embedded uncounted loop.

gcc/ChangeLog:

PR tree-optimization/123657
* tree-vect-loop.cc (vect_analyze_loop_form): Add
chrec_dont_know check.

gcc/testsuite/ChangeLog:

* gcc.dg/vect/vect-uncounted-run_4.c (main): New.

gcc/testsuite/gcc.dg/vect/vect-uncounted-run_4.c [new file with mode: 0644]
gcc/tree-vect-loop.cc

diff --git a/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_4.c b/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_4.c
new file mode 100644 (file)
index 0000000..f731daf
--- /dev/null
@@ -0,0 +1,17 @@
+/* Ensure we don't vectorize outer loops when the inner loop is uncounted.  */
+/* { dg-add-options vect_early_break } */
+/* { dg-require-effective-target vect_early_break_hw } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-O3" } */
+
+int a;
+int main() {
+  for (int b = 0; b < 21; b++) {
+    int c = b;
+    while (c)
+      a = c >>= 1;
+  }
+  if (a != 0) __builtin_abort();
+}
+
+/* { dg-final { scan-tree-dump "missed:   not vectorized: inner-loop count not invariant." "vect" } } */
index 73b103f3ee8a2630b85d28d51650bfea68e4973f..9fc169555e6707bb0419243ed53238d2944a01a4 100644 (file)
@@ -1556,7 +1556,8 @@ vect_analyze_loop_form (class loop *loop, gimple *loop_vectorized_call,
        return opt_result::failure_at (vect_location,
                                       "not vectorized: Bad inner loop.\n");
 
-      if (!expr_invariant_in_loop_p (loop, inner.number_of_iterations))
+      if (inner.number_of_iterations ==  chrec_dont_know
+         || !expr_invariant_in_loop_p (loop, inner.number_of_iterations))
        return opt_result::failure_at (vect_location,
                                       "not vectorized: inner-loop count not"
                                       " invariant.\n");