From: Richard Biener Date: Fri, 6 Feb 2026 14:47:08 +0000 (+0100) Subject: tree-optimization/123225 - require iteration estimate for uncounted loops X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=27c86eb6e7e277e55ca8c257fc3a9d1c1a2a126e;p=thirdparty%2Fgcc.git 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 --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 1331ea11923..0947962fcf2 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; }