GIMPLE_DEBUG statements currently consume UIDs and are then treated as
non-assignments, which flushes pending VEC_PERM simplify sequences.  This
makes sequence blending depend on whether debug information is present.

Skip debug statements before assigning UIDs and processing ordinary
statements.

Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.

        PR tree-optimization/127687

gcc/ChangeLog:

        * tree-ssa-forwprop.cc (pass_forwprop::execute): Skip debug
        statements before assigning UIDs.

gcc/testsuite/ChangeLog:

        * gcc.dg/pr127687.c: New test.

Signed-off-by: Yongqiang Tian <[email protected]>
---
Tested on x86_64-pc-linux-gnu with a bootstrap and regression test.
The unexpected results were also present in the unpatched baseline; no
regressions were found.

I do not have commit access.

 gcc/testsuite/gcc.dg/pr127687.c | 23 +++++++++++++++++++++++
 gcc/tree-ssa-forwprop.cc        |  6 ++++++
 2 files changed, 29 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr127687.c

diff --git a/gcc/testsuite/gcc.dg/pr127687.c b/gcc/testsuite/gcc.dg/pr127687.c
new file mode 100644
index 000000000..4b30bba53
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr127687.c
@@ -0,0 +1,23 @@
+/* PR tree-optimization/127687 */
+/* { dg-do compile { target aarch64*-*-* i?86-*-* x86_64-*-* } } */
+/* { dg-options "-O2 -fcompare-debug -fdump-tree-forwprop1-details" } */
+/* { dg-additional-options "-msse2" { target i?86-*-* x86_64-*-* } } */
+
+typedef int V __attribute__((vector_size (4 * sizeof (int))));
+
+[[gnu::noipa]] void
+foo (V *x, V *y)
+{
+  V a = *x;
+  V b = *y;
+  V c = __builtin_shufflevector (a, a, 0, 2, 0, 2);
+  V d = __builtin_shufflevector (a, a, 1, 3, 1, 3);
+  V e = __builtin_shufflevector (b, b, 0, 2, 0, 2);
+  V f = __builtin_shufflevector (b, b, 1, 3, 1, 3);
+  V g = __builtin_shufflevector (c + d, c - d, 0, 4, 1, 5);
+  V h = __builtin_shufflevector (e + f, e - f, 0, 4, 1, 5);
+  *x = g;
+  *y = h;
+}
+
+/* { dg-final { scan-tree-dump "Vec perm simplify sequences have been blended" 
"forwprop1" } } */
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index f341514a8..fba4387f5 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -6755,6 +6755,12 @@ pass_forwprop::execute (function *fun)
          tree lhs, rhs;
          enum tree_code code;
 
+         if (is_gimple_debug (stmt))
+           {
+             gsi_next (&gsi);
+             continue;
+           }
+
          gimple_set_uid (stmt, uid++);
 
          if (!is_gimple_assign (stmt))
-- 
2.34.1

Reply via email to