https://gcc.gnu.org/g:d6986e06db5eeb797344d86bd3ef9b0654606bbd

commit r16-4469-gd6986e06db5eeb797344d86bd3ef9b0654606bbd
Author: Richard Biener <[email protected]>
Date:   Fri Oct 17 15:12:11 2025 +0200

    tree-optimization/122308 - apply LIM after unroll-and-jam
    
    Just like with loop interchange, unroll-and-jam can leave invariant
    stmts in the inner loop from outer loop stmts inbetween the two
    inner loop copies.  Do a per-function invariant motion when we
    applied unroll-and-jam.  This avoids failed dataref analysis
    and fallback to gather/scatter during vectorization.
    
            PR tree-optimization/122308
            * gimple-loop-jam.cc (tree_loop_unroll_and_jam): Do LIM
            after applying unroll-and-jam.
    
            * gcc.dg/vect/vect-pr122308.c: New testcase.

Diff:
---
 gcc/gimple-loop-jam.cc                    |  1 +
 gcc/testsuite/gcc.dg/vect/vect-pr122308.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/gcc/gimple-loop-jam.cc b/gcc/gimple-loop-jam.cc
index 5e6c04a7d7f2..5c74f80af4c1 100644
--- a/gcc/gimple-loop-jam.cc
+++ b/gcc/gimple-loop-jam.cc
@@ -641,6 +641,7 @@ tree_loop_unroll_and_jam (void)
        {
          cleanup_tree_cfg ();
          todo &= ~TODO_cleanup_cfg;
+         todo |= loop_invariant_motion_in_fun (cfun, false);
        }
       rewrite_into_loop_closed_ssa (NULL, 0);
       scev_reset ();
diff --git a/gcc/testsuite/gcc.dg/vect/vect-pr122308.c 
b/gcc/testsuite/gcc.dg/vect/vect-pr122308.c
new file mode 100644
index 000000000000..58c73aed6945
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-pr122308.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-O3 -fdump-tree-unrolljam-optimized" } */
+
+int a[1024];
+int b[2048];
+int c[2048];
+
+void foo(int n)
+{
+  for (int i = 0; i < n; i++)
+    {
+      int index = c[i];
+
+      for (int j = 0; j < 1024; ++j)
+        a[j] += b[index + j];
+    }
+}
+
+/* { dg-final { scan-tree-dump "optimized: applying unroll and jam" 
"unrolljam" } } */
+/* { dg-final { scan-tree-dump-times "optimized: loop vectorized" 2 "vect" { 
target vect_int } } } */

Reply via email to