diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f64e24f65efb8bbd17b0f837ca08b7e63f1cec3a..60bbfbf42535c6f223a703b650aa0d7fea376a8a 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2018-01-02  Richard Sandiford  <richard.sandiford@linaro.org>
+
+	* 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.
+
 2018-01-02  Aaron Sawdey  <acsawdey@linux.vnet.ibm.com>
 
 	* rtlanal.c (canonicalize_condition): Return 0 if final rtx
diff --git a/gcc/optabs-query.c b/gcc/optabs-query.c
index 440462bc242340e46d04f86d41d3961817522096..02a583fa8a56d7fa703f20d619cff723c8e47c4b 100644
--- a/gcc/optabs-query.c
+++ b/gcc/optabs-query.c
@@ -345,6 +345,22 @@ can_conditionally_move_p (machine_mode mode)
   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 variable, vec_perm_indices *sel)
     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
diff --git a/gcc/optabs-query.h b/gcc/optabs-query.h
index 2701e2594e6a1d2f333f1156e777b1c798584eab..ce0c401a9d50c095cb8e925d2789a431027d02a6 100644
--- a/gcc/optabs-query.h
+++ b/gcc/optabs-query.h
@@ -174,6 +174,7 @@ enum insn_code can_extend_p (machine_mode, machine_mode, int);
 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) \
diff --git a/gcc/optabs.c b/gcc/optabs.c
index 225e9558bc3256bf0fabe4419a4328e4b0427fdb..a2213ddd72e9c23a34227dac1d1230598ac938b7 100644
--- a/gcc/optabs.c
+++ b/gcc/optabs.c
@@ -5472,9 +5472,7 @@ expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
 
   /* 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.  */