On Wed, Nov 18, 2026 at 9:29 PM Robin Dapp <[email protected]> wrote:
>
> Hi,
>
> 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.
>
> Bootstrapped and regtested on x86, power10, and aarch64.
> Regtested on riscv64.
OK.
Thanks,
Richard.
> Regards
> Robin
>
> 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.
> ---
> 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 | 25 ++++++++++++++++------
> gcc/tree-vectorizer.h | 2 +-
> 5 files changed, 67 insertions(+), 9 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/vect/pr128689-2.c
> create mode 100644 gcc/testsuite/gcc.dg/vect/pr128689.c
>
> 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 00000000000..c165bf82a0f
> --- /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 00000000000..1f1e5fb7e7f
> --- /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 9ce979aeac7..e113231dccc 100644
> --- a/gcc/tree-vect-data-refs.cc
> +++ b/gcc/tree-vect-data-refs.cc
> @@ -5062,7 +5062,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
> @@ -5081,7 +5082,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 5148f4118d9..ad5eaa3dd62 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -1986,18 +1986,29 @@ 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)
> @@ -2080,8 +2091,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)));
> @@ -3030,7 +3043,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 24c40556068..4c914a7f5b6 100644
> --- a/gcc/tree-vectorizer.h
> +++ b/gcc/tree-vectorizer.h
> @@ -2684,7 +2684,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 *,
> --
> 2.56.0
>