]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/122308 - apply LIM after unroll-and-jam
authorRichard Biener <rguenther@suse.de>
Fri, 17 Oct 2025 13:12:11 +0000 (15:12 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 17 Oct 2025 14:03:28 +0000 (16:03 +0200)
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.

gcc/gimple-loop-jam.cc
gcc/testsuite/gcc.dg/vect/vect-pr122308.c [new file with mode: 0644]

index 5e6c04a7d7f2e15a9d12a54b56bed9997e532103..5c74f80af4c124ac51d944c59820ae495095abbc 100644 (file)
@@ -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 (file)
index 0000000..58c73ae
--- /dev/null
@@ -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 } } } */