The vec_perm code falls back to doing byte-level permutes if element-level permutes aren't supported. There were two copies of the code to calculate the mode, and later patches add another, so this patch splits it out into a helper function.
2017-12-09 Richard Sandiford <richard.sandif...@linaro.org> gcc/ * optabs-query.h (qimode_for_vec_perm): Declare. * optabs-query.c (can_vec_perm_p): Split out qimode search to... (qimode_for_vec_perm): ...this new function. * optabs.c (expand_vec_perm): Use qimode_for_vec_perm. Index: gcc/optabs-query.h =================================================================== --- gcc/optabs-query.h 2017-12-09 22:47:12.476364764 +0000 +++ gcc/optabs-query.h 2017-12-09 22:47:14.730310076 +0000 @@ -174,6 +174,7 @@ enum insn_code can_extend_p (machine_mod enum insn_code can_float_p (machine_mode, machine_mode, int); enum insn_code can_fix_p (machine_mode, machine_mode, int, bool *); bool can_conditionally_move_p (machine_mode mode); +opt_machine_mode qimode_for_vec_perm (machine_mode); bool can_vec_perm_p (machine_mode, bool, vec_perm_indices *); /* Find a widening optab even if it doesn't widen as much as we want. */ #define find_widening_optab_handler(A, B, C) \ Index: gcc/optabs-query.c =================================================================== --- gcc/optabs-query.c 2017-12-09 22:47:12.476364764 +0000 +++ gcc/optabs-query.c 2017-12-09 22:47:14.729310075 +0000 @@ -345,6 +345,22 @@ can_conditionally_move_p (machine_mode m return direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing; } +/* If a target doesn't implement a permute on a vector with multibyte + elements, we can try to do the same permute on byte elements. + If this makes sense for vector mode MODE then return the appropriate + byte vector mode. */ + +opt_machine_mode +qimode_for_vec_perm (machine_mode mode) +{ + machine_mode qimode; + if (GET_MODE_INNER (mode) != QImode + && mode_for_vector (QImode, GET_MODE_SIZE (mode)).exists (&qimode) + && VECTOR_MODE_P (qimode)) + return qimode; + return opt_machine_mode (); +} + /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown constant. Note that additional permutations @@ -375,9 +391,7 @@ can_vec_perm_p (machine_mode mode, bool return true; /* We allow fallback to a QI vector mode, and adjust the mask. */ - if (GET_MODE_INNER (mode) == QImode - || !mode_for_vector (QImode, GET_MODE_SIZE (mode)).exists (&qimode) - || !VECTOR_MODE_P (qimode)) + if (!qimode_for_vec_perm (mode).exists (&qimode)) return false; /* ??? For completeness, we ought to check the QImode version of Index: gcc/optabs.c =================================================================== --- gcc/optabs.c 2017-12-09 22:47:12.476364764 +0000 +++ gcc/optabs.c 2017-12-09 22:47:14.731310077 +0000 @@ -5452,9 +5452,7 @@ expand_vec_perm (machine_mode mode, rtx /* Set QIMODE to a different vector mode with byte elements. If no such mode, or if MODE already has byte elements, use VOIDmode. */ - if (GET_MODE_INNER (mode) == QImode - || !mode_for_vector (QImode, w).exists (&qimode) - || !VECTOR_MODE_P (qimode)) + if (!qimode_for_vec_perm (mode).exists (&qimode)) qimode = VOIDmode; /* If the input is a constant, expand it specially. */