]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/123225 - require iteration estimate for uncounted loops
authorRichard Biener <rguenther@suse.de>
Fri, 6 Feb 2026 14:47:08 +0000 (15:47 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Mon, 9 Feb 2026 12:45:41 +0000 (13:45 +0100)
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.

gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c [new file with mode: 0644]
gcc/tree-vect-loop.cc

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 (file)
index 0000000..00dd79f
--- /dev/null
@@ -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" } } */
index 1331ea1192316918e69f640b6324d7615905e0f6..0947962fcf2b0925c38074c21d31ae2b0f4165ef 100644 (file)
@@ -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;
 }