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.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

        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                    |  1 +
 gcc/testsuite/gcc.dg/vect/vect-pr122308.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/vect/vect-pr122308.c

diff --git a/gcc/gimple-loop-jam.cc b/gcc/gimple-loop-jam.cc
index 5e6c04a7d7f..5c74f80af4c 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 00000000000..58c73aed694
--- /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 } } } */
-- 
2.51.0

Reply via email to