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