https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88533
--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
OK, so I can add heuristic that avoids copying BBs with exit conditions based
on non-IVs/non-invariants but that doesn't fix this testcase since the
fourth condition in the loop is based on the load from 'ja':
do j=1,n ! Outer loop j: columns of A
!CDIR ALTCODE=LOOPCNT
!CDIR NODEP
!DIR$ IVDEP
do k = ia(j), ia(j+1)-1 ! Inner loop i: rows of (sparse) A
i = ja(k) ! (the i's are distinct for different j's)
y(i) = y(i) + a(k) * x(j)
end do
end do
so we copy the header check for the ia(j), ia(j+1)-1 range (together with
its bounds checks) plus the bounds check for ja(k) but _not_ the bounds
check on 'i' which is strictly ordered before the invariant bounds check
for j on x(j).
I could relax the heuristic further but I think changing
gcc.dg/tree-ssa/copy-headers-5.c to
int is_sorted(int *a, int n)
{
for (int i = 0; i < n - 1; i++)
if (a[i] > 0)
return 0;
return 1;
}
shouldn't rotate the loop beyond hoisting the initial 0 < n - 1 check.
But we are now asking for exactly that to be able to hoist a further down
conditional that is invariant by means of rotating the loop.
That said, I have a patch restoring originally intended behavior (albeit
in a somewhat awkward way).