Branch data Line data Source code
1 : : /* SLP - Basic Block Vectorization
2 : : Copyright (C) 2007-2025 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 : : 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 : : static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
71 : : static bool vect_slp_can_convert_to_external (const vec<stmt_vec_info> &);
72 : :
73 : : static object_allocator<_slp_tree> *slp_tree_pool;
74 : : static slp_tree slp_first_node;
75 : :
76 : : void
77 : 1113948 : vect_slp_init (void)
78 : : {
79 : 1113948 : slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
80 : 1113948 : }
81 : :
82 : : void
83 : 1113948 : vect_slp_fini (void)
84 : : {
85 : 1683125 : while (slp_first_node)
86 : 569177 : delete slp_first_node;
87 : 2227896 : delete slp_tree_pool;
88 : 1113948 : slp_tree_pool = NULL;
89 : 1113948 : }
90 : :
91 : : void *
92 : 7828486 : _slp_tree::operator new (size_t n)
93 : : {
94 : 7828486 : gcc_assert (n == sizeof (_slp_tree));
95 : 7828486 : return slp_tree_pool->allocate_raw ();
96 : : }
97 : :
98 : : void
99 : 7828486 : _slp_tree::operator delete (void *node, size_t n)
100 : : {
101 : 7828486 : gcc_assert (n == sizeof (_slp_tree));
102 : 7828486 : slp_tree_pool->remove_raw (node);
103 : 7828486 : }
104 : :
105 : :
106 : : /* Initialize a SLP node. */
107 : :
108 : 7828486 : _slp_tree::_slp_tree ()
109 : : {
110 : 7828486 : this->prev_node = NULL;
111 : 7828486 : if (slp_first_node)
112 : 6927199 : slp_first_node->prev_node = this;
113 : 7828486 : this->next_node = slp_first_node;
114 : 7828486 : slp_first_node = this;
115 : 7828486 : SLP_TREE_SCALAR_STMTS (this) = vNULL;
116 : 7828486 : SLP_TREE_SCALAR_OPS (this) = vNULL;
117 : 7828486 : SLP_TREE_VEC_DEFS (this) = vNULL;
118 : 7828486 : SLP_TREE_CHILDREN (this) = vNULL;
119 : 7828486 : SLP_TREE_LOAD_PERMUTATION (this) = vNULL;
120 : 7828486 : SLP_TREE_LANE_PERMUTATION (this) = vNULL;
121 : 7828486 : SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def;
122 : 7828486 : SLP_TREE_CODE (this) = ERROR_MARK;
123 : 7828486 : SLP_TREE_GS_SCALE (this) = 0;
124 : 7828486 : SLP_TREE_GS_BASE (this) = NULL_TREE;
125 : 7828486 : this->ldst_lanes = false;
126 : 7828486 : this->avoid_stlf_fail = false;
127 : 7828486 : SLP_TREE_VECTYPE (this) = NULL_TREE;
128 : 7828486 : SLP_TREE_REPRESENTATIVE (this) = NULL;
129 : 7828486 : this->cycle_info.id = -1;
130 : 7828486 : this->cycle_info.reduc_idx = -1;
131 : 7828486 : SLP_TREE_REF_COUNT (this) = 1;
132 : 7828486 : this->failed = NULL;
133 : 7828486 : this->max_nunits = 1;
134 : 7828486 : this->lanes = 0;
135 : 7828486 : SLP_TREE_TYPE (this) = undef_vec_info_type;
136 : 7828486 : this->data = NULL;
137 : 7828486 : }
138 : :
139 : : /* Tear down a SLP node. */
140 : :
141 : 7828486 : _slp_tree::~_slp_tree ()
142 : : {
143 : 7828486 : if (this->prev_node)
144 : 5077219 : this->prev_node->next_node = this->next_node;
145 : : else
146 : 2751267 : slp_first_node = this->next_node;
147 : 7828486 : if (this->next_node)
148 : 5869525 : this->next_node->prev_node = this->prev_node;
149 : 7828486 : SLP_TREE_CHILDREN (this).release ();
150 : 7828486 : SLP_TREE_SCALAR_STMTS (this).release ();
151 : 7828486 : SLP_TREE_SCALAR_OPS (this).release ();
152 : 7828486 : SLP_TREE_VEC_DEFS (this).release ();
153 : 7828486 : SLP_TREE_LOAD_PERMUTATION (this).release ();
154 : 7828486 : SLP_TREE_LANE_PERMUTATION (this).release ();
155 : 7828486 : if (this->failed)
156 : 1917986 : free (failed);
157 : 7828486 : if (this->data)
158 : 1147573 : delete this->data;
159 : 7828486 : }
160 : :
161 : : /* Push the single SSA definition in DEF to the vector of vector defs. */
162 : :
163 : : void
164 : 519257 : _slp_tree::push_vec_def (gimple *def)
165 : : {
166 : 519257 : if (gphi *phi = dyn_cast <gphi *> (def))
167 : 61761 : vec_defs.quick_push (gimple_phi_result (phi));
168 : : else
169 : : {
170 : 457496 : def_operand_p defop = single_ssa_def_operand (def, SSA_OP_ALL_DEFS);
171 : 457496 : vec_defs.quick_push (get_def_from_ptr (defop));
172 : : }
173 : 519257 : }
174 : :
175 : : /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
176 : :
177 : : void
178 : 14984120 : vect_free_slp_tree (slp_tree node)
179 : : {
180 : 14984120 : int i;
181 : 14984120 : slp_tree child;
182 : :
183 : 14984120 : if (--SLP_TREE_REF_COUNT (node) != 0)
184 : 14984120 : return;
185 : :
186 : 11768405 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
187 : 4509096 : if (child)
188 : 3752753 : vect_free_slp_tree (child);
189 : :
190 : : /* If the node defines any SLP only patterns then those patterns are no
191 : : longer valid and should be removed. */
192 : 7259309 : stmt_vec_info rep_stmt_info = SLP_TREE_REPRESENTATIVE (node);
193 : 7259309 : if (rep_stmt_info && STMT_VINFO_SLP_VECT_ONLY_PATTERN (rep_stmt_info))
194 : : {
195 : 982 : stmt_vec_info stmt_info = vect_orig_stmt (rep_stmt_info);
196 : 982 : STMT_VINFO_IN_PATTERN_P (stmt_info) = false;
197 : 982 : STMT_SLP_TYPE (stmt_info) = STMT_SLP_TYPE (rep_stmt_info);
198 : : }
199 : :
200 : 7259309 : delete node;
201 : : }
202 : :
203 : : /* Return a location suitable for dumpings related to the SLP instance. */
204 : :
205 : : dump_user_location_t
206 : 3400936 : _slp_instance::location () const
207 : : {
208 : 3400936 : if (!root_stmts.is_empty ())
209 : 317514 : return root_stmts[0]->stmt;
210 : : else
211 : 3083422 : return SLP_TREE_SCALAR_STMTS (root)[0]->stmt;
212 : : }
213 : :
214 : :
215 : : /* Free the memory allocated for the SLP instance. */
216 : :
217 : : void
218 : 1751794 : vect_free_slp_instance (slp_instance instance)
219 : : {
220 : 1751794 : vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
221 : 1751794 : SLP_INSTANCE_LOADS (instance).release ();
222 : 1751794 : SLP_INSTANCE_ROOT_STMTS (instance).release ();
223 : 1751794 : SLP_INSTANCE_REMAIN_DEFS (instance).release ();
224 : 1751794 : instance->subgraph_entries.release ();
225 : 1751794 : instance->cost_vec.release ();
226 : 1751794 : free (instance);
227 : 1751794 : }
228 : :
229 : :
230 : : /* Create an SLP node for SCALAR_STMTS. */
231 : :
232 : : slp_tree
233 : 107626 : vect_create_new_slp_node (unsigned nops, tree_code code)
234 : : {
235 : 107626 : slp_tree node = new _slp_tree;
236 : 107626 : SLP_TREE_SCALAR_STMTS (node) = vNULL;
237 : 107626 : SLP_TREE_CHILDREN (node).create (nops);
238 : 107626 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
239 : 107626 : SLP_TREE_CODE (node) = code;
240 : 107626 : return node;
241 : : }
242 : : /* Create an SLP node for SCALAR_STMTS. */
243 : :
244 : : static slp_tree
245 : 3787972 : vect_create_new_slp_node (slp_tree node,
246 : : vec<stmt_vec_info> scalar_stmts, unsigned nops)
247 : : {
248 : 3787972 : SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
249 : 3787972 : SLP_TREE_CHILDREN (node).create (nops);
250 : 3787972 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
251 : 3787972 : SLP_TREE_REPRESENTATIVE (node) = scalar_stmts[0];
252 : 3787972 : SLP_TREE_LANES (node) = scalar_stmts.length ();
253 : 3787972 : return node;
254 : : }
255 : :
256 : : /* Create an SLP node for SCALAR_STMTS. */
257 : :
258 : : static slp_tree
259 : 6481 : vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts, unsigned nops)
260 : : {
261 : 6481 : return vect_create_new_slp_node (new _slp_tree, scalar_stmts, nops);
262 : : }
263 : :
264 : : /* Create an SLP node for OPS. */
265 : :
266 : : static slp_tree
267 : 2006908 : vect_create_new_slp_node (slp_tree node, vec<tree> ops)
268 : : {
269 : 2006908 : SLP_TREE_SCALAR_OPS (node) = ops;
270 : 2006908 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
271 : 0 : SLP_TREE_LANES (node) = ops.length ();
272 : 2006908 : return node;
273 : : }
274 : :
275 : : /* Create an SLP node for OPS. */
276 : :
277 : : static slp_tree
278 : 2006908 : vect_create_new_slp_node (vec<tree> ops)
279 : : {
280 : 2006908 : return vect_create_new_slp_node (new _slp_tree, ops);
281 : : }
282 : :
283 : :
284 : : /* This structure is used in creation of an SLP tree. Each instance
285 : : corresponds to the same operand in a group of scalar stmts in an SLP
286 : : node. */
287 : : typedef struct _slp_oprnd_info
288 : : {
289 : : /* Def-stmts for the operands. */
290 : : vec<stmt_vec_info> def_stmts;
291 : : /* Operands. */
292 : : vec<tree> ops;
293 : : /* Information about the first statement, its vector def-type, type, the
294 : : operand itself in case it's constant, and an indication if it's a pattern
295 : : stmt and gather/scatter info. */
296 : : tree first_op_type;
297 : : enum vect_def_type first_dt;
298 : : bool any_pattern;
299 : : bool first_gs_p;
300 : : gather_scatter_info first_gs_info;
301 : : } *slp_oprnd_info;
302 : :
303 : :
304 : : /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
305 : : operand. */
306 : : static vec<slp_oprnd_info>
307 : 3422856 : vect_create_oprnd_info (int nops, int group_size)
308 : : {
309 : 3422856 : int i;
310 : 3422856 : slp_oprnd_info oprnd_info;
311 : 3422856 : vec<slp_oprnd_info> oprnds_info;
312 : :
313 : 3422856 : oprnds_info.create (nops);
314 : 12393293 : for (i = 0; i < nops; i++)
315 : : {
316 : 5547581 : oprnd_info = XNEW (struct _slp_oprnd_info);
317 : 5547581 : oprnd_info->def_stmts.create (group_size);
318 : 5547581 : oprnd_info->ops.create (group_size);
319 : 5547581 : oprnd_info->first_dt = vect_uninitialized_def;
320 : 5547581 : oprnd_info->first_op_type = NULL_TREE;
321 : 5547581 : oprnd_info->any_pattern = false;
322 : 5547581 : oprnd_info->first_gs_p = false;
323 : 5547581 : oprnds_info.quick_push (oprnd_info);
324 : : }
325 : :
326 : 3422856 : return oprnds_info;
327 : : }
328 : :
329 : :
330 : : /* Free operands info. */
331 : :
332 : : static void
333 : 3422856 : vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
334 : : {
335 : 3422856 : int i;
336 : 3422856 : slp_oprnd_info oprnd_info;
337 : :
338 : 8970437 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
339 : : {
340 : 5547581 : oprnd_info->def_stmts.release ();
341 : 5547581 : oprnd_info->ops.release ();
342 : 5547581 : XDELETE (oprnd_info);
343 : : }
344 : :
345 : 3422856 : oprnds_info.release ();
346 : 3422856 : }
347 : :
348 : : /* Return the execution frequency of NODE (so that a higher value indicates
349 : : a "more important" node when optimizing for speed). */
350 : :
351 : : static sreal
352 : 3575061 : vect_slp_node_weight (slp_tree node)
353 : : {
354 : 3575061 : stmt_vec_info stmt_info = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (node));
355 : 3575061 : basic_block bb = gimple_bb (stmt_info->stmt);
356 : 3575061 : return bb->count.to_sreal_scale (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
357 : : }
358 : :
359 : : /* Return true if STMTS contains a pattern statement. */
360 : :
361 : : static bool
362 : 22657 : vect_contains_pattern_stmt_p (vec<stmt_vec_info> stmts)
363 : : {
364 : 22657 : stmt_vec_info stmt_info;
365 : 22657 : unsigned int i;
366 : 73813 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
367 : 53513 : if (stmt_info && is_pattern_stmt_p (stmt_info))
368 : : return true;
369 : : return false;
370 : : }
371 : :
372 : : /* Return true when all lanes in the external or constant NODE have
373 : : the same value. */
374 : :
375 : : static bool
376 : 600120 : vect_slp_tree_uniform_p (slp_tree node)
377 : : {
378 : 600120 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_constant_def
379 : : || SLP_TREE_DEF_TYPE (node) == vect_external_def);
380 : :
381 : : /* Pre-exsting vectors. */
382 : 1060446 : if (SLP_TREE_SCALAR_OPS (node).is_empty ())
383 : : return false;
384 : :
385 : : unsigned i;
386 : : tree op, first = NULL_TREE;
387 : 1368224 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
388 : 1228430 : if (!first)
389 : : first = op;
390 : 628310 : else if (!operand_equal_p (first, op, 0))
391 : : return false;
392 : :
393 : : return true;
394 : : }
395 : :
396 : : /* Find the place of the data-ref in STMT_INFO in the interleaving chain
397 : : that starts from FIRST_STMT_INFO. Return -1 if the data-ref is not a part
398 : : of the chain. */
399 : :
400 : : int
401 : 669766 : vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
402 : : stmt_vec_info first_stmt_info)
403 : : {
404 : 669766 : stmt_vec_info next_stmt_info = first_stmt_info;
405 : 669766 : int result = 0;
406 : :
407 : 669766 : if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
408 : : return -1;
409 : :
410 : 1710736 : do
411 : : {
412 : 1710736 : if (next_stmt_info == stmt_info)
413 : : return result;
414 : 1040970 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
415 : 1040970 : if (next_stmt_info)
416 : 1040970 : result += DR_GROUP_GAP (next_stmt_info);
417 : : }
418 : 1040970 : while (next_stmt_info);
419 : :
420 : : return -1;
421 : : }
422 : :
423 : : /* Check whether it is possible to load COUNT elements of type ELT_TYPE
424 : : using the method implemented by duplicate_and_interleave. Return true
425 : : if so, returning the number of intermediate vectors in *NVECTORS_OUT
426 : : (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
427 : : (if nonnull). */
428 : :
429 : : bool
430 : 0 : can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
431 : : tree elt_type, unsigned int *nvectors_out,
432 : : tree *vector_type_out,
433 : : tree *permutes)
434 : : {
435 : 0 : tree base_vector_type = get_vectype_for_scalar_type (vinfo, elt_type, count);
436 : 0 : if (!base_vector_type || !VECTOR_MODE_P (TYPE_MODE (base_vector_type)))
437 : 0 : return false;
438 : :
439 : 0 : machine_mode base_vector_mode = TYPE_MODE (base_vector_type);
440 : 0 : poly_int64 elt_bytes = count * GET_MODE_UNIT_SIZE (base_vector_mode);
441 : 0 : unsigned int nvectors = 1;
442 : 0 : for (;;)
443 : : {
444 : 0 : scalar_int_mode int_mode;
445 : 0 : poly_int64 elt_bits = elt_bytes * BITS_PER_UNIT;
446 : 0 : if (int_mode_for_size (elt_bits, 1).exists (&int_mode))
447 : : {
448 : : /* Get the natural vector type for this SLP group size. */
449 : 0 : tree int_type = build_nonstandard_integer_type
450 : 0 : (GET_MODE_BITSIZE (int_mode), 1);
451 : 0 : tree vector_type
452 : 0 : = get_vectype_for_scalar_type (vinfo, int_type, count);
453 : 0 : poly_int64 half_nelts;
454 : 0 : if (vector_type
455 : 0 : && VECTOR_MODE_P (TYPE_MODE (vector_type))
456 : 0 : && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
457 : : GET_MODE_SIZE (base_vector_mode))
458 : 0 : && multiple_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)),
459 : : 2, &half_nelts))
460 : : {
461 : : /* Try fusing consecutive sequences of COUNT / NVECTORS elements
462 : : together into elements of type INT_TYPE and using the result
463 : : to build NVECTORS vectors. */
464 : 0 : poly_uint64 nelts = GET_MODE_NUNITS (TYPE_MODE (vector_type));
465 : 0 : vec_perm_builder sel1 (nelts, 2, 3);
466 : 0 : vec_perm_builder sel2 (nelts, 2, 3);
467 : :
468 : 0 : for (unsigned int i = 0; i < 3; ++i)
469 : : {
470 : 0 : sel1.quick_push (i);
471 : 0 : sel1.quick_push (i + nelts);
472 : 0 : sel2.quick_push (half_nelts + i);
473 : 0 : sel2.quick_push (half_nelts + i + nelts);
474 : : }
475 : 0 : vec_perm_indices indices1 (sel1, 2, nelts);
476 : 0 : vec_perm_indices indices2 (sel2, 2, nelts);
477 : 0 : machine_mode vmode = TYPE_MODE (vector_type);
478 : 0 : if (can_vec_perm_const_p (vmode, vmode, indices1)
479 : 0 : && can_vec_perm_const_p (vmode, vmode, indices2))
480 : : {
481 : 0 : if (nvectors_out)
482 : 0 : *nvectors_out = nvectors;
483 : 0 : if (vector_type_out)
484 : 0 : *vector_type_out = vector_type;
485 : 0 : if (permutes)
486 : : {
487 : 0 : permutes[0] = vect_gen_perm_mask_checked (vector_type,
488 : : indices1);
489 : 0 : permutes[1] = vect_gen_perm_mask_checked (vector_type,
490 : : indices2);
491 : : }
492 : 0 : return true;
493 : : }
494 : 0 : }
495 : : }
496 : 0 : if (!multiple_p (elt_bytes, 2, &elt_bytes))
497 : : return false;
498 : 0 : nvectors *= 2;
499 : : /* We need to be able to fuse COUNT / NVECTORS elements together. */
500 : 0 : if (!multiple_p (count, nvectors))
501 : : return false;
502 : : }
503 : : }
504 : :
505 : : /* Return true if DTA and DTB match. */
506 : :
507 : : static bool
508 : 16628008 : vect_def_types_match (enum vect_def_type dta, enum vect_def_type dtb)
509 : : {
510 : 16628008 : return (dta == dtb
511 : 328789 : || ((dta == vect_external_def || dta == vect_constant_def)
512 : 205410 : && (dtb == vect_external_def || dtb == vect_constant_def)));
513 : : }
514 : :
515 : : #define GATHER_SCATTER_OFFSET (-3)
516 : :
517 : : static const int no_arg_map[] = { 0 };
518 : : static const int arg0_map[] = { 1, 0 };
519 : : static const int arg2_map[] = { 1, 2 };
520 : : static const int arg2_arg3_map[] = { 2, 2, 3 };
521 : : static const int arg2_arg4_map[] = { 2, 2, 4 };
522 : : static const int arg2_arg5_arg6_map[] = { 3, 2, 5, 6 };
523 : : static const int arg2_arg4_arg5_map[] = { 3, 2, 4, 5 };
524 : : static const int arg3_arg2_map[] = { 2, 3, 2 };
525 : : static const int op1_op0_map[] = { 2, 1, 0 };
526 : : static const int off_map[] = { 1, GATHER_SCATTER_OFFSET };
527 : : static const int off_op0_map[] = { 2, GATHER_SCATTER_OFFSET, 0 };
528 : : static const int off_arg2_arg3_map[] = { 3, GATHER_SCATTER_OFFSET, 2, 3 };
529 : : static const int off_arg3_arg2_map[] = { 3, GATHER_SCATTER_OFFSET, 3, 2 };
530 : : static const int mask_call_maps[6][7] = {
531 : : { 1, 1, },
532 : : { 2, 1, 2, },
533 : : { 3, 1, 2, 3, },
534 : : { 4, 1, 2, 3, 4, },
535 : : { 5, 1, 2, 3, 4, 5, },
536 : : { 6, 1, 2, 3, 4, 5, 6 },
537 : : };
538 : :
539 : : /* For most SLP statements, there is a one-to-one mapping between
540 : : gimple arguments and child nodes. If that is not true for STMT,
541 : : return an array that contains:
542 : :
543 : : - the number of child nodes, followed by
544 : : - for each child node, the index of the argument associated with that node.
545 : : The special index -1 is the first operand of an embedded comparison and
546 : : the special index -2 is the second operand of an embedded comparison.
547 : : The special indes -3 is the offset of a gather as analyzed by
548 : : vect_check_gather_scatter.
549 : :
550 : : SWAP is as for vect_get_and_check_slp_defs. */
551 : :
552 : : static const int *
553 : 19635276 : vect_get_operand_map (const gimple *stmt, bool gather_scatter_p = false,
554 : : unsigned char swap = 0)
555 : : {
556 : 19635276 : if (auto assign = dyn_cast<const gassign *> (stmt))
557 : : {
558 : 18044108 : if (gimple_assign_rhs_code (assign) == COND_EXPR
559 : 18044108 : && COMPARISON_CLASS_P (gimple_assign_rhs1 (assign)))
560 : 0 : gcc_unreachable ();
561 : 18044108 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) == tcc_comparison
562 : 18044108 : && swap)
563 : : return op1_op0_map;
564 : 18002783 : if (gather_scatter_p)
565 : 33172 : return (TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
566 : 33172 : ? off_op0_map : off_map);
567 : : }
568 : 19560779 : gcc_assert (!swap);
569 : 19560779 : if (auto call = dyn_cast<const gcall *> (stmt))
570 : : {
571 : 132302 : if (gimple_call_internal_p (call))
572 : 70594 : switch (gimple_call_internal_fn (call))
573 : : {
574 : 11285 : case IFN_MASK_LOAD:
575 : 18350 : return gather_scatter_p ? off_arg2_arg3_map : arg2_arg3_map;
576 : :
577 : 0 : case IFN_GATHER_LOAD:
578 : 0 : return arg2_map;
579 : :
580 : 0 : case IFN_MASK_GATHER_LOAD:
581 : 0 : case IFN_MASK_LEN_GATHER_LOAD:
582 : 0 : return arg2_arg5_arg6_map;
583 : :
584 : 0 : case IFN_SCATTER_STORE:
585 : 0 : return arg2_arg4_map;
586 : :
587 : 0 : case IFN_MASK_SCATTER_STORE:
588 : 0 : case IFN_MASK_LEN_SCATTER_STORE:
589 : 0 : return arg2_arg4_arg5_map;
590 : :
591 : 5830 : case IFN_MASK_STORE:
592 : 10362 : return gather_scatter_p ? off_arg3_arg2_map : arg3_arg2_map;
593 : :
594 : 842 : case IFN_MASK_CALL:
595 : 842 : {
596 : 842 : unsigned nargs = gimple_call_num_args (call);
597 : 842 : if (nargs >= 2 && nargs <= 7)
598 : 842 : return mask_call_maps[nargs-2];
599 : : else
600 : : return nullptr;
601 : : }
602 : :
603 : 140 : case IFN_CLZ:
604 : 140 : case IFN_CTZ:
605 : 140 : return arg0_map;
606 : :
607 : 6294 : case IFN_GOMP_SIMD_LANE:
608 : 6294 : return no_arg_map;
609 : :
610 : : default:
611 : : break;
612 : : }
613 : : }
614 : : return nullptr;
615 : : }
616 : :
617 : : /* Return the SLP node child index for operand OP of STMT. */
618 : :
619 : : int
620 : 1330319 : vect_slp_child_index_for_operand (const gimple *stmt, int op,
621 : : bool gather_scatter_p)
622 : : {
623 : 1330319 : const int *opmap = vect_get_operand_map (stmt, gather_scatter_p);
624 : 1330319 : if (!opmap)
625 : : return op;
626 : 19663 : for (int i = 1; i < 1 + opmap[0]; ++i)
627 : 19663 : if (opmap[i] == op)
628 : 10637 : return i - 1;
629 : 0 : gcc_unreachable ();
630 : : }
631 : :
632 : : /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
633 : : they are of a valid type and that they match the defs of the first stmt of
634 : : the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
635 : : by swapping operands of STMTS[STMT_NUM] when possible. Non-zero SWAP
636 : : indicates swap is required for cond_expr stmts. Specifically, SWAP
637 : : is 1 if STMT is cond and operands of comparison need to be swapped;
638 : : SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
639 : :
640 : : If there was a fatal error return -1; if the error could be corrected by
641 : : swapping operands of father node of this one, return 1; if everything is
642 : : ok return 0. */
643 : : static int
644 : 12592062 : vect_get_and_check_slp_defs (vec_info *vinfo, tree vectype, unsigned char swap,
645 : : bool *skip_args,
646 : : vec<stmt_vec_info> stmts, unsigned stmt_num,
647 : : vec<slp_oprnd_info> *oprnds_info)
648 : : {
649 : 12592062 : stmt_vec_info stmt_info = stmts[stmt_num];
650 : 12592062 : tree oprnd;
651 : 12592062 : unsigned int i, number_of_oprnds;
652 : 12592062 : enum vect_def_type dt = vect_uninitialized_def;
653 : 12592062 : slp_oprnd_info oprnd_info;
654 : 12592062 : gather_scatter_info gs_info;
655 : 12592062 : unsigned int gs_op = -1u;
656 : 12592062 : unsigned int commutative_op = -1U;
657 : 12592062 : bool first = stmt_num == 0;
658 : :
659 : 12592062 : if (!stmt_info)
660 : : {
661 : 0 : for (auto oi : *oprnds_info)
662 : : {
663 : 0 : oi->def_stmts.quick_push (NULL);
664 : 0 : oi->ops.quick_push (NULL_TREE);
665 : : }
666 : : return 0;
667 : : }
668 : :
669 : 12592062 : if (!is_a<gcall *> (stmt_info->stmt)
670 : : && !is_a<gassign *> (stmt_info->stmt)
671 : : && !is_a<gphi *> (stmt_info->stmt))
672 : : return -1;
673 : :
674 : 12592062 : number_of_oprnds = gimple_num_args (stmt_info->stmt);
675 : 12592062 : const int *map
676 : 25184124 : = vect_get_operand_map (stmt_info->stmt,
677 : 12592062 : STMT_VINFO_GATHER_SCATTER_P (stmt_info), swap);
678 : 12592062 : if (map)
679 : 64879 : number_of_oprnds = *map++;
680 : 12592062 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
681 : : {
682 : 38406 : if (gimple_call_internal_p (stmt))
683 : : {
684 : 22837 : internal_fn ifn = gimple_call_internal_fn (stmt);
685 : 22837 : commutative_op = first_commutative_argument (ifn);
686 : 22837 : if (internal_gather_scatter_fn_p (ifn))
687 : : {
688 : 0 : vect_describe_gather_scatter_call
689 : 0 : (stmt_info,
690 : 0 : first ? &(*oprnds_info)[0]->first_gs_info : &gs_info);
691 : 0 : if (first)
692 : 0 : (*oprnds_info)[0]->first_gs_p = true;
693 : : gs_op = 0;
694 : : }
695 : : }
696 : : }
697 : 12553656 : else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
698 : : {
699 : 14373879 : if (commutative_tree_code (gimple_assign_rhs_code (stmt)))
700 : 8213685 : commutative_op = 0;
701 : : }
702 : :
703 : 12592062 : bool swapped = (swap != 0);
704 : 12592062 : bool backedge = false;
705 : 12592062 : enum vect_def_type *dts = XALLOCAVEC (enum vect_def_type, number_of_oprnds);
706 : 34896657 : for (i = 0; i < number_of_oprnds; i++)
707 : : {
708 : 22307024 : oprnd_info = (*oprnds_info)[i];
709 : 22307024 : int opno = map ? map[i] : int (i);
710 : 22307024 : if (opno == GATHER_SCATTER_OFFSET)
711 : : {
712 : 17006 : gcc_assert (STMT_VINFO_GATHER_SCATTER_P (stmt_info));
713 : 17006 : if (!is_a <loop_vec_info> (vinfo)
714 : 17006 : || !vect_check_gather_scatter (stmt_info, vectype,
715 : : as_a <loop_vec_info> (vinfo),
716 : : first ? &oprnd_info->first_gs_info
717 : : : &gs_info))
718 : 2429 : return -1;
719 : :
720 : 17006 : if (first)
721 : : {
722 : 16793 : oprnd_info->first_gs_p = true;
723 : 16793 : oprnd = oprnd_info->first_gs_info.offset;
724 : : }
725 : : else
726 : : {
727 : 213 : gs_op = i;
728 : 213 : oprnd = gs_info.offset;
729 : : }
730 : : }
731 : 22290018 : else if (opno < 0)
732 : 0 : oprnd = TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
733 : : else
734 : : {
735 : 22290018 : oprnd = gimple_arg (stmt_info->stmt, opno);
736 : 22290018 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
737 : : {
738 : 1475602 : edge e = gimple_phi_arg_edge (stmt, opno);
739 : 2951204 : backedge = (is_a <bb_vec_info> (vinfo)
740 : 2415786 : ? e->flags & EDGE_DFS_BACK
741 : 940184 : : dominated_by_p (CDI_DOMINATORS, e->src,
742 : 940184 : gimple_bb (stmt_info->stmt)));
743 : : }
744 : : }
745 : 22307024 : if (TREE_CODE (oprnd) == VIEW_CONVERT_EXPR)
746 : 2386 : oprnd = TREE_OPERAND (oprnd, 0);
747 : :
748 : 22307024 : stmt_vec_info def_stmt_info;
749 : 22307024 : if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info))
750 : : {
751 : 1081 : if (dump_enabled_p ())
752 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
753 : : "Build SLP failed: can't analyze def for %T\n",
754 : : oprnd);
755 : :
756 : 1081 : return -1;
757 : : }
758 : :
759 : 22305943 : if (skip_args[i])
760 : : {
761 : 864321 : oprnd_info->def_stmts.quick_push (NULL);
762 : 864321 : oprnd_info->ops.quick_push (NULL_TREE);
763 : 864321 : oprnd_info->first_dt = vect_uninitialized_def;
764 : 864321 : continue;
765 : : }
766 : :
767 : 21441622 : oprnd_info->def_stmts.quick_push (def_stmt_info);
768 : 21441622 : oprnd_info->ops.quick_push (oprnd);
769 : :
770 : 21441622 : if (def_stmt_info
771 : 21441622 : && is_pattern_stmt_p (def_stmt_info))
772 : : {
773 : 343155 : if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info))
774 : : != def_stmt_info)
775 : 246625 : oprnd_info->any_pattern = true;
776 : : else
777 : : /* If we promote this to external use the original stmt def. */
778 : 96530 : oprnd_info->ops.last ()
779 : 193060 : = gimple_get_lhs (vect_orig_stmt (def_stmt_info)->stmt);
780 : : }
781 : :
782 : : /* If there's a extern def on a backedge make sure we can
783 : : code-generate at the region start.
784 : : ??? This is another case that could be fixed by adjusting
785 : : how we split the function but at the moment we'd have conflicting
786 : : goals there. */
787 : 21441622 : if (backedge
788 : 110882 : && dts[i] == vect_external_def
789 : 1369 : && is_a <bb_vec_info> (vinfo)
790 : 1369 : && TREE_CODE (oprnd) == SSA_NAME
791 : 1348 : && !SSA_NAME_IS_DEFAULT_DEF (oprnd)
792 : 21442970 : && !dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
793 : 1348 : gimple_bb (SSA_NAME_DEF_STMT (oprnd))))
794 : : {
795 : 1348 : if (dump_enabled_p ())
796 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
797 : : "Build SLP failed: extern def %T only defined "
798 : : "on backedge\n", oprnd);
799 : 1348 : return -1;
800 : : }
801 : :
802 : 21440274 : if (first)
803 : : {
804 : 4697502 : tree type = TREE_TYPE (oprnd);
805 : 4697502 : dt = dts[i];
806 : :
807 : : /* For the swapping logic below force vect_reduction_def
808 : : for the reduction op in a SLP reduction group. */
809 : 4697502 : if (!STMT_VINFO_DATA_REF (stmt_info)
810 : 3622276 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
811 : 5359 : && (int)i == STMT_VINFO_REDUC_IDX (stmt_info)
812 : 4700154 : && def_stmt_info)
813 : 2652 : dts[i] = dt = vect_reduction_def;
814 : :
815 : : /* Check the types of the definition. */
816 : 4697502 : switch (dt)
817 : : {
818 : 4697502 : case vect_external_def:
819 : 4697502 : case vect_constant_def:
820 : 4697502 : case vect_internal_def:
821 : 4697502 : case vect_reduction_def:
822 : 4697502 : case vect_double_reduction_def:
823 : 4697502 : case vect_induction_def:
824 : 4697502 : case vect_nested_cycle:
825 : 4697502 : case vect_first_order_recurrence:
826 : 4697502 : break;
827 : :
828 : 0 : default:
829 : : /* FORNOW: Not supported. */
830 : 0 : if (dump_enabled_p ())
831 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
832 : : "Build SLP failed: illegal type of def %T\n",
833 : : oprnd);
834 : 0 : return -1;
835 : : }
836 : :
837 : 4697502 : oprnd_info->first_dt = dt;
838 : 4697502 : oprnd_info->first_op_type = type;
839 : : }
840 : : }
841 : 12589633 : if (first)
842 : : return 0;
843 : :
844 : : /* Now match the operand definition types to that of the first stmt. */
845 : 25646399 : for (i = 0; i < number_of_oprnds;)
846 : : {
847 : 16730433 : if (skip_args[i])
848 : : {
849 : 18546 : ++i;
850 : 18546 : continue;
851 : : }
852 : :
853 : 16711887 : oprnd_info = (*oprnds_info)[i];
854 : 16711887 : dt = dts[i];
855 : 16711887 : stmt_vec_info def_stmt_info = oprnd_info->def_stmts[stmt_num];
856 : 16711887 : oprnd = oprnd_info->ops[stmt_num];
857 : 16711887 : tree type = TREE_TYPE (oprnd);
858 : :
859 : 16711887 : if (!types_compatible_p (oprnd_info->first_op_type, type))
860 : : {
861 : 89292 : if (dump_enabled_p ())
862 : 107 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
863 : : "Build SLP failed: different operand types\n");
864 : 89292 : return 1;
865 : : }
866 : :
867 : 16622595 : if ((gs_op == i) != oprnd_info->first_gs_p)
868 : : {
869 : 0 : if (dump_enabled_p ())
870 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
871 : : "Build SLP failed: mixed gather and non-gather\n");
872 : 0 : return 1;
873 : : }
874 : 16622595 : else if (gs_op == i)
875 : : {
876 : 183 : if (!operand_equal_p (oprnd_info->first_gs_info.base,
877 : 183 : gs_info.base))
878 : : {
879 : 16 : if (dump_enabled_p ())
880 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
881 : : "Build SLP failed: different gather base\n");
882 : 16 : return 1;
883 : : }
884 : 167 : if (oprnd_info->first_gs_info.scale != gs_info.scale)
885 : : {
886 : 8 : if (dump_enabled_p ())
887 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
888 : : "Build SLP failed: different gather scale\n");
889 : 8 : return 1;
890 : : }
891 : : }
892 : :
893 : : /* Not first stmt of the group, check that the def-stmt/s match
894 : : the def-stmt/s of the first stmt. Allow different definition
895 : : types for reduction chains: the first stmt must be a
896 : : vect_reduction_def (a phi node), and the rest
897 : : end in the reduction chain. */
898 : 16622571 : if ((!vect_def_types_match (oprnd_info->first_dt, dt)
899 : 271660 : && !(oprnd_info->first_dt == vect_reduction_def
900 : 4074 : && !STMT_VINFO_DATA_REF (stmt_info)
901 : 4074 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
902 : 4071 : && def_stmt_info
903 : 4069 : && !STMT_VINFO_DATA_REF (def_stmt_info)
904 : 4063 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
905 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
906 : 16354823 : || (!STMT_VINFO_DATA_REF (stmt_info)
907 : 15069125 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
908 : 8436 : && ((!def_stmt_info
909 : 8272 : || STMT_VINFO_DATA_REF (def_stmt_info)
910 : 15619 : || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
911 : : != REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
912 : 8436 : != (oprnd_info->first_dt != vect_reduction_def))))
913 : : {
914 : : /* Try swapping operands if we got a mismatch. For BB
915 : : vectorization only in case it will clearly improve things. */
916 : 270299 : if (i == commutative_op && !swapped
917 : 268184 : && (!is_a <bb_vec_info> (vinfo)
918 : 4564 : || (!vect_def_types_match ((*oprnds_info)[i+1]->first_dt,
919 : 4564 : dts[i+1])
920 : 682 : && (vect_def_types_match (oprnd_info->first_dt, dts[i+1])
921 : : || vect_def_types_match
922 : 191 : ((*oprnds_info)[i+1]->first_dt, dts[i])))))
923 : : {
924 : 2115 : if (dump_enabled_p ())
925 : 322 : dump_printf_loc (MSG_NOTE, vect_location,
926 : : "trying swapped operands\n");
927 : 2115 : std::swap (dts[i], dts[i+1]);
928 : 2115 : std::swap ((*oprnds_info)[i]->def_stmts[stmt_num],
929 : 2115 : (*oprnds_info)[i+1]->def_stmts[stmt_num]);
930 : 2115 : std::swap ((*oprnds_info)[i]->ops[stmt_num],
931 : 2115 : (*oprnds_info)[i+1]->ops[stmt_num]);
932 : : /* After swapping some operands we lost track whether an
933 : : operand has any pattern defs so be conservative here. */
934 : 2115 : if ((*oprnds_info)[i]->any_pattern
935 : 2115 : || (*oprnds_info)[i+1]->any_pattern)
936 : 4 : (*oprnds_info)[i]->any_pattern
937 : 2 : = (*oprnds_info)[i+1]->any_pattern = true;
938 : 2115 : swapped = true;
939 : 2115 : continue;
940 : : }
941 : :
942 : 266069 : if (is_a <bb_vec_info> (vinfo)
943 : 255456 : && !oprnd_info->any_pattern
944 : 521296 : && number_of_oprnds > 1)
945 : : {
946 : : /* Now for commutative ops we should see whether we can
947 : : make the other operand matching. */
948 : 100670 : if (dump_enabled_p ())
949 : 149 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
950 : : "treating operand as external\n");
951 : 100670 : oprnd_info->first_dt = dt = vect_external_def;
952 : : }
953 : : else
954 : : {
955 : 165399 : if (dump_enabled_p ())
956 : 484 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
957 : : "Build SLP failed: different types\n");
958 : 165399 : return 1;
959 : : }
960 : : }
961 : :
962 : : /* Make sure to demote the overall operand to external. */
963 : 16455057 : if (dt == vect_external_def)
964 : 337446 : oprnd_info->first_dt = vect_external_def;
965 : : /* For a SLP reduction chain we want to duplicate the reduction to
966 : : each of the chain members. That gets us a sane SLP graph (still
967 : : the stmts are not 100% correct wrt the initial values). */
968 : 16117611 : else if ((dt == vect_internal_def
969 : 16117611 : || dt == vect_reduction_def)
970 : 15220514 : && oprnd_info->first_dt == vect_reduction_def
971 : 20403 : && !STMT_VINFO_DATA_REF (stmt_info)
972 : 20403 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
973 : 3912 : && !STMT_VINFO_DATA_REF (def_stmt_info)
974 : 16121523 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
975 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
976 : : {
977 : 3912 : oprnd_info->def_stmts[stmt_num] = oprnd_info->def_stmts[0];
978 : 3912 : oprnd_info->ops[stmt_num] = oprnd_info->ops[0];
979 : : }
980 : :
981 : 16455057 : ++i;
982 : : }
983 : :
984 : : /* Swap operands. */
985 : 8915966 : if (swapped)
986 : : {
987 : 40823 : if (dump_enabled_p ())
988 : 377 : dump_printf_loc (MSG_NOTE, vect_location,
989 : : "swapped operands to match def types in %G",
990 : : stmt_info->stmt);
991 : : }
992 : :
993 : : return 0;
994 : : }
995 : :
996 : : /* Return true if call statements CALL1 and CALL2 are similar enough
997 : : to be combined into the same SLP group. */
998 : :
999 : : bool
1000 : 20761 : compatible_calls_p (gcall *call1, gcall *call2, bool allow_two_operators)
1001 : : {
1002 : 20761 : unsigned int nargs = gimple_call_num_args (call1);
1003 : 20761 : if (nargs != gimple_call_num_args (call2))
1004 : : return false;
1005 : :
1006 : 18877 : auto cfn1 = gimple_call_combined_fn (call1);
1007 : 18877 : auto cfn2 = gimple_call_combined_fn (call2);
1008 : 18877 : if (cfn1 != cfn2
1009 : 2 : && (!allow_two_operators
1010 : 2 : || !((cfn1 == CFN_FMA || cfn1 == CFN_FMS)
1011 : 2 : && (cfn2 == CFN_FMA || cfn2 == CFN_FMS))))
1012 : : return false;
1013 : :
1014 : 18877 : if (gimple_call_internal_p (call1))
1015 : : {
1016 : 6885 : if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
1017 : 6885 : TREE_TYPE (gimple_call_lhs (call2))))
1018 : : return false;
1019 : 13977 : for (unsigned int i = 0; i < nargs; ++i)
1020 : 7092 : if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
1021 : 7092 : TREE_TYPE (gimple_call_arg (call2, i))))
1022 : : return false;
1023 : : }
1024 : : else
1025 : : {
1026 : 11992 : if (!operand_equal_p (gimple_call_fn (call1),
1027 : 11992 : gimple_call_fn (call2), 0))
1028 : : return false;
1029 : :
1030 : 25998 : if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
1031 : : return false;
1032 : : }
1033 : :
1034 : : /* Check that any unvectorized arguments are equal. */
1035 : 15551 : if (const int *map = vect_get_operand_map (call1))
1036 : : {
1037 : 15 : unsigned int nkept = *map++;
1038 : 15 : unsigned int mapi = 0;
1039 : 57 : for (unsigned int i = 0; i < nargs; ++i)
1040 : 42 : if (mapi < nkept && map[mapi] == int (i))
1041 : 27 : mapi += 1;
1042 : 15 : else if (!operand_equal_p (gimple_call_arg (call1, i),
1043 : 15 : gimple_call_arg (call2, i)))
1044 : : return false;
1045 : : }
1046 : :
1047 : : return true;
1048 : : }
1049 : :
1050 : : /* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
1051 : : caller's attempt to find the vector type in STMT_INFO with the narrowest
1052 : : element type. Return true if VECTYPE is nonnull and if it is valid
1053 : : for STMT_INFO. When returning true, update MAX_NUNITS to reflect the
1054 : : number of units in VECTYPE. GROUP_SIZE and MAX_NUNITS are as for
1055 : : vect_build_slp_tree. */
1056 : :
1057 : : static bool
1058 : 5602006 : vect_record_max_nunits (vec_info *vinfo, stmt_vec_info stmt_info,
1059 : : unsigned int group_size,
1060 : : tree vectype, poly_uint64 *max_nunits)
1061 : : {
1062 : 5602006 : if (!vectype)
1063 : : {
1064 : 4104 : if (dump_enabled_p ())
1065 : 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1066 : : "Build SLP failed: unsupported data-type in %G\n",
1067 : : stmt_info->stmt);
1068 : : /* Fatal mismatch. */
1069 : 4104 : return false;
1070 : : }
1071 : :
1072 : : /* If populating the vector type requires unrolling then fail
1073 : : before adjusting *max_nunits for basic-block vectorization. */
1074 : 5597902 : if (is_a <bb_vec_info> (vinfo)
1075 : 5597902 : && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
1076 : : {
1077 : 140673 : if (dump_enabled_p ())
1078 : 34 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1079 : : "Build SLP failed: unrolling required "
1080 : : "in basic block SLP\n");
1081 : : /* Fatal mismatch. */
1082 : 140673 : return false;
1083 : : }
1084 : :
1085 : : /* In case of multiple types we need to detect the smallest type. */
1086 : 5457229 : vect_update_max_nunits (max_nunits, vectype);
1087 : 5457229 : return true;
1088 : : }
1089 : :
1090 : : /* Verify if the scalar stmts STMTS are isomorphic, require data
1091 : : permutation or are of unsupported types of operation. Return
1092 : : true if they are, otherwise return false and indicate in *MATCHES
1093 : : which stmts are not isomorphic to the first one. If MATCHES[0]
1094 : : is false then this indicates the comparison could not be
1095 : : carried out or the stmts will never be vectorized by SLP.
1096 : :
1097 : : Note COND_EXPR is possibly isomorphic to another one after swapping its
1098 : : operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
1099 : : the first stmt by swapping the two operands of comparison; set SWAP[i]
1100 : : to 2 if stmt I is isormorphic to the first stmt by inverting the code
1101 : : of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
1102 : : to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
1103 : :
1104 : : static bool
1105 : 5693238 : vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
1106 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1107 : : poly_uint64 *max_nunits, bool *matches,
1108 : : bool *two_operators, tree *node_vectype)
1109 : : {
1110 : 5693238 : unsigned int i;
1111 : 5693238 : stmt_vec_info first_stmt_info = stmts[0];
1112 : 5693238 : code_helper first_stmt_code = ERROR_MARK;
1113 : 5693238 : code_helper alt_stmt_code = ERROR_MARK;
1114 : 5693238 : code_helper first_cond_code = ERROR_MARK;
1115 : 5693238 : bool need_same_oprnds = false;
1116 : 5693238 : tree first_lhs = NULL_TREE;
1117 : 5693238 : tree first_op1 = NULL_TREE;
1118 : 5693238 : stmt_vec_info first_load = NULL, prev_first_load = NULL;
1119 : 5693238 : bool first_stmt_ldst_p = false, first_stmt_ldst_masklen_p = false;
1120 : 5693238 : bool first_stmt_phi_p = false;
1121 : 5693238 : int first_reduc_idx = -1;
1122 : 5693238 : bool maybe_soft_fail = false;
1123 : 5693238 : tree soft_fail_nunits_vectype = NULL_TREE;
1124 : :
1125 : 5693238 : tree vectype, nunits_vectype;
1126 : 5693238 : if (!vect_get_vector_types_for_stmt (vinfo, first_stmt_info, &vectype,
1127 : : &nunits_vectype, group_size))
1128 : : {
1129 : : /* Fatal mismatch. */
1130 : 203361 : matches[0] = false;
1131 : 203361 : return false;
1132 : : }
1133 : 5489877 : if (is_a <bb_vec_info> (vinfo)
1134 : 5489877 : && known_le (TYPE_VECTOR_SUBPARTS (vectype), 1U))
1135 : : {
1136 : 350767 : if (dump_enabled_p ())
1137 : 282 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1138 : : "Build SLP failed: not using single lane "
1139 : : "vector type %T\n", vectype);
1140 : 350767 : matches[0] = false;
1141 : 350767 : return false;
1142 : : }
1143 : : /* Record nunits required but continue analysis, producing matches[]
1144 : : as if nunits was not an issue. This allows splitting of groups
1145 : : to happen. */
1146 : 5139110 : if (nunits_vectype
1147 : 5139110 : && !vect_record_max_nunits (vinfo, first_stmt_info, group_size,
1148 : : nunits_vectype, max_nunits))
1149 : : {
1150 : 140673 : gcc_assert (is_a <bb_vec_info> (vinfo));
1151 : 140673 : maybe_soft_fail = true;
1152 : 140673 : soft_fail_nunits_vectype = nunits_vectype;
1153 : : }
1154 : :
1155 : 5139110 : gcc_assert (vectype || !gimple_get_lhs (first_stmt_info->stmt));
1156 : 5139110 : *node_vectype = vectype;
1157 : :
1158 : : /* For every stmt in NODE find its def stmt/s. */
1159 : 5139110 : stmt_vec_info stmt_info;
1160 : 21723696 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
1161 : : {
1162 : 16743699 : bool ldst_p = false;
1163 : 16743699 : bool ldst_masklen_p = false;
1164 : 16743699 : bool phi_p = false;
1165 : 16743699 : code_helper rhs_code = ERROR_MARK;
1166 : :
1167 : 16743699 : swap[i] = 0;
1168 : 16743699 : matches[i] = false;
1169 : 16743699 : if (!stmt_info)
1170 : : {
1171 : 50919 : matches[i] = true;
1172 : 16635505 : continue;
1173 : : }
1174 : :
1175 : 16692780 : gimple *stmt = stmt_info->stmt;
1176 : 16692780 : if (dump_enabled_p ())
1177 : 212904 : dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for %G", stmt);
1178 : :
1179 : : /* Fail to vectorize statements marked as unvectorizable, throw
1180 : : or are volatile. */
1181 : 16692780 : if (!STMT_VINFO_VECTORIZABLE (stmt_info)
1182 : 16504977 : || stmt_can_throw_internal (cfun, stmt)
1183 : 32241211 : || gimple_has_volatile_ops (stmt))
1184 : : {
1185 : 193255 : if (dump_enabled_p ())
1186 : 213 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1187 : : "Build SLP failed: unvectorizable statement %G",
1188 : : stmt);
1189 : : /* ??? For BB vectorization we want to commutate operands in a way
1190 : : to shuffle all unvectorizable defs into one operand and have
1191 : : the other still vectorized. The following doesn't reliably
1192 : : work for this though but it's the easiest we can do here. */
1193 : 193255 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1194 : 60956 : continue;
1195 : : /* Fatal mismatch. */
1196 : 132299 : matches[0] = false;
1197 : 132299 : return false;
1198 : : }
1199 : :
1200 : 16499525 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
1201 : 16499525 : tree lhs = gimple_get_lhs (stmt);
1202 : 16499525 : if (lhs == NULL_TREE && !call_stmt)
1203 : : {
1204 : 36 : if (dump_enabled_p ())
1205 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1206 : : "Build SLP failed: not GIMPLE_ASSIGN nor "
1207 : : "GIMPLE_CALL %G", stmt);
1208 : 36 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1209 : 36 : continue;
1210 : : /* Fatal mismatch. */
1211 : 0 : matches[0] = false;
1212 : 0 : return false;
1213 : : }
1214 : :
1215 : 16499489 : if (call_stmt)
1216 : : {
1217 : 88005 : combined_fn cfn = gimple_call_combined_fn (call_stmt);
1218 : 88005 : if (cfn != CFN_LAST && cfn != CFN_MASK_CALL)
1219 : 47721 : rhs_code = cfn;
1220 : : else
1221 : : rhs_code = CALL_EXPR;
1222 : :
1223 : 88005 : if (cfn == CFN_GATHER_LOAD
1224 : 88005 : || cfn == CFN_SCATTER_STORE)
1225 : : ldst_p = true;
1226 : : else if (cfn == CFN_MASK_LOAD
1227 : : || cfn == CFN_MASK_GATHER_LOAD
1228 : : || cfn == CFN_MASK_LEN_GATHER_LOAD
1229 : : || cfn == CFN_MASK_SCATTER_STORE
1230 : : || cfn == CFN_MASK_LEN_SCATTER_STORE)
1231 : : {
1232 : : ldst_p = true;
1233 : : ldst_masklen_p = true;
1234 : : }
1235 : : else if (cfn == CFN_MASK_STORE)
1236 : : {
1237 : : ldst_p = true;
1238 : : ldst_masklen_p = true;
1239 : : rhs_code = CFN_MASK_STORE;
1240 : : }
1241 : : else if (cfn == CFN_GOMP_SIMD_LANE)
1242 : : ;
1243 : 79936 : else if ((cfn != CFN_LAST
1244 : : && cfn != CFN_MASK_CALL
1245 : 39652 : && internal_fn_p (cfn)
1246 : 30418 : && !vectorizable_internal_fn_p (as_internal_fn (cfn)))
1247 : 79862 : || gimple_call_tail_p (call_stmt)
1248 : 79862 : || gimple_call_noreturn_p (call_stmt)
1249 : 159798 : || gimple_call_chain (call_stmt))
1250 : : {
1251 : 423 : if (dump_enabled_p ())
1252 : 13 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1253 : : "Build SLP failed: unsupported call type %G",
1254 : : (gimple *) call_stmt);
1255 : 423 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1256 : 63 : continue;
1257 : : /* Fatal mismatch. */
1258 : 360 : matches[0] = false;
1259 : 360 : return false;
1260 : : }
1261 : : }
1262 : 16411484 : else if (gimple_code (stmt) == GIMPLE_PHI)
1263 : : {
1264 : : rhs_code = ERROR_MARK;
1265 : : phi_p = true;
1266 : : }
1267 : : else
1268 : : {
1269 : 15454938 : rhs_code = gimple_assign_rhs_code (stmt);
1270 : 15454938 : ldst_p = STMT_VINFO_DATA_REF (stmt_info) != nullptr;
1271 : : }
1272 : :
1273 : : /* Check the operation. */
1274 : 16499066 : if (i == 0)
1275 : : {
1276 : 5006451 : first_lhs = lhs;
1277 : 5006451 : first_stmt_code = rhs_code;
1278 : 5006451 : first_stmt_ldst_p = ldst_p;
1279 : 5006451 : first_stmt_ldst_masklen_p = ldst_masklen_p;
1280 : 5006451 : first_stmt_phi_p = phi_p;
1281 : 5006451 : first_reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
1282 : :
1283 : : /* Shift arguments should be equal in all the packed stmts for a
1284 : : vector shift with scalar shift operand. */
1285 : 5006451 : if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
1286 : 4895093 : || rhs_code == LROTATE_EXPR
1287 : 9901502 : || rhs_code == RROTATE_EXPR)
1288 : : {
1289 : : /* First see if we have a vector/vector shift. */
1290 : 111602 : if (!directly_supported_p (rhs_code, vectype, optab_vector))
1291 : : {
1292 : : /* No vector/vector shift, try for a vector/scalar shift. */
1293 : 104884 : if (!directly_supported_p (rhs_code, vectype, optab_scalar))
1294 : : {
1295 : 7886 : if (dump_enabled_p ())
1296 : 382 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1297 : : "Build SLP failed: "
1298 : : "op not supported by target.\n");
1299 : 7886 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1300 : : continue;
1301 : : /* Fatal mismatch. */
1302 : 7886 : matches[0] = false;
1303 : 7886 : return false;
1304 : : }
1305 : 96998 : need_same_oprnds = true;
1306 : 96998 : first_op1 = gimple_assign_rhs2 (stmt);
1307 : : }
1308 : : }
1309 : 4894849 : else if (rhs_code == WIDEN_LSHIFT_EXPR)
1310 : : {
1311 : 0 : need_same_oprnds = true;
1312 : 0 : first_op1 = gimple_assign_rhs2 (stmt);
1313 : : }
1314 : 4894849 : else if (!ldst_p
1315 : 4894849 : && rhs_code == BIT_FIELD_REF)
1316 : : {
1317 : 5052 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1318 : 5052 : if (!is_a <bb_vec_info> (vinfo)
1319 : 4958 : || TREE_CODE (vec) != SSA_NAME
1320 : : /* When the element types are not compatible we pun the
1321 : : source to the target vectype which requires equal size. */
1322 : 9998 : || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
1323 : 4241 : || !types_compatible_p (TREE_TYPE (vectype),
1324 : 4241 : TREE_TYPE (TREE_TYPE (vec))))
1325 : 819 : && !operand_equal_p (TYPE_SIZE (vectype),
1326 : 819 : TYPE_SIZE (TREE_TYPE (vec)))))
1327 : : {
1328 : 683 : if (dump_enabled_p ())
1329 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1330 : : "Build SLP failed: "
1331 : : "BIT_FIELD_REF not supported\n");
1332 : : /* Fatal mismatch. */
1333 : 683 : matches[0] = false;
1334 : 683 : return false;
1335 : : }
1336 : : }
1337 : 4889797 : else if (rhs_code == CFN_DIV_POW2)
1338 : : {
1339 : 0 : need_same_oprnds = true;
1340 : 0 : first_op1 = gimple_call_arg (call_stmt, 1);
1341 : : }
1342 : 4889797 : else if (rhs_code == CFN_GOMP_SIMD_LANE)
1343 : : {
1344 : 3147 : need_same_oprnds = true;
1345 : 3147 : first_op1 = gimple_call_arg (call_stmt, 1);
1346 : : }
1347 : : }
1348 : : else
1349 : : {
1350 : 11493460 : if (first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1351 : : /* For SLP reduction groups the index isn't necessarily
1352 : : uniform but only that of the first stmt matters. */
1353 : 11492615 : && !(first_reduc_idx != -1
1354 : 1926 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1355 : 1926 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
1356 : : {
1357 : 845 : if (dump_enabled_p ())
1358 : : {
1359 : 69 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1360 : : "Build SLP failed: different reduc_idx "
1361 : : "%d instead of %d in %G",
1362 : : STMT_VINFO_REDUC_IDX (stmt_info),
1363 : : first_reduc_idx, stmt);
1364 : : }
1365 : : /* Mismatch. */
1366 : 845 : continue;
1367 : : }
1368 : 11491770 : if (!ldst_p
1369 : 9113109 : && first_stmt_code != rhs_code
1370 : 12877843 : && alt_stmt_code == ERROR_MARK)
1371 : : alt_stmt_code = rhs_code;
1372 : 12861093 : if ((!ldst_p
1373 : 9113109 : && first_stmt_code != rhs_code
1374 : 1386073 : && (first_stmt_code != IMAGPART_EXPR
1375 : 112 : || rhs_code != REALPART_EXPR)
1376 : 1386061 : && (first_stmt_code != REALPART_EXPR
1377 : 986 : || rhs_code != IMAGPART_EXPR)
1378 : : /* Handle mismatches in plus/minus by computing both
1379 : : and merging the results. */
1380 : 1386058 : && !((((first_stmt_code == PLUS_EXPR
1381 : 1297536 : || first_stmt_code == MINUS_EXPR)
1382 : 107932 : && (alt_stmt_code == PLUS_EXPR
1383 : 99195 : || alt_stmt_code == MINUS_EXPR))
1384 : 1370207 : || ((first_stmt_code == CFN_FMA
1385 : 1370205 : || first_stmt_code == CFN_FMS)
1386 : 2 : && (alt_stmt_code == CFN_FMA
1387 : 2 : || alt_stmt_code == CFN_FMS)))
1388 : 15853 : && rhs_code == alt_stmt_code)
1389 : 1412414 : && !(first_stmt_code.is_tree_code ()
1390 : 1291072 : && rhs_code.is_tree_code ()
1391 : 1200244 : && (TREE_CODE_CLASS (tree_code (first_stmt_code))
1392 : : == tcc_comparison)
1393 : 141471 : && (swap_tree_comparison (tree_code (first_stmt_code))
1394 : 141471 : == tree_code (rhs_code))
1395 : : && (first_reduc_idx == -1
1396 : 0 : || REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
1397 : : || (ldst_p
1398 : 4757322 : && (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1399 : 2378661 : != STMT_VINFO_GROUPED_ACCESS (first_stmt_info)))
1400 : : || (ldst_p
1401 : 2337637 : && (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1402 : 2337637 : != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info)))
1403 : 10122559 : || first_stmt_ldst_p != ldst_p
1404 : 10122447 : || (ldst_p && first_stmt_ldst_masklen_p != ldst_masklen_p)
1405 : 21614217 : || first_stmt_phi_p != phi_p)
1406 : : {
1407 : 1369323 : if (dump_enabled_p ())
1408 : : {
1409 : 2826 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1410 : : "Build SLP failed: different operation "
1411 : : "in stmt %G", stmt);
1412 : 2826 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1413 : : "original stmt %G", first_stmt_info->stmt);
1414 : : }
1415 : : /* Mismatch. */
1416 : 1369323 : continue;
1417 : : }
1418 : :
1419 : 10124232 : if (!ldst_p
1420 : 7784910 : && first_stmt_code == BIT_FIELD_REF
1421 : 10127120 : && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
1422 : 4673 : != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
1423 : : {
1424 : 1785 : if (dump_enabled_p ())
1425 : 36 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1426 : : "Build SLP failed: different BIT_FIELD_REF "
1427 : : "arguments in %G", stmt);
1428 : : /* Mismatch. */
1429 : 1785 : continue;
1430 : : }
1431 : :
1432 : 10120662 : if (call_stmt
1433 : 20888 : && first_stmt_code != CFN_MASK_LOAD
1434 : 10141485 : && first_stmt_code != CFN_MASK_STORE)
1435 : : {
1436 : 20761 : if (!is_a <gcall *> (stmts[0]->stmt)
1437 : 20761 : || !compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
1438 : : call_stmt, true))
1439 : : {
1440 : 5210 : if (dump_enabled_p ())
1441 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1442 : : "Build SLP failed: different calls in %G",
1443 : : stmt);
1444 : : /* Mismatch. */
1445 : 5210 : continue;
1446 : : }
1447 : : }
1448 : :
1449 : 9935930 : if ((phi_p || gimple_could_trap_p (stmt_info->stmt))
1450 : 10738964 : && (gimple_bb (first_stmt_info->stmt)
1451 : 803034 : != gimple_bb (stmt_info->stmt)))
1452 : : {
1453 : 27382 : if (dump_enabled_p ())
1454 : 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1455 : : "Build SLP failed: different BB for PHI "
1456 : : "or possibly trapping operation in %G", stmt);
1457 : : /* Mismatch. */
1458 : 27382 : continue;
1459 : : }
1460 : :
1461 : 10088070 : if (need_same_oprnds)
1462 : : {
1463 : 52955 : tree other_op1 = gimple_arg (stmt, 1);
1464 : 52955 : if (!operand_equal_p (first_op1, other_op1, 0))
1465 : : {
1466 : 7138 : if (dump_enabled_p ())
1467 : 123 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1468 : : "Build SLP failed: different shift "
1469 : : "arguments in %G", stmt);
1470 : : /* Mismatch. */
1471 : 7138 : continue;
1472 : : }
1473 : : }
1474 : :
1475 : 10081554 : if (first_lhs
1476 : 10080932 : && lhs
1477 : 10080932 : && !types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (first_lhs)))
1478 : : {
1479 : 622 : if (dump_enabled_p ())
1480 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1481 : : "Build SLP failed: different vector type "
1482 : : "in %G", stmt);
1483 : : /* Mismatch. */
1484 : 622 : continue;
1485 : : }
1486 : : }
1487 : :
1488 : : /* Grouped store or load. */
1489 : 15078192 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1490 : : {
1491 : 3735369 : gcc_assert (ldst_p);
1492 : 3735369 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1493 : : {
1494 : : /* Store. */
1495 : 2965306 : gcc_assert (rhs_code == CFN_MASK_STORE
1496 : : || REFERENCE_CLASS_P (lhs)
1497 : : || DECL_P (lhs));
1498 : : }
1499 : : else
1500 : : {
1501 : : /* Load. */
1502 : 770063 : first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
1503 : 770063 : if (prev_first_load)
1504 : : {
1505 : : /* Check that there are no loads from different interleaving
1506 : : chains in the same node. */
1507 : 333432 : if (prev_first_load != first_load)
1508 : : {
1509 : 31859 : if (dump_enabled_p ())
1510 : 1840 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1511 : : vect_location,
1512 : : "Build SLP failed: different "
1513 : : "interleaving chains in one node %G",
1514 : : stmt);
1515 : : /* Mismatch. */
1516 : 31859 : continue;
1517 : : }
1518 : : }
1519 : : else
1520 : : prev_first_load = first_load;
1521 : : }
1522 : : }
1523 : : /* Non-grouped store or load. */
1524 : 11342823 : else if (ldst_p)
1525 : : {
1526 : 630078 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
1527 : 420762 : && rhs_code != CFN_GATHER_LOAD
1528 : : && rhs_code != CFN_MASK_GATHER_LOAD
1529 : : && rhs_code != CFN_MASK_LEN_GATHER_LOAD
1530 : : && rhs_code != CFN_SCATTER_STORE
1531 : : && rhs_code != CFN_MASK_SCATTER_STORE
1532 : : && rhs_code != CFN_MASK_LEN_SCATTER_STORE
1533 : 420762 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1534 : : /* Not grouped loads are handled as externals for BB
1535 : : vectorization. For loop vectorization we can handle
1536 : : splats the same we handle single element interleaving. */
1537 : 1038548 : && (is_a <bb_vec_info> (vinfo)
1538 : 408470 : || stmt_info != first_stmt_info))
1539 : : {
1540 : : /* Not grouped load. */
1541 : 13001 : if (dump_enabled_p ())
1542 : 119 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1543 : : "Build SLP failed: not grouped load %G", stmt);
1544 : :
1545 : 13001 : if (i != 0)
1546 : 13001 : continue;
1547 : : /* Fatal mismatch. */
1548 : 0 : matches[0] = false;
1549 : 0 : return false;
1550 : : }
1551 : : }
1552 : : /* Not memory operation. */
1553 : : else
1554 : : {
1555 : 10712745 : if (!phi_p
1556 : 9876783 : && rhs_code.is_tree_code ()
1557 : 9835769 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_binary
1558 : 1429285 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_unary
1559 : 911423 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_expression
1560 : 862940 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_comparison
1561 : 58895 : && rhs_code != VIEW_CONVERT_EXPR
1562 : : && rhs_code != CALL_EXPR
1563 : : && rhs_code != BIT_FIELD_REF
1564 : 10712745 : && rhs_code != SSA_NAME)
1565 : : {
1566 : 17885 : if (dump_enabled_p ())
1567 : 17 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1568 : : "Build SLP failed: operation unsupported %G",
1569 : : stmt);
1570 : 17885 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1571 : 0 : continue;
1572 : : /* Fatal mismatch. */
1573 : 17885 : matches[0] = false;
1574 : 17885 : return false;
1575 : : }
1576 : :
1577 : 10694860 : if (rhs_code == COND_EXPR)
1578 : : {
1579 : 46024 : tree cond_expr = gimple_assign_rhs1 (stmt);
1580 : 46024 : enum tree_code cond_code = TREE_CODE (cond_expr);
1581 : 46024 : enum tree_code swap_code = ERROR_MARK;
1582 : 46024 : enum tree_code invert_code = ERROR_MARK;
1583 : :
1584 : 46024 : if (i == 0)
1585 : 37538 : first_cond_code = TREE_CODE (cond_expr);
1586 : 8486 : else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1587 : : {
1588 : 0 : bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
1589 : 0 : swap_code = swap_tree_comparison (cond_code);
1590 : 0 : invert_code = invert_tree_comparison (cond_code, honor_nans);
1591 : : }
1592 : :
1593 : 46024 : if (first_cond_code == cond_code)
1594 : : ;
1595 : : /* Isomorphic can be achieved by swapping. */
1596 : 0 : else if (first_cond_code == swap_code)
1597 : 0 : swap[i] = 1;
1598 : : /* Isomorphic can be achieved by inverting. */
1599 : 0 : else if (first_cond_code == invert_code)
1600 : 0 : swap[i] = 2;
1601 : : else
1602 : : {
1603 : 0 : if (dump_enabled_p ())
1604 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1605 : : "Build SLP failed: different"
1606 : : " operation %G", stmt);
1607 : : /* Mismatch. */
1608 : 0 : continue;
1609 : : }
1610 : : }
1611 : :
1612 : 10694860 : if (i != 0
1613 : 7744042 : && first_stmt_code != rhs_code
1614 : 57874 : && first_stmt_code.is_tree_code ()
1615 : 57872 : && rhs_code.is_tree_code ()
1616 : 57872 : && TREE_CODE_CLASS ((tree_code)first_stmt_code) == tcc_comparison
1617 : 10736975 : && (swap_tree_comparison ((tree_code)first_stmt_code)
1618 : 42115 : == (tree_code)rhs_code))
1619 : 42115 : swap[i] = 1;
1620 : : }
1621 : :
1622 : 15015447 : matches[i] = true;
1623 : : }
1624 : :
1625 : 20026067 : for (i = 0; i < group_size; ++i)
1626 : 15696598 : if (!matches[i])
1627 : : return false;
1628 : :
1629 : : /* If we allowed a two-operation SLP node verify the target can cope
1630 : : with the permute we are going to use. */
1631 : 4329469 : if (alt_stmt_code != ERROR_MARK
1632 : 4329469 : && (!alt_stmt_code.is_tree_code ()
1633 : 51249 : || (TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_reference
1634 : 51249 : && TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_comparison)))
1635 : : {
1636 : 10011 : *two_operators = true;
1637 : : }
1638 : :
1639 : 4329469 : if (maybe_soft_fail)
1640 : : {
1641 : 140272 : unsigned HOST_WIDE_INT const_nunits;
1642 : 140272 : if (!TYPE_VECTOR_SUBPARTS
1643 : 140272 : (soft_fail_nunits_vectype).is_constant (&const_nunits)
1644 : 140272 : || const_nunits > group_size)
1645 : 0 : matches[0] = false;
1646 : : else
1647 : : {
1648 : : /* With constant vector elements simulate a mismatch at the
1649 : : point we need to split. */
1650 : 140272 : unsigned tail = group_size & (const_nunits - 1);
1651 : 140272 : memset (&matches[group_size - tail], 0, sizeof (bool) * tail);
1652 : : }
1653 : 140272 : return false;
1654 : : }
1655 : :
1656 : : return true;
1657 : : }
1658 : :
1659 : : /* Traits for the hash_set to record failed SLP builds for a stmt set.
1660 : : Note we never remove apart from at destruction time so we do not
1661 : : need a special value for deleted that differs from empty. */
1662 : : struct bst_traits
1663 : : {
1664 : : typedef vec <stmt_vec_info> value_type;
1665 : : typedef vec <stmt_vec_info> compare_type;
1666 : : static inline hashval_t hash (value_type);
1667 : : static inline bool equal (value_type existing, value_type candidate);
1668 : 489137701 : static inline bool is_empty (value_type x) { return !x.exists (); }
1669 : 114008461 : static inline bool is_deleted (value_type x) { return !x.exists (); }
1670 : : static const bool empty_zero_p = true;
1671 : 0 : static inline void mark_empty (value_type &x) { x.release (); }
1672 : : static inline void mark_deleted (value_type &x) { x.release (); }
1673 : 9167312 : static inline void remove (value_type &x) { x.release (); }
1674 : : };
1675 : : inline hashval_t
1676 : 98379504 : bst_traits::hash (value_type x)
1677 : : {
1678 : 98379504 : inchash::hash h;
1679 : 424898339 : for (unsigned i = 0; i < x.length (); ++i)
1680 : 326518835 : h.add_int (x[i] ? gimple_uid (x[i]->stmt) : -1);
1681 : 98379504 : return h.end ();
1682 : : }
1683 : : inline bool
1684 : 88363414 : bst_traits::equal (value_type existing, value_type candidate)
1685 : : {
1686 : 265090242 : if (existing.length () != candidate.length ())
1687 : : return false;
1688 : 90571433 : for (unsigned i = 0; i < existing.length (); ++i)
1689 : 85820641 : if (existing[i] != candidate[i])
1690 : : return false;
1691 : : return true;
1692 : : }
1693 : :
1694 : : typedef hash_map <vec <stmt_vec_info>, slp_tree,
1695 : : simple_hashmap_traits <bst_traits, slp_tree> >
1696 : : scalar_stmts_to_slp_tree_map_t;
1697 : :
1698 : : /* Release BST_MAP. */
1699 : :
1700 : : static void
1701 : 1631829 : release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map)
1702 : : {
1703 : : /* The map keeps a reference on SLP nodes built, release that. */
1704 : 10799141 : for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin ();
1705 : 19966453 : it != bst_map->end (); ++it)
1706 : 9167312 : if ((*it).second)
1707 : 9167312 : vect_free_slp_tree ((*it).second);
1708 : 1631829 : delete bst_map;
1709 : 1631829 : }
1710 : :
1711 : : /* ??? This was std::pair<std::pair<tree_code, vect_def_type>, tree>
1712 : : but then vec::insert does memmove and that's not compatible with
1713 : : std::pair. */
1714 : : struct chain_op_t
1715 : : {
1716 : 3604290 : chain_op_t (tree_code code_, vect_def_type dt_, tree op_)
1717 : 3604290 : : code (code_), dt (dt_), op (op_) {}
1718 : : tree_code code;
1719 : : vect_def_type dt;
1720 : : tree op;
1721 : : };
1722 : :
1723 : : /* Comparator for sorting associatable chains. */
1724 : :
1725 : : static int
1726 : 8510057 : dt_sort_cmp (const void *op1_, const void *op2_, void *)
1727 : : {
1728 : 8510057 : auto *op1 = (const chain_op_t *) op1_;
1729 : 8510057 : auto *op2 = (const chain_op_t *) op2_;
1730 : 8510057 : if (op1->dt != op2->dt)
1731 : 1039053 : return (int)op1->dt - (int)op2->dt;
1732 : 7471004 : return (int)op1->code - (int)op2->code;
1733 : : }
1734 : :
1735 : : /* Linearize the associatable expression chain at START with the
1736 : : associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
1737 : : filling CHAIN with the result and using WORKLIST as intermediate storage.
1738 : : CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
1739 : : or MINUS_EXPR. *CHAIN_STMTS if not NULL is filled with all computation
1740 : : stmts, starting with START. */
1741 : :
1742 : : static void
1743 : 1614124 : vect_slp_linearize_chain (vec_info *vinfo,
1744 : : vec<std::pair<tree_code, gimple *> > &worklist,
1745 : : vec<chain_op_t> &chain,
1746 : : enum tree_code code, gimple *start,
1747 : : gimple *&code_stmt, gimple *&alt_code_stmt,
1748 : : vec<gimple *> *chain_stmts)
1749 : : {
1750 : : /* For each lane linearize the addition/subtraction (or other
1751 : : uniform associatable operation) expression tree. */
1752 : 1614124 : worklist.safe_push (std::make_pair (code, start));
1753 : 3604290 : while (!worklist.is_empty ())
1754 : : {
1755 : 1990166 : auto entry = worklist.pop ();
1756 : 1990166 : gassign *stmt = as_a <gassign *> (entry.second);
1757 : 1990166 : enum tree_code in_code = entry.first;
1758 : 3980332 : enum tree_code this_code = gimple_assign_rhs_code (stmt);
1759 : : /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE. */
1760 : 1990166 : if (!code_stmt
1761 : 1990166 : && gimple_assign_rhs_code (stmt) == code)
1762 : 1349798 : code_stmt = stmt;
1763 : 640368 : else if (!alt_code_stmt
1764 : 640368 : && gimple_assign_rhs_code (stmt) == MINUS_EXPR)
1765 : 339345 : alt_code_stmt = stmt;
1766 : 1990166 : if (chain_stmts)
1767 : 1981551 : chain_stmts->safe_push (stmt);
1768 : 5970498 : for (unsigned opnum = 1; opnum <= 2; ++opnum)
1769 : : {
1770 : 3980332 : tree op = gimple_op (stmt, opnum);
1771 : 3980332 : vect_def_type dt;
1772 : 3980332 : stmt_vec_info def_stmt_info;
1773 : 3980332 : bool res = vect_is_simple_use (op, vinfo, &dt, &def_stmt_info);
1774 : 3980332 : gcc_assert (res);
1775 : 3980332 : if (dt == vect_internal_def
1776 : 3980332 : && is_pattern_stmt_p (def_stmt_info))
1777 : 988 : op = gimple_get_lhs (def_stmt_info->stmt);
1778 : 3980332 : gimple *use_stmt;
1779 : 3980332 : use_operand_p use_p;
1780 : 3980332 : if (dt == vect_internal_def
1781 : 3720699 : && single_imm_use (op, &use_p, &use_stmt)
1782 : 2320084 : && is_gimple_assign (def_stmt_info->stmt)
1783 : 6108703 : && (gimple_assign_rhs_code (def_stmt_info->stmt) == code
1784 : 1778411 : || (code == PLUS_EXPR
1785 : 867879 : && (gimple_assign_rhs_code (def_stmt_info->stmt)
1786 : : == MINUS_EXPR))))
1787 : : {
1788 : 376042 : tree_code op_def_code = this_code;
1789 : 376042 : if (op_def_code == MINUS_EXPR && opnum == 1)
1790 : 55858 : op_def_code = PLUS_EXPR;
1791 : 376042 : if (in_code == MINUS_EXPR)
1792 : 193 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1793 : 376042 : worklist.safe_push (std::make_pair (op_def_code,
1794 : 376042 : def_stmt_info->stmt));
1795 : : }
1796 : : else
1797 : : {
1798 : 3604290 : tree_code op_def_code = this_code;
1799 : 3604290 : if (op_def_code == MINUS_EXPR && opnum == 1)
1800 : 287786 : op_def_code = PLUS_EXPR;
1801 : 3604290 : if (in_code == MINUS_EXPR)
1802 : 4263 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1803 : 3604290 : chain.safe_push (chain_op_t (op_def_code, dt, op));
1804 : : }
1805 : : }
1806 : : }
1807 : 1614124 : }
1808 : :
1809 : : static slp_tree
1810 : : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1811 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1812 : : poly_uint64 *max_nunits,
1813 : : bool *matches, unsigned *limit, unsigned *tree_size,
1814 : : scalar_stmts_to_slp_tree_map_t *bst_map);
1815 : :
1816 : : static slp_tree
1817 : 6170366 : vect_build_slp_tree (vec_info *vinfo,
1818 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1819 : : poly_uint64 *max_nunits,
1820 : : bool *matches, unsigned *limit, unsigned *tree_size,
1821 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1822 : : {
1823 : 6170366 : if (slp_tree *leader = bst_map->get (stmts))
1824 : : {
1825 : 471450 : if (dump_enabled_p ())
1826 : 17165 : dump_printf_loc (MSG_NOTE, vect_location, "re-using %sSLP tree %p\n",
1827 : 17165 : !(*leader)->failed ? "" : "failed ",
1828 : : (void *) *leader);
1829 : 471450 : if (!(*leader)->failed)
1830 : : {
1831 : 425577 : SLP_TREE_REF_COUNT (*leader)++;
1832 : 425577 : vect_update_max_nunits (max_nunits, (*leader)->max_nunits);
1833 : 425577 : stmts.release ();
1834 : 425577 : return *leader;
1835 : : }
1836 : 45873 : memcpy (matches, (*leader)->failed, sizeof (bool) * group_size);
1837 : 45873 : return NULL;
1838 : : }
1839 : :
1840 : : /* Single-lane SLP doesn't have the chance of run-away, do not account
1841 : : it to the limit. */
1842 : 5698916 : if (stmts.length () > 1)
1843 : : {
1844 : 3077689 : if (*limit == 0)
1845 : : {
1846 : 1501 : if (dump_enabled_p ())
1847 : 12 : dump_printf_loc (MSG_NOTE, vect_location,
1848 : : "SLP discovery limit exceeded\n");
1849 : 1501 : memset (matches, 0, sizeof (bool) * group_size);
1850 : 1501 : return NULL;
1851 : : }
1852 : 3076188 : --*limit;
1853 : : }
1854 : :
1855 : : /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
1856 : : so we can pick up backedge destinations during discovery. */
1857 : 5697415 : slp_tree res = new _slp_tree;
1858 : 5697415 : SLP_TREE_DEF_TYPE (res) = vect_internal_def;
1859 : 5697415 : SLP_TREE_SCALAR_STMTS (res) = stmts;
1860 : 5697415 : bst_map->put (stmts.copy (), res);
1861 : :
1862 : 5697415 : if (dump_enabled_p ())
1863 : 145574 : dump_printf_loc (MSG_NOTE, vect_location,
1864 : : "starting SLP discovery for node %p\n", (void *) res);
1865 : :
1866 : 5697415 : poly_uint64 this_max_nunits = 1;
1867 : 5697415 : slp_tree res_ = vect_build_slp_tree_2 (vinfo, res, stmts, group_size,
1868 : : &this_max_nunits,
1869 : : matches, limit, tree_size, bst_map);
1870 : 5697415 : if (!res_)
1871 : : {
1872 : 1917986 : if (dump_enabled_p ())
1873 : 7999 : dump_printf_loc (MSG_NOTE, vect_location,
1874 : : "SLP discovery for node %p failed\n", (void *) res);
1875 : : /* Mark the node invalid so we can detect those when still in use
1876 : : as backedge destinations. */
1877 : 1917986 : SLP_TREE_SCALAR_STMTS (res) = vNULL;
1878 : 1917986 : SLP_TREE_DEF_TYPE (res) = vect_uninitialized_def;
1879 : 1917986 : res->failed = XNEWVEC (bool, group_size);
1880 : 1917986 : if (flag_checking)
1881 : : {
1882 : : unsigned i;
1883 : 3375935 : for (i = 0; i < group_size; ++i)
1884 : 3375935 : if (!matches[i])
1885 : : break;
1886 : 1917986 : gcc_assert (i < group_size);
1887 : : }
1888 : 1917986 : memcpy (res->failed, matches, sizeof (bool) * group_size);
1889 : : }
1890 : : else
1891 : : {
1892 : 3779429 : if (dump_enabled_p ())
1893 : 137575 : dump_printf_loc (MSG_NOTE, vect_location,
1894 : : "SLP discovery for node %p succeeded\n",
1895 : : (void *) res);
1896 : 3779429 : gcc_assert (res_ == res);
1897 : 3779429 : res->max_nunits = this_max_nunits;
1898 : 3779429 : vect_update_max_nunits (max_nunits, this_max_nunits);
1899 : : /* Keep a reference for the bst_map use. */
1900 : 3779429 : SLP_TREE_REF_COUNT (res)++;
1901 : : }
1902 : : return res_;
1903 : : }
1904 : :
1905 : : /* Helper for building an associated SLP node chain. */
1906 : :
1907 : : static void
1908 : 122 : vect_slp_build_two_operator_nodes (slp_tree perm, tree vectype,
1909 : : slp_tree op0, slp_tree op1,
1910 : : stmt_vec_info oper1, stmt_vec_info oper2,
1911 : : vec<std::pair<unsigned, unsigned> > lperm)
1912 : : {
1913 : 122 : unsigned group_size = SLP_TREE_LANES (op1);
1914 : :
1915 : 122 : slp_tree child1 = new _slp_tree;
1916 : 122 : SLP_TREE_DEF_TYPE (child1) = vect_internal_def;
1917 : 122 : SLP_TREE_VECTYPE (child1) = vectype;
1918 : 122 : SLP_TREE_LANES (child1) = group_size;
1919 : 122 : SLP_TREE_CHILDREN (child1).create (2);
1920 : 122 : SLP_TREE_CHILDREN (child1).quick_push (op0);
1921 : 122 : SLP_TREE_CHILDREN (child1).quick_push (op1);
1922 : 122 : SLP_TREE_REPRESENTATIVE (child1) = oper1;
1923 : :
1924 : 122 : slp_tree child2 = new _slp_tree;
1925 : 122 : SLP_TREE_DEF_TYPE (child2) = vect_internal_def;
1926 : 122 : SLP_TREE_VECTYPE (child2) = vectype;
1927 : 122 : SLP_TREE_LANES (child2) = group_size;
1928 : 122 : SLP_TREE_CHILDREN (child2).create (2);
1929 : 122 : SLP_TREE_CHILDREN (child2).quick_push (op0);
1930 : 122 : SLP_TREE_REF_COUNT (op0)++;
1931 : 122 : SLP_TREE_CHILDREN (child2).quick_push (op1);
1932 : 122 : SLP_TREE_REF_COUNT (op1)++;
1933 : 122 : SLP_TREE_REPRESENTATIVE (child2) = oper2;
1934 : :
1935 : 122 : SLP_TREE_DEF_TYPE (perm) = vect_internal_def;
1936 : 122 : SLP_TREE_CODE (perm) = VEC_PERM_EXPR;
1937 : 122 : SLP_TREE_VECTYPE (perm) = vectype;
1938 : 122 : SLP_TREE_LANES (perm) = group_size;
1939 : : /* ??? We should set this NULL but that's not expected. */
1940 : 122 : SLP_TREE_REPRESENTATIVE (perm) = oper1;
1941 : 122 : SLP_TREE_LANE_PERMUTATION (perm) = lperm;
1942 : 122 : SLP_TREE_CHILDREN (perm).quick_push (child1);
1943 : 122 : SLP_TREE_CHILDREN (perm).quick_push (child2);
1944 : 122 : }
1945 : :
1946 : : /* Recursively build an SLP tree starting from NODE.
1947 : : Fail (and return a value not equal to zero) if def-stmts are not
1948 : : isomorphic, require data permutation or are of unsupported types of
1949 : : operation. Otherwise, return 0.
1950 : : The value returned is the depth in the SLP tree where a mismatch
1951 : : was found. */
1952 : :
1953 : : static slp_tree
1954 : 5697415 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1955 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1956 : : poly_uint64 *max_nunits,
1957 : : bool *matches, unsigned *limit, unsigned *tree_size,
1958 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1959 : : {
1960 : 5697415 : unsigned nops, i, this_tree_size = 0;
1961 : 5697415 : poly_uint64 this_max_nunits = *max_nunits;
1962 : :
1963 : 5697415 : matches[0] = false;
1964 : :
1965 : 5697415 : stmt_vec_info stmt_info = stmts[0];
1966 : 5697415 : if (!is_a<gcall *> (stmt_info->stmt)
1967 : : && !is_a<gassign *> (stmt_info->stmt)
1968 : : && !is_a<gphi *> (stmt_info->stmt))
1969 : : return NULL;
1970 : :
1971 : 5697344 : nops = gimple_num_args (stmt_info->stmt);
1972 : 5697344 : if (const int *map = vect_get_operand_map (stmt_info->stmt,
1973 : 5697344 : STMT_VINFO_GATHER_SCATTER_P
1974 : : (stmt_info)))
1975 : 23357 : nops = map[0];
1976 : :
1977 : : /* If the SLP node is a PHI (induction or reduction), terminate
1978 : : the recursion. */
1979 : 5697344 : bool *skip_args = XALLOCAVEC (bool, nops);
1980 : 5697344 : memset (skip_args, 0, sizeof (bool) * nops);
1981 : 5697344 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
1982 : 2740248 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
1983 : : {
1984 : 462916 : tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
1985 : 462916 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
1986 : : group_size);
1987 : 462916 : if (!vect_record_max_nunits (vinfo, stmt_info, group_size, vectype,
1988 : : max_nunits))
1989 : : return NULL;
1990 : :
1991 : 458812 : vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
1992 : 458812 : if (def_type == vect_induction_def)
1993 : : {
1994 : : /* Induction PHIs are not cycles but walk the initial
1995 : : value. Only for inner loops through, for outer loops
1996 : : we need to pick up the value from the actual PHIs
1997 : : to more easily support peeling and epilogue vectorization. */
1998 : 389043 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1999 : 389043 : if (!nested_in_vect_loop_p (loop, stmt_info))
2000 : 388420 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2001 : : else
2002 : : loop = loop->inner;
2003 : 389043 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2004 : : }
2005 : 69769 : else if (def_type == vect_reduction_def
2006 : : || def_type == vect_double_reduction_def
2007 : : || def_type == vect_nested_cycle
2008 : 69769 : || def_type == vect_first_order_recurrence)
2009 : : {
2010 : : /* Else def types have to match. */
2011 : : stmt_vec_info other_info;
2012 : : bool all_same = true;
2013 : 145794 : FOR_EACH_VEC_ELT (stmts, i, other_info)
2014 : : {
2015 : 77069 : if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
2016 : 1711461 : return NULL;
2017 : 77067 : if (other_info != stmt_info)
2018 : 4673 : all_same = false;
2019 : : }
2020 : 68725 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2021 : : /* Reduction initial values are not explicitely represented. */
2022 : 68725 : if (def_type != vect_first_order_recurrence
2023 : 68725 : && gimple_bb (stmt_info->stmt) == loop->header)
2024 : 65945 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2025 : : /* Reduction chain backedge defs are filled manually.
2026 : : ??? Need a better way to identify a SLP reduction chain PHI.
2027 : : Or a better overall way to SLP match those. */
2028 : 68725 : if (stmts.length () > 1
2029 : 68725 : && all_same && def_type == vect_reduction_def)
2030 : 2367 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2031 : : }
2032 : 1042 : else if (def_type != vect_internal_def)
2033 : : return NULL;
2034 : : }
2035 : :
2036 : :
2037 : 5693238 : bool two_operators = false;
2038 : 5693238 : unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
2039 : 5693238 : tree vectype = NULL_TREE;
2040 : 5693238 : if (!vect_build_slp_tree_1 (vinfo, swap, stmts, group_size,
2041 : : &this_max_nunits, matches, &two_operators,
2042 : : &vectype))
2043 : : return NULL;
2044 : :
2045 : : /* If the SLP node is a load, terminate the recursion unless masked. */
2046 : 4189197 : if (STMT_VINFO_DATA_REF (stmt_info)
2047 : 1830149 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2048 : : {
2049 : 779619 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2050 : : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
2051 : : else
2052 : : {
2053 : 767540 : *max_nunits = this_max_nunits;
2054 : 767540 : (*tree_size)++;
2055 : 767540 : node = vect_create_new_slp_node (node, stmts, 0);
2056 : 767540 : SLP_TREE_VECTYPE (node) = vectype;
2057 : : /* And compute the load permutation. Whether it is actually
2058 : : a permutation depends on the unrolling factor which is
2059 : : decided later. */
2060 : 767540 : vec<unsigned> load_permutation;
2061 : 767540 : int j;
2062 : 767540 : stmt_vec_info load_info;
2063 : 767540 : load_permutation.create (group_size);
2064 : 767540 : stmt_vec_info first_stmt_info
2065 : 767540 : = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2066 : 767540 : ? DR_GROUP_FIRST_ELEMENT (stmt_info) : stmt_info;
2067 : 767540 : bool any_permute = false;
2068 : 1879024 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
2069 : : {
2070 : 1111484 : int load_place;
2071 : 1111484 : if (! load_info)
2072 : : {
2073 : 50919 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2074 : : load_place = j;
2075 : : else
2076 : : load_place = 0;
2077 : : }
2078 : 1060565 : else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2079 : 669766 : load_place = vect_get_place_in_interleaving_chain
2080 : 669766 : (load_info, first_stmt_info);
2081 : : else
2082 : : load_place = 0;
2083 : 720685 : gcc_assert (load_place != -1);
2084 : 1111484 : any_permute |= load_place != j;
2085 : 1111484 : load_permutation.quick_push (load_place);
2086 : : }
2087 : :
2088 : 767540 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
2089 : : {
2090 : 2078 : gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD));
2091 : 2078 : bool has_gaps = false;
2092 : 2078 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2093 : 119 : for (stmt_vec_info si = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
2094 : 196 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2095 : 77 : if (DR_GROUP_GAP (si) != 1)
2096 : 20 : has_gaps = true;
2097 : : /* We cannot handle permuted masked loads directly, see
2098 : : PR114375. We cannot handle strided masked loads or masked
2099 : : loads with gaps unless the mask is uniform. */
2100 : 2078 : if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
2101 : 119 : && (DR_GROUP_GAP (first_stmt_info) != 0
2102 : 59 : || (has_gaps
2103 : 20 : && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))))
2104 : 4076 : || STMT_VINFO_STRIDED_P (stmt_info))
2105 : : {
2106 : 93 : load_permutation.release ();
2107 : 93 : matches[0] = false;
2108 : 765579 : return NULL;
2109 : : }
2110 : :
2111 : : /* For permuted masked loads do an unpermuted masked load of
2112 : : the whole group followed by a SLP permute node. */
2113 : 1985 : if (any_permute
2114 : 1985 : || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
2115 : 25 : && DR_GROUP_SIZE (first_stmt_info) != group_size))
2116 : : {
2117 : : /* Discover the whole unpermuted load. */
2118 : 24 : vec<stmt_vec_info> stmts2;
2119 : 24 : unsigned dr_group_size = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2120 : 38 : ? DR_GROUP_SIZE (first_stmt_info) : 1;
2121 : 24 : stmts2.create (dr_group_size);
2122 : 24 : stmts2.quick_grow_cleared (dr_group_size);
2123 : 24 : unsigned i = 0;
2124 : 24 : for (stmt_vec_info si = first_stmt_info;
2125 : 74 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2126 : : {
2127 : 50 : if (si != first_stmt_info)
2128 : 26 : for (unsigned k = 1; k < DR_GROUP_GAP (si); ++k)
2129 : 0 : stmts2[i++] = NULL;
2130 : 50 : stmts2[i++] = si;
2131 : : }
2132 : 24 : bool *matches2 = XALLOCAVEC (bool, dr_group_size);
2133 : 24 : slp_tree unperm_load
2134 : 24 : = vect_build_slp_tree (vinfo, stmts2, dr_group_size,
2135 : : &this_max_nunits, matches2, limit,
2136 : 24 : &this_tree_size, bst_map);
2137 : : /* When we are able to do the full masked load emit that
2138 : : followed by 'node' being the desired final permutation. */
2139 : 24 : if (unperm_load)
2140 : : {
2141 : 16 : gcc_assert
2142 : : (!SLP_TREE_LOAD_PERMUTATION (unperm_load).exists ());
2143 : 16 : lane_permutation_t lperm;
2144 : 16 : lperm.create (group_size);
2145 : 56 : for (unsigned j = 0; j < load_permutation.length (); ++j)
2146 : 40 : lperm.quick_push
2147 : 40 : (std::make_pair (0, load_permutation[j]));
2148 : 16 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2149 : 16 : SLP_TREE_CHILDREN (node).safe_push (unperm_load);
2150 : 16 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2151 : 16 : load_permutation.release ();
2152 : 16 : return node;
2153 : : }
2154 : 8 : stmts2.release ();
2155 : 8 : load_permutation.release ();
2156 : 8 : matches[0] = false;
2157 : 8 : return NULL;
2158 : : }
2159 : 1961 : load_permutation.release ();
2160 : : }
2161 : : else
2162 : : {
2163 : 765462 : if (!any_permute
2164 : 663405 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2165 : 1044720 : && group_size == DR_GROUP_SIZE (first_stmt_info))
2166 : 131938 : load_permutation.release ();
2167 : 765462 : SLP_TREE_LOAD_PERMUTATION (node) = load_permutation;
2168 : 765462 : return node;
2169 : : }
2170 : : }
2171 : : }
2172 : 3409578 : else if (gimple_assign_single_p (stmt_info->stmt)
2173 : 2106392 : && !gimple_vuse (stmt_info->stmt)
2174 : 3416666 : && gimple_assign_rhs_code (stmt_info->stmt) == BIT_FIELD_REF)
2175 : : {
2176 : : /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
2177 : : the same SSA name vector of a compatible type to vectype. */
2178 : 1984 : vec<std::pair<unsigned, unsigned> > lperm = vNULL;
2179 : 1984 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
2180 : 1984 : stmt_vec_info estmt_info;
2181 : 6532 : FOR_EACH_VEC_ELT (stmts, i, estmt_info)
2182 : : {
2183 : 4551 : gassign *estmt = as_a <gassign *> (estmt_info->stmt);
2184 : 4551 : tree bfref = gimple_assign_rhs1 (estmt);
2185 : 4551 : HOST_WIDE_INT lane;
2186 : 4551 : if (!known_eq (bit_field_size (bfref),
2187 : : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
2188 : 9099 : || !constant_multiple_p (bit_field_offset (bfref),
2189 : 4548 : bit_field_size (bfref), &lane))
2190 : : {
2191 : 3 : lperm.release ();
2192 : 3 : matches[0] = false;
2193 : 3 : return NULL;
2194 : : }
2195 : 4548 : lperm.safe_push (std::make_pair (0, (unsigned)lane));
2196 : : }
2197 : 1981 : slp_tree vnode = vect_create_new_slp_node (vNULL);
2198 : 1981 : if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
2199 : : /* ??? We record vectype here but we hide eventually necessary
2200 : : punning and instead rely on code generation to materialize
2201 : : VIEW_CONVERT_EXPRs as necessary. We instead should make
2202 : : this explicit somehow. */
2203 : 646 : SLP_TREE_VECTYPE (vnode) = vectype;
2204 : : else
2205 : : {
2206 : : /* For different size but compatible elements we can still
2207 : : use VEC_PERM_EXPR without punning. */
2208 : 1335 : gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
2209 : : && types_compatible_p (TREE_TYPE (vectype),
2210 : : TREE_TYPE (TREE_TYPE (vec))));
2211 : 1335 : SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
2212 : : }
2213 : 1981 : auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
2214 : 1981 : unsigned HOST_WIDE_INT const_nunits;
2215 : 1981 : if (nunits.is_constant (&const_nunits))
2216 : 1981 : SLP_TREE_LANES (vnode) = const_nunits;
2217 : 1981 : SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
2218 : : /* We are always building a permutation node even if it is an identity
2219 : : permute to shield the rest of the vectorizer from the odd node
2220 : : representing an actual vector without any scalar ops.
2221 : : ??? We could hide it completely with making the permute node
2222 : : external? */
2223 : 1981 : node = vect_create_new_slp_node (node, stmts, 1);
2224 : 1981 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2225 : 1981 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2226 : 1981 : SLP_TREE_VECTYPE (node) = vectype;
2227 : 1981 : SLP_TREE_CHILDREN (node).quick_push (vnode);
2228 : 1981 : return node;
2229 : : }
2230 : : /* When discovery reaches an associatable operation see whether we can
2231 : : improve that to match up lanes in a way superior to the operand
2232 : : swapping code which at most looks at two defs.
2233 : : ??? For BB vectorization we cannot do the brute-force search
2234 : : for matching as we can succeed by means of builds from scalars
2235 : : and have no good way to "cost" one build against another. */
2236 : 3407594 : else if (is_a <loop_vec_info> (vinfo)
2237 : : /* Do not bother for single-lane SLP. */
2238 : 2058861 : && group_size > 1
2239 : : /* ??? We don't handle !vect_internal_def defs below. */
2240 : 57780 : && STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
2241 : : /* ??? Do not associate a reduction, this will wreck REDUC_IDX
2242 : : mapping as long as that exists on the stmt_info level. */
2243 : 50992 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1
2244 : 47497 : && is_gimple_assign (stmt_info->stmt)
2245 : 47272 : && (associative_tree_code (gimple_assign_rhs_code (stmt_info->stmt))
2246 : 37129 : || gimple_assign_rhs_code (stmt_info->stmt) == MINUS_EXPR)
2247 : 3419294 : && ((FLOAT_TYPE_P (vectype) && flag_associative_math)
2248 : 7632 : || (INTEGRAL_TYPE_P (TREE_TYPE (vectype))
2249 : 5787 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype)))))
2250 : : {
2251 : : /* See if we have a chain of (mixed) adds or subtracts or other
2252 : : associatable ops. */
2253 : 7018 : enum tree_code code = gimple_assign_rhs_code (stmt_info->stmt);
2254 : 7018 : if (code == MINUS_EXPR)
2255 : 659 : code = PLUS_EXPR;
2256 : 7018 : stmt_vec_info other_op_stmt_info = NULL;
2257 : 7018 : stmt_vec_info op_stmt_info = NULL;
2258 : 7018 : unsigned chain_len = 0;
2259 : 7018 : auto_vec<chain_op_t> chain;
2260 : 7018 : auto_vec<std::pair<tree_code, gimple *> > worklist;
2261 : 7018 : auto_vec<vec<chain_op_t> > chains (group_size);
2262 : 7018 : auto_vec<slp_tree, 4> children;
2263 : 7018 : bool hard_fail = true;
2264 : 7817 : for (unsigned lane = 0; lane < group_size; ++lane)
2265 : : {
2266 : 7564 : if (!stmts[lane])
2267 : : {
2268 : : /* ??? Below we require lane zero is present. */
2269 : 0 : if (lane == 0)
2270 : : {
2271 : : hard_fail = false;
2272 : 6765 : break;
2273 : : }
2274 : 0 : chains.quick_push (vNULL);
2275 : 0 : continue;
2276 : : }
2277 : : /* For each lane linearize the addition/subtraction (or other
2278 : : uniform associatable operation) expression tree. */
2279 : 7564 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
2280 : 7564 : vect_slp_linearize_chain (vinfo, worklist, chain, code,
2281 : 7564 : stmts[lane]->stmt, op_stmt, other_op_stmt,
2282 : : NULL);
2283 : 7564 : if (!op_stmt_info && op_stmt)
2284 : 6506 : op_stmt_info = vinfo->lookup_stmt (op_stmt);
2285 : 7564 : if (!other_op_stmt_info && other_op_stmt)
2286 : 695 : other_op_stmt_info = vinfo->lookup_stmt (other_op_stmt);
2287 : 7564 : if (chain.length () == 2)
2288 : : {
2289 : : /* In a chain of just two elements resort to the regular
2290 : : operand swapping scheme. Likewise if we run into a
2291 : : length mismatch process regularly as well as we did not
2292 : : process the other lanes we cannot report a good hint what
2293 : : lanes to try swapping in the parent. */
2294 : : hard_fail = false;
2295 : : break;
2296 : : }
2297 : 802 : else if (chain_len == 0)
2298 : 293 : chain_len = chain.length ();
2299 : 1018 : else if (chain.length () != chain_len)
2300 : : {
2301 : : /* ??? Here we could slip in magic to compensate with
2302 : : neutral operands. */
2303 : 3 : matches[lane] = false;
2304 : 3 : if (lane != group_size - 1)
2305 : 3 : matches[0] = false;
2306 : : break;
2307 : : }
2308 : 799 : chains.quick_push (chain.copy ());
2309 : 799 : chain.truncate (0);
2310 : : }
2311 : 14036 : if (chains.length () == group_size)
2312 : : {
2313 : : /* We cannot yet use SLP_TREE_CODE to communicate the operation. */
2314 : 253 : if (!op_stmt_info)
2315 : : {
2316 : 2 : hard_fail = false;
2317 : 2 : goto out;
2318 : : }
2319 : : /* Now we have a set of chains with the same length. */
2320 : : /* 1. pre-sort according to def_type and operation. */
2321 : 940 : for (unsigned lane = 0; lane < group_size; ++lane)
2322 : 1378 : chains[lane].stablesort (dt_sort_cmp, vinfo);
2323 : 251 : if (dump_enabled_p ())
2324 : : {
2325 : 127 : dump_printf_loc (MSG_NOTE, vect_location,
2326 : : "pre-sorted chains of %s\n",
2327 : : get_tree_code_name (code));
2328 : 541 : for (unsigned lane = 0; lane < group_size; ++lane)
2329 : : {
2330 : 414 : if (!stmts[lane])
2331 : 0 : dump_printf (MSG_NOTE, "--");
2332 : : else
2333 : 1870 : for (unsigned opnum = 0; opnum < chain_len; ++opnum)
2334 : 2912 : dump_printf (MSG_NOTE, "%s %T ",
2335 : 1456 : get_tree_code_name (chains[lane][opnum].code),
2336 : 1456 : chains[lane][opnum].op);
2337 : 414 : dump_printf (MSG_NOTE, "\n");
2338 : : }
2339 : : }
2340 : : /* 2. try to build children nodes, associating as necessary. */
2341 : : /* 2a. prepare and perform early checks to avoid eating into
2342 : : discovery limit unnecessarily. */
2343 : 251 : vect_def_type *dts = XALLOCAVEC (vect_def_type, chain_len);
2344 : 1059 : for (unsigned n = 0; n < chain_len; ++n)
2345 : : {
2346 : 808 : vect_def_type dt = chains[0][n].dt;
2347 : 808 : unsigned lane;
2348 : 3121 : for (lane = 0; lane < group_size; ++lane)
2349 : 4626 : if (stmts[lane] && chains[lane][n].dt != dt)
2350 : : {
2351 : 0 : if (dt == vect_constant_def
2352 : 0 : && chains[lane][n].dt == vect_external_def)
2353 : : dt = vect_external_def;
2354 : 0 : else if (dt == vect_external_def
2355 : 0 : && chains[lane][n].dt == vect_constant_def)
2356 : : ;
2357 : : else
2358 : : break;
2359 : : }
2360 : 808 : if (lane != group_size)
2361 : : {
2362 : 0 : if (dump_enabled_p ())
2363 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2364 : : "giving up on chain due to mismatched "
2365 : : "def types\n");
2366 : 0 : matches[lane] = false;
2367 : 0 : if (lane != group_size - 1)
2368 : 0 : matches[0] = false;
2369 : 0 : goto out;
2370 : : }
2371 : 808 : dts[n] = dt;
2372 : 808 : if (dt == vect_constant_def
2373 : 808 : || dt == vect_external_def)
2374 : : {
2375 : : /* Check whether we can build the invariant. If we can't
2376 : : we never will be able to. */
2377 : 71 : tree type = TREE_TYPE (chains[0][n].op);
2378 : 808 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ()
2379 : : && (TREE_CODE (type) == BOOLEAN_TYPE
2380 : : || !can_duplicate_and_interleave_p (vinfo, group_size,
2381 : : type)))
2382 : : {
2383 : : matches[0] = false;
2384 : : goto out;
2385 : : }
2386 : : }
2387 : 737 : else if (dt != vect_internal_def)
2388 : : {
2389 : : /* Not sure, we might need sth special.
2390 : : gcc.dg/vect/pr96854.c,
2391 : : gfortran.dg/vect/fast-math-pr37021.f90
2392 : : and gfortran.dg/vect/pr61171.f trigger. */
2393 : : /* Soft-fail for now. */
2394 : 0 : hard_fail = false;
2395 : 0 : goto out;
2396 : : }
2397 : : }
2398 : : /* 2b. do the actual build. */
2399 : 993 : for (unsigned n = 0; n < chain_len; ++n)
2400 : : {
2401 : 765 : vect_def_type dt = dts[n];
2402 : 765 : unsigned lane;
2403 : 765 : if (dt == vect_constant_def
2404 : 765 : || dt == vect_external_def)
2405 : : {
2406 : 71 : vec<tree> ops;
2407 : 71 : ops.create (group_size);
2408 : 355 : for (lane = 0; lane < group_size; ++lane)
2409 : 213 : if (stmts[lane])
2410 : 213 : ops.quick_push (chains[lane][n].op);
2411 : : else
2412 : 0 : ops.quick_push (NULL_TREE);
2413 : 71 : slp_tree child = vect_create_new_slp_node (ops);
2414 : 71 : SLP_TREE_DEF_TYPE (child) = dt;
2415 : 71 : children.safe_push (child);
2416 : : }
2417 : : else
2418 : : {
2419 : 694 : vec<stmt_vec_info> op_stmts;
2420 : 694 : op_stmts.create (group_size);
2421 : 694 : slp_tree child = NULL;
2422 : : /* Brute-force our way. We have to consider a lane
2423 : : failing after fixing an earlier fail up in the
2424 : : SLP discovery recursion. So track the current
2425 : : permute per lane. */
2426 : 694 : unsigned *perms = XALLOCAVEC (unsigned, group_size);
2427 : 694 : memset (perms, 0, sizeof (unsigned) * group_size);
2428 : 781 : do
2429 : : {
2430 : 781 : op_stmts.truncate (0);
2431 : 3816 : for (lane = 0; lane < group_size; ++lane)
2432 : 2254 : if (stmts[lane])
2433 : 2254 : op_stmts.quick_push
2434 : 2254 : (vinfo->lookup_def (chains[lane][n].op));
2435 : : else
2436 : 0 : op_stmts.quick_push (NULL);
2437 : 781 : child = vect_build_slp_tree (vinfo, op_stmts,
2438 : : group_size, &this_max_nunits,
2439 : : matches, limit,
2440 : : &this_tree_size, bst_map);
2441 : : /* ??? We're likely getting too many fatal mismatches
2442 : : here so maybe we want to ignore them (but then we
2443 : : have no idea which lanes fatally mismatched). */
2444 : 781 : if (child || !matches[0])
2445 : : break;
2446 : : /* Swap another lane we have not yet matched up into
2447 : : lanes that did not match. If we run out of
2448 : : permute possibilities for a lane terminate the
2449 : : search. */
2450 : 281 : bool term = false;
2451 : 281 : for (lane = 1; lane < group_size; ++lane)
2452 : 194 : if (!matches[lane])
2453 : : {
2454 : 166 : if (n + perms[lane] + 1 == chain_len)
2455 : : {
2456 : : term = true;
2457 : : break;
2458 : : }
2459 : 143 : if (dump_enabled_p ())
2460 : 113 : dump_printf_loc (MSG_NOTE, vect_location,
2461 : : "swapping operand %d and %d "
2462 : : "of lane %d\n",
2463 : : n, n + perms[lane] + 1, lane);
2464 : 286 : std::swap (chains[lane][n],
2465 : 143 : chains[lane][n + perms[lane] + 1]);
2466 : 143 : perms[lane]++;
2467 : : }
2468 : 110 : if (term)
2469 : : break;
2470 : : }
2471 : : while (1);
2472 : 694 : if (!child)
2473 : : {
2474 : 23 : if (dump_enabled_p ())
2475 : 18 : dump_printf_loc (MSG_NOTE, vect_location,
2476 : : "failed to match up op %d\n", n);
2477 : 23 : op_stmts.release ();
2478 : 23 : if (lane != group_size - 1)
2479 : 11 : matches[0] = false;
2480 : : else
2481 : 12 : matches[lane] = false;
2482 : 23 : goto out;
2483 : : }
2484 : 671 : if (dump_enabled_p ())
2485 : : {
2486 : 337 : dump_printf_loc (MSG_NOTE, vect_location,
2487 : : "matched up op %d to\n", n);
2488 : 337 : vect_print_slp_tree (MSG_NOTE, vect_location, child);
2489 : : }
2490 : 671 : children.safe_push (child);
2491 : : }
2492 : : }
2493 : : /* 3. build SLP nodes to combine the chain. */
2494 : 834 : for (unsigned lane = 0; lane < group_size; ++lane)
2495 : 1224 : if (stmts[lane] && chains[lane][0].code != code)
2496 : : {
2497 : : /* See if there's any alternate all-PLUS entry. */
2498 : : unsigned n;
2499 : 6 : for (n = 1; n < chain_len; ++n)
2500 : : {
2501 : 30 : for (lane = 0; lane < group_size; ++lane)
2502 : 48 : if (stmts[lane] && chains[lane][n].code != code)
2503 : : break;
2504 : 6 : if (lane == group_size)
2505 : : break;
2506 : : }
2507 : 6 : if (n != chain_len)
2508 : : {
2509 : : /* Swap that in at first position. */
2510 : 6 : std::swap (children[0], children[n]);
2511 : 30 : for (lane = 0; lane < group_size; ++lane)
2512 : 24 : if (stmts[lane])
2513 : 24 : std::swap (chains[lane][0], chains[lane][n]);
2514 : : }
2515 : : else
2516 : : {
2517 : : /* ??? When this triggers and we end up with two
2518 : : vect_constant/external_def up-front things break (ICE)
2519 : : spectacularly finding an insertion place for the
2520 : : all-constant op. We should have a fully
2521 : : vect_internal_def operand though(?) so we can swap
2522 : : that into first place and then prepend the all-zero
2523 : : constant. */
2524 : 0 : if (dump_enabled_p ())
2525 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2526 : : "inserting constant zero to compensate "
2527 : : "for (partially) negated first "
2528 : : "operand\n");
2529 : 0 : chain_len++;
2530 : 0 : for (lane = 0; lane < group_size; ++lane)
2531 : 0 : if (stmts[lane])
2532 : 0 : chains[lane].safe_insert
2533 : 0 : (0, chain_op_t (code, vect_constant_def, NULL_TREE));
2534 : 0 : vec<tree> zero_ops;
2535 : 0 : zero_ops.create (group_size);
2536 : 0 : zero_ops.quick_push (build_zero_cst (TREE_TYPE (vectype)));
2537 : 0 : for (lane = 1; lane < group_size; ++lane)
2538 : 0 : if (stmts[lane])
2539 : 0 : zero_ops.quick_push (zero_ops[0]);
2540 : : else
2541 : 0 : zero_ops.quick_push (NULL_TREE);
2542 : 0 : slp_tree zero = vect_create_new_slp_node (zero_ops);
2543 : 0 : SLP_TREE_DEF_TYPE (zero) = vect_constant_def;
2544 : 0 : children.safe_insert (0, zero);
2545 : : }
2546 : : break;
2547 : : }
2548 : 737 : for (unsigned i = 1; i < children.length (); ++i)
2549 : : {
2550 : 509 : slp_tree op0 = children[i - 1];
2551 : 509 : slp_tree op1 = children[i];
2552 : 509 : bool this_two_op = false;
2553 : 1829 : for (unsigned lane = 0; lane < group_size; ++lane)
2554 : 2884 : if (stmts[lane] && chains[lane][i].code != chains[0][i].code)
2555 : : {
2556 : : this_two_op = true;
2557 : : break;
2558 : : }
2559 : 509 : slp_tree child;
2560 : 509 : if (i == children.length () - 1)
2561 : 228 : child = vect_create_new_slp_node (node, stmts, 2);
2562 : : else
2563 : 281 : child = vect_create_new_slp_node (2, ERROR_MARK);
2564 : 509 : if (this_two_op)
2565 : : {
2566 : 122 : vec<std::pair<unsigned, unsigned> > lperm;
2567 : 122 : lperm.create (group_size);
2568 : 462 : for (unsigned lane = 0; lane < group_size; ++lane)
2569 : 680 : lperm.quick_push (std::make_pair
2570 : 340 : (chains[lane][i].code != chains[0][i].code, lane));
2571 : 244 : vect_slp_build_two_operator_nodes (child, vectype, op0, op1,
2572 : 122 : (chains[0][i].code == code
2573 : : ? op_stmt_info
2574 : : : other_op_stmt_info),
2575 : 122 : (chains[0][i].code == code
2576 : : ? other_op_stmt_info
2577 : : : op_stmt_info),
2578 : : lperm);
2579 : : }
2580 : : else
2581 : : {
2582 : 387 : SLP_TREE_DEF_TYPE (child) = vect_internal_def;
2583 : 387 : SLP_TREE_VECTYPE (child) = vectype;
2584 : 387 : SLP_TREE_LANES (child) = group_size;
2585 : 387 : SLP_TREE_CHILDREN (child).quick_push (op0);
2586 : 387 : SLP_TREE_CHILDREN (child).quick_push (op1);
2587 : 387 : SLP_TREE_REPRESENTATIVE (child)
2588 : 774 : = (chains[0][i].code == code
2589 : 387 : ? op_stmt_info : other_op_stmt_info);
2590 : : }
2591 : 509 : children[i] = child;
2592 : : }
2593 : 228 : *tree_size += this_tree_size + 1;
2594 : 228 : *max_nunits = this_max_nunits;
2595 : 1112 : while (!chains.is_empty ())
2596 : 630 : chains.pop ().release ();
2597 : : return node;
2598 : : }
2599 : 6765 : out:
2600 : 6790 : if (dump_enabled_p ())
2601 : 2711 : dump_printf_loc (MSG_NOTE, vect_location,
2602 : : "failed to line up SLP graph by re-associating "
2603 : : "operations in lanes%s\n",
2604 : : !hard_fail ? " trying regular discovery" : "");
2605 : 6795 : while (!children.is_empty ())
2606 : 5 : vect_free_slp_tree (children.pop ());
2607 : 6959 : while (!chains.is_empty ())
2608 : 169 : chains.pop ().release ();
2609 : : /* Hard-fail, otherwise we might run into quadratic processing of the
2610 : : chains starting one stmt into the chain again. */
2611 : 6790 : if (hard_fail)
2612 : : return NULL;
2613 : : /* Fall thru to normal processing. */
2614 : 7018 : }
2615 : :
2616 : : /* Get at the operands, verifying they are compatible. */
2617 : 3421380 : vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
2618 : 3421380 : slp_oprnd_info oprnd_info;
2619 : 16011013 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
2620 : : {
2621 : 25184124 : int res = vect_get_and_check_slp_defs (vinfo, vectype,
2622 : 12592062 : swap[i], skip_args,
2623 : : stmts, i, &oprnds_info);
2624 : 12592062 : if (res != 0)
2625 : 511859 : matches[(res == -1) ? 0 : i] = false;
2626 : 12592062 : if (!matches[0])
2627 : : break;
2628 : : }
2629 : 15720467 : for (i = 0; i < group_size; ++i)
2630 : 12502330 : if (!matches[i])
2631 : : {
2632 : 203243 : vect_free_oprnd_info (oprnds_info);
2633 : 203243 : return NULL;
2634 : : }
2635 : 9654411 : swap = NULL;
2636 : :
2637 : 9654411 : bool has_two_operators_perm = false;
2638 : 19308822 : auto_vec<unsigned> two_op_perm_indices[2];
2639 : 3218137 : vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL};
2640 : :
2641 : 3227988 : if (two_operators && oprnds_info.length () == 2 && group_size > 2)
2642 : : {
2643 : 1476 : unsigned idx = 0;
2644 : 1476 : hash_map<gimple *, unsigned> seen;
2645 : 1476 : vec<slp_oprnd_info> new_oprnds_info
2646 : 1476 : = vect_create_oprnd_info (1, group_size);
2647 : 1476 : bool success = true;
2648 : :
2649 : 1476 : enum tree_code code = ERROR_MARK;
2650 : 1476 : if (oprnds_info[0]->def_stmts[0]
2651 : 1476 : && is_a<gassign *> (oprnds_info[0]->def_stmts[0]->stmt))
2652 : 1414 : code = gimple_assign_rhs_code (oprnds_info[0]->def_stmts[0]->stmt);
2653 : 1476 : basic_block bb = nullptr;
2654 : :
2655 : 4545 : for (unsigned j = 0; j < group_size; ++j)
2656 : : {
2657 : 11259 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2658 : : {
2659 : 8190 : stmt_vec_info stmt_info = oprnd_info->def_stmts[j];
2660 : 8190 : if (!stmt_info
2661 : 8019 : || !is_a<gassign *> (stmt_info->stmt)
2662 : 8012 : || gimple_assign_rhs_code (stmt_info->stmt) != code
2663 : 15285 : || skip_args[i])
2664 : : {
2665 : : success = false;
2666 : 1099 : break;
2667 : : }
2668 : : /* Avoid mixing lanes with defs in different basic-blocks. */
2669 : 7095 : if (!bb)
2670 : 1572 : bb = gimple_bb (vect_orig_stmt (stmt_info)->stmt);
2671 : 7045 : else if (gimple_bb (vect_orig_stmt (stmt_info)->stmt) != bb)
2672 : : {
2673 : : success = false;
2674 : : break;
2675 : : }
2676 : :
2677 : 7091 : bool exists;
2678 : 7091 : unsigned &stmt_idx
2679 : 7091 : = seen.get_or_insert (stmt_info->stmt, &exists);
2680 : :
2681 : 7091 : if (!exists)
2682 : : {
2683 : 6042 : new_oprnds_info[0]->def_stmts.safe_push (stmt_info);
2684 : 6042 : new_oprnds_info[0]->ops.safe_push (oprnd_info->ops[j]);
2685 : 6042 : stmt_idx = idx;
2686 : 6042 : idx++;
2687 : : }
2688 : :
2689 : 7091 : two_op_perm_indices[i].safe_push (stmt_idx);
2690 : : }
2691 : :
2692 : 4168 : if (!success)
2693 : : break;
2694 : : }
2695 : :
2696 : 1476 : if (success && idx == group_size)
2697 : : {
2698 : 56 : if (dump_enabled_p ())
2699 : : {
2700 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2701 : : "Replace two_operators operands:\n");
2702 : :
2703 : 0 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2704 : : {
2705 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2706 : : "Operand %u:\n", i);
2707 : 0 : for (unsigned j = 0; j < group_size; j++)
2708 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2709 : 0 : j, oprnd_info->def_stmts[j]->stmt);
2710 : : }
2711 : :
2712 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2713 : : "With a single operand:\n");
2714 : 0 : for (unsigned j = 0; j < group_size; j++)
2715 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2716 : 0 : j, new_oprnds_info[0]->def_stmts[j]->stmt);
2717 : : }
2718 : :
2719 : 56 : two_op_scalar_stmts[0].safe_splice (oprnds_info[0]->def_stmts);
2720 : 56 : two_op_scalar_stmts[1].safe_splice (oprnds_info[1]->def_stmts);
2721 : :
2722 : 56 : new_oprnds_info[0]->first_op_type = oprnds_info[0]->first_op_type;
2723 : 56 : new_oprnds_info[0]->first_dt = oprnds_info[0]->first_dt;
2724 : 56 : new_oprnds_info[0]->any_pattern = oprnds_info[0]->any_pattern;
2725 : 56 : new_oprnds_info[0]->first_gs_p = oprnds_info[0]->first_gs_p;
2726 : 56 : new_oprnds_info[0]->first_gs_info = oprnds_info[0]->first_gs_info;
2727 : :
2728 : 56 : vect_free_oprnd_info (oprnds_info);
2729 : 56 : oprnds_info = new_oprnds_info;
2730 : 56 : nops = 1;
2731 : 56 : has_two_operators_perm = true;
2732 : : }
2733 : : else
2734 : 1420 : vect_free_oprnd_info (new_oprnds_info);
2735 : 1476 : }
2736 : :
2737 : 6436274 : auto_vec<slp_tree, 4> children;
2738 : :
2739 : 3218137 : stmt_info = stmts[0];
2740 : :
2741 : 3218137 : int reduc_idx = -1;
2742 : 3218137 : int gs_scale = 0;
2743 : 3218137 : tree gs_base = NULL_TREE;
2744 : :
2745 : : /* Create SLP_TREE nodes for the definition node/s. */
2746 : 8416686 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2747 : : {
2748 : 5269039 : slp_tree child = nullptr;
2749 : 5269039 : unsigned int j;
2750 : :
2751 : : /* We're skipping certain operands from processing, for example
2752 : : outer loop reduction initial defs. */
2753 : 5269039 : if (skip_args[i])
2754 : : {
2755 : 845775 : children.safe_push (NULL);
2756 : 6044324 : continue;
2757 : : }
2758 : :
2759 : 4423264 : if (oprnd_info->first_dt == vect_uninitialized_def)
2760 : : {
2761 : : /* COND_EXPR have one too many eventually if the condition
2762 : : is a SSA name. */
2763 : 0 : gcc_assert (i == 3 && nops == 4);
2764 : 0 : continue;
2765 : : }
2766 : :
2767 : 4423264 : if (oprnd_info->first_gs_p)
2768 : : {
2769 : 16745 : gs_scale = oprnd_info->first_gs_info.scale;
2770 : 16745 : gs_base = oprnd_info->first_gs_info.base;
2771 : : }
2772 : :
2773 : 4423264 : if (is_a <bb_vec_info> (vinfo)
2774 : 1580260 : && oprnd_info->first_dt == vect_internal_def
2775 : 5248321 : && !oprnd_info->any_pattern)
2776 : : {
2777 : : /* For BB vectorization, if all defs are the same do not
2778 : : bother to continue the build along the single-lane
2779 : : graph but use a splat of the scalar value. */
2780 : 782562 : stmt_vec_info first_def = oprnd_info->def_stmts[0];
2781 : 841939 : for (j = 1; j < group_size; ++j)
2782 : 798829 : if (oprnd_info->def_stmts[j] != first_def)
2783 : : break;
2784 : 782562 : if (j == group_size
2785 : : /* But avoid doing this for loads where we may be
2786 : : able to CSE things, unless the stmt is not
2787 : : vectorizable. */
2788 : 782562 : && (!STMT_VINFO_VECTORIZABLE (first_def)
2789 : 49583 : || !gimple_vuse (first_def->stmt)))
2790 : : {
2791 : 33955 : if (dump_enabled_p ())
2792 : 102 : dump_printf_loc (MSG_NOTE, vect_location,
2793 : : "Using a splat of the uniform operand %G",
2794 : : first_def->stmt);
2795 : 33955 : oprnd_info->first_dt = vect_external_def;
2796 : : }
2797 : : }
2798 : :
2799 : 4423264 : if (oprnd_info->first_dt == vect_external_def
2800 : 4423264 : || oprnd_info->first_dt == vect_constant_def)
2801 : : {
2802 : 1633967 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ())
2803 : : {
2804 : : tree op0;
2805 : : tree uniform_val = op0 = oprnd_info->ops[0];
2806 : : for (j = 1; j < oprnd_info->ops.length (); ++j)
2807 : : if (oprnd_info->ops[j]
2808 : : && !operand_equal_p (uniform_val, oprnd_info->ops[j]))
2809 : : {
2810 : : uniform_val = NULL_TREE;
2811 : : break;
2812 : : }
2813 : : if (!uniform_val
2814 : : && !can_duplicate_and_interleave_p (vinfo,
2815 : : oprnd_info->ops.length (),
2816 : : TREE_TYPE (op0)))
2817 : : {
2818 : : matches[j] = false;
2819 : : if (dump_enabled_p ())
2820 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2821 : : "Build SLP failed: invalid type of def "
2822 : : "for variable-length SLP %T\n", op0);
2823 : : goto fail;
2824 : : }
2825 : : }
2826 : 1633967 : slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops);
2827 : 1633967 : SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt;
2828 : 1633967 : oprnd_info->ops = vNULL;
2829 : 1633967 : children.safe_push (invnode);
2830 : 1633967 : continue;
2831 : 1633967 : }
2832 : :
2833 : : /* See which SLP operand a reduction chain continues on. We want
2834 : : to chain even PHIs but not backedges. */
2835 : 2789297 : if (STMT_VINFO_REDUC_DEF (oprnd_info->def_stmts[0])
2836 : 2789297 : || STMT_VINFO_REDUC_IDX (oprnd_info->def_stmts[0]) != -1)
2837 : : {
2838 : 145416 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle)
2839 : : {
2840 : 626 : if (oprnd_info->first_dt == vect_double_reduction_def)
2841 : 313 : reduc_idx = i;
2842 : : }
2843 : 144790 : else if (is_a <gphi *> (stmt_info->stmt)
2844 : 144790 : && gimple_phi_num_args
2845 : 63895 : (as_a <gphi *> (stmt_info->stmt)) != 1)
2846 : : ;
2847 : 81212 : else if (STMT_VINFO_REDUC_IDX (stmt_info) == -1
2848 : 317 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_double_reduction_def)
2849 : : ;
2850 : 81212 : else if (reduc_idx == -1)
2851 : 77183 : reduc_idx = i;
2852 : : else
2853 : : /* For .COND_* reduction operations the else value can be the
2854 : : same as one of the operation operands. The other def
2855 : : stmts have been moved, so we can't check easily. Check
2856 : : it's a call at least. */
2857 : 4029 : gcc_assert (is_a <gcall *> (stmt_info->stmt));
2858 : : }
2859 : :
2860 : : /* When we have a masked load with uniform mask discover this
2861 : : as a single-lane mask with a splat permute. This way we can
2862 : : recognize this as a masked load-lane by stripping the splat. */
2863 : 2789297 : if (is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
2864 : 32413 : && gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
2865 : : IFN_MASK_LOAD)
2866 : 4496 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2867 : 2789319 : && ! STMT_VINFO_SLP_VECT_ONLY (DR_GROUP_FIRST_ELEMENT (stmt_info)))
2868 : : {
2869 : 0 : vec<stmt_vec_info> def_stmts2;
2870 : 0 : def_stmts2.create (1);
2871 : 0 : def_stmts2.quick_push (oprnd_info->def_stmts[0]);
2872 : 0 : child = vect_build_slp_tree (vinfo, def_stmts2, 1,
2873 : : &this_max_nunits,
2874 : : matches, limit,
2875 : : &this_tree_size, bst_map);
2876 : 0 : if (child)
2877 : : {
2878 : 0 : slp_tree pnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
2879 : 0 : SLP_TREE_VECTYPE (pnode) = SLP_TREE_VECTYPE (child);
2880 : 0 : SLP_TREE_LANES (pnode) = group_size;
2881 : 0 : SLP_TREE_SCALAR_STMTS (pnode).create (group_size);
2882 : 0 : SLP_TREE_LANE_PERMUTATION (pnode).create (group_size);
2883 : 0 : for (unsigned k = 0; k < group_size; ++k)
2884 : : {
2885 : 0 : SLP_TREE_SCALAR_STMTS (pnode)
2886 : 0 : .quick_push (oprnd_info->def_stmts[0]);
2887 : 0 : SLP_TREE_LANE_PERMUTATION (pnode)
2888 : 0 : .quick_push (std::make_pair (0u, 0u));
2889 : : }
2890 : 0 : SLP_TREE_CHILDREN (pnode).quick_push (child);
2891 : 0 : pnode->max_nunits = child->max_nunits;
2892 : 0 : children.safe_push (pnode);
2893 : 0 : oprnd_info->def_stmts = vNULL;
2894 : 0 : continue;
2895 : 0 : }
2896 : : else
2897 : 0 : def_stmts2.release ();
2898 : : }
2899 : :
2900 : 2789297 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
2901 : : group_size, &this_max_nunits,
2902 : : matches, limit,
2903 : : &this_tree_size, bst_map)) != NULL)
2904 : : {
2905 : 2343052 : oprnd_info->def_stmts = vNULL;
2906 : 2343052 : children.safe_push (child);
2907 : 2343052 : continue;
2908 : : }
2909 : :
2910 : : /* If the SLP build for operand zero failed and operand zero
2911 : : and one can be commutated try that for the scalar stmts
2912 : : that failed the match. */
2913 : 446245 : if (i == 0
2914 : : /* A first scalar stmt mismatch signals a fatal mismatch. */
2915 : 352851 : && matches[0]
2916 : : /* ??? For COND_EXPRs we can swap the comparison operands
2917 : : as well as the arms under some constraints. */
2918 : 158977 : && (nops == 2 || nops == 3)
2919 : 94291 : && oprnds_info[1]->first_dt == vect_internal_def
2920 : 53154 : && (is_gimple_assign (stmt_info->stmt)
2921 : 11119 : || is_gimple_call (stmt_info->stmt))
2922 : : /* Swapping operands for reductions breaks assumptions later on. */
2923 : 488293 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
2924 : : {
2925 : : /* See whether we can swap the matching or the non-matching
2926 : : stmt operands. */
2927 : : bool swap_not_matching = true;
2928 : 49412 : do
2929 : : {
2930 : 7016782 : for (j = 0; j < group_size; ++j)
2931 : : {
2932 : 6982028 : if (matches[j] != !swap_not_matching)
2933 : 59606 : continue;
2934 : 6922422 : stmt_vec_info stmt_info = stmts[j];
2935 : : /* Verify if we can swap operands of this stmt. */
2936 : 6922422 : if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
2937 : : {
2938 : 6922396 : tree_code code = gimple_assign_rhs_code (stmt);
2939 : 6922396 : if (! commutative_tree_code (code)
2940 : 6922396 : && ! commutative_ternary_tree_code (code))
2941 : : {
2942 : 14634 : if (!swap_not_matching)
2943 : 6836 : goto fail;
2944 : : swap_not_matching = false;
2945 : : break;
2946 : : }
2947 : : }
2948 : 6967396 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2949 : : {
2950 : 26 : internal_fn fn = (gimple_call_internal_p (call)
2951 : 26 : ? gimple_call_internal_fn (call)
2952 : : : IFN_LAST);
2953 : 26 : if ((! commutative_binary_fn_p (fn)
2954 : 26 : && ! commutative_ternary_fn_p (fn))
2955 : 28 : || first_commutative_argument (fn) != 0)
2956 : : {
2957 : 24 : if (!swap_not_matching)
2958 : 12 : goto fail;
2959 : : swap_not_matching = false;
2960 : : break;
2961 : : }
2962 : : }
2963 : : }
2964 : : }
2965 : 42564 : while (j != group_size);
2966 : :
2967 : : /* Swap mismatched definition stmts. */
2968 : 34754 : if (dump_enabled_p ())
2969 : 326 : dump_printf_loc (MSG_NOTE, vect_location,
2970 : : "Re-trying with swapped operands of stmts ");
2971 : 6993541 : for (j = 0; j < group_size; ++j)
2972 : 6958787 : if (matches[j] == !swap_not_matching)
2973 : : {
2974 : 13815264 : std::swap (oprnds_info[0]->def_stmts[j],
2975 : 6907632 : oprnds_info[1]->def_stmts[j]);
2976 : 13815264 : std::swap (oprnds_info[0]->ops[j],
2977 : 6907632 : oprnds_info[1]->ops[j]);
2978 : 6907632 : if (dump_enabled_p ())
2979 : 911 : dump_printf (MSG_NOTE, "%d ", j);
2980 : : }
2981 : 34754 : if (dump_enabled_p ())
2982 : 326 : dump_printf (MSG_NOTE, "\n");
2983 : : /* After swapping some operands we lost track whether an
2984 : : operand has any pattern defs so be conservative here. */
2985 : 66303 : if (oprnds_info[0]->any_pattern || oprnds_info[1]->any_pattern)
2986 : 3244 : oprnds_info[0]->any_pattern = oprnds_info[1]->any_pattern = true;
2987 : : /* And try again with scratch 'matches' ... */
2988 : 34754 : bool *tem = XALLOCAVEC (bool, group_size);
2989 : 34754 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
2990 : : group_size, &this_max_nunits,
2991 : : tem, limit,
2992 : : &this_tree_size, bst_map)) != NULL)
2993 : : {
2994 : 6518 : oprnd_info->def_stmts = vNULL;
2995 : 6518 : children.safe_push (child);
2996 : 6518 : continue;
2997 : : }
2998 : : }
2999 : 439727 : fail:
3000 : :
3001 : : /* If the SLP build failed and we analyze a basic-block
3002 : : simply treat nodes we fail to build as externally defined
3003 : : (and thus build vectors from the scalar defs).
3004 : : The cost model will reject outright expensive cases.
3005 : : ??? This doesn't treat cases where permutation ultimatively
3006 : : fails (or we don't try permutation below). Ideally we'd
3007 : : even compute a permutation that will end up with the maximum
3008 : : SLP tree size... */
3009 : 439727 : if (is_a <bb_vec_info> (vinfo)
3010 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
3011 : : do extra work to cancel the pattern so the uses see the
3012 : : scalar version. */
3013 : 403705 : && !is_pattern_stmt_p (stmt_info)
3014 : 819746 : && !oprnd_info->any_pattern)
3015 : : {
3016 : : /* But if there's a leading vector sized set of matching stmts
3017 : : fail here so we can split the group. This matches the condition
3018 : : vect_analyze_slp_instance uses. */
3019 : : /* ??? We might want to split here and combine the results to support
3020 : : multiple vector sizes better. */
3021 : 588537 : for (j = 0; j < group_size; ++j)
3022 : 588537 : if (!matches[j])
3023 : : break;
3024 : 379779 : if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype))
3025 : 379744 : && vect_slp_can_convert_to_external (oprnd_info->def_stmts))
3026 : : {
3027 : 369237 : if (dump_enabled_p ())
3028 : 503 : dump_printf_loc (MSG_NOTE, vect_location,
3029 : : "Building vector operands from scalars\n");
3030 : 369237 : this_tree_size++;
3031 : 369237 : child = vect_create_new_slp_node (oprnd_info->ops);
3032 : 369237 : children.safe_push (child);
3033 : 369237 : oprnd_info->ops = vNULL;
3034 : 369237 : continue;
3035 : : }
3036 : : }
3037 : :
3038 : 70490 : gcc_assert (child == NULL);
3039 : 75303 : FOR_EACH_VEC_ELT (children, j, child)
3040 : 4813 : if (child)
3041 : 4813 : vect_free_slp_tree (child);
3042 : 70490 : vect_free_oprnd_info (oprnds_info);
3043 : 70490 : return NULL;
3044 : : }
3045 : :
3046 : 3147647 : vect_free_oprnd_info (oprnds_info);
3047 : :
3048 : : /* If we have all children of a child built up from uniform scalars
3049 : : or does more than one possibly expensive vector construction then
3050 : : just throw that away, causing it built up from scalars.
3051 : : The exception is the SLP node for the vector store. */
3052 : 3147647 : if (is_a <bb_vec_info> (vinfo)
3053 : 1115329 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3054 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
3055 : : do extra work to cancel the pattern so the uses see the
3056 : : scalar version. */
3057 : 3601448 : && !is_pattern_stmt_p (stmt_info))
3058 : : {
3059 : : slp_tree child;
3060 : : unsigned j;
3061 : : bool all_uniform_p = true;
3062 : : unsigned n_vector_builds = 0;
3063 : 1264199 : FOR_EACH_VEC_ELT (children, j, child)
3064 : : {
3065 : 835358 : if (!child)
3066 : : ;
3067 : 835358 : else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3068 : : all_uniform_p = false;
3069 : 597333 : else if (!vect_slp_tree_uniform_p (child))
3070 : : {
3071 : 458674 : all_uniform_p = false;
3072 : 458674 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def)
3073 : 423469 : n_vector_builds++;
3074 : : }
3075 : : }
3076 : 428841 : if (all_uniform_p
3077 : 428841 : || n_vector_builds > 1
3078 : 730108 : || (n_vector_builds == children.length ()
3079 : 32769 : && is_a <gphi *> (stmt_info->stmt)))
3080 : : {
3081 : : /* Roll back. */
3082 : 135905 : matches[0] = false;
3083 : 417521 : FOR_EACH_VEC_ELT (children, j, child)
3084 : 281616 : if (child)
3085 : 281616 : vect_free_slp_tree (child);
3086 : :
3087 : 135905 : if (dump_enabled_p ())
3088 : 129 : dump_printf_loc (MSG_NOTE, vect_location,
3089 : : "Building parent vector operands from "
3090 : : "scalars instead\n");
3091 : 135905 : return NULL;
3092 : : }
3093 : : }
3094 : :
3095 : 3011742 : *tree_size += this_tree_size + 1;
3096 : 3011742 : *max_nunits = this_max_nunits;
3097 : :
3098 : 3011742 : if (two_operators)
3099 : : {
3100 : : /* ??? We'd likely want to either cache in bst_map sth like
3101 : : { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
3102 : : the true { a+b, a+b, a+b, a+b } ... but there we don't have
3103 : : explicit stmts to put in so the keying on 'stmts' doesn't
3104 : : work (but we have the same issue with nodes that use 'ops'). */
3105 : :
3106 : 4906 : if (has_two_operators_perm)
3107 : : {
3108 : 22 : slp_tree child = children[0];
3109 : 22 : children.truncate (0);
3110 : 66 : for (i = 0; i < 2; i++)
3111 : : {
3112 : 44 : slp_tree pnode
3113 : 44 : = vect_create_new_slp_node (two_op_scalar_stmts[i], 2);
3114 : 44 : SLP_TREE_CODE (pnode) = VEC_PERM_EXPR;
3115 : 44 : SLP_TREE_VECTYPE (pnode) = vectype;
3116 : 44 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3117 : 44 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3118 : 44 : lane_permutation_t& perm = SLP_TREE_LANE_PERMUTATION (pnode);
3119 : 44 : children.safe_push (pnode);
3120 : :
3121 : 476 : for (unsigned j = 0; j < stmts.length (); j++)
3122 : 432 : perm.safe_push (std::make_pair (0, two_op_perm_indices[i][j]));
3123 : : }
3124 : :
3125 : 22 : SLP_TREE_REF_COUNT (child) += 4;
3126 : : }
3127 : :
3128 : 4906 : slp_tree one = new _slp_tree;
3129 : 4906 : slp_tree two = new _slp_tree;
3130 : 4906 : SLP_TREE_DEF_TYPE (one) = vect_internal_def;
3131 : 4906 : SLP_TREE_DEF_TYPE (two) = vect_internal_def;
3132 : 4906 : SLP_TREE_VECTYPE (one) = vectype;
3133 : 4906 : SLP_TREE_VECTYPE (two) = vectype;
3134 : 4906 : SLP_TREE_CHILDREN (one).safe_splice (children);
3135 : 4906 : SLP_TREE_CHILDREN (two).safe_splice (children);
3136 : 4906 : slp_tree child;
3137 : 19626 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two), i, child)
3138 : 9814 : SLP_TREE_REF_COUNT (child)++;
3139 : :
3140 : : /* Here we record the original defs since this
3141 : : node represents the final lane configuration. */
3142 : 4906 : node = vect_create_new_slp_node (node, stmts, 2);
3143 : 4906 : SLP_TREE_VECTYPE (node) = vectype;
3144 : 4906 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
3145 : 4906 : SLP_TREE_CHILDREN (node).quick_push (one);
3146 : 4906 : SLP_TREE_CHILDREN (node).quick_push (two);
3147 : 4906 : enum tree_code code0 = ERROR_MARK;
3148 : 4906 : enum tree_code ocode = ERROR_MARK;
3149 : 4906 : if (gassign *stmt = dyn_cast <gassign *> (stmts[0]->stmt))
3150 : 4904 : code0 = gimple_assign_rhs_code (stmt);
3151 : 4906 : stmt_vec_info ostmt_info;
3152 : 4906 : unsigned j = 0;
3153 : 18898 : FOR_EACH_VEC_ELT (stmts, i, ostmt_info)
3154 : : {
3155 : 13992 : int op = 0;
3156 : 13992 : if (gassign *ostmt = dyn_cast <gassign *> (ostmt_info->stmt))
3157 : : {
3158 : 13988 : if (gimple_assign_rhs_code (ostmt) != code0)
3159 : : {
3160 : 7022 : ocode = gimple_assign_rhs_code (ostmt);
3161 : : op = 1;
3162 : : j = i;
3163 : : }
3164 : : }
3165 : : else
3166 : : {
3167 : 8 : if (gimple_call_combined_fn (stmts[0]->stmt)
3168 : 4 : != gimple_call_combined_fn (ostmt_info->stmt))
3169 : : {
3170 : 2 : op = 1;
3171 : 2 : j = i;
3172 : : }
3173 : : }
3174 : 13992 : SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (op, i));
3175 : : }
3176 : 4906 : SLP_TREE_CODE (one) = code0;
3177 : 4906 : SLP_TREE_CODE (two) = ocode;
3178 : 4906 : SLP_TREE_LANES (one) = stmts.length ();
3179 : 4906 : SLP_TREE_LANES (two) = stmts.length ();
3180 : 4906 : SLP_TREE_REPRESENTATIVE (one) = stmts[0];
3181 : 4906 : SLP_TREE_REPRESENTATIVE (two) = stmts[j];
3182 : :
3183 : 4906 : return node;
3184 : : }
3185 : :
3186 : 3006836 : node = vect_create_new_slp_node (node, stmts, nops);
3187 : 3006836 : SLP_TREE_VECTYPE (node) = vectype;
3188 : 3006836 : SLP_TREE_CHILDREN (node).splice (children);
3189 : 3006836 : SLP_TREE_GS_SCALE (node) = gs_scale;
3190 : 3006836 : SLP_TREE_GS_BASE (node) = gs_base;
3191 : 3006836 : if (reduc_idx != -1)
3192 : : {
3193 : 75967 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) != -1
3194 : : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
3195 : : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def);
3196 : 75967 : SLP_TREE_REDUC_IDX (node) = reduc_idx;
3197 : 75967 : node->cycle_info.id = SLP_TREE_CHILDREN (node)[reduc_idx]->cycle_info.id;
3198 : : }
3199 : : /* When reaching the reduction PHI, create a vect_reduc_info. */
3200 : 2930869 : else if ((STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def
3201 : 2930869 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3202 : 2930869 : && is_a <gphi *> (STMT_VINFO_STMT (stmt_info)))
3203 : : {
3204 : 65945 : loop_vec_info loop_vinfo = as_a <loop_vec_info> (vinfo);
3205 : 65945 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) == -1);
3206 : 65945 : node->cycle_info.id = loop_vinfo->reduc_infos.length ();
3207 : 65945 : vect_reduc_info reduc_info = new vect_reduc_info_s ();
3208 : 65945 : loop_vinfo->reduc_infos.safe_push (reduc_info);
3209 : 65945 : stmt_vec_info reduc_phi = stmt_info;
3210 : : /* ??? For double reductions vect_is_simple_reduction stores the
3211 : : reduction type and code on the inner loop header PHI. */
3212 : 65945 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3213 : : {
3214 : 313 : use_operand_p use_p;
3215 : 313 : gimple *use_stmt;
3216 : 313 : bool res = single_imm_use (gimple_phi_result (stmt_info->stmt),
3217 : : &use_p, &use_stmt);
3218 : 313 : gcc_assert (res);
3219 : 313 : reduc_phi = loop_vinfo->lookup_stmt (use_stmt);
3220 : : }
3221 : 65945 : VECT_REDUC_INFO_DEF_TYPE (reduc_info) = STMT_VINFO_DEF_TYPE (stmt_info);
3222 : 65945 : VECT_REDUC_INFO_TYPE (reduc_info) = STMT_VINFO_REDUC_TYPE (reduc_phi);
3223 : 65945 : VECT_REDUC_INFO_CODE (reduc_info) = STMT_VINFO_REDUC_CODE (reduc_phi);
3224 : 65945 : VECT_REDUC_INFO_FN (reduc_info) = IFN_LAST;
3225 : : }
3226 : : return node;
3227 : 9654411 : }
3228 : :
3229 : : /* Dump a single SLP tree NODE. */
3230 : :
3231 : : static void
3232 : 448957 : vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
3233 : : slp_tree node)
3234 : : {
3235 : 448957 : unsigned i, j;
3236 : 448957 : slp_tree child;
3237 : 448957 : stmt_vec_info stmt_info;
3238 : 448957 : tree op;
3239 : :
3240 : 448957 : dump_metadata_t metadata (dump_kind, loc.get_impl_location ());
3241 : 448957 : dump_user_location_t user_loc = loc.get_user_location ();
3242 : 448957 : dump_printf_loc (metadata, user_loc,
3243 : : "node%s %p (max_nunits=" HOST_WIDE_INT_PRINT_UNSIGNED
3244 : : ", refcnt=%u)",
3245 : 448957 : SLP_TREE_DEF_TYPE (node) == vect_external_def
3246 : : ? " (external)"
3247 : : : (SLP_TREE_DEF_TYPE (node) == vect_constant_def
3248 : 433846 : ? " (constant)"
3249 : : : ""), (void *) node,
3250 : 448957 : estimated_poly_value (node->max_nunits),
3251 : : SLP_TREE_REF_COUNT (node));
3252 : 448957 : if (SLP_TREE_VECTYPE (node))
3253 : 376506 : dump_printf (metadata, " %T", SLP_TREE_VECTYPE (node));
3254 : 448957 : dump_printf (metadata, "%s",
3255 : 448957 : node->avoid_stlf_fail ? " (avoid-stlf-fail)" : "");
3256 : 448957 : if (node->cycle_info.id != -1 || node->cycle_info.reduc_idx != -1)
3257 : 22558 : dump_printf (metadata, " cycle %d, link %d", node->cycle_info.id,
3258 : : node->cycle_info.reduc_idx);
3259 : 448957 : dump_printf (metadata, "\n");
3260 : 448957 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
3261 : : {
3262 : 361248 : if (SLP_TREE_PERMUTE_P (node))
3263 : 12839 : dump_printf_loc (metadata, user_loc, "op: VEC_PERM_EXPR\n");
3264 : : else
3265 : 348409 : dump_printf_loc (metadata, user_loc, "op template: %G",
3266 : 348409 : SLP_TREE_REPRESENTATIVE (node)->stmt);
3267 : : }
3268 : 448957 : if (SLP_TREE_SCALAR_STMTS (node).exists ())
3269 : 851443 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3270 : 497917 : if (stmt_info)
3271 : 493138 : dump_printf_loc (metadata, user_loc, "\t%sstmt %u %G",
3272 : 493138 : STMT_VINFO_LIVE_P (stmt_info) ? "[l] " : "",
3273 : : i, stmt_info->stmt);
3274 : : else
3275 : 4779 : dump_printf_loc (metadata, user_loc, "\tstmt %u ---\n", i);
3276 : : else
3277 : : {
3278 : 95431 : dump_printf_loc (metadata, user_loc, "\t{ ");
3279 : 302234 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
3280 : 111372 : dump_printf (metadata, "%T%s ", op,
3281 : 111372 : i < SLP_TREE_SCALAR_OPS (node).length () - 1 ? "," : "");
3282 : 95431 : dump_printf (metadata, "}\n");
3283 : : }
3284 : 448957 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
3285 : : {
3286 : 60480 : dump_printf_loc (metadata, user_loc, "\tload permutation {");
3287 : 196868 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), i, j)
3288 : 75908 : dump_printf (dump_kind, " %u", j);
3289 : 60480 : dump_printf (dump_kind, " }\n");
3290 : : }
3291 : 448957 : if (SLP_TREE_LANE_PERMUTATION (node).exists ())
3292 : : {
3293 : 12847 : dump_printf_loc (metadata, user_loc, "\tlane permutation {");
3294 : 61226 : for (i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
3295 : 35532 : dump_printf (dump_kind, " %u[%u]",
3296 : 35532 : SLP_TREE_LANE_PERMUTATION (node)[i].first,
3297 : 35532 : SLP_TREE_LANE_PERMUTATION (node)[i].second);
3298 : 12847 : dump_printf (dump_kind, " }%s\n",
3299 : 12847 : node->ldst_lanes ? " (load-lanes)" : "");
3300 : : }
3301 : 448957 : if (SLP_TREE_CHILDREN (node).is_empty ())
3302 : 169882 : return;
3303 : 279075 : dump_printf_loc (metadata, user_loc, "\tchildren");
3304 : 1019685 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3305 : 461535 : dump_printf (dump_kind, " %p", (void *)child);
3306 : 279075 : dump_printf (dump_kind, "%s\n",
3307 : 279075 : node->ldst_lanes && !SLP_TREE_LANE_PERMUTATION (node).exists ()
3308 : : ? " (store-lanes)" : "");
3309 : : }
3310 : :
3311 : : DEBUG_FUNCTION void
3312 : 0 : debug (slp_tree node)
3313 : : {
3314 : 0 : debug_dump_context ctx;
3315 : 0 : vect_print_slp_tree (MSG_NOTE,
3316 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3317 : : node);
3318 : 0 : }
3319 : :
3320 : : /* Recursive helper for the dot producer below. */
3321 : :
3322 : : static void
3323 : 0 : dot_slp_tree (FILE *f, slp_tree node, hash_set<slp_tree> &visited)
3324 : : {
3325 : 0 : if (visited.add (node))
3326 : : return;
3327 : :
3328 : 0 : fprintf (f, "\"%p\" [label=\"", (void *)node);
3329 : 0 : vect_print_slp_tree (MSG_NOTE,
3330 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3331 : : node);
3332 : 0 : fprintf (f, "\"];\n");
3333 : :
3334 : :
3335 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3336 : 0 : fprintf (f, "\"%p\" -> \"%p\";", (void *)node, (void *)child);
3337 : :
3338 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3339 : 0 : if (child)
3340 : 0 : dot_slp_tree (f, child, visited);
3341 : : }
3342 : :
3343 : : DEBUG_FUNCTION void
3344 : 0 : dot_slp_tree (const char *fname, slp_tree node)
3345 : : {
3346 : 0 : FILE *f = fopen (fname, "w");
3347 : 0 : fprintf (f, "digraph {\n");
3348 : 0 : fflush (f);
3349 : 0 : {
3350 : 0 : debug_dump_context ctx (f);
3351 : 0 : hash_set<slp_tree> visited;
3352 : 0 : dot_slp_tree (f, node, visited);
3353 : 0 : }
3354 : 0 : fflush (f);
3355 : 0 : fprintf (f, "}\n");
3356 : 0 : fclose (f);
3357 : 0 : }
3358 : :
3359 : : DEBUG_FUNCTION void
3360 : 0 : dot_slp_tree (const char *fname, const vec<slp_instance> &slp_instances)
3361 : : {
3362 : 0 : FILE *f = fopen (fname, "w");
3363 : 0 : fprintf (f, "digraph {\n");
3364 : 0 : fflush (f);
3365 : 0 : {
3366 : 0 : debug_dump_context ctx (f);
3367 : 0 : hash_set<slp_tree> visited;
3368 : 0 : for (auto inst : slp_instances)
3369 : 0 : dot_slp_tree (f, SLP_INSTANCE_TREE (inst), visited);
3370 : 0 : }
3371 : 0 : fflush (f);
3372 : 0 : fprintf (f, "}\n");
3373 : 0 : fclose (f);
3374 : 0 : }
3375 : :
3376 : : /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
3377 : :
3378 : : static void
3379 : 487116 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3380 : : slp_tree node, hash_set<slp_tree> &visited)
3381 : : {
3382 : 487116 : unsigned i;
3383 : 487116 : slp_tree child;
3384 : :
3385 : 487116 : if (visited.add (node))
3386 : 487116 : return;
3387 : :
3388 : 448569 : vect_print_slp_tree (dump_kind, loc, node);
3389 : :
3390 : 1358212 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3391 : 461074 : if (child)
3392 : 407849 : vect_print_slp_graph (dump_kind, loc, child, visited);
3393 : : }
3394 : :
3395 : : static void
3396 : 48106 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3397 : : slp_tree entry)
3398 : : {
3399 : 48106 : hash_set<slp_tree> visited;
3400 : 48106 : vect_print_slp_graph (dump_kind, loc, entry, visited);
3401 : 48106 : }
3402 : :
3403 : : DEBUG_FUNCTION void
3404 : 0 : debug (slp_instance instance)
3405 : : {
3406 : 0 : debug_dump_context ctx;
3407 : 0 : vect_print_slp_graph (MSG_NOTE,
3408 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3409 : : SLP_INSTANCE_TREE (instance));
3410 : 0 : }
3411 : :
3412 : : /* Mark the tree rooted at NODE with PURE_SLP. */
3413 : :
3414 : : static void
3415 : 6444118 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node,
3416 : : hash_set<slp_tree> &visited)
3417 : : {
3418 : 6444118 : int i;
3419 : 6444118 : stmt_vec_info stmt_info;
3420 : 6444118 : slp_tree child;
3421 : :
3422 : 6444118 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3423 : : return;
3424 : :
3425 : 4571126 : if (visited.add (node))
3426 : : return;
3427 : :
3428 : 10736545 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3429 : 6432229 : if (stmt_info)
3430 : : {
3431 : 6357817 : STMT_SLP_TYPE (stmt_info) = pure_slp;
3432 : : /* ??? For .MASK_LOAD and .MASK_STORE detected as load/store-lanes
3433 : : when there is the mask_conversion pattern applied we have lost the
3434 : : alternate lanes of the uniform mask which nevertheless
3435 : : have separate pattern defs. To not confuse hybrid
3436 : : analysis we mark those as covered as well here. */
3437 : 6357817 : if (node->ldst_lanes)
3438 : 6432229 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
3439 : 0 : if (gimple_call_internal_p (call, IFN_MASK_LOAD)
3440 : 0 : || gimple_call_internal_p (call, IFN_MASK_STORE))
3441 : : {
3442 : 0 : tree mask = gimple_call_arg (call,
3443 : : internal_fn_mask_index
3444 : 0 : (gimple_call_internal_fn (call)));
3445 : 0 : if (TREE_CODE (mask) == SSA_NAME)
3446 : 0 : if (stmt_vec_info mask_info = vinfo->lookup_def (mask))
3447 : : {
3448 : 0 : mask_info = vect_stmt_to_vectorize (mask_info);
3449 : 0 : STMT_SLP_TYPE (mask_info) = pure_slp;
3450 : : }
3451 : : }
3452 : : }
3453 : :
3454 : 10168257 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3455 : 5863941 : if (child)
3456 : 4713591 : vect_mark_slp_stmts (vinfo, child, visited);
3457 : : }
3458 : :
3459 : : static void
3460 : 1730527 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node)
3461 : : {
3462 : 1730527 : hash_set<slp_tree> visited;
3463 : 1730527 : vect_mark_slp_stmts (vinfo, node, visited);
3464 : 1730527 : }
3465 : :
3466 : : /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
3467 : :
3468 : : static void
3469 : 2343077 : vect_mark_slp_stmts_relevant (slp_tree node, hash_set<slp_tree> &visited)
3470 : : {
3471 : 2343077 : int i;
3472 : 2343077 : stmt_vec_info stmt_info;
3473 : 2343077 : slp_tree child;
3474 : :
3475 : 2343077 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3476 : : return;
3477 : :
3478 : 1375659 : if (visited.add (node))
3479 : : return;
3480 : :
3481 : 4311177 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3482 : 3034718 : if (stmt_info)
3483 : : {
3484 : 3034718 : gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
3485 : : || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
3486 : 3034718 : STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
3487 : : }
3488 : :
3489 : 2835339 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3490 : 1558880 : if (child)
3491 : 1558880 : vect_mark_slp_stmts_relevant (child, visited);
3492 : : }
3493 : :
3494 : : static void
3495 : 784197 : vect_mark_slp_stmts_relevant (slp_tree node)
3496 : : {
3497 : 784197 : hash_set<slp_tree> visited;
3498 : 784197 : vect_mark_slp_stmts_relevant (node, visited);
3499 : 784197 : }
3500 : :
3501 : :
3502 : : /* Gather loads in the SLP graph NODE and populate the INST loads array. */
3503 : :
3504 : : static void
3505 : 11751042 : vect_gather_slp_loads (vec<slp_tree> &loads, slp_tree node,
3506 : : hash_set<slp_tree> &visited)
3507 : : {
3508 : 11751042 : if (!node || visited.add (node))
3509 : 2498368 : return;
3510 : :
3511 : 9252674 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3512 : : return;
3513 : :
3514 : 6632639 : if (!SLP_TREE_PERMUTE_P (node))
3515 : : {
3516 : 6416656 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
3517 : 6416656 : if (STMT_VINFO_DATA_REF (stmt_info)
3518 : 2357326 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
3519 : 1271819 : loads.safe_push (node);
3520 : : }
3521 : :
3522 : : unsigned i;
3523 : : slp_tree child;
3524 : 15779692 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3525 : 9147053 : vect_gather_slp_loads (loads, child, visited);
3526 : : }
3527 : :
3528 : :
3529 : : /* Find the last store in SLP INSTANCE. */
3530 : :
3531 : : stmt_vec_info
3532 : 2732252 : vect_find_last_scalar_stmt_in_slp (slp_tree node)
3533 : : {
3534 : 2732252 : stmt_vec_info last = NULL;
3535 : 2732252 : stmt_vec_info stmt_vinfo;
3536 : :
3537 : 9967880 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3538 : 7235628 : if (stmt_vinfo)
3539 : : {
3540 : 7235628 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3541 : 7235628 : last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
3542 : : }
3543 : :
3544 : 2732252 : return last;
3545 : : }
3546 : :
3547 : : /* Find the first stmt in NODE. */
3548 : :
3549 : : stmt_vec_info
3550 : 526229 : vect_find_first_scalar_stmt_in_slp (slp_tree node)
3551 : : {
3552 : 526229 : stmt_vec_info first = NULL;
3553 : 526229 : stmt_vec_info stmt_vinfo;
3554 : :
3555 : 1772608 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3556 : 1246379 : if (stmt_vinfo)
3557 : : {
3558 : 1243758 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3559 : 1243758 : if (!first
3560 : 1243758 : || get_later_stmt (stmt_vinfo, first) == first)
3561 : : first = stmt_vinfo;
3562 : : }
3563 : :
3564 : 526229 : return first;
3565 : : }
3566 : :
3567 : : /* Splits a group of stores, currently beginning at FIRST_VINFO, into
3568 : : two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
3569 : : (also containing the first GROUP1_SIZE stmts, since stores are
3570 : : consecutive), the second containing the remainder.
3571 : : Return the first stmt in the second group. */
3572 : :
3573 : : static stmt_vec_info
3574 : 155893 : vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
3575 : : {
3576 : 155893 : gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
3577 : 155893 : gcc_assert (group1_size > 0);
3578 : 155893 : int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
3579 : 155893 : gcc_assert (group2_size > 0);
3580 : 155893 : DR_GROUP_SIZE (first_vinfo) = group1_size;
3581 : :
3582 : 155893 : stmt_vec_info stmt_info = first_vinfo;
3583 : 524353 : for (unsigned i = group1_size; i > 1; i--)
3584 : : {
3585 : 368460 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
3586 : 368460 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3587 : : }
3588 : : /* STMT is now the last element of the first group. */
3589 : 155893 : stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
3590 : 155893 : DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
3591 : :
3592 : 155893 : DR_GROUP_SIZE (group2) = group2_size;
3593 : 434498 : for (stmt_info = group2; stmt_info;
3594 : 278605 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
3595 : : {
3596 : 278605 : DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
3597 : 278605 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3598 : : }
3599 : :
3600 : : /* For the second group, the DR_GROUP_GAP is that before the original group,
3601 : : plus skipping over the first vector. */
3602 : 155893 : DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
3603 : :
3604 : : /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
3605 : 155893 : DR_GROUP_GAP (first_vinfo) += group2_size;
3606 : :
3607 : 155893 : if (dump_enabled_p ())
3608 : 53 : dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
3609 : : group1_size, group2_size);
3610 : :
3611 : 155893 : return group2;
3612 : : }
3613 : :
3614 : : /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
3615 : : statements and a vector of NUNITS elements. */
3616 : :
3617 : : static poly_uint64
3618 : 4404089 : calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
3619 : : {
3620 : 4404089 : return exact_div (common_multiple (nunits, group_size), group_size);
3621 : : }
3622 : :
3623 : : /* Helper that checks to see if a node is a load node. */
3624 : :
3625 : : static inline bool
3626 : 86 : vect_is_slp_load_node (slp_tree root)
3627 : : {
3628 : 86 : return (!SLP_TREE_PERMUTE_P (root)
3629 : 74 : && SLP_TREE_DEF_TYPE (root) == vect_internal_def
3630 : 62 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root))
3631 : 126 : && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))));
3632 : : }
3633 : :
3634 : :
3635 : : /* Helper function of optimize_load_redistribution that performs the operation
3636 : : recursively. */
3637 : :
3638 : : static slp_tree
3639 : 21502 : optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t *bst_map,
3640 : : vec_info *vinfo, unsigned int group_size,
3641 : : hash_map<slp_tree, slp_tree> *load_map,
3642 : : slp_tree root)
3643 : : {
3644 : 21502 : if (slp_tree *leader = load_map->get (root))
3645 : 3493 : return *leader;
3646 : :
3647 : 18009 : slp_tree node;
3648 : 18009 : unsigned i;
3649 : :
3650 : : /* For now, we don't know anything about externals so do not do anything. */
3651 : 18009 : if (!root || SLP_TREE_DEF_TYPE (root) != vect_internal_def)
3652 : : return NULL;
3653 : 13111 : else if (SLP_TREE_PERMUTE_P (root))
3654 : : {
3655 : : /* First convert this node into a load node and add it to the leaves
3656 : : list and flatten the permute from a lane to a load one. If it's
3657 : : unneeded it will be elided later. */
3658 : 66 : vec<stmt_vec_info> stmts;
3659 : 66 : stmts.create (SLP_TREE_LANES (root));
3660 : 66 : lane_permutation_t lane_perm = SLP_TREE_LANE_PERMUTATION (root);
3661 : 106 : for (unsigned j = 0; j < lane_perm.length (); j++)
3662 : : {
3663 : 86 : std::pair<unsigned, unsigned> perm = lane_perm[j];
3664 : 86 : node = SLP_TREE_CHILDREN (root)[perm.first];
3665 : :
3666 : 86 : if (!vect_is_slp_load_node (node)
3667 : 86 : || SLP_TREE_CHILDREN (node).exists ())
3668 : : {
3669 : 46 : stmts.release ();
3670 : 46 : goto next;
3671 : : }
3672 : :
3673 : 40 : stmts.quick_push (SLP_TREE_SCALAR_STMTS (node)[perm.second]);
3674 : : }
3675 : :
3676 : 20 : if (dump_enabled_p ())
3677 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
3678 : : "converting stmts on permute node %p\n",
3679 : : (void *) root);
3680 : :
3681 : 20 : bool *matches = XALLOCAVEC (bool, group_size);
3682 : 20 : poly_uint64 max_nunits = 1;
3683 : 20 : unsigned tree_size = 0, limit = 1;
3684 : 20 : node = vect_build_slp_tree (vinfo, stmts, group_size, &max_nunits,
3685 : : matches, &limit, &tree_size, bst_map);
3686 : 20 : if (!node)
3687 : 0 : stmts.release ();
3688 : :
3689 : 20 : load_map->put (root, node);
3690 : 20 : return node;
3691 : : }
3692 : :
3693 : 13045 : next:
3694 : 13091 : load_map->put (root, NULL);
3695 : :
3696 : 30725 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3697 : : {
3698 : 17634 : slp_tree value
3699 : 17634 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3700 : : node);
3701 : 17634 : if (value)
3702 : : {
3703 : 20 : SLP_TREE_REF_COUNT (value)++;
3704 : 20 : SLP_TREE_CHILDREN (root)[i] = value;
3705 : : /* ??? We know the original leafs of the replaced nodes will
3706 : : be referenced by bst_map, only the permutes created by
3707 : : pattern matching are not. */
3708 : 20 : if (SLP_TREE_REF_COUNT (node) == 1)
3709 : 20 : load_map->remove (node);
3710 : 20 : vect_free_slp_tree (node);
3711 : : }
3712 : : }
3713 : :
3714 : : return NULL;
3715 : : }
3716 : :
3717 : : /* Temporary workaround for loads not being CSEd during SLP build. This
3718 : : function will traverse the SLP tree rooted in ROOT for INSTANCE and find
3719 : : VEC_PERM nodes that blend vectors from multiple nodes that all read from the
3720 : : same DR such that the final operation is equal to a permuted load. Such
3721 : : NODES are then directly converted into LOADS themselves. The nodes are
3722 : : CSEd using BST_MAP. */
3723 : :
3724 : : static void
3725 : 2896 : optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t *bst_map,
3726 : : vec_info *vinfo, unsigned int group_size,
3727 : : hash_map<slp_tree, slp_tree> *load_map,
3728 : : slp_tree root)
3729 : : {
3730 : 2896 : slp_tree node;
3731 : 2896 : unsigned i;
3732 : :
3733 : 6764 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3734 : : {
3735 : 3868 : slp_tree value
3736 : 3868 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3737 : : node);
3738 : 3868 : if (value)
3739 : : {
3740 : 0 : SLP_TREE_REF_COUNT (value)++;
3741 : 0 : SLP_TREE_CHILDREN (root)[i] = value;
3742 : : /* ??? We know the original leafs of the replaced nodes will
3743 : : be referenced by bst_map, only the permutes created by
3744 : : pattern matching are not. */
3745 : 0 : if (SLP_TREE_REF_COUNT (node) == 1)
3746 : 0 : load_map->remove (node);
3747 : 0 : vect_free_slp_tree (node);
3748 : : }
3749 : : }
3750 : 2896 : }
3751 : :
3752 : : /* Helper function of vect_match_slp_patterns.
3753 : :
3754 : : Attempts to match patterns against the slp tree rooted in REF_NODE using
3755 : : VINFO. Patterns are matched in post-order traversal.
3756 : :
3757 : : If matching is successful the value in REF_NODE is updated and returned, if
3758 : : not then it is returned unchanged. */
3759 : :
3760 : : static bool
3761 : 6626914 : vect_match_slp_patterns_2 (slp_tree *ref_node, vec_info *vinfo,
3762 : : slp_tree_to_load_perm_map_t *perm_cache,
3763 : : slp_compat_nodes_map_t *compat_cache,
3764 : : hash_set<slp_tree> *visited)
3765 : : {
3766 : 6626914 : unsigned i;
3767 : 6626914 : slp_tree node = *ref_node;
3768 : 6626914 : bool found_p = false;
3769 : 6626914 : if (!node || visited->add (node))
3770 : 1224067 : return false;
3771 : :
3772 : : slp_tree child;
3773 : 10291458 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3774 : 4888611 : found_p |= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node)[i],
3775 : : vinfo, perm_cache, compat_cache,
3776 : : visited);
3777 : :
3778 : 16208541 : for (unsigned x = 0; x < num__slp_patterns; x++)
3779 : : {
3780 : 10805694 : vect_pattern *pattern
3781 : 10805694 : = slp_patterns[x] (perm_cache, compat_cache, ref_node);
3782 : 10805694 : if (pattern)
3783 : : {
3784 : 1090 : pattern->build (vinfo);
3785 : 1090 : delete pattern;
3786 : 1090 : found_p = true;
3787 : : }
3788 : : }
3789 : :
3790 : : return found_p;
3791 : : }
3792 : :
3793 : : /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
3794 : : vec_info VINFO.
3795 : :
3796 : : The modified tree is returned. Patterns are tried in order and multiple
3797 : : patterns may match. */
3798 : :
3799 : : static bool
3800 : 1738303 : vect_match_slp_patterns (slp_instance instance, vec_info *vinfo,
3801 : : hash_set<slp_tree> *visited,
3802 : : slp_tree_to_load_perm_map_t *perm_cache,
3803 : : slp_compat_nodes_map_t *compat_cache)
3804 : : {
3805 : 1738303 : DUMP_VECT_SCOPE ("vect_match_slp_patterns");
3806 : 1738303 : slp_tree *ref_node = &SLP_INSTANCE_TREE (instance);
3807 : :
3808 : 1738303 : if (dump_enabled_p ())
3809 : 32264 : dump_printf_loc (MSG_NOTE, vect_location,
3810 : : "Analyzing SLP tree %p for patterns\n",
3811 : 32264 : (void *) SLP_INSTANCE_TREE (instance));
3812 : :
3813 : 1738303 : return vect_match_slp_patterns_2 (ref_node, vinfo, perm_cache, compat_cache,
3814 : 1738303 : visited);
3815 : : }
3816 : :
3817 : : /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
3818 : : vectorizing with VECTYPE that might be NULL. MASKED_P indicates whether
3819 : : the stores are masked.
3820 : : Return true if we could use IFN_STORE_LANES instead and if that appears
3821 : : to be the better approach. */
3822 : :
3823 : : static bool
3824 : 4927 : vect_slp_prefer_store_lanes_p (vec_info *vinfo, stmt_vec_info stmt_info,
3825 : : tree vectype, bool masked_p,
3826 : : unsigned int group_size,
3827 : : unsigned int new_group_size)
3828 : : {
3829 : 4927 : if (!vectype)
3830 : : {
3831 : 4927 : tree scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
3832 : 4927 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
3833 : : }
3834 : 4927 : if (!vectype)
3835 : : return false;
3836 : : /* Allow the split if one of the two new groups would operate on full
3837 : : vectors *within* rather than across one scalar loop iteration.
3838 : : This is purely a heuristic, but it should work well for group
3839 : : sizes of 3 and 4, where the possible splits are:
3840 : :
3841 : : 3->2+1: OK if the vector has exactly two elements
3842 : : 4->2+2: Likewise
3843 : : 4->3+1: Less clear-cut. */
3844 : 4927 : if (multiple_p (group_size - new_group_size, TYPE_VECTOR_SUBPARTS (vectype))
3845 : 2558 : || multiple_p (new_group_size, TYPE_VECTOR_SUBPARTS (vectype)))
3846 : 2420 : return false;
3847 : 2507 : return vect_store_lanes_supported (vectype, group_size, masked_p) != IFN_LAST;
3848 : : }
3849 : :
3850 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
3851 : : vect_build_slp_tree to build a tree of packed stmts if possible.
3852 : : Return FALSE if it's impossible to SLP any stmt in the loop. */
3853 : :
3854 : : static bool
3855 : : vect_analyze_slp_instance (vec_info *vinfo,
3856 : : scalar_stmts_to_slp_tree_map_t *bst_map,
3857 : : stmt_vec_info stmt_info, slp_instance_kind kind,
3858 : : unsigned max_tree_size, unsigned *limit,
3859 : : bool force_single_lane);
3860 : :
3861 : : /* Build an interleaving scheme for the store sources RHS_NODES from
3862 : : SCALAR_STMTS. */
3863 : :
3864 : : static slp_tree
3865 : 6411 : vect_build_slp_store_interleaving (vec<slp_tree> &rhs_nodes,
3866 : : vec<stmt_vec_info> &scalar_stmts,
3867 : : poly_uint64 max_nunits)
3868 : : {
3869 : 6411 : unsigned int group_size = scalar_stmts.length ();
3870 : 12822 : slp_tree node = vect_create_new_slp_node (scalar_stmts,
3871 : 6411 : SLP_TREE_CHILDREN
3872 : : (rhs_nodes[0]).length ());
3873 : 6411 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
3874 : 6411 : node->max_nunits = max_nunits;
3875 : 6411 : for (unsigned l = 0;
3876 : 12849 : l < SLP_TREE_CHILDREN (rhs_nodes[0]).length (); ++l)
3877 : : {
3878 : : /* And a permute merging all RHS SLP trees. */
3879 : 6438 : slp_tree perm = vect_create_new_slp_node (rhs_nodes.length (),
3880 : 6438 : VEC_PERM_EXPR);
3881 : 6438 : SLP_TREE_CHILDREN (node).quick_push (perm);
3882 : 6438 : SLP_TREE_LANE_PERMUTATION (perm).create (group_size);
3883 : 6438 : SLP_TREE_VECTYPE (perm) = SLP_TREE_VECTYPE (node);
3884 : 6438 : perm->max_nunits = max_nunits;
3885 : 6438 : SLP_TREE_LANES (perm) = group_size;
3886 : : /* ??? We should set this NULL but that's not expected. */
3887 : 6438 : SLP_TREE_REPRESENTATIVE (perm)
3888 : 6438 : = SLP_TREE_REPRESENTATIVE (SLP_TREE_CHILDREN (rhs_nodes[0])[l]);
3889 : 25218 : for (unsigned j = 0; j < rhs_nodes.length (); ++j)
3890 : : {
3891 : 18780 : SLP_TREE_CHILDREN (perm)
3892 : 18780 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[l]);
3893 : 18780 : SLP_TREE_CHILDREN (rhs_nodes[j])[l]->refcnt++;
3894 : 18780 : for (unsigned k = 0;
3895 : 39624 : k < SLP_TREE_SCALAR_STMTS (rhs_nodes[j]).length (); ++k)
3896 : : {
3897 : : /* ??? We should populate SLP_TREE_SCALAR_STMTS
3898 : : or SLP_TREE_SCALAR_OPS but then we might have
3899 : : a mix of both in our children. */
3900 : 20844 : SLP_TREE_LANE_PERMUTATION (perm)
3901 : 20844 : .quick_push (std::make_pair (j, k));
3902 : : }
3903 : : }
3904 : :
3905 : : /* Now we have a single permute node but we cannot code-generate
3906 : : the case with more than two inputs.
3907 : : Perform pairwise reduction, reducing the two inputs
3908 : : with the least number of lanes to one and then repeat until
3909 : : we end up with two inputs. That scheme makes sure we end
3910 : : up with permutes satisfying the restriction of requiring at
3911 : : most two vector inputs to produce a single vector output
3912 : : when the number of lanes is even. */
3913 : 12342 : while (SLP_TREE_CHILDREN (perm).length () > 2)
3914 : : {
3915 : : /* When we have three equal sized groups left the pairwise
3916 : : reduction does not result in a scheme that avoids using
3917 : : three vectors. Instead merge the first two groups
3918 : : to the final size with do-not-care elements (chosen
3919 : : from the first group) and then merge with the third.
3920 : : { A0, B0, x, A1, B1, x, ... }
3921 : : -> { A0, B0, C0, A1, B1, C1, ... }
3922 : : This handles group size of three (and at least
3923 : : power-of-two multiples of that). */
3924 : 5904 : if (SLP_TREE_CHILDREN (perm).length () == 3
3925 : 3061 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3926 : 3061 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[1]))
3927 : 5904 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3928 : 2318 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[2])))
3929 : : {
3930 : 2122 : int ai = 0;
3931 : 2122 : int bi = 1;
3932 : 2122 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
3933 : 2122 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
3934 : 2122 : unsigned n = SLP_TREE_LANES (perm);
3935 : :
3936 : 2122 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
3937 : 2122 : SLP_TREE_LANES (permab) = n;
3938 : 2122 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
3939 : 2122 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
3940 : 2122 : permab->max_nunits = max_nunits;
3941 : : /* ??? Should be NULL but that's not expected. */
3942 : 2122 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
3943 : 2122 : SLP_TREE_CHILDREN (permab).quick_push (a);
3944 : 4253 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3945 : 2131 : SLP_TREE_LANE_PERMUTATION (permab)
3946 : 2131 : .quick_push (std::make_pair (0, k));
3947 : 2122 : SLP_TREE_CHILDREN (permab).quick_push (b);
3948 : 4253 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
3949 : 2131 : SLP_TREE_LANE_PERMUTATION (permab)
3950 : 2131 : .quick_push (std::make_pair (1, k));
3951 : : /* Push the do-not-care lanes. */
3952 : 4253 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3953 : 2131 : SLP_TREE_LANE_PERMUTATION (permab)
3954 : 2131 : .quick_push (std::make_pair (0, k));
3955 : :
3956 : : /* Put the merged node into 'perm', in place of a. */
3957 : 2122 : SLP_TREE_CHILDREN (perm)[ai] = permab;
3958 : : /* Adjust the references to b in the permutation
3959 : : of perm and to the later children which we'll
3960 : : remove. */
3961 : 8515 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
3962 : : {
3963 : 6393 : std::pair<unsigned, unsigned> &p
3964 : 6393 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
3965 : 6393 : if (p.first == (unsigned) bi)
3966 : : {
3967 : 2131 : p.first = ai;
3968 : 2131 : p.second += SLP_TREE_LANES (a);
3969 : : }
3970 : 4262 : else if (p.first > (unsigned) bi)
3971 : 2131 : p.first--;
3972 : : }
3973 : 2122 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
3974 : 2122 : break;
3975 : : }
3976 : :
3977 : : /* Pick the two nodes with the least number of lanes,
3978 : : prefer the earliest candidate and maintain ai < bi. */
3979 : : int ai = -1;
3980 : : int bi = -1;
3981 : 33073 : for (unsigned ci = 0; ci < SLP_TREE_CHILDREN (perm).length (); ++ci)
3982 : : {
3983 : 29291 : if (ai == -1)
3984 : 3782 : ai = ci;
3985 : 25509 : else if (bi == -1)
3986 : 3782 : bi = ci;
3987 : 21727 : else if ((SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
3988 : 21727 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai]))
3989 : 21727 : || (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
3990 : 17813 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi])))
3991 : : {
3992 : 8716 : if (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai])
3993 : 4358 : <= SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi]))
3994 : 2075 : bi = ci;
3995 : : else
3996 : : {
3997 : 2283 : ai = bi;
3998 : 2283 : bi = ci;
3999 : : }
4000 : : }
4001 : : }
4002 : :
4003 : : /* Produce a merge of nodes ai and bi. */
4004 : 3782 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
4005 : 3782 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
4006 : 3782 : unsigned n = SLP_TREE_LANES (a) + SLP_TREE_LANES (b);
4007 : 3782 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
4008 : 3782 : SLP_TREE_LANES (permab) = n;
4009 : 3782 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
4010 : 3782 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
4011 : 3782 : permab->max_nunits = max_nunits;
4012 : : /* ??? Should be NULL but that's not expected. */
4013 : 3782 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
4014 : 3782 : SLP_TREE_CHILDREN (permab).quick_push (a);
4015 : 9888 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
4016 : 6106 : SLP_TREE_LANE_PERMUTATION (permab)
4017 : 6106 : .quick_push (std::make_pair (0, k));
4018 : 3782 : SLP_TREE_CHILDREN (permab).quick_push (b);
4019 : 9400 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
4020 : 5618 : SLP_TREE_LANE_PERMUTATION (permab)
4021 : 5618 : .quick_push (std::make_pair (1, k));
4022 : :
4023 : : /* Put the merged node into 'perm', in place of a. */
4024 : 3782 : SLP_TREE_CHILDREN (perm)[ai] = permab;
4025 : : /* Adjust the references to b in the permutation
4026 : : of perm and to the later children which we'll
4027 : : remove. */
4028 : 52699 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
4029 : : {
4030 : 48917 : std::pair<unsigned, unsigned> &p
4031 : 48917 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
4032 : 48917 : if (p.first == (unsigned) bi)
4033 : : {
4034 : 5618 : p.first = ai;
4035 : 5618 : p.second += SLP_TREE_LANES (a);
4036 : : }
4037 : 43299 : else if (p.first > (unsigned) bi)
4038 : 17862 : p.first--;
4039 : : }
4040 : 3782 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
4041 : : }
4042 : : }
4043 : :
4044 : 6411 : return node;
4045 : : }
4046 : :
4047 : : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4048 : : of KIND. Return true if successful. */
4049 : :
4050 : : static bool
4051 : 2108129 : vect_build_slp_instance (vec_info *vinfo,
4052 : : slp_instance_kind kind,
4053 : : vec<stmt_vec_info> &scalar_stmts,
4054 : : vec<stmt_vec_info> &root_stmt_infos,
4055 : : vec<tree> &remain,
4056 : : unsigned max_tree_size, unsigned *limit,
4057 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4058 : : bool force_single_lane)
4059 : : {
4060 : : /* If there's no budget left bail out early. */
4061 : 2108129 : if (*limit == 0)
4062 : : return false;
4063 : :
4064 : 2080912 : if (kind == slp_inst_kind_ctor)
4065 : : {
4066 : 11658 : if (dump_enabled_p ())
4067 : 54 : dump_printf_loc (MSG_NOTE, vect_location,
4068 : : "Analyzing vectorizable constructor: %G\n",
4069 : 27 : root_stmt_infos[0]->stmt);
4070 : : }
4071 : 2069254 : else if (kind == slp_inst_kind_gcond)
4072 : : {
4073 : 248084 : if (dump_enabled_p ())
4074 : 5268 : dump_printf_loc (MSG_NOTE, vect_location,
4075 : : "Analyzing vectorizable control flow: %G",
4076 : 2634 : root_stmt_infos[0]->stmt);
4077 : : }
4078 : :
4079 : 2080912 : if (dump_enabled_p ())
4080 : : {
4081 : 28047 : dump_printf_loc (MSG_NOTE, vect_location,
4082 : : "Starting SLP discovery for\n");
4083 : 59889 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4084 : 63684 : dump_printf_loc (MSG_NOTE, vect_location,
4085 : 31842 : " %G", scalar_stmts[i]->stmt);
4086 : : }
4087 : :
4088 : : /* Build the tree for the SLP instance. */
4089 : 2080912 : unsigned int group_size = scalar_stmts.length ();
4090 : 2080912 : bool *matches = XALLOCAVEC (bool, group_size);
4091 : 2080912 : poly_uint64 max_nunits = 1;
4092 : 2080912 : unsigned tree_size = 0;
4093 : :
4094 : 2080912 : slp_tree node = NULL;
4095 : 2080912 : if (group_size > 1 && force_single_lane)
4096 : : {
4097 : 0 : matches[0] = true;
4098 : 0 : matches[1] = false;
4099 : : }
4100 : : else
4101 : 2080912 : node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4102 : : &max_nunits, matches, limit,
4103 : : &tree_size, bst_map);
4104 : 2080912 : if (node != NULL)
4105 : : {
4106 : : /* Calculate the unrolling factor based on the smallest type. */
4107 : 1004034 : poly_uint64 unrolling_factor
4108 : 1004034 : = calculate_unrolling_factor (max_nunits, group_size);
4109 : :
4110 : 1004034 : if (maybe_ne (unrolling_factor, 1U)
4111 : 1004034 : && is_a <bb_vec_info> (vinfo))
4112 : : {
4113 : 0 : unsigned HOST_WIDE_INT const_max_nunits;
4114 : 0 : if (!max_nunits.is_constant (&const_max_nunits)
4115 : 0 : || const_max_nunits > group_size)
4116 : : {
4117 : 0 : if (dump_enabled_p ())
4118 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4119 : : "Build SLP failed: store group "
4120 : : "size not a multiple of the vector size "
4121 : : "in basic block SLP\n");
4122 : 0 : vect_free_slp_tree (node);
4123 : 0 : return false;
4124 : : }
4125 : : /* Fatal mismatch. */
4126 : 0 : if (dump_enabled_p ())
4127 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
4128 : : "SLP discovery succeeded but node needs "
4129 : : "splitting\n");
4130 : 0 : memset (matches, true, group_size);
4131 : 0 : matches[group_size / const_max_nunits * const_max_nunits] = false;
4132 : 0 : vect_free_slp_tree (node);
4133 : : }
4134 : : else
4135 : : {
4136 : : /* Create a new SLP instance. */
4137 : 1004034 : slp_instance new_instance = XNEW (class _slp_instance);
4138 : 1004034 : SLP_INSTANCE_TREE (new_instance) = node;
4139 : 1004034 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4140 : 1004034 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4141 : 1004034 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4142 : 1004034 : SLP_INSTANCE_KIND (new_instance) = kind;
4143 : 1004034 : new_instance->reduc_phis = NULL;
4144 : 1004034 : new_instance->cost_vec = vNULL;
4145 : 1004034 : new_instance->subgraph_entries = vNULL;
4146 : :
4147 : 1004034 : if (dump_enabled_p ())
4148 : 24960 : dump_printf_loc (MSG_NOTE, vect_location,
4149 : : "SLP size %u vs. limit %u.\n",
4150 : : tree_size, max_tree_size);
4151 : :
4152 : 1004034 : vinfo->slp_instances.safe_push (new_instance);
4153 : :
4154 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4155 : : the number of scalar stmts in the root in a few places.
4156 : : Verify that assumption holds. */
4157 : 2008068 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4158 : : .length () == group_size);
4159 : :
4160 : 1004034 : if (dump_enabled_p ())
4161 : : {
4162 : 24960 : dump_printf_loc (MSG_NOTE, vect_location,
4163 : : "Final SLP tree for instance %p:\n",
4164 : : (void *) new_instance);
4165 : 24960 : vect_print_slp_graph (MSG_NOTE, vect_location,
4166 : : SLP_INSTANCE_TREE (new_instance));
4167 : : }
4168 : :
4169 : 1004034 : return true;
4170 : : }
4171 : : }
4172 : : /* Failed to SLP. */
4173 : :
4174 : : /* While we arrive here even with slp_inst_kind_store we should only
4175 : : for group_size == 1. The code to split store groups is only in
4176 : : vect_analyze_slp_instance now. */
4177 : 1076878 : gcc_assert (kind != slp_inst_kind_store || group_size == 1);
4178 : :
4179 : : /* Free the allocated memory. */
4180 : 1076878 : scalar_stmts.release ();
4181 : :
4182 : : /* Failed to SLP. */
4183 : 1076878 : if (dump_enabled_p ())
4184 : 3087 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4185 : : return false;
4186 : : }
4187 : :
4188 : : /* Analyze an SLP instance starting from a the start of a reduction chain.
4189 : : Call vect_build_slp_tree to build a tree of packed stmts if possible.
4190 : : Return FALSE if SLP build fails. */
4191 : :
4192 : : static bool
4193 : 50135 : vect_analyze_slp_reduc_chain (loop_vec_info vinfo,
4194 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4195 : : stmt_vec_info scalar_stmt,
4196 : : unsigned max_tree_size, unsigned *limit)
4197 : : {
4198 : 50135 : vec<stmt_vec_info> scalar_stmts = vNULL;
4199 : :
4200 : 50135 : bool fail = false;
4201 : : /* ??? We could leave operation code checking to SLP discovery. */
4202 : 50135 : code_helper code = STMT_VINFO_REDUC_CODE (STMT_VINFO_REDUC_DEF
4203 : : (vect_orig_stmt (scalar_stmt)));
4204 : 50135 : bool first = true;
4205 : 50135 : stmt_vec_info next_stmt = scalar_stmt;
4206 : 58454 : do
4207 : : {
4208 : 58454 : stmt_vec_info stmt = next_stmt;
4209 : 58454 : gimple_match_op op;
4210 : 58454 : if (!gimple_extract_op (STMT_VINFO_STMT (stmt), &op))
4211 : 0 : gcc_unreachable ();
4212 : 116908 : tree reduc_def = gimple_arg (STMT_VINFO_STMT (stmt),
4213 : 58454 : STMT_VINFO_REDUC_IDX (stmt));
4214 : 58454 : next_stmt = vect_stmt_to_vectorize (vinfo->lookup_def (reduc_def));
4215 : 58454 : gcc_assert (is_a <gphi *> (STMT_VINFO_STMT (next_stmt))
4216 : : || STMT_VINFO_REDUC_IDX (next_stmt) != -1);
4217 : 63524 : if (!gimple_extract_op (STMT_VINFO_STMT (vect_orig_stmt (stmt)), &op))
4218 : 0 : gcc_unreachable ();
4219 : 58454 : if (CONVERT_EXPR_CODE_P (op.code)
4220 : 58454 : && (first
4221 : 1562 : || is_a <gphi *> (STMT_VINFO_STMT (next_stmt))))
4222 : : ;
4223 : 55311 : else if (code != op.code)
4224 : : {
4225 : 2721 : fail = true;
4226 : 2721 : break;
4227 : : }
4228 : : else
4229 : 52590 : scalar_stmts.safe_push (stmt);
4230 : 55733 : first = false;
4231 : : }
4232 : 55733 : while (!is_a <gphi *> (STMT_VINFO_STMT (next_stmt)));
4233 : 50135 : if (fail || scalar_stmts.length () <= 1)
4234 : 46857 : return false;
4235 : :
4236 : 3278 : scalar_stmts.reverse ();
4237 : 3278 : stmt_vec_info reduc_phi_info = next_stmt;
4238 : :
4239 : : /* Build the tree for the SLP instance. */
4240 : 3278 : vec<stmt_vec_info> root_stmt_infos = vNULL;
4241 : 3278 : vec<tree> remain = vNULL;
4242 : :
4243 : 3278 : if (dump_enabled_p ())
4244 : : {
4245 : 317 : dump_printf_loc (MSG_NOTE, vect_location,
4246 : : "Starting SLP discovery of reduction chain for\n");
4247 : 1402 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4248 : 2170 : dump_printf_loc (MSG_NOTE, vect_location,
4249 : 1085 : " %G", scalar_stmts[i]->stmt);
4250 : : }
4251 : :
4252 : : /* Build the tree for the SLP instance. */
4253 : 3278 : unsigned int group_size = scalar_stmts.length ();
4254 : 3278 : bool *matches = XALLOCAVEC (bool, group_size);
4255 : 3278 : poly_uint64 max_nunits = 1;
4256 : 3278 : unsigned tree_size = 0;
4257 : :
4258 : : /* ??? We need this only for SLP discovery. */
4259 : 11698 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4260 : 8420 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = scalar_stmts[0];
4261 : :
4262 : 3278 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4263 : : &max_nunits, matches, limit,
4264 : 3278 : &tree_size, bst_map);
4265 : :
4266 : 11698 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4267 : 8420 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = NULL;
4268 : :
4269 : 3278 : if (node != NULL)
4270 : : {
4271 : : /* Create a new SLP instance. */
4272 : 2348 : slp_instance new_instance = XNEW (class _slp_instance);
4273 : 2348 : SLP_INSTANCE_TREE (new_instance) = node;
4274 : 2348 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4275 : 2348 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4276 : 2348 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4277 : 2348 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_chain;
4278 : 2348 : new_instance->reduc_phis = NULL;
4279 : 2348 : new_instance->cost_vec = vNULL;
4280 : 2348 : new_instance->subgraph_entries = vNULL;
4281 : :
4282 : 2348 : vect_reduc_info reduc_info = info_for_reduction (vinfo, node);
4283 : 2348 : reduc_info->is_reduc_chain = true;
4284 : :
4285 : 2348 : if (dump_enabled_p ())
4286 : 140 : dump_printf_loc (MSG_NOTE, vect_location,
4287 : : "SLP size %u vs. limit %u.\n",
4288 : : tree_size, max_tree_size);
4289 : :
4290 : : /* Fixup SLP reduction chains. If this is a reduction chain with
4291 : : a conversion in front amend the SLP tree with a node for that. */
4292 : 2348 : gimple *scalar_def = STMT_VINFO_REDUC_DEF (reduc_phi_info)->stmt;
4293 : 2348 : if (is_gimple_assign (scalar_def)
4294 : 2348 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (scalar_def)))
4295 : : {
4296 : 26 : stmt_vec_info conv_info = vect_stmt_to_vectorize
4297 : 26 : (STMT_VINFO_REDUC_DEF (reduc_phi_info));
4298 : 26 : scalar_stmts = vNULL;
4299 : 26 : scalar_stmts.create (group_size);
4300 : 84 : for (unsigned i = 0; i < group_size; ++i)
4301 : 58 : scalar_stmts.quick_push (conv_info);
4302 : 26 : slp_tree conv = vect_create_new_slp_node (scalar_stmts, 1);
4303 : 26 : SLP_TREE_VECTYPE (conv)
4304 : 26 : = get_vectype_for_scalar_type (vinfo,
4305 : 26 : TREE_TYPE
4306 : : (gimple_assign_lhs (scalar_def)),
4307 : : group_size);
4308 : 26 : SLP_TREE_REDUC_IDX (conv) = 0;
4309 : 26 : conv->cycle_info.id = node->cycle_info.id;
4310 : 26 : SLP_TREE_CHILDREN (conv).quick_push (node);
4311 : 26 : SLP_INSTANCE_TREE (new_instance) = conv;
4312 : : }
4313 : : /* Fill the backedge child of the PHI SLP node. The
4314 : : general matching code cannot find it because the
4315 : : scalar code does not reflect how we vectorize the
4316 : : reduction. */
4317 : 2348 : use_operand_p use_p;
4318 : 2348 : imm_use_iterator imm_iter;
4319 : 2348 : class loop *loop = LOOP_VINFO_LOOP (vinfo);
4320 : 9098 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
4321 : : gimple_get_lhs (scalar_def))
4322 : : /* There are exactly two non-debug uses, the reduction
4323 : : PHI and the loop-closed PHI node. */
4324 : 6750 : if (!is_gimple_debug (USE_STMT (use_p))
4325 : 6750 : && gimple_bb (USE_STMT (use_p)) == loop->header)
4326 : : {
4327 : 2348 : auto_vec<stmt_vec_info, 64> phis (group_size);
4328 : 2348 : stmt_vec_info phi_info = vinfo->lookup_stmt (USE_STMT (use_p));
4329 : 8317 : for (unsigned i = 0; i < group_size; ++i)
4330 : 5969 : phis.quick_push (phi_info);
4331 : 2348 : slp_tree *phi_node = bst_map->get (phis);
4332 : 2348 : unsigned dest_idx = loop_latch_edge (loop)->dest_idx;
4333 : 4696 : SLP_TREE_CHILDREN (*phi_node)[dest_idx]
4334 : 2348 : = SLP_INSTANCE_TREE (new_instance);
4335 : 2348 : SLP_INSTANCE_TREE (new_instance)->refcnt++;
4336 : 2348 : }
4337 : :
4338 : 2348 : vinfo->slp_instances.safe_push (new_instance);
4339 : :
4340 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4341 : : the number of scalar stmts in the root in a few places.
4342 : : Verify that assumption holds. */
4343 : 4696 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4344 : : .length () == group_size);
4345 : :
4346 : 2348 : if (dump_enabled_p ())
4347 : : {
4348 : 140 : dump_printf_loc (MSG_NOTE, vect_location,
4349 : : "Final SLP tree for instance %p:\n",
4350 : : (void *) new_instance);
4351 : 140 : vect_print_slp_graph (MSG_NOTE, vect_location,
4352 : : SLP_INSTANCE_TREE (new_instance));
4353 : : }
4354 : :
4355 : 2348 : return true;
4356 : : }
4357 : :
4358 : : /* Failed to SLP. */
4359 : 930 : scalar_stmts.release ();
4360 : 930 : if (dump_enabled_p ())
4361 : 177 : dump_printf_loc (MSG_NOTE, vect_location,
4362 : : "SLP discovery of reduction chain failed\n");
4363 : : return false;
4364 : : }
4365 : :
4366 : : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4367 : : of KIND. Return true if successful. */
4368 : :
4369 : : static bool
4370 : 66839 : vect_analyze_slp_reduction (loop_vec_info vinfo,
4371 : : stmt_vec_info scalar_stmt,
4372 : : unsigned max_tree_size, unsigned *limit,
4373 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4374 : : bool force_single_lane)
4375 : : {
4376 : 66839 : slp_instance_kind kind = slp_inst_kind_reduc_group;
4377 : :
4378 : : /* If there's no budget left bail out early. */
4379 : 66839 : if (*limit == 0)
4380 : : return false;
4381 : :
4382 : : /* Try to gather a reduction chain. */
4383 : 66839 : if (! force_single_lane
4384 : 50358 : && STMT_VINFO_DEF_TYPE (scalar_stmt) == vect_reduction_def
4385 : 116974 : && vect_analyze_slp_reduc_chain (vinfo, bst_map, scalar_stmt,
4386 : : max_tree_size, limit))
4387 : : return true;
4388 : :
4389 : 64491 : vec<stmt_vec_info> scalar_stmts;
4390 : 64491 : scalar_stmts.create (1);
4391 : 64491 : scalar_stmts.quick_push (scalar_stmt);
4392 : :
4393 : 64491 : if (dump_enabled_p ())
4394 : : {
4395 : 3583 : dump_printf_loc (MSG_NOTE, vect_location,
4396 : : "Starting SLP discovery for\n");
4397 : 7166 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4398 : 7166 : dump_printf_loc (MSG_NOTE, vect_location,
4399 : 3583 : " %G", scalar_stmts[i]->stmt);
4400 : : }
4401 : :
4402 : : /* Build the tree for the SLP instance. */
4403 : 64491 : unsigned int group_size = scalar_stmts.length ();
4404 : 64491 : bool *matches = XALLOCAVEC (bool, group_size);
4405 : 64491 : poly_uint64 max_nunits = 1;
4406 : 64491 : unsigned tree_size = 0;
4407 : :
4408 : 64491 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4409 : : &max_nunits, matches, limit,
4410 : : &tree_size, bst_map);
4411 : 64491 : if (node != NULL)
4412 : : {
4413 : : /* Create a new SLP instance. */
4414 : 62251 : slp_instance new_instance = XNEW (class _slp_instance);
4415 : 62251 : SLP_INSTANCE_TREE (new_instance) = node;
4416 : 62251 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4417 : 62251 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4418 : 62251 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4419 : 62251 : SLP_INSTANCE_KIND (new_instance) = kind;
4420 : 62251 : new_instance->reduc_phis = NULL;
4421 : 62251 : new_instance->cost_vec = vNULL;
4422 : 62251 : new_instance->subgraph_entries = vNULL;
4423 : :
4424 : 62251 : if (dump_enabled_p ())
4425 : 3477 : dump_printf_loc (MSG_NOTE, vect_location,
4426 : : "SLP size %u vs. limit %u.\n",
4427 : : tree_size, max_tree_size);
4428 : :
4429 : 62251 : vinfo->slp_instances.safe_push (new_instance);
4430 : :
4431 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4432 : : the number of scalar stmts in the root in a few places.
4433 : : Verify that assumption holds. */
4434 : 124502 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4435 : : .length () == group_size);
4436 : :
4437 : 62251 : if (dump_enabled_p ())
4438 : : {
4439 : 3477 : dump_printf_loc (MSG_NOTE, vect_location,
4440 : : "Final SLP tree for instance %p:\n",
4441 : : (void *) new_instance);
4442 : 3477 : vect_print_slp_graph (MSG_NOTE, vect_location,
4443 : : SLP_INSTANCE_TREE (new_instance));
4444 : : }
4445 : :
4446 : 62251 : return true;
4447 : : }
4448 : : /* Failed to SLP. */
4449 : :
4450 : : /* Free the allocated memory. */
4451 : 2240 : scalar_stmts.release ();
4452 : :
4453 : : /* Failed to SLP. */
4454 : 2240 : if (dump_enabled_p ())
4455 : 106 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4456 : : return false;
4457 : : }
4458 : :
4459 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
4460 : : vect_build_slp_tree to build a tree of packed stmts if possible.
4461 : : Return FALSE if it's impossible to SLP any stmt in the group. */
4462 : :
4463 : : static bool
4464 : 1084220 : vect_analyze_slp_instance (vec_info *vinfo,
4465 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4466 : : stmt_vec_info stmt_info,
4467 : : slp_instance_kind kind,
4468 : : unsigned max_tree_size, unsigned *limit,
4469 : : bool force_single_lane)
4470 : : {
4471 : 1084220 : vec<stmt_vec_info> scalar_stmts;
4472 : :
4473 : 1084220 : if (is_a <bb_vec_info> (vinfo))
4474 : 1062331 : vect_location = stmt_info->stmt;
4475 : :
4476 : 1084220 : gcc_assert (kind == slp_inst_kind_store);
4477 : :
4478 : : /* Collect the stores and store them in scalar_stmts. */
4479 : 1084220 : scalar_stmts.create (DR_GROUP_SIZE (stmt_info));
4480 : 1084220 : stmt_vec_info next_info = stmt_info;
4481 : 5387924 : while (next_info)
4482 : : {
4483 : 3219484 : scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
4484 : 3219484 : next_info = DR_GROUP_NEXT_ELEMENT (next_info);
4485 : : }
4486 : :
4487 : 1084220 : vec<stmt_vec_info> root_stmt_infos = vNULL;
4488 : 1084220 : vec<tree> remain = vNULL;
4489 : :
4490 : : /* Build the tree for the SLP instance. */
4491 : :
4492 : : /* If there's no budget left bail out early. */
4493 : 1084220 : if (*limit == 0)
4494 : : return false;
4495 : :
4496 : 1084197 : if (dump_enabled_p ())
4497 : : {
4498 : 3933 : dump_printf_loc (MSG_NOTE, vect_location,
4499 : : "Starting SLP discovery for\n");
4500 : 22549 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4501 : 37232 : dump_printf_loc (MSG_NOTE, vect_location,
4502 : 18616 : " %G", scalar_stmts[i]->stmt);
4503 : : }
4504 : :
4505 : : /* Build the tree for the SLP instance. */
4506 : 1084197 : unsigned int group_size = scalar_stmts.length ();
4507 : 1084197 : bool *matches = XALLOCAVEC (bool, group_size);
4508 : 1084197 : poly_uint64 max_nunits = 1;
4509 : 1084197 : unsigned tree_size = 0;
4510 : 1084197 : unsigned i;
4511 : :
4512 : 1084197 : slp_tree node = NULL;
4513 : 1084197 : if (group_size > 1 && force_single_lane)
4514 : : {
4515 : 1454 : matches[0] = true;
4516 : 1454 : matches[1] = false;
4517 : : }
4518 : : else
4519 : 1082743 : node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4520 : : &max_nunits, matches, limit,
4521 : : &tree_size, bst_map);
4522 : 1084197 : if (node != NULL)
4523 : : {
4524 : : /* Calculate the unrolling factor based on the smallest type. */
4525 : 676750 : poly_uint64 unrolling_factor
4526 : 676750 : = calculate_unrolling_factor (max_nunits, group_size);
4527 : :
4528 : 676750 : if (maybe_ne (unrolling_factor, 1U)
4529 : 676750 : && is_a <bb_vec_info> (vinfo))
4530 : : {
4531 : 0 : unsigned HOST_WIDE_INT const_max_nunits;
4532 : 0 : if (!max_nunits.is_constant (&const_max_nunits)
4533 : 0 : || const_max_nunits > group_size)
4534 : : {
4535 : 0 : if (dump_enabled_p ())
4536 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4537 : : "Build SLP failed: store group "
4538 : : "size not a multiple of the vector size "
4539 : : "in basic block SLP\n");
4540 : 0 : vect_free_slp_tree (node);
4541 : 0 : return false;
4542 : : }
4543 : : /* Fatal mismatch. */
4544 : 0 : if (dump_enabled_p ())
4545 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
4546 : : "SLP discovery succeeded but node needs "
4547 : : "splitting\n");
4548 : 0 : memset (matches, true, group_size);
4549 : 0 : matches[group_size / const_max_nunits * const_max_nunits] = false;
4550 : 0 : vect_free_slp_tree (node);
4551 : : }
4552 : : else
4553 : : {
4554 : : /* Create a new SLP instance. */
4555 : 676750 : slp_instance new_instance = XNEW (class _slp_instance);
4556 : 676750 : SLP_INSTANCE_TREE (new_instance) = node;
4557 : 676750 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4558 : 676750 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4559 : 676750 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4560 : 676750 : SLP_INSTANCE_KIND (new_instance) = kind;
4561 : 676750 : new_instance->reduc_phis = NULL;
4562 : 676750 : new_instance->cost_vec = vNULL;
4563 : 676750 : new_instance->subgraph_entries = vNULL;
4564 : :
4565 : 676750 : if (dump_enabled_p ())
4566 : 2976 : dump_printf_loc (MSG_NOTE, vect_location,
4567 : : "SLP size %u vs. limit %u.\n",
4568 : : tree_size, max_tree_size);
4569 : :
4570 : 676750 : vinfo->slp_instances.safe_push (new_instance);
4571 : :
4572 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4573 : : the number of scalar stmts in the root in a few places.
4574 : : Verify that assumption holds. */
4575 : 1353500 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4576 : : .length () == group_size);
4577 : :
4578 : 676750 : if (dump_enabled_p ())
4579 : : {
4580 : 2976 : dump_printf_loc (MSG_NOTE, vect_location,
4581 : : "Final SLP tree for instance %p:\n",
4582 : : (void *) new_instance);
4583 : 2976 : vect_print_slp_graph (MSG_NOTE, vect_location,
4584 : : SLP_INSTANCE_TREE (new_instance));
4585 : : }
4586 : :
4587 : 676750 : return true;
4588 : : }
4589 : : }
4590 : : /* Failed to SLP. */
4591 : :
4592 : : /* Try to break the group up into pieces. */
4593 : 407447 : if (*limit > 0 && kind == slp_inst_kind_store)
4594 : : {
4595 : : /* ??? We could delay all the actual splitting of store-groups
4596 : : until after SLP discovery of the original group completed.
4597 : : Then we can recurse to vect_build_slp_instance directly. */
4598 : 1064031 : for (i = 0; i < group_size; i++)
4599 : 1064031 : if (!matches[i])
4600 : : break;
4601 : :
4602 : : /* For basic block SLP, try to break the group up into multiples of
4603 : : a vector size. */
4604 : 407446 : if (is_a <bb_vec_info> (vinfo)
4605 : 407446 : && (i > 1 && i < group_size))
4606 : : {
4607 : : /* Free the allocated memory. */
4608 : 153709 : scalar_stmts.release ();
4609 : :
4610 : 153709 : tree scalar_type
4611 : 153709 : = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
4612 : 307418 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
4613 : 153709 : 1 << floor_log2 (i));
4614 : 153709 : unsigned HOST_WIDE_INT const_nunits;
4615 : 153709 : if (vectype
4616 : 153709 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits))
4617 : : {
4618 : : /* Split into two groups at the first vector boundary. */
4619 : 153709 : gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
4620 : 153709 : unsigned group1_size = i & ~(const_nunits - 1);
4621 : :
4622 : 153709 : if (dump_enabled_p ())
4623 : 51 : dump_printf_loc (MSG_NOTE, vect_location,
4624 : : "Splitting SLP group at stmt %u\n", i);
4625 : 153709 : stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
4626 : : group1_size);
4627 : 153709 : bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info,
4628 : : kind, max_tree_size,
4629 : : limit, false);
4630 : : /* Split the rest at the failure point and possibly
4631 : : re-analyze the remaining matching part if it has
4632 : : at least two lanes. */
4633 : 153709 : if (group1_size < i
4634 : 5224 : && (i + 1 < group_size
4635 : 3066 : || i - group1_size > 1))
4636 : : {
4637 : 2184 : stmt_vec_info rest2 = rest;
4638 : 2184 : rest = vect_split_slp_store_group (rest, i - group1_size);
4639 : 2184 : if (i - group1_size > 1)
4640 : 59 : res |= vect_analyze_slp_instance (vinfo, bst_map, rest2,
4641 : : kind, max_tree_size,
4642 : : limit, false);
4643 : : }
4644 : : /* Re-analyze the non-matching tail if it has at least
4645 : : two lanes. */
4646 : 153709 : if (i + 1 < group_size)
4647 : 22089 : res |= vect_analyze_slp_instance (vinfo, bst_map,
4648 : : rest, kind, max_tree_size,
4649 : : limit, false);
4650 : 153709 : return res;
4651 : : }
4652 : : }
4653 : :
4654 : : /* For loop vectorization split the RHS into arbitrary pieces of
4655 : : size >= 1. */
4656 : 253737 : else if (is_a <loop_vec_info> (vinfo)
4657 : 253737 : && (group_size != 1 && i < group_size))
4658 : : {
4659 : 6656 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
4660 : 27 : bool masked_p = call
4661 : 27 : && gimple_call_internal_p (call)
4662 : 27 : && internal_fn_mask_index (gimple_call_internal_fn (call)) != -1;
4663 : : /* There are targets that cannot do even/odd interleaving schemes
4664 : : so they absolutely need to use load/store-lanes. For now
4665 : : force single-lane SLP for them - they would be happy with
4666 : : uniform power-of-two lanes (but depending on element size),
4667 : : but even if we can use 'i' as indicator we would need to
4668 : : backtrack when later lanes fail to discover with the same
4669 : : granularity. We cannot turn any of strided or scatter store
4670 : : into store-lanes. */
4671 : : /* ??? If this is not in sync with what get_load_store_type
4672 : : later decides the SLP representation is not good for other
4673 : : store vectorization methods. */
4674 : 6656 : bool want_store_lanes
4675 : 6656 : = (! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
4676 : 6656 : && ! STMT_VINFO_STRIDED_P (stmt_info)
4677 : 4953 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
4678 : 4950 : && compare_step_with_zero (vinfo, stmt_info) > 0
4679 : 11583 : && vect_slp_prefer_store_lanes_p (vinfo, stmt_info, NULL_TREE,
4680 : 13312 : masked_p, group_size, i));
4681 : 6656 : if (want_store_lanes || force_single_lane)
4682 : : i = 1;
4683 : :
4684 : : /* A fatal discovery fail doesn't always mean single-lane SLP
4685 : : isn't a possibility, so try. */
4686 : 5202 : if (i == 0)
4687 : : i = 1;
4688 : :
4689 : 6656 : if (dump_enabled_p ())
4690 : 864 : dump_printf_loc (MSG_NOTE, vect_location,
4691 : : "Splitting SLP group at stmt %u\n", i);
4692 : :
4693 : : /* Analyze the stored values and pinch them together with
4694 : : a permute node so we can preserve the whole store group. */
4695 : 6656 : auto_vec<slp_tree> rhs_nodes;
4696 : 6656 : poly_uint64 max_nunits = 1;
4697 : :
4698 : 6656 : unsigned int rhs_common_nlanes = 0;
4699 : 6656 : unsigned int start = 0, end = i;
4700 : 29845 : while (start < group_size)
4701 : : {
4702 : 23434 : gcc_assert (end - start >= 1);
4703 : 23434 : vec<stmt_vec_info> substmts;
4704 : 23434 : substmts.create (end - start);
4705 : 70477 : for (unsigned j = start; j < end; ++j)
4706 : 47043 : substmts.quick_push (scalar_stmts[j]);
4707 : 23434 : max_nunits = 1;
4708 : 23434 : node = vect_build_slp_tree (vinfo, substmts, end - start,
4709 : : &max_nunits,
4710 : : matches, limit, &tree_size, bst_map);
4711 : 23434 : if (node)
4712 : : {
4713 : 18714 : rhs_nodes.safe_push (node);
4714 : 18714 : vect_update_max_nunits (&max_nunits, node->max_nunits);
4715 : 18714 : if (start == 0)
4716 : 6415 : rhs_common_nlanes = SLP_TREE_LANES (node);
4717 : 12299 : else if (rhs_common_nlanes != SLP_TREE_LANES (node))
4718 : 1335 : rhs_common_nlanes = 0;
4719 : 18714 : start = end;
4720 : 18714 : if (want_store_lanes || force_single_lane)
4721 : 4462 : end = start + 1;
4722 : : else
4723 : : end = group_size;
4724 : : }
4725 : : else
4726 : : {
4727 : 4720 : substmts.release ();
4728 : 4720 : if (end - start == 1)
4729 : : {
4730 : : /* Single-lane discovery failed. Free ressources. */
4731 : 259 : for (auto node : rhs_nodes)
4732 : 6 : vect_free_slp_tree (node);
4733 : 245 : scalar_stmts.release ();
4734 : 245 : if (dump_enabled_p ())
4735 : 41 : dump_printf_loc (MSG_NOTE, vect_location,
4736 : : "SLP discovery failed\n");
4737 : 245 : return false;
4738 : : }
4739 : :
4740 : : /* ??? It really happens that we soft-fail SLP
4741 : : build at a mismatch but the matching part hard-fails
4742 : : later. As we know we arrived here with a group
4743 : : larger than one try a group of size one! */
4744 : 4475 : if (!matches[0])
4745 : 42 : end = start + 1;
4746 : : else
4747 : 9958 : for (unsigned j = start; j < end; j++)
4748 : 9958 : if (!matches[j - start])
4749 : : {
4750 : : end = j;
4751 : : break;
4752 : : }
4753 : : }
4754 : : }
4755 : :
4756 : : /* Now re-assess whether we want store lanes in case the
4757 : : discovery ended up producing all single-lane RHSs. */
4758 : 6411 : if (! want_store_lanes
4759 : 6411 : && rhs_common_nlanes == 1
4760 : 5481 : && ! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
4761 : 5481 : && ! STMT_VINFO_STRIDED_P (stmt_info)
4762 : 4048 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
4763 : 4045 : && compare_step_with_zero (vinfo, stmt_info) > 0
4764 : 10445 : && (vect_store_lanes_supported (SLP_TREE_VECTYPE (rhs_nodes[0]),
4765 : : group_size, masked_p)
4766 : : != IFN_LAST))
4767 : : want_store_lanes = true;
4768 : :
4769 : : /* Now we assume we can build the root SLP node from all stores. */
4770 : 6411 : if (want_store_lanes)
4771 : : {
4772 : : /* For store-lanes feed the store node with all RHS nodes
4773 : : in order. */
4774 : 0 : node = vect_create_new_slp_node (scalar_stmts,
4775 : 0 : SLP_TREE_CHILDREN
4776 : : (rhs_nodes[0]).length ());
4777 : 0 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
4778 : 0 : node->max_nunits = max_nunits;
4779 : 0 : node->ldst_lanes = true;
4780 : 0 : SLP_TREE_CHILDREN (node)
4781 : 0 : .reserve_exact (SLP_TREE_CHILDREN (rhs_nodes[0]).length ()
4782 : 0 : + rhs_nodes.length () - 1);
4783 : : /* First store value and possibly mask. */
4784 : 0 : SLP_TREE_CHILDREN (node)
4785 : 0 : .splice (SLP_TREE_CHILDREN (rhs_nodes[0]));
4786 : : /* Rest of the store values. All mask nodes are the same,
4787 : : this should be guaranteed by dataref group discovery. */
4788 : 0 : for (unsigned j = 1; j < rhs_nodes.length (); ++j)
4789 : 0 : SLP_TREE_CHILDREN (node)
4790 : 0 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[0]);
4791 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
4792 : 0 : child->refcnt++;
4793 : : }
4794 : : else
4795 : 6411 : node = vect_build_slp_store_interleaving (rhs_nodes, scalar_stmts,
4796 : : max_nunits);
4797 : :
4798 : 25119 : while (!rhs_nodes.is_empty ())
4799 : 18708 : vect_free_slp_tree (rhs_nodes.pop ());
4800 : :
4801 : : /* Create a new SLP instance. */
4802 : 6411 : slp_instance new_instance = XNEW (class _slp_instance);
4803 : 6411 : SLP_INSTANCE_TREE (new_instance) = node;
4804 : 6411 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4805 : 6411 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4806 : 6411 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4807 : 6411 : SLP_INSTANCE_KIND (new_instance) = kind;
4808 : 6411 : new_instance->reduc_phis = NULL;
4809 : 6411 : new_instance->cost_vec = vNULL;
4810 : 6411 : new_instance->subgraph_entries = vNULL;
4811 : :
4812 : 6411 : if (dump_enabled_p ())
4813 : 823 : dump_printf_loc (MSG_NOTE, vect_location,
4814 : : "SLP size %u vs. limit %u.\n",
4815 : : tree_size, max_tree_size);
4816 : :
4817 : 6411 : vinfo->slp_instances.safe_push (new_instance);
4818 : :
4819 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4820 : : the number of scalar stmts in the root in a few places.
4821 : : Verify that assumption holds. */
4822 : 12822 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4823 : : .length () == group_size);
4824 : :
4825 : 6411 : if (dump_enabled_p ())
4826 : : {
4827 : 823 : dump_printf_loc (MSG_NOTE, vect_location,
4828 : : "Final SLP tree for instance %p:\n",
4829 : : (void *) new_instance);
4830 : 823 : vect_print_slp_graph (MSG_NOTE, vect_location,
4831 : : SLP_INSTANCE_TREE (new_instance));
4832 : : }
4833 : 6411 : return true;
4834 : 6656 : }
4835 : : else
4836 : : /* Free the allocated memory. */
4837 : 247081 : scalar_stmts.release ();
4838 : :
4839 : : /* Even though the first vector did not all match, we might be able to SLP
4840 : : (some) of the remainder. FORNOW ignore this possibility. */
4841 : : }
4842 : : else
4843 : : /* Free the allocated memory. */
4844 : 1 : scalar_stmts.release ();
4845 : :
4846 : : /* Failed to SLP. */
4847 : 247082 : if (dump_enabled_p ())
4848 : 42 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4849 : : return false;
4850 : : }
4851 : :
4852 : : /* qsort comparator ordering SLP load nodes. */
4853 : :
4854 : : static int
4855 : 2168926 : vllp_cmp (const void *a_, const void *b_)
4856 : : {
4857 : 2168926 : const slp_tree a = *(const slp_tree *)a_;
4858 : 2168926 : const slp_tree b = *(const slp_tree *)b_;
4859 : 2168926 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (a)[0];
4860 : 2168926 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (b)[0];
4861 : 2168926 : if (STMT_VINFO_GROUPED_ACCESS (a0)
4862 : 1339602 : && STMT_VINFO_GROUPED_ACCESS (b0)
4863 : 3455335 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
4864 : : {
4865 : : /* Same group, order after lanes used. */
4866 : 334559 : if (SLP_TREE_LANES (a) < SLP_TREE_LANES (b))
4867 : : return 1;
4868 : 325715 : else if (SLP_TREE_LANES (a) > SLP_TREE_LANES (b))
4869 : : return -1;
4870 : : else
4871 : : {
4872 : : /* Try to order loads using the same lanes together, breaking
4873 : : the tie with the lane number that first differs. */
4874 : 316241 : if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
4875 : 316241 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
4876 : : return 0;
4877 : 316241 : else if (SLP_TREE_LOAD_PERMUTATION (a).exists ()
4878 : 316241 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
4879 : : return 1;
4880 : 314393 : else if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
4881 : 314393 : && SLP_TREE_LOAD_PERMUTATION (b).exists ())
4882 : : return -1;
4883 : : else
4884 : : {
4885 : 311737 : for (unsigned i = 0; i < SLP_TREE_LANES (a); ++i)
4886 : 311737 : if (SLP_TREE_LOAD_PERMUTATION (a)[i]
4887 : 311737 : != SLP_TREE_LOAD_PERMUTATION (b)[i])
4888 : : {
4889 : : /* In-order lane first, that's what the above case for
4890 : : no permutation does. */
4891 : 311529 : if (SLP_TREE_LOAD_PERMUTATION (a)[i] == i)
4892 : : return -1;
4893 : 188905 : else if (SLP_TREE_LOAD_PERMUTATION (b)[i] == i)
4894 : : return 1;
4895 : 92318 : else if (SLP_TREE_LOAD_PERMUTATION (a)[i]
4896 : 92318 : < SLP_TREE_LOAD_PERMUTATION (b)[i])
4897 : : return -1;
4898 : : else
4899 : : return 1;
4900 : : }
4901 : : return 0;
4902 : : }
4903 : : }
4904 : : }
4905 : : else /* Different groups or non-groups. */
4906 : : {
4907 : : /* Order groups as their first element to keep them together. */
4908 : 1834367 : if (STMT_VINFO_GROUPED_ACCESS (a0))
4909 : 1834367 : a0 = DR_GROUP_FIRST_ELEMENT (a0);
4910 : 1834367 : if (STMT_VINFO_GROUPED_ACCESS (b0))
4911 : 1834367 : b0 = DR_GROUP_FIRST_ELEMENT (b0);
4912 : 1834367 : if (a0 == b0)
4913 : : return 0;
4914 : : /* Tie using UID. */
4915 : 1834355 : else if (gimple_uid (STMT_VINFO_STMT (a0))
4916 : 1834355 : < gimple_uid (STMT_VINFO_STMT (b0)))
4917 : : return -1;
4918 : : else
4919 : : {
4920 : 817722 : gcc_assert (gimple_uid (STMT_VINFO_STMT (a0))
4921 : : != gimple_uid (STMT_VINFO_STMT (b0)));
4922 : : return 1;
4923 : : }
4924 : : }
4925 : : }
4926 : :
4927 : : /* Process the set of LOADS that are all from the same dataref group. */
4928 : :
4929 : : static void
4930 : 136362 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
4931 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4932 : : const array_slice<slp_tree> &loads,
4933 : : bool force_single_lane)
4934 : : {
4935 : : /* We at this point want to lower without a fixed VF or vector
4936 : : size in mind which means we cannot actually compute whether we
4937 : : need three or more vectors for a load permutation yet. So always
4938 : : lower. */
4939 : 136362 : stmt_vec_info first
4940 : 136362 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (loads[0])[0]);
4941 : 136362 : unsigned group_lanes = DR_GROUP_SIZE (first);
4942 : :
4943 : : /* Verify if all load permutations can be implemented with a suitably
4944 : : large element load-lanes operation. */
4945 : 136362 : unsigned ld_lanes_lanes = SLP_TREE_LANES (loads[0]);
4946 : 136362 : if (STMT_VINFO_STRIDED_P (first)
4947 : 134318 : || compare_step_with_zero (loop_vinfo, first) <= 0
4948 : 132170 : || exact_log2 (ld_lanes_lanes) == -1
4949 : : /* ??? For now only support the single-lane case as there is
4950 : : missing support on the store-lane side and code generation
4951 : : isn't up to the task yet. */
4952 : 130327 : || ld_lanes_lanes != 1
4953 : 259585 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (loads[0]),
4954 : : group_lanes / ld_lanes_lanes,
4955 : : false) == IFN_LAST)
4956 : : ld_lanes_lanes = 0;
4957 : : else
4958 : : /* Verify the loads access the same number of lanes aligned to
4959 : : ld_lanes_lanes. */
4960 : 0 : for (slp_tree load : loads)
4961 : : {
4962 : 0 : if (SLP_TREE_LANES (load) != ld_lanes_lanes)
4963 : : {
4964 : : ld_lanes_lanes = 0;
4965 : : break;
4966 : : }
4967 : 0 : unsigned first = SLP_TREE_LOAD_PERMUTATION (load)[0];
4968 : 0 : if (first % ld_lanes_lanes != 0)
4969 : : {
4970 : : ld_lanes_lanes = 0;
4971 : : break;
4972 : : }
4973 : 0 : for (unsigned i = 1; i < SLP_TREE_LANES (load); ++i)
4974 : : if (SLP_TREE_LOAD_PERMUTATION (load)[i] != first + i)
4975 : : {
4976 : : ld_lanes_lanes = 0;
4977 : : break;
4978 : : }
4979 : : }
4980 : :
4981 : : /* Only a power-of-two number of lanes matches interleaving with N levels.
4982 : : ??? An even number of lanes could be reduced to 1<<ceil_log2(N)-1 lanes
4983 : : at each step. */
4984 : 221001 : if (ld_lanes_lanes == 0 && exact_log2 (group_lanes) == -1 && group_lanes != 3)
4985 : : return;
4986 : :
4987 : 240841 : for (slp_tree load : loads)
4988 : : {
4989 : : /* Leave masked or gather loads alone for now. */
4990 : 163691 : if (!SLP_TREE_CHILDREN (load).is_empty ())
4991 : 44088 : continue;
4992 : :
4993 : : /* For single-element interleaving spanning multiple vectors avoid
4994 : : lowering, we want to use VMAT_ELEMENTWISE later. */
4995 : 163685 : if (ld_lanes_lanes == 0
4996 : 163685 : && SLP_TREE_LANES (load) == 1
4997 : 152103 : && !DR_GROUP_NEXT_ELEMENT (first)
4998 : 220948 : && maybe_gt (group_lanes,
4999 : : TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (load))))
5000 : 28971 : return;
5001 : :
5002 : : /* We want to pattern-match special cases here and keep those
5003 : : alone. Candidates are splats and load-lane. */
5004 : :
5005 : : /* We need to lower only loads of less than half of the groups
5006 : : lanes, including duplicate lanes. Note this leaves nodes
5007 : : with a non-1:1 load permutation around instead of canonicalizing
5008 : : those into a load and a permute node. Removing this early
5009 : : check would do such canonicalization. */
5010 : 134714 : if (SLP_TREE_LANES (load) >= (group_lanes + 1) / 2
5011 : 40645 : && ld_lanes_lanes == 0)
5012 : 40645 : continue;
5013 : :
5014 : : /* Build the permute to get the original load permutation order. */
5015 : 94069 : bool contiguous = true;
5016 : 94069 : lane_permutation_t final_perm;
5017 : 94069 : final_perm.create (SLP_TREE_LANES (load));
5018 : 188680 : for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i)
5019 : : {
5020 : 94611 : final_perm.quick_push
5021 : 94611 : (std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i]));
5022 : 94611 : if (i != 0
5023 : 94611 : && (SLP_TREE_LOAD_PERMUTATION (load)[i]
5024 : 542 : != SLP_TREE_LOAD_PERMUTATION (load)[i-1] + 1))
5025 : : contiguous = false;
5026 : : }
5027 : :
5028 : : /* When the load permutation accesses a contiguous unpermuted,
5029 : : power-of-two aligned and sized chunk leave the load alone.
5030 : : We can likely (re-)load it more efficiently rather than
5031 : : extracting it from the larger load.
5032 : : ??? Long-term some of the lowering should move to where
5033 : : the vector types involved are fixed. */
5034 : 97506 : if (!force_single_lane
5035 : 94069 : && ld_lanes_lanes == 0
5036 : 58768 : && contiguous
5037 : 58559 : && (SLP_TREE_LANES (load) > 1 || loads.size () == 1)
5038 : 6562 : && pow2p_hwi (SLP_TREE_LANES (load))
5039 : 6562 : && pow2p_hwi (group_lanes)
5040 : 3437 : && SLP_TREE_LOAD_PERMUTATION (load)[0] % SLP_TREE_LANES (load) == 0
5041 : 97506 : && group_lanes % SLP_TREE_LANES (load) == 0)
5042 : : {
5043 : 3437 : final_perm.release ();
5044 : 3437 : continue;
5045 : : }
5046 : :
5047 : : /* First build (and possibly re-use) a load node for the
5048 : : unpermuted group. Gaps in the middle and on the end are
5049 : : represented with NULL stmts. */
5050 : 90632 : vec<stmt_vec_info> stmts;
5051 : 90632 : stmts.create (group_lanes);
5052 : 305814 : for (stmt_vec_info s = first; s; s = DR_GROUP_NEXT_ELEMENT (s))
5053 : : {
5054 : 215182 : if (s != first)
5055 : 128924 : for (unsigned i = 1; i < DR_GROUP_GAP (s); ++i)
5056 : 4374 : stmts.quick_push (NULL);
5057 : 215182 : stmts.quick_push (s);
5058 : : }
5059 : 174033 : for (unsigned i = 0; i < DR_GROUP_GAP (first); ++i)
5060 : 83401 : stmts.quick_push (NULL);
5061 : 90632 : poly_uint64 max_nunits = 1;
5062 : 90632 : bool *matches = XALLOCAVEC (bool, group_lanes);
5063 : 90632 : unsigned limit = 1;
5064 : 90632 : unsigned tree_size = 0;
5065 : 90632 : slp_tree l0 = vect_build_slp_tree (loop_vinfo, stmts,
5066 : : group_lanes,
5067 : : &max_nunits, matches, &limit,
5068 : 90632 : &tree_size, bst_map);
5069 : 90632 : gcc_assert (!SLP_TREE_LOAD_PERMUTATION (l0).exists ());
5070 : :
5071 : 90632 : if (ld_lanes_lanes != 0)
5072 : : {
5073 : : /* ??? If this is not in sync with what get_load_store_type
5074 : : later decides the SLP representation is not good for other
5075 : : store vectorization methods. */
5076 : 0 : l0->ldst_lanes = true;
5077 : 0 : load->ldst_lanes = true;
5078 : : }
5079 : :
5080 : 279334 : while (1)
5081 : : {
5082 : 184983 : unsigned group_lanes = SLP_TREE_LANES (l0);
5083 : 184983 : if (ld_lanes_lanes != 0
5084 : 184983 : || SLP_TREE_LANES (load) >= (group_lanes + 1) / 2)
5085 : : break;
5086 : :
5087 : : /* Try to lower by reducing the group to half its size using an
5088 : : interleaving scheme. For this try to compute whether all
5089 : : elements needed for this load are in even or odd elements of
5090 : : an even/odd decomposition with N consecutive elements.
5091 : : Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition
5092 : : with N == 2. */
5093 : : /* ??? Only an even number of lanes can be handed this way, but the
5094 : : fallback below could work for any number. We have to make sure
5095 : : to round up in that case. */
5096 : 94351 : gcc_assert ((group_lanes & 1) == 0 || group_lanes == 3);
5097 : 9960 : unsigned even = 0, odd = 0;
5098 : 9960 : if ((group_lanes & 1) == 0)
5099 : : {
5100 : 9960 : even = (1 << ceil_log2 (group_lanes)) - 1;
5101 : 9960 : odd = even;
5102 : 40255 : for (auto l : final_perm)
5103 : : {
5104 : 10375 : even &= ~l.second;
5105 : 10375 : odd &= l.second;
5106 : : }
5107 : : }
5108 : :
5109 : : /* Now build an even or odd extraction from the unpermuted load. */
5110 : 94351 : lane_permutation_t perm;
5111 : 94351 : perm.create ((group_lanes + 1) / 2);
5112 : 94351 : unsigned even_level = even ? 1 << ctz_hwi (even) : 0;
5113 : 94351 : unsigned odd_level = odd ? 1 << ctz_hwi (odd) : 0;
5114 : 94351 : if (even_level
5115 : 9307 : && group_lanes % (2 * even_level) == 0
5116 : : /* ??? When code generating permutes we do not try to pun
5117 : : to larger component modes so level != 1 isn't a natural
5118 : : even/odd extract. Prefer one if possible. */
5119 : 9307 : && (even_level == 1 || !odd_level || odd_level != 1))
5120 : : {
5121 : : /* { 0, 1, ... 4, 5 ..., } */
5122 : 34851 : for (unsigned i = 0; i < group_lanes / 2 / even_level; ++i)
5123 : 55094 : for (unsigned j = 0; j < even_level; ++j)
5124 : 27604 : perm.quick_push (std::make_pair (0, 2 * i * even_level + j));
5125 : : }
5126 : 85044 : else if (odd_level)
5127 : : {
5128 : : /* { ..., 2, 3, ... 6, 7 } */
5129 : 2599 : gcc_assert (group_lanes % (2 * odd_level) == 0);
5130 : 11847 : for (unsigned i = 0; i < group_lanes / 2 / odd_level; ++i)
5131 : 18496 : for (unsigned j = 0; j < odd_level; ++j)
5132 : 9248 : perm.quick_push
5133 : 9248 : (std::make_pair (0, (2 * i + 1) * odd_level + j));
5134 : : }
5135 : : else
5136 : : {
5137 : : /* As fallback extract all used lanes and fill to half the
5138 : : group size by repeating the last element.
5139 : : ??? This is quite a bad strathegy for re-use - we could
5140 : : brute force our way to find more optimal filling lanes to
5141 : : maximize re-use when looking at all loads from the group. */
5142 : 84391 : auto_bitmap l;
5143 : 337564 : for (auto p : final_perm)
5144 : 84391 : bitmap_set_bit (l, p.second);
5145 : 84391 : unsigned i = 0;
5146 : 84391 : bitmap_iterator bi;
5147 : 168782 : EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi)
5148 : 84391 : perm.quick_push (std::make_pair (0, i));
5149 : 337564 : while (perm.length () < (group_lanes + 1) / 2)
5150 : 84391 : perm.quick_push (perm.last ());
5151 : 84391 : }
5152 : :
5153 : : /* Update final_perm with the intermediate permute. */
5154 : 189117 : for (unsigned i = 0; i < final_perm.length (); ++i)
5155 : : {
5156 : 94766 : unsigned l = final_perm[i].second;
5157 : 94766 : unsigned j;
5158 : 101454 : for (j = 0; j < perm.length (); ++j)
5159 : 101454 : if (perm[j].second == l)
5160 : : {
5161 : 94766 : final_perm[i].second = j;
5162 : 94766 : break;
5163 : : }
5164 : 94766 : gcc_assert (j < perm.length ());
5165 : : }
5166 : :
5167 : : /* And create scalar stmts. */
5168 : 94351 : vec<stmt_vec_info> perm_stmts;
5169 : 94351 : perm_stmts.create (perm.length ());
5170 : 299985 : for (unsigned i = 0; i < perm.length (); ++i)
5171 : 205634 : perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]);
5172 : :
5173 : 94351 : slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR);
5174 : 94351 : SLP_TREE_CHILDREN (p).quick_push (l0);
5175 : 94351 : SLP_TREE_LANE_PERMUTATION (p) = perm;
5176 : 94351 : SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load);
5177 : 94351 : SLP_TREE_LANES (p) = perm.length ();
5178 : 94351 : SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load);
5179 : : /* ??? As we have scalar stmts for this intermediate permute we
5180 : : could CSE it via bst_map but we do not want to pick up
5181 : : another SLP node with a load permutation. We instead should
5182 : : have a "local" CSE map here. */
5183 : 94351 : SLP_TREE_SCALAR_STMTS (p) = perm_stmts;
5184 : :
5185 : : /* We now have a node for (group_lanes + 1) / 2 lanes. */
5186 : 94351 : l0 = p;
5187 : 94351 : }
5188 : :
5189 : : /* And finally from the ordered reduction node create the
5190 : : permute to shuffle the lanes into the original load-permutation
5191 : : order. We replace the original load node with this. */
5192 : 90632 : SLP_TREE_CODE (load) = VEC_PERM_EXPR;
5193 : 90632 : SLP_TREE_LOAD_PERMUTATION (load).release ();
5194 : 90632 : SLP_TREE_LANE_PERMUTATION (load) = final_perm;
5195 : 90632 : SLP_TREE_CHILDREN (load).create (1);
5196 : 90632 : SLP_TREE_CHILDREN (load).quick_push (l0);
5197 : : }
5198 : : }
5199 : :
5200 : : /* Transform SLP loads in the SLP graph created by SLP discovery to
5201 : : group loads from the same group and lower load permutations that
5202 : : are unlikely to be supported into a series of permutes.
5203 : : In the degenerate case of having only single-lane SLP instances
5204 : : this should result in a series of permute nodes emulating an
5205 : : interleaving scheme. */
5206 : :
5207 : : static void
5208 : 375411 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
5209 : : scalar_stmts_to_slp_tree_map_t *bst_map,
5210 : : bool force_single_lane)
5211 : : {
5212 : : /* Gather and sort loads across all instances. */
5213 : 375411 : hash_set<slp_tree> visited;
5214 : 375411 : auto_vec<slp_tree> loads;
5215 : 2070573 : for (auto inst : loop_vinfo->slp_instances)
5216 : 946330 : vect_gather_slp_loads (loads, SLP_INSTANCE_TREE (inst), visited);
5217 : 375411 : if (loads.is_empty ())
5218 : 68461 : return;
5219 : 306950 : loads.qsort (vllp_cmp);
5220 : :
5221 : : /* Now process each dataref group separately. */
5222 : 306950 : unsigned firsti = 0;
5223 : 582012 : for (unsigned i = 1; i < loads.length (); ++i)
5224 : : {
5225 : 275062 : slp_tree first = loads[firsti];
5226 : 275062 : slp_tree next = loads[i];
5227 : 275062 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (first)[0];
5228 : 275062 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (next)[0];
5229 : 275062 : if (STMT_VINFO_GROUPED_ACCESS (a0)
5230 : 140934 : && STMT_VINFO_GROUPED_ACCESS (b0)
5231 : 404016 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
5232 : 62832 : continue;
5233 : : /* Now we have one or multiple SLP loads of the same group from
5234 : : firsti to i - 1. */
5235 : 212230 : if (STMT_VINFO_GROUPED_ACCESS (a0))
5236 : 78102 : vect_lower_load_permutations (loop_vinfo, bst_map,
5237 : 78102 : make_array_slice (&loads[firsti],
5238 : : i - firsti),
5239 : : force_single_lane);
5240 : : firsti = i;
5241 : : }
5242 : 613900 : if (firsti < loads.length ()
5243 : 613900 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (loads[firsti])[0]))
5244 : 58260 : vect_lower_load_permutations (loop_vinfo, bst_map,
5245 : 58260 : make_array_slice (&loads[firsti],
5246 : 58260 : loads.length () - firsti),
5247 : : force_single_lane);
5248 : 375411 : }
5249 : :
5250 : : /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
5251 : : trees of packed scalar stmts if SLP is possible. */
5252 : :
5253 : : opt_result
5254 : 1037855 : vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size,
5255 : : bool force_single_lane)
5256 : : {
5257 : 1037855 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5258 : 1037855 : unsigned int i;
5259 : 1037855 : stmt_vec_info first_element;
5260 : 1037855 : slp_instance instance;
5261 : :
5262 : 1037855 : DUMP_VECT_SCOPE ("vect_analyze_slp");
5263 : :
5264 : 1037855 : unsigned limit = max_tree_size;
5265 : :
5266 : 1037855 : scalar_stmts_to_slp_tree_map_t *bst_map
5267 : 1037855 : = new scalar_stmts_to_slp_tree_map_t ();
5268 : :
5269 : : /* Find SLP sequences starting from groups of grouped stores. */
5270 : 2983820 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
5271 : 908363 : if (! vect_analyze_slp_instance (vinfo, bst_map, first_element,
5272 : : slp_inst_kind_store, max_tree_size, &limit,
5273 : : force_single_lane)
5274 : 908363 : && loop_vinfo)
5275 : 253 : return opt_result::failure_at (vect_location, "SLP build failed.\n");
5276 : :
5277 : : /* For loops also start SLP discovery from non-grouped stores. */
5278 : 1037602 : if (loop_vinfo)
5279 : : {
5280 : : data_reference_p dr;
5281 : 1313804 : FOR_EACH_VEC_ELT (vinfo->shared->datarefs, i, dr)
5282 : 915967 : if (DR_IS_WRITE (dr))
5283 : : {
5284 : 282188 : stmt_vec_info stmt_info = vinfo->lookup_dr (dr)->stmt;
5285 : : /* Grouped stores are already handled above. */
5286 : 282188 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5287 : 72872 : continue;
5288 : 209316 : vec<stmt_vec_info> stmts;
5289 : 209316 : vec<stmt_vec_info> roots = vNULL;
5290 : 209316 : vec<tree> remain = vNULL;
5291 : 209316 : stmts.create (1);
5292 : 209316 : stmts.quick_push (stmt_info);
5293 : 209316 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
5294 : : stmts, roots, remain, max_tree_size,
5295 : : &limit, bst_map, force_single_lane))
5296 : 3819 : return opt_result::failure_at (vect_location,
5297 : : "SLP build failed.\n");
5298 : : }
5299 : :
5300 : : stmt_vec_info stmt_info;
5301 : 397877 : FOR_EACH_VEC_ELT (LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo), i, stmt_info)
5302 : : {
5303 : 20 : vec<stmt_vec_info> stmts;
5304 : 20 : vec<stmt_vec_info> roots = vNULL;
5305 : 20 : vec<tree> remain = vNULL;
5306 : 20 : stmts.create (1);
5307 : 20 : stmts.quick_push (stmt_info);
5308 : 20 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
5309 : : stmts, roots, remain, max_tree_size,
5310 : : &limit, bst_map, force_single_lane))
5311 : 0 : return opt_result::failure_at (vect_location,
5312 : : "SLP build failed.\n");
5313 : : }
5314 : : }
5315 : :
5316 : 1033783 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
5317 : : {
5318 : 1846308 : for (unsigned i = 0; i < bb_vinfo->roots.length (); ++i)
5319 : : {
5320 : 1210362 : vect_location = bb_vinfo->roots[i].roots[0]->stmt;
5321 : : /* Apply patterns. */
5322 : 3782660 : for (unsigned j = 0; j < bb_vinfo->roots[i].stmts.length (); ++j)
5323 : 5144596 : bb_vinfo->roots[i].stmts[j]
5324 : 2646168 : = vect_stmt_to_vectorize (bb_vinfo->roots[i].stmts[j]);
5325 : 1210362 : if (vect_build_slp_instance (bb_vinfo, bb_vinfo->roots[i].kind,
5326 : 1210362 : bb_vinfo->roots[i].stmts,
5327 : 1210362 : bb_vinfo->roots[i].roots,
5328 : 1210362 : bb_vinfo->roots[i].remain,
5329 : : max_tree_size, &limit, bst_map, false))
5330 : : {
5331 : 130448 : bb_vinfo->roots[i].stmts = vNULL;
5332 : 130448 : bb_vinfo->roots[i].roots = vNULL;
5333 : 130448 : bb_vinfo->roots[i].remain = vNULL;
5334 : : }
5335 : : }
5336 : : }
5337 : :
5338 : 1033783 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5339 : : {
5340 : : /* Find SLP sequences starting from groups of reductions. */
5341 : 397837 : if (loop_vinfo->reductions.length () > 0)
5342 : : {
5343 : : /* Collect reduction statements we can combine into
5344 : : a SLP reduction. */
5345 : 50552 : vec<stmt_vec_info> scalar_stmts;
5346 : 50552 : scalar_stmts.create (loop_vinfo->reductions.length ());
5347 : 220378 : for (auto next_info : loop_vinfo->reductions)
5348 : : {
5349 : 68722 : next_info = vect_stmt_to_vectorize (next_info);
5350 : 68722 : if ((STMT_VINFO_RELEVANT_P (next_info)
5351 : 4 : || STMT_VINFO_LIVE_P (next_info))
5352 : : /* ??? Make sure we didn't skip a conversion around a
5353 : : reduction path. In that case we'd have to reverse
5354 : : engineer that conversion stmt following the chain using
5355 : : reduc_idx and from the PHI using reduc_def. */
5356 : 68718 : && (STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def
5357 : 68718 : || (STMT_VINFO_DEF_TYPE (next_info)
5358 : : == vect_double_reduction_def)))
5359 : : {
5360 : : /* Do not discover SLP reductions combining lane-reducing
5361 : : ops, that will fail later. */
5362 : 68718 : if (!force_single_lane
5363 : 68718 : && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info)))
5364 : 51817 : scalar_stmts.quick_push (next_info);
5365 : : /* Do SLP discovery for single-lane reductions. */
5366 : 16901 : else if (! vect_analyze_slp_reduction (loop_vinfo, next_info,
5367 : : max_tree_size, &limit,
5368 : : bst_map,
5369 : : force_single_lane))
5370 : 0 : return opt_result::failure_at (vect_location,
5371 : : "SLP build failed.\n");
5372 : : }
5373 : : }
5374 : : /* Save for re-processing on failure. */
5375 : 50552 : vec<stmt_vec_info> saved_stmts = scalar_stmts.copy ();
5376 : 50552 : vec<stmt_vec_info> roots = vNULL;
5377 : 50552 : vec<tree> remain = vNULL;
5378 : 50552 : if (scalar_stmts.length () <= 1
5379 : 50552 : || !vect_build_slp_instance (loop_vinfo,
5380 : : slp_inst_kind_reduc_group,
5381 : : scalar_stmts, roots, remain,
5382 : : max_tree_size, &limit, bst_map,
5383 : : force_single_lane))
5384 : : {
5385 : 49843 : if (scalar_stmts.length () <= 1)
5386 : 49843 : scalar_stmts.release ();
5387 : : /* Do SLP discovery for single-lane reductions. */
5388 : 171403 : for (auto stmt_info : saved_stmts)
5389 : 49938 : if (! vect_analyze_slp_reduction (loop_vinfo,
5390 : : vect_stmt_to_vectorize
5391 : : (stmt_info),
5392 : : max_tree_size, &limit,
5393 : : bst_map, force_single_lane))
5394 : 2240 : return opt_result::failure_at (vect_location,
5395 : : "SLP build failed.\n");
5396 : : }
5397 : 48312 : saved_stmts.release ();
5398 : : }
5399 : :
5400 : : /* Make sure to vectorize only-live stmts, usually inductions. */
5401 : 1830256 : for (edge e : get_loop_exit_edges (LOOP_VINFO_LOOP (loop_vinfo)))
5402 : 1176626 : for (auto gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
5403 : 526009 : gsi_next (&gsi))
5404 : : {
5405 : 533161 : gphi *lc_phi = *gsi;
5406 : 533161 : tree def = gimple_phi_arg_def_from_edge (lc_phi, e);
5407 : 533161 : stmt_vec_info stmt_info;
5408 : 533161 : if (TREE_CODE (def) == SSA_NAME
5409 : 440327 : && !virtual_operand_p (def)
5410 : 212293 : && (stmt_info = loop_vinfo->lookup_def (def))
5411 : 194083 : && ((stmt_info = vect_stmt_to_vectorize (stmt_info)), true)
5412 : 194083 : && STMT_VINFO_RELEVANT (stmt_info) == vect_used_only_live
5413 : 148856 : && STMT_VINFO_LIVE_P (stmt_info)
5414 : 148856 : && !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
5415 : 616106 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
5416 : : {
5417 : 82866 : vec<stmt_vec_info> stmts;
5418 : 82866 : vec<stmt_vec_info> roots = vNULL;
5419 : 82866 : vec<tree> remain = vNULL;
5420 : 82866 : stmts.create (1);
5421 : 82866 : stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
5422 : 82866 : if (! vect_build_slp_instance (vinfo,
5423 : : slp_inst_kind_reduc_group,
5424 : : stmts, roots, remain,
5425 : : max_tree_size, &limit,
5426 : : bst_map, force_single_lane))
5427 : 7152 : return opt_result::failure_at (vect_location,
5428 : : "SLP build failed.\n");
5429 : : }
5430 : 7152 : }
5431 : :
5432 : : /* Find SLP sequences starting from gconds. */
5433 : 1019766 : for (auto cond : LOOP_VINFO_LOOP_CONDS (loop_vinfo))
5434 : : {
5435 : 250451 : auto cond_info = loop_vinfo->lookup_stmt (cond);
5436 : :
5437 : 250451 : cond_info = vect_stmt_to_vectorize (cond_info);
5438 : 250451 : vec<stmt_vec_info> roots = vNULL;
5439 : 250451 : roots.safe_push (cond_info);
5440 : 250451 : gimple *stmt = STMT_VINFO_STMT (cond_info);
5441 : 250451 : tree args0 = gimple_cond_lhs (stmt);
5442 : 250451 : tree args1 = gimple_cond_rhs (stmt);
5443 : :
5444 : : /* These should be enforced by cond lowering, but if it failed
5445 : : bail. */
5446 : 250451 : if (gimple_cond_code (stmt) != NE_EXPR
5447 : 249170 : || TREE_TYPE (args0) != boolean_type_node
5448 : 498535 : || !integer_zerop (args1))
5449 : : {
5450 : 2367 : roots.release ();
5451 : 2367 : return opt_result::failure_at (vect_location,
5452 : : "SLP build failed.\n");
5453 : : }
5454 : :
5455 : : /* An argument without a loop def will be codegened from vectorizing the
5456 : : root gcond itself. As such we don't need to try to build an SLP tree
5457 : : from them. It's highly likely that the resulting SLP tree here if both
5458 : : arguments have a def will be incompatible, but we rely on it being split
5459 : : later on. */
5460 : 248084 : auto varg = loop_vinfo->lookup_def (args0);
5461 : 248084 : vec<stmt_vec_info> stmts;
5462 : 248084 : vec<tree> remain = vNULL;
5463 : 248084 : stmts.create (1);
5464 : 248084 : stmts.quick_push (vect_stmt_to_vectorize (varg));
5465 : :
5466 : 248084 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_gcond,
5467 : : stmts, roots, remain,
5468 : : max_tree_size, &limit,
5469 : : bst_map, force_single_lane))
5470 : : {
5471 : 2353 : roots.release ();
5472 : 2353 : return opt_result::failure_at (vect_location,
5473 : : "SLP build failed.\n");
5474 : : }
5475 : : }
5476 : :
5477 : : /* Find and create slp instances for inductions that have been forced
5478 : : live due to early break. */
5479 : 383725 : edge latch_e = loop_latch_edge (LOOP_VINFO_LOOP (loop_vinfo));
5480 : 1095478 : for (auto stmt_info : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo))
5481 : : {
5482 : 343917 : vec<stmt_vec_info> stmts;
5483 : 343917 : vec<stmt_vec_info> roots = vNULL;
5484 : 343917 : vec<tree> remain = vNULL;
5485 : 343917 : gphi *phi = as_a<gphi *> (STMT_VINFO_STMT (stmt_info));
5486 : 343917 : tree def = gimple_phi_arg_def_from_edge (phi, latch_e);
5487 : 343917 : stmt_vec_info lc_info = loop_vinfo->lookup_def (def);
5488 : 343917 : if (lc_info)
5489 : : {
5490 : 343917 : stmts.create (1);
5491 : 343947 : stmts.quick_push (vect_stmt_to_vectorize (lc_info));
5492 : 343917 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_reduc_group,
5493 : : stmts, roots, remain,
5494 : : max_tree_size, &limit,
5495 : : bst_map, force_single_lane))
5496 : 8314 : return opt_result::failure_at (vect_location,
5497 : : "SLP build failed.\n");
5498 : : }
5499 : : /* When the latch def is from a different cycle this can only
5500 : : be a induction. Build a simple instance for this.
5501 : : ??? We should be able to start discovery from the PHI
5502 : : for all inductions, but then there will be stray
5503 : : non-SLP stmts we choke on as needing non-SLP handling. */
5504 : 335603 : auto_vec<stmt_vec_info, 1> tem;
5505 : 335603 : tem.quick_push (stmt_info);
5506 : 335603 : if (!bst_map->get (tem))
5507 : : {
5508 : 10312 : stmts.create (1);
5509 : 10312 : stmts.quick_push (stmt_info);
5510 : 10312 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_reduc_group,
5511 : : stmts, roots, remain,
5512 : : max_tree_size, &limit,
5513 : : bst_map, force_single_lane))
5514 : 0 : return opt_result::failure_at (vect_location,
5515 : : "SLP build failed.\n");
5516 : : }
5517 : 335603 : }
5518 : : }
5519 : :
5520 : 1011357 : hash_set<slp_tree> visited_patterns;
5521 : 1011357 : slp_tree_to_load_perm_map_t perm_cache;
5522 : 1011357 : slp_compat_nodes_map_t compat_cache;
5523 : :
5524 : : /* See if any patterns can be found in the SLP tree. */
5525 : 1011357 : bool pattern_found = false;
5526 : 3761017 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5527 : 1738303 : pattern_found |= vect_match_slp_patterns (instance, vinfo,
5528 : : &visited_patterns, &perm_cache,
5529 : : &compat_cache);
5530 : :
5531 : : /* If any were found optimize permutations of loads. */
5532 : 1011357 : if (pattern_found)
5533 : : {
5534 : 200 : hash_map<slp_tree, slp_tree> load_map;
5535 : 3296 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5536 : : {
5537 : 2896 : slp_tree root = SLP_INSTANCE_TREE (instance);
5538 : 2896 : optimize_load_redistribution (bst_map, vinfo, SLP_TREE_LANES (root),
5539 : : &load_map, root);
5540 : : }
5541 : 200 : }
5542 : :
5543 : : /* Check whether we should force some SLP instances to use load/store-lanes
5544 : : and do so by forcing SLP re-discovery with single lanes. We used
5545 : : to cancel SLP when this applied to all instances in a loop but now
5546 : : we decide this per SLP instance. It's important to do this only
5547 : : after SLP pattern recognition. */
5548 : 1011357 : if (is_a <loop_vec_info> (vinfo))
5549 : 1321741 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5550 : 946330 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
5551 : 225225 : && !SLP_INSTANCE_TREE (instance)->ldst_lanes)
5552 : : {
5553 : 225225 : slp_tree slp_root = SLP_INSTANCE_TREE (instance);
5554 : 225225 : unsigned int group_size = SLP_TREE_LANES (slp_root);
5555 : 225225 : tree vectype = SLP_TREE_VECTYPE (slp_root);
5556 : :
5557 : 225225 : stmt_vec_info rep_info = SLP_TREE_REPRESENTATIVE (slp_root);
5558 : 225225 : gimple *rep = STMT_VINFO_STMT (rep_info);
5559 : 225225 : bool masked = (is_gimple_call (rep)
5560 : 1312 : && gimple_call_internal_p (rep)
5561 : 226517 : && internal_fn_mask_index
5562 : 1292 : (gimple_call_internal_fn (rep)) != -1);
5563 : 225205 : if (!STMT_VINFO_GROUPED_ACCESS (rep_info)
5564 : 21610 : || slp_root->ldst_lanes
5565 : 246835 : || (vect_store_lanes_supported (vectype, group_size, masked)
5566 : : == IFN_LAST))
5567 : 225225 : continue;
5568 : :
5569 : 0 : auto_vec<slp_tree> loads;
5570 : 0 : hash_set<slp_tree> visited;
5571 : 0 : vect_gather_slp_loads (loads, slp_root, visited);
5572 : :
5573 : : /* Check whether any load in the SLP instance is possibly
5574 : : permuted. */
5575 : 0 : bool loads_permuted = false;
5576 : 0 : slp_tree load_node;
5577 : 0 : unsigned j;
5578 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5579 : : {
5580 : 0 : if (!SLP_TREE_LOAD_PERMUTATION (load_node).exists ())
5581 : 0 : continue;
5582 : : unsigned k;
5583 : : stmt_vec_info load_info;
5584 : 0 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), k, load_info)
5585 : 0 : if (SLP_TREE_LOAD_PERMUTATION (load_node)[k] != k)
5586 : : {
5587 : : loads_permuted = true;
5588 : : break;
5589 : : }
5590 : : }
5591 : :
5592 : : /* If the loads and stores can use load/store-lanes force re-discovery
5593 : : with single lanes. */
5594 : 0 : if (loads_permuted)
5595 : : {
5596 : 0 : bool can_use_lanes = true;
5597 : : bool prefer_load_lanes = false;
5598 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5599 : 0 : if (STMT_VINFO_GROUPED_ACCESS
5600 : : (SLP_TREE_REPRESENTATIVE (load_node)))
5601 : : {
5602 : 0 : stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
5603 : : (SLP_TREE_REPRESENTATIVE (load_node));
5604 : 0 : rep = STMT_VINFO_STMT (stmt_vinfo);
5605 : 0 : masked = (is_gimple_call (rep)
5606 : 0 : && gimple_call_internal_p (rep)
5607 : 0 : && internal_fn_mask_index
5608 : 0 : (gimple_call_internal_fn (rep)));
5609 : : /* Use SLP for strided accesses (or if we can't
5610 : : load-lanes). */
5611 : 0 : if (STMT_VINFO_STRIDED_P (stmt_vinfo)
5612 : 0 : || compare_step_with_zero (vinfo, stmt_vinfo) <= 0
5613 : 0 : || vect_load_lanes_supported
5614 : 0 : (SLP_TREE_VECTYPE (load_node),
5615 : 0 : DR_GROUP_SIZE (stmt_vinfo), masked) == IFN_LAST
5616 : : /* ??? During SLP re-discovery with a single lane
5617 : : a masked grouped load will appear permuted and
5618 : : discovery will fail. We have to rework this
5619 : : on the discovery side - for now avoid ICEing. */
5620 : 0 : || masked)
5621 : : {
5622 : : can_use_lanes = false;
5623 : : break;
5624 : : }
5625 : : /* Make sure that the target would prefer store-lanes
5626 : : for at least one of the loads.
5627 : :
5628 : : ??? Perhaps we should instead require this for
5629 : : all loads? */
5630 : 0 : prefer_load_lanes
5631 : : = (prefer_load_lanes
5632 : 0 : || SLP_TREE_LANES (load_node) == group_size
5633 : 0 : || (vect_slp_prefer_store_lanes_p
5634 : 0 : (vinfo, stmt_vinfo,
5635 : : SLP_TREE_VECTYPE (load_node), masked,
5636 : : group_size, SLP_TREE_LANES (load_node))));
5637 : : }
5638 : :
5639 : 0 : if (can_use_lanes && prefer_load_lanes)
5640 : : {
5641 : 0 : if (dump_enabled_p ())
5642 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
5643 : : "SLP instance %p can use load/store-lanes,"
5644 : : " re-discovering with single-lanes\n",
5645 : : (void *) instance);
5646 : :
5647 : 0 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (slp_root);
5648 : :
5649 : 0 : vect_free_slp_instance (instance);
5650 : 0 : limit = max_tree_size;
5651 : 0 : bool res = vect_analyze_slp_instance (vinfo, bst_map,
5652 : : stmt_info,
5653 : : slp_inst_kind_store,
5654 : : max_tree_size, &limit,
5655 : : true);
5656 : 0 : gcc_assert (res);
5657 : 0 : auto new_inst = LOOP_VINFO_SLP_INSTANCES (vinfo).pop ();
5658 : 0 : LOOP_VINFO_SLP_INSTANCES (vinfo)[i] = new_inst;
5659 : : }
5660 : : }
5661 : 0 : }
5662 : :
5663 : : /* When we end up with load permutations that we cannot possibly handle,
5664 : : like those requiring three vector inputs, lower them using interleaving
5665 : : like schemes. */
5666 : 1011357 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5667 : : {
5668 : 375411 : vect_lower_load_permutations (loop_vinfo, bst_map, force_single_lane);
5669 : 375411 : if (dump_enabled_p ())
5670 : : {
5671 : 18433 : dump_printf_loc (MSG_NOTE, vect_location,
5672 : : "SLP graph after lowering permutations:\n");
5673 : 18433 : hash_set<slp_tree> visited;
5674 : 86261 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5675 : 30986 : vect_print_slp_graph (MSG_NOTE, vect_location,
5676 : : SLP_INSTANCE_TREE (instance), visited);
5677 : 18433 : }
5678 : : }
5679 : :
5680 : 1011357 : release_scalar_stmts_to_slp_tree_map (bst_map);
5681 : :
5682 : 1011357 : if (pattern_found && dump_enabled_p ())
5683 : : {
5684 : 22 : dump_printf_loc (MSG_NOTE, vect_location,
5685 : : "Pattern matched SLP tree\n");
5686 : 22 : hash_set<slp_tree> visited;
5687 : 138 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5688 : 72 : vect_print_slp_graph (MSG_NOTE, vect_location,
5689 : : SLP_INSTANCE_TREE (instance), visited);
5690 : 22 : }
5691 : :
5692 : 1011357 : return opt_result::success ();
5693 : 1011357 : }
5694 : :
5695 : : /* Estimates the cost of inserting layout changes into the SLP graph.
5696 : : It can also say that the insertion is impossible. */
5697 : :
5698 : : struct slpg_layout_cost
5699 : : {
5700 : 10979779 : slpg_layout_cost () = default;
5701 : : slpg_layout_cost (sreal, bool);
5702 : :
5703 : 474659 : static slpg_layout_cost impossible () { return { sreal::max (), 0 }; }
5704 : 5208920 : bool is_possible () const { return depth != sreal::max (); }
5705 : :
5706 : : bool operator== (const slpg_layout_cost &) const;
5707 : : bool operator!= (const slpg_layout_cost &) const;
5708 : :
5709 : : bool is_better_than (const slpg_layout_cost &, bool) const;
5710 : :
5711 : : void add_parallel_cost (const slpg_layout_cost &);
5712 : : void add_serial_cost (const slpg_layout_cost &);
5713 : : void split (unsigned int);
5714 : :
5715 : : /* The longest sequence of layout changes needed during any traversal
5716 : : of the partition dag, weighted by execution frequency.
5717 : :
5718 : : This is the most important metric when optimizing for speed, since
5719 : : it helps to ensure that we keep the number of operations on
5720 : : critical paths to a minimum. */
5721 : : sreal depth = 0;
5722 : :
5723 : : /* An estimate of the total number of operations needed. It is weighted by
5724 : : execution frequency when optimizing for speed but not when optimizing for
5725 : : size. In order to avoid double-counting, a node with a fanout of N will
5726 : : distribute 1/N of its total cost to each successor.
5727 : :
5728 : : This is the most important metric when optimizing for size, since
5729 : : it helps to keep the total number of operations to a minimum, */
5730 : : sreal total = 0;
5731 : : };
5732 : :
5733 : : /* Construct costs for a node with weight WEIGHT. A higher weight
5734 : : indicates more frequent execution. IS_FOR_SIZE is true if we are
5735 : : optimizing for size rather than speed. */
5736 : :
5737 : 1208885 : slpg_layout_cost::slpg_layout_cost (sreal weight, bool is_for_size)
5738 : 1209812 : : depth (weight), total (is_for_size && weight > 0 ? 1 : weight)
5739 : : {
5740 : 1208885 : }
5741 : :
5742 : : bool
5743 : 0 : slpg_layout_cost::operator== (const slpg_layout_cost &other) const
5744 : : {
5745 : 0 : return depth == other.depth && total == other.total;
5746 : : }
5747 : :
5748 : : bool
5749 : 0 : slpg_layout_cost::operator!= (const slpg_layout_cost &other) const
5750 : : {
5751 : 0 : return !operator== (other);
5752 : : }
5753 : :
5754 : : /* Return true if these costs are better than OTHER. IS_FOR_SIZE is
5755 : : true if we are optimizing for size rather than speed. */
5756 : :
5757 : : bool
5758 : 308275 : slpg_layout_cost::is_better_than (const slpg_layout_cost &other,
5759 : : bool is_for_size) const
5760 : : {
5761 : 308275 : if (is_for_size)
5762 : : {
5763 : 413 : if (total != other.total)
5764 : 177 : return total < other.total;
5765 : 236 : return depth < other.depth;
5766 : : }
5767 : : else
5768 : : {
5769 : 307862 : if (depth != other.depth)
5770 : 138712 : return depth < other.depth;
5771 : 169150 : return total < other.total;
5772 : : }
5773 : : }
5774 : :
5775 : : /* Increase the costs to account for something with cost INPUT_COST
5776 : : happening in parallel with the current costs. */
5777 : :
5778 : : void
5779 : 352019 : slpg_layout_cost::add_parallel_cost (const slpg_layout_cost &input_cost)
5780 : : {
5781 : 352019 : depth = std::max (depth, input_cost.depth);
5782 : 352019 : total += input_cost.total;
5783 : 352019 : }
5784 : :
5785 : : /* Increase the costs to account for something with cost INPUT_COST
5786 : : happening in series with the current costs. */
5787 : :
5788 : : void
5789 : 1471853 : slpg_layout_cost::add_serial_cost (const slpg_layout_cost &other)
5790 : : {
5791 : 1471853 : depth += other.depth;
5792 : 1471853 : total += other.total;
5793 : 1471853 : }
5794 : :
5795 : : /* Split the total cost among TIMES successors or predecessors. */
5796 : :
5797 : : void
5798 : 1182459 : slpg_layout_cost::split (unsigned int times)
5799 : : {
5800 : 1182459 : if (times > 1)
5801 : 466143 : total /= times;
5802 : 1182459 : }
5803 : :
5804 : : /* Information about one node in the SLP graph, for use during
5805 : : vect_optimize_slp_pass. */
5806 : :
5807 : : struct slpg_vertex
5808 : : {
5809 : 10436104 : slpg_vertex (slp_tree node_) : node (node_) {}
5810 : :
5811 : : /* The node itself. */
5812 : : slp_tree node;
5813 : :
5814 : : /* Which partition the node belongs to, or -1 if none. Nodes outside of
5815 : : partitions are flexible; they can have whichever layout consumers
5816 : : want them to have. */
5817 : : int partition = -1;
5818 : :
5819 : : /* The number of nodes that directly use the result of this one
5820 : : (i.e. the number of nodes that count this one as a child). */
5821 : : unsigned int out_degree = 0;
5822 : :
5823 : : /* The execution frequency of the node. */
5824 : : sreal weight = 0;
5825 : :
5826 : : /* The total execution frequency of all nodes that directly use the
5827 : : result of this one. */
5828 : : sreal out_weight = 0;
5829 : : };
5830 : :
5831 : : /* Information about one partition of the SLP graph, for use during
5832 : : vect_optimize_slp_pass. */
5833 : :
5834 : : struct slpg_partition_info
5835 : : {
5836 : : /* The nodes in the partition occupy indices [NODE_BEGIN, NODE_END)
5837 : : of m_partitioned_nodes. */
5838 : : unsigned int node_begin = 0;
5839 : : unsigned int node_end = 0;
5840 : :
5841 : : /* Which layout we've chosen to use for this partition, or -1 if
5842 : : we haven't picked one yet. */
5843 : : int layout = -1;
5844 : :
5845 : : /* The number of predecessors and successors in the partition dag.
5846 : : The predecessors always have lower partition numbers and the
5847 : : successors always have higher partition numbers.
5848 : :
5849 : : Note that the directions of these edges are not necessarily the
5850 : : same as in the data flow graph. For example, if an SCC has separate
5851 : : partitions for an inner loop and an outer loop, the inner loop's
5852 : : partition will have at least two incoming edges from the outer loop's
5853 : : partition: one for a live-in value and one for a live-out value.
5854 : : In data flow terms, one of these edges would also be from the outer loop
5855 : : to the inner loop, but the other would be in the opposite direction. */
5856 : : unsigned int in_degree = 0;
5857 : : unsigned int out_degree = 0;
5858 : : };
5859 : :
5860 : : /* Information about the costs of using a particular layout for a
5861 : : particular partition. It can also say that the combination is
5862 : : impossible. */
5863 : :
5864 : : struct slpg_partition_layout_costs
5865 : : {
5866 : 1502002 : bool is_possible () const { return internal_cost.is_possible (); }
5867 : 53442 : void mark_impossible () { internal_cost = slpg_layout_cost::impossible (); }
5868 : :
5869 : : /* The costs inherited from predecessor partitions. */
5870 : : slpg_layout_cost in_cost;
5871 : :
5872 : : /* The inherent cost of the layout within the node itself. For example,
5873 : : this is nonzero for a load if choosing a particular layout would require
5874 : : the load to permute the loaded elements. It is nonzero for a
5875 : : VEC_PERM_EXPR if the permutation cannot be eliminated or converted
5876 : : to full-vector moves. */
5877 : : slpg_layout_cost internal_cost;
5878 : :
5879 : : /* The costs inherited from successor partitions. */
5880 : : slpg_layout_cost out_cost;
5881 : : };
5882 : :
5883 : : /* This class tries to optimize the layout of vectors in order to avoid
5884 : : unnecessary shuffling. At the moment, the set of possible layouts are
5885 : : restricted to bijective permutations.
5886 : :
5887 : : The goal of the pass depends on whether we're optimizing for size or
5888 : : for speed. When optimizing for size, the goal is to reduce the overall
5889 : : number of layout changes (including layout changes implied by things
5890 : : like load permutations). When optimizing for speed, the goal is to
5891 : : reduce the maximum latency attributable to layout changes on any
5892 : : non-cyclical path through the data flow graph.
5893 : :
5894 : : For example, when optimizing a loop nest for speed, we will prefer
5895 : : to make layout changes outside of a loop rather than inside of a loop,
5896 : : and will prefer to make layout changes in parallel rather than serially,
5897 : : even if that increases the overall number of layout changes.
5898 : :
5899 : : The high-level procedure is:
5900 : :
5901 : : (1) Build a graph in which edges go from uses (parents) to definitions
5902 : : (children).
5903 : :
5904 : : (2) Divide the graph into a dag of strongly-connected components (SCCs).
5905 : :
5906 : : (3) When optimizing for speed, partition the nodes in each SCC based
5907 : : on their containing cfg loop. When optimizing for size, treat
5908 : : each SCC as a single partition.
5909 : :
5910 : : This gives us a dag of partitions. The goal is now to assign a
5911 : : layout to each partition.
5912 : :
5913 : : (4) Construct a set of vector layouts that are worth considering.
5914 : : Record which nodes must keep their current layout.
5915 : :
5916 : : (5) Perform a forward walk over the partition dag (from loads to stores)
5917 : : accumulating the "forward" cost of using each layout. When visiting
5918 : : each partition, assign a tentative choice of layout to the partition
5919 : : and use that choice when calculating the cost of using a different
5920 : : layout in successor partitions.
5921 : :
5922 : : (6) Perform a backward walk over the partition dag (from stores to loads),
5923 : : accumulating the "backward" cost of using each layout. When visiting
5924 : : each partition, make a final choice of layout for that partition based
5925 : : on the accumulated forward costs (from (5)) and backward costs
5926 : : (from (6)).
5927 : :
5928 : : (7) Apply the chosen layouts to the SLP graph.
5929 : :
5930 : : For example, consider the SLP statements:
5931 : :
5932 : : S1: a_1 = load
5933 : : loop:
5934 : : S2: a_2 = PHI<a_1, a_3>
5935 : : S3: b_1 = load
5936 : : S4: a_3 = a_2 + b_1
5937 : : exit:
5938 : : S5: a_4 = PHI<a_3>
5939 : : S6: store a_4
5940 : :
5941 : : S2 and S4 form an SCC and are part of the same loop. Every other
5942 : : statement is in a singleton SCC. In this example there is a one-to-one
5943 : : mapping between SCCs and partitions and the partition dag looks like this;
5944 : :
5945 : : S1 S3
5946 : : \ /
5947 : : S2+S4
5948 : : |
5949 : : S5
5950 : : |
5951 : : S6
5952 : :
5953 : : S2, S3 and S4 will have a higher execution frequency than the other
5954 : : statements, so when optimizing for speed, the goal is to avoid any
5955 : : layout changes:
5956 : :
5957 : : - within S3
5958 : : - within S2+S4
5959 : : - on the S3->S2+S4 edge
5960 : :
5961 : : For example, if S3 was originally a reversing load, the goal of the
5962 : : pass is to make it an unreversed load and change the layout on the
5963 : : S1->S2+S4 and S2+S4->S5 edges to compensate. (Changing the layout
5964 : : on S1->S2+S4 and S5->S6 would also be acceptable.)
5965 : :
5966 : : The difference between SCCs and partitions becomes important if we
5967 : : add an outer loop:
5968 : :
5969 : : S1: a_1 = ...
5970 : : loop1:
5971 : : S2: a_2 = PHI<a_1, a_6>
5972 : : S3: b_1 = load
5973 : : S4: a_3 = a_2 + b_1
5974 : : loop2:
5975 : : S5: a_4 = PHI<a_3, a_5>
5976 : : S6: c_1 = load
5977 : : S7: a_5 = a_4 + c_1
5978 : : exit2:
5979 : : S8: a_6 = PHI<a_5>
5980 : : S9: store a_6
5981 : : exit1:
5982 : :
5983 : : Here, S2, S4, S5, S7 and S8 form a single SCC. However, when optimizing
5984 : : for speed, we usually do not want restrictions in the outer loop to "infect"
5985 : : the decision for the inner loop. For example, if an outer-loop node
5986 : : in the SCC contains a statement with a fixed layout, that should not
5987 : : prevent the inner loop from using a different layout. Conversely,
5988 : : the inner loop should not dictate a layout to the outer loop: if the
5989 : : outer loop does a lot of computation, then it may not be efficient to
5990 : : do all of that computation in the inner loop's preferred layout.
5991 : :
5992 : : So when optimizing for speed, we partition the SCC into S2+S4+S8 (outer)
5993 : : and S5+S7 (inner). We also try to arrange partitions so that:
5994 : :
5995 : : - the partition for an outer loop comes before the partition for
5996 : : an inner loop
5997 : :
5998 : : - if a sibling loop A dominates a sibling loop B, A's partition
5999 : : comes before B's
6000 : :
6001 : : This gives the following partition dag for the example above:
6002 : :
6003 : : S1 S3
6004 : : \ /
6005 : : S2+S4+S8 S6
6006 : : | \\ /
6007 : : | S5+S7
6008 : : |
6009 : : S9
6010 : :
6011 : : There are two edges from S2+S4+S8 to S5+S7: one for the edge S4->S5 and
6012 : : one for a reversal of the edge S7->S8.
6013 : :
6014 : : The backward walk picks a layout for S5+S7 before S2+S4+S8. The choice
6015 : : for S2+S4+S8 therefore has to balance the cost of using the outer loop's
6016 : : preferred layout against the cost of changing the layout on entry to the
6017 : : inner loop (S4->S5) and on exit from the inner loop (S7->S8 reversed).
6018 : :
6019 : : Although this works well when optimizing for speed, it has the downside
6020 : : when optimizing for size that the choice of layout for S5+S7 is completely
6021 : : independent of S9, which lessens the chance of reducing the overall number
6022 : : of permutations. We therefore do not partition SCCs when optimizing
6023 : : for size.
6024 : :
6025 : : To give a concrete example of the difference between optimizing
6026 : : for size and speed, consider:
6027 : :
6028 : : a[0] = (b[1] << c[3]) - d[1];
6029 : : a[1] = (b[0] << c[2]) - d[0];
6030 : : a[2] = (b[3] << c[1]) - d[3];
6031 : : a[3] = (b[2] << c[0]) - d[2];
6032 : :
6033 : : There are three different layouts here: one for a, one for b and d,
6034 : : and one for c. When optimizing for speed it is better to permute each
6035 : : of b, c and d into the order required by a, since those permutations
6036 : : happen in parallel. But when optimizing for size, it is better to:
6037 : :
6038 : : - permute c into the same order as b
6039 : : - do the arithmetic
6040 : : - permute the result into the order required by a
6041 : :
6042 : : This gives 2 permutations rather than 3. */
6043 : :
6044 : : class vect_optimize_slp_pass
6045 : : {
6046 : : public:
6047 : 620472 : vect_optimize_slp_pass (vec_info *vinfo) : m_vinfo (vinfo) {}
6048 : : void run ();
6049 : :
6050 : : private:
6051 : : /* Graph building. */
6052 : : struct loop *containing_loop (slp_tree);
6053 : : bool is_cfg_latch_edge (graph_edge *);
6054 : : void build_vertices (hash_set<slp_tree> &, slp_tree);
6055 : : void build_vertices ();
6056 : : void build_graph ();
6057 : :
6058 : : /* Partitioning. */
6059 : : void create_partitions ();
6060 : : template<typename T> void for_each_partition_edge (unsigned int, T);
6061 : :
6062 : : /* Layout selection. */
6063 : : bool is_compatible_layout (slp_tree, unsigned int);
6064 : : int change_layout_cost (slp_tree, unsigned int, unsigned int);
6065 : : slpg_partition_layout_costs &partition_layout_costs (unsigned int,
6066 : : unsigned int);
6067 : : void change_vec_perm_layout (slp_tree, lane_permutation_t &,
6068 : : int, unsigned int);
6069 : : int internal_node_cost (slp_tree, int, unsigned int);
6070 : : void start_choosing_layouts ();
6071 : :
6072 : : /* Cost propagation. */
6073 : : slpg_layout_cost edge_layout_cost (graph_edge *, unsigned int,
6074 : : unsigned int, unsigned int);
6075 : : slpg_layout_cost total_in_cost (unsigned int);
6076 : : slpg_layout_cost forward_cost (graph_edge *, unsigned int, unsigned int);
6077 : : slpg_layout_cost backward_cost (graph_edge *, unsigned int, unsigned int);
6078 : : void forward_pass ();
6079 : : void backward_pass ();
6080 : :
6081 : : /* Rematerialization. */
6082 : : slp_tree get_result_with_layout (slp_tree, unsigned int);
6083 : : void materialize ();
6084 : :
6085 : : /* Clean-up. */
6086 : : void remove_redundant_permutations ();
6087 : :
6088 : : /* Masked load lanes discovery. */
6089 : : void decide_masked_load_lanes ();
6090 : :
6091 : : void dump ();
6092 : :
6093 : : vec_info *m_vinfo;
6094 : :
6095 : : /* True if we should optimize the graph for size, false if we should
6096 : : optimize it for speed. (It wouldn't be easy to make this decision
6097 : : more locally.) */
6098 : : bool m_optimize_size;
6099 : :
6100 : : /* A graph of all SLP nodes, with edges leading from uses to definitions.
6101 : : In other words, a node's predecessors are its slp_tree parents and
6102 : : a node's successors are its slp_tree children. */
6103 : : graph *m_slpg = nullptr;
6104 : :
6105 : : /* The vertices of M_SLPG, indexed by slp_tree::vertex. */
6106 : : auto_vec<slpg_vertex> m_vertices;
6107 : :
6108 : : /* The list of all leaves of M_SLPG. such as external definitions, constants,
6109 : : and loads. */
6110 : : auto_vec<int> m_leafs;
6111 : :
6112 : : /* This array has one entry for every vector layout that we're considering.
6113 : : Element 0 is null and indicates "no change". Other entries describe
6114 : : permutations that are inherent in the current graph and that we would
6115 : : like to reverse if possible.
6116 : :
6117 : : For example, a permutation { 1, 2, 3, 0 } means that something has
6118 : : effectively been permuted in that way, such as a load group
6119 : : { a[1], a[2], a[3], a[0] } (viewed as a permutation of a[0:3]).
6120 : : We'd then like to apply the reverse permutation { 3, 0, 1, 2 }
6121 : : in order to put things "back" in order. */
6122 : : auto_vec<vec<unsigned> > m_perms;
6123 : :
6124 : : /* A partitioning of the nodes for which a layout must be chosen.
6125 : : Each partition represents an <SCC, cfg loop> pair; that is,
6126 : : nodes in different SCCs belong to different partitions, and nodes
6127 : : within an SCC can be further partitioned according to a containing
6128 : : cfg loop. Partition <SCC1, L1> comes before <SCC2, L2> if:
6129 : :
6130 : : - SCC1 != SCC2 and SCC1 is a predecessor of SCC2 in a forward walk
6131 : : from leaves (such as loads) to roots (such as stores).
6132 : :
6133 : : - SCC1 == SCC2 and L1's header strictly dominates L2's header. */
6134 : : auto_vec<slpg_partition_info> m_partitions;
6135 : :
6136 : : /* The list of all nodes for which a layout must be chosen. Nodes for
6137 : : partition P come before the nodes for partition P+1. Nodes within a
6138 : : partition are in reverse postorder. */
6139 : : auto_vec<unsigned int> m_partitioned_nodes;
6140 : :
6141 : : /* Index P * num-layouts + L contains the cost of using layout L
6142 : : for partition P. */
6143 : : auto_vec<slpg_partition_layout_costs> m_partition_layout_costs;
6144 : :
6145 : : /* Index N * num-layouts + L, if nonnull, is a node that provides the
6146 : : original output of node N adjusted to have layout L. */
6147 : : auto_vec<slp_tree> m_node_layouts;
6148 : : };
6149 : :
6150 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph.
6151 : : Also record whether we should optimize anything for speed rather
6152 : : than size. */
6153 : :
6154 : : void
6155 : 11229494 : vect_optimize_slp_pass::build_vertices (hash_set<slp_tree> &visited,
6156 : : slp_tree node)
6157 : : {
6158 : 11229494 : unsigned i;
6159 : 11229494 : slp_tree child;
6160 : :
6161 : 11229494 : if (visited.add (node))
6162 : 11229494 : return;
6163 : :
6164 : 10436104 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
6165 : : {
6166 : 7883591 : basic_block bb = gimple_bb (vect_orig_stmt (rep)->stmt);
6167 : 7150751 : if (optimize_bb_for_speed_p (bb))
6168 : 7030679 : m_optimize_size = false;
6169 : : }
6170 : :
6171 : 10436104 : node->vertex = m_vertices.length ();
6172 : 10436104 : m_vertices.safe_push (slpg_vertex (node));
6173 : :
6174 : 10436104 : bool leaf = true;
6175 : 10436104 : bool force_leaf = false;
6176 : 19902708 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
6177 : 9466604 : if (child)
6178 : : {
6179 : 7914176 : leaf = false;
6180 : 7914176 : build_vertices (visited, child);
6181 : : }
6182 : : else
6183 : : force_leaf = true;
6184 : : /* Since SLP discovery works along use-def edges all cycles have an
6185 : : entry - but there's the exception of cycles where we do not handle
6186 : : the entry explicitely (but with a NULL SLP node), like some reductions
6187 : : and inductions. Force those SLP PHIs to act as leafs to make them
6188 : : backwards reachable. */
6189 : 10436104 : if (leaf || force_leaf)
6190 : 5332990 : m_leafs.safe_push (node->vertex);
6191 : : }
6192 : :
6193 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
6194 : :
6195 : : void
6196 : 1240944 : vect_optimize_slp_pass::build_vertices ()
6197 : : {
6198 : 1240944 : hash_set<slp_tree> visited;
6199 : 1240944 : unsigned i;
6200 : 1240944 : slp_instance instance;
6201 : 1240944 : m_vertices.truncate (0);
6202 : 1240944 : m_leafs.truncate (0);
6203 : 7038150 : FOR_EACH_VEC_ELT (m_vinfo->slp_instances, i, instance)
6204 : 3315318 : build_vertices (visited, SLP_INSTANCE_TREE (instance));
6205 : 1240944 : }
6206 : :
6207 : : /* Apply (reverse) bijectite PERM to VEC. */
6208 : :
6209 : : template <class T>
6210 : : static void
6211 : 186242 : vect_slp_permute (vec<unsigned> perm,
6212 : : vec<T> &vec, bool reverse)
6213 : : {
6214 : 186242 : auto_vec<T, 64> saved;
6215 : 186242 : saved.create (vec.length ());
6216 : 615178 : for (unsigned i = 0; i < vec.length (); ++i)
6217 : 428936 : saved.quick_push (vec[i]);
6218 : :
6219 : 186242 : if (reverse)
6220 : : {
6221 : 1220751 : for (unsigned i = 0; i < vec.length (); ++i)
6222 : 427420 : vec[perm[i]] = saved[i];
6223 : 612964 : for (unsigned i = 0; i < vec.length (); ++i)
6224 : 746563 : gcc_assert (vec[perm[i]] == saved[i]);
6225 : : }
6226 : : else
6227 : : {
6228 : 4428 : for (unsigned i = 0; i < vec.length (); ++i)
6229 : 1516 : vec[i] = saved[perm[i]];
6230 : 187758 : for (unsigned i = 0; i < vec.length (); ++i)
6231 : 2274 : gcc_assert (vec[i] == saved[perm[i]]);
6232 : : }
6233 : 186242 : }
6234 : :
6235 : : /* Return the cfg loop that contains NODE. */
6236 : :
6237 : : struct loop *
6238 : 3852342 : vect_optimize_slp_pass::containing_loop (slp_tree node)
6239 : : {
6240 : 3852342 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
6241 : 3852342 : if (!rep)
6242 : 4310 : return ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father;
6243 : 4225286 : return gimple_bb (vect_orig_stmt (rep)->stmt)->loop_father;
6244 : : }
6245 : :
6246 : : /* Return true if UD (an edge from a use to a definition) is associated
6247 : : with a loop latch edge in the cfg. */
6248 : :
6249 : : bool
6250 : 7914176 : vect_optimize_slp_pass::is_cfg_latch_edge (graph_edge *ud)
6251 : : {
6252 : 7914176 : slp_tree use = m_vertices[ud->src].node;
6253 : 7914176 : slp_tree def = m_vertices[ud->dest].node;
6254 : 7914176 : if ((SLP_TREE_DEF_TYPE (use) != vect_internal_def
6255 : 7914176 : || SLP_TREE_PERMUTE_P (use))
6256 : 7543550 : || SLP_TREE_DEF_TYPE (def) != vect_internal_def)
6257 : : return false;
6258 : :
6259 : 4268386 : stmt_vec_info use_rep = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (use));
6260 : 4268386 : return (is_a<gphi *> (use_rep->stmt)
6261 : 311316 : && bb_loop_header_p (gimple_bb (use_rep->stmt))
6262 : 4417386 : && containing_loop (def) == containing_loop (use));
6263 : : }
6264 : :
6265 : : /* Build the graph. Mark edges that correspond to cfg loop latch edges with
6266 : : a nonnull data field. */
6267 : :
6268 : : void
6269 : 1240944 : vect_optimize_slp_pass::build_graph ()
6270 : : {
6271 : 1240944 : m_optimize_size = true;
6272 : 1240944 : build_vertices ();
6273 : :
6274 : 2481888 : m_slpg = new_graph (m_vertices.length ());
6275 : 14158936 : for (slpg_vertex &v : m_vertices)
6276 : 31780332 : for (slp_tree child : SLP_TREE_CHILDREN (v.node))
6277 : 9466604 : if (child)
6278 : : {
6279 : 7914176 : graph_edge *ud = add_edge (m_slpg, v.node->vertex, child->vertex);
6280 : 7914176 : if (is_cfg_latch_edge (ud))
6281 : 140766 : ud->data = this;
6282 : : }
6283 : 1240944 : }
6284 : :
6285 : : /* Return true if E corresponds to a loop latch edge in the cfg. */
6286 : :
6287 : : static bool
6288 : 4027154 : skip_cfg_latch_edges (graph_edge *e)
6289 : : {
6290 : 4027154 : return e->data;
6291 : : }
6292 : :
6293 : : /* Create the node partitions. */
6294 : :
6295 : : void
6296 : 620472 : vect_optimize_slp_pass::create_partitions ()
6297 : : {
6298 : : /* Calculate a postorder of the graph, ignoring edges that correspond
6299 : : to natural latch edges in the cfg. Reading the vector from the end
6300 : : to the beginning gives the reverse postorder. */
6301 : 620472 : auto_vec<int> initial_rpo;
6302 : 1240944 : graphds_dfs (m_slpg, &m_leafs[0], m_leafs.length (), &initial_rpo,
6303 : : false, NULL, skip_cfg_latch_edges);
6304 : 1861416 : gcc_assert (initial_rpo.length () == m_vertices.length ());
6305 : :
6306 : : /* Calculate the strongly connected components of the graph. */
6307 : 620472 : auto_vec<int> scc_grouping;
6308 : 620472 : unsigned int num_sccs = graphds_scc (m_slpg, NULL, NULL, &scc_grouping);
6309 : :
6310 : : /* Create a new index order in which all nodes from the same SCC are
6311 : : consecutive. Use scc_pos to record the index of the first node in
6312 : : each SCC. */
6313 : 620472 : auto_vec<unsigned int> scc_pos (num_sccs);
6314 : 620472 : int last_component = -1;
6315 : 620472 : unsigned int node_count = 0;
6316 : 7079153 : for (unsigned int node_i : scc_grouping)
6317 : : {
6318 : 5217737 : if (last_component != m_slpg->vertices[node_i].component)
6319 : : {
6320 : 5133427 : last_component = m_slpg->vertices[node_i].component;
6321 : 10266854 : gcc_assert (last_component == int (scc_pos.length ()));
6322 : 5133427 : scc_pos.quick_push (node_count);
6323 : : }
6324 : 5217737 : node_count += 1;
6325 : : }
6326 : 1240944 : gcc_assert (node_count == initial_rpo.length ()
6327 : : && last_component + 1 == int (num_sccs));
6328 : :
6329 : : /* Use m_partitioned_nodes to group nodes into SCC order, with the nodes
6330 : : inside each SCC following the RPO we calculated above. The fact that
6331 : : we ignored natural latch edges when calculating the RPO should ensure
6332 : : that, for natural loop nests:
6333 : :
6334 : : - the first node that we encounter in a cfg loop is the loop header phi
6335 : : - the loop header phis are in dominance order
6336 : :
6337 : : Arranging for this is an optimization (see below) rather than a
6338 : : correctness issue. Unnatural loops with a tangled mess of backedges
6339 : : will still work correctly, but might give poorer results.
6340 : :
6341 : : Also update scc_pos so that it gives 1 + the index of the last node
6342 : : in the SCC. */
6343 : 620472 : m_partitioned_nodes.safe_grow (node_count);
6344 : 6458681 : for (unsigned int old_i = initial_rpo.length (); old_i-- > 0;)
6345 : : {
6346 : 5217737 : unsigned int node_i = initial_rpo[old_i];
6347 : 5217737 : unsigned int new_i = scc_pos[m_slpg->vertices[node_i].component]++;
6348 : 5217737 : m_partitioned_nodes[new_i] = node_i;
6349 : : }
6350 : :
6351 : : /* When optimizing for speed, partition each SCC based on the containing
6352 : : cfg loop. The order we constructed above should ensure that, for natural
6353 : : cfg loops, we'll create sub-SCC partitions for outer loops before
6354 : : the corresponding sub-SCC partitions for inner loops. Similarly,
6355 : : when one sibling loop A dominates another sibling loop B, we should
6356 : : create a sub-SCC partition for A before a sub-SCC partition for B.
6357 : :
6358 : : As above, nothing depends for correctness on whether this achieves
6359 : : a natural nesting, but we should get better results when it does. */
6360 : 1240944 : m_partitions.reserve (m_vertices.length ());
6361 : 620472 : unsigned int next_partition_i = 0;
6362 : 620472 : hash_map<struct loop *, int> loop_partitions;
6363 : 620472 : unsigned int rpo_begin = 0;
6364 : 620472 : unsigned int num_partitioned_nodes = 0;
6365 : 6994843 : for (unsigned int rpo_end : scc_pos)
6366 : : {
6367 : 5133427 : loop_partitions.empty ();
6368 : : unsigned int partition_i = next_partition_i;
6369 : 10351164 : for (unsigned int rpo_i = rpo_begin; rpo_i < rpo_end; ++rpo_i)
6370 : : {
6371 : : /* Handle externals and constants optimistically throughout.
6372 : : But treat existing vectors as fixed since we do not handle
6373 : : permuting them. */
6374 : 5217737 : unsigned int node_i = m_partitioned_nodes[rpo_i];
6375 : 5217737 : auto &vertex = m_vertices[node_i];
6376 : 5217737 : if ((SLP_TREE_DEF_TYPE (vertex.node) == vect_external_def
6377 : 502188 : && !SLP_TREE_VEC_DEFS (vertex.node).exists ())
6378 : 5219718 : || SLP_TREE_DEF_TYPE (vertex.node) == vect_constant_def)
6379 : 1638366 : vertex.partition = -1;
6380 : : else
6381 : : {
6382 : 3579371 : bool existed;
6383 : 3579371 : if (m_optimize_size)
6384 : 25029 : existed = next_partition_i > partition_i;
6385 : : else
6386 : : {
6387 : 3554342 : struct loop *loop = containing_loop (vertex.node);
6388 : 3554342 : auto &entry = loop_partitions.get_or_insert (loop, &existed);
6389 : 3554342 : if (!existed)
6390 : 3471012 : entry = next_partition_i;
6391 : 3554342 : partition_i = entry;
6392 : : }
6393 : 3579371 : if (!existed)
6394 : : {
6395 : 3495946 : m_partitions.quick_push (slpg_partition_info ());
6396 : 3495946 : next_partition_i += 1;
6397 : : }
6398 : 3579371 : vertex.partition = partition_i;
6399 : 3579371 : num_partitioned_nodes += 1;
6400 : 3579371 : m_partitions[partition_i].node_end += 1;
6401 : : }
6402 : : }
6403 : 5133427 : rpo_begin = rpo_end;
6404 : : }
6405 : :
6406 : : /* Assign ranges of consecutive node indices to each partition,
6407 : : in partition order. Start with node_end being the same as
6408 : : node_begin so that the next loop can use it as a counter. */
6409 : 620472 : unsigned int node_begin = 0;
6410 : 5357362 : for (auto &partition : m_partitions)
6411 : : {
6412 : 3495946 : partition.node_begin = node_begin;
6413 : 3495946 : node_begin += partition.node_end;
6414 : 3495946 : partition.node_end = partition.node_begin;
6415 : : }
6416 : 620472 : gcc_assert (node_begin == num_partitioned_nodes);
6417 : :
6418 : : /* Finally build the list of nodes in partition order. */
6419 : 620472 : m_partitioned_nodes.truncate (num_partitioned_nodes);
6420 : 5838209 : for (unsigned int node_i = 0; node_i < m_vertices.length (); ++node_i)
6421 : : {
6422 : 5217737 : int partition_i = m_vertices[node_i].partition;
6423 : 5217737 : if (partition_i >= 0)
6424 : : {
6425 : 3579371 : unsigned int order_i = m_partitions[partition_i].node_end++;
6426 : 3579371 : m_partitioned_nodes[order_i] = node_i;
6427 : : }
6428 : : }
6429 : 620472 : }
6430 : :
6431 : : /* Look for edges from earlier partitions into node NODE_I and edges from
6432 : : node NODE_I into later partitions. Call:
6433 : :
6434 : : FN (ud, other_node_i)
6435 : :
6436 : : for each such use-to-def edge ud, where other_node_i is the node at the
6437 : : other end of the edge. */
6438 : :
6439 : : template<typename T>
6440 : : void
6441 : 3993147 : vect_optimize_slp_pass::for_each_partition_edge (unsigned int node_i, T fn)
6442 : : {
6443 : 3993147 : int partition_i = m_vertices[node_i].partition;
6444 : 3993147 : for (graph_edge *pred = m_slpg->vertices[node_i].pred;
6445 : 6704548 : pred; pred = pred->pred_next)
6446 : : {
6447 : 2711401 : int src_partition_i = m_vertices[pred->src].partition;
6448 : 2711401 : if (src_partition_i >= 0 && src_partition_i != partition_i)
6449 : 2507315 : fn (pred, pred->src);
6450 : : }
6451 : 3993147 : for (graph_edge *succ = m_slpg->vertices[node_i].succ;
6452 : 8492033 : succ; succ = succ->succ_next)
6453 : : {
6454 : 4498886 : int dest_partition_i = m_vertices[succ->dest].partition;
6455 : 4498886 : if (dest_partition_i >= 0 && dest_partition_i != partition_i)
6456 : 2525317 : fn (succ, succ->dest);
6457 : : }
6458 : 3993147 : }
6459 : :
6460 : : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
6461 : : that NODE would operate on. This test is independent of NODE's actual
6462 : : operation. */
6463 : :
6464 : : bool
6465 : 1606520 : vect_optimize_slp_pass::is_compatible_layout (slp_tree node,
6466 : : unsigned int layout_i)
6467 : : {
6468 : 1606520 : if (layout_i == 0)
6469 : : return true;
6470 : :
6471 : 900374 : if (SLP_TREE_LANES (node) != m_perms[layout_i].length ())
6472 : 16617 : return false;
6473 : :
6474 : : return true;
6475 : : }
6476 : :
6477 : : /* Return the cost (in arbtirary units) of going from layout FROM_LAYOUT_I
6478 : : to layout TO_LAYOUT_I for a node like NODE. Return -1 if either of the
6479 : : layouts is incompatible with NODE or if the change is not possible for
6480 : : some other reason.
6481 : :
6482 : : The properties taken from NODE include the number of lanes and the
6483 : : vector type. The actual operation doesn't matter. */
6484 : :
6485 : : int
6486 : 691389 : vect_optimize_slp_pass::change_layout_cost (slp_tree node,
6487 : : unsigned int from_layout_i,
6488 : : unsigned int to_layout_i)
6489 : : {
6490 : 691389 : if (!is_compatible_layout (node, from_layout_i)
6491 : 691389 : || !is_compatible_layout (node, to_layout_i))
6492 : 539 : return -1;
6493 : :
6494 : 690850 : if (from_layout_i == to_layout_i)
6495 : : return 0;
6496 : :
6497 : 290435 : auto_vec<slp_tree, 1> children (1);
6498 : 290435 : children.quick_push (node);
6499 : 290435 : auto_lane_permutation_t perm (SLP_TREE_LANES (node));
6500 : 290435 : if (from_layout_i > 0)
6501 : 836854 : for (unsigned int i : m_perms[from_layout_i])
6502 : 368869 : perm.quick_push ({ 0, i });
6503 : : else
6504 : 436515 : for (unsigned int i = 0; i < SLP_TREE_LANES (node); ++i)
6505 : 302075 : perm.quick_push ({ 0, i });
6506 : 290435 : if (to_layout_i > 0)
6507 : 134867 : vect_slp_permute (m_perms[to_layout_i], perm, true);
6508 : 290435 : auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
6509 : : children, false);
6510 : 290435 : if (count >= 0)
6511 : 285673 : return MAX (count, 1);
6512 : :
6513 : : /* ??? In principle we could try changing via layout 0, giving two
6514 : : layout changes rather than 1. Doing that would require
6515 : : corresponding support in get_result_with_layout. */
6516 : : return -1;
6517 : 290435 : }
6518 : :
6519 : : /* Return the costs of assigning layout LAYOUT_I to partition PARTITION_I. */
6520 : :
6521 : : inline slpg_partition_layout_costs &
6522 : 1021575 : vect_optimize_slp_pass::partition_layout_costs (unsigned int partition_i,
6523 : : unsigned int layout_i)
6524 : : {
6525 : 2043150 : return m_partition_layout_costs[partition_i * m_perms.length () + layout_i];
6526 : : }
6527 : :
6528 : : /* Change PERM in one of two ways:
6529 : :
6530 : : - if IN_LAYOUT_I < 0, accept input operand I in the layout that has been
6531 : : chosen for child I of NODE.
6532 : :
6533 : : - if IN_LAYOUT >= 0, accept all inputs operands with that layout.
6534 : :
6535 : : In both cases, arrange for the output to have layout OUT_LAYOUT_I */
6536 : :
6537 : : void
6538 : 24291 : vect_optimize_slp_pass::
6539 : : change_vec_perm_layout (slp_tree node, lane_permutation_t &perm,
6540 : : int in_layout_i, unsigned int out_layout_i)
6541 : : {
6542 : 144149 : for (auto &entry : perm)
6543 : : {
6544 : 71276 : int this_in_layout_i = in_layout_i;
6545 : 71276 : if (this_in_layout_i < 0)
6546 : : {
6547 : 52961 : slp_tree in_node = SLP_TREE_CHILDREN (node)[entry.first];
6548 : 52961 : unsigned int in_partition_i = m_vertices[in_node->vertex].partition;
6549 : 52961 : if (in_partition_i == -1u)
6550 : 329 : continue;
6551 : 52632 : this_in_layout_i = m_partitions[in_partition_i].layout;
6552 : : }
6553 : 70947 : if (this_in_layout_i > 0)
6554 : 15123 : entry.second = m_perms[this_in_layout_i][entry.second];
6555 : : }
6556 : 24291 : if (out_layout_i > 0)
6557 : 5245 : vect_slp_permute (m_perms[out_layout_i], perm, true);
6558 : 24291 : }
6559 : :
6560 : : /* Check whether the target allows NODE to be rearranged so that the node's
6561 : : output has layout OUT_LAYOUT_I. Return the cost of the change if so,
6562 : : in the same arbitrary units as for change_layout_cost. Return -1 otherwise.
6563 : :
6564 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I < 0, also check whether
6565 : : NODE can adapt to the layout changes that have (perhaps provisionally)
6566 : : been chosen for NODE's children, so that no extra permutations are
6567 : : needed on either the input or the output of NODE.
6568 : :
6569 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I >= 0, instead assume
6570 : : that all inputs will be forced into layout IN_LAYOUT_I beforehand.
6571 : :
6572 : : IN_LAYOUT_I has no meaning for other types of node.
6573 : :
6574 : : Keeping the node as-is is always valid. If the target doesn't appear
6575 : : to support the node as-is, but might realistically support other layouts,
6576 : : then layout 0 instead has the cost of a worst-case permutation. On the
6577 : : one hand, this ensures that every node has at least one valid layout,
6578 : : avoiding what would otherwise be an awkward special case. On the other,
6579 : : it still encourages the pass to change an invalid pre-existing layout
6580 : : choice into a valid one. */
6581 : :
6582 : : int
6583 : 219200 : vect_optimize_slp_pass::internal_node_cost (slp_tree node, int in_layout_i,
6584 : : unsigned int out_layout_i)
6585 : : {
6586 : 219200 : const int fallback_cost = 1;
6587 : :
6588 : 219200 : if (SLP_TREE_PERMUTE_P (node))
6589 : : {
6590 : 21043 : auto_lane_permutation_t tmp_perm;
6591 : 21043 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
6592 : :
6593 : : /* Check that the child nodes support the chosen layout. Checking
6594 : : the first child is enough, since any second child would have the
6595 : : same shape. */
6596 : 21043 : auto first_child = SLP_TREE_CHILDREN (node)[0];
6597 : 21043 : if (in_layout_i > 0
6598 : 21043 : && !is_compatible_layout (first_child, in_layout_i))
6599 : : return -1;
6600 : :
6601 : 20506 : change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
6602 : 41012 : int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
6603 : : node, tmp_perm,
6604 : 20506 : SLP_TREE_CHILDREN (node),
6605 : : false);
6606 : 20506 : if (count < 0)
6607 : : {
6608 : 1454 : if (in_layout_i == 0 && out_layout_i == 0)
6609 : : {
6610 : : /* Use the fallback cost if the node could in principle support
6611 : : some nonzero layout for both the inputs and the outputs.
6612 : : Otherwise assume that the node will be rejected later
6613 : : and rebuilt from scalars. */
6614 : 343 : if (SLP_TREE_LANES (node) == SLP_TREE_LANES (first_child))
6615 : : return fallback_cost;
6616 : 269 : return 0;
6617 : : }
6618 : : return -1;
6619 : : }
6620 : :
6621 : : /* We currently have no way of telling whether the new layout is cheaper
6622 : : or more expensive than the old one. But at least in principle,
6623 : : it should be worth making zero permutations (whole-vector shuffles)
6624 : : cheaper than real permutations, in case the pass is able to remove
6625 : : the latter. */
6626 : 19052 : return count == 0 ? 0 : 1;
6627 : 21043 : }
6628 : :
6629 : 198157 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
6630 : 198157 : if (rep
6631 : 197261 : && STMT_VINFO_DATA_REF (rep)
6632 : 67359 : && DR_IS_READ (STMT_VINFO_DATA_REF (rep))
6633 : 248937 : && SLP_TREE_LOAD_PERMUTATION (node).exists ())
6634 : : {
6635 : 45175 : auto_load_permutation_t tmp_perm;
6636 : 45175 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
6637 : 45175 : if (out_layout_i > 0)
6638 : 16927 : vect_slp_permute (m_perms[out_layout_i], tmp_perm, true);
6639 : :
6640 : 45175 : poly_uint64 vf = 1;
6641 : 45175 : if (auto loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo))
6642 : 9370 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6643 : 45175 : unsigned int n_perms;
6644 : 45175 : if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
6645 : : nullptr, vf, true, false, &n_perms))
6646 : : {
6647 : 2048 : auto rep = SLP_TREE_REPRESENTATIVE (node);
6648 : 2048 : if (out_layout_i == 0)
6649 : : {
6650 : : /* Use the fallback cost if the load is an N-to-N permutation.
6651 : : Otherwise assume that the node will be rejected later
6652 : : and rebuilt from scalars. */
6653 : 1582 : if (STMT_VINFO_GROUPED_ACCESS (rep)
6654 : 3164 : && (DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (rep))
6655 : 1582 : == SLP_TREE_LANES (node)))
6656 : 1011 : return fallback_cost;
6657 : : return 0;
6658 : : }
6659 : : return -1;
6660 : : }
6661 : :
6662 : : /* See the comment above the corresponding VEC_PERM_EXPR handling. */
6663 : 43127 : return n_perms == 0 ? 0 : 1;
6664 : 45175 : }
6665 : :
6666 : : return 0;
6667 : : }
6668 : :
6669 : : /* Decide which element layouts we should consider using. Calculate the
6670 : : weights associated with inserting layout changes on partition edges.
6671 : : Also mark partitions that cannot change layout, by setting their
6672 : : layout to zero. */
6673 : :
6674 : : void
6675 : 620472 : vect_optimize_slp_pass::start_choosing_layouts ()
6676 : : {
6677 : : /* Used to assign unique permutation indices. */
6678 : 620472 : using perm_hash = unbounded_hashmap_traits<
6679 : : vec_free_hash_base<int_hash_base<unsigned>>,
6680 : : int_hash<int, -1, -2>
6681 : : >;
6682 : 620472 : hash_map<vec<unsigned>, int, perm_hash> layout_ids;
6683 : :
6684 : : /* Layout 0 is "no change". */
6685 : 620472 : m_perms.safe_push (vNULL);
6686 : :
6687 : : /* Create layouts from existing permutations. */
6688 : 620472 : auto_load_permutation_t tmp_perm;
6689 : 5440787 : for (unsigned int node_i : m_partitioned_nodes)
6690 : : {
6691 : : /* Leafs also double as entries to the reverse graph. Allow the
6692 : : layout of those to be changed. */
6693 : 3579371 : auto &vertex = m_vertices[node_i];
6694 : 3579371 : auto &partition = m_partitions[vertex.partition];
6695 : 3579371 : if (!m_slpg->vertices[node_i].succ)
6696 : 968645 : partition.layout = 0;
6697 : :
6698 : : /* Loads and VEC_PERM_EXPRs are the only things generating permutes. */
6699 : 3579371 : slp_tree node = vertex.node;
6700 : 3579371 : stmt_vec_info dr_stmt = SLP_TREE_REPRESENTATIVE (node);
6701 : 3579371 : slp_tree child;
6702 : 3579371 : unsigned HOST_WIDE_INT imin, imax = 0;
6703 : 3579371 : bool any_permute = false;
6704 : 3579371 : tmp_perm.truncate (0);
6705 : 3579371 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
6706 : : {
6707 : : /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the node
6708 : : unpermuted, record a layout that reverses this permutation.
6709 : :
6710 : : We would need more work to cope with loads that are internally
6711 : : permuted and also have inputs (such as masks for
6712 : : IFN_MASK_LOADs). */
6713 : 481528 : gcc_assert (partition.layout == 0 && !m_slpg->vertices[node_i].succ);
6714 : 481528 : if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt))
6715 : : {
6716 : 341064 : partition.layout = -1;
6717 : 3564044 : continue;
6718 : : }
6719 : 140464 : dr_stmt = DR_GROUP_FIRST_ELEMENT (dr_stmt);
6720 : 140464 : imin = DR_GROUP_SIZE (dr_stmt) + 1;
6721 : 140464 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
6722 : : }
6723 : 6039490 : else if (SLP_TREE_PERMUTE_P (node)
6724 : 170596 : && SLP_TREE_CHILDREN (node).length () == 1
6725 : 156196 : && (child = SLP_TREE_CHILDREN (node)[0])
6726 : 3254039 : && (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (child))
6727 : 156196 : .is_constant (&imin)))
6728 : : {
6729 : : /* If the child has the same vector size as this node,
6730 : : reversing the permutation can make the permutation a no-op.
6731 : : In other cases it can change a true permutation into a
6732 : : full-vector extract. */
6733 : 156196 : tmp_perm.reserve (SLP_TREE_LANES (node));
6734 : 410840 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6735 : 254644 : tmp_perm.quick_push (SLP_TREE_LANE_PERMUTATION (node)[j].second);
6736 : : }
6737 : : else
6738 : 2941647 : continue;
6739 : :
6740 : 788367 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6741 : : {
6742 : 491707 : unsigned idx = tmp_perm[j];
6743 : 491707 : imin = MIN (imin, idx);
6744 : 491707 : imax = MAX (imax, idx);
6745 : 491707 : if (idx - tmp_perm[0] != j)
6746 : 152276 : any_permute = true;
6747 : : }
6748 : : /* If the span doesn't match we'd disrupt VF computation, avoid
6749 : : that for now. */
6750 : 296660 : if (imax - imin + 1 != SLP_TREE_LANES (node))
6751 : 98781 : continue;
6752 : : /* If there's no permute no need to split one out. In this case
6753 : : we can consider turning a load into a permuted load, if that
6754 : : turns out to be cheaper than alternatives. */
6755 : 197879 : if (!any_permute)
6756 : : {
6757 : 182399 : partition.layout = -1;
6758 : 182399 : continue;
6759 : : }
6760 : :
6761 : : /* For now only handle true permutes, like
6762 : : vect_attempt_slp_rearrange_stmts did. This allows us to be lazy
6763 : : when permuting constants and invariants keeping the permute
6764 : : bijective. */
6765 : 15480 : auto_sbitmap load_index (SLP_TREE_LANES (node));
6766 : 15480 : bitmap_clear (load_index);
6767 : 60196 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6768 : 44716 : bitmap_set_bit (load_index, tmp_perm[j] - imin);
6769 : : unsigned j;
6770 : 59464 : for (j = 0; j < SLP_TREE_LANES (node); ++j)
6771 : 44137 : if (!bitmap_bit_p (load_index, j))
6772 : : break;
6773 : 15480 : if (j != SLP_TREE_LANES (node))
6774 : 153 : continue;
6775 : :
6776 : 15327 : vec<unsigned> perm = vNULL;
6777 : 15327 : perm.safe_grow (SLP_TREE_LANES (node), true);
6778 : 59206 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6779 : 43879 : perm[j] = tmp_perm[j] - imin;
6780 : :
6781 : 30654 : if (int (m_perms.length ()) >= param_vect_max_layout_candidates)
6782 : : {
6783 : : /* Continue to use existing layouts, but don't add any more. */
6784 : 0 : int *entry = layout_ids.get (perm);
6785 : 0 : partition.layout = entry ? *entry : 0;
6786 : 0 : perm.release ();
6787 : : }
6788 : : else
6789 : : {
6790 : 15327 : bool existed;
6791 : 15327 : int &layout_i = layout_ids.get_or_insert (perm, &existed);
6792 : 15327 : if (existed)
6793 : 5324 : perm.release ();
6794 : : else
6795 : : {
6796 : 10003 : layout_i = m_perms.length ();
6797 : 10003 : m_perms.safe_push (perm);
6798 : : }
6799 : 15327 : partition.layout = layout_i;
6800 : : }
6801 : 15480 : }
6802 : :
6803 : : /* Initially assume that every layout is possible and has zero cost
6804 : : in every partition. */
6805 : 620472 : m_partition_layout_costs.safe_grow_cleared (m_partitions.length ()
6806 : 1240944 : * m_perms.length ());
6807 : :
6808 : : /* We have to mark outgoing permutations facing non-associating-reduction
6809 : : graph entries that are not represented as to be materialized.
6810 : : slp_inst_kind_bb_reduc currently only covers associatable reductions. */
6811 : 3519075 : for (slp_instance instance : m_vinfo->slp_instances)
6812 : 1657659 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor)
6813 : : {
6814 : 5220 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
6815 : 5220 : m_partitions[m_vertices[node_i].partition].layout = 0;
6816 : : }
6817 : 1652439 : else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_chain)
6818 : : {
6819 : 2305 : stmt_vec_info stmt_info
6820 : 2305 : = SLP_TREE_REPRESENTATIVE (SLP_INSTANCE_TREE (instance));
6821 : 2305 : vect_reduc_info reduc_info
6822 : 2305 : = info_for_reduction (as_a <loop_vec_info> (m_vinfo),
6823 : : SLP_INSTANCE_TREE (instance));
6824 : 2305 : if (needs_fold_left_reduction_p (TREE_TYPE
6825 : : (gimple_get_lhs (stmt_info->stmt)),
6826 : : VECT_REDUC_INFO_CODE (reduc_info)))
6827 : : {
6828 : 64 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
6829 : 64 : m_partitions[m_vertices[node_i].partition].layout = 0;
6830 : : }
6831 : : }
6832 : :
6833 : : /* Check which layouts each node and partition can handle. Calculate the
6834 : : weights associated with inserting layout changes on edges. */
6835 : 5440787 : for (unsigned int node_i : m_partitioned_nodes)
6836 : : {
6837 : 3579371 : auto &vertex = m_vertices[node_i];
6838 : 3579371 : auto &partition = m_partitions[vertex.partition];
6839 : 3579371 : slp_tree node = vertex.node;
6840 : :
6841 : 3579371 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
6842 : : {
6843 : 3575061 : vertex.weight = vect_slp_node_weight (node);
6844 : :
6845 : : /* We do not handle stores with a permutation, so all
6846 : : incoming permutations must have been materialized.
6847 : :
6848 : : We also don't handle masked grouped loads, which lack a
6849 : : permutation vector. In this case the memory locations
6850 : : form an implicit second input to the loads, on top of the
6851 : : explicit mask input, and the memory input's layout cannot
6852 : : be changed.
6853 : :
6854 : : On the other hand, we do support permuting gather loads and
6855 : : masked gather loads, where each scalar load is independent
6856 : : of the others. This can be useful if the address/index input
6857 : : benefits from permutation. */
6858 : 3575061 : if (STMT_VINFO_DATA_REF (rep)
6859 : 1635511 : && STMT_VINFO_GROUPED_ACCESS (rep)
6860 : 4676674 : && !SLP_TREE_LOAD_PERMUTATION (node).exists ())
6861 : 961149 : partition.layout = 0;
6862 : :
6863 : : /* We cannot change the layout of an operation that is
6864 : : not independent on lanes. Note this is an explicit
6865 : : negative list since that's much shorter than the respective
6866 : : positive one but it's critical to keep maintaining it. */
6867 : 3575061 : if (is_gimple_call (STMT_VINFO_STMT (rep)))
6868 : 22017 : switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep)))
6869 : : {
6870 : 1080 : case CFN_COMPLEX_ADD_ROT90:
6871 : 1080 : case CFN_COMPLEX_ADD_ROT270:
6872 : 1080 : case CFN_COMPLEX_MUL:
6873 : 1080 : case CFN_COMPLEX_MUL_CONJ:
6874 : 1080 : case CFN_VEC_ADDSUB:
6875 : 1080 : case CFN_VEC_FMADDSUB:
6876 : 1080 : case CFN_VEC_FMSUBADD:
6877 : 1080 : partition.layout = 0;
6878 : : default:;
6879 : : }
6880 : : }
6881 : :
6882 : 7895029 : auto process_edge = [&](graph_edge *ud, unsigned int other_node_i)
6883 : : {
6884 : 4315658 : auto &other_vertex = m_vertices[other_node_i];
6885 : :
6886 : : /* Count the number of edges from earlier partitions and the number
6887 : : of edges to later partitions. */
6888 : 4315658 : if (other_vertex.partition < vertex.partition)
6889 : 2157829 : partition.in_degree += 1;
6890 : : else
6891 : 2157829 : partition.out_degree += 1;
6892 : :
6893 : : /* If the current node uses the result of OTHER_NODE_I, accumulate
6894 : : the effects of that. */
6895 : 4315658 : if (ud->src == int (node_i))
6896 : : {
6897 : 2157829 : other_vertex.out_weight += vertex.weight;
6898 : 2157829 : other_vertex.out_degree += 1;
6899 : : }
6900 : 7895029 : };
6901 : 3579371 : for_each_partition_edge (node_i, process_edge);
6902 : : }
6903 : 620472 : }
6904 : :
6905 : : /* Return the incoming costs for node NODE_I, assuming that each input keeps
6906 : : its current (provisional) choice of layout. The inputs do not necessarily
6907 : : have the same layout as each other. */
6908 : :
6909 : : slpg_layout_cost
6910 : 2999 : vect_optimize_slp_pass::total_in_cost (unsigned int node_i)
6911 : : {
6912 : 2999 : auto &vertex = m_vertices[node_i];
6913 : 2999 : slpg_layout_cost cost;
6914 : 10954 : auto add_cost = [&](graph_edge *, unsigned int other_node_i)
6915 : : {
6916 : 7955 : auto &other_vertex = m_vertices[other_node_i];
6917 : 7955 : if (other_vertex.partition < vertex.partition)
6918 : : {
6919 : 5065 : auto &other_partition = m_partitions[other_vertex.partition];
6920 : 10130 : auto &other_costs = partition_layout_costs (other_vertex.partition,
6921 : 5065 : other_partition.layout);
6922 : 5065 : slpg_layout_cost this_cost = other_costs.in_cost;
6923 : 5065 : this_cost.add_serial_cost (other_costs.internal_cost);
6924 : 5065 : this_cost.split (other_partition.out_degree);
6925 : 5065 : cost.add_parallel_cost (this_cost);
6926 : : }
6927 : 10954 : };
6928 : 2999 : for_each_partition_edge (node_i, add_cost);
6929 : 2999 : return cost;
6930 : : }
6931 : :
6932 : : /* Return the cost of switching between layout LAYOUT1_I (at node NODE1_I)
6933 : : and layout LAYOUT2_I on cross-partition use-to-def edge UD. Return
6934 : : slpg_layout_cost::impossible () if the change isn't possible. */
6935 : :
6936 : : slpg_layout_cost
6937 : 691389 : vect_optimize_slp_pass::
6938 : : edge_layout_cost (graph_edge *ud, unsigned int node1_i, unsigned int layout1_i,
6939 : : unsigned int layout2_i)
6940 : : {
6941 : 691389 : auto &def_vertex = m_vertices[ud->dest];
6942 : 691389 : auto &use_vertex = m_vertices[ud->src];
6943 : 691389 : auto def_layout_i = ud->dest == int (node1_i) ? layout1_i : layout2_i;
6944 : 691389 : auto use_layout_i = ud->dest == int (node1_i) ? layout2_i : layout1_i;
6945 : 691389 : auto factor = change_layout_cost (def_vertex.node, def_layout_i,
6946 : : use_layout_i);
6947 : 691389 : if (factor < 0)
6948 : 5301 : return slpg_layout_cost::impossible ();
6949 : :
6950 : : /* We have a choice of putting the layout change at the site of the
6951 : : definition or at the site of the use. Prefer the former when
6952 : : optimizing for size or when the execution frequency of the
6953 : : definition is no greater than the combined execution frequencies of
6954 : : the uses. When putting the layout change at the site of the definition,
6955 : : divvy up the cost among all consumers. */
6956 : 686088 : if (m_optimize_size || def_vertex.weight <= def_vertex.out_weight)
6957 : : {
6958 : 667550 : slpg_layout_cost cost = { def_vertex.weight * factor, m_optimize_size };
6959 : 667550 : cost.split (def_vertex.out_degree);
6960 : 667550 : return cost;
6961 : : }
6962 : 18538 : return { use_vertex.weight * factor, m_optimize_size };
6963 : : }
6964 : :
6965 : : /* UD represents a use-def link between FROM_NODE_I and a node in a later
6966 : : partition; FROM_NODE_I could be the definition node or the use node.
6967 : : The node at the other end of the link wants to use layout TO_LAYOUT_I.
6968 : : Return the cost of any necessary fix-ups on edge UD, or return
6969 : : slpg_layout_cost::impossible () if the change isn't possible.
6970 : :
6971 : : At this point, FROM_NODE_I's partition has chosen the cheapest
6972 : : layout based on the information available so far, but this choice
6973 : : is only provisional. */
6974 : :
6975 : : slpg_layout_cost
6976 : 180334 : vect_optimize_slp_pass::forward_cost (graph_edge *ud, unsigned int from_node_i,
6977 : : unsigned int to_layout_i)
6978 : : {
6979 : 180334 : auto &from_vertex = m_vertices[from_node_i];
6980 : 180334 : unsigned int from_partition_i = from_vertex.partition;
6981 : 180334 : slpg_partition_info &from_partition = m_partitions[from_partition_i];
6982 : 180334 : gcc_assert (from_partition.layout >= 0);
6983 : :
6984 : : /* First calculate the cost on the assumption that FROM_PARTITION sticks
6985 : : with its current layout preference. */
6986 : 180334 : slpg_layout_cost cost = slpg_layout_cost::impossible ();
6987 : 180334 : auto edge_cost = edge_layout_cost (ud, from_node_i,
6988 : 180334 : from_partition.layout, to_layout_i);
6989 : 180334 : if (edge_cost.is_possible ())
6990 : : {
6991 : 355218 : auto &from_costs = partition_layout_costs (from_partition_i,
6992 : 177609 : from_partition.layout);
6993 : 177609 : cost = from_costs.in_cost;
6994 : 177609 : cost.add_serial_cost (from_costs.internal_cost);
6995 : 177609 : cost.split (from_partition.out_degree);
6996 : 177609 : cost.add_serial_cost (edge_cost);
6997 : : }
6998 : 2725 : else if (from_partition.layout == 0)
6999 : : /* We must allow the source partition to have layout 0 as a fallback,
7000 : : in case all other options turn out to be impossible. */
7001 : 2725 : return cost;
7002 : :
7003 : : /* Take the minimum of that cost and the cost that applies if
7004 : : FROM_PARTITION instead switches to TO_LAYOUT_I. */
7005 : 177609 : auto &direct_layout_costs = partition_layout_costs (from_partition_i,
7006 : : to_layout_i);
7007 : 177609 : if (direct_layout_costs.is_possible ())
7008 : : {
7009 : 162890 : slpg_layout_cost direct_cost = direct_layout_costs.in_cost;
7010 : 162890 : direct_cost.add_serial_cost (direct_layout_costs.internal_cost);
7011 : 162890 : direct_cost.split (from_partition.out_degree);
7012 : 162890 : if (!cost.is_possible ()
7013 : 162890 : || direct_cost.is_better_than (cost, m_optimize_size))
7014 : 44299 : cost = direct_cost;
7015 : : }
7016 : :
7017 : 177609 : return cost;
7018 : : }
7019 : :
7020 : : /* UD represents a use-def link between TO_NODE_I and a node in an earlier
7021 : : partition; TO_NODE_I could be the definition node or the use node.
7022 : : The node at the other end of the link wants to use layout FROM_LAYOUT_I;
7023 : : return the cost of any necessary fix-ups on edge UD, or
7024 : : slpg_layout_cost::impossible () if the choice cannot be made.
7025 : :
7026 : : At this point, TO_NODE_I's partition has a fixed choice of layout. */
7027 : :
7028 : : slpg_layout_cost
7029 : 169345 : vect_optimize_slp_pass::backward_cost (graph_edge *ud, unsigned int to_node_i,
7030 : : unsigned int from_layout_i)
7031 : : {
7032 : 169345 : auto &to_vertex = m_vertices[to_node_i];
7033 : 169345 : unsigned int to_partition_i = to_vertex.partition;
7034 : 169345 : slpg_partition_info &to_partition = m_partitions[to_partition_i];
7035 : 169345 : gcc_assert (to_partition.layout >= 0);
7036 : :
7037 : : /* If TO_NODE_I is a VEC_PERM_EXPR consumer, see whether it can be
7038 : : adjusted for this input having layout FROM_LAYOUT_I. Assume that
7039 : : any other inputs keep their current choice of layout. */
7040 : 169345 : auto &to_costs = partition_layout_costs (to_partition_i,
7041 : : to_partition.layout);
7042 : 169345 : if (ud->src == int (to_node_i)
7043 : 169183 : && SLP_TREE_PERMUTE_P (to_vertex.node))
7044 : : {
7045 : 9046 : auto &from_partition = m_partitions[m_vertices[ud->dest].partition];
7046 : 9046 : auto old_layout = from_partition.layout;
7047 : 9046 : from_partition.layout = from_layout_i;
7048 : 18092 : int factor = internal_node_cost (to_vertex.node, -1,
7049 : 9046 : to_partition.layout);
7050 : 9046 : from_partition.layout = old_layout;
7051 : 9046 : if (factor >= 0)
7052 : : {
7053 : 8428 : slpg_layout_cost cost = to_costs.out_cost;
7054 : 16856 : cost.add_serial_cost ({ to_vertex.weight * factor,
7055 : 8428 : m_optimize_size });
7056 : 8428 : cost.split (to_partition.in_degree);
7057 : 8428 : return cost;
7058 : : }
7059 : : }
7060 : :
7061 : : /* Compute the cost if we insert any necessary layout change on edge UD. */
7062 : 160917 : auto edge_cost = edge_layout_cost (ud, to_node_i,
7063 : 160917 : to_partition.layout, from_layout_i);
7064 : 160917 : if (edge_cost.is_possible ())
7065 : : {
7066 : 160917 : slpg_layout_cost cost = to_costs.out_cost;
7067 : 160917 : cost.add_serial_cost (to_costs.internal_cost);
7068 : 160917 : cost.split (to_partition.in_degree);
7069 : 160917 : cost.add_serial_cost (edge_cost);
7070 : 160917 : return cost;
7071 : : }
7072 : :
7073 : 0 : return slpg_layout_cost::impossible ();
7074 : : }
7075 : :
7076 : : /* Make a forward pass through the partitions, accumulating input costs.
7077 : : Make a tentative (provisional) choice of layout for each partition,
7078 : : ensuring that this choice still allows later partitions to keep
7079 : : their original layout. */
7080 : :
7081 : : void
7082 : 9583 : vect_optimize_slp_pass::forward_pass ()
7083 : : {
7084 : 127374 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
7085 : : ++partition_i)
7086 : : {
7087 : 117791 : auto &partition = m_partitions[partition_i];
7088 : :
7089 : : /* If the partition consists of a single VEC_PERM_EXPR, precompute
7090 : : the incoming cost that would apply if every predecessor partition
7091 : : keeps its current layout. This is used within the loop below. */
7092 : 117791 : slpg_layout_cost in_cost;
7093 : 117791 : slp_tree single_node = nullptr;
7094 : 117791 : if (partition.node_end == partition.node_begin + 1)
7095 : : {
7096 : 112790 : unsigned int node_i = m_partitioned_nodes[partition.node_begin];
7097 : 112790 : single_node = m_vertices[node_i].node;
7098 : 112790 : if (SLP_TREE_PERMUTE_P (single_node))
7099 : 2999 : in_cost = total_in_cost (node_i);
7100 : : }
7101 : :
7102 : : /* Go through the possible layouts. Decide which ones are valid
7103 : : for this partition and record which of the valid layouts has
7104 : : the lowest cost. */
7105 : 117791 : unsigned int min_layout_i = 0;
7106 : 117791 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7107 : 359299 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7108 : : {
7109 : 241508 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7110 : 241508 : if (!layout_costs.is_possible ())
7111 : 53442 : continue;
7112 : :
7113 : : /* If the recorded layout is already 0 then the layout cannot
7114 : : change. */
7115 : 241508 : if (partition.layout == 0 && layout_i != 0)
7116 : : {
7117 : 34378 : layout_costs.mark_impossible ();
7118 : 34378 : continue;
7119 : : }
7120 : :
7121 : 207130 : bool is_possible = true;
7122 : 410953 : for (unsigned int order_i = partition.node_begin;
7123 : 410953 : order_i < partition.node_end; ++order_i)
7124 : : {
7125 : 220441 : unsigned int node_i = m_partitioned_nodes[order_i];
7126 : 220441 : auto &vertex = m_vertices[node_i];
7127 : :
7128 : : /* Reject the layout if it is individually incompatible
7129 : : with any node in the partition. */
7130 : 220441 : if (!is_compatible_layout (vertex.node, layout_i))
7131 : : {
7132 : 15541 : is_possible = false;
7133 : 16618 : break;
7134 : : }
7135 : :
7136 : 557884 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7137 : : {
7138 : 352984 : auto &other_vertex = m_vertices[other_node_i];
7139 : 352984 : if (other_vertex.partition < vertex.partition)
7140 : : {
7141 : : /* Accumulate the incoming costs from earlier
7142 : : partitions, plus the cost of any layout changes
7143 : : on UD itself. */
7144 : 180334 : auto cost = forward_cost (ud, other_node_i, layout_i);
7145 : 180334 : if (!cost.is_possible ())
7146 : 2725 : is_possible = false;
7147 : : else
7148 : 177609 : layout_costs.in_cost.add_parallel_cost (cost);
7149 : : }
7150 : : else
7151 : : /* Reject the layout if it would make layout 0 impossible
7152 : : for later partitions. This amounts to testing that the
7153 : : target supports reversing the layout change on edges
7154 : : to later partitions.
7155 : :
7156 : : In principle, it might be possible to push a layout
7157 : : change all the way down a graph, so that it never
7158 : : needs to be reversed and so that the target doesn't
7159 : : need to support the reverse operation. But it would
7160 : : be awkward to bail out if we hit a partition that
7161 : : does not support the new layout, especially since
7162 : : we are not dealing with a lattice. */
7163 : 172650 : is_possible &= edge_layout_cost (ud, other_node_i, 0,
7164 : 172650 : layout_i).is_possible ();
7165 : 557884 : };
7166 : 204900 : for_each_partition_edge (node_i, add_cost);
7167 : :
7168 : : /* Accumulate the cost of using LAYOUT_I within NODE,
7169 : : both for the inputs and the outputs. */
7170 : 204900 : int factor = internal_node_cost (vertex.node, layout_i,
7171 : : layout_i);
7172 : 204900 : if (factor < 0)
7173 : : {
7174 : 1077 : is_possible = false;
7175 : 1077 : break;
7176 : : }
7177 : 203823 : else if (factor)
7178 : 34875 : layout_costs.internal_cost.add_serial_cost
7179 : 34875 : ({ vertex.weight * factor, m_optimize_size });
7180 : : }
7181 : 207130 : if (!is_possible)
7182 : : {
7183 : 19064 : layout_costs.mark_impossible ();
7184 : 19064 : continue;
7185 : : }
7186 : :
7187 : : /* Combine the incoming and partition-internal costs. */
7188 : 188066 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7189 : 188066 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7190 : :
7191 : : /* If this partition consists of a single VEC_PERM_EXPR, see
7192 : : if the VEC_PERM_EXPR can be changed to support output layout
7193 : : LAYOUT_I while keeping all the provisional choices of input
7194 : : layout. */
7195 : 188066 : if (single_node && SLP_TREE_PERMUTE_P (single_node))
7196 : : {
7197 : 5254 : int factor = internal_node_cost (single_node, -1, layout_i);
7198 : 5254 : if (factor >= 0)
7199 : : {
7200 : 4835 : auto weight = m_vertices[single_node->vertex].weight;
7201 : 4835 : slpg_layout_cost internal_cost
7202 : 4835 : = { weight * factor, m_optimize_size };
7203 : :
7204 : 4835 : slpg_layout_cost alt_cost = in_cost;
7205 : 4835 : alt_cost.add_serial_cost (internal_cost);
7206 : 4835 : if (alt_cost.is_better_than (combined_cost, m_optimize_size))
7207 : : {
7208 : 1532 : combined_cost = alt_cost;
7209 : 1532 : layout_costs.in_cost = in_cost;
7210 : 1532 : layout_costs.internal_cost = internal_cost;
7211 : : }
7212 : : }
7213 : : }
7214 : :
7215 : : /* Record the layout with the lowest cost. Prefer layout 0 in
7216 : : the event of a tie between it and another layout. */
7217 : 188066 : if (!min_layout_cost.is_possible ()
7218 : 70275 : || combined_cost.is_better_than (min_layout_cost,
7219 : 70275 : m_optimize_size))
7220 : : {
7221 : 136089 : min_layout_i = layout_i;
7222 : 136089 : min_layout_cost = combined_cost;
7223 : : }
7224 : : }
7225 : :
7226 : : /* This loop's handling of earlier partitions should ensure that
7227 : : choosing the original layout for the current partition is no
7228 : : less valid than it was in the original graph, even with the
7229 : : provisional layout choices for those earlier partitions. */
7230 : 117791 : gcc_assert (min_layout_cost.is_possible ());
7231 : 117791 : partition.layout = min_layout_i;
7232 : : }
7233 : 9583 : }
7234 : :
7235 : : /* Make a backward pass through the partitions, accumulating output costs.
7236 : : Make a final choice of layout for each partition. */
7237 : :
7238 : : void
7239 : 9583 : vect_optimize_slp_pass::backward_pass ()
7240 : : {
7241 : 136957 : for (unsigned int partition_i = m_partitions.length (); partition_i-- > 0;)
7242 : : {
7243 : 117791 : auto &partition = m_partitions[partition_i];
7244 : :
7245 : 117791 : unsigned int min_layout_i = 0;
7246 : 117791 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7247 : 359299 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7248 : : {
7249 : 241508 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7250 : 241508 : if (!layout_costs.is_possible ())
7251 : 53442 : continue;
7252 : :
7253 : : /* Accumulate the costs from successor partitions. */
7254 : 188066 : bool is_possible = true;
7255 : 389404 : for (unsigned int order_i = partition.node_begin;
7256 : 389404 : order_i < partition.node_end; ++order_i)
7257 : : {
7258 : 201338 : unsigned int node_i = m_partitioned_nodes[order_i];
7259 : 201338 : auto &vertex = m_vertices[node_i];
7260 : 548171 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7261 : : {
7262 : 346833 : auto &other_vertex = m_vertices[other_node_i];
7263 : 346833 : auto &other_partition = m_partitions[other_vertex.partition];
7264 : 346833 : if (other_vertex.partition > vertex.partition)
7265 : : {
7266 : : /* Accumulate the incoming costs from later
7267 : : partitions, plus the cost of any layout changes
7268 : : on UD itself. */
7269 : 169345 : auto cost = backward_cost (ud, other_node_i, layout_i);
7270 : 169345 : if (!cost.is_possible ())
7271 : 0 : is_possible = false;
7272 : : else
7273 : 169345 : layout_costs.out_cost.add_parallel_cost (cost);
7274 : : }
7275 : : else
7276 : : /* Make sure that earlier partitions can (if necessary
7277 : : or beneficial) keep the layout that they chose in
7278 : : the forward pass. This ensures that there is at
7279 : : least one valid choice of layout. */
7280 : 177488 : is_possible &= edge_layout_cost (ud, other_node_i,
7281 : 177488 : other_partition.layout,
7282 : 177488 : layout_i).is_possible ();
7283 : 548171 : };
7284 : 201338 : for_each_partition_edge (node_i, add_cost);
7285 : : }
7286 : 188066 : if (!is_possible)
7287 : : {
7288 : 0 : layout_costs.mark_impossible ();
7289 : 0 : continue;
7290 : : }
7291 : :
7292 : : /* Locally combine the costs from the forward and backward passes.
7293 : : (This combined cost is not passed on, since that would lead
7294 : : to double counting.) */
7295 : 188066 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7296 : 188066 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7297 : 188066 : combined_cost.add_serial_cost (layout_costs.out_cost);
7298 : :
7299 : : /* Record the layout with the lowest cost. Prefer layout 0 in
7300 : : the event of a tie between it and another layout. */
7301 : 188066 : if (!min_layout_cost.is_possible ()
7302 : 70275 : || combined_cost.is_better_than (min_layout_cost,
7303 : 70275 : m_optimize_size))
7304 : : {
7305 : 130768 : min_layout_i = layout_i;
7306 : 130768 : min_layout_cost = combined_cost;
7307 : : }
7308 : : }
7309 : :
7310 : 117791 : gcc_assert (min_layout_cost.is_possible ());
7311 : 117791 : partition.layout = min_layout_i;
7312 : : }
7313 : 9583 : }
7314 : :
7315 : : /* Return a node that applies layout TO_LAYOUT_I to the original form of NODE.
7316 : : NODE already has the layout that was selected for its partition. */
7317 : :
7318 : : slp_tree
7319 : 149147 : vect_optimize_slp_pass::get_result_with_layout (slp_tree node,
7320 : : unsigned int to_layout_i)
7321 : : {
7322 : 149147 : unsigned int result_i = node->vertex * m_perms.length () + to_layout_i;
7323 : 149147 : slp_tree result = m_node_layouts[result_i];
7324 : 149147 : if (result)
7325 : : return result;
7326 : :
7327 : 148641 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
7328 : 148641 : || (SLP_TREE_DEF_TYPE (node) == vect_external_def
7329 : : /* We can't permute vector defs in place. */
7330 : 20151 : && SLP_TREE_VEC_DEFS (node).is_empty ()))
7331 : : {
7332 : : /* If the vector is uniform or unchanged, there's nothing to do. */
7333 : 38088 : if (to_layout_i == 0 || vect_slp_tree_uniform_p (node))
7334 : : result = node;
7335 : : else
7336 : : {
7337 : 1652 : auto scalar_ops = SLP_TREE_SCALAR_OPS (node).copy ();
7338 : 1652 : result = vect_create_new_slp_node (scalar_ops);
7339 : 1652 : vect_slp_permute (m_perms[to_layout_i], scalar_ops, true);
7340 : : }
7341 : : }
7342 : : else
7343 : : {
7344 : 110553 : unsigned int partition_i = m_vertices[node->vertex].partition;
7345 : 110553 : unsigned int from_layout_i = m_partitions[partition_i].layout;
7346 : 110553 : if (from_layout_i == to_layout_i)
7347 : 109921 : return node;
7348 : :
7349 : : /* If NODE is itself a VEC_PERM_EXPR, try to create a parallel
7350 : : permutation instead of a serial one. Leave the new permutation
7351 : : in TMP_PERM on success. */
7352 : 632 : auto_lane_permutation_t tmp_perm;
7353 : 632 : unsigned int num_inputs = 1;
7354 : 632 : if (SLP_TREE_PERMUTE_P (node))
7355 : : {
7356 : 6 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
7357 : 6 : if (from_layout_i != 0)
7358 : 6 : vect_slp_permute (m_perms[from_layout_i], tmp_perm, false);
7359 : 6 : if (to_layout_i != 0)
7360 : 4 : vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
7361 : 6 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
7362 : : tmp_perm,
7363 : 6 : SLP_TREE_CHILDREN (node),
7364 : : false) >= 0)
7365 : 6 : num_inputs = SLP_TREE_CHILDREN (node).length ();
7366 : : else
7367 : 0 : tmp_perm.truncate (0);
7368 : : }
7369 : :
7370 : 632 : if (dump_enabled_p ())
7371 : : {
7372 : 72 : if (tmp_perm.length () > 0)
7373 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
7374 : : "duplicating permutation node %p with"
7375 : : " layout %d\n",
7376 : : (void *) node, to_layout_i);
7377 : : else
7378 : 66 : dump_printf_loc (MSG_NOTE, vect_location,
7379 : : "inserting permutation node in place of %p\n",
7380 : : (void *) node);
7381 : : }
7382 : :
7383 : 632 : unsigned int num_lanes = SLP_TREE_LANES (node);
7384 : 632 : result = vect_create_new_slp_node (num_inputs, VEC_PERM_EXPR);
7385 : 632 : if (SLP_TREE_SCALAR_STMTS (node).length ())
7386 : : {
7387 : 631 : auto &stmts = SLP_TREE_SCALAR_STMTS (result);
7388 : 631 : stmts.safe_splice (SLP_TREE_SCALAR_STMTS (node));
7389 : 631 : if (from_layout_i != 0)
7390 : 349 : vect_slp_permute (m_perms[from_layout_i], stmts, false);
7391 : 631 : if (to_layout_i != 0)
7392 : 286 : vect_slp_permute (m_perms[to_layout_i], stmts, true);
7393 : : }
7394 : 632 : SLP_TREE_REPRESENTATIVE (result) = SLP_TREE_REPRESENTATIVE (node);
7395 : 632 : SLP_TREE_LANES (result) = num_lanes;
7396 : 632 : SLP_TREE_VECTYPE (result) = SLP_TREE_VECTYPE (node);
7397 : 632 : result->vertex = -1;
7398 : :
7399 : 632 : auto &lane_perm = SLP_TREE_LANE_PERMUTATION (result);
7400 : 632 : if (tmp_perm.length ())
7401 : : {
7402 : 6 : lane_perm.safe_splice (tmp_perm);
7403 : 6 : SLP_TREE_CHILDREN (result).safe_splice (SLP_TREE_CHILDREN (node));
7404 : : }
7405 : : else
7406 : : {
7407 : 626 : lane_perm.create (num_lanes);
7408 : 1936 : for (unsigned j = 0; j < num_lanes; ++j)
7409 : 1310 : lane_perm.quick_push ({ 0, j });
7410 : 626 : if (from_layout_i != 0)
7411 : 343 : vect_slp_permute (m_perms[from_layout_i], lane_perm, false);
7412 : 626 : if (to_layout_i != 0)
7413 : 283 : vect_slp_permute (m_perms[to_layout_i], lane_perm, true);
7414 : 626 : SLP_TREE_CHILDREN (result).safe_push (node);
7415 : : }
7416 : 2532 : for (slp_tree child : SLP_TREE_CHILDREN (result))
7417 : 636 : child->refcnt++;
7418 : 632 : }
7419 : 38720 : m_node_layouts[result_i] = result;
7420 : 38720 : return result;
7421 : : }
7422 : :
7423 : : /* Apply the chosen vector layouts to the SLP graph. */
7424 : :
7425 : : void
7426 : 9583 : vect_optimize_slp_pass::materialize ()
7427 : : {
7428 : : /* We no longer need the costs, so avoid having two O(N * P) arrays
7429 : : live at the same time. */
7430 : 9583 : m_partition_layout_costs.release ();
7431 : 28749 : m_node_layouts.safe_grow_cleared (m_vertices.length () * m_perms.length ());
7432 : :
7433 : 19166 : auto_sbitmap fully_folded (m_vertices.length ());
7434 : 9583 : bitmap_clear (fully_folded);
7435 : 155287 : for (unsigned int node_i : m_partitioned_nodes)
7436 : : {
7437 : 126538 : auto &vertex = m_vertices[node_i];
7438 : 126538 : slp_tree node = vertex.node;
7439 : 126538 : int layout_i = m_partitions[vertex.partition].layout;
7440 : 126538 : gcc_assert (layout_i >= 0);
7441 : :
7442 : : /* Rearrange the scalar statements to match the chosen layout. */
7443 : 126538 : if (layout_i > 0)
7444 : 13189 : vect_slp_permute (m_perms[layout_i],
7445 : 13189 : SLP_TREE_SCALAR_STMTS (node), true);
7446 : :
7447 : : /* Update load and lane permutations. */
7448 : 126538 : if (SLP_TREE_PERMUTE_P (node))
7449 : : {
7450 : : /* First try to absorb the input vector layouts. If that fails,
7451 : : force the inputs to have layout LAYOUT_I too. We checked that
7452 : : that was possible before deciding to use nonzero output layouts.
7453 : : (Note that at this stage we don't really have any guarantee that
7454 : : the target supports the original VEC_PERM_EXPR.) */
7455 : 3442 : auto &perm = SLP_TREE_LANE_PERMUTATION (node);
7456 : 3442 : auto_lane_permutation_t tmp_perm;
7457 : 3442 : tmp_perm.safe_splice (perm);
7458 : 3442 : change_vec_perm_layout (node, tmp_perm, -1, layout_i);
7459 : 3442 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
7460 : : tmp_perm,
7461 : 3442 : SLP_TREE_CHILDREN (node),
7462 : : false) >= 0)
7463 : : {
7464 : 3099 : if (dump_enabled_p ()
7465 : 3915 : && !std::equal (tmp_perm.begin (), tmp_perm.end (),
7466 : : perm.begin ()))
7467 : 62 : dump_printf_loc (MSG_NOTE, vect_location,
7468 : : "absorbing input layouts into %p\n",
7469 : : (void *) node);
7470 : 18304 : std::copy (tmp_perm.begin (), tmp_perm.end (), perm.begin ());
7471 : 3099 : bitmap_set_bit (fully_folded, node_i);
7472 : : }
7473 : : else
7474 : : {
7475 : : /* Not MSG_MISSED because it would make no sense to users. */
7476 : 343 : if (dump_enabled_p ())
7477 : 46 : dump_printf_loc (MSG_NOTE, vect_location,
7478 : : "failed to absorb input layouts into %p\n",
7479 : : (void *) node);
7480 : 343 : change_vec_perm_layout (nullptr, perm, layout_i, layout_i);
7481 : : }
7482 : 3442 : }
7483 : : else
7484 : : {
7485 : 123096 : gcc_assert (!SLP_TREE_LANE_PERMUTATION (node).exists ());
7486 : 123096 : auto &load_perm = SLP_TREE_LOAD_PERMUTATION (node);
7487 : 123096 : if (layout_i > 0)
7488 : : /* ??? When we handle non-bijective permutes the idea
7489 : : is that we can force the load-permutation to be
7490 : : { min, min + 1, min + 2, ... max }. But then the
7491 : : scalar defs might no longer match the lane content
7492 : : which means wrong-code with live lane vectorization.
7493 : : So we possibly have to have NULL entries for those. */
7494 : 13091 : vect_slp_permute (m_perms[layout_i], load_perm, true);
7495 : : }
7496 : : }
7497 : :
7498 : : /* Do this before any nodes disappear, since it involves a walk
7499 : : over the leaves. */
7500 : 9583 : remove_redundant_permutations ();
7501 : :
7502 : : /* Replace each child with a correctly laid-out version. */
7503 : 155287 : for (unsigned int node_i : m_partitioned_nodes)
7504 : : {
7505 : : /* Skip nodes that have already been handled above. */
7506 : 126538 : if (bitmap_bit_p (fully_folded, node_i))
7507 : 3099 : continue;
7508 : :
7509 : 123439 : auto &vertex = m_vertices[node_i];
7510 : 123439 : int in_layout_i = m_partitions[vertex.partition].layout;
7511 : 123439 : gcc_assert (in_layout_i >= 0);
7512 : :
7513 : : unsigned j;
7514 : : slp_tree child;
7515 : 368953 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (vertex.node), j, child)
7516 : : {
7517 : 153707 : if (!child)
7518 : 4560 : continue;
7519 : :
7520 : 149147 : slp_tree new_child = get_result_with_layout (child, in_layout_i);
7521 : 149147 : if (new_child != child)
7522 : : {
7523 : 2534 : vect_free_slp_tree (child);
7524 : 2534 : SLP_TREE_CHILDREN (vertex.node)[j] = new_child;
7525 : 2534 : new_child->refcnt += 1;
7526 : : }
7527 : : }
7528 : : }
7529 : 9583 : }
7530 : :
7531 : : /* Elide load permutations that are not necessary. Such permutations might
7532 : : be pre-existing, rather than created by the layout optimizations. */
7533 : :
7534 : : void
7535 : 620472 : vect_optimize_slp_pass::remove_redundant_permutations ()
7536 : : {
7537 : 4527911 : for (unsigned int node_i : m_leafs)
7538 : : {
7539 : 2666495 : slp_tree node = m_vertices[node_i].node;
7540 : 2666495 : if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
7541 : 2184967 : continue;
7542 : :
7543 : : /* In basic block vectorization we allow any subchain of an interleaving
7544 : : chain.
7545 : : FORNOW: not in loop SLP because of realignment complications. */
7546 : 481528 : if (is_a <bb_vec_info> (m_vinfo))
7547 : : {
7548 : 150838 : bool subchain_p = true;
7549 : : stmt_vec_info next_load_info = NULL;
7550 : : stmt_vec_info load_info;
7551 : : unsigned j;
7552 : 150838 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
7553 : : {
7554 : 122605 : if (j != 0
7555 : 122605 : && (next_load_info != load_info
7556 : 56860 : || ! load_info
7557 : 56860 : || DR_GROUP_GAP (load_info) != 1))
7558 : : {
7559 : : subchain_p = false;
7560 : : break;
7561 : : }
7562 : 100758 : next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
7563 : : }
7564 : 50080 : if (subchain_p)
7565 : : {
7566 : 28233 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7567 : 28233 : continue;
7568 : : }
7569 : : }
7570 : : else
7571 : : {
7572 : 431448 : loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
7573 : 431448 : stmt_vec_info load_info;
7574 : 431448 : bool this_load_permuted = false;
7575 : 431448 : unsigned j;
7576 : 1282577 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
7577 : 435240 : if (SLP_TREE_LOAD_PERMUTATION (node)[j] != j)
7578 : : {
7579 : : this_load_permuted = true;
7580 : : break;
7581 : : }
7582 : : /* When this isn't a grouped access we know it's single element
7583 : : and contiguous. */
7584 : 431448 : if (!STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (node)[0]))
7585 : : {
7586 : 341064 : if (!this_load_permuted
7587 : 341064 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7588 : 340616 : || SLP_TREE_LANES (node) == 1))
7589 : 340616 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7590 : 341064 : continue;
7591 : : }
7592 : 90384 : stmt_vec_info first_stmt_info
7593 : 90384 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node)[0]);
7594 : 90724 : if (!this_load_permuted
7595 : : /* The load requires permutation when unrolling exposes
7596 : : a gap either because the group is larger than the SLP
7597 : : group-size or because there is a gap between the groups. */
7598 : 90384 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7599 : 75027 : || ((SLP_TREE_LANES (node) == DR_GROUP_SIZE (first_stmt_info))
7600 : 94 : && DR_GROUP_GAP (first_stmt_info) == 0)))
7601 : : {
7602 : 340 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7603 : 340 : continue;
7604 : : }
7605 : : }
7606 : : }
7607 : 620472 : }
7608 : :
7609 : : /* Print the partition graph and layout information to the dump file. */
7610 : :
7611 : : void
7612 : 616 : vect_optimize_slp_pass::dump ()
7613 : : {
7614 : 616 : dump_printf_loc (MSG_NOTE, vect_location,
7615 : : "SLP optimize permutations:\n");
7616 : 1245 : for (unsigned int layout_i = 1; layout_i < m_perms.length (); ++layout_i)
7617 : : {
7618 : 629 : dump_printf_loc (MSG_NOTE, vect_location, " %d: { ", layout_i);
7619 : 629 : const char *sep = "";
7620 : 5346 : for (unsigned int idx : m_perms[layout_i])
7621 : : {
7622 : 3459 : dump_printf (MSG_NOTE, "%s%d", sep, idx);
7623 : 3459 : sep = ", ";
7624 : : }
7625 : 629 : dump_printf (MSG_NOTE, " }\n");
7626 : : }
7627 : 616 : dump_printf_loc (MSG_NOTE, vect_location,
7628 : : "SLP optimize partitions:\n");
7629 : 4982 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
7630 : : ++partition_i)
7631 : : {
7632 : 4366 : auto &partition = m_partitions[partition_i];
7633 : 4366 : dump_printf_loc (MSG_NOTE, vect_location, " -------------\n");
7634 : 4366 : dump_printf_loc (MSG_NOTE, vect_location,
7635 : : " partition %d (layout %d):\n",
7636 : : partition_i, partition.layout);
7637 : 4366 : dump_printf_loc (MSG_NOTE, vect_location, " nodes:\n");
7638 : 8905 : for (unsigned int order_i = partition.node_begin;
7639 : 8905 : order_i < partition.node_end; ++order_i)
7640 : : {
7641 : 4539 : auto &vertex = m_vertices[m_partitioned_nodes[order_i]];
7642 : 9078 : dump_printf_loc (MSG_NOTE, vect_location, " - %p:\n",
7643 : 4539 : (void *) vertex.node);
7644 : 4539 : dump_printf_loc (MSG_NOTE, vect_location,
7645 : : " weight: %f\n",
7646 : : vertex.weight.to_double ());
7647 : 4539 : if (vertex.out_degree)
7648 : 3547 : dump_printf_loc (MSG_NOTE, vect_location,
7649 : : " out weight: %f (degree %d)\n",
7650 : : vertex.out_weight.to_double (),
7651 : : vertex.out_degree);
7652 : 4539 : if (SLP_TREE_PERMUTE_P (vertex.node))
7653 : 454 : dump_printf_loc (MSG_NOTE, vect_location,
7654 : : " op: VEC_PERM_EXPR\n");
7655 : 4085 : else if (auto rep = SLP_TREE_REPRESENTATIVE (vertex.node))
7656 : 4067 : dump_printf_loc (MSG_NOTE, vect_location,
7657 : : " op template: %G", rep->stmt);
7658 : : }
7659 : 4366 : dump_printf_loc (MSG_NOTE, vect_location, " edges:\n");
7660 : 8905 : for (unsigned int order_i = partition.node_begin;
7661 : 8905 : order_i < partition.node_end; ++order_i)
7662 : : {
7663 : 4539 : unsigned int node_i = m_partitioned_nodes[order_i];
7664 : 4539 : auto &vertex = m_vertices[node_i];
7665 : 13741 : auto print_edge = [&](graph_edge *, unsigned int other_node_i)
7666 : : {
7667 : 9202 : auto &other_vertex = m_vertices[other_node_i];
7668 : 9202 : if (other_vertex.partition < vertex.partition)
7669 : 4601 : dump_printf_loc (MSG_NOTE, vect_location,
7670 : : " - %p [%d] --> %p\n",
7671 : 4601 : (void *) other_vertex.node,
7672 : : other_vertex.partition,
7673 : 4601 : (void *) vertex.node);
7674 : : else
7675 : 4601 : dump_printf_loc (MSG_NOTE, vect_location,
7676 : : " - %p --> [%d] %p\n",
7677 : 4601 : (void *) vertex.node,
7678 : : other_vertex.partition,
7679 : 4601 : (void *) other_vertex.node);
7680 : 13741 : };
7681 : 4539 : for_each_partition_edge (node_i, print_edge);
7682 : : }
7683 : :
7684 : 13297 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7685 : : {
7686 : 8931 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7687 : 8931 : if (layout_costs.is_possible ())
7688 : : {
7689 : 7255 : dump_printf_loc (MSG_NOTE, vect_location,
7690 : : " layout %d:%s\n", layout_i,
7691 : 7255 : partition.layout == int (layout_i)
7692 : : ? " (*)" : "");
7693 : 7255 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7694 : 7255 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7695 : 7255 : combined_cost.add_serial_cost (layout_costs.out_cost);
7696 : : #define TEMPLATE "{depth: %f, total: %f}"
7697 : 7255 : dump_printf_loc (MSG_NOTE, vect_location,
7698 : : " " TEMPLATE "\n",
7699 : : layout_costs.in_cost.depth.to_double (),
7700 : : layout_costs.in_cost.total.to_double ());
7701 : 7255 : dump_printf_loc (MSG_NOTE, vect_location,
7702 : : " + " TEMPLATE "\n",
7703 : : layout_costs.internal_cost.depth.to_double (),
7704 : : layout_costs.internal_cost.total.to_double ());
7705 : 7255 : dump_printf_loc (MSG_NOTE, vect_location,
7706 : : " + " TEMPLATE "\n",
7707 : : layout_costs.out_cost.depth.to_double (),
7708 : : layout_costs.out_cost.total.to_double ());
7709 : 7255 : dump_printf_loc (MSG_NOTE, vect_location,
7710 : : " = " TEMPLATE "\n",
7711 : : combined_cost.depth.to_double (),
7712 : : combined_cost.total.to_double ());
7713 : : #undef TEMPLATE
7714 : : }
7715 : : else
7716 : 1676 : dump_printf_loc (MSG_NOTE, vect_location,
7717 : : " layout %d: rejected\n", layout_i);
7718 : : }
7719 : : }
7720 : 616 : }
7721 : :
7722 : : /* Masked load lanes discovery. */
7723 : :
7724 : : void
7725 : 620472 : vect_optimize_slp_pass::decide_masked_load_lanes ()
7726 : : {
7727 : 7079783 : for (auto v : m_vertices)
7728 : : {
7729 : 5218367 : slp_tree node = v.node;
7730 : 5218367 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
7731 : 3578014 : || SLP_TREE_PERMUTE_P (node))
7732 : 1811579 : continue;
7733 : 3406788 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
7734 : 1478664 : if (! STMT_VINFO_GROUPED_ACCESS (stmt_info)
7735 : : /* The mask has to be uniform. */
7736 : 945312 : || STMT_VINFO_SLP_VECT_ONLY (stmt_info)
7737 : 945246 : || ! is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
7738 : 3406843 : || ! gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
7739 : : IFN_MASK_LOAD))
7740 : 3406785 : continue;
7741 : 3 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
7742 : 6 : if (STMT_VINFO_STRIDED_P (stmt_info)
7743 : 3 : || compare_step_with_zero (m_vinfo, stmt_info) <= 0
7744 : 3 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (node),
7745 : 0 : DR_GROUP_SIZE (stmt_info),
7746 : : true) == IFN_LAST)
7747 : 3 : continue;
7748 : :
7749 : : /* Uniform masks need to be suitably represented. */
7750 : 0 : slp_tree mask = SLP_TREE_CHILDREN (node)[0];
7751 : 0 : if (!SLP_TREE_PERMUTE_P (mask)
7752 : 0 : || SLP_TREE_CHILDREN (mask).length () != 1)
7753 : 0 : continue;
7754 : 0 : bool match = true;
7755 : 0 : for (auto perm : SLP_TREE_LANE_PERMUTATION (mask))
7756 : 0 : if (perm.first != 0 || perm.second != 0)
7757 : : {
7758 : : match = false;
7759 : : break;
7760 : : }
7761 : 0 : if (!match)
7762 : 0 : continue;
7763 : :
7764 : : /* Now see if the consumer side matches. */
7765 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
7766 : 0 : pred; pred = pred->pred_next)
7767 : : {
7768 : 0 : slp_tree pred_node = m_vertices[pred->src].node;
7769 : : /* All consumers should be a permute with a single outgoing lane. */
7770 : 0 : if (!SLP_TREE_PERMUTE_P (pred_node)
7771 : 0 : || SLP_TREE_LANES (pred_node) != 1)
7772 : : {
7773 : : match = false;
7774 : : break;
7775 : : }
7776 : 0 : gcc_assert (SLP_TREE_CHILDREN (pred_node).length () == 1);
7777 : : }
7778 : 0 : if (!match)
7779 : 0 : continue;
7780 : : /* Now we can mark the nodes as to use load lanes. */
7781 : 0 : node->ldst_lanes = true;
7782 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
7783 : 0 : pred; pred = pred->pred_next)
7784 : 0 : m_vertices[pred->src].node->ldst_lanes = true;
7785 : : /* The catch is we have to massage the mask. We have arranged
7786 : : analyzed uniform masks to be represented by a splat VEC_PERM
7787 : : which we can now simply elide as we cannot easily re-do SLP
7788 : : discovery here. */
7789 : 0 : slp_tree new_mask = SLP_TREE_CHILDREN (mask)[0];
7790 : 0 : SLP_TREE_REF_COUNT (new_mask)++;
7791 : 0 : SLP_TREE_CHILDREN (node)[0] = new_mask;
7792 : 0 : vect_free_slp_tree (mask);
7793 : : }
7794 : 620472 : }
7795 : :
7796 : : /* Main entry point for the SLP graph optimization pass. */
7797 : :
7798 : : void
7799 : 620472 : vect_optimize_slp_pass::run ()
7800 : : {
7801 : 620472 : build_graph ();
7802 : 620472 : create_partitions ();
7803 : 620472 : start_choosing_layouts ();
7804 : 620472 : if (m_perms.length () > 1)
7805 : : {
7806 : 9583 : forward_pass ();
7807 : 9583 : backward_pass ();
7808 : 9583 : if (dump_enabled_p ())
7809 : 616 : dump ();
7810 : 9583 : materialize ();
7811 : 38752 : while (!m_perms.is_empty ())
7812 : 19586 : m_perms.pop ().release ();
7813 : : }
7814 : : else
7815 : 610889 : remove_redundant_permutations ();
7816 : 620472 : free_graph (m_slpg);
7817 : 620472 : build_graph ();
7818 : 620472 : decide_masked_load_lanes ();
7819 : 620472 : free_graph (m_slpg);
7820 : 620472 : }
7821 : :
7822 : : /* Apply CSE to NODE and its children using BST_MAP. */
7823 : :
7824 : : static void
7825 : 5611973 : vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node)
7826 : : {
7827 : 5611973 : bool put_p = false;
7828 : 5611973 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def
7829 : : /* Besides some VEC_PERM_EXPR, two-operator nodes also
7830 : : lack scalar stmts and thus CSE doesn't work via bst_map. Ideally
7831 : : we'd have sth that works for all internal and external nodes. */
7832 : 5611973 : && !SLP_TREE_SCALAR_STMTS (node).is_empty ())
7833 : : {
7834 : 3951703 : slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node));
7835 : 3951703 : if (leader)
7836 : : {
7837 : : /* We've visited this node already. */
7838 : 395500 : if (!*leader || *leader == node)
7839 : : return;
7840 : :
7841 : 2379 : if (dump_enabled_p ())
7842 : 812 : dump_printf_loc (MSG_NOTE, vect_location,
7843 : : "re-using SLP tree %p for %p\n",
7844 : : (void *)*leader, (void *)node);
7845 : 2379 : vect_free_slp_tree (node);
7846 : 2379 : (*leader)->refcnt += 1;
7847 : 2379 : node = *leader;
7848 : 2379 : return;
7849 : : }
7850 : :
7851 : : /* Avoid creating a cycle by populating the map only after recursion. */
7852 : 3556203 : bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), nullptr);
7853 : 3556203 : node->refcnt += 1;
7854 : 3556203 : put_p = true;
7855 : : /* And recurse. */
7856 : : }
7857 : :
7858 : 15880389 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
7859 : 4730528 : if (child)
7860 : 3954314 : vect_cse_slp_nodes (bst_map, child);
7861 : :
7862 : : /* Now record the node for CSE in other siblings. */
7863 : 5216473 : if (put_p)
7864 : 3556203 : *bst_map->get (SLP_TREE_SCALAR_STMTS (node)) = node;
7865 : : }
7866 : :
7867 : : /* Optimize the SLP graph of VINFO. */
7868 : :
7869 : : void
7870 : 972897 : vect_optimize_slp (vec_info *vinfo)
7871 : : {
7872 : 972897 : if (vinfo->slp_instances.is_empty ())
7873 : : return;
7874 : 620472 : vect_optimize_slp_pass (vinfo).run ();
7875 : :
7876 : : /* Apply CSE again to nodes after permute optimization. */
7877 : 620472 : scalar_stmts_to_slp_tree_map_t *bst_map
7878 : 620472 : = new scalar_stmts_to_slp_tree_map_t ();
7879 : :
7880 : 3519075 : for (auto inst : vinfo->slp_instances)
7881 : 1657659 : vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst));
7882 : :
7883 : 620472 : release_scalar_stmts_to_slp_tree_map (bst_map);
7884 : : }
7885 : :
7886 : : /* Gather loads reachable from the individual SLP graph entries. */
7887 : :
7888 : : void
7889 : 972897 : vect_gather_slp_loads (vec_info *vinfo)
7890 : : {
7891 : 972897 : unsigned i;
7892 : 972897 : slp_instance instance;
7893 : 2630556 : FOR_EACH_VEC_ELT (vinfo->slp_instances, i, instance)
7894 : : {
7895 : 1657659 : hash_set<slp_tree> visited;
7896 : 1657659 : vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance),
7897 : : SLP_INSTANCE_TREE (instance), visited);
7898 : 1657659 : }
7899 : 972897 : }
7900 : :
7901 : : /* For NODE update VF based on the number of lanes and the vector types
7902 : : used. */
7903 : :
7904 : : static void
7905 : 4762263 : vect_update_slp_vf_for_node (slp_tree node, poly_uint64 &vf,
7906 : : hash_set<slp_tree> &visited)
7907 : : {
7908 : 4762263 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
7909 : 2041745 : return;
7910 : 3083316 : if (visited.add (node))
7911 : : return;
7912 : :
7913 : 10929261 : for (slp_tree child : SLP_TREE_CHILDREN (node))
7914 : 3815933 : vect_update_slp_vf_for_node (child, vf, visited);
7915 : :
7916 : : /* We do not visit SLP nodes for constants or externals - those neither
7917 : : have a vector type set yet (vectorizable_* does this) nor do they
7918 : : have max_nunits set. Instead we rely on internal nodes max_nunit
7919 : : to cover constant/external operands.
7920 : : Note that when we stop using fixed size vectors externs and constants
7921 : : shouldn't influence the (minimum) vectorization factor, instead
7922 : : vectorizable_* should honor the vectorization factor when trying to
7923 : : assign vector types to constants and externals and cause iteration
7924 : : to a higher vectorization factor when required. */
7925 : 2720518 : poly_uint64 node_vf
7926 : 2720518 : = calculate_unrolling_factor (node->max_nunits, SLP_TREE_LANES (node));
7927 : 2720518 : vf = force_common_multiple (vf, node_vf);
7928 : :
7929 : : /* For permute nodes that are fed from externs or constants we have to
7930 : : consider their number of lanes as well. Likewise for store-lanes. */
7931 : 2720518 : if (SLP_TREE_PERMUTE_P (node) || node->ldst_lanes)
7932 : 806371 : for (slp_tree child : SLP_TREE_CHILDREN (node))
7933 : 211549 : if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
7934 : : {
7935 : 2787 : poly_uint64 child_vf
7936 : 2787 : = calculate_unrolling_factor (node->max_nunits,
7937 : : SLP_TREE_LANES (child));
7938 : 2787 : vf = force_common_multiple (vf, child_vf);
7939 : : }
7940 : : }
7941 : :
7942 : : /* For each possible SLP instance decide whether to SLP it and calculate overall
7943 : : unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
7944 : : least one instance. */
7945 : :
7946 : : bool
7947 : 375411 : vect_make_slp_decision (loop_vec_info loop_vinfo)
7948 : : {
7949 : 375411 : unsigned int i;
7950 : 375411 : poly_uint64 unrolling_factor = 1;
7951 : 375411 : const vec<slp_instance> &slp_instances
7952 : : = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
7953 : 375411 : slp_instance instance;
7954 : 375411 : int decided_to_slp = 0;
7955 : :
7956 : 375411 : DUMP_VECT_SCOPE ("vect_make_slp_decision");
7957 : :
7958 : 375411 : hash_set<slp_tree> visited;
7959 : 1321741 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
7960 : : {
7961 : 946330 : slp_tree root = SLP_INSTANCE_TREE (instance);
7962 : :
7963 : : /* All unroll factors have the form:
7964 : :
7965 : : GET_MODE_SIZE (vinfo->vector_mode) * X
7966 : :
7967 : : for some rational X, so they must have a common multiple. */
7968 : 946330 : vect_update_slp_vf_for_node (root, unrolling_factor, visited);
7969 : :
7970 : : /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
7971 : : call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
7972 : : loop-based vectorization. Such stmts will be marked as HYBRID. */
7973 : 946330 : vect_mark_slp_stmts (loop_vinfo, root);
7974 : :
7975 : : /* If all instances ended up with vector(1) T roots make sure to
7976 : : not vectorize. RVV for example relies on loop vectorization
7977 : : when some instances are essentially kept scalar. See PR121048. */
7978 : 946330 : if (SLP_TREE_VECTYPE (root)
7979 : 946330 : && known_gt (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (root)), 1U))
7980 : 742027 : decided_to_slp++;
7981 : : }
7982 : :
7983 : 375411 : LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
7984 : :
7985 : 375411 : if (decided_to_slp && dump_enabled_p ())
7986 : : {
7987 : 17667 : dump_printf_loc (MSG_NOTE, vect_location,
7988 : : "Decided to SLP %d instances. Unrolling factor ",
7989 : : decided_to_slp);
7990 : 17667 : dump_dec (MSG_NOTE, unrolling_factor);
7991 : 17667 : dump_printf (MSG_NOTE, "\n");
7992 : : }
7993 : :
7994 : 375411 : return (decided_to_slp > 0);
7995 : 375411 : }
7996 : :
7997 : : /* Initialize a bb_vec_info struct for the statements in BBS basic blocks. */
7998 : :
7999 : 2394109 : _bb_vec_info::_bb_vec_info (vec<basic_block> _bbs, vec_info_shared *shared)
8000 : : : vec_info (vec_info::bb, shared),
8001 : 2394109 : roots (vNULL)
8002 : : {
8003 : : /* The region we are operating on. bbs[0] is the entry, excluding
8004 : : its PHI nodes. In the future we might want to track an explicit
8005 : : entry edge to cover bbs[0] PHI nodes and have a region entry
8006 : : insert location. */
8007 : 2394109 : bbs = _bbs.address ();
8008 : 2394109 : nbbs = _bbs.length ();
8009 : :
8010 : 18840190 : for (unsigned i = 0; i < nbbs; ++i)
8011 : : {
8012 : 16446081 : if (i != 0)
8013 : 21845721 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8014 : 7793749 : gsi_next (&si))
8015 : : {
8016 : 7793749 : gphi *phi = si.phi ();
8017 : 7793749 : gimple_set_uid (phi, 0);
8018 : 7793749 : add_stmt (phi);
8019 : : }
8020 : 32892162 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8021 : 133422866 : !gsi_end_p (gsi); gsi_next (&gsi))
8022 : : {
8023 : 116976785 : gimple *stmt = gsi_stmt (gsi);
8024 : 116976785 : gimple_set_uid (stmt, 0);
8025 : 116976785 : if (is_gimple_debug (stmt))
8026 : 71124132 : continue;
8027 : 45852653 : add_stmt (stmt);
8028 : : }
8029 : : }
8030 : 2394109 : }
8031 : :
8032 : :
8033 : : /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
8034 : : stmts in the basic block. */
8035 : :
8036 : 2394109 : _bb_vec_info::~_bb_vec_info ()
8037 : : {
8038 : : /* Reset region marker. */
8039 : 18840190 : for (unsigned i = 0; i < nbbs; ++i)
8040 : : {
8041 : 16446081 : if (i != 0)
8042 : 21862697 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8043 : 7810725 : gsi_next (&si))
8044 : : {
8045 : 7810725 : gphi *phi = si.phi ();
8046 : 7810725 : gimple_set_uid (phi, -1);
8047 : : }
8048 : 32892162 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8049 : 133348713 : !gsi_end_p (gsi); gsi_next (&gsi))
8050 : : {
8051 : 116902632 : gimple *stmt = gsi_stmt (gsi);
8052 : 116902632 : gimple_set_uid (stmt, -1);
8053 : : }
8054 : : }
8055 : :
8056 : 3604471 : for (unsigned i = 0; i < roots.length (); ++i)
8057 : : {
8058 : 1210362 : roots[i].stmts.release ();
8059 : 1210362 : roots[i].roots.release ();
8060 : 1210362 : roots[i].remain.release ();
8061 : : }
8062 : 2394109 : roots.release ();
8063 : 2394109 : }
8064 : :
8065 : : /* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
8066 : : given then that child nodes have already been processed, and that
8067 : : their def types currently match their SLP node's def type. */
8068 : :
8069 : : static bool
8070 : 2395259 : vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
8071 : : slp_instance node_instance,
8072 : : stmt_vector_for_cost *cost_vec)
8073 : : {
8074 : : /* Handle purely internal nodes. */
8075 : 2395259 : if (SLP_TREE_PERMUTE_P (node))
8076 : : {
8077 : 99004 : if (!vectorizable_slp_permutation (vinfo, NULL, node, cost_vec))
8078 : : return false;
8079 : :
8080 : : stmt_vec_info slp_stmt_info;
8081 : : unsigned int i;
8082 : 256000 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
8083 : : {
8084 : 158204 : if (slp_stmt_info
8085 : 153448 : && STMT_VINFO_LIVE_P (slp_stmt_info)
8086 : 158222 : && !vectorizable_live_operation (vinfo, slp_stmt_info, node,
8087 : : node_instance, i,
8088 : : false, cost_vec))
8089 : : return false;
8090 : : }
8091 : 97796 : SLP_TREE_TYPE (node) = permute_info_type;
8092 : 97796 : return true;
8093 : : }
8094 : :
8095 : 2296255 : return vect_analyze_stmt (vinfo, node, node_instance, cost_vec);
8096 : : }
8097 : :
8098 : : static int
8099 : 1853410 : sort_ints (const void *a_, const void *b_)
8100 : : {
8101 : 1853410 : int a = *(const int *)a_;
8102 : 1853410 : int b = *(const int *)b_;
8103 : 1853410 : return a - b;
8104 : : }
8105 : :
8106 : : /* Verify if we can externalize a set of internal defs. */
8107 : :
8108 : : static bool
8109 : 389362 : vect_slp_can_convert_to_external (const vec<stmt_vec_info> &stmts)
8110 : : {
8111 : : /* Constant generation uses get_later_stmt which can only handle
8112 : : defs from the same BB or a set of defs that can be ordered
8113 : : with a dominance query. */
8114 : 389362 : basic_block bb = NULL;
8115 : 389362 : bool all_same = true;
8116 : 389362 : auto_vec<int> bbs;
8117 : 778724 : bbs.reserve_exact (stmts.length ());
8118 : 2103904 : for (stmt_vec_info stmt : stmts)
8119 : : {
8120 : 935818 : if (!stmt)
8121 : : return false;
8122 : 935818 : else if (!bb)
8123 : 389362 : bb = gimple_bb (stmt->stmt);
8124 : 546456 : else if (gimple_bb (stmt->stmt) != bb)
8125 : 171846 : all_same = false;
8126 : 935818 : bbs.quick_push (gimple_bb (stmt->stmt)->index);
8127 : : }
8128 : 389362 : if (all_same)
8129 : : return true;
8130 : :
8131 : : /* Produce a vector of unique BB indexes for the defs. */
8132 : 128223 : bbs.qsort (sort_ints);
8133 : : unsigned i, j;
8134 : 313688 : for (i = 1, j = 1; i < bbs.length (); ++i)
8135 : 185465 : if (bbs[i] != bbs[j-1])
8136 : 136986 : bbs[j++] = bbs[i];
8137 : 128223 : gcc_assert (j >= 2);
8138 : 128223 : bbs.truncate (j);
8139 : :
8140 : 256446 : if (bbs.length () == 2)
8141 : 124759 : return (dominated_by_p (CDI_DOMINATORS,
8142 : 124759 : BASIC_BLOCK_FOR_FN (cfun, bbs[0]),
8143 : 124759 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]))
8144 : 242168 : || dominated_by_p (CDI_DOMINATORS,
8145 : 117409 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]),
8146 : 117409 : BASIC_BLOCK_FOR_FN (cfun, bbs[0])));
8147 : :
8148 : : /* ??? For more than two BBs we can sort the vector and verify the
8149 : : result is a total order. But we can't use vec::qsort with a
8150 : : compare function using a dominance query since there's no way to
8151 : : signal failure and any fallback for an unordered pair would
8152 : : fail qsort_chk later.
8153 : : For now simply hope that ordering after BB index provides the
8154 : : best candidate total order. If required we can implement our
8155 : : own mergesort or export an entry without checking. */
8156 : 404996 : for (unsigned i = 1; i < bbs.length (); ++i)
8157 : 12205 : if (!dominated_by_p (CDI_DOMINATORS,
8158 : 12205 : BASIC_BLOCK_FOR_FN (cfun, bbs[i]),
8159 : 12205 : BASIC_BLOCK_FOR_FN (cfun, bbs[i-1])))
8160 : : return false;
8161 : :
8162 : : return true;
8163 : 389362 : }
8164 : :
8165 : : /* Try to build NODE from scalars, returning true on success.
8166 : : NODE_INSTANCE is the SLP instance that contains NODE. */
8167 : :
8168 : : static bool
8169 : 529029 : vect_slp_convert_to_external (vec_info *vinfo, slp_tree node,
8170 : : slp_instance node_instance)
8171 : : {
8172 : 529029 : stmt_vec_info stmt_info;
8173 : 529029 : unsigned int i;
8174 : :
8175 : 529029 : if (!is_a <bb_vec_info> (vinfo)
8176 : 74616 : || node == SLP_INSTANCE_TREE (node_instance)
8177 : 22701 : || !SLP_TREE_SCALAR_STMTS (node).exists ()
8178 : 22657 : || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node))
8179 : : /* Force the mask use to be built from scalars instead. */
8180 : 20300 : || VECTOR_BOOLEAN_TYPE_P (SLP_TREE_VECTYPE (node))
8181 : 549119 : || !vect_slp_can_convert_to_external (SLP_TREE_SCALAR_STMTS (node)))
8182 : 508939 : return false;
8183 : :
8184 : 20090 : if (dump_enabled_p ())
8185 : 73 : dump_printf_loc (MSG_NOTE, vect_location,
8186 : : "Building vector operands of %p from scalars instead\n",
8187 : : (void *) node);
8188 : :
8189 : : /* Don't remove and free the child nodes here, since they could be
8190 : : referenced by other structures. The analysis and scheduling phases
8191 : : (need to) ignore child nodes of anything that isn't vect_internal_def. */
8192 : 20090 : unsigned int group_size = SLP_TREE_LANES (node);
8193 : 20090 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
8194 : : /* Invariants get their vector type from the uses. */
8195 : 20090 : SLP_TREE_VECTYPE (node) = NULL_TREE;
8196 : 20090 : SLP_TREE_SCALAR_OPS (node).safe_grow (group_size, true);
8197 : 20090 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8198 : 70552 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8199 : : {
8200 : 50462 : tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
8201 : 50462 : SLP_TREE_SCALAR_OPS (node)[i] = lhs;
8202 : : }
8203 : : return true;
8204 : : }
8205 : :
8206 : : /* Return true if all elements of the slice are the same. */
8207 : : bool
8208 : 460642 : vect_scalar_ops_slice::all_same_p () const
8209 : : {
8210 : 509663 : for (unsigned int i = 1; i < length; ++i)
8211 : 431690 : if (!operand_equal_p (op (0), op (i)))
8212 : : return false;
8213 : : return true;
8214 : : }
8215 : :
8216 : : hashval_t
8217 : 399221 : vect_scalar_ops_slice_hash::hash (const value_type &s)
8218 : : {
8219 : 399221 : hashval_t hash = 0;
8220 : 1534651 : for (unsigned i = 0; i < s.length; ++i)
8221 : 1135430 : hash = iterative_hash_expr (s.op (i), hash);
8222 : 399221 : return hash;
8223 : : }
8224 : :
8225 : : bool
8226 : 218400 : vect_scalar_ops_slice_hash::equal (const value_type &s1,
8227 : : const compare_type &s2)
8228 : : {
8229 : 218400 : if (s1.length != s2.length)
8230 : : return false;
8231 : 376660 : for (unsigned i = 0; i < s1.length; ++i)
8232 : 328949 : if (!operand_equal_p (s1.op (i), s2.op (i)))
8233 : : return false;
8234 : : return true;
8235 : : }
8236 : :
8237 : : /* Compute the prologue cost for invariant or constant operands represented
8238 : : by NODE. */
8239 : :
8240 : : static void
8241 : 1025718 : vect_prologue_cost_for_slp (vec_info *vinfo, slp_tree node,
8242 : : stmt_vector_for_cost *cost_vec)
8243 : : {
8244 : : /* There's a special case of an existing vector, that costs nothing. */
8245 : 1025718 : if (SLP_TREE_SCALAR_OPS (node).length () == 0
8246 : 1025718 : && !SLP_TREE_VEC_DEFS (node).is_empty ())
8247 : 1444 : return;
8248 : : /* Without looking at the actual initializer a vector of
8249 : : constants can be implemented as load from the constant pool.
8250 : : When all elements are the same we can use a splat. */
8251 : 1024274 : tree vectype = SLP_TREE_VECTYPE (node);
8252 : 1024274 : unsigned group_size = SLP_TREE_SCALAR_OPS (node).length ();
8253 : 1024274 : unsigned HOST_WIDE_INT const_nunits;
8254 : 1024274 : unsigned nelt_limit;
8255 : 1024274 : unsigned nvectors = vect_get_num_copies (vinfo, node);
8256 : 1024274 : auto ops = &SLP_TREE_SCALAR_OPS (node);
8257 : 1024274 : auto_vec<unsigned int> starts (nvectors);
8258 : 1024274 : if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
8259 : 1024274 : && ! multiple_p (const_nunits, group_size))
8260 : : {
8261 : 63560 : nelt_limit = const_nunits;
8262 : 63560 : hash_set<vect_scalar_ops_slice_hash> vector_ops;
8263 : 262847 : for (unsigned int i = 0; i < nvectors; ++i)
8264 : 199287 : if (!vector_ops.add ({ ops, i * nelt_limit, nelt_limit }))
8265 : 151576 : starts.quick_push (i * nelt_limit);
8266 : 63560 : }
8267 : : else
8268 : : {
8269 : : /* If either the vector has variable length or the vectors
8270 : : are composed of repeated whole groups we only need to
8271 : : cost construction once. All vectors will be the same. */
8272 : 960714 : nelt_limit = group_size;
8273 : 960714 : starts.quick_push (0);
8274 : : }
8275 : : /* ??? We're just tracking whether vectors in a single node are the same.
8276 : : Ideally we'd do something more global. */
8277 : 1024274 : bool passed = false;
8278 : 4185112 : for (unsigned int start : starts)
8279 : : {
8280 : 1112290 : vect_cost_for_stmt kind;
8281 : 1112290 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
8282 : : kind = vector_load;
8283 : 460642 : else if (vect_scalar_ops_slice { ops, start, nelt_limit }.all_same_p ())
8284 : : kind = scalar_to_vec;
8285 : : else
8286 : 382669 : kind = vec_construct;
8287 : : /* The target cost hook has no idea which part of the SLP node
8288 : : we are costing so avoid passing it down more than once. Pass
8289 : : it to the first vec_construct or scalar_to_vec part since for those
8290 : : the x86 backend tries to account for GPR to XMM register moves. */
8291 : 1112290 : record_stmt_cost (cost_vec, 1, kind, nullptr,
8292 : 1112290 : (kind != vector_load && !passed) ? node : nullptr,
8293 : : vectype, 0, vect_prologue);
8294 : 1112290 : if (kind != vector_load)
8295 : 460642 : passed = true;
8296 : : }
8297 : 1024274 : }
8298 : :
8299 : : /* Analyze statements contained in SLP tree NODE after recursively analyzing
8300 : : the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
8301 : :
8302 : : Return true if the operations are supported. */
8303 : :
8304 : : static bool
8305 : 4444557 : vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
8306 : : slp_instance node_instance,
8307 : : hash_set<slp_tree> &visited_set,
8308 : : vec<slp_tree> &visited_vec,
8309 : : stmt_vector_for_cost *cost_vec)
8310 : : {
8311 : 4444557 : int i, j;
8312 : 4444557 : slp_tree child;
8313 : :
8314 : : /* Assume we can code-generate all invariants. */
8315 : 4444557 : if (!node
8316 : 4156439 : || SLP_TREE_DEF_TYPE (node) == vect_constant_def
8317 : 3459556 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
8318 : : return true;
8319 : :
8320 : 2944661 : if (SLP_TREE_DEF_TYPE (node) == vect_uninitialized_def)
8321 : : {
8322 : 6 : if (dump_enabled_p ())
8323 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
8324 : : "Failed cyclic SLP reference in %p\n", (void *) node);
8325 : 6 : return false;
8326 : : }
8327 : 2944655 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_internal_def);
8328 : :
8329 : : /* If we already analyzed the exact same set of scalar stmts we're done.
8330 : : We share the generated vector stmts for those. */
8331 : 2944655 : if (visited_set.add (node))
8332 : : return true;
8333 : 2696561 : visited_vec.safe_push (node);
8334 : :
8335 : 2696561 : bool res = true;
8336 : 2696561 : unsigned visited_rec_start = visited_vec.length ();
8337 : 2696561 : unsigned cost_vec_rec_start = cost_vec->length ();
8338 : 2696561 : bool seen_non_constant_child = false;
8339 : 5637345 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8340 : : {
8341 : 3241939 : res = vect_slp_analyze_node_operations (vinfo, child, node_instance,
8342 : : visited_set, visited_vec,
8343 : : cost_vec);
8344 : 3241939 : if (!res)
8345 : : break;
8346 : 2940784 : if (child && SLP_TREE_DEF_TYPE (child) != vect_constant_def)
8347 : 2940784 : seen_non_constant_child = true;
8348 : : }
8349 : : /* We're having difficulties scheduling nodes with just constant
8350 : : operands and no scalar stmts since we then cannot compute a stmt
8351 : : insertion place. */
8352 : 2696561 : if (res
8353 : 2696561 : && !seen_non_constant_child
8354 : 2696561 : && SLP_TREE_SCALAR_STMTS (node).is_empty ())
8355 : : {
8356 : 147 : if (dump_enabled_p ())
8357 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
8358 : : "Cannot vectorize all-constant op node %p\n",
8359 : : (void *) node);
8360 : : res = false;
8361 : : }
8362 : :
8363 : 2696414 : if (res)
8364 : 2395259 : res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
8365 : : cost_vec);
8366 : : /* If analysis failed we have to pop all recursive visited nodes
8367 : : plus ourselves. */
8368 : 2696561 : if (!res)
8369 : : {
8370 : 2607320 : while (visited_vec.length () >= visited_rec_start)
8371 : 774631 : visited_set.remove (visited_vec.pop ());
8372 : 529029 : cost_vec->truncate (cost_vec_rec_start);
8373 : : }
8374 : :
8375 : : /* When the node can be vectorized cost invariant nodes it references.
8376 : : This is not done in DFS order to allow the refering node
8377 : : vectorizable_* calls to nail down the invariant nodes vector type
8378 : : and possibly unshare it if it needs a different vector type than
8379 : : other referrers. */
8380 : 2696561 : if (res)
8381 : 4817826 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
8382 : 2650294 : if (child
8383 : 2419310 : && (SLP_TREE_DEF_TYPE (child) == vect_constant_def
8384 : 2419310 : || SLP_TREE_DEF_TYPE (child) == vect_external_def)
8385 : : /* Perform usual caching, note code-generation still
8386 : : code-gens these nodes multiple times but we expect
8387 : : to CSE them later. */
8388 : 3737592 : && !visited_set.add (child))
8389 : : {
8390 : 1062826 : visited_vec.safe_push (child);
8391 : : /* ??? After auditing more code paths make a "default"
8392 : : and push the vector type from NODE to all children
8393 : : if it is not already set. */
8394 : : /* Compute the number of vectors to be generated. */
8395 : 1062826 : tree vector_type = SLP_TREE_VECTYPE (child);
8396 : 1062826 : if (!vector_type)
8397 : : {
8398 : : /* Masked loads can have an undefined (default SSA definition)
8399 : : else operand. We do not need to cost it. */
8400 : 37108 : vec<tree> ops = SLP_TREE_SCALAR_OPS (child);
8401 : 38117 : if (SLP_TREE_TYPE (node) == load_vec_info_type
8402 : 38117 : && ((ops.length ()
8403 : 1009 : && TREE_CODE (ops[0]) == SSA_NAME
8404 : 0 : && SSA_NAME_IS_DEFAULT_DEF (ops[0])
8405 : 0 : && VAR_P (SSA_NAME_VAR (ops[0])))
8406 : 1009 : || SLP_TREE_DEF_TYPE (child) == vect_constant_def))
8407 : 1009 : continue;
8408 : :
8409 : : /* For shifts with a scalar argument we don't need
8410 : : to cost or code-generate anything.
8411 : : ??? Represent this more explicitely. */
8412 : 36099 : gcc_assert (SLP_TREE_TYPE (node) == shift_vec_info_type
8413 : : && j == 1);
8414 : 36099 : continue;
8415 : 36099 : }
8416 : :
8417 : : /* And cost them. */
8418 : 1025718 : vect_prologue_cost_for_slp (vinfo, child, cost_vec);
8419 : : }
8420 : :
8421 : : /* If this node or any of its children can't be vectorized, try pruning
8422 : : the tree here rather than felling the whole thing. */
8423 : 529029 : if (!res && vect_slp_convert_to_external (vinfo, node, node_instance))
8424 : : {
8425 : : /* We'll need to revisit this for invariant costing and number
8426 : : of vectorized stmt setting. */
8427 : : res = true;
8428 : : }
8429 : :
8430 : : return res;
8431 : : }
8432 : :
8433 : : /* Given a definition DEF, analyze if it will have any live scalar use after
8434 : : performing SLP vectorization whose information is represented by BB_VINFO,
8435 : : and record result into hash map SCALAR_USE_MAP as cache for later fast
8436 : : check. If recursion DEPTH exceeds a limit, stop analysis and make a
8437 : : conservative assumption. Return 0 if no scalar use, 1 if there is, -1
8438 : : means recursion is limited. */
8439 : :
8440 : : static int
8441 : 558528 : vec_slp_has_scalar_use (bb_vec_info bb_vinfo, tree def,
8442 : : hash_map<tree, int> &scalar_use_map,
8443 : : int depth = 0)
8444 : : {
8445 : 558528 : const int depth_limit = 2;
8446 : 558528 : imm_use_iterator use_iter;
8447 : 558528 : gimple *use_stmt;
8448 : :
8449 : 558528 : if (int *res = scalar_use_map.get (def))
8450 : 22355 : return *res;
8451 : :
8452 : 536173 : int scalar_use = 1;
8453 : :
8454 : 1221650 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
8455 : : {
8456 : 796589 : if (is_gimple_debug (use_stmt))
8457 : 169914 : continue;
8458 : :
8459 : 626675 : stmt_vec_info use_stmt_info = bb_vinfo->lookup_stmt (use_stmt);
8460 : :
8461 : 626675 : if (!use_stmt_info)
8462 : : break;
8463 : :
8464 : 628992 : if (PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
8465 : 513629 : continue;
8466 : :
8467 : : /* Do not step forward when encounter PHI statement, since it may
8468 : : involve cyclic reference and cause infinite recursive invocation. */
8469 : 106897 : if (gimple_code (use_stmt) == GIMPLE_PHI)
8470 : : break;
8471 : :
8472 : : /* When pattern recognition is involved, a statement whose definition is
8473 : : consumed in some pattern, may not be included in the final replacement
8474 : : pattern statements, so would be skipped when building SLP graph.
8475 : :
8476 : : * Original
8477 : : char a_c = *(char *) a;
8478 : : char b_c = *(char *) b;
8479 : : unsigned short a_s = (unsigned short) a_c;
8480 : : int a_i = (int) a_s;
8481 : : int b_i = (int) b_c;
8482 : : int r_i = a_i - b_i;
8483 : :
8484 : : * After pattern replacement
8485 : : a_s = (unsigned short) a_c;
8486 : : a_i = (int) a_s;
8487 : :
8488 : : patt_b_s = (unsigned short) b_c; // b_i = (int) b_c
8489 : : patt_b_i = (int) patt_b_s; // b_i = (int) b_c
8490 : :
8491 : : patt_r_s = widen_minus(a_c, b_c); // r_i = a_i - b_i
8492 : : patt_r_i = (int) patt_r_s; // r_i = a_i - b_i
8493 : :
8494 : : The definitions of a_i(original statement) and b_i(pattern statement)
8495 : : are related to, but actually not part of widen_minus pattern.
8496 : : Vectorizing the pattern does not cause these definition statements to
8497 : : be marked as PURE_SLP. For this case, we need to recursively check
8498 : : whether their uses are all absorbed into vectorized code. But there
8499 : : is an exception that some use may participate in an vectorized
8500 : : operation via an external SLP node containing that use as an element.
8501 : : The parameter "scalar_use_map" tags such kind of SSA as having scalar
8502 : : use in advance. */
8503 : 88214 : tree lhs = gimple_get_lhs (use_stmt);
8504 : :
8505 : 88214 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
8506 : : break;
8507 : :
8508 : 55874 : if (depth_limit && depth >= depth_limit)
8509 : 10683 : return -1;
8510 : :
8511 : 45191 : if ((scalar_use = vec_slp_has_scalar_use (bb_vinfo, lhs, scalar_use_map,
8512 : : depth + 1)))
8513 : : break;
8514 : 536173 : }
8515 : :
8516 : 525490 : if (end_imm_use_stmt_p (&use_iter))
8517 : 425061 : scalar_use = 0;
8518 : :
8519 : : /* If recursion is limited, do not cache result for non-root defs. */
8520 : 525490 : if (!depth || scalar_use >= 0)
8521 : : {
8522 : 514807 : bool added = scalar_use_map.put (def, scalar_use);
8523 : 514807 : gcc_assert (!added);
8524 : : }
8525 : :
8526 : 525490 : return scalar_use;
8527 : : }
8528 : :
8529 : : /* Mark lanes of NODE that are live outside of the basic-block vectorized
8530 : : region and that can be vectorized using vectorizable_live_operation
8531 : : with STMT_VINFO_LIVE_P. Not handled live operations will cause the
8532 : : scalar code computing it to be retained. */
8533 : :
8534 : : static void
8535 : 906474 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo, slp_tree node,
8536 : : slp_instance instance,
8537 : : stmt_vector_for_cost *cost_vec,
8538 : : hash_map<tree, int> &scalar_use_map,
8539 : : hash_set<stmt_vec_info> &svisited,
8540 : : hash_set<slp_tree> &visited)
8541 : : {
8542 : 906474 : if (visited.add (node))
8543 : 34182 : return;
8544 : :
8545 : 872292 : unsigned i;
8546 : 872292 : stmt_vec_info stmt_info;
8547 : 872292 : stmt_vec_info last_stmt = vect_find_last_scalar_stmt_in_slp (node);
8548 : 3158776 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8549 : : {
8550 : 2286484 : if (!stmt_info || svisited.contains (stmt_info))
8551 : 29829 : continue;
8552 : 2265286 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
8553 : 2265286 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info)
8554 : 11575 : && STMT_VINFO_RELATED_STMT (orig_stmt_info) != stmt_info)
8555 : : /* Only the pattern root stmt computes the original scalar value. */
8556 : 8631 : continue;
8557 : 2256655 : bool mark_visited = true;
8558 : 2256655 : gimple *orig_stmt = orig_stmt_info->stmt;
8559 : 2256655 : ssa_op_iter op_iter;
8560 : 2256655 : def_operand_p def_p;
8561 : 5026647 : FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_stmt, op_iter, SSA_OP_DEF)
8562 : : {
8563 : 513337 : if (vec_slp_has_scalar_use (bb_vinfo, DEF_FROM_PTR (def_p),
8564 : : scalar_use_map))
8565 : : {
8566 : 90089 : STMT_VINFO_LIVE_P (stmt_info) = true;
8567 : 90089 : if (vectorizable_live_operation (bb_vinfo, stmt_info, node,
8568 : : instance, i, false, cost_vec))
8569 : : /* ??? So we know we can vectorize the live stmt from one SLP
8570 : : node. If we cannot do so from all or none consistently
8571 : : we'd have to record which SLP node (and lane) we want to
8572 : : use for the live operation. So make sure we can
8573 : : code-generate from all nodes. */
8574 : : mark_visited = false;
8575 : : else
8576 : 0 : STMT_VINFO_LIVE_P (stmt_info) = false;
8577 : : }
8578 : :
8579 : : /* We have to verify whether we can insert the lane extract
8580 : : before all uses. The following is a conservative approximation.
8581 : : We cannot put this into vectorizable_live_operation because
8582 : : iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
8583 : : doesn't work.
8584 : : Note that while the fact that we emit code for loads at the
8585 : : first load should make this a non-problem leafs we construct
8586 : : from scalars are vectorized after the last scalar def.
8587 : : ??? If we'd actually compute the insert location during
8588 : : analysis we could use sth less conservative than the last
8589 : : scalar stmt in the node for the dominance check. */
8590 : : /* ??? What remains is "live" uses in vector CTORs in the same
8591 : : SLP graph which is where those uses can end up code-generated
8592 : : right after their definition instead of close to their original
8593 : : use. But that would restrict us to code-generate lane-extracts
8594 : : from the latest stmt in a node. So we compensate for this
8595 : : during code-generation, simply not replacing uses for those
8596 : : hopefully rare cases. */
8597 : 513337 : imm_use_iterator use_iter;
8598 : 513337 : gimple *use_stmt;
8599 : 513337 : stmt_vec_info use_stmt_info;
8600 : :
8601 : 513337 : if (STMT_VINFO_LIVE_P (stmt_info))
8602 : 521088 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
8603 : 430999 : if (!is_gimple_debug (use_stmt)
8604 : 322454 : && (!(use_stmt_info = bb_vinfo->lookup_stmt (use_stmt))
8605 : 312229 : || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
8606 : 611319 : && !vect_stmt_dominates_stmt_p (last_stmt->stmt, use_stmt))
8607 : : {
8608 : 16182 : if (dump_enabled_p ())
8609 : 282 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8610 : : "Cannot determine insertion place for "
8611 : : "lane extract\n");
8612 : 16182 : STMT_VINFO_LIVE_P (stmt_info) = false;
8613 : 16182 : mark_visited = true;
8614 : 90089 : }
8615 : : }
8616 : 2256655 : if (mark_visited)
8617 : 2179716 : svisited.add (stmt_info);
8618 : : }
8619 : :
8620 : : slp_tree child;
8621 : 2515099 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8622 : 874313 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
8623 : 226527 : vect_bb_slp_mark_live_stmts (bb_vinfo, child, instance, cost_vec,
8624 : : scalar_use_map, svisited, visited);
8625 : : }
8626 : :
8627 : : /* Traverse all slp instances of BB_VINFO, and mark lanes of every node that
8628 : : are live outside of the basic-block vectorized region and that can be
8629 : : vectorized using vectorizable_live_operation with STMT_VINFO_LIVE_P. */
8630 : :
8631 : : static void
8632 : 281601 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo)
8633 : : {
8634 : 281601 : if (bb_vinfo->slp_instances.is_empty ())
8635 : 31476 : return;
8636 : :
8637 : 250125 : hash_set<stmt_vec_info> svisited;
8638 : 250125 : hash_set<slp_tree> visited;
8639 : 250125 : hash_map<tree, int> scalar_use_map;
8640 : 250125 : auto_vec<slp_tree> worklist;
8641 : :
8642 : 1430322 : for (slp_instance instance : bb_vinfo->slp_instances)
8643 : : {
8644 : 679947 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc)
8645 : 56776 : for (tree op : SLP_INSTANCE_REMAIN_DEFS (instance))
8646 : 16383 : if (TREE_CODE (op) == SSA_NAME)
8647 : 13845 : scalar_use_map.put (op, 1);
8648 : 679947 : if (!visited.add (SLP_INSTANCE_TREE (instance)))
8649 : 678051 : worklist.safe_push (SLP_INSTANCE_TREE (instance));
8650 : : }
8651 : :
8652 : 1518871 : do
8653 : : {
8654 : 1518871 : slp_tree node = worklist.pop ();
8655 : :
8656 : 1518871 : if (SLP_TREE_DEF_TYPE (node) == vect_external_def)
8657 : : {
8658 : 1589061 : for (tree op : SLP_TREE_SCALAR_OPS (node))
8659 : 703092 : if (TREE_CODE (op) == SSA_NAME)
8660 : 472516 : scalar_use_map.put (op, 1);
8661 : : }
8662 : : else
8663 : : {
8664 : 3634361 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8665 : 874289 : if (child && !visited.add (child))
8666 : 840820 : worklist.safe_push (child);
8667 : : }
8668 : : }
8669 : 3037742 : while (!worklist.is_empty ());
8670 : :
8671 : 250125 : visited.empty ();
8672 : :
8673 : 1430322 : for (slp_instance instance : bb_vinfo->slp_instances)
8674 : : {
8675 : 679947 : vect_location = instance->location ();
8676 : 679947 : vect_bb_slp_mark_live_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance),
8677 : : instance, &instance->cost_vec,
8678 : : scalar_use_map, svisited, visited);
8679 : : }
8680 : 250125 : }
8681 : :
8682 : : /* Determine whether we can vectorize the reduction epilogue for INSTANCE. */
8683 : :
8684 : : static bool
8685 : 74603 : vectorizable_bb_reduc_epilogue (slp_instance instance,
8686 : : stmt_vector_for_cost *cost_vec)
8687 : : {
8688 : 74603 : gassign *stmt = as_a <gassign *> (instance->root_stmts[0]->stmt);
8689 : 74603 : enum tree_code reduc_code = gimple_assign_rhs_code (stmt);
8690 : 74603 : if (reduc_code == MINUS_EXPR)
8691 : 0 : reduc_code = PLUS_EXPR;
8692 : 74603 : internal_fn reduc_fn;
8693 : 74603 : tree vectype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance));
8694 : 74603 : if (!vectype
8695 : 74591 : || !reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
8696 : 74591 : || reduc_fn == IFN_LAST
8697 : 74591 : || !direct_internal_fn_supported_p (reduc_fn, vectype, OPTIMIZE_FOR_BOTH)
8698 : 110717 : || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
8699 : 36114 : TREE_TYPE (vectype)))
8700 : : {
8701 : 50794 : if (dump_enabled_p ())
8702 : 275 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8703 : : "not vectorized: basic block reduction epilogue "
8704 : : "operation unsupported.\n");
8705 : 50794 : return false;
8706 : : }
8707 : :
8708 : : /* There's no way to cost a horizontal vector reduction via REDUC_FN so
8709 : : cost log2 vector operations plus shuffles and one extraction. */
8710 : 23809 : unsigned steps = floor_log2 (vect_nunits_for_cost (vectype));
8711 : 23809 : record_stmt_cost (cost_vec, steps, vector_stmt, instance->root_stmts[0],
8712 : : vectype, 0, vect_body);
8713 : 23809 : record_stmt_cost (cost_vec, steps, vec_perm, instance->root_stmts[0],
8714 : : vectype, 0, vect_body);
8715 : 23809 : record_stmt_cost (cost_vec, 1, vec_to_scalar, instance->root_stmts[0],
8716 : : vectype, 0, vect_body);
8717 : :
8718 : : /* Since we replace all stmts of a possibly longer scalar reduction
8719 : : chain account for the extra scalar stmts for that. */
8720 : 23809 : if (!instance->remain_defs.is_empty ())
8721 : 19102 : record_stmt_cost (cost_vec, instance->remain_defs.length (), scalar_stmt,
8722 : 9551 : instance->root_stmts[0], 0, vect_body);
8723 : : return true;
8724 : : }
8725 : :
8726 : : /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
8727 : : and recurse to children. */
8728 : :
8729 : : static void
8730 : 187017 : vect_slp_prune_covered_roots (slp_tree node, hash_set<stmt_vec_info> &roots,
8731 : : hash_set<slp_tree> &visited)
8732 : : {
8733 : 187017 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8734 : 187017 : || visited.add (node))
8735 : 82153 : return;
8736 : :
8737 : : stmt_vec_info stmt;
8738 : : unsigned i;
8739 : 348095 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
8740 : 243231 : if (stmt)
8741 : 246829 : roots.remove (vect_orig_stmt (stmt));
8742 : :
8743 : : slp_tree child;
8744 : 238289 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8745 : 133425 : if (child)
8746 : 126701 : vect_slp_prune_covered_roots (child, roots, visited);
8747 : : }
8748 : :
8749 : : /* Analyze statements in SLP instances of VINFO. Return true if the
8750 : : operations are supported. */
8751 : :
8752 : : bool
8753 : 602255 : vect_slp_analyze_operations (vec_info *vinfo)
8754 : : {
8755 : 602255 : slp_instance instance;
8756 : 602255 : int i;
8757 : :
8758 : 602255 : DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
8759 : :
8760 : 602255 : hash_set<slp_tree> visited;
8761 : 1594513 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
8762 : : {
8763 : 1202618 : auto_vec<slp_tree> visited_vec;
8764 : 1202618 : stmt_vector_for_cost cost_vec;
8765 : 1202618 : cost_vec.create (2);
8766 : 1202618 : if (is_a <bb_vec_info> (vinfo))
8767 : 784197 : vect_location = instance->location ();
8768 : 1202618 : if (!vect_slp_analyze_node_operations (vinfo,
8769 : : SLP_INSTANCE_TREE (instance),
8770 : : instance, visited, visited_vec,
8771 : : &cost_vec)
8772 : : /* CTOR instances require vectorized defs for the SLP tree root. */
8773 : 994828 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor
8774 : 4538 : && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance))
8775 : : != vect_internal_def
8776 : : /* Make sure we vectorized with the expected type. */
8777 : 4534 : || !useless_type_conversion_p
8778 : 4534 : (TREE_TYPE (TREE_TYPE (gimple_assign_rhs1
8779 : : (instance->root_stmts[0]->stmt))),
8780 : 4534 : TREE_TYPE (SLP_TREE_VECTYPE
8781 : : (SLP_INSTANCE_TREE (instance))))))
8782 : : /* Check we can vectorize the reduction. */
8783 : 994821 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
8784 : 74603 : && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
8785 : : /* Check we can vectorize the gcond. */
8786 : 2146645 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
8787 : 56668 : && !vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
8788 : 56668 : SLP_INSTANCE_ROOT_STMTS (instance)[0],
8789 : : NULL,
8790 : : SLP_INSTANCE_TREE (instance),
8791 : : &cost_vec)))
8792 : : {
8793 : 313076 : cost_vec.release ();
8794 : 313076 : slp_tree node = SLP_INSTANCE_TREE (instance);
8795 : 313076 : stmt_vec_info stmt_info;
8796 : 313076 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
8797 : 244595 : stmt_info = SLP_INSTANCE_ROOT_STMTS (instance)[0];
8798 : : else
8799 : 68481 : stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
8800 : 313076 : if (is_a <loop_vec_info> (vinfo))
8801 : : {
8802 : 210360 : if (dump_enabled_p ())
8803 : 6236 : dump_printf_loc (MSG_NOTE, vect_location,
8804 : : "unsupported SLP instance starting from: %G",
8805 : : stmt_info->stmt);
8806 : 210360 : return false;
8807 : : }
8808 : 102716 : if (dump_enabled_p ())
8809 : 328 : dump_printf_loc (MSG_NOTE, vect_location,
8810 : : "removing SLP instance operations starting from: %G",
8811 : : stmt_info->stmt);
8812 : 102716 : vect_free_slp_instance (instance);
8813 : 102716 : vinfo->slp_instances.ordered_remove (i);
8814 : 1546011 : while (!visited_vec.is_empty ())
8815 : 348368 : visited.remove (visited_vec.pop ());
8816 : : }
8817 : : else
8818 : : {
8819 : 889542 : i++;
8820 : 889542 : if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo))
8821 : : {
8822 : 208061 : add_stmt_costs (loop_vinfo->vector_costs, &cost_vec);
8823 : 208061 : cost_vec.release ();
8824 : : }
8825 : : else
8826 : : /* For BB vectorization remember the SLP graph entry
8827 : : cost for later. */
8828 : 681481 : instance->cost_vec = cost_vec;
8829 : : }
8830 : 1202618 : }
8831 : :
8832 : : /* Now look for SLP instances with a root that are covered by other
8833 : : instances and remove them. */
8834 : 391895 : hash_set<stmt_vec_info> roots;
8835 : 1619946 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
8836 : 866473 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
8837 : 30317 : roots.add (SLP_INSTANCE_ROOT_STMTS (instance)[0]);
8838 : 391895 : if (!roots.is_empty ())
8839 : : {
8840 : 12029 : visited.empty ();
8841 : 72345 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
8842 : 60316 : vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance), roots,
8843 : : visited);
8844 : 72345 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
8845 : 60316 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
8846 : 30317 : && !roots.contains (SLP_INSTANCE_ROOT_STMTS (instance)[0]))
8847 : : {
8848 : 1534 : stmt_vec_info root = SLP_INSTANCE_ROOT_STMTS (instance)[0];
8849 : 1534 : if (dump_enabled_p ())
8850 : 20 : dump_printf_loc (MSG_NOTE, vect_location,
8851 : : "removing SLP instance operations starting "
8852 : : "from: %G", root->stmt);
8853 : 1534 : vect_free_slp_instance (instance);
8854 : 1534 : vinfo->slp_instances.ordered_remove (i);
8855 : : }
8856 : : else
8857 : 58782 : ++i;
8858 : : }
8859 : :
8860 : : /* Compute vectorizable live stmts. */
8861 : 391895 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
8862 : 281601 : vect_bb_slp_mark_live_stmts (bb_vinfo);
8863 : :
8864 : 783790 : return !vinfo->slp_instances.is_empty ();
8865 : 994150 : }
8866 : :
8867 : : /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
8868 : : closing the eventual chain. */
8869 : :
8870 : : static slp_instance
8871 : 739146 : get_ultimate_leader (slp_instance instance,
8872 : : hash_map<slp_instance, slp_instance> &instance_leader)
8873 : : {
8874 : 739146 : auto_vec<slp_instance *, 8> chain;
8875 : 739146 : slp_instance *tem;
8876 : 809480 : while (*(tem = instance_leader.get (instance)) != instance)
8877 : : {
8878 : 70334 : chain.safe_push (tem);
8879 : 70334 : instance = *tem;
8880 : : }
8881 : 809480 : while (!chain.is_empty ())
8882 : 70334 : *chain.pop () = instance;
8883 : 739146 : return instance;
8884 : 739146 : }
8885 : :
8886 : : namespace {
8887 : : /* Subroutine of vect_bb_partition_graph_r. Map KEY to INSTANCE in
8888 : : KEY_TO_INSTANCE, making INSTANCE the leader of any previous mapping
8889 : : for KEY. Return true if KEY was already in KEY_TO_INSTANCE.
8890 : :
8891 : : INSTANCE_LEADER is as for get_ultimate_leader. */
8892 : :
8893 : : template<typename T>
8894 : : bool
8895 : 3276636 : vect_map_to_instance (slp_instance instance, T key,
8896 : : hash_map<T, slp_instance> &key_to_instance,
8897 : : hash_map<slp_instance, slp_instance> &instance_leader)
8898 : : {
8899 : : bool existed_p;
8900 : 3276636 : slp_instance &key_instance = key_to_instance.get_or_insert (key, &existed_p);
8901 : 3276636 : if (!existed_p)
8902 : : ;
8903 : 148179 : else if (key_instance != instance)
8904 : : {
8905 : : /* If we're running into a previously marked key make us the
8906 : : leader of the current ultimate leader. This keeps the
8907 : : leader chain acyclic and works even when the current instance
8908 : : connects two previously independent graph parts. */
8909 : 59199 : slp_instance key_leader
8910 : 59199 : = get_ultimate_leader (key_instance, instance_leader);
8911 : 59199 : if (key_leader != instance)
8912 : 17717 : instance_leader.put (key_leader, instance);
8913 : : }
8914 : 3276636 : key_instance = instance;
8915 : 3276636 : return existed_p;
8916 : : }
8917 : : }
8918 : :
8919 : : /* Worker of vect_bb_partition_graph, recurse on NODE. */
8920 : :
8921 : : static void
8922 : 906474 : vect_bb_partition_graph_r (bb_vec_info bb_vinfo,
8923 : : slp_instance instance, slp_tree node,
8924 : : hash_map<stmt_vec_info, slp_instance> &stmt_to_instance,
8925 : : hash_map<slp_tree, slp_instance> &node_to_instance,
8926 : : hash_map<slp_instance, slp_instance> &instance_leader)
8927 : : {
8928 : 906474 : stmt_vec_info stmt_info;
8929 : 906474 : unsigned i;
8930 : :
8931 : 3276636 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8932 : 2370162 : if (stmt_info)
8933 : 2370162 : vect_map_to_instance (instance, stmt_info, stmt_to_instance,
8934 : : instance_leader);
8935 : :
8936 : 906474 : if (vect_map_to_instance (instance, node, node_to_instance,
8937 : : instance_leader))
8938 : 906474 : return;
8939 : :
8940 : : slp_tree child;
8941 : 1746605 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8942 : 874313 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
8943 : 226527 : vect_bb_partition_graph_r (bb_vinfo, instance, child, stmt_to_instance,
8944 : : node_to_instance, instance_leader);
8945 : : }
8946 : :
8947 : : /* Partition the SLP graph into pieces that can be costed independently. */
8948 : :
8949 : : static void
8950 : 250125 : vect_bb_partition_graph (bb_vec_info bb_vinfo)
8951 : : {
8952 : 250125 : DUMP_VECT_SCOPE ("vect_bb_partition_graph");
8953 : :
8954 : : /* First walk the SLP graph assigning each involved scalar stmt a
8955 : : corresponding SLP graph entry and upon visiting a previously
8956 : : marked stmt, make the stmts leader the current SLP graph entry. */
8957 : 250125 : hash_map<stmt_vec_info, slp_instance> stmt_to_instance;
8958 : 250125 : hash_map<slp_tree, slp_instance> node_to_instance;
8959 : 250125 : hash_map<slp_instance, slp_instance> instance_leader;
8960 : 250125 : slp_instance instance;
8961 : 930072 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
8962 : : {
8963 : 679947 : instance_leader.put (instance, instance);
8964 : 679947 : vect_bb_partition_graph_r (bb_vinfo,
8965 : : instance, SLP_INSTANCE_TREE (instance),
8966 : : stmt_to_instance, node_to_instance,
8967 : : instance_leader);
8968 : : }
8969 : :
8970 : : /* Then collect entries to each independent subgraph. */
8971 : 1180197 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
8972 : : {
8973 : 679947 : slp_instance leader = get_ultimate_leader (instance, instance_leader);
8974 : 679947 : leader->subgraph_entries.safe_push (instance);
8975 : 679947 : if (dump_enabled_p ()
8976 : 679947 : && leader != instance)
8977 : 67 : dump_printf_loc (MSG_NOTE, vect_location,
8978 : : "instance %p is leader of %p\n",
8979 : : (void *) leader, (void *) instance);
8980 : : }
8981 : 250125 : }
8982 : :
8983 : : /* Compute the set of scalar stmts participating in internal and external
8984 : : nodes. */
8985 : :
8986 : : static void
8987 : 1541445 : vect_slp_gather_vectorized_scalar_stmts (vec_info *vinfo, slp_tree node,
8988 : : hash_set<slp_tree> &visited,
8989 : : hash_set<stmt_vec_info> &vstmts,
8990 : : hash_set<stmt_vec_info> &estmts)
8991 : : {
8992 : 1541445 : int i;
8993 : 1541445 : stmt_vec_info stmt_info;
8994 : 1541445 : slp_tree child;
8995 : :
8996 : 1541445 : if (visited.add (node))
8997 : 34134 : return;
8998 : :
8999 : 1507311 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
9000 : : {
9001 : 3101525 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9002 : 2237730 : if (stmt_info)
9003 : 2237730 : vstmts.add (stmt_info);
9004 : :
9005 : 3133883 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9006 : 864786 : if (child)
9007 : 864786 : vect_slp_gather_vectorized_scalar_stmts (vinfo, child, visited,
9008 : : vstmts, estmts);
9009 : : }
9010 : : else
9011 : 3614792 : for (tree def : SLP_TREE_SCALAR_OPS (node))
9012 : : {
9013 : 1685220 : stmt_vec_info def_stmt = vinfo->lookup_def (def);
9014 : 1685220 : if (def_stmt)
9015 : 333400 : estmts.add (def_stmt);
9016 : : }
9017 : : }
9018 : :
9019 : :
9020 : : /* Compute the scalar cost of the SLP node NODE and its children
9021 : : and return it. Do not account defs that are marked in LIFE and
9022 : : update LIFE according to uses of NODE. */
9023 : :
9024 : : static void
9025 : 897185 : vect_bb_slp_scalar_cost (vec_info *vinfo,
9026 : : slp_tree node, vec<bool, va_heap> *life,
9027 : : stmt_vector_for_cost *cost_vec,
9028 : : hash_set<stmt_vec_info> &vectorized_scalar_stmts,
9029 : : hash_set<stmt_vec_info> &scalar_stmts_in_externs,
9030 : : hash_set<slp_tree> &visited)
9031 : : {
9032 : 897185 : unsigned i;
9033 : 897185 : stmt_vec_info stmt_info;
9034 : 897185 : slp_tree child;
9035 : :
9036 : 897185 : if (visited.add (node))
9037 : 33373 : return;
9038 : :
9039 : 3101576 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9040 : : {
9041 : 2237764 : ssa_op_iter op_iter;
9042 : 2237764 : def_operand_p def_p;
9043 : :
9044 : 2268798 : if (!stmt_info
9045 : 2237764 : || (*life)[i]
9046 : : /* Defs also used in external nodes are not in the
9047 : : vectorized_scalar_stmts set as they need to be preserved.
9048 : : Honor that. */
9049 : 4447722 : || scalar_stmts_in_externs.contains (stmt_info))
9050 : 105867 : continue;
9051 : :
9052 : 2206730 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
9053 : 2206730 : gimple *orig_stmt = orig_stmt_info->stmt;
9054 : :
9055 : : /* If there is a non-vectorized use of the defs then the scalar
9056 : : stmt is kept live in which case we do not account it or any
9057 : : required defs in the SLP children in the scalar cost. This
9058 : : way we make the vectorization more costly when compared to
9059 : : the scalar cost. */
9060 : 2206730 : if (!STMT_VINFO_LIVE_P (stmt_info))
9061 : : {
9062 : 2139541 : auto_vec<gimple *, 8> worklist;
9063 : 2139541 : hash_set<gimple *> *worklist_visited = NULL;
9064 : 2139541 : worklist.quick_push (orig_stmt);
9065 : 2144569 : do
9066 : : {
9067 : 2144569 : gimple *work_stmt = worklist.pop ();
9068 : 4693327 : FOR_EACH_PHI_OR_STMT_DEF (def_p, work_stmt, op_iter, SSA_OP_DEF)
9069 : : {
9070 : 422773 : imm_use_iterator use_iter;
9071 : 422773 : gimple *use_stmt;
9072 : 1041567 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter,
9073 : : DEF_FROM_PTR (def_p))
9074 : 637378 : if (!is_gimple_debug (use_stmt))
9075 : : {
9076 : 493024 : stmt_vec_info use_stmt_info
9077 : 493024 : = vinfo->lookup_stmt (use_stmt);
9078 : 493024 : if (!use_stmt_info
9079 : 493024 : || !vectorized_scalar_stmts.contains (use_stmt_info))
9080 : : {
9081 : 23701 : if (use_stmt_info
9082 : 21299 : && STMT_VINFO_IN_PATTERN_P (use_stmt_info))
9083 : : {
9084 : : /* For stmts participating in patterns we have
9085 : : to check its uses recursively. */
9086 : 5117 : if (!worklist_visited)
9087 : 3968 : worklist_visited = new hash_set<gimple *> ();
9088 : 5117 : if (!worklist_visited->add (use_stmt))
9089 : 5117 : worklist.safe_push (use_stmt);
9090 : 5117 : continue;
9091 : : }
9092 : 18584 : (*life)[i] = true;
9093 : 18584 : goto next_lane;
9094 : : }
9095 : 422773 : }
9096 : : }
9097 : : }
9098 : 4251970 : while (!worklist.is_empty ());
9099 : 2120957 : next_lane:
9100 : 2139541 : if (worklist_visited)
9101 : 3968 : delete worklist_visited;
9102 : 2139541 : if ((*life)[i])
9103 : 18584 : continue;
9104 : 2139541 : }
9105 : :
9106 : : /* Count scalar stmts only once. */
9107 : 2188146 : if (gimple_visited_p (orig_stmt))
9108 : 25317 : continue;
9109 : 2162829 : gimple_set_visited (orig_stmt, true);
9110 : :
9111 : 2162829 : vect_cost_for_stmt kind;
9112 : 2162829 : if (STMT_VINFO_DATA_REF (orig_stmt_info))
9113 : : {
9114 : 1959070 : data_reference_p dr = STMT_VINFO_DATA_REF (orig_stmt_info);
9115 : 1959070 : tree base = get_base_address (DR_REF (dr));
9116 : : /* When the scalar access is to a non-global not address-taken
9117 : : decl that is not BLKmode assume we can access it with a single
9118 : : non-load/store instruction. */
9119 : 1959070 : if (DECL_P (base)
9120 : 1527433 : && !is_global_var (base)
9121 : 1453237 : && !TREE_ADDRESSABLE (base)
9122 : 2513189 : && DECL_MODE (base) != BLKmode)
9123 : : kind = scalar_stmt;
9124 : 1813871 : else if (DR_IS_READ (STMT_VINFO_DATA_REF (orig_stmt_info)))
9125 : : kind = scalar_load;
9126 : : else
9127 : 1592352 : kind = scalar_store;
9128 : : }
9129 : 203759 : else if (vect_nop_conversion_p (orig_stmt_info))
9130 : 20384 : continue;
9131 : : /* For single-argument PHIs assume coalescing which means zero cost
9132 : : for the scalar and the vector PHIs. This avoids artificially
9133 : : favoring the vector path (but may pessimize it in some cases). */
9134 : 183375 : else if (is_a <gphi *> (orig_stmt_info->stmt)
9135 : 183375 : && gimple_phi_num_args
9136 : 89032 : (as_a <gphi *> (orig_stmt_info->stmt)) == 1)
9137 : 10548 : continue;
9138 : : else
9139 : : kind = scalar_stmt;
9140 : 2131897 : record_stmt_cost (cost_vec, 1, kind, orig_stmt_info,
9141 : : SLP_TREE_VECTYPE (node), 0, vect_body);
9142 : : }
9143 : :
9144 : 1727624 : auto_vec<bool, 20> subtree_life;
9145 : 2490417 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9146 : : {
9147 : 864810 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9148 : : {
9149 : : /* Do not directly pass LIFE to the recursive call, copy it to
9150 : : confine changes in the callee to the current child/subtree. */
9151 : 220526 : if (SLP_TREE_PERMUTE_P (node))
9152 : : {
9153 : 3547 : subtree_life.safe_grow_cleared (SLP_TREE_LANES (child), true);
9154 : 12435 : for (unsigned j = 0;
9155 : 12435 : j < SLP_TREE_LANE_PERMUTATION (node).length (); ++j)
9156 : : {
9157 : 8888 : auto perm = SLP_TREE_LANE_PERMUTATION (node)[j];
9158 : 8888 : if (perm.first == i)
9159 : 4740 : subtree_life[perm.second] = (*life)[j];
9160 : : }
9161 : : }
9162 : : else
9163 : : {
9164 : 216979 : gcc_assert (SLP_TREE_LANES (node) == SLP_TREE_LANES (child));
9165 : 216979 : subtree_life.safe_splice (*life);
9166 : : }
9167 : 220526 : vect_bb_slp_scalar_cost (vinfo, child, &subtree_life, cost_vec,
9168 : : vectorized_scalar_stmts,
9169 : : scalar_stmts_in_externs, visited);
9170 : 220526 : subtree_life.truncate (0);
9171 : : }
9172 : : }
9173 : : }
9174 : :
9175 : : /* Comparator for the loop-index sorted cost vectors. */
9176 : :
9177 : : static int
9178 : 17523256 : li_cost_vec_cmp (const void *a_, const void *b_)
9179 : : {
9180 : 17523256 : auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_;
9181 : 17523256 : auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_;
9182 : 17523256 : if (a->first < b->first)
9183 : : return -1;
9184 : 16800949 : else if (a->first == b->first)
9185 : 16162772 : return 0;
9186 : : return 1;
9187 : : }
9188 : :
9189 : : /* Check if vectorization of the basic block is profitable for the
9190 : : subgraph denoted by SLP_INSTANCES. */
9191 : :
9192 : : static bool
9193 : 659070 : vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo,
9194 : : vec<slp_instance> slp_instances,
9195 : : loop_p orig_loop)
9196 : : {
9197 : 659070 : slp_instance instance;
9198 : 659070 : int i;
9199 : 659070 : unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
9200 : 659070 : unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
9201 : :
9202 : 659070 : if (dump_enabled_p ())
9203 : : {
9204 : 100 : dump_printf_loc (MSG_NOTE, vect_location, "Costing subgraph: \n");
9205 : 100 : hash_set<slp_tree> visited;
9206 : 403 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9207 : 103 : vect_print_slp_graph (MSG_NOTE, vect_location,
9208 : : SLP_INSTANCE_TREE (instance), visited);
9209 : 100 : }
9210 : :
9211 : : /* Compute the set of scalar stmts we know will go away 'locally' when
9212 : : vectorizing. This used to be tracked with just PURE_SLP_STMT but that's
9213 : : not accurate for nodes promoted extern late or for scalar stmts that
9214 : : are used both in extern defs and in vectorized defs. */
9215 : 659070 : hash_set<stmt_vec_info> vectorized_scalar_stmts;
9216 : 659070 : hash_set<stmt_vec_info> scalar_stmts_in_externs;
9217 : 659070 : hash_set<slp_tree> visited;
9218 : 1335729 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9219 : : {
9220 : 676659 : vect_slp_gather_vectorized_scalar_stmts (bb_vinfo,
9221 : : SLP_INSTANCE_TREE (instance),
9222 : : visited,
9223 : : vectorized_scalar_stmts,
9224 : : scalar_stmts_in_externs);
9225 : 779131 : for (stmt_vec_info rstmt : SLP_INSTANCE_ROOT_STMTS (instance))
9226 : 49342 : vectorized_scalar_stmts.add (rstmt);
9227 : : }
9228 : : /* Scalar stmts used as defs in external nodes need to be preseved, so
9229 : : remove them from vectorized_scalar_stmts. */
9230 : 957125 : for (stmt_vec_info stmt : scalar_stmts_in_externs)
9231 : 298055 : vectorized_scalar_stmts.remove (stmt);
9232 : :
9233 : : /* Calculate scalar cost and sum the cost for the vector stmts
9234 : : previously collected. */
9235 : 659070 : stmt_vector_for_cost scalar_costs = vNULL;
9236 : 659070 : stmt_vector_for_cost vector_costs = vNULL;
9237 : 659070 : visited.empty ();
9238 : 1335729 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9239 : : {
9240 : 676659 : auto_vec<bool, 20> life;
9241 : 676659 : life.safe_grow_cleared (SLP_TREE_LANES (SLP_INSTANCE_TREE (instance)),
9242 : : true);
9243 : 676659 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9244 : 53130 : record_stmt_cost (&scalar_costs,
9245 : 26565 : SLP_INSTANCE_ROOT_STMTS (instance).length (),
9246 : : scalar_stmt,
9247 : 26565 : SLP_INSTANCE_ROOT_STMTS (instance)[0], 0, vect_body);
9248 : 676659 : vect_bb_slp_scalar_cost (bb_vinfo,
9249 : : SLP_INSTANCE_TREE (instance),
9250 : : &life, &scalar_costs, vectorized_scalar_stmts,
9251 : : scalar_stmts_in_externs, visited);
9252 : 676659 : vector_costs.safe_splice (instance->cost_vec);
9253 : 676659 : instance->cost_vec.release ();
9254 : 676659 : }
9255 : :
9256 : 659070 : if (dump_enabled_p ())
9257 : 100 : dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
9258 : :
9259 : : /* When costing non-loop vectorization we need to consider each covered
9260 : : loop independently and make sure vectorization is profitable. For
9261 : : now we assume a loop may be not entered or executed an arbitrary
9262 : : number of iterations (??? static information can provide more
9263 : : precise info here) which means we can simply cost each containing
9264 : : loops stmts separately. */
9265 : :
9266 : : /* First produce cost vectors sorted by loop index. */
9267 : 659070 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9268 : 659070 : li_scalar_costs (scalar_costs.length ());
9269 : 659070 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9270 : 659070 : li_vector_costs (vector_costs.length ());
9271 : 659070 : stmt_info_for_cost *cost;
9272 : 2817532 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9273 : : {
9274 : 2158462 : unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9275 : 2158462 : li_scalar_costs.quick_push (std::make_pair (l, cost));
9276 : : }
9277 : : /* Use a random used loop as fallback in case the first vector_costs
9278 : : entry does not have a stmt_info associated with it. */
9279 : 659070 : unsigned l = li_scalar_costs[0].first;
9280 : 2419656 : FOR_EACH_VEC_ELT (vector_costs, i, cost)
9281 : : {
9282 : : /* We inherit from the previous COST, invariants, externals and
9283 : : extracts immediately follow the cost for the related stmt. */
9284 : 1760586 : if (cost->stmt_info)
9285 : 1041185 : l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9286 : 1760586 : li_vector_costs.quick_push (std::make_pair (l, cost));
9287 : : }
9288 : 659070 : li_scalar_costs.qsort (li_cost_vec_cmp);
9289 : 659070 : li_vector_costs.qsort (li_cost_vec_cmp);
9290 : :
9291 : : /* Now cost the portions individually. */
9292 : : unsigned vi = 0;
9293 : : unsigned si = 0;
9294 : 1143037 : bool profitable = true;
9295 : 1143037 : while (si < li_scalar_costs.length ()
9296 : 1806502 : && vi < li_vector_costs.length ())
9297 : : {
9298 : 663465 : unsigned sl = li_scalar_costs[si].first;
9299 : 663465 : unsigned vl = li_vector_costs[vi].first;
9300 : 663465 : if (sl != vl)
9301 : : {
9302 : 1174 : if (dump_enabled_p ())
9303 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9304 : : "Scalar %d and vector %d loop part do not "
9305 : : "match up, skipping scalar part\n", sl, vl);
9306 : : /* Skip the scalar part, assuming zero cost on the vector side. */
9307 : 2382 : do
9308 : : {
9309 : 2382 : si++;
9310 : : }
9311 : 2382 : while (si < li_scalar_costs.length ()
9312 : 4414 : && li_scalar_costs[si].first == sl);
9313 : 1174 : continue;
9314 : : }
9315 : :
9316 : 662291 : class vector_costs *scalar_target_cost_data = init_cost (bb_vinfo, true);
9317 : 2139724 : do
9318 : : {
9319 : 2139724 : add_stmt_cost (scalar_target_cost_data, li_scalar_costs[si].second);
9320 : 2139724 : si++;
9321 : : }
9322 : 2139724 : while (si < li_scalar_costs.length ()
9323 : 4286450 : && li_scalar_costs[si].first == sl);
9324 : 662291 : scalar_target_cost_data->finish_cost (nullptr);
9325 : 662291 : scalar_cost = scalar_target_cost_data->body_cost ();
9326 : :
9327 : : /* Complete the target-specific vector cost calculation. */
9328 : 662291 : class vector_costs *vect_target_cost_data = init_cost (bb_vinfo, false);
9329 : 1728818 : do
9330 : : {
9331 : 1728818 : add_stmt_cost (vect_target_cost_data, li_vector_costs[vi].second);
9332 : 1728818 : vi++;
9333 : : }
9334 : 1728818 : while (vi < li_vector_costs.length ()
9335 : 3465578 : && li_vector_costs[vi].first == vl);
9336 : 662291 : vect_target_cost_data->finish_cost (scalar_target_cost_data);
9337 : 662291 : vec_prologue_cost = vect_target_cost_data->prologue_cost ();
9338 : 662291 : vec_inside_cost = vect_target_cost_data->body_cost ();
9339 : 662291 : vec_epilogue_cost = vect_target_cost_data->epilogue_cost ();
9340 : 662291 : delete scalar_target_cost_data;
9341 : 662291 : delete vect_target_cost_data;
9342 : :
9343 : 662291 : vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
9344 : :
9345 : 662291 : if (dump_enabled_p ())
9346 : : {
9347 : 100 : dump_printf_loc (MSG_NOTE, vect_location,
9348 : : "Cost model analysis for part in loop %d:\n", sl);
9349 : 100 : dump_printf (MSG_NOTE, " Vector cost: %d\n",
9350 : : vec_inside_cost + vec_outside_cost);
9351 : 100 : dump_printf (MSG_NOTE, " Scalar cost: %d\n", scalar_cost);
9352 : : }
9353 : :
9354 : : /* Vectorization is profitable if its cost is more than the cost of scalar
9355 : : version. Note that we err on the vector side for equal cost because
9356 : : the cost estimate is otherwise quite pessimistic (constant uses are
9357 : : free on the scalar side but cost a load on the vector side for
9358 : : example). */
9359 : 662291 : if (vec_outside_cost + vec_inside_cost > scalar_cost)
9360 : : {
9361 : : profitable = false;
9362 : : break;
9363 : : }
9364 : : }
9365 : 1138629 : if (profitable && vi < li_vector_costs.length ())
9366 : : {
9367 : 1004 : if (dump_enabled_p ())
9368 : 12 : dump_printf_loc (MSG_NOTE, vect_location,
9369 : : "Excess vector cost for part in loop %d:\n",
9370 : 6 : li_vector_costs[vi].first);
9371 : : profitable = false;
9372 : : }
9373 : :
9374 : : /* Unset visited flag. This is delayed when the subgraph is profitable
9375 : : and we process the loop for remaining unvectorized if-converted code. */
9376 : 659070 : if (!orig_loop || !profitable)
9377 : 2816233 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9378 : 2157256 : gimple_set_visited (cost->stmt_info->stmt, false);
9379 : :
9380 : 659070 : scalar_costs.release ();
9381 : 659070 : vector_costs.release ();
9382 : :
9383 : 659070 : return profitable;
9384 : 659070 : }
9385 : :
9386 : : /* qsort comparator for lane defs. */
9387 : :
9388 : : static int
9389 : 40 : vld_cmp (const void *a_, const void *b_)
9390 : : {
9391 : 40 : auto *a = (const std::pair<unsigned, tree> *)a_;
9392 : 40 : auto *b = (const std::pair<unsigned, tree> *)b_;
9393 : 40 : return a->first - b->first;
9394 : : }
9395 : :
9396 : : /* Return true if USE_STMT is a vector lane insert into VEC and set
9397 : : *THIS_LANE to the lane number that is set. */
9398 : :
9399 : : static bool
9400 : 244 : vect_slp_is_lane_insert (gimple *use_stmt, tree vec, unsigned *this_lane)
9401 : : {
9402 : 244 : gassign *use_ass = dyn_cast <gassign *> (use_stmt);
9403 : 91 : if (!use_ass
9404 : 91 : || gimple_assign_rhs_code (use_ass) != BIT_INSERT_EXPR
9405 : 22 : || (vec
9406 : 22 : ? gimple_assign_rhs1 (use_ass) != vec
9407 : 24 : : ((vec = gimple_assign_rhs1 (use_ass)), false))
9408 : 46 : || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec)),
9409 : 46 : TREE_TYPE (gimple_assign_rhs2 (use_ass)))
9410 : 46 : || !constant_multiple_p
9411 : 46 : (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass)),
9412 : 92 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec)))),
9413 : : this_lane))
9414 : 198 : return false;
9415 : : return true;
9416 : : }
9417 : :
9418 : : /* Find any vectorizable constructors and add them to the grouped_store
9419 : : array. */
9420 : :
9421 : : static void
9422 : 2394109 : vect_slp_check_for_roots (bb_vec_info bb_vinfo)
9423 : : {
9424 : 18840190 : for (unsigned i = 0; i < bb_vinfo->nbbs; ++i)
9425 : 32892162 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[i]);
9426 : 133422866 : !gsi_end_p (gsi); gsi_next (&gsi))
9427 : : {
9428 : 116976785 : gassign *assign = dyn_cast<gassign *> (gsi_stmt (gsi));
9429 : : /* This can be used to start SLP discovery for early breaks for BB early breaks
9430 : : when we get that far. */
9431 : 116976785 : if (!assign)
9432 : 173188765 : continue;
9433 : :
9434 : 31315705 : tree rhs = gimple_assign_rhs1 (assign);
9435 : 31315705 : enum tree_code code = gimple_assign_rhs_code (assign);
9436 : 31315705 : use_operand_p use_p;
9437 : 31315705 : gimple *use_stmt;
9438 : 31315705 : if (code == CONSTRUCTOR)
9439 : : {
9440 : 1606298 : if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
9441 : 58947 : || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)),
9442 : 87242 : CONSTRUCTOR_NELTS (rhs))
9443 : 40487 : || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
9444 : 1646784 : || uniform_vector_p (rhs))
9445 : 1594642 : continue;
9446 : :
9447 : : unsigned j;
9448 : : tree val;
9449 : 57892 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9450 : 46236 : if (TREE_CODE (val) != SSA_NAME
9451 : 46236 : || !bb_vinfo->lookup_def (val))
9452 : : break;
9453 : 28006 : if (j != CONSTRUCTOR_NELTS (rhs))
9454 : 2347 : continue;
9455 : :
9456 : 11656 : vec<stmt_vec_info> roots = vNULL;
9457 : 11656 : roots.safe_push (bb_vinfo->lookup_stmt (assign));
9458 : 11656 : vec<stmt_vec_info> stmts;
9459 : 11656 : stmts.create (CONSTRUCTOR_NELTS (rhs));
9460 : 65776 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9461 : 42464 : stmts.quick_push
9462 : 42464 : (vect_stmt_to_vectorize (bb_vinfo->lookup_def (val)));
9463 : 11656 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
9464 : 11656 : stmts, roots));
9465 : : }
9466 : 29709407 : else if (code == BIT_INSERT_EXPR
9467 : 858 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
9468 : 559 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).is_constant ()
9469 : 559 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).to_constant () > 1
9470 : 556 : && integer_zerop (gimple_assign_rhs3 (assign))
9471 : 312 : && useless_type_conversion_p
9472 : 312 : (TREE_TYPE (TREE_TYPE (rhs)),
9473 : 312 : TREE_TYPE (gimple_assign_rhs2 (assign)))
9474 : 29709971 : && bb_vinfo->lookup_def (gimple_assign_rhs2 (assign)))
9475 : : {
9476 : : /* We start to match on insert to lane zero but since the
9477 : : inserts need not be ordered we'd have to search both
9478 : : the def and the use chains. */
9479 : 211 : tree vectype = TREE_TYPE (rhs);
9480 : 211 : unsigned nlanes = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
9481 : 211 : auto_vec<std::pair<unsigned, tree> > lane_defs (nlanes);
9482 : 211 : auto_sbitmap lanes (nlanes);
9483 : 211 : bitmap_clear (lanes);
9484 : 211 : bitmap_set_bit (lanes, 0);
9485 : 211 : tree def = gimple_assign_lhs (assign);
9486 : 211 : lane_defs.quick_push
9487 : 211 : (std::make_pair (0, gimple_assign_rhs2 (assign)));
9488 : 211 : unsigned lanes_found = 1;
9489 : : /* Start with the use chains, the last stmt will be the root. */
9490 : 211 : stmt_vec_info last = bb_vinfo->lookup_stmt (assign);
9491 : 211 : vec<stmt_vec_info> roots = vNULL;
9492 : 211 : roots.safe_push (last);
9493 : 213 : do
9494 : : {
9495 : 213 : use_operand_p use_p;
9496 : 213 : gimple *use_stmt;
9497 : 213 : if (!single_imm_use (def, &use_p, &use_stmt))
9498 : : break;
9499 : 207 : unsigned this_lane;
9500 : 207 : if (!bb_vinfo->lookup_stmt (use_stmt)
9501 : 207 : || !vect_slp_is_lane_insert (use_stmt, def, &this_lane)
9502 : 229 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (use_stmt)))
9503 : : break;
9504 : 22 : if (bitmap_bit_p (lanes, this_lane))
9505 : : break;
9506 : 2 : lanes_found++;
9507 : 2 : bitmap_set_bit (lanes, this_lane);
9508 : 2 : gassign *use_ass = as_a <gassign *> (use_stmt);
9509 : 2 : lane_defs.quick_push (std::make_pair
9510 : 2 : (this_lane, gimple_assign_rhs2 (use_ass)));
9511 : 2 : last = bb_vinfo->lookup_stmt (use_ass);
9512 : 2 : roots.safe_push (last);
9513 : 2 : def = gimple_assign_lhs (use_ass);
9514 : : }
9515 : 2 : while (lanes_found < nlanes);
9516 : 211 : if (roots.length () > 1)
9517 : 2 : std::swap(roots[0], roots[roots.length () - 1]);
9518 : 211 : if (lanes_found < nlanes)
9519 : : {
9520 : : /* Now search the def chain. */
9521 : 211 : def = gimple_assign_rhs1 (assign);
9522 : 213 : do
9523 : : {
9524 : 213 : if (TREE_CODE (def) != SSA_NAME
9525 : 213 : || !has_single_use (def))
9526 : : break;
9527 : 56 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
9528 : 56 : unsigned this_lane;
9529 : 56 : if (!bb_vinfo->lookup_stmt (def_stmt)
9530 : 37 : || !vect_slp_is_lane_insert (def_stmt,
9531 : : NULL_TREE, &this_lane)
9532 : 80 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (def_stmt)))
9533 : : break;
9534 : 24 : if (bitmap_bit_p (lanes, this_lane))
9535 : : break;
9536 : 4 : lanes_found++;
9537 : 4 : bitmap_set_bit (lanes, this_lane);
9538 : 8 : lane_defs.quick_push (std::make_pair
9539 : 4 : (this_lane,
9540 : 4 : gimple_assign_rhs2 (def_stmt)));
9541 : 4 : roots.safe_push (bb_vinfo->lookup_stmt (def_stmt));
9542 : 4 : def = gimple_assign_rhs1 (def_stmt);
9543 : : }
9544 : 4 : while (lanes_found < nlanes);
9545 : : }
9546 : 211 : if (lanes_found == nlanes)
9547 : : {
9548 : : /* Sort lane_defs after the lane index and register the root. */
9549 : 2 : lane_defs.qsort (vld_cmp);
9550 : 2 : vec<stmt_vec_info> stmts;
9551 : 2 : stmts.create (nlanes);
9552 : 10 : for (unsigned i = 0; i < nlanes; ++i)
9553 : 8 : stmts.quick_push (bb_vinfo->lookup_def (lane_defs[i].second));
9554 : 2 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
9555 : 2 : stmts, roots));
9556 : : }
9557 : : else
9558 : 209 : roots.release ();
9559 : 211 : }
9560 : 29709196 : else if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
9561 : 28775230 : && (associative_tree_code (code) || code == MINUS_EXPR)
9562 : : /* ??? This pessimizes a two-element reduction. PR54400.
9563 : : ??? In-order reduction could be handled if we only
9564 : : traverse one operand chain in vect_slp_linearize_chain. */
9565 : 33603981 : && !needs_fold_left_reduction_p (TREE_TYPE (rhs), code)
9566 : : /* Ops with constants at the tail can be stripped here. */
9567 : 5773308 : && TREE_CODE (rhs) == SSA_NAME
9568 : 5707487 : && TREE_CODE (gimple_assign_rhs2 (assign)) == SSA_NAME
9569 : : /* Should be the chain end. */
9570 : 31987354 : && (!single_imm_use (gimple_assign_lhs (assign),
9571 : : &use_p, &use_stmt)
9572 : 1757629 : || !is_gimple_assign (use_stmt)
9573 : 1166618 : || (gimple_assign_rhs_code (use_stmt) != code
9574 : 856937 : && ((code != PLUS_EXPR && code != MINUS_EXPR)
9575 : 478349 : || (gimple_assign_rhs_code (use_stmt)
9576 : 478349 : != (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR))))))
9577 : : {
9578 : : /* We start the match at the end of a possible association
9579 : : chain. */
9580 : 1878523 : auto_vec<chain_op_t> chain;
9581 : 1878523 : auto_vec<std::pair<tree_code, gimple *> > worklist;
9582 : 1878523 : auto_vec<gimple *> chain_stmts;
9583 : 1878523 : gimple *code_stmt = NULL, *alt_code_stmt = NULL;
9584 : 1878523 : if (code == MINUS_EXPR)
9585 : 316790 : code = PLUS_EXPR;
9586 : 1878523 : internal_fn reduc_fn;
9587 : 2150486 : if (!reduction_fn_for_scalar_code (code, &reduc_fn)
9588 : 1878523 : || reduc_fn == IFN_LAST)
9589 : 271963 : continue;
9590 : 1606560 : vect_slp_linearize_chain (bb_vinfo, worklist, chain, code, assign,
9591 : : /* ??? */
9592 : : code_stmt, alt_code_stmt, &chain_stmts);
9593 : 3213120 : if (chain.length () > 1)
9594 : : {
9595 : : /* Sort the chain according to def_type and operation. */
9596 : 1606560 : chain.sort (dt_sort_cmp, bb_vinfo);
9597 : : /* ??? Now we'd want to strip externals and constants
9598 : : but record those to be handled in the epilogue. */
9599 : : /* ??? For now do not allow mixing ops or externs/constants. */
9600 : 1606560 : bool invalid = false;
9601 : 1606560 : unsigned remain_cnt = 0;
9602 : 1606560 : unsigned last_idx = 0;
9603 : 4837391 : for (unsigned i = 0; i < chain.length (); ++i)
9604 : : {
9605 : 3569282 : if (chain[i].code != code)
9606 : : {
9607 : : invalid = true;
9608 : : break;
9609 : : }
9610 : 3230831 : if (chain[i].dt != vect_internal_def
9611 : : /* Avoid stmts where the def is not the LHS, like
9612 : : ASMs. */
9613 : 6228910 : || (gimple_get_lhs (bb_vinfo->lookup_def
9614 : 2998079 : (chain[i].op)->stmt)
9615 : 2998079 : != chain[i].op))
9616 : 235696 : remain_cnt++;
9617 : : else
9618 : : last_idx = i;
9619 : : }
9620 : : /* Make sure to have an even number of lanes as we later do
9621 : : all-or-nothing discovery, not trying to split further. */
9622 : 1606560 : if ((chain.length () - remain_cnt) & 1)
9623 : 188323 : remain_cnt++;
9624 : 1606560 : if (!invalid && chain.length () - remain_cnt > 1)
9625 : : {
9626 : 1198704 : vec<stmt_vec_info> stmts;
9627 : 1198704 : vec<tree> remain = vNULL;
9628 : 1198704 : stmts.create (chain.length ());
9629 : 1198704 : if (remain_cnt > 0)
9630 : 110314 : remain.create (remain_cnt);
9631 : 3850262 : for (unsigned i = 0; i < chain.length (); ++i)
9632 : : {
9633 : 2651558 : stmt_vec_info stmt_info;
9634 : 2651558 : if (chain[i].dt == vect_internal_def
9635 : 2614365 : && ((stmt_info = bb_vinfo->lookup_def (chain[i].op)),
9636 : 2614365 : gimple_get_lhs (stmt_info->stmt) == chain[i].op)
9637 : 5265839 : && (i != last_idx
9638 : 1198704 : || (stmts.length () & 1)))
9639 : 2529826 : stmts.quick_push (stmt_info);
9640 : : else
9641 : 121732 : remain.quick_push (chain[i].op);
9642 : : }
9643 : 1198704 : vec<stmt_vec_info> roots;
9644 : 1198704 : roots.create (chain_stmts.length ());
9645 : 2651558 : for (unsigned i = 0; i < chain_stmts.length (); ++i)
9646 : 1452854 : roots.quick_push (bb_vinfo->lookup_stmt (chain_stmts[i]));
9647 : 1198704 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_bb_reduc,
9648 : 1198704 : stmts, roots, remain));
9649 : : }
9650 : : }
9651 : 1878523 : }
9652 : : }
9653 : 2394109 : }
9654 : :
9655 : : /* Walk the grouped store chains and replace entries with their
9656 : : pattern variant if any. */
9657 : :
9658 : : static void
9659 : 635946 : vect_fixup_store_groups_with_patterns (vec_info *vinfo)
9660 : : {
9661 : 635946 : stmt_vec_info first_element;
9662 : 635946 : unsigned i;
9663 : :
9664 : 1522420 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
9665 : : {
9666 : : /* We also have CTORs in this array. */
9667 : 886474 : if (!STMT_VINFO_GROUPED_ACCESS (first_element))
9668 : 0 : continue;
9669 : 886474 : if (STMT_VINFO_IN_PATTERN_P (first_element))
9670 : : {
9671 : 248 : stmt_vec_info orig = first_element;
9672 : 248 : first_element = STMT_VINFO_RELATED_STMT (first_element);
9673 : 248 : DR_GROUP_FIRST_ELEMENT (first_element) = first_element;
9674 : 248 : DR_GROUP_SIZE (first_element) = DR_GROUP_SIZE (orig);
9675 : 248 : DR_GROUP_GAP (first_element) = DR_GROUP_GAP (orig);
9676 : 248 : DR_GROUP_NEXT_ELEMENT (first_element) = DR_GROUP_NEXT_ELEMENT (orig);
9677 : 248 : vinfo->grouped_stores[i] = first_element;
9678 : : }
9679 : 886474 : stmt_vec_info prev = first_element;
9680 : 2493248 : while (DR_GROUP_NEXT_ELEMENT (prev))
9681 : : {
9682 : 1606774 : stmt_vec_info elt = DR_GROUP_NEXT_ELEMENT (prev);
9683 : 1606774 : if (STMT_VINFO_IN_PATTERN_P (elt))
9684 : : {
9685 : 868 : stmt_vec_info orig = elt;
9686 : 868 : elt = STMT_VINFO_RELATED_STMT (elt);
9687 : 868 : DR_GROUP_NEXT_ELEMENT (prev) = elt;
9688 : 868 : DR_GROUP_GAP (elt) = DR_GROUP_GAP (orig);
9689 : 868 : DR_GROUP_NEXT_ELEMENT (elt) = DR_GROUP_NEXT_ELEMENT (orig);
9690 : : }
9691 : 1606774 : DR_GROUP_FIRST_ELEMENT (elt) = first_element;
9692 : 1606774 : prev = elt;
9693 : : }
9694 : : }
9695 : 635946 : }
9696 : :
9697 : : /* Check if the region described by BB_VINFO can be vectorized, returning
9698 : : true if so. When returning false, set FATAL to true if the same failure
9699 : : would prevent vectorization at other vector sizes, false if it is still
9700 : : worth trying other sizes. N_STMTS is the number of statements in the
9701 : : region. */
9702 : :
9703 : : static bool
9704 : 2394109 : vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal,
9705 : : vec<int> *dataref_groups)
9706 : : {
9707 : 2394109 : DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
9708 : :
9709 : 2394109 : slp_instance instance;
9710 : 2394109 : int i;
9711 : :
9712 : : /* The first group of checks is independent of the vector size. */
9713 : 2394109 : fatal = true;
9714 : :
9715 : : /* Analyze the data references. */
9716 : :
9717 : 2394109 : if (!vect_analyze_data_refs (bb_vinfo, NULL))
9718 : : {
9719 : 0 : if (dump_enabled_p ())
9720 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9721 : : "not vectorized: unhandled data-ref in basic "
9722 : : "block.\n");
9723 : 0 : return false;
9724 : : }
9725 : :
9726 : 2394109 : if (!vect_analyze_data_ref_accesses (bb_vinfo, dataref_groups))
9727 : : {
9728 : 0 : if (dump_enabled_p ())
9729 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9730 : : "not vectorized: unhandled data access in "
9731 : : "basic block.\n");
9732 : 0 : return false;
9733 : : }
9734 : :
9735 : 2394109 : vect_slp_check_for_roots (bb_vinfo);
9736 : :
9737 : : /* If there are no grouped stores and no constructors in the region
9738 : : there is no need to continue with pattern recog as vect_analyze_slp
9739 : : will fail anyway. */
9740 : 2394109 : if (bb_vinfo->grouped_stores.is_empty ()
9741 : 2032931 : && bb_vinfo->roots.is_empty ())
9742 : : {
9743 : 1758163 : if (dump_enabled_p ())
9744 : 1032 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9745 : : "not vectorized: no grouped stores in "
9746 : : "basic block.\n");
9747 : 1758163 : return false;
9748 : : }
9749 : :
9750 : : /* While the rest of the analysis below depends on it in some way. */
9751 : 635946 : fatal = false;
9752 : :
9753 : 635946 : vect_pattern_recog (bb_vinfo);
9754 : :
9755 : : /* Update store groups from pattern processing. */
9756 : 635946 : vect_fixup_store_groups_with_patterns (bb_vinfo);
9757 : :
9758 : : /* Check the SLP opportunities in the basic block, analyze and build SLP
9759 : : trees. */
9760 : 635946 : if (!vect_analyze_slp (bb_vinfo, n_stmts, false))
9761 : : {
9762 : 0 : if (dump_enabled_p ())
9763 : : {
9764 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9765 : : "Failed to SLP the basic block.\n");
9766 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9767 : : "not vectorized: failed to find SLP opportunities "
9768 : : "in basic block.\n");
9769 : : }
9770 : 0 : return false;
9771 : : }
9772 : :
9773 : : /* Optimize permutations. */
9774 : 635946 : vect_optimize_slp (bb_vinfo);
9775 : :
9776 : : /* Gather the loads reachable from the SLP graph entries. */
9777 : 635946 : vect_gather_slp_loads (bb_vinfo);
9778 : :
9779 : 635946 : vect_record_base_alignments (bb_vinfo);
9780 : :
9781 : : /* Analyze and verify the alignment of data references and the
9782 : : dependence in the SLP instances. */
9783 : 1427919 : for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
9784 : : {
9785 : 791973 : vect_location = instance->location ();
9786 : 791973 : if (! vect_slp_analyze_instance_alignment (bb_vinfo, instance)
9787 : 791973 : || ! vect_slp_analyze_instance_dependence (bb_vinfo, instance))
9788 : : {
9789 : 7776 : slp_tree node = SLP_INSTANCE_TREE (instance);
9790 : 7776 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
9791 : 7776 : if (dump_enabled_p ())
9792 : 4 : dump_printf_loc (MSG_NOTE, vect_location,
9793 : : "removing SLP instance operations starting from: %G",
9794 : : stmt_info->stmt);
9795 : 7776 : vect_free_slp_instance (instance);
9796 : 7776 : BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
9797 : 7776 : continue;
9798 : 7776 : }
9799 : :
9800 : : /* Mark all the statements that we want to vectorize as pure SLP and
9801 : : relevant. */
9802 : 784197 : vect_mark_slp_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance));
9803 : 784197 : vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
9804 : 784197 : unsigned j;
9805 : 784197 : stmt_vec_info root;
9806 : : /* Likewise consider instance root stmts as vectorized. */
9807 : 1733062 : FOR_EACH_VEC_ELT (SLP_INSTANCE_ROOT_STMTS (instance), j, root)
9808 : 164668 : STMT_SLP_TYPE (root) = pure_slp;
9809 : :
9810 : 784197 : i++;
9811 : : }
9812 : 2425585 : if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
9813 : : return false;
9814 : :
9815 : 281601 : if (!vect_slp_analyze_operations (bb_vinfo))
9816 : : {
9817 : 31476 : if (dump_enabled_p ())
9818 : 84 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9819 : : "not vectorized: bad operation in basic block.\n");
9820 : 31476 : return false;
9821 : : }
9822 : :
9823 : 250125 : vect_bb_partition_graph (bb_vinfo);
9824 : :
9825 : 250125 : return true;
9826 : : }
9827 : :
9828 : : /* Subroutine of vect_slp_bb. Try to vectorize the statements for all
9829 : : basic blocks in BBS, returning true on success.
9830 : : The region has N_STMTS statements and has the datarefs given by DATAREFS. */
9831 : :
9832 : : static bool
9833 : 2063871 : vect_slp_region (vec<basic_block> bbs, vec<data_reference_p> datarefs,
9834 : : vec<int> *dataref_groups, unsigned int n_stmts,
9835 : : loop_p orig_loop)
9836 : : {
9837 : 2063871 : bb_vec_info bb_vinfo;
9838 : 2063871 : auto_vector_modes vector_modes;
9839 : :
9840 : : /* Autodetect first vector size we try. */
9841 : 2063871 : machine_mode next_vector_mode = VOIDmode;
9842 : 2063871 : targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
9843 : 2063871 : unsigned int mode_i = 0;
9844 : :
9845 : 2063871 : vec_info_shared shared;
9846 : :
9847 : 2063871 : machine_mode autodetected_vector_mode = VOIDmode;
9848 : 2724347 : while (1)
9849 : : {
9850 : 2394109 : bool vectorized = false;
9851 : 2394109 : bool fatal = false;
9852 : 2394109 : bb_vinfo = new _bb_vec_info (bbs, &shared);
9853 : :
9854 : 2394109 : bool first_time_p = shared.datarefs.is_empty ();
9855 : 2394109 : BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
9856 : 2394109 : if (first_time_p)
9857 : 2086423 : bb_vinfo->shared->save_datarefs ();
9858 : : else
9859 : 307686 : bb_vinfo->shared->check_datarefs ();
9860 : 2394109 : bb_vinfo->vector_mode = next_vector_mode;
9861 : :
9862 : 2394109 : if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal, dataref_groups))
9863 : : {
9864 : 250125 : if (dump_enabled_p ())
9865 : : {
9866 : 1486 : dump_printf_loc (MSG_NOTE, vect_location,
9867 : : "***** Analysis succeeded with vector mode"
9868 : 743 : " %s\n", GET_MODE_NAME (bb_vinfo->vector_mode));
9869 : 743 : dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
9870 : : }
9871 : :
9872 : 250125 : bb_vinfo->shared->check_datarefs ();
9873 : :
9874 : 250125 : bool force_clear = false;
9875 : 250125 : auto_vec<slp_instance> profitable_subgraphs;
9876 : 1430322 : for (slp_instance instance : BB_VINFO_SLP_INSTANCES (bb_vinfo))
9877 : : {
9878 : 679947 : if (instance->subgraph_entries.is_empty ())
9879 : 215936 : continue;
9880 : :
9881 : 662230 : dump_user_location_t saved_vect_location = vect_location;
9882 : 662230 : vect_location = instance->location ();
9883 : 662230 : if (!unlimited_cost_model (NULL)
9884 : 1321300 : && !vect_bb_vectorization_profitable_p
9885 : 659070 : (bb_vinfo, instance->subgraph_entries, orig_loop))
9886 : : {
9887 : 180502 : if (dump_enabled_p ())
9888 : 29 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9889 : : "not vectorized: vectorization is not "
9890 : : "profitable.\n");
9891 : 180502 : vect_location = saved_vect_location;
9892 : 180502 : continue;
9893 : : }
9894 : :
9895 : 481728 : vect_location = saved_vect_location;
9896 : 481728 : if (!dbg_cnt (vect_slp))
9897 : : {
9898 : 0 : force_clear = true;
9899 : 0 : continue;
9900 : : }
9901 : :
9902 : 481728 : profitable_subgraphs.safe_push (instance);
9903 : : }
9904 : :
9905 : : /* When we're vectorizing an if-converted loop body make sure
9906 : : we vectorized all if-converted code. */
9907 : 417877 : if ((!profitable_subgraphs.is_empty () || force_clear) && orig_loop)
9908 : : {
9909 : 103 : gcc_assert (bb_vinfo->nbbs == 1);
9910 : 206 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[0]);
9911 : 4256 : !gsi_end_p (gsi); gsi_next (&gsi))
9912 : : {
9913 : : /* The costing above left us with DCEable vectorized scalar
9914 : : stmts having the visited flag set on profitable
9915 : : subgraphs. Do the delayed clearing of the flag here. */
9916 : 4153 : if (gimple_visited_p (gsi_stmt (gsi)))
9917 : : {
9918 : 1180 : gimple_set_visited (gsi_stmt (gsi), false);
9919 : 1180 : continue;
9920 : : }
9921 : 2973 : if (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED)
9922 : 867 : continue;
9923 : :
9924 : 6121 : if (gassign *ass = dyn_cast <gassign *> (gsi_stmt (gsi)))
9925 : 2558 : if (gimple_assign_rhs_code (ass) == COND_EXPR)
9926 : : {
9927 : 63 : if (!profitable_subgraphs.is_empty ()
9928 : 26 : && dump_enabled_p ())
9929 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9930 : : "not profitable because of "
9931 : : "unprofitable if-converted scalar "
9932 : : "code\n");
9933 : 37 : profitable_subgraphs.truncate (0);
9934 : : }
9935 : : }
9936 : : }
9937 : :
9938 : : /* Finally schedule the profitable subgraphs. */
9939 : 1067321 : for (slp_instance instance : profitable_subgraphs)
9940 : : {
9941 : 481692 : if (!vectorized && dump_enabled_p ())
9942 : 717 : dump_printf_loc (MSG_NOTE, vect_location,
9943 : : "Basic block will be vectorized "
9944 : : "using SLP\n");
9945 : 481692 : vectorized = true;
9946 : :
9947 : : /* Dump before scheduling as store vectorization will remove
9948 : : the original stores and mess with the instance tree
9949 : : so querying its location will eventually ICE. */
9950 : 481692 : if (flag_checking)
9951 : 1936560 : for (slp_instance sub : instance->subgraph_entries)
9952 : 491484 : gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
9953 : 481692 : unsigned HOST_WIDE_INT bytes;
9954 : 481692 : if (dump_enabled_p ())
9955 : 3387 : for (slp_instance sub : instance->subgraph_entries)
9956 : : {
9957 : 897 : tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
9958 : 1794 : if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
9959 : 897 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
9960 : 897 : sub->location (),
9961 : : "basic block part vectorized using %wu "
9962 : : "byte vectors\n", bytes);
9963 : : else
9964 : : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
9965 : : sub->location (),
9966 : : "basic block part vectorized using "
9967 : : "variable length vectors\n");
9968 : : }
9969 : :
9970 : 481692 : dump_user_location_t saved_vect_location = vect_location;
9971 : 481692 : vect_location = instance->location ();
9972 : :
9973 : 481692 : vect_schedule_slp (bb_vinfo, instance->subgraph_entries);
9974 : :
9975 : 481692 : vect_location = saved_vect_location;
9976 : : }
9977 : :
9978 : :
9979 : : /* Generate the invariant statements. */
9980 : 250125 : if (!gimple_seq_empty_p (bb_vinfo->inv_pattern_def_seq))
9981 : : {
9982 : 23 : if (dump_enabled_p ())
9983 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9984 : : "------>generating invariant statements\n");
9985 : :
9986 : 23 : bb_vinfo->insert_seq_on_entry (NULL,
9987 : : bb_vinfo->inv_pattern_def_seq);
9988 : : }
9989 : 250125 : }
9990 : : else
9991 : : {
9992 : 2143984 : if (dump_enabled_p ())
9993 : 1324 : dump_printf_loc (MSG_NOTE, vect_location,
9994 : : "***** Analysis failed with vector mode %s\n",
9995 : 1324 : GET_MODE_NAME (bb_vinfo->vector_mode));
9996 : : }
9997 : :
9998 : 2394109 : if (mode_i == 0)
9999 : 2063871 : autodetected_vector_mode = bb_vinfo->vector_mode;
10000 : :
10001 : 2394109 : if (!fatal)
10002 : 3380244 : while (mode_i < vector_modes.length ()
10003 : 1834247 : && vect_chooses_same_modes_p (bb_vinfo, vector_modes[mode_i]))
10004 : : {
10005 : 350189 : if (dump_enabled_p ())
10006 : 1636 : dump_printf_loc (MSG_NOTE, vect_location,
10007 : : "***** The result for vector mode %s would"
10008 : : " be the same\n",
10009 : 818 : GET_MODE_NAME (vector_modes[mode_i]));
10010 : 350189 : mode_i += 1;
10011 : : }
10012 : :
10013 : 2394109 : delete bb_vinfo;
10014 : :
10015 : 2394109 : if (mode_i < vector_modes.length ()
10016 : 2209552 : && VECTOR_MODE_P (autodetected_vector_mode)
10017 : 2082584 : && (related_vector_mode (vector_modes[mode_i],
10018 : : GET_MODE_INNER (autodetected_vector_mode))
10019 : 1041292 : == autodetected_vector_mode)
10020 : 4603661 : && (related_vector_mode (autodetected_vector_mode,
10021 : 542948 : GET_MODE_INNER (vector_modes[mode_i]))
10022 : 1085896 : == vector_modes[mode_i]))
10023 : : {
10024 : 542948 : if (dump_enabled_p ())
10025 : 210 : dump_printf_loc (MSG_NOTE, vect_location,
10026 : : "***** Skipping vector mode %s, which would"
10027 : : " repeat the analysis for %s\n",
10028 : 210 : GET_MODE_NAME (vector_modes[mode_i]),
10029 : 210 : GET_MODE_NAME (autodetected_vector_mode));
10030 : 542948 : mode_i += 1;
10031 : : }
10032 : :
10033 : 2394109 : if (vectorized
10034 : 2226383 : || mode_i == vector_modes.length ()
10035 : 2041867 : || autodetected_vector_mode == VOIDmode
10036 : : /* If vect_slp_analyze_bb_1 signaled that analysis for all
10037 : : vector sizes will fail do not bother iterating. */
10038 : 3267716 : || fatal)
10039 : 4127742 : return vectorized;
10040 : :
10041 : : /* Try the next biggest vector size. */
10042 : 330238 : next_vector_mode = vector_modes[mode_i++];
10043 : 330238 : if (dump_enabled_p ())
10044 : 219 : dump_printf_loc (MSG_NOTE, vect_location,
10045 : : "***** Re-trying analysis with vector mode %s\n",
10046 : 219 : GET_MODE_NAME (next_vector_mode));
10047 : 330238 : }
10048 : 2063871 : }
10049 : :
10050 : :
10051 : : /* Main entry for the BB vectorizer. Analyze and transform BBS, returns
10052 : : true if anything in the basic-block was vectorized. */
10053 : :
10054 : : static bool
10055 : 2063871 : vect_slp_bbs (const vec<basic_block> &bbs, loop_p orig_loop)
10056 : : {
10057 : 2063871 : vec<data_reference_p> datarefs = vNULL;
10058 : 2063871 : auto_vec<int> dataref_groups;
10059 : 2063871 : int insns = 0;
10060 : 2063871 : int current_group = 0;
10061 : :
10062 : 13318779 : for (unsigned i = 0; i < bbs.length (); i++)
10063 : : {
10064 : 11254908 : basic_block bb = bbs[i];
10065 : 89479442 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
10066 : 78224534 : gsi_next (&gsi))
10067 : : {
10068 : 78224534 : gimple *stmt = gsi_stmt (gsi);
10069 : 78224534 : if (is_gimple_debug (stmt))
10070 : 48166946 : continue;
10071 : :
10072 : 30057588 : insns++;
10073 : :
10074 : 30057588 : if (gimple_location (stmt) != UNKNOWN_LOCATION)
10075 : 27016341 : vect_location = stmt;
10076 : :
10077 : 30057588 : if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs,
10078 : : &dataref_groups, current_group))
10079 : 5185920 : ++current_group;
10080 : : }
10081 : : /* New BBs always start a new DR group. */
10082 : 11254908 : ++current_group;
10083 : : }
10084 : :
10085 : 2063871 : return vect_slp_region (bbs, datarefs, &dataref_groups, insns, orig_loop);
10086 : 2063871 : }
10087 : :
10088 : : /* Special entry for the BB vectorizer. Analyze and transform a single
10089 : : if-converted BB with ORIG_LOOPs body being the not if-converted
10090 : : representation. Returns true if anything in the basic-block was
10091 : : vectorized. */
10092 : :
10093 : : bool
10094 : 17152 : vect_slp_if_converted_bb (basic_block bb, loop_p orig_loop)
10095 : : {
10096 : 17152 : auto_vec<basic_block> bbs;
10097 : 17152 : bbs.safe_push (bb);
10098 : 17152 : return vect_slp_bbs (bbs, orig_loop);
10099 : 17152 : }
10100 : :
10101 : : /* Main entry for the BB vectorizer. Analyze and transform BB, returns
10102 : : true if anything in the basic-block was vectorized. */
10103 : :
10104 : : bool
10105 : 905232 : vect_slp_function (function *fun)
10106 : : {
10107 : 905232 : bool r = false;
10108 : 905232 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
10109 : 905232 : auto_bitmap exit_bbs;
10110 : 905232 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
10111 : 905232 : edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
10112 : 905232 : unsigned n = rev_post_order_and_mark_dfs_back_seme (fun, entry, exit_bbs,
10113 : 905232 : true, rpo, NULL);
10114 : :
10115 : : /* For the moment split the function into pieces to avoid making
10116 : : the iteration on the vector mode moot. Split at points we know
10117 : : to not handle well which is CFG merges (SLP discovery doesn't
10118 : : handle non-loop-header PHIs) and loop exits. Since pattern
10119 : : recog requires reverse iteration to visit uses before defs
10120 : : simply chop RPO into pieces. */
10121 : 905232 : auto_vec<basic_block> bbs;
10122 : 12171580 : for (unsigned i = 0; i < n; i++)
10123 : : {
10124 : 11266348 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
10125 : 11266348 : bool split = false;
10126 : :
10127 : : /* Split when a BB is not dominated by the first block. */
10128 : 21270485 : if (!bbs.is_empty ()
10129 : 10004137 : && !dominated_by_p (CDI_DOMINATORS, bb, bbs[0]))
10130 : : {
10131 : 801378 : if (dump_enabled_p ())
10132 : 162 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10133 : : "splitting region at dominance boundary bb%d\n",
10134 : : bb->index);
10135 : : split = true;
10136 : : }
10137 : : /* Split when the loop determined by the first block
10138 : : is exited. This is because we eventually insert
10139 : : invariants at region begin. */
10140 : 19667729 : else if (!bbs.is_empty ()
10141 : 9202759 : && bbs[0]->loop_father != bb->loop_father
10142 : 2312104 : && !flow_loop_nested_p (bbs[0]->loop_father, bb->loop_father))
10143 : : {
10144 : 5043 : if (dump_enabled_p ())
10145 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10146 : : "splitting region at loop %d exit at bb%d\n",
10147 : 3 : bbs[0]->loop_father->num, bb->index);
10148 : : split = true;
10149 : : }
10150 : 10459927 : else if (!bbs.is_empty ()
10151 : 9197716 : && bb->loop_father->header == bb
10152 : 479268 : && bb->loop_father->dont_vectorize)
10153 : : {
10154 : 6679 : if (dump_enabled_p ())
10155 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10156 : : "splitting region at dont-vectorize loop %d "
10157 : : "entry at bb%d\n",
10158 : : bb->loop_father->num, bb->index);
10159 : : split = true;
10160 : : }
10161 : :
10162 : 12079448 : if (split && !bbs.is_empty ())
10163 : : {
10164 : 813100 : r |= vect_slp_bbs (bbs, NULL);
10165 : 813100 : bbs.truncate (0);
10166 : : }
10167 : :
10168 : 11266348 : if (bbs.is_empty ())
10169 : : {
10170 : : /* We need to be able to insert at the head of the region which
10171 : : we cannot for region starting with a returns-twice call. */
10172 : 2075311 : if (gcall *first = safe_dyn_cast <gcall *> (first_stmt (bb)))
10173 : 404065 : if (gimple_call_flags (first) & ECF_RETURNS_TWICE)
10174 : : {
10175 : 294 : if (dump_enabled_p ())
10176 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10177 : : "skipping bb%d as start of region as it "
10178 : : "starts with returns-twice call\n",
10179 : : bb->index);
10180 : 28592 : continue;
10181 : : }
10182 : : /* If the loop this BB belongs to is marked as not to be vectorized
10183 : : honor that also for BB vectorization. */
10184 : 2075017 : if (bb->loop_father->dont_vectorize)
10185 : 28298 : continue;
10186 : : }
10187 : :
10188 : 11237756 : bbs.safe_push (bb);
10189 : :
10190 : : /* When we have a stmt ending this block and defining a
10191 : : value we have to insert on edges when inserting after it for
10192 : : a vector containing its definition. Avoid this for now. */
10193 : 22475512 : if (gimple *last = *gsi_last_bb (bb))
10194 : 8887794 : if (gimple_get_lhs (last)
10195 : 8887794 : && is_ctrl_altering_stmt (last))
10196 : : {
10197 : 328394 : if (dump_enabled_p ())
10198 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10199 : : "splitting region at control altering "
10200 : : "definition %G", last);
10201 : 328394 : r |= vect_slp_bbs (bbs, NULL);
10202 : 328394 : bbs.truncate (0);
10203 : : }
10204 : : }
10205 : :
10206 : 905232 : if (!bbs.is_empty ())
10207 : 905225 : r |= vect_slp_bbs (bbs, NULL);
10208 : :
10209 : 905232 : free (rpo);
10210 : :
10211 : 905232 : return r;
10212 : 905232 : }
10213 : :
10214 : : /* Build a variable-length vector in which the elements in ELTS are repeated
10215 : : to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
10216 : : RESULTS and add any new instructions to SEQ.
10217 : :
10218 : : The approach we use is:
10219 : :
10220 : : (1) Find a vector mode VM with integer elements of mode IM.
10221 : :
10222 : : (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10223 : : ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
10224 : : from small vectors to IM.
10225 : :
10226 : : (3) Duplicate each ELTS'[I] into a vector of mode VM.
10227 : :
10228 : : (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
10229 : : correct byte contents.
10230 : :
10231 : : (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
10232 : :
10233 : : We try to find the largest IM for which this sequence works, in order
10234 : : to cut down on the number of interleaves. */
10235 : :
10236 : : void
10237 : 0 : duplicate_and_interleave (vec_info *vinfo, gimple_seq *seq, tree vector_type,
10238 : : const vec<tree> &elts, unsigned int nresults,
10239 : : vec<tree> &results)
10240 : : {
10241 : 0 : unsigned int nelts = elts.length ();
10242 : 0 : tree element_type = TREE_TYPE (vector_type);
10243 : :
10244 : : /* (1) Find a vector mode VM with integer elements of mode IM. */
10245 : 0 : unsigned int nvectors = 1;
10246 : 0 : tree new_vector_type;
10247 : 0 : tree permutes[2];
10248 : 0 : if (!can_duplicate_and_interleave_p (vinfo, nelts, element_type,
10249 : : &nvectors, &new_vector_type,
10250 : : permutes))
10251 : 0 : gcc_unreachable ();
10252 : :
10253 : : /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
10254 : 0 : unsigned int partial_nelts = nelts / nvectors;
10255 : 0 : tree partial_vector_type = build_vector_type (element_type, partial_nelts);
10256 : :
10257 : 0 : tree_vector_builder partial_elts;
10258 : 0 : auto_vec<tree, 32> pieces (nvectors * 2);
10259 : 0 : pieces.quick_grow_cleared (nvectors * 2);
10260 : 0 : for (unsigned int i = 0; i < nvectors; ++i)
10261 : : {
10262 : : /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10263 : : ELTS' has mode IM. */
10264 : 0 : partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
10265 : 0 : for (unsigned int j = 0; j < partial_nelts; ++j)
10266 : 0 : partial_elts.quick_push (elts[i * partial_nelts + j]);
10267 : 0 : tree t = gimple_build_vector (seq, &partial_elts);
10268 : 0 : t = gimple_build (seq, VIEW_CONVERT_EXPR,
10269 : 0 : TREE_TYPE (new_vector_type), t);
10270 : :
10271 : : /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
10272 : 0 : pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
10273 : : }
10274 : :
10275 : : /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
10276 : : correct byte contents.
10277 : :
10278 : : Conceptually, we need to repeat the following operation log2(nvectors)
10279 : : times, where hi_start = nvectors / 2:
10280 : :
10281 : : out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
10282 : : out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
10283 : :
10284 : : However, if each input repeats every N elements and the VF is
10285 : : a multiple of N * 2, the HI result is the same as the LO result.
10286 : : This will be true for the first N1 iterations of the outer loop,
10287 : : followed by N2 iterations for which both the LO and HI results
10288 : : are needed. I.e.:
10289 : :
10290 : : N1 + N2 = log2(nvectors)
10291 : :
10292 : : Each "N1 iteration" doubles the number of redundant vectors and the
10293 : : effect of the process as a whole is to have a sequence of nvectors/2**N1
10294 : : vectors that repeats 2**N1 times. Rather than generate these redundant
10295 : : vectors, we halve the number of vectors for each N1 iteration. */
10296 : : unsigned int in_start = 0;
10297 : : unsigned int out_start = nvectors;
10298 : : unsigned int new_nvectors = nvectors;
10299 : 0 : for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
10300 : : {
10301 : 0 : unsigned int hi_start = new_nvectors / 2;
10302 : 0 : unsigned int out_i = 0;
10303 : 0 : for (unsigned int in_i = 0; in_i < new_nvectors; ++in_i)
10304 : : {
10305 : 0 : if ((in_i & 1) != 0
10306 : 0 : && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
10307 : : 2 * in_repeat))
10308 : 0 : continue;
10309 : :
10310 : 0 : tree output = make_ssa_name (new_vector_type);
10311 : 0 : tree input1 = pieces[in_start + (in_i / 2)];
10312 : 0 : tree input2 = pieces[in_start + (in_i / 2) + hi_start];
10313 : 0 : gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
10314 : : input1, input2,
10315 : : permutes[in_i & 1]);
10316 : 0 : gimple_seq_add_stmt (seq, stmt);
10317 : 0 : pieces[out_start + out_i] = output;
10318 : 0 : out_i += 1;
10319 : : }
10320 : 0 : std::swap (in_start, out_start);
10321 : 0 : new_nvectors = out_i;
10322 : : }
10323 : :
10324 : : /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
10325 : 0 : results.reserve (nresults);
10326 : 0 : for (unsigned int i = 0; i < nresults; ++i)
10327 : 0 : if (i < new_nvectors)
10328 : 0 : results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
10329 : 0 : pieces[in_start + i]));
10330 : : else
10331 : 0 : results.quick_push (results[i - new_nvectors]);
10332 : 0 : }
10333 : :
10334 : :
10335 : : /* For constant and loop invariant defs in OP_NODE this function creates
10336 : : vector defs that will be used in the vectorized stmts and stores them
10337 : : to SLP_TREE_VEC_DEFS of OP_NODE. */
10338 : :
10339 : : static void
10340 : 491998 : vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
10341 : : {
10342 : 491998 : unsigned HOST_WIDE_INT nunits;
10343 : 491998 : tree vec_cst;
10344 : 491998 : unsigned j, number_of_places_left_in_vector;
10345 : 491998 : tree vector_type;
10346 : 491998 : tree vop;
10347 : 491998 : int group_size = op_node->ops.length ();
10348 : 491998 : unsigned int vec_num, i;
10349 : 491998 : unsigned number_of_copies = 1;
10350 : 491998 : bool constant_p;
10351 : 491998 : gimple_seq ctor_seq = NULL;
10352 : 491998 : auto_vec<tree, 16> permute_results;
10353 : :
10354 : : /* We always want SLP_TREE_VECTYPE (op_node) here correctly set. */
10355 : 491998 : vector_type = SLP_TREE_VECTYPE (op_node);
10356 : :
10357 : 491998 : unsigned int number_of_vectors = vect_get_num_copies (vinfo, op_node);
10358 : 491998 : SLP_TREE_VEC_DEFS (op_node).create (number_of_vectors);
10359 : 491998 : auto_vec<tree> voprnds (number_of_vectors);
10360 : :
10361 : : /* NUMBER_OF_COPIES is the number of times we need to use the same values in
10362 : : created vectors. It is greater than 1 if unrolling is performed.
10363 : :
10364 : : For example, we have two scalar operands, s1 and s2 (e.g., group of
10365 : : strided accesses of size two), while NUNITS is four (i.e., four scalars
10366 : : of this type can be packed in a vector). The output vector will contain
10367 : : two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
10368 : : will be 2).
10369 : :
10370 : : If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
10371 : : containing the operands.
10372 : :
10373 : : For example, NUNITS is four as before, and the group size is 8
10374 : : (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
10375 : : {s5, s6, s7, s8}. */
10376 : :
10377 : : /* When using duplicate_and_interleave, we just need one element for
10378 : : each scalar statement. */
10379 : 491998 : if (!TYPE_VECTOR_SUBPARTS (vector_type).is_constant (&nunits))
10380 : : nunits = group_size;
10381 : :
10382 : 491998 : number_of_copies = nunits * number_of_vectors / group_size;
10383 : :
10384 : 491998 : number_of_places_left_in_vector = nunits;
10385 : 491998 : constant_p = true;
10386 : 491998 : tree uniform_elt = NULL_TREE;
10387 : 491998 : tree_vector_builder elts (vector_type, nunits, 1);
10388 : 491998 : elts.quick_grow (nunits);
10389 : 491998 : stmt_vec_info insert_after = NULL;
10390 : 1452947 : for (j = 0; j < number_of_copies; j++)
10391 : : {
10392 : 960949 : tree op;
10393 : 3699346 : for (i = group_size - 1; op_node->ops.iterate (i, &op); i--)
10394 : : {
10395 : : /* Create 'vect_ = {op0,op1,...,opn}'. */
10396 : 1777448 : tree orig_op = op;
10397 : 1777448 : if (number_of_places_left_in_vector == nunits)
10398 : : uniform_elt = op;
10399 : 1155308 : else if (uniform_elt && operand_equal_p (uniform_elt, op))
10400 : 728215 : op = elts[number_of_places_left_in_vector];
10401 : : else
10402 : : uniform_elt = NULL_TREE;
10403 : 1777448 : number_of_places_left_in_vector--;
10404 : 1777448 : if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
10405 : : {
10406 : 281897 : if (CONSTANT_CLASS_P (op))
10407 : : {
10408 : 102965 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10409 : : {
10410 : : /* Can't use VIEW_CONVERT_EXPR for booleans because
10411 : : of possibly different sizes of scalar value and
10412 : : vector element. */
10413 : 64 : if (integer_zerop (op))
10414 : 64 : op = build_int_cst (TREE_TYPE (vector_type), 0);
10415 : 0 : else if (integer_onep (op))
10416 : 0 : op = build_all_ones_cst (TREE_TYPE (vector_type));
10417 : : else
10418 : 0 : gcc_unreachable ();
10419 : : }
10420 : : else
10421 : 102901 : op = fold_unary (VIEW_CONVERT_EXPR,
10422 : : TREE_TYPE (vector_type), op);
10423 : 102965 : gcc_assert (op && CONSTANT_CLASS_P (op));
10424 : : }
10425 : : else
10426 : : {
10427 : 178932 : tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
10428 : 178932 : gimple *init_stmt;
10429 : 178932 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10430 : : {
10431 : 397 : tree true_val
10432 : 397 : = build_all_ones_cst (TREE_TYPE (vector_type));
10433 : 397 : tree false_val
10434 : 397 : = build_zero_cst (TREE_TYPE (vector_type));
10435 : 397 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
10436 : 397 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
10437 : : op, true_val,
10438 : : false_val);
10439 : : }
10440 : : else
10441 : : {
10442 : 178535 : op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
10443 : : op);
10444 : 178535 : init_stmt
10445 : 178535 : = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
10446 : : op);
10447 : : }
10448 : 178932 : gimple_seq_add_stmt (&ctor_seq, init_stmt);
10449 : 178932 : op = new_temp;
10450 : : }
10451 : : }
10452 : 1777448 : elts[number_of_places_left_in_vector] = op;
10453 : 1777448 : if (!CONSTANT_CLASS_P (op))
10454 : 319094 : constant_p = false;
10455 : : /* For BB vectorization we have to compute an insert location
10456 : : when a def is inside the analyzed region since we cannot
10457 : : simply insert at the BB start in this case. */
10458 : 1777448 : stmt_vec_info opdef;
10459 : 1777448 : if (TREE_CODE (orig_op) == SSA_NAME
10460 : 181885 : && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
10461 : 165919 : && is_a <bb_vec_info> (vinfo)
10462 : 1891172 : && (opdef = vinfo->lookup_def (orig_op)))
10463 : : {
10464 : 91129 : if (!insert_after)
10465 : : insert_after = opdef;
10466 : : else
10467 : 50363 : insert_after = get_later_stmt (insert_after, opdef);
10468 : : }
10469 : :
10470 : 1777448 : if (number_of_places_left_in_vector == 0)
10471 : : {
10472 : 622140 : auto type_nunits = TYPE_VECTOR_SUBPARTS (vector_type);
10473 : 622140 : if (uniform_elt)
10474 : 646750 : vec_cst = gimple_build_vector_from_val (&ctor_seq, vector_type,
10475 : 323375 : elts[0]);
10476 : 597530 : else if (constant_p
10477 : 597530 : ? multiple_p (type_nunits, nunits)
10478 : 114461 : : known_eq (type_nunits, nunits))
10479 : 298765 : vec_cst = gimple_build_vector (&ctor_seq, &elts);
10480 : : else
10481 : : {
10482 : 0 : if (permute_results.is_empty ())
10483 : 0 : duplicate_and_interleave (vinfo, &ctor_seq, vector_type,
10484 : : elts, number_of_vectors,
10485 : : permute_results);
10486 : 0 : vec_cst = permute_results[number_of_vectors - j - 1];
10487 : : }
10488 : 622140 : if (!gimple_seq_empty_p (ctor_seq))
10489 : : {
10490 : 140658 : if (insert_after)
10491 : : {
10492 : 40766 : gimple_stmt_iterator gsi;
10493 : 40766 : if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
10494 : : {
10495 : 915 : gsi = gsi_after_labels (gimple_bb (insert_after->stmt));
10496 : 915 : gsi_insert_seq_before (&gsi, ctor_seq,
10497 : : GSI_CONTINUE_LINKING);
10498 : : }
10499 : 39851 : else if (!stmt_ends_bb_p (insert_after->stmt))
10500 : : {
10501 : 39851 : gsi = gsi_for_stmt (insert_after->stmt);
10502 : 39851 : gsi_insert_seq_after (&gsi, ctor_seq,
10503 : : GSI_CONTINUE_LINKING);
10504 : : }
10505 : : else
10506 : : {
10507 : : /* When we want to insert after a def where the
10508 : : defining stmt throws then insert on the fallthru
10509 : : edge. */
10510 : 0 : edge e = find_fallthru_edge
10511 : 0 : (gimple_bb (insert_after->stmt)->succs);
10512 : 0 : basic_block new_bb
10513 : 0 : = gsi_insert_seq_on_edge_immediate (e, ctor_seq);
10514 : 0 : gcc_assert (!new_bb);
10515 : : }
10516 : : }
10517 : : else
10518 : 99892 : vinfo->insert_seq_on_entry (NULL, ctor_seq);
10519 : 140658 : ctor_seq = NULL;
10520 : : }
10521 : 622140 : voprnds.quick_push (vec_cst);
10522 : 622140 : insert_after = NULL;
10523 : 622140 : number_of_places_left_in_vector = nunits;
10524 : 622140 : constant_p = true;
10525 : 622140 : elts.new_vector (vector_type, nunits, 1);
10526 : 622140 : elts.quick_grow (nunits);
10527 : : }
10528 : : }
10529 : : }
10530 : :
10531 : : /* Since the vectors are created in the reverse order, we should invert
10532 : : them. */
10533 : 491998 : vec_num = voprnds.length ();
10534 : 1114138 : for (j = vec_num; j != 0; j--)
10535 : : {
10536 : 622140 : vop = voprnds[j - 1];
10537 : 622140 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10538 : : }
10539 : :
10540 : : /* In case that VF is greater than the unrolling factor needed for the SLP
10541 : : group of stmts, NUMBER_OF_VECTORS to be created is greater than
10542 : : NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
10543 : : to replicate the vectors. */
10544 : 491998 : while (number_of_vectors > SLP_TREE_VEC_DEFS (op_node).length ())
10545 : 491998 : for (i = 0; SLP_TREE_VEC_DEFS (op_node).iterate (i, &vop) && i < vec_num;
10546 : : i++)
10547 : 0 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10548 : 491998 : }
10549 : :
10550 : : /* Get the scalar definition of the Nth lane from SLP_NODE or NULL_TREE
10551 : : if there is no definition for it in the scalar IL or it is not known. */
10552 : :
10553 : : tree
10554 : 948 : vect_get_slp_scalar_def (slp_tree slp_node, unsigned n)
10555 : : {
10556 : 948 : if (SLP_TREE_DEF_TYPE (slp_node) == vect_internal_def)
10557 : : {
10558 : 948 : if (!SLP_TREE_SCALAR_STMTS (slp_node).exists ())
10559 : : return NULL_TREE;
10560 : 948 : stmt_vec_info def = SLP_TREE_SCALAR_STMTS (slp_node)[n];
10561 : 948 : if (!def)
10562 : : return NULL_TREE;
10563 : 948 : return gimple_get_lhs (STMT_VINFO_STMT (def));
10564 : : }
10565 : : else
10566 : 0 : return SLP_TREE_SCALAR_OPS (slp_node)[n];
10567 : : }
10568 : :
10569 : : /* Get the Ith vectorized definition from SLP_NODE. */
10570 : :
10571 : : tree
10572 : 143202 : vect_get_slp_vect_def (slp_tree slp_node, unsigned i)
10573 : : {
10574 : 143202 : return SLP_TREE_VEC_DEFS (slp_node)[i];
10575 : : }
10576 : :
10577 : : /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS. */
10578 : :
10579 : : void
10580 : 913431 : vect_get_slp_defs (slp_tree slp_node, vec<tree> *vec_defs)
10581 : : {
10582 : 1826862 : vec_defs->create (SLP_TREE_VEC_DEFS (slp_node).length ());
10583 : 913431 : vec_defs->splice (SLP_TREE_VEC_DEFS (slp_node));
10584 : 913431 : }
10585 : :
10586 : : /* Get N vectorized definitions for SLP_NODE. */
10587 : :
10588 : : void
10589 : 2859 : vect_get_slp_defs (vec_info *,
10590 : : slp_tree slp_node, vec<vec<tree> > *vec_oprnds, unsigned n)
10591 : : {
10592 : 2859 : if (n == -1U)
10593 : 2859 : n = SLP_TREE_CHILDREN (slp_node).length ();
10594 : :
10595 : 9986 : for (unsigned i = 0; i < n; ++i)
10596 : : {
10597 : 7127 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[i];
10598 : 7127 : vec<tree> vec_defs = vNULL;
10599 : 7127 : vect_get_slp_defs (child, &vec_defs);
10600 : 7127 : vec_oprnds->quick_push (vec_defs);
10601 : : }
10602 : 2859 : }
10603 : :
10604 : : /* A subroutine of vect_transform_slp_perm_load with two extra arguments:
10605 : : - PERM gives the permutation that the caller wants to use for NODE,
10606 : : which might be different from SLP_LOAD_PERMUTATION.
10607 : : - DUMP_P controls whether the function dumps information. */
10608 : :
10609 : : static bool
10610 : 123071 : vect_transform_slp_perm_load_1 (vec_info *vinfo, slp_tree node,
10611 : : load_permutation_t &perm,
10612 : : const vec<tree> &dr_chain,
10613 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
10614 : : bool analyze_only, bool dump_p,
10615 : : unsigned *n_perms, unsigned int *n_loads,
10616 : : bool dce_chain)
10617 : : {
10618 : 123071 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
10619 : 123071 : int vec_index = 0;
10620 : 123071 : tree vectype = SLP_TREE_VECTYPE (node);
10621 : 123071 : unsigned int group_size = SLP_TREE_SCALAR_STMTS (node).length ();
10622 : 123071 : unsigned int mask_element;
10623 : 123071 : unsigned dr_group_size;
10624 : 123071 : machine_mode mode;
10625 : :
10626 : 123071 : if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
10627 : : dr_group_size = 1;
10628 : : else
10629 : : {
10630 : 122098 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10631 : 122098 : dr_group_size = DR_GROUP_SIZE (stmt_info);
10632 : : }
10633 : :
10634 : 123071 : mode = TYPE_MODE (vectype);
10635 : 123071 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10636 : 123071 : unsigned int nstmts = vect_get_num_copies (vinfo, node);
10637 : :
10638 : : /* Initialize the vect stmts of NODE to properly insert the generated
10639 : : stmts later. */
10640 : 123071 : if (! analyze_only)
10641 : 56825 : for (unsigned i = SLP_TREE_VEC_DEFS (node).length (); i < nstmts; i++)
10642 : 22263 : SLP_TREE_VEC_DEFS (node).quick_push (NULL_TREE);
10643 : :
10644 : : /* Generate permutation masks for every NODE. Number of masks for each NODE
10645 : : is equal to GROUP_SIZE.
10646 : : E.g., we have a group of three nodes with three loads from the same
10647 : : location in each node, and the vector size is 4. I.e., we have a
10648 : : a0b0c0a1b1c1... sequence and we need to create the following vectors:
10649 : : for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
10650 : : for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
10651 : : ...
10652 : :
10653 : : The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
10654 : : The last mask is illegal since we assume two operands for permute
10655 : : operation, and the mask element values can't be outside that range.
10656 : : Hence, the last mask must be converted into {2,5,5,5}.
10657 : : For the first two permutations we need the first and the second input
10658 : : vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
10659 : : we need the second and the third vectors: {b1,c1,a2,b2} and
10660 : : {c2,a3,b3,c3}. */
10661 : :
10662 : 123071 : int vect_stmts_counter = 0;
10663 : 123071 : unsigned int index = 0;
10664 : 123071 : int first_vec_index = -1;
10665 : 123071 : int second_vec_index = -1;
10666 : 123071 : bool noop_p = true;
10667 : 123071 : *n_perms = 0;
10668 : :
10669 : 123071 : vec_perm_builder mask;
10670 : 123071 : unsigned int nelts_to_build;
10671 : 123071 : unsigned int nvectors_per_build;
10672 : 123071 : unsigned int in_nlanes;
10673 : 123071 : bool repeating_p = (group_size == dr_group_size
10674 : 164115 : && multiple_p (nunits, group_size));
10675 : 123071 : if (repeating_p)
10676 : : {
10677 : : /* A single vector contains a whole number of copies of the node, so:
10678 : : (a) all permutes can use the same mask; and
10679 : : (b) the permutes only need a single vector input. */
10680 : 34242 : mask.new_vector (nunits, group_size, 3);
10681 : 34242 : nelts_to_build = mask.encoded_nelts ();
10682 : : /* It's possible to obtain zero nstmts during analyze_only, so make
10683 : : it at least one to ensure the later computation for n_perms
10684 : : proceed. */
10685 : 34242 : nvectors_per_build = nstmts > 0 ? nstmts : 1;
10686 : 34242 : in_nlanes = dr_group_size * 3;
10687 : : }
10688 : : else
10689 : : {
10690 : : /* We need to construct a separate mask for each vector statement. */
10691 : 88829 : unsigned HOST_WIDE_INT const_nunits, const_vf;
10692 : 88829 : if (!nunits.is_constant (&const_nunits)
10693 : 88829 : || !vf.is_constant (&const_vf))
10694 : : return false;
10695 : 88829 : mask.new_vector (const_nunits, const_nunits, 1);
10696 : 88829 : nelts_to_build = const_vf * group_size;
10697 : 88829 : nvectors_per_build = 1;
10698 : 88829 : in_nlanes = const_vf * dr_group_size;
10699 : : }
10700 : 123071 : auto_sbitmap used_in_lanes (in_nlanes);
10701 : 123071 : bitmap_clear (used_in_lanes);
10702 : 123071 : auto_bitmap used_defs;
10703 : :
10704 : 123071 : unsigned int count = mask.encoded_nelts ();
10705 : 123071 : mask.quick_grow (count);
10706 : 123071 : vec_perm_indices indices;
10707 : :
10708 : 670354 : for (unsigned int j = 0; j < nelts_to_build; j++)
10709 : : {
10710 : 557650 : unsigned int iter_num = j / group_size;
10711 : 557650 : unsigned int stmt_num = j % group_size;
10712 : 557650 : unsigned int i = (iter_num * dr_group_size + perm[stmt_num]);
10713 : 557650 : bitmap_set_bit (used_in_lanes, i);
10714 : 557650 : if (repeating_p)
10715 : : {
10716 : : first_vec_index = 0;
10717 : : mask_element = i;
10718 : : }
10719 : : else
10720 : : {
10721 : : /* Enforced before the loop when !repeating_p. */
10722 : 336868 : unsigned int const_nunits = nunits.to_constant ();
10723 : 336868 : vec_index = i / const_nunits;
10724 : 336868 : mask_element = i % const_nunits;
10725 : 336868 : if (vec_index == first_vec_index
10726 : 336868 : || first_vec_index == -1)
10727 : : {
10728 : : first_vec_index = vec_index;
10729 : : }
10730 : 128526 : else if (vec_index == second_vec_index
10731 : 128526 : || second_vec_index == -1)
10732 : : {
10733 : 122204 : second_vec_index = vec_index;
10734 : 122204 : mask_element += const_nunits;
10735 : : }
10736 : : else
10737 : : {
10738 : 6322 : if (dump_p)
10739 : 229 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10740 : : "permutation requires at "
10741 : : "least three vectors %G",
10742 : : stmt_info->stmt);
10743 : 6322 : gcc_assert (analyze_only);
10744 : : return false;
10745 : : }
10746 : :
10747 : 330546 : gcc_assert (mask_element < 2 * const_nunits);
10748 : : }
10749 : :
10750 : 551328 : if (mask_element != index)
10751 : 353981 : noop_p = false;
10752 : 551328 : mask[index++] = mask_element;
10753 : :
10754 : 551328 : if (index == count)
10755 : : {
10756 : 147355 : if (!noop_p)
10757 : : {
10758 : 194645 : indices.new_vector (mask, second_vec_index == -1 ? 1 : 2, nunits);
10759 : 118289 : if (!can_vec_perm_const_p (mode, mode, indices))
10760 : : {
10761 : 4045 : if (dump_p)
10762 : : {
10763 : 75 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10764 : : "unsupported vect permute { ");
10765 : 625 : for (i = 0; i < count; ++i)
10766 : : {
10767 : 550 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
10768 : 550 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
10769 : : }
10770 : 75 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
10771 : : }
10772 : 4045 : gcc_assert (analyze_only);
10773 : : return false;
10774 : : }
10775 : :
10776 : 114244 : tree mask_vec = NULL_TREE;
10777 : 114244 : if (!analyze_only)
10778 : 19645 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
10779 : :
10780 : 114244 : if (second_vec_index == -1)
10781 : 39534 : second_vec_index = first_vec_index;
10782 : :
10783 : 232823 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
10784 : : {
10785 : 118579 : ++*n_perms;
10786 : 118579 : if (analyze_only)
10787 : 97499 : continue;
10788 : : /* Generate the permute statement if necessary. */
10789 : 21080 : tree first_vec = dr_chain[first_vec_index + ri];
10790 : 21080 : tree second_vec = dr_chain[second_vec_index + ri];
10791 : 21080 : gassign *stmt = as_a<gassign *> (stmt_info->stmt);
10792 : 21080 : tree perm_dest
10793 : 21080 : = vect_create_destination_var (gimple_assign_lhs (stmt),
10794 : : vectype);
10795 : 21080 : perm_dest = make_ssa_name (perm_dest);
10796 : 21080 : gimple *perm_stmt
10797 : 21080 : = gimple_build_assign (perm_dest, VEC_PERM_EXPR, first_vec,
10798 : : second_vec, mask_vec);
10799 : 21080 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
10800 : : gsi);
10801 : 21080 : if (dce_chain)
10802 : : {
10803 : 20183 : bitmap_set_bit (used_defs, first_vec_index + ri);
10804 : 20183 : bitmap_set_bit (used_defs, second_vec_index + ri);
10805 : : }
10806 : :
10807 : : /* Store the vector statement in NODE. */
10808 : 21080 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = perm_dest;
10809 : : }
10810 : : }
10811 : 29066 : else if (!analyze_only)
10812 : : {
10813 : 2366 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
10814 : : {
10815 : 1183 : tree first_vec = dr_chain[first_vec_index + ri];
10816 : : /* If mask was NULL_TREE generate the requested
10817 : : identity transform. */
10818 : 1183 : if (dce_chain)
10819 : 1182 : bitmap_set_bit (used_defs, first_vec_index + ri);
10820 : :
10821 : : /* Store the vector statement in NODE. */
10822 : 1183 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = first_vec;
10823 : : }
10824 : : }
10825 : :
10826 : : index = 0;
10827 : : first_vec_index = -1;
10828 : : second_vec_index = -1;
10829 : : noop_p = true;
10830 : : }
10831 : : }
10832 : :
10833 : 112704 : if (n_loads)
10834 : : {
10835 : 0 : if (repeating_p)
10836 : 0 : *n_loads = nstmts;
10837 : : else
10838 : : {
10839 : : /* Enforced above when !repeating_p. */
10840 : 0 : unsigned int const_nunits = nunits.to_constant ();
10841 : 0 : *n_loads = 0;
10842 : 0 : bool load_seen = false;
10843 : 0 : for (unsigned i = 0; i < in_nlanes; ++i)
10844 : : {
10845 : 0 : if (i % const_nunits == 0)
10846 : : {
10847 : 0 : if (load_seen)
10848 : 0 : *n_loads += 1;
10849 : : load_seen = false;
10850 : : }
10851 : 0 : if (bitmap_bit_p (used_in_lanes, i))
10852 : 0 : load_seen = true;
10853 : : }
10854 : 0 : if (load_seen)
10855 : 0 : *n_loads += 1;
10856 : : }
10857 : : }
10858 : :
10859 : 112704 : if (dce_chain)
10860 : 173889 : for (unsigned i = 0; i < dr_chain.length (); ++i)
10861 : 34224 : if (!bitmap_bit_p (used_defs, i))
10862 : : {
10863 : 2777 : tree def = dr_chain[i];
10864 : 2868 : do
10865 : : {
10866 : 2868 : gimple *stmt = SSA_NAME_DEF_STMT (def);
10867 : 2868 : if (is_gimple_assign (stmt)
10868 : 2868 : && (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10869 : 2868 : || gimple_assign_rhs_code (stmt) == CONSTRUCTOR))
10870 : 529 : def = single_ssa_tree_operand (stmt, SSA_OP_USE);
10871 : : else
10872 : : def = NULL;
10873 : 2868 : gimple_stmt_iterator rgsi = gsi_for_stmt (stmt);
10874 : 2868 : gsi_remove (&rgsi, true);
10875 : 2868 : release_defs (stmt);
10876 : : }
10877 : 2868 : while (def);
10878 : : }
10879 : :
10880 : : return true;
10881 : 123071 : }
10882 : :
10883 : : /* Generate vector permute statements from a list of loads in DR_CHAIN.
10884 : : If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
10885 : : permute statements for the SLP node NODE. Store the number of vector
10886 : : permute instructions in *N_PERMS and the number of vector load
10887 : : instructions in *N_LOADS. If DCE_CHAIN is true, remove all definitions
10888 : : that were not needed. */
10889 : :
10890 : : bool
10891 : 77896 : vect_transform_slp_perm_load (vec_info *vinfo,
10892 : : slp_tree node, const vec<tree> &dr_chain,
10893 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
10894 : : bool analyze_only, unsigned *n_perms,
10895 : : unsigned int *n_loads, bool dce_chain)
10896 : : {
10897 : 77896 : return vect_transform_slp_perm_load_1 (vinfo, node,
10898 : 77896 : SLP_TREE_LOAD_PERMUTATION (node),
10899 : : dr_chain, gsi, vf, analyze_only,
10900 : : dump_enabled_p (), n_perms, n_loads,
10901 : 77896 : dce_chain);
10902 : : }
10903 : :
10904 : : /* Produce the next vector result for SLP permutation NODE by adding a vector
10905 : : statement at GSI. If MASK_VEC is nonnull, add:
10906 : :
10907 : : <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
10908 : :
10909 : : otherwise add:
10910 : :
10911 : : <new SSA name> = FIRST_DEF. */
10912 : :
10913 : : static void
10914 : 30367 : vect_add_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
10915 : : slp_tree node, tree first_def, tree second_def,
10916 : : tree mask_vec, poly_uint64 identity_offset)
10917 : : {
10918 : 30367 : tree vectype = SLP_TREE_VECTYPE (node);
10919 : :
10920 : : /* ??? We SLP match existing vector element extracts but
10921 : : allow punning which we need to re-instantiate at uses
10922 : : but have no good way of explicitly representing. */
10923 : 30367 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)), TYPE_SIZE (vectype))
10924 : 30367 : && !types_compatible_p (TREE_TYPE (first_def), vectype))
10925 : : {
10926 : 13 : gassign *conv_stmt
10927 : 13 : = gimple_build_assign (make_ssa_name (vectype),
10928 : : build1 (VIEW_CONVERT_EXPR, vectype, first_def));
10929 : 13 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
10930 : 13 : first_def = gimple_assign_lhs (conv_stmt);
10931 : : }
10932 : 30367 : gassign *perm_stmt;
10933 : 30367 : tree perm_dest = make_ssa_name (vectype);
10934 : 30367 : if (mask_vec)
10935 : : {
10936 : 27367 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)),
10937 : 27367 : TYPE_SIZE (vectype))
10938 : 27367 : && !types_compatible_p (TREE_TYPE (second_def), vectype))
10939 : : {
10940 : 8 : gassign *conv_stmt
10941 : 8 : = gimple_build_assign (make_ssa_name (vectype),
10942 : : build1 (VIEW_CONVERT_EXPR,
10943 : : vectype, second_def));
10944 : 8 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
10945 : 8 : second_def = gimple_assign_lhs (conv_stmt);
10946 : : }
10947 : 27367 : perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
10948 : : first_def, second_def,
10949 : : mask_vec);
10950 : : }
10951 : 3000 : else if (!types_compatible_p (TREE_TYPE (first_def), vectype))
10952 : : {
10953 : : /* For identity permutes we still need to handle the case
10954 : : of offsetted extracts or concats. */
10955 : 201 : unsigned HOST_WIDE_INT c;
10956 : 201 : auto first_def_nunits
10957 : 201 : = TYPE_VECTOR_SUBPARTS (TREE_TYPE (first_def));
10958 : 201 : if (known_le (TYPE_VECTOR_SUBPARTS (vectype), first_def_nunits))
10959 : : {
10960 : 197 : unsigned HOST_WIDE_INT elsz
10961 : 197 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (first_def))));
10962 : 394 : tree lowpart = build3 (BIT_FIELD_REF, vectype, first_def,
10963 : 197 : TYPE_SIZE (vectype),
10964 : 197 : bitsize_int (identity_offset * elsz));
10965 : 197 : perm_stmt = gimple_build_assign (perm_dest, lowpart);
10966 : : }
10967 : 4 : else if (constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
10968 : 4 : first_def_nunits, &c) && c == 2)
10969 : : {
10970 : 4 : tree ctor = build_constructor_va (vectype, 2, NULL_TREE, first_def,
10971 : : NULL_TREE, second_def);
10972 : 4 : perm_stmt = gimple_build_assign (perm_dest, ctor);
10973 : : }
10974 : : else
10975 : 0 : gcc_unreachable ();
10976 : : }
10977 : : else
10978 : : {
10979 : : /* We need a copy here in case the def was external. */
10980 : 2799 : perm_stmt = gimple_build_assign (perm_dest, first_def);
10981 : : }
10982 : 30367 : vect_finish_stmt_generation (vinfo, NULL, perm_stmt, gsi);
10983 : : /* Store the vector statement in NODE. */
10984 : 30367 : node->push_vec_def (perm_stmt);
10985 : 30367 : }
10986 : :
10987 : : /* Subroutine of vectorizable_slp_permutation. Check whether the target
10988 : : can perform permutation PERM on the (1 or 2) input nodes in CHILDREN.
10989 : : If GSI is nonnull, emit the permutation there.
10990 : :
10991 : : When GSI is null, the only purpose of NODE is to give properties
10992 : : of the result, such as the vector type and number of SLP lanes.
10993 : : The node does not need to be a VEC_PERM_EXPR.
10994 : :
10995 : : If the target supports the operation, return the number of individual
10996 : : VEC_PERM_EXPRs needed, otherwise return -1. Print information to the
10997 : : dump file if DUMP_P is true. */
10998 : :
10999 : : static int
11000 : 429328 : vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
11001 : : slp_tree node, lane_permutation_t &perm,
11002 : : vec<slp_tree> &children, bool dump_p)
11003 : : {
11004 : 429328 : tree vectype = SLP_TREE_VECTYPE (node);
11005 : :
11006 : : /* ??? We currently only support all same vector input types
11007 : : while the SLP IL should really do a concat + select and thus accept
11008 : : arbitrary mismatches. */
11009 : 429328 : slp_tree child;
11010 : 429328 : unsigned i;
11011 : 429328 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11012 : 429328 : bool repeating_p = multiple_p (nunits, SLP_TREE_LANES (node));
11013 : : /* True if we're permuting a single input of 2N vectors down
11014 : : to N vectors. This case doesn't generalize beyond 2 since
11015 : : VEC_PERM_EXPR only takes 2 inputs. */
11016 : 429328 : bool pack_p = false;
11017 : : /* If we're permuting inputs of N vectors each into X*N outputs,
11018 : : this is the value of X, otherwise it is 1. */
11019 : 429328 : unsigned int unpack_factor = 1;
11020 : 429328 : tree op_vectype = NULL_TREE;
11021 : 430529 : FOR_EACH_VEC_ELT (children, i, child)
11022 : 430450 : if (SLP_TREE_VECTYPE (child))
11023 : : {
11024 : : op_vectype = SLP_TREE_VECTYPE (child);
11025 : : break;
11026 : : }
11027 : 429328 : if (!op_vectype)
11028 : 79 : op_vectype = vectype;
11029 : 914588 : FOR_EACH_VEC_ELT (children, i, child)
11030 : : {
11031 : 485260 : if ((SLP_TREE_DEF_TYPE (child) != vect_internal_def
11032 : 9512 : && !vect_maybe_update_slp_op_vectype (child, op_vectype))
11033 : 485260 : || !types_compatible_p (SLP_TREE_VECTYPE (child), op_vectype)
11034 : 970520 : || !types_compatible_p (TREE_TYPE (vectype), TREE_TYPE (op_vectype)))
11035 : : {
11036 : 0 : if (dump_p)
11037 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11038 : : "Unsupported vector types in lane permutation\n");
11039 : 0 : return -1;
11040 : : }
11041 : 485260 : auto op_nunits = TYPE_VECTOR_SUBPARTS (op_vectype);
11042 : 485260 : unsigned int this_unpack_factor;
11043 : : /* Detect permutations of external, pre-existing vectors. The external
11044 : : node's SLP_TREE_LANES stores the total number of units in the vector,
11045 : : or zero if the vector has variable length.
11046 : :
11047 : : We are expected to keep the original VEC_PERM_EXPR for such cases.
11048 : : There is no repetition to model. */
11049 : 485260 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def
11050 : 485260 : && SLP_TREE_SCALAR_OPS (child).is_empty ())
11051 : : repeating_p = false;
11052 : : /* Check whether the input has twice as many lanes per vector. */
11053 : 477885 : else if (children.length () == 1
11054 : 477885 : && known_eq (SLP_TREE_LANES (child) * nunits,
11055 : : SLP_TREE_LANES (node) * op_nunits * 2))
11056 : : pack_p = true;
11057 : : /* Check whether the output has N times as many lanes per vector. */
11058 : 485260 : else if (constant_multiple_p (SLP_TREE_LANES (node) * op_nunits,
11059 : 435825 : SLP_TREE_LANES (child) * nunits,
11060 : : &this_unpack_factor)
11061 : 401135 : && (i == 0 || unpack_factor == this_unpack_factor))
11062 : : unpack_factor = this_unpack_factor;
11063 : : else
11064 : : repeating_p = false;
11065 : : }
11066 : :
11067 : 858656 : gcc_assert (perm.length () == SLP_TREE_LANES (node));
11068 : :
11069 : : /* Load-lanes permute. This permute only acts as a forwarder to
11070 : : select the correct vector def of the load-lanes load which
11071 : : has the permuted vectors in its vector defs like
11072 : : { v0, w0, r0, v1, w1, r1 ... } for a ld3. All costs are
11073 : : accounted for in the costing for the actual load so we
11074 : : return zero here. */
11075 : 429328 : if (node->ldst_lanes)
11076 : : {
11077 : 0 : gcc_assert (children.length () == 1);
11078 : 0 : if (!gsi)
11079 : : /* This is a trivial op always supported. */
11080 : : return 0;
11081 : 0 : slp_tree child = children[0];
11082 : 0 : unsigned vec_idx = (SLP_TREE_LANE_PERMUTATION (node)[0].second
11083 : 0 : / SLP_TREE_LANES (node));
11084 : 0 : unsigned vec_num = SLP_TREE_LANES (child) / SLP_TREE_LANES (node);
11085 : 0 : unsigned nvectors = vect_get_num_copies (vinfo, node);
11086 : 0 : for (unsigned i = 0; i < nvectors; ++i)
11087 : : {
11088 : 0 : tree def = SLP_TREE_VEC_DEFS (child)[i * vec_num + vec_idx];
11089 : 0 : node->push_vec_def (def);
11090 : : }
11091 : : return 0;
11092 : : }
11093 : :
11094 : : /* Set REPEATING_P to true if the permutations are cylical wrt UNPACK_FACTOR
11095 : : and if we can generate the vectors in a vector-length agnostic way.
11096 : : This requires UNPACK_STEP == NUNITS / UNPACK_FACTOR to be known at
11097 : : compile time.
11098 : :
11099 : : The significance of UNPACK_STEP is that, when PACK_P is false,
11100 : : output vector I operates on a window of UNPACK_STEP elements from each
11101 : : input, starting at lane UNPACK_STEP * (I % UNPACK_FACTOR). For example,
11102 : : when UNPACK_FACTOR is 2, the first output vector operates on lanes
11103 : : [0, NUNITS / 2 - 1] of each input vector and the second output vector
11104 : : operates on lanes [NUNITS / 2, NUNITS - 1] of each input vector.
11105 : :
11106 : : When REPEATING_P is true, NOUTPUTS holds the total number of outputs
11107 : : that we actually need to generate. */
11108 : 429328 : uint64_t noutputs = 0;
11109 : 429328 : poly_uint64 unpack_step = 0;
11110 : 429328 : loop_vec_info linfo = dyn_cast <loop_vec_info> (vinfo);
11111 : 133393 : if (!linfo
11112 : 467560 : || !multiple_p (nunits, unpack_factor, &unpack_step)
11113 : 132508 : || !constant_multiple_p (LOOP_VINFO_VECT_FACTOR (linfo)
11114 : 132508 : * SLP_TREE_LANES (node), nunits, &noutputs))
11115 : : repeating_p = false;
11116 : :
11117 : : /* We can handle the conditions described for REPEATING_P above for
11118 : : both variable- and constant-length vectors. The fallback requires
11119 : : us to generate every element of every permute vector explicitly,
11120 : : which is only possible for constant-length permute vectors.
11121 : :
11122 : : Set:
11123 : :
11124 : : - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
11125 : : mask vectors that we want to build.
11126 : :
11127 : : - NCOPIES to the number of copies of PERM that we need in order
11128 : : to build the necessary permute mask vectors. */
11129 : 132508 : uint64_t npatterns;
11130 : 132508 : unsigned nelts_per_pattern;
11131 : 132508 : uint64_t ncopies;
11132 : 132508 : if (repeating_p)
11133 : : {
11134 : : /* We need permute mask vectors that have the form:
11135 : :
11136 : : { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
11137 : :
11138 : : In other words, the original n-element permute in PERM is
11139 : : "unrolled" to fill a full vector. The stepped vector encoding
11140 : : that we use for permutes requires 3n elements. */
11141 : 94276 : npatterns = SLP_TREE_LANES (node);
11142 : 94276 : nelts_per_pattern = ncopies = 3;
11143 : : }
11144 : : else
11145 : : {
11146 : : /* Calculate every element of every permute mask vector explicitly,
11147 : : instead of relying on the pattern described above. */
11148 : 335052 : if (!nunits.is_constant (&npatterns)
11149 : 335052 : || !TYPE_VECTOR_SUBPARTS (op_vectype).is_constant ())
11150 : : {
11151 : : if (dump_p)
11152 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11153 : : "unsupported permutation %p on variable-length"
11154 : : " vectors\n", (void *) node);
11155 : : return -1;
11156 : : }
11157 : 335052 : nelts_per_pattern = ncopies = 1;
11158 : 335052 : if (linfo && !LOOP_VINFO_VECT_FACTOR (linfo).is_constant (&ncopies))
11159 : : {
11160 : : if (dump_p)
11161 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11162 : : "unsupported permutation %p for variable VF\n",
11163 : : (void *) node);
11164 : : return -1;
11165 : : }
11166 : : pack_p = false;
11167 : : unpack_factor = 1;
11168 : : }
11169 : 429328 : unsigned olanes = unpack_factor * ncopies * SLP_TREE_LANES (node);
11170 : 429328 : gcc_assert (repeating_p || multiple_p (olanes, nunits));
11171 : :
11172 : : /* Compute the { { SLP operand, vector index}, lane } permutation sequence
11173 : : from the { SLP operand, scalar lane } permutation as recorded in the
11174 : : SLP node as intermediate step. This part should already work
11175 : : with SLP children with arbitrary number of lanes. */
11176 : 429328 : auto_vec<std::pair<std::pair<unsigned, unsigned>, poly_uint64>> vperm;
11177 : 429328 : auto_vec<poly_uint64> active_lane;
11178 : 429328 : vperm.create (olanes);
11179 : 429328 : active_lane.safe_grow_cleared (children.length (), true);
11180 : 865555 : for (unsigned int ui = 0; ui < unpack_factor; ++ui)
11181 : : {
11182 : 1870522 : for (unsigned j = 0; j < children.length (); ++j)
11183 : 499034 : active_lane[j] = ui * unpack_step;
11184 : 1181465 : for (unsigned i = 0; i < ncopies; ++i)
11185 : : {
11186 : 4646000 : for (unsigned pi = 0; pi < perm.length (); ++pi)
11187 : : {
11188 : 1577762 : std::pair<unsigned, unsigned> p = perm[pi];
11189 : 1577762 : tree vtype = SLP_TREE_VECTYPE (children[p.first]);
11190 : 1577762 : if (repeating_p)
11191 : 534846 : vperm.quick_push ({{p.first, 0},
11192 : 534846 : p.second + active_lane[p.first]});
11193 : : else
11194 : : {
11195 : : /* We checked above that the vectors are constant-length. */
11196 : 1042916 : unsigned vnunits = TYPE_VECTOR_SUBPARTS (vtype)
11197 : 1042916 : .to_constant ();
11198 : 1042916 : unsigned lane = active_lane[p.first].to_constant ();
11199 : 1042916 : unsigned vi = (lane + p.second) / vnunits;
11200 : 1042916 : unsigned vl = (lane + p.second) % vnunits;
11201 : 1042916 : vperm.quick_push ({{p.first, vi}, vl});
11202 : : }
11203 : : }
11204 : : /* Advance to the next group. */
11205 : 1598534 : for (unsigned j = 0; j < children.length (); ++j)
11206 : 853296 : active_lane[j] += SLP_TREE_LANES (children[j]);
11207 : : }
11208 : : }
11209 : :
11210 : 429328 : if (dump_p)
11211 : : {
11212 : 8159 : dump_printf_loc (MSG_NOTE, vect_location,
11213 : : "vectorizing permutation %p", (void *)node);
11214 : 29518 : for (unsigned i = 0; i < perm.length (); ++i)
11215 : 21359 : dump_printf (MSG_NOTE, " op%u[%u]", perm[i].first, perm[i].second);
11216 : 8159 : if (repeating_p)
11217 : 6927 : dump_printf (MSG_NOTE, " (repeat %d)", SLP_TREE_LANES (node));
11218 : 8159 : dump_printf (MSG_NOTE, "\n");
11219 : 8159 : dump_printf_loc (MSG_NOTE, vect_location, "as");
11220 : 82446 : for (unsigned i = 0; i < vperm.length (); ++i)
11221 : : {
11222 : 74287 : if (i != 0
11223 : 74287 : && (repeating_p
11224 : 51390 : ? multiple_p (i, npatterns)
11225 : 54922 : : multiple_p (i, TYPE_VECTOR_SUBPARTS (vectype))))
11226 : 22381 : dump_printf (MSG_NOTE, ",");
11227 : 74287 : dump_printf (MSG_NOTE, " vops%u[%u][",
11228 : 74287 : vperm[i].first.first, vperm[i].first.second);
11229 : 74287 : dump_dec (MSG_NOTE, vperm[i].second);
11230 : 74287 : dump_printf (MSG_NOTE, "]");
11231 : : }
11232 : 8159 : dump_printf (MSG_NOTE, "\n");
11233 : : }
11234 : :
11235 : : /* We can only handle two-vector permutes, everything else should
11236 : : be lowered on the SLP level. The following is closely inspired
11237 : : by vect_transform_slp_perm_load and is supposed to eventually
11238 : : replace it.
11239 : : ??? As intermediate step do code-gen in the SLP tree representation
11240 : : somehow? */
11241 : 429328 : std::pair<unsigned, unsigned> first_vec = std::make_pair (-1U, -1U);
11242 : 429328 : std::pair<unsigned, unsigned> second_vec = std::make_pair (-1U, -1U);
11243 : 429328 : unsigned int index = 0;
11244 : 429328 : poly_uint64 mask_element;
11245 : 429328 : vec_perm_builder mask;
11246 : 429328 : mask.new_vector (nunits, npatterns, nelts_per_pattern);
11247 : 429328 : unsigned int count = mask.encoded_nelts ();
11248 : 429328 : mask.quick_grow (count);
11249 : 429328 : vec_perm_indices indices;
11250 : 429328 : unsigned nperms = 0;
11251 : : /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
11252 : : vectors to check during analysis, but we need to generate NOUTPUTS
11253 : : vectors during transformation. */
11254 : 429328 : unsigned total_nelts = olanes;
11255 : 429328 : unsigned process_nelts = olanes;
11256 : 429328 : if (repeating_p)
11257 : : {
11258 : 94276 : total_nelts = (total_nelts / unpack_factor) * noutputs;
11259 : 94276 : if (gsi)
11260 : 9562 : process_nelts = total_nelts;
11261 : : }
11262 : 429328 : unsigned last_ei = (total_nelts - 1) % process_nelts;
11263 : 2015331 : for (unsigned i = 0; i < process_nelts; ++i)
11264 : : {
11265 : : /* VI is the input vector index when generating code for REPEATING_P. */
11266 : 1593770 : unsigned vi = i / olanes * (pack_p ? 2 : 1);
11267 : 1593770 : unsigned ei = i % olanes;
11268 : 1593770 : mask_element = vperm[ei].second;
11269 : 1593770 : if (pack_p)
11270 : : {
11271 : : /* In this case, we have N outputs and the single child provides 2N
11272 : : inputs. Output X permutes inputs 2X and 2X+1.
11273 : :
11274 : : The mask indices are taken directly from the SLP permutation node.
11275 : : Index X selects from the first vector if (X / NUNITS) % 2 == 0;
11276 : : X selects from the second vector otherwise. These conditions
11277 : : are only known at compile time for constant-length vectors. */
11278 : : first_vec = std::make_pair (0, 0);
11279 : : second_vec = std::make_pair (0, 1);
11280 : : }
11281 : 1434485 : else if (first_vec.first == -1U
11282 : 1434485 : || first_vec == vperm[ei].first)
11283 : 1253639 : first_vec = vperm[ei].first;
11284 : 180846 : else if (second_vec.first == -1U
11285 : 180846 : || second_vec == vperm[ei].first)
11286 : : {
11287 : 180460 : second_vec = vperm[ei].first;
11288 : 180460 : mask_element += nunits;
11289 : : }
11290 : : else
11291 : : {
11292 : 386 : if (dump_p)
11293 : 17 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11294 : : "permutation requires at "
11295 : : "least three vectors\n");
11296 : 386 : gcc_assert (!gsi);
11297 : : return -1;
11298 : : }
11299 : :
11300 : 1593384 : mask[index++] = mask_element;
11301 : :
11302 : 1593384 : if (index == count)
11303 : : {
11304 : 708979 : indices.new_vector (mask, second_vec.first == -1U ? 1 : 2,
11305 : : TYPE_VECTOR_SUBPARTS (op_vectype));
11306 : 569656 : bool identity_p = (indices.series_p (0, 1, mask[0], 1)
11307 : 883976 : && constant_multiple_p (mask[0], nunits));
11308 : 569656 : machine_mode vmode = TYPE_MODE (vectype);
11309 : 569656 : machine_mode op_vmode = TYPE_MODE (op_vectype);
11310 : 569656 : unsigned HOST_WIDE_INT c;
11311 : 569656 : if ((!identity_p
11312 : 529393 : && !can_vec_perm_const_p (vmode, op_vmode, indices))
11313 : 569656 : || (identity_p
11314 : 40263 : && !known_le (nunits,
11315 : : TYPE_VECTOR_SUBPARTS (op_vectype))
11316 : 7389 : && (!constant_multiple_p (nunits,
11317 : 8 : TYPE_VECTOR_SUBPARTS (op_vectype),
11318 : 8 : &c) || c != 2)))
11319 : : {
11320 : 7381 : if (dump_p)
11321 : : {
11322 : 152 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
11323 : : vect_location,
11324 : : "unsupported vect permute { ");
11325 : 1586 : for (i = 0; i < count; ++i)
11326 : : {
11327 : 1434 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
11328 : 1434 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
11329 : : }
11330 : 152 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
11331 : : }
11332 : 7381 : gcc_assert (!gsi);
11333 : 7767 : return -1;
11334 : : }
11335 : :
11336 : 562275 : if (!identity_p)
11337 : 522012 : nperms += CEIL (total_nelts, process_nelts) - (ei > last_ei);
11338 : 562275 : if (gsi)
11339 : : {
11340 : 30367 : if (second_vec.first == -1U)
11341 : 6760 : second_vec = first_vec;
11342 : :
11343 : 30367 : slp_tree
11344 : 30367 : first_node = children[first_vec.first],
11345 : 30367 : second_node = children[second_vec.first];
11346 : :
11347 : 30367 : tree mask_vec = NULL_TREE;
11348 : 30367 : if (!identity_p)
11349 : 27367 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
11350 : :
11351 : 30367 : tree first_def
11352 : 30367 : = vect_get_slp_vect_def (first_node, first_vec.second + vi);
11353 : 30367 : tree second_def
11354 : 30367 : = vect_get_slp_vect_def (second_node, second_vec.second + vi);
11355 : 30367 : vect_add_slp_permutation (vinfo, gsi, node, first_def,
11356 : 30367 : second_def, mask_vec, mask[0]);
11357 : : }
11358 : :
11359 : : index = 0;
11360 : : first_vec = std::make_pair (-1U, -1U);
11361 : : second_vec = std::make_pair (-1U, -1U);
11362 : : }
11363 : : }
11364 : :
11365 : 421561 : return nperms;
11366 : 429328 : }
11367 : :
11368 : : /* Vectorize the SLP permutations in NODE as specified
11369 : : in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
11370 : : child number and lane number.
11371 : : Interleaving of two two-lane two-child SLP subtrees (not supported):
11372 : : [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
11373 : : A blend of two four-lane two-child SLP subtrees:
11374 : : [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
11375 : : Highpart of a four-lane one-child SLP subtree (not supported):
11376 : : [ { 0, 2 }, { 0, 3 } ]
11377 : : Where currently only a subset is supported by code generating below. */
11378 : :
11379 : : bool
11380 : 114939 : vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
11381 : : slp_tree node, stmt_vector_for_cost *cost_vec)
11382 : : {
11383 : 114939 : tree vectype = SLP_TREE_VECTYPE (node);
11384 : 114939 : lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
11385 : 114939 : int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
11386 : 114939 : SLP_TREE_CHILDREN (node),
11387 : : dump_enabled_p ());
11388 : 114939 : if (nperms < 0)
11389 : : return false;
11390 : :
11391 : 113731 : if (!gsi && nperms != 0)
11392 : 92823 : record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
11393 : :
11394 : : return true;
11395 : : }
11396 : :
11397 : : /* Vectorize SLP NODE. */
11398 : :
11399 : : static void
11400 : 1459846 : vect_schedule_slp_node (vec_info *vinfo,
11401 : : slp_tree node, slp_instance instance)
11402 : : {
11403 : 1459846 : gimple_stmt_iterator si;
11404 : 1459846 : int i;
11405 : 1459846 : slp_tree child;
11406 : :
11407 : : /* Vectorize externals and constants. */
11408 : 1459846 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
11409 : 1459846 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
11410 : : {
11411 : : /* ??? vectorizable_shift can end up using a scalar operand which is
11412 : : currently denoted as !SLP_TREE_VECTYPE. No need to vectorize the
11413 : : node in this case. */
11414 : 498872 : if (!SLP_TREE_VECTYPE (node))
11415 : 498872 : return;
11416 : :
11417 : : /* There are two reasons vector defs might already exist. The first
11418 : : is that we are vectorizing an existing vector def. The second is
11419 : : when performing BB vectorization shared constant/external nodes
11420 : : are not split apart during partitioning so during the code-gen
11421 : : DFS walk we can end up visiting them twice. */
11422 : 492684 : if (! SLP_TREE_VEC_DEFS (node).exists ())
11423 : 491998 : vect_create_constant_vectors (vinfo, node);
11424 : 492684 : return;
11425 : : }
11426 : :
11427 : 960974 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
11428 : :
11429 : 960974 : gcc_assert (SLP_TREE_VEC_DEFS (node).is_empty ());
11430 : 960974 : if (SLP_TREE_VECTYPE (node))
11431 : 960968 : SLP_TREE_VEC_DEFS (node).create (vect_get_num_copies (vinfo, node));
11432 : :
11433 : 960974 : if (!SLP_TREE_PERMUTE_P (node) && STMT_VINFO_DATA_REF (stmt_info))
11434 : : {
11435 : : /* Vectorized loads go before the first scalar load to make it
11436 : : ready early, vectorized stores go before the last scalar
11437 : : stmt which is where all uses are ready. */
11438 : 707434 : stmt_vec_info last_stmt_info = NULL;
11439 : 707434 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
11440 : 163631 : last_stmt_info = vect_find_first_scalar_stmt_in_slp (node);
11441 : : else /* DR_IS_WRITE */
11442 : 543803 : last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
11443 : 707434 : si = gsi_for_stmt (last_stmt_info->stmt);
11444 : 707434 : }
11445 : 253540 : else if (!SLP_TREE_PERMUTE_P (node)
11446 : 237605 : && (SLP_TREE_TYPE (node) == cycle_phi_info_type
11447 : : || SLP_TREE_TYPE (node) == induc_vec_info_type
11448 : : || SLP_TREE_TYPE (node) == phi_info_type))
11449 : : {
11450 : : /* For PHI node vectorization we do not use the insertion iterator. */
11451 : 55426 : si = gsi_none ();
11452 : : }
11453 : : else
11454 : : {
11455 : : /* Emit other stmts after the children vectorized defs which is
11456 : : earliest possible. */
11457 : : gimple *last_stmt = NULL;
11458 : : bool seen_vector_def = false;
11459 : 551058 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11460 : 352944 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
11461 : : {
11462 : : /* For fold-left reductions we are retaining the scalar
11463 : : reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
11464 : : set so the representation isn't perfect. Resort to the
11465 : : last scalar def here. */
11466 : 280799 : if (SLP_TREE_VEC_DEFS (child).is_empty ())
11467 : : {
11468 : 849 : gcc_assert (SLP_TREE_TYPE (child) == cycle_phi_info_type);
11469 : 849 : gphi *phi = as_a <gphi *>
11470 : 849 : (vect_find_last_scalar_stmt_in_slp (child)->stmt);
11471 : 849 : if (!last_stmt)
11472 : : last_stmt = phi;
11473 : 622 : else if (vect_stmt_dominates_stmt_p (last_stmt, phi))
11474 : : last_stmt = phi;
11475 : 611 : else if (vect_stmt_dominates_stmt_p (phi, last_stmt))
11476 : : ;
11477 : : else
11478 : 0 : gcc_unreachable ();
11479 : : }
11480 : : /* We are emitting all vectorized stmts in the same place and
11481 : : the last one is the last.
11482 : : ??? Unless we have a load permutation applied and that
11483 : : figures to re-use an earlier generated load. */
11484 : : unsigned j;
11485 : : tree vdef;
11486 : 671497 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11487 : : {
11488 : 390698 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11489 : 390698 : if (!last_stmt)
11490 : : last_stmt = vstmt;
11491 : 202912 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
11492 : : last_stmt = vstmt;
11493 : 42359 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
11494 : : ;
11495 : : else
11496 : 0 : gcc_unreachable ();
11497 : : }
11498 : : }
11499 : 72145 : else if (!SLP_TREE_VECTYPE (child))
11500 : : {
11501 : : /* For externals we use unvectorized at all scalar defs. */
11502 : : unsigned j;
11503 : : tree def;
11504 : 12929 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
11505 : 7347 : if (TREE_CODE (def) == SSA_NAME
11506 : 7347 : && !SSA_NAME_IS_DEFAULT_DEF (def))
11507 : : {
11508 : 169 : gimple *stmt = SSA_NAME_DEF_STMT (def);
11509 : 169 : if (gimple_uid (stmt) == -1u)
11510 : : /* If the stmt is not inside the region do not
11511 : : use it as possible insertion point. */
11512 : : ;
11513 : 161 : else if (!last_stmt)
11514 : : last_stmt = stmt;
11515 : 155 : else if (vect_stmt_dominates_stmt_p (last_stmt, stmt))
11516 : : last_stmt = stmt;
11517 : 155 : else if (vect_stmt_dominates_stmt_p (stmt, last_stmt))
11518 : : ;
11519 : : else
11520 : 0 : gcc_unreachable ();
11521 : : }
11522 : : }
11523 : : else
11524 : : {
11525 : : /* For externals we have to look at all defs since their
11526 : : insertion place is decided per vector. But beware
11527 : : of pre-existing vectors where we need to make sure
11528 : : we do not insert before the region boundary. */
11529 : 66563 : if (SLP_TREE_SCALAR_OPS (child).is_empty ()
11530 : 549 : && !vinfo->lookup_def (SLP_TREE_VEC_DEFS (child)[0]))
11531 : : seen_vector_def = true;
11532 : : else
11533 : : {
11534 : : unsigned j;
11535 : : tree vdef;
11536 : 513332 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11537 : 93941 : if (TREE_CODE (vdef) == SSA_NAME
11538 : 93941 : && !SSA_NAME_IS_DEFAULT_DEF (vdef))
11539 : : {
11540 : 18449 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11541 : 18449 : if (!last_stmt)
11542 : : last_stmt = vstmt;
11543 : 10075 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
11544 : : last_stmt = vstmt;
11545 : 7867 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
11546 : : ;
11547 : : else
11548 : 0 : gcc_unreachable ();
11549 : : }
11550 : : }
11551 : : }
11552 : : /* This can happen when all children are pre-existing vectors or
11553 : : constants. */
11554 : 198114 : if (!last_stmt)
11555 : 1721 : last_stmt = vect_find_first_scalar_stmt_in_slp (node)->stmt;
11556 : 1721 : if (!last_stmt)
11557 : : {
11558 : 0 : gcc_assert (seen_vector_def);
11559 : 0 : si = gsi_after_labels (vinfo->bbs[0]);
11560 : : }
11561 : 198114 : else if (is_ctrl_altering_stmt (last_stmt))
11562 : : {
11563 : : /* We split regions to vectorize at control altering stmts
11564 : : with a definition so this must be an external which
11565 : : we can insert at the start of the region. */
11566 : 0 : si = gsi_after_labels (vinfo->bbs[0]);
11567 : : }
11568 : 198114 : else if (is_a <bb_vec_info> (vinfo)
11569 : 17331 : && !SLP_TREE_PERMUTE_P (node)
11570 : 16015 : && gimple_bb (last_stmt) != gimple_bb (stmt_info->stmt)
11571 : 199334 : && gimple_could_trap_p (stmt_info->stmt))
11572 : : {
11573 : : /* We've constrained possibly trapping operations to all come
11574 : : from the same basic-block, if vectorized defs would allow earlier
11575 : : scheduling still force vectorized stmts to the original block.
11576 : : This is only necessary for BB vectorization since for loop vect
11577 : : all operations are in a single BB and scalar stmt based
11578 : : placement doesn't play well with epilogue vectorization. */
11579 : 51 : gcc_assert (dominated_by_p (CDI_DOMINATORS,
11580 : : gimple_bb (stmt_info->stmt),
11581 : : gimple_bb (last_stmt)));
11582 : 51 : si = gsi_after_labels (gimple_bb (stmt_info->stmt));
11583 : : }
11584 : 198063 : else if (is_a <gphi *> (last_stmt))
11585 : 15241 : si = gsi_after_labels (gimple_bb (last_stmt));
11586 : : else
11587 : : {
11588 : 182822 : si = gsi_for_stmt (last_stmt);
11589 : 182822 : gsi_next (&si);
11590 : :
11591 : : /* Avoid scheduling internal defs outside of the loop when
11592 : : we might have only implicitly tracked loop mask/len defs. */
11593 : 182822 : if (auto loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
11594 : 61 : if (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
11595 : 165737 : || LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
11596 : : {
11597 : 61 : gimple_stmt_iterator si2
11598 : 61 : = gsi_after_labels (LOOP_VINFO_LOOP (loop_vinfo)->header);
11599 : 61 : if ((gsi_end_p (si2)
11600 : 0 : && (LOOP_VINFO_LOOP (loop_vinfo)->header
11601 : 0 : != gimple_bb (last_stmt))
11602 : 0 : && dominated_by_p (CDI_DOMINATORS,
11603 : : LOOP_VINFO_LOOP (loop_vinfo)->header,
11604 : 0 : gimple_bb (last_stmt)))
11605 : 61 : || (!gsi_end_p (si2)
11606 : 61 : && last_stmt != *si2
11607 : 60 : && vect_stmt_dominates_stmt_p (last_stmt, *si2)))
11608 : 3 : si = si2;
11609 : : }
11610 : : }
11611 : : }
11612 : :
11613 : 960974 : if (dump_enabled_p ())
11614 : : {
11615 : 69020 : if (stmt_info)
11616 : 68969 : dump_printf_loc (MSG_NOTE, vect_location,
11617 : : "------>vectorizing SLP node starting from: %G",
11618 : : stmt_info->stmt);
11619 : : else
11620 : : {
11621 : 51 : dump_printf_loc (MSG_NOTE, vect_location,
11622 : : "------>vectorizing SLP node:\n");
11623 : 51 : vect_print_slp_tree (MSG_NOTE, vect_location, node);
11624 : : }
11625 : : }
11626 : 960974 : vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
11627 : : }
11628 : :
11629 : : /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
11630 : : For loop vectorization this is done in vectorizable_call, but for SLP
11631 : : it needs to be deferred until end of vect_schedule_slp, because multiple
11632 : : SLP instances may refer to the same scalar stmt. */
11633 : :
11634 : : static void
11635 : 585212 : vect_remove_slp_scalar_calls (vec_info *vinfo,
11636 : : slp_tree node, hash_set<slp_tree> &visited)
11637 : : {
11638 : 585212 : gimple *new_stmt;
11639 : 585212 : gimple_stmt_iterator gsi;
11640 : 585212 : int i;
11641 : 585212 : slp_tree child;
11642 : 585212 : tree lhs;
11643 : 585212 : stmt_vec_info stmt_info;
11644 : :
11645 : 585212 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
11646 : 188030 : return;
11647 : :
11648 : 439520 : if (visited.add (node))
11649 : : return;
11650 : :
11651 : 892904 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11652 : 495722 : vect_remove_slp_scalar_calls (vinfo, child, visited);
11653 : :
11654 : 1261981 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
11655 : : {
11656 : 471692 : if (!stmt_info)
11657 : 3719 : continue;
11658 : 467973 : stmt_info = vect_orig_stmt (stmt_info);
11659 : 467973 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
11660 : 4805 : if (!stmt || gimple_bb (stmt) == NULL)
11661 : 463180 : continue;
11662 : 4793 : lhs = gimple_call_lhs (stmt);
11663 : 4793 : if (lhs)
11664 : 4233 : new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
11665 : : else
11666 : : {
11667 : 560 : new_stmt = gimple_build_nop ();
11668 : 560 : unlink_stmt_vdef (stmt_info->stmt);
11669 : : }
11670 : 4793 : gsi = gsi_for_stmt (stmt);
11671 : 4793 : vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
11672 : 4793 : if (lhs)
11673 : 4233 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
11674 : : }
11675 : : }
11676 : :
11677 : : static void
11678 : 89490 : vect_remove_slp_scalar_calls (vec_info *vinfo, slp_tree node)
11679 : : {
11680 : 89490 : hash_set<slp_tree> visited;
11681 : 89490 : vect_remove_slp_scalar_calls (vinfo, node, visited);
11682 : 89490 : }
11683 : :
11684 : : /* Vectorize the instance root. */
11685 : :
11686 : : void
11687 : 10028 : vectorize_slp_instance_root_stmt (vec_info *vinfo, slp_tree node, slp_instance instance)
11688 : : {
11689 : 10028 : gassign *rstmt = NULL;
11690 : :
11691 : 10028 : if (instance->kind == slp_inst_kind_ctor)
11692 : : {
11693 : 4143 : if (SLP_TREE_VEC_DEFS (node).length () == 1)
11694 : : {
11695 : 4139 : tree vect_lhs = SLP_TREE_VEC_DEFS (node)[0];
11696 : 4139 : tree root_lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
11697 : 4139 : if (!useless_type_conversion_p (TREE_TYPE (root_lhs),
11698 : 4139 : TREE_TYPE (vect_lhs)))
11699 : 0 : vect_lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (root_lhs),
11700 : : vect_lhs);
11701 : 4139 : rstmt = gimple_build_assign (root_lhs, vect_lhs);
11702 : : }
11703 : : else
11704 : : {
11705 : 4 : gcc_assert (SLP_TREE_VEC_DEFS (node).length () > 1);
11706 : 4 : tree child_def;
11707 : 4 : int j;
11708 : 4 : vec<constructor_elt, va_gc> *v;
11709 : 4 : vec_alloc (v, SLP_TREE_VEC_DEFS (node).length ());
11710 : :
11711 : : /* A CTOR can handle V16HI composition from VNx8HI so we
11712 : : do not need to convert vector elements if the types
11713 : : do not match. */
11714 : 12 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (node), j, child_def)
11715 : 8 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, child_def);
11716 : 4 : tree lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
11717 : 4 : tree rtype
11718 : 4 : = TREE_TYPE (gimple_assign_rhs1 (instance->root_stmts[0]->stmt));
11719 : 4 : tree r_constructor = build_constructor (rtype, v);
11720 : 4 : rstmt = gimple_build_assign (lhs, r_constructor);
11721 : : }
11722 : : }
11723 : 5885 : else if (instance->kind == slp_inst_kind_bb_reduc)
11724 : : {
11725 : : /* Largely inspired by reduction chain epilogue handling in
11726 : : vect_create_epilog_for_reduction. */
11727 : 4265 : vec<tree> vec_defs = vNULL;
11728 : 4265 : vect_get_slp_defs (node, &vec_defs);
11729 : 4265 : enum tree_code reduc_code
11730 : 4265 : = gimple_assign_rhs_code (instance->root_stmts[0]->stmt);
11731 : : /* ??? We actually have to reflect signs somewhere. */
11732 : 4265 : if (reduc_code == MINUS_EXPR)
11733 : 0 : reduc_code = PLUS_EXPR;
11734 : 4265 : gimple_seq epilogue = NULL;
11735 : : /* We may end up with more than one vector result, reduce them
11736 : : to one vector. */
11737 : 4265 : tree vec_def = vec_defs[0];
11738 : 4265 : tree vectype = TREE_TYPE (vec_def);
11739 : 4265 : tree compute_vectype = vectype;
11740 : 4265 : bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
11741 : 4066 : && TYPE_OVERFLOW_UNDEFINED (vectype)
11742 : 7193 : && operation_can_overflow (reduc_code));
11743 : 2800 : if (pun_for_overflow_p)
11744 : : {
11745 : 2800 : compute_vectype = unsigned_type_for (vectype);
11746 : 2800 : vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
11747 : : compute_vectype, vec_def);
11748 : : }
11749 : 6634 : for (unsigned i = 1; i < vec_defs.length (); ++i)
11750 : : {
11751 : 2369 : tree def = vec_defs[i];
11752 : 2369 : if (pun_for_overflow_p)
11753 : 2273 : def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
11754 : : compute_vectype, def);
11755 : 2369 : vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
11756 : : vec_def, def);
11757 : : }
11758 : 4265 : vec_defs.release ();
11759 : : /* ??? Support other schemes than direct internal fn. */
11760 : 4265 : internal_fn reduc_fn;
11761 : 4265 : if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
11762 : 4265 : || reduc_fn == IFN_LAST)
11763 : 0 : gcc_unreachable ();
11764 : 4265 : tree scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
11765 : 4265 : TREE_TYPE (compute_vectype), vec_def);
11766 : 4265 : if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
11767 : : {
11768 : 2798 : tree rem_def = NULL_TREE;
11769 : 12373 : for (auto def : SLP_INSTANCE_REMAIN_DEFS (instance))
11770 : : {
11771 : 9575 : def = gimple_convert (&epilogue, TREE_TYPE (scalar_def), def);
11772 : 9575 : if (!rem_def)
11773 : : rem_def = def;
11774 : : else
11775 : 6777 : rem_def = gimple_build (&epilogue, reduc_code,
11776 : 6777 : TREE_TYPE (scalar_def),
11777 : : rem_def, def);
11778 : : }
11779 : 2798 : scalar_def = gimple_build (&epilogue, reduc_code,
11780 : 2798 : TREE_TYPE (scalar_def),
11781 : : scalar_def, rem_def);
11782 : : }
11783 : 4265 : scalar_def = gimple_convert (&epilogue,
11784 : 4265 : TREE_TYPE (vectype), scalar_def);
11785 : 4265 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
11786 : 4265 : gsi_insert_seq_before (&rgsi, epilogue, GSI_SAME_STMT);
11787 : 4265 : gimple_assign_set_rhs_from_tree (&rgsi, scalar_def);
11788 : 4265 : update_stmt (gsi_stmt (rgsi));
11789 : 4265 : return;
11790 : : }
11791 : 1620 : else if (instance->kind == slp_inst_kind_gcond)
11792 : : {
11793 : : /* Only support a single root for now as we can't codegen CFG yet and so we
11794 : : can't support lane > 1 at this time. */
11795 : 1620 : gcc_assert (instance->root_stmts.length () == 1);
11796 : 1620 : auto root_stmt_info = instance->root_stmts[0];
11797 : 1620 : auto last_stmt = STMT_VINFO_STMT (vect_orig_stmt (root_stmt_info));
11798 : 1620 : gimple_stmt_iterator rgsi = gsi_for_stmt (last_stmt);
11799 : 1620 : gcc_assert (!SLP_TREE_VEC_DEFS (node).is_empty ());
11800 : 1620 : bool res = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
11801 : : root_stmt_info, &rgsi, node, NULL);
11802 : 1620 : gcc_assert (res);
11803 : 1620 : return;
11804 : : }
11805 : : else
11806 : 0 : gcc_unreachable ();
11807 : :
11808 : 4143 : gcc_assert (rstmt);
11809 : :
11810 : 4143 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
11811 : 4143 : gsi_replace (&rgsi, rstmt, true);
11812 : : }
11813 : :
11814 : : struct slp_scc_info
11815 : : {
11816 : : bool on_stack;
11817 : : int dfs;
11818 : : int lowlink;
11819 : : };
11820 : :
11821 : : /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs. */
11822 : :
11823 : : static void
11824 : 1459846 : vect_schedule_scc (vec_info *vinfo, slp_tree node, slp_instance instance,
11825 : : hash_map<slp_tree, slp_scc_info> &scc_info,
11826 : : int &maxdfs, vec<slp_tree> &stack)
11827 : : {
11828 : 1459846 : bool existed_p;
11829 : 1459846 : slp_scc_info *info = &scc_info.get_or_insert (node, &existed_p);
11830 : 1459846 : gcc_assert (!existed_p);
11831 : 1459846 : info->dfs = maxdfs;
11832 : 1459846 : info->lowlink = maxdfs;
11833 : 1459846 : maxdfs++;
11834 : :
11835 : : /* Leaf. */
11836 : 1459846 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
11837 : : {
11838 : 498872 : info->on_stack = false;
11839 : 498872 : vect_schedule_slp_node (vinfo, node, instance);
11840 : 1026573 : return;
11841 : : }
11842 : :
11843 : 960974 : info->on_stack = true;
11844 : 960974 : stack.safe_push (node);
11845 : :
11846 : 960974 : unsigned i;
11847 : 960974 : slp_tree child;
11848 : : /* DFS recurse. */
11849 : 1977062 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11850 : : {
11851 : 1016088 : if (!child)
11852 : 56608 : continue;
11853 : 959480 : slp_scc_info *child_info = scc_info.get (child);
11854 : 959480 : if (!child_info)
11855 : : {
11856 : 879053 : vect_schedule_scc (vinfo, child, instance, scc_info, maxdfs, stack);
11857 : : /* Recursion might have re-allocated the node. */
11858 : 879053 : info = scc_info.get (node);
11859 : 879053 : child_info = scc_info.get (child);
11860 : 879053 : info->lowlink = MIN (info->lowlink, child_info->lowlink);
11861 : : }
11862 : 80427 : else if (child_info->on_stack)
11863 : 24313 : info->lowlink = MIN (info->lowlink, child_info->dfs);
11864 : : }
11865 : 960974 : if (info->lowlink != info->dfs)
11866 : : return;
11867 : :
11868 : 932145 : auto_vec<slp_tree, 4> phis_to_fixup;
11869 : :
11870 : : /* Singleton. */
11871 : 932145 : if (stack.last () == node)
11872 : : {
11873 : 909187 : stack.pop ();
11874 : 909187 : info->on_stack = false;
11875 : 909187 : vect_schedule_slp_node (vinfo, node, instance);
11876 : 909187 : if (!SLP_TREE_PERMUTE_P (node)
11877 : 909187 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (node)->stmt))
11878 : 32506 : phis_to_fixup.quick_push (node);
11879 : : }
11880 : : else
11881 : : {
11882 : : /* SCC. */
11883 : 22958 : int last_idx = stack.length () - 1;
11884 : 51787 : while (stack[last_idx] != node)
11885 : 28829 : last_idx--;
11886 : : /* We can break the cycle at PHIs who have at least one child
11887 : : code generated. Then we could re-start the DFS walk until
11888 : : all nodes in the SCC are covered (we might have new entries
11889 : : for only back-reachable nodes). But it's simpler to just
11890 : : iterate and schedule those that are ready. */
11891 : 22958 : unsigned todo = stack.length () - last_idx;
11892 : 23278 : do
11893 : : {
11894 : 100061 : for (int idx = stack.length () - 1; idx >= last_idx; --idx)
11895 : : {
11896 : 53505 : slp_tree entry = stack[idx];
11897 : 53505 : if (!entry)
11898 : 934 : continue;
11899 : 52571 : bool phi = (!SLP_TREE_PERMUTE_P (entry)
11900 : 52571 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (entry)->stmt));
11901 : 52571 : bool ready = !phi;
11902 : 131758 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry), i, child)
11903 : 103339 : if (!child)
11904 : : {
11905 : 22164 : gcc_assert (phi);
11906 : : ready = true;
11907 : : break;
11908 : : }
11909 : 81175 : else if (scc_info.get (child)->on_stack)
11910 : : {
11911 : 23196 : if (!phi)
11912 : : {
11913 : : ready = false;
11914 : : break;
11915 : : }
11916 : : }
11917 : : else
11918 : : {
11919 : 57979 : if (phi)
11920 : : {
11921 : : ready = true;
11922 : : break;
11923 : : }
11924 : : }
11925 : 30407 : if (ready)
11926 : : {
11927 : 51787 : vect_schedule_slp_node (vinfo, entry, instance);
11928 : 51787 : scc_info.get (entry)->on_stack = false;
11929 : 51787 : stack[idx] = NULL;
11930 : 51787 : todo--;
11931 : 51787 : if (phi)
11932 : 23400 : phis_to_fixup.safe_push (entry);
11933 : : }
11934 : : }
11935 : : }
11936 : 23278 : while (todo != 0);
11937 : :
11938 : : /* Pop the SCC. */
11939 : 22958 : stack.truncate (last_idx);
11940 : : }
11941 : :
11942 : : /* Now fixup the backedge def of the vectorized PHIs in this SCC. */
11943 : : slp_tree phi_node;
11944 : 1920196 : FOR_EACH_VEC_ELT (phis_to_fixup, i, phi_node)
11945 : : {
11946 : 55906 : gphi *phi = as_a <gphi *> (SLP_TREE_REPRESENTATIVE (phi_node)->stmt);
11947 : 55906 : edge_iterator ei;
11948 : 55906 : edge e;
11949 : 172800 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
11950 : : {
11951 : 116894 : unsigned dest_idx = e->dest_idx;
11952 : 116894 : child = SLP_TREE_CHILDREN (phi_node)[dest_idx];
11953 : 116894 : if (!child || SLP_TREE_DEF_TYPE (child) != vect_internal_def)
11954 : 67285 : continue;
11955 : 49609 : unsigned n = SLP_TREE_VEC_DEFS (phi_node).length ();
11956 : : /* Simply fill all args. */
11957 : 49609 : if (STMT_VINFO_DEF_TYPE (SLP_TREE_REPRESENTATIVE (phi_node))
11958 : : != vect_first_order_recurrence)
11959 : 107018 : for (unsigned i = 0; i < n; ++i)
11960 : : {
11961 : 57451 : tree phidef = SLP_TREE_VEC_DEFS (phi_node)[i];
11962 : 57451 : gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (phidef));
11963 : 57451 : add_phi_arg (phi, vect_get_slp_vect_def (child, i),
11964 : : e, gimple_phi_arg_location (phi, dest_idx));
11965 : : }
11966 : : else
11967 : : {
11968 : : /* Unless it is a first order recurrence which needs
11969 : : args filled in for both the PHI node and the permutes. */
11970 : 42 : gimple *perm
11971 : 42 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[0]);
11972 : 42 : gimple *rphi = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (perm));
11973 : 42 : add_phi_arg (as_a <gphi *> (rphi),
11974 : : vect_get_slp_vect_def (child, n - 1),
11975 : : e, gimple_phi_arg_location (phi, dest_idx));
11976 : 121 : for (unsigned i = 0; i < n; ++i)
11977 : : {
11978 : 79 : gimple *perm
11979 : 79 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[i]);
11980 : 79 : if (i > 0)
11981 : 37 : gimple_assign_set_rhs1 (perm,
11982 : : vect_get_slp_vect_def (child, i - 1));
11983 : 79 : gimple_assign_set_rhs2 (perm,
11984 : : vect_get_slp_vect_def (child, i));
11985 : 79 : update_stmt (perm);
11986 : : }
11987 : : }
11988 : : }
11989 : : }
11990 : 932145 : }
11991 : :
11992 : : /* Generate vector code for SLP_INSTANCES in the loop/basic block. */
11993 : :
11994 : : void
11995 : 540903 : vect_schedule_slp (vec_info *vinfo, const vec<slp_instance> &slp_instances)
11996 : : {
11997 : 540903 : slp_instance instance;
11998 : 540903 : unsigned int i;
11999 : :
12000 : 540903 : hash_map<slp_tree, slp_scc_info> scc_info;
12001 : 540903 : int maxdfs = 0;
12002 : 1121877 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
12003 : : {
12004 : 580974 : slp_tree node = SLP_INSTANCE_TREE (instance);
12005 : 580974 : if (dump_enabled_p ())
12006 : : {
12007 : 15730 : dump_printf_loc (MSG_NOTE, vect_location,
12008 : : "Vectorizing SLP tree:\n");
12009 : : /* ??? Dump all? */
12010 : 15730 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
12011 : 405 : dump_printf_loc (MSG_NOTE, vect_location, "Root stmt: %G",
12012 : 405 : SLP_INSTANCE_ROOT_STMTS (instance)[0]->stmt);
12013 : 15730 : vect_print_slp_graph (MSG_NOTE, vect_location,
12014 : : SLP_INSTANCE_TREE (instance));
12015 : : }
12016 : : /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
12017 : : have a PHI be the node breaking the cycle. */
12018 : 580974 : auto_vec<slp_tree> stack;
12019 : 580974 : if (!scc_info.get (node))
12020 : 580793 : vect_schedule_scc (vinfo, node, instance, scc_info, maxdfs, stack);
12021 : :
12022 : 580974 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
12023 : 10028 : vectorize_slp_instance_root_stmt (vinfo, node, instance);
12024 : :
12025 : 580974 : if (dump_enabled_p ())
12026 : 15730 : dump_printf_loc (MSG_NOTE, vect_location,
12027 : : "vectorizing stmts using SLP.\n");
12028 : 580974 : }
12029 : :
12030 : 1662780 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
12031 : : {
12032 : 580974 : slp_tree root = SLP_INSTANCE_TREE (instance);
12033 : 580974 : stmt_vec_info store_info;
12034 : 580974 : unsigned int j;
12035 : :
12036 : : /* Remove scalar call stmts. Do not do this for basic-block
12037 : : vectorization as not all uses may be vectorized.
12038 : : ??? Why should this be necessary? DCE should be able to
12039 : : remove the stmts itself.
12040 : : ??? For BB vectorization we can as well remove scalar
12041 : : stmts starting from the SLP tree root if they have no
12042 : : uses. */
12043 : 580974 : if (is_a <loop_vec_info> (vinfo))
12044 : 89490 : vect_remove_slp_scalar_calls (vinfo, root);
12045 : :
12046 : : /* Remove vectorized stores original scalar stmts. */
12047 : 2594161 : for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info); j++)
12048 : : {
12049 : 1469384 : if (!STMT_VINFO_DATA_REF (store_info)
12050 : 1440050 : || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info)))
12051 : : break;
12052 : :
12053 : 1432213 : store_info = vect_orig_stmt (store_info);
12054 : : /* Free the attached stmt_vec_info and remove the stmt. */
12055 : 1432213 : vinfo->remove_stmt (store_info);
12056 : :
12057 : : /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
12058 : : to not crash in vect_free_slp_tree later. */
12059 : 1432213 : if (SLP_TREE_REPRESENTATIVE (root) == store_info)
12060 : 543507 : SLP_TREE_REPRESENTATIVE (root) = NULL;
12061 : : }
12062 : : }
12063 : 540903 : }
|