Callers of vect_halve_mask_nunits and vect_double_mask_nunits
already know what mode the resulting vector type should have,
so we might as well create the vector type directly with that mode,
just like build_vector_type_for_mode lets us build normal vectors
with a known mode.  This avoids the current awkwardness of having
to recompute the mode starting from vec_info::vector_size, which
hard-codes the assumption that all vectors have to be the same size.

A later patch gets rid of build_truth_vector_type and
build_same_sized_truth_vector_type, so the net effect of the
series is to reduce the number of type functions by one.

Tested individually on aarch64-linux-gnu and as a series on
x86_64-linux-gnu.  OK to install?

Richard


2019-10-23  Richard Sandiford  <richard.sandif...@arm.com>

gcc/
        * tree.h (build_truth_vector_type_for_mode): Declare.
        * tree.c (build_truth_vector_type_for_mode): New function,
        split out from...
        (build_truth_vector_type): ...here.
        (build_opaque_vector_type): Fix head comment.
        * tree-vectorizer.h (supportable_narrowing_operation): Remove
        vec_info parameter.
        (vect_halve_mask_nunits): Replace vec_info parameter with the
        mode of the new vector.
        (vect_double_mask_nunits): Likewise.
        * tree-vect-loop.c (vect_halve_mask_nunits): Likewise.
        (vect_double_mask_nunits): Likewise.
        * tree-vect-loop-manip.c: Include insn-config.h, rtl.h and recog.h.
        (vect_maybe_permute_loop_masks): Remove vinfo parameter.  Update call
        to vect_halve_mask_nunits, getting the required mode from the unpack
        patterns.
        (vect_set_loop_condition_masked): Update call accordingly.
        * tree-vect-stmts.c (supportable_narrowing_operation): Remove vec_info
        parameter and update call to vect_double_mask_nunits.
        (vectorizable_conversion): Update call accordingly.
        (simple_integer_narrowing): Likewise.  Remove vec_info parameter.
        (vectorizable_call): Update call accordingly.
        (supportable_widening_operation): Update call to
        vect_halve_mask_nunits.

Index: gcc/tree.h
===================================================================
--- gcc/tree.h  2019-09-21 13:56:07.519944842 +0100
+++ gcc/tree.h  2019-10-23 12:07:54.505663970 +0100
@@ -4437,6 +4437,7 @@ extern tree build_reference_type_for_mod
 extern tree build_reference_type (tree);
 extern tree build_vector_type_for_mode (tree, machine_mode);
 extern tree build_vector_type (tree, poly_int64);
+extern tree build_truth_vector_type_for_mode (poly_uint64, machine_mode);
 extern tree build_truth_vector_type (poly_uint64, poly_uint64);
 extern tree build_same_sized_truth_vector_type (tree vectype);
 extern tree build_opaque_vector_type (tree, poly_int64);
Index: gcc/tree.c
===================================================================
--- gcc/tree.c  2019-10-20 13:58:01.679637360 +0100
+++ gcc/tree.c  2019-10-23 12:07:54.501663998 +0100
@@ -11113,25 +11113,35 @@ build_vector_type (tree innertype, poly_
   return make_vector_type (innertype, nunits, VOIDmode);
 }
 
+/* Build a truth vector with NUNITS units, giving it mode MASK_MODE.  */
+
+tree
+build_truth_vector_type_for_mode (poly_uint64 nunits, machine_mode mask_mode)
+{
+  gcc_assert (mask_mode != BLKmode);
+
+  poly_uint64 vsize = GET_MODE_BITSIZE (mask_mode);
+  unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
+  tree bool_type = build_nonstandard_boolean_type (esize);
+
+  return make_vector_type (bool_type, nunits, mask_mode);
+}
+
 /* Build truth vector with specified length and number of units.  */
 
 tree
 build_truth_vector_type (poly_uint64 nunits, poly_uint64 vector_size)
 {
-  machine_mode mask_mode
-    = targetm.vectorize.get_mask_mode (nunits, vector_size).else_blk ();
-
-  poly_uint64 vsize;
-  if (mask_mode == BLKmode)
-    vsize = vector_size * BITS_PER_UNIT;
-  else
-    vsize = GET_MODE_BITSIZE (mask_mode);
+  machine_mode mask_mode;
+  if (targetm.vectorize.get_mask_mode (nunits,
+                                      vector_size).exists (&mask_mode))
+    return build_truth_vector_type_for_mode (nunits, mask_mode);
 
+  poly_uint64 vsize = vector_size * BITS_PER_UNIT;
   unsigned HOST_WIDE_INT esize = vector_element_size (vsize, nunits);
-
   tree bool_type = build_nonstandard_boolean_type (esize);
 
-  return make_vector_type (bool_type, nunits, mask_mode);
+  return make_vector_type (bool_type, nunits, BLKmode);
 }
 
 /* Returns a vector type corresponding to a comparison of VECTYPE.  */
