https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128732

--- Comment #4 from Suvarchal Cheedela (AWI) <suvarchal.cheedela at awi dot de> 
---
Created attachment 66185
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66185&action=edit
patch prototype

I tried the versioning route; a prototype is attached (about 70 lines in
tree-ssa-loop-unswitch.cc, plus two tests).

find_loop_guard gives up when the outer loop has side effects outside the
part the guard skips (here the store to c[i]).  In that case
tree_unswitch_outer_loop now versions the outer loop on the guard condition
(m > 0) and folds the guard to true in one copy and to false in the other.
In the false copy the inner loop is unreachable and cfg cleanup removes it.
The versioning is limited by max-unswitch-insns (outer body without the
inner loop) and is not done when the condition uses possibly undefined
values.

With outer.c (x86_64, -O3 -march=x86-64-v3), the true copy is now
vectorized as an outer loop ("OUTER LOOP VECTORIZED"); the same holds for
the Fortran version:

                 trunk     patched
  C,       m=32  1.40 s    0.37 s   (same as the hand-unswitched source)
  Fortran, m=32  3.05 s    0.76 s

For m = -3, 0, 1, 2 the results are identical to -O0.

New tests: gcc.dg/loop-unswitch-20.c (dump scan) and gcc.dg/vect/pr128732.c
(run test for m = 0..3, checks outer-loop vectorization).  Bootstrapped and
regression tested (C, C++, Fortran) on x86_64-pc-linux-gnu with no new
failures.  The expected counts in gfortran.dg/loop_versioning_4.f90 go up
by one: subroutine f6 there has the same shape, so its outer loop is now
versioned too and lversion sees one more loop.

Is this the right place for it, or would you prefer it in the innermost
unswitching, by also collecting predicates from the outer loop bodies?
If this direction is fine, I will post it to gcc-patches.

Reply via email to