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 : 1127011 : vect_slp_init (void)
78 : : {
79 : 1127011 : slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
80 : 1127011 : }
81 : :
82 : : void
83 : 1127011 : vect_slp_fini (void)
84 : : {
85 : 1670539 : while (slp_first_node)
86 : 543528 : delete slp_first_node;
87 : 2254022 : delete slp_tree_pool;
88 : 1127011 : slp_tree_pool = NULL;
89 : 1127011 : }
90 : :
91 : : void *
92 : 7120291 : _slp_tree::operator new (size_t n)
93 : : {
94 : 7120291 : gcc_assert (n == sizeof (_slp_tree));
95 : 7120291 : return slp_tree_pool->allocate_raw ();
96 : : }
97 : :
98 : : void
99 : 7120291 : _slp_tree::operator delete (void *node, size_t n)
100 : : {
101 : 7120291 : gcc_assert (n == sizeof (_slp_tree));
102 : 7120291 : slp_tree_pool->remove_raw (node);
103 : 7120291 : }
104 : :
105 : :
106 : : /* Initialize a SLP node. */
107 : :
108 : 7120291 : _slp_tree::_slp_tree ()
109 : : {
110 : 7120291 : this->prev_node = NULL;
111 : 7120291 : if (slp_first_node)
112 : 6171254 : slp_first_node->prev_node = this;
113 : 7120291 : this->next_node = slp_first_node;
114 : 7120291 : slp_first_node = this;
115 : 7120291 : SLP_TREE_SCALAR_STMTS (this) = vNULL;
116 : 7120291 : SLP_TREE_SCALAR_OPS (this) = vNULL;
117 : 7120291 : SLP_TREE_VEC_DEFS (this) = vNULL;
118 : 7120291 : SLP_TREE_CHILDREN (this) = vNULL;
119 : 7120291 : SLP_TREE_LOAD_PERMUTATION (this) = vNULL;
120 : 7120291 : SLP_TREE_LANE_PERMUTATION (this) = vNULL;
121 : 7120291 : SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def;
122 : 7120291 : SLP_TREE_CODE (this) = ERROR_MARK;
123 : 7120291 : SLP_TREE_GS_SCALE (this) = 0;
124 : 7120291 : SLP_TREE_GS_BASE (this) = NULL_TREE;
125 : 7120291 : this->ldst_lanes = false;
126 : 7120291 : this->avoid_stlf_fail = false;
127 : 7120291 : SLP_TREE_VECTYPE (this) = NULL_TREE;
128 : 7120291 : SLP_TREE_REPRESENTATIVE (this) = NULL;
129 : 7120291 : this->cycle_info.id = -1;
130 : 7120291 : this->cycle_info.reduc_idx = -1;
131 : 7120291 : SLP_TREE_REF_COUNT (this) = 1;
132 : 7120291 : this->failed = NULL;
133 : 7120291 : this->max_nunits = 1;
134 : 7120291 : this->lanes = 0;
135 : 7120291 : SLP_TREE_TYPE (this) = undef_vec_info_type;
136 : 7120291 : this->data = NULL;
137 : 7120291 : }
138 : :
139 : : /* Tear down a SLP node. */
140 : :
141 : 7120291 : _slp_tree::~_slp_tree ()
142 : : {
143 : 7120291 : if (this->prev_node)
144 : 4333469 : this->prev_node->next_node = this->next_node;
145 : : else
146 : 2786822 : slp_first_node = this->next_node;
147 : 7120291 : if (this->next_node)
148 : 5300380 : this->next_node->prev_node = this->prev_node;
149 : 7120291 : SLP_TREE_CHILDREN (this).release ();
150 : 7120291 : SLP_TREE_SCALAR_STMTS (this).release ();
151 : 7120291 : SLP_TREE_SCALAR_OPS (this).release ();
152 : 7120291 : SLP_TREE_VEC_DEFS (this).release ();
153 : 7120291 : SLP_TREE_LOAD_PERMUTATION (this).release ();
154 : 7120291 : SLP_TREE_LANE_PERMUTATION (this).release ();
155 : 7120291 : if (this->failed)
156 : 1958606 : free (failed);
157 : 7120291 : if (this->data)
158 : 1153397 : delete this->data;
159 : 7120291 : }
160 : :
161 : : /* Push the single SSA definition in DEF to the vector of vector defs. */
162 : :
163 : : void
164 : 514401 : _slp_tree::push_vec_def (gimple *def)
165 : : {
166 : 514401 : if (gphi *phi = dyn_cast <gphi *> (def))
167 : 57250 : vec_defs.quick_push (gimple_phi_result (phi));
168 : : else
169 : : {
170 : 457151 : def_operand_p defop = single_ssa_def_operand (def, SSA_OP_ALL_DEFS);
171 : 457151 : vec_defs.quick_push (get_def_from_ptr (defop));
172 : : }
173 : 514401 : }
174 : :
175 : : /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
176 : :
177 : : void
178 : 13285502 : vect_free_slp_tree (slp_tree node)
179 : : {
180 : 13285502 : int i;
181 : 13285502 : slp_tree child;
182 : :
183 : 13285502 : if (--SLP_TREE_REF_COUNT (node) != 0)
184 : 13285502 : return;
185 : :
186 : 10088072 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
187 : 3511309 : if (child)
188 : 3226223 : 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 : 6576763 : stmt_vec_info rep_stmt_info = SLP_TREE_REPRESENTATIVE (node);
193 : 6576763 : if (rep_stmt_info && STMT_VINFO_SLP_VECT_ONLY_PATTERN (rep_stmt_info))
194 : : {
195 : 964 : stmt_vec_info stmt_info = vect_orig_stmt (rep_stmt_info);
196 : 964 : STMT_VINFO_IN_PATTERN_P (stmt_info) = false;
197 : 964 : STMT_SLP_TYPE (stmt_info) = STMT_SLP_TYPE (rep_stmt_info);
198 : : }
199 : :
200 : 6576763 : delete node;
201 : : }
202 : :
203 : : /* Return a location suitable for dumpings related to the SLP instance. */
204 : :
205 : : dump_user_location_t
206 : 3416581 : _slp_instance::location () const
207 : : {
208 : 3416581 : if (!root_stmts.is_empty ())
209 : 323302 : return root_stmts[0]->stmt;
210 : : else
211 : 3093279 : return SLP_TREE_SCALAR_STMTS (root)[0]->stmt;
212 : : }
213 : :
214 : :
215 : : /* Free the memory allocated for the SLP instance. */
216 : :
217 : : void
218 : 1422698 : vect_free_slp_instance (slp_instance instance)
219 : : {
220 : 1422698 : vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
221 : 1422698 : SLP_INSTANCE_LOADS (instance).release ();
222 : 1422698 : SLP_INSTANCE_ROOT_STMTS (instance).release ();
223 : 1422698 : SLP_INSTANCE_REMAIN_DEFS (instance).release ();
224 : 1422698 : instance->subgraph_entries.release ();
225 : 1422698 : instance->cost_vec.release ();
226 : 1422698 : free (instance);
227 : 1422698 : }
228 : :
229 : :
230 : : /* Create an SLP node for SCALAR_STMTS. */
231 : :
232 : : slp_tree
233 : 108805 : vect_create_new_slp_node (unsigned nops, tree_code code)
234 : : {
235 : 108805 : slp_tree node = new _slp_tree;
236 : 108805 : SLP_TREE_SCALAR_STMTS (node) = vNULL;
237 : 108805 : SLP_TREE_CHILDREN (node).create (nops);
238 : 108805 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
239 : 108805 : SLP_TREE_CODE (node) = code;
240 : 108805 : return node;
241 : : }
242 : : /* Create an SLP node for SCALAR_STMTS. */
243 : :
244 : : static slp_tree
245 : 3300081 : vect_create_new_slp_node (slp_tree node,
246 : : vec<stmt_vec_info> scalar_stmts, unsigned nops)
247 : : {
248 : 3300081 : SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
249 : 3300081 : SLP_TREE_CHILDREN (node).create (nops);
250 : 3300081 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
251 : 3300081 : SLP_TREE_REPRESENTATIVE (node) = scalar_stmts[0];
252 : 3300081 : SLP_TREE_LANES (node) = scalar_stmts.length ();
253 : 3300081 : return node;
254 : : }
255 : :
256 : : /* Create an SLP node for SCALAR_STMTS. */
257 : :
258 : : static slp_tree
259 : 6240 : vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts, unsigned nops)
260 : : {
261 : 6240 : 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 : 1743043 : vect_create_new_slp_node (slp_tree node, vec<tree> ops)
268 : : {
269 : 1743043 : SLP_TREE_SCALAR_OPS (node) = ops;
270 : 1743043 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
271 : 0 : SLP_TREE_LANES (node) = ops.length ();
272 : 1743043 : return node;
273 : : }
274 : :
275 : : /* Create an SLP node for OPS. */
276 : :
277 : : static slp_tree
278 : 1743043 : vect_create_new_slp_node (vec<tree> ops)
279 : : {
280 : 1743043 : 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 : 2941039 : vect_create_oprnd_info (int nops, int group_size)
308 : : {
309 : 2941039 : int i;
310 : 2941039 : slp_oprnd_info oprnd_info;
311 : 2941039 : vec<slp_oprnd_info> oprnds_info;
312 : :
313 : 2941039 : oprnds_info.create (nops);
314 : 10482122 : for (i = 0; i < nops; i++)
315 : : {
316 : 4600044 : oprnd_info = XNEW (struct _slp_oprnd_info);
317 : 4600044 : oprnd_info->def_stmts.create (group_size);
318 : 4600044 : oprnd_info->ops.create (group_size);
319 : 4600044 : oprnd_info->first_dt = vect_uninitialized_def;
320 : 4600044 : oprnd_info->first_op_type = NULL_TREE;
321 : 4600044 : oprnd_info->any_pattern = false;
322 : 4600044 : oprnd_info->first_gs_p = false;
323 : 4600044 : oprnds_info.quick_push (oprnd_info);
324 : : }
325 : :
326 : 2941039 : return oprnds_info;
327 : : }
328 : :
329 : :
330 : : /* Free operands info. */
331 : :
332 : : static void
333 : 2941039 : vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
334 : : {
335 : 2941039 : int i;
336 : 2941039 : slp_oprnd_info oprnd_info;
337 : :
338 : 7541083 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
339 : : {
340 : 4600044 : oprnd_info->def_stmts.release ();
341 : 4600044 : oprnd_info->ops.release ();
342 : 4600044 : XDELETE (oprnd_info);
343 : : }
344 : :
345 : 2941039 : oprnds_info.release ();
346 : 2941039 : }
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 : 3126949 : vect_slp_node_weight (slp_tree node)
353 : : {
354 : 3126949 : stmt_vec_info stmt_info = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (node));
355 : 3126949 : basic_block bb = gimple_bb (stmt_info->stmt);
356 : 3126949 : 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 : 22349 : vect_contains_pattern_stmt_p (vec<stmt_vec_info> stmts)
363 : : {
364 : 22349 : stmt_vec_info stmt_info;
365 : 22349 : unsigned int i;
366 : 72913 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
367 : 52757 : 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 : 612372 : vect_slp_tree_uniform_p (slp_tree node)
377 : : {
378 : 612372 : 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 : 1080634 : if (SLP_TREE_SCALAR_OPS (node).is_empty ())
383 : : return false;
384 : :
385 : : unsigned i;
386 : : tree op, first = NULL_TREE;
387 : 1397197 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
388 : 1253087 : if (!first)
389 : : first = op;
390 : 640715 : 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 : 680263 : vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
402 : : stmt_vec_info first_stmt_info)
403 : : {
404 : 680263 : stmt_vec_info next_stmt_info = first_stmt_info;
405 : 680263 : int result = 0;
406 : :
407 : 680263 : if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
408 : : return -1;
409 : :
410 : 1723711 : do
411 : : {
412 : 1723711 : if (next_stmt_info == stmt_info)
413 : : return result;
414 : 1043448 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
415 : 1043448 : if (next_stmt_info)
416 : 1043448 : result += DR_GROUP_GAP (next_stmt_info);
417 : : }
418 : 1043448 : 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 : 16808173 : vect_def_types_match (enum vect_def_type dta, enum vect_def_type dtb)
509 : : {
510 : 16808173 : return (dta == dtb
511 : 337736 : || ((dta == vect_external_def || dta == vect_constant_def)
512 : 212417 : && (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 : 18796492 : vect_get_operand_map (const gimple *stmt, bool gather_scatter_p = false,
554 : : unsigned char swap = 0)
555 : : {
556 : 18796492 : if (auto assign = dyn_cast<const gassign *> (stmt))
557 : : {
558 : 17693823 : if (gimple_assign_rhs_code (assign) == COND_EXPR
559 : 17693823 : && COMPARISON_CLASS_P (gimple_assign_rhs1 (assign)))
560 : 0 : gcc_unreachable ();
561 : 17693823 : if ((TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) == tcc_comparison
562 : 16378531 : || commutative_tree_code (gimple_assign_rhs_code (assign)))
563 : 26191898 : && swap)
564 : : return op1_op0_map;
565 : 17650735 : if (gather_scatter_p)
566 : 30620 : return (TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
567 : 30620 : ? off_op0_map : off_map);
568 : : }
569 : 18722784 : gcc_assert (!swap);
570 : 18722784 : if (auto call = dyn_cast<const gcall *> (stmt))
571 : : {
572 : 134851 : if (gimple_call_internal_p (call))
573 : 72141 : switch (gimple_call_internal_fn (call))
574 : : {
575 : 11739 : case IFN_MASK_LOAD:
576 : 19258 : return gather_scatter_p ? off_arg2_arg3_map : arg2_arg3_map;
577 : :
578 : 0 : case IFN_GATHER_LOAD:
579 : 0 : return arg2_map;
580 : :
581 : 0 : case IFN_MASK_GATHER_LOAD:
582 : 0 : case IFN_MASK_LEN_GATHER_LOAD:
583 : 0 : return arg2_arg5_arg6_map;
584 : :
585 : 0 : case IFN_SCATTER_STORE:
586 : 0 : return arg2_arg4_map;
587 : :
588 : 0 : case IFN_MASK_SCATTER_STORE:
589 : 0 : case IFN_MASK_LEN_SCATTER_STORE:
590 : 0 : return arg2_arg4_arg5_map;
591 : :
592 : 5840 : case IFN_MASK_STORE:
593 : 10404 : return gather_scatter_p ? off_arg3_arg2_map : arg3_arg2_map;
594 : :
595 : 988 : case IFN_MASK_CALL:
596 : 988 : {
597 : 988 : unsigned nargs = gimple_call_num_args (call);
598 : 988 : if (nargs >= 2 && nargs <= 7)
599 : 988 : return mask_call_maps[nargs-2];
600 : : else
601 : : return nullptr;
602 : : }
603 : :
604 : 140 : case IFN_CLZ:
605 : 140 : case IFN_CTZ:
606 : 140 : return arg0_map;
607 : :
608 : 6306 : case IFN_GOMP_SIMD_LANE:
609 : 6306 : return no_arg_map;
610 : :
611 : : default:
612 : : break;
613 : : }
614 : : }
615 : : return nullptr;
616 : : }
617 : :
618 : : /* Return the SLP node child index for operand OP of STMT. */
619 : :
620 : : int
621 : 1336505 : vect_slp_child_index_for_operand (const gimple *stmt, int op,
622 : : bool gather_scatter_p)
623 : : {
624 : 1336505 : const int *opmap = vect_get_operand_map (stmt, gather_scatter_p);
625 : 1336505 : if (!opmap)
626 : : return op;
627 : 17833 : for (int i = 1; i < 1 + opmap[0]; ++i)
628 : 17833 : if (opmap[i] == op)
629 : 9765 : return i - 1;
630 : 0 : gcc_unreachable ();
631 : : }
632 : :
633 : : /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
634 : : they are of a valid type and that they match the defs of the first stmt of
635 : : the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
636 : : by swapping operands of STMTS[STMT_NUM] when possible. Non-zero SWAP
637 : : indicates swap is required for cond_expr stmts. Specifically, SWAP
638 : : is 1 if STMT is cond and operands of comparison need to be swapped;
639 : : SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
640 : :
641 : : If there was a fatal error return -1; if the error could be corrected by
642 : : swapping operands of father node of this one, return 1; if everything is
643 : : ok return 0. */
644 : : static int
645 : 12194154 : vect_get_and_check_slp_defs (vec_info *vinfo, tree vectype, unsigned char swap,
646 : : bool *skip_args,
647 : : vec<stmt_vec_info> stmts, unsigned stmt_num,
648 : : vec<slp_oprnd_info> *oprnds_info)
649 : : {
650 : 12194154 : stmt_vec_info stmt_info = stmts[stmt_num];
651 : 12194154 : tree oprnd;
652 : 12194154 : unsigned int i, number_of_oprnds;
653 : 12194154 : enum vect_def_type dt = vect_uninitialized_def;
654 : 12194154 : slp_oprnd_info oprnd_info;
655 : 12194154 : gather_scatter_info gs_info;
656 : 12194154 : unsigned int gs_op = -1u;
657 : 12194154 : unsigned int commutative_op = -1U;
658 : 12194154 : bool first = stmt_num == 0;
659 : :
660 : 12194154 : if (!stmt_info)
661 : : {
662 : 0 : for (auto oi : *oprnds_info)
663 : : {
664 : 0 : oi->def_stmts.quick_push (NULL);
665 : 0 : oi->ops.quick_push (NULL_TREE);
666 : : }
667 : : return 0;
668 : : }
669 : :
670 : 12194154 : if (!is_a<gcall *> (stmt_info->stmt)
671 : : && !is_a<gassign *> (stmt_info->stmt)
672 : : && !is_a<gphi *> (stmt_info->stmt))
673 : : return -1;
674 : :
675 : 12194154 : number_of_oprnds = gimple_num_args (stmt_info->stmt);
676 : 12194154 : const int *map
677 : 24388308 : = vect_get_operand_map (stmt_info->stmt,
678 : 12194154 : STMT_VINFO_GATHER_SCATTER_P (stmt_info), swap);
679 : 12194154 : if (map)
680 : 66125 : number_of_oprnds = *map++;
681 : 12194154 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
682 : : {
683 : 39875 : if (gimple_call_internal_p (stmt))
684 : : {
685 : 23548 : internal_fn ifn = gimple_call_internal_fn (stmt);
686 : 23548 : commutative_op = first_commutative_argument (ifn);
687 : 23548 : if (internal_gather_scatter_fn_p (ifn))
688 : : {
689 : 0 : vect_describe_gather_scatter_call
690 : 0 : (stmt_info,
691 : 0 : first ? &(*oprnds_info)[0]->first_gs_info : &gs_info);
692 : 0 : if (first)
693 : 0 : (*oprnds_info)[0]->first_gs_p = true;
694 : : gs_op = 0;
695 : : }
696 : : }
697 : : }
698 : 12154279 : else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
699 : : {
700 : 14244736 : if (commutative_tree_code (gimple_assign_rhs_code (stmt)))
701 : 8167450 : commutative_op = 0;
702 : : }
703 : :
704 : 12194154 : bool swapped = (swap != 0);
705 : 12194154 : bool backedge = false;
706 : 12194154 : enum vect_def_type *dts = XALLOCAVEC (enum vect_def_type, number_of_oprnds);
707 : 33741337 : for (i = 0; i < number_of_oprnds; i++)
708 : : {
709 : 21549657 : oprnd_info = (*oprnds_info)[i];
710 : 21549657 : int opno = map ? map[i] : int (i);
711 : 21549657 : if (opno == GATHER_SCATTER_OFFSET)
712 : : {
713 : 16259 : gcc_assert (STMT_VINFO_GATHER_SCATTER_P (stmt_info));
714 : 16259 : if (!is_a <loop_vec_info> (vinfo)
715 : 16259 : || !vect_check_gather_scatter (stmt_info, vectype,
716 : : as_a <loop_vec_info> (vinfo),
717 : : first ? &oprnd_info->first_gs_info
718 : : : &gs_info))
719 : 2474 : return -1;
720 : :
721 : 16259 : if (first)
722 : : {
723 : 16022 : oprnd_info->first_gs_p = true;
724 : 16022 : oprnd = oprnd_info->first_gs_info.offset;
725 : : }
726 : : else
727 : : {
728 : 237 : gs_op = i;
729 : 237 : oprnd = gs_info.offset;
730 : : }
731 : : }
732 : 21533398 : else if (opno < 0)
733 : 0 : oprnd = TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
734 : : else
735 : : {
736 : 21533398 : oprnd = gimple_arg (stmt_info->stmt, opno);
737 : 21533398 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
738 : : {
739 : 1034795 : edge e = gimple_phi_arg_edge (stmt, opno);
740 : 2069590 : backedge = (is_a <bb_vec_info> (vinfo)
741 : 1530242 : ? e->flags & EDGE_DFS_BACK
742 : 495447 : : dominated_by_p (CDI_DOMINATORS, e->src,
743 : 495447 : gimple_bb (stmt_info->stmt)));
744 : : }
745 : : }
746 : 21549657 : if (TREE_CODE (oprnd) == VIEW_CONVERT_EXPR)
747 : 2626 : oprnd = TREE_OPERAND (oprnd, 0);
748 : :
749 : 21549657 : stmt_vec_info def_stmt_info;
750 : 21549657 : if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info))
751 : : {
752 : 1109 : if (dump_enabled_p ())
753 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
754 : : "Build SLP failed: can't analyze def for %T\n",
755 : : oprnd);
756 : :
757 : 1109 : return -1;
758 : : }
759 : :
760 : 21548548 : if (skip_args[i])
761 : : {
762 : 403587 : oprnd_info->def_stmts.quick_push (NULL);
763 : 403587 : oprnd_info->ops.quick_push (NULL_TREE);
764 : 403587 : oprnd_info->first_dt = vect_uninitialized_def;
765 : 403587 : continue;
766 : : }
767 : :
768 : 21144961 : oprnd_info->def_stmts.quick_push (def_stmt_info);
769 : 21144961 : oprnd_info->ops.quick_push (oprnd);
770 : :
771 : 21144961 : if (def_stmt_info
772 : 21144961 : && is_pattern_stmt_p (def_stmt_info))
773 : : {
774 : 346823 : if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info))
775 : : != def_stmt_info)
776 : 249070 : oprnd_info->any_pattern = true;
777 : : else
778 : : /* If we promote this to external use the original stmt def. */
779 : 97753 : oprnd_info->ops.last ()
780 : 195506 : = gimple_get_lhs (vect_orig_stmt (def_stmt_info)->stmt);
781 : : }
782 : :
783 : : /* If there's a extern def on a backedge make sure we can
784 : : code-generate at the region start.
785 : : ??? This is another case that could be fixed by adjusting
786 : : how we split the function but at the moment we'd have conflicting
787 : : goals there. */
788 : 21144961 : if (backedge
789 : 126527 : && dts[i] == vect_external_def
790 : 1386 : && is_a <bb_vec_info> (vinfo)
791 : 1386 : && TREE_CODE (oprnd) == SSA_NAME
792 : 1365 : && !SSA_NAME_IS_DEFAULT_DEF (oprnd)
793 : 21146326 : && !dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
794 : 1365 : gimple_bb (SSA_NAME_DEF_STMT (oprnd))))
795 : : {
796 : 1365 : if (dump_enabled_p ())
797 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
798 : : "Build SLP failed: extern def %T only defined "
799 : : "on backedge\n", oprnd);
800 : 1365 : return -1;
801 : : }
802 : :
803 : 21143596 : if (first)
804 : : {
805 : 4218610 : tree type = TREE_TYPE (oprnd);
806 : 4218610 : dt = dts[i];
807 : :
808 : : /* For the swapping logic below force vect_reduction_def
809 : : for the reduction op in a SLP reduction group. */
810 : 4218610 : if (!STMT_VINFO_DATA_REF (stmt_info)
811 : 3134206 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
812 : 3268 : && (int)i == STMT_VINFO_REDUC_IDX (stmt_info)
813 : 4220220 : && def_stmt_info)
814 : 1610 : dts[i] = dt = vect_reduction_def;
815 : :
816 : : /* Check the types of the definition. */
817 : 4218610 : switch (dt)
818 : : {
819 : 4218610 : case vect_external_def:
820 : 4218610 : case vect_constant_def:
821 : 4218610 : case vect_internal_def:
822 : 4218610 : case vect_reduction_def:
823 : 4218610 : case vect_double_reduction_def:
824 : 4218610 : case vect_induction_def:
825 : 4218610 : case vect_nested_cycle:
826 : 4218610 : case vect_first_order_recurrence:
827 : 4218610 : break;
828 : :
829 : 0 : default:
830 : : /* FORNOW: Not supported. */
831 : 0 : if (dump_enabled_p ())
832 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
833 : : "Build SLP failed: illegal type of def %T\n",
834 : : oprnd);
835 : 0 : return -1;
836 : : }
837 : :
838 : 4218610 : oprnd_info->first_dt = dt;
839 : 4218610 : oprnd_info->first_op_type = type;
840 : : }
841 : : }
842 : 12191680 : if (first)
843 : : return 0;
844 : :
845 : : /* Now match the operand definition types to that of the first stmt. */
846 : 25916673 : for (i = 0; i < number_of_oprnds;)
847 : : {
848 : 16919880 : if (skip_args[i])
849 : : {
850 : 27655 : ++i;
851 : 27655 : continue;
852 : : }
853 : :
854 : 16892225 : oprnd_info = (*oprnds_info)[i];
855 : 16892225 : dt = dts[i];
856 : 16892225 : stmt_vec_info def_stmt_info = oprnd_info->def_stmts[stmt_num];
857 : 16892225 : oprnd = oprnd_info->ops[stmt_num];
858 : 16892225 : tree type = TREE_TYPE (oprnd);
859 : :
860 : 16892225 : if (!types_compatible_p (oprnd_info->first_op_type, type))
861 : : {
862 : 90816 : if (dump_enabled_p ())
863 : 107 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
864 : : "Build SLP failed: different operand types\n");
865 : 90816 : return 1;
866 : : }
867 : :
868 : 16801409 : if ((gs_op == i) != oprnd_info->first_gs_p)
869 : : {
870 : 0 : if (dump_enabled_p ())
871 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
872 : : "Build SLP failed: mixed gather and non-gather\n");
873 : 0 : return 1;
874 : : }
875 : 16801409 : else if (gs_op == i)
876 : : {
877 : 207 : if (!operand_equal_p (oprnd_info->first_gs_info.base,
878 : 207 : gs_info.base))
879 : : {
880 : 16 : if (dump_enabled_p ())
881 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
882 : : "Build SLP failed: different gather base\n");
883 : 16 : return 1;
884 : : }
885 : 191 : if (oprnd_info->first_gs_info.scale != gs_info.scale)
886 : : {
887 : 8 : if (dump_enabled_p ())
888 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
889 : : "Build SLP failed: different gather scale\n");
890 : 8 : return 1;
891 : : }
892 : : }
893 : :
894 : : /* Not first stmt of the group, check that the def-stmt/s match
895 : : the def-stmt/s of the first stmt. Allow different definition
896 : : types for reduction chains: the first stmt must be a
897 : : vect_reduction_def (a phi node), and the rest
898 : : end in the reduction chain. */
899 : 16801385 : if ((!vect_def_types_match (oprnd_info->first_dt, dt)
900 : 278120 : && !(oprnd_info->first_dt == vect_reduction_def
901 : 2764 : && !STMT_VINFO_DATA_REF (stmt_info)
902 : 2764 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
903 : 2754 : && def_stmt_info
904 : 2754 : && !STMT_VINFO_DATA_REF (def_stmt_info)
905 : 2754 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
906 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
907 : 16526019 : || (!STMT_VINFO_DATA_REF (stmt_info)
908 : 15234756 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
909 : 5788 : && ((!def_stmt_info
910 : 5627 : || STMT_VINFO_DATA_REF (def_stmt_info)
911 : 10329 : || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
912 : : != REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
913 : 5788 : != (oprnd_info->first_dt != vect_reduction_def))))
914 : : {
915 : : /* Try swapping operands if we got a mismatch. For BB
916 : : vectorization only in case it will clearly improve things. */
917 : 277409 : if (i == commutative_op && !swapped
918 : 275366 : && (!is_a <bb_vec_info> (vinfo)
919 : 5341 : || (!vect_def_types_match ((*oprnds_info)[i+1]->first_dt,
920 : 5341 : dts[i+1])
921 : 1258 : && (vect_def_types_match (oprnd_info->first_dt, dts[i+1])
922 : : || vect_def_types_match
923 : 189 : ((*oprnds_info)[i+1]->first_dt, dts[i])))))
924 : : {
925 : 2043 : if (dump_enabled_p ())
926 : 144 : dump_printf_loc (MSG_NOTE, vect_location,
927 : : "trying swapped operands\n");
928 : 2043 : std::swap (dts[i], dts[i+1]);
929 : 2043 : std::swap ((*oprnds_info)[i]->def_stmts[stmt_num],
930 : 2043 : (*oprnds_info)[i+1]->def_stmts[stmt_num]);
931 : 2043 : std::swap ((*oprnds_info)[i]->ops[stmt_num],
932 : 2043 : (*oprnds_info)[i+1]->ops[stmt_num]);
933 : : /* After swapping some operands we lost track whether an
934 : : operand has any pattern defs so be conservative here. */
935 : 2043 : if ((*oprnds_info)[i]->any_pattern
936 : 2043 : || (*oprnds_info)[i+1]->any_pattern)
937 : 4 : (*oprnds_info)[i]->any_pattern
938 : 2 : = (*oprnds_info)[i+1]->any_pattern = true;
939 : 2043 : swapped = true;
940 : 2043 : continue;
941 : : }
942 : :
943 : 273323 : if (is_a <bb_vec_info> (vinfo)
944 : 262814 : && !oprnd_info->any_pattern
945 : 535908 : && number_of_oprnds > 1)
946 : : {
947 : : /* Now for commutative ops we should see whether we can
948 : : make the other operand matching. */
949 : 105236 : if (dump_enabled_p ())
950 : 149 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
951 : : "treating operand as external\n");
952 : 105236 : oprnd_info->first_dt = dt = vect_external_def;
953 : : }
954 : : else
955 : : {
956 : 168087 : if (dump_enabled_p ())
957 : 404 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
958 : : "Build SLP failed: different types\n");
959 : 168087 : return 1;
960 : : }
961 : : }
962 : :
963 : : /* Make sure to demote the overall operand to external. */
964 : 16631255 : if (dt == vect_external_def)
965 : 346877 : oprnd_info->first_dt = vect_external_def;
966 : : /* For a SLP reduction chain we want to duplicate the reduction to
967 : : each of the chain members. That gets us a sane SLP graph (still
968 : : the stmts are not 100% correct wrt the initial values). */
969 : 16284378 : else if ((dt == vect_internal_def
970 : 16284378 : || dt == vect_reduction_def)
971 : 15379234 : && oprnd_info->first_dt == vect_reduction_def
972 : 64647 : && !STMT_VINFO_DATA_REF (stmt_info)
973 : 64647 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
974 : 2754 : && !STMT_VINFO_DATA_REF (def_stmt_info)
975 : 16287132 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
976 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
977 : : {
978 : 2754 : oprnd_info->def_stmts[stmt_num] = oprnd_info->def_stmts[0];
979 : 2754 : oprnd_info->ops[stmt_num] = oprnd_info->ops[0];
980 : : }
981 : :
982 : 16631255 : ++i;
983 : : }
984 : :
985 : : /* Swap operands. */
986 : 8996793 : if (swapped)
987 : : {
988 : 42503 : if (dump_enabled_p ())
989 : 413 : dump_printf_loc (MSG_NOTE, vect_location,
990 : : "swapped operands to match def types in %G",
991 : : stmt_info->stmt);
992 : : }
993 : :
994 : : return 0;
995 : : }
996 : :
997 : : /* Return true if call statements CALL1 and CALL2 are similar enough
998 : : to be combined into the same SLP group. */
999 : :
1000 : : bool
1001 : 20906 : compatible_calls_p (gcall *call1, gcall *call2, bool allow_two_operators)
1002 : : {
1003 : 20906 : unsigned int nargs = gimple_call_num_args (call1);
1004 : 20906 : if (nargs != gimple_call_num_args (call2))
1005 : : return false;
1006 : :
1007 : 19031 : auto cfn1 = gimple_call_combined_fn (call1);
1008 : 19031 : auto cfn2 = gimple_call_combined_fn (call2);
1009 : 19031 : if (cfn1 != cfn2
1010 : 2 : && (!allow_two_operators
1011 : 2 : || !((cfn1 == CFN_FMA || cfn1 == CFN_FMS)
1012 : 2 : && (cfn2 == CFN_FMA || cfn2 == CFN_FMS))))
1013 : : return false;
1014 : :
1015 : 19031 : if (gimple_call_internal_p (call1))
1016 : : {
1017 : 7077 : if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
1018 : 7077 : TREE_TYPE (gimple_call_lhs (call2))))
1019 : : return false;
1020 : 14361 : for (unsigned int i = 0; i < nargs; ++i)
1021 : 7284 : if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
1022 : 7284 : TREE_TYPE (gimple_call_arg (call2, i))))
1023 : : return false;
1024 : : }
1025 : : else
1026 : : {
1027 : 11954 : if (!operand_equal_p (gimple_call_fn (call1),
1028 : 11954 : gimple_call_fn (call2), 0))
1029 : : return false;
1030 : :
1031 : 25872 : if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
1032 : : return false;
1033 : : }
1034 : :
1035 : : /* Check that any unvectorized arguments are equal. */
1036 : 15701 : if (const int *map = vect_get_operand_map (call1))
1037 : : {
1038 : 15 : unsigned int nkept = *map++;
1039 : 15 : unsigned int mapi = 0;
1040 : 57 : for (unsigned int i = 0; i < nargs; ++i)
1041 : 42 : if (mapi < nkept && map[mapi] == int (i))
1042 : 27 : mapi += 1;
1043 : 15 : else if (!operand_equal_p (gimple_call_arg (call1, i),
1044 : 15 : gimple_call_arg (call2, i)))
1045 : : return false;
1046 : : }
1047 : :
1048 : : return true;
1049 : : }
1050 : :
1051 : : /* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
1052 : : caller's attempt to find the vector type in STMT_INFO with the narrowest
1053 : : element type. Return true if VECTYPE is nonnull and if it is valid
1054 : : for STMT_INFO. When returning true, update MAX_NUNITS to reflect the
1055 : : number of units in VECTYPE. GROUP_SIZE and MAX_NUNITS are as for
1056 : : vect_build_slp_tree. */
1057 : :
1058 : : static bool
1059 : 4920375 : vect_record_max_nunits (vec_info *vinfo, stmt_vec_info stmt_info,
1060 : : unsigned int group_size,
1061 : : tree vectype, poly_uint64 *max_nunits)
1062 : : {
1063 : 4920375 : if (!vectype)
1064 : : {
1065 : 4349 : if (dump_enabled_p ())
1066 : 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1067 : : "Build SLP failed: unsupported data-type in %G\n",
1068 : : stmt_info->stmt);
1069 : : /* Fatal mismatch. */
1070 : 4349 : return false;
1071 : : }
1072 : :
1073 : : /* If populating the vector type requires unrolling then fail
1074 : : before adjusting *max_nunits for basic-block vectorization. */
1075 : 4916026 : if (is_a <bb_vec_info> (vinfo)
1076 : 4916026 : && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
1077 : : {
1078 : 142227 : if (dump_enabled_p ())
1079 : 34 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1080 : : "Build SLP failed: unrolling required "
1081 : : "in basic block SLP\n");
1082 : : /* Fatal mismatch. */
1083 : 142227 : return false;
1084 : : }
1085 : :
1086 : : /* In case of multiple types we need to detect the smallest type. */
1087 : 4773799 : vect_update_max_nunits (max_nunits, vectype);
1088 : 4773799 : return true;
1089 : : }
1090 : :
1091 : : /* Verify if the scalar stmts STMTS are isomorphic, require data
1092 : : permutation or are of unsupported types of operation. Return
1093 : : true if they are, otherwise return false and indicate in *MATCHES
1094 : : which stmts are not isomorphic to the first one. If MATCHES[0]
1095 : : is false then this indicates the comparison could not be
1096 : : carried out or the stmts will never be vectorized by SLP.
1097 : :
1098 : : Note COND_EXPR is possibly isomorphic to another one after swapping its
1099 : : operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
1100 : : the first stmt by swapping the two operands of comparison; set SWAP[i]
1101 : : to 2 if stmt I is isormorphic to the first stmt by inverting the code
1102 : : of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
1103 : : to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
1104 : :
1105 : : static bool
1106 : 5245781 : vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
1107 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1108 : : poly_uint64 *max_nunits, bool *matches,
1109 : : bool *two_operators, tree *node_vectype)
1110 : : {
1111 : 5245781 : unsigned int i;
1112 : 5245781 : stmt_vec_info first_stmt_info = stmts[0];
1113 : 5245781 : code_helper first_stmt_code = ERROR_MARK;
1114 : 5245781 : code_helper alt_stmt_code = ERROR_MARK;
1115 : 5245781 : code_helper first_cond_code = ERROR_MARK;
1116 : 5245781 : bool need_same_oprnds = false;
1117 : 5245781 : tree first_lhs = NULL_TREE;
1118 : 5245781 : tree first_op1 = NULL_TREE;
1119 : 5245781 : stmt_vec_info first_load = NULL, prev_first_load = NULL;
1120 : 5245781 : bool first_stmt_ldst_p = false, first_stmt_ldst_masklen_p = false;
1121 : 5245781 : bool first_stmt_phi_p = false;
1122 : 5245781 : int first_reduc_idx = -1;
1123 : 5245781 : bool maybe_soft_fail = false;
1124 : 5245781 : tree soft_fail_nunits_vectype = NULL_TREE;
1125 : :
1126 : 5245781 : tree vectype, nunits_vectype;
1127 : 5245781 : if (!vect_get_vector_types_for_stmt (vinfo, first_stmt_info, &vectype,
1128 : : &nunits_vectype, group_size))
1129 : : {
1130 : : /* Fatal mismatch. */
1131 : 198142 : matches[0] = false;
1132 : 198142 : return false;
1133 : : }
1134 : 5047639 : if (is_a <bb_vec_info> (vinfo)
1135 : 5047639 : && known_le (TYPE_VECTOR_SUBPARTS (vectype), 1U))
1136 : : {
1137 : 357983 : if (dump_enabled_p ())
1138 : 279 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1139 : : "Build SLP failed: not using single lane "
1140 : : "vector type %T\n", vectype);
1141 : 357983 : matches[0] = false;
1142 : 357983 : return false;
1143 : : }
1144 : : /* Record nunits required but continue analysis, producing matches[]
1145 : : as if nunits was not an issue. This allows splitting of groups
1146 : : to happen. */
1147 : 4689656 : if (nunits_vectype
1148 : 4689656 : && !vect_record_max_nunits (vinfo, first_stmt_info, group_size,
1149 : : nunits_vectype, max_nunits))
1150 : : {
1151 : 142227 : gcc_assert (is_a <bb_vec_info> (vinfo));
1152 : 142227 : maybe_soft_fail = true;
1153 : 142227 : soft_fail_nunits_vectype = nunits_vectype;
1154 : : }
1155 : :
1156 : 4689656 : gcc_assert (vectype || !gimple_get_lhs (first_stmt_info->stmt));
1157 : 4689656 : *node_vectype = vectype;
1158 : :
1159 : : /* For every stmt in NODE find its def stmt/s. */
1160 : 4689656 : stmt_vec_info stmt_info;
1161 : 20988897 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
1162 : : {
1163 : 16458393 : bool ldst_p = false;
1164 : 16458393 : bool ldst_masklen_p = false;
1165 : 16458393 : bool phi_p = false;
1166 : 16458393 : code_helper rhs_code = ERROR_MARK;
1167 : :
1168 : 16458393 : swap[i] = 0;
1169 : 16458393 : matches[i] = false;
1170 : 16458393 : if (!stmt_info)
1171 : : {
1172 : 51747 : matches[i] = true;
1173 : 16350988 : continue;
1174 : : }
1175 : :
1176 : 16406646 : gimple *stmt = stmt_info->stmt;
1177 : 16406646 : if (dump_enabled_p ())
1178 : 206469 : dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for %G", stmt);
1179 : :
1180 : : /* Fail to vectorize statements marked as unvectorizable, throw
1181 : : or are volatile. */
1182 : 16406646 : if (!STMT_VINFO_VECTORIZABLE (stmt_info)
1183 : 16217719 : || stmt_can_throw_internal (cfun, stmt)
1184 : 31915568 : || gimple_has_volatile_ops (stmt))
1185 : : {
1186 : 194394 : if (dump_enabled_p ())
1187 : 195 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1188 : : "Build SLP failed: unvectorizable statement %G",
1189 : : stmt);
1190 : : /* ??? For BB vectorization we want to commutate operands in a way
1191 : : to shuffle all unvectorizable defs into one operand and have
1192 : : the other still vectorized. The following doesn't reliably
1193 : : work for this though but it's the easiest we can do here. */
1194 : 194394 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1195 : 62178 : continue;
1196 : : /* Fatal mismatch. */
1197 : 132216 : matches[0] = false;
1198 : 132216 : return false;
1199 : : }
1200 : :
1201 : 16212252 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
1202 : 16212252 : tree lhs = gimple_get_lhs (stmt);
1203 : 16212252 : if (lhs == NULL_TREE && !call_stmt)
1204 : : {
1205 : 36 : if (dump_enabled_p ())
1206 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1207 : : "Build SLP failed: not GIMPLE_ASSIGN nor "
1208 : : "GIMPLE_CALL %G", stmt);
1209 : 36 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1210 : 36 : continue;
1211 : : /* Fatal mismatch. */
1212 : 0 : matches[0] = false;
1213 : 0 : return false;
1214 : : }
1215 : :
1216 : 16212216 : if (call_stmt)
1217 : : {
1218 : 89190 : combined_fn cfn = gimple_call_combined_fn (call_stmt);
1219 : 89190 : if (cfn != CFN_LAST && cfn != CFN_MASK_CALL)
1220 : 48187 : rhs_code = cfn;
1221 : : else
1222 : : rhs_code = CALL_EXPR;
1223 : :
1224 : 89190 : if (cfn == CFN_GATHER_LOAD
1225 : 89190 : || cfn == CFN_SCATTER_STORE)
1226 : : ldst_p = true;
1227 : : else if (cfn == CFN_MASK_LOAD
1228 : : || cfn == CFN_MASK_GATHER_LOAD
1229 : : || cfn == CFN_MASK_LEN_GATHER_LOAD
1230 : : || cfn == CFN_MASK_SCATTER_STORE
1231 : : || cfn == CFN_MASK_LEN_SCATTER_STORE)
1232 : : {
1233 : : ldst_p = true;
1234 : : ldst_masklen_p = true;
1235 : : }
1236 : : else if (cfn == CFN_MASK_STORE)
1237 : : {
1238 : : ldst_p = true;
1239 : : ldst_masklen_p = true;
1240 : : rhs_code = CFN_MASK_STORE;
1241 : : }
1242 : : else if (cfn == CFN_GOMP_SIMD_LANE)
1243 : : ;
1244 : 80923 : else if ((cfn != CFN_LAST
1245 : : && cfn != CFN_MASK_CALL
1246 : 39920 : && internal_fn_p (cfn)
1247 : 30994 : && !vectorizable_internal_fn_p (as_internal_fn (cfn)))
1248 : 80849 : || gimple_call_tail_p (call_stmt)
1249 : 80849 : || gimple_call_noreturn_p (call_stmt)
1250 : 161772 : || gimple_call_chain (call_stmt))
1251 : : {
1252 : 423 : if (dump_enabled_p ())
1253 : 13 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1254 : : "Build SLP failed: unsupported call type %G",
1255 : : (gimple *) call_stmt);
1256 : 423 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1257 : 66 : continue;
1258 : : /* Fatal mismatch. */
1259 : 357 : matches[0] = false;
1260 : 357 : return false;
1261 : : }
1262 : : }
1263 : 16123026 : else if (gimple_code (stmt) == GIMPLE_PHI)
1264 : : {
1265 : : rhs_code = ERROR_MARK;
1266 : : phi_p = true;
1267 : : }
1268 : : else
1269 : : {
1270 : 15414229 : rhs_code = gimple_assign_rhs_code (stmt);
1271 : 15414229 : ldst_p = STMT_VINFO_DATA_REF (stmt_info) != nullptr;
1272 : : }
1273 : :
1274 : : /* Check the operation. */
1275 : 16211793 : if (i == 0)
1276 : : {
1277 : 4557083 : first_lhs = lhs;
1278 : 4557083 : first_stmt_code = rhs_code;
1279 : 4557083 : first_stmt_ldst_p = ldst_p;
1280 : 4557083 : first_stmt_ldst_masklen_p = ldst_masklen_p;
1281 : 4557083 : first_stmt_phi_p = phi_p;
1282 : 4557083 : first_reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
1283 : :
1284 : : /* Shift arguments should be equal in all the packed stmts for a
1285 : : vector shift with scalar shift operand. */
1286 : 4557083 : if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
1287 : 4444659 : || rhs_code == LROTATE_EXPR
1288 : 9001700 : || rhs_code == RROTATE_EXPR)
1289 : : {
1290 : : /* First see if we have a vector/vector shift. */
1291 : 112668 : if (!directly_supported_p (rhs_code, vectype, optab_vector))
1292 : : {
1293 : : /* No vector/vector shift, try for a vector/scalar shift. */
1294 : 105588 : if (!directly_supported_p (rhs_code, vectype, optab_scalar))
1295 : : {
1296 : 7809 : if (dump_enabled_p ())
1297 : 372 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1298 : : "Build SLP failed: "
1299 : : "op not supported by target.\n");
1300 : 7809 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1301 : : continue;
1302 : : /* Fatal mismatch. */
1303 : 7809 : matches[0] = false;
1304 : 7809 : return false;
1305 : : }
1306 : 97779 : need_same_oprnds = true;
1307 : 97779 : first_op1 = gimple_assign_rhs2 (stmt);
1308 : : }
1309 : : }
1310 : 4444415 : else if (rhs_code == WIDEN_LSHIFT_EXPR)
1311 : : {
1312 : 0 : need_same_oprnds = true;
1313 : 0 : first_op1 = gimple_assign_rhs2 (stmt);
1314 : : }
1315 : 4444415 : else if (!ldst_p
1316 : 4444415 : && rhs_code == BIT_FIELD_REF)
1317 : : {
1318 : 5588 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1319 : 5588 : if (!is_a <bb_vec_info> (vinfo)
1320 : 5462 : || TREE_CODE (vec) != SSA_NAME
1321 : : /* When the element types are not compatible we pun the
1322 : : source to the target vectype which requires equal size. */
1323 : 11038 : || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
1324 : 4735 : || !types_compatible_p (TREE_TYPE (vectype),
1325 : 4735 : TREE_TYPE (TREE_TYPE (vec))))
1326 : 1133 : && !operand_equal_p (TYPE_SIZE (vectype),
1327 : 1133 : TYPE_SIZE (TREE_TYPE (vec)))))
1328 : : {
1329 : 865 : if (dump_enabled_p ())
1330 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1331 : : "Build SLP failed: "
1332 : : "BIT_FIELD_REF not supported\n");
1333 : : /* Fatal mismatch. */
1334 : 865 : matches[0] = false;
1335 : 865 : return false;
1336 : : }
1337 : : }
1338 : 4438827 : else if (rhs_code == CFN_DIV_POW2)
1339 : : {
1340 : 0 : need_same_oprnds = true;
1341 : 0 : first_op1 = gimple_call_arg (call_stmt, 1);
1342 : : }
1343 : 4438827 : else if (rhs_code == CFN_GOMP_SIMD_LANE)
1344 : : {
1345 : 3153 : need_same_oprnds = true;
1346 : 3153 : first_op1 = gimple_call_arg (call_stmt, 1);
1347 : : }
1348 : : }
1349 : : else
1350 : : {
1351 : 11655029 : if (first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1352 : : /* For SLP reduction groups the index isn't necessarily
1353 : : uniform but only that of the first stmt matters. */
1354 : 1614 : && !(first_reduc_idx != -1
1355 : 1614 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1356 : 1614 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info))
1357 : 11654710 : && !(first_reduc_idx != -1
1358 : 882 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1359 : 882 : && rhs_code.is_tree_code ()
1360 : 882 : && commutative_tree_code (tree_code (rhs_code))
1361 : 701 : && first_reduc_idx == 1 - STMT_VINFO_REDUC_IDX (stmt_info)))
1362 : : {
1363 : 319 : if (dump_enabled_p ())
1364 : : {
1365 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1366 : : "Build SLP failed: different reduc_idx "
1367 : : "%d instead of %d in %G",
1368 : : STMT_VINFO_REDUC_IDX (stmt_info),
1369 : : first_reduc_idx, stmt);
1370 : : }
1371 : : /* Mismatch. */
1372 : 319 : continue;
1373 : : }
1374 : 11654391 : if (!ldst_p
1375 : 9198532 : && first_stmt_code != rhs_code
1376 : 13053566 : && alt_stmt_code == ERROR_MARK)
1377 : : alt_stmt_code = rhs_code;
1378 : 13030755 : if ((!ldst_p
1379 : 9198532 : && first_stmt_code != rhs_code
1380 : 1399175 : && (first_stmt_code != IMAGPART_EXPR
1381 : 135 : || rhs_code != REALPART_EXPR)
1382 : 1399147 : && (first_stmt_code != REALPART_EXPR
1383 : 450 : || rhs_code != IMAGPART_EXPR)
1384 : : /* Handle mismatches in plus/minus by computing both
1385 : : and merging the results. */
1386 : 1399144 : && !((((first_stmt_code == PLUS_EXPR
1387 : 1303869 : || first_stmt_code == MINUS_EXPR)
1388 : 114756 : && (alt_stmt_code == PLUS_EXPR
1389 : 106067 : || alt_stmt_code == MINUS_EXPR))
1390 : 1377042 : || ((first_stmt_code == CFN_FMA
1391 : 1377040 : || first_stmt_code == CFN_FMS)
1392 : 2 : && (alt_stmt_code == CFN_FMA
1393 : 2 : || alt_stmt_code == CFN_FMS)))
1394 : 22104 : && rhs_code == alt_stmt_code)
1395 : 1419944 : && !(first_stmt_code.is_tree_code ()
1396 : 1297300 : && rhs_code.is_tree_code ()
1397 : 1203893 : && (TREE_CODE_CLASS (tree_code (first_stmt_code))
1398 : : == tcc_comparison)
1399 : 142755 : && (swap_tree_comparison (tree_code (first_stmt_code))
1400 : 142755 : == tree_code (rhs_code))
1401 : : && (first_reduc_idx == -1
1402 : 0 : || REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
1403 : : || (ldst_p
1404 : 4911718 : && (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1405 : 2455859 : != STMT_VINFO_GROUPED_ACCESS (first_stmt_info)))
1406 : : || (ldst_p
1407 : 2413976 : && (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1408 : 2413976 : != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info)))
1409 : 10278169 : || first_stmt_ldst_p != ldst_p
1410 : 10278035 : || (ldst_p && first_stmt_ldst_masklen_p != ldst_masklen_p)
1411 : 21932418 : || first_stmt_phi_p != phi_p)
1412 : : {
1413 : 1376364 : if (dump_enabled_p ())
1414 : : {
1415 : 2777 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1416 : : "Build SLP failed: different operation "
1417 : : "in stmt %G", stmt);
1418 : 2777 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1419 : : "original stmt %G", first_stmt_info->stmt);
1420 : : }
1421 : : /* Mismatch. */
1422 : 1376364 : continue;
1423 : : }
1424 : :
1425 : 10280366 : if (!ldst_p
1426 : 7864181 : && first_stmt_code == BIT_FIELD_REF
1427 : 10283567 : && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
1428 : 5540 : != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
1429 : : {
1430 : 2339 : if (dump_enabled_p ())
1431 : 40 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1432 : : "Build SLP failed: different BIT_FIELD_REF "
1433 : : "arguments in %G", stmt);
1434 : : /* Mismatch. */
1435 : 2339 : continue;
1436 : : }
1437 : :
1438 : 10275688 : if (call_stmt
1439 : 21033 : && first_stmt_code != CFN_MASK_LOAD
1440 : 10296656 : && first_stmt_code != CFN_MASK_STORE)
1441 : : {
1442 : 20906 : if (!is_a <gcall *> (stmts[0]->stmt)
1443 : 20906 : || !compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
1444 : : call_stmt, true))
1445 : : {
1446 : 5205 : if (dump_enabled_p ())
1447 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1448 : : "Build SLP failed: different calls in %G",
1449 : : stmt);
1450 : : /* Mismatch. */
1451 : 5205 : continue;
1452 : : }
1453 : : }
1454 : :
1455 : 10098517 : if ((phi_p || gimple_could_trap_p (stmt_info->stmt))
1456 : 10967543 : && (gimple_bb (first_stmt_info->stmt)
1457 : 869026 : != gimple_bb (stmt_info->stmt)))
1458 : : {
1459 : 27658 : if (dump_enabled_p ())
1460 : 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1461 : : "Build SLP failed: different BB for PHI "
1462 : : "or possibly trapping operation in %G", stmt);
1463 : : /* Mismatch. */
1464 : 27658 : continue;
1465 : : }
1466 : :
1467 : 10242825 : if (need_same_oprnds)
1468 : : {
1469 : 53090 : tree other_op1 = gimple_arg (stmt, 1);
1470 : 53090 : if (!operand_equal_p (first_op1, other_op1, 0))
1471 : : {
1472 : 7191 : if (dump_enabled_p ())
1473 : 123 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1474 : : "Build SLP failed: different shift "
1475 : : "arguments in %G", stmt);
1476 : : /* Mismatch. */
1477 : 7191 : continue;
1478 : : }
1479 : : }
1480 : :
1481 : 10236347 : if (first_lhs
1482 : 10235634 : && lhs
1483 : 10235634 : && !types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (first_lhs)))
1484 : : {
1485 : 713 : if (dump_enabled_p ())
1486 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1487 : : "Build SLP failed: different vector type "
1488 : : "in %G", stmt);
1489 : : /* Mismatch. */
1490 : 713 : continue;
1491 : : }
1492 : : }
1493 : :
1494 : : /* Grouped store or load. */
1495 : 14783330 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1496 : : {
1497 : 3782256 : gcc_assert (ldst_p);
1498 : 3782256 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1499 : : {
1500 : : /* Store. */
1501 : 2984621 : gcc_assert (rhs_code == CFN_MASK_STORE
1502 : : || REFERENCE_CLASS_P (lhs)
1503 : : || DECL_P (lhs));
1504 : : }
1505 : : else
1506 : : {
1507 : : /* Load. */
1508 : 797635 : first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
1509 : 797635 : if (prev_first_load)
1510 : : {
1511 : : /* Check that there are no loads from different interleaving
1512 : : chains in the same node. */
1513 : 354088 : if (prev_first_load != first_load)
1514 : : {
1515 : 43083 : if (dump_enabled_p ())
1516 : 1904 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1517 : : vect_location,
1518 : : "Build SLP failed: different "
1519 : : "interleaving chains in one node %G",
1520 : : stmt);
1521 : : /* Mismatch. */
1522 : 43083 : continue;
1523 : : }
1524 : : }
1525 : : else
1526 : : prev_first_load = first_load;
1527 : : }
1528 : : }
1529 : : /* Non-grouped store or load. */
1530 : 11001074 : else if (ldst_p)
1531 : : {
1532 : 694562 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
1533 : 480511 : && rhs_code != CFN_GATHER_LOAD
1534 : : && rhs_code != CFN_MASK_GATHER_LOAD
1535 : : && rhs_code != CFN_MASK_LEN_GATHER_LOAD
1536 : : && rhs_code != CFN_SCATTER_STORE
1537 : : && rhs_code != CFN_MASK_SCATTER_STORE
1538 : : && rhs_code != CFN_MASK_LEN_SCATTER_STORE
1539 : 480511 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1540 : : /* Not grouped loads are handled as externals for BB
1541 : : vectorization. For loop vectorization we can handle
1542 : : splats the same we handle single element interleaving.
1543 : : Likewise we can handle a collection of invariant refs. */
1544 : 1162484 : && (is_a <bb_vec_info> (vinfo)
1545 : 467922 : || (stmt_info != first_stmt_info
1546 : 44532 : && !(integer_zerop (DR_STEP (STMT_VINFO_DATA_REF (stmt_info)))
1547 : 269 : && integer_zerop (DR_STEP (STMT_VINFO_DATA_REF
1548 : : (first_stmt_info)))))))
1549 : : {
1550 : : /* Not grouped load. */
1551 : 43994 : if (dump_enabled_p ())
1552 : 121 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1553 : : "Build SLP failed: not grouped load %G", stmt);
1554 : :
1555 : 43994 : if (i != 0)
1556 : 43994 : continue;
1557 : : /* Fatal mismatch. */
1558 : 0 : matches[0] = false;
1559 : 0 : return false;
1560 : : }
1561 : : }
1562 : : /* Not memory operation. */
1563 : : else
1564 : : {
1565 : 10306512 : if (!phi_p
1566 : 9720905 : && rhs_code.is_tree_code ()
1567 : 9679576 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_binary
1568 : 1453045 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_unary
1569 : 927303 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_expression
1570 : 877981 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_comparison
1571 : 60655 : && rhs_code != VIEW_CONVERT_EXPR
1572 : : && rhs_code != CALL_EXPR
1573 : : && rhs_code != BIT_FIELD_REF
1574 : 10306512 : && rhs_code != SSA_NAME)
1575 : : {
1576 : 17905 : if (dump_enabled_p ())
1577 : 17 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1578 : : "Build SLP failed: operation unsupported %G",
1579 : : stmt);
1580 : 17905 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1581 : 0 : continue;
1582 : : /* Fatal mismatch. */
1583 : 17905 : matches[0] = false;
1584 : 17905 : return false;
1585 : : }
1586 : :
1587 : 10288607 : if (rhs_code == COND_EXPR)
1588 : : {
1589 : 46753 : tree cond_expr = gimple_assign_rhs1 (stmt);
1590 : 46753 : enum tree_code cond_code = TREE_CODE (cond_expr);
1591 : 46753 : enum tree_code swap_code = ERROR_MARK;
1592 : 46753 : enum tree_code invert_code = ERROR_MARK;
1593 : :
1594 : 46753 : if (i == 0)
1595 : 38108 : first_cond_code = TREE_CODE (cond_expr);
1596 : 8645 : else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1597 : : {
1598 : 0 : bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
1599 : 0 : swap_code = swap_tree_comparison (cond_code);
1600 : 0 : invert_code = invert_tree_comparison (cond_code, honor_nans);
1601 : : }
1602 : :
1603 : 46753 : if (first_cond_code == cond_code)
1604 : : ;
1605 : : /* Isomorphic can be achieved by swapping. */
1606 : 0 : else if (first_cond_code == swap_code)
1607 : 0 : swap[i] = 1;
1608 : : /* Isomorphic can be achieved by inverting. */
1609 : 0 : else if (first_cond_code == invert_code)
1610 : 0 : swap[i] = 2;
1611 : : else
1612 : : {
1613 : 0 : if (dump_enabled_p ())
1614 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1615 : : "Build SLP failed: different"
1616 : : " operation %G", stmt);
1617 : : /* Mismatch. */
1618 : 0 : continue;
1619 : : }
1620 : : }
1621 : :
1622 : 10288607 : if (i != 0
1623 : 7822297 : && first_stmt_code != rhs_code
1624 : 64824 : && first_stmt_code.is_tree_code ()
1625 : 64822 : && rhs_code.is_tree_code ()
1626 : 64822 : && TREE_CODE_CLASS ((tree_code)first_stmt_code) == tcc_comparison
1627 : 10331419 : && (swap_tree_comparison ((tree_code)first_stmt_code)
1628 : 42812 : == (tree_code)rhs_code))
1629 : 42812 : swap[i] = 1;
1630 : :
1631 : 10288607 : if (i != 0
1632 : 7822297 : && first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1633 : 1066 : && first_reduc_idx != -1
1634 : 1066 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1635 : 1066 : && rhs_code.is_tree_code ()
1636 : 1066 : && commutative_tree_code (tree_code (rhs_code))
1637 : 10289673 : && first_reduc_idx == 1 - STMT_VINFO_REDUC_IDX (stmt_info))
1638 : 1066 : swap[i] = 1;
1639 : : }
1640 : :
1641 : 14678348 : matches[i] = true;
1642 : : }
1643 : :
1644 : 19234499 : for (i = 0; i < group_size; ++i)
1645 : 15371524 : if (!matches[i])
1646 : : return false;
1647 : :
1648 : : /* If we allowed a two-operation SLP node verify the target can cope
1649 : : with the permute we are going to use. */
1650 : 3862975 : if (alt_stmt_code != ERROR_MARK
1651 : 3862975 : && (!alt_stmt_code.is_tree_code ()
1652 : 54089 : || (TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_reference
1653 : 54089 : && TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_comparison)))
1654 : : {
1655 : 12148 : *two_operators = true;
1656 : : }
1657 : :
1658 : 3862975 : if (maybe_soft_fail)
1659 : : {
1660 : 141838 : unsigned HOST_WIDE_INT const_nunits;
1661 : 141838 : if (!TYPE_VECTOR_SUBPARTS
1662 : 141838 : (soft_fail_nunits_vectype).is_constant (&const_nunits)
1663 : 141838 : || const_nunits > group_size)
1664 : 0 : matches[0] = false;
1665 : : else
1666 : : {
1667 : : /* With constant vector elements simulate a mismatch at the
1668 : : point we need to split. */
1669 : 141838 : unsigned tail = group_size & (const_nunits - 1);
1670 : 141838 : memset (&matches[group_size - tail], 0, sizeof (bool) * tail);
1671 : : }
1672 : 141838 : return false;
1673 : : }
1674 : :
1675 : : return true;
1676 : : }
1677 : :
1678 : : /* Traits for the hash_set to record failed SLP builds for a stmt set.
1679 : : Note we never remove apart from at destruction time so we do not
1680 : : need a special value for deleted that differs from empty. */
1681 : : struct bst_traits
1682 : : {
1683 : : typedef vec <stmt_vec_info> value_type;
1684 : : typedef vec <stmt_vec_info> compare_type;
1685 : : static inline hashval_t hash (value_type);
1686 : : static inline bool equal (value_type existing, value_type candidate);
1687 : 432739389 : static inline bool is_empty (value_type x) { return !x.exists (); }
1688 : 95829102 : static inline bool is_deleted (value_type x) { return !x.exists (); }
1689 : : static const bool empty_zero_p = true;
1690 : 0 : static inline void mark_empty (value_type &x) { x.release (); }
1691 : : static inline void mark_deleted (value_type &x) { x.release (); }
1692 : 8308105 : static inline void remove (value_type &x) { x.release (); }
1693 : : };
1694 : : inline hashval_t
1695 : 83597358 : bst_traits::hash (value_type x)
1696 : : {
1697 : 83597358 : inchash::hash h;
1698 : 398517595 : for (unsigned i = 0; i < x.length (); ++i)
1699 : 314920237 : h.add_int (x[i] ? gimple_uid (x[i]->stmt) : -1);
1700 : 83597358 : return h.end ();
1701 : : }
1702 : : inline bool
1703 : 72784991 : bst_traits::equal (value_type existing, value_type candidate)
1704 : : {
1705 : 218354973 : if (existing.length () != candidate.length ())
1706 : : return false;
1707 : 73855304 : for (unsigned i = 0; i < existing.length (); ++i)
1708 : 70040419 : if (existing[i] != candidate[i])
1709 : : return false;
1710 : : return true;
1711 : : }
1712 : :
1713 : : typedef hash_map <vec <stmt_vec_info>, slp_tree,
1714 : : simple_hashmap_traits <bst_traits, slp_tree> >
1715 : : scalar_stmts_to_slp_tree_map_t;
1716 : :
1717 : : /* Release BST_MAP. */
1718 : :
1719 : : static void
1720 : 1669084 : release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map)
1721 : : {
1722 : : /* The map keeps a reference on SLP nodes built, release that. */
1723 : 9977189 : for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin ();
1724 : 18285294 : it != bst_map->end (); ++it)
1725 : 8308105 : if ((*it).second)
1726 : 8308105 : vect_free_slp_tree ((*it).second);
1727 : 1669084 : delete bst_map;
1728 : 1669084 : }
1729 : :
1730 : : /* ??? This was std::pair<std::pair<tree_code, vect_def_type>, tree>
1731 : : but then vec::insert does memmove and that's not compatible with
1732 : : std::pair. */
1733 : : struct chain_op_t
1734 : : {
1735 : 3729955 : chain_op_t (tree_code code_, vect_def_type dt_, tree op_)
1736 : 3729955 : : code (code_), dt (dt_), op (op_) {}
1737 : : tree_code code;
1738 : : vect_def_type dt;
1739 : : tree op;
1740 : : };
1741 : :
1742 : : /* Comparator for sorting associatable chains. */
1743 : :
1744 : : static int
1745 : 8593375 : dt_sort_cmp (const void *op1_, const void *op2_, void *)
1746 : : {
1747 : 8593375 : auto *op1 = (const chain_op_t *) op1_;
1748 : 8593375 : auto *op2 = (const chain_op_t *) op2_;
1749 : 8593375 : if (op1->dt != op2->dt)
1750 : 1048006 : return (int)op1->dt - (int)op2->dt;
1751 : 7545369 : return (int)op1->code - (int)op2->code;
1752 : : }
1753 : :
1754 : : /* Linearize the associatable expression chain at START with the
1755 : : associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
1756 : : filling CHAIN with the result and using WORKLIST as intermediate storage.
1757 : : CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
1758 : : or MINUS_EXPR. *CHAIN_STMTS if not NULL is filled with all computation
1759 : : stmts, starting with START. */
1760 : :
1761 : : static void
1762 : 1675649 : vect_slp_linearize_chain (vec_info *vinfo,
1763 : : vec<std::pair<tree_code, gimple *> > &worklist,
1764 : : vec<chain_op_t> &chain,
1765 : : enum tree_code code, gimple *start,
1766 : : gimple *&code_stmt, gimple *&alt_code_stmt,
1767 : : vec<gimple *> *chain_stmts)
1768 : : {
1769 : : /* For each lane linearize the addition/subtraction (or other
1770 : : uniform associatable operation) expression tree. */
1771 : 1675649 : worklist.safe_push (std::make_pair (code, start));
1772 : 3729955 : while (!worklist.is_empty ())
1773 : : {
1774 : 2054306 : auto entry = worklist.pop ();
1775 : 2054306 : gassign *stmt = as_a <gassign *> (entry.second);
1776 : 2054306 : enum tree_code in_code = entry.first;
1777 : 4108612 : enum tree_code this_code = gimple_assign_rhs_code (stmt);
1778 : : /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE. */
1779 : 2054306 : if (!code_stmt
1780 : 2054306 : && gimple_assign_rhs_code (stmt) == code)
1781 : 1410240 : code_stmt = stmt;
1782 : 644066 : else if (!alt_code_stmt
1783 : 644066 : && gimple_assign_rhs_code (stmt) == MINUS_EXPR)
1784 : 341279 : alt_code_stmt = stmt;
1785 : 2054306 : if (chain_stmts)
1786 : 2006192 : chain_stmts->safe_push (stmt);
1787 : 6162918 : for (unsigned opnum = 1; opnum <= 2; ++opnum)
1788 : : {
1789 : 4108612 : tree op = gimple_op (stmt, opnum);
1790 : 4108612 : vect_def_type dt;
1791 : 4108612 : stmt_vec_info def_stmt_info;
1792 : 4108612 : bool res = vect_is_simple_use (op, vinfo, &dt, &def_stmt_info);
1793 : 4108612 : gcc_assert (res);
1794 : 4108612 : if (dt == vect_internal_def
1795 : 4108612 : && is_pattern_stmt_p (def_stmt_info))
1796 : 6812 : op = gimple_get_lhs (def_stmt_info->stmt);
1797 : 4108612 : gimple *use_stmt;
1798 : 4108612 : use_operand_p use_p;
1799 : 4108612 : if (dt == vect_internal_def
1800 : 3813785 : && single_imm_use (op, &use_p, &use_stmt)
1801 : 2381415 : && is_gimple_assign (def_stmt_info->stmt)
1802 : 6296802 : && (gimple_assign_rhs_code (def_stmt_info->stmt) == code
1803 : 1835940 : || (code == PLUS_EXPR
1804 : 905348 : && (gimple_assign_rhs_code (def_stmt_info->stmt)
1805 : : == MINUS_EXPR))))
1806 : : {
1807 : 378657 : tree_code op_def_code = this_code;
1808 : 378657 : if (op_def_code == MINUS_EXPR && opnum == 1)
1809 : 56029 : op_def_code = PLUS_EXPR;
1810 : 378657 : if (in_code == MINUS_EXPR)
1811 : 193 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1812 : 378657 : worklist.safe_push (std::make_pair (op_def_code,
1813 : 378657 : def_stmt_info->stmt));
1814 : : }
1815 : : else
1816 : : {
1817 : 3729955 : tree_code op_def_code = this_code;
1818 : 3729955 : if (op_def_code == MINUS_EXPR && opnum == 1)
1819 : 289549 : op_def_code = PLUS_EXPR;
1820 : 3729955 : if (in_code == MINUS_EXPR)
1821 : 5811 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1822 : 3729955 : chain.safe_push (chain_op_t (op_def_code, dt, op));
1823 : : }
1824 : : }
1825 : : }
1826 : 1675649 : }
1827 : :
1828 : : static slp_tree
1829 : : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1830 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1831 : : poly_uint64 *max_nunits,
1832 : : bool *matches, unsigned *limit, unsigned *tree_size,
1833 : : scalar_stmts_to_slp_tree_map_t *bst_map);
1834 : :
1835 : : static slp_tree
1836 : 5640351 : vect_build_slp_tree (vec_info *vinfo,
1837 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1838 : : poly_uint64 *max_nunits,
1839 : : bool *matches, unsigned *limit, unsigned *tree_size,
1840 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1841 : : {
1842 : 5640351 : if (slp_tree *leader = bst_map->get (stmts))
1843 : : {
1844 : 388647 : if (dump_enabled_p ())
1845 : 16119 : dump_printf_loc (MSG_NOTE, vect_location, "re-using %sSLP tree %p\n",
1846 : 16119 : !(*leader)->failed ? "" : "failed ",
1847 : : (void *) *leader);
1848 : 388647 : if (!(*leader)->failed)
1849 : : {
1850 : 340732 : SLP_TREE_REF_COUNT (*leader)++;
1851 : 340732 : vect_update_max_nunits (max_nunits, (*leader)->max_nunits);
1852 : 340732 : stmts.release ();
1853 : 340732 : return *leader;
1854 : : }
1855 : 47915 : memcpy (matches, (*leader)->failed, sizeof (bool) * group_size);
1856 : 47915 : return NULL;
1857 : : }
1858 : :
1859 : : /* Single-lane SLP doesn't have the chance of run-away, do not account
1860 : : it to the limit. */
1861 : 5251704 : if (stmts.length () > 1)
1862 : : {
1863 : 3132515 : if (*limit == 0)
1864 : : {
1865 : 1501 : if (dump_enabled_p ())
1866 : 12 : dump_printf_loc (MSG_NOTE, vect_location,
1867 : : "SLP discovery limit exceeded\n");
1868 : 1501 : memset (matches, 0, sizeof (bool) * group_size);
1869 : 1501 : return NULL;
1870 : : }
1871 : 3131014 : --*limit;
1872 : : }
1873 : :
1874 : : /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
1875 : : so we can pick up backedge destinations during discovery. */
1876 : 5250203 : slp_tree res = new _slp_tree;
1877 : 5250203 : SLP_TREE_DEF_TYPE (res) = vect_internal_def;
1878 : 5250203 : SLP_TREE_SCALAR_STMTS (res) = stmts;
1879 : 5250203 : bst_map->put (stmts.copy (), res);
1880 : :
1881 : 5250203 : if (dump_enabled_p ())
1882 : 138164 : dump_printf_loc (MSG_NOTE, vect_location,
1883 : : "starting SLP discovery for node %p\n", (void *) res);
1884 : :
1885 : 5250203 : poly_uint64 this_max_nunits = 1;
1886 : 5250203 : slp_tree res_ = vect_build_slp_tree_2 (vinfo, res, stmts, group_size,
1887 : : &this_max_nunits,
1888 : : matches, limit, tree_size, bst_map);
1889 : 5250203 : if (!res_)
1890 : : {
1891 : 1958606 : if (dump_enabled_p ())
1892 : 7817 : dump_printf_loc (MSG_NOTE, vect_location,
1893 : : "SLP discovery for node %p failed\n", (void *) res);
1894 : : /* Mark the node invalid so we can detect those when still in use
1895 : : as backedge destinations. */
1896 : 1958606 : SLP_TREE_SCALAR_STMTS (res) = vNULL;
1897 : 1958606 : SLP_TREE_DEF_TYPE (res) = vect_uninitialized_def;
1898 : 1958606 : res->failed = XNEWVEC (bool, group_size);
1899 : 1958606 : if (flag_checking)
1900 : : {
1901 : : unsigned i;
1902 : 3470328 : for (i = 0; i < group_size; ++i)
1903 : 3470328 : if (!matches[i])
1904 : : break;
1905 : 1958606 : gcc_assert (i < group_size);
1906 : : }
1907 : 1958606 : memcpy (res->failed, matches, sizeof (bool) * group_size);
1908 : : }
1909 : : else
1910 : : {
1911 : 3291597 : if (dump_enabled_p ())
1912 : 130347 : dump_printf_loc (MSG_NOTE, vect_location,
1913 : : "SLP discovery for node %p succeeded\n",
1914 : : (void *) res);
1915 : 3291597 : gcc_assert (res_ == res);
1916 : 3291597 : res->max_nunits = this_max_nunits;
1917 : 3291597 : vect_update_max_nunits (max_nunits, this_max_nunits);
1918 : : /* Keep a reference for the bst_map use. */
1919 : 3291597 : SLP_TREE_REF_COUNT (res)++;
1920 : : }
1921 : : return res_;
1922 : : }
1923 : :
1924 : : /* Helper for building an associated SLP node chain. */
1925 : :
1926 : : static void
1927 : 122 : vect_slp_build_two_operator_nodes (slp_tree perm, tree vectype,
1928 : : slp_tree op0, slp_tree op1,
1929 : : stmt_vec_info oper1, stmt_vec_info oper2,
1930 : : vec<std::pair<unsigned, unsigned> > lperm)
1931 : : {
1932 : 122 : unsigned group_size = SLP_TREE_LANES (op1);
1933 : :
1934 : 122 : slp_tree child1 = new _slp_tree;
1935 : 122 : SLP_TREE_DEF_TYPE (child1) = vect_internal_def;
1936 : 122 : SLP_TREE_VECTYPE (child1) = vectype;
1937 : 122 : SLP_TREE_LANES (child1) = group_size;
1938 : 122 : SLP_TREE_CHILDREN (child1).create (2);
1939 : 122 : SLP_TREE_CHILDREN (child1).quick_push (op0);
1940 : 122 : SLP_TREE_CHILDREN (child1).quick_push (op1);
1941 : 122 : SLP_TREE_REPRESENTATIVE (child1) = oper1;
1942 : :
1943 : 122 : slp_tree child2 = new _slp_tree;
1944 : 122 : SLP_TREE_DEF_TYPE (child2) = vect_internal_def;
1945 : 122 : SLP_TREE_VECTYPE (child2) = vectype;
1946 : 122 : SLP_TREE_LANES (child2) = group_size;
1947 : 122 : SLP_TREE_CHILDREN (child2).create (2);
1948 : 122 : SLP_TREE_CHILDREN (child2).quick_push (op0);
1949 : 122 : SLP_TREE_REF_COUNT (op0)++;
1950 : 122 : SLP_TREE_CHILDREN (child2).quick_push (op1);
1951 : 122 : SLP_TREE_REF_COUNT (op1)++;
1952 : 122 : SLP_TREE_REPRESENTATIVE (child2) = oper2;
1953 : :
1954 : 122 : SLP_TREE_DEF_TYPE (perm) = vect_internal_def;
1955 : 122 : SLP_TREE_CODE (perm) = VEC_PERM_EXPR;
1956 : 122 : SLP_TREE_VECTYPE (perm) = vectype;
1957 : 122 : SLP_TREE_LANES (perm) = group_size;
1958 : : /* ??? We should set this NULL but that's not expected. */
1959 : 122 : SLP_TREE_REPRESENTATIVE (perm) = oper1;
1960 : 122 : SLP_TREE_LANE_PERMUTATION (perm) = lperm;
1961 : 122 : SLP_TREE_CHILDREN (perm).quick_push (child1);
1962 : 122 : SLP_TREE_CHILDREN (perm).quick_push (child2);
1963 : 122 : }
1964 : :
1965 : : /* Recursively build an SLP tree starting from NODE.
1966 : : Fail (and return a value not equal to zero) if def-stmts are not
1967 : : isomorphic, require data permutation or are of unsupported types of
1968 : : operation. Otherwise, return 0.
1969 : : The value returned is the depth in the SLP tree where a mismatch
1970 : : was found. */
1971 : :
1972 : : static slp_tree
1973 : 5250203 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1974 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1975 : : poly_uint64 *max_nunits,
1976 : : bool *matches, unsigned *limit, unsigned *tree_size,
1977 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1978 : : {
1979 : 5250203 : unsigned nops, i, this_tree_size = 0;
1980 : 5250203 : poly_uint64 this_max_nunits = *max_nunits;
1981 : :
1982 : 5250203 : matches[0] = false;
1983 : :
1984 : 5250203 : stmt_vec_info stmt_info = stmts[0];
1985 : 5250203 : if (!is_a<gcall *> (stmt_info->stmt)
1986 : : && !is_a<gassign *> (stmt_info->stmt)
1987 : : && !is_a<gphi *> (stmt_info->stmt))
1988 : : return NULL;
1989 : :
1990 : 5250132 : nops = gimple_num_args (stmt_info->stmt);
1991 : 5250132 : if (const int *map = vect_get_operand_map (stmt_info->stmt,
1992 : 5250132 : STMT_VINFO_GATHER_SCATTER_P
1993 : : (stmt_info)))
1994 : 22816 : nops = map[0];
1995 : :
1996 : : /* If the SLP node is a PHI (induction or reduction), terminate
1997 : : the recursion. */
1998 : 5250132 : bool *skip_args = XALLOCAVEC (bool, nops);
1999 : 5250132 : memset (skip_args, 0, sizeof (bool) * nops);
2000 : 5250132 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
2001 : 2273837 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
2002 : : {
2003 : 230739 : tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
2004 : 230739 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
2005 : : group_size);
2006 : 230739 : if (!vect_record_max_nunits (vinfo, stmt_info, group_size, vectype,
2007 : : max_nunits))
2008 : : return NULL;
2009 : :
2010 : 226390 : vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
2011 : 226390 : if (def_type == vect_induction_def)
2012 : : {
2013 : : /* Induction PHIs are not cycles but walk the initial
2014 : : value. Only for inner loops through, for outer loops
2015 : : we need to pick up the value from the actual PHIs
2016 : : to more easily support peeling and epilogue vectorization. */
2017 : 152581 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2018 : 152581 : if (!nested_in_vect_loop_p (loop, stmt_info))
2019 : 151937 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2020 : : else
2021 : : loop = loop->inner;
2022 : 152581 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2023 : : }
2024 : 73809 : else if (def_type == vect_reduction_def
2025 : : || def_type == vect_double_reduction_def
2026 : : || def_type == vect_nested_cycle
2027 : 73809 : || def_type == vect_first_order_recurrence)
2028 : : {
2029 : : /* Else def types have to match. */
2030 : : stmt_vec_info other_info;
2031 : : bool all_same = true;
2032 : 163817 : FOR_EACH_VEC_ELT (stmts, i, other_info)
2033 : : {
2034 : 91051 : if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
2035 : 1737035 : return NULL;
2036 : 91049 : if (other_info != stmt_info)
2037 : 15652 : all_same = false;
2038 : : }
2039 : 72766 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2040 : : /* Reduction initial values are not explicitely represented. */
2041 : 72766 : if (def_type != vect_first_order_recurrence
2042 : 72766 : && gimple_bb (stmt_info->stmt) == loop->header)
2043 : 70007 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
2044 : : /* Reduction chain backedge defs are filled manually.
2045 : : ??? Need a better way to identify a SLP reduction chain PHI.
2046 : : Or a better overall way to SLP match those. */
2047 : 72766 : if (stmts.length () > 1
2048 : 72766 : && all_same && def_type == vect_reduction_def)
2049 : 1407 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2050 : : }
2051 : 1041 : else if (def_type != vect_internal_def)
2052 : : return NULL;
2053 : : }
2054 : :
2055 : :
2056 : 5245781 : bool two_operators = false;
2057 : 5245781 : unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
2058 : 5245781 : tree vectype = NULL_TREE;
2059 : 5245781 : if (!vect_build_slp_tree_1 (vinfo, swap, stmts, group_size,
2060 : : &this_max_nunits, matches, &two_operators,
2061 : : &vectype))
2062 : : return NULL;
2063 : :
2064 : : /* If the SLP node is a load, terminate the recursion unless masked. */
2065 : 3721137 : if (STMT_VINFO_DATA_REF (stmt_info)
2066 : 1854834 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2067 : : {
2068 : 794723 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2069 : : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
2070 : : else
2071 : : {
2072 : 782371 : *max_nunits = this_max_nunits;
2073 : 782371 : (*tree_size)++;
2074 : 782371 : node = vect_create_new_slp_node (node, stmts, 0);
2075 : 782371 : SLP_TREE_VECTYPE (node) = vectype;
2076 : : /* And compute the load permutation. Whether it is actually
2077 : : a permutation depends on the unrolling factor which is
2078 : : decided later. */
2079 : 782371 : vec<unsigned> load_permutation;
2080 : 782371 : int j;
2081 : 782371 : stmt_vec_info load_info;
2082 : 782371 : load_permutation.create (group_size);
2083 : 782371 : stmt_vec_info first_stmt_info
2084 : 782371 : = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2085 : 782371 : ? DR_GROUP_FIRST_ELEMENT (stmt_info) : stmt_info;
2086 : 782371 : bool any_permute = false;
2087 : 1920974 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
2088 : : {
2089 : 1138603 : int load_place;
2090 : 1138603 : if (! load_info)
2091 : : {
2092 : 51747 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2093 : : load_place = j;
2094 : : else
2095 : : load_place = 0;
2096 : : }
2097 : 1086856 : else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2098 : 680263 : load_place = vect_get_place_in_interleaving_chain
2099 : 680263 : (load_info, first_stmt_info);
2100 : : else
2101 : : /* Recognize the splat case as { 0, 0, ... } but make
2102 : : sure to use the appropriate refs for collections
2103 : : of invariant refs. */
2104 : 406593 : load_place = (load_info == stmt_info) ? 0 : j;
2105 : 732279 : gcc_assert (load_place != -1);
2106 : 1138603 : any_permute |= load_place != j;
2107 : 1138603 : load_permutation.quick_push (load_place);
2108 : : }
2109 : :
2110 : 782371 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
2111 : : {
2112 : 2260 : gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD));
2113 : 2260 : bool has_gaps = false;
2114 : 2260 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2115 : 119 : for (stmt_vec_info si = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
2116 : 196 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2117 : 77 : if (DR_GROUP_GAP (si) != 1)
2118 : 20 : has_gaps = true;
2119 : : /* We cannot handle permuted masked loads directly, see
2120 : : PR114375. We cannot handle strided masked loads or masked
2121 : : loads with gaps unless the mask is uniform. */
2122 : 2260 : if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
2123 : 119 : && (DR_GROUP_GAP (first_stmt_info) != 0
2124 : 59 : || (has_gaps
2125 : 20 : && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))))
2126 : 4440 : || STMT_VINFO_STRIDED_P (stmt_info))
2127 : : {
2128 : 93 : load_permutation.release ();
2129 : 93 : matches[0] = false;
2130 : 780228 : return NULL;
2131 : : }
2132 : :
2133 : : /* For permuted masked loads do an unpermuted masked load of
2134 : : the whole group followed by a SLP permute node. */
2135 : 2167 : if (any_permute
2136 : 2167 : || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
2137 : 25 : && DR_GROUP_SIZE (first_stmt_info) != group_size))
2138 : : {
2139 : : /* Discover the whole unpermuted load. */
2140 : 24 : vec<stmt_vec_info> stmts2;
2141 : 24 : unsigned dr_group_size = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2142 : 38 : ? DR_GROUP_SIZE (first_stmt_info) : 1;
2143 : 24 : stmts2.create (dr_group_size);
2144 : 24 : stmts2.quick_grow_cleared (dr_group_size);
2145 : 24 : unsigned i = 0;
2146 : 24 : for (stmt_vec_info si = first_stmt_info;
2147 : 74 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2148 : : {
2149 : 50 : if (si != first_stmt_info)
2150 : 26 : for (unsigned k = 1; k < DR_GROUP_GAP (si); ++k)
2151 : 0 : stmts2[i++] = NULL;
2152 : 50 : stmts2[i++] = si;
2153 : : }
2154 : 24 : bool *matches2 = XALLOCAVEC (bool, dr_group_size);
2155 : 24 : slp_tree unperm_load
2156 : 24 : = vect_build_slp_tree (vinfo, stmts2, dr_group_size,
2157 : : &this_max_nunits, matches2, limit,
2158 : 24 : &this_tree_size, bst_map);
2159 : : /* When we are able to do the full masked load emit that
2160 : : followed by 'node' being the desired final permutation. */
2161 : 24 : if (unperm_load)
2162 : : {
2163 : 16 : gcc_assert
2164 : : (!SLP_TREE_LOAD_PERMUTATION (unperm_load).exists ());
2165 : 16 : lane_permutation_t lperm;
2166 : 16 : lperm.create (group_size);
2167 : 56 : for (unsigned j = 0; j < load_permutation.length (); ++j)
2168 : 40 : lperm.quick_push
2169 : 40 : (std::make_pair (0, load_permutation[j]));
2170 : 16 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2171 : 16 : SLP_TREE_CHILDREN (node).safe_push (unperm_load);
2172 : 16 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2173 : 16 : load_permutation.release ();
2174 : 16 : return node;
2175 : : }
2176 : 8 : stmts2.release ();
2177 : 8 : load_permutation.release ();
2178 : 8 : matches[0] = false;
2179 : 8 : return NULL;
2180 : : }
2181 : 2143 : load_permutation.release ();
2182 : : }
2183 : : else
2184 : : {
2185 : 780111 : if (!any_permute
2186 : 670434 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2187 : 1060443 : && group_size == DR_GROUP_SIZE (first_stmt_info))
2188 : 131961 : load_permutation.release ();
2189 : 780111 : SLP_TREE_LOAD_PERMUTATION (node) = load_permutation;
2190 : 780111 : return node;
2191 : : }
2192 : : }
2193 : : }
2194 : 2926414 : else if (gimple_assign_single_p (stmt_info->stmt)
2195 : 2126564 : && !gimple_vuse (stmt_info->stmt)
2196 : 2933988 : && gimple_assign_rhs_code (stmt_info->stmt) == BIT_FIELD_REF)
2197 : : {
2198 : : /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
2199 : : the same SSA name vector of a compatible type to vectype. */
2200 : 2218 : vec<std::pair<unsigned, unsigned> > lperm = vNULL;
2201 : 2218 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
2202 : 2218 : stmt_vec_info estmt_info;
2203 : 6956 : FOR_EACH_VEC_ELT (stmts, i, estmt_info)
2204 : : {
2205 : 4885 : gassign *estmt = as_a <gassign *> (estmt_info->stmt);
2206 : 4885 : tree bfref = gimple_assign_rhs1 (estmt);
2207 : 4885 : HOST_WIDE_INT lane;
2208 : 4885 : if (!known_eq (bit_field_size (bfref),
2209 : : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
2210 : 9623 : || !constant_multiple_p (bit_field_offset (bfref),
2211 : 4738 : bit_field_size (bfref), &lane))
2212 : : {
2213 : 147 : lperm.release ();
2214 : 147 : matches[0] = false;
2215 : 147 : return NULL;
2216 : : }
2217 : 4738 : lperm.safe_push (std::make_pair (0, (unsigned)lane));
2218 : : }
2219 : 2071 : slp_tree vnode = vect_create_new_slp_node (vNULL);
2220 : 2071 : if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
2221 : : /* ??? We record vectype here but we hide eventually necessary
2222 : : punning and instead rely on code generation to materialize
2223 : : VIEW_CONVERT_EXPRs as necessary. We instead should make
2224 : : this explicit somehow. */
2225 : 684 : SLP_TREE_VECTYPE (vnode) = vectype;
2226 : : else
2227 : : {
2228 : : /* For different size but compatible elements we can still
2229 : : use VEC_PERM_EXPR without punning. */
2230 : 1387 : gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
2231 : : && types_compatible_p (TREE_TYPE (vectype),
2232 : : TREE_TYPE (TREE_TYPE (vec))));
2233 : 1387 : SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
2234 : : }
2235 : 2071 : auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
2236 : 2071 : unsigned HOST_WIDE_INT const_nunits;
2237 : 2071 : if (nunits.is_constant (&const_nunits))
2238 : 2071 : SLP_TREE_LANES (vnode) = const_nunits;
2239 : 2071 : SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
2240 : : /* We are always building a permutation node even if it is an identity
2241 : : permute to shield the rest of the vectorizer from the odd node
2242 : : representing an actual vector without any scalar ops.
2243 : : ??? We could hide it completely with making the permute node
2244 : : external? */
2245 : 2071 : node = vect_create_new_slp_node (node, stmts, 1);
2246 : 2071 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2247 : 2071 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2248 : 2071 : SLP_TREE_VECTYPE (node) = vectype;
2249 : 2071 : SLP_TREE_CHILDREN (node).quick_push (vnode);
2250 : 2071 : return node;
2251 : : }
2252 : : /* When discovery reaches an associatable operation see whether we can
2253 : : improve that to match up lanes in a way superior to the operand
2254 : : swapping code which at most looks at two defs.
2255 : : ??? For BB vectorization we cannot do the brute-force search
2256 : : for matching as we can succeed by means of builds from scalars
2257 : : and have no good way to "cost" one build against another. */
2258 : 2924196 : else if (is_a <loop_vec_info> (vinfo)
2259 : : /* Do not bother for single-lane SLP. */
2260 : 1576460 : && group_size > 1
2261 : : /* ??? We don't handle !vect_internal_def defs below. */
2262 : 78896 : && STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
2263 : : /* ??? Do not associate a reduction, this will wreck REDUC_IDX
2264 : : mapping as long as that exists on the stmt_info level. */
2265 : 62412 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1
2266 : 57281 : && is_gimple_assign (stmt_info->stmt)
2267 : 57056 : && (associative_tree_code (gimple_assign_rhs_code (stmt_info->stmt))
2268 : 39670 : || gimple_assign_rhs_code (stmt_info->stmt) == MINUS_EXPR)
2269 : 2943184 : && ((FLOAT_TYPE_P (vectype) && flag_associative_math)
2270 : 11502 : || (INTEGRAL_TYPE_P (TREE_TYPE (vectype))
2271 : 9624 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype)))))
2272 : : {
2273 : : /* See if we have a chain of (mixed) adds or subtracts or other
2274 : : associatable ops. */
2275 : 13615 : enum tree_code code = gimple_assign_rhs_code (stmt_info->stmt);
2276 : 13615 : if (code == MINUS_EXPR)
2277 : 677 : code = PLUS_EXPR;
2278 : 13615 : stmt_vec_info other_op_stmt_info = NULL;
2279 : 13615 : stmt_vec_info op_stmt_info = NULL;
2280 : 13615 : unsigned chain_len = 0;
2281 : 13615 : auto_vec<chain_op_t> chain;
2282 : 13615 : auto_vec<std::pair<tree_code, gimple *> > worklist;
2283 : 13615 : auto_vec<vec<chain_op_t> > chains (group_size);
2284 : 13615 : auto_vec<slp_tree, 4> children;
2285 : 13615 : bool hard_fail = true;
2286 : 14458 : for (unsigned lane = 0; lane < group_size; ++lane)
2287 : : {
2288 : 14201 : if (!stmts[lane])
2289 : : {
2290 : : /* ??? Below we require lane zero is present. */
2291 : 0 : if (lane == 0)
2292 : : {
2293 : : hard_fail = false;
2294 : 13358 : break;
2295 : : }
2296 : 0 : chains.quick_push (vNULL);
2297 : 0 : continue;
2298 : : }
2299 : : /* For each lane linearize the addition/subtraction (or other
2300 : : uniform associatable operation) expression tree. */
2301 : 14201 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
2302 : 14201 : vect_slp_linearize_chain (vinfo, worklist, chain, code,
2303 : 14201 : stmts[lane]->stmt, op_stmt, other_op_stmt,
2304 : : NULL);
2305 : 14201 : if (!op_stmt_info && op_stmt)
2306 : 13085 : op_stmt_info = vinfo->lookup_stmt (op_stmt);
2307 : 14201 : if (!other_op_stmt_info && other_op_stmt)
2308 : 713 : other_op_stmt_info = vinfo->lookup_stmt (other_op_stmt);
2309 : 14201 : if (chain.length () == 2)
2310 : : {
2311 : : /* In a chain of just two elements resort to the regular
2312 : : operand swapping scheme. Likewise if we run into a
2313 : : length mismatch process regularly as well as we did not
2314 : : process the other lanes we cannot report a good hint what
2315 : : lanes to try swapping in the parent. */
2316 : : hard_fail = false;
2317 : : break;
2318 : : }
2319 : 846 : else if (chain_len == 0)
2320 : 297 : chain_len = chain.length ();
2321 : 1098 : else if (chain.length () != chain_len)
2322 : : {
2323 : : /* ??? Here we could slip in magic to compensate with
2324 : : neutral operands. */
2325 : 3 : matches[lane] = false;
2326 : 3 : if (lane != group_size - 1)
2327 : 3 : matches[0] = false;
2328 : : break;
2329 : : }
2330 : 843 : chains.quick_push (chain.copy ());
2331 : 843 : chain.truncate (0);
2332 : : }
2333 : 27230 : if (chains.length () == group_size)
2334 : : {
2335 : : /* We cannot yet use SLP_TREE_CODE to communicate the operation. */
2336 : 257 : if (!op_stmt_info)
2337 : : {
2338 : 2 : hard_fail = false;
2339 : 2 : goto out;
2340 : : }
2341 : : /* Now we have a set of chains with the same length. */
2342 : : /* 1. pre-sort according to def_type and operation. */
2343 : 988 : for (unsigned lane = 0; lane < group_size; ++lane)
2344 : 1466 : chains[lane].stablesort (dt_sort_cmp, vinfo);
2345 : 255 : if (dump_enabled_p ())
2346 : : {
2347 : 133 : dump_printf_loc (MSG_NOTE, vect_location,
2348 : : "pre-sorted chains of %s\n",
2349 : : get_tree_code_name (code));
2350 : 595 : for (unsigned lane = 0; lane < group_size; ++lane)
2351 : : {
2352 : 462 : if (!stmts[lane])
2353 : 0 : dump_printf (MSG_NOTE, "--");
2354 : : else
2355 : 2158 : for (unsigned opnum = 0; opnum < chain_len; ++opnum)
2356 : 3392 : dump_printf (MSG_NOTE, "%s %T ",
2357 : 1696 : get_tree_code_name (chains[lane][opnum].code),
2358 : 1696 : chains[lane][opnum].op);
2359 : 462 : dump_printf (MSG_NOTE, "\n");
2360 : : }
2361 : : }
2362 : : /* 2. try to build children nodes, associating as necessary. */
2363 : : /* 2a. prepare and perform early checks to avoid eating into
2364 : : discovery limit unnecessarily. */
2365 : 255 : vect_def_type *dts = XALLOCAVEC (vect_def_type, chain_len);
2366 : 1087 : for (unsigned n = 0; n < chain_len; ++n)
2367 : : {
2368 : 832 : vect_def_type dt = chains[0][n].dt;
2369 : 832 : unsigned lane;
2370 : 3373 : for (lane = 0; lane < group_size; ++lane)
2371 : 5082 : if (stmts[lane] && chains[lane][n].dt != dt)
2372 : : {
2373 : 0 : if (dt == vect_constant_def
2374 : 0 : && chains[lane][n].dt == vect_external_def)
2375 : : dt = vect_external_def;
2376 : 0 : else if (dt == vect_external_def
2377 : 0 : && chains[lane][n].dt == vect_constant_def)
2378 : : ;
2379 : : else
2380 : : break;
2381 : : }
2382 : 832 : if (lane != group_size)
2383 : : {
2384 : 0 : if (dump_enabled_p ())
2385 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2386 : : "giving up on chain due to mismatched "
2387 : : "def types\n");
2388 : 0 : matches[lane] = false;
2389 : 0 : if (lane != group_size - 1)
2390 : 0 : matches[0] = false;
2391 : 0 : goto out;
2392 : : }
2393 : 832 : dts[n] = dt;
2394 : 832 : if (dt == vect_constant_def
2395 : 832 : || dt == vect_external_def)
2396 : : {
2397 : : /* Check whether we can build the invariant. If we can't
2398 : : we never will be able to. */
2399 : 71 : tree type = TREE_TYPE (chains[0][n].op);
2400 : 832 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ()
2401 : : && (TREE_CODE (type) == BOOLEAN_TYPE
2402 : : || !can_duplicate_and_interleave_p (vinfo, group_size,
2403 : : type)))
2404 : : {
2405 : : matches[0] = false;
2406 : : goto out;
2407 : : }
2408 : : }
2409 : 761 : else if (dt != vect_internal_def)
2410 : : {
2411 : : /* Not sure, we might need sth special.
2412 : : gcc.dg/vect/pr96854.c,
2413 : : gfortran.dg/vect/fast-math-pr37021.f90
2414 : : and gfortran.dg/vect/pr61171.f trigger. */
2415 : : /* Soft-fail for now. */
2416 : 0 : hard_fail = false;
2417 : 0 : goto out;
2418 : : }
2419 : : }
2420 : : /* 2b. do the actual build. */
2421 : 1033 : for (unsigned n = 0; n < chain_len; ++n)
2422 : : {
2423 : 797 : vect_def_type dt = dts[n];
2424 : 797 : unsigned lane;
2425 : 797 : if (dt == vect_constant_def
2426 : 797 : || dt == vect_external_def)
2427 : : {
2428 : 71 : vec<tree> ops;
2429 : 71 : ops.create (group_size);
2430 : 355 : for (lane = 0; lane < group_size; ++lane)
2431 : 213 : if (stmts[lane])
2432 : 213 : ops.quick_push (chains[lane][n].op);
2433 : : else
2434 : 0 : ops.quick_push (NULL_TREE);
2435 : 71 : slp_tree child = vect_create_new_slp_node (ops);
2436 : 71 : SLP_TREE_DEF_TYPE (child) = dt;
2437 : 71 : children.safe_push (child);
2438 : : }
2439 : : else
2440 : : {
2441 : 726 : vec<stmt_vec_info> op_stmts;
2442 : 726 : op_stmts.create (group_size);
2443 : 726 : slp_tree child = NULL;
2444 : : /* Brute-force our way. We have to consider a lane
2445 : : failing after fixing an earlier fail up in the
2446 : : SLP discovery recursion. So track the current
2447 : : permute per lane. */
2448 : 726 : unsigned *perms = XALLOCAVEC (unsigned, group_size);
2449 : 726 : memset (perms, 0, sizeof (unsigned) * group_size);
2450 : 805 : do
2451 : : {
2452 : 805 : op_stmts.truncate (0);
2453 : 4092 : for (lane = 0; lane < group_size; ++lane)
2454 : 2482 : if (stmts[lane])
2455 : 2482 : op_stmts.quick_push
2456 : 2482 : (vinfo->lookup_def (chains[lane][n].op));
2457 : : else
2458 : 0 : op_stmts.quick_push (NULL);
2459 : 805 : child = vect_build_slp_tree (vinfo, op_stmts,
2460 : : group_size, &this_max_nunits,
2461 : : matches, limit,
2462 : : &this_tree_size, bst_map);
2463 : : /* ??? We're likely getting too many fatal mismatches
2464 : : here so maybe we want to ignore them (but then we
2465 : : have no idea which lanes fatally mismatched). */
2466 : 805 : if (child || !matches[0])
2467 : : break;
2468 : : /* Swap another lane we have not yet matched up into
2469 : : lanes that did not match. If we run out of
2470 : : permute possibilities for a lane terminate the
2471 : : search. */
2472 : 257 : bool term = false;
2473 : 257 : for (lane = 1; lane < group_size; ++lane)
2474 : 178 : if (!matches[lane])
2475 : : {
2476 : 150 : if (n + perms[lane] + 1 == chain_len)
2477 : : {
2478 : : term = true;
2479 : : break;
2480 : : }
2481 : 131 : if (dump_enabled_p ())
2482 : 113 : dump_printf_loc (MSG_NOTE, vect_location,
2483 : : "swapping operand %d and %d "
2484 : : "of lane %d\n",
2485 : : n, n + perms[lane] + 1, lane);
2486 : 262 : std::swap (chains[lane][n],
2487 : 131 : chains[lane][n + perms[lane] + 1]);
2488 : 131 : perms[lane]++;
2489 : : }
2490 : 98 : if (term)
2491 : : break;
2492 : : }
2493 : : while (1);
2494 : 726 : if (!child)
2495 : : {
2496 : 19 : if (dump_enabled_p ())
2497 : 18 : dump_printf_loc (MSG_NOTE, vect_location,
2498 : : "failed to match up op %d\n", n);
2499 : 19 : op_stmts.release ();
2500 : 19 : if (lane != group_size - 1)
2501 : 9 : matches[0] = false;
2502 : : else
2503 : 10 : matches[lane] = false;
2504 : 19 : goto out;
2505 : : }
2506 : 707 : if (dump_enabled_p ())
2507 : : {
2508 : 367 : dump_printf_loc (MSG_NOTE, vect_location,
2509 : : "matched up op %d to\n", n);
2510 : 367 : vect_print_slp_tree (MSG_NOTE, vect_location, child);
2511 : : }
2512 : 707 : children.safe_push (child);
2513 : : }
2514 : : }
2515 : : /* 3. build SLP nodes to combine the chain. */
2516 : 896 : for (unsigned lane = 0; lane < group_size; ++lane)
2517 : 1332 : if (stmts[lane] && chains[lane][0].code != code)
2518 : : {
2519 : : /* See if there's any alternate all-PLUS entry. */
2520 : : unsigned n;
2521 : 6 : for (n = 1; n < chain_len; ++n)
2522 : : {
2523 : 30 : for (lane = 0; lane < group_size; ++lane)
2524 : 48 : if (stmts[lane] && chains[lane][n].code != code)
2525 : : break;
2526 : 6 : if (lane == group_size)
2527 : : break;
2528 : : }
2529 : 6 : if (n != chain_len)
2530 : : {
2531 : : /* Swap that in at first position. */
2532 : 6 : std::swap (children[0], children[n]);
2533 : 30 : for (lane = 0; lane < group_size; ++lane)
2534 : 24 : if (stmts[lane])
2535 : 24 : std::swap (chains[lane][0], chains[lane][n]);
2536 : : }
2537 : : else
2538 : : {
2539 : : /* ??? When this triggers and we end up with two
2540 : : vect_constant/external_def up-front things break (ICE)
2541 : : spectacularly finding an insertion place for the
2542 : : all-constant op. We should have a fully
2543 : : vect_internal_def operand though(?) so we can swap
2544 : : that into first place and then prepend the all-zero
2545 : : constant. */
2546 : 0 : if (dump_enabled_p ())
2547 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2548 : : "inserting constant zero to compensate "
2549 : : "for (partially) negated first "
2550 : : "operand\n");
2551 : 0 : chain_len++;
2552 : 0 : for (lane = 0; lane < group_size; ++lane)
2553 : 0 : if (stmts[lane])
2554 : 0 : chains[lane].safe_insert
2555 : 0 : (0, chain_op_t (code, vect_constant_def, NULL_TREE));
2556 : 0 : vec<tree> zero_ops;
2557 : 0 : zero_ops.create (group_size);
2558 : 0 : zero_ops.quick_push (build_zero_cst (TREE_TYPE (vectype)));
2559 : 0 : for (lane = 1; lane < group_size; ++lane)
2560 : 0 : if (stmts[lane])
2561 : 0 : zero_ops.quick_push (zero_ops[0]);
2562 : : else
2563 : 0 : zero_ops.quick_push (NULL_TREE);
2564 : 0 : slp_tree zero = vect_create_new_slp_node (zero_ops);
2565 : 0 : SLP_TREE_DEF_TYPE (zero) = vect_constant_def;
2566 : 0 : children.safe_insert (0, zero);
2567 : : }
2568 : : break;
2569 : : }
2570 : 773 : for (unsigned i = 1; i < children.length (); ++i)
2571 : : {
2572 : 537 : slp_tree op0 = children[i - 1];
2573 : 537 : slp_tree op1 = children[i];
2574 : 537 : bool this_two_op = false;
2575 : 2061 : for (unsigned lane = 0; lane < group_size; ++lane)
2576 : 3292 : if (stmts[lane] && chains[lane][i].code != chains[0][i].code)
2577 : : {
2578 : : this_two_op = true;
2579 : : break;
2580 : : }
2581 : 537 : slp_tree child;
2582 : 537 : if (i == children.length () - 1)
2583 : 236 : child = vect_create_new_slp_node (node, stmts, 2);
2584 : : else
2585 : 301 : child = vect_create_new_slp_node (2, ERROR_MARK);
2586 : 537 : if (this_two_op)
2587 : : {
2588 : 122 : vec<std::pair<unsigned, unsigned> > lperm;
2589 : 122 : lperm.create (group_size);
2590 : 462 : for (unsigned lane = 0; lane < group_size; ++lane)
2591 : 680 : lperm.quick_push (std::make_pair
2592 : 340 : (chains[lane][i].code != chains[0][i].code, lane));
2593 : 244 : vect_slp_build_two_operator_nodes (child, vectype, op0, op1,
2594 : 122 : (chains[0][i].code == code
2595 : : ? op_stmt_info
2596 : : : other_op_stmt_info),
2597 : 122 : (chains[0][i].code == code
2598 : : ? other_op_stmt_info
2599 : : : op_stmt_info),
2600 : : lperm);
2601 : : }
2602 : : else
2603 : : {
2604 : 415 : SLP_TREE_DEF_TYPE (child) = vect_internal_def;
2605 : 415 : SLP_TREE_VECTYPE (child) = vectype;
2606 : 415 : SLP_TREE_LANES (child) = group_size;
2607 : 415 : SLP_TREE_CHILDREN (child).quick_push (op0);
2608 : 415 : SLP_TREE_CHILDREN (child).quick_push (op1);
2609 : 415 : SLP_TREE_REPRESENTATIVE (child)
2610 : 830 : = (chains[0][i].code == code
2611 : 415 : ? op_stmt_info : other_op_stmt_info);
2612 : : }
2613 : 537 : children[i] = child;
2614 : : }
2615 : 236 : *tree_size += this_tree_size + 1;
2616 : 236 : *max_nunits = this_max_nunits;
2617 : 1178 : while (!chains.is_empty ())
2618 : 684 : chains.pop ().release ();
2619 : : return node;
2620 : : }
2621 : 13358 : out:
2622 : 13379 : if (dump_enabled_p ())
2623 : 2723 : dump_printf_loc (MSG_NOTE, vect_location,
2624 : : "failed to line up SLP graph by re-associating "
2625 : : "operations in lanes%s\n",
2626 : : !hard_fail ? " trying regular discovery" : "");
2627 : 13384 : while (!children.is_empty ())
2628 : 5 : vect_free_slp_tree (children.pop ());
2629 : 13538 : while (!chains.is_empty ())
2630 : 159 : chains.pop ().release ();
2631 : : /* Hard-fail, otherwise we might run into quadratic processing of the
2632 : : chains starting one stmt into the chain again. */
2633 : 13379 : if (hard_fail)
2634 : : return NULL;
2635 : : /* Fall thru to normal processing. */
2636 : 13615 : }
2637 : :
2638 : : /* Get at the operands, verifying they are compatible. */
2639 : 2938433 : vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
2640 : 2938433 : slp_oprnd_info oprnd_info;
2641 : 15130113 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
2642 : : {
2643 : 24388308 : int res = vect_get_and_check_slp_defs (vinfo, vectype,
2644 : 12194154 : swap[i], skip_args,
2645 : : stmts, i, &oprnds_info);
2646 : 12194154 : if (res != 0)
2647 : 520328 : matches[(res == -1) ? 0 : i] = false;
2648 : 12194154 : if (!matches[0])
2649 : : break;
2650 : : }
2651 : 14835613 : for (i = 0; i < group_size; ++i)
2652 : 12105149 : if (!matches[i])
2653 : : {
2654 : 207969 : vect_free_oprnd_info (oprnds_info);
2655 : 207969 : return NULL;
2656 : : }
2657 : 8191392 : swap = NULL;
2658 : :
2659 : 8191392 : bool has_two_operators_perm = false;
2660 : 16382784 : auto_vec<unsigned> two_op_perm_indices[2];
2661 : 2730464 : vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL};
2662 : :
2663 : 2742446 : if (two_operators && oprnds_info.length () == 2 && group_size > 2)
2664 : : {
2665 : 2606 : unsigned idx = 0;
2666 : 2606 : hash_map<gimple *, unsigned> seen;
2667 : 2606 : vec<slp_oprnd_info> new_oprnds_info
2668 : 2606 : = vect_create_oprnd_info (1, group_size);
2669 : 2606 : bool success = true;
2670 : :
2671 : 2606 : enum tree_code code = ERROR_MARK;
2672 : 2606 : if (oprnds_info[0]->def_stmts[0]
2673 : 2606 : && is_a<gassign *> (oprnds_info[0]->def_stmts[0]->stmt))
2674 : 2548 : code = gimple_assign_rhs_code (oprnds_info[0]->def_stmts[0]->stmt);
2675 : 2606 : basic_block bb = nullptr;
2676 : :
2677 : 5675 : for (unsigned j = 0; j < group_size; ++j)
2678 : : {
2679 : 13523 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2680 : : {
2681 : 10454 : stmt_vec_info stmt_info = oprnd_info->def_stmts[j];
2682 : 10454 : if (!stmt_info
2683 : 10323 : || !is_a<gassign *> (stmt_info->stmt)
2684 : 10320 : || gimple_assign_rhs_code (stmt_info->stmt) != code
2685 : 18683 : || skip_args[i])
2686 : : {
2687 : : success = false;
2688 : 2229 : break;
2689 : : }
2690 : : /* Avoid mixing lanes with defs in different basic-blocks. */
2691 : 8229 : if (!bb)
2692 : 2706 : bb = gimple_bb (vect_orig_stmt (stmt_info)->stmt);
2693 : 7045 : else if (gimple_bb (vect_orig_stmt (stmt_info)->stmt) != bb)
2694 : : {
2695 : : success = false;
2696 : : break;
2697 : : }
2698 : :
2699 : 8225 : bool exists;
2700 : 8225 : unsigned &stmt_idx
2701 : 8225 : = seen.get_or_insert (stmt_info->stmt, &exists);
2702 : :
2703 : 8225 : if (!exists)
2704 : : {
2705 : 7176 : new_oprnds_info[0]->def_stmts.safe_push (stmt_info);
2706 : 7176 : new_oprnds_info[0]->ops.safe_push (oprnd_info->ops[j]);
2707 : 7176 : stmt_idx = idx;
2708 : 7176 : idx++;
2709 : : }
2710 : :
2711 : 8225 : two_op_perm_indices[i].safe_push (stmt_idx);
2712 : : }
2713 : :
2714 : 5298 : if (!success)
2715 : : break;
2716 : : }
2717 : :
2718 : 2606 : if (success && idx == group_size)
2719 : : {
2720 : 56 : if (dump_enabled_p ())
2721 : : {
2722 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2723 : : "Replace two_operators operands:\n");
2724 : :
2725 : 0 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2726 : : {
2727 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2728 : : "Operand %u:\n", i);
2729 : 0 : for (unsigned j = 0; j < group_size; j++)
2730 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2731 : 0 : j, oprnd_info->def_stmts[j]->stmt);
2732 : : }
2733 : :
2734 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2735 : : "With a single operand:\n");
2736 : 0 : for (unsigned j = 0; j < group_size; j++)
2737 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2738 : 0 : j, new_oprnds_info[0]->def_stmts[j]->stmt);
2739 : : }
2740 : :
2741 : 56 : two_op_scalar_stmts[0].safe_splice (oprnds_info[0]->def_stmts);
2742 : 56 : two_op_scalar_stmts[1].safe_splice (oprnds_info[1]->def_stmts);
2743 : :
2744 : 56 : new_oprnds_info[0]->first_op_type = oprnds_info[0]->first_op_type;
2745 : 56 : new_oprnds_info[0]->first_dt = oprnds_info[0]->first_dt;
2746 : 56 : new_oprnds_info[0]->any_pattern = oprnds_info[0]->any_pattern;
2747 : 56 : new_oprnds_info[0]->first_gs_p = oprnds_info[0]->first_gs_p;
2748 : 56 : new_oprnds_info[0]->first_gs_info = oprnds_info[0]->first_gs_info;
2749 : :
2750 : 56 : vect_free_oprnd_info (oprnds_info);
2751 : 56 : oprnds_info = new_oprnds_info;
2752 : 56 : nops = 1;
2753 : 56 : has_two_operators_perm = true;
2754 : : }
2755 : : else
2756 : 2550 : vect_free_oprnd_info (new_oprnds_info);
2757 : 2606 : }
2758 : :
2759 : 5460928 : auto_vec<slp_tree, 4> children;
2760 : :
2761 : 2730464 : stmt_info = stmts[0];
2762 : :
2763 : 2730464 : int reduc_idx = -1;
2764 : 2730464 : int gs_scale = 0;
2765 : 2730464 : tree gs_base = NULL_TREE;
2766 : :
2767 : : /* Create SLP_TREE nodes for the definition node/s. */
2768 : 6950033 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2769 : : {
2770 : 4305526 : slp_tree child = nullptr;
2771 : 4305526 : unsigned int j;
2772 : :
2773 : : /* We're skipping certain operands from processing, for example
2774 : : outer loop reduction initial defs. */
2775 : 4305526 : if (skip_args[i])
2776 : : {
2777 : 375932 : children.safe_push (NULL);
2778 : 4595501 : continue;
2779 : : }
2780 : :
2781 : 3929594 : if (oprnd_info->first_dt == vect_uninitialized_def)
2782 : : {
2783 : : /* COND_EXPR have one too many eventually if the condition
2784 : : is a SSA name. */
2785 : 0 : gcc_assert (i == 3 && nops == 4);
2786 : 0 : continue;
2787 : : }
2788 : :
2789 : 3929594 : if (oprnd_info->first_gs_p)
2790 : : {
2791 : 15974 : gs_scale = oprnd_info->first_gs_info.scale;
2792 : 15974 : gs_base = oprnd_info->first_gs_info.base;
2793 : : }
2794 : :
2795 : 3929594 : if (is_a <bb_vec_info> (vinfo)
2796 : 1591077 : && oprnd_info->first_dt == vect_internal_def
2797 : 4752106 : && !oprnd_info->any_pattern)
2798 : : {
2799 : : /* For BB vectorization, if all defs are the same do not
2800 : : bother to continue the build along the single-lane
2801 : : graph but use a splat of the scalar value. */
2802 : 779844 : stmt_vec_info first_def = oprnd_info->def_stmts[0];
2803 : 839662 : for (j = 1; j < group_size; ++j)
2804 : 795720 : if (oprnd_info->def_stmts[j] != first_def)
2805 : : break;
2806 : 779844 : if (j == group_size
2807 : : /* But avoid doing this for loads where we may be
2808 : : able to CSE things, unless the stmt is not
2809 : : vectorizable. */
2810 : 779844 : && (!STMT_VINFO_VECTORIZABLE (first_def)
2811 : 50205 : || !gimple_vuse (first_def->stmt)))
2812 : : {
2813 : 34710 : if (dump_enabled_p ())
2814 : 94 : dump_printf_loc (MSG_NOTE, vect_location,
2815 : : "Using a splat of the uniform operand %G",
2816 : : first_def->stmt);
2817 : 34710 : oprnd_info->first_dt = vect_external_def;
2818 : : }
2819 : : }
2820 : :
2821 : 3929594 : if (oprnd_info->first_dt == vect_external_def
2822 : 3929594 : || oprnd_info->first_dt == vect_constant_def)
2823 : : {
2824 : 1367342 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ())
2825 : : {
2826 : : tree op0;
2827 : : tree uniform_val = op0 = oprnd_info->ops[0];
2828 : : for (j = 1; j < oprnd_info->ops.length (); ++j)
2829 : : if (oprnd_info->ops[j]
2830 : : && !operand_equal_p (uniform_val, oprnd_info->ops[j]))
2831 : : {
2832 : : uniform_val = NULL_TREE;
2833 : : break;
2834 : : }
2835 : : if (!uniform_val
2836 : : && !can_duplicate_and_interleave_p (vinfo,
2837 : : oprnd_info->ops.length (),
2838 : : TREE_TYPE (op0)))
2839 : : {
2840 : : matches[j] = false;
2841 : : if (dump_enabled_p ())
2842 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2843 : : "Build SLP failed: invalid type of def "
2844 : : "for variable-length SLP %T\n", op0);
2845 : : goto fail;
2846 : : }
2847 : : }
2848 : 1367342 : slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops);
2849 : 1367342 : SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt;
2850 : 1367342 : oprnd_info->ops = vNULL;
2851 : 1367342 : children.safe_push (invnode);
2852 : 1367342 : continue;
2853 : 1367342 : }
2854 : :
2855 : : /* See which SLP operand a reduction chain continues on. We want
2856 : : to chain even PHIs but not backedges. */
2857 : 2562252 : if (STMT_VINFO_REDUC_DEF (oprnd_info->def_stmts[0])
2858 : 2562252 : || STMT_VINFO_REDUC_IDX (oprnd_info->def_stmts[0]) != -1)
2859 : : {
2860 : 157822 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle)
2861 : : {
2862 : 596 : if (oprnd_info->first_dt == vect_double_reduction_def)
2863 : 298 : reduc_idx = i;
2864 : : }
2865 : 157226 : else if (is_a <gphi *> (stmt_info->stmt)
2866 : 157226 : && gimple_phi_num_args
2867 : 68902 : (as_a <gphi *> (stmt_info->stmt)) != 1)
2868 : : ;
2869 : 88626 : else if (STMT_VINFO_REDUC_IDX (stmt_info) == -1
2870 : 302 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_double_reduction_def)
2871 : : ;
2872 : 88626 : else if (reduc_idx == -1)
2873 : 84320 : reduc_idx = i;
2874 : : else
2875 : : /* For .COND_* reduction operations the else value can be the
2876 : : same as one of the operation operands. The other def
2877 : : stmts have been moved, so we can't check easily. Check
2878 : : it's a call at least. */
2879 : 4306 : gcc_assert (is_a <gcall *> (stmt_info->stmt));
2880 : : }
2881 : :
2882 : : /* When we have a masked load with uniform mask discover this
2883 : : as a single-lane mask with a splat permute. This way we can
2884 : : recognize this as a masked load-lane by stripping the splat. */
2885 : 2562252 : if (is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
2886 : 34536 : && gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
2887 : : IFN_MASK_LOAD)
2888 : 4682 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2889 : 2562274 : && ! STMT_VINFO_SLP_VECT_ONLY (DR_GROUP_FIRST_ELEMENT (stmt_info)))
2890 : : {
2891 : 0 : vec<stmt_vec_info> def_stmts2;
2892 : 0 : def_stmts2.create (1);
2893 : 0 : def_stmts2.quick_push (oprnd_info->def_stmts[0]);
2894 : 0 : child = vect_build_slp_tree (vinfo, def_stmts2, 1,
2895 : : &this_max_nunits,
2896 : : matches, limit,
2897 : : &this_tree_size, bst_map);
2898 : 0 : if (child)
2899 : : {
2900 : 0 : slp_tree pnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
2901 : 0 : SLP_TREE_VECTYPE (pnode) = SLP_TREE_VECTYPE (child);
2902 : 0 : SLP_TREE_LANES (pnode) = group_size;
2903 : 0 : SLP_TREE_SCALAR_STMTS (pnode).create (group_size);
2904 : 0 : SLP_TREE_LANE_PERMUTATION (pnode).create (group_size);
2905 : 0 : for (unsigned k = 0; k < group_size; ++k)
2906 : : {
2907 : 0 : SLP_TREE_SCALAR_STMTS (pnode)
2908 : 0 : .quick_push (oprnd_info->def_stmts[0]);
2909 : 0 : SLP_TREE_LANE_PERMUTATION (pnode)
2910 : 0 : .quick_push (std::make_pair (0u, 0u));
2911 : : }
2912 : 0 : SLP_TREE_CHILDREN (pnode).quick_push (child);
2913 : 0 : pnode->max_nunits = child->max_nunits;
2914 : 0 : children.safe_push (pnode);
2915 : 0 : oprnd_info->def_stmts = vNULL;
2916 : 0 : continue;
2917 : 0 : }
2918 : : else
2919 : 0 : def_stmts2.release ();
2920 : : }
2921 : :
2922 : 2562252 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
2923 : : group_size, &this_max_nunits,
2924 : : matches, limit,
2925 : : &this_tree_size, bst_map)) != NULL)
2926 : : {
2927 : 2097418 : oprnd_info->def_stmts = vNULL;
2928 : 2097418 : children.safe_push (child);
2929 : 2097418 : continue;
2930 : : }
2931 : :
2932 : : /* If the SLP build for operand zero failed and operand zero
2933 : : and one can be commutated try that for the scalar stmts
2934 : : that failed the match. */
2935 : 464834 : if (i == 0
2936 : : /* A first scalar stmt mismatch signals a fatal mismatch. */
2937 : 364367 : && matches[0]
2938 : : /* ??? For COND_EXPRs we can swap the comparison operands
2939 : : as well as the arms under some constraints. */
2940 : 170285 : && (nops == 2 || nops == 3)
2941 : 104905 : && oprnds_info[1]->first_dt == vect_internal_def
2942 : 58816 : && (is_gimple_assign (stmt_info->stmt)
2943 : 12507 : || is_gimple_call (stmt_info->stmt))
2944 : : /* Swapping operands for reductions breaks assumptions later on. */
2945 : 511156 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
2946 : : {
2947 : : /* See whether we can swap the matching or the non-matching
2948 : : stmt operands. */
2949 : : bool swap_not_matching = true;
2950 : 52584 : do
2951 : : {
2952 : 7041218 : for (j = 0; j < group_size; ++j)
2953 : : {
2954 : 7003944 : if (matches[j] != !swap_not_matching)
2955 : 66564 : continue;
2956 : 6937380 : stmt_vec_info stmt_info = stmts[j];
2957 : : /* Verify if we can swap operands of this stmt. */
2958 : 6937380 : if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
2959 : : {
2960 : 6937354 : tree_code code = gimple_assign_rhs_code (stmt);
2961 : 6937354 : if (! commutative_tree_code (code)
2962 : 6937354 : && ! commutative_ternary_tree_code (code))
2963 : : {
2964 : 15286 : if (!swap_not_matching)
2965 : 7055 : goto fail;
2966 : : swap_not_matching = false;
2967 : : break;
2968 : : }
2969 : : }
2970 : 6988660 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2971 : : {
2972 : 26 : internal_fn fn = (gimple_call_internal_p (call)
2973 : 26 : ? gimple_call_internal_fn (call)
2974 : : : IFN_LAST);
2975 : 26 : if ((! commutative_binary_fn_p (fn)
2976 : 26 : && ! commutative_ternary_fn_p (fn))
2977 : 28 : || first_commutative_argument (fn) != 0)
2978 : : {
2979 : 24 : if (!swap_not_matching)
2980 : 12 : goto fail;
2981 : : swap_not_matching = false;
2982 : : break;
2983 : : }
2984 : : }
2985 : : }
2986 : : }
2987 : 45517 : while (j != group_size);
2988 : :
2989 : : /* Swap mismatched definition stmts. */
2990 : 37274 : if (dump_enabled_p ())
2991 : 330 : dump_printf_loc (MSG_NOTE, vect_location,
2992 : : "Re-trying with swapped operands of stmts ");
2993 : 7016905 : for (j = 0; j < group_size; ++j)
2994 : 6979631 : if (matches[j] == !swap_not_matching)
2995 : : {
2996 : 13843876 : std::swap (oprnds_info[0]->def_stmts[j],
2997 : 6921938 : oprnds_info[1]->def_stmts[j]);
2998 : 13843876 : std::swap (oprnds_info[0]->ops[j],
2999 : 6921938 : oprnds_info[1]->ops[j]);
3000 : 6921938 : if (dump_enabled_p ())
3001 : 899 : dump_printf (MSG_NOTE, "%d ", j);
3002 : : }
3003 : 37274 : if (dump_enabled_p ())
3004 : 330 : dump_printf (MSG_NOTE, "\n");
3005 : : /* After swapping some operands we lost track whether an
3006 : : operand has any pattern defs so be conservative here. */
3007 : 71331 : if (oprnds_info[0]->any_pattern || oprnds_info[1]->any_pattern)
3008 : 3256 : oprnds_info[0]->any_pattern = oprnds_info[1]->any_pattern = true;
3009 : : /* And try again with scratch 'matches' ... */
3010 : 37274 : bool *tem = XALLOCAVEC (bool, group_size);
3011 : 37274 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
3012 : : group_size, &this_max_nunits,
3013 : : tem, limit,
3014 : : &this_tree_size, bst_map)) != NULL)
3015 : : {
3016 : 7272 : oprnd_info->def_stmts = vNULL;
3017 : 7272 : children.safe_push (child);
3018 : 7272 : continue;
3019 : : }
3020 : : }
3021 : 457562 : fail:
3022 : :
3023 : : /* If the SLP build failed and we analyze a basic-block
3024 : : simply treat nodes we fail to build as externally defined
3025 : : (and thus build vectors from the scalar defs).
3026 : : The cost model will reject outright expensive cases.
3027 : : ??? This doesn't treat cases where permutation ultimatively
3028 : : fails (or we don't try permutation below). Ideally we'd
3029 : : even compute a permutation that will end up with the maximum
3030 : : SLP tree size... */
3031 : 457562 : if (is_a <bb_vec_info> (vinfo)
3032 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
3033 : : do extra work to cancel the pattern so the uses see the
3034 : : scalar version. */
3035 : 406213 : && !is_pattern_stmt_p (stmt_info)
3036 : 839876 : && !oprnd_info->any_pattern)
3037 : : {
3038 : : /* But if there's a leading vector sized set of matching stmts
3039 : : fail here so we can split the group. This matches the condition
3040 : : vect_analyze_slp_instance uses. */
3041 : : /* ??? We might want to split here and combine the results to support
3042 : : multiple vector sizes better. */
3043 : 592948 : for (j = 0; j < group_size; ++j)
3044 : 592948 : if (!matches[j])
3045 : : break;
3046 : 382098 : if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype))
3047 : 382033 : && vect_slp_can_convert_to_external (oprnd_info->def_stmts))
3048 : : {
3049 : 371605 : if (dump_enabled_p ())
3050 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
3051 : : "Building vector operands from scalars\n");
3052 : 371605 : this_tree_size++;
3053 : 371605 : child = vect_create_new_slp_node (oprnd_info->ops);
3054 : 371605 : children.safe_push (child);
3055 : 371605 : oprnd_info->ops = vNULL;
3056 : 371605 : continue;
3057 : : }
3058 : : }
3059 : :
3060 : 85957 : gcc_assert (child == NULL);
3061 : 96827 : FOR_EACH_VEC_ELT (children, j, child)
3062 : 10870 : if (child)
3063 : 10870 : vect_free_slp_tree (child);
3064 : 85957 : vect_free_oprnd_info (oprnds_info);
3065 : 85957 : return NULL;
3066 : : }
3067 : :
3068 : 2644507 : vect_free_oprnd_info (oprnds_info);
3069 : :
3070 : : /* If we have all children of a child built up from uniform scalars
3071 : : or does more than one possibly expensive vector construction then
3072 : : just throw that away, causing it built up from scalars.
3073 : : The exception is the SLP node for the vector store. */
3074 : 2644507 : if (is_a <bb_vec_info> (vinfo)
3075 : 1109377 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3076 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
3077 : : do extra work to cancel the pattern so the uses see the
3078 : : scalar version. */
3079 : 3089971 : && !is_pattern_stmt_p (stmt_info))
3080 : : {
3081 : : slp_tree child;
3082 : : unsigned j;
3083 : : bool all_uniform_p = true;
3084 : : unsigned n_vector_builds = 0;
3085 : 1263754 : FOR_EACH_VEC_ELT (children, j, child)
3086 : : {
3087 : 843238 : if (!child)
3088 : : ;
3089 : 843238 : else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3090 : : all_uniform_p = false;
3091 : 609158 : else if (!vect_slp_tree_uniform_p (child))
3092 : : {
3093 : 466308 : all_uniform_p = false;
3094 : 466308 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def)
3095 : 430532 : n_vector_builds++;
3096 : : }
3097 : : }
3098 : 420516 : if (all_uniform_p
3099 : 420516 : || n_vector_builds > 1
3100 : 711008 : || (n_vector_builds == children.length ()
3101 : 30899 : && is_a <gphi *> (stmt_info->stmt)))
3102 : : {
3103 : : /* Roll back. */
3104 : 135344 : matches[0] = false;
3105 : 427408 : FOR_EACH_VEC_ELT (children, j, child)
3106 : 292064 : if (child)
3107 : 292064 : vect_free_slp_tree (child);
3108 : :
3109 : 135344 : if (dump_enabled_p ())
3110 : 123 : dump_printf_loc (MSG_NOTE, vect_location,
3111 : : "Building parent vector operands from "
3112 : : "scalars instead\n");
3113 : 135344 : return NULL;
3114 : : }
3115 : : }
3116 : :
3117 : 2509163 : *tree_size += this_tree_size + 1;
3118 : 2509163 : *max_nunits = this_max_nunits;
3119 : :
3120 : 2509163 : if (two_operators)
3121 : : {
3122 : : /* ??? We'd likely want to either cache in bst_map sth like
3123 : : { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
3124 : : the true { a+b, a+b, a+b, a+b } ... but there we don't have
3125 : : explicit stmts to put in so the keying on 'stmts' doesn't
3126 : : work (but we have the same issue with nodes that use 'ops'). */
3127 : :
3128 : 5878 : if (has_two_operators_perm)
3129 : : {
3130 : 22 : slp_tree child = children[0];
3131 : 22 : children.truncate (0);
3132 : 66 : for (i = 0; i < 2; i++)
3133 : : {
3134 : 44 : slp_tree pnode
3135 : 44 : = vect_create_new_slp_node (two_op_scalar_stmts[i], 2);
3136 : 44 : SLP_TREE_CODE (pnode) = VEC_PERM_EXPR;
3137 : 44 : SLP_TREE_VECTYPE (pnode) = vectype;
3138 : 44 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3139 : 44 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3140 : 44 : lane_permutation_t& perm = SLP_TREE_LANE_PERMUTATION (pnode);
3141 : 44 : children.safe_push (pnode);
3142 : :
3143 : 476 : for (unsigned j = 0; j < stmts.length (); j++)
3144 : 432 : perm.safe_push (std::make_pair (0, two_op_perm_indices[i][j]));
3145 : : }
3146 : :
3147 : 22 : SLP_TREE_REF_COUNT (child) += 4;
3148 : : }
3149 : :
3150 : 5878 : slp_tree one = new _slp_tree;
3151 : 5878 : slp_tree two = new _slp_tree;
3152 : 5878 : SLP_TREE_DEF_TYPE (one) = vect_internal_def;
3153 : 5878 : SLP_TREE_DEF_TYPE (two) = vect_internal_def;
3154 : 5878 : SLP_TREE_VECTYPE (one) = vectype;
3155 : 5878 : SLP_TREE_VECTYPE (two) = vectype;
3156 : 5878 : SLP_TREE_CHILDREN (one).safe_splice (children);
3157 : 5878 : SLP_TREE_CHILDREN (two).safe_splice (children);
3158 : 5878 : slp_tree child;
3159 : 23514 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two), i, child)
3160 : 11758 : SLP_TREE_REF_COUNT (child)++;
3161 : :
3162 : : /* Here we record the original defs since this
3163 : : node represents the final lane configuration. */
3164 : 5878 : node = vect_create_new_slp_node (node, stmts, 2);
3165 : 5878 : SLP_TREE_VECTYPE (node) = vectype;
3166 : 5878 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
3167 : 5878 : SLP_TREE_CHILDREN (node).quick_push (one);
3168 : 5878 : SLP_TREE_CHILDREN (node).quick_push (two);
3169 : 5878 : enum tree_code code0 = ERROR_MARK;
3170 : 5878 : enum tree_code ocode = ERROR_MARK;
3171 : 5878 : if (gassign *stmt = dyn_cast <gassign *> (stmts[0]->stmt))
3172 : 5876 : code0 = gimple_assign_rhs_code (stmt);
3173 : 5878 : stmt_vec_info ostmt_info;
3174 : 5878 : unsigned j = 0;
3175 : 21779 : FOR_EACH_VEC_ELT (stmts, i, ostmt_info)
3176 : : {
3177 : 15901 : int op = 0;
3178 : 15901 : if (gassign *ostmt = dyn_cast <gassign *> (ostmt_info->stmt))
3179 : : {
3180 : 15897 : if (gimple_assign_rhs_code (ostmt) != code0)
3181 : : {
3182 : 7983 : ocode = gimple_assign_rhs_code (ostmt);
3183 : : op = 1;
3184 : : j = i;
3185 : : }
3186 : : }
3187 : : else
3188 : : {
3189 : 8 : if (gimple_call_combined_fn (stmts[0]->stmt)
3190 : 4 : != gimple_call_combined_fn (ostmt_info->stmt))
3191 : : {
3192 : 2 : op = 1;
3193 : 2 : j = i;
3194 : : }
3195 : : }
3196 : 15901 : SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (op, i));
3197 : : }
3198 : 5878 : SLP_TREE_CODE (one) = code0;
3199 : 5878 : SLP_TREE_CODE (two) = ocode;
3200 : 5878 : SLP_TREE_LANES (one) = stmts.length ();
3201 : 5878 : SLP_TREE_LANES (two) = stmts.length ();
3202 : 5878 : SLP_TREE_REPRESENTATIVE (one) = stmts[0];
3203 : 5878 : SLP_TREE_REPRESENTATIVE (two) = stmts[j];
3204 : :
3205 : 5878 : return node;
3206 : : }
3207 : :
3208 : 2503285 : node = vect_create_new_slp_node (node, stmts, nops);
3209 : 2503285 : SLP_TREE_VECTYPE (node) = vectype;
3210 : 2503285 : SLP_TREE_CHILDREN (node).splice (children);
3211 : 2503285 : SLP_TREE_GS_SCALE (node) = gs_scale;
3212 : 2503285 : SLP_TREE_GS_BASE (node) = gs_base;
3213 : 2503285 : if (reduc_idx != -1)
3214 : : {
3215 : 79379 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) != -1
3216 : : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
3217 : : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def);
3218 : 79379 : SLP_TREE_REDUC_IDX (node) = reduc_idx;
3219 : 79379 : node->cycle_info.id = SLP_TREE_CHILDREN (node)[reduc_idx]->cycle_info.id;
3220 : : }
3221 : : /* When reaching the reduction PHI, create a vect_reduc_info. */
3222 : 2423906 : else if ((STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def
3223 : 2423906 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3224 : 2423906 : && is_a <gphi *> (STMT_VINFO_STMT (stmt_info)))
3225 : : {
3226 : 70007 : loop_vec_info loop_vinfo = as_a <loop_vec_info> (vinfo);
3227 : 70007 : gcc_assert (STMT_VINFO_REDUC_IDX (stmt_info) == -1);
3228 : 70007 : node->cycle_info.id = loop_vinfo->reduc_infos.length ();
3229 : 70007 : vect_reduc_info reduc_info = new vect_reduc_info_s ();
3230 : 70007 : loop_vinfo->reduc_infos.safe_push (reduc_info);
3231 : 70007 : stmt_vec_info reduc_phi = stmt_info;
3232 : : /* ??? For double reductions vect_is_simple_reduction stores the
3233 : : reduction type and code on the inner loop header PHI. */
3234 : 70007 : if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_double_reduction_def)
3235 : : {
3236 : 298 : use_operand_p use_p;
3237 : 298 : gimple *use_stmt;
3238 : 298 : bool res = single_imm_use (gimple_phi_result (stmt_info->stmt),
3239 : : &use_p, &use_stmt);
3240 : 298 : gcc_assert (res);
3241 : 298 : reduc_phi = loop_vinfo->lookup_stmt (use_stmt);
3242 : : }
3243 : 70007 : VECT_REDUC_INFO_DEF_TYPE (reduc_info) = STMT_VINFO_DEF_TYPE (stmt_info);
3244 : 70007 : VECT_REDUC_INFO_TYPE (reduc_info) = STMT_VINFO_REDUC_TYPE (reduc_phi);
3245 : 70007 : VECT_REDUC_INFO_CODE (reduc_info) = STMT_VINFO_REDUC_CODE (reduc_phi);
3246 : 70007 : VECT_REDUC_INFO_FN (reduc_info) = IFN_LAST;
3247 : : }
3248 : : return node;
3249 : 8191392 : }
3250 : :
3251 : : /* Dump a single SLP tree NODE. */
3252 : :
3253 : : static void
3254 : 424745 : vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
3255 : : slp_tree node)
3256 : : {
3257 : 424745 : unsigned i, j;
3258 : 424745 : slp_tree child;
3259 : 424745 : stmt_vec_info stmt_info;
3260 : 424745 : tree op;
3261 : :
3262 : 424745 : dump_metadata_t metadata (dump_kind, loc.get_impl_location ());
3263 : 424745 : dump_user_location_t user_loc = loc.get_user_location ();
3264 : 424745 : dump_printf_loc (metadata, user_loc,
3265 : : "node%s %p (max_nunits=" HOST_WIDE_INT_PRINT_UNSIGNED
3266 : : ", refcnt=%u)",
3267 : 424745 : SLP_TREE_DEF_TYPE (node) == vect_external_def
3268 : : ? " (external)"
3269 : : : (SLP_TREE_DEF_TYPE (node) == vect_constant_def
3270 : 409860 : ? " (constant)"
3271 : : : ""), (void *) node,
3272 : 424745 : estimated_poly_value (node->max_nunits),
3273 : : SLP_TREE_REF_COUNT (node));
3274 : 424745 : if (SLP_TREE_VECTYPE (node))
3275 : 360168 : dump_printf (metadata, " %T", SLP_TREE_VECTYPE (node));
3276 : 424745 : dump_printf (metadata, "%s",
3277 : 424745 : node->avoid_stlf_fail ? " (avoid-stlf-fail)" : "");
3278 : 424745 : if (node->cycle_info.id != -1 || node->cycle_info.reduc_idx != -1)
3279 : 22007 : dump_printf (metadata, " cycle %d, link %d", node->cycle_info.id,
3280 : : node->cycle_info.reduc_idx);
3281 : 424745 : dump_printf (metadata, "\n");
3282 : 424745 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
3283 : : {
3284 : 345260 : if (SLP_TREE_PERMUTE_P (node))
3285 : 12595 : dump_printf_loc (metadata, user_loc, "op: VEC_PERM_EXPR\n");
3286 : : else
3287 : 332665 : dump_printf_loc (metadata, user_loc, "op template: %G",
3288 : 332665 : SLP_TREE_REPRESENTATIVE (node)->stmt);
3289 : : }
3290 : 424745 : if (SLP_TREE_SCALAR_STMTS (node).exists ())
3291 : 822324 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3292 : 484814 : if (stmt_info)
3293 : 479887 : dump_printf_loc (metadata, user_loc, "\t%sstmt %u %G",
3294 : 479887 : STMT_VINFO_LIVE_P (stmt_info) ? "[l] " : "",
3295 : : i, stmt_info->stmt);
3296 : : else
3297 : 4927 : dump_printf_loc (metadata, user_loc, "\tstmt %u ---\n", i);
3298 : : else
3299 : : {
3300 : 87235 : dump_printf_loc (metadata, user_loc, "\t{ ");
3301 : 277937 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
3302 : 103467 : dump_printf (metadata, "%T%s ", op,
3303 : 103467 : i < SLP_TREE_SCALAR_OPS (node).length () - 1 ? "," : "");
3304 : 87235 : dump_printf (metadata, "}\n");
3305 : : }
3306 : 424745 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
3307 : : {
3308 : 60611 : dump_printf_loc (metadata, user_loc, "\tload permutation {");
3309 : 198249 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), i, j)
3310 : 77027 : dump_printf (dump_kind, " %u", j);
3311 : 60611 : dump_printf (dump_kind, " }\n");
3312 : : }
3313 : 424745 : if (SLP_TREE_LANE_PERMUTATION (node).exists ())
3314 : : {
3315 : 12603 : dump_printf_loc (metadata, user_loc, "\tlane permutation {");
3316 : 59692 : for (i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
3317 : 34486 : dump_printf (dump_kind, " %u[%u]",
3318 : 34486 : SLP_TREE_LANE_PERMUTATION (node)[i].first,
3319 : 34486 : SLP_TREE_LANE_PERMUTATION (node)[i].second);
3320 : 12603 : dump_printf (dump_kind, " }%s\n",
3321 : 12603 : node->ldst_lanes ? " (load-lanes)" : "");
3322 : : }
3323 : 424745 : if (SLP_TREE_CHILDREN (node).is_empty ())
3324 : 161974 : return;
3325 : 262771 : dump_printf_loc (metadata, user_loc, "\tchildren");
3326 : 955625 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3327 : 430083 : dump_printf (dump_kind, " %p", (void *)child);
3328 : 262771 : dump_printf (dump_kind, "%s\n",
3329 : 262771 : node->ldst_lanes && !SLP_TREE_LANE_PERMUTATION (node).exists ()
3330 : : ? " (store-lanes)" : "");
3331 : : }
3332 : :
3333 : : DEBUG_FUNCTION void
3334 : 0 : debug (slp_tree node)
3335 : : {
3336 : 0 : debug_dump_context ctx;
3337 : 0 : vect_print_slp_tree (MSG_NOTE,
3338 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3339 : : node);
3340 : 0 : }
3341 : :
3342 : : /* Recursive helper for the dot producer below. */
3343 : :
3344 : : static void
3345 : 0 : dot_slp_tree (FILE *f, slp_tree node, hash_set<slp_tree> &visited)
3346 : : {
3347 : 0 : if (visited.add (node))
3348 : : return;
3349 : :
3350 : 0 : fprintf (f, "\"%p\" [label=\"", (void *)node);
3351 : 0 : vect_print_slp_tree (MSG_NOTE,
3352 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3353 : : node);
3354 : 0 : fprintf (f, "\"];\n");
3355 : :
3356 : :
3357 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3358 : 0 : fprintf (f, "\"%p\" -> \"%p\";", (void *)node, (void *)child);
3359 : :
3360 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3361 : 0 : if (child)
3362 : 0 : dot_slp_tree (f, child, visited);
3363 : : }
3364 : :
3365 : : DEBUG_FUNCTION void
3366 : 0 : dot_slp_tree (const char *fname, slp_tree node)
3367 : : {
3368 : 0 : FILE *f = fopen (fname, "w");
3369 : 0 : fprintf (f, "digraph {\n");
3370 : 0 : fflush (f);
3371 : 0 : {
3372 : 0 : debug_dump_context ctx (f);
3373 : 0 : hash_set<slp_tree> visited;
3374 : 0 : dot_slp_tree (f, node, visited);
3375 : 0 : }
3376 : 0 : fflush (f);
3377 : 0 : fprintf (f, "}\n");
3378 : 0 : fclose (f);
3379 : 0 : }
3380 : :
3381 : : DEBUG_FUNCTION void
3382 : 0 : dot_slp_tree (const char *fname, const vec<slp_instance> &slp_instances)
3383 : : {
3384 : 0 : FILE *f = fopen (fname, "w");
3385 : 0 : fprintf (f, "digraph {\n");
3386 : 0 : fflush (f);
3387 : 0 : {
3388 : 0 : debug_dump_context ctx (f);
3389 : 0 : hash_set<slp_tree> visited;
3390 : 0 : for (auto inst : slp_instances)
3391 : 0 : dot_slp_tree (f, SLP_INSTANCE_TREE (inst), visited);
3392 : 0 : }
3393 : 0 : fflush (f);
3394 : 0 : fprintf (f, "}\n");
3395 : 0 : fclose (f);
3396 : 0 : }
3397 : :
3398 : : /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
3399 : :
3400 : : static void
3401 : 462103 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3402 : : slp_tree node, hash_set<slp_tree> &visited)
3403 : : {
3404 : 462103 : unsigned i;
3405 : 462103 : slp_tree child;
3406 : :
3407 : 462103 : if (visited.add (node))
3408 : 462103 : return;
3409 : :
3410 : 424327 : vect_print_slp_tree (dump_kind, loc, node);
3411 : :
3412 : 1278246 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3413 : 429592 : if (child)
3414 : 390764 : vect_print_slp_graph (dump_kind, loc, child, visited);
3415 : : }
3416 : :
3417 : : static void
3418 : 43949 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3419 : : slp_tree entry)
3420 : : {
3421 : 43949 : hash_set<slp_tree> visited;
3422 : 43949 : vect_print_slp_graph (dump_kind, loc, entry, visited);
3423 : 43949 : }
3424 : :
3425 : : DEBUG_FUNCTION void
3426 : 0 : debug (slp_instance instance)
3427 : : {
3428 : 0 : debug_dump_context ctx;
3429 : 0 : vect_print_slp_graph (MSG_NOTE,
3430 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3431 : : SLP_INSTANCE_TREE (instance));
3432 : 0 : }
3433 : :
3434 : : /* Mark the tree rooted at NODE with PURE_SLP. */
3435 : :
3436 : : static void
3437 : 5550387 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node,
3438 : : hash_set<slp_tree> &visited)
3439 : : {
3440 : 5550387 : int i;
3441 : 5550387 : stmt_vec_info stmt_info;
3442 : 5550387 : slp_tree child;
3443 : :
3444 : 5550387 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3445 : : return;
3446 : :
3447 : 3988249 : if (visited.add (node))
3448 : : return;
3449 : :
3450 : 9547227 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3451 : 5838474 : if (stmt_info)
3452 : : {
3453 : 5763635 : STMT_SLP_TYPE (stmt_info) = pure_slp;
3454 : : /* ??? For .MASK_LOAD and .MASK_STORE detected as load/store-lanes
3455 : : when there is the mask_conversion pattern applied we have lost the
3456 : : alternate lanes of the uniform mask which nevertheless
3457 : : have separate pattern defs. To not confuse hybrid
3458 : : analysis we mark those as covered as well here. */
3459 : 5763635 : if (node->ldst_lanes)
3460 : 5838474 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
3461 : 0 : if (gimple_call_internal_p (call, IFN_MASK_LOAD)
3462 : 0 : || gimple_call_internal_p (call, IFN_MASK_STORE))
3463 : : {
3464 : 0 : tree mask = gimple_call_arg (call,
3465 : : internal_fn_mask_index
3466 : 0 : (gimple_call_internal_fn (call)));
3467 : 0 : if (TREE_CODE (mask) == SSA_NAME)
3468 : 0 : if (stmt_vec_info mask_info = vinfo->lookup_def (mask))
3469 : : {
3470 : 0 : mask_info = vect_stmt_to_vectorize (mask_info);
3471 : 0 : STMT_SLP_TYPE (mask_info) = pure_slp;
3472 : : }
3473 : : }
3474 : : }
3475 : :
3476 : 8310489 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3477 : 4601736 : if (child)
3478 : 4138115 : vect_mark_slp_stmts (vinfo, child, visited);
3479 : : }
3480 : :
3481 : : static void
3482 : 1412272 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node)
3483 : : {
3484 : 1412272 : hash_set<slp_tree> visited;
3485 : 1412272 : vect_mark_slp_stmts (vinfo, node, visited);
3486 : 1412272 : }
3487 : :
3488 : : /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
3489 : :
3490 : : static void
3491 : 2348850 : vect_mark_slp_stmts_relevant (slp_tree node, hash_set<slp_tree> &visited)
3492 : : {
3493 : 2348850 : int i;
3494 : 2348850 : stmt_vec_info stmt_info;
3495 : 2348850 : slp_tree child;
3496 : :
3497 : 2348850 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3498 : : return;
3499 : :
3500 : 1374209 : if (visited.add (node))
3501 : : return;
3502 : :
3503 : 4291174 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3504 : 3020994 : if (stmt_info)
3505 : : {
3506 : 3020994 : gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
3507 : : || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
3508 : 3020994 : STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
3509 : : }
3510 : :
3511 : 2830261 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3512 : 1560081 : if (child)
3513 : 1560081 : vect_mark_slp_stmts_relevant (child, visited);
3514 : : }
3515 : :
3516 : : static void
3517 : 788769 : vect_mark_slp_stmts_relevant (slp_tree node)
3518 : : {
3519 : 788769 : hash_set<slp_tree> visited;
3520 : 788769 : vect_mark_slp_stmts_relevant (node, visited);
3521 : 788769 : }
3522 : :
3523 : :
3524 : : /* Gather loads in the SLP graph NODE and populate the INST loads array. */
3525 : :
3526 : : static void
3527 : 8971472 : vect_gather_slp_loads (vec<slp_tree> &loads, slp_tree node,
3528 : : hash_set<slp_tree> &visited)
3529 : : {
3530 : 8971472 : if (!node || visited.add (node))
3531 : 1315111 : return;
3532 : :
3533 : 7656361 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3534 : : return;
3535 : :
3536 : 5602860 : if (!SLP_TREE_PERMUTE_P (node))
3537 : : {
3538 : 5382097 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
3539 : 5382097 : if (STMT_VINFO_DATA_REF (stmt_info)
3540 : 2421483 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
3541 : 1322925 : loads.safe_push (node);
3542 : : }
3543 : :
3544 : : unsigned i;
3545 : : slp_tree child;
3546 : 12595290 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3547 : 6992430 : vect_gather_slp_loads (loads, child, visited);
3548 : : }
3549 : :
3550 : :
3551 : : /* Find the last store in SLP INSTANCE. */
3552 : :
3553 : : stmt_vec_info
3554 : 2734939 : vect_find_last_scalar_stmt_in_slp (slp_tree node)
3555 : : {
3556 : 2734939 : stmt_vec_info last = NULL;
3557 : 2734939 : stmt_vec_info stmt_vinfo;
3558 : :
3559 : 9975535 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3560 : 7240596 : if (stmt_vinfo)
3561 : : {
3562 : 7240596 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3563 : 7240596 : last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
3564 : : }
3565 : :
3566 : 2734939 : return last;
3567 : : }
3568 : :
3569 : : /* Find the first stmt in NODE. */
3570 : :
3571 : : stmt_vec_info
3572 : 519286 : vect_find_first_scalar_stmt_in_slp (slp_tree node)
3573 : : {
3574 : 519286 : stmt_vec_info first = NULL;
3575 : 519286 : stmt_vec_info stmt_vinfo;
3576 : :
3577 : 1752335 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3578 : 1233049 : if (stmt_vinfo)
3579 : : {
3580 : 1230416 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3581 : 1230416 : if (!first
3582 : 1230416 : || get_later_stmt (stmt_vinfo, first) == first)
3583 : : first = stmt_vinfo;
3584 : : }
3585 : :
3586 : 519286 : return first;
3587 : : }
3588 : :
3589 : : /* Splits a group of stores, currently beginning at FIRST_VINFO, into
3590 : : two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
3591 : : (also containing the first GROUP1_SIZE stmts, since stores are
3592 : : consecutive), the second containing the remainder.
3593 : : Return the first stmt in the second group. */
3594 : :
3595 : : static stmt_vec_info
3596 : 157855 : vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
3597 : : {
3598 : 157855 : gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
3599 : 157855 : gcc_assert (group1_size > 0);
3600 : 157855 : int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
3601 : 157855 : gcc_assert (group2_size > 0);
3602 : 157855 : DR_GROUP_SIZE (first_vinfo) = group1_size;
3603 : :
3604 : 157855 : stmt_vec_info stmt_info = first_vinfo;
3605 : 530139 : for (unsigned i = group1_size; i > 1; i--)
3606 : : {
3607 : 372284 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
3608 : 372284 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3609 : : }
3610 : : /* STMT is now the last element of the first group. */
3611 : 157855 : stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
3612 : 157855 : DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
3613 : :
3614 : 157855 : DR_GROUP_SIZE (group2) = group2_size;
3615 : 439708 : for (stmt_info = group2; stmt_info;
3616 : 281853 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
3617 : : {
3618 : 281853 : DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
3619 : 281853 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3620 : : }
3621 : :
3622 : : /* For the second group, the DR_GROUP_GAP is that before the original group,
3623 : : plus skipping over the first vector. */
3624 : 157855 : DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
3625 : :
3626 : : /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
3627 : 157855 : DR_GROUP_GAP (first_vinfo) += group2_size;
3628 : :
3629 : 157855 : if (dump_enabled_p ())
3630 : 61 : dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
3631 : : group1_size, group2_size);
3632 : :
3633 : 157855 : return group2;
3634 : : }
3635 : :
3636 : : /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
3637 : : statements and a vector of NUNITS elements. */
3638 : :
3639 : : static poly_uint64
3640 : 3617712 : calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
3641 : : {
3642 : 3617712 : return exact_div (common_multiple (nunits, group_size), group_size);
3643 : : }
3644 : :
3645 : : /* Helper that checks to see if a node is a load node. */
3646 : :
3647 : : static inline bool
3648 : 54 : vect_is_slp_load_node (slp_tree root)
3649 : : {
3650 : 54 : return (!SLP_TREE_PERMUTE_P (root)
3651 : 54 : && SLP_TREE_DEF_TYPE (root) == vect_internal_def
3652 : 48 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root))
3653 : 94 : && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))));
3654 : : }
3655 : :
3656 : :
3657 : : /* Helper function of optimize_load_redistribution that performs the operation
3658 : : recursively. */
3659 : :
3660 : : static slp_tree
3661 : 20054 : optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t *bst_map,
3662 : : vec_info *vinfo, unsigned int group_size,
3663 : : hash_map<slp_tree, slp_tree> *load_map,
3664 : : slp_tree root)
3665 : : {
3666 : 20054 : if (slp_tree *leader = load_map->get (root))
3667 : 3579 : return *leader;
3668 : :
3669 : 16475 : slp_tree node;
3670 : 16475 : unsigned i;
3671 : :
3672 : : /* For now, we don't know anything about externals so do not do anything. */
3673 : 16475 : if (!root || SLP_TREE_DEF_TYPE (root) != vect_internal_def)
3674 : : return NULL;
3675 : 11975 : else if (SLP_TREE_PERMUTE_P (root))
3676 : : {
3677 : : /* First convert this node into a load node and add it to the leaves
3678 : : list and flatten the permute from a lane to a load one. If it's
3679 : : unneeded it will be elided later. */
3680 : 34 : vec<stmt_vec_info> stmts;
3681 : 34 : stmts.create (SLP_TREE_LANES (root));
3682 : 34 : lane_permutation_t lane_perm = SLP_TREE_LANE_PERMUTATION (root);
3683 : 74 : for (unsigned j = 0; j < lane_perm.length (); j++)
3684 : : {
3685 : 54 : std::pair<unsigned, unsigned> perm = lane_perm[j];
3686 : 54 : node = SLP_TREE_CHILDREN (root)[perm.first];
3687 : :
3688 : 54 : if (!vect_is_slp_load_node (node)
3689 : 54 : || SLP_TREE_CHILDREN (node).exists ())
3690 : : {
3691 : 14 : stmts.release ();
3692 : 14 : goto next;
3693 : : }
3694 : :
3695 : 40 : stmts.quick_push (SLP_TREE_SCALAR_STMTS (node)[perm.second]);
3696 : : }
3697 : :
3698 : 20 : if (dump_enabled_p ())
3699 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
3700 : : "converting stmts on permute node %p\n",
3701 : : (void *) root);
3702 : :
3703 : 20 : bool *matches = XALLOCAVEC (bool, group_size);
3704 : 20 : poly_uint64 max_nunits = 1;
3705 : 20 : unsigned tree_size = 0, limit = 1;
3706 : 20 : node = vect_build_slp_tree (vinfo, stmts, group_size, &max_nunits,
3707 : : matches, &limit, &tree_size, bst_map);
3708 : 20 : if (!node)
3709 : 0 : stmts.release ();
3710 : :
3711 : 20 : load_map->put (root, node);
3712 : 20 : return node;
3713 : : }
3714 : :
3715 : 11941 : next:
3716 : 11955 : load_map->put (root, NULL);
3717 : :
3718 : 28279 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3719 : : {
3720 : 16324 : slp_tree value
3721 : 16324 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3722 : : node);
3723 : 16324 : if (value)
3724 : : {
3725 : 20 : SLP_TREE_REF_COUNT (value)++;
3726 : 20 : SLP_TREE_CHILDREN (root)[i] = value;
3727 : : /* ??? We know the original leafs of the replaced nodes will
3728 : : be referenced by bst_map, only the permutes created by
3729 : : pattern matching are not. */
3730 : 20 : if (SLP_TREE_REF_COUNT (node) == 1)
3731 : 20 : load_map->remove (node);
3732 : 20 : vect_free_slp_tree (node);
3733 : : }
3734 : : }
3735 : :
3736 : : return NULL;
3737 : : }
3738 : :
3739 : : /* Temporary workaround for loads not being CSEd during SLP build. This
3740 : : function will traverse the SLP tree rooted in ROOT for INSTANCE and find
3741 : : VEC_PERM nodes that blend vectors from multiple nodes that all read from the
3742 : : same DR such that the final operation is equal to a permuted load. Such
3743 : : NODES are then directly converted into LOADS themselves. The nodes are
3744 : : CSEd using BST_MAP. */
3745 : :
3746 : : static void
3747 : 2814 : optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t *bst_map,
3748 : : vec_info *vinfo, unsigned int group_size,
3749 : : hash_map<slp_tree, slp_tree> *load_map,
3750 : : slp_tree root)
3751 : : {
3752 : 2814 : slp_tree node;
3753 : 2814 : unsigned i;
3754 : :
3755 : 6544 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3756 : : {
3757 : 3730 : slp_tree value
3758 : 3730 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3759 : : node);
3760 : 3730 : if (value)
3761 : : {
3762 : 0 : SLP_TREE_REF_COUNT (value)++;
3763 : 0 : SLP_TREE_CHILDREN (root)[i] = value;
3764 : : /* ??? We know the original leafs of the replaced nodes will
3765 : : be referenced by bst_map, only the permutes created by
3766 : : pattern matching are not. */
3767 : 0 : if (SLP_TREE_REF_COUNT (node) == 1)
3768 : 0 : load_map->remove (node);
3769 : 0 : vect_free_slp_tree (node);
3770 : : }
3771 : : }
3772 : 2814 : }
3773 : :
3774 : : /* Helper function of vect_match_slp_patterns.
3775 : :
3776 : : Attempts to match patterns against the slp tree rooted in REF_NODE using
3777 : : VINFO. Patterns are matched in post-order traversal.
3778 : :
3779 : : If matching is successful the value in REF_NODE is updated and returned, if
3780 : : not then it is returned unchanged. */
3781 : :
3782 : : static bool
3783 : 5339249 : vect_match_slp_patterns_2 (slp_tree *ref_node, vec_info *vinfo,
3784 : : slp_tree_to_load_perm_map_t *perm_cache,
3785 : : slp_compat_nodes_map_t *compat_cache,
3786 : : hash_set<slp_tree> *visited)
3787 : : {
3788 : 5339249 : unsigned i;
3789 : 5339249 : slp_tree node = *ref_node;
3790 : 5339249 : bool found_p = false;
3791 : 5339249 : if (!node || visited->add (node))
3792 : 668374 : return false;
3793 : :
3794 : : slp_tree child;
3795 : 8590178 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3796 : 3919303 : found_p |= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node)[i],
3797 : : vinfo, perm_cache, compat_cache,
3798 : : visited);
3799 : :
3800 : 14012625 : for (unsigned x = 0; x < num__slp_patterns; x++)
3801 : : {
3802 : 9341750 : vect_pattern *pattern
3803 : 9341750 : = slp_patterns[x] (perm_cache, compat_cache, ref_node);
3804 : 9341750 : if (pattern)
3805 : : {
3806 : 1072 : pattern->build (vinfo);
3807 : 1072 : delete pattern;
3808 : 1072 : found_p = true;
3809 : : }
3810 : : }
3811 : :
3812 : : return found_p;
3813 : : }
3814 : :
3815 : : /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
3816 : : vec_info VINFO.
3817 : :
3818 : : The modified tree is returned. Patterns are tried in order and multiple
3819 : : patterns may match. */
3820 : :
3821 : : static bool
3822 : 1419946 : vect_match_slp_patterns (slp_instance instance, vec_info *vinfo,
3823 : : hash_set<slp_tree> *visited,
3824 : : slp_tree_to_load_perm_map_t *perm_cache,
3825 : : slp_compat_nodes_map_t *compat_cache)
3826 : : {
3827 : 1419946 : DUMP_VECT_SCOPE ("vect_match_slp_patterns");
3828 : 1419946 : slp_tree *ref_node = &SLP_INSTANCE_TREE (instance);
3829 : :
3830 : 1419946 : if (dump_enabled_p ())
3831 : 28532 : dump_printf_loc (MSG_NOTE, vect_location,
3832 : : "Analyzing SLP tree %p for patterns\n",
3833 : 28532 : (void *) SLP_INSTANCE_TREE (instance));
3834 : :
3835 : 1419946 : return vect_match_slp_patterns_2 (ref_node, vinfo, perm_cache, compat_cache,
3836 : 1419946 : visited);
3837 : : }
3838 : :
3839 : : /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
3840 : : vectorizing with VECTYPE that might be NULL. MASKED_P indicates whether
3841 : : the stores are masked.
3842 : : Return true if we could use IFN_STORE_LANES instead and if that appears
3843 : : to be the better approach. */
3844 : :
3845 : : static bool
3846 : 4897 : vect_slp_prefer_store_lanes_p (vec_info *vinfo, stmt_vec_info stmt_info,
3847 : : tree vectype, bool masked_p,
3848 : : unsigned int group_size,
3849 : : unsigned int new_group_size)
3850 : : {
3851 : 4897 : if (!vectype)
3852 : : {
3853 : 4897 : tree scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
3854 : 4897 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
3855 : : }
3856 : 4897 : if (!vectype)
3857 : : return false;
3858 : : /* Allow the split if one of the two new groups would operate on full
3859 : : vectors *within* rather than across one scalar loop iteration.
3860 : : This is purely a heuristic, but it should work well for group
3861 : : sizes of 3 and 4, where the possible splits are:
3862 : :
3863 : : 3->2+1: OK if the vector has exactly two elements
3864 : : 4->2+2: Likewise
3865 : : 4->3+1: Less clear-cut. */
3866 : 4897 : if (multiple_p (group_size - new_group_size, TYPE_VECTOR_SUBPARTS (vectype))
3867 : 2543 : || multiple_p (new_group_size, TYPE_VECTOR_SUBPARTS (vectype)))
3868 : 2373 : return false;
3869 : 2524 : return vect_store_lanes_supported (vectype, group_size, masked_p) != IFN_LAST;
3870 : : }
3871 : :
3872 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
3873 : : vect_build_slp_tree to build a tree of packed stmts if possible.
3874 : : Return FALSE if it's impossible to SLP any stmt in the loop. */
3875 : :
3876 : : static bool
3877 : : vect_analyze_slp_instance (vec_info *vinfo,
3878 : : scalar_stmts_to_slp_tree_map_t *bst_map,
3879 : : stmt_vec_info stmt_info, slp_instance_kind kind,
3880 : : unsigned max_tree_size, unsigned *limit,
3881 : : bool force_single_lane);
3882 : :
3883 : : /* Build an interleaving scheme for the store sources RHS_NODES from
3884 : : SCALAR_STMTS. */
3885 : :
3886 : : static slp_tree
3887 : 6168 : vect_build_slp_store_interleaving (vec<slp_tree> &rhs_nodes,
3888 : : vec<stmt_vec_info> &scalar_stmts,
3889 : : poly_uint64 max_nunits)
3890 : : {
3891 : 6168 : unsigned int group_size = scalar_stmts.length ();
3892 : 12336 : slp_tree node = vect_create_new_slp_node (scalar_stmts,
3893 : 6168 : SLP_TREE_CHILDREN
3894 : : (rhs_nodes[0]).length ());
3895 : 6168 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
3896 : 6168 : node->max_nunits = max_nunits;
3897 : 6168 : for (unsigned l = 0;
3898 : 12363 : l < SLP_TREE_CHILDREN (rhs_nodes[0]).length (); ++l)
3899 : : {
3900 : : /* And a permute merging all RHS SLP trees. */
3901 : 6195 : slp_tree perm = vect_create_new_slp_node (rhs_nodes.length (),
3902 : 6195 : VEC_PERM_EXPR);
3903 : 6195 : SLP_TREE_CHILDREN (node).quick_push (perm);
3904 : 6195 : SLP_TREE_LANE_PERMUTATION (perm).create (group_size);
3905 : 6195 : SLP_TREE_VECTYPE (perm) = SLP_TREE_VECTYPE (node);
3906 : 6195 : perm->max_nunits = max_nunits;
3907 : 6195 : SLP_TREE_LANES (perm) = group_size;
3908 : : /* ??? We should set this NULL but that's not expected. */
3909 : 6195 : SLP_TREE_REPRESENTATIVE (perm)
3910 : 6195 : = SLP_TREE_REPRESENTATIVE (SLP_TREE_CHILDREN (rhs_nodes[0])[l]);
3911 : 24465 : for (unsigned j = 0; j < rhs_nodes.length (); ++j)
3912 : : {
3913 : 18270 : SLP_TREE_CHILDREN (perm)
3914 : 18270 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[l]);
3915 : 18270 : SLP_TREE_CHILDREN (rhs_nodes[j])[l]->refcnt++;
3916 : 18270 : for (unsigned k = 0;
3917 : 38549 : k < SLP_TREE_SCALAR_STMTS (rhs_nodes[j]).length (); ++k)
3918 : : {
3919 : : /* ??? We should populate SLP_TREE_SCALAR_STMTS
3920 : : or SLP_TREE_SCALAR_OPS but then we might have
3921 : : a mix of both in our children. */
3922 : 20279 : SLP_TREE_LANE_PERMUTATION (perm)
3923 : 20279 : .quick_push (std::make_pair (j, k));
3924 : : }
3925 : : }
3926 : :
3927 : : /* Now we have a single permute node but we cannot code-generate
3928 : : the case with more than two inputs.
3929 : : Perform pairwise reduction, reducing the two inputs
3930 : : with the least number of lanes to one and then repeat until
3931 : : we end up with two inputs. That scheme makes sure we end
3932 : : up with permutes satisfying the restriction of requiring at
3933 : : most two vector inputs to produce a single vector output
3934 : : when the number of lanes is even. */
3935 : 12075 : while (SLP_TREE_CHILDREN (perm).length () > 2)
3936 : : {
3937 : : /* When we have three equal sized groups left the pairwise
3938 : : reduction does not result in a scheme that avoids using
3939 : : three vectors. Instead merge the first two groups
3940 : : to the final size with do-not-care elements (chosen
3941 : : from the first group) and then merge with the third.
3942 : : { A0, B0, x, A1, B1, x, ... }
3943 : : -> { A0, B0, C0, A1, B1, C1, ... }
3944 : : This handles group size of three (and at least
3945 : : power-of-two multiples of that). */
3946 : 5880 : if (SLP_TREE_CHILDREN (perm).length () == 3
3947 : 3037 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3948 : 3037 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[1]))
3949 : 5880 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3950 : 2294 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[2])))
3951 : : {
3952 : 2098 : int ai = 0;
3953 : 2098 : int bi = 1;
3954 : 2098 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
3955 : 2098 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
3956 : 2098 : unsigned n = SLP_TREE_LANES (perm);
3957 : :
3958 : 2098 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
3959 : 2098 : SLP_TREE_LANES (permab) = n;
3960 : 2098 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
3961 : 2098 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
3962 : 2098 : permab->max_nunits = max_nunits;
3963 : : /* ??? Should be NULL but that's not expected. */
3964 : 2098 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
3965 : 2098 : SLP_TREE_CHILDREN (permab).quick_push (a);
3966 : 4205 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3967 : 2107 : SLP_TREE_LANE_PERMUTATION (permab)
3968 : 2107 : .quick_push (std::make_pair (0, k));
3969 : 2098 : SLP_TREE_CHILDREN (permab).quick_push (b);
3970 : 4205 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
3971 : 2107 : SLP_TREE_LANE_PERMUTATION (permab)
3972 : 2107 : .quick_push (std::make_pair (1, k));
3973 : : /* Push the do-not-care lanes. */
3974 : 4205 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3975 : 2107 : SLP_TREE_LANE_PERMUTATION (permab)
3976 : 2107 : .quick_push (std::make_pair (0, k));
3977 : :
3978 : : /* Put the merged node into 'perm', in place of a. */
3979 : 2098 : SLP_TREE_CHILDREN (perm)[ai] = permab;
3980 : : /* Adjust the references to b in the permutation
3981 : : of perm and to the later children which we'll
3982 : : remove. */
3983 : 8419 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
3984 : : {
3985 : 6321 : std::pair<unsigned, unsigned> &p
3986 : 6321 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
3987 : 6321 : if (p.first == (unsigned) bi)
3988 : : {
3989 : 2107 : p.first = ai;
3990 : 2107 : p.second += SLP_TREE_LANES (a);
3991 : : }
3992 : 4214 : else if (p.first > (unsigned) bi)
3993 : 2107 : p.first--;
3994 : : }
3995 : 2098 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
3996 : 2098 : break;
3997 : : }
3998 : :
3999 : : /* Pick the two nodes with the least number of lanes,
4000 : : prefer the earliest candidate and maintain ai < bi. */
4001 : : int ai = -1;
4002 : : int bi = -1;
4003 : 33073 : for (unsigned ci = 0; ci < SLP_TREE_CHILDREN (perm).length (); ++ci)
4004 : : {
4005 : 29291 : if (ai == -1)
4006 : 3782 : ai = ci;
4007 : 25509 : else if (bi == -1)
4008 : 3782 : bi = ci;
4009 : 21727 : else if ((SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
4010 : 21727 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai]))
4011 : 21727 : || (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
4012 : 17813 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi])))
4013 : : {
4014 : 8716 : if (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai])
4015 : 4358 : <= SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi]))
4016 : 2075 : bi = ci;
4017 : : else
4018 : : {
4019 : 2283 : ai = bi;
4020 : 2283 : bi = ci;
4021 : : }
4022 : : }
4023 : : }
4024 : :
4025 : : /* Produce a merge of nodes ai and bi. */
4026 : 3782 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
4027 : 3782 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
4028 : 3782 : unsigned n = SLP_TREE_LANES (a) + SLP_TREE_LANES (b);
4029 : 3782 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
4030 : 3782 : SLP_TREE_LANES (permab) = n;
4031 : 3782 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
4032 : 3782 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
4033 : 3782 : permab->max_nunits = max_nunits;
4034 : : /* ??? Should be NULL but that's not expected. */
4035 : 3782 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
4036 : 3782 : SLP_TREE_CHILDREN (permab).quick_push (a);
4037 : 9888 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
4038 : 6106 : SLP_TREE_LANE_PERMUTATION (permab)
4039 : 6106 : .quick_push (std::make_pair (0, k));
4040 : 3782 : SLP_TREE_CHILDREN (permab).quick_push (b);
4041 : 9400 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
4042 : 5618 : SLP_TREE_LANE_PERMUTATION (permab)
4043 : 5618 : .quick_push (std::make_pair (1, k));
4044 : :
4045 : : /* Put the merged node into 'perm', in place of a. */
4046 : 3782 : SLP_TREE_CHILDREN (perm)[ai] = permab;
4047 : : /* Adjust the references to b in the permutation
4048 : : of perm and to the later children which we'll
4049 : : remove. */
4050 : 52699 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
4051 : : {
4052 : 48917 : std::pair<unsigned, unsigned> &p
4053 : 48917 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
4054 : 48917 : if (p.first == (unsigned) bi)
4055 : : {
4056 : 5618 : p.first = ai;
4057 : 5618 : p.second += SLP_TREE_LANES (a);
4058 : : }
4059 : 43299 : else if (p.first > (unsigned) bi)
4060 : 17862 : p.first--;
4061 : : }
4062 : 3782 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
4063 : : }
4064 : : }
4065 : :
4066 : 6168 : return node;
4067 : : }
4068 : :
4069 : : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4070 : : of KIND. Return true if successful. SCALAR_STMTS is owned by this
4071 : : function, REMAIN and ROOT_STMT_INFOS ownership is transfered back to
4072 : : the caller upon failure. */
4073 : :
4074 : : static bool
4075 : 1779758 : vect_build_slp_instance (vec_info *vinfo,
4076 : : slp_instance_kind kind,
4077 : : vec<stmt_vec_info> &scalar_stmts,
4078 : : vec<stmt_vec_info> &root_stmt_infos,
4079 : : vec<tree> &remain,
4080 : : unsigned max_tree_size, unsigned *limit,
4081 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4082 : : bool force_single_lane)
4083 : : {
4084 : : /* If there's no budget left bail out early. */
4085 : 1779758 : if (*limit == 0)
4086 : : {
4087 : 27205 : scalar_stmts.release ();
4088 : 27205 : return false;
4089 : : }
4090 : :
4091 : 1752553 : if (kind == slp_inst_kind_ctor)
4092 : : {
4093 : 12053 : if (dump_enabled_p ())
4094 : 86 : dump_printf_loc (MSG_NOTE, vect_location,
4095 : : "Analyzing vectorizable constructor: %G\n",
4096 : 43 : root_stmt_infos[0]->stmt);
4097 : : }
4098 : 1740500 : else if (kind == slp_inst_kind_gcond)
4099 : : {
4100 : 251573 : if (dump_enabled_p ())
4101 : 5166 : dump_printf_loc (MSG_NOTE, vect_location,
4102 : : "Analyzing vectorizable control flow: %G",
4103 : 2583 : root_stmt_infos[0]->stmt);
4104 : : }
4105 : :
4106 : 1752553 : if (dump_enabled_p ())
4107 : : {
4108 : 23893 : dump_printf_loc (MSG_NOTE, vect_location,
4109 : : "Starting SLP discovery for\n");
4110 : 51019 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4111 : 54252 : dump_printf_loc (MSG_NOTE, vect_location,
4112 : 27126 : " %G", scalar_stmts[i]->stmt);
4113 : : }
4114 : :
4115 : : /* Build the tree for the SLP instance. */
4116 : 1752553 : unsigned int group_size = scalar_stmts.length ();
4117 : 1752553 : bool *matches = XALLOCAVEC (bool, group_size);
4118 : 1752553 : poly_uint64 max_nunits = 1;
4119 : 1752553 : unsigned tree_size = 0;
4120 : :
4121 : 1752553 : slp_tree node = NULL;
4122 : 1752553 : if (group_size > 1 && force_single_lane)
4123 : : {
4124 : 0 : matches[0] = true;
4125 : 0 : matches[1] = false;
4126 : : }
4127 : : else
4128 : 1752553 : node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4129 : : &max_nunits, matches, limit,
4130 : : &tree_size, bst_map);
4131 : 1752553 : if (node != NULL)
4132 : : {
4133 : : /* Calculate the unrolling factor based on the smallest type. */
4134 : 668125 : poly_uint64 unrolling_factor
4135 : 668125 : = calculate_unrolling_factor (max_nunits, group_size);
4136 : :
4137 : 668125 : if (maybe_ne (unrolling_factor, 1U)
4138 : 668125 : && is_a <bb_vec_info> (vinfo))
4139 : : {
4140 : 0 : unsigned HOST_WIDE_INT const_max_nunits;
4141 : 0 : if (!max_nunits.is_constant (&const_max_nunits)
4142 : 0 : || const_max_nunits > group_size)
4143 : : {
4144 : 0 : if (dump_enabled_p ())
4145 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4146 : : "Build SLP failed: store group "
4147 : : "size not a multiple of the vector size "
4148 : : "in basic block SLP\n");
4149 : 0 : vect_free_slp_tree (node);
4150 : 0 : return false;
4151 : : }
4152 : : /* Fatal mismatch. */
4153 : 0 : if (dump_enabled_p ())
4154 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
4155 : : "SLP discovery succeeded but node needs "
4156 : : "splitting\n");
4157 : 0 : memset (matches, true, group_size);
4158 : 0 : matches[group_size / const_max_nunits * const_max_nunits] = false;
4159 : 0 : vect_free_slp_tree (node);
4160 : : }
4161 : : else
4162 : : {
4163 : : /* Create a new SLP instance. */
4164 : 668125 : slp_instance new_instance = XNEW (class _slp_instance);
4165 : 668125 : SLP_INSTANCE_TREE (new_instance) = node;
4166 : 668125 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4167 : 668125 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4168 : 668125 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4169 : 668125 : SLP_INSTANCE_KIND (new_instance) = kind;
4170 : 668125 : new_instance->reduc_phis = NULL;
4171 : 668125 : new_instance->cost_vec = vNULL;
4172 : 668125 : new_instance->subgraph_entries = vNULL;
4173 : :
4174 : 668125 : if (dump_enabled_p ())
4175 : 21025 : dump_printf_loc (MSG_NOTE, vect_location,
4176 : : "SLP size %u vs. limit %u.\n",
4177 : : tree_size, max_tree_size);
4178 : :
4179 : 668125 : vinfo->slp_instances.safe_push (new_instance);
4180 : :
4181 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4182 : : the number of scalar stmts in the root in a few places.
4183 : : Verify that assumption holds. */
4184 : 1336250 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4185 : : .length () == group_size);
4186 : :
4187 : 668125 : if (dump_enabled_p ())
4188 : : {
4189 : 21025 : if (kind == slp_inst_kind_reduc_group)
4190 : 1157 : dump_printf_loc (MSG_NOTE, vect_location,
4191 : : "SLP discovery of size %d reduction group "
4192 : : "succeeded\n", group_size);
4193 : 21025 : dump_printf_loc (MSG_NOTE, vect_location,
4194 : : "Final SLP tree for instance %p:\n",
4195 : : (void *) new_instance);
4196 : 21025 : vect_print_slp_graph (MSG_NOTE, vect_location,
4197 : : SLP_INSTANCE_TREE (new_instance));
4198 : : }
4199 : :
4200 : 668125 : return true;
4201 : : }
4202 : : }
4203 : : /* Failed to SLP. */
4204 : :
4205 : : /* While we arrive here even with slp_inst_kind_store we should only
4206 : : for group_size == 1. The code to split store groups is only in
4207 : : vect_analyze_slp_instance now. */
4208 : 1084428 : gcc_assert (kind != slp_inst_kind_store || group_size == 1);
4209 : :
4210 : : /* Free the allocated memory. */
4211 : 1084428 : scalar_stmts.release ();
4212 : :
4213 : : /* Failed to SLP. */
4214 : 1084428 : if (dump_enabled_p ())
4215 : 2868 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4216 : : return false;
4217 : : }
4218 : :
4219 : : /* Analyze an SLP instance starting from a the start of a reduction chain.
4220 : : Call vect_build_slp_tree to build a tree of packed stmts if possible.
4221 : : Return FALSE if SLP build fails. */
4222 : :
4223 : : static bool
4224 : 41696 : vect_analyze_slp_reduc_chain (loop_vec_info vinfo,
4225 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4226 : : stmt_vec_info scalar_stmt,
4227 : : unsigned max_tree_size, unsigned *limit)
4228 : : {
4229 : 41696 : vec<stmt_vec_info> scalar_stmts = vNULL;
4230 : :
4231 : 41696 : bool fail = false;
4232 : : /* ??? We could leave operation code checking to SLP discovery. */
4233 : 41696 : code_helper code = STMT_VINFO_REDUC_CODE (STMT_VINFO_REDUC_DEF
4234 : : (vect_orig_stmt (scalar_stmt)));
4235 : 41696 : bool first = true;
4236 : 41696 : stmt_vec_info next_stmt = scalar_stmt;
4237 : 46821 : do
4238 : : {
4239 : 46821 : stmt_vec_info stmt = next_stmt;
4240 : 46821 : gimple_match_op op;
4241 : 46821 : if (!gimple_extract_op (STMT_VINFO_STMT (stmt), &op))
4242 : 0 : gcc_unreachable ();
4243 : 93642 : tree reduc_def = gimple_arg (STMT_VINFO_STMT (stmt),
4244 : 46821 : STMT_VINFO_REDUC_IDX (stmt));
4245 : 46821 : next_stmt = vect_stmt_to_vectorize (vinfo->lookup_def (reduc_def));
4246 : 46821 : gcc_assert (is_a <gphi *> (STMT_VINFO_STMT (next_stmt))
4247 : : || STMT_VINFO_REDUC_IDX (next_stmt) != -1);
4248 : 50225 : if (!gimple_extract_op (STMT_VINFO_STMT (vect_orig_stmt (stmt)), &op))
4249 : 0 : gcc_unreachable ();
4250 : 46821 : if (CONVERT_EXPR_CODE_P (op.code)
4251 : 2145 : && tree_nop_conversion_p (op.type, TREE_TYPE (op.ops[0]))
4252 : 48954 : && (first
4253 : 1056 : || is_a <gphi *> (STMT_VINFO_STMT (next_stmt))))
4254 : : ;
4255 : 44690 : else if (code != op.code)
4256 : : {
4257 : 1716 : fail = true;
4258 : 1716 : break;
4259 : : }
4260 : : else
4261 : 42974 : scalar_stmts.safe_push (stmt);
4262 : 45105 : first = false;
4263 : : }
4264 : 45105 : while (!is_a <gphi *> (STMT_VINFO_STMT (next_stmt)));
4265 : 41696 : if (fail)
4266 : 1716 : return false;
4267 : :
4268 : : /* Remember a stmt with the actual reduction operation. */
4269 : 39980 : stmt_vec_info reduc_scalar_stmt = scalar_stmts[0];
4270 : :
4271 : : /* When the SSA def chain through reduc-idx does not form a natural
4272 : : reduction chain try to linearize an associative operation manually. */
4273 : 39980 : if (scalar_stmts.length () == 1
4274 : 38335 : && code.is_tree_code ()
4275 : 34949 : && associative_tree_code ((tree_code)code)
4276 : : /* We may not associate if a fold-left reduction is required. */
4277 : 74070 : && !needs_fold_left_reduction_p (TREE_TYPE (gimple_get_lhs
4278 : : (reduc_scalar_stmt->stmt)),
4279 : : code))
4280 : : {
4281 : 32269 : auto_vec<chain_op_t> chain;
4282 : 32269 : auto_vec<std::pair<tree_code, gimple *> > worklist;
4283 : 32269 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
4284 : 32269 : vect_slp_linearize_chain (vinfo, worklist, chain, (tree_code)code,
4285 : 32269 : scalar_stmts[0]->stmt, op_stmt, other_op_stmt,
4286 : : NULL);
4287 : :
4288 : 32269 : scalar_stmts.truncate (0);
4289 : 32269 : stmt_vec_info tail = NULL;
4290 : 160600 : for (auto el : chain)
4291 : : {
4292 : 64457 : if (el.dt == vect_external_def
4293 : 64457 : || el.dt == vect_constant_def
4294 : 64457 : || el.code != (tree_code) code)
4295 : : {
4296 : 664 : scalar_stmts.release ();
4297 : 664 : return false;
4298 : : }
4299 : 63793 : stmt_vec_info stmt = vinfo->lookup_def (el.op);
4300 : 63793 : if (STMT_VINFO_REDUC_IDX (stmt) != -1
4301 : 62828 : || STMT_VINFO_REDUC_DEF (stmt))
4302 : : {
4303 : 31765 : gcc_assert (tail == NULL);
4304 : 31765 : tail = stmt;
4305 : 31765 : continue;
4306 : : }
4307 : 32028 : scalar_stmts.safe_push (stmt);
4308 : : }
4309 : 31605 : gcc_assert (tail);
4310 : :
4311 : : /* When this linearization didn't produce a chain see if stripping
4312 : : a wrapping sign conversion produces one. */
4313 : 31605 : if (scalar_stmts.length () == 1
4314 : 31605 : && (code == PLUS_EXPR || code == MULT_EXPR || code == BIT_IOR_EXPR
4315 : : || code == BIT_AND_EXPR || code == BIT_XOR_EXPR))
4316 : : {
4317 : 30285 : gimple *stmt = scalar_stmts[0]->stmt;
4318 : 30285 : if (!is_gimple_assign (stmt)
4319 : 29409 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
4320 : 3922 : || TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME
4321 : 34207 : || !tree_nop_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
4322 : 3922 : TREE_TYPE (gimple_assign_rhs1 (stmt))))
4323 : : {
4324 : 28426 : scalar_stmts.release ();
4325 : 28426 : return false;
4326 : : }
4327 : 1859 : stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
4328 : 1859 : if (!is_gimple_assign (stmt)
4329 : 1859 : || gimple_assign_rhs_code (stmt) != (tree_code)code)
4330 : : {
4331 : 1846 : scalar_stmts.release ();
4332 : 1846 : return false;
4333 : : }
4334 : 13 : chain.truncate (0);
4335 : 13 : vect_slp_linearize_chain (vinfo, worklist, chain, (tree_code)code,
4336 : : stmt, op_stmt, other_op_stmt, NULL);
4337 : :
4338 : 13 : scalar_stmts.truncate (0);
4339 : 13 : tail = NULL;
4340 : 67 : for (auto el : chain)
4341 : : {
4342 : 32 : if (el.dt == vect_external_def
4343 : 32 : || el.dt == vect_constant_def
4344 : 32 : || el.code != (tree_code) code)
4345 : : {
4346 : 4 : scalar_stmts.release ();
4347 : 4 : return false;
4348 : : }
4349 : 28 : stmt_vec_info stmt = vinfo->lookup_def (el.op);
4350 : 28 : if (STMT_VINFO_REDUC_IDX (stmt) != -1
4351 : 28 : || STMT_VINFO_REDUC_DEF (stmt))
4352 : : {
4353 : 0 : gcc_assert (tail == NULL);
4354 : 0 : tail = stmt;
4355 : 0 : continue;
4356 : : }
4357 : 28 : scalar_stmts.safe_push (stmt);
4358 : : }
4359 : : /* Unlike the above this does not include the reduction SSA
4360 : : cycle. */
4361 : 9 : gcc_assert (!tail);
4362 : : }
4363 : :
4364 : 1329 : if (scalar_stmts.length () < 2)
4365 : : {
4366 : 1241 : scalar_stmts.release ();
4367 : 1241 : return false;
4368 : : }
4369 : :
4370 : 88 : if (dump_enabled_p ())
4371 : : {
4372 : 30 : dump_printf_loc (MSG_NOTE, vect_location,
4373 : : "Starting SLP discovery of reduction chain for\n");
4374 : 128 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4375 : 196 : dump_printf_loc (MSG_NOTE, vect_location,
4376 : 98 : " %G", scalar_stmts[i]->stmt);
4377 : : }
4378 : :
4379 : 88 : unsigned int group_size = scalar_stmts.length ();
4380 : 88 : bool *matches = XALLOCAVEC (bool, group_size);
4381 : 88 : poly_uint64 max_nunits = 1;
4382 : 88 : unsigned tree_size = 0;
4383 : 88 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4384 : : &max_nunits, matches, limit,
4385 : 88 : &tree_size, bst_map);
4386 : 88 : if (!node)
4387 : : {
4388 : 36 : scalar_stmts.release ();
4389 : 36 : return false;
4390 : : }
4391 : :
4392 : 52 : unsigned cycle_id = vinfo->reduc_infos.length ();
4393 : 52 : vect_reduc_info reduc_info = new vect_reduc_info_s ();
4394 : 52 : vinfo->reduc_infos.safe_push (reduc_info);
4395 : 52 : VECT_REDUC_INFO_DEF_TYPE (reduc_info) = STMT_VINFO_DEF_TYPE (next_stmt);
4396 : 52 : VECT_REDUC_INFO_TYPE (reduc_info) = STMT_VINFO_REDUC_TYPE (next_stmt);
4397 : 52 : VECT_REDUC_INFO_CODE (reduc_info) = STMT_VINFO_REDUC_CODE (next_stmt);
4398 : 52 : VECT_REDUC_INFO_FN (reduc_info) = IFN_LAST;
4399 : 52 : reduc_info->is_reduc_chain = true;
4400 : :
4401 : : /* Build the node for the PHI and possibly the conversions. */
4402 : 52 : slp_tree phis = vect_create_new_slp_node (2, ERROR_MARK);
4403 : 52 : SLP_TREE_REPRESENTATIVE (phis) = next_stmt;
4404 : 52 : phis->cycle_info.id = cycle_id;
4405 : 52 : SLP_TREE_LANES (phis) = group_size;
4406 : 52 : if (reduc_scalar_stmt == scalar_stmt)
4407 : 48 : SLP_TREE_VECTYPE (phis) = SLP_TREE_VECTYPE (node);
4408 : : else
4409 : 4 : SLP_TREE_VECTYPE (phis)
4410 : 4 : = signed_or_unsigned_type_for (TYPE_UNSIGNED
4411 : : (TREE_TYPE (gimple_get_lhs
4412 : : (scalar_stmt->stmt))),
4413 : : SLP_TREE_VECTYPE (node));
4414 : : /* ??? vect_cse_slp_nodes cannot cope with cycles without any
4415 : : SLP_TREE_SCALAR_STMTS. */
4416 : 52 : SLP_TREE_SCALAR_STMTS (phis).create (group_size);
4417 : 223 : for (unsigned i = 0; i < group_size; ++i)
4418 : 171 : SLP_TREE_SCALAR_STMTS (phis).quick_push (next_stmt);
4419 : :
4420 : 52 : slp_tree op_input = phis;
4421 : 52 : if (reduc_scalar_stmt != scalar_stmt)
4422 : : {
4423 : 4 : slp_tree conv = vect_create_new_slp_node (1, ERROR_MARK);
4424 : 4 : SLP_TREE_REPRESENTATIVE (conv)
4425 : 4 : = vinfo->lookup_def (gimple_arg (reduc_scalar_stmt->stmt,
4426 : 4 : STMT_VINFO_REDUC_IDX
4427 : : (reduc_scalar_stmt)));
4428 : 4 : SLP_TREE_CHILDREN (conv).quick_push (phis);
4429 : 4 : conv->cycle_info.id = cycle_id;
4430 : 4 : SLP_TREE_REDUC_IDX (conv) = 0;
4431 : 4 : SLP_TREE_LANES (conv) = group_size;
4432 : 4 : SLP_TREE_VECTYPE (conv) = SLP_TREE_VECTYPE (node);
4433 : 4 : SLP_TREE_SCALAR_STMTS (conv) = vNULL;
4434 : 4 : op_input = conv;
4435 : : }
4436 : :
4437 : 52 : slp_tree reduc = vect_create_new_slp_node (2, ERROR_MARK);
4438 : 52 : SLP_TREE_REPRESENTATIVE (reduc) = reduc_scalar_stmt;
4439 : 52 : SLP_TREE_CHILDREN (reduc).quick_push (op_input);
4440 : 52 : SLP_TREE_CHILDREN (reduc).quick_push (node);
4441 : 52 : reduc->cycle_info.id = cycle_id;
4442 : 52 : SLP_TREE_REDUC_IDX (reduc) = 0;
4443 : 52 : SLP_TREE_LANES (reduc) = group_size;
4444 : 52 : SLP_TREE_VECTYPE (reduc) = SLP_TREE_VECTYPE (node);
4445 : : /* ??? For the reduction epilogue we need a live lane. */
4446 : 52 : SLP_TREE_SCALAR_STMTS (reduc).create (group_size);
4447 : 52 : SLP_TREE_SCALAR_STMTS (reduc).quick_push (reduc_scalar_stmt);
4448 : 171 : for (unsigned i = 1; i < group_size; ++i)
4449 : 119 : SLP_TREE_SCALAR_STMTS (reduc).quick_push (NULL);
4450 : :
4451 : 52 : if (reduc_scalar_stmt != scalar_stmt)
4452 : : {
4453 : 4 : slp_tree conv = vect_create_new_slp_node (1, ERROR_MARK);
4454 : 4 : SLP_TREE_REPRESENTATIVE (conv) = scalar_stmt;
4455 : 4 : SLP_TREE_CHILDREN (conv).quick_push (reduc);
4456 : 4 : conv->cycle_info.id = cycle_id;
4457 : 4 : SLP_TREE_REDUC_IDX (conv) = 0;
4458 : 4 : SLP_TREE_LANES (conv) = group_size;
4459 : 4 : SLP_TREE_VECTYPE (conv) = SLP_TREE_VECTYPE (phis);
4460 : : /* ??? For the reduction epilogue we need a live lane. */
4461 : 4 : SLP_TREE_SCALAR_STMTS (conv).create (group_size);
4462 : 4 : SLP_TREE_SCALAR_STMTS (conv).quick_push (scalar_stmt);
4463 : 8 : for (unsigned i = 1; i < group_size; ++i)
4464 : 4 : SLP_TREE_SCALAR_STMTS (conv).quick_push (NULL);
4465 : 4 : reduc = conv;
4466 : : }
4467 : :
4468 : 52 : edge le = loop_latch_edge (LOOP_VINFO_LOOP (vinfo));
4469 : 52 : SLP_TREE_CHILDREN (phis).quick_push (NULL);
4470 : 52 : SLP_TREE_CHILDREN (phis).quick_push (NULL);
4471 : 52 : SLP_TREE_CHILDREN (phis)[le->dest_idx] = reduc;
4472 : 52 : SLP_TREE_REF_COUNT (reduc)++;
4473 : :
4474 : : /* Create a new SLP instance. */
4475 : 52 : slp_instance new_instance = XNEW (class _slp_instance);
4476 : 52 : SLP_INSTANCE_TREE (new_instance) = reduc;
4477 : 52 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4478 : 52 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4479 : 52 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4480 : 52 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_chain;
4481 : 52 : new_instance->reduc_phis = NULL;
4482 : 52 : new_instance->cost_vec = vNULL;
4483 : 52 : new_instance->subgraph_entries = vNULL;
4484 : :
4485 : 52 : vinfo->slp_instances.safe_push (new_instance);
4486 : :
4487 : 52 : if (dump_enabled_p ())
4488 : : {
4489 : 20 : dump_printf_loc (MSG_NOTE, vect_location,
4490 : : "Final SLP tree for instance %p:\n",
4491 : : (void *) new_instance);
4492 : 20 : vect_print_slp_graph (MSG_NOTE, vect_location,
4493 : : SLP_INSTANCE_TREE (new_instance));
4494 : : }
4495 : :
4496 : 52 : return true;
4497 : 32269 : }
4498 : :
4499 : 7711 : if (scalar_stmts.length () <= 1)
4500 : : {
4501 : 6066 : scalar_stmts.release ();
4502 : 6066 : return false;
4503 : : }
4504 : :
4505 : 1645 : scalar_stmts.reverse ();
4506 : 1645 : stmt_vec_info reduc_phi_info = next_stmt;
4507 : :
4508 : : /* Build the tree for the SLP instance. */
4509 : 1645 : vec<stmt_vec_info> root_stmt_infos = vNULL;
4510 : 1645 : vec<tree> remain = vNULL;
4511 : :
4512 : 1645 : if (dump_enabled_p ())
4513 : : {
4514 : 173 : dump_printf_loc (MSG_NOTE, vect_location,
4515 : : "Starting SLP discovery of reduction chain for\n");
4516 : 939 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4517 : 1532 : dump_printf_loc (MSG_NOTE, vect_location,
4518 : 766 : " %G", scalar_stmts[i]->stmt);
4519 : : }
4520 : :
4521 : : /* Build the tree for the SLP instance. */
4522 : 1645 : unsigned int group_size = scalar_stmts.length ();
4523 : 1645 : bool *matches = XALLOCAVEC (bool, group_size);
4524 : 1645 : poly_uint64 max_nunits = 1;
4525 : 1645 : unsigned tree_size = 0;
4526 : :
4527 : : /* ??? We need this only for SLP discovery. */
4528 : 6280 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4529 : 4635 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = scalar_stmts[0];
4530 : :
4531 : 1645 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4532 : : &max_nunits, matches, limit,
4533 : 1645 : &tree_size, bst_map);
4534 : :
4535 : 6280 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4536 : 4635 : REDUC_GROUP_FIRST_ELEMENT (scalar_stmts[i]) = NULL;
4537 : :
4538 : 1645 : if (node != NULL)
4539 : : {
4540 : : /* Create a new SLP instance. */
4541 : 1388 : slp_instance new_instance = XNEW (class _slp_instance);
4542 : 1388 : SLP_INSTANCE_TREE (new_instance) = node;
4543 : 1388 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4544 : 1388 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4545 : 1388 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4546 : 1388 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_chain;
4547 : 1388 : new_instance->reduc_phis = NULL;
4548 : 1388 : new_instance->cost_vec = vNULL;
4549 : 1388 : new_instance->subgraph_entries = vNULL;
4550 : :
4551 : 1388 : vect_reduc_info reduc_info = info_for_reduction (vinfo, node);
4552 : 1388 : reduc_info->is_reduc_chain = true;
4553 : :
4554 : 1388 : if (dump_enabled_p ())
4555 : 128 : dump_printf_loc (MSG_NOTE, vect_location,
4556 : : "SLP size %u vs. limit %u.\n",
4557 : : tree_size, max_tree_size);
4558 : :
4559 : : /* Fixup SLP reduction chains. If this is a reduction chain with
4560 : : a conversion in front amend the SLP tree with a node for that. */
4561 : 1388 : gimple *scalar_def = STMT_VINFO_REDUC_DEF (reduc_phi_info)->stmt;
4562 : 1388 : if (is_gimple_assign (scalar_def)
4563 : 1388 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (scalar_def)))
4564 : : {
4565 : 28 : stmt_vec_info conv_info = vect_stmt_to_vectorize
4566 : 28 : (STMT_VINFO_REDUC_DEF (reduc_phi_info));
4567 : 28 : scalar_stmts = vNULL;
4568 : 28 : scalar_stmts.create (group_size);
4569 : 90 : for (unsigned i = 0; i < group_size; ++i)
4570 : 62 : scalar_stmts.quick_push (conv_info);
4571 : 28 : slp_tree conv = vect_create_new_slp_node (scalar_stmts, 1);
4572 : 28 : SLP_TREE_VECTYPE (conv)
4573 : 28 : = get_vectype_for_scalar_type (vinfo,
4574 : 28 : TREE_TYPE
4575 : : (gimple_assign_lhs (scalar_def)),
4576 : : group_size);
4577 : 28 : SLP_TREE_REDUC_IDX (conv) = 0;
4578 : 28 : conv->cycle_info.id = node->cycle_info.id;
4579 : 28 : SLP_TREE_CHILDREN (conv).quick_push (node);
4580 : 28 : SLP_INSTANCE_TREE (new_instance) = conv;
4581 : : }
4582 : : /* Fill the backedge child of the PHI SLP node. The
4583 : : general matching code cannot find it because the
4584 : : scalar code does not reflect how we vectorize the
4585 : : reduction. */
4586 : 1388 : use_operand_p use_p;
4587 : 1388 : imm_use_iterator imm_iter;
4588 : 1388 : class loop *loop = LOOP_VINFO_LOOP (vinfo);
4589 : 6642 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
4590 : : gimple_get_lhs (scalar_def))
4591 : : /* There are exactly two non-debug uses, the reduction
4592 : : PHI and the loop-closed PHI node. */
4593 : 3866 : if (!is_gimple_debug (USE_STMT (use_p))
4594 : 3866 : && gimple_bb (USE_STMT (use_p)) == loop->header)
4595 : : {
4596 : 1388 : auto_vec<stmt_vec_info, 64> phis (group_size);
4597 : 1388 : stmt_vec_info phi_info = vinfo->lookup_stmt (USE_STMT (use_p));
4598 : 5359 : for (unsigned i = 0; i < group_size; ++i)
4599 : 3971 : phis.quick_push (phi_info);
4600 : 1388 : slp_tree *phi_node = bst_map->get (phis);
4601 : 1388 : unsigned dest_idx = loop_latch_edge (loop)->dest_idx;
4602 : 2776 : SLP_TREE_CHILDREN (*phi_node)[dest_idx]
4603 : 1388 : = SLP_INSTANCE_TREE (new_instance);
4604 : 1388 : SLP_INSTANCE_TREE (new_instance)->refcnt++;
4605 : 1388 : }
4606 : :
4607 : 1388 : vinfo->slp_instances.safe_push (new_instance);
4608 : :
4609 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4610 : : the number of scalar stmts in the root in a few places.
4611 : : Verify that assumption holds. */
4612 : 2776 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4613 : : .length () == group_size);
4614 : :
4615 : 1388 : if (dump_enabled_p ())
4616 : : {
4617 : 128 : dump_printf_loc (MSG_NOTE, vect_location,
4618 : : "Final SLP tree for instance %p:\n",
4619 : : (void *) new_instance);
4620 : 128 : vect_print_slp_graph (MSG_NOTE, vect_location,
4621 : : SLP_INSTANCE_TREE (new_instance));
4622 : : }
4623 : :
4624 : 1388 : return true;
4625 : : }
4626 : :
4627 : : /* Failed to SLP. */
4628 : 257 : scalar_stmts.release ();
4629 : 257 : if (dump_enabled_p ())
4630 : 45 : dump_printf_loc (MSG_NOTE, vect_location,
4631 : : "SLP discovery of reduction chain failed\n");
4632 : : return false;
4633 : : }
4634 : :
4635 : : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
4636 : : of KIND. Return true if successful. */
4637 : :
4638 : : static bool
4639 : 61893 : vect_analyze_slp_reduction (loop_vec_info vinfo,
4640 : : stmt_vec_info scalar_stmt,
4641 : : unsigned max_tree_size, unsigned *limit,
4642 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4643 : : bool force_single_lane)
4644 : : {
4645 : 61893 : slp_instance_kind kind = slp_inst_kind_reduc_group;
4646 : :
4647 : : /* If there's no budget left bail out early. */
4648 : 61893 : if (*limit == 0)
4649 : : return false;
4650 : :
4651 : : /* Try to gather a reduction chain. */
4652 : 61893 : if (! force_single_lane
4653 : 41903 : && STMT_VINFO_DEF_TYPE (scalar_stmt) == vect_reduction_def
4654 : 103589 : && vect_analyze_slp_reduc_chain (vinfo, bst_map, scalar_stmt,
4655 : : max_tree_size, limit))
4656 : : return true;
4657 : :
4658 : 60453 : vec<stmt_vec_info> scalar_stmts;
4659 : 60453 : scalar_stmts.create (1);
4660 : 60453 : scalar_stmts.quick_push (scalar_stmt);
4661 : :
4662 : 60453 : if (dump_enabled_p ())
4663 : : {
4664 : 3156 : dump_printf_loc (MSG_NOTE, vect_location,
4665 : : "Starting SLP discovery for\n");
4666 : 6312 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4667 : 6312 : dump_printf_loc (MSG_NOTE, vect_location,
4668 : 3156 : " %G", scalar_stmts[i]->stmt);
4669 : : }
4670 : :
4671 : : /* Build the tree for the SLP instance. */
4672 : 60453 : unsigned int group_size = scalar_stmts.length ();
4673 : 60453 : bool *matches = XALLOCAVEC (bool, group_size);
4674 : 60453 : poly_uint64 max_nunits = 1;
4675 : 60453 : unsigned tree_size = 0;
4676 : :
4677 : 60453 : slp_tree node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4678 : : &max_nunits, matches, limit,
4679 : : &tree_size, bst_map);
4680 : 60453 : if (node != NULL)
4681 : : {
4682 : : /* Create a new SLP instance. */
4683 : 58249 : slp_instance new_instance = XNEW (class _slp_instance);
4684 : 58249 : SLP_INSTANCE_TREE (new_instance) = node;
4685 : 58249 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4686 : 58249 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4687 : 58249 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4688 : 58249 : SLP_INSTANCE_KIND (new_instance) = kind;
4689 : 58249 : new_instance->reduc_phis = NULL;
4690 : 58249 : new_instance->cost_vec = vNULL;
4691 : 58249 : new_instance->subgraph_entries = vNULL;
4692 : :
4693 : 58249 : if (dump_enabled_p ())
4694 : 3052 : dump_printf_loc (MSG_NOTE, vect_location,
4695 : : "SLP size %u vs. limit %u.\n",
4696 : : tree_size, max_tree_size);
4697 : :
4698 : 58249 : vinfo->slp_instances.safe_push (new_instance);
4699 : :
4700 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4701 : : the number of scalar stmts in the root in a few places.
4702 : : Verify that assumption holds. */
4703 : 116498 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4704 : : .length () == group_size);
4705 : :
4706 : 58249 : if (dump_enabled_p ())
4707 : : {
4708 : 3052 : dump_printf_loc (MSG_NOTE, vect_location,
4709 : : "Final SLP tree for instance %p:\n",
4710 : : (void *) new_instance);
4711 : 3052 : vect_print_slp_graph (MSG_NOTE, vect_location,
4712 : : SLP_INSTANCE_TREE (new_instance));
4713 : : }
4714 : :
4715 : 58249 : return true;
4716 : : }
4717 : : /* Failed to SLP. */
4718 : :
4719 : : /* Free the allocated memory. */
4720 : 2204 : scalar_stmts.release ();
4721 : :
4722 : : /* Failed to SLP. */
4723 : 2204 : if (dump_enabled_p ())
4724 : 104 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4725 : : return false;
4726 : : }
4727 : :
4728 : : /* Analyze a single SLP reduction group. If successful add a SLP instance
4729 : : for it and return true, otherwise return false and have *MATCHES
4730 : : populated. */
4731 : :
4732 : : static bool
4733 : 17970 : vect_analyze_slp_reduction_group (loop_vec_info loop_vinfo,
4734 : : vec<stmt_vec_info> scalar_stmts,
4735 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4736 : : unsigned max_tree_size, unsigned *limit,
4737 : : bool *matches)
4738 : : {
4739 : : /* Try to form a reduction group. */
4740 : 17970 : unsigned int group_size = scalar_stmts.length ();
4741 : 17970 : if (!matches)
4742 : 7370 : matches = XALLOCAVEC (bool, group_size);
4743 : 17970 : poly_uint64 max_nunits = 1;
4744 : 17970 : unsigned tree_size = 0;
4745 : 17970 : slp_tree node = vect_build_slp_tree (loop_vinfo, scalar_stmts,
4746 : : group_size,
4747 : : &max_nunits, matches, limit,
4748 : : &tree_size, bst_map);
4749 : 17970 : if (!node)
4750 : : return false;
4751 : :
4752 : : /* Create a new SLP instance. */
4753 : 8502 : slp_instance new_instance = XNEW (class _slp_instance);
4754 : 8502 : SLP_INSTANCE_TREE (new_instance) = node;
4755 : 8502 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4756 : 8502 : SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
4757 : 8502 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
4758 : 8502 : SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_group;
4759 : 8502 : new_instance->reduc_phis = NULL;
4760 : 8502 : new_instance->cost_vec = vNULL;
4761 : 8502 : new_instance->subgraph_entries = vNULL;
4762 : :
4763 : 8502 : if (dump_enabled_p ())
4764 : 526 : dump_printf_loc (MSG_NOTE, vect_location,
4765 : : "SLP size %u vs. limit %u.\n",
4766 : : tree_size, max_tree_size);
4767 : :
4768 : 8502 : loop_vinfo->slp_instances.safe_push (new_instance);
4769 : :
4770 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4771 : : the number of scalar stmts in the root in a few places.
4772 : : Verify that assumption holds. */
4773 : 17004 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4774 : : .length () == group_size);
4775 : :
4776 : 8502 : if (dump_enabled_p ())
4777 : : {
4778 : 526 : dump_printf_loc (MSG_NOTE, vect_location,
4779 : : "SLP discovery of size %d reduction group "
4780 : : "succeeded\n", group_size);
4781 : 526 : dump_printf_loc (MSG_NOTE, vect_location,
4782 : : "Final SLP tree for instance %p:\n",
4783 : : (void *) new_instance);
4784 : 526 : vect_print_slp_graph (MSG_NOTE, vect_location,
4785 : : SLP_INSTANCE_TREE (new_instance));
4786 : : }
4787 : :
4788 : : return true;
4789 : : }
4790 : :
4791 : : /* Analyze reductions in LOOP_VINFO and populate SLP instances
4792 : : accordingly. Returns false if something fails. */
4793 : :
4794 : : static bool
4795 : 406601 : vect_analyze_slp_reductions (loop_vec_info loop_vinfo,
4796 : : unsigned max_tree_size, unsigned *limit,
4797 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4798 : : bool force_single_lane)
4799 : : {
4800 : 452681 : if (loop_vinfo->reductions.is_empty ())
4801 : : return true;
4802 : :
4803 : : /* Collect reduction statements we can combine into
4804 : : a SLP reduction. */
4805 : 51430 : vec<stmt_vec_info> scalar_stmts;
4806 : 51430 : scalar_stmts.create (loop_vinfo->reductions.length ());
4807 : 227294 : for (auto next_info : loop_vinfo->reductions)
4808 : : {
4809 : 73004 : next_info = vect_stmt_to_vectorize (next_info);
4810 : 73004 : if ((STMT_VINFO_RELEVANT_P (next_info)
4811 : 4 : || STMT_VINFO_LIVE_P (next_info))
4812 : : /* ??? Make sure we didn't skip a conversion around a
4813 : : reduction path. In that case we'd have to reverse
4814 : : engineer that conversion stmt following the chain using
4815 : : reduc_idx and from the PHI using reduc_def. */
4816 : 73000 : && (STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def
4817 : 73000 : || (STMT_VINFO_DEF_TYPE (next_info)
4818 : : == vect_double_reduction_def)))
4819 : : {
4820 : : /* Do not discover SLP reductions combining lane-reducing
4821 : : ops, that will fail later. */
4822 : 73000 : if (!force_single_lane
4823 : 73000 : && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info)))
4824 : 52575 : scalar_stmts.quick_push (next_info);
4825 : : /* Do SLP discovery for single-lane reductions. */
4826 : 20425 : else if (! vect_analyze_slp_reduction (loop_vinfo, next_info,
4827 : : max_tree_size, limit,
4828 : : bst_map,
4829 : : force_single_lane))
4830 : : {
4831 : 0 : scalar_stmts.release ();
4832 : 0 : return false;
4833 : : }
4834 : : }
4835 : : }
4836 : :
4837 : 51430 : if (scalar_stmts.length () > 1)
4838 : : {
4839 : : /* Try to form a reduction group. */
4840 : 3250 : unsigned int group_size = scalar_stmts.length ();
4841 : 3250 : bool *matches = XALLOCAVEC (bool, group_size);
4842 : 3250 : if (vect_analyze_slp_reduction_group (loop_vinfo, scalar_stmts, bst_map,
4843 : : max_tree_size, limit, matches))
4844 : 3166 : return true;
4845 : :
4846 : : /* When analysis as a single SLP reduction group failed try to
4847 : : form sub-groups by collecting matching lanes. Do not recurse
4848 : : that on failure (to limit compile-time costs), but recurse
4849 : : for the initial non-matching parts. Everything not covered
4850 : : by a sub-group gets single-reduction treatment. */
4851 : 2353 : vec<stmt_vec_info> cands = vNULL;
4852 : 7454 : while (matches[0])
4853 : : {
4854 : 7370 : cands.truncate (0);
4855 : 7370 : cands.reserve (group_size, true);
4856 : 57924 : for (unsigned i = 0; i < group_size; ++i)
4857 : 50554 : if (matches[i])
4858 : 12341 : cands.quick_push (scalar_stmts[i]);
4859 : :
4860 : : /* Try to form a reduction group. */
4861 : 7370 : if (vect_analyze_slp_reduction_group (loop_vinfo, cands, bst_map,
4862 : : max_tree_size, limit, NULL))
4863 : 5356 : cands = vNULL;
4864 : : else
4865 : : {
4866 : : /* Do SLP discovery for single-lane reductions. */
4867 : 12241 : for (auto stmt_info : cands)
4868 : 6219 : if (! vect_analyze_slp_reduction (loop_vinfo,
4869 : : vect_stmt_to_vectorize
4870 : : (stmt_info),
4871 : : max_tree_size, limit,
4872 : : bst_map, force_single_lane))
4873 : : {
4874 : 20 : scalar_stmts.release ();
4875 : 20 : cands.release ();
4876 : 20 : return false;
4877 : : }
4878 : : }
4879 : : /* Remove the handled stmts from scalar_stmts and try again,
4880 : : possibly repeating the above with updated matches[]. */
4881 : : unsigned j = 0;
4882 : 57846 : for (unsigned i = 0; i < group_size; ++i)
4883 : 50496 : if (!matches[i])
4884 : : {
4885 : 38188 : scalar_stmts[j] = scalar_stmts[i];
4886 : 38188 : ++j;
4887 : : }
4888 : 7350 : scalar_stmts.truncate (j);
4889 : 7350 : group_size = scalar_stmts.length ();
4890 : 7350 : if (vect_analyze_slp_reduction_group (loop_vinfo, scalar_stmts,
4891 : : bst_map, max_tree_size, limit,
4892 : : matches))
4893 : : return true;
4894 : : }
4895 : : }
4896 : : /* Do SLP discovery for single-lane reductions. */
4897 : 177857 : for (auto stmt_info : scalar_stmts)
4898 : 35249 : if (! vect_analyze_slp_reduction (loop_vinfo,
4899 : : vect_stmt_to_vectorize (stmt_info),
4900 : : max_tree_size, limit,
4901 : : bst_map, force_single_lane))
4902 : : {
4903 : 2184 : scalar_stmts.release ();
4904 : 2184 : return false;
4905 : : }
4906 : :
4907 : 46080 : scalar_stmts.release ();
4908 : 46080 : return true;
4909 : : }
4910 : :
4911 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
4912 : : vect_build_slp_tree to build a tree of packed stmts if possible.
4913 : : Return FALSE if it's impossible to SLP any stmt in the group. */
4914 : :
4915 : : static bool
4916 : 1093668 : vect_analyze_slp_instance (vec_info *vinfo,
4917 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4918 : : stmt_vec_info stmt_info,
4919 : : slp_instance_kind kind,
4920 : : unsigned max_tree_size, unsigned *limit,
4921 : : bool force_single_lane)
4922 : : {
4923 : 1093668 : vec<stmt_vec_info> scalar_stmts;
4924 : :
4925 : 1093668 : if (is_a <bb_vec_info> (vinfo))
4926 : 1070939 : vect_location = stmt_info->stmt;
4927 : :
4928 : 1093668 : gcc_assert (kind == slp_inst_kind_store);
4929 : :
4930 : : /* Collect the stores and store them in scalar_stmts. */
4931 : 1093668 : scalar_stmts.create (DR_GROUP_SIZE (stmt_info));
4932 : 1093668 : stmt_vec_info next_info = stmt_info;
4933 : 5433385 : while (next_info)
4934 : : {
4935 : 3246049 : scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
4936 : 3246049 : next_info = DR_GROUP_NEXT_ELEMENT (next_info);
4937 : : }
4938 : :
4939 : 1093668 : vec<stmt_vec_info> root_stmt_infos = vNULL;
4940 : 1093668 : vec<tree> remain = vNULL;
4941 : :
4942 : : /* Build the tree for the SLP instance. */
4943 : :
4944 : : /* If there's no budget left bail out early. */
4945 : 1093668 : if (*limit == 0)
4946 : : return false;
4947 : :
4948 : 1093645 : if (dump_enabled_p ())
4949 : : {
4950 : 3983 : dump_printf_loc (MSG_NOTE, vect_location,
4951 : : "Starting SLP discovery for\n");
4952 : 22769 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
4953 : 37572 : dump_printf_loc (MSG_NOTE, vect_location,
4954 : 18786 : " %G", scalar_stmts[i]->stmt);
4955 : : }
4956 : :
4957 : : /* Build the tree for the SLP instance. */
4958 : 1093645 : unsigned int group_size = scalar_stmts.length ();
4959 : 1093645 : bool *matches = XALLOCAVEC (bool, group_size);
4960 : 1093645 : poly_uint64 max_nunits = 1;
4961 : 1093645 : unsigned tree_size = 0;
4962 : 1093645 : unsigned i;
4963 : :
4964 : 1093645 : slp_tree node = NULL;
4965 : 1093645 : if (group_size > 1 && force_single_lane)
4966 : : {
4967 : 1456 : matches[0] = true;
4968 : 1456 : matches[1] = false;
4969 : : }
4970 : : else
4971 : 1092189 : node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4972 : : &max_nunits, matches, limit,
4973 : : &tree_size, bst_map);
4974 : 1093645 : if (node != NULL)
4975 : : {
4976 : : /* Calculate the unrolling factor based on the smallest type. */
4977 : 680214 : poly_uint64 unrolling_factor
4978 : 680214 : = calculate_unrolling_factor (max_nunits, group_size);
4979 : :
4980 : 680214 : if (maybe_ne (unrolling_factor, 1U)
4981 : 680214 : && is_a <bb_vec_info> (vinfo))
4982 : : {
4983 : 0 : unsigned HOST_WIDE_INT const_max_nunits;
4984 : 0 : if (!max_nunits.is_constant (&const_max_nunits)
4985 : 0 : || const_max_nunits > group_size)
4986 : : {
4987 : 0 : if (dump_enabled_p ())
4988 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4989 : : "Build SLP failed: store group "
4990 : : "size not a multiple of the vector size "
4991 : : "in basic block SLP\n");
4992 : 0 : vect_free_slp_tree (node);
4993 : 0 : return false;
4994 : : }
4995 : : /* Fatal mismatch. */
4996 : 0 : if (dump_enabled_p ())
4997 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
4998 : : "SLP discovery succeeded but node needs "
4999 : : "splitting\n");
5000 : 0 : memset (matches, true, group_size);
5001 : 0 : matches[group_size / const_max_nunits * const_max_nunits] = false;
5002 : 0 : vect_free_slp_tree (node);
5003 : : }
5004 : : else
5005 : : {
5006 : : /* Create a new SLP instance. */
5007 : 680214 : slp_instance new_instance = XNEW (class _slp_instance);
5008 : 680214 : SLP_INSTANCE_TREE (new_instance) = node;
5009 : 680214 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
5010 : 680214 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
5011 : 680214 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
5012 : 680214 : SLP_INSTANCE_KIND (new_instance) = kind;
5013 : 680214 : new_instance->reduc_phis = NULL;
5014 : 680214 : new_instance->cost_vec = vNULL;
5015 : 680214 : new_instance->subgraph_entries = vNULL;
5016 : :
5017 : 680214 : if (dump_enabled_p ())
5018 : 3010 : dump_printf_loc (MSG_NOTE, vect_location,
5019 : : "SLP size %u vs. limit %u.\n",
5020 : : tree_size, max_tree_size);
5021 : :
5022 : 680214 : vinfo->slp_instances.safe_push (new_instance);
5023 : :
5024 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
5025 : : the number of scalar stmts in the root in a few places.
5026 : : Verify that assumption holds. */
5027 : 1360428 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
5028 : : .length () == group_size);
5029 : :
5030 : 680214 : if (dump_enabled_p ())
5031 : : {
5032 : 3010 : dump_printf_loc (MSG_NOTE, vect_location,
5033 : : "Final SLP tree for instance %p:\n",
5034 : : (void *) new_instance);
5035 : 3010 : vect_print_slp_graph (MSG_NOTE, vect_location,
5036 : : SLP_INSTANCE_TREE (new_instance));
5037 : : }
5038 : :
5039 : 680214 : return true;
5040 : : }
5041 : : }
5042 : : /* Failed to SLP. */
5043 : :
5044 : : /* Try to break the group up into pieces. */
5045 : 413431 : if (*limit > 0 && kind == slp_inst_kind_store)
5046 : : {
5047 : : /* ??? We could delay all the actual splitting of store-groups
5048 : : until after SLP discovery of the original group completed.
5049 : : Then we can recurse to vect_build_slp_instance directly. */
5050 : 1077408 : for (i = 0; i < group_size; i++)
5051 : 1077408 : if (!matches[i])
5052 : : break;
5053 : :
5054 : : /* For basic block SLP, try to break the group up into multiples of
5055 : : a vector size. */
5056 : 413430 : if (is_a <bb_vec_info> (vinfo)
5057 : 413430 : && (i > 1 && i < group_size))
5058 : : {
5059 : : /* Free the allocated memory. */
5060 : 155539 : scalar_stmts.release ();
5061 : :
5062 : 155539 : tree scalar_type
5063 : 155539 : = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
5064 : 311078 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
5065 : 155539 : 1 << floor_log2 (i));
5066 : 155539 : unsigned HOST_WIDE_INT const_nunits;
5067 : 155539 : if (vectype
5068 : 155539 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits))
5069 : : {
5070 : : /* Split into two groups at the first vector boundary. */
5071 : 155539 : gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
5072 : 155539 : unsigned group1_size = i & ~(const_nunits - 1);
5073 : :
5074 : 155539 : if (dump_enabled_p ())
5075 : 59 : dump_printf_loc (MSG_NOTE, vect_location,
5076 : : "Splitting SLP group at stmt %u\n", i);
5077 : 155539 : stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
5078 : : group1_size);
5079 : 155539 : bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info,
5080 : : kind, max_tree_size,
5081 : : limit, false);
5082 : : /* Split the rest at the failure point and possibly
5083 : : re-analyze the remaining matching part if it has
5084 : : at least two lanes. */
5085 : 155539 : if (group1_size < i
5086 : 5408 : && (i + 1 < group_size
5087 : 3122 : || i - group1_size > 1))
5088 : : {
5089 : 2316 : stmt_vec_info rest2 = rest;
5090 : 2316 : rest = vect_split_slp_store_group (rest, i - group1_size);
5091 : 2316 : if (i - group1_size > 1)
5092 : 59 : res |= vect_analyze_slp_instance (vinfo, bst_map, rest2,
5093 : : kind, max_tree_size,
5094 : : limit, false);
5095 : : }
5096 : : /* Re-analyze the non-matching tail if it has at least
5097 : : two lanes. */
5098 : 155539 : if (i + 1 < group_size)
5099 : 22319 : res |= vect_analyze_slp_instance (vinfo, bst_map,
5100 : : rest, kind, max_tree_size,
5101 : : limit, false);
5102 : 155539 : return res;
5103 : : }
5104 : : }
5105 : :
5106 : : /* For loop vectorization split the RHS into arbitrary pieces of
5107 : : size >= 1. */
5108 : 257891 : else if (is_a <loop_vec_info> (vinfo)
5109 : 257891 : && (group_size != 1 && i < group_size))
5110 : : {
5111 : 6417 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
5112 : 27 : bool masked_p = call
5113 : 27 : && gimple_call_internal_p (call)
5114 : 27 : && internal_fn_mask_index (gimple_call_internal_fn (call)) != -1;
5115 : : /* There are targets that cannot do even/odd interleaving schemes
5116 : : so they absolutely need to use load/store-lanes. For now
5117 : : force single-lane SLP for them - they would be happy with
5118 : : uniform power-of-two lanes (but depending on element size),
5119 : : but even if we can use 'i' as indicator we would need to
5120 : : backtrack when later lanes fail to discover with the same
5121 : : granularity. We cannot turn any of strided or scatter store
5122 : : into store-lanes. */
5123 : : /* ??? If this is not in sync with what get_load_store_type
5124 : : later decides the SLP representation is not good for other
5125 : : store vectorization methods. */
5126 : 6417 : bool want_store_lanes
5127 : 6417 : = (! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
5128 : 6417 : && ! STMT_VINFO_STRIDED_P (stmt_info)
5129 : 4923 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
5130 : 4920 : && compare_step_with_zero (vinfo, stmt_info) > 0
5131 : 11314 : && vect_slp_prefer_store_lanes_p (vinfo, stmt_info, NULL_TREE,
5132 : 12834 : masked_p, group_size, i));
5133 : 6417 : if (want_store_lanes || force_single_lane)
5134 : : i = 1;
5135 : :
5136 : : /* A fatal discovery fail doesn't always mean single-lane SLP
5137 : : isn't a possibility, so try. */
5138 : 4961 : if (i == 0)
5139 : : i = 1;
5140 : :
5141 : 6417 : if (dump_enabled_p ())
5142 : 872 : dump_printf_loc (MSG_NOTE, vect_location,
5143 : : "Splitting SLP group at stmt %u\n", i);
5144 : :
5145 : : /* Analyze the stored values and pinch them together with
5146 : : a permute node so we can preserve the whole store group. */
5147 : 6417 : auto_vec<slp_tree> rhs_nodes;
5148 : 6417 : poly_uint64 max_nunits = 1;
5149 : :
5150 : 6417 : unsigned int rhs_common_nlanes = 0;
5151 : 6417 : unsigned int start = 0, end = i;
5152 : 29093 : while (start < group_size)
5153 : : {
5154 : 22925 : gcc_assert (end - start >= 1);
5155 : 22925 : vec<stmt_vec_info> substmts;
5156 : 22925 : substmts.create (end - start);
5157 : 69383 : for (unsigned j = start; j < end; ++j)
5158 : 46458 : substmts.quick_push (scalar_stmts[j]);
5159 : 22925 : max_nunits = 1;
5160 : 22925 : node = vect_build_slp_tree (vinfo, substmts, end - start,
5161 : : &max_nunits,
5162 : : matches, limit, &tree_size, bst_map);
5163 : 22925 : if (node)
5164 : : {
5165 : 18213 : rhs_nodes.safe_push (node);
5166 : 18213 : vect_update_max_nunits (&max_nunits, node->max_nunits);
5167 : 18213 : if (start == 0)
5168 : 6172 : rhs_common_nlanes = SLP_TREE_LANES (node);
5169 : 12041 : else if (rhs_common_nlanes != SLP_TREE_LANES (node))
5170 : 1271 : rhs_common_nlanes = 0;
5171 : 18213 : start = end;
5172 : 18213 : if (want_store_lanes || force_single_lane)
5173 : 4463 : end = start + 1;
5174 : : else
5175 : : end = group_size;
5176 : : }
5177 : : else
5178 : : {
5179 : 4712 : substmts.release ();
5180 : 4712 : if (end - start == 1)
5181 : : {
5182 : : /* Single-lane discovery failed. Free ressources. */
5183 : 263 : for (auto node : rhs_nodes)
5184 : 6 : vect_free_slp_tree (node);
5185 : 249 : scalar_stmts.release ();
5186 : 249 : if (dump_enabled_p ())
5187 : 41 : dump_printf_loc (MSG_NOTE, vect_location,
5188 : : "SLP discovery failed\n");
5189 : 249 : return false;
5190 : : }
5191 : :
5192 : : /* ??? It really happens that we soft-fail SLP
5193 : : build at a mismatch but the matching part hard-fails
5194 : : later. As we know we arrived here with a group
5195 : : larger than one try a group of size one! */
5196 : 4463 : if (!matches[0])
5197 : 42 : end = start + 1;
5198 : : else
5199 : 9934 : for (unsigned j = start; j < end; j++)
5200 : 9934 : if (!matches[j - start])
5201 : : {
5202 : : end = j;
5203 : : break;
5204 : : }
5205 : : }
5206 : : }
5207 : :
5208 : : /* Now re-assess whether we want store lanes in case the
5209 : : discovery ended up producing all single-lane RHSs. */
5210 : 6168 : if (! want_store_lanes
5211 : 6168 : && rhs_common_nlanes == 1
5212 : 5302 : && ! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
5213 : 5302 : && ! STMT_VINFO_STRIDED_P (stmt_info)
5214 : 4074 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
5215 : 4071 : && compare_step_with_zero (vinfo, stmt_info) > 0
5216 : 10228 : && (vect_store_lanes_supported (SLP_TREE_VECTYPE (rhs_nodes[0]),
5217 : : group_size, masked_p)
5218 : : != IFN_LAST))
5219 : : want_store_lanes = true;
5220 : :
5221 : : /* Now we assume we can build the root SLP node from all stores. */
5222 : 6168 : if (want_store_lanes)
5223 : : {
5224 : : /* For store-lanes feed the store node with all RHS nodes
5225 : : in order. */
5226 : 0 : node = vect_create_new_slp_node (scalar_stmts,
5227 : 0 : SLP_TREE_CHILDREN
5228 : : (rhs_nodes[0]).length ());
5229 : 0 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
5230 : 0 : node->max_nunits = max_nunits;
5231 : 0 : node->ldst_lanes = true;
5232 : 0 : SLP_TREE_CHILDREN (node)
5233 : 0 : .reserve_exact (SLP_TREE_CHILDREN (rhs_nodes[0]).length ()
5234 : 0 : + rhs_nodes.length () - 1);
5235 : : /* First store value and possibly mask. */
5236 : 0 : SLP_TREE_CHILDREN (node)
5237 : 0 : .splice (SLP_TREE_CHILDREN (rhs_nodes[0]));
5238 : : /* Rest of the store values. All mask nodes are the same,
5239 : : this should be guaranteed by dataref group discovery. */
5240 : 0 : for (unsigned j = 1; j < rhs_nodes.length (); ++j)
5241 : 0 : SLP_TREE_CHILDREN (node)
5242 : 0 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[0]);
5243 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
5244 : 0 : child->refcnt++;
5245 : : }
5246 : : else
5247 : 6168 : node = vect_build_slp_store_interleaving (rhs_nodes, scalar_stmts,
5248 : : max_nunits);
5249 : :
5250 : 24375 : while (!rhs_nodes.is_empty ())
5251 : 18207 : vect_free_slp_tree (rhs_nodes.pop ());
5252 : :
5253 : : /* Create a new SLP instance. */
5254 : 6168 : slp_instance new_instance = XNEW (class _slp_instance);
5255 : 6168 : SLP_INSTANCE_TREE (new_instance) = node;
5256 : 6168 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
5257 : 6168 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
5258 : 6168 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
5259 : 6168 : SLP_INSTANCE_KIND (new_instance) = kind;
5260 : 6168 : new_instance->reduc_phis = NULL;
5261 : 6168 : new_instance->cost_vec = vNULL;
5262 : 6168 : new_instance->subgraph_entries = vNULL;
5263 : :
5264 : 6168 : if (dump_enabled_p ())
5265 : 831 : dump_printf_loc (MSG_NOTE, vect_location,
5266 : : "SLP size %u vs. limit %u.\n",
5267 : : tree_size, max_tree_size);
5268 : :
5269 : 6168 : vinfo->slp_instances.safe_push (new_instance);
5270 : :
5271 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
5272 : : the number of scalar stmts in the root in a few places.
5273 : : Verify that assumption holds. */
5274 : 12336 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
5275 : : .length () == group_size);
5276 : :
5277 : 6168 : if (dump_enabled_p ())
5278 : : {
5279 : 831 : dump_printf_loc (MSG_NOTE, vect_location,
5280 : : "Final SLP tree for instance %p:\n",
5281 : : (void *) new_instance);
5282 : 831 : vect_print_slp_graph (MSG_NOTE, vect_location,
5283 : : SLP_INSTANCE_TREE (new_instance));
5284 : : }
5285 : 6168 : return true;
5286 : 6417 : }
5287 : : else
5288 : : /* Free the allocated memory. */
5289 : 251474 : scalar_stmts.release ();
5290 : :
5291 : : /* Even though the first vector did not all match, we might be able to SLP
5292 : : (some) of the remainder. FORNOW ignore this possibility. */
5293 : : }
5294 : : else
5295 : : /* Free the allocated memory. */
5296 : 1 : scalar_stmts.release ();
5297 : :
5298 : : /* Failed to SLP. */
5299 : 251475 : if (dump_enabled_p ())
5300 : 42 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
5301 : : return false;
5302 : : }
5303 : :
5304 : : /* qsort comparator ordering SLP load nodes. */
5305 : :
5306 : : static int
5307 : 2265135 : vllp_cmp (const void *a_, const void *b_)
5308 : : {
5309 : 2265135 : const slp_tree a = *(const slp_tree *)a_;
5310 : 2265135 : const slp_tree b = *(const slp_tree *)b_;
5311 : 2265135 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (a)[0];
5312 : 2265135 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (b)[0];
5313 : 2265135 : if (STMT_VINFO_GROUPED_ACCESS (a0)
5314 : 1398309 : && STMT_VINFO_GROUPED_ACCESS (b0)
5315 : 3603326 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
5316 : : {
5317 : : /* Same group, order after lanes used. */
5318 : 338864 : if (SLP_TREE_LANES (a) < SLP_TREE_LANES (b))
5319 : : return 1;
5320 : 332894 : else if (SLP_TREE_LANES (a) > SLP_TREE_LANES (b))
5321 : : return -1;
5322 : : else
5323 : : {
5324 : : /* Try to order loads using the same lanes together, breaking
5325 : : the tie with the lane number that first differs. */
5326 : 326279 : if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
5327 : 326279 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
5328 : : return 0;
5329 : 326279 : else if (SLP_TREE_LOAD_PERMUTATION (a).exists ()
5330 : 326279 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
5331 : : return 1;
5332 : 323683 : else if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
5333 : 323683 : && SLP_TREE_LOAD_PERMUTATION (b).exists ())
5334 : : return -1;
5335 : : else
5336 : : {
5337 : 318901 : for (unsigned i = 0; i < SLP_TREE_LANES (a); ++i)
5338 : 318901 : if (SLP_TREE_LOAD_PERMUTATION (a)[i]
5339 : 318901 : != SLP_TREE_LOAD_PERMUTATION (b)[i])
5340 : : {
5341 : : /* In-order lane first, that's what the above case for
5342 : : no permutation does. */
5343 : 318069 : if (SLP_TREE_LOAD_PERMUTATION (a)[i] == i)
5344 : : return -1;
5345 : 188743 : else if (SLP_TREE_LOAD_PERMUTATION (b)[i] == i)
5346 : : return 1;
5347 : 87901 : else if (SLP_TREE_LOAD_PERMUTATION (a)[i]
5348 : 87901 : < SLP_TREE_LOAD_PERMUTATION (b)[i])
5349 : : return -1;
5350 : : else
5351 : : return 1;
5352 : : }
5353 : : return 0;
5354 : : }
5355 : : }
5356 : : }
5357 : : else /* Different groups or non-groups. */
5358 : : {
5359 : : /* Order groups as their first element to keep them together. */
5360 : 1926271 : if (STMT_VINFO_GROUPED_ACCESS (a0))
5361 : 1926271 : a0 = DR_GROUP_FIRST_ELEMENT (a0);
5362 : 1926271 : if (STMT_VINFO_GROUPED_ACCESS (b0))
5363 : 1926271 : b0 = DR_GROUP_FIRST_ELEMENT (b0);
5364 : 1926271 : if (a0 == b0)
5365 : : return 0;
5366 : : /* Tie using UID. */
5367 : 1926151 : else if (gimple_uid (STMT_VINFO_STMT (a0))
5368 : 1926151 : < gimple_uid (STMT_VINFO_STMT (b0)))
5369 : : return -1;
5370 : : else
5371 : : {
5372 : 861108 : gcc_assert (gimple_uid (STMT_VINFO_STMT (a0))
5373 : : != gimple_uid (STMT_VINFO_STMT (b0)));
5374 : : return 1;
5375 : : }
5376 : : }
5377 : : }
5378 : :
5379 : : /* Return whether if the load permutation of NODE is consecutive starting
5380 : : with value START_VAL in the first element. If START_VAL is not given
5381 : : the first element's value is used. */
5382 : :
5383 : : bool
5384 : 538549 : vect_load_perm_consecutive_p (slp_tree node, unsigned start_val)
5385 : : {
5386 : 538549 : load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION (node);
5387 : :
5388 : 538549 : if (!perm.exists () || !perm.length ())
5389 : : return false;
5390 : :
5391 : 538549 : if (start_val == UINT_MAX)
5392 : 95948 : start_val = perm[0];
5393 : :
5394 : 1064759 : for (unsigned int i = 0; i < perm.length (); i++)
5395 : 544340 : if (perm[i] != start_val + (unsigned int) i)
5396 : : return false;
5397 : :
5398 : : return true;
5399 : : }
5400 : :
5401 : : /* Process the set of LOADS that are all from the same dataref group. */
5402 : :
5403 : : static void
5404 : 142680 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
5405 : : scalar_stmts_to_slp_tree_map_t *bst_map,
5406 : : const array_slice<slp_tree> &loads,
5407 : : bool force_single_lane)
5408 : : {
5409 : : /* We at this point want to lower without a fixed VF or vector
5410 : : size in mind which means we cannot actually compute whether we
5411 : : need three or more vectors for a load permutation yet. So always
5412 : : lower. */
5413 : 142680 : stmt_vec_info first
5414 : 142680 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (loads[0])[0]);
5415 : 142680 : unsigned group_lanes = DR_GROUP_SIZE (first);
5416 : :
5417 : : /* Verify if all load permutations can be implemented with a suitably
5418 : : large element load-lanes operation. */
5419 : 142680 : unsigned ld_lanes_lanes = SLP_TREE_LANES (loads[0]);
5420 : 142680 : if (STMT_VINFO_STRIDED_P (first)
5421 : 140534 : || compare_step_with_zero (loop_vinfo, first) <= 0
5422 : 138381 : || exact_log2 (ld_lanes_lanes) == -1
5423 : : /* ??? For now only support the single-lane case as there is
5424 : : missing support on the store-lane side and code generation
5425 : : isn't up to the task yet. */
5426 : 136474 : || ld_lanes_lanes != 1
5427 : 271972 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (loads[0]),
5428 : : group_lanes / ld_lanes_lanes,
5429 : : false) == IFN_LAST)
5430 : : ld_lanes_lanes = 0;
5431 : : else
5432 : : /* Verify the loads access the same number of lanes aligned to
5433 : : ld_lanes_lanes. */
5434 : 0 : for (slp_tree load : loads)
5435 : : {
5436 : 0 : if (SLP_TREE_LANES (load) != ld_lanes_lanes)
5437 : : {
5438 : : ld_lanes_lanes = 0;
5439 : : break;
5440 : : }
5441 : 0 : unsigned first = SLP_TREE_LOAD_PERMUTATION (load)[0];
5442 : 0 : if (first % ld_lanes_lanes != 0)
5443 : : {
5444 : : ld_lanes_lanes = 0;
5445 : : break;
5446 : : }
5447 : 0 : if (!vect_load_perm_consecutive_p (load))
5448 : : {
5449 : : ld_lanes_lanes = 0;
5450 : : break;
5451 : : }
5452 : : }
5453 : :
5454 : : /* Only a power-of-two number of lanes matches interleaving with N levels.
5455 : : ??? An even number of lanes could be reduced to 1<<ceil_log2(N)-1 lanes
5456 : : at each step. */
5457 : 230520 : if (ld_lanes_lanes == 0 && exact_log2 (group_lanes) == -1 && group_lanes != 3)
5458 : : return;
5459 : :
5460 : 251641 : for (slp_tree load : loads)
5461 : : {
5462 : : /* Leave masked or gather loads alone for now. */
5463 : 171009 : if (!SLP_TREE_CHILDREN (load).is_empty ())
5464 : 48566 : continue;
5465 : :
5466 : : /* For single-element interleaving spanning multiple vectors avoid
5467 : : lowering, we want to use VMAT_ELEMENTWISE later. */
5468 : 171003 : if (ld_lanes_lanes == 0
5469 : 171003 : && SLP_TREE_LANES (load) == 1
5470 : 158012 : && !DR_GROUP_NEXT_ELEMENT (first)
5471 : 229902 : && maybe_gt (group_lanes,
5472 : : TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (load))))
5473 : 30290 : return;
5474 : :
5475 : : /* We want to pattern-match special cases here and keep those
5476 : : alone. Candidates are splats and load-lane. */
5477 : :
5478 : : /* We need to lower only loads of less than half of the groups
5479 : : lanes, including duplicate lanes. Note this leaves nodes
5480 : : with a non-1:1 load permutation around instead of canonicalizing
5481 : : those into a load and a permute node. Removing this early
5482 : : check would do such canonicalization. */
5483 : 140713 : if (SLP_TREE_LANES (load) >= (group_lanes + 1) / 2
5484 : 45127 : && ld_lanes_lanes == 0)
5485 : 45127 : continue;
5486 : :
5487 : : /* Build the permute to get the original load permutation order. */
5488 : 95586 : bool contiguous = vect_load_perm_consecutive_p (load);
5489 : 95586 : lane_permutation_t final_perm;
5490 : 95586 : final_perm.create (SLP_TREE_LANES (load));
5491 : 191758 : for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i)
5492 : 192344 : final_perm.quick_push (
5493 : 96172 : std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i]));
5494 : :
5495 : : /* When the load permutation accesses a contiguous unpermuted,
5496 : : power-of-two aligned and sized chunk leave the load alone.
5497 : : We can likely (re-)load it more efficiently rather than
5498 : : extracting it from the larger load.
5499 : : ??? Long-term some of the lowering should move to where
5500 : : the vector types involved are fixed. */
5501 : 99019 : if (!force_single_lane
5502 : 95586 : && ld_lanes_lanes == 0
5503 : 59763 : && contiguous
5504 : 59526 : && (SLP_TREE_LANES (load) > 1 || loads.size () == 1)
5505 : 6589 : && pow2p_hwi (SLP_TREE_LANES (load))
5506 : 6589 : && pow2p_hwi (group_lanes)
5507 : 3433 : && SLP_TREE_LOAD_PERMUTATION (load)[0] % SLP_TREE_LANES (load) == 0
5508 : 99019 : && group_lanes % SLP_TREE_LANES (load) == 0)
5509 : : {
5510 : 3433 : final_perm.release ();
5511 : 3433 : continue;
5512 : : }
5513 : :
5514 : : /* First build (and possibly re-use) a load node for the
5515 : : unpermuted group. Gaps in the middle and on the end are
5516 : : represented with NULL stmts. */
5517 : 92153 : vec<stmt_vec_info> stmts;
5518 : 92153 : stmts.create (group_lanes);
5519 : 310110 : for (stmt_vec_info s = first; s; s = DR_GROUP_NEXT_ELEMENT (s))
5520 : : {
5521 : 217957 : if (s != first)
5522 : 130286 : for (unsigned i = 1; i < DR_GROUP_GAP (s); ++i)
5523 : 4482 : stmts.quick_push (NULL);
5524 : 217957 : stmts.quick_push (s);
5525 : : }
5526 : 176842 : for (unsigned i = 0; i < DR_GROUP_GAP (first); ++i)
5527 : 84689 : stmts.quick_push (NULL);
5528 : 92153 : poly_uint64 max_nunits = 1;
5529 : 92153 : bool *matches = XALLOCAVEC (bool, group_lanes);
5530 : 92153 : unsigned limit = 1;
5531 : 92153 : unsigned tree_size = 0;
5532 : 92153 : slp_tree l0 = vect_build_slp_tree (loop_vinfo, stmts,
5533 : : group_lanes,
5534 : : &max_nunits, matches, &limit,
5535 : 92153 : &tree_size, bst_map);
5536 : 92153 : gcc_assert (!SLP_TREE_LOAD_PERMUTATION (l0).exists ());
5537 : :
5538 : 92153 : if (ld_lanes_lanes != 0)
5539 : : {
5540 : : /* ??? If this is not in sync with what get_load_store_type
5541 : : later decides the SLP representation is not good for other
5542 : : store vectorization methods. */
5543 : 0 : l0->ldst_lanes = true;
5544 : 0 : load->ldst_lanes = true;
5545 : : }
5546 : :
5547 : 283561 : while (1)
5548 : : {
5549 : 187857 : unsigned group_lanes = SLP_TREE_LANES (l0);
5550 : 187857 : if (ld_lanes_lanes != 0
5551 : 187857 : || SLP_TREE_LANES (load) >= (group_lanes + 1) / 2)
5552 : : break;
5553 : :
5554 : : /* Try to lower by reducing the group to half its size using an
5555 : : interleaving scheme. For this try to compute whether all
5556 : : elements needed for this load are in even or odd elements of
5557 : : an even/odd decomposition with N consecutive elements.
5558 : : Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition
5559 : : with N == 2. */
5560 : : /* ??? Only an even number of lanes can be handed this way, but the
5561 : : fallback below could work for any number. We have to make sure
5562 : : to round up in that case. */
5563 : 95704 : gcc_assert ((group_lanes & 1) == 0 || group_lanes == 3);
5564 : 10056 : unsigned even = 0, odd = 0;
5565 : 10056 : if ((group_lanes & 1) == 0)
5566 : : {
5567 : 10056 : even = (1 << ceil_log2 (group_lanes)) - 1;
5568 : 10056 : odd = even;
5569 : 40751 : for (auto l : final_perm)
5570 : : {
5571 : 10583 : even &= ~l.second;
5572 : 10583 : odd &= l.second;
5573 : : }
5574 : : }
5575 : :
5576 : : /* Now build an even or odd extraction from the unpermuted load. */
5577 : 95704 : lane_permutation_t perm;
5578 : 95704 : perm.create ((group_lanes + 1) / 2);
5579 : 95704 : unsigned even_level = even ? 1 << ctz_hwi (even) : 0;
5580 : 95704 : unsigned odd_level = odd ? 1 << ctz_hwi (odd) : 0;
5581 : 95704 : if (even_level
5582 : 9342 : && group_lanes % (2 * even_level) == 0
5583 : : /* ??? When code generating permutes we do not try to pun
5584 : : to larger component modes so level != 1 isn't a natural
5585 : : even/odd extract. Prefer one if possible. */
5586 : 9342 : && (even_level == 1 || !odd_level || odd_level != 1))
5587 : : {
5588 : : /* { 0, 1, ... 4, 5 ..., } */
5589 : 34849 : for (unsigned i = 0; i < group_lanes / 2 / even_level; ++i)
5590 : 55054 : for (unsigned j = 0; j < even_level; ++j)
5591 : 27616 : perm.quick_push (std::make_pair (0, 2 * i * even_level + j));
5592 : : }
5593 : 86362 : else if (odd_level)
5594 : : {
5595 : : /* { ..., 2, 3, ... 6, 7 } */
5596 : 2633 : gcc_assert (group_lanes % (2 * odd_level) == 0);
5597 : 11621 : for (unsigned i = 0; i < group_lanes / 2 / odd_level; ++i)
5598 : 17976 : for (unsigned j = 0; j < odd_level; ++j)
5599 : 8988 : perm.quick_push
5600 : 8988 : (std::make_pair (0, (2 * i + 1) * odd_level + j));
5601 : : }
5602 : : else
5603 : : {
5604 : : /* As fallback extract all used lanes and fill to half the
5605 : : group size by repeating the last element.
5606 : : ??? This is quite a bad strathegy for re-use - we could
5607 : : brute force our way to find more optimal filling lanes to
5608 : : maximize re-use when looking at all loads from the group. */
5609 : 85660 : auto_bitmap l;
5610 : 342660 : for (auto p : final_perm)
5611 : 85680 : bitmap_set_bit (l, p.second);
5612 : 85660 : unsigned i = 0;
5613 : 85660 : bitmap_iterator bi;
5614 : 171340 : EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi)
5615 : 85680 : perm.quick_push (std::make_pair (0, i));
5616 : 342648 : while (perm.length () < (group_lanes + 1) / 2)
5617 : 85664 : perm.quick_push (perm.last ());
5618 : 85660 : }
5619 : :
5620 : : /* Update final_perm with the intermediate permute. */
5621 : 191935 : for (unsigned i = 0; i < final_perm.length (); ++i)
5622 : : {
5623 : 96231 : unsigned l = final_perm[i].second;
5624 : 96231 : unsigned j;
5625 : 103157 : for (j = 0; j < perm.length (); ++j)
5626 : 103157 : if (perm[j].second == l)
5627 : : {
5628 : 96231 : final_perm[i].second = j;
5629 : 96231 : break;
5630 : : }
5631 : 96231 : gcc_assert (j < perm.length ());
5632 : : }
5633 : :
5634 : : /* And create scalar stmts. */
5635 : 95704 : vec<stmt_vec_info> perm_stmts;
5636 : 95704 : perm_stmts.create (perm.length ());
5637 : 303652 : for (unsigned i = 0; i < perm.length (); ++i)
5638 : 207948 : perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]);
5639 : :
5640 : 95704 : slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR);
5641 : 95704 : SLP_TREE_CHILDREN (p).quick_push (l0);
5642 : 95704 : SLP_TREE_LANE_PERMUTATION (p) = perm;
5643 : 95704 : SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load);
5644 : 95704 : SLP_TREE_LANES (p) = perm.length ();
5645 : 95704 : SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load);
5646 : : /* ??? As we have scalar stmts for this intermediate permute we
5647 : : could CSE it via bst_map but we do not want to pick up
5648 : : another SLP node with a load permutation. We instead should
5649 : : have a "local" CSE map here. */
5650 : 95704 : SLP_TREE_SCALAR_STMTS (p) = perm_stmts;
5651 : :
5652 : : /* We now have a node for (group_lanes + 1) / 2 lanes. */
5653 : 95704 : l0 = p;
5654 : 95704 : }
5655 : :
5656 : : /* And finally from the ordered reduction node create the
5657 : : permute to shuffle the lanes into the original load-permutation
5658 : : order. We replace the original load node with this. */
5659 : 92153 : SLP_TREE_CODE (load) = VEC_PERM_EXPR;
5660 : 92153 : SLP_TREE_LOAD_PERMUTATION (load).release ();
5661 : 92153 : SLP_TREE_LANE_PERMUTATION (load) = final_perm;
5662 : 92153 : SLP_TREE_CHILDREN (load).create (1);
5663 : 92153 : SLP_TREE_CHILDREN (load).quick_push (l0);
5664 : : }
5665 : : }
5666 : :
5667 : : /* Transform SLP loads in the SLP graph created by SLP discovery to
5668 : : group loads from the same group and lower load permutations that
5669 : : are unlikely to be supported into a series of permutes.
5670 : : In the degenerate case of having only single-lane SLP instances
5671 : : this should result in a series of permute nodes emulating an
5672 : : interleaving scheme. */
5673 : :
5674 : : static void
5675 : 392224 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
5676 : : scalar_stmts_to_slp_tree_map_t *bst_map,
5677 : : bool force_single_lane)
5678 : : {
5679 : : /* Gather and sort loads across all instances. */
5680 : 392224 : hash_set<slp_tree> visited;
5681 : 392224 : auto_vec<slp_tree> loads;
5682 : 1798125 : for (auto inst : loop_vinfo->slp_instances)
5683 : 623503 : vect_gather_slp_loads (loads, SLP_INSTANCE_TREE (inst), visited);
5684 : 392224 : if (loads.is_empty ())
5685 : 71282 : return;
5686 : 320942 : loads.qsort (vllp_cmp);
5687 : :
5688 : : /* Now process each dataref group separately. */
5689 : 320942 : unsigned firsti = 0;
5690 : 609943 : for (unsigned i = 1; i < loads.length (); ++i)
5691 : : {
5692 : 289001 : slp_tree first = loads[firsti];
5693 : 289001 : slp_tree next = loads[i];
5694 : 289001 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (first)[0];
5695 : 289001 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (next)[0];
5696 : 289001 : if (STMT_VINFO_GROUPED_ACCESS (a0)
5697 : 147095 : && STMT_VINFO_GROUPED_ACCESS (b0)
5698 : 423125 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
5699 : 65041 : continue;
5700 : : /* Now we have one or multiple SLP loads of the same group from
5701 : : firsti to i - 1. */
5702 : 223960 : if (STMT_VINFO_GROUPED_ACCESS (a0))
5703 : 82054 : vect_lower_load_permutations (loop_vinfo, bst_map,
5704 : 82054 : make_array_slice (&loads[firsti],
5705 : : i - firsti),
5706 : : force_single_lane);
5707 : : firsti = i;
5708 : : }
5709 : 641884 : if (firsti < loads.length ()
5710 : 641884 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (loads[firsti])[0]))
5711 : 60626 : vect_lower_load_permutations (loop_vinfo, bst_map,
5712 : 60626 : make_array_slice (&loads[firsti],
5713 : 60626 : loads.length () - firsti),
5714 : : force_single_lane);
5715 : 392224 : }
5716 : :
5717 : : /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
5718 : : trees of packed scalar stmts if SLP is possible. */
5719 : :
5720 : : opt_result
5721 : 1055128 : vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size,
5722 : : bool force_single_lane)
5723 : : {
5724 : 1055128 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5725 : 1055128 : unsigned int i;
5726 : 1055128 : stmt_vec_info first_element;
5727 : 1055128 : slp_instance instance;
5728 : :
5729 : 1055128 : DUMP_VECT_SCOPE ("vect_analyze_slp");
5730 : :
5731 : 1055128 : unsigned limit = max_tree_size;
5732 : :
5733 : 1055128 : scalar_stmts_to_slp_tree_map_t *bst_map
5734 : 1055128 : = new scalar_stmts_to_slp_tree_map_t ();
5735 : :
5736 : : /* Find SLP sequences starting from groups of grouped stores. */
5737 : 3025750 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
5738 : 915751 : if (! vect_analyze_slp_instance (vinfo, bst_map, first_element,
5739 : : slp_inst_kind_store, max_tree_size, &limit,
5740 : : force_single_lane)
5741 : 915751 : && loop_vinfo)
5742 : 257 : return opt_result::failure_at (vect_location, "SLP build failed.\n");
5743 : :
5744 : : /* For loops also start SLP discovery from non-grouped stores. */
5745 : 1054871 : if (loop_vinfo)
5746 : : {
5747 : : data_reference_p dr;
5748 : 1341925 : FOR_EACH_VEC_ELT (vinfo->shared->datarefs, i, dr)
5749 : 935324 : if (DR_IS_WRITE (dr))
5750 : : {
5751 : 288902 : stmt_vec_info stmt_info = vinfo->lookup_dr (dr)->stmt;
5752 : : /* Grouped stores are already handled above. */
5753 : 288902 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5754 : 74851 : continue;
5755 : 214051 : vec<stmt_vec_info> stmts;
5756 : 214051 : vec<stmt_vec_info> roots = vNULL;
5757 : 214051 : vec<tree> remain = vNULL;
5758 : 214051 : stmts.create (1);
5759 : 214051 : stmts.quick_push (stmt_info);
5760 : 214051 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
5761 : : stmts, roots, remain, max_tree_size,
5762 : : &limit, bst_map, force_single_lane))
5763 : 3933 : return opt_result::failure_at (vect_location,
5764 : : "SLP build failed.\n");
5765 : : }
5766 : :
5767 : : stmt_vec_info stmt_info;
5768 : 406641 : FOR_EACH_VEC_ELT (LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo), i, stmt_info)
5769 : : {
5770 : 20 : vec<stmt_vec_info> stmts;
5771 : 20 : vec<stmt_vec_info> roots = vNULL;
5772 : 20 : vec<tree> remain = vNULL;
5773 : 20 : stmts.create (1);
5774 : 20 : stmts.quick_push (stmt_info);
5775 : 20 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_store,
5776 : : stmts, roots, remain, max_tree_size,
5777 : : &limit, bst_map, force_single_lane))
5778 : 0 : return opt_result::failure_at (vect_location,
5779 : : "SLP build failed.\n");
5780 : : }
5781 : : }
5782 : :
5783 : 1050938 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
5784 : : {
5785 : 1874783 : for (unsigned i = 0; i < bb_vinfo->roots.length (); ++i)
5786 : : {
5787 : 1230446 : vect_location = bb_vinfo->roots[i].roots[0]->stmt;
5788 : : /* Apply patterns. */
5789 : 3843598 : for (unsigned j = 0; j < bb_vinfo->roots[i].stmts.length (); ++j)
5790 : 5226304 : bb_vinfo->roots[i].stmts[j]
5791 : 2687270 : = vect_stmt_to_vectorize (bb_vinfo->roots[i].stmts[j]);
5792 : 1230446 : if (vect_build_slp_instance (bb_vinfo, bb_vinfo->roots[i].kind,
5793 : 1230446 : bb_vinfo->roots[i].stmts,
5794 : 1230446 : bb_vinfo->roots[i].roots,
5795 : 1230446 : bb_vinfo->roots[i].remain,
5796 : : max_tree_size, &limit, bst_map, false))
5797 : : {
5798 : 132533 : bb_vinfo->roots[i].roots = vNULL;
5799 : 132533 : bb_vinfo->roots[i].remain = vNULL;
5800 : : }
5801 : 1230446 : bb_vinfo->roots[i].stmts = vNULL;
5802 : : }
5803 : : }
5804 : :
5805 : 1050938 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5806 : : {
5807 : : /* Find SLP sequences starting from groups of reductions. */
5808 : 406601 : if (!vect_analyze_slp_reductions (loop_vinfo, max_tree_size, &limit,
5809 : : bst_map, force_single_lane))
5810 : 2204 : return opt_result::failure_at (vect_location, "SLP build failed.\n");
5811 : :
5812 : : /* Make sure to vectorize only-live stmts, usually inductions. */
5813 : 1868610 : for (edge e : get_loop_exit_edges (LOOP_VINFO_LOOP (loop_vinfo)))
5814 : 1233953 : for (auto gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
5815 : 571132 : gsi_next (&gsi))
5816 : : {
5817 : 578534 : gphi *lc_phi = *gsi;
5818 : 578534 : tree def = gimple_phi_arg_def_from_edge (lc_phi, e);
5819 : 578534 : stmt_vec_info stmt_info;
5820 : 578534 : if (TREE_CODE (def) == SSA_NAME
5821 : 463882 : && !virtual_operand_p (def)
5822 : 229910 : && (stmt_info = loop_vinfo->lookup_def (def))
5823 : 198293 : && ((stmt_info = vect_stmt_to_vectorize (stmt_info)), true)
5824 : 198293 : && STMT_VINFO_RELEVANT (stmt_info) == vect_used_only_live
5825 : 153992 : && STMT_VINFO_LIVE_P (stmt_info)
5826 : 153992 : && !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
5827 : 662281 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
5828 : : {
5829 : 83668 : vec<stmt_vec_info> stmts;
5830 : 83668 : vec<stmt_vec_info> roots = vNULL;
5831 : 83668 : vec<tree> remain = vNULL;
5832 : 83668 : stmts.create (1);
5833 : 83668 : stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
5834 : 83668 : if (! vect_build_slp_instance (vinfo,
5835 : : slp_inst_kind_reduc_group,
5836 : : stmts, roots, remain,
5837 : : max_tree_size, &limit,
5838 : : bst_map, force_single_lane))
5839 : 7402 : return opt_result::failure_at (vect_location,
5840 : : "SLP build failed.\n");
5841 : : }
5842 : 7402 : }
5843 : :
5844 : : /* Find SLP sequences starting from gconds. */
5845 : 1041003 : for (auto cond : LOOP_VINFO_LOOP_CONDS (loop_vinfo))
5846 : : {
5847 : 253959 : auto cond_info = loop_vinfo->lookup_stmt (cond);
5848 : :
5849 : 253959 : cond_info = vect_stmt_to_vectorize (cond_info);
5850 : 253959 : vec<stmt_vec_info> roots = vNULL;
5851 : 253959 : roots.safe_push (cond_info);
5852 : 253959 : gimple *stmt = STMT_VINFO_STMT (cond_info);
5853 : 253959 : tree args0 = gimple_cond_lhs (stmt);
5854 : 253959 : tree args1 = gimple_cond_rhs (stmt);
5855 : :
5856 : : /* These should be enforced by cond lowering, but if it failed
5857 : : bail. */
5858 : 253959 : if (gimple_cond_code (stmt) != NE_EXPR
5859 : 252601 : || TREE_TYPE (args0) != boolean_type_node
5860 : 505532 : || !integer_zerop (args1))
5861 : : {
5862 : 2386 : roots.release ();
5863 : 2386 : return opt_result::failure_at (vect_location,
5864 : : "SLP build failed.\n");
5865 : : }
5866 : :
5867 : : /* An argument without a loop def will be codegened from vectorizing the
5868 : : root gcond itself. As such we don't need to try to build an SLP tree
5869 : : from them. It's highly likely that the resulting SLP tree here if both
5870 : : arguments have a def will be incompatible, but we rely on it being split
5871 : : later on. */
5872 : 251573 : auto varg = loop_vinfo->lookup_def (args0);
5873 : 251573 : vec<stmt_vec_info> stmts;
5874 : 251573 : vec<tree> remain = vNULL;
5875 : 251573 : stmts.create (1);
5876 : 251573 : stmts.quick_push (vect_stmt_to_vectorize (varg));
5877 : :
5878 : 251573 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_gcond,
5879 : : stmts, roots, remain,
5880 : : max_tree_size, &limit,
5881 : : bst_map, force_single_lane))
5882 : : {
5883 : 2385 : roots.release ();
5884 : 2385 : return opt_result::failure_at (vect_location,
5885 : : "SLP build failed.\n");
5886 : : }
5887 : : }
5888 : : }
5889 : :
5890 : 1036561 : hash_set<slp_tree> visited_patterns;
5891 : 1036561 : slp_tree_to_load_perm_map_t perm_cache;
5892 : 1036561 : slp_compat_nodes_map_t compat_cache;
5893 : :
5894 : : /* See if any patterns can be found in the SLP tree. */
5895 : 1036561 : bool pattern_found = false;
5896 : 3493068 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5897 : 1419946 : pattern_found |= vect_match_slp_patterns (instance, vinfo,
5898 : : &visited_patterns, &perm_cache,
5899 : : &compat_cache);
5900 : :
5901 : : /* If any were found optimize permutations of loads. */
5902 : 1036561 : if (pattern_found)
5903 : : {
5904 : 192 : hash_map<slp_tree, slp_tree> load_map;
5905 : 3198 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5906 : : {
5907 : 2814 : slp_tree root = SLP_INSTANCE_TREE (instance);
5908 : 2814 : optimize_load_redistribution (bst_map, vinfo, SLP_TREE_LANES (root),
5909 : : &load_map, root);
5910 : : }
5911 : 192 : }
5912 : :
5913 : : /* Check whether we should force some SLP instances to use load/store-lanes
5914 : : and do so by forcing SLP re-discovery with single lanes. We used
5915 : : to cancel SLP when this applied to all instances in a loop but now
5916 : : we decide this per SLP instance. It's important to do this only
5917 : : after SLP pattern recognition. */
5918 : 1036561 : if (is_a <loop_vec_info> (vinfo))
5919 : 1015727 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5920 : 623503 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
5921 : 230745 : && !SLP_INSTANCE_TREE (instance)->ldst_lanes)
5922 : : {
5923 : 230745 : slp_tree slp_root = SLP_INSTANCE_TREE (instance);
5924 : 230745 : unsigned int group_size = SLP_TREE_LANES (slp_root);
5925 : 230745 : tree vectype = SLP_TREE_VECTYPE (slp_root);
5926 : :
5927 : 230745 : stmt_vec_info rep_info = SLP_TREE_REPRESENTATIVE (slp_root);
5928 : 230745 : gimple *rep = STMT_VINFO_STMT (rep_info);
5929 : 230745 : bool masked = (is_gimple_call (rep)
5930 : 1331 : && gimple_call_internal_p (rep)
5931 : 232056 : && internal_fn_mask_index
5932 : 1311 : (gimple_call_internal_fn (rep)) != -1);
5933 : 230725 : if (!STMT_VINFO_GROUPED_ACCESS (rep_info)
5934 : 22456 : || slp_root->ldst_lanes
5935 : 253201 : || (vect_store_lanes_supported (vectype, group_size, masked)
5936 : : == IFN_LAST))
5937 : 230745 : continue;
5938 : :
5939 : 0 : auto_vec<slp_tree> loads;
5940 : 0 : hash_set<slp_tree> visited;
5941 : 0 : vect_gather_slp_loads (loads, slp_root, visited);
5942 : :
5943 : : /* Check whether any load in the SLP instance is possibly
5944 : : permuted. */
5945 : 0 : bool loads_permuted = false;
5946 : 0 : slp_tree load_node;
5947 : 0 : unsigned j;
5948 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5949 : : {
5950 : 0 : if (!SLP_TREE_LOAD_PERMUTATION (load_node).exists ())
5951 : 0 : continue;
5952 : : unsigned k;
5953 : : stmt_vec_info load_info;
5954 : 0 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), k, load_info)
5955 : 0 : if (SLP_TREE_LOAD_PERMUTATION (load_node)[k] != k)
5956 : : {
5957 : : loads_permuted = true;
5958 : : break;
5959 : : }
5960 : : }
5961 : :
5962 : : /* If the loads and stores can use load/store-lanes force re-discovery
5963 : : with single lanes. */
5964 : 0 : if (loads_permuted)
5965 : : {
5966 : 0 : bool can_use_lanes = true;
5967 : : bool prefer_load_lanes = false;
5968 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5969 : 0 : if (STMT_VINFO_GROUPED_ACCESS
5970 : : (SLP_TREE_REPRESENTATIVE (load_node)))
5971 : : {
5972 : 0 : stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
5973 : : (SLP_TREE_REPRESENTATIVE (load_node));
5974 : 0 : rep = STMT_VINFO_STMT (stmt_vinfo);
5975 : 0 : masked = (is_gimple_call (rep)
5976 : 0 : && gimple_call_internal_p (rep)
5977 : 0 : && internal_fn_mask_index
5978 : 0 : (gimple_call_internal_fn (rep)));
5979 : : /* Use SLP for strided accesses (or if we can't
5980 : : load-lanes). */
5981 : 0 : if (STMT_VINFO_STRIDED_P (stmt_vinfo)
5982 : 0 : || compare_step_with_zero (vinfo, stmt_vinfo) <= 0
5983 : 0 : || vect_load_lanes_supported
5984 : 0 : (SLP_TREE_VECTYPE (load_node),
5985 : 0 : DR_GROUP_SIZE (stmt_vinfo), masked) == IFN_LAST
5986 : : /* ??? During SLP re-discovery with a single lane
5987 : : a masked grouped load will appear permuted and
5988 : : discovery will fail. We have to rework this
5989 : : on the discovery side - for now avoid ICEing. */
5990 : 0 : || masked)
5991 : : {
5992 : : can_use_lanes = false;
5993 : : break;
5994 : : }
5995 : : /* Make sure that the target would prefer store-lanes
5996 : : for at least one of the loads.
5997 : :
5998 : : ??? Perhaps we should instead require this for
5999 : : all loads? */
6000 : 0 : prefer_load_lanes
6001 : : = (prefer_load_lanes
6002 : 0 : || SLP_TREE_LANES (load_node) == group_size
6003 : 0 : || (vect_slp_prefer_store_lanes_p
6004 : 0 : (vinfo, stmt_vinfo,
6005 : : SLP_TREE_VECTYPE (load_node), masked,
6006 : : group_size, SLP_TREE_LANES (load_node))));
6007 : : }
6008 : :
6009 : 0 : if (can_use_lanes && prefer_load_lanes)
6010 : : {
6011 : 0 : if (dump_enabled_p ())
6012 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6013 : : "SLP instance %p can use load/store-lanes,"
6014 : : " re-discovering with single-lanes\n",
6015 : : (void *) instance);
6016 : :
6017 : 0 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (slp_root);
6018 : :
6019 : 0 : vect_free_slp_instance (instance);
6020 : 0 : limit = max_tree_size;
6021 : 0 : bool res = vect_analyze_slp_instance (vinfo, bst_map,
6022 : : stmt_info,
6023 : : slp_inst_kind_store,
6024 : : max_tree_size, &limit,
6025 : : true);
6026 : 0 : gcc_assert (res);
6027 : 0 : auto new_inst = LOOP_VINFO_SLP_INSTANCES (vinfo).pop ();
6028 : 0 : LOOP_VINFO_SLP_INSTANCES (vinfo)[i] = new_inst;
6029 : : }
6030 : : }
6031 : 0 : }
6032 : :
6033 : : /* When we end up with load permutations that we cannot possibly handle,
6034 : : like those requiring three vector inputs, lower them using interleaving
6035 : : like schemes. */
6036 : 1036561 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
6037 : : {
6038 : 392224 : vect_lower_load_permutations (loop_vinfo, bst_map, force_single_lane);
6039 : 392224 : if (dump_enabled_p ())
6040 : : {
6041 : 18609 : dump_printf_loc (MSG_NOTE, vect_location,
6042 : : "SLP graph after lowering permutations:\n");
6043 : 18609 : hash_set<slp_tree> visited;
6044 : 83062 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6045 : 27259 : vect_print_slp_graph (MSG_NOTE, vect_location,
6046 : : SLP_INSTANCE_TREE (instance), visited);
6047 : 18609 : }
6048 : : }
6049 : :
6050 : 1036561 : release_scalar_stmts_to_slp_tree_map (bst_map);
6051 : :
6052 : 1036561 : if (pattern_found && dump_enabled_p ())
6053 : : {
6054 : 14 : dump_printf_loc (MSG_NOTE, vect_location,
6055 : : "Pattern matched SLP tree\n");
6056 : 14 : hash_set<slp_tree> visited;
6057 : 74 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
6058 : 32 : vect_print_slp_graph (MSG_NOTE, vect_location,
6059 : : SLP_INSTANCE_TREE (instance), visited);
6060 : 14 : }
6061 : :
6062 : 1036561 : return opt_result::success ();
6063 : 1036561 : }
6064 : :
6065 : : /* Estimates the cost of inserting layout changes into the SLP graph.
6066 : : It can also say that the insertion is impossible. */
6067 : :
6068 : : struct slpg_layout_cost
6069 : : {
6070 : 9599031 : slpg_layout_cost () = default;
6071 : : slpg_layout_cost (sreal, bool);
6072 : :
6073 : 447029 : static slpg_layout_cost impossible () { return { sreal::max (), 0 }; }
6074 : 4940149 : bool is_possible () const { return depth != sreal::max (); }
6075 : :
6076 : : bool operator== (const slpg_layout_cost &) const;
6077 : : bool operator!= (const slpg_layout_cost &) const;
6078 : :
6079 : : bool is_better_than (const slpg_layout_cost &, bool) const;
6080 : :
6081 : : void add_parallel_cost (const slpg_layout_cost &);
6082 : : void add_serial_cost (const slpg_layout_cost &);
6083 : : void split (unsigned int);
6084 : :
6085 : : /* The longest sequence of layout changes needed during any traversal
6086 : : of the partition dag, weighted by execution frequency.
6087 : :
6088 : : This is the most important metric when optimizing for speed, since
6089 : : it helps to ensure that we keep the number of operations on
6090 : : critical paths to a minimum. */
6091 : : sreal depth = 0;
6092 : :
6093 : : /* An estimate of the total number of operations needed. It is weighted by
6094 : : execution frequency when optimizing for speed but not when optimizing for
6095 : : size. In order to avoid double-counting, a node with a fanout of N will
6096 : : distribute 1/N of its total cost to each successor.
6097 : :
6098 : : This is the most important metric when optimizing for size, since
6099 : : it helps to keep the total number of operations to a minimum, */
6100 : : sreal total = 0;
6101 : : };
6102 : :
6103 : : /* Construct costs for a node with weight WEIGHT. A higher weight
6104 : : indicates more frequent execution. IS_FOR_SIZE is true if we are
6105 : : optimizing for size rather than speed. */
6106 : :
6107 : 1160402 : slpg_layout_cost::slpg_layout_cost (sreal weight, bool is_for_size)
6108 : 1161270 : : depth (weight), total (is_for_size && weight > 0 ? 1 : weight)
6109 : : {
6110 : 1160402 : }
6111 : :
6112 : : bool
6113 : 0 : slpg_layout_cost::operator== (const slpg_layout_cost &other) const
6114 : : {
6115 : 0 : return depth == other.depth && total == other.total;
6116 : : }
6117 : :
6118 : : bool
6119 : 0 : slpg_layout_cost::operator!= (const slpg_layout_cost &other) const
6120 : : {
6121 : 0 : return !operator== (other);
6122 : : }
6123 : :
6124 : : /* Return true if these costs are better than OTHER. IS_FOR_SIZE is
6125 : : true if we are optimizing for size rather than speed. */
6126 : :
6127 : : bool
6128 : 291408 : slpg_layout_cost::is_better_than (const slpg_layout_cost &other,
6129 : : bool is_for_size) const
6130 : : {
6131 : 291408 : if (is_for_size)
6132 : : {
6133 : 382 : if (total != other.total)
6134 : 159 : return total < other.total;
6135 : 223 : return depth < other.depth;
6136 : : }
6137 : : else
6138 : : {
6139 : 291026 : if (depth != other.depth)
6140 : 124540 : return depth < other.depth;
6141 : 166486 : return total < other.total;
6142 : : }
6143 : : }
6144 : :
6145 : : /* Increase the costs to account for something with cost INPUT_COST
6146 : : happening in parallel with the current costs. */
6147 : :
6148 : : void
6149 : 343551 : slpg_layout_cost::add_parallel_cost (const slpg_layout_cost &input_cost)
6150 : : {
6151 : 343551 : depth = std::max (depth, input_cost.depth);
6152 : 343551 : total += input_cost.total;
6153 : 343551 : }
6154 : :
6155 : : /* Increase the costs to account for something with cost INPUT_COST
6156 : : happening in series with the current costs. */
6157 : :
6158 : : void
6159 : 1398715 : slpg_layout_cost::add_serial_cost (const slpg_layout_cost &other)
6160 : : {
6161 : 1398715 : depth += other.depth;
6162 : 1398715 : total += other.total;
6163 : 1398715 : }
6164 : :
6165 : : /* Split the total cost among TIMES successors or predecessors. */
6166 : :
6167 : : void
6168 : 1152372 : slpg_layout_cost::split (unsigned int times)
6169 : : {
6170 : 1152372 : if (times > 1)
6171 : 480642 : total /= times;
6172 : 1152372 : }
6173 : :
6174 : : /* Information about one node in the SLP graph, for use during
6175 : : vect_optimize_slp_pass. */
6176 : :
6177 : : struct slpg_vertex
6178 : : {
6179 : 9038825 : slpg_vertex (slp_tree node_) : node (node_) {}
6180 : :
6181 : : /* The node itself. */
6182 : : slp_tree node;
6183 : :
6184 : : /* Which partition the node belongs to, or -1 if none. Nodes outside of
6185 : : partitions are flexible; they can have whichever layout consumers
6186 : : want them to have. */
6187 : : int partition = -1;
6188 : :
6189 : : /* The number of nodes that directly use the result of this one
6190 : : (i.e. the number of nodes that count this one as a child). */
6191 : : unsigned int out_degree = 0;
6192 : :
6193 : : /* The execution frequency of the node. */
6194 : : sreal weight = 0;
6195 : :
6196 : : /* The total execution frequency of all nodes that directly use the
6197 : : result of this one. */
6198 : : sreal out_weight = 0;
6199 : : };
6200 : :
6201 : : /* Information about one partition of the SLP graph, for use during
6202 : : vect_optimize_slp_pass. */
6203 : :
6204 : : struct slpg_partition_info
6205 : : {
6206 : : /* The nodes in the partition occupy indices [NODE_BEGIN, NODE_END)
6207 : : of m_partitioned_nodes. */
6208 : : unsigned int node_begin = 0;
6209 : : unsigned int node_end = 0;
6210 : :
6211 : : /* Which layout we've chosen to use for this partition, or -1 if
6212 : : we haven't picked one yet. */
6213 : : int layout = -1;
6214 : :
6215 : : /* The number of predecessors and successors in the partition dag.
6216 : : The predecessors always have lower partition numbers and the
6217 : : successors always have higher partition numbers.
6218 : :
6219 : : Note that the directions of these edges are not necessarily the
6220 : : same as in the data flow graph. For example, if an SCC has separate
6221 : : partitions for an inner loop and an outer loop, the inner loop's
6222 : : partition will have at least two incoming edges from the outer loop's
6223 : : partition: one for a live-in value and one for a live-out value.
6224 : : In data flow terms, one of these edges would also be from the outer loop
6225 : : to the inner loop, but the other would be in the opposite direction. */
6226 : : unsigned int in_degree = 0;
6227 : : unsigned int out_degree = 0;
6228 : : };
6229 : :
6230 : : /* Information about the costs of using a particular layout for a
6231 : : particular partition. It can also say that the combination is
6232 : : impossible. */
6233 : :
6234 : : struct slpg_partition_layout_costs
6235 : : {
6236 : 1411167 : bool is_possible () const { return internal_cost.is_possible (); }
6237 : 49410 : void mark_impossible () { internal_cost = slpg_layout_cost::impossible (); }
6238 : :
6239 : : /* The costs inherited from predecessor partitions. */
6240 : : slpg_layout_cost in_cost;
6241 : :
6242 : : /* The inherent cost of the layout within the node itself. For example,
6243 : : this is nonzero for a load if choosing a particular layout would require
6244 : : the load to permute the loaded elements. It is nonzero for a
6245 : : VEC_PERM_EXPR if the permutation cannot be eliminated or converted
6246 : : to full-vector moves. */
6247 : : slpg_layout_cost internal_cost;
6248 : :
6249 : : /* The costs inherited from successor partitions. */
6250 : : slpg_layout_cost out_cost;
6251 : : };
6252 : :
6253 : : /* This class tries to optimize the layout of vectors in order to avoid
6254 : : unnecessary shuffling. At the moment, the set of possible layouts are
6255 : : restricted to bijective permutations.
6256 : :
6257 : : The goal of the pass depends on whether we're optimizing for size or
6258 : : for speed. When optimizing for size, the goal is to reduce the overall
6259 : : number of layout changes (including layout changes implied by things
6260 : : like load permutations). When optimizing for speed, the goal is to
6261 : : reduce the maximum latency attributable to layout changes on any
6262 : : non-cyclical path through the data flow graph.
6263 : :
6264 : : For example, when optimizing a loop nest for speed, we will prefer
6265 : : to make layout changes outside of a loop rather than inside of a loop,
6266 : : and will prefer to make layout changes in parallel rather than serially,
6267 : : even if that increases the overall number of layout changes.
6268 : :
6269 : : The high-level procedure is:
6270 : :
6271 : : (1) Build a graph in which edges go from uses (parents) to definitions
6272 : : (children).
6273 : :
6274 : : (2) Divide the graph into a dag of strongly-connected components (SCCs).
6275 : :
6276 : : (3) When optimizing for speed, partition the nodes in each SCC based
6277 : : on their containing cfg loop. When optimizing for size, treat
6278 : : each SCC as a single partition.
6279 : :
6280 : : This gives us a dag of partitions. The goal is now to assign a
6281 : : layout to each partition.
6282 : :
6283 : : (4) Construct a set of vector layouts that are worth considering.
6284 : : Record which nodes must keep their current layout.
6285 : :
6286 : : (5) Perform a forward walk over the partition dag (from loads to stores)
6287 : : accumulating the "forward" cost of using each layout. When visiting
6288 : : each partition, assign a tentative choice of layout to the partition
6289 : : and use that choice when calculating the cost of using a different
6290 : : layout in successor partitions.
6291 : :
6292 : : (6) Perform a backward walk over the partition dag (from stores to loads),
6293 : : accumulating the "backward" cost of using each layout. When visiting
6294 : : each partition, make a final choice of layout for that partition based
6295 : : on the accumulated forward costs (from (5)) and backward costs
6296 : : (from (6)).
6297 : :
6298 : : (7) Apply the chosen layouts to the SLP graph.
6299 : :
6300 : : For example, consider the SLP statements:
6301 : :
6302 : : S1: a_1 = load
6303 : : loop:
6304 : : S2: a_2 = PHI<a_1, a_3>
6305 : : S3: b_1 = load
6306 : : S4: a_3 = a_2 + b_1
6307 : : exit:
6308 : : S5: a_4 = PHI<a_3>
6309 : : S6: store a_4
6310 : :
6311 : : S2 and S4 form an SCC and are part of the same loop. Every other
6312 : : statement is in a singleton SCC. In this example there is a one-to-one
6313 : : mapping between SCCs and partitions and the partition dag looks like this;
6314 : :
6315 : : S1 S3
6316 : : \ /
6317 : : S2+S4
6318 : : |
6319 : : S5
6320 : : |
6321 : : S6
6322 : :
6323 : : S2, S3 and S4 will have a higher execution frequency than the other
6324 : : statements, so when optimizing for speed, the goal is to avoid any
6325 : : layout changes:
6326 : :
6327 : : - within S3
6328 : : - within S2+S4
6329 : : - on the S3->S2+S4 edge
6330 : :
6331 : : For example, if S3 was originally a reversing load, the goal of the
6332 : : pass is to make it an unreversed load and change the layout on the
6333 : : S1->S2+S4 and S2+S4->S5 edges to compensate. (Changing the layout
6334 : : on S1->S2+S4 and S5->S6 would also be acceptable.)
6335 : :
6336 : : The difference between SCCs and partitions becomes important if we
6337 : : add an outer loop:
6338 : :
6339 : : S1: a_1 = ...
6340 : : loop1:
6341 : : S2: a_2 = PHI<a_1, a_6>
6342 : : S3: b_1 = load
6343 : : S4: a_3 = a_2 + b_1
6344 : : loop2:
6345 : : S5: a_4 = PHI<a_3, a_5>
6346 : : S6: c_1 = load
6347 : : S7: a_5 = a_4 + c_1
6348 : : exit2:
6349 : : S8: a_6 = PHI<a_5>
6350 : : S9: store a_6
6351 : : exit1:
6352 : :
6353 : : Here, S2, S4, S5, S7 and S8 form a single SCC. However, when optimizing
6354 : : for speed, we usually do not want restrictions in the outer loop to "infect"
6355 : : the decision for the inner loop. For example, if an outer-loop node
6356 : : in the SCC contains a statement with a fixed layout, that should not
6357 : : prevent the inner loop from using a different layout. Conversely,
6358 : : the inner loop should not dictate a layout to the outer loop: if the
6359 : : outer loop does a lot of computation, then it may not be efficient to
6360 : : do all of that computation in the inner loop's preferred layout.
6361 : :
6362 : : So when optimizing for speed, we partition the SCC into S2+S4+S8 (outer)
6363 : : and S5+S7 (inner). We also try to arrange partitions so that:
6364 : :
6365 : : - the partition for an outer loop comes before the partition for
6366 : : an inner loop
6367 : :
6368 : : - if a sibling loop A dominates a sibling loop B, A's partition
6369 : : comes before B's
6370 : :
6371 : : This gives the following partition dag for the example above:
6372 : :
6373 : : S1 S3
6374 : : \ /
6375 : : S2+S4+S8 S6
6376 : : | \\ /
6377 : : | S5+S7
6378 : : |
6379 : : S9
6380 : :
6381 : : There are two edges from S2+S4+S8 to S5+S7: one for the edge S4->S5 and
6382 : : one for a reversal of the edge S7->S8.
6383 : :
6384 : : The backward walk picks a layout for S5+S7 before S2+S4+S8. The choice
6385 : : for S2+S4+S8 therefore has to balance the cost of using the outer loop's
6386 : : preferred layout against the cost of changing the layout on entry to the
6387 : : inner loop (S4->S5) and on exit from the inner loop (S7->S8 reversed).
6388 : :
6389 : : Although this works well when optimizing for speed, it has the downside
6390 : : when optimizing for size that the choice of layout for S5+S7 is completely
6391 : : independent of S9, which lessens the chance of reducing the overall number
6392 : : of permutations. We therefore do not partition SCCs when optimizing
6393 : : for size.
6394 : :
6395 : : To give a concrete example of the difference between optimizing
6396 : : for size and speed, consider:
6397 : :
6398 : : a[0] = (b[1] << c[3]) - d[1];
6399 : : a[1] = (b[0] << c[2]) - d[0];
6400 : : a[2] = (b[3] << c[1]) - d[3];
6401 : : a[3] = (b[2] << c[0]) - d[2];
6402 : :
6403 : : There are three different layouts here: one for a, one for b and d,
6404 : : and one for c. When optimizing for speed it is better to permute each
6405 : : of b, c and d into the order required by a, since those permutations
6406 : : happen in parallel. But when optimizing for size, it is better to:
6407 : :
6408 : : - permute c into the same order as b
6409 : : - do the arithmetic
6410 : : - permute the result into the order required by a
6411 : :
6412 : : This gives 2 permutations rather than 3. */
6413 : :
6414 : : class vect_optimize_slp_pass
6415 : : {
6416 : : public:
6417 : 632523 : vect_optimize_slp_pass (vec_info *vinfo) : m_vinfo (vinfo) {}
6418 : : void run ();
6419 : :
6420 : : private:
6421 : : /* Graph building. */
6422 : : struct loop *containing_loop (slp_tree);
6423 : : bool is_cfg_latch_edge (graph_edge *);
6424 : : void build_vertices (hash_set<slp_tree> &, slp_tree);
6425 : : void build_vertices ();
6426 : : void build_graph ();
6427 : :
6428 : : /* Partitioning. */
6429 : : void create_partitions ();
6430 : : template<typename T> void for_each_partition_edge (unsigned int, T);
6431 : :
6432 : : /* Layout selection. */
6433 : : bool is_compatible_layout (slp_tree, unsigned int);
6434 : : bool is_compatible_layout (const slpg_partition_info &, unsigned int);
6435 : : int change_layout_cost (slp_tree, unsigned int, unsigned int);
6436 : : slpg_partition_layout_costs &partition_layout_costs (unsigned int,
6437 : : unsigned int);
6438 : : void change_vec_perm_layout (slp_tree, lane_permutation_t &,
6439 : : int, unsigned int);
6440 : : int internal_node_cost (slp_tree, int, unsigned int);
6441 : : void start_choosing_layouts ();
6442 : : bool legitimize ();
6443 : :
6444 : : /* Cost propagation. */
6445 : : slpg_layout_cost edge_layout_cost (graph_edge *, unsigned int,
6446 : : unsigned int, unsigned int);
6447 : : slpg_layout_cost total_in_cost (unsigned int);
6448 : : slpg_layout_cost forward_cost (graph_edge *, unsigned int, unsigned int);
6449 : : slpg_layout_cost backward_cost (graph_edge *, unsigned int, unsigned int);
6450 : : void forward_pass ();
6451 : : void backward_pass ();
6452 : :
6453 : : /* Rematerialization. */
6454 : : slp_tree get_result_with_layout (slp_tree, unsigned int);
6455 : : void materialize ();
6456 : :
6457 : : /* Clean-up. */
6458 : : void remove_redundant_permutations ();
6459 : :
6460 : : /* Masked load lanes discovery. */
6461 : : void decide_masked_load_lanes ();
6462 : :
6463 : : void dump ();
6464 : :
6465 : : vec_info *m_vinfo;
6466 : :
6467 : : /* True if we should optimize the graph for size, false if we should
6468 : : optimize it for speed. (It wouldn't be easy to make this decision
6469 : : more locally.) */
6470 : : bool m_optimize_size;
6471 : :
6472 : : /* A graph of all SLP nodes, with edges leading from uses to definitions.
6473 : : In other words, a node's predecessors are its slp_tree parents and
6474 : : a node's successors are its slp_tree children. */
6475 : : graph *m_slpg = nullptr;
6476 : :
6477 : : /* The vertices of M_SLPG, indexed by slp_tree::vertex. */
6478 : : auto_vec<slpg_vertex> m_vertices;
6479 : :
6480 : : /* The list of all leaves of M_SLPG. such as external definitions, constants,
6481 : : and loads. */
6482 : : auto_vec<int> m_leafs;
6483 : :
6484 : : /* This array has one entry for every vector layout that we're considering.
6485 : : Element 0 is null and indicates "no change". Other entries describe
6486 : : permutations that are inherent in the current graph and that we would
6487 : : like to reverse if possible.
6488 : :
6489 : : For example, a permutation { 1, 2, 3, 0 } means that something has
6490 : : effectively been permuted in that way, such as a load group
6491 : : { a[1], a[2], a[3], a[0] } (viewed as a permutation of a[0:3]).
6492 : : We'd then like to apply the reverse permutation { 3, 0, 1, 2 }
6493 : : in order to put things "back" in order. */
6494 : : auto_vec<vec<unsigned> > m_perms;
6495 : :
6496 : : /* A partitioning of the nodes for which a layout must be chosen.
6497 : : Each partition represents an <SCC, cfg loop> pair; that is,
6498 : : nodes in different SCCs belong to different partitions, and nodes
6499 : : within an SCC can be further partitioned according to a containing
6500 : : cfg loop. Partition <SCC1, L1> comes before <SCC2, L2> if:
6501 : :
6502 : : - SCC1 != SCC2 and SCC1 is a predecessor of SCC2 in a forward walk
6503 : : from leaves (such as loads) to roots (such as stores).
6504 : :
6505 : : - SCC1 == SCC2 and L1's header strictly dominates L2's header. */
6506 : : auto_vec<slpg_partition_info> m_partitions;
6507 : :
6508 : : /* The list of all nodes for which a layout must be chosen. Nodes for
6509 : : partition P come before the nodes for partition P+1. Nodes within a
6510 : : partition are in reverse postorder. */
6511 : : auto_vec<unsigned int> m_partitioned_nodes;
6512 : :
6513 : : /* Index P * num-layouts + L contains the cost of using layout L
6514 : : for partition P. */
6515 : : auto_vec<slpg_partition_layout_costs> m_partition_layout_costs;
6516 : :
6517 : : /* Index N * num-layouts + L, if nonnull, is a node that provides the
6518 : : original output of node N adjusted to have layout L. */
6519 : : auto_vec<slp_tree> m_node_layouts;
6520 : : };
6521 : :
6522 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph.
6523 : : Also record whether we should optimize anything for speed rather
6524 : : than size. */
6525 : :
6526 : : void
6527 : 9678479 : vect_optimize_slp_pass::build_vertices (hash_set<slp_tree> &visited,
6528 : : slp_tree node)
6529 : : {
6530 : 9678479 : unsigned i;
6531 : 9678479 : slp_tree child;
6532 : :
6533 : 9678479 : if (visited.add (node))
6534 : 9678479 : return;
6535 : :
6536 : 9038825 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
6537 : : {
6538 : 7003034 : basic_block bb = gimple_bb (vect_orig_stmt (rep)->stmt);
6539 : 6254488 : if (optimize_bb_for_speed_p (bb))
6540 : 6133662 : m_optimize_size = false;
6541 : : }
6542 : :
6543 : 9038825 : node->vertex = m_vertices.length ();
6544 : 9038825 : m_vertices.safe_push (slpg_vertex (node));
6545 : :
6546 : 9038825 : bool leaf = true;
6547 : 9038825 : bool force_leaf = false;
6548 : 16684278 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
6549 : 7645453 : if (child)
6550 : : {
6551 : 6967401 : leaf = false;
6552 : 6967401 : build_vertices (visited, child);
6553 : : }
6554 : : else
6555 : : force_leaf = true;
6556 : : /* Since SLP discovery works along use-def edges all cycles have an
6557 : : entry - but there's the exception of cycles where we do not handle
6558 : : the entry explicitely (but with a NULL SLP node), like some reductions
6559 : : and inductions. Force those SLP PHIs to act as leafs to make them
6560 : : backwards reachable. */
6561 : 9038825 : if (leaf || force_leaf)
6562 : 4419604 : m_leafs.safe_push (node->vertex);
6563 : : }
6564 : :
6565 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
6566 : :
6567 : : void
6568 : 1265046 : vect_optimize_slp_pass::build_vertices ()
6569 : : {
6570 : 1265046 : hash_set<slp_tree> visited;
6571 : 1265046 : unsigned i;
6572 : 1265046 : slp_instance instance;
6573 : 1265046 : m_vertices.truncate (0);
6574 : 1265046 : m_leafs.truncate (0);
6575 : 6506216 : FOR_EACH_VEC_ELT (m_vinfo->slp_instances, i, instance)
6576 : 2711078 : build_vertices (visited, SLP_INSTANCE_TREE (instance));
6577 : 1265046 : }
6578 : :
6579 : : /* Apply (reverse) bijectite PERM to VEC. */
6580 : :
6581 : : template <class T>
6582 : : static void
6583 : 190124 : vect_slp_permute (vec<unsigned> perm,
6584 : : vec<T> &vec, bool reverse)
6585 : : {
6586 : 190124 : auto_vec<T, 64> saved;
6587 : 190124 : saved.create (vec.length ());
6588 : 619562 : for (unsigned i = 0; i < vec.length (); ++i)
6589 : 429438 : saved.quick_push (vec[i]);
6590 : :
6591 : 190124 : if (reverse)
6592 : : {
6593 : 1228594 : for (unsigned i = 0; i < vec.length (); ++i)
6594 : 427990 : vec[perm[i]] = saved[i];
6595 : 617462 : for (unsigned i = 0; i < vec.length (); ++i)
6596 : 755061 : gcc_assert (vec[perm[i]] == saved[i]);
6597 : : }
6598 : : else
6599 : : {
6600 : 4200 : for (unsigned i = 0; i < vec.length (); ++i)
6601 : 1448 : vec[i] = saved[perm[i]];
6602 : 191572 : for (unsigned i = 0; i < vec.length (); ++i)
6603 : 2172 : gcc_assert (vec[i] == saved[perm[i]]);
6604 : : }
6605 : 190124 : }
6606 : :
6607 : : /* Return the cfg loop that contains NODE. */
6608 : :
6609 : : struct loop *
6610 : 3412839 : vect_optimize_slp_pass::containing_loop (slp_tree node)
6611 : : {
6612 : 3412839 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
6613 : 3412839 : if (!rep)
6614 : 4365 : return ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father;
6615 : 3791748 : return gimple_bb (vect_orig_stmt (rep)->stmt)->loop_father;
6616 : : }
6617 : :
6618 : : /* Return true if UD (an edge from a use to a definition) is associated
6619 : : with a loop latch edge in the cfg. */
6620 : :
6621 : : bool
6622 : 6967401 : vect_optimize_slp_pass::is_cfg_latch_edge (graph_edge *ud)
6623 : : {
6624 : 6967401 : slp_tree use = m_vertices[ud->src].node;
6625 : 6967401 : slp_tree def = m_vertices[ud->dest].node;
6626 : 6967401 : if ((SLP_TREE_DEF_TYPE (use) != vect_internal_def
6627 : 6967401 : || SLP_TREE_PERMUTE_P (use))
6628 : 6588434 : || SLP_TREE_DEF_TYPE (def) != vect_internal_def)
6629 : : return false;
6630 : :
6631 : 3814310 : stmt_vec_info use_rep = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (use));
6632 : 3814310 : return (is_a<gphi *> (use_rep->stmt)
6633 : 305618 : && bb_loop_header_p (gimple_bb (use_rep->stmt))
6634 : 3968144 : && containing_loop (def) == containing_loop (use));
6635 : : }
6636 : :
6637 : : /* Build the graph. Mark edges that correspond to cfg loop latch edges with
6638 : : a nonnull data field. */
6639 : :
6640 : : void
6641 : 1265046 : vect_optimize_slp_pass::build_graph ()
6642 : : {
6643 : 1265046 : m_optimize_size = true;
6644 : 1265046 : build_vertices ();
6645 : :
6646 : 2530092 : m_slpg = new_graph (m_vertices.length ());
6647 : 12833963 : for (slpg_vertex &v : m_vertices)
6648 : 26725596 : for (slp_tree child : SLP_TREE_CHILDREN (v.node))
6649 : 7645453 : if (child)
6650 : : {
6651 : 6967401 : graph_edge *ud = add_edge (m_slpg, v.node->vertex, child->vertex);
6652 : 6967401 : if (is_cfg_latch_edge (ud))
6653 : 145680 : ud->data = this;
6654 : : }
6655 : 1265046 : }
6656 : :
6657 : : /* Return true if E corresponds to a loop latch edge in the cfg. */
6658 : :
6659 : : static bool
6660 : 3556243 : skip_cfg_latch_edges (graph_edge *e)
6661 : : {
6662 : 3556243 : return e->data;
6663 : : }
6664 : :
6665 : : /* Create the node partitions. */
6666 : :
6667 : : void
6668 : 632523 : vect_optimize_slp_pass::create_partitions ()
6669 : : {
6670 : : /* Calculate a postorder of the graph, ignoring edges that correspond
6671 : : to natural latch edges in the cfg. Reading the vector from the end
6672 : : to the beginning gives the reverse postorder. */
6673 : 632523 : auto_vec<int> initial_rpo;
6674 : 1265046 : graphds_dfs (m_slpg, &m_leafs[0], m_leafs.length (), &initial_rpo,
6675 : : false, NULL, skip_cfg_latch_edges);
6676 : 1897569 : gcc_assert (initial_rpo.length () == m_vertices.length ());
6677 : :
6678 : : /* Calculate the strongly connected components of the graph. */
6679 : 632523 : auto_vec<int> scc_grouping;
6680 : 632523 : unsigned int num_sccs = graphds_scc (m_slpg, NULL, NULL, &scc_grouping);
6681 : :
6682 : : /* Create a new index order in which all nodes from the same SCC are
6683 : : consecutive. Use scc_pos to record the index of the first node in
6684 : : each SCC. */
6685 : 632523 : auto_vec<unsigned int> scc_pos (num_sccs);
6686 : 632523 : int last_component = -1;
6687 : 632523 : unsigned int node_count = 0;
6688 : 6416686 : for (unsigned int node_i : scc_grouping)
6689 : : {
6690 : 4519117 : if (last_component != m_slpg->vertices[node_i].component)
6691 : : {
6692 : 4428966 : last_component = m_slpg->vertices[node_i].component;
6693 : 8857932 : gcc_assert (last_component == int (scc_pos.length ()));
6694 : 4428966 : scc_pos.quick_push (node_count);
6695 : : }
6696 : 4519117 : node_count += 1;
6697 : : }
6698 : 1265046 : gcc_assert (node_count == initial_rpo.length ()
6699 : : && last_component + 1 == int (num_sccs));
6700 : :
6701 : : /* Use m_partitioned_nodes to group nodes into SCC order, with the nodes
6702 : : inside each SCC following the RPO we calculated above. The fact that
6703 : : we ignored natural latch edges when calculating the RPO should ensure
6704 : : that, for natural loop nests:
6705 : :
6706 : : - the first node that we encounter in a cfg loop is the loop header phi
6707 : : - the loop header phis are in dominance order
6708 : :
6709 : : Arranging for this is an optimization (see below) rather than a
6710 : : correctness issue. Unnatural loops with a tangled mess of backedges
6711 : : will still work correctly, but might give poorer results.
6712 : :
6713 : : Also update scc_pos so that it gives 1 + the index of the last node
6714 : : in the SCC. */
6715 : 632523 : m_partitioned_nodes.safe_grow (node_count);
6716 : 5784163 : for (unsigned int old_i = initial_rpo.length (); old_i-- > 0;)
6717 : : {
6718 : 4519117 : unsigned int node_i = initial_rpo[old_i];
6719 : 4519117 : unsigned int new_i = scc_pos[m_slpg->vertices[node_i].component]++;
6720 : 4519117 : m_partitioned_nodes[new_i] = node_i;
6721 : : }
6722 : :
6723 : : /* When optimizing for speed, partition each SCC based on the containing
6724 : : cfg loop. The order we constructed above should ensure that, for natural
6725 : : cfg loops, we'll create sub-SCC partitions for outer loops before
6726 : : the corresponding sub-SCC partitions for inner loops. Similarly,
6727 : : when one sibling loop A dominates another sibling loop B, we should
6728 : : create a sub-SCC partition for A before a sub-SCC partition for B.
6729 : :
6730 : : As above, nothing depends for correctness on whether this achieves
6731 : : a natural nesting, but we should get better results when it does. */
6732 : 1265046 : m_partitions.reserve (m_vertices.length ());
6733 : 632523 : unsigned int next_partition_i = 0;
6734 : 632523 : hash_map<struct loop *, int> loop_partitions;
6735 : 632523 : unsigned int rpo_begin = 0;
6736 : 632523 : unsigned int num_partitioned_nodes = 0;
6737 : 6326535 : for (unsigned int rpo_end : scc_pos)
6738 : : {
6739 : 4428966 : loop_partitions.empty ();
6740 : : unsigned int partition_i = next_partition_i;
6741 : 8948083 : for (unsigned int rpo_i = rpo_begin; rpo_i < rpo_end; ++rpo_i)
6742 : : {
6743 : : /* Handle externals and constants optimistically throughout.
6744 : : But treat existing vectors as fixed since we do not handle
6745 : : permuting them. */
6746 : 4519117 : unsigned int node_i = m_partitioned_nodes[rpo_i];
6747 : 4519117 : auto &vertex = m_vertices[node_i];
6748 : 4519117 : if ((SLP_TREE_DEF_TYPE (vertex.node) == vect_external_def
6749 : 506825 : && !SLP_TREE_VEC_DEFS (vertex.node).exists ())
6750 : 4521188 : || SLP_TREE_DEF_TYPE (vertex.node) == vect_constant_def)
6751 : 1387803 : vertex.partition = -1;
6752 : : else
6753 : : {
6754 : 3131314 : bool existed;
6755 : 3131314 : if (m_optimize_size)
6756 : 26143 : existed = next_partition_i > partition_i;
6757 : : else
6758 : : {
6759 : 3105171 : struct loop *loop = containing_loop (vertex.node);
6760 : 3105171 : auto &entry = loop_partitions.get_or_insert (loop, &existed);
6761 : 3105171 : if (!existed)
6762 : 3015938 : entry = next_partition_i;
6763 : 3105171 : partition_i = entry;
6764 : : }
6765 : 3131314 : if (!existed)
6766 : : {
6767 : 3042003 : m_partitions.quick_push (slpg_partition_info ());
6768 : 3042003 : next_partition_i += 1;
6769 : : }
6770 : 3131314 : vertex.partition = partition_i;
6771 : 3131314 : num_partitioned_nodes += 1;
6772 : 3131314 : m_partitions[partition_i].node_end += 1;
6773 : : }
6774 : : }
6775 : 4428966 : rpo_begin = rpo_end;
6776 : : }
6777 : :
6778 : : /* Assign ranges of consecutive node indices to each partition,
6779 : : in partition order. Start with node_end being the same as
6780 : : node_begin so that the next loop can use it as a counter. */
6781 : 632523 : unsigned int node_begin = 0;
6782 : 4939572 : for (auto &partition : m_partitions)
6783 : : {
6784 : 3042003 : partition.node_begin = node_begin;
6785 : 3042003 : node_begin += partition.node_end;
6786 : 3042003 : partition.node_end = partition.node_begin;
6787 : : }
6788 : 632523 : gcc_assert (node_begin == num_partitioned_nodes);
6789 : :
6790 : : /* Finally build the list of nodes in partition order. */
6791 : 632523 : m_partitioned_nodes.truncate (num_partitioned_nodes);
6792 : 5151640 : for (unsigned int node_i = 0; node_i < m_vertices.length (); ++node_i)
6793 : : {
6794 : 4519117 : int partition_i = m_vertices[node_i].partition;
6795 : 4519117 : if (partition_i >= 0)
6796 : : {
6797 : 3131314 : unsigned int order_i = m_partitions[partition_i].node_end++;
6798 : 3131314 : m_partitioned_nodes[order_i] = node_i;
6799 : : }
6800 : : }
6801 : 632523 : }
6802 : :
6803 : : /* Look for edges from earlier partitions into node NODE_I and edges from
6804 : : node NODE_I into later partitions. Call:
6805 : :
6806 : : FN (ud, other_node_i)
6807 : :
6808 : : for each such use-to-def edge ud, where other_node_i is the node at the
6809 : : other end of the edge. */
6810 : :
6811 : : template<typename T>
6812 : : void
6813 : 3520259 : vect_optimize_slp_pass::for_each_partition_edge (unsigned int node_i, T fn)
6814 : : {
6815 : 3520259 : int partition_i = m_vertices[node_i].partition;
6816 : 3520259 : for (graph_edge *pred = m_slpg->vertices[node_i].pred;
6817 : 6006681 : pred; pred = pred->pred_next)
6818 : : {
6819 : 2486422 : int src_partition_i = m_vertices[pred->src].partition;
6820 : 2486422 : if (src_partition_i >= 0 && src_partition_i != partition_i)
6821 : 2263464 : fn (pred, pred->src);
6822 : : }
6823 : 3520259 : for (graph_edge *succ = m_slpg->vertices[node_i].succ;
6824 : 7543146 : succ; succ = succ->succ_next)
6825 : : {
6826 : 4022887 : int dest_partition_i = m_vertices[succ->dest].partition;
6827 : 4022887 : if (dest_partition_i >= 0 && dest_partition_i != partition_i)
6828 : 2285200 : fn (succ, succ->dest);
6829 : : }
6830 : 3520259 : }
6831 : :
6832 : : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
6833 : : that NODE would operate on. This test is independent of NODE's actual
6834 : : operation. */
6835 : :
6836 : : bool
6837 : 1571899 : vect_optimize_slp_pass::is_compatible_layout (slp_tree node,
6838 : : unsigned int layout_i)
6839 : : {
6840 : 1571899 : if (layout_i == 0)
6841 : : return true;
6842 : :
6843 : 909684 : if (SLP_TREE_LANES (node) != m_perms[layout_i].length ())
6844 : 11521 : return false;
6845 : :
6846 : : return true;
6847 : : }
6848 : :
6849 : : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
6850 : : that NODE would operate on for each NODE in PARTITION.
6851 : : This test is independent of NODE's actual operations. */
6852 : :
6853 : : bool
6854 : 16633 : vect_optimize_slp_pass::is_compatible_layout (const slpg_partition_info
6855 : : &partition,
6856 : : unsigned int layout_i)
6857 : : {
6858 : 33506 : for (unsigned int order_i = partition.node_begin;
6859 : 33506 : order_i < partition.node_end; ++order_i)
6860 : : {
6861 : 16939 : unsigned int node_i = m_partitioned_nodes[order_i];
6862 : 16939 : auto &vertex = m_vertices[node_i];
6863 : :
6864 : : /* The layout is incompatible if it is individually incompatible
6865 : : with any node in the partition. */
6866 : 16939 : if (!is_compatible_layout (vertex.node, layout_i))
6867 : : return false;
6868 : : }
6869 : : return true;
6870 : : }
6871 : :
6872 : : /* Return the cost (in arbtirary units) of going from layout FROM_LAYOUT_I
6873 : : to layout TO_LAYOUT_I for a node like NODE. Return -1 if either of the
6874 : : layouts is incompatible with NODE or if the change is not possible for
6875 : : some other reason.
6876 : :
6877 : : The properties taken from NODE include the number of lanes and the
6878 : : vector type. The actual operation doesn't matter. */
6879 : :
6880 : : int
6881 : 674017 : vect_optimize_slp_pass::change_layout_cost (slp_tree node,
6882 : : unsigned int from_layout_i,
6883 : : unsigned int to_layout_i)
6884 : : {
6885 : 674017 : if (!is_compatible_layout (node, from_layout_i)
6886 : 674017 : || !is_compatible_layout (node, to_layout_i))
6887 : 531 : return -1;
6888 : :
6889 : 673486 : if (from_layout_i == to_layout_i)
6890 : : return 0;
6891 : :
6892 : 292014 : auto_vec<slp_tree, 1> children (1);
6893 : 292014 : children.quick_push (node);
6894 : 292014 : auto_lane_permutation_t perm (SLP_TREE_LANES (node));
6895 : 292014 : if (from_layout_i > 0)
6896 : 824277 : for (unsigned int i : m_perms[from_layout_i])
6897 : 362421 : perm.quick_push ({ 0, i });
6898 : : else
6899 : 445965 : for (unsigned int i = 0; i < SLP_TREE_LANES (node); ++i)
6900 : 307903 : perm.quick_push ({ 0, i });
6901 : 292014 : if (to_layout_i > 0)
6902 : 138525 : vect_slp_permute (m_perms[to_layout_i], perm, true);
6903 : 292014 : auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
6904 : : children, false);
6905 : 292014 : if (count >= 0)
6906 : 287500 : return MAX (count, 1);
6907 : :
6908 : : /* ??? In principle we could try changing via layout 0, giving two
6909 : : layout changes rather than 1. Doing that would require
6910 : : corresponding support in get_result_with_layout. */
6911 : : return -1;
6912 : 292014 : }
6913 : :
6914 : : /* Return the costs of assigning layout LAYOUT_I to partition PARTITION_I. */
6915 : :
6916 : : inline slpg_partition_layout_costs &
6917 : 970282 : vect_optimize_slp_pass::partition_layout_costs (unsigned int partition_i,
6918 : : unsigned int layout_i)
6919 : : {
6920 : 1940564 : return m_partition_layout_costs[partition_i * m_perms.length () + layout_i];
6921 : : }
6922 : :
6923 : : /* Change PERM in one of two ways:
6924 : :
6925 : : - if IN_LAYOUT_I < 0, accept input operand I in the layout that has been
6926 : : chosen for child I of NODE.
6927 : :
6928 : : - if IN_LAYOUT >= 0, accept all inputs operands with that layout.
6929 : :
6930 : : In both cases, arrange for the output to have layout OUT_LAYOUT_I */
6931 : :
6932 : : void
6933 : 27358 : vect_optimize_slp_pass::
6934 : : change_vec_perm_layout (slp_tree node, lane_permutation_t &perm,
6935 : : int in_layout_i, unsigned int out_layout_i)
6936 : : {
6937 : 159792 : for (auto &entry : perm)
6938 : : {
6939 : 77718 : int this_in_layout_i = in_layout_i;
6940 : 77718 : if (this_in_layout_i < 0)
6941 : : {
6942 : 55395 : slp_tree in_node = SLP_TREE_CHILDREN (node)[entry.first];
6943 : 55395 : unsigned int in_partition_i = m_vertices[in_node->vertex].partition;
6944 : 55395 : if (in_partition_i == -1u)
6945 : 329 : continue;
6946 : 55066 : this_in_layout_i = m_partitions[in_partition_i].layout;
6947 : : }
6948 : 77389 : if (this_in_layout_i > 0)
6949 : 17057 : entry.second = m_perms[this_in_layout_i][entry.second];
6950 : : }
6951 : 27358 : if (out_layout_i > 0)
6952 : 6235 : vect_slp_permute (m_perms[out_layout_i], perm, true);
6953 : 27358 : }
6954 : :
6955 : : /* Check whether the target allows NODE to be rearranged so that the node's
6956 : : output has layout OUT_LAYOUT_I. Return the cost of the change if so,
6957 : : in the same arbitrary units as for change_layout_cost. Return -1 otherwise.
6958 : :
6959 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I < 0, also check whether
6960 : : NODE can adapt to the layout changes that have (perhaps provisionally)
6961 : : been chosen for NODE's children, so that no extra permutations are
6962 : : needed on either the input or the output of NODE.
6963 : :
6964 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I >= 0, instead assume
6965 : : that all inputs will be forced into layout IN_LAYOUT_I beforehand.
6966 : :
6967 : : IN_LAYOUT_I has no meaning for other types of node.
6968 : :
6969 : : Keeping the node as-is is always valid. If the target doesn't appear
6970 : : to support the node as-is, but might realistically support other layouts,
6971 : : then layout 0 instead has the cost of a worst-case permutation. On the
6972 : : one hand, this ensures that every node has at least one valid layout,
6973 : : avoiding what would otherwise be an awkward special case. On the other,
6974 : : it still encourages the pass to change an invalid pre-existing layout
6975 : : choice into a valid one. */
6976 : :
6977 : : int
6978 : 206673 : vect_optimize_slp_pass::internal_node_cost (slp_tree node, int in_layout_i,
6979 : : unsigned int out_layout_i)
6980 : : {
6981 : 206673 : const int fallback_cost = 1;
6982 : :
6983 : 206673 : if (SLP_TREE_PERMUTE_P (node))
6984 : : {
6985 : 23112 : auto_lane_permutation_t tmp_perm;
6986 : 23112 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
6987 : :
6988 : : /* Check that the child nodes support the chosen layout. Checking
6989 : : the first child is enough, since any second child would have the
6990 : : same shape. */
6991 : 23112 : auto first_child = SLP_TREE_CHILDREN (node)[0];
6992 : 23112 : if (in_layout_i > 0
6993 : 23112 : && !is_compatible_layout (first_child, in_layout_i))
6994 : : return -1;
6995 : :
6996 : 22585 : change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
6997 : 45170 : int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
6998 : : node, tmp_perm,
6999 : 22585 : SLP_TREE_CHILDREN (node),
7000 : : false);
7001 : 22585 : if (count < 0)
7002 : : {
7003 : 1430 : if (in_layout_i == 0 && out_layout_i == 0)
7004 : : {
7005 : : /* Use the fallback cost if the node could in principle support
7006 : : some nonzero layout for both the inputs and the outputs.
7007 : : Otherwise assume that the node will be rejected later
7008 : : and rebuilt from scalars. */
7009 : 335 : if (SLP_TREE_LANES (node) == SLP_TREE_LANES (first_child))
7010 : : return fallback_cost;
7011 : 261 : return 0;
7012 : : }
7013 : : return -1;
7014 : : }
7015 : :
7016 : : /* We currently have no way of telling whether the new layout is cheaper
7017 : : or more expensive than the old one. But at least in principle,
7018 : : it should be worth making zero permutations (whole-vector shuffles)
7019 : : cheaper than real permutations, in case the pass is able to remove
7020 : : the latter. */
7021 : 21155 : return count == 0 ? 0 : 1;
7022 : 23112 : }
7023 : :
7024 : 183561 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
7025 : 183561 : if (rep
7026 : 182666 : && STMT_VINFO_DATA_REF (rep)
7027 : 57360 : && DR_IS_READ (STMT_VINFO_DATA_REF (rep))
7028 : 224406 : && SLP_TREE_LOAD_PERMUTATION (node).exists ())
7029 : : {
7030 : 34876 : auto_load_permutation_t tmp_perm;
7031 : 34876 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
7032 : 34876 : if (out_layout_i > 0)
7033 : 12282 : vect_slp_permute (m_perms[out_layout_i], tmp_perm, true);
7034 : :
7035 : 34876 : poly_uint64 vf = 1;
7036 : 34876 : if (auto loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo))
7037 : 7832 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
7038 : 34876 : unsigned int n_perms;
7039 : 34876 : if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
7040 : : nullptr, vf, true, false, &n_perms))
7041 : : {
7042 : 1494 : auto rep = SLP_TREE_REPRESENTATIVE (node);
7043 : 1494 : if (out_layout_i == 0)
7044 : : {
7045 : : /* Use the fallback cost if the load is an N-to-N permutation.
7046 : : Otherwise assume that the node will be rejected later
7047 : : and rebuilt from scalars. */
7048 : 1089 : if (STMT_VINFO_GROUPED_ACCESS (rep)
7049 : 2178 : && (DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (rep))
7050 : 1089 : == SLP_TREE_LANES (node)))
7051 : 575 : return fallback_cost;
7052 : : return 0;
7053 : : }
7054 : : return -1;
7055 : : }
7056 : :
7057 : : /* See the comment above the corresponding VEC_PERM_EXPR handling. */
7058 : 33382 : return n_perms == 0 ? 0 : 1;
7059 : 34876 : }
7060 : :
7061 : : return 0;
7062 : : }
7063 : :
7064 : : /* Decide which element layouts we should consider using. Calculate the
7065 : : weights associated with inserting layout changes on partition edges.
7066 : : Also mark partitions that cannot change layout, by setting their
7067 : : layout to zero. */
7068 : :
7069 : : void
7070 : 632523 : vect_optimize_slp_pass::start_choosing_layouts ()
7071 : : {
7072 : : /* Used to assign unique permutation indices. */
7073 : 632523 : using perm_hash = unbounded_hashmap_traits<
7074 : : vec_free_hash_base<int_hash_base<unsigned>>,
7075 : : int_hash<int, -1, -2>
7076 : : >;
7077 : 632523 : hash_map<vec<unsigned>, int, perm_hash> layout_ids;
7078 : :
7079 : : /* Layout 0 is "no change". */
7080 : 632523 : m_perms.safe_push (vNULL);
7081 : :
7082 : : /* Create layouts from existing permutations. */
7083 : 632523 : auto_load_permutation_t tmp_perm;
7084 : 5028883 : for (unsigned int node_i : m_partitioned_nodes)
7085 : : {
7086 : : /* Leafs also double as entries to the reverse graph. Allow the
7087 : : layout of those to be changed. */
7088 : 3131314 : auto &vertex = m_vertices[node_i];
7089 : 3131314 : auto &partition = m_partitions[vertex.partition];
7090 : 3131314 : if (!m_slpg->vertices[node_i].succ)
7091 : 759587 : partition.layout = 0;
7092 : :
7093 : : /* Loads and VEC_PERM_EXPRs are the only things generating permutes. */
7094 : 3131314 : slp_tree node = vertex.node;
7095 : 3131314 : stmt_vec_info dr_stmt = SLP_TREE_REPRESENTATIVE (node);
7096 : 3131314 : slp_tree child;
7097 : 3131314 : unsigned HOST_WIDE_INT imin, imax = 0;
7098 : 3131314 : bool any_permute = false;
7099 : 3131314 : tmp_perm.truncate (0);
7100 : 3131314 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
7101 : : {
7102 : : /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the node
7103 : : unpermuted, record a layout that reverses this permutation.
7104 : :
7105 : : We would need more work to cope with loads that are internally
7106 : : permuted and also have inputs (such as masks for
7107 : : IFN_MASK_LOADs). */
7108 : 492767 : gcc_assert (partition.layout == 0 && !m_slpg->vertices[node_i].succ);
7109 : 492767 : if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt))
7110 : : {
7111 : 349483 : partition.layout = -1;
7112 : 3115755 : continue;
7113 : : }
7114 : 143284 : dr_stmt = DR_GROUP_FIRST_ELEMENT (dr_stmt);
7115 : 143284 : imin = DR_GROUP_SIZE (dr_stmt) + 1;
7116 : 143284 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
7117 : : }
7118 : 5118210 : else if (SLP_TREE_PERMUTE_P (node)
7119 : 174035 : && SLP_TREE_CHILDREN (node).length () == 1
7120 : 158884 : && (child = SLP_TREE_CHILDREN (node)[0])
7121 : 2797431 : && (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (child))
7122 : 158884 : .is_constant (&imin)))
7123 : : {
7124 : : /* If the child has the same vector size as this node,
7125 : : reversing the permutation can make the permutation a no-op.
7126 : : In other cases it can change a true permutation into a
7127 : : full-vector extract. */
7128 : 158884 : tmp_perm.reserve (SLP_TREE_LANES (node));
7129 : 417213 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7130 : 258329 : tmp_perm.quick_push (SLP_TREE_LANE_PERMUTATION (node)[j].second);
7131 : : }
7132 : : else
7133 : 2479663 : continue;
7134 : :
7135 : 801958 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7136 : : {
7137 : 499790 : unsigned idx = tmp_perm[j];
7138 : 499790 : imin = MIN (imin, idx);
7139 : 499790 : imax = MAX (imax, idx);
7140 : 499790 : if (idx - tmp_perm[0] != j)
7141 : 154825 : any_permute = true;
7142 : : }
7143 : : /* If the span doesn't match we'd disrupt VF computation, avoid
7144 : : that for now. */
7145 : 302168 : if (imax - imin + 1 != SLP_TREE_LANES (node))
7146 : 101284 : continue;
7147 : : /* If there's no permute no need to split one out. In this case
7148 : : we can consider turning a load into a permuted load, if that
7149 : : turns out to be cheaper than alternatives. */
7150 : 200884 : if (!any_permute)
7151 : : {
7152 : 185191 : partition.layout = -1;
7153 : 185191 : continue;
7154 : : }
7155 : :
7156 : : /* For now only handle true permutes, like
7157 : : vect_attempt_slp_rearrange_stmts did. This allows us to be lazy
7158 : : when permuting constants and invariants keeping the permute
7159 : : bijective. */
7160 : 15693 : auto_sbitmap load_index (SLP_TREE_LANES (node));
7161 : 15693 : bitmap_clear (load_index);
7162 : 60961 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7163 : 45268 : bitmap_set_bit (load_index, tmp_perm[j] - imin);
7164 : : unsigned j;
7165 : 60276 : for (j = 0; j < SLP_TREE_LANES (node); ++j)
7166 : 44717 : if (!bitmap_bit_p (load_index, j))
7167 : : break;
7168 : 15693 : if (j != SLP_TREE_LANES (node))
7169 : 134 : continue;
7170 : :
7171 : 15559 : vec<unsigned> perm = vNULL;
7172 : 15559 : perm.safe_grow (SLP_TREE_LANES (node), true);
7173 : 60040 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
7174 : 44481 : perm[j] = tmp_perm[j] - imin;
7175 : :
7176 : 31118 : if (int (m_perms.length ()) >= param_vect_max_layout_candidates)
7177 : : {
7178 : : /* Continue to use existing layouts, but don't add any more. */
7179 : 0 : int *entry = layout_ids.get (perm);
7180 : 0 : partition.layout = entry ? *entry : 0;
7181 : 0 : perm.release ();
7182 : : }
7183 : : else
7184 : : {
7185 : 15559 : bool existed;
7186 : 15559 : int &layout_i = layout_ids.get_or_insert (perm, &existed);
7187 : 15559 : if (existed)
7188 : 5413 : perm.release ();
7189 : : else
7190 : : {
7191 : 10146 : layout_i = m_perms.length ();
7192 : 10146 : m_perms.safe_push (perm);
7193 : : }
7194 : 15559 : partition.layout = layout_i;
7195 : : }
7196 : 15693 : }
7197 : :
7198 : : /* Initially assume that every layout is possible and has zero cost
7199 : : in every partition. */
7200 : 632523 : m_partition_layout_costs.safe_grow_cleared (m_partitions.length ()
7201 : 1265046 : * m_perms.length ());
7202 : :
7203 : : /* We have to mark outgoing permutations facing non-associating-reduction
7204 : : graph entries that are not represented as to be materialized.
7205 : : slp_inst_kind_bb_reduc currently only covers associatable reductions. */
7206 : 3253108 : for (slp_instance instance : m_vinfo->slp_instances)
7207 : 1355539 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor)
7208 : : {
7209 : 5547 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
7210 : 5547 : m_partitions[m_vertices[node_i].partition].layout = 0;
7211 : : }
7212 : 1349992 : else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_chain)
7213 : : {
7214 : 1389 : stmt_vec_info stmt_info
7215 : 1389 : = SLP_TREE_REPRESENTATIVE (SLP_INSTANCE_TREE (instance));
7216 : 1389 : vect_reduc_info reduc_info
7217 : 1389 : = info_for_reduction (as_a <loop_vec_info> (m_vinfo),
7218 : : SLP_INSTANCE_TREE (instance));
7219 : 1389 : if (needs_fold_left_reduction_p (TREE_TYPE
7220 : : (gimple_get_lhs (stmt_info->stmt)),
7221 : : VECT_REDUC_INFO_CODE (reduc_info)))
7222 : : {
7223 : 64 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
7224 : 64 : m_partitions[m_vertices[node_i].partition].layout = 0;
7225 : : }
7226 : : }
7227 : :
7228 : : /* Check which layouts each node and partition can handle. Calculate the
7229 : : weights associated with inserting layout changes on edges. */
7230 : 5028883 : for (unsigned int node_i : m_partitioned_nodes)
7231 : : {
7232 : 3131314 : auto &vertex = m_vertices[node_i];
7233 : 3131314 : auto &partition = m_partitions[vertex.partition];
7234 : 3131314 : slp_tree node = vertex.node;
7235 : :
7236 : 3131314 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
7237 : : {
7238 : 3126949 : vertex.weight = vect_slp_node_weight (node);
7239 : :
7240 : : /* We do not handle stores with a permutation, so all
7241 : : incoming permutations must have been materialized.
7242 : :
7243 : : We also don't handle masked grouped loads, which lack a
7244 : : permutation vector. In this case the memory locations
7245 : : form an implicit second input to the loads, on top of the
7246 : : explicit mask input, and the memory input's layout cannot
7247 : : be changed.
7248 : :
7249 : : On the other hand, we do support permuting gather loads and
7250 : : masked gather loads, where each scalar load is independent
7251 : : of the others. This can be useful if the address/index input
7252 : : benefits from permutation. */
7253 : 3126949 : if (STMT_VINFO_DATA_REF (rep)
7254 : 1656778 : && STMT_VINFO_GROUPED_ACCESS (rep)
7255 : 4236888 : && !SLP_TREE_LOAD_PERMUTATION (node).exists ())
7256 : 966655 : partition.layout = 0;
7257 : :
7258 : : /* We cannot change the layout of an operation that is
7259 : : not independent on lanes. Note this is an explicit
7260 : : negative list since that's much shorter than the respective
7261 : : positive one but it's critical to keep maintaining it. */
7262 : 3126949 : if (is_gimple_call (STMT_VINFO_STMT (rep)))
7263 : 23360 : switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep)))
7264 : : {
7265 : 1062 : case CFN_COMPLEX_ADD_ROT90:
7266 : 1062 : case CFN_COMPLEX_ADD_ROT270:
7267 : 1062 : case CFN_COMPLEX_MUL:
7268 : 1062 : case CFN_COMPLEX_MUL_CONJ:
7269 : 1062 : case CFN_VEC_ADDSUB:
7270 : 1062 : case CFN_VEC_FMADDSUB:
7271 : 1062 : case CFN_VEC_FMSUBADD:
7272 : 1062 : partition.layout = 0;
7273 : : default:;
7274 : : }
7275 : : }
7276 : :
7277 : 6980104 : auto process_edge = [&](graph_edge *ud, unsigned int other_node_i)
7278 : : {
7279 : 3848790 : auto &other_vertex = m_vertices[other_node_i];
7280 : :
7281 : : /* Count the number of edges from earlier partitions and the number
7282 : : of edges to later partitions. */
7283 : 3848790 : if (other_vertex.partition < vertex.partition)
7284 : 1924395 : partition.in_degree += 1;
7285 : : else
7286 : 1924395 : partition.out_degree += 1;
7287 : :
7288 : : /* If the current node uses the result of OTHER_NODE_I, accumulate
7289 : : the effects of that. */
7290 : 3848790 : if (ud->src == int (node_i))
7291 : : {
7292 : 1924395 : other_vertex.out_weight += vertex.weight;
7293 : 1924395 : other_vertex.out_degree += 1;
7294 : : }
7295 : 6980104 : };
7296 : 3131314 : for_each_partition_edge (node_i, process_edge);
7297 : : }
7298 : 632523 : }
7299 : :
7300 : : /* Return the incoming costs for node NODE_I, assuming that each input keeps
7301 : : its current (provisional) choice of layout. The inputs do not necessarily
7302 : : have the same layout as each other. */
7303 : :
7304 : : slpg_layout_cost
7305 : 3035 : vect_optimize_slp_pass::total_in_cost (unsigned int node_i)
7306 : : {
7307 : 3035 : auto &vertex = m_vertices[node_i];
7308 : 3035 : slpg_layout_cost cost;
7309 : 11081 : auto add_cost = [&](graph_edge *, unsigned int other_node_i)
7310 : : {
7311 : 8046 : auto &other_vertex = m_vertices[other_node_i];
7312 : 8046 : if (other_vertex.partition < vertex.partition)
7313 : : {
7314 : 5110 : auto &other_partition = m_partitions[other_vertex.partition];
7315 : 10220 : auto &other_costs = partition_layout_costs (other_vertex.partition,
7316 : 5110 : other_partition.layout);
7317 : 5110 : slpg_layout_cost this_cost = other_costs.in_cost;
7318 : 5110 : this_cost.add_serial_cost (other_costs.internal_cost);
7319 : 5110 : this_cost.split (other_partition.out_degree);
7320 : 5110 : cost.add_parallel_cost (this_cost);
7321 : : }
7322 : 11081 : };
7323 : 3035 : for_each_partition_edge (node_i, add_cost);
7324 : 3035 : return cost;
7325 : : }
7326 : :
7327 : : /* Return the cost of switching between layout LAYOUT1_I (at node NODE1_I)
7328 : : and layout LAYOUT2_I on cross-partition use-to-def edge UD. Return
7329 : : slpg_layout_cost::impossible () if the change isn't possible. */
7330 : :
7331 : : slpg_layout_cost
7332 : 674017 : vect_optimize_slp_pass::
7333 : : edge_layout_cost (graph_edge *ud, unsigned int node1_i, unsigned int layout1_i,
7334 : : unsigned int layout2_i)
7335 : : {
7336 : 674017 : auto &def_vertex = m_vertices[ud->dest];
7337 : 674017 : auto &use_vertex = m_vertices[ud->src];
7338 : 674017 : auto def_layout_i = ud->dest == int (node1_i) ? layout1_i : layout2_i;
7339 : 674017 : auto use_layout_i = ud->dest == int (node1_i) ? layout2_i : layout1_i;
7340 : 674017 : auto factor = change_layout_cost (def_vertex.node, def_layout_i,
7341 : : use_layout_i);
7342 : 674017 : if (factor < 0)
7343 : 5045 : return slpg_layout_cost::impossible ();
7344 : :
7345 : : /* We have a choice of putting the layout change at the site of the
7346 : : definition or at the site of the use. Prefer the former when
7347 : : optimizing for size or when the execution frequency of the
7348 : : definition is no greater than the combined execution frequencies of
7349 : : the uses. When putting the layout change at the site of the definition,
7350 : : divvy up the cost among all consumers. */
7351 : 668972 : if (m_optimize_size || def_vertex.weight <= def_vertex.out_weight)
7352 : : {
7353 : 651116 : slpg_layout_cost cost = { def_vertex.weight * factor, m_optimize_size };
7354 : 651116 : cost.split (def_vertex.out_degree);
7355 : 651116 : return cost;
7356 : : }
7357 : 17856 : return { use_vertex.weight * factor, m_optimize_size };
7358 : : }
7359 : :
7360 : : /* UD represents a use-def link between FROM_NODE_I and a node in a later
7361 : : partition; FROM_NODE_I could be the definition node or the use node.
7362 : : The node at the other end of the link wants to use layout TO_LAYOUT_I.
7363 : : Return the cost of any necessary fix-ups on edge UD, or return
7364 : : slpg_layout_cost::impossible () if the change isn't possible.
7365 : :
7366 : : At this point, FROM_NODE_I's partition has chosen the cheapest
7367 : : layout based on the information available so far, but this choice
7368 : : is only provisional. */
7369 : :
7370 : : slpg_layout_cost
7371 : 176878 : vect_optimize_slp_pass::forward_cost (graph_edge *ud, unsigned int from_node_i,
7372 : : unsigned int to_layout_i)
7373 : : {
7374 : 176878 : auto &from_vertex = m_vertices[from_node_i];
7375 : 176878 : unsigned int from_partition_i = from_vertex.partition;
7376 : 176878 : slpg_partition_info &from_partition = m_partitions[from_partition_i];
7377 : 176878 : gcc_assert (from_partition.layout >= 0);
7378 : :
7379 : : /* First calculate the cost on the assumption that FROM_PARTITION sticks
7380 : : with its current layout preference. */
7381 : 176878 : slpg_layout_cost cost = slpg_layout_cost::impossible ();
7382 : 176878 : auto edge_cost = edge_layout_cost (ud, from_node_i,
7383 : 176878 : from_partition.layout, to_layout_i);
7384 : 176878 : if (edge_cost.is_possible ())
7385 : : {
7386 : 348568 : auto &from_costs = partition_layout_costs (from_partition_i,
7387 : 174284 : from_partition.layout);
7388 : 174284 : cost = from_costs.in_cost;
7389 : 174284 : cost.add_serial_cost (from_costs.internal_cost);
7390 : 174284 : cost.split (from_partition.out_degree);
7391 : 174284 : cost.add_serial_cost (edge_cost);
7392 : : }
7393 : 2594 : else if (from_partition.layout == 0)
7394 : : /* We must allow the source partition to have layout 0 as a fallback,
7395 : : in case all other options turn out to be impossible. */
7396 : 2594 : return cost;
7397 : :
7398 : : /* Take the minimum of that cost and the cost that applies if
7399 : : FROM_PARTITION instead switches to TO_LAYOUT_I. */
7400 : 174284 : auto &direct_layout_costs = partition_layout_costs (from_partition_i,
7401 : : to_layout_i);
7402 : 174284 : if (direct_layout_costs.is_possible ())
7403 : : {
7404 : 157705 : slpg_layout_cost direct_cost = direct_layout_costs.in_cost;
7405 : 157705 : direct_cost.add_serial_cost (direct_layout_costs.internal_cost);
7406 : 157705 : direct_cost.split (from_partition.out_degree);
7407 : 157705 : if (!cost.is_possible ()
7408 : 157705 : || direct_cost.is_better_than (cost, m_optimize_size))
7409 : 42002 : cost = direct_cost;
7410 : : }
7411 : :
7412 : 174284 : return cost;
7413 : : }
7414 : :
7415 : : /* UD represents a use-def link between TO_NODE_I and a node in an earlier
7416 : : partition; TO_NODE_I could be the definition node or the use node.
7417 : : The node at the other end of the link wants to use layout FROM_LAYOUT_I;
7418 : : return the cost of any necessary fix-ups on edge UD, or
7419 : : slpg_layout_cost::impossible () if the choice cannot be made.
7420 : :
7421 : : At this point, TO_NODE_I's partition has a fixed choice of layout. */
7422 : :
7423 : : slpg_layout_cost
7424 : 164157 : vect_optimize_slp_pass::backward_cost (graph_edge *ud, unsigned int to_node_i,
7425 : : unsigned int from_layout_i)
7426 : : {
7427 : 164157 : auto &to_vertex = m_vertices[to_node_i];
7428 : 164157 : unsigned int to_partition_i = to_vertex.partition;
7429 : 164157 : slpg_partition_info &to_partition = m_partitions[to_partition_i];
7430 : 164157 : gcc_assert (to_partition.layout >= 0);
7431 : :
7432 : : /* If TO_NODE_I is a VEC_PERM_EXPR consumer, see whether it can be
7433 : : adjusted for this input having layout FROM_LAYOUT_I. Assume that
7434 : : any other inputs keep their current choice of layout. */
7435 : 164157 : auto &to_costs = partition_layout_costs (to_partition_i,
7436 : : to_partition.layout);
7437 : 164157 : if (ud->src == int (to_node_i)
7438 : 163995 : && SLP_TREE_PERMUTE_P (to_vertex.node))
7439 : : {
7440 : 9105 : auto &from_partition = m_partitions[m_vertices[ud->dest].partition];
7441 : 9105 : auto old_layout = from_partition.layout;
7442 : 9105 : from_partition.layout = from_layout_i;
7443 : 18210 : int factor = internal_node_cost (to_vertex.node, -1,
7444 : 9105 : to_partition.layout);
7445 : 9105 : from_partition.layout = old_layout;
7446 : 9105 : if (factor >= 0)
7447 : : {
7448 : 8495 : slpg_layout_cost cost = to_costs.out_cost;
7449 : 16990 : cost.add_serial_cost ({ to_vertex.weight * factor,
7450 : 8495 : m_optimize_size });
7451 : 8495 : cost.split (to_partition.in_degree);
7452 : 8495 : return cost;
7453 : : }
7454 : : }
7455 : :
7456 : : /* Compute the cost if we insert any necessary layout change on edge UD. */
7457 : 155662 : auto edge_cost = edge_layout_cost (ud, to_node_i,
7458 : 155662 : to_partition.layout, from_layout_i);
7459 : 155662 : if (edge_cost.is_possible ())
7460 : : {
7461 : 155662 : slpg_layout_cost cost = to_costs.out_cost;
7462 : 155662 : cost.add_serial_cost (to_costs.internal_cost);
7463 : 155662 : cost.split (to_partition.in_degree);
7464 : 155662 : cost.add_serial_cost (edge_cost);
7465 : 155662 : return cost;
7466 : : }
7467 : :
7468 : 0 : return slpg_layout_cost::impossible ();
7469 : : }
7470 : :
7471 : : /* Make a forward pass through the partitions, accumulating input costs.
7472 : : Make a tentative (provisional) choice of layout for each partition,
7473 : : ensuring that this choice still allows later partitions to keep
7474 : : their original layout. */
7475 : :
7476 : : void
7477 : 5218 : vect_optimize_slp_pass::forward_pass ()
7478 : : {
7479 : 113066 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
7480 : : ++partition_i)
7481 : : {
7482 : 107848 : auto &partition = m_partitions[partition_i];
7483 : :
7484 : : /* If the partition consists of a single VEC_PERM_EXPR, precompute
7485 : : the incoming cost that would apply if every predecessor partition
7486 : : keeps its current layout. This is used within the loop below. */
7487 : 107848 : slpg_layout_cost in_cost;
7488 : 107848 : slp_tree single_node = nullptr;
7489 : 107848 : if (partition.node_end == partition.node_begin + 1)
7490 : : {
7491 : 103963 : unsigned int node_i = m_partitioned_nodes[partition.node_begin];
7492 : 103963 : single_node = m_vertices[node_i].node;
7493 : 103963 : if (SLP_TREE_PERMUTE_P (single_node))
7494 : 3035 : in_cost = total_in_cost (node_i);
7495 : : }
7496 : :
7497 : : /* Go through the possible layouts. Decide which ones are valid
7498 : : for this partition and record which of the valid layouts has
7499 : : the lowest cost. */
7500 : 107848 : unsigned int min_layout_i = 0;
7501 : 107848 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7502 : 329511 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7503 : : {
7504 : 221663 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7505 : 221663 : if (!layout_costs.is_possible ())
7506 : 49410 : continue;
7507 : :
7508 : : /* If the recorded layout is already 0 then the layout cannot
7509 : : change. */
7510 : 221663 : if (partition.layout == 0 && layout_i != 0)
7511 : : {
7512 : 35756 : layout_costs.mark_impossible ();
7513 : 35756 : continue;
7514 : : }
7515 : :
7516 : 185907 : bool is_possible = true;
7517 : 377165 : for (unsigned int order_i = partition.node_begin;
7518 : 377165 : order_i < partition.node_end; ++order_i)
7519 : : {
7520 : 202661 : unsigned int node_i = m_partitioned_nodes[order_i];
7521 : 202661 : auto &vertex = m_vertices[node_i];
7522 : :
7523 : : /* Reject the layout if it is individually incompatible
7524 : : with any node in the partition. */
7525 : 202661 : if (!is_compatible_layout (vertex.node, layout_i))
7526 : : {
7527 : 10397 : is_possible = false;
7528 : 11403 : break;
7529 : : }
7530 : :
7531 : 536460 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7532 : : {
7533 : 344196 : auto &other_vertex = m_vertices[other_node_i];
7534 : 344196 : if (other_vertex.partition < vertex.partition)
7535 : : {
7536 : : /* Accumulate the incoming costs from earlier
7537 : : partitions, plus the cost of any layout changes
7538 : : on UD itself. */
7539 : 176878 : auto cost = forward_cost (ud, other_node_i, layout_i);
7540 : 176878 : if (!cost.is_possible ())
7541 : 2594 : is_possible = false;
7542 : : else
7543 : 174284 : layout_costs.in_cost.add_parallel_cost (cost);
7544 : : }
7545 : : else
7546 : : /* Reject the layout if it would make layout 0 impossible
7547 : : for later partitions. This amounts to testing that the
7548 : : target supports reversing the layout change on edges
7549 : : to later partitions.
7550 : :
7551 : : In principle, it might be possible to push a layout
7552 : : change all the way down a graph, so that it never
7553 : : needs to be reversed and so that the target doesn't
7554 : : need to support the reverse operation. But it would
7555 : : be awkward to bail out if we hit a partition that
7556 : : does not support the new layout, especially since
7557 : : we are not dealing with a lattice. */
7558 : 167318 : is_possible &= edge_layout_cost (ud, other_node_i, 0,
7559 : 167318 : layout_i).is_possible ();
7560 : 536460 : };
7561 : 192264 : for_each_partition_edge (node_i, add_cost);
7562 : :
7563 : : /* Accumulate the cost of using LAYOUT_I within NODE,
7564 : : both for the inputs and the outputs. */
7565 : 192264 : int factor = internal_node_cost (vertex.node, layout_i,
7566 : : layout_i);
7567 : 192264 : if (factor < 0)
7568 : : {
7569 : 1006 : is_possible = false;
7570 : 1006 : break;
7571 : : }
7572 : 191258 : else if (factor)
7573 : 31013 : layout_costs.internal_cost.add_serial_cost
7574 : 31013 : ({ vertex.weight * factor, m_optimize_size });
7575 : : }
7576 : 185907 : if (!is_possible)
7577 : : {
7578 : 13654 : layout_costs.mark_impossible ();
7579 : 13654 : continue;
7580 : : }
7581 : :
7582 : : /* Combine the incoming and partition-internal costs. */
7583 : 172253 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7584 : 172253 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7585 : :
7586 : : /* If this partition consists of a single VEC_PERM_EXPR, see
7587 : : if the VEC_PERM_EXPR can be changed to support output layout
7588 : : LAYOUT_I while keeping all the provisional choices of input
7589 : : layout. */
7590 : 172253 : if (single_node && SLP_TREE_PERMUTE_P (single_node))
7591 : : {
7592 : 5304 : int factor = internal_node_cost (single_node, -1, layout_i);
7593 : 5304 : if (factor >= 0)
7594 : : {
7595 : 4893 : auto weight = m_vertices[single_node->vertex].weight;
7596 : 4893 : slpg_layout_cost internal_cost
7597 : 4893 : = { weight * factor, m_optimize_size };
7598 : :
7599 : 4893 : slpg_layout_cost alt_cost = in_cost;
7600 : 4893 : alt_cost.add_serial_cost (internal_cost);
7601 : 4893 : if (alt_cost.is_better_than (combined_cost, m_optimize_size))
7602 : : {
7603 : 1552 : combined_cost = alt_cost;
7604 : 1552 : layout_costs.in_cost = in_cost;
7605 : 1552 : layout_costs.internal_cost = internal_cost;
7606 : : }
7607 : : }
7608 : : }
7609 : :
7610 : : /* Record the layout with the lowest cost. Prefer layout 0 in
7611 : : the event of a tie between it and another layout. */
7612 : 172253 : if (!min_layout_cost.is_possible ()
7613 : 64405 : || combined_cost.is_better_than (min_layout_cost,
7614 : 64405 : m_optimize_size))
7615 : : {
7616 : 121080 : min_layout_i = layout_i;
7617 : 121080 : min_layout_cost = combined_cost;
7618 : : }
7619 : : }
7620 : :
7621 : : /* This loop's handling of earlier partitions should ensure that
7622 : : choosing the original layout for the current partition is no
7623 : : less valid than it was in the original graph, even with the
7624 : : provisional layout choices for those earlier partitions. */
7625 : 107848 : gcc_assert (min_layout_cost.is_possible ());
7626 : 107848 : partition.layout = min_layout_i;
7627 : : }
7628 : 5218 : }
7629 : :
7630 : : /* Make a backward pass through the partitions, accumulating output costs.
7631 : : Make a final choice of layout for each partition. */
7632 : :
7633 : : void
7634 : 5218 : vect_optimize_slp_pass::backward_pass ()
7635 : : {
7636 : 118284 : for (unsigned int partition_i = m_partitions.length (); partition_i-- > 0;)
7637 : : {
7638 : 107848 : auto &partition = m_partitions[partition_i];
7639 : :
7640 : 107848 : unsigned int min_layout_i = 0;
7641 : 107848 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
7642 : 329511 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7643 : : {
7644 : 221663 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7645 : 221663 : if (!layout_costs.is_possible ())
7646 : 49410 : continue;
7647 : :
7648 : : /* Accumulate the costs from successor partitions. */
7649 : 172253 : bool is_possible = true;
7650 : 361212 : for (unsigned int order_i = partition.node_begin;
7651 : 361212 : order_i < partition.node_end; ++order_i)
7652 : : {
7653 : 188959 : unsigned int node_i = m_partitioned_nodes[order_i];
7654 : 188959 : auto &vertex = m_vertices[node_i];
7655 : 527275 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
7656 : : {
7657 : 338316 : auto &other_vertex = m_vertices[other_node_i];
7658 : 338316 : auto &other_partition = m_partitions[other_vertex.partition];
7659 : 338316 : if (other_vertex.partition > vertex.partition)
7660 : : {
7661 : : /* Accumulate the incoming costs from later
7662 : : partitions, plus the cost of any layout changes
7663 : : on UD itself. */
7664 : 164157 : auto cost = backward_cost (ud, other_node_i, layout_i);
7665 : 164157 : if (!cost.is_possible ())
7666 : 0 : is_possible = false;
7667 : : else
7668 : 164157 : layout_costs.out_cost.add_parallel_cost (cost);
7669 : : }
7670 : : else
7671 : : /* Make sure that earlier partitions can (if necessary
7672 : : or beneficial) keep the layout that they chose in
7673 : : the forward pass. This ensures that there is at
7674 : : least one valid choice of layout. */
7675 : 174159 : is_possible &= edge_layout_cost (ud, other_node_i,
7676 : 174159 : other_partition.layout,
7677 : 174159 : layout_i).is_possible ();
7678 : 527275 : };
7679 : 188959 : for_each_partition_edge (node_i, add_cost);
7680 : : }
7681 : 172253 : if (!is_possible)
7682 : : {
7683 : 0 : layout_costs.mark_impossible ();
7684 : 0 : continue;
7685 : : }
7686 : :
7687 : : /* Locally combine the costs from the forward and backward passes.
7688 : : (This combined cost is not passed on, since that would lead
7689 : : to double counting.) */
7690 : 172253 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7691 : 172253 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7692 : 172253 : combined_cost.add_serial_cost (layout_costs.out_cost);
7693 : :
7694 : : /* Record the layout with the lowest cost. Prefer layout 0 in
7695 : : the event of a tie between it and another layout. */
7696 : 172253 : if (!min_layout_cost.is_possible ()
7697 : 64405 : || combined_cost.is_better_than (min_layout_cost,
7698 : 64405 : m_optimize_size))
7699 : : {
7700 : 115678 : min_layout_i = layout_i;
7701 : 115678 : min_layout_cost = combined_cost;
7702 : : }
7703 : : }
7704 : :
7705 : 107848 : gcc_assert (min_layout_cost.is_possible ());
7706 : 107848 : partition.layout = min_layout_i;
7707 : : }
7708 : 5218 : }
7709 : :
7710 : : /* Return a node that applies layout TO_LAYOUT_I to the original form of NODE.
7711 : : NODE already has the layout that was selected for its partition. */
7712 : :
7713 : : slp_tree
7714 : 145592 : vect_optimize_slp_pass::get_result_with_layout (slp_tree node,
7715 : : unsigned int to_layout_i)
7716 : : {
7717 : 145592 : unsigned int result_i = node->vertex * m_perms.length () + to_layout_i;
7718 : 145592 : slp_tree result = m_node_layouts[result_i];
7719 : 145592 : if (result)
7720 : : return result;
7721 : :
7722 : 145119 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
7723 : 145119 : || (SLP_TREE_DEF_TYPE (node) == vect_external_def
7724 : : /* We can't permute vector defs in place. */
7725 : 20133 : && SLP_TREE_VEC_DEFS (node).is_empty ()))
7726 : : {
7727 : : /* If the vector is uniform or unchanged, there's nothing to do. */
7728 : 37830 : if (to_layout_i == 0 || vect_slp_tree_uniform_p (node))
7729 : : result = node;
7730 : : else
7731 : : {
7732 : 1954 : auto scalar_ops = SLP_TREE_SCALAR_OPS (node).copy ();
7733 : 1954 : result = vect_create_new_slp_node (scalar_ops);
7734 : 1954 : vect_slp_permute (m_perms[to_layout_i], scalar_ops, true);
7735 : : }
7736 : : }
7737 : : else
7738 : : {
7739 : 107289 : unsigned int partition_i = m_vertices[node->vertex].partition;
7740 : 107289 : unsigned int from_layout_i = m_partitions[partition_i].layout;
7741 : 107289 : if (from_layout_i == to_layout_i)
7742 : 106696 : return node;
7743 : :
7744 : : /* If NODE is itself a VEC_PERM_EXPR, try to create a parallel
7745 : : permutation instead of a serial one. Leave the new permutation
7746 : : in TMP_PERM on success. */
7747 : 593 : auto_lane_permutation_t tmp_perm;
7748 : 593 : unsigned int num_inputs = 1;
7749 : 593 : if (SLP_TREE_PERMUTE_P (node))
7750 : : {
7751 : 7 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
7752 : 7 : if (from_layout_i != 0)
7753 : 7 : vect_slp_permute (m_perms[from_layout_i], tmp_perm, false);
7754 : 7 : if (to_layout_i != 0)
7755 : 4 : vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
7756 : 7 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
7757 : : tmp_perm,
7758 : 7 : SLP_TREE_CHILDREN (node),
7759 : : false) >= 0)
7760 : 7 : num_inputs = SLP_TREE_CHILDREN (node).length ();
7761 : : else
7762 : 0 : tmp_perm.truncate (0);
7763 : : }
7764 : :
7765 : 593 : if (dump_enabled_p ())
7766 : : {
7767 : 68 : if (tmp_perm.length () > 0)
7768 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
7769 : : "duplicating permutation node %p with"
7770 : : " layout %d\n",
7771 : : (void *) node, to_layout_i);
7772 : : else
7773 : 62 : dump_printf_loc (MSG_NOTE, vect_location,
7774 : : "inserting permutation node in place of %p\n",
7775 : : (void *) node);
7776 : : }
7777 : :
7778 : 593 : unsigned int num_lanes = SLP_TREE_LANES (node);
7779 : 593 : result = vect_create_new_slp_node (num_inputs, VEC_PERM_EXPR);
7780 : 593 : if (SLP_TREE_SCALAR_STMTS (node).length ())
7781 : : {
7782 : 592 : auto &stmts = SLP_TREE_SCALAR_STMTS (result);
7783 : 592 : stmts.safe_splice (SLP_TREE_SCALAR_STMTS (node));
7784 : 592 : if (from_layout_i != 0)
7785 : 326 : vect_slp_permute (m_perms[from_layout_i], stmts, false);
7786 : 592 : if (to_layout_i != 0)
7787 : 270 : vect_slp_permute (m_perms[to_layout_i], stmts, true);
7788 : : }
7789 : 593 : SLP_TREE_REPRESENTATIVE (result) = SLP_TREE_REPRESENTATIVE (node);
7790 : 593 : SLP_TREE_LANES (result) = num_lanes;
7791 : 593 : SLP_TREE_VECTYPE (result) = SLP_TREE_VECTYPE (node);
7792 : 593 : result->vertex = -1;
7793 : :
7794 : 593 : auto &lane_perm = SLP_TREE_LANE_PERMUTATION (result);
7795 : 593 : if (tmp_perm.length ())
7796 : : {
7797 : 7 : lane_perm.safe_splice (tmp_perm);
7798 : 7 : SLP_TREE_CHILDREN (result).safe_splice (SLP_TREE_CHILDREN (node));
7799 : : }
7800 : : else
7801 : : {
7802 : 586 : lane_perm.create (num_lanes);
7803 : 1826 : for (unsigned j = 0; j < num_lanes; ++j)
7804 : 1240 : lane_perm.quick_push ({ 0, j });
7805 : 586 : if (from_layout_i != 0)
7806 : 319 : vect_slp_permute (m_perms[from_layout_i], lane_perm, false);
7807 : 586 : if (to_layout_i != 0)
7808 : 267 : vect_slp_permute (m_perms[to_layout_i], lane_perm, true);
7809 : 586 : SLP_TREE_CHILDREN (result).safe_push (node);
7810 : : }
7811 : 2376 : for (slp_tree child : SLP_TREE_CHILDREN (result))
7812 : 597 : child->refcnt++;
7813 : 593 : }
7814 : 38423 : m_node_layouts[result_i] = result;
7815 : 38423 : return result;
7816 : : }
7817 : :
7818 : : /* Apply the chosen vector layouts to the SLP graph. */
7819 : :
7820 : : void
7821 : 9721 : vect_optimize_slp_pass::materialize ()
7822 : : {
7823 : : /* We no longer need the costs, so avoid having two O(N * P) arrays
7824 : : live at the same time. */
7825 : 9721 : m_partition_layout_costs.release ();
7826 : 29163 : m_node_layouts.safe_grow_cleared (m_vertices.length () * m_perms.length ());
7827 : :
7828 : 19442 : auto_sbitmap fully_folded (m_vertices.length ());
7829 : 9721 : bitmap_clear (fully_folded);
7830 : 153650 : for (unsigned int node_i : m_partitioned_nodes)
7831 : : {
7832 : 124487 : auto &vertex = m_vertices[node_i];
7833 : 124487 : slp_tree node = vertex.node;
7834 : 124487 : int layout_i = m_partitions[vertex.partition].layout;
7835 : 124487 : gcc_assert (layout_i >= 0);
7836 : :
7837 : : /* Rearrange the scalar statements to match the chosen layout. */
7838 : 124487 : if (layout_i > 0)
7839 : 15017 : vect_slp_permute (m_perms[layout_i],
7840 : 15017 : SLP_TREE_SCALAR_STMTS (node), true);
7841 : :
7842 : : /* Update load and lane permutations. */
7843 : 124487 : if (SLP_TREE_PERMUTE_P (node))
7844 : : {
7845 : : /* First try to absorb the input vector layouts. If that fails,
7846 : : force the inputs to have layout LAYOUT_I too. We checked that
7847 : : that was possible before deciding to use nonzero output layouts.
7848 : : (Note that at this stage we don't really have any guarantee that
7849 : : the target supports the original VEC_PERM_EXPR.) */
7850 : 4438 : auto &perm = SLP_TREE_LANE_PERMUTATION (node);
7851 : 4438 : auto_lane_permutation_t tmp_perm;
7852 : 4438 : tmp_perm.safe_splice (perm);
7853 : 4438 : change_vec_perm_layout (node, tmp_perm, -1, layout_i);
7854 : 4438 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
7855 : : tmp_perm,
7856 : 4438 : SLP_TREE_CHILDREN (node),
7857 : : false) >= 0)
7858 : : {
7859 : 4103 : if (dump_enabled_p ()
7860 : 4919 : && !std::equal (tmp_perm.begin (), tmp_perm.end (),
7861 : : perm.begin ()))
7862 : 58 : dump_printf_loc (MSG_NOTE, vect_location,
7863 : : "absorbing input layouts into %p\n",
7864 : : (void *) node);
7865 : 23390 : std::copy (tmp_perm.begin (), tmp_perm.end (), perm.begin ());
7866 : 4103 : bitmap_set_bit (fully_folded, node_i);
7867 : : }
7868 : : else
7869 : : {
7870 : : /* Not MSG_MISSED because it would make no sense to users. */
7871 : 335 : if (dump_enabled_p ())
7872 : 46 : dump_printf_loc (MSG_NOTE, vect_location,
7873 : : "failed to absorb input layouts into %p\n",
7874 : : (void *) node);
7875 : 335 : change_vec_perm_layout (nullptr, perm, layout_i, layout_i);
7876 : : }
7877 : 4438 : }
7878 : : else
7879 : : {
7880 : 120049 : gcc_assert (!SLP_TREE_LANE_PERMUTATION (node).exists ());
7881 : 120049 : auto &load_perm = SLP_TREE_LOAD_PERMUTATION (node);
7882 : 120049 : if (layout_i > 0)
7883 : : /* ??? When we handle non-bijective permutes the idea
7884 : : is that we can force the load-permutation to be
7885 : : { min, min + 1, min + 2, ... max }. But then the
7886 : : scalar defs might no longer match the lane content
7887 : : which means wrong-code with live lane vectorization.
7888 : : So we possibly have to have NULL entries for those. */
7889 : 14918 : vect_slp_permute (m_perms[layout_i], load_perm, true);
7890 : : }
7891 : : }
7892 : :
7893 : : /* Do this before any nodes disappear, since it involves a walk
7894 : : over the leaves. */
7895 : 9721 : remove_redundant_permutations ();
7896 : :
7897 : : /* Replace each child with a correctly laid-out version. */
7898 : 153650 : for (unsigned int node_i : m_partitioned_nodes)
7899 : : {
7900 : : /* Skip nodes that have already been handled above. */
7901 : 124487 : if (bitmap_bit_p (fully_folded, node_i))
7902 : 4103 : continue;
7903 : :
7904 : 120384 : auto &vertex = m_vertices[node_i];
7905 : 120384 : int in_layout_i = m_partitions[vertex.partition].layout;
7906 : 120384 : gcc_assert (in_layout_i >= 0);
7907 : :
7908 : : unsigned j;
7909 : : slp_tree child;
7910 : 359773 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (vertex.node), j, child)
7911 : : {
7912 : 149257 : if (!child)
7913 : 3665 : continue;
7914 : :
7915 : 145592 : slp_tree new_child = get_result_with_layout (child, in_layout_i);
7916 : 145592 : if (new_child != child)
7917 : : {
7918 : 2762 : vect_free_slp_tree (child);
7919 : 2762 : SLP_TREE_CHILDREN (vertex.node)[j] = new_child;
7920 : 2762 : new_child->refcnt += 1;
7921 : : }
7922 : : }
7923 : : }
7924 : 9721 : }
7925 : :
7926 : : /* Elide load permutations that are not necessary. Such permutations might
7927 : : be pre-existing, rather than created by the layout optimizations. */
7928 : :
7929 : : void
7930 : 632523 : vect_optimize_slp_pass::remove_redundant_permutations ()
7931 : : {
7932 : 4107371 : for (unsigned int node_i : m_leafs)
7933 : : {
7934 : 2209802 : slp_tree node = m_vertices[node_i].node;
7935 : 2209802 : if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
7936 : 1717035 : continue;
7937 : :
7938 : : /* In basic block vectorization we allow any subchain of an interleaving
7939 : : chain.
7940 : : FORNOW: not in loop SLP because of realignment complications. */
7941 : 492767 : if (is_a <bb_vec_info> (m_vinfo))
7942 : : {
7943 : 151213 : bool subchain_p = true;
7944 : : stmt_vec_info next_load_info = NULL;
7945 : : stmt_vec_info load_info;
7946 : : unsigned j;
7947 : 151213 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
7948 : : {
7949 : 122960 : if (j != 0
7950 : 122960 : && (next_load_info != load_info
7951 : 57394 : || ! load_info
7952 : 57394 : || DR_GROUP_GAP (load_info) != 1))
7953 : : {
7954 : : subchain_p = false;
7955 : : break;
7956 : : }
7957 : 101047 : next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
7958 : : }
7959 : 50166 : if (subchain_p)
7960 : : {
7961 : 28253 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7962 : 28253 : continue;
7963 : : }
7964 : : }
7965 : : else
7966 : : {
7967 : 442601 : loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
7968 : 442601 : bool this_load_permuted = !vect_load_perm_consecutive_p (node, 0);
7969 : : /* When this isn't a grouped access we know it's single element
7970 : : and contiguous. */
7971 : 442601 : if (!STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (node)[0]))
7972 : : {
7973 : 349483 : if (!this_load_permuted
7974 : 349483 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7975 : 348946 : || SLP_TREE_LANES (node) == 1))
7976 : 348763 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7977 : 349483 : continue;
7978 : : }
7979 : 93118 : stmt_vec_info first_stmt_info
7980 : 93118 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node)[0]);
7981 : 93520 : if (!this_load_permuted
7982 : : /* The load requires permutation when unrolling exposes
7983 : : a gap either because the group is larger than the SLP
7984 : : group-size or because there is a gap between the groups. */
7985 : 93118 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7986 : 75813 : || ((SLP_TREE_LANES (node) == DR_GROUP_SIZE (first_stmt_info))
7987 : 124 : && DR_GROUP_GAP (first_stmt_info) == 0)))
7988 : : {
7989 : 402 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7990 : 402 : continue;
7991 : : }
7992 : : }
7993 : : }
7994 : 632523 : }
7995 : :
7996 : : /* Print the partition graph and layout information to the dump file. */
7997 : :
7998 : : void
7999 : 643 : vect_optimize_slp_pass::dump ()
8000 : : {
8001 : 643 : dump_printf_loc (MSG_NOTE, vect_location,
8002 : : "SLP optimize permutations:\n");
8003 : 1299 : for (unsigned int layout_i = 1; layout_i < m_perms.length (); ++layout_i)
8004 : : {
8005 : 656 : dump_printf_loc (MSG_NOTE, vect_location, " %d: { ", layout_i);
8006 : 656 : const char *sep = "";
8007 : 5561 : for (unsigned int idx : m_perms[layout_i])
8008 : : {
8009 : 3593 : dump_printf (MSG_NOTE, "%s%d", sep, idx);
8010 : 3593 : sep = ", ";
8011 : : }
8012 : 656 : dump_printf (MSG_NOTE, " }\n");
8013 : : }
8014 : 643 : dump_printf_loc (MSG_NOTE, vect_location,
8015 : : "SLP optimize partitions:\n");
8016 : 5104 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
8017 : : ++partition_i)
8018 : : {
8019 : 4461 : auto &partition = m_partitions[partition_i];
8020 : 4461 : dump_printf_loc (MSG_NOTE, vect_location, " -------------\n");
8021 : 4461 : dump_printf_loc (MSG_NOTE, vect_location,
8022 : : " partition %d (layout %d):\n",
8023 : : partition_i, partition.layout);
8024 : 4461 : dump_printf_loc (MSG_NOTE, vect_location, " nodes:\n");
8025 : 9148 : for (unsigned int order_i = partition.node_begin;
8026 : 9148 : order_i < partition.node_end; ++order_i)
8027 : : {
8028 : 4687 : auto &vertex = m_vertices[m_partitioned_nodes[order_i]];
8029 : 9374 : dump_printf_loc (MSG_NOTE, vect_location, " - %p:\n",
8030 : 4687 : (void *) vertex.node);
8031 : 4687 : dump_printf_loc (MSG_NOTE, vect_location,
8032 : : " weight: %f\n",
8033 : : vertex.weight.to_double ());
8034 : 4687 : if (vertex.out_degree)
8035 : 3606 : dump_printf_loc (MSG_NOTE, vect_location,
8036 : : " out weight: %f (degree %d)\n",
8037 : : vertex.out_weight.to_double (),
8038 : : vertex.out_degree);
8039 : 4687 : if (SLP_TREE_PERMUTE_P (vertex.node))
8040 : 454 : dump_printf_loc (MSG_NOTE, vect_location,
8041 : : " op: VEC_PERM_EXPR\n");
8042 : 4233 : else if (auto rep = SLP_TREE_REPRESENTATIVE (vertex.node))
8043 : 4215 : dump_printf_loc (MSG_NOTE, vect_location,
8044 : : " op template: %G", rep->stmt);
8045 : : }
8046 : 4461 : dump_printf_loc (MSG_NOTE, vect_location, " edges:\n");
8047 : 9148 : for (unsigned int order_i = partition.node_begin;
8048 : 9148 : order_i < partition.node_end; ++order_i)
8049 : : {
8050 : 4687 : unsigned int node_i = m_partitioned_nodes[order_i];
8051 : 4687 : auto &vertex = m_vertices[node_i];
8052 : 14003 : auto print_edge = [&](graph_edge *, unsigned int other_node_i)
8053 : : {
8054 : 9316 : auto &other_vertex = m_vertices[other_node_i];
8055 : 9316 : if (other_vertex.partition < vertex.partition)
8056 : 4658 : dump_printf_loc (MSG_NOTE, vect_location,
8057 : : " - %p [%d] --> %p\n",
8058 : 4658 : (void *) other_vertex.node,
8059 : : other_vertex.partition,
8060 : 4658 : (void *) vertex.node);
8061 : : else
8062 : 4658 : dump_printf_loc (MSG_NOTE, vect_location,
8063 : : " - %p --> [%d] %p\n",
8064 : 4658 : (void *) vertex.node,
8065 : : other_vertex.partition,
8066 : 4658 : (void *) other_vertex.node);
8067 : 14003 : };
8068 : 4687 : for_each_partition_edge (node_i, print_edge);
8069 : : }
8070 : :
8071 : 13582 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
8072 : : {
8073 : 9121 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
8074 : 9121 : if (layout_costs.is_possible ())
8075 : : {
8076 : 7424 : dump_printf_loc (MSG_NOTE, vect_location,
8077 : : " layout %d:%s\n", layout_i,
8078 : 7424 : partition.layout == int (layout_i)
8079 : : ? " (*)" : "");
8080 : 7424 : slpg_layout_cost combined_cost = layout_costs.in_cost;
8081 : 7424 : combined_cost.add_serial_cost (layout_costs.internal_cost);
8082 : 7424 : combined_cost.add_serial_cost (layout_costs.out_cost);
8083 : : #define TEMPLATE "{depth: %f, total: %f}"
8084 : 7424 : dump_printf_loc (MSG_NOTE, vect_location,
8085 : : " " TEMPLATE "\n",
8086 : : layout_costs.in_cost.depth.to_double (),
8087 : : layout_costs.in_cost.total.to_double ());
8088 : 7424 : dump_printf_loc (MSG_NOTE, vect_location,
8089 : : " + " TEMPLATE "\n",
8090 : : layout_costs.internal_cost.depth.to_double (),
8091 : : layout_costs.internal_cost.total.to_double ());
8092 : 7424 : dump_printf_loc (MSG_NOTE, vect_location,
8093 : : " + " TEMPLATE "\n",
8094 : : layout_costs.out_cost.depth.to_double (),
8095 : : layout_costs.out_cost.total.to_double ());
8096 : 7424 : dump_printf_loc (MSG_NOTE, vect_location,
8097 : : " = " TEMPLATE "\n",
8098 : : combined_cost.depth.to_double (),
8099 : : combined_cost.total.to_double ());
8100 : : #undef TEMPLATE
8101 : : }
8102 : : else
8103 : 1697 : dump_printf_loc (MSG_NOTE, vect_location,
8104 : : " layout %d: rejected\n", layout_i);
8105 : : }
8106 : : }
8107 : 643 : }
8108 : :
8109 : : /* Masked load lanes discovery. */
8110 : :
8111 : : void
8112 : 632523 : vect_optimize_slp_pass::decide_masked_load_lanes ()
8113 : : {
8114 : 6417277 : for (auto v : m_vertices)
8115 : : {
8116 : 4519708 : slp_tree node = v.node;
8117 : 4519708 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8118 : 3129824 : || SLP_TREE_PERMUTE_P (node))
8119 : 1564510 : continue;
8120 : 2955198 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
8121 : 1497528 : if (! STMT_VINFO_GROUPED_ACCESS (stmt_info)
8122 : : /* The mask has to be uniform. */
8123 : 951040 : || STMT_VINFO_SLP_VECT_ONLY (stmt_info)
8124 : 950974 : || ! is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
8125 : 2955253 : || ! gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
8126 : : IFN_MASK_LOAD))
8127 : 2955195 : continue;
8128 : 3 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8129 : 6 : if (STMT_VINFO_STRIDED_P (stmt_info)
8130 : 3 : || compare_step_with_zero (m_vinfo, stmt_info) <= 0
8131 : 3 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (node),
8132 : 0 : DR_GROUP_SIZE (stmt_info),
8133 : : true) == IFN_LAST)
8134 : 3 : continue;
8135 : :
8136 : : /* Uniform masks need to be suitably represented. */
8137 : 0 : slp_tree mask = SLP_TREE_CHILDREN (node)[0];
8138 : 0 : if (!SLP_TREE_PERMUTE_P (mask)
8139 : 0 : || SLP_TREE_CHILDREN (mask).length () != 1)
8140 : 0 : continue;
8141 : 0 : bool match = true;
8142 : 0 : for (auto perm : SLP_TREE_LANE_PERMUTATION (mask))
8143 : 0 : if (perm.first != 0 || perm.second != 0)
8144 : : {
8145 : : match = false;
8146 : : break;
8147 : : }
8148 : 0 : if (!match)
8149 : 0 : continue;
8150 : :
8151 : : /* Now see if the consumer side matches. */
8152 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
8153 : 0 : pred; pred = pred->pred_next)
8154 : : {
8155 : 0 : slp_tree pred_node = m_vertices[pred->src].node;
8156 : : /* All consumers should be a permute with a single outgoing lane. */
8157 : 0 : if (!SLP_TREE_PERMUTE_P (pred_node)
8158 : 0 : || SLP_TREE_LANES (pred_node) != 1)
8159 : : {
8160 : : match = false;
8161 : : break;
8162 : : }
8163 : 0 : gcc_assert (SLP_TREE_CHILDREN (pred_node).length () == 1);
8164 : : }
8165 : 0 : if (!match)
8166 : 0 : continue;
8167 : : /* Now we can mark the nodes as to use load lanes. */
8168 : 0 : node->ldst_lanes = true;
8169 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
8170 : 0 : pred; pred = pred->pred_next)
8171 : 0 : m_vertices[pred->src].node->ldst_lanes = true;
8172 : : /* The catch is we have to massage the mask. We have arranged
8173 : : analyzed uniform masks to be represented by a splat VEC_PERM
8174 : : which we can now simply elide as we cannot easily re-do SLP
8175 : : discovery here. */
8176 : 0 : slp_tree new_mask = SLP_TREE_CHILDREN (mask)[0];
8177 : 0 : SLP_TREE_REF_COUNT (new_mask)++;
8178 : 0 : SLP_TREE_CHILDREN (node)[0] = new_mask;
8179 : 0 : vect_free_slp_tree (mask);
8180 : : }
8181 : 632523 : }
8182 : :
8183 : : /* Perform legitimizing attempts. This is intended to improve the
8184 : : situation when layout 0 is not valid which is a situation the cost
8185 : : based propagation does not handle well.
8186 : : Return true if further layout optimization is possible, false if
8187 : : the layout configuration should be considered final. */
8188 : :
8189 : : bool
8190 : 9721 : vect_optimize_slp_pass::legitimize ()
8191 : : {
8192 : : /* Perform a very simple legitimizing attempt by attempting to choose
8193 : : a single layout for all partitions that will make all permutations
8194 : : a noop. That should also be the optimal layout choice in case
8195 : : layout zero is legitimate.
8196 : : ??? Disconnected components of the SLP graph could have distinct
8197 : : single layouts. */
8198 : 9721 : int single_layout_i = -1;
8199 : 9721 : unsigned deferred_up_to = -1U;
8200 : 29465 : for (unsigned partition_i = 0; partition_i < m_partitions.length ();
8201 : : ++partition_i)
8202 : : {
8203 : 24956 : auto &partition = m_partitions[partition_i];
8204 : 24956 : if (single_layout_i == -1)
8205 : : {
8206 : 13014 : single_layout_i = partition.layout;
8207 : 13014 : deferred_up_to = partition_i;
8208 : : }
8209 : 11942 : else if (partition.layout == single_layout_i || partition.layout == -1)
8210 : : ;
8211 : : else
8212 : : single_layout_i = 0;
8213 : 21884 : if (single_layout_i == 0)
8214 : : return true;
8215 : :
8216 : 19804 : if (single_layout_i != -1
8217 : 19804 : && !is_compatible_layout (partition, single_layout_i))
8218 : : return true;
8219 : : }
8220 : :
8221 : 4509 : if (single_layout_i <= 0)
8222 : : return true;
8223 : :
8224 : 4625 : for (unsigned partition_i = 0; partition_i < deferred_up_to; ++partition_i)
8225 : 122 : if (!is_compatible_layout (m_partitions[partition_i],
8226 : : single_layout_i))
8227 : : return true;
8228 : :
8229 : 11401 : for (unsigned partition_i = 0; partition_i < m_partitions.length ();
8230 : : ++partition_i)
8231 : : {
8232 : 6898 : auto &partition = m_partitions[partition_i];
8233 : 6898 : partition.layout = single_layout_i;
8234 : : }
8235 : :
8236 : : return false;
8237 : : }
8238 : :
8239 : : /* Main entry point for the SLP graph optimization pass. */
8240 : :
8241 : : void
8242 : 632523 : vect_optimize_slp_pass::run ()
8243 : : {
8244 : 632523 : build_graph ();
8245 : 632523 : create_partitions ();
8246 : 632523 : start_choosing_layouts ();
8247 : 632523 : if (m_perms.length () > 1)
8248 : : {
8249 : 9721 : if (legitimize ())
8250 : : {
8251 : 5218 : forward_pass ();
8252 : 5218 : backward_pass ();
8253 : : }
8254 : 9721 : if (dump_enabled_p ())
8255 : 643 : dump ();
8256 : 9721 : materialize ();
8257 : 39309 : while (!m_perms.is_empty ())
8258 : 19867 : m_perms.pop ().release ();
8259 : : }
8260 : : else
8261 : 622802 : remove_redundant_permutations ();
8262 : 632523 : free_graph (m_slpg);
8263 : 632523 : build_graph ();
8264 : 632523 : decide_masked_load_lanes ();
8265 : 632523 : free_graph (m_slpg);
8266 : 632523 : }
8267 : :
8268 : : /* Apply CSE to NODE and its children using BST_MAP. */
8269 : :
8270 : : static void
8271 : 4836435 : vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node)
8272 : : {
8273 : 4836435 : bool put_p = false;
8274 : 4836435 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def
8275 : : /* Besides some VEC_PERM_EXPR, two-operator nodes also
8276 : : lack scalar stmts and thus CSE doesn't work via bst_map. Ideally
8277 : : we'd have sth that works for all internal and external nodes. */
8278 : 4836435 : && !SLP_TREE_SCALAR_STMTS (node).is_empty ())
8279 : : {
8280 : 3424850 : slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node));
8281 : 3424850 : if (leader)
8282 : : {
8283 : : /* We've visited this node already. */
8284 : 318580 : if (!*leader || *leader == node)
8285 : : return;
8286 : :
8287 : 2398 : if (dump_enabled_p ())
8288 : 756 : dump_printf_loc (MSG_NOTE, vect_location,
8289 : : "re-using SLP tree %p for %p\n",
8290 : : (void *)*leader, (void *)node);
8291 : 2398 : vect_free_slp_tree (node);
8292 : 2398 : (*leader)->refcnt += 1;
8293 : 2398 : node = *leader;
8294 : 2398 : return;
8295 : : }
8296 : :
8297 : : /* Avoid creating a cycle by populating the map only after recursion. */
8298 : 3106270 : bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), nullptr);
8299 : 3106270 : node->refcnt += 1;
8300 : 3106270 : put_p = true;
8301 : : /* And recurse. */
8302 : : }
8303 : :
8304 : 13352971 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
8305 : 3819922 : if (child)
8306 : 3480896 : vect_cse_slp_nodes (bst_map, child);
8307 : :
8308 : : /* Now record the node for CSE in other siblings. */
8309 : 4517855 : if (put_p)
8310 : 3106270 : *bst_map->get (SLP_TREE_SCALAR_STMTS (node)) = node;
8311 : : }
8312 : :
8313 : : /* Optimize the SLP graph of VINFO. */
8314 : :
8315 : : void
8316 : 991029 : vect_optimize_slp (vec_info *vinfo)
8317 : : {
8318 : 991029 : if (vinfo->slp_instances.is_empty ())
8319 : : return;
8320 : 632523 : vect_optimize_slp_pass (vinfo).run ();
8321 : :
8322 : : /* Apply CSE again to nodes after permute optimization. */
8323 : 632523 : scalar_stmts_to_slp_tree_map_t *bst_map
8324 : 632523 : = new scalar_stmts_to_slp_tree_map_t ();
8325 : :
8326 : 3253108 : for (auto inst : vinfo->slp_instances)
8327 : 1355539 : vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst));
8328 : :
8329 : 632523 : release_scalar_stmts_to_slp_tree_map (bst_map);
8330 : : }
8331 : :
8332 : : /* Gather loads reachable from the individual SLP graph entries. */
8333 : :
8334 : : void
8335 : 991029 : vect_gather_slp_loads (vec_info *vinfo)
8336 : : {
8337 : 991029 : unsigned i;
8338 : 991029 : slp_instance instance;
8339 : 2346568 : FOR_EACH_VEC_ELT (vinfo->slp_instances, i, instance)
8340 : : {
8341 : 1355539 : hash_set<slp_tree> visited;
8342 : 1355539 : vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance),
8343 : : SLP_INSTANCE_TREE (instance), visited);
8344 : 1355539 : }
8345 : 991029 : }
8346 : :
8347 : : /* For NODE update VF based on the number of lanes and the vector types
8348 : : used. */
8349 : :
8350 : : static void
8351 : 3473479 : vect_update_slp_vf_for_node (slp_tree node, poly_uint64 &vf,
8352 : : hash_set<slp_tree> &visited)
8353 : : {
8354 : 3473479 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
8355 : 1206848 : return;
8356 : 2540278 : if (visited.add (node))
8357 : : return;
8358 : :
8359 : 8548269 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8360 : 2849976 : vect_update_slp_vf_for_node (child, vf, visited);
8361 : :
8362 : : /* We do not visit SLP nodes for constants or externals - those neither
8363 : : have a vector type set yet (vectorizable_* does this) nor do they
8364 : : have max_nunits set. Instead we rely on internal nodes max_nunit
8365 : : to cover constant/external operands.
8366 : : Note that when we stop using fixed size vectors externs and constants
8367 : : shouldn't influence the (minimum) vectorization factor, instead
8368 : : vectorizable_* should honor the vectorization factor when trying to
8369 : : assign vector types to constants and externals and cause iteration
8370 : : to a higher vectorization factor when required. */
8371 : 2266631 : poly_uint64 node_vf
8372 : 2266631 : = calculate_unrolling_factor (node->max_nunits, SLP_TREE_LANES (node));
8373 : 2266631 : vf = force_common_multiple (vf, node_vf);
8374 : :
8375 : : /* For permute nodes that are fed from externs or constants we have to
8376 : : consider their number of lanes as well. Likewise for store-lanes. */
8377 : 2266631 : if (SLP_TREE_PERMUTE_P (node) || node->ldst_lanes)
8378 : 821472 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8379 : 215865 : if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
8380 : : {
8381 : 2742 : poly_uint64 child_vf
8382 : 2742 : = calculate_unrolling_factor (node->max_nunits,
8383 : : SLP_TREE_LANES (child));
8384 : 2742 : vf = force_common_multiple (vf, child_vf);
8385 : : }
8386 : : }
8387 : :
8388 : : /* For each possible SLP instance decide whether to SLP it and calculate overall
8389 : : unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
8390 : : least one instance. */
8391 : :
8392 : : bool
8393 : 392224 : vect_make_slp_decision (loop_vec_info loop_vinfo)
8394 : : {
8395 : 392224 : unsigned int i;
8396 : 392224 : poly_uint64 unrolling_factor = 1;
8397 : 392224 : const vec<slp_instance> &slp_instances
8398 : : = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
8399 : 392224 : slp_instance instance;
8400 : 392224 : int decided_to_slp = 0;
8401 : :
8402 : 392224 : DUMP_VECT_SCOPE ("vect_make_slp_decision");
8403 : :
8404 : 392224 : hash_set<slp_tree> visited;
8405 : 1015727 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
8406 : : {
8407 : 623503 : slp_tree root = SLP_INSTANCE_TREE (instance);
8408 : :
8409 : : /* All unroll factors have the form:
8410 : :
8411 : : GET_MODE_SIZE (vinfo->vector_mode) * X
8412 : :
8413 : : for some rational X, so they must have a common multiple. */
8414 : 623503 : vect_update_slp_vf_for_node (root, unrolling_factor, visited);
8415 : :
8416 : : /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
8417 : : call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
8418 : : loop-based vectorization. Such stmts will be marked as HYBRID. */
8419 : 623503 : vect_mark_slp_stmts (loop_vinfo, root);
8420 : :
8421 : : /* If all instances ended up with vector(1) T roots make sure to
8422 : : not vectorize. RVV for example relies on loop vectorization
8423 : : when some instances are essentially kept scalar. See PR121048. */
8424 : 623503 : if (SLP_TREE_VECTYPE (root)
8425 : 623503 : && known_gt (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (root)), 1U))
8426 : 519143 : decided_to_slp++;
8427 : : }
8428 : :
8429 : 392224 : LOOP_VINFO_VECT_FACTOR (loop_vinfo) = unrolling_factor;
8430 : :
8431 : 392224 : if (decided_to_slp && dump_enabled_p ())
8432 : : {
8433 : 17809 : dump_printf_loc (MSG_NOTE, vect_location,
8434 : : "Decided to SLP %d instances. Unrolling factor ",
8435 : : decided_to_slp);
8436 : 17809 : dump_dec (MSG_NOTE, unrolling_factor);
8437 : 17809 : dump_printf (MSG_NOTE, "\n");
8438 : : }
8439 : :
8440 : 392224 : return (decided_to_slp > 0);
8441 : 392224 : }
8442 : :
8443 : : /* Initialize a bb_vec_info struct for the statements in BBS basic blocks. */
8444 : :
8445 : 2391070 : _bb_vec_info::_bb_vec_info (vec<basic_block> _bbs, vec_info_shared *shared)
8446 : : : vec_info (vec_info::bb, shared),
8447 : 2391070 : roots (vNULL)
8448 : : {
8449 : : /* The region we are operating on. bbs[0] is the entry, excluding
8450 : : its PHI nodes. In the future we might want to track an explicit
8451 : : entry edge to cover bbs[0] PHI nodes and have a region entry
8452 : : insert location. */
8453 : 2391070 : bbs = _bbs.address ();
8454 : 2391070 : nbbs = _bbs.length ();
8455 : :
8456 : 18602572 : for (unsigned i = 0; i < nbbs; ++i)
8457 : : {
8458 : 16211502 : if (i != 0)
8459 : 21075236 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8460 : 7254804 : gsi_next (&si))
8461 : : {
8462 : 7254804 : gphi *phi = si.phi ();
8463 : 7254804 : gimple_set_uid (phi, 0);
8464 : 7254804 : add_stmt (phi);
8465 : : }
8466 : 32423004 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8467 : 136658820 : !gsi_end_p (gsi); gsi_next (&gsi))
8468 : : {
8469 : 120447318 : gimple *stmt = gsi_stmt (gsi);
8470 : 120447318 : gimple_set_uid (stmt, 0);
8471 : 120447318 : if (is_gimple_debug (stmt))
8472 : 73877042 : continue;
8473 : 46570276 : add_stmt (stmt);
8474 : : }
8475 : : }
8476 : 2391070 : }
8477 : :
8478 : :
8479 : : /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
8480 : : stmts in the basic block. */
8481 : :
8482 : 2391070 : _bb_vec_info::~_bb_vec_info ()
8483 : : {
8484 : : /* Reset region marker. */
8485 : 18602572 : for (unsigned i = 0; i < nbbs; ++i)
8486 : : {
8487 : 16211502 : if (i != 0)
8488 : 21090206 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
8489 : 7269774 : gsi_next (&si))
8490 : : {
8491 : 7269774 : gphi *phi = si.phi ();
8492 : 7269774 : gimple_set_uid (phi, -1);
8493 : : }
8494 : 32423004 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
8495 : 136583849 : !gsi_end_p (gsi); gsi_next (&gsi))
8496 : : {
8497 : 120372347 : gimple *stmt = gsi_stmt (gsi);
8498 : 120372347 : gimple_set_uid (stmt, -1);
8499 : : }
8500 : : }
8501 : :
8502 : 3621516 : for (unsigned i = 0; i < roots.length (); ++i)
8503 : : {
8504 : 1230446 : roots[i].stmts.release ();
8505 : 1230446 : roots[i].roots.release ();
8506 : 1230446 : roots[i].remain.release ();
8507 : : }
8508 : 2391070 : roots.release ();
8509 : 2391070 : }
8510 : :
8511 : : /* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
8512 : : given then that child nodes have already been processed, and that
8513 : : their def types currently match their SLP node's def type. */
8514 : :
8515 : : static bool
8516 : 2413057 : vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
8517 : : slp_instance node_instance,
8518 : : stmt_vector_for_cost *cost_vec)
8519 : : {
8520 : : /* Handle purely internal nodes. */
8521 : 2413057 : if (SLP_TREE_PERMUTE_P (node))
8522 : : {
8523 : 98956 : if (!vectorizable_slp_permutation (vinfo, NULL, node, cost_vec))
8524 : : return false;
8525 : :
8526 : : stmt_vec_info slp_stmt_info;
8527 : : unsigned int i;
8528 : 255928 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
8529 : : {
8530 : 158198 : if (slp_stmt_info
8531 : 153358 : && STMT_VINFO_LIVE_P (slp_stmt_info)
8532 : 158216 : && !vectorizable_live_operation (vinfo, slp_stmt_info, node,
8533 : : node_instance, i,
8534 : : false, cost_vec))
8535 : : return false;
8536 : : }
8537 : 97730 : SLP_TREE_TYPE (node) = permute_info_type;
8538 : 97730 : return true;
8539 : : }
8540 : :
8541 : 2314101 : return vect_analyze_stmt (vinfo, node, node_instance, cost_vec);
8542 : : }
8543 : :
8544 : : static int
8545 : 1851158 : sort_ints (const void *a_, const void *b_)
8546 : : {
8547 : 1851158 : int a = *(const int *)a_;
8548 : 1851158 : int b = *(const int *)b_;
8549 : 1851158 : return a - b;
8550 : : }
8551 : :
8552 : : /* Verify if we can externalize a set of internal defs. */
8553 : :
8554 : : static bool
8555 : 391644 : vect_slp_can_convert_to_external (const vec<stmt_vec_info> &stmts)
8556 : : {
8557 : : /* Constant generation uses get_later_stmt which can only handle
8558 : : defs from the same BB or a set of defs that can be ordered
8559 : : with a dominance query. */
8560 : 391644 : basic_block bb = NULL;
8561 : 391644 : bool all_same = true;
8562 : 391644 : auto_vec<int> bbs;
8563 : 783288 : bbs.reserve_exact (stmts.length ());
8564 : 2115072 : for (stmt_vec_info stmt : stmts)
8565 : : {
8566 : 940140 : if (!stmt)
8567 : : return false;
8568 : 940140 : else if (!bb)
8569 : 391644 : bb = gimple_bb (stmt->stmt);
8570 : 548496 : else if (gimple_bb (stmt->stmt) != bb)
8571 : 172913 : all_same = false;
8572 : 940140 : bbs.quick_push (gimple_bb (stmt->stmt)->index);
8573 : : }
8574 : 391644 : if (all_same)
8575 : : return true;
8576 : :
8577 : : /* Produce a vector of unique BB indexes for the defs. */
8578 : 129370 : bbs.qsort (sort_ints);
8579 : : unsigned i, j;
8580 : 315544 : for (i = 1, j = 1; i < bbs.length (); ++i)
8581 : 186174 : if (bbs[i] != bbs[j-1])
8582 : 138195 : bbs[j++] = bbs[i];
8583 : 129370 : gcc_assert (j >= 2);
8584 : 129370 : bbs.truncate (j);
8585 : :
8586 : 258740 : if (bbs.length () == 2)
8587 : 125896 : return (dominated_by_p (CDI_DOMINATORS,
8588 : 125896 : BASIC_BLOCK_FOR_FN (cfun, bbs[0]),
8589 : 125896 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]))
8590 : 241332 : || dominated_by_p (CDI_DOMINATORS,
8591 : 115436 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]),
8592 : 115436 : BASIC_BLOCK_FOR_FN (cfun, bbs[0])));
8593 : :
8594 : : /* ??? For more than two BBs we can sort the vector and verify the
8595 : : result is a total order. But we can't use vec::qsort with a
8596 : : compare function using a dominance query since there's no way to
8597 : : signal failure and any fallback for an unordered pair would
8598 : : fail qsort_chk later.
8599 : : For now simply hope that ordering after BB index provides the
8600 : : best candidate total order. If required we can implement our
8601 : : own mergesort or export an entry without checking. */
8602 : 407326 : for (unsigned i = 1; i < bbs.length (); ++i)
8603 : 12273 : if (!dominated_by_p (CDI_DOMINATORS,
8604 : 12273 : BASIC_BLOCK_FOR_FN (cfun, bbs[i]),
8605 : 12273 : BASIC_BLOCK_FOR_FN (cfun, bbs[i-1])))
8606 : : return false;
8607 : :
8608 : : return true;
8609 : 391644 : }
8610 : :
8611 : : /* Try to build NODE from scalars, returning true on success.
8612 : : NODE_INSTANCE is the SLP instance that contains NODE. */
8613 : :
8614 : : static bool
8615 : 545481 : vect_slp_convert_to_external (vec_info *vinfo, slp_tree node,
8616 : : slp_instance node_instance)
8617 : : {
8618 : 545481 : stmt_vec_info stmt_info;
8619 : 545481 : unsigned int i;
8620 : :
8621 : 545481 : if (!is_a <bb_vec_info> (vinfo)
8622 : 74568 : || node == SLP_INSTANCE_TREE (node_instance)
8623 : 22393 : || !SLP_TREE_SCALAR_STMTS (node).exists ()
8624 : 22349 : || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node))
8625 : : /* Force the mask use to be built from scalars instead. */
8626 : 20156 : || VECTOR_BOOLEAN_TYPE_P (SLP_TREE_VECTYPE (node))
8627 : 565455 : || !vect_slp_can_convert_to_external (SLP_TREE_SCALAR_STMTS (node)))
8628 : 525507 : return false;
8629 : :
8630 : 19974 : if (dump_enabled_p ())
8631 : 73 : dump_printf_loc (MSG_NOTE, vect_location,
8632 : : "Building vector operands of %p from scalars instead\n",
8633 : : (void *) node);
8634 : :
8635 : : /* Don't remove and free the child nodes here, since they could be
8636 : : referenced by other structures. The analysis and scheduling phases
8637 : : (need to) ignore child nodes of anything that isn't vect_internal_def. */
8638 : 19974 : unsigned int group_size = SLP_TREE_LANES (node);
8639 : 19974 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
8640 : : /* Invariants get their vector type from the uses. */
8641 : 19974 : SLP_TREE_VECTYPE (node) = NULL_TREE;
8642 : 19974 : SLP_TREE_SCALAR_OPS (node).safe_grow (group_size, true);
8643 : 19974 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8644 : 69948 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8645 : : {
8646 : 49974 : tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
8647 : 49974 : SLP_TREE_SCALAR_OPS (node)[i] = lhs;
8648 : : }
8649 : : return true;
8650 : : }
8651 : :
8652 : : /* Return true if all elements of the slice are the same. */
8653 : : bool
8654 : 463551 : vect_scalar_ops_slice::all_same_p () const
8655 : : {
8656 : 513197 : for (unsigned int i = 1; i < length; ++i)
8657 : 435576 : if (!operand_equal_p (op (0), op (i)))
8658 : : return false;
8659 : : return true;
8660 : : }
8661 : :
8662 : : hashval_t
8663 : 397757 : vect_scalar_ops_slice_hash::hash (const value_type &s)
8664 : : {
8665 : 397757 : hashval_t hash = 0;
8666 : 1532329 : for (unsigned i = 0; i < s.length; ++i)
8667 : 1134572 : hash = iterative_hash_expr (s.op (i), hash);
8668 : 397757 : return hash;
8669 : : }
8670 : :
8671 : : bool
8672 : 215815 : vect_scalar_ops_slice_hash::equal (const value_type &s1,
8673 : : const compare_type &s2)
8674 : : {
8675 : 215815 : if (s1.length != s2.length)
8676 : : return false;
8677 : 375135 : for (unsigned i = 0; i < s1.length; ++i)
8678 : 327296 : if (!operand_equal_p (s1.op (i), s2.op (i)))
8679 : : return false;
8680 : : return true;
8681 : : }
8682 : :
8683 : : /* Compute the prologue cost for invariant or constant operands represented
8684 : : by NODE. */
8685 : :
8686 : : static void
8687 : 1031878 : vect_prologue_cost_for_slp (vec_info *vinfo, slp_tree node,
8688 : : stmt_vector_for_cost *cost_vec)
8689 : : {
8690 : : /* There's a special case of an existing vector, that costs nothing. */
8691 : 1031878 : if (SLP_TREE_SCALAR_OPS (node).length () == 0
8692 : 1031878 : && !SLP_TREE_VEC_DEFS (node).is_empty ())
8693 : 1510 : return;
8694 : : /* Without looking at the actual initializer a vector of
8695 : : constants can be implemented as load from the constant pool.
8696 : : When all elements are the same we can use a splat. */
8697 : 1030368 : tree vectype = SLP_TREE_VECTYPE (node);
8698 : 1030368 : unsigned group_size = SLP_TREE_SCALAR_OPS (node).length ();
8699 : 1030368 : unsigned HOST_WIDE_INT const_nunits;
8700 : 1030368 : unsigned nelt_limit;
8701 : 1030368 : unsigned nvectors = vect_get_num_copies (vinfo, node);
8702 : 1030368 : auto ops = &SLP_TREE_SCALAR_OPS (node);
8703 : 1030368 : auto_vec<unsigned int> starts (nvectors);
8704 : 1030368 : if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
8705 : 1030368 : && ! multiple_p (const_nunits, group_size))
8706 : : {
8707 : 63770 : nelt_limit = const_nunits;
8708 : 63770 : hash_set<vect_scalar_ops_slice_hash> vector_ops;
8709 : 263459 : for (unsigned int i = 0; i < nvectors; ++i)
8710 : 199689 : if (!vector_ops.add ({ ops, i * nelt_limit, nelt_limit }))
8711 : 151850 : starts.quick_push (i * nelt_limit);
8712 : 63770 : }
8713 : : else
8714 : : {
8715 : : /* If either the vector has variable length or the vectors
8716 : : are composed of repeated whole groups we only need to
8717 : : cost construction once. All vectors will be the same. */
8718 : 966598 : nelt_limit = group_size;
8719 : 966598 : starts.quick_push (0);
8720 : : }
8721 : : /* ??? We're just tracking whether vectors in a single node are the same.
8722 : : Ideally we'd do something more global. */
8723 : 1030368 : bool passed = false;
8724 : 4209552 : for (unsigned int start : starts)
8725 : : {
8726 : 1118448 : vect_cost_for_stmt kind;
8727 : 1118448 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
8728 : : kind = vector_load;
8729 : 463551 : else if (vect_scalar_ops_slice { ops, start, nelt_limit }.all_same_p ())
8730 : : kind = scalar_to_vec;
8731 : : else
8732 : 385930 : kind = vec_construct;
8733 : : /* The target cost hook has no idea which part of the SLP node
8734 : : we are costing so avoid passing it down more than once. Pass
8735 : : it to the first vec_construct or scalar_to_vec part since for those
8736 : : the x86 backend tries to account for GPR to XMM register moves. */
8737 : 1118448 : record_stmt_cost (cost_vec, 1, kind, nullptr,
8738 : 1118448 : (kind != vector_load && !passed) ? node : nullptr,
8739 : : vectype, 0, vect_prologue);
8740 : 1118448 : if (kind != vector_load)
8741 : 463551 : passed = true;
8742 : : }
8743 : 1030368 : }
8744 : :
8745 : : /* Analyze statements contained in SLP tree NODE after recursively analyzing
8746 : : the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
8747 : :
8748 : : Return true if the operations are supported. */
8749 : :
8750 : : static bool
8751 : 4495615 : vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
8752 : : slp_instance node_instance,
8753 : : hash_set<slp_tree> &visited_set,
8754 : : vec<slp_tree> &visited_vec,
8755 : : stmt_vector_for_cost *cost_vec)
8756 : : {
8757 : 4495615 : int i, j;
8758 : 4495615 : slp_tree child;
8759 : :
8760 : : /* Assume we can code-generate all invariants. */
8761 : 4495615 : if (!node
8762 : 4208182 : || SLP_TREE_DEF_TYPE (node) == vect_constant_def
8763 : 3507568 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
8764 : : return true;
8765 : :
8766 : 2988128 : if (SLP_TREE_DEF_TYPE (node) == vect_uninitialized_def)
8767 : : {
8768 : 10 : if (dump_enabled_p ())
8769 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
8770 : : "Failed cyclic SLP reference in %p\n", (void *) node);
8771 : 10 : return false;
8772 : : }
8773 : 2988118 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_internal_def);
8774 : :
8775 : : /* If we already analyzed the exact same set of scalar stmts we're done.
8776 : : We share the generated vector stmts for those. */
8777 : 2988118 : if (visited_set.add (node))
8778 : : return true;
8779 : 2724858 : visited_vec.safe_push (node);
8780 : :
8781 : 2724858 : bool res = true;
8782 : 2724858 : unsigned visited_rec_start = visited_vec.length ();
8783 : 2724858 : unsigned cost_vec_rec_start = cost_vec->length ();
8784 : 2724858 : bool seen_non_constant_child = false;
8785 : 5692810 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8786 : : {
8787 : 3279606 : res = vect_slp_analyze_node_operations (vinfo, child, node_instance,
8788 : : visited_set, visited_vec,
8789 : : cost_vec);
8790 : 3279606 : if (!res)
8791 : : break;
8792 : 2967952 : if (child && SLP_TREE_DEF_TYPE (child) != vect_constant_def)
8793 : 2967952 : seen_non_constant_child = true;
8794 : : }
8795 : : /* We're having difficulties scheduling nodes with just constant
8796 : : operands and no scalar stmts since we then cannot compute a stmt
8797 : : insertion place. */
8798 : 2724858 : if (res
8799 : 2724858 : && !seen_non_constant_child
8800 : 2724858 : && SLP_TREE_SCALAR_STMTS (node).is_empty ())
8801 : : {
8802 : 147 : if (dump_enabled_p ())
8803 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
8804 : : "Cannot vectorize all-constant op node %p\n",
8805 : : (void *) node);
8806 : : res = false;
8807 : : }
8808 : :
8809 : 2724711 : if (res)
8810 : 2413057 : res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
8811 : : cost_vec);
8812 : : /* If analysis failed we have to pop all recursive visited nodes
8813 : : plus ourselves. */
8814 : 2724858 : if (!res)
8815 : : {
8816 : 2670136 : while (visited_vec.length () >= visited_rec_start)
8817 : 789587 : visited_set.remove (visited_vec.pop ());
8818 : 545481 : cost_vec->truncate (cost_vec_rec_start);
8819 : : }
8820 : :
8821 : : /* When the node can be vectorized cost invariant nodes it references.
8822 : : This is not done in DFS order to allow the refering node
8823 : : vectorizable_* calls to nail down the invariant nodes vector type
8824 : : and possibly unshare it if it needs a different vector type than
8825 : : other referrers. */
8826 : 2724858 : if (res)
8827 : 4855480 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
8828 : 2676103 : if (child
8829 : 2445700 : && (SLP_TREE_DEF_TYPE (child) == vect_constant_def
8830 : 2445700 : || SLP_TREE_DEF_TYPE (child) == vect_external_def)
8831 : : /* Perform usual caching, note code-generation still
8832 : : code-gens these nodes multiple times but we expect
8833 : : to CSE them later. */
8834 : 3769470 : && !visited_set.add (child))
8835 : : {
8836 : 1068844 : visited_vec.safe_push (child);
8837 : : /* ??? After auditing more code paths make a "default"
8838 : : and push the vector type from NODE to all children
8839 : : if it is not already set. */
8840 : : /* Compute the number of vectors to be generated. */
8841 : 1068844 : tree vector_type = SLP_TREE_VECTYPE (child);
8842 : 1068844 : if (!vector_type)
8843 : : {
8844 : : /* Masked loads can have an undefined (default SSA definition)
8845 : : else operand. We do not need to cost it. */
8846 : 36966 : vec<tree> ops = SLP_TREE_SCALAR_OPS (child);
8847 : 38001 : if (SLP_TREE_TYPE (node) == load_vec_info_type
8848 : 38001 : && ((ops.length ()
8849 : 1035 : && TREE_CODE (ops[0]) == SSA_NAME
8850 : 0 : && SSA_NAME_IS_DEFAULT_DEF (ops[0])
8851 : 0 : && VAR_P (SSA_NAME_VAR (ops[0])))
8852 : 1035 : || SLP_TREE_DEF_TYPE (child) == vect_constant_def))
8853 : 1035 : continue;
8854 : :
8855 : : /* For shifts with a scalar argument we don't need
8856 : : to cost or code-generate anything.
8857 : : ??? Represent this more explicitely. */
8858 : 35931 : gcc_assert (SLP_TREE_TYPE (node) == shift_vec_info_type
8859 : : && j == 1);
8860 : 35931 : continue;
8861 : 35931 : }
8862 : :
8863 : : /* And cost them. */
8864 : 1031878 : vect_prologue_cost_for_slp (vinfo, child, cost_vec);
8865 : : }
8866 : :
8867 : : /* If this node or any of its children can't be vectorized, try pruning
8868 : : the tree here rather than felling the whole thing. */
8869 : 545481 : if (!res && vect_slp_convert_to_external (vinfo, node, node_instance))
8870 : : {
8871 : : /* We'll need to revisit this for invariant costing and number
8872 : : of vectorized stmt setting. */
8873 : : res = true;
8874 : : }
8875 : :
8876 : : return res;
8877 : : }
8878 : :
8879 : : /* Given a definition DEF, analyze if it will have any live scalar use after
8880 : : performing SLP vectorization whose information is represented by BB_VINFO,
8881 : : and record result into hash map SCALAR_USE_MAP as cache for later fast
8882 : : check. If recursion DEPTH exceeds a limit, stop analysis and make a
8883 : : conservative assumption. Return 0 if no scalar use, 1 if there is, -1
8884 : : means recursion is limited. */
8885 : :
8886 : : static int
8887 : 546436 : vec_slp_has_scalar_use (bb_vec_info bb_vinfo, tree def,
8888 : : hash_map<tree, int> &scalar_use_map,
8889 : : int depth = 0)
8890 : : {
8891 : 546436 : const int depth_limit = 2;
8892 : 546436 : imm_use_iterator use_iter;
8893 : 546436 : gimple *use_stmt;
8894 : :
8895 : 546436 : if (int *res = scalar_use_map.get (def))
8896 : 21905 : return *res;
8897 : :
8898 : 524531 : int scalar_use = 1;
8899 : :
8900 : 1721374 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
8901 : : {
8902 : 783967 : if (is_gimple_debug (use_stmt))
8903 : 169527 : continue;
8904 : :
8905 : 614440 : stmt_vec_info use_stmt_info = bb_vinfo->lookup_stmt (use_stmt);
8906 : :
8907 : 614440 : if (!use_stmt_info)
8908 : : break;
8909 : :
8910 : 616806 : if (PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
8911 : 500709 : continue;
8912 : :
8913 : : /* Do not step forward when encounter PHI statement, since it may
8914 : : involve cyclic reference and cause infinite recursive invocation. */
8915 : 107443 : if (gimple_code (use_stmt) == GIMPLE_PHI)
8916 : : break;
8917 : :
8918 : : /* When pattern recognition is involved, a statement whose definition is
8919 : : consumed in some pattern, may not be included in the final replacement
8920 : : pattern statements, so would be skipped when building SLP graph.
8921 : :
8922 : : * Original
8923 : : char a_c = *(char *) a;
8924 : : char b_c = *(char *) b;
8925 : : unsigned short a_s = (unsigned short) a_c;
8926 : : int a_i = (int) a_s;
8927 : : int b_i = (int) b_c;
8928 : : int r_i = a_i - b_i;
8929 : :
8930 : : * After pattern replacement
8931 : : a_s = (unsigned short) a_c;
8932 : : a_i = (int) a_s;
8933 : :
8934 : : patt_b_s = (unsigned short) b_c; // b_i = (int) b_c
8935 : : patt_b_i = (int) patt_b_s; // b_i = (int) b_c
8936 : :
8937 : : patt_r_s = widen_minus(a_c, b_c); // r_i = a_i - b_i
8938 : : patt_r_i = (int) patt_r_s; // r_i = a_i - b_i
8939 : :
8940 : : The definitions of a_i(original statement) and b_i(pattern statement)
8941 : : are related to, but actually not part of widen_minus pattern.
8942 : : Vectorizing the pattern does not cause these definition statements to
8943 : : be marked as PURE_SLP. For this case, we need to recursively check
8944 : : whether their uses are all absorbed into vectorized code. But there
8945 : : is an exception that some use may participate in an vectorized
8946 : : operation via an external SLP node containing that use as an element.
8947 : : The parameter "scalar_use_map" tags such kind of SSA as having scalar
8948 : : use in advance. */
8949 : 89093 : tree lhs = gimple_get_lhs (use_stmt);
8950 : :
8951 : 89093 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
8952 : : break;
8953 : :
8954 : 56839 : if (depth_limit && depth >= depth_limit)
8955 : 10878 : return -1;
8956 : :
8957 : 45961 : if ((scalar_use = vec_slp_has_scalar_use (bb_vinfo, lhs, scalar_use_map,
8958 : : depth + 1)))
8959 : : break;
8960 : 10878 : }
8961 : :
8962 : 513653 : if (end_imm_use_stmt_p (&use_iter))
8963 : 412876 : scalar_use = 0;
8964 : :
8965 : : /* If recursion is limited, do not cache result for non-root defs. */
8966 : 513653 : if (!depth || scalar_use >= 0)
8967 : : {
8968 : 502775 : bool added = scalar_use_map.put (def, scalar_use);
8969 : 502775 : gcc_assert (!added);
8970 : : }
8971 : :
8972 : 513653 : return scalar_use;
8973 : : }
8974 : :
8975 : : /* Mark lanes of NODE that are live outside of the basic-block vectorized
8976 : : region and that can be vectorized using vectorizable_live_operation
8977 : : with STMT_VINFO_LIVE_P. Not handled live operations will cause the
8978 : : scalar code computing it to be retained. */
8979 : :
8980 : : static void
8981 : 907657 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo, slp_tree node,
8982 : : slp_instance instance,
8983 : : stmt_vector_for_cost *cost_vec,
8984 : : hash_map<tree, int> &scalar_use_map,
8985 : : hash_set<stmt_vec_info> &svisited,
8986 : : hash_set<slp_tree> &visited)
8987 : : {
8988 : 907657 : if (visited.add (node))
8989 : 38475 : return;
8990 : :
8991 : 869182 : unsigned i;
8992 : 869182 : stmt_vec_info stmt_info;
8993 : 869182 : stmt_vec_info last_stmt = vect_find_last_scalar_stmt_in_slp (node);
8994 : 3148762 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8995 : : {
8996 : 2279580 : if (!stmt_info || svisited.contains (stmt_info))
8997 : 30051 : continue;
8998 : 2258082 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
8999 : 2258082 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info)
9000 : 11471 : && STMT_VINFO_RELATED_STMT (orig_stmt_info) != stmt_info)
9001 : : /* Only the pattern root stmt computes the original scalar value. */
9002 : 8553 : continue;
9003 : 2249529 : bool mark_visited = true;
9004 : 2249529 : gimple *orig_stmt = orig_stmt_info->stmt;
9005 : 2249529 : ssa_op_iter op_iter;
9006 : 2249529 : def_operand_p def_p;
9007 : 4999533 : FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_stmt, op_iter, SSA_OP_DEF)
9008 : : {
9009 : 500475 : if (vec_slp_has_scalar_use (bb_vinfo, DEF_FROM_PTR (def_p),
9010 : : scalar_use_map))
9011 : : {
9012 : 89551 : STMT_VINFO_LIVE_P (stmt_info) = true;
9013 : 89551 : if (vectorizable_live_operation (bb_vinfo, stmt_info, node,
9014 : : instance, i, false, cost_vec))
9015 : : /* ??? So we know we can vectorize the live stmt from one SLP
9016 : : node. If we cannot do so from all or none consistently
9017 : : we'd have to record which SLP node (and lane) we want to
9018 : : use for the live operation. So make sure we can
9019 : : code-generate from all nodes. */
9020 : : mark_visited = false;
9021 : : else
9022 : 0 : STMT_VINFO_LIVE_P (stmt_info) = false;
9023 : : }
9024 : :
9025 : : /* We have to verify whether we can insert the lane extract
9026 : : before all uses. The following is a conservative approximation.
9027 : : We cannot put this into vectorizable_live_operation because
9028 : : iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
9029 : : doesn't work.
9030 : : Note that while the fact that we emit code for loads at the
9031 : : first load should make this a non-problem leafs we construct
9032 : : from scalars are vectorized after the last scalar def.
9033 : : ??? If we'd actually compute the insert location during
9034 : : analysis we could use sth less conservative than the last
9035 : : scalar stmt in the node for the dominance check. */
9036 : : /* ??? What remains is "live" uses in vector CTORs in the same
9037 : : SLP graph which is where those uses can end up code-generated
9038 : : right after their definition instead of close to their original
9039 : : use. But that would restrict us to code-generate lane-extracts
9040 : : from the latest stmt in a node. So we compensate for this
9041 : : during code-generation, simply not replacing uses for those
9042 : : hopefully rare cases. */
9043 : 500475 : imm_use_iterator use_iter;
9044 : 500475 : gimple *use_stmt;
9045 : 500475 : stmt_vec_info use_stmt_info;
9046 : :
9047 : 500475 : if (STMT_VINFO_LIVE_P (stmt_info))
9048 : 607124 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
9049 : 428022 : if (!is_gimple_debug (use_stmt)
9050 : 318273 : && (!(use_stmt_info = bb_vinfo->lookup_stmt (use_stmt))
9051 : 307853 : || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
9052 : 605715 : && !vect_stmt_dominates_stmt_p (last_stmt->stmt, use_stmt))
9053 : : {
9054 : 16820 : if (dump_enabled_p ())
9055 : 282 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9056 : : "Cannot determine insertion place for "
9057 : : "lane extract\n");
9058 : 16820 : STMT_VINFO_LIVE_P (stmt_info) = false;
9059 : 16820 : mark_visited = true;
9060 : 89551 : }
9061 : : }
9062 : 2249529 : if (mark_visited)
9063 : 2173765 : svisited.add (stmt_info);
9064 : : }
9065 : :
9066 : : slp_tree child;
9067 : 2514021 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9068 : 878000 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9069 : 224538 : vect_bb_slp_mark_live_stmts (bb_vinfo, child, instance, cost_vec,
9070 : : scalar_use_map, svisited, visited);
9071 : : }
9072 : :
9073 : : /* Traverse all slp instances of BB_VINFO, and mark lanes of every node that
9074 : : are live outside of the basic-block vectorized region and that can be
9075 : : vectorized using vectorizable_live_operation with STMT_VINFO_LIVE_P. */
9076 : :
9077 : : static void
9078 : 284062 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo)
9079 : : {
9080 : 284062 : if (bb_vinfo->slp_instances.is_empty ())
9081 : 32428 : return;
9082 : :
9083 : 251634 : hash_set<stmt_vec_info> svisited;
9084 : 251634 : hash_set<slp_tree> visited;
9085 : 251634 : hash_map<tree, int> scalar_use_map;
9086 : 251634 : auto_vec<slp_tree> worklist;
9087 : :
9088 : 1438021 : for (slp_instance instance : bb_vinfo->slp_instances)
9089 : : {
9090 : 683119 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc)
9091 : 57641 : for (tree op : SLP_INSTANCE_REMAIN_DEFS (instance))
9092 : 16553 : if (TREE_CODE (op) == SSA_NAME)
9093 : 13979 : scalar_use_map.put (op, 1);
9094 : 683119 : if (!visited.add (SLP_INSTANCE_TREE (instance)))
9095 : 681183 : worklist.safe_push (SLP_INSTANCE_TREE (instance));
9096 : : }
9097 : :
9098 : 1521403 : do
9099 : : {
9100 : 1521403 : slp_tree node = worklist.pop ();
9101 : :
9102 : 1521403 : if (SLP_TREE_DEF_TYPE (node) == vect_external_def)
9103 : : {
9104 : 1602115 : for (tree op : SLP_TREE_SCALAR_OPS (node))
9105 : 708614 : if (TREE_CODE (op) == SSA_NAME)
9106 : 477053 : scalar_use_map.put (op, 1);
9107 : : }
9108 : : else
9109 : : {
9110 : 3634718 : for (slp_tree child : SLP_TREE_CHILDREN (node))
9111 : 877976 : if (child && !visited.add (child))
9112 : 840220 : worklist.safe_push (child);
9113 : : }
9114 : : }
9115 : 3042806 : while (!worklist.is_empty ());
9116 : :
9117 : 251634 : visited.empty ();
9118 : :
9119 : 1438021 : for (slp_instance instance : bb_vinfo->slp_instances)
9120 : : {
9121 : 683119 : vect_location = instance->location ();
9122 : 683119 : vect_bb_slp_mark_live_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance),
9123 : : instance, &instance->cost_vec,
9124 : : scalar_use_map, svisited, visited);
9125 : : }
9126 : 251634 : }
9127 : :
9128 : : /* Determine whether we can vectorize the reduction epilogue for INSTANCE. */
9129 : :
9130 : : static bool
9131 : 76098 : vectorizable_bb_reduc_epilogue (slp_instance instance,
9132 : : stmt_vector_for_cost *cost_vec)
9133 : : {
9134 : 76098 : gassign *stmt = as_a <gassign *> (instance->root_stmts[0]->stmt);
9135 : 76098 : enum tree_code reduc_code = gimple_assign_rhs_code (stmt);
9136 : 76098 : if (reduc_code == MINUS_EXPR)
9137 : 0 : reduc_code = PLUS_EXPR;
9138 : 76098 : internal_fn reduc_fn;
9139 : 76098 : tree vectype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance));
9140 : 76098 : if (!vectype
9141 : 76086 : || !reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
9142 : 76086 : || reduc_fn == IFN_LAST
9143 : 76086 : || !direct_internal_fn_supported_p (reduc_fn, vectype, OPTIMIZE_FOR_BOTH)
9144 : 112522 : || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
9145 : 36424 : TREE_TYPE (vectype)))
9146 : : {
9147 : 51953 : if (dump_enabled_p ())
9148 : 271 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9149 : : "not vectorized: basic block reduction epilogue "
9150 : : "operation unsupported.\n");
9151 : 51953 : return false;
9152 : : }
9153 : :
9154 : : /* There's no way to cost a horizontal vector reduction via REDUC_FN so
9155 : : cost log2 vector operations plus shuffles and one extraction. */
9156 : 24145 : unsigned steps = floor_log2 (vect_nunits_for_cost (vectype));
9157 : 24145 : record_stmt_cost (cost_vec, steps, vector_stmt, instance->root_stmts[0],
9158 : : vectype, 0, vect_body);
9159 : 24145 : record_stmt_cost (cost_vec, steps, vec_perm, instance->root_stmts[0],
9160 : : vectype, 0, vect_body);
9161 : 24145 : record_stmt_cost (cost_vec, 1, vec_to_scalar, instance->root_stmts[0],
9162 : : vectype, 0, vect_body);
9163 : :
9164 : : /* Since we replace all stmts of a possibly longer scalar reduction
9165 : : chain account for the extra scalar stmts for that. */
9166 : 24145 : if (!instance->remain_defs.is_empty ())
9167 : 19368 : record_stmt_cost (cost_vec, instance->remain_defs.length (), scalar_stmt,
9168 : 9684 : instance->root_stmts[0], 0, vect_body);
9169 : : return true;
9170 : : }
9171 : :
9172 : : /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
9173 : : and recurse to children. */
9174 : :
9175 : : static void
9176 : 177869 : vect_slp_prune_covered_roots (slp_tree node, hash_set<stmt_vec_info> &roots,
9177 : : hash_set<slp_tree> &visited)
9178 : : {
9179 : 177869 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
9180 : 177869 : || visited.add (node))
9181 : 78938 : return;
9182 : :
9183 : : stmt_vec_info stmt;
9184 : : unsigned i;
9185 : 336201 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
9186 : 237270 : if (stmt)
9187 : 240775 : roots.remove (vect_orig_stmt (stmt));
9188 : :
9189 : : slp_tree child;
9190 : 220190 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9191 : 121259 : if (child)
9192 : 120071 : vect_slp_prune_covered_roots (child, roots, visited);
9193 : : }
9194 : :
9195 : : /* Analyze statements in SLP instances of VINFO. Return true if the
9196 : : operations are supported. */
9197 : :
9198 : : bool
9199 : 613915 : vect_slp_analyze_operations (vec_info *vinfo)
9200 : : {
9201 : 613915 : slp_instance instance;
9202 : 613915 : int i;
9203 : :
9204 : 613915 : DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
9205 : :
9206 : 613915 : hash_set<slp_tree> visited;
9207 : 1613464 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
9208 : : {
9209 : 1216009 : auto_vec<slp_tree> visited_vec;
9210 : 1216009 : stmt_vector_for_cost cost_vec;
9211 : 1216009 : cost_vec.create (2);
9212 : 1216009 : if (is_a <bb_vec_info> (vinfo))
9213 : 788769 : vect_location = instance->location ();
9214 : 1216009 : if (!vect_slp_analyze_node_operations (vinfo,
9215 : : SLP_INSTANCE_TREE (instance),
9216 : : instance, visited, visited_vec,
9217 : : &cost_vec)
9218 : : /* CTOR instances require vectorized defs for the SLP tree root. */
9219 : 1002146 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor
9220 : 4856 : && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance))
9221 : : != vect_internal_def
9222 : : /* Make sure we vectorized with the expected type. */
9223 : 4852 : || !useless_type_conversion_p
9224 : 4852 : (TREE_TYPE (TREE_TYPE (gimple_assign_rhs1
9225 : : (instance->root_stmts[0]->stmt))),
9226 : 4852 : TREE_TYPE (SLP_TREE_VECTYPE
9227 : : (SLP_INSTANCE_TREE (instance))))))
9228 : : /* Check we can vectorize the reduction. */
9229 : 1002127 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
9230 : 76098 : && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
9231 : : /* Check we can vectorize the gcond. */
9232 : 2166183 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
9233 : 56760 : && !vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
9234 : 56760 : SLP_INSTANCE_ROOT_STMTS (instance)[0],
9235 : : NULL,
9236 : : SLP_INSTANCE_TREE (instance),
9237 : : &cost_vec)))
9238 : : {
9239 : 320607 : cost_vec.release ();
9240 : 320607 : slp_tree node = SLP_INSTANCE_TREE (instance);
9241 : 320607 : stmt_vec_info stmt_info;
9242 : 320607 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9243 : 252143 : stmt_info = SLP_INSTANCE_ROOT_STMTS (instance)[0];
9244 : 68464 : else if (!SLP_TREE_SCALAR_STMTS (node).is_empty ()
9245 : 68464 : && SLP_TREE_SCALAR_STMTS (node)[0])
9246 : : stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
9247 : : else
9248 : 0 : stmt_info = SLP_TREE_REPRESENTATIVE (node);
9249 : 320607 : if (is_a <loop_vec_info> (vinfo))
9250 : : {
9251 : 216460 : if (dump_enabled_p ())
9252 : 6197 : dump_printf_loc (MSG_NOTE, vect_location,
9253 : : "unsupported SLP instance starting from: %G",
9254 : : stmt_info->stmt);
9255 : 216460 : return false;
9256 : : }
9257 : 104147 : if (dump_enabled_p ())
9258 : 319 : dump_printf_loc (MSG_NOTE, vect_location,
9259 : : "removing SLP instance operations starting from: %G",
9260 : : stmt_info->stmt);
9261 : 104147 : vect_free_slp_instance (instance);
9262 : 104147 : vinfo->slp_instances.ordered_remove (i);
9263 : 1554593 : while (!visited_vec.is_empty ())
9264 : 346797 : visited.remove (visited_vec.pop ());
9265 : : }
9266 : : else
9267 : : {
9268 : 895402 : i++;
9269 : 895402 : if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo))
9270 : : {
9271 : 210780 : add_stmt_costs (loop_vinfo->vector_costs, &cost_vec);
9272 : 210780 : cost_vec.release ();
9273 : : }
9274 : : else
9275 : : /* For BB vectorization remember the SLP graph entry
9276 : : cost for later. */
9277 : 684622 : instance->cost_vec = cost_vec;
9278 : : }
9279 : 1216009 : }
9280 : :
9281 : : /* Now look for SLP instances with a root that are covered by other
9282 : : instances and remove them. */
9283 : 397455 : hash_set<stmt_vec_info> roots;
9284 : 1634324 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
9285 : 870372 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9286 : 30958 : roots.add (SLP_INSTANCE_ROOT_STMTS (instance)[0]);
9287 : 397455 : if (!roots.is_empty ())
9288 : : {
9289 : 12241 : visited.empty ();
9290 : 70039 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
9291 : 57798 : vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance), roots,
9292 : : visited);
9293 : 70039 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
9294 : 57798 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
9295 : 30958 : && !roots.contains (SLP_INSTANCE_ROOT_STMTS (instance)[0]))
9296 : : {
9297 : 1503 : stmt_vec_info root = SLP_INSTANCE_ROOT_STMTS (instance)[0];
9298 : 1503 : if (dump_enabled_p ())
9299 : 20 : dump_printf_loc (MSG_NOTE, vect_location,
9300 : : "removing SLP instance operations starting "
9301 : : "from: %G", root->stmt);
9302 : 1503 : vect_free_slp_instance (instance);
9303 : 1503 : vinfo->slp_instances.ordered_remove (i);
9304 : : }
9305 : : else
9306 : 56295 : ++i;
9307 : : }
9308 : :
9309 : : /* Compute vectorizable live stmts. */
9310 : 397455 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
9311 : 284062 : vect_bb_slp_mark_live_stmts (bb_vinfo);
9312 : :
9313 : 794910 : return !vinfo->slp_instances.is_empty ();
9314 : 1011370 : }
9315 : :
9316 : : /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
9317 : : closing the eventual chain. */
9318 : :
9319 : : static slp_instance
9320 : 742645 : get_ultimate_leader (slp_instance instance,
9321 : : hash_map<slp_instance, slp_instance> &instance_leader)
9322 : : {
9323 : 742645 : auto_vec<slp_instance *, 8> chain;
9324 : 742645 : slp_instance *tem;
9325 : 813349 : while (*(tem = instance_leader.get (instance)) != instance)
9326 : : {
9327 : 70704 : chain.safe_push (tem);
9328 : 70704 : instance = *tem;
9329 : : }
9330 : 813349 : while (!chain.is_empty ())
9331 : 70704 : *chain.pop () = instance;
9332 : 742645 : return instance;
9333 : 742645 : }
9334 : :
9335 : : namespace {
9336 : : /* Subroutine of vect_bb_partition_graph_r. Map KEY to INSTANCE in
9337 : : KEY_TO_INSTANCE, making INSTANCE the leader of any previous mapping
9338 : : for KEY. Return true if KEY was already in KEY_TO_INSTANCE.
9339 : :
9340 : : INSTANCE_LEADER is as for get_ultimate_leader. */
9341 : :
9342 : : template<typename T>
9343 : : bool
9344 : 3281269 : vect_map_to_instance (slp_instance instance, T key,
9345 : : hash_map<T, slp_instance> &key_to_instance,
9346 : : hash_map<slp_instance, slp_instance> &instance_leader)
9347 : : {
9348 : : bool existed_p;
9349 : 3281269 : slp_instance &key_instance = key_to_instance.get_or_insert (key, &existed_p);
9350 : 3281269 : if (!existed_p)
9351 : : ;
9352 : 163203 : else if (key_instance != instance)
9353 : : {
9354 : : /* If we're running into a previously marked key make us the
9355 : : leader of the current ultimate leader. This keeps the
9356 : : leader chain acyclic and works even when the current instance
9357 : : connects two previously independent graph parts. */
9358 : 59526 : slp_instance key_leader
9359 : 59526 : = get_ultimate_leader (key_instance, instance_leader);
9360 : 59526 : if (key_leader != instance)
9361 : 17857 : instance_leader.put (key_leader, instance);
9362 : : }
9363 : 3281269 : key_instance = instance;
9364 : 3281269 : return existed_p;
9365 : : }
9366 : : }
9367 : :
9368 : : /* Worker of vect_bb_partition_graph, recurse on NODE. */
9369 : :
9370 : : static void
9371 : 907657 : vect_bb_partition_graph_r (bb_vec_info bb_vinfo,
9372 : : slp_instance instance, slp_tree node,
9373 : : hash_map<stmt_vec_info, slp_instance> &stmt_to_instance,
9374 : : hash_map<slp_tree, slp_instance> &node_to_instance,
9375 : : hash_map<slp_instance, slp_instance> &instance_leader)
9376 : : {
9377 : 907657 : stmt_vec_info stmt_info;
9378 : 907657 : unsigned i;
9379 : :
9380 : 3281269 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9381 : 2373612 : if (stmt_info)
9382 : 2373612 : vect_map_to_instance (instance, stmt_info, stmt_to_instance,
9383 : : instance_leader);
9384 : :
9385 : 907657 : if (vect_map_to_instance (instance, node, node_to_instance,
9386 : : instance_leader))
9387 : 907657 : return;
9388 : :
9389 : : slp_tree child;
9390 : 1747182 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9391 : 878000 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9392 : 224538 : vect_bb_partition_graph_r (bb_vinfo, instance, child, stmt_to_instance,
9393 : : node_to_instance, instance_leader);
9394 : : }
9395 : :
9396 : : /* Partition the SLP graph into pieces that can be costed independently. */
9397 : :
9398 : : static void
9399 : 251634 : vect_bb_partition_graph (bb_vec_info bb_vinfo)
9400 : : {
9401 : 251634 : DUMP_VECT_SCOPE ("vect_bb_partition_graph");
9402 : :
9403 : : /* First walk the SLP graph assigning each involved scalar stmt a
9404 : : corresponding SLP graph entry and upon visiting a previously
9405 : : marked stmt, make the stmts leader the current SLP graph entry. */
9406 : 251634 : hash_map<stmt_vec_info, slp_instance> stmt_to_instance;
9407 : 251634 : hash_map<slp_tree, slp_instance> node_to_instance;
9408 : 251634 : hash_map<slp_instance, slp_instance> instance_leader;
9409 : 251634 : slp_instance instance;
9410 : 934753 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
9411 : : {
9412 : 683119 : instance_leader.put (instance, instance);
9413 : 683119 : vect_bb_partition_graph_r (bb_vinfo,
9414 : : instance, SLP_INSTANCE_TREE (instance),
9415 : : stmt_to_instance, node_to_instance,
9416 : : instance_leader);
9417 : : }
9418 : :
9419 : : /* Then collect entries to each independent subgraph. */
9420 : 1186387 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
9421 : : {
9422 : 683119 : slp_instance leader = get_ultimate_leader (instance, instance_leader);
9423 : 683119 : leader->subgraph_entries.safe_push (instance);
9424 : 683119 : if (dump_enabled_p ()
9425 : 683119 : && leader != instance)
9426 : 67 : dump_printf_loc (MSG_NOTE, vect_location,
9427 : : "instance %p is leader of %p\n",
9428 : : (void *) leader, (void *) instance);
9429 : : }
9430 : 251634 : }
9431 : :
9432 : : /* Compute the set of scalar stmts participating in internal and external
9433 : : nodes. */
9434 : :
9435 : : static void
9436 : 1547852 : vect_slp_gather_vectorized_scalar_stmts (vec_info *vinfo, slp_tree node,
9437 : : hash_set<slp_tree> &visited,
9438 : : hash_set<stmt_vec_info> &vstmts,
9439 : : hash_set<stmt_vec_info> &estmts)
9440 : : {
9441 : 1547852 : int i;
9442 : 1547852 : stmt_vec_info stmt_info;
9443 : 1547852 : slp_tree child;
9444 : :
9445 : 1547852 : if (visited.add (node))
9446 : 38441 : return;
9447 : :
9448 : 1509411 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
9449 : : {
9450 : 3090660 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9451 : 2230268 : if (stmt_info)
9452 : 2230268 : vstmts.add (stmt_info);
9453 : :
9454 : 3137349 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9455 : 868049 : if (child)
9456 : 868049 : vect_slp_gather_vectorized_scalar_stmts (vinfo, child, visited,
9457 : : vstmts, estmts);
9458 : : }
9459 : : else
9460 : 3642699 : for (tree def : SLP_TREE_SCALAR_OPS (node))
9461 : : {
9462 : 1696652 : stmt_vec_info def_stmt = vinfo->lookup_def (def);
9463 : 1696652 : if (def_stmt)
9464 : 335213 : estmts.add (def_stmt);
9465 : : }
9466 : : }
9467 : :
9468 : :
9469 : : /* Compute the scalar cost of the SLP node NODE and its children
9470 : : and return it. Do not account defs that are marked in LIFE and
9471 : : update LIFE according to uses of NODE. */
9472 : :
9473 : : static void
9474 : 898052 : vect_bb_slp_scalar_cost (vec_info *vinfo,
9475 : : slp_tree node, vec<bool, va_heap> *life,
9476 : : stmt_vector_for_cost *cost_vec,
9477 : : hash_set<stmt_vec_info> &vectorized_scalar_stmts,
9478 : : hash_set<stmt_vec_info> &scalar_stmts_in_externs,
9479 : : hash_set<slp_tree> &visited)
9480 : : {
9481 : 898052 : unsigned i;
9482 : 898052 : stmt_vec_info stmt_info;
9483 : 898052 : slp_tree child;
9484 : :
9485 : 898052 : if (visited.add (node))
9486 : 37643 : return;
9487 : :
9488 : 3090711 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
9489 : : {
9490 : 2230302 : ssa_op_iter op_iter;
9491 : 2230302 : def_operand_p def_p;
9492 : :
9493 : 2260548 : if (!stmt_info
9494 : 2230302 : || (*life)[i]
9495 : : /* Defs also used in external nodes are not in the
9496 : : vectorized_scalar_stmts set as they need to be preserved.
9497 : : Honor that. */
9498 : 4433014 : || scalar_stmts_in_externs.contains (stmt_info))
9499 : 102391 : continue;
9500 : :
9501 : 2200056 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
9502 : 2200056 : gimple *orig_stmt = orig_stmt_info->stmt;
9503 : :
9504 : : /* If there is a non-vectorized use of the defs then the scalar
9505 : : stmt is kept live in which case we do not account it or any
9506 : : required defs in the SLP children in the scalar cost. This
9507 : : way we make the vectorization more costly when compared to
9508 : : the scalar cost. */
9509 : 2200056 : if (!STMT_VINFO_LIVE_P (stmt_info))
9510 : : {
9511 : 2133510 : auto_vec<gimple *, 8> worklist;
9512 : 2133510 : hash_set<gimple *> *worklist_visited = NULL;
9513 : 2133510 : worklist.quick_push (orig_stmt);
9514 : 2138532 : do
9515 : : {
9516 : 2138532 : gimple *work_stmt = worklist.pop ();
9517 : 4669117 : FOR_EACH_PHI_OR_STMT_DEF (def_p, work_stmt, op_iter, SSA_OP_DEF)
9518 : : {
9519 : 411064 : imm_use_iterator use_iter;
9520 : 411064 : gimple *use_stmt;
9521 : 1016758 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter,
9522 : : DEF_FROM_PTR (def_p))
9523 : 624705 : if (!is_gimple_debug (use_stmt))
9524 : : {
9525 : 481804 : stmt_vec_info use_stmt_info
9526 : 481804 : = vinfo->lookup_stmt (use_stmt);
9527 : 481804 : if (!use_stmt_info
9528 : 481804 : || !vectorized_scalar_stmts.contains (use_stmt_info))
9529 : : {
9530 : 24135 : if (use_stmt_info
9531 : 21659 : && STMT_VINFO_IN_PATTERN_P (use_stmt_info))
9532 : : {
9533 : : /* For stmts participating in patterns we have
9534 : : to check its uses recursively. */
9535 : 5124 : if (!worklist_visited)
9536 : 3975 : worklist_visited = new hash_set<gimple *> ();
9537 : 5124 : if (!worklist_visited->add (use_stmt))
9538 : 5124 : worklist.safe_push (use_stmt);
9539 : 5124 : continue;
9540 : : }
9541 : 19011 : (*life)[i] = true;
9542 : 19011 : goto next_lane;
9543 : : }
9544 : 411064 : }
9545 : : }
9546 : : }
9547 : 4239042 : while (!worklist.is_empty ());
9548 : 2114499 : next_lane:
9549 : 2133510 : if (worklist_visited)
9550 : 3975 : delete worklist_visited;
9551 : 2133510 : if ((*life)[i])
9552 : 19011 : continue;
9553 : 2133510 : }
9554 : :
9555 : : /* Count scalar stmts only once. */
9556 : 2181045 : if (gimple_visited_p (orig_stmt))
9557 : 25360 : continue;
9558 : 2155685 : gimple_set_visited (orig_stmt, true);
9559 : :
9560 : 2155685 : vect_cost_for_stmt kind;
9561 : 2155685 : if (STMT_VINFO_DATA_REF (orig_stmt_info))
9562 : : {
9563 : 1961794 : data_reference_p dr = STMT_VINFO_DATA_REF (orig_stmt_info);
9564 : 1961794 : tree base = get_base_address (DR_REF (dr));
9565 : : /* When the scalar access is to a non-global not address-taken
9566 : : decl that is not BLKmode assume we can access it with a single
9567 : : non-load/store instruction. */
9568 : 1961794 : if (DECL_P (base)
9569 : 1529832 : && !is_global_var (base)
9570 : 1454980 : && !TREE_ADDRESSABLE (base)
9571 : 2516839 : && DECL_MODE (base) != BLKmode)
9572 : : kind = scalar_stmt;
9573 : 1816516 : else if (DR_IS_READ (STMT_VINFO_DATA_REF (orig_stmt_info)))
9574 : : kind = scalar_load;
9575 : : else
9576 : 1597900 : kind = scalar_store;
9577 : : }
9578 : 193891 : else if (vect_nop_conversion_p (orig_stmt_info))
9579 : 20558 : continue;
9580 : : /* For single-argument PHIs assume coalescing which means zero cost
9581 : : for the scalar and the vector PHIs. This avoids artificially
9582 : : favoring the vector path (but may pessimize it in some cases). */
9583 : 173333 : else if (is_a <gphi *> (orig_stmt_info->stmt)
9584 : 173333 : && gimple_phi_num_args
9585 : 78884 : (as_a <gphi *> (orig_stmt_info->stmt)) == 1)
9586 : 7216 : continue;
9587 : : else
9588 : : kind = scalar_stmt;
9589 : 2127911 : record_stmt_cost (cost_vec, 1, kind, orig_stmt_info,
9590 : : SLP_TREE_VECTYPE (node), 0, vect_body);
9591 : : }
9592 : :
9593 : 1720818 : auto_vec<bool, 20> subtree_life;
9594 : 2488380 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
9595 : : {
9596 : 868073 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
9597 : : {
9598 : : /* Do not directly pass LIFE to the recursive call, copy it to
9599 : : confine changes in the callee to the current child/subtree. */
9600 : 218249 : if (SLP_TREE_PERMUTE_P (node))
9601 : : {
9602 : 3551 : subtree_life.safe_grow_cleared (SLP_TREE_LANES (child), true);
9603 : 12409 : for (unsigned j = 0;
9604 : 12409 : j < SLP_TREE_LANE_PERMUTATION (node).length (); ++j)
9605 : : {
9606 : 8858 : auto perm = SLP_TREE_LANE_PERMUTATION (node)[j];
9607 : 8858 : if (perm.first == i)
9608 : 4710 : subtree_life[perm.second] = (*life)[j];
9609 : : }
9610 : : }
9611 : : else
9612 : : {
9613 : 214698 : gcc_assert (SLP_TREE_LANES (node) == SLP_TREE_LANES (child));
9614 : 214698 : subtree_life.safe_splice (*life);
9615 : : }
9616 : 218249 : vect_bb_slp_scalar_cost (vinfo, child, &subtree_life, cost_vec,
9617 : : vectorized_scalar_stmts,
9618 : : scalar_stmts_in_externs, visited);
9619 : 218249 : subtree_life.truncate (0);
9620 : : }
9621 : : }
9622 : : }
9623 : :
9624 : : /* Comparator for the loop-index sorted cost vectors. */
9625 : :
9626 : : static int
9627 : 17435797 : li_cost_vec_cmp (const void *a_, const void *b_)
9628 : : {
9629 : 17435797 : auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_;
9630 : 17435797 : auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_;
9631 : 17435797 : if (a->first < b->first)
9632 : : return -1;
9633 : 16726853 : else if (a->first == b->first)
9634 : 16101616 : return 0;
9635 : : return 1;
9636 : : }
9637 : :
9638 : : /* Check if vectorization of the basic block is profitable for the
9639 : : subgraph denoted by SLP_INSTANCES. */
9640 : :
9641 : : static bool
9642 : 662073 : vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo,
9643 : : vec<slp_instance> slp_instances,
9644 : : loop_p orig_loop)
9645 : : {
9646 : 662073 : slp_instance instance;
9647 : 662073 : int i;
9648 : 662073 : unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
9649 : 662073 : unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
9650 : :
9651 : 662073 : if (dump_enabled_p ())
9652 : : {
9653 : 96 : dump_printf_loc (MSG_NOTE, vect_location, "Costing subgraph: \n");
9654 : 96 : hash_set<slp_tree> visited;
9655 : 387 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9656 : 99 : vect_print_slp_graph (MSG_NOTE, vect_location,
9657 : : SLP_INSTANCE_TREE (instance), visited);
9658 : 96 : }
9659 : :
9660 : : /* Compute the set of scalar stmts we know will go away 'locally' when
9661 : : vectorizing. This used to be tracked with just PURE_SLP_STMT but that's
9662 : : not accurate for nodes promoted extern late or for scalar stmts that
9663 : : are used both in extern defs and in vectorized defs. */
9664 : 662073 : hash_set<stmt_vec_info> vectorized_scalar_stmts;
9665 : 662073 : hash_set<stmt_vec_info> scalar_stmts_in_externs;
9666 : 662073 : hash_set<slp_tree> visited;
9667 : 1341876 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9668 : : {
9669 : 679803 : vect_slp_gather_vectorized_scalar_stmts (bb_vinfo,
9670 : : SLP_INSTANCE_TREE (instance),
9671 : : visited,
9672 : : vectorized_scalar_stmts,
9673 : : scalar_stmts_in_externs);
9674 : 784553 : for (stmt_vec_info rstmt : SLP_INSTANCE_ROOT_STMTS (instance))
9675 : 50290 : vectorized_scalar_stmts.add (rstmt);
9676 : : }
9677 : : /* Scalar stmts used as defs in external nodes need to be preseved, so
9678 : : remove them from vectorized_scalar_stmts. */
9679 : 961230 : for (stmt_vec_info stmt : scalar_stmts_in_externs)
9680 : 299157 : vectorized_scalar_stmts.remove (stmt);
9681 : :
9682 : : /* Calculate scalar cost and sum the cost for the vector stmts
9683 : : previously collected. */
9684 : 662073 : stmt_vector_for_cost scalar_costs = vNULL;
9685 : 662073 : stmt_vector_for_cost vector_costs = vNULL;
9686 : 662073 : visited.empty ();
9687 : 1341876 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9688 : : {
9689 : 679803 : auto_vec<bool, 20> life;
9690 : 679803 : life.safe_grow_cleared (SLP_TREE_LANES (SLP_INSTANCE_TREE (instance)),
9691 : : true);
9692 : 679803 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9693 : 54460 : record_stmt_cost (&scalar_costs,
9694 : 27230 : SLP_INSTANCE_ROOT_STMTS (instance).length (),
9695 : : scalar_stmt,
9696 : 27230 : SLP_INSTANCE_ROOT_STMTS (instance)[0], 0, vect_body);
9697 : 679803 : vect_bb_slp_scalar_cost (bb_vinfo,
9698 : : SLP_INSTANCE_TREE (instance),
9699 : : &life, &scalar_costs, vectorized_scalar_stmts,
9700 : : scalar_stmts_in_externs, visited);
9701 : 679803 : vector_costs.safe_splice (instance->cost_vec);
9702 : 679803 : instance->cost_vec.release ();
9703 : 679803 : }
9704 : :
9705 : 662073 : if (dump_enabled_p ())
9706 : 96 : dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
9707 : :
9708 : : /* When costing non-loop vectorization we need to consider each covered
9709 : : loop independently and make sure vectorization is profitable. For
9710 : : now we assume a loop may be not entered or executed an arbitrary
9711 : : number of iterations (??? static information can provide more
9712 : : precise info here) which means we can simply cost each containing
9713 : : loops stmts separately. */
9714 : :
9715 : : /* First produce cost vectors sorted by loop index. */
9716 : 662073 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9717 : 662073 : li_scalar_costs (scalar_costs.length ());
9718 : 662073 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9719 : 662073 : li_vector_costs (vector_costs.length ());
9720 : 662073 : stmt_info_for_cost *cost;
9721 : 2817214 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9722 : : {
9723 : 2155141 : unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9724 : 2155141 : li_scalar_costs.quick_push (std::make_pair (l, cost));
9725 : : }
9726 : : /* Use a random used loop as fallback in case the first vector_costs
9727 : : entry does not have a stmt_info associated with it. */
9728 : 662073 : unsigned l = li_scalar_costs[0].first;
9729 : 2427327 : FOR_EACH_VEC_ELT (vector_costs, i, cost)
9730 : : {
9731 : : /* We inherit from the previous COST, invariants, externals and
9732 : : extracts immediately follow the cost for the related stmt. */
9733 : 1765254 : if (cost->stmt_info)
9734 : 1040347 : l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9735 : 1765254 : li_vector_costs.quick_push (std::make_pair (l, cost));
9736 : : }
9737 : 662073 : li_scalar_costs.qsort (li_cost_vec_cmp);
9738 : 662073 : li_vector_costs.qsort (li_cost_vec_cmp);
9739 : :
9740 : : /* Now cost the portions individually. */
9741 : : unsigned vi = 0;
9742 : : unsigned si = 0;
9743 : 1146526 : bool profitable = true;
9744 : 1146526 : while (si < li_scalar_costs.length ()
9745 : 1813079 : && vi < li_vector_costs.length ())
9746 : : {
9747 : 666553 : unsigned sl = li_scalar_costs[si].first;
9748 : 666553 : unsigned vl = li_vector_costs[vi].first;
9749 : 666553 : if (sl != vl)
9750 : : {
9751 : 1270 : if (dump_enabled_p ())
9752 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9753 : : "Scalar %d and vector %d loop part do not "
9754 : : "match up, skipping scalar part\n", sl, vl);
9755 : : /* Skip the scalar part, assuming zero cost on the vector side. */
9756 : 2563 : do
9757 : : {
9758 : 2563 : si++;
9759 : : }
9760 : 2563 : while (si < li_scalar_costs.length ()
9761 : 4745 : && li_scalar_costs[si].first == sl);
9762 : 1270 : continue;
9763 : : }
9764 : :
9765 : 665283 : class vector_costs *scalar_target_cost_data = init_cost (bb_vinfo, true);
9766 : 2136039 : do
9767 : : {
9768 : 2136039 : add_stmt_cost (scalar_target_cost_data, li_scalar_costs[si].second);
9769 : 2136039 : si++;
9770 : : }
9771 : 2136039 : while (si < li_scalar_costs.length ()
9772 : 4279107 : && li_scalar_costs[si].first == sl);
9773 : 665283 : scalar_target_cost_data->finish_cost (nullptr);
9774 : 665283 : scalar_cost = (scalar_target_cost_data->body_cost ()
9775 : 665283 : * param_vect_scalar_cost_multiplier) / 100;
9776 : :
9777 : : /* Complete the target-specific vector cost calculation. */
9778 : 665283 : class vector_costs *vect_target_cost_data = init_cost (bb_vinfo, false);
9779 : 1733005 : do
9780 : : {
9781 : 1733005 : add_stmt_cost (vect_target_cost_data, li_vector_costs[vi].second);
9782 : 1733005 : vi++;
9783 : : }
9784 : 1733005 : while (vi < li_vector_costs.length ()
9785 : 3473973 : && li_vector_costs[vi].first == vl);
9786 : 665283 : vect_target_cost_data->finish_cost (scalar_target_cost_data);
9787 : 665283 : vec_prologue_cost = vect_target_cost_data->prologue_cost ();
9788 : 665283 : vec_inside_cost = vect_target_cost_data->body_cost ();
9789 : 665283 : vec_epilogue_cost = vect_target_cost_data->epilogue_cost ();
9790 : 665283 : delete scalar_target_cost_data;
9791 : 665283 : delete vect_target_cost_data;
9792 : :
9793 : 665283 : vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
9794 : :
9795 : 665283 : if (dump_enabled_p ())
9796 : : {
9797 : 96 : dump_printf_loc (MSG_NOTE, vect_location,
9798 : : "Cost model analysis for part in loop %d:\n", sl);
9799 : 96 : dump_printf (MSG_NOTE, " Vector cost: %d\n",
9800 : : vec_inside_cost + vec_outside_cost);
9801 : 96 : dump_printf (MSG_NOTE, " Scalar cost: %d\n", scalar_cost);
9802 : : }
9803 : :
9804 : : /* Vectorization is profitable if its cost is more than the cost of scalar
9805 : : version. Note that we err on the vector side for equal cost because
9806 : : the cost estimate is otherwise quite pessimistic (constant uses are
9807 : : free on the scalar side but cost a load on the vector side for
9808 : : example). */
9809 : 665283 : if (vec_outside_cost + vec_inside_cost > scalar_cost)
9810 : : {
9811 : : profitable = false;
9812 : : break;
9813 : : }
9814 : : }
9815 : 1142033 : if (profitable && vi < li_vector_costs.length ())
9816 : : {
9817 : 1042 : if (dump_enabled_p ())
9818 : 12 : dump_printf_loc (MSG_NOTE, vect_location,
9819 : : "Excess vector cost for part in loop %d:\n",
9820 : 6 : li_vector_costs[vi].first);
9821 : : profitable = false;
9822 : : }
9823 : :
9824 : : /* Unset visited flag. This is delayed when the subgraph is profitable
9825 : : and we process the loop for remaining unvectorized if-converted code. */
9826 : 662073 : if (!orig_loop || !profitable)
9827 : 2815915 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9828 : 2153935 : gimple_set_visited (cost->stmt_info->stmt, false);
9829 : :
9830 : 662073 : scalar_costs.release ();
9831 : 662073 : vector_costs.release ();
9832 : :
9833 : 662073 : return profitable;
9834 : 662073 : }
9835 : :
9836 : : /* qsort comparator for lane defs. */
9837 : :
9838 : : static int
9839 : 40 : vld_cmp (const void *a_, const void *b_)
9840 : : {
9841 : 40 : auto *a = (const std::pair<unsigned, tree> *)a_;
9842 : 40 : auto *b = (const std::pair<unsigned, tree> *)b_;
9843 : 40 : return a->first - b->first;
9844 : : }
9845 : :
9846 : : /* Return true if USE_STMT is a vector lane insert into VEC and set
9847 : : *THIS_LANE to the lane number that is set. */
9848 : :
9849 : : static bool
9850 : 244 : vect_slp_is_lane_insert (gimple *use_stmt, tree vec, unsigned *this_lane)
9851 : : {
9852 : 244 : gassign *use_ass = dyn_cast <gassign *> (use_stmt);
9853 : 91 : if (!use_ass
9854 : 91 : || gimple_assign_rhs_code (use_ass) != BIT_INSERT_EXPR
9855 : 22 : || (vec
9856 : 22 : ? gimple_assign_rhs1 (use_ass) != vec
9857 : 24 : : ((vec = gimple_assign_rhs1 (use_ass)), false))
9858 : 46 : || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec)),
9859 : 46 : TREE_TYPE (gimple_assign_rhs2 (use_ass)))
9860 : 46 : || !constant_multiple_p
9861 : 46 : (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass)),
9862 : 92 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec)))),
9863 : : this_lane))
9864 : 198 : return false;
9865 : : return true;
9866 : : }
9867 : :
9868 : : /* Find any vectorizable constructors and add them to the grouped_store
9869 : : array. */
9870 : :
9871 : : static void
9872 : 2391070 : vect_slp_check_for_roots (bb_vec_info bb_vinfo)
9873 : : {
9874 : 18602572 : for (unsigned i = 0; i < bb_vinfo->nbbs; ++i)
9875 : 32423004 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[i]);
9876 : 136658820 : !gsi_end_p (gsi); gsi_next (&gsi))
9877 : : {
9878 : 120447318 : gassign *assign = dyn_cast<gassign *> (gsi_stmt (gsi));
9879 : : /* This can be used to start SLP discovery for early breaks for BB early breaks
9880 : : when we get that far. */
9881 : 120447318 : if (!assign)
9882 : 179270627 : continue;
9883 : :
9884 : 31763107 : tree rhs = gimple_assign_rhs1 (assign);
9885 : 31763107 : enum tree_code code = gimple_assign_rhs_code (assign);
9886 : 31763107 : use_operand_p use_p;
9887 : 31763107 : gimple *use_stmt;
9888 : 31763107 : if (code == CONSTRUCTOR)
9889 : : {
9890 : 1635866 : if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
9891 : 60346 : || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)),
9892 : 88144 : CONSTRUCTOR_NELTS (rhs))
9893 : 40955 : || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
9894 : 1676821 : || uniform_vector_p (rhs))
9895 : 1623815 : continue;
9896 : :
9897 : : unsigned j;
9898 : : tree val;
9899 : 59964 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9900 : 47913 : if (TREE_CODE (val) != SSA_NAME
9901 : 47913 : || !bb_vinfo->lookup_def (val))
9902 : : break;
9903 : 29868 : if (j != CONSTRUCTOR_NELTS (rhs))
9904 : 2883 : continue;
9905 : :
9906 : 12051 : vec<stmt_vec_info> roots = vNULL;
9907 : 12051 : roots.safe_push (bb_vinfo->lookup_stmt (assign));
9908 : 12051 : vec<stmt_vec_info> stmts;
9909 : 12051 : stmts.create (CONSTRUCTOR_NELTS (rhs));
9910 : 67612 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9911 : 43510 : stmts.quick_push
9912 : 43510 : (vect_stmt_to_vectorize (bb_vinfo->lookup_def (val)));
9913 : 12051 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
9914 : 12051 : stmts, roots));
9915 : : }
9916 : 30127241 : else if (code == BIT_INSERT_EXPR
9917 : 931 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
9918 : 584 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).is_constant ()
9919 : 584 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).to_constant () > 1
9920 : 581 : && integer_zerop (gimple_assign_rhs3 (assign))
9921 : 335 : && useless_type_conversion_p
9922 : 335 : (TREE_TYPE (TREE_TYPE (rhs)),
9923 : 335 : TREE_TYPE (gimple_assign_rhs2 (assign)))
9924 : 30127851 : && bb_vinfo->lookup_def (gimple_assign_rhs2 (assign)))
9925 : : {
9926 : : /* We start to match on insert to lane zero but since the
9927 : : inserts need not be ordered we'd have to search both
9928 : : the def and the use chains. */
9929 : 211 : tree vectype = TREE_TYPE (rhs);
9930 : 211 : unsigned nlanes = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
9931 : 211 : auto_vec<std::pair<unsigned, tree> > lane_defs (nlanes);
9932 : 211 : auto_sbitmap lanes (nlanes);
9933 : 211 : bitmap_clear (lanes);
9934 : 211 : bitmap_set_bit (lanes, 0);
9935 : 211 : tree def = gimple_assign_lhs (assign);
9936 : 211 : lane_defs.quick_push
9937 : 211 : (std::make_pair (0, gimple_assign_rhs2 (assign)));
9938 : 211 : unsigned lanes_found = 1;
9939 : : /* Start with the use chains, the last stmt will be the root. */
9940 : 211 : stmt_vec_info last = bb_vinfo->lookup_stmt (assign);
9941 : 211 : vec<stmt_vec_info> roots = vNULL;
9942 : 211 : roots.safe_push (last);
9943 : 213 : do
9944 : : {
9945 : 213 : use_operand_p use_p;
9946 : 213 : gimple *use_stmt;
9947 : 213 : if (!single_imm_use (def, &use_p, &use_stmt))
9948 : : break;
9949 : 207 : unsigned this_lane;
9950 : 207 : if (!bb_vinfo->lookup_stmt (use_stmt)
9951 : 207 : || !vect_slp_is_lane_insert (use_stmt, def, &this_lane)
9952 : 229 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (use_stmt)))
9953 : : break;
9954 : 22 : if (bitmap_bit_p (lanes, this_lane))
9955 : : break;
9956 : 2 : lanes_found++;
9957 : 2 : bitmap_set_bit (lanes, this_lane);
9958 : 2 : gassign *use_ass = as_a <gassign *> (use_stmt);
9959 : 2 : lane_defs.quick_push (std::make_pair
9960 : 2 : (this_lane, gimple_assign_rhs2 (use_ass)));
9961 : 2 : last = bb_vinfo->lookup_stmt (use_ass);
9962 : 2 : roots.safe_push (last);
9963 : 2 : def = gimple_assign_lhs (use_ass);
9964 : : }
9965 : 2 : while (lanes_found < nlanes);
9966 : 211 : if (roots.length () > 1)
9967 : 2 : std::swap(roots[0], roots[roots.length () - 1]);
9968 : 211 : if (lanes_found < nlanes)
9969 : : {
9970 : : /* Now search the def chain. */
9971 : 211 : def = gimple_assign_rhs1 (assign);
9972 : 213 : do
9973 : : {
9974 : 213 : if (TREE_CODE (def) != SSA_NAME
9975 : 213 : || !has_single_use (def))
9976 : : break;
9977 : 56 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
9978 : 56 : unsigned this_lane;
9979 : 56 : if (!bb_vinfo->lookup_stmt (def_stmt)
9980 : 37 : || !vect_slp_is_lane_insert (def_stmt,
9981 : : NULL_TREE, &this_lane)
9982 : 80 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (def_stmt)))
9983 : : break;
9984 : 24 : if (bitmap_bit_p (lanes, this_lane))
9985 : : break;
9986 : 4 : lanes_found++;
9987 : 4 : bitmap_set_bit (lanes, this_lane);
9988 : 8 : lane_defs.quick_push (std::make_pair
9989 : 4 : (this_lane,
9990 : 4 : gimple_assign_rhs2 (def_stmt)));
9991 : 4 : roots.safe_push (bb_vinfo->lookup_stmt (def_stmt));
9992 : 4 : def = gimple_assign_rhs1 (def_stmt);
9993 : : }
9994 : 4 : while (lanes_found < nlanes);
9995 : : }
9996 : 211 : if (lanes_found == nlanes)
9997 : : {
9998 : : /* Sort lane_defs after the lane index and register the root. */
9999 : 2 : lane_defs.qsort (vld_cmp);
10000 : 2 : vec<stmt_vec_info> stmts;
10001 : 2 : stmts.create (nlanes);
10002 : 10 : for (unsigned i = 0; i < nlanes; ++i)
10003 : 8 : stmts.quick_push (bb_vinfo->lookup_def (lane_defs[i].second));
10004 : 2 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
10005 : 2 : stmts, roots));
10006 : : }
10007 : : else
10008 : 209 : roots.release ();
10009 : 211 : }
10010 : 30127030 : else if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
10011 : 29180066 : && (associative_tree_code (code) || code == MINUS_EXPR)
10012 : : /* ??? This pessimizes a two-element reduction. PR54400.
10013 : : ??? In-order reduction could be handled if we only
10014 : : traverse one operand chain in vect_slp_linearize_chain. */
10015 : 34105126 : && !needs_fold_left_reduction_p (TREE_TYPE (rhs), code)
10016 : : /* Ops with constants at the tail can be stripped here. */
10017 : 5885652 : && TREE_CODE (rhs) == SSA_NAME
10018 : 5817706 : && TREE_CODE (gimple_assign_rhs2 (assign)) == SSA_NAME
10019 : : /* Should be the chain end. */
10020 : 32440698 : && (!single_imm_use (gimple_assign_lhs (assign),
10021 : : &use_p, &use_stmt)
10022 : 1787580 : || !is_gimple_assign (use_stmt)
10023 : 1188865 : || (gimple_assign_rhs_code (use_stmt) != code
10024 : 877420 : && ((code != PLUS_EXPR && code != MINUS_EXPR)
10025 : 493452 : || (gimple_assign_rhs_code (use_stmt)
10026 : 493452 : != (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR))))))
10027 : : {
10028 : : /* We start the match at the end of a possible association
10029 : : chain. */
10030 : 1907556 : auto_vec<chain_op_t> chain;
10031 : 1907556 : auto_vec<std::pair<tree_code, gimple *> > worklist;
10032 : 1907556 : auto_vec<gimple *> chain_stmts;
10033 : 1907556 : gimple *code_stmt = NULL, *alt_code_stmt = NULL;
10034 : 1907556 : if (code == MINUS_EXPR)
10035 : 318381 : code = PLUS_EXPR;
10036 : 1907556 : internal_fn reduc_fn;
10037 : 2185946 : if (!reduction_fn_for_scalar_code (code, &reduc_fn)
10038 : 1907556 : || reduc_fn == IFN_LAST)
10039 : 278390 : continue;
10040 : 1629166 : vect_slp_linearize_chain (bb_vinfo, worklist, chain, code, assign,
10041 : : /* ??? */
10042 : : code_stmt, alt_code_stmt, &chain_stmts);
10043 : 3258332 : if (chain.length () > 1)
10044 : : {
10045 : : /* Sort the chain according to def_type and operation. */
10046 : 1629166 : chain.sort (dt_sort_cmp, bb_vinfo);
10047 : : /* ??? Now we'd want to strip externals and constants
10048 : : but record those to be handled in the epilogue. */
10049 : : /* ??? For now do not allow mixing ops or externs/constants. */
10050 : 1629166 : bool invalid = false;
10051 : 1629166 : unsigned remain_cnt = 0;
10052 : 1629166 : unsigned last_idx = 0;
10053 : 4903351 : for (unsigned i = 0; i < chain.length (); ++i)
10054 : : {
10055 : 3614499 : if (chain[i].code != code)
10056 : : {
10057 : : invalid = true;
10058 : : break;
10059 : : }
10060 : 3274185 : if (chain[i].dt != vect_internal_def
10061 : : /* Avoid stmts where the def is not the LHS, like
10062 : : ASMs. */
10063 : 6314233 : || (gimple_get_lhs (bb_vinfo->lookup_def
10064 : 3040048 : (chain[i].op)->stmt)
10065 : 3040048 : != chain[i].op))
10066 : 237081 : remain_cnt++;
10067 : : else
10068 : : last_idx = i;
10069 : : }
10070 : : /* Make sure to have an even number of lanes as we later do
10071 : : all-or-nothing discovery, not trying to split further. */
10072 : 1629166 : if ((chain.length () - remain_cnt) & 1)
10073 : 191141 : remain_cnt++;
10074 : 1629166 : if (!invalid && chain.length () - remain_cnt > 1)
10075 : : {
10076 : 1218393 : vec<stmt_vec_info> stmts;
10077 : 1218393 : vec<tree> remain = vNULL;
10078 : 1218393 : stmts.create (chain.length ());
10079 : 1218393 : if (remain_cnt > 0)
10080 : 110780 : remain.create (remain_cnt);
10081 : 3910132 : for (unsigned i = 0; i < chain.length (); ++i)
10082 : : {
10083 : 2691739 : stmt_vec_info stmt_info;
10084 : 2691739 : if (chain[i].dt == vect_internal_def
10085 : 2655201 : && ((stmt_info = bb_vinfo->lookup_def (chain[i].op)),
10086 : 2655201 : gimple_get_lhs (stmt_info->stmt) == chain[i].op)
10087 : 5346856 : && (i != last_idx
10088 : 1218393 : || (stmts.length () & 1)))
10089 : 2569634 : stmts.quick_push (stmt_info);
10090 : : else
10091 : 122105 : remain.quick_push (chain[i].op);
10092 : : }
10093 : 1218393 : vec<stmt_vec_info> roots;
10094 : 1218393 : roots.create (chain_stmts.length ());
10095 : 2691739 : for (unsigned i = 0; i < chain_stmts.length (); ++i)
10096 : 1473346 : roots.quick_push (bb_vinfo->lookup_stmt (chain_stmts[i]));
10097 : 1218393 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_bb_reduc,
10098 : 1218393 : stmts, roots, remain));
10099 : : }
10100 : : }
10101 : 1907556 : }
10102 : : }
10103 : 2391070 : }
10104 : :
10105 : : /* Walk the grouped store chains and replace entries with their
10106 : : pattern variant if any. */
10107 : :
10108 : : static void
10109 : 644337 : vect_fixup_store_groups_with_patterns (vec_info *vinfo)
10110 : : {
10111 : 644337 : stmt_vec_info first_element;
10112 : 644337 : unsigned i;
10113 : :
10114 : 1537359 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
10115 : : {
10116 : : /* We also have CTORs in this array. */
10117 : 893022 : if (!STMT_VINFO_GROUPED_ACCESS (first_element))
10118 : 0 : continue;
10119 : 893022 : if (STMT_VINFO_IN_PATTERN_P (first_element))
10120 : : {
10121 : 241 : stmt_vec_info orig = first_element;
10122 : 241 : first_element = STMT_VINFO_RELATED_STMT (first_element);
10123 : 241 : DR_GROUP_FIRST_ELEMENT (first_element) = first_element;
10124 : 241 : DR_GROUP_SIZE (first_element) = DR_GROUP_SIZE (orig);
10125 : 241 : DR_GROUP_GAP (first_element) = DR_GROUP_GAP (orig);
10126 : 241 : DR_GROUP_NEXT_ELEMENT (first_element) = DR_GROUP_NEXT_ELEMENT (orig);
10127 : 241 : vinfo->grouped_stores[i] = first_element;
10128 : : }
10129 : 893022 : stmt_vec_info prev = first_element;
10130 : 2511316 : while (DR_GROUP_NEXT_ELEMENT (prev))
10131 : : {
10132 : 1618294 : stmt_vec_info elt = DR_GROUP_NEXT_ELEMENT (prev);
10133 : 1618294 : if (STMT_VINFO_IN_PATTERN_P (elt))
10134 : : {
10135 : 857 : stmt_vec_info orig = elt;
10136 : 857 : elt = STMT_VINFO_RELATED_STMT (elt);
10137 : 857 : DR_GROUP_NEXT_ELEMENT (prev) = elt;
10138 : 857 : DR_GROUP_GAP (elt) = DR_GROUP_GAP (orig);
10139 : 857 : DR_GROUP_NEXT_ELEMENT (elt) = DR_GROUP_NEXT_ELEMENT (orig);
10140 : : }
10141 : 1618294 : DR_GROUP_FIRST_ELEMENT (elt) = first_element;
10142 : 1618294 : prev = elt;
10143 : : }
10144 : : }
10145 : 644337 : }
10146 : :
10147 : : /* Check if the region described by BB_VINFO can be vectorized, returning
10148 : : true if so. When returning false, set FATAL to true if the same failure
10149 : : would prevent vectorization at other vector sizes, false if it is still
10150 : : worth trying other sizes. N_STMTS is the number of statements in the
10151 : : region. */
10152 : :
10153 : : static bool
10154 : 2391070 : vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal,
10155 : : vec<int> *dataref_groups)
10156 : : {
10157 : 2391070 : DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
10158 : :
10159 : 2391070 : slp_instance instance;
10160 : 2391070 : int i;
10161 : :
10162 : : /* The first group of checks is independent of the vector size. */
10163 : 2391070 : fatal = true;
10164 : :
10165 : : /* Analyze the data references. */
10166 : :
10167 : 2391070 : if (!vect_analyze_data_refs (bb_vinfo, NULL))
10168 : : {
10169 : 0 : if (dump_enabled_p ())
10170 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10171 : : "not vectorized: unhandled data-ref in basic "
10172 : : "block.\n");
10173 : 0 : return false;
10174 : : }
10175 : :
10176 : 2391070 : if (!vect_analyze_data_ref_accesses (bb_vinfo, dataref_groups))
10177 : : {
10178 : 0 : if (dump_enabled_p ())
10179 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10180 : : "not vectorized: unhandled data access in "
10181 : : "basic block.\n");
10182 : 0 : return false;
10183 : : }
10184 : :
10185 : 2391070 : vect_slp_check_for_roots (bb_vinfo);
10186 : :
10187 : : /* If there are no grouped stores and no constructors in the region
10188 : : there is no need to continue with pattern recog as vect_analyze_slp
10189 : : will fail anyway. */
10190 : 2391070 : if (bb_vinfo->grouped_stores.is_empty ()
10191 : 2025779 : && bb_vinfo->roots.is_empty ())
10192 : : {
10193 : 1746733 : if (dump_enabled_p ())
10194 : 1012 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10195 : : "not vectorized: no grouped stores in "
10196 : : "basic block.\n");
10197 : 1746733 : return false;
10198 : : }
10199 : :
10200 : : /* While the rest of the analysis below depends on it in some way. */
10201 : 644337 : fatal = false;
10202 : :
10203 : 644337 : vect_pattern_recog (bb_vinfo);
10204 : :
10205 : : /* Update store groups from pattern processing. */
10206 : 644337 : vect_fixup_store_groups_with_patterns (bb_vinfo);
10207 : :
10208 : : /* Check the SLP opportunities in the basic block, analyze and build SLP
10209 : : trees. */
10210 : 644337 : if (!vect_analyze_slp (bb_vinfo, n_stmts, false))
10211 : : {
10212 : 0 : if (dump_enabled_p ())
10213 : : {
10214 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10215 : : "Failed to SLP the basic block.\n");
10216 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10217 : : "not vectorized: failed to find SLP opportunities "
10218 : : "in basic block.\n");
10219 : : }
10220 : 0 : return false;
10221 : : }
10222 : :
10223 : : /* Optimize permutations. */
10224 : 644337 : vect_optimize_slp (bb_vinfo);
10225 : :
10226 : : /* Gather the loads reachable from the SLP graph entries. */
10227 : 644337 : vect_gather_slp_loads (bb_vinfo);
10228 : :
10229 : 644337 : vect_record_base_alignments (bb_vinfo);
10230 : :
10231 : : /* Analyze and verify the alignment of data references and the
10232 : : dependence in the SLP instances. */
10233 : 1440780 : for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
10234 : : {
10235 : 796443 : vect_location = instance->location ();
10236 : 796443 : if (! vect_slp_analyze_instance_alignment (bb_vinfo, instance)
10237 : 796443 : || ! vect_slp_analyze_instance_dependence (bb_vinfo, instance))
10238 : : {
10239 : 7674 : slp_tree node = SLP_INSTANCE_TREE (instance);
10240 : 7674 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
10241 : 7674 : if (dump_enabled_p ())
10242 : 4 : dump_printf_loc (MSG_NOTE, vect_location,
10243 : : "removing SLP instance operations starting from: %G",
10244 : : stmt_info->stmt);
10245 : 7674 : vect_free_slp_instance (instance);
10246 : 7674 : BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
10247 : 7674 : continue;
10248 : 7674 : }
10249 : :
10250 : : /* Mark all the statements that we want to vectorize as pure SLP and
10251 : : relevant. */
10252 : 788769 : vect_mark_slp_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance));
10253 : 788769 : vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
10254 : 788769 : unsigned j;
10255 : 788769 : stmt_vec_info root;
10256 : : /* Likewise consider instance root stmts as vectorized. */
10257 : 1744501 : FOR_EACH_VEC_ELT (SLP_INSTANCE_ROOT_STMTS (instance), j, root)
10258 : 166963 : STMT_SLP_TYPE (root) = pure_slp;
10259 : :
10260 : 788769 : i++;
10261 : : }
10262 : 2423498 : if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
10263 : : return false;
10264 : :
10265 : 284062 : if (!vect_slp_analyze_operations (bb_vinfo))
10266 : : {
10267 : 32428 : if (dump_enabled_p ())
10268 : 78 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10269 : : "not vectorized: bad operation in basic block.\n");
10270 : 32428 : return false;
10271 : : }
10272 : :
10273 : 251634 : vect_bb_partition_graph (bb_vinfo);
10274 : :
10275 : 251634 : return true;
10276 : : }
10277 : :
10278 : : /* Subroutine of vect_slp_bb. Try to vectorize the statements for all
10279 : : basic blocks in BBS, returning true on success.
10280 : : The region has N_STMTS statements and has the datarefs given by DATAREFS. */
10281 : :
10282 : : static bool
10283 : 2054007 : vect_slp_region (vec<basic_block> bbs, vec<data_reference_p> datarefs,
10284 : : vec<int> *dataref_groups, unsigned int n_stmts,
10285 : : loop_p orig_loop)
10286 : : {
10287 : 2054007 : bb_vec_info bb_vinfo;
10288 : 2054007 : auto_vector_modes vector_modes;
10289 : :
10290 : : /* Autodetect first vector size we try. */
10291 : 2054007 : machine_mode next_vector_mode = VOIDmode;
10292 : 2054007 : targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
10293 : 2054007 : unsigned int mode_i = 0;
10294 : :
10295 : 2054007 : vec_info_shared shared;
10296 : :
10297 : 2054007 : machine_mode autodetected_vector_mode = VOIDmode;
10298 : 2728133 : while (1)
10299 : : {
10300 : 2391070 : bool vectorized = false;
10301 : 2391070 : bool fatal = false;
10302 : 2391070 : bb_vinfo = new _bb_vec_info (bbs, &shared);
10303 : :
10304 : 2391070 : bool first_time_p = shared.datarefs.is_empty ();
10305 : 2391070 : BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
10306 : 2391070 : if (first_time_p)
10307 : 2077186 : bb_vinfo->shared->save_datarefs ();
10308 : : else
10309 : 313884 : bb_vinfo->shared->check_datarefs ();
10310 : 2391070 : bb_vinfo->vector_mode = next_vector_mode;
10311 : :
10312 : 2391070 : if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal, dataref_groups))
10313 : : {
10314 : 251634 : if (dump_enabled_p ())
10315 : : {
10316 : 1478 : dump_printf_loc (MSG_NOTE, vect_location,
10317 : : "***** Analysis succeeded with vector mode"
10318 : 739 : " %s\n", GET_MODE_NAME (bb_vinfo->vector_mode));
10319 : 739 : dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
10320 : : }
10321 : :
10322 : 251634 : bb_vinfo->shared->check_datarefs ();
10323 : :
10324 : 251634 : bool force_clear = false;
10325 : 251634 : auto_vec<slp_instance> profitable_subgraphs;
10326 : 1438021 : for (slp_instance instance : BB_VINFO_SLP_INSTANCES (bb_vinfo))
10327 : : {
10328 : 683119 : if (instance->subgraph_entries.is_empty ())
10329 : 218856 : continue;
10330 : :
10331 : 665262 : dump_user_location_t saved_vect_location = vect_location;
10332 : 665262 : vect_location = instance->location ();
10333 : 665262 : if (!unlimited_cost_model (NULL)
10334 : 1327335 : && !vect_bb_vectorization_profitable_p
10335 : 662073 : (bb_vinfo, instance->subgraph_entries, orig_loop))
10336 : : {
10337 : 183142 : if (dump_enabled_p ())
10338 : 26 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10339 : : "not vectorized: vectorization is not "
10340 : : "profitable.\n");
10341 : 183142 : vect_location = saved_vect_location;
10342 : 183142 : continue;
10343 : : }
10344 : :
10345 : 482120 : vect_location = saved_vect_location;
10346 : 482120 : if (!dbg_cnt (vect_slp))
10347 : : {
10348 : 0 : force_clear = true;
10349 : 0 : continue;
10350 : : }
10351 : :
10352 : 482120 : profitable_subgraphs.safe_push (instance);
10353 : : }
10354 : :
10355 : : /* When we're vectorizing an if-converted loop body make sure
10356 : : we vectorized all if-converted code. */
10357 : 419163 : if ((!profitable_subgraphs.is_empty () || force_clear) && orig_loop)
10358 : : {
10359 : 103 : gcc_assert (bb_vinfo->nbbs == 1);
10360 : 206 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[0]);
10361 : 4256 : !gsi_end_p (gsi); gsi_next (&gsi))
10362 : : {
10363 : : /* The costing above left us with DCEable vectorized scalar
10364 : : stmts having the visited flag set on profitable
10365 : : subgraphs. Do the delayed clearing of the flag here. */
10366 : 4153 : if (gimple_visited_p (gsi_stmt (gsi)))
10367 : : {
10368 : 1180 : gimple_set_visited (gsi_stmt (gsi), false);
10369 : 1180 : continue;
10370 : : }
10371 : 2973 : if (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED)
10372 : 867 : continue;
10373 : :
10374 : 6121 : if (gassign *ass = dyn_cast <gassign *> (gsi_stmt (gsi)))
10375 : 2558 : if (gimple_assign_rhs_code (ass) == COND_EXPR)
10376 : : {
10377 : 63 : if (!profitable_subgraphs.is_empty ()
10378 : 26 : && dump_enabled_p ())
10379 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10380 : : "not profitable because of "
10381 : : "unprofitable if-converted scalar "
10382 : : "code\n");
10383 : 37 : profitable_subgraphs.truncate (0);
10384 : : }
10385 : : }
10386 : : }
10387 : :
10388 : : /* Finally schedule the profitable subgraphs. */
10389 : 1068776 : for (slp_instance instance : profitable_subgraphs)
10390 : : {
10391 : 482084 : if (!vectorized && dump_enabled_p ())
10392 : 716 : dump_printf_loc (MSG_NOTE, vect_location,
10393 : : "Basic block will be vectorized "
10394 : : "using SLP\n");
10395 : 482084 : vectorized = true;
10396 : :
10397 : : /* Dump before scheduling as store vectorization will remove
10398 : : the original stores and mess with the instance tree
10399 : : so querying its location will eventually ICE. */
10400 : 482084 : if (flag_checking)
10401 : 1938078 : for (slp_instance sub : instance->subgraph_entries)
10402 : 491826 : gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
10403 : 482084 : unsigned HOST_WIDE_INT bytes;
10404 : 482084 : if (dump_enabled_p ())
10405 : 3415 : for (slp_instance sub : instance->subgraph_entries)
10406 : : {
10407 : 904 : tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
10408 : 1808 : if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
10409 : 904 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
10410 : 904 : sub->location (),
10411 : : "basic block part vectorized using %wu "
10412 : : "byte vectors\n", bytes);
10413 : : else
10414 : : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
10415 : : sub->location (),
10416 : : "basic block part vectorized using "
10417 : : "variable length vectors\n");
10418 : : }
10419 : :
10420 : 482084 : dump_user_location_t saved_vect_location = vect_location;
10421 : 482084 : vect_location = instance->location ();
10422 : :
10423 : 482084 : vect_schedule_slp (bb_vinfo, instance->subgraph_entries);
10424 : :
10425 : 482084 : vect_location = saved_vect_location;
10426 : : }
10427 : :
10428 : :
10429 : : /* Generate the invariant statements. */
10430 : 251634 : if (!gimple_seq_empty_p (bb_vinfo->inv_pattern_def_seq))
10431 : : {
10432 : 23 : if (dump_enabled_p ())
10433 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10434 : : "------>generating invariant statements\n");
10435 : :
10436 : 23 : bb_vinfo->insert_seq_on_entry (NULL,
10437 : : bb_vinfo->inv_pattern_def_seq);
10438 : : }
10439 : 251634 : }
10440 : : else
10441 : : {
10442 : 2139436 : if (dump_enabled_p ())
10443 : 1289 : dump_printf_loc (MSG_NOTE, vect_location,
10444 : : "***** Analysis failed with vector mode %s\n",
10445 : 1289 : GET_MODE_NAME (bb_vinfo->vector_mode));
10446 : : }
10447 : :
10448 : 2391070 : if (mode_i == 0)
10449 : 2054007 : autodetected_vector_mode = bb_vinfo->vector_mode;
10450 : :
10451 : 2391070 : if (!fatal)
10452 : 3388739 : while (mode_i < vector_modes.length ()
10453 : 1855524 : && vect_chooses_same_modes_p (bb_vinfo, vector_modes[mode_i]))
10454 : : {
10455 : 353332 : if (dump_enabled_p ())
10456 : 1622 : dump_printf_loc (MSG_NOTE, vect_location,
10457 : : "***** The result for vector mode %s would"
10458 : : " be the same\n",
10459 : 811 : GET_MODE_NAME (vector_modes[mode_i]));
10460 : 353332 : mode_i += 1;
10461 : : }
10462 : :
10463 : 2391070 : delete bb_vinfo;
10464 : :
10465 : 2391070 : if (mode_i < vector_modes.length ()
10466 : 2203165 : && VECTOR_MODE_P (autodetected_vector_mode)
10467 : 2111698 : && (related_vector_mode (vector_modes[mode_i],
10468 : : GET_MODE_INNER (autodetected_vector_mode))
10469 : 1055849 : == autodetected_vector_mode)
10470 : 4594235 : && (related_vector_mode (autodetected_vector_mode,
10471 : 550864 : GET_MODE_INNER (vector_modes[mode_i]))
10472 : 1101728 : == vector_modes[mode_i]))
10473 : : {
10474 : 550864 : if (dump_enabled_p ())
10475 : 206 : dump_printf_loc (MSG_NOTE, vect_location,
10476 : : "***** Skipping vector mode %s, which would"
10477 : : " repeat the analysis for %s\n",
10478 : 206 : GET_MODE_NAME (vector_modes[mode_i]),
10479 : 206 : GET_MODE_NAME (autodetected_vector_mode));
10480 : 550864 : mode_i += 1;
10481 : : }
10482 : :
10483 : 2391070 : if (vectorized
10484 : 2223567 : || mode_i == vector_modes.length ()
10485 : 2035705 : || autodetected_vector_mode == VOIDmode
10486 : : /* If vect_slp_analyze_bb_1 signaled that analysis for all
10487 : : vector sizes will fail do not bother iterating. */
10488 : 3279459 : || fatal)
10489 : 4108014 : return vectorized;
10490 : :
10491 : : /* Try the next biggest vector size. */
10492 : 337063 : next_vector_mode = vector_modes[mode_i++];
10493 : 337063 : if (dump_enabled_p ())
10494 : 207 : dump_printf_loc (MSG_NOTE, vect_location,
10495 : : "***** Re-trying analysis with vector mode %s\n",
10496 : 207 : GET_MODE_NAME (next_vector_mode));
10497 : 337063 : }
10498 : 2054007 : }
10499 : :
10500 : :
10501 : : /* Main entry for the BB vectorizer. Analyze and transform BBS, returns
10502 : : true if anything in the basic-block was vectorized. */
10503 : :
10504 : : static bool
10505 : 2054007 : vect_slp_bbs (const vec<basic_block> &bbs, loop_p orig_loop)
10506 : : {
10507 : 2054007 : vec<data_reference_p> datarefs = vNULL;
10508 : 2054007 : auto_vec<int> dataref_groups;
10509 : 2054007 : int insns = 0;
10510 : 2054007 : int current_group = 0;
10511 : :
10512 : 13128761 : for (unsigned i = 0; i < bbs.length (); i++)
10513 : : {
10514 : 11074754 : basic_block bb = bbs[i];
10515 : 90756833 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
10516 : 79682079 : gsi_next (&gsi))
10517 : : {
10518 : 79682079 : gimple *stmt = gsi_stmt (gsi);
10519 : 79682079 : if (is_gimple_debug (stmt))
10520 : 49338524 : continue;
10521 : :
10522 : 30343555 : insns++;
10523 : :
10524 : 30343555 : if (gimple_location (stmt) != UNKNOWN_LOCATION)
10525 : 27264764 : vect_location = stmt;
10526 : :
10527 : 30343555 : if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs,
10528 : : &dataref_groups, current_group))
10529 : 5225908 : ++current_group;
10530 : : }
10531 : : /* New BBs always start a new DR group. */
10532 : 11074754 : ++current_group;
10533 : : }
10534 : :
10535 : 2054007 : return vect_slp_region (bbs, datarefs, &dataref_groups, insns, orig_loop);
10536 : 2054007 : }
10537 : :
10538 : : /* Special entry for the BB vectorizer. Analyze and transform a single
10539 : : if-converted BB with ORIG_LOOPs body being the not if-converted
10540 : : representation. Returns true if anything in the basic-block was
10541 : : vectorized. */
10542 : :
10543 : : bool
10544 : 16999 : vect_slp_if_converted_bb (basic_block bb, loop_p orig_loop)
10545 : : {
10546 : 16999 : auto_vec<basic_block> bbs;
10547 : 16999 : bbs.safe_push (bb);
10548 : 16999 : return vect_slp_bbs (bbs, orig_loop);
10549 : 16999 : }
10550 : :
10551 : : /* Main entry for the BB vectorizer. Analyze and transform BB, returns
10552 : : true if anything in the basic-block was vectorized. */
10553 : :
10554 : : bool
10555 : 915688 : vect_slp_function (function *fun)
10556 : : {
10557 : 915688 : bool r = false;
10558 : 915688 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
10559 : 915688 : auto_bitmap exit_bbs;
10560 : 915688 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
10561 : 915688 : edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
10562 : 915688 : unsigned n = rev_post_order_and_mark_dfs_back_seme (fun, entry, exit_bbs,
10563 : 915688 : true, rpo, NULL);
10564 : :
10565 : : /* For the moment split the function into pieces to avoid making
10566 : : the iteration on the vector mode moot. Split at points we know
10567 : : to not handle well which is CFG merges (SLP discovery doesn't
10568 : : handle non-loop-header PHIs) and loop exits. Since pattern
10569 : : recog requires reverse iteration to visit uses before defs
10570 : : simply chop RPO into pieces. */
10571 : 915688 : auto_vec<basic_block> bbs;
10572 : 12003679 : for (unsigned i = 0; i < n; i++)
10573 : : {
10574 : 11087991 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
10575 : 11087991 : bool split = false;
10576 : :
10577 : : /* Split when a BB is not dominated by the first block. */
10578 : 20898356 : if (!bbs.is_empty ()
10579 : 9810365 : && !dominated_by_p (CDI_DOMINATORS, bb, bbs[0]))
10580 : : {
10581 : 777758 : if (dump_enabled_p ())
10582 : 146 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10583 : : "splitting region at dominance boundary bb%d\n",
10584 : : bb->index);
10585 : : split = true;
10586 : : }
10587 : : /* Split when the loop determined by the first block
10588 : : is exited. This is because we eventually insert
10589 : : invariants at region begin. */
10590 : 19342840 : else if (!bbs.is_empty ()
10591 : 9032607 : && bbs[0]->loop_father != bb->loop_father
10592 : 2274470 : && !flow_loop_nested_p (bbs[0]->loop_father, bb->loop_father))
10593 : : {
10594 : 4725 : if (dump_enabled_p ())
10595 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10596 : : "splitting region at loop %d exit at bb%d\n",
10597 : 3 : bbs[0]->loop_father->num, bb->index);
10598 : : split = true;
10599 : : }
10600 : 10305508 : else if (!bbs.is_empty ()
10601 : 9027882 : && bb->loop_father->header == bb
10602 : 488248 : && bb->loop_father->dont_vectorize)
10603 : : {
10604 : 7135 : if (dump_enabled_p ())
10605 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10606 : : "splitting region at dont-vectorize loop %d "
10607 : : "entry at bb%d\n",
10608 : : bb->loop_father->num, bb->index);
10609 : : split = true;
10610 : : }
10611 : :
10612 : 11877609 : if (split && !bbs.is_empty ())
10613 : : {
10614 : 789618 : r |= vect_slp_bbs (bbs, NULL);
10615 : 789618 : bbs.truncate (0);
10616 : : }
10617 : :
10618 : 11087991 : if (bbs.is_empty ())
10619 : : {
10620 : : /* We need to be able to insert at the head of the region which
10621 : : we cannot for region starting with a returns-twice call. */
10622 : 2067244 : if (gcall *first = safe_dyn_cast <gcall *> (first_stmt (bb)))
10623 : 408935 : if (gimple_call_flags (first) & ECF_RETURNS_TWICE)
10624 : : {
10625 : 294 : if (dump_enabled_p ())
10626 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10627 : : "skipping bb%d as start of region as it "
10628 : : "starts with returns-twice call\n",
10629 : : bb->index);
10630 : 30236 : continue;
10631 : : }
10632 : : /* If the loop this BB belongs to is marked as not to be vectorized
10633 : : honor that also for BB vectorization. */
10634 : 2066950 : if (bb->loop_father->dont_vectorize)
10635 : 29942 : continue;
10636 : : }
10637 : :
10638 : 11057755 : bbs.safe_push (bb);
10639 : :
10640 : : /* When we have a stmt ending this block and defining a
10641 : : value we have to insert on edges when inserting after it for
10642 : : a vector containing its definition. Avoid this for now. */
10643 : 22115510 : if (gimple *last = *gsi_last_bb (bb))
10644 : 8962624 : if (gimple_get_lhs (last)
10645 : 8962624 : && is_ctrl_altering_stmt (last))
10646 : : {
10647 : 331709 : if (dump_enabled_p ())
10648 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10649 : : "splitting region at control altering "
10650 : : "definition %G", last);
10651 : 331709 : r |= vect_slp_bbs (bbs, NULL);
10652 : 331709 : bbs.truncate (0);
10653 : : }
10654 : : }
10655 : :
10656 : 915688 : if (!bbs.is_empty ())
10657 : 915681 : r |= vect_slp_bbs (bbs, NULL);
10658 : :
10659 : 915688 : free (rpo);
10660 : :
10661 : 915688 : return r;
10662 : 915688 : }
10663 : :
10664 : : /* Build a variable-length vector in which the elements in ELTS are repeated
10665 : : to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
10666 : : RESULTS and add any new instructions to SEQ.
10667 : :
10668 : : The approach we use is:
10669 : :
10670 : : (1) Find a vector mode VM with integer elements of mode IM.
10671 : :
10672 : : (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10673 : : ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
10674 : : from small vectors to IM.
10675 : :
10676 : : (3) Duplicate each ELTS'[I] into a vector of mode VM.
10677 : :
10678 : : (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
10679 : : correct byte contents.
10680 : :
10681 : : (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
10682 : :
10683 : : We try to find the largest IM for which this sequence works, in order
10684 : : to cut down on the number of interleaves. */
10685 : :
10686 : : void
10687 : 0 : duplicate_and_interleave (vec_info *vinfo, gimple_seq *seq, tree vector_type,
10688 : : const vec<tree> &elts, unsigned int nresults,
10689 : : vec<tree> &results)
10690 : : {
10691 : 0 : unsigned int nelts = elts.length ();
10692 : 0 : tree element_type = TREE_TYPE (vector_type);
10693 : :
10694 : : /* (1) Find a vector mode VM with integer elements of mode IM. */
10695 : 0 : unsigned int nvectors = 1;
10696 : 0 : tree new_vector_type;
10697 : 0 : tree permutes[2];
10698 : 0 : if (!can_duplicate_and_interleave_p (vinfo, nelts, element_type,
10699 : : &nvectors, &new_vector_type,
10700 : : permutes))
10701 : 0 : gcc_unreachable ();
10702 : :
10703 : : /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
10704 : 0 : unsigned int partial_nelts = nelts / nvectors;
10705 : 0 : tree partial_vector_type = build_vector_type (element_type, partial_nelts);
10706 : :
10707 : 0 : tree_vector_builder partial_elts;
10708 : 0 : auto_vec<tree, 32> pieces (nvectors * 2);
10709 : 0 : pieces.quick_grow_cleared (nvectors * 2);
10710 : 0 : for (unsigned int i = 0; i < nvectors; ++i)
10711 : : {
10712 : : /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10713 : : ELTS' has mode IM. */
10714 : 0 : partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
10715 : 0 : for (unsigned int j = 0; j < partial_nelts; ++j)
10716 : 0 : partial_elts.quick_push (elts[i * partial_nelts + j]);
10717 : 0 : tree t = gimple_build_vector (seq, &partial_elts);
10718 : 0 : t = gimple_build (seq, VIEW_CONVERT_EXPR,
10719 : 0 : TREE_TYPE (new_vector_type), t);
10720 : :
10721 : : /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
10722 : 0 : pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
10723 : : }
10724 : :
10725 : : /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
10726 : : correct byte contents.
10727 : :
10728 : : Conceptually, we need to repeat the following operation log2(nvectors)
10729 : : times, where hi_start = nvectors / 2:
10730 : :
10731 : : out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
10732 : : out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
10733 : :
10734 : : However, if each input repeats every N elements and the VF is
10735 : : a multiple of N * 2, the HI result is the same as the LO result.
10736 : : This will be true for the first N1 iterations of the outer loop,
10737 : : followed by N2 iterations for which both the LO and HI results
10738 : : are needed. I.e.:
10739 : :
10740 : : N1 + N2 = log2(nvectors)
10741 : :
10742 : : Each "N1 iteration" doubles the number of redundant vectors and the
10743 : : effect of the process as a whole is to have a sequence of nvectors/2**N1
10744 : : vectors that repeats 2**N1 times. Rather than generate these redundant
10745 : : vectors, we halve the number of vectors for each N1 iteration. */
10746 : : unsigned int in_start = 0;
10747 : : unsigned int out_start = nvectors;
10748 : : unsigned int new_nvectors = nvectors;
10749 : 0 : for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
10750 : : {
10751 : 0 : unsigned int hi_start = new_nvectors / 2;
10752 : 0 : unsigned int out_i = 0;
10753 : 0 : for (unsigned int in_i = 0; in_i < new_nvectors; ++in_i)
10754 : : {
10755 : 0 : if ((in_i & 1) != 0
10756 : 0 : && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
10757 : : 2 * in_repeat))
10758 : 0 : continue;
10759 : :
10760 : 0 : tree output = make_ssa_name (new_vector_type);
10761 : 0 : tree input1 = pieces[in_start + (in_i / 2)];
10762 : 0 : tree input2 = pieces[in_start + (in_i / 2) + hi_start];
10763 : 0 : gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
10764 : : input1, input2,
10765 : : permutes[in_i & 1]);
10766 : 0 : gimple_seq_add_stmt (seq, stmt);
10767 : 0 : pieces[out_start + out_i] = output;
10768 : 0 : out_i += 1;
10769 : : }
10770 : 0 : std::swap (in_start, out_start);
10771 : 0 : new_nvectors = out_i;
10772 : : }
10773 : :
10774 : : /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
10775 : 0 : results.reserve (nresults);
10776 : 0 : for (unsigned int i = 0; i < nresults; ++i)
10777 : 0 : if (i < new_nvectors)
10778 : 0 : results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
10779 : 0 : pieces[in_start + i]));
10780 : : else
10781 : 0 : results.quick_push (results[i - new_nvectors]);
10782 : 0 : }
10783 : :
10784 : :
10785 : : /* For constant and loop invariant defs in OP_NODE this function creates
10786 : : vector defs that will be used in the vectorized stmts and stores them
10787 : : to SLP_TREE_VEC_DEFS of OP_NODE. */
10788 : :
10789 : : static void
10790 : 493745 : vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
10791 : : {
10792 : 493745 : unsigned HOST_WIDE_INT nunits;
10793 : 493745 : tree vec_cst;
10794 : 493745 : unsigned j, number_of_places_left_in_vector;
10795 : 493745 : tree vector_type;
10796 : 493745 : tree vop;
10797 : 493745 : int group_size = op_node->ops.length ();
10798 : 493745 : unsigned int vec_num, i;
10799 : 493745 : unsigned number_of_copies = 1;
10800 : 493745 : bool constant_p;
10801 : 493745 : gimple_seq ctor_seq = NULL;
10802 : 493745 : auto_vec<tree, 16> permute_results;
10803 : :
10804 : : /* We always want SLP_TREE_VECTYPE (op_node) here correctly set. */
10805 : 493745 : vector_type = SLP_TREE_VECTYPE (op_node);
10806 : :
10807 : 493745 : unsigned int number_of_vectors = vect_get_num_copies (vinfo, op_node);
10808 : 493745 : SLP_TREE_VEC_DEFS (op_node).create (number_of_vectors);
10809 : 493745 : auto_vec<tree> voprnds (number_of_vectors);
10810 : :
10811 : : /* NUMBER_OF_COPIES is the number of times we need to use the same values in
10812 : : created vectors. It is greater than 1 if unrolling is performed.
10813 : :
10814 : : For example, we have two scalar operands, s1 and s2 (e.g., group of
10815 : : strided accesses of size two), while NUNITS is four (i.e., four scalars
10816 : : of this type can be packed in a vector). The output vector will contain
10817 : : two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
10818 : : will be 2).
10819 : :
10820 : : If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
10821 : : containing the operands.
10822 : :
10823 : : For example, NUNITS is four as before, and the group size is 8
10824 : : (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
10825 : : {s5, s6, s7, s8}. */
10826 : :
10827 : : /* When using duplicate_and_interleave, we just need one element for
10828 : : each scalar statement. */
10829 : 493745 : if (!TYPE_VECTOR_SUBPARTS (vector_type).is_constant (&nunits))
10830 : : nunits = group_size;
10831 : :
10832 : 493745 : number_of_copies = nunits * number_of_vectors / group_size;
10833 : :
10834 : 493745 : number_of_places_left_in_vector = nunits;
10835 : 493745 : constant_p = true;
10836 : 493745 : tree uniform_elt = NULL_TREE;
10837 : 493745 : tree_vector_builder elts (vector_type, nunits, 1);
10838 : 493745 : elts.quick_grow (nunits);
10839 : 493745 : stmt_vec_info insert_after = NULL;
10840 : 1472004 : for (j = 0; j < number_of_copies; j++)
10841 : : {
10842 : 978259 : tree op;
10843 : 3752180 : for (i = group_size - 1; op_node->ops.iterate (i, &op); i--)
10844 : : {
10845 : : /* Create 'vect_ = {op0,op1,...,opn}'. */
10846 : 1795662 : tree orig_op = op;
10847 : 1795662 : if (number_of_places_left_in_vector == nunits)
10848 : : uniform_elt = op;
10849 : 1172349 : else if (uniform_elt && operand_equal_p (uniform_elt, op))
10850 : 745261 : op = elts[number_of_places_left_in_vector];
10851 : : else
10852 : : uniform_elt = NULL_TREE;
10853 : 1795662 : number_of_places_left_in_vector--;
10854 : 1795662 : if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
10855 : : {
10856 : 284058 : if (CONSTANT_CLASS_P (op))
10857 : : {
10858 : 103654 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10859 : : {
10860 : : /* Can't use VIEW_CONVERT_EXPR for booleans because
10861 : : of possibly different sizes of scalar value and
10862 : : vector element. */
10863 : 58 : if (integer_zerop (op))
10864 : 58 : op = build_int_cst (TREE_TYPE (vector_type), 0);
10865 : 0 : else if (integer_onep (op))
10866 : 0 : op = build_all_ones_cst (TREE_TYPE (vector_type));
10867 : : else
10868 : 0 : gcc_unreachable ();
10869 : : }
10870 : : else
10871 : 103596 : op = fold_unary (VIEW_CONVERT_EXPR,
10872 : : TREE_TYPE (vector_type), op);
10873 : 103654 : gcc_assert (op && CONSTANT_CLASS_P (op));
10874 : : }
10875 : : else
10876 : : {
10877 : 180404 : tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
10878 : 180404 : gimple *init_stmt;
10879 : 180404 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10880 : : {
10881 : 403 : tree true_val
10882 : 403 : = build_all_ones_cst (TREE_TYPE (vector_type));
10883 : 403 : tree false_val
10884 : 403 : = build_zero_cst (TREE_TYPE (vector_type));
10885 : 403 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
10886 : 403 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
10887 : : op, true_val,
10888 : : false_val);
10889 : : }
10890 : : else
10891 : : {
10892 : 180001 : op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
10893 : : op);
10894 : 180001 : init_stmt
10895 : 180001 : = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
10896 : : op);
10897 : : }
10898 : 180404 : gimple_seq_add_stmt (&ctor_seq, init_stmt);
10899 : 180404 : op = new_temp;
10900 : : }
10901 : : }
10902 : 1795662 : elts[number_of_places_left_in_vector] = op;
10903 : 1795662 : if (!CONSTANT_CLASS_P (op))
10904 : 326387 : constant_p = false;
10905 : : /* For BB vectorization we have to compute an insert location
10906 : : when a def is inside the analyzed region since we cannot
10907 : : simply insert at the BB start in this case. */
10908 : 1795662 : stmt_vec_info opdef;
10909 : 1795662 : if (TREE_CODE (orig_op) == SSA_NAME
10910 : 187877 : && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
10911 : 168243 : && is_a <bb_vec_info> (vinfo)
10912 : 1907470 : && (opdef = vinfo->lookup_def (orig_op)))
10913 : : {
10914 : 88619 : if (!insert_after)
10915 : : insert_after = opdef;
10916 : : else
10917 : 48946 : insert_after = get_later_stmt (insert_after, opdef);
10918 : : }
10919 : :
10920 : 1795662 : if (number_of_places_left_in_vector == 0)
10921 : : {
10922 : 623313 : auto type_nunits = TYPE_VECTOR_SUBPARTS (vector_type);
10923 : 623313 : if (uniform_elt)
10924 : 649608 : vec_cst = gimple_build_vector_from_val (&ctor_seq, vector_type,
10925 : 324804 : elts[0]);
10926 : 597018 : else if (constant_p
10927 : 597018 : ? multiple_p (type_nunits, nunits)
10928 : 114381 : : known_eq (type_nunits, nunits))
10929 : 298509 : vec_cst = gimple_build_vector (&ctor_seq, &elts);
10930 : : else
10931 : : {
10932 : 0 : if (permute_results.is_empty ())
10933 : 0 : duplicate_and_interleave (vinfo, &ctor_seq, vector_type,
10934 : : elts, number_of_vectors,
10935 : : permute_results);
10936 : 0 : vec_cst = permute_results[number_of_vectors - j - 1];
10937 : : }
10938 : 623313 : if (!gimple_seq_empty_p (ctor_seq))
10939 : : {
10940 : 141600 : if (insert_after)
10941 : : {
10942 : 39673 : gimple_stmt_iterator gsi;
10943 : 39673 : if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
10944 : : {
10945 : 724 : gsi = gsi_after_labels (gimple_bb (insert_after->stmt));
10946 : 724 : gsi_insert_seq_before (&gsi, ctor_seq,
10947 : : GSI_CONTINUE_LINKING);
10948 : : }
10949 : 38949 : else if (!stmt_ends_bb_p (insert_after->stmt))
10950 : : {
10951 : 38949 : gsi = gsi_for_stmt (insert_after->stmt);
10952 : 38949 : gsi_insert_seq_after (&gsi, ctor_seq,
10953 : : GSI_CONTINUE_LINKING);
10954 : : }
10955 : : else
10956 : : {
10957 : : /* When we want to insert after a def where the
10958 : : defining stmt throws then insert on the fallthru
10959 : : edge. */
10960 : 0 : edge e = find_fallthru_edge
10961 : 0 : (gimple_bb (insert_after->stmt)->succs);
10962 : 0 : basic_block new_bb
10963 : 0 : = gsi_insert_seq_on_edge_immediate (e, ctor_seq);
10964 : 0 : gcc_assert (!new_bb);
10965 : : }
10966 : : }
10967 : : else
10968 : 101927 : vinfo->insert_seq_on_entry (NULL, ctor_seq);
10969 : 141600 : ctor_seq = NULL;
10970 : : }
10971 : 623313 : voprnds.quick_push (vec_cst);
10972 : 623313 : insert_after = NULL;
10973 : 623313 : number_of_places_left_in_vector = nunits;
10974 : 623313 : constant_p = true;
10975 : 623313 : elts.new_vector (vector_type, nunits, 1);
10976 : 623313 : elts.quick_grow (nunits);
10977 : : }
10978 : : }
10979 : : }
10980 : :
10981 : : /* Since the vectors are created in the reverse order, we should invert
10982 : : them. */
10983 : 493745 : vec_num = voprnds.length ();
10984 : 1117058 : for (j = vec_num; j != 0; j--)
10985 : : {
10986 : 623313 : vop = voprnds[j - 1];
10987 : 623313 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10988 : : }
10989 : :
10990 : : /* In case that VF is greater than the unrolling factor needed for the SLP
10991 : : group of stmts, NUMBER_OF_VECTORS to be created is greater than
10992 : : NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
10993 : : to replicate the vectors. */
10994 : 493745 : while (number_of_vectors > SLP_TREE_VEC_DEFS (op_node).length ())
10995 : 493745 : for (i = 0; SLP_TREE_VEC_DEFS (op_node).iterate (i, &vop) && i < vec_num;
10996 : : i++)
10997 : 0 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10998 : 493745 : }
10999 : :
11000 : : /* Get the scalar definition of the Nth lane from SLP_NODE or NULL_TREE
11001 : : if there is no definition for it in the scalar IL or it is not known. */
11002 : :
11003 : : tree
11004 : 1837 : vect_get_slp_scalar_def (slp_tree slp_node, unsigned n)
11005 : : {
11006 : 1837 : if (SLP_TREE_DEF_TYPE (slp_node) == vect_internal_def)
11007 : : {
11008 : 1827 : if (!SLP_TREE_SCALAR_STMTS (slp_node).exists ())
11009 : : return NULL_TREE;
11010 : 1827 : stmt_vec_info def = SLP_TREE_SCALAR_STMTS (slp_node)[n];
11011 : 1827 : if (!def)
11012 : : return NULL_TREE;
11013 : 1827 : return gimple_get_lhs (STMT_VINFO_STMT (def));
11014 : : }
11015 : : else
11016 : 10 : return SLP_TREE_SCALAR_OPS (slp_node)[n];
11017 : : }
11018 : :
11019 : : /* Get the Ith vectorized definition from SLP_NODE. */
11020 : :
11021 : : tree
11022 : 140468 : vect_get_slp_vect_def (slp_tree slp_node, unsigned i)
11023 : : {
11024 : 140468 : return SLP_TREE_VEC_DEFS (slp_node)[i];
11025 : : }
11026 : :
11027 : : /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS. */
11028 : :
11029 : : void
11030 : 923547 : vect_get_slp_defs (slp_tree slp_node, vec<tree> *vec_defs)
11031 : : {
11032 : 1847094 : vec_defs->create (SLP_TREE_VEC_DEFS (slp_node).length ());
11033 : 923547 : vec_defs->splice (SLP_TREE_VEC_DEFS (slp_node));
11034 : 923547 : }
11035 : :
11036 : : /* Get N vectorized definitions for SLP_NODE. */
11037 : :
11038 : : void
11039 : 2940 : vect_get_slp_defs (vec_info *,
11040 : : slp_tree slp_node, vec<vec<tree> > *vec_oprnds, unsigned n)
11041 : : {
11042 : 2940 : if (n == -1U)
11043 : 2940 : n = SLP_TREE_CHILDREN (slp_node).length ();
11044 : :
11045 : 10384 : for (unsigned i = 0; i < n; ++i)
11046 : : {
11047 : 7444 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[i];
11048 : 7444 : vec<tree> vec_defs = vNULL;
11049 : 7444 : vect_get_slp_defs (child, &vec_defs);
11050 : 7444 : vec_oprnds->quick_push (vec_defs);
11051 : : }
11052 : 2940 : }
11053 : :
11054 : : /* A subroutine of vect_transform_slp_perm_load with two extra arguments:
11055 : : - PERM gives the permutation that the caller wants to use for NODE,
11056 : : which might be different from SLP_LOAD_PERMUTATION.
11057 : : - DUMP_P controls whether the function dumps information. */
11058 : :
11059 : : static bool
11060 : 113065 : vect_transform_slp_perm_load_1 (vec_info *vinfo, slp_tree node,
11061 : : load_permutation_t &perm,
11062 : : const vec<tree> &dr_chain,
11063 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
11064 : : bool analyze_only, bool dump_p,
11065 : : unsigned *n_perms, unsigned int *n_loads,
11066 : : bool dce_chain)
11067 : : {
11068 : 113065 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
11069 : 113065 : int vec_index = 0;
11070 : 113065 : tree vectype = SLP_TREE_VECTYPE (node);
11071 : 113065 : unsigned int group_size = SLP_TREE_SCALAR_STMTS (node).length ();
11072 : 113065 : unsigned int mask_element;
11073 : 113065 : unsigned dr_group_size;
11074 : 113065 : machine_mode mode;
11075 : :
11076 : 113065 : if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
11077 : : {
11078 : : /* We have both splats of the same non-grouped load and groups
11079 : : of distinct invariant loads entering here. */
11080 : 1007 : unsigned max_idx = 0;
11081 : 5587 : for (auto idx : perm)
11082 : 2566 : max_idx = idx > max_idx ? idx : max_idx;
11083 : 1007 : dr_group_size = max_idx + 1;
11084 : : }
11085 : : else
11086 : : {
11087 : 112058 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
11088 : 112058 : dr_group_size = DR_GROUP_SIZE (stmt_info);
11089 : : }
11090 : :
11091 : 113065 : mode = TYPE_MODE (vectype);
11092 : 113065 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11093 : 113065 : unsigned int nstmts = vect_get_num_copies (vinfo, node);
11094 : :
11095 : : /* Initialize the vect stmts of NODE to properly insert the generated
11096 : : stmts later. */
11097 : 113065 : if (! analyze_only)
11098 : 52866 : for (unsigned i = SLP_TREE_VEC_DEFS (node).length (); i < nstmts; i++)
11099 : 20204 : SLP_TREE_VEC_DEFS (node).quick_push (NULL_TREE);
11100 : :
11101 : : /* Generate permutation masks for every NODE. Number of masks for each NODE
11102 : : is equal to GROUP_SIZE.
11103 : : E.g., we have a group of three nodes with three loads from the same
11104 : : location in each node, and the vector size is 4. I.e., we have a
11105 : : a0b0c0a1b1c1... sequence and we need to create the following vectors:
11106 : : for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
11107 : : for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
11108 : : ...
11109 : :
11110 : : The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
11111 : : The last mask is illegal since we assume two operands for permute
11112 : : operation, and the mask element values can't be outside that range.
11113 : : Hence, the last mask must be converted into {2,5,5,5}.
11114 : : For the first two permutations we need the first and the second input
11115 : : vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
11116 : : we need the second and the third vectors: {b1,c1,a2,b2} and
11117 : : {c2,a3,b3,c3}. */
11118 : :
11119 : 113065 : int vect_stmts_counter = 0;
11120 : 113065 : unsigned int index = 0;
11121 : 113065 : int first_vec_index = -1;
11122 : 113065 : int second_vec_index = -1;
11123 : 113065 : bool noop_p = true;
11124 : 113065 : *n_perms = 0;
11125 : :
11126 : 113065 : vec_perm_builder mask;
11127 : 113065 : unsigned int nelts_to_build;
11128 : 113065 : unsigned int nvectors_per_build;
11129 : 113065 : unsigned int in_nlanes;
11130 : 113065 : bool repeating_p = (group_size == dr_group_size
11131 : 144417 : && multiple_p (nunits, group_size));
11132 : 113065 : if (repeating_p)
11133 : : {
11134 : : /* A single vector contains a whole number of copies of the node, so:
11135 : : (a) all permutes can use the same mask; and
11136 : : (b) the permutes only need a single vector input. */
11137 : 29214 : mask.new_vector (nunits, group_size, 3);
11138 : 29214 : nelts_to_build = mask.encoded_nelts ();
11139 : : /* It's possible to obtain zero nstmts during analyze_only, so make
11140 : : it at least one to ensure the later computation for n_perms
11141 : : proceed. */
11142 : 29214 : nvectors_per_build = nstmts > 0 ? nstmts : 1;
11143 : 29214 : in_nlanes = dr_group_size * 3;
11144 : : }
11145 : : else
11146 : : {
11147 : : /* We need to construct a separate mask for each vector statement. */
11148 : 83851 : unsigned HOST_WIDE_INT const_nunits, const_vf;
11149 : 83851 : if (!nunits.is_constant (&const_nunits)
11150 : 83851 : || !vf.is_constant (&const_vf))
11151 : : return false;
11152 : 83851 : mask.new_vector (const_nunits, const_nunits, 1);
11153 : 83851 : nelts_to_build = const_vf * group_size;
11154 : 83851 : nvectors_per_build = 1;
11155 : 83851 : in_nlanes = const_vf * dr_group_size;
11156 : : }
11157 : 113065 : auto_sbitmap used_in_lanes (in_nlanes);
11158 : 113065 : bitmap_clear (used_in_lanes);
11159 : 113065 : auto_bitmap used_defs;
11160 : :
11161 : 113065 : unsigned int count = mask.encoded_nelts ();
11162 : 113065 : mask.quick_grow (count);
11163 : 113065 : vec_perm_indices indices;
11164 : :
11165 : 609111 : for (unsigned int j = 0; j < nelts_to_build; j++)
11166 : : {
11167 : 505870 : unsigned int iter_num = j / group_size;
11168 : 505870 : unsigned int stmt_num = j % group_size;
11169 : 505870 : unsigned int i = (iter_num * dr_group_size + perm[stmt_num]);
11170 : 505870 : bitmap_set_bit (used_in_lanes, i);
11171 : 505870 : if (repeating_p)
11172 : : {
11173 : : first_vec_index = 0;
11174 : : mask_element = i;
11175 : : }
11176 : : else
11177 : : {
11178 : : /* Enforced before the loop when !repeating_p. */
11179 : 317380 : unsigned int const_nunits = nunits.to_constant ();
11180 : 317380 : vec_index = i / const_nunits;
11181 : 317380 : mask_element = i % const_nunits;
11182 : 317380 : if (vec_index == first_vec_index
11183 : 317380 : || first_vec_index == -1)
11184 : : {
11185 : : first_vec_index = vec_index;
11186 : : }
11187 : 128009 : else if (vec_index == second_vec_index
11188 : 128009 : || second_vec_index == -1)
11189 : : {
11190 : 121580 : second_vec_index = vec_index;
11191 : 121580 : mask_element += const_nunits;
11192 : : }
11193 : : else
11194 : : {
11195 : 6429 : if (dump_p)
11196 : 232 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11197 : : "permutation requires at "
11198 : : "least three vectors %G",
11199 : : stmt_info->stmt);
11200 : 6429 : gcc_assert (analyze_only);
11201 : : return false;
11202 : : }
11203 : :
11204 : 310951 : gcc_assert (mask_element < 2 * const_nunits);
11205 : : }
11206 : :
11207 : 499441 : if (mask_element != index)
11208 : 324004 : noop_p = false;
11209 : 499441 : mask[index++] = mask_element;
11210 : :
11211 : 499441 : if (index == count)
11212 : : {
11213 : 130238 : if (!noop_p)
11214 : : {
11215 : 181372 : indices.new_vector (mask, second_vec_index == -1 ? 1 : 2, nunits);
11216 : 107958 : if (!can_vec_perm_const_p (mode, mode, indices))
11217 : : {
11218 : 3395 : if (dump_p)
11219 : : {
11220 : 79 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11221 : : "unsupported vect permute { ");
11222 : 669 : for (i = 0; i < count; ++i)
11223 : : {
11224 : 590 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
11225 : 590 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
11226 : : }
11227 : 79 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
11228 : : }
11229 : 3395 : gcc_assert (analyze_only);
11230 : : return false;
11231 : : }
11232 : :
11233 : 104563 : tree mask_vec = NULL_TREE;
11234 : 104563 : if (!analyze_only)
11235 : 18740 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
11236 : :
11237 : 104563 : if (second_vec_index == -1)
11238 : 32671 : second_vec_index = first_vec_index;
11239 : :
11240 : 211126 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
11241 : : {
11242 : 106563 : ++*n_perms;
11243 : 106563 : if (analyze_only)
11244 : 87540 : continue;
11245 : : /* Generate the permute statement if necessary. */
11246 : 19023 : tree first_vec = dr_chain[first_vec_index + ri];
11247 : 19023 : tree second_vec = dr_chain[second_vec_index + ri];
11248 : 19023 : gassign *stmt = as_a<gassign *> (stmt_info->stmt);
11249 : 19023 : tree perm_dest
11250 : 19023 : = vect_create_destination_var (gimple_assign_lhs (stmt),
11251 : : vectype);
11252 : 19023 : perm_dest = make_ssa_name (perm_dest);
11253 : 19023 : gimple *perm_stmt
11254 : 19023 : = gimple_build_assign (perm_dest, VEC_PERM_EXPR, first_vec,
11255 : : second_vec, mask_vec);
11256 : 19023 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
11257 : : gsi);
11258 : 19023 : if (dce_chain)
11259 : : {
11260 : 18246 : bitmap_set_bit (used_defs, first_vec_index + ri);
11261 : 18246 : bitmap_set_bit (used_defs, second_vec_index + ri);
11262 : : }
11263 : :
11264 : : /* Store the vector statement in NODE. */
11265 : 19023 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = perm_dest;
11266 : : }
11267 : : }
11268 : 22280 : else if (!analyze_only)
11269 : : {
11270 : 2362 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
11271 : : {
11272 : 1181 : tree first_vec = dr_chain[first_vec_index + ri];
11273 : : /* If mask was NULL_TREE generate the requested
11274 : : identity transform. */
11275 : 1181 : if (dce_chain)
11276 : 1180 : bitmap_set_bit (used_defs, first_vec_index + ri);
11277 : :
11278 : : /* Store the vector statement in NODE. */
11279 : 1181 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = first_vec;
11280 : : }
11281 : : }
11282 : :
11283 : : index = 0;
11284 : : first_vec_index = -1;
11285 : : second_vec_index = -1;
11286 : : noop_p = true;
11287 : : }
11288 : : }
11289 : :
11290 : 103241 : if (n_loads)
11291 : : {
11292 : 0 : if (repeating_p)
11293 : 0 : *n_loads = nstmts;
11294 : : else
11295 : : {
11296 : : /* Enforced above when !repeating_p. */
11297 : 0 : unsigned int const_nunits = nunits.to_constant ();
11298 : 0 : *n_loads = 0;
11299 : 0 : bool load_seen = false;
11300 : 0 : for (unsigned i = 0; i < in_nlanes; ++i)
11301 : : {
11302 : 0 : if (i % const_nunits == 0)
11303 : : {
11304 : 0 : if (load_seen)
11305 : 0 : *n_loads += 1;
11306 : : load_seen = false;
11307 : : }
11308 : 0 : if (bitmap_bit_p (used_in_lanes, i))
11309 : 0 : load_seen = true;
11310 : : }
11311 : 0 : if (load_seen)
11312 : 0 : *n_loads += 1;
11313 : : }
11314 : : }
11315 : :
11316 : 103241 : if (dce_chain)
11317 : 160878 : for (unsigned i = 0; i < dr_chain.length (); ++i)
11318 : 32111 : if (!bitmap_bit_p (used_defs, i))
11319 : : {
11320 : 2373 : tree def = dr_chain[i];
11321 : 2433 : do
11322 : : {
11323 : 2433 : gimple *stmt = SSA_NAME_DEF_STMT (def);
11324 : 2433 : if (is_gimple_assign (stmt)
11325 : 2433 : && (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
11326 : 2433 : || gimple_assign_rhs_code (stmt) == CONSTRUCTOR))
11327 : 398 : def = single_ssa_tree_operand (stmt, SSA_OP_USE);
11328 : : else
11329 : : def = NULL;
11330 : 2433 : gimple_stmt_iterator rgsi = gsi_for_stmt (stmt);
11331 : 2433 : gsi_remove (&rgsi, true);
11332 : 2433 : release_defs (stmt);
11333 : : }
11334 : 2433 : while (def);
11335 : : }
11336 : :
11337 : : return true;
11338 : 113065 : }
11339 : :
11340 : : /* Generate vector permute statements from a list of loads in DR_CHAIN.
11341 : : If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
11342 : : permute statements for the SLP node NODE. Store the number of vector
11343 : : permute instructions in *N_PERMS and the number of vector load
11344 : : instructions in *N_LOADS. If DCE_CHAIN is true, remove all definitions
11345 : : that were not needed. */
11346 : :
11347 : : bool
11348 : 78189 : vect_transform_slp_perm_load (vec_info *vinfo,
11349 : : slp_tree node, const vec<tree> &dr_chain,
11350 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
11351 : : bool analyze_only, unsigned *n_perms,
11352 : : unsigned int *n_loads, bool dce_chain)
11353 : : {
11354 : 78189 : return vect_transform_slp_perm_load_1 (vinfo, node,
11355 : 78189 : SLP_TREE_LOAD_PERMUTATION (node),
11356 : : dr_chain, gsi, vf, analyze_only,
11357 : : dump_enabled_p (), n_perms, n_loads,
11358 : 78189 : dce_chain);
11359 : : }
11360 : :
11361 : : /* Produce the next vector result for SLP permutation NODE by adding a vector
11362 : : statement at GSI. If MASK_VEC is nonnull, add:
11363 : :
11364 : : <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
11365 : :
11366 : : otherwise add:
11367 : :
11368 : : <new SSA name> = FIRST_DEF. */
11369 : :
11370 : : static void
11371 : 30249 : vect_add_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
11372 : : slp_tree node, tree first_def, tree second_def,
11373 : : tree mask_vec, poly_uint64 identity_offset)
11374 : : {
11375 : 30249 : tree vectype = SLP_TREE_VECTYPE (node);
11376 : :
11377 : : /* ??? We SLP match existing vector element extracts but
11378 : : allow punning which we need to re-instantiate at uses
11379 : : but have no good way of explicitly representing. */
11380 : 30249 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)), TYPE_SIZE (vectype))
11381 : 30249 : && !types_compatible_p (TREE_TYPE (first_def), vectype))
11382 : : {
11383 : 15 : gassign *conv_stmt
11384 : 15 : = gimple_build_assign (make_ssa_name (vectype),
11385 : : build1 (VIEW_CONVERT_EXPR, vectype, first_def));
11386 : 15 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
11387 : 15 : first_def = gimple_assign_lhs (conv_stmt);
11388 : : }
11389 : 30249 : gassign *perm_stmt;
11390 : 30249 : tree perm_dest = make_ssa_name (vectype);
11391 : 30249 : if (mask_vec)
11392 : : {
11393 : 27216 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)),
11394 : 27216 : TYPE_SIZE (vectype))
11395 : 27216 : && !types_compatible_p (TREE_TYPE (second_def), vectype))
11396 : : {
11397 : 8 : gassign *conv_stmt
11398 : 8 : = gimple_build_assign (make_ssa_name (vectype),
11399 : : build1 (VIEW_CONVERT_EXPR,
11400 : : vectype, second_def));
11401 : 8 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
11402 : 8 : second_def = gimple_assign_lhs (conv_stmt);
11403 : : }
11404 : 27216 : perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
11405 : : first_def, second_def,
11406 : : mask_vec);
11407 : : }
11408 : 3033 : else if (!types_compatible_p (TREE_TYPE (first_def), vectype))
11409 : : {
11410 : : /* For identity permutes we still need to handle the case
11411 : : of offsetted extracts or concats. */
11412 : 237 : unsigned HOST_WIDE_INT c;
11413 : 237 : auto first_def_nunits
11414 : 237 : = TYPE_VECTOR_SUBPARTS (TREE_TYPE (first_def));
11415 : 237 : if (known_le (TYPE_VECTOR_SUBPARTS (vectype), first_def_nunits))
11416 : : {
11417 : 233 : unsigned HOST_WIDE_INT elsz
11418 : 233 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (first_def))));
11419 : 466 : tree lowpart = build3 (BIT_FIELD_REF, vectype, first_def,
11420 : 233 : TYPE_SIZE (vectype),
11421 : 233 : bitsize_int (identity_offset * elsz));
11422 : 233 : perm_stmt = gimple_build_assign (perm_dest, lowpart);
11423 : : }
11424 : 4 : else if (constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
11425 : 4 : first_def_nunits, &c) && c == 2)
11426 : : {
11427 : 4 : tree ctor = build_constructor_va (vectype, 2, NULL_TREE, first_def,
11428 : : NULL_TREE, second_def);
11429 : 4 : perm_stmt = gimple_build_assign (perm_dest, ctor);
11430 : : }
11431 : : else
11432 : 0 : gcc_unreachable ();
11433 : : }
11434 : : else
11435 : : {
11436 : : /* We need a copy here in case the def was external. */
11437 : 2796 : perm_stmt = gimple_build_assign (perm_dest, first_def);
11438 : : }
11439 : 30249 : vect_finish_stmt_generation (vinfo, NULL, perm_stmt, gsi);
11440 : : /* Store the vector statement in NODE. */
11441 : 30249 : node->push_vec_def (perm_stmt);
11442 : 30249 : }
11443 : :
11444 : : /* Subroutine of vectorizable_slp_permutation. Check whether the target
11445 : : can perform permutation PERM on the (1 or 2) input nodes in CHILDREN.
11446 : : If GSI is nonnull, emit the permutation there.
11447 : :
11448 : : When GSI is null, the only purpose of NODE is to give properties
11449 : : of the result, such as the vector type and number of SLP lanes.
11450 : : The node does not need to be a VEC_PERM_EXPR.
11451 : :
11452 : : If the target supports the operation, return the number of individual
11453 : : VEC_PERM_EXPRs needed, otherwise return -1. Print information to the
11454 : : dump file if DUMP_P is true. */
11455 : :
11456 : : static int
11457 : 433952 : vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
11458 : : slp_tree node, lane_permutation_t &perm,
11459 : : vec<slp_tree> &children, bool dump_p)
11460 : : {
11461 : 433952 : tree vectype = SLP_TREE_VECTYPE (node);
11462 : :
11463 : : /* ??? We currently only support all same vector input types
11464 : : while the SLP IL should really do a concat + select and thus accept
11465 : : arbitrary mismatches. */
11466 : 433952 : slp_tree child;
11467 : 433952 : unsigned i;
11468 : 433952 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
11469 : 433952 : bool repeating_p = multiple_p (nunits, SLP_TREE_LANES (node));
11470 : : /* True if we're permuting a single input of 2N vectors down
11471 : : to N vectors. This case doesn't generalize beyond 2 since
11472 : : VEC_PERM_EXPR only takes 2 inputs. */
11473 : 433952 : bool pack_p = false;
11474 : : /* If we're permuting inputs of N vectors each into X*N outputs,
11475 : : this is the value of X, otherwise it is 1. */
11476 : 433952 : unsigned int unpack_factor = 1;
11477 : 433952 : tree op_vectype = NULL_TREE;
11478 : 435123 : FOR_EACH_VEC_ELT (children, i, child)
11479 : 435044 : if (SLP_TREE_VECTYPE (child))
11480 : : {
11481 : : op_vectype = SLP_TREE_VECTYPE (child);
11482 : : break;
11483 : : }
11484 : 433952 : if (!op_vectype)
11485 : 79 : op_vectype = vectype;
11486 : 926736 : FOR_EACH_VEC_ELT (children, i, child)
11487 : : {
11488 : 492784 : if ((SLP_TREE_DEF_TYPE (child) != vect_internal_def
11489 : 9643 : && !vect_maybe_update_slp_op_vectype (child, op_vectype))
11490 : 492784 : || !types_compatible_p (SLP_TREE_VECTYPE (child), op_vectype)
11491 : 985568 : || !types_compatible_p (TREE_TYPE (vectype), TREE_TYPE (op_vectype)))
11492 : : {
11493 : 0 : if (dump_p)
11494 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11495 : : "Unsupported vector types in lane permutation\n");
11496 : 0 : return -1;
11497 : : }
11498 : 492784 : auto op_nunits = TYPE_VECTOR_SUBPARTS (op_vectype);
11499 : 492784 : unsigned int this_unpack_factor;
11500 : : /* Detect permutations of external, pre-existing vectors. The external
11501 : : node's SLP_TREE_LANES stores the total number of units in the vector,
11502 : : or zero if the vector has variable length.
11503 : :
11504 : : We are expected to keep the original VEC_PERM_EXPR for such cases.
11505 : : There is no repetition to model. */
11506 : 492784 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def
11507 : 492784 : && SLP_TREE_SCALAR_OPS (child).is_empty ())
11508 : : repeating_p = false;
11509 : : /* Check whether the input has twice as many lanes per vector. */
11510 : 485230 : else if (children.length () == 1
11511 : 485230 : && known_eq (SLP_TREE_LANES (child) * nunits,
11512 : : SLP_TREE_LANES (node) * op_nunits * 2))
11513 : : pack_p = true;
11514 : : /* Check whether the output has N times as many lanes per vector. */
11515 : 492784 : else if (constant_multiple_p (SLP_TREE_LANES (node) * op_nunits,
11516 : 443130 : SLP_TREE_LANES (child) * nunits,
11517 : : &this_unpack_factor)
11518 : 408523 : && (i == 0 || unpack_factor == this_unpack_factor))
11519 : : unpack_factor = this_unpack_factor;
11520 : : else
11521 : : repeating_p = false;
11522 : : }
11523 : :
11524 : 867904 : gcc_assert (perm.length () == SLP_TREE_LANES (node));
11525 : :
11526 : : /* Load-lanes permute. This permute only acts as a forwarder to
11527 : : select the correct vector def of the load-lanes load which
11528 : : has the permuted vectors in its vector defs like
11529 : : { v0, w0, r0, v1, w1, r1 ... } for a ld3. All costs are
11530 : : accounted for in the costing for the actual load so we
11531 : : return zero here. */
11532 : 433952 : if (node->ldst_lanes)
11533 : : {
11534 : 0 : gcc_assert (children.length () == 1);
11535 : 0 : if (!gsi)
11536 : : /* This is a trivial op always supported. */
11537 : : return 0;
11538 : 0 : slp_tree child = children[0];
11539 : 0 : unsigned vec_idx = (SLP_TREE_LANE_PERMUTATION (node)[0].second
11540 : 0 : / SLP_TREE_LANES (node));
11541 : 0 : unsigned vec_num = SLP_TREE_LANES (child) / SLP_TREE_LANES (node);
11542 : 0 : unsigned nvectors = vect_get_num_copies (vinfo, node);
11543 : 0 : for (unsigned i = 0; i < nvectors; ++i)
11544 : : {
11545 : 0 : tree def = SLP_TREE_VEC_DEFS (child)[i * vec_num + vec_idx];
11546 : 0 : node->push_vec_def (def);
11547 : : }
11548 : : return 0;
11549 : : }
11550 : :
11551 : : /* Set REPEATING_P to true if the permutations are cyclical wrt UNPACK_FACTOR
11552 : : and if we can generate the vectors in a vector-length agnostic way.
11553 : : This requires UNPACK_STEP == NUNITS / UNPACK_FACTOR to be known at
11554 : : compile time.
11555 : :
11556 : : The significance of UNPACK_STEP is that, when PACK_P is false,
11557 : : output vector I operates on a window of UNPACK_STEP elements from each
11558 : : input, starting at lane UNPACK_STEP * (I % UNPACK_FACTOR). For example,
11559 : : when UNPACK_FACTOR is 2, the first output vector operates on lanes
11560 : : [0, NUNITS / 2 - 1] of each input vector and the second output vector
11561 : : operates on lanes [NUNITS / 2, NUNITS - 1] of each input vector.
11562 : :
11563 : : When REPEATING_P is true, NOUTPUTS holds the total number of outputs
11564 : : that we actually need to generate. */
11565 : 433952 : uint64_t noutputs = 0;
11566 : 433952 : poly_uint64 unpack_step = 0;
11567 : 433952 : loop_vec_info linfo = dyn_cast <loop_vec_info> (vinfo);
11568 : 146899 : if (!linfo
11569 : 472064 : || !multiple_p (nunits, unpack_factor, &unpack_step)
11570 : 146024 : || !constant_multiple_p (LOOP_VINFO_VECT_FACTOR (linfo)
11571 : 146024 : * SLP_TREE_LANES (node), nunits, &noutputs))
11572 : : repeating_p = false;
11573 : :
11574 : : /* We can handle the conditions described for REPEATING_P above for
11575 : : both variable- and constant-length vectors. The fallback requires
11576 : : us to generate every element of every permute vector explicitly,
11577 : : which is only possible for constant-length permute vectors.
11578 : :
11579 : : Set:
11580 : :
11581 : : - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
11582 : : mask vectors that we want to build.
11583 : :
11584 : : - NCOPIES to the number of copies of PERM that we need in order
11585 : : to build the necessary permute mask vectors. */
11586 : 146024 : uint64_t npatterns;
11587 : 146024 : unsigned nelts_per_pattern;
11588 : 146024 : uint64_t ncopies;
11589 : 146024 : if (repeating_p)
11590 : : {
11591 : : /* We need permute mask vectors that have the form:
11592 : :
11593 : : { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
11594 : :
11595 : : In other words, the original n-element permute in PERM is
11596 : : "unrolled" to fill a full vector. The stepped vector encoding
11597 : : that we use for permutes requires 3n elements. */
11598 : 107912 : npatterns = SLP_TREE_LANES (node);
11599 : 107912 : nelts_per_pattern = ncopies = 3;
11600 : : }
11601 : : else
11602 : : {
11603 : : /* Calculate every element of every permute mask vector explicitly,
11604 : : instead of relying on the pattern described above. */
11605 : 326040 : if (!nunits.is_constant (&npatterns)
11606 : 326040 : || !TYPE_VECTOR_SUBPARTS (op_vectype).is_constant ())
11607 : : {
11608 : : if (dump_p)
11609 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11610 : : "unsupported permutation %p on variable-length"
11611 : : " vectors\n", (void *) node);
11612 : : return -1;
11613 : : }
11614 : 326040 : nelts_per_pattern = ncopies = 1;
11615 : 326040 : if (linfo && !LOOP_VINFO_VECT_FACTOR (linfo).is_constant (&ncopies))
11616 : : {
11617 : : if (dump_p)
11618 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11619 : : "unsupported permutation %p for variable VF\n",
11620 : : (void *) node);
11621 : : return -1;
11622 : : }
11623 : : pack_p = false;
11624 : : unpack_factor = 1;
11625 : : }
11626 : 433952 : unsigned olanes = unpack_factor * ncopies * SLP_TREE_LANES (node);
11627 : 433952 : gcc_assert (repeating_p || multiple_p (olanes, nunits));
11628 : :
11629 : : /* Compute the { { SLP operand, vector index}, lane } permutation sequence
11630 : : from the { SLP operand, scalar lane } permutation as recorded in the
11631 : : SLP node as intermediate step. This part should already work
11632 : : with SLP children with arbitrary number of lanes. */
11633 : 433952 : auto_vec<std::pair<std::pair<unsigned, unsigned>, poly_uint64>> vperm;
11634 : 433952 : auto_vec<poly_uint64> active_lane;
11635 : 433952 : vperm.create (olanes);
11636 : 433952 : active_lane.safe_grow_cleared (children.length (), true);
11637 : 874829 : for (unsigned int ui = 0; ui < unpack_factor; ++ui)
11638 : : {
11639 : 1894974 : for (unsigned j = 0; j < children.length (); ++j)
11640 : 506610 : active_lane[j] = ui * unpack_step;
11641 : 1210543 : for (unsigned i = 0; i < ncopies; ++i)
11642 : : {
11643 : 4800540 : for (unsigned pi = 0; pi < perm.length (); ++pi)
11644 : : {
11645 : 1630604 : std::pair<unsigned, unsigned> p = perm[pi];
11646 : 1630604 : tree vtype = SLP_TREE_VECTYPE (children[p.first]);
11647 : 1630604 : if (repeating_p)
11648 : 615870 : vperm.quick_push ({{p.first, 0},
11649 : 615870 : p.second + active_lane[p.first]});
11650 : : else
11651 : : {
11652 : : /* We checked above that the vectors are constant-length. */
11653 : 1014734 : unsigned vnunits = TYPE_VECTOR_SUBPARTS (vtype)
11654 : 1014734 : .to_constant ();
11655 : 1014734 : unsigned lane = active_lane[p.first].to_constant ();
11656 : 1014734 : unsigned vi = (lane + p.second) / vnunits;
11657 : 1014734 : unsigned vl = (lane + p.second) % vnunits;
11658 : 1014734 : vperm.quick_push ({{p.first, vi}, vl});
11659 : : }
11660 : : }
11661 : : /* Advance to the next group. */
11662 : 1657310 : for (unsigned j = 0; j < children.length (); ++j)
11663 : 887644 : active_lane[j] += SLP_TREE_LANES (children[j]);
11664 : : }
11665 : : }
11666 : :
11667 : 433952 : if (dump_p)
11668 : : {
11669 : 8082 : dump_printf_loc (MSG_NOTE, vect_location,
11670 : : "vectorizing permutation %p", (void *)node);
11671 : 29237 : for (unsigned i = 0; i < perm.length (); ++i)
11672 : 21155 : dump_printf (MSG_NOTE, " op%u[%u]", perm[i].first, perm[i].second);
11673 : 8082 : if (repeating_p)
11674 : 6842 : dump_printf (MSG_NOTE, " (repeat %d)", SLP_TREE_LANES (node));
11675 : 8082 : dump_printf (MSG_NOTE, "\n");
11676 : 8082 : dump_printf_loc (MSG_NOTE, vect_location, "as");
11677 : 81641 : for (unsigned i = 0; i < vperm.length (); ++i)
11678 : : {
11679 : 73559 : if (i != 0
11680 : 73559 : && (repeating_p
11681 : 50923 : ? multiple_p (i, npatterns)
11682 : 54328 : : multiple_p (i, TYPE_VECTOR_SUBPARTS (vectype))))
11683 : 22223 : dump_printf (MSG_NOTE, ",");
11684 : 73559 : dump_printf (MSG_NOTE, " vops%u[%u][",
11685 : 73559 : vperm[i].first.first, vperm[i].first.second);
11686 : 73559 : dump_dec (MSG_NOTE, vperm[i].second);
11687 : 73559 : dump_printf (MSG_NOTE, "]");
11688 : : }
11689 : 8082 : dump_printf (MSG_NOTE, "\n");
11690 : : }
11691 : :
11692 : : /* We can only handle two-vector permutes, everything else should
11693 : : be lowered on the SLP level. The following is closely inspired
11694 : : by vect_transform_slp_perm_load and is supposed to eventually
11695 : : replace it.
11696 : : ??? As intermediate step do code-gen in the SLP tree representation
11697 : : somehow? */
11698 : 433952 : std::pair<unsigned, unsigned> first_vec = std::make_pair (-1U, -1U);
11699 : 433952 : std::pair<unsigned, unsigned> second_vec = std::make_pair (-1U, -1U);
11700 : 433952 : unsigned int index = 0;
11701 : 433952 : poly_uint64 mask_element;
11702 : 433952 : vec_perm_builder mask;
11703 : 433952 : mask.new_vector (nunits, npatterns, nelts_per_pattern);
11704 : 433952 : unsigned int count = mask.encoded_nelts ();
11705 : 433952 : mask.quick_grow (count);
11706 : 433952 : vec_perm_indices indices;
11707 : 433952 : unsigned nperms = 0;
11708 : : /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
11709 : : vectors to check during analysis, but we need to generate NOUTPUTS
11710 : : vectors during transformation. */
11711 : 433952 : unsigned total_nelts = olanes;
11712 : 433952 : unsigned process_nelts = olanes;
11713 : 433952 : if (repeating_p)
11714 : : {
11715 : 107912 : total_nelts = (total_nelts / unpack_factor) * noutputs;
11716 : 107912 : if (gsi)
11717 : 9569 : process_nelts = total_nelts;
11718 : : }
11719 : 433952 : unsigned last_ei = (total_nelts - 1) % process_nelts;
11720 : 2072543 : for (unsigned i = 0; i < process_nelts; ++i)
11721 : : {
11722 : : /* VI is the input vector index when generating code for REPEATING_P. */
11723 : 1646096 : unsigned vi = i / olanes * (pack_p ? 2 : 1);
11724 : 1646096 : unsigned ei = i % olanes;
11725 : 1646096 : mask_element = vperm[ei].second;
11726 : 1646096 : if (pack_p)
11727 : : {
11728 : : /* In this case, we have N outputs and the single child provides 2N
11729 : : inputs. Output X permutes inputs 2X and 2X+1.
11730 : :
11731 : : The mask indices are taken directly from the SLP permutation node.
11732 : : Index X selects from the first vector if (X / NUNITS) % 2 == 0;
11733 : : X selects from the second vector otherwise. These conditions
11734 : : are only known at compile time for constant-length vectors. */
11735 : : first_vec = std::make_pair (0, 0);
11736 : : second_vec = std::make_pair (0, 1);
11737 : : }
11738 : 1486973 : else if (first_vec.first == -1U
11739 : 1486973 : || first_vec == vperm[ei].first)
11740 : 1294721 : first_vec = vperm[ei].first;
11741 : 192252 : else if (second_vec.first == -1U
11742 : 192252 : || second_vec == vperm[ei].first)
11743 : : {
11744 : 191864 : second_vec = vperm[ei].first;
11745 : 191864 : mask_element += nunits;
11746 : : }
11747 : : else
11748 : : {
11749 : 388 : if (dump_p)
11750 : 7 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11751 : : "permutation requires at "
11752 : : "least three vectors\n");
11753 : 388 : gcc_assert (!gsi);
11754 : : return -1;
11755 : : }
11756 : :
11757 : 1645708 : mask[index++] = mask_element;
11758 : :
11759 : 1645708 : if (index == count)
11760 : : {
11761 : 714035 : indices.new_vector (mask, second_vec.first == -1U ? 1 : 2,
11762 : : TYPE_VECTOR_SUBPARTS (op_vectype));
11763 : 569664 : bool identity_p = (indices.series_p (0, 1, mask[0], 1)
11764 : 883320 : && constant_multiple_p (mask[0], nunits));
11765 : 569664 : machine_mode vmode = TYPE_MODE (vectype);
11766 : 569664 : machine_mode op_vmode = TYPE_MODE (op_vectype);
11767 : 569664 : unsigned HOST_WIDE_INT c;
11768 : 569664 : if ((!identity_p
11769 : 529394 : && !can_vec_perm_const_p (vmode, op_vmode, indices))
11770 : 569664 : || (identity_p
11771 : 40270 : && !known_le (nunits,
11772 : : TYPE_VECTOR_SUBPARTS (op_vectype))
11773 : 7125 : && (!constant_multiple_p (nunits,
11774 : 8 : TYPE_VECTOR_SUBPARTS (op_vectype),
11775 : 8 : &c) || c != 2)))
11776 : : {
11777 : 7117 : if (dump_p)
11778 : : {
11779 : 152 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
11780 : : vect_location,
11781 : : "unsupported vect permute { ");
11782 : 1586 : for (i = 0; i < count; ++i)
11783 : : {
11784 : 1434 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
11785 : 1434 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
11786 : : }
11787 : 152 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
11788 : : }
11789 : 7117 : gcc_assert (!gsi);
11790 : 7505 : return -1;
11791 : : }
11792 : :
11793 : 562547 : if (!identity_p)
11794 : 522277 : nperms += CEIL (total_nelts, process_nelts) - (ei > last_ei);
11795 : 562547 : if (gsi)
11796 : : {
11797 : 30249 : if (second_vec.first == -1U)
11798 : 6713 : second_vec = first_vec;
11799 : :
11800 : 30249 : slp_tree
11801 : 30249 : first_node = children[first_vec.first],
11802 : 30249 : second_node = children[second_vec.first];
11803 : :
11804 : 30249 : tree mask_vec = NULL_TREE;
11805 : 30249 : if (!identity_p)
11806 : 27216 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
11807 : :
11808 : 30249 : tree first_def
11809 : 30249 : = vect_get_slp_vect_def (first_node, first_vec.second + vi);
11810 : 30249 : tree second_def
11811 : 30249 : = vect_get_slp_vect_def (second_node, second_vec.second + vi);
11812 : 30249 : vect_add_slp_permutation (vinfo, gsi, node, first_def,
11813 : 30249 : second_def, mask_vec, mask[0]);
11814 : : }
11815 : :
11816 : : index = 0;
11817 : : first_vec = std::make_pair (-1U, -1U);
11818 : : second_vec = std::make_pair (-1U, -1U);
11819 : : }
11820 : : }
11821 : :
11822 : 426447 : return nperms;
11823 : 433952 : }
11824 : :
11825 : : /* Vectorize the SLP permutations in NODE as specified
11826 : : in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
11827 : : child number and lane number.
11828 : : Interleaving of two two-lane two-child SLP subtrees (not supported):
11829 : : [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
11830 : : A blend of two four-lane two-child SLP subtrees:
11831 : : [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
11832 : : Highpart of a four-lane one-child SLP subtree (not supported):
11833 : : [ { 0, 2 }, { 0, 3 } ]
11834 : : Where currently only a subset is supported by code generating below. */
11835 : :
11836 : : bool
11837 : 114908 : vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
11838 : : slp_tree node, stmt_vector_for_cost *cost_vec)
11839 : : {
11840 : 114908 : tree vectype = SLP_TREE_VECTYPE (node);
11841 : 114908 : lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
11842 : 114908 : int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
11843 : 114908 : SLP_TREE_CHILDREN (node),
11844 : : dump_enabled_p ());
11845 : 114908 : if (nperms < 0)
11846 : : return false;
11847 : :
11848 : 113682 : if (!gsi && nperms != 0)
11849 : 92718 : record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
11850 : :
11851 : : return true;
11852 : : }
11853 : :
11854 : : /* Vectorize SLP NODE. */
11855 : :
11856 : : static void
11857 : 1462359 : vect_schedule_slp_node (vec_info *vinfo,
11858 : : slp_tree node, slp_instance instance)
11859 : : {
11860 : 1462359 : gimple_stmt_iterator si;
11861 : 1462359 : int i;
11862 : 1462359 : slp_tree child;
11863 : :
11864 : : /* Vectorize externals and constants. */
11865 : 1462359 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
11866 : 1462359 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
11867 : : {
11868 : : /* ??? vectorizable_shift can end up using a scalar operand which is
11869 : : currently denoted as !SLP_TREE_VECTYPE. No need to vectorize the
11870 : : node in this case. */
11871 : 500579 : if (!SLP_TREE_VECTYPE (node))
11872 : 500579 : return;
11873 : :
11874 : : /* There are two reasons vector defs might already exist. The first
11875 : : is that we are vectorizing an existing vector def. The second is
11876 : : when performing BB vectorization shared constant/external nodes
11877 : : are not split apart during partitioning so during the code-gen
11878 : : DFS walk we can end up visiting them twice. */
11879 : 494493 : if (! SLP_TREE_VEC_DEFS (node).exists ())
11880 : 493745 : vect_create_constant_vectors (vinfo, node);
11881 : 494493 : return;
11882 : : }
11883 : :
11884 : 961780 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
11885 : :
11886 : 961780 : gcc_assert (SLP_TREE_VEC_DEFS (node).is_empty ());
11887 : 961780 : if (SLP_TREE_VECTYPE (node))
11888 : 961774 : SLP_TREE_VEC_DEFS (node).create (vect_get_num_copies (vinfo, node));
11889 : :
11890 : 961780 : if (!SLP_TREE_PERMUTE_P (node) && STMT_VINFO_DATA_REF (stmt_info))
11891 : : {
11892 : : /* Vectorized loads go before the first scalar load to make it
11893 : : ready early, vectorized stores go before the last scalar
11894 : : stmt which is where all uses are ready. */
11895 : 705713 : stmt_vec_info last_stmt_info = NULL;
11896 : 705713 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
11897 : 160981 : last_stmt_info = vect_find_first_scalar_stmt_in_slp (node);
11898 : : else /* DR_IS_WRITE */
11899 : 544732 : last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
11900 : 705713 : si = gsi_for_stmt (last_stmt_info->stmt);
11901 : 705713 : }
11902 : 256067 : else if (!SLP_TREE_PERMUTE_P (node)
11903 : 240115 : && (SLP_TREE_TYPE (node) == cycle_phi_info_type
11904 : : || SLP_TREE_TYPE (node) == induc_vec_info_type
11905 : : || SLP_TREE_TYPE (node) == phi_info_type))
11906 : : {
11907 : : /* For PHI node vectorization we do not use the insertion iterator. */
11908 : 52736 : si = gsi_none ();
11909 : : }
11910 : : else
11911 : : {
11912 : : /* Emit other stmts after the children vectorized defs which is
11913 : : earliest possible. */
11914 : : gimple *last_stmt = NULL;
11915 : : bool seen_vector_def = false;
11916 : 566515 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11917 : 363184 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
11918 : : {
11919 : : /* For fold-left reductions we are retaining the scalar
11920 : : reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
11921 : : set so the representation isn't perfect. Resort to the
11922 : : last scalar def here. */
11923 : 291247 : if (SLP_TREE_VEC_DEFS (child).is_empty ())
11924 : : {
11925 : 845 : gcc_assert (SLP_TREE_TYPE (child) == cycle_phi_info_type);
11926 : 845 : gphi *phi = as_a <gphi *>
11927 : 845 : (vect_find_last_scalar_stmt_in_slp (child)->stmt);
11928 : 845 : if (!last_stmt)
11929 : : last_stmt = phi;
11930 : 629 : else if (vect_stmt_dominates_stmt_p (last_stmt, phi))
11931 : : last_stmt = phi;
11932 : 618 : else if (vect_stmt_dominates_stmt_p (phi, last_stmt))
11933 : : ;
11934 : : else
11935 : 0 : gcc_unreachable ();
11936 : : }
11937 : : /* We are emitting all vectorized stmts in the same place and
11938 : : the last one is the last.
11939 : : ??? Unless we have a load permutation applied and that
11940 : : figures to re-use an earlier generated load. */
11941 : : unsigned j;
11942 : : tree vdef;
11943 : 686893 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11944 : : {
11945 : 395646 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11946 : 395646 : if (!last_stmt)
11947 : : last_stmt = vstmt;
11948 : 202778 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
11949 : : last_stmt = vstmt;
11950 : 44823 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
11951 : : ;
11952 : : else
11953 : 0 : gcc_unreachable ();
11954 : : }
11955 : : }
11956 : 71937 : else if (!SLP_TREE_VECTYPE (child))
11957 : : {
11958 : : /* For externals we use unvectorized at all scalar defs. */
11959 : : unsigned j;
11960 : : tree def;
11961 : 12691 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
11962 : 7224 : if (TREE_CODE (def) == SSA_NAME
11963 : 7224 : && !SSA_NAME_IS_DEFAULT_DEF (def))
11964 : : {
11965 : 163 : gimple *stmt = SSA_NAME_DEF_STMT (def);
11966 : 163 : if (gimple_uid (stmt) == -1u)
11967 : : /* If the stmt is not inside the region do not
11968 : : use it as possible insertion point. */
11969 : : ;
11970 : 155 : else if (!last_stmt)
11971 : : last_stmt = stmt;
11972 : 149 : else if (vect_stmt_dominates_stmt_p (last_stmt, stmt))
11973 : : last_stmt = stmt;
11974 : 149 : else if (vect_stmt_dominates_stmt_p (stmt, last_stmt))
11975 : : ;
11976 : : else
11977 : 0 : gcc_unreachable ();
11978 : : }
11979 : : }
11980 : : else
11981 : : {
11982 : : /* For externals we have to look at all defs since their
11983 : : insertion place is decided per vector. But beware
11984 : : of pre-existing vectors where we need to make sure
11985 : : we do not insert before the region boundary. */
11986 : 66470 : if (SLP_TREE_SCALAR_OPS (child).is_empty ()
11987 : 612 : && !vinfo->lookup_def (SLP_TREE_VEC_DEFS (child)[0]))
11988 : : seen_vector_def = true;
11989 : : else
11990 : : {
11991 : : unsigned j;
11992 : : tree vdef;
11993 : 523391 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11994 : 93853 : if (TREE_CODE (vdef) == SSA_NAME
11995 : 93853 : && !SSA_NAME_IS_DEFAULT_DEF (vdef))
11996 : : {
11997 : 19274 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11998 : 19274 : if (!last_stmt)
11999 : : last_stmt = vstmt;
12000 : 10756 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
12001 : : last_stmt = vstmt;
12002 : 8583 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
12003 : : ;
12004 : : else
12005 : 0 : gcc_unreachable ();
12006 : : }
12007 : : }
12008 : : }
12009 : : /* This can happen when all children are pre-existing vectors or
12010 : : constants. */
12011 : 203331 : if (!last_stmt)
12012 : 1723 : last_stmt = vect_find_first_scalar_stmt_in_slp (node)->stmt;
12013 : 1723 : if (!last_stmt)
12014 : : {
12015 : 0 : gcc_assert (seen_vector_def);
12016 : 0 : si = gsi_after_labels (vinfo->bbs[0]);
12017 : : }
12018 : 203331 : else if (is_ctrl_altering_stmt (last_stmt))
12019 : : {
12020 : : /* We split regions to vectorize at control altering stmts
12021 : : with a definition so this must be an external which
12022 : : we can insert at the start of the region. */
12023 : 0 : si = gsi_after_labels (vinfo->bbs[0]);
12024 : : }
12025 : 203331 : else if (is_a <bb_vec_info> (vinfo)
12026 : 17340 : && !SLP_TREE_PERMUTE_P (node)
12027 : 15977 : && gimple_bb (last_stmt) != gimple_bb (stmt_info->stmt)
12028 : 204560 : && gimple_could_trap_p (stmt_info->stmt))
12029 : : {
12030 : : /* We've constrained possibly trapping operations to all come
12031 : : from the same basic-block, if vectorized defs would allow earlier
12032 : : scheduling still force vectorized stmts to the original block.
12033 : : This is only necessary for BB vectorization since for loop vect
12034 : : all operations are in a single BB and scalar stmt based
12035 : : placement doesn't play well with epilogue vectorization. */
12036 : 51 : gcc_assert (dominated_by_p (CDI_DOMINATORS,
12037 : : gimple_bb (stmt_info->stmt),
12038 : : gimple_bb (last_stmt)));
12039 : 51 : si = gsi_after_labels (gimple_bb (stmt_info->stmt));
12040 : : }
12041 : 203280 : else if (is_a <gphi *> (last_stmt))
12042 : 14152 : si = gsi_after_labels (gimple_bb (last_stmt));
12043 : : else
12044 : : {
12045 : 189128 : si = gsi_for_stmt (last_stmt);
12046 : 189128 : gsi_next (&si);
12047 : :
12048 : : /* Avoid scheduling internal defs outside of the loop when
12049 : : we might have only implicitly tracked loop mask/len defs. */
12050 : 189128 : if (auto loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
12051 : 74 : if (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
12052 : 172050 : || LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12053 : : {
12054 : 74 : gimple_stmt_iterator si2
12055 : 74 : = gsi_after_labels (LOOP_VINFO_LOOP (loop_vinfo)->header);
12056 : 74 : if ((gsi_end_p (si2)
12057 : 0 : && (LOOP_VINFO_LOOP (loop_vinfo)->header
12058 : 0 : != gimple_bb (last_stmt))
12059 : 0 : && dominated_by_p (CDI_DOMINATORS,
12060 : : LOOP_VINFO_LOOP (loop_vinfo)->header,
12061 : 0 : gimple_bb (last_stmt)))
12062 : 74 : || (!gsi_end_p (si2)
12063 : 74 : && last_stmt != *si2
12064 : 72 : && vect_stmt_dominates_stmt_p (last_stmt, *si2)))
12065 : 3 : si = si2;
12066 : : }
12067 : : }
12068 : : }
12069 : :
12070 : 961780 : if (dump_enabled_p ())
12071 : : {
12072 : 69123 : if (stmt_info)
12073 : 69072 : dump_printf_loc (MSG_NOTE, vect_location,
12074 : : "------>vectorizing SLP node starting from: %G",
12075 : : stmt_info->stmt);
12076 : : else
12077 : : {
12078 : 51 : dump_printf_loc (MSG_NOTE, vect_location,
12079 : : "------>vectorizing SLP node:\n");
12080 : 51 : vect_print_slp_tree (MSG_NOTE, vect_location, node);
12081 : : }
12082 : : }
12083 : 961780 : vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
12084 : : }
12085 : :
12086 : : /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
12087 : : For loop vectorization this is done in vectorizable_call, but for SLP
12088 : : it needs to be deferred until end of vect_schedule_slp, because multiple
12089 : : SLP instances may refer to the same scalar stmt. */
12090 : :
12091 : : static void
12092 : 593187 : vect_remove_slp_scalar_calls (vec_info *vinfo,
12093 : : slp_tree node, hash_set<slp_tree> &visited)
12094 : : {
12095 : 593187 : gimple *new_stmt;
12096 : 593187 : gimple_stmt_iterator gsi;
12097 : 593187 : int i;
12098 : 593187 : slp_tree child;
12099 : 593187 : tree lhs;
12100 : 593187 : stmt_vec_info stmt_info;
12101 : :
12102 : 593187 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
12103 : 186250 : return;
12104 : :
12105 : 450290 : if (visited.add (node))
12106 : : return;
12107 : :
12108 : 911725 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12109 : 504788 : vect_remove_slp_scalar_calls (vinfo, child, visited);
12110 : :
12111 : 1283252 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
12112 : : {
12113 : 473427 : if (!stmt_info)
12114 : 3821 : continue;
12115 : 469606 : stmt_info = vect_orig_stmt (stmt_info);
12116 : 469606 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
12117 : 5091 : if (!stmt || gimple_bb (stmt) == NULL)
12118 : 464537 : continue;
12119 : 5069 : lhs = gimple_call_lhs (stmt);
12120 : 5069 : if (lhs)
12121 : 4505 : new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
12122 : : else
12123 : : {
12124 : 564 : new_stmt = gimple_build_nop ();
12125 : 564 : unlink_stmt_vdef (stmt_info->stmt);
12126 : : }
12127 : 5069 : gsi = gsi_for_stmt (stmt);
12128 : 5069 : vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
12129 : 5069 : if (lhs)
12130 : 4505 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12131 : : }
12132 : : }
12133 : :
12134 : : static void
12135 : 88399 : vect_remove_slp_scalar_calls (vec_info *vinfo, slp_tree node)
12136 : : {
12137 : 88399 : hash_set<slp_tree> visited;
12138 : 88399 : vect_remove_slp_scalar_calls (vinfo, node, visited);
12139 : 88399 : }
12140 : :
12141 : : /* Vectorize the instance root. */
12142 : :
12143 : : void
12144 : 10417 : vectorize_slp_instance_root_stmt (vec_info *vinfo, slp_tree node, slp_instance instance)
12145 : : {
12146 : 10417 : gassign *rstmt = NULL;
12147 : :
12148 : 10417 : if (instance->kind == slp_inst_kind_ctor)
12149 : : {
12150 : 4435 : if (SLP_TREE_VEC_DEFS (node).length () == 1)
12151 : : {
12152 : 4431 : tree vect_lhs = SLP_TREE_VEC_DEFS (node)[0];
12153 : 4431 : tree root_lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
12154 : 4431 : if (!useless_type_conversion_p (TREE_TYPE (root_lhs),
12155 : 4431 : TREE_TYPE (vect_lhs)))
12156 : 0 : vect_lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (root_lhs),
12157 : : vect_lhs);
12158 : 4431 : rstmt = gimple_build_assign (root_lhs, vect_lhs);
12159 : : }
12160 : : else
12161 : : {
12162 : 4 : gcc_assert (SLP_TREE_VEC_DEFS (node).length () > 1);
12163 : 4 : tree child_def;
12164 : 4 : int j;
12165 : 4 : vec<constructor_elt, va_gc> *v;
12166 : 4 : vec_alloc (v, SLP_TREE_VEC_DEFS (node).length ());
12167 : :
12168 : : /* A CTOR can handle V16HI composition from VNx8HI so we
12169 : : do not need to convert vector elements if the types
12170 : : do not match. */
12171 : 12 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (node), j, child_def)
12172 : 8 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, child_def);
12173 : 4 : tree lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
12174 : 4 : tree rtype
12175 : 4 : = TREE_TYPE (gimple_assign_rhs1 (instance->root_stmts[0]->stmt));
12176 : 4 : tree r_constructor = build_constructor (rtype, v);
12177 : 4 : rstmt = gimple_build_assign (lhs, r_constructor);
12178 : : }
12179 : : }
12180 : 5982 : else if (instance->kind == slp_inst_kind_bb_reduc)
12181 : : {
12182 : : /* Largely inspired by reduction chain epilogue handling in
12183 : : vect_create_epilog_for_reduction. */
12184 : 4295 : vec<tree> vec_defs = vNULL;
12185 : 4295 : vect_get_slp_defs (node, &vec_defs);
12186 : 4295 : enum tree_code reduc_code
12187 : 4295 : = gimple_assign_rhs_code (instance->root_stmts[0]->stmt);
12188 : : /* ??? We actually have to reflect signs somewhere. */
12189 : 4295 : if (reduc_code == MINUS_EXPR)
12190 : 0 : reduc_code = PLUS_EXPR;
12191 : 4295 : gimple_seq epilogue = NULL;
12192 : : /* We may end up with more than one vector result, reduce them
12193 : : to one vector. */
12194 : 4295 : tree vec_def = vec_defs[0];
12195 : 4295 : tree vectype = TREE_TYPE (vec_def);
12196 : 4295 : tree compute_vectype = vectype;
12197 : 4295 : bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
12198 : 4096 : && TYPE_OVERFLOW_UNDEFINED (vectype)
12199 : 7235 : && operation_can_overflow (reduc_code));
12200 : 2810 : if (pun_for_overflow_p)
12201 : : {
12202 : 2810 : compute_vectype = unsigned_type_for (vectype);
12203 : 2810 : vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
12204 : : compute_vectype, vec_def);
12205 : : }
12206 : 6657 : for (unsigned i = 1; i < vec_defs.length (); ++i)
12207 : : {
12208 : 2362 : tree def = vec_defs[i];
12209 : 2362 : if (pun_for_overflow_p)
12210 : 2273 : def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
12211 : : compute_vectype, def);
12212 : 2362 : vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
12213 : : vec_def, def);
12214 : : }
12215 : 4295 : vec_defs.release ();
12216 : : /* ??? Support other schemes than direct internal fn. */
12217 : 4295 : internal_fn reduc_fn;
12218 : 4295 : if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
12219 : 4295 : || reduc_fn == IFN_LAST)
12220 : 0 : gcc_unreachable ();
12221 : 4295 : tree scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
12222 : 4295 : TREE_TYPE (compute_vectype), vec_def);
12223 : 4295 : if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
12224 : : {
12225 : 2809 : tree rem_def = NULL_TREE;
12226 : 12395 : for (auto def : SLP_INSTANCE_REMAIN_DEFS (instance))
12227 : : {
12228 : 9586 : def = gimple_convert (&epilogue, TREE_TYPE (scalar_def), def);
12229 : 9586 : if (!rem_def)
12230 : : rem_def = def;
12231 : : else
12232 : 6777 : rem_def = gimple_build (&epilogue, reduc_code,
12233 : 6777 : TREE_TYPE (scalar_def),
12234 : : rem_def, def);
12235 : : }
12236 : 2809 : scalar_def = gimple_build (&epilogue, reduc_code,
12237 : 2809 : TREE_TYPE (scalar_def),
12238 : : scalar_def, rem_def);
12239 : : }
12240 : 4295 : scalar_def = gimple_convert (&epilogue,
12241 : 4295 : TREE_TYPE (vectype), scalar_def);
12242 : 4295 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
12243 : 4295 : gsi_insert_seq_before (&rgsi, epilogue, GSI_SAME_STMT);
12244 : 4295 : gimple_assign_set_rhs_from_tree (&rgsi, scalar_def);
12245 : 4295 : update_stmt (gsi_stmt (rgsi));
12246 : 4295 : return;
12247 : : }
12248 : 1687 : else if (instance->kind == slp_inst_kind_gcond)
12249 : : {
12250 : : /* Only support a single root for now as we can't codegen CFG yet and so we
12251 : : can't support lane > 1 at this time. */
12252 : 1687 : gcc_assert (instance->root_stmts.length () == 1);
12253 : 1687 : auto root_stmt_info = instance->root_stmts[0];
12254 : 1687 : auto last_stmt = STMT_VINFO_STMT (vect_orig_stmt (root_stmt_info));
12255 : 1687 : gimple_stmt_iterator rgsi = gsi_for_stmt (last_stmt);
12256 : 1687 : gcc_assert (!SLP_TREE_VEC_DEFS (node).is_empty ());
12257 : 1687 : bool res = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
12258 : : root_stmt_info, &rgsi, node, NULL);
12259 : 1687 : gcc_assert (res);
12260 : 1687 : return;
12261 : : }
12262 : : else
12263 : 0 : gcc_unreachable ();
12264 : :
12265 : 4435 : gcc_assert (rstmt);
12266 : :
12267 : 4435 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
12268 : 4435 : gsi_replace (&rgsi, rstmt, true);
12269 : : }
12270 : :
12271 : : struct slp_scc_info
12272 : : {
12273 : : bool on_stack;
12274 : : int dfs;
12275 : : int lowlink;
12276 : : };
12277 : :
12278 : : /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs. */
12279 : :
12280 : : static void
12281 : 1462359 : vect_schedule_scc (vec_info *vinfo, slp_tree node, slp_instance instance,
12282 : : hash_map<slp_tree, slp_scc_info> &scc_info,
12283 : : int &maxdfs, vec<slp_tree> &stack)
12284 : : {
12285 : 1462359 : bool existed_p;
12286 : 1462359 : slp_scc_info *info = &scc_info.get_or_insert (node, &existed_p);
12287 : 1462359 : gcc_assert (!existed_p);
12288 : 1462359 : info->dfs = maxdfs;
12289 : 1462359 : info->lowlink = maxdfs;
12290 : 1462359 : maxdfs++;
12291 : :
12292 : : /* Leaf. */
12293 : 1462359 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
12294 : : {
12295 : 500579 : info->on_stack = false;
12296 : 500579 : vect_schedule_slp_node (vinfo, node, instance);
12297 : 1032515 : return;
12298 : : }
12299 : :
12300 : 961780 : info->on_stack = true;
12301 : 961780 : stack.safe_push (node);
12302 : :
12303 : 961780 : unsigned i;
12304 : 961780 : slp_tree child;
12305 : : /* DFS recurse. */
12306 : 1986316 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
12307 : : {
12308 : 1024536 : if (!child)
12309 : 54362 : continue;
12310 : 970174 : slp_scc_info *child_info = scc_info.get (child);
12311 : 970174 : if (!child_info)
12312 : : {
12313 : 882217 : vect_schedule_scc (vinfo, child, instance, scc_info, maxdfs, stack);
12314 : : /* Recursion might have re-allocated the node. */
12315 : 882217 : info = scc_info.get (node);
12316 : 882217 : child_info = scc_info.get (child);
12317 : 882217 : info->lowlink = MIN (info->lowlink, child_info->lowlink);
12318 : : }
12319 : 87957 : else if (child_info->on_stack)
12320 : 25136 : info->lowlink = MIN (info->lowlink, child_info->dfs);
12321 : : }
12322 : 961780 : if (info->lowlink != info->dfs)
12323 : : return;
12324 : :
12325 : 930423 : auto_vec<slp_tree, 4> phis_to_fixup;
12326 : :
12327 : : /* Singleton. */
12328 : 930423 : if (stack.last () == node)
12329 : : {
12330 : 906883 : stack.pop ();
12331 : 906883 : info->on_stack = false;
12332 : 906883 : vect_schedule_slp_node (vinfo, node, instance);
12333 : 906883 : if (!SLP_TREE_PERMUTE_P (node)
12334 : 906883 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (node)->stmt))
12335 : 29266 : phis_to_fixup.quick_push (node);
12336 : : }
12337 : : else
12338 : : {
12339 : : /* SCC. */
12340 : 23540 : int last_idx = stack.length () - 1;
12341 : 54897 : while (stack[last_idx] != node)
12342 : 31357 : last_idx--;
12343 : : /* We can break the cycle at PHIs who have at least one child
12344 : : code generated. Then we could re-start the DFS walk until
12345 : : all nodes in the SCC are covered (we might have new entries
12346 : : for only back-reachable nodes). But it's simpler to just
12347 : : iterate and schedule those that are ready. */
12348 : 23540 : unsigned todo = stack.length () - last_idx;
12349 : 23861 : do
12350 : : {
12351 : 104337 : for (int idx = stack.length () - 1; idx >= last_idx; --idx)
12352 : : {
12353 : 56615 : slp_tree entry = stack[idx];
12354 : 56615 : if (!entry)
12355 : 928 : continue;
12356 : 55687 : bool phi = (!SLP_TREE_PERMUTE_P (entry)
12357 : 55687 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (entry)->stmt));
12358 : 55687 : bool ready = !phi;
12359 : 141000 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry), i, child)
12360 : 110030 : if (!child)
12361 : : {
12362 : 22762 : gcc_assert (phi);
12363 : : ready = true;
12364 : : break;
12365 : : }
12366 : 87268 : else if (scc_info.get (child)->on_stack)
12367 : : {
12368 : 23740 : if (!phi)
12369 : : {
12370 : : ready = false;
12371 : : break;
12372 : : }
12373 : : }
12374 : : else
12375 : : {
12376 : 63528 : if (phi)
12377 : : {
12378 : : ready = true;
12379 : : break;
12380 : : }
12381 : : }
12382 : 32925 : if (ready)
12383 : : {
12384 : 54897 : vect_schedule_slp_node (vinfo, entry, instance);
12385 : 54897 : scc_info.get (entry)->on_stack = false;
12386 : 54897 : stack[idx] = NULL;
12387 : 54897 : todo--;
12388 : 54897 : if (phi)
12389 : 23957 : phis_to_fixup.safe_push (entry);
12390 : : }
12391 : : }
12392 : : }
12393 : 23861 : while (todo != 0);
12394 : :
12395 : : /* Pop the SCC. */
12396 : 23540 : stack.truncate (last_idx);
12397 : : }
12398 : :
12399 : : /* Now fixup the backedge def of the vectorized PHIs in this SCC. */
12400 : : slp_tree phi_node;
12401 : 1914069 : FOR_EACH_VEC_ELT (phis_to_fixup, i, phi_node)
12402 : : {
12403 : 53223 : gphi *phi = as_a <gphi *> (SLP_TREE_REPRESENTATIVE (phi_node)->stmt);
12404 : 53223 : edge_iterator ei;
12405 : 53223 : edge e;
12406 : 167373 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
12407 : : {
12408 : 114150 : unsigned dest_idx = e->dest_idx;
12409 : 114150 : child = SLP_TREE_CHILDREN (phi_node)[dest_idx];
12410 : 114150 : if (!child || SLP_TREE_DEF_TYPE (child) != vect_internal_def)
12411 : 64682 : continue;
12412 : 49468 : unsigned n = SLP_TREE_VEC_DEFS (phi_node).length ();
12413 : : /* Simply fill all args. */
12414 : 49468 : if (STMT_VINFO_DEF_TYPE (SLP_TREE_REPRESENTATIVE (phi_node))
12415 : : != vect_first_order_recurrence)
12416 : 105589 : for (unsigned i = 0; i < n; ++i)
12417 : : {
12418 : 56161 : tree phidef = SLP_TREE_VEC_DEFS (phi_node)[i];
12419 : 56161 : gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (phidef));
12420 : 56161 : add_phi_arg (phi, vect_get_slp_vect_def (child, i),
12421 : : e, gimple_phi_arg_location (phi, dest_idx));
12422 : : }
12423 : : else
12424 : : {
12425 : : /* Unless it is a first order recurrence which needs
12426 : : args filled in for both the PHI node and the permutes. */
12427 : 40 : gimple *perm
12428 : 40 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[0]);
12429 : 40 : gimple *rphi = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (perm));
12430 : 40 : add_phi_arg (as_a <gphi *> (rphi),
12431 : : vect_get_slp_vect_def (child, n - 1),
12432 : : e, gimple_phi_arg_location (phi, dest_idx));
12433 : 117 : for (unsigned i = 0; i < n; ++i)
12434 : : {
12435 : 77 : gimple *perm
12436 : 77 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[i]);
12437 : 77 : if (i > 0)
12438 : 37 : gimple_assign_set_rhs1 (perm,
12439 : : vect_get_slp_vect_def (child, i - 1));
12440 : 77 : gimple_assign_set_rhs2 (perm,
12441 : : vect_get_slp_vect_def (child, i));
12442 : 77 : update_stmt (perm);
12443 : : }
12444 : : }
12445 : : }
12446 : : }
12447 : 930423 : }
12448 : :
12449 : : /* Generate vector code for SLP_INSTANCES in the loop/basic block. */
12450 : :
12451 : : void
12452 : 542568 : vect_schedule_slp (vec_info *vinfo, const vec<slp_instance> &slp_instances)
12453 : : {
12454 : 542568 : slp_instance instance;
12455 : 542568 : unsigned int i;
12456 : :
12457 : 542568 : hash_map<slp_tree, slp_scc_info> scc_info;
12458 : 542568 : int maxdfs = 0;
12459 : 1122793 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
12460 : : {
12461 : 580225 : slp_tree node = SLP_INSTANCE_TREE (instance);
12462 : 580225 : if (dump_enabled_p ())
12463 : : {
12464 : 15357 : dump_printf_loc (MSG_NOTE, vect_location,
12465 : : "Vectorizing SLP tree:\n");
12466 : : /* ??? Dump all? */
12467 : 15357 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
12468 : 427 : dump_printf_loc (MSG_NOTE, vect_location, "Root stmt: %G",
12469 : 427 : SLP_INSTANCE_ROOT_STMTS (instance)[0]->stmt);
12470 : 15357 : vect_print_slp_graph (MSG_NOTE, vect_location,
12471 : : SLP_INSTANCE_TREE (instance));
12472 : : }
12473 : : /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
12474 : : have a PHI be the node breaking the cycle. */
12475 : 580225 : auto_vec<slp_tree> stack;
12476 : 580225 : if (!scc_info.get (node))
12477 : 580142 : vect_schedule_scc (vinfo, node, instance, scc_info, maxdfs, stack);
12478 : :
12479 : 580225 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
12480 : 10417 : vectorize_slp_instance_root_stmt (vinfo, node, instance);
12481 : :
12482 : 580225 : if (dump_enabled_p ())
12483 : 15357 : dump_printf_loc (MSG_NOTE, vect_location,
12484 : : "vectorizing stmts using SLP.\n");
12485 : 580225 : }
12486 : :
12487 : 1665361 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
12488 : : {
12489 : 580225 : slp_tree root = SLP_INSTANCE_TREE (instance);
12490 : 580225 : stmt_vec_info store_info;
12491 : 580225 : unsigned int j;
12492 : :
12493 : : /* Remove scalar call stmts. Do not do this for basic-block
12494 : : vectorization as not all uses may be vectorized.
12495 : : ??? Why should this be necessary? DCE should be able to
12496 : : remove the stmts itself.
12497 : : ??? For BB vectorization we can as well remove scalar
12498 : : stmts starting from the SLP tree root if they have no
12499 : : uses. */
12500 : 580225 : if (is_a <loop_vec_info> (vinfo))
12501 : 88399 : vect_remove_slp_scalar_calls (vinfo, root);
12502 : :
12503 : : /* Remove vectorized stores original scalar stmts. */
12504 : 2593329 : for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info); j++)
12505 : : {
12506 : 1468372 : if (!store_info
12507 : 1468358 : || !STMT_VINFO_DATA_REF (store_info)
12508 : 1441025 : || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info)))
12509 : : break;
12510 : :
12511 : 1432879 : store_info = vect_orig_stmt (store_info);
12512 : : /* Free the attached stmt_vec_info and remove the stmt. */
12513 : 1432879 : vinfo->remove_stmt (store_info);
12514 : :
12515 : : /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
12516 : : to not crash in vect_free_slp_tree later. */
12517 : 1432879 : if (SLP_TREE_REPRESENTATIVE (root) == store_info)
12518 : 544439 : SLP_TREE_REPRESENTATIVE (root) = NULL;
12519 : : }
12520 : : }
12521 : 542568 : }
|