]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/121830 - SLP cycle detection confused by nested cycle
authorRichard Biener <rguenther@suse.de>
Mon, 8 Sep 2025 11:25:37 +0000 (13:25 +0200)
committerRichard Biener <rguenth@gcc.gnu.org>
Mon, 8 Sep 2025 12:40:18 +0000 (14:40 +0200)
The SLP reduc-index computation is confused by having an outer reduction
inner loop nested cycle fed by another non-reduction nested cycle.
Instead of undoing the unfortunate mixing of outer reduction inner
cycles with general nested cycles the following instead distinguishes
them by not setting STMT_VINFO_REDUC_DEF on the non-reduction nested
cycles.

PR tree-optimization/121830
* tree-vect-loop.cc (vect_analyze_scalar_cycles_1): Only
set STMT_VINFO_REDUC_DEF on reductions.
* tree-vect-slp.cc (vect_build_slp_tree_2): Identify reduction
PHIs by a set STMT_VINFO_REDUC_DEF instead of their def type.

* gcc.dg/vect/pr121830.c: New testcase.

gcc/testsuite/gcc.dg/vect/pr121830.c [new file with mode: 0644]
gcc/tree-vect-loop.cc
gcc/tree-vect-slp.cc

diff --git a/gcc/testsuite/gcc.dg/vect/pr121830.c b/gcc/testsuite/gcc.dg/vect/pr121830.c
new file mode 100644 (file)
index 0000000..7a6ea94
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+
+int a, b;
+short c;
+short d(short e) { return e - 1; }
+int main() {
+  for (; c; c++)
+    for (a = 2; a != 0; a = d(a)) {
+      int *i = &b;
+      *i &= a;
+    }
+  return 0;
+}
index 34ca32619f21cde8464962a79fea101e044e0f7f..446c4e3b3db66900ba4b4c75ecdf6459f754a259 100644 (file)
@@ -460,8 +460,6 @@ vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, class loop *loop,
        }
       else if (reduc_stmt_info)
        {
-         STMT_VINFO_REDUC_DEF (stmt_vinfo) = reduc_stmt_info;
-         STMT_VINFO_REDUC_DEF (reduc_stmt_info) = stmt_vinfo;
          if (loop != LOOP_VINFO_LOOP (loop_vinfo))
            {
              if (dump_enabled_p ())
@@ -472,6 +470,8 @@ vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, class loop *loop,
            }
          else
            {
+             STMT_VINFO_REDUC_DEF (stmt_vinfo) = reduc_stmt_info;
+             STMT_VINFO_REDUC_DEF (reduc_stmt_info) = stmt_vinfo;
              if (dump_enabled_p ())
                dump_printf_loc (MSG_NOTE, vect_location,
                                 "Detected reduction.\n");
index 59bca1dfce7c3a37d33207d90f9e95394d56fc04..8187dbc47f1d7a79ca2a6955cd4d0a32d3e80340 100644 (file)
@@ -2832,7 +2832,7 @@ out:
 
       /* See which SLP operand a reduction chain continues on.  We want
         to chain even PHIs but not backedges.  */
-      if (VECTORIZABLE_CYCLE_DEF (oprnd_info->first_dt)
+      if (STMT_VINFO_REDUC_DEF (oprnd_info->def_stmts[0])
          || STMT_VINFO_REDUC_IDX (oprnd_info->def_stmts[0]) != -1)
        {
          if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle)