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.

When Tamar lands the patch to elide the epilog in some cases the
vect_estimate_min_profitable_iters estimate should improve, not
accounting for the epilog, eventually passing the triviality
check (which is quite permissive as written here).

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

OK?

Thanks,
Richard.

        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.
---
 .../vect/costmodel/x86_64/costmodel-pr123225.c  | 17 +++++++++++++++++
 gcc/tree-vect-loop.cc                           | 16 ++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 
gcc/testsuite/gcc.dg/vect/costmodel/x86_64/costmodel-pr123225.c

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 00000000000..00dd79f5d77
--- /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 bb9bb35cfa8..e6b63ffe37f 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -1958,6 +1958,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;
 }
 
-- 
2.51.0

Reply via email to