vect: Fix missing alias checks for 128-bit SVE [PR98371]
authorRichard Sandiford <richard.sandiford@arm.com>
Tue, 5 Jan 2021 11:03:22 +0000 (11:03 +0000)
committerRichard Sandiford <richard.sandiford@arm.com>
Tue, 5 Jan 2021 11:03:22 +0000 (11:03 +0000)
commit01be45eccee42d0cc6c900f43e2363186517f7ed
tree6f7c11daffb31f26b5ef8e5934e1ef80f06b2bf2
parenta20893cf6b95f60f5319b2196621acc65180125c
vect: Fix missing alias checks for 128-bit SVE [PR98371]

On AArch64, the vectoriser tries various ways of vectorising with both
SVE and Advanced SIMD and picks the best one.  All other things being
equal, it prefers earlier attempts over later attempts.

The way this works currently is that, once it has a successful
vectorisation attempt A, it analyses all other attempts as epilogue
loops of A:

      /* When pick_lowest_cost_p is true, we should in principle iterate
 over all the loop_vec_infos that LOOP_VINFO could replace and
 try to vectorize LOOP_VINFO under the same conditions.
 E.g. when trying to replace an epilogue loop, we should vectorize
 LOOP_VINFO as an epilogue loop with the same VF limit.  When trying
 to replace the main loop, we should vectorize LOOP_VINFO as a main
 loop too.

 However, autovectorize_vector_modes is usually sorted as follows:

 - Modes that naturally produce lower VFs usually follow modes that
   naturally produce higher VFs.

 - When modes naturally produce the same VF, maskable modes
   usually follow unmaskable ones, so that the maskable mode
   can be used to vectorize the epilogue of the unmaskable mode.

 This order is preferred because it leads to the maximum
 epilogue vectorization opportunities.  Targets should only use
 a different order if they want to make wide modes available while
 disparaging them relative to earlier, smaller modes.  The assumption
 in that case is that the wider modes are more expensive in some
 way that isn't reflected directly in the costs.

 There should therefore be few interesting cases in which
 LOOP_VINFO fails when treated as an epilogue loop, succeeds when
 treated as a standalone loop, and ends up being genuinely cheaper
 than FIRST_LOOP_VINFO.  */

However, the vectoriser can normally elide alias checks for epilogue
loops, on the basis that the main loop should do them instead.
Converting an epilogue loop to a main loop can therefore cause the alias
checks to be skipped.  (It probably also unfairly penalises the original
loop in the cost comparison, given that one loop will have alias checks
and the other won't.)

As the comment says, we should in principle analyse each vector mode
twice: once as a main loop and once as an epilogue.  However, doing
that up-front would be quite expensive.  This patch instead goes for a
compromise: if an epilogue loop for mode M2 seems better than a main
loop for mode M1, re-analyse with M2 as the main loop.

The patch fixes dg.torture.exp=pr69719.c when testing with
-msve-vector-bits=128.

gcc/
PR tree-optimization/98371
* tree-vect-loop.c (vect_reanalyze_as_main_loop): New function.
(vect_analyze_loop): If an epilogue loop appears to be cheaper
than the main loop, re-analyze it as a main loop before adopting
it as a main loop.
gcc/tree-vect-loop.c