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