]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-vect-slp.c
[01/11] Schedule SLP earlier
[thirdparty/gcc.git] / gcc / tree-vect-slp.c
CommitLineData
ebfd146a 1/* SLP - Basic Block Vectorization
85ec4feb 2 Copyright (C) 2007-2018 Free Software Foundation, Inc.
b8698a0f 3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
ebfd146a
IR
4 and Ira Rosen <irar@il.ibm.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
c7131fb2 25#include "backend.h"
957060b5
AM
26#include "target.h"
27#include "rtl.h"
ebfd146a 28#include "tree.h"
c7131fb2 29#include "gimple.h"
957060b5 30#include "tree-pass.h"
c7131fb2 31#include "ssa.h"
957060b5
AM
32#include "optabs-tree.h"
33#include "insn-config.h"
34#include "recog.h" /* FIXME: for insn_data */
957060b5 35#include "params.h"
40e23961 36#include "fold-const.h"
d8a2d370 37#include "stor-layout.h"
5be5c238 38#include "gimple-iterator.h"
ebfd146a 39#include "cfgloop.h"
ebfd146a 40#include "tree-vectorizer.h"
2635892a 41#include "langhooks.h"
642fce57 42#include "gimple-walk.h"
428db0ba 43#include "dbgcnt.h"
5ebaa477 44#include "tree-vector-builder.h"
f151c9e1 45#include "vec-perm-indices.h"
018b2744
RS
46#include "gimple-fold.h"
47#include "internal-fn.h"
a70d6342
IR
48
49
6e2dd807
RS
50/* Recursively free the memory allocated for the SLP tree rooted at NODE.
51 FINAL_P is true if we have vectorized the instance or if we have
52 made a final decision not to vectorize the statements in any way. */
ebfd146a
IR
53
54static void
6e2dd807 55vect_free_slp_tree (slp_tree node, bool final_p)
ebfd146a 56{
d092494c 57 int i;
d755c7ef 58 slp_tree child;
d092494c 59
9771b263 60 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
6e2dd807 61 vect_free_slp_tree (child, final_p);
b8698a0f 62
6e2dd807
RS
63 /* Don't update STMT_VINFO_NUM_SLP_USES if it isn't relevant.
64 Some statements might no longer exist, after having been
65 removed by vect_transform_stmt. Updating the remaining
66 statements would be redundant. */
67 if (!final_p)
68 {
b9787581
RS
69 stmt_vec_info stmt_info;
70 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
6e2dd807 71 {
b9787581
RS
72 gcc_assert (STMT_VINFO_NUM_SLP_USES (stmt_info) > 0);
73 STMT_VINFO_NUM_SLP_USES (stmt_info)--;
6e2dd807
RS
74 }
75 }
78810bd3 76
9771b263
DN
77 SLP_TREE_CHILDREN (node).release ();
78 SLP_TREE_SCALAR_STMTS (node).release ();
79 SLP_TREE_VEC_STMTS (node).release ();
01d8bf07 80 SLP_TREE_LOAD_PERMUTATION (node).release ();
ebfd146a
IR
81
82 free (node);
83}
84
85
6e2dd807
RS
86/* Free the memory allocated for the SLP instance. FINAL_P is true if we
87 have vectorized the instance or if we have made a final decision not
88 to vectorize the statements in any way. */
ebfd146a
IR
89
90void
6e2dd807 91vect_free_slp_instance (slp_instance instance, bool final_p)
ebfd146a 92{
6e2dd807 93 vect_free_slp_tree (SLP_INSTANCE_TREE (instance), final_p);
9771b263 94 SLP_INSTANCE_LOADS (instance).release ();
c7e62a26 95 free (instance);
ebfd146a
IR
96}
97
98
d092494c
IR
99/* Create an SLP node for SCALAR_STMTS. */
100
101static slp_tree
b9787581 102vect_create_new_slp_node (vec<stmt_vec_info> scalar_stmts)
d092494c 103{
d3cfd39e 104 slp_tree node;
b9787581 105 stmt_vec_info stmt_info = scalar_stmts[0];
d092494c
IR
106 unsigned int nops;
107
b9787581 108 if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
d092494c 109 nops = gimple_call_num_args (stmt);
b9787581 110 else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
f7e531cf
IR
111 {
112 nops = gimple_num_ops (stmt) - 1;
113 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
114 nops++;
115 }
b9787581 116 else if (is_a <gphi *> (stmt_info->stmt))
e7baeb39 117 nops = 0;
d092494c
IR
118 else
119 return NULL;
120
d3cfd39e 121 node = XNEW (struct _slp_tree);
d092494c 122 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
9771b263 123 SLP_TREE_VEC_STMTS (node).create (0);
68435eb2 124 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = 0;
9771b263 125 SLP_TREE_CHILDREN (node).create (nops);
01d8bf07 126 SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
6876e5bc 127 SLP_TREE_TWO_OPERATORS (node) = false;
603cca93 128 SLP_TREE_DEF_TYPE (node) = vect_internal_def;
d092494c 129
78810bd3 130 unsigned i;
b9787581
RS
131 FOR_EACH_VEC_ELT (scalar_stmts, i, stmt_info)
132 STMT_VINFO_NUM_SLP_USES (stmt_info)++;
78810bd3 133
d092494c
IR
134 return node;
135}
136
137
ddf56386
RB
138/* This structure is used in creation of an SLP tree. Each instance
139 corresponds to the same operand in a group of scalar stmts in an SLP
140 node. */
141typedef struct _slp_oprnd_info
142{
143 /* Def-stmts for the operands. */
b9787581 144 vec<stmt_vec_info> def_stmts;
ddf56386
RB
145 /* Information about the first statement, its vector def-type, type, the
146 operand itself in case it's constant, and an indication if it's a pattern
147 stmt. */
ddf56386 148 tree first_op_type;
34e82342 149 enum vect_def_type first_dt;
ddf56386
RB
150 bool first_pattern;
151 bool second_pattern;
152} *slp_oprnd_info;
153
154
d092494c
IR
155/* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
156 operand. */
9771b263 157static vec<slp_oprnd_info>
d092494c
IR
158vect_create_oprnd_info (int nops, int group_size)
159{
160 int i;
161 slp_oprnd_info oprnd_info;
9771b263 162 vec<slp_oprnd_info> oprnds_info;
d092494c 163
9771b263 164 oprnds_info.create (nops);
d092494c
IR
165 for (i = 0; i < nops; i++)
166 {
167 oprnd_info = XNEW (struct _slp_oprnd_info);
9771b263 168 oprnd_info->def_stmts.create (group_size);
d092494c 169 oprnd_info->first_dt = vect_uninitialized_def;
793d9a16 170 oprnd_info->first_op_type = NULL_TREE;
d092494c 171 oprnd_info->first_pattern = false;
effb52da 172 oprnd_info->second_pattern = false;
9771b263 173 oprnds_info.quick_push (oprnd_info);
d092494c
IR
174 }
175
176 return oprnds_info;
177}
178
179
d3cfd39e
JJ
180/* Free operands info. */
181
d092494c 182static void
9771b263 183vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
d092494c
IR
184{
185 int i;
186 slp_oprnd_info oprnd_info;
187
9771b263 188 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
d3cfd39e 189 {
9771b263 190 oprnd_info->def_stmts.release ();
d3cfd39e
JJ
191 XDELETE (oprnd_info);
192 }
d092494c 193
9771b263 194 oprnds_info.release ();
d092494c
IR
195}
196
197
32e8e429
RS
198/* Find the place of the data-ref in STMT_INFO in the interleaving chain
199 that starts from FIRST_STMT_INFO. Return -1 if the data-ref is not a part
200 of the chain. */
d755c7ef 201
b210f45f 202int
32e8e429
RS
203vect_get_place_in_interleaving_chain (stmt_vec_info stmt_info,
204 stmt_vec_info first_stmt_info)
d755c7ef 205{
a1824cfd 206 stmt_vec_info next_stmt_info = first_stmt_info;
d755c7ef
RB
207 int result = 0;
208
a1824cfd 209 if (first_stmt_info != DR_GROUP_FIRST_ELEMENT (stmt_info))
d755c7ef
RB
210 return -1;
211
212 do
213 {
a1824cfd 214 if (next_stmt_info == stmt_info)
d755c7ef 215 return result;
a1824cfd
RS
216 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
217 if (next_stmt_info)
218 result += DR_GROUP_GAP (next_stmt_info);
d755c7ef 219 }
a1824cfd 220 while (next_stmt_info);
d755c7ef
RB
221
222 return -1;
223}
224
018b2744
RS
225/* Check whether it is possible to load COUNT elements of type ELT_MODE
226 using the method implemented by duplicate_and_interleave. Return true
227 if so, returning the number of intermediate vectors in *NVECTORS_OUT
228 (if nonnull) and the type of each intermediate vector in *VECTOR_TYPE_OUT
229 (if nonnull). */
230
f1739b48 231bool
018b2744 232can_duplicate_and_interleave_p (unsigned int count, machine_mode elt_mode,
f1739b48
RS
233 unsigned int *nvectors_out,
234 tree *vector_type_out,
235 tree *permutes)
018b2744
RS
236{
237 poly_int64 elt_bytes = count * GET_MODE_SIZE (elt_mode);
238 poly_int64 nelts;
239 unsigned int nvectors = 1;
240 for (;;)
241 {
242 scalar_int_mode int_mode;
243 poly_int64 elt_bits = elt_bytes * BITS_PER_UNIT;
244 if (multiple_p (current_vector_size, elt_bytes, &nelts)
245 && int_mode_for_size (elt_bits, 0).exists (&int_mode))
246 {
247 tree int_type = build_nonstandard_integer_type
248 (GET_MODE_BITSIZE (int_mode), 1);
249 tree vector_type = build_vector_type (int_type, nelts);
250 if (VECTOR_MODE_P (TYPE_MODE (vector_type)))
251 {
252 vec_perm_builder sel1 (nelts, 2, 3);
253 vec_perm_builder sel2 (nelts, 2, 3);
254 poly_int64 half_nelts = exact_div (nelts, 2);
255 for (unsigned int i = 0; i < 3; ++i)
256 {
257 sel1.quick_push (i);
258 sel1.quick_push (i + nelts);
259 sel2.quick_push (half_nelts + i);
260 sel2.quick_push (half_nelts + i + nelts);
261 }
262 vec_perm_indices indices1 (sel1, 2, nelts);
263 vec_perm_indices indices2 (sel2, 2, nelts);
264 if (can_vec_perm_const_p (TYPE_MODE (vector_type), indices1)
265 && can_vec_perm_const_p (TYPE_MODE (vector_type), indices2))
266 {
267 if (nvectors_out)
268 *nvectors_out = nvectors;
269 if (vector_type_out)
270 *vector_type_out = vector_type;
271 if (permutes)
272 {
273 permutes[0] = vect_gen_perm_mask_checked (vector_type,
274 indices1);
275 permutes[1] = vect_gen_perm_mask_checked (vector_type,
276 indices2);
277 }
278 return true;
279 }
280 }
281 }
282 if (!multiple_p (elt_bytes, 2, &elt_bytes))
283 return false;
284 nvectors *= 2;
285 }
286}
d755c7ef 287
d092494c
IR
288/* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
289 they are of a valid type and that they match the defs of the first stmt of
4cecd659 290 the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
018b2744
RS
291 by swapping operands of STMTS[STMT_NUM] when possible. Non-zero *SWAP
292 indicates swap is required for cond_expr stmts. Specifically, *SWAP
293 is 1 if STMT is cond and operands of comparison need to be swapped;
294 *SWAP is 2 if STMT is cond and code of comparison needs to be inverted.
295 If there is any operand swap in this function, *SWAP is set to non-zero
296 value.
4cecd659
BC
297 If there was a fatal error return -1; if the error could be corrected by
298 swapping operands of father node of this one, return 1; if everything is
299 ok return 0. */
4cecd659
BC
300static int
301vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char *swap,
b9787581 302 vec<stmt_vec_info> stmts, unsigned stmt_num,
4cecd659 303 vec<slp_oprnd_info> *oprnds_info)
ebfd146a 304{
b9787581 305 stmt_vec_info stmt_info = stmts[stmt_num];
ebfd146a
IR
306 tree oprnd;
307 unsigned int i, number_of_oprnds;
d092494c 308 enum vect_def_type dt = vect_uninitialized_def;
d092494c 309 bool pattern = false;
abf9bfbc 310 slp_oprnd_info oprnd_info;
b0b4483e
RB
311 int first_op_idx = 1;
312 bool commutative = false;
313 bool first_op_cond = false;
effb52da
RB
314 bool first = stmt_num == 0;
315 bool second = stmt_num == 1;
b8698a0f 316
b9787581 317 if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
190c2236
JJ
318 {
319 number_of_oprnds = gimple_call_num_args (stmt);
b0b4483e 320 first_op_idx = 3;
190c2236 321 }
b9787581 322 else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
f7e531cf 323 {
b0b4483e 324 enum tree_code code = gimple_assign_rhs_code (stmt);
f7e531cf 325 number_of_oprnds = gimple_num_ops (stmt) - 1;
4cecd659
BC
326 /* Swap can only be done for cond_expr if asked to, otherwise we
327 could result in different comparison code to the first stmt. */
a414c77f
IE
328 if (gimple_assign_rhs_code (stmt) == COND_EXPR
329 && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt)))
b0b4483e
RB
330 {
331 first_op_cond = true;
b0b4483e
RB
332 number_of_oprnds++;
333 }
334 else
335 commutative = commutative_tree_code (code);
f7e531cf 336 }
d092494c 337 else
b0b4483e 338 return -1;
ebfd146a 339
4cecd659
BC
340 bool swapped = (*swap != 0);
341 gcc_assert (!swapped || first_op_cond);
ebfd146a
IR
342 for (i = 0; i < number_of_oprnds; i++)
343 {
b0b4483e
RB
344again:
345 if (first_op_cond)
f7e531cf 346 {
4cecd659
BC
347 /* Map indicating how operands of cond_expr should be swapped. */
348 int maps[3][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 1, 3, 2}};
349 int *map = maps[*swap];
350
351 if (i < 2)
b9787581
RS
352 oprnd = TREE_OPERAND (gimple_op (stmt_info->stmt,
353 first_op_idx), map[i]);
b0b4483e 354 else
b9787581 355 oprnd = gimple_op (stmt_info->stmt, map[i]);
f7e531cf
IR
356 }
357 else
b9787581 358 oprnd = gimple_op (stmt_info->stmt, first_op_idx + (swapped ? !i : i));
f7e531cf 359
9771b263 360 oprnd_info = (*oprnds_info)[i];
ebfd146a 361
fef96d8e
RS
362 stmt_vec_info def_stmt_info;
363 if (!vect_is_simple_use (oprnd, vinfo, &dt, &def_stmt_info))
ebfd146a 364 {
73fbfcad 365 if (dump_enabled_p ())
ebfd146a 366 {
78c60e3d 367 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3fc356dc 368 "Build SLP failed: can't analyze def for ");
78c60e3d 369 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
e645e942 370 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
ebfd146a
IR
371 }
372
b0b4483e 373 return -1;
ebfd146a
IR
374 }
375
fef96d8e
RS
376 /* Check if DEF_STMT_INFO is a part of a pattern in LOOP and get
377 the def stmt from the pattern. Check that all the stmts of the
378 node are in the pattern. */
379 if (def_stmt_info && is_pattern_stmt_p (def_stmt_info))
ebfd146a 380 {
d092494c 381 pattern = true;
effb52da
RB
382 if (!first && !oprnd_info->first_pattern
383 /* Allow different pattern state for the defs of the
384 first stmt in reduction chains. */
385 && (oprnd_info->first_dt != vect_reduction_def
386 || (!second && !oprnd_info->second_pattern)))
d092494c 387 {
b0b4483e
RB
388 if (i == 0
389 && !swapped
390 && commutative)
391 {
392 swapped = true;
393 goto again;
394 }
395
73fbfcad 396 if (dump_enabled_p ())
d092494c 397 {
78c60e3d
SS
398 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
399 "Build SLP failed: some of the stmts"
400 " are in a pattern, and others are not ");
401 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
e645e942 402 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
d092494c 403 }
ebfd146a 404
b0b4483e 405 return 1;
ebfd146a
IR
406 }
407
fef96d8e 408 dt = STMT_VINFO_DEF_TYPE (def_stmt_info);
ebfd146a 409
f7e531cf 410 if (dt == vect_unknown_def_type)
ebfd146a 411 {
73fbfcad 412 if (dump_enabled_p ())
78c60e3d 413 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 414 "Unsupported pattern.\n");
b0b4483e 415 return -1;
ebfd146a
IR
416 }
417
fef96d8e 418 switch (gimple_code (def_stmt_info->stmt))
ebfd146a 419 {
81c40241
RB
420 case GIMPLE_PHI:
421 case GIMPLE_ASSIGN:
422 break;
423
424 default:
425 if (dump_enabled_p ())
426 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
427 "unsupported defining stmt:\n");
428 return -1;
ebfd146a
IR
429 }
430 }
431
effb52da
RB
432 if (second)
433 oprnd_info->second_pattern = pattern;
434
d092494c 435 if (first)
ebfd146a 436 {
d092494c
IR
437 oprnd_info->first_dt = dt;
438 oprnd_info->first_pattern = pattern;
793d9a16 439 oprnd_info->first_op_type = TREE_TYPE (oprnd);
ebfd146a 440 }
ebfd146a
IR
441 else
442 {
d092494c
IR
443 /* Not first stmt of the group, check that the def-stmt/s match
444 the def-stmt/s of the first stmt. Allow different definition
445 types for reduction chains: the first stmt must be a
446 vect_reduction_def (a phi node), and the rest
447 vect_internal_def. */
018b2744
RS
448 tree type = TREE_TYPE (oprnd);
449 if ((oprnd_info->first_dt != dt
450 && !(oprnd_info->first_dt == vect_reduction_def
451 && dt == vect_internal_def)
452 && !((oprnd_info->first_dt == vect_external_def
453 || oprnd_info->first_dt == vect_constant_def)
454 && (dt == vect_external_def
455 || dt == vect_constant_def)))
456 || !types_compatible_p (oprnd_info->first_op_type, type))
ebfd146a 457 {
b0b4483e
RB
458 /* Try swapping operands if we got a mismatch. */
459 if (i == 0
460 && !swapped
461 && commutative)
462 {
463 swapped = true;
464 goto again;
465 }
466
abf9bfbc
RB
467 if (dump_enabled_p ())
468 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 469 "Build SLP failed: different types\n");
d092494c 470
b0b4483e 471 return 1;
ebfd146a 472 }
018b2744
RS
473 if ((dt == vect_constant_def
474 || dt == vect_external_def)
475 && !current_vector_size.is_constant ()
476 && (TREE_CODE (type) == BOOLEAN_TYPE
477 || !can_duplicate_and_interleave_p (stmts.length (),
478 TYPE_MODE (type))))
a23644f2
RS
479 {
480 if (dump_enabled_p ())
481 {
482 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
483 "Build SLP failed: invalid type of def "
484 "for variable-length SLP ");
485 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
486 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
487 }
488 return -1;
489 }
018b2744
RS
490 }
491
492 /* Check the types of the definitions. */
493 switch (dt)
494 {
495 case vect_constant_def:
496 case vect_external_def:
ebfd146a 497 break;
b8698a0f 498
c78e3652 499 case vect_reduction_def:
e7baeb39 500 case vect_induction_def:
8644a673 501 case vect_internal_def:
fef96d8e 502 oprnd_info->def_stmts.quick_push (def_stmt_info);
ebfd146a
IR
503 break;
504
505 default:
506 /* FORNOW: Not supported. */
73fbfcad 507 if (dump_enabled_p ())
ebfd146a 508 {
78c60e3d
SS
509 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
510 "Build SLP failed: illegal type of def ");
81c40241 511 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
e645e942 512 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
ebfd146a
IR
513 }
514
b0b4483e 515 return -1;
ebfd146a
IR
516 }
517 }
518
b0b4483e
RB
519 /* Swap operands. */
520 if (swapped)
521 {
78810bd3
RB
522 /* If there are already uses of this stmt in a SLP instance then
523 we've committed to the operand order and can't swap it. */
b9787581 524 if (STMT_VINFO_NUM_SLP_USES (stmt_info) != 0)
78810bd3
RB
525 {
526 if (dump_enabled_p ())
527 {
528 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
529 "Build SLP failed: cannot swap operands of "
530 "shared stmt ");
b9787581
RS
531 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
532 stmt_info->stmt, 0);
78810bd3
RB
533 }
534 return -1;
535 }
536
b9787581 537 gassign *stmt = as_a <gassign *> (stmt_info->stmt);
b0b4483e
RB
538 if (first_op_cond)
539 {
540 tree cond = gimple_assign_rhs1 (stmt);
4cecd659
BC
541 enum tree_code code = TREE_CODE (cond);
542
543 /* Swap. */
544 if (*swap == 1)
545 {
546 swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
547 &TREE_OPERAND (cond, 1));
548 TREE_SET_CODE (cond, swap_tree_comparison (code));
549 }
550 /* Invert. */
551 else
552 {
553 swap_ssa_operands (stmt, gimple_assign_rhs2_ptr (stmt),
554 gimple_assign_rhs3_ptr (stmt));
555 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond, 0));
556 code = invert_tree_comparison (TREE_CODE (cond), honor_nans);
557 gcc_assert (code != ERROR_MARK);
558 TREE_SET_CODE (cond, code);
559 }
b0b4483e
RB
560 }
561 else
562 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
563 gimple_assign_rhs2_ptr (stmt));
78810bd3
RB
564 if (dump_enabled_p ())
565 {
566 dump_printf_loc (MSG_NOTE, vect_location,
567 "swapped operands to match def types in ");
568 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
569 }
b0b4483e
RB
570 }
571
4cecd659 572 *swap = swapped;
b0b4483e 573 return 0;
ebfd146a
IR
574}
575
5249ee4d
RS
576/* Return true if call statements CALL1 and CALL2 are similar enough
577 to be combined into the same SLP group. */
578
579static bool
580compatible_calls_p (gcall *call1, gcall *call2)
581{
582 unsigned int nargs = gimple_call_num_args (call1);
583 if (nargs != gimple_call_num_args (call2))
584 return false;
585
586 if (gimple_call_combined_fn (call1) != gimple_call_combined_fn (call2))
587 return false;
588
589 if (gimple_call_internal_p (call1))
590 {
591 if (!types_compatible_p (TREE_TYPE (gimple_call_lhs (call1)),
592 TREE_TYPE (gimple_call_lhs (call2))))
593 return false;
594 for (unsigned int i = 0; i < nargs; ++i)
595 if (!types_compatible_p (TREE_TYPE (gimple_call_arg (call1, i)),
596 TREE_TYPE (gimple_call_arg (call2, i))))
597 return false;
598 }
599 else
600 {
601 if (!operand_equal_p (gimple_call_fn (call1),
602 gimple_call_fn (call2), 0))
603 return false;
604
605 if (gimple_call_fntype (call1) != gimple_call_fntype (call2))
606 return false;
607 }
608 return true;
609}
610
b161f2c9 611/* A subroutine of vect_build_slp_tree for checking VECTYPE, which is the
d7609678 612 caller's attempt to find the vector type in STMT_INFO with the narrowest
b161f2c9 613 element type. Return true if VECTYPE is nonnull and if it is valid
d7609678
RS
614 for STMT_INFO. When returning true, update MAX_NUNITS to reflect the
615 number of units in VECTYPE. GROUP_SIZE and MAX_NUNITS are as for
616 vect_build_slp_tree. */
b161f2c9
RS
617
618static bool
d7609678 619vect_record_max_nunits (stmt_vec_info stmt_info, unsigned int group_size,
4b6068ea 620 tree vectype, poly_uint64 *max_nunits)
b161f2c9
RS
621{
622 if (!vectype)
623 {
624 if (dump_enabled_p ())
625 {
626 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
627 "Build SLP failed: unsupported data-type in ");
d7609678
RS
628 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
629 stmt_info->stmt, 0);
b161f2c9
RS
630 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
631 }
632 /* Fatal mismatch. */
633 return false;
634 }
635
636 /* If populating the vector type requires unrolling then fail
637 before adjusting *max_nunits for basic-block vectorization. */
4b6068ea
RS
638 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
639 unsigned HOST_WIDE_INT const_nunits;
d7609678 640 if (STMT_VINFO_BB_VINFO (stmt_info)
4b6068ea
RS
641 && (!nunits.is_constant (&const_nunits)
642 || const_nunits > group_size))
b161f2c9
RS
643 {
644 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
645 "Build SLP failed: unrolling required "
646 "in basic block SLP\n");
647 /* Fatal mismatch. */
648 return false;
649 }
650
651 /* In case of multiple types we need to detect the smallest type. */
4b6068ea 652 vect_update_max_nunits (max_nunits, vectype);
b161f2c9
RS
653 return true;
654}
ebfd146a 655
1f3cb663
RS
656/* STMTS is a group of GROUP_SIZE SLP statements in which some
657 statements do the same operation as the first statement and in which
658 the others do ALT_STMT_CODE. Return true if we can take one vector
659 of the first operation and one vector of the second and permute them
660 to get the required result. VECTYPE is the type of the vector that
661 would be permuted. */
662
663static bool
b9787581
RS
664vect_two_operations_perm_ok_p (vec<stmt_vec_info> stmts,
665 unsigned int group_size, tree vectype,
666 tree_code alt_stmt_code)
1f3cb663
RS
667{
668 unsigned HOST_WIDE_INT count;
669 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&count))
670 return false;
671
672 vec_perm_builder sel (count, count, 1);
673 for (unsigned int i = 0; i < count; ++i)
674 {
675 unsigned int elt = i;
b9787581
RS
676 gassign *stmt = as_a <gassign *> (stmts[i % group_size]->stmt);
677 if (gimple_assign_rhs_code (stmt) == alt_stmt_code)
1f3cb663
RS
678 elt += count;
679 sel.quick_push (elt);
680 }
681 vec_perm_indices indices (sel, 2, count);
682 return can_vec_perm_const_p (TYPE_MODE (vectype), indices);
683}
684
6983e6b5
RB
685/* Verify if the scalar stmts STMTS are isomorphic, require data
686 permutation or are of unsupported types of operation. Return
687 true if they are, otherwise return false and indicate in *MATCHES
688 which stmts are not isomorphic to the first one. If MATCHES[0]
689 is false then this indicates the comparison could not be
4cecd659
BC
690 carried out or the stmts will never be vectorized by SLP.
691
692 Note COND_EXPR is possibly ismorphic to another one after swapping its
693 operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
694 the first stmt by swapping the two operands of comparison; set SWAP[i]
695 to 2 if stmt I is isormorphic to the first stmt by inverting the code
696 of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
697 to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
ebfd146a
IR
698
699static bool
d7609678 700vect_build_slp_tree_1 (unsigned char *swap,
b9787581 701 vec<stmt_vec_info> stmts, unsigned int group_size,
5249ee4d
RS
702 poly_uint64 *max_nunits, bool *matches,
703 bool *two_operators)
ebfd146a 704{
ebfd146a 705 unsigned int i;
b9787581 706 stmt_vec_info first_stmt_info = stmts[0];
6876e5bc
RB
707 enum tree_code first_stmt_code = ERROR_MARK;
708 enum tree_code alt_stmt_code = ERROR_MARK;
709 enum tree_code rhs_code = ERROR_MARK;
f7e531cf 710 enum tree_code first_cond_code = ERROR_MARK;
ebfd146a 711 tree lhs;
6983e6b5 712 bool need_same_oprnds = false;
1f3cb663 713 tree vectype = NULL_TREE, first_op1 = NULL_TREE;
ebfd146a
IR
714 optab optab;
715 int icode;
ef4bddc2
RS
716 machine_mode optab_op2_mode;
717 machine_mode vec_mode;
bffb8014 718 stmt_vec_info first_load = NULL, prev_first_load = NULL;
d092494c 719
ebfd146a 720 /* For every stmt in NODE find its def stmt/s. */
b9787581
RS
721 stmt_vec_info stmt_info;
722 FOR_EACH_VEC_ELT (stmts, i, stmt_info)
ebfd146a 723 {
b9787581 724 gimple *stmt = stmt_info->stmt;
4cecd659 725 swap[i] = 0;
6983e6b5
RB
726 matches[i] = false;
727
73fbfcad 728 if (dump_enabled_p ())
ebfd146a 729 {
78c60e3d
SS
730 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
731 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
ebfd146a
IR
732 }
733
4b5caab7 734 /* Fail to vectorize statements marked as unvectorizable. */
b9787581 735 if (!STMT_VINFO_VECTORIZABLE (stmt_info))
4b5caab7 736 {
73fbfcad 737 if (dump_enabled_p ())
4b5caab7 738 {
78c60e3d
SS
739 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
740 "Build SLP failed: unvectorizable statement ");
741 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
4b5caab7 742 }
6983e6b5
RB
743 /* Fatal mismatch. */
744 matches[0] = false;
4b5caab7
IR
745 return false;
746 }
747
ebfd146a
IR
748 lhs = gimple_get_lhs (stmt);
749 if (lhs == NULL_TREE)
750 {
73fbfcad 751 if (dump_enabled_p ())
ebfd146a 752 {
78c60e3d
SS
753 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
754 "Build SLP failed: not GIMPLE_ASSIGN nor "
755 "GIMPLE_CALL ");
756 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
ebfd146a 757 }
6983e6b5
RB
758 /* Fatal mismatch. */
759 matches[0] = false;
ebfd146a
IR
760 return false;
761 }
762
1f3cb663
RS
763 tree nunits_vectype;
764 if (!vect_get_vector_types_for_stmt (stmt_info, &vectype,
765 &nunits_vectype)
766 || (nunits_vectype
d7609678 767 && !vect_record_max_nunits (stmt_info, group_size,
1f3cb663 768 nunits_vectype, max_nunits)))
b161f2c9 769 {
6983e6b5
RB
770 /* Fatal mismatch. */
771 matches[0] = false;
1f3cb663
RS
772 return false;
773 }
774
775 gcc_assert (vectype);
b8698a0f 776
538dd0b7 777 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
190c2236
JJ
778 {
779 rhs_code = CALL_EXPR;
5249ee4d
RS
780 if ((gimple_call_internal_p (call_stmt)
781 && (!vectorizable_internal_fn_p
782 (gimple_call_internal_fn (call_stmt))))
538dd0b7
DM
783 || gimple_call_tail_p (call_stmt)
784 || gimple_call_noreturn_p (call_stmt)
785 || !gimple_call_nothrow_p (call_stmt)
786 || gimple_call_chain (call_stmt))
190c2236 787 {
73fbfcad 788 if (dump_enabled_p ())
190c2236 789 {
78c60e3d
SS
790 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
791 "Build SLP failed: unsupported call type ");
538dd0b7
DM
792 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
793 call_stmt, 0);
190c2236 794 }
6983e6b5
RB
795 /* Fatal mismatch. */
796 matches[0] = false;
190c2236
JJ
797 return false;
798 }
799 }
ebfd146a
IR
800 else
801 rhs_code = gimple_assign_rhs_code (stmt);
802
803 /* Check the operation. */
804 if (i == 0)
805 {
806 first_stmt_code = rhs_code;
807
b8698a0f 808 /* Shift arguments should be equal in all the packed stmts for a
ebfd146a
IR
809 vector shift with scalar shift operand. */
810 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
811 || rhs_code == LROTATE_EXPR
812 || rhs_code == RROTATE_EXPR)
813 {
1f3cb663
RS
814 if (vectype == boolean_type_node)
815 {
816 if (dump_enabled_p ())
817 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
818 "Build SLP failed: shift of a"
819 " boolean.\n");
820 /* Fatal mismatch. */
821 matches[0] = false;
822 return false;
823 }
824
ebfd146a
IR
825 vec_mode = TYPE_MODE (vectype);
826
827 /* First see if we have a vector/vector shift. */
828 optab = optab_for_tree_code (rhs_code, vectype,
829 optab_vector);
830
831 if (!optab
947131ba 832 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
ebfd146a
IR
833 {
834 /* No vector/vector shift, try for a vector/scalar shift. */
835 optab = optab_for_tree_code (rhs_code, vectype,
836 optab_scalar);
837
838 if (!optab)
839 {
73fbfcad 840 if (dump_enabled_p ())
78c60e3d 841 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 842 "Build SLP failed: no optab.\n");
6983e6b5
RB
843 /* Fatal mismatch. */
844 matches[0] = false;
ebfd146a
IR
845 return false;
846 }
947131ba 847 icode = (int) optab_handler (optab, vec_mode);
ebfd146a
IR
848 if (icode == CODE_FOR_nothing)
849 {
73fbfcad 850 if (dump_enabled_p ())
78c60e3d
SS
851 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
852 "Build SLP failed: "
e645e942 853 "op not supported by target.\n");
6983e6b5
RB
854 /* Fatal mismatch. */
855 matches[0] = false;
ebfd146a
IR
856 return false;
857 }
858 optab_op2_mode = insn_data[icode].operand[2].mode;
859 if (!VECTOR_MODE_P (optab_op2_mode))
860 {
861 need_same_oprnds = true;
862 first_op1 = gimple_assign_rhs2 (stmt);
863 }
864 }
865 }
36ba4aae
IR
866 else if (rhs_code == WIDEN_LSHIFT_EXPR)
867 {
868 need_same_oprnds = true;
869 first_op1 = gimple_assign_rhs2 (stmt);
870 }
ebfd146a
IR
871 }
872 else
873 {
6876e5bc
RB
874 if (first_stmt_code != rhs_code
875 && alt_stmt_code == ERROR_MARK)
876 alt_stmt_code = rhs_code;
ebfd146a
IR
877 if (first_stmt_code != rhs_code
878 && (first_stmt_code != IMAGPART_EXPR
879 || rhs_code != REALPART_EXPR)
880 && (first_stmt_code != REALPART_EXPR
69f11a13 881 || rhs_code != IMAGPART_EXPR)
6876e5bc
RB
882 /* Handle mismatches in plus/minus by computing both
883 and merging the results. */
884 && !((first_stmt_code == PLUS_EXPR
885 || first_stmt_code == MINUS_EXPR)
886 && (alt_stmt_code == PLUS_EXPR
887 || alt_stmt_code == MINUS_EXPR)
888 && rhs_code == alt_stmt_code)
b9787581 889 && !(STMT_VINFO_GROUPED_ACCESS (stmt_info)
69f11a13 890 && (first_stmt_code == ARRAY_REF
38000232 891 || first_stmt_code == BIT_FIELD_REF
69f11a13
IR
892 || first_stmt_code == INDIRECT_REF
893 || first_stmt_code == COMPONENT_REF
894 || first_stmt_code == MEM_REF)))
ebfd146a 895 {
73fbfcad 896 if (dump_enabled_p ())
ebfd146a 897 {
78c60e3d
SS
898 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
899 "Build SLP failed: different operation "
900 "in stmt ");
901 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
6876e5bc
RB
902 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
903 "original stmt ");
904 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
b9787581 905 first_stmt_info->stmt, 0);
ebfd146a 906 }
6983e6b5
RB
907 /* Mismatch. */
908 continue;
ebfd146a 909 }
b8698a0f
L
910
911 if (need_same_oprnds
ebfd146a
IR
912 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
913 {
73fbfcad 914 if (dump_enabled_p ())
ebfd146a 915 {
78c60e3d
SS
916 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
917 "Build SLP failed: different shift "
918 "arguments in ");
919 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
ebfd146a 920 }
6983e6b5
RB
921 /* Mismatch. */
922 continue;
ebfd146a 923 }
190c2236
JJ
924
925 if (rhs_code == CALL_EXPR)
926 {
b9787581 927 if (!compatible_calls_p (as_a <gcall *> (stmts[0]->stmt),
5249ee4d 928 as_a <gcall *> (stmt)))
190c2236 929 {
73fbfcad 930 if (dump_enabled_p ())
190c2236 931 {
78c60e3d
SS
932 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
933 "Build SLP failed: different calls in ");
934 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
935 stmt, 0);
190c2236 936 }
6983e6b5
RB
937 /* Mismatch. */
938 continue;
190c2236
JJ
939 }
940 }
ebfd146a
IR
941 }
942
0d0293ac 943 /* Grouped store or load. */
b9787581 944 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a
IR
945 {
946 if (REFERENCE_CLASS_P (lhs))
947 {
948 /* Store. */
6983e6b5 949 ;
ebfd146a 950 }
b5aeb3bb
IR
951 else
952 {
953 /* Load. */
b9787581 954 first_load = DR_GROUP_FIRST_ELEMENT (stmt_info);
b5aeb3bb
IR
955 if (prev_first_load)
956 {
957 /* Check that there are no loads from different interleaving
6983e6b5
RB
958 chains in the same node. */
959 if (prev_first_load != first_load)
78c60e3d 960 {
73fbfcad 961 if (dump_enabled_p ())
b5aeb3bb 962 {
78c60e3d
SS
963 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
964 vect_location,
965 "Build SLP failed: different "
966 "interleaving chains in one node ");
967 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
968 stmt, 0);
b5aeb3bb 969 }
6983e6b5
RB
970 /* Mismatch. */
971 continue;
b5aeb3bb
IR
972 }
973 }
974 else
975 prev_first_load = first_load;
ebfd146a 976 }
0d0293ac 977 } /* Grouped access. */
ebfd146a
IR
978 else
979 {
980 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
981 {
0d0293ac 982 /* Not grouped load. */
73fbfcad 983 if (dump_enabled_p ())
ebfd146a 984 {
78c60e3d
SS
985 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
986 "Build SLP failed: not grouped load ");
987 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
ebfd146a
IR
988 }
989
0d0293ac 990 /* FORNOW: Not grouped loads are not supported. */
6983e6b5
RB
991 /* Fatal mismatch. */
992 matches[0] = false;
ebfd146a
IR
993 return false;
994 }
995
996 /* Not memory operation. */
997 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
f7e531cf 998 && TREE_CODE_CLASS (rhs_code) != tcc_unary
effb52da 999 && TREE_CODE_CLASS (rhs_code) != tcc_expression
42fd8198 1000 && TREE_CODE_CLASS (rhs_code) != tcc_comparison
190c2236 1001 && rhs_code != CALL_EXPR)
ebfd146a 1002 {
73fbfcad 1003 if (dump_enabled_p ())
ebfd146a 1004 {
78c60e3d
SS
1005 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1006 "Build SLP failed: operation");
1007 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
1008 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
ebfd146a 1009 }
6983e6b5
RB
1010 /* Fatal mismatch. */
1011 matches[0] = false;
ebfd146a
IR
1012 return false;
1013 }
1014
4cecd659
BC
1015 if (rhs_code == COND_EXPR)
1016 {
1017 tree cond_expr = gimple_assign_rhs1 (stmt);
1018 enum tree_code cond_code = TREE_CODE (cond_expr);
1019 enum tree_code swap_code = ERROR_MARK;
1020 enum tree_code invert_code = ERROR_MARK;
f7e531cf
IR
1021
1022 if (i == 0)
1023 first_cond_code = TREE_CODE (cond_expr);
4cecd659
BC
1024 else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1025 {
1026 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
1027 swap_code = swap_tree_comparison (cond_code);
1028 invert_code = invert_tree_comparison (cond_code, honor_nans);
1029 }
1030
1031 if (first_cond_code == cond_code)
1032 ;
1033 /* Isomorphic can be achieved by swapping. */
1034 else if (first_cond_code == swap_code)
1035 swap[i] = 1;
1036 /* Isomorphic can be achieved by inverting. */
1037 else if (first_cond_code == invert_code)
1038 swap[i] = 2;
1039 else
1040 {
1041 if (dump_enabled_p ())
1042 {
1043 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
78c60e3d
SS
1044 "Build SLP failed: different"
1045 " operation");
4cecd659 1046 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
78c60e3d 1047 stmt, 0);
4cecd659 1048 }
6983e6b5
RB
1049 /* Mismatch. */
1050 continue;
f7e531cf 1051 }
4cecd659 1052 }
ebfd146a 1053 }
6983e6b5
RB
1054
1055 matches[i] = true;
1056 }
1057
1058 for (i = 0; i < group_size; ++i)
1059 if (!matches[i])
1060 return false;
1061
6876e5bc
RB
1062 /* If we allowed a two-operation SLP node verify the target can cope
1063 with the permute we are going to use. */
1064 if (alt_stmt_code != ERROR_MARK
1065 && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference)
1066 {
1f3cb663
RS
1067 if (vectype == boolean_type_node
1068 || !vect_two_operations_perm_ok_p (stmts, group_size,
1069 vectype, alt_stmt_code))
6876e5bc
RB
1070 {
1071 for (i = 0; i < group_size; ++i)
b9787581 1072 if (gimple_assign_rhs_code (stmts[i]->stmt) == alt_stmt_code)
6876e5bc
RB
1073 {
1074 matches[i] = false;
1075 if (dump_enabled_p ())
1076 {
1077 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1078 "Build SLP failed: different operation "
1079 "in stmt ");
1080 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
b9787581 1081 stmts[i]->stmt, 0);
6876e5bc
RB
1082 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1083 "original stmt ");
1084 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
b9787581 1085 first_stmt_info->stmt, 0);
6876e5bc
RB
1086 }
1087 }
1088 return false;
1089 }
1090 *two_operators = true;
1091 }
1092
6983e6b5
RB
1093 return true;
1094}
1095
26d66f28
RB
1096/* Traits for the hash_set to record failed SLP builds for a stmt set.
1097 Note we never remove apart from at destruction time so we do not
1098 need a special value for deleted that differs from empty. */
1099struct bst_traits
1100{
b9787581
RS
1101 typedef vec <stmt_vec_info> value_type;
1102 typedef vec <stmt_vec_info> compare_type;
26d66f28
RB
1103 static inline hashval_t hash (value_type);
1104 static inline bool equal (value_type existing, value_type candidate);
1105 static inline bool is_empty (value_type x) { return !x.exists (); }
1106 static inline bool is_deleted (value_type x) { return !x.exists (); }
1107 static inline void mark_empty (value_type &x) { x.release (); }
1108 static inline void mark_deleted (value_type &x) { x.release (); }
1109 static inline void remove (value_type &x) { x.release (); }
1110};
1111inline hashval_t
1112bst_traits::hash (value_type x)
1113{
1114 inchash::hash h;
1115 for (unsigned i = 0; i < x.length (); ++i)
b9787581 1116 h.add_int (gimple_uid (x[i]->stmt));
26d66f28
RB
1117 return h.end ();
1118}
1119inline bool
1120bst_traits::equal (value_type existing, value_type candidate)
1121{
1122 if (existing.length () != candidate.length ())
1123 return false;
1124 for (unsigned i = 0; i < existing.length (); ++i)
1125 if (existing[i] != candidate[i])
1126 return false;
1127 return true;
1128}
1129
f7300fff
RB
1130typedef hash_set <vec <gimple *>, bst_traits> scalar_stmts_set_t;
1131static scalar_stmts_set_t *bst_fail;
26d66f28 1132
68435eb2
RB
1133typedef hash_map <vec <gimple *>, slp_tree,
1134 simple_hashmap_traits <bst_traits, slp_tree> >
1135 scalar_stmts_to_slp_tree_map_t;
1136
26d66f28
RB
1137static slp_tree
1138vect_build_slp_tree_2 (vec_info *vinfo,
b9787581 1139 vec<stmt_vec_info> stmts, unsigned int group_size,
4b6068ea 1140 poly_uint64 *max_nunits,
26d66f28
RB
1141 vec<slp_tree> *loads,
1142 bool *matches, unsigned *npermutes, unsigned *tree_size,
1143 unsigned max_tree_size);
6983e6b5 1144
e403d17e 1145static slp_tree
310213d4 1146vect_build_slp_tree (vec_info *vinfo,
b9787581 1147 vec<stmt_vec_info> stmts, unsigned int group_size,
4b6068ea 1148 poly_uint64 *max_nunits, vec<slp_tree> *loads,
1428105c
RB
1149 bool *matches, unsigned *npermutes, unsigned *tree_size,
1150 unsigned max_tree_size)
26d66f28
RB
1151{
1152 if (bst_fail->contains (stmts))
1153 return NULL;
1154 slp_tree res = vect_build_slp_tree_2 (vinfo, stmts, group_size, max_nunits,
1155 loads, matches, npermutes, tree_size,
1156 max_tree_size);
1157 /* When SLP build fails for stmts record this, otherwise SLP build
1158 can be exponential in time when we allow to construct parts from
1159 scalars, see PR81723. */
1160 if (! res)
1161 {
b9787581 1162 vec <stmt_vec_info> x;
26d66f28
RB
1163 x.create (stmts.length ());
1164 x.splice (stmts);
1165 bst_fail->add (x);
1166 }
1167 return res;
1168}
1169
1170/* Recursively build an SLP tree starting from NODE.
1171 Fail (and return a value not equal to zero) if def-stmts are not
1172 isomorphic, require data permutation or are of unsupported types of
1173 operation. Otherwise, return 0.
1174 The value returned is the depth in the SLP tree where a mismatch
1175 was found. */
1176
1177static slp_tree
1178vect_build_slp_tree_2 (vec_info *vinfo,
b9787581 1179 vec<stmt_vec_info> stmts, unsigned int group_size,
4b6068ea 1180 poly_uint64 *max_nunits,
26d66f28
RB
1181 vec<slp_tree> *loads,
1182 bool *matches, unsigned *npermutes, unsigned *tree_size,
1183 unsigned max_tree_size)
6983e6b5 1184{
4b6068ea
RS
1185 unsigned nops, i, this_tree_size = 0;
1186 poly_uint64 this_max_nunits = *max_nunits;
e403d17e 1187 slp_tree node;
6983e6b5 1188
6983e6b5
RB
1189 matches[0] = false;
1190
b9787581
RS
1191 stmt_vec_info stmt_info = stmts[0];
1192 if (gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt))
6983e6b5 1193 nops = gimple_call_num_args (stmt);
b9787581 1194 else if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
6983e6b5
RB
1195 {
1196 nops = gimple_num_ops (stmt) - 1;
1197 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
1198 nops++;
ebfd146a 1199 }
b9787581 1200 else if (is_a <gphi *> (stmt_info->stmt))
e7baeb39 1201 nops = 0;
6983e6b5 1202 else
e403d17e 1203 return NULL;
6983e6b5 1204
c78e3652
RB
1205 /* If the SLP node is a PHI (induction or reduction), terminate
1206 the recursion. */
b9787581 1207 if (gphi *stmt = dyn_cast <gphi *> (stmt_info->stmt))
e7baeb39 1208 {
b161f2c9
RS
1209 tree scalar_type = TREE_TYPE (PHI_RESULT (stmt));
1210 tree vectype = get_vectype_for_scalar_type (scalar_type);
d7609678 1211 if (!vect_record_max_nunits (stmt_info, group_size, vectype, max_nunits))
b161f2c9
RS
1212 return NULL;
1213
b9787581 1214 vect_def_type def_type = STMT_VINFO_DEF_TYPE (stmt_info);
c78e3652 1215 /* Induction from different IVs is not supported. */
719488f8
RB
1216 if (def_type == vect_induction_def)
1217 {
b9787581
RS
1218 stmt_vec_info other_info;
1219 FOR_EACH_VEC_ELT (stmts, i, other_info)
1220 if (stmt_info != other_info)
719488f8
RB
1221 return NULL;
1222 }
1223 else
1224 {
1225 /* Else def types have to match. */
b9787581
RS
1226 stmt_vec_info other_info;
1227 FOR_EACH_VEC_ELT (stmts, i, other_info)
719488f8
RB
1228 {
1229 /* But for reduction chains only check on the first stmt. */
b9787581
RS
1230 if (REDUC_GROUP_FIRST_ELEMENT (other_info)
1231 && REDUC_GROUP_FIRST_ELEMENT (other_info) != stmt_info)
719488f8 1232 continue;
b9787581 1233 if (STMT_VINFO_DEF_TYPE (other_info) != def_type)
719488f8
RB
1234 return NULL;
1235 }
1236 }
e7baeb39
RB
1237 node = vect_create_new_slp_node (stmts);
1238 return node;
1239 }
1240
1241
6876e5bc 1242 bool two_operators = false;
4cecd659 1243 unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
d7609678 1244 if (!vect_build_slp_tree_1 (swap, stmts, group_size,
e403d17e
RB
1245 &this_max_nunits, matches, &two_operators))
1246 return NULL;
ebfd146a 1247
6983e6b5 1248 /* If the SLP node is a load, terminate the recursion. */
b9787581
RS
1249 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1250 && DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
ebfd146a 1251 {
e403d17e
RB
1252 *max_nunits = this_max_nunits;
1253 node = vect_create_new_slp_node (stmts);
1254 loads->safe_push (node);
1255 return node;
ebfd146a
IR
1256 }
1257
6983e6b5
RB
1258 /* Get at the operands, verifying they are compatible. */
1259 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
1260 slp_oprnd_info oprnd_info;
b9787581 1261 FOR_EACH_VEC_ELT (stmts, i, stmt_info)
6983e6b5 1262 {
4cecd659 1263 int res = vect_get_and_check_slp_defs (vinfo, &swap[i],
018b2744 1264 stmts, i, &oprnds_info);
4cecd659
BC
1265 if (res != 0)
1266 matches[(res == -1) ? 0 : i] = false;
1267 if (!matches[0])
1268 break;
6983e6b5 1269 }
b0b4483e
RB
1270 for (i = 0; i < group_size; ++i)
1271 if (!matches[i])
1272 {
1273 vect_free_oprnd_info (oprnds_info);
e403d17e 1274 return NULL;
b0b4483e 1275 }
6983e6b5 1276
e403d17e
RB
1277 auto_vec<slp_tree, 4> children;
1278 auto_vec<slp_tree> this_loads;
1279
b9787581 1280 stmt_info = stmts[0];
6983e6b5 1281
26d66f28
RB
1282 if (tree_size)
1283 max_tree_size -= *tree_size;
1284
b8698a0f 1285 /* Create SLP_TREE nodes for the definition node/s. */
9771b263 1286 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
ebfd146a 1287 {
d092494c 1288 slp_tree child;
e403d17e
RB
1289 unsigned old_nloads = this_loads.length ();
1290 unsigned old_tree_size = this_tree_size;
1291 unsigned int j;
b8698a0f 1292
e7baeb39 1293 if (oprnd_info->first_dt != vect_internal_def
c78e3652 1294 && oprnd_info->first_dt != vect_reduction_def
e7baeb39 1295 && oprnd_info->first_dt != vect_induction_def)
d092494c 1296 continue;
ebfd146a 1297
1428105c
RB
1298 if (++this_tree_size > max_tree_size)
1299 {
e403d17e 1300 FOR_EACH_VEC_ELT (children, j, child)
6e2dd807 1301 vect_free_slp_tree (child, false);
1428105c 1302 vect_free_oprnd_info (oprnds_info);
e403d17e 1303 return NULL;
1428105c
RB
1304 }
1305
e403d17e
RB
1306 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1307 group_size, &this_max_nunits,
1308 &this_loads, matches, npermutes,
1309 &this_tree_size,
1310 max_tree_size)) != NULL)
6983e6b5 1311 {
3fc356dc
RB
1312 /* If we have all children of child built up from scalars then just
1313 throw that away and build it up this node from scalars. */
995b6fe0
RB
1314 if (!SLP_TREE_CHILDREN (child).is_empty ()
1315 /* ??? Rejecting patterns this way doesn't work. We'd have to
1316 do extra work to cancel the pattern so the uses see the
1317 scalar version. */
b9787581 1318 && !is_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (child)[0]))
3fc356dc 1319 {
3fc356dc
RB
1320 slp_tree grandchild;
1321
1322 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
603cca93 1323 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
3fc356dc
RB
1324 break;
1325 if (!grandchild)
1326 {
1327 /* Roll back. */
e403d17e
RB
1328 this_loads.truncate (old_nloads);
1329 this_tree_size = old_tree_size;
3fc356dc 1330 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
6e2dd807 1331 vect_free_slp_tree (grandchild, false);
3fc356dc
RB
1332 SLP_TREE_CHILDREN (child).truncate (0);
1333
1334 dump_printf_loc (MSG_NOTE, vect_location,
1335 "Building parent vector operands from "
1336 "scalars instead\n");
1337 oprnd_info->def_stmts = vNULL;
603cca93 1338 SLP_TREE_DEF_TYPE (child) = vect_external_def;
e403d17e 1339 children.safe_push (child);
3fc356dc
RB
1340 continue;
1341 }
1342 }
1343
6983e6b5 1344 oprnd_info->def_stmts = vNULL;
e403d17e 1345 children.safe_push (child);
6983e6b5
RB
1346 continue;
1347 }
1348
90dd6e3d
RB
1349 /* If the SLP build failed fatally and we analyze a basic-block
1350 simply treat nodes we fail to build as externally defined
1351 (and thus build vectors from the scalar defs).
1352 The cost model will reject outright expensive cases.
1353 ??? This doesn't treat cases where permutation ultimatively
1354 fails (or we don't try permutation below). Ideally we'd
1355 even compute a permutation that will end up with the maximum
1356 SLP tree size... */
310213d4 1357 if (is_a <bb_vec_info> (vinfo)
90dd6e3d
RB
1358 && !matches[0]
1359 /* ??? Rejecting patterns this way doesn't work. We'd have to
1360 do extra work to cancel the pattern so the uses see the
1361 scalar version. */
b9787581 1362 && !is_pattern_stmt_p (stmt_info))
90dd6e3d
RB
1363 {
1364 dump_printf_loc (MSG_NOTE, vect_location,
1365 "Building vector operands from scalars\n");
e403d17e 1366 child = vect_create_new_slp_node (oprnd_info->def_stmts);
603cca93 1367 SLP_TREE_DEF_TYPE (child) = vect_external_def;
e403d17e
RB
1368 children.safe_push (child);
1369 oprnd_info->def_stmts = vNULL;
90dd6e3d
RB
1370 continue;
1371 }
1372
6983e6b5
RB
1373 /* If the SLP build for operand zero failed and operand zero
1374 and one can be commutated try that for the scalar stmts
1375 that failed the match. */
1376 if (i == 0
1377 /* A first scalar stmt mismatch signals a fatal mismatch. */
1378 && matches[0]
1379 /* ??? For COND_EXPRs we can swap the comparison operands
1380 as well as the arms under some constraints. */
1381 && nops == 2
1382 && oprnds_info[1]->first_dt == vect_internal_def
b9787581 1383 && is_gimple_assign (stmt_info->stmt)
6983e6b5
RB
1384 /* Do so only if the number of not successful permutes was nor more
1385 than a cut-ff as re-trying the recursive match on
1386 possibly each level of the tree would expose exponential
1387 behavior. */
1388 && *npermutes < 4)
1389 {
85c5e2f5
RB
1390 /* See whether we can swap the matching or the non-matching
1391 stmt operands. */
1392 bool swap_not_matching = true;
1393 do
1394 {
1395 for (j = 0; j < group_size; ++j)
1396 {
1397 if (matches[j] != !swap_not_matching)
1398 continue;
b9787581 1399 stmt_vec_info stmt_info = stmts[j];
85c5e2f5 1400 /* Verify if we can swap operands of this stmt. */
b9787581
RS
1401 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
1402 if (!stmt
85c5e2f5
RB
1403 || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
1404 {
1405 if (!swap_not_matching)
1406 goto fail;
1407 swap_not_matching = false;
1408 break;
1409 }
1410 /* Verify if we can safely swap or if we committed to a
1411 specific operand order already.
1412 ??? Instead of modifying GIMPLE stmts here we could
1413 record whether we want to swap operands in the SLP
1414 node and temporarily do that when processing it
1415 (or wrap operand accessors in a helper). */
1416 else if (swap[j] != 0
b9787581 1417 || STMT_VINFO_NUM_SLP_USES (stmt_info))
85c5e2f5
RB
1418 {
1419 if (!swap_not_matching)
1420 {
1421 if (dump_enabled_p ())
1422 {
1423 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1424 vect_location,
1425 "Build SLP failed: cannot swap "
1426 "operands of shared stmt ");
1427 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION,
b9787581 1428 TDF_SLIM, stmts[j]->stmt, 0);
85c5e2f5
RB
1429 }
1430 goto fail;
1431 }
1432 swap_not_matching = false;
1433 break;
1434 }
1435 }
1436 }
1437 while (j != group_size);
78810bd3 1438
6983e6b5 1439 /* Swap mismatched definition stmts. */
b0b4483e
RB
1440 dump_printf_loc (MSG_NOTE, vect_location,
1441 "Re-trying with swapped operands of stmts ");
e72baed7 1442 for (j = 0; j < group_size; ++j)
85c5e2f5 1443 if (matches[j] == !swap_not_matching)
6983e6b5 1444 {
6b4db501
MM
1445 std::swap (oprnds_info[0]->def_stmts[j],
1446 oprnds_info[1]->def_stmts[j]);
b0b4483e 1447 dump_printf (MSG_NOTE, "%d ", j);
6983e6b5 1448 }
b0b4483e 1449 dump_printf (MSG_NOTE, "\n");
74574669
RB
1450 /* And try again with scratch 'matches' ... */
1451 bool *tem = XALLOCAVEC (bool, group_size);
e403d17e
RB
1452 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1453 group_size, &this_max_nunits,
1454 &this_loads, tem, npermutes,
1455 &this_tree_size,
1456 max_tree_size)) != NULL)
6983e6b5 1457 {
60f2b864
RB
1458 /* ... so if successful we can apply the operand swapping
1459 to the GIMPLE IL. This is necessary because for example
1460 vect_get_slp_defs uses operand indexes and thus expects
1461 canonical operand order. This is also necessary even
1462 if we end up building the operand from scalars as
1463 we'll continue to process swapped operand two. */
1464 for (j = 0; j < group_size; ++j)
b9787581 1465 gimple_set_plf (stmts[j]->stmt, GF_PLF_1, false);
f47cda24 1466 for (j = 0; j < group_size; ++j)
b9787581
RS
1467 if (matches[j] == !swap_not_matching)
1468 {
1469 gassign *stmt = as_a <gassign *> (stmts[j]->stmt);
1470 /* Avoid swapping operands twice. */
1471 if (gimple_plf (stmt, GF_PLF_1))
1472 continue;
1473 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1474 gimple_assign_rhs2_ptr (stmt));
1475 gimple_set_plf (stmt, GF_PLF_1, true);
1476 }
f47cda24
RB
1477 /* Verify we swap all duplicates or none. */
1478 if (flag_checking)
1479 for (j = 0; j < group_size; ++j)
b9787581
RS
1480 gcc_assert (gimple_plf (stmts[j]->stmt, GF_PLF_1)
1481 == (matches[j] == !swap_not_matching));
60f2b864 1482
85c69b0b
RB
1483 /* If we have all children of child built up from scalars then
1484 just throw that away and build it up this node from scalars. */
995b6fe0
RB
1485 if (!SLP_TREE_CHILDREN (child).is_empty ()
1486 /* ??? Rejecting patterns this way doesn't work. We'd have
1487 to do extra work to cancel the pattern so the uses see the
1488 scalar version. */
b9787581 1489 && !is_pattern_stmt_p (SLP_TREE_SCALAR_STMTS (child)[0]))
85c69b0b
RB
1490 {
1491 unsigned int j;
1492 slp_tree grandchild;
1493
1494 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
603cca93 1495 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
85c69b0b
RB
1496 break;
1497 if (!grandchild)
1498 {
1499 /* Roll back. */
e403d17e
RB
1500 this_loads.truncate (old_nloads);
1501 this_tree_size = old_tree_size;
85c69b0b 1502 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
6e2dd807 1503 vect_free_slp_tree (grandchild, false);
85c69b0b
RB
1504 SLP_TREE_CHILDREN (child).truncate (0);
1505
1506 dump_printf_loc (MSG_NOTE, vect_location,
1507 "Building parent vector operands from "
1508 "scalars instead\n");
1509 oprnd_info->def_stmts = vNULL;
603cca93 1510 SLP_TREE_DEF_TYPE (child) = vect_external_def;
e403d17e 1511 children.safe_push (child);
85c69b0b
RB
1512 continue;
1513 }
1514 }
1515
6983e6b5 1516 oprnd_info->def_stmts = vNULL;
e403d17e 1517 children.safe_push (child);
6983e6b5
RB
1518 continue;
1519 }
1520
1521 ++*npermutes;
1522 }
1523
78810bd3 1524fail:
e403d17e
RB
1525 gcc_assert (child == NULL);
1526 FOR_EACH_VEC_ELT (children, j, child)
6e2dd807 1527 vect_free_slp_tree (child, false);
6983e6b5 1528 vect_free_oprnd_info (oprnds_info);
e403d17e 1529 return NULL;
ebfd146a
IR
1530 }
1531
e403d17e
RB
1532 vect_free_oprnd_info (oprnds_info);
1533
1428105c
RB
1534 if (tree_size)
1535 *tree_size += this_tree_size;
e403d17e
RB
1536 *max_nunits = this_max_nunits;
1537 loads->safe_splice (this_loads);
1428105c 1538
e403d17e
RB
1539 node = vect_create_new_slp_node (stmts);
1540 SLP_TREE_TWO_OPERATORS (node) = two_operators;
1541 SLP_TREE_CHILDREN (node).splice (children);
1542 return node;
ebfd146a
IR
1543}
1544
78c60e3d 1545/* Dump a slp tree NODE using flags specified in DUMP_KIND. */
ebfd146a
IR
1546
1547static void
4f5b9c80
DM
1548vect_print_slp_tree (dump_flags_t dump_kind, dump_location_t loc,
1549 slp_tree node)
ebfd146a
IR
1550{
1551 int i;
b9787581 1552 stmt_vec_info stmt_info;
d755c7ef 1553 slp_tree child;
ebfd146a 1554
603cca93
RB
1555 dump_printf_loc (dump_kind, loc, "node%s\n",
1556 SLP_TREE_DEF_TYPE (node) != vect_internal_def
1557 ? " (external)" : "");
b9787581 1558 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
ebfd146a 1559 {
c2a12ca0 1560 dump_printf_loc (dump_kind, loc, "\tstmt %d ", i);
b9787581 1561 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt_info->stmt, 0);
ebfd146a 1562 }
9771b263 1563 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
c2a12ca0 1564 vect_print_slp_tree (dump_kind, loc, child);
ebfd146a
IR
1565}
1566
1567
b8698a0f
L
1568/* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1569 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
ff802fa1 1570 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
ebfd146a
IR
1571 stmts in NODE are to be marked. */
1572
1573static void
1574vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1575{
1576 int i;
b9787581 1577 stmt_vec_info stmt_info;
d755c7ef 1578 slp_tree child;
ebfd146a 1579
603cca93 1580 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
ebfd146a
IR
1581 return;
1582
b9787581 1583 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
ebfd146a 1584 if (j < 0 || i == j)
b9787581 1585 STMT_SLP_TYPE (stmt_info) = mark;
ebfd146a 1586
9771b263 1587 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
d755c7ef 1588 vect_mark_slp_stmts (child, mark, j);
ebfd146a
IR
1589}
1590
1591
a70d6342
IR
1592/* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1593
1594static void
1595vect_mark_slp_stmts_relevant (slp_tree node)
1596{
1597 int i;
a70d6342 1598 stmt_vec_info stmt_info;
d755c7ef 1599 slp_tree child;
a70d6342 1600
603cca93 1601 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
a70d6342
IR
1602 return;
1603
b9787581 1604 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
a70d6342 1605 {
b8698a0f 1606 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
a70d6342
IR
1607 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1608 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1609 }
1610
9771b263 1611 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
d755c7ef 1612 vect_mark_slp_stmts_relevant (child);
a70d6342
IR
1613}
1614
1615
b5aeb3bb
IR
1616/* Rearrange the statements of NODE according to PERMUTATION. */
1617
1618static void
1619vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
01d8bf07 1620 vec<unsigned> permutation)
b5aeb3bb 1621{
b9787581
RS
1622 stmt_vec_info stmt_info;
1623 vec<stmt_vec_info> tmp_stmts;
d755c7ef
RB
1624 unsigned int i;
1625 slp_tree child;
b5aeb3bb 1626
9771b263 1627 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
d755c7ef 1628 vect_slp_rearrange_stmts (child, group_size, permutation);
b5aeb3bb 1629
9771b263
DN
1630 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1631 tmp_stmts.create (group_size);
d755c7ef 1632 tmp_stmts.quick_grow_cleared (group_size);
b5aeb3bb 1633
b9787581
RS
1634 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
1635 tmp_stmts[permutation[i]] = stmt_info;
b5aeb3bb 1636
9771b263 1637 SLP_TREE_SCALAR_STMTS (node).release ();
b5aeb3bb
IR
1638 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1639}
1640
1641
b266b968
RB
1642/* Attempt to reorder stmts in a reduction chain so that we don't
1643 require any load permutation. Return true if that was possible,
1644 otherwise return false. */
1645
1646static bool
1647vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1648{
1649 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1650 unsigned int i, j;
b266b968
RB
1651 unsigned int lidx;
1652 slp_tree node, load;
1653
1654 /* Compare all the permutation sequences to the first one. We know
1655 that at least one load is permuted. */
1656 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1657 if (!node->load_permutation.exists ())
1658 return false;
1659 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1660 {
1661 if (!load->load_permutation.exists ())
1662 return false;
1663 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1664 if (lidx != node->load_permutation[j])
1665 return false;
1666 }
1667
1668 /* Check that the loads in the first sequence are different and there
1669 are no gaps between them. */
7ba9e72d 1670 auto_sbitmap load_index (group_size);
b266b968
RB
1671 bitmap_clear (load_index);
1672 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1673 {
41eefe13 1674 if (lidx >= group_size)
7ba9e72d 1675 return false;
b266b968 1676 if (bitmap_bit_p (load_index, lidx))
7ba9e72d
TS
1677 return false;
1678
b266b968
RB
1679 bitmap_set_bit (load_index, lidx);
1680 }
1681 for (i = 0; i < group_size; i++)
1682 if (!bitmap_bit_p (load_index, i))
7ba9e72d 1683 return false;
b266b968
RB
1684
1685 /* This permutation is valid for reduction. Since the order of the
1686 statements in the nodes is not important unless they are memory
1687 accesses, we can rearrange the statements in all the nodes
1688 according to the order of the loads. */
1689 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1690 node->load_permutation);
1691
1692 /* We are done, no actual permutations need to be generated. */
d9f21f6a 1693 poly_uint64 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_instn);
b266b968 1694 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
c4e360f4 1695 {
b9787581 1696 stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
bffb8014 1697 first_stmt_info = DR_GROUP_FIRST_ELEMENT (first_stmt_info);
c4e360f4
RB
1698 /* But we have to keep those permutations that are required because
1699 of handling of gaps. */
d9f21f6a 1700 if (known_eq (unrolling_factor, 1U)
b9787581
RS
1701 || (group_size == DR_GROUP_SIZE (first_stmt_info)
1702 && DR_GROUP_GAP (first_stmt_info) == 0))
c4e360f4 1703 SLP_TREE_LOAD_PERMUTATION (node).release ();
cbd400b4
RB
1704 else
1705 for (j = 0; j < SLP_TREE_LOAD_PERMUTATION (node).length (); ++j)
1706 SLP_TREE_LOAD_PERMUTATION (node)[j] = j;
c4e360f4
RB
1707 }
1708
b266b968
RB
1709 return true;
1710}
1711
01d8bf07
RB
1712/* Check if the required load permutations in the SLP instance
1713 SLP_INSTN are supported. */
ebfd146a
IR
1714
1715static bool
01d8bf07 1716vect_supported_load_permutation_p (slp_instance slp_instn)
ebfd146a 1717{
01d8bf07
RB
1718 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1719 unsigned int i, j, k, next;
6983e6b5 1720 slp_tree node;
ebfd146a 1721
73fbfcad 1722 if (dump_enabled_p ())
ebfd146a 1723 {
78c60e3d 1724 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
01d8bf07
RB
1725 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1726 if (node->load_permutation.exists ())
1727 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1728 dump_printf (MSG_NOTE, "%d ", next);
1729 else
bddc974e
TJ
1730 for (k = 0; k < group_size; ++k)
1731 dump_printf (MSG_NOTE, "%d ", k);
e645e942 1732 dump_printf (MSG_NOTE, "\n");
ebfd146a
IR
1733 }
1734
b5aeb3bb
IR
1735 /* In case of reduction every load permutation is allowed, since the order
1736 of the reduction statements is not important (as opposed to the case of
0d0293ac 1737 grouped stores). The only condition we need to check is that all the
b5aeb3bb
IR
1738 load nodes are of the same size and have the same permutation (and then
1739 rearrange all the nodes of the SLP instance according to this
1740 permutation). */
1741
1742 /* Check that all the load nodes are of the same size. */
01d8bf07 1743 /* ??? Can't we assert this? */
9771b263 1744 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
6983e6b5
RB
1745 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1746 return false;
2200fc49 1747
b5aeb3bb 1748 node = SLP_INSTANCE_TREE (slp_instn);
b9787581 1749 stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
b5aeb3bb 1750
b010117a 1751 /* Reduction (there are no data-refs in the root).
b266b968 1752 In reduction chain the order of the loads is not important. */
b9787581
RS
1753 if (!STMT_VINFO_DATA_REF (stmt_info)
1754 && !REDUC_GROUP_FIRST_ELEMENT (stmt_info))
c4e360f4 1755 vect_attempt_slp_rearrange_stmts (slp_instn);
b5aeb3bb 1756
6aa904c4
IR
1757 /* In basic block vectorization we allow any subchain of an interleaving
1758 chain.
1759 FORNOW: not supported in loop SLP because of realignment compications. */
b9787581 1760 if (STMT_VINFO_BB_VINFO (stmt_info))
6aa904c4 1761 {
240a94da
RB
1762 /* Check whether the loads in an instance form a subchain and thus
1763 no permutation is necessary. */
9771b263 1764 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
6aa904c4 1765 {
9626d143
RB
1766 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1767 continue;
240a94da 1768 bool subchain_p = true;
bffb8014 1769 stmt_vec_info next_load_info = NULL;
b9787581
RS
1770 stmt_vec_info load_info;
1771 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load_info)
1772 {
1773 if (j != 0
bffb8014 1774 && (next_load_info != load_info
b9787581 1775 || DR_GROUP_GAP (load_info) != 1))
240a94da
RB
1776 {
1777 subchain_p = false;
1778 break;
1779 }
bffb8014 1780 next_load_info = DR_GROUP_NEXT_ELEMENT (load_info);
b9787581 1781 }
240a94da
RB
1782 if (subchain_p)
1783 SLP_TREE_LOAD_PERMUTATION (node).release ();
1784 else
1785 {
b9787581 1786 stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (node)[0];
bffb8014 1787 group_info = DR_GROUP_FIRST_ELEMENT (group_info);
928686b1 1788 unsigned HOST_WIDE_INT nunits;
fe73a332
RB
1789 unsigned k, maxk = 0;
1790 FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), j, k)
1791 if (k > maxk)
1792 maxk = k;
1793 /* In BB vectorization we may not actually use a loaded vector
2c53b149 1794 accessing elements in excess of DR_GROUP_SIZE. */
928686b1
RS
1795 tree vectype = STMT_VINFO_VECTYPE (group_info);
1796 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
2c53b149 1797 || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
fe73a332
RB
1798 {
1799 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1800 "BB vectorization with gaps at the end of "
1801 "a load is not supported\n");
1802 return false;
1803 }
1804
240a94da
RB
1805 /* Verify the permutation can be generated. */
1806 vec<tree> tem;
29afecdf 1807 unsigned n_perms;
240a94da 1808 if (!vect_transform_slp_perm_load (node, tem, NULL,
29afecdf 1809 1, slp_instn, true, &n_perms))
240a94da
RB
1810 {
1811 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1812 vect_location,
1813 "unsupported load permutation\n");
1814 return false;
1815 }
1816 }
6aa904c4 1817 }
01d8bf07 1818 return true;
6aa904c4
IR
1819 }
1820
31bee964
RB
1821 /* For loop vectorization verify we can generate the permutation. Be
1822 conservative about the vectorization factor, there are permutations
1823 that will use three vector inputs only starting from a specific factor
1824 and the vectorization factor is not yet final.
1825 ??? The SLP instance unrolling factor might not be the maximum one. */
29afecdf 1826 unsigned n_perms;
d9f21f6a
RS
1827 poly_uint64 test_vf
1828 = force_common_multiple (SLP_INSTANCE_UNROLLING_FACTOR (slp_instn),
31bee964 1829 LOOP_VINFO_VECT_FACTOR
b9787581 1830 (STMT_VINFO_LOOP_VINFO (stmt_info)));
01d8bf07
RB
1831 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1832 if (node->load_permutation.exists ()
31bee964
RB
1833 && !vect_transform_slp_perm_load (node, vNULL, NULL, test_vf,
1834 slp_instn, true, &n_perms))
01d8bf07 1835 return false;
9b999e8c 1836
01d8bf07 1837 return true;
ebfd146a
IR
1838}
1839
1840
e4a707c4 1841/* Find the last store in SLP INSTANCE. */
ff802fa1 1842
95c68311 1843stmt_vec_info
2e8ab70c 1844vect_find_last_scalar_stmt_in_slp (slp_tree node)
e4a707c4 1845{
95c68311 1846 stmt_vec_info last = NULL;
b9787581 1847 stmt_vec_info stmt_vinfo;
e4a707c4 1848
b9787581 1849 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt_vinfo); i++)
2e8ab70c 1850 {
2e8ab70c 1851 if (is_pattern_stmt_p (stmt_vinfo))
95c68311
RS
1852 stmt_vinfo = STMT_VINFO_RELATED_STMT (stmt_vinfo);
1853 last = last ? get_later_stmt (stmt_vinfo, last) : stmt_vinfo;
2e8ab70c 1854 }
e4a707c4 1855
2e8ab70c 1856 return last;
e4a707c4
IR
1857}
1858
82570274
RS
1859/* Splits a group of stores, currently beginning at FIRST_VINFO, into
1860 two groups: one (still beginning at FIRST_VINFO) of size GROUP1_SIZE
1861 (also containing the first GROUP1_SIZE stmts, since stores are
1862 consecutive), the second containing the remainder.
1ba91a49
AL
1863 Return the first stmt in the second group. */
1864
82570274
RS
1865static stmt_vec_info
1866vect_split_slp_store_group (stmt_vec_info first_vinfo, unsigned group1_size)
1ba91a49 1867{
bffb8014 1868 gcc_assert (DR_GROUP_FIRST_ELEMENT (first_vinfo) == first_vinfo);
1ba91a49 1869 gcc_assert (group1_size > 0);
2c53b149 1870 int group2_size = DR_GROUP_SIZE (first_vinfo) - group1_size;
1ba91a49 1871 gcc_assert (group2_size > 0);
2c53b149 1872 DR_GROUP_SIZE (first_vinfo) = group1_size;
1ba91a49 1873
bffb8014 1874 stmt_vec_info stmt_info = first_vinfo;
1ba91a49
AL
1875 for (unsigned i = group1_size; i > 1; i--)
1876 {
bffb8014
RS
1877 stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
1878 gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
1ba91a49
AL
1879 }
1880 /* STMT is now the last element of the first group. */
bffb8014
RS
1881 stmt_vec_info group2 = DR_GROUP_NEXT_ELEMENT (stmt_info);
1882 DR_GROUP_NEXT_ELEMENT (stmt_info) = 0;
1ba91a49 1883
bffb8014
RS
1884 DR_GROUP_SIZE (group2) = group2_size;
1885 for (stmt_info = group2; stmt_info;
1886 stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
1ba91a49 1887 {
bffb8014
RS
1888 DR_GROUP_FIRST_ELEMENT (stmt_info) = group2;
1889 gcc_assert (DR_GROUP_GAP (stmt_info) == 1);
1ba91a49
AL
1890 }
1891
2c53b149 1892 /* For the second group, the DR_GROUP_GAP is that before the original group,
1ba91a49 1893 plus skipping over the first vector. */
bffb8014 1894 DR_GROUP_GAP (group2) = DR_GROUP_GAP (first_vinfo) + group1_size;
1ba91a49 1895
2c53b149
RB
1896 /* DR_GROUP_GAP of the first group now has to skip over the second group too. */
1897 DR_GROUP_GAP (first_vinfo) += group2_size;
1ba91a49
AL
1898
1899 if (dump_enabled_p ())
1900 dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
1901 group1_size, group2_size);
1902
1903 return group2;
1904}
1905
4b6068ea
RS
1906/* Calculate the unrolling factor for an SLP instance with GROUP_SIZE
1907 statements and a vector of NUNITS elements. */
1908
1909static poly_uint64
1910calculate_unrolling_factor (poly_uint64 nunits, unsigned int group_size)
1911{
1912 return exact_div (common_multiple (nunits, group_size), group_size);
1913}
1914
0d0293ac 1915/* Analyze an SLP instance starting from a group of grouped stores. Call
b8698a0f 1916 vect_build_slp_tree to build a tree of packed stmts if possible.
ebfd146a
IR
1917 Return FALSE if it's impossible to SLP any stmt in the loop. */
1918
1919static bool
310213d4 1920vect_analyze_slp_instance (vec_info *vinfo,
32e8e429 1921 stmt_vec_info stmt_info, unsigned max_tree_size)
ebfd146a
IR
1922{
1923 slp_instance new_instance;
d092494c 1924 slp_tree node;
2c53b149 1925 unsigned int group_size;
b5aeb3bb 1926 tree vectype, scalar_type = NULL_TREE;
1ba91a49 1927 unsigned int i;
9771b263 1928 vec<slp_tree> loads;
b9787581
RS
1929 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1930 vec<stmt_vec_info> scalar_stmts;
b5aeb3bb 1931
b9787581 1932 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
b5aeb3bb 1933 {
2c53b149
RB
1934 scalar_type = TREE_TYPE (DR_REF (dr));
1935 vectype = get_vectype_for_scalar_type (scalar_type);
b9787581 1936 group_size = DR_GROUP_SIZE (stmt_info);
2c53b149 1937 }
b9787581 1938 else if (!dr && REDUC_GROUP_FIRST_ELEMENT (stmt_info))
2c53b149
RB
1939 {
1940 gcc_assert (is_a <loop_vec_info> (vinfo));
b9787581
RS
1941 vectype = STMT_VINFO_VECTYPE (stmt_info);
1942 group_size = REDUC_GROUP_SIZE (stmt_info);
b5aeb3bb
IR
1943 }
1944 else
1945 {
310213d4 1946 gcc_assert (is_a <loop_vec_info> (vinfo));
b9787581 1947 vectype = STMT_VINFO_VECTYPE (stmt_info);
310213d4 1948 group_size = as_a <loop_vec_info> (vinfo)->reductions.length ();
b5aeb3bb 1949 }
b8698a0f 1950
ebfd146a
IR
1951 if (!vectype)
1952 {
73fbfcad 1953 if (dump_enabled_p ())
ebfd146a 1954 {
78c60e3d
SS
1955 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1956 "Build SLP failed: unsupported data-type ");
1957 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
e645e942 1958 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
ebfd146a 1959 }
b5aeb3bb 1960
ebfd146a
IR
1961 return false;
1962 }
4b6068ea 1963 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
a70d6342 1964
0d0293ac 1965 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
9771b263 1966 scalar_stmts.create (group_size);
bffb8014 1967 stmt_vec_info next_info = stmt_info;
b9787581 1968 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a 1969 {
b5aeb3bb 1970 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
bffb8014 1971 while (next_info)
b5aeb3bb 1972 {
b9787581
RS
1973 if (STMT_VINFO_IN_PATTERN_P (next_info)
1974 && STMT_VINFO_RELATED_STMT (next_info))
1975 scalar_stmts.safe_push (STMT_VINFO_RELATED_STMT (next_info));
f7e531cf 1976 else
b9787581 1977 scalar_stmts.safe_push (next_info);
bffb8014 1978 next_info = DR_GROUP_NEXT_ELEMENT (next_info);
2c53b149
RB
1979 }
1980 }
b9787581 1981 else if (!dr && REDUC_GROUP_FIRST_ELEMENT (stmt_info))
2c53b149
RB
1982 {
1983 /* Collect the reduction stmts and store them in
1984 SLP_TREE_SCALAR_STMTS. */
bffb8014 1985 while (next_info)
2c53b149 1986 {
b9787581
RS
1987 if (STMT_VINFO_IN_PATTERN_P (next_info)
1988 && STMT_VINFO_RELATED_STMT (next_info))
1989 scalar_stmts.safe_push (STMT_VINFO_RELATED_STMT (next_info));
2c53b149 1990 else
b9787581 1991 scalar_stmts.safe_push (next_info);
bffb8014 1992 next_info = REDUC_GROUP_NEXT_ELEMENT (next_info);
b5aeb3bb 1993 }
14a61437
RB
1994 /* Mark the first element of the reduction chain as reduction to properly
1995 transform the node. In the reduction analysis phase only the last
1996 element of the chain is marked as reduction. */
b9787581 1997 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
b5aeb3bb
IR
1998 }
1999 else
2000 {
2001 /* Collect reduction statements. */
32c91dfc
RS
2002 vec<stmt_vec_info> reductions = as_a <loop_vec_info> (vinfo)->reductions;
2003 for (i = 0; reductions.iterate (i, &next_info); i++)
2004 scalar_stmts.safe_push (next_info);
ebfd146a
IR
2005 }
2006
9771b263 2007 loads.create (group_size);
ebfd146a
IR
2008
2009 /* Build the tree for the SLP instance. */
89d390e5
RB
2010 bool *matches = XALLOCAVEC (bool, group_size);
2011 unsigned npermutes = 0;
f7300fff 2012 bst_fail = new scalar_stmts_set_t ();
4b6068ea 2013 poly_uint64 max_nunits = nunits;
e569db5f 2014 node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
4b6068ea 2015 &max_nunits, &loads, matches, &npermutes,
e569db5f 2016 NULL, max_tree_size);
26d66f28 2017 delete bst_fail;
e569db5f 2018 if (node != NULL)
ebfd146a 2019 {
4ef69dfc 2020 /* Calculate the unrolling factor based on the smallest type. */
d9f21f6a 2021 poly_uint64 unrolling_factor
4b6068ea 2022 = calculate_unrolling_factor (max_nunits, group_size);
b8698a0f 2023
d9f21f6a 2024 if (maybe_ne (unrolling_factor, 1U)
e569db5f
VK
2025 && is_a <bb_vec_info> (vinfo))
2026 {
4b6068ea
RS
2027 unsigned HOST_WIDE_INT const_max_nunits;
2028 if (!max_nunits.is_constant (&const_max_nunits)
2029 || const_max_nunits > group_size)
2030 {
2031 if (dump_enabled_p ())
2032 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2033 "Build SLP failed: store group "
2034 "size not a multiple of the vector size "
2035 "in basic block SLP\n");
6e2dd807 2036 vect_free_slp_tree (node, false);
4b6068ea
RS
2037 loads.release ();
2038 return false;
2039 }
e569db5f 2040 /* Fatal mismatch. */
4b6068ea 2041 matches[group_size / const_max_nunits * const_max_nunits] = false;
6e2dd807 2042 vect_free_slp_tree (node, false);
e569db5f
VK
2043 loads.release ();
2044 }
2045 else
2046 {
4ef69dfc
IR
2047 /* Create a new SLP instance. */
2048 new_instance = XNEW (struct _slp_instance);
2049 SLP_INSTANCE_TREE (new_instance) = node;
2050 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
ebfd146a 2051 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
ebfd146a 2052 SLP_INSTANCE_LOADS (new_instance) = loads;
abf9bfbc
RB
2053
2054 /* Compute the load permutation. */
2055 slp_tree load_node;
2056 bool loads_permuted = false;
abf9bfbc
RB
2057 FOR_EACH_VEC_ELT (loads, i, load_node)
2058 {
01d8bf07 2059 vec<unsigned> load_permutation;
abf9bfbc 2060 int j;
b9787581 2061 stmt_vec_info load_info;
01d8bf07
RB
2062 bool this_load_permuted = false;
2063 load_permutation.create (group_size);
bffb8014 2064 stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT
b9787581
RS
2065 (SLP_TREE_SCALAR_STMTS (load_node)[0]);
2066 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load_info)
abf9bfbc 2067 {
b9787581 2068 int load_place = vect_get_place_in_interleaving_chain
bffb8014 2069 (load_info, first_stmt_info);
6983e6b5
RB
2070 gcc_assert (load_place != -1);
2071 if (load_place != j)
01d8bf07 2072 this_load_permuted = true;
abf9bfbc
RB
2073 load_permutation.safe_push (load_place);
2074 }
fe2bef71
RB
2075 if (!this_load_permuted
2076 /* The load requires permutation when unrolling exposes
2077 a gap either because the group is larger than the SLP
2078 group-size or because there is a gap between the groups. */
d9f21f6a 2079 && (known_eq (unrolling_factor, 1U)
bffb8014
RS
2080 || (group_size == DR_GROUP_SIZE (first_stmt_info)
2081 && DR_GROUP_GAP (first_stmt_info) == 0)))
01d8bf07
RB
2082 {
2083 load_permutation.release ();
2084 continue;
2085 }
2086 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
2087 loads_permuted = true;
abf9bfbc 2088 }
6aa904c4
IR
2089
2090 if (loads_permuted)
ebfd146a 2091 {
01d8bf07 2092 if (!vect_supported_load_permutation_p (new_instance))
ebfd146a 2093 {
73fbfcad 2094 if (dump_enabled_p ())
ebfd146a 2095 {
e645e942 2096 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
78c60e3d
SS
2097 "Build SLP failed: unsupported load "
2098 "permutation ");
86a91c0a
RS
2099 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION,
2100 TDF_SLIM, stmt_info->stmt, 0);
ebfd146a 2101 }
6e2dd807 2102 vect_free_slp_instance (new_instance, false);
ebfd146a
IR
2103 return false;
2104 }
ebfd146a 2105 }
ebfd146a 2106
e569db5f 2107 /* If the loads and stores can be handled with load/store-lan
bb0f5ca7
AL
2108 instructions do not generate this SLP instance. */
2109 if (is_a <loop_vec_info> (vinfo)
2110 && loads_permuted
7e11fc7f 2111 && dr && vect_store_lanes_supported (vectype, group_size, false))
bb0f5ca7
AL
2112 {
2113 slp_tree load_node;
2114 FOR_EACH_VEC_ELT (loads, i, load_node)
2115 {
bffb8014 2116 stmt_vec_info stmt_vinfo = DR_GROUP_FIRST_ELEMENT
b9787581 2117 (SLP_TREE_SCALAR_STMTS (load_node)[0]);
bffb8014 2118 /* Use SLP for strided accesses (or if we can't load-lanes). */
bb0f5ca7
AL
2119 if (STMT_VINFO_STRIDED_P (stmt_vinfo)
2120 || ! vect_load_lanes_supported
2121 (STMT_VINFO_VECTYPE (stmt_vinfo),
2c53b149 2122 DR_GROUP_SIZE (stmt_vinfo), false))
bb0f5ca7
AL
2123 break;
2124 }
2125 if (i == loads.length ())
2126 {
2127 if (dump_enabled_p ())
2128 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2129 "Built SLP cancelled: can use "
2130 "load/store-lanes\n");
6e2dd807 2131 vect_free_slp_instance (new_instance, false);
bb0f5ca7
AL
2132 return false;
2133 }
2134 }
2135
310213d4 2136 vinfo->slp_instances.safe_push (new_instance);
b8698a0f 2137
73fbfcad 2138 if (dump_enabled_p ())
c2a12ca0
RB
2139 {
2140 dump_printf_loc (MSG_NOTE, vect_location,
2141 "Final SLP tree for instance:\n");
2142 vect_print_slp_tree (MSG_NOTE, vect_location, node);
2143 }
ebfd146a
IR
2144
2145 return true;
2146 }
e569db5f
VK
2147 }
2148 else
2149 {
ebfd146a
IR
2150 /* Failed to SLP. */
2151 /* Free the allocated memory. */
e403d17e 2152 scalar_stmts.release ();
9771b263 2153 loads.release ();
e569db5f 2154 }
b8698a0f 2155
1ba91a49 2156 /* For basic block SLP, try to break the group up into multiples of the
97a1a642 2157 vector size. */
4b6068ea 2158 unsigned HOST_WIDE_INT const_nunits;
1ba91a49 2159 if (is_a <bb_vec_info> (vinfo)
91987857
RS
2160 && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2161 && DR_GROUP_FIRST_ELEMENT (stmt_info)
4b6068ea 2162 && nunits.is_constant (&const_nunits))
1ba91a49
AL
2163 {
2164 /* We consider breaking the group only on VF boundaries from the existing
2165 start. */
2166 for (i = 0; i < group_size; i++)
2167 if (!matches[i]) break;
2168
4b6068ea 2169 if (i >= const_nunits && i < group_size)
1ba91a49
AL
2170 {
2171 /* Split into two groups at the first vector boundary before i. */
4b6068ea
RS
2172 gcc_assert ((const_nunits & (const_nunits - 1)) == 0);
2173 unsigned group1_size = i & ~(const_nunits - 1);
1ba91a49 2174
82570274
RS
2175 stmt_vec_info rest = vect_split_slp_store_group (stmt_info,
2176 group1_size);
86a91c0a
RS
2177 bool res = vect_analyze_slp_instance (vinfo, stmt_info,
2178 max_tree_size);
1ba91a49
AL
2179 /* If the first non-match was in the middle of a vector,
2180 skip the rest of that vector. */
2181 if (group1_size < i)
2182 {
4b6068ea 2183 i = group1_size + const_nunits;
1ba91a49 2184 if (i < group_size)
4b6068ea 2185 rest = vect_split_slp_store_group (rest, const_nunits);
1ba91a49
AL
2186 }
2187 if (i < group_size)
2188 res |= vect_analyze_slp_instance (vinfo, rest, max_tree_size);
2189 return res;
2190 }
2191 /* Even though the first vector did not all match, we might be able to SLP
2192 (some) of the remainder. FORNOW ignore this possibility. */
2193 }
2194
a70d6342 2195 return false;
ebfd146a
IR
2196}
2197
2198
ff802fa1 2199/* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
ebfd146a
IR
2200 trees of packed scalar stmts if SLP is possible. */
2201
2202bool
310213d4 2203vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size)
ebfd146a
IR
2204{
2205 unsigned int i;
f698fccf 2206 stmt_vec_info first_element;
ebfd146a 2207
adac3a68 2208 DUMP_VECT_SCOPE ("vect_analyze_slp");
ebfd146a 2209
0d0293ac 2210 /* Find SLP sequences starting from groups of grouped stores. */
310213d4 2211 FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
0630a4ec 2212 vect_analyze_slp_instance (vinfo, first_element, max_tree_size);
ebfd146a 2213
310213d4 2214 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
b010117a 2215 {
310213d4
RB
2216 if (loop_vinfo->reduction_chains.length () > 0)
2217 {
2218 /* Find SLP sequences starting from reduction chains. */
2219 FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
0630a4ec 2220 if (! vect_analyze_slp_instance (vinfo, first_element,
310213d4 2221 max_tree_size))
6b5e165b
RB
2222 {
2223 /* Dissolve reduction chain group. */
f698fccf
RS
2224 stmt_vec_info vinfo = first_element;
2225 while (vinfo)
6b5e165b 2226 {
bffb8014 2227 stmt_vec_info next = REDUC_GROUP_NEXT_ELEMENT (vinfo);
2c53b149
RB
2228 REDUC_GROUP_FIRST_ELEMENT (vinfo) = NULL;
2229 REDUC_GROUP_NEXT_ELEMENT (vinfo) = NULL;
f698fccf 2230 vinfo = next;
6b5e165b 2231 }
f698fccf 2232 STMT_VINFO_DEF_TYPE (first_element) = vect_internal_def;
6b5e165b 2233 }
310213d4 2234 }
b010117a 2235
310213d4 2236 /* Find SLP sequences starting from groups of reductions. */
0630a4ec
RB
2237 if (loop_vinfo->reductions.length () > 1)
2238 vect_analyze_slp_instance (vinfo, loop_vinfo->reductions[0],
2239 max_tree_size);
310213d4 2240 }
b5aeb3bb 2241
ebfd146a
IR
2242 return true;
2243}
2244
2245
2246/* For each possible SLP instance decide whether to SLP it and calculate overall
437f4a00
IR
2247 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
2248 least one instance. */
ebfd146a 2249
437f4a00 2250bool
ebfd146a
IR
2251vect_make_slp_decision (loop_vec_info loop_vinfo)
2252{
d9f21f6a
RS
2253 unsigned int i;
2254 poly_uint64 unrolling_factor = 1;
9771b263 2255 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
ebfd146a
IR
2256 slp_instance instance;
2257 int decided_to_slp = 0;
2258
adac3a68 2259 DUMP_VECT_SCOPE ("vect_make_slp_decision");
ebfd146a 2260
9771b263 2261 FOR_EACH_VEC_ELT (slp_instances, i, instance)
ebfd146a
IR
2262 {
2263 /* FORNOW: SLP if you can. */
d9f21f6a
RS
2264 /* All unroll factors have the form current_vector_size * X for some
2265 rational X, so they must have a common multiple. */
2266 unrolling_factor
2267 = force_common_multiple (unrolling_factor,
2268 SLP_INSTANCE_UNROLLING_FACTOR (instance));
ebfd146a 2269
ff802fa1 2270 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
b8698a0f 2271 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
ff802fa1 2272 loop-based vectorization. Such stmts will be marked as HYBRID. */
ebfd146a
IR
2273 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2274 decided_to_slp++;
2275 }
2276
2277 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
2278
73fbfcad 2279 if (decided_to_slp && dump_enabled_p ())
d9f21f6a
RS
2280 {
2281 dump_printf_loc (MSG_NOTE, vect_location,
2282 "Decided to SLP %d instances. Unrolling factor ",
2283 decided_to_slp);
2284 dump_dec (MSG_NOTE, unrolling_factor);
2285 dump_printf (MSG_NOTE, "\n");
2286 }
437f4a00
IR
2287
2288 return (decided_to_slp > 0);
ebfd146a
IR
2289}
2290
2291
2292/* Find stmts that must be both vectorized and SLPed (since they feed stmts that
ff802fa1 2293 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
ebfd146a
IR
2294
2295static void
642fce57 2296vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
ebfd146a 2297{
b9787581 2298 stmt_vec_info stmt_vinfo = SLP_TREE_SCALAR_STMTS (node)[i];
ebfd146a 2299 imm_use_iterator imm_iter;
355fe088 2300 gimple *use_stmt;
b9787581 2301 stmt_vec_info use_vinfo;
d755c7ef 2302 slp_tree child;
f2c74cc4 2303 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
642fce57
RB
2304 int j;
2305
2306 /* Propagate hybrid down the SLP tree. */
2307 if (stype == hybrid)
2308 ;
2309 else if (HYBRID_SLP_STMT (stmt_vinfo))
2310 stype = hybrid;
2311 else
2312 {
2313 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
2314 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
2935d994
RB
2315 /* If we get a pattern stmt here we have to use the LHS of the
2316 original stmt for immediate uses. */
b9787581 2317 gimple *stmt = stmt_vinfo->stmt;
2935d994
RB
2318 if (! STMT_VINFO_IN_PATTERN_P (stmt_vinfo)
2319 && STMT_VINFO_RELATED_STMT (stmt_vinfo))
10681ce8 2320 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo)->stmt;
e7baeb39
RB
2321 tree def;
2322 if (gimple_code (stmt) == GIMPLE_PHI)
2323 def = gimple_phi_result (stmt);
2324 else
2325 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
2326 if (def)
2327 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
29764870 2328 {
6585ff8f
RS
2329 use_vinfo = loop_vinfo->lookup_stmt (use_stmt);
2330 if (!use_vinfo)
29764870 2331 continue;
29764870
RB
2332 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
2333 && STMT_VINFO_RELATED_STMT (use_vinfo))
10681ce8 2334 use_vinfo = STMT_VINFO_RELATED_STMT (use_vinfo);
29764870
RB
2335 if (!STMT_SLP_TYPE (use_vinfo)
2336 && (STMT_VINFO_RELEVANT (use_vinfo)
2337 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2338 && !(gimple_code (use_stmt) == GIMPLE_PHI
2339 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
502f0263
RB
2340 {
2341 if (dump_enabled_p ())
2342 {
2343 dump_printf_loc (MSG_NOTE, vect_location, "use of SLP "
2344 "def in non-SLP stmt: ");
2345 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0);
2346 }
2347 stype = hybrid;
2348 }
29764870 2349 }
642fce57 2350 }
ebfd146a 2351
502f0263
RB
2352 if (stype == hybrid
2353 && !HYBRID_SLP_STMT (stmt_vinfo))
b1af7da6
RB
2354 {
2355 if (dump_enabled_p ())
2356 {
2357 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
b9787581 2358 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt_vinfo->stmt, 0);
b1af7da6
RB
2359 }
2360 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2361 }
ebfd146a 2362
642fce57 2363 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
603cca93 2364 if (SLP_TREE_DEF_TYPE (child) != vect_external_def)
90dd6e3d 2365 vect_detect_hybrid_slp_stmts (child, i, stype);
642fce57 2366}
f2c74cc4 2367
642fce57 2368/* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
ebfd146a 2369
642fce57
RB
2370static tree
2371vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2372{
2373 walk_stmt_info *wi = (walk_stmt_info *)data;
6585ff8f 2374 loop_vec_info loop_vinfo = (loop_vec_info) wi->info;
642fce57
RB
2375
2376 if (wi->is_lhs)
2377 return NULL_TREE;
2378
c98d0595
RS
2379 stmt_vec_info def_stmt_info = loop_vinfo->lookup_def (*tp);
2380 if (def_stmt_info && PURE_SLP_STMT (def_stmt_info))
642fce57 2381 {
6585ff8f 2382 if (dump_enabled_p ())
b1af7da6 2383 {
6585ff8f
RS
2384 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2385 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt_info->stmt, 0);
b1af7da6 2386 }
6585ff8f 2387 STMT_SLP_TYPE (def_stmt_info) = hybrid;
642fce57
RB
2388 }
2389
2390 return NULL_TREE;
ebfd146a
IR
2391}
2392
642fce57
RB
2393static tree
2394vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
6585ff8f 2395 walk_stmt_info *wi)
642fce57 2396{
6585ff8f
RS
2397 loop_vec_info loop_vinfo = (loop_vec_info) wi->info;
2398 stmt_vec_info use_vinfo = loop_vinfo->lookup_stmt (gsi_stmt (*gsi));
642fce57
RB
2399 /* If the stmt is in a SLP instance then this isn't a reason
2400 to mark use definitions in other SLP instances as hybrid. */
9186a9d3
RB
2401 if (! STMT_SLP_TYPE (use_vinfo)
2402 && (STMT_VINFO_RELEVANT (use_vinfo)
2403 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2404 && ! (gimple_code (gsi_stmt (*gsi)) == GIMPLE_PHI
2405 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2406 ;
2407 else
642fce57
RB
2408 *handled = true;
2409 return NULL_TREE;
2410}
ebfd146a
IR
2411
2412/* Find stmts that must be both vectorized and SLPed. */
2413
2414void
2415vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2416{
2417 unsigned int i;
9771b263 2418 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
ebfd146a
IR
2419 slp_instance instance;
2420
adac3a68 2421 DUMP_VECT_SCOPE ("vect_detect_hybrid_slp");
ebfd146a 2422
642fce57
RB
2423 /* First walk all pattern stmt in the loop and mark defs of uses as
2424 hybrid because immediate uses in them are not recorded. */
2425 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2426 {
2427 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2428 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2429 gsi_next (&gsi))
2430 {
355fe088 2431 gimple *stmt = gsi_stmt (gsi);
6585ff8f 2432 stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
642fce57
RB
2433 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2434 {
2435 walk_stmt_info wi;
2436 memset (&wi, 0, sizeof (wi));
6585ff8f 2437 wi.info = loop_vinfo;
642fce57 2438 gimple_stmt_iterator gsi2
10681ce8 2439 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info)->stmt);
642fce57
RB
2440 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2441 vect_detect_hybrid_slp_1, &wi);
2442 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2443 vect_detect_hybrid_slp_2,
2444 vect_detect_hybrid_slp_1, &wi);
2445 }
2446 }
2447 }
2448
2449 /* Then walk the SLP instance trees marking stmts with uses in
2450 non-SLP stmts as hybrid, also propagating hybrid down the
2451 SLP tree, collecting the above info on-the-fly. */
9771b263 2452 FOR_EACH_VEC_ELT (slp_instances, i, instance)
642fce57
RB
2453 {
2454 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2455 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2456 i, pure_slp);
2457 }
ebfd146a
IR
2458}
2459
a70d6342 2460
2c515559
RS
2461/* Initialize a bb_vec_info struct for the statements between
2462 REGION_BEGIN_IN (inclusive) and REGION_END_IN (exclusive). */
2463
2464_bb_vec_info::_bb_vec_info (gimple_stmt_iterator region_begin_in,
ca823c85
RB
2465 gimple_stmt_iterator region_end_in,
2466 vec_info_shared *shared)
2467 : vec_info (vec_info::bb, init_cost (NULL), shared),
2c515559
RS
2468 bb (gsi_bb (region_begin_in)),
2469 region_begin (region_begin_in),
2470 region_end (region_end_in)
a70d6342 2471{
a70d6342
IR
2472 gimple_stmt_iterator gsi;
2473
61d371eb
RB
2474 for (gsi = region_begin; gsi_stmt (gsi) != gsi_stmt (region_end);
2475 gsi_next (&gsi))
a70d6342 2476 {
355fe088 2477 gimple *stmt = gsi_stmt (gsi);
a70d6342 2478 gimple_set_uid (stmt, 0);
4fbeb363 2479 add_stmt (stmt);
a70d6342
IR
2480 }
2481
2c515559 2482 bb->aux = this;
a70d6342
IR
2483}
2484
2485
2486/* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2487 stmts in the basic block. */
2488
2c515559 2489_bb_vec_info::~_bb_vec_info ()
a70d6342 2490{
2c515559
RS
2491 for (gimple_stmt_iterator si = region_begin;
2492 gsi_stmt (si) != gsi_stmt (region_end); gsi_next (&si))
458135c0
RS
2493 /* Reset region marker. */
2494 gimple_set_uid (gsi_stmt (si), -1);
a70d6342 2495
2c515559 2496 bb->aux = NULL;
a70d6342
IR
2497}
2498
15944069
RS
2499/* Subroutine of vect_slp_analyze_node_operations. Handle the root of NODE,
2500 given then that child nodes have already been processed, and that
2501 their def types currently match their SLP node's def type. */
a70d6342
IR
2502
2503static bool
15944069
RS
2504vect_slp_analyze_node_operations_1 (vec_info *vinfo, slp_tree node,
2505 slp_instance node_instance,
2506 stmt_vector_for_cost *cost_vec)
a70d6342 2507{
b9787581 2508 stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
bd2f172f
RB
2509 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2510
2511 /* For BB vectorization vector types are assigned here.
2512 Memory accesses already got their vector type assigned
2513 in vect_analyze_data_refs. */
2514 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2515 if (bb_vinfo
2516 && ! STMT_VINFO_DATA_REF (stmt_info))
a70d6342 2517 {
1f3cb663
RS
2518 tree vectype, nunits_vectype;
2519 if (!vect_get_vector_types_for_stmt (stmt_info, &vectype,
2520 &nunits_vectype))
2521 /* We checked this when building the node. */
2522 gcc_unreachable ();
2523 if (vectype == boolean_type_node)
bd2f172f 2524 {
1f3cb663
RS
2525 vectype = vect_get_mask_type_for_stmt (stmt_info);
2526 if (!vectype)
2527 /* vect_get_mask_type_for_stmt has already explained the
2528 failure. */
2529 return false;
bd2f172f
RB
2530 }
2531
b9787581 2532 stmt_vec_info sstmt_info;
15944069 2533 unsigned int i;
b9787581
RS
2534 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, sstmt_info)
2535 STMT_VINFO_VECTYPE (sstmt_info) = vectype;
a70d6342
IR
2536 }
2537
8b7e9dba
RS
2538 /* Calculate the number of vector statements to be created for the
2539 scalar stmts in this node. For SLP reductions it is equal to the
2540 number of vector statements in the children (which has already been
2541 calculated by the recursive call). Otherwise it is the number of
2c53b149 2542 scalar elements in one scalar iteration (DR_GROUP_SIZE) multiplied by
8b7e9dba 2543 VF divided by the number of elements in a vector. */
2c53b149
RB
2544 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
2545 && REDUC_GROUP_FIRST_ELEMENT (stmt_info))
8b7e9dba
RS
2546 SLP_TREE_NUMBER_OF_VEC_STMTS (node)
2547 = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
2548 else
2549 {
d9f21f6a 2550 poly_uint64 vf;
8b7e9dba
RS
2551 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
2552 vf = loop_vinfo->vectorization_factor;
2553 else
2554 vf = 1;
2555 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (node_instance);
2556 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2557 SLP_TREE_NUMBER_OF_VEC_STMTS (node)
d9f21f6a 2558 = vect_get_num_vectors (vf * group_size, vectype);
8b7e9dba
RS
2559 }
2560
15944069 2561 bool dummy;
86a91c0a 2562 return vect_analyze_stmt (stmt_info, &dummy, node, node_instance, cost_vec);
15944069
RS
2563}
2564
2565/* Analyze statements contained in SLP tree NODE after recursively analyzing
2566 the subtree. NODE_INSTANCE contains NODE and VINFO contains INSTANCE.
2567
2568 Return true if the operations are supported. */
2569
2570static bool
2571vect_slp_analyze_node_operations (vec_info *vinfo, slp_tree node,
2572 slp_instance node_instance,
2573 scalar_stmts_to_slp_tree_map_t *visited,
2574 scalar_stmts_to_slp_tree_map_t *lvisited,
2575 stmt_vector_for_cost *cost_vec)
2576{
2577 int i, j;
2578 slp_tree child;
2579
2580 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
2581 return true;
2582
2583 /* If we already analyzed the exact same set of scalar stmts we're done.
2584 We share the generated vector stmts for those. */
2585 slp_tree *leader;
2586 if ((leader = visited->get (SLP_TREE_SCALAR_STMTS (node)))
2587 || (leader = lvisited->get (SLP_TREE_SCALAR_STMTS (node))))
2588 {
2589 SLP_TREE_NUMBER_OF_VEC_STMTS (node)
2590 = SLP_TREE_NUMBER_OF_VEC_STMTS (*leader);
2591 return true;
2592 }
2593
2594 /* The SLP graph is acyclic so not caching whether we failed or succeeded
2595 doesn't result in any issue since we throw away the lvisited set
2596 when we fail. */
2597 lvisited->put (SLP_TREE_SCALAR_STMTS (node).copy (), node);
2598
2599 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2600 if (!vect_slp_analyze_node_operations (vinfo, child, node_instance,
2601 visited, lvisited, cost_vec))
2602 return false;
2603
bd2f172f
RB
2604 /* Push SLP node def-type to stmt operands. */
2605 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2606 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
b9787581 2607 STMT_VINFO_DEF_TYPE (SLP_TREE_SCALAR_STMTS (child)[0])
bd2f172f 2608 = SLP_TREE_DEF_TYPE (child);
15944069
RS
2609 bool res = vect_slp_analyze_node_operations_1 (vinfo, node, node_instance,
2610 cost_vec);
bd2f172f
RB
2611 /* Restore def-types. */
2612 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2613 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
b9787581 2614 STMT_VINFO_DEF_TYPE (SLP_TREE_SCALAR_STMTS (child)[0])
bd2f172f
RB
2615 = vect_internal_def;
2616 if (! res)
2617 return false;
2618
2619 return true;
a70d6342
IR
2620}
2621
2622
8b7e9dba 2623/* Analyze statements in SLP instances of VINFO. Return true if the
a70d6342
IR
2624 operations are supported. */
2625
a12e42fc 2626bool
8b7e9dba 2627vect_slp_analyze_operations (vec_info *vinfo)
a70d6342 2628{
a70d6342
IR
2629 slp_instance instance;
2630 int i;
2631
adac3a68 2632 DUMP_VECT_SCOPE ("vect_slp_analyze_operations");
a12e42fc 2633
68435eb2
RB
2634 scalar_stmts_to_slp_tree_map_t *visited
2635 = new scalar_stmts_to_slp_tree_map_t ();
8b7e9dba 2636 for (i = 0; vinfo->slp_instances.iterate (i, &instance); )
a70d6342 2637 {
68435eb2
RB
2638 scalar_stmts_to_slp_tree_map_t lvisited;
2639 stmt_vector_for_cost cost_vec;
2640 cost_vec.create (2);
8b7e9dba
RS
2641 if (!vect_slp_analyze_node_operations (vinfo,
2642 SLP_INSTANCE_TREE (instance),
68435eb2
RB
2643 instance, visited, &lvisited,
2644 &cost_vec))
a70d6342 2645 {
b9787581
RS
2646 slp_tree node = SLP_INSTANCE_TREE (instance);
2647 stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
a12e42fc
RB
2648 dump_printf_loc (MSG_NOTE, vect_location,
2649 "removing SLP instance operations starting from: ");
b9787581 2650 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt_info->stmt, 0);
6e2dd807 2651 vect_free_slp_instance (instance, false);
8b7e9dba 2652 vinfo->slp_instances.ordered_remove (i);
68435eb2 2653 cost_vec.release ();
a70d6342
IR
2654 }
2655 else
68435eb2
RB
2656 {
2657 for (scalar_stmts_to_slp_tree_map_t::iterator x = lvisited.begin();
2658 x != lvisited.end(); ++x)
2659 visited->put ((*x).first.copy (), (*x).second);
2660 i++;
78604de0 2661
68435eb2
RB
2662 add_stmt_costs (vinfo->target_cost_data, &cost_vec);
2663 cost_vec.release ();
2664 }
2665 }
78604de0
RB
2666 delete visited;
2667
8b7e9dba 2668 return !vinfo->slp_instances.is_empty ();
a70d6342
IR
2669}
2670
6eddf228
RB
2671
2672/* Compute the scalar cost of the SLP node NODE and its children
2673 and return it. Do not account defs that are marked in LIFE and
2674 update LIFE according to uses of NODE. */
2675
a296d6d3 2676static void
292cba13 2677vect_bb_slp_scalar_cost (basic_block bb,
a296d6d3
RB
2678 slp_tree node, vec<bool, va_heap> *life,
2679 stmt_vector_for_cost *cost_vec)
6eddf228 2680{
6eddf228 2681 unsigned i;
b9787581 2682 stmt_vec_info stmt_info;
6eddf228
RB
2683 slp_tree child;
2684
b9787581 2685 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
6eddf228 2686 {
b9787581 2687 gimple *stmt = stmt_info->stmt;
91987857 2688 vec_info *vinfo = stmt_info->vinfo;
6eddf228
RB
2689 ssa_op_iter op_iter;
2690 def_operand_p def_p;
6eddf228 2691
ff4c81cc 2692 if ((*life)[i])
6eddf228
RB
2693 continue;
2694
2695 /* If there is a non-vectorized use of the defs then the scalar
2696 stmt is kept live in which case we do not account it or any
2697 required defs in the SLP children in the scalar cost. This
2698 way we make the vectorization more costly when compared to
2699 the scalar cost. */
2700 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2701 {
2702 imm_use_iterator use_iter;
355fe088 2703 gimple *use_stmt;
6eddf228 2704 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
91987857 2705 if (!is_gimple_debug (use_stmt))
6eddf228 2706 {
91987857
RS
2707 stmt_vec_info use_stmt_info = vinfo->lookup_stmt (use_stmt);
2708 if (!use_stmt_info || !PURE_SLP_STMT (use_stmt_info))
2709 {
2710 (*life)[i] = true;
2711 BREAK_FROM_IMM_USE_STMT (use_iter);
2712 }
6eddf228
RB
2713 }
2714 }
ff4c81cc 2715 if ((*life)[i])
6eddf228
RB
2716 continue;
2717
b555a2e4
RB
2718 /* Count scalar stmts only once. */
2719 if (gimple_visited_p (stmt))
2720 continue;
2721 gimple_set_visited (stmt, true);
2722
a296d6d3 2723 vect_cost_for_stmt kind;
6eddf228
RB
2724 if (STMT_VINFO_DATA_REF (stmt_info))
2725 {
2726 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
a296d6d3 2727 kind = scalar_load;
6eddf228 2728 else
a296d6d3 2729 kind = scalar_store;
6eddf228
RB
2730 }
2731 else
a296d6d3
RB
2732 kind = scalar_stmt;
2733 record_stmt_cost (cost_vec, 1, kind, stmt_info, 0, vect_body);
6eddf228
RB
2734 }
2735
faa5399b 2736 auto_vec<bool, 20> subtree_life;
6eddf228 2737 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
faa5399b
RB
2738 {
2739 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
2740 {
2741 /* Do not directly pass LIFE to the recursive call, copy it to
2742 confine changes in the callee to the current child/subtree. */
2743 subtree_life.safe_splice (*life);
a296d6d3 2744 vect_bb_slp_scalar_cost (bb, child, &subtree_life, cost_vec);
faa5399b
RB
2745 subtree_life.truncate (0);
2746 }
2747 }
6eddf228
RB
2748}
2749
69f11a13
IR
2750/* Check if vectorization of the basic block is profitable. */
2751
2752static bool
2753vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2754{
9771b263 2755 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
69f11a13 2756 slp_instance instance;
1a4b99c1 2757 int i;
c3e7ee41 2758 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
92345349 2759 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
69f11a13
IR
2760
2761 /* Calculate scalar cost. */
a296d6d3
RB
2762 stmt_vector_for_cost scalar_costs;
2763 scalar_costs.create (0);
6eddf228 2764 FOR_EACH_VEC_ELT (slp_instances, i, instance)
69f11a13 2765 {
00f96dc9 2766 auto_vec<bool, 20> life;
ff4c81cc 2767 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
a296d6d3
RB
2768 vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2769 SLP_INSTANCE_TREE (instance),
2770 &life, &scalar_costs);
2771 }
2772 void *target_cost_data = init_cost (NULL);
68435eb2 2773 add_stmt_costs (target_cost_data, &scalar_costs);
a296d6d3
RB
2774 scalar_costs.release ();
2775 unsigned dummy;
2776 finish_cost (target_cost_data, &dummy, &scalar_cost, &dummy);
2777 destroy_cost_data (target_cost_data);
69f11a13 2778
b555a2e4
RB
2779 /* Unset visited flag. */
2780 for (gimple_stmt_iterator gsi = bb_vinfo->region_begin;
2781 gsi_stmt (gsi) != gsi_stmt (bb_vinfo->region_end); gsi_next (&gsi))
2782 gimple_set_visited (gsi_stmt (gsi), false);
2783
c3e7ee41 2784 /* Complete the target-specific cost calculation. */
92345349
BS
2785 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2786 &vec_inside_cost, &vec_epilogue_cost);
2787
2788 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
c3e7ee41 2789
73fbfcad 2790 if (dump_enabled_p ())
69f11a13 2791 {
78c60e3d
SS
2792 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2793 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2794 vec_inside_cost);
2795 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2796 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
e645e942 2797 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
69f11a13
IR
2798 }
2799
a6524bba
RB
2800 /* Vectorization is profitable if its cost is more than the cost of scalar
2801 version. Note that we err on the vector side for equal cost because
2802 the cost estimate is otherwise quite pessimistic (constant uses are
2803 free on the scalar side but cost a load on the vector side for
2804 example). */
2805 if (vec_outside_cost + vec_inside_cost > scalar_cost)
69f11a13
IR
2806 return false;
2807
2808 return true;
2809}
2810
a5b50aa1
RB
2811/* Check if the basic block can be vectorized. Returns a bb_vec_info
2812 if so and sets fatal to true if failure is independent of
2813 current_vector_size. */
a70d6342 2814
8e19f5a1 2815static bb_vec_info
61d371eb
RB
2816vect_slp_analyze_bb_1 (gimple_stmt_iterator region_begin,
2817 gimple_stmt_iterator region_end,
a5b50aa1 2818 vec<data_reference_p> datarefs, int n_stmts,
ca823c85 2819 bool &fatal, vec_info_shared *shared)
a70d6342
IR
2820{
2821 bb_vec_info bb_vinfo;
a70d6342 2822 slp_instance instance;
8e19f5a1 2823 int i;
d9f21f6a 2824 poly_uint64 min_vf = 2;
e4a707c4 2825
a5b50aa1
RB
2826 /* The first group of checks is independent of the vector size. */
2827 fatal = true;
2828
61d371eb
RB
2829 if (n_stmts > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2830 {
2831 if (dump_enabled_p ())
2832 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2833 "not vectorized: too many instructions in "
2834 "basic block.\n");
2835 free_data_refs (datarefs);
2836 return NULL;
2837 }
2838
ca823c85 2839 bb_vinfo = new _bb_vec_info (region_begin, region_end, shared);
a70d6342
IR
2840 if (!bb_vinfo)
2841 return NULL;
2842
61d371eb 2843 BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
ca823c85 2844 bb_vinfo->shared->save_datarefs ();
428db0ba
RB
2845
2846 /* Analyze the data references. */
2847
2848 if (!vect_analyze_data_refs (bb_vinfo, &min_vf))
a70d6342 2849 {
73fbfcad 2850 if (dump_enabled_p ())
78c60e3d
SS
2851 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2852 "not vectorized: unhandled data-ref in basic "
2853 "block.\n");
b8698a0f 2854
2c515559 2855 delete bb_vinfo;
a70d6342
IR
2856 return NULL;
2857 }
2858
fcac74a1 2859 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
a70d6342 2860 {
73fbfcad 2861 if (dump_enabled_p ())
78c60e3d
SS
2862 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2863 "not vectorized: not enough data-refs in "
2864 "basic block.\n");
a70d6342 2865
2c515559 2866 delete bb_vinfo;
a70d6342
IR
2867 return NULL;
2868 }
2869
310213d4 2870 if (!vect_analyze_data_ref_accesses (bb_vinfo))
5abe1e05
RB
2871 {
2872 if (dump_enabled_p ())
2873 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2874 "not vectorized: unhandled data access in "
2875 "basic block.\n");
2876
2c515559 2877 delete bb_vinfo;
5abe1e05
RB
2878 return NULL;
2879 }
2880
a5b50aa1
RB
2881 /* If there are no grouped stores in the region there is no need
2882 to continue with pattern recog as vect_analyze_slp will fail
2883 anyway. */
2884 if (bb_vinfo->grouped_stores.is_empty ())
a70d6342 2885 {
73fbfcad 2886 if (dump_enabled_p ())
a5b50aa1
RB
2887 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2888 "not vectorized: no grouped stores in "
2889 "basic block.\n");
b8698a0f 2890
2c515559 2891 delete bb_vinfo;
a70d6342
IR
2892 return NULL;
2893 }
b8698a0f 2894
a5b50aa1
RB
2895 /* While the rest of the analysis below depends on it in some way. */
2896 fatal = false;
2897
2898 vect_pattern_recog (bb_vinfo);
2899
a70d6342
IR
2900 /* Check the SLP opportunities in the basic block, analyze and build SLP
2901 trees. */
310213d4 2902 if (!vect_analyze_slp (bb_vinfo, n_stmts))
a70d6342 2903 {
73fbfcad 2904 if (dump_enabled_p ())
effb52da
RB
2905 {
2906 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2907 "Failed to SLP the basic block.\n");
2908 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2909 "not vectorized: failed to find SLP opportunities "
2910 "in basic block.\n");
2911 }
a70d6342 2912
2c515559 2913 delete bb_vinfo;
a70d6342
IR
2914 return NULL;
2915 }
b8698a0f 2916
62c8a2cf
RS
2917 vect_record_base_alignments (bb_vinfo);
2918
c2a12ca0
RB
2919 /* Analyze and verify the alignment of data references and the
2920 dependence in the SLP instances. */
a5b50aa1
RB
2921 for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
2922 {
c2a12ca0
RB
2923 if (! vect_slp_analyze_and_verify_instance_alignment (instance)
2924 || ! vect_slp_analyze_instance_dependence (instance))
a5b50aa1 2925 {
b9787581
RS
2926 slp_tree node = SLP_INSTANCE_TREE (instance);
2927 stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
a5b50aa1
RB
2928 dump_printf_loc (MSG_NOTE, vect_location,
2929 "removing SLP instance operations starting from: ");
b9787581 2930 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt_info->stmt, 0);
6e2dd807 2931 vect_free_slp_instance (instance, false);
a5b50aa1
RB
2932 BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
2933 continue;
2934 }
c2a12ca0
RB
2935
2936 /* Mark all the statements that we want to vectorize as pure SLP and
2937 relevant. */
2938 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2939 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2940
a5b50aa1
RB
2941 i++;
2942 }
a5b50aa1
RB
2943 if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
2944 {
2c515559 2945 delete bb_vinfo;
a5b50aa1
RB
2946 return NULL;
2947 }
2948
8b7e9dba 2949 if (!vect_slp_analyze_operations (bb_vinfo))
a70d6342 2950 {
73fbfcad 2951 if (dump_enabled_p ())
e645e942 2952 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
78c60e3d 2953 "not vectorized: bad operation in basic block.\n");
a70d6342 2954
2c515559 2955 delete bb_vinfo;
a70d6342
IR
2956 return NULL;
2957 }
2958
69f11a13 2959 /* Cost model: check if the vectorization is worthwhile. */
8b5e1202 2960 if (!unlimited_cost_model (NULL)
69f11a13
IR
2961 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2962 {
73fbfcad 2963 if (dump_enabled_p ())
78c60e3d
SS
2964 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2965 "not vectorized: vectorization is not "
2966 "profitable.\n");
69f11a13 2967
2c515559 2968 delete bb_vinfo;
69f11a13
IR
2969 return NULL;
2970 }
2971
73fbfcad 2972 if (dump_enabled_p ())
78c60e3d
SS
2973 dump_printf_loc (MSG_NOTE, vect_location,
2974 "Basic block will be vectorized using SLP\n");
a70d6342
IR
2975
2976 return bb_vinfo;
2977}
2978
2979
428db0ba
RB
2980/* Main entry for the BB vectorizer. Analyze and transform BB, returns
2981 true if anything in the basic-block was vectorized. */
2982
2983bool
2984vect_slp_bb (basic_block bb)
8e19f5a1
IR
2985{
2986 bb_vec_info bb_vinfo;
8e19f5a1 2987 gimple_stmt_iterator gsi;
61d371eb 2988 bool any_vectorized = false;
86e36728 2989 auto_vector_sizes vector_sizes;
8e19f5a1 2990
adac3a68 2991 DUMP_VECT_SCOPE ("vect_slp_analyze_bb");
8e19f5a1 2992
8e19f5a1
IR
2993 /* Autodetect first vector size we try. */
2994 current_vector_size = 0;
86e36728
RS
2995 targetm.vectorize.autovectorize_vector_sizes (&vector_sizes);
2996 unsigned int next_size = 0;
8e19f5a1 2997
61d371eb
RB
2998 gsi = gsi_start_bb (bb);
2999
86e36728 3000 poly_uint64 autodetected_vector_size = 0;
8e19f5a1
IR
3001 while (1)
3002 {
61d371eb
RB
3003 if (gsi_end_p (gsi))
3004 break;
3005
3006 gimple_stmt_iterator region_begin = gsi;
3007 vec<data_reference_p> datarefs = vNULL;
3008 int insns = 0;
3009
3010 for (; !gsi_end_p (gsi); gsi_next (&gsi))
428db0ba 3011 {
61d371eb
RB
3012 gimple *stmt = gsi_stmt (gsi);
3013 if (is_gimple_debug (stmt))
3014 continue;
3015 insns++;
3016
3017 if (gimple_location (stmt) != UNKNOWN_LOCATION)
4f5b9c80 3018 vect_location = stmt;
61d371eb 3019
8e846c66 3020 if (!vect_find_stmt_data_reference (NULL, stmt, &datarefs))
61d371eb
RB
3021 break;
3022 }
3023
3024 /* Skip leading unhandled stmts. */
3025 if (gsi_stmt (region_begin) == gsi_stmt (gsi))
3026 {
3027 gsi_next (&gsi);
3028 continue;
3029 }
428db0ba 3030
61d371eb
RB
3031 gimple_stmt_iterator region_end = gsi;
3032
3033 bool vectorized = false;
a5b50aa1 3034 bool fatal = false;
ca823c85 3035 vec_info_shared shared;
61d371eb 3036 bb_vinfo = vect_slp_analyze_bb_1 (region_begin, region_end,
ca823c85 3037 datarefs, insns, fatal, &shared);
61d371eb
RB
3038 if (bb_vinfo
3039 && dbg_cnt (vect_slp))
3040 {
428db0ba 3041 if (dump_enabled_p ())
61d371eb 3042 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
428db0ba 3043
ca823c85 3044 bb_vinfo->shared->check_datarefs ();
428db0ba
RB
3045 vect_schedule_slp (bb_vinfo);
3046
d1ac60d5
RB
3047 unsigned HOST_WIDE_INT bytes;
3048 if (current_vector_size.is_constant (&bytes))
3049 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
3050 "basic block part vectorized using "
3051 HOST_WIDE_INT_PRINT_UNSIGNED " byte "
3052 "vectors\n", bytes);
3053 else
3054 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
3055 "basic block part vectorized using variable "
3056 "length vectors\n");
428db0ba 3057
61d371eb 3058 vectorized = true;
428db0ba 3059 }
2c515559 3060 delete bb_vinfo;
8e19f5a1 3061
61d371eb 3062 any_vectorized |= vectorized;
8e19f5a1 3063
86e36728
RS
3064 if (next_size == 0)
3065 autodetected_vector_size = current_vector_size;
3066
3067 if (next_size < vector_sizes.length ()
3068 && known_eq (vector_sizes[next_size], autodetected_vector_size))
3069 next_size += 1;
3070
61d371eb 3071 if (vectorized
86e36728
RS
3072 || next_size == vector_sizes.length ()
3073 || known_eq (current_vector_size, 0U)
a5b50aa1
RB
3074 /* If vect_slp_analyze_bb_1 signaled that analysis for all
3075 vector sizes will fail do not bother iterating. */
3076 || fatal)
61d371eb
RB
3077 {
3078 if (gsi_end_p (region_end))
3079 break;
8e19f5a1 3080
61d371eb
RB
3081 /* Skip the unhandled stmt. */
3082 gsi_next (&gsi);
3083
3084 /* And reset vector sizes. */
3085 current_vector_size = 0;
86e36728 3086 next_size = 0;
61d371eb
RB
3087 }
3088 else
3089 {
3090 /* Try the next biggest vector size. */
86e36728 3091 current_vector_size = vector_sizes[next_size++];
61d371eb 3092 if (dump_enabled_p ())
86e36728
RS
3093 {
3094 dump_printf_loc (MSG_NOTE, vect_location,
3095 "***** Re-trying analysis with "
3096 "vector size ");
3097 dump_dec (MSG_NOTE, current_vector_size);
3098 dump_printf (MSG_NOTE, "\n");
3099 }
61d371eb
RB
3100
3101 /* Start over. */
3102 gsi = region_begin;
3103 }
8e19f5a1 3104 }
61d371eb
RB
3105
3106 return any_vectorized;
8e19f5a1
IR
3107}
3108
3109
e4af0bc4 3110/* Return 1 if vector type of boolean constant which is OPNUM
32e8e429 3111 operand in statement STMT_VINFO is a boolean vector. */
e4af0bc4
IE
3112
3113static bool
32e8e429 3114vect_mask_constant_operand_p (stmt_vec_info stmt_vinfo, int opnum)
e4af0bc4 3115{
32e8e429 3116 enum tree_code code = gimple_expr_code (stmt_vinfo->stmt);
e4af0bc4 3117 tree op, vectype;
e4af0bc4
IE
3118 enum vect_def_type dt;
3119
3120 /* For comparison and COND_EXPR type is chosen depending
3121 on the other comparison operand. */
3122 if (TREE_CODE_CLASS (code) == tcc_comparison)
3123 {
32e8e429 3124 gassign *stmt = as_a <gassign *> (stmt_vinfo->stmt);
e4af0bc4
IE
3125 if (opnum)
3126 op = gimple_assign_rhs1 (stmt);
3127 else
3128 op = gimple_assign_rhs2 (stmt);
3129
894dd753 3130 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &dt, &vectype))
e4af0bc4
IE
3131 gcc_unreachable ();
3132
3133 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
3134 }
3135
3136 if (code == COND_EXPR)
3137 {
32e8e429 3138 gassign *stmt = as_a <gassign *> (stmt_vinfo->stmt);
e4af0bc4
IE
3139 tree cond = gimple_assign_rhs1 (stmt);
3140
3141 if (TREE_CODE (cond) == SSA_NAME)
7b1b0cc1
RB
3142 op = cond;
3143 else if (opnum)
e4af0bc4
IE
3144 op = TREE_OPERAND (cond, 1);
3145 else
3146 op = TREE_OPERAND (cond, 0);
3147
894dd753 3148 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &dt, &vectype))
e4af0bc4
IE
3149 gcc_unreachable ();
3150
3151 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
3152 }
3153
3154 return VECTOR_BOOLEAN_TYPE_P (STMT_VINFO_VECTYPE (stmt_vinfo));
3155}
3156
018b2744
RS
3157/* Build a variable-length vector in which the elements in ELTS are repeated
3158 to a fill NRESULTS vectors of type VECTOR_TYPE. Store the vectors in
3159 RESULTS and add any new instructions to SEQ.
3160
3161 The approach we use is:
3162
3163 (1) Find a vector mode VM with integer elements of mode IM.
3164
3165 (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
3166 ELTS' has mode IM. This involves creating NELTS' VIEW_CONVERT_EXPRs
3167 from small vectors to IM.
3168
3169 (3) Duplicate each ELTS'[I] into a vector of mode VM.
3170
3171 (4) Use a tree of interleaving VEC_PERM_EXPRs to create VMs with the
3172 correct byte contents.
3173
3174 (5) Use VIEW_CONVERT_EXPR to cast the final VMs to the required type.
3175
3176 We try to find the largest IM for which this sequence works, in order
3177 to cut down on the number of interleaves. */
3178
f1739b48 3179void
018b2744
RS
3180duplicate_and_interleave (gimple_seq *seq, tree vector_type, vec<tree> elts,
3181 unsigned int nresults, vec<tree> &results)
3182{
3183 unsigned int nelts = elts.length ();
3184 tree element_type = TREE_TYPE (vector_type);
3185
3186 /* (1) Find a vector mode VM with integer elements of mode IM. */
3187 unsigned int nvectors = 1;
3188 tree new_vector_type;
3189 tree permutes[2];
3190 if (!can_duplicate_and_interleave_p (nelts, TYPE_MODE (element_type),
3191 &nvectors, &new_vector_type,
3192 permutes))
3193 gcc_unreachable ();
3194
3195 /* Get a vector type that holds ELTS[0:NELTS/NELTS']. */
3196 unsigned int partial_nelts = nelts / nvectors;
3197 tree partial_vector_type = build_vector_type (element_type, partial_nelts);
3198
3199 tree_vector_builder partial_elts;
3200 auto_vec<tree, 32> pieces (nvectors * 2);
3201 pieces.quick_grow (nvectors * 2);
3202 for (unsigned int i = 0; i < nvectors; ++i)
3203 {
3204 /* (2) Replace ELTS[0:NELTS] with ELTS'[0:NELTS'], where each element of
3205 ELTS' has mode IM. */
3206 partial_elts.new_vector (partial_vector_type, partial_nelts, 1);
3207 for (unsigned int j = 0; j < partial_nelts; ++j)
3208 partial_elts.quick_push (elts[i * partial_nelts + j]);
3209 tree t = gimple_build_vector (seq, &partial_elts);
3210 t = gimple_build (seq, VIEW_CONVERT_EXPR,
3211 TREE_TYPE (new_vector_type), t);
3212
3213 /* (3) Duplicate each ELTS'[I] into a vector of mode VM. */
3214 pieces[i] = gimple_build_vector_from_val (seq, new_vector_type, t);
3215 }
3216
3217 /* (4) Use a tree of VEC_PERM_EXPRs to create a single VM with the
3218 correct byte contents.
3219
3220 We need to repeat the following operation log2(nvectors) times:
3221
3222 out[i * 2] = VEC_PERM_EXPR (in[i], in[i + hi_start], lo_permute);
3223 out[i * 2 + 1] = VEC_PERM_EXPR (in[i], in[i + hi_start], hi_permute);
3224
3225 However, if each input repeats every N elements and the VF is
3226 a multiple of N * 2, the HI result is the same as the LO. */
3227 unsigned int in_start = 0;
3228 unsigned int out_start = nvectors;
3229 unsigned int hi_start = nvectors / 2;
3230 /* A bound on the number of outputs needed to produce NRESULTS results
3231 in the final iteration. */
3232 unsigned int noutputs_bound = nvectors * nresults;
3233 for (unsigned int in_repeat = 1; in_repeat < nvectors; in_repeat *= 2)
3234 {
3235 noutputs_bound /= 2;
3236 unsigned int limit = MIN (noutputs_bound, nvectors);
3237 for (unsigned int i = 0; i < limit; ++i)
3238 {
3239 if ((i & 1) != 0
3240 && multiple_p (TYPE_VECTOR_SUBPARTS (new_vector_type),
3241 2 * in_repeat))
3242 {
3243 pieces[out_start + i] = pieces[out_start + i - 1];
3244 continue;
3245 }
3246
3247 tree output = make_ssa_name (new_vector_type);
3248 tree input1 = pieces[in_start + (i / 2)];
3249 tree input2 = pieces[in_start + (i / 2) + hi_start];
3250 gassign *stmt = gimple_build_assign (output, VEC_PERM_EXPR,
3251 input1, input2,
3252 permutes[i & 1]);
3253 gimple_seq_add_stmt (seq, stmt);
3254 pieces[out_start + i] = output;
3255 }
3256 std::swap (in_start, out_start);
3257 }
3258
3259 /* (5) Use VIEW_CONVERT_EXPR to cast the final VM to the required type. */
3260 results.reserve (nresults);
3261 for (unsigned int i = 0; i < nresults; ++i)
3262 if (i < nvectors)
3263 results.quick_push (gimple_build (seq, VIEW_CONVERT_EXPR, vector_type,
3264 pieces[in_start + i]));
3265 else
3266 results.quick_push (results[i - nvectors]);
3267}
3268
e4af0bc4 3269
b8698a0f
L
3270/* For constant and loop invariant defs of SLP_NODE this function returns
3271 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
d59dc888
IR
3272 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
3273 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
b5aeb3bb
IR
3274 REDUC_INDEX is the index of the reduction operand in the statements, unless
3275 it is -1. */
ebfd146a
IR
3276
3277static void
9dc3f7de 3278vect_get_constant_vectors (tree op, slp_tree slp_node,
9771b263 3279 vec<tree> *vec_oprnds,
306b0c92 3280 unsigned int op_num, unsigned int number_of_vectors)
ebfd146a 3281{
b9787581
RS
3282 vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
3283 stmt_vec_info stmt_vinfo = stmts[0];
3284 gimple *stmt = stmt_vinfo->stmt;
018b2744 3285 unsigned HOST_WIDE_INT nunits;
ebfd146a 3286 tree vec_cst;
d2a12ae7 3287 unsigned j, number_of_places_left_in_vector;
ebfd146a 3288 tree vector_type;
9dc3f7de 3289 tree vop;
9771b263 3290 int group_size = stmts.length ();
ebfd146a 3291 unsigned int vec_num, i;
d2a12ae7 3292 unsigned number_of_copies = 1;
9771b263
DN
3293 vec<tree> voprnds;
3294 voprnds.create (number_of_vectors);
ebfd146a 3295 bool constant_p, is_store;
b5aeb3bb 3296 tree neutral_op = NULL;
bac430c9 3297 enum tree_code code = gimple_expr_code (stmt);
13396b6e 3298 gimple_seq ctor_seq = NULL;
018b2744 3299 auto_vec<tree, 16> permute_results;
b5aeb3bb 3300
42fd8198 3301 /* Check if vector type is a boolean vector. */
2568d8a1 3302 if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
b9787581 3303 && vect_mask_constant_operand_p (stmt_vinfo, op_num))
42fd8198
IE
3304 vector_type
3305 = build_same_sized_truth_vector_type (STMT_VINFO_VECTYPE (stmt_vinfo));
3306 else
3307 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
afbe6325 3308
ebfd146a
IR
3309 if (STMT_VINFO_DATA_REF (stmt_vinfo))
3310 {
3311 is_store = true;
3312 op = gimple_assign_rhs1 (stmt);
3313 }
3314 else
9dc3f7de
IR
3315 is_store = false;
3316
3317 gcc_assert (op);
ebfd146a 3318
ebfd146a 3319 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
b8698a0f 3320 created vectors. It is greater than 1 if unrolling is performed.
ebfd146a
IR
3321
3322 For example, we have two scalar operands, s1 and s2 (e.g., group of
3323 strided accesses of size two), while NUNITS is four (i.e., four scalars
f7e531cf
IR
3324 of this type can be packed in a vector). The output vector will contain
3325 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
ebfd146a
IR
3326 will be 2).
3327
b8698a0f 3328 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
ebfd146a
IR
3329 containing the operands.
3330
3331 For example, NUNITS is four as before, and the group size is 8
f7e531cf 3332 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
ebfd146a 3333 {s5, s6, s7, s8}. */
b8698a0f 3334
018b2744
RS
3335 /* When using duplicate_and_interleave, we just need one element for
3336 each scalar statement. */
3337 if (!TYPE_VECTOR_SUBPARTS (vector_type).is_constant (&nunits))
3338 nunits = group_size;
3339
14a61437 3340 number_of_copies = nunits * number_of_vectors / group_size;
ebfd146a
IR
3341
3342 number_of_places_left_in_vector = nunits;
62cf7335 3343 constant_p = true;
5ebaa477 3344 tree_vector_builder elts (vector_type, nunits, 1);
794e3180 3345 elts.quick_grow (nunits);
90dd6e3d 3346 bool place_after_defs = false;
ebfd146a
IR
3347 for (j = 0; j < number_of_copies; j++)
3348 {
b9787581 3349 for (i = group_size - 1; stmts.iterate (i, &stmt_vinfo); i--)
ebfd146a 3350 {
b9787581 3351 stmt = stmt_vinfo->stmt;
ebfd146a
IR
3352 if (is_store)
3353 op = gimple_assign_rhs1 (stmt);
bac430c9 3354 else
f7e531cf 3355 {
bac430c9 3356 switch (code)
f7e531cf 3357 {
bac430c9 3358 case COND_EXPR:
a989bcc3
IE
3359 {
3360 tree cond = gimple_assign_rhs1 (stmt);
3361 if (TREE_CODE (cond) == SSA_NAME)
3362 op = gimple_op (stmt, op_num + 1);
3363 else if (op_num == 0 || op_num == 1)
bac430c9 3364 op = TREE_OPERAND (cond, op_num);
a989bcc3
IE
3365 else
3366 {
3367 if (op_num == 2)
3368 op = gimple_assign_rhs2 (stmt);
3369 else
3370 op = gimple_assign_rhs3 (stmt);
3371 }
3372 }
bac430c9
IR
3373 break;
3374
3375 case CALL_EXPR:
3376 op = gimple_call_arg (stmt, op_num);
3377 break;
3378
b84b294a
JJ
3379 case LSHIFT_EXPR:
3380 case RSHIFT_EXPR:
3381 case LROTATE_EXPR:
3382 case RROTATE_EXPR:
3383 op = gimple_op (stmt, op_num + 1);
3384 /* Unlike the other binary operators, shifts/rotates have
3385 the shift count being int, instead of the same type as
3386 the lhs, so make sure the scalar is the right type if
3387 we are dealing with vectors of
3388 long long/long/short/char. */
793d9a16 3389 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
b84b294a
JJ
3390 op = fold_convert (TREE_TYPE (vector_type), op);
3391 break;
3392
bac430c9
IR
3393 default:
3394 op = gimple_op (stmt, op_num + 1);
b84b294a 3395 break;
f7e531cf
IR
3396 }
3397 }
b8698a0f 3398
ebfd146a 3399 /* Create 'vect_ = {op0,op1,...,opn}'. */
ebfd146a 3400 number_of_places_left_in_vector--;
90dd6e3d 3401 tree orig_op = op;
13396b6e 3402 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
50eeef09 3403 {
793d9a16 3404 if (CONSTANT_CLASS_P (op))
13396b6e 3405 {
42fd8198
IE
3406 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3407 {
3408 /* Can't use VIEW_CONVERT_EXPR for booleans because
3409 of possibly different sizes of scalar value and
3410 vector element. */
3411 if (integer_zerop (op))
3412 op = build_int_cst (TREE_TYPE (vector_type), 0);
3413 else if (integer_onep (op))
158beb4a 3414 op = build_all_ones_cst (TREE_TYPE (vector_type));
42fd8198
IE
3415 else
3416 gcc_unreachable ();
3417 }
3418 else
3419 op = fold_unary (VIEW_CONVERT_EXPR,
3420 TREE_TYPE (vector_type), op);
13396b6e
JJ
3421 gcc_assert (op && CONSTANT_CLASS_P (op));
3422 }
3423 else
3424 {
b731b390 3425 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
355fe088 3426 gimple *init_stmt;
262a363f
JJ
3427 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3428 {
158beb4a
JJ
3429 tree true_val
3430 = build_all_ones_cst (TREE_TYPE (vector_type));
3431 tree false_val
3432 = build_zero_cst (TREE_TYPE (vector_type));
7c285ab9 3433 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
158beb4a
JJ
3434 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
3435 op, true_val,
3436 false_val);
262a363f 3437 }
262a363f
JJ
3438 else
3439 {
3440 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
3441 op);
3442 init_stmt
3443 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
3444 op);
3445 }
13396b6e
JJ
3446 gimple_seq_add_stmt (&ctor_seq, init_stmt);
3447 op = new_temp;
3448 }
50eeef09 3449 }
d2a12ae7 3450 elts[number_of_places_left_in_vector] = op;
793d9a16
RB
3451 if (!CONSTANT_CLASS_P (op))
3452 constant_p = false;
90dd6e3d
RB
3453 if (TREE_CODE (orig_op) == SSA_NAME
3454 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
3455 && STMT_VINFO_BB_VINFO (stmt_vinfo)
3456 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
3457 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
3458 place_after_defs = true;
ebfd146a
IR
3459
3460 if (number_of_places_left_in_vector == 0)
3461 {
018b2744
RS
3462 if (constant_p
3463 ? multiple_p (TYPE_VECTOR_SUBPARTS (vector_type), nunits)
3464 : known_eq (TYPE_VECTOR_SUBPARTS (vector_type), nunits))
3465 vec_cst = gimple_build_vector (&ctor_seq, &elts);
ebfd146a 3466 else
d2a12ae7 3467 {
018b2744
RS
3468 if (vec_oprnds->is_empty ())
3469 duplicate_and_interleave (&ctor_seq, vector_type, elts,
3470 number_of_vectors,
3471 permute_results);
3472 vec_cst = permute_results[number_of_vectors - j - 1];
d2a12ae7 3473 }
90dd6e3d
RB
3474 tree init;
3475 gimple_stmt_iterator gsi;
3476 if (place_after_defs)
3477 {
95c68311
RS
3478 stmt_vec_info last_stmt_info
3479 = vect_find_last_scalar_stmt_in_slp (slp_node);
3480 gsi = gsi_for_stmt (last_stmt_info->stmt);
b9787581
RS
3481 init = vect_init_vector (stmt_vinfo, vec_cst, vector_type,
3482 &gsi);
90dd6e3d
RB
3483 }
3484 else
b9787581
RS
3485 init = vect_init_vector (stmt_vinfo, vec_cst, vector_type,
3486 NULL);
13396b6e
JJ
3487 if (ctor_seq != NULL)
3488 {
90dd6e3d 3489 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
018b2744 3490 gsi_insert_seq_before (&gsi, ctor_seq, GSI_SAME_STMT);
13396b6e
JJ
3491 ctor_seq = NULL;
3492 }
90dd6e3d
RB
3493 voprnds.quick_push (init);
3494 place_after_defs = false;
62cf7335
RB
3495 number_of_places_left_in_vector = nunits;
3496 constant_p = true;
5ebaa477
RS
3497 elts.new_vector (vector_type, nunits, 1);
3498 elts.quick_grow (nunits);
ebfd146a
IR
3499 }
3500 }
3501 }
3502
b8698a0f 3503 /* Since the vectors are created in the reverse order, we should invert
ebfd146a 3504 them. */
9771b263 3505 vec_num = voprnds.length ();
d2a12ae7 3506 for (j = vec_num; j != 0; j--)
ebfd146a 3507 {
9771b263
DN
3508 vop = voprnds[j - 1];
3509 vec_oprnds->quick_push (vop);
ebfd146a
IR
3510 }
3511
9771b263 3512 voprnds.release ();
ebfd146a
IR
3513
3514 /* In case that VF is greater than the unrolling factor needed for the SLP
b8698a0f
L
3515 group of stmts, NUMBER_OF_VECTORS to be created is greater than
3516 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
ebfd146a 3517 to replicate the vectors. */
9771b263 3518 while (number_of_vectors > vec_oprnds->length ())
ebfd146a 3519 {
b5aeb3bb
IR
3520 tree neutral_vec = NULL;
3521
3522 if (neutral_op)
3523 {
3524 if (!neutral_vec)
b9acc9f1 3525 neutral_vec = build_vector_from_val (vector_type, neutral_op);
b5aeb3bb 3526
9771b263 3527 vec_oprnds->quick_push (neutral_vec);
b5aeb3bb
IR
3528 }
3529 else
3530 {
9771b263
DN
3531 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
3532 vec_oprnds->quick_push (vop);
b5aeb3bb 3533 }
ebfd146a
IR
3534 }
3535}
3536
3537
3538/* Get vectorized definitions from SLP_NODE that contains corresponding
3539 vectorized def-stmts. */
3540
3541static void
9771b263 3542vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
ebfd146a
IR
3543{
3544 tree vec_oprnd;
16edaeb8 3545 stmt_vec_info vec_def_stmt_info;
ebfd146a
IR
3546 unsigned int i;
3547
9771b263 3548 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
ebfd146a 3549
16edaeb8 3550 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt_info)
ebfd146a 3551 {
16edaeb8
RS
3552 gcc_assert (vec_def_stmt_info);
3553 if (gphi *vec_def_phi = dyn_cast <gphi *> (vec_def_stmt_info->stmt))
3554 vec_oprnd = gimple_phi_result (vec_def_phi);
e7baeb39 3555 else
16edaeb8 3556 vec_oprnd = gimple_get_lhs (vec_def_stmt_info->stmt);
9771b263 3557 vec_oprnds->quick_push (vec_oprnd);
ebfd146a
IR
3558 }
3559}
3560
3561
b8698a0f
L
3562/* Get vectorized definitions for SLP_NODE.
3563 If the scalar definitions are loop invariants or constants, collect them and
ebfd146a
IR
3564 call vect_get_constant_vectors() to create vector stmts.
3565 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
d092494c
IR
3566 must be stored in the corresponding child of SLP_NODE, and we call
3567 vect_get_slp_vect_defs () to retrieve them. */
b8698a0f 3568
ebfd146a 3569void
9771b263 3570vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
306b0c92 3571 vec<vec<tree> > *vec_oprnds)
ebfd146a 3572{
d092494c 3573 int number_of_vects = 0, i;
77eefb71 3574 unsigned int child_index = 0;
b8698a0f 3575 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
d092494c 3576 slp_tree child = NULL;
37b5ec8f 3577 vec<tree> vec_defs;
e44978dc 3578 tree oprnd;
77eefb71 3579 bool vectorized_defs;
ebfd146a 3580
a1824cfd 3581 stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
9771b263 3582 FOR_EACH_VEC_ELT (ops, i, oprnd)
ebfd146a 3583 {
d092494c
IR
3584 /* For each operand we check if it has vectorized definitions in a child
3585 node or we need to create them (for invariants and constants). We
3586 check if the LHS of the first stmt of the next child matches OPRND.
3587 If it does, we found the correct child. Otherwise, we call
77eefb71
RB
3588 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
3589 to check this child node for the next operand. */
3590 vectorized_defs = false;
3591 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
ebfd146a 3592 {
01d8bf07 3593 child = SLP_TREE_CHILDREN (slp_node)[child_index];
d092494c 3594
e44978dc 3595 /* We have to check both pattern and original def, if available. */
603cca93 3596 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
e44978dc 3597 {
b9787581
RS
3598 stmt_vec_info first_def_info = SLP_TREE_SCALAR_STMTS (child)[0];
3599 stmt_vec_info related = STMT_VINFO_RELATED_STMT (first_def_info);
e7baeb39 3600 tree first_def_op;
90dd6e3d 3601
b9787581 3602 if (gphi *first_def = dyn_cast <gphi *> (first_def_info->stmt))
e7baeb39
RB
3603 first_def_op = gimple_phi_result (first_def);
3604 else
b9787581 3605 first_def_op = gimple_get_lhs (first_def_info->stmt);
e7baeb39 3606 if (operand_equal_p (oprnd, first_def_op, 0)
90dd6e3d 3607 || (related
10681ce8
RS
3608 && operand_equal_p (oprnd,
3609 gimple_get_lhs (related->stmt), 0)))
90dd6e3d
RB
3610 {
3611 /* The number of vector defs is determined by the number of
3612 vector statements in the node from which we get those
3613 statements. */
3614 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
3615 vectorized_defs = true;
77eefb71 3616 child_index++;
90dd6e3d 3617 }
e44978dc 3618 }
77eefb71
RB
3619 else
3620 child_index++;
d092494c 3621 }
ebfd146a 3622
77eefb71
RB
3623 if (!vectorized_defs)
3624 {
3625 if (i == 0)
3626 {
3627 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
3628 /* Number of vector stmts was calculated according to LHS in
3629 vect_schedule_slp_instance (), fix it by replacing LHS with
3630 RHS, if necessary. See vect_get_smallest_scalar_type () for
3631 details. */
a1824cfd
RS
3632 vect_get_smallest_scalar_type (first_stmt_info, &lhs_size_unit,
3633 &rhs_size_unit);
77eefb71
RB
3634 if (rhs_size_unit != lhs_size_unit)
3635 {
3636 number_of_vects *= rhs_size_unit;
3637 number_of_vects /= lhs_size_unit;
3638 }
3639 }
d092494c 3640 }
b5aeb3bb 3641
d092494c 3642 /* Allocate memory for vectorized defs. */
37b5ec8f
JJ
3643 vec_defs = vNULL;
3644 vec_defs.create (number_of_vects);
ebfd146a 3645
d092494c
IR
3646 /* For reduction defs we call vect_get_constant_vectors (), since we are
3647 looking for initial loop invariant values. */
306b0c92 3648 if (vectorized_defs)
d092494c 3649 /* The defs are already vectorized. */
37b5ec8f 3650 vect_get_slp_vect_defs (child, &vec_defs);
d092494c 3651 else
e7baeb39 3652 /* Build vectors from scalar defs. */
37b5ec8f 3653 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
306b0c92 3654 number_of_vects);
ebfd146a 3655
37b5ec8f 3656 vec_oprnds->quick_push (vec_defs);
d092494c 3657 }
ebfd146a
IR
3658}
3659
ebfd146a
IR
3660/* Generate vector permute statements from a list of loads in DR_CHAIN.
3661 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
01d8bf07
RB
3662 permute statements for the SLP node NODE of the SLP instance
3663 SLP_NODE_INSTANCE. */
3664
ebfd146a 3665bool
01d8bf07 3666vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
d9f21f6a
RS
3667 gimple_stmt_iterator *gsi, poly_uint64 vf,
3668 slp_instance slp_node_instance, bool analyze_only,
29afecdf 3669 unsigned *n_perms)
ebfd146a 3670{
b9787581 3671 stmt_vec_info stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
16edaeb8 3672 vec_info *vinfo = stmt_info->vinfo;
ebfd146a 3673 tree mask_element_type = NULL_TREE, mask_type;
928686b1 3674 int vec_index = 0;
2635892a 3675 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
ebfd146a 3676 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
928686b1 3677 unsigned int mask_element;
ef4bddc2 3678 machine_mode mode;
928686b1 3679 unsigned HOST_WIDE_INT nunits, const_vf;
ebfd146a 3680
91ff1504
RB
3681 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3682 return false;
3683
bffb8014 3684 stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
91ff1504 3685
22e4dee7
RH
3686 mode = TYPE_MODE (vectype);
3687
d9f21f6a 3688 /* At the moment, all permutations are represented using per-element
928686b1
RS
3689 indices, so we can't cope with variable vector lengths or
3690 vectorization factors. */
3691 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
3692 || !vf.is_constant (&const_vf))
d9f21f6a
RS
3693 return false;
3694
2635892a
RH
3695 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3696 same size as the vector element being permuted. */
96f9265a 3697 mask_element_type = lang_hooks.types.type_for_mode
304b9962 3698 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))).require (), 1);
ebfd146a 3699 mask_type = get_vectype_for_scalar_type (mask_element_type);
e3342de4 3700 vec_perm_builder mask (nunits, nunits, 1);
908a1a16 3701 mask.quick_grow (nunits);
e3342de4 3702 vec_perm_indices indices;
ebfd146a 3703
61fdfd8c
RB
3704 /* Initialize the vect stmts of NODE to properly insert the generated
3705 stmts later. */
3706 if (! analyze_only)
3707 for (unsigned i = SLP_TREE_VEC_STMTS (node).length ();
3708 i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3709 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
ebfd146a 3710
b8698a0f
L
3711 /* Generate permutation masks for every NODE. Number of masks for each NODE
3712 is equal to GROUP_SIZE.
3713 E.g., we have a group of three nodes with three loads from the same
3714 location in each node, and the vector size is 4. I.e., we have a
3715 a0b0c0a1b1c1... sequence and we need to create the following vectors:
ebfd146a
IR
3716 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3717 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3718 ...
3719
2635892a 3720 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
b8698a0f 3721 The last mask is illegal since we assume two operands for permute
ff802fa1
IR
3722 operation, and the mask element values can't be outside that range.
3723 Hence, the last mask must be converted into {2,5,5,5}.
b8698a0f 3724 For the first two permutations we need the first and the second input
ebfd146a 3725 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
b8698a0f 3726 we need the second and the third vectors: {b1,c1,a2,b2} and
ebfd146a
IR
3727 {c2,a3,b3,c3}. */
3728
2ce27200 3729 int vect_stmts_counter = 0;
928686b1 3730 unsigned int index = 0;
2ce27200
RB
3731 int first_vec_index = -1;
3732 int second_vec_index = -1;
be377c80 3733 bool noop_p = true;
29afecdf 3734 *n_perms = 0;
ebfd146a 3735
d9f21f6a 3736 for (unsigned int j = 0; j < const_vf; j++)
2ce27200
RB
3737 {
3738 for (int k = 0; k < group_size; k++)
3739 {
928686b1 3740 unsigned int i = (SLP_TREE_LOAD_PERMUTATION (node)[k]
2c53b149 3741 + j * DR_GROUP_SIZE (stmt_info));
2ce27200
RB
3742 vec_index = i / nunits;
3743 mask_element = i % nunits;
3744 if (vec_index == first_vec_index
3745 || first_vec_index == -1)
3746 {
3747 first_vec_index = vec_index;
3748 }
3749 else if (vec_index == second_vec_index
3750 || second_vec_index == -1)
3751 {
3752 second_vec_index = vec_index;
3753 mask_element += nunits;
3754 }
3755 else
3756 {
3757 if (dump_enabled_p ())
3758 {
3759 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3760 "permutation requires at "
3761 "least three vectors ");
3762 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
b9787581 3763 stmt_info->stmt, 0);
2ce27200 3764 }
31bee964 3765 gcc_assert (analyze_only);
2ce27200
RB
3766 return false;
3767 }
ebfd146a 3768
928686b1 3769 gcc_assert (mask_element < 2 * nunits);
be377c80
RB
3770 if (mask_element != index)
3771 noop_p = false;
2ce27200
RB
3772 mask[index++] = mask_element;
3773
e3342de4 3774 if (index == nunits && !noop_p)
2ce27200 3775 {
e3342de4
RS
3776 indices.new_vector (mask, 2, nunits);
3777 if (!can_vec_perm_const_p (mode, indices))
2ce27200
RB
3778 {
3779 if (dump_enabled_p ())
22e4dee7 3780 {
2ce27200
RB
3781 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3782 vect_location,
3783 "unsupported vect permute { ");
3784 for (i = 0; i < nunits; ++i)
6b0630fb
RS
3785 {
3786 dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
3787 dump_printf (MSG_MISSED_OPTIMIZATION, " ");
3788 }
2ce27200 3789 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
22e4dee7 3790 }
31bee964 3791 gcc_assert (analyze_only);
2ce27200
RB
3792 return false;
3793 }
22e4dee7 3794
e3342de4
RS
3795 ++*n_perms;
3796 }
29afecdf 3797
e3342de4
RS
3798 if (index == nunits)
3799 {
2ce27200
RB
3800 if (!analyze_only)
3801 {
be377c80
RB
3802 tree mask_vec = NULL_TREE;
3803
3804 if (! noop_p)
736d0f28 3805 mask_vec = vec_perm_indices_to_tree (mask_type, indices);
2ce27200
RB
3806
3807 if (second_vec_index == -1)
3808 second_vec_index = first_vec_index;
61fdfd8c
RB
3809
3810 /* Generate the permute statement if necessary. */
3811 tree first_vec = dr_chain[first_vec_index];
3812 tree second_vec = dr_chain[second_vec_index];
16edaeb8 3813 stmt_vec_info perm_stmt_info;
61fdfd8c
RB
3814 if (! noop_p)
3815 {
b9787581 3816 gassign *stmt = as_a <gassign *> (stmt_info->stmt);
61fdfd8c
RB
3817 tree perm_dest
3818 = vect_create_destination_var (gimple_assign_lhs (stmt),
3819 vectype);
3820 perm_dest = make_ssa_name (perm_dest);
16edaeb8
RS
3821 gassign *perm_stmt
3822 = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
3823 first_vec, second_vec,
3824 mask_vec);
3825 perm_stmt_info
b9787581
RS
3826 = vect_finish_stmt_generation (stmt_info, perm_stmt,
3827 gsi);
61fdfd8c
RB
3828 }
3829 else
3830 /* If mask was NULL_TREE generate the requested
3831 identity transform. */
16edaeb8 3832 perm_stmt_info = vinfo->lookup_def (first_vec);
61fdfd8c
RB
3833
3834 /* Store the vector statement in NODE. */
16edaeb8
RS
3835 SLP_TREE_VEC_STMTS (node)[vect_stmts_counter++]
3836 = perm_stmt_info;
2ce27200 3837 }
ebfd146a 3838
2ce27200
RB
3839 index = 0;
3840 first_vec_index = -1;
3841 second_vec_index = -1;
be377c80 3842 noop_p = true;
2ce27200
RB
3843 }
3844 }
b8698a0f 3845 }
ebfd146a 3846
ebfd146a
IR
3847 return true;
3848}
3849
ebfd146a
IR
3850/* Vectorize SLP instance tree in postorder. */
3851
3852static bool
f7300fff
RB
3853vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3854 scalar_stmts_to_slp_tree_map_t *bst_map)
ebfd146a 3855{
0d0293ac 3856 bool grouped_store, is_store;
ebfd146a
IR
3857 gimple_stmt_iterator si;
3858 stmt_vec_info stmt_info;
8b7e9dba 3859 unsigned int group_size;
ebfd146a 3860 tree vectype;
603cca93 3861 int i, j;
d755c7ef 3862 slp_tree child;
ebfd146a 3863
603cca93 3864 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
ebfd146a
IR
3865 return false;
3866
f7300fff
RB
3867 /* See if we have already vectorized the same set of stmts and reuse their
3868 vectorized stmts. */
dd172744 3869 if (slp_tree *leader = bst_map->get (SLP_TREE_SCALAR_STMTS (node)))
f7300fff 3870 {
dd172744 3871 SLP_TREE_VEC_STMTS (node).safe_splice (SLP_TREE_VEC_STMTS (*leader));
f7300fff
RB
3872 return false;
3873 }
3874
dd172744 3875 bst_map->put (SLP_TREE_SCALAR_STMTS (node).copy (), node);
9771b263 3876 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
f7300fff 3877 vect_schedule_slp_instance (child, instance, bst_map);
b8698a0f 3878
603cca93
RB
3879 /* Push SLP node def-type to stmts. */
3880 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3881 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
b9787581
RS
3882 {
3883 stmt_vec_info child_stmt_info;
3884 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, child_stmt_info)
3885 STMT_VINFO_DEF_TYPE (child_stmt_info) = SLP_TREE_DEF_TYPE (child);
3886 }
603cca93 3887
b9787581 3888 stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
ebfd146a
IR
3889
3890 /* VECTYPE is the type of the destination. */
b690cc0f 3891 vectype = STMT_VINFO_VECTYPE (stmt_info);
dad55d70 3892 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
ebfd146a
IR
3893 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3894
68435eb2 3895 gcc_assert (SLP_TREE_NUMBER_OF_VEC_STMTS (node) != 0);
9771b263 3896 if (!SLP_TREE_VEC_STMTS (node).exists ())
8b7e9dba 3897 SLP_TREE_VEC_STMTS (node).create (SLP_TREE_NUMBER_OF_VEC_STMTS (node));
ebfd146a 3898
73fbfcad 3899 if (dump_enabled_p ())
ebfd146a 3900 {
78c60e3d
SS
3901 dump_printf_loc (MSG_NOTE,vect_location,
3902 "------>vectorizing SLP node starting from: ");
b9787581 3903 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt_info->stmt, 0);
b8698a0f 3904 }
ebfd146a 3905
2e8ab70c
RB
3906 /* Vectorized stmts go before the last scalar stmt which is where
3907 all uses are ready. */
95c68311
RS
3908 stmt_vec_info last_stmt_info = vect_find_last_scalar_stmt_in_slp (node);
3909 si = gsi_for_stmt (last_stmt_info->stmt);
e4a707c4 3910
b010117a
IR
3911 /* Mark the first element of the reduction chain as reduction to properly
3912 transform the node. In the analysis phase only the last element of the
3913 chain is marked as reduction. */
2c53b149
RB
3914 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info)
3915 && REDUC_GROUP_FIRST_ELEMENT (stmt_info)
b9787581 3916 && REDUC_GROUP_FIRST_ELEMENT (stmt_info) == stmt_info)
b010117a
IR
3917 {
3918 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3919 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3920 }
3921
6876e5bc
RB
3922 /* Handle two-operation SLP nodes by vectorizing the group with
3923 both operations and then performing a merge. */
3924 if (SLP_TREE_TWO_OPERATORS (node))
3925 {
b9787581 3926 gassign *stmt = as_a <gassign *> (stmt_info->stmt);
6876e5bc 3927 enum tree_code code0 = gimple_assign_rhs_code (stmt);
567a3691 3928 enum tree_code ocode = ERROR_MARK;
b9787581 3929 stmt_vec_info ostmt_info;
e3342de4 3930 vec_perm_builder mask (group_size, group_size, 1);
b9787581
RS
3931 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt_info)
3932 {
3933 gassign *ostmt = as_a <gassign *> (ostmt_info->stmt);
3934 if (gimple_assign_rhs_code (ostmt) != code0)
3935 {
3936 mask.quick_push (1);
3937 ocode = gimple_assign_rhs_code (ostmt);
3938 }
3939 else
3940 mask.quick_push (0);
3941 }
567a3691 3942 if (ocode != ERROR_MARK)
6876e5bc 3943 {
16edaeb8
RS
3944 vec<stmt_vec_info> v0;
3945 vec<stmt_vec_info> v1;
6876e5bc
RB
3946 unsigned j;
3947 tree tmask = NULL_TREE;
b9787581 3948 vect_transform_stmt (stmt_info, &si, &grouped_store, node, instance);
6876e5bc
RB
3949 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3950 SLP_TREE_VEC_STMTS (node).truncate (0);
3951 gimple_assign_set_rhs_code (stmt, ocode);
b9787581 3952 vect_transform_stmt (stmt_info, &si, &grouped_store, node, instance);
6876e5bc
RB
3953 gimple_assign_set_rhs_code (stmt, code0);
3954 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3955 SLP_TREE_VEC_STMTS (node).truncate (0);
3956 tree meltype = build_nonstandard_integer_type
b397965c 3957 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (vectype))), 1);
6876e5bc
RB
3958 tree mvectype = get_same_sized_vectype (meltype, vectype);
3959 unsigned k = 0, l;
3960 for (j = 0; j < v0.length (); ++j)
3961 {
dad55d70
RS
3962 /* Enforced by vect_build_slp_tree, which rejects variable-length
3963 vectors for SLP_TREE_TWO_OPERATORS. */
3964 unsigned int const_nunits = nunits.to_constant ();
3965 tree_vector_builder melts (mvectype, const_nunits, 1);
3966 for (l = 0; l < const_nunits; ++l)
6876e5bc 3967 {
1ece8d4c 3968 if (k >= group_size)
6876e5bc 3969 k = 0;
dad55d70
RS
3970 tree t = build_int_cst (meltype,
3971 mask[k++] * const_nunits + l);
794e3180 3972 melts.quick_push (t);
6876e5bc 3973 }
5ebaa477 3974 tmask = melts.build ();
6876e5bc
RB
3975
3976 /* ??? Not all targets support a VEC_PERM_EXPR with a
3977 constant mask that would translate to a vec_merge RTX
3978 (with their vec_perm_const_ok). We can either not
3979 vectorize in that case or let veclower do its job.
3980 Unfortunately that isn't too great and at least for
3981 plus/minus we'd eventually like to match targets
3982 vector addsub instructions. */
355fe088 3983 gimple *vstmt;
6876e5bc
RB
3984 vstmt = gimple_build_assign (make_ssa_name (vectype),
3985 VEC_PERM_EXPR,
16edaeb8
RS
3986 gimple_assign_lhs (v0[j]->stmt),
3987 gimple_assign_lhs (v1[j]->stmt),
3988 tmask);
3989 SLP_TREE_VEC_STMTS (node).quick_push
b9787581 3990 (vect_finish_stmt_generation (stmt_info, vstmt, &si));
6876e5bc
RB
3991 }
3992 v0.release ();
3993 v1.release ();
3994 return false;
3995 }
3996 }
b9787581
RS
3997 is_store = vect_transform_stmt (stmt_info, &si, &grouped_store, node,
3998 instance);
603cca93
RB
3999
4000 /* Restore stmt def-types. */
4001 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
4002 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
b9787581
RS
4003 {
4004 stmt_vec_info child_stmt_info;
4005 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, child_stmt_info)
4006 STMT_VINFO_DEF_TYPE (child_stmt_info) = vect_internal_def;
4007 }
603cca93 4008
b5aeb3bb 4009 return is_store;
ebfd146a
IR
4010}
4011
dd34c087
JJ
4012/* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
4013 For loop vectorization this is done in vectorizable_call, but for SLP
4014 it needs to be deferred until end of vect_schedule_slp, because multiple
4015 SLP instances may refer to the same scalar stmt. */
4016
4017static void
4018vect_remove_slp_scalar_calls (slp_tree node)
4019{
b9787581 4020 gimple *new_stmt;
dd34c087
JJ
4021 gimple_stmt_iterator gsi;
4022 int i;
d755c7ef 4023 slp_tree child;
dd34c087
JJ
4024 tree lhs;
4025 stmt_vec_info stmt_info;
4026
603cca93 4027 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
dd34c087
JJ
4028 return;
4029
9771b263 4030 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
d755c7ef 4031 vect_remove_slp_scalar_calls (child);
dd34c087 4032
b9787581 4033 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt_info)
dd34c087 4034 {
b9787581
RS
4035 gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
4036 if (!stmt || gimple_bb (stmt) == NULL)
dd34c087 4037 continue;
b9787581 4038 if (is_pattern_stmt_p (stmt_info)
dd34c087
JJ
4039 || !PURE_SLP_STMT (stmt_info))
4040 continue;
4041 lhs = gimple_call_lhs (stmt);
4042 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
dd34c087 4043 gsi = gsi_for_stmt (stmt);
9d97912b 4044 stmt_info->vinfo->replace_stmt (&gsi, stmt_info, new_stmt);
dd34c087
JJ
4045 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
4046 }
4047}
ebfd146a 4048
ff802fa1
IR
4049/* Generate vector code for all SLP instances in the loop/basic block. */
4050
ebfd146a 4051bool
310213d4 4052vect_schedule_slp (vec_info *vinfo)
ebfd146a 4053{
9771b263 4054 vec<slp_instance> slp_instances;
ebfd146a 4055 slp_instance instance;
8b7e9dba 4056 unsigned int i;
ebfd146a
IR
4057 bool is_store = false;
4058
78604de0
RB
4059
4060 scalar_stmts_to_slp_tree_map_t *bst_map
4061 = new scalar_stmts_to_slp_tree_map_t ();
310213d4 4062 slp_instances = vinfo->slp_instances;
9771b263 4063 FOR_EACH_VEC_ELT (slp_instances, i, instance)
ebfd146a
IR
4064 {
4065 /* Schedule the tree of INSTANCE. */
4066 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
f7300fff 4067 instance, bst_map);
73fbfcad 4068 if (dump_enabled_p ())
78c60e3d 4069 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4070 "vectorizing stmts using SLP.\n");
ebfd146a 4071 }
78604de0 4072 delete bst_map;
ebfd146a 4073
9771b263 4074 FOR_EACH_VEC_ELT (slp_instances, i, instance)
b5aeb3bb
IR
4075 {
4076 slp_tree root = SLP_INSTANCE_TREE (instance);
b9787581 4077 stmt_vec_info store_info;
b5aeb3bb 4078 unsigned int j;
b5aeb3bb 4079
c40eced0
RB
4080 /* Remove scalar call stmts. Do not do this for basic-block
4081 vectorization as not all uses may be vectorized.
4082 ??? Why should this be necessary? DCE should be able to
4083 remove the stmts itself.
4084 ??? For BB vectorization we can as well remove scalar
4085 stmts starting from the SLP tree root if they have no
4086 uses. */
310213d4 4087 if (is_a <loop_vec_info> (vinfo))
c40eced0 4088 vect_remove_slp_scalar_calls (root);
dd34c087 4089
b9787581 4090 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store_info)
b5aeb3bb
IR
4091 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
4092 {
b9787581
RS
4093 if (!STMT_VINFO_DATA_REF (store_info))
4094 break;
4095
4096 if (is_pattern_stmt_p (store_info))
4097 store_info = STMT_VINFO_RELATED_STMT (store_info);
4098 /* Free the attached stmt_vec_info and remove the stmt. */
b5b56c2a 4099 vinfo->remove_stmt (store_info);
b5aeb3bb
IR
4100 }
4101 }
4102
ebfd146a
IR
4103 return is_store;
4104}