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 : : static bool vect_transform_slp_perm_load_1 (vec_info *, slp_tree,
57 : : load_permutation_t &,
58 : : const vec<tree> &,
59 : : gimple_stmt_iterator *,
60 : : poly_uint64, bool, bool,
61 : : unsigned *,
62 : : unsigned * = nullptr,
63 : : bool = false);
64 : : static int vectorizable_slp_permutation_1 (vec_info *, gimple_stmt_iterator *,
65 : : slp_tree, lane_permutation_t &,
66 : : vec<slp_tree> &, bool);
67 : : static bool vectorizable_slp_permutation (vec_info *, gimple_stmt_iterator *,
68 : : slp_tree, stmt_vector_for_cost *);
69 : : static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
70 : : static bool vect_slp_can_convert_to_external (const vec<stmt_vec_info> &);
71 : :
72 : : static object_allocator<_slp_tree> *slp_tree_pool;
73 : : static slp_tree slp_first_node;
74 : :
75 : : void
76 : 1104845 : vect_slp_init (void)
77 : : {
78 : 1104845 : slp_tree_pool = new object_allocator<_slp_tree> ("SLP nodes");
79 : 1104845 : }
80 : :
81 : : void
82 : 1104845 : vect_slp_fini (void)
83 : : {
84 : 1506216 : while (slp_first_node)
85 : 401371 : delete slp_first_node;
86 : 2209690 : delete slp_tree_pool;
87 : 1104845 : slp_tree_pool = NULL;
88 : 1104845 : }
89 : :
90 : : void *
91 : 6908090 : _slp_tree::operator new (size_t n)
92 : : {
93 : 6908090 : gcc_assert (n == sizeof (_slp_tree));
94 : 6908090 : return slp_tree_pool->allocate_raw ();
95 : : }
96 : :
97 : : void
98 : 6908090 : _slp_tree::operator delete (void *node, size_t n)
99 : : {
100 : 6908090 : gcc_assert (n == sizeof (_slp_tree));
101 : 6908090 : slp_tree_pool->remove_raw (node);
102 : 6908090 : }
103 : :
104 : :
105 : : /* Initialize a SLP node. */
106 : :
107 : 6908090 : _slp_tree::_slp_tree ()
108 : : {
109 : 6908090 : this->prev_node = NULL;
110 : 6908090 : if (slp_first_node)
111 : 6057925 : slp_first_node->prev_node = this;
112 : 6908090 : this->next_node = slp_first_node;
113 : 6908090 : slp_first_node = this;
114 : 6908090 : SLP_TREE_SCALAR_STMTS (this) = vNULL;
115 : 6908090 : SLP_TREE_SCALAR_OPS (this) = vNULL;
116 : 6908090 : SLP_TREE_VEC_DEFS (this) = vNULL;
117 : 6908090 : SLP_TREE_NUMBER_OF_VEC_STMTS (this) = 0;
118 : 6908090 : SLP_TREE_CHILDREN (this) = vNULL;
119 : 6908090 : SLP_TREE_LOAD_PERMUTATION (this) = vNULL;
120 : 6908090 : SLP_TREE_LANE_PERMUTATION (this) = vNULL;
121 : 6908090 : SLP_TREE_SIMD_CLONE_INFO (this) = vNULL;
122 : 6908090 : SLP_TREE_DEF_TYPE (this) = vect_uninitialized_def;
123 : 6908090 : SLP_TREE_CODE (this) = ERROR_MARK;
124 : 6908090 : this->ldst_lanes = false;
125 : 6908090 : this->avoid_stlf_fail = false;
126 : 6908090 : SLP_TREE_VECTYPE (this) = NULL_TREE;
127 : 6908090 : SLP_TREE_REPRESENTATIVE (this) = NULL;
128 : 6908090 : SLP_TREE_MEMORY_ACCESS_TYPE (this) = VMAT_INVARIANT;
129 : 6908090 : SLP_TREE_REF_COUNT (this) = 1;
130 : 6908090 : this->failed = NULL;
131 : 6908090 : this->max_nunits = 1;
132 : 6908090 : this->lanes = 0;
133 : 6908090 : }
134 : :
135 : : /* Tear down a SLP node. */
136 : :
137 : 6908090 : _slp_tree::~_slp_tree ()
138 : : {
139 : 6908090 : if (this->prev_node)
140 : 4489647 : this->prev_node->next_node = this->next_node;
141 : : else
142 : 2418443 : slp_first_node = this->next_node;
143 : 6908090 : if (this->next_node)
144 : 5046502 : this->next_node->prev_node = this->prev_node;
145 : 6908090 : SLP_TREE_CHILDREN (this).release ();
146 : 6908090 : SLP_TREE_SCALAR_STMTS (this).release ();
147 : 6908090 : SLP_TREE_SCALAR_OPS (this).release ();
148 : 6908090 : SLP_TREE_VEC_DEFS (this).release ();
149 : 6908090 : SLP_TREE_LOAD_PERMUTATION (this).release ();
150 : 6908090 : SLP_TREE_LANE_PERMUTATION (this).release ();
151 : 6908090 : SLP_TREE_SIMD_CLONE_INFO (this).release ();
152 : 6908090 : if (this->failed)
153 : 1855256 : free (failed);
154 : 6908090 : }
155 : :
156 : : /* Push the single SSA definition in DEF to the vector of vector defs. */
157 : :
158 : : void
159 : 477278 : _slp_tree::push_vec_def (gimple *def)
160 : : {
161 : 477278 : if (gphi *phi = dyn_cast <gphi *> (def))
162 : 54920 : vec_defs.quick_push (gimple_phi_result (phi));
163 : : else
164 : : {
165 : 422358 : def_operand_p defop = single_ssa_def_operand (def, SSA_OP_ALL_DEFS);
166 : 422358 : vec_defs.quick_push (get_def_from_ptr (defop));
167 : : }
168 : 477278 : }
169 : :
170 : : /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
171 : :
172 : : void
173 : 13042259 : vect_free_slp_tree (slp_tree node)
174 : : {
175 : 13042259 : int i;
176 : 13042259 : slp_tree child;
177 : :
178 : 13042259 : if (--SLP_TREE_REF_COUNT (node) != 0)
179 : 13042259 : return;
180 : :
181 : 10127089 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
182 : 3620370 : if (child)
183 : 3119718 : vect_free_slp_tree (child);
184 : :
185 : : /* If the node defines any SLP only patterns then those patterns are no
186 : : longer valid and should be removed. */
187 : 6506719 : stmt_vec_info rep_stmt_info = SLP_TREE_REPRESENTATIVE (node);
188 : 6506719 : if (rep_stmt_info && STMT_VINFO_SLP_VECT_ONLY_PATTERN (rep_stmt_info))
189 : : {
190 : 833 : stmt_vec_info stmt_info = vect_orig_stmt (rep_stmt_info);
191 : 833 : STMT_VINFO_IN_PATTERN_P (stmt_info) = false;
192 : 833 : STMT_SLP_TYPE (stmt_info) = STMT_SLP_TYPE (rep_stmt_info);
193 : : }
194 : :
195 : 6506719 : delete node;
196 : : }
197 : :
198 : : /* Return a location suitable for dumpings related to the SLP instance. */
199 : :
200 : : dump_user_location_t
201 : 3706551 : _slp_instance::location () const
202 : : {
203 : 3706551 : if (!root_stmts.is_empty ())
204 : 335398 : return root_stmts[0]->stmt;
205 : : else
206 : 3371153 : return SLP_TREE_SCALAR_STMTS (root)[0]->stmt;
207 : : }
208 : :
209 : :
210 : : /* Free the memory allocated for the SLP instance. */
211 : :
212 : : void
213 : 1516633 : vect_free_slp_instance (slp_instance instance)
214 : : {
215 : 1516633 : vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
216 : 1516633 : SLP_INSTANCE_LOADS (instance).release ();
217 : 1516633 : SLP_INSTANCE_ROOT_STMTS (instance).release ();
218 : 1516633 : SLP_INSTANCE_REMAIN_DEFS (instance).release ();
219 : 1516633 : instance->subgraph_entries.release ();
220 : 1516633 : instance->cost_vec.release ();
221 : 1516633 : free (instance);
222 : 1516633 : }
223 : :
224 : :
225 : : /* Create an SLP node for SCALAR_STMTS. */
226 : :
227 : : slp_tree
228 : 63379 : vect_create_new_slp_node (unsigned nops, tree_code code)
229 : : {
230 : 63379 : slp_tree node = new _slp_tree;
231 : 63379 : SLP_TREE_SCALAR_STMTS (node) = vNULL;
232 : 63379 : SLP_TREE_CHILDREN (node).create (nops);
233 : 63379 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
234 : 63379 : SLP_TREE_CODE (node) = code;
235 : 63379 : return node;
236 : : }
237 : : /* Create an SLP node for SCALAR_STMTS. */
238 : :
239 : : static slp_tree
240 : 3112112 : vect_create_new_slp_node (slp_tree node,
241 : : vec<stmt_vec_info> scalar_stmts, unsigned nops)
242 : : {
243 : 3112112 : SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
244 : 3112112 : SLP_TREE_CHILDREN (node).create (nops);
245 : 3112112 : SLP_TREE_DEF_TYPE (node) = vect_internal_def;
246 : 3112112 : SLP_TREE_REPRESENTATIVE (node) = scalar_stmts[0];
247 : 3112112 : SLP_TREE_LANES (node) = scalar_stmts.length ();
248 : 3112112 : return node;
249 : : }
250 : :
251 : : /* Create an SLP node for SCALAR_STMTS. */
252 : :
253 : : static slp_tree
254 : 5294 : vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts, unsigned nops)
255 : : {
256 : 5294 : return vect_create_new_slp_node (new _slp_tree, scalar_stmts, nops);
257 : : }
258 : :
259 : : /* Create an SLP node for OPS. */
260 : :
261 : : static slp_tree
262 : 1870615 : vect_create_new_slp_node (slp_tree node, vec<tree> ops)
263 : : {
264 : 1870615 : SLP_TREE_SCALAR_OPS (node) = ops;
265 : 1870615 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
266 : 0 : SLP_TREE_LANES (node) = ops.length ();
267 : 1870615 : return node;
268 : : }
269 : :
270 : : /* Create an SLP node for OPS. */
271 : :
272 : : static slp_tree
273 : 1870615 : vect_create_new_slp_node (vec<tree> ops)
274 : : {
275 : 1870615 : return vect_create_new_slp_node (new _slp_tree, ops);
276 : : }
277 : :
278 : :
279 : : /* This structure is used in creation of an SLP tree. Each instance
280 : : corresponds to the same operand in a group of scalar stmts in an SLP
281 : : node. */
282 : : typedef struct _slp_oprnd_info
283 : : {
284 : : /* Def-stmts for the operands. */
285 : : vec<stmt_vec_info> def_stmts;
286 : : /* Operands. */
287 : : vec<tree> ops;
288 : : /* Information about the first statement, its vector def-type, type, the
289 : : operand itself in case it's constant, and an indication if it's a pattern
290 : : stmt and gather/scatter info. */
291 : : tree first_op_type;
292 : : enum vect_def_type first_dt;
293 : : bool any_pattern;
294 : : bool first_gs_p;
295 : : gather_scatter_info first_gs_info;
296 : : } *slp_oprnd_info;
297 : :
298 : :
299 : : /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
300 : : operand. */
301 : : static vec<slp_oprnd_info>
302 : 3042361 : vect_create_oprnd_info (int nops, int group_size)
303 : : {
304 : 3042361 : int i;
305 : 3042361 : slp_oprnd_info oprnd_info;
306 : 3042361 : vec<slp_oprnd_info> oprnds_info;
307 : :
308 : 3042361 : oprnds_info.create (nops);
309 : 10841115 : for (i = 0; i < nops; i++)
310 : : {
311 : 4756393 : oprnd_info = XNEW (struct _slp_oprnd_info);
312 : 4756393 : oprnd_info->def_stmts.create (group_size);
313 : 4756393 : oprnd_info->ops.create (group_size);
314 : 4756393 : oprnd_info->first_dt = vect_uninitialized_def;
315 : 4756393 : oprnd_info->first_op_type = NULL_TREE;
316 : 4756393 : oprnd_info->any_pattern = false;
317 : 4756393 : oprnd_info->first_gs_p = false;
318 : 4756393 : oprnds_info.quick_push (oprnd_info);
319 : : }
320 : :
321 : 3042361 : return oprnds_info;
322 : : }
323 : :
324 : :
325 : : /* Free operands info. */
326 : :
327 : : static void
328 : 3042361 : vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
329 : : {
330 : 3042361 : int i;
331 : 3042361 : slp_oprnd_info oprnd_info;
332 : :
333 : 7798754 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
334 : : {
335 : 4756393 : oprnd_info->def_stmts.release ();
336 : 4756393 : oprnd_info->ops.release ();
337 : 4756393 : XDELETE (oprnd_info);
338 : : }
339 : :
340 : 3042361 : oprnds_info.release ();
341 : 3042361 : }
342 : :
343 : : /* Return the execution frequency of NODE (so that a higher value indicates
344 : : a "more important" node when optimizing for speed). */
345 : :
346 : : static sreal
347 : 3136276 : vect_slp_node_weight (slp_tree node)
348 : : {
349 : 3136276 : stmt_vec_info stmt_info = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (node));
350 : 3136276 : basic_block bb = gimple_bb (stmt_info->stmt);
351 : 3136276 : return bb->count.to_sreal_scale (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
352 : : }
353 : :
354 : : /* Return true if STMTS contains a pattern statement. */
355 : :
356 : : static bool
357 : 27785 : vect_contains_pattern_stmt_p (vec<stmt_vec_info> stmts)
358 : : {
359 : 27785 : stmt_vec_info stmt_info;
360 : 27785 : unsigned int i;
361 : 90472 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
362 : 65298 : if (stmt_info && is_pattern_stmt_p (stmt_info))
363 : : return true;
364 : : return false;
365 : : }
366 : :
367 : : /* Return true when all lanes in the external or constant NODE have
368 : : the same value. */
369 : :
370 : : static bool
371 : 635654 : vect_slp_tree_uniform_p (slp_tree node)
372 : : {
373 : 635654 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_constant_def
374 : : || SLP_TREE_DEF_TYPE (node) == vect_external_def);
375 : :
376 : : /* Pre-exsting vectors. */
377 : 1120084 : if (SLP_TREE_SCALAR_OPS (node).is_empty ())
378 : : return false;
379 : :
380 : : unsigned i;
381 : : tree op, first = NULL_TREE;
382 : 1450955 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
383 : 1299731 : if (!first)
384 : : first = op;
385 : 664077 : else if (!operand_equal_p (first, op, 0))
386 : : return false;
387 : :
388 : : return true;
389 : : }
390 : :
391 : : /* Find the place of the data-ref in STMT_INFO in the interleaving chain
392 : : that starts from FIRST_STMT_INFO. Return -1 if the data-ref is not a part
393 : : of the chain. */
394 : :
395 : : int
396 : 567981 : vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
397 : : stmt_vec_info first_stmt_info)
398 : : {
399 : 567981 : stmt_vec_info next_stmt_info = first_stmt_info;
400 : 567981 : int result = 0;
401 : :
402 : 567981 : if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
403 : : return -1;
404 : :
405 : 1583471 : do
406 : : {
407 : 1583471 : if (next_stmt_info == stmt_info)
408 : : return result;
409 : 1015490 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
410 : 1015490 : if (next_stmt_info)
411 : 1015490 : result += DR_GROUP_GAP (next_stmt_info);
412 : : }
413 : 1015490 : while (next_stmt_info);
414 : :
415 : : return -1;
416 : : }
417 : :
418 : : /* Check whether it is possible to load COUNT elements of type ELT_TYPE
419 : : using the method implemented by duplicate_and_interleave. Return true
420 : : if so, returning the number of intermediate vectors in *NVECTORS_OUT
421 : : (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
422 : : (if nonnull). */
423 : :
424 : : bool
425 : 0 : can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
426 : : tree elt_type, unsigned int *nvectors_out,
427 : : tree *vector_type_out,
428 : : tree *permutes)
429 : : {
430 : 0 : tree base_vector_type = get_vectype_for_scalar_type (vinfo, elt_type, count);
431 : 0 : if (!base_vector_type || !VECTOR_MODE_P (TYPE_MODE (base_vector_type)))
432 : 0 : return false;
433 : :
434 : 0 : machine_mode base_vector_mode = TYPE_MODE (base_vector_type);
435 : 0 : poly_int64 elt_bytes = count * GET_MODE_UNIT_SIZE (base_vector_mode);
436 : 0 : unsigned int nvectors = 1;
437 : 0 : for (;;)
438 : : {
439 : 0 : scalar_int_mode int_mode;
440 : 0 : poly_int64 elt_bits = elt_bytes * BITS_PER_UNIT;
441 : 0 : if (int_mode_for_size (elt_bits, 1).exists (&int_mode))
442 : : {
443 : : /* Get the natural vector type for this SLP group size. */
444 : 0 : tree int_type = build_nonstandard_integer_type
445 : 0 : (GET_MODE_BITSIZE (int_mode), 1);
446 : 0 : tree vector_type
447 : 0 : = get_vectype_for_scalar_type (vinfo, int_type, count);
448 : 0 : poly_int64 half_nelts;
449 : 0 : if (vector_type
450 : 0 : && VECTOR_MODE_P (TYPE_MODE (vector_type))
451 : 0 : && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
452 : : GET_MODE_SIZE (base_vector_mode))
453 : 0 : && multiple_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)),
454 : : 2, &half_nelts))
455 : : {
456 : : /* Try fusing consecutive sequences of COUNT / NVECTORS elements
457 : : together into elements of type INT_TYPE and using the result
458 : : to build NVECTORS vectors. */
459 : 0 : poly_uint64 nelts = GET_MODE_NUNITS (TYPE_MODE (vector_type));
460 : 0 : vec_perm_builder sel1 (nelts, 2, 3);
461 : 0 : vec_perm_builder sel2 (nelts, 2, 3);
462 : :
463 : 0 : for (unsigned int i = 0; i < 3; ++i)
464 : : {
465 : 0 : sel1.quick_push (i);
466 : 0 : sel1.quick_push (i + nelts);
467 : 0 : sel2.quick_push (half_nelts + i);
468 : 0 : sel2.quick_push (half_nelts + i + nelts);
469 : : }
470 : 0 : vec_perm_indices indices1 (sel1, 2, nelts);
471 : 0 : vec_perm_indices indices2 (sel2, 2, nelts);
472 : 0 : machine_mode vmode = TYPE_MODE (vector_type);
473 : 0 : if (can_vec_perm_const_p (vmode, vmode, indices1)
474 : 0 : && can_vec_perm_const_p (vmode, vmode, indices2))
475 : : {
476 : 0 : if (nvectors_out)
477 : 0 : *nvectors_out = nvectors;
478 : 0 : if (vector_type_out)
479 : 0 : *vector_type_out = vector_type;
480 : 0 : if (permutes)
481 : : {
482 : 0 : permutes[0] = vect_gen_perm_mask_checked (vector_type,
483 : : indices1);
484 : 0 : permutes[1] = vect_gen_perm_mask_checked (vector_type,
485 : : indices2);
486 : : }
487 : 0 : return true;
488 : : }
489 : 0 : }
490 : : }
491 : 0 : if (!multiple_p (elt_bytes, 2, &elt_bytes))
492 : : return false;
493 : 0 : nvectors *= 2;
494 : : /* We need to be able to fuse COUNT / NVECTORS elements together. */
495 : 0 : if (!multiple_p (count, nvectors))
496 : : return false;
497 : : }
498 : : }
499 : :
500 : : /* Return true if DTA and DTB match. */
501 : :
502 : : static bool
503 : 16843984 : vect_def_types_match (enum vect_def_type dta, enum vect_def_type dtb)
504 : : {
505 : 16843984 : return (dta == dtb
506 : 381133 : || ((dta == vect_external_def || dta == vect_constant_def)
507 : 242448 : && (dtb == vect_external_def || dtb == vect_constant_def)));
508 : : }
509 : :
510 : : static const int no_arg_map[] = { 0 };
511 : : static const int arg0_map[] = { 1, 0 };
512 : : static const int arg1_map[] = { 1, 1 };
513 : : static const int arg2_arg3_map[] = { 2, 2, 3 };
514 : : static const int arg1_arg3_map[] = { 2, 1, 3 };
515 : : static const int arg1_arg4_arg5_map[] = { 3, 1, 4, 5 };
516 : : static const int arg1_arg3_arg4_map[] = { 3, 1, 3, 4 };
517 : : static const int arg3_arg2_map[] = { 2, 3, 2 };
518 : : static const int op1_op0_map[] = { 2, 1, 0 };
519 : : static const int off_map[] = { 1, -3 };
520 : : static const int off_op0_map[] = { 2, -3, 0 };
521 : : static const int off_arg2_arg3_map[] = { 3, -3, 2, 3 };
522 : : static const int off_arg3_arg2_map[] = { 3, -3, 3, 2 };
523 : : static const int mask_call_maps[6][7] = {
524 : : { 1, 1, },
525 : : { 2, 1, 2, },
526 : : { 3, 1, 2, 3, },
527 : : { 4, 1, 2, 3, 4, },
528 : : { 5, 1, 2, 3, 4, 5, },
529 : : { 6, 1, 2, 3, 4, 5, 6 },
530 : : };
531 : :
532 : : /* For most SLP statements, there is a one-to-one mapping between
533 : : gimple arguments and child nodes. If that is not true for STMT,
534 : : return an array that contains:
535 : :
536 : : - the number of child nodes, followed by
537 : : - for each child node, the index of the argument associated with that node.
538 : : The special index -1 is the first operand of an embedded comparison and
539 : : the special index -2 is the second operand of an embedded comparison.
540 : : The special indes -3 is the offset of a gather as analyzed by
541 : : vect_check_gather_scatter.
542 : :
543 : : SWAP is as for vect_get_and_check_slp_defs. */
544 : :
545 : : static const int *
546 : 18785662 : vect_get_operand_map (const gimple *stmt, bool gather_scatter_p = false,
547 : : unsigned char swap = 0)
548 : : {
549 : 18785662 : if (auto assign = dyn_cast<const gassign *> (stmt))
550 : : {
551 : 17397603 : if (gimple_assign_rhs_code (assign) == COND_EXPR
552 : 17397603 : && COMPARISON_CLASS_P (gimple_assign_rhs1 (assign)))
553 : 0 : gcc_unreachable ();
554 : 17397603 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) == tcc_comparison
555 : 17397603 : && swap)
556 : : return op1_op0_map;
557 : 16991266 : if (gather_scatter_p)
558 : 22522 : return (TREE_CODE (gimple_assign_lhs (assign)) != SSA_NAME
559 : 22522 : ? off_op0_map : off_map);
560 : : }
561 : 18356803 : gcc_assert (!swap);
562 : 18356803 : if (auto call = dyn_cast<const gcall *> (stmt))
563 : : {
564 : 141033 : if (gimple_call_internal_p (call))
565 : 67141 : switch (gimple_call_internal_fn (call))
566 : : {
567 : 10706 : case IFN_MASK_LOAD:
568 : 17428 : return gather_scatter_p ? off_arg2_arg3_map : arg2_arg3_map;
569 : :
570 : 0 : case IFN_GATHER_LOAD:
571 : 0 : return arg1_map;
572 : :
573 : 0 : case IFN_MASK_GATHER_LOAD:
574 : 0 : case IFN_MASK_LEN_GATHER_LOAD:
575 : 0 : return arg1_arg4_arg5_map;
576 : :
577 : 0 : case IFN_SCATTER_STORE:
578 : 0 : return arg1_arg3_map;
579 : :
580 : 0 : case IFN_MASK_SCATTER_STORE:
581 : 0 : case IFN_MASK_LEN_SCATTER_STORE:
582 : 0 : return arg1_arg3_arg4_map;
583 : :
584 : 5276 : case IFN_MASK_STORE:
585 : 9460 : return gather_scatter_p ? off_arg3_arg2_map : arg3_arg2_map;
586 : :
587 : 776 : case IFN_MASK_CALL:
588 : 776 : {
589 : 776 : unsigned nargs = gimple_call_num_args (call);
590 : 776 : if (nargs >= 2 && nargs <= 7)
591 : 776 : return mask_call_maps[nargs-2];
592 : : else
593 : : return nullptr;
594 : : }
595 : :
596 : 140 : case IFN_CLZ:
597 : 140 : case IFN_CTZ:
598 : 140 : return arg0_map;
599 : :
600 : 6296 : case IFN_GOMP_SIMD_LANE:
601 : 6296 : return no_arg_map;
602 : :
603 : : default:
604 : : break;
605 : : }
606 : : }
607 : : return nullptr;
608 : : }
609 : :
610 : : /* Return the SLP node child index for operand OP of STMT. */
611 : :
612 : : int
613 : 1389437 : vect_slp_child_index_for_operand (const gimple *stmt, int op,
614 : : bool gather_scatter_p)
615 : : {
616 : 1389437 : const int *opmap = vect_get_operand_map (stmt, gather_scatter_p);
617 : 1389437 : if (!opmap)
618 : : return op;
619 : 18671 : for (int i = 1; i < 1 + opmap[0]; ++i)
620 : 18671 : if (opmap[i] == op)
621 : 10045 : return i - 1;
622 : 0 : gcc_unreachable ();
623 : : }
624 : :
625 : : /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
626 : : they are of a valid type and that they match the defs of the first stmt of
627 : : the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
628 : : by swapping operands of STMTS[STMT_NUM] when possible. Non-zero SWAP
629 : : indicates swap is required for cond_expr stmts. Specifically, SWAP
630 : : is 1 if STMT is cond and operands of comparison need to be swapped;
631 : : SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
632 : :
633 : : If there was a fatal error return -1; if the error could be corrected by
634 : : swapping operands of father node of this one, return 1; if everything is
635 : : ok return 0. */
636 : : static int
637 : 12417581 : vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char swap,
638 : : bool *skip_args,
639 : : vec<stmt_vec_info> stmts, unsigned stmt_num,
640 : : vec<slp_oprnd_info> *oprnds_info)
641 : : {
642 : 12417581 : stmt_vec_info stmt_info = stmts[stmt_num];
643 : 12417581 : tree oprnd;
644 : 12417581 : unsigned int i, number_of_oprnds;
645 : 12417581 : enum vect_def_type dt = vect_uninitialized_def;
646 : 12417581 : slp_oprnd_info oprnd_info;
647 : 12417581 : gather_scatter_info gs_info;
648 : 12417581 : unsigned int gs_op = -1u;
649 : 12417581 : unsigned int commutative_op = -1U;
650 : 12417581 : bool first = stmt_num == 0;
651 : :
652 : 12417581 : if (!stmt_info)
653 : : {
654 : 0 : for (auto oi : *oprnds_info)
655 : : {
656 : 0 : oi->def_stmts.quick_push (NULL);
657 : 0 : oi->ops.quick_push (NULL_TREE);
658 : : }
659 : : return 0;
660 : : }
661 : :
662 : 12417581 : if (!is_a<gcall *> (stmt_info->stmt)
663 : : && !is_a<gassign *> (stmt_info->stmt)
664 : : && !is_a<gphi *> (stmt_info->stmt))
665 : : return -1;
666 : :
667 : 12417581 : number_of_oprnds = gimple_num_args (stmt_info->stmt);
668 : 12417581 : const int *map
669 : 24835162 : = vect_get_operand_map (stmt_info->stmt,
670 : 12417581 : STMT_VINFO_GATHER_SCATTER_P (stmt_info), swap);
671 : 12417581 : if (map)
672 : 424248 : number_of_oprnds = *map++;
673 : 12417581 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
674 : : {
675 : 40891 : if (gimple_call_internal_p (stmt))
676 : : {
677 : 21362 : internal_fn ifn = gimple_call_internal_fn (stmt);
678 : 21362 : commutative_op = first_commutative_argument (ifn);
679 : : }
680 : : }
681 : 12376690 : else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
682 : : {
683 : 14517453 : if (commutative_tree_code (gimple_assign_rhs_code (stmt)))
684 : 12417581 : commutative_op = 0;
685 : : }
686 : :
687 : 12417581 : bool swapped = (swap != 0);
688 : 12417581 : bool backedge = false;
689 : 12417581 : enum vect_def_type *dts = XALLOCAVEC (enum vect_def_type, number_of_oprnds);
690 : 34167808 : for (i = 0; i < number_of_oprnds; i++)
691 : : {
692 : 21753052 : oprnd_info = (*oprnds_info)[i];
693 : 21753052 : int opno = map ? map[i] : int (i);
694 : 21753052 : if (opno == -3)
695 : : {
696 : 11525 : gcc_assert (STMT_VINFO_GATHER_SCATTER_P (stmt_info));
697 : 11525 : if (!is_a <loop_vec_info> (vinfo)
698 : 11525 : || !vect_check_gather_scatter (stmt_info,
699 : : as_a <loop_vec_info> (vinfo),
700 : : first ? &oprnd_info->first_gs_info
701 : : : &gs_info))
702 : 2825 : return -1;
703 : :
704 : 11525 : if (first)
705 : : {
706 : 11358 : oprnd_info->first_gs_p = true;
707 : 11358 : oprnd = oprnd_info->first_gs_info.offset;
708 : : }
709 : : else
710 : : {
711 : 167 : gs_op = i;
712 : 167 : oprnd = gs_info.offset;
713 : : }
714 : : }
715 : 21741527 : else if (opno < 0)
716 : 0 : oprnd = TREE_OPERAND (gimple_arg (stmt_info->stmt, 0), -1 - opno);
717 : : else
718 : : {
719 : 21741527 : oprnd = gimple_arg (stmt_info->stmt, opno);
720 : 21741527 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
721 : : {
722 : 1295643 : edge e = gimple_phi_arg_edge (stmt, opno);
723 : 2591286 : backedge = (is_a <bb_vec_info> (vinfo)
724 : 1938209 : ? e->flags & EDGE_DFS_BACK
725 : 642566 : : dominated_by_p (CDI_DOMINATORS, e->src,
726 : 642566 : gimple_bb (stmt_info->stmt)));
727 : : }
728 : : }
729 : 21753052 : if (TREE_CODE (oprnd) == VIEW_CONVERT_EXPR)
730 : 2534 : oprnd = TREE_OPERAND (oprnd, 0);
731 : :
732 : 21753052 : stmt_vec_info def_stmt_info;
733 : 21753052 : if (!vect_is_simple_use (oprnd, vinfo, &dts[i], &def_stmt_info))
734 : : {
735 : 1321 : if (dump_enabled_p ())
736 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
737 : : "Build SLP failed: can't analyze def for %T\n",
738 : : oprnd);
739 : :
740 : 1321 : return -1;
741 : : }
742 : :
743 : 21751731 : if (skip_args[i])
744 : : {
745 : 580743 : oprnd_info->def_stmts.quick_push (NULL);
746 : 580743 : oprnd_info->ops.quick_push (NULL_TREE);
747 : 580743 : oprnd_info->first_dt = vect_uninitialized_def;
748 : 580743 : continue;
749 : : }
750 : :
751 : 21170988 : oprnd_info->def_stmts.quick_push (def_stmt_info);
752 : 21170988 : oprnd_info->ops.quick_push (oprnd);
753 : :
754 : 21170988 : if (def_stmt_info
755 : 21170988 : && is_pattern_stmt_p (def_stmt_info))
756 : : {
757 : 316188 : if (STMT_VINFO_RELATED_STMT (vect_orig_stmt (def_stmt_info))
758 : : != def_stmt_info)
759 : 229198 : oprnd_info->any_pattern = true;
760 : : else
761 : : /* If we promote this to external use the original stmt def. */
762 : 86990 : oprnd_info->ops.last ()
763 : 173980 : = gimple_get_lhs (vect_orig_stmt (def_stmt_info)->stmt);
764 : : }
765 : :
766 : : /* If there's a extern def on a backedge make sure we can
767 : : code-generate at the region start.
768 : : ??? This is another case that could be fixed by adjusting
769 : : how we split the function but at the moment we'd have conflicting
770 : : goals there. */
771 : 21170988 : if (backedge
772 : 111049 : && dts[i] == vect_external_def
773 : 1528 : && is_a <bb_vec_info> (vinfo)
774 : 1528 : && TREE_CODE (oprnd) == SSA_NAME
775 : 1504 : && !SSA_NAME_IS_DEFAULT_DEF (oprnd)
776 : 21172492 : && !dominated_by_p (CDI_DOMINATORS, vinfo->bbs[0],
777 : 1504 : gimple_bb (SSA_NAME_DEF_STMT (oprnd))))
778 : : {
779 : 1504 : if (dump_enabled_p ())
780 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
781 : : "Build SLP failed: extern def %T only defined "
782 : : "on backedge\n", oprnd);
783 : 1504 : return -1;
784 : : }
785 : :
786 : 21169484 : if (first)
787 : : {
788 : 4179467 : tree type = TREE_TYPE (oprnd);
789 : 4179467 : dt = dts[i];
790 : :
791 : : /* For the swapping logic below force vect_reduction_def
792 : : for the reduction op in a SLP reduction group. */
793 : 4179467 : if (!STMT_VINFO_DATA_REF (stmt_info)
794 : 3034536 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
795 : 997 : && (int)i == STMT_VINFO_REDUC_IDX (stmt_info)
796 : 4179954 : && def_stmt_info)
797 : 487 : dts[i] = dt = vect_reduction_def;
798 : :
799 : : /* Check the types of the definition. */
800 : 4179467 : switch (dt)
801 : : {
802 : 4179467 : case vect_external_def:
803 : 4179467 : case vect_constant_def:
804 : 4179467 : case vect_internal_def:
805 : 4179467 : case vect_reduction_def:
806 : 4179467 : case vect_double_reduction_def:
807 : 4179467 : case vect_induction_def:
808 : 4179467 : case vect_nested_cycle:
809 : 4179467 : case vect_first_order_recurrence:
810 : 4179467 : break;
811 : :
812 : 0 : default:
813 : : /* FORNOW: Not supported. */
814 : 0 : if (dump_enabled_p ())
815 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
816 : : "Build SLP failed: illegal type of def %T\n",
817 : : oprnd);
818 : 0 : return -1;
819 : : }
820 : :
821 : 4179467 : oprnd_info->first_dt = dt;
822 : 4179467 : oprnd_info->first_op_type = type;
823 : : }
824 : : }
825 : 12414756 : if (first)
826 : : return 0;
827 : :
828 : : /* Now match the operand definition types to that of the first stmt. */
829 : 26027217 : for (i = 0; i < number_of_oprnds;)
830 : : {
831 : 16964036 : if (skip_args[i])
832 : : {
833 : 8694 : ++i;
834 : 8694 : continue;
835 : : }
836 : :
837 : 16955342 : oprnd_info = (*oprnds_info)[i];
838 : 16955342 : dt = dts[i];
839 : 16955342 : stmt_vec_info def_stmt_info = oprnd_info->def_stmts[stmt_num];
840 : 16955342 : oprnd = oprnd_info->ops[stmt_num];
841 : 16955342 : tree type = TREE_TYPE (oprnd);
842 : :
843 : 16955342 : if (!types_compatible_p (oprnd_info->first_op_type, type))
844 : : {
845 : 115058 : if (dump_enabled_p ())
846 : 229 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
847 : : "Build SLP failed: different operand types\n");
848 : 115058 : return 1;
849 : : }
850 : :
851 : 16840284 : if ((gs_op == i) != oprnd_info->first_gs_p)
852 : : {
853 : 0 : if (dump_enabled_p ())
854 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
855 : : "Build SLP failed: mixed gather and non-gather\n");
856 : 0 : return 1;
857 : : }
858 : 16840284 : else if (gs_op == i)
859 : : {
860 : 145 : if (!operand_equal_p (oprnd_info->first_gs_info.base,
861 : 145 : gs_info.base))
862 : : {
863 : 11 : if (dump_enabled_p ())
864 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
865 : : "Build SLP failed: different gather base\n");
866 : 11 : return 1;
867 : : }
868 : 134 : if (oprnd_info->first_gs_info.scale != gs_info.scale)
869 : : {
870 : 5 : if (dump_enabled_p ())
871 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
872 : : "Build SLP failed: different gather scale\n");
873 : 5 : return 1;
874 : : }
875 : : }
876 : :
877 : : /* Not first stmt of the group, check that the def-stmt/s match
878 : : the def-stmt/s of the first stmt. Allow different definition
879 : : types for reduction chains: the first stmt must be a
880 : : vect_reduction_def (a phi node), and the rest
881 : : end in the reduction chain. */
882 : 16840268 : if ((!vect_def_types_match (oprnd_info->first_dt, dt)
883 : 319270 : && !(oprnd_info->first_dt == vect_reduction_def
884 : 3466 : && !STMT_VINFO_DATA_REF (stmt_info)
885 : 3466 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
886 : 1332 : && def_stmt_info
887 : 1328 : && !STMT_VINFO_DATA_REF (def_stmt_info)
888 : 1322 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
889 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info))))
890 : 16522191 : || (!STMT_VINFO_DATA_REF (stmt_info)
891 : 15130228 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
892 : 3476 : && ((!def_stmt_info
893 : 3401 : || STMT_VINFO_DATA_REF (def_stmt_info)
894 : 5976 : || (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
895 : : != REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
896 : 3476 : != (oprnd_info->first_dt != vect_reduction_def))))
897 : : {
898 : : /* Try swapping operands if we got a mismatch. For BB
899 : : vectorization only in case it will clearly improve things. */
900 : 320021 : if (i == commutative_op && !swapped
901 : 318390 : && (!is_a <bb_vec_info> (vinfo)
902 : 2968 : || (!vect_def_types_match ((*oprnds_info)[i+1]->first_dt,
903 : 2968 : dts[i+1])
904 : 500 : && (vect_def_types_match (oprnd_info->first_dt, dts[i+1])
905 : : || vect_def_types_match
906 : 248 : ((*oprnds_info)[i+1]->first_dt, dts[i])))))
907 : : {
908 : 1631 : if (dump_enabled_p ())
909 : 326 : dump_printf_loc (MSG_NOTE, vect_location,
910 : : "trying swapped operands\n");
911 : 1631 : std::swap (dts[i], dts[i+1]);
912 : 1631 : std::swap ((*oprnds_info)[i]->def_stmts[stmt_num],
913 : 1631 : (*oprnds_info)[i+1]->def_stmts[stmt_num]);
914 : 1631 : std::swap ((*oprnds_info)[i]->ops[stmt_num],
915 : 1631 : (*oprnds_info)[i+1]->ops[stmt_num]);
916 : : /* After swapping some operands we lost track whether an
917 : : operand has any pattern defs so be conservative here. */
918 : 1631 : if ((*oprnds_info)[i]->any_pattern
919 : 1631 : || (*oprnds_info)[i+1]->any_pattern)
920 : 8 : (*oprnds_info)[i]->any_pattern
921 : 4 : = (*oprnds_info)[i+1]->any_pattern = true;
922 : 1631 : swapped = true;
923 : 1631 : continue;
924 : : }
925 : :
926 : 316759 : if (is_a <bb_vec_info> (vinfo)
927 : 307205 : && !oprnd_info->any_pattern
928 : 623724 : && number_of_oprnds > 1)
929 : : {
930 : : /* Now for commutative ops we should see whether we can
931 : : make the other operand matching. */
932 : 118364 : if (dump_enabled_p ())
933 : 149 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
934 : : "treating operand as external\n");
935 : 118364 : oprnd_info->first_dt = dt = vect_external_def;
936 : : }
937 : : else
938 : : {
939 : 198395 : if (dump_enabled_p ())
940 : 456 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
941 : : "Build SLP failed: different types\n");
942 : 198395 : return 1;
943 : : }
944 : : }
945 : :
946 : : /* Make sure to demote the overall operand to external. */
947 : 16640242 : if (dt == vect_external_def)
948 : 418842 : oprnd_info->first_dt = vect_external_def;
949 : : /* For a SLP reduction chain we want to duplicate the reduction to
950 : : each of the chain members. That gets us a sane SLP graph (still
951 : : the stmts are not 100% correct wrt the initial values). */
952 : 16221400 : else if ((dt == vect_internal_def
953 : 16221400 : || dt == vect_reduction_def)
954 : 15310245 : && oprnd_info->first_dt == vect_reduction_def
955 : 10875 : && !STMT_VINFO_DATA_REF (stmt_info)
956 : 10875 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
957 : 1519 : && !STMT_VINFO_DATA_REF (def_stmt_info)
958 : 16222919 : && (REDUC_GROUP_FIRST_ELEMENT (def_stmt_info)
959 : : == REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
960 : : {
961 : 1519 : oprnd_info->def_stmts[stmt_num] = oprnd_info->def_stmts[0];
962 : 1519 : oprnd_info->ops[stmt_num] = oprnd_info->ops[0];
963 : : }
964 : :
965 : 16640242 : ++i;
966 : : }
967 : :
968 : : /* Swap operands. */
969 : 9063181 : if (swapped)
970 : : {
971 : 119647 : if (dump_enabled_p ())
972 : 943 : dump_printf_loc (MSG_NOTE, vect_location,
973 : : "swapped operands to match def types in %G",
974 : : stmt_info->stmt);
975 : : }
976 : :
977 : : return 0;
978 : : }
979 : :
980 : : /* Return true if call statements CALL1 and CALL2 are similar enough
981 : : to be combined into the same SLP group. */
982 : :
983 : : bool
984 : 26356 : compatible_calls_p (gcall *call1, gcall *call2, bool allow_two_operators)
985 : : {
986 : 26356 : unsigned int nargs = gimple_call_num_args (call1);
987 : 26356 : if (nargs != gimple_call_num_args (call2))
988 : : return false;
989 : :
990 : 23503 : auto cfn1 = gimple_call_combined_fn (call1);
991 : 23503 : auto cfn2 = gimple_call_combined_fn (call2);
992 : 23503 : if (cfn1 != cfn2
993 : 2 : && (!allow_two_operators
994 : 2 : || !((cfn1 == CFN_FMA || cfn1 == CFN_FMS)
995 : 2 : && (cfn2 == CFN_FMA || cfn2 == CFN_FMS))))
996 : : return false;
997 : :
998 : 23503 : if (gimple_call_internal_p (call1))
999 : : {
1000 : 6389 : if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
1001 : 6389 : TREE_TYPE (gimple_call_lhs (call2))))
1002 : : return false;
1003 : 12985 : for (unsigned int i = 0; i < nargs; ++i)
1004 : 6596 : if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
1005 : 6596 : TREE_TYPE (gimple_call_arg (call2, i))))
1006 : : return false;
1007 : : }
1008 : : else
1009 : : {
1010 : 17114 : if (!operand_equal_p (gimple_call_fn (call1),
1011 : 17114 : gimple_call_fn (call2), 0))
1012 : : return false;
1013 : :
1014 : 36780 : if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
1015 : : return false;
1016 : : }
1017 : :
1018 : : /* Check that any unvectorized arguments are equal. */
1019 : 18649 : if (const int *map = vect_get_operand_map (call1))
1020 : : {
1021 : 15 : unsigned int nkept = *map++;
1022 : 15 : unsigned int mapi = 0;
1023 : 57 : for (unsigned int i = 0; i < nargs; ++i)
1024 : 42 : if (mapi < nkept && map[mapi] == int (i))
1025 : 27 : mapi += 1;
1026 : 15 : else if (!operand_equal_p (gimple_call_arg (call1, i),
1027 : 15 : gimple_call_arg (call2, i)))
1028 : : return false;
1029 : : }
1030 : :
1031 : : return true;
1032 : : }
1033 : :
1034 : : /* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
1035 : : caller's attempt to find the vector type in STMT_INFO with the narrowest
1036 : : element type. Return true if VECTYPE is nonnull and if it is valid
1037 : : for STMT_INFO. When returning true, update MAX_NUNITS to reflect the
1038 : : number of units in VECTYPE. GROUP_SIZE and MAX_NUNITS are as for
1039 : : vect_build_slp_tree. */
1040 : :
1041 : : static bool
1042 : 5090021 : vect_record_max_nunits (vec_info *vinfo, stmt_vec_info stmt_info,
1043 : : unsigned int group_size,
1044 : : tree vectype, poly_uint64 *max_nunits)
1045 : : {
1046 : 5090021 : if (!vectype)
1047 : : {
1048 : 0 : if (dump_enabled_p ())
1049 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1050 : : "Build SLP failed: unsupported data-type in %G\n",
1051 : : stmt_info->stmt);
1052 : : /* Fatal mismatch. */
1053 : 0 : return false;
1054 : : }
1055 : :
1056 : : /* If populating the vector type requires unrolling then fail
1057 : : before adjusting *max_nunits for basic-block vectorization. */
1058 : 5090021 : if (is_a <bb_vec_info> (vinfo)
1059 : 5090021 : && !multiple_p (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
1060 : : {
1061 : 140203 : if (dump_enabled_p ())
1062 : 34 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1063 : : "Build SLP failed: unrolling required "
1064 : : "in basic block SLP\n");
1065 : : /* Fatal mismatch. */
1066 : 140203 : return false;
1067 : : }
1068 : :
1069 : : /* In case of multiple types we need to detect the smallest type. */
1070 : 4949818 : vect_update_max_nunits (max_nunits, vectype);
1071 : 4949818 : return true;
1072 : : }
1073 : :
1074 : : /* Verify if the scalar stmts STMTS are isomorphic, require data
1075 : : permutation or are of unsupported types of operation. Return
1076 : : true if they are, otherwise return false and indicate in *MATCHES
1077 : : which stmts are not isomorphic to the first one. If MATCHES[0]
1078 : : is false then this indicates the comparison could not be
1079 : : carried out or the stmts will never be vectorized by SLP.
1080 : :
1081 : : Note COND_EXPR is possibly isomorphic to another one after swapping its
1082 : : operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
1083 : : the first stmt by swapping the two operands of comparison; set SWAP[i]
1084 : : to 2 if stmt I is isormorphic to the first stmt by inverting the code
1085 : : of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
1086 : : to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
1087 : :
1088 : : static bool
1089 : 4959985 : vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
1090 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1091 : : poly_uint64 *max_nunits, bool *matches,
1092 : : bool *two_operators, tree *node_vectype)
1093 : : {
1094 : 4959985 : unsigned int i;
1095 : 4959985 : stmt_vec_info first_stmt_info = stmts[0];
1096 : 4959985 : code_helper first_stmt_code = ERROR_MARK;
1097 : 4959985 : code_helper alt_stmt_code = ERROR_MARK;
1098 : 4959985 : code_helper first_cond_code = ERROR_MARK;
1099 : 4959985 : bool need_same_oprnds = false;
1100 : 4959985 : tree first_lhs = NULL_TREE;
1101 : 4959985 : tree first_op1 = NULL_TREE;
1102 : 4959985 : stmt_vec_info first_load = NULL, prev_first_load = NULL;
1103 : 4959985 : bool first_stmt_ldst_p = false, first_stmt_ldst_masklen_p = false;
1104 : 4959985 : bool first_stmt_phi_p = false;
1105 : 4959985 : int first_reduc_idx = -1;
1106 : 4959985 : bool maybe_soft_fail = false;
1107 : 4959985 : tree soft_fail_nunits_vectype = NULL_TREE;
1108 : :
1109 : 4959985 : tree vectype, nunits_vectype;
1110 : 4959985 : if (!vect_get_vector_types_for_stmt (vinfo, first_stmt_info, &vectype,
1111 : : &nunits_vectype, group_size))
1112 : : {
1113 : : /* Fatal mismatch. */
1114 : 186841 : matches[0] = false;
1115 : 186841 : return false;
1116 : : }
1117 : : /* Record nunits required but continue analysis, producing matches[]
1118 : : as if nunits was not an issue. This allows splitting of groups
1119 : : to happen. */
1120 : 4773144 : if (nunits_vectype
1121 : 4773144 : && !vect_record_max_nunits (vinfo, first_stmt_info, group_size,
1122 : : nunits_vectype, max_nunits))
1123 : : {
1124 : 140203 : gcc_assert (is_a <bb_vec_info> (vinfo));
1125 : 140203 : maybe_soft_fail = true;
1126 : 140203 : soft_fail_nunits_vectype = nunits_vectype;
1127 : : }
1128 : :
1129 : 4773144 : gcc_assert (vectype);
1130 : 4773144 : *node_vectype = vectype;
1131 : :
1132 : : /* For every stmt in NODE find its def stmt/s. */
1133 : 4773144 : stmt_vec_info stmt_info;
1134 : 21285091 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
1135 : : {
1136 : 16721345 : bool ldst_p = false;
1137 : 16721345 : bool ldst_masklen_p = false;
1138 : 16721345 : bool phi_p = false;
1139 : 16721345 : code_helper rhs_code = ERROR_MARK;
1140 : :
1141 : 16721345 : swap[i] = 0;
1142 : 16721345 : matches[i] = false;
1143 : 16721345 : if (!stmt_info)
1144 : : {
1145 : 33509 : matches[i] = true;
1146 : 16545456 : continue;
1147 : : }
1148 : :
1149 : 16687836 : gimple *stmt = stmt_info->stmt;
1150 : 16687836 : if (dump_enabled_p ())
1151 : 194701 : dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for %G", stmt);
1152 : :
1153 : : /* Fail to vectorize statements marked as unvectorizable, throw
1154 : : or are volatile. */
1155 : 16687836 : if (!STMT_VINFO_VECTORIZABLE (stmt_info)
1156 : 16453886 : || stmt_can_throw_internal (cfun, stmt)
1157 : 32203139 : || gimple_has_volatile_ops (stmt))
1158 : : {
1159 : 240690 : if (dump_enabled_p ())
1160 : 269 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1161 : : "Build SLP failed: unvectorizable statement %G",
1162 : : stmt);
1163 : : /* ??? For BB vectorization we want to commutate operands in a way
1164 : : to shuffle all unvectorizable defs into one operand and have
1165 : : the other still vectorized. The following doesn't reliably
1166 : : work for this though but it's the easiest we can do here. */
1167 : 240690 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1168 : 78513 : continue;
1169 : : /* Fatal mismatch. */
1170 : 162177 : matches[0] = false;
1171 : 162177 : return false;
1172 : : }
1173 : :
1174 : 16447146 : gcall *call_stmt = dyn_cast <gcall *> (stmt);
1175 : 16447146 : tree lhs = gimple_get_lhs (stmt);
1176 : 16447146 : if (lhs == NULL_TREE
1177 : 16447146 : && (!call_stmt
1178 : 1347 : || !gimple_call_internal_p (stmt)
1179 : 1347 : || !internal_store_fn_p (gimple_call_internal_fn (stmt))))
1180 : : {
1181 : 60 : if (dump_enabled_p ())
1182 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1183 : : "Build SLP failed: not GIMPLE_ASSIGN nor "
1184 : : "GIMPLE_CALL %G", stmt);
1185 : 60 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1186 : 60 : continue;
1187 : : /* Fatal mismatch. */
1188 : 0 : matches[0] = false;
1189 : 0 : return false;
1190 : : }
1191 : :
1192 : 16447086 : if (call_stmt)
1193 : : {
1194 : 103633 : combined_fn cfn = gimple_call_combined_fn (call_stmt);
1195 : 103633 : if (cfn != CFN_LAST && cfn != CFN_MASK_CALL)
1196 : 49483 : rhs_code = cfn;
1197 : : else
1198 : : rhs_code = CALL_EXPR;
1199 : :
1200 : 103633 : if (cfn == CFN_GATHER_LOAD
1201 : 103633 : || cfn == CFN_SCATTER_STORE)
1202 : : ldst_p = true;
1203 : : else if (cfn == CFN_MASK_LOAD
1204 : : || cfn == CFN_MASK_GATHER_LOAD
1205 : : || cfn == CFN_MASK_LEN_GATHER_LOAD
1206 : : || cfn == CFN_MASK_SCATTER_STORE
1207 : : || cfn == CFN_MASK_LEN_SCATTER_STORE)
1208 : : {
1209 : : ldst_p = true;
1210 : : ldst_masklen_p = true;
1211 : : }
1212 : : else if (cfn == CFN_MASK_STORE)
1213 : : {
1214 : : ldst_p = true;
1215 : : ldst_masklen_p = true;
1216 : : rhs_code = CFN_MASK_STORE;
1217 : : }
1218 : : else if (cfn == CFN_GOMP_SIMD_LANE)
1219 : : ;
1220 : 95945 : else if ((cfn != CFN_LAST
1221 : : && cfn != CFN_MASK_CALL
1222 : 41795 : && internal_fn_p (cfn)
1223 : 29767 : && !vectorizable_internal_fn_p (as_internal_fn (cfn)))
1224 : 95880 : || gimple_call_tail_p (call_stmt)
1225 : 95880 : || gimple_call_noreturn_p (call_stmt)
1226 : 191825 : || gimple_call_chain (call_stmt))
1227 : : {
1228 : 665 : if (dump_enabled_p ())
1229 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1230 : : "Build SLP failed: unsupported call type %G",
1231 : : (gimple *) call_stmt);
1232 : 665 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1233 : 87 : continue;
1234 : : /* Fatal mismatch. */
1235 : 578 : matches[0] = false;
1236 : 578 : return false;
1237 : : }
1238 : : }
1239 : 16343453 : else if (gimple_code (stmt) == GIMPLE_PHI)
1240 : : {
1241 : : rhs_code = ERROR_MARK;
1242 : : phi_p = true;
1243 : : }
1244 : : else
1245 : : {
1246 : 15404870 : rhs_code = gimple_assign_rhs_code (stmt);
1247 : 15404870 : ldst_p = STMT_VINFO_DATA_REF (stmt_info) != nullptr;
1248 : : }
1249 : :
1250 : : /* Check the operation. */
1251 : 16446421 : if (i == 0)
1252 : : {
1253 : 4610389 : first_lhs = lhs;
1254 : 4610389 : first_stmt_code = rhs_code;
1255 : 4610389 : first_stmt_ldst_p = ldst_p;
1256 : 4610389 : first_stmt_ldst_masklen_p = ldst_masklen_p;
1257 : 4610389 : first_stmt_phi_p = phi_p;
1258 : 4610389 : first_reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
1259 : :
1260 : : /* Shift arguments should be equal in all the packed stmts for a
1261 : : vector shift with scalar shift operand. */
1262 : 4610389 : if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
1263 : 4480966 : || rhs_code == LROTATE_EXPR
1264 : 9091322 : || rhs_code == RROTATE_EXPR)
1265 : : {
1266 : : /* First see if we have a vector/vector shift. */
1267 : 129982 : if (!directly_supported_p (rhs_code, vectype, optab_vector))
1268 : : {
1269 : : /* No vector/vector shift, try for a vector/scalar shift. */
1270 : 117427 : if (!directly_supported_p (rhs_code, vectype, optab_scalar))
1271 : : {
1272 : 25726 : if (dump_enabled_p ())
1273 : 77 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1274 : : "Build SLP failed: "
1275 : : "op not supported by target.\n");
1276 : 25726 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1277 : : continue;
1278 : : /* Fatal mismatch. */
1279 : 25726 : matches[0] = false;
1280 : 25726 : return false;
1281 : : }
1282 : 91701 : need_same_oprnds = true;
1283 : 91701 : first_op1 = gimple_assign_rhs2 (stmt);
1284 : : }
1285 : : }
1286 : 4480407 : else if (rhs_code == WIDEN_LSHIFT_EXPR)
1287 : : {
1288 : 0 : need_same_oprnds = true;
1289 : 0 : first_op1 = gimple_assign_rhs2 (stmt);
1290 : : }
1291 : 4480407 : else if (!ldst_p
1292 : 4480407 : && rhs_code == BIT_FIELD_REF)
1293 : : {
1294 : 5857 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1295 : 5857 : if (!is_a <bb_vec_info> (vinfo)
1296 : 5811 : || TREE_CODE (vec) != SSA_NAME
1297 : : /* When the element types are not compatible we pun the
1298 : : source to the target vectype which requires equal size. */
1299 : 11656 : || ((!VECTOR_TYPE_P (TREE_TYPE (vec))
1300 : 4923 : || !types_compatible_p (TREE_TYPE (vectype),
1301 : 4923 : TREE_TYPE (TREE_TYPE (vec))))
1302 : 1108 : && !operand_equal_p (TYPE_SIZE (vectype),
1303 : 1108 : TYPE_SIZE (TREE_TYPE (vec)))))
1304 : : {
1305 : 818 : if (dump_enabled_p ())
1306 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1307 : : "Build SLP failed: "
1308 : : "BIT_FIELD_REF not supported\n");
1309 : : /* Fatal mismatch. */
1310 : 818 : matches[0] = false;
1311 : 818 : return false;
1312 : : }
1313 : : }
1314 : 4474550 : else if (rhs_code == CFN_DIV_POW2)
1315 : : {
1316 : 0 : need_same_oprnds = true;
1317 : 0 : first_op1 = gimple_call_arg (call_stmt, 1);
1318 : : }
1319 : 4474550 : else if (rhs_code == CFN_GOMP_SIMD_LANE)
1320 : : {
1321 : 3148 : need_same_oprnds = true;
1322 : 3148 : first_op1 = gimple_call_arg (call_stmt, 1);
1323 : : }
1324 : : }
1325 : : else
1326 : : {
1327 : 11836717 : if (first_reduc_idx != STMT_VINFO_REDUC_IDX (stmt_info)
1328 : : /* For SLP reduction groups the index isn't necessarily
1329 : : uniform but only that of the first stmt matters. */
1330 : 11836032 : && !(first_reduc_idx != -1
1331 : 1268 : && STMT_VINFO_REDUC_IDX (stmt_info) != -1
1332 : 1268 : && REDUC_GROUP_FIRST_ELEMENT (stmt_info)))
1333 : : {
1334 : 685 : if (dump_enabled_p ())
1335 : : {
1336 : 48 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1337 : : "Build SLP failed: different reduc_idx "
1338 : : "%d instead of %d in %G",
1339 : : STMT_VINFO_REDUC_IDX (stmt_info),
1340 : : first_reduc_idx, stmt);
1341 : : }
1342 : : /* Mismatch. */
1343 : 685 : continue;
1344 : : }
1345 : 11835347 : if (!ldst_p
1346 : 9317034 : && first_stmt_code != rhs_code
1347 : 13355835 : && alt_stmt_code == ERROR_MARK)
1348 : : alt_stmt_code = rhs_code;
1349 : 13345224 : if ((!ldst_p
1350 : 9317034 : && first_stmt_code != rhs_code
1351 : 1520488 : && (first_stmt_code != IMAGPART_EXPR
1352 : 121 : || rhs_code != REALPART_EXPR)
1353 : 1520476 : && (first_stmt_code != REALPART_EXPR
1354 : 445 : || rhs_code != IMAGPART_EXPR)
1355 : : /* Handle mismatches in plus/minus by computing both
1356 : : and merging the results. */
1357 : 1520473 : && !((((first_stmt_code == PLUS_EXPR
1358 : 1410303 : || first_stmt_code == MINUS_EXPR)
1359 : 135109 : && (alt_stmt_code == PLUS_EXPR
1360 : 124946 : || alt_stmt_code == MINUS_EXPR))
1361 : 1501802 : || ((first_stmt_code == CFN_FMA
1362 : 1501800 : || first_stmt_code == CFN_FMS)
1363 : 2 : && (alt_stmt_code == CFN_FMA
1364 : 2 : || alt_stmt_code == CFN_FMS)))
1365 : 18673 : && rhs_code == alt_stmt_code)
1366 : 1502090 : && !(first_stmt_code.is_tree_code ()
1367 : 1390394 : && rhs_code.is_tree_code ()
1368 : 1272263 : && (TREE_CODE_CLASS (tree_code (first_stmt_code))
1369 : : == tcc_comparison)
1370 : 141851 : && (swap_tree_comparison (tree_code (first_stmt_code))
1371 : 141851 : == tree_code (rhs_code))))
1372 : : || (ldst_p
1373 : 5036626 : && (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1374 : 2518313 : != STMT_VINFO_GROUPED_ACCESS (first_stmt_info)))
1375 : : || (ldst_p
1376 : 2468469 : && (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1377 : 2468469 : != STMT_VINFO_GATHER_SCATTER_P (first_stmt_info)))
1378 : 10325577 : || first_stmt_ldst_p != ldst_p
1379 : 10325470 : || (ldst_p && first_stmt_ldst_masklen_p != ldst_masklen_p)
1380 : 22160817 : || first_stmt_phi_p != phi_p)
1381 : : {
1382 : 1509877 : if (dump_enabled_p ())
1383 : : {
1384 : 2889 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1385 : : "Build SLP failed: different operation "
1386 : : "in stmt %G", stmt);
1387 : 2889 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1388 : : "original stmt %G", first_stmt_info->stmt);
1389 : : }
1390 : : /* Mismatch. */
1391 : 1509877 : continue;
1392 : : }
1393 : :
1394 : 10327330 : if (!ldst_p
1395 : 7857095 : && first_stmt_code == BIT_FIELD_REF
1396 : 10330737 : && (TREE_OPERAND (gimple_assign_rhs1 (first_stmt_info->stmt), 0)
1397 : 5267 : != TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0)))
1398 : : {
1399 : 1860 : if (dump_enabled_p ())
1400 : 36 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1401 : : "Build SLP failed: different BIT_FIELD_REF "
1402 : : "arguments in %G", stmt);
1403 : : /* Mismatch. */
1404 : 1860 : continue;
1405 : : }
1406 : :
1407 : 10323610 : if (call_stmt
1408 : 26451 : && first_stmt_code != CFN_MASK_LOAD
1409 : 10350014 : && first_stmt_code != CFN_MASK_STORE)
1410 : : {
1411 : 26356 : if (!is_a <gcall *> (stmts[0]->stmt)
1412 : 26356 : || !compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
1413 : : call_stmt, true))
1414 : : {
1415 : 7707 : if (dump_enabled_p ())
1416 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1417 : : "Build SLP failed: different calls in %G",
1418 : : stmt);
1419 : : /* Mismatch. */
1420 : 7707 : continue;
1421 : : }
1422 : : }
1423 : :
1424 : 10105287 : if ((phi_p || gimple_could_trap_p (stmt_info->stmt))
1425 : 10998793 : && (gimple_bb (first_stmt_info->stmt)
1426 : 893506 : != gimple_bb (stmt_info->stmt)))
1427 : : {
1428 : 34957 : if (dump_enabled_p ())
1429 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1430 : : "Build SLP failed: different BB for PHI "
1431 : : "or possibly trapping operation in %G", stmt);
1432 : : /* Mismatch. */
1433 : 34957 : continue;
1434 : : }
1435 : :
1436 : 10280946 : if (need_same_oprnds)
1437 : : {
1438 : 50274 : tree other_op1 = gimple_arg (stmt, 1);
1439 : 50274 : if (!operand_equal_p (first_op1, other_op1, 0))
1440 : : {
1441 : 7522 : if (dump_enabled_p ())
1442 : 125 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1443 : : "Build SLP failed: different shift "
1444 : : "arguments in %G", stmt);
1445 : : /* Mismatch. */
1446 : 7522 : continue;
1447 : : }
1448 : : }
1449 : :
1450 : 10273941 : if (first_lhs
1451 : 10273424 : && lhs
1452 : 10273424 : && !types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (first_lhs)))
1453 : : {
1454 : 517 : if (dump_enabled_p ())
1455 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1456 : : "Build SLP failed: different vector type "
1457 : : "in %G", stmt);
1458 : : /* Mismatch. */
1459 : 517 : continue;
1460 : : }
1461 : : }
1462 : :
1463 : : /* Grouped store or load. */
1464 : 14856752 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1465 : : {
1466 : 3905667 : gcc_assert (ldst_p);
1467 : 3905667 : if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1468 : : {
1469 : : /* Store. */
1470 : 3235598 : gcc_assert (rhs_code == CFN_MASK_STORE
1471 : : || REFERENCE_CLASS_P (lhs)
1472 : : || DECL_P (lhs));
1473 : : }
1474 : : else
1475 : : {
1476 : : /* Load. */
1477 : 670069 : first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
1478 : 670069 : if (prev_first_load)
1479 : : {
1480 : : /* Check that there are no loads from different interleaving
1481 : : chains in the same node. */
1482 : 311787 : if (prev_first_load != first_load)
1483 : : {
1484 : 26078 : if (dump_enabled_p ())
1485 : 1822 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1486 : : vect_location,
1487 : : "Build SLP failed: different "
1488 : : "interleaving chains in one node %G",
1489 : : stmt);
1490 : : /* Mismatch. */
1491 : 26078 : continue;
1492 : : }
1493 : : }
1494 : : else
1495 : : prev_first_load = first_load;
1496 : : }
1497 : : }
1498 : : /* Non-grouped store or load. */
1499 : 10951085 : else if (ldst_p)
1500 : : {
1501 : 477775 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info))
1502 : 310305 : && rhs_code != CFN_GATHER_LOAD
1503 : : && rhs_code != CFN_MASK_GATHER_LOAD
1504 : : && rhs_code != CFN_MASK_LEN_GATHER_LOAD
1505 : : && rhs_code != CFN_SCATTER_STORE
1506 : : && rhs_code != CFN_MASK_SCATTER_STORE
1507 : : && rhs_code != CFN_MASK_LEN_SCATTER_STORE
1508 : 310305 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info)
1509 : : /* Not grouped loads are handled as externals for BB
1510 : : vectorization. For loop vectorization we can handle
1511 : : splats the same we handle single element interleaving. */
1512 : 779969 : && (is_a <bb_vec_info> (vinfo)
1513 : 302194 : || stmt_info != first_stmt_info))
1514 : : {
1515 : : /* Not grouped load. */
1516 : 11786 : if (dump_enabled_p ())
1517 : 108 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1518 : : "Build SLP failed: not grouped load %G", stmt);
1519 : :
1520 : 11786 : if (i != 0)
1521 : 11786 : continue;
1522 : : /* Fatal mismatch. */
1523 : 0 : matches[0] = false;
1524 : 0 : return false;
1525 : : }
1526 : : }
1527 : : /* Not memory operation. */
1528 : : else
1529 : : {
1530 : 10473310 : if (!phi_p
1531 : 9688806 : && rhs_code.is_tree_code ()
1532 : 9646259 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_binary
1533 : 1344245 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_unary
1534 : 804911 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_expression
1535 : 754387 : && TREE_CODE_CLASS (tree_code (rhs_code)) != tcc_comparison
1536 : 72244 : && rhs_code != VIEW_CONVERT_EXPR
1537 : : && rhs_code != CALL_EXPR
1538 : : && rhs_code != BIT_FIELD_REF
1539 : 10473310 : && rhs_code != SSA_NAME)
1540 : : {
1541 : 20099 : if (dump_enabled_p ())
1542 : 9 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1543 : : "Build SLP failed: operation unsupported %G",
1544 : : stmt);
1545 : 20099 : if (is_a <bb_vec_info> (vinfo) && i != 0)
1546 : 0 : continue;
1547 : : /* Fatal mismatch. */
1548 : 20099 : matches[0] = false;
1549 : 20099 : return false;
1550 : : }
1551 : :
1552 : 10453211 : if (rhs_code == COND_EXPR)
1553 : : {
1554 : 47858 : tree cond_expr = gimple_assign_rhs1 (stmt);
1555 : 47858 : enum tree_code cond_code = TREE_CODE (cond_expr);
1556 : 47858 : enum tree_code swap_code = ERROR_MARK;
1557 : 47858 : enum tree_code invert_code = ERROR_MARK;
1558 : :
1559 : 47858 : if (i == 0)
1560 : 39531 : first_cond_code = TREE_CODE (cond_expr);
1561 : 8327 : else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1562 : : {
1563 : 0 : bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
1564 : 0 : swap_code = swap_tree_comparison (cond_code);
1565 : 0 : invert_code = invert_tree_comparison (cond_code, honor_nans);
1566 : : }
1567 : :
1568 : 47858 : if (first_cond_code == cond_code)
1569 : : ;
1570 : : /* Isomorphic can be achieved by swapping. */
1571 : 0 : else if (first_cond_code == swap_code)
1572 : 0 : swap[i] = 1;
1573 : : /* Isomorphic can be achieved by inverting. */
1574 : 0 : else if (first_cond_code == invert_code)
1575 : 0 : swap[i] = 2;
1576 : : else
1577 : : {
1578 : 0 : if (dump_enabled_p ())
1579 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1580 : : "Build SLP failed: different"
1581 : : " operation %G", stmt);
1582 : : /* Mismatch. */
1583 : 0 : continue;
1584 : : }
1585 : : }
1586 : :
1587 : 10453211 : if (rhs_code.is_tree_code ()
1588 : 9626160 : && TREE_CODE_CLASS ((tree_code)rhs_code) == tcc_comparison
1589 : 11135354 : && (swap_tree_comparison ((tree_code)first_stmt_code)
1590 : 682143 : == (tree_code)rhs_code))
1591 : 460052 : swap[i] = 1;
1592 : : }
1593 : :
1594 : 14798789 : matches[i] = true;
1595 : : }
1596 : :
1597 : 19369207 : for (i = 0; i < group_size; ++i)
1598 : 15621901 : if (!matches[i])
1599 : : return false;
1600 : :
1601 : : /* If we allowed a two-operation SLP node verify the target can cope
1602 : : with the permute we are going to use. */
1603 : 3747306 : if (alt_stmt_code != ERROR_MARK
1604 : 3747306 : && (!alt_stmt_code.is_tree_code ()
1605 : 53144 : || (TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_reference
1606 : 53144 : && TREE_CODE_CLASS (tree_code (alt_stmt_code)) != tcc_comparison)))
1607 : : {
1608 : 11770 : *two_operators = true;
1609 : : }
1610 : :
1611 : 3747306 : if (maybe_soft_fail)
1612 : : {
1613 : 139698 : unsigned HOST_WIDE_INT const_nunits;
1614 : 139698 : if (!TYPE_VECTOR_SUBPARTS
1615 : 139698 : (soft_fail_nunits_vectype).is_constant (&const_nunits)
1616 : 139698 : || const_nunits > group_size)
1617 : 0 : matches[0] = false;
1618 : : else
1619 : : {
1620 : : /* With constant vector elements simulate a mismatch at the
1621 : : point we need to split. */
1622 : 139698 : unsigned tail = group_size & (const_nunits - 1);
1623 : 139698 : memset (&matches[group_size - tail], 0, sizeof (bool) * tail);
1624 : : }
1625 : 139698 : return false;
1626 : : }
1627 : :
1628 : : return true;
1629 : : }
1630 : :
1631 : : /* Traits for the hash_set to record failed SLP builds for a stmt set.
1632 : : Note we never remove apart from at destruction time so we do not
1633 : : need a special value for deleted that differs from empty. */
1634 : : struct bst_traits
1635 : : {
1636 : : typedef vec <stmt_vec_info> value_type;
1637 : : typedef vec <stmt_vec_info> compare_type;
1638 : : static inline hashval_t hash (value_type);
1639 : : static inline bool equal (value_type existing, value_type candidate);
1640 : 427738783 : static inline bool is_empty (value_type x) { return !x.exists (); }
1641 : 98650269 : static inline bool is_deleted (value_type x) { return !x.exists (); }
1642 : : static const bool empty_zero_p = true;
1643 : 0 : static inline void mark_empty (value_type &x) { x.release (); }
1644 : : static inline void mark_deleted (value_type &x) { x.release (); }
1645 : 8078830 : static inline void remove (value_type &x) { x.release (); }
1646 : : };
1647 : : inline hashval_t
1648 : 85423475 : bst_traits::hash (value_type x)
1649 : : {
1650 : 85423475 : inchash::hash h;
1651 : 396922418 : for (unsigned i = 0; i < x.length (); ++i)
1652 : 311498943 : h.add_int (x[i] ? gimple_uid (x[i]->stmt) : -1);
1653 : 85423475 : return h.end ();
1654 : : }
1655 : : inline bool
1656 : 75960821 : bst_traits::equal (value_type existing, value_type candidate)
1657 : : {
1658 : 227882463 : if (existing.length () != candidate.length ())
1659 : : return false;
1660 : 79253935 : for (unsigned i = 0; i < existing.length (); ++i)
1661 : 75221564 : if (existing[i] != candidate[i])
1662 : : return false;
1663 : : return true;
1664 : : }
1665 : :
1666 : : typedef hash_map <vec <stmt_vec_info>, slp_tree,
1667 : : simple_hashmap_traits <bst_traits, slp_tree> >
1668 : : scalar_stmts_to_slp_tree_map_t;
1669 : :
1670 : : /* Release BST_MAP. */
1671 : :
1672 : : static void
1673 : 1529693 : release_scalar_stmts_to_slp_tree_map (scalar_stmts_to_slp_tree_map_t *bst_map)
1674 : : {
1675 : : /* The map keeps a reference on SLP nodes built, release that. */
1676 : 9608523 : for (scalar_stmts_to_slp_tree_map_t::iterator it = bst_map->begin ();
1677 : 17687353 : it != bst_map->end (); ++it)
1678 : 8078830 : if ((*it).second)
1679 : 8078830 : vect_free_slp_tree ((*it).second);
1680 : 1529693 : delete bst_map;
1681 : 1529693 : }
1682 : :
1683 : : /* ??? This was std::pair<std::pair<tree_code, vect_def_type>, tree>
1684 : : but then vec::insert does memmove and that's not compatible with
1685 : : std::pair. */
1686 : : struct chain_op_t
1687 : : {
1688 : 3540609 : chain_op_t (tree_code code_, vect_def_type dt_, tree op_)
1689 : 3540609 : : code (code_), dt (dt_), op (op_) {}
1690 : : tree_code code;
1691 : : vect_def_type dt;
1692 : : tree op;
1693 : : };
1694 : :
1695 : : /* Comparator for sorting associatable chains. */
1696 : :
1697 : : static int
1698 : 8424022 : dt_sort_cmp (const void *op1_, const void *op2_, void *)
1699 : : {
1700 : 8424022 : auto *op1 = (const chain_op_t *) op1_;
1701 : 8424022 : auto *op2 = (const chain_op_t *) op2_;
1702 : 8424022 : if (op1->dt != op2->dt)
1703 : 1020529 : return (int)op1->dt - (int)op2->dt;
1704 : 7403493 : return (int)op1->code - (int)op2->code;
1705 : : }
1706 : :
1707 : : /* Linearize the associatable expression chain at START with the
1708 : : associatable operation CODE (where PLUS_EXPR also allows MINUS_EXPR),
1709 : : filling CHAIN with the result and using WORKLIST as intermediate storage.
1710 : : CODE_STMT and ALT_CODE_STMT are filled with the first stmt using CODE
1711 : : or MINUS_EXPR. *CHAIN_STMTS if not NULL is filled with all computation
1712 : : stmts, starting with START. */
1713 : :
1714 : : static void
1715 : 1582812 : vect_slp_linearize_chain (vec_info *vinfo,
1716 : : vec<std::pair<tree_code, gimple *> > &worklist,
1717 : : vec<chain_op_t> &chain,
1718 : : enum tree_code code, gimple *start,
1719 : : gimple *&code_stmt, gimple *&alt_code_stmt,
1720 : : vec<gimple *> *chain_stmts)
1721 : : {
1722 : : /* For each lane linearize the addition/subtraction (or other
1723 : : uniform associatable operation) expression tree. */
1724 : 1582812 : worklist.safe_push (std::make_pair (code, start));
1725 : 3540609 : while (!worklist.is_empty ())
1726 : : {
1727 : 1957797 : auto entry = worklist.pop ();
1728 : 1957797 : gassign *stmt = as_a <gassign *> (entry.second);
1729 : 1957797 : enum tree_code in_code = entry.first;
1730 : 3915594 : enum tree_code this_code = gimple_assign_rhs_code (stmt);
1731 : : /* Pick some stmts suitable for SLP_TREE_REPRESENTATIVE. */
1732 : 1957797 : if (!code_stmt
1733 : 1957797 : && gimple_assign_rhs_code (stmt) == code)
1734 : 1324796 : code_stmt = stmt;
1735 : 633001 : else if (!alt_code_stmt
1736 : 633001 : && gimple_assign_rhs_code (stmt) == MINUS_EXPR)
1737 : 331939 : alt_code_stmt = stmt;
1738 : 1957797 : if (chain_stmts)
1739 : 1952801 : chain_stmts->safe_push (stmt);
1740 : 5873391 : for (unsigned opnum = 1; opnum <= 2; ++opnum)
1741 : : {
1742 : 3915594 : tree op = gimple_op (stmt, opnum);
1743 : 3915594 : vect_def_type dt;
1744 : 3915594 : stmt_vec_info def_stmt_info;
1745 : 3915594 : bool res = vect_is_simple_use (op, vinfo, &dt, &def_stmt_info);
1746 : 3915594 : gcc_assert (res);
1747 : 3915594 : if (dt == vect_internal_def
1748 : 3915594 : && is_pattern_stmt_p (def_stmt_info))
1749 : 808 : op = gimple_get_lhs (def_stmt_info->stmt);
1750 : 3915594 : gimple *use_stmt;
1751 : 3915594 : use_operand_p use_p;
1752 : 3915594 : if (dt == vect_internal_def
1753 : 3659659 : && single_imm_use (op, &use_p, &use_stmt)
1754 : 2297313 : && is_gimple_assign (def_stmt_info->stmt)
1755 : 6031725 : && (gimple_assign_rhs_code (def_stmt_info->stmt) == code
1756 : 1766557 : || (code == PLUS_EXPR
1757 : 852728 : && (gimple_assign_rhs_code (def_stmt_info->stmt)
1758 : : == MINUS_EXPR))))
1759 : : {
1760 : 374985 : tree_code op_def_code = this_code;
1761 : 374985 : if (op_def_code == MINUS_EXPR && opnum == 1)
1762 : 54345 : op_def_code = PLUS_EXPR;
1763 : 374985 : if (in_code == MINUS_EXPR)
1764 : 193 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1765 : 374985 : worklist.safe_push (std::make_pair (op_def_code,
1766 : 374985 : def_stmt_info->stmt));
1767 : : }
1768 : : else
1769 : : {
1770 : 3540609 : tree_code op_def_code = this_code;
1771 : 3540609 : if (op_def_code == MINUS_EXPR && opnum == 1)
1772 : 281614 : op_def_code = PLUS_EXPR;
1773 : 3540609 : if (in_code == MINUS_EXPR)
1774 : 4411 : op_def_code = op_def_code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
1775 : 3540609 : chain.safe_push (chain_op_t (op_def_code, dt, op));
1776 : : }
1777 : : }
1778 : : }
1779 : 1582812 : }
1780 : :
1781 : : static slp_tree
1782 : : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1783 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1784 : : poly_uint64 *max_nunits,
1785 : : bool *matches, unsigned *limit, unsigned *tree_size,
1786 : : scalar_stmts_to_slp_tree_map_t *bst_map);
1787 : :
1788 : : static slp_tree
1789 : 5342026 : vect_build_slp_tree (vec_info *vinfo,
1790 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1791 : : poly_uint64 *max_nunits,
1792 : : bool *matches, unsigned *limit, unsigned *tree_size,
1793 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1794 : : {
1795 : 5342026 : if (slp_tree *leader = bst_map->get (stmts))
1796 : : {
1797 : 380366 : if (dump_enabled_p ())
1798 : 15405 : dump_printf_loc (MSG_NOTE, vect_location, "re-using %sSLP tree %p\n",
1799 : 15405 : !(*leader)->failed ? "" : "failed ",
1800 : : (void *) *leader);
1801 : 380366 : if (!(*leader)->failed)
1802 : : {
1803 : 331303 : SLP_TREE_REF_COUNT (*leader)++;
1804 : 331303 : vect_update_max_nunits (max_nunits, (*leader)->max_nunits);
1805 : 331303 : stmts.release ();
1806 : 331303 : return *leader;
1807 : : }
1808 : 49063 : memcpy (matches, (*leader)->failed, sizeof (bool) * group_size);
1809 : 49063 : return NULL;
1810 : : }
1811 : :
1812 : : /* Single-lane SLP doesn't have the chance of run-away, do not account
1813 : : it to the limit. */
1814 : 4961660 : if (stmts.length () > 1)
1815 : : {
1816 : 3126232 : if (*limit == 0)
1817 : : {
1818 : 1556 : if (dump_enabled_p ())
1819 : 46 : dump_printf_loc (MSG_NOTE, vect_location,
1820 : : "SLP discovery limit exceeded\n");
1821 : 1556 : memset (matches, 0, sizeof (bool) * group_size);
1822 : 1556 : return NULL;
1823 : : }
1824 : 3124676 : --*limit;
1825 : : }
1826 : :
1827 : : /* Seed the bst_map with a stub node to be filled by vect_build_slp_tree_2
1828 : : so we can pick up backedge destinations during discovery. */
1829 : 4960104 : slp_tree res = new _slp_tree;
1830 : 4960104 : SLP_TREE_DEF_TYPE (res) = vect_internal_def;
1831 : 4960104 : SLP_TREE_SCALAR_STMTS (res) = stmts;
1832 : 4960104 : bst_map->put (stmts.copy (), res);
1833 : :
1834 : 4960104 : if (dump_enabled_p ())
1835 : 129731 : dump_printf_loc (MSG_NOTE, vect_location,
1836 : : "starting SLP discovery for node %p\n", (void *) res);
1837 : :
1838 : 4960104 : poly_uint64 this_max_nunits = 1;
1839 : 4960104 : slp_tree res_ = vect_build_slp_tree_2 (vinfo, res, stmts, group_size,
1840 : : &this_max_nunits,
1841 : : matches, limit, tree_size, bst_map);
1842 : 4960104 : if (!res_)
1843 : : {
1844 : 1855256 : if (dump_enabled_p ())
1845 : 6613 : dump_printf_loc (MSG_NOTE, vect_location,
1846 : : "SLP discovery for node %p failed\n", (void *) res);
1847 : : /* Mark the node invalid so we can detect those when still in use
1848 : : as backedge destinations. */
1849 : 1855256 : SLP_TREE_SCALAR_STMTS (res) = vNULL;
1850 : 1855256 : SLP_TREE_DEF_TYPE (res) = vect_uninitialized_def;
1851 : 1855256 : res->failed = XNEWVEC (bool, group_size);
1852 : 1855256 : if (flag_checking)
1853 : : {
1854 : : unsigned i;
1855 : 3586500 : for (i = 0; i < group_size; ++i)
1856 : 3586500 : if (!matches[i])
1857 : : break;
1858 : 1855256 : gcc_assert (i < group_size);
1859 : : }
1860 : 1855256 : memcpy (res->failed, matches, sizeof (bool) * group_size);
1861 : : }
1862 : : else
1863 : : {
1864 : 3104848 : if (dump_enabled_p ())
1865 : 123118 : dump_printf_loc (MSG_NOTE, vect_location,
1866 : : "SLP discovery for node %p succeeded\n",
1867 : : (void *) res);
1868 : 3104848 : gcc_assert (res_ == res);
1869 : 3104848 : res->max_nunits = this_max_nunits;
1870 : 3104848 : vect_update_max_nunits (max_nunits, this_max_nunits);
1871 : : /* Keep a reference for the bst_map use. */
1872 : 3104848 : SLP_TREE_REF_COUNT (res)++;
1873 : : }
1874 : : return res_;
1875 : : }
1876 : :
1877 : : /* Helper for building an associated SLP node chain. */
1878 : :
1879 : : static void
1880 : 129 : vect_slp_build_two_operator_nodes (slp_tree perm, tree vectype,
1881 : : slp_tree op0, slp_tree op1,
1882 : : stmt_vec_info oper1, stmt_vec_info oper2,
1883 : : vec<std::pair<unsigned, unsigned> > lperm)
1884 : : {
1885 : 129 : unsigned group_size = SLP_TREE_LANES (op1);
1886 : :
1887 : 129 : slp_tree child1 = new _slp_tree;
1888 : 129 : SLP_TREE_DEF_TYPE (child1) = vect_internal_def;
1889 : 129 : SLP_TREE_VECTYPE (child1) = vectype;
1890 : 129 : SLP_TREE_LANES (child1) = group_size;
1891 : 129 : SLP_TREE_CHILDREN (child1).create (2);
1892 : 129 : SLP_TREE_CHILDREN (child1).quick_push (op0);
1893 : 129 : SLP_TREE_CHILDREN (child1).quick_push (op1);
1894 : 129 : SLP_TREE_REPRESENTATIVE (child1) = oper1;
1895 : :
1896 : 129 : slp_tree child2 = new _slp_tree;
1897 : 129 : SLP_TREE_DEF_TYPE (child2) = vect_internal_def;
1898 : 129 : SLP_TREE_VECTYPE (child2) = vectype;
1899 : 129 : SLP_TREE_LANES (child2) = group_size;
1900 : 129 : SLP_TREE_CHILDREN (child2).create (2);
1901 : 129 : SLP_TREE_CHILDREN (child2).quick_push (op0);
1902 : 129 : SLP_TREE_REF_COUNT (op0)++;
1903 : 129 : SLP_TREE_CHILDREN (child2).quick_push (op1);
1904 : 129 : SLP_TREE_REF_COUNT (op1)++;
1905 : 129 : SLP_TREE_REPRESENTATIVE (child2) = oper2;
1906 : :
1907 : 129 : SLP_TREE_DEF_TYPE (perm) = vect_internal_def;
1908 : 129 : SLP_TREE_CODE (perm) = VEC_PERM_EXPR;
1909 : 129 : SLP_TREE_VECTYPE (perm) = vectype;
1910 : 129 : SLP_TREE_LANES (perm) = group_size;
1911 : : /* ??? We should set this NULL but that's not expected. */
1912 : 129 : SLP_TREE_REPRESENTATIVE (perm) = oper1;
1913 : 129 : SLP_TREE_LANE_PERMUTATION (perm) = lperm;
1914 : 129 : SLP_TREE_CHILDREN (perm).quick_push (child1);
1915 : 129 : SLP_TREE_CHILDREN (perm).quick_push (child2);
1916 : 129 : }
1917 : :
1918 : : /* Recursively build an SLP tree starting from NODE.
1919 : : Fail (and return a value not equal to zero) if def-stmts are not
1920 : : isomorphic, require data permutation or are of unsupported types of
1921 : : operation. Otherwise, return 0.
1922 : : The value returned is the depth in the SLP tree where a mismatch
1923 : : was found. */
1924 : :
1925 : : static slp_tree
1926 : 4960104 : vect_build_slp_tree_2 (vec_info *vinfo, slp_tree node,
1927 : : vec<stmt_vec_info> stmts, unsigned int group_size,
1928 : : poly_uint64 *max_nunits,
1929 : : bool *matches, unsigned *limit, unsigned *tree_size,
1930 : : scalar_stmts_to_slp_tree_map_t *bst_map)
1931 : : {
1932 : 4960104 : unsigned nops, i, this_tree_size = 0;
1933 : 4960104 : poly_uint64 this_max_nunits = *max_nunits;
1934 : :
1935 : 4960104 : matches[0] = false;
1936 : :
1937 : 4960104 : stmt_vec_info stmt_info = stmts[0];
1938 : 4960104 : if (!is_a<gcall *> (stmt_info->stmt)
1939 : : && !is_a<gassign *> (stmt_info->stmt)
1940 : : && !is_a<gphi *> (stmt_info->stmt))
1941 : : return NULL;
1942 : :
1943 : 4959995 : nops = gimple_num_args (stmt_info->stmt);
1944 : 4959995 : if (const int *map = vect_get_operand_map (stmt_info->stmt,
1945 : 4959995 : STMT_VINFO_GATHER_SCATTER_P
1946 : : (stmt_info)))
1947 : 17745 : nops = map[0];
1948 : :
1949 : : /* If the SLP node is a PHI (induction or reduction), terminate
1950 : : the recursion. */
1951 : 4959995 : bool *skip_args = XALLOCAVEC (bool, nops);
1952 : 4959995 : memset (skip_args, 0, sizeof (bool) * nops);
1953 : 4959995 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
1954 : 1908005 : if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
1955 : : {
1956 : 316877 : tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
1957 : 316877 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
1958 : : group_size);
1959 : 316877 : if (!vect_record_max_nunits (vinfo, stmt_info, group_size, vectype,
1960 : : max_nunits))
1961 : : return NULL;
1962 : :
1963 : 316877 : vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
1964 : 316877 : if (def_type == vect_induction_def)
1965 : : {
1966 : : /* Induction PHIs are not cycles but walk the initial
1967 : : value. Only for inner loops through, for outer loops
1968 : : we need to pick up the value from the actual PHIs
1969 : : to more easily support peeling and epilogue vectorization. */
1970 : 258465 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1971 : 258465 : if (!nested_in_vect_loop_p (loop, stmt_info))
1972 : 257929 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
1973 : : else
1974 : : loop = loop->inner;
1975 : 258465 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
1976 : : }
1977 : 58412 : else if (def_type == vect_reduction_def
1978 : : || def_type == vect_double_reduction_def
1979 : : || def_type == vect_nested_cycle
1980 : 58412 : || def_type == vect_first_order_recurrence)
1981 : : {
1982 : : /* Else def types have to match. */
1983 : : stmt_vec_info other_info;
1984 : : bool all_same = true;
1985 : 116855 : FOR_EACH_VEC_ELT (stmts, i, other_info)
1986 : : {
1987 : 59354 : if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
1988 : 1599886 : return NULL;
1989 : 59354 : if (other_info != stmt_info)
1990 : 612 : all_same = false;
1991 : : }
1992 : 57501 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1993 : : /* Reduction initial values are not explicitely represented. */
1994 : 57501 : if (def_type != vect_first_order_recurrence
1995 : 57501 : && gimple_bb (stmt_info->stmt) == loop->header)
1996 : 55364 : skip_args[loop_preheader_edge (loop)->dest_idx] = true;
1997 : : /* Reduction chain backedge defs are filled manually.
1998 : : ??? Need a better way to identify a SLP reduction chain PHI.
1999 : : Or a better overall way to SLP match those. */
2000 : 57501 : if (stmts.length () > 1
2001 : 57501 : && all_same && def_type == vect_reduction_def)
2002 : 291 : skip_args[loop_latch_edge (loop)->dest_idx] = true;
2003 : : }
2004 : 911 : else if (def_type != vect_internal_def)
2005 : : return NULL;
2006 : : }
2007 : :
2008 : :
2009 : 4959985 : bool two_operators = false;
2010 : 4959985 : unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
2011 : 4959985 : tree vectype = NULL_TREE;
2012 : 4959985 : if (!vect_build_slp_tree_1 (vinfo, swap, stmts, group_size,
2013 : : &this_max_nunits, matches, &two_operators,
2014 : : &vectype))
2015 : : return NULL;
2016 : :
2017 : : /* If the SLP node is a load, terminate the recursion unless masked. */
2018 : 3607608 : if (STMT_VINFO_DATA_REF (stmt_info)
2019 : 1700042 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2020 : : {
2021 : 573872 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2022 : : gcc_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
2023 : : else
2024 : : {
2025 : 565928 : *max_nunits = this_max_nunits;
2026 : 565928 : (*tree_size)++;
2027 : 565928 : node = vect_create_new_slp_node (node, stmts, 0);
2028 : 565928 : SLP_TREE_VECTYPE (node) = vectype;
2029 : : /* And compute the load permutation. Whether it is actually
2030 : : a permutation depends on the unrolling factor which is
2031 : : decided later. */
2032 : 565928 : vec<unsigned> load_permutation;
2033 : 565928 : int j;
2034 : 565928 : stmt_vec_info load_info;
2035 : 565928 : load_permutation.create (group_size);
2036 : 565928 : stmt_vec_info first_stmt_info
2037 : 565928 : = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2038 : 565928 : ? DR_GROUP_FIRST_ELEMENT (stmt_info) : stmt_info;
2039 : 565928 : bool any_permute = false;
2040 : 1444675 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
2041 : : {
2042 : 878747 : int load_place;
2043 : 878747 : if (! load_info)
2044 : : {
2045 : 33509 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2046 : : load_place = j;
2047 : : else
2048 : : load_place = 0;
2049 : : }
2050 : 845238 : else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2051 : 558887 : load_place = vect_get_place_in_interleaving_chain
2052 : 558887 : (load_info, first_stmt_info);
2053 : : else
2054 : : load_place = 0;
2055 : 592396 : gcc_assert (load_place != -1);
2056 : 878747 : any_permute |= load_place != j;
2057 : 878747 : load_permutation.quick_push (load_place);
2058 : : }
2059 : :
2060 : 565928 : if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
2061 : : {
2062 : 1980 : gcc_assert (gimple_call_internal_p (stmt, IFN_MASK_LOAD));
2063 : 1980 : bool has_gaps = false;
2064 : 1980 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2065 : 99 : for (stmt_vec_info si = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
2066 : 168 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2067 : 69 : if (DR_GROUP_GAP (si) != 1)
2068 : 12 : has_gaps = true;
2069 : : /* We cannot handle permuted masked loads directly, see
2070 : : PR114375. We cannot handle strided masked loads or masked
2071 : : loads with gaps unless the mask is uniform. */
2072 : 1980 : if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
2073 : 99 : && (DR_GROUP_GAP (first_stmt_info) != 0
2074 : 51 : || (has_gaps
2075 : 12 : && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))))
2076 : 3900 : || STMT_VINFO_STRIDED_P (stmt_info))
2077 : : {
2078 : 70 : load_permutation.release ();
2079 : 70 : matches[0] = false;
2080 : 564036 : return NULL;
2081 : : }
2082 : :
2083 : : /* For permuted masked loads do an unpermuted masked load of
2084 : : the whole group followed by a SLP permute node. */
2085 : 1910 : if (any_permute
2086 : 1910 : || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
2087 : 25 : && DR_GROUP_SIZE (first_stmt_info) != group_size))
2088 : : {
2089 : : /* Discover the whole unpermuted load. */
2090 : 18 : vec<stmt_vec_info> stmts2;
2091 : 18 : unsigned dr_group_size = STMT_VINFO_GROUPED_ACCESS (stmt_info)
2092 : 32 : ? DR_GROUP_SIZE (first_stmt_info) : 1;
2093 : 18 : stmts2.create (dr_group_size);
2094 : 18 : stmts2.quick_grow_cleared (dr_group_size);
2095 : 18 : unsigned i = 0;
2096 : 18 : for (stmt_vec_info si = first_stmt_info;
2097 : 62 : si; si = DR_GROUP_NEXT_ELEMENT (si))
2098 : : {
2099 : 44 : if (si != first_stmt_info)
2100 : 26 : for (unsigned k = 1; k < DR_GROUP_GAP (si); ++k)
2101 : 0 : stmts2[i++] = NULL;
2102 : 44 : stmts2[i++] = si;
2103 : : }
2104 : 18 : bool *matches2 = XALLOCAVEC (bool, dr_group_size);
2105 : 18 : slp_tree unperm_load
2106 : 18 : = vect_build_slp_tree (vinfo, stmts2, dr_group_size,
2107 : : &this_max_nunits, matches2, limit,
2108 : 18 : &this_tree_size, bst_map);
2109 : : /* When we are able to do the full masked load emit that
2110 : : followed by 'node' being the desired final permutation. */
2111 : 18 : if (unperm_load)
2112 : : {
2113 : 10 : gcc_assert
2114 : : (!SLP_TREE_LOAD_PERMUTATION (unperm_load).exists ());
2115 : 10 : lane_permutation_t lperm;
2116 : 10 : lperm.create (group_size);
2117 : 30 : for (unsigned j = 0; j < load_permutation.length (); ++j)
2118 : 20 : lperm.quick_push
2119 : 20 : (std::make_pair (0, load_permutation[j]));
2120 : 10 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2121 : 10 : SLP_TREE_CHILDREN (node).safe_push (unperm_load);
2122 : 10 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2123 : 10 : load_permutation.release ();
2124 : 10 : return node;
2125 : : }
2126 : 8 : stmts2.release ();
2127 : 8 : load_permutation.release ();
2128 : 8 : matches[0] = false;
2129 : 8 : return NULL;
2130 : : }
2131 : 1892 : load_permutation.release ();
2132 : : }
2133 : : else
2134 : : {
2135 : 563948 : if (!any_permute
2136 : 485426 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2137 : 768958 : && group_size == DR_GROUP_SIZE (first_stmt_info))
2138 : 118470 : load_permutation.release ();
2139 : 563948 : SLP_TREE_LOAD_PERMUTATION (node) = load_permutation;
2140 : 563948 : return node;
2141 : : }
2142 : : }
2143 : : }
2144 : 3033736 : else if (gimple_assign_single_p (stmt_info->stmt)
2145 : 2258892 : && !gimple_vuse (stmt_info->stmt)
2146 : 3041560 : && gimple_assign_rhs_code (stmt_info->stmt) == BIT_FIELD_REF)
2147 : : {
2148 : : /* vect_build_slp_tree_2 determined all BIT_FIELD_REFs reference
2149 : : the same SSA name vector of a compatible type to vectype. */
2150 : 2414 : vec<std::pair<unsigned, unsigned> > lperm = vNULL;
2151 : 2414 : tree vec = TREE_OPERAND (gimple_assign_rhs1 (stmt_info->stmt), 0);
2152 : 2414 : stmt_vec_info estmt_info;
2153 : 7894 : FOR_EACH_VEC_ELT (stmts, i, estmt_info)
2154 : : {
2155 : 5483 : gassign *estmt = as_a <gassign *> (estmt_info->stmt);
2156 : 5483 : tree bfref = gimple_assign_rhs1 (estmt);
2157 : 5483 : HOST_WIDE_INT lane;
2158 : 5483 : if (!known_eq (bit_field_size (bfref),
2159 : : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (vectype))))
2160 : 10963 : || !constant_multiple_p (bit_field_offset (bfref),
2161 : 5480 : bit_field_size (bfref), &lane))
2162 : : {
2163 : 3 : lperm.release ();
2164 : 3 : matches[0] = false;
2165 : 3 : return NULL;
2166 : : }
2167 : 5480 : lperm.safe_push (std::make_pair (0, (unsigned)lane));
2168 : : }
2169 : 2411 : slp_tree vnode = vect_create_new_slp_node (vNULL);
2170 : 2411 : if (operand_equal_p (TYPE_SIZE (vectype), TYPE_SIZE (TREE_TYPE (vec))))
2171 : : /* ??? We record vectype here but we hide eventually necessary
2172 : : punning and instead rely on code generation to materialize
2173 : : VIEW_CONVERT_EXPRs as necessary. We instead should make
2174 : : this explicit somehow. */
2175 : 668 : SLP_TREE_VECTYPE (vnode) = vectype;
2176 : : else
2177 : : {
2178 : : /* For different size but compatible elements we can still
2179 : : use VEC_PERM_EXPR without punning. */
2180 : 1743 : gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec))
2181 : : && types_compatible_p (TREE_TYPE (vectype),
2182 : : TREE_TYPE (TREE_TYPE (vec))));
2183 : 1743 : SLP_TREE_VECTYPE (vnode) = TREE_TYPE (vec);
2184 : : }
2185 : 2411 : auto nunits = TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (vnode));
2186 : 2411 : unsigned HOST_WIDE_INT const_nunits;
2187 : 2411 : if (nunits.is_constant (&const_nunits))
2188 : 2411 : SLP_TREE_LANES (vnode) = const_nunits;
2189 : 2411 : SLP_TREE_VEC_DEFS (vnode).safe_push (vec);
2190 : : /* We are always building a permutation node even if it is an identity
2191 : : permute to shield the rest of the vectorizer from the odd node
2192 : : representing an actual vector without any scalar ops.
2193 : : ??? We could hide it completely with making the permute node
2194 : : external? */
2195 : 2411 : node = vect_create_new_slp_node (node, stmts, 1);
2196 : 2411 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
2197 : 2411 : SLP_TREE_LANE_PERMUTATION (node) = lperm;
2198 : 2411 : SLP_TREE_VECTYPE (node) = vectype;
2199 : 2411 : SLP_TREE_CHILDREN (node).quick_push (vnode);
2200 : 2411 : return node;
2201 : : }
2202 : : /* When discovery reaches an associatable operation see whether we can
2203 : : improve that to match up lanes in a way superior to the operand
2204 : : swapping code which at most looks at two defs.
2205 : : ??? For BB vectorization we cannot do the brute-force search
2206 : : for matching as we can succeed by means of builds from scalars
2207 : : and have no good way to "cost" one build against another. */
2208 : 3031322 : else if (is_a <loop_vec_info> (vinfo)
2209 : : /* Do not bother for single-lane SLP. */
2210 : 1471262 : && group_size > 1
2211 : : /* ??? We don't handle !vect_internal_def defs below. */
2212 : 38645 : && STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
2213 : : /* ??? Do not associate a reduction, this will wreck REDUC_IDX
2214 : : mapping as long as that exists on the stmt_info level. */
2215 : 34926 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1
2216 : 34630 : && is_gimple_assign (stmt_info->stmt)
2217 : 34433 : && (associative_tree_code (gimple_assign_rhs_code (stmt_info->stmt))
2218 : 28206 : || gimple_assign_rhs_code (stmt_info->stmt) == MINUS_EXPR)
2219 : 3038962 : && ((FLOAT_TYPE_P (vectype) && flag_associative_math)
2220 : 6650 : || (INTEGRAL_TYPE_P (TREE_TYPE (vectype))
2221 : 4848 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (vectype)))))
2222 : : {
2223 : : /* See if we have a chain of (mixed) adds or subtracts or other
2224 : : associatable ops. */
2225 : 3586 : enum tree_code code = gimple_assign_rhs_code (stmt_info->stmt);
2226 : 3586 : if (code == MINUS_EXPR)
2227 : 596 : code = PLUS_EXPR;
2228 : 3586 : stmt_vec_info other_op_stmt_info = NULL;
2229 : 3586 : stmt_vec_info op_stmt_info = NULL;
2230 : 3586 : unsigned chain_len = 0;
2231 : 3586 : auto_vec<chain_op_t> chain;
2232 : 3586 : auto_vec<std::pair<tree_code, gimple *> > worklist;
2233 : 3586 : auto_vec<vec<chain_op_t> > chains (group_size);
2234 : 3586 : auto_vec<slp_tree, 4> children;
2235 : 3586 : bool hard_fail = true;
2236 : 4286 : for (unsigned lane = 0; lane < group_size; ++lane)
2237 : : {
2238 : 4044 : if (!stmts[lane])
2239 : : {
2240 : : /* ??? Below we require lane zero is present. */
2241 : 0 : if (lane == 0)
2242 : : {
2243 : : hard_fail = false;
2244 : 3344 : break;
2245 : : }
2246 : 0 : chains.quick_push (vNULL);
2247 : 0 : continue;
2248 : : }
2249 : : /* For each lane linearize the addition/subtraction (or other
2250 : : uniform associatable operation) expression tree. */
2251 : 4044 : gimple *op_stmt = NULL, *other_op_stmt = NULL;
2252 : 4044 : vect_slp_linearize_chain (vinfo, worklist, chain, code,
2253 : 4044 : stmts[lane]->stmt, op_stmt, other_op_stmt,
2254 : : NULL);
2255 : 4044 : if (!op_stmt_info && op_stmt)
2256 : 3101 : op_stmt_info = vinfo->lookup_stmt (op_stmt);
2257 : 4044 : if (!other_op_stmt_info && other_op_stmt)
2258 : 619 : other_op_stmt_info = vinfo->lookup_stmt (other_op_stmt);
2259 : 4044 : if (chain.length () == 2)
2260 : : {
2261 : : /* In a chain of just two elements resort to the regular
2262 : : operand swapping scheme. Likewise if we run into a
2263 : : length mismatch process regularly as well as we did not
2264 : : process the other lanes we cannot report a good hint what
2265 : : lanes to try swapping in the parent. */
2266 : : hard_fail = false;
2267 : : break;
2268 : : }
2269 : 700 : else if (chain_len == 0)
2270 : 279 : chain_len = chain.length ();
2271 : 842 : else if (chain.length () != chain_len)
2272 : : {
2273 : : /* ??? Here we could slip in magic to compensate with
2274 : : neutral operands. */
2275 : 0 : matches[lane] = false;
2276 : 0 : if (lane != group_size - 1)
2277 : 0 : matches[0] = false;
2278 : : break;
2279 : : }
2280 : 700 : chains.quick_push (chain.copy ());
2281 : 700 : chain.truncate (0);
2282 : : }
2283 : 7172 : if (chains.length () == group_size)
2284 : : {
2285 : : /* We cannot yet use SLP_TREE_CODE to communicate the operation. */
2286 : 242 : if (!op_stmt_info)
2287 : : {
2288 : 14 : hard_fail = false;
2289 : 14 : goto out;
2290 : : }
2291 : : /* Now we have a set of chains with the same length. */
2292 : : /* 1. pre-sort according to def_type and operation. */
2293 : 853 : for (unsigned lane = 0; lane < group_size; ++lane)
2294 : 1250 : chains[lane].stablesort (dt_sort_cmp, vinfo);
2295 : 228 : if (dump_enabled_p ())
2296 : : {
2297 : 107 : dump_printf_loc (MSG_NOTE, vect_location,
2298 : : "pre-sorted chains of %s\n",
2299 : : get_tree_code_name (code));
2300 : 465 : for (unsigned lane = 0; lane < group_size; ++lane)
2301 : : {
2302 : 358 : if (!stmts[lane])
2303 : 0 : dump_printf (MSG_NOTE, "--");
2304 : : else
2305 : 1646 : for (unsigned opnum = 0; opnum < chain_len; ++opnum)
2306 : 2576 : dump_printf (MSG_NOTE, "%s %T ",
2307 : 1288 : get_tree_code_name (chains[lane][opnum].code),
2308 : 1288 : chains[lane][opnum].op);
2309 : 358 : dump_printf (MSG_NOTE, "\n");
2310 : : }
2311 : : }
2312 : : /* 2. try to build children nodes, associating as necessary. */
2313 : : /* 2a. prepare and perform early checks to avoid eating into
2314 : : discovery limit unnecessarily. */
2315 : 228 : vect_def_type *dts = XALLOCAVEC (vect_def_type, chain_len);
2316 : 967 : for (unsigned n = 0; n < chain_len; ++n)
2317 : : {
2318 : 739 : vect_def_type dt = chains[0][n].dt;
2319 : 739 : unsigned lane;
2320 : 2860 : for (lane = 0; lane < group_size; ++lane)
2321 : 4242 : if (stmts[lane] && chains[lane][n].dt != dt)
2322 : : {
2323 : 0 : if (dt == vect_constant_def
2324 : 0 : && chains[lane][n].dt == vect_external_def)
2325 : : dt = vect_external_def;
2326 : 0 : else if (dt == vect_external_def
2327 : 0 : && chains[lane][n].dt == vect_constant_def)
2328 : : ;
2329 : : else
2330 : : break;
2331 : : }
2332 : 739 : if (lane != group_size)
2333 : : {
2334 : 0 : if (dump_enabled_p ())
2335 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2336 : : "giving up on chain due to mismatched "
2337 : : "def types\n");
2338 : 0 : matches[lane] = false;
2339 : 0 : if (lane != group_size - 1)
2340 : 0 : matches[0] = false;
2341 : 0 : goto out;
2342 : : }
2343 : 739 : dts[n] = dt;
2344 : 739 : if (dt == vect_constant_def
2345 : 739 : || dt == vect_external_def)
2346 : : {
2347 : : /* Check whether we can build the invariant. If we can't
2348 : : we never will be able to. */
2349 : 63 : tree type = TREE_TYPE (chains[0][n].op);
2350 : 739 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ()
2351 : : && (TREE_CODE (type) == BOOLEAN_TYPE
2352 : : || !can_duplicate_and_interleave_p (vinfo, group_size,
2353 : : type)))
2354 : : {
2355 : : matches[0] = false;
2356 : : goto out;
2357 : : }
2358 : : }
2359 : 676 : else if (dt != vect_internal_def)
2360 : : {
2361 : : /* Not sure, we might need sth special.
2362 : : gcc.dg/vect/pr96854.c,
2363 : : gfortran.dg/vect/fast-math-pr37021.f90
2364 : : and gfortran.dg/vect/pr61171.f trigger. */
2365 : : /* Soft-fail for now. */
2366 : 0 : hard_fail = false;
2367 : 0 : goto out;
2368 : : }
2369 : : }
2370 : : /* 2b. do the actual build. */
2371 : 903 : for (unsigned n = 0; n < chain_len; ++n)
2372 : : {
2373 : 696 : vect_def_type dt = dts[n];
2374 : 696 : unsigned lane;
2375 : 696 : if (dt == vect_constant_def
2376 : 696 : || dt == vect_external_def)
2377 : : {
2378 : 63 : vec<tree> ops;
2379 : 63 : ops.create (group_size);
2380 : 317 : for (lane = 0; lane < group_size; ++lane)
2381 : 191 : if (stmts[lane])
2382 : 191 : ops.quick_push (chains[lane][n].op);
2383 : : else
2384 : 0 : ops.quick_push (NULL_TREE);
2385 : 63 : slp_tree child = vect_create_new_slp_node (ops);
2386 : 63 : SLP_TREE_DEF_TYPE (child) = dt;
2387 : 63 : children.safe_push (child);
2388 : : }
2389 : : else
2390 : : {
2391 : 633 : vec<stmt_vec_info> op_stmts;
2392 : 633 : op_stmts.create (group_size);
2393 : 633 : slp_tree child = NULL;
2394 : : /* Brute-force our way. We have to consider a lane
2395 : : failing after fixing an earlier fail up in the
2396 : : SLP discovery recursion. So track the current
2397 : : permute per lane. */
2398 : 633 : unsigned *perms = XALLOCAVEC (unsigned, group_size);
2399 : 633 : memset (perms, 0, sizeof (unsigned) * group_size);
2400 : 738 : do
2401 : : {
2402 : 738 : op_stmts.truncate (0);
2403 : 3626 : for (lane = 0; lane < group_size; ++lane)
2404 : 2150 : if (stmts[lane])
2405 : 2150 : op_stmts.quick_push
2406 : 2150 : (vinfo->lookup_def (chains[lane][n].op));
2407 : : else
2408 : 0 : op_stmts.quick_push (NULL);
2409 : 738 : child = vect_build_slp_tree (vinfo, op_stmts,
2410 : : group_size, &this_max_nunits,
2411 : : matches, limit,
2412 : : &this_tree_size, bst_map);
2413 : : /* ??? We're likely getting too many fatal mismatches
2414 : : here so maybe we want to ignore them (but then we
2415 : : have no idea which lanes fatally mismatched). */
2416 : 738 : if (child || !matches[0])
2417 : : break;
2418 : : /* Swap another lane we have not yet matched up into
2419 : : lanes that did not match. If we run out of
2420 : : permute possibilities for a lane terminate the
2421 : : search. */
2422 : 345 : bool term = false;
2423 : 345 : for (lane = 1; lane < group_size; ++lane)
2424 : 240 : if (!matches[lane])
2425 : : {
2426 : 184 : if (n + perms[lane] + 1 == chain_len)
2427 : : {
2428 : : term = true;
2429 : : break;
2430 : : }
2431 : 163 : if (dump_enabled_p ())
2432 : 107 : dump_printf_loc (MSG_NOTE, vect_location,
2433 : : "swapping operand %d and %d "
2434 : : "of lane %d\n",
2435 : : n, n + perms[lane] + 1, lane);
2436 : 326 : std::swap (chains[lane][n],
2437 : 163 : chains[lane][n + perms[lane] + 1]);
2438 : 163 : perms[lane]++;
2439 : : }
2440 : 126 : if (term)
2441 : : break;
2442 : : }
2443 : : while (1);
2444 : 633 : if (!child)
2445 : : {
2446 : 21 : if (dump_enabled_p ())
2447 : 16 : dump_printf_loc (MSG_NOTE, vect_location,
2448 : : "failed to match up op %d\n", n);
2449 : 21 : op_stmts.release ();
2450 : 21 : if (lane != group_size - 1)
2451 : 9 : matches[0] = false;
2452 : : else
2453 : 12 : matches[lane] = false;
2454 : 21 : goto out;
2455 : : }
2456 : 612 : if (dump_enabled_p ())
2457 : : {
2458 : 280 : dump_printf_loc (MSG_NOTE, vect_location,
2459 : : "matched up op %d to\n", n);
2460 : 280 : vect_print_slp_tree (MSG_NOTE, vect_location, child);
2461 : : }
2462 : 612 : children.safe_push (child);
2463 : : }
2464 : : }
2465 : : /* 3. build SLP nodes to combine the chain. */
2466 : 757 : for (unsigned lane = 0; lane < group_size; ++lane)
2467 : 1112 : if (stmts[lane] && chains[lane][0].code != code)
2468 : : {
2469 : : /* See if there's any alternate all-PLUS entry. */
2470 : : unsigned n;
2471 : 6 : for (n = 1; n < chain_len; ++n)
2472 : : {
2473 : 30 : for (lane = 0; lane < group_size; ++lane)
2474 : 48 : if (stmts[lane] && chains[lane][n].code != code)
2475 : : break;
2476 : 6 : if (lane == group_size)
2477 : : break;
2478 : : }
2479 : 6 : if (n != chain_len)
2480 : : {
2481 : : /* Swap that in at first position. */
2482 : 6 : std::swap (children[0], children[n]);
2483 : 30 : for (lane = 0; lane < group_size; ++lane)
2484 : 24 : if (stmts[lane])
2485 : 24 : std::swap (chains[lane][0], chains[lane][n]);
2486 : : }
2487 : : else
2488 : : {
2489 : : /* ??? When this triggers and we end up with two
2490 : : vect_constant/external_def up-front things break (ICE)
2491 : : spectacularly finding an insertion place for the
2492 : : all-constant op. We should have a fully
2493 : : vect_internal_def operand though(?) so we can swap
2494 : : that into first place and then prepend the all-zero
2495 : : constant. */
2496 : 0 : if (dump_enabled_p ())
2497 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2498 : : "inserting constant zero to compensate "
2499 : : "for (partially) negated first "
2500 : : "operand\n");
2501 : 0 : chain_len++;
2502 : 0 : for (lane = 0; lane < group_size; ++lane)
2503 : 0 : if (stmts[lane])
2504 : 0 : chains[lane].safe_insert
2505 : 0 : (0, chain_op_t (code, vect_constant_def, NULL_TREE));
2506 : 0 : vec<tree> zero_ops;
2507 : 0 : zero_ops.create (group_size);
2508 : 0 : zero_ops.quick_push (build_zero_cst (TREE_TYPE (vectype)));
2509 : 0 : for (lane = 1; lane < group_size; ++lane)
2510 : 0 : if (stmts[lane])
2511 : 0 : zero_ops.quick_push (zero_ops[0]);
2512 : : else
2513 : 0 : zero_ops.quick_push (NULL_TREE);
2514 : 0 : slp_tree zero = vect_create_new_slp_node (zero_ops);
2515 : 0 : SLP_TREE_DEF_TYPE (zero) = vect_constant_def;
2516 : 0 : children.safe_insert (0, zero);
2517 : : }
2518 : : break;
2519 : : }
2520 : 674 : for (unsigned i = 1; i < children.length (); ++i)
2521 : : {
2522 : 467 : slp_tree op0 = children[i - 1];
2523 : 467 : slp_tree op1 = children[i];
2524 : 467 : bool this_two_op = false;
2525 : 1656 : for (unsigned lane = 0; lane < group_size; ++lane)
2526 : 2636 : if (stmts[lane] && chains[lane][i].code != chains[0][i].code)
2527 : : {
2528 : : this_two_op = true;
2529 : : break;
2530 : : }
2531 : 467 : slp_tree child;
2532 : 467 : if (i == children.length () - 1)
2533 : 207 : child = vect_create_new_slp_node (node, stmts, 2);
2534 : : else
2535 : 260 : child = vect_create_new_slp_node (2, ERROR_MARK);
2536 : 467 : if (this_two_op)
2537 : : {
2538 : 129 : vec<std::pair<unsigned, unsigned> > lperm;
2539 : 129 : lperm.create (group_size);
2540 : 495 : for (unsigned lane = 0; lane < group_size; ++lane)
2541 : 732 : lperm.quick_push (std::make_pair
2542 : 366 : (chains[lane][i].code != chains[0][i].code, lane));
2543 : 258 : vect_slp_build_two_operator_nodes (child, vectype, op0, op1,
2544 : 129 : (chains[0][i].code == code
2545 : : ? op_stmt_info
2546 : : : other_op_stmt_info),
2547 : 129 : (chains[0][i].code == code
2548 : : ? other_op_stmt_info
2549 : : : op_stmt_info),
2550 : : lperm);
2551 : : }
2552 : : else
2553 : : {
2554 : 338 : SLP_TREE_DEF_TYPE (child) = vect_internal_def;
2555 : 338 : SLP_TREE_VECTYPE (child) = vectype;
2556 : 338 : SLP_TREE_LANES (child) = group_size;
2557 : 338 : SLP_TREE_CHILDREN (child).quick_push (op0);
2558 : 338 : SLP_TREE_CHILDREN (child).quick_push (op1);
2559 : 338 : SLP_TREE_REPRESENTATIVE (child)
2560 : 676 : = (chains[0][i].code == code
2561 : 338 : ? op_stmt_info : other_op_stmt_info);
2562 : : }
2563 : 467 : children[i] = child;
2564 : : }
2565 : 207 : *tree_size += this_tree_size + 1;
2566 : 207 : *max_nunits = this_max_nunits;
2567 : 1009 : while (!chains.is_empty ())
2568 : 574 : chains.pop ().release ();
2569 : : return node;
2570 : : }
2571 : 3344 : out:
2572 : 3379 : if (dump_enabled_p ())
2573 : 2642 : dump_printf_loc (MSG_NOTE, vect_location,
2574 : : "failed to line up SLP graph by re-associating "
2575 : : "operations in lanes%s\n",
2576 : : !hard_fail ? " trying regular discovery" : "");
2577 : 3380 : while (!children.is_empty ())
2578 : 1 : vect_free_slp_tree (children.pop ());
2579 : 3505 : while (!chains.is_empty ())
2580 : 126 : chains.pop ().release ();
2581 : : /* Hard-fail, otherwise we might run into quadratic processing of the
2582 : : chains starting one stmt into the chain again. */
2583 : 3379 : if (hard_fail)
2584 : : return NULL;
2585 : : /* Fall thru to normal processing. */
2586 : 3586 : }
2587 : :
2588 : : /* Get at the operands, verifying they are compatible. */
2589 : 3040930 : vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
2590 : 3040930 : slp_oprnd_info oprnd_info;
2591 : 15455686 : FOR_EACH_VEC_ELT (stmts, i, stmt_info)
2592 : : {
2593 : 12417581 : int res = vect_get_and_check_slp_defs (vinfo, swap[i], skip_args,
2594 : : stmts, i, &oprnds_info);
2595 : 12417581 : if (res != 0)
2596 : 629763 : matches[(res == -1) ? 0 : i] = false;
2597 : 12417581 : if (!matches[0])
2598 : : break;
2599 : : }
2600 : 15101336 : for (i = 0; i < group_size; ++i)
2601 : 12307796 : if (!matches[i])
2602 : : {
2603 : 247390 : vect_free_oprnd_info (oprnds_info);
2604 : 247390 : return NULL;
2605 : : }
2606 : 8380620 : swap = NULL;
2607 : :
2608 : 8380620 : bool has_two_operators_perm = false;
2609 : 16761240 : auto_vec<unsigned> two_op_perm_indices[2];
2610 : 2793540 : vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL};
2611 : :
2612 : 2804676 : if (two_operators && oprnds_info.length () == 2 && group_size > 2)
2613 : : {
2614 : 1431 : unsigned idx = 0;
2615 : 1431 : hash_map<gimple *, unsigned> seen;
2616 : 1431 : vec<slp_oprnd_info> new_oprnds_info
2617 : 1431 : = vect_create_oprnd_info (1, group_size);
2618 : 1431 : bool success = true;
2619 : :
2620 : 1431 : enum tree_code code = ERROR_MARK;
2621 : 1431 : if (oprnds_info[0]->def_stmts[0]
2622 : 1431 : && is_a<gassign *> (oprnds_info[0]->def_stmts[0]->stmt))
2623 : 1333 : code = gimple_assign_rhs_code (oprnds_info[0]->def_stmts[0]->stmt);
2624 : 1431 : basic_block bb = nullptr;
2625 : :
2626 : 4555 : for (unsigned j = 0; j < group_size; ++j)
2627 : : {
2628 : 11244 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2629 : : {
2630 : 8120 : stmt_vec_info stmt_info = oprnd_info->def_stmts[j];
2631 : 8120 : if (!stmt_info
2632 : 7897 : || !is_a<gassign *> (stmt_info->stmt)
2633 : 7894 : || gimple_assign_rhs_code (stmt_info->stmt) != code
2634 : 15179 : || skip_args[i])
2635 : : {
2636 : : success = false;
2637 : 1065 : break;
2638 : : }
2639 : : /* Avoid mixing lanes with defs in different basic-blocks. */
2640 : 7059 : if (!bb)
2641 : 1507 : bb = gimple_bb (vect_orig_stmt (stmt_info)->stmt);
2642 : 7090 : else if (gimple_bb (vect_orig_stmt (stmt_info)->stmt) != bb)
2643 : : {
2644 : : success = false;
2645 : : break;
2646 : : }
2647 : :
2648 : 7055 : bool exists;
2649 : 7055 : unsigned &stmt_idx
2650 : 7055 : = seen.get_or_insert (stmt_info->stmt, &exists);
2651 : :
2652 : 7055 : if (!exists)
2653 : : {
2654 : 6039 : new_oprnds_info[0]->def_stmts.safe_push (stmt_info);
2655 : 6039 : new_oprnds_info[0]->ops.safe_push (oprnd_info->ops[j]);
2656 : 6039 : stmt_idx = idx;
2657 : 6039 : idx++;
2658 : : }
2659 : :
2660 : 7055 : two_op_perm_indices[i].safe_push (stmt_idx);
2661 : : }
2662 : :
2663 : 4189 : if (!success)
2664 : : break;
2665 : : }
2666 : :
2667 : 1431 : if (success && idx == group_size)
2668 : : {
2669 : 47 : if (dump_enabled_p ())
2670 : : {
2671 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2672 : : "Replace two_operators operands:\n");
2673 : :
2674 : 0 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2675 : : {
2676 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2677 : : "Operand %u:\n", i);
2678 : 0 : for (unsigned j = 0; j < group_size; j++)
2679 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2680 : 0 : j, oprnd_info->def_stmts[j]->stmt);
2681 : : }
2682 : :
2683 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2684 : : "With a single operand:\n");
2685 : 0 : for (unsigned j = 0; j < group_size; j++)
2686 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "\tstmt %u %G",
2687 : 0 : j, new_oprnds_info[0]->def_stmts[j]->stmt);
2688 : : }
2689 : :
2690 : 47 : two_op_scalar_stmts[0].safe_splice (oprnds_info[0]->def_stmts);
2691 : 47 : two_op_scalar_stmts[1].safe_splice (oprnds_info[1]->def_stmts);
2692 : :
2693 : 47 : new_oprnds_info[0]->first_op_type = oprnds_info[0]->first_op_type;
2694 : 47 : new_oprnds_info[0]->first_dt = oprnds_info[0]->first_dt;
2695 : 47 : new_oprnds_info[0]->any_pattern = oprnds_info[0]->any_pattern;
2696 : 47 : new_oprnds_info[0]->first_gs_p = oprnds_info[0]->first_gs_p;
2697 : 47 : new_oprnds_info[0]->first_gs_info = oprnds_info[0]->first_gs_info;
2698 : :
2699 : 47 : vect_free_oprnd_info (oprnds_info);
2700 : 47 : oprnds_info = new_oprnds_info;
2701 : 47 : nops = 1;
2702 : 47 : has_two_operators_perm = true;
2703 : : }
2704 : : else
2705 : 1384 : vect_free_oprnd_info (new_oprnds_info);
2706 : 1431 : }
2707 : :
2708 : 5587080 : auto_vec<slp_tree, 4> children;
2709 : :
2710 : 2793540 : stmt_info = stmts[0];
2711 : :
2712 : : /* Create SLP_TREE nodes for the definition node/s. */
2713 : 7091758 : FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
2714 : : {
2715 : 4411655 : slp_tree child = nullptr;
2716 : 4411655 : unsigned int j;
2717 : :
2718 : : /* We're skipping certain operands from processing, for example
2719 : : outer loop reduction initial defs. */
2720 : 4411655 : if (skip_args[i])
2721 : : {
2722 : 572049 : children.safe_push (NULL);
2723 : 4298218 : continue;
2724 : : }
2725 : :
2726 : 3839606 : if (oprnd_info->first_dt == vect_uninitialized_def)
2727 : : {
2728 : : /* COND_EXPR have one too many eventually if the condition
2729 : : is a SSA name. */
2730 : 0 : gcc_assert (i == 3 && nops == 4);
2731 : 0 : continue;
2732 : : }
2733 : :
2734 : 3839606 : if (is_a <bb_vec_info> (vinfo)
2735 : 1777523 : && oprnd_info->first_dt == vect_internal_def
2736 : 4780957 : && !oprnd_info->any_pattern)
2737 : : {
2738 : : /* For BB vectorization, if all defs are the same do not
2739 : : bother to continue the build along the single-lane
2740 : : graph but use a splat of the scalar value. */
2741 : 896382 : stmt_vec_info first_def = oprnd_info->def_stmts[0];
2742 : 962325 : for (j = 1; j < group_size; ++j)
2743 : 908868 : if (oprnd_info->def_stmts[j] != first_def)
2744 : : break;
2745 : 896382 : if (j == group_size
2746 : : /* But avoid doing this for loads where we may be
2747 : : able to CSE things, unless the stmt is not
2748 : : vectorizable. */
2749 : 896382 : && (!STMT_VINFO_VECTORIZABLE (first_def)
2750 : 67254 : || !gimple_vuse (first_def->stmt)))
2751 : : {
2752 : 43446 : if (dump_enabled_p ())
2753 : 96 : dump_printf_loc (MSG_NOTE, vect_location,
2754 : : "Using a splat of the uniform operand %G",
2755 : : first_def->stmt);
2756 : 43446 : oprnd_info->first_dt = vect_external_def;
2757 : : }
2758 : : }
2759 : :
2760 : 3839606 : if (oprnd_info->first_dt == vect_external_def
2761 : 3839606 : || oprnd_info->first_dt == vect_constant_def)
2762 : : {
2763 : 1481562 : if (!GET_MODE_SIZE (vinfo->vector_mode).is_constant ())
2764 : : {
2765 : : tree op0;
2766 : : tree uniform_val = op0 = oprnd_info->ops[0];
2767 : : for (j = 1; j < oprnd_info->ops.length (); ++j)
2768 : : if (oprnd_info->ops[j]
2769 : : && !operand_equal_p (uniform_val, oprnd_info->ops[j]))
2770 : : {
2771 : : uniform_val = NULL_TREE;
2772 : : break;
2773 : : }
2774 : : if (!uniform_val
2775 : : && !can_duplicate_and_interleave_p (vinfo,
2776 : : oprnd_info->ops.length (),
2777 : : TREE_TYPE (op0)))
2778 : : {
2779 : : matches[j] = false;
2780 : : if (dump_enabled_p ())
2781 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2782 : : "Build SLP failed: invalid type of def "
2783 : : "for variable-length SLP %T\n", op0);
2784 : : goto fail;
2785 : : }
2786 : : }
2787 : 1481562 : slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops);
2788 : 1481562 : SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt;
2789 : 1481562 : oprnd_info->ops = vNULL;
2790 : 1481562 : children.safe_push (invnode);
2791 : 1481562 : continue;
2792 : 1481562 : }
2793 : :
2794 : : /* When we have a masked load with uniform mask discover this
2795 : : as a single-lane mask with a splat permute. This way we can
2796 : : recognize this as a masked load-lane by stripping the splat. */
2797 : 2358044 : if (is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
2798 : 30132 : && gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
2799 : : IFN_MASK_LOAD)
2800 : 4209 : && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2801 : 2358066 : && ! STMT_VINFO_SLP_VECT_ONLY (DR_GROUP_FIRST_ELEMENT (stmt_info)))
2802 : : {
2803 : 0 : vec<stmt_vec_info> def_stmts2;
2804 : 0 : def_stmts2.create (1);
2805 : 0 : def_stmts2.quick_push (oprnd_info->def_stmts[0]);
2806 : 0 : child = vect_build_slp_tree (vinfo, def_stmts2, 1,
2807 : : &this_max_nunits,
2808 : : matches, limit,
2809 : : &this_tree_size, bst_map);
2810 : 0 : if (child)
2811 : : {
2812 : 0 : slp_tree pnode = vect_create_new_slp_node (1, VEC_PERM_EXPR);
2813 : 0 : SLP_TREE_VECTYPE (pnode) = SLP_TREE_VECTYPE (child);
2814 : 0 : SLP_TREE_LANES (pnode) = group_size;
2815 : 0 : SLP_TREE_SCALAR_STMTS (pnode).create (group_size);
2816 : 0 : SLP_TREE_LANE_PERMUTATION (pnode).create (group_size);
2817 : 0 : for (unsigned k = 0; k < group_size; ++k)
2818 : : {
2819 : 0 : SLP_TREE_SCALAR_STMTS (pnode)
2820 : 0 : .quick_push (oprnd_info->def_stmts[0]);
2821 : 0 : SLP_TREE_LANE_PERMUTATION (pnode)
2822 : 0 : .quick_push (std::make_pair (0u, 0u));
2823 : : }
2824 : 0 : SLP_TREE_CHILDREN (pnode).quick_push (child);
2825 : 0 : pnode->max_nunits = child->max_nunits;
2826 : 0 : children.safe_push (pnode);
2827 : 0 : oprnd_info->def_stmts = vNULL;
2828 : 0 : continue;
2829 : 0 : }
2830 : : else
2831 : 0 : def_stmts2.release ();
2832 : : }
2833 : :
2834 : 2358044 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
2835 : : group_size, &this_max_nunits,
2836 : : matches, limit,
2837 : : &this_tree_size, bst_map)) != NULL)
2838 : : {
2839 : 1851932 : oprnd_info->def_stmts = vNULL;
2840 : 1851932 : children.safe_push (child);
2841 : 1851932 : continue;
2842 : : }
2843 : :
2844 : : /* If the SLP build for operand zero failed and operand zero
2845 : : and one can be commutated try that for the scalar stmts
2846 : : that failed the match. */
2847 : 506112 : if (i == 0
2848 : : /* A first scalar stmt mismatch signals a fatal mismatch. */
2849 : 360853 : && matches[0]
2850 : : /* ??? For COND_EXPRs we can swap the comparison operands
2851 : : as well as the arms under some constraints. */
2852 : 218797 : && (nops == 2 || nops == 3)
2853 : 126787 : && oprnds_info[1]->first_dt == vect_internal_def
2854 : 72977 : && (is_gimple_assign (stmt_info->stmt)
2855 : 18998 : || is_gimple_call (stmt_info->stmt))
2856 : : /* Swapping operands for reductions breaks assumptions later on. */
2857 : 560112 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)
2858 : : {
2859 : : /* See whether we can swap the matching or the non-matching
2860 : : stmt operands. */
2861 : : bool swap_not_matching = true;
2862 : 65582 : do
2863 : : {
2864 : 7046916 : for (j = 0; j < group_size; ++j)
2865 : : {
2866 : 7003325 : if (matches[j] != !swap_not_matching)
2867 : 70788 : continue;
2868 : 6932537 : stmt_vec_info stmt_info = stmts[j];
2869 : : /* Verify if we can swap operands of this stmt. */
2870 : 6932537 : if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
2871 : : {
2872 : 6932495 : tree_code code = gimple_assign_rhs_code (stmt);
2873 : 6932495 : if (! commutative_tree_code (code)
2874 : 6932495 : && ! commutative_ternary_tree_code (code))
2875 : : {
2876 : 21951 : if (!swap_not_matching)
2877 : 10293 : goto fail;
2878 : : swap_not_matching = false;
2879 : : break;
2880 : : }
2881 : : }
2882 : 6981376 : else if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2883 : : {
2884 : 42 : internal_fn fn = (gimple_call_internal_p (call)
2885 : 42 : ? gimple_call_internal_fn (call)
2886 : : : IFN_LAST);
2887 : 42 : if ((! commutative_binary_fn_p (fn)
2888 : 42 : && ! commutative_ternary_fn_p (fn))
2889 : 44 : || first_commutative_argument (fn) != 0)
2890 : : {
2891 : 40 : if (!swap_not_matching)
2892 : 20 : goto fail;
2893 : : swap_not_matching = false;
2894 : : break;
2895 : : }
2896 : : }
2897 : : }
2898 : : }
2899 : 55269 : while (j != group_size);
2900 : :
2901 : : /* Swap mismatched definition stmts. */
2902 : 43591 : if (dump_enabled_p ())
2903 : 330 : dump_printf_loc (MSG_NOTE, vect_location,
2904 : : "Re-trying with swapped operands of stmts ");
2905 : 7012446 : for (j = 0; j < group_size; ++j)
2906 : 6968855 : if (matches[j] == !swap_not_matching)
2907 : : {
2908 : 13820820 : std::swap (oprnds_info[0]->def_stmts[j],
2909 : 6910410 : oprnds_info[1]->def_stmts[j]);
2910 : 13820820 : std::swap (oprnds_info[0]->ops[j],
2911 : 6910410 : oprnds_info[1]->ops[j]);
2912 : 6910410 : if (dump_enabled_p ())
2913 : 943 : dump_printf (MSG_NOTE, "%d ", j);
2914 : : }
2915 : 43591 : if (dump_enabled_p ())
2916 : 330 : dump_printf (MSG_NOTE, "\n");
2917 : : /* After swapping some operands we lost track whether an
2918 : : operand has any pattern defs so be conservative here. */
2919 : 83965 : if (oprnds_info[0]->any_pattern || oprnds_info[1]->any_pattern)
2920 : 3238 : oprnds_info[0]->any_pattern = oprnds_info[1]->any_pattern = true;
2921 : : /* And try again with scratch 'matches' ... */
2922 : 43591 : bool *tem = XALLOCAVEC (bool, group_size);
2923 : 43591 : if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
2924 : : group_size, &this_max_nunits,
2925 : : tem, limit,
2926 : : &this_tree_size, bst_map)) != NULL)
2927 : : {
2928 : 7805 : oprnd_info->def_stmts = vNULL;
2929 : 7805 : children.safe_push (child);
2930 : 7805 : continue;
2931 : : }
2932 : : }
2933 : 498307 : fail:
2934 : :
2935 : : /* If the SLP build failed and we analyze a basic-block
2936 : : simply treat nodes we fail to build as externally defined
2937 : : (and thus build vectors from the scalar defs).
2938 : : The cost model will reject outright expensive cases.
2939 : : ??? This doesn't treat cases where permutation ultimatively
2940 : : fails (or we don't try permutation below). Ideally we'd
2941 : : even compute a permutation that will end up with the maximum
2942 : : SLP tree size... */
2943 : 498307 : if (is_a <bb_vec_info> (vinfo)
2944 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
2945 : : do extra work to cancel the pattern so the uses see the
2946 : : scalar version. */
2947 : 485469 : && !is_pattern_stmt_p (stmt_info)
2948 : 957033 : && !oprnd_info->any_pattern)
2949 : : {
2950 : : /* But if there's a leading vector sized set of matching stmts
2951 : : fail here so we can split the group. This matches the condition
2952 : : vect_analyze_slp_instance uses. */
2953 : : /* ??? We might want to split here and combine the results to support
2954 : : multiple vector sizes better. */
2955 : 743103 : for (j = 0; j < group_size; ++j)
2956 : 743103 : if (!matches[j])
2957 : : break;
2958 : 458445 : if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype))
2959 : 458399 : && vect_slp_can_convert_to_external (oprnd_info->def_stmts))
2960 : : {
2961 : 384870 : if (dump_enabled_p ())
2962 : 469 : dump_printf_loc (MSG_NOTE, vect_location,
2963 : : "Building vector operands from scalars\n");
2964 : 384870 : this_tree_size++;
2965 : 384870 : child = vect_create_new_slp_node (oprnd_info->ops);
2966 : 384870 : children.safe_push (child);
2967 : 384870 : oprnd_info->ops = vNULL;
2968 : 384870 : continue;
2969 : : }
2970 : : }
2971 : :
2972 : 113437 : gcc_assert (child == NULL);
2973 : 124056 : FOR_EACH_VEC_ELT (children, j, child)
2974 : 10619 : if (child)
2975 : 10619 : vect_free_slp_tree (child);
2976 : 113437 : vect_free_oprnd_info (oprnds_info);
2977 : 113437 : return NULL;
2978 : : }
2979 : :
2980 : 2680103 : vect_free_oprnd_info (oprnds_info);
2981 : :
2982 : : /* If we have all children of a child built up from uniform scalars
2983 : : or does more than one possibly expensive vector construction then
2984 : : just throw that away, causing it built up from scalars.
2985 : : The exception is the SLP node for the vector store. */
2986 : 2680103 : if (is_a <bb_vec_info> (vinfo)
2987 : 1215648 : && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
2988 : : /* ??? Rejecting patterns this way doesn't work. We'd have to
2989 : : do extra work to cancel the pattern so the uses see the
2990 : : scalar version. */
2991 : 3160710 : && !is_pattern_stmt_p (stmt_info))
2992 : : {
2993 : : slp_tree child;
2994 : : unsigned j;
2995 : : bool all_uniform_p = true;
2996 : : unsigned n_vector_builds = 0;
2997 : 1339191 : FOR_EACH_VEC_ELT (children, j, child)
2998 : : {
2999 : 884199 : if (!child)
3000 : : ;
3001 : 884199 : else if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3002 : : all_uniform_p = false;
3003 : 632664 : else if (!vect_slp_tree_uniform_p (child))
3004 : : {
3005 : 482721 : all_uniform_p = false;
3006 : 482721 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def)
3007 : 444622 : n_vector_builds++;
3008 : : }
3009 : : }
3010 : 454992 : if (all_uniform_p
3011 : 454992 : || n_vector_builds > 1
3012 : 776600 : || (n_vector_builds == children.length ()
3013 : 34292 : && is_a <gphi *> (stmt_info->stmt)))
3014 : : {
3015 : : /* Roll back. */
3016 : 141831 : matches[0] = false;
3017 : 437228 : FOR_EACH_VEC_ELT (children, j, child)
3018 : 295397 : if (child)
3019 : 295397 : vect_free_slp_tree (child);
3020 : :
3021 : 141831 : if (dump_enabled_p ())
3022 : 114 : dump_printf_loc (MSG_NOTE, vect_location,
3023 : : "Building parent vector operands from "
3024 : : "scalars instead\n");
3025 : 141831 : return NULL;
3026 : : }
3027 : : }
3028 : :
3029 : 2538272 : *tree_size += this_tree_size + 1;
3030 : 2538272 : *max_nunits = this_max_nunits;
3031 : :
3032 : 2538272 : if (two_operators)
3033 : : {
3034 : : /* ??? We'd likely want to either cache in bst_map sth like
3035 : : { a+b, NULL, a+b, NULL } and { NULL, a-b, NULL, a-b } or
3036 : : the true { a+b, a+b, a+b, a+b } ... but there we don't have
3037 : : explicit stmts to put in so the keying on 'stmts' doesn't
3038 : : work (but we have the same issue with nodes that use 'ops'). */
3039 : :
3040 : 4220 : if (has_two_operators_perm)
3041 : : {
3042 : 23 : slp_tree child = children[0];
3043 : 23 : children.truncate (0);
3044 : 69 : for (i = 0; i < 2; i++)
3045 : : {
3046 : 46 : slp_tree pnode
3047 : 46 : = vect_create_new_slp_node (two_op_scalar_stmts[i], 2);
3048 : 46 : SLP_TREE_CODE (pnode) = VEC_PERM_EXPR;
3049 : 46 : SLP_TREE_VECTYPE (pnode) = vectype;
3050 : 46 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3051 : 46 : SLP_TREE_CHILDREN (pnode).quick_push (child);
3052 : 46 : lane_permutation_t& perm = SLP_TREE_LANE_PERMUTATION (pnode);
3053 : 46 : children.safe_push (pnode);
3054 : :
3055 : 486 : for (unsigned j = 0; j < stmts.length (); j++)
3056 : 440 : perm.safe_push (std::make_pair (0, two_op_perm_indices[i][j]));
3057 : : }
3058 : :
3059 : 23 : SLP_TREE_REF_COUNT (child) += 4;
3060 : : }
3061 : :
3062 : 4220 : slp_tree one = new _slp_tree;
3063 : 4220 : slp_tree two = new _slp_tree;
3064 : 4220 : SLP_TREE_DEF_TYPE (one) = vect_internal_def;
3065 : 4220 : SLP_TREE_DEF_TYPE (two) = vect_internal_def;
3066 : 4220 : SLP_TREE_VECTYPE (one) = vectype;
3067 : 4220 : SLP_TREE_VECTYPE (two) = vectype;
3068 : 4220 : SLP_TREE_CHILDREN (one).safe_splice (children);
3069 : 4220 : SLP_TREE_CHILDREN (two).safe_splice (children);
3070 : 4220 : slp_tree child;
3071 : 16882 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (two), i, child)
3072 : 8442 : SLP_TREE_REF_COUNT (child)++;
3073 : :
3074 : : /* Here we record the original defs since this
3075 : : node represents the final lane configuration. */
3076 : 4220 : node = vect_create_new_slp_node (node, stmts, 2);
3077 : 4220 : SLP_TREE_VECTYPE (node) = vectype;
3078 : 4220 : SLP_TREE_CODE (node) = VEC_PERM_EXPR;
3079 : 4220 : SLP_TREE_CHILDREN (node).quick_push (one);
3080 : 4220 : SLP_TREE_CHILDREN (node).quick_push (two);
3081 : 4220 : enum tree_code code0 = ERROR_MARK;
3082 : 4220 : enum tree_code ocode = ERROR_MARK;
3083 : 4220 : if (gassign *stmt = dyn_cast <gassign *> (stmts[0]->stmt))
3084 : 4218 : code0 = gimple_assign_rhs_code (stmt);
3085 : 4220 : stmt_vec_info ostmt_info;
3086 : 4220 : unsigned j = 0;
3087 : 15970 : FOR_EACH_VEC_ELT (stmts, i, ostmt_info)
3088 : : {
3089 : 11750 : int op = 0;
3090 : 11750 : if (gassign *ostmt = dyn_cast <gassign *> (ostmt_info->stmt))
3091 : : {
3092 : 11746 : if (gimple_assign_rhs_code (ostmt) != code0)
3093 : : {
3094 : 5877 : ocode = gimple_assign_rhs_code (ostmt);
3095 : : op = 1;
3096 : : j = i;
3097 : : }
3098 : : }
3099 : : else
3100 : : {
3101 : 8 : if (gimple_call_combined_fn (stmts[0]->stmt)
3102 : 4 : != gimple_call_combined_fn (ostmt_info->stmt))
3103 : : {
3104 : 2 : op = 1;
3105 : 2 : j = i;
3106 : : }
3107 : : }
3108 : 11750 : SLP_TREE_LANE_PERMUTATION (node).safe_push (std::make_pair (op, i));
3109 : : }
3110 : 4220 : SLP_TREE_CODE (one) = code0;
3111 : 4220 : SLP_TREE_CODE (two) = ocode;
3112 : 4220 : SLP_TREE_LANES (one) = stmts.length ();
3113 : 4220 : SLP_TREE_LANES (two) = stmts.length ();
3114 : 4220 : SLP_TREE_REPRESENTATIVE (one) = stmts[0];
3115 : 4220 : SLP_TREE_REPRESENTATIVE (two) = stmts[j];
3116 : :
3117 : 4220 : return node;
3118 : : }
3119 : :
3120 : 2534052 : node = vect_create_new_slp_node (node, stmts, nops);
3121 : 2534052 : SLP_TREE_VECTYPE (node) = vectype;
3122 : 2534052 : SLP_TREE_CHILDREN (node).splice (children);
3123 : 2534052 : return node;
3124 : 8380620 : }
3125 : :
3126 : : /* Dump a single SLP tree NODE. */
3127 : :
3128 : : static void
3129 : 408151 : vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
3130 : : slp_tree node)
3131 : : {
3132 : 408151 : unsigned i, j;
3133 : 408151 : slp_tree child;
3134 : 408151 : stmt_vec_info stmt_info;
3135 : 408151 : tree op;
3136 : :
3137 : 408151 : dump_metadata_t metadata (dump_kind, loc.get_impl_location ());
3138 : 408151 : dump_user_location_t user_loc = loc.get_user_location ();
3139 : 408151 : dump_printf_loc (metadata, user_loc,
3140 : : "node%s %p (max_nunits=" HOST_WIDE_INT_PRINT_UNSIGNED
3141 : : ", refcnt=%u)",
3142 : 408151 : SLP_TREE_DEF_TYPE (node) == vect_external_def
3143 : : ? " (external)"
3144 : : : (SLP_TREE_DEF_TYPE (node) == vect_constant_def
3145 : 393804 : ? " (constant)"
3146 : : : ""), (void *) node,
3147 : 408151 : estimated_poly_value (node->max_nunits),
3148 : : SLP_TREE_REF_COUNT (node));
3149 : 408151 : if (SLP_TREE_VECTYPE (node))
3150 : 344111 : dump_printf (metadata, " %T", SLP_TREE_VECTYPE (node));
3151 : 408151 : dump_printf (metadata, "%s\n",
3152 : 408151 : node->avoid_stlf_fail ? " (avoid-stlf-fail)" : "");
3153 : 408151 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
3154 : : {
3155 : 329872 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
3156 : 12983 : dump_printf_loc (metadata, user_loc, "op: VEC_PERM_EXPR\n");
3157 : : else
3158 : 316889 : dump_printf_loc (metadata, user_loc, "op template: %G",
3159 : 316889 : SLP_TREE_REPRESENTATIVE (node)->stmt);
3160 : : }
3161 : 408151 : if (SLP_TREE_SCALAR_STMTS (node).exists ())
3162 : 785415 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3163 : 463083 : if (stmt_info)
3164 : 458259 : dump_printf_loc (metadata, user_loc, "\t%sstmt %u %G",
3165 : 458259 : STMT_VINFO_LIVE_P (stmt_info) ? "[l] " : "",
3166 : : i, stmt_info->stmt);
3167 : : else
3168 : 4824 : dump_printf_loc (metadata, user_loc, "\tstmt %u ---\n", i);
3169 : : else
3170 : : {
3171 : 85819 : dump_printf_loc (metadata, user_loc, "\t{ ");
3172 : 273032 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (node), i, op)
3173 : 101394 : dump_printf (metadata, "%T%s ", op,
3174 : 101394 : i < SLP_TREE_SCALAR_OPS (node).length () - 1 ? "," : "");
3175 : 85819 : dump_printf (metadata, "}\n");
3176 : : }
3177 : 408151 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
3178 : : {
3179 : 55535 : dump_printf_loc (metadata, user_loc, "\tload permutation {");
3180 : 181648 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), i, j)
3181 : 70578 : dump_printf (dump_kind, " %u", j);
3182 : 55535 : dump_printf (dump_kind, " }\n");
3183 : : }
3184 : 408151 : if (SLP_TREE_LANE_PERMUTATION (node).exists ())
3185 : : {
3186 : 12991 : dump_printf_loc (metadata, user_loc, "\tlane permutation {");
3187 : 62085 : for (i = 0; i < SLP_TREE_LANE_PERMUTATION (node).length (); ++i)
3188 : 36103 : dump_printf (dump_kind, " %u[%u]",
3189 : 36103 : SLP_TREE_LANE_PERMUTATION (node)[i].first,
3190 : 36103 : SLP_TREE_LANE_PERMUTATION (node)[i].second);
3191 : 12991 : dump_printf (dump_kind, " }%s\n",
3192 : 12991 : node->ldst_lanes ? " (load-lanes)" : "");
3193 : : }
3194 : 408151 : if (SLP_TREE_CHILDREN (node).is_empty ())
3195 : 154904 : return;
3196 : 253247 : dump_printf_loc (metadata, user_loc, "\tchildren");
3197 : 923907 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3198 : 417413 : dump_printf (dump_kind, " %p", (void *)child);
3199 : 253247 : dump_printf (dump_kind, "%s\n",
3200 : 253247 : node->ldst_lanes && !SLP_TREE_LANE_PERMUTATION (node).exists ()
3201 : : ? " (store-lanes)" : "");
3202 : : }
3203 : :
3204 : : DEBUG_FUNCTION void
3205 : 0 : debug (slp_tree node)
3206 : : {
3207 : 0 : debug_dump_context ctx;
3208 : 0 : vect_print_slp_tree (MSG_NOTE,
3209 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3210 : : node);
3211 : 0 : }
3212 : :
3213 : : /* Recursive helper for the dot producer below. */
3214 : :
3215 : : static void
3216 : 0 : dot_slp_tree (FILE *f, slp_tree node, hash_set<slp_tree> &visited)
3217 : : {
3218 : 0 : if (visited.add (node))
3219 : : return;
3220 : :
3221 : 0 : fprintf (f, "\"%p\" [label=\"", (void *)node);
3222 : 0 : vect_print_slp_tree (MSG_NOTE,
3223 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3224 : : node);
3225 : 0 : fprintf (f, "\"];\n");
3226 : :
3227 : :
3228 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3229 : 0 : fprintf (f, "\"%p\" -> \"%p\";", (void *)node, (void *)child);
3230 : :
3231 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
3232 : 0 : if (child)
3233 : 0 : dot_slp_tree (f, child, visited);
3234 : : }
3235 : :
3236 : : DEBUG_FUNCTION void
3237 : 0 : dot_slp_tree (const char *fname, slp_tree node)
3238 : : {
3239 : 0 : FILE *f = fopen (fname, "w");
3240 : 0 : fprintf (f, "digraph {\n");
3241 : 0 : fflush (f);
3242 : 0 : {
3243 : 0 : debug_dump_context ctx (f);
3244 : 0 : hash_set<slp_tree> visited;
3245 : 0 : dot_slp_tree (f, node, visited);
3246 : 0 : }
3247 : 0 : fflush (f);
3248 : 0 : fprintf (f, "}\n");
3249 : 0 : fclose (f);
3250 : 0 : }
3251 : :
3252 : : DEBUG_FUNCTION void
3253 : 0 : dot_slp_tree (const char *fname, const vec<slp_instance> &slp_instances)
3254 : : {
3255 : 0 : FILE *f = fopen (fname, "w");
3256 : 0 : fprintf (f, "digraph {\n");
3257 : 0 : fflush (f);
3258 : 0 : {
3259 : 0 : debug_dump_context ctx (f);
3260 : 0 : hash_set<slp_tree> visited;
3261 : 0 : for (auto inst : slp_instances)
3262 : 0 : dot_slp_tree (f, SLP_INSTANCE_TREE (inst), visited);
3263 : 0 : }
3264 : 0 : fflush (f);
3265 : 0 : fprintf (f, "}\n");
3266 : 0 : fclose (f);
3267 : 0 : }
3268 : :
3269 : : /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
3270 : :
3271 : : static void
3272 : 442623 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3273 : : slp_tree node, hash_set<slp_tree> &visited)
3274 : : {
3275 : 442623 : unsigned i;
3276 : 442623 : slp_tree child;
3277 : :
3278 : 442623 : if (visited.add (node))
3279 : 442623 : return;
3280 : :
3281 : 407871 : vect_print_slp_tree (dump_kind, loc, node);
3282 : :
3283 : 1232852 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3284 : 417110 : if (child)
3285 : 370014 : vect_print_slp_graph (dump_kind, loc, child, visited);
3286 : : }
3287 : :
3288 : : static void
3289 : 44590 : vect_print_slp_graph (dump_flags_t dump_kind, dump_location_t loc,
3290 : : slp_tree entry)
3291 : : {
3292 : 44590 : hash_set<slp_tree> visited;
3293 : 44590 : vect_print_slp_graph (dump_kind, loc, entry, visited);
3294 : 44590 : }
3295 : :
3296 : : DEBUG_FUNCTION void
3297 : 0 : debug (slp_instance instance)
3298 : : {
3299 : 0 : debug_dump_context ctx;
3300 : 0 : vect_print_slp_graph (MSG_NOTE,
3301 : 0 : dump_location_t::from_location_t (UNKNOWN_LOCATION),
3302 : : SLP_INSTANCE_TREE (instance));
3303 : 0 : }
3304 : :
3305 : : /* Mark the tree rooted at NODE with PURE_SLP. */
3306 : :
3307 : : static void
3308 : 5468111 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node,
3309 : : hash_set<slp_tree> &visited)
3310 : : {
3311 : 5468111 : int i;
3312 : 5468111 : stmt_vec_info stmt_info;
3313 : 5468111 : slp_tree child;
3314 : :
3315 : 5468111 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3316 : : return;
3317 : :
3318 : 3765649 : if (visited.add (node))
3319 : : return;
3320 : :
3321 : 9193737 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3322 : 5657056 : if (stmt_info)
3323 : : {
3324 : 5605945 : STMT_SLP_TYPE (stmt_info) = pure_slp;
3325 : : /* ??? For .MASK_LOAD and .MASK_STORE detected as load/store-lanes
3326 : : when there is the mask_conversion pattern applied we have lost the
3327 : : alternate lanes of the uniform mask which nevertheless
3328 : : have separate pattern defs. To not confuse hybrid
3329 : : analysis we mark those as covered as well here. */
3330 : 5605945 : if (node->ldst_lanes)
3331 : 5657056 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
3332 : 0 : if (gimple_call_internal_p (call, IFN_MASK_LOAD)
3333 : 0 : || gimple_call_internal_p (call, IFN_MASK_STORE))
3334 : : {
3335 : 0 : tree mask = gimple_call_arg (call,
3336 : : internal_fn_mask_index
3337 : 0 : (gimple_call_internal_fn (call)));
3338 : 0 : if (TREE_CODE (mask) == SSA_NAME)
3339 : 0 : if (stmt_vec_info mask_info = vinfo->lookup_def (mask))
3340 : : {
3341 : 0 : mask_info = vect_stmt_to_vectorize (mask_info);
3342 : 0 : STMT_SLP_TYPE (mask_info) = pure_slp;
3343 : : }
3344 : : }
3345 : : }
3346 : :
3347 : 8268535 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3348 : 4731854 : if (child)
3349 : 3960913 : vect_mark_slp_stmts (vinfo, child, visited);
3350 : : }
3351 : :
3352 : : static void
3353 : 1507198 : vect_mark_slp_stmts (vec_info *vinfo, slp_tree node)
3354 : : {
3355 : 1507198 : hash_set<slp_tree> visited;
3356 : 1507198 : vect_mark_slp_stmts (vinfo, node, visited);
3357 : 1507198 : }
3358 : :
3359 : : /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
3360 : :
3361 : : static void
3362 : 2543872 : vect_mark_slp_stmts_relevant (slp_tree node, hash_set<slp_tree> &visited)
3363 : : {
3364 : 2543872 : int i;
3365 : 2543872 : stmt_vec_info stmt_info;
3366 : 2543872 : slp_tree child;
3367 : :
3368 : 2543872 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3369 : : return;
3370 : :
3371 : 1485694 : if (visited.add (node))
3372 : : return;
3373 : :
3374 : 4648996 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
3375 : 3263975 : if (stmt_info)
3376 : : {
3377 : 3263975 : gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
3378 : : || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
3379 : 3263975 : STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
3380 : : }
3381 : :
3382 : 3063504 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3383 : 1678483 : if (child)
3384 : 1678483 : vect_mark_slp_stmts_relevant (child, visited);
3385 : : }
3386 : :
3387 : : static void
3388 : 865389 : vect_mark_slp_stmts_relevant (slp_tree node)
3389 : : {
3390 : 865389 : hash_set<slp_tree> visited;
3391 : 865389 : vect_mark_slp_stmts_relevant (node, visited);
3392 : 865389 : }
3393 : :
3394 : :
3395 : : /* Gather loads in the SLP graph NODE and populate the INST loads array. */
3396 : :
3397 : : static void
3398 : 9524893 : vect_gather_slp_loads (vec<slp_tree> &loads, slp_tree node,
3399 : : hash_set<slp_tree> &visited)
3400 : : {
3401 : 9524893 : if (!node || visited.add (node))
3402 : 1833705 : return;
3403 : :
3404 : 7691188 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3405 : : return;
3406 : :
3407 : 5406054 : if (SLP_TREE_CODE (node) != VEC_PERM_EXPR)
3408 : : {
3409 : 5246014 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
3410 : 5246014 : if (STMT_VINFO_DATA_REF (stmt_info)
3411 : 2094508 : && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
3412 : 993126 : loads.safe_push (node);
3413 : : }
3414 : :
3415 : : unsigned i;
3416 : : slp_tree child;
3417 : 12777086 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3418 : 7371032 : vect_gather_slp_loads (loads, child, visited);
3419 : : }
3420 : :
3421 : :
3422 : : /* Find the last store in SLP INSTANCE. */
3423 : :
3424 : : stmt_vec_info
3425 : 2951257 : vect_find_last_scalar_stmt_in_slp (slp_tree node)
3426 : : {
3427 : 2951257 : stmt_vec_info last = NULL;
3428 : 2951257 : stmt_vec_info stmt_vinfo;
3429 : :
3430 : 10634430 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3431 : 7683173 : if (stmt_vinfo)
3432 : : {
3433 : 7683173 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3434 : 7683173 : last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
3435 : : }
3436 : :
3437 : 2951257 : return last;
3438 : : }
3439 : :
3440 : : /* Find the first stmt in NODE. */
3441 : :
3442 : : stmt_vec_info
3443 : 536886 : vect_find_first_scalar_stmt_in_slp (slp_tree node)
3444 : : {
3445 : 536886 : stmt_vec_info first = NULL;
3446 : 536886 : stmt_vec_info stmt_vinfo;
3447 : :
3448 : 1816289 : for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
3449 : 1279403 : if (stmt_vinfo)
3450 : : {
3451 : 1276780 : stmt_vinfo = vect_orig_stmt (stmt_vinfo);
3452 : 1276780 : if (!first
3453 : 1276780 : || get_later_stmt (stmt_vinfo, first) == first)
3454 : : first = stmt_vinfo;
3455 : : }
3456 : :
3457 : 536886 : return first;
3458 : : }
3459 : :
3460 : : /* Splits a group of stores, currently beginning at FIRST_VINFO, into
3461 : : two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
3462 : : (also containing the first GROUP1_SIZE stmts, since stores are
3463 : : consecutive), the second containing the remainder.
3464 : : Return the first stmt in the second group. */
3465 : :
3466 : : static stmt_vec_info
3467 : 159675 : vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
3468 : : {
3469 : 159675 : gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
3470 : 159675 : gcc_assert (group1_size > 0);
3471 : 159675 : int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
3472 : 159675 : gcc_assert (group2_size > 0);
3473 : 159675 : DR_GROUP_SIZE (first_vinfo) = group1_size;
3474 : :
3475 : 159675 : stmt_vec_info stmt_info = first_vinfo;
3476 : 534162 : for (unsigned i = group1_size; i > 1; i--)
3477 : : {
3478 : 374487 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
3479 : 374487 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3480 : : }
3481 : : /* STMT is now the last element of the first group. */
3482 : 159675 : stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
3483 : 159675 : DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
3484 : :
3485 : 159675 : DR_GROUP_SIZE (group2) = group2_size;
3486 : 444126 : for (stmt_info = group2; stmt_info;
3487 : 284451 : stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
3488 : : {
3489 : 284451 : DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
3490 : 284451 : gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
3491 : : }
3492 : :
3493 : : /* For the second group, the DR_GROUP_GAP is that before the original group,
3494 : : plus skipping over the first vector. */
3495 : 159675 : DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
3496 : :
3497 : : /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
3498 : 159675 : DR_GROUP_GAP (first_vinfo) += group2_size;
3499 : :
3500 : 159675 : if (dump_enabled_p ())
3501 : 59 : dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
3502 : : group1_size, group2_size);
3503 : :
3504 : 159675 : return group2;
3505 : : }
3506 : :
3507 : : /* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
3508 : : statements and a vector of NUNITS elements. */
3509 : :
3510 : : static poly_uint64
3511 : 3448896 : calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
3512 : : {
3513 : 3448896 : return exact_div (common_multiple (nunits, group_size), group_size);
3514 : : }
3515 : :
3516 : : /* Helper that checks to see if a node is a load node. */
3517 : :
3518 : : static inline bool
3519 : 91 : vect_is_slp_load_node (slp_tree root)
3520 : : {
3521 : 91 : return (SLP_TREE_CODE (root) != VEC_PERM_EXPR
3522 : 79 : && SLP_TREE_DEF_TYPE (root) == vect_internal_def
3523 : 67 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_REPRESENTATIVE (root))
3524 : 131 : && DR_IS_READ (STMT_VINFO_DATA_REF (SLP_TREE_REPRESENTATIVE (root))));
3525 : : }
3526 : :
3527 : :
3528 : : /* Helper function of optimize_load_redistribution that performs the operation
3529 : : recursively. */
3530 : :
3531 : : static slp_tree
3532 : 14091 : optimize_load_redistribution_1 (scalar_stmts_to_slp_tree_map_t *bst_map,
3533 : : vec_info *vinfo, unsigned int group_size,
3534 : : hash_map<slp_tree, slp_tree> *load_map,
3535 : : slp_tree root)
3536 : : {
3537 : 14091 : if (slp_tree *leader = load_map->get (root))
3538 : 844 : return *leader;
3539 : :
3540 : 13247 : slp_tree node;
3541 : 13247 : unsigned i;
3542 : :
3543 : : /* For now, we don't know anything about externals so do not do anything. */
3544 : 13247 : if (!root || SLP_TREE_DEF_TYPE (root) != vect_internal_def)
3545 : : return NULL;
3546 : 7957 : else if (SLP_TREE_CODE (root) == VEC_PERM_EXPR)
3547 : : {
3548 : : /* First convert this node into a load node and add it to the leaves
3549 : : list and flatten the permute from a lane to a load one. If it's
3550 : : unneeded it will be elided later. */
3551 : 71 : vec<stmt_vec_info> stmts;
3552 : 71 : stmts.create (SLP_TREE_LANES (root));
3553 : 71 : lane_permutation_t lane_perm = SLP_TREE_LANE_PERMUTATION (root);
3554 : 111 : for (unsigned j = 0; j < lane_perm.length (); j++)
3555 : : {
3556 : 91 : std::pair<unsigned, unsigned> perm = lane_perm[j];
3557 : 91 : node = SLP_TREE_CHILDREN (root)[perm.first];
3558 : :
3559 : 91 : if (!vect_is_slp_load_node (node)
3560 : 91 : || SLP_TREE_CHILDREN (node).exists ())
3561 : : {
3562 : 51 : stmts.release ();
3563 : 51 : goto next;
3564 : : }
3565 : :
3566 : 40 : stmts.quick_push (SLP_TREE_SCALAR_STMTS (node)[perm.second]);
3567 : : }
3568 : :
3569 : 20 : if (dump_enabled_p ())
3570 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
3571 : : "converting stmts on permute node %p\n",
3572 : : (void *) root);
3573 : :
3574 : 20 : bool *matches = XALLOCAVEC (bool, group_size);
3575 : 20 : poly_uint64 max_nunits = 1;
3576 : 20 : unsigned tree_size = 0, limit = 1;
3577 : 20 : node = vect_build_slp_tree (vinfo, stmts, group_size, &max_nunits,
3578 : : matches, &limit, &tree_size, bst_map);
3579 : 20 : if (!node)
3580 : 0 : stmts.release ();
3581 : :
3582 : 20 : load_map->put (root, node);
3583 : 20 : return node;
3584 : : }
3585 : :
3586 : 7886 : next:
3587 : 7937 : load_map->put (root, NULL);
3588 : :
3589 : 19323 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3590 : : {
3591 : 11386 : slp_tree value
3592 : 11386 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3593 : : node);
3594 : 11386 : if (value)
3595 : : {
3596 : 20 : SLP_TREE_REF_COUNT (value)++;
3597 : 20 : SLP_TREE_CHILDREN (root)[i] = value;
3598 : : /* ??? We know the original leafs of the replaced nodes will
3599 : : be referenced by bst_map, only the permutes created by
3600 : : pattern matching are not. */
3601 : 20 : if (SLP_TREE_REF_COUNT (node) == 1)
3602 : 20 : load_map->remove (node);
3603 : 20 : vect_free_slp_tree (node);
3604 : : }
3605 : : }
3606 : :
3607 : : return NULL;
3608 : : }
3609 : :
3610 : : /* Temporary workaround for loads not being CSEd during SLP build. This
3611 : : function will traverse the SLP tree rooted in ROOT for INSTANCE and find
3612 : : VEC_PERM nodes that blend vectors from multiple nodes that all read from the
3613 : : same DR such that the final operation is equal to a permuted load. Such
3614 : : NODES are then directly converted into LOADS themselves. The nodes are
3615 : : CSEd using BST_MAP. */
3616 : :
3617 : : static void
3618 : 2205 : optimize_load_redistribution (scalar_stmts_to_slp_tree_map_t *bst_map,
3619 : : vec_info *vinfo, unsigned int group_size,
3620 : : hash_map<slp_tree, slp_tree> *load_map,
3621 : : slp_tree root)
3622 : : {
3623 : 2205 : slp_tree node;
3624 : 2205 : unsigned i;
3625 : :
3626 : 4910 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (root), i , node)
3627 : : {
3628 : 2705 : slp_tree value
3629 : 2705 : = optimize_load_redistribution_1 (bst_map, vinfo, group_size, load_map,
3630 : : node);
3631 : 2705 : if (value)
3632 : : {
3633 : 0 : SLP_TREE_REF_COUNT (value)++;
3634 : 0 : SLP_TREE_CHILDREN (root)[i] = value;
3635 : : /* ??? We know the original leafs of the replaced nodes will
3636 : : be referenced by bst_map, only the permutes created by
3637 : : pattern matching are not. */
3638 : 0 : if (SLP_TREE_REF_COUNT (node) == 1)
3639 : 0 : load_map->remove (node);
3640 : 0 : vect_free_slp_tree (node);
3641 : : }
3642 : : }
3643 : 2205 : }
3644 : :
3645 : : /* Helper function of vect_match_slp_patterns.
3646 : :
3647 : : Attempts to match patterns against the slp tree rooted in REF_NODE using
3648 : : VINFO. Patterns are matched in post-order traversal.
3649 : :
3650 : : If matching is successful the value in REF_NODE is updated and returned, if
3651 : : not then it is returned unchanged. */
3652 : :
3653 : : static bool
3654 : 5514890 : vect_match_slp_patterns_2 (slp_tree *ref_node, vec_info *vinfo,
3655 : : slp_tree_to_load_perm_map_t *perm_cache,
3656 : : slp_compat_nodes_map_t *compat_cache,
3657 : : hash_set<slp_tree> *visited)
3658 : : {
3659 : 5514890 : unsigned i;
3660 : 5514890 : slp_tree node = *ref_node;
3661 : 5514890 : bool found_p = false;
3662 : 5514890 : if (!node || visited->add (node))
3663 : 879334 : return false;
3664 : :
3665 : : slp_tree child;
3666 : 8633813 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3667 : 3998257 : found_p |= vect_match_slp_patterns_2 (&SLP_TREE_CHILDREN (node)[i],
3668 : : vinfo, perm_cache, compat_cache,
3669 : : visited);
3670 : :
3671 : 13906668 : for (unsigned x = 0; x < num__slp_patterns; x++)
3672 : : {
3673 : 9271112 : vect_pattern *pattern
3674 : 9271112 : = slp_patterns[x] (perm_cache, compat_cache, ref_node);
3675 : 9271112 : if (pattern)
3676 : : {
3677 : 833 : pattern->build (vinfo);
3678 : 833 : delete pattern;
3679 : 833 : found_p = true;
3680 : : }
3681 : : }
3682 : :
3683 : : return found_p;
3684 : : }
3685 : :
3686 : : /* Applies pattern matching to the given SLP tree rooted in REF_NODE using
3687 : : vec_info VINFO.
3688 : :
3689 : : The modified tree is returned. Patterns are tried in order and multiple
3690 : : patterns may match. */
3691 : :
3692 : : static bool
3693 : 1516633 : vect_match_slp_patterns (slp_instance instance, vec_info *vinfo,
3694 : : hash_set<slp_tree> *visited,
3695 : : slp_tree_to_load_perm_map_t *perm_cache,
3696 : : slp_compat_nodes_map_t *compat_cache)
3697 : : {
3698 : 1516633 : DUMP_VECT_SCOPE ("vect_match_slp_patterns");
3699 : 1516633 : slp_tree *ref_node = &SLP_INSTANCE_TREE (instance);
3700 : :
3701 : 1516633 : if (dump_enabled_p ())
3702 : 29116 : dump_printf_loc (MSG_NOTE, vect_location,
3703 : : "Analyzing SLP tree %p for patterns\n",
3704 : 29116 : (void *) SLP_INSTANCE_TREE (instance));
3705 : :
3706 : 1516633 : return vect_match_slp_patterns_2 (ref_node, vinfo, perm_cache, compat_cache,
3707 : 1516633 : visited);
3708 : : }
3709 : :
3710 : : /* STMT_INFO is a store group of size GROUP_SIZE that we are considering
3711 : : vectorizing with VECTYPE that might be NULL. MASKED_P indicates whether
3712 : : the stores are masked.
3713 : : Return true if we could use IFN_STORE_LANES instead and if that appears
3714 : : to be the better approach. */
3715 : :
3716 : : static bool
3717 : 3784 : vect_slp_prefer_store_lanes_p (vec_info *vinfo, stmt_vec_info stmt_info,
3718 : : tree vectype, bool masked_p,
3719 : : unsigned int group_size,
3720 : : unsigned int new_group_size)
3721 : : {
3722 : 3784 : if (!vectype)
3723 : : {
3724 : 3784 : tree scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
3725 : 3784 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
3726 : : }
3727 : 3784 : if (!vectype)
3728 : : return false;
3729 : : /* Allow the split if one of the two new groups would operate on full
3730 : : vectors *within* rather than across one scalar loop iteration.
3731 : : This is purely a heuristic, but it should work well for group
3732 : : sizes of 3 and 4, where the possible splits are:
3733 : :
3734 : : 3->2+1: OK if the vector has exactly two elements
3735 : : 4->2+2: Likewise
3736 : : 4->3+1: Less clear-cut. */
3737 : 3784 : if (multiple_p (group_size - new_group_size, TYPE_VECTOR_SUBPARTS (vectype))
3738 : 2383 : || multiple_p (new_group_size, TYPE_VECTOR_SUBPARTS (vectype)))
3739 : 1446 : return false;
3740 : 2338 : return vect_store_lanes_supported (vectype, group_size, masked_p) != IFN_LAST;
3741 : : }
3742 : :
3743 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
3744 : : vect_build_slp_tree to build a tree of packed stmts if possible.
3745 : : Return FALSE if it's impossible to SLP any stmt in the loop. */
3746 : :
3747 : : static bool
3748 : : vect_analyze_slp_instance (vec_info *vinfo,
3749 : : scalar_stmts_to_slp_tree_map_t *bst_map,
3750 : : stmt_vec_info stmt_info, slp_instance_kind kind,
3751 : : unsigned max_tree_size, unsigned *limit,
3752 : : bool force_single_lane);
3753 : :
3754 : : /* Build an interleaving scheme for the store sources RHS_NODES from
3755 : : SCALAR_STMTS. */
3756 : :
3757 : : static slp_tree
3758 : 5206 : vect_build_slp_store_interleaving (vec<slp_tree> &rhs_nodes,
3759 : : vec<stmt_vec_info> &scalar_stmts,
3760 : : poly_uint64 max_nunits)
3761 : : {
3762 : 5206 : unsigned int group_size = scalar_stmts.length ();
3763 : 10412 : slp_tree node = vect_create_new_slp_node (scalar_stmts,
3764 : 5206 : SLP_TREE_CHILDREN
3765 : : (rhs_nodes[0]).length ());
3766 : 5206 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
3767 : 5206 : node->max_nunits = max_nunits;
3768 : 5206 : for (unsigned l = 0;
3769 : 10437 : l < SLP_TREE_CHILDREN (rhs_nodes[0]).length (); ++l)
3770 : : {
3771 : : /* And a permute merging all RHS SLP trees. */
3772 : 5231 : slp_tree perm = vect_create_new_slp_node (rhs_nodes.length (),
3773 : 5231 : VEC_PERM_EXPR);
3774 : 5231 : SLP_TREE_CHILDREN (node).quick_push (perm);
3775 : 5231 : SLP_TREE_LANE_PERMUTATION (perm).create (group_size);
3776 : 5231 : SLP_TREE_VECTYPE (perm) = SLP_TREE_VECTYPE (node);
3777 : 5231 : perm->max_nunits = max_nunits;
3778 : 5231 : SLP_TREE_LANES (perm) = group_size;
3779 : : /* ??? We should set this NULL but that's not expected. */
3780 : 5231 : SLP_TREE_REPRESENTATIVE (perm)
3781 : 5231 : = SLP_TREE_REPRESENTATIVE (SLP_TREE_CHILDREN (rhs_nodes[0])[l]);
3782 : 20403 : for (unsigned j = 0; j < rhs_nodes.length (); ++j)
3783 : : {
3784 : 15172 : SLP_TREE_CHILDREN (perm)
3785 : 15172 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[l]);
3786 : 15172 : SLP_TREE_CHILDREN (rhs_nodes[j])[l]->refcnt++;
3787 : 15172 : for (unsigned k = 0;
3788 : 32017 : k < SLP_TREE_SCALAR_STMTS (rhs_nodes[j]).length (); ++k)
3789 : : {
3790 : : /* ??? We should populate SLP_TREE_SCALAR_STMTS
3791 : : or SLP_TREE_SCALAR_OPS but then we might have
3792 : : a mix of both in our children. */
3793 : 16845 : SLP_TREE_LANE_PERMUTATION (perm)
3794 : 16845 : .quick_push (std::make_pair (j, k));
3795 : : }
3796 : : }
3797 : :
3798 : : /* Now we have a single permute node but we cannot code-generate
3799 : : the case with more than two inputs.
3800 : : Perform pairwise reduction, reducing the two inputs
3801 : : with the least number of lanes to one and then repeat until
3802 : : we end up with two inputs. That scheme makes sure we end
3803 : : up with permutes satisfying the restriction of requiring at
3804 : : most two vector inputs to produce a single vector output
3805 : : when the number of lanes is even. */
3806 : 9941 : while (SLP_TREE_CHILDREN (perm).length () > 2)
3807 : : {
3808 : : /* When we have three equal sized groups left the pairwise
3809 : : reduction does not result in a scheme that avoids using
3810 : : three vectors. Instead merge the first two groups
3811 : : to the final size with do-not-care elements (chosen
3812 : : from the first group) and then merge with the third.
3813 : : { A0, B0, x, A1, B1, x, ... }
3814 : : -> { A0, B0, C0, A1, B1, C1, ... }
3815 : : This handles group size of three (and at least
3816 : : power-of-two multiples of that). */
3817 : 4710 : if (SLP_TREE_CHILDREN (perm).length () == 3
3818 : 2337 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3819 : 2337 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[1]))
3820 : 4710 : && (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[0])
3821 : 1726 : == SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[2])))
3822 : : {
3823 : 1564 : int ai = 0;
3824 : 1564 : int bi = 1;
3825 : 1564 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
3826 : 1564 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
3827 : 1564 : unsigned n = SLP_TREE_LANES (perm);
3828 : :
3829 : 1564 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
3830 : 1564 : SLP_TREE_LANES (permab) = n;
3831 : 1564 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
3832 : 1564 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
3833 : 1564 : permab->max_nunits = max_nunits;
3834 : : /* ??? Should be NULL but that's not expected. */
3835 : 1564 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
3836 : 1564 : SLP_TREE_CHILDREN (permab).quick_push (a);
3837 : 3137 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3838 : 1573 : SLP_TREE_LANE_PERMUTATION (permab)
3839 : 1573 : .quick_push (std::make_pair (0, k));
3840 : 1564 : SLP_TREE_CHILDREN (permab).quick_push (b);
3841 : 3137 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
3842 : 1573 : SLP_TREE_LANE_PERMUTATION (permab)
3843 : 1573 : .quick_push (std::make_pair (1, k));
3844 : : /* Push the do-not-care lanes. */
3845 : 3137 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3846 : 1573 : SLP_TREE_LANE_PERMUTATION (permab)
3847 : 1573 : .quick_push (std::make_pair (0, k));
3848 : :
3849 : : /* Put the merged node into 'perm', in place of a. */
3850 : 1564 : SLP_TREE_CHILDREN (perm)[ai] = permab;
3851 : : /* Adjust the references to b in the permutation
3852 : : of perm and to the later children which we'll
3853 : : remove. */
3854 : 6283 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
3855 : : {
3856 : 4719 : std::pair<unsigned, unsigned> &p
3857 : 4719 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
3858 : 4719 : if (p.first == (unsigned) bi)
3859 : : {
3860 : 1573 : p.first = ai;
3861 : 1573 : p.second += SLP_TREE_LANES (a);
3862 : : }
3863 : 3146 : else if (p.first > (unsigned) bi)
3864 : 1573 : p.first--;
3865 : : }
3866 : 1564 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
3867 : 1564 : break;
3868 : : }
3869 : :
3870 : : /* Pick the two nodes with the least number of lanes,
3871 : : prefer the earliest candidate and maintain ai < bi. */
3872 : : int ai = -1;
3873 : : int bi = -1;
3874 : 26557 : for (unsigned ci = 0; ci < SLP_TREE_CHILDREN (perm).length (); ++ci)
3875 : : {
3876 : 23411 : if (ai == -1)
3877 : 3146 : ai = ci;
3878 : 20265 : else if (bi == -1)
3879 : 3146 : bi = ci;
3880 : 17119 : else if ((SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
3881 : 17119 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai]))
3882 : 17119 : || (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ci])
3883 : 13897 : < SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi])))
3884 : : {
3885 : 7044 : if (SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[ai])
3886 : 3522 : <= SLP_TREE_LANES (SLP_TREE_CHILDREN (perm)[bi]))
3887 : 1610 : bi = ci;
3888 : : else
3889 : : {
3890 : 1912 : ai = bi;
3891 : 1912 : bi = ci;
3892 : : }
3893 : : }
3894 : : }
3895 : :
3896 : : /* Produce a merge of nodes ai and bi. */
3897 : 3146 : slp_tree a = SLP_TREE_CHILDREN (perm)[ai];
3898 : 3146 : slp_tree b = SLP_TREE_CHILDREN (perm)[bi];
3899 : 3146 : unsigned n = SLP_TREE_LANES (a) + SLP_TREE_LANES (b);
3900 : 3146 : slp_tree permab = vect_create_new_slp_node (2, VEC_PERM_EXPR);
3901 : 3146 : SLP_TREE_LANES (permab) = n;
3902 : 3146 : SLP_TREE_LANE_PERMUTATION (permab).create (n);
3903 : 3146 : SLP_TREE_VECTYPE (permab) = SLP_TREE_VECTYPE (perm);
3904 : 3146 : permab->max_nunits = max_nunits;
3905 : : /* ??? Should be NULL but that's not expected. */
3906 : 3146 : SLP_TREE_REPRESENTATIVE (permab) = SLP_TREE_REPRESENTATIVE (perm);
3907 : 3146 : SLP_TREE_CHILDREN (permab).quick_push (a);
3908 : 8135 : for (unsigned k = 0; k < SLP_TREE_LANES (a); ++k)
3909 : 4989 : SLP_TREE_LANE_PERMUTATION (permab)
3910 : 4989 : .quick_push (std::make_pair (0, k));
3911 : 3146 : SLP_TREE_CHILDREN (permab).quick_push (b);
3912 : 7758 : for (unsigned k = 0; k < SLP_TREE_LANES (b); ++k)
3913 : 4612 : SLP_TREE_LANE_PERMUTATION (permab)
3914 : 4612 : .quick_push (std::make_pair (1, k));
3915 : :
3916 : : /* Put the merged node into 'perm', in place of a. */
3917 : 3146 : SLP_TREE_CHILDREN (perm)[ai] = permab;
3918 : : /* Adjust the references to b in the permutation
3919 : : of perm and to the later children which we'll
3920 : : remove. */
3921 : 41938 : for (unsigned k = 0; k < SLP_TREE_LANES (perm); ++k)
3922 : : {
3923 : 38792 : std::pair<unsigned, unsigned> &p
3924 : 38792 : = SLP_TREE_LANE_PERMUTATION (perm)[k];
3925 : 38792 : if (p.first == (unsigned) bi)
3926 : : {
3927 : 4612 : p.first = ai;
3928 : 4612 : p.second += SLP_TREE_LANES (a);
3929 : : }
3930 : 34180 : else if (p.first > (unsigned) bi)
3931 : 14059 : p.first--;
3932 : : }
3933 : 3146 : SLP_TREE_CHILDREN (perm).ordered_remove (bi);
3934 : : }
3935 : : }
3936 : :
3937 : 5206 : return node;
3938 : : }
3939 : :
3940 : : /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
3941 : : of KIND. Return true if successful. */
3942 : :
3943 : : static bool
3944 : 2900623 : vect_build_slp_instance (vec_info *vinfo,
3945 : : slp_instance_kind kind,
3946 : : vec<stmt_vec_info> &scalar_stmts,
3947 : : vec<stmt_vec_info> &root_stmt_infos,
3948 : : vec<tree> &remain,
3949 : : unsigned max_tree_size, unsigned *limit,
3950 : : scalar_stmts_to_slp_tree_map_t *bst_map,
3951 : : /* ??? We need stmt_info for group splitting. */
3952 : : stmt_vec_info stmt_info_,
3953 : : bool force_single_lane)
3954 : : {
3955 : : /* If there's no budget left bail out early. */
3956 : 2900623 : if (*limit == 0)
3957 : : return false;
3958 : :
3959 : 2873289 : if (kind == slp_inst_kind_ctor)
3960 : : {
3961 : 11600 : if (dump_enabled_p ())
3962 : 54 : dump_printf_loc (MSG_NOTE, vect_location,
3963 : : "Analyzing vectorizable constructor: %G\n",
3964 : 27 : root_stmt_infos[0]->stmt);
3965 : : }
3966 : 2861689 : else if (kind == slp_inst_kind_gcond)
3967 : : {
3968 : 143682 : if (dump_enabled_p ())
3969 : 4078 : dump_printf_loc (MSG_NOTE, vect_location,
3970 : : "Analyzing vectorizable control flow: %G",
3971 : 2039 : root_stmt_infos[0]->stmt);
3972 : : }
3973 : :
3974 : 2873289 : if (dump_enabled_p ())
3975 : : {
3976 : 32121 : dump_printf_loc (MSG_NOTE, vect_location,
3977 : : "Starting SLP discovery for\n");
3978 : 82252 : for (unsigned i = 0; i < scalar_stmts.length (); ++i)
3979 : 100262 : dump_printf_loc (MSG_NOTE, vect_location,
3980 : 50131 : " %G", scalar_stmts[i]->stmt);
3981 : : }
3982 : :
3983 : : /* Build the tree for the SLP instance. */
3984 : 2873289 : unsigned int group_size = scalar_stmts.length ();
3985 : 2873289 : bool *matches = XALLOCAVEC (bool, group_size);
3986 : 2873289 : poly_uint64 max_nunits = 1;
3987 : 2873289 : unsigned tree_size = 0;
3988 : 2873289 : unsigned i;
3989 : :
3990 : 2873289 : slp_tree node = NULL;
3991 : 2873289 : if (group_size > 1 && force_single_lane)
3992 : : {
3993 : 1416 : matches[0] = true;
3994 : 1416 : matches[1] = false;
3995 : : }
3996 : : else
3997 : 2871873 : node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
3998 : : &max_nunits, matches, limit,
3999 : : &tree_size, bst_map);
4000 : 2873289 : if (node != NULL)
4001 : : {
4002 : : /* Calculate the unrolling factor based on the smallest type. */
4003 : 1511427 : poly_uint64 unrolling_factor
4004 : 1511427 : = calculate_unrolling_factor (max_nunits, group_size);
4005 : :
4006 : 1511427 : if (maybe_ne (unrolling_factor, 1U)
4007 : 1511427 : && is_a <bb_vec_info> (vinfo))
4008 : : {
4009 : 0 : unsigned HOST_WIDE_INT const_max_nunits;
4010 : 0 : if (!max_nunits.is_constant (&const_max_nunits)
4011 : 0 : || const_max_nunits > group_size)
4012 : : {
4013 : 0 : if (dump_enabled_p ())
4014 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4015 : : "Build SLP failed: store group "
4016 : : "size not a multiple of the vector size "
4017 : : "in basic block SLP\n");
4018 : 0 : vect_free_slp_tree (node);
4019 : 0 : return false;
4020 : : }
4021 : : /* Fatal mismatch. */
4022 : 0 : if (dump_enabled_p ())
4023 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
4024 : : "SLP discovery succeeded but node needs "
4025 : : "splitting\n");
4026 : 0 : memset (matches, true, group_size);
4027 : 0 : matches[group_size / const_max_nunits * const_max_nunits] = false;
4028 : 0 : vect_free_slp_tree (node);
4029 : : }
4030 : : else
4031 : : {
4032 : : /* Create a new SLP instance. */
4033 : 1511427 : slp_instance new_instance = XNEW (class _slp_instance);
4034 : 1511427 : SLP_INSTANCE_TREE (new_instance) = node;
4035 : 1511427 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4036 : 1511427 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4037 : 1511427 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4038 : 1511427 : SLP_INSTANCE_KIND (new_instance) = kind;
4039 : 1511427 : new_instance->reduc_phis = NULL;
4040 : 1511427 : new_instance->cost_vec = vNULL;
4041 : 1511427 : new_instance->subgraph_entries = vNULL;
4042 : :
4043 : 1511427 : if (dump_enabled_p ())
4044 : 28326 : dump_printf_loc (MSG_NOTE, vect_location,
4045 : : "SLP size %u vs. limit %u.\n",
4046 : : tree_size, max_tree_size);
4047 : :
4048 : : /* Fixup SLP reduction chains. */
4049 : 1511427 : if (kind == slp_inst_kind_reduc_chain)
4050 : : {
4051 : : /* If this is a reduction chain with a conversion in front
4052 : : amend the SLP tree with a node for that. */
4053 : 274 : gimple *scalar_def
4054 : 274 : = vect_orig_stmt (scalar_stmts[group_size - 1])->stmt;
4055 : 274 : if (STMT_VINFO_DEF_TYPE (scalar_stmts[0]) != vect_reduction_def)
4056 : : {
4057 : : /* Get at the conversion stmt - we know it's the single use
4058 : : of the last stmt of the reduction chain. */
4059 : 42 : use_operand_p use_p;
4060 : 42 : bool r = single_imm_use (gimple_assign_lhs (scalar_def),
4061 : : &use_p, &scalar_def);
4062 : 42 : gcc_assert (r);
4063 : 42 : stmt_vec_info next_info = vinfo->lookup_stmt (scalar_def);
4064 : 42 : next_info = vect_stmt_to_vectorize (next_info);
4065 : 42 : scalar_stmts = vNULL;
4066 : 42 : scalar_stmts.create (group_size);
4067 : 132 : for (unsigned i = 0; i < group_size; ++i)
4068 : 90 : scalar_stmts.quick_push (next_info);
4069 : 42 : slp_tree conv = vect_create_new_slp_node (scalar_stmts, 1);
4070 : 42 : SLP_TREE_VECTYPE (conv)
4071 : 42 : = get_vectype_for_scalar_type (vinfo,
4072 : 42 : TREE_TYPE
4073 : : (gimple_assign_lhs
4074 : : (scalar_def)),
4075 : : group_size);
4076 : 42 : SLP_TREE_CHILDREN (conv).quick_push (node);
4077 : 42 : SLP_INSTANCE_TREE (new_instance) = conv;
4078 : : /* We also have to fake this conversion stmt as SLP reduction
4079 : : group so we don't have to mess with too much code
4080 : : elsewhere. */
4081 : 42 : REDUC_GROUP_FIRST_ELEMENT (next_info) = next_info;
4082 : 42 : REDUC_GROUP_NEXT_ELEMENT (next_info) = NULL;
4083 : : }
4084 : : /* Fill the backedge child of the PHI SLP node. The
4085 : : general matching code cannot find it because the
4086 : : scalar code does not reflect how we vectorize the
4087 : : reduction. */
4088 : 274 : use_operand_p use_p;
4089 : 274 : imm_use_iterator imm_iter;
4090 : 274 : class loop *loop = LOOP_VINFO_LOOP (as_a <loop_vec_info> (vinfo));
4091 : 858 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter,
4092 : : gimple_get_lhs (scalar_def))
4093 : : /* There are exactly two non-debug uses, the reduction
4094 : : PHI and the loop-closed PHI node. */
4095 : 584 : if (!is_gimple_debug (USE_STMT (use_p))
4096 : 584 : && gimple_bb (USE_STMT (use_p)) == loop->header)
4097 : : {
4098 : 274 : auto_vec<stmt_vec_info, 64> phis (group_size);
4099 : 274 : stmt_vec_info phi_info
4100 : 274 : = vinfo->lookup_stmt (USE_STMT (use_p));
4101 : 1698 : for (unsigned i = 0; i < group_size; ++i)
4102 : 1424 : phis.quick_push (phi_info);
4103 : 274 : slp_tree *phi_node = bst_map->get (phis);
4104 : 274 : unsigned dest_idx = loop_latch_edge (loop)->dest_idx;
4105 : 548 : SLP_TREE_CHILDREN (*phi_node)[dest_idx]
4106 : 274 : = SLP_INSTANCE_TREE (new_instance);
4107 : 274 : SLP_INSTANCE_TREE (new_instance)->refcnt++;
4108 : 274 : }
4109 : : }
4110 : :
4111 : 1511427 : vinfo->slp_instances.safe_push (new_instance);
4112 : :
4113 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4114 : : the number of scalar stmts in the root in a few places.
4115 : : Verify that assumption holds. */
4116 : 3022854 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4117 : : .length () == group_size);
4118 : :
4119 : 1511427 : if (dump_enabled_p ())
4120 : : {
4121 : 28326 : dump_printf_loc (MSG_NOTE, vect_location,
4122 : : "Final SLP tree for instance %p:\n",
4123 : : (void *) new_instance);
4124 : 28326 : vect_print_slp_graph (MSG_NOTE, vect_location,
4125 : : SLP_INSTANCE_TREE (new_instance));
4126 : : }
4127 : :
4128 : 1511427 : return true;
4129 : : }
4130 : : }
4131 : : /* Failed to SLP. */
4132 : :
4133 : 1361862 : stmt_vec_info stmt_info = stmt_info_;
4134 : : /* Try to break the group up into pieces. */
4135 : 1361862 : if (*limit > 0 && kind == slp_inst_kind_store)
4136 : : {
4137 : : /* ??? We could delay all the actual splitting of store-groups
4138 : : until after SLP discovery of the original group completed.
4139 : : Then we can recurse to vect_build_slp_instance directly. */
4140 : 1050861 : for (i = 0; i < group_size; i++)
4141 : 1050861 : if (!matches[i])
4142 : : break;
4143 : :
4144 : : /* For basic block SLP, try to break the group up into multiples of
4145 : : a vector size. */
4146 : 335799 : if (is_a <bb_vec_info> (vinfo)
4147 : 335799 : && (i > 1 && i < group_size))
4148 : : {
4149 : : /* Free the allocated memory. */
4150 : 157533 : scalar_stmts.release ();
4151 : :
4152 : 157533 : tree scalar_type
4153 : 157533 : = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
4154 : 315066 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
4155 : 157533 : 1 << floor_log2 (i));
4156 : 157533 : unsigned HOST_WIDE_INT const_nunits;
4157 : 157533 : if (vectype
4158 : 157533 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits))
4159 : : {
4160 : : /* Split into two groups at the first vector boundary. */
4161 : 157533 : gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
4162 : 157533 : unsigned group1_size = i & ~(const_nunits - 1);
4163 : :
4164 : 157533 : if (dump_enabled_p ())
4165 : 57 : dump_printf_loc (MSG_NOTE, vect_location,
4166 : : "Splitting SLP group at stmt %u\n", i);
4167 : 157533 : stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
4168 : : group1_size);
4169 : 157533 : bool res = vect_analyze_slp_instance (vinfo, bst_map, stmt_info,
4170 : : kind, max_tree_size,
4171 : : limit, false);
4172 : : /* Split the rest at the failure point and possibly
4173 : : re-analyze the remaining matching part if it has
4174 : : at least two lanes. */
4175 : 157533 : if (group1_size < i
4176 : 5292 : && (i + 1 < group_size
4177 : 3174 : || i - group1_size > 1))
4178 : : {
4179 : 2142 : stmt_vec_info rest2 = rest;
4180 : 2142 : rest = vect_split_slp_store_group (rest, i - group1_size);
4181 : 2142 : if (i - group1_size > 1)
4182 : 55 : res |= vect_analyze_slp_instance (vinfo, bst_map, rest2,
4183 : : kind, max_tree_size,
4184 : : limit, false);
4185 : : }
4186 : : /* Re-analyze the non-matching tail if it has at least
4187 : : two lanes. */
4188 : 157533 : if (i + 1 < group_size)
4189 : 23343 : res |= vect_analyze_slp_instance (vinfo, bst_map,
4190 : : rest, kind, max_tree_size,
4191 : : limit, false);
4192 : 157533 : return res;
4193 : : }
4194 : : }
4195 : :
4196 : : /* For loop vectorization split the RHS into arbitrary pieces of
4197 : : size >= 1. */
4198 : 178266 : else if (is_a <loop_vec_info> (vinfo)
4199 : 178266 : && (group_size != 1 && i < group_size))
4200 : : {
4201 : 5284 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
4202 : 25 : bool masked_p = call
4203 : 25 : && gimple_call_internal_p (call)
4204 : 25 : && internal_fn_mask_index (gimple_call_internal_fn (call)) != -1;
4205 : : /* There are targets that cannot do even/odd interleaving schemes
4206 : : so they absolutely need to use load/store-lanes. For now
4207 : : force single-lane SLP for them - they would be happy with
4208 : : uniform power-of-two lanes (but depending on element size),
4209 : : but even if we can use 'i' as indicator we would need to
4210 : : backtrack when later lanes fail to discover with the same
4211 : : granularity. We cannot turn any of strided or scatter store
4212 : : into store-lanes. */
4213 : : /* ??? If this is not in sync with what get_load_store_type
4214 : : later decides the SLP representation is not good for other
4215 : : store vectorization methods. */
4216 : 5284 : bool want_store_lanes
4217 : 5284 : = (! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
4218 : 5284 : && ! STMT_VINFO_STRIDED_P (stmt_info)
4219 : 3814 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
4220 : 3811 : && compare_step_with_zero (vinfo, stmt_info) > 0
4221 : 9068 : && vect_slp_prefer_store_lanes_p (vinfo, stmt_info, NULL_TREE,
4222 : 10568 : masked_p, group_size, i));
4223 : 5284 : if (want_store_lanes || force_single_lane)
4224 : : i = 1;
4225 : :
4226 : : /* A fatal discovery fail doesn't always mean single-lane SLP
4227 : : isn't a possibility, so try. */
4228 : 3868 : if (i == 0)
4229 : : i = 1;
4230 : :
4231 : 5284 : if (dump_enabled_p ())
4232 : 796 : dump_printf_loc (MSG_NOTE, vect_location,
4233 : : "Splitting SLP group at stmt %u\n", i);
4234 : :
4235 : : /* Analyze the stored values and pinch them together with
4236 : : a permute node so we can preserve the whole store group. */
4237 : 5284 : auto_vec<slp_tree> rhs_nodes;
4238 : 5284 : poly_uint64 max_nunits = 1;
4239 : :
4240 : 5284 : unsigned int rhs_common_nlanes = 0;
4241 : 5284 : unsigned int start = 0, end = i;
4242 : 23707 : while (start < group_size)
4243 : : {
4244 : 18501 : gcc_assert (end - start >= 1);
4245 : 18501 : vec<stmt_vec_info> substmts;
4246 : 18501 : substmts.create (end - start);
4247 : 55144 : for (unsigned j = start; j < end; ++j)
4248 : 36643 : substmts.quick_push (scalar_stmts[j]);
4249 : 18501 : max_nunits = 1;
4250 : 18501 : node = vect_build_slp_tree (vinfo, substmts, end - start,
4251 : : &max_nunits,
4252 : : matches, limit, &tree_size, bst_map);
4253 : 18501 : if (node)
4254 : : {
4255 : 15104 : rhs_nodes.safe_push (node);
4256 : 15104 : vect_update_max_nunits (&max_nunits, node->max_nunits);
4257 : 15104 : if (start == 0)
4258 : 5206 : rhs_common_nlanes = SLP_TREE_LANES (node);
4259 : 9898 : else if (rhs_common_nlanes != SLP_TREE_LANES (node))
4260 : 1021 : rhs_common_nlanes = 0;
4261 : 15104 : start = end;
4262 : 15104 : if (want_store_lanes || force_single_lane)
4263 : 4320 : end = start + 1;
4264 : : else
4265 : : end = group_size;
4266 : : }
4267 : : else
4268 : : {
4269 : 3397 : substmts.release ();
4270 : 3397 : if (end - start == 1)
4271 : : {
4272 : : /* Single-lane discovery failed. Free ressources. */
4273 : 78 : for (auto node : rhs_nodes)
4274 : 0 : vect_free_slp_tree (node);
4275 : 78 : scalar_stmts.release ();
4276 : 78 : if (dump_enabled_p ())
4277 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
4278 : : "SLP discovery failed\n");
4279 : 78 : return false;
4280 : : }
4281 : :
4282 : : /* ??? It really happens that we soft-fail SLP
4283 : : build at a mismatch but the matching part hard-fails
4284 : : later. As we know we arrived here with a group
4285 : : larger than one try a group of size one! */
4286 : 3319 : if (!matches[0])
4287 : 44 : end = start + 1;
4288 : : else
4289 : 7408 : for (unsigned j = start; j < end; j++)
4290 : 7408 : if (!matches[j - start])
4291 : : {
4292 : : end = j;
4293 : : break;
4294 : : }
4295 : : }
4296 : : }
4297 : :
4298 : : /* Now re-assess whether we want store lanes in case the
4299 : : discovery ended up producing all single-lane RHSs. */
4300 : 5206 : if (! want_store_lanes
4301 : 5206 : && rhs_common_nlanes == 1
4302 : 4467 : && ! STMT_VINFO_GATHER_SCATTER_P (stmt_info)
4303 : 4467 : && ! STMT_VINFO_STRIDED_P (stmt_info)
4304 : 3257 : && ! STMT_VINFO_SLP_VECT_ONLY (stmt_info)
4305 : 3254 : && compare_step_with_zero (vinfo, stmt_info) > 0
4306 : 8442 : && (vect_store_lanes_supported (SLP_TREE_VECTYPE (rhs_nodes[0]),
4307 : : group_size, masked_p)
4308 : : != IFN_LAST))
4309 : : want_store_lanes = true;
4310 : :
4311 : : /* Now we assume we can build the root SLP node from all stores. */
4312 : 5206 : if (want_store_lanes)
4313 : : {
4314 : : /* For store-lanes feed the store node with all RHS nodes
4315 : : in order. */
4316 : 0 : node = vect_create_new_slp_node (scalar_stmts,
4317 : 0 : SLP_TREE_CHILDREN
4318 : : (rhs_nodes[0]).length ());
4319 : 0 : SLP_TREE_VECTYPE (node) = SLP_TREE_VECTYPE (rhs_nodes[0]);
4320 : 0 : node->max_nunits = max_nunits;
4321 : 0 : node->ldst_lanes = true;
4322 : 0 : SLP_TREE_CHILDREN (node)
4323 : 0 : .reserve_exact (SLP_TREE_CHILDREN (rhs_nodes[0]).length ()
4324 : 0 : + rhs_nodes.length () - 1);
4325 : : /* First store value and possibly mask. */
4326 : 0 : SLP_TREE_CHILDREN (node)
4327 : 0 : .splice (SLP_TREE_CHILDREN (rhs_nodes[0]));
4328 : : /* Rest of the store values. All mask nodes are the same,
4329 : : this should be guaranteed by dataref group discovery. */
4330 : 0 : for (unsigned j = 1; j < rhs_nodes.length (); ++j)
4331 : 0 : SLP_TREE_CHILDREN (node)
4332 : 0 : .quick_push (SLP_TREE_CHILDREN (rhs_nodes[j])[0]);
4333 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (node))
4334 : 0 : child->refcnt++;
4335 : : }
4336 : : else
4337 : 5206 : node = vect_build_slp_store_interleaving (rhs_nodes, scalar_stmts,
4338 : : max_nunits);
4339 : :
4340 : 20310 : while (!rhs_nodes.is_empty ())
4341 : 15104 : vect_free_slp_tree (rhs_nodes.pop ());
4342 : :
4343 : : /* Create a new SLP instance. */
4344 : 5206 : slp_instance new_instance = XNEW (class _slp_instance);
4345 : 5206 : SLP_INSTANCE_TREE (new_instance) = node;
4346 : 5206 : SLP_INSTANCE_LOADS (new_instance) = vNULL;
4347 : 5206 : SLP_INSTANCE_ROOT_STMTS (new_instance) = root_stmt_infos;
4348 : 5206 : SLP_INSTANCE_REMAIN_DEFS (new_instance) = remain;
4349 : 5206 : SLP_INSTANCE_KIND (new_instance) = kind;
4350 : 5206 : new_instance->reduc_phis = NULL;
4351 : 5206 : new_instance->cost_vec = vNULL;
4352 : 5206 : new_instance->subgraph_entries = vNULL;
4353 : :
4354 : 5206 : if (dump_enabled_p ())
4355 : 790 : dump_printf_loc (MSG_NOTE, vect_location,
4356 : : "SLP size %u vs. limit %u.\n",
4357 : : tree_size, max_tree_size);
4358 : :
4359 : 5206 : vinfo->slp_instances.safe_push (new_instance);
4360 : :
4361 : : /* ??? We've replaced the old SLP_INSTANCE_GROUP_SIZE with
4362 : : the number of scalar stmts in the root in a few places.
4363 : : Verify that assumption holds. */
4364 : 10412 : gcc_assert (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (new_instance))
4365 : : .length () == group_size);
4366 : :
4367 : 5206 : if (dump_enabled_p ())
4368 : : {
4369 : 790 : dump_printf_loc (MSG_NOTE, vect_location,
4370 : : "Final SLP tree for instance %p:\n",
4371 : : (void *) new_instance);
4372 : 790 : vect_print_slp_graph (MSG_NOTE, vect_location,
4373 : : SLP_INSTANCE_TREE (new_instance));
4374 : : }
4375 : 5206 : return true;
4376 : 5284 : }
4377 : : else
4378 : : /* Free the allocated memory. */
4379 : 172982 : scalar_stmts.release ();
4380 : :
4381 : : /* Even though the first vector did not all match, we might be able to SLP
4382 : : (some) of the remainder. FORNOW ignore this possibility. */
4383 : : }
4384 : : else
4385 : : /* Free the allocated memory. */
4386 : 1026063 : scalar_stmts.release ();
4387 : :
4388 : : /* Failed to SLP. */
4389 : 1199045 : if (dump_enabled_p ())
4390 : 2942 : dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
4391 : : return false;
4392 : : }
4393 : :
4394 : :
4395 : : /* Analyze an SLP instance starting from a group of grouped stores. Call
4396 : : vect_build_slp_tree to build a tree of packed stmts if possible.
4397 : : Return FALSE if it's impossible to SLP any stmt in the loop. */
4398 : :
4399 : : static bool
4400 : 1082002 : vect_analyze_slp_instance (vec_info *vinfo,
4401 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4402 : : stmt_vec_info stmt_info,
4403 : : slp_instance_kind kind,
4404 : : unsigned max_tree_size, unsigned *limit,
4405 : : bool force_single_lane)
4406 : : {
4407 : 1082002 : vec<stmt_vec_info> scalar_stmts;
4408 : :
4409 : 1082002 : if (is_a <bb_vec_info> (vinfo))
4410 : 1065241 : vect_location = stmt_info->stmt;
4411 : :
4412 : 1082002 : stmt_vec_info next_info = stmt_info;
4413 : 1082002 : if (kind == slp_inst_kind_store)
4414 : : {
4415 : : /* Collect the stores and store them in scalar_stmts. */
4416 : 1081515 : scalar_stmts.create (DR_GROUP_SIZE (stmt_info));
4417 : 5367931 : while (next_info)
4418 : : {
4419 : 3204901 : scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
4420 : 3204901 : next_info = DR_GROUP_NEXT_ELEMENT (next_info);
4421 : : }
4422 : : }
4423 : 487 : else if (kind == slp_inst_kind_reduc_chain)
4424 : : {
4425 : : /* Collect the reduction stmts and store them in scalar_stmts. */
4426 : 487 : scalar_stmts.create (REDUC_GROUP_SIZE (stmt_info));
4427 : 3047 : while (next_info)
4428 : : {
4429 : 2073 : scalar_stmts.quick_push (vect_stmt_to_vectorize (next_info));
4430 : 2073 : next_info = REDUC_GROUP_NEXT_ELEMENT (next_info);
4431 : : }
4432 : : /* Mark the first element of the reduction chain as reduction to properly
4433 : : transform the node. In the reduction analysis phase only the last
4434 : : element of the chain is marked as reduction. */
4435 : 487 : STMT_VINFO_DEF_TYPE (stmt_info)
4436 : 487 : = STMT_VINFO_DEF_TYPE (scalar_stmts.last ());
4437 : 487 : STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info))
4438 : 543 : = STMT_VINFO_REDUC_DEF (vect_orig_stmt (scalar_stmts.last ()));
4439 : : }
4440 : : else
4441 : 0 : gcc_unreachable ();
4442 : :
4443 : 1082002 : vec<stmt_vec_info> roots = vNULL;
4444 : 1082002 : vec<tree> remain = vNULL;
4445 : : /* Build the tree for the SLP instance. */
4446 : 1082489 : bool res = vect_build_slp_instance (vinfo, kind, scalar_stmts,
4447 : : roots, remain,
4448 : : max_tree_size, limit, bst_map,
4449 : : kind == slp_inst_kind_store
4450 : : ? stmt_info : NULL, force_single_lane);
4451 : :
4452 : : /* ??? If this is slp_inst_kind_store and the above succeeded here's
4453 : : where we should do store group splitting. */
4454 : :
4455 : 1082002 : return res;
4456 : : }
4457 : :
4458 : : /* qsort comparator ordering SLP load nodes. */
4459 : :
4460 : : static int
4461 : 1382084 : vllp_cmp (const void *a_, const void *b_)
4462 : : {
4463 : 1382084 : const slp_tree a = *(const slp_tree *)a_;
4464 : 1382084 : const slp_tree b = *(const slp_tree *)b_;
4465 : 1382084 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (a)[0];
4466 : 1382084 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (b)[0];
4467 : 1382084 : if (STMT_VINFO_GROUPED_ACCESS (a0)
4468 : 723689 : && STMT_VINFO_GROUPED_ACCESS (b0)
4469 : 2073266 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
4470 : : {
4471 : : /* Same group, order after lanes used. */
4472 : 204402 : if (SLP_TREE_LANES (a) < SLP_TREE_LANES (b))
4473 : : return 1;
4474 : 202124 : else if (SLP_TREE_LANES (a) > SLP_TREE_LANES (b))
4475 : : return -1;
4476 : : else
4477 : : {
4478 : : /* Try to order loads using the same lanes together, breaking
4479 : : the tie with the lane number that first differs. */
4480 : 199479 : if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
4481 : 199479 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
4482 : : return 0;
4483 : 199479 : else if (SLP_TREE_LOAD_PERMUTATION (a).exists ()
4484 : 199479 : && !SLP_TREE_LOAD_PERMUTATION (b).exists ())
4485 : : return 1;
4486 : 199001 : else if (!SLP_TREE_LOAD_PERMUTATION (a).exists ()
4487 : 199001 : && SLP_TREE_LOAD_PERMUTATION (b).exists ())
4488 : : return -1;
4489 : : else
4490 : : {
4491 : 198527 : for (unsigned i = 0; i < SLP_TREE_LANES (a); ++i)
4492 : 198527 : if (SLP_TREE_LOAD_PERMUTATION (a)[i]
4493 : 198527 : != SLP_TREE_LOAD_PERMUTATION (b)[i])
4494 : : {
4495 : : /* In-order lane first, that's what the above case for
4496 : : no permutation does. */
4497 : 198319 : if (SLP_TREE_LOAD_PERMUTATION (a)[i] == i)
4498 : : return -1;
4499 : 127767 : else if (SLP_TREE_LOAD_PERMUTATION (b)[i] == i)
4500 : : return 1;
4501 : 74769 : else if (SLP_TREE_LOAD_PERMUTATION (a)[i]
4502 : 74769 : < SLP_TREE_LOAD_PERMUTATION (b)[i])
4503 : : return -1;
4504 : : else
4505 : : return 1;
4506 : : }
4507 : : return 0;
4508 : : }
4509 : : }
4510 : : }
4511 : : else /* Different groups or non-groups. */
4512 : : {
4513 : : /* Order groups as their first element to keep them together. */
4514 : 1177682 : if (STMT_VINFO_GROUPED_ACCESS (a0))
4515 : 1177682 : a0 = DR_GROUP_FIRST_ELEMENT (a0);
4516 : 1177682 : if (STMT_VINFO_GROUPED_ACCESS (b0))
4517 : 1177682 : b0 = DR_GROUP_FIRST_ELEMENT (b0);
4518 : 1177682 : if (a0 == b0)
4519 : : return 0;
4520 : : /* Tie using UID. */
4521 : 1177682 : else if (gimple_uid (STMT_VINFO_STMT (a0))
4522 : 1177682 : < gimple_uid (STMT_VINFO_STMT (b0)))
4523 : : return -1;
4524 : : else
4525 : : {
4526 : 484510 : gcc_assert (gimple_uid (STMT_VINFO_STMT (a0))
4527 : : != gimple_uid (STMT_VINFO_STMT (b0)));
4528 : : return 1;
4529 : : }
4530 : : }
4531 : : }
4532 : :
4533 : : /* Process the set of LOADS that are all from the same dataref group. */
4534 : :
4535 : : static void
4536 : 77477 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
4537 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4538 : : const array_slice<slp_tree> &loads,
4539 : : bool force_single_lane)
4540 : : {
4541 : : /* We at this point want to lower without a fixed VF or vector
4542 : : size in mind which means we cannot actually compute whether we
4543 : : need three or more vectors for a load permutation yet. So always
4544 : : lower. */
4545 : 77477 : stmt_vec_info first
4546 : 77477 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (loads[0])[0]);
4547 : 77477 : unsigned group_lanes = DR_GROUP_SIZE (first);
4548 : :
4549 : : /* Verify if all load permutations can be implemented with a suitably
4550 : : large element load-lanes operation. */
4551 : 77477 : unsigned ld_lanes_lanes = SLP_TREE_LANES (loads[0]);
4552 : 77477 : if (STMT_VINFO_STRIDED_P (first)
4553 : 75838 : || compare_step_with_zero (loop_vinfo, first) <= 0
4554 : 74096 : || exact_log2 (ld_lanes_lanes) == -1
4555 : : /* ??? For now only support the single-lane case as there is
4556 : : missing support on the store-lane side and code generation
4557 : : isn't up to the task yet. */
4558 : 72608 : || ld_lanes_lanes != 1
4559 : 145271 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (loads[0]),
4560 : : group_lanes / ld_lanes_lanes,
4561 : : false) == IFN_LAST)
4562 : : ld_lanes_lanes = 0;
4563 : : else
4564 : : /* Verify the loads access the same number of lanes aligned to
4565 : : ld_lanes_lanes. */
4566 : 0 : for (slp_tree load : loads)
4567 : : {
4568 : 0 : if (SLP_TREE_LANES (load) != ld_lanes_lanes)
4569 : : {
4570 : : ld_lanes_lanes = 0;
4571 : : break;
4572 : : }
4573 : 0 : unsigned first = SLP_TREE_LOAD_PERMUTATION (load)[0];
4574 : 0 : if (first % ld_lanes_lanes != 0)
4575 : : {
4576 : : ld_lanes_lanes = 0;
4577 : : break;
4578 : : }
4579 : 0 : for (unsigned i = 1; i < SLP_TREE_LANES (load); ++i)
4580 : : if (SLP_TREE_LOAD_PERMUTATION (load)[i] != first + i)
4581 : : {
4582 : : ld_lanes_lanes = 0;
4583 : : break;
4584 : : }
4585 : : }
4586 : :
4587 : : /* Only a power-of-two number of lanes matches interleaving with N levels.
4588 : : ??? An even number of lanes could be reduced to 1<<ceil_log2(N)-1 lanes
4589 : : at each step. */
4590 : 119179 : if (ld_lanes_lanes == 0 && exact_log2 (group_lanes) == -1 && group_lanes != 3)
4591 : : return;
4592 : :
4593 : 148168 : for (slp_tree load : loads)
4594 : : {
4595 : : /* Leave masked or gather loads alone for now. */
4596 : 94947 : if (!SLP_TREE_CHILDREN (load).is_empty ())
4597 : 35339 : continue;
4598 : :
4599 : : /* For single-element interleaving spanning multiple vectors avoid
4600 : : lowering, we want to use VMAT_ELEMENTWISE later. */
4601 : 94933 : if (ld_lanes_lanes == 0
4602 : 94933 : && SLP_TREE_LANES (load) == 1
4603 : 87714 : && !DR_GROUP_NEXT_ELEMENT (first)
4604 : 131230 : && maybe_gt (group_lanes,
4605 : : TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (load))))
4606 : 10367 : return;
4607 : :
4608 : : /* We want to pattern-match special cases here and keep those
4609 : : alone. Candidates are splats and load-lane. */
4610 : :
4611 : : /* We need to lower only loads of less than half of the groups
4612 : : lanes, including duplicate lanes. Note this leaves nodes
4613 : : with a non-1:1 load permutation around instead of canonicalizing
4614 : : those into a load and a permute node. Removing this early
4615 : : check would do such canonicalization. */
4616 : 84566 : if (SLP_TREE_LANES (load) >= (group_lanes + 1) / 2
4617 : 32106 : && ld_lanes_lanes == 0)
4618 : 32106 : continue;
4619 : :
4620 : : /* Build the permute to get the original load permutation order. */
4621 : 52460 : bool contiguous = true;
4622 : 52460 : lane_permutation_t final_perm;
4623 : 52460 : final_perm.create (SLP_TREE_LANES (load));
4624 : 105397 : for (unsigned i = 0; i < SLP_TREE_LANES (load); ++i)
4625 : : {
4626 : 52937 : final_perm.quick_push
4627 : 52937 : (std::make_pair (0, SLP_TREE_LOAD_PERMUTATION (load)[i]));
4628 : 52937 : if (i != 0
4629 : 52937 : && (SLP_TREE_LOAD_PERMUTATION (load)[i]
4630 : 477 : != SLP_TREE_LOAD_PERMUTATION (load)[i-1] + 1))
4631 : : contiguous = false;
4632 : : }
4633 : :
4634 : : /* When the load permutation accesses a contiguous unpermuted,
4635 : : power-of-two aligned and sized chunk leave the load alone.
4636 : : We can likely (re-)load it more efficiently rather than
4637 : : extracting it from the larger load.
4638 : : ??? Long-term some of the lowering should move to where
4639 : : the vector types involved are fixed. */
4640 : 55679 : if (!force_single_lane
4641 : 52460 : && ld_lanes_lanes == 0
4642 : 30291 : && contiguous
4643 : 30115 : && (SLP_TREE_LANES (load) > 1 || loads.size () == 1)
4644 : 6366 : && pow2p_hwi (SLP_TREE_LANES (load))
4645 : 6366 : && pow2p_hwi (group_lanes)
4646 : 3219 : && SLP_TREE_LOAD_PERMUTATION (load)[0] % SLP_TREE_LANES (load) == 0
4647 : 55679 : && group_lanes % SLP_TREE_LANES (load) == 0)
4648 : : {
4649 : 3219 : final_perm.release ();
4650 : 3219 : continue;
4651 : : }
4652 : :
4653 : : /* First build (and possibly re-use) a load node for the
4654 : : unpermuted group. Gaps in the middle and on the end are
4655 : : represented with NULL stmts. */
4656 : 49241 : vec<stmt_vec_info> stmts;
4657 : 49241 : stmts.create (group_lanes);
4658 : 172932 : for (stmt_vec_info s = first; s; s = DR_GROUP_NEXT_ELEMENT (s))
4659 : : {
4660 : 123691 : if (s != first)
4661 : 77728 : for (unsigned i = 1; i < DR_GROUP_GAP (s); ++i)
4662 : 3278 : stmts.quick_push (NULL);
4663 : 123691 : stmts.quick_push (s);
4664 : : }
4665 : 96119 : for (unsigned i = 0; i < DR_GROUP_GAP (first); ++i)
4666 : 46878 : stmts.quick_push (NULL);
4667 : 49241 : poly_uint64 max_nunits = 1;
4668 : 49241 : bool *matches = XALLOCAVEC (bool, group_lanes);
4669 : 49241 : unsigned limit = 1;
4670 : 49241 : unsigned tree_size = 0;
4671 : 49241 : slp_tree l0 = vect_build_slp_tree (loop_vinfo, stmts,
4672 : : group_lanes,
4673 : : &max_nunits, matches, &limit,
4674 : 49241 : &tree_size, bst_map);
4675 : 49241 : gcc_assert (!SLP_TREE_LOAD_PERMUTATION (l0).exists ());
4676 : :
4677 : 49241 : if (ld_lanes_lanes != 0)
4678 : : {
4679 : : /* ??? If this is not in sync with what get_load_store_type
4680 : : later decides the SLP representation is not good for other
4681 : : store vectorization methods. */
4682 : 0 : l0->ldst_lanes = true;
4683 : 0 : load->ldst_lanes = true;
4684 : : }
4685 : :
4686 : 154801 : while (1)
4687 : : {
4688 : 102021 : unsigned group_lanes = SLP_TREE_LANES (l0);
4689 : 102021 : if (ld_lanes_lanes != 0
4690 : 102021 : || SLP_TREE_LANES (load) >= (group_lanes + 1) / 2)
4691 : : break;
4692 : :
4693 : : /* Try to lower by reducing the group to half its size using an
4694 : : interleaving scheme. For this try to compute whether all
4695 : : elements needed for this load are in even or odd elements of
4696 : : an even/odd decomposition with N consecutive elements.
4697 : : Thus { e, e, o, o, e, e, o, o } woud be an even/odd decomposition
4698 : : with N == 2. */
4699 : : /* ??? Only an even number of lanes can be handed this way, but the
4700 : : fallback below could work for any number. We have to make sure
4701 : : to round up in that case. */
4702 : 52780 : gcc_assert ((group_lanes & 1) == 0 || group_lanes == 3);
4703 : 9383 : unsigned even = 0, odd = 0;
4704 : 9383 : if ((group_lanes & 1) == 0)
4705 : : {
4706 : 9383 : even = (1 << ceil_log2 (group_lanes)) - 1;
4707 : 9383 : odd = even;
4708 : 37886 : for (auto l : final_perm)
4709 : : {
4710 : 9737 : even &= ~l.second;
4711 : 9737 : odd &= l.second;
4712 : : }
4713 : : }
4714 : :
4715 : : /* Now build an even or odd extraction from the unpermuted load. */
4716 : 52780 : lane_permutation_t perm;
4717 : 52780 : perm.create ((group_lanes + 1) / 2);
4718 : 52780 : unsigned even_level = even ? 1 << ctz_hwi (even) : 0;
4719 : 52780 : unsigned odd_level = odd ? 1 << ctz_hwi (odd) : 0;
4720 : 52780 : if (even_level
4721 : 8763 : && group_lanes % (2 * even_level) == 0
4722 : : /* ??? When code generating permutes we do not try to pun
4723 : : to larger component modes so level != 1 isn't a natural
4724 : : even/odd extract. Prefer one if possible. */
4725 : 8763 : && (even_level == 1 || !odd_level || odd_level != 1))
4726 : : {
4727 : : /* { 0, 1, ... 4, 5 ..., } */
4728 : 29560 : for (unsigned i = 0; i < group_lanes / 2 / even_level; ++i)
4729 : 45422 : for (unsigned j = 0; j < even_level; ++j)
4730 : 22768 : perm.quick_push (std::make_pair (0, 2 * i * even_level + j));
4731 : : }
4732 : 44017 : else if (odd_level)
4733 : : {
4734 : : /* { ..., 2, 3, ... 6, 7 } */
4735 : 2477 : gcc_assert (group_lanes % (2 * odd_level) == 0);
4736 : 11293 : for (unsigned i = 0; i < group_lanes / 2 / odd_level; ++i)
4737 : 17632 : for (unsigned j = 0; j < odd_level; ++j)
4738 : 8816 : perm.quick_push
4739 : 8816 : (std::make_pair (0, (2 * i + 1) * odd_level + j));
4740 : : }
4741 : : else
4742 : : {
4743 : : /* As fallback extract all used lanes and fill to half the
4744 : : group size by repeating the last element.
4745 : : ??? This is quite a bad strathegy for re-use - we could
4746 : : brute force our way to find more optimal filling lanes to
4747 : : maximize re-use when looking at all loads from the group. */
4748 : 43397 : auto_bitmap l;
4749 : 173588 : for (auto p : final_perm)
4750 : 43397 : bitmap_set_bit (l, p.second);
4751 : 43397 : unsigned i = 0;
4752 : 43397 : bitmap_iterator bi;
4753 : 86794 : EXECUTE_IF_SET_IN_BITMAP (l, 0, i, bi)
4754 : 43397 : perm.quick_push (std::make_pair (0, i));
4755 : 173588 : while (perm.length () < (group_lanes + 1) / 2)
4756 : 43397 : perm.quick_push (perm.last ());
4757 : 43397 : }
4758 : :
4759 : : /* Update final_perm with the intermediate permute. */
4760 : 105914 : for (unsigned i = 0; i < final_perm.length (); ++i)
4761 : : {
4762 : 53134 : unsigned l = final_perm[i].second;
4763 : 53134 : unsigned j;
4764 : 59397 : for (j = 0; j < perm.length (); ++j)
4765 : 59397 : if (perm[j].second == l)
4766 : : {
4767 : 53134 : final_perm[i].second = j;
4768 : 53134 : break;
4769 : : }
4770 : 53134 : gcc_assert (j < perm.length ());
4771 : : }
4772 : :
4773 : : /* And create scalar stmts. */
4774 : 52780 : vec<stmt_vec_info> perm_stmts;
4775 : 52780 : perm_stmts.create (perm.length ());
4776 : 171158 : for (unsigned i = 0; i < perm.length (); ++i)
4777 : 118378 : perm_stmts.quick_push (SLP_TREE_SCALAR_STMTS (l0)[perm[i].second]);
4778 : :
4779 : 52780 : slp_tree p = vect_create_new_slp_node (1, VEC_PERM_EXPR);
4780 : 52780 : SLP_TREE_CHILDREN (p).quick_push (l0);
4781 : 52780 : SLP_TREE_LANE_PERMUTATION (p) = perm;
4782 : 52780 : SLP_TREE_VECTYPE (p) = SLP_TREE_VECTYPE (load);
4783 : 52780 : SLP_TREE_LANES (p) = perm.length ();
4784 : 52780 : SLP_TREE_REPRESENTATIVE (p) = SLP_TREE_REPRESENTATIVE (load);
4785 : : /* ??? As we have scalar stmts for this intermediate permute we
4786 : : could CSE it via bst_map but we do not want to pick up
4787 : : another SLP node with a load permutation. We instead should
4788 : : have a "local" CSE map here. */
4789 : 52780 : SLP_TREE_SCALAR_STMTS (p) = perm_stmts;
4790 : :
4791 : : /* We now have a node for (group_lanes + 1) / 2 lanes. */
4792 : 52780 : l0 = p;
4793 : 52780 : }
4794 : :
4795 : : /* And finally from the ordered reduction node create the
4796 : : permute to shuffle the lanes into the original load-permutation
4797 : : order. We replace the original load node with this. */
4798 : 49241 : SLP_TREE_CODE (load) = VEC_PERM_EXPR;
4799 : 49241 : SLP_TREE_LOAD_PERMUTATION (load).release ();
4800 : 49241 : SLP_TREE_LANE_PERMUTATION (load) = final_perm;
4801 : 49241 : SLP_TREE_CHILDREN (load).create (1);
4802 : 49241 : SLP_TREE_CHILDREN (load).quick_push (l0);
4803 : : }
4804 : : }
4805 : :
4806 : : /* Transform SLP loads in the SLP graph created by SLP discovery to
4807 : : group loads from the same group and lower load permutations that
4808 : : are unlikely to be supported into a series of permutes.
4809 : : In the degenerate case of having only single-lane SLP instances
4810 : : this should result in a series of permute nodes emulating an
4811 : : interleaving scheme. */
4812 : :
4813 : : static void
4814 : 278208 : vect_lower_load_permutations (loop_vec_info loop_vinfo,
4815 : : scalar_stmts_to_slp_tree_map_t *bst_map,
4816 : : bool force_single_lane)
4817 : : {
4818 : : /* Gather and sort loads across all instances. */
4819 : 278208 : hash_set<slp_tree> visited;
4820 : 278208 : auto_vec<slp_tree> loads;
4821 : 1473227 : for (auto inst : loop_vinfo->slp_instances)
4822 : 641809 : vect_gather_slp_loads (loads, SLP_INSTANCE_TREE (inst), visited);
4823 : 278208 : if (loads.is_empty ())
4824 : 54979 : return;
4825 : 223229 : loads.qsort (vllp_cmp);
4826 : :
4827 : : /* Now process each dataref group separately. */
4828 : 223229 : unsigned firsti = 0;
4829 : 403211 : for (unsigned i = 1; i < loads.length (); ++i)
4830 : : {
4831 : 179982 : slp_tree first = loads[firsti];
4832 : 179982 : slp_tree next = loads[i];
4833 : 179982 : stmt_vec_info a0 = SLP_TREE_SCALAR_STMTS (first)[0];
4834 : 179982 : stmt_vec_info b0 = SLP_TREE_SCALAR_STMTS (next)[0];
4835 : 179982 : if (STMT_VINFO_GROUPED_ACCESS (a0)
4836 : 76292 : && STMT_VINFO_GROUPED_ACCESS (b0)
4837 : 248826 : && DR_GROUP_FIRST_ELEMENT (a0) == DR_GROUP_FIRST_ELEMENT (b0))
4838 : 35411 : continue;
4839 : : /* Now we have one or multiple SLP loads of the same group from
4840 : : firsti to i - 1. */
4841 : 144571 : if (STMT_VINFO_GROUPED_ACCESS (a0))
4842 : 40881 : vect_lower_load_permutations (loop_vinfo, bst_map,
4843 : 40881 : make_array_slice (&loads[firsti],
4844 : : i - firsti),
4845 : : force_single_lane);
4846 : : firsti = i;
4847 : : }
4848 : 446458 : if (firsti < loads.length ()
4849 : 446458 : && STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (loads[firsti])[0]))
4850 : 36596 : vect_lower_load_permutations (loop_vinfo, bst_map,
4851 : 36596 : make_array_slice (&loads[firsti],
4852 : 36596 : loads.length () - firsti),
4853 : : force_single_lane);
4854 : 278208 : }
4855 : :
4856 : : /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
4857 : : trees of packed scalar stmts if SLP is possible. */
4858 : :
4859 : : opt_result
4860 : 909221 : vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size,
4861 : : bool force_single_lane)
4862 : : {
4863 : 909221 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
4864 : 909221 : unsigned int i;
4865 : 909221 : stmt_vec_info first_element;
4866 : 909221 : slp_instance instance;
4867 : :
4868 : 909221 : DUMP_VECT_SCOPE ("vect_analyze_slp");
4869 : :
4870 : 909221 : unsigned limit = max_tree_size;
4871 : :
4872 : 909221 : scalar_stmts_to_slp_tree_map_t *bst_map
4873 : 909221 : = new scalar_stmts_to_slp_tree_map_t ();
4874 : :
4875 : : /* Find SLP sequences starting from groups of grouped stores. */
4876 : 2719026 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
4877 : 900584 : vect_analyze_slp_instance (vinfo, bst_map, first_element,
4878 : : slp_inst_kind_store, max_tree_size, &limit,
4879 : : force_single_lane);
4880 : :
4881 : : /* For loops also start SLP discovery from non-grouped stores. */
4882 : 909221 : if (loop_vinfo)
4883 : : {
4884 : : data_reference_p dr;
4885 : 923960 : FOR_EACH_VEC_ELT (vinfo->shared->datarefs, i, dr)
4886 : 645752 : if (DR_IS_WRITE (dr))
4887 : : {
4888 : 224751 : stmt_vec_info stmt_info = vinfo->lookup_dr (dr)->stmt;
4889 : : /* Grouped stores are already handled above. */
4890 : 224751 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
4891 : 57281 : continue;
4892 : 167470 : vec<stmt_vec_info> stmts;
4893 : 167470 : vec<stmt_vec_info> roots = vNULL;
4894 : 167470 : vec<tree> remain = vNULL;
4895 : 167470 : stmts.create (1);
4896 : 167470 : stmts.quick_push (stmt_info);
4897 : 167470 : vect_build_slp_instance (vinfo, slp_inst_kind_store,
4898 : : stmts, roots, remain, max_tree_size,
4899 : : &limit, bst_map, NULL, force_single_lane);
4900 : : }
4901 : : }
4902 : :
4903 : 909221 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
4904 : : {
4905 : 1821252 : for (unsigned i = 0; i < bb_vinfo->roots.length (); ++i)
4906 : : {
4907 : 1190239 : vect_location = bb_vinfo->roots[i].roots[0]->stmt;
4908 : : /* Apply patterns. */
4909 : 3724965 : for (unsigned j = 0; j < bb_vinfo->roots[i].stmts.length (); ++j)
4910 : 5069452 : bb_vinfo->roots[i].stmts[j]
4911 : 2599201 : = vect_stmt_to_vectorize (bb_vinfo->roots[i].stmts[j]);
4912 : 1190239 : if (vect_build_slp_instance (bb_vinfo, bb_vinfo->roots[i].kind,
4913 : 1190239 : bb_vinfo->roots[i].stmts,
4914 : 1190239 : bb_vinfo->roots[i].roots,
4915 : 1190239 : bb_vinfo->roots[i].remain,
4916 : : max_tree_size, &limit, bst_map, NULL,
4917 : : false))
4918 : : {
4919 : 139786 : bb_vinfo->roots[i].stmts = vNULL;
4920 : 139786 : bb_vinfo->roots[i].roots = vNULL;
4921 : 139786 : bb_vinfo->roots[i].remain = vNULL;
4922 : : }
4923 : : }
4924 : : }
4925 : :
4926 : 909221 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
4927 : : {
4928 : : /* Find SLP sequences starting from reduction chains. */
4929 : 278699 : FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
4930 : 491 : if (! STMT_VINFO_RELEVANT_P (first_element)
4931 : 4 : && ! STMT_VINFO_LIVE_P (first_element))
4932 : : ;
4933 : 487 : else if (force_single_lane
4934 : 487 : || ! vect_analyze_slp_instance (vinfo, bst_map, first_element,
4935 : : slp_inst_kind_reduc_chain,
4936 : : max_tree_size, &limit,
4937 : : force_single_lane))
4938 : : {
4939 : : /* Dissolve reduction chain group. */
4940 : 213 : stmt_vec_info vinfo = first_element;
4941 : 213 : stmt_vec_info last = NULL;
4942 : 862 : while (vinfo)
4943 : : {
4944 : 649 : stmt_vec_info next = REDUC_GROUP_NEXT_ELEMENT (vinfo);
4945 : 649 : REDUC_GROUP_FIRST_ELEMENT (vinfo) = NULL;
4946 : 649 : REDUC_GROUP_NEXT_ELEMENT (vinfo) = NULL;
4947 : 649 : last = vinfo;
4948 : 649 : vinfo = next;
4949 : : }
4950 : 213 : STMT_VINFO_DEF_TYPE (first_element) = vect_internal_def;
4951 : : /* It can be still vectorized as part of an SLP reduction. */
4952 : 213 : loop_vinfo->reductions.safe_push (last);
4953 : : }
4954 : :
4955 : : /* Find SLP sequences starting from groups of reductions. */
4956 : 278208 : if (loop_vinfo->reductions.length () > 0)
4957 : : {
4958 : : /* Collect reduction statements we can combine into
4959 : : a SLP reduction. */
4960 : 39770 : vec<stmt_vec_info> scalar_stmts;
4961 : 39770 : scalar_stmts.create (loop_vinfo->reductions.length ());
4962 : 175049 : for (auto next_info : loop_vinfo->reductions)
4963 : : {
4964 : 55739 : next_info = vect_stmt_to_vectorize (next_info);
4965 : 55739 : if ((STMT_VINFO_RELEVANT_P (next_info)
4966 : 4 : || STMT_VINFO_LIVE_P (next_info))
4967 : : /* ??? Make sure we didn't skip a conversion around a
4968 : : reduction path. In that case we'd have to reverse
4969 : : engineer that conversion stmt following the chain using
4970 : : reduc_idx and from the PHI using reduc_def. */
4971 : 55735 : && (STMT_VINFO_DEF_TYPE (next_info) == vect_reduction_def
4972 : 55735 : || (STMT_VINFO_DEF_TYPE (next_info)
4973 : : == vect_double_reduction_def)))
4974 : : {
4975 : : /* Do not discover SLP reductions combining lane-reducing
4976 : : ops, that will fail later. */
4977 : 55683 : if (!force_single_lane
4978 : 55683 : && !lane_reducing_stmt_p (STMT_VINFO_STMT (next_info)))
4979 : 41007 : scalar_stmts.quick_push (next_info);
4980 : : else
4981 : : {
4982 : : /* Do SLP discovery for single-lane reductions. */
4983 : 14676 : vec<stmt_vec_info> stmts;
4984 : 14676 : vec<stmt_vec_info> roots = vNULL;
4985 : 14676 : vec<tree> remain = vNULL;
4986 : 14676 : stmts.create (1);
4987 : 14676 : stmts.quick_push (next_info);
4988 : 14676 : vect_build_slp_instance (vinfo,
4989 : : slp_inst_kind_reduc_group,
4990 : : stmts, roots, remain,
4991 : : max_tree_size, &limit,
4992 : : bst_map, NULL,
4993 : : force_single_lane);
4994 : : }
4995 : : }
4996 : : }
4997 : : /* Save for re-processing on failure. */
4998 : 39770 : vec<stmt_vec_info> saved_stmts = scalar_stmts.copy ();
4999 : 39770 : vec<stmt_vec_info> roots = vNULL;
5000 : 39770 : vec<tree> remain = vNULL;
5001 : 39770 : if (scalar_stmts.length () <= 1
5002 : 39770 : || !vect_build_slp_instance (loop_vinfo,
5003 : : slp_inst_kind_reduc_group,
5004 : : scalar_stmts, roots, remain,
5005 : : max_tree_size, &limit, bst_map,
5006 : : NULL, force_single_lane))
5007 : : {
5008 : 39484 : if (scalar_stmts.length () <= 1)
5009 : 39484 : scalar_stmts.release ();
5010 : : /* Do SLP discovery for single-lane reductions. */
5011 : 135803 : for (auto stmt_info : saved_stmts)
5012 : : {
5013 : 40199 : vec<stmt_vec_info> stmts;
5014 : 40199 : vec<stmt_vec_info> roots = vNULL;
5015 : 40199 : vec<tree> remain = vNULL;
5016 : 40199 : stmts.create (1);
5017 : 40199 : stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
5018 : 40199 : vect_build_slp_instance (vinfo,
5019 : : slp_inst_kind_reduc_group,
5020 : : stmts, roots, remain,
5021 : : max_tree_size, &limit,
5022 : : bst_map, NULL, force_single_lane);
5023 : : }
5024 : : }
5025 : 39770 : saved_stmts.release ();
5026 : : }
5027 : :
5028 : : /* Make sure to vectorize only-live stmts, usually inductions. */
5029 : 1256514 : for (edge e : get_loop_exit_edges (LOOP_VINFO_LOOP (loop_vinfo)))
5030 : 770339 : for (auto gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
5031 : 348449 : gsi_next (&gsi))
5032 : : {
5033 : 348449 : gphi *lc_phi = *gsi;
5034 : 348449 : tree def = gimple_phi_arg_def_from_edge (lc_phi, e);
5035 : 348449 : stmt_vec_info stmt_info;
5036 : 348449 : if (TREE_CODE (def) == SSA_NAME
5037 : 314209 : && !virtual_operand_p (def)
5038 : 139236 : && (stmt_info = loop_vinfo->lookup_def (def))
5039 : 128681 : && ((stmt_info = vect_stmt_to_vectorize (stmt_info)), true)
5040 : 128681 : && STMT_VINFO_RELEVANT (stmt_info) == vect_used_only_live
5041 : 102245 : && STMT_VINFO_LIVE_P (stmt_info)
5042 : 450694 : && (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def
5043 : 83663 : || (STMT_VINFO_DEF_TYPE (stmt_info) == vect_internal_def
5044 : 25599 : && STMT_VINFO_REDUC_IDX (stmt_info) == -1)))
5045 : : {
5046 : 44128 : vec<stmt_vec_info> stmts;
5047 : 44128 : vec<stmt_vec_info> roots = vNULL;
5048 : 44128 : vec<tree> remain = vNULL;
5049 : 44128 : stmts.create (1);
5050 : 44128 : stmts.quick_push (vect_stmt_to_vectorize (stmt_info));
5051 : 44128 : vect_build_slp_instance (vinfo,
5052 : : slp_inst_kind_reduc_group,
5053 : : stmts, roots, remain,
5054 : : max_tree_size, &limit,
5055 : : bst_map, NULL, force_single_lane);
5056 : : }
5057 : 278208 : }
5058 : :
5059 : : /* Find SLP sequences starting from gconds. */
5060 : 646710 : for (auto cond : LOOP_VINFO_LOOP_CONDS (loop_vinfo))
5061 : : {
5062 : 143682 : auto cond_info = loop_vinfo->lookup_stmt (cond);
5063 : :
5064 : 143682 : cond_info = vect_stmt_to_vectorize (cond_info);
5065 : 143682 : vec<stmt_vec_info> roots = vNULL;
5066 : 143682 : roots.safe_push (cond_info);
5067 : 143682 : gimple *stmt = STMT_VINFO_STMT (cond_info);
5068 : 143682 : tree args0 = gimple_cond_lhs (stmt);
5069 : 143682 : tree args1 = gimple_cond_rhs (stmt);
5070 : :
5071 : : /* These should be enforced by cond lowering, but if it failed
5072 : : bail. */
5073 : 143682 : if (gimple_cond_code (stmt) != NE_EXPR
5074 : 143682 : || TREE_TYPE (args0) != boolean_type_node
5075 : 287364 : || !integer_zerop (args1))
5076 : : {
5077 : 0 : roots.release ();
5078 : 0 : continue;
5079 : : }
5080 : :
5081 : : /* An argument without a loop def will be codegened from vectorizing the
5082 : : root gcond itself. As such we don't need to try to build an SLP tree
5083 : : from them. It's highly likely that the resulting SLP tree here if both
5084 : : arguments have a def will be incompatible, but we rely on it being split
5085 : : later on. */
5086 : 143682 : auto varg = loop_vinfo->lookup_def (args0);
5087 : 143682 : vec<stmt_vec_info> stmts;
5088 : 143682 : vec<tree> remain = vNULL;
5089 : 143682 : stmts.create (1);
5090 : 143682 : stmts.quick_push (vect_stmt_to_vectorize (varg));
5091 : :
5092 : 143682 : if (! vect_build_slp_instance (vinfo, slp_inst_kind_gcond,
5093 : : stmts, roots, remain,
5094 : : max_tree_size, &limit,
5095 : : bst_map, NULL, force_single_lane))
5096 : 106 : roots.release ();
5097 : : }
5098 : :
5099 : : /* Find and create slp instances for inductions that have been forced
5100 : : live due to early break. */
5101 : 278208 : edge latch_e = loop_latch_edge (LOOP_VINFO_LOOP (loop_vinfo));
5102 : 713087 : for (auto stmt_info : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo))
5103 : : {
5104 : 210059 : vec<stmt_vec_info> stmts;
5105 : 210059 : vec<stmt_vec_info> roots = vNULL;
5106 : 210059 : vec<tree> remain = vNULL;
5107 : 210059 : gphi *phi = as_a<gphi *> (STMT_VINFO_STMT (stmt_info));
5108 : 210059 : tree def = gimple_phi_arg_def_from_edge (phi, latch_e);
5109 : 210059 : stmt_vec_info lc_info = loop_vinfo->lookup_def (def);
5110 : 210059 : if (lc_info)
5111 : : {
5112 : 210049 : stmts.create (1);
5113 : 210079 : stmts.quick_push (vect_stmt_to_vectorize (lc_info));
5114 : 210049 : vect_build_slp_instance (vinfo, slp_inst_kind_reduc_group,
5115 : : stmts, roots, remain,
5116 : : max_tree_size, &limit,
5117 : : bst_map, NULL, force_single_lane);
5118 : : }
5119 : : /* When the latch def is from a different cycle this can only
5120 : : be a induction. Build a simple instance for this.
5121 : : ??? We should be able to start discovery from the PHI
5122 : : for all inductions, but then there will be stray
5123 : : non-SLP stmts we choke on as needing non-SLP handling. */
5124 : 210059 : auto_vec<stmt_vec_info, 1> tem;
5125 : 210059 : tem.quick_push (stmt_info);
5126 : 210059 : if (!bst_map->get (tem))
5127 : : {
5128 : 5544 : stmts.create (1);
5129 : 5544 : stmts.quick_push (stmt_info);
5130 : 5544 : vect_build_slp_instance (vinfo, slp_inst_kind_reduc_group,
5131 : : stmts, roots, remain,
5132 : : max_tree_size, &limit,
5133 : : bst_map, NULL, force_single_lane);
5134 : : }
5135 : 210059 : }
5136 : : }
5137 : :
5138 : 909221 : hash_set<slp_tree> visited_patterns;
5139 : 909221 : slp_tree_to_load_perm_map_t perm_cache;
5140 : 909221 : slp_compat_nodes_map_t compat_cache;
5141 : :
5142 : : /* See if any patterns can be found in the SLP tree. */
5143 : 909221 : bool pattern_found = false;
5144 : 3335075 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5145 : 1516633 : pattern_found |= vect_match_slp_patterns (instance, vinfo,
5146 : : &visited_patterns, &perm_cache,
5147 : : &compat_cache);
5148 : :
5149 : : /* If any were found optimize permutations of loads. */
5150 : 909221 : if (pattern_found)
5151 : : {
5152 : 201 : hash_map<slp_tree, slp_tree> load_map;
5153 : 2607 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5154 : : {
5155 : 2205 : slp_tree root = SLP_INSTANCE_TREE (instance);
5156 : 2205 : optimize_load_redistribution (bst_map, vinfo, SLP_TREE_LANES (root),
5157 : : &load_map, root);
5158 : : }
5159 : 201 : }
5160 : :
5161 : : /* Check whether we should force some SLP instances to use load/store-lanes
5162 : : and do so by forcing SLP re-discovery with single lanes. We used
5163 : : to cancel SLP when this applied to all instances in a loop but now
5164 : : we decide this per SLP instance. It's important to do this only
5165 : : after SLP pattern recognition. */
5166 : 909221 : if (is_a <loop_vec_info> (vinfo))
5167 : 920017 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5168 : 641809 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_store
5169 : 183332 : && !SLP_INSTANCE_TREE (instance)->ldst_lanes)
5170 : : {
5171 : 183332 : slp_tree slp_root = SLP_INSTANCE_TREE (instance);
5172 : 183332 : unsigned int group_size = SLP_TREE_LANES (slp_root);
5173 : 183332 : tree vectype = SLP_TREE_VECTYPE (slp_root);
5174 : :
5175 : 183332 : stmt_vec_info rep_info = SLP_TREE_REPRESENTATIVE (slp_root);
5176 : 183332 : gimple *rep = STMT_VINFO_STMT (rep_info);
5177 : 183332 : bool masked = (is_gimple_call (rep)
5178 : 1178 : && gimple_call_internal_p (rep)
5179 : 184510 : && internal_fn_mask_index
5180 : 1178 : (gimple_call_internal_fn (rep)) != -1);
5181 : 183332 : if (!STMT_VINFO_GROUPED_ACCESS (rep_info)
5182 : 16168 : || slp_root->ldst_lanes
5183 : 199500 : || (vect_store_lanes_supported (vectype, group_size, masked)
5184 : : == IFN_LAST))
5185 : 183332 : continue;
5186 : :
5187 : 0 : auto_vec<slp_tree> loads;
5188 : 0 : hash_set<slp_tree> visited;
5189 : 0 : vect_gather_slp_loads (loads, slp_root, visited);
5190 : :
5191 : : /* Check whether any load in the SLP instance is possibly
5192 : : permuted. */
5193 : 0 : bool loads_permuted = false;
5194 : 0 : slp_tree load_node;
5195 : 0 : unsigned j;
5196 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5197 : : {
5198 : 0 : if (!SLP_TREE_LOAD_PERMUTATION (load_node).exists ())
5199 : 0 : continue;
5200 : : unsigned k;
5201 : : stmt_vec_info load_info;
5202 : 0 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), k, load_info)
5203 : 0 : if (SLP_TREE_LOAD_PERMUTATION (load_node)[k] != k)
5204 : : {
5205 : : loads_permuted = true;
5206 : : break;
5207 : : }
5208 : : }
5209 : :
5210 : : /* If the loads and stores can use load/store-lanes force re-discovery
5211 : : with single lanes. */
5212 : 0 : if (loads_permuted)
5213 : : {
5214 : 0 : bool can_use_lanes = true;
5215 : : bool prefer_load_lanes = false;
5216 : 0 : FOR_EACH_VEC_ELT (loads, j, load_node)
5217 : 0 : if (STMT_VINFO_GROUPED_ACCESS
5218 : : (SLP_TREE_REPRESENTATIVE (load_node)))
5219 : : {
5220 : 0 : stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
5221 : : (SLP_TREE_REPRESENTATIVE (load_node));
5222 : 0 : rep = STMT_VINFO_STMT (stmt_vinfo);
5223 : 0 : masked = (is_gimple_call (rep)
5224 : 0 : && gimple_call_internal_p (rep)
5225 : 0 : && internal_fn_mask_index
5226 : 0 : (gimple_call_internal_fn (rep)));
5227 : : /* Use SLP for strided accesses (or if we can't
5228 : : load-lanes). */
5229 : 0 : if (STMT_VINFO_STRIDED_P (stmt_vinfo)
5230 : 0 : || compare_step_with_zero (vinfo, stmt_vinfo) <= 0
5231 : 0 : || vect_load_lanes_supported
5232 : 0 : (STMT_VINFO_VECTYPE (stmt_vinfo),
5233 : 0 : DR_GROUP_SIZE (stmt_vinfo), masked) == IFN_LAST
5234 : : /* ??? During SLP re-discovery with a single lane
5235 : : a masked grouped load will appear permuted and
5236 : : discovery will fail. We have to rework this
5237 : : on the discovery side - for now avoid ICEing. */
5238 : 0 : || masked)
5239 : : {
5240 : : can_use_lanes = false;
5241 : : break;
5242 : : }
5243 : : /* Make sure that the target would prefer store-lanes
5244 : : for at least one of the loads.
5245 : :
5246 : : ??? Perhaps we should instead require this for
5247 : : all loads? */
5248 : 0 : prefer_load_lanes
5249 : : = (prefer_load_lanes
5250 : 0 : || SLP_TREE_LANES (load_node) == group_size
5251 : 0 : || (vect_slp_prefer_store_lanes_p
5252 : 0 : (vinfo, stmt_vinfo,
5253 : : STMT_VINFO_VECTYPE (stmt_vinfo), masked,
5254 : : group_size, SLP_TREE_LANES (load_node))));
5255 : : }
5256 : :
5257 : 0 : if (can_use_lanes && prefer_load_lanes)
5258 : : {
5259 : 0 : if (dump_enabled_p ())
5260 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
5261 : : "SLP instance %p can use load/store-lanes,"
5262 : : " re-discovering with single-lanes\n",
5263 : : (void *) instance);
5264 : :
5265 : 0 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (slp_root);
5266 : :
5267 : 0 : vect_free_slp_instance (instance);
5268 : 0 : limit = max_tree_size;
5269 : 0 : bool res = vect_analyze_slp_instance (vinfo, bst_map,
5270 : : stmt_info,
5271 : : slp_inst_kind_store,
5272 : : max_tree_size, &limit,
5273 : : true);
5274 : 0 : gcc_assert (res);
5275 : 0 : auto new_inst = LOOP_VINFO_SLP_INSTANCES (vinfo).pop ();
5276 : 0 : LOOP_VINFO_SLP_INSTANCES (vinfo)[i] = new_inst;
5277 : : }
5278 : : }
5279 : 0 : }
5280 : :
5281 : : /* When we end up with load permutations that we cannot possibly handle,
5282 : : like those requiring three vector inputs, lower them using interleaving
5283 : : like schemes. */
5284 : 909221 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
5285 : : {
5286 : 278208 : vect_lower_load_permutations (loop_vinfo, bst_map, force_single_lane);
5287 : 278208 : if (dump_enabled_p ())
5288 : : {
5289 : 16732 : dump_printf_loc (MSG_NOTE, vect_location,
5290 : : "SLP graph after lowering permutations:\n");
5291 : 16732 : hash_set<slp_tree> visited;
5292 : 77994 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5293 : 27843 : vect_print_slp_graph (MSG_NOTE, vect_location,
5294 : : SLP_INSTANCE_TREE (instance), visited);
5295 : 16732 : }
5296 : : }
5297 : :
5298 : 909221 : release_scalar_stmts_to_slp_tree_map (bst_map);
5299 : :
5300 : 909221 : if (pattern_found && dump_enabled_p ())
5301 : : {
5302 : 23 : dump_printf_loc (MSG_NOTE, vect_location,
5303 : : "Pattern matched SLP tree\n");
5304 : 23 : hash_set<slp_tree> visited;
5305 : 143 : FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (vinfo), i, instance)
5306 : 74 : vect_print_slp_graph (MSG_NOTE, vect_location,
5307 : : SLP_INSTANCE_TREE (instance), visited);
5308 : 23 : }
5309 : :
5310 : 909221 : return opt_result::success ();
5311 : 909221 : }
5312 : :
5313 : : /* Estimates the cost of inserting layout changes into the SLP graph.
5314 : : It can also say that the insertion is impossible. */
5315 : :
5316 : : struct slpg_layout_cost
5317 : : {
5318 : 9620328 : slpg_layout_cost () = default;
5319 : : slpg_layout_cost (sreal, bool);
5320 : :
5321 : 390121 : static slpg_layout_cost impossible () { return { sreal::max (), 0 }; }
5322 : 4219181 : bool is_possible () const { return depth != sreal::max (); }
5323 : :
5324 : : bool operator== (const slpg_layout_cost &) const;
5325 : : bool operator!= (const slpg_layout_cost &) const;
5326 : :
5327 : : bool is_better_than (const slpg_layout_cost &, bool) const;
5328 : :
5329 : : void add_parallel_cost (const slpg_layout_cost &);
5330 : : void add_serial_cost (const slpg_layout_cost &);
5331 : : void split (unsigned int);
5332 : :
5333 : : /* The longest sequence of layout changes needed during any traversal
5334 : : of the partition dag, weighted by execution frequency.
5335 : :
5336 : : This is the most important metric when optimizing for speed, since
5337 : : it helps to ensure that we keep the number of operations on
5338 : : critical paths to a minimum. */
5339 : : sreal depth = 0;
5340 : :
5341 : : /* An estimate of the total number of operations needed. It is weighted by
5342 : : execution frequency when optimizing for speed but not when optimizing for
5343 : : size. In order to avoid double-counting, a node with a fanout of N will
5344 : : distribute 1/N of its total cost to each successor.
5345 : :
5346 : : This is the most important metric when optimizing for size, since
5347 : : it helps to keep the total number of operations to a minimum, */
5348 : : sreal total = 0;
5349 : : };
5350 : :
5351 : : /* Construct costs for a node with weight WEIGHT. A higher weight
5352 : : indicates more frequent execution. IS_FOR_SIZE is true if we are
5353 : : optimizing for size rather than speed. */
5354 : :
5355 : 957586 : slpg_layout_cost::slpg_layout_cost (sreal weight, bool is_for_size)
5356 : 958501 : : depth (weight), total (is_for_size && weight > 0 ? 1 : weight)
5357 : : {
5358 : 957586 : }
5359 : :
5360 : : bool
5361 : 0 : slpg_layout_cost::operator== (const slpg_layout_cost &other) const
5362 : : {
5363 : 0 : return depth == other.depth && total == other.total;
5364 : : }
5365 : :
5366 : : bool
5367 : 0 : slpg_layout_cost::operator!= (const slpg_layout_cost &other) const
5368 : : {
5369 : 0 : return !operator== (other);
5370 : : }
5371 : :
5372 : : /* Return true if these costs are better than OTHER. IS_FOR_SIZE is
5373 : : true if we are optimizing for size rather than speed. */
5374 : :
5375 : : bool
5376 : 254304 : slpg_layout_cost::is_better_than (const slpg_layout_cost &other,
5377 : : bool is_for_size) const
5378 : : {
5379 : 254304 : if (is_for_size)
5380 : : {
5381 : 404 : if (total != other.total)
5382 : 170 : return total < other.total;
5383 : 234 : return depth < other.depth;
5384 : : }
5385 : : else
5386 : : {
5387 : 253900 : if (depth != other.depth)
5388 : 115940 : return depth < other.depth;
5389 : 137960 : return total < other.total;
5390 : : }
5391 : : }
5392 : :
5393 : : /* Increase the costs to account for something with cost INPUT_COST
5394 : : happening in parallel with the current costs. */
5395 : :
5396 : : void
5397 : 272449 : slpg_layout_cost::add_parallel_cost (const slpg_layout_cost &input_cost)
5398 : : {
5399 : 272449 : depth = std::max (depth, input_cost.depth);
5400 : 272449 : total += input_cost.total;
5401 : 272449 : }
5402 : :
5403 : : /* Increase the costs to account for something with cost INPUT_COST
5404 : : happening in series with the current costs. */
5405 : :
5406 : : void
5407 : 1188523 : slpg_layout_cost::add_serial_cost (const slpg_layout_cost &other)
5408 : : {
5409 : 1188523 : depth += other.depth;
5410 : 1188523 : total += other.total;
5411 : 1188523 : }
5412 : :
5413 : : /* Split the total cost among TIMES successors or predecessors. */
5414 : :
5415 : : void
5416 : 905541 : slpg_layout_cost::split (unsigned int times)
5417 : : {
5418 : 905541 : if (times > 1)
5419 : 289783 : total /= times;
5420 : 905541 : }
5421 : :
5422 : : /* Information about one node in the SLP graph, for use during
5423 : : vect_optimize_slp_pass. */
5424 : :
5425 : : struct slpg_vertex
5426 : : {
5427 : 9397015 : slpg_vertex (slp_tree node_) : node (node_) {}
5428 : :
5429 : : /* The node itself. */
5430 : : slp_tree node;
5431 : :
5432 : : /* Which partition the node belongs to, or -1 if none. Nodes outside of
5433 : : partitions are flexible; they can have whichever layout consumers
5434 : : want them to have. */
5435 : : int partition = -1;
5436 : :
5437 : : /* The number of nodes that directly use the result of this one
5438 : : (i.e. the number of nodes that count this one as a child). */
5439 : : unsigned int out_degree = 0;
5440 : :
5441 : : /* The execution frequency of the node. */
5442 : : sreal weight = 0;
5443 : :
5444 : : /* The total execution frequency of all nodes that directly use the
5445 : : result of this one. */
5446 : : sreal out_weight = 0;
5447 : : };
5448 : :
5449 : : /* Information about one partition of the SLP graph, for use during
5450 : : vect_optimize_slp_pass. */
5451 : :
5452 : : struct slpg_partition_info
5453 : : {
5454 : : /* The nodes in the partition occupy indices [NODE_BEGIN, NODE_END)
5455 : : of m_partitioned_nodes. */
5456 : : unsigned int node_begin = 0;
5457 : : unsigned int node_end = 0;
5458 : :
5459 : : /* Which layout we've chosen to use for this partition, or -1 if
5460 : : we haven't picked one yet. */
5461 : : int layout = -1;
5462 : :
5463 : : /* The number of predecessors and successors in the partition dag.
5464 : : The predecessors always have lower partition numbers and the
5465 : : successors always have higher partition numbers.
5466 : :
5467 : : Note that the directions of these edges are not necessarily the
5468 : : same as in the data flow graph. For example, if an SCC has separate
5469 : : partitions for an inner loop and an outer loop, the inner loop's
5470 : : partition will have at least two incoming edges from the outer loop's
5471 : : partition: one for a live-in value and one for a live-out value.
5472 : : In data flow terms, one of these edges would also be from the outer loop
5473 : : to the inner loop, but the other would be in the opposite direction. */
5474 : : unsigned int in_degree = 0;
5475 : : unsigned int out_degree = 0;
5476 : : };
5477 : :
5478 : : /* Information about the costs of using a particular layout for a
5479 : : particular partition. It can also say that the combination is
5480 : : impossible. */
5481 : :
5482 : : struct slpg_partition_layout_costs
5483 : : {
5484 : 1245566 : bool is_possible () const { return internal_cost.is_possible (); }
5485 : 46183 : void mark_impossible () { internal_cost = slpg_layout_cost::impossible (); }
5486 : :
5487 : : /* The costs inherited from predecessor partitions. */
5488 : : slpg_layout_cost in_cost;
5489 : :
5490 : : /* The inherent cost of the layout within the node itself. For example,
5491 : : this is nonzero for a load if choosing a particular layout would require
5492 : : the load to permute the loaded elements. It is nonzero for a
5493 : : VEC_PERM_EXPR if the permutation cannot be eliminated or converted
5494 : : to full-vector moves. */
5495 : : slpg_layout_cost internal_cost;
5496 : :
5497 : : /* The costs inherited from successor partitions. */
5498 : : slpg_layout_cost out_cost;
5499 : : };
5500 : :
5501 : : /* This class tries to optimize the layout of vectors in order to avoid
5502 : : unnecessary shuffling. At the moment, the set of possible layouts are
5503 : : restricted to bijective permutations.
5504 : :
5505 : : The goal of the pass depends on whether we're optimizing for size or
5506 : : for speed. When optimizing for size, the goal is to reduce the overall
5507 : : number of layout changes (including layout changes implied by things
5508 : : like load permutations). When optimizing for speed, the goal is to
5509 : : reduce the maximum latency attributable to layout changes on any
5510 : : non-cyclical path through the data flow graph.
5511 : :
5512 : : For example, when optimizing a loop nest for speed, we will prefer
5513 : : to make layout changes outside of a loop rather than inside of a loop,
5514 : : and will prefer to make layout changes in parallel rather than serially,
5515 : : even if that increases the overall number of layout changes.
5516 : :
5517 : : The high-level procedure is:
5518 : :
5519 : : (1) Build a graph in which edges go from uses (parents) to definitions
5520 : : (children).
5521 : :
5522 : : (2) Divide the graph into a dag of strongly-connected components (SCCs).
5523 : :
5524 : : (3) When optimizing for speed, partition the nodes in each SCC based
5525 : : on their containing cfg loop. When optimizing for size, treat
5526 : : each SCC as a single partition.
5527 : :
5528 : : This gives us a dag of partitions. The goal is now to assign a
5529 : : layout to each partition.
5530 : :
5531 : : (4) Construct a set of vector layouts that are worth considering.
5532 : : Record which nodes must keep their current layout.
5533 : :
5534 : : (5) Perform a forward walk over the partition dag (from loads to stores)
5535 : : accumulating the "forward" cost of using each layout. When visiting
5536 : : each partition, assign a tentative choice of layout to the partition
5537 : : and use that choice when calculating the cost of using a different
5538 : : layout in successor partitions.
5539 : :
5540 : : (6) Perform a backward walk over the partition dag (from stores to loads),
5541 : : accumulating the "backward" cost of using each layout. When visiting
5542 : : each partition, make a final choice of layout for that partition based
5543 : : on the accumulated forward costs (from (5)) and backward costs
5544 : : (from (6)).
5545 : :
5546 : : (7) Apply the chosen layouts to the SLP graph.
5547 : :
5548 : : For example, consider the SLP statements:
5549 : :
5550 : : S1: a_1 = load
5551 : : loop:
5552 : : S2: a_2 = PHI<a_1, a_3>
5553 : : S3: b_1 = load
5554 : : S4: a_3 = a_2 + b_1
5555 : : exit:
5556 : : S5: a_4 = PHI<a_3>
5557 : : S6: store a_4
5558 : :
5559 : : S2 and S4 form an SCC and are part of the same loop. Every other
5560 : : statement is in a singleton SCC. In this example there is a one-to-one
5561 : : mapping between SCCs and partitions and the partition dag looks like this;
5562 : :
5563 : : S1 S3
5564 : : \ /
5565 : : S2+S4
5566 : : |
5567 : : S5
5568 : : |
5569 : : S6
5570 : :
5571 : : S2, S3 and S4 will have a higher execution frequency than the other
5572 : : statements, so when optimizing for speed, the goal is to avoid any
5573 : : layout changes:
5574 : :
5575 : : - within S3
5576 : : - within S2+S4
5577 : : - on the S3->S2+S4 edge
5578 : :
5579 : : For example, if S3 was originally a reversing load, the goal of the
5580 : : pass is to make it an unreversed load and change the layout on the
5581 : : S1->S2+S4 and S2+S4->S5 edges to compensate. (Changing the layout
5582 : : on S1->S2+S4 and S5->S6 would also be acceptable.)
5583 : :
5584 : : The difference between SCCs and partitions becomes important if we
5585 : : add an outer loop:
5586 : :
5587 : : S1: a_1 = ...
5588 : : loop1:
5589 : : S2: a_2 = PHI<a_1, a_6>
5590 : : S3: b_1 = load
5591 : : S4: a_3 = a_2 + b_1
5592 : : loop2:
5593 : : S5: a_4 = PHI<a_3, a_5>
5594 : : S6: c_1 = load
5595 : : S7: a_5 = a_4 + c_1
5596 : : exit2:
5597 : : S8: a_6 = PHI<a_5>
5598 : : S9: store a_6
5599 : : exit1:
5600 : :
5601 : : Here, S2, S4, S5, S7 and S8 form a single SCC. However, when optimizing
5602 : : for speed, we usually do not want restrictions in the outer loop to "infect"
5603 : : the decision for the inner loop. For example, if an outer-loop node
5604 : : in the SCC contains a statement with a fixed layout, that should not
5605 : : prevent the inner loop from using a different layout. Conversely,
5606 : : the inner loop should not dictate a layout to the outer loop: if the
5607 : : outer loop does a lot of computation, then it may not be efficient to
5608 : : do all of that computation in the inner loop's preferred layout.
5609 : :
5610 : : So when optimizing for speed, we partition the SCC into S2+S4+S8 (outer)
5611 : : and S5+S7 (inner). We also try to arrange partitions so that:
5612 : :
5613 : : - the partition for an outer loop comes before the partition for
5614 : : an inner loop
5615 : :
5616 : : - if a sibling loop A dominates a sibling loop B, A's partition
5617 : : comes before B's
5618 : :
5619 : : This gives the following partition dag for the example above:
5620 : :
5621 : : S1 S3
5622 : : \ /
5623 : : S2+S4+S8 S6
5624 : : | \\ /
5625 : : | S5+S7
5626 : : |
5627 : : S9
5628 : :
5629 : : There are two edges from S2+S4+S8 to S5+S7: one for the edge S4->S5 and
5630 : : one for a reversal of the edge S7->S8.
5631 : :
5632 : : The backward walk picks a layout for S5+S7 before S2+S4+S8. The choice
5633 : : for S2+S4+S8 therefore has to balance the cost of using the outer loop's
5634 : : preferred layout against the cost of changing the layout on entry to the
5635 : : inner loop (S4->S5) and on exit from the inner loop (S7->S8 reversed).
5636 : :
5637 : : Although this works well when optimizing for speed, it has the downside
5638 : : when optimizing for size that the choice of layout for S5+S7 is completely
5639 : : independent of S9, which lessens the chance of reducing the overall number
5640 : : of permutations. We therefore do not partition SCCs when optimizing
5641 : : for size.
5642 : :
5643 : : To give a concrete example of the difference between optimizing
5644 : : for size and speed, consider:
5645 : :
5646 : : a[0] = (b[1] << c[3]) - d[1];
5647 : : a[1] = (b[0] << c[2]) - d[0];
5648 : : a[2] = (b[3] << c[1]) - d[3];
5649 : : a[3] = (b[2] << c[0]) - d[2];
5650 : :
5651 : : There are three different layouts here: one for a, one for b and d,
5652 : : and one for c. When optimizing for speed it is better to permute each
5653 : : of b, c and d into the order required by a, since those permutations
5654 : : happen in parallel. But when optimizing for size, it is better to:
5655 : :
5656 : : - permute c into the same order as b
5657 : : - do the arithmetic
5658 : : - permute the result into the order required by a
5659 : :
5660 : : This gives 2 permutations rather than 3. */
5661 : :
5662 : : class vect_optimize_slp_pass
5663 : : {
5664 : : public:
5665 : 620472 : vect_optimize_slp_pass (vec_info *vinfo) : m_vinfo (vinfo) {}
5666 : : void run ();
5667 : :
5668 : : private:
5669 : : /* Graph building. */
5670 : : struct loop *containing_loop (slp_tree);
5671 : : bool is_cfg_latch_edge (graph_edge *);
5672 : : void build_vertices (hash_set<slp_tree> &, slp_tree);
5673 : : void build_vertices ();
5674 : : void build_graph ();
5675 : :
5676 : : /* Partitioning. */
5677 : : void create_partitions ();
5678 : : template<typename T> void for_each_partition_edge (unsigned int, T);
5679 : :
5680 : : /* Layout selection. */
5681 : : bool is_compatible_layout (slp_tree, unsigned int);
5682 : : int change_layout_cost (slp_tree, unsigned int, unsigned int);
5683 : : slpg_partition_layout_costs &partition_layout_costs (unsigned int,
5684 : : unsigned int);
5685 : : void change_vec_perm_layout (slp_tree, lane_permutation_t &,
5686 : : int, unsigned int);
5687 : : int internal_node_cost (slp_tree, int, unsigned int);
5688 : : void start_choosing_layouts ();
5689 : :
5690 : : /* Cost propagation. */
5691 : : slpg_layout_cost edge_layout_cost (graph_edge *, unsigned int,
5692 : : unsigned int, unsigned int);
5693 : : slpg_layout_cost total_in_cost (unsigned int);
5694 : : slpg_layout_cost forward_cost (graph_edge *, unsigned int, unsigned int);
5695 : : slpg_layout_cost backward_cost (graph_edge *, unsigned int, unsigned int);
5696 : : void forward_pass ();
5697 : : void backward_pass ();
5698 : :
5699 : : /* Rematerialization. */
5700 : : slp_tree get_result_with_layout (slp_tree, unsigned int);
5701 : : void materialize ();
5702 : :
5703 : : /* Clean-up. */
5704 : : void remove_redundant_permutations ();
5705 : :
5706 : : /* Masked load lanes discovery. */
5707 : : void decide_masked_load_lanes ();
5708 : :
5709 : : void dump ();
5710 : :
5711 : : vec_info *m_vinfo;
5712 : :
5713 : : /* True if we should optimize the graph for size, false if we should
5714 : : optimize it for speed. (It wouldn't be easy to make this decision
5715 : : more locally.) */
5716 : : bool m_optimize_size;
5717 : :
5718 : : /* A graph of all SLP nodes, with edges leading from uses to definitions.
5719 : : In other words, a node's predecessors are its slp_tree parents and
5720 : : a node's successors are its slp_tree children. */
5721 : : graph *m_slpg = nullptr;
5722 : :
5723 : : /* The vertices of M_SLPG, indexed by slp_tree::vertex. */
5724 : : auto_vec<slpg_vertex> m_vertices;
5725 : :
5726 : : /* The list of all leaves of M_SLPG. such as external definitions, constants,
5727 : : and loads. */
5728 : : auto_vec<int> m_leafs;
5729 : :
5730 : : /* This array has one entry for every vector layout that we're considering.
5731 : : Element 0 is null and indicates "no change". Other entries describe
5732 : : permutations that are inherent in the current graph and that we would
5733 : : like to reverse if possible.
5734 : :
5735 : : For example, a permutation { 1, 2, 3, 0 } means that something has
5736 : : effectively been permuted in that way, such as a load group
5737 : : { a[1], a[2], a[3], a[0] } (viewed as a permutation of a[0:3]).
5738 : : We'd then like to apply the reverse permutation { 3, 0, 1, 2 }
5739 : : in order to put things "back" in order. */
5740 : : auto_vec<vec<unsigned> > m_perms;
5741 : :
5742 : : /* A partitioning of the nodes for which a layout must be chosen.
5743 : : Each partition represents an <SCC, cfg loop> pair; that is,
5744 : : nodes in different SCCs belong to different partitions, and nodes
5745 : : within an SCC can be further partitioned according to a containing
5746 : : cfg loop. Partition <SCC1, L1> comes before <SCC2, L2> if:
5747 : :
5748 : : - SCC1 != SCC2 and SCC1 is a predecessor of SCC2 in a forward walk
5749 : : from leaves (such as loads) to roots (such as stores).
5750 : :
5751 : : - SCC1 == SCC2 and L1's header strictly dominates L2's header. */
5752 : : auto_vec<slpg_partition_info> m_partitions;
5753 : :
5754 : : /* The list of all nodes for which a layout must be chosen. Nodes for
5755 : : partition P come before the nodes for partition P+1. Nodes within a
5756 : : partition are in reverse postorder. */
5757 : : auto_vec<unsigned int> m_partitioned_nodes;
5758 : :
5759 : : /* Index P * num-layouts + L contains the cost of using layout L
5760 : : for partition P. */
5761 : : auto_vec<slpg_partition_layout_costs> m_partition_layout_costs;
5762 : :
5763 : : /* Index N * num-layouts + L, if nonnull, is a node that provides the
5764 : : original output of node N adjusted to have layout L. */
5765 : : auto_vec<slp_tree> m_node_layouts;
5766 : : };
5767 : :
5768 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph.
5769 : : Also record whether we should optimize anything for speed rather
5770 : : than size. */
5771 : :
5772 : : void
5773 : 10056835 : vect_optimize_slp_pass::build_vertices (hash_set<slp_tree> &visited,
5774 : : slp_tree node)
5775 : : {
5776 : 10056835 : unsigned i;
5777 : 10056835 : slp_tree child;
5778 : :
5779 : 10056835 : if (visited.add (node))
5780 : 10056835 : return;
5781 : :
5782 : 9397015 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
5783 : : {
5784 : 6819196 : basic_block bb = gimple_bb (vect_orig_stmt (rep)->stmt);
5785 : 6272924 : if (optimize_bb_for_speed_p (bb))
5786 : 6135842 : m_optimize_size = false;
5787 : : }
5788 : :
5789 : 9397015 : node->vertex = m_vertices.length ();
5790 : 9397015 : m_vertices.safe_push (slpg_vertex (node));
5791 : :
5792 : 9397015 : bool leaf = true;
5793 : 9397015 : bool force_leaf = false;
5794 : 17564226 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
5795 : 8167211 : if (child)
5796 : : {
5797 : 7032731 : leaf = false;
5798 : 7032731 : build_vertices (visited, child);
5799 : : }
5800 : : else
5801 : : force_leaf = true;
5802 : : /* Since SLP discovery works along use-def edges all cycles have an
5803 : : entry - but there's the exception of cycles where we do not handle
5804 : : the entry explicitely (but with a NULL SLP node), like some reductions
5805 : : and inductions. Force those SLP PHIs to act as leafs to make them
5806 : : backwards reachable. */
5807 : 9397015 : if (leaf || force_leaf)
5808 : 4765210 : m_leafs.safe_push (node->vertex);
5809 : : }
5810 : :
5811 : : /* Fill the vertices and leafs vector with all nodes in the SLP graph. */
5812 : :
5813 : : void
5814 : 1240944 : vect_optimize_slp_pass::build_vertices ()
5815 : : {
5816 : 1240944 : hash_set<slp_tree> visited;
5817 : 1240944 : unsigned i;
5818 : 1240944 : slp_instance instance;
5819 : 1240944 : m_vertices.truncate (0);
5820 : 1240944 : m_leafs.truncate (0);
5821 : 6746936 : FOR_EACH_VEC_ELT (m_vinfo->slp_instances, i, instance)
5822 : 3024104 : build_vertices (visited, SLP_INSTANCE_TREE (instance));
5823 : 1240944 : }
5824 : :
5825 : : /* Apply (reverse) bijectite PERM to VEC. */
5826 : :
5827 : : template <class T>
5828 : : static void
5829 : 161398 : vect_slp_permute (vec<unsigned> perm,
5830 : : vec<T> &vec, bool reverse)
5831 : : {
5832 : 161398 : auto_vec<T, 64> saved;
5833 : 161398 : saved.create (vec.length ());
5834 : 547730 : for (unsigned i = 0; i < vec.length (); ++i)
5835 : 386332 : saved.quick_push (vec[i]);
5836 : :
5837 : 161398 : if (reverse)
5838 : : {
5839 : 1086197 : for (unsigned i = 0; i < vec.length (); ++i)
5840 : 384936 : vec[perm[i]] = saved[i];
5841 : 545696 : for (unsigned i = 0; i < vec.length (); ++i)
5842 : 661490 : gcc_assert (vec[perm[i]] == saved[i]);
5843 : : }
5844 : : else
5845 : : {
5846 : 4068 : for (unsigned i = 0; i < vec.length (); ++i)
5847 : 1396 : vec[i] = saved[perm[i]];
5848 : 162794 : for (unsigned i = 0; i < vec.length (); ++i)
5849 : 2094 : gcc_assert (vec[i] == saved[perm[i]]);
5850 : : }
5851 : 161398 : }
5852 : :
5853 : : /* Return the cfg loop that contains NODE. */
5854 : :
5855 : : struct loop *
5856 : 3394168 : vect_optimize_slp_pass::containing_loop (slp_tree node)
5857 : : {
5858 : 3394168 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
5859 : 3394168 : if (!rep)
5860 : 4718 : return ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father;
5861 : 3669886 : return gimple_bb (vect_orig_stmt (rep)->stmt)->loop_father;
5862 : : }
5863 : :
5864 : : /* Return true if UD (an edge from a use to a definition) is associated
5865 : : with a loop latch edge in the cfg. */
5866 : :
5867 : : bool
5868 : 7032731 : vect_optimize_slp_pass::is_cfg_latch_edge (graph_edge *ud)
5869 : : {
5870 : 7032731 : slp_tree use = m_vertices[ud->src].node;
5871 : 7032731 : slp_tree def = m_vertices[ud->dest].node;
5872 : 7032731 : if ((SLP_TREE_DEF_TYPE (use) != vect_internal_def
5873 : 7032731 : || SLP_TREE_CODE (use) == VEC_PERM_EXPR)
5874 : 6769894 : || SLP_TREE_DEF_TYPE (def) != vect_internal_def)
5875 : : return false;
5876 : :
5877 : 3656316 : stmt_vec_info use_rep = vect_orig_stmt (SLP_TREE_REPRESENTATIVE (use));
5878 : 3656316 : return (is_a<gphi *> (use_rep->stmt)
5879 : 308350 : && bb_loop_header_p (gimple_bb (use_rep->stmt))
5880 : 3799422 : && containing_loop (def) == containing_loop (use));
5881 : : }
5882 : :
5883 : : /* Build the graph. Mark edges that correspond to cfg loop latch edges with
5884 : : a nonnull data field. */
5885 : :
5886 : : void
5887 : 1240944 : vect_optimize_slp_pass::build_graph ()
5888 : : {
5889 : 1240944 : m_optimize_size = true;
5890 : 1240944 : build_vertices ();
5891 : :
5892 : 2481888 : m_slpg = new_graph (m_vertices.length ());
5893 : 13119847 : for (slpg_vertex &v : m_vertices)
5894 : 28073660 : for (slp_tree child : SLP_TREE_CHILDREN (v.node))
5895 : 8167211 : if (child)
5896 : : {
5897 : 7032731 : graph_edge *ud = add_edge (m_slpg, v.node->vertex, child->vertex);
5898 : 7032731 : if (is_cfg_latch_edge (ud))
5899 : 134778 : ud->data = this;
5900 : : }
5901 : 1240944 : }
5902 : :
5903 : : /* Return true if E corresponds to a loop latch edge in the cfg. */
5904 : :
5905 : : static bool
5906 : 3583566 : skip_cfg_latch_edges (graph_edge *e)
5907 : : {
5908 : 3583566 : return e->data;
5909 : : }
5910 : :
5911 : : /* Create the node partitions. */
5912 : :
5913 : : void
5914 : 620472 : vect_optimize_slp_pass::create_partitions ()
5915 : : {
5916 : : /* Calculate a postorder of the graph, ignoring edges that correspond
5917 : : to natural latch edges in the cfg. Reading the vector from the end
5918 : : to the beginning gives the reverse postorder. */
5919 : 620472 : auto_vec<int> initial_rpo;
5920 : 1240944 : graphds_dfs (m_slpg, &m_leafs[0], m_leafs.length (), &initial_rpo,
5921 : : false, NULL, skip_cfg_latch_edges);
5922 : 1861416 : gcc_assert (initial_rpo.length () == m_vertices.length ());
5923 : :
5924 : : /* Calculate the strongly connected components of the graph. */
5925 : 620472 : auto_vec<int> scc_grouping;
5926 : 620472 : unsigned int num_sccs = graphds_scc (m_slpg, NULL, NULL, &scc_grouping);
5927 : :
5928 : : /* Create a new index order in which all nodes from the same SCC are
5929 : : consecutive. Use scc_pos to record the index of the first node in
5930 : : each SCC. */
5931 : 620472 : auto_vec<unsigned int> scc_pos (num_sccs);
5932 : 620472 : int last_component = -1;
5933 : 620472 : unsigned int node_count = 0;
5934 : 6559737 : for (unsigned int node_i : scc_grouping)
5935 : : {
5936 : 4698321 : if (last_component != m_slpg->vertices[node_i].component)
5937 : : {
5938 : 4621100 : last_component = m_slpg->vertices[node_i].component;
5939 : 9242200 : gcc_assert (last_component == int (scc_pos.length ()));
5940 : 4621100 : scc_pos.quick_push (node_count);
5941 : : }
5942 : 4698321 : node_count += 1;
5943 : : }
5944 : 1240944 : gcc_assert (node_count == initial_rpo.length ()
5945 : : && last_component + 1 == int (num_sccs));
5946 : :
5947 : : /* Use m_partitioned_nodes to group nodes into SCC order, with the nodes
5948 : : inside each SCC following the RPO we calculated above. The fact that
5949 : : we ignored natural latch edges when calculating the RPO should ensure
5950 : : that, for natural loop nests:
5951 : :
5952 : : - the first node that we encounter in a cfg loop is the loop header phi
5953 : : - the loop header phis are in dominance order
5954 : :
5955 : : Arranging for this is an optimization (see below) rather than a
5956 : : correctness issue. Unnatural loops with a tangled mess of backedges
5957 : : will still work correctly, but might give poorer results.
5958 : :
5959 : : Also update scc_pos so that it gives 1 + the index of the last node
5960 : : in the SCC. */
5961 : 620472 : m_partitioned_nodes.safe_grow (node_count);
5962 : 5939265 : for (unsigned int old_i = initial_rpo.length (); old_i-- > 0;)
5963 : : {
5964 : 4698321 : unsigned int node_i = initial_rpo[old_i];
5965 : 4698321 : unsigned int new_i = scc_pos[m_slpg->vertices[node_i].component]++;
5966 : 4698321 : m_partitioned_nodes[new_i] = node_i;
5967 : : }
5968 : :
5969 : : /* When optimizing for speed, partition each SCC based on the containing
5970 : : cfg loop. The order we constructed above should ensure that, for natural
5971 : : cfg loops, we'll create sub-SCC partitions for outer loops before
5972 : : the corresponding sub-SCC partitions for inner loops. Similarly,
5973 : : when one sibling loop A dominates another sibling loop B, we should
5974 : : create a sub-SCC partition for A before a sub-SCC partition for B.
5975 : :
5976 : : As above, nothing depends for correctness on whether this achieves
5977 : : a natural nesting, but we should get better results when it does. */
5978 : 1240944 : m_partitions.reserve (m_vertices.length ());
5979 : 620472 : unsigned int next_partition_i = 0;
5980 : 620472 : hash_map<struct loop *, int> loop_partitions;
5981 : 620472 : unsigned int rpo_begin = 0;
5982 : 620472 : unsigned int num_partitioned_nodes = 0;
5983 : 6482516 : for (unsigned int rpo_end : scc_pos)
5984 : : {
5985 : 4621100 : loop_partitions.empty ();
5986 : : unsigned int partition_i = next_partition_i;
5987 : 9319421 : for (unsigned int rpo_i = rpo_begin; rpo_i < rpo_end; ++rpo_i)
5988 : : {
5989 : : /* Handle externals and constants optimistically throughout.
5990 : : But treat existing vectors as fixed since we do not handle
5991 : : permuting them. */
5992 : 4698321 : unsigned int node_i = m_partitioned_nodes[rpo_i];
5993 : 4698321 : auto &vertex = m_vertices[node_i];
5994 : 4698321 : if ((SLP_TREE_DEF_TYPE (vertex.node) == vect_external_def
5995 : 553627 : && !SLP_TREE_VEC_DEFS (vertex.node).exists ())
5996 : 4700732 : || SLP_TREE_DEF_TYPE (vertex.node) == vect_constant_def)
5997 : 1557327 : vertex.partition = -1;
5998 : : else
5999 : : {
6000 : 3140994 : bool existed;
6001 : 3140994 : if (m_optimize_size)
6002 : 33038 : existed = next_partition_i > partition_i;
6003 : : else
6004 : : {
6005 : 3107956 : struct loop *loop = containing_loop (vertex.node);
6006 : 3107956 : auto &entry = loop_partitions.get_or_insert (loop, &existed);
6007 : 3107956 : if (!existed)
6008 : 3031706 : entry = next_partition_i;
6009 : 3107956 : partition_i = entry;
6010 : : }
6011 : 3140994 : if (!existed)
6012 : : {
6013 : 3064661 : m_partitions.quick_push (slpg_partition_info ());
6014 : 3064661 : next_partition_i += 1;
6015 : : }
6016 : 3140994 : vertex.partition = partition_i;
6017 : 3140994 : num_partitioned_nodes += 1;
6018 : 3140994 : m_partitions[partition_i].node_end += 1;
6019 : : }
6020 : : }
6021 : 4621100 : rpo_begin = rpo_end;
6022 : : }
6023 : :
6024 : : /* Assign ranges of consecutive node indices to each partition,
6025 : : in partition order. Start with node_end being the same as
6026 : : node_begin so that the next loop can use it as a counter. */
6027 : 620472 : unsigned int node_begin = 0;
6028 : 4926077 : for (auto &partition : m_partitions)
6029 : : {
6030 : 3064661 : partition.node_begin = node_begin;
6031 : 3064661 : node_begin += partition.node_end;
6032 : 3064661 : partition.node_end = partition.node_begin;
6033 : : }
6034 : 620472 : gcc_assert (node_begin == num_partitioned_nodes);
6035 : :
6036 : : /* Finally build the list of nodes in partition order. */
6037 : 620472 : m_partitioned_nodes.truncate (num_partitioned_nodes);
6038 : 5318793 : for (unsigned int node_i = 0; node_i < m_vertices.length (); ++node_i)
6039 : : {
6040 : 4698321 : int partition_i = m_vertices[node_i].partition;
6041 : 4698321 : if (partition_i >= 0)
6042 : : {
6043 : 3140994 : unsigned int order_i = m_partitions[partition_i].node_end++;
6044 : 3140994 : m_partitioned_nodes[order_i] = node_i;
6045 : : }
6046 : : }
6047 : 620472 : }
6048 : :
6049 : : /* Look for edges from earlier partitions into node NODE_I and edges from
6050 : : node NODE_I into later partitions. Call:
6051 : :
6052 : : FN (ud, other_node_i)
6053 : :
6054 : : for each such use-to-def edge ud, where other_node_i is the node at the
6055 : : other end of the edge. */
6056 : :
6057 : : template<typename T>
6058 : : void
6059 : 3479907 : vect_optimize_slp_pass::for_each_partition_edge (unsigned int node_i, T fn)
6060 : : {
6061 : 3479907 : int partition_i = m_vertices[node_i].partition;
6062 : 3479907 : for (graph_edge *pred = m_slpg->vertices[node_i].pred;
6063 : 5719912 : pred; pred = pred->pred_next)
6064 : : {
6065 : 2240005 : int src_partition_i = m_vertices[pred->src].partition;
6066 : 2240005 : if (src_partition_i >= 0 && src_partition_i != partition_i)
6067 : 2083550 : fn (pred, pred->src);
6068 : : }
6069 : 3479907 : for (graph_edge *succ = m_slpg->vertices[node_i].succ;
6070 : 7421338 : succ; succ = succ->succ_next)
6071 : : {
6072 : 3941431 : int dest_partition_i = m_vertices[succ->dest].partition;
6073 : 3941431 : if (dest_partition_i >= 0 && dest_partition_i != partition_i)
6074 : 2091628 : fn (succ, succ->dest);
6075 : : }
6076 : 3479907 : }
6077 : :
6078 : : /* Return true if layout LAYOUT_I is compatible with the number of SLP lanes
6079 : : that NODE would operate on. This test is independent of NODE's actual
6080 : : operation. */
6081 : :
6082 : : bool
6083 : 1244848 : vect_optimize_slp_pass::is_compatible_layout (slp_tree node,
6084 : : unsigned int layout_i)
6085 : : {
6086 : 1244848 : if (layout_i == 0)
6087 : : return true;
6088 : :
6089 : 733374 : if (SLP_TREE_LANES (node) != m_perms[layout_i].length ())
6090 : 9131 : return false;
6091 : :
6092 : : return true;
6093 : : }
6094 : :
6095 : : /* Return the cost (in arbtirary units) of going from layout FROM_LAYOUT_I
6096 : : to layout TO_LAYOUT_I for a node like NODE. Return -1 if either of the
6097 : : layouts is incompatible with NODE or if the change is not possible for
6098 : : some other reason.
6099 : :
6100 : : The properties taken from NODE include the number of lanes and the
6101 : : vector type. The actual operation doesn't matter. */
6102 : :
6103 : : int
6104 : 533164 : vect_optimize_slp_pass::change_layout_cost (slp_tree node,
6105 : : unsigned int from_layout_i,
6106 : : unsigned int to_layout_i)
6107 : : {
6108 : 533164 : if (!is_compatible_layout (node, from_layout_i)
6109 : 533164 : || !is_compatible_layout (node, to_layout_i))
6110 : 648 : return -1;
6111 : :
6112 : 532516 : if (from_layout_i == to_layout_i)
6113 : : return 0;
6114 : :
6115 : 240205 : auto_vec<slp_tree, 1> children (1);
6116 : 240205 : children.quick_push (node);
6117 : 240205 : auto_lane_permutation_t perm (SLP_TREE_LANES (node));
6118 : 240205 : if (from_layout_i > 0)
6119 : 710019 : for (unsigned int i : m_perms[from_layout_i])
6120 : 322794 : perm.quick_push ({ 0, i });
6121 : : else
6122 : 371928 : for (unsigned int i = 0; i < SLP_TREE_LANES (node); ++i)
6123 : 260798 : perm.quick_push ({ 0, i });
6124 : 240205 : if (to_layout_i > 0)
6125 : 111561 : vect_slp_permute (m_perms[to_layout_i], perm, true);
6126 : 240205 : auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
6127 : : children, false);
6128 : 240205 : if (count >= 0)
6129 : 235161 : return MAX (count, 1);
6130 : :
6131 : : /* ??? In principle we could try changing via layout 0, giving two
6132 : : layout changes rather than 1. Doing that would require
6133 : : corresponding support in get_result_with_layout. */
6134 : : return -1;
6135 : 240205 : }
6136 : :
6137 : : /* Return the costs of assigning layout LAYOUT_I to partition PARTITION_I. */
6138 : :
6139 : : inline slpg_partition_layout_costs &
6140 : 832110 : vect_optimize_slp_pass::partition_layout_costs (unsigned int partition_i,
6141 : : unsigned int layout_i)
6142 : : {
6143 : 1664220 : return m_partition_layout_costs[partition_i * m_perms.length () + layout_i];
6144 : : }
6145 : :
6146 : : /* Change PERM in one of two ways:
6147 : :
6148 : : - if IN_LAYOUT_I < 0, accept input operand I in the layout that has been
6149 : : chosen for child I of NODE.
6150 : :
6151 : : - if IN_LAYOUT >= 0, accept all inputs operands with that layout.
6152 : :
6153 : : In both cases, arrange for the output to have layout OUT_LAYOUT_I */
6154 : :
6155 : : void
6156 : 23102 : vect_optimize_slp_pass::
6157 : : change_vec_perm_layout (slp_tree node, lane_permutation_t &perm,
6158 : : int in_layout_i, unsigned int out_layout_i)
6159 : : {
6160 : 138116 : for (auto &entry : perm)
6161 : : {
6162 : 68810 : int this_in_layout_i = in_layout_i;
6163 : 68810 : if (this_in_layout_i < 0)
6164 : : {
6165 : 52024 : slp_tree in_node = SLP_TREE_CHILDREN (node)[entry.first];
6166 : 52024 : unsigned int in_partition_i = m_vertices[in_node->vertex].partition;
6167 : 52024 : if (in_partition_i == -1u)
6168 : 326 : continue;
6169 : 51698 : this_in_layout_i = m_partitions[in_partition_i].layout;
6170 : : }
6171 : 68484 : if (this_in_layout_i > 0)
6172 : 14070 : entry.second = m_perms[this_in_layout_i][entry.second];
6173 : : }
6174 : 23102 : if (out_layout_i > 0)
6175 : 4769 : vect_slp_permute (m_perms[out_layout_i], perm, true);
6176 : 23102 : }
6177 : :
6178 : : /* Check whether the target allows NODE to be rearranged so that the node's
6179 : : output has layout OUT_LAYOUT_I. Return the cost of the change if so,
6180 : : in the same arbitrary units as for change_layout_cost. Return -1 otherwise.
6181 : :
6182 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I < 0, also check whether
6183 : : NODE can adapt to the layout changes that have (perhaps provisionally)
6184 : : been chosen for NODE's children, so that no extra permutations are
6185 : : needed on either the input or the output of NODE.
6186 : :
6187 : : If NODE is a VEC_PERM_EXPR and IN_LAYOUT_I >= 0, instead assume
6188 : : that all inputs will be forced into layout IN_LAYOUT_I beforehand.
6189 : :
6190 : : IN_LAYOUT_I has no meaning for other types of node.
6191 : :
6192 : : Keeping the node as-is is always valid. If the target doesn't appear
6193 : : to support the node as-is, but might realistically support other layouts,
6194 : : then layout 0 instead has the cost of a worst-case permutation. On the
6195 : : one hand, this ensures that every node has at least one valid layout,
6196 : : avoiding what would otherwise be an awkward special case. On the other,
6197 : : it still encourages the pass to change an invalid pre-existing layout
6198 : : choice into a valid one. */
6199 : :
6200 : : int
6201 : 181876 : vect_optimize_slp_pass::internal_node_cost (slp_tree node, int in_layout_i,
6202 : : unsigned int out_layout_i)
6203 : : {
6204 : 181876 : const int fallback_cost = 1;
6205 : :
6206 : 181876 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
6207 : : {
6208 : 20271 : auto_lane_permutation_t tmp_perm;
6209 : 20271 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
6210 : :
6211 : : /* Check that the child nodes support the chosen layout. Checking
6212 : : the first child is enough, since any second child would have the
6213 : : same shape. */
6214 : 20271 : auto first_child = SLP_TREE_CHILDREN (node)[0];
6215 : 20271 : if (in_layout_i > 0
6216 : 20271 : && !is_compatible_layout (first_child, in_layout_i))
6217 : : return -1;
6218 : :
6219 : 19625 : change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
6220 : 39250 : int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
6221 : : node, tmp_perm,
6222 : 19625 : SLP_TREE_CHILDREN (node),
6223 : : false);
6224 : 19625 : if (count < 0)
6225 : : {
6226 : 1484 : if (in_layout_i == 0 && out_layout_i == 0)
6227 : : {
6228 : : /* Use the fallback cost if the node could in principle support
6229 : : some nonzero layout for both the inputs and the outputs.
6230 : : Otherwise assume that the node will be rejected later
6231 : : and rebuilt from scalars. */
6232 : 353 : if (SLP_TREE_LANES (node) == SLP_TREE_LANES (first_child))
6233 : : return fallback_cost;
6234 : 279 : return 0;
6235 : : }
6236 : : return -1;
6237 : : }
6238 : :
6239 : : /* We currently have no way of telling whether the new layout is cheaper
6240 : : or more expensive than the old one. But at least in principle,
6241 : : it should be worth making zero permutations (whole-vector shuffles)
6242 : : cheaper than real permutations, in case the pass is able to remove
6243 : : the latter. */
6244 : 18141 : return count == 0 ? 0 : 1;
6245 : 20271 : }
6246 : :
6247 : 161605 : stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node);
6248 : 161605 : if (rep
6249 : 160546 : && STMT_VINFO_DATA_REF (rep)
6250 : 62100 : && DR_IS_READ (STMT_VINFO_DATA_REF (rep))
6251 : 205710 : && SLP_TREE_LOAD_PERMUTATION (node).exists ())
6252 : : {
6253 : 39491 : auto_load_permutation_t tmp_perm;
6254 : 39491 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
6255 : 39491 : if (out_layout_i > 0)
6256 : 16212 : vect_slp_permute (m_perms[out_layout_i], tmp_perm, true);
6257 : :
6258 : 39491 : poly_uint64 vf = 1;
6259 : 39491 : if (auto loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo))
6260 : 1904 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6261 : 39491 : unsigned int n_perms;
6262 : 39491 : if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
6263 : : nullptr, vf, true, false, &n_perms))
6264 : : {
6265 : 2331 : auto rep = SLP_TREE_REPRESENTATIVE (node);
6266 : 2331 : if (out_layout_i == 0)
6267 : : {
6268 : : /* Use the fallback cost if the load is an N-to-N permutation.
6269 : : Otherwise assume that the node will be rejected later
6270 : : and rebuilt from scalars. */
6271 : 1866 : if (STMT_VINFO_GROUPED_ACCESS (rep)
6272 : 3732 : && (DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (rep))
6273 : 1866 : == SLP_TREE_LANES (node)))
6274 : 1254 : return fallback_cost;
6275 : : return 0;
6276 : : }
6277 : : return -1;
6278 : : }
6279 : :
6280 : : /* See the comment above the corresponding VEC_PERM_EXPR handling. */
6281 : 37160 : return n_perms == 0 ? 0 : 1;
6282 : 39491 : }
6283 : :
6284 : : return 0;
6285 : : }
6286 : :
6287 : : /* Decide which element layouts we should consider using. Calculate the
6288 : : weights associated with inserting layout changes on partition edges.
6289 : : Also mark partitions that cannot change layout, by setting their
6290 : : layout to zero. */
6291 : :
6292 : : void
6293 : 620472 : vect_optimize_slp_pass::start_choosing_layouts ()
6294 : : {
6295 : : /* Used to assign unique permutation indices. */
6296 : 620472 : using perm_hash = unbounded_hashmap_traits<
6297 : : vec_free_hash_base<int_hash_base<unsigned>>,
6298 : : int_hash<int, -1, -2>
6299 : : >;
6300 : 620472 : hash_map<vec<unsigned>, int, perm_hash> layout_ids;
6301 : :
6302 : : /* Layout 0 is "no change". */
6303 : 620472 : m_perms.safe_push (vNULL);
6304 : :
6305 : : /* Create layouts from existing permutations. */
6306 : 620472 : auto_load_permutation_t tmp_perm;
6307 : 5002410 : for (unsigned int node_i : m_partitioned_nodes)
6308 : : {
6309 : : /* Leafs also double as entries to the reverse graph. Allow the
6310 : : layout of those to be changed. */
6311 : 3140994 : auto &vertex = m_vertices[node_i];
6312 : 3140994 : auto &partition = m_partitions[vertex.partition];
6313 : 3140994 : if (!m_slpg->vertices[node_i].succ)
6314 : 769606 : partition.layout = 0;
6315 : :
6316 : : /* Loads and VEC_PERM_EXPRs are the only things generating permutes. */
6317 : 3140994 : slp_tree node = vertex.node;
6318 : 3140994 : stmt_vec_info dr_stmt = SLP_TREE_REPRESENTATIVE (node);
6319 : 3140994 : slp_tree child;
6320 : 3140994 : unsigned HOST_WIDE_INT imin, imax = 0;
6321 : 3140994 : bool any_permute = false;
6322 : 3140994 : tmp_perm.truncate (0);
6323 : 3140994 : if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
6324 : : {
6325 : : /* If splitting out a SLP_TREE_LANE_PERMUTATION can make the node
6326 : : unpermuted, record a layout that reverses this permutation.
6327 : :
6328 : : We would need more work to cope with loads that are internally
6329 : : permuted and also have inputs (such as masks for
6330 : : IFN_MASK_LOADs). */
6331 : 392742 : gcc_assert (partition.layout == 0 && !m_slpg->vertices[node_i].succ);
6332 : 392742 : if (!STMT_VINFO_GROUPED_ACCESS (dr_stmt))
6333 : : {
6334 : 279512 : partition.layout = -1;
6335 : 3126089 : continue;
6336 : : }
6337 : 113230 : dr_stmt = DR_GROUP_FIRST_ELEMENT (dr_stmt);
6338 : 113230 : imin = DR_GROUP_SIZE (dr_stmt) + 1;
6339 : 113230 : tmp_perm.safe_splice (SLP_TREE_LOAD_PERMUTATION (node));
6340 : : }
6341 : 5392162 : else if (SLP_TREE_CODE (node) == VEC_PERM_EXPR
6342 : 117786 : && SLP_TREE_CHILDREN (node).length () == 1
6343 : 104342 : && (child = SLP_TREE_CHILDREN (node)[0])
6344 : 2852594 : && (TYPE_VECTOR_SUBPARTS (SLP_TREE_VECTYPE (child))
6345 : 104342 : .is_constant (&imin)))
6346 : : {
6347 : : /* If the child has the same vector size as this node,
6348 : : reversing the permutation can make the permutation a no-op.
6349 : : In other cases it can change a true permutation into a
6350 : : full-vector extract. */
6351 : 104342 : tmp_perm.reserve (SLP_TREE_LANES (node));
6352 : 277519 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6353 : 173177 : tmp_perm.quick_push (SLP_TREE_LANE_PERMUTATION (node)[j].second);
6354 : : }
6355 : : else
6356 : 2643910 : continue;
6357 : :
6358 : 607130 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6359 : : {
6360 : 389558 : unsigned idx = tmp_perm[j];
6361 : 389558 : imin = MIN (imin, idx);
6362 : 389558 : imax = MAX (imax, idx);
6363 : 389558 : if (idx - tmp_perm[0] != j)
6364 : 123749 : any_permute = true;
6365 : : }
6366 : : /* If the span doesn't match we'd disrupt VF computation, avoid
6367 : : that for now. */
6368 : 217572 : if (imax - imin + 1 != SLP_TREE_LANES (node))
6369 : 73317 : continue;
6370 : : /* If there's no permute no need to split one out. In this case
6371 : : we can consider turning a load into a permuted load, if that
6372 : : turns out to be cheaper than alternatives. */
6373 : 144255 : if (!any_permute)
6374 : : {
6375 : 129262 : partition.layout = -1;
6376 : 129262 : continue;
6377 : : }
6378 : :
6379 : : /* For now only handle true permutes, like
6380 : : vect_attempt_slp_rearrange_stmts did. This allows us to be lazy
6381 : : when permuting constants and invariants keeping the permute
6382 : : bijective. */
6383 : 14993 : auto_sbitmap load_index (SLP_TREE_LANES (node));
6384 : 14993 : bitmap_clear (load_index);
6385 : 60336 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6386 : 45343 : bitmap_set_bit (load_index, tmp_perm[j] - imin);
6387 : : unsigned j;
6388 : 59702 : for (j = 0; j < SLP_TREE_LANES (node); ++j)
6389 : 44797 : if (!bitmap_bit_p (load_index, j))
6390 : : break;
6391 : 14993 : if (j != SLP_TREE_LANES (node))
6392 : 88 : continue;
6393 : :
6394 : 14905 : vec<unsigned> perm = vNULL;
6395 : 14905 : perm.safe_grow (SLP_TREE_LANES (node), true);
6396 : 59501 : for (unsigned j = 0; j < SLP_TREE_LANES (node); ++j)
6397 : 44596 : perm[j] = tmp_perm[j] - imin;
6398 : :
6399 : 29810 : if (int (m_perms.length ()) >= param_vect_max_layout_candidates)
6400 : : {
6401 : : /* Continue to use existing layouts, but don't add any more. */
6402 : 0 : int *entry = layout_ids.get (perm);
6403 : 0 : partition.layout = entry ? *entry : 0;
6404 : 0 : perm.release ();
6405 : : }
6406 : : else
6407 : : {
6408 : 14905 : bool existed;
6409 : 14905 : int &layout_i = layout_ids.get_or_insert (perm, &existed);
6410 : 14905 : if (existed)
6411 : 3905 : perm.release ();
6412 : : else
6413 : : {
6414 : 11000 : layout_i = m_perms.length ();
6415 : 11000 : m_perms.safe_push (perm);
6416 : : }
6417 : 14905 : partition.layout = layout_i;
6418 : : }
6419 : 14993 : }
6420 : :
6421 : : /* Initially assume that every layout is possible and has zero cost
6422 : : in every partition. */
6423 : 620472 : m_partition_layout_costs.safe_grow_cleared (m_partitions.length ()
6424 : 1240944 : * m_perms.length ());
6425 : :
6426 : : /* We have to mark outgoing permutations facing non-associating-reduction
6427 : : graph entries that are not represented as to be materialized.
6428 : : slp_inst_kind_bb_reduc currently only covers associatable reductions. */
6429 : 3373468 : for (slp_instance instance : m_vinfo->slp_instances)
6430 : 1512052 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor)
6431 : : {
6432 : 5269 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
6433 : 5269 : m_partitions[m_vertices[node_i].partition].layout = 0;
6434 : : }
6435 : 1506783 : else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_reduc_chain)
6436 : : {
6437 : 270 : stmt_vec_info stmt_info
6438 : 270 : = SLP_TREE_REPRESENTATIVE (SLP_INSTANCE_TREE (instance));
6439 : 270 : stmt_vec_info reduc_info = info_for_reduction (m_vinfo, stmt_info);
6440 : 270 : if (needs_fold_left_reduction_p (TREE_TYPE
6441 : : (gimple_get_lhs (stmt_info->stmt)),
6442 : : STMT_VINFO_REDUC_CODE (reduc_info)))
6443 : : {
6444 : 60 : unsigned int node_i = SLP_INSTANCE_TREE (instance)->vertex;
6445 : 60 : m_partitions[m_vertices[node_i].partition].layout = 0;
6446 : : }
6447 : : }
6448 : :
6449 : : /* Check which layouts each node and partition can handle. Calculate the
6450 : : weights associated with inserting layout changes on edges. */
6451 : 5002410 : for (unsigned int node_i : m_partitioned_nodes)
6452 : : {
6453 : 3140994 : auto &vertex = m_vertices[node_i];
6454 : 3140994 : auto &partition = m_partitions[vertex.partition];
6455 : 3140994 : slp_tree node = vertex.node;
6456 : :
6457 : 3140994 : if (stmt_vec_info rep = SLP_TREE_REPRESENTATIVE (node))
6458 : : {
6459 : 3136276 : vertex.weight = vect_slp_node_weight (node);
6460 : :
6461 : : /* We do not handle stores with a permutation, so all
6462 : : incoming permutations must have been materialized.
6463 : :
6464 : : We also don't handle masked grouped loads, which lack a
6465 : : permutation vector. In this case the memory locations
6466 : : form an implicit second input to the loads, on top of the
6467 : : explicit mask input, and the memory input's layout cannot
6468 : : be changed.
6469 : :
6470 : : On the other hand, we do support permuting gather loads and
6471 : : masked gather loads, where each scalar load is independent
6472 : : of the others. This can be useful if the address/index input
6473 : : benefits from permutation. */
6474 : 3136276 : if (STMT_VINFO_DATA_REF (rep)
6475 : 1540491 : && STMT_VINFO_GROUPED_ACCESS (rep)
6476 : 4220260 : && !SLP_TREE_LOAD_PERMUTATION (node).exists ())
6477 : 970754 : partition.layout = 0;
6478 : :
6479 : : /* We cannot change the layout of an operation that is
6480 : : not independent on lanes. Note this is an explicit
6481 : : negative list since that's much shorter than the respective
6482 : : positive one but it's critical to keep maintaining it. */
6483 : 3136276 : if (is_gimple_call (STMT_VINFO_STMT (rep)))
6484 : 21330 : switch (gimple_call_combined_fn (STMT_VINFO_STMT (rep)))
6485 : : {
6486 : 823 : case CFN_COMPLEX_ADD_ROT90:
6487 : 823 : case CFN_COMPLEX_ADD_ROT270:
6488 : 823 : case CFN_COMPLEX_MUL:
6489 : 823 : case CFN_COMPLEX_MUL_CONJ:
6490 : 823 : case CFN_VEC_ADDSUB:
6491 : 823 : case CFN_VEC_FMADDSUB:
6492 : 823 : case CFN_VEC_FMSUBADD:
6493 : 823 : partition.layout = 0;
6494 : : default:;
6495 : : }
6496 : : }
6497 : :
6498 : 6757668 : auto process_edge = [&](graph_edge *ud, unsigned int other_node_i)
6499 : : {
6500 : 3616674 : auto &other_vertex = m_vertices[other_node_i];
6501 : :
6502 : : /* Count the number of edges from earlier partitions and the number
6503 : : of edges to later partitions. */
6504 : 3616674 : if (other_vertex.partition < vertex.partition)
6505 : 1808337 : partition.in_degree += 1;
6506 : : else
6507 : 1808337 : partition.out_degree += 1;
6508 : :
6509 : : /* If the current node uses the result of OTHER_NODE_I, accumulate
6510 : : the effects of that. */
6511 : 3616674 : if (ud->src == int (node_i))
6512 : : {
6513 : 1808337 : other_vertex.out_weight += vertex.weight;
6514 : 1808337 : other_vertex.out_degree += 1;
6515 : : }
6516 : 6757668 : };
6517 : 3140994 : for_each_partition_edge (node_i, process_edge);
6518 : : }
6519 : 620472 : }
6520 : :
6521 : : /* Return the incoming costs for node NODE_I, assuming that each input keeps
6522 : : its current (provisional) choice of layout. The inputs do not necessarily
6523 : : have the same layout as each other. */
6524 : :
6525 : : slpg_layout_cost
6526 : 3083 : vect_optimize_slp_pass::total_in_cost (unsigned int node_i)
6527 : : {
6528 : 3083 : auto &vertex = m_vertices[node_i];
6529 : 3083 : slpg_layout_cost cost;
6530 : 11050 : auto add_cost = [&](graph_edge *, unsigned int other_node_i)
6531 : : {
6532 : 7967 : auto &other_vertex = m_vertices[other_node_i];
6533 : 7967 : if (other_vertex.partition < vertex.partition)
6534 : : {
6535 : 5071 : auto &other_partition = m_partitions[other_vertex.partition];
6536 : 10142 : auto &other_costs = partition_layout_costs (other_vertex.partition,
6537 : 5071 : other_partition.layout);
6538 : 5071 : slpg_layout_cost this_cost = other_costs.in_cost;
6539 : 5071 : this_cost.add_serial_cost (other_costs.internal_cost);
6540 : 5071 : this_cost.split (other_partition.out_degree);
6541 : 5071 : cost.add_parallel_cost (this_cost);
6542 : : }
6543 : 11050 : };
6544 : 3083 : for_each_partition_edge (node_i, add_cost);
6545 : 3083 : return cost;
6546 : : }
6547 : :
6548 : : /* Return the cost of switching between layout LAYOUT1_I (at node NODE1_I)
6549 : : and layout LAYOUT2_I on cross-partition use-to-def edge UD. Return
6550 : : slpg_layout_cost::impossible () if the change isn't possible. */
6551 : :
6552 : : slpg_layout_cost
6553 : 533164 : vect_optimize_slp_pass::
6554 : : edge_layout_cost (graph_edge *ud, unsigned int node1_i, unsigned int layout1_i,
6555 : : unsigned int layout2_i)
6556 : : {
6557 : 533164 : auto &def_vertex = m_vertices[ud->dest];
6558 : 533164 : auto &use_vertex = m_vertices[ud->src];
6559 : 533164 : auto def_layout_i = ud->dest == int (node1_i) ? layout1_i : layout2_i;
6560 : 533164 : auto use_layout_i = ud->dest == int (node1_i) ? layout2_i : layout1_i;
6561 : 533164 : auto factor = change_layout_cost (def_vertex.node, def_layout_i,
6562 : : use_layout_i);
6563 : 533164 : if (factor < 0)
6564 : 5692 : return slpg_layout_cost::impossible ();
6565 : :
6566 : : /* We have a choice of putting the layout change at the site of the
6567 : : definition or at the site of the use. Prefer the former when
6568 : : optimizing for size or when the execution frequency of the
6569 : : definition is no greater than the combined execution frequencies of
6570 : : the uses. When putting the layout change at the site of the definition,
6571 : : divvy up the cost among all consumers. */
6572 : 527472 : if (m_optimize_size || def_vertex.weight <= def_vertex.out_weight)
6573 : : {
6574 : 506848 : slpg_layout_cost cost = { def_vertex.weight * factor, m_optimize_size };
6575 : 506848 : cost.split (def_vertex.out_degree);
6576 : 506848 : return cost;
6577 : : }
6578 : 20624 : return { use_vertex.weight * factor, m_optimize_size };
6579 : : }
6580 : :
6581 : : /* UD represents a use-def link between FROM_NODE_I and a node in a later
6582 : : partition; FROM_NODE_I could be the definition node or the use node.
6583 : : The node at the other end of the link wants to use layout TO_LAYOUT_I.
6584 : : Return the cost of any necessary fix-ups on edge UD, or return
6585 : : slpg_layout_cost::impossible () if the change isn't possible.
6586 : :
6587 : : At this point, FROM_NODE_I's partition has chosen the cheapest
6588 : : layout based on the information available so far, but this choice
6589 : : is only provisional. */
6590 : :
6591 : : slpg_layout_cost
6592 : 138390 : vect_optimize_slp_pass::forward_cost (graph_edge *ud, unsigned int from_node_i,
6593 : : unsigned int to_layout_i)
6594 : : {
6595 : 138390 : auto &from_vertex = m_vertices[from_node_i];
6596 : 138390 : unsigned int from_partition_i = from_vertex.partition;
6597 : 138390 : slpg_partition_info &from_partition = m_partitions[from_partition_i];
6598 : 138390 : gcc_assert (from_partition.layout >= 0);
6599 : :
6600 : : /* First calculate the cost on the assumption that FROM_PARTITION sticks
6601 : : with its current layout preference. */
6602 : 138390 : slpg_layout_cost cost = slpg_layout_cost::impossible ();
6603 : 138390 : auto edge_cost = edge_layout_cost (ud, from_node_i,
6604 : 138390 : from_partition.layout, to_layout_i);
6605 : 138390 : if (edge_cost.is_possible ())
6606 : : {
6607 : 270784 : auto &from_costs = partition_layout_costs (from_partition_i,
6608 : 135392 : from_partition.layout);
6609 : 135392 : cost = from_costs.in_cost;
6610 : 135392 : cost.add_serial_cost (from_costs.internal_cost);
6611 : 135392 : cost.split (from_partition.out_degree);
6612 : 135392 : cost.add_serial_cost (edge_cost);
6613 : : }
6614 : 2998 : else if (from_partition.layout == 0)
6615 : : /* We must allow the source partition to have layout 0 as a fallback,
6616 : : in case all other options turn out to be impossible. */
6617 : 2998 : return cost;
6618 : :
6619 : : /* Take the minimum of that cost and the cost that applies if
6620 : : FROM_PARTITION instead switches to TO_LAYOUT_I. */
6621 : 135392 : auto &direct_layout_costs = partition_layout_costs (from_partition_i,
6622 : : to_layout_i);
6623 : 135392 : if (direct_layout_costs.is_possible ())
6624 : : {
6625 : 126244 : slpg_layout_cost direct_cost = direct_layout_costs.in_cost;
6626 : 126244 : direct_cost.add_serial_cost (direct_layout_costs.internal_cost);
6627 : 126244 : direct_cost.split (from_partition.out_degree);
6628 : 126244 : if (!cost.is_possible ()
6629 : 126244 : || direct_cost.is_better_than (cost, m_optimize_size))
6630 : 39459 : cost = direct_cost;
6631 : : }
6632 : :
6633 : 135392 : return cost;
6634 : : }
6635 : :
6636 : : /* UD represents a use-def link between TO_NODE_I and a node in an earlier
6637 : : partition; TO_NODE_I could be the definition node or the use node.
6638 : : The node at the other end of the link wants to use layout FROM_LAYOUT_I;
6639 : : return the cost of any necessary fix-ups on edge UD, or
6640 : : slpg_layout_cost::impossible () if the choice cannot be made.
6641 : :
6642 : : At this point, TO_NODE_I's partition has a fixed choice of layout. */
6643 : :
6644 : : slpg_layout_cost
6645 : 131986 : vect_optimize_slp_pass::backward_cost (graph_edge *ud, unsigned int to_node_i,
6646 : : unsigned int from_layout_i)
6647 : : {
6648 : 131986 : auto &to_vertex = m_vertices[to_node_i];
6649 : 131986 : unsigned int to_partition_i = to_vertex.partition;
6650 : 131986 : slpg_partition_info &to_partition = m_partitions[to_partition_i];
6651 : 131986 : gcc_assert (to_partition.layout >= 0);
6652 : :
6653 : : /* If TO_NODE_I is a VEC_PERM_EXPR consumer, see whether it can be
6654 : : adjusted for this input having layout FROM_LAYOUT_I. Assume that
6655 : : any other inputs keep their current choice of layout. */
6656 : 131986 : auto &to_costs = partition_layout_costs (to_partition_i,
6657 : : to_partition.layout);
6658 : 131986 : if (ud->src == int (to_node_i)
6659 : 131824 : && SLP_TREE_CODE (to_vertex.node) == VEC_PERM_EXPR)
6660 : : {
6661 : 8871 : auto &from_partition = m_partitions[m_vertices[ud->dest].partition];
6662 : 8871 : auto old_layout = from_partition.layout;
6663 : 8871 : from_partition.layout = from_layout_i;
6664 : 17742 : int factor = internal_node_cost (to_vertex.node, -1,
6665 : 8871 : to_partition.layout);
6666 : 8871 : from_partition.layout = old_layout;
6667 : 8871 : if (factor >= 0)
6668 : : {
6669 : 8243 : slpg_layout_cost cost = to_costs.out_cost;
6670 : 16486 : cost.add_serial_cost ({ to_vertex.weight * factor,
6671 : 8243 : m_optimize_size });
6672 : 8243 : cost.split (to_partition.in_degree);
6673 : 8243 : return cost;
6674 : : }
6675 : : }
6676 : :
6677 : : /* Compute the cost if we insert any necessary layout change on edge UD. */
6678 : 123743 : auto edge_cost = edge_layout_cost (ud, to_node_i,
6679 : 123743 : to_partition.layout, from_layout_i);
6680 : 123743 : if (edge_cost.is_possible ())
6681 : : {
6682 : 123743 : slpg_layout_cost cost = to_costs.out_cost;
6683 : 123743 : cost.add_serial_cost (to_costs.internal_cost);
6684 : 123743 : cost.split (to_partition.in_degree);
6685 : 123743 : cost.add_serial_cost (edge_cost);
6686 : 123743 : return cost;
6687 : : }
6688 : :
6689 : 0 : return slpg_layout_cost::impossible ();
6690 : : }
6691 : :
6692 : : /* Make a forward pass through the partitions, accumulating input costs.
6693 : : Make a tentative (provisional) choice of layout for each partition,
6694 : : ensuring that this choice still allows later partitions to keep
6695 : : their original layout. */
6696 : :
6697 : : void
6698 : 10475 : vect_optimize_slp_pass::forward_pass ()
6699 : : {
6700 : 110403 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
6701 : : ++partition_i)
6702 : : {
6703 : 99928 : auto &partition = m_partitions[partition_i];
6704 : :
6705 : : /* If the partition consists of a single VEC_PERM_EXPR, precompute
6706 : : the incoming cost that would apply if every predecessor partition
6707 : : keeps its current layout. This is used within the loop below. */
6708 : 99928 : slpg_layout_cost in_cost;
6709 : 99928 : slp_tree single_node = nullptr;
6710 : 99928 : if (partition.node_end == partition.node_begin + 1)
6711 : : {
6712 : 99338 : unsigned int node_i = m_partitioned_nodes[partition.node_begin];
6713 : 99338 : single_node = m_vertices[node_i].node;
6714 : 99338 : if (SLP_TREE_CODE (single_node) == VEC_PERM_EXPR)
6715 : 3083 : in_cost = total_in_cost (node_i);
6716 : : }
6717 : :
6718 : : /* Go through the possible layouts. Decide which ones are valid
6719 : : for this partition and record which of the valid layouts has
6720 : : the lowest cost. */
6721 : 99928 : unsigned int min_layout_i = 0;
6722 : 99928 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
6723 : 307634 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
6724 : : {
6725 : 207706 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
6726 : 207706 : if (!layout_costs.is_possible ())
6727 : 46183 : continue;
6728 : :
6729 : : /* If the recorded layout is already 0 then the layout cannot
6730 : : change. */
6731 : 207706 : if (partition.layout == 0 && layout_i != 0)
6732 : : {
6733 : 34304 : layout_costs.mark_impossible ();
6734 : 34304 : continue;
6735 : : }
6736 : :
6737 : 173402 : bool is_possible = true;
6738 : 339923 : for (unsigned int order_i = partition.node_begin;
6739 : 339923 : order_i < partition.node_end; ++order_i)
6740 : : {
6741 : 175543 : unsigned int node_i = m_partitioned_nodes[order_i];
6742 : 175543 : auto &vertex = m_vertices[node_i];
6743 : :
6744 : : /* Reject the layout if it is individually incompatible
6745 : : with any node in the partition. */
6746 : 175543 : if (!is_compatible_layout (vertex.node, layout_i))
6747 : : {
6748 : 7837 : is_possible = false;
6749 : 9022 : break;
6750 : : }
6751 : :
6752 : 441862 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
6753 : : {
6754 : 274156 : auto &other_vertex = m_vertices[other_node_i];
6755 : 274156 : if (other_vertex.partition < vertex.partition)
6756 : : {
6757 : : /* Accumulate the incoming costs from earlier
6758 : : partitions, plus the cost of any layout changes
6759 : : on UD itself. */
6760 : 138390 : auto cost = forward_cost (ud, other_node_i, layout_i);
6761 : 138390 : if (!cost.is_possible ())
6762 : 2998 : is_possible = false;
6763 : : else
6764 : 135392 : layout_costs.in_cost.add_parallel_cost (cost);
6765 : : }
6766 : : else
6767 : : /* Reject the layout if it would make layout 0 impossible
6768 : : for later partitions. This amounts to testing that the
6769 : : target supports reversing the layout change on edges
6770 : : to later partitions.
6771 : :
6772 : : In principle, it might be possible to push a layout
6773 : : change all the way down a graph, so that it never
6774 : : needs to be reversed and so that the target doesn't
6775 : : need to support the reverse operation. But it would
6776 : : be awkward to bail out if we hit a partition that
6777 : : does not support the new layout, especially since
6778 : : we are not dealing with a lattice. */
6779 : 135766 : is_possible &= edge_layout_cost (ud, other_node_i, 0,
6780 : 135766 : layout_i).is_possible ();
6781 : 441862 : };
6782 : 167706 : for_each_partition_edge (node_i, add_cost);
6783 : :
6784 : : /* Accumulate the cost of using LAYOUT_I within NODE,
6785 : : both for the inputs and the outputs. */
6786 : 167706 : int factor = internal_node_cost (vertex.node, layout_i,
6787 : : layout_i);
6788 : 167706 : if (factor < 0)
6789 : : {
6790 : 1185 : is_possible = false;
6791 : 1185 : break;
6792 : : }
6793 : 166521 : else if (factor)
6794 : 26880 : layout_costs.internal_cost.add_serial_cost
6795 : 26880 : ({ vertex.weight * factor, m_optimize_size });
6796 : : }
6797 : 173402 : if (!is_possible)
6798 : : {
6799 : 11879 : layout_costs.mark_impossible ();
6800 : 11879 : continue;
6801 : : }
6802 : :
6803 : : /* Combine the incoming and partition-internal costs. */
6804 : 161523 : slpg_layout_cost combined_cost = layout_costs.in_cost;
6805 : 161523 : combined_cost.add_serial_cost (layout_costs.internal_cost);
6806 : :
6807 : : /* If this partition consists of a single VEC_PERM_EXPR, see
6808 : : if the VEC_PERM_EXPR can be changed to support output layout
6809 : : LAYOUT_I while keeping all the provisional choices of input
6810 : : layout. */
6811 : 161523 : if (single_node
6812 : 160408 : && SLP_TREE_CODE (single_node) == VEC_PERM_EXPR)
6813 : : {
6814 : 5299 : int factor = internal_node_cost (single_node, -1, layout_i);
6815 : 5299 : if (factor >= 0)
6816 : : {
6817 : 4870 : auto weight = m_vertices[single_node->vertex].weight;
6818 : 4870 : slpg_layout_cost internal_cost
6819 : 4870 : = { weight * factor, m_optimize_size };
6820 : :
6821 : 4870 : slpg_layout_cost alt_cost = in_cost;
6822 : 4870 : alt_cost.add_serial_cost (internal_cost);
6823 : 4870 : if (alt_cost.is_better_than (combined_cost, m_optimize_size))
6824 : : {
6825 : 1452 : combined_cost = alt_cost;
6826 : 1452 : layout_costs.in_cost = in_cost;
6827 : 1452 : layout_costs.internal_cost = internal_cost;
6828 : : }
6829 : : }
6830 : : }
6831 : :
6832 : : /* Record the layout with the lowest cost. Prefer layout 0 in
6833 : : the event of a tie between it and another layout. */
6834 : 161523 : if (!min_layout_cost.is_possible ()
6835 : 61595 : || combined_cost.is_better_than (min_layout_cost,
6836 : 61595 : m_optimize_size))
6837 : : {
6838 : 115683 : min_layout_i = layout_i;
6839 : 115683 : min_layout_cost = combined_cost;
6840 : : }
6841 : : }
6842 : :
6843 : : /* This loop's handling of earlier partitions should ensure that
6844 : : choosing the original layout for the current partition is no
6845 : : less valid than it was in the original graph, even with the
6846 : : provisional layout choices for those earlier partitions. */
6847 : 99928 : gcc_assert (min_layout_cost.is_possible ());
6848 : 99928 : partition.layout = min_layout_i;
6849 : : }
6850 : 10475 : }
6851 : :
6852 : : /* Make a backward pass through the partitions, accumulating output costs.
6853 : : Make a final choice of layout for each partition. */
6854 : :
6855 : : void
6856 : 10475 : vect_optimize_slp_pass::backward_pass ()
6857 : : {
6858 : 120878 : for (unsigned int partition_i = m_partitions.length (); partition_i-- > 0;)
6859 : : {
6860 : 99928 : auto &partition = m_partitions[partition_i];
6861 : :
6862 : 99928 : unsigned int min_layout_i = 0;
6863 : 99928 : slpg_layout_cost min_layout_cost = slpg_layout_cost::impossible ();
6864 : 307634 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
6865 : : {
6866 : 207706 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
6867 : 207706 : if (!layout_costs.is_possible ())
6868 : 46183 : continue;
6869 : :
6870 : : /* Accumulate the costs from successor partitions. */
6871 : 161523 : bool is_possible = true;
6872 : 325140 : for (unsigned int order_i = partition.node_begin;
6873 : 325140 : order_i < partition.node_end; ++order_i)
6874 : : {
6875 : 163617 : unsigned int node_i = m_partitioned_nodes[order_i];
6876 : 163617 : auto &vertex = m_vertices[node_i];
6877 : 430868 : auto add_cost = [&](graph_edge *ud, unsigned int other_node_i)
6878 : : {
6879 : 267251 : auto &other_vertex = m_vertices[other_node_i];
6880 : 267251 : auto &other_partition = m_partitions[other_vertex.partition];
6881 : 267251 : if (other_vertex.partition > vertex.partition)
6882 : : {
6883 : : /* Accumulate the incoming costs from later
6884 : : partitions, plus the cost of any layout changes
6885 : : on UD itself. */
6886 : 131986 : auto cost = backward_cost (ud, other_node_i, layout_i);
6887 : 131986 : if (!cost.is_possible ())
6888 : 0 : is_possible = false;
6889 : : else
6890 : 131986 : layout_costs.out_cost.add_parallel_cost (cost);
6891 : : }
6892 : : else
6893 : : /* Make sure that earlier partitions can (if necessary
6894 : : or beneficial) keep the layout that they chose in
6895 : : the forward pass. This ensures that there is at
6896 : : least one valid choice of layout. */
6897 : 135265 : is_possible &= edge_layout_cost (ud, other_node_i,
6898 : 135265 : other_partition.layout,
6899 : 135265 : layout_i).is_possible ();
6900 : 430868 : };
6901 : 163617 : for_each_partition_edge (node_i, add_cost);
6902 : : }
6903 : 161523 : if (!is_possible)
6904 : : {
6905 : 0 : layout_costs.mark_impossible ();
6906 : 0 : continue;
6907 : : }
6908 : :
6909 : : /* Locally combine the costs from the forward and backward passes.
6910 : : (This combined cost is not passed on, since that would lead
6911 : : to double counting.) */
6912 : 161523 : slpg_layout_cost combined_cost = layout_costs.in_cost;
6913 : 161523 : combined_cost.add_serial_cost (layout_costs.internal_cost);
6914 : 161523 : combined_cost.add_serial_cost (layout_costs.out_cost);
6915 : :
6916 : : /* Record the layout with the lowest cost. Prefer layout 0 in
6917 : : the event of a tie between it and another layout. */
6918 : 161523 : if (!min_layout_cost.is_possible ()
6919 : 61595 : || combined_cost.is_better_than (min_layout_cost,
6920 : 61595 : m_optimize_size))
6921 : : {
6922 : 112957 : min_layout_i = layout_i;
6923 : 112957 : min_layout_cost = combined_cost;
6924 : : }
6925 : : }
6926 : :
6927 : 99928 : gcc_assert (min_layout_cost.is_possible ());
6928 : 99928 : partition.layout = min_layout_i;
6929 : : }
6930 : 10475 : }
6931 : :
6932 : : /* Return a node that applies layout TO_LAYOUT_I to the original form of NODE.
6933 : : NODE already has the layout that was selected for its partition. */
6934 : :
6935 : : slp_tree
6936 : 112221 : vect_optimize_slp_pass::get_result_with_layout (slp_tree node,
6937 : : unsigned int to_layout_i)
6938 : : {
6939 : 112221 : unsigned int result_i = node->vertex * m_perms.length () + to_layout_i;
6940 : 112221 : slp_tree result = m_node_layouts[result_i];
6941 : 112221 : if (result)
6942 : : return result;
6943 : :
6944 : 111693 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
6945 : 111693 : || (SLP_TREE_DEF_TYPE (node) == vect_external_def
6946 : : /* We can't permute vector defs in place. */
6947 : 21161 : && SLP_TREE_VEC_DEFS (node).is_empty ()))
6948 : : {
6949 : : /* If the vector is uniform or unchanged, there's nothing to do. */
6950 : 39792 : if (to_layout_i == 0 || vect_slp_tree_uniform_p (node))
6951 : : result = node;
6952 : : else
6953 : : {
6954 : 1709 : auto scalar_ops = SLP_TREE_SCALAR_OPS (node).copy ();
6955 : 1709 : result = vect_create_new_slp_node (scalar_ops);
6956 : 1709 : vect_slp_permute (m_perms[to_layout_i], scalar_ops, true);
6957 : : }
6958 : : }
6959 : : else
6960 : : {
6961 : 71901 : unsigned int partition_i = m_vertices[node->vertex].partition;
6962 : 71901 : unsigned int from_layout_i = m_partitions[partition_i].layout;
6963 : 71901 : if (from_layout_i == to_layout_i)
6964 : 71523 : return node;
6965 : :
6966 : : /* If NODE is itself a VEC_PERM_EXPR, try to create a parallel
6967 : : permutation instead of a serial one. Leave the new permutation
6968 : : in TMP_PERM on success. */
6969 : 378 : auto_lane_permutation_t tmp_perm;
6970 : 378 : unsigned int num_inputs = 1;
6971 : 378 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
6972 : : {
6973 : 9 : tmp_perm.safe_splice (SLP_TREE_LANE_PERMUTATION (node));
6974 : 9 : if (from_layout_i != 0)
6975 : 9 : vect_slp_permute (m_perms[from_layout_i], tmp_perm, false);
6976 : 9 : if (to_layout_i != 0)
6977 : 4 : vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
6978 : 9 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
6979 : : tmp_perm,
6980 : 9 : SLP_TREE_CHILDREN (node),
6981 : : false) >= 0)
6982 : 9 : num_inputs = SLP_TREE_CHILDREN (node).length ();
6983 : : else
6984 : 0 : tmp_perm.truncate (0);
6985 : : }
6986 : :
6987 : 378 : if (dump_enabled_p ())
6988 : : {
6989 : 68 : if (tmp_perm.length () > 0)
6990 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
6991 : : "duplicating permutation node %p with"
6992 : : " layout %d\n",
6993 : : (void *) node, to_layout_i);
6994 : : else
6995 : 62 : dump_printf_loc (MSG_NOTE, vect_location,
6996 : : "inserting permutation node in place of %p\n",
6997 : : (void *) node);
6998 : : }
6999 : :
7000 : 378 : unsigned int num_lanes = SLP_TREE_LANES (node);
7001 : 378 : result = vect_create_new_slp_node (num_inputs, VEC_PERM_EXPR);
7002 : 378 : if (SLP_TREE_SCALAR_STMTS (node).length ())
7003 : : {
7004 : 377 : auto &stmts = SLP_TREE_SCALAR_STMTS (result);
7005 : 377 : stmts.safe_splice (SLP_TREE_SCALAR_STMTS (node));
7006 : 377 : if (from_layout_i != 0)
7007 : 319 : vect_slp_permute (m_perms[from_layout_i], stmts, false);
7008 : 377 : if (to_layout_i != 0)
7009 : 62 : vect_slp_permute (m_perms[to_layout_i], stmts, true);
7010 : : }
7011 : 378 : SLP_TREE_REPRESENTATIVE (result) = SLP_TREE_REPRESENTATIVE (node);
7012 : 378 : SLP_TREE_LANES (result) = num_lanes;
7013 : 378 : SLP_TREE_VECTYPE (result) = SLP_TREE_VECTYPE (node);
7014 : 378 : result->vertex = -1;
7015 : :
7016 : 378 : auto &lane_perm = SLP_TREE_LANE_PERMUTATION (result);
7017 : 378 : if (tmp_perm.length ())
7018 : : {
7019 : 9 : lane_perm.safe_splice (tmp_perm);
7020 : 9 : SLP_TREE_CHILDREN (result).safe_splice (SLP_TREE_CHILDREN (node));
7021 : : }
7022 : : else
7023 : : {
7024 : 369 : lane_perm.create (num_lanes);
7025 : 1165 : for (unsigned j = 0; j < num_lanes; ++j)
7026 : 796 : lane_perm.quick_push ({ 0, j });
7027 : 369 : if (from_layout_i != 0)
7028 : 310 : vect_slp_permute (m_perms[from_layout_i], lane_perm, false);
7029 : 369 : if (to_layout_i != 0)
7030 : 59 : vect_slp_permute (m_perms[to_layout_i], lane_perm, true);
7031 : 369 : SLP_TREE_CHILDREN (result).safe_push (node);
7032 : : }
7033 : 1516 : for (slp_tree child : SLP_TREE_CHILDREN (result))
7034 : 382 : child->refcnt++;
7035 : 378 : }
7036 : 40170 : m_node_layouts[result_i] = result;
7037 : 40170 : return result;
7038 : : }
7039 : :
7040 : : /* Apply the chosen vector layouts to the SLP graph. */
7041 : :
7042 : : void
7043 : 10475 : vect_optimize_slp_pass::materialize ()
7044 : : {
7045 : : /* We no longer need the costs, so avoid having two O(N * P) arrays
7046 : : live at the same time. */
7047 : 10475 : m_partition_layout_costs.release ();
7048 : 31425 : m_node_layouts.safe_grow_cleared (m_vertices.length () * m_perms.length ());
7049 : :
7050 : 20950 : auto_sbitmap fully_folded (m_vertices.length ());
7051 : 10475 : bitmap_clear (fully_folded);
7052 : 132456 : for (unsigned int node_i : m_partitioned_nodes)
7053 : : {
7054 : 101031 : auto &vertex = m_vertices[node_i];
7055 : 101031 : slp_tree node = vertex.node;
7056 : 101031 : int layout_i = m_partitions[vertex.partition].layout;
7057 : 101031 : gcc_assert (layout_i >= 0);
7058 : :
7059 : : /* Rearrange the scalar statements to match the chosen layout. */
7060 : 101031 : if (layout_i > 0)
7061 : 13240 : vect_slp_permute (m_perms[layout_i],
7062 : 13240 : SLP_TREE_SCALAR_STMTS (node), true);
7063 : :
7064 : : /* Update load and lane permutations. */
7065 : 101031 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
7066 : : {
7067 : : /* First try to absorb the input vector layouts. If that fails,
7068 : : force the inputs to have layout LAYOUT_I too. We checked that
7069 : : that was possible before deciding to use nonzero output layouts.
7070 : : (Note that at this stage we don't really have any guarantee that
7071 : : the target supports the original VEC_PERM_EXPR.) */
7072 : 3124 : auto &perm = SLP_TREE_LANE_PERMUTATION (node);
7073 : 3124 : auto_lane_permutation_t tmp_perm;
7074 : 3124 : tmp_perm.safe_splice (perm);
7075 : 3124 : change_vec_perm_layout (node, tmp_perm, -1, layout_i);
7076 : 3124 : if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
7077 : : tmp_perm,
7078 : 3124 : SLP_TREE_CHILDREN (node),
7079 : : false) >= 0)
7080 : : {
7081 : 2771 : if (dump_enabled_p ()
7082 : 3587 : && !std::equal (tmp_perm.begin (), tmp_perm.end (),
7083 : : perm.begin ()))
7084 : 62 : dump_printf_loc (MSG_NOTE, vect_location,
7085 : : "absorbing input layouts into %p\n",
7086 : : (void *) node);
7087 : 16629 : std::copy (tmp_perm.begin (), tmp_perm.end (), perm.begin ());
7088 : 2771 : bitmap_set_bit (fully_folded, node_i);
7089 : : }
7090 : : else
7091 : : {
7092 : : /* Not MSG_MISSED because it would make no sense to users. */
7093 : 353 : if (dump_enabled_p ())
7094 : 46 : dump_printf_loc (MSG_NOTE, vect_location,
7095 : : "failed to absorb input layouts into %p\n",
7096 : : (void *) node);
7097 : 353 : change_vec_perm_layout (nullptr, perm, layout_i, layout_i);
7098 : : }
7099 : 3124 : }
7100 : : else
7101 : : {
7102 : 97907 : gcc_assert (!SLP_TREE_LANE_PERMUTATION (node).exists ());
7103 : 97907 : auto &load_perm = SLP_TREE_LOAD_PERMUTATION (node);
7104 : 97907 : if (layout_i > 0)
7105 : : /* ??? When we handle non-bijective permutes the idea
7106 : : is that we can force the load-permutation to be
7107 : : { min, min + 1, min + 2, ... max }. But then the
7108 : : scalar defs might no longer match the lane content
7109 : : which means wrong-code with live lane vectorization.
7110 : : So we possibly have to have NULL entries for those. */
7111 : 13144 : vect_slp_permute (m_perms[layout_i], load_perm, true);
7112 : : }
7113 : : }
7114 : :
7115 : : /* Do this before any nodes disappear, since it involves a walk
7116 : : over the leaves. */
7117 : 10475 : remove_redundant_permutations ();
7118 : :
7119 : : /* Replace each child with a correctly laid-out version. */
7120 : 132456 : for (unsigned int node_i : m_partitioned_nodes)
7121 : : {
7122 : : /* Skip nodes that have already been handled above. */
7123 : 101031 : if (bitmap_bit_p (fully_folded, node_i))
7124 : 2771 : continue;
7125 : :
7126 : 98260 : auto &vertex = m_vertices[node_i];
7127 : 98260 : int in_layout_i = m_partitions[vertex.partition].layout;
7128 : 98260 : gcc_assert (in_layout_i >= 0);
7129 : :
7130 : : unsigned j;
7131 : : slp_tree child;
7132 : 282755 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (vertex.node), j, child)
7133 : : {
7134 : 112399 : if (!child)
7135 : 178 : continue;
7136 : :
7137 : 112221 : slp_tree new_child = get_result_with_layout (child, in_layout_i);
7138 : 112221 : if (new_child != child)
7139 : : {
7140 : 2264 : vect_free_slp_tree (child);
7141 : 2264 : SLP_TREE_CHILDREN (vertex.node)[j] = new_child;
7142 : 2264 : new_child->refcnt += 1;
7143 : : }
7144 : : }
7145 : : }
7146 : 10475 : }
7147 : :
7148 : : /* Elide load permutations that are not necessary. Such permutations might
7149 : : be pre-existing, rather than created by the layout optimizations. */
7150 : :
7151 : : void
7152 : 620472 : vect_optimize_slp_pass::remove_redundant_permutations ()
7153 : : {
7154 : 4244021 : for (unsigned int node_i : m_leafs)
7155 : : {
7156 : 2382605 : slp_tree node = m_vertices[node_i].node;
7157 : 2382605 : if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
7158 : 1989863 : continue;
7159 : :
7160 : : /* In basic block vectorization we allow any subchain of an interleaving
7161 : : chain.
7162 : : FORNOW: not in loop SLP because of realignment complications. */
7163 : 392742 : if (is_a <bb_vec_info> (m_vinfo))
7164 : : {
7165 : 165645 : bool subchain_p = true;
7166 : : stmt_vec_info next_load_info = NULL;
7167 : : stmt_vec_info load_info;
7168 : : unsigned j;
7169 : 165645 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
7170 : : {
7171 : 135212 : if (j != 0
7172 : 135212 : && (next_load_info != load_info
7173 : 62423 : || ! load_info
7174 : 62423 : || DR_GROUP_GAP (load_info) != 1))
7175 : : {
7176 : : subchain_p = false;
7177 : : break;
7178 : : }
7179 : 110798 : next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
7180 : : }
7181 : 54847 : if (subchain_p)
7182 : : {
7183 : 30433 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7184 : 30433 : continue;
7185 : : }
7186 : : }
7187 : : else
7188 : : {
7189 : 337895 : loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
7190 : 337895 : stmt_vec_info load_info;
7191 : 337895 : bool this_load_permuted = false;
7192 : 337895 : unsigned j;
7193 : 1005542 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
7194 : 341129 : if (SLP_TREE_LOAD_PERMUTATION (node)[j] != j)
7195 : : {
7196 : : this_load_permuted = true;
7197 : : break;
7198 : : }
7199 : : /* When this isn't a grouped access we know it's single element
7200 : : and contiguous. */
7201 : 337895 : if (!STMT_VINFO_GROUPED_ACCESS (SLP_TREE_SCALAR_STMTS (node)[0]))
7202 : : {
7203 : 279512 : if (!this_load_permuted
7204 : 279512 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7205 : 279082 : || SLP_TREE_LANES (node) == 1))
7206 : 279082 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7207 : 279512 : continue;
7208 : : }
7209 : 58383 : stmt_vec_info first_stmt_info
7210 : 58383 : = DR_GROUP_FIRST_ELEMENT (SLP_TREE_SCALAR_STMTS (node)[0]);
7211 : 58724 : if (!this_load_permuted
7212 : : /* The load requires permutation when unrolling exposes
7213 : : a gap either because the group is larger than the SLP
7214 : : group-size or because there is a gap between the groups. */
7215 : 58383 : && (known_eq (LOOP_VINFO_VECT_FACTOR (loop_vinfo), 1U)
7216 : 47188 : || ((SLP_TREE_LANES (node) == DR_GROUP_SIZE (first_stmt_info))
7217 : 93 : && DR_GROUP_GAP (first_stmt_info) == 0)))
7218 : : {
7219 : 341 : SLP_TREE_LOAD_PERMUTATION (node).release ();
7220 : 341 : continue;
7221 : : }
7222 : : }
7223 : : }
7224 : 620472 : }
7225 : :
7226 : : /* Print the partition graph and layout information to the dump file. */
7227 : :
7228 : : void
7229 : 607 : vect_optimize_slp_pass::dump ()
7230 : : {
7231 : 607 : dump_printf_loc (MSG_NOTE, vect_location,
7232 : : "SLP optimize permutations:\n");
7233 : 1227 : for (unsigned int layout_i = 1; layout_i < m_perms.length (); ++layout_i)
7234 : : {
7235 : 620 : dump_printf_loc (MSG_NOTE, vect_location, " %d: { ", layout_i);
7236 : 620 : const char *sep = "";
7237 : 5283 : for (unsigned int idx : m_perms[layout_i])
7238 : : {
7239 : 3423 : dump_printf (MSG_NOTE, "%s%d", sep, idx);
7240 : 3423 : sep = ", ";
7241 : : }
7242 : 620 : dump_printf (MSG_NOTE, " }\n");
7243 : : }
7244 : 607 : dump_printf_loc (MSG_NOTE, vect_location,
7245 : : "SLP optimize partitions:\n");
7246 : 4936 : for (unsigned int partition_i = 0; partition_i < m_partitions.length ();
7247 : : ++partition_i)
7248 : : {
7249 : 4329 : auto &partition = m_partitions[partition_i];
7250 : 4329 : dump_printf_loc (MSG_NOTE, vect_location, " -------------\n");
7251 : 4329 : dump_printf_loc (MSG_NOTE, vect_location,
7252 : : " partition %d (layout %d):\n",
7253 : : partition_i, partition.layout);
7254 : 4329 : dump_printf_loc (MSG_NOTE, vect_location, " nodes:\n");
7255 : 8836 : for (unsigned int order_i = partition.node_begin;
7256 : 8836 : order_i < partition.node_end; ++order_i)
7257 : : {
7258 : 4507 : auto &vertex = m_vertices[m_partitioned_nodes[order_i]];
7259 : 9014 : dump_printf_loc (MSG_NOTE, vect_location, " - %p:\n",
7260 : 4507 : (void *) vertex.node);
7261 : 4507 : dump_printf_loc (MSG_NOTE, vect_location,
7262 : : " weight: %f\n",
7263 : : vertex.weight.to_double ());
7264 : 4507 : if (vertex.out_degree)
7265 : 3517 : dump_printf_loc (MSG_NOTE, vect_location,
7266 : : " out weight: %f (degree %d)\n",
7267 : : vertex.out_weight.to_double (),
7268 : : vertex.out_degree);
7269 : 4507 : if (SLP_TREE_CODE (vertex.node) == VEC_PERM_EXPR)
7270 : 454 : dump_printf_loc (MSG_NOTE, vect_location,
7271 : : " op: VEC_PERM_EXPR\n");
7272 : 4053 : else if (auto rep = SLP_TREE_REPRESENTATIVE (vertex.node))
7273 : 4035 : dump_printf_loc (MSG_NOTE, vect_location,
7274 : : " op template: %G", rep->stmt);
7275 : : }
7276 : 4329 : dump_printf_loc (MSG_NOTE, vect_location, " edges:\n");
7277 : 8836 : for (unsigned int order_i = partition.node_begin;
7278 : 8836 : order_i < partition.node_end; ++order_i)
7279 : : {
7280 : 4507 : unsigned int node_i = m_partitioned_nodes[order_i];
7281 : 4507 : auto &vertex = m_vertices[node_i];
7282 : 13637 : auto print_edge = [&](graph_edge *, unsigned int other_node_i)
7283 : : {
7284 : 9130 : auto &other_vertex = m_vertices[other_node_i];
7285 : 9130 : if (other_vertex.partition < vertex.partition)
7286 : 4565 : dump_printf_loc (MSG_NOTE, vect_location,
7287 : : " - %p [%d] --> %p\n",
7288 : 4565 : (void *) other_vertex.node,
7289 : : other_vertex.partition,
7290 : 4565 : (void *) vertex.node);
7291 : : else
7292 : 4565 : dump_printf_loc (MSG_NOTE, vect_location,
7293 : : " - %p --> [%d] %p\n",
7294 : 4565 : (void *) vertex.node,
7295 : : other_vertex.partition,
7296 : 4565 : (void *) other_vertex.node);
7297 : 13637 : };
7298 : 4507 : for_each_partition_edge (node_i, print_edge);
7299 : : }
7300 : :
7301 : 13186 : for (unsigned int layout_i = 0; layout_i < m_perms.length (); ++layout_i)
7302 : : {
7303 : 8857 : auto &layout_costs = partition_layout_costs (partition_i, layout_i);
7304 : 8857 : if (layout_costs.is_possible ())
7305 : : {
7306 : 7188 : dump_printf_loc (MSG_NOTE, vect_location,
7307 : : " layout %d:%s\n", layout_i,
7308 : 7188 : partition.layout == int (layout_i)
7309 : : ? " (*)" : "");
7310 : 7188 : slpg_layout_cost combined_cost = layout_costs.in_cost;
7311 : 7188 : combined_cost.add_serial_cost (layout_costs.internal_cost);
7312 : 7188 : combined_cost.add_serial_cost (layout_costs.out_cost);
7313 : : #define TEMPLATE "{depth: %f, total: %f}"
7314 : 7188 : dump_printf_loc (MSG_NOTE, vect_location,
7315 : : " " TEMPLATE "\n",
7316 : : layout_costs.in_cost.depth.to_double (),
7317 : : layout_costs.in_cost.total.to_double ());
7318 : 7188 : dump_printf_loc (MSG_NOTE, vect_location,
7319 : : " + " TEMPLATE "\n",
7320 : : layout_costs.internal_cost.depth.to_double (),
7321 : : layout_costs.internal_cost.total.to_double ());
7322 : 7188 : dump_printf_loc (MSG_NOTE, vect_location,
7323 : : " + " TEMPLATE "\n",
7324 : : layout_costs.out_cost.depth.to_double (),
7325 : : layout_costs.out_cost.total.to_double ());
7326 : 7188 : dump_printf_loc (MSG_NOTE, vect_location,
7327 : : " = " TEMPLATE "\n",
7328 : : combined_cost.depth.to_double (),
7329 : : combined_cost.total.to_double ());
7330 : : #undef TEMPLATE
7331 : : }
7332 : : else
7333 : 1669 : dump_printf_loc (MSG_NOTE, vect_location,
7334 : : " layout %d: rejected\n", layout_i);
7335 : : }
7336 : : }
7337 : 607 : }
7338 : :
7339 : : /* Masked load lanes discovery. */
7340 : :
7341 : : void
7342 : 620472 : vect_optimize_slp_pass::decide_masked_load_lanes ()
7343 : : {
7344 : 6560110 : for (auto v : m_vertices)
7345 : : {
7346 : 4698694 : slp_tree node = v.node;
7347 : 4698694 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
7348 : 3138935 : || SLP_TREE_CODE (node) == VEC_PERM_EXPR)
7349 : 1677918 : continue;
7350 : 3020776 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
7351 : 1436032 : if (! STMT_VINFO_GROUPED_ACCESS (stmt_info)
7352 : : /* The mask has to be uniform. */
7353 : 979950 : || STMT_VINFO_SLP_VECT_ONLY (stmt_info)
7354 : 979860 : || ! is_a <gcall *> (STMT_VINFO_STMT (stmt_info))
7355 : 3020823 : || ! gimple_call_internal_p (STMT_VINFO_STMT (stmt_info),
7356 : : IFN_MASK_LOAD))
7357 : 3020773 : continue;
7358 : 3 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
7359 : 6 : if (STMT_VINFO_STRIDED_P (stmt_info)
7360 : 3 : || compare_step_with_zero (m_vinfo, stmt_info) <= 0
7361 : 3 : || vect_load_lanes_supported (SLP_TREE_VECTYPE (node),
7362 : 0 : DR_GROUP_SIZE (stmt_info),
7363 : : true) == IFN_LAST)
7364 : 3 : continue;
7365 : :
7366 : : /* Uniform masks need to be suitably represented. */
7367 : 0 : slp_tree mask = SLP_TREE_CHILDREN (node)[0];
7368 : 0 : if (SLP_TREE_CODE (mask) != VEC_PERM_EXPR
7369 : 0 : || SLP_TREE_CHILDREN (mask).length () != 1)
7370 : 0 : continue;
7371 : 0 : bool match = true;
7372 : 0 : for (auto perm : SLP_TREE_LANE_PERMUTATION (mask))
7373 : 0 : if (perm.first != 0 || perm.second != 0)
7374 : : {
7375 : : match = false;
7376 : : break;
7377 : : }
7378 : 0 : if (!match)
7379 : 0 : continue;
7380 : :
7381 : : /* Now see if the consumer side matches. */
7382 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
7383 : 0 : pred; pred = pred->pred_next)
7384 : : {
7385 : 0 : slp_tree pred_node = m_vertices[pred->src].node;
7386 : : /* All consumers should be a permute with a single outgoing lane. */
7387 : 0 : if (SLP_TREE_CODE (pred_node) != VEC_PERM_EXPR
7388 : 0 : || SLP_TREE_LANES (pred_node) != 1)
7389 : : {
7390 : : match = false;
7391 : : break;
7392 : : }
7393 : 0 : gcc_assert (SLP_TREE_CHILDREN (pred_node).length () == 1);
7394 : : }
7395 : 0 : if (!match)
7396 : 0 : continue;
7397 : : /* Now we can mark the nodes as to use load lanes. */
7398 : 0 : node->ldst_lanes = true;
7399 : 0 : for (graph_edge *pred = m_slpg->vertices[node->vertex].pred;
7400 : 0 : pred; pred = pred->pred_next)
7401 : 0 : m_vertices[pred->src].node->ldst_lanes = true;
7402 : : /* The catch is we have to massage the mask. We have arranged
7403 : : analyzed uniform masks to be represented by a splat VEC_PERM
7404 : : which we can now simply elide as we cannot easily re-do SLP
7405 : : discovery here. */
7406 : 0 : slp_tree new_mask = SLP_TREE_CHILDREN (mask)[0];
7407 : 0 : SLP_TREE_REF_COUNT (new_mask)++;
7408 : 0 : SLP_TREE_CHILDREN (node)[0] = new_mask;
7409 : 0 : vect_free_slp_tree (mask);
7410 : : }
7411 : 620472 : }
7412 : :
7413 : : /* Main entry point for the SLP graph optimization pass. */
7414 : :
7415 : : void
7416 : 620472 : vect_optimize_slp_pass::run ()
7417 : : {
7418 : 620472 : build_graph ();
7419 : 620472 : create_partitions ();
7420 : 620472 : start_choosing_layouts ();
7421 : 620472 : if (m_perms.length () > 1)
7422 : : {
7423 : 10475 : forward_pass ();
7424 : 10475 : backward_pass ();
7425 : 10475 : if (dump_enabled_p ())
7426 : 607 : dump ();
7427 : 10475 : materialize ();
7428 : 42425 : while (!m_perms.is_empty ())
7429 : 21475 : m_perms.pop ().release ();
7430 : : }
7431 : : else
7432 : 609997 : remove_redundant_permutations ();
7433 : 620472 : free_graph (m_slpg);
7434 : 620472 : build_graph ();
7435 : 620472 : decide_masked_load_lanes ();
7436 : 620472 : free_graph (m_slpg);
7437 : 620472 : }
7438 : :
7439 : : /* Apply CSE to NODE and its children using BST_MAP. */
7440 : :
7441 : : static void
7442 : 5025608 : vect_cse_slp_nodes (scalar_stmts_to_slp_tree_map_t *bst_map, slp_tree& node)
7443 : : {
7444 : 5025608 : bool put_p = false;
7445 : 5025608 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def
7446 : : /* Besides some VEC_PERM_EXPR, two-operator nodes also
7447 : : lack scalar stmts and thus CSE doesn't work via bst_map. Ideally
7448 : : we'd have sth that works for all internal and external nodes. */
7449 : 5025608 : && !SLP_TREE_SCALAR_STMTS (node).is_empty ())
7450 : : {
7451 : 3447216 : slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node));
7452 : 3447216 : if (leader)
7453 : : {
7454 : : /* We've visited this node already. */
7455 : 328490 : if (!*leader || *leader == node)
7456 : : return;
7457 : :
7458 : 2007 : if (dump_enabled_p ())
7459 : 846 : dump_printf_loc (MSG_NOTE, vect_location,
7460 : : "re-using SLP tree %p for %p\n",
7461 : : (void *)*leader, (void *)node);
7462 : 2007 : vect_free_slp_tree (node);
7463 : 2007 : (*leader)->refcnt += 1;
7464 : 2007 : node = *leader;
7465 : 2007 : return;
7466 : : }
7467 : :
7468 : : /* Avoid creating a cycle by populating the map only after recursion. */
7469 : 3118726 : bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), nullptr);
7470 : 3118726 : node->refcnt += 1;
7471 : 3118726 : put_p = true;
7472 : : /* And recurse. */
7473 : : }
7474 : :
7475 : 14027288 : for (slp_tree &child : SLP_TREE_CHILDREN (node))
7476 : 4080796 : if (child)
7477 : 3513556 : vect_cse_slp_nodes (bst_map, child);
7478 : :
7479 : : /* Now record the node for CSE in other siblings. */
7480 : 4697118 : if (put_p)
7481 : 3118726 : *bst_map->get (SLP_TREE_SCALAR_STMTS (node)) = node;
7482 : : }
7483 : :
7484 : : /* Optimize the SLP graph of VINFO. */
7485 : :
7486 : : void
7487 : 906422 : vect_optimize_slp (vec_info *vinfo)
7488 : : {
7489 : 906422 : if (vinfo->slp_instances.is_empty ())
7490 : : return;
7491 : 620472 : vect_optimize_slp_pass (vinfo).run ();
7492 : :
7493 : : /* Apply CSE again to nodes after permute optimization. */
7494 : 620472 : scalar_stmts_to_slp_tree_map_t *bst_map
7495 : 620472 : = new scalar_stmts_to_slp_tree_map_t ();
7496 : :
7497 : 3373468 : for (auto inst : vinfo->slp_instances)
7498 : 1512052 : vect_cse_slp_nodes (bst_map, SLP_INSTANCE_TREE (inst));
7499 : :
7500 : 620472 : release_scalar_stmts_to_slp_tree_map (bst_map);
7501 : : }
7502 : :
7503 : : /* Gather loads reachable from the individual SLP graph entries. */
7504 : :
7505 : : void
7506 : 906422 : vect_gather_slp_loads (vec_info *vinfo)
7507 : : {
7508 : 906422 : unsigned i;
7509 : 906422 : slp_instance instance;
7510 : 2418474 : FOR_EACH_VEC_ELT (vinfo->slp_instances, i, instance)
7511 : : {
7512 : 1512052 : hash_set<slp_tree> visited;
7513 : 1512052 : vect_gather_slp_loads (SLP_INSTANCE_LOADS (instance),
7514 : : SLP_INSTANCE_TREE (instance), visited);
7515 : 1512052 : }
7516 : 906422 : }
7517 : :
7518 : : /* For NODE update VF based on the number of lanes and the vector types
7519 : : used. */
7520 : :
7521 : : static void
7522 : 3374869 : vect_update_slp_vf_for_node (slp_tree node, poly_uint64 &vf,
7523 : : hash_set<slp_tree> &visited)
7524 : : {
7525 : 3374869 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
7526 : 1439536 : return;
7527 : 2201878 : if (visited.add (node))
7528 : : return;
7529 : :
7530 : 7796107 : for (slp_tree child : SLP_TREE_CHILDREN (node))
7531 : 2733060 : vect_update_slp_vf_for_node (child, vf, visited);
7532 : :
7533 : : /* We do not visit SLP nodes for constants or externals - those neither
7534 : : have a vector type set yet (vectorizable_* does this) nor do they
7535 : : have max_nunits set. Instead we rely on internal nodes max_nunit
7536 : : to cover constant/external operands.
7537 : : Note that when we stop using fixed size vectors externs and constants
7538 : : shouldn't influence the (minimum) vectorization factor, instead
7539 : : vectorizable_* should honor the vectorization factor when trying to
7540 : : assign vector types to constants and externals and cause iteration
7541 : : to a higher vectorization factor when required. */
7542 : 1935333 : poly_uint64 node_vf
7543 : 1935333 : = calculate_unrolling_factor (node->max_nunits, SLP_TREE_LANES (node));
7544 : 1935333 : vf = force_common_multiple (vf, node_vf);
7545 : :
7546 : : /* For permute nodes that are fed from externs or constants we have to
7547 : : consider their number of lanes as well. Likewise for store-lanes. */
7548 : 1935333 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR
7549 : 1822820 : || node->ldst_lanes)
7550 : 460534 : for (slp_tree child : SLP_TREE_CHILDREN (node))
7551 : 122995 : if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
7552 : : {
7553 : 2136 : poly_uint64 child_vf
7554 : 2136 : = calculate_unrolling_factor (node->max_nunits,
7555 : : SLP_TREE_LANES (child));
7556 : 2136 : vf = force_common_multiple (vf, child_vf);
7557 : : }
7558 : : }
7559 : :
7560 : : /* For each possible SLP instance decide whether to SLP it and calculate overall
7561 : : unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
7562 : : least one instance. */
7563 : :
7564 : : bool
7565 : 278208 : vect_make_slp_decision (loop_vec_info loop_vinfo)
7566 : : {
7567 : 278208 : unsigned int i;
7568 : 278208 : poly_uint64 unrolling_factor = 1;
7569 : 278208 : const vec<slp_instance> &slp_instances
7570 : : = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
7571 : 278208 : slp_instance instance;
7572 : 278208 : int decided_to_slp = 0;
7573 : :
7574 : 278208 : DUMP_VECT_SCOPE ("vect_make_slp_decision");
7575 : :
7576 : 278208 : hash_set<slp_tree> visited;
7577 : 1198225 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
7578 : : {
7579 : : /* FORNOW: SLP if you can. */
7580 : : /* All unroll factors have the form:
7581 : :
7582 : : GET_MODE_SIZE (vinfo->vector_mode) * X
7583 : :
7584 : : for some rational X, so they must have a common multiple. */
7585 : 641809 : vect_update_slp_vf_for_node (SLP_INSTANCE_TREE (instance),
7586 : : unrolling_factor, visited);
7587 : :
7588 : : /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
7589 : : call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
7590 : : loop-based vectorization. Such stmts will be marked as HYBRID. */
7591 : 641809 : vect_mark_slp_stmts (loop_vinfo, SLP_INSTANCE_TREE (instance));
7592 : 641809 : decided_to_slp++;
7593 : : }
7594 : :
7595 : 278208 : LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
7596 : :
7597 : 278208 : if (decided_to_slp && dump_enabled_p ())
7598 : : {
7599 : 16687 : dump_printf_loc (MSG_NOTE, vect_location,
7600 : : "Decided to SLP %d instances. Unrolling factor ",
7601 : : decided_to_slp);
7602 : 16687 : dump_dec (MSG_NOTE, unrolling_factor);
7603 : 16687 : dump_printf (MSG_NOTE, "\n");
7604 : : }
7605 : :
7606 : 278208 : return (decided_to_slp > 0);
7607 : 278208 : }
7608 : :
7609 : : /* Private data for vect_detect_hybrid_slp. */
7610 : : struct vdhs_data
7611 : : {
7612 : : loop_vec_info loop_vinfo;
7613 : : vec<stmt_vec_info> *worklist;
7614 : : };
7615 : :
7616 : : /* Walker for walk_gimple_op. */
7617 : :
7618 : : static tree
7619 : 18380 : vect_detect_hybrid_slp (tree *tp, int *, void *data)
7620 : : {
7621 : 18380 : walk_stmt_info *wi = (walk_stmt_info *)data;
7622 : 18380 : vdhs_data *dat = (vdhs_data *)wi->info;
7623 : :
7624 : 18380 : if (wi->is_lhs)
7625 : : return NULL_TREE;
7626 : :
7627 : 11980 : stmt_vec_info def_stmt_info = dat->loop_vinfo->lookup_def (*tp);
7628 : 11980 : if (!def_stmt_info)
7629 : : return NULL_TREE;
7630 : 5141 : def_stmt_info = vect_stmt_to_vectorize (def_stmt_info);
7631 : 5141 : if (PURE_SLP_STMT (def_stmt_info))
7632 : : {
7633 : 2342 : if (dump_enabled_p ())
7634 : 83 : dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: %G",
7635 : : def_stmt_info->stmt);
7636 : 2342 : STMT_SLP_TYPE (def_stmt_info) = hybrid;
7637 : 2342 : dat->worklist->safe_push (def_stmt_info);
7638 : : }
7639 : :
7640 : : return NULL_TREE;
7641 : : }
7642 : :
7643 : : /* Look if STMT_INFO is consumed by SLP indirectly and mark it pure_slp
7644 : : if so, otherwise pushing it to WORKLIST. */
7645 : :
7646 : : static void
7647 : 148290 : maybe_push_to_hybrid_worklist (vec_info *vinfo,
7648 : : vec<stmt_vec_info> &worklist,
7649 : : stmt_vec_info stmt_info)
7650 : : {
7651 : 148290 : if (dump_enabled_p ())
7652 : 3079 : dump_printf_loc (MSG_NOTE, vect_location,
7653 : : "Processing hybrid candidate : %G", stmt_info->stmt);
7654 : 148290 : stmt_vec_info orig_info = vect_orig_stmt (stmt_info);
7655 : 148290 : imm_use_iterator iter2;
7656 : 148290 : ssa_op_iter iter1;
7657 : 148290 : use_operand_p use_p;
7658 : 148290 : def_operand_p def_p;
7659 : 148290 : bool any_def = false;
7660 : 297890 : FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_info->stmt, iter1, SSA_OP_DEF)
7661 : : {
7662 : 4277 : any_def = true;
7663 : 5685 : FOR_EACH_IMM_USE_FAST (use_p, iter2, DEF_FROM_PTR (def_p))
7664 : : {
7665 : 4375 : if (is_gimple_debug (USE_STMT (use_p)))
7666 : 67 : continue;
7667 : 4308 : stmt_vec_info use_info = vinfo->lookup_stmt (USE_STMT (use_p));
7668 : : /* An out-of loop use means this is a loop_vect sink. */
7669 : 4308 : if (!use_info)
7670 : : {
7671 : 1748 : if (dump_enabled_p ())
7672 : 84 : dump_printf_loc (MSG_NOTE, vect_location,
7673 : : "Found loop_vect sink: %G", stmt_info->stmt);
7674 : 1748 : worklist.safe_push (stmt_info);
7675 : 5046 : return;
7676 : : }
7677 : 2843 : else if (!STMT_SLP_TYPE (vect_stmt_to_vectorize (use_info)))
7678 : : {
7679 : 1219 : if (dump_enabled_p ())
7680 : 459 : dump_printf_loc (MSG_NOTE, vect_location,
7681 : : "Found loop_vect use: %G", use_info->stmt);
7682 : 1219 : worklist.safe_push (stmt_info);
7683 : 1219 : return;
7684 : : }
7685 : : }
7686 : : }
7687 : : /* No def means this is a loop_vect sink. Gimple conditionals also don't have a
7688 : : def but shouldn't be considered sinks. */
7689 : 145323 : if (!any_def && STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
7690 : : {
7691 : 331 : if (dump_enabled_p ())
7692 : 47 : dump_printf_loc (MSG_NOTE, vect_location,
7693 : : "Found loop_vect sink: %G", stmt_info->stmt);
7694 : 331 : worklist.safe_push (stmt_info);
7695 : 331 : return;
7696 : : }
7697 : 144992 : if (dump_enabled_p ())
7698 : 2489 : dump_printf_loc (MSG_NOTE, vect_location,
7699 : : "Marked SLP consumed stmt pure: %G", stmt_info->stmt);
7700 : 144992 : STMT_SLP_TYPE (stmt_info) = pure_slp;
7701 : : }
7702 : :
7703 : : /* Find stmts that must be both vectorized and SLPed. */
7704 : :
7705 : : bool
7706 : 276605 : vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
7707 : : {
7708 : 276605 : DUMP_VECT_SCOPE ("vect_detect_hybrid_slp");
7709 : :
7710 : : /* All stmts participating in SLP are marked pure_slp, all other
7711 : : stmts are loop_vect.
7712 : : First collect all loop_vect stmts into a worklist.
7713 : : SLP patterns cause not all original scalar stmts to appear in
7714 : : SLP_TREE_SCALAR_STMTS and thus not all of them are marked pure_slp.
7715 : : Rectify this here and do a backward walk over the IL only considering
7716 : : stmts as loop_vect when they are used by a loop_vect stmt and otherwise
7717 : : mark them as pure_slp. */
7718 : 276605 : auto_vec<stmt_vec_info> worklist;
7719 : 977843 : for (int i = LOOP_VINFO_LOOP (loop_vinfo)->num_nodes - 1; i >= 0; --i)
7720 : : {
7721 : 701238 : basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
7722 : 1405526 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7723 : 704288 : gsi_next (&gsi))
7724 : : {
7725 : 704288 : gphi *phi = gsi.phi ();
7726 : 704288 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (phi);
7727 : 704288 : if (!STMT_SLP_TYPE (stmt_info) && STMT_VINFO_RELEVANT (stmt_info))
7728 : 1833 : maybe_push_to_hybrid_worklist (loop_vinfo,
7729 : : worklist, stmt_info);
7730 : : }
7731 : 701238 : for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
7732 : 8351866 : gsi_prev (&gsi))
7733 : : {
7734 : 3825314 : gimple *stmt = gsi_stmt (gsi);
7735 : 3825314 : if (is_gimple_debug (stmt))
7736 : 1106769 : continue;
7737 : 2718545 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
7738 : 2718545 : if (STMT_VINFO_IN_PATTERN_P (stmt_info))
7739 : : {
7740 : 341630 : for (gimple_stmt_iterator gsi2
7741 : 341630 : = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
7742 : 703134 : !gsi_end_p (gsi2); gsi_next (&gsi2))
7743 : : {
7744 : 361504 : stmt_vec_info patt_info
7745 : 361504 : = loop_vinfo->lookup_stmt (gsi_stmt (gsi2));
7746 : 361504 : if (!STMT_SLP_TYPE (patt_info)
7747 : 145407 : && STMT_VINFO_RELEVANT (patt_info))
7748 : 317 : maybe_push_to_hybrid_worklist (loop_vinfo,
7749 : : worklist, patt_info);
7750 : : }
7751 : 341630 : stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
7752 : : }
7753 : 2718545 : if (!STMT_SLP_TYPE (stmt_info) && STMT_VINFO_RELEVANT (stmt_info))
7754 : 146140 : maybe_push_to_hybrid_worklist (loop_vinfo,
7755 : : worklist, stmt_info);
7756 : : }
7757 : : }
7758 : :
7759 : : /* Now we have a worklist of non-SLP stmts, follow use->def chains and
7760 : : mark any SLP vectorized stmt as hybrid.
7761 : : ??? We're visiting def stmts N times (once for each non-SLP and
7762 : : once for each hybrid-SLP use). */
7763 : 276605 : walk_stmt_info wi;
7764 : 276605 : vdhs_data dat;
7765 : 276605 : dat.worklist = &worklist;
7766 : 276605 : dat.loop_vinfo = loop_vinfo;
7767 : 276605 : memset (&wi, 0, sizeof (wi));
7768 : 276605 : wi.info = (void *)&dat;
7769 : 282245 : while (!worklist.is_empty ())
7770 : : {
7771 : 5640 : stmt_vec_info stmt_info = worklist.pop ();
7772 : : /* Since SSA operands are not set up for pattern stmts we need
7773 : : to use walk_gimple_op. */
7774 : 5640 : wi.is_lhs = 0;
7775 : 5640 : walk_gimple_op (stmt_info->stmt, vect_detect_hybrid_slp, &wi);
7776 : : /* For gather/scatter make sure to walk the offset operand, that
7777 : : can be a scaling and conversion away. */
7778 : 5640 : gather_scatter_info gs_info;
7779 : 5640 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
7780 : 5640 : && vect_check_gather_scatter (stmt_info, loop_vinfo, &gs_info))
7781 : : {
7782 : 14 : int dummy;
7783 : 14 : vect_detect_hybrid_slp (&gs_info.offset, &dummy, &wi);
7784 : : }
7785 : : }
7786 : :
7787 : : /* Determine if all the stmts in the loop can be SLPed. */
7788 : 974048 : for (unsigned i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; i++)
7789 : : {
7790 : 698639 : basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
7791 : 1398910 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7792 : 700271 : gsi_next (&si))
7793 : : {
7794 : 701378 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (si.phi ());
7795 : 701378 : if (!stmt_info)
7796 : 0 : continue;
7797 : 701378 : if ((STMT_VINFO_RELEVANT_P (stmt_info)
7798 : 385139 : || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
7799 : 316251 : && !PURE_SLP_STMT (stmt_info))
7800 : : {
7801 : : /* STMT needs both SLP and loop-based vectorization. */
7802 : 1107 : if (dump_enabled_p ())
7803 : 40 : dump_printf_loc (MSG_NOTE, vect_location,
7804 : : "Loop contains SLP and non-SLP stmts\n");
7805 : 1107 : return false;
7806 : : }
7807 : : }
7808 : 5201101 : for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7809 : 3806037 : gsi_next (&si))
7810 : : {
7811 : 3806126 : if (is_gimple_debug (gsi_stmt (si)))
7812 : 1099150 : continue;
7813 : 2706976 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
7814 : 2706976 : stmt_info = vect_stmt_to_vectorize (stmt_info);
7815 : 2706976 : if ((STMT_VINFO_RELEVANT_P (stmt_info)
7816 : 1175170 : || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
7817 : 1531806 : && !PURE_SLP_STMT (stmt_info))
7818 : : {
7819 : : /* STMT needs both SLP and loop-based vectorization. */
7820 : 89 : if (dump_enabled_p ())
7821 : 32 : dump_printf_loc (MSG_NOTE, vect_location,
7822 : : "Loop contains SLP and non-SLP stmts\n");
7823 : 89 : return false;
7824 : : }
7825 : : }
7826 : : }
7827 : :
7828 : 275409 : if (dump_enabled_p ())
7829 : 16615 : dump_printf_loc (MSG_NOTE, vect_location,
7830 : : "Loop contains only SLP stmts\n");
7831 : : return true;
7832 : 276605 : }
7833 : :
7834 : :
7835 : : /* Initialize a bb_vec_info struct for the statements in BBS basic blocks. */
7836 : :
7837 : 2389978 : _bb_vec_info::_bb_vec_info (vec<basic_block> _bbs, vec_info_shared *shared)
7838 : : : vec_info (vec_info::bb, shared),
7839 : 2389978 : roots (vNULL)
7840 : : {
7841 : : /* The region we are operating on. bbs[0] is the entry, excluding
7842 : : its PHI nodes. In the future we might want to track an explicit
7843 : : entry edge to cover bbs[0] PHI nodes and have a region entry
7844 : : insert location. */
7845 : 2389978 : bbs = _bbs.address ();
7846 : 2389978 : nbbs = _bbs.length ();
7847 : :
7848 : 18597676 : for (unsigned i = 0; i < nbbs; ++i)
7849 : : {
7850 : 16207698 : if (i != 0)
7851 : 21507561 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
7852 : 7689841 : gsi_next (&si))
7853 : : {
7854 : 7689841 : gphi *phi = si.phi ();
7855 : 7689841 : gimple_set_uid (phi, 0);
7856 : 7689841 : add_stmt (phi);
7857 : : }
7858 : 32415396 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
7859 : 131557883 : !gsi_end_p (gsi); gsi_next (&gsi))
7860 : : {
7861 : 115350185 : gimple *stmt = gsi_stmt (gsi);
7862 : 115350185 : gimple_set_uid (stmt, 0);
7863 : 115350185 : if (is_gimple_debug (stmt))
7864 : 69780939 : continue;
7865 : 45569246 : add_stmt (stmt);
7866 : : }
7867 : : }
7868 : 2389978 : }
7869 : :
7870 : :
7871 : : /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
7872 : : stmts in the basic block. */
7873 : :
7874 : 2389978 : _bb_vec_info::~_bb_vec_info ()
7875 : : {
7876 : : /* Reset region marker. */
7877 : 18597676 : for (unsigned i = 0; i < nbbs; ++i)
7878 : : {
7879 : 16207698 : if (i != 0)
7880 : 21522298 : for (gphi_iterator si = gsi_start_phis (bbs[i]); !gsi_end_p (si);
7881 : 7704578 : gsi_next (&si))
7882 : : {
7883 : 7704578 : gphi *phi = si.phi ();
7884 : 7704578 : gimple_set_uid (phi, -1);
7885 : : }
7886 : 32415396 : for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
7887 : 131483438 : !gsi_end_p (gsi); gsi_next (&gsi))
7888 : : {
7889 : 115275740 : gimple *stmt = gsi_stmt (gsi);
7890 : 115275740 : gimple_set_uid (stmt, -1);
7891 : : }
7892 : : }
7893 : :
7894 : 3580217 : for (unsigned i = 0; i < roots.length (); ++i)
7895 : : {
7896 : 1190239 : roots[i].stmts.release ();
7897 : 1190239 : roots[i].roots.release ();
7898 : 1190239 : roots[i].remain.release ();
7899 : : }
7900 : 2389978 : roots.release ();
7901 : 2389978 : }
7902 : :
7903 : : /* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
7904 : : given then that child nodes have already been processed, and that
7905 : : their def types currently match their SLP node's def type. */
7906 : :
7907 : : static bool
7908 : 2394351 : vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
7909 : : slp_instance node_instance,
7910 : : stmt_vector_for_cost *cost_vec)
7911 : : {
7912 : : /* Calculate the number of vector statements to be created for the scalar
7913 : : stmts in this node. It is the number of scalar elements in one scalar
7914 : : iteration (DR_GROUP_SIZE) multiplied by VF divided by the number of
7915 : : elements in a vector. For single-defuse-cycle, lane-reducing op, and
7916 : : PHI statement that starts reduction comprised of only lane-reducing ops,
7917 : : the number is more than effective vector statements actually required. */
7918 : 2394351 : SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vect_get_num_copies (vinfo, node);
7919 : :
7920 : : /* Handle purely internal nodes. */
7921 : 2394351 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
7922 : : {
7923 : 95926 : if (!vectorizable_slp_permutation (vinfo, NULL, node, cost_vec))
7924 : : return false;
7925 : :
7926 : : stmt_vec_info slp_stmt_info;
7927 : : unsigned int i;
7928 : 248323 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
7929 : : {
7930 : 153623 : if (slp_stmt_info
7931 : 149158 : && STMT_VINFO_LIVE_P (slp_stmt_info)
7932 : 153641 : && !vectorizable_live_operation (vinfo, slp_stmt_info, node,
7933 : : node_instance, i,
7934 : : false, cost_vec))
7935 : : return false;
7936 : : }
7937 : : return true;
7938 : : }
7939 : :
7940 : 2298425 : return vect_analyze_stmt (vinfo, node, node_instance, cost_vec);
7941 : : }
7942 : :
7943 : : static int
7944 : 1873987 : sort_ints (const void *a_, const void *b_)
7945 : : {
7946 : 1873987 : int a = *(const int *)a_;
7947 : 1873987 : int b = *(const int *)b_;
7948 : 1873987 : return a - b;
7949 : : }
7950 : :
7951 : : /* Verify if we can externalize a set of internal defs. */
7952 : :
7953 : : static bool
7954 : 409882 : vect_slp_can_convert_to_external (const vec<stmt_vec_info> &stmts)
7955 : : {
7956 : : /* Constant generation uses get_later_stmt which can only handle
7957 : : defs from the same BB or a set of defs that can be ordered
7958 : : with a dominance query. */
7959 : 409882 : basic_block bb = NULL;
7960 : 409882 : bool all_same = true;
7961 : 409882 : auto_vec<int> bbs;
7962 : 819764 : bbs.reserve_exact (stmts.length ());
7963 : 2200808 : for (stmt_vec_info stmt : stmts)
7964 : : {
7965 : 971162 : if (!stmt)
7966 : : return false;
7967 : 971162 : else if (!bb)
7968 : 409882 : bb = gimple_bb (stmt->stmt);
7969 : 561280 : else if (gimple_bb (stmt->stmt) != bb)
7970 : 176629 : all_same = false;
7971 : 971162 : bbs.quick_push (gimple_bb (stmt->stmt)->index);
7972 : : }
7973 : 409882 : if (all_same)
7974 : : return true;
7975 : :
7976 : : /* Produce a vector of unique BB indexes for the defs. */
7977 : 132969 : bbs.qsort (sort_ints);
7978 : : unsigned i, j;
7979 : 323651 : for (i = 1, j = 1; i < bbs.length (); ++i)
7980 : 190682 : if (bbs[i] != bbs[j-1])
7981 : 141722 : bbs[j++] = bbs[i];
7982 : 132969 : gcc_assert (j >= 2);
7983 : 132969 : bbs.truncate (j);
7984 : :
7985 : 265938 : if (bbs.length () == 2)
7986 : 129504 : return (dominated_by_p (CDI_DOMINATORS,
7987 : 129504 : BASIC_BLOCK_FOR_FN (cfun, bbs[0]),
7988 : 129504 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]))
7989 : 251520 : || dominated_by_p (CDI_DOMINATORS,
7990 : 122016 : BASIC_BLOCK_FOR_FN (cfun, bbs[1]),
7991 : 122016 : BASIC_BLOCK_FOR_FN (cfun, bbs[0])));
7992 : :
7993 : : /* ??? For more than two BBs we can sort the vector and verify the
7994 : : result is a total order. But we can't use vec::qsort with a
7995 : : compare function using a dominance query since there's no way to
7996 : : signal failure and any fallback for an unordered pair would
7997 : : fail qsort_chk later.
7998 : : For now simply hope that ordering after BB index provides the
7999 : : best candidate total order. If required we can implement our
8000 : : own mergesort or export an entry without checking. */
8001 : 425491 : for (unsigned i = 1; i < bbs.length (); ++i)
8002 : 12190 : if (!dominated_by_p (CDI_DOMINATORS,
8003 : 12190 : BASIC_BLOCK_FOR_FN (cfun, bbs[i]),
8004 : 12190 : BASIC_BLOCK_FOR_FN (cfun, bbs[i-1])))
8005 : : return false;
8006 : :
8007 : : return true;
8008 : 409882 : }
8009 : :
8010 : : /* Try to build NODE from scalars, returning true on success.
8011 : : NODE_INSTANCE is the SLP instance that contains NODE. */
8012 : :
8013 : : static bool
8014 : 427165 : vect_slp_convert_to_external (vec_info *vinfo, slp_tree node,
8015 : : slp_instance node_instance)
8016 : : {
8017 : 427165 : stmt_vec_info stmt_info;
8018 : 427165 : unsigned int i;
8019 : :
8020 : 427165 : if (!is_a <bb_vec_info> (vinfo)
8021 : 87857 : || node == SLP_INSTANCE_TREE (node_instance)
8022 : 27846 : || !SLP_TREE_SCALAR_STMTS (node).exists ()
8023 : 27785 : || vect_contains_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (node))
8024 : : /* Force the mask use to be built from scalars instead. */
8025 : 25174 : || VECTOR_BOOLEAN_TYPE_P (SLP_TREE_VECTYPE (node))
8026 : 452131 : || !vect_slp_can_convert_to_external (SLP_TREE_SCALAR_STMTS (node)))
8027 : 402199 : return false;
8028 : :
8029 : 24966 : if (dump_enabled_p ())
8030 : 83 : dump_printf_loc (MSG_NOTE, vect_location,
8031 : : "Building vector operands of %p from scalars instead\n",
8032 : : (void *) node);
8033 : :
8034 : : /* Don't remove and free the child nodes here, since they could be
8035 : : referenced by other structures. The analysis and scheduling phases
8036 : : (need to) ignore child nodes of anything that isn't vect_internal_def. */
8037 : 24966 : unsigned int group_size = SLP_TREE_LANES (node);
8038 : 24966 : SLP_TREE_DEF_TYPE (node) = vect_external_def;
8039 : : /* Invariants get their vector type from the uses. */
8040 : 24966 : SLP_TREE_VECTYPE (node) = NULL_TREE;
8041 : 24966 : SLP_TREE_SCALAR_OPS (node).safe_grow (group_size, true);
8042 : 24966 : SLP_TREE_LOAD_PERMUTATION (node).release ();
8043 : 86935 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8044 : : {
8045 : 61969 : tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
8046 : 61969 : SLP_TREE_SCALAR_OPS (node)[i] = lhs;
8047 : : }
8048 : : return true;
8049 : : }
8050 : :
8051 : : /* Return true if all elements of the slice are the same. */
8052 : : bool
8053 : 598592 : vect_scalar_ops_slice::all_same_p () const
8054 : : {
8055 : 646774 : for (unsigned int i = 1; i < length; ++i)
8056 : 429586 : if (!operand_equal_p (op (0), op (i)))
8057 : : return false;
8058 : : return true;
8059 : : }
8060 : :
8061 : : hashval_t
8062 : 689083 : vect_scalar_ops_slice_hash::hash (const value_type &s)
8063 : : {
8064 : 689083 : hashval_t hash = 0;
8065 : 2081569 : for (unsigned i = 0; i < s.length; ++i)
8066 : 1392486 : hash = iterative_hash_expr (s.op (i), hash);
8067 : 689083 : return hash;
8068 : : }
8069 : :
8070 : : bool
8071 : 344944 : vect_scalar_ops_slice_hash::equal (const value_type &s1,
8072 : : const compare_type &s2)
8073 : : {
8074 : 344944 : if (s1.length != s2.length)
8075 : : return false;
8076 : 524529 : for (unsigned i = 0; i < s1.length; ++i)
8077 : 448625 : if (!operand_equal_p (s1.op (i), s2.op (i)))
8078 : : return false;
8079 : : return true;
8080 : : }
8081 : :
8082 : : /* Compute the prologue cost for invariant or constant operands represented
8083 : : by NODE. */
8084 : :
8085 : : static void
8086 : 1086608 : vect_prologue_cost_for_slp (slp_tree node,
8087 : : stmt_vector_for_cost *cost_vec)
8088 : : {
8089 : : /* There's a special case of an existing vector, that costs nothing. */
8090 : 1086608 : if (SLP_TREE_SCALAR_OPS (node).length () == 0
8091 : 1086608 : && !SLP_TREE_VEC_DEFS (node).is_empty ())
8092 : 1853 : return;
8093 : : /* Without looking at the actual initializer a vector of
8094 : : constants can be implemented as load from the constant pool.
8095 : : When all elements are the same we can use a splat. */
8096 : 1084755 : tree vectype = SLP_TREE_VECTYPE (node);
8097 : 1084755 : unsigned group_size = SLP_TREE_SCALAR_OPS (node).length ();
8098 : 1084755 : unsigned HOST_WIDE_INT const_nunits;
8099 : 1084755 : unsigned nelt_limit;
8100 : 1084755 : auto ops = &SLP_TREE_SCALAR_OPS (node);
8101 : 1084755 : auto_vec<unsigned int> starts (SLP_TREE_NUMBER_OF_VEC_STMTS (node));
8102 : 1084755 : if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
8103 : 1084755 : && ! multiple_p (const_nunits, group_size))
8104 : : {
8105 : 137649 : nelt_limit = const_nunits;
8106 : 137649 : hash_set<vect_scalar_ops_slice_hash> vector_ops;
8107 : 512455 : for (unsigned int i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); ++i)
8108 : 374806 : if (!vector_ops.add ({ ops, i * nelt_limit, nelt_limit }))
8109 : 298902 : starts.quick_push (i * nelt_limit);
8110 : 137649 : }
8111 : : else
8112 : : {
8113 : : /* If either the vector has variable length or the vectors
8114 : : are composed of repeated whole groups we only need to
8115 : : cost construction once. All vectors will be the same. */
8116 : 947106 : nelt_limit = group_size;
8117 : 947106 : starts.quick_push (0);
8118 : : }
8119 : : /* ??? We're just tracking whether vectors in a single node are the same.
8120 : : Ideally we'd do something more global. */
8121 : 1084755 : bool passed = false;
8122 : 4500273 : for (unsigned int start : starts)
8123 : : {
8124 : 1246008 : vect_cost_for_stmt kind;
8125 : 1246008 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def)
8126 : : kind = vector_load;
8127 : 598592 : else if (vect_scalar_ops_slice { ops, start, nelt_limit }.all_same_p ())
8128 : : kind = scalar_to_vec;
8129 : : else
8130 : 381404 : kind = vec_construct;
8131 : : /* The target cost hook has no idea which part of the SLP node
8132 : : we are costing so avoid passing it down more than once. Pass
8133 : : it to the first vec_construct or scalar_to_vec part since for those
8134 : : the x86 backend tries to account for GPR to XMM register moves. */
8135 : 1246008 : record_stmt_cost (cost_vec, 1, kind, nullptr,
8136 : 1246008 : (kind != vector_load && !passed) ? node : nullptr,
8137 : : vectype, 0, vect_prologue);
8138 : 1246008 : if (kind != vector_load)
8139 : 598592 : passed = true;
8140 : : }
8141 : 1084755 : }
8142 : :
8143 : : /* Analyze statements contained in SLP tree NODE after recursively analyzing
8144 : : the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
8145 : :
8146 : : Return true if the operations are supported. */
8147 : :
8148 : : static bool
8149 : 4436870 : vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
8150 : : slp_instance node_instance,
8151 : : hash_set<slp_tree> &visited_set,
8152 : : vec<slp_tree> &visited_vec,
8153 : : stmt_vector_for_cost *cost_vec)
8154 : : {
8155 : 4436870 : int i, j;
8156 : 4436870 : slp_tree child;
8157 : :
8158 : : /* Assume we can code-generate all invariants. */
8159 : 4436870 : if (!node
8160 : 4182363 : || SLP_TREE_DEF_TYPE (node) == vect_constant_def
8161 : 3452389 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
8162 : : return true;
8163 : :
8164 : 2872300 : if (SLP_TREE_DEF_TYPE (node) == vect_uninitialized_def)
8165 : : {
8166 : 21 : if (dump_enabled_p ())
8167 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
8168 : : "Failed cyclic SLP reference in %p\n", (void *) node);
8169 : 21 : return false;
8170 : : }
8171 : 2872279 : gcc_assert (SLP_TREE_DEF_TYPE (node) == vect_internal_def);
8172 : :
8173 : : /* If we already analyzed the exact same set of scalar stmts we're done.
8174 : : We share the generated vector stmts for those. */
8175 : 2872279 : if (visited_set.add (node))
8176 : : return true;
8177 : 2624022 : visited_vec.safe_push (node);
8178 : :
8179 : 2624022 : bool res = true;
8180 : 2624022 : unsigned visited_rec_start = visited_vec.length ();
8181 : 2624022 : unsigned cost_vec_rec_start = cost_vec->length ();
8182 : 2624022 : bool seen_non_constant_child = false;
8183 : 5611177 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8184 : : {
8185 : 3216678 : res = vect_slp_analyze_node_operations (vinfo, child, node_instance,
8186 : : visited_set, visited_vec,
8187 : : cost_vec);
8188 : 3216678 : if (!res)
8189 : : break;
8190 : 2987155 : if (child && SLP_TREE_DEF_TYPE (child) != vect_constant_def)
8191 : 2987155 : seen_non_constant_child = true;
8192 : : }
8193 : : /* We're having difficulties scheduling nodes with just constant
8194 : : operands and no scalar stmts since we then cannot compute a stmt
8195 : : insertion place. */
8196 : 2624022 : if (res
8197 : 2624022 : && !seen_non_constant_child
8198 : 2624022 : && SLP_TREE_SCALAR_STMTS (node).is_empty ())
8199 : : {
8200 : 148 : if (dump_enabled_p ())
8201 : 4 : dump_printf_loc (MSG_NOTE, vect_location,
8202 : : "Cannot vectorize all-constant op node %p\n",
8203 : : (void *) node);
8204 : : res = false;
8205 : : }
8206 : :
8207 : 2623874 : if (res)
8208 : 2394351 : res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
8209 : : cost_vec);
8210 : : /* If analysis failed we have to pop all recursive visited nodes
8211 : : plus ourselves. */
8212 : 2624022 : if (!res)
8213 : : {
8214 : 2263840 : while (visited_vec.length () >= visited_rec_start)
8215 : 704755 : visited_set.remove (visited_vec.pop ());
8216 : 427165 : cost_vec->truncate (cost_vec_rec_start);
8217 : : }
8218 : :
8219 : : /* When the node can be vectorized cost invariant nodes it references.
8220 : : This is not done in DFS order to allow the refering node
8221 : : vectorizable_* calls to nail down the invariant nodes vector type
8222 : : and possibly unshare it if it needs a different vector type than
8223 : : other referrers. */
8224 : 2624022 : if (res)
8225 : 4874910 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
8226 : 2678053 : if (child
8227 : 2439307 : && (SLP_TREE_DEF_TYPE (child) == vect_constant_def
8228 : 2439307 : || SLP_TREE_DEF_TYPE (child) == vect_external_def)
8229 : : /* Perform usual caching, note code-generation still
8230 : : code-gens these nodes multiple times but we expect
8231 : : to CSE them later. */
8232 : 3825995 : && !visited_set.add (child))
8233 : : {
8234 : 1123437 : visited_vec.safe_push (child);
8235 : : /* ??? After auditing more code paths make a "default"
8236 : : and push the vector type from NODE to all children
8237 : : if it is not already set. */
8238 : : /* Compute the number of vectors to be generated. */
8239 : 1123437 : tree vector_type = SLP_TREE_VECTYPE (child);
8240 : 1123437 : if (!vector_type)
8241 : : {
8242 : : /* Masked loads can have an undefined (default SSA definition)
8243 : : else operand. We do not need to cost it. */
8244 : 36829 : vec<tree> ops = SLP_TREE_SCALAR_OPS (child);
8245 : 37782 : if ((STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (node))
8246 : : == load_vec_info_type)
8247 : 37782 : && ((ops.length ()
8248 : 953 : && TREE_CODE (ops[0]) == SSA_NAME
8249 : 0 : && SSA_NAME_IS_DEFAULT_DEF (ops[0])
8250 : 0 : && VAR_P (SSA_NAME_VAR (ops[0])))
8251 : 953 : || SLP_TREE_DEF_TYPE (child) == vect_constant_def))
8252 : 953 : continue;
8253 : :
8254 : : /* For shifts with a scalar argument we don't need
8255 : : to cost or code-generate anything.
8256 : : ??? Represent this more explicitely. */
8257 : 35876 : gcc_assert ((STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (node))
8258 : : == shift_vec_info_type)
8259 : : && j == 1);
8260 : 35876 : continue;
8261 : 35876 : }
8262 : :
8263 : 1086608 : SLP_TREE_NUMBER_OF_VEC_STMTS (child)
8264 : 1086608 : = vect_get_num_copies (vinfo, child);
8265 : : /* And cost them. */
8266 : 1086608 : vect_prologue_cost_for_slp (child, cost_vec);
8267 : : }
8268 : :
8269 : : /* If this node or any of its children can't be vectorized, try pruning
8270 : : the tree here rather than felling the whole thing. */
8271 : 427165 : if (!res && vect_slp_convert_to_external (vinfo, node, node_instance))
8272 : : {
8273 : : /* We'll need to revisit this for invariant costing and number
8274 : : of vectorized stmt setting. */
8275 : : res = true;
8276 : : }
8277 : :
8278 : : return res;
8279 : : }
8280 : :
8281 : : /* Given a definition DEF, analyze if it will have any live scalar use after
8282 : : performing SLP vectorization whose information is represented by BB_VINFO,
8283 : : and record result into hash map SCALAR_USE_MAP as cache for later fast
8284 : : check. If recursion DEPTH exceeds a limit, stop analysis and make a
8285 : : conservative assumption. Return 0 if no scalar use, 1 if there is, -1
8286 : : means recursion is limited. */
8287 : :
8288 : : static int
8289 : 578894 : vec_slp_has_scalar_use (bb_vec_info bb_vinfo, tree def,
8290 : : hash_map<tree, int> &scalar_use_map,
8291 : : int depth = 0)
8292 : : {
8293 : 578894 : const int depth_limit = 2;
8294 : 578894 : imm_use_iterator use_iter;
8295 : 578894 : gimple *use_stmt;
8296 : :
8297 : 578894 : if (int *res = scalar_use_map.get (def))
8298 : 23274 : return *res;
8299 : :
8300 : 555620 : int scalar_use = 1;
8301 : :
8302 : 1249334 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
8303 : : {
8304 : 818249 : if (is_gimple_debug (use_stmt))
8305 : 175315 : continue;
8306 : :
8307 : 642934 : stmt_vec_info use_stmt_info = bb_vinfo->lookup_stmt (use_stmt);
8308 : :
8309 : 642934 : if (!use_stmt_info)
8310 : : break;
8311 : :
8312 : 643968 : if (PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
8313 : 516726 : continue;
8314 : :
8315 : : /* Do not step forward when encounter PHI statement, since it may
8316 : : involve cyclic reference and cause infinite recursive invocation. */
8317 : 118688 : if (gimple_code (use_stmt) == GIMPLE_PHI)
8318 : : break;
8319 : :
8320 : : /* When pattern recognition is involved, a statement whose definition is
8321 : : consumed in some pattern, may not be included in the final replacement
8322 : : pattern statements, so would be skipped when building SLP graph.
8323 : :
8324 : : * Original
8325 : : char a_c = *(char *) a;
8326 : : char b_c = *(char *) b;
8327 : : unsigned short a_s = (unsigned short) a_c;
8328 : : int a_i = (int) a_s;
8329 : : int b_i = (int) b_c;
8330 : : int r_i = a_i - b_i;
8331 : :
8332 : : * After pattern replacement
8333 : : a_s = (unsigned short) a_c;
8334 : : a_i = (int) a_s;
8335 : :
8336 : : patt_b_s = (unsigned short) b_c; // b_i = (int) b_c
8337 : : patt_b_i = (int) patt_b_s; // b_i = (int) b_c
8338 : :
8339 : : patt_r_s = widen_minus(a_c, b_c); // r_i = a_i - b_i
8340 : : patt_r_i = (int) patt_r_s; // r_i = a_i - b_i
8341 : :
8342 : : The definitions of a_i(original statement) and b_i(pattern statement)
8343 : : are related to, but actually not part of widen_minus pattern.
8344 : : Vectorizing the pattern does not cause these definition statements to
8345 : : be marked as PURE_SLP. For this case, we need to recursively check
8346 : : whether their uses are all absorbed into vectorized code. But there
8347 : : is an exception that some use may participate in an vectorized
8348 : : operation via an external SLP node containing that use as an element.
8349 : : The parameter "scalar_use_map" tags such kind of SSA as having scalar
8350 : : use in advance. */
8351 : 95119 : tree lhs = gimple_get_lhs (use_stmt);
8352 : :
8353 : 95119 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
8354 : : break;
8355 : :
8356 : 60444 : if (depth_limit && depth >= depth_limit)
8357 : 11009 : return -1;
8358 : :
8359 : 49435 : if ((scalar_use = vec_slp_has_scalar_use (bb_vinfo, lhs, scalar_use_map,
8360 : : depth + 1)))
8361 : : break;
8362 : 555620 : }
8363 : :
8364 : 544611 : if (end_imm_use_stmt_p (&use_iter))
8365 : 431085 : scalar_use = 0;
8366 : :
8367 : : /* If recursion is limited, do not cache result for non-root defs. */
8368 : 544611 : if (!depth || scalar_use >= 0)
8369 : : {
8370 : 533602 : bool added = scalar_use_map.put (def, scalar_use);
8371 : 533602 : gcc_assert (!added);
8372 : : }
8373 : :
8374 : 544611 : return scalar_use;
8375 : : }
8376 : :
8377 : : /* Mark lanes of NODE that are live outside of the basic-block vectorized
8378 : : region and that can be vectorized using vectorizable_live_operation
8379 : : with STMT_VINFO_LIVE_P. Not handled live operations will cause the
8380 : : scalar code computing it to be retained. */
8381 : :
8382 : : static void
8383 : 981258 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo, slp_tree node,
8384 : : slp_instance instance,
8385 : : stmt_vector_for_cost *cost_vec,
8386 : : hash_map<tree, int> &scalar_use_map,
8387 : : hash_set<stmt_vec_info> &svisited,
8388 : : hash_set<slp_tree> &visited)
8389 : : {
8390 : 981258 : if (visited.add (node))
8391 : 32436 : return;
8392 : :
8393 : 948822 : unsigned i;
8394 : 948822 : stmt_vec_info stmt_info;
8395 : 948822 : stmt_vec_info last_stmt = vect_find_last_scalar_stmt_in_slp (node);
8396 : 3395622 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8397 : : {
8398 : 2446800 : if (!stmt_info || svisited.contains (stmt_info))
8399 : 28028 : continue;
8400 : 2427269 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
8401 : 2427269 : if (STMT_VINFO_IN_PATTERN_P (orig_stmt_info)
8402 : 11329 : && STMT_VINFO_RELATED_STMT (orig_stmt_info) != stmt_info)
8403 : : /* Only the pattern root stmt computes the original scalar value. */
8404 : 8497 : continue;
8405 : 2418772 : bool mark_visited = true;
8406 : 2418772 : gimple *orig_stmt = orig_stmt_info->stmt;
8407 : 2418772 : ssa_op_iter op_iter;
8408 : 2418772 : def_operand_p def_p;
8409 : 5367003 : FOR_EACH_PHI_OR_STMT_DEF (def_p, orig_stmt, op_iter, SSA_OP_DEF)
8410 : : {
8411 : 529459 : if (vec_slp_has_scalar_use (bb_vinfo, DEF_FROM_PTR (def_p),
8412 : : scalar_use_map))
8413 : : {
8414 : 99925 : STMT_VINFO_LIVE_P (stmt_info) = true;
8415 : 99925 : if (vectorizable_live_operation (bb_vinfo, stmt_info, node,
8416 : : instance, i, false, cost_vec))
8417 : : /* ??? So we know we can vectorize the live stmt from one SLP
8418 : : node. If we cannot do so from all or none consistently
8419 : : we'd have to record which SLP node (and lane) we want to
8420 : : use for the live operation. So make sure we can
8421 : : code-generate from all nodes. */
8422 : : mark_visited = false;
8423 : : else
8424 : 0 : STMT_VINFO_LIVE_P (stmt_info) = false;
8425 : : }
8426 : :
8427 : : /* We have to verify whether we can insert the lane extract
8428 : : before all uses. The following is a conservative approximation.
8429 : : We cannot put this into vectorizable_live_operation because
8430 : : iterating over all use stmts from inside a FOR_EACH_IMM_USE_STMT
8431 : : doesn't work.
8432 : : Note that while the fact that we emit code for loads at the
8433 : : first load should make this a non-problem leafs we construct
8434 : : from scalars are vectorized after the last scalar def.
8435 : : ??? If we'd actually compute the insert location during
8436 : : analysis we could use sth less conservative than the last
8437 : : scalar stmt in the node for the dominance check. */
8438 : : /* ??? What remains is "live" uses in vector CTORs in the same
8439 : : SLP graph which is where those uses can end up code-generated
8440 : : right after their definition instead of close to their original
8441 : : use. But that would restrict us to code-generate lane-extracts
8442 : : from the latest stmt in a node. So we compensate for this
8443 : : during code-generation, simply not replacing uses for those
8444 : : hopefully rare cases. */
8445 : 529459 : imm_use_iterator use_iter;
8446 : 529459 : gimple *use_stmt;
8447 : 529459 : stmt_vec_info use_stmt_info;
8448 : :
8449 : 529459 : if (STMT_VINFO_LIVE_P (stmt_info))
8450 : 568158 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
8451 : 468233 : if (!is_gimple_debug (use_stmt)
8452 : 351911 : && (!(use_stmt_info = bb_vinfo->lookup_stmt (use_stmt))
8453 : 339731 : || !PURE_SLP_STMT (vect_stmt_to_vectorize (use_stmt_info)))
8454 : 666560 : && !vect_stmt_dominates_stmt_p (last_stmt->stmt, use_stmt))
8455 : : {
8456 : 19154 : if (dump_enabled_p ())
8457 : 282 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8458 : : "Cannot determine insertion place for "
8459 : : "lane extract\n");
8460 : 19154 : STMT_VINFO_LIVE_P (stmt_info) = false;
8461 : 19154 : mark_visited = true;
8462 : 99925 : }
8463 : : }
8464 : 2418772 : if (mark_visited)
8465 : 2334921 : svisited.add (stmt_info);
8466 : : }
8467 : :
8468 : : slp_tree child;
8469 : 2741779 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8470 : 950788 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
8471 : 231271 : vect_bb_slp_mark_live_stmts (bb_vinfo, child, instance, cost_vec,
8472 : : scalar_use_map, svisited, visited);
8473 : : }
8474 : :
8475 : : /* Traverse all slp instances of BB_VINFO, and mark lanes of every node that
8476 : : are live outside of the basic-block vectorized region and that can be
8477 : : vectorized using vectorizable_live_operation with STMT_VINFO_LIVE_P. */
8478 : :
8479 : : static void
8480 : 342318 : vect_bb_slp_mark_live_stmts (bb_vec_info bb_vinfo)
8481 : : {
8482 : 342318 : if (bb_vinfo->slp_instances.is_empty ())
8483 : 37378 : return;
8484 : :
8485 : 304940 : hash_set<stmt_vec_info> svisited;
8486 : 304940 : hash_set<slp_tree> visited;
8487 : 304940 : hash_map<tree, int> scalar_use_map;
8488 : 304940 : auto_vec<slp_tree> worklist;
8489 : :
8490 : 1664807 : for (slp_instance instance : bb_vinfo->slp_instances)
8491 : : {
8492 : 749987 : if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc)
8493 : 55754 : for (tree op : SLP_INSTANCE_REMAIN_DEFS (instance))
8494 : 16212 : if (TREE_CODE (op) == SSA_NAME)
8495 : 13703 : scalar_use_map.put (op, 1);
8496 : 749987 : if (!visited.add (SLP_INSTANCE_TREE (instance)))
8497 : 748125 : worklist.safe_push (SLP_INSTANCE_TREE (instance));
8498 : : }
8499 : :
8500 : 1667155 : do
8501 : : {
8502 : 1667155 : slp_tree node = worklist.pop ();
8503 : :
8504 : 1667155 : if (SLP_TREE_DEF_TYPE (node) == vect_external_def)
8505 : : {
8506 : 1915384 : for (tree op : SLP_TREE_SCALAR_OPS (node))
8507 : 841157 : if (TREE_CODE (op) == SSA_NAME)
8508 : 575831 : scalar_use_map.put (op, 1);
8509 : : }
8510 : : else
8511 : : {
8512 : 3943700 : for (slp_tree child : SLP_TREE_CHILDREN (node))
8513 : 950764 : if (child && !visited.add (child))
8514 : 919030 : worklist.safe_push (child);
8515 : : }
8516 : : }
8517 : 3334310 : while (!worklist.is_empty ());
8518 : :
8519 : 304940 : visited.empty ();
8520 : :
8521 : 1664807 : for (slp_instance instance : bb_vinfo->slp_instances)
8522 : : {
8523 : 749987 : vect_location = instance->location ();
8524 : 749987 : vect_bb_slp_mark_live_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance),
8525 : : instance, &instance->cost_vec,
8526 : : scalar_use_map, svisited, visited);
8527 : : }
8528 : 304940 : }
8529 : :
8530 : : /* Determine whether we can vectorize the reduction epilogue for INSTANCE. */
8531 : :
8532 : : static bool
8533 : 77158 : vectorizable_bb_reduc_epilogue (slp_instance instance,
8534 : : stmt_vector_for_cost *cost_vec)
8535 : : {
8536 : 77158 : gassign *stmt = as_a <gassign *> (instance->root_stmts[0]->stmt);
8537 : 77158 : enum tree_code reduc_code = gimple_assign_rhs_code (stmt);
8538 : 77158 : if (reduc_code == MINUS_EXPR)
8539 : 0 : reduc_code = PLUS_EXPR;
8540 : 77158 : internal_fn reduc_fn;
8541 : 77158 : tree vectype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (instance));
8542 : 77158 : if (!vectype
8543 : 77059 : || !reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
8544 : 77059 : || reduc_fn == IFN_LAST
8545 : 77059 : || !direct_internal_fn_supported_p (reduc_fn, vectype, OPTIMIZE_FOR_BOTH)
8546 : 112005 : || !useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (stmt)),
8547 : 34847 : TREE_TYPE (vectype)))
8548 : : {
8549 : 53922 : if (dump_enabled_p ())
8550 : 254 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8551 : : "not vectorized: basic block reduction epilogue "
8552 : : "operation unsupported.\n");
8553 : 53922 : return false;
8554 : : }
8555 : :
8556 : : /* There's no way to cost a horizontal vector reduction via REDUC_FN so
8557 : : cost log2 vector operations plus shuffles and one extraction. */
8558 : 23236 : unsigned steps = floor_log2 (vect_nunits_for_cost (vectype));
8559 : 23236 : record_stmt_cost (cost_vec, steps, vector_stmt, instance->root_stmts[0],
8560 : : vectype, 0, vect_body);
8561 : 23236 : record_stmt_cost (cost_vec, steps, vec_perm, instance->root_stmts[0],
8562 : : vectype, 0, vect_body);
8563 : 23236 : record_stmt_cost (cost_vec, 1, vec_to_scalar, instance->root_stmts[0],
8564 : : vectype, 0, vect_body);
8565 : :
8566 : : /* Since we replace all stmts of a possibly longer scalar reduction
8567 : : chain account for the extra scalar stmts for that. */
8568 : 23236 : record_stmt_cost (cost_vec, instance->remain_defs.length (), scalar_stmt,
8569 : 23236 : instance->root_stmts[0], 0, vect_body);
8570 : 23236 : return true;
8571 : : }
8572 : :
8573 : : /* Prune from ROOTS all stmts that are computed as part of lanes of NODE
8574 : : and recurse to children. */
8575 : :
8576 : : static void
8577 : 179295 : vect_slp_prune_covered_roots (slp_tree node, hash_set<stmt_vec_info> &roots,
8578 : : hash_set<slp_tree> &visited)
8579 : : {
8580 : 179295 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def
8581 : 179295 : || visited.add (node))
8582 : 80407 : return;
8583 : :
8584 : : stmt_vec_info stmt;
8585 : : unsigned i;
8586 : 328788 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
8587 : 229900 : if (stmt)
8588 : 233485 : roots.remove (vect_orig_stmt (stmt));
8589 : :
8590 : : slp_tree child;
8591 : 226012 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8592 : 127124 : if (child)
8593 : 120422 : vect_slp_prune_covered_roots (child, roots, visited);
8594 : : }
8595 : :
8596 : : /* Analyze statements in SLP instances of VINFO. Return true if the
8597 : : operations are supported. */
8598 : :
8599 : : bool
8600 : 604421 : vect_slp_analyze_operations (vec_info *vinfo)
8601 : : {
8602 : 604421 : slp_instance instance;
8603 : 604421 : int i;
8604 : :
8605 : 604421 : DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
8606 : :
8607 : 604421 : hash_set<slp_tree> visited;
8608 : 1667074 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
8609 : : {
8610 : 1220192 : auto_vec<slp_tree> visited_vec;
8611 : 1220192 : stmt_vector_for_cost cost_vec;
8612 : 1220192 : cost_vec.create (2);
8613 : 1220192 : if (is_a <bb_vec_info> (vinfo))
8614 : 865389 : vect_location = instance->location ();
8615 : 1220192 : if (!vect_slp_analyze_node_operations (vinfo,
8616 : : SLP_INSTANCE_TREE (instance),
8617 : : instance, visited, visited_vec,
8618 : : &cost_vec)
8619 : : /* CTOR instances require vectorized defs for the SLP tree root. */
8620 : 1047495 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_ctor
8621 : 4566 : && (SLP_TREE_DEF_TYPE (SLP_INSTANCE_TREE (instance))
8622 : : != vect_internal_def
8623 : : /* Make sure we vectorized with the expected type. */
8624 : 4562 : || !useless_type_conversion_p
8625 : 4562 : (TREE_TYPE (TREE_TYPE (gimple_assign_rhs1
8626 : : (instance->root_stmts[0]->stmt))),
8627 : 4562 : TREE_TYPE (SLP_TREE_VECTYPE
8628 : : (SLP_INSTANCE_TREE (instance))))))
8629 : : /* Check we can vectorize the reduction. */
8630 : 1047488 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
8631 : 77158 : && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
8632 : : /* Check we can vectorize the gcond. */
8633 : 2213758 : || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
8634 : 47027 : && !vectorizable_early_exit (vinfo,
8635 : 47027 : SLP_INSTANCE_ROOT_STMTS (instance)[0],
8636 : : NULL, NULL,
8637 : : SLP_INSTANCE_TREE (instance),
8638 : : &cost_vec)))
8639 : : {
8640 : 271479 : cost_vec.release ();
8641 : 271479 : slp_tree node = SLP_INSTANCE_TREE (instance);
8642 : 271479 : stmt_vec_info stmt_info;
8643 : 271479 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
8644 : 216578 : stmt_info = SLP_INSTANCE_ROOT_STMTS (instance)[0];
8645 : : else
8646 : 54901 : stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
8647 : 271479 : if (is_a <loop_vec_info> (vinfo))
8648 : : {
8649 : 157539 : if (dump_enabled_p ())
8650 : 5481 : dump_printf_loc (MSG_NOTE, vect_location,
8651 : : "unsupported SLP instance starting from: %G",
8652 : : stmt_info->stmt);
8653 : 157539 : return false;
8654 : : }
8655 : 113940 : if (dump_enabled_p ())
8656 : 321 : dump_printf_loc (MSG_NOTE, vect_location,
8657 : : "removing SLP instance operations starting from: %G",
8658 : : stmt_info->stmt);
8659 : 113940 : vect_free_slp_instance (instance);
8660 : 113940 : vinfo->slp_instances.ordered_remove (i);
8661 : 1641624 : while (!visited_vec.is_empty ())
8662 : 351236 : visited.remove (visited_vec.pop ());
8663 : : }
8664 : : else
8665 : : {
8666 : 948713 : i++;
8667 : 948713 : if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo))
8668 : : {
8669 : 197264 : add_stmt_costs (loop_vinfo->vector_costs, &cost_vec);
8670 : 197264 : cost_vec.release ();
8671 : : }
8672 : : else
8673 : : /* For BB vectorization remember the SLP graph entry
8674 : : cost for later. */
8675 : 751449 : instance->cost_vec = cost_vec;
8676 : : }
8677 : 1220192 : }
8678 : :
8679 : : /* Now look for SLP instances with a root that are covered by other
8680 : : instances and remove them. */
8681 : 446882 : hash_set<stmt_vec_info> roots;
8682 : 1792673 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
8683 : 928674 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
8684 : 29765 : roots.add (SLP_INSTANCE_ROOT_STMTS (instance)[0]);
8685 : 446882 : if (!roots.is_empty ())
8686 : : {
8687 : 11960 : visited.empty ();
8688 : 70833 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); ++i)
8689 : 58873 : vect_slp_prune_covered_roots (SLP_INSTANCE_TREE (instance), roots,
8690 : : visited);
8691 : 70833 : for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
8692 : 58873 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
8693 : 29765 : && !roots.contains (SLP_INSTANCE_ROOT_STMTS (instance)[0]))
8694 : : {
8695 : 1462 : stmt_vec_info root = SLP_INSTANCE_ROOT_STMTS (instance)[0];
8696 : 1462 : if (dump_enabled_p ())
8697 : 20 : dump_printf_loc (MSG_NOTE, vect_location,
8698 : : "removing SLP instance operations starting "
8699 : : "from: %G", root->stmt);
8700 : 1462 : vect_free_slp_instance (instance);
8701 : 1462 : vinfo->slp_instances.ordered_remove (i);
8702 : : }
8703 : : else
8704 : 57411 : ++i;
8705 : : }
8706 : :
8707 : : /* Compute vectorizable live stmts. */
8708 : 446882 : if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo))
8709 : 342318 : vect_bb_slp_mark_live_stmts (bb_vinfo);
8710 : :
8711 : 893764 : return !vinfo->slp_instances.is_empty ();
8712 : 1051303 : }
8713 : :
8714 : : /* Get the SLP instance leader from INSTANCE_LEADER thereby transitively
8715 : : closing the eventual chain. */
8716 : :
8717 : : static slp_instance
8718 : 805609 : get_ultimate_leader (slp_instance instance,
8719 : : hash_map<slp_instance, slp_instance> &instance_leader)
8720 : : {
8721 : 805609 : auto_vec<slp_instance *, 8> chain;
8722 : 805609 : slp_instance *tem;
8723 : 871488 : while (*(tem = instance_leader.get (instance)) != instance)
8724 : : {
8725 : 65879 : chain.safe_push (tem);
8726 : 65879 : instance = *tem;
8727 : : }
8728 : 871488 : while (!chain.is_empty ())
8729 : 65879 : *chain.pop () = instance;
8730 : 805609 : return instance;
8731 : 805609 : }
8732 : :
8733 : : namespace {
8734 : : /* Subroutine of vect_bb_partition_graph_r. Map KEY to INSTANCE in
8735 : : KEY_TO_INSTANCE, making INSTANCE the leader of any previous mapping
8736 : : for KEY. Return true if KEY was already in KEY_TO_INSTANCE.
8737 : :
8738 : : INSTANCE_LEADER is as for get_ultimate_leader. */
8739 : :
8740 : : template<typename T>
8741 : : bool
8742 : 3507434 : vect_map_to_instance (slp_instance instance, T key,
8743 : : hash_map<T, slp_instance> &key_to_instance,
8744 : : hash_map<slp_instance, slp_instance> &instance_leader)
8745 : : {
8746 : : bool existed_p;
8747 : 3507434 : slp_instance &key_instance = key_to_instance.get_or_insert (key, &existed_p);
8748 : 3507434 : if (!existed_p)
8749 : : ;
8750 : 139988 : else if (key_instance != instance)
8751 : : {
8752 : : /* If we're running into a previously marked key make us the
8753 : : leader of the current ultimate leader. This keeps the
8754 : : leader chain acyclic and works even when the current instance
8755 : : connects two previously independent graph parts. */
8756 : 55622 : slp_instance key_leader
8757 : 55622 : = get_ultimate_leader (key_instance, instance_leader);
8758 : 55622 : if (key_leader != instance)
8759 : 16972 : instance_leader.put (key_leader, instance);
8760 : : }
8761 : 3507434 : key_instance = instance;
8762 : 3507434 : return existed_p;
8763 : : }
8764 : : }
8765 : :
8766 : : /* Worker of vect_bb_partition_graph, recurse on NODE. */
8767 : :
8768 : : static void
8769 : 981258 : vect_bb_partition_graph_r (bb_vec_info bb_vinfo,
8770 : : slp_instance instance, slp_tree node,
8771 : : hash_map<stmt_vec_info, slp_instance> &stmt_to_instance,
8772 : : hash_map<slp_tree, slp_instance> &node_to_instance,
8773 : : hash_map<slp_instance, slp_instance> &instance_leader)
8774 : : {
8775 : 981258 : stmt_vec_info stmt_info;
8776 : 981258 : unsigned i;
8777 : :
8778 : 3507434 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8779 : 2526176 : if (stmt_info)
8780 : 2526176 : vect_map_to_instance (instance, stmt_info, stmt_to_instance,
8781 : : instance_leader);
8782 : :
8783 : 981258 : if (vect_map_to_instance (instance, node, node_to_instance,
8784 : : instance_leader))
8785 : 981258 : return;
8786 : :
8787 : : slp_tree child;
8788 : 1899610 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8789 : 950788 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
8790 : 231271 : vect_bb_partition_graph_r (bb_vinfo, instance, child, stmt_to_instance,
8791 : : node_to_instance, instance_leader);
8792 : : }
8793 : :
8794 : : /* Partition the SLP graph into pieces that can be costed independently. */
8795 : :
8796 : : static void
8797 : 304940 : vect_bb_partition_graph (bb_vec_info bb_vinfo)
8798 : : {
8799 : 304940 : DUMP_VECT_SCOPE ("vect_bb_partition_graph");
8800 : :
8801 : : /* First walk the SLP graph assigning each involved scalar stmt a
8802 : : corresponding SLP graph entry and upon visiting a previously
8803 : : marked stmt, make the stmts leader the current SLP graph entry. */
8804 : 304940 : hash_map<stmt_vec_info, slp_instance> stmt_to_instance;
8805 : 304940 : hash_map<slp_tree, slp_instance> node_to_instance;
8806 : 304940 : hash_map<slp_instance, slp_instance> instance_leader;
8807 : 304940 : slp_instance instance;
8808 : 1054927 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
8809 : : {
8810 : 749987 : instance_leader.put (instance, instance);
8811 : 749987 : vect_bb_partition_graph_r (bb_vinfo,
8812 : : instance, SLP_INSTANCE_TREE (instance),
8813 : : stmt_to_instance, node_to_instance,
8814 : : instance_leader);
8815 : : }
8816 : :
8817 : : /* Then collect entries to each independent subgraph. */
8818 : 1359867 : for (unsigned i = 0; bb_vinfo->slp_instances.iterate (i, &instance); ++i)
8819 : : {
8820 : 749987 : slp_instance leader = get_ultimate_leader (instance, instance_leader);
8821 : 749987 : leader->subgraph_entries.safe_push (instance);
8822 : 749987 : if (dump_enabled_p ()
8823 : 749987 : && leader != instance)
8824 : 67 : dump_printf_loc (MSG_NOTE, vect_location,
8825 : : "instance %p is leader of %p\n",
8826 : : (void *) leader, (void *) instance);
8827 : : }
8828 : 304940 : }
8829 : :
8830 : : /* Compute the set of scalar stmts participating in internal and external
8831 : : nodes. */
8832 : :
8833 : : static void
8834 : 1687237 : vect_slp_gather_vectorized_scalar_stmts (vec_info *vinfo, slp_tree node,
8835 : : hash_set<slp_tree> &visited,
8836 : : hash_set<stmt_vec_info> &vstmts,
8837 : : hash_set<stmt_vec_info> &estmts)
8838 : : {
8839 : 1687237 : int i;
8840 : 1687237 : stmt_vec_info stmt_info;
8841 : 1687237 : slp_tree child;
8842 : :
8843 : 1687237 : if (visited.add (node))
8844 : 32371 : return;
8845 : :
8846 : 1654866 : if (SLP_TREE_DEF_TYPE (node) == vect_internal_def)
8847 : : {
8848 : 3337793 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8849 : 2397824 : if (stmt_info)
8850 : 2397824 : vstmts.add (stmt_info);
8851 : :
8852 : 3430870 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8853 : 940902 : if (child)
8854 : 940902 : vect_slp_gather_vectorized_scalar_stmts (vinfo, child, visited,
8855 : : vstmts, estmts);
8856 : : }
8857 : : else
8858 : 3976096 : for (tree def : SLP_TREE_SCALAR_OPS (node))
8859 : : {
8860 : 1832433 : stmt_vec_info def_stmt = vinfo->lookup_def (def);
8861 : 1832433 : if (def_stmt)
8862 : 368996 : estmts.add (def_stmt);
8863 : : }
8864 : : }
8865 : :
8866 : :
8867 : : /* Compute the scalar cost of the SLP node NODE and its children
8868 : : and return it. Do not account defs that are marked in LIFE and
8869 : : update LIFE according to uses of NODE. */
8870 : :
8871 : : static void
8872 : 971619 : vect_bb_slp_scalar_cost (vec_info *vinfo,
8873 : : slp_tree node, vec<bool, va_heap> *life,
8874 : : stmt_vector_for_cost *cost_vec,
8875 : : hash_set<stmt_vec_info> &vectorized_scalar_stmts,
8876 : : hash_set<stmt_vec_info> &scalar_stmts_in_externs,
8877 : : hash_set<slp_tree> &visited)
8878 : : {
8879 : 971619 : unsigned i;
8880 : 971619 : stmt_vec_info stmt_info;
8881 : 971619 : slp_tree child;
8882 : :
8883 : 971619 : if (visited.add (node))
8884 : 31633 : return;
8885 : :
8886 : 3337844 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
8887 : : {
8888 : 2397858 : ssa_op_iter op_iter;
8889 : 2397858 : def_operand_p def_p;
8890 : :
8891 : 2428767 : if (!stmt_info
8892 : 2397858 : || (*life)[i]
8893 : : /* Defs also used in external nodes are not in the
8894 : : vectorized_scalar_stmts set as they need to be preserved.
8895 : : Honor that. */
8896 : 4768285 : || scalar_stmts_in_externs.contains (stmt_info))
8897 : 109665 : continue;
8898 : :
8899 : 2366949 : stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
8900 : 2366949 : gimple *orig_stmt = orig_stmt_info->stmt;
8901 : :
8902 : : /* If there is a non-vectorized use of the defs then the scalar
8903 : : stmt is kept live in which case we do not account it or any
8904 : : required defs in the SLP children in the scalar cost. This
8905 : : way we make the vectorization more costly when compared to
8906 : : the scalar cost. */
8907 : 2366949 : if (!STMT_VINFO_LIVE_P (stmt_info))
8908 : : {
8909 : 2293342 : auto_vec<gimple *, 8> worklist;
8910 : 2293342 : hash_set<gimple *> *worklist_visited = NULL;
8911 : 2293342 : worklist.quick_push (orig_stmt);
8912 : 2298191 : do
8913 : : {
8914 : 2298191 : gimple *work_stmt = worklist.pop ();
8915 : 5004953 : FOR_EACH_PHI_OR_STMT_DEF (def_p, work_stmt, op_iter, SSA_OP_DEF)
8916 : : {
8917 : 430630 : imm_use_iterator use_iter;
8918 : 430630 : gimple *use_stmt;
8919 : 1046311 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter,
8920 : : DEF_FROM_PTR (def_p))
8921 : 637740 : if (!is_gimple_debug (use_stmt))
8922 : : {
8923 : 488174 : stmt_vec_info use_stmt_info
8924 : 488174 : = vinfo->lookup_stmt (use_stmt);
8925 : 488174 : if (!use_stmt_info
8926 : 488174 : || !vectorized_scalar_stmts.contains (use_stmt_info))
8927 : : {
8928 : 26988 : if (use_stmt_info
8929 : 23771 : && STMT_VINFO_IN_PATTERN_P (use_stmt_info))
8930 : : {
8931 : : /* For stmts participating in patterns we have
8932 : : to check its uses recursively. */
8933 : 4929 : if (!worklist_visited)
8934 : 3817 : worklist_visited = new hash_set<gimple *> ();
8935 : 4929 : if (!worklist_visited->add (use_stmt))
8936 : 4929 : worklist.safe_push (use_stmt);
8937 : 4929 : continue;
8938 : : }
8939 : 22059 : (*life)[i] = true;
8940 : 22059 : goto next_lane;
8941 : : }
8942 : 430630 : }
8943 : : }
8944 : : }
8945 : 4552264 : while (!worklist.is_empty ());
8946 : 2271283 : next_lane:
8947 : 2293342 : if (worklist_visited)
8948 : 3817 : delete worklist_visited;
8949 : 2293342 : if ((*life)[i])
8950 : 22059 : continue;
8951 : 2293342 : }
8952 : :
8953 : : /* Count scalar stmts only once. */
8954 : 2344890 : if (gimple_visited_p (orig_stmt))
8955 : 24877 : continue;
8956 : 2320013 : gimple_set_visited (orig_stmt, true);
8957 : :
8958 : 2320013 : vect_cost_for_stmt kind;
8959 : 2320013 : if (STMT_VINFO_DATA_REF (orig_stmt_info))
8960 : : {
8961 : 2111840 : data_reference_p dr = STMT_VINFO_DATA_REF (orig_stmt_info);
8962 : 2111840 : tree base = get_base_address (DR_REF (dr));
8963 : : /* When the scalar access is to a non-global not address-taken
8964 : : decl that is not BLKmode assume we can access it with a single
8965 : : non-load/store instruction. */
8966 : 2111840 : if (DECL_P (base)
8967 : 1663982 : && !is_global_var (base)
8968 : 1594193 : && !TREE_ADDRESSABLE (base)
8969 : 2764307 : && DECL_MODE (base) != BLKmode)
8970 : : kind = scalar_stmt;
8971 : 1900698 : else if (DR_IS_READ (STMT_VINFO_DATA_REF (orig_stmt_info)))
8972 : : kind = scalar_load;
8973 : : else
8974 : 1677097 : kind = scalar_store;
8975 : : }
8976 : 208173 : else if (vect_nop_conversion_p (orig_stmt_info))
8977 : 20986 : continue;
8978 : : /* For single-argument PHIs assume coalescing which means zero cost
8979 : : for the scalar and the vector PHIs. This avoids artificially
8980 : : favoring the vector path (but may pessimize it in some cases). */
8981 : 187187 : else if (is_a <gphi *> (orig_stmt_info->stmt)
8982 : 187187 : && gimple_phi_num_args
8983 : 96752 : (as_a <gphi *> (orig_stmt_info->stmt)) == 1)
8984 : 10834 : continue;
8985 : : else
8986 : : kind = scalar_stmt;
8987 : 2288193 : record_stmt_cost (cost_vec, 1, kind, orig_stmt_info,
8988 : : SLP_TREE_VECTYPE (node), 0, vect_body);
8989 : : }
8990 : :
8991 : 1879972 : auto_vec<bool, 20> subtree_life;
8992 : 2716023 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
8993 : : {
8994 : 940926 : if (child && SLP_TREE_DEF_TYPE (child) == vect_internal_def)
8995 : : {
8996 : : /* Do not directly pass LIFE to the recursive call, copy it to
8997 : : confine changes in the callee to the current child/subtree. */
8998 : 225284 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
8999 : : {
9000 : 3401 : subtree_life.safe_grow_cleared (SLP_TREE_LANES (child), true);
9001 : 12013 : for (unsigned j = 0;
9002 : 12013 : j < SLP_TREE_LANE_PERMUTATION (node).length (); ++j)
9003 : : {
9004 : 8612 : auto perm = SLP_TREE_LANE_PERMUTATION (node)[j];
9005 : 8612 : if (perm.first == i)
9006 : 4562 : subtree_life[perm.second] = (*life)[j];
9007 : : }
9008 : : }
9009 : : else
9010 : : {
9011 : 221883 : gcc_assert (SLP_TREE_LANES (node) == SLP_TREE_LANES (child));
9012 : 221883 : subtree_life.safe_splice (*life);
9013 : : }
9014 : 225284 : vect_bb_slp_scalar_cost (vinfo, child, &subtree_life, cost_vec,
9015 : : vectorized_scalar_stmts,
9016 : : scalar_stmts_in_externs, visited);
9017 : 225284 : subtree_life.truncate (0);
9018 : : }
9019 : : }
9020 : : }
9021 : :
9022 : : /* Comparator for the loop-index sorted cost vectors. */
9023 : :
9024 : : static int
9025 : 19302222 : li_cost_vec_cmp (const void *a_, const void *b_)
9026 : : {
9027 : 19302222 : auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_;
9028 : 19302222 : auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_;
9029 : 19302222 : if (a->first < b->first)
9030 : : return -1;
9031 : 18505850 : else if (a->first == b->first)
9032 : 17805485 : return 0;
9033 : : return 1;
9034 : : }
9035 : :
9036 : : /* Check if vectorization of the basic block is profitable for the
9037 : : subgraph denoted by SLP_INSTANCES. */
9038 : :
9039 : : static bool
9040 : 729496 : vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo,
9041 : : vec<slp_instance> slp_instances,
9042 : : loop_p orig_loop)
9043 : : {
9044 : 729496 : slp_instance instance;
9045 : 729496 : int i;
9046 : 729496 : unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
9047 : 729496 : unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
9048 : :
9049 : 729496 : if (dump_enabled_p ())
9050 : : {
9051 : 99 : dump_printf_loc (MSG_NOTE, vect_location, "Costing subgraph: \n");
9052 : 99 : hash_set<slp_tree> visited;
9053 : 399 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9054 : 102 : vect_print_slp_graph (MSG_NOTE, vect_location,
9055 : : SLP_INSTANCE_TREE (instance), visited);
9056 : 99 : }
9057 : :
9058 : : /* Compute the set of scalar stmts we know will go away 'locally' when
9059 : : vectorizing. This used to be tracked with just PURE_SLP_STMT but that's
9060 : : not accurate for nodes promoted extern late or for scalar stmts that
9061 : : are used both in extern defs and in vectorized defs. */
9062 : 729496 : hash_set<stmt_vec_info> vectorized_scalar_stmts;
9063 : 729496 : hash_set<stmt_vec_info> scalar_stmts_in_externs;
9064 : 729496 : hash_set<slp_tree> visited;
9065 : 1475831 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9066 : : {
9067 : 746335 : vect_slp_gather_vectorized_scalar_stmts (bb_vinfo,
9068 : : SLP_INSTANCE_TREE (instance),
9069 : : visited,
9070 : : vectorized_scalar_stmts,
9071 : : scalar_stmts_in_externs);
9072 : 847243 : for (stmt_vec_info rstmt : SLP_INSTANCE_ROOT_STMTS (instance))
9073 : 48724 : vectorized_scalar_stmts.add (rstmt);
9074 : : }
9075 : : /* Scalar stmts used as defs in external nodes need to be preseved, so
9076 : : remove them from vectorized_scalar_stmts. */
9077 : 1060651 : for (stmt_vec_info stmt : scalar_stmts_in_externs)
9078 : 331155 : vectorized_scalar_stmts.remove (stmt);
9079 : :
9080 : : /* Calculate scalar cost and sum the cost for the vector stmts
9081 : : previously collected. */
9082 : 729496 : stmt_vector_for_cost scalar_costs = vNULL;
9083 : 729496 : stmt_vector_for_cost vector_costs = vNULL;
9084 : 729496 : visited.empty ();
9085 : 1475831 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
9086 : : {
9087 : 746335 : auto_vec<bool, 20> life;
9088 : 746335 : life.safe_grow_cleared (SLP_TREE_LANES (SLP_INSTANCE_TREE (instance)),
9089 : : true);
9090 : 746335 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
9091 : 52184 : record_stmt_cost (&scalar_costs,
9092 : 26092 : SLP_INSTANCE_ROOT_STMTS (instance).length (),
9093 : : scalar_stmt,
9094 : 26092 : SLP_INSTANCE_ROOT_STMTS (instance)[0], 0, vect_body);
9095 : 746335 : vect_bb_slp_scalar_cost (bb_vinfo,
9096 : : SLP_INSTANCE_TREE (instance),
9097 : : &life, &scalar_costs, vectorized_scalar_stmts,
9098 : : scalar_stmts_in_externs, visited);
9099 : 746335 : vector_costs.safe_splice (instance->cost_vec);
9100 : 746335 : instance->cost_vec.release ();
9101 : 746335 : }
9102 : :
9103 : 729496 : if (dump_enabled_p ())
9104 : 99 : dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
9105 : :
9106 : : /* When costing non-loop vectorization we need to consider each covered
9107 : : loop independently and make sure vectorization is profitable. For
9108 : : now we assume a loop may be not entered or executed an arbitrary
9109 : : number of iterations (??? static information can provide more
9110 : : precise info here) which means we can simply cost each containing
9111 : : loops stmts separately. */
9112 : :
9113 : : /* First produce cost vectors sorted by loop index. */
9114 : 729496 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9115 : 729496 : li_scalar_costs (scalar_costs.length ());
9116 : 729496 : auto_vec<std::pair<unsigned, stmt_info_for_cost *> >
9117 : 729496 : li_vector_costs (vector_costs.length ());
9118 : 729496 : stmt_info_for_cost *cost;
9119 : 3043781 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9120 : : {
9121 : 2314285 : unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9122 : 2314285 : li_scalar_costs.quick_push (std::make_pair (l, cost));
9123 : : }
9124 : : /* Use a random used loop as fallback in case the first vector_costs
9125 : : entry does not have a stmt_info associated with it. */
9126 : 729496 : unsigned l = li_scalar_costs[0].first;
9127 : 2740565 : FOR_EACH_VEC_ELT (vector_costs, i, cost)
9128 : : {
9129 : : /* We inherit from the previous COST, invariants, externals and
9130 : : extracts immediately follow the cost for the related stmt. */
9131 : 2011069 : if (cost->stmt_info)
9132 : 1152005 : l = gimple_bb (cost->stmt_info->stmt)->loop_father->num;
9133 : 2011069 : li_vector_costs.quick_push (std::make_pair (l, cost));
9134 : : }
9135 : 729496 : li_scalar_costs.qsort (li_cost_vec_cmp);
9136 : 729496 : li_vector_costs.qsort (li_cost_vec_cmp);
9137 : :
9138 : : /* Now cost the portions individually. */
9139 : : unsigned vi = 0;
9140 : : unsigned si = 0;
9141 : 1214255 : bool profitable = true;
9142 : 1214255 : while (si < li_scalar_costs.length ()
9143 : 1948503 : && vi < li_vector_costs.length ())
9144 : : {
9145 : 734248 : unsigned sl = li_scalar_costs[si].first;
9146 : 734248 : unsigned vl = li_vector_costs[vi].first;
9147 : 734248 : if (sl != vl)
9148 : : {
9149 : 1062 : if (dump_enabled_p ())
9150 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9151 : : "Scalar %d and vector %d loop part do not "
9152 : : "match up, skipping scalar part\n", sl, vl);
9153 : : /* Skip the scalar part, assuming zero cost on the vector side. */
9154 : 2359 : do
9155 : : {
9156 : 2359 : si++;
9157 : : }
9158 : 2359 : while (si < li_scalar_costs.length ()
9159 : 4135 : && li_scalar_costs[si].first == sl);
9160 : 1062 : continue;
9161 : : }
9162 : :
9163 : 733186 : class vector_costs *scalar_target_cost_data = init_cost (bb_vinfo, true);
9164 : 2290909 : do
9165 : : {
9166 : 2290909 : add_stmt_cost (scalar_target_cost_data, li_scalar_costs[si].second);
9167 : 2290909 : si++;
9168 : : }
9169 : 2290909 : while (si < li_scalar_costs.length ()
9170 : 4590141 : && li_scalar_costs[si].first == sl);
9171 : 733186 : scalar_target_cost_data->finish_cost (nullptr);
9172 : 733186 : scalar_cost = scalar_target_cost_data->body_cost ();
9173 : :
9174 : : /* Complete the target-specific vector cost calculation. */
9175 : 733186 : class vector_costs *vect_target_cost_data = init_cost (bb_vinfo, false);
9176 : 1972150 : do
9177 : : {
9178 : 1972150 : add_stmt_cost (vect_target_cost_data, li_vector_costs[vi].second);
9179 : 1972150 : vi++;
9180 : : }
9181 : 1972150 : while (vi < li_vector_costs.length ()
9182 : 3953596 : && li_vector_costs[vi].first == vl);
9183 : 733186 : vect_target_cost_data->finish_cost (scalar_target_cost_data);
9184 : 733186 : vec_prologue_cost = vect_target_cost_data->prologue_cost ();
9185 : 733186 : vec_inside_cost = vect_target_cost_data->body_cost ();
9186 : 733186 : vec_epilogue_cost = vect_target_cost_data->epilogue_cost ();
9187 : 733186 : delete scalar_target_cost_data;
9188 : 733186 : delete vect_target_cost_data;
9189 : :
9190 : 733186 : vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
9191 : :
9192 : 733186 : if (dump_enabled_p ())
9193 : : {
9194 : 99 : dump_printf_loc (MSG_NOTE, vect_location,
9195 : : "Cost model analysis for part in loop %d:\n", sl);
9196 : 99 : dump_printf (MSG_NOTE, " Vector cost: %d\n",
9197 : : vec_inside_cost + vec_outside_cost);
9198 : 99 : dump_printf (MSG_NOTE, " Scalar cost: %d\n", scalar_cost);
9199 : : }
9200 : :
9201 : : /* Vectorization is profitable if its cost is more than the cost of scalar
9202 : : version. Note that we err on the vector side for equal cost because
9203 : : the cost estimate is otherwise quite pessimistic (constant uses are
9204 : : free on the scalar side but cost a load on the vector side for
9205 : : example). */
9206 : 733186 : if (vec_outside_cost + vec_inside_cost > scalar_cost)
9207 : : {
9208 : : profitable = false;
9209 : : break;
9210 : : }
9211 : : }
9212 : 1209503 : if (profitable && vi < li_vector_costs.length ())
9213 : : {
9214 : 1047 : if (dump_enabled_p ())
9215 : 12 : dump_printf_loc (MSG_NOTE, vect_location,
9216 : : "Excess vector cost for part in loop %d:\n",
9217 : 6 : li_vector_costs[vi].first);
9218 : : profitable = false;
9219 : : }
9220 : :
9221 : : /* Unset visited flag. This is delayed when the subgraph is profitable
9222 : : and we process the loop for remaining unvectorized if-converted code. */
9223 : 729496 : if (!orig_loop || !profitable)
9224 : 3042473 : FOR_EACH_VEC_ELT (scalar_costs, i, cost)
9225 : 2313073 : gimple_set_visited (cost->stmt_info->stmt, false);
9226 : :
9227 : 729496 : scalar_costs.release ();
9228 : 729496 : vector_costs.release ();
9229 : :
9230 : 729496 : return profitable;
9231 : 729496 : }
9232 : :
9233 : : /* qsort comparator for lane defs. */
9234 : :
9235 : : static int
9236 : 40 : vld_cmp (const void *a_, const void *b_)
9237 : : {
9238 : 40 : auto *a = (const std::pair<unsigned, tree> *)a_;
9239 : 40 : auto *b = (const std::pair<unsigned, tree> *)b_;
9240 : 40 : return a->first - b->first;
9241 : : }
9242 : :
9243 : : /* Return true if USE_STMT is a vector lane insert into VEC and set
9244 : : *THIS_LANE to the lane number that is set. */
9245 : :
9246 : : static bool
9247 : 240 : vect_slp_is_lane_insert (gimple *use_stmt, tree vec, unsigned *this_lane)
9248 : : {
9249 : 240 : gassign *use_ass = dyn_cast <gassign *> (use_stmt);
9250 : 91 : if (!use_ass
9251 : 91 : || gimple_assign_rhs_code (use_ass) != BIT_INSERT_EXPR
9252 : 22 : || (vec
9253 : 22 : ? gimple_assign_rhs1 (use_ass) != vec
9254 : 24 : : ((vec = gimple_assign_rhs1 (use_ass)), false))
9255 : 46 : || !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (vec)),
9256 : 46 : TREE_TYPE (gimple_assign_rhs2 (use_ass)))
9257 : 46 : || !constant_multiple_p
9258 : 46 : (tree_to_poly_uint64 (gimple_assign_rhs3 (use_ass)),
9259 : 92 : tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (TREE_TYPE (vec)))),
9260 : : this_lane))
9261 : 194 : return false;
9262 : : return true;
9263 : : }
9264 : :
9265 : : /* Find any vectorizable constructors and add them to the grouped_store
9266 : : array. */
9267 : :
9268 : : static void
9269 : 2389978 : vect_slp_check_for_roots (bb_vec_info bb_vinfo)
9270 : : {
9271 : 18597676 : for (unsigned i = 0; i < bb_vinfo->nbbs; ++i)
9272 : 32415396 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[i]);
9273 : 131557883 : !gsi_end_p (gsi); gsi_next (&gsi))
9274 : : {
9275 : 115350185 : gassign *assign = dyn_cast<gassign *> (gsi_stmt (gsi));
9276 : : /* This can be used to start SLP discovery for early breaks for BB early breaks
9277 : : when we get that far. */
9278 : 115350185 : if (!assign)
9279 : 170358605 : continue;
9280 : :
9281 : 31241769 : tree rhs = gimple_assign_rhs1 (assign);
9282 : 31241769 : enum tree_code code = gimple_assign_rhs_code (assign);
9283 : 31241769 : use_operand_p use_p;
9284 : 31241769 : gimple *use_stmt;
9285 : 31241769 : if (code == CONSTRUCTOR)
9286 : : {
9287 : 1883152 : if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
9288 : 58646 : || maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)),
9289 : 86565 : CONSTRUCTOR_NELTS (rhs))
9290 : 40064 : || VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
9291 : 1923215 : || uniform_vector_p (rhs))
9292 : 1871554 : continue;
9293 : :
9294 : : unsigned j;
9295 : : tree val;
9296 : 57333 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9297 : 45735 : if (TREE_CODE (val) != SSA_NAME
9298 : 45735 : || !bb_vinfo->lookup_def (val))
9299 : : break;
9300 : 27822 : if (j != CONSTRUCTOR_NELTS (rhs))
9301 : 2313 : continue;
9302 : :
9303 : 11598 : vec<stmt_vec_info> roots = vNULL;
9304 : 11598 : roots.safe_push (bb_vinfo->lookup_stmt (assign));
9305 : 11598 : vec<stmt_vec_info> stmts;
9306 : 11598 : stmts.create (CONSTRUCTOR_NELTS (rhs));
9307 : 65222 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), j, val)
9308 : 42026 : stmts.quick_push
9309 : 42026 : (vect_stmt_to_vectorize (bb_vinfo->lookup_def (val)));
9310 : 11598 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
9311 : 11598 : stmts, roots));
9312 : : }
9313 : 29358617 : else if (code == BIT_INSERT_EXPR
9314 : 870 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
9315 : 559 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).is_constant ()
9316 : 559 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)).to_constant () > 1
9317 : 556 : && integer_zerop (gimple_assign_rhs3 (assign))
9318 : 312 : && useless_type_conversion_p
9319 : 312 : (TREE_TYPE (TREE_TYPE (rhs)),
9320 : 312 : TREE_TYPE (gimple_assign_rhs2 (assign)))
9321 : 29359181 : && bb_vinfo->lookup_def (gimple_assign_rhs2 (assign)))
9322 : : {
9323 : : /* We start to match on insert to lane zero but since the
9324 : : inserts need not be ordered we'd have to search both
9325 : : the def and the use chains. */
9326 : 211 : tree vectype = TREE_TYPE (rhs);
9327 : 211 : unsigned nlanes = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
9328 : 211 : auto_vec<std::pair<unsigned, tree> > lane_defs (nlanes);
9329 : 211 : auto_sbitmap lanes (nlanes);
9330 : 211 : bitmap_clear (lanes);
9331 : 211 : bitmap_set_bit (lanes, 0);
9332 : 211 : tree def = gimple_assign_lhs (assign);
9333 : 211 : lane_defs.quick_push
9334 : 211 : (std::make_pair (0, gimple_assign_rhs2 (assign)));
9335 : 211 : unsigned lanes_found = 1;
9336 : : /* Start with the use chains, the last stmt will be the root. */
9337 : 211 : stmt_vec_info last = bb_vinfo->lookup_stmt (assign);
9338 : 211 : vec<stmt_vec_info> roots = vNULL;
9339 : 211 : roots.safe_push (last);
9340 : 213 : do
9341 : : {
9342 : 213 : use_operand_p use_p;
9343 : 213 : gimple *use_stmt;
9344 : 213 : if (!single_imm_use (def, &use_p, &use_stmt))
9345 : : break;
9346 : 207 : unsigned this_lane;
9347 : 207 : if (!bb_vinfo->lookup_stmt (use_stmt)
9348 : 207 : || !vect_slp_is_lane_insert (use_stmt, def, &this_lane)
9349 : 229 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (use_stmt)))
9350 : : break;
9351 : 22 : if (bitmap_bit_p (lanes, this_lane))
9352 : : break;
9353 : 2 : lanes_found++;
9354 : 2 : bitmap_set_bit (lanes, this_lane);
9355 : 2 : gassign *use_ass = as_a <gassign *> (use_stmt);
9356 : 2 : lane_defs.quick_push (std::make_pair
9357 : 2 : (this_lane, gimple_assign_rhs2 (use_ass)));
9358 : 2 : last = bb_vinfo->lookup_stmt (use_ass);
9359 : 2 : roots.safe_push (last);
9360 : 2 : def = gimple_assign_lhs (use_ass);
9361 : : }
9362 : 2 : while (lanes_found < nlanes);
9363 : 211 : if (roots.length () > 1)
9364 : 2 : std::swap(roots[0], roots[roots.length () - 1]);
9365 : 211 : if (lanes_found < nlanes)
9366 : : {
9367 : : /* Now search the def chain. */
9368 : 211 : def = gimple_assign_rhs1 (assign);
9369 : 213 : do
9370 : : {
9371 : 213 : if (TREE_CODE (def) != SSA_NAME
9372 : 213 : || !has_single_use (def))
9373 : : break;
9374 : 56 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
9375 : 56 : unsigned this_lane;
9376 : 56 : if (!bb_vinfo->lookup_stmt (def_stmt)
9377 : 33 : || !vect_slp_is_lane_insert (def_stmt,
9378 : : NULL_TREE, &this_lane)
9379 : 80 : || !bb_vinfo->lookup_def (gimple_assign_rhs2 (def_stmt)))
9380 : : break;
9381 : 24 : if (bitmap_bit_p (lanes, this_lane))
9382 : : break;
9383 : 4 : lanes_found++;
9384 : 4 : bitmap_set_bit (lanes, this_lane);
9385 : 8 : lane_defs.quick_push (std::make_pair
9386 : 4 : (this_lane,
9387 : 4 : gimple_assign_rhs2 (def_stmt)));
9388 : 4 : roots.safe_push (bb_vinfo->lookup_stmt (def_stmt));
9389 : 4 : def = gimple_assign_rhs1 (def_stmt);
9390 : : }
9391 : 4 : while (lanes_found < nlanes);
9392 : : }
9393 : 211 : if (lanes_found == nlanes)
9394 : : {
9395 : : /* Sort lane_defs after the lane index and register the root. */
9396 : 2 : lane_defs.qsort (vld_cmp);
9397 : 2 : vec<stmt_vec_info> stmts;
9398 : 2 : stmts.create (nlanes);
9399 : 10 : for (unsigned i = 0; i < nlanes; ++i)
9400 : 8 : stmts.quick_push (bb_vinfo->lookup_def (lane_defs[i].second));
9401 : 2 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_ctor,
9402 : 2 : stmts, roots));
9403 : : }
9404 : : else
9405 : 209 : roots.release ();
9406 : 211 : }
9407 : 29358406 : else if (!VECTOR_TYPE_P (TREE_TYPE (rhs))
9408 : 28463318 : && (associative_tree_code (code) || code == MINUS_EXPR)
9409 : : /* ??? This pessimizes a two-element reduction. PR54400.
9410 : : ??? In-order reduction could be handled if we only
9411 : : traverse one operand chain in vect_slp_linearize_chain. */
9412 : 33210892 : && !needs_fold_left_reduction_p (TREE_TYPE (rhs), code)
9413 : : /* Ops with constants at the tail can be stripped here. */
9414 : 5701473 : && TREE_CODE (rhs) == SSA_NAME
9415 : 5637187 : && TREE_CODE (gimple_assign_rhs2 (assign)) == SSA_NAME
9416 : : /* Should be the chain end. */
9417 : 31608251 : && (!single_imm_use (gimple_assign_lhs (assign),
9418 : : &use_p, &use_stmt)
9419 : 1738186 : || !is_gimple_assign (use_stmt)
9420 : 1156536 : || (gimple_assign_rhs_code (use_stmt) != code
9421 : 844844 : && ((code != PLUS_EXPR && code != MINUS_EXPR)
9422 : 470362 : || (gimple_assign_rhs_code (use_stmt)
9423 : 470362 : != (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR))))))
9424 : : {
9425 : : /* We start the match at the end of a possible association
9426 : : chain. */
9427 : 1848987 : auto_vec<chain_op_t> chain;
9428 : 1848987 : auto_vec<std::pair<tree_code, gimple *> > worklist;
9429 : 1848987 : auto_vec<gimple *> chain_stmts;
9430 : 1848987 : gimple *code_stmt = NULL, *alt_code_stmt = NULL;
9431 : 1848987 : if (code == MINUS_EXPR)
9432 : 309830 : code = PLUS_EXPR;
9433 : 1848987 : internal_fn reduc_fn;
9434 : 2119206 : if (!reduction_fn_for_scalar_code (code, &reduc_fn)
9435 : 1848987 : || reduc_fn == IFN_LAST)
9436 : 270219 : continue;
9437 : 1578768 : vect_slp_linearize_chain (bb_vinfo, worklist, chain, code, assign,
9438 : : /* ??? */
9439 : : code_stmt, alt_code_stmt, &chain_stmts);
9440 : 3157536 : if (chain.length () > 1)
9441 : : {
9442 : : /* Sort the chain according to def_type and operation. */
9443 : 1578768 : chain.sort (dt_sort_cmp, bb_vinfo);
9444 : : /* ??? Now we'd want to strip externals and constants
9445 : : but record those to be handled in the epilogue. */
9446 : : /* ??? For now do not allow mixing ops or externs/constants. */
9447 : 1578768 : bool invalid = false;
9448 : 1578768 : unsigned remain_cnt = 0;
9449 : 1578768 : unsigned last_idx = 0;
9450 : 4760906 : for (unsigned i = 0; i < chain.length (); ++i)
9451 : : {
9452 : 3513327 : if (chain[i].code != code)
9453 : : {
9454 : : invalid = true;
9455 : : break;
9456 : : }
9457 : 3182138 : if (chain[i].dt != vect_internal_def
9458 : : /* Avoid stmts where the def is not the LHS, like
9459 : : ASMs. */
9460 : 6134640 : || (gimple_get_lhs (bb_vinfo->lookup_def
9461 : 2952502 : (chain[i].op)->stmt)
9462 : 2952502 : != chain[i].op))
9463 : 232568 : remain_cnt++;
9464 : : else
9465 : : last_idx = i;
9466 : : }
9467 : : /* Make sure to have an even number of lanes as we later do
9468 : : all-or-nothing discovery, not trying to split further. */
9469 : 1578768 : if ((chain.length () - remain_cnt) & 1)
9470 : 187463 : remain_cnt++;
9471 : 1578768 : if (!invalid && chain.length () - remain_cnt > 1)
9472 : : {
9473 : 1178639 : vec<stmt_vec_info> stmts;
9474 : 1178639 : vec<tree> remain = vNULL;
9475 : 1178639 : stmts.create (chain.length ());
9476 : 1178639 : if (remain_cnt > 0)
9477 : 108927 : remain.create (remain_cnt);
9478 : 3791170 : for (unsigned i = 0; i < chain.length (); ++i)
9479 : : {
9480 : 2612531 : stmt_vec_info stmt_info;
9481 : 2612531 : if (chain[i].dt == vect_internal_def
9482 : 2577135 : && ((stmt_info = bb_vinfo->lookup_def (chain[i].op)),
9483 : 2577135 : gimple_get_lhs (stmt_info->stmt) == chain[i].op)
9484 : 5189582 : && (i != last_idx
9485 : 1178639 : || (stmts.length () & 1)))
9486 : 2492692 : stmts.quick_push (stmt_info);
9487 : : else
9488 : 119839 : remain.quick_push (chain[i].op);
9489 : : }
9490 : 1178639 : vec<stmt_vec_info> roots;
9491 : 1178639 : roots.create (chain_stmts.length ());
9492 : 2612531 : for (unsigned i = 0; i < chain_stmts.length (); ++i)
9493 : 1433892 : roots.quick_push (bb_vinfo->lookup_stmt (chain_stmts[i]));
9494 : 1178639 : bb_vinfo->roots.safe_push (slp_root (slp_inst_kind_bb_reduc,
9495 : 1178639 : stmts, roots, remain));
9496 : : }
9497 : : }
9498 : 1848987 : }
9499 : : }
9500 : 2389978 : }
9501 : :
9502 : : /* Walk the grouped store chains and replace entries with their
9503 : : pattern variant if any. */
9504 : :
9505 : : static void
9506 : 631013 : vect_fixup_store_groups_with_patterns (vec_info *vinfo)
9507 : : {
9508 : 631013 : stmt_vec_info first_element;
9509 : 631013 : unsigned i;
9510 : :
9511 : 1515323 : FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
9512 : : {
9513 : : /* We also have CTORs in this array. */
9514 : 884310 : if (!STMT_VINFO_GROUPED_ACCESS (first_element))
9515 : 0 : continue;
9516 : 884310 : if (STMT_VINFO_IN_PATTERN_P (first_element))
9517 : : {
9518 : 238 : stmt_vec_info orig = first_element;
9519 : 238 : first_element = STMT_VINFO_RELATED_STMT (first_element);
9520 : 238 : DR_GROUP_FIRST_ELEMENT (first_element) = first_element;
9521 : 238 : DR_GROUP_SIZE (first_element) = DR_GROUP_SIZE (orig);
9522 : 238 : DR_GROUP_GAP (first_element) = DR_GROUP_GAP (orig);
9523 : 238 : DR_GROUP_NEXT_ELEMENT (first_element) = DR_GROUP_NEXT_ELEMENT (orig);
9524 : 238 : vinfo->grouped_stores[i] = first_element;
9525 : : }
9526 : 884310 : stmt_vec_info prev = first_element;
9527 : 2482180 : while (DR_GROUP_NEXT_ELEMENT (prev))
9528 : : {
9529 : 1597870 : stmt_vec_info elt = DR_GROUP_NEXT_ELEMENT (prev);
9530 : 1597870 : if (STMT_VINFO_IN_PATTERN_P (elt))
9531 : : {
9532 : 876 : stmt_vec_info orig = elt;
9533 : 876 : elt = STMT_VINFO_RELATED_STMT (elt);
9534 : 876 : DR_GROUP_NEXT_ELEMENT (prev) = elt;
9535 : 876 : DR_GROUP_GAP (elt) = DR_GROUP_GAP (orig);
9536 : 876 : DR_GROUP_NEXT_ELEMENT (elt) = DR_GROUP_NEXT_ELEMENT (orig);
9537 : : }
9538 : 1597870 : DR_GROUP_FIRST_ELEMENT (elt) = first_element;
9539 : 1597870 : prev = elt;
9540 : : }
9541 : : }
9542 : 631013 : }
9543 : :
9544 : : /* Check if the region described by BB_VINFO can be vectorized, returning
9545 : : true if so. When returning false, set FATAL to true if the same failure
9546 : : would prevent vectorization at other vector sizes, false if it is still
9547 : : worth trying other sizes. N_STMTS is the number of statements in the
9548 : : region. */
9549 : :
9550 : : static bool
9551 : 2389978 : vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal,
9552 : : vec<int> *dataref_groups)
9553 : : {
9554 : 2389978 : DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
9555 : :
9556 : 2389978 : slp_instance instance;
9557 : 2389978 : int i;
9558 : 2389978 : poly_uint64 min_vf = 2;
9559 : :
9560 : : /* The first group of checks is independent of the vector size. */
9561 : 2389978 : fatal = true;
9562 : :
9563 : : /* Analyze the data references. */
9564 : :
9565 : 2389978 : if (!vect_analyze_data_refs (bb_vinfo, &min_vf, NULL))
9566 : : {
9567 : 0 : if (dump_enabled_p ())
9568 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9569 : : "not vectorized: unhandled data-ref in basic "
9570 : : "block.\n");
9571 : 0 : return false;
9572 : : }
9573 : :
9574 : 2389978 : if (!vect_analyze_data_ref_accesses (bb_vinfo, dataref_groups))
9575 : : {
9576 : 0 : if (dump_enabled_p ())
9577 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9578 : : "not vectorized: unhandled data access in "
9579 : : "basic block.\n");
9580 : 0 : return false;
9581 : : }
9582 : :
9583 : 2389978 : vect_slp_check_for_roots (bb_vinfo);
9584 : :
9585 : : /* If there are no grouped stores and no constructors in the region
9586 : : there is no need to continue with pattern recog as vect_analyze_slp
9587 : : will fail anyway. */
9588 : 2389978 : if (bb_vinfo->grouped_stores.is_empty ()
9589 : 2030008 : && bb_vinfo->roots.is_empty ())
9590 : : {
9591 : 1758965 : if (dump_enabled_p ())
9592 : 1044 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9593 : : "not vectorized: no grouped stores in "
9594 : : "basic block.\n");
9595 : 1758965 : return false;
9596 : : }
9597 : :
9598 : : /* While the rest of the analysis below depends on it in some way. */
9599 : 631013 : fatal = false;
9600 : :
9601 : 631013 : vect_pattern_recog (bb_vinfo);
9602 : :
9603 : : /* Update store groups from pattern processing. */
9604 : 631013 : vect_fixup_store_groups_with_patterns (bb_vinfo);
9605 : :
9606 : : /* Check the SLP opportunities in the basic block, analyze and build SLP
9607 : : trees. */
9608 : 631013 : if (!vect_analyze_slp (bb_vinfo, n_stmts, false))
9609 : : {
9610 : 0 : if (dump_enabled_p ())
9611 : : {
9612 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9613 : : "Failed to SLP the basic block.\n");
9614 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9615 : : "not vectorized: failed to find SLP opportunities "
9616 : : "in basic block.\n");
9617 : : }
9618 : 0 : return false;
9619 : : }
9620 : :
9621 : : /* Optimize permutations. */
9622 : 631013 : vect_optimize_slp (bb_vinfo);
9623 : :
9624 : : /* Gather the loads reachable from the SLP graph entries. */
9625 : 631013 : vect_gather_slp_loads (bb_vinfo);
9626 : :
9627 : 631013 : vect_record_base_alignments (bb_vinfo);
9628 : :
9629 : : /* Analyze and verify the alignment of data references and the
9630 : : dependence in the SLP instances. */
9631 : 1505837 : for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
9632 : : {
9633 : 874824 : vect_location = instance->location ();
9634 : 874824 : if (! vect_slp_analyze_instance_alignment (bb_vinfo, instance)
9635 : 874824 : || ! vect_slp_analyze_instance_dependence (bb_vinfo, instance))
9636 : : {
9637 : 9435 : slp_tree node = SLP_INSTANCE_TREE (instance);
9638 : 9435 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
9639 : 9435 : if (dump_enabled_p ())
9640 : 6 : dump_printf_loc (MSG_NOTE, vect_location,
9641 : : "removing SLP instance operations starting from: %G",
9642 : : stmt_info->stmt);
9643 : 9435 : vect_free_slp_instance (instance);
9644 : 9435 : BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
9645 : 9435 : continue;
9646 : 9435 : }
9647 : :
9648 : : /* Mark all the statements that we want to vectorize as pure SLP and
9649 : : relevant. */
9650 : 865389 : vect_mark_slp_stmts (bb_vinfo, SLP_INSTANCE_TREE (instance));
9651 : 865389 : vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
9652 : 865389 : unsigned j;
9653 : 865389 : stmt_vec_info root;
9654 : : /* Likewise consider instance root stmts as vectorized. */
9655 : 1907290 : FOR_EACH_VEC_ELT (SLP_INSTANCE_ROOT_STMTS (instance), j, root)
9656 : 176512 : STMT_SLP_TYPE (root) = pure_slp;
9657 : :
9658 : 865389 : i++;
9659 : : }
9660 : 2427356 : if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
9661 : : return false;
9662 : :
9663 : 342318 : if (!vect_slp_analyze_operations (bb_vinfo))
9664 : : {
9665 : 37378 : if (dump_enabled_p ())
9666 : 93 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9667 : : "not vectorized: bad operation in basic block.\n");
9668 : 37378 : return false;
9669 : : }
9670 : :
9671 : 304940 : vect_bb_partition_graph (bb_vinfo);
9672 : :
9673 : 304940 : return true;
9674 : : }
9675 : :
9676 : : /* Subroutine of vect_slp_bb. Try to vectorize the statements for all
9677 : : basic blocks in BBS, returning true on success.
9678 : : The region has N_STMTS statements and has the datarefs given by DATAREFS. */
9679 : :
9680 : : static bool
9681 : 2063296 : vect_slp_region (vec<basic_block> bbs, vec<data_reference_p> datarefs,
9682 : : vec<int> *dataref_groups, unsigned int n_stmts,
9683 : : loop_p orig_loop)
9684 : : {
9685 : 2063296 : bb_vec_info bb_vinfo;
9686 : 2063296 : auto_vector_modes vector_modes;
9687 : :
9688 : : /* Autodetect first vector size we try. */
9689 : 2063296 : machine_mode next_vector_mode = VOIDmode;
9690 : 2063296 : targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
9691 : 2063296 : unsigned int mode_i = 0;
9692 : :
9693 : 2063296 : vec_info_shared shared;
9694 : :
9695 : 2063296 : machine_mode autodetected_vector_mode = VOIDmode;
9696 : 2716660 : while (1)
9697 : : {
9698 : 2389978 : bool vectorized = false;
9699 : 2389978 : bool fatal = false;
9700 : 2389978 : bb_vinfo = new _bb_vec_info (bbs, &shared);
9701 : :
9702 : 2389978 : bool first_time_p = shared.datarefs.is_empty ();
9703 : 2389978 : BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
9704 : 2389978 : if (first_time_p)
9705 : 2085976 : bb_vinfo->shared->save_datarefs ();
9706 : : else
9707 : 304002 : bb_vinfo->shared->check_datarefs ();
9708 : 2389978 : bb_vinfo->vector_mode = next_vector_mode;
9709 : :
9710 : 2389978 : if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal, dataref_groups))
9711 : : {
9712 : 304940 : if (dump_enabled_p ())
9713 : : {
9714 : 1484 : dump_printf_loc (MSG_NOTE, vect_location,
9715 : : "***** Analysis succeeded with vector mode"
9716 : 742 : " %s\n", GET_MODE_NAME (bb_vinfo->vector_mode));
9717 : 742 : dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
9718 : : }
9719 : :
9720 : 304940 : bb_vinfo->shared->check_datarefs ();
9721 : :
9722 : 304940 : bool force_clear = false;
9723 : 304940 : auto_vec<slp_instance> profitable_subgraphs;
9724 : 1664807 : for (slp_instance instance : BB_VINFO_SLP_INSTANCES (bb_vinfo))
9725 : : {
9726 : 749987 : if (instance->subgraph_entries.is_empty ())
9727 : 284480 : continue;
9728 : :
9729 : 733015 : dump_user_location_t saved_vect_location = vect_location;
9730 : 733015 : vect_location = instance->location ();
9731 : 733015 : if (!unlimited_cost_model (NULL)
9732 : 1462511 : && !vect_bb_vectorization_profitable_p
9733 : 729496 : (bb_vinfo, instance->subgraph_entries, orig_loop))
9734 : : {
9735 : 250536 : if (dump_enabled_p ())
9736 : 30 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9737 : : "not vectorized: vectorization is not "
9738 : : "profitable.\n");
9739 : 250536 : vect_location = saved_vect_location;
9740 : 250536 : continue;
9741 : : }
9742 : :
9743 : 482479 : vect_location = saved_vect_location;
9744 : 482479 : if (!dbg_cnt (vect_slp))
9745 : : {
9746 : 0 : force_clear = true;
9747 : 0 : continue;
9748 : : }
9749 : :
9750 : 482479 : profitable_subgraphs.safe_push (instance);
9751 : : }
9752 : :
9753 : : /* When we're vectorizing an if-converted loop body make sure
9754 : : we vectorized all if-converted code. */
9755 : 472626 : if ((!profitable_subgraphs.is_empty () || force_clear) && orig_loop)
9756 : : {
9757 : 106 : gcc_assert (bb_vinfo->nbbs == 1);
9758 : 212 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb_vinfo->bbs[0]);
9759 : 4395 : !gsi_end_p (gsi); gsi_next (&gsi))
9760 : : {
9761 : : /* The costing above left us with DCEable vectorized scalar
9762 : : stmts having the visited flag set on profitable
9763 : : subgraphs. Do the delayed clearing of the flag here. */
9764 : 4289 : if (gimple_visited_p (gsi_stmt (gsi)))
9765 : : {
9766 : 1186 : gimple_set_visited (gsi_stmt (gsi), false);
9767 : 1186 : continue;
9768 : : }
9769 : 3103 : if (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED)
9770 : 868 : continue;
9771 : :
9772 : 6335 : if (gassign *ass = dyn_cast <gassign *> (gsi_stmt (gsi)))
9773 : 2648 : if (gimple_assign_rhs_code (ass) == COND_EXPR)
9774 : : {
9775 : 72 : if (!profitable_subgraphs.is_empty ()
9776 : 29 : && dump_enabled_p ())
9777 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9778 : : "not profitable because of "
9779 : : "unprofitable if-converted scalar "
9780 : : "code\n");
9781 : 43 : profitable_subgraphs.truncate (0);
9782 : : }
9783 : : }
9784 : : }
9785 : :
9786 : : /* Finally schedule the profitable subgraphs. */
9787 : 1122752 : for (slp_instance instance : profitable_subgraphs)
9788 : : {
9789 : 482440 : if (!vectorized && dump_enabled_p ())
9790 : 716 : dump_printf_loc (MSG_NOTE, vect_location,
9791 : : "Basic block will be vectorized "
9792 : : "using SLP\n");
9793 : 482440 : vectorized = true;
9794 : :
9795 : : /* Dump before scheduling as store vectorization will remove
9796 : : the original stores and mess with the instance tree
9797 : : so querying its location will eventually ICE. */
9798 : 482440 : if (flag_checking)
9799 : 1937920 : for (slp_instance sub : instance->subgraph_entries)
9800 : 490600 : gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
9801 : 482440 : unsigned HOST_WIDE_INT bytes;
9802 : 482440 : if (dump_enabled_p ())
9803 : 3383 : for (slp_instance sub : instance->subgraph_entries)
9804 : : {
9805 : 896 : tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
9806 : 1792 : if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
9807 : 896 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
9808 : 896 : sub->location (),
9809 : : "basic block part vectorized using %wu "
9810 : : "byte vectors\n", bytes);
9811 : : else
9812 : : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
9813 : : sub->location (),
9814 : : "basic block part vectorized using "
9815 : : "variable length vectors\n");
9816 : : }
9817 : :
9818 : 482440 : dump_user_location_t saved_vect_location = vect_location;
9819 : 482440 : vect_location = instance->location ();
9820 : :
9821 : 482440 : vect_schedule_slp (bb_vinfo, instance->subgraph_entries);
9822 : :
9823 : 482440 : vect_location = saved_vect_location;
9824 : : }
9825 : :
9826 : :
9827 : : /* Generate the invariant statements. */
9828 : 304940 : if (!gimple_seq_empty_p (bb_vinfo->inv_pattern_def_seq))
9829 : : {
9830 : 47 : if (dump_enabled_p ())
9831 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9832 : : "------>generating invariant statements\n");
9833 : :
9834 : 47 : bb_vinfo->insert_seq_on_entry (NULL,
9835 : : bb_vinfo->inv_pattern_def_seq);
9836 : : }
9837 : 304940 : }
9838 : : else
9839 : : {
9840 : 2085038 : if (dump_enabled_p ())
9841 : 1392 : dump_printf_loc (MSG_NOTE, vect_location,
9842 : : "***** Analysis failed with vector mode %s\n",
9843 : 1392 : GET_MODE_NAME (bb_vinfo->vector_mode));
9844 : : }
9845 : :
9846 : 2389978 : if (mode_i == 0)
9847 : 2063296 : autodetected_vector_mode = bb_vinfo->vector_mode;
9848 : :
9849 : 2389978 : if (!fatal)
9850 : 3369442 : while (mode_i < vector_modes.length ()
9851 : 1822214 : && vect_chooses_same_modes_p (bb_vinfo, vector_modes[mode_i]))
9852 : : {
9853 : 348451 : if (dump_enabled_p ())
9854 : 1672 : dump_printf_loc (MSG_NOTE, vect_location,
9855 : : "***** The result for vector mode %s would"
9856 : : " be the same\n",
9857 : 836 : GET_MODE_NAME (vector_modes[mode_i]));
9858 : 348451 : mode_i += 1;
9859 : : }
9860 : :
9861 : 2389978 : delete bb_vinfo;
9862 : :
9863 : 2389978 : if (mode_i < vector_modes.length ()
9864 : 2207089 : && VECTOR_MODE_P (autodetected_vector_mode)
9865 : 2074650 : && (related_vector_mode (vector_modes[mode_i],
9866 : : GET_MODE_INNER (autodetected_vector_mode))
9867 : 1037325 : == autodetected_vector_mode)
9868 : 4597067 : && (related_vector_mode (autodetected_vector_mode,
9869 : 542567 : GET_MODE_INNER (vector_modes[mode_i]))
9870 : 1085134 : == vector_modes[mode_i]))
9871 : : {
9872 : 542567 : if (dump_enabled_p ())
9873 : 212 : dump_printf_loc (MSG_NOTE, vect_location,
9874 : : "***** Skipping vector mode %s, which would"
9875 : : " repeat the analysis for %s\n",
9876 : 212 : GET_MODE_NAME (vector_modes[mode_i]),
9877 : 212 : GET_MODE_NAME (autodetected_vector_mode));
9878 : 542567 : mode_i += 1;
9879 : : }
9880 : :
9881 : 2389978 : if (vectorized
9882 : 2222321 : || mode_i == vector_modes.length ()
9883 : 2039472 : || autodetected_vector_mode == VOIDmode
9884 : : /* If vect_slp_analyze_bb_1 signaled that analysis for all
9885 : : vector sizes will fail do not bother iterating. */
9886 : 3259686 : || fatal)
9887 : 4126592 : return vectorized;
9888 : :
9889 : : /* Try the next biggest vector size. */
9890 : 326682 : next_vector_mode = vector_modes[mode_i++];
9891 : 326682 : if (dump_enabled_p ())
9892 : 256 : dump_printf_loc (MSG_NOTE, vect_location,
9893 : : "***** Re-trying analysis with vector mode %s\n",
9894 : 256 : GET_MODE_NAME (next_vector_mode));
9895 : 326682 : }
9896 : 2063296 : }
9897 : :
9898 : :
9899 : : /* Main entry for the BB vectorizer. Analyze and transform BBS, returns
9900 : : true if anything in the basic-block was vectorized. */
9901 : :
9902 : : static bool
9903 : 2063296 : vect_slp_bbs (const vec<basic_block> &bbs, loop_p orig_loop)
9904 : : {
9905 : 2063296 : vec<data_reference_p> datarefs = vNULL;
9906 : 2063296 : auto_vec<int> dataref_groups;
9907 : 2063296 : int insns = 0;
9908 : 2063296 : int current_group = 0;
9909 : :
9910 : 13183968 : for (unsigned i = 0; i < bbs.length (); i++)
9911 : : {
9912 : 11120672 : basic_block bb = bbs[i];
9913 : 88508447 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
9914 : 77387775 : gsi_next (&gsi))
9915 : : {
9916 : 77387775 : gimple *stmt = gsi_stmt (gsi);
9917 : 77387775 : if (is_gimple_debug (stmt))
9918 : 47385662 : continue;
9919 : :
9920 : 30002113 : insns++;
9921 : :
9922 : 30002113 : if (gimple_location (stmt) != UNKNOWN_LOCATION)
9923 : 26966521 : vect_location = stmt;
9924 : :
9925 : 30002113 : if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs,
9926 : : &dataref_groups, current_group))
9927 : 5204096 : ++current_group;
9928 : : }
9929 : : /* New BBs always start a new DR group. */
9930 : 11120672 : ++current_group;
9931 : : }
9932 : :
9933 : 2063296 : return vect_slp_region (bbs, datarefs, &dataref_groups, insns, orig_loop);
9934 : 2063296 : }
9935 : :
9936 : : /* Special entry for the BB vectorizer. Analyze and transform a single
9937 : : if-converted BB with ORIG_LOOPs body being the not if-converted
9938 : : representation. Returns true if anything in the basic-block was
9939 : : vectorized. */
9940 : :
9941 : : bool
9942 : 18294 : vect_slp_if_converted_bb (basic_block bb, loop_p orig_loop)
9943 : : {
9944 : 18294 : auto_vec<basic_block> bbs;
9945 : 18294 : bbs.safe_push (bb);
9946 : 18294 : return vect_slp_bbs (bbs, orig_loop);
9947 : 18294 : }
9948 : :
9949 : : /* Main entry for the BB vectorizer. Analyze and transform BB, returns
9950 : : true if anything in the basic-block was vectorized. */
9951 : :
9952 : : bool
9953 : 898056 : vect_slp_function (function *fun)
9954 : : {
9955 : 898056 : bool r = false;
9956 : 898056 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
9957 : 898056 : auto_bitmap exit_bbs;
9958 : 898056 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
9959 : 898056 : edge entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fun));
9960 : 898056 : unsigned n = rev_post_order_and_mark_dfs_back_seme (fun, entry, exit_bbs,
9961 : 898056 : true, rpo, NULL);
9962 : :
9963 : : /* For the moment split the function into pieces to avoid making
9964 : : the iteration on the vector mode moot. Split at points we know
9965 : : to not handle well which is CFG merges (SLP discovery doesn't
9966 : : handle non-loop-header PHIs) and loop exits. Since pattern
9967 : : recog requires reverse iteration to visit uses before defs
9968 : : simply chop RPO into pieces. */
9969 : 898056 : auto_vec<basic_block> bbs;
9970 : 12026822 : for (unsigned i = 0; i < n; i++)
9971 : : {
9972 : 11128766 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
9973 : 11128766 : bool split = false;
9974 : :
9975 : : /* Split when a BB is not dominated by the first block. */
9976 : 20999537 : if (!bbs.is_empty ()
9977 : 9870771 : && !dominated_by_p (CDI_DOMINATORS, bb, bbs[0]))
9978 : : {
9979 : 802210 : if (dump_enabled_p ())
9980 : 162 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9981 : : "splitting region at dominance boundary bb%d\n",
9982 : : bb->index);
9983 : : split = true;
9984 : : }
9985 : : /* Split when the loop determined by the first block
9986 : : is exited. This is because we eventually insert
9987 : : invariants at region begin. */
9988 : 19395117 : else if (!bbs.is_empty ()
9989 : 9068561 : && bbs[0]->loop_father != bb->loop_father
9990 : 2284615 : && !flow_loop_nested_p (bbs[0]->loop_father, bb->loop_father))
9991 : : {
9992 : 5155 : if (dump_enabled_p ())
9993 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9994 : : "splitting region at loop %d exit at bb%d\n",
9995 : 3 : bbs[0]->loop_father->num, bb->index);
9996 : : split = true;
9997 : : }
9998 : 10321401 : else if (!bbs.is_empty ()
9999 : 9063406 : && bb->loop_father->header == bb
10000 : 474134 : && bb->loop_father->dont_vectorize)
10001 : : {
10002 : 6030 : if (dump_enabled_p ())
10003 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10004 : : "splitting region at dont-vectorize loop %d "
10005 : : "entry at bb%d\n",
10006 : : bb->loop_father->num, bb->index);
10007 : : split = true;
10008 : : }
10009 : :
10010 : 11942161 : if (split && !bbs.is_empty ())
10011 : : {
10012 : 813395 : r |= vect_slp_bbs (bbs, NULL);
10013 : 813395 : bbs.truncate (0);
10014 : : }
10015 : :
10016 : 11128766 : if (bbs.is_empty ())
10017 : : {
10018 : : /* We need to be able to insert at the head of the region which
10019 : : we cannot for region starting with a returns-twice call. */
10020 : 2071390 : if (gcall *first = safe_dyn_cast <gcall *> (first_stmt (bb)))
10021 : 401094 : if (gimple_call_flags (first) & ECF_RETURNS_TWICE)
10022 : : {
10023 : 294 : if (dump_enabled_p ())
10024 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10025 : : "skipping bb%d as start of region as it "
10026 : : "starts with returns-twice call\n",
10027 : : bb->index);
10028 : 26388 : continue;
10029 : : }
10030 : : /* If the loop this BB belongs to is marked as not to be vectorized
10031 : : honor that also for BB vectorization. */
10032 : 2071096 : if (bb->loop_father->dont_vectorize)
10033 : 26094 : continue;
10034 : : }
10035 : :
10036 : 11102378 : bbs.safe_push (bb);
10037 : :
10038 : : /* When we have a stmt ending this block and defining a
10039 : : value we have to insert on edges when inserting after it for
10040 : : a vector containing its definition. Avoid this for now. */
10041 : 22204756 : if (gimple *last = *gsi_last_bb (bb))
10042 : 8765700 : if (gimple_get_lhs (last)
10043 : 8765700 : && is_ctrl_altering_stmt (last))
10044 : : {
10045 : 333558 : if (dump_enabled_p ())
10046 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10047 : : "splitting region at control altering "
10048 : : "definition %G", last);
10049 : 333558 : r |= vect_slp_bbs (bbs, NULL);
10050 : 333558 : bbs.truncate (0);
10051 : : }
10052 : : }
10053 : :
10054 : 898056 : if (!bbs.is_empty ())
10055 : 898049 : r |= vect_slp_bbs (bbs, NULL);
10056 : :
10057 : 898056 : free (rpo);
10058 : :
10059 : 898056 : return r;
10060 : 898056 : }
10061 : :
10062 : : /* Build a variable-length vector in which the elements in ELTS are repeated
10063 : : to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
10064 : : RESULTS and add any new instructions to SEQ.
10065 : :
10066 : : The approach we use is:
10067 : :
10068 : : (1) Find a vector mode VM with integer elements of mode IM.
10069 : :
10070 : : (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10071 : : ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
10072 : : from small vectors to IM.
10073 : :
10074 : : (3) Duplicate each ELTS'[I] into a vector of mode VM.
10075 : :
10076 : : (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
10077 : : correct byte contents.
10078 : :
10079 : : (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
10080 : :
10081 : : We try to find the largest IM for which this sequence works, in order
10082 : : to cut down on the number of interleaves. */
10083 : :
10084 : : void
10085 : 0 : duplicate_and_interleave (vec_info *vinfo, gimple_seq *seq, tree vector_type,
10086 : : const vec<tree> &elts, unsigned int nresults,
10087 : : vec<tree> &results)
10088 : : {
10089 : 0 : unsigned int nelts = elts.length ();
10090 : 0 : tree element_type = TREE_TYPE (vector_type);
10091 : :
10092 : : /* (1) Find a vector mode VM with integer elements of mode IM. */
10093 : 0 : unsigned int nvectors = 1;
10094 : 0 : tree new_vector_type;
10095 : 0 : tree permutes[2];
10096 : 0 : if (!can_duplicate_and_interleave_p (vinfo, nelts, element_type,
10097 : : &nvectors, &new_vector_type,
10098 : : permutes))
10099 : 0 : gcc_unreachable ();
10100 : :
10101 : : /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
10102 : 0 : unsigned int partial_nelts = nelts / nvectors;
10103 : 0 : tree partial_vector_type = build_vector_type (element_type, partial_nelts);
10104 : :
10105 : 0 : tree_vector_builder partial_elts;
10106 : 0 : auto_vec<tree, 32> pieces (nvectors * 2);
10107 : 0 : pieces.quick_grow_cleared (nvectors * 2);
10108 : 0 : for (unsigned int i = 0; i < nvectors; ++i)
10109 : : {
10110 : : /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
10111 : : ELTS' has mode IM. */
10112 : 0 : partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
10113 : 0 : for (unsigned int j = 0; j < partial_nelts; ++j)
10114 : 0 : partial_elts.quick_push (elts[i * partial_nelts + j]);
10115 : 0 : tree t = gimple_build_vector (seq, &partial_elts);
10116 : 0 : t = gimple_build (seq, VIEW_CONVERT_EXPR,
10117 : 0 : TREE_TYPE (new_vector_type), t);
10118 : :
10119 : : /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
10120 : 0 : pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
10121 : : }
10122 : :
10123 : : /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
10124 : : correct byte contents.
10125 : :
10126 : : Conceptually, we need to repeat the following operation log2(nvectors)
10127 : : times, where hi_start = nvectors / 2:
10128 : :
10129 : : out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
10130 : : out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
10131 : :
10132 : : However, if each input repeats every N elements and the VF is
10133 : : a multiple of N * 2, the HI result is the same as the LO result.
10134 : : This will be true for the first N1 iterations of the outer loop,
10135 : : followed by N2 iterations for which both the LO and HI results
10136 : : are needed. I.e.:
10137 : :
10138 : : N1 + N2 = log2(nvectors)
10139 : :
10140 : : Each "N1 iteration" doubles the number of redundant vectors and the
10141 : : effect of the process as a whole is to have a sequence of nvectors/2**N1
10142 : : vectors that repeats 2**N1 times. Rather than generate these redundant
10143 : : vectors, we halve the number of vectors for each N1 iteration. */
10144 : : unsigned int in_start = 0;
10145 : : unsigned int out_start = nvectors;
10146 : : unsigned int new_nvectors = nvectors;
10147 : 0 : for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
10148 : : {
10149 : 0 : unsigned int hi_start = new_nvectors / 2;
10150 : 0 : unsigned int out_i = 0;
10151 : 0 : for (unsigned int in_i = 0; in_i < new_nvectors; ++in_i)
10152 : : {
10153 : 0 : if ((in_i & 1) != 0
10154 : 0 : && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
10155 : : 2 * in_repeat))
10156 : 0 : continue;
10157 : :
10158 : 0 : tree output = make_ssa_name (new_vector_type);
10159 : 0 : tree input1 = pieces[in_start + (in_i / 2)];
10160 : 0 : tree input2 = pieces[in_start + (in_i / 2) + hi_start];
10161 : 0 : gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
10162 : : input1, input2,
10163 : : permutes[in_i & 1]);
10164 : 0 : gimple_seq_add_stmt (seq, stmt);
10165 : 0 : pieces[out_start + out_i] = output;
10166 : 0 : out_i += 1;
10167 : : }
10168 : 0 : std::swap (in_start, out_start);
10169 : 0 : new_nvectors = out_i;
10170 : : }
10171 : :
10172 : : /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
10173 : 0 : results.reserve (nresults);
10174 : 0 : for (unsigned int i = 0; i < nresults; ++i)
10175 : 0 : if (i < new_nvectors)
10176 : 0 : results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
10177 : 0 : pieces[in_start + i]));
10178 : : else
10179 : 0 : results.quick_push (results[i - new_nvectors]);
10180 : 0 : }
10181 : :
10182 : :
10183 : : /* For constant and loop invariant defs in OP_NODE this function creates
10184 : : vector defs that will be used in the vectorized stmts and stores them
10185 : : to SLP_TREE_VEC_DEFS of OP_NODE. */
10186 : :
10187 : : static void
10188 : 484788 : vect_create_constant_vectors (vec_info *vinfo, slp_tree op_node)
10189 : : {
10190 : 484788 : unsigned HOST_WIDE_INT nunits;
10191 : 484788 : tree vec_cst;
10192 : 484788 : unsigned j, number_of_places_left_in_vector;
10193 : 484788 : tree vector_type;
10194 : 484788 : tree vop;
10195 : 484788 : int group_size = op_node->ops.length ();
10196 : 484788 : unsigned int vec_num, i;
10197 : 484788 : unsigned number_of_copies = 1;
10198 : 484788 : bool constant_p;
10199 : 484788 : gimple_seq ctor_seq = NULL;
10200 : 484788 : auto_vec<tree, 16> permute_results;
10201 : :
10202 : : /* We always want SLP_TREE_VECTYPE (op_node) here correctly set. */
10203 : 484788 : vector_type = SLP_TREE_VECTYPE (op_node);
10204 : :
10205 : 484788 : unsigned int number_of_vectors = SLP_TREE_NUMBER_OF_VEC_STMTS (op_node);
10206 : 484788 : SLP_TREE_VEC_DEFS (op_node).create (number_of_vectors);
10207 : 484788 : auto_vec<tree> voprnds (number_of_vectors);
10208 : :
10209 : : /* NUMBER_OF_COPIES is the number of times we need to use the same values in
10210 : : created vectors. It is greater than 1 if unrolling is performed.
10211 : :
10212 : : For example, we have two scalar operands, s1 and s2 (e.g., group of
10213 : : strided accesses of size two), while NUNITS is four (i.e., four scalars
10214 : : of this type can be packed in a vector). The output vector will contain
10215 : : two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
10216 : : will be 2).
10217 : :
10218 : : If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
10219 : : containing the operands.
10220 : :
10221 : : For example, NUNITS is four as before, and the group size is 8
10222 : : (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
10223 : : {s5, s6, s7, s8}. */
10224 : :
10225 : : /* When using duplicate_and_interleave, we just need one element for
10226 : : each scalar statement. */
10227 : 484788 : if (!TYPE_VECTOR_SUBPARTS (vector_type).is_constant (&nunits))
10228 : : nunits = group_size;
10229 : :
10230 : 484788 : number_of_copies = nunits * number_of_vectors / group_size;
10231 : :
10232 : 484788 : number_of_places_left_in_vector = nunits;
10233 : 484788 : constant_p = true;
10234 : 484788 : tree uniform_elt = NULL_TREE;
10235 : 484788 : tree_vector_builder elts (vector_type, nunits, 1);
10236 : 484788 : elts.quick_grow (nunits);
10237 : 484788 : stmt_vec_info insert_after = NULL;
10238 : 1348753 : for (j = 0; j < number_of_copies; j++)
10239 : : {
10240 : 863965 : tree op;
10241 : 3392336 : for (i = group_size - 1; op_node->ops.iterate (i, &op); i--)
10242 : : {
10243 : : /* Create 'vect_ = {op0,op1,...,opn}'. */
10244 : 1664406 : tree orig_op = op;
10245 : 1664406 : if (number_of_places_left_in_vector == nunits)
10246 : : uniform_elt = op;
10247 : 1057476 : else if (uniform_elt && operand_equal_p (uniform_elt, op))
10248 : 636018 : op = elts[number_of_places_left_in_vector];
10249 : : else
10250 : : uniform_elt = NULL_TREE;
10251 : 1664406 : number_of_places_left_in_vector--;
10252 : 1664406 : if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
10253 : : {
10254 : 283935 : if (CONSTANT_CLASS_P (op))
10255 : : {
10256 : 105785 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10257 : : {
10258 : : /* Can't use VIEW_CONVERT_EXPR for booleans because
10259 : : of possibly different sizes of scalar value and
10260 : : vector element. */
10261 : 64 : if (integer_zerop (op))
10262 : 64 : op = build_int_cst (TREE_TYPE (vector_type), 0);
10263 : 0 : else if (integer_onep (op))
10264 : 0 : op = build_all_ones_cst (TREE_TYPE (vector_type));
10265 : : else
10266 : 0 : gcc_unreachable ();
10267 : : }
10268 : : else
10269 : 105721 : op = fold_unary (VIEW_CONVERT_EXPR,
10270 : : TREE_TYPE (vector_type), op);
10271 : 105785 : gcc_assert (op && CONSTANT_CLASS_P (op));
10272 : : }
10273 : : else
10274 : : {
10275 : 178150 : tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
10276 : 178150 : gimple *init_stmt;
10277 : 178150 : if (VECTOR_BOOLEAN_TYPE_P (vector_type))
10278 : : {
10279 : 317 : tree true_val
10280 : 317 : = build_all_ones_cst (TREE_TYPE (vector_type));
10281 : 317 : tree false_val
10282 : 317 : = build_zero_cst (TREE_TYPE (vector_type));
10283 : 317 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
10284 : 317 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
10285 : : op, true_val,
10286 : : false_val);
10287 : : }
10288 : : else
10289 : : {
10290 : 177833 : op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
10291 : : op);
10292 : 177833 : init_stmt
10293 : 177833 : = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
10294 : : op);
10295 : : }
10296 : 178150 : gimple_seq_add_stmt (&ctor_seq, init_stmt);
10297 : 178150 : op = new_temp;
10298 : : }
10299 : : }
10300 : 1664406 : elts[number_of_places_left_in_vector] = op;
10301 : 1664406 : if (!CONSTANT_CLASS_P (op))
10302 : 309436 : constant_p = false;
10303 : : /* For BB vectorization we have to compute an insert location
10304 : : when a def is inside the analyzed region since we cannot
10305 : : simply insert at the BB start in this case. */
10306 : 1664406 : stmt_vec_info opdef;
10307 : 1664406 : if (TREE_CODE (orig_op) == SSA_NAME
10308 : 171954 : && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
10309 : 159247 : && is_a <bb_vec_info> (vinfo)
10310 : 1772086 : && (opdef = vinfo->lookup_def (orig_op)))
10311 : : {
10312 : 85563 : if (!insert_after)
10313 : : insert_after = opdef;
10314 : : else
10315 : 46481 : insert_after = get_later_stmt (insert_after, opdef);
10316 : : }
10317 : :
10318 : 1664406 : if (number_of_places_left_in_vector == 0)
10319 : : {
10320 : 606930 : auto type_nunits = TYPE_VECTOR_SUBPARTS (vector_type);
10321 : 606930 : if (uniform_elt)
10322 : 622168 : vec_cst = gimple_build_vector_from_val (&ctor_seq, vector_type,
10323 : 311084 : elts[0]);
10324 : 591692 : else if (constant_p
10325 : 591692 : ? multiple_p (type_nunits, nunits)
10326 : 113001 : : known_eq (type_nunits, nunits))
10327 : 295846 : vec_cst = gimple_build_vector (&ctor_seq, &elts);
10328 : : else
10329 : : {
10330 : 0 : if (permute_results.is_empty ())
10331 : 0 : duplicate_and_interleave (vinfo, &ctor_seq, vector_type,
10332 : : elts, number_of_vectors,
10333 : : permute_results);
10334 : 0 : vec_cst = permute_results[number_of_vectors - j - 1];
10335 : : }
10336 : 606930 : if (!gimple_seq_empty_p (ctor_seq))
10337 : : {
10338 : 138262 : if (insert_after)
10339 : : {
10340 : 39082 : gimple_stmt_iterator gsi;
10341 : 39082 : if (gimple_code (insert_after->stmt) == GIMPLE_PHI)
10342 : : {
10343 : 744 : gsi = gsi_after_labels (gimple_bb (insert_after->stmt));
10344 : 744 : gsi_insert_seq_before (&gsi, ctor_seq,
10345 : : GSI_CONTINUE_LINKING);
10346 : : }
10347 : 38338 : else if (!stmt_ends_bb_p (insert_after->stmt))
10348 : : {
10349 : 38338 : gsi = gsi_for_stmt (insert_after->stmt);
10350 : 38338 : gsi_insert_seq_after (&gsi, ctor_seq,
10351 : : GSI_CONTINUE_LINKING);
10352 : : }
10353 : : else
10354 : : {
10355 : : /* When we want to insert after a def where the
10356 : : defining stmt throws then insert on the fallthru
10357 : : edge. */
10358 : 0 : edge e = find_fallthru_edge
10359 : 0 : (gimple_bb (insert_after->stmt)->succs);
10360 : 0 : basic_block new_bb
10361 : 0 : = gsi_insert_seq_on_edge_immediate (e, ctor_seq);
10362 : 0 : gcc_assert (!new_bb);
10363 : : }
10364 : : }
10365 : : else
10366 : 99180 : vinfo->insert_seq_on_entry (NULL, ctor_seq);
10367 : 138262 : ctor_seq = NULL;
10368 : : }
10369 : 606930 : voprnds.quick_push (vec_cst);
10370 : 606930 : insert_after = NULL;
10371 : 606930 : number_of_places_left_in_vector = nunits;
10372 : 606930 : constant_p = true;
10373 : 606930 : elts.new_vector (vector_type, nunits, 1);
10374 : 606930 : elts.quick_grow (nunits);
10375 : : }
10376 : : }
10377 : : }
10378 : :
10379 : : /* Since the vectors are created in the reverse order, we should invert
10380 : : them. */
10381 : 484788 : vec_num = voprnds.length ();
10382 : 1091718 : for (j = vec_num; j != 0; j--)
10383 : : {
10384 : 606930 : vop = voprnds[j - 1];
10385 : 606930 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10386 : : }
10387 : :
10388 : : /* In case that VF is greater than the unrolling factor needed for the SLP
10389 : : group of stmts, NUMBER_OF_VECTORS to be created is greater than
10390 : : NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
10391 : : to replicate the vectors. */
10392 : 484788 : while (number_of_vectors > SLP_TREE_VEC_DEFS (op_node).length ())
10393 : 484788 : for (i = 0; SLP_TREE_VEC_DEFS (op_node).iterate (i, &vop) && i < vec_num;
10394 : : i++)
10395 : 0 : SLP_TREE_VEC_DEFS (op_node).quick_push (vop);
10396 : 484788 : }
10397 : :
10398 : : /* Get the scalar definition of the Nth lane from SLP_NODE or NULL_TREE
10399 : : if there is no definition for it in the scalar IL or it is not known. */
10400 : :
10401 : : tree
10402 : 52 : vect_get_slp_scalar_def (slp_tree slp_node, unsigned n)
10403 : : {
10404 : 52 : if (SLP_TREE_DEF_TYPE (slp_node) == vect_internal_def)
10405 : : {
10406 : 52 : if (!SLP_TREE_SCALAR_STMTS (slp_node).exists ())
10407 : : return NULL_TREE;
10408 : 52 : stmt_vec_info def = SLP_TREE_SCALAR_STMTS (slp_node)[n];
10409 : 52 : if (!def)
10410 : : return NULL_TREE;
10411 : 52 : return gimple_get_lhs (STMT_VINFO_STMT (def));
10412 : : }
10413 : : else
10414 : 0 : return SLP_TREE_SCALAR_OPS (slp_node)[n];
10415 : : }
10416 : :
10417 : : /* Get the Ith vectorized definition from SLP_NODE. */
10418 : :
10419 : : tree
10420 : 133138 : vect_get_slp_vect_def (slp_tree slp_node, unsigned i)
10421 : : {
10422 : 133138 : return SLP_TREE_VEC_DEFS (slp_node)[i];
10423 : : }
10424 : :
10425 : : /* Get the vectorized definitions of SLP_NODE in *VEC_DEFS. */
10426 : :
10427 : : void
10428 : 885024 : vect_get_slp_defs (slp_tree slp_node, vec<tree> *vec_defs)
10429 : : {
10430 : 885024 : vec_defs->create (SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node));
10431 : 885024 : vec_defs->splice (SLP_TREE_VEC_DEFS (slp_node));
10432 : 885024 : }
10433 : :
10434 : : /* Get N vectorized definitions for SLP_NODE. */
10435 : :
10436 : : void
10437 : 2213 : vect_get_slp_defs (vec_info *,
10438 : : slp_tree slp_node, vec<vec<tree> > *vec_oprnds, unsigned n)
10439 : : {
10440 : 2213 : if (n == -1U)
10441 : 2213 : n = SLP_TREE_CHILDREN (slp_node).length ();
10442 : :
10443 : 8044 : for (unsigned i = 0; i < n; ++i)
10444 : : {
10445 : 5831 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[i];
10446 : 5831 : vec<tree> vec_defs = vNULL;
10447 : 5831 : vect_get_slp_defs (child, &vec_defs);
10448 : 5831 : vec_oprnds->quick_push (vec_defs);
10449 : : }
10450 : 2213 : }
10451 : :
10452 : : /* A subroutine of vect_transform_slp_perm_load with two extra arguments:
10453 : : - PERM gives the permutation that the caller wants to use for NODE,
10454 : : which might be different from SLP_LOAD_PERMUTATION.
10455 : : - DUMP_P controls whether the function dumps information. */
10456 : :
10457 : : static bool
10458 : 143886 : vect_transform_slp_perm_load_1 (vec_info *vinfo, slp_tree node,
10459 : : load_permutation_t &perm,
10460 : : const vec<tree> &dr_chain,
10461 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
10462 : : bool analyze_only, bool dump_p,
10463 : : unsigned *n_perms, unsigned int *n_loads,
10464 : : bool dce_chain)
10465 : : {
10466 : 143886 : stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
10467 : 143886 : int vec_index = 0;
10468 : 143886 : tree vectype = SLP_TREE_VECTYPE (node);
10469 : 143886 : unsigned int group_size = SLP_TREE_SCALAR_STMTS (node).length ();
10470 : 143886 : unsigned int mask_element;
10471 : 143886 : unsigned dr_group_size;
10472 : 143886 : machine_mode mode;
10473 : :
10474 : 143886 : if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
10475 : : dr_group_size = 1;
10476 : : else
10477 : : {
10478 : 142484 : stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10479 : 142484 : dr_group_size = DR_GROUP_SIZE (stmt_info);
10480 : : }
10481 : :
10482 : 143886 : mode = TYPE_MODE (vectype);
10483 : 143886 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10484 : 143886 : unsigned int nstmts = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
10485 : :
10486 : : /* Initialize the vect stmts of NODE to properly insert the generated
10487 : : stmts later. */
10488 : 143886 : if (! analyze_only)
10489 : 46222 : for (unsigned i = SLP_TREE_VEC_DEFS (node).length (); i < nstmts; i++)
10490 : 17648 : SLP_TREE_VEC_DEFS (node).quick_push (NULL_TREE);
10491 : :
10492 : : /* Generate permutation masks for every NODE. Number of masks for each NODE
10493 : : is equal to GROUP_SIZE.
10494 : : E.g., we have a group of three nodes with three loads from the same
10495 : : location in each node, and the vector size is 4. I.e., we have a
10496 : : a0b0c0a1b1c1... sequence and we need to create the following vectors:
10497 : : for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
10498 : : for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
10499 : : ...
10500 : :
10501 : : The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
10502 : : The last mask is illegal since we assume two operands for permute
10503 : : operation, and the mask element values can't be outside that range.
10504 : : Hence, the last mask must be converted into {2,5,5,5}.
10505 : : For the first two permutations we need the first and the second input
10506 : : vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
10507 : : we need the second and the third vectors: {b1,c1,a2,b2} and
10508 : : {c2,a3,b3,c3}. */
10509 : :
10510 : 143886 : int vect_stmts_counter = 0;
10511 : 143886 : unsigned int index = 0;
10512 : 143886 : int first_vec_index = -1;
10513 : 143886 : int second_vec_index = -1;
10514 : 143886 : bool noop_p = true;
10515 : 143886 : *n_perms = 0;
10516 : :
10517 : 143886 : vec_perm_builder mask;
10518 : 143886 : unsigned int nelts_to_build;
10519 : 143886 : unsigned int nvectors_per_build;
10520 : 143886 : unsigned int in_nlanes;
10521 : 143886 : bool repeating_p = (group_size == dr_group_size
10522 : 184349 : && multiple_p (nunits, group_size));
10523 : 143886 : if (repeating_p)
10524 : : {
10525 : : /* A single vector contains a whole number of copies of the node, so:
10526 : : (a) all permutes can use the same mask; and
10527 : : (b) the permutes only need a single vector input. */
10528 : 28878 : mask.new_vector (nunits, group_size, 3);
10529 : 28878 : nelts_to_build = mask.encoded_nelts ();
10530 : : /* It's possible to obtain zero nstmts during analyze_only, so make
10531 : : it at least one to ensure the later computation for n_perms
10532 : : proceed. */
10533 : 28878 : nvectors_per_build = nstmts > 0 ? nstmts : 1;
10534 : 28878 : in_nlanes = dr_group_size * 3;
10535 : : }
10536 : : else
10537 : : {
10538 : : /* We need to construct a separate mask for each vector statement. */
10539 : 115008 : unsigned HOST_WIDE_INT const_nunits, const_vf;
10540 : 115008 : if (!nunits.is_constant (&const_nunits)
10541 : 115008 : || !vf.is_constant (&const_vf))
10542 : : return false;
10543 : 115008 : mask.new_vector (const_nunits, const_nunits, 1);
10544 : 115008 : nelts_to_build = const_vf * group_size;
10545 : 115008 : nvectors_per_build = 1;
10546 : 115008 : in_nlanes = const_vf * dr_group_size;
10547 : : }
10548 : 143886 : auto_sbitmap used_in_lanes (in_nlanes);
10549 : 143886 : bitmap_clear (used_in_lanes);
10550 : 143886 : auto_bitmap used_defs;
10551 : :
10552 : 143886 : unsigned int count = mask.encoded_nelts ();
10553 : 143886 : mask.quick_grow (count);
10554 : 143886 : vec_perm_indices indices;
10555 : :
10556 : 772979 : for (unsigned int j = 0; j < nelts_to_build; j++)
10557 : : {
10558 : 635684 : unsigned int iter_num = j / group_size;
10559 : 635684 : unsigned int stmt_num = j % group_size;
10560 : 635684 : unsigned int i = (iter_num * dr_group_size + perm[stmt_num]);
10561 : 635684 : bitmap_set_bit (used_in_lanes, i);
10562 : 635684 : if (repeating_p)
10563 : : {
10564 : : first_vec_index = 0;
10565 : : mask_element = i;
10566 : : }
10567 : : else
10568 : : {
10569 : : /* Enforced before the loop when !repeating_p. */
10570 : 442664 : unsigned int const_nunits = nunits.to_constant ();
10571 : 442664 : vec_index = i / const_nunits;
10572 : 442664 : mask_element = i % const_nunits;
10573 : 442664 : if (vec_index == first_vec_index
10574 : 442664 : || first_vec_index == -1)
10575 : : {
10576 : : first_vec_index = vec_index;
10577 : : }
10578 : 157096 : else if (vec_index == second_vec_index
10579 : 157096 : || second_vec_index == -1)
10580 : : {
10581 : 154557 : second_vec_index = vec_index;
10582 : 154557 : mask_element += const_nunits;
10583 : : }
10584 : : else
10585 : : {
10586 : 2539 : if (dump_p)
10587 : 134 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10588 : : "permutation requires at "
10589 : : "least three vectors %G",
10590 : : stmt_info->stmt);
10591 : 2539 : gcc_assert (analyze_only);
10592 : : return false;
10593 : : }
10594 : :
10595 : 440125 : gcc_assert (mask_element < 2 * const_nunits);
10596 : : }
10597 : :
10598 : 633145 : if (mask_element != index)
10599 : 408500 : noop_p = false;
10600 : 633145 : mask[index++] = mask_element;
10601 : :
10602 : 633145 : if (index == count)
10603 : : {
10604 : 188798 : if (!noop_p)
10605 : : {
10606 : 225489 : indices.new_vector (mask, second_vec_index == -1 ? 1 : 2, nunits);
10607 : 134921 : if (!can_vec_perm_const_p (mode, mode, indices))
10608 : : {
10609 : 4052 : if (dump_p)
10610 : : {
10611 : 74 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10612 : : "unsupported vect permute { ");
10613 : 664 : for (i = 0; i < count; ++i)
10614 : : {
10615 : 590 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
10616 : 590 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
10617 : : }
10618 : 74 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
10619 : : }
10620 : 4052 : gcc_assert (analyze_only);
10621 : : return false;
10622 : : }
10623 : :
10624 : 130869 : tree mask_vec = NULL_TREE;
10625 : 130869 : if (!analyze_only)
10626 : 16240 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
10627 : :
10628 : 130869 : if (second_vec_index == -1)
10629 : 41537 : second_vec_index = first_vec_index;
10630 : :
10631 : 262686 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
10632 : : {
10633 : 131817 : ++*n_perms;
10634 : 131817 : if (analyze_only)
10635 : 115352 : continue;
10636 : : /* Generate the permute statement if necessary. */
10637 : 16465 : tree first_vec = dr_chain[first_vec_index + ri];
10638 : 16465 : tree second_vec = dr_chain[second_vec_index + ri];
10639 : 16465 : gassign *stmt = as_a<gassign *> (stmt_info->stmt);
10640 : 16465 : tree perm_dest
10641 : 16465 : = vect_create_destination_var (gimple_assign_lhs (stmt),
10642 : : vectype);
10643 : 16465 : perm_dest = make_ssa_name (perm_dest);
10644 : 16465 : gimple *perm_stmt
10645 : 16465 : = gimple_build_assign (perm_dest, VEC_PERM_EXPR, first_vec,
10646 : : second_vec, mask_vec);
10647 : 16465 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
10648 : : gsi);
10649 : 16465 : if (dce_chain)
10650 : : {
10651 : 15802 : bitmap_set_bit (used_defs, first_vec_index + ri);
10652 : 15802 : bitmap_set_bit (used_defs, second_vec_index + ri);
10653 : : }
10654 : :
10655 : : /* Store the vector statement in NODE. */
10656 : 16465 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = perm_dest;
10657 : : }
10658 : : }
10659 : 53877 : else if (!analyze_only)
10660 : : {
10661 : 2366 : for (unsigned int ri = 0; ri < nvectors_per_build; ++ri)
10662 : : {
10663 : 1183 : tree first_vec = dr_chain[first_vec_index + ri];
10664 : : /* If mask was NULL_TREE generate the requested
10665 : : identity transform. */
10666 : 1183 : if (dce_chain)
10667 : 1182 : bitmap_set_bit (used_defs, first_vec_index + ri);
10668 : :
10669 : : /* Store the vector statement in NODE. */
10670 : 1183 : SLP_TREE_VEC_DEFS (node)[vect_stmts_counter++] = first_vec;
10671 : : }
10672 : : }
10673 : :
10674 : : index = 0;
10675 : : first_vec_index = -1;
10676 : : second_vec_index = -1;
10677 : : noop_p = true;
10678 : : }
10679 : : }
10680 : :
10681 : 137295 : if (n_loads)
10682 : : {
10683 : 1838 : if (repeating_p)
10684 : 156 : *n_loads = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
10685 : : else
10686 : : {
10687 : : /* Enforced above when !repeating_p. */
10688 : 1682 : unsigned int const_nunits = nunits.to_constant ();
10689 : 1682 : *n_loads = 0;
10690 : 1682 : bool load_seen = false;
10691 : 21047 : for (unsigned i = 0; i < in_nlanes; ++i)
10692 : : {
10693 : 19365 : if (i % const_nunits == 0)
10694 : : {
10695 : 3339 : if (load_seen)
10696 : 1654 : *n_loads += 1;
10697 : : load_seen = false;
10698 : : }
10699 : 19365 : if (bitmap_bit_p (used_in_lanes, i))
10700 : 9708 : load_seen = true;
10701 : : }
10702 : 1682 : if (load_seen)
10703 : 1682 : *n_loads += 1;
10704 : : }
10705 : : }
10706 : :
10707 : 137295 : if (dce_chain)
10708 : 187403 : for (unsigned i = 0; i < dr_chain.length (); ++i)
10709 : 29867 : if (!bitmap_bit_p (used_defs, i))
10710 : : {
10711 : 2842 : tree def = dr_chain[i];
10712 : 2902 : do
10713 : : {
10714 : 2902 : gimple *stmt = SSA_NAME_DEF_STMT (def);
10715 : 2902 : if (is_gimple_assign (stmt)
10716 : 2902 : && (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10717 : 2902 : || gimple_assign_rhs_code (stmt) == CONSTRUCTOR))
10718 : 498 : def = single_ssa_tree_operand (stmt, SSA_OP_USE);
10719 : : else
10720 : : def = NULL;
10721 : 2902 : gimple_stmt_iterator rgsi = gsi_for_stmt (stmt);
10722 : 2902 : gsi_remove (&rgsi, true);
10723 : 2902 : release_defs (stmt);
10724 : : }
10725 : 2902 : while (def);
10726 : : }
10727 : :
10728 : : return true;
10729 : 143886 : }
10730 : :
10731 : : /* Generate vector permute statements from a list of loads in DR_CHAIN.
10732 : : If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
10733 : : permute statements for the SLP node NODE. Store the number of vector
10734 : : permute instructions in *N_PERMS and the number of vector load
10735 : : instructions in *N_LOADS. If DCE_CHAIN is true, remove all definitions
10736 : : that were not needed. */
10737 : :
10738 : : bool
10739 : 104395 : vect_transform_slp_perm_load (vec_info *vinfo,
10740 : : slp_tree node, const vec<tree> &dr_chain,
10741 : : gimple_stmt_iterator *gsi, poly_uint64 vf,
10742 : : bool analyze_only, unsigned *n_perms,
10743 : : unsigned int *n_loads, bool dce_chain)
10744 : : {
10745 : 104395 : return vect_transform_slp_perm_load_1 (vinfo, node,
10746 : 104395 : SLP_TREE_LOAD_PERMUTATION (node),
10747 : : dr_chain, gsi, vf, analyze_only,
10748 : : dump_enabled_p (), n_perms, n_loads,
10749 : 104395 : dce_chain);
10750 : : }
10751 : :
10752 : : /* Produce the next vector result for SLP permutation NODE by adding a vector
10753 : : statement at GSI. If MASK_VEC is nonnull, add:
10754 : :
10755 : : <new SSA name> = VEC_PERM_EXPR <FIRST_DEF, SECOND_DEF, MASK_VEC>
10756 : :
10757 : : otherwise add:
10758 : :
10759 : : <new SSA name> = FIRST_DEF. */
10760 : :
10761 : : static void
10762 : 30255 : vect_add_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
10763 : : slp_tree node, tree first_def, tree second_def,
10764 : : tree mask_vec, poly_uint64 identity_offset)
10765 : : {
10766 : 30255 : tree vectype = SLP_TREE_VECTYPE (node);
10767 : :
10768 : : /* ??? We SLP match existing vector element extracts but
10769 : : allow punning which we need to re-instantiate at uses
10770 : : but have no good way of explicitly representing. */
10771 : 30255 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)), TYPE_SIZE (vectype))
10772 : 30255 : && !types_compatible_p (TREE_TYPE (first_def), vectype))
10773 : : {
10774 : 26 : gassign *conv_stmt
10775 : 26 : = gimple_build_assign (make_ssa_name (vectype),
10776 : : build1 (VIEW_CONVERT_EXPR, vectype, first_def));
10777 : 26 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
10778 : 26 : first_def = gimple_assign_lhs (conv_stmt);
10779 : : }
10780 : 30255 : gassign *perm_stmt;
10781 : 30255 : tree perm_dest = make_ssa_name (vectype);
10782 : 30255 : if (mask_vec)
10783 : : {
10784 : 27232 : if (operand_equal_p (TYPE_SIZE (TREE_TYPE (first_def)),
10785 : 27232 : TYPE_SIZE (vectype))
10786 : 27232 : && !types_compatible_p (TREE_TYPE (second_def), vectype))
10787 : : {
10788 : 9 : gassign *conv_stmt
10789 : 9 : = gimple_build_assign (make_ssa_name (vectype),
10790 : : build1 (VIEW_CONVERT_EXPR,
10791 : : vectype, second_def));
10792 : 9 : vect_finish_stmt_generation (vinfo, NULL, conv_stmt, gsi);
10793 : 9 : second_def = gimple_assign_lhs (conv_stmt);
10794 : : }
10795 : 27232 : perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
10796 : : first_def, second_def,
10797 : : mask_vec);
10798 : : }
10799 : 3023 : else if (!types_compatible_p (TREE_TYPE (first_def), vectype))
10800 : : {
10801 : : /* For identity permutes we still need to handle the case
10802 : : of offsetted extracts or concats. */
10803 : 202 : unsigned HOST_WIDE_INT c;
10804 : 202 : auto first_def_nunits
10805 : 202 : = TYPE_VECTOR_SUBPARTS (TREE_TYPE (first_def));
10806 : 202 : if (known_le (TYPE_VECTOR_SUBPARTS (vectype), first_def_nunits))
10807 : : {
10808 : 198 : unsigned HOST_WIDE_INT elsz
10809 : 198 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (first_def))));
10810 : 396 : tree lowpart = build3 (BIT_FIELD_REF, vectype, first_def,
10811 : 198 : TYPE_SIZE (vectype),
10812 : 198 : bitsize_int (identity_offset * elsz));
10813 : 198 : perm_stmt = gimple_build_assign (perm_dest, lowpart);
10814 : : }
10815 : 4 : else if (constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
10816 : 4 : first_def_nunits, &c) && c == 2)
10817 : : {
10818 : 4 : tree ctor = build_constructor_va (vectype, 2, NULL_TREE, first_def,
10819 : : NULL_TREE, second_def);
10820 : 4 : perm_stmt = gimple_build_assign (perm_dest, ctor);
10821 : : }
10822 : : else
10823 : 0 : gcc_unreachable ();
10824 : : }
10825 : : else
10826 : : {
10827 : : /* We need a copy here in case the def was external. */
10828 : 2821 : perm_stmt = gimple_build_assign (perm_dest, first_def);
10829 : : }
10830 : 30255 : vect_finish_stmt_generation (vinfo, NULL, perm_stmt, gsi);
10831 : : /* Store the vector statement in NODE. */
10832 : 30255 : node->push_vec_def (perm_stmt);
10833 : 30255 : }
10834 : :
10835 : : /* Subroutine of vectorizable_slp_permutation. Check whether the target
10836 : : can perform permutation PERM on the (1 or 2) input nodes in CHILDREN.
10837 : : If GSI is nonnull, emit the permutation there.
10838 : :
10839 : : When GSI is null, the only purpose of NODE is to give properties
10840 : : of the result, such as the vector type and number of SLP lanes.
10841 : : The node does not need to be a VEC_PERM_EXPR.
10842 : :
10843 : : If the target supports the operation, return the number of individual
10844 : : VEC_PERM_EXPRs needed, otherwise return -1. Print information to the
10845 : : dump file if DUMP_P is true. */
10846 : :
10847 : : static int
10848 : 374705 : vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
10849 : : slp_tree node, lane_permutation_t &perm,
10850 : : vec<slp_tree> &children, bool dump_p)
10851 : : {
10852 : 374705 : tree vectype = SLP_TREE_VECTYPE (node);
10853 : :
10854 : : /* ??? We currently only support all same vector input types
10855 : : while the SLP IL should really do a concat + select and thus accept
10856 : : arbitrary mismatches. */
10857 : 374705 : slp_tree child;
10858 : 374705 : unsigned i;
10859 : 374705 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10860 : 374705 : bool repeating_p = multiple_p (nunits, SLP_TREE_LANES (node));
10861 : : /* True if we're permuting a single input of 2N vectors down
10862 : : to N vectors. This case doesn't generalize beyond 2 since
10863 : : VEC_PERM_EXPR only takes 2 inputs. */
10864 : 374705 : bool pack_p = false;
10865 : : /* If we're permuting inputs of N vectors each into X*N outputs,
10866 : : this is the value of X, otherwise it is 1. */
10867 : 374705 : unsigned int unpack_factor = 1;
10868 : 374705 : tree op_vectype = NULL_TREE;
10869 : 375876 : FOR_EACH_VEC_ELT (children, i, child)
10870 : 375804 : if (SLP_TREE_VECTYPE (child))
10871 : : {
10872 : : op_vectype = SLP_TREE_VECTYPE (child);
10873 : : break;
10874 : : }
10875 : 374705 : if (!op_vectype)
10876 : 72 : op_vectype = vectype;
10877 : 803107 : FOR_EACH_VEC_ELT (children, i, child)
10878 : : {
10879 : 428402 : if ((SLP_TREE_DEF_TYPE (child) != vect_internal_def
10880 : 10788 : && !vect_maybe_update_slp_op_vectype (child, op_vectype))
10881 : 428402 : || !types_compatible_p (SLP_TREE_VECTYPE (child), op_vectype)
10882 : 856804 : || !types_compatible_p (TREE_TYPE (vectype), TREE_TYPE (op_vectype)))
10883 : : {
10884 : 0 : if (dump_p)
10885 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10886 : : "Unsupported vector types in lane permutation\n");
10887 : 0 : return -1;
10888 : : }
10889 : 428402 : auto op_nunits = TYPE_VECTOR_SUBPARTS (op_vectype);
10890 : 428402 : unsigned int this_unpack_factor;
10891 : : /* Detect permutations of external, pre-existing vectors. The external
10892 : : node's SLP_TREE_LANES stores the total number of units in the vector,
10893 : : or zero if the vector has variable length.
10894 : :
10895 : : We are expected to keep the original VEC_PERM_EXPR for such cases.
10896 : : There is no repetition to model. */
10897 : 428402 : if (SLP_TREE_DEF_TYPE (child) == vect_external_def
10898 : 428402 : && SLP_TREE_SCALAR_OPS (child).is_empty ())
10899 : : repeating_p = false;
10900 : : /* Check whether the input has twice as many lanes per vector. */
10901 : 419712 : else if (children.length () == 1
10902 : 419712 : && known_eq (SLP_TREE_LANES (child) * nunits,
10903 : : SLP_TREE_LANES (node) * op_nunits * 2))
10904 : : pack_p = true;
10905 : : /* Check whether the output has N times as many lanes per vector. */
10906 : 428402 : else if (constant_multiple_p (SLP_TREE_LANES (node) * op_nunits,
10907 : 379329 : SLP_TREE_LANES (child) * nunits,
10908 : : &this_unpack_factor)
10909 : 346248 : && (i == 0 || unpack_factor == this_unpack_factor))
10910 : : unpack_factor = this_unpack_factor;
10911 : : else
10912 : : repeating_p = false;
10913 : : }
10914 : :
10915 : 749410 : gcc_assert (perm.length () == SLP_TREE_LANES (node));
10916 : :
10917 : : /* Load-lanes permute. This permute only acts as a forwarder to
10918 : : select the correct vector def of the load-lanes load which
10919 : : has the permuted vectors in its vector defs like
10920 : : { v0, w0, r0, v1, w1, r1 ... } for a ld3. All costs are
10921 : : accounted for in the costing for the actual load so we
10922 : : return zero here. */
10923 : 374705 : if (node->ldst_lanes)
10924 : : {
10925 : 0 : gcc_assert (children.length () == 1);
10926 : 0 : if (!gsi)
10927 : : /* This is a trivial op always supported. */
10928 : : return 0;
10929 : 0 : slp_tree child = children[0];
10930 : 0 : unsigned vec_idx = (SLP_TREE_LANE_PERMUTATION (node)[0].second
10931 : 0 : / SLP_TREE_LANES (node));
10932 : 0 : unsigned vec_num = SLP_TREE_LANES (child) / SLP_TREE_LANES (node);
10933 : 0 : for (unsigned i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); ++i)
10934 : : {
10935 : 0 : tree def = SLP_TREE_VEC_DEFS (child)[i * vec_num + vec_idx];
10936 : 0 : node->push_vec_def (def);
10937 : : }
10938 : : return 0;
10939 : : }
10940 : :
10941 : : /* Set REPEATING_P to true if the permutations are cylical wrt UNPACK_FACTOR
10942 : : and if we can generate the vectors in a vector-length agnostic way.
10943 : : This requires UNPACK_STEP == NUNITS / UNPACK_FACTOR to be known at
10944 : : compile time.
10945 : :
10946 : : The significance of UNPACK_STEP is that, when PACK_P is false,
10947 : : output vector I operates on a window of UNPACK_STEP elements from each
10948 : : input, starting at lane UNPACK_STEP * (I % UNPACK_FACTOR). For example,
10949 : : when UNPACK_FACTOR is 2, the first output vector operates on lanes
10950 : : [0, NUNITS / 2 - 1] of each input vector and the second output vector
10951 : : operates on lanes [NUNITS / 2, NUNITS - 1] of each input vector.
10952 : :
10953 : : When REPEATING_P is true, NOUTPUTS holds the total number of outputs
10954 : : that we actually need to generate. */
10955 : 374705 : uint64_t noutputs = 0;
10956 : 374705 : poly_uint64 unpack_step = 0;
10957 : 374705 : loop_vec_info linfo = dyn_cast <loop_vec_info> (vinfo);
10958 : 99817 : if (!linfo
10959 : 411137 : || !multiple_p (nunits, unpack_factor, &unpack_step)
10960 : 98951 : || !constant_multiple_p (LOOP_VINFO_VECT_FACTOR (linfo)
10961 : 98951 : * SLP_TREE_LANES (node), nunits, &noutputs))
10962 : : repeating_p = false;
10963 : :
10964 : : /* We can handle the conditions described for REPEATING_P above for
10965 : : both variable- and constant-length vectors. The fallback requires
10966 : : us to generate every element of every permute vector explicitly,
10967 : : which is only possible for constant-length permute vectors.
10968 : :
10969 : : Set:
10970 : :
10971 : : - NPATTERNS and NELTS_PER_PATTERN to the encoding of the permute
10972 : : mask vectors that we want to build.
10973 : :
10974 : : - NCOPIES to the number of copies of PERM that we need in order
10975 : : to build the necessary permute mask vectors. */
10976 : 98951 : uint64_t npatterns;
10977 : 98951 : unsigned nelts_per_pattern;
10978 : 98951 : uint64_t ncopies;
10979 : 98951 : if (repeating_p)
10980 : : {
10981 : : /* We need permute mask vectors that have the form:
10982 : :
10983 : : { X1, ..., Xn, X1 + n, ..., Xn + n, X1 + 2n, ..., Xn + 2n, ... }
10984 : :
10985 : : In other words, the original n-element permute in PERM is
10986 : : "unrolled" to fill a full vector. The stepped vector encoding
10987 : : that we use for permutes requires 3n elements. */
10988 : 62519 : npatterns = SLP_TREE_LANES (node);
10989 : 62519 : nelts_per_pattern = ncopies = 3;
10990 : : }
10991 : : else
10992 : : {
10993 : : /* Calculate every element of every permute mask vector explicitly,
10994 : : instead of relying on the pattern described above. */
10995 : 312186 : if (!nunits.is_constant (&npatterns)
10996 : 312186 : || !TYPE_VECTOR_SUBPARTS (op_vectype).is_constant ())
10997 : : {
10998 : : if (dump_p)
10999 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11000 : : "unsupported permutation %p on variable-length"
11001 : : " vectors\n", (void *) node);
11002 : : return -1;
11003 : : }
11004 : 312186 : nelts_per_pattern = ncopies = 1;
11005 : 312186 : if (linfo && !LOOP_VINFO_VECT_FACTOR (linfo).is_constant (&ncopies))
11006 : : {
11007 : : if (dump_p)
11008 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11009 : : "unsupported permutation %p for variable VF\n",
11010 : : (void *) node);
11011 : : return -1;
11012 : : }
11013 : : pack_p = false;
11014 : : unpack_factor = 1;
11015 : : }
11016 : 374705 : unsigned olanes = unpack_factor * ncopies * SLP_TREE_LANES (node);
11017 : 374705 : gcc_assert (repeating_p || multiple_p (olanes, nunits));
11018 : :
11019 : : /* Compute the { { SLP operand, vector index}, lane } permutation sequence
11020 : : from the { SLP operand, scalar lane } permutation as recorded in the
11021 : : SLP node as intermediate step. This part should already work
11022 : : with SLP children with arbitrary number of lanes. */
11023 : 374705 : auto_vec<std::pair<std::pair<unsigned, unsigned>, poly_uint64>> vperm;
11024 : 374705 : auto_vec<poly_uint64> active_lane;
11025 : 374705 : vperm.create (olanes);
11026 : 374705 : active_lane.safe_grow_cleared (children.length (), true);
11027 : 756208 : for (unsigned int ui = 0; ui < unpack_factor; ++ui)
11028 : : {
11029 : 1647002 : for (unsigned j = 0; j < children.length (); ++j)
11030 : 441998 : active_lane[j] = ui * unpack_step;
11031 : 998368 : for (unsigned i = 0; i < ncopies; ++i)
11032 : : {
11033 : 3908656 : for (unsigned pi = 0; pi < perm.length (); ++pi)
11034 : : {
11035 : 1337463 : std::pair<unsigned, unsigned> p = perm[pi];
11036 : 1337463 : tree vtype = SLP_TREE_VECTYPE (children[p.first]);
11037 : 1337463 : if (repeating_p)
11038 : 348807 : vperm.quick_push ({{p.first, 0},
11039 : 348807 : p.second + active_lane[p.first]});
11040 : : else
11041 : : {
11042 : : /* We checked above that the vectors are constant-length. */
11043 : 988656 : unsigned vnunits = TYPE_VECTOR_SUBPARTS (vtype)
11044 : 988656 : .to_constant ();
11045 : 988656 : unsigned lane = active_lane[p.first].to_constant ();
11046 : 988656 : unsigned vi = (lane + p.second) / vnunits;
11047 : 988656 : unsigned vl = (lane + p.second) % vnunits;
11048 : 988656 : vperm.quick_push ({{p.first, vi}, vl});
11049 : : }
11050 : : }
11051 : : /* Advance to the next group. */
11052 : 1336544 : for (unsigned j = 0; j < children.length (); ++j)
11053 : 719679 : active_lane[j] += SLP_TREE_LANES (children[j]);
11054 : : }
11055 : : }
11056 : :
11057 : 374705 : if (dump_p)
11058 : : {
11059 : 8225 : dump_printf_loc (MSG_NOTE, vect_location,
11060 : : "vectorizing permutation %p", (void *)node);
11061 : 29707 : for (unsigned i = 0; i < perm.length (); ++i)
11062 : 21482 : dump_printf (MSG_NOTE, " op%u[%u]", perm[i].first, perm[i].second);
11063 : 8225 : if (repeating_p)
11064 : 6996 : dump_printf (MSG_NOTE, " (repeat %d)", SLP_TREE_LANES (node));
11065 : 8225 : dump_printf (MSG_NOTE, "\n");
11066 : 8225 : dump_printf_loc (MSG_NOTE, vect_location, "as");
11067 : 82334 : for (unsigned i = 0; i < vperm.length (); ++i)
11068 : : {
11069 : 74109 : if (i != 0
11070 : 74109 : && (repeating_p
11071 : 51219 : ? multiple_p (i, npatterns)
11072 : 54756 : : multiple_p (i, TYPE_VECTOR_SUBPARTS (vectype))))
11073 : 22379 : dump_printf (MSG_NOTE, ",");
11074 : 74109 : dump_printf (MSG_NOTE, " vops%u[%u][",
11075 : 74109 : vperm[i].first.first, vperm[i].first.second);
11076 : 74109 : dump_dec (MSG_NOTE, vperm[i].second);
11077 : 74109 : dump_printf (MSG_NOTE, "]");
11078 : : }
11079 : 8225 : dump_printf (MSG_NOTE, "\n");
11080 : : }
11081 : :
11082 : : /* We can only handle two-vector permutes, everything else should
11083 : : be lowered on the SLP level. The following is closely inspired
11084 : : by vect_transform_slp_perm_load and is supposed to eventually
11085 : : replace it.
11086 : : ??? As intermediate step do code-gen in the SLP tree representation
11087 : : somehow? */
11088 : 374705 : std::pair<unsigned, unsigned> first_vec = std::make_pair (-1U, -1U);
11089 : 374705 : std::pair<unsigned, unsigned> second_vec = std::make_pair (-1U, -1U);
11090 : 374705 : unsigned int index = 0;
11091 : 374705 : poly_uint64 mask_element;
11092 : 374705 : vec_perm_builder mask;
11093 : 374705 : mask.new_vector (nunits, npatterns, nelts_per_pattern);
11094 : 374705 : unsigned int count = mask.encoded_nelts ();
11095 : 374705 : mask.quick_grow (count);
11096 : 374705 : vec_perm_indices indices;
11097 : 374705 : unsigned nperms = 0;
11098 : : /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
11099 : : vectors to check during analysis, but we need to generate NOUTPUTS
11100 : : vectors during transformation. */
11101 : 374705 : unsigned total_nelts = olanes;
11102 : 374705 : unsigned process_nelts = olanes;
11103 : 374705 : if (repeating_p)
11104 : : {
11105 : 62519 : total_nelts = (total_nelts / unpack_factor) * noutputs;
11106 : 62519 : if (gsi)
11107 : 9605 : process_nelts = total_nelts;
11108 : : }
11109 : 374705 : unsigned last_ei = (total_nelts - 1) % process_nelts;
11110 : 1721207 : for (unsigned i = 0; i < process_nelts; ++i)
11111 : : {
11112 : : /* VI is the input vector index when generating code for REPEATING_P. */
11113 : 1354609 : unsigned vi = i / olanes * (pack_p ? 2 : 1);
11114 : 1354609 : unsigned ei = i % olanes;
11115 : 1354609 : mask_element = vperm[ei].second;
11116 : 1354609 : if (pack_p)
11117 : : {
11118 : : /* In this case, we have N outputs and the single child provides 2N
11119 : : inputs. Output X permutes inputs 2X and 2X+1.
11120 : :
11121 : : The mask indices are taken directly from the SLP permutation node.
11122 : : Index X selects from the first vector if (X / NUNITS) % 2 == 0;
11123 : : X selects from the second vector otherwise. These conditions
11124 : : are only known at compile time for constant-length vectors. */
11125 : : first_vec = std::make_pair (0, 0);
11126 : : second_vec = std::make_pair (0, 1);
11127 : : }
11128 : 1198465 : else if (first_vec.first == -1U
11129 : 1198465 : || first_vec == vperm[ei].first)
11130 : 1023917 : first_vec = vperm[ei].first;
11131 : 174548 : else if (second_vec.first == -1U
11132 : 174548 : || second_vec == vperm[ei].first)
11133 : : {
11134 : 174162 : second_vec = vperm[ei].first;
11135 : 174162 : mask_element += nunits;
11136 : : }
11137 : : else
11138 : : {
11139 : 386 : if (dump_p)
11140 : 19 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11141 : : "permutation requires at "
11142 : : "least three vectors\n");
11143 : 386 : gcc_assert (!gsi);
11144 : : return -1;
11145 : : }
11146 : :
11147 : 1354223 : mask[index++] = mask_element;
11148 : :
11149 : 1354223 : if (index == count)
11150 : : {
11151 : 649935 : indices.new_vector (mask, second_vec.first == -1U ? 1 : 2,
11152 : : TYPE_VECTOR_SUBPARTS (op_vectype));
11153 : 515462 : bool identity_p = (indices.series_p (0, 1, mask[0], 1)
11154 : 798038 : && constant_multiple_p (mask[0], nunits));
11155 : 515462 : machine_mode vmode = TYPE_MODE (vectype);
11156 : 515462 : machine_mode op_vmode = TYPE_MODE (op_vectype);
11157 : 515462 : unsigned HOST_WIDE_INT c;
11158 : 515462 : if ((!identity_p
11159 : 467412 : && !can_vec_perm_const_p (vmode, op_vmode, indices))
11160 : 515462 : || (identity_p
11161 : 48050 : && !known_le (nunits,
11162 : : TYPE_VECTOR_SUBPARTS (op_vectype))
11163 : 7729 : && (!constant_multiple_p (nunits,
11164 : 8 : TYPE_VECTOR_SUBPARTS (op_vectype),
11165 : 8 : &c) || c != 2)))
11166 : : {
11167 : 7721 : if (dump_p)
11168 : : {
11169 : 147 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
11170 : : vect_location,
11171 : : "unsupported vect permute { ");
11172 : 1501 : for (i = 0; i < count; ++i)
11173 : : {
11174 : 1354 : dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
11175 : 1354 : dump_printf (MSG_MISSED_OPTIMIZATION, " ");
11176 : : }
11177 : 147 : dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
11178 : : }
11179 : 7721 : gcc_assert (!gsi);
11180 : 8107 : return -1;
11181 : : }
11182 : :
11183 : 507741 : if (!identity_p)
11184 : 459691 : nperms += CEIL (total_nelts, process_nelts) - (ei > last_ei);
11185 : 507741 : if (gsi)
11186 : : {
11187 : 30255 : if (second_vec.first == -1U)
11188 : 6726 : second_vec = first_vec;
11189 : :
11190 : 30255 : slp_tree
11191 : 30255 : first_node = children[first_vec.first],
11192 : 30255 : second_node = children[second_vec.first];
11193 : :
11194 : 30255 : tree mask_vec = NULL_TREE;
11195 : 30255 : if (!identity_p)
11196 : 27232 : mask_vec = vect_gen_perm_mask_checked (vectype, indices);
11197 : :
11198 : 30255 : tree first_def
11199 : 30255 : = vect_get_slp_vect_def (first_node, first_vec.second + vi);
11200 : 30255 : tree second_def
11201 : 30255 : = vect_get_slp_vect_def (second_node, second_vec.second + vi);
11202 : 30255 : vect_add_slp_permutation (vinfo, gsi, node, first_def,
11203 : 30255 : second_def, mask_vec, mask[0]);
11204 : : }
11205 : :
11206 : : index = 0;
11207 : : first_vec = std::make_pair (-1U, -1U);
11208 : : second_vec = std::make_pair (-1U, -1U);
11209 : : }
11210 : : }
11211 : :
11212 : 366598 : return nperms;
11213 : 374705 : }
11214 : :
11215 : : /* Vectorize the SLP permutations in NODE as specified
11216 : : in SLP_TREE_LANE_PERMUTATION which is a vector of pairs of SLP
11217 : : child number and lane number.
11218 : : Interleaving of two two-lane two-child SLP subtrees (not supported):
11219 : : [ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } ]
11220 : : A blend of two four-lane two-child SLP subtrees:
11221 : : [ { 0, 0 }, { 1, 1 }, { 0, 2 }, { 1, 3 } ]
11222 : : Highpart of a four-lane one-child SLP subtree (not supported):
11223 : : [ { 0, 2 }, { 0, 3 } ]
11224 : : Where currently only a subset is supported by code generating below. */
11225 : :
11226 : : static bool
11227 : 111742 : vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
11228 : : slp_tree node, stmt_vector_for_cost *cost_vec)
11229 : : {
11230 : 111742 : tree vectype = SLP_TREE_VECTYPE (node);
11231 : 111742 : lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
11232 : 111742 : int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
11233 : 111742 : SLP_TREE_CHILDREN (node),
11234 : : dump_enabled_p ());
11235 : 111742 : if (nperms < 0)
11236 : : return false;
11237 : :
11238 : 110516 : if (!gsi)
11239 : 94700 : record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
11240 : :
11241 : : return true;
11242 : : }
11243 : :
11244 : : /* Vectorize SLP NODE. */
11245 : :
11246 : : static void
11247 : 1427054 : vect_schedule_slp_node (vec_info *vinfo,
11248 : : slp_tree node, slp_instance instance)
11249 : : {
11250 : 1427054 : gimple_stmt_iterator si;
11251 : 1427054 : int i;
11252 : 1427054 : slp_tree child;
11253 : :
11254 : : /* Vectorize externals and constants. */
11255 : 1427054 : if (SLP_TREE_DEF_TYPE (node) == vect_constant_def
11256 : 1427054 : || SLP_TREE_DEF_TYPE (node) == vect_external_def)
11257 : : {
11258 : : /* ??? vectorizable_shift can end up using a scalar operand which is
11259 : : currently denoted as !SLP_TREE_VECTYPE. No need to vectorize the
11260 : : node in this case. */
11261 : 491072 : if (!SLP_TREE_VECTYPE (node))
11262 : 491072 : return;
11263 : :
11264 : : /* There are two reasons vector defs might already exist. The first
11265 : : is that we are vectorizing an existing vector def. The second is
11266 : : when performing BB vectorization shared constant/external nodes
11267 : : are not split apart during partitioning so during the code-gen
11268 : : DFS walk we can end up visiting them twice. */
11269 : 485483 : if (! SLP_TREE_VEC_DEFS (node).exists ())
11270 : 484788 : vect_create_constant_vectors (vinfo, node);
11271 : 485483 : return;
11272 : : }
11273 : :
11274 : 935982 : gcc_assert (SLP_TREE_VEC_DEFS (node).is_empty ());
11275 : :
11276 : 935982 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
11277 : :
11278 : 935982 : gcc_assert (SLP_TREE_NUMBER_OF_VEC_STMTS (node) != 0);
11279 : 935982 : SLP_TREE_VEC_DEFS (node).create (SLP_TREE_NUMBER_OF_VEC_STMTS (node));
11280 : :
11281 : 935982 : if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
11282 : 920166 : && STMT_VINFO_DATA_REF (stmt_info))
11283 : : {
11284 : : /* Vectorized loads go before the first scalar load to make it
11285 : : ready early, vectorized stores go before the last scalar
11286 : : stmt which is where all uses are ready. */
11287 : 697543 : stmt_vec_info last_stmt_info = NULL;
11288 : 697543 : if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
11289 : 156663 : last_stmt_info = vect_find_first_scalar_stmt_in_slp (node);
11290 : : else /* DR_IS_WRITE */
11291 : 540880 : last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
11292 : 697543 : si = gsi_for_stmt (last_stmt_info->stmt);
11293 : 697543 : }
11294 : 238439 : else if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
11295 : 222623 : && (STMT_VINFO_TYPE (stmt_info) == cycle_phi_info_type
11296 : : || STMT_VINFO_TYPE (stmt_info) == induc_vec_info_type
11297 : : || STMT_VINFO_TYPE (stmt_info) == phi_info_type))
11298 : : {
11299 : : /* For PHI node vectorization we do not use the insertion iterator. */
11300 : 51726 : si = gsi_none ();
11301 : : }
11302 : : else
11303 : : {
11304 : : /* Emit other stmts after the children vectorized defs which is
11305 : : earliest possible. */
11306 : : gimple *last_stmt = NULL;
11307 : : bool seen_vector_def = false;
11308 : 518109 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11309 : 331396 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
11310 : : {
11311 : : /* For fold-left reductions we are retaining the scalar
11312 : : reduction PHI but we still have SLP_TREE_NUM_VEC_STMTS
11313 : : set so the representation isn't perfect. Resort to the
11314 : : last scalar def here. */
11315 : 266875 : if (SLP_TREE_VEC_DEFS (child).is_empty ())
11316 : : {
11317 : 841 : gcc_assert (STMT_VINFO_TYPE (SLP_TREE_REPRESENTATIVE (child))
11318 : : == cycle_phi_info_type);
11319 : 841 : gphi *phi = as_a <gphi *>
11320 : 841 : (vect_find_last_scalar_stmt_in_slp (child)->stmt);
11321 : 841 : if (!last_stmt)
11322 : : last_stmt = phi;
11323 : 616 : else if (vect_stmt_dominates_stmt_p (last_stmt, phi))
11324 : : last_stmt = phi;
11325 : 605 : else if (vect_stmt_dominates_stmt_p (phi, last_stmt))
11326 : : ;
11327 : : else
11328 : 0 : gcc_unreachable ();
11329 : : }
11330 : : /* We are emitting all vectorized stmts in the same place and
11331 : : the last one is the last.
11332 : : ??? Unless we have a load permutation applied and that
11333 : : figures to re-use an earlier generated load. */
11334 : : unsigned j;
11335 : : tree vdef;
11336 : 618086 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11337 : : {
11338 : 351211 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11339 : 351211 : if (!last_stmt)
11340 : : last_stmt = vstmt;
11341 : 174083 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
11342 : : last_stmt = vstmt;
11343 : 42953 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
11344 : : ;
11345 : : else
11346 : 0 : gcc_unreachable ();
11347 : : }
11348 : : }
11349 : 64521 : else if (!SLP_TREE_VECTYPE (child))
11350 : : {
11351 : : /* For externals we use unvectorized at all scalar defs. */
11352 : : unsigned j;
11353 : : tree def;
11354 : 11793 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_OPS (child), j, def)
11355 : 6778 : if (TREE_CODE (def) == SSA_NAME
11356 : 6778 : && !SSA_NAME_IS_DEFAULT_DEF (def))
11357 : : {
11358 : 165 : gimple *stmt = SSA_NAME_DEF_STMT (def);
11359 : 165 : if (!last_stmt)
11360 : : last_stmt = stmt;
11361 : 156 : else if (vect_stmt_dominates_stmt_p (last_stmt, stmt))
11362 : : last_stmt = stmt;
11363 : 153 : else if (vect_stmt_dominates_stmt_p (stmt, last_stmt))
11364 : : ;
11365 : : else
11366 : 0 : gcc_unreachable ();
11367 : : }
11368 : : }
11369 : : else
11370 : : {
11371 : : /* For externals we have to look at all defs since their
11372 : : insertion place is decided per vector. But beware
11373 : : of pre-existing vectors where we need to make sure
11374 : : we do not insert before the region boundary. */
11375 : 59506 : if (SLP_TREE_SCALAR_OPS (child).is_empty ()
11376 : 575 : && !vinfo->lookup_def (SLP_TREE_VEC_DEFS (child)[0]))
11377 : : seen_vector_def = true;
11378 : : else
11379 : : {
11380 : : unsigned j;
11381 : : tree vdef;
11382 : 469384 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (child), j, vdef)
11383 : 78597 : if (TREE_CODE (vdef) == SSA_NAME
11384 : 78597 : && !SSA_NAME_IS_DEFAULT_DEF (vdef))
11385 : : {
11386 : 16570 : gimple *vstmt = SSA_NAME_DEF_STMT (vdef);
11387 : 16570 : if (!last_stmt)
11388 : : last_stmt = vstmt;
11389 : 8930 : else if (vect_stmt_dominates_stmt_p (last_stmt, vstmt))
11390 : : last_stmt = vstmt;
11391 : 6781 : else if (vect_stmt_dominates_stmt_p (vstmt, last_stmt))
11392 : : ;
11393 : : else
11394 : 0 : gcc_unreachable ();
11395 : : }
11396 : : }
11397 : : }
11398 : : /* This can happen when all children are pre-existing vectors or
11399 : : constants. */
11400 : 186713 : if (!last_stmt)
11401 : 1711 : last_stmt = vect_find_first_scalar_stmt_in_slp (node)->stmt;
11402 : 1711 : if (!last_stmt)
11403 : : {
11404 : 0 : gcc_assert (seen_vector_def);
11405 : 0 : si = gsi_after_labels (vinfo->bbs[0]);
11406 : : }
11407 : 186713 : else if (is_ctrl_altering_stmt (last_stmt))
11408 : : {
11409 : : /* We split regions to vectorize at control altering stmts
11410 : : with a definition so this must be an external which
11411 : : we can insert at the start of the region. */
11412 : 3 : si = gsi_after_labels (vinfo->bbs[0]);
11413 : : }
11414 : 186710 : else if (is_a <bb_vec_info> (vinfo)
11415 : 12895 : && SLP_TREE_CODE (node) != VEC_PERM_EXPR
11416 : 11743 : && gimple_bb (last_stmt) != gimple_bb (stmt_info->stmt)
11417 : 187913 : && gimple_could_trap_p (stmt_info->stmt))
11418 : : {
11419 : : /* We've constrained possibly trapping operations to all come
11420 : : from the same basic-block, if vectorized defs would allow earlier
11421 : : scheduling still force vectorized stmts to the original block.
11422 : : This is only necessary for BB vectorization since for loop vect
11423 : : all operations are in a single BB and scalar stmt based
11424 : : placement doesn't play well with epilogue vectorization. */
11425 : 51 : gcc_assert (dominated_by_p (CDI_DOMINATORS,
11426 : : gimple_bb (stmt_info->stmt),
11427 : : gimple_bb (last_stmt)));
11428 : 51 : si = gsi_after_labels (gimple_bb (stmt_info->stmt));
11429 : : }
11430 : 186659 : else if (is_a <gphi *> (last_stmt))
11431 : 14299 : si = gsi_after_labels (gimple_bb (last_stmt));
11432 : : else
11433 : : {
11434 : 172360 : si = gsi_for_stmt (last_stmt);
11435 : 172360 : gsi_next (&si);
11436 : :
11437 : : /* Avoid scheduling internal defs outside of the loop when
11438 : : we might have only implicitly tracked loop mask/len defs. */
11439 : 172360 : if (auto loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
11440 : 43 : if (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
11441 : 159717 : || LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
11442 : : {
11443 : 43 : gimple_stmt_iterator si2
11444 : 43 : = gsi_after_labels (LOOP_VINFO_LOOP (loop_vinfo)->header);
11445 : 43 : if ((gsi_end_p (si2)
11446 : 0 : && (LOOP_VINFO_LOOP (loop_vinfo)->header
11447 : 0 : != gimple_bb (last_stmt))
11448 : 0 : && dominated_by_p (CDI_DOMINATORS,
11449 : : LOOP_VINFO_LOOP (loop_vinfo)->header,
11450 : 0 : gimple_bb (last_stmt)))
11451 : 43 : || (!gsi_end_p (si2)
11452 : 43 : && last_stmt != *si2
11453 : 42 : && vect_stmt_dominates_stmt_p (last_stmt, *si2)))
11454 : 1 : si = si2;
11455 : : }
11456 : : }
11457 : : }
11458 : :
11459 : : /* Handle purely internal nodes. */
11460 : 935982 : if (SLP_TREE_CODE (node) == VEC_PERM_EXPR)
11461 : : {
11462 : 15816 : if (dump_enabled_p ())
11463 : 3129 : dump_printf_loc (MSG_NOTE, vect_location,
11464 : : "------>vectorizing SLP permutation node\n");
11465 : : /* ??? the transform kind is stored to STMT_VINFO_TYPE which might
11466 : : be shared with different SLP nodes (but usually it's the same
11467 : : operation apart from the case the stmt is only there for denoting
11468 : : the actual scalar lane defs ...). So do not call vect_transform_stmt
11469 : : but open-code it here (partly). */
11470 : 15816 : bool done = vectorizable_slp_permutation (vinfo, &si, node, NULL);
11471 : 15816 : gcc_assert (done);
11472 : : stmt_vec_info slp_stmt_info;
11473 : : unsigned int i;
11474 : 972486 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, slp_stmt_info)
11475 : 23771 : if (slp_stmt_info && STMT_VINFO_LIVE_P (slp_stmt_info))
11476 : : {
11477 : 652 : done = vectorizable_live_operation (vinfo, slp_stmt_info, node,
11478 : : instance, i, true, NULL);
11479 : 652 : gcc_assert (done);
11480 : : }
11481 : : }
11482 : : else
11483 : : {
11484 : 920166 : if (dump_enabled_p ())
11485 : 63809 : dump_printf_loc (MSG_NOTE, vect_location,
11486 : : "------>vectorizing SLP node starting from: %G",
11487 : : stmt_info->stmt);
11488 : 920166 : vect_transform_stmt (vinfo, stmt_info, &si, node, instance);
11489 : : }
11490 : : }
11491 : :
11492 : : /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
11493 : : For loop vectorization this is done in vectorizable_call, but for SLP
11494 : : it needs to be deferred until end of vect_schedule_slp, because multiple
11495 : : SLP instances may refer to the same scalar stmt. */
11496 : :
11497 : : static void
11498 : 564268 : vect_remove_slp_scalar_calls (vec_info *vinfo,
11499 : : slp_tree node, hash_set<slp_tree> &visited)
11500 : : {
11501 : 564268 : gimple *new_stmt;
11502 : 564268 : gimple_stmt_iterator gsi;
11503 : 564268 : int i;
11504 : 564268 : slp_tree child;
11505 : 564268 : tree lhs;
11506 : 564268 : stmt_vec_info stmt_info;
11507 : :
11508 : 564268 : if (!node || SLP_TREE_DEF_TYPE (node) != vect_internal_def)
11509 : 176767 : return;
11510 : :
11511 : 428743 : if (visited.add (node))
11512 : : return;
11513 : :
11514 : 864799 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11515 : 477298 : vect_remove_slp_scalar_calls (vinfo, child, visited);
11516 : :
11517 : 1221830 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
11518 : : {
11519 : 450885 : if (!stmt_info)
11520 : 3723 : continue;
11521 : 447162 : if (!PURE_SLP_STMT (stmt_info))
11522 : 0 : continue;
11523 : 447162 : stmt_info = vect_orig_stmt (stmt_info);
11524 : 447162 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
11525 : 4664 : if (!stmt || gimple_bb (stmt) == NULL)
11526 : 442506 : continue;
11527 : 4656 : lhs = gimple_call_lhs (stmt);
11528 : 4656 : if (lhs)
11529 : 4169 : new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
11530 : : else
11531 : : {
11532 : 487 : new_stmt = gimple_build_nop ();
11533 : 487 : unlink_stmt_vdef (stmt_info->stmt);
11534 : : }
11535 : 4656 : gsi = gsi_for_stmt (stmt);
11536 : 4656 : vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
11537 : 4656 : if (lhs)
11538 : 4169 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
11539 : : }
11540 : : }
11541 : :
11542 : : static void
11543 : 86970 : vect_remove_slp_scalar_calls (vec_info *vinfo, slp_tree node)
11544 : : {
11545 : 86970 : hash_set<slp_tree> visited;
11546 : 86970 : vect_remove_slp_scalar_calls (vinfo, node, visited);
11547 : 86970 : }
11548 : :
11549 : : /* Vectorize the instance root. */
11550 : :
11551 : : void
11552 : 9842 : vectorize_slp_instance_root_stmt (vec_info *vinfo, slp_tree node, slp_instance instance)
11553 : : {
11554 : 9842 : gassign *rstmt = NULL;
11555 : :
11556 : 9842 : if (instance->kind == slp_inst_kind_ctor)
11557 : : {
11558 : 4118 : if (SLP_TREE_NUMBER_OF_VEC_STMTS (node) == 1)
11559 : : {
11560 : 4112 : tree vect_lhs = SLP_TREE_VEC_DEFS (node)[0];
11561 : 4112 : tree root_lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
11562 : 4112 : if (!useless_type_conversion_p (TREE_TYPE (root_lhs),
11563 : 4112 : TREE_TYPE (vect_lhs)))
11564 : 0 : vect_lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (root_lhs),
11565 : : vect_lhs);
11566 : 4112 : rstmt = gimple_build_assign (root_lhs, vect_lhs);
11567 : : }
11568 : 6 : else if (SLP_TREE_NUMBER_OF_VEC_STMTS (node) > 1)
11569 : : {
11570 : 6 : int nelts = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
11571 : 6 : tree child_def;
11572 : 6 : int j;
11573 : 6 : vec<constructor_elt, va_gc> *v;
11574 : 6 : vec_alloc (v, nelts);
11575 : :
11576 : : /* A CTOR can handle V16HI composition from VNx8HI so we
11577 : : do not need to convert vector elements if the types
11578 : : do not match. */
11579 : 18 : FOR_EACH_VEC_ELT (SLP_TREE_VEC_DEFS (node), j, child_def)
11580 : 12 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, child_def);
11581 : 6 : tree lhs = gimple_get_lhs (instance->root_stmts[0]->stmt);
11582 : 6 : tree rtype
11583 : 6 : = TREE_TYPE (gimple_assign_rhs1 (instance->root_stmts[0]->stmt));
11584 : 6 : tree r_constructor = build_constructor (rtype, v);
11585 : 6 : rstmt = gimple_build_assign (lhs, r_constructor);
11586 : : }
11587 : : }
11588 : 5724 : else if (instance->kind == slp_inst_kind_bb_reduc)
11589 : : {
11590 : : /* Largely inspired by reduction chain epilogue handling in
11591 : : vect_create_epilog_for_reduction. */
11592 : 4111 : vec<tree> vec_defs = vNULL;
11593 : 4111 : vect_get_slp_defs (node, &vec_defs);
11594 : 4111 : enum tree_code reduc_code
11595 : 4111 : = gimple_assign_rhs_code (instance->root_stmts[0]->stmt);
11596 : : /* ??? We actually have to reflect signs somewhere. */
11597 : 4111 : if (reduc_code == MINUS_EXPR)
11598 : 0 : reduc_code = PLUS_EXPR;
11599 : 4111 : gimple_seq epilogue = NULL;
11600 : : /* We may end up with more than one vector result, reduce them
11601 : : to one vector. */
11602 : 4111 : tree vec_def = vec_defs[0];
11603 : 4111 : tree vectype = TREE_TYPE (vec_def);
11604 : 4111 : tree compute_vectype = vectype;
11605 : 4111 : bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
11606 : 4042 : && TYPE_OVERFLOW_UNDEFINED (vectype)
11607 : 7034 : && operation_can_overflow (reduc_code));
11608 : 2798 : if (pun_for_overflow_p)
11609 : : {
11610 : 2798 : compute_vectype = unsigned_type_for (vectype);
11611 : 2798 : vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
11612 : : compute_vectype, vec_def);
11613 : : }
11614 : 6480 : for (unsigned i = 1; i < vec_defs.length (); ++i)
11615 : : {
11616 : 2369 : tree def = vec_defs[i];
11617 : 2369 : if (pun_for_overflow_p)
11618 : 2273 : def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
11619 : : compute_vectype, def);
11620 : 2369 : vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
11621 : : vec_def, def);
11622 : : }
11623 : 4111 : vec_defs.release ();
11624 : : /* ??? Support other schemes than direct internal fn. */
11625 : 4111 : internal_fn reduc_fn;
11626 : 4111 : if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
11627 : 4111 : || reduc_fn == IFN_LAST)
11628 : 0 : gcc_unreachable ();
11629 : 4111 : tree scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
11630 : 4111 : TREE_TYPE (compute_vectype), vec_def);
11631 : 4111 : if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
11632 : : {
11633 : 2653 : tree rem_def = NULL_TREE;
11634 : 12083 : for (auto def : SLP_INSTANCE_REMAIN_DEFS (instance))
11635 : : {
11636 : 9430 : def = gimple_convert (&epilogue, TREE_TYPE (scalar_def), def);
11637 : 9430 : if (!rem_def)
11638 : : rem_def = def;
11639 : : else
11640 : 6777 : rem_def = gimple_build (&epilogue, reduc_code,
11641 : 6777 : TREE_TYPE (scalar_def),
11642 : : rem_def, def);
11643 : : }
11644 : 2653 : scalar_def = gimple_build (&epilogue, reduc_code,
11645 : 2653 : TREE_TYPE (scalar_def),
11646 : : scalar_def, rem_def);
11647 : : }
11648 : 4111 : scalar_def = gimple_convert (&epilogue,
11649 : 4111 : TREE_TYPE (vectype), scalar_def);
11650 : 4111 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
11651 : 4111 : gsi_insert_seq_before (&rgsi, epilogue, GSI_SAME_STMT);
11652 : 4111 : gimple_assign_set_rhs_from_tree (&rgsi, scalar_def);
11653 : 4111 : update_stmt (gsi_stmt (rgsi));
11654 : 4111 : return;
11655 : : }
11656 : 1613 : else if (instance->kind == slp_inst_kind_gcond)
11657 : : {
11658 : : /* Only support a single root for now as we can't codegen CFG yet and so we
11659 : : can't support lane > 1 at this time. */
11660 : 1613 : gcc_assert (instance->root_stmts.length () == 1);
11661 : 1613 : auto root_stmt_info = instance->root_stmts[0];
11662 : 1613 : auto last_stmt = STMT_VINFO_STMT (vect_orig_stmt (root_stmt_info));
11663 : 1613 : gimple_stmt_iterator rgsi = gsi_for_stmt (last_stmt);
11664 : 1613 : gimple *vec_stmt = NULL;
11665 : 1613 : gcc_assert (!SLP_TREE_VEC_DEFS (node).is_empty ());
11666 : 1613 : bool res = vectorizable_early_exit (vinfo, root_stmt_info, &rgsi,
11667 : : &vec_stmt, node, NULL);
11668 : 1613 : gcc_assert (res);
11669 : 1613 : return;
11670 : : }
11671 : : else
11672 : 0 : gcc_unreachable ();
11673 : :
11674 : 4118 : gcc_assert (rstmt);
11675 : :
11676 : 4118 : gimple_stmt_iterator rgsi = gsi_for_stmt (instance->root_stmts[0]->stmt);
11677 : 4118 : gsi_replace (&rgsi, rstmt, true);
11678 : : }
11679 : :
11680 : : struct slp_scc_info
11681 : : {
11682 : : bool on_stack;
11683 : : int dfs;
11684 : : int lowlink;
11685 : : };
11686 : :
11687 : : /* Schedule the SLP INSTANCE doing a DFS walk and collecting SCCs. */
11688 : :
11689 : : static void
11690 : 1427054 : vect_schedule_scc (vec_info *vinfo, slp_tree node, slp_instance instance,
11691 : : hash_map<slp_tree, slp_scc_info> &scc_info,
11692 : : int &maxdfs, vec<slp_tree> &stack)
11693 : : {
11694 : 1427054 : bool existed_p;
11695 : 1427054 : slp_scc_info *info = &scc_info.get_or_insert (node, &existed_p);
11696 : 1427054 : gcc_assert (!existed_p);
11697 : 1427054 : info->dfs = maxdfs;
11698 : 1427054 : info->lowlink = maxdfs;
11699 : 1427054 : maxdfs++;
11700 : :
11701 : : /* Leaf. */
11702 : 1427054 : if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
11703 : : {
11704 : 491072 : info->on_stack = false;
11705 : 491072 : vect_schedule_slp_node (vinfo, node, instance);
11706 : 1010334 : return;
11707 : : }
11708 : :
11709 : 935982 : info->on_stack = true;
11710 : 935982 : stack.safe_push (node);
11711 : :
11712 : 935982 : unsigned i;
11713 : 935982 : slp_tree child;
11714 : : /* DFS recurse. */
11715 : 1919853 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
11716 : : {
11717 : 983871 : if (!child)
11718 : 53822 : continue;
11719 : 930049 : slp_scc_info *child_info = scc_info.get (child);
11720 : 930049 : if (!child_info)
11721 : : {
11722 : 849658 : vect_schedule_scc (vinfo, child, instance, scc_info, maxdfs, stack);
11723 : : /* Recursion might have re-allocated the node. */
11724 : 849658 : info = scc_info.get (node);
11725 : 849658 : child_info = scc_info.get (child);
11726 : 849658 : info->lowlink = MIN (info->lowlink, child_info->lowlink);
11727 : : }
11728 : 80391 : else if (child_info->on_stack)
11729 : 24046 : info->lowlink = MIN (info->lowlink, child_info->dfs);
11730 : : }
11731 : 935982 : if (info->lowlink != info->dfs)
11732 : : return;
11733 : :
11734 : 907792 : auto_vec<slp_tree, 4> phis_to_fixup;
11735 : :
11736 : : /* Singleton. */
11737 : 907792 : if (stack.last () == node)
11738 : : {
11739 : 885076 : stack.pop ();
11740 : 885076 : info->on_stack = false;
11741 : 885076 : vect_schedule_slp_node (vinfo, node, instance);
11742 : 885076 : if (SLP_TREE_CODE (node) != VEC_PERM_EXPR
11743 : 885076 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (node)->stmt))
11744 : 29026 : phis_to_fixup.quick_push (node);
11745 : : }
11746 : : else
11747 : : {
11748 : : /* SCC. */
11749 : 22716 : int last_idx = stack.length () - 1;
11750 : 50906 : while (stack[last_idx] != node)
11751 : 28190 : last_idx--;
11752 : : /* We can break the cycle at PHIs who have at least one child
11753 : : code generated. Then we could re-start the DFS walk until
11754 : : all nodes in the SCC are covered (we might have new entries
11755 : : for only back-reachable nodes). But it's simpler to just
11756 : : iterate and schedule those that are ready. */
11757 : 22716 : unsigned todo = stack.length () - last_idx;
11758 : 23023 : do
11759 : : {
11760 : 98634 : for (int idx = stack.length () - 1; idx >= last_idx; --idx)
11761 : : {
11762 : 52588 : slp_tree entry = stack[idx];
11763 : 52588 : if (!entry)
11764 : 911 : continue;
11765 : 51677 : bool phi = (SLP_TREE_CODE (entry) != VEC_PERM_EXPR
11766 : 51677 : && is_a <gphi *> (SLP_TREE_REPRESENTATIVE (entry)->stmt));
11767 : 51677 : bool ready = !phi;
11768 : 129357 : FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (entry), i, child)
11769 : 101565 : if (!child)
11770 : : {
11771 : 21964 : gcc_assert (phi);
11772 : : ready = true;
11773 : : break;
11774 : : }
11775 : 79601 : else if (scc_info.get (child)->on_stack)
11776 : : {
11777 : 22937 : if (!phi)
11778 : : {
11779 : : ready = false;
11780 : : break;
11781 : : }
11782 : : }
11783 : : else
11784 : : {
11785 : 56664 : if (phi)
11786 : : {
11787 : : ready = true;
11788 : : break;
11789 : : }
11790 : : }
11791 : 29713 : if (ready)
11792 : : {
11793 : 50906 : vect_schedule_slp_node (vinfo, entry, instance);
11794 : 50906 : scc_info.get (entry)->on_stack = false;
11795 : 50906 : stack[idx] = NULL;
11796 : 50906 : todo--;
11797 : 50906 : if (phi)
11798 : 23146 : phis_to_fixup.safe_push (entry);
11799 : : }
11800 : : }
11801 : : }
11802 : 23023 : while (todo != 0);
11803 : :
11804 : : /* Pop the SCC. */
11805 : 22716 : stack.truncate (last_idx);
11806 : : }
11807 : :
11808 : : /* Now fixup the backedge def of the vectorized PHIs in this SCC. */
11809 : : slp_tree phi_node;
11810 : 1867756 : FOR_EACH_VEC_ELT (phis_to_fixup, i, phi_node)
11811 : : {
11812 : 52172 : gphi *phi = as_a <gphi *> (SLP_TREE_REPRESENTATIVE (phi_node)->stmt);
11813 : 52172 : edge_iterator ei;
11814 : 52172 : edge e;
11815 : 161400 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
11816 : : {
11817 : 109228 : unsigned dest_idx = e->dest_idx;
11818 : 109228 : child = SLP_TREE_CHILDREN (phi_node)[dest_idx];
11819 : 109228 : if (!child || SLP_TREE_DEF_TYPE (child) != vect_internal_def)
11820 : 63885 : continue;
11821 : 45343 : unsigned n = SLP_TREE_VEC_DEFS (phi_node).length ();
11822 : : /* Simply fill all args. */
11823 : 45343 : if (STMT_VINFO_DEF_TYPE (SLP_TREE_REPRESENTATIVE (phi_node))
11824 : : != vect_first_order_recurrence)
11825 : 95899 : for (unsigned i = 0; i < n; ++i)
11826 : : {
11827 : 50588 : tree phidef = SLP_TREE_VEC_DEFS (phi_node)[i];
11828 : 50588 : gphi *phi = as_a <gphi *> (SSA_NAME_DEF_STMT (phidef));
11829 : 50588 : add_phi_arg (phi, vect_get_slp_vect_def (child, i),
11830 : : e, gimple_phi_arg_location (phi, dest_idx));
11831 : : }
11832 : : else
11833 : : {
11834 : : /* Unless it is a first order recurrence which needs
11835 : : args filled in for both the PHI node and the permutes. */
11836 : 32 : gimple *perm
11837 : 32 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[0]);
11838 : 32 : gimple *rphi = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (perm));
11839 : 32 : add_phi_arg (as_a <gphi *> (rphi),
11840 : : vect_get_slp_vect_def (child, n - 1),
11841 : : e, gimple_phi_arg_location (phi, dest_idx));
11842 : 84 : for (unsigned i = 0; i < n; ++i)
11843 : : {
11844 : 52 : gimple *perm
11845 : 52 : = SSA_NAME_DEF_STMT (SLP_TREE_VEC_DEFS (phi_node)[i]);
11846 : 52 : if (i > 0)
11847 : 20 : gimple_assign_set_rhs1 (perm,
11848 : : vect_get_slp_vect_def (child, i - 1));
11849 : 52 : gimple_assign_set_rhs2 (perm,
11850 : : vect_get_slp_vect_def (child, i));
11851 : 52 : update_stmt (perm);
11852 : : }
11853 : : }
11854 : : }
11855 : : }
11856 : 907792 : }
11857 : :
11858 : : /* Generate vector code for SLP_INSTANCES in the loop/basic block. */
11859 : :
11860 : : void
11861 : 539338 : vect_schedule_slp (vec_info *vinfo, const vec<slp_instance> &slp_instances)
11862 : : {
11863 : 539338 : slp_instance instance;
11864 : 539338 : unsigned int i;
11865 : :
11866 : 539338 : hash_map<slp_tree, slp_scc_info> scc_info;
11867 : 539338 : int maxdfs = 0;
11868 : 1116908 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
11869 : : {
11870 : 577570 : slp_tree node = SLP_INSTANCE_TREE (instance);
11871 : 577570 : if (dump_enabled_p ())
11872 : : {
11873 : 15474 : dump_printf_loc (MSG_NOTE, vect_location,
11874 : : "Vectorizing SLP tree:\n");
11875 : : /* ??? Dump all? */
11876 : 15474 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
11877 : 405 : dump_printf_loc (MSG_NOTE, vect_location, "Root stmt: %G",
11878 : 405 : SLP_INSTANCE_ROOT_STMTS (instance)[0]->stmt);
11879 : 15474 : vect_print_slp_graph (MSG_NOTE, vect_location,
11880 : : SLP_INSTANCE_TREE (instance));
11881 : : }
11882 : : /* Schedule the tree of INSTANCE, scheduling SCCs in a way to
11883 : : have a PHI be the node breaking the cycle. */
11884 : 577570 : auto_vec<slp_tree> stack;
11885 : 577570 : if (!scc_info.get (node))
11886 : 577396 : vect_schedule_scc (vinfo, node, instance, scc_info, maxdfs, stack);
11887 : :
11888 : 577570 : if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ())
11889 : 9842 : vectorize_slp_instance_root_stmt (vinfo, node, instance);
11890 : :
11891 : 577570 : if (dump_enabled_p ())
11892 : 15474 : dump_printf_loc (MSG_NOTE, vect_location,
11893 : : "vectorizing stmts using SLP.\n");
11894 : 577570 : }
11895 : :
11896 : 1656246 : FOR_EACH_VEC_ELT (slp_instances, i, instance)
11897 : : {
11898 : 577570 : slp_tree root = SLP_INSTANCE_TREE (instance);
11899 : 577570 : stmt_vec_info store_info;
11900 : 577570 : unsigned int j;
11901 : :
11902 : : /* Remove scalar call stmts. Do not do this for basic-block
11903 : : vectorization as not all uses may be vectorized.
11904 : : ??? Why should this be necessary? DCE should be able to
11905 : : remove the stmts itself.
11906 : : ??? For BB vectorization we can as well remove scalar
11907 : : stmts starting from the SLP tree root if they have no
11908 : : uses. */
11909 : 577570 : if (is_a <loop_vec_info> (vinfo))
11910 : 86970 : vect_remove_slp_scalar_calls (vinfo, root);
11911 : :
11912 : : /* Remove vectorized stores original scalar stmts. */
11913 : 2574811 : for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info); j++)
11914 : : {
11915 : 1456361 : if (!STMT_VINFO_DATA_REF (store_info)
11916 : 1427454 : || !DR_IS_WRITE (STMT_VINFO_DATA_REF (store_info)))
11917 : : break;
11918 : :
11919 : 1419671 : store_info = vect_orig_stmt (store_info);
11920 : : /* Free the attached stmt_vec_info and remove the stmt. */
11921 : 1419671 : vinfo->remove_stmt (store_info);
11922 : :
11923 : : /* Invalidate SLP_TREE_REPRESENTATIVE in case we released it
11924 : : to not crash in vect_free_slp_tree later. */
11925 : 1419671 : if (SLP_TREE_REPRESENTATIVE (root) == store_info)
11926 : 540678 : SLP_TREE_REPRESENTATIVE (root) = NULL;
11927 : : }
11928 : : }
11929 : 539338 : }
|