var-tracking.c (vt_add_function_parameter): Adjust for VEC changes.
[official-gcc.git] / gcc / tree-vect-slp.c
blob11dbdfb5a02436f3c1f513beeb0eccc5ee6be8d4
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>
7 This file is part of GCC.
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.
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.
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/>. */
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"
41 /* Extract the location of the basic block in the source code.
42 Return the basic block location if succeed and NULL if not. */
44 LOC
45 find_bb_location (basic_block bb)
47 gimple stmt = NULL;
48 gimple_stmt_iterator si;
50 if (!bb)
51 return UNKNOWN_LOC;
53 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
55 stmt = gsi_stmt (si);
56 if (gimple_location (stmt) != UNKNOWN_LOC)
57 return gimple_location (stmt);
60 return UNKNOWN_LOC;
64 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
66 static void
67 vect_free_slp_tree (slp_tree node)
69 int i;
70 slp_void_p child;
72 if (!node)
73 return;
75 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
76 vect_free_slp_tree ((slp_tree) child);
78 VEC_free (slp_void_p, heap, SLP_TREE_CHILDREN (node));
79 VEC_free (gimple, heap, SLP_TREE_SCALAR_STMTS (node));
81 if (SLP_TREE_VEC_STMTS (node))
82 VEC_free (gimple, heap, SLP_TREE_VEC_STMTS (node));
84 free (node);
88 /* Free the memory allocated for the SLP instance. */
90 void
91 vect_free_slp_instance (slp_instance instance)
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 free (instance);
101 /* Create an SLP node for SCALAR_STMTS. */
103 static slp_tree
104 vect_create_new_slp_node (VEC (gimple, heap) *scalar_stmts)
106 slp_tree node;
107 gimple stmt = VEC_index (gimple, scalar_stmts, 0);
108 unsigned int nops;
110 if (is_gimple_call (stmt))
111 nops = gimple_call_num_args (stmt);
112 else if (is_gimple_assign (stmt))
114 nops = gimple_num_ops (stmt) - 1;
115 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
116 nops++;
118 else
119 return NULL;
121 node = XNEW (struct _slp_tree);
122 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
123 SLP_TREE_VEC_STMTS (node) = NULL;
124 SLP_TREE_CHILDREN (node) = VEC_alloc (slp_void_p, heap, nops);
126 return node;
130 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
131 operand. */
132 static VEC (slp_oprnd_info, heap) *
133 vect_create_oprnd_info (int nops, int group_size)
135 int i;
136 slp_oprnd_info oprnd_info;
137 VEC (slp_oprnd_info, heap) *oprnds_info;
139 oprnds_info = VEC_alloc (slp_oprnd_info, heap, nops);
140 for (i = 0; i < nops; i++)
142 oprnd_info = XNEW (struct _slp_oprnd_info);
143 oprnd_info->def_stmts = VEC_alloc (gimple, heap, group_size);
144 oprnd_info->first_dt = vect_uninitialized_def;
145 oprnd_info->first_def_type = NULL_TREE;
146 oprnd_info->first_const_oprnd = NULL_TREE;
147 oprnd_info->first_pattern = false;
148 VEC_quick_push (slp_oprnd_info, oprnds_info, oprnd_info);
151 return oprnds_info;
155 /* Free operands info. */
157 static void
158 vect_free_oprnd_info (VEC (slp_oprnd_info, heap) **oprnds_info)
160 int i;
161 slp_oprnd_info oprnd_info;
163 FOR_EACH_VEC_ELT (slp_oprnd_info, *oprnds_info, i, oprnd_info)
165 VEC_free (gimple, heap, oprnd_info->def_stmts);
166 XDELETE (oprnd_info);
169 VEC_free (slp_oprnd_info, heap, *oprnds_info);
173 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
174 they are of a valid type and that they match the defs of the first stmt of
175 the SLP group (stored in OPRNDS_INFO). */
177 static bool
178 vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
179 slp_tree slp_node, gimple stmt,
180 int ncopies_for_cost, bool first,
181 VEC (slp_oprnd_info, heap) **oprnds_info,
182 stmt_vector_for_cost *prologue_cost_vec,
183 stmt_vector_for_cost *body_cost_vec)
185 tree oprnd;
186 unsigned int i, number_of_oprnds;
187 tree def, def_op0 = NULL_TREE;
188 gimple def_stmt;
189 enum vect_def_type dt = vect_uninitialized_def;
190 enum vect_def_type dt_op0 = vect_uninitialized_def;
191 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
192 tree lhs = gimple_get_lhs (stmt);
193 struct loop *loop = NULL;
194 enum tree_code rhs_code;
195 bool different_types = false;
196 bool pattern = false;
197 slp_oprnd_info oprnd_info, oprnd0_info, oprnd1_info;
198 int op_idx = 1;
199 tree compare_rhs = NULL_TREE;
201 if (loop_vinfo)
202 loop = LOOP_VINFO_LOOP (loop_vinfo);
204 if (is_gimple_call (stmt))
206 number_of_oprnds = gimple_call_num_args (stmt);
207 op_idx = 3;
209 else if (is_gimple_assign (stmt))
211 number_of_oprnds = gimple_num_ops (stmt) - 1;
212 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
213 number_of_oprnds++;
215 else
216 return false;
218 for (i = 0; i < number_of_oprnds; i++)
220 if (compare_rhs)
222 oprnd = compare_rhs;
223 compare_rhs = NULL_TREE;
225 else
226 oprnd = gimple_op (stmt, op_idx++);
228 oprnd_info = VEC_index (slp_oprnd_info, *oprnds_info, i);
230 if (COMPARISON_CLASS_P (oprnd))
232 compare_rhs = TREE_OPERAND (oprnd, 1);
233 oprnd = TREE_OPERAND (oprnd, 0);
236 if (!vect_is_simple_use (oprnd, NULL, loop_vinfo, bb_vinfo, &def_stmt,
237 &def, &dt)
238 || (!def_stmt && dt != vect_constant_def))
240 if (vect_print_dump_info (REPORT_SLP))
242 fprintf (vect_dump, "Build SLP failed: can't find def for ");
243 print_generic_expr (vect_dump, oprnd, TDF_SLIM);
246 return false;
249 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
250 from the pattern. Check that all the stmts of the node are in the
251 pattern. */
252 if (def_stmt && gimple_bb (def_stmt)
253 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
254 || (!loop && gimple_bb (def_stmt) == BB_VINFO_BB (bb_vinfo)
255 && gimple_code (def_stmt) != GIMPLE_PHI))
256 && vinfo_for_stmt (def_stmt)
257 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
258 && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
259 && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
261 pattern = true;
262 if (!first && !oprnd_info->first_pattern)
264 if (vect_print_dump_info (REPORT_DETAILS))
266 fprintf (vect_dump, "Build SLP failed: some of the stmts"
267 " are in a pattern, and others are not ");
268 print_generic_expr (vect_dump, oprnd, TDF_SLIM);
271 return false;
274 def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
275 dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
277 if (dt == vect_unknown_def_type)
279 if (vect_print_dump_info (REPORT_DETAILS))
280 fprintf (vect_dump, "Unsupported pattern.");
281 return false;
284 switch (gimple_code (def_stmt))
286 case GIMPLE_PHI:
287 def = gimple_phi_result (def_stmt);
288 break;
290 case GIMPLE_ASSIGN:
291 def = gimple_assign_lhs (def_stmt);
292 break;
294 default:
295 if (vect_print_dump_info (REPORT_DETAILS))
296 fprintf (vect_dump, "unsupported defining stmt: ");
297 return false;
301 if (first)
303 oprnd_info->first_dt = dt;
304 oprnd_info->first_pattern = pattern;
305 if (def)
307 oprnd_info->first_def_type = TREE_TYPE (def);
308 oprnd_info->first_const_oprnd = NULL_TREE;
310 else
312 oprnd_info->first_def_type = NULL_TREE;
313 oprnd_info->first_const_oprnd = oprnd;
316 if (i == 0)
318 def_op0 = def;
319 dt_op0 = dt;
320 /* Analyze costs (for the first stmt of the group only). */
321 if (REFERENCE_CLASS_P (lhs))
322 /* Store. */
323 vect_model_store_cost (stmt_info, ncopies_for_cost, false,
324 dt, slp_node, prologue_cost_vec,
325 body_cost_vec);
326 else
328 enum vect_def_type dts[2];
329 dts[0] = dt;
330 dts[1] = vect_uninitialized_def;
331 /* Not memory operation (we don't call this function for
332 loads). */
333 vect_model_simple_cost (stmt_info, ncopies_for_cost, dts,
334 prologue_cost_vec, body_cost_vec);
338 else
340 /* Not first stmt of the group, check that the def-stmt/s match
341 the def-stmt/s of the first stmt. Allow different definition
342 types for reduction chains: the first stmt must be a
343 vect_reduction_def (a phi node), and the rest
344 vect_internal_def. */
345 if (((oprnd_info->first_dt != dt
346 && !(oprnd_info->first_dt == vect_reduction_def
347 && dt == vect_internal_def))
348 || (oprnd_info->first_def_type != NULL_TREE
349 && def
350 && !types_compatible_p (oprnd_info->first_def_type,
351 TREE_TYPE (def))))
352 || (!def
353 && !types_compatible_p (TREE_TYPE (oprnd_info->first_const_oprnd),
354 TREE_TYPE (oprnd)))
355 || different_types)
357 if (number_of_oprnds != 2)
359 if (vect_print_dump_info (REPORT_SLP))
360 fprintf (vect_dump, "Build SLP failed: different types ");
362 return false;
365 /* Try to swap operands in case of binary operation. */
366 if (i == 0)
367 different_types = true;
368 else
370 oprnd0_info = VEC_index (slp_oprnd_info, *oprnds_info, 0);
371 if (is_gimple_assign (stmt)
372 && (rhs_code = gimple_assign_rhs_code (stmt))
373 && TREE_CODE_CLASS (rhs_code) == tcc_binary
374 && commutative_tree_code (rhs_code)
375 && oprnd0_info->first_dt == dt
376 && oprnd_info->first_dt == dt_op0
377 && def_op0 && def
378 && !(oprnd0_info->first_def_type
379 && !types_compatible_p (oprnd0_info->first_def_type,
380 TREE_TYPE (def)))
381 && !(oprnd_info->first_def_type
382 && !types_compatible_p (oprnd_info->first_def_type,
383 TREE_TYPE (def_op0))))
385 if (vect_print_dump_info (REPORT_SLP))
387 fprintf (vect_dump, "Swapping operands of ");
388 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
391 swap_tree_operands (stmt, gimple_assign_rhs1_ptr (stmt),
392 gimple_assign_rhs2_ptr (stmt));
394 else
396 if (vect_print_dump_info (REPORT_SLP))
397 fprintf (vect_dump, "Build SLP failed: different types ");
399 return false;
405 /* Check the types of the definitions. */
406 switch (dt)
408 case vect_constant_def:
409 case vect_external_def:
410 case vect_reduction_def:
411 break;
413 case vect_internal_def:
414 if (different_types)
416 oprnd0_info = VEC_index (slp_oprnd_info, *oprnds_info, 0);
417 oprnd1_info = VEC_index (slp_oprnd_info, *oprnds_info, 0);
418 if (i == 0)
419 VEC_quick_push (gimple, oprnd1_info->def_stmts, def_stmt);
420 else
421 VEC_quick_push (gimple, oprnd0_info->def_stmts, def_stmt);
423 else
424 VEC_quick_push (gimple, oprnd_info->def_stmts, def_stmt);
426 break;
428 default:
429 /* FORNOW: Not supported. */
430 if (vect_print_dump_info (REPORT_SLP))
432 fprintf (vect_dump, "Build SLP failed: illegal type of def ");
433 print_generic_expr (vect_dump, def, TDF_SLIM);
436 return false;
440 return true;
444 /* Recursively build an SLP tree starting from NODE.
445 Fail (and return FALSE) if def-stmts are not isomorphic, require data
446 permutation or are of unsupported types of operation. Otherwise, return
447 TRUE. */
449 static bool
450 vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
451 slp_tree *node, unsigned int group_size, int *outside_cost,
452 int ncopies_for_cost, unsigned int *max_nunits,
453 VEC (int, heap) **load_permutation,
454 VEC (slp_tree, heap) **loads,
455 unsigned int vectorization_factor, bool *loads_permuted,
456 stmt_vector_for_cost *prologue_cost_vec,
457 stmt_vector_for_cost *body_cost_vec)
459 unsigned int i;
460 VEC (gimple, heap) *stmts = SLP_TREE_SCALAR_STMTS (*node);
461 gimple stmt = VEC_index (gimple, stmts, 0);
462 enum tree_code first_stmt_code = ERROR_MARK, rhs_code = ERROR_MARK;
463 enum tree_code first_cond_code = ERROR_MARK;
464 tree lhs;
465 bool stop_recursion = false, need_same_oprnds = false;
466 tree vectype, scalar_type, first_op1 = NULL_TREE;
467 unsigned int ncopies;
468 optab optab;
469 int icode;
470 enum machine_mode optab_op2_mode;
471 enum machine_mode vec_mode;
472 struct data_reference *first_dr;
473 HOST_WIDE_INT dummy;
474 bool permutation = false;
475 unsigned int load_place;
476 gimple first_load = NULL, prev_first_load = NULL, old_first_load = NULL;
477 VEC (slp_oprnd_info, heap) *oprnds_info;
478 unsigned int nops;
479 slp_oprnd_info oprnd_info;
480 tree cond;
482 if (is_gimple_call (stmt))
483 nops = gimple_call_num_args (stmt);
484 else if (is_gimple_assign (stmt))
486 nops = gimple_num_ops (stmt) - 1;
487 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
488 nops++;
490 else
491 return false;
493 oprnds_info = vect_create_oprnd_info (nops, group_size);
495 /* For every stmt in NODE find its def stmt/s. */
496 FOR_EACH_VEC_ELT (gimple, stmts, i, stmt)
498 if (vect_print_dump_info (REPORT_SLP))
500 fprintf (vect_dump, "Build SLP for ");
501 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
504 /* Fail to vectorize statements marked as unvectorizable. */
505 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
507 if (vect_print_dump_info (REPORT_SLP))
509 fprintf (vect_dump,
510 "Build SLP failed: unvectorizable statement ");
511 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
514 vect_free_oprnd_info (&oprnds_info);
515 return false;
518 lhs = gimple_get_lhs (stmt);
519 if (lhs == NULL_TREE)
521 if (vect_print_dump_info (REPORT_SLP))
523 fprintf (vect_dump,
524 "Build SLP failed: not GIMPLE_ASSIGN nor GIMPLE_CALL ");
525 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
528 vect_free_oprnd_info (&oprnds_info);
529 return false;
532 if (is_gimple_assign (stmt)
533 && gimple_assign_rhs_code (stmt) == COND_EXPR
534 && (cond = gimple_assign_rhs1 (stmt))
535 && !COMPARISON_CLASS_P (cond))
537 if (vect_print_dump_info (REPORT_SLP))
539 fprintf (vect_dump,
540 "Build SLP failed: condition is not comparison ");
541 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
544 vect_free_oprnd_info (&oprnds_info);
545 return false;
548 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
549 vectype = get_vectype_for_scalar_type (scalar_type);
550 if (!vectype)
552 if (vect_print_dump_info (REPORT_SLP))
554 fprintf (vect_dump, "Build SLP failed: unsupported data-type ");
555 print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
558 vect_free_oprnd_info (&oprnds_info);
559 return false;
562 /* In case of multiple types we need to detect the smallest type. */
563 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
565 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
566 if (bb_vinfo)
567 vectorization_factor = *max_nunits;
570 ncopies = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
572 if (is_gimple_call (stmt))
574 rhs_code = CALL_EXPR;
575 if (gimple_call_internal_p (stmt)
576 || gimple_call_tail_p (stmt)
577 || gimple_call_noreturn_p (stmt)
578 || !gimple_call_nothrow_p (stmt)
579 || gimple_call_chain (stmt))
581 if (vect_print_dump_info (REPORT_SLP))
583 fprintf (vect_dump,
584 "Build SLP failed: unsupported call type ");
585 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
588 vect_free_oprnd_info (&oprnds_info);
589 return false;
592 else
593 rhs_code = gimple_assign_rhs_code (stmt);
595 /* Check the operation. */
596 if (i == 0)
598 first_stmt_code = rhs_code;
600 /* Shift arguments should be equal in all the packed stmts for a
601 vector shift with scalar shift operand. */
602 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
603 || rhs_code == LROTATE_EXPR
604 || rhs_code == RROTATE_EXPR)
606 vec_mode = TYPE_MODE (vectype);
608 /* First see if we have a vector/vector shift. */
609 optab = optab_for_tree_code (rhs_code, vectype,
610 optab_vector);
612 if (!optab
613 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
615 /* No vector/vector shift, try for a vector/scalar shift. */
616 optab = optab_for_tree_code (rhs_code, vectype,
617 optab_scalar);
619 if (!optab)
621 if (vect_print_dump_info (REPORT_SLP))
622 fprintf (vect_dump, "Build SLP failed: no optab.");
623 vect_free_oprnd_info (&oprnds_info);
624 return false;
626 icode = (int) optab_handler (optab, vec_mode);
627 if (icode == CODE_FOR_nothing)
629 if (vect_print_dump_info (REPORT_SLP))
630 fprintf (vect_dump, "Build SLP failed: "
631 "op not supported by target.");
632 vect_free_oprnd_info (&oprnds_info);
633 return false;
635 optab_op2_mode = insn_data[icode].operand[2].mode;
636 if (!VECTOR_MODE_P (optab_op2_mode))
638 need_same_oprnds = true;
639 first_op1 = gimple_assign_rhs2 (stmt);
643 else if (rhs_code == WIDEN_LSHIFT_EXPR)
645 need_same_oprnds = true;
646 first_op1 = gimple_assign_rhs2 (stmt);
649 else
651 if (first_stmt_code != rhs_code
652 && (first_stmt_code != IMAGPART_EXPR
653 || rhs_code != REALPART_EXPR)
654 && (first_stmt_code != REALPART_EXPR
655 || rhs_code != IMAGPART_EXPR)
656 && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
657 && (first_stmt_code == ARRAY_REF
658 || first_stmt_code == INDIRECT_REF
659 || first_stmt_code == COMPONENT_REF
660 || first_stmt_code == MEM_REF)))
662 if (vect_print_dump_info (REPORT_SLP))
664 fprintf (vect_dump,
665 "Build SLP failed: different operation in stmt ");
666 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
669 vect_free_oprnd_info (&oprnds_info);
670 return false;
673 if (need_same_oprnds
674 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
676 if (vect_print_dump_info (REPORT_SLP))
678 fprintf (vect_dump,
679 "Build SLP failed: different shift arguments in ");
680 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
683 vect_free_oprnd_info (&oprnds_info);
684 return false;
687 if (rhs_code == CALL_EXPR)
689 gimple first_stmt = VEC_index (gimple, stmts, 0);
690 if (gimple_call_num_args (stmt) != nops
691 || !operand_equal_p (gimple_call_fn (first_stmt),
692 gimple_call_fn (stmt), 0)
693 || gimple_call_fntype (first_stmt)
694 != gimple_call_fntype (stmt))
696 if (vect_print_dump_info (REPORT_SLP))
698 fprintf (vect_dump,
699 "Build SLP failed: different calls in ");
700 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
703 vect_free_oprnd_info (&oprnds_info);
704 return false;
709 /* Grouped store or load. */
710 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
712 if (REFERENCE_CLASS_P (lhs))
714 /* Store. */
715 if (!vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo, *node,
716 stmt, ncopies_for_cost,
717 (i == 0), &oprnds_info,
718 prologue_cost_vec,
719 body_cost_vec))
721 vect_free_oprnd_info (&oprnds_info);
722 return false;
725 else
727 /* Load. */
728 /* FORNOW: Check that there is no gap between the loads. */
729 if ((GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
730 && GROUP_GAP (vinfo_for_stmt (stmt)) != 0)
731 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != stmt
732 && GROUP_GAP (vinfo_for_stmt (stmt)) != 1))
734 if (vect_print_dump_info (REPORT_SLP))
736 fprintf (vect_dump, "Build SLP failed: grouped "
737 "loads have gaps ");
738 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
741 vect_free_oprnd_info (&oprnds_info);
742 return false;
745 /* Check that the size of interleaved loads group is not
746 greater than the SLP group size. */
747 if (loop_vinfo
748 && GROUP_SIZE (vinfo_for_stmt (stmt)) > ncopies * group_size)
750 if (vect_print_dump_info (REPORT_SLP))
752 fprintf (vect_dump, "Build SLP failed: the number of "
753 "interleaved loads is greater than"
754 " the SLP group size ");
755 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
758 vect_free_oprnd_info (&oprnds_info);
759 return false;
762 old_first_load = first_load;
763 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
764 if (prev_first_load)
766 /* Check that there are no loads from different interleaving
767 chains in the same node. The only exception is complex
768 numbers. */
769 if (prev_first_load != first_load
770 && rhs_code != REALPART_EXPR
771 && rhs_code != IMAGPART_EXPR)
773 if (vect_print_dump_info (REPORT_SLP))
775 fprintf (vect_dump, "Build SLP failed: different "
776 "interleaving chains in one node ");
777 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
780 vect_free_oprnd_info (&oprnds_info);
781 return false;
784 else
785 prev_first_load = first_load;
787 /* In some cases a group of loads is just the same load
788 repeated N times. Only analyze its cost once. */
789 if (first_load == stmt && old_first_load != first_load)
791 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
792 if (vect_supportable_dr_alignment (first_dr, false)
793 == dr_unaligned_unsupported)
795 if (vect_print_dump_info (REPORT_SLP))
797 fprintf (vect_dump, "Build SLP failed: unsupported "
798 "unaligned load ");
799 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
802 vect_free_oprnd_info (&oprnds_info);
803 return false;
806 /* Analyze costs (for the first stmt in the group). */
807 vect_model_load_cost (vinfo_for_stmt (stmt),
808 ncopies_for_cost, false, *node,
809 prologue_cost_vec, body_cost_vec);
812 /* Store the place of this load in the interleaving chain. In
813 case that permutation is needed we later decide if a specific
814 permutation is supported. */
815 load_place = vect_get_place_in_interleaving_chain (stmt,
816 first_load);
817 if (load_place != i)
818 permutation = true;
820 VEC_safe_push (int, heap, *load_permutation, load_place);
822 /* We stop the tree when we reach a group of loads. */
823 stop_recursion = true;
824 continue;
826 } /* Grouped access. */
827 else
829 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
831 /* Not grouped load. */
832 if (vect_print_dump_info (REPORT_SLP))
834 fprintf (vect_dump, "Build SLP failed: not grouped load ");
835 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
838 /* FORNOW: Not grouped loads are not supported. */
839 vect_free_oprnd_info (&oprnds_info);
840 return false;
843 /* Not memory operation. */
844 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
845 && TREE_CODE_CLASS (rhs_code) != tcc_unary
846 && rhs_code != COND_EXPR
847 && rhs_code != CALL_EXPR)
849 if (vect_print_dump_info (REPORT_SLP))
851 fprintf (vect_dump, "Build SLP failed: operation");
852 fprintf (vect_dump, " unsupported ");
853 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
856 vect_free_oprnd_info (&oprnds_info);
857 return false;
860 if (rhs_code == COND_EXPR)
862 tree cond_expr = gimple_assign_rhs1 (stmt);
864 if (i == 0)
865 first_cond_code = TREE_CODE (cond_expr);
866 else if (first_cond_code != TREE_CODE (cond_expr))
868 if (vect_print_dump_info (REPORT_SLP))
870 fprintf (vect_dump, "Build SLP failed: different"
871 " operation");
872 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
875 vect_free_oprnd_info (&oprnds_info);
876 return false;
880 /* Find the def-stmts. */
881 if (!vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo, *node, stmt,
882 ncopies_for_cost, (i == 0),
883 &oprnds_info, prologue_cost_vec,
884 body_cost_vec))
886 vect_free_oprnd_info (&oprnds_info);
887 return false;
892 /* Grouped loads were reached - stop the recursion. */
893 if (stop_recursion)
895 VEC_safe_push (slp_tree, heap, *loads, *node);
896 if (permutation)
898 gimple first_stmt = VEC_index (gimple, stmts, 0);
899 *loads_permuted = true;
900 (void) record_stmt_cost (body_cost_vec, group_size, vec_perm,
901 vinfo_for_stmt (first_stmt), 0, vect_body);
903 else
905 /* We don't check here complex numbers chains, so we set
906 LOADS_PERMUTED for further check in
907 vect_supported_load_permutation_p. */
908 if (rhs_code == REALPART_EXPR || rhs_code == IMAGPART_EXPR)
909 *loads_permuted = true;
912 vect_free_oprnd_info (&oprnds_info);
913 return true;
916 /* Create SLP_TREE nodes for the definition node/s. */
917 FOR_EACH_VEC_ELT (slp_oprnd_info, oprnds_info, i, oprnd_info)
919 slp_tree child;
921 if (oprnd_info->first_dt != vect_internal_def)
922 continue;
924 child = vect_create_new_slp_node (oprnd_info->def_stmts);
925 if (!child
926 || !vect_build_slp_tree (loop_vinfo, bb_vinfo, &child, group_size,
927 outside_cost, ncopies_for_cost,
928 max_nunits, load_permutation, loads,
929 vectorization_factor, loads_permuted,
930 prologue_cost_vec, body_cost_vec))
932 if (child)
933 oprnd_info->def_stmts = NULL;
934 vect_free_slp_tree (child);
935 vect_free_oprnd_info (&oprnds_info);
936 return false;
939 oprnd_info->def_stmts = NULL;
940 VEC_quick_push (slp_void_p, SLP_TREE_CHILDREN (*node), child);
943 vect_free_oprnd_info (&oprnds_info);
944 return true;
948 static void
949 vect_print_slp_tree (slp_tree node)
951 int i;
952 gimple stmt;
953 slp_void_p child;
955 if (!node)
956 return;
958 fprintf (vect_dump, "node ");
959 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
961 fprintf (vect_dump, "\n\tstmt %d ", i);
962 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
964 fprintf (vect_dump, "\n");
966 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
967 vect_print_slp_tree ((slp_tree) child);
971 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
972 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
973 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
974 stmts in NODE are to be marked. */
976 static void
977 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
979 int i;
980 gimple stmt;
981 slp_void_p child;
983 if (!node)
984 return;
986 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
987 if (j < 0 || i == j)
988 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
990 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
991 vect_mark_slp_stmts ((slp_tree) child, mark, j);
995 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
997 static void
998 vect_mark_slp_stmts_relevant (slp_tree node)
1000 int i;
1001 gimple stmt;
1002 stmt_vec_info stmt_info;
1003 slp_void_p child;
1005 if (!node)
1006 return;
1008 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
1010 stmt_info = vinfo_for_stmt (stmt);
1011 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1012 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1013 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1016 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
1017 vect_mark_slp_stmts_relevant ((slp_tree) child);
1021 /* Check if the permutation required by the SLP INSTANCE is supported.
1022 Reorganize the SLP nodes stored in SLP_INSTANCE_LOADS if needed. */
1024 static bool
1025 vect_supported_slp_permutation_p (slp_instance instance)
1027 slp_tree node = VEC_index (slp_tree, SLP_INSTANCE_LOADS (instance), 0);
1028 gimple stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
1029 gimple first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
1030 VEC (slp_tree, heap) *sorted_loads = NULL;
1031 int index;
1032 slp_tree *tmp_loads = NULL;
1033 int group_size = SLP_INSTANCE_GROUP_SIZE (instance), i, j;
1034 slp_tree load;
1036 /* FORNOW: The only supported loads permutation is loads from the same
1037 location in all the loads in the node, when the data-refs in
1038 nodes of LOADS constitute an interleaving chain.
1039 Sort the nodes according to the order of accesses in the chain. */
1040 tmp_loads = (slp_tree *) xmalloc (sizeof (slp_tree) * group_size);
1041 for (i = 0, j = 0;
1042 VEC_iterate (int, SLP_INSTANCE_LOAD_PERMUTATION (instance), i, index)
1043 && VEC_iterate (slp_tree, SLP_INSTANCE_LOADS (instance), j, load);
1044 i += group_size, j++)
1046 gimple scalar_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (load), 0);
1047 /* Check that the loads are all in the same interleaving chain. */
1048 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (scalar_stmt)) != first_load)
1050 if (vect_print_dump_info (REPORT_DETAILS))
1052 fprintf (vect_dump, "Build SLP failed: unsupported data "
1053 "permutation ");
1054 print_gimple_stmt (vect_dump, scalar_stmt, 0, TDF_SLIM);
1057 free (tmp_loads);
1058 return false;
1061 tmp_loads[index] = load;
1064 sorted_loads = VEC_alloc (slp_tree, heap, group_size);
1065 for (i = 0; i < group_size; i++)
1066 VEC_safe_push (slp_tree, heap, sorted_loads, tmp_loads[i]);
1068 VEC_free (slp_tree, heap, SLP_INSTANCE_LOADS (instance));
1069 SLP_INSTANCE_LOADS (instance) = sorted_loads;
1070 free (tmp_loads);
1072 if (!vect_transform_slp_perm_load (stmt, NULL, NULL,
1073 SLP_INSTANCE_UNROLLING_FACTOR (instance),
1074 instance, true))
1075 return false;
1077 return true;
1081 /* Rearrange the statements of NODE according to PERMUTATION. */
1083 static void
1084 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1085 VEC (int, heap) *permutation)
1087 gimple stmt;
1088 VEC (gimple, heap) *tmp_stmts;
1089 unsigned int index, i;
1090 slp_void_p child;
1092 if (!node)
1093 return;
1095 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
1096 vect_slp_rearrange_stmts ((slp_tree) child, group_size, permutation);
1098 gcc_assert (group_size == VEC_length (gimple, SLP_TREE_SCALAR_STMTS (node)));
1099 tmp_stmts = VEC_alloc (gimple, heap, group_size);
1101 for (i = 0; i < group_size; i++)
1102 VEC_safe_push (gimple, heap, tmp_stmts, NULL);
1104 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
1106 index = VEC_index (int, permutation, i);
1107 VEC_replace (gimple, tmp_stmts, index, stmt);
1110 VEC_free (gimple, heap, SLP_TREE_SCALAR_STMTS (node));
1111 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1115 /* Check if the required load permutation is supported.
1116 LOAD_PERMUTATION contains a list of indices of the loads.
1117 In SLP this permutation is relative to the order of grouped stores that are
1118 the base of the SLP instance. */
1120 static bool
1121 vect_supported_load_permutation_p (slp_instance slp_instn, int group_size,
1122 VEC (int, heap) *load_permutation)
1124 int i = 0, j, prev = -1, next, k, number_of_groups;
1125 bool supported, bad_permutation = false;
1126 sbitmap load_index;
1127 slp_tree node, other_complex_node;
1128 gimple stmt, first = NULL, other_node_first, load, next_load, first_load;
1129 unsigned complex_numbers = 0;
1130 struct data_reference *dr;
1131 bb_vec_info bb_vinfo;
1133 /* FORNOW: permutations are only supported in SLP. */
1134 if (!slp_instn)
1135 return false;
1137 if (vect_print_dump_info (REPORT_SLP))
1139 fprintf (vect_dump, "Load permutation ");
1140 FOR_EACH_VEC_ELT (int, load_permutation, i, next)
1141 fprintf (vect_dump, "%d ", next);
1144 /* In case of reduction every load permutation is allowed, since the order
1145 of the reduction statements is not important (as opposed to the case of
1146 grouped stores). The only condition we need to check is that all the
1147 load nodes are of the same size and have the same permutation (and then
1148 rearrange all the nodes of the SLP instance according to this
1149 permutation). */
1151 /* Check that all the load nodes are of the same size. */
1152 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (slp_instn), i, node)
1154 if (VEC_length (gimple, SLP_TREE_SCALAR_STMTS (node))
1155 != (unsigned) group_size)
1156 return false;
1158 stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
1159 if (is_gimple_assign (stmt)
1160 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1161 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR))
1162 complex_numbers++;
1165 /* Complex operands can be swapped as following:
1166 real_c = real_b + real_a;
1167 imag_c = imag_a + imag_b;
1168 i.e., we have {real_b, imag_a} and {real_a, imag_b} instead of
1169 {real_a, imag_a} and {real_b, imag_b}. We check here that if interleaving
1170 chains are mixed, they match the above pattern. */
1171 if (complex_numbers)
1173 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (slp_instn), i, node)
1175 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), j, stmt)
1177 if (j == 0)
1178 first = stmt;
1179 else
1181 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != first)
1183 if (complex_numbers != 2)
1184 return false;
1186 if (i == 0)
1187 k = 1;
1188 else
1189 k = 0;
1191 other_complex_node = VEC_index (slp_tree,
1192 SLP_INSTANCE_LOADS (slp_instn), k);
1193 other_node_first = VEC_index (gimple,
1194 SLP_TREE_SCALAR_STMTS (other_complex_node), 0);
1196 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
1197 != other_node_first)
1198 return false;
1205 /* We checked that this case ok, so there is no need to proceed with
1206 permutation tests. */
1207 if (complex_numbers == 2
1208 && VEC_length (slp_tree, SLP_INSTANCE_LOADS (slp_instn)) == 2)
1210 VEC_free (slp_tree, heap, SLP_INSTANCE_LOADS (slp_instn));
1211 VEC_free (int, heap, SLP_INSTANCE_LOAD_PERMUTATION (slp_instn));
1212 return true;
1215 node = SLP_INSTANCE_TREE (slp_instn);
1216 stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
1217 /* LOAD_PERMUTATION is a list of indices of all the loads of the SLP
1218 instance, not all the loads belong to the same node or interleaving
1219 group. Hence, we need to divide them into groups according to
1220 GROUP_SIZE. */
1221 number_of_groups = VEC_length (int, load_permutation) / group_size;
1223 /* Reduction (there are no data-refs in the root).
1224 In reduction chain the order of the loads is important. */
1225 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1226 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1228 int first_group_load_index;
1230 /* Compare all the permutation sequences to the first one. */
1231 for (i = 1; i < number_of_groups; i++)
1233 k = 0;
1234 for (j = i * group_size; j < i * group_size + group_size; j++)
1236 next = VEC_index (int, load_permutation, j);
1237 first_group_load_index = VEC_index (int, load_permutation, k);
1239 if (next != first_group_load_index)
1241 bad_permutation = true;
1242 break;
1245 k++;
1248 if (bad_permutation)
1249 break;
1252 if (!bad_permutation)
1254 /* Check that the loads in the first sequence are different and there
1255 are no gaps between them. */
1256 load_index = sbitmap_alloc (group_size);
1257 sbitmap_zero (load_index);
1258 for (k = 0; k < group_size; k++)
1260 first_group_load_index = VEC_index (int, load_permutation, k);
1261 if (TEST_BIT (load_index, first_group_load_index))
1263 bad_permutation = true;
1264 break;
1267 SET_BIT (load_index, first_group_load_index);
1270 if (!bad_permutation)
1271 for (k = 0; k < group_size; k++)
1272 if (!TEST_BIT (load_index, k))
1274 bad_permutation = true;
1275 break;
1278 sbitmap_free (load_index);
1281 if (!bad_permutation)
1283 /* This permutation is valid for reduction. Since the order of the
1284 statements in the nodes is not important unless they are memory
1285 accesses, we can rearrange the statements in all the nodes
1286 according to the order of the loads. */
1287 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1288 load_permutation);
1289 VEC_free (int, heap, SLP_INSTANCE_LOAD_PERMUTATION (slp_instn));
1290 return true;
1294 /* In basic block vectorization we allow any subchain of an interleaving
1295 chain.
1296 FORNOW: not supported in loop SLP because of realignment compications. */
1297 bb_vinfo = STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt));
1298 bad_permutation = false;
1299 /* Check that for every node in the instance the loads form a subchain. */
1300 if (bb_vinfo)
1302 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (slp_instn), i, node)
1304 next_load = NULL;
1305 first_load = NULL;
1306 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), j, load)
1308 if (!first_load)
1309 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (load));
1310 else if (first_load
1311 != GROUP_FIRST_ELEMENT (vinfo_for_stmt (load)))
1313 bad_permutation = true;
1314 break;
1317 if (j != 0 && next_load != load)
1319 bad_permutation = true;
1320 break;
1323 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1326 if (bad_permutation)
1327 break;
1330 /* Check that the alignment of the first load in every subchain, i.e.,
1331 the first statement in every load node, is supported. */
1332 if (!bad_permutation)
1334 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (slp_instn), i, node)
1336 first_load = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
1337 if (first_load
1338 != GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_load)))
1340 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_load));
1341 if (vect_supportable_dr_alignment (dr, false)
1342 == dr_unaligned_unsupported)
1344 if (vect_print_dump_info (REPORT_SLP))
1346 fprintf (vect_dump, "unsupported unaligned load ");
1347 print_gimple_stmt (vect_dump, first_load, 0,
1348 TDF_SLIM);
1350 bad_permutation = true;
1351 break;
1356 if (!bad_permutation)
1358 VEC_free (int, heap, SLP_INSTANCE_LOAD_PERMUTATION (slp_instn));
1359 return true;
1364 /* FORNOW: the only supported permutation is 0..01..1.. of length equal to
1365 GROUP_SIZE and where each sequence of same drs is of GROUP_SIZE length as
1366 well (unless it's reduction). */
1367 if (VEC_length (int, load_permutation)
1368 != (unsigned int) (group_size * group_size))
1369 return false;
1371 supported = true;
1372 load_index = sbitmap_alloc (group_size);
1373 sbitmap_zero (load_index);
1374 for (j = 0; j < group_size; j++)
1376 for (i = j * group_size, k = 0;
1377 VEC_iterate (int, load_permutation, i, next) && k < group_size;
1378 i++, k++)
1380 if (i != j * group_size && next != prev)
1382 supported = false;
1383 break;
1386 prev = next;
1389 if (TEST_BIT (load_index, prev))
1391 supported = false;
1392 break;
1395 SET_BIT (load_index, prev);
1398 for (j = 0; j < group_size; j++)
1399 if (!TEST_BIT (load_index, j))
1400 return false;
1402 sbitmap_free (load_index);
1404 if (supported && i == group_size * group_size
1405 && vect_supported_slp_permutation_p (slp_instn))
1406 return true;
1408 return false;
1412 /* Find the first load in the loop that belongs to INSTANCE.
1413 When loads are in several SLP nodes, there can be a case in which the first
1414 load does not appear in the first SLP node to be transformed, causing
1415 incorrect order of statements. Since we generate all the loads together,
1416 they must be inserted before the first load of the SLP instance and not
1417 before the first load of the first node of the instance. */
1419 static gimple
1420 vect_find_first_load_in_slp_instance (slp_instance instance)
1422 int i, j;
1423 slp_tree load_node;
1424 gimple first_load = NULL, load;
1426 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (instance), i, load_node)
1427 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (load_node), j, load)
1428 first_load = get_earlier_stmt (load, first_load);
1430 return first_load;
1434 /* Find the last store in SLP INSTANCE. */
1436 static gimple
1437 vect_find_last_store_in_slp_instance (slp_instance instance)
1439 int i;
1440 slp_tree node;
1441 gimple last_store = NULL, store;
1443 node = SLP_INSTANCE_TREE (instance);
1444 for (i = 0;
1445 VEC_iterate (gimple, SLP_TREE_SCALAR_STMTS (node), i, store);
1446 i++)
1447 last_store = get_later_stmt (store, last_store);
1449 return last_store;
1453 /* Analyze an SLP instance starting from a group of grouped stores. Call
1454 vect_build_slp_tree to build a tree of packed stmts if possible.
1455 Return FALSE if it's impossible to SLP any stmt in the loop. */
1457 static bool
1458 vect_analyze_slp_instance (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1459 gimple stmt)
1461 slp_instance new_instance;
1462 slp_tree node;
1463 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1464 unsigned int unrolling_factor = 1, nunits;
1465 tree vectype, scalar_type = NULL_TREE;
1466 gimple next;
1467 unsigned int vectorization_factor = 0;
1468 int outside_cost = 0, ncopies_for_cost, i;
1469 unsigned int max_nunits = 0;
1470 VEC (int, heap) *load_permutation;
1471 VEC (slp_tree, heap) *loads;
1472 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1473 bool loads_permuted = false;
1474 VEC (gimple, heap) *scalar_stmts;
1475 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1476 stmt_info_for_cost *si;
1478 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1480 if (dr)
1482 scalar_type = TREE_TYPE (DR_REF (dr));
1483 vectype = get_vectype_for_scalar_type (scalar_type);
1485 else
1487 gcc_assert (loop_vinfo);
1488 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1491 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1493 else
1495 gcc_assert (loop_vinfo);
1496 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1497 group_size = VEC_length (gimple, LOOP_VINFO_REDUCTIONS (loop_vinfo));
1500 if (!vectype)
1502 if (vect_print_dump_info (REPORT_SLP))
1504 fprintf (vect_dump, "Build SLP failed: unsupported data-type ");
1505 print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
1508 return false;
1511 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1512 if (loop_vinfo)
1513 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1514 else
1515 vectorization_factor = nunits;
1517 /* Calculate the unrolling factor. */
1518 unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
1519 if (unrolling_factor != 1 && !loop_vinfo)
1521 if (vect_print_dump_info (REPORT_SLP))
1522 fprintf (vect_dump, "Build SLP failed: unrolling required in basic"
1523 " block SLP");
1525 return false;
1528 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1529 scalar_stmts = VEC_alloc (gimple, heap, group_size);
1530 next = stmt;
1531 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1533 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1534 while (next)
1536 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1537 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1538 VEC_safe_push (gimple, heap, scalar_stmts,
1539 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1540 else
1541 VEC_safe_push (gimple, heap, scalar_stmts, next);
1542 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1545 else
1547 /* Collect reduction statements. */
1548 VEC (gimple, heap) *reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1549 for (i = 0; VEC_iterate (gimple, reductions, i, next); i++)
1550 VEC_safe_push (gimple, heap, scalar_stmts, next);
1553 node = vect_create_new_slp_node (scalar_stmts);
1555 /* Calculate the number of vector stmts to create based on the unrolling
1556 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1557 GROUP_SIZE / NUNITS otherwise. */
1558 ncopies_for_cost = unrolling_factor * group_size / nunits;
1560 load_permutation = VEC_alloc (int, heap, group_size * group_size);
1561 loads = VEC_alloc (slp_tree, heap, group_size);
1562 prologue_cost_vec = VEC_alloc (stmt_info_for_cost, heap, 10);
1563 body_cost_vec = VEC_alloc (stmt_info_for_cost, heap, 10);
1565 /* Build the tree for the SLP instance. */
1566 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &node, group_size,
1567 &outside_cost, ncopies_for_cost,
1568 &max_nunits, &load_permutation, &loads,
1569 vectorization_factor, &loads_permuted,
1570 &prologue_cost_vec, &body_cost_vec))
1572 void *data = (loop_vinfo ? LOOP_VINFO_TARGET_COST_DATA (loop_vinfo)
1573 : BB_VINFO_TARGET_COST_DATA (bb_vinfo));
1575 /* Calculate the unrolling factor based on the smallest type. */
1576 if (max_nunits > nunits)
1577 unrolling_factor = least_common_multiple (max_nunits, group_size)
1578 / group_size;
1580 if (unrolling_factor != 1 && !loop_vinfo)
1582 if (vect_print_dump_info (REPORT_SLP))
1583 fprintf (vect_dump, "Build SLP failed: unrolling required in basic"
1584 " block SLP");
1585 vect_free_slp_tree (node);
1586 VEC_free (stmt_info_for_cost, heap, body_cost_vec);
1587 VEC_free (stmt_info_for_cost, heap, prologue_cost_vec);
1588 VEC_free (int, heap, load_permutation);
1589 VEC_free (slp_tree, heap, loads);
1590 return false;
1593 /* Create a new SLP instance. */
1594 new_instance = XNEW (struct _slp_instance);
1595 SLP_INSTANCE_TREE (new_instance) = node;
1596 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1597 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1598 SLP_INSTANCE_BODY_COST_VEC (new_instance) = body_cost_vec;
1599 SLP_INSTANCE_LOADS (new_instance) = loads;
1600 SLP_INSTANCE_FIRST_LOAD_STMT (new_instance) = NULL;
1601 SLP_INSTANCE_LOAD_PERMUTATION (new_instance) = load_permutation;
1603 if (loads_permuted)
1605 if (!vect_supported_load_permutation_p (new_instance, group_size,
1606 load_permutation))
1608 if (vect_print_dump_info (REPORT_SLP))
1610 fprintf (vect_dump, "Build SLP failed: unsupported load "
1611 "permutation ");
1612 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
1615 vect_free_slp_instance (new_instance);
1616 VEC_free (stmt_info_for_cost, heap, prologue_cost_vec);
1617 return false;
1620 SLP_INSTANCE_FIRST_LOAD_STMT (new_instance)
1621 = vect_find_first_load_in_slp_instance (new_instance);
1623 else
1624 VEC_free (int, heap, SLP_INSTANCE_LOAD_PERMUTATION (new_instance));
1626 /* Record the prologue costs, which were delayed until we were
1627 sure that SLP was successful. Unlike the body costs, we know
1628 the final values now regardless of the loop vectorization factor. */
1629 FOR_EACH_VEC_ELT (stmt_info_for_cost, prologue_cost_vec, i, si)
1631 struct _stmt_vec_info *stmt_info
1632 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1633 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1634 si->misalign, vect_prologue);
1637 VEC_free (stmt_info_for_cost, heap, prologue_cost_vec);
1639 if (loop_vinfo)
1640 VEC_safe_push (slp_instance, heap,
1641 LOOP_VINFO_SLP_INSTANCES (loop_vinfo),
1642 new_instance);
1643 else
1644 VEC_safe_push (slp_instance, heap, BB_VINFO_SLP_INSTANCES (bb_vinfo),
1645 new_instance);
1647 if (vect_print_dump_info (REPORT_SLP))
1648 vect_print_slp_tree (node);
1650 return true;
1652 else
1654 VEC_free (stmt_info_for_cost, heap, body_cost_vec);
1655 VEC_free (stmt_info_for_cost, heap, prologue_cost_vec);
1658 /* Failed to SLP. */
1659 /* Free the allocated memory. */
1660 vect_free_slp_tree (node);
1661 VEC_free (int, heap, load_permutation);
1662 VEC_free (slp_tree, heap, loads);
1664 return false;
1668 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
1669 trees of packed scalar stmts if SLP is possible. */
1671 bool
1672 vect_analyze_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
1674 unsigned int i;
1675 VEC (gimple, heap) *grouped_stores, *reductions = NULL, *reduc_chains = NULL;
1676 gimple first_element;
1677 bool ok = false;
1679 if (vect_print_dump_info (REPORT_SLP))
1680 fprintf (vect_dump, "=== vect_analyze_slp ===");
1682 if (loop_vinfo)
1684 grouped_stores = LOOP_VINFO_GROUPED_STORES (loop_vinfo);
1685 reduc_chains = LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo);
1686 reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1688 else
1689 grouped_stores = BB_VINFO_GROUPED_STORES (bb_vinfo);
1691 /* Find SLP sequences starting from groups of grouped stores. */
1692 FOR_EACH_VEC_ELT (gimple, grouped_stores, i, first_element)
1693 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element))
1694 ok = true;
1696 if (bb_vinfo && !ok)
1698 if (vect_print_dump_info (REPORT_SLP))
1699 fprintf (vect_dump, "Failed to SLP the basic block.");
1701 return false;
1704 if (loop_vinfo
1705 && VEC_length (gimple, LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo)) > 0)
1707 /* Find SLP sequences starting from reduction chains. */
1708 FOR_EACH_VEC_ELT (gimple, reduc_chains, i, first_element)
1709 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element))
1710 ok = true;
1711 else
1712 return false;
1714 /* Don't try to vectorize SLP reductions if reduction chain was
1715 detected. */
1716 return ok;
1719 /* Find SLP sequences starting from groups of reductions. */
1720 if (loop_vinfo && VEC_length (gimple, LOOP_VINFO_REDUCTIONS (loop_vinfo)) > 1
1721 && vect_analyze_slp_instance (loop_vinfo, bb_vinfo,
1722 VEC_index (gimple, reductions, 0)))
1723 ok = true;
1725 return true;
1729 /* For each possible SLP instance decide whether to SLP it and calculate overall
1730 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
1731 least one instance. */
1733 bool
1734 vect_make_slp_decision (loop_vec_info loop_vinfo)
1736 unsigned int i, unrolling_factor = 1;
1737 VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1738 slp_instance instance;
1739 int decided_to_slp = 0;
1741 if (vect_print_dump_info (REPORT_SLP))
1742 fprintf (vect_dump, "=== vect_make_slp_decision ===");
1744 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
1746 /* FORNOW: SLP if you can. */
1747 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
1748 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
1750 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
1751 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
1752 loop-based vectorization. Such stmts will be marked as HYBRID. */
1753 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
1754 decided_to_slp++;
1757 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
1759 if (decided_to_slp && vect_print_dump_info (REPORT_SLP))
1760 fprintf (vect_dump, "Decided to SLP %d instances. Unrolling factor %d",
1761 decided_to_slp, unrolling_factor);
1763 return (decided_to_slp > 0);
1767 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
1768 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
1770 static void
1771 vect_detect_hybrid_slp_stmts (slp_tree node)
1773 int i;
1774 VEC (gimple, heap) *stmts = SLP_TREE_SCALAR_STMTS (node);
1775 gimple stmt = VEC_index (gimple, stmts, 0);
1776 imm_use_iterator imm_iter;
1777 gimple use_stmt;
1778 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1779 slp_void_p child;
1780 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1781 struct loop *loop = NULL;
1782 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1783 basic_block bb = NULL;
1785 if (!node)
1786 return;
1788 if (loop_vinfo)
1789 loop = LOOP_VINFO_LOOP (loop_vinfo);
1790 else
1791 bb = BB_VINFO_BB (bb_vinfo);
1793 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
1794 if (PURE_SLP_STMT (vinfo_for_stmt (stmt))
1795 && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
1796 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
1797 if (gimple_bb (use_stmt)
1798 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
1799 || bb == gimple_bb (use_stmt))
1800 && (stmt_vinfo = vinfo_for_stmt (use_stmt))
1801 && !STMT_SLP_TYPE (stmt_vinfo)
1802 && (STMT_VINFO_RELEVANT (stmt_vinfo)
1803 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_vinfo)))
1804 && !(gimple_code (use_stmt) == GIMPLE_PHI
1805 && STMT_VINFO_DEF_TYPE (stmt_vinfo)
1806 == vect_reduction_def))
1807 vect_mark_slp_stmts (node, hybrid, i);
1809 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
1810 vect_detect_hybrid_slp_stmts ((slp_tree) child);
1814 /* Find stmts that must be both vectorized and SLPed. */
1816 void
1817 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
1819 unsigned int i;
1820 VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1821 slp_instance instance;
1823 if (vect_print_dump_info (REPORT_SLP))
1824 fprintf (vect_dump, "=== vect_detect_hybrid_slp ===");
1826 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
1827 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance));
1831 /* Create and initialize a new bb_vec_info struct for BB, as well as
1832 stmt_vec_info structs for all the stmts in it. */
1834 static bb_vec_info
1835 new_bb_vec_info (basic_block bb)
1837 bb_vec_info res = NULL;
1838 gimple_stmt_iterator gsi;
1840 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
1841 BB_VINFO_BB (res) = bb;
1843 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1845 gimple stmt = gsi_stmt (gsi);
1846 gimple_set_uid (stmt, 0);
1847 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, NULL, res));
1850 BB_VINFO_GROUPED_STORES (res) = VEC_alloc (gimple, heap, 10);
1851 BB_VINFO_SLP_INSTANCES (res) = VEC_alloc (slp_instance, heap, 2);
1852 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
1854 bb->aux = res;
1855 return res;
1859 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
1860 stmts in the basic block. */
1862 static void
1863 destroy_bb_vec_info (bb_vec_info bb_vinfo)
1865 VEC (slp_instance, heap) *slp_instances;
1866 slp_instance instance;
1867 basic_block bb;
1868 gimple_stmt_iterator si;
1869 unsigned i;
1871 if (!bb_vinfo)
1872 return;
1874 bb = BB_VINFO_BB (bb_vinfo);
1876 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1878 gimple stmt = gsi_stmt (si);
1879 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1881 if (stmt_info)
1882 /* Free stmt_vec_info. */
1883 free_stmt_vec_info (stmt);
1886 free_data_refs (BB_VINFO_DATAREFS (bb_vinfo));
1887 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
1888 VEC_free (gimple, heap, BB_VINFO_GROUPED_STORES (bb_vinfo));
1889 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
1890 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
1891 vect_free_slp_instance (instance);
1892 VEC_free (slp_instance, heap, BB_VINFO_SLP_INSTANCES (bb_vinfo));
1893 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
1894 free (bb_vinfo);
1895 bb->aux = NULL;
1899 /* Analyze statements contained in SLP tree node after recursively analyzing
1900 the subtree. Return TRUE if the operations are supported. */
1902 static bool
1903 vect_slp_analyze_node_operations (bb_vec_info bb_vinfo, slp_tree node)
1905 bool dummy;
1906 int i;
1907 gimple stmt;
1908 slp_void_p child;
1910 if (!node)
1911 return true;
1913 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
1914 if (!vect_slp_analyze_node_operations (bb_vinfo, (slp_tree) child))
1915 return false;
1917 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
1919 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1920 gcc_assert (stmt_info);
1921 gcc_assert (PURE_SLP_STMT (stmt_info));
1923 if (!vect_analyze_stmt (stmt, &dummy, node))
1924 return false;
1927 return true;
1931 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
1932 operations are supported. */
1934 static bool
1935 vect_slp_analyze_operations (bb_vec_info bb_vinfo)
1937 VEC (slp_instance, heap) *slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
1938 slp_instance instance;
1939 int i;
1941 for (i = 0; VEC_iterate (slp_instance, slp_instances, i, instance); )
1943 if (!vect_slp_analyze_node_operations (bb_vinfo,
1944 SLP_INSTANCE_TREE (instance)))
1946 vect_free_slp_instance (instance);
1947 VEC_ordered_remove (slp_instance, slp_instances, i);
1949 else
1950 i++;
1953 if (!VEC_length (slp_instance, slp_instances))
1954 return false;
1956 return true;
1959 /* Check if vectorization of the basic block is profitable. */
1961 static bool
1962 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
1964 VEC (slp_instance, heap) *slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
1965 slp_instance instance;
1966 int i, j;
1967 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
1968 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
1969 unsigned int stmt_cost;
1970 gimple stmt;
1971 gimple_stmt_iterator si;
1972 basic_block bb = BB_VINFO_BB (bb_vinfo);
1973 void *target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
1974 stmt_vec_info stmt_info = NULL;
1975 stmt_vector_for_cost body_cost_vec;
1976 stmt_info_for_cost *ci;
1978 /* Calculate vector costs. */
1979 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
1981 body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
1983 FOR_EACH_VEC_ELT (stmt_info_for_cost, body_cost_vec, j, ci)
1985 stmt_info = ci->stmt ? vinfo_for_stmt (ci->stmt) : NULL;
1986 (void) add_stmt_cost (target_cost_data, ci->count, ci->kind,
1987 stmt_info, ci->misalign, vect_body);
1991 /* Calculate scalar cost. */
1992 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1994 stmt = gsi_stmt (si);
1995 stmt_info = vinfo_for_stmt (stmt);
1997 if (!stmt_info || !STMT_VINFO_VECTORIZABLE (stmt_info)
1998 || !PURE_SLP_STMT (stmt_info))
1999 continue;
2001 if (STMT_VINFO_DATA_REF (stmt_info))
2003 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2004 stmt_cost = vect_get_stmt_cost (scalar_load);
2005 else
2006 stmt_cost = vect_get_stmt_cost (scalar_store);
2008 else
2009 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2011 scalar_cost += stmt_cost;
2014 /* Complete the target-specific cost calculation. */
2015 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2016 &vec_inside_cost, &vec_epilogue_cost);
2018 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2020 if (vect_print_dump_info (REPORT_COST))
2022 fprintf (vect_dump, "Cost model analysis: \n");
2023 fprintf (vect_dump, " Vector inside of basic block cost: %d\n",
2024 vec_inside_cost);
2025 fprintf (vect_dump, " Vector prologue cost: %d\n", vec_prologue_cost);
2026 fprintf (vect_dump, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2027 fprintf (vect_dump, " Scalar cost of basic block: %d", scalar_cost);
2030 /* Vectorization is profitable if its cost is less than the cost of scalar
2031 version. */
2032 if (vec_outside_cost + vec_inside_cost >= scalar_cost)
2033 return false;
2035 return true;
2038 /* Check if the basic block can be vectorized. */
2040 static bb_vec_info
2041 vect_slp_analyze_bb_1 (basic_block bb)
2043 bb_vec_info bb_vinfo;
2044 VEC (ddr_p, heap) *ddrs;
2045 VEC (slp_instance, heap) *slp_instances;
2046 slp_instance instance;
2047 int i;
2048 int min_vf = 2;
2049 int max_vf = MAX_VECTORIZATION_FACTOR;
2051 bb_vinfo = new_bb_vec_info (bb);
2052 if (!bb_vinfo)
2053 return NULL;
2055 if (!vect_analyze_data_refs (NULL, bb_vinfo, &min_vf))
2057 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2058 fprintf (vect_dump, "not vectorized: unhandled data-ref in basic "
2059 "block.\n");
2061 destroy_bb_vec_info (bb_vinfo);
2062 return NULL;
2065 ddrs = BB_VINFO_DDRS (bb_vinfo);
2066 if (!VEC_length (ddr_p, ddrs))
2068 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2069 fprintf (vect_dump, "not vectorized: not enough data-refs in basic "
2070 "block.\n");
2072 destroy_bb_vec_info (bb_vinfo);
2073 return NULL;
2076 vect_pattern_recog (NULL, bb_vinfo);
2078 if (!vect_analyze_data_ref_dependences (NULL, bb_vinfo, &max_vf)
2079 || min_vf > max_vf)
2081 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2082 fprintf (vect_dump, "not vectorized: unhandled data dependence "
2083 "in basic block.\n");
2085 destroy_bb_vec_info (bb_vinfo);
2086 return NULL;
2089 if (!vect_analyze_data_refs_alignment (NULL, bb_vinfo))
2091 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2092 fprintf (vect_dump, "not vectorized: bad data alignment in basic "
2093 "block.\n");
2095 destroy_bb_vec_info (bb_vinfo);
2096 return NULL;
2099 if (!vect_analyze_data_ref_accesses (NULL, bb_vinfo))
2101 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2102 fprintf (vect_dump, "not vectorized: unhandled data access in basic "
2103 "block.\n");
2105 destroy_bb_vec_info (bb_vinfo);
2106 return NULL;
2109 /* Check the SLP opportunities in the basic block, analyze and build SLP
2110 trees. */
2111 if (!vect_analyze_slp (NULL, bb_vinfo))
2113 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2114 fprintf (vect_dump, "not vectorized: failed to find SLP opportunities "
2115 "in basic block.\n");
2117 destroy_bb_vec_info (bb_vinfo);
2118 return NULL;
2121 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2123 /* Mark all the statements that we want to vectorize as pure SLP and
2124 relevant. */
2125 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
2127 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2128 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2131 if (!vect_verify_datarefs_alignment (NULL, bb_vinfo))
2133 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2134 fprintf (vect_dump, "not vectorized: unsupported alignment in basic "
2135 "block.\n");
2137 destroy_bb_vec_info (bb_vinfo);
2138 return NULL;
2141 if (!vect_slp_analyze_operations (bb_vinfo))
2143 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2144 fprintf (vect_dump, "not vectorized: bad operation in basic block.\n");
2146 destroy_bb_vec_info (bb_vinfo);
2147 return NULL;
2150 /* Cost model: check if the vectorization is worthwhile. */
2151 if (flag_vect_cost_model
2152 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2154 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2155 fprintf (vect_dump, "not vectorized: vectorization is not "
2156 "profitable.\n");
2158 destroy_bb_vec_info (bb_vinfo);
2159 return NULL;
2162 if (vect_print_dump_info (REPORT_DETAILS))
2163 fprintf (vect_dump, "Basic block will be vectorized using SLP\n");
2165 return bb_vinfo;
2169 bb_vec_info
2170 vect_slp_analyze_bb (basic_block bb)
2172 bb_vec_info bb_vinfo;
2173 int insns = 0;
2174 gimple_stmt_iterator gsi;
2175 unsigned int vector_sizes;
2177 if (vect_print_dump_info (REPORT_DETAILS))
2178 fprintf (vect_dump, "===vect_slp_analyze_bb===\n");
2180 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2182 gimple stmt = gsi_stmt (gsi);
2183 if (!is_gimple_debug (stmt)
2184 && !gimple_nop_p (stmt)
2185 && gimple_code (stmt) != GIMPLE_LABEL)
2186 insns++;
2189 if (insns > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2191 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
2192 fprintf (vect_dump, "not vectorized: too many instructions in basic "
2193 "block.\n");
2195 return NULL;
2198 /* Autodetect first vector size we try. */
2199 current_vector_size = 0;
2200 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2202 while (1)
2204 bb_vinfo = vect_slp_analyze_bb_1 (bb);
2205 if (bb_vinfo)
2206 return bb_vinfo;
2208 destroy_bb_vec_info (bb_vinfo);
2210 vector_sizes &= ~current_vector_size;
2211 if (vector_sizes == 0
2212 || current_vector_size == 0)
2213 return NULL;
2215 /* Try the next biggest vector size. */
2216 current_vector_size = 1 << floor_log2 (vector_sizes);
2217 if (vect_print_dump_info (REPORT_DETAILS))
2218 fprintf (vect_dump, "***** Re-trying analysis with "
2219 "vector size %d\n", current_vector_size);
2224 /* SLP costs are calculated according to SLP instance unrolling factor (i.e.,
2225 the number of created vector stmts depends on the unrolling factor).
2226 However, the actual number of vector stmts for every SLP node depends on
2227 VF which is set later in vect_analyze_operations (). Hence, SLP costs
2228 should be updated. In this function we assume that the inside costs
2229 calculated in vect_model_xxx_cost are linear in ncopies. */
2231 void
2232 vect_update_slp_costs_according_to_vf (loop_vec_info loop_vinfo)
2234 unsigned int i, j, vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2235 VEC (slp_instance, heap) *slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2236 slp_instance instance;
2237 stmt_vector_for_cost body_cost_vec;
2238 stmt_info_for_cost *si;
2239 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2241 if (vect_print_dump_info (REPORT_SLP))
2242 fprintf (vect_dump, "=== vect_update_slp_costs_according_to_vf ===");
2244 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
2246 /* We assume that costs are linear in ncopies. */
2247 int ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (instance);
2249 /* Record the instance's instructions in the target cost model.
2250 This was delayed until here because the count of instructions
2251 isn't known beforehand. */
2252 body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
2254 FOR_EACH_VEC_ELT (stmt_info_for_cost, body_cost_vec, j, si)
2255 (void) add_stmt_cost (data, si->count * ncopies, si->kind,
2256 vinfo_for_stmt (si->stmt), si->misalign,
2257 vect_body);
2262 /* For constant and loop invariant defs of SLP_NODE this function returns
2263 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2264 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2265 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2266 REDUC_INDEX is the index of the reduction operand in the statements, unless
2267 it is -1. */
2269 static void
2270 vect_get_constant_vectors (tree op, slp_tree slp_node,
2271 VEC (tree, heap) **vec_oprnds,
2272 unsigned int op_num, unsigned int number_of_vectors,
2273 int reduc_index)
2275 VEC (gimple, heap) *stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2276 gimple stmt = VEC_index (gimple, stmts, 0);
2277 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2278 unsigned nunits;
2279 tree vec_cst;
2280 tree *elts;
2281 unsigned j, number_of_places_left_in_vector;
2282 tree vector_type;
2283 tree vop;
2284 int group_size = VEC_length (gimple, stmts);
2285 unsigned int vec_num, i;
2286 unsigned number_of_copies = 1;
2287 VEC (tree, heap) *voprnds = VEC_alloc (tree, heap, number_of_vectors);
2288 bool constant_p, is_store;
2289 tree neutral_op = NULL;
2290 enum tree_code code = gimple_expr_code (stmt);
2291 gimple def_stmt;
2292 struct loop *loop;
2294 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
2295 && reduc_index != -1)
2297 op_num = reduc_index - 1;
2298 op = gimple_op (stmt, reduc_index);
2299 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
2300 we need either neutral operands or the original operands. See
2301 get_initial_def_for_reduction() for details. */
2302 switch (code)
2304 case WIDEN_SUM_EXPR:
2305 case DOT_PROD_EXPR:
2306 case PLUS_EXPR:
2307 case MINUS_EXPR:
2308 case BIT_IOR_EXPR:
2309 case BIT_XOR_EXPR:
2310 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2311 neutral_op = build_real (TREE_TYPE (op), dconst0);
2312 else
2313 neutral_op = build_int_cst (TREE_TYPE (op), 0);
2315 break;
2317 case MULT_EXPR:
2318 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2319 neutral_op = build_real (TREE_TYPE (op), dconst1);
2320 else
2321 neutral_op = build_int_cst (TREE_TYPE (op), 1);
2323 break;
2325 case BIT_AND_EXPR:
2326 neutral_op = build_int_cst (TREE_TYPE (op), -1);
2327 break;
2329 case MAX_EXPR:
2330 case MIN_EXPR:
2331 def_stmt = SSA_NAME_DEF_STMT (op);
2332 loop = (gimple_bb (stmt))->loop_father;
2333 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2334 loop_preheader_edge (loop));
2335 break;
2337 default:
2338 neutral_op = NULL;
2342 if (STMT_VINFO_DATA_REF (stmt_vinfo))
2344 is_store = true;
2345 op = gimple_assign_rhs1 (stmt);
2347 else
2348 is_store = false;
2350 gcc_assert (op);
2352 if (CONSTANT_CLASS_P (op))
2353 constant_p = true;
2354 else
2355 constant_p = false;
2357 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
2358 gcc_assert (vector_type);
2359 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
2361 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
2362 created vectors. It is greater than 1 if unrolling is performed.
2364 For example, we have two scalar operands, s1 and s2 (e.g., group of
2365 strided accesses of size two), while NUNITS is four (i.e., four scalars
2366 of this type can be packed in a vector). The output vector will contain
2367 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
2368 will be 2).
2370 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
2371 containing the operands.
2373 For example, NUNITS is four as before, and the group size is 8
2374 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
2375 {s5, s6, s7, s8}. */
2377 number_of_copies = least_common_multiple (nunits, group_size) / group_size;
2379 number_of_places_left_in_vector = nunits;
2380 elts = XALLOCAVEC (tree, nunits);
2381 for (j = 0; j < number_of_copies; j++)
2383 for (i = group_size - 1; VEC_iterate (gimple, stmts, i, stmt); i--)
2385 if (is_store)
2386 op = gimple_assign_rhs1 (stmt);
2387 else
2389 switch (code)
2391 case COND_EXPR:
2392 if (op_num == 0 || op_num == 1)
2394 tree cond = gimple_assign_rhs1 (stmt);
2395 op = TREE_OPERAND (cond, op_num);
2397 else
2399 if (op_num == 2)
2400 op = gimple_assign_rhs2 (stmt);
2401 else
2402 op = gimple_assign_rhs3 (stmt);
2404 break;
2406 case CALL_EXPR:
2407 op = gimple_call_arg (stmt, op_num);
2408 break;
2410 case LSHIFT_EXPR:
2411 case RSHIFT_EXPR:
2412 case LROTATE_EXPR:
2413 case RROTATE_EXPR:
2414 op = gimple_op (stmt, op_num + 1);
2415 /* Unlike the other binary operators, shifts/rotates have
2416 the shift count being int, instead of the same type as
2417 the lhs, so make sure the scalar is the right type if
2418 we are dealing with vectors of
2419 long long/long/short/char. */
2420 if (op_num == 1 && constant_p)
2421 op = fold_convert (TREE_TYPE (vector_type), op);
2422 break;
2424 default:
2425 op = gimple_op (stmt, op_num + 1);
2426 break;
2430 if (reduc_index != -1)
2432 loop = (gimple_bb (stmt))->loop_father;
2433 def_stmt = SSA_NAME_DEF_STMT (op);
2435 gcc_assert (loop);
2437 /* Get the def before the loop. In reduction chain we have only
2438 one initial value. */
2439 if ((j != (number_of_copies - 1)
2440 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2441 && i != 0))
2442 && neutral_op)
2443 op = neutral_op;
2444 else
2445 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2446 loop_preheader_edge (loop));
2449 /* Create 'vect_ = {op0,op1,...,opn}'. */
2450 number_of_places_left_in_vector--;
2451 if (constant_p
2452 && !types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
2454 op = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), op);
2455 gcc_assert (op && CONSTANT_CLASS_P (op));
2457 elts[number_of_places_left_in_vector] = op;
2459 if (number_of_places_left_in_vector == 0)
2461 number_of_places_left_in_vector = nunits;
2463 if (constant_p)
2464 vec_cst = build_vector (vector_type, elts);
2465 else
2467 VEC(constructor_elt,gc) *v;
2468 unsigned k;
2469 v = VEC_alloc (constructor_elt, gc, nunits);
2470 for (k = 0; k < nunits; ++k)
2471 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
2472 vec_cst = build_constructor (vector_type, v);
2474 VEC_quick_push (tree, voprnds,
2475 vect_init_vector (stmt, vec_cst,
2476 vector_type, NULL));
2481 /* Since the vectors are created in the reverse order, we should invert
2482 them. */
2483 vec_num = VEC_length (tree, voprnds);
2484 for (j = vec_num; j != 0; j--)
2486 vop = VEC_index (tree, voprnds, j - 1);
2487 VEC_quick_push (tree, *vec_oprnds, vop);
2490 VEC_free (tree, heap, voprnds);
2492 /* In case that VF is greater than the unrolling factor needed for the SLP
2493 group of stmts, NUMBER_OF_VECTORS to be created is greater than
2494 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
2495 to replicate the vectors. */
2496 while (number_of_vectors > VEC_length (tree, *vec_oprnds))
2498 tree neutral_vec = NULL;
2500 if (neutral_op)
2502 if (!neutral_vec)
2503 neutral_vec = build_vector_from_val (vector_type, neutral_op);
2505 VEC_quick_push (tree, *vec_oprnds, neutral_vec);
2507 else
2509 for (i = 0; VEC_iterate (tree, *vec_oprnds, i, vop) && i < vec_num; i++)
2510 VEC_quick_push (tree, *vec_oprnds, vop);
2516 /* Get vectorized definitions from SLP_NODE that contains corresponding
2517 vectorized def-stmts. */
2519 static void
2520 vect_get_slp_vect_defs (slp_tree slp_node, VEC (tree,heap) **vec_oprnds)
2522 tree vec_oprnd;
2523 gimple vec_def_stmt;
2524 unsigned int i;
2526 gcc_assert (SLP_TREE_VEC_STMTS (slp_node));
2528 FOR_EACH_VEC_ELT (gimple, SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
2530 gcc_assert (vec_def_stmt);
2531 vec_oprnd = gimple_get_lhs (vec_def_stmt);
2532 VEC_quick_push (tree, *vec_oprnds, vec_oprnd);
2537 /* Get vectorized definitions for SLP_NODE.
2538 If the scalar definitions are loop invariants or constants, collect them and
2539 call vect_get_constant_vectors() to create vector stmts.
2540 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
2541 must be stored in the corresponding child of SLP_NODE, and we call
2542 vect_get_slp_vect_defs () to retrieve them. */
2544 void
2545 vect_get_slp_defs (VEC (tree, heap) *ops, slp_tree slp_node,
2546 VEC (slp_void_p, heap) **vec_oprnds, int reduc_index)
2548 gimple first_stmt, first_def;
2549 int number_of_vects = 0, i;
2550 unsigned int child_index = 0;
2551 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
2552 slp_tree child = NULL;
2553 VEC (tree, heap) *vec_defs;
2554 tree oprnd, def_lhs;
2555 bool vectorized_defs;
2557 first_stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (slp_node), 0);
2558 FOR_EACH_VEC_ELT (tree, ops, i, oprnd)
2560 /* For each operand we check if it has vectorized definitions in a child
2561 node or we need to create them (for invariants and constants). We
2562 check if the LHS of the first stmt of the next child matches OPRND.
2563 If it does, we found the correct child. Otherwise, we call
2564 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
2565 to check this child node for the next operand. */
2566 vectorized_defs = false;
2567 if (VEC_length (slp_void_p, SLP_TREE_CHILDREN (slp_node)) > child_index)
2569 child = (slp_tree) VEC_index (slp_void_p,
2570 SLP_TREE_CHILDREN (slp_node),
2571 child_index);
2572 first_def = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (child), 0);
2574 /* In the end of a pattern sequence we have a use of the original stmt,
2575 so we need to compare OPRND with the original def. */
2576 if (is_pattern_stmt_p (vinfo_for_stmt (first_def))
2577 && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first_stmt))
2578 && !is_pattern_stmt_p (vinfo_for_stmt (first_stmt)))
2579 first_def = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
2581 if (is_gimple_call (first_def))
2582 def_lhs = gimple_call_lhs (first_def);
2583 else
2584 def_lhs = gimple_assign_lhs (first_def);
2586 if (operand_equal_p (oprnd, def_lhs, 0))
2588 /* The number of vector defs is determined by the number of
2589 vector statements in the node from which we get those
2590 statements. */
2591 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
2592 vectorized_defs = true;
2593 child_index++;
2597 if (!vectorized_defs)
2599 if (i == 0)
2601 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
2602 /* Number of vector stmts was calculated according to LHS in
2603 vect_schedule_slp_instance (), fix it by replacing LHS with
2604 RHS, if necessary. See vect_get_smallest_scalar_type () for
2605 details. */
2606 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
2607 &rhs_size_unit);
2608 if (rhs_size_unit != lhs_size_unit)
2610 number_of_vects *= rhs_size_unit;
2611 number_of_vects /= lhs_size_unit;
2616 /* Allocate memory for vectorized defs. */
2617 vec_defs = VEC_alloc (tree, heap, number_of_vects);
2619 /* For reduction defs we call vect_get_constant_vectors (), since we are
2620 looking for initial loop invariant values. */
2621 if (vectorized_defs && reduc_index == -1)
2622 /* The defs are already vectorized. */
2623 vect_get_slp_vect_defs (child, &vec_defs);
2624 else
2625 /* Build vectors from scalar defs. */
2626 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
2627 number_of_vects, reduc_index);
2629 VEC_quick_push (slp_void_p, *vec_oprnds, (slp_void_p) vec_defs);
2631 /* For reductions, we only need initial values. */
2632 if (reduc_index != -1)
2633 return;
2638 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
2639 building a vector of type MASK_TYPE from it) and two input vectors placed in
2640 DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
2641 shifting by STRIDE elements of DR_CHAIN for every copy.
2642 (STRIDE is the number of vectorized stmts for NODE divided by the number of
2643 copies).
2644 VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
2645 the created stmts must be inserted. */
2647 static inline void
2648 vect_create_mask_and_perm (gimple stmt, gimple next_scalar_stmt,
2649 tree mask, int first_vec_indx, int second_vec_indx,
2650 gimple_stmt_iterator *gsi, slp_tree node,
2651 tree vectype, VEC(tree,heap) *dr_chain,
2652 int ncopies, int vect_stmts_counter)
2654 tree perm_dest;
2655 gimple perm_stmt = NULL;
2656 stmt_vec_info next_stmt_info;
2657 int i, stride;
2658 tree first_vec, second_vec, data_ref;
2660 stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
2662 /* Initialize the vect stmts of NODE to properly insert the generated
2663 stmts later. */
2664 for (i = VEC_length (gimple, SLP_TREE_VEC_STMTS (node));
2665 i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
2666 VEC_quick_push (gimple, SLP_TREE_VEC_STMTS (node), NULL);
2668 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
2669 for (i = 0; i < ncopies; i++)
2671 first_vec = VEC_index (tree, dr_chain, first_vec_indx);
2672 second_vec = VEC_index (tree, dr_chain, second_vec_indx);
2674 /* Generate the permute statement. */
2675 perm_stmt = gimple_build_assign_with_ops3 (VEC_PERM_EXPR, perm_dest,
2676 first_vec, second_vec, mask);
2677 data_ref = make_ssa_name (perm_dest, perm_stmt);
2678 gimple_set_lhs (perm_stmt, data_ref);
2679 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
2681 /* Store the vector statement in NODE. */
2682 VEC_replace (gimple, SLP_TREE_VEC_STMTS (node),
2683 stride * i + vect_stmts_counter, perm_stmt);
2685 first_vec_indx += stride;
2686 second_vec_indx += stride;
2689 /* Mark the scalar stmt as vectorized. */
2690 next_stmt_info = vinfo_for_stmt (next_scalar_stmt);
2691 STMT_VINFO_VEC_STMT (next_stmt_info) = perm_stmt;
2695 /* Given FIRST_MASK_ELEMENT - the mask element in element representation,
2696 return in CURRENT_MASK_ELEMENT its equivalent in target specific
2697 representation. Check that the mask is valid and return FALSE if not.
2698 Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
2699 the next vector, i.e., the current first vector is not needed. */
2701 static bool
2702 vect_get_mask_element (gimple stmt, int first_mask_element, int m,
2703 int mask_nunits, bool only_one_vec, int index,
2704 unsigned char *mask, int *current_mask_element,
2705 bool *need_next_vector, int *number_of_mask_fixes,
2706 bool *mask_fixed, bool *needs_first_vector)
2708 int i;
2710 /* Convert to target specific representation. */
2711 *current_mask_element = first_mask_element + m;
2712 /* Adjust the value in case it's a mask for second and third vectors. */
2713 *current_mask_element -= mask_nunits * (*number_of_mask_fixes - 1);
2715 if (*current_mask_element < mask_nunits)
2716 *needs_first_vector = true;
2718 /* We have only one input vector to permute but the mask accesses values in
2719 the next vector as well. */
2720 if (only_one_vec && *current_mask_element >= mask_nunits)
2722 if (vect_print_dump_info (REPORT_DETAILS))
2724 fprintf (vect_dump, "permutation requires at least two vectors ");
2725 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
2728 return false;
2731 /* The mask requires the next vector. */
2732 if (*current_mask_element >= mask_nunits * 2)
2734 if (*needs_first_vector || *mask_fixed)
2736 /* We either need the first vector too or have already moved to the
2737 next vector. In both cases, this permutation needs three
2738 vectors. */
2739 if (vect_print_dump_info (REPORT_DETAILS))
2741 fprintf (vect_dump, "permutation requires at "
2742 "least three vectors ");
2743 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
2746 return false;
2749 /* We move to the next vector, dropping the first one and working with
2750 the second and the third - we need to adjust the values of the mask
2751 accordingly. */
2752 *current_mask_element -= mask_nunits * *number_of_mask_fixes;
2754 for (i = 0; i < index; i++)
2755 mask[i] -= mask_nunits * *number_of_mask_fixes;
2757 (*number_of_mask_fixes)++;
2758 *mask_fixed = true;
2761 *need_next_vector = *mask_fixed;
2763 /* This was the last element of this mask. Start a new one. */
2764 if (index == mask_nunits - 1)
2766 *number_of_mask_fixes = 1;
2767 *mask_fixed = false;
2768 *needs_first_vector = false;
2771 return true;
2775 /* Generate vector permute statements from a list of loads in DR_CHAIN.
2776 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
2777 permute statements for SLP_NODE_INSTANCE. */
2778 bool
2779 vect_transform_slp_perm_load (gimple stmt, VEC (tree, heap) *dr_chain,
2780 gimple_stmt_iterator *gsi, int vf,
2781 slp_instance slp_node_instance, bool analyze_only)
2783 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2784 tree mask_element_type = NULL_TREE, mask_type;
2785 int i, j, k, nunits, vec_index = 0, scalar_index;
2786 slp_tree node;
2787 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2788 gimple next_scalar_stmt;
2789 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
2790 int first_mask_element;
2791 int index, unroll_factor, current_mask_element, ncopies;
2792 unsigned char *mask;
2793 bool only_one_vec = false, need_next_vector = false;
2794 int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
2795 int number_of_mask_fixes = 1;
2796 bool mask_fixed = false;
2797 bool needs_first_vector = false;
2798 enum machine_mode mode;
2800 mode = TYPE_MODE (vectype);
2802 if (!can_vec_perm_p (mode, false, NULL))
2804 if (vect_print_dump_info (REPORT_DETAILS))
2806 fprintf (vect_dump, "no vect permute for ");
2807 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
2809 return false;
2812 /* The generic VEC_PERM_EXPR code always uses an integral type of the
2813 same size as the vector element being permuted. */
2814 mask_element_type = lang_hooks.types.type_for_mode
2815 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
2816 mask_type = get_vectype_for_scalar_type (mask_element_type);
2817 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2818 mask = XALLOCAVEC (unsigned char, nunits);
2819 unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
2821 /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
2822 unrolling factor. */
2823 orig_vec_stmts_num = group_size *
2824 SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance) / nunits;
2825 if (orig_vec_stmts_num == 1)
2826 only_one_vec = true;
2828 /* Number of copies is determined by the final vectorization factor
2829 relatively to SLP_NODE_INSTANCE unrolling factor. */
2830 ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
2832 /* Generate permutation masks for every NODE. Number of masks for each NODE
2833 is equal to GROUP_SIZE.
2834 E.g., we have a group of three nodes with three loads from the same
2835 location in each node, and the vector size is 4. I.e., we have a
2836 a0b0c0a1b1c1... sequence and we need to create the following vectors:
2837 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
2838 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
2841 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
2842 The last mask is illegal since we assume two operands for permute
2843 operation, and the mask element values can't be outside that range.
2844 Hence, the last mask must be converted into {2,5,5,5}.
2845 For the first two permutations we need the first and the second input
2846 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
2847 we need the second and the third vectors: {b1,c1,a2,b2} and
2848 {c2,a3,b3,c3}. */
2850 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (slp_node_instance), i, node)
2852 scalar_index = 0;
2853 index = 0;
2854 vect_stmts_counter = 0;
2855 vec_index = 0;
2856 first_vec_index = vec_index++;
2857 if (only_one_vec)
2858 second_vec_index = first_vec_index;
2859 else
2860 second_vec_index = vec_index++;
2862 for (j = 0; j < unroll_factor; j++)
2864 for (k = 0; k < group_size; k++)
2866 first_mask_element = i + j * group_size;
2867 if (!vect_get_mask_element (stmt, first_mask_element, 0,
2868 nunits, only_one_vec, index,
2869 mask, &current_mask_element,
2870 &need_next_vector,
2871 &number_of_mask_fixes, &mask_fixed,
2872 &needs_first_vector))
2873 return false;
2874 mask[index++] = current_mask_element;
2876 if (index == nunits)
2878 tree mask_vec, *mask_elts;
2879 int l;
2881 if (!can_vec_perm_p (mode, false, mask))
2883 if (vect_print_dump_info (REPORT_DETAILS))
2885 fprintf (vect_dump, "unsupported vect permute { ");
2886 for (i = 0; i < nunits; ++i)
2887 fprintf (vect_dump, "%d ", mask[i]);
2888 fprintf (vect_dump, "}\n");
2890 return false;
2893 mask_elts = XALLOCAVEC (tree, nunits);
2894 for (l = 0; l < nunits; ++l)
2895 mask_elts[l] = build_int_cst (mask_element_type, mask[l]);
2896 mask_vec = build_vector (mask_type, mask_elts);
2897 index = 0;
2899 if (!analyze_only)
2901 if (need_next_vector)
2903 first_vec_index = second_vec_index;
2904 second_vec_index = vec_index;
2907 next_scalar_stmt = VEC_index (gimple,
2908 SLP_TREE_SCALAR_STMTS (node), scalar_index++);
2910 vect_create_mask_and_perm (stmt, next_scalar_stmt,
2911 mask_vec, first_vec_index, second_vec_index,
2912 gsi, node, vectype, dr_chain,
2913 ncopies, vect_stmts_counter++);
2920 return true;
2925 /* Vectorize SLP instance tree in postorder. */
2927 static bool
2928 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
2929 unsigned int vectorization_factor)
2931 gimple stmt;
2932 bool grouped_store, is_store;
2933 gimple_stmt_iterator si;
2934 stmt_vec_info stmt_info;
2935 unsigned int vec_stmts_size, nunits, group_size;
2936 tree vectype;
2937 int i;
2938 slp_tree loads_node;
2939 slp_void_p child;
2941 if (!node)
2942 return false;
2944 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
2945 vect_schedule_slp_instance ((slp_tree) child, instance,
2946 vectorization_factor);
2948 stmt = VEC_index (gimple, SLP_TREE_SCALAR_STMTS (node), 0);
2949 stmt_info = vinfo_for_stmt (stmt);
2951 /* VECTYPE is the type of the destination. */
2952 vectype = STMT_VINFO_VECTYPE (stmt_info);
2953 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
2954 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
2956 /* For each SLP instance calculate number of vector stmts to be created
2957 for the scalar stmts in each node of the SLP tree. Number of vector
2958 elements in one vector iteration is the number of scalar elements in
2959 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
2960 size. */
2961 vec_stmts_size = (vectorization_factor * group_size) / nunits;
2963 /* In case of load permutation we have to allocate vectorized statements for
2964 all the nodes that participate in that permutation. */
2965 if (SLP_INSTANCE_LOAD_PERMUTATION (instance))
2967 FOR_EACH_VEC_ELT (slp_tree, SLP_INSTANCE_LOADS (instance), i, loads_node)
2969 if (!SLP_TREE_VEC_STMTS (loads_node))
2971 SLP_TREE_VEC_STMTS (loads_node) = VEC_alloc (gimple, heap,
2972 vec_stmts_size);
2973 SLP_TREE_NUMBER_OF_VEC_STMTS (loads_node) = vec_stmts_size;
2978 if (!SLP_TREE_VEC_STMTS (node))
2980 SLP_TREE_VEC_STMTS (node) = VEC_alloc (gimple, heap, vec_stmts_size);
2981 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
2984 if (vect_print_dump_info (REPORT_DETAILS))
2986 fprintf (vect_dump, "------>vectorizing SLP node starting from: ");
2987 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
2990 /* Loads should be inserted before the first load. */
2991 if (SLP_INSTANCE_FIRST_LOAD_STMT (instance)
2992 && STMT_VINFO_GROUPED_ACCESS (stmt_info)
2993 && !REFERENCE_CLASS_P (gimple_get_lhs (stmt))
2994 && SLP_INSTANCE_LOAD_PERMUTATION (instance))
2995 si = gsi_for_stmt (SLP_INSTANCE_FIRST_LOAD_STMT (instance));
2996 else if (is_pattern_stmt_p (stmt_info))
2997 si = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2998 else
2999 si = gsi_for_stmt (stmt);
3001 /* Stores should be inserted just before the last store. */
3002 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
3003 && REFERENCE_CLASS_P (gimple_get_lhs (stmt)))
3005 gimple last_store = vect_find_last_store_in_slp_instance (instance);
3006 if (is_pattern_stmt_p (vinfo_for_stmt (last_store)))
3007 last_store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (last_store));
3008 si = gsi_for_stmt (last_store);
3011 /* Mark the first element of the reduction chain as reduction to properly
3012 transform the node. In the analysis phase only the last element of the
3013 chain is marked as reduction. */
3014 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3015 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3017 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3018 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3021 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3022 return is_store;
3025 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3026 For loop vectorization this is done in vectorizable_call, but for SLP
3027 it needs to be deferred until end of vect_schedule_slp, because multiple
3028 SLP instances may refer to the same scalar stmt. */
3030 static void
3031 vect_remove_slp_scalar_calls (slp_tree node)
3033 gimple stmt, new_stmt;
3034 gimple_stmt_iterator gsi;
3035 int i;
3036 slp_void_p child;
3037 tree lhs;
3038 stmt_vec_info stmt_info;
3040 if (!node)
3041 return;
3043 FOR_EACH_VEC_ELT (slp_void_p, SLP_TREE_CHILDREN (node), i, child)
3044 vect_remove_slp_scalar_calls ((slp_tree) child);
3046 FOR_EACH_VEC_ELT (gimple, SLP_TREE_SCALAR_STMTS (node), i, stmt)
3048 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3049 continue;
3050 stmt_info = vinfo_for_stmt (stmt);
3051 if (stmt_info == NULL
3052 || is_pattern_stmt_p (stmt_info)
3053 || !PURE_SLP_STMT (stmt_info))
3054 continue;
3055 lhs = gimple_call_lhs (stmt);
3056 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3057 set_vinfo_for_stmt (new_stmt, stmt_info);
3058 set_vinfo_for_stmt (stmt, NULL);
3059 STMT_VINFO_STMT (stmt_info) = new_stmt;
3060 gsi = gsi_for_stmt (stmt);
3061 gsi_replace (&gsi, new_stmt, false);
3062 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3066 /* Generate vector code for all SLP instances in the loop/basic block. */
3068 bool
3069 vect_schedule_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
3071 VEC (slp_instance, heap) *slp_instances;
3072 slp_instance instance;
3073 unsigned int i, vf;
3074 bool is_store = false;
3076 if (loop_vinfo)
3078 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3079 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3081 else
3083 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
3084 vf = 1;
3087 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
3089 /* Schedule the tree of INSTANCE. */
3090 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3091 instance, vf);
3092 if (vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS)
3093 || vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
3094 fprintf (vect_dump, "vectorizing stmts using SLP.");
3097 FOR_EACH_VEC_ELT (slp_instance, slp_instances, i, instance)
3099 slp_tree root = SLP_INSTANCE_TREE (instance);
3100 gimple store;
3101 unsigned int j;
3102 gimple_stmt_iterator gsi;
3104 vect_remove_slp_scalar_calls (root);
3106 for (j = 0; VEC_iterate (gimple, SLP_TREE_SCALAR_STMTS (root), j, store)
3107 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3109 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3110 break;
3112 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3113 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3114 /* Free the attached stmt_vec_info and remove the stmt. */
3115 gsi = gsi_for_stmt (store);
3116 unlink_stmt_vdef (store);
3117 gsi_remove (&gsi, true);
3118 release_defs (store);
3119 free_stmt_vec_info (store);
3123 return is_store;
3127 /* Vectorize the basic block. */
3129 void
3130 vect_slp_transform_bb (basic_block bb)
3132 bb_vec_info bb_vinfo = vec_info_for_bb (bb);
3133 gimple_stmt_iterator si;
3135 gcc_assert (bb_vinfo);
3137 if (vect_print_dump_info (REPORT_DETAILS))
3138 fprintf (vect_dump, "SLPing BB\n");
3140 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
3142 gimple stmt = gsi_stmt (si);
3143 stmt_vec_info stmt_info;
3145 if (vect_print_dump_info (REPORT_DETAILS))
3147 fprintf (vect_dump, "------>SLPing statement: ");
3148 print_gimple_stmt (vect_dump, stmt, 0, TDF_SLIM);
3151 stmt_info = vinfo_for_stmt (stmt);
3152 gcc_assert (stmt_info);
3154 /* Schedule all the SLP instances when the first SLP stmt is reached. */
3155 if (STMT_SLP_TYPE (stmt_info))
3157 vect_schedule_slp (NULL, bb_vinfo);
3158 break;
3162 if (vect_print_dump_info (REPORT_DETAILS))
3163 fprintf (vect_dump, "BASIC BLOCK VECTORIZED\n");
3165 destroy_bb_vec_info (bb_vinfo);