@@ -11150,7 +11160,8 @@ build_same_sized_truth_vector_type (tree
   return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype), size);
 }
 
-/* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set.  */
+/* Like build_vector_type, but builds a variant type with TYPE_VECTOR_OPAQUE
+   set.  */
 
 tree
 build_opaque_vector_type (tree innertype, poly_int64 nunits)
Index: gcc/tree-vectorizer.h
===================================================================
--- gcc/tree-vectorizer.h       2019-10-22 08:46:56.395362885 +0100
+++ gcc/tree-vectorizer.h       2019-10-23 12:07:54.501663998 +0100
@@ -1609,9 +1609,9 @@ extern bool supportable_widening_operati
                                            tree, tree, enum tree_code *,
                                            enum tree_code *, int *,
                                            vec<tree> *);
-extern bool supportable_narrowing_operation (vec_info *, enum tree_code, tree,
-                                            tree, enum tree_code *,
-                                            int *, vec<tree> *);
+extern bool supportable_narrowing_operation (enum tree_code, tree, tree,
+                                            enum tree_code *, int *,
+                                            vec<tree> *);
 extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
                                  enum vect_cost_for_stmt, stmt_vec_info,
                                  int, enum vect_cost_model_location);
@@ -1711,8 +1711,8 @@ extern opt_loop_vec_info vect_analyze_lo
 extern tree vect_build_loop_niters (loop_vec_info, bool * = NULL);
 extern void vect_gen_vector_loop_niters (loop_vec_info, tree, tree *,
                                         tree *, bool);
-extern tree vect_halve_mask_nunits (vec_info *, tree);
-extern tree vect_double_mask_nunits (vec_info *, tree);
+extern tree vect_halve_mask_nunits (tree, machine_mode);
+extern tree vect_double_mask_nunits (tree, machine_mode);
 extern void vect_record_loop_mask (loop_vec_info, vec_loop_masks *,
                                   unsigned int, tree, tree);
 extern tree vect_get_loop_mask (gimple_stmt_iterator *, vec_loop_masks *,
Index: gcc/tree-vect-loop.c
===================================================================
--- gcc/tree-vect-loop.c        2019-10-23 11:29:46.721892339 +0100
+++ gcc/tree-vect-loop.c        2019-10-23 12:07:54.497664026 +0100
@@ -7746,22 +7746,24 @@ loop_niters_no_overflow (loop_vec_info l
   return false;
 }
 
-/* Return a mask type with half the number of elements as TYPE.  */
+/* Return a mask type with half the number of elements as OLD_TYPE,
+   given that it should have mode NEW_MODE.  */
 
 tree
-vect_halve_mask_nunits (vec_info *vinfo, tree type)
+vect_halve_mask_nunits (tree old_type, machine_mode new_mode)
 {
-  poly_uint64 nunits = exact_div (TYPE_VECTOR_SUBPARTS (type), 2);
-  return build_truth_vector_type (nunits, vinfo->vector_size);
+  poly_uint64 nunits = exact_div (TYPE_VECTOR_SUBPARTS (old_type), 2);
+  return build_truth_vector_type_for_mode (nunits, new_mode);
 }
 
-/* Return a mask type with twice as many elements as TYPE.  */
+/* Return a mask type with twice as many elements as OLD_TYPE,
+   given that it should have mode NEW_MODE.  */
 
 tree
-vect_double_mask_nunits (vec_info *vinfo, tree type)
+vect_double_mask_nunits (tree old_type, machine_mode new_mode)
 {
-  poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type) * 2;
-  return build_truth_vector_type (nunits, vinfo->vector_size);
+  poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (old_type) * 2;
+  return build_truth_vector_type_for_mode (nunits, new_mode);
 }
 
 /* Record that a fully-masked version of LOOP_VINFO would need MASKS to
Index: gcc/tree-vect-loop-manip.c
===================================================================
--- gcc/tree-vect-loop-manip.c  2019-10-21 07:41:22.533961407 +0100
+++ gcc/tree-vect-loop-manip.c  2019-10-23 12:07:54.493664054 +0100
@@ -47,6 +47,9 @@ Software Foundation; either version 3, o
 #include "stor-layout.h"
 #include "optabs-query.h"
 #include "vec-perm-indices.h"
+#include "insn-config.h"
+#include "rtl.h"
+#include "recog.h"
 
 /*************************************************************************
   Simple Loop Peeling Utilities
@@ -317,20 +320,24 @@ interleave_supported_p (vec_perm_indices
    latter.  Return true on success, adding any new statements to SEQ.  */
 
 static bool
