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