On Fri, Oct 30, 2026 at 3:48 AM sbig <[email protected]> wrote:
>
> From: Steve Bezalel <[email protected]>
>
> tree_predictive_commoning only resets SCEV when a loop was unrolled or
> needed loop-closed SSA fixup (changed > 1).  When a loop is transformed
> without either, the SCEV results cached by predcom's data-reference
> analysis survive the transform.  At -O2, where predcom only runs
> implicitly with -ftree-loop-vectorize and never unrolls, that is the
> common case.
>
> In the testcase, the evolution of s is cached as not analyzable while
> its step is still a load of a[4].  Predcom then hoists that load, after
> which s is the affine IV {a_I_lsm0.4_26, +, a_I_lsm0.4_26}_1, but ivopts
> still reads the cached result, finds no niter and does not
> strength-reduce the b[s] store.
>
> At plain -O2 this is hidden because passes between predcom and ivopts
> clear the SCEV cache as a side effect: fre4 in pass_pre_slp_scalar_cleanup,
> and slp1 through the SSA name freelist flush.  -fno-tree-slp-vectorize
> disables both.
>
> The fix resets SCEV whenever predcom changed something.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu.  OK for trunk?

OK.

Thanks,
Richard.

>         PR tree-optimization/127211
>
> gcc/ChangeLog:
>
>         * tree-predcom.cc (tree_predictive_commoning): Reset SCEV
>         whenever a loop was transformed, not only when unrolled.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/pr127211.c: New test.
>
> Signed-off-by: Steve Bezalel <[email protected]>
> ---
>  gcc/testsuite/gcc.dg/tree-ssa/pr127211.c | 24 ++++++++++++++++++++++++
>  gcc/tree-predcom.cc                      |  7 +++++--
>  2 files changed, 29 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr127211.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c
> new file mode 100644
> index 000000000..bf9ae22bf
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127211.c
> @@ -0,0 +1,24 @@
> +/* PR tree-optimization/127211 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fno-tree-slp-vectorize -fdump-tree-ivopts-details" } */
> +
> +int a[4096], b[4096];
> +
> +void
> +fn (int m)
> +{
> +  int s = 0;
> +  for (int i = 0; i < a[4]; i++)
> +    {
> +      a[3*i + 2] = m;
> +      if (m & 1)
> +       a[3*i + 5] = m - 1;
> +      b[s] = m;
> +      s += a[4];
> +    }
> +}
> +
> +/* Predictive commoning hoists the load of a[4] out of the loop, after
> +   which s is an affine IV.  SCEV results cached by its data-reference
> +   analysis before the transform must not reach ivopts.  */
> +/* { dg-final { scan-tree-dump-not "niter scev_not_known" "ivopts" } } */
> diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc
> index 8e2894d46..a2043ef31 100644
> --- a/gcc/tree-predcom.cc
> +++ b/gcc/tree-predcom.cc
> @@ -3512,11 +3512,14 @@ tree_predictive_commoning (bool allow_unroll_p)
>      {
>        ret = TODO_update_ssa_only_virtuals;
>
> +      /* Cached SCEV information is stale after any transformation, not
> +        only after unrolling: the data-reference analysis above queried
> +        SCEV before references were rewritten.  */
> +      scev_reset ();
> +
>        /* Some loop(s) got unrolled.  */
>        if (changed > 1)
>         {
> -         scev_reset ();
> -
>           /* Need to fix up loop closed SSA.  */
>           if (changed >= 4)
>             rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
> --
> 2.43.0

Reply via email to