https://gcc.gnu.org/g:b55cf3fda452a9a14dd2ea64a489099f6f70cb88
commit r17-6501-gb55cf3fda452a9a14dd2ea64a489099f6f70cb88 Author: Robin Dapp <[email protected]> Date: Wed Nov 18 13:19:55 2026 +0100 vect: Don't version uncounted loops for no wrap [PR128689]. We currently just discard no-wrap assumptions we recorded for an uncounted loop. As an uncounted loop doesn't have analyzed niters, we can't easily build a guard condition. While we might be able to tackle this in the future, this patch doesn't try to record nowrap assumptions for an uncounted loop and asserts that niter is analyzed in vect_build_assumptions. PR tree-optimization/128689 gcc/ChangeLog: * tree-vect-data-refs.cc (vect_find_stmt_data_reference): New param. * tree-vect-loop.cc (vect_get_datarefs_in_loop): Pass vect_loop_form_info. (vect_build_assumptions): Assert that niters was analyzed if we have bounds. (vect_analyze_loop): Pass loop_form_info. * tree-vectorizer.h (vect_find_stmt_data_reference): Change param to loop_form_info. gcc/testsuite/ChangeLog: * gcc.dg/vect/pr128689-2.c: New test. * gcc.dg/vect/pr128689.c: New test. Diff: --- gcc/testsuite/gcc.dg/vect/pr128689-2.c | 29 +++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/vect/pr128689.c | 14 ++++++++++++++ gcc/tree-vect-data-refs.cc | 6 ++++-- gcc/tree-vect-loop.cc | 26 ++++++++++++++++++++------ gcc/tree-vectorizer.h | 2 +- 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/pr128689-2.c b/gcc/testsuite/gcc.dg/vect/pr128689-2.c new file mode 100644 index 000000000000..c165bf82a0fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr128689-2.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ + +struct +{ + unsigned char ring[256]; + unsigned char after[4096]; +} g; + +int __attribute__ ((noipa)) +find (unsigned char i, unsigned char key) +{ + int n = 0; + while (g.ring[i] != key) /* i wraps 255 -> 0 */ + { + i++; + n++; + } + return n; +} + +int +main (void) +{ + g.ring[10] = 1; + g.after[100] = 1; + int n = find (200, 1); /* 200..255, 0..10 -> 66 */ + if (n != 66) + __builtin_abort (); +} diff --git a/gcc/testsuite/gcc.dg/vect/pr128689.c b/gcc/testsuite/gcc.dg/vect/pr128689.c new file mode 100644 index 000000000000..1f1e5fb7e7f6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr128689.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ + +struct { unsigned char ring[256]; unsigned char after[4096]; } g; + +int find(unsigned char i, unsigned char key) { + int n = 0; + while (g.ring[i] != key) { + i++; + n++; + } + return n; +} + +/* { dg-final { scan-tree-dump-not "adding no-wrap assumption" "vect" } } */ diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc index 08b94149e7c2..737b017d9881 100644 --- a/gcc/tree-vect-data-refs.cc +++ b/gcc/tree-vect-data-refs.cc @@ -5046,7 +5046,8 @@ vect_check_gather_scatter (stmt_vec_info stmt_info, tree vectype, opt_result vect_find_stmt_data_reference (loop_p loop, gimple *stmt, vec<data_reference_p> *datarefs, - vec<int> *dataref_groups, int group_id) + vec<int> *dataref_groups, int group_id, + bool record_nowrap_assumptions_p) { /* We can ignore clobbers for dataref analysis - they are removed during loop vectorization and BB vectorization checks dependences with a @@ -5065,7 +5066,8 @@ vect_find_stmt_data_reference (loop_p loop, gimple *stmt, stmt); auto_vec<data_reference_p, 2> refs; - opt_result res = find_data_references_in_stmt (loop, stmt, &refs, true); + opt_result res = find_data_references_in_stmt (loop, stmt, &refs, + record_nowrap_assumptions_p); if (!res) return res; diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 78bfd607c5d7..524bd151b175 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -1983,18 +1983,30 @@ vect_analyze_loop_costing (loop_vec_info loop_vinfo, *DATAREFS. */ static opt_result -vect_get_datarefs_in_loop (loop_p loop, basic_block *bbs, +vect_get_datarefs_in_loop (loop_p loop, + const vect_loop_form_info &loop_form_info, vec<data_reference_p> *datarefs) { for (unsigned i = 0; i < loop->num_nodes; i++) - for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]); + for (gimple_stmt_iterator gsi = gsi_start_bb (loop_form_info.bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple *stmt = gsi_stmt (gsi); if (is_gimple_debug (stmt)) continue; - opt_result res = vect_find_stmt_data_reference (loop, stmt, datarefs, - NULL, 0); + + /* An uncounted loop doesn't have analyzed niters. Don't record + nowrap assumptions for it. + ??? We could probably make this work by adding a virtual + early-break exit that guards the type bound? */ + bool record_nowrap_assumptions = true; + if (!loop_form_info.number_of_iterations + || chrec_contains_undetermined + (loop_form_info.number_of_iterations)) + record_nowrap_assumptions = false; + + opt_result res = vect_find_stmt_data_reference + (loop, stmt, datarefs, NULL, 0, record_nowrap_assumptions); if (!res) { if (is_gimple_call (stmt) && loop->safelen) @@ -2077,8 +2089,10 @@ vect_build_assumptions (class loop *loop, vec_info_shared *shared, scev_combine_nowrap_bounds (&bound_min, bound); } + gcc_assert (!bound_min || niter_known); + /* Build "NITER < BOUND_MIN". */ - if (niter_known && bound_min) + if (bound_min) { unsigned prec = MAX (TYPE_PRECISION (TREE_TYPE (niter)), TYPE_PRECISION (TREE_TYPE (bound_min))); @@ -3027,7 +3041,7 @@ vect_analyze_loop (class loop *loop, gimple *loop_vectorized_call, return opt_loop_vec_info::propagate_failure (res); } - res = vect_get_datarefs_in_loop (loop, loop_form_info.bbs, + res = vect_get_datarefs_in_loop (loop, loop_form_info, &shared->datarefs); if (!res) { diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h index 7184f193b074..84c23960b717 100644 --- a/gcc/tree-vectorizer.h +++ b/gcc/tree-vectorizer.h @@ -2648,7 +2648,7 @@ extern void vect_describe_gather_scatter_call (stmt_vec_info, gather_scatter_info *); extern opt_result vect_find_stmt_data_reference (loop_p, gimple *, vec<data_reference_p> *, - vec<int> *, int); + vec<int> *, int, bool = false); extern opt_result vect_analyze_data_refs (vec_info *, bool *); extern void vect_record_base_alignments (vec_info *); extern tree vect_create_data_ref_ptr (vec_info *,
