1 /* SLP - Basic Block Vectorization
2 Copyright (C) 2007-2021 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "insn-config.h"
34 #include "recog.h" /* FIXME: for insn_data */
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "gimple-iterator.h"
39 #include "tree-vectorizer.h"
40 #include "langhooks.h"
41 #include "gimple-walk.h"
43 #include "tree-vector-builder.h"
44 #include "vec-perm-indices.h"
45 #include "gimple-fold.h"
46 #include "internal-fn.h"
47 #include "dump-context.h"
51 #include "alloc-pool.h"
53 static bool vectorizable_slp_permutation (vec_info
*, gimple_stmt_iterator
*,
54 slp_tree
, stmt_vector_for_cost
*);
55 static void vect_print_slp_tree (dump_flags_t
, dump_location_t
, slp_tree
);
57 static object_allocator
<_slp_tree
> *slp_tree_pool
;
58 static slp_tree slp_first_node
;
63 slp_tree_pool
= new object_allocator
<_slp_tree
> ("SLP nodes");
69 while (slp_first_node
)
70 delete slp_first_node
;
76 _slp_tree::operator new (size_t n
)
78 gcc_assert (n
== sizeof (_slp_tree
));
79 return slp_tree_pool
->allocate_raw ();
83 _slp_tree::operator delete (void *node
, size_t n
)
85 gcc_assert (n
== sizeof (_slp_tree
));
86 slp_tree_pool
->remove_raw (node
);
90 /* Initialize a SLP node. */
92 _slp_tree::_slp_tree ()
94 this->prev_node
= NULL
;
96 slp_first_node
->prev_node
= this;
97 this->next_node
= slp_first_node
;
98 slp_first_node
= this;
99 SLP_TREE_SCALAR_STMTS (this) = vNULL
;
100 SLP_TREE_SCALAR_OPS (this) = vNULL
;
101 SLP_TREE_VEC_STMTS (this) = vNULL
;
102 SLP_TREE_VEC_DEFS (this) = vNULL
;
103 SLP_TREE_NUMBER_OF_VEC_STMTS (this) = 0;
104 SLP_TREE_CHILDREN (this) = vNULL
;
105 SLP_TREE_LOAD_PERMUTATION (this) = vNULL
;
106 SLP_TREE_LANE_PERMUTATION (this) = vNULL
;
107 SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def
;
108 SLP_TREE_CODE (this) = ERROR_MARK
;
109 SLP_TREE_VECTYPE (this) = NULL_TREE
;
110 SLP_TREE_REPRESENTATIVE (this) = NULL
;
111 SLP_TREE_REF_COUNT (this) = 1;
113 this->max_nunits
= 1;
117 /* Tear down a SLP node. */
119 _slp_tree::~_slp_tree ()
122 this->prev_node
->next_node
= this->next_node
;
124 slp_first_node
= this->next_node
;
126 this->next_node
->prev_node
= this->prev_node
;
127 SLP_TREE_CHILDREN (this).release ();
128 SLP_TREE_SCALAR_STMTS (this).release ();
129 SLP_TREE_SCALAR_OPS (this).release ();
130 SLP_TREE_VEC_STMTS (this).release ();
131 SLP_TREE_VEC_DEFS (this).release ();
132 SLP_TREE_LOAD_PERMUTATION (this).release ();
133 SLP_TREE_LANE_PERMUTATION (this).release ();
138 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
141 vect_free_slp_tree (slp_tree node
)
146 if (--SLP_TREE_REF_COUNT (node
) != 0)
149 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
151 vect_free_slp_tree (child
);
153 /* If the node defines any SLP only patterns then those patterns are no
154 longer valid and should be removed. */
155 stmt_vec_info rep_stmt_info
= SLP_TREE_REPRESENTATIVE (node
);
156 if (rep_stmt_info
&& STMT_VINFO_SLP_VECT_ONLY_PATTERN (rep_stmt_info
))
158 stmt_vec_info stmt_info
= vect_orig_stmt (rep_stmt_info
);
159 STMT_VINFO_IN_PATTERN_P (stmt_info
) = false;
160 STMT_SLP_TYPE (stmt_info
) = STMT_SLP_TYPE (rep_stmt_info
);
166 /* Return a location suitable for dumpings related to the SLP instance. */
169 _slp_instance::location () const
171 if (!root_stmts
.is_empty ())
172 return root_stmts
[0]->stmt
;
174 return SLP_TREE_SCALAR_STMTS (root
)[0]->stmt
;
178 /* Free the memory allocated for the SLP instance. */
181 vect_free_slp_instance (slp_instance instance
)
183 vect_free_slp_tree (SLP_INSTANCE_TREE (instance
));
184 SLP_INSTANCE_LOADS (instance
).release ();
185 SLP_INSTANCE_ROOT_STMTS (instance
).release ();
186 instance
->subgraph_entries
.release ();
187 instance
->cost_vec
.release ();
192 /* Create an SLP node for SCALAR_STMTS. */
195 vect_create_new_slp_node (unsigned nops
, tree_code code
)
197 slp_tree node
= new _slp_tree
;
198 SLP_TREE_SCALAR_STMTS (node
) = vNULL
;
199 SLP_TREE_CHILDREN (node
).create (nops
);
200 SLP_TREE_DEF_TYPE (node
) = vect_internal_def
;
201 SLP_TREE_CODE (node
) = code
;
204 /* Create an SLP node for SCALAR_STMTS. */
207 vect_create_new_slp_node (slp_tree node
,
208 vec
<stmt_vec_info
> scalar_stmts
, unsigned nops
)
210 SLP_TREE_SCALAR_STMTS (node
) = scalar_stmts
;
211 SLP_TREE_CHILDREN (node
).create (nops
);
212 SLP_TREE_DEF_TYPE (node
) = vect_internal_def
;
213 SLP_TREE_REPRESENTATIVE (node
) = scalar_stmts
[0];
214 SLP_TREE_LANES (node
) = scalar_stmts
.length ();
218 /* Create an SLP node for SCALAR_STMTS. */
221 vect_create_new_slp_node (vec
<stmt_vec_info
> scalar_stmts
, unsigned nops
)
223 return vect_create_new_slp_node (new _slp_tree
, scalar_stmts
, nops
);
226 /* Create an SLP node for OPS. */
229 vect_create_new_slp_node (slp_tree node
, vec
<tree
> ops
)
231 SLP_TREE_SCALAR_OPS (node
) = ops
;
232 SLP_TREE_DEF_TYPE (node
) = vect_external_def
;
233 SLP_TREE_LANES (node
) = ops
.length ();
237 /* Create an SLP node for OPS. */
240 vect_create_new_slp_node (vec
<tree
> ops
)
242 return vect_create_new_slp_node (new _slp_tree
, ops
);
246 /* This structure is used in creation of an SLP tree. Each instance
247 corresponds to the same operand in a group of scalar stmts in an SLP
249 typedef struct _slp_oprnd_info
251 /* Def-stmts for the operands. */
252 vec
<stmt_vec_info
> def_stmts
;
255 /* Information about the first statement, its vector def-type, type, the
256 operand itself in case it's constant, and an indication if it's a pattern
259 enum vect_def_type first_dt
;
264 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
266 static vec
<slp_oprnd_info
>
267 vect_create_oprnd_info (int nops
, int group_size
)
270 slp_oprnd_info oprnd_info
;
271 vec
<slp_oprnd_info
> oprnds_info
;
273 oprnds_info
.create (nops
);
274 for (i
= 0; i
< nops
; i
++)
276 oprnd_info
= XNEW (struct _slp_oprnd_info
);
277 oprnd_info
->def_stmts
.create (group_size
);
278 oprnd_info
->ops
.create (group_size
);
279 oprnd_info
->first_dt
= vect_uninitialized_def
;
280 oprnd_info
->first_op_type
= NULL_TREE
;
281 oprnd_info
->any_pattern
= false;
282 oprnds_info
.quick_push (oprnd_info
);
289 /* Free operands info. */
292 vect_free_oprnd_info (vec
<slp_oprnd_info
> &oprnds_info
)
295 slp_oprnd_info oprnd_info
;
297 FOR_EACH_VEC_ELT (oprnds_info
, i
, oprnd_info
)
299 oprnd_info
->def_stmts
.release ();
300 oprnd_info
->ops
.release ();
301 XDELETE (oprnd_info
);
304 oprnds_info
.release ();
308 /* Return true if STMTS contains a pattern statement. */
311 vect_contains_pattern_stmt_p (vec
<stmt_vec_info
> stmts
)
313 stmt_vec_info stmt_info
;
315 FOR_EACH_VEC_ELT (stmts
, i
, stmt_info
)
316 if (is_pattern_stmt_p (stmt_info
))
321 /* Return true when all lanes in the external or constant NODE have
325 vect_slp_tree_uniform_p (slp_tree node
)
327 gcc_assert (SLP_TREE_DEF_TYPE (node
) == vect_constant_def
328 || SLP_TREE_DEF_TYPE (node
) == vect_external_def
);
330 /* Pre-exsting vectors. */
331 if (SLP_TREE_SCALAR_OPS (node
).is_empty ())
335 tree op
, first
= NULL_TREE
;
336 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node
), i
, op
)
339 else if (!operand_equal_p (first
, op
, 0))
345 /* Find the place of the data-ref in STMT_INFO in the interleaving chain
346 that starts from FIRST_STMT_INFO. Return -1 if the data-ref is not a part
350 vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info
,
351 stmt_vec_info first_stmt_info
)
353 stmt_vec_info next_stmt_info
= first_stmt_info
;
356 if (first_stmt_info
!= DR_GROUP_FIRST_ELEMENT (stmt_info
))
361 if (next_stmt_info
== stmt_info
)
363 next_stmt_info
= DR_GROUP_NEXT_ELEMENT (next_stmt_info
);
365 result
+= DR_GROUP_GAP (next_stmt_info
);
367 while (next_stmt_info
);
372 /* Check whether it is possible to load COUNT elements of type ELT_TYPE
373 using the method implemented by duplicate_and_interleave. Return true
374 if so, returning the number of intermediate vectors in *NVECTORS_OUT
375 (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
379 can_duplicate_and_interleave_p (vec_info
*vinfo
, unsigned int count
,
380 tree elt_type
, unsigned int *nvectors_out
,
381 tree
*vector_type_out
,
384 tree base_vector_type
= get_vectype_for_scalar_type (vinfo
, elt_type
, count
);
385 if (!base_vector_type
|| !VECTOR_MODE_P (TYPE_MODE (base_vector_type
)))
388 machine_mode base_vector_mode
= TYPE_MODE (base_vector_type
);
389 poly_int64 elt_bytes
= count
* GET_MODE_UNIT_SIZE (base_vector_mode
);
390 unsigned int nvectors
= 1;
393 scalar_int_mode int_mode
;
394 poly_int64 elt_bits
= elt_bytes
* BITS_PER_UNIT
;
395 if (int_mode_for_size (elt_bits
, 1).exists (&int_mode
))
397 /* Get the natural vector type for this SLP group size. */
398 tree int_type
= build_nonstandard_integer_type
399 (GET_MODE_BITSIZE (int_mode
), 1);
401 = get_vectype_for_scalar_type (vinfo
, int_type
, count
);
403 && VECTOR_MODE_P (TYPE_MODE (vector_type
))
404 && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type
)),
405 GET_MODE_SIZE (base_vector_mode
)))
407 /* Try fusing consecutive sequences of COUNT / NVECTORS elements
408 together into elements of type INT_TYPE and using the result
409 to build NVECTORS vectors. */
410 poly_uint64 nelts
= GET_MODE_NUNITS (TYPE_MODE (vector_type
));
411 vec_perm_builder
sel1 (nelts
, 2, 3);
412 vec_perm_builder
sel2 (nelts
, 2, 3);
413 poly_int64 half_nelts
= exact_div (nelts
, 2);
414 for (unsigned int i
= 0; i
< 3; ++i
)
417 sel1
.quick_push (i
+ nelts
);
418 sel2
.quick_push (half_nelts
+ i
);
419 sel2
.quick_push (half_nelts
+ i
+ nelts
);
421 vec_perm_indices
indices1 (sel1
, 2, nelts
);
422 vec_perm_indices
indices2 (sel2
, 2, nelts
);
423 if (can_vec_perm_const_p (TYPE_MODE (vector_type
), indices1
)
424 && can_vec_perm_const_p (TYPE_MODE (vector_type
), indices2
))
427 *nvectors_out
= nvectors
;
429 *vector_type_out
= vector_type
;
432 permutes
[0] = vect_gen_perm_mask_checked (vector_type
,
434 permutes
[1] = vect_gen_perm_mask_checked (vector_type
,
441 if (!multiple_p (elt_bytes
, 2, &elt_bytes
))
447 /* Return true if DTA and DTB match. */
450 vect_def_types_match (enum vect_def_type dta
, enum vect_def_type dtb
)
453 || ((dta
== vect_external_def
|| dta
== vect_constant_def
)
454 && (dtb
== vect_external_def
|| dtb
== vect_constant_def
)));
457 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
458 they are of a valid type and that they match the defs of the first stmt of
459 the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
460 by swapping operands of STMTS[STMT_NUM] when possible. Non-zero *SWAP
461 indicates swap is required for cond_expr stmts. Specifically, *SWAP
462 is 1 if STMT is cond and operands of comparison need to be swapped;
463 *SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
464 If there is any operand swap in this function, *SWAP is set to non-zero
466 If there was a fatal error return -1; if the error could be corrected by
467 swapping operands of father node of this one, return 1; if everything is
470 vect_get_and_check_slp_defs (vec_info
*vinfo
, unsigned char swap
,
472 vec
<stmt_vec_info
> stmts
, unsigned stmt_num
,
473 vec
<slp_oprnd_info
> *oprnds_info
)
475 stmt_vec_info stmt_info
= stmts
[stmt_num
];
477 unsigned int i
, number_of_oprnds
;
478 enum vect_def_type dt
= vect_uninitialized_def
;
479 slp_oprnd_info oprnd_info
;
480 int first_op_idx
= 1;
481 unsigned int commutative_op
= -1U;
482 bool first_op_cond
= false;
483 bool first
= stmt_num
== 0;
485 if (gcall
*stmt
= dyn_cast
<gcall
*> (stmt_info
->stmt
))
487 number_of_oprnds
= gimple_call_num_args (stmt
);
489 if (gimple_call_internal_p (stmt
))
491 internal_fn ifn
= gimple_call_internal_fn (stmt
);
492 commutative_op
= first_commutative_argument (ifn
);
494 /* Masked load, only look at mask. */
495 if (ifn
== IFN_MASK_LOAD
)
497 number_of_oprnds
= 1;
498 /* Mask operand index. */
503 else if (gassign
*stmt
= dyn_cast
<gassign
*> (stmt_info
->stmt
))
505 enum tree_code code
= gimple_assign_rhs_code (stmt
);
506 number_of_oprnds
= gimple_num_ops (stmt
) - 1;
507 /* Swap can only be done for cond_expr if asked to, otherwise we
508 could result in different comparison code to the first stmt. */
509 if (code
== COND_EXPR
510 && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt
)))
512 first_op_cond
= true;
516 commutative_op
= commutative_tree_code (code
) ? 0U : -1U;
518 else if (gphi
*stmt
= dyn_cast
<gphi
*> (stmt_info
->stmt
))
519 number_of_oprnds
= gimple_phi_num_args (stmt
);
523 bool swapped
= (swap
!= 0);
524 bool backedge
= false;
525 gcc_assert (!swapped
|| first_op_cond
);
526 enum vect_def_type
*dts
= XALLOCAVEC (enum vect_def_type
, number_of_oprnds
);
527 for (i
= 0; i
< number_of_oprnds
; i
++)
531 /* Map indicating how operands of cond_expr should be swapped. */
532 int maps
[3][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 1, 3, 2}};
533 int *map
= maps
[swap
];
536 oprnd
= TREE_OPERAND (gimple_op (stmt_info
->stmt
,
537 first_op_idx
), map
[i
]);
539 oprnd
= gimple_op (stmt_info
->stmt
, map
[i
]);
541 else if (gphi
*stmt
= dyn_cast
<gphi
*> (stmt_info
->stmt
))
543 oprnd
= gimple_phi_arg_def (stmt
, i
);
544 backedge
= dominated_by_p (CDI_DOMINATORS
,
545 gimple_phi_arg_edge (stmt
, i
)->src
,
546 gimple_bb (stmt_info
->stmt
));
549 oprnd
= gimple_op (stmt_info
->stmt
, first_op_idx
+ (swapped
? !i
: i
));
550 if (TREE_CODE (oprnd
) == VIEW_CONVERT_EXPR
)
551 oprnd
= TREE_OPERAND (oprnd
, 0);
553 oprnd_info
= (*oprnds_info
)[i
];
555 stmt_vec_info def_stmt_info
;
556 if (!vect_is_simple_use (oprnd
, vinfo
, &dts
[i
], &def_stmt_info
))
558 if (dump_enabled_p ())
559 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
560 "Build SLP failed: can't analyze def for %T\n",
568 oprnd_info
->def_stmts
.quick_push (NULL
);
569 oprnd_info
->ops
.quick_push (NULL_TREE
);
570 oprnd_info
->first_dt
= vect_uninitialized_def
;
574 oprnd_info
->def_stmts
.quick_push (def_stmt_info
);
575 oprnd_info
->ops
.quick_push (oprnd
);
578 && is_pattern_stmt_p (def_stmt_info
))
580 if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info
))
582 oprnd_info
->any_pattern
= true;
584 /* If we promote this to external use the original stmt def. */
585 oprnd_info
->ops
.last ()
586 = gimple_get_lhs (vect_orig_stmt (def_stmt_info
)->stmt
);
589 /* If there's a extern def on a backedge make sure we can
590 code-generate at the region start.
591 ??? This is another case that could be fixed by adjusting
592 how we split the function but at the moment we'd have conflicting
595 && dts
[i
] == vect_external_def
596 && is_a
<bb_vec_info
> (vinfo
)
597 && TREE_CODE (oprnd
) == SSA_NAME
598 && !SSA_NAME_IS_DEFAULT_DEF (oprnd
)
599 && !dominated_by_p (CDI_DOMINATORS
,
600 as_a
<bb_vec_info
> (vinfo
)->bbs
[0],
601 gimple_bb (SSA_NAME_DEF_STMT (oprnd
))))
603 if (dump_enabled_p ())
604 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
605 "Build SLP failed: extern def %T only defined "
606 "on backedge\n", oprnd
);
612 tree type
= TREE_TYPE (oprnd
);
614 if ((dt
== vect_constant_def
615 || dt
== vect_external_def
)
616 && !GET_MODE_SIZE (vinfo
->vector_mode
).is_constant ()
617 && (TREE_CODE (type
) == BOOLEAN_TYPE
618 || !can_duplicate_and_interleave_p (vinfo
, stmts
.length (),
621 if (dump_enabled_p ())
622 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
623 "Build SLP failed: invalid type of def "
624 "for variable-length SLP %T\n", oprnd
);
628 /* For the swapping logic below force vect_reduction_def
629 for the reduction op in a SLP reduction group. */
630 if (!STMT_VINFO_DATA_REF (stmt_info
)
631 && REDUC_GROUP_FIRST_ELEMENT (stmt_info
)
632 && (int)i
== STMT_VINFO_REDUC_IDX (stmt_info
)
634 dts
[i
] = dt
= vect_reduction_def
;
636 /* Check the types of the definition. */
639 case vect_external_def
:
640 case vect_constant_def
:
641 case vect_internal_def
:
642 case vect_reduction_def
:
643 case vect_induction_def
:
644 case vect_nested_cycle
:
648 /* FORNOW: Not supported. */
649 if (dump_enabled_p ())
650 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
651 "Build SLP failed: illegal type of def %T\n",
656 oprnd_info
->first_dt
= dt
;
657 oprnd_info
->first_op_type
= type
;
663 /* Now match the operand definition types to that of the first stmt. */
664 for (i
= 0; i
< number_of_oprnds
;)
672 oprnd_info
= (*oprnds_info
)[i
];
674 stmt_vec_info def_stmt_info
= oprnd_info
->def_stmts
[stmt_num
];
675 oprnd
= oprnd_info
->ops
[stmt_num
];
676 tree type
= TREE_TYPE (oprnd
);
678 if (!types_compatible_p (oprnd_info
->first_op_type
, type
))
680 if (dump_enabled_p ())
681 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
682 "Build SLP failed: different operand types\n");
686 /* Not first stmt of the group, check that the def-stmt/s match
687 the def-stmt/s of the first stmt. Allow different definition
688 types for reduction chains: the first stmt must be a
689 vect_reduction_def (a phi node), and the rest
690 end in the reduction chain. */
691 if ((!vect_def_types_match (oprnd_info
->first_dt
, dt
)
692 && !(oprnd_info
->first_dt
== vect_reduction_def
693 && !STMT_VINFO_DATA_REF (stmt_info
)
694 && REDUC_GROUP_FIRST_ELEMENT (stmt_info
)
696 && !STMT_VINFO_DATA_REF (def_stmt_info
)
697 && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info
)
698 == REDUC_GROUP_FIRST_ELEMENT (stmt_info
))))
699 || (!STMT_VINFO_DATA_REF (stmt_info
)
700 && REDUC_GROUP_FIRST_ELEMENT (stmt_info
)
702 || STMT_VINFO_DATA_REF (def_stmt_info
)
703 || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info
)
704 != REDUC_GROUP_FIRST_ELEMENT (stmt_info
)))
705 != (oprnd_info
->first_dt
!= vect_reduction_def
))))
707 /* Try swapping operands if we got a mismatch. For BB
708 vectorization only in case it will clearly improve things. */
709 if (i
== commutative_op
&& !swapped
710 && (!is_a
<bb_vec_info
> (vinfo
)
711 || (!vect_def_types_match ((*oprnds_info
)[i
+1]->first_dt
,
713 && (vect_def_types_match (oprnd_info
->first_dt
, dts
[i
+1])
714 || vect_def_types_match
715 ((*oprnds_info
)[i
+1]->first_dt
, dts
[i
])))))
717 if (dump_enabled_p ())
718 dump_printf_loc (MSG_NOTE
, vect_location
,
719 "trying swapped operands\n");
720 std::swap (dts
[i
], dts
[i
+1]);
721 std::swap ((*oprnds_info
)[i
]->def_stmts
[stmt_num
],
722 (*oprnds_info
)[i
+1]->def_stmts
[stmt_num
]);
723 std::swap ((*oprnds_info
)[i
]->ops
[stmt_num
],
724 (*oprnds_info
)[i
+1]->ops
[stmt_num
]);
729 if (is_a
<bb_vec_info
> (vinfo
)
730 && !oprnd_info
->any_pattern
)
732 /* Now for commutative ops we should see whether we can
733 make the other operand matching. */
734 if (dump_enabled_p ())
735 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
736 "treating operand as external\n");
737 oprnd_info
->first_dt
= dt
= vect_external_def
;
741 if (dump_enabled_p ())
742 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
743 "Build SLP failed: different types\n");
748 /* Make sure to demote the overall operand to external. */
749 if (dt
== vect_external_def
)
750 oprnd_info
->first_dt
= vect_external_def
;
751 /* For a SLP reduction chain we want to duplicate the reduction to
752 each of the chain members. That gets us a sane SLP graph (still
753 the stmts are not 100% correct wrt the initial values). */
754 else if ((dt
== vect_internal_def
755 || dt
== vect_reduction_def
)
756 && oprnd_info
->first_dt
== vect_reduction_def
757 && !STMT_VINFO_DATA_REF (stmt_info
)
758 && REDUC_GROUP_FIRST_ELEMENT (stmt_info
)
759 && !STMT_VINFO_DATA_REF (def_stmt_info
)
760 && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info
)
761 == REDUC_GROUP_FIRST_ELEMENT (stmt_info
)))
763 oprnd_info
->def_stmts
[stmt_num
] = oprnd_info
->def_stmts
[0];
764 oprnd_info
->ops
[stmt_num
] = oprnd_info
->ops
[0];
773 if (dump_enabled_p ())
774 dump_printf_loc (MSG_NOTE
, vect_location
,
775 "swapped operands to match def types in %G",
782 /* Return true if call statements CALL1 and CALL2 are similar enough
783 to be combined into the same SLP group. */
786 compatible_calls_p (gcall
*call1
, gcall
*call2
)
788 unsigned int nargs
= gimple_call_num_args (call1
);
789 if (nargs
!= gimple_call_num_args (call2
))
792 if (gimple_call_combined_fn (call1
) != gimple_call_combined_fn (call2
))
795 if (gimple_call_internal_p (call1
))
797 if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1
)),
798 TREE_TYPE (gimple_call_lhs (call2
))))
800 for (unsigned int i
= 0; i
< nargs
; ++i
)
801 if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1
, i
)),
802 TREE_TYPE (gimple_call_arg (call2
, i
))))
807 if (!operand_equal_p (gimple_call_fn (call1
),
808 gimple_call_fn (call2
), 0))
811 if (gimple_call_fntype (call1
) != gimple_call_fntype (call2
))
817 /* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
818 caller's attempt to find the vector type in STMT_INFO with the narrowest
819 element type. Return true if VECTYPE is nonnull and if it is valid
820 for STMT_INFO. When returning true, update MAX_NUNITS to reflect the
821 number of units in VECTYPE. GROUP_SIZE and MAX_NUNITS are as for
822 vect_build_slp_tree. */
825 vect_record_max_nunits (vec_info
*vinfo
, stmt_vec_info stmt_info
,
826 unsigned int group_size
,
827 tree vectype
, poly_uint64
*max_nunits
)
831 if (dump_enabled_p ())
832 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
833 "Build SLP failed: unsupported data-type in %G\n",
835 /* Fatal mismatch. */
839 /* If populating the vector type requires unrolling then fail
840 before adjusting *max_nunits for basic-block vectorization. */
841 if (is_a
<bb_vec_info
> (vinfo
)
842 && !multiple_p (group_size
, TYPE_VECTOR_SUBPARTS (vectype
)))
844 if (dump_enabled_p ())
845 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
846 "Build SLP failed: unrolling required "
847 "in basic block SLP\n");
848 /* Fatal mismatch. */
852 /* In case of multiple types we need to detect the smallest type. */
853 vect_update_max_nunits (max_nunits
, vectype
);
857 /* Verify if the scalar stmts STMTS are isomorphic, require data
858 permutation or are of unsupported types of operation. Return
859 true if they are, otherwise return false and indicate in *MATCHES
860 which stmts are not isomorphic to the first one. If MATCHES[0]
861 is false then this indicates the comparison could not be
862 carried out or the stmts will never be vectorized by SLP.
864 Note COND_EXPR is possibly isomorphic to another one after swapping its
865 operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
866 the first stmt by swapping the two operands of comparison; set SWAP[i]
867 to 2 if stmt I is isormorphic to the first stmt by inverting the code
868 of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
869 to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
872 vect_build_slp_tree_1 (vec_info
*vinfo
, unsigned char *swap
,
873 vec
<stmt_vec_info
> stmts
, unsigned int group_size
,
874 poly_uint64
*max_nunits
, bool *matches
,
875 bool *two_operators
, tree
*node_vectype
)
878 stmt_vec_info first_stmt_info
= stmts
[0];
879 enum tree_code first_stmt_code
= ERROR_MARK
;
880 enum tree_code alt_stmt_code
= ERROR_MARK
;
881 enum tree_code rhs_code
= ERROR_MARK
;
882 enum tree_code first_cond_code
= ERROR_MARK
;
884 bool need_same_oprnds
= false;
885 tree vectype
= NULL_TREE
, first_op1
= NULL_TREE
;
888 machine_mode optab_op2_mode
;
889 machine_mode vec_mode
;
890 stmt_vec_info first_load
= NULL
, prev_first_load
= NULL
;
891 bool first_stmt_load_p
= false, load_p
= false;
892 bool first_stmt_phi_p
= false, phi_p
= false;
893 bool maybe_soft_fail
= false;
894 tree soft_fail_nunits_vectype
= NULL_TREE
;
896 /* For every stmt in NODE find its def stmt/s. */
897 stmt_vec_info stmt_info
;
898 FOR_EACH_VEC_ELT (stmts
, i
, stmt_info
)
900 gimple
*stmt
= stmt_info
->stmt
;
904 if (dump_enabled_p ())
905 dump_printf_loc (MSG_NOTE
, vect_location
, "Build SLP for %G", stmt
);
907 /* Fail to vectorize statements marked as unvectorizable, throw
909 if (!STMT_VINFO_VECTORIZABLE (stmt_info
)
910 || stmt_can_throw_internal (cfun
, stmt
)
911 || gimple_has_volatile_ops (stmt
))
913 if (dump_enabled_p ())
914 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
915 "Build SLP failed: unvectorizable statement %G",
917 /* ??? For BB vectorization we want to commutate operands in a way
918 to shuffle all unvectorizable defs into one operand and have
919 the other still vectorized. The following doesn't reliably
920 work for this though but it's the easiest we can do here. */
921 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
923 /* Fatal mismatch. */
928 lhs
= gimple_get_lhs (stmt
);
929 if (lhs
== NULL_TREE
)
931 if (dump_enabled_p ())
932 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
933 "Build SLP failed: not GIMPLE_ASSIGN nor "
934 "GIMPLE_CALL %G", stmt
);
935 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
937 /* Fatal mismatch. */
943 if (!vect_get_vector_types_for_stmt (vinfo
, stmt_info
, &vectype
,
944 &nunits_vectype
, group_size
))
946 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
948 /* Fatal mismatch. */
952 /* Record nunits required but continue analysis, producing matches[]
953 as if nunits was not an issue. This allows splitting of groups
956 && !vect_record_max_nunits (vinfo
, stmt_info
, group_size
,
957 nunits_vectype
, max_nunits
))
959 gcc_assert (is_a
<bb_vec_info
> (vinfo
));
960 maybe_soft_fail
= true;
961 soft_fail_nunits_vectype
= nunits_vectype
;
964 gcc_assert (vectype
);
966 gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
);
969 rhs_code
= CALL_EXPR
;
971 if (gimple_call_internal_p (stmt
, IFN_MASK_LOAD
))
973 else if ((gimple_call_internal_p (call_stmt
)
974 && (!vectorizable_internal_fn_p
975 (gimple_call_internal_fn (call_stmt
))))
976 || gimple_call_tail_p (call_stmt
)
977 || gimple_call_noreturn_p (call_stmt
)
978 || gimple_call_chain (call_stmt
))
980 if (dump_enabled_p ())
981 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
982 "Build SLP failed: unsupported call type %G",
984 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
986 /* Fatal mismatch. */
991 else if (gimple_code (stmt
) == GIMPLE_PHI
)
993 rhs_code
= ERROR_MARK
;
998 rhs_code
= gimple_assign_rhs_code (stmt
);
999 load_p
= gimple_vuse (stmt
);
1002 /* Check the operation. */
1005 *node_vectype
= vectype
;
1006 first_stmt_code
= rhs_code
;
1007 first_stmt_load_p
= load_p
;
1008 first_stmt_phi_p
= phi_p
;
1010 /* Shift arguments should be equal in all the packed stmts for a
1011 vector shift with scalar shift operand. */
1012 if (rhs_code
== LSHIFT_EXPR
|| rhs_code
== RSHIFT_EXPR
1013 || rhs_code
== LROTATE_EXPR
1014 || rhs_code
== RROTATE_EXPR
)
1016 vec_mode
= TYPE_MODE (vectype
);
1018 /* First see if we have a vector/vector shift. */
1019 optab
= optab_for_tree_code (rhs_code
, vectype
,
1023 || optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
1025 /* No vector/vector shift, try for a vector/scalar shift. */
1026 optab
= optab_for_tree_code (rhs_code
, vectype
,
1031 if (dump_enabled_p ())
1032 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1033 "Build SLP failed: no optab.\n");
1034 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
1036 /* Fatal mismatch. */
1040 icode
= (int) optab_handler (optab
, vec_mode
);
1041 if (icode
== CODE_FOR_nothing
)
1043 if (dump_enabled_p ())
1044 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1045 "Build SLP failed: "
1046 "op not supported by target.\n");
1047 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
1049 /* Fatal mismatch. */
1053 optab_op2_mode
= insn_data
[icode
].operand
[2].mode
;
1054 if (!VECTOR_MODE_P (optab_op2_mode
))
1056 need_same_oprnds
= true;
1057 first_op1
= gimple_assign_rhs2 (stmt
);
1061 else if (rhs_code
== WIDEN_LSHIFT_EXPR
)
1063 need_same_oprnds
= true;
1064 first_op1
= gimple_assign_rhs2 (stmt
);
1067 && rhs_code
== BIT_FIELD_REF
)
1069 tree vec
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
1070 if (!is_a
<bb_vec_info
> (vinfo
)
1071 || TREE_CODE (vec
) != SSA_NAME
1072 || !operand_equal_p (TYPE_SIZE (vectype
),
1073 TYPE_SIZE (TREE_TYPE (vec
))))
1075 if (dump_enabled_p ())
1076 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1077 "Build SLP failed: "
1078 "BIT_FIELD_REF not supported\n");
1079 /* Fatal mismatch. */
1085 && gimple_call_internal_p (call_stmt
, IFN_DIV_POW2
))
1087 need_same_oprnds
= true;
1088 first_op1
= gimple_call_arg (call_stmt
, 1);
1093 if (first_stmt_code
!= rhs_code
1094 && alt_stmt_code
== ERROR_MARK
)
1095 alt_stmt_code
= rhs_code
;
1096 if ((first_stmt_code
!= rhs_code
1097 && (first_stmt_code
!= IMAGPART_EXPR
1098 || rhs_code
!= REALPART_EXPR
)
1099 && (first_stmt_code
!= REALPART_EXPR
1100 || rhs_code
!= IMAGPART_EXPR
)
1101 /* Handle mismatches in plus/minus by computing both
1102 and merging the results. */
1103 && !((first_stmt_code
== PLUS_EXPR
1104 || first_stmt_code
== MINUS_EXPR
)
1105 && (alt_stmt_code
== PLUS_EXPR
1106 || alt_stmt_code
== MINUS_EXPR
)
1107 && rhs_code
== alt_stmt_code
)
1108 && !(STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1109 && (first_stmt_code
== ARRAY_REF
1110 || first_stmt_code
== BIT_FIELD_REF
1111 || first_stmt_code
== INDIRECT_REF
1112 || first_stmt_code
== COMPONENT_REF
1113 || first_stmt_code
== MEM_REF
)))
1114 || first_stmt_load_p
!= load_p
1115 || first_stmt_phi_p
!= phi_p
)
1117 if (dump_enabled_p ())
1119 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1120 "Build SLP failed: different operation "
1121 "in stmt %G", stmt
);
1122 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1123 "original stmt %G", first_stmt_info
->stmt
);
1130 && first_stmt_code
== BIT_FIELD_REF
1131 && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info
->stmt
), 0)
1132 != TREE_OPERAND (gimple_assign_rhs1 (stmt_info
->stmt
), 0)))
1134 if (dump_enabled_p ())
1135 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1136 "Build SLP failed: different BIT_FIELD_REF "
1137 "arguments in %G", stmt
);
1142 if (!load_p
&& rhs_code
== CALL_EXPR
)
1144 if (!compatible_calls_p (as_a
<gcall
*> (stmts
[0]->stmt
),
1145 as_a
<gcall
*> (stmt
)))
1147 if (dump_enabled_p ())
1148 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1149 "Build SLP failed: different calls in %G",
1156 if ((phi_p
|| gimple_could_trap_p (stmt_info
->stmt
))
1157 && (gimple_bb (first_stmt_info
->stmt
)
1158 != gimple_bb (stmt_info
->stmt
)))
1160 if (dump_enabled_p ())
1161 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1162 "Build SLP failed: different BB for PHI "
1163 "or possibly trapping operation in %G", stmt
);
1168 if (need_same_oprnds
)
1170 tree other_op1
= (call_stmt
1171 ? gimple_call_arg (call_stmt
, 1)
1172 : gimple_assign_rhs2 (stmt
));
1173 if (!operand_equal_p (first_op1
, other_op1
, 0))
1175 if (dump_enabled_p ())
1176 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1177 "Build SLP failed: different shift "
1178 "arguments in %G", stmt
);
1184 if (!types_compatible_p (vectype
, *node_vectype
))
1186 if (dump_enabled_p ())
1187 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1188 "Build SLP failed: different vector type "
1195 /* Grouped store or load. */
1196 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
1198 if (REFERENCE_CLASS_P (lhs
))
1206 first_load
= DR_GROUP_FIRST_ELEMENT (stmt_info
);
1207 if (prev_first_load
)
1209 /* Check that there are no loads from different interleaving
1210 chains in the same node. */
1211 if (prev_first_load
!= first_load
)
1213 if (dump_enabled_p ())
1214 dump_printf_loc (MSG_MISSED_OPTIMIZATION
,
1216 "Build SLP failed: different "
1217 "interleaving chains in one node %G",
1224 prev_first_load
= first_load
;
1226 } /* Grouped access. */
1231 /* Not grouped load. */
1232 if (dump_enabled_p ())
1233 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1234 "Build SLP failed: not grouped load %G", stmt
);
1236 /* FORNOW: Not grouped loads are not supported. */
1237 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
1239 /* Fatal mismatch. */
1244 /* Not memory operation. */
1246 && TREE_CODE_CLASS (rhs_code
) != tcc_binary
1247 && TREE_CODE_CLASS (rhs_code
) != tcc_unary
1248 && TREE_CODE_CLASS (rhs_code
) != tcc_expression
1249 && TREE_CODE_CLASS (rhs_code
) != tcc_comparison
1250 && rhs_code
!= VIEW_CONVERT_EXPR
1251 && rhs_code
!= CALL_EXPR
1252 && rhs_code
!= BIT_FIELD_REF
)
1254 if (dump_enabled_p ())
1255 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1256 "Build SLP failed: operation unsupported %G",
1258 if (is_a
<bb_vec_info
> (vinfo
) && i
!= 0)
1260 /* Fatal mismatch. */
1265 if (rhs_code
== COND_EXPR
)
1267 tree cond_expr
= gimple_assign_rhs1 (stmt
);
1268 enum tree_code cond_code
= TREE_CODE (cond_expr
);
1269 enum tree_code swap_code
= ERROR_MARK
;
1270 enum tree_code invert_code
= ERROR_MARK
;
1273 first_cond_code
= TREE_CODE (cond_expr
);
1274 else if (TREE_CODE_CLASS (cond_code
) == tcc_comparison
)
1276 bool honor_nans
= HONOR_NANS (TREE_OPERAND (cond_expr
, 0));
1277 swap_code
= swap_tree_comparison (cond_code
);
1278 invert_code
= invert_tree_comparison (cond_code
, honor_nans
);
1281 if (first_cond_code
== cond_code
)
1283 /* Isomorphic can be achieved by swapping. */
1284 else if (first_cond_code
== swap_code
)
1286 /* Isomorphic can be achieved by inverting. */
1287 else if (first_cond_code
== invert_code
)
1291 if (dump_enabled_p ())
1292 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1293 "Build SLP failed: different"
1294 " operation %G", stmt
);
1304 for (i
= 0; i
< group_size
; ++i
)
1308 /* If we allowed a two-operation SLP node verify the target can cope
1309 with the permute we are going to use. */
1310 if (alt_stmt_code
!= ERROR_MARK
1311 && TREE_CODE_CLASS (alt_stmt_code
) != tcc_reference
)
1313 *two_operators
= true;
1316 if (maybe_soft_fail
)
1318 unsigned HOST_WIDE_INT const_nunits
;
1319 if (!TYPE_VECTOR_SUBPARTS
1320 (soft_fail_nunits_vectype
).is_constant (&const_nunits
)
1321 || const_nunits
> group_size
)
1325 /* With constant vector elements simulate a mismatch at the
1326 point we need to split. */
1327 unsigned tail
= group_size
& (const_nunits
- 1);
1328 memset (&matches
[group_size
- tail
], 0, sizeof (bool) * tail
);
1336 /* Traits for the hash_set to record failed SLP builds for a stmt set.
1337 Note we never remove apart from at destruction time so we do not
1338 need a special value for deleted that differs from empty. */
1341 typedef vec
<stmt_vec_info
> value_type
;
1342 typedef vec
<stmt_vec_info
> compare_type
;
1343 static inline hashval_t
hash (value_type
);
1344 static inline bool equal (value_type existing
, value_type candidate
);
1345 static inline bool is_empty (value_type x
) { return !x
.exists (); }
1346 static inline bool is_deleted (value_type x
) { return !x
.exists (); }
1347 static const bool empty_zero_p
= true;
1348 static inline void mark_empty (value_type
&x
) { x
.release (); }
1349 static inline void mark_deleted (value_type
&x
) { x
.release (); }
1350 static inline void remove (value_type
&x
) { x
.release (); }
1353 bst_traits::hash (value_type x
)
1356 for (unsigned i
= 0; i
< x
.length (); ++i
)
1357 h
.add_int (gimple_uid (x
[i
]->stmt
));
1361 bst_traits::equal (value_type existing
, value_type candidate
)
1363 if (existing
.length () != candidate
.length ())
1365 for (unsigned i
= 0; i
< existing
.length (); ++i
)
1366 if (existing
[i
] != candidate
[i
])
1371 /* ??? This was std::pair<std::pair<tree_code, vect_def_type>, tree>
1372 but then vec::insert does memmove and that's not compatible with
1376 chain_op_t (tree_code code_
, vect_def_type dt_
, tree op_
)
1377 : code (code_
), dt (dt_
), op (op_
) {}
1383 /* Comparator for sorting associatable chains. */
1386 dt_sort_cmp (const void *op1_
, const void *op2_
, void *)
1388 auto *op1
= (const chain_op_t
*) op1_
;
1389 auto *op2
= (const chain_op_t
*) op2_
;
1390 if (op1
->dt
!= op2
->dt
)
1391 return (int)op1
->dt
- (int)op2
->dt
;
1392 return (int)op1
->code
- (int)op2
->code
;
1395 /* Linearize the associatable expression chain at START with the
1396 associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
1397 filling CHAIN with the result and using WORKLIST as intermediate storage.
1398 CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
1399 or MINUS_EXPR. *CHAIN_STMTS if not NULL is filled with all computation
1400 stmts, starting with START. */
1403 vect_slp_linearize_chain (vec_info
*vinfo
,
1404 vec
<std::pair
<tree_code
, gimple
*> > &worklist
,
1405 vec
<chain_op_t
> &chain
,
1406 enum tree_code code
, gimple
*start
,
1407 gimple
*&code_stmt
, gimple
*&alt_code_stmt
,
1408 vec
<gimple
*> *chain_stmts
)
1410 /* For each lane linearize the addition/subtraction (or other
1411 uniform associatable operation) expression tree. */
1412 worklist
.safe_push (std::make_pair (code
, start
));
1413 while (!worklist
.is_empty ())
1415 auto entry
= worklist
.pop ();
1416 gassign
*stmt
= as_a
<gassign
*> (entry
.second
);
1417 enum tree_code in_code
= entry
.first
;
1418 enum tree_code this_code
= gimple_assign_rhs_code (stmt
);
1419 /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE. */
1421 && gimple_assign_rhs_code (stmt
) == code
)
1423 else if (!alt_code_stmt
1424 && gimple_assign_rhs_code (stmt
) == MINUS_EXPR
)
1425 alt_code_stmt
= stmt
;
1427 chain_stmts
->safe_push (stmt
);
1428 for (unsigned opnum
= 1; opnum
<= 2; ++opnum
)
1430 tree op
= gimple_op (stmt
, opnum
);
1432 stmt_vec_info def_stmt_info
;
1433 bool res
= vect_is_simple_use (op
, vinfo
, &dt
, &def_stmt_info
);
1435 if (dt
== vect_internal_def
1436 && is_pattern_stmt_p (def_stmt_info
))
1437 op
= gimple_get_lhs (def_stmt_info
->stmt
);
1439 use_operand_p use_p
;
1440 if (dt
== vect_internal_def
1441 && single_imm_use (op
, &use_p
, &use_stmt
)
1442 && is_gimple_assign (def_stmt_info
->stmt
)
1443 && (gimple_assign_rhs_code (def_stmt_info
->stmt
) == code
1444 || (code
== PLUS_EXPR
1445 && (gimple_assign_rhs_code (def_stmt_info
->stmt
)
1448 tree_code op_def_code
= this_code
;
1449 if (op_def_code
== MINUS_EXPR
&& opnum
== 1)
1450 op_def_code
= PLUS_EXPR
;
1451 if (in_code
== MINUS_EXPR
)
1452 op_def_code
= op_def_code
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
;
1453 worklist
.safe_push (std::make_pair (op_def_code
,
1454 def_stmt_info
->stmt
));
1458 tree_code op_def_code
= this_code
;
1459 if (op_def_code
== MINUS_EXPR
&& opnum
== 1)
1460 op_def_code
= PLUS_EXPR
;
1461 if (in_code
== MINUS_EXPR
)
1462 op_def_code
= op_def_code
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
;
1463 chain
.safe_push (chain_op_t (op_def_code
, dt
, op
));
1469 typedef hash_map
<vec
<stmt_vec_info
>, slp_tree
,
1470 simple_hashmap_traits
<bst_traits
, slp_tree
> >
1471 scalar_stmts_to_slp_tree_map_t
;
1474 vect_build_slp_tree_2 (vec_info
*vinfo
, slp_tree node
,
1475 vec
<stmt_vec_info
> stmts
, unsigned int group_size
,
1476 poly_uint64
*max_nunits
,
1477 bool *matches
, unsigned *limit
, unsigned *tree_size
,
1478 scalar_stmts_to_slp_tree_map_t
*bst_map
);
1481 vect_build_slp_tree (vec_info
*vinfo
,
1482 vec
<stmt_vec_info
> stmts
, unsigned int group_size
,
1483 poly_uint64
*max_nunits
,
1484 bool *matches
, unsigned *limit
, unsigned *tree_size
,
1485 scalar_stmts_to_slp_tree_map_t
*bst_map
)
1487 if (slp_tree
*leader
= bst_map
->get (stmts
))
1489 if (dump_enabled_p ())
1490 dump_printf_loc (MSG_NOTE
, vect_location
, "re-using %sSLP tree %p\n",
1491 !(*leader
)->failed
? "" : "failed ", *leader
);
1492 if (!(*leader
)->failed
)
1494 SLP_TREE_REF_COUNT (*leader
)++;
1495 vect_update_max_nunits (max_nunits
, (*leader
)->max_nunits
);
1499 memcpy (matches
, (*leader
)->failed
, sizeof (bool) * group_size
);
1503 /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
1504 so we can pick up backedge destinations during discovery. */
1505 slp_tree res
= new _slp_tree
;
1506 SLP_TREE_DEF_TYPE (res
) = vect_internal_def
;
1507 SLP_TREE_SCALAR_STMTS (res
) = stmts
;
1508 bst_map
->put (stmts
.copy (), res
);
1512 if (dump_enabled_p ())
1513 dump_printf_loc (MSG_NOTE
, vect_location
,
1514 "SLP discovery limit exceeded\n");
1515 /* Mark the node invalid so we can detect those when still in use
1516 as backedge destinations. */
1517 SLP_TREE_SCALAR_STMTS (res
) = vNULL
;
1518 SLP_TREE_DEF_TYPE (res
) = vect_uninitialized_def
;
1519 res
->failed
= XNEWVEC (bool, group_size
);
1520 memset (res
->failed
, 0, sizeof (bool) * group_size
);
1521 memset (matches
, 0, sizeof (bool) * group_size
);
1526 if (dump_enabled_p ())
1527 dump_printf_loc (MSG_NOTE
, vect_location
,
1528 "starting SLP discovery for node %p\n", res
);
1530 poly_uint64 this_max_nunits
= 1;
1531 slp_tree res_
= vect_build_slp_tree_2 (vinfo
, res
, stmts
, group_size
,
1533 matches
, limit
, tree_size
, bst_map
);
1536 if (dump_enabled_p ())
1537 dump_printf_loc (MSG_NOTE
, vect_location
,
1538 "SLP discovery for node %p failed\n", res
);
1539 /* Mark the node invalid so we can detect those when still in use
1540 as backedge destinations. */
1541 SLP_TREE_SCALAR_STMTS (res
) = vNULL
;
1542 SLP_TREE_DEF_TYPE (res
) = vect_uninitialized_def
;
1543 res
->failed
= XNEWVEC (bool, group_size
);
1547 for (i
= 0; i
< group_size
; ++i
)
1550 gcc_assert (i
< group_size
);
1552 memcpy (res
->failed
, matches
, sizeof (bool) * group_size
);
1556 if (dump_enabled_p ())
1557 dump_printf_loc (MSG_NOTE
, vect_location
,
1558 "SLP discovery for node %p succeeded\n", res
);
1559 gcc_assert (res_
== res
);
1560 res
->max_nunits
= this_max_nunits
;
1561 vect_update_max_nunits (max_nunits
, this_max_nunits
);
1562 /* Keep a reference for the bst_map use. */
1563 SLP_TREE_REF_COUNT (res
)++;
1568 /* Helper for building an associated SLP node chain. */
1571 vect_slp_build_two_operator_nodes (slp_tree perm
, tree vectype
,
1572 slp_tree op0
, slp_tree op1
,
1573 stmt_vec_info oper1
, stmt_vec_info oper2
,
1574 vec
<std::pair
<unsigned, unsigned> > lperm
)
1576 unsigned group_size
= SLP_TREE_LANES (op1
);
1578 slp_tree child1
= new _slp_tree
;
1579 SLP_TREE_DEF_TYPE (child1
) = vect_internal_def
;
1580 SLP_TREE_VECTYPE (child1
) = vectype
;
1581 SLP_TREE_LANES (child1
) = group_size
;
1582 SLP_TREE_CHILDREN (child1
).create (2);
1583 SLP_TREE_CHILDREN (child1
).quick_push (op0
);
1584 SLP_TREE_CHILDREN (child1
).quick_push (op1
);
1585 SLP_TREE_REPRESENTATIVE (child1
) = oper1
;
1587 slp_tree child2
= new _slp_tree
;
1588 SLP_TREE_DEF_TYPE (child2
) = vect_internal_def
;
1589 SLP_TREE_VECTYPE (child2
) = vectype
;
1590 SLP_TREE_LANES (child2
) = group_size
;
1591 SLP_TREE_CHILDREN (child2
).create (2);
1592 SLP_TREE_CHILDREN (child2
).quick_push (op0
);
1593 SLP_TREE_REF_COUNT (op0
)++;
1594 SLP_TREE_CHILDREN (child2
).quick_push (op1
);
1595 SLP_TREE_REF_COUNT (op1
)++;
1596 SLP_TREE_REPRESENTATIVE (child2
) = oper2
;
1598 SLP_TREE_DEF_TYPE (perm
) = vect_internal_def
;
1599 SLP_TREE_CODE (perm
) = VEC_PERM_EXPR
;
1600 SLP_TREE_VECTYPE (perm
) = vectype
;
1601 SLP_TREE_LANES (perm
) = group_size
;
1602 /* ??? We should set this NULL but that's not expected. */
1603 SLP_TREE_REPRESENTATIVE (perm
) = oper1
;
1604 SLP_TREE_LANE_PERMUTATION (perm
) = lperm
;
1605 SLP_TREE_CHILDREN (perm
).quick_push (child1
);
1606 SLP_TREE_CHILDREN (perm
).quick_push (child2
);
1609 /* Recursively build an SLP tree starting from NODE.
1610 Fail (and return a value not equal to zero) if def-stmts are not
1611 isomorphic, require data permutation or are of unsupported types of
1612 operation. Otherwise, return 0.
1613 The value returned is the depth in the SLP tree where a mismatch
1617 vect_build_slp_tree_2 (vec_info
*vinfo
, slp_tree node
,
1618 vec
<stmt_vec_info
> stmts
, unsigned int group_size
,
1619 poly_uint64
*max_nunits
,
1620 bool *matches
, unsigned *limit
, unsigned *tree_size
,
1621 scalar_stmts_to_slp_tree_map_t
*bst_map
)
1623 unsigned nops
, i
, this_tree_size
= 0;
1624 poly_uint64 this_max_nunits
= *max_nunits
;
1628 stmt_vec_info stmt_info
= stmts
[0];
1629 if (gcall
*stmt
= dyn_cast
<gcall
*> (stmt_info
->stmt
))
1630 nops
= gimple_call_num_args (stmt
);
1631 else if (gassign
*stmt
= dyn_cast
<gassign
*> (stmt_info
->stmt
))
1633 nops
= gimple_num_ops (stmt
) - 1;
1634 if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
1637 else if (gphi
*phi
= dyn_cast
<gphi
*> (stmt_info
->stmt
))
1638 nops
= gimple_phi_num_args (phi
);
1642 /* If the SLP node is a PHI (induction or reduction), terminate
1644 bool *skip_args
= XALLOCAVEC (bool, nops
);
1645 memset (skip_args
, 0, sizeof (bool) * nops
);
1646 if (loop_vec_info loop_vinfo
= dyn_cast
<loop_vec_info
> (vinfo
))
1647 if (gphi
*stmt
= dyn_cast
<gphi
*> (stmt_info
->stmt
))
1649 tree scalar_type
= TREE_TYPE (PHI_RESULT (stmt
));
1650 tree vectype
= get_vectype_for_scalar_type (vinfo
, scalar_type
,
1652 if (!vect_record_max_nunits (vinfo
, stmt_info
, group_size
, vectype
,
1656 vect_def_type def_type
= STMT_VINFO_DEF_TYPE (stmt_info
);
1657 if (def_type
== vect_induction_def
)
1659 /* Induction PHIs are not cycles but walk the initial
1660 value. Only for inner loops through, for outer loops
1661 we need to pick up the value from the actual PHIs
1662 to more easily support peeling and epilogue vectorization. */
1663 class loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1664 if (!nested_in_vect_loop_p (loop
, stmt_info
))
1665 skip_args
[loop_preheader_edge (loop
)->dest_idx
] = true;
1668 skip_args
[loop_latch_edge (loop
)->dest_idx
] = true;
1670 else if (def_type
== vect_reduction_def
1671 || def_type
== vect_double_reduction_def
1672 || def_type
== vect_nested_cycle
)
1674 /* Else def types have to match. */
1675 stmt_vec_info other_info
;
1676 bool all_same
= true;
1677 FOR_EACH_VEC_ELT (stmts
, i
, other_info
)
1679 if (STMT_VINFO_DEF_TYPE (other_info
) != def_type
)
1681 if (other_info
!= stmt_info
)
1684 class loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1685 /* Reduction initial values are not explicitely represented. */
1686 if (!nested_in_vect_loop_p (loop
, stmt_info
))
1687 skip_args
[loop_preheader_edge (loop
)->dest_idx
] = true;
1688 /* Reduction chain backedge defs are filled manually.
1689 ??? Need a better way to identify a SLP reduction chain PHI.
1690 Or a better overall way to SLP match those. */
1691 if (all_same
&& def_type
== vect_reduction_def
)
1692 skip_args
[loop_latch_edge (loop
)->dest_idx
] = true;
1694 else if (def_type
!= vect_internal_def
)
1699 bool two_operators
= false;
1700 unsigned char *swap
= XALLOCAVEC (unsigned char, group_size
);
1701 tree vectype
= NULL_TREE
;
1702 if (!vect_build_slp_tree_1 (vinfo
, swap
, stmts
, group_size
,
1703 &this_max_nunits
, matches
, &two_operators
,
1707 /* If the SLP node is a load, terminate the recursion unless masked. */
1708 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1709 && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info
)))
1711 if (gcall
*stmt
= dyn_cast
<gcall
*> (stmt_info
->stmt
))
1714 gcc_assert (gimple_call_internal_p (stmt
, IFN_MASK_LOAD
));
1719 *max_nunits
= this_max_nunits
;
1721 node
= vect_create_new_slp_node (node
, stmts
, 0);
1722 SLP_TREE_VECTYPE (node
) = vectype
;
1723 /* And compute the load permutation. Whether it is actually
1724 a permutation depends on the unrolling factor which is
1726 vec
<unsigned> load_permutation
;
1728 stmt_vec_info load_info
;
1729 load_permutation
.create (group_size
);
1730 stmt_vec_info first_stmt_info
1731 = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node
)[0]);
1732 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), j
, load_info
)
1734 int load_place
= vect_get_place_in_interleaving_chain
1735 (load_info
, first_stmt_info
);
1736 gcc_assert (load_place
!= -1);
1737 load_permutation
.safe_push (load_place
);
1739 SLP_TREE_LOAD_PERMUTATION (node
) = load_permutation
;
1743 else if (gimple_assign_single_p (stmt_info
->stmt
)
1744 && !gimple_vuse (stmt_info
->stmt
)
1745 && gimple_assign_rhs_code (stmt_info
->stmt
) == BIT_FIELD_REF
)
1747 /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
1748 the same SSA name vector of a compatible type to vectype. */
1749 vec
<std::pair
<unsigned, unsigned> > lperm
= vNULL
;
1750 tree vec
= TREE_OPERAND (gimple_assign_rhs1 (stmt_info
->stmt
), 0);
1751 stmt_vec_info estmt_info
;
1752 FOR_EACH_VEC_ELT (stmts
, i
, estmt_info
)
1754 gassign
*estmt
= as_a
<gassign
*> (estmt_info
->stmt
);
1755 tree bfref
= gimple_assign_rhs1 (estmt
);
1757 if (!known_eq (bit_field_size (bfref
),
1758 tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype
))))
1759 || !constant_multiple_p (bit_field_offset (bfref
),
1760 bit_field_size (bfref
), &lane
))
1766 lperm
.safe_push (std::make_pair (0, (unsigned)lane
));
1768 slp_tree vnode
= vect_create_new_slp_node (vNULL
);
1769 /* ??? We record vectype here but we hide eventually necessary
1770 punning and instead rely on code generation to materialize
1771 VIEW_CONVERT_EXPRs as necessary. We instead should make
1772 this explicit somehow. */
1773 SLP_TREE_VECTYPE (vnode
) = vectype
;
1774 SLP_TREE_VEC_DEFS (vnode
).safe_push (vec
);
1775 /* We are always building a permutation node even if it is an identity
1776 permute to shield the rest of the vectorizer from the odd node
1777 representing an actual vector without any scalar ops.
1778 ??? We could hide it completely with making the permute node
1780 node
= vect_create_new_slp_node (node
, stmts
, 1);
1781 SLP_TREE_CODE (node
) = VEC_PERM_EXPR
;
1782 SLP_TREE_LANE_PERMUTATION (node
) = lperm
;
1783 SLP_TREE_VECTYPE (node
) = vectype
;
1784 SLP_TREE_CHILDREN (node
).quick_push (vnode
);
1787 /* When discovery reaches an associatable operation see whether we can
1788 improve that to match up lanes in a way superior to the operand
1789 swapping code which at most looks at two defs.
1790 ??? For BB vectorization we cannot do the brute-force search
1791 for matching as we can succeed by means of builds from scalars
1792 and have no good way to "cost" one build against another. */
1793 else if (is_a
<loop_vec_info
> (vinfo
)
1794 /* ??? We don't handle !vect_internal_def defs below. */
1795 && STMT_VINFO_DEF_TYPE (stmt_info
) == vect_internal_def
1796 && is_gimple_assign (stmt_info
->stmt
)
1797 && (associative_tree_code (gimple_assign_rhs_code (stmt_info
->stmt
))
1798 || gimple_assign_rhs_code (stmt_info
->stmt
) == MINUS_EXPR
)
1799 && ((FLOAT_TYPE_P (vectype
) && flag_associative_math
)
1800 || (INTEGRAL_TYPE_P (TREE_TYPE (vectype
))
1801 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype
)))))
1803 /* See if we have a chain of (mixed) adds or subtracts or other
1804 associatable ops. */
1805 enum tree_code code
= gimple_assign_rhs_code (stmt_info
->stmt
);
1806 if (code
== MINUS_EXPR
)
1808 stmt_vec_info other_op_stmt_info
= NULL
;
1809 stmt_vec_info op_stmt_info
= NULL
;
1810 unsigned chain_len
= 0;
1811 auto_vec
<chain_op_t
> chain
;
1812 auto_vec
<std::pair
<tree_code
, gimple
*> > worklist
;
1813 auto_vec
<vec
<chain_op_t
> > chains (group_size
);
1814 auto_vec
<slp_tree
, 4> children
;
1815 bool hard_fail
= true;
1816 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
1818 /* For each lane linearize the addition/subtraction (or other
1819 uniform associatable operation) expression tree. */
1820 gimple
*op_stmt
= NULL
, *other_op_stmt
= NULL
;
1821 vect_slp_linearize_chain (vinfo
, worklist
, chain
, code
,
1822 stmts
[lane
]->stmt
, op_stmt
, other_op_stmt
,
1824 if (!op_stmt_info
&& op_stmt
)
1825 op_stmt_info
= vinfo
->lookup_stmt (op_stmt
);
1826 if (!other_op_stmt_info
&& other_op_stmt
)
1827 other_op_stmt_info
= vinfo
->lookup_stmt (other_op_stmt
);
1828 if (chain
.length () == 2)
1830 /* In a chain of just two elements resort to the regular
1831 operand swapping scheme. If we run into a length
1832 mismatch still hard-FAIL. */
1837 matches
[lane
] = false;
1838 /* ??? We might want to process the other lanes, but
1839 make sure to not give false matching hints to the
1840 caller for lanes we did not process. */
1841 if (lane
!= group_size
- 1)
1846 else if (chain_len
== 0)
1847 chain_len
= chain
.length ();
1848 else if (chain
.length () != chain_len
)
1850 /* ??? Here we could slip in magic to compensate with
1851 neutral operands. */
1852 matches
[lane
] = false;
1853 if (lane
!= group_size
- 1)
1857 chains
.quick_push (chain
.copy ());
1860 if (chains
.length () == group_size
)
1862 /* We cannot yet use SLP_TREE_CODE to communicate the operation. */
1868 /* Now we have a set of chains with the same length. */
1869 /* 1. pre-sort according to def_type and operation. */
1870 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
1871 chains
[lane
].stablesort (dt_sort_cmp
, vinfo
);
1872 if (dump_enabled_p ())
1874 dump_printf_loc (MSG_NOTE
, vect_location
,
1875 "pre-sorted chains of %s\n",
1876 get_tree_code_name (code
));
1877 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
1879 for (unsigned opnum
= 0; opnum
< chain_len
; ++opnum
)
1880 dump_printf (MSG_NOTE
, "%s %T ",
1881 get_tree_code_name (chains
[lane
][opnum
].code
),
1882 chains
[lane
][opnum
].op
);
1883 dump_printf (MSG_NOTE
, "\n");
1886 /* 2. try to build children nodes, associating as necessary. */
1887 for (unsigned n
= 0; n
< chain_len
; ++n
)
1889 vect_def_type dt
= chains
[0][n
].dt
;
1891 for (lane
= 0; lane
< group_size
; ++lane
)
1892 if (chains
[lane
][n
].dt
!= dt
)
1894 if (dt
== vect_constant_def
1895 && chains
[lane
][n
].dt
== vect_external_def
)
1896 dt
= vect_external_def
;
1897 else if (dt
== vect_external_def
1898 && chains
[lane
][n
].dt
== vect_constant_def
)
1903 if (lane
!= group_size
)
1905 if (dump_enabled_p ())
1906 dump_printf_loc (MSG_NOTE
, vect_location
,
1907 "giving up on chain due to mismatched "
1909 matches
[lane
] = false;
1910 if (lane
!= group_size
- 1)
1914 if (dt
== vect_constant_def
1915 || dt
== vect_external_def
)
1917 /* We can always build those. Might want to sort last
1918 or defer building. */
1920 ops
.create (group_size
);
1921 for (lane
= 0; lane
< group_size
; ++lane
)
1922 ops
.quick_push (chains
[lane
][n
].op
);
1923 slp_tree child
= vect_create_new_slp_node (ops
);
1924 SLP_TREE_DEF_TYPE (child
) = dt
;
1925 children
.safe_push (child
);
1927 else if (dt
!= vect_internal_def
)
1929 /* Not sure, we might need sth special.
1930 gcc.dg/vect/pr96854.c,
1931 gfortran.dg/vect/fast-math-pr37021.f90
1932 and gfortran.dg/vect/pr61171.f trigger. */
1933 /* Soft-fail for now. */
1939 vec
<stmt_vec_info
> op_stmts
;
1940 op_stmts
.create (group_size
);
1941 slp_tree child
= NULL
;
1942 /* Brute-force our way. We have to consider a lane
1943 failing after fixing an earlier fail up in the
1944 SLP discovery recursion. So track the current
1945 permute per lane. */
1946 unsigned *perms
= XALLOCAVEC (unsigned, group_size
);
1947 memset (perms
, 0, sizeof (unsigned) * group_size
);
1950 op_stmts
.truncate (0);
1951 for (lane
= 0; lane
< group_size
; ++lane
)
1953 (vinfo
->lookup_def (chains
[lane
][n
].op
));
1954 child
= vect_build_slp_tree (vinfo
, op_stmts
,
1955 group_size
, &this_max_nunits
,
1957 &this_tree_size
, bst_map
);
1958 /* ??? We're likely getting too many fatal mismatches
1959 here so maybe we want to ignore them (but then we
1960 have no idea which lanes fatally mismatched). */
1961 if (child
|| !matches
[0])
1963 /* Swap another lane we have not yet matched up into
1964 lanes that did not match. If we run out of
1965 permute possibilities for a lane terminate the
1968 for (lane
= 1; lane
< group_size
; ++lane
)
1971 if (n
+ perms
[lane
] + 1 == chain_len
)
1976 std::swap (chains
[lane
][n
],
1977 chains
[lane
][n
+ perms
[lane
] + 1]);
1986 if (dump_enabled_p ())
1987 dump_printf_loc (MSG_NOTE
, vect_location
,
1988 "failed to match up op %d\n", n
);
1989 op_stmts
.release ();
1990 if (lane
!= group_size
- 1)
1993 matches
[lane
] = false;
1996 if (dump_enabled_p ())
1998 dump_printf_loc (MSG_NOTE
, vect_location
,
1999 "matched up op %d to\n", n
);
2000 vect_print_slp_tree (MSG_NOTE
, vect_location
, child
);
2002 children
.safe_push (child
);
2005 /* 3. build SLP nodes to combine the chain. */
2006 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
2007 if (chains
[lane
][0].code
!= code
)
2009 /* See if there's any alternate all-PLUS entry. */
2011 for (n
= 1; n
< chain_len
; ++n
)
2013 for (lane
= 0; lane
< group_size
; ++lane
)
2014 if (chains
[lane
][n
].code
!= code
)
2016 if (lane
== group_size
)
2021 /* Swap that in at first position. */
2022 std::swap (children
[0], children
[n
]);
2023 for (lane
= 0; lane
< group_size
; ++lane
)
2024 std::swap (chains
[lane
][0], chains
[lane
][n
]);
2028 /* ??? When this triggers and we end up with two
2029 vect_constant/external_def up-front things break (ICE)
2030 spectacularly finding an insertion place for the
2031 all-constant op. We should have a fully
2032 vect_internal_def operand though(?) so we can swap
2033 that into first place and then prepend the all-zero
2035 if (dump_enabled_p ())
2036 dump_printf_loc (MSG_NOTE
, vect_location
,
2037 "inserting constant zero to compensate "
2038 "for (partially) negated first "
2041 for (lane
= 0; lane
< group_size
; ++lane
)
2042 chains
[lane
].safe_insert
2043 (0, chain_op_t (code
, vect_constant_def
, NULL_TREE
));
2045 zero_ops
.create (group_size
);
2046 zero_ops
.quick_push (build_zero_cst (TREE_TYPE (vectype
)));
2047 for (lane
= 1; lane
< group_size
; ++lane
)
2048 zero_ops
.quick_push (zero_ops
[0]);
2049 slp_tree zero
= vect_create_new_slp_node (zero_ops
);
2050 SLP_TREE_DEF_TYPE (zero
) = vect_constant_def
;
2051 children
.safe_insert (0, zero
);
2055 for (unsigned i
= 1; i
< children
.length (); ++i
)
2057 slp_tree op0
= children
[i
- 1];
2058 slp_tree op1
= children
[i
];
2059 bool this_two_op
= false;
2060 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
2061 if (chains
[lane
][i
].code
!= chains
[0][i
].code
)
2067 if (i
== children
.length () - 1)
2068 child
= vect_create_new_slp_node (node
, stmts
, 2);
2070 child
= vect_create_new_slp_node (2, ERROR_MARK
);
2073 vec
<std::pair
<unsigned, unsigned> > lperm
;
2074 lperm
.create (group_size
);
2075 for (unsigned lane
= 0; lane
< group_size
; ++lane
)
2076 lperm
.quick_push (std::make_pair
2077 (chains
[lane
][i
].code
!= chains
[0][i
].code
, lane
));
2078 vect_slp_build_two_operator_nodes (child
, vectype
, op0
, op1
,
2079 (chains
[0][i
].code
== code
2081 : other_op_stmt_info
),
2082 (chains
[0][i
].code
== code
2083 ? other_op_stmt_info
2089 SLP_TREE_DEF_TYPE (child
) = vect_internal_def
;
2090 SLP_TREE_VECTYPE (child
) = vectype
;
2091 SLP_TREE_LANES (child
) = group_size
;
2092 SLP_TREE_CHILDREN (child
).quick_push (op0
);
2093 SLP_TREE_CHILDREN (child
).quick_push (op1
);
2094 SLP_TREE_REPRESENTATIVE (child
)
2095 = (chains
[0][i
].code
== code
2096 ? op_stmt_info
: other_op_stmt_info
);
2098 children
[i
] = child
;
2100 *tree_size
+= this_tree_size
+ 1;
2101 *max_nunits
= this_max_nunits
;
2102 while (!chains
.is_empty ())
2103 chains
.pop ().release ();
2107 while (!children
.is_empty ())
2108 vect_free_slp_tree (children
.pop ());
2109 while (!chains
.is_empty ())
2110 chains
.pop ().release ();
2111 /* Hard-fail, otherwise we might run into quadratic processing of the
2112 chains starting one stmt into the chain again. */
2115 /* Fall thru to normal processing. */
2118 /* Get at the operands, verifying they are compatible. */
2119 vec
<slp_oprnd_info
> oprnds_info
= vect_create_oprnd_info (nops
, group_size
);
2120 slp_oprnd_info oprnd_info
;
2121 FOR_EACH_VEC_ELT (stmts
, i
, stmt_info
)
2123 int res
= vect_get_and_check_slp_defs (vinfo
, swap
[i
], skip_args
,
2124 stmts
, i
, &oprnds_info
);
2126 matches
[(res
== -1) ? 0 : i
] = false;
2130 for (i
= 0; i
< group_size
; ++i
)
2133 vect_free_oprnd_info (oprnds_info
);
2138 auto_vec
<slp_tree
, 4> children
;
2140 stmt_info
= stmts
[0];
2142 /* Create SLP_TREE nodes for the definition node/s. */
2143 FOR_EACH_VEC_ELT (oprnds_info
, i
, oprnd_info
)
2148 /* We're skipping certain operands from processing, for example
2149 outer loop reduction initial defs. */
2152 children
.safe_push (NULL
);
2156 if (oprnd_info
->first_dt
== vect_uninitialized_def
)
2158 /* COND_EXPR have one too many eventually if the condition
2160 gcc_assert (i
== 3 && nops
== 4);
2164 if (is_a
<bb_vec_info
> (vinfo
)
2165 && oprnd_info
->first_dt
== vect_internal_def
2166 && !oprnd_info
->any_pattern
)
2168 /* For BB vectorization, if all defs are the same do not
2169 bother to continue the build along the single-lane
2170 graph but use a splat of the scalar value. */
2171 stmt_vec_info first_def
= oprnd_info
->def_stmts
[0];
2172 for (j
= 1; j
< group_size
; ++j
)
2173 if (oprnd_info
->def_stmts
[j
] != first_def
)
2176 /* But avoid doing this for loads where we may be
2177 able to CSE things, unless the stmt is not
2179 && (!STMT_VINFO_VECTORIZABLE (first_def
)
2180 || !gimple_vuse (first_def
->stmt
)))
2182 if (dump_enabled_p ())
2183 dump_printf_loc (MSG_NOTE
, vect_location
,
2184 "Using a splat of the uniform operand\n");
2185 oprnd_info
->first_dt
= vect_external_def
;
2189 if (oprnd_info
->first_dt
== vect_external_def
2190 || oprnd_info
->first_dt
== vect_constant_def
)
2192 slp_tree invnode
= vect_create_new_slp_node (oprnd_info
->ops
);
2193 SLP_TREE_DEF_TYPE (invnode
) = oprnd_info
->first_dt
;
2194 oprnd_info
->ops
= vNULL
;
2195 children
.safe_push (invnode
);
2199 if ((child
= vect_build_slp_tree (vinfo
, oprnd_info
->def_stmts
,
2200 group_size
, &this_max_nunits
,
2202 &this_tree_size
, bst_map
)) != NULL
)
2204 oprnd_info
->def_stmts
= vNULL
;
2205 children
.safe_push (child
);
2209 /* If the SLP build for operand zero failed and operand zero
2210 and one can be commutated try that for the scalar stmts
2211 that failed the match. */
2213 /* A first scalar stmt mismatch signals a fatal mismatch. */
2215 /* ??? For COND_EXPRs we can swap the comparison operands
2216 as well as the arms under some constraints. */
2218 && oprnds_info
[1]->first_dt
== vect_internal_def
2219 && is_gimple_assign (stmt_info
->stmt
)
2220 /* Swapping operands for reductions breaks assumptions later on. */
2221 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_reduction_def
2222 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_double_reduction_def
)
2224 /* See whether we can swap the matching or the non-matching
2226 bool swap_not_matching
= true;
2229 for (j
= 0; j
< group_size
; ++j
)
2231 if (matches
[j
] != !swap_not_matching
)
2233 stmt_vec_info stmt_info
= stmts
[j
];
2234 /* Verify if we can swap operands of this stmt. */
2235 gassign
*stmt
= dyn_cast
<gassign
*> (stmt_info
->stmt
);
2237 || !commutative_tree_code (gimple_assign_rhs_code (stmt
)))
2239 if (!swap_not_matching
)
2241 swap_not_matching
= false;
2246 while (j
!= group_size
);
2248 /* Swap mismatched definition stmts. */
2249 if (dump_enabled_p ())
2250 dump_printf_loc (MSG_NOTE
, vect_location
,
2251 "Re-trying with swapped operands of stmts ");
2252 for (j
= 0; j
< group_size
; ++j
)
2253 if (matches
[j
] == !swap_not_matching
)
2255 std::swap (oprnds_info
[0]->def_stmts
[j
],
2256 oprnds_info
[1]->def_stmts
[j
]);
2257 std::swap (oprnds_info
[0]->ops
[j
],
2258 oprnds_info
[1]->ops
[j
]);
2259 if (dump_enabled_p ())
2260 dump_printf (MSG_NOTE
, "%d ", j
);
2262 if (dump_enabled_p ())
2263 dump_printf (MSG_NOTE
, "\n");
2264 /* After swapping some operands we lost track whether an
2265 operand has any pattern defs so be conservative here. */
2266 if (oprnds_info
[0]->any_pattern
|| oprnds_info
[1]->any_pattern
)
2267 oprnds_info
[0]->any_pattern
= oprnds_info
[1]->any_pattern
= true;
2268 /* And try again with scratch 'matches' ... */
2269 bool *tem
= XALLOCAVEC (bool, group_size
);
2270 if ((child
= vect_build_slp_tree (vinfo
, oprnd_info
->def_stmts
,
2271 group_size
, &this_max_nunits
,
2273 &this_tree_size
, bst_map
)) != NULL
)
2275 oprnd_info
->def_stmts
= vNULL
;
2276 children
.safe_push (child
);
2282 /* If the SLP build failed and we analyze a basic-block
2283 simply treat nodes we fail to build as externally defined
2284 (and thus build vectors from the scalar defs).
2285 The cost model will reject outright expensive cases.
2286 ??? This doesn't treat cases where permutation ultimatively
2287 fails (or we don't try permutation below). Ideally we'd
2288 even compute a permutation that will end up with the maximum
2290 if (is_a
<bb_vec_info
> (vinfo
)
2291 /* ??? Rejecting patterns this way doesn't work. We'd have to
2292 do extra work to cancel the pattern so the uses see the
2294 && !is_pattern_stmt_p (stmt_info
)
2295 && !oprnd_info
->any_pattern
)
2297 /* But if there's a leading vector sized set of matching stmts
2298 fail here so we can split the group. This matches the condition
2299 vect_analyze_slp_instance uses. */
2300 /* ??? We might want to split here and combine the results to support
2301 multiple vector sizes better. */
2302 for (j
= 0; j
< group_size
; ++j
)
2305 if (!known_ge (j
, TYPE_VECTOR_SUBPARTS (vectype
)))
2307 if (dump_enabled_p ())
2308 dump_printf_loc (MSG_NOTE
, vect_location
,
2309 "Building vector operands from scalars\n");
2311 child
= vect_create_new_slp_node (oprnd_info
->ops
);
2312 children
.safe_push (child
);
2313 oprnd_info
->ops
= vNULL
;
2318 gcc_assert (child
== NULL
);
2319 FOR_EACH_VEC_ELT (children
, j
, child
)
2321 vect_free_slp_tree (child
);
2322 vect_free_oprnd_info (oprnds_info
);
2326 vect_free_oprnd_info (oprnds_info
);
2328 /* If we have all children of a child built up from uniform scalars
2329 or does more than one possibly expensive vector construction then
2330 just throw that away, causing it built up from scalars.
2331 The exception is the SLP node for the vector store. */
2332 if (is_a
<bb_vec_info
> (vinfo
)
2333 && !STMT_VINFO_GROUPED_ACCESS (stmt_info
)
2334 /* ??? Rejecting patterns this way doesn't work. We'd have to
2335 do extra work to cancel the pattern so the uses see the
2337 && !is_pattern_stmt_p (stmt_info
))
2341 bool all_uniform_p
= true;
2342 unsigned n_vector_builds
= 0;
2343 FOR_EACH_VEC_ELT (children
, j
, child
)
2347 else if (SLP_TREE_DEF_TYPE (child
) == vect_internal_def
)
2348 all_uniform_p
= false;
2349 else if (!vect_slp_tree_uniform_p (child
))
2351 all_uniform_p
= false;
2352 if (SLP_TREE_DEF_TYPE (child
) == vect_external_def
)
2357 || n_vector_builds
> 1
2358 || (n_vector_builds
== children
.length ()
2359 && is_a
<gphi
*> (stmt_info
->stmt
)))
2363 FOR_EACH_VEC_ELT (children
, j
, child
)
2365 vect_free_slp_tree (child
);
2367 if (dump_enabled_p ())
2368 dump_printf_loc (MSG_NOTE
, vect_location
,
2369 "Building parent vector operands from "
2370 "scalars instead\n");
2375 *tree_size
+= this_tree_size
+ 1;
2376 *max_nunits
= this_max_nunits
;
2380 /* ??? We'd likely want to either cache in bst_map sth like
2381 { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
2382 the true { a+b, a+b, a+b, a+b } ... but there we don't have
2383 explicit stmts to put in so the keying on 'stmts' doesn't
2384 work (but we have the same issue with nodes that use 'ops'). */
2385 slp_tree one
= new _slp_tree
;
2386 slp_tree two
= new _slp_tree
;
2387 SLP_TREE_DEF_TYPE (one
) = vect_internal_def
;
2388 SLP_TREE_DEF_TYPE (two
) = vect_internal_def
;
2389 SLP_TREE_VECTYPE (one
) = vectype
;
2390 SLP_TREE_VECTYPE (two
) = vectype
;
2391 SLP_TREE_CHILDREN (one
).safe_splice (children
);
2392 SLP_TREE_CHILDREN (two
).safe_splice (children
);
2394 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two
), i
, child
)
2395 SLP_TREE_REF_COUNT (child
)++;
2397 /* Here we record the original defs since this
2398 node represents the final lane configuration. */
2399 node
= vect_create_new_slp_node (node
, stmts
, 2);
2400 SLP_TREE_VECTYPE (node
) = vectype
;
2401 SLP_TREE_CODE (node
) = VEC_PERM_EXPR
;
2402 SLP_TREE_CHILDREN (node
).quick_push (one
);
2403 SLP_TREE_CHILDREN (node
).quick_push (two
);
2404 gassign
*stmt
= as_a
<gassign
*> (stmts
[0]->stmt
);
2405 enum tree_code code0
= gimple_assign_rhs_code (stmt
);
2406 enum tree_code ocode
= ERROR_MARK
;
2407 stmt_vec_info ostmt_info
;
2409 FOR_EACH_VEC_ELT (stmts
, i
, ostmt_info
)
2411 gassign
*ostmt
= as_a
<gassign
*> (ostmt_info
->stmt
);
2412 if (gimple_assign_rhs_code (ostmt
) != code0
)
2414 SLP_TREE_LANE_PERMUTATION (node
).safe_push (std::make_pair (1, i
));
2415 ocode
= gimple_assign_rhs_code (ostmt
);
2419 SLP_TREE_LANE_PERMUTATION (node
).safe_push (std::make_pair (0, i
));
2421 SLP_TREE_CODE (one
) = code0
;
2422 SLP_TREE_CODE (two
) = ocode
;
2423 SLP_TREE_LANES (one
) = stmts
.length ();
2424 SLP_TREE_LANES (two
) = stmts
.length ();
2425 SLP_TREE_REPRESENTATIVE (one
) = stmts
[0];
2426 SLP_TREE_REPRESENTATIVE (two
) = stmts
[j
];
2430 node
= vect_create_new_slp_node (node
, stmts
, nops
);
2431 SLP_TREE_VECTYPE (node
) = vectype
;
2432 SLP_TREE_CHILDREN (node
).splice (children
);
2436 /* Dump a single SLP tree NODE. */
2439 vect_print_slp_tree (dump_flags_t dump_kind
, dump_location_t loc
,
2444 stmt_vec_info stmt_info
;
2447 dump_metadata_t
metadata (dump_kind
, loc
.get_impl_location ());
2448 dump_user_location_t user_loc
= loc
.get_user_location ();
2449 dump_printf_loc (metadata
, user_loc
, "node%s %p (max_nunits=%u, refcnt=%u)\n",
2450 SLP_TREE_DEF_TYPE (node
) == vect_external_def
2452 : (SLP_TREE_DEF_TYPE (node
) == vect_constant_def
2455 estimated_poly_value (node
->max_nunits
),
2456 SLP_TREE_REF_COUNT (node
));
2457 if (SLP_TREE_DEF_TYPE (node
) == vect_internal_def
)
2459 if (SLP_TREE_CODE (node
) == VEC_PERM_EXPR
)
2460 dump_printf_loc (metadata
, user_loc
, "op: VEC_PERM_EXPR\n");
2462 dump_printf_loc (metadata
, user_loc
, "op template: %G",
2463 SLP_TREE_REPRESENTATIVE (node
)->stmt
);
2465 if (SLP_TREE_SCALAR_STMTS (node
).exists ())
2466 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
2467 dump_printf_loc (metadata
, user_loc
, "\tstmt %u %G", i
, stmt_info
->stmt
);
2470 dump_printf_loc (metadata
, user_loc
, "\t{ ");
2471 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node
), i
, op
)
2472 dump_printf (metadata
, "%T%s ", op
,
2473 i
< SLP_TREE_SCALAR_OPS (node
).length () - 1 ? "," : "");
2474 dump_printf (metadata
, "}\n");
2476 if (SLP_TREE_LOAD_PERMUTATION (node
).exists ())
2478 dump_printf_loc (metadata
, user_loc
, "\tload permutation {");
2479 FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node
), i
, j
)
2480 dump_printf (dump_kind
, " %u", j
);
2481 dump_printf (dump_kind
, " }\n");
2483 if (SLP_TREE_LANE_PERMUTATION (node
).exists ())
2485 dump_printf_loc (metadata
, user_loc
, "\tlane permutation {");
2486 for (i
= 0; i
< SLP_TREE_LANE_PERMUTATION (node
).length (); ++i
)
2487 dump_printf (dump_kind
, " %u[%u]",
2488 SLP_TREE_LANE_PERMUTATION (node
)[i
].first
,
2489 SLP_TREE_LANE_PERMUTATION (node
)[i
].second
);
2490 dump_printf (dump_kind
, " }\n");
2492 if (SLP_TREE_CHILDREN (node
).is_empty ())
2494 dump_printf_loc (metadata
, user_loc
, "\tchildren");
2495 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2496 dump_printf (dump_kind
, " %p", (void *)child
);
2497 dump_printf (dump_kind
, "\n");
2501 debug (slp_tree node
)
2503 debug_dump_context ctx
;
2504 vect_print_slp_tree (MSG_NOTE
,
2505 dump_location_t::from_location_t (UNKNOWN_LOCATION
),
2509 /* Recursive helper for the dot producer below. */
2512 dot_slp_tree (FILE *f
, slp_tree node
, hash_set
<slp_tree
> &visited
)
2514 if (visited
.add (node
))
2517 fprintf (f
, "\"%p\" [label=\"", (void *)node
);
2518 vect_print_slp_tree (MSG_NOTE
,
2519 dump_location_t::from_location_t (UNKNOWN_LOCATION
),
2521 fprintf (f
, "\"];\n");
2524 for (slp_tree child
: SLP_TREE_CHILDREN (node
))
2525 fprintf (f
, "\"%p\" -> \"%p\";", (void *)node
, (void *)child
);
2527 for (slp_tree child
: SLP_TREE_CHILDREN (node
))
2528 dot_slp_tree (f
, child
, visited
);
2532 dot_slp_tree (const char *fname
, slp_tree node
)
2534 FILE *f
= fopen (fname
, "w");
2535 fprintf (f
, "digraph {\n");
2538 debug_dump_context
ctx (f
);
2539 hash_set
<slp_tree
> visited
;
2540 dot_slp_tree (f
, node
, visited
);
2547 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
2550 vect_print_slp_graph (dump_flags_t dump_kind
, dump_location_t loc
,
2551 slp_tree node
, hash_set
<slp_tree
> &visited
)
2556 if (visited
.add (node
))
2559 vect_print_slp_tree (dump_kind
, loc
, node
);
2561 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2563 vect_print_slp_graph (dump_kind
, loc
, child
, visited
);
2567 vect_print_slp_graph (dump_flags_t dump_kind
, dump_location_t loc
,
2570 hash_set
<slp_tree
> visited
;
2571 vect_print_slp_graph (dump_kind
, loc
, entry
, visited
);
2574 /* Mark the tree rooted at NODE with PURE_SLP. */
2577 vect_mark_slp_stmts (slp_tree node
, hash_set
<slp_tree
> &visited
)
2580 stmt_vec_info stmt_info
;
2583 if (SLP_TREE_DEF_TYPE (node
) != vect_internal_def
)
2586 if (visited
.add (node
))
2589 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
2590 STMT_SLP_TYPE (stmt_info
) = pure_slp
;
2592 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2594 vect_mark_slp_stmts (child
, visited
);
2598 vect_mark_slp_stmts (slp_tree node
)
2600 hash_set
<slp_tree
> visited
;
2601 vect_mark_slp_stmts (node
, visited
);
2604 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
2607 vect_mark_slp_stmts_relevant (slp_tree node
, hash_set
<slp_tree
> &visited
)
2610 stmt_vec_info stmt_info
;
2613 if (SLP_TREE_DEF_TYPE (node
) != vect_internal_def
)
2616 if (visited
.add (node
))
2619 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
2621 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info
)
2622 || STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
);
2623 STMT_VINFO_RELEVANT (stmt_info
) = vect_used_in_scope
;
2626 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2628 vect_mark_slp_stmts_relevant (child
, visited
);
2632 vect_mark_slp_stmts_relevant (slp_tree node
)
2634 hash_set
<slp_tree
> visited
;
2635 vect_mark_slp_stmts_relevant (node
, visited
);
2639 /* Gather loads in the SLP graph NODE and populate the INST loads array. */
2642 vect_gather_slp_loads (vec
<slp_tree
> &loads
, slp_tree node
,
2643 hash_set
<slp_tree
> &visited
)
2645 if (!node
|| visited
.add (node
))
2648 if (SLP_TREE_CHILDREN (node
).length () == 0)
2650 if (SLP_TREE_DEF_TYPE (node
) != vect_internal_def
)
2652 stmt_vec_info stmt_info
= SLP_TREE_SCALAR_STMTS (node
)[0];
2653 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
2654 && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info
)))
2655 loads
.safe_push (node
);
2661 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2662 vect_gather_slp_loads (loads
, child
, visited
);
2667 /* Find the last store in SLP INSTANCE. */
2670 vect_find_last_scalar_stmt_in_slp (slp_tree node
)
2672 stmt_vec_info last
= NULL
;
2673 stmt_vec_info stmt_vinfo
;
2675 for (int i
= 0; SLP_TREE_SCALAR_STMTS (node
).iterate (i
, &stmt_vinfo
); i
++)
2677 stmt_vinfo
= vect_orig_stmt (stmt_vinfo
);
2678 last
= last
? get_later_stmt (stmt_vinfo
, last
) : stmt_vinfo
;
2684 /* Find the first stmt in NODE. */
2687 vect_find_first_scalar_stmt_in_slp (slp_tree node
)
2689 stmt_vec_info first
= NULL
;
2690 stmt_vec_info stmt_vinfo
;
2692 for (int i
= 0; SLP_TREE_SCALAR_STMTS (node
).iterate (i
, &stmt_vinfo
); i
++)
2694 stmt_vinfo
= vect_orig_stmt (stmt_vinfo
);
2696 || get_later_stmt (stmt_vinfo
, first
) == first
)
2703 /* Splits a group of stores, currently beginning at FIRST_VINFO, into
2704 two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
2705 (also containing the first GROUP1_SIZE stmts, since stores are
2706 consecutive), the second containing the remainder.
2707 Return the first stmt in the second group. */
2709 static stmt_vec_info
2710 vect_split_slp_store_group (stmt_vec_info first_vinfo
, unsigned group1_size
)
2712 gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo
) == first_vinfo
);
2713 gcc_assert (group1_size
> 0);
2714 int group2_size
= DR_GROUP_SIZE (first_vinfo
) - group1_size
;
2715 gcc_assert (group2_size
> 0);
2716 DR_GROUP_SIZE (first_vinfo
) = group1_size
;
2718 stmt_vec_info stmt_info
= first_vinfo
;
2719 for (unsigned i
= group1_size
; i
> 1; i
--)
2721 stmt_info
= DR_GROUP_NEXT_ELEMENT (stmt_info
);
2722 gcc_assert (DR_GROUP_GAP (stmt_info
) == 1);
2724 /* STMT is now the last element of the first group. */
2725 stmt_vec_info group2
= DR_GROUP_NEXT_ELEMENT (stmt_info
);
2726 DR_GROUP_NEXT_ELEMENT (stmt_info
) = 0;
2728 DR_GROUP_SIZE (group2
) = group2_size
;
2729 for (stmt_info
= group2
; stmt_info
;
2730 stmt_info
= DR_GROUP_NEXT_ELEMENT (stmt_info
))
2732 DR_GROUP_FIRST_ELEMENT (stmt_info
) = group2
;
2733 gcc_assert (DR_GROUP_GAP (stmt_info
) == 1);
2736 /* For the second group, the DR_GROUP_GAP is that before the original group,
2737 plus skipping over the first vector. */
2738 DR_GROUP_GAP (group2
) = DR_GROUP_GAP (first_vinfo
) + group1_size
;
2740 /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
2741 DR_GROUP_GAP (first_vinfo
) += group2_size
;
2743 if (dump_enabled_p ())
2744 dump_printf_loc (MSG_NOTE
, vect_location
, "Split group into %d and %d\n",
2745 group1_size
, group2_size
);
2750 /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
2751 statements and a vector of NUNITS elements. */
2754 calculate_unrolling_factor (poly_uint64 nunits
, unsigned int group_size
)
2756 return exact_div (common_multiple (nunits
, group_size
), group_size
);
2759 /* Helper that checks to see if a node is a load node. */
2762 vect_is_slp_load_node (slp_tree root
)
2764 return SLP_TREE_DEF_TYPE (root
) == vect_internal_def
2765 && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root
))
2766 && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root
)));
2770 /* Helper function of optimize_load_redistribution that performs the operation
2774 optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t
*bst_map
,
2775 vec_info
*vinfo
, unsigned int group_size
,
2776 hash_map
<slp_tree
, slp_tree
> *load_map
,
2779 if (slp_tree
*leader
= load_map
->get (root
))
2785 /* For now, we don't know anything about externals so do not do anything. */
2786 if (!root
|| SLP_TREE_DEF_TYPE (root
) != vect_internal_def
)
2788 else if (SLP_TREE_CODE (root
) == VEC_PERM_EXPR
)
2790 /* First convert this node into a load node and add it to the leaves
2791 list and flatten the permute from a lane to a load one. If it's
2792 unneeded it will be elided later. */
2793 vec
<stmt_vec_info
> stmts
;
2794 stmts
.create (SLP_TREE_LANES (root
));
2795 lane_permutation_t lane_perm
= SLP_TREE_LANE_PERMUTATION (root
);
2796 for (unsigned j
= 0; j
< lane_perm
.length (); j
++)
2798 std::pair
<unsigned, unsigned> perm
= lane_perm
[j
];
2799 node
= SLP_TREE_CHILDREN (root
)[perm
.first
];
2801 if (!vect_is_slp_load_node (node
)
2802 || SLP_TREE_CHILDREN (node
).exists ())
2808 stmts
.quick_push (SLP_TREE_SCALAR_STMTS (node
)[perm
.second
]);
2811 if (dump_enabled_p ())
2812 dump_printf_loc (MSG_NOTE
, vect_location
,
2813 "converting stmts on permute node %p\n", root
);
2815 bool *matches
= XALLOCAVEC (bool, group_size
);
2816 poly_uint64 max_nunits
= 1;
2817 unsigned tree_size
= 0, limit
= 1;
2818 node
= vect_build_slp_tree (vinfo
, stmts
, group_size
, &max_nunits
,
2819 matches
, &limit
, &tree_size
, bst_map
);
2823 load_map
->put (root
, node
);
2828 load_map
->put (root
, NULL
);
2830 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root
), i
, node
)
2833 = optimize_load_redistribution_1 (bst_map
, vinfo
, group_size
, load_map
,
2837 SLP_TREE_REF_COUNT (value
)++;
2838 SLP_TREE_CHILDREN (root
)[i
] = value
;
2839 /* ??? We know the original leafs of the replaced nodes will
2840 be referenced by bst_map, only the permutes created by
2841 pattern matching are not. */
2842 if (SLP_TREE_REF_COUNT (node
) == 1)
2843 load_map
->remove (node
);
2844 vect_free_slp_tree (node
);
2851 /* Temporary workaround for loads not being CSEd during SLP build. This
2852 function will traverse the SLP tree rooted in ROOT for INSTANCE and find
2853 VEC_PERM nodes that blend vectors from multiple nodes that all read from the
2854 same DR such that the final operation is equal to a permuted load. Such
2855 NODES are then directly converted into LOADS themselves. The nodes are
2856 CSEd using BST_MAP. */
2859 optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t
*bst_map
,
2860 vec_info
*vinfo
, unsigned int group_size
,
2861 hash_map
<slp_tree
, slp_tree
> *load_map
,
2867 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root
), i
, node
)
2870 = optimize_load_redistribution_1 (bst_map
, vinfo
, group_size
, load_map
,
2874 SLP_TREE_REF_COUNT (value
)++;
2875 SLP_TREE_CHILDREN (root
)[i
] = value
;
2876 /* ??? We know the original leafs of the replaced nodes will
2877 be referenced by bst_map, only the permutes created by
2878 pattern matching are not. */
2879 if (SLP_TREE_REF_COUNT (node
) == 1)
2880 load_map
->remove (node
);
2881 vect_free_slp_tree (node
);
2886 /* Helper function of vect_match_slp_patterns.
2888 Attempts to match patterns against the slp tree rooted in REF_NODE using
2889 VINFO. Patterns are matched in post-order traversal.
2891 If matching is successful the value in REF_NODE is updated and returned, if
2892 not then it is returned unchanged. */
2895 vect_match_slp_patterns_2 (slp_tree
*ref_node
, vec_info
*vinfo
,
2896 slp_tree_to_load_perm_map_t
*perm_cache
,
2897 hash_set
<slp_tree
> *visited
)
2900 slp_tree node
= *ref_node
;
2901 bool found_p
= false;
2902 if (!node
|| visited
->add (node
))
2906 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
2907 found_p
|= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node
)[i
],
2908 vinfo
, perm_cache
, visited
);
2910 for (unsigned x
= 0; x
< num__slp_patterns
; x
++)
2912 vect_pattern
*pattern
= slp_patterns
[x
] (perm_cache
, ref_node
);
2915 pattern
->build (vinfo
);
2924 /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
2927 The modified tree is returned. Patterns are tried in order and multiple
2928 patterns may match. */
2931 vect_match_slp_patterns (slp_instance instance
, vec_info
*vinfo
,
2932 hash_set
<slp_tree
> *visited
,
2933 slp_tree_to_load_perm_map_t
*perm_cache
)
2935 DUMP_VECT_SCOPE ("vect_match_slp_patterns");
2936 slp_tree
*ref_node
= &SLP_INSTANCE_TREE (instance
);
2938 if (dump_enabled_p ())
2939 dump_printf_loc (MSG_NOTE
, vect_location
,
2940 "Analyzing SLP tree %p for patterns\n",
2941 SLP_INSTANCE_TREE (instance
));
2943 return vect_match_slp_patterns_2 (ref_node
, vinfo
, perm_cache
, visited
);
2946 /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
2947 splitting into two, with the first split group having size NEW_GROUP_SIZE.
2948 Return true if we could use IFN_STORE_LANES instead and if that appears
2949 to be the better approach. */
2952 vect_slp_prefer_store_lanes_p (vec_info
*vinfo
, stmt_vec_info stmt_info
,
2953 unsigned int group_size
,
2954 unsigned int new_group_size
)
2956 tree scalar_type
= TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info
)));
2957 tree vectype
= get_vectype_for_scalar_type (vinfo
, scalar_type
);
2960 /* Allow the split if one of the two new groups would operate on full
2961 vectors *within* rather than across one scalar loop iteration.
2962 This is purely a heuristic, but it should work well for group
2963 sizes of 3 and 4, where the possible splits are:
2965 3->2+1: OK if the vector has exactly two elements
2967 4->3+1: Less clear-cut. */
2968 if (multiple_p (group_size
- new_group_size
, TYPE_VECTOR_SUBPARTS (vectype
))
2969 || multiple_p (new_group_size
, TYPE_VECTOR_SUBPARTS (vectype
)))
2971 return vect_store_lanes_supported (vectype
, group_size
, false);
2974 /* Analyze an SLP instance starting from a group of grouped stores. Call
2975 vect_build_slp_tree to build a tree of packed stmts if possible.
2976 Return FALSE if it's impossible to SLP any stmt in the loop. */
2979 vect_analyze_slp_instance (vec_info
*vinfo
,
2980 scalar_stmts_to_slp_tree_map_t
*bst_map
,
2981 stmt_vec_info stmt_info
, slp_instance_kind kind
,
2982 unsigned max_tree_size
, unsigned *limit
);
2984 /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
2985 of KIND. Return true if successful. */
2988 vect_build_slp_instance (vec_info
*vinfo
,
2989 slp_instance_kind kind
,
2990 vec
<stmt_vec_info
> &scalar_stmts
,
2991 vec
<stmt_vec_info
> &root_stmt_infos
,
2992 unsigned max_tree_size
, unsigned *limit
,
2993 scalar_stmts_to_slp_tree_map_t
*bst_map
,
2994 /* ??? We need stmt_info for group splitting. */
2995 stmt_vec_info stmt_info_
)
2997 if (dump_enabled_p ())
2999 dump_printf_loc (MSG_NOTE
, vect_location
,
3000 "Starting SLP discovery for\n");
3001 for (unsigned i
= 0; i
< scalar_stmts
.length (); ++i
)
3002 dump_printf_loc (MSG_NOTE
, vect_location
,
3003 " %G", scalar_stmts
[i
]->stmt
);
3006 /* Build the tree for the SLP instance. */
3007 unsigned int group_size
= scalar_stmts
.length ();
3008 bool *matches
= XALLOCAVEC (bool, group_size
);
3009 poly_uint64 max_nunits
= 1;
3010 unsigned tree_size
= 0;
3012 slp_tree node
= vect_build_slp_tree (vinfo
, scalar_stmts
, group_size
,
3013 &max_nunits
, matches
, limit
,
3014 &tree_size
, bst_map
);
3017 /* Calculate the unrolling factor based on the smallest type. */
3018 poly_uint64 unrolling_factor
3019 = calculate_unrolling_factor (max_nunits
, group_size
);
3021 if (maybe_ne (unrolling_factor
, 1U)
3022 && is_a
<bb_vec_info
> (vinfo
))
3024 unsigned HOST_WIDE_INT const_max_nunits
;
3025 if (!max_nunits
.is_constant (&const_max_nunits
)
3026 || const_max_nunits
> group_size
)
3028 if (dump_enabled_p ())
3029 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3030 "Build SLP failed: store group "
3031 "size not a multiple of the vector size "
3032 "in basic block SLP\n");
3033 vect_free_slp_tree (node
);
3036 /* Fatal mismatch. */
3037 if (dump_enabled_p ())
3038 dump_printf_loc (MSG_NOTE
, vect_location
,
3039 "SLP discovery succeeded but node needs "
3041 memset (matches
, true, group_size
);
3042 matches
[group_size
/ const_max_nunits
* const_max_nunits
] = false;
3043 vect_free_slp_tree (node
);
3047 /* Create a new SLP instance. */
3048 slp_instance new_instance
= XNEW (class _slp_instance
);
3049 SLP_INSTANCE_TREE (new_instance
) = node
;
3050 SLP_INSTANCE_UNROLLING_FACTOR (new_instance
) = unrolling_factor
;
3051 SLP_INSTANCE_LOADS (new_instance
) = vNULL
;
3052 SLP_INSTANCE_ROOT_STMTS (new_instance
) = root_stmt_infos
;
3053 SLP_INSTANCE_KIND (new_instance
) = kind
;
3054 new_instance
->reduc_phis
= NULL
;
3055 new_instance
->cost_vec
= vNULL
;
3056 new_instance
->subgraph_entries
= vNULL
;
3058 if (dump_enabled_p ())
3059 dump_printf_loc (MSG_NOTE
, vect_location
,
3060 "SLP size %u vs. limit %u.\n",
3061 tree_size
, max_tree_size
);
3063 /* Fixup SLP reduction chains. */
3064 if (kind
== slp_inst_kind_reduc_chain
)
3066 /* If this is a reduction chain with a conversion in front
3067 amend the SLP tree with a node for that. */
3069 = vect_orig_stmt (scalar_stmts
[group_size
- 1])->stmt
;
3070 if (STMT_VINFO_DEF_TYPE (scalar_stmts
[0]) != vect_reduction_def
)
3072 /* Get at the conversion stmt - we know it's the single use
3073 of the last stmt of the reduction chain. */
3074 use_operand_p use_p
;
3075 bool r
= single_imm_use (gimple_assign_lhs (scalar_def
),
3076 &use_p
, &scalar_def
);
3078 stmt_vec_info next_info
= vinfo
->lookup_stmt (scalar_def
);
3079 next_info
= vect_stmt_to_vectorize (next_info
);
3080 scalar_stmts
= vNULL
;
3081 scalar_stmts
.create (group_size
);
3082 for (unsigned i
= 0; i
< group_size
; ++i
)
3083 scalar_stmts
.quick_push (next_info
);
3084 slp_tree conv
= vect_create_new_slp_node (scalar_stmts
, 1);
3085 SLP_TREE_VECTYPE (conv
) = STMT_VINFO_VECTYPE (next_info
);
3086 SLP_TREE_CHILDREN (conv
).quick_push (node
);
3087 SLP_INSTANCE_TREE (new_instance
) = conv
;
3088 /* We also have to fake this conversion stmt as SLP reduction
3089 group so we don't have to mess with too much code
3091 REDUC_GROUP_FIRST_ELEMENT (next_info
) = next_info
;
3092 REDUC_GROUP_NEXT_ELEMENT (next_info
) = NULL
;
3094 /* Fill the backedge child of the PHI SLP node. The
3095 general matching code cannot find it because the
3096 scalar code does not reflect how we vectorize the
3098 use_operand_p use_p
;
3099 imm_use_iterator imm_iter
;
3100 class loop
*loop
= LOOP_VINFO_LOOP (as_a
<loop_vec_info
> (vinfo
));
3101 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
,
3102 gimple_get_lhs (scalar_def
))
3103 /* There are exactly two non-debug uses, the reduction
3104 PHI and the loop-closed PHI node. */
3105 if (!is_gimple_debug (USE_STMT (use_p
))
3106 && gimple_bb (USE_STMT (use_p
)) == loop
->header
)
3108 auto_vec
<stmt_vec_info
, 64> phis (group_size
);
3109 stmt_vec_info phi_info
3110 = vinfo
->lookup_stmt (USE_STMT (use_p
));
3111 for (unsigned i
= 0; i
< group_size
; ++i
)
3112 phis
.quick_push (phi_info
);
3113 slp_tree
*phi_node
= bst_map
->get (phis
);
3114 unsigned dest_idx
= loop_latch_edge (loop
)->dest_idx
;
3115 SLP_TREE_CHILDREN (*phi_node
)[dest_idx
]
3116 = SLP_INSTANCE_TREE (new_instance
);
3117 SLP_INSTANCE_TREE (new_instance
)->refcnt
++;
3121 vinfo
->slp_instances
.safe_push (new_instance
);
3123 /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
3124 the number of scalar stmts in the root in a few places.
3125 Verify that assumption holds. */
3126 gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance
))
3127 .length () == group_size
);
3129 if (dump_enabled_p ())
3131 dump_printf_loc (MSG_NOTE
, vect_location
,
3132 "Final SLP tree for instance %p:\n", new_instance
);
3133 vect_print_slp_graph (MSG_NOTE
, vect_location
,
3134 SLP_INSTANCE_TREE (new_instance
));
3142 /* Failed to SLP. */
3143 /* Free the allocated memory. */
3144 scalar_stmts
.release ();
3147 stmt_vec_info stmt_info
= stmt_info_
;
3148 /* Try to break the group up into pieces. */
3149 if (kind
== slp_inst_kind_store
)
3151 /* ??? We could delay all the actual splitting of store-groups
3152 until after SLP discovery of the original group completed.
3153 Then we can recurse to vect_build_slp_instance directly. */
3154 for (i
= 0; i
< group_size
; i
++)
3158 /* For basic block SLP, try to break the group up into multiples of
3160 if (is_a
<bb_vec_info
> (vinfo
)
3161 && (i
> 1 && i
< group_size
))
3164 = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info
)));
3165 tree vectype
= get_vectype_for_scalar_type (vinfo
, scalar_type
,
3166 1 << floor_log2 (i
));
3167 unsigned HOST_WIDE_INT const_nunits
;
3169 && TYPE_VECTOR_SUBPARTS (vectype
).is_constant (&const_nunits
))
3171 /* Split into two groups at the first vector boundary. */
3172 gcc_assert ((const_nunits
& (const_nunits
- 1)) == 0);
3173 unsigned group1_size
= i
& ~(const_nunits
- 1);
3175 if (dump_enabled_p ())
3176 dump_printf_loc (MSG_NOTE
, vect_location
,
3177 "Splitting SLP group at stmt %u\n", i
);
3178 stmt_vec_info rest
= vect_split_slp_store_group (stmt_info
,
3180 bool res
= vect_analyze_slp_instance (vinfo
, bst_map
, stmt_info
,
3181 kind
, max_tree_size
,
3183 /* Split the rest at the failure point and possibly
3184 re-analyze the remaining matching part if it has
3185 at least two lanes. */
3187 && (i
+ 1 < group_size
3188 || i
- group1_size
> 1))
3190 stmt_vec_info rest2
= rest
;
3191 rest
= vect_split_slp_store_group (rest
, i
- group1_size
);
3192 if (i
- group1_size
> 1)
3193 res
|= vect_analyze_slp_instance (vinfo
, bst_map
, rest2
,
3194 kind
, max_tree_size
,
3197 /* Re-analyze the non-matching tail if it has at least
3199 if (i
+ 1 < group_size
)
3200 res
|= vect_analyze_slp_instance (vinfo
, bst_map
,
3201 rest
, kind
, max_tree_size
,
3207 /* For loop vectorization split into arbitrary pieces of size > 1. */
3208 if (is_a
<loop_vec_info
> (vinfo
)
3209 && (i
> 1 && i
< group_size
)
3210 && !vect_slp_prefer_store_lanes_p (vinfo
, stmt_info
, group_size
, i
))
3212 unsigned group1_size
= i
;
3214 if (dump_enabled_p ())
3215 dump_printf_loc (MSG_NOTE
, vect_location
,
3216 "Splitting SLP group at stmt %u\n", i
);
3218 stmt_vec_info rest
= vect_split_slp_store_group (stmt_info
,
3220 /* Loop vectorization cannot handle gaps in stores, make sure
3221 the split group appears as strided. */
3222 STMT_VINFO_STRIDED_P (rest
) = 1;
3223 DR_GROUP_GAP (rest
) = 0;
3224 STMT_VINFO_STRIDED_P (stmt_info
) = 1;
3225 DR_GROUP_GAP (stmt_info
) = 0;
3227 bool res
= vect_analyze_slp_instance (vinfo
, bst_map
, stmt_info
,
3228 kind
, max_tree_size
, limit
);
3229 if (i
+ 1 < group_size
)
3230 res
|= vect_analyze_slp_instance (vinfo
, bst_map
,
3231 rest
, kind
, max_tree_size
, limit
);
3236 /* Even though the first vector did not all match, we might be able to SLP
3237 (some) of the remainder. FORNOW ignore this possibility. */
3240 /* Failed to SLP. */
3241 if (dump_enabled_p ())
3242 dump_printf_loc (MSG_NOTE
, vect_location
, "SLP discovery failed\n");
3247 /* Analyze an SLP instance starting from a group of grouped stores. Call
3248 vect_build_slp_tree to build a tree of packed stmts if possible.
3249 Return FALSE if it's impossible to SLP any stmt in the loop. */
3252 vect_analyze_slp_instance (vec_info
*vinfo
,
3253 scalar_stmts_to_slp_tree_map_t
*bst_map
,
3254 stmt_vec_info stmt_info
,
3255 slp_instance_kind kind
,
3256 unsigned max_tree_size
, unsigned *limit
)
3259 vec
<stmt_vec_info
> scalar_stmts
;
3261 if (is_a
<bb_vec_info
> (vinfo
))
3262 vect_location
= stmt_info
->stmt
;
3264 stmt_vec_info next_info
= stmt_info
;
3265 if (kind
== slp_inst_kind_store
)
3267 /* Collect the stores and store them in scalar_stmts. */
3268 scalar_stmts
.create (DR_GROUP_SIZE (stmt_info
));
3271 scalar_stmts
.quick_push (vect_stmt_to_vectorize (next_info
));
3272 next_info
= DR_GROUP_NEXT_ELEMENT (next_info
);
3275 else if (kind
== slp_inst_kind_reduc_chain
)
3277 /* Collect the reduction stmts and store them in scalar_stmts. */
3278 scalar_stmts
.create (REDUC_GROUP_SIZE (stmt_info
));
3281 scalar_stmts
.quick_push (vect_stmt_to_vectorize (next_info
));
3282 next_info
= REDUC_GROUP_NEXT_ELEMENT (next_info
);
3284 /* Mark the first element of the reduction chain as reduction to properly
3285 transform the node. In the reduction analysis phase only the last
3286 element of the chain is marked as reduction. */
3287 STMT_VINFO_DEF_TYPE (stmt_info
)
3288 = STMT_VINFO_DEF_TYPE (scalar_stmts
.last ());
3289 STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info
))
3290 = STMT_VINFO_REDUC_DEF (vect_orig_stmt (scalar_stmts
.last ()));
3292 else if (kind
== slp_inst_kind_ctor
)
3294 tree rhs
= gimple_assign_rhs1 (stmt_info
->stmt
);
3296 scalar_stmts
.create (CONSTRUCTOR_NELTS (rhs
));
3297 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), i
, val
)
3299 stmt_vec_info def_info
= vinfo
->lookup_def (val
);
3300 def_info
= vect_stmt_to_vectorize (def_info
);
3301 scalar_stmts
.quick_push (def_info
);
3303 if (dump_enabled_p ())
3304 dump_printf_loc (MSG_NOTE
, vect_location
,
3305 "Analyzing vectorizable constructor: %G\n",
3308 else if (kind
== slp_inst_kind_reduc_group
)
3310 /* Collect reduction statements. */
3311 const vec
<stmt_vec_info
> &reductions
3312 = as_a
<loop_vec_info
> (vinfo
)->reductions
;
3313 scalar_stmts
.create (reductions
.length ());
3314 for (i
= 0; reductions
.iterate (i
, &next_info
); i
++)
3315 if (STMT_VINFO_RELEVANT_P (next_info
)
3316 || STMT_VINFO_LIVE_P (next_info
))
3317 scalar_stmts
.quick_push (next_info
);
3318 /* If less than two were relevant/live there's nothing to SLP. */
3319 if (scalar_stmts
.length () < 2)
3325 vec
<stmt_vec_info
> roots
= vNULL
;
3326 if (kind
== slp_inst_kind_ctor
)
3329 roots
.quick_push (stmt_info
);
3331 /* Build the tree for the SLP instance. */
3332 bool res
= vect_build_slp_instance (vinfo
, kind
, scalar_stmts
,
3334 max_tree_size
, limit
, bst_map
,
3335 kind
== slp_inst_kind_store
3336 ? stmt_info
: NULL
);
3340 /* ??? If this is slp_inst_kind_store and the above succeeded here's
3341 where we should do store group splitting. */
3346 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
3347 trees of packed scalar stmts if SLP is possible. */
3350 vect_analyze_slp (vec_info
*vinfo
, unsigned max_tree_size
)
3353 stmt_vec_info first_element
;
3354 slp_instance instance
;
3356 DUMP_VECT_SCOPE ("vect_analyze_slp");
3358 unsigned limit
= max_tree_size
;
3360 scalar_stmts_to_slp_tree_map_t
*bst_map
3361 = new scalar_stmts_to_slp_tree_map_t ();
3363 /* Find SLP sequences starting from groups of grouped stores. */
3364 FOR_EACH_VEC_ELT (vinfo
->grouped_stores
, i
, first_element
)
3365 vect_analyze_slp_instance (vinfo
, bst_map
, first_element
,
3366 STMT_VINFO_GROUPED_ACCESS (first_element
)
3367 ? slp_inst_kind_store
: slp_inst_kind_ctor
,
3368 max_tree_size
, &limit
);
3370 if (bb_vec_info bb_vinfo
= dyn_cast
<bb_vec_info
> (vinfo
))
3372 for (unsigned i
= 0; i
< bb_vinfo
->roots
.length (); ++i
)
3374 vect_location
= bb_vinfo
->roots
[i
].roots
[0]->stmt
;
3375 if (vect_build_slp_instance (bb_vinfo
, bb_vinfo
->roots
[i
].kind
,
3376 bb_vinfo
->roots
[i
].stmts
,
3377 bb_vinfo
->roots
[i
].roots
,
3378 max_tree_size
, &limit
, bst_map
, NULL
))
3380 bb_vinfo
->roots
[i
].stmts
= vNULL
;
3381 bb_vinfo
->roots
[i
].roots
= vNULL
;
3386 if (loop_vec_info loop_vinfo
= dyn_cast
<loop_vec_info
> (vinfo
))
3388 /* Find SLP sequences starting from reduction chains. */
3389 FOR_EACH_VEC_ELT (loop_vinfo
->reduction_chains
, i
, first_element
)
3390 if (! STMT_VINFO_RELEVANT_P (first_element
)
3391 && ! STMT_VINFO_LIVE_P (first_element
))
3393 else if (! vect_analyze_slp_instance (vinfo
, bst_map
, first_element
,
3394 slp_inst_kind_reduc_chain
,
3395 max_tree_size
, &limit
))
3397 /* Dissolve reduction chain group. */
3398 stmt_vec_info vinfo
= first_element
;
3399 stmt_vec_info last
= NULL
;
3402 stmt_vec_info next
= REDUC_GROUP_NEXT_ELEMENT (vinfo
);
3403 REDUC_GROUP_FIRST_ELEMENT (vinfo
) = NULL
;
3404 REDUC_GROUP_NEXT_ELEMENT (vinfo
) = NULL
;
3408 STMT_VINFO_DEF_TYPE (first_element
) = vect_internal_def
;
3409 /* It can be still vectorized as part of an SLP reduction. */
3410 loop_vinfo
->reductions
.safe_push (last
);
3413 /* Find SLP sequences starting from groups of reductions. */
3414 if (loop_vinfo
->reductions
.length () > 1)
3415 vect_analyze_slp_instance (vinfo
, bst_map
, loop_vinfo
->reductions
[0],
3416 slp_inst_kind_reduc_group
, max_tree_size
,
3420 hash_set
<slp_tree
> visited_patterns
;
3421 slp_tree_to_load_perm_map_t perm_cache
;
3423 /* See if any patterns can be found in the SLP tree. */
3424 bool pattern_found
= false;
3425 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo
), i
, instance
)
3426 pattern_found
|= vect_match_slp_patterns (instance
, vinfo
,
3427 &visited_patterns
, &perm_cache
);
3429 /* If any were found optimize permutations of loads. */
3432 hash_map
<slp_tree
, slp_tree
> load_map
;
3433 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo
), i
, instance
)
3435 slp_tree root
= SLP_INSTANCE_TREE (instance
);
3436 optimize_load_redistribution (bst_map
, vinfo
, SLP_TREE_LANES (root
),
3443 /* The map keeps a reference on SLP nodes built, release that. */
3444 for (scalar_stmts_to_slp_tree_map_t::iterator it
= bst_map
->begin ();
3445 it
!= bst_map
->end (); ++it
)
3447 vect_free_slp_tree ((*it
).second
);
3450 if (pattern_found
&& dump_enabled_p ())
3452 dump_printf_loc (MSG_NOTE
, vect_location
,
3453 "Pattern matched SLP tree\n");
3454 hash_set
<slp_tree
> visited
;
3455 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo
), i
, instance
)
3456 vect_print_slp_graph (MSG_NOTE
, vect_location
,
3457 SLP_INSTANCE_TREE (instance
), visited
);
3460 return opt_result::success ();
3465 slpg_vertex (slp_tree node_
)
3466 : node (node_
), perm_in (-1), perm_out (-1) {}
3468 int get_perm_materialized () const
3469 { return perm_in
!= perm_out
? perm_in
: 0; }
3472 /* The common permutation on the incoming lanes (towards SLP children). */
3474 /* The permutation on the outgoing lanes (towards SLP parents). When
3475 the node is a materialization point for a permute this differs
3476 from perm_in (and is then usually zero). Materialization happens
3477 on the input side. */
3481 /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
3484 vect_slp_build_vertices (hash_set
<slp_tree
> &visited
, slp_tree node
,
3485 vec
<slpg_vertex
> &vertices
, vec
<int> &leafs
)
3490 if (visited
.add (node
))
3493 node
->vertex
= vertices
.length ();
3494 vertices
.safe_push (slpg_vertex (node
));
3497 bool force_leaf
= false;
3498 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
3502 vect_slp_build_vertices (visited
, child
, vertices
, leafs
);
3506 /* Since SLP discovery works along use-def edges all cycles have an
3507 entry - but there's the exception of cycles where we do not handle
3508 the entry explicitely (but with a NULL SLP node), like some reductions
3509 and inductions. Force those SLP PHIs to act as leafs to make them
3510 backwards reachable. */
3511 if (leaf
|| force_leaf
)
3512 leafs
.safe_push (node
->vertex
);
3515 /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
3518 vect_slp_build_vertices (vec_info
*info
, vec
<slpg_vertex
> &vertices
,
3521 hash_set
<slp_tree
> visited
;
3523 slp_instance instance
;
3524 FOR_EACH_VEC_ELT (info
->slp_instances
, i
, instance
)
3525 vect_slp_build_vertices (visited
, SLP_INSTANCE_TREE (instance
), vertices
,
3529 /* Apply (reverse) bijectite PERM to VEC. */
3533 vect_slp_permute (vec
<unsigned> perm
,
3534 vec
<T
> &vec
, bool reverse
)
3536 auto_vec
<T
, 64> saved
;
3537 saved
.create (vec
.length ());
3538 for (unsigned i
= 0; i
< vec
.length (); ++i
)
3539 saved
.quick_push (vec
[i
]);
3543 for (unsigned i
= 0; i
< vec
.length (); ++i
)
3544 vec
[perm
[i
]] = saved
[i
];
3545 for (unsigned i
= 0; i
< vec
.length (); ++i
)
3546 gcc_assert (vec
[perm
[i
]] == saved
[i
]);
3550 for (unsigned i
= 0; i
< vec
.length (); ++i
)
3551 vec
[i
] = saved
[perm
[i
]];
3552 for (unsigned i
= 0; i
< vec
.length (); ++i
)
3553 gcc_assert (vec
[i
] == saved
[perm
[i
]]);
3557 /* Return whether permutations PERM_A and PERM_B as recorded in the
3558 PERMS vector are equal. */
3561 vect_slp_perms_eq (const vec
<vec
<unsigned> > &perms
,
3562 int perm_a
, int perm_b
)
3564 return (perm_a
== perm_b
3565 || (perm_a
!= -1 && perm_b
!= -1
3566 && perms
[perm_a
].length () == perms
[perm_b
].length ()
3567 && memcmp (&perms
[perm_a
][0], &perms
[perm_b
][0],
3568 sizeof (unsigned) * perms
[perm_a
].length ()) == 0));
3571 /* Optimize the SLP graph of VINFO. */
3574 vect_optimize_slp (vec_info
*vinfo
)
3576 if (vinfo
->slp_instances
.is_empty ())
3581 auto_vec
<slpg_vertex
> vertices
;
3582 auto_vec
<int> leafs
;
3583 vect_slp_build_vertices (vinfo
, vertices
, leafs
);
3585 struct graph
*slpg
= new_graph (vertices
.length ());
3586 for (slpg_vertex
&v
: vertices
)
3587 for (slp_tree child
: SLP_TREE_CHILDREN (v
.node
))
3589 add_edge (slpg
, v
.node
->vertex
, child
->vertex
);
3591 /* Compute (reverse) postorder on the inverted graph. */
3593 graphds_dfs (slpg
, &leafs
[0], leafs
.length (), &ipo
, false, NULL
, NULL
);
3595 auto_vec
<vec
<unsigned> > perms
;
3596 perms
.safe_push (vNULL
); /* zero is no permute */
3598 /* Produce initial permutations. */
3599 for (i
= 0; i
< leafs
.length (); ++i
)
3602 slp_tree node
= vertices
[idx
].node
;
3604 /* Handle externals and constants optimistically throughout the
3605 iteration. But treat existing vectors as fixed since we
3606 do not handle permuting them below. */
3607 if ((SLP_TREE_DEF_TYPE (node
) == vect_external_def
3608 && !SLP_TREE_VEC_DEFS (node
).exists ())
3609 || SLP_TREE_DEF_TYPE (node
) == vect_constant_def
)
3612 /* Leafs do not change across iterations. Note leafs also double
3613 as entries to the reverse graph. */
3614 if (!slpg
->vertices
[idx
].succ
)
3616 vertices
[idx
].perm_in
= 0;
3617 vertices
[idx
].perm_out
= 0;
3620 /* Loads are the only thing generating permutes. */
3621 if (!SLP_TREE_LOAD_PERMUTATION (node
).exists ())
3624 /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the
3625 node unpermuted, record this permute. */
3626 stmt_vec_info dr_stmt
= SLP_TREE_REPRESENTATIVE (node
);
3627 if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt
))
3629 dr_stmt
= DR_GROUP_FIRST_ELEMENT (dr_stmt
);
3630 unsigned imin
= DR_GROUP_SIZE (dr_stmt
) + 1, imax
= 0;
3631 bool any_permute
= false;
3632 for (unsigned j
= 0; j
< SLP_TREE_LANES (node
); ++j
)
3634 unsigned idx
= SLP_TREE_LOAD_PERMUTATION (node
)[j
];
3635 imin
= MIN (imin
, idx
);
3636 imax
= MAX (imax
, idx
);
3637 if (idx
- SLP_TREE_LOAD_PERMUTATION (node
)[0] != j
)
3640 /* If there's no permute no need to split one out. */
3643 /* If the span doesn't match we'd disrupt VF computation, avoid
3645 if (imax
- imin
+ 1 != SLP_TREE_LANES (node
))
3648 /* For now only handle true permutes, like
3649 vect_attempt_slp_rearrange_stmts did. This allows us to be lazy
3650 when permuting constants and invariants keeping the permute
3652 auto_sbitmap
load_index (SLP_TREE_LANES (node
));
3653 bitmap_clear (load_index
);
3654 for (unsigned j
= 0; j
< SLP_TREE_LANES (node
); ++j
)
3655 bitmap_set_bit (load_index
, SLP_TREE_LOAD_PERMUTATION (node
)[j
] - imin
);
3657 for (j
= 0; j
< SLP_TREE_LANES (node
); ++j
)
3658 if (!bitmap_bit_p (load_index
, j
))
3660 if (j
!= SLP_TREE_LANES (node
))
3663 vec
<unsigned> perm
= vNULL
;
3664 perm
.safe_grow (SLP_TREE_LANES (node
), true);
3665 for (unsigned j
= 0; j
< SLP_TREE_LANES (node
); ++j
)
3666 perm
[j
] = SLP_TREE_LOAD_PERMUTATION (node
)[j
] - imin
;
3667 perms
.safe_push (perm
);
3668 vertices
[idx
].perm_in
= perms
.length () - 1;
3669 vertices
[idx
].perm_out
= perms
.length () - 1;
3672 /* In addition to the above we have to mark outgoing permutes facing
3673 non-reduction graph entries that are not represented as to be
3675 for (slp_instance instance
: vinfo
->slp_instances
)
3676 if (SLP_INSTANCE_KIND (instance
) == slp_inst_kind_ctor
)
3678 /* Just setting perm_out isn't enough for the propagation to
3680 vertices
[SLP_INSTANCE_TREE (instance
)->vertex
].perm_in
= 0;
3681 vertices
[SLP_INSTANCE_TREE (instance
)->vertex
].perm_out
= 0;
3684 /* Propagate permutes along the graph and compute materialization points. */
3686 bool do_materialization
= false;
3687 unsigned iteration
= 0;
3693 if (dump_enabled_p ())
3694 dump_printf_loc (MSG_NOTE
, vect_location
,
3695 "SLP optimize iteration %d\n", iteration
);
3697 for (i
= vertices
.length (); i
> 0 ; --i
)
3700 slp_tree node
= vertices
[idx
].node
;
3702 /* Handle externals and constants optimistically throughout the
3704 if (SLP_TREE_DEF_TYPE (node
) == vect_external_def
3705 || SLP_TREE_DEF_TYPE (node
) == vect_constant_def
)
3708 /* We still eventually have failed backedge SLP nodes in the
3709 graph, those are only cancelled when analyzing operations.
3710 Simply treat them as transparent ops, propagating permutes
3712 if (SLP_TREE_DEF_TYPE (node
) == vect_internal_def
)
3714 /* We do not handle stores with a permutation, so all
3715 incoming permutes must have been materialized. */
3716 stmt_vec_info rep
= SLP_TREE_REPRESENTATIVE (node
);
3717 if (STMT_VINFO_DATA_REF (rep
)
3718 && DR_IS_WRITE (STMT_VINFO_DATA_REF (rep
)))
3720 /* ??? We're forcing materialization in place
3721 of the child here, we'd need special handling
3722 in materialization to leave perm_in -1 here. */
3723 vertices
[idx
].perm_in
= 0;
3724 vertices
[idx
].perm_out
= 0;
3726 /* We cannot move a permute across an operation that is
3727 not independent on lanes. Note this is an explicit
3728 negative list since that's much shorter than the respective
3729 positive one but it's critical to keep maintaining it. */
3730 if (is_gimple_call (STMT_VINFO_STMT (rep
)))
3731 switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep
)))
3733 case CFN_COMPLEX_ADD_ROT90
:
3734 case CFN_COMPLEX_ADD_ROT270
:
3735 case CFN_COMPLEX_MUL
:
3736 case CFN_COMPLEX_MUL_CONJ
:
3737 case CFN_VEC_ADDSUB
:
3738 case CFN_VEC_FMADDSUB
:
3739 case CFN_VEC_FMSUBADD
:
3740 vertices
[idx
].perm_in
= 0;
3741 vertices
[idx
].perm_out
= 0;
3746 if (!slpg
->vertices
[idx
].succ
)
3747 /* Pick up pre-computed leaf values. */
3751 bool any_succ_perm_out_m1
= false;
3752 int perm_in
= vertices
[idx
].perm_in
;
3753 for (graph_edge
*succ
= slpg
->vertices
[idx
].succ
;
3754 succ
; succ
= succ
->succ_next
)
3756 int succ_idx
= succ
->dest
;
3757 int succ_perm
= vertices
[succ_idx
].perm_out
;
3758 /* Handle unvisited (and constant) nodes optimistically. */
3759 /* ??? But for constants once we want to handle
3760 non-bijective permutes we have to verify the permute,
3761 when unifying lanes, will not unify different constants.
3762 For example see gcc.dg/vect/bb-slp-14.c for a case
3763 that would break. */
3764 if (succ_perm
== -1)
3766 /* When we handled a non-leaf optimistically, note
3767 that so we can adjust its outgoing permute below. */
3768 slp_tree succ_node
= vertices
[succ_idx
].node
;
3769 if (SLP_TREE_DEF_TYPE (succ_node
) != vect_external_def
3770 && SLP_TREE_DEF_TYPE (succ_node
) != vect_constant_def
)
3771 any_succ_perm_out_m1
= true;
3775 perm_in
= succ_perm
;
3776 else if (succ_perm
== 0
3777 || !vect_slp_perms_eq (perms
, perm_in
, succ_perm
))
3784 /* Adjust any incoming permutes we treated optimistically. */
3785 if (perm_in
!= -1 && any_succ_perm_out_m1
)
3787 for (graph_edge
*succ
= slpg
->vertices
[idx
].succ
;
3788 succ
; succ
= succ
->succ_next
)
3790 slp_tree succ_node
= vertices
[succ
->dest
].node
;
3791 if (vertices
[succ
->dest
].perm_out
== -1
3792 && SLP_TREE_DEF_TYPE (succ_node
) != vect_external_def
3793 && SLP_TREE_DEF_TYPE (succ_node
) != vect_constant_def
)
3795 vertices
[succ
->dest
].perm_out
= perm_in
;
3796 /* And ensure this propagates. */
3797 if (vertices
[succ
->dest
].perm_in
== -1)
3798 vertices
[succ
->dest
].perm_in
= perm_in
;
3804 if (!vect_slp_perms_eq (perms
, perm_in
,
3805 vertices
[idx
].perm_in
))
3807 /* Make sure we eventually converge. */
3808 gcc_checking_assert (vertices
[idx
].perm_in
== -1
3810 vertices
[idx
].perm_in
= perm_in
;
3812 /* While we can handle VEC_PERM nodes as transparent
3813 pass-through they can be a cheap materialization
3814 point as well. In addition they can act as source
3815 of a random permutation as well.
3816 The following ensures that former materialization
3817 points that now have zero incoming permutes no
3818 longer appear as such and that former "any" permutes
3819 get pass-through. We keep VEC_PERM nodes optimistic
3820 as "any" outgoing permute though. */
3821 if (vertices
[idx
].perm_out
!= 0
3822 && SLP_TREE_CODE (node
) != VEC_PERM_EXPR
)
3823 vertices
[idx
].perm_out
= perm_in
;
3828 /* Elide pruning at materialization points in the first
3830 if (!do_materialization
)
3833 int perm
= vertices
[idx
].perm_out
;
3834 if (perm
== 0 || perm
== -1)
3837 /* Decide on permute materialization. Look whether there's
3838 a use (pred) edge that is permuted differently than us.
3839 In that case mark ourselves so the permutation is applied. */
3840 bool all_preds_permuted
= slpg
->vertices
[idx
].pred
!= NULL
;
3841 if (all_preds_permuted
)
3842 for (graph_edge
*pred
= slpg
->vertices
[idx
].pred
;
3843 pred
; pred
= pred
->pred_next
)
3845 int pred_perm
= vertices
[pred
->src
].perm_in
;
3846 gcc_checking_assert (pred_perm
!= -1);
3847 if (!vect_slp_perms_eq (perms
, perm
, pred_perm
))
3849 all_preds_permuted
= false;
3853 if (!all_preds_permuted
)
3855 vertices
[idx
].perm_out
= 0;
3860 /* If the initial propagation converged, switch on materialization
3861 and re-propagate. */
3862 if (!changed
&& !do_materialization
)
3864 do_materialization
= true;
3869 statistics_histogram_event (cfun
, "SLP optimize perm iterations", iteration
);
3872 for (i
= 0; i
< vertices
.length (); ++i
)
3874 int perm_in
= vertices
[i
].perm_in
;
3875 slp_tree node
= vertices
[i
].node
;
3877 /* First permute invariant/external original successors, we handle
3878 those optimistically during propagation and duplicate them if
3879 they are used with different permutations. */
3883 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), j
, child
)
3886 || (SLP_TREE_DEF_TYPE (child
) != vect_constant_def
3887 && SLP_TREE_DEF_TYPE (child
) != vect_external_def
))
3890 /* If the vector is uniform there's nothing to do. */
3891 if (vect_slp_tree_uniform_p (child
))
3894 /* We can end up sharing some externals via two_operator
3895 handling. Be prepared to unshare those. */
3896 if (child
->refcnt
!= 1)
3898 gcc_assert (slpg
->vertices
[child
->vertex
].pred
->pred_next
);
3899 SLP_TREE_CHILDREN (node
)[j
] = child
3900 = vect_create_new_slp_node
3901 (SLP_TREE_SCALAR_OPS (child
).copy ());
3903 vect_slp_permute (perms
[perm_in
],
3904 SLP_TREE_SCALAR_OPS (child
), true);
3907 if (SLP_TREE_CODE (node
) == VEC_PERM_EXPR
)
3909 /* Apply the common permutes to the input vectors. */
3912 /* If the node is already a permute node we can apply
3913 the permutation to the lane selection, effectively
3914 materializing it on the incoming vectors. */
3915 if (dump_enabled_p ())
3916 dump_printf_loc (MSG_NOTE
, vect_location
,
3917 "simplifying permute node %p\n",
3919 for (unsigned k
= 0;
3920 k
< SLP_TREE_LANE_PERMUTATION (node
).length (); ++k
)
3921 SLP_TREE_LANE_PERMUTATION (node
)[k
].second
3922 = perms
[perm_in
][SLP_TREE_LANE_PERMUTATION (node
)[k
].second
];
3924 /* Apply the anticipated output permute to the permute and
3926 int perm_out
= vertices
[i
].perm_out
;
3929 vect_slp_permute (perms
[perm_out
],
3930 SLP_TREE_SCALAR_STMTS (node
), true);
3931 vect_slp_permute (perms
[perm_out
],
3932 SLP_TREE_LANE_PERMUTATION (node
), true);
3935 else if (vertices
[i
].get_perm_materialized () != 0)
3937 if (SLP_TREE_LOAD_PERMUTATION (node
).exists ())
3938 /* For loads simply drop the permutation, the load permutation
3939 already performs the desired permutation. */
3941 else if (SLP_TREE_LANE_PERMUTATION (node
).exists ())
3945 if (dump_enabled_p ())
3946 dump_printf_loc (MSG_NOTE
, vect_location
,
3947 "inserting permute node in place of %p\n",
3950 /* Make a copy of NODE and in-place change it to a
3951 VEC_PERM node to permute the lanes of the copy. */
3952 slp_tree copy
= new _slp_tree
;
3953 SLP_TREE_CHILDREN (copy
) = SLP_TREE_CHILDREN (node
);
3954 SLP_TREE_CHILDREN (node
) = vNULL
;
3955 SLP_TREE_SCALAR_STMTS (copy
)
3956 = SLP_TREE_SCALAR_STMTS (node
).copy ();
3957 vect_slp_permute (perms
[perm_in
],
3958 SLP_TREE_SCALAR_STMTS (copy
), true);
3959 gcc_assert (!SLP_TREE_SCALAR_OPS (node
).exists ());
3960 SLP_TREE_REPRESENTATIVE (copy
) = SLP_TREE_REPRESENTATIVE (node
);
3961 gcc_assert (!SLP_TREE_LOAD_PERMUTATION (node
).exists ());
3962 SLP_TREE_LANE_PERMUTATION (copy
)
3963 = SLP_TREE_LANE_PERMUTATION (node
);
3964 SLP_TREE_LANE_PERMUTATION (node
) = vNULL
;
3965 SLP_TREE_VECTYPE (copy
) = SLP_TREE_VECTYPE (node
);
3967 copy
->max_nunits
= node
->max_nunits
;
3968 SLP_TREE_DEF_TYPE (copy
) = SLP_TREE_DEF_TYPE (node
);
3969 SLP_TREE_LANES (copy
) = SLP_TREE_LANES (node
);
3970 SLP_TREE_CODE (copy
) = SLP_TREE_CODE (node
);
3972 /* Now turn NODE into a VEC_PERM. */
3973 SLP_TREE_CHILDREN (node
).safe_push (copy
);
3974 SLP_TREE_LANE_PERMUTATION (node
).create (SLP_TREE_LANES (node
));
3975 for (unsigned j
= 0; j
< SLP_TREE_LANES (node
); ++j
)
3976 SLP_TREE_LANE_PERMUTATION (node
)
3977 .quick_push (std::make_pair (0, perms
[perm_in
][j
]));
3978 SLP_TREE_CODE (node
) = VEC_PERM_EXPR
;
3981 else if (perm_in
> 0) /* perm_in == perm_out */
3983 /* Apply the reverse permutation to our stmts. */
3984 vect_slp_permute (perms
[perm_in
],
3985 SLP_TREE_SCALAR_STMTS (node
), true);
3986 /* And to the lane/load permutation, which we can simply
3987 make regular by design. */
3988 if (SLP_TREE_LOAD_PERMUTATION (node
).exists ())
3990 gcc_assert (!SLP_TREE_LANE_PERMUTATION (node
).exists ());
3991 /* ??? When we handle non-bijective permutes the idea
3992 is that we can force the load-permutation to be
3993 { min, min + 1, min + 2, ... max }. But then the
3994 scalar defs might no longer match the lane content
3995 which means wrong-code with live lane vectorization.
3996 So we possibly have to have NULL entries for those. */
3997 vect_slp_permute (perms
[perm_in
],
3998 SLP_TREE_LOAD_PERMUTATION (node
), true);
4000 else if (SLP_TREE_LANE_PERMUTATION (node
).exists ())
4005 /* Elide any permutations at BB reduction roots. */
4006 if (is_a
<bb_vec_info
> (vinfo
))
4008 for (slp_instance instance
: vinfo
->slp_instances
)
4010 if (SLP_INSTANCE_KIND (instance
) != slp_inst_kind_bb_reduc
)
4012 slp_tree old
= SLP_INSTANCE_TREE (instance
);
4013 if (SLP_TREE_CODE (old
) == VEC_PERM_EXPR
4014 && SLP_TREE_CHILDREN (old
).length () == 1)
4016 slp_tree child
= SLP_TREE_CHILDREN (old
)[0];
4017 if (SLP_TREE_DEF_TYPE (child
) == vect_external_def
)
4019 /* Preserve the special VEC_PERM we use to shield existing
4020 vector defs from the rest. But make it a no-op. */
4022 for (std::pair
<unsigned, unsigned> &p
4023 : SLP_TREE_LANE_PERMUTATION (old
))
4028 SLP_INSTANCE_TREE (instance
) = child
;
4029 SLP_TREE_REF_COUNT (child
)++;
4030 vect_free_slp_tree (old
);
4033 else if (SLP_TREE_LOAD_PERMUTATION (old
).exists ()
4034 && SLP_TREE_REF_COUNT (old
) == 1
4035 && vertices
[old
->vertex
].get_perm_materialized () != 0)
4037 /* ??? For loads the situation is more complex since
4038 we can't modify the permute in place in case the
4039 node is used multiple times. In fact for loads this
4040 should be somehow handled in the propagation engine. */
4041 /* Apply the reverse permutation to our stmts. */
4042 int perm
= vertices
[old
->vertex
].get_perm_materialized ();
4043 vect_slp_permute (perms
[perm
],
4044 SLP_TREE_SCALAR_STMTS (old
), true);
4045 vect_slp_permute (perms
[perm
],
4046 SLP_TREE_LOAD_PERMUTATION (old
), true);
4051 /* Free the perms vector used for propagation. */
4052 while (!perms
.is_empty ())
4053 perms
.pop ().release ();
4057 /* Now elide load permutations that are not necessary. */
4058 for (i
= 0; i
< leafs
.length (); ++i
)
4060 node
= vertices
[leafs
[i
]].node
;
4061 if (!SLP_TREE_LOAD_PERMUTATION (node
).exists ())
4064 /* In basic block vectorization we allow any subchain of an interleaving
4066 FORNOW: not in loop SLP because of realignment complications. */
4067 if (is_a
<bb_vec_info
> (vinfo
))
4069 bool subchain_p
= true;
4070 stmt_vec_info next_load_info
= NULL
;
4071 stmt_vec_info load_info
;
4073 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), j
, load_info
)
4076 && (next_load_info
!= load_info
4077 || DR_GROUP_GAP (load_info
) != 1))
4082 next_load_info
= DR_GROUP_NEXT_ELEMENT (load_info
);
4086 SLP_TREE_LOAD_PERMUTATION (node
).release ();
4092 stmt_vec_info load_info
;
4093 bool this_load_permuted
= false;
4095 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), j
, load_info
)
4096 if (SLP_TREE_LOAD_PERMUTATION (node
)[j
] != j
)
4098 this_load_permuted
= true;
4101 stmt_vec_info first_stmt_info
4102 = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node
)[0]);
4103 if (!this_load_permuted
4104 /* The load requires permutation when unrolling exposes
4105 a gap either because the group is larger than the SLP
4106 group-size or because there is a gap between the groups. */
4107 && (known_eq (LOOP_VINFO_VECT_FACTOR
4108 (as_a
<loop_vec_info
> (vinfo
)), 1U)
4109 || ((SLP_TREE_LANES (node
) == DR_GROUP_SIZE (first_stmt_info
))
4110 && DR_GROUP_GAP (first_stmt_info
) == 0)))
4112 SLP_TREE_LOAD_PERMUTATION (node
).release ();
4119 /* Gather loads reachable from the individual SLP graph entries. */
4122 vect_gather_slp_loads (vec_info
*vinfo
)
4125 slp_instance instance
;
4126 FOR_EACH_VEC_ELT (vinfo
->slp_instances
, i
, instance
)
4128 hash_set
<slp_tree
> visited
;
4129 vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance
),
4130 SLP_INSTANCE_TREE (instance
), visited
);
4135 /* For each possible SLP instance decide whether to SLP it and calculate overall
4136 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
4137 least one instance. */
4140 vect_make_slp_decision (loop_vec_info loop_vinfo
)
4143 poly_uint64 unrolling_factor
= 1;
4144 const vec
<slp_instance
> &slp_instances
4145 = LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
4146 slp_instance instance
;
4147 int decided_to_slp
= 0;
4149 DUMP_VECT_SCOPE ("vect_make_slp_decision");
4151 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
4153 /* FORNOW: SLP if you can. */
4154 /* All unroll factors have the form:
4156 GET_MODE_SIZE (vinfo->vector_mode) * X
4158 for some rational X, so they must have a common multiple. */
4160 = force_common_multiple (unrolling_factor
,
4161 SLP_INSTANCE_UNROLLING_FACTOR (instance
));
4163 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
4164 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
4165 loop-based vectorization. Such stmts will be marked as HYBRID. */
4166 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance
));
4170 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
) = unrolling_factor
;
4172 if (decided_to_slp
&& dump_enabled_p ())
4174 dump_printf_loc (MSG_NOTE
, vect_location
,
4175 "Decided to SLP %d instances. Unrolling factor ",
4177 dump_dec (MSG_NOTE
, unrolling_factor
);
4178 dump_printf (MSG_NOTE
, "\n");
4181 return (decided_to_slp
> 0);
4184 /* Private data for vect_detect_hybrid_slp. */
4187 loop_vec_info loop_vinfo
;
4188 vec
<stmt_vec_info
> *worklist
;
4191 /* Walker for walk_gimple_op. */
4194 vect_detect_hybrid_slp (tree
*tp
, int *, void *data
)
4196 walk_stmt_info
*wi
= (walk_stmt_info
*)data
;
4197 vdhs_data
*dat
= (vdhs_data
*)wi
->info
;
4202 stmt_vec_info def_stmt_info
= dat
->loop_vinfo
->lookup_def (*tp
);
4205 def_stmt_info
= vect_stmt_to_vectorize (def_stmt_info
);
4206 if (PURE_SLP_STMT (def_stmt_info
))
4208 if (dump_enabled_p ())
4209 dump_printf_loc (MSG_NOTE
, vect_location
, "marking hybrid: %G",
4210 def_stmt_info
->stmt
);
4211 STMT_SLP_TYPE (def_stmt_info
) = hybrid
;
4212 dat
->worklist
->safe_push (def_stmt_info
);
4218 /* Look if STMT_INFO is consumed by SLP indirectly and mark it pure_slp
4219 if so, otherwise pushing it to WORKLIST. */
4222 maybe_push_to_hybrid_worklist (vec_info
*vinfo
,
4223 vec
<stmt_vec_info
> &worklist
,
4224 stmt_vec_info stmt_info
)
4226 if (dump_enabled_p ())
4227 dump_printf_loc (MSG_NOTE
, vect_location
,
4228 "Processing hybrid candidate : %G", stmt_info
->stmt
);
4229 stmt_vec_info orig_info
= vect_orig_stmt (stmt_info
);
4230 imm_use_iterator iter2
;
4232 use_operand_p use_p
;
4233 def_operand_p def_p
;
4234 bool any_def
= false;
4235 FOR_EACH_PHI_OR_STMT_DEF (def_p
, orig_info
->stmt
, iter1
, SSA_OP_DEF
)
4238 FOR_EACH_IMM_USE_FAST (use_p
, iter2
, DEF_FROM_PTR (def_p
))
4240 if (is_gimple_debug (USE_STMT (use_p
)))
4242 stmt_vec_info use_info
= vinfo
->lookup_stmt (USE_STMT (use_p
));
4243 /* An out-of loop use means this is a loop_vect sink. */
4246 if (dump_enabled_p ())
4247 dump_printf_loc (MSG_NOTE
, vect_location
,
4248 "Found loop_vect sink: %G", stmt_info
->stmt
);
4249 worklist
.safe_push (stmt_info
);
4252 else if (!STMT_SLP_TYPE (vect_stmt_to_vectorize (use_info
)))
4254 if (dump_enabled_p ())
4255 dump_printf_loc (MSG_NOTE
, vect_location
,
4256 "Found loop_vect use: %G", use_info
->stmt
);
4257 worklist
.safe_push (stmt_info
);
4262 /* No def means this is a loo_vect sink. */
4265 if (dump_enabled_p ())
4266 dump_printf_loc (MSG_NOTE
, vect_location
,
4267 "Found loop_vect sink: %G", stmt_info
->stmt
);
4268 worklist
.safe_push (stmt_info
);
4271 if (dump_enabled_p ())
4272 dump_printf_loc (MSG_NOTE
, vect_location
,
4273 "Marked SLP consumed stmt pure: %G", stmt_info
->stmt
);
4274 STMT_SLP_TYPE (stmt_info
) = pure_slp
;
4277 /* Find stmts that must be both vectorized and SLPed. */
4280 vect_detect_hybrid_slp (loop_vec_info loop_vinfo
)
4282 DUMP_VECT_SCOPE ("vect_detect_hybrid_slp");
4284 /* All stmts participating in SLP are marked pure_slp, all other
4285 stmts are loop_vect.
4286 First collect all loop_vect stmts into a worklist.
4287 SLP patterns cause not all original scalar stmts to appear in
4288 SLP_TREE_SCALAR_STMTS and thus not all of them are marked pure_slp.
4289 Rectify this here and do a backward walk over the IL only considering
4290 stmts as loop_vect when they are used by a loop_vect stmt and otherwise
4291 mark them as pure_slp. */
4292 auto_vec
<stmt_vec_info
> worklist
;
4293 for (int i
= LOOP_VINFO_LOOP (loop_vinfo
)->num_nodes
- 1; i
>= 0; --i
)
4295 basic_block bb
= LOOP_VINFO_BBS (loop_vinfo
)[i
];
4296 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
4299 gphi
*phi
= gsi
.phi ();
4300 stmt_vec_info stmt_info
= loop_vinfo
->lookup_stmt (phi
);
4301 if (!STMT_SLP_TYPE (stmt_info
) && STMT_VINFO_RELEVANT (stmt_info
))
4302 maybe_push_to_hybrid_worklist (loop_vinfo
,
4303 worklist
, stmt_info
);
4305 for (gimple_stmt_iterator gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);
4308 gimple
*stmt
= gsi_stmt (gsi
);
4309 if (is_gimple_debug (stmt
))
4311 stmt_vec_info stmt_info
= loop_vinfo
->lookup_stmt (stmt
);
4312 if (STMT_VINFO_IN_PATTERN_P (stmt_info
))
4314 for (gimple_stmt_iterator gsi2
4315 = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
));
4316 !gsi_end_p (gsi2
); gsi_next (&gsi2
))
4318 stmt_vec_info patt_info
4319 = loop_vinfo
->lookup_stmt (gsi_stmt (gsi2
));
4320 if (!STMT_SLP_TYPE (patt_info
)
4321 && STMT_VINFO_RELEVANT (patt_info
))
4322 maybe_push_to_hybrid_worklist (loop_vinfo
,
4323 worklist
, patt_info
);
4325 stmt_info
= STMT_VINFO_RELATED_STMT (stmt_info
);
4327 if (!STMT_SLP_TYPE (stmt_info
) && STMT_VINFO_RELEVANT (stmt_info
))
4328 maybe_push_to_hybrid_worklist (loop_vinfo
,
4329 worklist
, stmt_info
);
4333 /* Now we have a worklist of non-SLP stmts, follow use->def chains and
4334 mark any SLP vectorized stmt as hybrid.
4335 ??? We're visiting def stmts N times (once for each non-SLP and
4336 once for each hybrid-SLP use). */
4339 dat
.worklist
= &worklist
;
4340 dat
.loop_vinfo
= loop_vinfo
;
4341 memset (&wi
, 0, sizeof (wi
));
4342 wi
.info
= (void *)&dat
;
4343 while (!worklist
.is_empty ())
4345 stmt_vec_info stmt_info
= worklist
.pop ();
4346 /* Since SSA operands are not set up for pattern stmts we need
4347 to use walk_gimple_op. */
4349 walk_gimple_op (stmt_info
->stmt
, vect_detect_hybrid_slp
, &wi
);
4354 /* Initialize a bb_vec_info struct for the statements in BBS basic blocks. */
4356 _bb_vec_info::_bb_vec_info (vec
<basic_block
> _bbs
, vec_info_shared
*shared
)
4357 : vec_info (vec_info::bb
, shared
),
4361 for (unsigned i
= 0; i
< bbs
.length (); ++i
)
4364 for (gphi_iterator si
= gsi_start_phis (bbs
[i
]); !gsi_end_p (si
);
4367 gphi
*phi
= si
.phi ();
4368 gimple_set_uid (phi
, 0);
4371 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
4372 !gsi_end_p (gsi
); gsi_next (&gsi
))
4374 gimple
*stmt
= gsi_stmt (gsi
);
4375 gimple_set_uid (stmt
, 0);
4376 if (is_gimple_debug (stmt
))
4384 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
4385 stmts in the basic block. */
4387 _bb_vec_info::~_bb_vec_info ()
4389 /* Reset region marker. */
4390 for (unsigned i
= 0; i
< bbs
.length (); ++i
)
4393 for (gphi_iterator si
= gsi_start_phis (bbs
[i
]); !gsi_end_p (si
);
4396 gphi
*phi
= si
.phi ();
4397 gimple_set_uid (phi
, -1);
4399 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
4400 !gsi_end_p (gsi
); gsi_next (&gsi
))
4402 gimple
*stmt
= gsi_stmt (gsi
);
4403 gimple_set_uid (stmt
, -1);
4407 for (unsigned i
= 0; i
< roots
.length (); ++i
)
4409 roots
[i
].stmts
.release ();
4410 roots
[i
].roots
.release ();
4415 /* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
4416 given then that child nodes have already been processed, and that
4417 their def types currently match their SLP node's def type. */
4420 vect_slp_analyze_node_operations_1 (vec_info
*vinfo
, slp_tree node
,
4421 slp_instance node_instance
,
4422 stmt_vector_for_cost
*cost_vec
)
4424 stmt_vec_info stmt_info
= SLP_TREE_REPRESENTATIVE (node
);
4426 /* Calculate the number of vector statements to be created for the
4427 scalar stmts in this node. For SLP reductions it is equal to the
4428 number of vector statements in the children (which has already been
4429 calculated by the recursive call). Otherwise it is the number of
4430 scalar elements in one scalar iteration (DR_GROUP_SIZE) multiplied by
4431 VF divided by the number of elements in a vector. */
4432 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info
)
4433 && REDUC_GROUP_FIRST_ELEMENT (stmt_info
))
4435 for (unsigned i
= 0; i
< SLP_TREE_CHILDREN (node
).length (); ++i
)
4436 if (SLP_TREE_DEF_TYPE (SLP_TREE_CHILDREN (node
)[i
]) == vect_internal_def
)
4438 SLP_TREE_NUMBER_OF_VEC_STMTS (node
)
4439 = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node
)[i
]);
4446 if (loop_vec_info loop_vinfo
= dyn_cast
<loop_vec_info
> (vinfo
))
4447 vf
= loop_vinfo
->vectorization_factor
;
4450 unsigned int group_size
= SLP_TREE_LANES (node
);
4451 tree vectype
= SLP_TREE_VECTYPE (node
);
4452 SLP_TREE_NUMBER_OF_VEC_STMTS (node
)
4453 = vect_get_num_vectors (vf
* group_size
, vectype
);
4456 /* Handle purely internal nodes. */
4457 if (SLP_TREE_CODE (node
) == VEC_PERM_EXPR
)
4458 return vectorizable_slp_permutation (vinfo
, NULL
, node
, cost_vec
);
4460 gcc_assert (STMT_SLP_TYPE (stmt_info
) != loop_vect
);
4463 return vect_analyze_stmt (vinfo
, stmt_info
, &dummy
,
4464 node
, node_instance
, cost_vec
);
4467 /* Try to build NODE from scalars, returning true on success.
4468 NODE_INSTANCE is the SLP instance that contains NODE. */
4471 vect_slp_convert_to_external (vec_info
*vinfo
, slp_tree node
,
4472 slp_instance node_instance
)
4474 stmt_vec_info stmt_info
;
4477 if (!is_a
<bb_vec_info
> (vinfo
)
4478 || node
== SLP_INSTANCE_TREE (node_instance
)
4479 || !SLP_TREE_SCALAR_STMTS (node
).exists ()
4480 || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node
)))
4483 if (dump_enabled_p ())
4484 dump_printf_loc (MSG_NOTE
, vect_location
,
4485 "Building vector operands of %p from scalars instead\n", node
);
4487 /* Don't remove and free the child nodes here, since they could be
4488 referenced by other structures. The analysis and scheduling phases
4489 (need to) ignore child nodes of anything that isn't vect_internal_def. */
4490 unsigned int group_size
= SLP_TREE_LANES (node
);
4491 SLP_TREE_DEF_TYPE (node
) = vect_external_def
;
4492 SLP_TREE_SCALAR_OPS (node
).safe_grow (group_size
, true);
4493 SLP_TREE_LOAD_PERMUTATION (node
).release ();
4494 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
4496 tree lhs
= gimple_get_lhs (vect_orig_stmt (stmt_info
)->stmt
);
4497 SLP_TREE_SCALAR_OPS (node
)[i
] = lhs
;
4502 /* Compute the prologue cost for invariant or constant operands represented
4506 vect_prologue_cost_for_slp (slp_tree node
,
4507 stmt_vector_for_cost
*cost_vec
)
4509 /* There's a special case of an existing vector, that costs nothing. */
4510 if (SLP_TREE_SCALAR_OPS (node
).length () == 0
4511 && !SLP_TREE_VEC_DEFS (node
).is_empty ())
4513 /* Without looking at the actual initializer a vector of
4514 constants can be implemented as load from the constant pool.
4515 When all elements are the same we can use a splat. */
4516 tree vectype
= SLP_TREE_VECTYPE (node
);
4517 unsigned group_size
= SLP_TREE_SCALAR_OPS (node
).length ();
4518 unsigned num_vects_to_check
;
4519 unsigned HOST_WIDE_INT const_nunits
;
4520 unsigned nelt_limit
;
4521 if (TYPE_VECTOR_SUBPARTS (vectype
).is_constant (&const_nunits
)
4522 && ! multiple_p (const_nunits
, group_size
))
4524 num_vects_to_check
= SLP_TREE_NUMBER_OF_VEC_STMTS (node
);
4525 nelt_limit
= const_nunits
;
4529 /* If either the vector has variable length or the vectors
4530 are composed of repeated whole groups we only need to
4531 cost construction once. All vectors will be the same. */
4532 num_vects_to_check
= 1;
4533 nelt_limit
= group_size
;
4535 tree elt
= NULL_TREE
;
4537 for (unsigned j
= 0; j
< num_vects_to_check
* nelt_limit
; ++j
)
4539 unsigned si
= j
% group_size
;
4541 elt
= SLP_TREE_SCALAR_OPS (node
)[si
];
4542 /* ??? We're just tracking whether all operands of a single
4543 vector initializer are the same, ideally we'd check if
4544 we emitted the same one already. */
4545 else if (elt
!= SLP_TREE_SCALAR_OPS (node
)[si
])
4548 if (nelt
== nelt_limit
)
4550 record_stmt_cost (cost_vec
, 1,
4551 SLP_TREE_DEF_TYPE (node
) == vect_external_def
4552 ? (elt
? scalar_to_vec
: vec_construct
)
4554 NULL
, vectype
, 0, vect_prologue
);
4560 /* Analyze statements contained in SLP tree NODE after recursively analyzing
4561 the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
4563 Return true if the operations are supported. */
4566 vect_slp_analyze_node_operations (vec_info
*vinfo
, slp_tree node
,
4567 slp_instance node_instance
,
4568 hash_set
<slp_tree
> &visited_set
,
4569 vec
<slp_tree
> &visited_vec
,
4570 stmt_vector_for_cost
*cost_vec
)
4575 /* Assume we can code-generate all invariants. */
4577 || SLP_TREE_DEF_TYPE (node
) == vect_constant_def
4578 || SLP_TREE_DEF_TYPE (node
) == vect_external_def
)
4581 if (SLP_TREE_DEF_TYPE (node
) == vect_uninitialized_def
)
4583 if (dump_enabled_p ())
4584 dump_printf_loc (MSG_NOTE
, vect_location
,
4585 "Failed cyclic SLP reference in %p\n", node
);
4588 gcc_assert (SLP_TREE_DEF_TYPE (node
) == vect_internal_def
);
4590 /* If we already analyzed the exact same set of scalar stmts we're done.
4591 We share the generated vector stmts for those. */
4592 if (visited_set
.add (node
))
4594 visited_vec
.safe_push (node
);
4597 unsigned visited_rec_start
= visited_vec
.length ();
4598 unsigned cost_vec_rec_start
= cost_vec
->length ();
4599 bool seen_non_constant_child
= false;
4600 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
4602 res
= vect_slp_analyze_node_operations (vinfo
, child
, node_instance
,
4603 visited_set
, visited_vec
,
4607 if (child
&& SLP_TREE_DEF_TYPE (child
) != vect_constant_def
)
4608 seen_non_constant_child
= true;
4610 /* We're having difficulties scheduling nodes with just constant
4611 operands and no scalar stmts since we then cannot compute a stmt
4613 if (!seen_non_constant_child
&& SLP_TREE_SCALAR_STMTS (node
).is_empty ())
4615 if (dump_enabled_p ())
4616 dump_printf_loc (MSG_NOTE
, vect_location
,
4617 "Cannot vectorize all-constant op node %p\n", node
);
4622 res
= vect_slp_analyze_node_operations_1 (vinfo
, node
, node_instance
,
4624 /* If analysis failed we have to pop all recursive visited nodes
4628 while (visited_vec
.length () >= visited_rec_start
)
4629 visited_set
.remove (visited_vec
.pop ());
4630 cost_vec
->truncate (cost_vec_rec_start
);
4633 /* When the node can be vectorized cost invariant nodes it references.
4634 This is not done in DFS order to allow the refering node
4635 vectorizable_* calls to nail down the invariant nodes vector type
4636 and possibly unshare it if it needs a different vector type than
4639 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), j
, child
)
4641 && (SLP_TREE_DEF_TYPE (child
) == vect_constant_def
4642 || SLP_TREE_DEF_TYPE (child
) == vect_external_def
)
4643 /* Perform usual caching, note code-generation still
4644 code-gens these nodes multiple times but we expect
4645 to CSE them later. */
4646 && !visited_set
.add (child
))
4648 visited_vec
.safe_push (child
);
4649 /* ??? After auditing more code paths make a "default"
4650 and push the vector type from NODE to all children
4651 if it is not already set. */
4652 /* Compute the number of vectors to be generated. */
4653 tree vector_type
= SLP_TREE_VECTYPE (child
);
4656 /* For shifts with a scalar argument we don't need
4657 to cost or code-generate anything.
4658 ??? Represent this more explicitely. */
4659 gcc_assert ((STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (node
))
4660 == shift_vec_info_type
)
4664 unsigned group_size
= SLP_TREE_LANES (child
);
4666 if (loop_vec_info loop_vinfo
= dyn_cast
<loop_vec_info
> (vinfo
))
4667 vf
= loop_vinfo
->vectorization_factor
;
4668 SLP_TREE_NUMBER_OF_VEC_STMTS (child
)
4669 = vect_get_num_vectors (vf
* group_size
, vector_type
);
4670 /* And cost them. */
4671 vect_prologue_cost_for_slp (child
, cost_vec
);
4674 /* If this node or any of its children can't be vectorized, try pruning
4675 the tree here rather than felling the whole thing. */
4676 if (!res
&& vect_slp_convert_to_external (vinfo
, node
, node_instance
))
4678 /* We'll need to revisit this for invariant costing and number
4679 of vectorized stmt setting. */
4686 /* Mark lanes of NODE that are live outside of the basic-block vectorized
4687 region and that can be vectorized using vectorizable_live_operation
4688 with STMT_VINFO_LIVE_P. Not handled live operations will cause the
4689 scalar code computing it to be retained. */
4692 vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo
, slp_tree node
,
4693 slp_instance instance
,
4694 stmt_vector_for_cost
*cost_vec
,
4695 hash_set
<stmt_vec_info
> &svisited
,
4696 hash_set
<slp_tree
> &visited
)
4698 if (visited
.add (node
))
4702 stmt_vec_info stmt_info
;
4703 stmt_vec_info last_stmt
= vect_find_last_scalar_stmt_in_slp (node
);
4704 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
4706 if (svisited
.contains (stmt_info
))
4708 stmt_vec_info orig_stmt_info
= vect_orig_stmt (stmt_info
);
4709 if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
)
4710 && STMT_VINFO_RELATED_STMT (orig_stmt_info
) != stmt_info
)
4711 /* Only the pattern root stmt computes the original scalar value. */
4713 bool mark_visited
= true;
4714 gimple
*orig_stmt
= orig_stmt_info
->stmt
;
4715 ssa_op_iter op_iter
;
4716 def_operand_p def_p
;
4717 FOR_EACH_PHI_OR_STMT_DEF (def_p
, orig_stmt
, op_iter
, SSA_OP_DEF
)
4719 imm_use_iterator use_iter
;
4721 stmt_vec_info use_stmt_info
;
4722 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, DEF_FROM_PTR (def_p
))
4723 if (!is_gimple_debug (use_stmt
))
4725 use_stmt_info
= bb_vinfo
->lookup_stmt (use_stmt
);
4727 || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info
)))
4729 STMT_VINFO_LIVE_P (stmt_info
) = true;
4730 if (vectorizable_live_operation (bb_vinfo
, stmt_info
,
4731 NULL
, node
, instance
, i
,
4733 /* ??? So we know we can vectorize the live stmt
4734 from one SLP node. If we cannot do so from all
4735 or none consistently we'd have to record which
4736 SLP node (and lane) we want to use for the live
4737 operation. So make sure we can code-generate
4739 mark_visited
= false;
4741 STMT_VINFO_LIVE_P (stmt_info
) = false;
4745 /* We have to verify whether we can insert the lane extract
4746 before all uses. The following is a conservative approximation.
4747 We cannot put this into vectorizable_live_operation because
4748 iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
4750 Note that while the fact that we emit code for loads at the
4751 first load should make this a non-problem leafs we construct
4752 from scalars are vectorized after the last scalar def.
4753 ??? If we'd actually compute the insert location during
4754 analysis we could use sth less conservative than the last
4755 scalar stmt in the node for the dominance check. */
4756 /* ??? What remains is "live" uses in vector CTORs in the same
4757 SLP graph which is where those uses can end up code-generated
4758 right after their definition instead of close to their original
4759 use. But that would restrict us to code-generate lane-extracts
4760 from the latest stmt in a node. So we compensate for this
4761 during code-generation, simply not replacing uses for those
4762 hopefully rare cases. */
4763 if (STMT_VINFO_LIVE_P (stmt_info
))
4764 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, DEF_FROM_PTR (def_p
))
4765 if (!is_gimple_debug (use_stmt
)
4766 && (!(use_stmt_info
= bb_vinfo
->lookup_stmt (use_stmt
))
4767 || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info
)))
4768 && !vect_stmt_dominates_stmt_p (last_stmt
->stmt
, use_stmt
))
4770 if (dump_enabled_p ())
4771 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4772 "Cannot determine insertion place for "
4774 STMT_VINFO_LIVE_P (stmt_info
) = false;
4775 mark_visited
= true;
4779 svisited
.add (stmt_info
);
4783 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
4784 if (child
&& SLP_TREE_DEF_TYPE (child
) == vect_internal_def
)
4785 vect_bb_slp_mark_live_stmts (bb_vinfo
, child
, instance
,
4786 cost_vec
, svisited
, visited
);
4789 /* Determine whether we can vectorize the reduction epilogue for INSTANCE. */
4792 vectorizable_bb_reduc_epilogue (slp_instance instance
,
4793 stmt_vector_for_cost
*cost_vec
)
4795 gassign
*stmt
= as_a
<gassign
*> (instance
->root_stmts
[0]->stmt
);
4796 enum tree_code reduc_code
= gimple_assign_rhs_code (stmt
);
4797 if (reduc_code
== MINUS_EXPR
)
4798 reduc_code
= PLUS_EXPR
;
4799 internal_fn reduc_fn
;
4800 tree vectype
= SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance
));
4801 if (!reduction_fn_for_scalar_code (reduc_code
, &reduc_fn
)
4802 || reduc_fn
== IFN_LAST
4803 || !direct_internal_fn_supported_p (reduc_fn
, vectype
, OPTIMIZE_FOR_BOTH
)
4804 || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt
)),
4805 TREE_TYPE (vectype
)))
4808 /* There's no way to cost a horizontal vector reduction via REDUC_FN so
4809 cost log2 vector operations plus shuffles and one extraction. */
4810 unsigned steps
= floor_log2 (vect_nunits_for_cost (vectype
));
4811 record_stmt_cost (cost_vec
, steps
, vector_stmt
, instance
->root_stmts
[0],
4812 vectype
, 0, vect_body
);
4813 record_stmt_cost (cost_vec
, steps
, vec_perm
, instance
->root_stmts
[0],
4814 vectype
, 0, vect_body
);
4815 record_stmt_cost (cost_vec
, 1, vec_to_scalar
, instance
->root_stmts
[0],
4816 vectype
, 0, vect_body
);
4820 /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
4821 and recurse to children. */
4824 vect_slp_prune_covered_roots (slp_tree node
, hash_set
<stmt_vec_info
> &roots
,
4825 hash_set
<slp_tree
> &visited
)
4827 if (SLP_TREE_DEF_TYPE (node
) != vect_internal_def
4828 || visited
.add (node
))
4833 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt
)
4834 roots
.remove (vect_orig_stmt (stmt
));
4837 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
4839 vect_slp_prune_covered_roots (child
, roots
, visited
);
4842 /* Analyze statements in SLP instances of VINFO. Return true if the
4843 operations are supported. */
4846 vect_slp_analyze_operations (vec_info
*vinfo
)
4848 slp_instance instance
;
4851 DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
4853 hash_set
<slp_tree
> visited
;
4854 for (i
= 0; vinfo
->slp_instances
.iterate (i
, &instance
); )
4856 auto_vec
<slp_tree
> visited_vec
;
4857 stmt_vector_for_cost cost_vec
;
4858 cost_vec
.create (2);
4859 if (is_a
<bb_vec_info
> (vinfo
))
4860 vect_location
= instance
->location ();
4861 if (!vect_slp_analyze_node_operations (vinfo
,
4862 SLP_INSTANCE_TREE (instance
),
4863 instance
, visited
, visited_vec
,
4865 /* CTOR instances require vectorized defs for the SLP tree root. */
4866 || (SLP_INSTANCE_KIND (instance
) == slp_inst_kind_ctor
4867 && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance
))
4868 != vect_internal_def
))
4869 /* Check we can vectorize the reduction. */
4870 || (SLP_INSTANCE_KIND (instance
) == slp_inst_kind_bb_reduc
4871 && !vectorizable_bb_reduc_epilogue (instance
, &cost_vec
)))
4873 slp_tree node
= SLP_INSTANCE_TREE (instance
);
4874 stmt_vec_info stmt_info
;
4875 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ())
4876 stmt_info
= SLP_INSTANCE_ROOT_STMTS (instance
)[0];
4878 stmt_info
= SLP_TREE_SCALAR_STMTS (node
)[0];
4879 if (dump_enabled_p ())
4880 dump_printf_loc (MSG_NOTE
, vect_location
,
4881 "removing SLP instance operations starting from: %G",
4883 vect_free_slp_instance (instance
);
4884 vinfo
->slp_instances
.ordered_remove (i
);
4885 cost_vec
.release ();
4886 while (!visited_vec
.is_empty ())
4887 visited
.remove (visited_vec
.pop ());
4893 /* For BB vectorization remember the SLP graph entry
4895 if (is_a
<bb_vec_info
> (vinfo
))
4896 instance
->cost_vec
= cost_vec
;
4899 add_stmt_costs (vinfo
->target_cost_data
, &cost_vec
);
4900 cost_vec
.release ();
4905 /* Now look for SLP instances with a root that are covered by other
4906 instances and remove them. */
4907 hash_set
<stmt_vec_info
> roots
;
4908 for (i
= 0; vinfo
->slp_instances
.iterate (i
, &instance
); ++i
)
4909 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ())
4910 roots
.add (SLP_INSTANCE_ROOT_STMTS (instance
)[0]);
4911 if (!roots
.is_empty ())
4914 for (i
= 0; vinfo
->slp_instances
.iterate (i
, &instance
); ++i
)
4915 vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance
), roots
,
4917 for (i
= 0; vinfo
->slp_instances
.iterate (i
, &instance
); )
4918 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ()
4919 && !roots
.contains (SLP_INSTANCE_ROOT_STMTS (instance
)[0]))
4921 stmt_vec_info root
= SLP_INSTANCE_ROOT_STMTS (instance
)[0];
4922 if (dump_enabled_p ())
4923 dump_printf_loc (MSG_NOTE
, vect_location
,
4924 "removing SLP instance operations starting "
4925 "from: %G", root
->stmt
);
4926 vect_free_slp_instance (instance
);
4927 vinfo
->slp_instances
.ordered_remove (i
);
4933 /* Compute vectorizable live stmts. */
4934 if (bb_vec_info bb_vinfo
= dyn_cast
<bb_vec_info
> (vinfo
))
4936 hash_set
<stmt_vec_info
> svisited
;
4937 hash_set
<slp_tree
> visited
;
4938 for (i
= 0; vinfo
->slp_instances
.iterate (i
, &instance
); ++i
)
4940 vect_location
= instance
->location ();
4941 vect_bb_slp_mark_live_stmts (bb_vinfo
, SLP_INSTANCE_TREE (instance
),
4942 instance
, &instance
->cost_vec
, svisited
,
4947 return !vinfo
->slp_instances
.is_empty ();
4950 /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
4951 closing the eventual chain. */
4954 get_ultimate_leader (slp_instance instance
,
4955 hash_map
<slp_instance
, slp_instance
> &instance_leader
)
4957 auto_vec
<slp_instance
*, 8> chain
;
4959 while (*(tem
= instance_leader
.get (instance
)) != instance
)
4961 chain
.safe_push (tem
);
4964 while (!chain
.is_empty ())
4965 *chain
.pop () = instance
;
4969 /* Worker of vect_bb_partition_graph, recurse on NODE. */
4972 vect_bb_partition_graph_r (bb_vec_info bb_vinfo
,
4973 slp_instance instance
, slp_tree node
,
4974 hash_map
<stmt_vec_info
, slp_instance
> &stmt_to_instance
,
4975 hash_map
<slp_instance
, slp_instance
> &instance_leader
,
4976 hash_set
<slp_tree
> &visited
)
4978 stmt_vec_info stmt_info
;
4981 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
4984 slp_instance
&stmt_instance
4985 = stmt_to_instance
.get_or_insert (stmt_info
, &existed_p
);
4988 else if (stmt_instance
!= instance
)
4990 /* If we're running into a previously marked stmt make us the
4991 leader of the current ultimate leader. This keeps the
4992 leader chain acyclic and works even when the current instance
4993 connects two previously independent graph parts. */
4994 slp_instance stmt_leader
4995 = get_ultimate_leader (stmt_instance
, instance_leader
);
4996 if (stmt_leader
!= instance
)
4997 instance_leader
.put (stmt_leader
, instance
);
4999 stmt_instance
= instance
;
5002 if (!SLP_TREE_SCALAR_STMTS (node
).is_empty () && visited
.add (node
))
5006 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
5007 if (child
&& SLP_TREE_DEF_TYPE (child
) == vect_internal_def
)
5008 vect_bb_partition_graph_r (bb_vinfo
, instance
, child
, stmt_to_instance
,
5009 instance_leader
, visited
);
5012 /* Partition the SLP graph into pieces that can be costed independently. */
5015 vect_bb_partition_graph (bb_vec_info bb_vinfo
)
5017 DUMP_VECT_SCOPE ("vect_bb_partition_graph");
5019 /* First walk the SLP graph assigning each involved scalar stmt a
5020 corresponding SLP graph entry and upon visiting a previously
5021 marked stmt, make the stmts leader the current SLP graph entry. */
5022 hash_map
<stmt_vec_info
, slp_instance
> stmt_to_instance
;
5023 hash_map
<slp_instance
, slp_instance
> instance_leader
;
5024 hash_set
<slp_tree
> visited
;
5025 slp_instance instance
;
5026 for (unsigned i
= 0; bb_vinfo
->slp_instances
.iterate (i
, &instance
); ++i
)
5028 instance_leader
.put (instance
, instance
);
5029 vect_bb_partition_graph_r (bb_vinfo
,
5030 instance
, SLP_INSTANCE_TREE (instance
),
5031 stmt_to_instance
, instance_leader
,
5035 /* Then collect entries to each independent subgraph. */
5036 for (unsigned i
= 0; bb_vinfo
->slp_instances
.iterate (i
, &instance
); ++i
)
5038 slp_instance leader
= get_ultimate_leader (instance
, instance_leader
);
5039 leader
->subgraph_entries
.safe_push (instance
);
5040 if (dump_enabled_p ()
5041 && leader
!= instance
)
5042 dump_printf_loc (MSG_NOTE
, vect_location
,
5043 "instance %p is leader of %p\n",
5048 /* Compute the set of scalar stmts participating in internal and external
5052 vect_slp_gather_vectorized_scalar_stmts (vec_info
*vinfo
, slp_tree node
,
5053 hash_set
<slp_tree
> &visited
,
5054 hash_set
<stmt_vec_info
> &vstmts
,
5055 hash_set
<stmt_vec_info
> &estmts
)
5058 stmt_vec_info stmt_info
;
5061 if (visited
.add (node
))
5064 if (SLP_TREE_DEF_TYPE (node
) == vect_internal_def
)
5066 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
5067 vstmts
.add (stmt_info
);
5069 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
5071 vect_slp_gather_vectorized_scalar_stmts (vinfo
, child
, visited
,
5075 for (tree def
: SLP_TREE_SCALAR_OPS (node
))
5077 stmt_vec_info def_stmt
= vinfo
->lookup_def (def
);
5079 estmts
.add (def_stmt
);
5084 /* Compute the scalar cost of the SLP node NODE and its children
5085 and return it. Do not account defs that are marked in LIFE and
5086 update LIFE according to uses of NODE. */
5089 vect_bb_slp_scalar_cost (vec_info
*vinfo
,
5090 slp_tree node
, vec
<bool, va_heap
> *life
,
5091 stmt_vector_for_cost
*cost_vec
,
5092 hash_set
<stmt_vec_info
> &vectorized_scalar_stmts
,
5093 hash_set
<slp_tree
> &visited
)
5096 stmt_vec_info stmt_info
;
5099 if (visited
.add (node
))
5102 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
5104 ssa_op_iter op_iter
;
5105 def_operand_p def_p
;
5110 stmt_vec_info orig_stmt_info
= vect_orig_stmt (stmt_info
);
5111 gimple
*orig_stmt
= orig_stmt_info
->stmt
;
5113 /* If there is a non-vectorized use of the defs then the scalar
5114 stmt is kept live in which case we do not account it or any
5115 required defs in the SLP children in the scalar cost. This
5116 way we make the vectorization more costly when compared to
5118 if (!STMT_VINFO_LIVE_P (stmt_info
))
5120 FOR_EACH_PHI_OR_STMT_DEF (def_p
, orig_stmt
, op_iter
, SSA_OP_DEF
)
5122 imm_use_iterator use_iter
;
5124 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, DEF_FROM_PTR (def_p
))
5125 if (!is_gimple_debug (use_stmt
))
5127 stmt_vec_info use_stmt_info
= vinfo
->lookup_stmt (use_stmt
);
5129 || !vectorized_scalar_stmts
.contains (use_stmt_info
))
5140 /* Count scalar stmts only once. */
5141 if (gimple_visited_p (orig_stmt
))
5143 gimple_set_visited (orig_stmt
, true);
5145 vect_cost_for_stmt kind
;
5146 if (STMT_VINFO_DATA_REF (orig_stmt_info
))
5148 if (DR_IS_READ (STMT_VINFO_DATA_REF (orig_stmt_info
)))
5151 kind
= scalar_store
;
5153 else if (vect_nop_conversion_p (orig_stmt_info
))
5155 /* For single-argument PHIs assume coalescing which means zero cost
5156 for the scalar and the vector PHIs. This avoids artificially
5157 favoring the vector path (but may pessimize it in some cases). */
5158 else if (is_a
<gphi
*> (orig_stmt_info
->stmt
)
5159 && gimple_phi_num_args
5160 (as_a
<gphi
*> (orig_stmt_info
->stmt
)) == 1)
5164 record_stmt_cost (cost_vec
, 1, kind
, orig_stmt_info
,
5165 SLP_TREE_VECTYPE (node
), 0, vect_body
);
5168 auto_vec
<bool, 20> subtree_life
;
5169 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
5171 if (child
&& SLP_TREE_DEF_TYPE (child
) == vect_internal_def
)
5173 /* Do not directly pass LIFE to the recursive call, copy it to
5174 confine changes in the callee to the current child/subtree. */
5175 if (SLP_TREE_CODE (node
) == VEC_PERM_EXPR
)
5177 subtree_life
.safe_grow_cleared (SLP_TREE_LANES (child
), true);
5178 for (unsigned j
= 0;
5179 j
< SLP_TREE_LANE_PERMUTATION (node
).length (); ++j
)
5181 auto perm
= SLP_TREE_LANE_PERMUTATION (node
)[j
];
5182 if (perm
.first
== i
)
5183 subtree_life
[perm
.second
] = (*life
)[j
];
5188 gcc_assert (SLP_TREE_LANES (node
) == SLP_TREE_LANES (child
));
5189 subtree_life
.safe_splice (*life
);
5191 vect_bb_slp_scalar_cost (vinfo
, child
, &subtree_life
, cost_vec
,
5192 vectorized_scalar_stmts
, visited
);
5193 subtree_life
.truncate (0);
5198 /* Comparator for the loop-index sorted cost vectors. */
5201 li_cost_vec_cmp (const void *a_
, const void *b_
)
5203 auto *a
= (const std::pair
<unsigned, stmt_info_for_cost
*> *)a_
;
5204 auto *b
= (const std::pair
<unsigned, stmt_info_for_cost
*> *)b_
;
5205 if (a
->first
< b
->first
)
5207 else if (a
->first
== b
->first
)
5212 /* Check if vectorization of the basic block is profitable for the
5213 subgraph denoted by SLP_INSTANCES. */
5216 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo
,
5217 vec
<slp_instance
> slp_instances
,
5220 slp_instance instance
;
5222 unsigned int vec_inside_cost
= 0, vec_outside_cost
= 0, scalar_cost
= 0;
5223 unsigned int vec_prologue_cost
= 0, vec_epilogue_cost
= 0;
5225 if (dump_enabled_p ())
5227 dump_printf_loc (MSG_NOTE
, vect_location
, "Costing subgraph: \n");
5228 hash_set
<slp_tree
> visited
;
5229 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
5230 vect_print_slp_graph (MSG_NOTE
, vect_location
,
5231 SLP_INSTANCE_TREE (instance
), visited
);
5234 /* Compute the set of scalar stmts we know will go away 'locally' when
5235 vectorizing. This used to be tracked with just PURE_SLP_STMT but that's
5236 not accurate for nodes promoted extern late or for scalar stmts that
5237 are used both in extern defs and in vectorized defs. */
5238 hash_set
<stmt_vec_info
> vectorized_scalar_stmts
;
5239 hash_set
<stmt_vec_info
> scalar_stmts_in_externs
;
5240 hash_set
<slp_tree
> visited
;
5241 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
5243 vect_slp_gather_vectorized_scalar_stmts (bb_vinfo
,
5244 SLP_INSTANCE_TREE (instance
),
5246 vectorized_scalar_stmts
,
5247 scalar_stmts_in_externs
);
5248 for (stmt_vec_info rstmt
: SLP_INSTANCE_ROOT_STMTS (instance
))
5249 vectorized_scalar_stmts
.add (rstmt
);
5251 /* Scalar stmts used as defs in external nodes need to be preseved, so
5252 remove them from vectorized_scalar_stmts. */
5253 for (stmt_vec_info stmt
: scalar_stmts_in_externs
)
5254 vectorized_scalar_stmts
.remove (stmt
);
5256 /* Calculate scalar cost and sum the cost for the vector stmts
5257 previously collected. */
5258 stmt_vector_for_cost scalar_costs
= vNULL
;
5259 stmt_vector_for_cost vector_costs
= vNULL
;
5261 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
5263 auto_vec
<bool, 20> life
;
5264 life
.safe_grow_cleared (SLP_TREE_LANES (SLP_INSTANCE_TREE (instance
)),
5266 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ())
5267 record_stmt_cost (&scalar_costs
,
5268 SLP_INSTANCE_ROOT_STMTS (instance
).length (),
5270 SLP_INSTANCE_ROOT_STMTS (instance
)[0], 0, vect_body
);
5271 vect_bb_slp_scalar_cost (bb_vinfo
,
5272 SLP_INSTANCE_TREE (instance
),
5273 &life
, &scalar_costs
, vectorized_scalar_stmts
,
5275 vector_costs
.safe_splice (instance
->cost_vec
);
5276 instance
->cost_vec
.release ();
5279 if (dump_enabled_p ())
5280 dump_printf_loc (MSG_NOTE
, vect_location
, "Cost model analysis: \n");
5282 /* When costing non-loop vectorization we need to consider each covered
5283 loop independently and make sure vectorization is profitable. For
5284 now we assume a loop may be not entered or executed an arbitrary
5285 number of iterations (??? static information can provide more
5286 precise info here) which means we can simply cost each containing
5287 loops stmts separately. */
5289 /* First produce cost vectors sorted by loop index. */
5290 auto_vec
<std::pair
<unsigned, stmt_info_for_cost
*> >
5291 li_scalar_costs (scalar_costs
.length ());
5292 auto_vec
<std::pair
<unsigned, stmt_info_for_cost
*> >
5293 li_vector_costs (vector_costs
.length ());
5294 stmt_info_for_cost
*cost
;
5295 FOR_EACH_VEC_ELT (scalar_costs
, i
, cost
)
5297 unsigned l
= gimple_bb (cost
->stmt_info
->stmt
)->loop_father
->num
;
5298 li_scalar_costs
.quick_push (std::make_pair (l
, cost
));
5300 /* Use a random used loop as fallback in case the first vector_costs
5301 entry does not have a stmt_info associated with it. */
5302 unsigned l
= li_scalar_costs
[0].first
;
5303 FOR_EACH_VEC_ELT (vector_costs
, i
, cost
)
5305 /* We inherit from the previous COST, invariants, externals and
5306 extracts immediately follow the cost for the related stmt. */
5307 if (cost
->stmt_info
)
5308 l
= gimple_bb (cost
->stmt_info
->stmt
)->loop_father
->num
;
5309 li_vector_costs
.quick_push (std::make_pair (l
, cost
));
5311 li_scalar_costs
.qsort (li_cost_vec_cmp
);
5312 li_vector_costs
.qsort (li_cost_vec_cmp
);
5314 /* Now cost the portions individually. */
5317 bool profitable
= true;
5318 while (si
< li_scalar_costs
.length ()
5319 && vi
< li_vector_costs
.length ())
5321 unsigned sl
= li_scalar_costs
[si
].first
;
5322 unsigned vl
= li_vector_costs
[vi
].first
;
5325 if (dump_enabled_p ())
5326 dump_printf_loc (MSG_NOTE
, vect_location
,
5327 "Scalar %d and vector %d loop part do not "
5328 "match up, skipping scalar part\n", sl
, vl
);
5329 /* Skip the scalar part, assuming zero cost on the vector side. */
5334 while (si
< li_scalar_costs
.length ()
5335 && li_scalar_costs
[si
].first
== sl
);
5339 class vector_costs
*scalar_target_cost_data
= init_cost (bb_vinfo
, true);
5342 add_stmt_cost (scalar_target_cost_data
, li_scalar_costs
[si
].second
);
5345 while (si
< li_scalar_costs
.length ()
5346 && li_scalar_costs
[si
].first
== sl
);
5348 finish_cost (scalar_target_cost_data
, &dummy
, &scalar_cost
, &dummy
);
5349 delete scalar_target_cost_data
;
5351 /* Complete the target-specific vector cost calculation. */
5352 class vector_costs
*vect_target_cost_data
= init_cost (bb_vinfo
, false);
5355 add_stmt_cost (vect_target_cost_data
, li_vector_costs
[vi
].second
);
5358 while (vi
< li_vector_costs
.length ()
5359 && li_vector_costs
[vi
].first
== vl
);
5360 finish_cost (vect_target_cost_data
, &vec_prologue_cost
,
5361 &vec_inside_cost
, &vec_epilogue_cost
);
5362 delete vect_target_cost_data
;
5364 vec_outside_cost
= vec_prologue_cost
+ vec_epilogue_cost
;
5366 if (dump_enabled_p ())
5368 dump_printf_loc (MSG_NOTE
, vect_location
,
5369 "Cost model analysis for part in loop %d:\n", sl
);
5370 dump_printf (MSG_NOTE
, " Vector cost: %d\n",
5371 vec_inside_cost
+ vec_outside_cost
);
5372 dump_printf (MSG_NOTE
, " Scalar cost: %d\n", scalar_cost
);
5375 /* Vectorization is profitable if its cost is more than the cost of scalar
5376 version. Note that we err on the vector side for equal cost because
5377 the cost estimate is otherwise quite pessimistic (constant uses are
5378 free on the scalar side but cost a load on the vector side for
5380 if (vec_outside_cost
+ vec_inside_cost
> scalar_cost
)
5386 if (profitable
&& vi
< li_vector_costs
.length ())
5388 if (dump_enabled_p ())
5389 dump_printf_loc (MSG_NOTE
, vect_location
,
5390 "Excess vector cost for part in loop %d:\n",
5391 li_vector_costs
[vi
].first
);
5395 /* Unset visited flag. This is delayed when the subgraph is profitable
5396 and we process the loop for remaining unvectorized if-converted code. */
5397 if (!orig_loop
|| !profitable
)
5398 FOR_EACH_VEC_ELT (scalar_costs
, i
, cost
)
5399 gimple_set_visited (cost
->stmt_info
->stmt
, false);
5401 scalar_costs
.release ();
5402 vector_costs
.release ();
5407 /* qsort comparator for lane defs. */
5410 vld_cmp (const void *a_
, const void *b_
)
5412 auto *a
= (const std::pair
<unsigned, tree
> *)a_
;
5413 auto *b
= (const std::pair
<unsigned, tree
> *)b_
;
5414 return a
->first
- b
->first
;
5417 /* Return true if USE_STMT is a vector lane insert into VEC and set
5418 *THIS_LANE to the lane number that is set. */
5421 vect_slp_is_lane_insert (gimple
*use_stmt
, tree vec
, unsigned *this_lane
)
5423 gassign
*use_ass
= dyn_cast
<gassign
*> (use_stmt
);
5425 || gimple_assign_rhs_code (use_ass
) != BIT_INSERT_EXPR
5427 ? gimple_assign_rhs1 (use_ass
) != vec
5428 : ((vec
= gimple_assign_rhs1 (use_ass
)), false))
5429 || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec
)),
5430 TREE_TYPE (gimple_assign_rhs2 (use_ass
)))
5431 || !constant_multiple_p
5432 (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass
)),
5433 tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec
)))),
5439 /* Find any vectorizable constructors and add them to the grouped_store
5443 vect_slp_check_for_constructors (bb_vec_info bb_vinfo
)
5445 for (unsigned i
= 0; i
< bb_vinfo
->bbs
.length (); ++i
)
5446 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb_vinfo
->bbs
[i
]);
5447 !gsi_end_p (gsi
); gsi_next (&gsi
))
5449 gassign
*assign
= dyn_cast
<gassign
*> (gsi_stmt (gsi
));
5453 tree rhs
= gimple_assign_rhs1 (assign
);
5454 enum tree_code code
= gimple_assign_rhs_code (assign
);
5455 use_operand_p use_p
;
5457 if (code
== CONSTRUCTOR
)
5459 if (!VECTOR_TYPE_P (TREE_TYPE (rhs
))
5460 || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)),
5461 CONSTRUCTOR_NELTS (rhs
))
5462 || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs
, 0)->value
))
5463 || uniform_vector_p (rhs
))
5468 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs
), j
, val
)
5469 if (TREE_CODE (val
) != SSA_NAME
5470 || !bb_vinfo
->lookup_def (val
))
5472 if (j
!= CONSTRUCTOR_NELTS (rhs
))
5475 stmt_vec_info stmt_info
= bb_vinfo
->lookup_stmt (assign
);
5476 BB_VINFO_GROUPED_STORES (bb_vinfo
).safe_push (stmt_info
);
5478 else if (code
== BIT_INSERT_EXPR
5479 && VECTOR_TYPE_P (TREE_TYPE (rhs
))
5480 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)).is_constant ()
5481 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs
)).to_constant () > 1
5482 && integer_zerop (gimple_assign_rhs3 (assign
))
5483 && useless_type_conversion_p
5484 (TREE_TYPE (TREE_TYPE (rhs
)),
5485 TREE_TYPE (gimple_assign_rhs2 (assign
)))
5486 && bb_vinfo
->lookup_def (gimple_assign_rhs2 (assign
)))
5488 /* We start to match on insert to lane zero but since the
5489 inserts need not be ordered we'd have to search both
5490 the def and the use chains. */
5491 tree vectype
= TREE_TYPE (rhs
);
5492 unsigned nlanes
= TYPE_VECTOR_SUBPARTS (vectype
).to_constant ();
5493 auto_vec
<std::pair
<unsigned, tree
> > lane_defs (nlanes
);
5494 auto_sbitmap
lanes (nlanes
);
5495 bitmap_clear (lanes
);
5496 bitmap_set_bit (lanes
, 0);
5497 tree def
= gimple_assign_lhs (assign
);
5498 lane_defs
.quick_push
5499 (std::make_pair (0, gimple_assign_rhs2 (assign
)));
5500 unsigned lanes_found
= 1;
5501 /* Start with the use chains, the last stmt will be the root. */
5502 stmt_vec_info last
= bb_vinfo
->lookup_stmt (assign
);
5503 vec
<stmt_vec_info
> roots
= vNULL
;
5504 roots
.safe_push (last
);
5507 use_operand_p use_p
;
5509 if (!single_imm_use (def
, &use_p
, &use_stmt
))
5512 if (!bb_vinfo
->lookup_stmt (use_stmt
)
5513 || !vect_slp_is_lane_insert (use_stmt
, def
, &this_lane
)
5514 || !bb_vinfo
->lookup_def (gimple_assign_rhs2 (use_stmt
)))
5516 if (bitmap_bit_p (lanes
, this_lane
))
5519 bitmap_set_bit (lanes
, this_lane
);
5520 gassign
*use_ass
= as_a
<gassign
*> (use_stmt
);
5521 lane_defs
.quick_push (std::make_pair
5522 (this_lane
, gimple_assign_rhs2 (use_ass
)));
5523 last
= bb_vinfo
->lookup_stmt (use_ass
);
5524 roots
.safe_push (last
);
5525 def
= gimple_assign_lhs (use_ass
);
5527 while (lanes_found
< nlanes
);
5528 if (roots
.length () > 1)
5529 std::swap(roots
[0], roots
[roots
.length () - 1]);
5530 if (lanes_found
< nlanes
)
5532 /* Now search the def chain. */
5533 def
= gimple_assign_rhs1 (assign
);
5536 if (TREE_CODE (def
) != SSA_NAME
5537 || !has_single_use (def
))
5539 gimple
*def_stmt
= SSA_NAME_DEF_STMT (def
);
5541 if (!bb_vinfo
->lookup_stmt (def_stmt
)
5542 || !vect_slp_is_lane_insert (def_stmt
,
5543 NULL_TREE
, &this_lane
)
5544 || !bb_vinfo
->lookup_def (gimple_assign_rhs2 (def_stmt
)))
5546 if (bitmap_bit_p (lanes
, this_lane
))
5549 bitmap_set_bit (lanes
, this_lane
);
5550 lane_defs
.quick_push (std::make_pair
5552 gimple_assign_rhs2 (def_stmt
)));
5553 roots
.safe_push (bb_vinfo
->lookup_stmt (def_stmt
));
5554 def
= gimple_assign_rhs1 (def_stmt
);
5556 while (lanes_found
< nlanes
);
5558 if (lanes_found
== nlanes
)
5560 /* Sort lane_defs after the lane index and register the root. */
5561 lane_defs
.qsort (vld_cmp
);
5562 vec
<stmt_vec_info
> stmts
;
5563 stmts
.create (nlanes
);
5564 for (unsigned i
= 0; i
< nlanes
; ++i
)
5565 stmts
.quick_push (bb_vinfo
->lookup_def (lane_defs
[i
].second
));
5566 bb_vinfo
->roots
.safe_push (slp_root (slp_inst_kind_ctor
,
5572 else if (!VECTOR_TYPE_P (TREE_TYPE (rhs
))
5573 && (associative_tree_code (code
) || code
== MINUS_EXPR
)
5574 /* ??? The flag_associative_math and TYPE_OVERFLOW_WRAPS
5575 checks pessimize a two-element reduction. PR54400.
5576 ??? In-order reduction could be handled if we only
5577 traverse one operand chain in vect_slp_linearize_chain. */
5578 && ((FLOAT_TYPE_P (TREE_TYPE (rhs
)) && flag_associative_math
)
5579 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5580 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (rhs
))))
5581 /* Ops with constants at the tail can be stripped here. */
5582 && TREE_CODE (rhs
) == SSA_NAME
5583 && TREE_CODE (gimple_assign_rhs2 (assign
)) == SSA_NAME
5584 /* Should be the chain end. */
5585 && (!single_imm_use (gimple_assign_lhs (assign
),
5587 || !is_gimple_assign (use_stmt
)
5588 || (gimple_assign_rhs_code (use_stmt
) != code
5589 && ((code
!= PLUS_EXPR
&& code
!= MINUS_EXPR
)
5590 || (gimple_assign_rhs_code (use_stmt
)
5591 != (code
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
))))))
5593 /* We start the match at the end of a possible association
5595 auto_vec
<chain_op_t
> chain
;
5596 auto_vec
<std::pair
<tree_code
, gimple
*> > worklist
;
5597 auto_vec
<gimple
*> chain_stmts
;
5598 gimple
*code_stmt
= NULL
, *alt_code_stmt
= NULL
;
5599 if (code
== MINUS_EXPR
)
5601 internal_fn reduc_fn
;
5602 if (!reduction_fn_for_scalar_code (code
, &reduc_fn
)
5603 || reduc_fn
== IFN_LAST
)
5605 vect_slp_linearize_chain (bb_vinfo
, worklist
, chain
, code
, assign
,
5607 code_stmt
, alt_code_stmt
, &chain_stmts
);
5608 if (chain
.length () > 1)
5610 /* Sort the chain according to def_type and operation. */
5611 chain
.sort (dt_sort_cmp
, bb_vinfo
);
5612 /* ??? Now we'd want to strip externals and constants
5613 but record those to be handled in the epilogue. */
5614 /* ??? For now do not allow mixing ops or externs/constants. */
5615 bool invalid
= false;
5616 for (unsigned i
= 0; i
< chain
.length (); ++i
)
5617 if (chain
[i
].dt
!= vect_internal_def
5618 || chain
[i
].code
!= code
)
5622 vec
<stmt_vec_info
> stmts
;
5623 stmts
.create (chain
.length ());
5624 for (unsigned i
= 0; i
< chain
.length (); ++i
)
5625 stmts
.quick_push (bb_vinfo
->lookup_def (chain
[i
].op
));
5626 vec
<stmt_vec_info
> roots
;
5627 roots
.create (chain_stmts
.length ());
5628 for (unsigned i
= 0; i
< chain_stmts
.length (); ++i
)
5629 roots
.quick_push (bb_vinfo
->lookup_stmt (chain_stmts
[i
]));
5630 bb_vinfo
->roots
.safe_push (slp_root (slp_inst_kind_bb_reduc
,
5638 /* Walk the grouped store chains and replace entries with their
5639 pattern variant if any. */
5642 vect_fixup_store_groups_with_patterns (vec_info
*vinfo
)
5644 stmt_vec_info first_element
;
5647 FOR_EACH_VEC_ELT (vinfo
->grouped_stores
, i
, first_element
)
5649 /* We also have CTORs in this array. */
5650 if (!STMT_VINFO_GROUPED_ACCESS (first_element
))
5652 if (STMT_VINFO_IN_PATTERN_P (first_element
))
5654 stmt_vec_info orig
= first_element
;
5655 first_element
= STMT_VINFO_RELATED_STMT (first_element
);
5656 DR_GROUP_FIRST_ELEMENT (first_element
) = first_element
;
5657 DR_GROUP_SIZE (first_element
) = DR_GROUP_SIZE (orig
);
5658 DR_GROUP_GAP (first_element
) = DR_GROUP_GAP (orig
);
5659 DR_GROUP_NEXT_ELEMENT (first_element
) = DR_GROUP_NEXT_ELEMENT (orig
);
5660 vinfo
->grouped_stores
[i
] = first_element
;
5662 stmt_vec_info prev
= first_element
;
5663 while (DR_GROUP_NEXT_ELEMENT (prev
))
5665 stmt_vec_info elt
= DR_GROUP_NEXT_ELEMENT (prev
);
5666 if (STMT_VINFO_IN_PATTERN_P (elt
))
5668 stmt_vec_info orig
= elt
;
5669 elt
= STMT_VINFO_RELATED_STMT (elt
);
5670 DR_GROUP_NEXT_ELEMENT (prev
) = elt
;
5671 DR_GROUP_GAP (elt
) = DR_GROUP_GAP (orig
);
5672 DR_GROUP_NEXT_ELEMENT (elt
) = DR_GROUP_NEXT_ELEMENT (orig
);
5674 DR_GROUP_FIRST_ELEMENT (elt
) = first_element
;
5680 /* Check if the region described by BB_VINFO can be vectorized, returning
5681 true if so. When returning false, set FATAL to true if the same failure
5682 would prevent vectorization at other vector sizes, false if it is still
5683 worth trying other sizes. N_STMTS is the number of statements in the
5687 vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo
, int n_stmts
, bool &fatal
,
5688 vec
<int> *dataref_groups
)
5690 DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
5692 slp_instance instance
;
5694 poly_uint64 min_vf
= 2;
5696 /* The first group of checks is independent of the vector size. */
5699 /* Analyze the data references. */
5701 if (!vect_analyze_data_refs (bb_vinfo
, &min_vf
, NULL
))
5703 if (dump_enabled_p ())
5704 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5705 "not vectorized: unhandled data-ref in basic "
5710 if (!vect_analyze_data_ref_accesses (bb_vinfo
, dataref_groups
))
5712 if (dump_enabled_p ())
5713 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5714 "not vectorized: unhandled data access in "
5719 vect_slp_check_for_constructors (bb_vinfo
);
5721 /* If there are no grouped stores and no constructors in the region
5722 there is no need to continue with pattern recog as vect_analyze_slp
5723 will fail anyway. */
5724 if (bb_vinfo
->grouped_stores
.is_empty ()
5725 && bb_vinfo
->roots
.is_empty ())
5727 if (dump_enabled_p ())
5728 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5729 "not vectorized: no grouped stores in "
5734 /* While the rest of the analysis below depends on it in some way. */
5737 vect_pattern_recog (bb_vinfo
);
5739 /* Update store groups from pattern processing. */
5740 vect_fixup_store_groups_with_patterns (bb_vinfo
);
5742 /* Check the SLP opportunities in the basic block, analyze and build SLP
5744 if (!vect_analyze_slp (bb_vinfo
, n_stmts
))
5746 if (dump_enabled_p ())
5748 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5749 "Failed to SLP the basic block.\n");
5750 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5751 "not vectorized: failed to find SLP opportunities "
5752 "in basic block.\n");
5757 /* Optimize permutations. */
5758 vect_optimize_slp (bb_vinfo
);
5760 /* Gather the loads reachable from the SLP graph entries. */
5761 vect_gather_slp_loads (bb_vinfo
);
5763 vect_record_base_alignments (bb_vinfo
);
5765 /* Analyze and verify the alignment of data references and the
5766 dependence in the SLP instances. */
5767 for (i
= 0; BB_VINFO_SLP_INSTANCES (bb_vinfo
).iterate (i
, &instance
); )
5769 vect_location
= instance
->location ();
5770 if (! vect_slp_analyze_instance_alignment (bb_vinfo
, instance
)
5771 || ! vect_slp_analyze_instance_dependence (bb_vinfo
, instance
))
5773 slp_tree node
= SLP_INSTANCE_TREE (instance
);
5774 stmt_vec_info stmt_info
= SLP_TREE_SCALAR_STMTS (node
)[0];
5775 if (dump_enabled_p ())
5776 dump_printf_loc (MSG_NOTE
, vect_location
,
5777 "removing SLP instance operations starting from: %G",
5779 vect_free_slp_instance (instance
);
5780 BB_VINFO_SLP_INSTANCES (bb_vinfo
).ordered_remove (i
);
5784 /* Mark all the statements that we want to vectorize as pure SLP and
5786 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance
));
5787 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance
));
5790 /* Likewise consider instance root stmts as vectorized. */
5791 FOR_EACH_VEC_ELT (SLP_INSTANCE_ROOT_STMTS (instance
), j
, root
)
5792 STMT_SLP_TYPE (root
) = pure_slp
;
5796 if (! BB_VINFO_SLP_INSTANCES (bb_vinfo
).length ())
5799 if (!vect_slp_analyze_operations (bb_vinfo
))
5801 if (dump_enabled_p ())
5802 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5803 "not vectorized: bad operation in basic block.\n");
5807 vect_bb_partition_graph (bb_vinfo
);
5812 /* Subroutine of vect_slp_bb. Try to vectorize the statements for all
5813 basic blocks in BBS, returning true on success.
5814 The region has N_STMTS statements and has the datarefs given by DATAREFS. */
5817 vect_slp_region (vec
<basic_block
> bbs
, vec
<data_reference_p
> datarefs
,
5818 vec
<int> *dataref_groups
, unsigned int n_stmts
,
5821 bb_vec_info bb_vinfo
;
5822 auto_vector_modes vector_modes
;
5824 /* Autodetect first vector size we try. */
5825 machine_mode next_vector_mode
= VOIDmode
;
5826 targetm
.vectorize
.autovectorize_vector_modes (&vector_modes
, false);
5827 unsigned int mode_i
= 0;
5829 vec_info_shared shared
;
5831 machine_mode autodetected_vector_mode
= VOIDmode
;
5834 bool vectorized
= false;
5836 bb_vinfo
= new _bb_vec_info (bbs
, &shared
);
5838 bool first_time_p
= shared
.datarefs
.is_empty ();
5839 BB_VINFO_DATAREFS (bb_vinfo
) = datarefs
;
5841 bb_vinfo
->shared
->save_datarefs ();
5843 bb_vinfo
->shared
->check_datarefs ();
5844 bb_vinfo
->vector_mode
= next_vector_mode
;
5846 if (vect_slp_analyze_bb_1 (bb_vinfo
, n_stmts
, fatal
, dataref_groups
))
5848 if (dump_enabled_p ())
5850 dump_printf_loc (MSG_NOTE
, vect_location
,
5851 "***** Analysis succeeded with vector mode"
5852 " %s\n", GET_MODE_NAME (bb_vinfo
->vector_mode
));
5853 dump_printf_loc (MSG_NOTE
, vect_location
, "SLPing BB part\n");
5856 bb_vinfo
->shared
->check_datarefs ();
5858 auto_vec
<slp_instance
> profitable_subgraphs
;
5859 for (slp_instance instance
: BB_VINFO_SLP_INSTANCES (bb_vinfo
))
5861 if (instance
->subgraph_entries
.is_empty ())
5864 vect_location
= instance
->location ();
5865 if (!unlimited_cost_model (NULL
)
5866 && !vect_bb_vectorization_profitable_p
5867 (bb_vinfo
, instance
->subgraph_entries
, orig_loop
))
5869 if (dump_enabled_p ())
5870 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5871 "not vectorized: vectorization is not "
5876 if (!dbg_cnt (vect_slp
))
5879 profitable_subgraphs
.safe_push (instance
);
5882 /* When we're vectorizing an if-converted loop body with the
5883 very-cheap cost model make sure we vectorized all if-converted
5885 if (!profitable_subgraphs
.is_empty ()
5888 gcc_assert (bb_vinfo
->bbs
.length () == 1);
5889 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb_vinfo
->bbs
[0]);
5890 !gsi_end_p (gsi
); gsi_next (&gsi
))
5892 /* The costing above left us with DCEable vectorized scalar
5893 stmts having the visited flag set on profitable
5894 subgraphs. Do the delayed clearing of the flag here. */
5895 if (gimple_visited_p (gsi_stmt (gsi
)))
5897 gimple_set_visited (gsi_stmt (gsi
), false);
5900 if (flag_vect_cost_model
!= VECT_COST_MODEL_VERY_CHEAP
)
5903 if (gassign
*ass
= dyn_cast
<gassign
*> (gsi_stmt (gsi
)))
5904 if (gimple_assign_rhs_code (ass
) == COND_EXPR
)
5906 if (!profitable_subgraphs
.is_empty ()
5907 && dump_enabled_p ())
5908 dump_printf_loc (MSG_NOTE
, vect_location
,
5909 "not profitable because of "
5910 "unprofitable if-converted scalar "
5912 profitable_subgraphs
.truncate (0);
5917 /* Finally schedule the profitable subgraphs. */
5918 for (slp_instance instance
: profitable_subgraphs
)
5920 if (!vectorized
&& dump_enabled_p ())
5921 dump_printf_loc (MSG_NOTE
, vect_location
,
5922 "Basic block will be vectorized "
5926 vect_schedule_slp (bb_vinfo
, instance
->subgraph_entries
);
5928 unsigned HOST_WIDE_INT bytes
;
5929 if (dump_enabled_p ())
5932 (bb_vinfo
->vector_mode
).is_constant (&bytes
))
5933 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
5934 "basic block part vectorized using %wu "
5935 "byte vectors\n", bytes
);
5937 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
5938 "basic block part vectorized using "
5939 "variable length vectors\n");
5945 if (dump_enabled_p ())
5946 dump_printf_loc (MSG_NOTE
, vect_location
,
5947 "***** Analysis failed with vector mode %s\n",
5948 GET_MODE_NAME (bb_vinfo
->vector_mode
));
5952 autodetected_vector_mode
= bb_vinfo
->vector_mode
;
5955 while (mode_i
< vector_modes
.length ()
5956 && vect_chooses_same_modes_p (bb_vinfo
, vector_modes
[mode_i
]))
5958 if (dump_enabled_p ())
5959 dump_printf_loc (MSG_NOTE
, vect_location
,
5960 "***** The result for vector mode %s would"
5962 GET_MODE_NAME (vector_modes
[mode_i
]));
5968 if (mode_i
< vector_modes
.length ()
5969 && VECTOR_MODE_P (autodetected_vector_mode
)
5970 && (related_vector_mode (vector_modes
[mode_i
],
5971 GET_MODE_INNER (autodetected_vector_mode
))
5972 == autodetected_vector_mode
)
5973 && (related_vector_mode (autodetected_vector_mode
,
5974 GET_MODE_INNER (vector_modes
[mode_i
]))
5975 == vector_modes
[mode_i
]))
5977 if (dump_enabled_p ())
5978 dump_printf_loc (MSG_NOTE
, vect_location
,
5979 "***** Skipping vector mode %s, which would"
5980 " repeat the analysis for %s\n",
5981 GET_MODE_NAME (vector_modes
[mode_i
]),
5982 GET_MODE_NAME (autodetected_vector_mode
));
5987 || mode_i
== vector_modes
.length ()
5988 || autodetected_vector_mode
== VOIDmode
5989 /* If vect_slp_analyze_bb_1 signaled that analysis for all
5990 vector sizes will fail do not bother iterating. */
5994 /* Try the next biggest vector size. */
5995 next_vector_mode
= vector_modes
[mode_i
++];
5996 if (dump_enabled_p ())
5997 dump_printf_loc (MSG_NOTE
, vect_location
,
5998 "***** Re-trying analysis with vector mode %s\n",
5999 GET_MODE_NAME (next_vector_mode
));
6004 /* Main entry for the BB vectorizer. Analyze and transform BBS, returns
6005 true if anything in the basic-block was vectorized. */
6008 vect_slp_bbs (const vec
<basic_block
> &bbs
, loop_p orig_loop
)
6010 vec
<data_reference_p
> datarefs
= vNULL
;
6011 auto_vec
<int> dataref_groups
;
6013 int current_group
= 0;
6015 for (unsigned i
= 0; i
< bbs
.length (); i
++)
6017 basic_block bb
= bbs
[i
];
6018 for (gimple_stmt_iterator gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
);
6021 gimple
*stmt
= gsi_stmt (gsi
);
6022 if (is_gimple_debug (stmt
))
6027 if (gimple_location (stmt
) != UNKNOWN_LOCATION
)
6028 vect_location
= stmt
;
6030 if (!vect_find_stmt_data_reference (NULL
, stmt
, &datarefs
,
6031 &dataref_groups
, current_group
))
6034 /* New BBs always start a new DR group. */
6038 return vect_slp_region (bbs
, datarefs
, &dataref_groups
, insns
, orig_loop
);
6041 /* Special entry for the BB vectorizer. Analyze and transform a single
6042 if-converted BB with ORIG_LOOPs body being the not if-converted
6043 representation. Returns true if anything in the basic-block was
6047 vect_slp_if_converted_bb (basic_block bb
, loop_p orig_loop
)
6049 auto_vec
<basic_block
> bbs
;
6051 return vect_slp_bbs (bbs
, orig_loop
);
6054 /* Main entry for the BB vectorizer. Analyze and transform BB, returns
6055 true if anything in the basic-block was vectorized. */
6058 vect_slp_function (function
*fun
)
6061 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fun
));
6062 unsigned n
= pre_and_rev_post_order_compute_fn (fun
, NULL
, rpo
, false);
6064 /* For the moment split the function into pieces to avoid making
6065 the iteration on the vector mode moot. Split at points we know
6066 to not handle well which is CFG merges (SLP discovery doesn't
6067 handle non-loop-header PHIs) and loop exits. Since pattern
6068 recog requires reverse iteration to visit uses before defs
6069 simply chop RPO into pieces. */
6070 auto_vec
<basic_block
> bbs
;
6071 for (unsigned i
= 0; i
< n
; i
++)
6073 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]);
6076 /* Split when a BB is not dominated by the first block. */
6077 if (!bbs
.is_empty ()
6078 && !dominated_by_p (CDI_DOMINATORS
, bb
, bbs
[0]))
6080 if (dump_enabled_p ())
6081 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6082 "splitting region at dominance boundary bb%d\n",
6086 /* Split when the loop determined by the first block
6087 is exited. This is because we eventually insert
6088 invariants at region begin. */
6089 else if (!bbs
.is_empty ()
6090 && bbs
[0]->loop_father
!= bb
->loop_father
6091 && !flow_loop_nested_p (bbs
[0]->loop_father
, bb
->loop_father
))
6093 if (dump_enabled_p ())
6094 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6095 "splitting region at loop %d exit at bb%d\n",
6096 bbs
[0]->loop_father
->num
, bb
->index
);
6100 if (split
&& !bbs
.is_empty ())
6102 r
|= vect_slp_bbs (bbs
, NULL
);
6104 bbs
.quick_push (bb
);
6109 /* When we have a stmt ending this block and defining a
6110 value we have to insert on edges when inserting after it for
6111 a vector containing its definition. Avoid this for now. */
6112 if (gimple
*last
= last_stmt (bb
))
6113 if (gimple_get_lhs (last
)
6114 && is_ctrl_altering_stmt (last
))
6116 if (dump_enabled_p ())
6117 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6118 "splitting region at control altering "
6119 "definition %G", last
);
6120 r
|= vect_slp_bbs (bbs
, NULL
);
6125 if (!bbs
.is_empty ())
6126 r
|= vect_slp_bbs (bbs
, NULL
);
6133 /* Build a variable-length vector in which the elements in ELTS are repeated
6134 to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
6135 RESULTS and add any new instructions to SEQ.
6137 The approach we use is:
6139 (1) Find a vector mode VM with integer elements of mode IM.
6141 (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
6142 ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
6143 from small vectors to IM.
6145 (3) Duplicate each ELTS'[I] into a vector of mode VM.
6147 (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
6148 correct byte contents.
6150 (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
6152 We try to find the largest IM for which this sequence works, in order
6153 to cut down on the number of interleaves. */
6156 duplicate_and_interleave (vec_info
*vinfo
, gimple_seq
*seq
, tree vector_type
,
6157 const vec
<tree
> &elts
, unsigned int nresults
,
6160 unsigned int nelts
= elts
.length ();
6161 tree element_type
= TREE_TYPE (vector_type
);
6163 /* (1) Find a vector mode VM with integer elements of mode IM. */
6164 unsigned int nvectors
= 1;
6165 tree new_vector_type
;
6167 if (!can_duplicate_and_interleave_p (vinfo
, nelts
, element_type
,
6168 &nvectors
, &new_vector_type
,
6172 /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
6173 unsigned int partial_nelts
= nelts
/ nvectors
;
6174 tree partial_vector_type
= build_vector_type (element_type
, partial_nelts
);
6176 tree_vector_builder partial_elts
;
6177 auto_vec
<tree
, 32> pieces (nvectors
* 2);
6178 pieces
.quick_grow_cleared (nvectors
* 2);
6179 for (unsigned int i
= 0; i
< nvectors
; ++i
)
6181 /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
6182 ELTS' has mode IM. */
6183 partial_elts
.new_vector (partial_vector_type
, partial_nelts
, 1);
6184 for (unsigned int j
= 0; j
< partial_nelts
; ++j
)
6185 partial_elts
.quick_push (elts
[i
* partial_nelts
+ j
]);
6186 tree t
= gimple_build_vector (seq
, &partial_elts
);
6187 t
= gimple_build (seq
, VIEW_CONVERT_EXPR
,
6188 TREE_TYPE (new_vector_type
), t
);
6190 /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
6191 pieces
[i
] = gimple_build_vector_from_val (seq
, new_vector_type
, t
);
6194 /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
6195 correct byte contents.
6197 Conceptually, we need to repeat the following operation log2(nvectors)
6198 times, where hi_start = nvectors / 2:
6200 out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
6201 out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
6203 However, if each input repeats every N elements and the VF is
6204 a multiple of N * 2, the HI result is the same as the LO result.
6205 This will be true for the first N1 iterations of the outer loop,
6206 followed by N2 iterations for which both the LO and HI results
6209 N1 + N2 = log2(nvectors)
6211 Each "N1 iteration" doubles the number of redundant vectors and the
6212 effect of the process as a whole is to have a sequence of nvectors/2**N1
6213 vectors that repeats 2**N1 times. Rather than generate these redundant
6214 vectors, we halve the number of vectors for each N1 iteration. */
6215 unsigned int in_start
= 0;
6216 unsigned int out_start
= nvectors
;
6217 unsigned int new_nvectors
= nvectors
;
6218 for (unsigned int in_repeat
= 1; in_repeat
< nvectors
; in_repeat
*= 2)
6220 unsigned int hi_start
= new_nvectors
/ 2;
6221 unsigned int out_i
= 0;
6222 for (unsigned int in_i
= 0; in_i
< new_nvectors
; ++in_i
)
6225 && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type
),
6229 tree output
= make_ssa_name (new_vector_type
);
6230 tree input1
= pieces
[in_start
+ (in_i
/ 2)];
6231 tree input2
= pieces
[in_start
+ (in_i
/ 2) + hi_start
];
6232 gassign
*stmt
= gimple_build_assign (output
, VEC_PERM_EXPR
,
6234 permutes
[in_i
& 1]);
6235 gimple_seq_add_stmt (seq
, stmt
);
6236 pieces
[out_start
+ out_i
] = output
;
6239 std::swap (in_start
, out_start
);
6240 new_nvectors
= out_i
;
6243 /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
6244 results
.reserve (nresults
);
6245 for (unsigned int i
= 0; i
< nresults
; ++i
)
6246 if (i
< new_nvectors
)
6247 results
.quick_push (gimple_build (seq
, VIEW_CONVERT_EXPR
, vector_type
,
6248 pieces
[in_start
+ i
]));
6250 results
.quick_push (results
[i
- new_nvectors
]);
6254 /* For constant and loop invariant defs in OP_NODE this function creates
6255 vector defs that will be used in the vectorized stmts and stores them
6256 to SLP_TREE_VEC_DEFS of OP_NODE. */
6259 vect_create_constant_vectors (vec_info
*vinfo
, slp_tree op_node
)
6261 unsigned HOST_WIDE_INT nunits
;
6263 unsigned j
, number_of_places_left_in_vector
;
6266 int group_size
= op_node
->ops
.length ();
6267 unsigned int vec_num
, i
;
6268 unsigned number_of_copies
= 1;
6270 gimple_seq ctor_seq
= NULL
;
6271 auto_vec
<tree
, 16> permute_results
;
6273 /* We always want SLP_TREE_VECTYPE (op_node) here correctly set. */
6274 vector_type
= SLP_TREE_VECTYPE (op_node
);
6276 unsigned int number_of_vectors
= SLP_TREE_NUMBER_OF_VEC_STMTS (op_node
);
6277 SLP_TREE_VEC_DEFS (op_node
).create (number_of_vectors
);
6278 auto_vec
<tree
> voprnds (number_of_vectors
);
6280 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
6281 created vectors. It is greater than 1 if unrolling is performed.
6283 For example, we have two scalar operands, s1 and s2 (e.g., group of
6284 strided accesses of size two), while NUNITS is four (i.e., four scalars
6285 of this type can be packed in a vector). The output vector will contain
6286 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
6289 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
6290 containing the operands.
6292 For example, NUNITS is four as before, and the group size is 8
6293 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
6294 {s5, s6, s7, s8}. */
6296 /* When using duplicate_and_interleave, we just need one element for
6297 each scalar statement. */
6298 if (!TYPE_VECTOR_SUBPARTS (vector_type
).is_constant (&nunits
))
6299 nunits
= group_size
;
6301 number_of_copies
= nunits
* number_of_vectors
/ group_size
;
6303 number_of_places_left_in_vector
= nunits
;
6305 tree_vector_builder
elts (vector_type
, nunits
, 1);
6306 elts
.quick_grow (nunits
);
6307 stmt_vec_info insert_after
= NULL
;
6308 for (j
= 0; j
< number_of_copies
; j
++)
6311 for (i
= group_size
- 1; op_node
->ops
.iterate (i
, &op
); i
--)
6313 /* Create 'vect_ = {op0,op1,...,opn}'. */
6314 number_of_places_left_in_vector
--;
6316 if (!types_compatible_p (TREE_TYPE (vector_type
), TREE_TYPE (op
)))
6318 if (CONSTANT_CLASS_P (op
))
6320 if (VECTOR_BOOLEAN_TYPE_P (vector_type
))
6322 /* Can't use VIEW_CONVERT_EXPR for booleans because
6323 of possibly different sizes of scalar value and
6325 if (integer_zerop (op
))
6326 op
= build_int_cst (TREE_TYPE (vector_type
), 0);
6327 else if (integer_onep (op
))
6328 op
= build_all_ones_cst (TREE_TYPE (vector_type
));
6333 op
= fold_unary (VIEW_CONVERT_EXPR
,
6334 TREE_TYPE (vector_type
), op
);
6335 gcc_assert (op
&& CONSTANT_CLASS_P (op
));
6339 tree new_temp
= make_ssa_name (TREE_TYPE (vector_type
));
6341 if (VECTOR_BOOLEAN_TYPE_P (vector_type
))
6344 = build_all_ones_cst (TREE_TYPE (vector_type
));
6346 = build_zero_cst (TREE_TYPE (vector_type
));
6347 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op
)));
6348 init_stmt
= gimple_build_assign (new_temp
, COND_EXPR
,
6354 op
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (vector_type
),
6357 = gimple_build_assign (new_temp
, VIEW_CONVERT_EXPR
,
6360 gimple_seq_add_stmt (&ctor_seq
, init_stmt
);
6364 elts
[number_of_places_left_in_vector
] = op
;
6365 if (!CONSTANT_CLASS_P (op
))
6367 /* For BB vectorization we have to compute an insert location
6368 when a def is inside the analyzed region since we cannot
6369 simply insert at the BB start in this case. */
6370 stmt_vec_info opdef
;
6371 if (TREE_CODE (orig_op
) == SSA_NAME
6372 && !SSA_NAME_IS_DEFAULT_DEF (orig_op
)
6373 && is_a
<bb_vec_info
> (vinfo
)
6374 && (opdef
= vinfo
->lookup_def (orig_op
)))
6377 insert_after
= opdef
;
6379 insert_after
= get_later_stmt (insert_after
, opdef
);
6382 if (number_of_places_left_in_vector
== 0)
6385 ? multiple_p (TYPE_VECTOR_SUBPARTS (vector_type
), nunits
)
6386 : known_eq (TYPE_VECTOR_SUBPARTS (vector_type
), nunits
))
6387 vec_cst
= gimple_build_vector (&ctor_seq
, &elts
);
6390 if (permute_results
.is_empty ())
6391 duplicate_and_interleave (vinfo
, &ctor_seq
, vector_type
,
6392 elts
, number_of_vectors
,
6394 vec_cst
= permute_results
[number_of_vectors
- j
- 1];
6396 if (!gimple_seq_empty_p (ctor_seq
))
6400 gimple_stmt_iterator gsi
;
6401 if (gimple_code (insert_after
->stmt
) == GIMPLE_PHI
)
6403 gsi
= gsi_after_labels (gimple_bb (insert_after
->stmt
));
6404 gsi_insert_seq_before (&gsi
, ctor_seq
,
6405 GSI_CONTINUE_LINKING
);
6407 else if (!stmt_ends_bb_p (insert_after
->stmt
))
6409 gsi
= gsi_for_stmt (insert_after
->stmt
);
6410 gsi_insert_seq_after (&gsi
, ctor_seq
,
6411 GSI_CONTINUE_LINKING
);
6415 /* When we want to insert after a def where the
6416 defining stmt throws then insert on the fallthru
6418 edge e
= find_fallthru_edge
6419 (gimple_bb (insert_after
->stmt
)->succs
);
6421 = gsi_insert_seq_on_edge_immediate (e
, ctor_seq
);
6422 gcc_assert (!new_bb
);
6426 vinfo
->insert_seq_on_entry (NULL
, ctor_seq
);
6429 voprnds
.quick_push (vec_cst
);
6430 insert_after
= NULL
;
6431 number_of_places_left_in_vector
= nunits
;
6433 elts
.new_vector (vector_type
, nunits
, 1);
6434 elts
.quick_grow (nunits
);
6439 /* Since the vectors are created in the reverse order, we should invert
6441 vec_num
= voprnds
.length ();
6442 for (j
= vec_num
; j
!= 0; j
--)
6444 vop
= voprnds
[j
- 1];
6445 SLP_TREE_VEC_DEFS (op_node
).quick_push (vop
);
6448 /* In case that VF is greater than the unrolling factor needed for the SLP
6449 group of stmts, NUMBER_OF_VECTORS to be created is greater than
6450 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
6451 to replicate the vectors. */
6452 while (number_of_vectors
> SLP_TREE_VEC_DEFS (op_node
).length ())
6453 for (i
= 0; SLP_TREE_VEC_DEFS (op_node
).iterate (i
, &vop
) && i
< vec_num
;
6455 SLP_TREE_VEC_DEFS (op_node
).quick_push (vop
);
6458 /* Get the Ith vectorized definition from SLP_NODE. */
6461 vect_get_slp_vect_def (slp_tree slp_node
, unsigned i
)
6463 if (SLP_TREE_VEC_STMTS (slp_node
).exists ())
6464 return gimple_get_lhs (SLP_TREE_VEC_STMTS (slp_node
)[i
]);
6466 return SLP_TREE_VEC_DEFS (slp_node
)[i
];
6469 /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS. */
6472 vect_get_slp_defs (slp_tree slp_node
, vec
<tree
> *vec_defs
)
6474 vec_defs
->create (SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
));
6475 if (SLP_TREE_DEF_TYPE (slp_node
) == vect_internal_def
)
6478 gimple
*vec_def_stmt
;
6479 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node
), j
, vec_def_stmt
)
6480 vec_defs
->quick_push (gimple_get_lhs (vec_def_stmt
));
6483 vec_defs
->splice (SLP_TREE_VEC_DEFS (slp_node
));
6486 /* Get N vectorized definitions for SLP_NODE. */
6489 vect_get_slp_defs (vec_info
*,
6490 slp_tree slp_node
, vec
<vec
<tree
> > *vec_oprnds
, unsigned n
)
6493 n
= SLP_TREE_CHILDREN (slp_node
).length ();
6495 for (unsigned i
= 0; i
< n
; ++i
)
6497 slp_tree child
= SLP_TREE_CHILDREN (slp_node
)[i
];
6498 vec
<tree
> vec_defs
= vNULL
;
6499 vect_get_slp_defs (child
, &vec_defs
);
6500 vec_oprnds
->quick_push (vec_defs
);
6504 /* Generate vector permute statements from a list of loads in DR_CHAIN.
6505 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
6506 permute statements for the SLP node NODE. Store the number of vector
6507 permute instructions in *N_PERMS and the number of vector load
6508 instructions in *N_LOADS. If DCE_CHAIN is true, remove all definitions
6509 that were not needed. */
6512 vect_transform_slp_perm_load (vec_info
*vinfo
,
6513 slp_tree node
, const vec
<tree
> &dr_chain
,
6514 gimple_stmt_iterator
*gsi
, poly_uint64 vf
,
6515 bool analyze_only
, unsigned *n_perms
,
6516 unsigned int *n_loads
, bool dce_chain
)
6518 stmt_vec_info stmt_info
= SLP_TREE_SCALAR_STMTS (node
)[0];
6520 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6521 unsigned int group_size
= SLP_TREE_SCALAR_STMTS (node
).length ();
6522 unsigned int mask_element
;
6525 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info
))
6528 stmt_info
= DR_GROUP_FIRST_ELEMENT (stmt_info
);
6530 mode
= TYPE_MODE (vectype
);
6531 poly_uint64 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6533 /* Initialize the vect stmts of NODE to properly insert the generated
6536 for (unsigned i
= SLP_TREE_VEC_STMTS (node
).length ();
6537 i
< SLP_TREE_NUMBER_OF_VEC_STMTS (node
); i
++)
6538 SLP_TREE_VEC_STMTS (node
).quick_push (NULL
);
6540 /* Generate permutation masks for every NODE. Number of masks for each NODE
6541 is equal to GROUP_SIZE.
6542 E.g., we have a group of three nodes with three loads from the same
6543 location in each node, and the vector size is 4. I.e., we have a
6544 a0b0c0a1b1c1... sequence and we need to create the following vectors:
6545 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
6546 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
6549 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
6550 The last mask is illegal since we assume two operands for permute
6551 operation, and the mask element values can't be outside that range.
6552 Hence, the last mask must be converted into {2,5,5,5}.
6553 For the first two permutations we need the first and the second input
6554 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
6555 we need the second and the third vectors: {b1,c1,a2,b2} and
6558 int vect_stmts_counter
= 0;
6559 unsigned int index
= 0;
6560 int first_vec_index
= -1;
6561 int second_vec_index
= -1;
6565 vec_perm_builder mask
;
6566 unsigned int nelts_to_build
;
6567 unsigned int nvectors_per_build
;
6568 unsigned int in_nlanes
;
6569 bool repeating_p
= (group_size
== DR_GROUP_SIZE (stmt_info
)
6570 && multiple_p (nunits
, group_size
));
6573 /* A single vector contains a whole number of copies of the node, so:
6574 (a) all permutes can use the same mask; and
6575 (b) the permutes only need a single vector input. */
6576 mask
.new_vector (nunits
, group_size
, 3);
6577 nelts_to_build
= mask
.encoded_nelts ();
6578 nvectors_per_build
= SLP_TREE_VEC_STMTS (node
).length ();
6579 in_nlanes
= DR_GROUP_SIZE (stmt_info
) * 3;
6583 /* We need to construct a separate mask for each vector statement. */
6584 unsigned HOST_WIDE_INT const_nunits
, const_vf
;
6585 if (!nunits
.is_constant (&const_nunits
)
6586 || !vf
.is_constant (&const_vf
))
6588 mask
.new_vector (const_nunits
, const_nunits
, 1);
6589 nelts_to_build
= const_vf
* group_size
;
6590 nvectors_per_build
= 1;
6591 in_nlanes
= const_vf
* DR_GROUP_SIZE (stmt_info
);
6593 auto_sbitmap
used_in_lanes (in_nlanes
);
6594 bitmap_clear (used_in_lanes
);
6595 auto_bitmap used_defs
;
6597 unsigned int count
= mask
.encoded_nelts ();
6598 mask
.quick_grow (count
);
6599 vec_perm_indices indices
;
6601 for (unsigned int j
= 0; j
< nelts_to_build
; j
++)
6603 unsigned int iter_num
= j
/ group_size
;
6604 unsigned int stmt_num
= j
% group_size
;
6605 unsigned int i
= (iter_num
* DR_GROUP_SIZE (stmt_info
)
6606 + SLP_TREE_LOAD_PERMUTATION (node
)[stmt_num
]);
6607 bitmap_set_bit (used_in_lanes
, i
);
6610 first_vec_index
= 0;
6615 /* Enforced before the loop when !repeating_p. */
6616 unsigned int const_nunits
= nunits
.to_constant ();
6617 vec_index
= i
/ const_nunits
;
6618 mask_element
= i
% const_nunits
;
6619 if (vec_index
== first_vec_index
6620 || first_vec_index
== -1)
6622 first_vec_index
= vec_index
;
6624 else if (vec_index
== second_vec_index
6625 || second_vec_index
== -1)
6627 second_vec_index
= vec_index
;
6628 mask_element
+= const_nunits
;
6632 if (dump_enabled_p ())
6633 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6634 "permutation requires at "
6635 "least three vectors %G",
6637 gcc_assert (analyze_only
);
6641 gcc_assert (mask_element
< 2 * const_nunits
);
6644 if (mask_element
!= index
)
6646 mask
[index
++] = mask_element
;
6648 if (index
== count
&& !noop_p
)
6650 indices
.new_vector (mask
, second_vec_index
== -1 ? 1 : 2, nunits
);
6651 if (!can_vec_perm_const_p (mode
, indices
))
6653 if (dump_enabled_p ())
6655 dump_printf_loc (MSG_MISSED_OPTIMIZATION
,
6657 "unsupported vect permute { ");
6658 for (i
= 0; i
< count
; ++i
)
6660 dump_dec (MSG_MISSED_OPTIMIZATION
, mask
[i
]);
6661 dump_printf (MSG_MISSED_OPTIMIZATION
, " ");
6663 dump_printf (MSG_MISSED_OPTIMIZATION
, "}\n");
6665 gcc_assert (analyze_only
);
6676 tree mask_vec
= NULL_TREE
;
6679 mask_vec
= vect_gen_perm_mask_checked (vectype
, indices
);
6681 if (second_vec_index
== -1)
6682 second_vec_index
= first_vec_index
;
6684 for (unsigned int ri
= 0; ri
< nvectors_per_build
; ++ri
)
6686 /* Generate the permute statement if necessary. */
6687 tree first_vec
= dr_chain
[first_vec_index
+ ri
];
6688 tree second_vec
= dr_chain
[second_vec_index
+ ri
];
6692 gassign
*stmt
= as_a
<gassign
*> (stmt_info
->stmt
);
6694 = vect_create_destination_var (gimple_assign_lhs (stmt
),
6696 perm_dest
= make_ssa_name (perm_dest
);
6698 = gimple_build_assign (perm_dest
, VEC_PERM_EXPR
,
6699 first_vec
, second_vec
,
6701 vect_finish_stmt_generation (vinfo
, stmt_info
, perm_stmt
,
6705 bitmap_set_bit (used_defs
, first_vec_index
+ ri
);
6706 bitmap_set_bit (used_defs
, second_vec_index
+ ri
);
6711 /* If mask was NULL_TREE generate the requested
6712 identity transform. */
6713 perm_stmt
= SSA_NAME_DEF_STMT (first_vec
);
6715 bitmap_set_bit (used_defs
, first_vec_index
+ ri
);
6718 /* Store the vector statement in NODE. */
6719 SLP_TREE_VEC_STMTS (node
)[vect_stmts_counter
++] = perm_stmt
;
6724 first_vec_index
= -1;
6725 second_vec_index
= -1;
6733 *n_loads
= SLP_TREE_NUMBER_OF_VEC_STMTS (node
);
6736 /* Enforced above when !repeating_p. */
6737 unsigned int const_nunits
= nunits
.to_constant ();
6739 bool load_seen
= false;
6740 for (unsigned i
= 0; i
< in_nlanes
; ++i
)
6742 if (i
% const_nunits
== 0)
6748 if (bitmap_bit_p (used_in_lanes
, i
))
6757 for (unsigned i
= 0; i
< dr_chain
.length (); ++i
)
6758 if (!bitmap_bit_p (used_defs
, i
))
6760 gimple
*stmt
= SSA_NAME_DEF_STMT (dr_chain
[i
]);
6761 gimple_stmt_iterator rgsi
= gsi_for_stmt (stmt
);
6762 gsi_remove (&rgsi
, true);
6763 release_defs (stmt
);
6769 /* Produce the next vector result for SLP permutation NODE by adding a vector
6770 statement at GSI. If MASK_VEC is nonnull, add:
6772 <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
6776 <new SSA name> = FIRST_DEF. */
6779 vect_add_slp_permutation (vec_info
*vinfo
, gimple_stmt_iterator
*gsi
,
6780 slp_tree node
, tree first_def
, tree second_def
,
6783 tree vectype
= SLP_TREE_VECTYPE (node
);
6785 /* ??? We SLP match existing vector element extracts but
6786 allow punning which we need to re-instantiate at uses
6787 but have no good way of explicitly representing. */
6788 if (!types_compatible_p (TREE_TYPE (first_def
), vectype
))
6791 = gimple_build_assign (make_ssa_name (vectype
),
6792 build1 (VIEW_CONVERT_EXPR
, vectype
, first_def
));
6793 vect_finish_stmt_generation (vinfo
, NULL
, conv_stmt
, gsi
);
6794 first_def
= gimple_assign_lhs (conv_stmt
);
6797 tree perm_dest
= make_ssa_name (vectype
);
6800 if (!types_compatible_p (TREE_TYPE (second_def
), vectype
))
6803 = gimple_build_assign (make_ssa_name (vectype
),
6804 build1 (VIEW_CONVERT_EXPR
,
6805 vectype
, second_def
));
6806 vect_finish_stmt_generation (vinfo
, NULL
, conv_stmt
, gsi
);
6807 second_def
= gimple_assign_lhs (conv_stmt
);
6809 perm_stmt
= gimple_build_assign (perm_dest
, VEC_PERM_EXPR
,
6810 first_def
, second_def
,
6814 /* We need a copy here in case the def was external. */
6815 perm_stmt
= gimple_build_assign (perm_dest
, first_def
);
6816 vect_finish_stmt_generation (vinfo
, NULL
, perm_stmt
, gsi
);
6817 /* Store the vector statement in NODE. */
6818 SLP_TREE_VEC_STMTS (node
).quick_push (perm_stmt
);
6821 /* Vectorize the SLP permutations in NODE as specified
6822 in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
6823 child number and lane number.
6824 Interleaving of two two-lane two-child SLP subtrees (not supported):
6825 [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
6826 A blend of two four-lane two-child SLP subtrees:
6827 [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
6828 Highpart of a four-lane one-child SLP subtree (not supported):
6829 [ { 0, 2 }, { 0, 3 } ]
6830 Where currently only a subset is supported by code generating below. */
6833 vectorizable_slp_permutation (vec_info
*vinfo
, gimple_stmt_iterator
*gsi
,
6834 slp_tree node
, stmt_vector_for_cost
*cost_vec
)
6836 tree vectype
= SLP_TREE_VECTYPE (node
);
6838 /* ??? We currently only support all same vector input and output types
6839 while the SLP IL should really do a concat + select and thus accept
6840 arbitrary mismatches. */
6843 poly_uint64 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6844 bool repeating_p
= multiple_p (nunits
, SLP_TREE_LANES (node
));
6845 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
6847 if (!vect_maybe_update_slp_op_vectype (child
, vectype
)
6848 || !types_compatible_p (SLP_TREE_VECTYPE (child
), vectype
))
6850 if (dump_enabled_p ())
6851 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6852 "Unsupported lane permutation\n");
6855 if (SLP_TREE_LANES (child
) != SLP_TREE_LANES (node
))
6856 repeating_p
= false;
6859 vec
<std::pair
<unsigned, unsigned> > &perm
= SLP_TREE_LANE_PERMUTATION (node
);
6860 gcc_assert (perm
.length () == SLP_TREE_LANES (node
));
6861 if (dump_enabled_p ())
6863 dump_printf_loc (MSG_NOTE
, vect_location
,
6864 "vectorizing permutation");
6865 for (unsigned i
= 0; i
< perm
.length (); ++i
)
6866 dump_printf (MSG_NOTE
, " op%u[%u]", perm
[i
].first
, perm
[i
].second
);
6868 dump_printf (MSG_NOTE
, " (repeat %d)\n", SLP_TREE_LANES (node
));
6869 dump_printf (MSG_NOTE
, "\n");
6872 /* REPEATING_P is true if every output vector is guaranteed to use the
6873 same permute vector. We can handle that case for both variable-length
6874 and constant-length vectors, but we only handle other cases for
6875 constant-length vectors.
6879 - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
6880 mask vector that we want to build.
6882 - NCOPIES to the number of copies of PERM that we need in order
6883 to build the necessary permute mask vectors.
6885 - NOUTPUTS_PER_MASK to the number of output vectors we want to create
6886 for each permute mask vector. This is only relevant when GSI is
6889 unsigned nelts_per_pattern
;
6891 unsigned noutputs_per_mask
;
6894 /* We need a single permute mask vector that has the form:
6896 { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
6898 In other words, the original n-element permute in PERM is
6899 "unrolled" to fill a full vector. The stepped vector encoding
6900 that we use for permutes requires 3n elements. */
6901 npatterns
= SLP_TREE_LANES (node
);
6902 nelts_per_pattern
= ncopies
= 3;
6903 noutputs_per_mask
= SLP_TREE_NUMBER_OF_VEC_STMTS (node
);
6907 /* Calculate every element of every permute mask vector explicitly,
6908 instead of relying on the pattern described above. */
6909 if (!nunits
.is_constant (&npatterns
))
6911 nelts_per_pattern
= ncopies
= 1;
6912 if (loop_vec_info linfo
= dyn_cast
<loop_vec_info
> (vinfo
))
6913 if (!LOOP_VINFO_VECT_FACTOR (linfo
).is_constant (&ncopies
))
6915 noutputs_per_mask
= 1;
6917 unsigned olanes
= ncopies
* SLP_TREE_LANES (node
);
6918 gcc_assert (repeating_p
|| multiple_p (olanes
, nunits
));
6920 /* Compute the { { SLP operand, vector index}, lane } permutation sequence
6921 from the { SLP operand, scalar lane } permutation as recorded in the
6922 SLP node as intermediate step. This part should already work
6923 with SLP children with arbitrary number of lanes. */
6924 auto_vec
<std::pair
<std::pair
<unsigned, unsigned>, unsigned> > vperm
;
6925 auto_vec
<unsigned> active_lane
;
6926 vperm
.create (olanes
);
6927 active_lane
.safe_grow_cleared (SLP_TREE_CHILDREN (node
).length (), true);
6928 for (unsigned i
= 0; i
< ncopies
; ++i
)
6930 for (unsigned pi
= 0; pi
< perm
.length (); ++pi
)
6932 std::pair
<unsigned, unsigned> p
= perm
[pi
];
6933 tree vtype
= SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (node
)[p
.first
]);
6935 vperm
.quick_push ({{p
.first
, 0}, p
.second
+ active_lane
[p
.first
]});
6938 /* We checked above that the vectors are constant-length. */
6939 unsigned vnunits
= TYPE_VECTOR_SUBPARTS (vtype
).to_constant ();
6940 unsigned vi
= (active_lane
[p
.first
] + p
.second
) / vnunits
;
6941 unsigned vl
= (active_lane
[p
.first
] + p
.second
) % vnunits
;
6942 vperm
.quick_push ({{p
.first
, vi
}, vl
});
6945 /* Advance to the next group. */
6946 for (unsigned j
= 0; j
< SLP_TREE_CHILDREN (node
).length (); ++j
)
6947 active_lane
[j
] += SLP_TREE_LANES (SLP_TREE_CHILDREN (node
)[j
]);
6950 if (dump_enabled_p ())
6952 dump_printf_loc (MSG_NOTE
, vect_location
, "as");
6953 for (unsigned i
= 0; i
< vperm
.length (); ++i
)
6957 ? multiple_p (i
, npatterns
)
6958 : multiple_p (i
, TYPE_VECTOR_SUBPARTS (vectype
))))
6959 dump_printf (MSG_NOTE
, ",");
6960 dump_printf (MSG_NOTE
, " vops%u[%u][%u]",
6961 vperm
[i
].first
.first
, vperm
[i
].first
.second
,
6964 dump_printf (MSG_NOTE
, "\n");
6967 /* We can only handle two-vector permutes, everything else should
6968 be lowered on the SLP level. The following is closely inspired
6969 by vect_transform_slp_perm_load and is supposed to eventually
6971 ??? As intermediate step do code-gen in the SLP tree representation
6973 std::pair
<unsigned, unsigned> first_vec
= std::make_pair (-1U, -1U);
6974 std::pair
<unsigned, unsigned> second_vec
= std::make_pair (-1U, -1U);
6975 unsigned int index
= 0;
6976 poly_uint64 mask_element
;
6977 vec_perm_builder mask
;
6978 mask
.new_vector (nunits
, npatterns
, nelts_per_pattern
);
6979 unsigned int count
= mask
.encoded_nelts ();
6980 mask
.quick_grow (count
);
6981 vec_perm_indices indices
;
6982 unsigned nperms
= 0;
6983 for (unsigned i
= 0; i
< vperm
.length (); ++i
)
6985 mask_element
= vperm
[i
].second
;
6986 if (first_vec
.first
== -1U
6987 || first_vec
== vperm
[i
].first
)
6988 first_vec
= vperm
[i
].first
;
6989 else if (second_vec
.first
== -1U
6990 || second_vec
== vperm
[i
].first
)
6992 second_vec
= vperm
[i
].first
;
6993 mask_element
+= nunits
;
6997 if (dump_enabled_p ())
6998 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6999 "permutation requires at "
7000 "least three vectors\n");
7005 mask
[index
++] = mask_element
;
7009 indices
.new_vector (mask
, second_vec
.first
== -1U ? 1 : 2, nunits
);
7010 bool identity_p
= indices
.series_p (0, 1, 0, 1);
7012 && !can_vec_perm_const_p (TYPE_MODE (vectype
), indices
))
7014 if (dump_enabled_p ())
7016 dump_printf_loc (MSG_MISSED_OPTIMIZATION
,
7018 "unsupported vect permute { ");
7019 for (i
= 0; i
< count
; ++i
)
7021 dump_dec (MSG_MISSED_OPTIMIZATION
, mask
[i
]);
7022 dump_printf (MSG_MISSED_OPTIMIZATION
, " ");
7024 dump_printf (MSG_MISSED_OPTIMIZATION
, "}\n");
7034 if (second_vec
.first
== -1U)
7035 second_vec
= first_vec
;
7038 first_node
= SLP_TREE_CHILDREN (node
)[first_vec
.first
],
7039 second_node
= SLP_TREE_CHILDREN (node
)[second_vec
.first
];
7041 tree mask_vec
= NULL_TREE
;
7043 mask_vec
= vect_gen_perm_mask_checked (vectype
, indices
);
7045 for (unsigned int vi
= 0; vi
< noutputs_per_mask
; ++vi
)
7048 = vect_get_slp_vect_def (first_node
,
7049 first_vec
.second
+ vi
);
7051 = vect_get_slp_vect_def (second_node
,
7052 second_vec
.second
+ vi
);
7053 vect_add_slp_permutation (vinfo
, gsi
, node
, first_def
,
7054 second_def
, mask_vec
);
7059 first_vec
= std::make_pair (-1U, -1U);
7060 second_vec
= std::make_pair (-1U, -1U);
7065 record_stmt_cost (cost_vec
, nperms
, vec_perm
, NULL
, vectype
, 0, vect_body
);
7070 /* Vectorize SLP NODE. */
7073 vect_schedule_slp_node (vec_info
*vinfo
,
7074 slp_tree node
, slp_instance instance
)
7076 gimple_stmt_iterator si
;
7080 /* For existing vectors there's nothing to do. */
7081 if (SLP_TREE_VEC_DEFS (node
).exists ())
7084 gcc_assert (SLP_TREE_VEC_STMTS (node
).is_empty ());
7086 /* Vectorize externals and constants. */
7087 if (SLP_TREE_DEF_TYPE (node
) == vect_constant_def
7088 || SLP_TREE_DEF_TYPE (node
) == vect_external_def
)
7090 /* ??? vectorizable_shift can end up using a scalar operand which is
7091 currently denoted as !SLP_TREE_VECTYPE. No need to vectorize the
7092 node in this case. */
7093 if (!SLP_TREE_VECTYPE (node
))
7096 vect_create_constant_vectors (vinfo
, node
);
7100 stmt_vec_info stmt_info
= SLP_TREE_REPRESENTATIVE (node
);
7102 gcc_assert (SLP_TREE_NUMBER_OF_VEC_STMTS (node
) != 0);
7103 SLP_TREE_VEC_STMTS (node
).create (SLP_TREE_NUMBER_OF_VEC_STMTS (node
));
7105 if (dump_enabled_p ())
7106 dump_printf_loc (MSG_NOTE
, vect_location
,
7107 "------>vectorizing SLP node starting from: %G",
7110 if (STMT_VINFO_DATA_REF (stmt_info
)
7111 && SLP_TREE_CODE (node
) != VEC_PERM_EXPR
)
7113 /* Vectorized loads go before the first scalar load to make it
7114 ready early, vectorized stores go before the last scalar
7115 stmt which is where all uses are ready. */
7116 stmt_vec_info last_stmt_info
= NULL
;
7117 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info
)))
7118 last_stmt_info
= vect_find_first_scalar_stmt_in_slp (node
);
7119 else /* DR_IS_WRITE */
7120 last_stmt_info
= vect_find_last_scalar_stmt_in_slp (node
);
7121 si
= gsi_for_stmt (last_stmt_info
->stmt
);
7123 else if ((STMT_VINFO_TYPE (stmt_info
) == cycle_phi_info_type
7124 || STMT_VINFO_TYPE (stmt_info
) == induc_vec_info_type
7125 || STMT_VINFO_TYPE (stmt_info
) == phi_info_type
)
7126 && SLP_TREE_CODE (node
) != VEC_PERM_EXPR
)
7128 /* For PHI node vectorization we do not use the insertion iterator. */
7133 /* Emit other stmts after the children vectorized defs which is
7134 earliest possible. */
7135 gimple
*last_stmt
= NULL
;
7136 bool seen_vector_def
= false;
7137 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
7138 if (SLP_TREE_DEF_TYPE (child
) == vect_internal_def
)
7140 /* For fold-left reductions we are retaining the scalar
7141 reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
7142 set so the representation isn't perfect. Resort to the
7143 last scalar def here. */
7144 if (SLP_TREE_VEC_STMTS (child
).is_empty ())
7146 gcc_assert (STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (child
))
7147 == cycle_phi_info_type
);
7148 gphi
*phi
= as_a
<gphi
*>
7149 (vect_find_last_scalar_stmt_in_slp (child
)->stmt
);
7151 || vect_stmt_dominates_stmt_p (last_stmt
, phi
))
7154 /* We are emitting all vectorized stmts in the same place and
7155 the last one is the last.
7156 ??? Unless we have a load permutation applied and that
7157 figures to re-use an earlier generated load. */
7160 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (child
), j
, vstmt
)
7162 || vect_stmt_dominates_stmt_p (last_stmt
, vstmt
))
7165 else if (!SLP_TREE_VECTYPE (child
))
7167 /* For externals we use unvectorized at all scalar defs. */
7170 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child
), j
, def
)
7171 if (TREE_CODE (def
) == SSA_NAME
7172 && !SSA_NAME_IS_DEFAULT_DEF (def
))
7174 gimple
*stmt
= SSA_NAME_DEF_STMT (def
);
7176 || vect_stmt_dominates_stmt_p (last_stmt
, stmt
))
7182 /* For externals we have to look at all defs since their
7183 insertion place is decided per vector. But beware
7184 of pre-existing vectors where we need to make sure
7185 we do not insert before the region boundary. */
7186 if (SLP_TREE_SCALAR_OPS (child
).is_empty ()
7187 && !vinfo
->lookup_def (SLP_TREE_VEC_DEFS (child
)[0]))
7188 seen_vector_def
= true;
7193 FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child
), j
, vdef
)
7194 if (TREE_CODE (vdef
) == SSA_NAME
7195 && !SSA_NAME_IS_DEFAULT_DEF (vdef
))
7197 gimple
*vstmt
= SSA_NAME_DEF_STMT (vdef
);
7199 || vect_stmt_dominates_stmt_p (last_stmt
, vstmt
))
7204 /* This can happen when all children are pre-existing vectors or
7207 last_stmt
= vect_find_first_scalar_stmt_in_slp (node
)->stmt
;
7210 gcc_assert (seen_vector_def
);
7211 si
= gsi_after_labels (as_a
<bb_vec_info
> (vinfo
)->bbs
[0]);
7213 else if (is_a
<bb_vec_info
> (vinfo
)
7214 && gimple_bb (last_stmt
) != gimple_bb (stmt_info
->stmt
)
7215 && gimple_could_trap_p (stmt_info
->stmt
))
7217 /* We've constrained possibly trapping operations to all come
7218 from the same basic-block, if vectorized defs would allow earlier
7219 scheduling still force vectorized stmts to the original block.
7220 This is only necessary for BB vectorization since for loop vect
7221 all operations are in a single BB and scalar stmt based
7222 placement doesn't play well with epilogue vectorization. */
7223 gcc_assert (dominated_by_p (CDI_DOMINATORS
,
7224 gimple_bb (stmt_info
->stmt
),
7225 gimple_bb (last_stmt
)));
7226 si
= gsi_after_labels (gimple_bb (stmt_info
->stmt
));
7228 else if (is_a
<gphi
*> (last_stmt
))
7229 si
= gsi_after_labels (gimple_bb (last_stmt
));
7232 si
= gsi_for_stmt (last_stmt
);
7237 bool done_p
= false;
7239 /* Handle purely internal nodes. */
7240 if (SLP_TREE_CODE (node
) == VEC_PERM_EXPR
)
7242 /* ??? the transform kind is stored to STMT_VINFO_TYPE which might
7243 be shared with different SLP nodes (but usually it's the same
7244 operation apart from the case the stmt is only there for denoting
7245 the actual scalar lane defs ...). So do not call vect_transform_stmt
7246 but open-code it here (partly). */
7247 bool done
= vectorizable_slp_permutation (vinfo
, &si
, node
, NULL
);
7252 vect_transform_stmt (vinfo
, stmt_info
, &si
, node
, instance
);
7255 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
7256 For loop vectorization this is done in vectorizable_call, but for SLP
7257 it needs to be deferred until end of vect_schedule_slp, because multiple
7258 SLP instances may refer to the same scalar stmt. */
7261 vect_remove_slp_scalar_calls (vec_info
*vinfo
,
7262 slp_tree node
, hash_set
<slp_tree
> &visited
)
7265 gimple_stmt_iterator gsi
;
7269 stmt_vec_info stmt_info
;
7271 if (!node
|| SLP_TREE_DEF_TYPE (node
) != vect_internal_def
)
7274 if (visited
.add (node
))
7277 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
7278 vect_remove_slp_scalar_calls (vinfo
, child
, visited
);
7280 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node
), i
, stmt_info
)
7282 gcall
*stmt
= dyn_cast
<gcall
*> (stmt_info
->stmt
);
7283 if (!stmt
|| gimple_bb (stmt
) == NULL
)
7285 if (is_pattern_stmt_p (stmt_info
)
7286 || !PURE_SLP_STMT (stmt_info
))
7288 lhs
= gimple_call_lhs (stmt
);
7289 new_stmt
= gimple_build_assign (lhs
, build_zero_cst (TREE_TYPE (lhs
)));
7290 gsi
= gsi_for_stmt (stmt
);
7291 vinfo
->replace_stmt (&gsi
, stmt_info
, new_stmt
);
7292 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt
)) = new_stmt
;
7297 vect_remove_slp_scalar_calls (vec_info
*vinfo
, slp_tree node
)
7299 hash_set
<slp_tree
> visited
;
7300 vect_remove_slp_scalar_calls (vinfo
, node
, visited
);
7303 /* Vectorize the instance root. */
7306 vectorize_slp_instance_root_stmt (slp_tree node
, slp_instance instance
)
7308 gassign
*rstmt
= NULL
;
7310 if (instance
->kind
== slp_inst_kind_ctor
)
7312 if (SLP_TREE_NUMBER_OF_VEC_STMTS (node
) == 1)
7317 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (node
), j
, child_stmt
)
7319 tree vect_lhs
= gimple_get_lhs (child_stmt
);
7320 tree root_lhs
= gimple_get_lhs (instance
->root_stmts
[0]->stmt
);
7321 if (!useless_type_conversion_p (TREE_TYPE (root_lhs
),
7322 TREE_TYPE (vect_lhs
)))
7323 vect_lhs
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (root_lhs
),
7325 rstmt
= gimple_build_assign (root_lhs
, vect_lhs
);
7329 else if (SLP_TREE_NUMBER_OF_VEC_STMTS (node
) > 1)
7331 int nelts
= SLP_TREE_NUMBER_OF_VEC_STMTS (node
);
7334 vec
<constructor_elt
, va_gc
> *v
;
7335 vec_alloc (v
, nelts
);
7337 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (node
), j
, child_stmt
)
7338 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
7339 gimple_get_lhs (child_stmt
));
7340 tree lhs
= gimple_get_lhs (instance
->root_stmts
[0]->stmt
);
7342 = TREE_TYPE (gimple_assign_rhs1 (instance
->root_stmts
[0]->stmt
));
7343 tree r_constructor
= build_constructor (rtype
, v
);
7344 rstmt
= gimple_build_assign (lhs
, r_constructor
);
7347 else if (instance
->kind
== slp_inst_kind_bb_reduc
)
7349 /* Largely inspired by reduction chain epilogue handling in
7350 vect_create_epilog_for_reduction. */
7351 vec
<tree
> vec_defs
= vNULL
;
7352 vect_get_slp_defs (node
, &vec_defs
);
7353 enum tree_code reduc_code
7354 = gimple_assign_rhs_code (instance
->root_stmts
[0]->stmt
);
7355 /* ??? We actually have to reflect signs somewhere. */
7356 if (reduc_code
== MINUS_EXPR
)
7357 reduc_code
= PLUS_EXPR
;
7358 gimple_seq epilogue
= NULL
;
7359 /* We may end up with more than one vector result, reduce them
7361 tree vec_def
= vec_defs
[0];
7362 for (unsigned i
= 1; i
< vec_defs
.length (); ++i
)
7363 vec_def
= gimple_build (&epilogue
, reduc_code
, TREE_TYPE (vec_def
),
7364 vec_def
, vec_defs
[i
]);
7365 vec_defs
.release ();
7366 /* ??? Support other schemes than direct internal fn. */
7367 internal_fn reduc_fn
;
7368 if (!reduction_fn_for_scalar_code (reduc_code
, &reduc_fn
)
7369 || reduc_fn
== IFN_LAST
)
7371 tree scalar_def
= gimple_build (&epilogue
, as_combined_fn (reduc_fn
),
7372 TREE_TYPE (TREE_TYPE (vec_def
)), vec_def
);
7374 gimple_stmt_iterator rgsi
= gsi_for_stmt (instance
->root_stmts
[0]->stmt
);
7375 gsi_insert_seq_before (&rgsi
, epilogue
, GSI_SAME_STMT
);
7376 gimple_assign_set_rhs_from_tree (&rgsi
, scalar_def
);
7377 update_stmt (gsi_stmt (rgsi
));
7385 gimple_stmt_iterator rgsi
= gsi_for_stmt (instance
->root_stmts
[0]->stmt
);
7386 gsi_replace (&rgsi
, rstmt
, true);
7396 /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs. */
7399 vect_schedule_scc (vec_info
*vinfo
, slp_tree node
, slp_instance instance
,
7400 hash_map
<slp_tree
, slp_scc_info
> &scc_info
,
7401 int &maxdfs
, vec
<slp_tree
> &stack
)
7404 slp_scc_info
*info
= &scc_info
.get_or_insert (node
, &existed_p
);
7405 gcc_assert (!existed_p
);
7407 info
->lowlink
= maxdfs
;
7411 if (SLP_TREE_DEF_TYPE (node
) != vect_internal_def
)
7413 info
->on_stack
= false;
7414 vect_schedule_slp_node (vinfo
, node
, instance
);
7418 info
->on_stack
= true;
7419 stack
.safe_push (node
);
7424 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node
), i
, child
)
7428 slp_scc_info
*child_info
= scc_info
.get (child
);
7431 vect_schedule_scc (vinfo
, child
, instance
, scc_info
, maxdfs
, stack
);
7432 /* Recursion might have re-allocated the node. */
7433 info
= scc_info
.get (node
);
7434 child_info
= scc_info
.get (child
);
7435 info
->lowlink
= MIN (info
->lowlink
, child_info
->lowlink
);
7437 else if (child_info
->on_stack
)
7438 info
->lowlink
= MIN (info
->lowlink
, child_info
->dfs
);
7440 if (info
->lowlink
!= info
->dfs
)
7443 auto_vec
<slp_tree
, 4> phis_to_fixup
;
7446 if (stack
.last () == node
)
7449 info
->on_stack
= false;
7450 vect_schedule_slp_node (vinfo
, node
, instance
);
7451 if (SLP_TREE_CODE (node
) != VEC_PERM_EXPR
7452 && is_a
<gphi
*> (SLP_TREE_REPRESENTATIVE (node
)->stmt
))
7453 phis_to_fixup
.quick_push (node
);
7458 int last_idx
= stack
.length () - 1;
7459 while (stack
[last_idx
] != node
)
7461 /* We can break the cycle at PHIs who have at least one child
7462 code generated. Then we could re-start the DFS walk until
7463 all nodes in the SCC are covered (we might have new entries
7464 for only back-reachable nodes). But it's simpler to just
7465 iterate and schedule those that are ready. */
7466 unsigned todo
= stack
.length () - last_idx
;
7469 for (int idx
= stack
.length () - 1; idx
>= last_idx
; --idx
)
7471 slp_tree entry
= stack
[idx
];
7474 bool phi
= (SLP_TREE_CODE (entry
) != VEC_PERM_EXPR
7475 && is_a
<gphi
*> (SLP_TREE_REPRESENTATIVE (entry
)->stmt
));
7477 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry
), i
, child
)
7484 else if (scc_info
.get (child
)->on_stack
)
7502 vect_schedule_slp_node (vinfo
, entry
, instance
);
7503 scc_info
.get (entry
)->on_stack
= false;
7507 phis_to_fixup
.safe_push (entry
);
7514 stack
.truncate (last_idx
);
7517 /* Now fixup the backedge def of the vectorized PHIs in this SCC. */
7519 FOR_EACH_VEC_ELT (phis_to_fixup
, i
, phi_node
)
7521 gphi
*phi
= as_a
<gphi
*> (SLP_TREE_REPRESENTATIVE (phi_node
)->stmt
);
7524 FOR_EACH_EDGE (e
, ei
, gimple_bb (phi
)->preds
)
7526 unsigned dest_idx
= e
->dest_idx
;
7527 child
= SLP_TREE_CHILDREN (phi_node
)[dest_idx
];
7528 if (!child
|| SLP_TREE_DEF_TYPE (child
) != vect_internal_def
)
7530 /* Simply fill all args. */
7531 for (unsigned i
= 0; i
< SLP_TREE_VEC_STMTS (phi_node
).length (); ++i
)
7532 add_phi_arg (as_a
<gphi
*> (SLP_TREE_VEC_STMTS (phi_node
)[i
]),
7533 vect_get_slp_vect_def (child
, i
),
7534 e
, gimple_phi_arg_location (phi
, dest_idx
));
7539 /* Generate vector code for SLP_INSTANCES in the loop/basic block. */
7542 vect_schedule_slp (vec_info
*vinfo
, const vec
<slp_instance
> &slp_instances
)
7544 slp_instance instance
;
7547 hash_map
<slp_tree
, slp_scc_info
> scc_info
;
7549 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
7551 slp_tree node
= SLP_INSTANCE_TREE (instance
);
7552 if (dump_enabled_p ())
7554 dump_printf_loc (MSG_NOTE
, vect_location
,
7555 "Vectorizing SLP tree:\n");
7557 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ())
7558 dump_printf_loc (MSG_NOTE
, vect_location
, "Root stmt: %G",
7559 SLP_INSTANCE_ROOT_STMTS (instance
)[0]->stmt
);
7560 vect_print_slp_graph (MSG_NOTE
, vect_location
,
7561 SLP_INSTANCE_TREE (instance
));
7563 /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
7564 have a PHI be the node breaking the cycle. */
7565 auto_vec
<slp_tree
> stack
;
7566 if (!scc_info
.get (node
))
7567 vect_schedule_scc (vinfo
, node
, instance
, scc_info
, maxdfs
, stack
);
7569 if (!SLP_INSTANCE_ROOT_STMTS (instance
).is_empty ())
7570 vectorize_slp_instance_root_stmt (node
, instance
);
7572 if (dump_enabled_p ())
7573 dump_printf_loc (MSG_NOTE
, vect_location
,
7574 "vectorizing stmts using SLP.\n");
7577 FOR_EACH_VEC_ELT (slp_instances
, i
, instance
)
7579 slp_tree root
= SLP_INSTANCE_TREE (instance
);
7580 stmt_vec_info store_info
;
7583 /* Remove scalar call stmts. Do not do this for basic-block
7584 vectorization as not all uses may be vectorized.
7585 ??? Why should this be necessary? DCE should be able to
7586 remove the stmts itself.
7587 ??? For BB vectorization we can as well remove scalar
7588 stmts starting from the SLP tree root if they have no
7590 if (is_a
<loop_vec_info
> (vinfo
))
7591 vect_remove_slp_scalar_calls (vinfo
, root
);
7593 /* Remove vectorized stores original scalar stmts. */
7594 for (j
= 0; SLP_TREE_SCALAR_STMTS (root
).iterate (j
, &store_info
); j
++)
7596 if (!STMT_VINFO_DATA_REF (store_info
)
7597 || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info
)))
7600 store_info
= vect_orig_stmt (store_info
);
7601 /* Free the attached stmt_vec_info and remove the stmt. */
7602 vinfo
->remove_stmt (store_info
);
7604 /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
7605 to not crash in vect_free_slp_tree later. */
7606 if (SLP_TREE_REPRESENTATIVE (root
) == store_info
)
7607 SLP_TREE_REPRESENTATIVE (root
) = NULL
;