Line data Source code
1 : /* SLP - Basic Block Vectorization
2 : Copyright (C) 2007-2026 Free Software Foundation, Inc.
3 : Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 : and Ira Rosen <irar@il.ibm.com>
5 :
6 : This file is part of GCC.
7 :
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
11 : version.
12 :
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
16 : for more details.
17 :
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/>. */
21 :
22 : #include "config.h"
23 : #define INCLUDE_ALGORITHM
24 : #include "system.h"
25 : #include "coretypes.h"
26 : #include "backend.h"
27 : #include "target.h"
28 : #include "rtl.h"
29 : #include "tree.h"
30 : #include "gimple.h"
31 : #include "tree-pass.h"
32 : #include "ssa.h"
33 : #include "optabs-tree.h"
34 : #include "insn-config.h"
35 : #include "recog.h" /* FIXME: for insn_data */
36 : #include "fold-const.h"
37 : #include "stor-layout.h"
38 : #include "gimple-iterator.h"
39 : #include "cfgloop.h"
40 : #include "tree-vectorizer.h"
41 : #include "langhooks.h"
42 : #include "gimple-walk.h"
43 : #include "dbgcnt.h"
44 : #include "tree-vector-builder.h"
45 : #include "vec-perm-indices.h"
46 : #include "gimple-fold.h"
47 : #include "internal-fn.h"
48 : #include "dump-context.h"
49 : #include "cfganal.h"
50 : #include "tree-eh.h"
51 : #include "tree-cfg.h"
52 : #include "alloc-pool.h"
53 : #include "sreal.h"
54 : #include "predict.h"
55 :
56 : #define REDUC_GROUP_FIRST_ELEMENT(S) \
57 : (gcc_checking_assert (!(S)->dr_aux.dr), (S)->first_element)
58 :
59 : static bool vect_transform_slp_perm_load_1 (vec_info *, slp_tree,
60 : load_permutation_t &,
61 : const vec<tree> &,
62 : gimple_stmt_iterator *,
63 : poly_uint64, bool, bool,
64 : vect_load_store_data *, unsigned *,
65 : unsigned * = nullptr,
66 : bool = false);
67 : static int vectorizable_slp_permutation_1 (vec_info *, gimple_stmt_iterator *,
68 : slp_tree, lane_permutation_t &,
69 : vec<slp_tree> &, bool,
70 : vect_permute_data *);
71 : static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
72 : static bool vect_slp_can_convert_to_external (const vec<stmt_vec_info> &);
73 :
74 : static object_allocator<_slp_tree> *slp_tree_pool;
75 : static slp_tree slp_first_node;
76 :
77 : void
78 1147195 : vect_slp_init (void)
79 : {
80 1147195 : slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
81 1147195 : }
82 :
83 : void
84 1147195 : vect_slp_fini (void)
85 : {
86 1856102 : while (slp_first_node)
87 708907 : delete slp_first_node;
88 2294390 : delete slp_tree_pool;
89 1147195 : slp_tree_pool = NULL;
90 1147195 : }
91 :
92 : void *
93 8428985 : _slp_tree::operator new (size_t n)
94 : {
95 8428985 : gcc_assert (n == sizeof (_slp_tree));
96 8428985 : return slp_tree_pool->allocate_raw ();
97 : }
98 :
99 : void
100 8428985 : _slp_tree::operator delete (void *node, size_t n)
101 : {
102 8428985 : gcc_assert (n == sizeof (_slp_tree));
103 8428985 : slp_tree_pool->remove_raw (node);
104 8428985 : }
105 :
106 :
107 : /* Initialize a SLP node. */
108 :
109 8428985 : _slp_tree::_slp_tree ()
110 : {
111 8428985 : this->prev_node = NULL;
112 8428985 : if (slp_first_node)
113 7370439 : slp_first_node->prev_node = this;
114 8428985 : this->next_node = slp_first_node;
115 8428985 : slp_first_node = this;
116 8428985 : SLP_TREE_SCALAR_STMTS (this) = vNULL;
117 8428985 : SLP_TREE_SCALAR_OPS (this) = vNULL;
118 8428985 : SLP_TREE_LIVE_LANES (this) = vNULL;
119 8428985 : SLP_TREE_VEC_DEFS (this) = vNULL;
120 8428985 : SLP_TREE_CHILDREN (this) = vNULL;
121 8428985 : SLP_TREE_LOAD_PERMUTATION (this) = vNULL;
122 8428985 : SLP_TREE_LANE_PERMUTATION (this) = vNULL;
123 8428985 : SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def;
124 8428985 : SLP_TREE_CODE (this) = ERROR_MARK;
125 8428985 : SLP_TREE_GS_SCALE (this) = 0;
126 8428985 : SLP_TREE_GS_BASE (this) = NULL_TREE;
127 8428985 : this->ldst_lanes = false;
128 8428985 : this->avoid_stlf_fail = false;
129 8428985 : SLP_TREE_VECTYPE (this) = NULL_TREE;
130 8428985 : SLP_TREE_REPRESENTATIVE (this) = NULL;
131 8428985 : this->cycle_info.id = -1;
132 8428985 : this->cycle_info.reduc_idx = -1;
133 8428985 : SLP_TREE_REF_COUNT (this) = 1;
134 8428985 : this->failed = NULL;
135 8428985 : this->lanes = 0;
136 8428985 : SLP_TREE_TYPE (this) = undef_vec_info_type;
137 8428985 : this->data = NULL;
138 8428985 : this->si = NULL;
139 8428985 : }
140 :
141 : /* Tear down a SLP node. */
142 :
143 8428985 : _slp_tree::~_slp_tree ()
144 : {
145 8428985 : if (this->prev_node)
146 5123135 : this->prev_node->next_node = this->next_node;
147 : else
148 3305850 : slp_first_node = this->next_node;
149 8428985 : if (this->next_node)
150 6373406 : this->next_node->prev_node = this->prev_node;
151 8428985 : SLP_TREE_CHILDREN (this).release ();
152 8428985 : SLP_TREE_SCALAR_STMTS (this).release ();
153 8428985 : SLP_TREE_SCALAR_OPS (this).release ();
154 8428985 : SLP_TREE_LIVE_LANES (this).release ();
155 8428985 : SLP_TREE_VEC_DEFS (this).release ();
156 8428985 : SLP_TREE_LOAD_PERMUTATION (this).release ();
157 8428985 : SLP_TREE_LANE_PERMUTATION (this).release ();
158 8428985 : if (this->failed)
159 2253639 : free (failed);
160 8428985 : if (this->data)
161 1275903 : delete this->data;
162 8428985 : }
163 :
164 : /* Push the single SSA definition in DEF to the vector of vector defs. */
165 :
166 : void
167 529906 : _slp_tree::push_vec_def (gimple *def)
168 : {
169 529906 : if (gphi *phi = dyn_cast <gphi *> (def))
170 59134 : vec_defs.quick_push (gimple_phi_result (phi));
171 : else
172 : {
173 470772 : def_operand_p defop = single_ssa_def_operand (def, SSA_OP_ALL_DEFS);
174 470772 : vec_defs.quick_push (get_def_from_ptr (defop));
175 : }
176 529906 : }
177 :
178 : /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
179 :
180 : void
181 15931448 : vect_free_slp_tree (slp_tree node)
182 : {
183 15931448 : int i;
184 15931448 : slp_tree child;
185 :
186 15931448 : if (--SLP_TREE_REF_COUNT (node) != 0)
187 15931448 : return;
188 :
189 11998009 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
190 4277931 : if (child)
191 3884192 : vect_free_slp_tree (child);
192 :
193 7720078 : delete node;
194 : }
195 :
196 : /* Return a location suitable for dumpings related to the SLP instance. */
197 :
198 : dump_user_location_t
199 3533685 : _slp_instance::location () const
200 : {
201 3533685 : if (!root_stmts.is_empty ())
202 429366 : return root_stmts[0]->stmt;
203 : else
204 3104319 : return SLP_TREE_SCALAR_STMTS (root)[0]->stmt;
205 : }
206 :
207 :
208 : /* Free the memory allocated for the SLP instance. */
209 :
210 : void
211 1651766 : vect_free_slp_instance (slp_instance instance)
212 : {
213 1651766 : vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
214 1651766 : SLP_INSTANCE_LOADS (instance).release ();
215 1651766 : SLP_INSTANCE_ROOT_STMTS (instance).release ();
216 1651766 : SLP_INSTANCE_REMAIN_DEFS (instance).release ();
217 1651766 : instance->subgraph_entries.release ();
218 1651766 : instance->cost_vec.release ();
219 1651766 : free (instance);
220 1651766 : }
221 :
222 :
223 : /* Create a SLP node with NOPS children with CODE, either VEC_PERM_EXPR
224 : for a permute node or else ERROR_MARK. */
225 :
226 : slp_tree
227 119918 : vect_create_new_slp_node (unsigned nops, tree_code code)
228 : {
229 119918 : gcc_assert (code == ERROR_MARK || code == VEC_PERM_EXPR);
230 119918 : slp_tree node = new _slp_tree;
231 119918 : SLP_TREE_SCALAR_STMTS (node) = vNULL;
232 119918 : SLP_TREE_CHILDREN (node).create (nops);
233 119918 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
234 119918 : SLP_TREE_CODE (node) = code;
235 119918 : return node;
236 : }
237 :
238 : /* Create a SLP node inplace at NODE for SCALAR_STMTS and NOPS children. */
239 :
240 : static slp_tree
241 4063459 : vect_create_new_slp_node (slp_tree node,
242 : vec<stmt_vec_info> scalar_stmts, unsigned nops)
243 : {
244 4063459 : SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
245 4063459 : SLP_TREE_CHILDREN (node).create (nops);
246 4063459 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
247 4063459 : SLP_TREE_REPRESENTATIVE (node) = scalar_stmts[0];
248 4063459 : SLP_TREE_LANES (node) = scalar_stmts.length ();
249 4063459 : return node;
250 : }
251 :
252 : /* Create an SLP node for SCALAR_STMTS and NOPS children. */
253 :
254 : static slp_tree
255 8434 : vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts, unsigned nops)
256 : {
257 8434 : return vect_create_new_slp_node (new _slp_tree, scalar_stmts, nops);
258 : }
259 :
260 : /* Create a vect_external_def SLP node inplace at NODE for scalar
261 : operands OPS. */
262 :
263 : static slp_tree
264 1976364 : vect_create_new_slp_node (slp_tree node, vec<tree> ops)
265 : {
266 1976364 : SLP_TREE_SCALAR_OPS (node) = ops;
267 1976364 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
268 0 : SLP_TREE_LANES (node) = ops.length ();
269 1976364 : return node;
270 : }
271 :
272 : /* Create a vect_external_def SLP node for scalar operands OPS. */
273 :
274 : static slp_tree
275 1976364 : vect_create_new_slp_node (vec<tree> ops)
276 : {
277 1976364 : return vect_create_new_slp_node (new _slp_tree, ops);
278 : }
279 :
280 :
281 : /* This structure is used in creation of an SLP tree. Each instance
282 : corresponds to the same operand in a group of scalar stmts in an SLP
283 : node. */
284 : typedef struct _slp_oprnd_info
285 : {
286 : /* Def-stmts for the operands. */
287 : vec<stmt_vec_info> def_stmts;
288 : /* Operands. */
289 : vec<tree> ops;
290 : /* Information about the first statement, its vector def-type, type, the
291 : operand itself in case it's constant, and an indication if it's a pattern
292 : stmt and gather/scatter info. */
293 : tree first_op_type;
294 : enum vect_def_type first_dt;
295 : bool any_pattern;
296 : bool first_gs_p;
297 : gather_scatter_info first_gs_info;
298 : } *slp_oprnd_info;
299 :
300 :
301 : /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
302 : operand. */
303 : static vec<slp_oprnd_info>
304 3714798 : vect_create_oprnd_info (int nops, int group_size)
305 : {
306 3714798 : int i;
307 3714798 : slp_oprnd_info oprnd_info;
308 3714798 : vec<slp_oprnd_info> oprnds_info;
309 :
310 3714798 : oprnds_info.create (nops);
311 13302781 : for (i = 0; i < nops; i++)
312 : {
313 5873185 : oprnd_info = XNEW (struct _slp_oprnd_info);
314 5873185 : oprnd_info->def_stmts.create (group_size);
315 5873185 : oprnd_info->ops.create (group_size);
316 5873185 : oprnd_info->first_dt = vect_uninitialized_def;
317 5873185 : oprnd_info->first_op_type = NULL_TREE;
318 5873185 : oprnd_info->any_pattern = false;
319 5873185 : oprnd_info->first_gs_p = false;
320 5873185 : oprnds_info.quick_push (oprnd_info);
321 : }
322 :
323 3714798 : return oprnds_info;
324 : }
325 :
326 :
327 : /* Free operands info. */
328 :
329 : static void
330 3714798 : vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
331 : {
332 3714798 : int i;
333 3714798 : slp_oprnd_info oprnd_info;
334 :
335 9587983 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
336 : {
337 5873185 : oprnd_info->def_stmts.release ();
338 5873185 : oprnd_info->ops.release ();
339 5873185 : XDELETE (oprnd_info);
340 : }
341 :
342 3714798 : oprnds_info.release ();
343 3714798 : }
344 :
345 : /* Return the execution frequency of NODE (so that a higher value indicates
346 : a "more important" node when optimizing for speed). */
347 :
348 : static sreal
349 3768004 : vect_slp_node_weight (vec_info *vinfo, slp_tree node)
350 : {
351 3768004 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
352 3768004 : basic_block bb;
353 : /* ??? This is imprecise, VEC_PERM nodes do not have a representative
354 : but are laid out close to their children. */
355 3768004 : if (!stmt_info)
356 188943 : bb = vinfo->bbs[0];
357 : else
358 4058601 : bb = gimple_bb (vect_orig_stmt (stmt_info)->stmt);
359 3768004 : return bb->count.to_sreal_scale (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
360 : }
361 :
362 : /* Return true if STMTS contains a pattern statement. */
363 :
364 : static bool
365 24733 : vect_contains_pattern_stmt_p (vec<stmt_vec_info> stmts)
366 : {
367 24733 : stmt_vec_info stmt_info;
368 24733 : unsigned int i;
369 83249 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
370 60131 : if (stmt_info && is_pattern_stmt_p (stmt_info))
371 : return true;
372 : return false;
373 : }
374 :
375 : /* Return true when all lanes in the external or constant NODE have
376 : the same value. */
377 :
378 : static bool
379 649122 : vect_slp_tree_uniform_p (slp_tree node)
380 : {
381 649122 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_constant_def
382 : || SLP_TREE_DEF_TYPE (node) == vect_external_def);
383 :
384 : /* Pre-existing vectors. */
385 649122 : if (SLP_TREE_SCALAR_OPS (node).is_empty ())
386 : return false;
387 :
388 : unsigned i;
389 : tree op, first = NULL_TREE;
390 1484839 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
391 1326358 : if (!first)
392 : first = op;
393 677236 : else if (!operand_equal_p (first, op, 0))
394 : return false;
395 :
396 : return true;
397 : }
398 :
399 : /* Find the place of the data-ref in STMT_INFO in the interleaving chain
400 : that starts from FIRST_STMT_INFO. Return -1 if the data-ref is not a part
401 : of the chain. */
402 :
403 : int
404 792332 : vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
405 : stmt_vec_info first_stmt_info)
406 : {
407 792332 : stmt_vec_info next_stmt_info = first_stmt_info;
408 792332 : int result = 0;
409 :
410 792332 : if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
411 : return -1;
412 :
413 2213613 : do
414 : {
415 2213613 : if (next_stmt_info == stmt_info)
416 : return result;
417 1421281 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
418 1421281 : if (next_stmt_info)
419 1421281 : result += DR_GROUP_GAP (next_stmt_info);
420 : }
421 1421281 : while (next_stmt_info);
422 :
423 : return -1;
424 : }
425 :
426 : /* Check whether it is possible to load COUNT elements of type ELT_TYPE
427 : using the method implemented by duplicate_and_interleave. Return true
428 : if so, returning the number of intermediate vectors in *NVECTORS_OUT
429 : (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
430 : (if nonnull). */
431 :
432 : bool
433 0 : can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
434 : tree elt_type, unsigned int *nvectors_out,
435 : tree *vector_type_out,
436 : tree *permutes)
437 : {
438 0 : tree base_vector_type = get_vectype_for_scalar_type (vinfo, elt_type, count);
439 0 : if (!base_vector_type || !VECTOR_MODE_P (TYPE_MODE (base_vector_type)))
440 : return false;
441 :
442 0 : machine_mode base_vector_mode = TYPE_MODE (base_vector_type);
443 0 : poly_int64 elt_bytes = count * GET_MODE_UNIT_SIZE (base_vector_mode);
444 0 : unsigned int nvectors = 1;
445 0 : for (;;)
446 : {
447 0 : scalar_int_mode int_mode;
448 0 : poly_int64 elt_bits = elt_bytes * BITS_PER_UNIT;
449 0 : if (int_mode_for_size (elt_bits, 1).exists (&int_mode))
450 : {
451 : /* Get the natural vector type for this SLP group size. */
452 0 : tree int_type = build_nonstandard_integer_type
453 0 : (GET_MODE_BITSIZE (int_mode), 1);
454 0 : tree vector_type
455 0 : = get_vectype_for_scalar_type (vinfo, int_type, count);
456 0 : poly_int64 half_nelts;
457 0 : if (vector_type
458 0 : && VECTOR_MODE_P (TYPE_MODE (vector_type))
459 0 : && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
460 : GET_MODE_SIZE (base_vector_mode))
461 0 : && multiple_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)),
462 : 2, &half_nelts))
463 : {
464 : /* Try fusing consecutive sequences of COUNT / NVECTORS elements
465 : together into elements of type INT_TYPE and using the result
466 : to build NVECTORS vectors. */
467 0 : poly_uint64 nelts = GET_MODE_NUNITS (TYPE_MODE (vector_type));
468 0 : vec_perm_builder sel1 (nelts, 2, 3);
469 0 : vec_perm_builder sel2 (nelts, 2, 3);
470 :
471 0 : for (unsigned int i = 0; i < 3; ++i)
472 : {
473 0 : sel1.quick_push (i);
474 0 : sel1.quick_push (i + nelts);
475 0 : sel2.quick_push (half_nelts + i);
476 0 : sel2.quick_push (half_nelts + i + nelts);
477 : }
478 0 : vec_perm_indices indices1 (sel1, 2, nelts);
479 0 : vec_perm_indices indices2 (sel2, 2, nelts);
480 0 : machine_mode vmode = TYPE_MODE (vector_type);
481 0 : if (can_vec_perm_const_p (vmode, vmode, indices1)
482 0 : && can_vec_perm_const_p (vmode, vmode, indices2))
483 : {
484 0 : if (nvectors_out)
485 0 : *nvectors_out = nvectors;
486 0 : if (vector_type_out)
487 0 : *vector_type_out = vector_type;
488 0 : if (permutes)
489 : {
490 0 : permutes[0] = vect_gen_perm_mask_checked (vector_type,
491 : indices1);
492 0 : permutes[1] = vect_gen_perm_mask_checked (vector_type,
493 : indices2);
494 : }
495 0 : return true;
496 : }
497 0 : }
498 : }
499 0 : if (!multiple_p (elt_bytes, 2, &elt_bytes))
500 : return false;
501 0 : nvectors *= 2;
502 : /* We need to be able to fuse COUNT / NVECTORS elements together. */
503 0 : if (!multiple_p (count, nvectors))
504 : return false;
505 : }
506 : }
507 :
508 : /* Return true if DTA and DTB match. */
509 :
510 : static bool
511 17578798 : vect_def_types_match (enum vect_def_type dta, enum vect_def_type dtb)
512 : {
513 17578798 : return (dta == dtb
514 415435 : || ((dta == vect_external_def || dta == vect_constant_def)
515 267525 : && (dtb == vect_external_def || dtb == vect_constant_def)));
516 : }
517 :
518 : #define GATHER_SCATTER_OFFSET (-3)
519 :
520 : /* For most SLP statements, there is a one-to-one mapping between
521 : gimple arguments and child nodes. If that is not true for STMT,
522 : return an array that contains:
523 :
524 : - the number of child nodes, followed by
525 : - for each child node, the index of the argument associated with that node.
526 : The special index -1 is the first operand of an embedded comparison and
527 : the special index -2 is the second operand of an embedded comparison.
528 : The special index -3 is the offset of a gather as analyzed by
529 : vect_check_gather_scatter.
530 :
531 : SWAP is as for vect_get_and_check_slp_defs. */
532 :
533 : static const int *
534 26043844 : vect_get_operand_map (const gimple *stmt, bool gather_scatter_p,
535 : unsigned char swap)
536 : {
537 26043844 : static const int no_arg_map[] = { 0 };
538 26043844 : static const int arg0_map[] = { 1, 0 };
539 26043844 : static const int arg2_map[] = { 1, 2 };
540 26043844 : static const int arg2_arg3_map[] = { 2, 2, 3 };
541 26043844 : static const int arg2_arg4_map[] = { 2, 2, 4 };
542 26043844 : static const int arg2_arg5_arg6_map[] = { 3, 2, 5, 6 };
543 26043844 : static const int arg2_arg4_arg5_map[] = { 3, 2, 4, 5 };
544 26043844 : static const int arg3_arg2_map[] = { 2, 3, 2 };
545 26043844 : static const int op00_map[] = { 1, -1 };
546 26043844 : static const int op1_op0_map[] = { 2, 1, 0 };
547 26043844 : static const int off_map[] = { 1, GATHER_SCATTER_OFFSET };
548 26043844 : static const int off_op0_map[] = { 2, GATHER_SCATTER_OFFSET, 0 };
549 26043844 : static const int off_arg2_arg3_map[] = { 3, GATHER_SCATTER_OFFSET, 2, 3 };
550 26043844 : static const int off_arg3_arg2_map[] = { 3, GATHER_SCATTER_OFFSET, 3, 2 };
551 26043844 : static const int mask_call_maps[6][7] = {
552 : { 1, 1, },
553 : { 2, 1, 2, },
554 : { 3, 1, 2, 3, },
555 : { 4, 1, 2, 3, 4, },
556 : { 5, 1, 2, 3, 4, 5, },
557 : { 6, 1, 2, 3, 4, 5, 6 },
558 : };
559 :
560 26043844 : gcc_checking_assert (!swap
561 : || !is_gimple_assign (stmt)
562 : || TREE_CODE_CLASS
563 : (gimple_assign_rhs_code (stmt)) == tcc_comparison
564 : || commutative_tree_code
565 : (gimple_assign_rhs_code (stmt)));
566 :
567 26043844 : if (auto assign = dyn_cast<const gassign *> (stmt))
568 : {
569 24453828 : tree_code code = gimple_assign_rhs_code (assign);
570 24453828 : if (code == COND_EXPR
571 24453828 : && COMPARISON_CLASS_P (gimple_assign_rhs1 (assign)))
572 0 : gcc_unreachable ();
573 24453828 : else if ((TREE_CODE_CLASS (code) == tcc_comparison
574 22907611 : || commutative_tree_code (code))
575 33527978 : && swap)
576 : return op1_op0_map;
577 24411389 : else if (code == VIEW_CONVERT_EXPR)
578 : return op00_map;
579 24401719 : else if (gather_scatter_p)
580 45813 : return (TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
581 45813 : ? off_op0_map : off_map);
582 : }
583 1590016 : else if (auto call = dyn_cast<const gcall *> (stmt))
584 : {
585 179913 : if (gimple_call_internal_p (call))
586 97776 : switch (gimple_call_internal_fn (call))
587 : {
588 14490 : case IFN_MASK_LOAD:
589 14490 : return gather_scatter_p ? off_arg2_arg3_map : arg2_arg3_map;
590 :
591 0 : case IFN_GATHER_LOAD:
592 0 : return arg2_map;
593 :
594 0 : case IFN_MASK_GATHER_LOAD:
595 0 : case IFN_MASK_LEN_GATHER_LOAD:
596 0 : return arg2_arg5_arg6_map;
597 :
598 0 : case IFN_SCATTER_STORE:
599 0 : return arg2_arg4_map;
600 :
601 0 : case IFN_MASK_SCATTER_STORE:
602 0 : case IFN_MASK_LEN_SCATTER_STORE:
603 0 : return arg2_arg4_arg5_map;
604 :
605 9696 : case IFN_MASK_STORE:
606 9696 : return gather_scatter_p ? off_arg3_arg2_map : arg3_arg2_map;
607 :
608 1090 : case IFN_MASK_CALL:
609 1090 : {
610 1090 : unsigned nargs = gimple_call_num_args (call);
611 1090 : if (nargs >= 2 && nargs <= 7)
612 1090 : return mask_call_maps[nargs-2];
613 : else
614 : return nullptr;
615 : }
616 :
617 719 : case IFN_CLZ:
618 719 : case IFN_CTZ:
619 719 : return arg0_map;
620 :
621 8108 : case IFN_GOMP_SIMD_LANE:
622 8108 : return no_arg_map;
623 :
624 : default:
625 : break;
626 : }
627 : }
628 : return nullptr;
629 : }
630 :
631 : static const int *
632 26023032 : vect_get_operand_map (const stmt_vec_info stmt, unsigned char swap = 0)
633 : {
634 0 : return vect_get_operand_map (stmt->stmt, STMT_VINFO_GATHER_SCATTER_P (stmt),
635 0 : swap);
636 : }
637 :
638 : /* Return the SLP node child index for operand OP of STMT. */
639 :
640 : int
641 1398409 : vect_slp_child_index_for_operand (const stmt_vec_info stmt, int op)
642 : {
643 1398409 : const int *opmap = vect_get_operand_map (stmt);
644 1398409 : if (!opmap)
645 : return op;
646 20755 : for (int i = 1; i < 1 + opmap[0]; ++i)
647 20755 : if (opmap[i] == op)
648 11865 : return i - 1;
649 0 : gcc_unreachable ();
650 : }
651 :
652 : /* Helper class for mapping of GIMPLE operands to SLP children. */
653 : /* ??? Add vect_slp_child_index_for_operand here and amend opmaps
654 : with the full reverse mapping and indicating the position of the
655 : first commutative operand index, eliding the swap_p argument from
656 : vect_get_operand_map. Adjust all consumers. */
657 :
658 : struct slp_oprnds {
659 : slp_oprnds (stmt_vec_info);
660 : tree get_op_for_slp_child (stmt_vec_info, unsigned);
661 : const int *opmap;
662 : const unsigned int num_slp_children;
663 : };
664 :
665 4639585 : slp_oprnds::slp_oprnds (stmt_vec_info stmt_info)
666 4639585 : : opmap (vect_get_operand_map (stmt_info)),
667 4639585 : num_slp_children (opmap ? opmap[0] : gimple_num_args (stmt_info->stmt))
668 : {
669 4639585 : }
670 :
671 : /* For SLP child number N get the corresponding tree operand from GIMPLE
672 : statement described by STMT_INFO. */
673 :
674 : tree
675 5190783 : slp_oprnds::get_op_for_slp_child (stmt_vec_info stmt_info, unsigned n)
676 : {
677 5190783 : gcc_assert (n < num_slp_children);
678 5190783 : int opno = opmap ? opmap[n + 1] : (int) n;
679 5190783 : if (opno == GATHER_SCATTER_OFFSET)
680 0 : gcc_unreachable (); // TODO
681 5190783 : else if (opno < 0)
682 2600 : return TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
683 : else
684 5188183 : return gimple_arg (stmt_info->stmt, opno);
685 : }
686 :
687 : /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
688 : they are of a valid type and that they match the defs of the first stmt of
689 : the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
690 : by swapping operands of STMTS[STMT_NUM] when possible. Non-zero SWAP
691 : indicates swap is required for cond_expr stmts. Specifically, SWAP
692 : is 1 if STMT is cond and operands of comparison need to be swapped;
693 : SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
694 :
695 : If there was a fatal error return -1; if the error could be corrected by
696 : swapping operands of father node of this one, return 1; if everything is
697 : ok return 0. */
698 : static int
699 13680009 : vect_get_and_check_slp_defs (vec_info *vinfo, tree vectype, unsigned char swap,
700 : bool *skip_args,
701 : vec<stmt_vec_info> stmts, unsigned stmt_num,
702 : vec<slp_oprnd_info> *oprnds_info,
703 : bool soft_fail)
704 : {
705 13680009 : stmt_vec_info stmt_info = stmts[stmt_num];
706 13680009 : tree oprnd;
707 13680009 : unsigned int i, number_of_oprnds;
708 13680009 : enum vect_def_type dt = vect_uninitialized_def;
709 13680009 : slp_oprnd_info oprnd_info;
710 13680009 : gather_scatter_info gs_info;
711 13680009 : unsigned int gs_op = -1u;
712 13680009 : unsigned int commutative_op = -1U;
713 13680009 : bool first = stmt_num == 0;
714 :
715 13680009 : if (!stmt_info)
716 : {
717 0 : for (auto oi : *oprnds_info)
718 : {
719 0 : oi->def_stmts.quick_push (NULL);
720 0 : oi->ops.quick_push (NULL_TREE);
721 : }
722 : return 0;
723 : }
724 :
725 13680009 : if (!is_a<gcall *> (stmt_info->stmt)
726 : && !is_a<gassign *> (stmt_info->stmt)
727 : && !is_a<gphi *> (stmt_info->stmt))
728 : return -1;
729 :
730 13680009 : number_of_oprnds = gimple_num_args (stmt_info->stmt);
731 13680009 : const int *map = vect_get_operand_map (stmt_info, swap);
732 13680009 : if (map)
733 80387 : number_of_oprnds = *map++;
734 13680009 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
735 : {
736 58995 : if (gimple_call_internal_p (stmt))
737 : {
738 35730 : internal_fn ifn = gimple_call_internal_fn (stmt);
739 35730 : commutative_op = first_commutative_argument (ifn);
740 35730 : if (internal_gather_scatter_fn_p (ifn))
741 : {
742 0 : vect_describe_gather_scatter_call
743 0 : (stmt_info,
744 0 : first ? &(*oprnds_info)[0]->first_gs_info : &gs_info);
745 0 : if (first)
746 0 : (*oprnds_info)[0]->first_gs_p = true;
747 : gs_op = 0;
748 : }
749 : }
750 : }
751 13621014 : else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
752 : {
753 16352560 : if (commutative_tree_code (gimple_assign_rhs_code (stmt)))
754 8474064 : commutative_op = 0;
755 : }
756 :
757 13680009 : bool swapped = (swap != 0);
758 13680009 : bool backedge = false;
759 13680009 : enum vect_def_type *dts = XALLOCAVEC (enum vect_def_type, number_of_oprnds);
760 37337108 : for (i = 0; i < number_of_oprnds; i++)
761 : {
762 23658808 : oprnd_info = (*oprnds_info)[i];
763 23658808 : int opno = map ? map[i] : int (i);
764 23658808 : if (opno == GATHER_SCATTER_OFFSET)
765 : {
766 23363 : gcc_assert (STMT_VINFO_GATHER_SCATTER_P (stmt_info));
767 23363 : if (!is_a <loop_vec_info> (vinfo)
768 23363 : || !vect_check_gather_scatter (stmt_info, vectype,
769 : as_a <loop_vec_info> (vinfo),
770 : first ? &oprnd_info->first_gs_info
771 : : &gs_info))
772 1709 : return -1;
773 :
774 23363 : if (first)
775 : {
776 23037 : oprnd_info->first_gs_p = true;
777 23037 : oprnd = oprnd_info->first_gs_info.offset;
778 : }
779 : else
780 : {
781 326 : gs_op = i;
782 326 : oprnd = gs_info.offset;
783 : }
784 : }
785 23635445 : else if (opno < 0)
786 3391 : oprnd = TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
787 : else
788 : {
789 23632054 : oprnd = gimple_arg (stmt_info->stmt, opno);
790 23632054 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
791 : {
792 1310829 : edge e = gimple_phi_arg_edge (stmt, opno);
793 2621658 : backedge = (is_a <bb_vec_info> (vinfo)
794 1996069 : ? e->flags & EDGE_DFS_BACK
795 685240 : : dominated_by_p (CDI_DOMINATORS, e->src,
796 685240 : gimple_bb (stmt_info->stmt)));
797 : }
798 : }
799 :
800 23658808 : stmt_vec_info def_stmt_info;
801 23658808 : if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info))
802 : {
803 1262 : if (dump_enabled_p ())
804 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
805 : "Build SLP failed: can't analyze def for %T\n",
806 : oprnd);
807 :
808 : return -1;
809 : }
810 :
811 23657546 : if (skip_args[i])
812 : {
813 557536 : oprnd_info->def_stmts.quick_push (NULL);
814 557536 : oprnd_info->ops.quick_push (NULL_TREE);
815 557536 : oprnd_info->first_dt = vect_uninitialized_def;
816 557536 : continue;
817 : }
818 :
819 23100010 : oprnd_info->def_stmts.quick_push (def_stmt_info);
820 23100010 : oprnd_info->ops.quick_push (oprnd);
821 :
822 23100010 : if (def_stmt_info
823 23100010 : && is_pattern_stmt_p (def_stmt_info))
824 : {
825 459292 : if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info))
826 : != def_stmt_info)
827 318025 : oprnd_info->any_pattern = true;
828 : else
829 : /* If we promote this to external use the original stmt def. */
830 141267 : oprnd_info->ops.last ()
831 282534 : = gimple_get_lhs (vect_orig_stmt (def_stmt_info)->stmt);
832 : }
833 :
834 : /* If there's a extern def on a backedge make sure we can
835 : code-generate at the region start.
836 : ??? This is another case that could be fixed by adjusting
837 : how we split the function but at the moment we'd have conflicting
838 : goals there. */
839 23100010 : if (backedge
840 169622 : && dts[i] == vect_external_def
841 482 : && is_a <bb_vec_info> (vinfo)
842 482 : && TREE_CODE (oprnd) == SSA_NAME
843 447 : && !SSA_NAME_IS_DEFAULT_DEF (oprnd)
844 23100457 : && !dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
845 447 : gimple_bb (SSA_NAME_DEF_STMT (oprnd))))
846 : {
847 447 : if (dump_enabled_p ())
848 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
849 : "Build SLP failed: extern def %T only defined "
850 : "on backedge\n", oprnd);
851 : return -1;
852 : }
853 :
854 23099563 : if (first)
855 : {
856 5341380 : tree type = TREE_TYPE (oprnd);
857 5341380 : dt = dts[i];
858 :
859 : /* For the swapping logic below force vect_reduction_def
860 : for the reduction op in a SLP reduction group. */
861 5341380 : if (!STMT_VINFO_DATA_REF (stmt_info)
862 4025475 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
863 1870 : && (int)i == STMT_VINFO_REDUC_IDX (stmt_info)
864 5342272 : && def_stmt_info)
865 892 : dts[i] = dt = vect_reduction_def;
866 :
867 : /* Check the types of the definition. */
868 5341380 : switch (dt)
869 : {
870 5341380 : case vect_external_def:
871 5341380 : case vect_constant_def:
872 5341380 : case vect_internal_def:
873 5341380 : case vect_reduction_def:
874 5341380 : case vect_double_reduction_def:
875 5341380 : case vect_induction_def:
876 5341380 : case vect_nested_cycle:
877 5341380 : case vect_first_order_recurrence:
878 5341380 : break;
879 :
880 0 : default:
881 : /* FORNOW: Not supported. */
882 0 : if (dump_enabled_p ())
883 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
884 : "Build SLP failed: illegal type of def %T\n",
885 : oprnd);
886 : return -1;
887 : }
888 :
889 5341380 : oprnd_info->first_dt = dt;
890 5341380 : oprnd_info->first_op_type = type;
891 : }
892 : }
893 13678300 : if (first)
894 : return 0;
895 :
896 : /* Now match the operand definition types to that of the first stmt. */
897 27345923 : for (i = 0; i < number_of_oprnds;)
898 : {
899 17724393 : if (skip_args[i])
900 : {
901 30630 : ++i;
902 30630 : continue;
903 : }
904 :
905 17693763 : oprnd_info = (*oprnds_info)[i];
906 17693763 : dt = dts[i];
907 17693763 : stmt_vec_info def_stmt_info = oprnd_info->def_stmts[stmt_num];
908 17693763 : oprnd = oprnd_info->ops[stmt_num];
909 17693763 : tree type = TREE_TYPE (oprnd);
910 :
911 17693763 : if (!types_compatible_p (oprnd_info->first_op_type, type))
912 : {
913 122250 : if (dump_enabled_p ())
914 93 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
915 : "Build SLP failed: different operand types\n");
916 : return 1;
917 : }
918 :
919 17571513 : if ((gs_op == i) != oprnd_info->first_gs_p)
920 : {
921 0 : if (dump_enabled_p ())
922 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
923 : "Build SLP failed: mixed gather and non-gather\n");
924 : return 1;
925 : }
926 17571513 : else if (gs_op == i)
927 : {
928 294 : if (!operand_equal_p (oprnd_info->first_gs_info.base,
929 294 : gs_info.base))
930 : {
931 52 : if (dump_enabled_p ())
932 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
933 : "Build SLP failed: different gather base\n");
934 : return 1;
935 : }
936 242 : if (oprnd_info->first_gs_info.scale != gs_info.scale)
937 : {
938 8 : if (dump_enabled_p ())
939 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
940 : "Build SLP failed: different gather scale\n");
941 : return 1;
942 : }
943 : }
944 :
945 : /* Not first stmt of the group, check that the def-stmt/s match
946 : the def-stmt/s of the first stmt. Allow different definition
947 : types for reduction chains: the first stmt must be a
948 : vect_reduction_def (a phi node), and the rest
949 : end in the reduction chain. */
950 17571453 : if ((!vect_def_types_match (oprnd_info->first_dt, dt)
951 333901 : && !(oprnd_info->first_dt == vect_reduction_def
952 3359 : && !STMT_VINFO_DATA_REF (stmt_info)
953 3359 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
954 3333 : && def_stmt_info
955 3331 : && !STMT_VINFO_DATA_REF (def_stmt_info)
956 3331 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
957 : == REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
958 17240883 : || (!STMT_VINFO_DATA_REF (stmt_info)
959 15459855 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
960 7101 : && ((!def_stmt_info
961 6865 : || STMT_VINFO_DATA_REF (def_stmt_info)
962 11940 : || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
963 : != REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
964 7101 : != (oprnd_info->first_dt != vect_reduction_def))))
965 : {
966 : /* Try swapping operands if we got a mismatch. For BB
967 : vectorization only in case it will clearly improve things. */
968 333261 : if (i == commutative_op && !swapped
969 330570 : && (!is_a <bb_vec_info> (vinfo)
970 5656 : || (!vect_def_types_match ((*oprnds_info)[i+1]->first_dt,
971 5656 : dts[i+1])
972 1456 : && (vect_def_types_match (oprnd_info->first_dt, dts[i+1])
973 : || vect_def_types_match
974 233 : ((*oprnds_info)[i+1]->first_dt, dts[i])))))
975 : {
976 2691 : if (dump_enabled_p ())
977 200 : dump_printf_loc (MSG_NOTE, vect_location,
978 : "trying swapped operands\n");
979 2691 : std::swap (dts[i], dts[i+1]);
980 2691 : std::swap ((*oprnds_info)[i]->def_stmts[stmt_num],
981 2691 : (*oprnds_info)[i+1]->def_stmts[stmt_num]);
982 2691 : std::swap ((*oprnds_info)[i]->ops[stmt_num],
983 2691 : (*oprnds_info)[i+1]->ops[stmt_num]);
984 : /* After swapping some operands we lost track whether an
985 : operand has any pattern defs so be conservative here. */
986 2691 : if ((*oprnds_info)[i]->any_pattern
987 2691 : || (*oprnds_info)[i+1]->any_pattern)
988 36 : (*oprnds_info)[i]->any_pattern
989 18 : = (*oprnds_info)[i+1]->any_pattern = true;
990 2691 : swapped = true;
991 2691 : continue;
992 : }
993 :
994 327879 : if (is_a <bb_vec_info> (vinfo)
995 310499 : && !oprnd_info->any_pattern
996 310225 : && number_of_oprnds > 1
997 638104 : && !soft_fail)
998 : {
999 : /* Now for commutative ops we should see whether we can
1000 : make the other operand matching. */
1001 103868 : if (dump_enabled_p ())
1002 290 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1003 : "treating operand as external\n");
1004 : oprnd_info->first_dt = dt = vect_external_def;
1005 : }
1006 : else
1007 : {
1008 224011 : if (dump_enabled_p ())
1009 429 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1010 : "Build SLP failed: different types\n");
1011 : return 1;
1012 : }
1013 : }
1014 :
1015 : /* Make sure to demote the overall operand to external. */
1016 17240883 : if (dt == vect_external_def)
1017 427106 : oprnd_info->first_dt = vect_external_def;
1018 : /* For a SLP reduction chain we want to duplicate the reduction to
1019 : each of the chain members. That gets us a sane SLP graph (still
1020 : the stmts are not 100% correct wrt the initial values). */
1021 16917645 : else if ((dt == vect_internal_def
1022 16917645 : || dt == vect_reduction_def)
1023 15629946 : && oprnd_info->first_dt == vect_reduction_def
1024 49314 : && !STMT_VINFO_DATA_REF (stmt_info)
1025 49314 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
1026 3331 : && !STMT_VINFO_DATA_REF (def_stmt_info)
1027 16920976 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
1028 : == REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
1029 : {
1030 3331 : oprnd_info->def_stmts[stmt_num] = oprnd_info->def_stmts[0];
1031 3331 : oprnd_info->ops[stmt_num] = oprnd_info->ops[0];
1032 : }
1033 :
1034 17344751 : ++i;
1035 : }
1036 :
1037 : /* Swap operands. */
1038 9621530 : if (swapped)
1039 : {
1040 42162 : if (dump_enabled_p ())
1041 564 : dump_printf_loc (MSG_NOTE, vect_location,
1042 : "swapped operands to match def types in %G",
1043 : stmt_info->stmt);
1044 : }
1045 :
1046 : return 0;
1047 : }
1048 :
1049 : /* Return true if call statements CALL1 and CALL2 are similar enough
1050 : to be combined into the same SLP group. */
1051 :
1052 : bool
1053 67691 : compatible_calls_p (gcall *call1, gcall *call2, bool allow_two_operators)
1054 : {
1055 67691 : unsigned int nargs = gimple_call_num_args (call1);
1056 67691 : if (nargs != gimple_call_num_args (call2))
1057 : return false;
1058 :
1059 50468 : auto cfn1 = gimple_call_combined_fn (call1);
1060 50468 : auto cfn2 = gimple_call_combined_fn (call2);
1061 50468 : if (cfn1 != cfn2
1062 39 : && (!allow_two_operators
1063 39 : || !((cfn1 == CFN_FMA || cfn1 == CFN_FMS)
1064 2 : && (cfn2 == CFN_FMA || cfn2 == CFN_FMS))))
1065 : return false;
1066 :
1067 50431 : if (gimple_call_internal_p (call1))
1068 : {
1069 8034 : if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
1070 8034 : TREE_TYPE (gimple_call_lhs (call2))))
1071 : return false;
1072 17269 : for (unsigned int i = 0; i < nargs; ++i)
1073 9235 : if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
1074 9235 : TREE_TYPE (gimple_call_arg (call2, i))))
1075 : return false;
1076 : }
1077 : else
1078 : {
1079 42397 : if (!operand_equal_p (gimple_call_fn (call1),
1080 42397 : gimple_call_fn (call2), 0))
1081 : return false;
1082 :
1083 38334 : if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
1084 : return false;
1085 : }
1086 :
1087 : /* Check that any unvectorized arguments are equal. */
1088 20812 : if (const int *map = vect_get_operand_map (call1, false, false))
1089 : {
1090 73 : unsigned int nkept = *map++;
1091 73 : unsigned int mapi = 0;
1092 231 : for (unsigned int i = 0; i < nargs; ++i)
1093 158 : if (mapi < nkept && map[mapi] == int (i))
1094 85 : mapi += 1;
1095 73 : else if (!operand_equal_p (gimple_call_arg (call1, i),
1096 73 : gimple_call_arg (call2, i)))
1097 : return false;
1098 : }
1099 :
1100 : return true;
1101 : }
1102 :
1103 : /* Verify if the scalar stmts STMTS are isomorphic, require data
1104 : permutation or are of unsupported types of operation.
1105 : Return false if at least one (or all in case of BB vectorization)
1106 : stmt is unvectorizable or the comparison could not be carried out.
1107 : Return true if they all (or at least one in case of BB vectorization)
1108 : are and indicate in MATCHES[] which stmts are not isomorphic to the
1109 : stmt at *START_I.
1110 :
1111 : This function is designed to be invoked repeatedly on the same
1112 : set of STMTS with increasing *START_I.
1113 :
1114 : When *START_I is zero and the function returns true MATCHES[] will be
1115 : initialized with, in case of BB vectorization, unvectorizable stmts
1116 : marked with -2, stmts isomorphic to *START_I as *START_I and
1117 : other vectorizable stmts with -1 (not isomorphic to *START_I).
1118 : *START_I, when initially zero, is updated to the first vectorizable
1119 : statement, so MATCHES[] can have a prefix with entries valued -2.
1120 :
1121 : When *START_I is not zero MATCHES[] is expected to be pre-initialized
1122 : by a former call with *START_I zero and MATCHES[*START_I] should be -1.
1123 : The function will return true and have the stmts isomorphic to
1124 : MATCHES[*START_I] marked with *START_I.
1125 :
1126 : *TWO_OPERATORS indicates whether the group of isomorphic statements
1127 : uses two related operations like PLUS_EXPR and MINUS_EXPR. If
1128 : TWO_OPERATORS is NULL such case is not considered isomorphic.
1129 :
1130 : SWAP[] indicates whether for a stmt to be isomorphic to its group
1131 : leader, has to have its operands swapped (1) or its predicate inverted (2).
1132 : COND_EXPR is possibly isomorphic to another one after swapping its
1133 : operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
1134 : the first stmt by swapping the two operands of comparison; set SWAP[i]
1135 : to 2 if stmt I is isormorphic to the first stmt by inverting the code
1136 : of comparison. Take A1 >= B1 ? X1 : Y1 as an example, it can be swapped
1137 : to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
1138 :
1139 : static bool
1140 6417004 : vect_build_slp_tree_3 (vec_info *vinfo, vec<stmt_vec_info> stmts,
1141 : match_elt_t *matches, unsigned char *swap,
1142 : bool *two_operators, tree vectype, unsigned *start_i)
1143 : {
1144 6417004 : stmt_vec_info first_stmt_info = NULL;
1145 6417004 : code_helper first_stmt_code = ERROR_MARK;
1146 6417004 : code_helper alt_stmt_code = ERROR_MARK;
1147 6417004 : code_helper first_cond_code = ERROR_MARK;
1148 6417004 : bool need_same_oprnds = false;
1149 6417004 : tree first_lhs = NULL_TREE;
1150 6417004 : tree first_op1 = NULL_TREE;
1151 6417004 : stmt_vec_info first_load = NULL, prev_first_load = NULL;
1152 6417004 : bool first_stmt_ldst_p = false, first_stmt_ldst_masklen_p = false;
1153 6417004 : bool first_stmt_phi_p = false;
1154 6417004 : int first_reduc_idx = -1;
1155 :
1156 6417004 : basic_block common_bb = NULL;
1157 6417004 : gimple *trapping_stmt = NULL;
1158 6417004 : int first_match = -1;
1159 :
1160 26481694 : for (unsigned i = *start_i; i < stmts.length (); ++i)
1161 : {
1162 20064961 : stmt_vec_info stmt_info = stmts[i];
1163 20064961 : bool ldst_p = false;
1164 20064961 : bool ldst_masklen_p = false;
1165 20064961 : bool phi_p = false;
1166 20064961 : code_helper rhs_code = ERROR_MARK;
1167 :
1168 20064961 : if (*start_i == 0)
1169 : {
1170 18159704 : swap[i] = 0;
1171 18159704 : matches[i] = -1;
1172 : }
1173 1905257 : else if (matches[i] != -1)
1174 20064690 : continue;
1175 :
1176 20003348 : if (!stmt_info)
1177 : {
1178 : /* ??? We shouldn't run into this. */
1179 52056 : gcc_assert (first_match != -1);
1180 52056 : matches[i] = first_match;
1181 52056 : continue;
1182 : }
1183 :
1184 19951292 : gimple *stmt = stmt_info->stmt;
1185 19951292 : if (dump_enabled_p ())
1186 251611 : dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for %G", stmt);
1187 :
1188 : /* Fail to vectorize statements marked as unvectorizable, throw
1189 : or are volatile. */
1190 19951292 : if (!STMT_VINFO_VECTORIZABLE (stmt_info)
1191 19555650 : || stmt_can_throw_internal (cfun, stmt)
1192 38452747 : || gimple_has_volatile_ops (stmt))
1193 : {
1194 413215 : if (dump_enabled_p ())
1195 596 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1196 : "Build SLP failed: unvectorizable statement %G",
1197 : stmt);
1198 413215 : if (is_a <bb_vec_info> (vinfo))
1199 : {
1200 413215 : matches[i] = -2;
1201 413215 : continue;
1202 : }
1203 6417004 : return false;
1204 : }
1205 :
1206 19538077 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
1207 19538077 : tree lhs = gimple_get_lhs (stmt);
1208 19538077 : if (lhs == NULL_TREE && !call_stmt)
1209 : {
1210 34 : if (dump_enabled_p ())
1211 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1212 : "Build SLP failed: not GIMPLE_ASSIGN nor "
1213 : "GIMPLE_CALL %G", stmt);
1214 34 : if (is_a <bb_vec_info> (vinfo))
1215 : {
1216 34 : matches[i] = -2;
1217 34 : continue;
1218 : }
1219 : return false;
1220 : }
1221 :
1222 19538043 : if (call_stmt)
1223 : {
1224 212611 : combined_fn cfn = gimple_call_combined_fn (call_stmt);
1225 212611 : if (cfn != CFN_LAST && cfn != CFN_MASK_CALL)
1226 67862 : rhs_code = cfn;
1227 : else
1228 : rhs_code = CALL_EXPR;
1229 :
1230 212611 : if (cfn == CFN_GATHER_LOAD
1231 212611 : || cfn == CFN_SCATTER_STORE)
1232 : ldst_p = true;
1233 : else if (cfn == CFN_MASK_LOAD
1234 : || cfn == CFN_MASK_GATHER_LOAD
1235 : || cfn == CFN_MASK_LEN_GATHER_LOAD
1236 : || cfn == CFN_MASK_SCATTER_STORE
1237 : || cfn == CFN_MASK_LEN_SCATTER_STORE)
1238 : {
1239 : ldst_p = true;
1240 : ldst_masklen_p = true;
1241 : }
1242 : else if (cfn == CFN_MASK_STORE)
1243 : {
1244 : ldst_p = true;
1245 : ldst_masklen_p = true;
1246 : rhs_code = CFN_MASK_STORE;
1247 : }
1248 : else if (cfn == CFN_GOMP_SIMD_LANE)
1249 : ;
1250 200486 : else if ((cfn != CFN_LAST
1251 : && cfn != CFN_MASK_CALL
1252 55737 : && internal_fn_p (cfn)
1253 40957 : && !vectorizable_internal_fn_p (as_internal_fn (cfn)))
1254 200321 : || gimple_call_tail_p (call_stmt)
1255 200321 : || gimple_call_noreturn_p (call_stmt)
1256 400807 : || gimple_call_chain (call_stmt))
1257 : {
1258 1776 : if (dump_enabled_p ())
1259 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1260 : "Build SLP failed: unsupported call type %G",
1261 : (gimple *) call_stmt);
1262 1776 : if (is_a <bb_vec_info> (vinfo))
1263 : {
1264 1776 : matches[i] = -2;
1265 1776 : continue;
1266 : }
1267 : return false;
1268 : }
1269 : }
1270 19325432 : else if (gimple_code (stmt) == GIMPLE_PHI)
1271 : {
1272 19485819 : rhs_code = ERROR_MARK;
1273 : phi_p = true;
1274 : }
1275 : else
1276 : {
1277 18271237 : rhs_code = gimple_assign_rhs_code (stmt);
1278 18271237 : ldst_p = STMT_VINFO_DATA_REF (stmt_info) != nullptr;
1279 : }
1280 :
1281 18271237 : if (!ldst_p
1282 : && !phi_p
1283 13331528 : && rhs_code.is_tree_code ()
1284 13271902 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_binary
1285 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_unary
1286 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_expression
1287 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_comparison
1288 248688 : && rhs_code != VIEW_CONVERT_EXPR
1289 : && rhs_code != CALL_EXPR
1290 : && rhs_code != BIT_FIELD_REF
1291 18271237 : && rhs_code != SSA_NAME)
1292 : {
1293 50448 : if (dump_enabled_p ())
1294 98 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1295 : "Build SLP failed: operation unsupported %G",
1296 : stmt);
1297 50448 : if (is_a <bb_vec_info> (vinfo))
1298 : {
1299 50303 : matches[i] = -2;
1300 50303 : continue;
1301 : }
1302 : return false;
1303 : }
1304 :
1305 : /* Non-grouped store or load. */
1306 19485819 : if (ldst_p
1307 5150544 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
1308 961856 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
1309 674141 : && rhs_code != CFN_GATHER_LOAD
1310 674141 : && rhs_code != CFN_MASK_GATHER_LOAD
1311 674141 : && rhs_code != CFN_MASK_LEN_GATHER_LOAD
1312 674141 : && rhs_code != CFN_SCATTER_STORE
1313 674141 : && rhs_code != CFN_MASK_SCATTER_STORE
1314 674141 : && rhs_code != CFN_MASK_LEN_SCATTER_STORE
1315 674141 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1316 : /* Not grouped loads are handled as externals for BB
1317 : vectorization. Treat them as not vectorizable. */
1318 20140781 : && is_a <bb_vec_info> (vinfo))
1319 : {
1320 : /* Not grouped load. */
1321 0 : if (dump_enabled_p ())
1322 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1323 : "Build SLP failed: not grouped load %G", stmt);
1324 0 : matches[i] = -2;
1325 0 : continue;
1326 : }
1327 :
1328 19485819 : if (!ldst_p && rhs_code == BIT_FIELD_REF)
1329 : {
1330 47577 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1331 47577 : if (!is_a <bb_vec_info> (vinfo)
1332 47451 : || TREE_CODE (vec) != SSA_NAME
1333 47427 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vec)
1334 : /* When the element types are not compatible we pun the
1335 : source to the target vectype which requires equal size. */
1336 94948 : || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
1337 44607 : || !types_compatible_p (TREE_TYPE (vectype),
1338 44607 : TREE_TYPE (TREE_TYPE (vec))))
1339 4191 : && !operand_equal_p (TYPE_SIZE (vectype),
1340 4191 : TYPE_SIZE (TREE_TYPE (vec)))))
1341 : {
1342 2530 : if (dump_enabled_p ())
1343 32 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1344 : "Build SLP failed: "
1345 : "BIT_FIELD_REF not supported\n");
1346 2530 : if (is_a <bb_vec_info> (vinfo))
1347 : {
1348 2404 : matches[i] = -2;
1349 2404 : continue;
1350 : }
1351 : return false;
1352 : }
1353 : }
1354 :
1355 : /* Check the operation. */
1356 19483289 : if (first_match == -1)
1357 : {
1358 6315294 : first_match = i;
1359 6315294 : first_stmt_info = stmt_info;
1360 6315294 : common_bb = gimple_bb (stmt_info->stmt);
1361 6315294 : first_lhs = lhs;
1362 6315294 : first_stmt_code = rhs_code;
1363 6315294 : first_stmt_ldst_p = ldst_p;
1364 6315294 : first_stmt_ldst_masklen_p = ldst_masklen_p;
1365 6315294 : first_stmt_phi_p = phi_p;
1366 6315294 : first_reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
1367 :
1368 : /* Shift arguments should be equal in all the packed stmts for a
1369 : vector shift with scalar shift operand. */
1370 6315294 : if (rhs_code == LSHIFT_EXPR
1371 6212879 : || rhs_code == RSHIFT_EXPR
1372 6116937 : || rhs_code == LROTATE_EXPR
1373 12432117 : || rhs_code == RROTATE_EXPR)
1374 : {
1375 : /* First see if we have a vector/vector shift. */
1376 198956 : if (!directly_supported_p (rhs_code, vectype, optab_vector))
1377 : {
1378 : /* No vector/vector shift, arrange for a vector/scalar
1379 : SLP layout. */
1380 185450 : need_same_oprnds = true;
1381 185450 : first_op1 = gimple_assign_rhs2 (stmt);
1382 : }
1383 : }
1384 6116338 : else if (rhs_code == WIDEN_LSHIFT_EXPR)
1385 : {
1386 0 : need_same_oprnds = true;
1387 0 : first_op1 = gimple_assign_rhs2 (stmt);
1388 : }
1389 6116338 : else if (rhs_code == CFN_DIV_POW2)
1390 : {
1391 0 : need_same_oprnds = true;
1392 0 : first_op1 = gimple_call_arg (call_stmt, 1);
1393 : }
1394 6116338 : else if (rhs_code == CFN_GOMP_SIMD_LANE)
1395 : {
1396 4054 : need_same_oprnds = true;
1397 4054 : first_op1 = gimple_call_arg (call_stmt, 1);
1398 : }
1399 : }
1400 : else
1401 : {
1402 13167995 : int comm_arg;
1403 13168396 : if (first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1404 : /* For SLP reduction groups the index isn't necessarily
1405 : uniform but only that of the first stmt matters. */
1406 2570 : && !(first_reduc_idx != -1
1407 2570 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1408 2570 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info))
1409 13167995 : && !(first_reduc_idx != -1
1410 1141 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1411 1141 : && (comm_arg = first_commutative_argument
1412 1141 : (rhs_code, TREE_TYPE (lhs))) >= 0
1413 : && (first_reduc_idx
1414 886 : == 2 * comm_arg + 1 - STMT_VINFO_REDUC_IDX (stmt_info))))
1415 : {
1416 401 : if (dump_enabled_p ())
1417 : {
1418 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1419 : "Build SLP failed: different reduc_idx "
1420 : "%d instead of %d in %G",
1421 : STMT_VINFO_REDUC_IDX (stmt_info),
1422 : first_reduc_idx, stmt);
1423 : }
1424 : /* Mismatch. */
1425 401 : continue;
1426 : }
1427 13167594 : if (!ldst_p
1428 13167594 : && two_operators
1429 10480708 : && first_stmt_code != rhs_code
1430 14721474 : && alt_stmt_code == ERROR_MARK)
1431 : alt_stmt_code = rhs_code;
1432 14711008 : if ((!ldst_p
1433 10480708 : && first_stmt_code != rhs_code
1434 1553880 : && (first_stmt_code != IMAGPART_EXPR
1435 206 : || rhs_code != REALPART_EXPR)
1436 1553880 : && (first_stmt_code != REALPART_EXPR
1437 674 : || rhs_code != IMAGPART_EXPR)
1438 : /* Handle mismatches in plus/minus by computing both
1439 : and merging the results. */
1440 1553880 : && !((((first_stmt_code == PLUS_EXPR
1441 1448392 : || first_stmt_code == MINUS_EXPR)
1442 129872 : && (alt_stmt_code == PLUS_EXPR
1443 119938 : || alt_stmt_code == MINUS_EXPR))
1444 1527195 : || ((first_stmt_code == CFN_FMA
1445 1527191 : || first_stmt_code == CFN_FMS)
1446 4 : && (alt_stmt_code == CFN_FMA
1447 4 : || alt_stmt_code == CFN_FMS)))
1448 26687 : && rhs_code == alt_stmt_code)
1449 1569196 : && !(first_stmt_code.is_tree_code ()
1450 1427752 : && rhs_code.is_tree_code ()
1451 1308040 : && (TREE_CODE_CLASS (tree_code (first_stmt_code))
1452 : == tcc_comparison)
1453 142837 : && (swap_tree_comparison (tree_code (first_stmt_code))
1454 142837 : == tree_code (rhs_code))
1455 : && (first_reduc_idx == -1
1456 0 : || REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
1457 : || (ldst_p
1458 5373772 : && (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1459 2686886 : != STMT_VINFO_GROUPED_ACCESS (first_stmt_info)))
1460 : || (ldst_p
1461 2629883 : && (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1462 2629883 : != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info)))
1463 11624334 : || first_stmt_ldst_p != ldst_p
1464 11624188 : || (ldst_p && first_stmt_ldst_masklen_p != ldst_masklen_p)
1465 24791774 : || first_stmt_phi_p != phi_p)
1466 : {
1467 1543414 : if (dump_enabled_p ())
1468 : {
1469 3687 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1470 : "Build SLP failed: different operation "
1471 : "in stmt %G", stmt);
1472 3687 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1473 : "original stmt %G", first_stmt_info->stmt);
1474 : }
1475 : /* Mismatch. */
1476 1543414 : continue;
1477 : }
1478 :
1479 11644041 : if (!ldst_p
1480 8994466 : && first_stmt_code == BIT_FIELD_REF
1481 11656029 : && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
1482 31849 : != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
1483 : {
1484 19861 : if (dump_enabled_p ())
1485 116 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1486 : "Build SLP failed: different BIT_FIELD_REF "
1487 : "arguments in %G", stmt);
1488 : /* Mismatch. */
1489 19861 : continue;
1490 : }
1491 :
1492 11604319 : if (call_stmt
1493 91428 : && first_stmt_code != CFN_MASK_LOAD
1494 11695101 : && first_stmt_code != CFN_MASK_STORE)
1495 : {
1496 90315 : if (!is_a <gcall *> (stmts[0]->stmt)
1497 90315 : || !compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
1498 : call_stmt, true))
1499 : {
1500 69503 : if (dump_enabled_p ())
1501 13 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1502 : "Build SLP failed: different calls in %G",
1503 : stmt);
1504 : /* Mismatch. */
1505 69503 : continue;
1506 : }
1507 : }
1508 :
1509 11580340 : if (phi_p
1510 11534816 : && (gimple_bb (first_stmt_info->stmt)
1511 220663 : != gimple_bb (stmt_info->stmt)))
1512 : {
1513 45524 : if (dump_enabled_p ())
1514 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1515 : "Build SLP failed: different BB for PHI %G",
1516 : stmt);
1517 : /* Mismatch. */
1518 45524 : continue;
1519 : }
1520 :
1521 11489292 : if (need_same_oprnds)
1522 : {
1523 67238 : tree other_op1 = gimple_arg (stmt, 1);
1524 67238 : if (!operand_equal_p (first_op1, other_op1, 0))
1525 : {
1526 12122 : if (dump_enabled_p ())
1527 195 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1528 : "Build SLP failed: different shift "
1529 : "arguments in %G", stmt);
1530 : /* Mismatch. */
1531 12122 : continue;
1532 : }
1533 : }
1534 :
1535 11477915 : if (first_lhs
1536 11477170 : && lhs
1537 11477170 : && !types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (first_lhs)))
1538 : {
1539 745 : if (dump_enabled_p ())
1540 15 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1541 : "Build SLP failed: different vector type "
1542 : "in %G", stmt);
1543 : /* Mismatch. */
1544 745 : continue;
1545 : }
1546 :
1547 : /* We need to ensure all stmts are in the same BB when one stmt could
1548 : trap. */
1549 11476425 : if (trapping_stmt || gimple_could_trap_p (stmt))
1550 : {
1551 978060 : gcc_assert (!trapping_stmt || common_bb);
1552 978060 : if (gimple_bb (stmt) != common_bb)
1553 : {
1554 6872 : if (dump_enabled_p ())
1555 13 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1556 : "Build SLP failed: not all stmts in same "
1557 : "BB but possibly trapping operation in %G",
1558 : trapping_stmt ? trapping_stmt : stmt);
1559 6872 : continue;
1560 : }
1561 : }
1562 : }
1563 :
1564 : /* Grouped store or load. */
1565 17784847 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1566 : {
1567 4125513 : gcc_assert (ldst_p);
1568 4125513 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1569 : {
1570 : /* Store. */
1571 3014310 : gcc_assert (rhs_code == CFN_MASK_STORE
1572 : || REFERENCE_CLASS_P (lhs)
1573 : || DECL_P (lhs));
1574 : }
1575 : else
1576 : {
1577 : /* Load. */
1578 1111203 : first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
1579 1111203 : if (prev_first_load)
1580 : {
1581 : /* Check that there are no loads from different interleaving
1582 : chains in the same node. */
1583 506172 : if (prev_first_load != first_load)
1584 : {
1585 70352 : if (dump_enabled_p ())
1586 2296 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1587 : vect_location,
1588 : "Build SLP failed: different "
1589 : "interleaving chains in one node %G",
1590 : stmt);
1591 : /* Mismatch. */
1592 70352 : continue;
1593 : }
1594 : }
1595 : else
1596 : prev_first_load = first_load;
1597 : }
1598 : }
1599 : /* Non-grouped store or load. */
1600 13659334 : else if (ldst_p)
1601 : {
1602 961529 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
1603 673814 : && rhs_code != CFN_GATHER_LOAD
1604 : && rhs_code != CFN_MASK_GATHER_LOAD
1605 : && rhs_code != CFN_MASK_LEN_GATHER_LOAD
1606 : && rhs_code != CFN_SCATTER_STORE
1607 : && rhs_code != CFN_MASK_SCATTER_STORE
1608 : && rhs_code != CFN_MASK_LEN_SCATTER_STORE
1609 1635343 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1610 : {
1611 654654 : gcc_assert (!is_a <bb_vec_info> (vinfo));
1612 : /* For loop vectorization we can handle splats the same we
1613 : handle single element interleaving. Likewise we can handle
1614 : a collection of invariant refs. */
1615 729504 : if (stmt_info != first_stmt_info
1616 654940 : && !(integer_zerop (DR_STEP (STMT_VINFO_DATA_REF (stmt_info)))
1617 286 : && integer_zerop (DR_STEP (STMT_VINFO_DATA_REF
1618 : (first_stmt_info)))))
1619 : {
1620 : /* Not grouped load. */
1621 74850 : if (dump_enabled_p ())
1622 160 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1623 : "Build SLP failed: not grouped load %G",
1624 : stmt);
1625 74850 : continue;
1626 : }
1627 : }
1628 : }
1629 : /* Not memory operation. */
1630 : else
1631 : {
1632 12697805 : if (rhs_code == COND_EXPR)
1633 : {
1634 68164 : tree cond_expr = gimple_assign_rhs1 (stmt);
1635 68164 : enum tree_code cond_code = TREE_CODE (cond_expr);
1636 68164 : enum tree_code swap_code = ERROR_MARK;
1637 68164 : enum tree_code invert_code = ERROR_MARK;
1638 :
1639 68164 : if (i == (unsigned)first_match)
1640 56644 : first_cond_code = TREE_CODE (cond_expr);
1641 11520 : else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1642 : {
1643 0 : bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
1644 0 : swap_code = swap_tree_comparison (cond_code);
1645 0 : invert_code = invert_tree_comparison (cond_code, honor_nans);
1646 : }
1647 :
1648 68164 : if (first_cond_code == cond_code)
1649 : ;
1650 : /* Isomorphic can be achieved by swapping. */
1651 0 : else if (first_cond_code == swap_code)
1652 0 : swap[i] = 1;
1653 : /* Isomorphic can be achieved by inverting. */
1654 0 : else if (first_cond_code == invert_code)
1655 0 : swap[i] = 2;
1656 : else
1657 : {
1658 0 : if (dump_enabled_p ())
1659 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1660 : "Build SLP failed: different"
1661 : " operation %G", stmt);
1662 : /* Mismatch. */
1663 0 : continue;
1664 : }
1665 : }
1666 :
1667 12697805 : if (i != (unsigned) first_match
1668 8846169 : && first_stmt_code != rhs_code
1669 67636 : && first_stmt_code.is_tree_code ()
1670 67634 : && rhs_code.is_tree_code ()
1671 67634 : && TREE_CODE_CLASS ((tree_code)first_stmt_code) == tcc_comparison
1672 12739284 : && (swap_tree_comparison ((tree_code)first_stmt_code)
1673 41479 : == (tree_code)rhs_code))
1674 41479 : swap[i] = 1;
1675 :
1676 12697805 : if (i != (unsigned) first_match
1677 8846169 : && first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1678 1869 : && first_reduc_idx != -1
1679 1869 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1680 1869 : && rhs_code.is_tree_code ()
1681 1865 : && commutative_tree_code (tree_code (rhs_code))
1682 12699668 : && first_reduc_idx == 1 - STMT_VINFO_REDUC_IDX (stmt_info))
1683 1863 : swap[i] = 1;
1684 : }
1685 :
1686 : /* We need to ensure all stmts are in the same BB when one stmt could
1687 : trap. Not matching stmts are not relevant, so exclude those. */
1688 17639645 : if (!trapping_stmt && gimple_could_trap_p (stmt))
1689 : trapping_stmt = stmt;
1690 17639645 : if (common_bb != gimple_bb (stmt))
1691 : {
1692 67782 : common_bb = NULL;
1693 67782 : gcc_assert (!trapping_stmt);
1694 : }
1695 :
1696 17639645 : matches[i] = first_match;
1697 : }
1698 :
1699 : /* Record if we allowed two distinct operations for the SLP node. */
1700 6416733 : if (((first_stmt_code == PLUS_EXPR
1701 5889356 : || first_stmt_code == MINUS_EXPR)
1702 640138 : && (alt_stmt_code == PLUS_EXPR
1703 632746 : || alt_stmt_code == MINUS_EXPR))
1704 12815696 : || ((first_stmt_code == CFN_FMA
1705 6398913 : || first_stmt_code == CFN_FMS)
1706 50 : && (alt_stmt_code == CFN_FMA
1707 50 : || alt_stmt_code == CFN_FMS)))
1708 17772 : *two_operators = true;
1709 :
1710 6416733 : if (first_match == -1)
1711 : {
1712 : /* If there was no useful stmt, fail. Can only happen during
1713 : the first sweep. */
1714 101439 : gcc_assert (*start_i == 0);
1715 101439 : *start_i = stmts.length () - 1;
1716 101439 : return false;
1717 : }
1718 :
1719 6315294 : *start_i = first_match;
1720 6315294 : return true;
1721 : }
1722 :
1723 : /* Verify if the scalar stmts STMTS are isomorphic, require data
1724 : permutation or are of unsupported types of operation. Return
1725 : true if they are, otherwise return false and indicate in *MATCHES
1726 : the groups of isomorphic stmts. See vect_build_slp_tree_3 for
1727 : details. *TWO_OPERATORS is for the first isomorphic group,
1728 : knowledge whether following isomorphic groups have one or two operators
1729 : is not retained. */
1730 :
1731 : static bool
1732 6300742 : vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
1733 : vec<stmt_vec_info> stmts, match_elt_t *matches,
1734 : bool *two_operators, tree *node_vectype)
1735 : {
1736 6300742 : stmt_vec_info first_stmt_info = stmts[0];
1737 6300742 : unsigned int group_size = stmts.length ();
1738 6300742 : tree vectype;
1739 6300742 : if (!vect_get_vector_types_for_stmt (vinfo, first_stmt_info, &vectype,
1740 : group_size))
1741 : {
1742 : /* Fatal mismatch. */
1743 258478 : matches[0] = -1;
1744 258478 : return false;
1745 : }
1746 6042264 : if (is_a <bb_vec_info> (vinfo)
1747 6042264 : && known_le (TYPE_VECTOR_SUBPARTS (vectype), 1U))
1748 : {
1749 386555 : if (dump_enabled_p ())
1750 324 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1751 : "Build SLP failed: not using single lane "
1752 : "vector type %T\n", vectype);
1753 386555 : matches[0] = -1;
1754 386555 : return false;
1755 : }
1756 : /* Check nunits required but continue analysis, producing matches[]
1757 : as if nunits was not an issue. This allows splitting of groups
1758 : to happen. */
1759 5655709 : unsigned HOST_WIDE_INT const_nunits = 0;
1760 5655709 : if (vectype
1761 5655679 : && is_a <bb_vec_info> (vinfo)
1762 11311388 : && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
1763 : {
1764 265816 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
1765 265816 : || const_nunits > group_size)
1766 : {
1767 0 : if (dump_enabled_p ())
1768 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1769 : "Build SLP failed: unrolling required "
1770 : "in basic block SLP\n");
1771 : /* Fatal mismatch. */
1772 0 : matches[0] = -1;
1773 0 : return false;
1774 : }
1775 : }
1776 :
1777 5655709 : gcc_assert (vectype || !gimple_get_lhs (first_stmt_info->stmt));
1778 5655709 : *node_vectype = vectype;
1779 :
1780 5655709 : unsigned start_i = 0;
1781 5655709 : if (!vect_build_slp_tree_3 (vinfo, stmts, matches, swap, two_operators,
1782 : vectype, &start_i))
1783 : {
1784 : /* Fatal mismatch. */
1785 101710 : matches[0] = -1;
1786 101710 : return false;
1787 : }
1788 5553999 : gcc_assert (matches[start_i] == (int)start_i);
1789 : /* Discover further isomorphic groups. */
1790 17805412 : for (start_i = start_i + 1; start_i < group_size; ++start_i)
1791 12251413 : if (matches[start_i] == -1)
1792 : {
1793 761295 : unsigned prev_start_i = start_i;
1794 761295 : bool tem_two_operators;
1795 761295 : bool res = vect_build_slp_tree_3 (vinfo, stmts, matches, swap,
1796 : &tem_two_operators,
1797 : vectype, &start_i);
1798 761295 : gcc_assert (res && matches[start_i] == (int)prev_start_i);
1799 : }
1800 :
1801 : bool res = true;
1802 23442589 : for (unsigned i = 0; i < group_size; ++i)
1803 17888590 : if (matches[i] != 0)
1804 1897974 : res = false;
1805 : if (!res)
1806 : return false;
1807 :
1808 : return true;
1809 : }
1810 :
1811 : /* Traits for the hash_set to record failed SLP builds for a stmt set.
1812 : Note we never remove apart from at destruction time so we do not
1813 : need a special value for deleted that differs from empty. */
1814 : struct bst_traits
1815 : {
1816 : typedef vec <stmt_vec_info> value_type;
1817 : typedef vec <stmt_vec_info> compare_type;
1818 : static inline hashval_t hash (value_type);
1819 : static inline bool equal (value_type existing, value_type candidate);
1820 521291669 : static inline bool is_empty (value_type x) { return !x.exists (); }
1821 116940844 : static inline bool is_deleted (value_type x) { return !x.exists (); }
1822 : static const bool empty_zero_p = true;
1823 0 : static inline void mark_empty (value_type &x) { x.release (); }
1824 : static inline void mark_deleted (value_type &x) { x.release (); }
1825 10035731 : static inline void remove (value_type &x) { x.release (); }
1826 : };
1827 : inline hashval_t
1828 101921882 : bst_traits::hash (value_type x)
1829 : {
1830 101921882 : inchash::hash h;
1831 443364624 : for (unsigned i = 0; i < x.length (); ++i)
1832 341442742 : h.add_int (x[i] ? gimple_uid (x[i]->stmt) : -1);
1833 101921882 : return h.end ();
1834 : }
1835 : inline bool
1836 89134573 : bst_traits::equal (value_type existing, value_type candidate)
1837 : {
1838 267403719 : if (existing.length () != candidate.length ())
1839 : return false;
1840 89182500 : for (unsigned i = 0; i < existing.length (); ++i)
1841 84499723 : if (existing[i] != candidate[i])
1842 : return false;
1843 : return true;
1844 : }
1845 :
1846 : typedef hash_map <vec <stmt_vec_info>, slp_tree,
1847 : simple_hashmap_traits <bst_traits, slp_tree> >
1848 : scalar_stmts_to_slp_tree_map_t;
1849 :
1850 : /* Release BST_MAP. */
1851 :
1852 : static void
1853 1953149 : release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map)
1854 : {
1855 : /* The map keeps a reference on SLP nodes built, release that. */
1856 11988880 : for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin ();
1857 11988880 : it != bst_map->end (); ++it)
1858 10035731 : if ((*it).second)
1859 10035731 : vect_free_slp_tree ((*it).second);
1860 3906298 : delete bst_map;
1861 1953149 : }
1862 :
1863 : /* ??? This was std::pair<std::pair<tree_code, vect_def_type>, tree>
1864 : but then vec::insert does memmove and that's not compatible with
1865 : std::pair. */
1866 : struct chain_op_t
1867 : {
1868 4476480 : chain_op_t (tree_code code_, vect_def_type dt_, tree op_)
1869 4476480 : : code (code_), dt (dt_), op (op_) {}
1870 : tree_code code;
1871 : vect_def_type dt;
1872 : tree op;
1873 : };
1874 :
1875 : /* Comparator for sorting associatable chains. */
1876 :
1877 : static int
1878 13049784 : dt_sort_cmp (const void *op1_, const void *op2_, void *)
1879 : {
1880 13049784 : auto *op1 = (const chain_op_t *) op1_;
1881 13049784 : auto *op2 = (const chain_op_t *) op2_;
1882 13049784 : if (op1->dt != op2->dt)
1883 2047539 : return (int)op1->dt - (int)op2->dt;
1884 11002245 : return (int)op1->code - (int)op2->code;
1885 : }
1886 :
1887 : /* Linearize the associatable expression chain at START with the
1888 : associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
1889 : filling CHAIN with the result and using WORKLIST as intermediate storage.
1890 : CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
1891 : or MINUS_EXPR. *CHAIN_STMTS if not NULL is filled with all computation
1892 : stmts, starting with START. When ALLOW_ALT_CODE is false, do not
1893 : follow into MINUS_EXPR when building a PLUS chain (treat MINUS as leaf). */
1894 :
1895 : static void
1896 1931098 : vect_slp_linearize_chain (vec_info *vinfo,
1897 : vec<std::pair<tree_code, gimple *> > &worklist,
1898 : vec<chain_op_t> &chain,
1899 : enum tree_code code, gimple *start,
1900 : gimple *&code_stmt, gimple *&alt_code_stmt,
1901 : vec<gimple *> *chain_stmts,
1902 : bool allow_alt_code = true)
1903 : {
1904 : /* For each lane linearize the addition/subtraction (or other
1905 : uniform associatable operation) expression tree. */
1906 1931098 : worklist.safe_push (std::make_pair (code, start));
1907 4476480 : while (!worklist.is_empty ())
1908 : {
1909 2545382 : auto entry = worklist.pop ();
1910 2545382 : gassign *stmt = as_a <gassign *> (entry.second);
1911 2545382 : enum tree_code in_code = entry.first;
1912 5090764 : enum tree_code this_code = gimple_assign_rhs_code (stmt);
1913 : /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE. */
1914 2545382 : if (!code_stmt
1915 2545382 : && gimple_assign_rhs_code (stmt) == code)
1916 1619853 : code_stmt = stmt;
1917 925529 : else if (!alt_code_stmt
1918 925529 : && gimple_assign_rhs_code (stmt) == MINUS_EXPR)
1919 369505 : alt_code_stmt = stmt;
1920 2545382 : if (chain_stmts)
1921 2465893 : chain_stmts->safe_push (stmt);
1922 7636146 : for (unsigned opnum = 1; opnum <= 2; ++opnum)
1923 : {
1924 5090764 : tree op = gimple_op (stmt, opnum);
1925 5090764 : vect_def_type dt;
1926 5090764 : stmt_vec_info def_stmt_info;
1927 5090764 : bool res = vect_is_simple_use (op, vinfo, &dt, &def_stmt_info);
1928 5090764 : gcc_assert (res);
1929 5090764 : if (dt == vect_internal_def
1930 5090764 : && is_pattern_stmt_p (def_stmt_info))
1931 9407 : op = gimple_get_lhs (def_stmt_info->stmt);
1932 5090764 : gimple *use_stmt;
1933 5090764 : use_operand_p use_p;
1934 5090764 : if (dt == vect_internal_def
1935 4633946 : && single_imm_use (op, &use_p, &use_stmt)
1936 2941153 : && is_gimple_assign (def_stmt_info->stmt)
1937 7827145 : && (gimple_assign_rhs_code (def_stmt_info->stmt) == code
1938 2122440 : || (allow_alt_code
1939 57766 : && code == PLUS_EXPR
1940 37519 : && (gimple_assign_rhs_code (def_stmt_info->stmt)
1941 : == MINUS_EXPR))))
1942 : {
1943 614284 : tree_code op_def_code = this_code;
1944 614284 : if (op_def_code == MINUS_EXPR && opnum == 1)
1945 56410 : op_def_code = PLUS_EXPR;
1946 614284 : if (in_code == MINUS_EXPR)
1947 222 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1948 614284 : worklist.safe_push (std::make_pair (op_def_code,
1949 614284 : def_stmt_info->stmt));
1950 : }
1951 : else
1952 : {
1953 4476480 : tree_code op_def_code = this_code;
1954 4476480 : if (op_def_code == MINUS_EXPR && opnum == 1)
1955 313217 : op_def_code = PLUS_EXPR;
1956 4476480 : if (in_code == MINUS_EXPR)
1957 4412 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1958 4476480 : chain.safe_push (chain_op_t (op_def_code, dt, op));
1959 : }
1960 : }
1961 : }
1962 1931098 : }
1963 :
1964 : /* Distance from the node currently being discovered to the closest upthread
1965 : commutative operation whose operand-zero discovery may still be fixed by
1966 : retrying with swapped operands, or -1U if there is none. */
1967 :
1968 : static unsigned least_upthread_swappable_op_distance = -1U;
1969 :
1970 : static slp_tree
1971 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1972 : vec<stmt_vec_info> stmts,
1973 : match_elt_t *matches, unsigned *limit,
1974 : unsigned *tree_size,
1975 : scalar_stmts_to_slp_tree_map_t *bst_map);
1976 :
1977 : static slp_tree
1978 6822612 : vect_build_slp_tree (vec_info *vinfo,
1979 : vec<stmt_vec_info> stmts,
1980 : match_elt_t *matches, unsigned *limit, unsigned *tree_size,
1981 : scalar_stmts_to_slp_tree_map_t *bst_map)
1982 : {
1983 6822612 : unsigned int group_size = stmts.length ();
1984 6822612 : if (slp_tree *leader = bst_map->get (stmts))
1985 : {
1986 516065 : if (dump_enabled_p ())
1987 19301 : dump_printf_loc (MSG_NOTE, vect_location, "re-using %sSLP tree %p\n",
1988 19301 : !(*leader)->failed ? "" : "failed ",
1989 : (void *) *leader);
1990 516065 : if (!(*leader)->failed)
1991 : {
1992 466197 : SLP_TREE_REF_COUNT (*leader)++;
1993 466197 : stmts.release ();
1994 466197 : return *leader;
1995 : }
1996 49868 : memcpy (matches, (*leader)->failed, sizeof (match_elt_t) * group_size);
1997 49868 : return NULL;
1998 : }
1999 :
2000 : /* Single-lane SLP doesn't have the chance of run-away, do not account
2001 : it to the limit. */
2002 6306547 : if (stmts.length () > 1)
2003 : {
2004 3523453 : if (*limit == 0)
2005 : {
2006 1320 : if (dump_enabled_p ())
2007 15 : dump_printf_loc (MSG_NOTE, vect_location,
2008 : "SLP discovery limit exceeded\n");
2009 1320 : memset (matches, -1, sizeof (match_elt_t) * group_size);
2010 1320 : return NULL;
2011 : }
2012 3522133 : --*limit;
2013 : }
2014 :
2015 : /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
2016 : so we can pick up backedge destinations during discovery. */
2017 6305227 : slp_tree res = new _slp_tree;
2018 6305227 : SLP_TREE_DEF_TYPE (res) = vect_internal_def;
2019 6305227 : SLP_TREE_SCALAR_STMTS (res) = stmts;
2020 6305227 : bst_map->put (stmts.copy (), res);
2021 :
2022 6305227 : if (dump_enabled_p ())
2023 163901 : dump_printf_loc (MSG_NOTE, vect_location,
2024 : "starting SLP discovery for node %p\n", (void *) res);
2025 :
2026 6305227 : slp_tree res_ = vect_build_slp_tree_2 (vinfo, res, stmts,
2027 : matches, limit, tree_size, bst_map);
2028 6305227 : if (!res_)
2029 : {
2030 2253639 : if (dump_enabled_p ())
2031 8604 : dump_printf_loc (MSG_NOTE, vect_location,
2032 : "SLP discovery for node %p failed\n", (void *) res);
2033 : /* Mark the node invalid so we can detect those when still in use
2034 : as backedge destinations. */
2035 2253639 : SLP_TREE_SCALAR_STMTS (res) = vNULL;
2036 2253639 : SLP_TREE_DEF_TYPE (res) = vect_uninitialized_def;
2037 2253639 : res->failed = XNEWVEC (match_elt_t, group_size);
2038 2253639 : if (flag_checking)
2039 : {
2040 : unsigned i;
2041 3953263 : for (i = 0; i < group_size; ++i)
2042 3953263 : if (matches[i] != 0)
2043 : break;
2044 2253639 : gcc_assert (i < group_size);
2045 : }
2046 2253639 : memcpy (res->failed, matches, sizeof (match_elt_t) * group_size);
2047 : }
2048 : else
2049 : {
2050 4051588 : if (dump_enabled_p ())
2051 155297 : dump_printf_loc (MSG_NOTE, vect_location,
2052 : "SLP discovery for node %p succeeded\n",
2053 : (void *) res);
2054 4051588 : gcc_assert (res_ == res);
2055 : /* Keep a reference for the bst_map use. */
2056 4051588 : SLP_TREE_REF_COUNT (res)++;
2057 : /* For BB vectorization nodes that need splitting should be failed. */
2058 4051588 : gcc_assert (!is_a <bb_vec_info> (vinfo)
2059 : || multiple_p (group_size,
2060 : TYPE_VECTOR_SUBPARTS (res->vectype)));
2061 : }
2062 : return res_;
2063 : }
2064 :
2065 : /* Helper for building an associated SLP node chain. */
2066 :
2067 : static void
2068 165 : vect_slp_build_two_operator_nodes (slp_tree perm, tree vectype,
2069 : slp_tree op0, slp_tree op1,
2070 : stmt_vec_info oper1, stmt_vec_info oper2,
2071 : vec<std::pair<unsigned, unsigned> > lperm)
2072 : {
2073 165 : unsigned group_size = SLP_TREE_LANES (op1);
2074 :
2075 165 : slp_tree child1 = new _slp_tree;
2076 165 : SLP_TREE_DEF_TYPE (child1) = vect_internal_def;
2077 165 : SLP_TREE_VECTYPE (child1) = vectype;
2078 165 : SLP_TREE_LANES (child1) = group_size;
2079 165 : SLP_TREE_CHILDREN (child1).create (2);
2080 165 : SLP_TREE_CHILDREN (child1).quick_push (op0);
2081 165 : SLP_TREE_CHILDREN (child1).quick_push (op1);
2082 165 : SLP_TREE_REPRESENTATIVE (child1) = oper1;
2083 :
2084 165 : slp_tree child2 = new _slp_tree;
2085 165 : SLP_TREE_DEF_TYPE (child2) = vect_internal_def;
2086 165 : SLP_TREE_VECTYPE (child2) = vectype;
2087 165 : SLP_TREE_LANES (child2) = group_size;
2088 165 : SLP_TREE_CHILDREN (child2).create (2);
2089 165 : SLP_TREE_CHILDREN (child2).quick_push (op0);
2090 165 : SLP_TREE_REF_COUNT (op0)++;
2091 165 : SLP_TREE_CHILDREN (child2).quick_push (op1);
2092 165 : SLP_TREE_REF_COUNT (op1)++;
2093 165 : SLP_TREE_REPRESENTATIVE (child2) = oper2;
2094 :
2095 165 : SLP_TREE_DEF_TYPE (perm) = vect_internal_def;
2096 165 : SLP_TREE_CODE (perm) = VEC_PERM_EXPR;
2097 165 : SLP_TREE_VECTYPE (perm) = vectype;
2098 165 : SLP_TREE_LANES (perm) = group_size;
2099 165 : SLP_TREE_REPRESENTATIVE (perm) = NULL;
2100 165 : SLP_TREE_LANE_PERMUTATION (perm) = lperm;
2101 165 : SLP_TREE_CHILDREN (perm).quick_push (child1);
2102 165 : SLP_TREE_CHILDREN (perm).quick_push (child2);
2103 165 : }
2104 :
2105 : /* For isomorphic matches[], indicate a splitting point according to
2106 : CONST_NUNITS. */
2107 :
2108 : static void
2109 99311 : force_split_matches (match_elt_t *matches, unsigned group_size,
2110 : unsigned const_nunits)
2111 : {
2112 99311 : if (dump_enabled_p ())
2113 56 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2114 : "Build SLP failed: unrolling required "
2115 : "in basic block SLP\n");
2116 :
2117 : /* With constant vector elements simulate a mismatch at the
2118 : point we need to split. But indicate the tail is isomorphic. */
2119 99311 : unsigned tail = group_size & (const_nunits - 1);
2120 480053 : for (unsigned i = 0; i < group_size - tail; ++i)
2121 380742 : matches[i] = 0;
2122 222235 : for (unsigned i = group_size - tail; i < group_size; ++i)
2123 122924 : matches[i] = (int)(group_size - tail);
2124 99311 : }
2125 :
2126 : /* Recursively build an SLP tree starting from NODE.
2127 : Fail (and return a value not equal to zero) if def-stmts are not
2128 : isomorphic, require data permutation or are of unsupported types of
2129 : operation. Otherwise, return 0.
2130 : The value returned is the depth in the SLP tree where a mismatch
2131 : was found. */
2132 :
2133 : static slp_tree
2134 6305227 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
2135 : vec<stmt_vec_info> stmts,
2136 : match_elt_t *matches, unsigned *limit,
2137 : unsigned *tree_size,
2138 : scalar_stmts_to_slp_tree_map_t *bst_map)
2139 : {
2140 6305227 : unsigned int group_size = stmts.length ();
2141 6305227 : unsigned nops, i, this_tree_size = 0;
2142 :
2143 6305227 : matches[0] = -1;
2144 :
2145 6305227 : stmt_vec_info stmt_info = stmts[0];
2146 6305227 : if (!is_a<gcall *> (stmt_info->stmt)
2147 : && !is_a<gassign *> (stmt_info->stmt)
2148 : && !is_a<gphi *> (stmt_info->stmt))
2149 : return NULL;
2150 :
2151 6305029 : nops = gimple_num_args (stmt_info->stmt);
2152 6305029 : if (const int *map = vect_get_operand_map (stmt_info))
2153 37084 : nops = map[0];
2154 :
2155 : /* If the SLP node is a PHI (induction or reduction), terminate
2156 : the recursion. */
2157 6305029 : bool *skip_args = XALLOCAVEC (bool, nops);
2158 6305029 : memset (skip_args, 0, sizeof (bool) * nops);
2159 6305029 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
2160 2968884 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
2161 : {
2162 324373 : tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
2163 324373 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
2164 : group_size);
2165 324373 : if (!vectype)
2166 : {
2167 4281 : if (dump_enabled_p ())
2168 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2169 : "Build SLP failed: unsupported data-type in %G\n",
2170 : stmt_info->stmt);
2171 : return NULL;
2172 : }
2173 :
2174 320092 : vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
2175 320092 : if (def_type == vect_induction_def)
2176 : {
2177 : /* Induction PHIs are not cycles but walk the initial
2178 : value. Only for inner loops through, for outer loops
2179 : we need to pick up the value from the actual PHIs
2180 : to more easily support peeling and epilogue vectorization. */
2181 211565 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2182 211565 : if (!nested_in_vect_loop_p (loop, stmt_info))
2183 210716 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2184 : else
2185 : loop = loop->inner;
2186 211565 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2187 : }
2188 108527 : else if (def_type == vect_reduction_def
2189 : || def_type == vect_double_reduction_def
2190 : || def_type == vect_nested_cycle
2191 108527 : || def_type == vect_first_order_recurrence)
2192 : {
2193 : /* Else def types have to match. */
2194 : stmt_vec_info other_info;
2195 : bool all_same = true;
2196 232729 : FOR_EACH_VEC_ELT (stmts, i, other_info)
2197 : {
2198 125514 : if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
2199 6305227 : return NULL;
2200 125508 : if (other_info != stmt_info)
2201 15115 : all_same = false;
2202 : }
2203 107215 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2204 : /* Reduction initial values are not explicitly represented. */
2205 107215 : if (def_type != vect_first_order_recurrence
2206 107215 : && gimple_bb (stmt_info->stmt) == loop->header)
2207 103946 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2208 : /* Reduction chain backedge defs are filled manually.
2209 : ??? Need a better way to identify a SLP reduction chain PHI.
2210 : Or a better overall way to SLP match those. */
2211 107215 : if (stmts.length () > 1
2212 107215 : && all_same && def_type == vect_reduction_def)
2213 679 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2214 : }
2215 1306 : else if (def_type != vect_internal_def)
2216 : return NULL;
2217 : }
2218 :
2219 :
2220 6300742 : bool two_operators = false;
2221 6300742 : unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
2222 6300742 : tree vectype = NULL_TREE;
2223 6300742 : if (!vect_build_slp_tree_1 (vinfo, swap, stmts, matches, &two_operators,
2224 : &vectype))
2225 : return NULL;
2226 :
2227 4692186 : bool soft_fail
2228 4692186 : = (is_a <bb_vec_info> (vinfo)
2229 4692186 : && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)));
2230 :
2231 : /* If the SLP node is a load, terminate the recursion unless masked. */
2232 4692186 : if (STMT_VINFO_DATA_REF (stmt_info)
2233 2280170 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2234 : {
2235 998138 : if (soft_fail)
2236 : {
2237 9728 : force_split_matches (matches, group_size,
2238 9728 : TYPE_VECTOR_SUBPARTS (vectype).to_constant ());
2239 9728 : return NULL;
2240 : }
2241 988410 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2242 : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
2243 : else
2244 : {
2245 969595 : (*tree_size)++;
2246 969595 : node = vect_create_new_slp_node (node, stmts, 0);
2247 969595 : SLP_TREE_VECTYPE (node) = vectype;
2248 : /* And compute the load permutation. Whether it is actually
2249 : a permutation depends on the unrolling factor which is
2250 : decided later. */
2251 969595 : vec<unsigned> load_permutation;
2252 969595 : int j;
2253 969595 : stmt_vec_info load_info;
2254 969595 : load_permutation.create (group_size);
2255 969595 : stmt_vec_info first_stmt_info
2256 969595 : = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2257 969595 : ? DR_GROUP_FIRST_ELEMENT (stmt_info) : stmt_info;
2258 969595 : bool any_permute = false;
2259 2357954 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
2260 : {
2261 1388359 : int load_place;
2262 1388359 : if (! load_info)
2263 : {
2264 51816 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2265 : load_place = j;
2266 : else
2267 : load_place = 0;
2268 : }
2269 1336543 : else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2270 792332 : load_place = vect_get_place_in_interleaving_chain
2271 792332 : (load_info, first_stmt_info);
2272 : else
2273 : /* Recognize the splat case as { 0, 0, ... } but make
2274 : sure to use the appropriate refs for collections
2275 : of invariant refs. */
2276 544211 : load_place = (load_info == stmt_info) ? 0 : j;
2277 844425 : gcc_assert (load_place != -1);
2278 1388359 : any_permute |= load_place != j;
2279 1388359 : load_permutation.quick_push (load_place);
2280 : }
2281 :
2282 969595 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
2283 : {
2284 3469 : gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD));
2285 3469 : bool has_gaps = false;
2286 3469 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2287 260 : for (stmt_vec_info si = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
2288 1201 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2289 941 : if (DR_GROUP_GAP (si) != 1)
2290 80 : has_gaps = true;
2291 : /* We cannot handle permuted masked loads directly, see
2292 : PR114375. We cannot handle strided masked loads or masked
2293 : loads with gaps unless the mask is uniform. */
2294 3469 : if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
2295 260 : && (DR_GROUP_GAP (first_stmt_info) != 0
2296 200 : || (has_gaps
2297 35 : && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))))
2298 6858 : || STMT_VINFO_STRIDED_P (stmt_info))
2299 : {
2300 108 : load_permutation.release ();
2301 108 : matches[0] = -1;
2302 966289 : return NULL;
2303 : }
2304 :
2305 : /* For permuted masked loads do an unpermuted masked load of
2306 : the whole group followed by a SLP permute node. */
2307 3361 : if (any_permute
2308 3361 : || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
2309 146 : && DR_GROUP_SIZE (first_stmt_info) != group_size))
2310 : {
2311 : /* Discover the whole unpermuted load. */
2312 55 : vec<stmt_vec_info> stmts2;
2313 55 : unsigned dr_group_size = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2314 100 : ? DR_GROUP_SIZE (first_stmt_info) : 1;
2315 55 : stmts2.create (dr_group_size);
2316 55 : stmts2.quick_grow_cleared (dr_group_size);
2317 55 : unsigned i = 0;
2318 55 : for (stmt_vec_info si = first_stmt_info;
2319 560 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2320 : {
2321 505 : if (si != first_stmt_info)
2322 1650 : for (unsigned k = 1; k < DR_GROUP_GAP (si); ++k)
2323 1200 : stmts2[i++] = NULL;
2324 505 : stmts2[i++] = si;
2325 : }
2326 55 : match_elt_t *matches2
2327 55 : = XALLOCAVEC (match_elt_t, dr_group_size);
2328 55 : slp_tree unperm_load
2329 55 : = vect_build_slp_tree (vinfo, stmts2, matches2, limit,
2330 55 : &this_tree_size, bst_map);
2331 : /* When we are able to do the full masked load emit that
2332 : followed by 'node' being the desired final permutation. */
2333 55 : if (unperm_load)
2334 : {
2335 32 : gcc_assert
2336 : (!SLP_TREE_LOAD_PERMUTATION (unperm_load).exists ());
2337 32 : lane_permutation_t lperm;
2338 32 : lperm.create (group_size);
2339 144 : for (unsigned j = 0; j < load_permutation.length (); ++j)
2340 80 : lperm.quick_push
2341 80 : (std::make_pair (0, load_permutation[j]));
2342 32 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2343 32 : SLP_TREE_CHILDREN (node).safe_push (unperm_load);
2344 32 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2345 32 : SLP_TREE_REPRESENTATIVE (node) = NULL;
2346 32 : load_permutation.release ();
2347 32 : return node;
2348 : }
2349 23 : stmts2.release ();
2350 23 : load_permutation.release ();
2351 23 : matches[0] = -1;
2352 23 : return NULL;
2353 : }
2354 3306 : load_permutation.release ();
2355 : }
2356 : else
2357 : {
2358 966126 : if (!any_permute
2359 824718 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2360 1266173 : && group_size == DR_GROUP_SIZE (first_stmt_info))
2361 143354 : load_permutation.release ();
2362 966126 : SLP_TREE_LOAD_PERMUTATION (node) = load_permutation;
2363 966126 : return node;
2364 : }
2365 : }
2366 : }
2367 3694048 : else if (gimple_assign_single_p (stmt_info->stmt)
2368 2571898 : && !gimple_vuse (stmt_info->stmt)
2369 3703619 : && gimple_assign_rhs_code (stmt_info->stmt) == BIT_FIELD_REF)
2370 : {
2371 3687 : if (soft_fail)
2372 : {
2373 223 : force_split_matches (matches, group_size,
2374 223 : TYPE_VECTOR_SUBPARTS (vectype).to_constant ());
2375 223 : return NULL;
2376 : }
2377 : /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
2378 : the same SSA name vector of a compatible type to vectype. */
2379 3464 : vec<std::pair<unsigned, unsigned> > lperm = vNULL;
2380 3464 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
2381 3464 : stmt_vec_info estmt_info;
2382 12148 : FOR_EACH_VEC_ELT (stmts, i, estmt_info)
2383 : {
2384 8831 : gassign *estmt = as_a <gassign *> (estmt_info->stmt);
2385 8831 : tree bfref = gimple_assign_rhs1 (estmt);
2386 8831 : HOST_WIDE_INT lane;
2387 8831 : if (!known_eq (bit_field_size (bfref),
2388 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
2389 17515 : || !constant_multiple_p (bit_field_offset (bfref),
2390 8831 : bit_field_size (bfref), &lane))
2391 : {
2392 147 : lperm.release ();
2393 147 : matches[0] = -1;
2394 147 : return NULL;
2395 : }
2396 8684 : lperm.safe_push (std::make_pair (0, (unsigned)lane));
2397 : }
2398 3317 : slp_tree vnode = vect_create_new_slp_node (vNULL);
2399 3317 : if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
2400 : /* ??? We record vectype here but we hide eventually necessary
2401 : punning and instead rely on code generation to materialize
2402 : VIEW_CONVERT_EXPRs as necessary. We instead should make
2403 : this explicit somehow. */
2404 1496 : SLP_TREE_VECTYPE (vnode) = vectype;
2405 : else
2406 : {
2407 : /* For different size but compatible elements we can still
2408 : use VEC_PERM_EXPR without punning. */
2409 1821 : gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
2410 : && types_compatible_p (TREE_TYPE (vectype),
2411 : TREE_TYPE (TREE_TYPE (vec))));
2412 1821 : SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
2413 : }
2414 3317 : auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
2415 3317 : unsigned HOST_WIDE_INT const_nunits;
2416 3317 : if (nunits.is_constant (&const_nunits))
2417 3317 : SLP_TREE_LANES (vnode) = const_nunits;
2418 3317 : SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
2419 : /* We are always building a permutation node even if it is an identity
2420 : permute to shield the rest of the vectorizer from the odd node
2421 : representing an actual vector without any scalar ops.
2422 : ??? We could hide it completely with making the permute node
2423 : external? */
2424 3317 : node = vect_create_new_slp_node (node, stmts, 1);
2425 3317 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2426 3317 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2427 3317 : SLP_TREE_VECTYPE (node) = vectype;
2428 3317 : SLP_TREE_CHILDREN (node).quick_push (vnode);
2429 3317 : SLP_TREE_REPRESENTATIVE (node) = NULL;
2430 3317 : return node;
2431 : }
2432 : /* When discovery reaches an associatable operation see whether we can
2433 : improve that to match up lanes in a way superior to the operand
2434 : swapping code which at most looks at two defs.
2435 : ??? For BB vectorization we cannot do the brute-force search
2436 : for matching as we can succeed by means of builds from scalars
2437 : and have no good way to "cost" one build against another. */
2438 3690361 : else if (is_a <loop_vec_info> (vinfo)
2439 : /* Do not bother for single-lane SLP. */
2440 2105771 : && group_size > 1
2441 : /* ??? We don't handle !vect_internal_def defs below. */
2442 102378 : && STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
2443 : /* ??? Do not associate a reduction, this will wreck REDUC_IDX
2444 : mapping as long as that exists on the stmt_info level. */
2445 83091 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1
2446 76223 : && is_gimple_assign (stmt_info->stmt)
2447 75856 : && (associative_tree_code (gimple_assign_rhs_code (stmt_info->stmt))
2448 52570 : || gimple_assign_rhs_code (stmt_info->stmt) == MINUS_EXPR)
2449 3715624 : && ((FLOAT_TYPE_P (vectype) && flag_associative_math)
2450 13828 : || (INTEGRAL_TYPE_P (TREE_TYPE (vectype))
2451 11267 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype)))))
2452 : {
2453 : /* See if we have a chain of (mixed) adds or subtracts or other
2454 : associatable ops. */
2455 18252 : enum tree_code code = gimple_assign_rhs_code (stmt_info->stmt);
2456 18252 : if (code == MINUS_EXPR)
2457 845 : code = PLUS_EXPR;
2458 18252 : stmt_vec_info other_op_stmt_info = NULL;
2459 18252 : stmt_vec_info op_stmt_info = NULL;
2460 18252 : unsigned chain_len = 0;
2461 18252 : auto_vec<chain_op_t> chain;
2462 18252 : auto_vec<std::pair<tree_code, gimple *> > worklist;
2463 18252 : auto_vec<vec<chain_op_t> > chains (group_size);
2464 18252 : auto_vec<slp_tree, 4> children;
2465 18252 : bool hard_fail = true;
2466 19276 : for (unsigned lane = 0; lane < group_size; ++lane)
2467 : {
2468 18939 : if (!stmts[lane])
2469 : {
2470 : /* ??? Below we require lane zero is present. */
2471 0 : if (lane == 0)
2472 : {
2473 : hard_fail = false;
2474 17915 : break;
2475 : }
2476 0 : chains.quick_push (vNULL);
2477 0 : continue;
2478 : }
2479 : /* For each lane linearize the addition/subtraction (or other
2480 : uniform associatable operation) expression tree. */
2481 18939 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
2482 18939 : vect_slp_linearize_chain (vinfo, worklist, chain, code,
2483 18939 : stmts[lane]->stmt, op_stmt, other_op_stmt,
2484 : NULL);
2485 18939 : if (!op_stmt_info && op_stmt)
2486 17598 : op_stmt_info = vinfo->lookup_stmt (op_stmt);
2487 18939 : if (!other_op_stmt_info && other_op_stmt)
2488 889 : other_op_stmt_info = vinfo->lookup_stmt (other_op_stmt);
2489 18939 : if (chain.length () == 2)
2490 : {
2491 : /* In a chain of just two elements resort to the regular
2492 : operand swapping scheme. Likewise if we run into a
2493 : length mismatch process regularly as well as we did not
2494 : process the other lanes we cannot report a good hint what
2495 : lanes to try swapping in the parent. */
2496 : hard_fail = false;
2497 : break;
2498 : }
2499 1027 : else if (chain_len == 0)
2500 377 : chain_len = chain.length ();
2501 1300 : else if (chain.length () != chain_len)
2502 : {
2503 : /* ??? Here we could slip in magic to compensate with
2504 : neutral operands. */
2505 3 : matches[lane] = -1;
2506 3 : if (lane != group_size - 1)
2507 3 : matches[0] = -1;
2508 : break;
2509 : }
2510 1024 : chains.quick_push (chain.copy ());
2511 1024 : chain.truncate (0);
2512 : }
2513 36504 : if (chains.length () == group_size)
2514 : {
2515 : /* We cannot yet use SLP_TREE_CODE to communicate the operation. */
2516 337 : if (!op_stmt_info)
2517 : {
2518 3 : hard_fail = false;
2519 3 : goto out;
2520 : }
2521 : /* Now we have a set of chains with the same length. */
2522 : /* 1. pre-sort according to def_type and operation. */
2523 1246 : for (unsigned lane = 0; lane < group_size; ++lane)
2524 1824 : chains[lane].stablesort (dt_sort_cmp, vinfo);
2525 334 : if (dump_enabled_p ())
2526 : {
2527 161 : dump_printf_loc (MSG_NOTE, vect_location,
2528 : "pre-sorted chains of %s\n",
2529 : get_tree_code_name (code));
2530 682 : for (unsigned lane = 0; lane < group_size; ++lane)
2531 : {
2532 521 : if (!stmts[lane])
2533 0 : dump_printf (MSG_NOTE, "--");
2534 : else
2535 2334 : for (unsigned opnum = 0; opnum < chain_len; ++opnum)
2536 3626 : dump_printf (MSG_NOTE, "%s %T ",
2537 1813 : get_tree_code_name (chains[lane][opnum].code),
2538 1813 : chains[lane][opnum].op);
2539 521 : dump_printf (MSG_NOTE, "\n");
2540 : }
2541 : }
2542 : /* 2. try to build children nodes, associating as necessary. */
2543 : /* 2a. prepare and perform early checks to avoid eating into
2544 : discovery limit unnecessarily. */
2545 334 : vect_def_type *dts = XALLOCAVEC (vect_def_type, chain_len);
2546 1407 : for (unsigned n = 0; n < chain_len; ++n)
2547 : {
2548 1073 : vect_def_type dt = chains[0][n].dt;
2549 1073 : unsigned lane;
2550 4115 : for (lane = 0; lane < group_size; ++lane)
2551 6084 : if (stmts[lane] && chains[lane][n].dt != dt)
2552 : {
2553 0 : if (dt == vect_constant_def
2554 0 : && chains[lane][n].dt == vect_external_def)
2555 : dt = vect_external_def;
2556 0 : else if (dt == vect_external_def
2557 0 : && chains[lane][n].dt == vect_constant_def)
2558 : ;
2559 : else
2560 : break;
2561 : }
2562 1073 : if (lane != group_size)
2563 : {
2564 0 : if (dump_enabled_p ())
2565 0 : dump_printf_loc (MSG_NOTE, vect_location,
2566 : "giving up on chain due to mismatched "
2567 : "def types\n");
2568 0 : matches[lane] = -1;
2569 0 : if (lane != group_size - 1)
2570 0 : matches[0] = -1;
2571 0 : goto out;
2572 : }
2573 1073 : dts[n] = dt;
2574 1073 : if (dt == vect_constant_def
2575 1073 : || dt == vect_external_def)
2576 : {
2577 : /* Check whether we can build the invariant. If we can't
2578 : we never will be able to. */
2579 83 : tree type = TREE_TYPE (chains[0][n].op);
2580 1073 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ()
2581 : && (TREE_CODE (type) == BOOLEAN_TYPE
2582 : || !can_duplicate_and_interleave_p (vinfo, group_size,
2583 : type)))
2584 : {
2585 : matches[0] = -1;
2586 : goto out;
2587 : }
2588 : }
2589 990 : else if (dt != vect_internal_def)
2590 : {
2591 : /* Not sure, we might need sth special.
2592 : gcc.dg/vect/pr96854.c,
2593 : gfortran.dg/vect/fast-math-pr37021.f90
2594 : and gfortran.dg/vect/pr61171.f trigger. */
2595 : /* Soft-fail for now. */
2596 0 : hard_fail = false;
2597 0 : goto out;
2598 : }
2599 : }
2600 : /* 2b. do the actual build. */
2601 1352 : for (unsigned n = 0; n < chain_len; ++n)
2602 : {
2603 1038 : vect_def_type dt = dts[n];
2604 1038 : unsigned lane;
2605 1038 : if (dt == vect_constant_def
2606 1038 : || dt == vect_external_def)
2607 : {
2608 83 : vec<tree> ops;
2609 83 : ops.create (group_size);
2610 439 : for (lane = 0; lane < group_size; ++lane)
2611 273 : if (stmts[lane])
2612 273 : ops.quick_push (chains[lane][n].op);
2613 : else
2614 0 : ops.quick_push (NULL_TREE);
2615 83 : slp_tree child = vect_create_new_slp_node (ops);
2616 83 : SLP_TREE_DEF_TYPE (child) = dt;
2617 83 : children.safe_push (child);
2618 : }
2619 : else
2620 : {
2621 955 : vec<stmt_vec_info> op_stmts;
2622 955 : op_stmts.create (group_size);
2623 955 : slp_tree child = NULL;
2624 : /* Brute-force our way. We have to consider a lane
2625 : failing after fixing an earlier fail up in the
2626 : SLP discovery recursion. So track the current
2627 : permute per lane. */
2628 955 : unsigned *perms = XALLOCAVEC (unsigned, group_size);
2629 955 : memset (perms, 0, sizeof (unsigned) * group_size);
2630 1051 : do
2631 : {
2632 1051 : op_stmts.truncate (0);
2633 5059 : for (lane = 0; lane < group_size; ++lane)
2634 2957 : if (stmts[lane])
2635 2957 : op_stmts.quick_push
2636 2957 : (vinfo->lookup_def (chains[lane][n].op));
2637 : else
2638 0 : op_stmts.quick_push (NULL);
2639 1051 : child = vect_build_slp_tree (vinfo, op_stmts,
2640 : matches, limit,
2641 : &this_tree_size, bst_map);
2642 : /* ??? We're likely getting too many fatal mismatches
2643 : here so maybe we want to ignore them (but then we
2644 : have no idea which lanes fatally mismatched). */
2645 : /* ??? Revisit this with matches[] improvements. */
2646 1051 : if (child || matches[0] != 0)
2647 : break;
2648 : /* Swap another lane we have not yet matched up into
2649 : lanes that did not match. If we run out of
2650 : permute possibilities for a lane terminate the
2651 : search. */
2652 291 : bool term = false;
2653 291 : for (lane = 1; lane < group_size; ++lane)
2654 195 : if (matches[lane] != 0)
2655 : {
2656 167 : if (n + perms[lane] + 1 == chain_len)
2657 : {
2658 : term = true;
2659 : break;
2660 : }
2661 148 : if (dump_enabled_p ())
2662 115 : dump_printf_loc (MSG_NOTE, vect_location,
2663 : "swapping operand %d and %d "
2664 : "of lane %d\n",
2665 : n, n + perms[lane] + 1, lane);
2666 296 : std::swap (chains[lane][n],
2667 148 : chains[lane][n + perms[lane] + 1]);
2668 148 : perms[lane]++;
2669 : }
2670 115 : if (term)
2671 : break;
2672 : }
2673 : while (1);
2674 955 : if (!child)
2675 : {
2676 20 : if (dump_enabled_p ())
2677 18 : dump_printf_loc (MSG_NOTE, vect_location,
2678 : "failed to match up op %d\n", n);
2679 20 : op_stmts.release ();
2680 20 : if (lane != group_size - 1)
2681 10 : matches[0] = -1;
2682 : else
2683 10 : matches[lane] = -1;
2684 20 : goto out;
2685 : }
2686 935 : if (dump_enabled_p ())
2687 : {
2688 421 : dump_printf_loc (MSG_NOTE, vect_location,
2689 : "matched up op %d to\n", n);
2690 421 : vect_print_slp_tree (MSG_NOTE, vect_location, child);
2691 : }
2692 935 : children.safe_push (child);
2693 : }
2694 : }
2695 : /* 3. build SLP nodes to combine the chain. */
2696 1149 : for (unsigned lane = 0; lane < group_size; ++lane)
2697 1682 : if (stmts[lane] && chains[lane][0].code != code)
2698 : {
2699 : /* See if there's any alternate all-PLUS entry. */
2700 : unsigned n;
2701 6 : for (n = 1; n < chain_len; ++n)
2702 : {
2703 30 : for (lane = 0; lane < group_size; ++lane)
2704 48 : if (stmts[lane] && chains[lane][n].code != code)
2705 : break;
2706 6 : if (lane == group_size)
2707 : break;
2708 : }
2709 6 : if (n != chain_len)
2710 : {
2711 : /* Swap that in at first position. */
2712 6 : std::swap (children[0], children[n]);
2713 30 : for (lane = 0; lane < group_size; ++lane)
2714 24 : if (stmts[lane])
2715 24 : std::swap (chains[lane][0], chains[lane][n]);
2716 : }
2717 : else
2718 : {
2719 : /* ??? When this triggers and we end up with two
2720 : vect_constant/external_def up-front things break (ICE)
2721 : spectacularly finding an insertion place for the
2722 : all-constant op. We should have a fully
2723 : vect_internal_def operand though(?) so we can swap
2724 : that into first place and then prepend the all-zero
2725 : constant. */
2726 0 : if (dump_enabled_p ())
2727 0 : dump_printf_loc (MSG_NOTE, vect_location,
2728 : "inserting constant zero to compensate "
2729 : "for (partially) negated first "
2730 : "operand\n");
2731 0 : chain_len++;
2732 0 : for (lane = 0; lane < group_size; ++lane)
2733 0 : if (stmts[lane])
2734 0 : chains[lane].safe_insert
2735 0 : (0, chain_op_t (code, vect_constant_def, NULL_TREE));
2736 0 : vec<tree> zero_ops;
2737 0 : zero_ops.create (group_size);
2738 0 : zero_ops.quick_push (build_zero_cst (TREE_TYPE (vectype)));
2739 0 : for (lane = 1; lane < group_size; ++lane)
2740 0 : if (stmts[lane])
2741 0 : zero_ops.quick_push (zero_ops[0]);
2742 : else
2743 0 : zero_ops.quick_push (NULL_TREE);
2744 0 : slp_tree zero = vect_create_new_slp_node (zero_ops);
2745 0 : SLP_TREE_DEF_TYPE (zero) = vect_constant_def;
2746 0 : children.safe_insert (0, zero);
2747 : }
2748 : break;
2749 : }
2750 1011 : for (unsigned i = 1; i < children.length (); ++i)
2751 : {
2752 697 : slp_tree op0 = children[i - 1];
2753 697 : slp_tree op1 = children[i];
2754 697 : bool this_two_op = false;
2755 2492 : for (unsigned lane = 0; lane < group_size; ++lane)
2756 3920 : if (stmts[lane] && chains[lane][i].code != chains[0][i].code)
2757 : {
2758 : this_two_op = true;
2759 : break;
2760 : }
2761 697 : slp_tree child;
2762 697 : if (i == children.length () - 1)
2763 314 : child = vect_create_new_slp_node (node, stmts, 2);
2764 : else
2765 383 : child = vect_create_new_slp_node (2, ERROR_MARK);
2766 697 : if (this_two_op)
2767 : {
2768 165 : vec<std::pair<unsigned, unsigned> > lperm;
2769 165 : lperm.create (group_size);
2770 756 : for (unsigned lane = 0; lane < group_size; ++lane)
2771 852 : lperm.quick_push (std::make_pair
2772 426 : (chains[lane][i].code != chains[0][i].code, lane));
2773 330 : vect_slp_build_two_operator_nodes (child, vectype, op0, op1,
2774 165 : (chains[0][i].code == code
2775 : ? op_stmt_info
2776 : : other_op_stmt_info),
2777 165 : (chains[0][i].code == code
2778 : ? other_op_stmt_info
2779 : : op_stmt_info),
2780 : lperm);
2781 : }
2782 : else
2783 : {
2784 532 : SLP_TREE_DEF_TYPE (child) = vect_internal_def;
2785 532 : SLP_TREE_VECTYPE (child) = vectype;
2786 532 : SLP_TREE_LANES (child) = group_size;
2787 532 : SLP_TREE_CHILDREN (child).quick_push (op0);
2788 532 : SLP_TREE_CHILDREN (child).quick_push (op1);
2789 532 : SLP_TREE_REPRESENTATIVE (child)
2790 1064 : = (chains[0][i].code == code
2791 532 : ? op_stmt_info : other_op_stmt_info);
2792 : }
2793 697 : children[i] = child;
2794 : }
2795 314 : *tree_size += this_tree_size + 1;
2796 1510 : while (!chains.is_empty ())
2797 859 : chains.pop ().release ();
2798 : return node;
2799 : }
2800 17915 : out:
2801 17938 : if (dump_enabled_p ())
2802 2887 : dump_printf_loc (MSG_NOTE, vect_location,
2803 : "failed to line up SLP graph by re-associating "
2804 : "operations in lanes%s\n",
2805 : !hard_fail ? " trying regular discovery" : "");
2806 17945 : while (!children.is_empty ())
2807 7 : vect_free_slp_tree (children.pop ());
2808 18103 : while (!chains.is_empty ())
2809 165 : chains.pop ().release ();
2810 : /* Hard-fail, otherwise we might run into quadratic processing of the
2811 : chains starting one stmt into the chain again. */
2812 17938 : if (hard_fail)
2813 : return NULL;
2814 : /* Fall thru to normal processing. */
2815 18252 : }
2816 :
2817 : /* Get at the operands, verifying they are compatible. */
2818 3712145 : vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
2819 3712145 : slp_oprnd_info oprnd_info;
2820 21102590 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
2821 : {
2822 27360018 : int res = vect_get_and_check_slp_defs (vinfo, vectype,
2823 13680009 : swap[i], skip_args,
2824 : stmts, i, &oprnds_info,
2825 : soft_fail);
2826 13680009 : if (res != 0)
2827 : /* ??? This puts -1 back into matches[] and the cache. */
2828 348030 : matches[(res == -1) ? 0 : i] = -1;
2829 13680009 : if (matches[0] == -1)
2830 : break;
2831 : }
2832 16989045 : for (i = 0; i < group_size; ++i)
2833 13532028 : if (matches[i] != 0)
2834 : {
2835 255128 : vect_free_oprnd_info (oprnds_info);
2836 255128 : return NULL;
2837 : }
2838 3457017 : swap = NULL;
2839 :
2840 : /* Instead of soft-failing here, continue building up the SLP
2841 : graph, failing only at leafs. Note as we don't know whether
2842 : we soft- or hard-failed children avoid any operand swapping
2843 : when we soft-failed. */
2844 :
2845 3457017 : bool has_two_operators_perm = false;
2846 17285085 : auto_vec<unsigned> two_op_perm_indices[2];
2847 3457017 : vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL};
2848 :
2849 3457017 : if (!soft_fail
2850 3315138 : && two_operators
2851 16719 : && oprnds_info.length () == 2
2852 3473734 : && group_size > 2)
2853 : {
2854 2653 : unsigned idx = 0;
2855 2653 : hash_map<gimple *, unsigned> seen;
2856 2653 : vec<slp_oprnd_info> new_oprnds_info
2857 2653 : = vect_create_oprnd_info (1, group_size);
2858 2653 : bool success = true;
2859 :
2860 2653 : enum tree_code code = ERROR_MARK;
2861 2653 : if (oprnds_info[0]->def_stmts[0]
2862 2653 : && is_a<gassign *> (oprnds_info[0]->def_stmts[0]->stmt))
2863 2596 : code = gimple_assign_rhs_code (oprnds_info[0]->def_stmts[0]->stmt);
2864 2653 : basic_block bb = nullptr;
2865 :
2866 6307 : for (unsigned j = 0; j < group_size; ++j)
2867 : {
2868 15167 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2869 : {
2870 11513 : stmt_vec_info stmt_info = oprnd_info->def_stmts[j];
2871 11513 : if (!stmt_info
2872 11346 : || !is_a<gassign *> (stmt_info->stmt)
2873 11346 : || gimple_assign_rhs_code (stmt_info->stmt) != code
2874 20854 : || skip_args[i])
2875 : {
2876 : success = false;
2877 2176 : break;
2878 : }
2879 : /* Avoid mixing lanes with defs in different basic-blocks. */
2880 9341 : if (!bb)
2881 2774 : bb = gimple_bb (vect_orig_stmt (stmt_info)->stmt);
2882 8483 : else if (gimple_bb (vect_orig_stmt (stmt_info)->stmt) != bb)
2883 : {
2884 : success = false;
2885 : break;
2886 : }
2887 :
2888 9337 : bool exists;
2889 9337 : unsigned &stmt_idx
2890 9337 : = seen.get_or_insert (stmt_info->stmt, &exists);
2891 :
2892 9337 : if (!exists)
2893 : {
2894 7958 : new_oprnds_info[0]->def_stmts.safe_push (stmt_info);
2895 7958 : new_oprnds_info[0]->ops.safe_push (oprnd_info->ops[j]);
2896 7958 : stmt_idx = idx;
2897 7958 : idx++;
2898 : }
2899 :
2900 9337 : two_op_perm_indices[i].safe_push (stmt_idx);
2901 : }
2902 :
2903 5830 : if (!success)
2904 : break;
2905 : }
2906 :
2907 2653 : if (success && idx == group_size)
2908 : {
2909 98 : if (dump_enabled_p ())
2910 : {
2911 0 : dump_printf_loc (MSG_NOTE, vect_location,
2912 : "Replace two_operators operands:\n");
2913 :
2914 0 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2915 : {
2916 0 : dump_printf_loc (MSG_NOTE, vect_location,
2917 : "Operand %u:\n", i);
2918 0 : for (unsigned j = 0; j < group_size; j++)
2919 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2920 0 : j, oprnd_info->def_stmts[j]->stmt);
2921 : }
2922 :
2923 0 : dump_printf_loc (MSG_NOTE, vect_location,
2924 : "With a single operand:\n");
2925 0 : for (unsigned j = 0; j < group_size; j++)
2926 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2927 0 : j, new_oprnds_info[0]->def_stmts[j]->stmt);
2928 : }
2929 :
2930 98 : two_op_scalar_stmts[0].safe_splice (oprnds_info[0]->def_stmts);
2931 98 : two_op_scalar_stmts[1].safe_splice (oprnds_info[1]->def_stmts);
2932 :
2933 98 : new_oprnds_info[0]->first_op_type = oprnds_info[0]->first_op_type;
2934 98 : new_oprnds_info[0]->first_dt = oprnds_info[0]->first_dt;
2935 98 : new_oprnds_info[0]->any_pattern = oprnds_info[0]->any_pattern;
2936 98 : new_oprnds_info[0]->first_gs_p = oprnds_info[0]->first_gs_p;
2937 98 : new_oprnds_info[0]->first_gs_info = oprnds_info[0]->first_gs_info;
2938 :
2939 98 : vect_free_oprnd_info (oprnds_info);
2940 98 : oprnds_info = new_oprnds_info;
2941 98 : nops = 1;
2942 98 : has_two_operators_perm = true;
2943 : }
2944 : else
2945 2555 : vect_free_oprnd_info (new_oprnds_info);
2946 2653 : }
2947 :
2948 6914034 : auto_vec<slp_tree, 4> children;
2949 :
2950 3457017 : stmt_info = stmts[0];
2951 :
2952 3457017 : int reduc_idx = -1;
2953 3457017 : int gs_scale = 0;
2954 3457017 : tree gs_base = NULL_TREE;
2955 :
2956 : /* Create SLP_TREE nodes for the definition node/s. */
2957 8704710 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2958 : {
2959 5480137 : slp_tree child = nullptr;
2960 5480137 : unsigned int j;
2961 5480137 : unsigned old_swap_distance;
2962 5480137 : bool can_swap;
2963 5480137 : bool can_swap_nonmatching;
2964 5480137 : bool *stmt_can_swap;
2965 :
2966 : /* We're skipping certain operands from processing, for example
2967 : outer loop reduction initial defs. */
2968 5480137 : if (skip_args[i])
2969 : {
2970 526906 : children.safe_push (NULL);
2971 5247693 : continue;
2972 : }
2973 :
2974 4953231 : if (oprnd_info->first_dt == vect_uninitialized_def)
2975 : {
2976 : /* COND_EXPR have one too many eventually if the condition
2977 : is a SSA name. */
2978 0 : gcc_assert (i == 3 && nops == 4);
2979 0 : continue;
2980 : }
2981 :
2982 4953231 : if (oprnd_info->first_gs_p)
2983 : {
2984 22975 : gs_scale = oprnd_info->first_gs_info.scale;
2985 22975 : gs_base = oprnd_info->first_gs_info.base;
2986 : }
2987 :
2988 4953231 : if (is_a <bb_vec_info> (vinfo)
2989 1817413 : && oprnd_info->first_dt == vect_internal_def
2990 5902001 : && !oprnd_info->any_pattern)
2991 : {
2992 : /* For BB vectorization, if all defs are the same do not
2993 : bother to continue the build along the single-lane
2994 : graph but use a splat of the scalar value. */
2995 902745 : stmt_vec_info first_def = oprnd_info->def_stmts[0];
2996 978731 : for (j = 1; j < group_size; ++j)
2997 924587 : if (oprnd_info->def_stmts[j] != first_def)
2998 : break;
2999 902745 : if (j == group_size
3000 : /* But avoid doing this for loads where we may be
3001 : able to CSE things, unless the stmt is not
3002 : vectorizable. */
3003 902745 : && (!STMT_VINFO_VECTORIZABLE (first_def)
3004 65892 : || !gimple_vuse (first_def->stmt)))
3005 : {
3006 44292 : if (dump_enabled_p ())
3007 131 : dump_printf_loc (MSG_NOTE, vect_location,
3008 : "Using a splat of the uniform operand %G",
3009 : first_def->stmt);
3010 44292 : oprnd_info->first_dt = vect_external_def;
3011 : }
3012 : }
3013 :
3014 4953231 : if (oprnd_info->first_dt == vect_external_def
3015 4953231 : || oprnd_info->first_dt == vect_constant_def)
3016 : {
3017 1675166 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ())
3018 : {
3019 : tree op0;
3020 : tree uniform_val = op0 = oprnd_info->ops[0];
3021 : for (j = 1; j < oprnd_info->ops.length (); ++j)
3022 : if (oprnd_info->ops[j]
3023 : && !operand_equal_p (uniform_val, oprnd_info->ops[j]))
3024 : {
3025 : uniform_val = NULL_TREE;
3026 : break;
3027 : }
3028 : if (!uniform_val
3029 : && !can_duplicate_and_interleave_p (vinfo,
3030 : oprnd_info->ops.length (),
3031 : TREE_TYPE (op0)))
3032 : {
3033 : for (unsigned k = 0; k < j; ++k)
3034 : matches[k] = 0;
3035 : matches[j] = -1;
3036 : for (unsigned k = j + 1; k < oprnd_info->ops.length (); ++k)
3037 : if (oprnd_info->ops[k]
3038 : && !operand_equal_p (op0, oprnd_info->ops[k]))
3039 : matches[k] = -1;
3040 : else
3041 : matches[k] = 0;
3042 : if (dump_enabled_p ())
3043 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3044 : "Build SLP failed: invalid type of def "
3045 : "for variable-length SLP %T\n", op0);
3046 85425 : goto fail;
3047 : }
3048 : }
3049 : /* At leafs force soft-failing. */
3050 1675166 : if (soft_fail)
3051 : {
3052 85425 : force_split_matches (matches, group_size,
3053 85425 : TYPE_VECTOR_SUBPARTS (vectype).to_constant ());
3054 85425 : goto fail;
3055 : }
3056 1589741 : slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops);
3057 1589741 : SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt;
3058 1589741 : oprnd_info->ops = vNULL;
3059 1589741 : children.safe_push (invnode);
3060 1589741 : continue;
3061 1589741 : }
3062 :
3063 : /* See which SLP operand a reduction chain continues on. We want
3064 : to chain even PHIs but not backedges. */
3065 3278065 : if (STMT_VINFO_REDUC_DEF (oprnd_info->def_stmts[0])
3066 3278065 : || STMT_VINFO_REDUC_IDX (oprnd_info->def_stmts[0]) != -1)
3067 : {
3068 238222 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle)
3069 : {
3070 784 : if (oprnd_info->first_dt == vect_double_reduction_def)
3071 392 : reduc_idx = i;
3072 : }
3073 237438 : else if (is_a <gphi *> (stmt_info->stmt)
3074 237438 : && gimple_phi_num_args
3075 103664 : (as_a <gphi *> (stmt_info->stmt)) != 1)
3076 : ;
3077 134171 : else if (STMT_VINFO_REDUC_IDX (stmt_info) == -1
3078 397 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_double_reduction_def)
3079 : ;
3080 134171 : else if (reduc_idx == -1)
3081 125679 : reduc_idx = i;
3082 : else
3083 : /* For .COND_* reduction operations the else value can be the
3084 : same as one of the operation operands. The other def
3085 : stmts have been moved, so we can't check easily. Check
3086 : it's a call at least. */
3087 8492 : gcc_assert (is_a <gcall *> (stmt_info->stmt));
3088 : }
3089 :
3090 : /* When we have a masked load with uniform mask discover this
3091 : as a single-lane mask with a splat permute. This way we can
3092 : recognize this as a masked load-lane by stripping the splat. */
3093 3278065 : if (is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
3094 60938 : && gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
3095 : IFN_MASK_LOAD)
3096 4839 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
3097 3278197 : && ! STMT_VINFO_SLP_VECT_ONLY (DR_GROUP_FIRST_ELEMENT (stmt_info)))
3098 : {
3099 70 : vec<stmt_vec_info> def_stmts2;
3100 70 : def_stmts2.create (1);
3101 70 : def_stmts2.quick_push (oprnd_info->def_stmts[0]);
3102 70 : child = vect_build_slp_tree (vinfo, def_stmts2,
3103 : matches, limit,
3104 : &this_tree_size, bst_map);
3105 70 : if (child)
3106 : {
3107 70 : slp_tree pnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
3108 70 : SLP_TREE_VECTYPE (pnode) = SLP_TREE_VECTYPE (child);
3109 70 : SLP_TREE_LANES (pnode) = group_size;
3110 70 : SLP_TREE_SCALAR_STMTS (pnode).create (group_size);
3111 70 : SLP_TREE_LANE_PERMUTATION (pnode).create (group_size);
3112 490 : for (unsigned k = 0; k < group_size; ++k)
3113 : {
3114 350 : SLP_TREE_SCALAR_STMTS (pnode)
3115 350 : .quick_push (oprnd_info->def_stmts[0]);
3116 350 : SLP_TREE_LANE_PERMUTATION (pnode)
3117 350 : .quick_push (std::make_pair (0u, 0u));
3118 : }
3119 70 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3120 70 : children.safe_push (pnode);
3121 70 : oprnd_info->def_stmts = vNULL;
3122 70 : continue;
3123 70 : }
3124 : else
3125 0 : def_stmts2.release ();
3126 : }
3127 :
3128 6555990 : can_swap = (!soft_fail
3129 3221269 : && i == 0
3130 2370746 : && (nops == 2 || nops == 3)
3131 1539337 : && oprnds_info.length () > 1
3132 1539337 : && oprnds_info[1]->first_dt == vect_internal_def
3133 622711 : && (is_gimple_assign (stmt_info->stmt)
3134 52589 : || is_gimple_call (stmt_info->stmt))
3135 : /* Swapping operands for reductions breaks assumptions
3136 : later on. */
3137 3853586 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1);
3138 3277995 : can_swap_nonmatching = can_swap;
3139 3277995 : stmt_can_swap = NULL;
3140 3277995 : if (can_swap)
3141 : {
3142 523961 : stmt_can_swap = XALLOCAVEC (bool, group_size);
3143 8143953 : for (j = 0; j < group_size; ++j)
3144 : {
3145 7619992 : stmt_can_swap[j] = false;
3146 7619992 : if (!stmts[j])
3147 : /* NULL lanes are gaps and have no stmt to swap. */
3148 0 : stmt_can_swap[j] = true;
3149 7619992 : else if (gassign *stmt = dyn_cast <gassign *> (stmts[j]->stmt))
3150 : {
3151 7613605 : tree_code code = gimple_assign_rhs_code (stmt);
3152 15227210 : stmt_can_swap[j] = (commutative_tree_code (code)
3153 7613605 : || commutative_ternary_tree_code (code));
3154 : }
3155 6387 : else if (gcall *call = dyn_cast <gcall *> (stmts[j]->stmt))
3156 : {
3157 6387 : internal_fn fn = (gimple_call_internal_p (call)
3158 6387 : ? gimple_call_internal_fn (call) : IFN_LAST);
3159 12774 : stmt_can_swap[j] = ((commutative_binary_fn_p (fn)
3160 5272 : || commutative_ternary_fn_p (fn))
3161 6423 : && first_commutative_argument (fn) == 0);
3162 : }
3163 :
3164 7619992 : if (j != 0 && !stmt_can_swap[j])
3165 7619992 : can_swap_nonmatching = false;
3166 : }
3167 : }
3168 :
3169 3277995 : old_swap_distance = least_upthread_swappable_op_distance;
3170 3277995 : if (can_swap_nonmatching)
3171 482320 : least_upthread_swappable_op_distance = 1;
3172 2795675 : else if (least_upthread_swappable_op_distance != -1U)
3173 354073 : least_upthread_swappable_op_distance++;
3174 3277995 : child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
3175 : matches, limit,
3176 : &this_tree_size, bst_map);
3177 3277995 : least_upthread_swappable_op_distance = old_swap_distance;
3178 3277995 : if (child != NULL)
3179 : {
3180 2745340 : oprnd_info->def_stmts = vNULL;
3181 2745340 : children.safe_push (child);
3182 2745340 : continue;
3183 : }
3184 :
3185 : /* If the SLP build for operand zero failed and operand zero
3186 : and one can be commuted try that for the scalar stmts
3187 : that failed the match. */
3188 532655 : if (/* A first scalar stmt mismatch signals a fatal mismatch. */
3189 532655 : matches[0] == 0
3190 532655 : && can_swap)
3191 : {
3192 : /* See whether we can swap the matching or the non-matching
3193 : stmt operands. */
3194 : bool swap_not_matching = true;
3195 69419 : do
3196 : {
3197 7068670 : for (j = 0; j < group_size; ++j)
3198 : {
3199 7017137 : if ((matches[j] == 0) != !swap_not_matching)
3200 75258 : continue;
3201 : /* Verify if we can swap operands of this stmt. */
3202 6941879 : if (!stmt_can_swap[j])
3203 : {
3204 17886 : if (!swap_not_matching)
3205 7872 : goto fail;
3206 : swap_not_matching = false;
3207 : break;
3208 : }
3209 : }
3210 : }
3211 61547 : while (j != group_size);
3212 :
3213 : /* Swap mismatched definition stmts. */
3214 51533 : if (dump_enabled_p ())
3215 428 : dump_printf_loc (MSG_NOTE, vect_location,
3216 : "Re-trying with swapped operands of stmts ");
3217 7039769 : for (j = 0; j < group_size; ++j)
3218 6988236 : if ((matches[j] == 0) == !swap_not_matching)
3219 : {
3220 13847600 : std::swap (oprnds_info[0]->def_stmts[j],
3221 6923800 : oprnds_info[1]->def_stmts[j]);
3222 13847600 : std::swap (oprnds_info[0]->ops[j],
3223 6923800 : oprnds_info[1]->ops[j]);
3224 6923800 : if (dump_enabled_p ())
3225 1091 : dump_printf (MSG_NOTE, "%d ", j);
3226 : }
3227 51533 : if (dump_enabled_p ())
3228 428 : dump_printf (MSG_NOTE, "\n");
3229 : /* After swapping some operands we lost track whether an
3230 : operand has any pattern defs so be conservative here. */
3231 100241 : if (oprnds_info[0]->any_pattern || oprnds_info[1]->any_pattern)
3232 2888 : oprnds_info[0]->any_pattern = oprnds_info[1]->any_pattern = true;
3233 : /* And try again with scratch 'matches' ... */
3234 51533 : match_elt_t *tem = XALLOCAVEC (match_elt_t, group_size);
3235 51533 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
3236 : tem, limit,
3237 : &this_tree_size, bst_map)) != NULL)
3238 : {
3239 6110 : oprnd_info->def_stmts = vNULL;
3240 6110 : children.safe_push (child);
3241 6110 : continue;
3242 : }
3243 : }
3244 611970 : fail:
3245 :
3246 : /* If the SLP build failed and we analyze a basic-block
3247 : simply treat nodes we fail to build as externally defined
3248 : (and thus build vectors from the scalar defs) when not
3249 : soft-failing.
3250 : The cost model will reject outright expensive cases.
3251 : ??? This doesn't treat cases where permutation ultimatively
3252 : fails (or we don't try permutation below). Ideally we'd
3253 : even compute a permutation that will end up with the maximum
3254 : SLP tree size... */
3255 611970 : if (is_a <bb_vec_info> (vinfo)
3256 : /* ??? Rejecting patterns this way doesn't work. We'd have to
3257 : do extra work to cancel the pattern so the uses see the
3258 : scalar version. */
3259 : /* Skip building vector operands from scalars while operand
3260 : discovery may still be fixed by retrying with swapped operands. */
3261 568079 : && (least_upthread_swappable_op_distance != 1
3262 : /* A first scalar stmt mismatch signals a fatal mismatch
3263 : that the parent commutative retry cannot recover.
3264 : ??? Possibly revisit this with the matches[] improvements
3265 : as we can have matches[0] == -2 here. */
3266 27025 : || matches[0] != 0)
3267 549593 : && !is_pattern_stmt_p (stmt_info)
3268 1134683 : && !oprnd_info->any_pattern)
3269 : {
3270 : /* But if there's a leading vector sized set of matching stmts
3271 : fail here so we can split the group. This matches the condition
3272 : vect_analyze_slp_instance uses. */
3273 : /* ??? We might want to split here and combine the results to support
3274 : multiple vector sizes better. */
3275 1158681 : for (j = 0; j < group_size; ++j)
3276 1158681 : if (matches[j] != 0)
3277 : break;
3278 522450 : if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype))
3279 522293 : && vect_slp_can_convert_to_external (oprnd_info->def_stmts))
3280 : {
3281 : /* When we'd soft-fail we do not want to build leaf nodes,
3282 : we also do not want to use the matches[] from a fatal
3283 : failure to guide splitting if we would build from scalars, so
3284 : indicate the soft-fail split point here in that case. */
3285 404195 : if (soft_fail)
3286 : {
3287 24669 : if (matches[0] == -1)
3288 3440 : force_split_matches (matches, group_size,
3289 : TYPE_VECTOR_SUBPARTS
3290 3440 : (vectype).to_constant ());
3291 : }
3292 : else
3293 : {
3294 379526 : if (dump_enabled_p ())
3295 824 : dump_printf_loc (MSG_NOTE, vect_location,
3296 : "Building vector operands from scalars\n");
3297 379526 : this_tree_size++;
3298 379526 : child = vect_create_new_slp_node (oprnd_info->ops);
3299 379526 : children.safe_push (child);
3300 379526 : oprnd_info->ops = vNULL;
3301 379526 : continue;
3302 : }
3303 : }
3304 : }
3305 :
3306 232444 : gcc_assert (child == NULL);
3307 481442 : FOR_EACH_VEC_ELT (children, j, child)
3308 16554 : if (child)
3309 16554 : vect_free_slp_tree (child);
3310 232444 : vect_free_oprnd_info (oprnds_info);
3311 232444 : return NULL;
3312 : }
3313 :
3314 3224573 : vect_free_oprnd_info (oprnds_info);
3315 :
3316 : /* Conversions might make children built successfully but we still
3317 : have to soft-fail the parent in that case. */
3318 3224573 : if (soft_fail)
3319 : {
3320 495 : force_split_matches (matches, group_size,
3321 495 : TYPE_VECTOR_SUBPARTS (vectype).to_constant ());
3322 1947 : for (slp_tree child : children)
3323 462 : if (child)
3324 462 : vect_free_slp_tree (child);
3325 : return NULL;
3326 : }
3327 :
3328 : /* If we have all children of a child built up from uniform scalars
3329 : or does more than one possibly expensive vector construction then
3330 : just throw that away, causing it built up from scalars.
3331 : The exception is the SLP node for the vector store. */
3332 3224078 : if (is_a <bb_vec_info> (vinfo)
3333 1146618 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3334 : /* ??? Rejecting patterns this way doesn't work. We'd have to
3335 : do extra work to cancel the pattern so the uses see the
3336 : scalar version. */
3337 3704926 : && !is_pattern_stmt_p (stmt_info))
3338 : {
3339 : slp_tree child;
3340 : unsigned j;
3341 : bool all_uniform_p = true;
3342 : unsigned n_vector_builds = 0;
3343 1353911 : FOR_EACH_VEC_ELT (children, j, child)
3344 : {
3345 901129 : if (!child)
3346 : ;
3347 901129 : else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3348 : all_uniform_p = false;
3349 642577 : else if (!vect_slp_tree_uniform_p (child))
3350 : {
3351 486944 : all_uniform_p = false;
3352 486944 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def)
3353 446186 : n_vector_builds++;
3354 : }
3355 : }
3356 452782 : if (all_uniform_p
3357 452782 : || n_vector_builds > 1
3358 767736 : || (n_vector_builds == children.length ()
3359 36586 : && is_a <gphi *> (stmt_info->stmt)))
3360 : {
3361 : /* Roll back. */
3362 142279 : matches[0] = -1;
3363 447737 : FOR_EACH_VEC_ELT (children, j, child)
3364 305458 : if (child)
3365 305458 : vect_free_slp_tree (child);
3366 :
3367 142279 : if (dump_enabled_p ())
3368 255 : dump_printf_loc (MSG_NOTE, vect_location,
3369 : "Building parent vector operands from "
3370 : "scalars instead\n");
3371 3457017 : return NULL;
3372 : }
3373 : }
3374 :
3375 3081799 : *tree_size += this_tree_size + 1;
3376 :
3377 3081799 : if (two_operators)
3378 : {
3379 : /* ??? We'd likely want to either cache in bst_map sth like
3380 : { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
3381 : the true { a+b, a+b, a+b, a+b } ... but there we don't have
3382 : explicit stmts to put in so the keying on 'stmts' doesn't
3383 : work (but we have the same issue with nodes that use 'ops'). */
3384 :
3385 9356 : if (has_two_operators_perm)
3386 : {
3387 46 : slp_tree child = children[0];
3388 46 : children.truncate (0);
3389 184 : for (i = 0; i < 2; i++)
3390 : {
3391 92 : slp_tree pnode
3392 92 : = vect_create_new_slp_node (two_op_scalar_stmts[i], 2);
3393 92 : SLP_TREE_CODE (pnode) = VEC_PERM_EXPR;
3394 92 : SLP_TREE_VECTYPE (pnode) = vectype;
3395 92 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3396 92 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3397 92 : SLP_TREE_REPRESENTATIVE (pnode) = NULL;
3398 92 : lane_permutation_t& perm = SLP_TREE_LANE_PERMUTATION (pnode);
3399 92 : children.safe_push (pnode);
3400 :
3401 808 : for (unsigned j = 0; j < stmts.length (); j++)
3402 624 : perm.safe_push (std::make_pair (0, two_op_perm_indices[i][j]));
3403 : }
3404 :
3405 46 : SLP_TREE_REF_COUNT (child) += 4;
3406 : }
3407 :
3408 9356 : slp_tree one = new _slp_tree;
3409 9356 : slp_tree two = new _slp_tree;
3410 9356 : SLP_TREE_DEF_TYPE (one) = vect_internal_def;
3411 9356 : SLP_TREE_DEF_TYPE (two) = vect_internal_def;
3412 9356 : SLP_TREE_VECTYPE (one) = vectype;
3413 9356 : SLP_TREE_VECTYPE (two) = vectype;
3414 9356 : SLP_TREE_CHILDREN (one).safe_splice (children);
3415 9356 : SLP_TREE_CHILDREN (two).safe_splice (children);
3416 9356 : slp_tree child;
3417 37426 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two), i, child)
3418 18714 : SLP_TREE_REF_COUNT (child)++;
3419 :
3420 : /* Here we record the original defs since this
3421 : node represents the final lane configuration. */
3422 9356 : node = vect_create_new_slp_node (node, stmts, 2);
3423 9356 : SLP_TREE_VECTYPE (node) = vectype;
3424 9356 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
3425 9356 : SLP_TREE_CHILDREN (node).quick_push (one);
3426 9356 : SLP_TREE_CHILDREN (node).quick_push (two);
3427 9356 : SLP_TREE_REPRESENTATIVE (node) = NULL;
3428 9356 : enum tree_code code0 = ERROR_MARK;
3429 9356 : if (gassign *stmt = dyn_cast <gassign *> (stmts[0]->stmt))
3430 9354 : code0 = gimple_assign_rhs_code (stmt);
3431 9356 : stmt_vec_info ostmt_info;
3432 9356 : unsigned j = 0;
3433 32437 : FOR_EACH_VEC_ELT (stmts, i, ostmt_info)
3434 : {
3435 23081 : int op = 0;
3436 23081 : gassign *ostmt = dyn_cast<gassign *> (ostmt_info->stmt);
3437 23077 : if ((ostmt && gimple_assign_rhs_code (ostmt) != code0)
3438 : || (!ostmt
3439 8 : && gimple_call_combined_fn (stmts[0]->stmt)
3440 4 : != gimple_call_combined_fn (ostmt_info->stmt)))
3441 : {
3442 : op = 1;
3443 : j = i;
3444 : }
3445 23081 : SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (op, i));
3446 : }
3447 9356 : SLP_TREE_LANES (one) = stmts.length ();
3448 9356 : SLP_TREE_LANES (two) = stmts.length ();
3449 9356 : SLP_TREE_REPRESENTATIVE (one) = stmts[0];
3450 9356 : SLP_TREE_REPRESENTATIVE (two) = stmts[j];
3451 :
3452 9356 : return node;
3453 : }
3454 :
3455 3072443 : node = vect_create_new_slp_node (node, stmts, nops);
3456 3072443 : SLP_TREE_VECTYPE (node) = vectype;
3457 3072443 : SLP_TREE_CHILDREN (node).splice (children);
3458 3072443 : SLP_TREE_GS_SCALE (node) = gs_scale;
3459 3072443 : SLP_TREE_GS_BASE (node) = gs_base;
3460 3072443 : if (reduc_idx != -1)
3461 : {
3462 118796 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) != -1
3463 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
3464 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def);
3465 118796 : SLP_TREE_REDUC_IDX (node) = reduc_idx;
3466 118796 : node->cycle_info.id = SLP_TREE_CHILDREN (node)[reduc_idx]->cycle_info.id;
3467 : }
3468 : /* When reaching the reduction PHI, create a vect_reduc_info. */
3469 2953647 : else if ((STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def
3470 2953647 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3471 2953647 : && is_a <gphi *> (STMT_VINFO_STMT (stmt_info)))
3472 : {
3473 103946 : loop_vec_info loop_vinfo = as_a <loop_vec_info> (vinfo);
3474 103946 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) == -1);
3475 103946 : node->cycle_info.id = loop_vinfo->reduc_infos.length ();
3476 103946 : vect_reduc_info reduc_info = new vect_reduc_info_s ();
3477 103946 : loop_vinfo->reduc_infos.safe_push (reduc_info);
3478 103946 : stmt_vec_info reduc_phi = stmt_info;
3479 : /* ??? For double reductions vect_is_simple_reduction stores the
3480 : reduction type and code on the inner loop header PHI. */
3481 103946 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3482 : {
3483 392 : use_operand_p use_p;
3484 392 : gimple *use_stmt;
3485 392 : bool res = single_imm_use (gimple_phi_result (stmt_info->stmt),
3486 : &use_p, &use_stmt);
3487 392 : gcc_assert (res);
3488 392 : reduc_phi = loop_vinfo->lookup_stmt (use_stmt);
3489 : }
3490 103946 : VECT_REDUC_INFO_DEF_TYPE (reduc_info) = STMT_VINFO_DEF_TYPE (stmt_info);
3491 103946 : VECT_REDUC_INFO_TYPE (reduc_info) = STMT_VINFO_REDUC_TYPE (reduc_phi);
3492 103946 : VECT_REDUC_INFO_CODE (reduc_info) = STMT_VINFO_REDUC_CODE (reduc_phi);
3493 103946 : VECT_REDUC_INFO_FN (reduc_info) = IFN_LAST;
3494 : }
3495 : return node;
3496 10371051 : }
3497 :
3498 : /* Dump a single SLP tree NODE. */
3499 :
3500 : static void
3501 503727 : vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
3502 : slp_tree node)
3503 : {
3504 503727 : unsigned i, j;
3505 503727 : slp_tree child;
3506 503727 : stmt_vec_info stmt_info;
3507 503727 : tree op;
3508 :
3509 503727 : dump_metadata_t metadata (dump_kind, loc.get_impl_location ());
3510 503727 : dump_user_location_t user_loc = loc.get_user_location ();
3511 503727 : dump_printf_loc (metadata, user_loc,
3512 : "node%s %p (refcnt=%u)",
3513 503727 : SLP_TREE_DEF_TYPE (node) == vect_external_def
3514 : ? " (external)"
3515 : : (SLP_TREE_DEF_TYPE (node) == vect_constant_def
3516 486409 : ? " (constant)"
3517 : : ""), (void *) node,
3518 : SLP_TREE_REF_COUNT (node));
3519 503727 : if (SLP_TREE_VECTYPE (node))
3520 423996 : dump_printf (metadata, " %T", SLP_TREE_VECTYPE (node));
3521 503727 : dump_printf (metadata, "%s",
3522 503727 : node->avoid_stlf_fail ? " (avoid-stlf-fail)" : "");
3523 503727 : if (node->cycle_info.id != -1 || node->cycle_info.reduc_idx != -1)
3524 25407 : dump_printf (metadata, " cycle %d, link %d", node->cycle_info.id,
3525 : node->cycle_info.reduc_idx);
3526 503727 : if (node->si)
3527 5628 : dump_printf (metadata, " @%G", node->si);
3528 : else
3529 498099 : dump_printf (metadata, "\n");
3530 503727 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
3531 : {
3532 407418 : if (SLP_TREE_PERMUTE_P (node))
3533 17520 : dump_printf_loc (metadata, user_loc, "op: VEC_PERM_EXPR\n");
3534 : else
3535 389898 : dump_printf_loc (metadata, user_loc, "op template: %G",
3536 389898 : SLP_TREE_REPRESENTATIVE (node)->stmt);
3537 : }
3538 503727 : if (SLP_TREE_SCALAR_STMTS (node).exists ())
3539 973010 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3540 574714 : if (stmt_info)
3541 568529 : dump_printf_loc (metadata, user_loc, "\t%sstmt %u %G",
3542 568529 : SLP_TREE_LIVE_LANES (node).contains (i)
3543 564621 : ? "[l*]" : (STMT_VINFO_LIVE_P (stmt_info)
3544 564621 : ? "[l] " : ""),
3545 : i, stmt_info->stmt);
3546 : else
3547 6185 : dump_printf_loc (metadata, user_loc, "\tstmt %u ---\n", i);
3548 : else
3549 : {
3550 105431 : dump_printf_loc (metadata, user_loc, "\t{ ");
3551 233874 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
3552 128443 : dump_printf (metadata, "%T%s ", op,
3553 128443 : i < SLP_TREE_SCALAR_OPS (node).length () - 1 ? "," : "");
3554 105431 : dump_printf (metadata, "}\n");
3555 : }
3556 503727 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
3557 : {
3558 70818 : dump_printf_loc (metadata, user_loc, "\tload permutation {");
3559 160757 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), i, j)
3560 89939 : dump_printf (dump_kind, " %u", j);
3561 70818 : dump_printf (dump_kind, " }\n");
3562 : }
3563 503727 : if (SLP_TREE_LANE_PERMUTATION (node).exists ())
3564 : {
3565 17528 : dump_printf_loc (metadata, user_loc, "\tlane permutation {");
3566 65787 : for (i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
3567 48259 : dump_printf (dump_kind, " %u[%u]",
3568 48259 : SLP_TREE_LANE_PERMUTATION (node)[i].first,
3569 48259 : SLP_TREE_LANE_PERMUTATION (node)[i].second);
3570 17528 : dump_printf (dump_kind, " }%s\n",
3571 17528 : node->ldst_lanes ? " (load-lanes)" : "");
3572 : }
3573 503727 : if (SLP_TREE_CHILDREN (node).is_empty ())
3574 190204 : return;
3575 313523 : dump_printf_loc (metadata, user_loc, "\tchildren");
3576 827433 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3577 513910 : dump_printf (dump_kind, " %p", (void *)child);
3578 313523 : dump_printf (dump_kind, "%s\n",
3579 313523 : node->ldst_lanes && !SLP_TREE_LANE_PERMUTATION (node).exists ()
3580 : ? " (store-lanes)" : "");
3581 : }
3582 :
3583 : DEBUG_FUNCTION void
3584 0 : debug (slp_tree node)
3585 : {
3586 0 : debug_dump_context ctx;
3587 0 : vect_print_slp_tree (MSG_NOTE,
3588 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3589 : node);
3590 0 : }
3591 :
3592 : /* Recursive helper for the dot producer below. */
3593 :
3594 : static void
3595 0 : dot_slp_tree (FILE *f, slp_tree node, hash_set<slp_tree> &visited)
3596 : {
3597 0 : if (visited.add (node))
3598 : return;
3599 :
3600 0 : fprintf (f, "\"%p\" [label=\"", (void *)node);
3601 0 : vect_print_slp_tree (MSG_NOTE,
3602 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3603 : node);
3604 0 : fprintf (f, "\"];\n");
3605 :
3606 :
3607 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3608 0 : fprintf (f, "\"%p\" -> \"%p\";", (void *)node, (void *)child);
3609 :
3610 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3611 0 : if (child)
3612 0 : dot_slp_tree (f, child, visited);
3613 : }
3614 :
3615 : DEBUG_FUNCTION void
3616 0 : dot_slp_tree (const char *fname, slp_tree node)
3617 : {
3618 0 : FILE *f = fopen (fname, "w");
3619 0 : fprintf (f, "digraph {\n");
3620 0 : fflush (f);
3621 0 : {
3622 0 : debug_dump_context ctx (f);
3623 0 : hash_set<slp_tree> visited;
3624 0 : dot_slp_tree (f, node, visited);
3625 0 : }
3626 0 : fflush (f);
3627 0 : fprintf (f, "}\n");
3628 0 : fclose (f);
3629 0 : }
3630 :
3631 : DEBUG_FUNCTION void
3632 0 : dot_slp_tree (const char *fname, const vec<slp_instance> &slp_instances)
3633 : {
3634 0 : FILE *f = fopen (fname, "w");
3635 0 : fprintf (f, "digraph {\n");
3636 0 : fflush (f);
3637 0 : {
3638 0 : debug_dump_context ctx (f);
3639 0 : hash_set<slp_tree> visited;
3640 0 : for (auto inst : slp_instances)
3641 0 : dot_slp_tree (f, SLP_INSTANCE_TREE (inst), visited);
3642 0 : }
3643 0 : fflush (f);
3644 0 : fprintf (f, "}\n");
3645 0 : fclose (f);
3646 0 : }
3647 :
3648 : /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
3649 :
3650 : static void
3651 543974 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3652 : slp_tree node, hash_set<slp_tree> &visited)
3653 : {
3654 543974 : unsigned i;
3655 543974 : slp_tree child;
3656 :
3657 543974 : if (visited.add (node))
3658 543974 : return;
3659 :
3660 499868 : vect_print_slp_tree (dump_kind, loc, node);
3661 :
3662 1508479 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3663 508743 : if (child)
3664 459706 : vect_print_slp_graph (dump_kind, loc, child, visited);
3665 : }
3666 :
3667 : static void
3668 51436 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3669 : slp_tree entry)
3670 : {
3671 51436 : hash_set<slp_tree> visited;
3672 51436 : vect_print_slp_graph (dump_kind, loc, entry, visited);
3673 51436 : }
3674 :
3675 : DEBUG_FUNCTION void
3676 0 : debug (slp_instance instance)
3677 : {
3678 0 : debug_dump_context ctx;
3679 0 : vect_print_slp_graph (MSG_NOTE,
3680 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3681 : SLP_INSTANCE_TREE (instance));
3682 0 : }
3683 :
3684 :
3685 : /* Compute the set of scalar stmts participating in external nodes. */
3686 :
3687 : static void
3688 1700855 : vect_slp_gather_extern_scalar_stmts (vec_info *vinfo, slp_tree node,
3689 : hash_set<slp_tree> &visited,
3690 : hash_set<stmt_vec_info> &estmts)
3691 : {
3692 1700855 : if (visited.add (node))
3693 : return;
3694 :
3695 1639750 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
3696 : {
3697 : slp_tree child;
3698 : int i;
3699 1931503 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3700 985528 : if (child)
3701 985528 : vect_slp_gather_extern_scalar_stmts (vinfo, child, visited, estmts);
3702 : }
3703 : else
3704 3881429 : for (tree def : SLP_TREE_SCALAR_OPS (node))
3705 : {
3706 1802362 : stmt_vec_info def_stmt = vinfo->lookup_def (def);
3707 1802362 : if (def_stmt)
3708 358230 : estmts.add (def_stmt);
3709 : }
3710 : }
3711 :
3712 : /* Mark the original scalar stmt coverage of the vector SLP graph of VINFO
3713 : with STMT_SLP_TYPE == pure_slp. */
3714 :
3715 : static void
3716 246787 : vect_bb_slp_mark_stmts_vectorized (bb_vec_info vinfo)
3717 : {
3718 : /* Gather the scalar stmt leafs of the SLP graph to stop the below DFS
3719 : walk on. */
3720 246787 : hash_set<stmt_vec_info> scalar_stmts_in_externs;
3721 246787 : hash_set<slp_tree> visited;
3722 1455688 : for (auto instance : BB_VINFO_SLP_INSTANCES (vinfo))
3723 715327 : vect_slp_gather_extern_scalar_stmts (vinfo, SLP_INSTANCE_TREE (instance),
3724 : visited, scalar_stmts_in_externs);
3725 :
3726 : /* DFS walk scalar stmts to compute the vectorized coverage indicated
3727 : by STMT_SLP_TYPE (stmt) == pure_slp on the original scalar (non-pattern)
3728 : stmts. */
3729 1455688 : for (auto instance : BB_VINFO_SLP_INSTANCES (vinfo))
3730 : {
3731 925004 : for (auto stmt : SLP_INSTANCE_ROOT_STMTS (instance))
3732 96989 : if (!scalar_stmts_in_externs.contains (stmt))
3733 96061 : STMT_SLP_TYPE (stmt) = pure_slp;
3734 715327 : auto_vec<stmt_vec_info> worklist;
3735 4030917 : for (auto stmt : SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance)))
3736 : {
3737 1884936 : stmt = vect_orig_stmt (stmt);
3738 1884936 : if (!scalar_stmts_in_externs.contains (stmt)
3739 1884936 : && STMT_SLP_TYPE (stmt) != pure_slp)
3740 : {
3741 1868097 : STMT_SLP_TYPE (stmt) = pure_slp;
3742 1868097 : worklist.safe_push (stmt);
3743 : }
3744 : }
3745 3778165 : while (!worklist.is_empty ())
3746 : {
3747 2352431 : stmt_vec_info stmt = worklist.pop ();
3748 :
3749 : /* Now walk relevant parts of the SSA use-def graph. */
3750 2352431 : slp_oprnds child_ops (stmt);
3751 7343792 : for (unsigned i = 0; i < child_ops.num_slp_children; ++i)
3752 : {
3753 2638930 : tree op = child_ops.get_op_for_slp_child (stmt, i);
3754 2638930 : stmt_vec_info def = vinfo->lookup_def (op);
3755 2638930 : if (def
3756 927082 : && !scalar_stmts_in_externs.contains (def)
3757 3219463 : && STMT_SLP_TYPE (def) != pure_slp)
3758 : {
3759 484334 : STMT_SLP_TYPE (def) = pure_slp;
3760 484334 : worklist.safe_push (def);
3761 : }
3762 : }
3763 : }
3764 715327 : }
3765 246787 : }
3766 :
3767 : /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
3768 :
3769 : static void
3770 2585975 : vect_mark_slp_stmts_relevant (slp_tree node, hash_set<slp_tree> &visited)
3771 : {
3772 2585975 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3773 : return;
3774 :
3775 1559214 : if (visited.add (node))
3776 : return;
3777 :
3778 7277861 : for (auto stmt_info : SLP_TREE_SCALAR_STMTS (node))
3779 3245890 : if (stmt_info)
3780 : {
3781 3245890 : gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
3782 : || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
3783 3245890 : STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
3784 : }
3785 :
3786 5627204 : for (auto child: SLP_TREE_CHILDREN (node))
3787 1766543 : if (child)
3788 1766543 : vect_mark_slp_stmts_relevant (child, visited);
3789 : }
3790 :
3791 : static void
3792 819432 : vect_mark_slp_stmts_relevant (slp_tree node)
3793 : {
3794 819432 : hash_set<slp_tree> visited;
3795 819432 : vect_mark_slp_stmts_relevant (node, visited);
3796 819432 : }
3797 :
3798 :
3799 : /* Gather loads in the SLP graph NODE and populate the INST loads array. */
3800 :
3801 : static void
3802 11428574 : vect_gather_slp_loads (vec<slp_tree> &loads, slp_tree node,
3803 : hash_set<slp_tree> &visited)
3804 : {
3805 11428574 : if (!node || visited.add (node))
3806 : return;
3807 :
3808 9555678 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3809 : return;
3810 :
3811 7098898 : if (!SLP_TREE_PERMUTE_P (node))
3812 : {
3813 6837737 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
3814 6837737 : if (STMT_VINFO_DATA_REF (stmt_info)
3815 2928828 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
3816 1683371 : loads.safe_push (node);
3817 : }
3818 :
3819 7098898 : unsigned i;
3820 7098898 : slp_tree child;
3821 16152650 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3822 9053752 : vect_gather_slp_loads (loads, child, visited);
3823 : }
3824 :
3825 :
3826 : /* Find the last store in SLP INSTANCE. */
3827 :
3828 : stmt_vec_info
3829 2540411 : vect_find_last_scalar_stmt_in_slp (slp_tree node)
3830 : {
3831 2540411 : stmt_vec_info last = NULL;
3832 14376050 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (node))
3833 6754817 : if (stmt_vinfo)
3834 : {
3835 6754817 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3836 6754817 : last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
3837 : }
3838 :
3839 2540411 : return last;
3840 : }
3841 :
3842 : /* Find the first stmt in NODE. */
3843 :
3844 : stmt_vec_info
3845 676577 : vect_find_first_scalar_stmt_in_slp (slp_tree node)
3846 : {
3847 676577 : stmt_vec_info first = NULL;
3848 :
3849 3655752 : for (auto stmt_vinfo : SLP_TREE_SCALAR_STMTS (node))
3850 1626021 : if (stmt_vinfo)
3851 : {
3852 1623424 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3853 1623424 : if (!first
3854 1623424 : || get_later_stmt (stmt_vinfo, first) == first)
3855 : first = stmt_vinfo;
3856 : }
3857 :
3858 676577 : return first;
3859 : }
3860 :
3861 : /* Splits a group of stores, currently beginning at FIRST_VINFO, into
3862 : two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
3863 : (also containing the first GROUP1_SIZE stmts, since stores are
3864 : consecutive), the second containing the remainder.
3865 : Return the first stmt in the second group. */
3866 :
3867 : static stmt_vec_info
3868 123365 : vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
3869 : {
3870 123365 : gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
3871 123365 : gcc_assert (group1_size > 0);
3872 123365 : int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
3873 123365 : gcc_assert (group2_size > 0);
3874 123365 : DR_GROUP_SIZE (first_vinfo) = group1_size;
3875 :
3876 123365 : stmt_vec_info stmt_info = first_vinfo;
3877 423891 : for (unsigned i = group1_size; i > 1; i--)
3878 : {
3879 300526 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
3880 300526 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3881 : }
3882 : /* STMT is now the last element of the first group. */
3883 123365 : stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
3884 123365 : DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
3885 :
3886 123365 : DR_GROUP_SIZE (group2) = group2_size;
3887 383676 : for (stmt_info = group2; stmt_info;
3888 260311 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
3889 : {
3890 260311 : DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
3891 260311 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3892 : }
3893 :
3894 : /* For the second group, the DR_GROUP_GAP is that before the original group,
3895 : plus skipping over the first vector. */
3896 123365 : DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
3897 :
3898 : /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
3899 123365 : DR_GROUP_GAP (first_vinfo) += group2_size;
3900 :
3901 123365 : if (dump_enabled_p ())
3902 106 : dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
3903 : group1_size, group2_size);
3904 :
3905 123365 : return group2;
3906 : }
3907 :
3908 : /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
3909 : statements and a vector of NUNITS elements. */
3910 :
3911 : static poly_uint64
3912 2986788 : calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
3913 : {
3914 2986788 : return exact_div (common_multiple (nunits, group_size), group_size);
3915 : }
3916 :
3917 : /* Helper that checks to see if a node is a load node. */
3918 :
3919 : static inline bool
3920 103 : vect_is_slp_load_node (slp_tree root)
3921 : {
3922 103 : return (!SLP_TREE_PERMUTE_P (root)
3923 103 : && SLP_TREE_DEF_TYPE (root) == vect_internal_def
3924 97 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root))
3925 167 : && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))));
3926 : }
3927 :
3928 :
3929 : /* Helper function of optimize_load_redistribution that performs the operation
3930 : recursively. */
3931 :
3932 : static slp_tree
3933 21715 : optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t *bst_map,
3934 : vec_info *vinfo, unsigned int group_size,
3935 : hash_map<slp_tree, slp_tree> *load_map,
3936 : slp_tree root)
3937 : {
3938 21715 : if (slp_tree *leader = load_map->get (root))
3939 4547 : return *leader;
3940 :
3941 17168 : slp_tree node;
3942 17168 : unsigned i;
3943 :
3944 : /* For now, we don't know anything about externals so do not do anything. */
3945 17168 : if (!root || SLP_TREE_DEF_TYPE (root) != vect_internal_def)
3946 : return NULL;
3947 12111 : else if (SLP_TREE_PERMUTE_P (root))
3948 : {
3949 : /* First convert this node into a load node and add it to the leaves
3950 : list and flatten the permute from a lane to a load one. If it's
3951 : unneeded it will be elided later. */
3952 71 : vec<stmt_vec_info> stmts;
3953 71 : stmts.create (SLP_TREE_LANES (root));
3954 71 : lane_permutation_t lane_perm = SLP_TREE_LANE_PERMUTATION (root);
3955 135 : for (unsigned j = 0; j < lane_perm.length (); j++)
3956 : {
3957 103 : std::pair<unsigned, unsigned> perm = lane_perm[j];
3958 103 : node = SLP_TREE_CHILDREN (root)[perm.first];
3959 :
3960 103 : if (!vect_is_slp_load_node (node)
3961 103 : || SLP_TREE_CHILDREN (node).exists ())
3962 : {
3963 39 : stmts.release ();
3964 39 : goto next;
3965 : }
3966 :
3967 64 : stmts.quick_push (SLP_TREE_SCALAR_STMTS (node)[perm.second]);
3968 : }
3969 :
3970 32 : if (dump_enabled_p ())
3971 0 : dump_printf_loc (MSG_NOTE, vect_location,
3972 : "converting stmts on permute node %p\n",
3973 : (void *) root);
3974 :
3975 32 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
3976 32 : unsigned tree_size = 0, limit = 1;
3977 32 : node = vect_build_slp_tree (vinfo, stmts,
3978 : matches, &limit, &tree_size, bst_map);
3979 32 : if (!node)
3980 0 : stmts.release ();
3981 :
3982 32 : load_map->put (root, node);
3983 32 : return node;
3984 : }
3985 :
3986 12040 : next:
3987 12079 : load_map->put (root, NULL);
3988 :
3989 27514 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3990 : {
3991 15435 : slp_tree value
3992 15435 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3993 : node);
3994 15435 : if (value)
3995 : {
3996 32 : SLP_TREE_REF_COUNT (value)++;
3997 32 : SLP_TREE_CHILDREN (root)[i] = value;
3998 : /* ??? We know the original leafs of the replaced nodes will
3999 : be referenced by bst_map, only the permutes created by
4000 : pattern matching are not. */
4001 32 : if (SLP_TREE_REF_COUNT (node) == 1)
4002 32 : load_map->remove (node);
4003 32 : vect_free_slp_tree (node);
4004 : }
4005 : }
4006 :
4007 : return NULL;
4008 : }
4009 :
4010 : /* Temporary workaround for loads not being CSEd during SLP build. This
4011 : function will traverse the SLP tree rooted in ROOT for INSTANCE and find
4012 : VEC_PERM nodes that blend vectors from multiple nodes that all read from the
4013 : same DR such that the final operation is equal to a permuted load. Such
4014 : NODES are then directly converted into LOADS themselves. The nodes are
4015 : CSEd using BST_MAP. */
4016 :
4017 : static void
4018 4138 : optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t *bst_map,
4019 : vec_info *vinfo, unsigned int group_size,
4020 : hash_map<slp_tree, slp_tree> *load_map,
4021 : slp_tree root)
4022 : {
4023 4138 : slp_tree node;
4024 4138 : unsigned i;
4025 :
4026 10418 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
4027 : {
4028 6280 : slp_tree value
4029 6280 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
4030 : node);
4031 6280 : if (value)
4032 : {
4033 0 : SLP_TREE_REF_COUNT (value)++;
4034 0 : SLP_TREE_CHILDREN (root)[i] = value;
4035 : /* ??? We know the original leafs of the replaced nodes will
4036 : be referenced by bst_map, only the permutes created by
4037 : pattern matching are not. */
4038 0 : if (SLP_TREE_REF_COUNT (node) == 1)
4039 0 : load_map->remove (node);
4040 0 : vect_free_slp_tree (node);
4041 : }
4042 : }
4043 4138 : }
4044 :
4045 : /* Helper function of vect_match_slp_patterns.
4046 :
4047 : Attempts to match patterns against the slp tree rooted in REF_NODE using
4048 : VINFO. Patterns are matched in post-order traversal.
4049 :
4050 : If matching is successful the value in REF_NODE is updated and returned, if
4051 : not then it is returned unchanged. */
4052 :
4053 : static bool
4054 6590528 : vect_match_slp_patterns_2 (slp_tree *ref_node, vec_info *vinfo,
4055 : slp_tree_to_load_perm_map_t *perm_cache,
4056 : slp_compat_nodes_map_t *compat_cache,
4057 : hash_set<slp_tree> *visited)
4058 : {
4059 6590528 : unsigned i;
4060 6590528 : slp_tree node = *ref_node;
4061 6590528 : bool found_p = false;
4062 6590528 : if (!node || visited->add (node))
4063 : return false;
4064 :
4065 : slp_tree child;
4066 10583683 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
4067 4940940 : found_p |= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node)[i],
4068 : vinfo, perm_cache, compat_cache,
4069 : visited);
4070 :
4071 16928229 : for (unsigned x = 0; x < num__slp_patterns; x++)
4072 : {
4073 11285486 : vect_pattern *pattern
4074 11285486 : = slp_patterns[x] (perm_cache, compat_cache, ref_node);
4075 11285486 : if (pattern)
4076 : {
4077 1112 : pattern->build (vinfo);
4078 1112 : delete pattern;
4079 : found_p = true;
4080 : }
4081 : }
4082 :
4083 : return found_p;
4084 : }
4085 :
4086 : /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
4087 : vec_info VINFO.
4088 :
4089 : The modified tree is returned. Patterns are tried in order and multiple
4090 : patterns may match. */
4091 :
4092 : static bool
4093 1649588 : vect_match_slp_patterns (slp_instance instance, vec_info *vinfo,
4094 : hash_set<slp_tree> *visited,
4095 : slp_tree_to_load_perm_map_t *perm_cache,
4096 : slp_compat_nodes_map_t *compat_cache)
4097 : {
4098 1649588 : DUMP_VECT_SCOPE ("vect_match_slp_patterns");
4099 1649588 : slp_tree *ref_node = &SLP_INSTANCE_TREE (instance);
4100 :
4101 1649588 : if (dump_enabled_p ())
4102 34250 : dump_printf_loc (MSG_NOTE, vect_location,
4103 : "Analyzing SLP tree %p for patterns\n",
4104 34250 : (void *) SLP_INSTANCE_TREE (instance));
4105 :
4106 1649588 : return vect_match_slp_patterns_2 (ref_node, vinfo, perm_cache, compat_cache,
4107 1649588 : visited);
4108 : }
4109 :
4110 : /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
4111 : vectorizing with VECTYPE that might be NULL. MASKED_P indicates whether
4112 : the stores are masked.
4113 : Return true if we could use IFN_STORE_LANES instead and if that appears
4114 : to be the better approach. */
4115 :
4116 : static bool
4117 6169 : vect_slp_prefer_store_lanes_p (vec_info *vinfo, stmt_vec_info stmt_info,
4118 : tree vectype, bool masked_p,
4119 : unsigned int group_size,
4120 : unsigned int new_group_size)
4121 : {
4122 6169 : if (!vectype)
4123 : {
4124 6169 : tree scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
4125 6169 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
4126 : }
4127 6169 : if (!vectype)
4128 : return false;
4129 : /* Allow the split if one of the two new groups would operate on full
4130 : vectors *within* rather than across one scalar loop iteration.
4131 : This is purely a heuristic, but it should work well for group
4132 : sizes of 3 and 4, where the possible splits are:
4133 :
4134 : 3->2+1: OK if the vector has exactly two elements
4135 : 4->2+2: Likewise
4136 : 4->3+1: Less clear-cut. */
4137 6169 : if (multiple_p (group_size - new_group_size, TYPE_VECTOR_SUBPARTS (vectype))
4138 6169 : || multiple_p (new_group_size, TYPE_VECTOR_SUBPARTS (vectype)))
4139 : return false;
4140 3500 : return vect_store_lanes_supported (vectype, group_size, masked_p) != IFN_LAST;
4141 : }
4142 :
4143 : /* Analyze an SLP instance starting from a group of grouped stores. Call
4144 : vect_build_slp_tree to build a tree of packed stmts if possible.
4145 : Return FALSE if it's impossible to SLP any stmt in the loop. */
4146 :
4147 : static bool
4148 : vect_analyze_slp_instance (vec_info *vinfo,
4149 : scalar_stmts_to_slp_tree_map_t *bst_map,
4150 : stmt_vec_info stmt_info, slp_instance_kind kind,
4151 : unsigned max_tree_size, unsigned *limit,
4152 : bool force_single_lane);
4153 :
4154 : /* Build an interleaving scheme for the store sources RHS_NODES from
4155 : SCALAR_STMTS. */
4156 :
4157 : static slp_tree
4158 8299 : vect_build_slp_store_interleaving (vec<slp_tree> &rhs_nodes,
4159 : vec<stmt_vec_info> &scalar_stmts)
4160 : {
4161 8299 : unsigned int group_size = scalar_stmts.length ();
4162 16598 : slp_tree node = vect_create_new_slp_node (scalar_stmts,
4163 8299 : SLP_TREE_CHILDREN
4164 : (rhs_nodes[0]).length ());
4165 8299 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
4166 8299 : for (unsigned l = 0;
4167 16629 : l < SLP_TREE_CHILDREN (rhs_nodes[0]).length (); ++l)
4168 : {
4169 : /* And a permute merging all RHS SLP trees. */
4170 8330 : slp_tree perm = vect_create_new_slp_node (rhs_nodes.length (),
4171 8330 : VEC_PERM_EXPR);
4172 8330 : SLP_TREE_CHILDREN (node).quick_push (perm);
4173 8330 : SLP_TREE_LANE_PERMUTATION (perm).create (group_size);
4174 8330 : SLP_TREE_VECTYPE (perm) = SLP_TREE_VECTYPE (node);
4175 8330 : SLP_TREE_LANES (perm) = group_size;
4176 8330 : SLP_TREE_REPRESENTATIVE (perm) = NULL;
4177 65136 : for (unsigned j = 0; j < rhs_nodes.length (); ++j)
4178 : {
4179 24238 : SLP_TREE_CHILDREN (perm)
4180 24238 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[l]);
4181 24238 : SLP_TREE_CHILDREN (rhs_nodes[j])[l]->refcnt++;
4182 24238 : for (unsigned k = 0;
4183 50911 : k < SLP_TREE_LANES (rhs_nodes[j]); ++k)
4184 : {
4185 : /* ??? We should populate SLP_TREE_SCALAR_STMTS
4186 : or SLP_TREE_SCALAR_OPS but then we might have
4187 : a mix of both in our children. */
4188 26673 : SLP_TREE_LANE_PERMUTATION (perm)
4189 26673 : .quick_push (std::make_pair (j, k));
4190 : }
4191 : }
4192 :
4193 : /* Now we have a single permute node but we cannot code-generate
4194 : the case with more than two inputs.
4195 : Perform pairwise reduction, reducing the two inputs
4196 : with the least number of lanes to one and then repeat until
4197 : we end up with two inputs. That scheme makes sure we end
4198 : up with permutes satisfying the restriction of requiring at
4199 : most two vector inputs to produce a single vector output
4200 : when the number of lanes is even. */
4201 15908 : while (SLP_TREE_CHILDREN (perm).length () > 2)
4202 : {
4203 : /* When we have three equal sized groups left the pairwise
4204 : reduction does not result in a scheme that avoids using
4205 : three vectors. Instead merge the first two groups
4206 : to the final size with do-not-care elements (chosen
4207 : from the first group) and then merge with the third.
4208 : { A0, B0, x, A1, B1, x, ... }
4209 : -> { A0, B0, C0, A1, B1, C1, ... }
4210 : This handles group size of three (and at least
4211 : power-of-two multiples of that). */
4212 7578 : if (SLP_TREE_CHILDREN (perm).length () == 3
4213 3408 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
4214 3408 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[1]))
4215 7578 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
4216 2517 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[2])))
4217 : {
4218 2207 : int ai = 0;
4219 2207 : int bi = 1;
4220 2207 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
4221 2207 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
4222 2207 : unsigned n = SLP_TREE_LANES (perm);
4223 :
4224 2207 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
4225 2207 : SLP_TREE_LANES (permab) = n;
4226 2207 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
4227 2207 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
4228 : /* ??? Should be NULL but that's not expected. */
4229 2207 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
4230 2207 : SLP_TREE_CHILDREN (permab).quick_push (a);
4231 6635 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
4232 2221 : SLP_TREE_LANE_PERMUTATION (permab)
4233 2221 : .quick_push (std::make_pair (0, k));
4234 2207 : SLP_TREE_CHILDREN (permab).quick_push (b);
4235 6635 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
4236 2221 : SLP_TREE_LANE_PERMUTATION (permab)
4237 2221 : .quick_push (std::make_pair (1, k));
4238 : /* Push the do-not-care lanes. */
4239 4428 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
4240 2221 : SLP_TREE_LANE_PERMUTATION (permab)
4241 2221 : .quick_push (std::make_pair (0, k));
4242 :
4243 : /* Put the merged node into 'perm', in place of a. */
4244 2207 : SLP_TREE_CHILDREN (perm)[ai] = permab;
4245 : /* Adjust the references to b in the permutation
4246 : of perm and to the later children which we'll
4247 : remove. */
4248 8870 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
4249 : {
4250 6663 : std::pair<unsigned, unsigned> &p
4251 6663 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
4252 6663 : if (p.first == (unsigned) bi)
4253 : {
4254 2221 : p.first = ai;
4255 2221 : p.second += SLP_TREE_LANES (a);
4256 : }
4257 4442 : else if (p.first > (unsigned) bi)
4258 2221 : p.first--;
4259 : }
4260 2207 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
4261 2207 : break;
4262 : }
4263 :
4264 : /* Pick the two nodes with the least number of lanes,
4265 : prefer the earliest candidate and maintain ai < bi. */
4266 : int ai = -1;
4267 : int bi = -1;
4268 50576 : for (unsigned ci = 0; ci < SLP_TREE_CHILDREN (perm).length (); ++ci)
4269 : {
4270 45205 : if (ai == -1)
4271 5371 : ai = ci;
4272 39834 : else if (bi == -1)
4273 5371 : bi = ci;
4274 34463 : else if ((SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
4275 34463 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai]))
4276 34463 : || (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
4277 28625 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi])))
4278 : {
4279 12566 : if (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai])
4280 6283 : <= SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi]))
4281 2896 : bi = ci;
4282 : else
4283 : {
4284 3387 : ai = bi;
4285 3387 : bi = ci;
4286 : }
4287 : }
4288 : }
4289 :
4290 : /* Produce a merge of nodes ai and bi. */
4291 5371 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
4292 5371 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
4293 5371 : unsigned n = SLP_TREE_LANES (a) + SLP_TREE_LANES (b);
4294 5371 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
4295 5371 : SLP_TREE_LANES (permab) = n;
4296 5371 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
4297 5371 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
4298 : /* ??? Should be NULL but that's not expected. */
4299 5371 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
4300 5371 : SLP_TREE_CHILDREN (permab).quick_push (a);
4301 19645 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
4302 8903 : SLP_TREE_LANE_PERMUTATION (permab)
4303 8903 : .quick_push (std::make_pair (0, k));
4304 5371 : SLP_TREE_CHILDREN (permab).quick_push (b);
4305 18913 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
4306 8171 : SLP_TREE_LANE_PERMUTATION (permab)
4307 8171 : .quick_push (std::make_pair (1, k));
4308 :
4309 : /* Put the merged node into 'perm', in place of a. */
4310 5371 : SLP_TREE_CHILDREN (perm)[ai] = permab;
4311 : /* Adjust the references to b in the permutation
4312 : of perm and to the later children which we'll
4313 : remove. */
4314 81421 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
4315 : {
4316 76050 : std::pair<unsigned, unsigned> &p
4317 76050 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
4318 76050 : if (p.first == (unsigned) bi)
4319 : {
4320 8171 : p.first = ai;
4321 8171 : p.second += SLP_TREE_LANES (a);
4322 : }
4323 67879 : else if (p.first > (unsigned) bi)
4324 28722 : p.first--;
4325 : }
4326 5371 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
4327 : }
4328 : }
4329 :
4330 8299 : return node;
4331 : }
4332 :
4333 : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4334 : of KIND. Return true if successful. SCALAR_STMTS is owned by this
4335 : function, REMAIN and ROOT_STMT_INFOS ownership is transferred back to
4336 : the caller upon failure. */
4337 :
4338 : static bool
4339 2172885 : vect_build_slp_instance (vec_info *vinfo,
4340 : slp_instance_kind kind,
4341 : vec<stmt_vec_info> &scalar_stmts,
4342 : vec<stmt_vec_info> &root_stmt_infos,
4343 : vec<tree> &remain,
4344 : unsigned max_tree_size, unsigned *limit,
4345 : scalar_stmts_to_slp_tree_map_t *bst_map,
4346 : bool force_single_lane)
4347 : {
4348 : /* If there's no budget left bail out early. */
4349 2172885 : if (*limit == 0)
4350 : {
4351 15680 : scalar_stmts.release ();
4352 15680 : return false;
4353 : }
4354 :
4355 2157205 : if (kind == slp_inst_kind_ctor)
4356 : {
4357 16489 : if (dump_enabled_p ())
4358 102 : dump_printf_loc (MSG_NOTE, vect_location,
4359 : "Analyzing vectorizable constructor: %G\n",
4360 51 : root_stmt_infos[0]->stmt);
4361 : }
4362 2140716 : else if (kind == slp_inst_kind_gcond)
4363 : {
4364 301087 : if (dump_enabled_p ())
4365 6946 : dump_printf_loc (MSG_NOTE, vect_location,
4366 : "Analyzing vectorizable control flow: %G",
4367 3473 : root_stmt_infos[0]->stmt);
4368 : }
4369 1839629 : else if (kind == slp_inst_kind_bb_reduc)
4370 : {
4371 1432818 : if (dump_enabled_p ())
4372 7598 : dump_printf_loc (MSG_NOTE, vect_location,
4373 : "Analyzing vectorizable BB reduction: %G",
4374 3799 : root_stmt_infos[0]->stmt);
4375 : }
4376 :
4377 2157205 : if (dump_enabled_p ())
4378 : {
4379 29077 : dump_printf_loc (MSG_NOTE, vect_location,
4380 : "Starting SLP discovery for\n");
4381 62739 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4382 67324 : dump_printf_loc (MSG_NOTE, vect_location,
4383 33662 : " %G", scalar_stmts[i]->stmt);
4384 : }
4385 :
4386 : /* Build the tree for the SLP instance. */
4387 2157205 : unsigned int group_size = scalar_stmts.length ();
4388 2157205 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
4389 2157205 : unsigned tree_size = 0;
4390 :
4391 2157205 : slp_tree node = NULL;
4392 2157205 : if (group_size > 1 && force_single_lane)
4393 : {
4394 0 : matches[0] = 0;
4395 0 : matches[1] = -1;
4396 : }
4397 : else
4398 2157205 : node = vect_build_slp_tree (vinfo, scalar_stmts, matches, limit,
4399 : &tree_size, bst_map);
4400 2157205 : if (node != NULL)
4401 : {
4402 : /* Create a new SLP instance. */
4403 852435 : slp_instance new_instance = XNEW (class _slp_instance);
4404 852435 : SLP_INSTANCE_TREE (new_instance) = node;
4405 852435 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4406 852435 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4407 852435 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4408 852435 : SLP_INSTANCE_KIND (new_instance) = kind;
4409 852435 : new_instance->reduc_phis = NULL;
4410 852435 : new_instance->cost_vec = vNULL;
4411 852435 : new_instance->subgraph_entries = vNULL;
4412 :
4413 852435 : if (dump_enabled_p ())
4414 25710 : dump_printf_loc (MSG_NOTE, vect_location,
4415 : "SLP size %u vs. limit %u.\n",
4416 : tree_size, max_tree_size);
4417 :
4418 852435 : vinfo->slp_instances.safe_push (new_instance);
4419 :
4420 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4421 : the number of SLP lanes of the root in a few places.
4422 : Verify that assumption holds. */
4423 852435 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
4424 : == group_size);
4425 :
4426 852435 : if (dump_enabled_p ())
4427 : {
4428 25710 : if (kind == slp_inst_kind_reduc_group)
4429 1873 : dump_printf_loc (MSG_NOTE, vect_location,
4430 : "SLP discovery of size %d reduction group "
4431 : "succeeded\n", group_size);
4432 25710 : dump_printf_loc (MSG_NOTE, vect_location,
4433 : "Final SLP tree for instance %p:\n",
4434 : (void *) new_instance);
4435 25710 : vect_print_slp_graph (MSG_NOTE, vect_location,
4436 : SLP_INSTANCE_TREE (new_instance));
4437 : }
4438 :
4439 852435 : return true;
4440 : }
4441 : /* Failed to SLP. */
4442 :
4443 : /* While we arrive here even with slp_inst_kind_store we should only
4444 : for group_size == 1. The code to split store groups is only in
4445 : vect_analyze_slp_instance now. */
4446 1304770 : gcc_assert (kind != slp_inst_kind_store || group_size == 1);
4447 :
4448 : /* For BB vectorization we get failures only in case of the need of
4449 : unrolling, as otherwise we'll simply get operands built from scalars.
4450 : Iff there is any mismatches in the toplevel stmts those will prevail,
4451 : otherwise we get the non-power-of-two tail of the lanes failed.
4452 : For BB reductions we mainly want to catch the first case so we pick
4453 : a more useful subset of lanes to reduce. Pick the largest matching
4454 : subset of that covers half of the group or more. */
4455 1304770 : if (kind == slp_inst_kind_bb_reduc && matches[0] != -1)
4456 : {
4457 701622 : unsigned *n_matching = XALLOCAVEC (unsigned, group_size);
4458 701622 : memset (n_matching, 0, sizeof (unsigned) * group_size);
4459 2264556 : for (unsigned i = 0; i < group_size; ++i)
4460 1562934 : if (matches[i] != -2 && matches[i] != -1)
4461 1319421 : n_matching[matches[i].v]++;
4462 : unsigned largest_i = 0;
4463 1562934 : for (unsigned i = 1; i < group_size; ++i)
4464 861312 : if (n_matching[i] > n_matching[largest_i])
4465 66364 : largest_i = i;
4466 : /* Pick the largest matching part and put the rest to remain. */
4467 701622 : if (n_matching[largest_i] >= 2
4468 49777 : && n_matching[largest_i] >= group_size / 2)
4469 : {
4470 : /* As we know the matches[] stmts match up, recursing for
4471 : non-power-of-two sizes will just force-fail the tail
4472 : for us at hopefully optimal vector size and succesfully
4473 : finish discovery. */
4474 48371 : vec<stmt_vec_info> scalar_stmts2;
4475 48371 : scalar_stmts2.create (n_matching[largest_i]);
4476 284768 : for (unsigned i = 0; i < group_size; ++i)
4477 188026 : if (matches[i] == (int)largest_i)
4478 126982 : scalar_stmts2.quick_push (scalar_stmts[i]);
4479 : else
4480 61044 : remain.safe_push
4481 64239 : (gimple_get_lhs (vect_orig_stmt (scalar_stmts[i])->stmt));
4482 48371 : if (dump_enabled_p ())
4483 244 : dump_printf_loc (MSG_NOTE, vect_location, "Splitting %d "
4484 : "non-matching lanes to scalar remains\n",
4485 244 : scalar_stmts.length () - scalar_stmts2.length ());
4486 48371 : scalar_stmts.release ();
4487 48371 : return vect_build_slp_instance (vinfo, kind, scalar_stmts2,
4488 : root_stmt_infos, remain,
4489 : max_tree_size, limit, bst_map,
4490 : force_single_lane);
4491 : }
4492 : }
4493 :
4494 : /* Free the allocated memory. */
4495 1256399 : scalar_stmts.release ();
4496 :
4497 : /* Failed to SLP. */
4498 1256399 : if (dump_enabled_p ())
4499 3245 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4500 : return false;
4501 : }
4502 :
4503 : /* Analyze an SLP instance starting from a the start of a reduction chain.
4504 : Call vect_build_slp_tree to build a tree of packed stmts if possible.
4505 : Return FALSE if SLP build fails. */
4506 :
4507 : static bool
4508 72016 : vect_analyze_slp_reduc_chain (loop_vec_info vinfo,
4509 : scalar_stmts_to_slp_tree_map_t *bst_map,
4510 : stmt_vec_info scalar_stmt,
4511 : unsigned max_tree_size, unsigned *limit)
4512 : {
4513 72016 : vec<stmt_vec_info> scalar_stmts = vNULL;
4514 :
4515 72016 : bool fail = false;
4516 : /* ??? We could leave operation code checking to SLP discovery. */
4517 72016 : code_helper code = STMT_VINFO_REDUC_CODE (STMT_VINFO_REDUC_DEF
4518 : (vect_orig_stmt (scalar_stmt)));
4519 72016 : bool first = true;
4520 72016 : stmt_vec_info next_stmt = scalar_stmt;
4521 80565 : do
4522 : {
4523 80565 : stmt_vec_info stmt = next_stmt;
4524 80565 : gimple_match_op op, orig_op;
4525 80565 : if (!gimple_extract_op (STMT_VINFO_STMT (stmt), &op))
4526 0 : gcc_unreachable ();
4527 161130 : tree reduc_def = gimple_arg (STMT_VINFO_STMT (stmt),
4528 80565 : STMT_VINFO_REDUC_IDX (stmt));
4529 80565 : next_stmt = vect_stmt_to_vectorize (vinfo->lookup_def (reduc_def));
4530 80565 : gcc_assert (is_a <gphi *> (STMT_VINFO_STMT (next_stmt))
4531 : || STMT_VINFO_REDUC_IDX (next_stmt) != -1);
4532 86582 : if (!gimple_extract_op (STMT_VINFO_STMT (vect_orig_stmt (stmt)),
4533 : &orig_op))
4534 0 : gcc_unreachable ();
4535 80565 : if (CONVERT_EXPR_CODE_P (op.code)
4536 4999 : && tree_nop_conversion_p (op.type, TREE_TYPE (op.ops[0]))
4537 85552 : && (first
4538 2498 : || is_a <gphi *> (STMT_VINFO_STMT (next_stmt))))
4539 : ;
4540 75638 : else if (code != orig_op.code)
4541 : {
4542 895 : fail = true;
4543 895 : break;
4544 : }
4545 : else
4546 74743 : scalar_stmts.safe_push (stmt);
4547 79670 : first = false;
4548 : }
4549 79670 : while (!is_a <gphi *> (STMT_VINFO_STMT (next_stmt)));
4550 72016 : if (fail)
4551 : {
4552 895 : scalar_stmts.release ();
4553 895 : return false;
4554 : }
4555 :
4556 : /* When the SSA def chain through reduc-idx does not form a natural
4557 : reduction chain try to linearize an associative operation manually. */
4558 71121 : if (scalar_stmts.length () == 1
4559 70169 : && code.is_tree_code ()
4560 64097 : && associative_tree_code ((tree_code)code)
4561 : /* We may not associate if a fold-left reduction is required. */
4562 133773 : && !needs_fold_left_reduction_p (TREE_TYPE (gimple_get_lhs
4563 : (scalar_stmts[0]->stmt)),
4564 : code))
4565 : {
4566 : /* Remember a stmt with the actual reduction operation. */
4567 59054 : stmt_vec_info reduc_scalar_stmt = scalar_stmts[0];
4568 59054 : auto_vec<chain_op_t> chain;
4569 59054 : auto_vec<std::pair<tree_code, gimple *> > worklist;
4570 59054 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
4571 59054 : if (is_a <gassign *> (scalar_stmts[0]->stmt)
4572 : /* We cannot linearize an operation that vect_slp_linearize_chain
4573 : would not put on its worklist. */
4574 59054 : && gimple_assign_rhs_code (scalar_stmts[0]->stmt) == (tree_code)code)
4575 : {
4576 58350 : vect_slp_linearize_chain (vinfo, worklist, chain, (tree_code)code,
4577 58350 : scalar_stmts[0]->stmt, op_stmt,
4578 : other_op_stmt,
4579 : NULL);
4580 :
4581 58350 : scalar_stmts.truncate (0);
4582 58350 : stmt_vec_info tail = NULL;
4583 292022 : for (auto el : chain)
4584 : {
4585 117380 : if (el.dt == vect_external_def
4586 117380 : || el.dt == vect_constant_def
4587 117380 : || el.code != (tree_code) code)
4588 : {
4589 408 : scalar_stmts.release ();
4590 408 : return false;
4591 : }
4592 116972 : stmt_vec_info stmt = vinfo->lookup_def (el.op);
4593 116972 : if (STMT_VINFO_REDUC_IDX (stmt) != -1
4594 114686 : || STMT_VINFO_REDUC_DEF (stmt))
4595 : {
4596 58229 : gcc_assert (tail == NULL);
4597 58229 : tail = stmt;
4598 58229 : continue;
4599 : }
4600 58743 : scalar_stmts.safe_push (stmt);
4601 : }
4602 57942 : gcc_assert (tail);
4603 : }
4604 :
4605 : /* When this linearization didn't produce a chain see if stripping
4606 : a wrapping sign conversion produces one. */
4607 58646 : if (scalar_stmts.length () == 1
4608 58646 : && (code == PLUS_EXPR || code == MULT_EXPR || code == BIT_IOR_EXPR
4609 : || code == BIT_AND_EXPR || code == BIT_XOR_EXPR))
4610 : {
4611 56637 : auto stmt = dyn_cast<gassign *> (scalar_stmts[0]->stmt);
4612 55477 : if (!stmt
4613 55477 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
4614 4917 : || TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME
4615 4917 : || !tree_nop_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
4616 4917 : TREE_TYPE (gimple_assign_rhs1 (stmt))))
4617 : {
4618 54809 : scalar_stmts.release ();
4619 54809 : return false;
4620 : }
4621 1828 : stmt = dyn_cast<gassign *>
4622 1828 : (SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)));
4623 1808 : if (!stmt
4624 1808 : || gimple_assign_rhs_code (stmt) != (tree_code)code)
4625 : {
4626 1786 : scalar_stmts.release ();
4627 1786 : return false;
4628 : }
4629 42 : chain.truncate (0);
4630 42 : vect_slp_linearize_chain (vinfo, worklist, chain, (tree_code)code,
4631 : stmt, op_stmt, other_op_stmt, NULL);
4632 :
4633 42 : scalar_stmts.truncate (0);
4634 42 : stmt_vec_info tail = NULL;
4635 184 : for (auto el : chain)
4636 : {
4637 90 : if (el.dt == vect_external_def
4638 90 : || el.dt == vect_constant_def
4639 90 : || el.code != (tree_code) code)
4640 : {
4641 32 : scalar_stmts.release ();
4642 32 : return false;
4643 : }
4644 58 : stmt_vec_info stmt = vinfo->lookup_def (el.op);
4645 58 : if (STMT_VINFO_REDUC_IDX (stmt) != -1
4646 58 : || STMT_VINFO_REDUC_DEF (stmt))
4647 : {
4648 0 : gcc_assert (tail == NULL);
4649 0 : tail = stmt;
4650 0 : continue;
4651 : }
4652 58 : scalar_stmts.safe_push (stmt);
4653 : }
4654 : /* Unlike the above this does not include the reduction SSA
4655 : cycle. */
4656 10 : gcc_assert (!tail);
4657 : }
4658 :
4659 2019 : if (scalar_stmts.length () < 2)
4660 : {
4661 1866 : scalar_stmts.release ();
4662 1866 : return false;
4663 : }
4664 :
4665 153 : if (dump_enabled_p ())
4666 : {
4667 43 : dump_printf_loc (MSG_NOTE, vect_location,
4668 : "Starting SLP discovery of reduction chain for\n");
4669 173 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4670 260 : dump_printf_loc (MSG_NOTE, vect_location,
4671 130 : " %G", scalar_stmts[i]->stmt);
4672 : }
4673 :
4674 153 : unsigned int group_size = scalar_stmts.length ();
4675 153 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
4676 153 : unsigned tree_size = 0;
4677 153 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, matches, limit,
4678 153 : &tree_size, bst_map);
4679 153 : if (!node)
4680 : {
4681 66 : scalar_stmts.release ();
4682 66 : return false;
4683 : }
4684 :
4685 87 : unsigned cycle_id = vinfo->reduc_infos.length ();
4686 87 : vect_reduc_info reduc_info = new vect_reduc_info_s ();
4687 87 : vinfo->reduc_infos.safe_push (reduc_info);
4688 87 : VECT_REDUC_INFO_DEF_TYPE (reduc_info) = STMT_VINFO_DEF_TYPE (next_stmt);
4689 87 : VECT_REDUC_INFO_TYPE (reduc_info) = STMT_VINFO_REDUC_TYPE (next_stmt);
4690 87 : VECT_REDUC_INFO_CODE (reduc_info) = STMT_VINFO_REDUC_CODE (next_stmt);
4691 87 : VECT_REDUC_INFO_FN (reduc_info) = IFN_LAST;
4692 87 : reduc_info->is_reduc_chain = true;
4693 :
4694 : /* Build the node for the PHI and possibly the conversions. */
4695 87 : slp_tree phis = vect_create_new_slp_node (2, ERROR_MARK);
4696 87 : SLP_TREE_REPRESENTATIVE (phis) = next_stmt;
4697 87 : phis->cycle_info.id = cycle_id;
4698 87 : SLP_TREE_LANES (phis) = group_size;
4699 87 : if (reduc_scalar_stmt == scalar_stmt)
4700 83 : SLP_TREE_VECTYPE (phis) = SLP_TREE_VECTYPE (node);
4701 : else
4702 4 : SLP_TREE_VECTYPE (phis)
4703 4 : = signed_or_unsigned_type_for (TYPE_UNSIGNED
4704 : (TREE_TYPE (gimple_get_lhs
4705 : (scalar_stmt->stmt))),
4706 : SLP_TREE_VECTYPE (node));
4707 : /* ??? vect_cse_slp_nodes cannot cope with cycles without any
4708 : SLP_TREE_SCALAR_STMTS. */
4709 87 : SLP_TREE_SCALAR_STMTS (phis).create (group_size);
4710 513 : for (unsigned i = 0; i < group_size; ++i)
4711 339 : SLP_TREE_SCALAR_STMTS (phis).quick_push (next_stmt);
4712 :
4713 87 : slp_tree op_input = phis;
4714 87 : if (reduc_scalar_stmt != scalar_stmt)
4715 : {
4716 4 : slp_tree conv = vect_create_new_slp_node (1, ERROR_MARK);
4717 4 : SLP_TREE_REPRESENTATIVE (conv)
4718 4 : = vinfo->lookup_def (gimple_arg (reduc_scalar_stmt->stmt,
4719 4 : STMT_VINFO_REDUC_IDX
4720 : (reduc_scalar_stmt)));
4721 4 : SLP_TREE_CHILDREN (conv).quick_push (phis);
4722 4 : conv->cycle_info.id = cycle_id;
4723 4 : SLP_TREE_REDUC_IDX (conv) = 0;
4724 4 : SLP_TREE_LANES (conv) = group_size;
4725 4 : SLP_TREE_VECTYPE (conv) = SLP_TREE_VECTYPE (node);
4726 4 : SLP_TREE_SCALAR_STMTS (conv) = vNULL;
4727 4 : op_input = conv;
4728 : }
4729 :
4730 87 : slp_tree reduc = vect_create_new_slp_node (2, ERROR_MARK);
4731 87 : SLP_TREE_REPRESENTATIVE (reduc) = reduc_scalar_stmt;
4732 87 : SLP_TREE_CHILDREN (reduc).quick_push (op_input);
4733 87 : SLP_TREE_CHILDREN (reduc).quick_push (node);
4734 87 : reduc->cycle_info.id = cycle_id;
4735 87 : SLP_TREE_REDUC_IDX (reduc) = 0;
4736 87 : SLP_TREE_LANES (reduc) = group_size;
4737 87 : SLP_TREE_VECTYPE (reduc) = SLP_TREE_VECTYPE (node);
4738 : /* ??? For the reduction epilogue we need a live lane. */
4739 87 : SLP_TREE_SCALAR_STMTS (reduc).create (group_size);
4740 87 : SLP_TREE_SCALAR_STMTS (reduc).quick_push (reduc_scalar_stmt);
4741 426 : for (unsigned i = 1; i < group_size; ++i)
4742 252 : SLP_TREE_SCALAR_STMTS (reduc).quick_push (NULL);
4743 :
4744 87 : if (reduc_scalar_stmt != scalar_stmt)
4745 : {
4746 4 : slp_tree conv = vect_create_new_slp_node (1, ERROR_MARK);
4747 4 : SLP_TREE_REPRESENTATIVE (conv) = scalar_stmt;
4748 4 : SLP_TREE_CHILDREN (conv).quick_push (reduc);
4749 4 : conv->cycle_info.id = cycle_id;
4750 4 : SLP_TREE_REDUC_IDX (conv) = 0;
4751 4 : SLP_TREE_LANES (conv) = group_size;
4752 4 : SLP_TREE_VECTYPE (conv) = SLP_TREE_VECTYPE (phis);
4753 : /* ??? For the reduction epilogue we need a live lane. */
4754 4 : SLP_TREE_SCALAR_STMTS (conv).create (group_size);
4755 4 : SLP_TREE_SCALAR_STMTS (conv).quick_push (scalar_stmt);
4756 12 : for (unsigned i = 1; i < group_size; ++i)
4757 4 : SLP_TREE_SCALAR_STMTS (conv).quick_push (NULL);
4758 4 : reduc = conv;
4759 : }
4760 :
4761 87 : edge le = loop_latch_edge (LOOP_VINFO_LOOP (vinfo));
4762 87 : SLP_TREE_CHILDREN (phis).quick_push (NULL);
4763 87 : SLP_TREE_CHILDREN (phis).quick_push (NULL);
4764 87 : SLP_TREE_CHILDREN (phis)[le->dest_idx] = reduc;
4765 87 : SLP_TREE_REF_COUNT (reduc)++;
4766 :
4767 : /* Create a new SLP instance. */
4768 87 : slp_instance new_instance = XNEW (class _slp_instance);
4769 87 : SLP_INSTANCE_TREE (new_instance) = reduc;
4770 87 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4771 87 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4772 87 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4773 87 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_chain;
4774 87 : new_instance->reduc_phis = NULL;
4775 87 : new_instance->cost_vec = vNULL;
4776 87 : new_instance->subgraph_entries = vNULL;
4777 :
4778 87 : vinfo->slp_instances.safe_push (new_instance);
4779 :
4780 87 : if (dump_enabled_p ())
4781 : {
4782 33 : dump_printf_loc (MSG_NOTE, vect_location,
4783 : "Final SLP tree for instance %p:\n",
4784 : (void *) new_instance);
4785 33 : vect_print_slp_graph (MSG_NOTE, vect_location,
4786 : SLP_INSTANCE_TREE (new_instance));
4787 : }
4788 :
4789 : return true;
4790 59054 : }
4791 :
4792 12067 : if (scalar_stmts.length () <= 1)
4793 : {
4794 11121 : scalar_stmts.release ();
4795 11121 : return false;
4796 : }
4797 :
4798 946 : scalar_stmts.reverse ();
4799 946 : stmt_vec_info reduc_phi_info = next_stmt;
4800 :
4801 : /* Build the tree for the SLP instance. */
4802 946 : vec<stmt_vec_info> root_stmt_infos = vNULL;
4803 946 : vec<tree> remain = vNULL;
4804 :
4805 946 : if (dump_enabled_p ())
4806 : {
4807 255 : dump_printf_loc (MSG_NOTE, vect_location,
4808 : "Starting SLP discovery of reduction chain for\n");
4809 1267 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4810 2024 : dump_printf_loc (MSG_NOTE, vect_location,
4811 1012 : " %G", scalar_stmts[i]->stmt);
4812 : }
4813 :
4814 : /* Build the tree for the SLP instance. */
4815 946 : unsigned int group_size = scalar_stmts.length ();
4816 946 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
4817 946 : unsigned tree_size = 0;
4818 :
4819 : /* ??? We need this only for SLP discovery. */
4820 5438 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4821 4492 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = scalar_stmts[0];
4822 :
4823 946 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, matches, limit,
4824 946 : &tree_size, bst_map);
4825 :
4826 5438 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4827 4492 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = NULL;
4828 :
4829 946 : if (node != NULL)
4830 : {
4831 : /* Create a new SLP instance. */
4832 630 : slp_instance new_instance = XNEW (class _slp_instance);
4833 630 : SLP_INSTANCE_TREE (new_instance) = node;
4834 630 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4835 630 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4836 630 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4837 630 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_chain;
4838 630 : new_instance->reduc_phis = NULL;
4839 630 : new_instance->cost_vec = vNULL;
4840 630 : new_instance->subgraph_entries = vNULL;
4841 :
4842 630 : vect_reduc_info reduc_info = info_for_reduction (vinfo, node);
4843 630 : reduc_info->is_reduc_chain = true;
4844 :
4845 630 : if (dump_enabled_p ())
4846 156 : dump_printf_loc (MSG_NOTE, vect_location,
4847 : "SLP size %u vs. limit %u.\n",
4848 : tree_size, max_tree_size);
4849 :
4850 : /* Fixup SLP reduction chains. If this is a reduction chain with
4851 : a conversion in front amend the SLP tree with a node for that. */
4852 630 : gimple *scalar_def = STMT_VINFO_REDUC_DEF (reduc_phi_info)->stmt;
4853 630 : auto a_scalar_def = dyn_cast<gassign *> (scalar_def);
4854 630 : if (a_scalar_def
4855 630 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (a_scalar_def)))
4856 : {
4857 43 : stmt_vec_info conv_info = vect_stmt_to_vectorize
4858 43 : (STMT_VINFO_REDUC_DEF (reduc_phi_info));
4859 43 : scalar_stmts = vNULL;
4860 43 : scalar_stmts.create (group_size);
4861 178 : for (unsigned i = 0; i < group_size; ++i)
4862 92 : scalar_stmts.quick_push (conv_info);
4863 43 : slp_tree conv = vect_create_new_slp_node (scalar_stmts, 1);
4864 43 : SLP_TREE_VECTYPE (conv)
4865 43 : = get_vectype_for_scalar_type (vinfo,
4866 43 : TREE_TYPE
4867 : (gimple_assign_lhs (a_scalar_def)),
4868 : group_size);
4869 43 : SLP_TREE_REDUC_IDX (conv) = 0;
4870 43 : conv->cycle_info.id = node->cycle_info.id;
4871 43 : SLP_TREE_CHILDREN (conv).quick_push (node);
4872 43 : SLP_INSTANCE_TREE (new_instance) = conv;
4873 : }
4874 : /* Fill the backedge child of the PHI SLP node. The
4875 : general matching code cannot find it because the
4876 : scalar code does not reflect how we vectorize the
4877 : reduction. */
4878 630 : use_operand_p use_p;
4879 630 : imm_use_iterator imm_iter;
4880 630 : class loop *loop = LOOP_VINFO_LOOP (vinfo);
4881 2025 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
4882 : gimple_get_lhs (scalar_def))
4883 : /* There are exactly two non-debug uses, the reduction
4884 : PHI and the loop-closed PHI node. */
4885 1395 : if (!is_gimple_debug (USE_STMT (use_p))
4886 1395 : && gimple_bb (USE_STMT (use_p)) == loop->header)
4887 : {
4888 630 : auto_vec<stmt_vec_info, 64> phis (group_size);
4889 630 : stmt_vec_info phi_info = vinfo->lookup_stmt (USE_STMT (use_p));
4890 4327 : for (unsigned i = 0; i < group_size; ++i)
4891 3697 : phis.quick_push (phi_info);
4892 630 : slp_tree *phi_node = bst_map->get (phis);
4893 630 : unsigned dest_idx = loop_latch_edge (loop)->dest_idx;
4894 1260 : SLP_TREE_CHILDREN (*phi_node)[dest_idx]
4895 630 : = SLP_INSTANCE_TREE (new_instance);
4896 630 : SLP_INSTANCE_TREE (new_instance)->refcnt++;
4897 630 : }
4898 :
4899 630 : vinfo->slp_instances.safe_push (new_instance);
4900 :
4901 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4902 : the number of SLP lanes of the root in a few places.
4903 : Verify that assumption holds. */
4904 630 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
4905 : == group_size);
4906 :
4907 630 : if (dump_enabled_p ())
4908 : {
4909 156 : dump_printf_loc (MSG_NOTE, vect_location,
4910 : "Final SLP tree for instance %p:\n",
4911 : (void *) new_instance);
4912 156 : vect_print_slp_graph (MSG_NOTE, vect_location,
4913 : SLP_INSTANCE_TREE (new_instance));
4914 : }
4915 :
4916 630 : return true;
4917 : }
4918 :
4919 : /* Failed to SLP. */
4920 316 : scalar_stmts.release ();
4921 316 : if (dump_enabled_p ())
4922 99 : dump_printf_loc (MSG_NOTE, vect_location,
4923 : "SLP discovery of reduction chain failed\n");
4924 : return false;
4925 : }
4926 :
4927 : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4928 : of KIND. Return true if successful. */
4929 :
4930 : static bool
4931 99814 : vect_analyze_slp_reduction (loop_vec_info vinfo,
4932 : stmt_vec_info scalar_stmt,
4933 : unsigned max_tree_size, unsigned *limit,
4934 : scalar_stmts_to_slp_tree_map_t *bst_map,
4935 : bool force_single_lane)
4936 : {
4937 99814 : slp_instance_kind kind = slp_inst_kind_reduc_group;
4938 :
4939 : /* Try to gather a reduction chain. Only attempt if there's budget left
4940 : since chain analysis may build multi-lane trees that consume limit. */
4941 99814 : if (! force_single_lane
4942 72307 : && *limit != 0
4943 72307 : && STMT_VINFO_DEF_TYPE (scalar_stmt) == vect_reduction_def
4944 171830 : && vect_analyze_slp_reduc_chain (vinfo, bst_map, scalar_stmt,
4945 : max_tree_size, limit))
4946 : return true;
4947 :
4948 99097 : vec<stmt_vec_info> scalar_stmts;
4949 99097 : scalar_stmts.create (1);
4950 99097 : scalar_stmts.quick_push (scalar_stmt);
4951 :
4952 99097 : if (dump_enabled_p ())
4953 : {
4954 4096 : dump_printf_loc (MSG_NOTE, vect_location,
4955 : "Starting SLP discovery for\n");
4956 8192 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4957 8192 : dump_printf_loc (MSG_NOTE, vect_location,
4958 4096 : " %G", scalar_stmts[i]->stmt);
4959 : }
4960 :
4961 : /* Build the tree for the SLP instance. */
4962 99097 : unsigned int group_size = scalar_stmts.length ();
4963 99097 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
4964 99097 : unsigned tree_size = 0;
4965 :
4966 99097 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, matches, limit,
4967 : &tree_size, bst_map);
4968 99097 : if (node != NULL)
4969 : {
4970 : /* Create a new SLP instance. */
4971 97224 : slp_instance new_instance = XNEW (class _slp_instance);
4972 97224 : SLP_INSTANCE_TREE (new_instance) = node;
4973 97224 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4974 97224 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4975 97224 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4976 97224 : SLP_INSTANCE_KIND (new_instance) = kind;
4977 97224 : new_instance->reduc_phis = NULL;
4978 97224 : new_instance->cost_vec = vNULL;
4979 97224 : new_instance->subgraph_entries = vNULL;
4980 :
4981 97224 : if (dump_enabled_p ())
4982 4004 : dump_printf_loc (MSG_NOTE, vect_location,
4983 : "SLP size %u vs. limit %u.\n",
4984 : tree_size, max_tree_size);
4985 :
4986 97224 : vinfo->slp_instances.safe_push (new_instance);
4987 :
4988 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4989 : the number of SLP lanes of the root in a few places.
4990 : Verify that assumption holds. */
4991 97224 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
4992 : == group_size);
4993 :
4994 97224 : if (dump_enabled_p ())
4995 : {
4996 4004 : dump_printf_loc (MSG_NOTE, vect_location,
4997 : "Final SLP tree for instance %p:\n",
4998 : (void *) new_instance);
4999 4004 : vect_print_slp_graph (MSG_NOTE, vect_location,
5000 : SLP_INSTANCE_TREE (new_instance));
5001 : }
5002 :
5003 97224 : return true;
5004 : }
5005 : /* Failed to SLP. */
5006 :
5007 : /* Free the allocated memory. */
5008 1873 : scalar_stmts.release ();
5009 :
5010 : /* Failed to SLP. */
5011 1873 : if (dump_enabled_p ())
5012 92 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
5013 : return false;
5014 : }
5015 :
5016 : /* Analyze SCALAR_STMTS for SLP reduction groups. If successful add
5017 : SLP instances for them and return true. Any groups of size one
5018 : are handled appropriately. Otherwise return false. */
5019 :
5020 : static bool
5021 12177 : vect_analyze_slp_reduction_group (loop_vec_info loop_vinfo,
5022 : vec<stmt_vec_info> scalar_stmts,
5023 : scalar_stmts_to_slp_tree_map_t *bst_map,
5024 : unsigned max_tree_size, unsigned *limit)
5025 : {
5026 : /* Try to form a reduction group. Size-1 groups are not suitable
5027 : for SLP reduction and should fall back to single-lane reduction. */
5028 12177 : unsigned int group_size = scalar_stmts.length ();
5029 12177 : gcc_assert (group_size > 1);
5030 12177 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
5031 12177 : unsigned tree_size = 0;
5032 12177 : slp_tree node = vect_build_slp_tree (loop_vinfo, scalar_stmts, matches, limit,
5033 : &tree_size, bst_map);
5034 12177 : if (node)
5035 : {
5036 : /* Create a new SLP instance. */
5037 4566 : slp_instance new_instance = XNEW (class _slp_instance);
5038 4566 : SLP_INSTANCE_TREE (new_instance) = node;
5039 4566 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
5040 4566 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
5041 4566 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
5042 4566 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_group;
5043 4566 : new_instance->reduc_phis = NULL;
5044 4566 : new_instance->cost_vec = vNULL;
5045 4566 : new_instance->subgraph_entries = vNULL;
5046 :
5047 4566 : if (dump_enabled_p ())
5048 204 : dump_printf_loc (MSG_NOTE, vect_location,
5049 : "SLP size %u vs. limit %u.\n",
5050 : tree_size, max_tree_size);
5051 :
5052 4566 : loop_vinfo->slp_instances.safe_push (new_instance);
5053 :
5054 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
5055 : the number of SLP lanes of the root in a few places.
5056 : Verify that assumption holds. */
5057 4566 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
5058 : == group_size);
5059 :
5060 4566 : if (dump_enabled_p ())
5061 : {
5062 204 : dump_printf_loc (MSG_NOTE, vect_location,
5063 : "SLP discovery of size %d reduction group "
5064 : "succeeded\n", group_size);
5065 204 : dump_printf_loc (MSG_NOTE, vect_location,
5066 : "Final SLP tree for instance %p:\n",
5067 : (void *) new_instance);
5068 204 : vect_print_slp_graph (MSG_NOTE, vect_location,
5069 : SLP_INSTANCE_TREE (new_instance));
5070 : }
5071 :
5072 4566 : return true;
5073 : }
5074 :
5075 : /* Bail on fatal error. */
5076 7611 : if (matches[0] == -1)
5077 : return false;
5078 :
5079 : /* When analysis as a single SLP reduction group failed try to form
5080 : sub-groups by collecting matching lanes. Everything not
5081 : covered by a sub-group gets single-reduction treatment. */
5082 7483 : vec<stmt_vec_info> cands = vNULL;
5083 7483 : int first_cand = -1;
5084 31420 : do
5085 : {
5086 31420 : cands.truncate (0);
5087 31420 : cands.reserve (group_size, true);
5088 31420 : int new_first_cand = -1;
5089 265134 : for (unsigned i = 0; i < group_size; ++i)
5090 202294 : if (matches[i] == first_cand)
5091 : {
5092 36403 : cands.quick_push (scalar_stmts[i]);
5093 36403 : scalar_stmts[i] = NULL;
5094 : }
5095 165891 : else if (new_first_cand == -1
5096 165891 : && matches[i] > first_cand)
5097 : {
5098 23970 : gcc_assert (matches[i] == (int)i);
5099 : new_first_cand = i;
5100 : }
5101 :
5102 31420 : if (cands.is_empty ())
5103 : ;
5104 24072 : else if (cands.length () == 1)
5105 : {
5106 33204 : if (! vect_analyze_slp_reduction (loop_vinfo,
5107 16602 : vect_stmt_to_vectorize (cands[0]),
5108 : max_tree_size, limit,
5109 : bst_map, false))
5110 : {
5111 36 : cands.release ();
5112 36 : return false;
5113 : }
5114 16566 : cands.release ();
5115 : }
5116 : /* Try to form a reduction group. */
5117 7470 : else if (!vect_analyze_slp_reduction_group (loop_vinfo, cands, bst_map,
5118 : max_tree_size, limit))
5119 : {
5120 14 : cands.release ();
5121 14 : return false;
5122 : }
5123 : else
5124 7456 : cands = vNULL;
5125 31370 : first_cand = new_first_cand;
5126 : }
5127 31370 : while (first_cand >= 0);
5128 7433 : cands.release ();
5129 :
5130 7433 : scalar_stmts.release ();
5131 7433 : return true;
5132 : }
5133 :
5134 : /* Analyze reductions in LOOP_VINFO and populate SLP instances
5135 : accordingly. Returns false if something fails. */
5136 :
5137 : static bool
5138 535124 : vect_analyze_slp_reductions (loop_vec_info loop_vinfo,
5139 : unsigned max_tree_size, unsigned *limit,
5140 : scalar_stmts_to_slp_tree_map_t *bst_map,
5141 : bool force_single_lane)
5142 : {
5143 535124 : if (loop_vinfo->reductions.is_empty ())
5144 : return true;
5145 :
5146 : /* Collect reduction statements we can combine into
5147 : a SLP reduction. */
5148 77584 : vec<stmt_vec_info> scalar_stmts;
5149 77584 : scalar_stmts.create (loop_vinfo->reductions.length ());
5150 342698 : for (auto next_info : loop_vinfo->reductions)
5151 : {
5152 109946 : next_info = vect_stmt_to_vectorize (next_info);
5153 109946 : if ((STMT_VINFO_RELEVANT_P (next_info)
5154 14 : || STMT_VINFO_LIVE_P (next_info))
5155 : /* ??? Make sure we didn't skip a conversion around a
5156 : reduction path. In that case we'd have to reverse
5157 : engineer that conversion stmt following the chain using
5158 : reduc_idx and from the PHI using reduc_def. */
5159 109932 : && (STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def
5160 109932 : || (STMT_VINFO_DEF_TYPE (next_info)
5161 : == vect_double_reduction_def)))
5162 : {
5163 : /* Do not discover SLP reductions combining lane-reducing
5164 : ops, that will fail later. */
5165 109932 : if (!force_single_lane
5166 109932 : && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info)))
5167 81662 : scalar_stmts.quick_push (next_info);
5168 : /* Do SLP discovery for single-lane reductions. */
5169 28270 : else if (! vect_analyze_slp_reduction (loop_vinfo, next_info,
5170 : max_tree_size, limit,
5171 : bst_map,
5172 : force_single_lane))
5173 : {
5174 0 : scalar_stmts.release ();
5175 0 : return false;
5176 : }
5177 : }
5178 : }
5179 :
5180 : /* Try to form reduction groups. */
5181 77584 : if (scalar_stmts.length () > 1)
5182 : {
5183 4707 : if (!vect_analyze_slp_reduction_group (loop_vinfo, scalar_stmts, bst_map,
5184 : max_tree_size, limit))
5185 : {
5186 164 : scalar_stmts.release ();
5187 164 : return false;
5188 : }
5189 : return true;
5190 : }
5191 :
5192 : /* Do SLP discovery for a single reduction. */
5193 72877 : if (scalar_stmts.length () == 1
5194 127819 : && !vect_analyze_slp_reduction (loop_vinfo,
5195 54942 : vect_stmt_to_vectorize (scalar_stmts[0]),
5196 : max_tree_size, limit,
5197 : bst_map, force_single_lane))
5198 : {
5199 1837 : scalar_stmts.release ();
5200 1837 : return false;
5201 : }
5202 :
5203 71040 : scalar_stmts.release ();
5204 71040 : return true;
5205 : }
5206 :
5207 : /* Analyze an SLP instance starting from a group of grouped stores. Call
5208 : vect_build_slp_tree to build a tree of packed stmts if possible.
5209 : Return FALSE if it's impossible to SLP any stmt in the group. */
5210 :
5211 : static bool
5212 1096195 : vect_analyze_slp_instance (vec_info *vinfo,
5213 : scalar_stmts_to_slp_tree_map_t *bst_map,
5214 : stmt_vec_info stmt_info,
5215 : slp_instance_kind kind,
5216 : unsigned max_tree_size, unsigned *limit,
5217 : bool force_single_lane)
5218 : {
5219 : /* If there's no budget left bail out before allocating the lane list. */
5220 1096195 : if (*limit == 0)
5221 : return false;
5222 :
5223 1096189 : vec<stmt_vec_info> scalar_stmts;
5224 :
5225 1096189 : if (is_a <bb_vec_info> (vinfo))
5226 1065007 : vect_location = stmt_info->stmt;
5227 :
5228 1096189 : gcc_assert (kind == slp_inst_kind_store);
5229 :
5230 : /* Collect the stores and store them in scalar_stmts. */
5231 1096189 : scalar_stmts.create (DR_GROUP_SIZE (stmt_info));
5232 1096189 : stmt_vec_info next_info = stmt_info;
5233 5457091 : while (next_info)
5234 : {
5235 3264713 : scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
5236 3264713 : next_info = DR_GROUP_NEXT_ELEMENT (next_info);
5237 : }
5238 :
5239 1096189 : vec<stmt_vec_info> root_stmt_infos = vNULL;
5240 1096189 : vec<tree> remain = vNULL;
5241 :
5242 : /* Build the tree for the SLP instance. */
5243 :
5244 1096189 : if (dump_enabled_p ())
5245 : {
5246 4374 : dump_printf_loc (MSG_NOTE, vect_location,
5247 : "Starting SLP discovery for\n");
5248 25102 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
5249 41456 : dump_printf_loc (MSG_NOTE, vect_location,
5250 20728 : " %G", scalar_stmts[i]->stmt);
5251 : }
5252 :
5253 : /* Build the tree for the SLP instance. */
5254 1096189 : unsigned int group_size = scalar_stmts.length ();
5255 1096189 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
5256 1096189 : unsigned tree_size = 0;
5257 1096189 : unsigned i;
5258 :
5259 1096189 : slp_tree node = NULL;
5260 1096189 : if (group_size > 1 && force_single_lane)
5261 : {
5262 1785 : matches[0] = 0;
5263 1785 : matches[1] = -1;
5264 : }
5265 : else
5266 1094404 : node = vect_build_slp_tree (vinfo, scalar_stmts, matches, limit,
5267 : &tree_size, bst_map);
5268 1096189 : if (node != NULL)
5269 : {
5270 : /* Create a new SLP instance. */
5271 688525 : slp_instance new_instance = XNEW (class _slp_instance);
5272 688525 : SLP_INSTANCE_TREE (new_instance) = node;
5273 688525 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
5274 688525 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
5275 688525 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
5276 688525 : SLP_INSTANCE_KIND (new_instance) = kind;
5277 688525 : new_instance->reduc_phis = NULL;
5278 688525 : new_instance->cost_vec = vNULL;
5279 688525 : new_instance->subgraph_entries = vNULL;
5280 :
5281 688525 : if (dump_enabled_p ())
5282 3324 : dump_printf_loc (MSG_NOTE, vect_location,
5283 : "SLP size %u vs. limit %u.\n",
5284 : tree_size, max_tree_size);
5285 :
5286 688525 : vinfo->slp_instances.safe_push (new_instance);
5287 :
5288 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
5289 : the number of SLP lanes of the root in a few places.
5290 : Verify that assumption holds. */
5291 688525 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
5292 : == group_size);
5293 :
5294 688525 : if (dump_enabled_p ())
5295 : {
5296 3324 : dump_printf_loc (MSG_NOTE, vect_location,
5297 : "Final SLP tree for instance %p:\n",
5298 : (void *) new_instance);
5299 3324 : vect_print_slp_graph (MSG_NOTE, vect_location,
5300 : SLP_INSTANCE_TREE (new_instance));
5301 : }
5302 688525 : return true;
5303 : }
5304 : /* Failed to SLP. */
5305 :
5306 : /* Try to break the group up into pieces. */
5307 : /* ??? Improve this with the matches[] improvements. */
5308 407664 : if (*limit > 0 && kind == slp_inst_kind_store)
5309 : {
5310 : /* ??? We could delay all the actual splitting of store-groups
5311 : until after SLP discovery of the original group completed.
5312 : Then we can recurse to vect_build_slp_instance directly. */
5313 989136 : for (i = 0; i < group_size; i++)
5314 989136 : if (matches[i] != 0)
5315 : break;
5316 :
5317 : /* For basic block SLP, try to break the group up into multiples of
5318 : a vector size. */
5319 407658 : if (is_a <bb_vec_info> (vinfo)
5320 407658 : && (i > 1 && i < group_size))
5321 : {
5322 : /* Free the allocated memory. */
5323 119808 : scalar_stmts.release ();
5324 :
5325 119808 : tree scalar_type
5326 119808 : = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
5327 239616 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
5328 119808 : 1 << floor_log2 (i));
5329 119808 : unsigned HOST_WIDE_INT const_nunits;
5330 119808 : if (vectype
5331 119808 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits))
5332 : {
5333 : /* Split into two groups at the first vector boundary. */
5334 119808 : gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
5335 119808 : unsigned group1_size = i & ~(const_nunits - 1);
5336 :
5337 119808 : if (dump_enabled_p ())
5338 98 : dump_printf_loc (MSG_NOTE, vect_location,
5339 : "Splitting SLP group at stmt %u\n", i);
5340 119808 : stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
5341 : group1_size);
5342 119808 : bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info,
5343 : kind, max_tree_size,
5344 : limit, false);
5345 : /* Split the rest at the failure point and possibly
5346 : re-analyze the remaining matching part if it has
5347 : at least two lanes. */
5348 119808 : if (group1_size < i
5349 6079 : && (i + 1 < group_size
5350 2655 : || i - group1_size > 1))
5351 : {
5352 3557 : stmt_vec_info rest2 = rest;
5353 3557 : rest = vect_split_slp_store_group (rest, i - group1_size);
5354 3557 : if (i - group1_size > 1)
5355 180 : res |= vect_analyze_slp_instance (vinfo, bst_map, rest2,
5356 : kind, max_tree_size,
5357 : limit, false);
5358 : }
5359 : /* Re-analyze the non-matching tail if it has at least
5360 : two lanes. */
5361 119808 : if (i + 1 < group_size)
5362 22628 : res |= vect_analyze_slp_instance (vinfo, bst_map,
5363 : rest, kind, max_tree_size,
5364 : limit, false);
5365 1096195 : return res;
5366 : }
5367 : }
5368 :
5369 : /* For loop vectorization split the RHS into arbitrary pieces of
5370 : size >= 1. */
5371 287850 : else if (is_a <loop_vec_info> (vinfo)
5372 287850 : && (group_size != 1 && i < group_size))
5373 : {
5374 8416 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
5375 33 : bool masked_p = call
5376 33 : && gimple_call_internal_p (call)
5377 33 : && internal_fn_mask_index (gimple_call_internal_fn (call)) != -1;
5378 : /* There are targets that cannot do even/odd interleaving schemes
5379 : so they absolutely need to use load/store-lanes. For now
5380 : force single-lane SLP for them - they would be happy with
5381 : uniform power-of-two lanes (but depending on element size),
5382 : but even if we can use 'i' as indicator we would need to
5383 : backtrack when later lanes fail to discover with the same
5384 : granularity. We cannot turn any of strided or scatter store
5385 : into store-lanes. */
5386 : /* ??? If this is not in sync with what get_load_store_type
5387 : later decides the SLP representation is not good for other
5388 : store vectorization methods. */
5389 8416 : bool want_store_lanes
5390 8416 : = (! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
5391 8416 : && ! STMT_VINFO_STRIDED_P (stmt_info)
5392 6287 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
5393 6278 : && compare_step_with_zero (vinfo, stmt_info) > 0
5394 14585 : && vect_slp_prefer_store_lanes_p (vinfo, stmt_info, NULL_TREE,
5395 16832 : masked_p, group_size, i));
5396 8416 : if (want_store_lanes || force_single_lane)
5397 : i = 1;
5398 :
5399 : /* A fatal discovery fail doesn't always mean single-lane SLP
5400 : isn't a possibility, so try. */
5401 6631 : if (i == 0)
5402 : i = 1;
5403 :
5404 8416 : if (dump_enabled_p ())
5405 890 : dump_printf_loc (MSG_NOTE, vect_location,
5406 : "Splitting SLP group at stmt %u\n", i);
5407 :
5408 : /* Analyze the stored values and pinch them together with
5409 : a permute node so we can preserve the whole store group. */
5410 8416 : auto_vec<slp_tree> rhs_nodes;
5411 :
5412 8416 : unsigned int rhs_common_nlanes = 0;
5413 8416 : unsigned int start = 0, end = i;
5414 38561 : while (start < group_size)
5415 : {
5416 30262 : gcc_assert (end - start >= 1);
5417 30262 : vec<stmt_vec_info> substmts;
5418 30262 : substmts.create (end - start);
5419 127044 : for (unsigned j = start; j < end; ++j)
5420 66520 : substmts.quick_push (scalar_stmts[j]);
5421 30262 : node = vect_build_slp_tree (vinfo, substmts,
5422 : matches, limit, &tree_size, bst_map);
5423 30262 : if (node)
5424 : {
5425 24167 : rhs_nodes.safe_push (node);
5426 24167 : if (start == 0)
5427 8299 : rhs_common_nlanes = SLP_TREE_LANES (node);
5428 15868 : else if (rhs_common_nlanes != SLP_TREE_LANES (node))
5429 1437 : rhs_common_nlanes = 0;
5430 24167 : start = end;
5431 24167 : if (want_store_lanes || force_single_lane)
5432 5364 : end = start + 1;
5433 : else
5434 : end = group_size;
5435 : }
5436 : else
5437 : {
5438 6095 : substmts.release ();
5439 6095 : if (end - start == 1)
5440 : {
5441 : /* Single-lane discovery failed. Free resources. */
5442 117 : for (auto node : rhs_nodes)
5443 0 : vect_free_slp_tree (node);
5444 117 : scalar_stmts.release ();
5445 117 : if (dump_enabled_p ())
5446 15 : dump_printf_loc (MSG_NOTE, vect_location,
5447 : "SLP discovery failed\n");
5448 117 : return false;
5449 : }
5450 :
5451 : /* ??? It really happens that we soft-fail SLP
5452 : build at a mismatch but the matching part hard-fails
5453 : later. As we know we arrived here with a group
5454 : larger than one try a group of size one! */
5455 5978 : if (matches[0] != 0)
5456 12 : end = start + 1;
5457 : else
5458 13103 : for (unsigned j = start; j < end; j++)
5459 13103 : if (matches[j - start] != 0)
5460 : {
5461 : end = j;
5462 : break;
5463 : }
5464 : }
5465 : }
5466 :
5467 : /* Now re-assess whether we want store lanes in case the
5468 : discovery ended up producing all single-lane RHSs. */
5469 8299 : if (! want_store_lanes
5470 8299 : && rhs_common_nlanes == 1
5471 7199 : && ! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
5472 7199 : && ! STMT_VINFO_STRIDED_P (stmt_info)
5473 5441 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
5474 5438 : && compare_step_with_zero (vinfo, stmt_info) > 0
5475 13646 : && (vect_store_lanes_supported (SLP_TREE_VECTYPE (rhs_nodes[0]),
5476 : group_size, masked_p)
5477 : != IFN_LAST))
5478 : want_store_lanes = true;
5479 :
5480 : /* Now we assume we can build the root SLP node from all stores. */
5481 8299 : if (want_store_lanes)
5482 : {
5483 : /* For store-lanes feed the store node with all RHS nodes
5484 : in order. */
5485 0 : node = vect_create_new_slp_node (scalar_stmts,
5486 0 : SLP_TREE_CHILDREN
5487 : (rhs_nodes[0]).length ());
5488 0 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
5489 0 : node->ldst_lanes = true;
5490 0 : SLP_TREE_CHILDREN (node)
5491 0 : .reserve_exact (SLP_TREE_CHILDREN (rhs_nodes[0]).length ()
5492 0 : + rhs_nodes.length () - 1);
5493 : /* First store value and possibly mask. */
5494 0 : SLP_TREE_CHILDREN (node)
5495 0 : .splice (SLP_TREE_CHILDREN (rhs_nodes[0]));
5496 : /* Rest of the store values. All mask nodes are the same,
5497 : this should be guaranteed by dataref group discovery. */
5498 0 : for (unsigned j = 1; j < rhs_nodes.length (); ++j)
5499 0 : SLP_TREE_CHILDREN (node)
5500 0 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[0]);
5501 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
5502 0 : child->refcnt++;
5503 : }
5504 : else
5505 8299 : node = vect_build_slp_store_interleaving (rhs_nodes, scalar_stmts);
5506 :
5507 32466 : while (!rhs_nodes.is_empty ())
5508 24167 : vect_free_slp_tree (rhs_nodes.pop ());
5509 :
5510 : /* Create a new SLP instance. */
5511 8299 : slp_instance new_instance = XNEW (class _slp_instance);
5512 8299 : SLP_INSTANCE_TREE (new_instance) = node;
5513 8299 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
5514 8299 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
5515 8299 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
5516 8299 : SLP_INSTANCE_KIND (new_instance) = kind;
5517 8299 : new_instance->reduc_phis = NULL;
5518 8299 : new_instance->cost_vec = vNULL;
5519 8299 : new_instance->subgraph_entries = vNULL;
5520 :
5521 8299 : if (dump_enabled_p ())
5522 875 : dump_printf_loc (MSG_NOTE, vect_location,
5523 : "SLP size %u vs. limit %u.\n",
5524 : tree_size, max_tree_size);
5525 :
5526 8299 : vinfo->slp_instances.safe_push (new_instance);
5527 :
5528 : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
5529 : the number of SLP lanes of the root in a few places.
5530 : Verify that assumption holds. */
5531 8299 : gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
5532 : == group_size);
5533 :
5534 8299 : if (dump_enabled_p ())
5535 : {
5536 875 : dump_printf_loc (MSG_NOTE, vect_location,
5537 : "Final SLP tree for instance %p:\n",
5538 : (void *) new_instance);
5539 875 : vect_print_slp_graph (MSG_NOTE, vect_location,
5540 : SLP_INSTANCE_TREE (new_instance));
5541 : }
5542 : return true;
5543 8416 : }
5544 : else
5545 : /* Free the allocated memory. */
5546 279434 : scalar_stmts.release ();
5547 :
5548 : /* Even though the first vector did not all match, we might be able to SLP
5549 : (some) of the remainder. FORNOW ignore this possibility. */
5550 : }
5551 : else
5552 : /* Free the allocated memory. */
5553 6 : scalar_stmts.release ();
5554 :
5555 : /* Failed to SLP. */
5556 279440 : if (dump_enabled_p ())
5557 62 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
5558 : return false;
5559 : }
5560 :
5561 : /* qsort comparator ordering SLP load nodes. */
5562 :
5563 : static int
5564 2746849 : vllp_cmp (const void *a_, const void *b_)
5565 : {
5566 2746849 : const slp_tree a = *(const slp_tree *)a_;
5567 2746849 : const slp_tree b = *(const slp_tree *)b_;
5568 2746849 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (a)[0];
5569 2746849 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (b)[0];
5570 2746849 : if (STMT_VINFO_GROUPED_ACCESS (a0)
5571 1561447 : && STMT_VINFO_GROUPED_ACCESS (b0)
5572 4245422 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
5573 : {
5574 : /* Same group, order after lanes used. */
5575 389057 : if (SLP_TREE_LANES (a) < SLP_TREE_LANES (b))
5576 : return 1;
5577 386068 : else if (SLP_TREE_LANES (a) > SLP_TREE_LANES (b))
5578 : return -1;
5579 : else
5580 : {
5581 : /* Try to order loads using the same lanes together, breaking
5582 : the tie with the lane number that first differs. */
5583 382587 : if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
5584 382587 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
5585 : return 0;
5586 382587 : else if (SLP_TREE_LOAD_PERMUTATION (a).exists ()
5587 382587 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
5588 : return 1;
5589 377145 : else if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
5590 377145 : && SLP_TREE_LOAD_PERMUTATION (b).exists ())
5591 : return -1;
5592 : else
5593 : {
5594 369359 : for (unsigned i = 0; i < SLP_TREE_LANES (a); ++i)
5595 369359 : if (SLP_TREE_LOAD_PERMUTATION (a)[i]
5596 369359 : != SLP_TREE_LOAD_PERMUTATION (b)[i])
5597 : {
5598 : /* In-order lane first, that's what the above case for
5599 : no permutation does. */
5600 365871 : if (SLP_TREE_LOAD_PERMUTATION (a)[i] == i)
5601 : return -1;
5602 208916 : else if (SLP_TREE_LOAD_PERMUTATION (b)[i] == i)
5603 : return 1;
5604 103904 : else if (SLP_TREE_LOAD_PERMUTATION (a)[i]
5605 103904 : < SLP_TREE_LOAD_PERMUTATION (b)[i])
5606 : return -1;
5607 : else
5608 38918 : return 1;
5609 : }
5610 : return 0;
5611 : }
5612 : }
5613 : }
5614 : else /* Different groups or non-groups. */
5615 : {
5616 : /* Order groups as their first element to keep them together. */
5617 2357792 : if (STMT_VINFO_GROUPED_ACCESS (a0))
5618 2357792 : a0 = DR_GROUP_FIRST_ELEMENT (a0);
5619 2357792 : if (STMT_VINFO_GROUPED_ACCESS (b0))
5620 2357792 : b0 = DR_GROUP_FIRST_ELEMENT (b0);
5621 2357792 : if (a0 == b0)
5622 : return 0;
5623 : /* Tie using UID. */
5624 2357672 : else if (gimple_uid (STMT_VINFO_STMT (a0))
5625 2357672 : < gimple_uid (STMT_VINFO_STMT (b0)))
5626 : return -1;
5627 : else
5628 : {
5629 1057756 : gcc_assert (gimple_uid (STMT_VINFO_STMT (a0))
5630 : != gimple_uid (STMT_VINFO_STMT (b0)));
5631 : return 1;
5632 : }
5633 : }
5634 : }
5635 :
5636 : /* Return whether if the load permutation of NODE is consecutive starting
5637 : with value START_VAL in the first element. If START_VAL is not given
5638 : the first element's value is used. */
5639 :
5640 : bool
5641 668513 : vect_load_perm_consecutive_p (slp_tree node, unsigned start_val)
5642 : {
5643 668513 : load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION (node);
5644 :
5645 668513 : if (!perm.exists () || !perm.length ())
5646 : return false;
5647 :
5648 668513 : if (start_val == UINT_MAX)
5649 101697 : start_val = perm[0];
5650 :
5651 1320972 : for (unsigned int i = 0; i < perm.length (); i++)
5652 676833 : if (perm[i] != start_val + (unsigned int) i)
5653 : return false;
5654 :
5655 : return true;
5656 : }
5657 :
5658 : /* Process the set of LOADS that are all from the same dataref group. */
5659 :
5660 : static void
5661 156800 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
5662 : scalar_stmts_to_slp_tree_map_t *bst_map,
5663 : const array_slice<slp_tree> &loads,
5664 : bool force_single_lane)
5665 : {
5666 : /* We at this point want to lower without a fixed VF or vector
5667 : size in mind which means we cannot actually compute whether we
5668 : need three or more vectors for a load permutation yet. So always
5669 : lower. */
5670 156800 : stmt_vec_info first
5671 156800 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (loads[0])[0]);
5672 156800 : unsigned group_lanes = DR_GROUP_SIZE (first);
5673 :
5674 : /* Verify if all load permutations can be implemented with a suitably
5675 : large element load-lanes operation. */
5676 156800 : unsigned ld_lanes_lanes = SLP_TREE_LANES (loads[0]);
5677 156800 : if (STMT_VINFO_STRIDED_P (first)
5678 154301 : || compare_step_with_zero (loop_vinfo, first) <= 0
5679 151936 : || exact_log2 (ld_lanes_lanes) == -1
5680 : /* ??? For now only support the single-lane case as there is
5681 : missing support on the store-lane side and code generation
5682 : isn't up to the task yet. */
5683 149047 : || ld_lanes_lanes != 1
5684 293743 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (loads[0]),
5685 : group_lanes / ld_lanes_lanes,
5686 : false) == IFN_LAST)
5687 : ld_lanes_lanes = 0;
5688 : else
5689 : /* Verify the loads access the same number of lanes aligned to
5690 : ld_lanes_lanes. */
5691 0 : for (slp_tree load : loads)
5692 : {
5693 0 : if (SLP_TREE_LANES (load) != ld_lanes_lanes)
5694 : {
5695 : ld_lanes_lanes = 0;
5696 : break;
5697 : }
5698 0 : unsigned first = SLP_TREE_LOAD_PERMUTATION (load)[0];
5699 0 : if (first % ld_lanes_lanes != 0)
5700 : {
5701 : ld_lanes_lanes = 0;
5702 : break;
5703 : }
5704 0 : if (!vect_load_perm_consecutive_p (load))
5705 : {
5706 : ld_lanes_lanes = 0;
5707 : break;
5708 : }
5709 : }
5710 :
5711 : /* Only a power-of-two number of lanes matches interleaving with N levels.
5712 : ??? An even number of lanes could be reduced to 1<<ceil_log2(N)-1 lanes
5713 : at each step. */
5714 249840 : if (ld_lanes_lanes == 0 && exact_log2 (group_lanes) == -1 && group_lanes != 3)
5715 : return;
5716 :
5717 282421 : for (slp_tree load : loads)
5718 : {
5719 : /* Leave masked or gather loads alone for now. */
5720 191118 : if (!SLP_TREE_CHILDREN (load).is_empty ())
5721 62490 : continue;
5722 :
5723 : /* For single-element interleaving spanning multiple vectors avoid
5724 : lowering, we want to use VMAT_ELEMENTWISE later. */
5725 191112 : if (ld_lanes_lanes == 0
5726 191112 : && SLP_TREE_LANES (load) == 1
5727 169155 : && !DR_GROUP_NEXT_ELEMENT (first)
5728 251104 : && maybe_gt (group_lanes,
5729 : TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (load))))
5730 30996 : return;
5731 :
5732 : /* We want to pattern-match special cases here and keep those
5733 : alone. Candidates are splats and load-lane. */
5734 :
5735 : /* We need to lower only loads of less than half of the groups
5736 : lanes, including duplicate lanes. Note this leaves nodes
5737 : with a non-1:1 load permutation around instead of canonicalizing
5738 : those into a load and a permute node. Removing this early
5739 : check would do such canonicalization. */
5740 160116 : if (SLP_TREE_LANES (load) >= (group_lanes + 1) / 2
5741 58908 : && ld_lanes_lanes == 0)
5742 58908 : continue;
5743 :
5744 : /* Build the permute to get the original load permutation order. */
5745 101208 : bool contiguous = vect_load_perm_consecutive_p (load);
5746 101208 : lane_permutation_t final_perm;
5747 101208 : final_perm.create (SLP_TREE_LANES (load));
5748 304568 : for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i)
5749 204304 : final_perm.quick_push (
5750 102152 : std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i]));
5751 :
5752 : /* When the load permutation accesses a contiguous unpermuted,
5753 : power-of-two aligned and sized chunk leave the load alone.
5754 : We can likely (re-)load it more efficiently rather than
5755 : extracting it from the larger load.
5756 : ??? Long-term some of the lowering should move to where
5757 : the vector types involved are fixed. */
5758 104784 : if (!force_single_lane
5759 101208 : && ld_lanes_lanes == 0
5760 64534 : && contiguous
5761 64274 : && (SLP_TREE_LANES (load) > 1 || loads.size () == 1)
5762 6694 : && pow2p_hwi (SLP_TREE_LANES (load))
5763 6658 : && pow2p_hwi (group_lanes)
5764 3576 : && SLP_TREE_LOAD_PERMUTATION (load)[0] % SLP_TREE_LANES (load) == 0
5765 104784 : && group_lanes % SLP_TREE_LANES (load) == 0)
5766 : {
5767 3576 : final_perm.release ();
5768 3576 : continue;
5769 : }
5770 :
5771 : /* First build (and possibly re-use) a load node for the
5772 : unpermuted group. Gaps in the middle and on the end are
5773 : represented with NULL stmts. */
5774 97632 : vec<stmt_vec_info> stmts;
5775 97632 : stmts.create (group_lanes);
5776 336399 : for (stmt_vec_info s = first; s; s = DR_GROUP_NEXT_ELEMENT (s))
5777 : {
5778 238767 : if (s != first)
5779 146812 : for (unsigned i = 1; i < DR_GROUP_GAP (s); ++i)
5780 5677 : stmts.quick_push (NULL);
5781 238767 : stmts.quick_push (s);
5782 : }
5783 186501 : for (unsigned i = 0; i < DR_GROUP_GAP (first); ++i)
5784 88869 : stmts.quick_push (NULL);
5785 97632 : match_elt_t *matches = XALLOCAVEC (match_elt_t, group_lanes);
5786 97632 : unsigned limit = 1;
5787 97632 : unsigned tree_size = 0;
5788 97632 : slp_tree l0 = vect_build_slp_tree (loop_vinfo, stmts, matches, &limit,
5789 97632 : &tree_size, bst_map);
5790 97632 : gcc_assert (!SLP_TREE_LOAD_PERMUTATION (l0).exists ());
5791 :
5792 97632 : if (ld_lanes_lanes != 0)
5793 : {
5794 : /* ??? If this is not in sync with what get_load_store_type
5795 : later decides the SLP representation is not good for other
5796 : store vectorization methods. */
5797 0 : l0->ldst_lanes = true;
5798 0 : load->ldst_lanes = true;
5799 : }
5800 :
5801 301802 : while (1)
5802 : {
5803 199717 : unsigned group_lanes = SLP_TREE_LANES (l0);
5804 199717 : if (ld_lanes_lanes != 0
5805 199717 : || SLP_TREE_LANES (load) >= (group_lanes + 1) / 2)
5806 : break;
5807 :
5808 : /* Try to lower by reducing the group to half its size using an
5809 : interleaving scheme. For this try to compute whether all
5810 : elements needed for this load are in even or odd elements of
5811 : an even/odd decomposition with N consecutive elements.
5812 : Thus { e, e, o, o, e, e, o, o } would be an even/odd decomposition
5813 : with N == 2. */
5814 : /* ??? Only an even number of lanes can be handed this way, but the
5815 : fallback below could work for any number. We have to make sure
5816 : to round up in that case. */
5817 102085 : gcc_assert ((group_lanes & 1) == 0 || group_lanes == 3);
5818 12358 : unsigned even = 0, odd = 0;
5819 12358 : if ((group_lanes & 1) == 0)
5820 : {
5821 12358 : even = (1 << ceil_log2 (group_lanes)) - 1;
5822 12358 : odd = even;
5823 50181 : for (auto l : final_perm)
5824 : {
5825 13107 : even &= ~l.second;
5826 13107 : odd &= l.second;
5827 : }
5828 : }
5829 :
5830 : /* Now build an even or odd extraction from the unpermuted load. */
5831 102085 : lane_permutation_t perm;
5832 102085 : perm.create ((group_lanes + 1) / 2);
5833 102085 : unsigned even_level = even ? 1 << ctz_hwi (even) : 0;
5834 102085 : unsigned odd_level = odd ? 1 << ctz_hwi (odd) : 0;
5835 102085 : if (even_level
5836 11378 : && group_lanes % (2 * even_level) == 0
5837 : /* ??? When code generating permutes we do not try to pun
5838 : to larger component modes so level != 1 isn't a natural
5839 : even/odd extract. Prefer one if possible. */
5840 11378 : && (even_level == 1 || !odd_level || odd_level != 1))
5841 : {
5842 : /* { 0, 1, ... 4, 5 ..., } */
5843 43128 : for (unsigned i = 0; i < group_lanes / 2 / even_level; ++i)
5844 69345 : for (unsigned j = 0; j < even_level; ++j)
5845 34868 : perm.quick_push (std::make_pair (0, 2 * i * even_level + j));
5846 : }
5847 90707 : else if (odd_level)
5848 : {
5849 : /* { ..., 2, 3, ... 6, 7 } */
5850 3677 : gcc_assert (group_lanes % (2 * odd_level) == 0);
5851 16233 : for (unsigned i = 0; i < group_lanes / 2 / odd_level; ++i)
5852 25166 : for (unsigned j = 0; j < odd_level; ++j)
5853 12610 : perm.quick_push
5854 12610 : (std::make_pair (0, (2 * i + 1) * odd_level + j));
5855 : }
5856 : else
5857 : {
5858 : /* As fallback extract all used lanes and fill to half the
5859 : group size by repeating the last element.
5860 : ??? This is quite a bad strathegy for re-use - we could
5861 : brute force our way to find more optimal filling lanes to
5862 : maximize re-use when looking at all loads from the group. */
5863 89757 : auto_bitmap l;
5864 359084 : for (auto p : final_perm)
5865 89813 : bitmap_set_bit (l, p.second);
5866 89757 : unsigned i = 0;
5867 89757 : bitmap_iterator bi;
5868 179570 : EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi)
5869 89813 : perm.quick_push (std::make_pair (0, i));
5870 359180 : while (perm.length () < (group_lanes + 1) / 2)
5871 89833 : perm.quick_push (perm.last ());
5872 89757 : }
5873 :
5874 : /* Update final_perm with the intermediate permute. */
5875 204919 : for (unsigned i = 0; i < final_perm.length (); ++i)
5876 : {
5877 102834 : unsigned l = final_perm[i].second;
5878 102834 : unsigned j;
5879 112830 : for (j = 0; j < perm.length (); ++j)
5880 112830 : if (perm[j].second == l)
5881 : {
5882 102834 : final_perm[i].second = j;
5883 102834 : break;
5884 : }
5885 102834 : gcc_assert (j < perm.length ());
5886 : }
5887 :
5888 : /* And create scalar stmts. */
5889 102085 : vec<stmt_vec_info> perm_stmts;
5890 102085 : perm_stmts.create (perm.length ());
5891 431294 : for (unsigned i = 0; i < perm.length (); ++i)
5892 227124 : perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]);
5893 :
5894 102085 : slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR);
5895 102085 : SLP_TREE_CHILDREN (p).quick_push (l0);
5896 102085 : SLP_TREE_LANE_PERMUTATION (p) = perm;
5897 102085 : SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load);
5898 102085 : SLP_TREE_LANES (p) = perm.length ();
5899 : /* ??? As we have scalar stmts for this intermediate permute we
5900 : could CSE it via bst_map but we do not want to pick up
5901 : another SLP node with a load permutation. We instead should
5902 : have a "local" CSE map here. */
5903 102085 : SLP_TREE_SCALAR_STMTS (p) = perm_stmts;
5904 :
5905 : /* We now have a node for (group_lanes + 1) / 2 lanes. */
5906 102085 : l0 = p;
5907 102085 : }
5908 :
5909 : /* And finally from the ordered reduction node create the
5910 : permute to shuffle the lanes into the original load-permutation
5911 : order. We replace the original load node with this. */
5912 97632 : SLP_TREE_CODE (load) = VEC_PERM_EXPR;
5913 97632 : SLP_TREE_LOAD_PERMUTATION (load).release ();
5914 97632 : SLP_TREE_LANE_PERMUTATION (load) = final_perm;
5915 97632 : SLP_TREE_CHILDREN (load).create (1);
5916 97632 : SLP_TREE_CHILDREN (load).quick_push (l0);
5917 97632 : SLP_TREE_REPRESENTATIVE (load) = NULL;
5918 : }
5919 : }
5920 :
5921 : /* Transform SLP loads in the SLP graph created by SLP discovery to
5922 : group loads from the same group and lower load permutations that
5923 : are unlikely to be supported into a series of permutes.
5924 : In the degenerate case of having only single-lane SLP instances
5925 : this should result in a series of permute nodes emulating an
5926 : interleaving scheme. */
5927 :
5928 : static void
5929 519733 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
5930 : scalar_stmts_to_slp_tree_map_t *bst_map,
5931 : bool force_single_lane)
5932 : {
5933 : /* Gather and sort loads across all instances. */
5934 519733 : hash_set<slp_tree> visited;
5935 519733 : auto_vec<slp_tree> loads;
5936 2380934 : for (auto inst : loop_vinfo->slp_instances)
5937 823807 : vect_gather_slp_loads (loads, SLP_INSTANCE_TREE (inst), visited);
5938 519733 : if (loads.is_empty ())
5939 98979 : return;
5940 420754 : loads.qsort (vllp_cmp);
5941 :
5942 : /* Now process each dataref group separately. */
5943 420754 : unsigned firsti = 0;
5944 1197215 : for (unsigned i = 1; i < loads.length (); ++i)
5945 : {
5946 355707 : slp_tree first = loads[firsti];
5947 355707 : slp_tree next = loads[i];
5948 355707 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (first)[0];
5949 355707 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (next)[0];
5950 355707 : if (STMT_VINFO_GROUPED_ACCESS (a0)
5951 161840 : && STMT_VINFO_GROUPED_ACCESS (b0)
5952 504136 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
5953 74129 : continue;
5954 : /* Now we have one or multiple SLP loads of the same group from
5955 : firsti to i - 1. */
5956 281578 : if (STMT_VINFO_GROUPED_ACCESS (a0))
5957 87711 : vect_lower_load_permutations (loop_vinfo, bst_map,
5958 87711 : make_array_slice (&loads[firsti],
5959 : i - firsti),
5960 : force_single_lane);
5961 : firsti = i;
5962 : }
5963 841508 : if (firsti < loads.length ()
5964 841508 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (loads[firsti])[0]))
5965 69089 : vect_lower_load_permutations (loop_vinfo, bst_map,
5966 69089 : make_array_slice (&loads[firsti],
5967 69089 : loads.length () - firsti),
5968 : force_single_lane);
5969 519733 : }
5970 :
5971 : /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
5972 : trees of packed scalar stmts if SLP is possible. */
5973 :
5974 : opt_result
5975 1221916 : vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size,
5976 : bool force_single_lane)
5977 : {
5978 1221916 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5979 1221916 : unsigned int i;
5980 1221916 : stmt_vec_info first_element;
5981 1221916 : slp_instance instance;
5982 :
5983 1221916 : DUMP_VECT_SCOPE ("vect_analyze_slp");
5984 :
5985 1221916 : unsigned limit = max_tree_size;
5986 :
5987 1221916 : scalar_stmts_to_slp_tree_map_t *bst_map
5988 1221916 : = new scalar_stmts_to_slp_tree_map_t ();
5989 :
5990 : /* Find SLP sequences starting from groups of grouped stores. */
5991 3397286 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
5992 953579 : if (! vect_analyze_slp_instance (vinfo, bst_map, first_element,
5993 : slp_inst_kind_store, max_tree_size, &limit,
5994 : force_single_lane)
5995 953579 : && loop_vinfo)
5996 : {
5997 125 : release_scalar_stmts_to_slp_tree_map (bst_map);
5998 125 : return opt_result::failure_at (vect_location, "SLP build failed.\n");
5999 : }
6000 :
6001 : /* For loops also start SLP discovery from non-grouped stores. */
6002 1221791 : if (loop_vinfo)
6003 : {
6004 : data_reference_p dr;
6005 1753367 : FOR_EACH_VEC_ELT (vinfo->shared->datarefs, i, dr)
6006 1218243 : if (DR_IS_WRITE (dr))
6007 : {
6008 395923 : stmt_vec_info stmt_info = vinfo->lookup_dr (dr)->stmt;
6009 : /* Grouped stores are already handled above. */
6010 395923 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
6011 108208 : continue;
6012 287715 : vec<stmt_vec_info> stmts;
6013 287715 : vec<stmt_vec_info> roots = vNULL;
6014 287715 : vec<tree> remain = vNULL;
6015 287715 : stmts.create (1);
6016 287715 : stmts.quick_push (stmt_info);
6017 287715 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
6018 : stmts, roots, remain, max_tree_size,
6019 : &limit, bst_map, force_single_lane))
6020 : {
6021 3854 : release_scalar_stmts_to_slp_tree_map (bst_map);
6022 3854 : return opt_result::failure_at (vect_location,
6023 : "SLP build failed.\n");
6024 : }
6025 : }
6026 :
6027 : stmt_vec_info stmt_info;
6028 535184 : FOR_EACH_VEC_ELT (LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo), i, stmt_info)
6029 : {
6030 30 : vec<stmt_vec_info> stmts;
6031 30 : vec<stmt_vec_info> roots = vNULL;
6032 30 : vec<tree> remain = vNULL;
6033 30 : stmts.create (1);
6034 30 : stmts.quick_push (stmt_info);
6035 30 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
6036 : stmts, roots, remain, max_tree_size,
6037 : &limit, bst_map, force_single_lane))
6038 : {
6039 0 : release_scalar_stmts_to_slp_tree_map (bst_map);
6040 0 : return opt_result::failure_at (vect_location,
6041 : "SLP build failed.\n");
6042 : }
6043 : }
6044 : }
6045 :
6046 1217937 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
6047 : {
6048 2099429 : for (unsigned i = 0; i < bb_vinfo->roots.length (); ++i)
6049 : {
6050 1416616 : vect_location = bb_vinfo->roots[i].roots[0]->stmt;
6051 : /* Apply patterns. */
6052 4624529 : for (unsigned j = 0; j < bb_vinfo->roots[i].stmts.length (); ++j)
6053 6415826 : bb_vinfo->roots[i].stmts[j]
6054 3316638 : = vect_stmt_to_vectorize (bb_vinfo->roots[i].stmts[j]);
6055 1416616 : if (vect_build_slp_instance (bb_vinfo, bb_vinfo->roots[i].kind,
6056 1416616 : bb_vinfo->roots[i].stmts,
6057 1416616 : bb_vinfo->roots[i].roots,
6058 1416616 : bb_vinfo->roots[i].remain,
6059 : max_tree_size, &limit, bst_map, false))
6060 : {
6061 160014 : bb_vinfo->roots[i].roots = vNULL;
6062 160014 : bb_vinfo->roots[i].remain = vNULL;
6063 : }
6064 1416616 : bb_vinfo->roots[i].stmts = vNULL;
6065 : }
6066 : }
6067 :
6068 1217937 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
6069 : {
6070 : /* Find SLP sequences starting from groups of reductions. */
6071 535124 : if (!vect_analyze_slp_reductions (loop_vinfo, max_tree_size, &limit,
6072 : bst_map, force_single_lane))
6073 : {
6074 2001 : release_scalar_stmts_to_slp_tree_map (bst_map);
6075 2001 : return opt_result::failure_at (vect_location, "SLP build failed.\n");
6076 : }
6077 :
6078 : /* Make sure to vectorize only-live stmts, usually inductions. */
6079 2401090 : for (edge e : get_loop_exit_edges (LOOP_VINFO_LOOP (loop_vinfo)))
6080 1551936 : for (auto gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
6081 739517 : gsi_next (&gsi))
6082 : {
6083 750215 : gphi *lc_phi = *gsi;
6084 750215 : tree def = gimple_phi_arg_def_from_edge (lc_phi, e);
6085 750215 : stmt_vec_info stmt_info;
6086 750215 : if (TREE_CODE (def) == SSA_NAME
6087 633521 : && !virtual_operand_p (def)
6088 323975 : && (stmt_info = loop_vinfo->lookup_def (def))
6089 287180 : && ((stmt_info = vect_stmt_to_vectorize (stmt_info)), true)
6090 287180 : && STMT_VINFO_RELEVANT (stmt_info) == vect_used_only_live
6091 226443 : && STMT_VINFO_LIVE_P (stmt_info)
6092 226443 : && !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
6093 869369 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
6094 : {
6095 119066 : vec<stmt_vec_info> stmts;
6096 119066 : vec<stmt_vec_info> roots = vNULL;
6097 119066 : vec<tree> remain = vNULL;
6098 119066 : stmts.create (1);
6099 119066 : stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
6100 119066 : if (! vect_build_slp_instance (vinfo,
6101 : slp_inst_kind_reduc_group,
6102 : stmts, roots, remain,
6103 : max_tree_size, &limit,
6104 : bst_map, force_single_lane))
6105 : {
6106 10698 : release_scalar_stmts_to_slp_tree_map (bst_map);
6107 10698 : return opt_result::failure_at (vect_location,
6108 : "SLP build failed.\n");
6109 : }
6110 : }
6111 10698 : }
6112 :
6113 : /* Find SLP sequences starting from gconds. */
6114 1301841 : for (auto cond : LOOP_VINFO_LOOP_CONDS (loop_vinfo))
6115 : {
6116 302854 : auto cond_info = loop_vinfo->lookup_stmt (cond);
6117 :
6118 302854 : cond_info = vect_stmt_to_vectorize (cond_info);
6119 302854 : vec<stmt_vec_info> roots = vNULL;
6120 302854 : roots.safe_push (cond_info);
6121 302854 : gimple *stmt = STMT_VINFO_STMT (cond_info);
6122 302854 : tree args0 = gimple_cond_lhs (stmt);
6123 302854 : tree args1 = gimple_cond_rhs (stmt);
6124 :
6125 : /* These should be enforced by cond lowering, but if it failed
6126 : bail. */
6127 302854 : if (gimple_cond_code (stmt) != NE_EXPR
6128 301692 : || TREE_TYPE (args0) != boolean_type_node
6129 603941 : || !integer_zerop (args1))
6130 : {
6131 1767 : roots.release ();
6132 1767 : release_scalar_stmts_to_slp_tree_map (bst_map);
6133 1767 : return opt_result::failure_at (vect_location,
6134 : "SLP build failed.\n");
6135 : }
6136 :
6137 : /* An argument without a loop def will be codegened from vectorizing the
6138 : root gcond itself. As such we don't need to try to build an SLP tree
6139 : from them. It's highly likely that the resulting SLP tree here if both
6140 : arguments have a def will be incompatible, but we rely on it being split
6141 : later on. */
6142 301087 : auto varg = loop_vinfo->lookup_def (args0);
6143 301087 : vec<stmt_vec_info> stmts;
6144 301087 : vec<tree> remain = vNULL;
6145 301087 : stmts.create (1);
6146 301087 : stmts.quick_push (vect_stmt_to_vectorize (varg));
6147 :
6148 301087 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_gcond,
6149 : stmts, roots, remain,
6150 : max_tree_size, &limit,
6151 : bst_map, force_single_lane))
6152 : {
6153 925 : roots.release ();
6154 925 : release_scalar_stmts_to_slp_tree_map (bst_map);
6155 925 : return opt_result::failure_at (vect_location,
6156 : "SLP build failed.\n");
6157 : }
6158 : }
6159 : }
6160 :
6161 1202546 : hash_set<slp_tree> visited_patterns;
6162 1202546 : slp_tree_to_load_perm_map_t perm_cache;
6163 1202546 : slp_compat_nodes_map_t compat_cache;
6164 :
6165 : /* See if any patterns can be found in the SLP tree. */
6166 1202546 : bool pattern_found = false;
6167 4054680 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6168 1649588 : pattern_found |= vect_match_slp_patterns (instance, vinfo,
6169 : &visited_patterns, &perm_cache,
6170 : &compat_cache);
6171 :
6172 : /* If any were found optimize permutations of loads. */
6173 1202546 : if (pattern_found)
6174 : {
6175 266 : hash_map<slp_tree, slp_tree> load_map;
6176 4936 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6177 : {
6178 4138 : slp_tree root = SLP_INSTANCE_TREE (instance);
6179 4138 : optimize_load_redistribution (bst_map, vinfo, SLP_TREE_LANES (root),
6180 : &load_map, root);
6181 : }
6182 266 : }
6183 :
6184 : /* Check whether we should force some SLP instances to use load/store-lanes
6185 : and do so by forcing SLP re-discovery with single lanes. We used
6186 : to cancel SLP when this applied to all instances in a loop but now
6187 : we decide this per SLP instance. It's important to do this only
6188 : after SLP pattern recognition. */
6189 1202546 : if (is_a <loop_vec_info> (vinfo))
6190 1343540 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6191 823807 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
6192 313456 : && !SLP_INSTANCE_TREE (instance)->ldst_lanes)
6193 : {
6194 313456 : slp_tree slp_root = SLP_INSTANCE_TREE (instance);
6195 313456 : unsigned int group_size = SLP_TREE_LANES (slp_root);
6196 313456 : tree vectype = SLP_TREE_VECTYPE (slp_root);
6197 :
6198 313456 : stmt_vec_info rep_info = SLP_TREE_REPRESENTATIVE (slp_root);
6199 313456 : gimple *rep = STMT_VINFO_STMT (rep_info);
6200 313456 : bool masked = (is_gimple_call (rep)
6201 2549 : && gimple_call_internal_p (rep)
6202 315975 : && internal_fn_mask_index
6203 2519 : (gimple_call_internal_fn (rep)) != -1);
6204 313426 : if (!STMT_VINFO_GROUPED_ACCESS (rep_info)
6205 31022 : || slp_root->ldst_lanes
6206 344478 : || (vect_store_lanes_supported (vectype, group_size, masked)
6207 : == IFN_LAST))
6208 313456 : continue;
6209 :
6210 0 : auto_vec<slp_tree> loads;
6211 0 : hash_set<slp_tree> visited;
6212 0 : vect_gather_slp_loads (loads, slp_root, visited);
6213 :
6214 : /* Check whether any load in the SLP instance is possibly
6215 : permuted. */
6216 0 : bool loads_permuted = false;
6217 0 : slp_tree load_node;
6218 0 : unsigned j;
6219 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
6220 : {
6221 0 : if (!SLP_TREE_LOAD_PERMUTATION (load_node).exists ())
6222 0 : continue;
6223 0 : for (unsigned k = 0; k < SLP_TREE_LANES (load_node); k++)
6224 0 : if (SLP_TREE_LOAD_PERMUTATION (load_node)[k] != k)
6225 : {
6226 : loads_permuted = true;
6227 : break;
6228 : }
6229 : }
6230 :
6231 : /* If the loads and stores can use load/store-lanes force re-discovery
6232 : with single lanes. */
6233 0 : if (loads_permuted)
6234 : {
6235 0 : bool can_use_lanes = true;
6236 : bool prefer_load_lanes = false;
6237 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
6238 0 : if (STMT_VINFO_GROUPED_ACCESS
6239 : (SLP_TREE_REPRESENTATIVE (load_node)))
6240 : {
6241 0 : stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
6242 : (SLP_TREE_REPRESENTATIVE (load_node));
6243 0 : rep = STMT_VINFO_STMT (stmt_vinfo);
6244 0 : masked = (is_gimple_call (rep)
6245 0 : && gimple_call_internal_p (rep)
6246 0 : && internal_fn_mask_index
6247 0 : (gimple_call_internal_fn (rep)));
6248 : /* Use SLP for strided accesses (or if we can't
6249 : load-lanes). */
6250 0 : if (STMT_VINFO_STRIDED_P (stmt_vinfo)
6251 0 : || compare_step_with_zero (vinfo, stmt_vinfo) <= 0
6252 0 : || vect_load_lanes_supported
6253 0 : (SLP_TREE_VECTYPE (load_node),
6254 0 : DR_GROUP_SIZE (stmt_vinfo), masked) == IFN_LAST
6255 : /* ??? During SLP re-discovery with a single lane
6256 : a masked grouped load will appear permuted and
6257 : discovery will fail. We have to rework this
6258 : on the discovery side - for now avoid ICEing. */
6259 0 : || masked)
6260 : {
6261 : can_use_lanes = false;
6262 : break;
6263 : }
6264 : /* Make sure that the target would prefer store-lanes
6265 : for at least one of the loads.
6266 :
6267 : ??? Perhaps we should instead require this for
6268 : all loads? */
6269 0 : prefer_load_lanes
6270 : = (prefer_load_lanes
6271 0 : || SLP_TREE_LANES (load_node) == group_size
6272 0 : || (vect_slp_prefer_store_lanes_p
6273 0 : (vinfo, stmt_vinfo,
6274 : SLP_TREE_VECTYPE (load_node), masked,
6275 : group_size, SLP_TREE_LANES (load_node))));
6276 : }
6277 :
6278 0 : if (can_use_lanes && prefer_load_lanes)
6279 : {
6280 0 : if (dump_enabled_p ())
6281 0 : dump_printf_loc (MSG_NOTE, vect_location,
6282 : "SLP instance %p can use load/store-lanes,"
6283 : " re-discovering with single-lanes\n",
6284 : (void *) instance);
6285 :
6286 0 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (slp_root);
6287 :
6288 0 : vect_free_slp_instance (instance);
6289 0 : limit = max_tree_size;
6290 0 : bool res = vect_analyze_slp_instance (vinfo, bst_map,
6291 : stmt_info,
6292 : slp_inst_kind_store,
6293 : max_tree_size, &limit,
6294 : true);
6295 0 : gcc_assert (res);
6296 0 : auto new_inst = LOOP_VINFO_SLP_INSTANCES (vinfo).pop ();
6297 0 : LOOP_VINFO_SLP_INSTANCES (vinfo)[i] = new_inst;
6298 : }
6299 : }
6300 0 : }
6301 :
6302 : /* When we end up with load permutations that we cannot possibly handle,
6303 : like those requiring three vector inputs, lower them using interleaving
6304 : like schemes. */
6305 1202546 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
6306 : {
6307 519733 : vect_lower_load_permutations (loop_vinfo, bst_map, force_single_lane);
6308 519733 : if (dump_enabled_p ())
6309 : {
6310 22602 : dump_printf_loc (MSG_NOTE, vect_location,
6311 : "SLP graph after lowering permutations:\n");
6312 22602 : hash_set<slp_tree> visited;
6313 100424 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6314 32645 : vect_print_slp_graph (MSG_NOTE, vect_location,
6315 : SLP_INSTANCE_TREE (instance), visited);
6316 22602 : }
6317 : }
6318 :
6319 1202546 : release_scalar_stmts_to_slp_tree_map (bst_map);
6320 :
6321 1202546 : if (pattern_found && dump_enabled_p ())
6322 : {
6323 20 : dump_printf_loc (MSG_NOTE, vect_location,
6324 : "Pattern matched SLP tree\n");
6325 20 : hash_set<slp_tree> visited;
6326 101 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6327 41 : vect_print_slp_graph (MSG_NOTE, vect_location,
6328 : SLP_INSTANCE_TREE (instance), visited);
6329 20 : }
6330 :
6331 1202546 : return opt_result::success ();
6332 1202546 : }
6333 :
6334 : /* Estimates the cost of inserting layout changes into the SLP graph.
6335 : It can also say that the insertion is impossible. */
6336 :
6337 : struct slpg_layout_cost
6338 : {
6339 11503510 : slpg_layout_cost () = default;
6340 : slpg_layout_cost (sreal, bool);
6341 :
6342 535830 : static slpg_layout_cost impossible () { return { sreal::max (), 0 }; }
6343 5729230 : bool is_possible () const { return depth != sreal::max (); }
6344 :
6345 : bool operator== (const slpg_layout_cost &) const;
6346 : bool operator!= (const slpg_layout_cost &) const;
6347 :
6348 : bool is_better_than (const slpg_layout_cost &, bool) const;
6349 :
6350 : void add_parallel_cost (const slpg_layout_cost &);
6351 : void add_serial_cost (const slpg_layout_cost &);
6352 : void split (unsigned int);
6353 :
6354 : /* The longest sequence of layout changes needed during any traversal
6355 : of the partition dag, weighted by execution frequency.
6356 :
6357 : This is the most important metric when optimizing for speed, since
6358 : it helps to ensure that we keep the number of operations on
6359 : critical paths to a minimum. */
6360 : sreal depth = 0;
6361 :
6362 : /* An estimate of the total number of operations needed. It is weighted by
6363 : execution frequency when optimizing for speed but not when optimizing for
6364 : size. In order to avoid double-counting, a node with a fanout of N will
6365 : distribute 1/N of its total cost to each successor.
6366 :
6367 : This is the most important metric when optimizing for size, since
6368 : it helps to keep the total number of operations to a minimum, */
6369 : sreal total = 0;
6370 : };
6371 :
6372 : /* Construct costs for a node with weight WEIGHT. A higher weight
6373 : indicates more frequent execution. IS_FOR_SIZE is true if we are
6374 : optimizing for size rather than speed. */
6375 :
6376 1357625 : slpg_layout_cost::slpg_layout_cost (sreal weight, bool is_for_size)
6377 1365808 : : depth (weight), total (is_for_size && weight > 0 ? 1 : weight)
6378 : {
6379 1357625 : }
6380 :
6381 : bool
6382 0 : slpg_layout_cost::operator== (const slpg_layout_cost &other) const
6383 : {
6384 0 : return depth == other.depth && total == other.total;
6385 : }
6386 :
6387 : bool
6388 0 : slpg_layout_cost::operator!= (const slpg_layout_cost &other) const
6389 : {
6390 0 : return !operator== (other);
6391 : }
6392 :
6393 : /* Return true if these costs are better than OTHER. IS_FOR_SIZE is
6394 : true if we are optimizing for size rather than speed. */
6395 :
6396 : bool
6397 329719 : slpg_layout_cost::is_better_than (const slpg_layout_cost &other,
6398 : bool is_for_size) const
6399 : {
6400 329719 : if (is_for_size)
6401 : {
6402 3537 : if (total != other.total)
6403 1750 : return total < other.total;
6404 1787 : return depth < other.depth;
6405 : }
6406 : else
6407 : {
6408 326182 : if (depth != other.depth)
6409 130592 : return depth < other.depth;
6410 195590 : return total < other.total;
6411 : }
6412 : }
6413 :
6414 : /* Increase the costs to account for something with cost INPUT_COST
6415 : happening in parallel with the current costs. */
6416 :
6417 : void
6418 394202 : slpg_layout_cost::add_parallel_cost (const slpg_layout_cost &input_cost)
6419 : {
6420 394202 : depth = std::max (depth, input_cost.depth);
6421 394202 : total += input_cost.total;
6422 394202 : }
6423 :
6424 : /* Increase the costs to account for something with cost INPUT_COST
6425 : happening in series with the current costs. */
6426 :
6427 : void
6428 1613109 : slpg_layout_cost::add_serial_cost (const slpg_layout_cost &other)
6429 : {
6430 1613109 : depth += other.depth;
6431 1613109 : total += other.total;
6432 1613109 : }
6433 :
6434 : /* Split the total cost among TIMES successors or predecessors. */
6435 :
6436 : void
6437 1290247 : slpg_layout_cost::split (unsigned int times)
6438 : {
6439 1290247 : if (times > 1)
6440 650417 : total /= times;
6441 1290247 : }
6442 :
6443 : /* Information about one node in the SLP graph, for use during
6444 : vect_optimize_slp_pass. */
6445 :
6446 : struct slpg_vertex
6447 : {
6448 10667043 : slpg_vertex (slp_tree node_) : node (node_) {}
6449 :
6450 : /* The node itself. */
6451 : slp_tree node;
6452 :
6453 : /* Which partition the node belongs to, or -1 if none. Nodes outside of
6454 : partitions are flexible; they can have whichever layout consumers
6455 : want them to have. */
6456 : int partition = -1;
6457 :
6458 : /* The number of nodes that directly use the result of this one
6459 : (i.e. the number of nodes that count this one as a child). */
6460 : unsigned int out_degree = 0;
6461 :
6462 : /* The execution frequency of the node. */
6463 : sreal weight = 0;
6464 :
6465 : /* The total execution frequency of all nodes that directly use the
6466 : result of this one. */
6467 : sreal out_weight = 0;
6468 : };
6469 :
6470 : /* Information about one partition of the SLP graph, for use during
6471 : vect_optimize_slp_pass. */
6472 :
6473 : struct slpg_partition_info
6474 : {
6475 : /* The nodes in the partition occupy indices [NODE_BEGIN, NODE_END)
6476 : of m_partitioned_nodes. */
6477 : unsigned int node_begin = 0;
6478 : unsigned int node_end = 0;
6479 :
6480 : /* Which layout we've chosen to use for this partition, or -1 if
6481 : we haven't picked one yet. */
6482 : int layout = -1;
6483 :
6484 : /* The number of predecessors and successors in the partition dag.
6485 : The predecessors always have lower partition numbers and the
6486 : successors always have higher partition numbers.
6487 :
6488 : Note that the directions of these edges are not necessarily the
6489 : same as in the data flow graph. For example, if an SCC has separate
6490 : partitions for an inner loop and an outer loop, the inner loop's
6491 : partition will have at least two incoming edges from the outer loop's
6492 : partition: one for a live-in value and one for a live-out value.
6493 : In data flow terms, one of these edges would also be from the outer loop
6494 : to the inner loop, but the other would be in the opposite direction. */
6495 : unsigned int in_degree = 0;
6496 : unsigned int out_degree = 0;
6497 : };
6498 :
6499 : /* Information about the costs of using a particular layout for a
6500 : particular partition. It can also say that the combination is
6501 : impossible. */
6502 :
6503 : struct slpg_partition_layout_costs
6504 : {
6505 1677349 : bool is_possible () const { return internal_cost.is_possible (); }
6506 67267 : void mark_impossible () { internal_cost = slpg_layout_cost::impossible (); }
6507 :
6508 : /* The costs inherited from predecessor partitions. */
6509 : slpg_layout_cost in_cost;
6510 :
6511 : /* The inherent cost of the layout within the node itself. For example,
6512 : this is nonzero for a load if choosing a particular layout would require
6513 : the load to permute the loaded elements. It is nonzero for a
6514 : VEC_PERM_EXPR if the permutation cannot be eliminated or converted
6515 : to full-vector moves. */
6516 : slpg_layout_cost internal_cost;
6517 :
6518 : /* The costs inherited from successor partitions. */
6519 : slpg_layout_cost out_cost;
6520 : };
6521 :
6522 : /* This class tries to optimize the layout of vectors in order to avoid
6523 : unnecessary shuffling. At the moment, the set of possible layouts are
6524 : restricted to bijective permutations.
6525 :
6526 : The goal of the pass depends on whether we're optimizing for size or
6527 : for speed. When optimizing for size, the goal is to reduce the overall
6528 : number of layout changes (including layout changes implied by things
6529 : like load permutations). When optimizing for speed, the goal is to
6530 : reduce the maximum latency attributable to layout changes on any
6531 : non-cyclical path through the data flow graph.
6532 :
6533 : For example, when optimizing a loop nest for speed, we will prefer
6534 : to make layout changes outside of a loop rather than inside of a loop,
6535 : and will prefer to make layout changes in parallel rather than serially,
6536 : even if that increases the overall number of layout changes.
6537 :
6538 : The high-level procedure is:
6539 :
6540 : (1) Build a graph in which edges go from uses (parents) to definitions
6541 : (children).
6542 :
6543 : (2) Divide the graph into a dag of strongly-connected components (SCCs).
6544 :
6545 : (3) When optimizing for speed, partition the nodes in each SCC based
6546 : on their containing cfg loop. When optimizing for size, treat
6547 : each SCC as a single partition.
6548 :
6549 : This gives us a dag of partitions. The goal is now to assign a
6550 : layout to each partition.
6551 :
6552 : (4) Construct a set of vector layouts that are worth considering.
6553 : Record which nodes must keep their current layout.
6554 :
6555 : (5) Perform a forward walk over the partition dag (from loads to stores)
6556 : accumulating the "forward" cost of using each layout. When visiting
6557 : each partition, assign a tentative choice of layout to the partition
6558 : and use that choice when calculating the cost of using a different
6559 : layout in successor partitions.
6560 :
6561 : (6) Perform a backward walk over the partition dag (from stores to loads),
6562 : accumulating the "backward" cost of using each layout. When visiting
6563 : each partition, make a final choice of layout for that partition based
6564 : on the accumulated forward costs (from (5)) and backward costs
6565 : (from (6)).
6566 :
6567 : (7) Apply the chosen layouts to the SLP graph.
6568 :
6569 : For example, consider the SLP statements:
6570 :
6571 : S1: a_1 = load
6572 : loop:
6573 : S2: a_2 = PHI<a_1, a_3>
6574 : S3: b_1 = load
6575 : S4: a_3 = a_2 + b_1
6576 : exit:
6577 : S5: a_4 = PHI<a_3>
6578 : S6: store a_4
6579 :
6580 : S2 and S4 form an SCC and are part of the same loop. Every other
6581 : statement is in a singleton SCC. In this example there is a one-to-one
6582 : mapping between SCCs and partitions and the partition dag looks like this;
6583 :
6584 : S1 S3
6585 : \ /
6586 : S2+S4
6587 : |
6588 : S5
6589 : |
6590 : S6
6591 :
6592 : S2, S3 and S4 will have a higher execution frequency than the other
6593 : statements, so when optimizing for speed, the goal is to avoid any
6594 : layout changes:
6595 :
6596 : - within S3
6597 : - within S2+S4
6598 : - on the S3->S2+S4 edge
6599 :
6600 : For example, if S3 was originally a reversing load, the goal of the
6601 : pass is to make it an unreversed load and change the layout on the
6602 : S1->S2+S4 and S2+S4->S5 edges to compensate. (Changing the layout
6603 : on S1->S2+S4 and S5->S6 would also be acceptable.)
6604 :
6605 : The difference between SCCs and partitions becomes important if we
6606 : add an outer loop:
6607 :
6608 : S1: a_1 = ...
6609 : loop1:
6610 : S2: a_2 = PHI<a_1, a_6>
6611 : S3: b_1 = load
6612 : S4: a_3 = a_2 + b_1
6613 : loop2:
6614 : S5: a_4 = PHI<a_3, a_5>
6615 : S6: c_1 = load
6616 : S7: a_5 = a_4 + c_1
6617 : exit2:
6618 : S8: a_6 = PHI<a_5>
6619 : S9: store a_6
6620 : exit1:
6621 :
6622 : Here, S2, S4, S5, S7 and S8 form a single SCC. However, when optimizing
6623 : for speed, we usually do not want restrictions in the outer loop to "infect"
6624 : the decision for the inner loop. For example, if an outer-loop node
6625 : in the SCC contains a statement with a fixed layout, that should not
6626 : prevent the inner loop from using a different layout. Conversely,
6627 : the inner loop should not dictate a layout to the outer loop: if the
6628 : outer loop does a lot of computation, then it may not be efficient to
6629 : do all of that computation in the inner loop's preferred layout.
6630 :
6631 : So when optimizing for speed, we partition the SCC into S2+S4+S8 (outer)
6632 : and S5+S7 (inner). We also try to arrange partitions so that:
6633 :
6634 : - the partition for an outer loop comes before the partition for
6635 : an inner loop
6636 :
6637 : - if a sibling loop A dominates a sibling loop B, A's partition
6638 : comes before B's
6639 :
6640 : This gives the following partition dag for the example above:
6641 :
6642 : S1 S3
6643 : \ /
6644 : S2+S4+S8 S6
6645 : | \\ /
6646 : | S5+S7
6647 : |
6648 : S9
6649 :
6650 : There are two edges from S2+S4+S8 to S5+S7: one for the edge S4->S5 and
6651 : one for a reversal of the edge S7->S8.
6652 :
6653 : The backward walk picks a layout for S5+S7 before S2+S4+S8. The choice
6654 : for S2+S4+S8 therefore has to balance the cost of using the outer loop's
6655 : preferred layout against the cost of changing the layout on entry to the
6656 : inner loop (S4->S5) and on exit from the inner loop (S7->S8 reversed).
6657 :
6658 : Although this works well when optimizing for speed, it has the downside
6659 : when optimizing for size that the choice of layout for S5+S7 is completely
6660 : independent of S9, which lessens the chance of reducing the overall number
6661 : of permutations. We therefore do not partition SCCs when optimizing
6662 : for size.
6663 :
6664 : To give a concrete example of the difference between optimizing
6665 : for size and speed, consider:
6666 :
6667 : a[0] = (b[1] << c[3]) - d[1];
6668 : a[1] = (b[0] << c[2]) - d[0];
6669 : a[2] = (b[3] << c[1]) - d[3];
6670 : a[3] = (b[2] << c[0]) - d[2];
6671 :
6672 : There are three different layouts here: one for a, one for b and d,
6673 : and one for c. When optimizing for speed it is better to permute each
6674 : of b, c and d into the order required by a, since those permutations
6675 : happen in parallel. But when optimizing for size, it is better to:
6676 :
6677 : - permute c into the same order as b
6678 : - do the arithmetic
6679 : - permute the result into the order required by a
6680 :
6681 : This gives 2 permutations rather than 3. */
6682 :
6683 : class vect_optimize_slp_pass
6684 : {
6685 : public:
6686 731233 : vect_optimize_slp_pass (vec_info *vinfo) : m_vinfo (vinfo) {}
6687 : void run ();
6688 :
6689 : private:
6690 : /* Graph building. */
6691 : struct loop *containing_loop (slp_tree);
6692 : bool is_cfg_latch_edge (graph_edge *);
6693 : void build_vertices (hash_set<slp_tree> &, slp_tree);
6694 : void build_vertices ();
6695 : void build_graph ();
6696 :
6697 : /* Partitioning. */
6698 : void create_partitions ();
6699 : template<typename T> void for_each_partition_edge (unsigned int, T);
6700 :
6701 : /* Layout selection. */
6702 : bool is_compatible_layout (slp_tree, unsigned int);
6703 : bool is_compatible_layout (const slpg_partition_info &, unsigned int);
6704 : int change_layout_cost (slp_tree, unsigned int, unsigned int);
6705 : slpg_partition_layout_costs &partition_layout_costs (unsigned int,
6706 : unsigned int);
6707 : void change_vec_perm_layout (slp_tree, lane_permutation_t &,
6708 : int, unsigned int);
6709 : int internal_node_cost (slp_tree, int, unsigned int);
6710 : void start_choosing_layouts ();
6711 : bool legitimize ();
6712 :
6713 : /* Cost propagation. */
6714 : slpg_layout_cost edge_layout_cost (graph_edge *, unsigned int,
6715 : unsigned int, unsigned int);
6716 : slpg_layout_cost total_in_cost (unsigned int);
6717 : slpg_layout_cost forward_cost (graph_edge *, unsigned int, unsigned int);
6718 : slpg_layout_cost backward_cost (graph_edge *, unsigned int, unsigned int);
6719 : void forward_pass ();
6720 : void backward_pass ();
6721 :
6722 : /* Rematerialization. */
6723 : slp_tree get_result_with_layout (slp_tree, unsigned int);
6724 : void materialize ();
6725 :
6726 : /* Clean-up. */
6727 : void remove_redundant_permutations ();
6728 :
6729 : /* Masked load lanes discovery. */
6730 : void decide_masked_load_lanes ();
6731 :
6732 : void dump ();
6733 :
6734 : vec_info *m_vinfo;
6735 :
6736 : /* True if we should optimize the graph for size, false if we should
6737 : optimize it for speed. (It wouldn't be easy to make this decision
6738 : more locally.) */
6739 : bool m_optimize_size;
6740 :
6741 : /* A graph of all SLP nodes, with edges leading from uses to definitions.
6742 : In other words, a node's predecessors are its slp_tree parents and
6743 : a node's successors are its slp_tree children. */
6744 : graph *m_slpg = nullptr;
6745 :
6746 : /* The vertices of M_SLPG, indexed by slp_tree::vertex. */
6747 : auto_vec<slpg_vertex> m_vertices;
6748 :
6749 : /* The list of all leaves of M_SLPG. such as external definitions, constants,
6750 : and loads. */
6751 : auto_vec<int> m_leafs;
6752 :
6753 : /* This array has one entry for every vector layout that we're considering.
6754 : Element 0 is null and indicates "no change". Other entries describe
6755 : permutations that are inherent in the current graph and that we would
6756 : like to reverse if possible.
6757 :
6758 : For example, a permutation { 1, 2, 3, 0 } means that something has
6759 : effectively been permuted in that way, such as a load group
6760 : { a[1], a[2], a[3], a[0] } (viewed as a permutation of a[0:3]).
6761 : We'd then like to apply the reverse permutation { 3, 0, 1, 2 }
6762 : in order to put things "back" in order. */
6763 : auto_vec<vec<unsigned> > m_perms;
6764 :
6765 : /* A partitioning of the nodes for which a layout must be chosen.
6766 : Each partition represents an <SCC, cfg loop> pair; that is,
6767 : nodes in different SCCs belong to different partitions, and nodes
6768 : within an SCC can be further partitioned according to a containing
6769 : cfg loop. Partition <SCC1, L1> comes before <SCC2, L2> if:
6770 :
6771 : - SCC1 != SCC2 and SCC1 is a predecessor of SCC2 in a forward walk
6772 : from leaves (such as loads) to roots (such as stores).
6773 :
6774 : - SCC1 == SCC2 and L1's header strictly dominates L2's header. */
6775 : auto_vec<slpg_partition_info> m_partitions;
6776 :
6777 : /* The list of all nodes for which a layout must be chosen. Nodes for
6778 : partition P come before the nodes for partition P+1. Nodes within a
6779 : partition are in reverse postorder. */
6780 : auto_vec<unsigned int> m_partitioned_nodes;
6781 :
6782 : /* Index P * num-layouts + L contains the cost of using layout L
6783 : for partition P. */
6784 : auto_vec<slpg_partition_layout_costs> m_partition_layout_costs;
6785 :
6786 : /* Index N * num-layouts + L, if nonnull, is a node that provides the
6787 : original output of node N adjusted to have layout L. */
6788 : auto_vec<slp_tree> m_node_layouts;
6789 : };
6790 :
6791 : /* Fill the vertices and leafs vector with all nodes in the SLP graph.
6792 : Also record whether we should optimize anything for speed rather
6793 : than size. */
6794 :
6795 : void
6796 11540047 : vect_optimize_slp_pass::build_vertices (hash_set<slp_tree> &visited,
6797 : slp_tree node)
6798 : {
6799 11540047 : unsigned i;
6800 11540047 : slp_tree child;
6801 :
6802 11540047 : if (visited.add (node))
6803 11540047 : return;
6804 :
6805 10667043 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
6806 : {
6807 8117202 : basic_block bb = gimple_bb (vect_orig_stmt (rep)->stmt);
6808 7158122 : if (optimize_bb_for_speed_p (bb))
6809 7032018 : m_optimize_size = false;
6810 : }
6811 :
6812 10667043 : node->vertex = m_vertices.length ();
6813 10667043 : m_vertices.safe_push (slpg_vertex (node));
6814 :
6815 10667043 : bool leaf = true;
6816 10667043 : bool force_leaf = false;
6817 20044778 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
6818 9377735 : if (child)
6819 : {
6820 8438017 : leaf = false;
6821 8438017 : build_vertices (visited, child);
6822 : }
6823 : else
6824 : force_leaf = true;
6825 : /* Since SLP discovery works along use-def edges all cycles have an
6826 : entry - but there's the exception of cycles where we do not handle
6827 : the entry explicitly (but with a NULL SLP node), like some reductions
6828 : and inductions. Force those SLP PHIs to act as leafs to make them
6829 : backwards reachable. */
6830 10667043 : if (leaf || force_leaf)
6831 5234778 : m_leafs.safe_push (node->vertex);
6832 : }
6833 :
6834 : /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
6835 :
6836 : void
6837 1462466 : vect_optimize_slp_pass::build_vertices ()
6838 : {
6839 1462466 : hash_set<slp_tree> visited;
6840 1462466 : unsigned i;
6841 1462466 : slp_instance instance;
6842 1462466 : m_vertices.truncate (0);
6843 1462466 : m_leafs.truncate (0);
6844 7489428 : FOR_EACH_VEC_ELT (m_vinfo->slp_instances, i, instance)
6845 3102030 : build_vertices (visited, SLP_INSTANCE_TREE (instance));
6846 1462466 : }
6847 :
6848 : /* Apply (reverse) bijectite PERM to VEC. */
6849 :
6850 : template <class T>
6851 : static void
6852 253585 : vect_slp_permute (vec<unsigned> perm,
6853 : vec<T> &vec, bool reverse)
6854 : {
6855 253585 : auto_vec<T, 64> saved;
6856 253585 : saved.create (vec.length ());
6857 838231 : for (unsigned i = 0; i < vec.length (); ++i)
6858 584646 : saved.quick_push (vec[i]);
6859 :
6860 253585 : if (reverse)
6861 : {
6862 1656741 : for (unsigned i = 0; i < vec.length (); ++i)
6863 582438 : vec[perm[i]] = saved[i];
6864 834979 : for (unsigned i = 0; i < vec.length (); ++i)
6865 996301 : gcc_assert (vec[perm[i]] == saved[i]);
6866 : }
6867 : else
6868 : {
6869 6504 : for (unsigned i = 0; i < vec.length (); ++i)
6870 2208 : vec[i] = saved[perm[i]];
6871 255793 : for (unsigned i = 0; i < vec.length (); ++i)
6872 3312 : gcc_assert (vec[i] == saved[perm[i]]);
6873 : }
6874 253585 : }
6875 :
6876 : /* Return the cfg loop that contains NODE. */
6877 :
6878 : struct loop *
6879 4176407 : vect_optimize_slp_pass::containing_loop (slp_tree node)
6880 : {
6881 4176407 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
6882 : /* ??? This is imprecise, VEC_PERM nodes do not have a representative
6883 : but are laid out close to their children. */
6884 4176407 : if (!rep)
6885 194241 : return m_vinfo->bbs[0]->loop_father;
6886 4475221 : return gimple_bb (vect_orig_stmt (rep)->stmt)->loop_father;
6887 : }
6888 :
6889 : /* Return true if UD (an edge from a use to a definition) is associated
6890 : with a loop latch edge in the cfg. */
6891 :
6892 : bool
6893 8438017 : vect_optimize_slp_pass::is_cfg_latch_edge (graph_edge *ud)
6894 : {
6895 8438017 : slp_tree use = m_vertices[ud->src].node;
6896 8438017 : slp_tree def = m_vertices[ud->dest].node;
6897 8438017 : if ((SLP_TREE_DEF_TYPE (use) != vect_internal_def
6898 8438017 : || SLP_TREE_PERMUTE_P (use))
6899 8022200 : || SLP_TREE_DEF_TYPE (def) != vect_internal_def)
6900 : return false;
6901 :
6902 4894726 : stmt_vec_info use_rep = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (use));
6903 4894726 : return (is_a<gphi *> (use_rep->stmt)
6904 385272 : && bb_loop_header_p (gimple_bb (use_rep->stmt))
6905 5113998 : && containing_loop (def) == containing_loop (use));
6906 : }
6907 :
6908 : /* Build the graph. Mark edges that correspond to cfg loop latch edges with
6909 : a nonnull data field. */
6910 :
6911 : void
6912 1462466 : vect_optimize_slp_pass::build_graph ()
6913 : {
6914 1462466 : m_optimize_size = true;
6915 1462466 : build_vertices ();
6916 :
6917 2924932 : m_slpg = new_graph (m_vertices.length ());
6918 15054441 : for (slpg_vertex &v : m_vertices)
6919 32035240 : for (slp_tree child : SLP_TREE_CHILDREN (v.node))
6920 9377735 : if (child)
6921 : {
6922 8438017 : graph_edge *ud = add_edge (m_slpg, v.node->vertex, child->vertex);
6923 8438017 : if (is_cfg_latch_edge (ud))
6924 209294 : ud->data = this;
6925 : }
6926 1462466 : }
6927 :
6928 : /* Return true if E corresponds to a loop latch edge in the cfg. */
6929 :
6930 : static bool
6931 4323066 : skip_cfg_latch_edges (graph_edge *e)
6932 : {
6933 4323066 : return e->data;
6934 : }
6935 :
6936 : /* Create the node partitions. */
6937 :
6938 : void
6939 731233 : vect_optimize_slp_pass::create_partitions ()
6940 : {
6941 : /* Calculate a postorder of the graph, ignoring edges that correspond
6942 : to natural latch edges in the cfg. Reading the vector from the end
6943 : to the beginning gives the reverse postorder. */
6944 731233 : auto_vec<int> initial_rpo;
6945 1462466 : graphds_dfs (m_slpg, &m_leafs[0], m_leafs.length (), &initial_rpo,
6946 : false, NULL, skip_cfg_latch_edges);
6947 2193699 : gcc_assert (initial_rpo.length () == m_vertices.length ());
6948 :
6949 : /* Calculate the strongly connected components of the graph. */
6950 731233 : auto_vec<int> scc_grouping;
6951 731233 : unsigned int num_sccs = graphds_scc (m_slpg, NULL, NULL, &scc_grouping);
6952 :
6953 : /* Create a new index order in which all nodes from the same SCC are
6954 : consecutive. Use scc_pos to record the index of the first node in
6955 : each SCC. */
6956 731233 : auto_vec<unsigned int> scc_pos (num_sccs);
6957 731233 : int last_component = -1;
6958 731233 : unsigned int node_count = 0;
6959 7526637 : for (unsigned int node_i : scc_grouping)
6960 : {
6961 5332938 : if (last_component != m_slpg->vertices[node_i].component)
6962 : {
6963 5198840 : last_component = m_slpg->vertices[node_i].component;
6964 10397680 : gcc_assert (last_component == int (scc_pos.length ()));
6965 5198840 : scc_pos.quick_push (node_count);
6966 : }
6967 5332938 : node_count += 1;
6968 : }
6969 1462466 : gcc_assert (node_count == initial_rpo.length ()
6970 : && last_component + 1 == int (num_sccs));
6971 :
6972 : /* Use m_partitioned_nodes to group nodes into SCC order, with the nodes
6973 : inside each SCC following the RPO we calculated above. The fact that
6974 : we ignored natural latch edges when calculating the RPO should ensure
6975 : that, for natural loop nests:
6976 :
6977 : - the first node that we encounter in a cfg loop is the loop header phi
6978 : - the loop header phis are in dominance order
6979 :
6980 : Arranging for this is an optimization (see below) rather than a
6981 : correctness issue. Unnatural loops with a tangled mess of backedges
6982 : will still work correctly, but might give poorer results.
6983 :
6984 : Also update scc_pos so that it gives 1 + the index of the last node
6985 : in the SCC. */
6986 731233 : m_partitioned_nodes.safe_grow (node_count);
6987 6795404 : for (unsigned int old_i = initial_rpo.length (); old_i-- > 0;)
6988 : {
6989 5332938 : unsigned int node_i = initial_rpo[old_i];
6990 5332938 : unsigned int new_i = scc_pos[m_slpg->vertices[node_i].component]++;
6991 5332938 : m_partitioned_nodes[new_i] = node_i;
6992 : }
6993 :
6994 : /* When optimizing for speed, partition each SCC based on the containing
6995 : cfg loop. The order we constructed above should ensure that, for natural
6996 : cfg loops, we'll create sub-SCC partitions for outer loops before
6997 : the corresponding sub-SCC partitions for inner loops. Similarly,
6998 : when one sibling loop A dominates another sibling loop B, we should
6999 : create a sub-SCC partition for A before a sub-SCC partition for B.
7000 :
7001 : As above, nothing depends for correctness on whether this achieves
7002 : a natural nesting, but we should get better results when it does. */
7003 1462466 : m_partitions.reserve (m_vertices.length ());
7004 731233 : unsigned int next_partition_i = 0;
7005 731233 : hash_map<struct loop *, int> loop_partitions;
7006 731233 : unsigned int rpo_begin = 0;
7007 731233 : unsigned int num_partitioned_nodes = 0;
7008 7392539 : for (unsigned int rpo_end : scc_pos)
7009 : {
7010 5198840 : loop_partitions.empty ();
7011 5198840 : unsigned int partition_i = next_partition_i;
7012 10531778 : for (unsigned int rpo_i = rpo_begin; rpo_i < rpo_end; ++rpo_i)
7013 : {
7014 : /* Handle externals and constants optimistically throughout.
7015 : But treat existing vectors as fixed since we do not handle
7016 : permuting them. */
7017 5332938 : unsigned int node_i = m_partitioned_nodes[rpo_i];
7018 5332938 : auto &vertex = m_vertices[node_i];
7019 5332938 : if ((SLP_TREE_DEF_TYPE (vertex.node) == vect_external_def
7020 518671 : && !SLP_TREE_VEC_DEFS (vertex.node).exists ())
7021 5336227 : || SLP_TREE_DEF_TYPE (vertex.node) == vect_constant_def)
7022 1564934 : vertex.partition = -1;
7023 : else
7024 : {
7025 3768004 : bool existed;
7026 3768004 : if (m_optimize_size)
7027 30141 : existed = next_partition_i > partition_i;
7028 : else
7029 : {
7030 3737863 : struct loop *loop = containing_loop (vertex.node);
7031 3737863 : auto &entry = loop_partitions.get_or_insert (loop, &existed);
7032 3737863 : if (!existed)
7033 3605002 : entry = next_partition_i;
7034 3737863 : partition_i = entry;
7035 : }
7036 3768004 : if (!existed)
7037 : {
7038 3635051 : m_partitions.quick_push (slpg_partition_info ());
7039 3635051 : next_partition_i += 1;
7040 : }
7041 3768004 : vertex.partition = partition_i;
7042 3768004 : num_partitioned_nodes += 1;
7043 3768004 : m_partitions[partition_i].node_end += 1;
7044 : }
7045 : }
7046 5198840 : rpo_begin = rpo_end;
7047 : }
7048 :
7049 : /* Assign ranges of consecutive node indices to each partition,
7050 : in partition order. Start with node_end being the same as
7051 : node_begin so that the next loop can use it as a counter. */
7052 731233 : unsigned int node_begin = 0;
7053 5828750 : for (auto &partition : m_partitions)
7054 : {
7055 3635051 : partition.node_begin = node_begin;
7056 3635051 : node_begin += partition.node_end;
7057 3635051 : partition.node_end = partition.node_begin;
7058 : }
7059 731233 : gcc_assert (node_begin == num_partitioned_nodes);
7060 :
7061 : /* Finally build the list of nodes in partition order. */
7062 731233 : m_partitioned_nodes.truncate (num_partitioned_nodes);
7063 6795404 : for (unsigned int node_i = 0; node_i < m_vertices.length (); ++node_i)
7064 : {
7065 5332938 : int partition_i = m_vertices[node_i].partition;
7066 5332938 : if (partition_i >= 0)
7067 : {
7068 3768004 : unsigned int order_i = m_partitions[partition_i].node_end++;
7069 3768004 : m_partitioned_nodes[order_i] = node_i;
7070 : }
7071 : }
7072 731233 : }
7073 :
7074 : /* Look for edges from earlier partitions into node NODE_I and edges from
7075 : node NODE_I into later partitions. Call:
7076 :
7077 : FN (ud, other_node_i)
7078 :
7079 : for each such use-to-def edge ud, where other_node_i is the node at the
7080 : other end of the edge. */
7081 :
7082 : template<typename T>
7083 : void
7084 4251552 : vect_optimize_slp_pass::for_each_partition_edge (unsigned int node_i, T fn)
7085 : {
7086 4251552 : int partition_i = m_vertices[node_i].partition;
7087 4251552 : for (graph_edge *pred = m_slpg->vertices[node_i].pred;
7088 7385293 : pred; pred = pred->pred_next)
7089 : {
7090 3133741 : int src_partition_i = m_vertices[pred->src].partition;
7091 3133741 : if (src_partition_i >= 0 && src_partition_i != partition_i)
7092 2780512 : fn (pred, pred->src);
7093 : }
7094 4251552 : for (graph_edge *succ = m_slpg->vertices[node_i].succ;
7095 9133187 : succ; succ = succ->succ_next)
7096 : {
7097 4881635 : int dest_partition_i = m_vertices[succ->dest].partition;
7098 4881635 : if (dest_partition_i >= 0 && dest_partition_i != partition_i)
7099 2824935 : fn (succ, succ->dest);
7100 : }
7101 4251552 : }
7102 :
7103 : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
7104 : that NODE would operate on. This test is independent of NODE's actual
7105 : operation. */
7106 :
7107 : bool
7108 1827625 : vect_optimize_slp_pass::is_compatible_layout (slp_tree node,
7109 : unsigned int layout_i)
7110 : {
7111 1827625 : if (layout_i == 0)
7112 : return true;
7113 :
7114 1121628 : if (SLP_TREE_LANES (node) != m_perms[layout_i].length ())
7115 12953 : return false;
7116 :
7117 : return true;
7118 : }
7119 :
7120 : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
7121 : that NODE would operate on for each NODE in PARTITION.
7122 : This test is independent of NODE's actual operations. */
7123 :
7124 : bool
7125 26246 : vect_optimize_slp_pass::is_compatible_layout (const slpg_partition_info
7126 : &partition,
7127 : unsigned int layout_i)
7128 : {
7129 52884 : for (unsigned int order_i = partition.node_begin;
7130 52884 : order_i < partition.node_end; ++order_i)
7131 : {
7132 26744 : unsigned int node_i = m_partitioned_nodes[order_i];
7133 26744 : auto &vertex = m_vertices[node_i];
7134 :
7135 : /* The layout is incompatible if it is individually incompatible
7136 : with any node in the partition. */
7137 26744 : if (!is_compatible_layout (vertex.node, layout_i))
7138 : return false;
7139 : }
7140 : return true;
7141 : }
7142 :
7143 : /* Return the cost (in arbitrary units) of going from layout FROM_LAYOUT_I
7144 : to layout TO_LAYOUT_I for a node like NODE. Return -1 if either of the
7145 : layouts is incompatible with NODE or if the change is not possible for
7146 : some other reason.
7147 :
7148 : The properties taken from NODE include the number of lanes and the
7149 : vector type. The actual operation doesn't matter. */
7150 :
7151 : int
7152 770953 : vect_optimize_slp_pass::change_layout_cost (slp_tree node,
7153 : unsigned int from_layout_i,
7154 : unsigned int to_layout_i)
7155 : {
7156 770953 : if (!is_compatible_layout (node, from_layout_i)
7157 770953 : || !is_compatible_layout (node, to_layout_i))
7158 : return -1;
7159 :
7160 770268 : if (from_layout_i == to_layout_i)
7161 : return 0;
7162 :
7163 338963 : auto_vec<slp_tree, 1> children (1);
7164 338963 : children.quick_push (node);
7165 338963 : auto_lane_permutation_t perm (SLP_TREE_LANES (node));
7166 338963 : if (from_layout_i > 0)
7167 933926 : for (unsigned int i : m_perms[from_layout_i])
7168 413273 : perm.quick_push ({ 0, i });
7169 : else
7170 539455 : for (unsigned int i = 0; i < SLP_TREE_LANES (node); ++i)
7171 374043 : perm.quick_push ({ 0, i });
7172 338963 : if (to_layout_i > 0)
7173 166349 : vect_slp_permute (m_perms[to_layout_i], perm, true);
7174 338963 : auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
7175 : children, false, nullptr);
7176 338963 : if (count >= 0)
7177 333105 : return MAX (count, 1);
7178 :
7179 : /* ??? In principle we could try changing via layout 0, giving two
7180 : layout changes rather than 1. Doing that would require
7181 : corresponding support in get_result_with_layout. */
7182 : return -1;
7183 338963 : }
7184 :
7185 : /* Return the costs of assigning layout LAYOUT_I to partition PARTITION_I. */
7186 :
7187 : inline slpg_partition_layout_costs &
7188 1144888 : vect_optimize_slp_pass::partition_layout_costs (unsigned int partition_i,
7189 : unsigned int layout_i)
7190 : {
7191 2289776 : return m_partition_layout_costs[partition_i * m_perms.length () + layout_i];
7192 : }
7193 :
7194 : /* Change PERM in one of two ways:
7195 :
7196 : - if IN_LAYOUT_I < 0, accept input operand I in the layout that has been
7197 : chosen for child I of NODE.
7198 :
7199 : - if IN_LAYOUT >= 0, accept all inputs operands with that layout.
7200 :
7201 : In both cases, arrange for the output to have layout OUT_LAYOUT_I */
7202 :
7203 : void
7204 44470 : vect_optimize_slp_pass::
7205 : change_vec_perm_layout (slp_tree node, lane_permutation_t &perm,
7206 : int in_layout_i, unsigned int out_layout_i)
7207 : {
7208 256204 : for (auto &entry : perm)
7209 : {
7210 122794 : int this_in_layout_i = in_layout_i;
7211 122794 : if (this_in_layout_i < 0)
7212 : {
7213 80047 : slp_tree in_node = SLP_TREE_CHILDREN (node)[entry.first];
7214 80047 : unsigned int in_partition_i = m_vertices[in_node->vertex].partition;
7215 80047 : if (in_partition_i == -1u)
7216 329 : continue;
7217 79718 : this_in_layout_i = m_partitions[in_partition_i].layout;
7218 : }
7219 122465 : if (this_in_layout_i > 0)
7220 29043 : entry.second = m_perms[this_in_layout_i][entry.second];
7221 : }
7222 44470 : if (out_layout_i > 0)
7223 12817 : vect_slp_permute (m_perms[out_layout_i], perm, true);
7224 44470 : }
7225 :
7226 : /* Check whether the target allows NODE to be rearranged so that the node's
7227 : output has layout OUT_LAYOUT_I. Return the cost of the change if so,
7228 : in the same arbitrary units as for change_layout_cost. Return -1 otherwise.
7229 :
7230 : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I < 0, also check whether
7231 : NODE can adapt to the layout changes that have (perhaps provisionally)
7232 : been chosen for NODE's children, so that no extra permutations are
7233 : needed on either the input or the output of NODE.
7234 :
7235 : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I >= 0, instead assume
7236 : that all inputs will be forced into layout IN_LAYOUT_I beforehand.
7237 :
7238 : IN_LAYOUT_I has no meaning for other types of node.
7239 :
7240 : Keeping the node as-is is always valid. If the target doesn't appear
7241 : to support the node as-is, but might realistically support other layouts,
7242 : then layout 0 instead has the cost of a worst-case permutation. On the
7243 : one hand, this ensures that every node has at least one valid layout,
7244 : avoiding what would otherwise be an awkward special case. On the other,
7245 : it still encourages the pass to change an invalid pre-existing layout
7246 : choice into a valid one. */
7247 :
7248 : int
7249 258604 : vect_optimize_slp_pass::internal_node_cost (slp_tree node, int in_layout_i,
7250 : unsigned int out_layout_i)
7251 : {
7252 258604 : const int fallback_cost = 1;
7253 :
7254 258604 : if (SLP_TREE_PERMUTE_P (node))
7255 : {
7256 36077 : auto_lane_permutation_t tmp_perm;
7257 36077 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
7258 :
7259 : /* Check that the child nodes support the chosen layout. Checking
7260 : the first child is enough, since any second child would have the
7261 : same shape. */
7262 36077 : auto first_child = SLP_TREE_CHILDREN (node)[0];
7263 36077 : if (in_layout_i > 0
7264 36077 : && !is_compatible_layout (first_child, in_layout_i))
7265 : return -1;
7266 :
7267 35397 : change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
7268 70794 : int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
7269 : node, tmp_perm,
7270 35397 : SLP_TREE_CHILDREN (node),
7271 : false, nullptr);
7272 35397 : if (count < 0)
7273 : {
7274 2620 : if (in_layout_i == 0 && out_layout_i == 0)
7275 : {
7276 : /* Use the fallback cost if the node could in principle support
7277 : some nonzero layout for both the inputs and the outputs.
7278 : Otherwise assume that the node will be rejected later
7279 : and rebuilt from scalars. */
7280 653 : if (SLP_TREE_LANES (node) == SLP_TREE_LANES (first_child))
7281 : return fallback_cost;
7282 357 : return 0;
7283 : }
7284 : return -1;
7285 : }
7286 :
7287 : /* We currently have no way of telling whether the new layout is cheaper
7288 : or more expensive than the old one. But at least in principle,
7289 : it should be worth making zero permutations (whole-vector shuffles)
7290 : cheaper than real permutations, in case the pass is able to remove
7291 : the latter. */
7292 32777 : return count == 0 ? 0 : 1;
7293 36077 : }
7294 :
7295 222527 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
7296 222527 : if (rep
7297 220567 : && STMT_VINFO_DATA_REF (rep)
7298 74703 : && DR_IS_READ (STMT_VINFO_DATA_REF (rep))
7299 270685 : && SLP_TREE_LOAD_PERMUTATION (node).exists ())
7300 : {
7301 39485 : auto_load_permutation_t tmp_perm;
7302 39485 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
7303 39485 : if (out_layout_i > 0)
7304 15657 : vect_slp_permute (m_perms[out_layout_i], tmp_perm, true);
7305 :
7306 39485 : poly_uint64 vf = 1;
7307 39485 : if (auto loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo))
7308 11368 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
7309 39485 : unsigned int n_perms;
7310 39485 : if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
7311 : nullptr, vf, true, false, nullptr,
7312 : &n_perms))
7313 : {
7314 2378 : auto rep = SLP_TREE_REPRESENTATIVE (node);
7315 2378 : if (out_layout_i == 0)
7316 : {
7317 : /* Use the fallback cost if the load is an N-to-N permutation.
7318 : Otherwise assume that the node will be rejected later
7319 : and rebuilt from scalars. */
7320 1673 : if (STMT_VINFO_GROUPED_ACCESS (rep)
7321 3346 : && (DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (rep))
7322 1673 : == SLP_TREE_LANES (node)))
7323 565 : return fallback_cost;
7324 : return 0;
7325 : }
7326 : return -1;
7327 : }
7328 :
7329 : /* See the comment above the corresponding VEC_PERM_EXPR handling. */
7330 37107 : return n_perms == 0 ? 0 : 1;
7331 39485 : }
7332 :
7333 : return 0;
7334 : }
7335 :
7336 : /* Decide which element layouts we should consider using. Calculate the
7337 : weights associated with inserting layout changes on partition edges.
7338 : Also mark partitions that cannot change layout, by setting their
7339 : layout to zero. */
7340 :
7341 : void
7342 731233 : vect_optimize_slp_pass::start_choosing_layouts ()
7343 : {
7344 : /* Used to assign unique permutation indices. */
7345 731233 : using perm_hash = unbounded_hashmap_traits<
7346 : vec_free_hash_base<int_hash_base<unsigned>>,
7347 : int_hash<int, -1, -2>
7348 : >;
7349 731233 : hash_map<vec<unsigned>, int, perm_hash> layout_ids;
7350 :
7351 : /* Layout 0 is "no change". */
7352 731233 : m_perms.safe_push (vNULL);
7353 :
7354 : /* Create layouts from existing permutations. */
7355 731233 : auto_load_permutation_t tmp_perm;
7356 5961703 : for (unsigned int node_i : m_partitioned_nodes)
7357 : {
7358 : /* Leafs also double as entries to the reverse graph. Allow the
7359 : layout of those to be changed. */
7360 3768004 : auto &vertex = m_vertices[node_i];
7361 3768004 : auto &partition = m_partitions[vertex.partition];
7362 3768004 : if (!m_slpg->vertices[node_i].succ)
7363 959348 : partition.layout = 0;
7364 :
7365 : /* Loads and VEC_PERM_EXPRs are the only things generating permutes. */
7366 3768004 : slp_tree node = vertex.node;
7367 3768004 : stmt_vec_info dr_stmt = SLP_TREE_REPRESENTATIVE (node);
7368 3768004 : slp_tree child;
7369 3768004 : unsigned HOST_WIDE_INT imin, imax = 0;
7370 3768004 : bool any_permute = false;
7371 3768004 : tmp_perm.truncate (0);
7372 3768004 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
7373 : {
7374 : /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the node
7375 : unpermuted, record a layout that reverses this permutation.
7376 :
7377 : We would need more work to cope with loads that are internally
7378 : permuted and also have inputs (such as masks for
7379 : IFN_MASK_LOADs). */
7380 633309 : gcc_assert (partition.layout == 0 && !m_slpg->vertices[node_i].succ);
7381 633309 : if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt))
7382 : {
7383 463927 : partition.layout = -1;
7384 3745119 : continue;
7385 : }
7386 169382 : dr_stmt = DR_GROUP_FIRST_ELEMENT (dr_stmt);
7387 169382 : imin = DR_GROUP_SIZE (dr_stmt) + 1;
7388 169382 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
7389 : }
7390 6105411 : else if (SLP_TREE_PERMUTE_P (node)
7391 185649 : && SLP_TREE_CHILDREN (node).length () == 1
7392 163979 : && (child = SLP_TREE_CHILDREN (node)[0])
7393 3320344 : && (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (child))
7394 163979 : .is_constant (&imin)))
7395 : {
7396 : /* If the child has the same vector size as this node,
7397 : reversing the permutation can make the permutation a no-op.
7398 : In other cases it can change a true permutation into a
7399 : full-vector extract. */
7400 163979 : tmp_perm.reserve (SLP_TREE_LANES (node));
7401 602421 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7402 274463 : tmp_perm.quick_push (SLP_TREE_LANE_PERMUTATION (node)[j].second);
7403 : }
7404 : else
7405 2970716 : continue;
7406 :
7407 904561 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7408 : {
7409 571200 : unsigned idx = tmp_perm[j];
7410 571200 : imin = MIN (imin, idx);
7411 571200 : imax = MAX (imax, idx);
7412 571200 : if (idx - tmp_perm[0] != j)
7413 193354 : any_permute = true;
7414 : }
7415 : /* If the span doesn't match we'd disrupt VF computation, avoid
7416 : that for now. */
7417 333361 : if (imax - imin + 1 != SLP_TREE_LANES (node))
7418 116187 : continue;
7419 : /* If there's no permute no need to split one out. In this case
7420 : we can consider turning a load into a permuted load, if that
7421 : turns out to be cheaper than alternatives. */
7422 217174 : if (!any_permute)
7423 : {
7424 194144 : partition.layout = -1;
7425 194144 : continue;
7426 : }
7427 :
7428 : /* For now only handle true permutes, like
7429 : vect_attempt_slp_rearrange_stmts did. This allows us to be lazy
7430 : when permuting constants and invariants keeping the permute
7431 : bijective. */
7432 23030 : auto_sbitmap load_index (SLP_TREE_LANES (node));
7433 23030 : bitmap_clear (load_index);
7434 115519 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7435 69459 : bitmap_set_bit (load_index, tmp_perm[j] - imin);
7436 : unsigned j;
7437 91704 : for (j = 0; j < SLP_TREE_LANES (node); ++j)
7438 68819 : if (!bitmap_bit_p (load_index, j))
7439 : break;
7440 23030 : if (j != SLP_TREE_LANES (node))
7441 145 : continue;
7442 :
7443 22885 : vec<unsigned> perm = vNULL;
7444 22885 : perm.safe_grow (SLP_TREE_LANES (node), true);
7445 114328 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7446 68558 : perm[j] = tmp_perm[j] - imin;
7447 :
7448 45770 : if (int (m_perms.length ()) >= param_vect_max_layout_candidates)
7449 : {
7450 : /* Continue to use existing layouts, but don't add any more. */
7451 0 : int *entry = layout_ids.get (perm);
7452 0 : partition.layout = entry ? *entry : 0;
7453 0 : perm.release ();
7454 : }
7455 : else
7456 : {
7457 22885 : bool existed;
7458 22885 : int &layout_i = layout_ids.get_or_insert (perm, &existed);
7459 22885 : if (existed)
7460 7952 : perm.release ();
7461 : else
7462 : {
7463 14933 : layout_i = m_perms.length ();
7464 14933 : m_perms.safe_push (perm);
7465 : }
7466 22885 : partition.layout = layout_i;
7467 : }
7468 23030 : }
7469 :
7470 : /* Initially assume that every layout is possible and has zero cost
7471 : in every partition. */
7472 731233 : m_partition_layout_costs.safe_grow_cleared (m_partitions.length ()
7473 1462466 : * m_perms.length ());
7474 :
7475 : /* We have to mark outgoing permutations facing non-associating-reduction
7476 : graph entries that are not represented as to be materialized.
7477 : slp_inst_kind_bb_reduc currently only covers associatable reductions. */
7478 3744714 : for (slp_instance instance : m_vinfo->slp_instances)
7479 1551015 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor)
7480 : {
7481 8233 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
7482 8233 : m_partitions[m_vertices[node_i].partition].layout = 0;
7483 : }
7484 1542782 : else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_chain)
7485 : {
7486 596 : stmt_vec_info stmt_info
7487 596 : = SLP_TREE_REPRESENTATIVE (SLP_INSTANCE_TREE (instance));
7488 596 : vect_reduc_info reduc_info
7489 596 : = info_for_reduction (as_a <loop_vec_info> (m_vinfo),
7490 : SLP_INSTANCE_TREE (instance));
7491 596 : if (needs_fold_left_reduction_p (TREE_TYPE
7492 : (gimple_get_lhs (stmt_info->stmt)),
7493 : VECT_REDUC_INFO_CODE (reduc_info)))
7494 : {
7495 124 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
7496 124 : m_partitions[m_vertices[node_i].partition].layout = 0;
7497 : }
7498 : }
7499 :
7500 : /* Check which layouts each node and partition can handle. Calculate the
7501 : weights associated with inserting layout changes on edges. */
7502 5961703 : for (unsigned int node_i : m_partitioned_nodes)
7503 : {
7504 3768004 : auto &vertex = m_vertices[node_i];
7505 3768004 : auto &partition = m_partitions[vertex.partition];
7506 3768004 : slp_tree node = vertex.node;
7507 :
7508 3768004 : vertex.weight = vect_slp_node_weight (m_vinfo, node);
7509 :
7510 3768004 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
7511 : {
7512 : /* We do not handle stores with a permutation, so all
7513 : incoming permutations must have been materialized.
7514 :
7515 : We also don't handle masked grouped loads, which lack a
7516 : permutation vector. In this case the memory locations
7517 : form an implicit second input to the loads, on top of the
7518 : explicit mask input, and the memory input's layout cannot
7519 : be changed.
7520 :
7521 : On the other hand, we do support permuting gather loads and
7522 : masked gather loads, where each scalar load is independent
7523 : of the others. This can be useful if the address/index input
7524 : benefits from permutation. */
7525 3579061 : if (STMT_VINFO_DATA_REF (rep)
7526 1715751 : && STMT_VINFO_GROUPED_ACCESS (rep)
7527 4570781 : && !SLP_TREE_LOAD_PERMUTATION (node).exists ())
7528 822338 : partition.layout = 0;
7529 :
7530 : /* We cannot change the layout of an operation that is
7531 : not independent on lanes. Note this is an explicit
7532 : negative list since that's much shorter than the respective
7533 : positive one but it's critical to keep maintaining it. */
7534 3579061 : if (is_gimple_call (STMT_VINFO_STMT (rep)))
7535 35218 : switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep)))
7536 : {
7537 1096 : case CFN_COMPLEX_ADD_ROT90:
7538 1096 : case CFN_COMPLEX_ADD_ROT270:
7539 1096 : case CFN_COMPLEX_MUL:
7540 1096 : case CFN_COMPLEX_MUL_CONJ:
7541 1096 : case CFN_VEC_ADDSUB:
7542 1096 : case CFN_VEC_FMADDSUB:
7543 1096 : case CFN_VEC_FMSUBADD:
7544 1096 : partition.layout = 0;
7545 : default:;
7546 : }
7547 : }
7548 :
7549 8570228 : auto process_edge = [&](graph_edge *ud, unsigned int other_node_i)
7550 : {
7551 4802224 : auto &other_vertex = m_vertices[other_node_i];
7552 :
7553 : /* Count the number of edges from earlier partitions and the number
7554 : of edges to later partitions. */
7555 4802224 : if (other_vertex.partition < vertex.partition)
7556 2401112 : partition.in_degree += 1;
7557 : else
7558 2401112 : partition.out_degree += 1;
7559 :
7560 : /* If the current node uses the result of OTHER_NODE_I, accumulate
7561 : the effects of that. */
7562 4802224 : if (ud->src == int (node_i))
7563 : {
7564 2401112 : other_vertex.out_weight += vertex.weight;
7565 2401112 : other_vertex.out_degree += 1;
7566 : }
7567 8570228 : };
7568 3768004 : for_each_partition_edge (node_i, process_edge);
7569 : }
7570 731233 : }
7571 :
7572 : /* Return the incoming costs for node NODE_I, assuming that each input keeps
7573 : its current (provisional) choice of layout. The inputs do not necessarily
7574 : have the same layout as each other. */
7575 :
7576 : slpg_layout_cost
7577 4453 : vect_optimize_slp_pass::total_in_cost (unsigned int node_i)
7578 : {
7579 4453 : auto &vertex = m_vertices[node_i];
7580 4453 : slpg_layout_cost cost;
7581 14700 : auto add_cost = [&](graph_edge *, unsigned int other_node_i)
7582 : {
7583 10247 : auto &other_vertex = m_vertices[other_node_i];
7584 10247 : if (other_vertex.partition < vertex.partition)
7585 : {
7586 6881 : auto &other_partition = m_partitions[other_vertex.partition];
7587 13762 : auto &other_costs = partition_layout_costs (other_vertex.partition,
7588 6881 : other_partition.layout);
7589 6881 : slpg_layout_cost this_cost = other_costs.in_cost;
7590 6881 : this_cost.add_serial_cost (other_costs.internal_cost);
7591 6881 : this_cost.split (other_partition.out_degree);
7592 6881 : cost.add_parallel_cost (this_cost);
7593 : }
7594 14700 : };
7595 4453 : for_each_partition_edge (node_i, add_cost);
7596 4453 : return cost;
7597 : }
7598 :
7599 : /* Return the cost of switching between layout LAYOUT1_I (at node NODE1_I)
7600 : and layout LAYOUT2_I on cross-partition use-to-def edge UD. Return
7601 : slpg_layout_cost::impossible () if the change isn't possible. */
7602 :
7603 : slpg_layout_cost
7604 770953 : vect_optimize_slp_pass::
7605 : edge_layout_cost (graph_edge *ud, unsigned int node1_i, unsigned int layout1_i,
7606 : unsigned int layout2_i)
7607 : {
7608 770953 : auto &def_vertex = m_vertices[ud->dest];
7609 770953 : auto &use_vertex = m_vertices[ud->src];
7610 770953 : auto def_layout_i = ud->dest == int (node1_i) ? layout1_i : layout2_i;
7611 770953 : auto use_layout_i = ud->dest == int (node1_i) ? layout2_i : layout1_i;
7612 770953 : auto factor = change_layout_cost (def_vertex.node, def_layout_i,
7613 : use_layout_i);
7614 770953 : if (factor < 0)
7615 6543 : return slpg_layout_cost::impossible ();
7616 :
7617 : /* We have a choice of putting the layout change at the site of the
7618 : definition or at the site of the use. Prefer the former when
7619 : optimizing for size or when the execution frequency of the
7620 : definition is no greater than the combined execution frequencies of
7621 : the uses. When putting the layout change at the site of the definition,
7622 : divvy up the cost among all consumers. */
7623 764410 : if (m_optimize_size || def_vertex.weight <= def_vertex.out_weight)
7624 : {
7625 720068 : slpg_layout_cost cost = { def_vertex.weight * factor, m_optimize_size };
7626 720068 : cost.split (def_vertex.out_degree);
7627 720068 : return cost;
7628 : }
7629 44342 : return { use_vertex.weight * factor, m_optimize_size };
7630 : }
7631 :
7632 : /* UD represents a use-def link between FROM_NODE_I and a node in a later
7633 : partition; FROM_NODE_I could be the definition node or the use node.
7634 : The node at the other end of the link wants to use layout TO_LAYOUT_I.
7635 : Return the cost of any necessary fix-ups on edge UD, or return
7636 : slpg_layout_cost::impossible () if the change isn't possible.
7637 :
7638 : At this point, FROM_NODE_I's partition has chosen the cheapest
7639 : layout based on the information available so far, but this choice
7640 : is only provisional. */
7641 :
7642 : slpg_layout_cost
7643 207662 : vect_optimize_slp_pass::forward_cost (graph_edge *ud, unsigned int from_node_i,
7644 : unsigned int to_layout_i)
7645 : {
7646 207662 : auto &from_vertex = m_vertices[from_node_i];
7647 207662 : unsigned int from_partition_i = from_vertex.partition;
7648 207662 : slpg_partition_info &from_partition = m_partitions[from_partition_i];
7649 207662 : gcc_assert (from_partition.layout >= 0);
7650 :
7651 : /* First calculate the cost on the assumption that FROM_PARTITION sticks
7652 : with its current layout preference. */
7653 207662 : slpg_layout_cost cost = slpg_layout_cost::impossible ();
7654 207662 : auto edge_cost = edge_layout_cost (ud, from_node_i,
7655 207662 : from_partition.layout, to_layout_i);
7656 207662 : if (edge_cost.is_possible ())
7657 : {
7658 408390 : auto &from_costs = partition_layout_costs (from_partition_i,
7659 204195 : from_partition.layout);
7660 204195 : cost = from_costs.in_cost;
7661 204195 : cost.add_serial_cost (from_costs.internal_cost);
7662 204195 : cost.split (from_partition.out_degree);
7663 204195 : cost.add_serial_cost (edge_cost);
7664 : }
7665 3467 : else if (from_partition.layout == 0)
7666 : /* We must allow the source partition to have layout 0 as a fallback,
7667 : in case all other options turn out to be impossible. */
7668 3467 : return cost;
7669 :
7670 : /* Take the minimum of that cost and the cost that applies if
7671 : FROM_PARTITION instead switches to TO_LAYOUT_I. */
7672 204195 : auto &direct_layout_costs = partition_layout_costs (from_partition_i,
7673 : to_layout_i);
7674 204195 : if (direct_layout_costs.is_possible ())
7675 : {
7676 175977 : slpg_layout_cost direct_cost = direct_layout_costs.in_cost;
7677 175977 : direct_cost.add_serial_cost (direct_layout_costs.internal_cost);
7678 175977 : direct_cost.split (from_partition.out_degree);
7679 175977 : if (!cost.is_possible ()
7680 175977 : || direct_cost.is_better_than (cost, m_optimize_size))
7681 36630 : cost = direct_cost;
7682 : }
7683 :
7684 204195 : return cost;
7685 : }
7686 :
7687 : /* UD represents a use-def link between TO_NODE_I and a node in an earlier
7688 : partition; TO_NODE_I could be the definition node or the use node.
7689 : The node at the other end of the link wants to use layout FROM_LAYOUT_I;
7690 : return the cost of any necessary fix-ups on edge UD, or
7691 : slpg_layout_cost::impossible () if the choice cannot be made.
7692 :
7693 : At this point, TO_NODE_I's partition has a fixed choice of layout. */
7694 :
7695 : slpg_layout_cost
7696 183126 : vect_optimize_slp_pass::backward_cost (graph_edge *ud, unsigned int to_node_i,
7697 : unsigned int from_layout_i)
7698 : {
7699 183126 : auto &to_vertex = m_vertices[to_node_i];
7700 183126 : unsigned int to_partition_i = to_vertex.partition;
7701 183126 : slpg_partition_info &to_partition = m_partitions[to_partition_i];
7702 183126 : gcc_assert (to_partition.layout >= 0);
7703 :
7704 : /* If TO_NODE_I is a VEC_PERM_EXPR consumer, see whether it can be
7705 : adjusted for this input having layout FROM_LAYOUT_I. Assume that
7706 : any other inputs keep their current choice of layout. */
7707 183126 : auto &to_costs = partition_layout_costs (to_partition_i,
7708 : to_partition.layout);
7709 183126 : if (ud->src == int (to_node_i)
7710 182836 : && SLP_TREE_PERMUTE_P (to_vertex.node))
7711 : {
7712 11621 : auto &from_partition = m_partitions[m_vertices[ud->dest].partition];
7713 11621 : auto old_layout = from_partition.layout;
7714 11621 : from_partition.layout = from_layout_i;
7715 23242 : int factor = internal_node_cost (to_vertex.node, -1,
7716 11621 : to_partition.layout);
7717 11621 : from_partition.layout = old_layout;
7718 11621 : if (factor >= 0)
7719 : {
7720 10705 : slpg_layout_cost cost = to_costs.out_cost;
7721 10705 : cost.add_serial_cost ({ to_vertex.weight * factor,
7722 : m_optimize_size });
7723 10705 : cost.split (to_partition.in_degree);
7724 10705 : return cost;
7725 : }
7726 : }
7727 :
7728 : /* Compute the cost if we insert any necessary layout change on edge UD. */
7729 172421 : auto edge_cost = edge_layout_cost (ud, to_node_i,
7730 172421 : to_partition.layout, from_layout_i);
7731 172421 : if (edge_cost.is_possible ())
7732 : {
7733 172421 : slpg_layout_cost cost = to_costs.out_cost;
7734 172421 : cost.add_serial_cost (to_costs.internal_cost);
7735 172421 : cost.split (to_partition.in_degree);
7736 172421 : cost.add_serial_cost (edge_cost);
7737 172421 : return cost;
7738 : }
7739 :
7740 0 : return slpg_layout_cost::impossible ();
7741 : }
7742 :
7743 : /* Make a forward pass through the partitions, accumulating input costs.
7744 : Make a tentative (provisional) choice of layout for each partition,
7745 : ensuring that this choice still allows later partitions to keep
7746 : their original layout. */
7747 :
7748 : void
7749 6787 : vect_optimize_slp_pass::forward_pass ()
7750 : {
7751 133966 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
7752 : ++partition_i)
7753 : {
7754 127179 : auto &partition = m_partitions[partition_i];
7755 :
7756 : /* If the partition consists of a single VEC_PERM_EXPR, precompute
7757 : the incoming cost that would apply if every predecessor partition
7758 : keeps its current layout. This is used within the loop below. */
7759 127179 : slpg_layout_cost in_cost;
7760 127179 : slp_tree single_node = nullptr;
7761 127179 : if (partition.node_end == partition.node_begin + 1)
7762 : {
7763 122519 : unsigned int node_i = m_partitioned_nodes[partition.node_begin];
7764 122519 : single_node = m_vertices[node_i].node;
7765 122519 : if (SLP_TREE_PERMUTE_P (single_node))
7766 4453 : in_cost = total_in_cost (node_i);
7767 : }
7768 :
7769 : /* Go through the possible layouts. Decide which ones are valid
7770 : for this partition and record which of the valid layouts has
7771 : the lowest cost. */
7772 127179 : unsigned int min_layout_i = 0;
7773 127179 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7774 394982 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7775 : {
7776 267803 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7777 267803 : if (!layout_costs.is_possible ())
7778 67267 : continue;
7779 :
7780 : /* If the recorded layout is already 0 then the layout cannot
7781 : change. */
7782 267803 : if (partition.layout == 0 && layout_i != 0)
7783 : {
7784 50882 : layout_costs.mark_impossible ();
7785 50882 : continue;
7786 : }
7787 :
7788 216921 : bool is_possible = true;
7789 454440 : for (unsigned int order_i = partition.node_begin;
7790 454440 : order_i < partition.node_end; ++order_i)
7791 : {
7792 250702 : unsigned int node_i = m_partitioned_nodes[order_i];
7793 250702 : auto &vertex = m_vertices[node_i];
7794 :
7795 : /* Reject the layout if it is individually incompatible
7796 : with any node in the partition. */
7797 250702 : if (!is_compatible_layout (vertex.node, layout_i))
7798 : {
7799 11482 : is_possible = false;
7800 13183 : break;
7801 : }
7802 :
7803 634119 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7804 : {
7805 394899 : auto &other_vertex = m_vertices[other_node_i];
7806 394899 : if (other_vertex.partition < vertex.partition)
7807 : {
7808 : /* Accumulate the incoming costs from earlier
7809 : partitions, plus the cost of any layout changes
7810 : on UD itself. */
7811 207662 : auto cost = forward_cost (ud, other_node_i, layout_i);
7812 207662 : if (!cost.is_possible ())
7813 3467 : is_possible = false;
7814 : else
7815 204195 : layout_costs.in_cost.add_parallel_cost (cost);
7816 : }
7817 : else
7818 : /* Reject the layout if it would make layout 0 impossible
7819 : for later partitions. This amounts to testing that the
7820 : target supports reversing the layout change on edges
7821 : to later partitions.
7822 :
7823 : In principle, it might be possible to push a layout
7824 : change all the way down a graph, so that it never
7825 : needs to be reversed and so that the target doesn't
7826 : need to support the reverse operation. But it would
7827 : be awkward to bail out if we hit a partition that
7828 : does not support the new layout, especially since
7829 : we are not dealing with a lattice. */
7830 187237 : is_possible &= edge_layout_cost (ud, other_node_i, 0,
7831 187237 : layout_i).is_possible ();
7832 634119 : };
7833 239220 : for_each_partition_edge (node_i, add_cost);
7834 :
7835 : /* Accumulate the cost of using LAYOUT_I within NODE,
7836 : both for the inputs and the outputs. */
7837 239220 : int factor = internal_node_cost (vertex.node, layout_i,
7838 : layout_i);
7839 239220 : if (factor < 0)
7840 : {
7841 1701 : is_possible = false;
7842 1701 : break;
7843 : }
7844 237519 : else if (factor)
7845 39652 : layout_costs.internal_cost.add_serial_cost
7846 39652 : ({ vertex.weight * factor, m_optimize_size });
7847 : }
7848 216921 : if (!is_possible)
7849 : {
7850 16385 : layout_costs.mark_impossible ();
7851 16385 : continue;
7852 : }
7853 :
7854 : /* Combine the incoming and partition-internal costs. */
7855 200536 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7856 200536 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7857 :
7858 : /* If this partition consists of a single VEC_PERM_EXPR, see
7859 : if the VEC_PERM_EXPR can be changed to support output layout
7860 : LAYOUT_I while keeping all the provisional choices of input
7861 : layout. */
7862 200536 : if (single_node && SLP_TREE_PERMUTE_P (single_node))
7863 : {
7864 7763 : int factor = internal_node_cost (single_node, -1, layout_i);
7865 7763 : if (factor >= 0)
7866 : {
7867 7028 : auto weight = m_vertices[single_node->vertex].weight;
7868 7028 : slpg_layout_cost internal_cost
7869 7028 : = { weight * factor, m_optimize_size };
7870 :
7871 7028 : slpg_layout_cost alt_cost = in_cost;
7872 7028 : alt_cost.add_serial_cost (internal_cost);
7873 7028 : if (alt_cost.is_better_than (combined_cost, m_optimize_size))
7874 : {
7875 1742 : combined_cost = alt_cost;
7876 1742 : layout_costs.in_cost = in_cost;
7877 1742 : layout_costs.internal_cost = internal_cost;
7878 : }
7879 : }
7880 : }
7881 :
7882 : /* Record the layout with the lowest cost. Prefer layout 0 in
7883 : the event of a tie between it and another layout. */
7884 200536 : if (!min_layout_cost.is_possible ()
7885 73357 : || combined_cost.is_better_than (min_layout_cost,
7886 : m_optimize_size))
7887 : {
7888 144952 : min_layout_i = layout_i;
7889 144952 : min_layout_cost = combined_cost;
7890 : }
7891 : }
7892 :
7893 : /* This loop's handling of earlier partitions should ensure that
7894 : choosing the original layout for the current partition is no
7895 : less valid than it was in the original graph, even with the
7896 : provisional layout choices for those earlier partitions. */
7897 127179 : gcc_assert (min_layout_cost.is_possible ());
7898 127179 : partition.layout = min_layout_i;
7899 : }
7900 6787 : }
7901 :
7902 : /* Make a backward pass through the partitions, accumulating output costs.
7903 : Make a final choice of layout for each partition. */
7904 :
7905 : void
7906 6787 : vect_optimize_slp_pass::backward_pass ()
7907 : {
7908 140753 : for (unsigned int partition_i = m_partitions.length (); partition_i-- > 0;)
7909 : {
7910 127179 : auto &partition = m_partitions[partition_i];
7911 :
7912 127179 : unsigned int min_layout_i = 0;
7913 127179 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7914 394982 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7915 : {
7916 267803 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7917 267803 : if (!layout_costs.is_possible ())
7918 67267 : continue;
7919 :
7920 : /* Accumulate the costs from successor partitions. */
7921 200536 : bool is_possible = true;
7922 434828 : for (unsigned int order_i = partition.node_begin;
7923 434828 : order_i < partition.node_end; ++order_i)
7924 : {
7925 234292 : unsigned int node_i = m_partitioned_nodes[order_i];
7926 234292 : auto &vertex = m_vertices[node_i];
7927 621051 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7928 : {
7929 386759 : auto &other_vertex = m_vertices[other_node_i];
7930 386759 : auto &other_partition = m_partitions[other_vertex.partition];
7931 386759 : if (other_vertex.partition > vertex.partition)
7932 : {
7933 : /* Accumulate the incoming costs from later
7934 : partitions, plus the cost of any layout changes
7935 : on UD itself. */
7936 183126 : auto cost = backward_cost (ud, other_node_i, layout_i);
7937 183126 : if (!cost.is_possible ())
7938 0 : is_possible = false;
7939 : else
7940 183126 : layout_costs.out_cost.add_parallel_cost (cost);
7941 : }
7942 : else
7943 : /* Make sure that earlier partitions can (if necessary
7944 : or beneficial) keep the layout that they chose in
7945 : the forward pass. This ensures that there is at
7946 : least one valid choice of layout. */
7947 203633 : is_possible &= edge_layout_cost (ud, other_node_i,
7948 203633 : other_partition.layout,
7949 203633 : layout_i).is_possible ();
7950 621051 : };
7951 234292 : for_each_partition_edge (node_i, add_cost);
7952 : }
7953 200536 : if (!is_possible)
7954 : {
7955 0 : layout_costs.mark_impossible ();
7956 0 : continue;
7957 : }
7958 :
7959 : /* Locally combine the costs from the forward and backward passes.
7960 : (This combined cost is not passed on, since that would lead
7961 : to double counting.) */
7962 200536 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7963 200536 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7964 200536 : combined_cost.add_serial_cost (layout_costs.out_cost);
7965 :
7966 : /* Record the layout with the lowest cost. Prefer layout 0 in
7967 : the event of a tie between it and another layout. */
7968 200536 : if (!min_layout_cost.is_possible ()
7969 73357 : || combined_cost.is_better_than (min_layout_cost,
7970 : m_optimize_size))
7971 : {
7972 138361 : min_layout_i = layout_i;
7973 138361 : min_layout_cost = combined_cost;
7974 : }
7975 : }
7976 :
7977 127179 : gcc_assert (min_layout_cost.is_possible ());
7978 127179 : partition.layout = min_layout_i;
7979 : }
7980 6787 : }
7981 :
7982 : /* Return a node that applies layout TO_LAYOUT_I to the original form of NODE.
7983 : NODE already has the layout that was selected for its partition. */
7984 :
7985 : slp_tree
7986 177810 : vect_optimize_slp_pass::get_result_with_layout (slp_tree node,
7987 : unsigned int to_layout_i)
7988 : {
7989 177810 : unsigned int result_i = node->vertex * m_perms.length () + to_layout_i;
7990 177810 : slp_tree result = m_node_layouts[result_i];
7991 177810 : if (result)
7992 : return result;
7993 :
7994 176892 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
7995 176892 : || (SLP_TREE_DEF_TYPE (node) == vect_external_def
7996 : /* We can't permute vector defs in place. */
7997 21560 : && SLP_TREE_VEC_DEFS (node).is_empty ()))
7998 : {
7999 : /* If the vector is uniform or unchanged, there's nothing to do. */
8000 47646 : if (to_layout_i == 0 || vect_slp_tree_uniform_p (node))
8001 : result = node;
8002 : else
8003 : {
8004 3697 : auto scalar_ops = SLP_TREE_SCALAR_OPS (node).copy ();
8005 3697 : result = vect_create_new_slp_node (scalar_ops);
8006 3697 : vect_slp_permute (m_perms[to_layout_i], scalar_ops, true);
8007 : }
8008 : }
8009 : else
8010 : {
8011 129246 : unsigned int partition_i = m_vertices[node->vertex].partition;
8012 129246 : unsigned int from_layout_i = m_partitions[partition_i].layout;
8013 129246 : if (from_layout_i == to_layout_i)
8014 127988 : return node;
8015 :
8016 : /* If NODE is itself a VEC_PERM_EXPR, try to create a parallel
8017 : permutation instead of a serial one. Leave the new permutation
8018 : in TMP_PERM on success. */
8019 1258 : auto_lane_permutation_t tmp_perm;
8020 1258 : unsigned int num_inputs = 1;
8021 1258 : if (SLP_TREE_PERMUTE_P (node))
8022 : {
8023 103 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
8024 103 : if (from_layout_i != 0)
8025 43 : vect_slp_permute (m_perms[from_layout_i], tmp_perm, false);
8026 103 : if (to_layout_i != 0)
8027 64 : vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
8028 103 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
8029 : tmp_perm,
8030 103 : SLP_TREE_CHILDREN (node),
8031 : false, nullptr) >= 0)
8032 103 : num_inputs = SLP_TREE_CHILDREN (node).length ();
8033 : else
8034 0 : tmp_perm.truncate (0);
8035 : }
8036 :
8037 1258 : if (dump_enabled_p ())
8038 : {
8039 71 : if (tmp_perm.length () > 0)
8040 6 : dump_printf_loc (MSG_NOTE, vect_location,
8041 : "duplicating permutation node %p with"
8042 : " layout %d\n",
8043 : (void *) node, to_layout_i);
8044 : else
8045 65 : dump_printf_loc (MSG_NOTE, vect_location,
8046 : "inserting permutation node in place of %p\n",
8047 : (void *) node);
8048 : }
8049 :
8050 1258 : unsigned int num_lanes = SLP_TREE_LANES (node);
8051 1258 : result = vect_create_new_slp_node (num_inputs, VEC_PERM_EXPR);
8052 1258 : if (SLP_TREE_SCALAR_STMTS (node).exists ())
8053 : {
8054 1258 : auto &stmts = SLP_TREE_SCALAR_STMTS (result);
8055 1258 : stmts.safe_splice (SLP_TREE_SCALAR_STMTS (node));
8056 1258 : if (from_layout_i != 0)
8057 522 : vect_slp_permute (m_perms[from_layout_i], stmts, false);
8058 1258 : if (to_layout_i != 0)
8059 740 : vect_slp_permute (m_perms[to_layout_i], stmts, true);
8060 : }
8061 1258 : SLP_TREE_LANES (result) = num_lanes;
8062 1258 : SLP_TREE_VECTYPE (result) = SLP_TREE_VECTYPE (node);
8063 1258 : result->vertex = -1;
8064 :
8065 1258 : auto &lane_perm = SLP_TREE_LANE_PERMUTATION (result);
8066 1258 : if (tmp_perm.length ())
8067 : {
8068 103 : lane_perm.safe_splice (tmp_perm);
8069 103 : SLP_TREE_CHILDREN (result).safe_splice (SLP_TREE_CHILDREN (node));
8070 : }
8071 : else
8072 : {
8073 1155 : lane_perm.create (num_lanes);
8074 4702 : for (unsigned j = 0; j < num_lanes; ++j)
8075 2392 : lane_perm.quick_push ({ 0, j });
8076 1155 : if (from_layout_i != 0)
8077 479 : vect_slp_permute (m_perms[from_layout_i], lane_perm, false);
8078 1155 : if (to_layout_i != 0)
8079 676 : vect_slp_permute (m_perms[to_layout_i], lane_perm, true);
8080 1155 : SLP_TREE_CHILDREN (result).safe_push (node);
8081 : }
8082 5104 : for (slp_tree child : SLP_TREE_CHILDREN (result))
8083 1330 : child->refcnt++;
8084 1258 : }
8085 48904 : m_node_layouts[result_i] = result;
8086 48904 : return result;
8087 : }
8088 :
8089 : /* Apply the chosen vector layouts to the SLP graph. */
8090 :
8091 : void
8092 14305 : vect_optimize_slp_pass::materialize ()
8093 : {
8094 : /* We no longer need the costs, so avoid having two O(N * P) arrays
8095 : live at the same time. */
8096 14305 : m_partition_layout_costs.release ();
8097 42915 : m_node_layouts.safe_grow_cleared (m_vertices.length () * m_perms.length ());
8098 :
8099 28610 : auto_sbitmap fully_folded (m_vertices.length ());
8100 14305 : bitmap_clear (fully_folded);
8101 202387 : for (unsigned int node_i : m_partitioned_nodes)
8102 : {
8103 159472 : auto &vertex = m_vertices[node_i];
8104 159472 : slp_tree node = vertex.node;
8105 159472 : int layout_i = m_partitions[vertex.partition].layout;
8106 159472 : gcc_assert (layout_i >= 0);
8107 :
8108 : /* Rearrange the scalar statements to match the chosen layout. */
8109 159472 : if (layout_i > 0)
8110 26717 : vect_slp_permute (m_perms[layout_i],
8111 26717 : SLP_TREE_SCALAR_STMTS (node), true);
8112 :
8113 : /* Update load and lane permutations. */
8114 159472 : if (SLP_TREE_PERMUTE_P (node))
8115 : {
8116 : /* First try to absorb the input vector layouts. If that fails,
8117 : force the inputs to have layout LAYOUT_I too. We checked that
8118 : that was possible before deciding to use nonzero output layouts.
8119 : (Note that at this stage we don't really have any guarantee that
8120 : the target supports the original VEC_PERM_EXPR.) */
8121 8420 : auto &perm = SLP_TREE_LANE_PERMUTATION (node);
8122 8420 : auto_lane_permutation_t tmp_perm;
8123 8420 : tmp_perm.safe_splice (perm);
8124 8420 : change_vec_perm_layout (node, tmp_perm, -1, layout_i);
8125 8420 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
8126 : tmp_perm,
8127 8420 : SLP_TREE_CHILDREN (node),
8128 : false, nullptr) >= 0)
8129 : {
8130 7767 : if (dump_enabled_p ()
8131 8701 : && !std::equal (tmp_perm.begin (), tmp_perm.end (),
8132 : perm.begin ()))
8133 58 : dump_printf_loc (MSG_NOTE, vect_location,
8134 : "absorbing input layouts into %p\n",
8135 : (void *) node);
8136 42804 : std::copy (tmp_perm.begin (), tmp_perm.end (), perm.begin ());
8137 7767 : bitmap_set_bit (fully_folded, node_i);
8138 : }
8139 : else
8140 : {
8141 : /* Not MSG_MISSED because it would make no sense to users. */
8142 653 : if (dump_enabled_p ())
8143 46 : dump_printf_loc (MSG_NOTE, vect_location,
8144 : "failed to absorb input layouts into %p\n",
8145 : (void *) node);
8146 653 : change_vec_perm_layout (nullptr, perm, layout_i, layout_i);
8147 : }
8148 8420 : }
8149 : else
8150 : {
8151 151052 : gcc_assert (!SLP_TREE_LANE_PERMUTATION (node).exists ());
8152 151052 : auto &load_perm = SLP_TREE_LOAD_PERMUTATION (node);
8153 151052 : if (layout_i > 0)
8154 : /* ??? When we handle non-bijective permutes the idea
8155 : is that we can force the load-permutation to be
8156 : { min, min + 1, min + 2, ... max }. But then the
8157 : scalar defs might no longer match the lane content
8158 : which means wrong-code with live lane vectorization.
8159 : So we possibly have to have NULL entries for those. */
8160 25824 : vect_slp_permute (m_perms[layout_i], load_perm, true);
8161 : }
8162 : }
8163 :
8164 : /* Do this before any nodes disappear, since it involves a walk
8165 : over the leaves. */
8166 14305 : remove_redundant_permutations ();
8167 :
8168 : /* Replace each child with a correctly laid-out version. */
8169 202387 : for (unsigned int node_i : m_partitioned_nodes)
8170 : {
8171 : /* Skip nodes that have already been handled above. */
8172 159472 : if (bitmap_bit_p (fully_folded, node_i))
8173 7767 : continue;
8174 :
8175 151705 : auto &vertex = m_vertices[node_i];
8176 151705 : int in_layout_i = m_partitions[vertex.partition].layout;
8177 151705 : gcc_assert (in_layout_i >= 0);
8178 :
8179 : unsigned j;
8180 : slp_tree child;
8181 450376 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (vertex.node), j, child)
8182 : {
8183 182097 : if (!child)
8184 4287 : continue;
8185 :
8186 177810 : slp_tree new_child = get_result_with_layout (child, in_layout_i);
8187 177810 : if (new_child != child)
8188 : {
8189 5541 : vect_free_slp_tree (child);
8190 5541 : SLP_TREE_CHILDREN (vertex.node)[j] = new_child;
8191 5541 : new_child->refcnt += 1;
8192 : }
8193 : }
8194 : }
8195 14305 : }
8196 :
8197 : /* Elide load permutations that are not necessary. Such permutations might
8198 : be pre-existing, rather than created by the layout optimizations. */
8199 :
8200 : void
8201 731233 : vect_optimize_slp_pass::remove_redundant_permutations ()
8202 : {
8203 4811088 : for (unsigned int node_i : m_leafs)
8204 : {
8205 2617389 : slp_tree node = m_vertices[node_i].node;
8206 2617389 : if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
8207 1984080 : continue;
8208 :
8209 : /* In basic block vectorization we allow any subchain of an interleaving
8210 : chain.
8211 : FORNOW: not in loop SLP because of realignment complications. */
8212 633309 : if (is_a <bb_vec_info> (m_vinfo))
8213 : {
8214 195290 : bool subchain_p = true;
8215 : stmt_vec_info next_load_info = NULL;
8216 : stmt_vec_info load_info;
8217 : unsigned j;
8218 195290 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
8219 : {
8220 161642 : if (j != 0
8221 161642 : && (next_load_info != load_info
8222 71365 : || ! load_info
8223 71365 : || DR_GROUP_GAP (load_info) != 1))
8224 : {
8225 : subchain_p = false;
8226 : break;
8227 : }
8228 128797 : next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
8229 : }
8230 66493 : if (subchain_p)
8231 : {
8232 33648 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8233 33648 : continue;
8234 : }
8235 : }
8236 : else
8237 : {
8238 566816 : loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
8239 566816 : bool this_load_permuted = !vect_load_perm_consecutive_p (node, 0);
8240 : /* When this isn't a grouped access we know it's single element
8241 : and contiguous. */
8242 566816 : if (!STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (node)[0]))
8243 : {
8244 463927 : if (!this_load_permuted
8245 463927 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
8246 463149 : || SLP_TREE_LANES (node) == 1))
8247 463167 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8248 463927 : continue;
8249 : }
8250 102889 : stmt_vec_info first_stmt_info
8251 102889 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node)[0]);
8252 103426 : if (!this_load_permuted
8253 : /* The load requires permutation when unrolling exposes
8254 : a gap either because the group is larger than the SLP
8255 : group-size or because there is a gap between the groups. */
8256 102889 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
8257 79520 : || ((SLP_TREE_LANES (node) == DR_GROUP_SIZE (first_stmt_info))
8258 147 : && DR_GROUP_GAP (first_stmt_info) == 0)))
8259 : {
8260 537 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8261 537 : continue;
8262 : }
8263 : }
8264 : }
8265 731233 : }
8266 :
8267 : /* Print the partition graph and layout information to the dump file. */
8268 :
8269 : void
8270 695 : vect_optimize_slp_pass::dump ()
8271 : {
8272 695 : dump_printf_loc (MSG_NOTE, vect_location,
8273 : "SLP optimize permutations:\n");
8274 1403 : for (unsigned int layout_i = 1; layout_i < m_perms.length (); ++layout_i)
8275 : {
8276 708 : dump_printf_loc (MSG_NOTE, vect_location, " %d: { ", layout_i);
8277 708 : const char *sep = "";
8278 5996 : for (unsigned int idx : m_perms[layout_i])
8279 : {
8280 3872 : dump_printf (MSG_NOTE, "%s%d", sep, idx);
8281 3872 : sep = ", ";
8282 : }
8283 708 : dump_printf (MSG_NOTE, " }\n");
8284 : }
8285 695 : dump_printf_loc (MSG_NOTE, vect_location,
8286 : "SLP optimize partitions:\n");
8287 6038 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
8288 : ++partition_i)
8289 : {
8290 5343 : auto &partition = m_partitions[partition_i];
8291 5343 : dump_printf_loc (MSG_NOTE, vect_location, " -------------\n");
8292 5343 : dump_printf_loc (MSG_NOTE, vect_location,
8293 : " partition %d (layout %d):\n",
8294 : partition_i, partition.layout);
8295 5343 : dump_printf_loc (MSG_NOTE, vect_location, " nodes:\n");
8296 10926 : for (unsigned int order_i = partition.node_begin;
8297 10926 : order_i < partition.node_end; ++order_i)
8298 : {
8299 5583 : auto &vertex = m_vertices[m_partitioned_nodes[order_i]];
8300 11166 : dump_printf_loc (MSG_NOTE, vect_location, " - %p:\n",
8301 5583 : (void *) vertex.node);
8302 5583 : dump_printf_loc (MSG_NOTE, vect_location,
8303 : " weight: %f\n",
8304 : vertex.weight.to_double ());
8305 5583 : if (vertex.out_degree)
8306 4378 : dump_printf_loc (MSG_NOTE, vect_location,
8307 : " out weight: %f (degree %d)\n",
8308 : vertex.out_weight.to_double (),
8309 : vertex.out_degree);
8310 5583 : if (SLP_TREE_PERMUTE_P (vertex.node))
8311 513 : dump_printf_loc (MSG_NOTE, vect_location,
8312 : " op: VEC_PERM_EXPR\n");
8313 5070 : else if (auto rep = SLP_TREE_REPRESENTATIVE (vertex.node))
8314 5052 : dump_printf_loc (MSG_NOTE, vect_location,
8315 : " op template: %G", rep->stmt);
8316 : }
8317 5343 : dump_printf_loc (MSG_NOTE, vect_location, " edges:\n");
8318 10926 : for (unsigned int order_i = partition.node_begin;
8319 10926 : order_i < partition.node_end; ++order_i)
8320 : {
8321 5583 : unsigned int node_i = m_partitioned_nodes[order_i];
8322 5583 : auto &vertex = m_vertices[node_i];
8323 16901 : auto print_edge = [&](graph_edge *, unsigned int other_node_i)
8324 : {
8325 11318 : auto &other_vertex = m_vertices[other_node_i];
8326 11318 : if (other_vertex.partition < vertex.partition)
8327 5659 : dump_printf_loc (MSG_NOTE, vect_location,
8328 : " - %p [%d] --> %p\n",
8329 5659 : (void *) other_vertex.node,
8330 : other_vertex.partition,
8331 5659 : (void *) vertex.node);
8332 : else
8333 5659 : dump_printf_loc (MSG_NOTE, vect_location,
8334 : " - %p --> [%d] %p\n",
8335 5659 : (void *) vertex.node,
8336 : other_vertex.partition,
8337 5659 : (void *) other_vertex.node);
8338 16901 : };
8339 5583 : for_each_partition_edge (node_i, print_edge);
8340 : }
8341 :
8342 16228 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
8343 : {
8344 10885 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
8345 10885 : if (layout_costs.is_possible ())
8346 : {
8347 9013 : dump_printf_loc (MSG_NOTE, vect_location,
8348 : " layout %d:%s\n", layout_i,
8349 9013 : partition.layout == int (layout_i)
8350 : ? " (*)" : "");
8351 9013 : slpg_layout_cost combined_cost = layout_costs.in_cost;
8352 9013 : combined_cost.add_serial_cost (layout_costs.internal_cost);
8353 9013 : combined_cost.add_serial_cost (layout_costs.out_cost);
8354 : #define TEMPLATE "{depth: %f, total: %f}"
8355 9013 : dump_printf_loc (MSG_NOTE, vect_location,
8356 : " " TEMPLATE "\n",
8357 : layout_costs.in_cost.depth.to_double (),
8358 : layout_costs.in_cost.total.to_double ());
8359 9013 : dump_printf_loc (MSG_NOTE, vect_location,
8360 : " + " TEMPLATE "\n",
8361 : layout_costs.internal_cost.depth.to_double (),
8362 : layout_costs.internal_cost.total.to_double ());
8363 9013 : dump_printf_loc (MSG_NOTE, vect_location,
8364 : " + " TEMPLATE "\n",
8365 : layout_costs.out_cost.depth.to_double (),
8366 : layout_costs.out_cost.total.to_double ());
8367 9013 : dump_printf_loc (MSG_NOTE, vect_location,
8368 : " = " TEMPLATE "\n",
8369 : combined_cost.depth.to_double (),
8370 : combined_cost.total.to_double ());
8371 : #undef TEMPLATE
8372 : }
8373 : else
8374 1872 : dump_printf_loc (MSG_NOTE, vect_location,
8375 : " layout %d: rejected\n", layout_i);
8376 : }
8377 : }
8378 695 : }
8379 :
8380 : /* Masked load lanes discovery. */
8381 :
8382 : void
8383 731233 : vect_optimize_slp_pass::decide_masked_load_lanes ()
8384 : {
8385 7527804 : for (auto v : m_vertices)
8386 : {
8387 5334105 : slp_tree node = v.node;
8388 5334105 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8389 3765877 : || SLP_TREE_PERMUTE_P (node))
8390 1755044 : continue;
8391 3579061 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
8392 1715751 : if (! STMT_VINFO_GROUPED_ACCESS (stmt_info)
8393 : /* The mask has to be uniform. */
8394 991720 : || STMT_VINFO_SLP_VECT_ONLY (stmt_info)
8395 991609 : || ! is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
8396 3579188 : || ! gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
8397 : IFN_MASK_LOAD))
8398 3578998 : continue;
8399 63 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8400 126 : if (STMT_VINFO_STRIDED_P (stmt_info)
8401 63 : || compare_step_with_zero (m_vinfo, stmt_info) <= 0
8402 123 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (node),
8403 60 : DR_GROUP_SIZE (stmt_info),
8404 : true) == IFN_LAST)
8405 63 : continue;
8406 :
8407 : /* Uniform masks need to be suitably represented. */
8408 0 : slp_tree mask = SLP_TREE_CHILDREN (node)[0];
8409 0 : if (!SLP_TREE_PERMUTE_P (mask)
8410 0 : || SLP_TREE_CHILDREN (mask).length () != 1)
8411 0 : continue;
8412 0 : bool match = true;
8413 0 : for (auto perm : SLP_TREE_LANE_PERMUTATION (mask))
8414 0 : if (perm.first != 0 || perm.second != 0)
8415 : {
8416 : match = false;
8417 : break;
8418 : }
8419 0 : if (!match)
8420 0 : continue;
8421 :
8422 : /* Now see if the consumer side matches. */
8423 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
8424 0 : pred; pred = pred->pred_next)
8425 : {
8426 0 : slp_tree pred_node = m_vertices[pred->src].node;
8427 : /* All consumers should be a permute with a single outgoing lane. */
8428 0 : if (!SLP_TREE_PERMUTE_P (pred_node)
8429 0 : || SLP_TREE_LANES (pred_node) != 1)
8430 : {
8431 : match = false;
8432 : break;
8433 : }
8434 0 : gcc_assert (SLP_TREE_CHILDREN (pred_node).length () == 1);
8435 : }
8436 0 : if (!match)
8437 0 : continue;
8438 : /* Now we can mark the nodes as to use load lanes. */
8439 0 : node->ldst_lanes = true;
8440 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
8441 0 : pred; pred = pred->pred_next)
8442 0 : m_vertices[pred->src].node->ldst_lanes = true;
8443 : /* The catch is we have to massage the mask. We have arranged
8444 : analyzed uniform masks to be represented by a splat VEC_PERM
8445 : which we can now simply elide as we cannot easily re-do SLP
8446 : discovery here. */
8447 0 : slp_tree new_mask = SLP_TREE_CHILDREN (mask)[0];
8448 0 : SLP_TREE_REF_COUNT (new_mask)++;
8449 0 : SLP_TREE_CHILDREN (node)[0] = new_mask;
8450 0 : vect_free_slp_tree (mask);
8451 : }
8452 731233 : }
8453 :
8454 : /* Perform legitimizing attempts. This is intended to improve the
8455 : situation when layout 0 is not valid which is a situation the cost
8456 : based propagation does not handle well.
8457 : Return true if further layout optimization is possible, false if
8458 : the layout configuration should be considered final. */
8459 :
8460 : bool
8461 14305 : vect_optimize_slp_pass::legitimize ()
8462 : {
8463 : /* Perform a very simple legitimizing attempt by attempting to choose
8464 : a single layout for all partitions that will make all permutations
8465 : a noop. That should also be the optimal layout choice in case
8466 : layout zero is legitimate.
8467 : ??? Disconnected components of the SLP graph could have distinct
8468 : single layouts. */
8469 14305 : int single_layout_i = -1;
8470 14305 : unsigned deferred_up_to = -1U;
8471 43896 : for (unsigned partition_i = 0; partition_i < m_partitions.length ();
8472 : ++partition_i)
8473 : {
8474 36361 : auto &partition = m_partitions[partition_i];
8475 36361 : if (single_layout_i == -1)
8476 : {
8477 18387 : single_layout_i = partition.layout;
8478 18387 : deferred_up_to = partition_i;
8479 : }
8480 17974 : else if (partition.layout == single_layout_i || partition.layout == -1)
8481 : ;
8482 : else
8483 : single_layout_i = 0;
8484 33077 : if (single_layout_i == 0)
8485 : return true;
8486 :
8487 29680 : if (single_layout_i != -1
8488 29680 : && !is_compatible_layout (partition, single_layout_i))
8489 : return true;
8490 : }
8491 :
8492 7535 : if (single_layout_i <= 0)
8493 : return true;
8494 :
8495 8166 : for (unsigned partition_i = 0; partition_i < deferred_up_to; ++partition_i)
8496 648 : if (!is_compatible_layout (m_partitions[partition_i],
8497 : single_layout_i))
8498 : return true;
8499 :
8500 22469 : for (unsigned partition_i = 0; partition_i < m_partitions.length ();
8501 : ++partition_i)
8502 : {
8503 14951 : auto &partition = m_partitions[partition_i];
8504 14951 : partition.layout = single_layout_i;
8505 : }
8506 :
8507 : return false;
8508 : }
8509 :
8510 : /* Main entry point for the SLP graph optimization pass. */
8511 :
8512 : void
8513 731233 : vect_optimize_slp_pass::run ()
8514 : {
8515 731233 : build_graph ();
8516 731233 : create_partitions ();
8517 731233 : start_choosing_layouts ();
8518 731233 : if (m_perms.length () > 1)
8519 : {
8520 14305 : if (legitimize ())
8521 : {
8522 6787 : forward_pass ();
8523 6787 : backward_pass ();
8524 : }
8525 14305 : if (dump_enabled_p ())
8526 695 : dump ();
8527 14305 : materialize ();
8528 57848 : while (!m_perms.is_empty ())
8529 29238 : m_perms.pop ().release ();
8530 : }
8531 : else
8532 716928 : remove_redundant_permutations ();
8533 731233 : free_graph (m_slpg);
8534 731233 : build_graph ();
8535 731233 : decide_masked_load_lanes ();
8536 731233 : free_graph (m_slpg);
8537 731233 : }
8538 :
8539 : /* Apply CSE to NODE and its children using BST_MAP. */
8540 :
8541 : static void
8542 5766251 : vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node)
8543 : {
8544 5766251 : bool put_p = false;
8545 5766251 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def
8546 : /* Besides some VEC_PERM_EXPR, two-operator nodes also
8547 : lack scalar stmts and thus CSE doesn't work via bst_map. Ideally
8548 : we'd have sth that works for all internal and external nodes. */
8549 5766251 : && !SLP_TREE_SCALAR_STMTS (node).is_empty ())
8550 : {
8551 4166082 : slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node));
8552 4166082 : if (leader)
8553 : {
8554 : /* We've visited this node already. */
8555 435578 : if (!*leader || *leader == node)
8556 : return;
8557 :
8558 4564 : if (dump_enabled_p ())
8559 985 : dump_printf_loc (MSG_NOTE, vect_location,
8560 : "re-using SLP tree %p for %p\n",
8561 : (void *)*leader, (void *)node);
8562 4564 : vect_free_slp_tree (node);
8563 4564 : (*leader)->refcnt += 1;
8564 4564 : node = *leader;
8565 4564 : return;
8566 : }
8567 :
8568 : /* Avoid creating a cycle by populating the map only after recursion. */
8569 3730504 : bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), nullptr);
8570 3730504 : node->refcnt += 1;
8571 3730504 : put_p = true;
8572 : /* And recurse. */
8573 : }
8574 :
8575 16003652 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
8576 4685083 : if (child)
8577 4215236 : vect_cse_slp_nodes (bst_map, child);
8578 :
8579 : /* Now record the node for CSE in other siblings. */
8580 5330673 : if (put_p)
8581 3730504 : *bst_map->get (SLP_TREE_SCALAR_STMTS (node)) = node;
8582 : }
8583 :
8584 : /* Associate stmts with possible starts of a subset of lanes of NODE
8585 : in PART_STARTS. */
8586 :
8587 : static void
8588 2125600 : vect_cse_gather_part_starts (hash_set<slp_tree> &visited,
8589 : vec<vec<slp_tree>> part_starts, slp_tree node)
8590 : {
8591 : /* CSEing external nodes complicates scheduling since we materialize
8592 : those at the latest position, so avoid that. */
8593 2125600 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8594 2125600 : || visited.add (node))
8595 : return;
8596 :
8597 : /* Besides some VEC_PERM_EXPR, two-operator nodes also lack scalar stmts
8598 : and thus CSE doesn't work. For now gather two-lane aligned starts
8599 : of nodes with a multiple of two number of lanes. */
8600 1146992 : if (!SLP_TREE_SCALAR_STMTS (node).is_empty ()
8601 1139550 : && SLP_TREE_LANES (node) > 2
8602 149249 : && (SLP_TREE_LANES (node) & 1) == 0)
8603 : {
8604 149249 : auto_vec<unsigned, 8> uids;
8605 619277 : for (unsigned i = 0; i < SLP_TREE_LANES (node); i += 2)
8606 : {
8607 470028 : stmt_vec_info s = SLP_TREE_SCALAR_STMTS (node)[i];
8608 470028 : if (!s)
8609 0 : continue;
8610 470028 : unsigned uid = gimple_uid (s->stmt);
8611 470028 : if (!uids.contains (uid))
8612 : {
8613 466377 : uids.safe_push (uid);
8614 466377 : part_starts[uid].safe_push (node);
8615 : }
8616 : }
8617 149249 : }
8618 :
8619 4443611 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
8620 1299819 : if (child)
8621 1299819 : vect_cse_gather_part_starts (visited, part_starts, child);
8622 : }
8623 :
8624 : /* Apply CSE to NODE and its children using lowparts of nodes in BST_MAP. */
8625 :
8626 : static void
8627 2123679 : vect_cse_slp_node_parts (hash_set<slp_tree> &visited,
8628 : const vec<vec<slp_tree>> part_starts,
8629 : vec<slp_tree> &drops, slp_tree node)
8630 : {
8631 2123679 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8632 2123679 : || visited.add (node))
8633 : return;
8634 :
8635 : /* Besides some VEC_PERM_EXPR, two-operator nodes also
8636 : lack scalar stmts and thus CSE doesn't work. */
8637 1146099 : unsigned HOST_WIDE_INT c;
8638 1146099 : if (!SLP_TREE_SCALAR_STMTS (node).is_empty ()
8639 1138661 : && SLP_TREE_SCALAR_STMTS (node)[0]
8640 : /* Avoid touching loads which need care with load permutations
8641 : and specialities like load-lane representations. */
8642 1138661 : && (SLP_TREE_PERMUTE_P (node)
8643 1130919 : || !STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (node))))
8644 34358 : for (slp_tree cand
8645 422067 : : part_starts[gimple_uid (SLP_TREE_SCALAR_STMTS (node)[0]->stmt)])
8646 : /* ??? There is a possible ordering/optimality problem in that
8647 : the CSE then can keep a wider feeding live even though it itself
8648 : becomes dead by means of CSE. Which might be solvable by doing
8649 : the CSE in a wide-to-narrow order. */
8650 34756 : if (SLP_TREE_LANES (cand) > SLP_TREE_LANES (node)
8651 : /* We can do high/lo extracts and full vector copies. */
8652 : && constant_multiple_p
8653 3240 : (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (cand)),
8654 35248 : TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (node)), &c)
8655 37996 : && c <= 2)
8656 : {
8657 2748 : unsigned HOST_WIDE_INT s;
8658 2748 : bool const_p
8659 2748 : = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (node)).is_constant (&s);
8660 2748 : unsigned i;
8661 9975 : for (i = 0; i <= SLP_TREE_LANES (cand) - SLP_TREE_LANES (node);)
8662 : {
8663 : unsigned j;
8664 10854 : for (j = 0; j < SLP_TREE_LANES (node); ++j)
8665 10456 : if (!SLP_TREE_SCALAR_STMTS (node)[j]
8666 10456 : || (SLP_TREE_SCALAR_STMTS (cand)[i+j]
8667 10456 : != SLP_TREE_SCALAR_STMTS (node)[j]))
8668 : break;
8669 7625 : if (j == SLP_TREE_LANES (node))
8670 : break;
8671 7227 : if (!const_p)
8672 : {
8673 : i = SLP_TREE_LANES (cand);
8674 : break;
8675 : }
8676 : /* We can extract only aligned on node vector type boundary. */
8677 7227 : i += s;
8678 : }
8679 2748 : if (i > SLP_TREE_LANES (cand) - SLP_TREE_LANES (node))
8680 2350 : continue;
8681 : /* Found node within cand at i. Put a permute in place
8682 : of it, selecting the subset from cand. */
8683 398 : if (dump_enabled_p ())
8684 41 : dump_printf (MSG_NOTE, "CSEd node %p as %spart of node %p\n",
8685 : (void *)node, i == 0 ? "low" : "high", (void *)cand);
8686 1944 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8687 : /* Delay SLP tree release since we might still reference a node
8688 : from the part_starts map. */
8689 750 : drops.safe_push (child);
8690 398 : SLP_TREE_CHILDREN (node).truncate (1);
8691 398 : SLP_TREE_REF_COUNT (cand)++;
8692 398 : SLP_TREE_CHILDREN (node)[0] = cand;
8693 398 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
8694 398 : SLP_TREE_REPRESENTATIVE (node) = NULL;
8695 398 : SLP_TREE_LANE_PERMUTATION (node).create (SLP_TREE_LANES (node));
8696 1708 : for (unsigned j = i; j < i + SLP_TREE_LANES (node); ++j)
8697 912 : SLP_TREE_LANE_PERMUTATION (node).quick_push (std::make_pair (0, j));
8698 2123679 : return;
8699 : }
8700 :
8701 4438401 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
8702 1297898 : if (child)
8703 1297898 : vect_cse_slp_node_parts (visited, part_starts, drops, child);
8704 : }
8705 :
8706 : /* Optimize the SLP graph of VINFO. */
8707 :
8708 : void
8709 1132291 : vect_optimize_slp (vec_info *vinfo)
8710 : {
8711 1132291 : if (vinfo->slp_instances.is_empty ())
8712 850536 : return;
8713 731233 : vect_optimize_slp_pass (vinfo).run ();
8714 :
8715 : /* Apply CSE again to nodes after permute optimization. */
8716 731233 : scalar_stmts_to_slp_tree_map_t *bst_map
8717 731233 : = new scalar_stmts_to_slp_tree_map_t ();
8718 :
8719 3744714 : for (auto inst : vinfo->slp_instances)
8720 1551015 : vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst));
8721 :
8722 731233 : release_scalar_stmts_to_slp_tree_map (bst_map);
8723 :
8724 731233 : if (!is_a <bb_vec_info> (vinfo))
8725 : return;
8726 :
8727 : /* Attempt to merge SLP sub-graphs that intersect in low or highparts of
8728 : each other. Build the reverse mapping from stmt to SLP node for
8729 : lanes starting at the low or high part.
8730 : ??? In the future we can extend this to do a two-step permute
8731 : and extract or extract and permute to put the high/low part in
8732 : place on the original vector or permute the hogh/low part to
8733 : match up the target lane order. */
8734 281755 : hash_set<slp_tree> visited;
8735 281755 : vec<vec<slp_tree>> start_for_part;
8736 563510 : start_for_part.create (vinfo->stmt_vec_infos.length () + 1);
8737 563510 : start_for_part.quick_grow_cleared (vinfo->stmt_vec_infos.length () + 1);
8738 1671046 : for (auto inst : vinfo->slp_instances)
8739 825781 : vect_cse_gather_part_starts (visited,
8740 : start_for_part, SLP_INSTANCE_TREE (inst));
8741 :
8742 : /* Now replace low/highpart copies with extracting permutes. */
8743 281755 : auto_vec<slp_tree> drops;
8744 281755 : visited.empty ();
8745 1671046 : for (auto inst : vinfo->slp_instances)
8746 825781 : vect_cse_slp_node_parts (visited, start_for_part, drops,
8747 : SLP_INSTANCE_TREE (inst));
8748 :
8749 : /* Now perform delayed releases of nodes. */
8750 282915 : for (slp_tree node : drops)
8751 750 : vect_free_slp_tree (node);
8752 :
8753 24197465 : for (auto v : start_for_part)
8754 23352200 : v.release ();
8755 281755 : start_for_part.release ();
8756 281755 : }
8757 :
8758 : /* Gather loads reachable from the individual SLP graph entries. */
8759 :
8760 : void
8761 1132291 : vect_gather_slp_loads (vec_info *vinfo)
8762 : {
8763 1132291 : unsigned i;
8764 1132291 : slp_instance instance;
8765 2683306 : FOR_EACH_VEC_ELT (vinfo->slp_instances, i, instance)
8766 : {
8767 1551015 : hash_set<slp_tree> visited;
8768 1551015 : vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance),
8769 : SLP_INSTANCE_TREE (instance), visited);
8770 1551015 : }
8771 1132291 : }
8772 :
8773 : /* For NODE update VF based on the number of lanes and the vector types
8774 : used. */
8775 :
8776 : static void
8777 4660308 : vect_update_slp_vf_for_node (slp_tree node, poly_uint64 &vf,
8778 : hash_set<slp_tree> &visited)
8779 : {
8780 4660308 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
8781 1677228 : return;
8782 3375855 : if (visited.add (node))
8783 : return;
8784 :
8785 11372643 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8786 3836501 : vect_update_slp_vf_for_node (child, vf, visited);
8787 :
8788 : /* We do not visit SLP nodes for constants or externals - those neither
8789 : have a vector type set yet (vectorizable_* does this).
8790 : Note that when we stop using fixed size vectors externs and constants
8791 : shouldn't influence the (minimum) vectorization factor, instead
8792 : vectorizable_* should honor the vectorization factor when trying to
8793 : assign vector types to constants and externals and cause iteration
8794 : to a higher vectorization factor when required. */
8795 2983110 : tree vectype = SLP_TREE_VECTYPE (node);
8796 2983110 : if (!vectype)
8797 : /* OMP SIMD calls w/o LHS have no SLP_TREE_VECTYPE set. */
8798 : return;
8799 2983080 : poly_uint64 node_vf
8800 2983080 : = calculate_unrolling_factor (TYPE_VECTOR_SUBPARTS (vectype),
8801 : SLP_TREE_LANES (node));
8802 2983080 : vf = force_common_multiple (vf, node_vf);
8803 :
8804 : /* For permute nodes that are fed from externs or constants we have to
8805 : consider their number of lanes as well. Likewise for store-lanes. */
8806 2983080 : if (SLP_TREE_PERMUTE_P (node) || node->ldst_lanes)
8807 902066 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8808 240935 : if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
8809 : {
8810 3708 : poly_uint64 child_vf
8811 3708 : = calculate_unrolling_factor (TYPE_VECTOR_SUBPARTS (vectype),
8812 : SLP_TREE_LANES (child));
8813 3708 : vf = force_common_multiple (vf, child_vf);
8814 : }
8815 : }
8816 :
8817 : /* For each possible SLP instance decide whether to SLP it and calculate overall
8818 : unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
8819 : least one instance. */
8820 :
8821 : bool
8822 519733 : vect_make_slp_decision (loop_vec_info loop_vinfo)
8823 : {
8824 519733 : unsigned int i;
8825 519733 : poly_uint64 unrolling_factor = 1;
8826 519733 : const vec<slp_instance> &slp_instances
8827 : = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
8828 519733 : slp_instance instance;
8829 519733 : int decided_to_slp = 0;
8830 :
8831 519733 : DUMP_VECT_SCOPE ("vect_make_slp_decision");
8832 :
8833 519733 : hash_set<slp_tree> visited;
8834 1863273 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
8835 : {
8836 823807 : slp_tree root = SLP_INSTANCE_TREE (instance);
8837 :
8838 : /* All unroll factors have the form:
8839 :
8840 : GET_MODE_SIZE (vinfo->vector_mode) * X
8841 :
8842 : for some rational X, so they must have a common multiple. */
8843 823807 : vect_update_slp_vf_for_node (root, unrolling_factor, visited);
8844 :
8845 : /* If all instances ended up with vector(1) T roots make sure to
8846 : not vectorize. RVV for example relies on loop vectorization
8847 : when some instances are essentially kept scalar. See PR121048. */
8848 823807 : if (SLP_TREE_VECTYPE (root)
8849 823807 : && known_gt (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (root)), 1U))
8850 668146 : decided_to_slp++;
8851 : }
8852 :
8853 519733 : LOOP_VINFO_VECT_FACTOR (loop_vinfo) = unrolling_factor;
8854 :
8855 519733 : if (decided_to_slp && dump_enabled_p ())
8856 : {
8857 21388 : dump_printf_loc (MSG_NOTE, vect_location,
8858 : "Decided to SLP %d instances. Unrolling factor ",
8859 : decided_to_slp);
8860 21388 : dump_dec (MSG_NOTE, unrolling_factor);
8861 21388 : dump_printf (MSG_NOTE, "\n");
8862 : }
8863 :
8864 519733 : return (decided_to_slp > 0);
8865 519733 : }
8866 :
8867 : /* Initialize a bb_vec_info struct for the statements in BBS basic blocks. */
8868 :
8869 2314427 : _bb_vec_info::_bb_vec_info (vec<basic_block> _bbs, vec_info_shared *shared)
8870 : : vec_info (vec_info::bb, shared),
8871 2314427 : roots (vNULL)
8872 : {
8873 : /* The region we are operating on. bbs[0] is the entry, excluding
8874 : its PHI nodes. In the future we might want to track an explicit
8875 : entry edge to cover bbs[0] PHI nodes and have a region entry
8876 : insert location. */
8877 2314427 : bbs = _bbs.address ();
8878 2314427 : nbbs = _bbs.length ();
8879 :
8880 18563199 : for (unsigned i = 0; i < nbbs; ++i)
8881 : {
8882 16248772 : if (i != 0)
8883 21253809 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8884 7319464 : gsi_next (&si))
8885 : {
8886 7319464 : gphi *phi = si.phi ();
8887 7319464 : gimple_set_uid (phi, 0);
8888 7319464 : add_stmt (phi);
8889 : }
8890 32497544 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8891 147210189 : !gsi_end_p (gsi); gsi_next (&gsi))
8892 : {
8893 130961417 : gimple *stmt = gsi_stmt (gsi);
8894 130961417 : gimple_set_uid (stmt, 0);
8895 130961417 : if (is_gimple_debug (stmt) || is_a <glabel *> (stmt))
8896 83414232 : continue;
8897 47547185 : add_stmt (stmt);
8898 : }
8899 : }
8900 2314427 : }
8901 :
8902 :
8903 : /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
8904 : stmts in the basic block. */
8905 :
8906 2314427 : _bb_vec_info::~_bb_vec_info ()
8907 : {
8908 : /* Reset region marker. */
8909 18563199 : for (unsigned i = 0; i < nbbs; ++i)
8910 : {
8911 16248772 : if (i != 0)
8912 21267625 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8913 7333280 : gsi_next (&si))
8914 : {
8915 7333280 : gphi *phi = si.phi ();
8916 7333280 : gimple_set_uid (phi, -1);
8917 : }
8918 32497544 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8919 147084377 : !gsi_end_p (gsi); gsi_next (&gsi))
8920 : {
8921 130835605 : gimple *stmt = gsi_stmt (gsi);
8922 130835605 : gimple_set_uid (stmt, -1);
8923 : }
8924 : }
8925 :
8926 3731043 : for (unsigned i = 0; i < roots.length (); ++i)
8927 : {
8928 1416616 : roots[i].stmts.release ();
8929 1416616 : roots[i].roots.release ();
8930 1416616 : roots[i].remain.release ();
8931 : }
8932 2314427 : roots.release ();
8933 2314427 : }
8934 :
8935 : /* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
8936 : given then that child nodes have already been processed, and that
8937 : their def types currently match their SLP node's def type. */
8938 :
8939 : static bool
8940 2761323 : vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
8941 : slp_instance node_instance,
8942 : stmt_vector_for_cost *cost_vec)
8943 : {
8944 : /* Handle purely internal nodes. */
8945 2761323 : if (SLP_TREE_PERMUTE_P (node))
8946 : {
8947 83395 : gcc_checking_assert (!SLP_TREE_REPRESENTATIVE (node));
8948 83395 : if (!vectorizable_slp_permutation (vinfo, NULL, node, cost_vec))
8949 : return false;
8950 :
8951 : stmt_vec_info slp_stmt_info;
8952 : unsigned int i;
8953 205685 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
8954 : {
8955 123929 : if (slp_stmt_info
8956 117854 : && STMT_VINFO_LIVE_P (slp_stmt_info)
8957 123929 : && !vectorizable_live_operation (vinfo, slp_stmt_info, node,
8958 : node_instance, i,
8959 : false, cost_vec))
8960 : return false;
8961 : }
8962 81756 : SLP_TREE_TYPE (node) = permute_info_type;
8963 81756 : return true;
8964 : }
8965 :
8966 2677928 : return vect_analyze_stmt (vinfo, node, node_instance, cost_vec);
8967 : }
8968 :
8969 : static int
8970 1098101 : sort_ints (const void *a_, const void *b_)
8971 : {
8972 1098101 : int a = *(const int *)a_;
8973 1098101 : int b = *(const int *)b_;
8974 1098101 : return a - b;
8975 : }
8976 :
8977 : /* Verify if we can externalize a set of internal defs. */
8978 :
8979 : static bool
8980 427221 : vect_slp_can_convert_to_external (const vec<stmt_vec_info> &stmts)
8981 : {
8982 : /* Constant generation uses get_later_stmt which can only handle
8983 : defs from the same BB or a set of defs that can be ordered
8984 : with a dominance query. */
8985 427221 : basic_block bb = NULL;
8986 427221 : bool all_same = true;
8987 427221 : auto_vec<int> bbs;
8988 854442 : bbs.reserve_exact (stmts.length ());
8989 2284913 : for (stmt_vec_info stmt : stmts)
8990 : {
8991 1003250 : if (!stmt)
8992 : return false;
8993 1003250 : else if (!bb)
8994 427221 : bb = gimple_bb (stmt->stmt);
8995 576029 : else if (gimple_bb (stmt->stmt) != bb)
8996 174342 : all_same = false;
8997 1003250 : bbs.quick_push (gimple_bb (stmt->stmt)->index);
8998 : }
8999 427221 : if (all_same)
9000 : return true;
9001 :
9002 : /* Produce a vector of unique BB indexes for the defs. */
9003 144397 : bbs.qsort (sort_ints);
9004 144397 : unsigned i, j;
9005 328125 : for (i = 1, j = 1; i < bbs.length (); ++i)
9006 183728 : if (bbs[i] != bbs[j-1])
9007 155947 : bbs[j++] = bbs[i];
9008 144397 : gcc_assert (j >= 2);
9009 144397 : bbs.truncate (j);
9010 :
9011 288794 : if (bbs.length () == 2)
9012 136450 : return (dominated_by_p (CDI_DOMINATORS,
9013 136450 : BASIC_BLOCK_FOR_FN (cfun, bbs[0]),
9014 136450 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]))
9015 266579 : || dominated_by_p (CDI_DOMINATORS,
9016 130129 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]),
9017 130129 : BASIC_BLOCK_FOR_FN (cfun, bbs[0])));
9018 :
9019 : /* ??? For more than two BBs we can sort the vector and verify the
9020 : result is a total order. But we can't use vec::qsort with a
9021 : compare function using a dominance query since there's no way to
9022 : signal failure and any fallback for an unordered pair would
9023 : fail qsort_chk later.
9024 : For now simply hope that ordering after BB index provides the
9025 : best candidate total order. If required we can implement our
9026 : own mergesort or export an entry without checking. */
9027 454383 : for (unsigned i = 1; i < bbs.length (); ++i)
9028 19372 : if (!dominated_by_p (CDI_DOMINATORS,
9029 19372 : BASIC_BLOCK_FOR_FN (cfun, bbs[i]),
9030 19372 : BASIC_BLOCK_FOR_FN (cfun, bbs[i-1])))
9031 : return false;
9032 :
9033 : return true;
9034 427221 : }
9035 :
9036 : /* Try to build NODE from scalars, returning true on success.
9037 : NODE_INSTANCE is the SLP instance that contains NODE. */
9038 :
9039 : static bool
9040 649907 : vect_slp_convert_to_external (vec_info *vinfo, slp_tree node,
9041 : slp_instance node_instance)
9042 : {
9043 649907 : stmt_vec_info stmt_info;
9044 649907 : unsigned int i;
9045 :
9046 649907 : if (!is_a <bb_vec_info> (vinfo)
9047 83989 : || node == SLP_INSTANCE_TREE (node_instance)
9048 24772 : || !SLP_TREE_SCALAR_STMTS (node).exists ()
9049 24733 : || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node))
9050 : /* Force the mask use to be built from scalars instead. */
9051 23118 : || VECTOR_BOOLEAN_TYPE_P (SLP_TREE_VECTYPE (node))
9052 672776 : || !vect_slp_can_convert_to_external (SLP_TREE_SCALAR_STMTS (node)))
9053 : return false;
9054 :
9055 22869 : if (dump_enabled_p ())
9056 103 : dump_printf_loc (MSG_NOTE, vect_location,
9057 : "Building vector operands of %p from scalars instead\n",
9058 : (void *) node);
9059 :
9060 : /* Don't remove and free the child nodes here, since they could be
9061 : referenced by other structures. The analysis and scheduling phases
9062 : (need to) ignore child nodes of anything that isn't vect_internal_def. */
9063 22869 : unsigned int group_size = SLP_TREE_LANES (node);
9064 22869 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
9065 : /* Invariants get their vector type from the uses. */
9066 22869 : SLP_TREE_VECTYPE (node) = NULL_TREE;
9067 22869 : SLP_TREE_SCALAR_OPS (node).safe_grow (group_size, true);
9068 22869 : SLP_TREE_LOAD_PERMUTATION (node).release ();
9069 103514 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9070 : {
9071 57776 : tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
9072 57776 : SLP_TREE_SCALAR_OPS (node)[i] = lhs;
9073 : }
9074 : return true;
9075 : }
9076 :
9077 : /* Return true if all elements of the slice are the same. */
9078 : bool
9079 436406 : vect_scalar_ops_slice::all_same_p () const
9080 : {
9081 488039 : for (unsigned int i = 1; i < length; ++i)
9082 399280 : if (!operand_equal_p (op (0), op (i)))
9083 : return false;
9084 : return true;
9085 : }
9086 :
9087 : hashval_t
9088 422188 : vect_scalar_ops_slice_hash::hash (const value_type &s)
9089 : {
9090 422188 : hashval_t hash = 0;
9091 1614705 : for (unsigned i = 0; i < s.length; ++i)
9092 1192517 : hash = iterative_hash_expr (s.op (i), hash);
9093 422188 : return hash;
9094 : }
9095 :
9096 : bool
9097 233987 : vect_scalar_ops_slice_hash::equal (const value_type &s1,
9098 : const compare_type &s2)
9099 : {
9100 233987 : if (s1.length != s2.length)
9101 : return false;
9102 404819 : for (unsigned i = 0; i < s1.length; ++i)
9103 353037 : if (!operand_equal_p (s1.op (i), s2.op (i)))
9104 : return false;
9105 : return true;
9106 : }
9107 :
9108 : /* Like vect_get_num_copies but N copies of the vector might have
9109 : excess elements in the last vector. Stores the number of excess
9110 : elements in the last vector in *EXCESS_ELTS. For BB SLP with a
9111 : variable-length vector type, use the minimum vector length to
9112 : calculate the minimum number of excess elements.
9113 : Returns false if *NVECTORS or *EXCESS_ELTS cannot be computed. */
9114 :
9115 : bool
9116 1607911 : vect_get_num_copies_for_invariant (vec_info *vinfo, slp_tree node,
9117 : unsigned *nvectors,
9118 : unsigned *excess_elts)
9119 : {
9120 1607911 : tree vectype = SLP_TREE_VECTYPE (node);
9121 1607911 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9122 1607911 : poly_uint64 nelts;
9123 :
9124 1607911 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
9125 394423 : nelts = LOOP_VINFO_VECT_FACTOR (loop_vinfo) * SLP_TREE_LANES (node);
9126 : else
9127 : {
9128 1213488 : unsigned int group_size = SLP_TREE_LANES (node);
9129 1213488 : if (!nunits.is_constant () && known_le (group_size, nunits))
9130 : {
9131 : *nvectors = 1;
9132 : *excess_elts = constant_lower_bound (nunits) - group_size;
9133 : return true;
9134 : }
9135 1213488 : nelts = group_size;
9136 : }
9137 :
9138 1607911 : if (can_div_away_from_zero_p (nelts, nunits, nvectors))
9139 : {
9140 1607911 : poly_uint64 excess = (nunits * *nvectors) - nelts;
9141 1607911 : uint64_t const_excess;
9142 1607911 : if (excess.is_constant (&const_excess))
9143 : {
9144 1607911 : *excess_elts = const_excess;
9145 1607911 : return true;
9146 : }
9147 : }
9148 :
9149 : return false;
9150 : }
9151 :
9152 : /* Compute the prologue cost for invariant or constant operands represented
9153 : by NODE. */
9154 :
9155 : static void
9156 1099000 : vect_prologue_cost_for_slp (slp_tree node, unsigned nvectors,
9157 : stmt_vector_for_cost *cost_vec)
9158 : {
9159 : /* There's a special case of an existing vector, that costs nothing. */
9160 1099000 : if (SLP_TREE_SCALAR_OPS (node).length () == 0
9161 1099000 : && !SLP_TREE_VEC_DEFS (node).is_empty ())
9162 2359 : return;
9163 : /* Without looking at the actual initializer a vector of
9164 : constants can be implemented as load from the constant pool.
9165 : When all elements are the same we can use a splat. */
9166 1096641 : tree vectype = SLP_TREE_VECTYPE (node);
9167 1096641 : unsigned group_size = SLP_TREE_LANES (node);
9168 1096641 : unsigned HOST_WIDE_INT const_nunits;
9169 1096641 : unsigned nelt_limit;
9170 1096641 : auto ops = &SLP_TREE_SCALAR_OPS (node);
9171 1096641 : auto_vec<unsigned int> starts (nvectors);
9172 1096641 : if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
9173 1096641 : && ! multiple_p (const_nunits, group_size))
9174 : {
9175 65841 : nelt_limit = const_nunits;
9176 65841 : hash_set<vect_scalar_ops_slice_hash> vector_ops;
9177 340458 : for (unsigned int i = 0; i < nvectors; ++i)
9178 208776 : if (!vector_ops.add ({ ops, i * nelt_limit, nelt_limit }))
9179 156994 : starts.quick_push (i * nelt_limit);
9180 65841 : }
9181 : else
9182 : {
9183 : /* If either the vector has variable length or the vectors
9184 : are composed of repeated whole groups we only need to
9185 : cost construction once. All vectors will be the same. */
9186 1030800 : nelt_limit = group_size;
9187 1030800 : starts.quick_push (0);
9188 : }
9189 : /* ??? We're just tracking whether vectors in a single node are the same.
9190 : Ideally we'd do something more global. */
9191 4477717 : for (unsigned int start : starts)
9192 : {
9193 1187794 : vect_cost_for_stmt kind;
9194 1187794 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
9195 : kind = vector_load;
9196 436406 : else if (vect_scalar_ops_slice { ops, start, nelt_limit }.all_same_p ())
9197 : kind = scalar_to_vec;
9198 : else
9199 347647 : kind = vec_construct;
9200 1187794 : record_stmt_cost (cost_vec, 1, kind, nullptr, node,
9201 : vectype, 0, vect_prologue);
9202 : }
9203 1096641 : }
9204 :
9205 : /* Analyze statements contained in SLP tree NODE after recursively analyzing
9206 : the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
9207 :
9208 : Return true if the operations are supported. */
9209 :
9210 : static bool
9211 5110377 : vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
9212 : slp_instance node_instance,
9213 : hash_set<slp_tree> &visited_set,
9214 : vec<slp_tree> &visited_vec,
9215 : stmt_vector_for_cost *cost_vec)
9216 : {
9217 5110377 : int i, j;
9218 5110377 : slp_tree child;
9219 :
9220 : /* Costing and analysis of invariants is delayed. */
9221 5110377 : if (!node
9222 4711914 : || SLP_TREE_DEF_TYPE (node) == vect_constant_def
9223 3902075 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
9224 : return true;
9225 :
9226 3426154 : if (SLP_TREE_DEF_TYPE (node) == vect_uninitialized_def)
9227 : {
9228 5 : if (dump_enabled_p ())
9229 0 : dump_printf_loc (MSG_NOTE, vect_location,
9230 : "Failed cyclic SLP reference in %p\n", (void *) node);
9231 : return false;
9232 : }
9233 3426149 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_internal_def);
9234 :
9235 : /* If we already analyzed the exact same set of scalar stmts we're done.
9236 : We share the generated vector stmts for those. */
9237 3426149 : if (visited_set.add (node))
9238 : return true;
9239 3130860 : visited_vec.safe_push (node);
9240 :
9241 3130860 : bool res = true;
9242 3130860 : unsigned visited_rec_start = visited_vec.length ();
9243 3130860 : unsigned cost_vec_rec_start = cost_vec->length ();
9244 3130860 : bool seen_non_constant_child = false;
9245 9047383 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9246 : {
9247 3733328 : res = vect_slp_analyze_node_operations (vinfo, child, node_instance,
9248 : visited_set, visited_vec,
9249 : cost_vec);
9250 3733328 : if (!res)
9251 : break;
9252 3364005 : if (child && SLP_TREE_DEF_TYPE (child) != vect_constant_def)
9253 3364005 : seen_non_constant_child = true;
9254 : }
9255 : /* We're having difficulties scheduling nodes with just constant
9256 : operands and no scalar stmts since we then cannot compute a stmt
9257 : insertion place. */
9258 3130860 : if (res
9259 3130860 : && !seen_non_constant_child
9260 3130860 : && SLP_TREE_SCALAR_STMTS (node).is_empty ())
9261 : {
9262 214 : if (dump_enabled_p ())
9263 6 : dump_printf_loc (MSG_NOTE, vect_location,
9264 : "Cannot vectorize all-constant op node %p\n",
9265 : (void *) node);
9266 : res = false;
9267 : }
9268 :
9269 3130652 : if (res)
9270 2761323 : res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
9271 : cost_vec);
9272 : /* If analysis failed we have to pop all recursive visited nodes
9273 : plus ourselves. */
9274 3130860 : if (!res)
9275 : {
9276 3202518 : while (visited_vec.length () >= visited_rec_start)
9277 : {
9278 951352 : slp_tree nd = visited_vec.pop ();
9279 951352 : visited_set.remove (nd);
9280 951352 : SLP_TREE_TYPE (nd) = undef_vec_info_type;
9281 951352 : delete nd->data;
9282 951352 : nd->data = nullptr;
9283 : }
9284 649907 : cost_vec->truncate (cost_vec_rec_start);
9285 : }
9286 :
9287 : /* When the node can be vectorized cost invariant nodes it references.
9288 : This is not done in DFS order to allow the referring node
9289 : vectorizable_* calls to nail down the invariant nodes vector type
9290 : and possibly unshare it if it needs a different vector type than
9291 : other referrers. */
9292 3130860 : if (res)
9293 9502039 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
9294 3008289 : if (child
9295 2682878 : && (SLP_TREE_DEF_TYPE (child) == vect_constant_def
9296 2682878 : || SLP_TREE_DEF_TYPE (child) == vect_external_def)
9297 : /* Perform usual caching, note code-generation still
9298 : code-gens these nodes multiple times but we expect
9299 : to CSE them later. */
9300 4153608 : && !visited_set.add (child))
9301 : {
9302 1142332 : visited_vec.safe_push (child);
9303 : /* ??? After auditing more code paths make a "default"
9304 : and push the vector type from NODE to all children
9305 : if it is not already set. */
9306 : /* Compute the number of vectors to be generated. */
9307 1142332 : tree vector_type = SLP_TREE_VECTYPE (child);
9308 1142332 : if (!vector_type)
9309 : {
9310 : /* Masked loads can have an undefined (default SSA definition)
9311 : else operand. We do not need to cost it. */
9312 43332 : vec<tree> ops = SLP_TREE_SCALAR_OPS (child);
9313 44812 : if (SLP_TREE_TYPE (node) == load_vec_info_type
9314 44812 : && ((ops.length ()
9315 1480 : && TREE_CODE (ops[0]) == SSA_NAME
9316 0 : && SSA_NAME_IS_DEFAULT_DEF (ops[0])
9317 0 : && VAR_P (SSA_NAME_VAR (ops[0])))
9318 1480 : || SLP_TREE_DEF_TYPE (child) == vect_constant_def))
9319 1480 : continue;
9320 :
9321 : /* For shifts with a scalar argument we don't need
9322 : to cost or code-generate anything.
9323 : ??? Represent this more explicitly. */
9324 41852 : gcc_assert (SLP_TREE_TYPE (node) == shift_vec_info_type
9325 : && j == 1);
9326 41852 : continue;
9327 41852 : }
9328 :
9329 : /* Make sure we can generate them and then cost them. */
9330 1099000 : unsigned nvectors, excess_elts;
9331 1099000 : if (!vect_get_num_copies_for_invariant (vinfo, child, &nvectors,
9332 : &excess_elts))
9333 0 : return false;
9334 1099000 : vect_prologue_cost_for_slp (child, nvectors, cost_vec);
9335 : }
9336 :
9337 : /* If this node or any of its children can't be vectorized, try pruning
9338 : the tree here rather than felling the whole thing. */
9339 649907 : if (!res && vect_slp_convert_to_external (vinfo, node, node_instance))
9340 : {
9341 : /* We'll need to revisit this for invariant costing and number
9342 : of vectorized stmt setting. */
9343 : res = true;
9344 : }
9345 :
9346 : return res;
9347 : }
9348 :
9349 : /* Mark lanes of NODE that are live outside of the basic-block vectorized
9350 : region and that can be vectorized using vectorizable_live_operation
9351 : with STMT_VINFO_LIVE_P. Not handled live operations will cause the
9352 : scalar code computing it to be retained. */
9353 :
9354 : static void
9355 1004709 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo, slp_tree node,
9356 : slp_instance instance,
9357 : stmt_vector_for_cost *cost_vec,
9358 : hash_set<stmt_vec_info> &svisited,
9359 : hash_set<slp_tree> &visited)
9360 : {
9361 1004709 : if (visited.add (node))
9362 58716 : return;
9363 :
9364 : unsigned i;
9365 : stmt_vec_info stmt_info;
9366 : gimple *last_stmt = NULL;
9367 3388311 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9368 : {
9369 2442318 : if (!stmt_info || svisited.contains (stmt_info))
9370 94312 : continue;
9371 2406540 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
9372 2406540 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info)
9373 38620 : && STMT_VINFO_RELATED_STMT (orig_stmt_info) != stmt_info)
9374 : /* Only the pattern root stmt computes the original scalar value. */
9375 28193 : continue;
9376 2378347 : if (!PURE_SLP_STMT (orig_stmt_info))
9377 : /* Iff the stmt is not part of the vector coverage because it or
9378 : uses of it are used by SLP graph leafs as extern input there is
9379 : no point in trying to live code-generate from a vector stmt as
9380 : the scalar stmt will survive anyway. */
9381 30341 : continue;
9382 2348006 : bool mark_visited = true;
9383 2348006 : gimple *orig_stmt = orig_stmt_info->stmt;
9384 2348006 : ssa_op_iter op_iter;
9385 2348006 : def_operand_p def_p;
9386 5287402 : FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_stmt, op_iter, SSA_OP_DEF)
9387 : {
9388 : /* We have to verify whether we can insert the lane extract
9389 : before all uses. The following is a conservative approximation.
9390 : We cannot put this into vectorizable_live_operation because
9391 : iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
9392 : doesn't work.
9393 : Note that while the fact that we emit code for loads at the
9394 : first load should make this a non-problem leafs we construct
9395 : from scalars are vectorized after the last scalar def.
9396 : ??? If we'd actually compute the insert location during
9397 : analysis we could use sth less conservative than the last
9398 : scalar stmt in the node for the dominance check. */
9399 : /* ??? What remains is "live" uses in vector CTORs in the same
9400 : SLP graph which is where those uses can end up code-generated
9401 : right after their definition instead of close to their original
9402 : use. But that would restrict us to code-generate lane-extracts
9403 : from the latest stmt in a node. So we compensate for this
9404 : during code-generation, simply not replacing uses for those
9405 : hopefully rare cases. */
9406 591390 : imm_use_iterator use_iter;
9407 :
9408 591390 : bool live_p = false;
9409 591390 : bool can_insert = true;
9410 591390 : use_operand_p use_p;
9411 1765203 : FOR_EACH_IMM_USE_FAST (use_p, use_iter, DEF_FROM_PTR (def_p))
9412 : {
9413 1181655 : gimple *use_stmt = USE_STMT (use_p);
9414 1181655 : stmt_vec_info use_stmt_info;
9415 1181655 : if (!(!is_gimple_debug (use_stmt)
9416 914318 : && (!(use_stmt_info = bb_vinfo->lookup_stmt (use_stmt))
9417 902876 : || !PURE_SLP_STMT (use_stmt_info))))
9418 962936 : continue;
9419 218719 : live_p = true;
9420 218719 : if (!last_stmt)
9421 68872 : last_stmt
9422 69159 : = (node->si ? node->si
9423 287 : : vect_find_last_scalar_stmt_in_slp (node)->stmt);
9424 218719 : if (is_a <gphi *> (use_stmt))
9425 : {
9426 76624 : if (!dominated_by_p (CDI_DOMINATORS,
9427 38312 : phi_arg_edge_from_use (use_p)->src,
9428 38312 : gimple_bb (last_stmt)))
9429 : can_insert = false;
9430 : }
9431 : /* As we instert after last_stmt it may not be the use_stmt
9432 : itself. */
9433 180407 : else if (last_stmt == use_stmt
9434 180407 : || !vect_stmt_dominates_stmt_p (last_stmt, use_stmt))
9435 : can_insert = false;
9436 : if (!can_insert)
9437 : {
9438 7842 : if (dump_enabled_p ())
9439 80 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9440 : "Cannot determine insertion place for "
9441 : "lane extract of %T at node %p\n",
9442 : DEF_FROM_PTR (def_p), (void *)node);
9443 : can_insert = false;
9444 : break;
9445 : }
9446 591390 : }
9447 591390 : if (live_p && can_insert)
9448 : {
9449 : /* Only record a live stmt when we can replace all uses. We
9450 : record from which SLP tree we vectorize the uses, so we'll
9451 : cost once and can deal with the case that not all SLP nodes
9452 : may be suitable for code-generation of all live uses.
9453 : ??? But we never split up the work between multiple SLP
9454 : nodes. */
9455 109965 : STMT_VINFO_LIVE_P (stmt_info) = true;
9456 109965 : if (!vectorizable_live_operation (bb_vinfo, stmt_info, node,
9457 : instance, i, false, cost_vec))
9458 : {
9459 0 : STMT_VINFO_LIVE_P (stmt_info) = false;
9460 0 : mark_visited = false;
9461 : }
9462 : }
9463 : }
9464 2348006 : if (mark_visited)
9465 2348006 : svisited.add (stmt_info);
9466 : }
9467 :
9468 : slp_tree child;
9469 2756723 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9470 985540 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9471 289382 : vect_bb_slp_mark_live_stmts (bb_vinfo, child, instance, cost_vec,
9472 : svisited, visited);
9473 : }
9474 :
9475 : /* Traverse all slp instances of BB_VINFO, and mark lanes of every node that
9476 : are live outside of the basic-block vectorized region and that can be
9477 : vectorized using vectorizable_live_operation with STMT_VINFO_LIVE_P. */
9478 :
9479 : static void
9480 246787 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo)
9481 : {
9482 246787 : if (bb_vinfo->slp_instances.is_empty ())
9483 0 : return;
9484 :
9485 246787 : hash_set<slp_tree> visited;
9486 246787 : hash_set<stmt_vec_info> svisited;
9487 1455688 : for (slp_instance instance : bb_vinfo->slp_instances)
9488 : {
9489 715327 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9490 56344 : STMT_VINFO_LIVE_P (SLP_INSTANCE_ROOT_STMTS (instance)[0]) = true;
9491 715327 : vect_location = instance->location ();
9492 715327 : vect_bb_slp_mark_live_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance),
9493 : instance, &instance->cost_vec,
9494 : svisited, visited);
9495 : }
9496 246787 : }
9497 :
9498 : /* Determine whether we can vectorize the reduction epilogue for INSTANCE. */
9499 :
9500 : static bool
9501 93792 : vectorizable_bb_reduc_epilogue (slp_instance instance,
9502 : stmt_vector_for_cost *cost_vec)
9503 : {
9504 93792 : gassign *stmt = as_a <gassign *> (instance->root_stmts[0]->stmt);
9505 93792 : enum tree_code reduc_code = gimple_assign_rhs_code (stmt);
9506 93792 : if (reduc_code == MINUS_EXPR)
9507 0 : reduc_code = PLUS_EXPR;
9508 93792 : internal_fn reduc_fn;
9509 93792 : tree vectype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance));
9510 93792 : if (!vectype
9511 93780 : || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
9512 93780 : TREE_TYPE (vectype))
9513 56695 : || (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), 2u)
9514 1131 : && (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
9515 1131 : || reduc_fn == IFN_LAST
9516 1131 : || !direct_internal_fn_supported_p (reduc_fn, vectype,
9517 : OPTIMIZE_FOR_BOTH)))
9518 : /* Two-element reductions do not need special-handling for fold-left,
9519 : other cases are not yet implemented. remain_defs also have to
9520 : be included here. */
9521 187620 : || (needs_fold_left_reduction_p (TREE_TYPE (vectype), reduc_code)
9522 6184 : && (!instance->remain_defs.is_empty ()
9523 1912 : || SLP_TREE_LANES (SLP_INSTANCE_TREE (instance)) != 2)))
9524 : {
9525 39291 : if (dump_enabled_p ())
9526 54 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9527 : "not vectorized: basic block reduction epilogue "
9528 : "operation unsupported.\n");
9529 : return false;
9530 : }
9531 :
9532 : /* There's no way to cost a horizontal vector reduction via REDUC_FN so
9533 : cost log2 vector operations plus shuffles and one extraction. */
9534 54501 : unsigned steps = floor_log2 (vect_nunits_for_cost (vectype));
9535 54501 : record_stmt_cost (cost_vec, steps, vector_stmt, instance->root_stmts[0],
9536 : vectype, 0, vect_body);
9537 54501 : record_stmt_cost (cost_vec, steps, vec_perm, instance->root_stmts[0],
9538 : vectype, 0, vect_body);
9539 54501 : record_stmt_cost (cost_vec, 1, vec_to_scalar, instance->root_stmts[0],
9540 : vectype, 0, vect_body);
9541 :
9542 : /* Since we replace all stmts of a possibly longer scalar reduction
9543 : chain account for the extra scalar stmts for that. */
9544 54501 : if (!instance->remain_defs.is_empty ())
9545 36600 : record_stmt_cost (cost_vec, instance->remain_defs.length (), scalar_stmt,
9546 18300 : instance->root_stmts[0], 0, vect_body);
9547 : return true;
9548 : }
9549 :
9550 : /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
9551 : and recurse to children. */
9552 :
9553 : static void
9554 390068 : vect_slp_prune_covered_roots (slp_tree node, hash_set<stmt_vec_info> &roots,
9555 : hash_set<slp_tree> &visited)
9556 : {
9557 390068 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
9558 390068 : || visited.add (node))
9559 : return;
9560 :
9561 1125629 : for (auto stmt : SLP_TREE_SCALAR_STMTS (node))
9562 503460 : if (stmt)
9563 540581 : roots.remove (vect_orig_stmt (stmt));
9564 :
9565 828676 : for (auto child : SLP_TREE_CHILDREN (node))
9566 275495 : if (child)
9567 273818 : vect_slp_prune_covered_roots (child, roots, visited);
9568 : }
9569 :
9570 : /* Hand over COST_VEC to the target COSTS grouped by SLP node. */
9571 :
9572 : static void
9573 1037015 : add_slp_costs (vector_costs *costs, stmt_vector_for_cost& cost_vec)
9574 : {
9575 3870410 : for (unsigned start = 0; start < cost_vec.length ();)
9576 : {
9577 2833395 : unsigned end = start + 1;
9578 3562769 : while (end < cost_vec.length ()
9579 6098025 : && cost_vec[start].node == cost_vec[end].node)
9580 729374 : end++;
9581 2833395 : if (cost_vec[start].node)
9582 2787911 : costs->add_slp_cost (cost_vec[start].node,
9583 2787911 : array_slice<stmt_info_for_cost>
9584 2787911 : (cost_vec.begin () + start, end - start));
9585 : else
9586 45484 : costs->vector_costs::add_slp_cost (cost_vec[start].node,
9587 45484 : array_slice<stmt_info_for_cost>
9588 45484 : (cost_vec.begin () + start, end - start));
9589 2833395 : start = end;
9590 : }
9591 1037015 : }
9592 :
9593 : /* Analyze statements in SLP instances of VINFO. Return true if the
9594 : operations are supported. */
9595 :
9596 : bool
9597 707989 : vect_slp_analyze_operations (vec_info *vinfo)
9598 : {
9599 707989 : slp_instance instance;
9600 707989 : int i;
9601 :
9602 707989 : DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
9603 :
9604 707989 : hash_set<slp_tree> visited;
9605 2529336 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
9606 : {
9607 1377049 : auto_vec<slp_tree> visited_vec;
9608 1377049 : stmt_vector_for_cost cost_vec;
9609 1377049 : cost_vec.create (2);
9610 1377049 : if (is_a <bb_vec_info> (vinfo))
9611 819432 : vect_location = instance->location ();
9612 1377049 : if (!vect_slp_analyze_node_operations (vinfo,
9613 : SLP_INSTANCE_TREE (instance),
9614 : instance, visited, visited_vec,
9615 : &cost_vec)
9616 : /* CTOR instances require vectorized defs for the SLP tree root. */
9617 1119329 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor
9618 7432 : && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance))
9619 : != vect_internal_def
9620 : /* Make sure we vectorized with the expected type. */
9621 7431 : || !useless_type_conversion_p
9622 7431 : (TREE_TYPE (TREE_TYPE (gimple_assign_rhs1
9623 : (instance->root_stmts[0]->stmt))),
9624 7431 : TREE_TYPE (SLP_TREE_VECTYPE
9625 : (SLP_INSTANCE_TREE (instance))))))
9626 : /* Check we can vectorize the reduction. */
9627 1119313 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
9628 93792 : && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
9629 : /* Check we can vectorize the gcond. */
9630 2457071 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
9631 67974 : && !vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
9632 67974 : SLP_INSTANCE_ROOT_STMTS (instance)[0],
9633 : NULL,
9634 : SLP_INSTANCE_TREE (instance),
9635 : &cost_vec)))
9636 : {
9637 362215 : cost_vec.release ();
9638 362215 : slp_tree node = SLP_INSTANCE_TREE (instance);
9639 362215 : stmt_vec_info stmt_info;
9640 362215 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9641 269623 : stmt_info = SLP_INSTANCE_ROOT_STMTS (instance)[0];
9642 92592 : else if (!SLP_TREE_SCALAR_STMTS (node).is_empty ()
9643 92592 : && SLP_TREE_SCALAR_STMTS (node)[0])
9644 : stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
9645 : else
9646 3 : stmt_info = SLP_TREE_REPRESENTATIVE (node);
9647 362215 : if (is_a <loop_vec_info> (vinfo))
9648 : {
9649 263691 : if (dump_enabled_p ())
9650 7801 : dump_printf_loc (MSG_NOTE, vect_location,
9651 : "unsupported SLP instance starting from: %G",
9652 : stmt_info->stmt);
9653 263691 : return false;
9654 : }
9655 98524 : if (dump_enabled_p ())
9656 114 : dump_printf_loc (MSG_NOTE, vect_location,
9657 : "removing SLP instance operations starting from: %G",
9658 : stmt_info->stmt);
9659 235045 : while (!visited_vec.is_empty ())
9660 : {
9661 136521 : slp_tree node = visited_vec.pop ();
9662 136521 : SLP_TREE_TYPE (node) = undef_vec_info_type;
9663 136521 : if (node->data)
9664 : {
9665 10264 : delete node->data;
9666 10264 : node->data = nullptr;
9667 : }
9668 136521 : visited.remove (node);
9669 : }
9670 98524 : vect_free_slp_instance (instance);
9671 98524 : vinfo->slp_instances.ordered_remove (i);
9672 : }
9673 : else
9674 : {
9675 1014834 : i++;
9676 1014834 : if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo))
9677 : {
9678 293926 : add_slp_costs (loop_vinfo->vector_costs, cost_vec);
9679 293926 : cost_vec.release ();
9680 : }
9681 : else
9682 : /* For BB vectorization remember the SLP graph entry
9683 : cost for later. */
9684 720908 : instance->cost_vec = cost_vec;
9685 : }
9686 1377049 : }
9687 :
9688 : /* Now look for SLP instances with a root that are covered by other
9689 : instances and remove them. */
9690 444298 : hash_set<stmt_vec_info> roots;
9691 1835133 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
9692 1011228 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9693 64691 : roots.add (SLP_INSTANCE_ROOT_STMTS (instance)[0]);
9694 444298 : if (!roots.is_empty ())
9695 : {
9696 27096 : visited.empty ();
9697 143346 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
9698 116250 : vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance), roots,
9699 : visited);
9700 143346 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
9701 116250 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
9702 64691 : && !roots.contains (SLP_INSTANCE_ROOT_STMTS (instance)[0]))
9703 : {
9704 5543 : stmt_vec_info root = SLP_INSTANCE_ROOT_STMTS (instance)[0];
9705 5543 : if (dump_enabled_p ())
9706 287 : dump_printf_loc (MSG_NOTE, vect_location,
9707 : "removing SLP instance operations starting "
9708 : "from: %G", root->stmt);
9709 5543 : vect_free_slp_instance (instance);
9710 5543 : vinfo->slp_instances.ordered_remove (i);
9711 : }
9712 : else
9713 110707 : ++i;
9714 : }
9715 :
9716 888596 : return !vinfo->slp_instances.is_empty ();
9717 1152287 : }
9718 :
9719 : /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
9720 : closing the eventual chain. */
9721 :
9722 : static slp_instance
9723 806943 : get_ultimate_leader (slp_instance instance,
9724 : hash_map<slp_instance, slp_instance> &instance_leader)
9725 : {
9726 806943 : auto_vec<slp_instance *, 8> chain;
9727 806943 : slp_instance *tem;
9728 918800 : while (*(tem = instance_leader.get (instance)) != instance)
9729 : {
9730 111857 : chain.safe_push (tem);
9731 111857 : instance = *tem;
9732 : }
9733 918800 : while (!chain.is_empty ())
9734 111857 : *chain.pop () = instance;
9735 806943 : return instance;
9736 806943 : }
9737 :
9738 : namespace {
9739 : /* Subroutine of vect_bb_partition_graph_r. Map KEY to INSTANCE in
9740 : KEY_TO_INSTANCE, making INSTANCE the leader of any previous mapping
9741 : for KEY. Return true if KEY was already in KEY_TO_INSTANCE.
9742 :
9743 : INSTANCE_LEADER is as for get_ultimate_leader. */
9744 :
9745 : template<typename T>
9746 : bool
9747 3584285 : vect_map_to_instance (slp_instance instance, T key,
9748 : hash_map<T, slp_instance> &key_to_instance,
9749 : hash_map<slp_instance, slp_instance> &instance_leader)
9750 : {
9751 : bool existed_p;
9752 3584285 : slp_instance &key_instance = key_to_instance.get_or_insert (key, &existed_p);
9753 3584285 : if (!existed_p)
9754 : ;
9755 244740 : else if (key_instance != instance)
9756 : {
9757 : /* If we're running into a previously marked key make us the
9758 : leader of the current ultimate leader. This keeps the
9759 : leader chain acyclic and works even when the current instance
9760 : connects two previously independent graph parts. */
9761 91616 : slp_instance key_leader
9762 91616 : = get_ultimate_leader (key_instance, instance_leader);
9763 91616 : if (key_leader != instance)
9764 27508 : instance_leader.put (key_leader, instance);
9765 : }
9766 3584285 : key_instance = instance;
9767 3584285 : return existed_p;
9768 : }
9769 : }
9770 :
9771 : /* Worker of vect_bb_partition_graph, recurse on NODE. */
9772 :
9773 : static void
9774 1004709 : vect_bb_partition_graph_r (bb_vec_info bb_vinfo,
9775 : slp_instance instance, slp_tree node,
9776 : hash_map<stmt_vec_info, slp_instance> &stmt_to_instance,
9777 : hash_map<slp_tree, slp_instance> &node_to_instance,
9778 : hash_map<slp_instance, slp_instance> &instance_leader)
9779 : {
9780 5582663 : for (auto stmt_info : SLP_TREE_SCALAR_STMTS (node))
9781 2579576 : if (stmt_info)
9782 2579576 : vect_map_to_instance (instance, stmt_info, stmt_to_instance,
9783 : instance_leader);
9784 :
9785 1004709 : if (vect_map_to_instance (instance, node, node_to_instance,
9786 : instance_leader))
9787 : return;
9788 :
9789 3581913 : for (auto child : SLP_TREE_CHILDREN (node))
9790 985540 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9791 289382 : vect_bb_partition_graph_r (bb_vinfo, instance, child, stmt_to_instance,
9792 : node_to_instance, instance_leader);
9793 : }
9794 :
9795 : /* Partition the SLP graph into pieces that can be costed independently. */
9796 :
9797 : static void
9798 246787 : vect_bb_partition_graph (bb_vec_info bb_vinfo)
9799 : {
9800 246787 : DUMP_VECT_SCOPE ("vect_bb_partition_graph");
9801 :
9802 : /* First walk the SLP graph assigning each involved scalar stmt a
9803 : corresponding SLP graph entry and upon visiting a previously
9804 : marked stmt, make the stmts leader the current SLP graph entry. */
9805 246787 : hash_map<stmt_vec_info, slp_instance> stmt_to_instance;
9806 246787 : hash_map<slp_tree, slp_instance> node_to_instance;
9807 246787 : hash_map<slp_instance, slp_instance> instance_leader;
9808 246787 : slp_instance instance;
9809 1208901 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
9810 : {
9811 715327 : instance_leader.put (instance, instance);
9812 715327 : vect_bb_partition_graph_r (bb_vinfo,
9813 : instance, SLP_INSTANCE_TREE (instance),
9814 : stmt_to_instance, node_to_instance,
9815 : instance_leader);
9816 : }
9817 :
9818 : /* Then collect entries to each independent subgraph. */
9819 1208901 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
9820 : {
9821 715327 : slp_instance leader = get_ultimate_leader (instance, instance_leader);
9822 715327 : leader->subgraph_entries.safe_push (instance);
9823 715327 : if (dump_enabled_p ()
9824 715327 : && leader != instance)
9825 109 : dump_printf_loc (MSG_NOTE, vect_location,
9826 : "instance %p is leader of %p\n",
9827 : (void *) leader, (void *) instance);
9828 : }
9829 246787 : }
9830 :
9831 : /* Compute the scalar cost of the SLP node NODE and its children
9832 : and return it. Do not account defs that are marked in LIFE and
9833 : update LIFE according to uses of NODE. */
9834 :
9835 : static void
9836 711022 : vect_bb_slp_scalar_cost (bb_vec_info vinfo,
9837 : vec<stmt_vec_info> &worklist,
9838 : stmt_vector_for_cost *cost_vec,
9839 : hash_set<stmt_vec_info> &visited)
9840 : {
9841 3300305 : while (!worklist.is_empty ())
9842 : {
9843 2589283 : stmt_vec_info stmt = worklist.pop ();
9844 2883087 : if (!PURE_SLP_STMT (stmt))
9845 302129 : continue;
9846 :
9847 : /* When the stmt is live but not actually vectorized we have
9848 : to keep the feeding scalar defs. */
9849 2305471 : if (!STMT_VINFO_LIVE_P (vect_stmt_to_vectorize (stmt)))
9850 : {
9851 2187949 : bool live_p = false;
9852 2187949 : ssa_op_iter op_iter;
9853 2187949 : def_operand_p def_p;
9854 4830389 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt->stmt, op_iter, SSA_OP_DEF)
9855 : {
9856 454491 : imm_use_iterator use_iter;
9857 454491 : gimple *use_stmt;
9858 1157253 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
9859 702762 : if (!is_gimple_debug (use_stmt))
9860 : {
9861 532522 : stmt_vec_info use_stmt_info = vinfo->lookup_stmt (use_stmt);
9862 532522 : if (!use_stmt_info || !PURE_SLP_STMT (use_stmt_info))
9863 : {
9864 15301 : if (dump_enabled_p ())
9865 : {
9866 92 : dump_printf_loc (MSG_NOTE, vect_location,
9867 : "stmt considered live: %G",
9868 : stmt->stmt);
9869 92 : dump_printf_loc (MSG_NOTE, vect_location,
9870 : "because of use in: %G",
9871 : use_stmt);
9872 : }
9873 : live_p = true;
9874 : }
9875 454491 : }
9876 : }
9877 2187949 : if (live_p)
9878 8325 : continue;
9879 : }
9880 :
9881 : /* The following assert verifies that vect_bb_partition_graph
9882 : partitions the SLP graph in a way that each scalar stmt of
9883 : the coverage of the SLP graph belongs to exactly one subgraph.
9884 : ??? This is currently not guaranteed since the function
9885 : works purely on SLP_TREE_SCALAR_STMTS, resulting in the assert
9886 : tripping or scalar stmts costed multiple times, making vectorization
9887 : more profitable than it really is. */
9888 : /* gcc_checking_assert (!gimple_visited_p (stmt->stmt)); */
9889 :
9890 2287154 : if (vect_nop_conversion_p (stmt))
9891 : ;
9892 : /* For single-argument PHIs assume coalescing which means zero
9893 : cost for the scalar and the vector PHIs. This avoids
9894 : artificially favoring the vector path (but may pessimize it
9895 : in some cases). */
9896 2255592 : else if (is_a <gphi *> (stmt->stmt)
9897 2255592 : && gimple_phi_num_args (as_a <gphi *> (stmt->stmt)) == 1)
9898 : ;
9899 : else
9900 : {
9901 2245078 : vect_cost_for_stmt kind;
9902 2245078 : if (STMT_VINFO_DATA_REF (stmt))
9903 : {
9904 2011626 : data_reference_p dr = STMT_VINFO_DATA_REF (stmt);
9905 2011626 : tree base = get_base_address (DR_REF (dr));
9906 : /* When the scalar access is to a non-global not
9907 : address-taken decl that is not BLKmode assume we can
9908 : access it with a single non-load/store instruction. */
9909 2011626 : if (DECL_P (base)
9910 1543523 : && !is_global_var (base)
9911 1465421 : && !TREE_ADDRESSABLE (base)
9912 2570033 : && DECL_MODE (base) != BLKmode)
9913 : kind = scalar_stmt;
9914 1866198 : else if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt)))
9915 : kind = scalar_load;
9916 : else
9917 1605272 : kind = scalar_store;
9918 : }
9919 : else
9920 : kind = scalar_stmt;
9921 : /* Cost each scalar stmt only once. */
9922 2245078 : gimple_set_visited (stmt->stmt, true);
9923 2245078 : record_stmt_cost (cost_vec, 1, kind, stmt, NULL_TREE, 0, vect_body);
9924 : }
9925 :
9926 : /* Now walk relevant parts of the SSA use-def graph. */
9927 2287154 : slp_oprnds child_ops (stmt);
9928 7126161 : for (unsigned i = 0; i < child_ops.num_slp_children; ++i)
9929 : {
9930 2551853 : tree op = child_ops.get_op_for_slp_child (stmt, i);
9931 2551853 : stmt_vec_info def = vinfo->lookup_def (op);
9932 2551853 : if (def && !visited.add (def))
9933 745635 : worklist.safe_push (def);
9934 : }
9935 : }
9936 711022 : }
9937 :
9938 :
9939 : /* Comparator for the loop-index sorted cost vectors. */
9940 :
9941 : static int
9942 20535209 : li_cost_vec_cmp (const void *a_, const void *b_, void *)
9943 : {
9944 20535209 : auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_;
9945 20535209 : auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_;
9946 20535209 : if (a->first < b->first)
9947 : return -1;
9948 19092683 : else if (a->first == b->first)
9949 17947659 : return 0;
9950 : return 1;
9951 : }
9952 :
9953 : /* Check if vectorization of the basic block is profitable for the
9954 : subgraph denoted by SLP_INSTANCES. */
9955 :
9956 : static bool
9957 683719 : vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo,
9958 : vec<slp_instance> slp_instances,
9959 : loop_p orig_loop)
9960 : {
9961 683719 : slp_instance instance;
9962 683719 : int i;
9963 683719 : unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
9964 683719 : unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
9965 :
9966 683719 : if (dump_enabled_p ())
9967 : {
9968 136 : dump_printf_loc (MSG_NOTE, vect_location, "Costing subgraph:\n");
9969 282 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9970 146 : dump_printf_loc (MSG_NOTE, vect_location, " entry instance %p -> "
9971 : "node %p\n", (void *)instance,
9972 146 : (void *)SLP_INSTANCE_TREE (instance));
9973 136 : hash_set<slp_tree> visited;
9974 554 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9975 146 : vect_print_slp_graph (MSG_NOTE, vect_location,
9976 : SLP_INSTANCE_TREE (instance), visited);
9977 136 : }
9978 :
9979 : /* Then DFS walk scalar stmts, performing costing and handling
9980 : still live scalar stmts via the previously computed vector coverage. */
9981 683719 : stmt_vector_for_cost scalar_costs = vNULL;
9982 683719 : stmt_vector_for_cost vector_costs = vNULL;
9983 683719 : hash_set<slp_tree> visited;
9984 683719 : hash_set<stmt_vec_info> svisited;
9985 2078460 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9986 : {
9987 711022 : auto_vec<stmt_vec_info> worklist;
9988 711022 : if (SLP_INSTANCE_ROOT_STMTS (instance).exists ())
9989 110980 : record_stmt_cost (&scalar_costs,
9990 55490 : SLP_INSTANCE_ROOT_STMTS (instance).length (),
9991 : scalar_stmt,
9992 55490 : SLP_INSTANCE_ROOT_STMTS (instance)[0], 0, vect_body);
9993 3992254 : for (auto stmt : SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance)))
9994 : {
9995 1859188 : stmt = vect_orig_stmt (stmt);
9996 1859188 : if (!svisited.add (stmt))
9997 1843648 : worklist.safe_push (stmt);
9998 : }
9999 711022 : vect_bb_slp_scalar_cost (bb_vinfo, worklist, &scalar_costs, svisited);
10000 711022 : vector_costs.safe_splice (instance->cost_vec);
10001 711022 : instance->cost_vec.release ();
10002 711022 : }
10003 :
10004 683719 : if (dump_enabled_p ())
10005 136 : dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
10006 :
10007 : /* When costing non-loop vectorization we need to consider each covered
10008 : loop independently and make sure vectorization is profitable. For
10009 : now we assume a loop may be not entered or executed an arbitrary
10010 : number of iterations (??? static information can provide more
10011 : precise info here) which means we can simply cost each containing
10012 : loops stmts separately. */
10013 :
10014 : /* First produce cost vectors sorted by loop index. */
10015 683719 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
10016 1367438 : li_scalar_costs (scalar_costs.length ());
10017 683719 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
10018 1367425 : li_vector_costs (vector_costs.length ());
10019 683719 : stmt_info_for_cost *cost;
10020 3668006 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
10021 : {
10022 2300568 : unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
10023 2300568 : li_scalar_costs.quick_push (std::make_pair (l, cost));
10024 : }
10025 : /* Use a random used loop as fallback in case the first vector_costs
10026 : entry does not have a location associated with it. */
10027 683719 : unsigned l = li_scalar_costs[0].first;
10028 2652934 : FOR_EACH_VEC_ELT (vector_costs, i, cost)
10029 : {
10030 : /* Use SLP node placement according to the computed schedule. */
10031 1969215 : if (cost->node && cost->node->si)
10032 1215684 : l = gimple_bb (cost->node->si)->loop_father->num;
10033 : /* For schedules at region boundary use the region entry loop. */
10034 753531 : else if (cost->node)
10035 592913 : l = bb_vinfo->bbs[0]->loop_father->num;
10036 : /* SLP instance root stmts do not have an associated SLP node. */
10037 160618 : else if (cost->stmt_info)
10038 160618 : l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
10039 : /* And since vect_prologue_cost_for_slp can end up costing with
10040 : neither, inherit from the previous node. */
10041 1969215 : li_vector_costs.quick_push (std::make_pair (l, cost));
10042 : }
10043 683719 : li_scalar_costs.stablesort (li_cost_vec_cmp, NULL);
10044 683719 : li_vector_costs.stablesort (li_cost_vec_cmp, NULL);
10045 :
10046 683719 : unsigned total_vec_outside_cost = 0;
10047 683719 : unsigned total_vec_inside_cost = 0;
10048 683719 : unsigned total_scalar_cost = 0;
10049 :
10050 : /* Now cost the portions individually. */
10051 683719 : unsigned vi = 0;
10052 683719 : unsigned si = 0;
10053 683719 : bool profitable = true;
10054 1368695 : while (si < li_scalar_costs.length ()
10055 2113561 : || vi < li_vector_costs.length ())
10056 : {
10057 744879 : unsigned sl
10058 1488501 : = si < li_scalar_costs.length () ? li_scalar_costs[si].first : -1U;
10059 744879 : unsigned vl
10060 1489674 : = vi < li_vector_costs.length () ? li_vector_costs[vi].first : -1U;
10061 :
10062 744879 : class vector_costs *scalar_target_cost_data = nullptr;
10063 744879 : scalar_cost = 0;
10064 744879 : if (sl <= vl)
10065 : {
10066 699126 : if (dump_enabled_p ())
10067 158 : dump_printf_loc (MSG_NOTE, vect_location,
10068 : "Scalar cost for part in loop %d\n", sl);
10069 699126 : scalar_target_cost_data = init_cost (bb_vinfo, true);
10070 2300568 : do
10071 : {
10072 2300568 : add_stmt_cost (scalar_target_cost_data,
10073 2300568 : li_scalar_costs[si].second);
10074 2300568 : si++;
10075 : }
10076 2300568 : while (si < li_scalar_costs.length ()
10077 4616543 : && li_scalar_costs[si].first == sl);
10078 699126 : scalar_target_cost_data->finish_cost (nullptr);
10079 699126 : scalar_cost = scalar_target_cost_data->body_cost ();
10080 699126 : total_scalar_cost += scalar_cost;
10081 699126 : if (sl < vl)
10082 : {
10083 1790 : if (dump_enabled_p ())
10084 2 : dump_printf_loc (MSG_NOTE, vect_location,
10085 : "Scalar %d loop part does not "
10086 : "have corresponding vector part\n", sl);
10087 1790 : delete scalar_target_cost_data;
10088 644783 : continue;
10089 : }
10090 : }
10091 :
10092 : /* Complete the target-specific vector cost calculation. */
10093 743089 : if (dump_enabled_p ())
10094 160 : dump_printf_loc (MSG_NOTE, vect_location,
10095 : "Vector cost for part in loop %d\n", vl);
10096 743089 : class vector_costs *vect_target_cost_data = init_cost (bb_vinfo, false);
10097 743089 : auto_vec<stmt_info_for_cost> tem;
10098 1969215 : do
10099 : {
10100 1969215 : tem.safe_push (*li_vector_costs[vi].second);
10101 1969215 : vi++;
10102 : }
10103 1969215 : while (vi < li_vector_costs.length ()
10104 3997813 : && li_vector_costs[vi].first == vl);
10105 743089 : add_slp_costs (vect_target_cost_data, tem);
10106 743089 : vect_target_cost_data->finish_cost (scalar_target_cost_data);
10107 743089 : vec_prologue_cost = vect_target_cost_data->prologue_cost ();
10108 743089 : vec_inside_cost = vect_target_cost_data->body_cost ();
10109 743089 : vec_epilogue_cost = vect_target_cost_data->epilogue_cost ();
10110 743089 : if (scalar_target_cost_data)
10111 697336 : delete scalar_target_cost_data;
10112 743089 : delete vect_target_cost_data;
10113 :
10114 743089 : vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
10115 :
10116 743089 : total_vec_outside_cost += vec_outside_cost;
10117 743089 : total_vec_inside_cost += vec_inside_cost;
10118 :
10119 743089 : if (sl > vl && dump_enabled_p ())
10120 4 : dump_printf_loc (MSG_NOTE, vect_location,
10121 : "Vector %d loop part does not "
10122 : "have corresponding scalar part\n", vl);
10123 :
10124 : /* When this is vector costs for the region entry delay costing
10125 : and instead only require the total costs to be profitable. */
10126 743089 : if (vl == (unsigned) bb_vinfo->bbs[0]->loop_father->num)
10127 642993 : continue;
10128 :
10129 100096 : if (dump_enabled_p ())
10130 : {
10131 52 : dump_printf_loc (MSG_NOTE, vect_location,
10132 : "Cost model analysis for part in loop %d:\n",
10133 52 : std::min (sl, vl));
10134 52 : dump_printf (MSG_NOTE, " Vector cost: %d\n",
10135 : vec_inside_cost + vec_outside_cost);
10136 52 : dump_printf (MSG_NOTE, " Scalar cost: %d\n", scalar_cost);
10137 : }
10138 :
10139 : /* Vectorization is profitable if its cost is more than the cost of scalar
10140 : version. Note that we err on the vector side for equal cost because
10141 : the cost estimate is otherwise quite pessimistic (constant uses are
10142 : free on the scalar side but cost a load on the vector side for
10143 : example). */
10144 100096 : if (vec_outside_cost + vec_inside_cost > scalar_cost)
10145 45754 : profitable = false;
10146 743089 : }
10147 :
10148 683719 : if (dump_enabled_p ())
10149 : {
10150 136 : dump_printf_loc (MSG_NOTE, vect_location,
10151 : "Cost model analysis for whole subgraph:\n");
10152 136 : dump_printf (MSG_NOTE, " Vector cost: %d\n",
10153 : total_vec_inside_cost + total_vec_outside_cost);
10154 136 : dump_printf (MSG_NOTE, " Scalar cost: %d\n", total_scalar_cost);
10155 : }
10156 :
10157 : /* For the case where the outermost region had no scalar cost require
10158 : overall profitability. */
10159 683719 : if (total_vec_outside_cost + total_vec_inside_cost > total_scalar_cost)
10160 203192 : profitable = false;
10161 :
10162 : /* Unset visited flag. This is delayed when the subgraph is profitable
10163 : and we process the loop for remaining unvectorized if-converted code. */
10164 683719 : if (!orig_loop || !profitable)
10165 2982235 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
10166 2298641 : gimple_set_visited (cost->stmt_info->stmt, false);
10167 :
10168 683719 : scalar_costs.release ();
10169 683719 : vector_costs.release ();
10170 :
10171 683719 : return profitable;
10172 683719 : }
10173 :
10174 : /* qsort comparator for lane defs. */
10175 :
10176 : static int
10177 8 : vld_cmp (const void *a_, const void *b_)
10178 : {
10179 8 : auto *a = (const std::pair<unsigned, tree> *)a_;
10180 8 : auto *b = (const std::pair<unsigned, tree> *)b_;
10181 8 : return a->first - b->first;
10182 : }
10183 :
10184 : /* Return true if USE_STMT is a vector lane insert into VEC and set
10185 : *THIS_LANE to the lane number that is set. */
10186 :
10187 : static bool
10188 275 : vect_slp_is_lane_insert (gimple *use_stmt, tree vec, unsigned *this_lane)
10189 : {
10190 275 : gassign *use_ass = dyn_cast <gassign *> (use_stmt);
10191 92 : if (!use_ass
10192 92 : || gimple_assign_rhs_code (use_ass) != BIT_INSERT_EXPR
10193 21 : || (vec
10194 21 : ? gimple_assign_rhs1 (use_ass) != vec
10195 22 : : ((vec = gimple_assign_rhs1 (use_ass)), false))
10196 43 : || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec)),
10197 43 : TREE_TYPE (gimple_assign_rhs2 (use_ass)))
10198 43 : || !constant_multiple_p
10199 43 : (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass)),
10200 318 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec)))),
10201 : this_lane))
10202 : return false;
10203 : return true;
10204 : }
10205 :
10206 : /* Find any vectorizable constructors and add them to the grouped_store
10207 : array. */
10208 :
10209 : static void
10210 2314427 : vect_slp_check_for_roots (bb_vec_info bb_vinfo)
10211 : {
10212 18563199 : for (unsigned i = 0; i < bb_vinfo->nbbs; ++i)
10213 32497544 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[i]);
10214 147210189 : !gsi_end_p (gsi); gsi_next (&gsi))
10215 : {
10216 130961417 : gassign *assign = dyn_cast<gassign *> (gsi_stmt (gsi));
10217 : /* This can be used to start SLP discovery for early breaks for BB early breaks
10218 : when we get that far. */
10219 130961417 : if (!assign)
10220 99396040 : continue;
10221 :
10222 33722164 : tree rhs = gimple_assign_rhs1 (assign);
10223 33722164 : enum tree_code code = gimple_assign_rhs_code (assign);
10224 33722164 : use_operand_p use_p;
10225 33722164 : gimple *use_stmt;
10226 33722164 : if (code == CONSTRUCTOR)
10227 : {
10228 1756077 : if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
10229 1810067 : || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)),
10230 117461 : CONSTRUCTOR_NELTS (rhs))
10231 53990 : || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
10232 1810047 : || uniform_vector_p (rhs))
10233 1739590 : continue;
10234 :
10235 : unsigned j;
10236 : tree val;
10237 80314 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
10238 63827 : if (TREE_CODE (val) != SSA_NAME
10239 63827 : || !bb_vinfo->lookup_def (val))
10240 : break;
10241 40216 : if (j != CONSTRUCTOR_NELTS (rhs))
10242 3621 : continue;
10243 :
10244 16487 : vec<stmt_vec_info> roots = vNULL;
10245 16487 : roots.safe_push (bb_vinfo->lookup_stmt (assign));
10246 16487 : vec<stmt_vec_info> stmts;
10247 16487 : stmts.create (CONSTRUCTOR_NELTS (rhs));
10248 91086 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
10249 58112 : stmts.quick_push
10250 58112 : (vect_stmt_to_vectorize (bb_vinfo->lookup_def (val)));
10251 16487 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
10252 16487 : stmts, roots));
10253 : }
10254 31966087 : else if (code == BIT_INSERT_EXPR
10255 892 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
10256 592 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).is_constant ()
10257 592 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).to_constant () > 1
10258 586 : && integer_zerop (gimple_assign_rhs3 (assign))
10259 347 : && useless_type_conversion_p
10260 347 : (TREE_TYPE (TREE_TYPE (rhs)),
10261 347 : TREE_TYPE (gimple_assign_rhs2 (assign)))
10262 31966721 : && bb_vinfo->lookup_def (gimple_assign_rhs2 (assign)))
10263 : {
10264 : /* We start to match on insert to lane zero but since the
10265 : inserts need not be ordered we'd have to search both
10266 : the def and the use chains. */
10267 251 : tree vectype = TREE_TYPE (rhs);
10268 251 : unsigned nlanes = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
10269 251 : auto_vec<std::pair<unsigned, tree> > lane_defs (nlanes);
10270 251 : auto_sbitmap lanes (nlanes);
10271 251 : bitmap_clear (lanes);
10272 251 : bitmap_set_bit (lanes, 0);
10273 251 : tree def = gimple_assign_lhs (assign);
10274 251 : lane_defs.quick_push
10275 251 : (std::make_pair (0, gimple_assign_rhs2 (assign)));
10276 251 : unsigned lanes_found = 1;
10277 : /* Start with the use chains, the last stmt will be the root. */
10278 251 : stmt_vec_info last = bb_vinfo->lookup_stmt (assign);
10279 251 : vec<stmt_vec_info> roots = vNULL;
10280 251 : roots.safe_push (last);
10281 252 : do
10282 : {
10283 252 : use_operand_p use_p;
10284 252 : gimple *use_stmt;
10285 252 : if (!single_imm_use (def, &use_p, &use_stmt))
10286 : break;
10287 240 : unsigned this_lane;
10288 240 : if (!bb_vinfo->lookup_stmt (use_stmt)
10289 240 : || !vect_slp_is_lane_insert (use_stmt, def, &this_lane)
10290 261 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (use_stmt)))
10291 : break;
10292 21 : if (bitmap_bit_p (lanes, this_lane))
10293 : break;
10294 1 : lanes_found++;
10295 1 : bitmap_set_bit (lanes, this_lane);
10296 1 : gassign *use_ass = as_a <gassign *> (use_stmt);
10297 1 : lane_defs.quick_push (std::make_pair
10298 1 : (this_lane, gimple_assign_rhs2 (use_ass)));
10299 1 : last = bb_vinfo->lookup_stmt (use_ass);
10300 1 : roots.safe_push (last);
10301 1 : def = gimple_assign_lhs (use_ass);
10302 : }
10303 1 : while (lanes_found < nlanes);
10304 251 : if (roots.length () > 1)
10305 1 : std::swap(roots[0], roots[roots.length () - 1]);
10306 251 : if (lanes_found < nlanes)
10307 : {
10308 : /* Now search the def chain. */
10309 251 : def = gimple_assign_rhs1 (assign);
10310 251 : do
10311 : {
10312 251 : if (TREE_CODE (def) != SSA_NAME
10313 251 : || !has_single_use (def))
10314 : break;
10315 55 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
10316 55 : unsigned this_lane;
10317 55 : if (!bb_vinfo->lookup_stmt (def_stmt)
10318 35 : || !vect_slp_is_lane_insert (def_stmt,
10319 : NULL_TREE, &this_lane)
10320 77 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (def_stmt)))
10321 : break;
10322 22 : if (bitmap_bit_p (lanes, this_lane))
10323 : break;
10324 2 : lanes_found++;
10325 2 : bitmap_set_bit (lanes, this_lane);
10326 4 : lane_defs.quick_push (std::make_pair
10327 2 : (this_lane,
10328 2 : gimple_assign_rhs2 (def_stmt)));
10329 2 : roots.safe_push (bb_vinfo->lookup_stmt (def_stmt));
10330 2 : def = gimple_assign_rhs1 (def_stmt);
10331 : }
10332 2 : while (lanes_found < nlanes);
10333 : }
10334 251 : if (lanes_found == nlanes)
10335 : {
10336 : /* Sort lane_defs after the lane index and register the root. */
10337 2 : lane_defs.qsort (vld_cmp);
10338 2 : vec<stmt_vec_info> stmts;
10339 2 : stmts.create (nlanes);
10340 8 : for (unsigned i = 0; i < nlanes; ++i)
10341 4 : stmts.quick_push (bb_vinfo->lookup_def (lane_defs[i].second));
10342 2 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
10343 2 : stmts, roots));
10344 : }
10345 : else
10346 249 : roots.release ();
10347 251 : }
10348 31965836 : else if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
10349 30853410 : && (associative_tree_code (code) || code == MINUS_EXPR)
10350 : /* Ops with constants at the tail can be stripped here. */
10351 6861953 : && TREE_CODE (rhs) == SSA_NAME
10352 6797134 : && TREE_CODE (gimple_assign_rhs2 (assign)) == SSA_NAME
10353 : /* Should be the chain end. */
10354 34935793 : && (!single_imm_use (gimple_assign_lhs (assign),
10355 : &use_p, &use_stmt)
10356 2378878 : || !is_gimple_assign (use_stmt)
10357 1764133 : || (gimple_assign_rhs_code (use_stmt) != code
10358 1188732 : && ((code != PLUS_EXPR && code != MINUS_EXPR)
10359 688199 : || (gimple_assign_rhs_code (use_stmt)
10360 688199 : != (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR))))))
10361 : {
10362 : /* We start the match at the end of a possible association
10363 : chain. */
10364 2270964 : auto_vec<chain_op_t> chain;
10365 2270964 : auto_vec<std::pair<tree_code, gimple *> > worklist;
10366 2270964 : auto_vec<gimple *> chain_stmts;
10367 2270964 : gimple *code_stmt = NULL, *alt_code_stmt = NULL;
10368 2270964 : if (code == MINUS_EXPR)
10369 368307 : code = PLUS_EXPR;
10370 2270964 : internal_fn reduc_fn;
10371 2688161 : if (!reduction_fn_for_scalar_code (code, &reduc_fn)
10372 2270964 : || reduc_fn == IFN_LAST)
10373 417197 : continue;
10374 1853767 : vect_slp_linearize_chain (bb_vinfo, worklist, chain, code, assign,
10375 : /* ??? */
10376 : code_stmt, alt_code_stmt, &chain_stmts,
10377 : false);
10378 3707534 : if (chain.length () > 1)
10379 : {
10380 : /* Sort the chain according to def_type and operation. */
10381 1853767 : chain.sort (dt_sort_cmp, bb_vinfo);
10382 : /* ??? Now we'd want to strip externals and constants
10383 : but record those to be handled in the epilogue. */
10384 : /* ??? For now do not allow mixing ops or externs/constants. */
10385 1853767 : bool invalid = false;
10386 1853767 : unsigned remain_cnt = 0;
10387 7645337 : for (unsigned i = 0; i < chain.length (); ++i)
10388 : {
10389 4306110 : if (chain[i].code != code)
10390 : {
10391 : invalid = true;
10392 : break;
10393 : }
10394 3937803 : if (chain[i].dt != vect_internal_def
10395 : /* Avoid stmts where the def is not the LHS, like
10396 : ASMs. */
10397 7500975 : || (gimple_get_lhs (bb_vinfo->lookup_def
10398 3563172 : (chain[i].op)->stmt)
10399 3563172 : != chain[i].op))
10400 377697 : remain_cnt++;
10401 : }
10402 1853767 : if (!invalid && chain.length () - remain_cnt > 1)
10403 : {
10404 1400127 : vec<stmt_vec_info> stmts;
10405 1400127 : vec<tree> remain = vNULL;
10406 1400127 : stmts.create (chain.length ());
10407 1400127 : if (remain_cnt > 0)
10408 53551 : remain.create (remain_cnt);
10409 4658090 : for (unsigned i = 0; i < chain.length (); ++i)
10410 : {
10411 3257963 : stmt_vec_info stmt_info;
10412 3257963 : if (chain[i].dt == vect_internal_def
10413 3257963 : && ((stmt_info = bb_vinfo->lookup_def (chain[i].op)),
10414 3149995 : gimple_get_lhs (stmt_info->stmt) == chain[i].op))
10415 3149797 : stmts.quick_push (stmt_info);
10416 : else
10417 108166 : remain.quick_push (chain[i].op);
10418 : }
10419 1400127 : vec<stmt_vec_info> roots;
10420 1400127 : roots.create (chain_stmts.length ());
10421 4658090 : for (unsigned i = 0; i < chain_stmts.length (); ++i)
10422 1857836 : roots.quick_push (bb_vinfo->lookup_stmt (chain_stmts[i]));
10423 1400127 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_bb_reduc,
10424 1400127 : stmts, roots, remain));
10425 : }
10426 : }
10427 2270964 : }
10428 : }
10429 2314427 : }
10430 :
10431 : /* Walk the grouped store chains and replace entries with their
10432 : pattern variant if any. */
10433 :
10434 : static void
10435 682813 : vect_fixup_store_groups_with_patterns (vec_info *vinfo)
10436 : {
10437 682813 : stmt_vec_info first_element;
10438 682813 : unsigned i;
10439 :
10440 1605210 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
10441 : {
10442 : /* We also have CTORs in this array. */
10443 922397 : if (!STMT_VINFO_GROUPED_ACCESS (first_element))
10444 0 : continue;
10445 922397 : if (STMT_VINFO_IN_PATTERN_P (first_element))
10446 : {
10447 253 : stmt_vec_info orig = first_element;
10448 253 : first_element = STMT_VINFO_RELATED_STMT (first_element);
10449 253 : DR_GROUP_FIRST_ELEMENT (first_element) = first_element;
10450 253 : DR_GROUP_SIZE (first_element) = DR_GROUP_SIZE (orig);
10451 253 : DR_GROUP_GAP (first_element) = DR_GROUP_GAP (orig);
10452 253 : DR_GROUP_NEXT_ELEMENT (first_element) = DR_GROUP_NEXT_ELEMENT (orig);
10453 253 : vinfo->grouped_stores[i] = first_element;
10454 : }
10455 922397 : stmt_vec_info prev = first_element;
10456 2596313 : while (DR_GROUP_NEXT_ELEMENT (prev))
10457 : {
10458 1673916 : stmt_vec_info elt = DR_GROUP_NEXT_ELEMENT (prev);
10459 1673916 : if (STMT_VINFO_IN_PATTERN_P (elt))
10460 : {
10461 933 : stmt_vec_info orig = elt;
10462 933 : elt = STMT_VINFO_RELATED_STMT (elt);
10463 933 : DR_GROUP_NEXT_ELEMENT (prev) = elt;
10464 933 : DR_GROUP_GAP (elt) = DR_GROUP_GAP (orig);
10465 933 : DR_GROUP_NEXT_ELEMENT (elt) = DR_GROUP_NEXT_ELEMENT (orig);
10466 : }
10467 1673916 : DR_GROUP_FIRST_ELEMENT (elt) = first_element;
10468 1673916 : prev = elt;
10469 : }
10470 : }
10471 682813 : }
10472 :
10473 : /* Check if the region described by BB_VINFO can be vectorized, returning
10474 : true if so. When returning false, set FATAL to true if the same failure
10475 : would prevent vectorization at other vector sizes, false if it is still
10476 : worth trying other sizes. N_STMTS is the number of statements in the
10477 : region. */
10478 :
10479 : static bool
10480 2314427 : vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal,
10481 : vec<int> *dataref_groups)
10482 : {
10483 2314427 : DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
10484 :
10485 2314427 : slp_instance instance;
10486 2314427 : int i;
10487 :
10488 : /* The first group of checks is independent of the vector size. */
10489 2314427 : fatal = true;
10490 :
10491 : /* Analyze the data references. */
10492 :
10493 2314427 : if (!vect_analyze_data_refs (bb_vinfo, NULL))
10494 : {
10495 0 : if (dump_enabled_p ())
10496 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10497 : "not vectorized: unhandled data-ref in basic "
10498 : "block.\n");
10499 : return false;
10500 : }
10501 :
10502 2314427 : if (!vect_analyze_data_ref_accesses (bb_vinfo, dataref_groups))
10503 : {
10504 0 : if (dump_enabled_p ())
10505 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10506 : "not vectorized: unhandled data access in "
10507 : "basic block.\n");
10508 : return false;
10509 : }
10510 :
10511 2314427 : vect_slp_check_for_roots (bb_vinfo);
10512 :
10513 : /* If there are no grouped stores and no constructors in the region
10514 : there is no need to continue with pattern recog as vect_analyze_slp
10515 : will fail anyway. */
10516 2314427 : if (bb_vinfo->grouped_stores.is_empty ()
10517 1959800 : && bb_vinfo->roots.is_empty ())
10518 : {
10519 1631614 : if (dump_enabled_p ())
10520 1142 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10521 : "not vectorized: no grouped stores in "
10522 : "basic block.\n");
10523 : return false;
10524 : }
10525 :
10526 : /* While the rest of the analysis below depends on it in some way. */
10527 682813 : fatal = false;
10528 :
10529 682813 : vect_pattern_recog (bb_vinfo);
10530 :
10531 : /* Update store groups from pattern processing. */
10532 682813 : vect_fixup_store_groups_with_patterns (bb_vinfo);
10533 :
10534 : /* Check the SLP opportunities in the basic block, analyze and build SLP
10535 : trees. */
10536 682813 : if (!vect_analyze_slp (bb_vinfo, n_stmts, false))
10537 : {
10538 0 : if (dump_enabled_p ())
10539 : {
10540 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10541 : "Failed to SLP the basic block.\n");
10542 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10543 : "not vectorized: failed to find SLP opportunities "
10544 : "in basic block.\n");
10545 : }
10546 : return false;
10547 : }
10548 :
10549 : /* Optimize permutations. */
10550 682813 : vect_optimize_slp (bb_vinfo);
10551 :
10552 : /* Gather the loads reachable from the SLP graph entries. */
10553 682813 : vect_gather_slp_loads (bb_vinfo);
10554 :
10555 682813 : vect_record_base_alignments (bb_vinfo);
10556 :
10557 : /* Analyze and verify the alignment of data references and the
10558 : dependence in the SLP instances. */
10559 2191407 : for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
10560 : {
10561 825781 : vect_location = instance->location ();
10562 825781 : if (! vect_slp_analyze_instance_alignment (bb_vinfo, instance)
10563 825781 : || ! vect_slp_analyze_instance_dependence (bb_vinfo, instance))
10564 : {
10565 6349 : slp_tree node = SLP_INSTANCE_TREE (instance);
10566 6349 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
10567 6349 : if (dump_enabled_p ())
10568 4 : dump_printf_loc (MSG_NOTE, vect_location,
10569 : "removing SLP instance operations starting from: %G",
10570 : stmt_info->stmt);
10571 6349 : vect_free_slp_instance (instance);
10572 6349 : BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
10573 6349 : continue;
10574 6349 : }
10575 :
10576 : /* Mark all the statements that we want to vectorize as relevant. */
10577 819432 : vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
10578 :
10579 819432 : i++;
10580 : }
10581 2594244 : if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
10582 : return false;
10583 :
10584 279817 : if (!vect_slp_analyze_operations (bb_vinfo))
10585 : {
10586 33030 : if (dump_enabled_p ())
10587 67 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10588 : "not vectorized: bad operation in basic block.\n");
10589 : return false;
10590 : }
10591 :
10592 : /* Compute vector stmt placement. */
10593 246787 : if (!vect_schedule_slp (bb_vinfo, BB_VINFO_SLP_INSTANCES (bb_vinfo), true))
10594 : {
10595 0 : if (dump_enabled_p ())
10596 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10597 : "not vectorized: cannot schedule SLP graph\n");
10598 : return false;
10599 : }
10600 :
10601 : /* Mark all the statements that we vectorize. */
10602 246787 : vect_bb_slp_mark_stmts_vectorized (bb_vinfo);
10603 :
10604 : /* Compute vectorizable live stmts. */
10605 246787 : vect_bb_slp_mark_live_stmts (bb_vinfo);
10606 :
10607 246787 : vect_bb_partition_graph (bb_vinfo);
10608 :
10609 246787 : return true;
10610 : }
10611 :
10612 : /* Subroutine of vect_slp_bb. Try to vectorize the statements for all
10613 : basic blocks in BBS, returning true on success.
10614 : The region has N_STMTS statements and has the datarefs given by DATAREFS. */
10615 :
10616 : static bool
10617 1944692 : vect_slp_region (vec<basic_block> bbs, vec<data_reference_p> datarefs,
10618 : vec<int> *dataref_groups, unsigned int n_stmts,
10619 : loop_p orig_loop)
10620 : {
10621 1944692 : bb_vec_info bb_vinfo;
10622 1944692 : auto_vector_modes vector_modes;
10623 :
10624 : /* Autodetect first vector size we try. */
10625 1944692 : machine_mode next_vector_mode = VOIDmode;
10626 1944692 : targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
10627 1944692 : unsigned int mode_i = 0;
10628 :
10629 1944692 : vec_info_shared shared;
10630 :
10631 1944692 : machine_mode autodetected_vector_mode = VOIDmode;
10632 2684162 : while (1)
10633 : {
10634 2314427 : bool vectorized = false;
10635 2314427 : bool fatal = false;
10636 2314427 : bb_vinfo = new _bb_vec_info (bbs, &shared);
10637 :
10638 2314427 : bool first_time_p = shared.datarefs.is_empty ();
10639 2314427 : BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
10640 2314427 : if (first_time_p)
10641 1969991 : bb_vinfo->shared->save_datarefs ();
10642 : else
10643 344436 : bb_vinfo->shared->check_datarefs ();
10644 2314427 : bb_vinfo->vector_mode = next_vector_mode;
10645 :
10646 2314427 : if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal, dataref_groups))
10647 : {
10648 246787 : if (dump_enabled_p ())
10649 : {
10650 1794 : dump_printf_loc (MSG_NOTE, vect_location,
10651 : "***** Analysis succeeded with vector mode"
10652 897 : " %s\n", GET_MODE_NAME (bb_vinfo->vector_mode));
10653 897 : dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
10654 : }
10655 :
10656 246787 : bb_vinfo->shared->check_datarefs ();
10657 :
10658 246787 : bool force_clear = false;
10659 246787 : auto_vec<slp_instance> profitable_subgraphs;
10660 1455688 : for (slp_instance instance : BB_VINFO_SLP_INSTANCES (bb_vinfo))
10661 : {
10662 715327 : if (instance->subgraph_entries.is_empty ())
10663 231036 : continue;
10664 :
10665 687819 : dump_user_location_t saved_vect_location = vect_location;
10666 687819 : vect_location = instance->location ();
10667 687819 : if (!unlimited_cost_model (NULL)
10668 683728 : && !param_vect_allow_possibly_not_worthwhile_vectorizations
10669 1371538 : && !vect_bb_vectorization_profitable_p
10670 683719 : (bb_vinfo, instance->subgraph_entries, orig_loop))
10671 : {
10672 203528 : if (dump_enabled_p ())
10673 55 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10674 : "not vectorized: vectorization is not "
10675 : "profitable.\n");
10676 203528 : vect_location = saved_vect_location;
10677 203528 : continue;
10678 : }
10679 :
10680 484291 : vect_location = saved_vect_location;
10681 484291 : if (!dbg_cnt (vect_slp))
10682 : {
10683 0 : force_clear = true;
10684 0 : continue;
10685 : }
10686 :
10687 484291 : profitable_subgraphs.safe_push (instance);
10688 : }
10689 :
10690 : /* When we're vectorizing an if-converted loop body make sure
10691 : we vectorized all if-converted code. */
10692 404723 : if ((!profitable_subgraphs.is_empty () || force_clear) && orig_loop)
10693 : {
10694 130 : gcc_assert (bb_vinfo->nbbs == 1);
10695 260 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[0]);
10696 5837 : !gsi_end_p (gsi); gsi_next (&gsi))
10697 : {
10698 : /* The costing above left us with DCEable vectorized scalar
10699 : stmts having the visited flag set on profitable
10700 : subgraphs. Do the delayed clearing of the flag here. */
10701 5707 : if (gimple_visited_p (gsi_stmt (gsi)))
10702 : {
10703 1816 : gimple_set_visited (gsi_stmt (gsi), false);
10704 1816 : continue;
10705 : }
10706 3891 : if (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED)
10707 893 : continue;
10708 :
10709 8450 : if (gassign *ass = dyn_cast <gassign *> (gsi_stmt (gsi)))
10710 3456 : if (gimple_assign_rhs_code (ass) == COND_EXPR)
10711 : {
10712 151 : if (!profitable_subgraphs.is_empty ()
10713 66 : && dump_enabled_p ())
10714 0 : dump_printf_loc (MSG_NOTE, vect_location,
10715 : "not profitable because of "
10716 : "unprofitable if-converted scalar "
10717 : "code\n");
10718 85 : profitable_subgraphs.truncate (0);
10719 : }
10720 : }
10721 : }
10722 :
10723 : /* Finally schedule the profitable subgraphs. */
10724 1046867 : for (slp_instance instance : profitable_subgraphs)
10725 : {
10726 484208 : if (!vectorized && dump_enabled_p ())
10727 854 : dump_printf_loc (MSG_NOTE, vect_location,
10728 : "Basic block will be vectorized "
10729 : "using SLP\n");
10730 484208 : vectorized = true;
10731 :
10732 : /* Dump before scheduling as store vectorization will remove
10733 : the original stores and mess with the instance tree
10734 : so querying its location will eventually ICE. */
10735 484208 : if (flag_checking)
10736 1947948 : for (slp_instance sub : instance->subgraph_entries)
10737 495324 : gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
10738 484208 : unsigned HOST_WIDE_INT bytes;
10739 484208 : if (dump_enabled_p ())
10740 4154 : for (slp_instance sub : instance->subgraph_entries)
10741 : {
10742 1118 : tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
10743 2236 : if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
10744 1118 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
10745 1118 : sub->location (),
10746 : "basic block part vectorized using %wu "
10747 : "byte vectors\n", bytes);
10748 : else
10749 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
10750 : sub->location (),
10751 : "basic block part vectorized using "
10752 : "variable length vectors\n");
10753 : }
10754 :
10755 484208 : dump_user_location_t saved_vect_location = vect_location;
10756 484208 : vect_location = instance->location ();
10757 :
10758 484208 : vect_schedule_slp (bb_vinfo, instance->subgraph_entries, false);
10759 :
10760 484208 : vect_location = saved_vect_location;
10761 : }
10762 :
10763 : /* Generate the invariant statements. */
10764 246787 : if (!gimple_seq_empty_p (bb_vinfo->inv_pattern_def_seq))
10765 : {
10766 43 : if (dump_enabled_p ())
10767 0 : dump_printf_loc (MSG_NOTE, vect_location,
10768 : "------>generating invariant statements\n");
10769 :
10770 43 : bb_vinfo->insert_seq_on_entry (NULL,
10771 : bb_vinfo->inv_pattern_def_seq);
10772 : }
10773 246787 : }
10774 : else
10775 : {
10776 2067640 : if (dump_enabled_p ())
10777 1483 : dump_printf_loc (MSG_NOTE, vect_location,
10778 : "***** Analysis failed with vector mode %s\n",
10779 1483 : GET_MODE_NAME (bb_vinfo->vector_mode));
10780 : }
10781 :
10782 2314427 : if (mode_i == 0)
10783 1944692 : autodetected_vector_mode = bb_vinfo->vector_mode;
10784 :
10785 2314427 : if (!fatal)
10786 3356352 : while (mode_i < vector_modes.length ()
10787 1928589 : && vect_chooses_same_modes_p (bb_vinfo, vector_modes[mode_i]))
10788 : {
10789 359112 : if (dump_enabled_p ())
10790 1952 : dump_printf_loc (MSG_NOTE, vect_location,
10791 : "***** The result for vector mode %s would"
10792 : " be the same\n",
10793 976 : GET_MODE_NAME (vector_modes[mode_i]));
10794 359112 : mode_i += 1;
10795 : }
10796 :
10797 2314427 : delete bb_vinfo;
10798 :
10799 2314427 : if (mode_i < vector_modes.length ()
10800 2111721 : && VECTOR_MODE_P (autodetected_vector_mode)
10801 2090188 : && (related_vector_mode (vector_modes[mode_i],
10802 : GET_MODE_INNER (autodetected_vector_mode))
10803 1045094 : == autodetected_vector_mode)
10804 4426148 : && (related_vector_mode (autodetected_vector_mode,
10805 517388 : GET_MODE_INNER (vector_modes[mode_i]))
10806 1034776 : == vector_modes[mode_i]))
10807 : {
10808 517388 : if (dump_enabled_p ())
10809 230 : dump_printf_loc (MSG_NOTE, vect_location,
10810 : "***** Skipping vector mode %s, which would"
10811 : " repeat the analysis for %s\n",
10812 230 : GET_MODE_NAME (vector_modes[mode_i]),
10813 230 : GET_MODE_NAME (autodetected_vector_mode));
10814 517388 : mode_i += 1;
10815 : }
10816 :
10817 2314427 : if (vectorized
10818 2156557 : || mode_i == vector_modes.length ()
10819 1953904 : || autodetected_vector_mode == VOIDmode
10820 : /* If vect_slp_analyze_bb_1 signaled that analysis for all
10821 : vector sizes will fail do not bother iterating. */
10822 3201704 : || fatal)
10823 3889384 : return vectorized;
10824 :
10825 : /* Try the next biggest vector size. */
10826 369735 : next_vector_mode = vector_modes[mode_i++];
10827 369735 : shared.used_vector_modes.empty ();
10828 369735 : if (dump_enabled_p ())
10829 265 : dump_printf_loc (MSG_NOTE, vect_location,
10830 : "***** Re-trying analysis with vector mode %s\n",
10831 265 : GET_MODE_NAME (next_vector_mode));
10832 369735 : }
10833 1944692 : }
10834 :
10835 :
10836 : /* Main entry for the BB vectorizer. Analyze and transform BBS, returns
10837 : true if anything in the basic-block was vectorized. */
10838 :
10839 : static bool
10840 1944692 : vect_slp_bbs (const vec<basic_block> &bbs, loop_p orig_loop)
10841 : {
10842 1944692 : vec<data_reference_p> datarefs = vNULL;
10843 1944692 : auto_vec<int> dataref_groups;
10844 1944692 : int insns = 0;
10845 1944692 : int current_group = 0;
10846 :
10847 12914845 : for (unsigned i = 0; i < bbs.length (); i++)
10848 : {
10849 10970153 : basic_block bb = bbs[i];
10850 94778271 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
10851 83808118 : gsi_next (&gsi))
10852 : {
10853 83808118 : gimple *stmt = gsi_stmt (gsi);
10854 83808118 : if (is_gimple_debug (stmt))
10855 52703991 : continue;
10856 :
10857 31104127 : insns++;
10858 :
10859 31104127 : if (gimple_location (stmt) != UNKNOWN_LOCATION)
10860 27831852 : vect_location = stmt;
10861 :
10862 31104127 : if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs,
10863 : &dataref_groups, current_group))
10864 5327057 : ++current_group;
10865 : }
10866 : /* New BBs always start a new DR group. */
10867 10970153 : ++current_group;
10868 : }
10869 :
10870 1944692 : return vect_slp_region (bbs, datarefs, &dataref_groups, insns, orig_loop);
10871 1944692 : }
10872 :
10873 : /* Special entry for the BB vectorizer. Analyze and transform a single
10874 : if-converted BB with ORIG_LOOPs body being the not if-converted
10875 : representation. Returns true if anything in the basic-block was
10876 : vectorized. */
10877 :
10878 : bool
10879 16834 : vect_slp_if_converted_bb (basic_block bb, loop_p orig_loop)
10880 : {
10881 16834 : auto_vec<basic_block> bbs;
10882 16834 : bbs.safe_push (bb);
10883 16834 : return vect_slp_bbs (bbs, orig_loop);
10884 16834 : }
10885 :
10886 : /* Main entry for the BB vectorizer. Analyze and transform BB, returns
10887 : true if anything in the basic-block was vectorized. */
10888 :
10889 : bool
10890 930230 : vect_slp_function (function *fun)
10891 : {
10892 930230 : bool r = false;
10893 930230 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
10894 930230 : auto_bitmap exit_bbs;
10895 930230 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
10896 930230 : edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
10897 930230 : unsigned n = rev_post_order_and_mark_dfs_back_seme (fun, entry, exit_bbs,
10898 930230 : true, rpo, NULL);
10899 :
10900 : /* For the moment split the function into pieces to avoid making
10901 : the iteration on the vector mode moot. Split at points we know
10902 : to not handle well which is CFG merges (SLP discovery doesn't
10903 : handle non-loop-header PHIs) and loop exits. Since pattern
10904 : recog requires reverse iteration to visit uses before defs
10905 : simply chop RPO into pieces. */
10906 930230 : auto_vec<basic_block> bbs;
10907 11911006 : for (unsigned i = 0; i < n; i++)
10908 : {
10909 10980776 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
10910 10980776 : bool split = false;
10911 :
10912 : /* Split when a BB is not dominated by the first block. */
10913 20715062 : if (!bbs.is_empty ()
10914 9734286 : && !dominated_by_p (CDI_DOMINATORS, bb, bbs[0]))
10915 : {
10916 697237 : if (dump_enabled_p ())
10917 189 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10918 : "splitting region at dominance boundary bb%d\n",
10919 : bb->index);
10920 : split = true;
10921 : }
10922 : /* Split when the loop determined by the first block
10923 : is exited. This is because we eventually insert
10924 : invariants at region begin. */
10925 19320588 : else if (!bbs.is_empty ()
10926 9037049 : && bbs[0]->loop_father != bb->loop_father
10927 2386305 : && !flow_loop_nested_p (bbs[0]->loop_father, bb->loop_father))
10928 : {
10929 3953 : if (dump_enabled_p ())
10930 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10931 : "splitting region at loop %d exit at bb%d\n",
10932 3 : bbs[0]->loop_father->num, bb->index);
10933 : split = true;
10934 : }
10935 10279586 : else if (!bbs.is_empty ()
10936 9033096 : && bb->loop_father->header == bb
10937 489669 : && bb->loop_father->dont_vectorize)
10938 : {
10939 6371 : if (dump_enabled_p ())
10940 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10941 : "splitting region at dont-vectorize loop %d "
10942 : "entry at bb%d\n",
10943 : bb->loop_father->num, bb->index);
10944 : split = true;
10945 : }
10946 10273215 : else if (!bbs.is_empty ()
10947 9026725 : && bb_has_abnormal_pred (bb))
10948 : {
10949 1264 : if (dump_enabled_p ())
10950 15 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10951 : "splitting region at incoming abnormal edge "
10952 : "at bb%d\n", bb->index);
10953 : split = true;
10954 : }
10955 :
10956 279 : if (split && !bbs.is_empty ())
10957 : {
10958 708825 : r |= vect_slp_bbs (bbs, NULL);
10959 708825 : bbs.truncate (0);
10960 : }
10961 :
10962 10980776 : if (bbs.is_empty ())
10963 : {
10964 : /* We need to be able to insert at the head of the region which
10965 : we cannot for region starting with a returns-twice call. */
10966 1955315 : if (gcall *first = safe_dyn_cast <gcall *> (first_stmt (bb)))
10967 414141 : if (gimple_call_flags (first) & ECF_RETURNS_TWICE)
10968 : {
10969 616 : if (dump_enabled_p ())
10970 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10971 : "skipping bb%d as start of region as it "
10972 : "starts with returns-twice call\n",
10973 : bb->index);
10974 27457 : continue;
10975 : }
10976 : /* If the loop this BB belongs to is marked as not to be vectorized
10977 : honor that also for BB vectorization. */
10978 1954699 : if (bb->loop_father->dont_vectorize)
10979 26841 : continue;
10980 : }
10981 :
10982 10953319 : bbs.safe_push (bb);
10983 :
10984 : /* When we have a stmt ending this block and defining a
10985 : value we have to insert on edges when inserting after it for
10986 : a vector containing its definition. Avoid this for now. */
10987 21906638 : if (gimple *last = *gsi_last_bb (bb))
10988 8904777 : if ((gimple_get_lhs (last)
10989 7580147 : || !zero_ssa_operands (last, SSA_OP_DEF))
10990 8904866 : && is_ctrl_altering_stmt (last))
10991 : {
10992 288813 : if (dump_enabled_p ())
10993 9 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10994 : "splitting region at control altering "
10995 : "definition %G", last);
10996 288813 : r |= vect_slp_bbs (bbs, NULL);
10997 288813 : bbs.truncate (0);
10998 : }
10999 : }
11000 :
11001 930230 : if (!bbs.is_empty ())
11002 930220 : r |= vect_slp_bbs (bbs, NULL);
11003 :
11004 930230 : free (rpo);
11005 :
11006 930230 : return r;
11007 930230 : }
11008 :
11009 : /* Build a variable-length vector in which the elements in ELTS are repeated
11010 : to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
11011 : RESULTS and add any new instructions to SEQ.
11012 :
11013 : The approach we use is:
11014 :
11015 : (1) Find a vector mode VM with integer elements of mode IM.
11016 :
11017 : (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
11018 : ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
11019 : from small vectors to IM.
11020 :
11021 : (3) Duplicate each ELTS'[I] into a vector of mode VM.
11022 :
11023 : (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
11024 : correct byte contents.
11025 :
11026 : (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
11027 :
11028 : We try to find the largest IM for which this sequence works, in order
11029 : to cut down on the number of interleaves. */
11030 :
11031 : void
11032 0 : duplicate_and_interleave (vec_info *vinfo, gimple_seq *seq, tree vector_type,
11033 : const vec<tree> &elts, unsigned int nresults,
11034 : vec<tree> &results)
11035 : {
11036 0 : unsigned int nelts = elts.length ();
11037 0 : tree element_type = TREE_TYPE (vector_type);
11038 :
11039 : /* (1) Find a vector mode VM with integer elements of mode IM. */
11040 0 : unsigned int nvectors = 1;
11041 0 : tree new_vector_type;
11042 0 : tree permutes[2];
11043 0 : if (!can_duplicate_and_interleave_p (vinfo, nelts, element_type,
11044 : &nvectors, &new_vector_type,
11045 : permutes))
11046 0 : gcc_unreachable ();
11047 :
11048 : /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
11049 0 : unsigned int partial_nelts = nelts / nvectors;
11050 0 : tree partial_vector_type = build_vector_type (element_type, partial_nelts);
11051 :
11052 0 : tree_vector_builder partial_elts;
11053 0 : auto_vec<tree, 32> pieces (nvectors * 2);
11054 0 : pieces.quick_grow_cleared (nvectors * 2);
11055 0 : for (unsigned int i = 0; i < nvectors; ++i)
11056 : {
11057 : /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
11058 : ELTS' has mode IM. */
11059 0 : partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
11060 0 : for (unsigned int j = 0; j < partial_nelts; ++j)
11061 0 : partial_elts.quick_push (elts[i * partial_nelts + j]);
11062 0 : tree t = gimple_build_vector (seq, &partial_elts);
11063 0 : t = gimple_build (seq, VIEW_CONVERT_EXPR,
11064 0 : TREE_TYPE (new_vector_type), t);
11065 :
11066 : /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
11067 0 : pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
11068 : }
11069 :
11070 : /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
11071 : correct byte contents.
11072 :
11073 : Conceptually, we need to repeat the following operation log2(nvectors)
11074 : times, where hi_start = nvectors / 2:
11075 :
11076 : out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
11077 : out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
11078 :
11079 : However, if each input repeats every N elements and the VF is
11080 : a multiple of N * 2, the HI result is the same as the LO result.
11081 : This will be true for the first N1 iterations of the outer loop,
11082 : followed by N2 iterations for which both the LO and HI results
11083 : are needed. I.e.:
11084 :
11085 : N1 + N2 = log2(nvectors)
11086 :
11087 : Each "N1 iteration" doubles the number of redundant vectors and the
11088 : effect of the process as a whole is to have a sequence of nvectors/2**N1
11089 : vectors that repeats 2**N1 times. Rather than generate these redundant
11090 : vectors, we halve the number of vectors for each N1 iteration. */
11091 : unsigned int in_start = 0;
11092 : unsigned int out_start = nvectors;
11093 : unsigned int new_nvectors = nvectors;
11094 0 : for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
11095 : {
11096 0 : unsigned int hi_start = new_nvectors / 2;
11097 0 : unsigned int out_i = 0;
11098 0 : for (unsigned int in_i = 0; in_i < new_nvectors; ++in_i)
11099 : {
11100 0 : if ((in_i & 1) != 0
11101 0 : && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
11102 : 2 * in_repeat))
11103 0 : continue;
11104 :
11105 0 : tree output = make_ssa_name (new_vector_type);
11106 0 : tree input1 = pieces[in_start + (in_i / 2)];
11107 0 : tree input2 = pieces[in_start + (in_i / 2) + hi_start];
11108 0 : gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
11109 : input1, input2,
11110 : permutes[in_i & 1]);
11111 0 : gimple_seq_add_stmt (seq, stmt);
11112 0 : pieces[out_start + out_i] = output;
11113 0 : out_i += 1;
11114 : }
11115 0 : std::swap (in_start, out_start);
11116 0 : new_nvectors = out_i;
11117 : }
11118 :
11119 : /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
11120 0 : results.reserve (nresults);
11121 0 : for (unsigned int i = 0; i < nresults; ++i)
11122 0 : if (i < new_nvectors)
11123 0 : results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
11124 0 : pieces[in_start + i]));
11125 : else
11126 0 : results.quick_push (results[i - new_nvectors]);
11127 0 : }
11128 :
11129 :
11130 : /* For constant and loop invariant defs in OP_NODE this function creates
11131 : vector defs that will be used in the vectorized stmts and stores them
11132 : to SLP_TREE_VEC_DEFS of OP_NODE. */
11133 :
11134 : static void
11135 508123 : vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
11136 : {
11137 508123 : unsigned HOST_WIDE_INT nunits;
11138 508123 : tree vec_cst;
11139 508123 : unsigned j, number_of_places_in_vector, number_of_places_left_in_vector;
11140 508123 : tree vector_type;
11141 508123 : tree vop;
11142 508123 : unsigned int group_size = op_node->ops.length ();
11143 508123 : unsigned int vec_num, i;
11144 508123 : unsigned number_of_copies = 1;
11145 508123 : bool constant_p;
11146 508123 : gimple_seq ctor_seq = NULL;
11147 508123 : auto_vec<tree, 16> permute_results;
11148 :
11149 : /* We always want SLP_TREE_VECTYPE (op_node) here correctly set. */
11150 508123 : vector_type = SLP_TREE_VECTYPE (op_node);
11151 :
11152 508123 : unsigned int number_of_vectors, excess_elts;
11153 508123 : bool res = vect_get_num_copies_for_invariant (vinfo, op_node,
11154 : &number_of_vectors,
11155 : &excess_elts);
11156 508123 : gcc_assert (res);
11157 508123 : SLP_TREE_VEC_DEFS (op_node).create (number_of_vectors);
11158 508123 : auto_vec<tree> voprnds (number_of_vectors);
11159 :
11160 : /* NUMBER_OF_COPIES is the number of times we need to use the same values in
11161 : created vectors. It is greater than 1 if unrolling is performed.
11162 :
11163 : For example, we have two scalar operands, s1 and s2 (e.g., group of
11164 : strided accesses of size two), while NUNITS is four (i.e., four scalars
11165 : of this type can be packed in a vector). The output vector will contain
11166 : two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
11167 : will be 2).
11168 :
11169 : If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
11170 : containing the operands.
11171 :
11172 : For example, NUNITS is four as before, and the group size is 8
11173 : (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
11174 : {s5, s6, s7, s8}. */
11175 :
11176 508123 : unsigned int npatterns, nelts_per_pattern = 1;
11177 508123 : poly_uint64 type_nunits = TYPE_VECTOR_SUBPARTS (vector_type);
11178 508123 : if (is_a <bb_vec_info> (vinfo))
11179 : {
11180 : /* We don't use duplicate_and_interleave for basic block vectorization.
11181 : If the vector length is unknown then we need two elements per pattern
11182 : to encode an infinite-length tail of constant zeros. */
11183 : if (!type_nunits.is_constant (&nunits))
11184 : {
11185 : nunits = constant_lower_bound (type_nunits);
11186 : nelts_per_pattern = 2;
11187 : }
11188 :
11189 508123 : npatterns = nunits;
11190 : }
11191 : else
11192 : {
11193 : /* When using duplicate_and_interleave, we just need one element for
11194 : each scalar statement. */
11195 508123 : if (!type_nunits.is_constant (&nunits))
11196 : nunits = group_size;
11197 :
11198 508123 : npatterns = nunits;
11199 : }
11200 :
11201 508123 : number_of_copies = ((nunits * number_of_vectors) - excess_elts) / group_size;
11202 :
11203 508123 : constant_p = true;
11204 508123 : tree uniform_elt = NULL_TREE;
11205 508123 : tree_vector_builder elts (vector_type, npatterns, nelts_per_pattern);
11206 508123 : elts.quick_grow (elts.encoded_nelts ());
11207 :
11208 508123 : number_of_places_in_vector = nunits - excess_elts;
11209 508123 : number_of_places_left_in_vector = number_of_places_in_vector;
11210 :
11211 508123 : if (!is_a <bb_vec_info> (vinfo))
11212 : {
11213 : /* Zero-pad the last vector if necessary. */
11214 74527 : for (i = nunits; i > number_of_places_left_in_vector; --i)
11215 252 : elts[i - 1] = build_zero_cst (TREE_TYPE (vector_type));
11216 : }
11217 :
11218 508123 : stmt_vec_info insert_after = NULL;
11219 1524362 : for (j = 0; j < number_of_copies; j++)
11220 : {
11221 1016239 : tree op;
11222 3883649 : for (i = group_size - 1; op_node->ops.iterate (i, &op); i--)
11223 : {
11224 : /* Create 'vect_ = {op0,op1,...,opn}'. */
11225 1851171 : tree orig_op = op;
11226 1851171 : if (number_of_places_left_in_vector == nunits)
11227 : uniform_elt = op;
11228 1208467 : else if (uniform_elt && operand_equal_p (uniform_elt, op))
11229 771073 : op = elts[number_of_places_left_in_vector];
11230 : else
11231 : uniform_elt = NULL_TREE;
11232 1851171 : number_of_places_left_in_vector--;
11233 1851171 : if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
11234 : {
11235 279881 : if (CONSTANT_CLASS_P (op))
11236 : {
11237 102706 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
11238 : {
11239 : /* Can't use VIEW_CONVERT_EXPR for booleans because
11240 : of possibly different sizes of scalar value and
11241 : vector element. */
11242 66 : if (integer_zerop (op))
11243 66 : op = build_int_cst (TREE_TYPE (vector_type), 0);
11244 0 : else if (integer_onep (op))
11245 0 : op = build_all_ones_cst (TREE_TYPE (vector_type));
11246 : else
11247 0 : gcc_unreachable ();
11248 : }
11249 : else
11250 102640 : op = fold_unary (VIEW_CONVERT_EXPR,
11251 : TREE_TYPE (vector_type), op);
11252 102706 : gcc_assert (op && CONSTANT_CLASS_P (op));
11253 : }
11254 : else
11255 : {
11256 177175 : tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
11257 177175 : gimple *init_stmt;
11258 177175 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
11259 : {
11260 430 : tree true_val
11261 430 : = build_all_ones_cst (TREE_TYPE (vector_type));
11262 430 : tree false_val
11263 430 : = build_zero_cst (TREE_TYPE (vector_type));
11264 430 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
11265 430 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
11266 : op, true_val,
11267 : false_val);
11268 : }
11269 : else
11270 : {
11271 176745 : op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
11272 : op);
11273 176745 : init_stmt
11274 176745 : = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
11275 : op);
11276 : }
11277 177175 : gimple_seq_add_stmt (&ctor_seq, init_stmt);
11278 177175 : op = new_temp;
11279 : }
11280 : }
11281 1851171 : elts[number_of_places_left_in_vector] = op;
11282 1851171 : if (!CONSTANT_CLASS_P (op))
11283 : constant_p = false;
11284 : /* For BB vectorization we have to compute an insert location
11285 : when a def is inside the analyzed region since we cannot
11286 : simply insert at the BB start in this case. */
11287 1851171 : stmt_vec_info opdef;
11288 1851171 : if (TREE_CODE (orig_op) == SSA_NAME
11289 184897 : && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
11290 163818 : && is_a <bb_vec_info> (vinfo)
11291 1951810 : && (opdef = vinfo->lookup_def (orig_op)))
11292 : {
11293 80570 : if (!insert_after)
11294 : insert_after = opdef;
11295 : else
11296 44537 : insert_after = get_later_stmt (insert_after, opdef);
11297 : }
11298 :
11299 1851171 : if (number_of_places_left_in_vector == 0)
11300 : {
11301 642733 : if (uniform_elt)
11302 667732 : vec_cst = gimple_build_vector_from_val (&ctor_seq, vector_type,
11303 333866 : elts[0]);
11304 308867 : else if (is_a <bb_vec_info> (vinfo))
11305 : {
11306 304698 : unsigned int encoded_nelts = elts.encoded_nelts ();
11307 304698 : tree zero_val = build_zero_cst (TREE_TYPE (vector_type));
11308 304698 : for (unsigned int k = number_of_places_in_vector;
11309 304704 : k < encoded_nelts; ++k)
11310 6 : elts[k] = zero_val;
11311 304698 : vec_cst = gimple_build_vector (&ctor_seq, &elts);
11312 : }
11313 4169 : else if (constant_p
11314 : ? multiple_p (type_nunits, nunits)
11315 : : known_eq (type_nunits, nunits))
11316 4169 : vec_cst = gimple_build_vector (&ctor_seq, &elts);
11317 : else
11318 : {
11319 : if (permute_results.is_empty ())
11320 : duplicate_and_interleave (vinfo, &ctor_seq, vector_type,
11321 : elts, number_of_vectors,
11322 : permute_results);
11323 : vec_cst = permute_results[number_of_vectors - j - 1];
11324 : }
11325 642733 : if (!gimple_seq_empty_p (ctor_seq))
11326 : {
11327 139606 : if (insert_after)
11328 : {
11329 36033 : gimple_stmt_iterator gsi;
11330 36033 : if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
11331 : {
11332 605 : gsi = gsi_after_labels (gimple_bb (insert_after->stmt));
11333 605 : gsi_insert_seq_before (&gsi, ctor_seq,
11334 : GSI_CONTINUE_LINKING);
11335 : }
11336 35428 : else if (!stmt_ends_bb_p (insert_after->stmt))
11337 : {
11338 35428 : gsi = gsi_for_stmt (insert_after->stmt);
11339 35428 : gsi_insert_seq_after (&gsi, ctor_seq,
11340 : GSI_CONTINUE_LINKING);
11341 : }
11342 : else
11343 : {
11344 : /* When we want to insert after a def where the
11345 : defining stmt throws then insert on the fallthru
11346 : edge. */
11347 0 : edge e = find_fallthru_edge
11348 0 : (gimple_bb (insert_after->stmt)->succs);
11349 0 : basic_block new_bb
11350 0 : = gsi_insert_seq_on_edge_immediate (e, ctor_seq);
11351 0 : gcc_assert (!new_bb);
11352 : }
11353 : }
11354 : else
11355 103573 : vinfo->insert_seq_on_entry (NULL, ctor_seq);
11356 139606 : ctor_seq = NULL;
11357 : }
11358 642733 : voprnds.quick_push (vec_cst);
11359 642733 : insert_after = NULL;
11360 642733 : number_of_places_in_vector = nunits;
11361 642733 : number_of_places_left_in_vector = nunits;
11362 642733 : constant_p = true;
11363 642733 : elts.new_vector (vector_type, npatterns, nelts_per_pattern);
11364 642733 : elts.quick_grow (elts.encoded_nelts ());
11365 : }
11366 : }
11367 : }
11368 :
11369 508123 : gcc_assert (number_of_places_left_in_vector == nunits);
11370 :
11371 : /* Since the vectors are created in the reverse order, we should invert
11372 : them. */
11373 508123 : vec_num = voprnds.length ();
11374 508123 : gcc_checking_assert (vec_num > 0);
11375 :
11376 1150856 : for (j = vec_num; j != 0; j--)
11377 : {
11378 642733 : vop = voprnds[j - 1];
11379 642733 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
11380 : }
11381 :
11382 : /* In case that VF is greater than the unrolling factor needed for the SLP
11383 : group of stmts, NUMBER_OF_VECTORS to be created is greater than
11384 : NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
11385 : to replicate the vectors. */
11386 1016246 : while (number_of_vectors > SLP_TREE_VEC_DEFS (op_node).length ())
11387 0 : for (i = 0; SLP_TREE_VEC_DEFS (op_node).iterate (i, &vop) && i < vec_num;
11388 : i++)
11389 0 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
11390 508123 : }
11391 :
11392 : /* Get the scalar definition of the Nth lane from SLP_NODE or NULL_TREE
11393 : if there is no definition for it in the scalar IL or it is not known. */
11394 :
11395 : tree
11396 84962 : vect_get_slp_scalar_def (slp_tree slp_node, unsigned n)
11397 : {
11398 84962 : if (SLP_TREE_DEF_TYPE (slp_node) == vect_internal_def)
11399 : {
11400 9341 : if (!SLP_TREE_SCALAR_STMTS (slp_node).exists ())
11401 : return NULL_TREE;
11402 9341 : stmt_vec_info def = SLP_TREE_SCALAR_STMTS (slp_node)[n];
11403 9341 : if (!def)
11404 : return NULL_TREE;
11405 9341 : return gimple_get_lhs (STMT_VINFO_STMT (def));
11406 : }
11407 : else
11408 75621 : return SLP_TREE_SCALAR_OPS (slp_node)[n];
11409 : }
11410 :
11411 : /* Get the Ith vectorized definition from SLP_NODE. */
11412 :
11413 : tree
11414 141475 : vect_get_slp_vect_def (slp_tree slp_node, unsigned i)
11415 : {
11416 141475 : return SLP_TREE_VEC_DEFS (slp_node)[i];
11417 : }
11418 :
11419 : /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS. */
11420 :
11421 : void
11422 956462 : vect_get_slp_defs (slp_tree slp_node, vec<tree> *vec_defs)
11423 : {
11424 1912924 : vec_defs->create (SLP_TREE_VEC_DEFS (slp_node).length ());
11425 956462 : vec_defs->splice (SLP_TREE_VEC_DEFS (slp_node));
11426 956462 : }
11427 :
11428 : /* Get N vectorized definitions for SLP_NODE. */
11429 :
11430 : void
11431 3409 : vect_get_slp_defs (vec_info *,
11432 : slp_tree slp_node, vec<vec<tree> > *vec_oprnds, unsigned n)
11433 : {
11434 3409 : if (n == -1U)
11435 3409 : n = SLP_TREE_CHILDREN (slp_node).length ();
11436 :
11437 11877 : for (unsigned i = 0; i < n; ++i)
11438 : {
11439 8468 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[i];
11440 8468 : vec<tree> vec_defs = vNULL;
11441 8468 : vect_get_slp_defs (child, &vec_defs);
11442 8468 : vec_oprnds->quick_push (vec_defs);
11443 : }
11444 3409 : }
11445 :
11446 : /* A subroutine of vect_transform_slp_perm_load with two extra arguments:
11447 : - PERM gives the permutation that the caller wants to use for NODE,
11448 : which might be different from SLP_LOAD_PERMUTATION.
11449 : - DUMP_P controls whether the function dumps information. */
11450 :
11451 : static bool
11452 133018 : vect_transform_slp_perm_load_1 (vec_info *vinfo, slp_tree node,
11453 : load_permutation_t &perm,
11454 : const vec<tree> &dr_chain,
11455 : gimple_stmt_iterator *gsi, poly_uint64 vf,
11456 : bool analyze_only, bool dump_p,
11457 : vect_load_store_data *perm_data,
11458 : unsigned *n_perms, unsigned int *n_loads,
11459 : bool dce_chain)
11460 : {
11461 133018 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
11462 133018 : int vec_index = 0;
11463 133018 : tree vectype = SLP_TREE_VECTYPE (node);
11464 133018 : unsigned int group_size = SLP_TREE_LANES (node);
11465 133018 : unsigned int mask_element;
11466 133018 : unsigned dr_group_size;
11467 133018 : machine_mode mode;
11468 :
11469 133018 : if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
11470 : {
11471 : /* We have both splats of the same non-grouped load and groups
11472 : of distinct invariant loads entering here. */
11473 1510 : unsigned max_idx = 0;
11474 8356 : for (auto idx : perm)
11475 3826 : max_idx = idx > max_idx ? idx : max_idx;
11476 1510 : dr_group_size = max_idx + 1;
11477 : }
11478 : else
11479 : {
11480 131508 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
11481 131508 : dr_group_size = DR_GROUP_SIZE (stmt_info);
11482 : }
11483 :
11484 133018 : mode = TYPE_MODE (vectype);
11485 133018 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11486 133018 : unsigned int nstmts = vect_get_num_copies (vinfo, node);
11487 :
11488 : /* Initialize the vect stmts of NODE to properly insert the generated
11489 : stmts later. */
11490 133018 : if (! analyze_only)
11491 59539 : for (unsigned i = SLP_TREE_VEC_DEFS (node).length (); i < nstmts; i++)
11492 22753 : SLP_TREE_VEC_DEFS (node).quick_push (NULL_TREE);
11493 :
11494 : /* Generate permutation masks for every NODE. Number of masks for each NODE
11495 : is equal to GROUP_SIZE.
11496 : E.g., we have a group of three nodes with three loads from the same
11497 : location in each node, and the vector size is 4. I.e., we have a
11498 : a0b0c0a1b1c1... sequence and we need to create the following vectors:
11499 : for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
11500 : for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
11501 : ...
11502 :
11503 : The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
11504 : The last mask is illegal since we assume two operands for permute
11505 : operation, and the mask element values can't be outside that range.
11506 : Hence, the last mask must be converted into {2,5,5,5}.
11507 : For the first two permutations we need the first and the second input
11508 : vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
11509 : we need the second and the third vectors: {b1,c1,a2,b2} and
11510 : {c2,a3,b3,c3}. */
11511 :
11512 133018 : int vect_stmts_counter = 0;
11513 133018 : unsigned int index = 0;
11514 133018 : int first_vec_index = -1;
11515 133018 : int second_vec_index = -1;
11516 133018 : bool noop_p = true;
11517 133018 : *n_perms = 0;
11518 133018 : unsigned int perm_count = 0;
11519 :
11520 133018 : vec_perm_builder mask;
11521 133018 : unsigned int nelts_to_build;
11522 133018 : unsigned int nvectors_per_build;
11523 133018 : unsigned int in_nlanes;
11524 133018 : bool repeating_p = (group_size == dr_group_size
11525 168596 : && multiple_p (nunits, group_size));
11526 133018 : if (repeating_p)
11527 : {
11528 : /* A single vector contains a whole number of copies of the node, so:
11529 : (a) all permutes can use the same mask; and
11530 : (b) the permutes only need a single vector input. */
11531 33079 : mask.new_vector (nunits, group_size, 3);
11532 33079 : nelts_to_build = mask.encoded_nelts ();
11533 : /* It's possible to obtain zero nstmts during analyze_only, so make
11534 : it at least one to ensure the later computation for n_perms
11535 : proceed. */
11536 33079 : nvectors_per_build = nstmts > 0 ? nstmts : 1;
11537 33079 : in_nlanes = dr_group_size * 3;
11538 : }
11539 : else
11540 : {
11541 : /* We need to construct a separate mask for each vector statement. */
11542 99939 : unsigned HOST_WIDE_INT const_nunits, const_vf;
11543 99939 : if (!nunits.is_constant (&const_nunits)
11544 99939 : || !vf.is_constant (&const_vf))
11545 : return false;
11546 99939 : mask.new_vector (const_nunits, const_nunits, 1);
11547 99939 : nelts_to_build = const_vf * group_size;
11548 99939 : nvectors_per_build = 1;
11549 99939 : in_nlanes = const_vf * dr_group_size;
11550 : }
11551 133018 : auto_sbitmap used_in_lanes (in_nlanes);
11552 133018 : bitmap_clear (used_in_lanes);
11553 133018 : auto_bitmap used_defs;
11554 :
11555 133018 : unsigned int count = mask.encoded_nelts ();
11556 133018 : mask.quick_grow (count);
11557 :
11558 133018 : unsigned int active_nelts_to_build = nelts_to_build;
11559 133018 : if (!repeating_p)
11560 99939 : nelts_to_build = CEIL (nelts_to_build, count) * count;
11561 133018 : vec_perm_indices indices;
11562 :
11563 695785 : for (unsigned int j = 0; j < nelts_to_build; j++)
11564 : {
11565 574401 : if (j >= active_nelts_to_build)
11566 : {
11567 : /* Fill unused lanes of a partial final SLP vector. Their
11568 : values are irrelevant, but the permutation mask itself
11569 : must describe a complete vector. The optimal choice
11570 : would be an identity mapping. */
11571 : mask_element = index;
11572 : }
11573 : else
11574 : {
11575 574401 : unsigned int iter_num = j / group_size;
11576 574401 : unsigned int stmt_num = j % group_size;
11577 574401 : unsigned int i = (iter_num * dr_group_size + perm[stmt_num]);
11578 574401 : bitmap_set_bit (used_in_lanes, i);
11579 574401 : if (repeating_p)
11580 : {
11581 : first_vec_index = 0;
11582 : mask_element = i;
11583 : }
11584 : else
11585 : {
11586 : /* Enforced before the loop when !repeating_p. */
11587 358587 : unsigned int const_nunits = nunits.to_constant ();
11588 358587 : vec_index = i / const_nunits;
11589 358587 : mask_element = i % const_nunits;
11590 358587 : if (vec_index == first_vec_index
11591 358587 : || first_vec_index == -1)
11592 : {
11593 : first_vec_index = vec_index;
11594 : }
11595 143615 : else if (vec_index == second_vec_index
11596 143615 : || second_vec_index == -1)
11597 : {
11598 137027 : second_vec_index = vec_index;
11599 137027 : mask_element += const_nunits;
11600 : }
11601 : else
11602 : {
11603 6588 : if (dump_p)
11604 286 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11605 : "permutation requires at "
11606 : "least three vectors %G",
11607 : stmt_info->stmt);
11608 6588 : gcc_assert (analyze_only);
11609 : return false;
11610 : }
11611 :
11612 351999 : gcc_assert (mask_element < 2 * const_nunits);
11613 : }
11614 : }
11615 :
11616 567813 : if (mask_element != index)
11617 363155 : noop_p = false;
11618 567813 : mask[index++] = mask_element;
11619 :
11620 567813 : if (index == count)
11621 : {
11622 152086 : if (!noop_p)
11623 : {
11624 126808 : indices.new_vector (mask, second_vec_index == -1 ? 1 : 2, nunits);
11625 126808 : if (!can_vec_perm_const_p (mode, mode, indices))
11626 : {
11627 5046 : if (dump_p)
11628 : {
11629 90 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11630 : "unsupported vect permute { ");
11631 876 : for (unsigned i = 0; i < count; ++i)
11632 : {
11633 786 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
11634 786 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
11635 : }
11636 90 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
11637 : }
11638 5046 : gcc_assert (analyze_only);
11639 : return false;
11640 : }
11641 :
11642 121762 : tree mask_vec = NULL_TREE;
11643 121762 : if (perm_data)
11644 : {
11645 93582 : if (analyze_only)
11646 : {
11647 72541 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
11648 72541 : perm_data->perms.safe_push
11649 72541 : ({mask_vec, nvectors_per_build, indices.ninputs ()});
11650 : }
11651 : else
11652 21041 : mask_vec = perm_data->perms[perm_count].mask;
11653 93582 : perm_count++;
11654 : }
11655 :
11656 121762 : if (second_vec_index == -1)
11657 36683 : second_vec_index = first_vec_index;
11658 :
11659 244349 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
11660 : {
11661 122587 : ++*n_perms;
11662 122587 : if (analyze_only)
11663 101263 : continue;
11664 : /* Generate the permute statement if necessary. */
11665 21324 : tree first_vec = dr_chain[first_vec_index + ri];
11666 21324 : tree second_vec = dr_chain[second_vec_index + ri];
11667 21324 : gassign *stmt = as_a<gassign *> (stmt_info->stmt);
11668 21324 : tree perm_dest
11669 21324 : = vect_create_destination_var (gimple_assign_lhs (stmt),
11670 : vectype);
11671 21324 : perm_dest = make_ssa_name (perm_dest);
11672 21324 : gimple *perm_stmt
11673 21324 : = gimple_build_assign (perm_dest, VEC_PERM_EXPR, first_vec,
11674 : second_vec, mask_vec);
11675 21324 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
11676 : gsi);
11677 21324 : if (dce_chain)
11678 : {
11679 20400 : bitmap_set_bit (used_defs, first_vec_index + ri);
11680 20400 : bitmap_set_bit (used_defs, second_vec_index + ri);
11681 : }
11682 :
11683 : /* Store the vector statement in NODE. */
11684 21324 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = perm_dest;
11685 : }
11686 : }
11687 25278 : else if (!analyze_only)
11688 : {
11689 2858 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
11690 : {
11691 1429 : tree first_vec = dr_chain[first_vec_index + ri];
11692 : /* If mask was NULL_TREE generate the requested
11693 : identity transform. */
11694 1429 : if (dce_chain)
11695 1422 : bitmap_set_bit (used_defs, first_vec_index + ri);
11696 :
11697 : /* Store the vector statement in NODE. */
11698 1429 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = first_vec;
11699 : }
11700 : }
11701 :
11702 : index = 0;
11703 : first_vec_index = -1;
11704 : second_vec_index = -1;
11705 : noop_p = true;
11706 : }
11707 : }
11708 :
11709 121384 : if (n_loads)
11710 : {
11711 83483 : if (repeating_p)
11712 9355 : *n_loads = nstmts;
11713 : else
11714 : {
11715 : /* Enforced above when !repeating_p. */
11716 74128 : unsigned int const_nunits = nunits.to_constant ();
11717 74128 : *n_loads = 0;
11718 74128 : bool load_seen = false;
11719 1013550 : for (unsigned i = 0; i < in_nlanes; ++i)
11720 : {
11721 939422 : if (i % const_nunits == 0)
11722 : {
11723 395196 : if (load_seen)
11724 110074 : *n_loads += 1;
11725 : load_seen = false;
11726 : }
11727 939422 : if (bitmap_bit_p (used_in_lanes, i))
11728 253429 : load_seen = true;
11729 : }
11730 74128 : if (load_seen)
11731 52481 : *n_loads += 1;
11732 : }
11733 : }
11734 :
11735 121384 : if (dce_chain)
11736 208261 : for (unsigned i = 0; i < dr_chain.length (); ++i)
11737 75243 : if (!bitmap_bit_p (used_defs, i))
11738 : {
11739 40605 : tree def = dr_chain[i];
11740 40982 : do
11741 : {
11742 40982 : gimple *stmt = SSA_NAME_DEF_STMT (def);
11743 40982 : if (is_gimple_assign (stmt)
11744 40982 : && (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
11745 40982 : || gimple_assign_rhs_code (stmt) == CONSTRUCTOR))
11746 4990 : def = single_ssa_tree_operand (stmt, SSA_OP_USE);
11747 : else
11748 : def = NULL;
11749 40982 : gimple_stmt_iterator rgsi = gsi_for_stmt (stmt);
11750 40982 : gsi_remove (&rgsi, true);
11751 40982 : release_defs (stmt);
11752 : }
11753 40982 : while (def);
11754 : }
11755 :
11756 : return true;
11757 133018 : }
11758 :
11759 : /* Generate vector permute statements from a list of loads in DR_CHAIN.
11760 : If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
11761 : permute statements for the SLP node NODE. Store the number of vector
11762 : permute instructions in *N_PERMS and the number of vector load
11763 : instructions in *N_LOADS. If DCE_CHAIN is true, remove all definitions
11764 : that were not needed. */
11765 :
11766 : bool
11767 93533 : vect_transform_slp_perm_load (vec_info *vinfo,
11768 : slp_tree node, const vec<tree> &dr_chain,
11769 : gimple_stmt_iterator *gsi, poly_uint64 vf,
11770 : bool analyze_only, vect_load_store_data *ls_data,
11771 : unsigned *n_perms, unsigned int *n_loads,
11772 : bool dce_chain)
11773 : {
11774 93533 : if (analyze_only && ls_data)
11775 75140 : ls_data->perms.truncate (0);
11776 93533 : bool ok = vect_transform_slp_perm_load_1 (vinfo, node,
11777 93533 : SLP_TREE_LOAD_PERMUTATION (node),
11778 : dr_chain, gsi, vf, analyze_only,
11779 : dump_enabled_p (), ls_data,
11780 : n_perms, n_loads, dce_chain);
11781 93533 : return ok;
11782 : }
11783 :
11784 : /* Produce the next vector result for SLP permutation NODE by adding a vector
11785 : statement at GSI. If MASK_VEC is nonnull, add:
11786 :
11787 : <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
11788 :
11789 : otherwise add:
11790 :
11791 : <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF,
11792 : { N, N+1, N+2, ... }>
11793 :
11794 : where N == IDENTITY_OFFSET which is either zero or equal to the
11795 : number of elements of the result. */
11796 :
11797 : static void
11798 32317 : vect_add_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
11799 : slp_tree node, tree first_def, tree second_def,
11800 : tree mask_vec, poly_uint64 identity_offset)
11801 : {
11802 32317 : tree vectype = SLP_TREE_VECTYPE (node);
11803 :
11804 : /* ??? We SLP match existing vector element extracts but
11805 : allow punning which we need to re-instantiate at uses
11806 : but have no good way of explicitly representing. */
11807 32317 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)), TYPE_SIZE (vectype))
11808 32317 : && !types_compatible_p (TREE_TYPE (first_def), vectype))
11809 : {
11810 15 : gassign *conv_stmt
11811 15 : = gimple_build_assign (make_ssa_name (vectype),
11812 : build1 (VIEW_CONVERT_EXPR, vectype, first_def));
11813 15 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
11814 15 : first_def = gimple_assign_lhs (conv_stmt);
11815 : }
11816 32317 : gassign *perm_stmt;
11817 32317 : if (mask_vec)
11818 : {
11819 28378 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)),
11820 28378 : TYPE_SIZE (vectype))
11821 28378 : && !types_compatible_p (TREE_TYPE (second_def), vectype))
11822 : {
11823 8 : gassign *conv_stmt
11824 8 : = gimple_build_assign (make_ssa_name (vectype),
11825 : build1 (VIEW_CONVERT_EXPR,
11826 : vectype, second_def));
11827 8 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
11828 8 : second_def = gimple_assign_lhs (conv_stmt);
11829 : }
11830 28378 : tree perm_dest = make_ssa_name (vectype);
11831 28378 : perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
11832 : first_def, second_def,
11833 : mask_vec);
11834 : }
11835 : else
11836 : {
11837 3939 : auto def_nunits = TYPE_VECTOR_SUBPARTS (TREE_TYPE (first_def));
11838 3939 : unsigned HOST_WIDE_INT vecno;
11839 3939 : poly_uint64 eltno;
11840 3939 : if (!can_div_trunc_p (poly_uint64 (identity_offset), def_nunits,
11841 : &vecno, &eltno))
11842 : gcc_unreachable ();
11843 3939 : tree def = vecno & 1 ? second_def : first_def;
11844 3939 : if (!types_compatible_p (TREE_TYPE (def), vectype))
11845 : {
11846 : /* For identity permutes we still need to handle the case
11847 : of offsetted extracts or concats. */
11848 383 : tree perm_dest = make_ssa_name (vectype);
11849 383 : unsigned HOST_WIDE_INT c;
11850 383 : if (known_le (TYPE_VECTOR_SUBPARTS (vectype), def_nunits))
11851 : {
11852 379 : unsigned HOST_WIDE_INT elsz
11853 379 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
11854 758 : tree lowpart = build3 (BIT_FIELD_REF, vectype, def,
11855 379 : TYPE_SIZE (vectype),
11856 379 : bitsize_int (eltno * elsz));
11857 379 : perm_stmt = gimple_build_assign (perm_dest, lowpart);
11858 : }
11859 4 : else if (constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
11860 4 : def_nunits, &c) && c == 2)
11861 : {
11862 4 : gcc_assert (known_eq (identity_offset, 0U));
11863 4 : tree ctor = build_constructor_va (vectype, 2,
11864 : NULL_TREE, first_def,
11865 : NULL_TREE, second_def);
11866 4 : perm_stmt = gimple_build_assign (perm_dest, ctor);
11867 : }
11868 : else
11869 0 : gcc_unreachable ();
11870 : }
11871 : else
11872 : {
11873 3556 : gcc_assert (known_eq (eltno, 0U));
11874 3556 : node->push_vec_def (def);
11875 3556 : return;
11876 : }
11877 : }
11878 28761 : vect_finish_stmt_generation (vinfo, NULL, perm_stmt, gsi);
11879 : /* Store the vector statement in NODE. */
11880 28761 : node->push_vec_def (perm_stmt);
11881 : }
11882 :
11883 : /* Subroutine of vectorizable_slp_permutation. Check whether the target
11884 : can perform permutation PERM on the (1 or 2) input nodes in CHILDREN.
11885 : If GSI is nonnull, emit the permutation there.
11886 :
11887 : When GSI is null, the only purpose of NODE is to give properties
11888 : of the result, such as the vector type and number of SLP lanes.
11889 : The node does not need to be a VEC_PERM_EXPR.
11890 :
11891 : If the target supports the operation, return the number of individual
11892 : VEC_PERM_EXPRs needed, otherwise return -1. Print information to the
11893 : dump file if DUMP_P is true. */
11894 :
11895 : static int
11896 483235 : vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
11897 : slp_tree node, lane_permutation_t &perm,
11898 : vec<slp_tree> &children, bool dump_p,
11899 : vect_permute_data *perm_data)
11900 : {
11901 483235 : tree vectype = SLP_TREE_VECTYPE (node);
11902 :
11903 : /* ??? We currently only support all same vector input types
11904 : while the SLP IL should really do a concat + select and thus accept
11905 : arbitrary mismatches. */
11906 483235 : slp_tree child;
11907 483235 : unsigned i;
11908 483235 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11909 483235 : bool repeating_p = multiple_p (nunits, SLP_TREE_LANES (node));
11910 : /* True if we're permuting a single input of 2N vectors down
11911 : to N vectors. This case doesn't generalize beyond 2 since
11912 : VEC_PERM_EXPR only takes 2 inputs. */
11913 483235 : bool pack_p = false;
11914 : /* If we're permuting inputs of N vectors each into X*N outputs,
11915 : this is the value of X, otherwise it is 1. */
11916 483235 : unsigned int unpack_factor = 1;
11917 483235 : tree op_vectype = NULL_TREE;
11918 484854 : FOR_EACH_VEC_ELT (children, i, child)
11919 484753 : if (SLP_TREE_VECTYPE (child))
11920 : {
11921 : op_vectype = SLP_TREE_VECTYPE (child);
11922 : break;
11923 : }
11924 483235 : if (!op_vectype)
11925 101 : op_vectype = vectype;
11926 1017422 : FOR_EACH_VEC_ELT (children, i, child)
11927 : {
11928 534187 : if ((SLP_TREE_DEF_TYPE (child) != vect_internal_def
11929 19242 : && !vect_maybe_update_slp_op_vectype (child, op_vectype))
11930 534187 : || !types_compatible_p (SLP_TREE_VECTYPE (child), op_vectype)
11931 1068374 : || !types_compatible_p (TREE_TYPE (vectype), TREE_TYPE (op_vectype)))
11932 : {
11933 0 : if (dump_p)
11934 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11935 : "Unsupported vector types in lane permutation\n");
11936 483235 : return -1;
11937 : }
11938 534187 : auto op_nunits = TYPE_VECTOR_SUBPARTS (op_vectype);
11939 534187 : unsigned int this_unpack_factor;
11940 : /* Detect permutations of external, pre-existing vectors. The external
11941 : node's SLP_TREE_LANES stores the total number of units in the vector,
11942 : or zero if the vector has variable length.
11943 :
11944 : We are expected to keep the original VEC_PERM_EXPR for such cases.
11945 : There is no repetition to model. */
11946 534187 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def
11947 534187 : && SLP_TREE_SCALAR_OPS (child).is_empty ())
11948 : repeating_p = false;
11949 : /* Check whether the input has twice as many lanes per vector. */
11950 517709 : else if (children.length () == 1
11951 517709 : && known_eq (SLP_TREE_LANES (child) * nunits,
11952 : SLP_TREE_LANES (node) * op_nunits * 2))
11953 : pack_p = true;
11954 : /* Check whether the output has N times as many lanes per vector. */
11955 534187 : else if (constant_multiple_p (SLP_TREE_LANES (node) * op_nunits,
11956 473411 : SLP_TREE_LANES (child) * nunits,
11957 : &this_unpack_factor)
11958 438413 : && (i == 0 || unpack_factor == this_unpack_factor))
11959 : unpack_factor = this_unpack_factor;
11960 : else
11961 : repeating_p = false;
11962 : }
11963 :
11964 966470 : gcc_assert (perm.length () == SLP_TREE_LANES (node));
11965 :
11966 : /* Load-lanes permute. This permute only acts as a forwarder to
11967 : select the correct vector def of the load-lanes load which
11968 : has the permuted vectors in its vector defs like
11969 : { v0, w0, r0, v1, w1, r1 ... } for a ld3. All costs are
11970 : accounted for in the costing for the actual load so we
11971 : return zero here. */
11972 483235 : if (node->ldst_lanes)
11973 : {
11974 0 : gcc_assert (children.length () == 1);
11975 0 : if (!gsi)
11976 : /* This is a trivial op always supported. */
11977 : return 0;
11978 0 : slp_tree child = children[0];
11979 0 : unsigned vec_idx = (SLP_TREE_LANE_PERMUTATION (node)[0].second
11980 0 : / SLP_TREE_LANES (node));
11981 0 : unsigned vec_num = SLP_TREE_LANES (child) / SLP_TREE_LANES (node);
11982 0 : unsigned nvectors = vect_get_num_copies (vinfo, node);
11983 0 : for (unsigned i = 0; i < nvectors; ++i)
11984 : {
11985 0 : tree def = SLP_TREE_VEC_DEFS (child)[i * vec_num + vec_idx];
11986 0 : node->push_vec_def (def);
11987 : }
11988 : return 0;
11989 : }
11990 :
11991 : /* Set REPEATING_P to true if the permutations are cyclical wrt UNPACK_FACTOR
11992 : and if we can generate the vectors in a vector-length agnostic way.
11993 : This requires UNPACK_STEP == NUNITS / UNPACK_FACTOR to be known at
11994 : compile time.
11995 :
11996 : The significance of UNPACK_STEP is that, when PACK_P is false,
11997 : output vector I operates on a window of UNPACK_STEP elements from each
11998 : input, starting at lane UNPACK_STEP * (I % UNPACK_FACTOR). For example,
11999 : when UNPACK_FACTOR is 2, the first output vector operates on lanes
12000 : [0, NUNITS / 2 - 1] of each input vector and the second output vector
12001 : operates on lanes [NUNITS / 2, NUNITS - 1] of each input vector.
12002 :
12003 : When REPEATING_P is true, NOUTPUTS holds the total number of outputs
12004 : that we actually need to generate. */
12005 483235 : uint64_t noutputs = 0;
12006 483235 : poly_uint64 unpack_step = 0;
12007 483235 : loop_vec_info linfo = dyn_cast <loop_vec_info> (vinfo);
12008 210675 : if (!linfo
12009 522444 : || !multiple_p (nunits, unpack_factor, &unpack_step)
12010 209720 : || !constant_multiple_p (LOOP_VINFO_VECT_FACTOR (linfo)
12011 209720 : * SLP_TREE_LANES (node), nunits, &noutputs))
12012 : repeating_p = false;
12013 :
12014 : /* We can handle the conditions described for REPEATING_P above for
12015 : both variable- and constant-length vectors. The fallback requires
12016 : us to generate every element of every permute vector explicitly,
12017 : which is only possible for constant-length permute vectors.
12018 :
12019 : Set:
12020 :
12021 : - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
12022 : mask vectors that we want to build.
12023 :
12024 : - NCOPIES to the number of copies of PERM that we need in order
12025 : to build the necessary permute mask vectors. */
12026 209720 : uint64_t npatterns;
12027 209720 : unsigned nelts_per_pattern;
12028 209720 : uint64_t ncopies;
12029 209720 : if (repeating_p)
12030 : {
12031 : /* We need permute mask vectors that have the form:
12032 :
12033 : { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
12034 :
12035 : In other words, the original n-element permute in PERM is
12036 : "unrolled" to fill a full vector. The stepped vector encoding
12037 : that we use for permutes requires 3n elements. */
12038 170511 : npatterns = SLP_TREE_LANES (node);
12039 170511 : nelts_per_pattern = ncopies = 3;
12040 : }
12041 : else
12042 : {
12043 : /* Calculate every element of every permute mask vector explicitly,
12044 : instead of relying on the pattern described above. */
12045 312724 : if (!nunits.is_constant (&npatterns)
12046 312724 : || !TYPE_VECTOR_SUBPARTS (op_vectype).is_constant ())
12047 : {
12048 : if (dump_p)
12049 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12050 : "unsupported permutation %p on variable-length"
12051 : " vectors\n", (void *) node);
12052 : return -1;
12053 : }
12054 312724 : nelts_per_pattern = ncopies = 1;
12055 312724 : if (linfo && !LOOP_VINFO_VECT_FACTOR (linfo).is_constant (&ncopies))
12056 : {
12057 : if (dump_p)
12058 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12059 : "unsupported permutation %p for variable VF\n",
12060 : (void *) node);
12061 : return -1;
12062 : }
12063 : pack_p = false;
12064 : unpack_factor = 1;
12065 : }
12066 483235 : unsigned olanes = unpack_factor * ncopies * SLP_TREE_LANES (node);
12067 483235 : gcc_assert (repeating_p || multiple_p (olanes, nunits));
12068 :
12069 : /* Compute the { { SLP operand, vector index}, lane } permutation sequence
12070 : from the { SLP operand, scalar lane } permutation as recorded in the
12071 : SLP node as intermediate step. This part should already work
12072 : with SLP children with arbitrary number of lanes. */
12073 483235 : auto_vec<std::pair<std::pair<unsigned, unsigned>, poly_uint64>> vperm;
12074 483235 : auto_vec<poly_uint64> active_lane;
12075 483235 : vperm.create (olanes);
12076 483235 : active_lane.safe_grow_cleared (children.length (), true);
12077 1458255 : for (unsigned int ui = 0; ui < unpack_factor; ++ui)
12078 : {
12079 2086096 : for (unsigned j = 0; j < children.length (); ++j)
12080 551263 : active_lane[j] = ui * unpack_step;
12081 1439819 : for (unsigned i = 0; i < ncopies; ++i)
12082 : {
12083 5963660 : for (unsigned pi = 0; pi < perm.length (); ++pi)
12084 : {
12085 2033796 : std::pair<unsigned, unsigned> p = perm[pi];
12086 2033796 : tree vtype = SLP_TREE_VECTYPE (children[p.first]);
12087 2033796 : if (repeating_p)
12088 1001448 : vperm.quick_push ({{p.first, 0},
12089 1001448 : p.second + active_lane[p.first]});
12090 : else
12091 : {
12092 : /* We checked above that the vectors are constant-length. */
12093 1032348 : unsigned vnunits = TYPE_VECTOR_SUBPARTS (vtype)
12094 1032348 : .to_constant ();
12095 1032348 : unsigned lane = active_lane[p.first].to_constant ();
12096 1032348 : unsigned vi = (lane + p.second) / vnunits;
12097 1032348 : unsigned vl = (lane + p.second) % vnunits;
12098 1032348 : vperm.quick_push ({{p.first, vi}, vl});
12099 : }
12100 : }
12101 : /* Advance to the next group. */
12102 2032229 : for (unsigned j = 0; j < children.length (); ++j)
12103 1084195 : active_lane[j] += SLP_TREE_LANES (children[j]);
12104 : }
12105 : }
12106 :
12107 483235 : if (dump_p)
12108 : {
12109 9078 : dump_printf_loc (MSG_NOTE, vect_location,
12110 : "vectorizing permutation %p", (void *)node);
12111 32840 : for (unsigned i = 0; i < perm.length (); ++i)
12112 23762 : dump_printf (MSG_NOTE, " op%u[%u]", perm[i].first, perm[i].second);
12113 9078 : if (repeating_p)
12114 7604 : dump_printf (MSG_NOTE, " (repeat %d)", SLP_TREE_LANES (node));
12115 9078 : dump_printf (MSG_NOTE, "\n");
12116 9078 : dump_printf_loc (MSG_NOTE, vect_location, "as");
12117 91443 : for (unsigned i = 0; i < vperm.length (); ++i)
12118 : {
12119 82365 : if (i != 0
12120 82365 : && (repeating_p
12121 55555 : ? multiple_p (i, npatterns)
12122 82365 : : multiple_p (i, TYPE_VECTOR_SUBPARTS (vectype))))
12123 24517 : dump_printf (MSG_NOTE, ",");
12124 82365 : dump_printf (MSG_NOTE, " vops%u[%u][",
12125 82365 : vperm[i].first.first, vperm[i].first.second);
12126 82365 : dump_dec (MSG_NOTE, vperm[i].second);
12127 82365 : dump_printf (MSG_NOTE, "]");
12128 : }
12129 9078 : dump_printf (MSG_NOTE, "\n");
12130 : }
12131 :
12132 : /* We can only handle two-vector permutes, everything else should
12133 : be lowered on the SLP level. The following is closely inspired
12134 : by vect_transform_slp_perm_load and is supposed to eventually
12135 : replace it.
12136 : ??? As intermediate step do code-gen in the SLP tree representation
12137 : somehow? */
12138 483235 : std::pair<unsigned, unsigned> first_vec = std::make_pair (-1U, -1U);
12139 483235 : std::pair<unsigned, unsigned> second_vec = std::make_pair (-1U, -1U);
12140 483235 : unsigned int index = 0;
12141 483235 : poly_uint64 mask_element;
12142 483235 : vec_perm_builder mask;
12143 483235 : mask.new_vector (nunits, npatterns, nelts_per_pattern);
12144 483235 : unsigned int count = mask.encoded_nelts ();
12145 483235 : mask.quick_grow (count);
12146 483235 : vec_perm_indices indices;
12147 483235 : unsigned nperms = 0;
12148 483235 : unsigned int perm_count = 0;
12149 : /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
12150 : vectors to check during analysis, but we need to generate NOUTPUTS
12151 : vectors during transformation. */
12152 483235 : unsigned total_nelts = olanes;
12153 483235 : unsigned process_nelts = olanes;
12154 483235 : if (repeating_p)
12155 : {
12156 170511 : total_nelts = (total_nelts / unpack_factor) * noutputs;
12157 170511 : if (gsi)
12158 9991 : process_nelts = total_nelts;
12159 : }
12160 483235 : unsigned last_ei = (total_nelts - 1) % process_nelts;
12161 2520744 : for (unsigned i = 0; i < process_nelts; ++i)
12162 : {
12163 : /* VI is the input vector index when generating code for REPEATING_P. */
12164 2048279 : unsigned vi = i / olanes * (pack_p ? 2 : 1);
12165 2048279 : unsigned ei = i % olanes;
12166 2048279 : mask_element = vperm[ei].second;
12167 2048279 : if (ei == 0)
12168 486099 : perm_count = 0;
12169 2048279 : if (pack_p)
12170 : {
12171 : /* In this case, we have N outputs and the single child provides 2N
12172 : inputs. Output X permutes inputs 2X and 2X+1.
12173 :
12174 : The mask indices are taken directly from the SLP permutation node.
12175 : Index X selects from the first vector if (X / NUNITS) % 2 == 0;
12176 : X selects from the second vector otherwise. These conditions
12177 : are only known at compile time for constant-length vectors. */
12178 : first_vec = std::make_pair (0, 0);
12179 : second_vec = std::make_pair (0, 1);
12180 : }
12181 1877099 : else if (first_vec.first == -1U
12182 1877099 : || first_vec == vperm[ei].first)
12183 1662354 : first_vec = vperm[ei].first;
12184 214745 : else if (second_vec.first == -1U
12185 214745 : || second_vec == vperm[ei].first)
12186 : {
12187 214333 : second_vec = vperm[ei].first;
12188 214333 : mask_element += nunits;
12189 : }
12190 : else
12191 : {
12192 412 : if (dump_p)
12193 11 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12194 : "permutation requires at "
12195 : "least three vectors\n");
12196 412 : gcc_assert (!gsi);
12197 : return -1;
12198 : }
12199 :
12200 2047867 : mask[index++] = mask_element;
12201 :
12202 2047867 : if (index == count)
12203 : {
12204 629453 : indices.new_vector (mask, second_vec.first == -1U ? 1 : 2,
12205 : TYPE_VECTOR_SUBPARTS (op_vectype));
12206 629453 : bool identity_p = (indices.series_p (0, 1, mask[0], 1)
12207 978135 : && constant_multiple_p (mask[0], nunits));
12208 629453 : machine_mode vmode = TYPE_MODE (vectype);
12209 629453 : machine_mode op_vmode = TYPE_MODE (op_vectype);
12210 629453 : unsigned HOST_WIDE_INT c;
12211 629453 : if ((!identity_p
12212 579422 : && !can_vec_perm_const_p (vmode, op_vmode, indices))
12213 629453 : || (identity_p
12214 50031 : && !known_le (nunits,
12215 : TYPE_VECTOR_SUBPARTS (op_vectype))
12216 8 : && (!constant_multiple_p (nunits,
12217 10366 : TYPE_VECTOR_SUBPARTS (op_vectype),
12218 8 : &c) || c != 2)))
12219 : {
12220 10358 : if (dump_p)
12221 : {
12222 154 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
12223 : vect_location,
12224 : "unsupported vect permute { ");
12225 1596 : for (i = 0; i < count; ++i)
12226 : {
12227 1442 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
12228 1442 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
12229 : }
12230 154 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
12231 : }
12232 10358 : gcc_assert (!gsi);
12233 483235 : return -1;
12234 : }
12235 :
12236 619095 : tree mask_vec = NULL_TREE;
12237 619095 : unsigned perm_num
12238 619095 : = CEIL (total_nelts, process_nelts) - (ei > last_ei);
12239 619095 : if (!identity_p && perm_data)
12240 : {
12241 161179 : if (!gsi)
12242 : {
12243 132801 : mask_vec = vect_gen_perm_mask_any (vectype, indices);
12244 132801 : perm_data->perms.safe_push ({mask_vec, perm_num,
12245 132801 : indices.ninputs ()});
12246 : }
12247 : else
12248 28378 : mask_vec = perm_data->perms[perm_count].mask;
12249 161179 : perm_count++;
12250 : }
12251 :
12252 619095 : if (!identity_p)
12253 569064 : nperms += perm_num;
12254 619095 : if (gsi)
12255 : {
12256 32317 : if (second_vec.first == -1U)
12257 7796 : second_vec = first_vec;
12258 :
12259 32317 : slp_tree
12260 32317 : first_node = children[first_vec.first],
12261 32317 : second_node = children[second_vec.first];
12262 :
12263 32317 : tree first_def
12264 32317 : = vect_get_slp_vect_def (first_node, first_vec.second + vi);
12265 32317 : tree second_def
12266 32317 : = vect_get_slp_vect_def (second_node, second_vec.second + vi);
12267 32317 : vect_add_slp_permutation (vinfo, gsi, node, first_def,
12268 32317 : second_def, mask_vec, mask[0]);
12269 : }
12270 :
12271 : index = 0;
12272 : first_vec = std::make_pair (-1U, -1U);
12273 : second_vec = std::make_pair (-1U, -1U);
12274 : }
12275 : }
12276 :
12277 472465 : return nperms;
12278 483235 : }
12279 :
12280 : /* Vectorize the SLP permutations in NODE as specified
12281 : in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
12282 : child number and lane number.
12283 : Interleaving of two two-lane two-child SLP subtrees (not supported):
12284 : [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
12285 : A blend of two four-lane two-child SLP subtrees:
12286 : [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
12287 : Highpart of a four-lane one-child SLP subtree (not supported):
12288 : [ { 0, 2 }, { 0, 3 } ]
12289 : Where currently only a subset is supported by code generating below. */
12290 :
12291 : bool
12292 100352 : vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
12293 : slp_tree node, stmt_vector_for_cost *cost_vec)
12294 : {
12295 100352 : tree vectype = SLP_TREE_VECTYPE (node);
12296 100352 : lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
12297 100352 : vect_permute_data _data;
12298 100352 : vect_permute_data &data = node->get_data (_data);
12299 :
12300 100352 : if (!gsi)
12301 83395 : gcc_checking_assert (!node->data);
12302 100352 : int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
12303 100352 : SLP_TREE_CHILDREN (node),
12304 : dump_enabled_p (), &data);
12305 100352 : if (nperms < 0)
12306 : return false;
12307 :
12308 98713 : if (!gsi && nperms != 0)
12309 74860 : record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
12310 :
12311 98713 : if (!gsi && !node->data)
12312 81756 : node->data = new vect_permute_data (std::move (data));
12313 :
12314 : return true;
12315 100352 : }
12316 :
12317 : /* Vectorize SLP NODE. Only compute the vector insertion places when
12318 : PLACE_ONLY is true. When placing, return false if there is no possible
12319 : schedule. */
12320 :
12321 : static bool
12322 3148218 : vect_schedule_slp_node (vec_info *vinfo,
12323 : slp_tree node, slp_instance instance, bool place_only)
12324 : {
12325 3148218 : int i;
12326 3148218 : slp_tree child;
12327 :
12328 : /* Vectorize externals and constants. */
12329 3148218 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
12330 3148218 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
12331 : {
12332 1210753 : if (place_only)
12333 : {
12334 693785 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
12335 : return true;
12336 307831 : gimple *last_stmt = NULL;
12337 307831 : vec<tree> &defs = (!SLP_TREE_SCALAR_OPS (node).is_empty ()
12338 : ? SLP_TREE_SCALAR_OPS (node)
12339 307831 : : SLP_TREE_VEC_DEFS (node));
12340 1644276 : for (tree def : defs)
12341 : /* If the stmt is not inside the region do not
12342 : use it as possible insertion point. */
12343 720783 : if (auto stmt_info = vinfo->lookup_def (def))
12344 : {
12345 359220 : gimple *stmt = stmt_info->stmt;
12346 359220 : if (!last_stmt)
12347 : last_stmt = stmt;
12348 200612 : else if (vect_stmt_dominates_stmt_p (last_stmt, stmt))
12349 : last_stmt = stmt;
12350 63499 : else if (vect_stmt_dominates_stmt_p (stmt, last_stmt))
12351 : ;
12352 : else
12353 0 : gcc_unreachable ();
12354 : }
12355 307831 : node->si = last_stmt;
12356 307831 : return true;
12357 : }
12358 :
12359 : /* ??? vectorizable_shift can end up using a scalar operand which is
12360 : currently denoted as !SLP_TREE_VECTYPE. No need to vectorize the
12361 : node in this case. */
12362 516968 : if (!SLP_TREE_VECTYPE (node))
12363 : return true;
12364 :
12365 : /* There are two reasons vector defs might already exist. The first
12366 : is that we are vectorizing an existing vector def. The second is
12367 : when performing BB vectorization shared constant/external nodes
12368 : are not split apart during partitioning so during the code-gen
12369 : DFS walk we can end up visiting them twice. */
12370 508932 : if (! SLP_TREE_VEC_DEFS (node).exists ())
12371 508123 : vect_create_constant_vectors (vinfo, node);
12372 : return true;
12373 : }
12374 :
12375 1937465 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
12376 :
12377 1937465 : gcc_assert (SLP_TREE_VEC_DEFS (node).is_empty ());
12378 1937465 : if (!place_only && SLP_TREE_VECTYPE (node))
12379 991421 : SLP_TREE_VEC_DEFS (node).create (vect_get_num_copies (vinfo, node));
12380 :
12381 1937465 : gimple *last_stmt;
12382 1937465 : gimple_stmt_iterator si;
12383 : /* ??? When !place_only we'd like to re-use place_only computed info,
12384 : but this is a bit awkward due to using gsi_insert_before and the
12385 : requirement to insert after vector defs. So we compute last_stmt
12386 : during pre-scheduling and si during scheduling. */
12387 1937465 : if (!SLP_TREE_PERMUTE_P (node) && STMT_VINFO_DATA_REF (stmt_info))
12388 : {
12389 : /* Vectorized loads go before the first scalar load to make it
12390 : ready early, vectorized stores go before the last scalar
12391 : stmt which is where all uses are ready.
12392 : In theory, if we delay dependence checking until after
12393 : placing, we can schedule at other points, but then
12394 : dependence checking would need to honor that. On the
12395 : other hand dependence checking could request a different
12396 : scheduling point as well, if dependences require that. */
12397 1494905 : stmt_vec_info last_stmt_info = NULL;
12398 1494905 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
12399 281042 : last_stmt_info = vect_find_first_scalar_stmt_in_slp (node);
12400 : else /* DR_IS_WRITE */
12401 : {
12402 1213863 : last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
12403 1213863 : if (place_only)
12404 1317978 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12405 : {
12406 658991 : if (child->si
12407 658991 : && !vect_stmt_dominates_stmt_p (child->si,
12408 : last_stmt_info->stmt))
12409 : return false;
12410 : }
12411 : }
12412 1494901 : last_stmt = last_stmt_info->stmt;
12413 1494901 : si = gsi_for_stmt (last_stmt);
12414 1494901 : }
12415 442560 : else if (!SLP_TREE_PERMUTE_P (node)
12416 420772 : && (SLP_TREE_TYPE (node) == cycle_phi_info_type
12417 : || SLP_TREE_TYPE (node) == induc_vec_info_type
12418 : || SLP_TREE_TYPE (node) == phi_info_type))
12419 : {
12420 : /* For PHI node vectorization we do not use the insertion iterator. */
12421 108157 : last_stmt = SLP_TREE_SCALAR_STMTS (node)[0]->stmt;
12422 108157 : if (place_only)
12423 182452 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12424 : {
12425 129093 : if (child->si
12426 224961 : && !dominated_by_p (CDI_DOMINATORS,
12427 : gimple_phi_arg_edge
12428 95868 : (as_a <gphi *> (last_stmt), i)->src,
12429 95868 : gimple_bb (child->si)))
12430 : return false;
12431 : }
12432 108155 : si = gsi_none ();
12433 : }
12434 : else
12435 : {
12436 : /* Emit other stmts after the children vectorized defs which is
12437 : earliest possible. */
12438 : last_stmt = NULL;
12439 926555 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12440 592154 : if (place_only)
12441 : {
12442 197523 : gimple *vstmt = child->si;
12443 197523 : if (!vstmt)
12444 : {
12445 : /* vect_constant_def and defs at region boundary do not
12446 : constrain placement. */
12447 : gcc_assert (SLP_TREE_DEF_TYPE (child) == vect_constant_def
12448 : /* ??? Region boundary is not representated
12449 : by a NULL stmt. */
12450 : || true);
12451 : }
12452 154429 : else if (!last_stmt)
12453 : last_stmt = vstmt;
12454 44653 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
12455 : last_stmt = vstmt;
12456 11023 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
12457 : ;
12458 : else
12459 : /* Non-trapping stmts from different BBs might be combined,
12460 : and if we later CSE a low/high part we can run into this. */
12461 : return false;
12462 : }
12463 394631 : else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
12464 : {
12465 : /* For fold-left reductions we are retaining the scalar
12466 : reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
12467 : set so the representation isn't perfect. Resort to the
12468 : last scalar def here. */
12469 313682 : if (SLP_TREE_VEC_DEFS (child).is_empty ())
12470 : {
12471 1050 : gcc_assert (SLP_TREE_TYPE (child) == cycle_phi_info_type);
12472 1050 : gphi *phi = as_a <gphi *>
12473 1050 : (vect_find_last_scalar_stmt_in_slp (child)->stmt);
12474 1050 : if (!last_stmt)
12475 : last_stmt = phi;
12476 798 : else if (vect_stmt_dominates_stmt_p (last_stmt, phi))
12477 : last_stmt = phi;
12478 787 : else if (vect_stmt_dominates_stmt_p (phi, last_stmt))
12479 : ;
12480 : else
12481 0 : gcc_unreachable ();
12482 : }
12483 : /* We are emitting all vectorized stmts in the same place and
12484 : the last one is the last.
12485 : ??? Unless we have a load permutation applied and that
12486 : figures to re-use an earlier generated load. */
12487 313682 : unsigned j;
12488 313682 : tree vdef;
12489 737053 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
12490 423371 : if (TREE_CODE (vdef) == SSA_NAME
12491 423371 : && !SSA_NAME_IS_DEFAULT_DEF (vdef))
12492 : {
12493 423319 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
12494 : /* Ignore existing external vector defs so we insert
12495 : at region boundary. Those can leak to an
12496 : internal identity permute node. */
12497 423319 : if (is_a <bb_vec_info> (vinfo)
12498 30727 : && vinfo->bbs[0] != gimple_bb (vstmt)
12499 443990 : && dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
12500 20671 : gimple_bb (vstmt)))
12501 : ;
12502 423305 : else if (!last_stmt)
12503 : last_stmt = vstmt;
12504 214115 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
12505 : last_stmt = vstmt;
12506 48888 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
12507 : ;
12508 : else
12509 0 : gcc_unreachable ();
12510 : }
12511 : }
12512 80949 : else if (!SLP_TREE_VECTYPE (child))
12513 : {
12514 : /* For externals we use unvectorized at all scalar defs. */
12515 : unsigned j;
12516 : tree def;
12517 17173 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
12518 : /* If the stmt is not inside the region do not
12519 : use it as possible insertion point. */
12520 9781 : if (auto stmt_info = vinfo->lookup_def (def))
12521 : {
12522 138 : gimple *stmt = stmt_info->stmt;
12523 138 : if (!last_stmt)
12524 : last_stmt = stmt;
12525 118 : else if (vect_stmt_dominates_stmt_p (last_stmt, stmt))
12526 : last_stmt = stmt;
12527 26 : else if (vect_stmt_dominates_stmt_p (stmt, last_stmt))
12528 : ;
12529 : else
12530 0 : gcc_unreachable ();
12531 : }
12532 : }
12533 : else
12534 : {
12535 : /* For externals we have to look at all defs since their
12536 : insertion place is decided per vector. But beware
12537 : of pre-existing vectors where we need to make sure
12538 : we do not insert before the region boundary. */
12539 146971 : if (SLP_TREE_SCALAR_OPS (child).is_empty ()
12540 636 : && !vinfo->lookup_def (SLP_TREE_VEC_DEFS (child)[0]))
12541 : ;
12542 : else
12543 : {
12544 : unsigned j;
12545 : tree vdef;
12546 767372 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
12547 101806 : if (TREE_CODE (vdef) == SSA_NAME
12548 101806 : && !SSA_NAME_IS_DEFAULT_DEF (vdef))
12549 : {
12550 21330 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
12551 21330 : if (!last_stmt)
12552 : last_stmt = vstmt;
12553 11330 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
12554 : last_stmt = vstmt;
12555 9036 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
12556 : ;
12557 : else
12558 0 : gcc_unreachable ();
12559 : }
12560 : }
12561 : }
12562 :
12563 : /* We split regions to vectorize at control altering stmts
12564 : with a definition so this can only be an external. */
12565 334401 : gcc_checking_assert (!last_stmt
12566 : || !is_ctrl_altering_stmt (last_stmt));
12567 :
12568 334401 : if (is_a <bb_vec_info> (vinfo)
12569 133105 : && !SLP_TREE_PERMUTE_P (node)
12570 126552 : && (!last_stmt
12571 123569 : || !dominated_by_p (CDI_DOMINATORS, gimple_bb (last_stmt),
12572 123569 : gimple_bb (stmt_info->stmt)))
12573 360503 : && gimple_could_trap_p (stmt_info->stmt))
12574 : {
12575 : /* We've constrained possibly trapping operations to all come
12576 : from the same basic-block, if vectorized defs would allow earlier
12577 : scheduling still force vectorized stmts to the original block.
12578 : This is only necessary for BB vectorization since for loop vect
12579 : all operations are in a single BB and scalar stmt based
12580 : placement doesn't play well with epilogue vectorization. */
12581 674 : if (last_stmt
12582 1248 : && !dominated_by_p (CDI_DOMINATORS,
12583 574 : gimple_bb (stmt_info->stmt),
12584 574 : gimple_bb (last_stmt)))
12585 : {
12586 4 : gcc_assert (place_only);
12587 : return false;
12588 : }
12589 670 : si = gsi_after_labels (gimple_bb (stmt_info->stmt));
12590 670 : last_stmt = gsi_stmt (si);
12591 : }
12592 : /* When there is no in-region child def to guide placement, insert
12593 : at region boundary. */
12594 333727 : else if (!last_stmt)
12595 : {
12596 5065 : si = gsi_after_labels (vinfo->bbs[0]);
12597 : /* last_stmt NULL marks the region start. */
12598 : }
12599 328662 : else if (is_a <gphi *> (last_stmt))
12600 33098 : si = gsi_after_labels (gimple_bb (last_stmt));
12601 : else
12602 : {
12603 295564 : si = gsi_for_stmt (last_stmt);
12604 : /* We use gsi_insert_before, so when last_stmt is a vector
12605 : def we have to advance (or use gsi_insert_after). */
12606 295564 : gsi_next (&si);
12607 :
12608 295564 : if (auto loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
12609 : {
12610 : /* We'll have to fix this up for loop vect. */
12611 183517 : gcc_assert (!place_only);
12612 : /* Avoid scheduling stmts to random places in the CFG, any
12613 : stmt dominance check we performed is possibly wrong as UIDs
12614 : are not initialized for all of the function for loop
12615 : vectorization. Instead append to the loop preheader. */
12616 183517 : if ((LOOP_VINFO_LOOP (loop_vinfo)->header
12617 183517 : != gimple_bb (last_stmt))
12618 186486 : && dominated_by_p (CDI_DOMINATORS,
12619 : LOOP_VINFO_LOOP (loop_vinfo)->header,
12620 2969 : gimple_bb (last_stmt)))
12621 1176 : si = gsi_end_bb (loop_preheader_edge
12622 588 : (LOOP_VINFO_LOOP (loop_vinfo))->src);
12623 : /* Avoid scheduling internal defs outside of the loop when
12624 : we might have only implicitly tracked loop mask/len defs. */
12625 84 : if (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
12626 183517 : || LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12627 : {
12628 84 : gimple_stmt_iterator si2
12629 84 : = gsi_after_labels (LOOP_VINFO_LOOP (loop_vinfo)->header);
12630 84 : if ((gsi_end_p (si2)
12631 0 : && (LOOP_VINFO_LOOP (loop_vinfo)->header
12632 0 : != gimple_bb (last_stmt))
12633 0 : && dominated_by_p (CDI_DOMINATORS,
12634 : LOOP_VINFO_LOOP (loop_vinfo)->header,
12635 0 : gimple_bb (last_stmt)))
12636 84 : || (!gsi_end_p (si2)
12637 84 : && last_stmt != *si2
12638 79 : && vect_stmt_dominates_stmt_p (last_stmt, *si2)))
12639 3 : si = si2;
12640 : }
12641 : }
12642 : }
12643 : }
12644 :
12645 1937453 : if (place_only)
12646 : {
12647 946026 : if (dump_enabled_p () && last_stmt)
12648 4133 : dump_printf_loc (MSG_NOTE, vect_location,
12649 : "placing node %p at %G:", (void *)node, last_stmt);
12650 : /* Verify we either get a stmt anchor or region start. */
12651 946026 : gcc_assert ((last_stmt && gimple_bb (last_stmt))
12652 : || (!last_stmt && gsi_bb (si)));
12653 946026 : node->si = last_stmt;
12654 946026 : return true;
12655 : }
12656 :
12657 : /* ??? Asserting vect_stmt_dominates_stmt_p (gsi_stmt (si), node->si)
12658 : does not work because in some cases we advance si from last_stmt (as
12659 : we want to insert after vector stmts) and because vector stmts of
12660 : children have been inserted possibly at the same location constraint,
12661 : moving si even further. */
12662 991427 : if (flag_checking && node->si && gimple_bb (node->si) && !gsi_end_p (si))
12663 : {
12664 604476 : auto gsi2 = si;
12665 604476 : while (1)
12666 : {
12667 604476 : if (vect_stmt_dominates_stmt_p (gsi_stmt (gsi2), node->si))
12668 : break;
12669 : /* As we have possibly advanced si it might now point to the
12670 : scalar stmt immediately following node->si. That's OK. */
12671 22726 : if (gsi_stmt (gsi2) != gsi_stmt (si)
12672 22726 : && gimple_uid (gsi_stmt (gsi2)) != 0)
12673 0 : gcc_unreachable ();
12674 22726 : gsi_prev (&gsi2);
12675 22726 : if (gsi_end_p (gsi2))
12676 : {
12677 533 : if (is_a <gphi *> (node->si)
12678 533 : && gimple_bb (node->si) == gsi_bb (si))
12679 : break;
12680 0 : gcc_unreachable ();
12681 : }
12682 : }
12683 : }
12684 :
12685 991427 : if (dump_enabled_p ())
12686 : {
12687 76443 : if (stmt_info)
12688 73005 : dump_printf_loc (MSG_NOTE, vect_location,
12689 : "------>vectorizing SLP node starting from: %G",
12690 : stmt_info->stmt);
12691 : else
12692 : {
12693 3438 : dump_printf_loc (MSG_NOTE, vect_location,
12694 : "------>vectorizing SLP node:\n");
12695 3438 : vect_print_slp_tree (MSG_NOTE, vect_location, node);
12696 : }
12697 : }
12698 991427 : vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
12699 991427 : return true;
12700 : }
12701 :
12702 : /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
12703 : For loop vectorization this is done in vectorizable_call, but for SLP
12704 : it needs to be deferred until end of vect_schedule_slp, because multiple
12705 : SLP instances may refer to the same scalar stmt. */
12706 :
12707 : static void
12708 640090 : vect_remove_slp_scalar_calls (vec_info *vinfo,
12709 : slp_tree node, hash_set<slp_tree> &visited)
12710 : {
12711 640090 : gimple *new_stmt;
12712 640090 : gimple_stmt_iterator gsi;
12713 640090 : tree lhs;
12714 :
12715 640090 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
12716 203140 : return;
12717 :
12718 482182 : if (visited.add (node))
12719 : return;
12720 :
12721 1636383 : for (auto child : SLP_TREE_CHILDREN (node))
12722 544119 : vect_remove_slp_scalar_calls (vinfo, child, visited);
12723 :
12724 1816172 : for (auto stmt_info : SLP_TREE_SCALAR_STMTS (node))
12725 : {
12726 513994 : if (!stmt_info)
12727 3763 : continue;
12728 510231 : stmt_info = vect_orig_stmt (stmt_info);
12729 510231 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
12730 5819 : if (!stmt || gimple_bb (stmt) == NULL)
12731 504454 : continue;
12732 5777 : lhs = gimple_call_lhs (stmt);
12733 5777 : if (lhs)
12734 5159 : new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
12735 : else
12736 618 : new_stmt = gimple_build_nop ();
12737 5777 : unlink_stmt_vdef (stmt_info->stmt);
12738 5777 : gsi = gsi_for_stmt (stmt);
12739 5777 : vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
12740 5777 : if (lhs)
12741 5159 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12742 : }
12743 : }
12744 :
12745 : static void
12746 95971 : vect_remove_slp_scalar_calls (vec_info *vinfo, slp_tree node)
12747 : {
12748 95971 : hash_set<slp_tree> visited;
12749 95971 : vect_remove_slp_scalar_calls (vinfo, node, visited);
12750 95971 : }
12751 :
12752 : /* Vectorize the instance root. */
12753 :
12754 : void
12755 10444 : vectorize_slp_instance_root_stmt (vec_info *vinfo, slp_tree node, slp_instance instance)
12756 : {
12757 10444 : if (instance->kind == slp_inst_kind_ctor)
12758 : {
12759 6424 : tree new_def;
12760 6424 : if (SLP_TREE_VEC_DEFS (node).length () == 1)
12761 : {
12762 6375 : new_def = SLP_TREE_VEC_DEFS (node)[0];
12763 6375 : tree root_lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
12764 6375 : if (!useless_type_conversion_p (TREE_TYPE (root_lhs),
12765 6375 : TREE_TYPE (new_def)))
12766 0 : new_def = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (root_lhs),
12767 : new_def);
12768 : }
12769 : else
12770 : {
12771 49 : gcc_assert (SLP_TREE_VEC_DEFS (node).length () > 1);
12772 49 : tree child_def;
12773 49 : int j;
12774 49 : vec<constructor_elt, va_gc> *v;
12775 49 : vec_alloc (v, SLP_TREE_VEC_DEFS (node).length ());
12776 :
12777 : /* A CTOR can handle V16HI composition from VNx8HI so we
12778 : do not need to convert vector elements if the types
12779 : do not match. */
12780 198 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (node), j, child_def)
12781 100 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, child_def);
12782 49 : tree rtype
12783 49 : = TREE_TYPE (gimple_assign_rhs1 (instance->root_stmts[0]->stmt));
12784 49 : new_def = build_constructor (rtype, v);
12785 : }
12786 :
12787 6424 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
12788 6424 : gimple_assign_set_rhs_from_tree (&rgsi, new_def);
12789 6424 : update_stmt (gsi_stmt (rgsi));
12790 6424 : return;
12791 : }
12792 4020 : else if (instance->kind == slp_inst_kind_bb_reduc)
12793 : {
12794 : /* Largely inspired by reduction chain epilogue handling in
12795 : vect_create_epilog_for_reduction. */
12796 2338 : vec<tree> vec_defs = vNULL;
12797 2338 : vect_get_slp_defs (node, &vec_defs);
12798 2338 : enum tree_code reduc_code
12799 2338 : = gimple_assign_rhs_code (instance->root_stmts[0]->stmt);
12800 : /* ??? We actually have to reflect signs somewhere. */
12801 2338 : if (reduc_code == MINUS_EXPR)
12802 0 : reduc_code = PLUS_EXPR;
12803 2338 : gimple_seq epilogue = NULL;
12804 : /* We may end up with more than one vector result, reduce them
12805 : to one vector. */
12806 2338 : tree vec_def = vec_defs[0];
12807 2338 : tree vectype = TREE_TYPE (vec_def);
12808 2338 : tree compute_vectype = vectype;
12809 2338 : bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
12810 1151 : && TYPE_OVERFLOW_UNDEFINED (vectype)
12811 3084 : && operation_can_overflow (reduc_code));
12812 539 : if (pun_for_overflow_p)
12813 : {
12814 539 : compute_vectype = unsigned_type_for (vectype);
12815 539 : vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
12816 : compute_vectype, vec_def);
12817 : }
12818 2629 : for (unsigned i = 1; i < vec_defs.length (); ++i)
12819 : {
12820 291 : tree def = vec_defs[i];
12821 291 : if (pun_for_overflow_p)
12822 45 : def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
12823 : compute_vectype, def);
12824 291 : vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
12825 : vec_def, def);
12826 : }
12827 2338 : vec_defs.release ();
12828 : /* ??? Support other schemes than direct internal fn or two
12829 : element vectors. */
12830 2338 : tree scalar_def;
12831 2338 : internal_fn reduc_fn;
12832 3342 : if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
12833 2338 : || reduc_fn == IFN_LAST
12834 4676 : || !direct_internal_fn_supported_p (reduc_fn, compute_vectype,
12835 : OPTIMIZE_FOR_BOTH))
12836 : {
12837 1004 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (compute_vectype), 2u));
12838 1004 : tree tem0 = gimple_build (&epilogue, BIT_FIELD_REF,
12839 1004 : TREE_TYPE (compute_vectype), vec_def,
12840 1004 : TYPE_SIZE (TREE_TYPE (compute_vectype)),
12841 1004 : bitsize_zero_node);
12842 1004 : tree tem1 = gimple_build (&epilogue, BIT_FIELD_REF,
12843 1004 : TREE_TYPE (compute_vectype), vec_def,
12844 1004 : TYPE_SIZE (TREE_TYPE (compute_vectype)),
12845 1004 : TYPE_SIZE (TREE_TYPE (compute_vectype)));
12846 1004 : scalar_def = gimple_build (&epilogue, reduc_code,
12847 1004 : TREE_TYPE (compute_vectype), tem0, tem1);
12848 : }
12849 : else
12850 1334 : scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
12851 1334 : TREE_TYPE (compute_vectype), vec_def);
12852 2338 : if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
12853 : {
12854 938 : tree rem_def = NULL_TREE;
12855 2166 : for (auto def : SLP_INSTANCE_REMAIN_DEFS (instance))
12856 : {
12857 1228 : def = gimple_convert (&epilogue, TREE_TYPE (scalar_def), def);
12858 1228 : if (!rem_def)
12859 : rem_def = def;
12860 : else
12861 290 : rem_def = gimple_build (&epilogue, reduc_code,
12862 290 : TREE_TYPE (scalar_def),
12863 : rem_def, def);
12864 : }
12865 938 : scalar_def = gimple_build (&epilogue, reduc_code,
12866 938 : TREE_TYPE (scalar_def),
12867 : scalar_def, rem_def);
12868 : }
12869 2338 : scalar_def = gimple_convert (&epilogue,
12870 2338 : TREE_TYPE (vectype), scalar_def);
12871 2338 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
12872 2338 : gsi_insert_seq_before (&rgsi, epilogue, GSI_SAME_STMT);
12873 2338 : gimple_assign_set_rhs_from_tree (&rgsi, scalar_def);
12874 2338 : update_stmt (gsi_stmt (rgsi));
12875 2338 : return;
12876 : }
12877 1682 : else if (instance->kind == slp_inst_kind_gcond)
12878 : {
12879 : /* Only support a single root for now as we can't codegen CFG yet and so we
12880 : can't support lane > 1 at this time. */
12881 1682 : gcc_assert (instance->root_stmts.length () == 1);
12882 1682 : auto root_stmt_info = instance->root_stmts[0];
12883 1682 : auto last_stmt = STMT_VINFO_STMT (vect_orig_stmt (root_stmt_info));
12884 1682 : gimple_stmt_iterator rgsi = gsi_for_stmt (last_stmt);
12885 1682 : gcc_assert (!SLP_TREE_VEC_DEFS (node).is_empty ());
12886 1682 : bool res = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
12887 : root_stmt_info, &rgsi, node, NULL);
12888 1682 : gcc_assert (res);
12889 1682 : return;
12890 : }
12891 : else
12892 0 : gcc_unreachable ();
12893 : }
12894 :
12895 : struct slp_scc_info
12896 : {
12897 : bool on_stack;
12898 : bool res;
12899 : int dfs;
12900 : int lowlink;
12901 : };
12902 :
12903 : /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs.
12904 : When PLACE_ONLY, return false if there is no possible schedule. */
12905 :
12906 : static bool
12907 3148218 : vect_schedule_scc (vec_info *vinfo, slp_tree node, slp_instance instance,
12908 : hash_map<slp_tree, slp_scc_info> &scc_info,
12909 : int &maxdfs, vec<slp_tree> &stack, bool place_only)
12910 : {
12911 3148218 : bool existed_p;
12912 3148218 : slp_scc_info *info = &scc_info.get_or_insert (node, &existed_p);
12913 3148218 : gcc_assert (!existed_p);
12914 3148218 : info->dfs = maxdfs;
12915 3148218 : info->lowlink = maxdfs;
12916 3148218 : maxdfs++;
12917 :
12918 : /* Leaf. */
12919 3148218 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
12920 : {
12921 1210753 : info->on_stack = false;
12922 1210753 : bool res = vect_schedule_slp_node (vinfo, node, instance, place_only);
12923 1210753 : gcc_assert (res);
12924 1210753 : info->res = res;
12925 1210753 : return res;
12926 : }
12927 :
12928 1937465 : info->on_stack = true;
12929 1937465 : info->res = true;
12930 1937465 : stack.safe_push (node);
12931 :
12932 1937465 : bool res = true;
12933 1937465 : unsigned i;
12934 1937465 : slp_tree child;
12935 : /* DFS recurse. */
12936 7585483 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12937 : {
12938 2054597 : if (!child)
12939 59718 : continue;
12940 1994879 : slp_scc_info *child_info = scc_info.get (child);
12941 1994879 : if (!child_info)
12942 : {
12943 1845455 : res &= vect_schedule_scc (vinfo, child, instance, scc_info,
12944 : maxdfs, stack, place_only);
12945 : /* Recursion might have re-allocated the node. */
12946 1845455 : info = scc_info.get (node);
12947 1845455 : child_info = scc_info.get (child);
12948 1845455 : info->lowlink = MIN (info->lowlink, child_info->lowlink);
12949 : }
12950 149424 : else if (child_info->on_stack)
12951 35766 : info->lowlink = MIN (info->lowlink, child_info->dfs);
12952 : else
12953 113658 : res &= child_info->res;
12954 : }
12955 1937465 : if (info->lowlink != info->dfs)
12956 : return res;
12957 :
12958 1897850 : auto_vec<slp_tree, 4> phis_to_fixup;
12959 :
12960 : /* Singleton. */
12961 1897850 : if (stack.last () == node)
12962 : {
12963 1868722 : stack.pop ();
12964 1868722 : info->on_stack = false;
12965 1868722 : res &= vect_schedule_slp_node (vinfo, node, instance, place_only);
12966 1868722 : info->res = res;
12967 1868722 : if (!SLP_TREE_PERMUTE_P (node)
12968 1868722 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (node)->stmt))
12969 73134 : phis_to_fixup.quick_push (node);
12970 : }
12971 : else
12972 : {
12973 : /* SCC. */
12974 29128 : int last_idx = stack.length () - 1;
12975 68743 : while (stack[last_idx] != node)
12976 39615 : last_idx--;
12977 : /* We can break the cycle at PHIs who have at least one child
12978 : code generated. Then we could re-start the DFS walk until
12979 : all nodes in the SCC are covered (we might have new entries
12980 : for only back-reachable nodes). But it's simpler to just
12981 : iterate and schedule those that are ready. */
12982 29128 : unsigned todo = stack.length () - last_idx;
12983 29128 : auto_vec<slp_tree, 4> saved_scc (todo);
12984 29128 : saved_scc.quick_grow (todo);
12985 87384 : memcpy (saved_scc.address (), &stack.address ()[last_idx],
12986 : sizeof (slp_tree) * todo);
12987 29932 : do
12988 : {
12989 132791 : for (int idx = stack.length () - 1; idx >= last_idx; --idx)
12990 : {
12991 72927 : slp_tree entry = stack[idx];
12992 72927 : if (!entry)
12993 2541 : continue;
12994 70386 : bool phi = (!SLP_TREE_PERMUTE_P (entry)
12995 70386 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (entry)->stmt));
12996 70386 : bool ready = !phi;
12997 214173 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry), i, child)
12998 133628 : if (!child)
12999 : {
13000 23200 : gcc_assert (phi);
13001 : ready = true;
13002 : break;
13003 : }
13004 110428 : else if (scc_info.get (child)->on_stack)
13005 : {
13006 30967 : if (!phi)
13007 : {
13008 : ready = false;
13009 : break;
13010 : }
13011 : }
13012 : else
13013 : {
13014 79461 : if (phi)
13015 : {
13016 : ready = true;
13017 : break;
13018 : }
13019 : }
13020 47186 : if (ready)
13021 : {
13022 68743 : res &= vect_schedule_slp_node (vinfo, entry, instance,
13023 : place_only);
13024 68743 : scc_info.get (entry)->on_stack = false;
13025 68743 : stack[idx] = NULL;
13026 68743 : todo--;
13027 68743 : if (phi)
13028 35560 : phis_to_fixup.safe_push (entry);
13029 : }
13030 : }
13031 : }
13032 29932 : while (todo != 0);
13033 :
13034 : /* Push the scheduling result to all of the SCC. */
13035 156127 : for (slp_tree entry : saved_scc)
13036 68743 : scc_info.get (entry)->res = res;
13037 :
13038 : /* Pop the SCC. */
13039 29128 : stack.truncate (last_idx);
13040 29128 : }
13041 :
13042 1897850 : if (place_only)
13043 : return res;
13044 :
13045 : /* Now fixup the backedge def of the vectorized PHIs in this SCC. */
13046 : slp_tree phi_node;
13047 1014769 : FOR_EACH_VEC_ELT (phis_to_fixup, i, phi_node)
13048 : {
13049 55333 : gphi *phi = as_a <gphi *> (SLP_TREE_REPRESENTATIVE (phi_node)->stmt);
13050 55333 : edge_iterator ei;
13051 55333 : edge e;
13052 172164 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
13053 : {
13054 116831 : unsigned dest_idx = e->dest_idx;
13055 116831 : child = SLP_TREE_CHILDREN (phi_node)[dest_idx];
13056 116831 : if (!child || SLP_TREE_DEF_TYPE (child) != vect_internal_def)
13057 69448 : continue;
13058 47383 : unsigned n = SLP_TREE_VEC_DEFS (phi_node).length ();
13059 : /* Simply fill all args. */
13060 47383 : if (STMT_VINFO_DEF_TYPE (SLP_TREE_REPRESENTATIVE (phi_node))
13061 : != vect_first_order_recurrence)
13062 99862 : for (unsigned i = 0; i < n; ++i)
13063 : {
13064 52522 : tree phidef = SLP_TREE_VEC_DEFS (phi_node)[i];
13065 52522 : gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (phidef));
13066 52522 : add_phi_arg (phi, vect_get_slp_vect_def (child, i),
13067 : e, gimple_phi_arg_location (phi, dest_idx));
13068 : }
13069 : else
13070 : {
13071 : /* Unless it is a first order recurrence which needs
13072 : args filled in for both the PHI node and the permutes. */
13073 43 : gimple *perm
13074 43 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[0]);
13075 43 : gimple *rphi = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (perm));
13076 43 : add_phi_arg (as_a <gphi *> (rphi),
13077 : vect_get_slp_vect_def (child, n - 1),
13078 : e, gimple_phi_arg_location (phi, dest_idx));
13079 166 : for (unsigned i = 0; i < n; ++i)
13080 : {
13081 80 : gimple *perm
13082 80 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[i]);
13083 80 : if (i > 0)
13084 37 : gimple_assign_set_rhs1 (perm,
13085 : vect_get_slp_vect_def (child, i - 1));
13086 80 : gimple_assign_set_rhs2 (perm,
13087 : vect_get_slp_vect_def (child, i));
13088 80 : update_stmt (perm);
13089 : }
13090 : }
13091 : }
13092 : }
13093 :
13094 959436 : gcc_assert (res);
13095 : return true;
13096 1897850 : }
13097 :
13098 : /* Generate vector code for SLP_INSTANCES in the loop/basic block. Perform
13099 : vector stmt placement only when PLACE_ONLY is true, removing SLP graph
13100 : entries that cannot be scheduled. If placing, return false if a schedule
13101 : cannot be computed for any entry. */
13102 :
13103 : bool
13104 797509 : vect_schedule_slp (vec_info *vinfo, vec<slp_instance> &slp_instances,
13105 : bool place_only)
13106 : {
13107 797509 : slp_instance instance;
13108 797509 : unsigned int i;
13109 :
13110 797509 : hash_map<slp_tree, slp_scc_info> scc_info;
13111 797509 : int maxdfs = 0;
13112 2104169 : for (i = 0; slp_instances.iterate (i, &instance); )
13113 : {
13114 1306660 : slp_tree node = SLP_INSTANCE_TREE (instance);
13115 1306660 : if (!place_only && dump_enabled_p ())
13116 : {
13117 17130 : dump_printf_loc (MSG_NOTE, vect_location,
13118 : "Vectorizing SLP tree:\n");
13119 : /* ??? Dump all? */
13120 17130 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
13121 625 : dump_printf_loc (MSG_NOTE, vect_location, "Root stmt: %G",
13122 625 : SLP_INSTANCE_ROOT_STMTS (instance)[0]->stmt);
13123 17130 : vect_print_slp_graph (MSG_NOTE, vect_location,
13124 : SLP_INSTANCE_TREE (instance));
13125 : }
13126 : /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
13127 : have a PHI be the node breaking the cycle. */
13128 1306660 : bool res;
13129 1306660 : if (slp_scc_info *info = scc_info.get (node))
13130 3897 : res = info->res;
13131 : else
13132 : {
13133 1302763 : auto_vec<slp_tree> stack;
13134 1302763 : res = vect_schedule_scc (vinfo, node, instance, scc_info,
13135 : maxdfs, stack, place_only);
13136 1302763 : }
13137 :
13138 1306660 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
13139 : {
13140 66818 : if (place_only)
13141 : {
13142 56374 : gimple *root_stmt = instance->root_stmts[0]->stmt;
13143 56374 : res &= (!node->si
13144 : /* As we instert after node->si it may not be the
13145 : root_stmt itself. */
13146 56374 : || (node->si != root_stmt
13147 55427 : && vect_stmt_dominates_stmt_p (node->si, root_stmt)));
13148 : }
13149 : else
13150 10444 : vectorize_slp_instance_root_stmt (vinfo, node, instance);
13151 : }
13152 :
13153 1306660 : if (!place_only && dump_enabled_p ())
13154 17130 : dump_printf_loc (MSG_NOTE, vect_location,
13155 : "vectorizing stmts using SLP.\n");
13156 :
13157 1306660 : if (!res)
13158 : {
13159 38 : gcc_assert (place_only);
13160 38 : if (dump_enabled_p ())
13161 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13162 : "not vectorized: cannot schedule SLP graph "
13163 : "entry %p\n", (void *)instance);
13164 38 : vect_free_slp_instance (instance);
13165 38 : slp_instances.ordered_remove (i);
13166 38 : continue;
13167 : }
13168 1306622 : ++i;
13169 : }
13170 :
13171 797509 : if (place_only)
13172 493574 : return !slp_instances.is_empty ();
13173 :
13174 1939526 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
13175 : {
13176 591295 : slp_tree root = SLP_INSTANCE_TREE (instance);
13177 591295 : stmt_vec_info store_info;
13178 591295 : unsigned int j;
13179 :
13180 : /* Remove scalar call stmts. Do not do this for basic-block
13181 : vectorization as not all uses may be vectorized.
13182 : ??? Why should this be necessary? DCE should be able to
13183 : remove the stmts itself.
13184 : ??? For BB vectorization we can as well remove scalar
13185 : stmts starting from the SLP tree root if they have no
13186 : uses. */
13187 591295 : if (is_a <loop_vec_info> (vinfo))
13188 95971 : vect_remove_slp_scalar_calls (vinfo, root);
13189 :
13190 : /* Remove vectorized stores original scalar stmts. */
13191 2640295 : for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info); j++)
13192 : {
13193 1494128 : if (!store_info
13194 1494111 : || !STMT_VINFO_DATA_REF (store_info)
13195 1464462 : || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info)))
13196 : break;
13197 :
13198 1457705 : store_info = vect_orig_stmt (store_info);
13199 : /* Free the attached stmt_vec_info and remove the stmt. */
13200 1457705 : vinfo->remove_stmt (store_info);
13201 :
13202 : /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
13203 : to not crash in vect_free_slp_tree later. */
13204 1457705 : if (SLP_TREE_REPRESENTATIVE (root) == store_info)
13205 554519 : SLP_TREE_REPRESENTATIVE (root) = NULL;
13206 : }
13207 : }
13208 :
13209 : return true;
13210 797509 : }
|