]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/121758 - fix pattern stmt REDUC_IDX updating
authorRichard Biener <rguenther@suse.de>
Wed, 3 Sep 2025 08:04:58 +0000 (10:04 +0200)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 3 Sep 2025 11:02:35 +0000 (13:02 +0200)
The following fixes a corner case of pattern stmt STMT_VINFO_REDUC_IDX
updating which happens auto-magically.  When a 2nd pattern sequence
uses defs from inside a prior pattern sequence then the first guess
for the lookfor can be off.  This happens when for example widening
patterns use vect_get_internal_def, which looks into earlier patterns.

PR tree-optimization/121758
* tree-vect-patterns.cc (vect_mark_pattern_stmts): Try
harder to find a reduction continuation.

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

gcc/testsuite/gcc.dg/vect/pr121758.c [new file with mode: 0644]
gcc/tree-vect-patterns.cc

diff --git a/gcc/testsuite/gcc.dg/vect/pr121758.c b/gcc/testsuite/gcc.dg/vect/pr121758.c
new file mode 100644 (file)
index 0000000..b27bc67
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+
+long g_2205, g_3005;
+int g_3320;
+void main()
+{
+  for (; g_2205; g_2205 += 1)
+    {
+      g_3005 = 0;
+      for (; g_3005 <= 8; g_3005 += 1)
+       g_3320 &= 611 & (unsigned char)g_3005;
+    }
+}
+
+/* { dg-final { scan-tree-dump-not "failed to update reduction index" "vect" } } */
index d0bf2f9e79906b52193d6d2228ea7a6c9bf386ec..41ca0f085f0ec89399b997320332e46f0731cc56 100644 (file)
@@ -7074,14 +7074,32 @@ vect_mark_pattern_stmts (vec_info *vinfo,
        {
          bool found = false;
          if (gimple_extract_op (s, &op))
-           for (unsigned i = 0; i < op.num_ops; ++i)
-             if (op.ops[i] == lookfor)
+           {
+             for (unsigned i = 0; i < op.num_ops; ++i)
+               if (op.ops[i] == lookfor)
+                 {
+                   STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
+                   lookfor = gimple_get_lhs (s);
+                   found = true;
+                   break;
+                 }
+             /* Try harder to find a mid-entry into an earlier pattern
+                sequence.  This means that the initial 'lookfor' was
+                bogus.  */
+             if (!found)
                {
-                 STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
-                 lookfor = gimple_get_lhs (s);
-                 found = true;
-                 break;
+                 for (unsigned i = 0; i < op.num_ops; ++i)
+                   if (TREE_CODE (op.ops[i]) == SSA_NAME)
+                     if (auto def = vinfo->lookup_def (op.ops[i]))
+                       if (vect_is_reduction (def))
+                         {
+                           STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
+                           lookfor = gimple_get_lhs (s);
+                           found = true;
+                           break;
+                         }
                }
+           }
          if (s == pattern_stmt)
            {
              if (!found && dump_enabled_p ())