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.
{
cleanup_tree_cfg ();
todo &= ~TODO_cleanup_cfg;
+ todo |= loop_invariant_motion_in_fun (cfun, false);
}
rewrite_into_loop_closed_ssa (NULL, 0);
scev_reset ();
--- /dev/null
+/* { 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 } } } */