Index: gcc/tree-vectorizer.h
===================================================================
--- gcc/tree-vectorizer.h	2019-11-16 10:40:08.422638677 +0000
+++ gcc/tree-vectorizer.h	2019-11-29 09:11:27.781086362 +0000
@@ -1640,7 +1640,7 @@ extern tree get_related_vectype_for_scal
 						 poly_uint64 = 0);
 extern tree get_vectype_for_scalar_type (vec_info *, tree, unsigned int = 0);
 extern tree get_vectype_for_scalar_type (vec_info *, tree, slp_tree);
-extern tree get_mask_type_for_scalar_type (vec_info *, tree, slp_tree = 0);
+extern tree get_mask_type_for_scalar_type (vec_info *, tree, unsigned int = 0);
 extern tree get_same_sized_vectype (tree, tree);
 extern bool vect_chooses_same_modes_p (vec_info *, machine_mode);
 extern bool vect_get_loop_mask_type (loop_vec_info);
@@ -1693,7 +1693,7 @@ extern gcall *vect_gen_while (tree, tree
 extern tree vect_gen_while_not (gimple_seq *, tree, tree, tree);
 extern opt_result vect_get_vector_types_for_stmt (stmt_vec_info, tree *,
 						  tree *, unsigned int = 0);
-extern opt_tree vect_get_mask_type_for_stmt (stmt_vec_info, slp_tree = 0);
+extern opt_tree vect_get_mask_type_for_stmt (stmt_vec_info, unsigned int = 0);
 
 /* In tree-vect-data-refs.c.  */
 extern bool vect_can_force_dr_alignment_p (const_tree, poly_uint64);
Index: gcc/tree-vect-stmts.c
===================================================================
--- gcc/tree-vect-stmts.c	2019-11-29 09:11:24.553108756 +0000
+++ gcc/tree-vect-stmts.c	2019-11-29 09:11:27.781086362 +0000
@@ -11362,14 +11362,15 @@ get_vectype_for_scalar_type (vec_info *v
 
    Returns the mask type corresponding to a result of comparison
    of vectors of specified SCALAR_TYPE as supported by target.
-   NODE, if nonnull, is the SLP tree node that will use the returned
-   vector type.  */
+   If GROUP_SIZE is nonzero and we're performing BB vectorization,
+   make sure that the number of elements in the vector is no bigger
+   than GROUP_SIZE.  */
 
 tree
 get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
-			       slp_tree node)
+			       unsigned int group_size)
 {
-  tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
+  tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
 
   if (!vectype)
     return NULL;
@@ -12229,11 +12230,12 @@ vect_get_vector_types_for_stmt (stmt_vec
 
 /* Try to determine the correct vector type for STMT_INFO, which is a
    statement that produces a scalar boolean result.  Return the vector
-   type on success, otherwise return NULL_TREE.  NODE, if nonnull,
-   is the SLP tree node that will use the returned vector type.  */
+   type on success, otherwise return NULL_TREE.  If GROUP_SIZE is nonzero
+   and we're performing BB vectorization, make sure that the number of
+   elements in the vector is no bigger than GROUP_SIZE.  */
 
 opt_tree
-vect_get_mask_type_for_stmt (stmt_vec_info stmt_info, slp_tree node)
+vect_get_mask_type_for_stmt (stmt_vec_info stmt_info, unsigned int group_size)
 {
   vec_info *vinfo = stmt_info->vinfo;
   gimple *stmt = stmt_info->stmt;
@@ -12245,7 +12247,8 @@ vect_get_mask_type_for_stmt (stmt_vec_in
       && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt))))
     {
       scalar_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
-      mask_type = get_mask_type_for_scalar_type (vinfo, scalar_type, node);
+      mask_type = get_mask_type_for_scalar_type (vinfo, scalar_type,
+						 group_size);
 
       if (!mask_type)
 	return opt_tree::failure_at (stmt,
Index: gcc/tree-vect-slp.c
===================================================================
--- gcc/tree-vect-slp.c	2019-11-26 22:04:58.099362339 +0000
+++ gcc/tree-vect-slp.c	2019-11-29 09:11:27.777086392 +0000
@@ -2757,7 +2757,8 @@ vect_slp_analyze_node_operations_1 (vec_
   bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
   if (bb_vinfo && STMT_VINFO_VECTYPE (stmt_info) == boolean_type_node)
     {
-      tree vectype = vect_get_mask_type_for_stmt (stmt_info, node);
+      unsigned int group_size = SLP_TREE_SCALAR_STMTS (node).length ();
+      tree vectype = vect_get_mask_type_for_stmt (stmt_info, group_size);
       if (!vectype)
 	/* vect_get_mask_type_for_stmt has already explained the
 	   failure.  */
