https://gcc.gnu.org/g:27c86eb6e7e277e55ca8c257fc3a9d1c1a2a126e

commit r16-7410-g27c86eb6e7e277e55ca8c257fc3a9d1c1a2a126e
Author: Richard Biener <[email protected]>
Date:   Fri Feb 6 15:47:08 2026 +0100

    tree-optimization/123225 - require iteration estimate for uncounted loops
    
    The following makes uncounted loops not profitable to vectorize
    unless there's an estimate on the number of iterations, either
    from array sizes, overflow, or PGO, that indicates proftiability.
    Or trivial profitability, but that's impossible to reach - Tamars
    pending patch might change this in some cases.
    
    I have verified that with PGO we do vectorize the testcase in the PR.
    
            PR tree-optimization/123225
            * tree-vect-loop.cc (vect_analyze_loop_costing): For uncounted
            loops reject not trivially profitable loops that have no
            estimate on the number of scalar iterations.
    
            * gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c: New testcase.

Diff:
---
 .../gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c   | 17 +++++++++++++++++
 gcc/tree-vect-loop.cc                                   | 16 ++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c 
b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c
new file mode 100644
index 000000000000..00dd79f5d77a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-msse4" } */
+
+short *
+foo (short *arr)
+{
+  unsigned int pos = 0;
+  while(1)
+    {
+      arr++;
+      if (*arr == 0)
+       break;
+    }
+  return arr;
+}
+
+/* { dg-final { scan-tree-dump-not "optimized" "vect" } } */
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 1331ea119231..0947962fcf2b 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -1956,6 +1956,22 @@ vect_analyze_loop_costing (loop_vec_info loop_vinfo,
       return -1;
     }
 
+  /* As we cannot use a runtime check to gate profitability for uncounted
+     loops require either an estimate or if none, at least a profitable
+     vectorization within the first vector iteration (that condition
+     will practically never be true due to the required epilog and
+     likely alignment prologue).   */
+  if (LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo)
+      && estimated_niter == -1
+      && min_profitable_estimate > (int) vect_vf_for_cost (loop_vinfo))
+    {
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_NOTE, vect_location,
+                        "not vectorized: no loop iteration estimate on the "
+                        "uncounted loop and not trivially profitable.\n");
+      return -1;
+    }
+
   return 1;
 }

Reply via email to