-vect_maybe_permute_loop_masks (loop_vec_info loop_vinfo, gimple_seq *seq,
-                              rgroup_masks *dest_rgm,
+vect_maybe_permute_loop_masks (gimple_seq *seq, rgroup_masks *dest_rgm,
                               rgroup_masks *src_rgm)
 {
   tree src_masktype = src_rgm->mask_type;
   tree dest_masktype = dest_rgm->mask_type;
   machine_mode src_mode = TYPE_MODE (src_masktype);
+  insn_code icode1, icode2;
   if (dest_rgm->max_nscalars_per_iter <= src_rgm->max_nscalars_per_iter
-      && optab_handler (vec_unpacku_hi_optab, src_mode) != CODE_FOR_nothing
-      && optab_handler (vec_unpacku_lo_optab, src_mode) != CODE_FOR_nothing)
+      && (icode1 = optab_handler (vec_unpacku_hi_optab,
+                                 src_mode)) != CODE_FOR_nothing
+      && (icode2 = optab_handler (vec_unpacku_lo_optab,
+                                 src_mode)) != CODE_FOR_nothing)
     {
       /* Unpacking the source masks gives at least as many mask bits as
         we need.  We can then VIEW_CONVERT any excess bits away.  */
-      tree unpack_masktype = vect_halve_mask_nunits (loop_vinfo, src_masktype);
+      machine_mode dest_mode = insn_data[icode1].operand[0].mode;
+      gcc_assert (dest_mode == insn_data[icode2].operand[0].mode);
+      tree unpack_masktype = vect_halve_mask_nunits (src_masktype, dest_mode);
       for (unsigned int i = 0; i < dest_rgm->masks.length (); ++i)
        {
          tree src = src_rgm->masks[i / 2];
@@ -690,8 +697,7 @@ vect_set_loop_condition_masked (class lo
          {
            rgroup_masks *half_rgm = &(*masks)[nmasks / 2 - 1];
            if (!half_rgm->masks.is_empty ()
-               && vect_maybe_permute_loop_masks (loop_vinfo, &header_seq,
-                                                 rgm, half_rgm))
+               && vect_maybe_permute_loop_masks (&header_seq, rgm, half_rgm))
              continue;
          }
 
Index: gcc/tree-vect-stmts.c
===================================================================
--- gcc/tree-vect-stmts.c       2019-10-23 11:33:01.572510226 +0100
+++ gcc/tree-vect-stmts.c       2019-10-23 12:07:54.497664026 +0100
@@ -3166,7 +3166,7 @@ vectorizable_bswap (stmt_vec_info stmt_i
    *CONVERT_CODE.  */
 
 static bool
-simple_integer_narrowing (vec_info *vinfo, tree vectype_out, tree vectype_in,
+simple_integer_narrowing (tree vectype_out, tree vectype_in,
                          tree_code *convert_code)
 {
   if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
@@ -3176,9 +3176,8 @@ simple_integer_narrowing (vec_info *vinf
   tree_code code;
   int multi_step_cvt = 0;
   auto_vec <tree, 8> interm_types;
-  if (!supportable_narrowing_operation (vinfo, NOP_EXPR, vectype_out,
-                                       vectype_in, &code, &multi_step_cvt,
-                                       &interm_types)
+  if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
+                                       &code, &multi_step_cvt, &interm_types)
       || multi_step_cvt)
     return false;
 
@@ -3360,7 +3359,7 @@ vectorizable_call (stmt_vec_info stmt_in
   if (cfn != CFN_LAST
       && (modifier == NONE
          || (modifier == NARROW
-             && simple_integer_narrowing (vinfo, vectype_out, vectype_in,
+             && simple_integer_narrowing (vectype_out, vectype_in,
                                           &convert_code))))
     ifn = vectorizable_internal_function (cfn, callee, vectype_out,
                                          vectype_in);
@@ -4944,8 +4943,8 @@ vectorizable_conversion (stmt_vec_info s
 
     case NARROW:
       gcc_assert (op_type == unary_op);
-      if (supportable_narrowing_operation (vinfo, code, vectype_out,
-                                          vectype_in, &code1, &multi_step_cvt,
+      if (supportable_narrowing_operation (code, vectype_out, vectype_in,
+                                          &code1, &multi_step_cvt,
                                           &interm_types))
        break;
 
@@ -4961,8 +4960,8 @@ vectorizable_conversion (stmt_vec_info s
       if (!supportable_convert_operation (code, cvt_type, vectype_in,
                                          &decl1, &codecvt1))
        goto unsupported;
-      if (supportable_narrowing_operation (vinfo, NOP_EXPR, vectype_out,
-                                          cvt_type, &code1, &multi_step_cvt,
+      if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
+                                          &code1, &multi_step_cvt,
                                           &interm_types))
        break;
       goto unsupported;
@@ -11377,7 +11376,6 @@ supportable_widening_operation (enum tre
                                 int *multi_step_cvt,
                                 vec<tree> *interm_types)
 {
-  vec_info *vinfo = stmt_info->vinfo;
   loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
   class loop *vect_loop = NULL;
   machine_mode vec_mode;
@@ -11562,11 +11560,8 @@ supportable_widening_operation (enum tre
     {
       intermediate_mode = insn_data[icode1].operand[0].mode;
       if (VECTOR_BOOLEAN_TYPE_P (prev_type))
-       {
-         intermediate_type = vect_halve_mask_nunits (vinfo, prev_type);
-         if (intermediate_mode != TYPE_MODE (intermediate_type))
-           return false;
-       }
+       intermediate_type
+         = vect_halve_mask_nunits (prev_type, intermediate_mode);
       else
        intermediate_type
          = lang_hooks.types.type_for_mode (intermediate_mode,
@@ -11642,7 +11637,7 @@ supportable_widening_operation (enum tre
    narrowing operation (short in the above example).   */
 
 bool
-supportable_narrowing_operation (vec_info *vinfo, enum tree_code code,
+supportable_narrowing_operation (enum tree_code code,
                                 tree vectype_out, tree vectype_in,
                                 enum tree_code *code1, int *multi_step_cvt,
                                  vec<tree> *interm_types)
@@ -11750,11 +11745,8 @@ supportable_narrowing_operation (vec_inf
     {
       intermediate_mode = insn_data[icode1].operand[0].mode;
       if (VECTOR_BOOLEAN_TYPE_P (prev_type))
-       {
-         intermediate_type = vect_double_mask_nunits (vinfo, prev_type);
-         if (intermediate_mode != TYPE_MODE (intermediate_type))
-           return false;
-       }
+       intermediate_type
+         = vect_double_mask_nunits (prev_type, intermediate_mode);
       else
        intermediate_type
          = lang_hooks.types.type_for_mode (intermediate_mode, uns);

Reply via email to