1 /* SLP - Basic Block Vectorization
2 Copyright (C) 2007-2013 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "basic-block.h"
31 #include "gimple-pretty-print.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
36 #include "recog.h" /* FIXME: for insn_data */
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. */
45 find_bb_location (basic_block bb)
48 gimple_stmt_iterator si;
53 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
56 if (gimple_location (stmt) != UNKNOWN_LOC)
57 return gimple_location (stmt);
64 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
67 vect_free_slp_tree (slp_tree node)
75 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
76 vect_free_slp_tree (child);
78 SLP_TREE_CHILDREN (node).release ();
79 SLP_TREE_SCALAR_STMTS (node).release ();
80 SLP_TREE_VEC_STMTS (node).release ();
86 /* Free the memory allocated for the SLP instance. */
89 vect_free_slp_instance (slp_instance instance)
91 vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
92 SLP_INSTANCE_LOAD_PERMUTATION (instance).release ();
93 SLP_INSTANCE_LOADS (instance).release ();
94 SLP_INSTANCE_BODY_COST_VEC (instance).release ();
99 /* Create an SLP node for SCALAR_STMTS. */
102 vect_create_new_slp_node (vec<gimple> scalar_stmts)
105 gimple stmt = scalar_stmts[0];
108 if (is_gimple_call (stmt))
109 nops = gimple_call_num_args (stmt);
110 else if (is_gimple_assign (stmt))
112 nops = gimple_num_ops (stmt) - 1;
113 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
119 node = XNEW (struct _slp_tree);
120 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
121 SLP_TREE_VEC_STMTS (node).create (0);
122 SLP_TREE_CHILDREN (node).create (nops);
128 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
130 static vec<slp_oprnd_info>
131 vect_create_oprnd_info (int nops, int group_size)
134 slp_oprnd_info oprnd_info;
135 vec<slp_oprnd_info> oprnds_info;
137 oprnds_info.create (nops);
138 for (i = 0; i < nops; i++)
140 oprnd_info = XNEW (struct _slp_oprnd_info);
141 oprnd_info->def_stmts.create (group_size);
142 oprnd_info->first_dt = vect_uninitialized_def;
143 oprnd_info->first_def_type = NULL_TREE;
144 oprnd_info->first_const_oprnd = NULL_TREE;
145 oprnd_info->first_pattern = false;
146 oprnds_info.quick_push (oprnd_info);
153 /* Free operands info. */
156 vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
159 slp_oprnd_info oprnd_info;
161 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
163 oprnd_info->def_stmts.release ();
164 XDELETE (oprnd_info);
167 oprnds_info.release ();
171 /* Find the place of the data-ref in STMT in the interleaving chain that starts
172 from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
175 vect_get_place_in_interleaving_chain (gimple stmt, gimple first_stmt)
177 gimple next_stmt = first_stmt;
180 if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
185 if (next_stmt == stmt)
188 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
196 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
197 they are of a valid type and that they match the defs of the first stmt of
198 the SLP group (stored in OPRNDS_INFO). */
201 vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
202 gimple stmt, bool first,
203 vec<slp_oprnd_info> *oprnds_info)
206 unsigned int i, number_of_oprnds;
207 tree def, def_op0 = NULL_TREE;
209 enum vect_def_type dt = vect_uninitialized_def;
210 enum vect_def_type dt_op0 = vect_uninitialized_def;
211 struct loop *loop = NULL;
212 enum tree_code rhs_code;
213 bool different_types = false;
214 bool pattern = false;
215 slp_oprnd_info oprnd_info, oprnd0_info, oprnd1_info;
217 tree compare_rhs = NULL_TREE;
220 loop = LOOP_VINFO_LOOP (loop_vinfo);
222 if (is_gimple_call (stmt))
224 number_of_oprnds = gimple_call_num_args (stmt);
227 else if (is_gimple_assign (stmt))
229 number_of_oprnds = gimple_num_ops (stmt) - 1;
230 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
236 for (i = 0; i < number_of_oprnds; i++)
241 compare_rhs = NULL_TREE;
244 oprnd = gimple_op (stmt, op_idx++);
246 oprnd_info = (*oprnds_info)[i];
248 if (COMPARISON_CLASS_P (oprnd))
250 compare_rhs = TREE_OPERAND (oprnd, 1);
251 oprnd = TREE_OPERAND (oprnd, 0);
254 if (!vect_is_simple_use (oprnd, NULL, loop_vinfo, bb_vinfo, &def_stmt,
256 || (!def_stmt && dt != vect_constant_def))
258 if (dump_enabled_p ())
260 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
261 "Build SLP failed: can't find def for ");
262 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
268 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
269 from the pattern. Check that all the stmts of the node are in the
271 if (def_stmt && gimple_bb (def_stmt)
272 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
273 || (!loop && gimple_bb (def_stmt) == BB_VINFO_BB (bb_vinfo)
274 && gimple_code (def_stmt) != GIMPLE_PHI))
275 && vinfo_for_stmt (def_stmt)
276 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
277 && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
278 && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
281 if (!first && !oprnd_info->first_pattern)
283 if (dump_enabled_p ())
285 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
286 "Build SLP failed: some of the stmts"
287 " are in a pattern, and others are not ");
288 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
294 def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
295 dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
297 if (dt == vect_unknown_def_type)
299 if (dump_enabled_p ())
300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
301 "Unsupported pattern.");
305 switch (gimple_code (def_stmt))
308 def = gimple_phi_result (def_stmt);
312 def = gimple_assign_lhs (def_stmt);
316 if (dump_enabled_p ())
317 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
318 "unsupported defining stmt: ");
325 oprnd_info->first_dt = dt;
326 oprnd_info->first_pattern = pattern;
329 oprnd_info->first_def_type = TREE_TYPE (def);
330 oprnd_info->first_const_oprnd = NULL_TREE;
334 oprnd_info->first_def_type = NULL_TREE;
335 oprnd_info->first_const_oprnd = oprnd;
346 /* Not first stmt of the group, check that the def-stmt/s match
347 the def-stmt/s of the first stmt. Allow different definition
348 types for reduction chains: the first stmt must be a
349 vect_reduction_def (a phi node), and the rest
350 vect_internal_def. */
351 if (((oprnd_info->first_dt != dt
352 && !(oprnd_info->first_dt == vect_reduction_def
353 && dt == vect_internal_def))
354 || (oprnd_info->first_def_type != NULL_TREE
356 && !types_compatible_p (oprnd_info->first_def_type,
359 && !types_compatible_p (TREE_TYPE (oprnd_info->first_const_oprnd),
363 if (number_of_oprnds != 2)
365 if (dump_enabled_p ())
366 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
367 "Build SLP failed: different types ");
372 /* Try to swap operands in case of binary operation. */
374 different_types = true;
377 oprnd0_info = (*oprnds_info)[0];
378 if (is_gimple_assign (stmt)
379 && (rhs_code = gimple_assign_rhs_code (stmt))
380 && TREE_CODE_CLASS (rhs_code) == tcc_binary
381 && commutative_tree_code (rhs_code)
382 && oprnd0_info->first_dt == dt
383 && oprnd_info->first_dt == dt_op0
385 && !(oprnd0_info->first_def_type
386 && !types_compatible_p (oprnd0_info->first_def_type,
388 && !(oprnd_info->first_def_type
389 && !types_compatible_p (oprnd_info->first_def_type,
390 TREE_TYPE (def_op0))))
392 if (dump_enabled_p ())
394 dump_printf_loc (MSG_NOTE, vect_location,
395 "Swapping operands of ");
396 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
399 swap_tree_operands (stmt, gimple_assign_rhs1_ptr (stmt),
400 gimple_assign_rhs2_ptr (stmt));
404 if (dump_enabled_p ())
405 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
406 "Build SLP failed: different types ");
414 /* Check the types of the definitions. */
417 case vect_constant_def:
418 case vect_external_def:
419 case vect_reduction_def:
422 case vect_internal_def:
425 oprnd0_info = (*oprnds_info)[0];
426 oprnd1_info = (*oprnds_info)[0];
428 oprnd1_info->def_stmts.quick_push (def_stmt);
430 oprnd0_info->def_stmts.quick_push (def_stmt);
433 oprnd_info->def_stmts.quick_push (def_stmt);
438 /* FORNOW: Not supported. */
439 if (dump_enabled_p ())
441 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
442 "Build SLP failed: illegal type of def ");
443 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, def);
454 /* Recursively build an SLP tree starting from NODE.
455 Fail (and return FALSE) if def-stmts are not isomorphic, require data
456 permutation or are of unsupported types of operation. Otherwise, return
460 vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
461 slp_tree *node, unsigned int group_size,
462 unsigned int *max_nunits,
463 vec<int> *load_permutation,
464 vec<slp_tree> *loads,
465 unsigned int vectorization_factor, bool *loads_permuted)
468 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (*node);
469 gimple stmt = stmts[0];
470 enum tree_code first_stmt_code = ERROR_MARK, rhs_code = ERROR_MARK;
471 enum tree_code first_cond_code = ERROR_MARK;
473 bool stop_recursion = false, need_same_oprnds = false;
474 tree vectype, scalar_type, first_op1 = NULL_TREE;
477 enum machine_mode optab_op2_mode;
478 enum machine_mode vec_mode;
479 struct data_reference *first_dr;
481 bool permutation = false;
482 unsigned int load_place;
483 gimple first_load = NULL, prev_first_load = NULL, old_first_load = NULL;
484 vec<slp_oprnd_info> oprnds_info;
486 slp_oprnd_info oprnd_info;
489 if (is_gimple_call (stmt))
490 nops = gimple_call_num_args (stmt);
491 else if (is_gimple_assign (stmt))
493 nops = gimple_num_ops (stmt) - 1;
494 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
500 oprnds_info = vect_create_oprnd_info (nops, group_size);
502 /* For every stmt in NODE find its def stmt/s. */
503 FOR_EACH_VEC_ELT (stmts, i, stmt)
505 if (dump_enabled_p ())
507 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
508 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
511 /* Fail to vectorize statements marked as unvectorizable. */
512 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
514 if (dump_enabled_p ())
516 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
517 "Build SLP failed: unvectorizable statement ");
518 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
521 vect_free_oprnd_info (oprnds_info);
525 lhs = gimple_get_lhs (stmt);
526 if (lhs == NULL_TREE)
528 if (dump_enabled_p ())
530 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
531 "Build SLP failed: not GIMPLE_ASSIGN nor "
533 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
536 vect_free_oprnd_info (oprnds_info);
540 if (is_gimple_assign (stmt)
541 && gimple_assign_rhs_code (stmt) == COND_EXPR
542 && (cond = gimple_assign_rhs1 (stmt))
543 && !COMPARISON_CLASS_P (cond))
545 if (dump_enabled_p ())
547 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
548 "Build SLP failed: condition is not "
550 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
553 vect_free_oprnd_info (oprnds_info);
557 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
558 vectype = get_vectype_for_scalar_type (scalar_type);
561 if (dump_enabled_p ())
563 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
564 "Build SLP failed: unsupported data-type ");
565 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
569 vect_free_oprnd_info (oprnds_info);
573 /* In case of multiple types we need to detect the smallest type. */
574 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
576 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
578 vectorization_factor = *max_nunits;
581 if (is_gimple_call (stmt))
583 rhs_code = CALL_EXPR;
584 if (gimple_call_internal_p (stmt)
585 || gimple_call_tail_p (stmt)
586 || gimple_call_noreturn_p (stmt)
587 || !gimple_call_nothrow_p (stmt)
588 || gimple_call_chain (stmt))
590 if (dump_enabled_p ())
592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
593 "Build SLP failed: unsupported call type ");
594 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
597 vect_free_oprnd_info (oprnds_info);
602 rhs_code = gimple_assign_rhs_code (stmt);
604 /* Check the operation. */
607 first_stmt_code = rhs_code;
609 /* Shift arguments should be equal in all the packed stmts for a
610 vector shift with scalar shift operand. */
611 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
612 || rhs_code == LROTATE_EXPR
613 || rhs_code == RROTATE_EXPR)
615 vec_mode = TYPE_MODE (vectype);
617 /* First see if we have a vector/vector shift. */
618 optab = optab_for_tree_code (rhs_code, vectype,
622 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
624 /* No vector/vector shift, try for a vector/scalar shift. */
625 optab = optab_for_tree_code (rhs_code, vectype,
630 if (dump_enabled_p ())
631 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
632 "Build SLP failed: no optab.");
633 vect_free_oprnd_info (oprnds_info);
636 icode = (int) optab_handler (optab, vec_mode);
637 if (icode == CODE_FOR_nothing)
639 if (dump_enabled_p ())
640 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
642 "op not supported by target.");
643 vect_free_oprnd_info (oprnds_info);
646 optab_op2_mode = insn_data[icode].operand[2].mode;
647 if (!VECTOR_MODE_P (optab_op2_mode))
649 need_same_oprnds = true;
650 first_op1 = gimple_assign_rhs2 (stmt);
654 else if (rhs_code == WIDEN_LSHIFT_EXPR)
656 need_same_oprnds = true;
657 first_op1 = gimple_assign_rhs2 (stmt);
662 if (first_stmt_code != rhs_code
663 && (first_stmt_code != IMAGPART_EXPR
664 || rhs_code != REALPART_EXPR)
665 && (first_stmt_code != REALPART_EXPR
666 || rhs_code != IMAGPART_EXPR)
667 && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
668 && (first_stmt_code == ARRAY_REF
669 || first_stmt_code == BIT_FIELD_REF
670 || first_stmt_code == INDIRECT_REF
671 || first_stmt_code == COMPONENT_REF
672 || first_stmt_code == MEM_REF)))
674 if (dump_enabled_p ())
676 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
677 "Build SLP failed: different operation "
679 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
682 vect_free_oprnd_info (oprnds_info);
687 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
689 if (dump_enabled_p ())
691 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
692 "Build SLP failed: different shift "
694 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
697 vect_free_oprnd_info (oprnds_info);
701 if (rhs_code == CALL_EXPR)
703 gimple first_stmt = stmts[0];
704 if (gimple_call_num_args (stmt) != nops
705 || !operand_equal_p (gimple_call_fn (first_stmt),
706 gimple_call_fn (stmt), 0)
707 || gimple_call_fntype (first_stmt)
708 != gimple_call_fntype (stmt))
710 if (dump_enabled_p ())
712 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
713 "Build SLP failed: different calls in ");
714 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
718 vect_free_oprnd_info (oprnds_info);
724 /* Grouped store or load. */
725 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
727 if (REFERENCE_CLASS_P (lhs))
730 if (!vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo,
731 stmt, (i == 0), &oprnds_info))
733 vect_free_oprnd_info (oprnds_info);
740 unsigned unrolling_factor
741 = least_common_multiple
742 (*max_nunits, group_size) / group_size;
743 /* FORNOW: Check that there is no gap between the loads
744 and no gap between the groups when we need to load
745 multiple groups at once.
746 ??? We should enhance this to only disallow gaps
748 if ((unrolling_factor > 1
749 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
750 && GROUP_GAP (vinfo_for_stmt (stmt)) != 0)
751 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != stmt
752 && GROUP_GAP (vinfo_for_stmt (stmt)) != 1))
754 if (dump_enabled_p ())
756 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
757 "Build SLP failed: grouped "
759 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
763 vect_free_oprnd_info (oprnds_info);
767 /* Check that the size of interleaved loads group is not
768 greater than the SLP group size. */
770 = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
772 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
773 && ((GROUP_SIZE (vinfo_for_stmt (stmt))
774 - GROUP_GAP (vinfo_for_stmt (stmt)))
775 > ncopies * group_size))
777 if (dump_enabled_p ())
779 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
780 "Build SLP failed: the number "
781 "of interleaved loads is greater than "
782 "the SLP group size ");
783 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
787 vect_free_oprnd_info (oprnds_info);
791 old_first_load = first_load;
792 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
795 /* Check that there are no loads from different interleaving
796 chains in the same node. The only exception is complex
798 if (prev_first_load != first_load
799 && rhs_code != REALPART_EXPR
800 && rhs_code != IMAGPART_EXPR)
802 if (dump_enabled_p ())
804 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
806 "Build SLP failed: different "
807 "interleaving chains in one node ");
808 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
812 vect_free_oprnd_info (oprnds_info);
817 prev_first_load = first_load;
819 /* In some cases a group of loads is just the same load
820 repeated N times. Only analyze its cost once. */
821 if (first_load == stmt && old_first_load != first_load)
823 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
824 if (vect_supportable_dr_alignment (first_dr, false)
825 == dr_unaligned_unsupported)
827 if (dump_enabled_p ())
829 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
831 "Build SLP failed: unsupported "
833 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
837 vect_free_oprnd_info (oprnds_info);
842 /* Store the place of this load in the interleaving chain. In
843 case that permutation is needed we later decide if a specific
844 permutation is supported. */
845 load_place = vect_get_place_in_interleaving_chain (stmt,
850 load_permutation->safe_push (load_place);
852 /* We stop the tree when we reach a group of loads. */
853 stop_recursion = true;
856 } /* Grouped access. */
859 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
861 /* Not grouped load. */
862 if (dump_enabled_p ())
864 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
865 "Build SLP failed: not grouped load ");
866 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
869 /* FORNOW: Not grouped loads are not supported. */
870 vect_free_oprnd_info (oprnds_info);
874 /* Not memory operation. */
875 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
876 && TREE_CODE_CLASS (rhs_code) != tcc_unary
877 && rhs_code != COND_EXPR
878 && rhs_code != CALL_EXPR)
880 if (dump_enabled_p ())
882 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
883 "Build SLP failed: operation");
884 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
885 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
888 vect_free_oprnd_info (oprnds_info);
892 if (rhs_code == COND_EXPR)
894 tree cond_expr = gimple_assign_rhs1 (stmt);
897 first_cond_code = TREE_CODE (cond_expr);
898 else if (first_cond_code != TREE_CODE (cond_expr))
900 if (dump_enabled_p ())
902 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
903 "Build SLP failed: different"
905 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
909 vect_free_oprnd_info (oprnds_info);
914 /* Find the def-stmts. */
915 if (!vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo, stmt,
916 (i == 0), &oprnds_info))
918 vect_free_oprnd_info (oprnds_info);
924 /* Grouped loads were reached - stop the recursion. */
927 loads->safe_push (*node);
929 *loads_permuted = true;
932 /* We don't check here complex numbers chains, so we set
933 LOADS_PERMUTED for further check in
934 vect_supported_load_permutation_p. */
935 if (rhs_code == REALPART_EXPR || rhs_code == IMAGPART_EXPR)
936 *loads_permuted = true;
939 vect_free_oprnd_info (oprnds_info);
943 /* Create SLP_TREE nodes for the definition node/s. */
944 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
948 if (oprnd_info->first_dt != vect_internal_def)
951 child = vect_create_new_slp_node (oprnd_info->def_stmts);
953 || !vect_build_slp_tree (loop_vinfo, bb_vinfo, &child, group_size,
954 max_nunits, load_permutation, loads,
955 vectorization_factor, loads_permuted))
958 oprnd_info->def_stmts = vNULL;
959 vect_free_slp_tree (child);
960 vect_free_oprnd_info (oprnds_info);
964 oprnd_info->def_stmts.create (0);
965 SLP_TREE_CHILDREN (*node).quick_push (child);
968 vect_free_oprnd_info (oprnds_info);
972 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
975 vect_print_slp_tree (int dump_kind, slp_tree node)
984 dump_printf (dump_kind, "node ");
985 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
987 dump_printf (dump_kind, "\n\tstmt %d ", i);
988 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
990 dump_printf (dump_kind, "\n");
992 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
993 vect_print_slp_tree (dump_kind, child);
997 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
998 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
999 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1000 stmts in NODE are to be marked. */
1003 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1012 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1013 if (j < 0 || i == j)
1014 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1016 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1017 vect_mark_slp_stmts (child, mark, j);
1021 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1024 vect_mark_slp_stmts_relevant (slp_tree node)
1028 stmt_vec_info stmt_info;
1034 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1036 stmt_info = vinfo_for_stmt (stmt);
1037 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1038 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1039 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1042 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1043 vect_mark_slp_stmts_relevant (child);
1047 /* Check if the permutation required by the SLP INSTANCE is supported.
1048 Reorganize the SLP nodes stored in SLP_INSTANCE_LOADS if needed. */
1051 vect_supported_slp_permutation_p (slp_instance instance)
1053 slp_tree node = SLP_INSTANCE_LOADS (instance)[0];
1054 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1055 gimple first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
1056 vec<slp_tree> sorted_loads = vNULL;
1058 slp_tree *tmp_loads = NULL;
1059 int group_size = SLP_INSTANCE_GROUP_SIZE (instance), i, j;
1062 /* FORNOW: The only supported loads permutation is loads from the same
1063 location in all the loads in the node, when the data-refs in
1064 nodes of LOADS constitute an interleaving chain.
1065 Sort the nodes according to the order of accesses in the chain. */
1066 tmp_loads = (slp_tree *) xmalloc (sizeof (slp_tree) * group_size);
1068 SLP_INSTANCE_LOAD_PERMUTATION (instance).iterate (i, &index)
1069 && SLP_INSTANCE_LOADS (instance).iterate (j, &load);
1070 i += group_size, j++)
1072 gimple scalar_stmt = SLP_TREE_SCALAR_STMTS (load)[0];
1073 /* Check that the loads are all in the same interleaving chain. */
1074 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (scalar_stmt)) != first_load)
1076 if (dump_enabled_p ())
1078 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1079 "Build SLP failed: unsupported data "
1081 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1089 tmp_loads[index] = load;
1092 sorted_loads.create (group_size);
1093 for (i = 0; i < group_size; i++)
1094 sorted_loads.safe_push (tmp_loads[i]);
1096 SLP_INSTANCE_LOADS (instance).release ();
1097 SLP_INSTANCE_LOADS (instance) = sorted_loads;
1100 if (!vect_transform_slp_perm_load (stmt, vNULL, NULL,
1101 SLP_INSTANCE_UNROLLING_FACTOR (instance),
1109 /* Rearrange the statements of NODE according to PERMUTATION. */
1112 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1113 vec<int> permutation)
1116 vec<gimple> tmp_stmts;
1120 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1121 vect_slp_rearrange_stmts (child, group_size, permutation);
1123 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1124 tmp_stmts.create (group_size);
1125 tmp_stmts.quick_grow_cleared (group_size);
1127 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1128 tmp_stmts[permutation[i]] = stmt;
1130 SLP_TREE_SCALAR_STMTS (node).release ();
1131 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1135 /* Check if the required load permutation is supported.
1136 LOAD_PERMUTATION contains a list of indices of the loads.
1137 In SLP this permutation is relative to the order of grouped stores that are
1138 the base of the SLP instance. */
1141 vect_supported_load_permutation_p (slp_instance slp_instn, int group_size,
1142 vec<int> load_permutation)
1144 int i = 0, j, prev = -1, next, k, number_of_groups;
1145 bool supported, bad_permutation = false;
1147 slp_tree node, other_complex_node;
1148 gimple stmt, first = NULL, other_node_first, load, next_load, first_load;
1149 unsigned complex_numbers = 0;
1150 struct data_reference *dr;
1151 bb_vec_info bb_vinfo;
1153 /* FORNOW: permutations are only supported in SLP. */
1157 if (dump_enabled_p ())
1159 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1160 FOR_EACH_VEC_ELT (load_permutation, i, next)
1161 dump_printf (MSG_NOTE, "%d ", next);
1164 /* In case of reduction every load permutation is allowed, since the order
1165 of the reduction statements is not important (as opposed to the case of
1166 grouped stores). The only condition we need to check is that all the
1167 load nodes are of the same size and have the same permutation (and then
1168 rearrange all the nodes of the SLP instance according to this
1171 /* Check that all the load nodes are of the same size. */
1172 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1174 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1177 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1178 if (is_gimple_assign (stmt)
1179 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1180 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR))
1184 /* Complex operands can be swapped as following:
1185 real_c = real_b + real_a;
1186 imag_c = imag_a + imag_b;
1187 i.e., we have {real_b, imag_a} and {real_a, imag_b} instead of
1188 {real_a, imag_a} and {real_b, imag_b}. We check here that if interleaving
1189 chains are mixed, they match the above pattern. */
1190 if (complex_numbers)
1192 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1194 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, stmt)
1200 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != first)
1202 if (complex_numbers != 2)
1210 other_complex_node = SLP_INSTANCE_LOADS (slp_instn)[k];
1212 SLP_TREE_SCALAR_STMTS (other_complex_node)[0];
1214 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
1215 != other_node_first)
1223 /* We checked that this case ok, so there is no need to proceed with
1224 permutation tests. */
1225 if (complex_numbers == 2
1226 && SLP_INSTANCE_LOADS (slp_instn).length () == 2)
1228 SLP_INSTANCE_LOADS (slp_instn).release ();
1229 SLP_INSTANCE_LOAD_PERMUTATION (slp_instn).release ();
1233 node = SLP_INSTANCE_TREE (slp_instn);
1234 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1235 /* LOAD_PERMUTATION is a list of indices of all the loads of the SLP
1236 instance, not all the loads belong to the same node or interleaving
1237 group. Hence, we need to divide them into groups according to
1239 number_of_groups = load_permutation.length () / group_size;
1241 /* Reduction (there are no data-refs in the root).
1242 In reduction chain the order of the loads is important. */
1243 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1244 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1246 int first_group_load_index;
1248 /* Compare all the permutation sequences to the first one. */
1249 for (i = 1; i < number_of_groups; i++)
1252 for (j = i * group_size; j < i * group_size + group_size; j++)
1254 next = load_permutation[j];
1255 first_group_load_index = load_permutation[k];
1257 if (next != first_group_load_index)
1259 bad_permutation = true;
1266 if (bad_permutation)
1270 if (!bad_permutation)
1272 /* Check that the loads in the first sequence are different and there
1273 are no gaps between them. */
1274 load_index = sbitmap_alloc (group_size);
1275 bitmap_clear (load_index);
1276 for (k = 0; k < group_size; k++)
1278 first_group_load_index = load_permutation[k];
1279 if (bitmap_bit_p (load_index, first_group_load_index))
1281 bad_permutation = true;
1285 bitmap_set_bit (load_index, first_group_load_index);
1288 if (!bad_permutation)
1289 for (k = 0; k < group_size; k++)
1290 if (!bitmap_bit_p (load_index, k))
1292 bad_permutation = true;
1296 sbitmap_free (load_index);
1299 if (!bad_permutation)
1301 /* This permutation is valid for reduction. Since the order of the
1302 statements in the nodes is not important unless they are memory
1303 accesses, we can rearrange the statements in all the nodes
1304 according to the order of the loads. */
1305 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1307 SLP_INSTANCE_LOAD_PERMUTATION (slp_instn).release ();
1312 /* In basic block vectorization we allow any subchain of an interleaving
1314 FORNOW: not supported in loop SLP because of realignment compications. */
1315 bb_vinfo = STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt));
1316 bad_permutation = false;
1317 /* Check that for every node in the instance the loads form a subchain. */
1320 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1324 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1327 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (load));
1329 != GROUP_FIRST_ELEMENT (vinfo_for_stmt (load)))
1331 bad_permutation = true;
1335 if (j != 0 && next_load != load)
1337 bad_permutation = true;
1341 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1344 if (bad_permutation)
1348 /* Check that the alignment of the first load in every subchain, i.e.,
1349 the first statement in every load node, is supported. */
1350 if (!bad_permutation)
1352 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1354 first_load = SLP_TREE_SCALAR_STMTS (node)[0];
1356 != GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_load)))
1358 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_load));
1359 if (vect_supportable_dr_alignment (dr, false)
1360 == dr_unaligned_unsupported)
1362 if (dump_enabled_p ())
1364 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1366 "unsupported unaligned load ");
1367 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1370 bad_permutation = true;
1376 if (!bad_permutation)
1378 SLP_INSTANCE_LOAD_PERMUTATION (slp_instn).release ();
1384 /* FORNOW: the only supported permutation is 0..01..1.. of length equal to
1385 GROUP_SIZE and where each sequence of same drs is of GROUP_SIZE length as
1386 well (unless it's reduction). */
1387 if (load_permutation.length ()
1388 != (unsigned int) (group_size * group_size))
1392 load_index = sbitmap_alloc (group_size);
1393 bitmap_clear (load_index);
1394 for (j = 0; j < group_size; j++)
1396 for (i = j * group_size, k = 0;
1397 load_permutation.iterate (i, &next) && k < group_size;
1400 if (i != j * group_size && next != prev)
1409 if (bitmap_bit_p (load_index, prev))
1415 bitmap_set_bit (load_index, prev);
1418 for (j = 0; j < group_size; j++)
1419 if (!bitmap_bit_p (load_index, j))
1421 sbitmap_free (load_index);
1425 sbitmap_free (load_index);
1427 if (supported && i == group_size * group_size
1428 && vect_supported_slp_permutation_p (slp_instn))
1435 /* Find the first load in the loop that belongs to INSTANCE.
1436 When loads are in several SLP nodes, there can be a case in which the first
1437 load does not appear in the first SLP node to be transformed, causing
1438 incorrect order of statements. Since we generate all the loads together,
1439 they must be inserted before the first load of the SLP instance and not
1440 before the first load of the first node of the instance. */
1443 vect_find_first_load_in_slp_instance (slp_instance instance)
1447 gimple first_load = NULL, load;
1449 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, load_node)
1450 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
1451 first_load = get_earlier_stmt (load, first_load);
1457 /* Find the last store in SLP INSTANCE. */
1460 vect_find_last_store_in_slp_instance (slp_instance instance)
1464 gimple last_store = NULL, store;
1466 node = SLP_INSTANCE_TREE (instance);
1467 for (i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &store); i++)
1468 last_store = get_later_stmt (store, last_store);
1473 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1476 vect_analyze_slp_cost_1 (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1477 slp_instance instance, slp_tree node,
1478 stmt_vector_for_cost *prologue_cost_vec,
1479 unsigned ncopies_for_cost)
1481 stmt_vector_for_cost *body_cost_vec = &SLP_INSTANCE_BODY_COST_VEC (instance);
1486 stmt_vec_info stmt_info;
1488 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1490 /* Recurse down the SLP tree. */
1491 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1492 vect_analyze_slp_cost_1 (loop_vinfo, bb_vinfo,
1493 instance, child, prologue_cost_vec,
1496 /* Look at the first scalar stmt to determine the cost. */
1497 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1498 stmt_info = vinfo_for_stmt (stmt);
1499 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1501 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1502 vect_model_store_cost (stmt_info, ncopies_for_cost, false,
1503 vect_uninitialized_def,
1504 node, prologue_cost_vec, body_cost_vec);
1508 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1509 vect_model_load_cost (stmt_info, ncopies_for_cost, false,
1510 node, prologue_cost_vec, body_cost_vec);
1511 /* If the load is permuted record the cost for the permutation.
1512 ??? Loads from multiple chains are let through here only
1513 for a single special case involving complex numbers where
1514 in the end no permutation is necessary. */
1515 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, s)
1516 if ((STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo_for_stmt (s))
1517 == STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info))
1518 && vect_get_place_in_interleaving_chain
1519 (s, STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info)) != i)
1521 record_stmt_cost (body_cost_vec, group_size, vec_perm,
1522 stmt_info, 0, vect_body);
1528 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1529 stmt_info, 0, vect_body);
1531 /* Scan operands and account for prologue cost of constants/externals.
1532 ??? This over-estimates cost for multiple uses and should be
1534 lhs = gimple_get_lhs (stmt);
1535 for (i = 0; i < gimple_num_ops (stmt); ++i)
1537 tree def, op = gimple_op (stmt, i);
1539 enum vect_def_type dt;
1540 if (!op || op == lhs)
1542 if (vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo,
1543 &def_stmt, &def, &dt)
1544 && (dt == vect_constant_def || dt == vect_external_def))
1545 record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
1546 stmt_info, 0, vect_prologue);
1550 /* Compute the cost for the SLP instance INSTANCE. */
1553 vect_analyze_slp_cost (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1554 slp_instance instance, unsigned nunits)
1556 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1557 unsigned ncopies_for_cost;
1558 stmt_info_for_cost *si;
1561 /* Calculate the number of vector stmts to create based on the unrolling
1562 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1563 GROUP_SIZE / NUNITS otherwise. */
1564 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1565 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1567 prologue_cost_vec.create (10);
1568 body_cost_vec.create (10);
1569 SLP_INSTANCE_BODY_COST_VEC (instance) = body_cost_vec;
1570 vect_analyze_slp_cost_1 (loop_vinfo, bb_vinfo,
1571 instance, SLP_INSTANCE_TREE (instance),
1572 &prologue_cost_vec, ncopies_for_cost);
1574 /* Record the prologue costs, which were delayed until we were
1575 sure that SLP was successful. Unlike the body costs, we know
1576 the final values now regardless of the loop vectorization factor. */
1577 void *data = (loop_vinfo ? LOOP_VINFO_TARGET_COST_DATA (loop_vinfo)
1578 : BB_VINFO_TARGET_COST_DATA (bb_vinfo));
1579 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1581 struct _stmt_vec_info *stmt_info
1582 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1583 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1584 si->misalign, vect_prologue);
1587 prologue_cost_vec.release ();
1590 /* Analyze an SLP instance starting from a group of grouped stores. Call
1591 vect_build_slp_tree to build a tree of packed stmts if possible.
1592 Return FALSE if it's impossible to SLP any stmt in the loop. */
1595 vect_analyze_slp_instance (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1598 slp_instance new_instance;
1600 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1601 unsigned int unrolling_factor = 1, nunits;
1602 tree vectype, scalar_type = NULL_TREE;
1604 unsigned int vectorization_factor = 0;
1606 unsigned int max_nunits = 0;
1607 vec<int> load_permutation;
1608 vec<slp_tree> loads;
1609 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1610 bool loads_permuted = false;
1611 vec<gimple> scalar_stmts;
1613 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1617 scalar_type = TREE_TYPE (DR_REF (dr));
1618 vectype = get_vectype_for_scalar_type (scalar_type);
1622 gcc_assert (loop_vinfo);
1623 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1626 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1630 gcc_assert (loop_vinfo);
1631 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1632 group_size = LOOP_VINFO_REDUCTIONS (loop_vinfo).length ();
1637 if (dump_enabled_p ())
1639 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1640 "Build SLP failed: unsupported data-type ");
1641 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1647 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1649 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1651 vectorization_factor = nunits;
1653 /* Calculate the unrolling factor. */
1654 unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
1655 if (unrolling_factor != 1 && !loop_vinfo)
1657 if (dump_enabled_p ())
1658 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1659 "Build SLP failed: unrolling required in basic"
1665 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1666 scalar_stmts.create (group_size);
1668 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1670 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1673 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1674 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1675 scalar_stmts.safe_push (
1676 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1678 scalar_stmts.safe_push (next);
1679 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1684 /* Collect reduction statements. */
1685 vec<gimple> reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1686 for (i = 0; reductions.iterate (i, &next); i++)
1687 scalar_stmts.safe_push (next);
1690 node = vect_create_new_slp_node (scalar_stmts);
1692 load_permutation.create (group_size * group_size);
1693 loads.create (group_size);
1695 /* Build the tree for the SLP instance. */
1696 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &node, group_size,
1697 &max_nunits, &load_permutation, &loads,
1698 vectorization_factor, &loads_permuted))
1700 /* Calculate the unrolling factor based on the smallest type. */
1701 if (max_nunits > nunits)
1702 unrolling_factor = least_common_multiple (max_nunits, group_size)
1705 if (unrolling_factor != 1 && !loop_vinfo)
1707 if (dump_enabled_p ())
1708 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1709 "Build SLP failed: unrolling required in basic"
1711 vect_free_slp_tree (node);
1712 load_permutation.release ();
1717 /* Create a new SLP instance. */
1718 new_instance = XNEW (struct _slp_instance);
1719 SLP_INSTANCE_TREE (new_instance) = node;
1720 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1721 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1722 SLP_INSTANCE_BODY_COST_VEC (new_instance) = vNULL;
1723 SLP_INSTANCE_LOADS (new_instance) = loads;
1724 SLP_INSTANCE_FIRST_LOAD_STMT (new_instance) = NULL;
1725 SLP_INSTANCE_LOAD_PERMUTATION (new_instance) = load_permutation;
1729 if (!vect_supported_load_permutation_p (new_instance, group_size,
1732 if (dump_enabled_p ())
1734 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1735 "Build SLP failed: unsupported load "
1737 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
1740 vect_free_slp_instance (new_instance);
1744 SLP_INSTANCE_FIRST_LOAD_STMT (new_instance)
1745 = vect_find_first_load_in_slp_instance (new_instance);
1748 SLP_INSTANCE_LOAD_PERMUTATION (new_instance).release ();
1750 /* Compute the costs of this SLP instance. */
1751 vect_analyze_slp_cost (loop_vinfo, bb_vinfo,
1752 new_instance, TYPE_VECTOR_SUBPARTS (vectype));
1755 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).safe_push (new_instance);
1757 BB_VINFO_SLP_INSTANCES (bb_vinfo).safe_push (new_instance);
1759 if (dump_enabled_p ())
1760 vect_print_slp_tree (MSG_NOTE, node);
1765 /* Failed to SLP. */
1766 /* Free the allocated memory. */
1767 vect_free_slp_tree (node);
1768 load_permutation.release ();
1775 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
1776 trees of packed scalar stmts if SLP is possible. */
1779 vect_analyze_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
1782 vec<gimple> grouped_stores;
1783 vec<gimple> reductions = vNULL;
1784 vec<gimple> reduc_chains = vNULL;
1785 gimple first_element;
1788 if (dump_enabled_p ())
1789 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===");
1793 grouped_stores = LOOP_VINFO_GROUPED_STORES (loop_vinfo);
1794 reduc_chains = LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo);
1795 reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1798 grouped_stores = BB_VINFO_GROUPED_STORES (bb_vinfo);
1800 /* Find SLP sequences starting from groups of grouped stores. */
1801 FOR_EACH_VEC_ELT (grouped_stores, i, first_element)
1802 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element))
1805 if (bb_vinfo && !ok)
1807 if (dump_enabled_p ())
1808 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1809 "Failed to SLP the basic block.");
1815 && LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).length () > 0)
1817 /* Find SLP sequences starting from reduction chains. */
1818 FOR_EACH_VEC_ELT (reduc_chains, i, first_element)
1819 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element))
1824 /* Don't try to vectorize SLP reductions if reduction chain was
1829 /* Find SLP sequences starting from groups of reductions. */
1830 if (loop_vinfo && LOOP_VINFO_REDUCTIONS (loop_vinfo).length () > 1
1831 && vect_analyze_slp_instance (loop_vinfo, bb_vinfo, reductions[0]))
1838 /* For each possible SLP instance decide whether to SLP it and calculate overall
1839 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
1840 least one instance. */
1843 vect_make_slp_decision (loop_vec_info loop_vinfo)
1845 unsigned int i, unrolling_factor = 1;
1846 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1847 slp_instance instance;
1848 int decided_to_slp = 0;
1850 if (dump_enabled_p ())
1851 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ===");
1853 FOR_EACH_VEC_ELT (slp_instances, i, instance)
1855 /* FORNOW: SLP if you can. */
1856 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
1857 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
1859 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
1860 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
1861 loop-based vectorization. Such stmts will be marked as HYBRID. */
1862 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
1866 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
1868 if (decided_to_slp && dump_enabled_p ())
1869 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1870 "Decided to SLP %d instances. Unrolling factor %d",
1871 decided_to_slp, unrolling_factor);
1873 return (decided_to_slp > 0);
1877 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
1878 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
1881 vect_detect_hybrid_slp_stmts (slp_tree node)
1884 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (node);
1885 gimple stmt = stmts[0];
1886 imm_use_iterator imm_iter;
1888 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1890 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1891 struct loop *loop = NULL;
1892 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1893 basic_block bb = NULL;
1899 loop = LOOP_VINFO_LOOP (loop_vinfo);
1901 bb = BB_VINFO_BB (bb_vinfo);
1903 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1904 if (PURE_SLP_STMT (vinfo_for_stmt (stmt))
1905 && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
1906 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
1907 if (gimple_bb (use_stmt)
1908 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
1909 || bb == gimple_bb (use_stmt))
1910 && (stmt_vinfo = vinfo_for_stmt (use_stmt))
1911 && !STMT_SLP_TYPE (stmt_vinfo)
1912 && (STMT_VINFO_RELEVANT (stmt_vinfo)
1913 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_vinfo)))
1914 && !(gimple_code (use_stmt) == GIMPLE_PHI
1915 && STMT_VINFO_DEF_TYPE (stmt_vinfo)
1916 == vect_reduction_def))
1917 vect_mark_slp_stmts (node, hybrid, i);
1919 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1920 vect_detect_hybrid_slp_stmts (child);
1924 /* Find stmts that must be both vectorized and SLPed. */
1927 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
1930 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1931 slp_instance instance;
1933 if (dump_enabled_p ())
1934 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ===");
1936 FOR_EACH_VEC_ELT (slp_instances, i, instance)
1937 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance));
1941 /* Create and initialize a new bb_vec_info struct for BB, as well as
1942 stmt_vec_info structs for all the stmts in it. */
1945 new_bb_vec_info (basic_block bb)
1947 bb_vec_info res = NULL;
1948 gimple_stmt_iterator gsi;
1950 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
1951 BB_VINFO_BB (res) = bb;
1953 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1955 gimple stmt = gsi_stmt (gsi);
1956 gimple_set_uid (stmt, 0);
1957 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, NULL, res));
1960 BB_VINFO_GROUPED_STORES (res).create (10);
1961 BB_VINFO_SLP_INSTANCES (res).create (2);
1962 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
1969 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
1970 stmts in the basic block. */
1973 destroy_bb_vec_info (bb_vec_info bb_vinfo)
1975 vec<slp_instance> slp_instances;
1976 slp_instance instance;
1978 gimple_stmt_iterator si;
1984 bb = BB_VINFO_BB (bb_vinfo);
1986 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1988 gimple stmt = gsi_stmt (si);
1989 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1992 /* Free stmt_vec_info. */
1993 free_stmt_vec_info (stmt);
1996 free_data_refs (BB_VINFO_DATAREFS (bb_vinfo));
1997 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
1998 BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
1999 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2000 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2001 vect_free_slp_instance (instance);
2002 BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
2003 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
2009 /* Analyze statements contained in SLP tree node after recursively analyzing
2010 the subtree. Return TRUE if the operations are supported. */
2013 vect_slp_analyze_node_operations (bb_vec_info bb_vinfo, slp_tree node)
2023 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2024 if (!vect_slp_analyze_node_operations (bb_vinfo, child))
2027 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2029 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2030 gcc_assert (stmt_info);
2031 gcc_assert (PURE_SLP_STMT (stmt_info));
2033 if (!vect_analyze_stmt (stmt, &dummy, node))
2041 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2042 operations are supported. */
2045 vect_slp_analyze_operations (bb_vec_info bb_vinfo)
2047 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2048 slp_instance instance;
2051 for (i = 0; slp_instances.iterate (i, &instance); )
2053 if (!vect_slp_analyze_node_operations (bb_vinfo,
2054 SLP_INSTANCE_TREE (instance)))
2056 vect_free_slp_instance (instance);
2057 slp_instances.ordered_remove (i);
2063 if (!slp_instances.length ())
2069 /* Check if vectorization of the basic block is profitable. */
2072 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2074 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2075 slp_instance instance;
2077 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2078 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2079 unsigned int stmt_cost;
2081 gimple_stmt_iterator si;
2082 basic_block bb = BB_VINFO_BB (bb_vinfo);
2083 void *target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
2084 stmt_vec_info stmt_info = NULL;
2085 stmt_vector_for_cost body_cost_vec;
2086 stmt_info_for_cost *ci;
2088 /* Calculate vector costs. */
2089 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2091 body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
2093 FOR_EACH_VEC_ELT (body_cost_vec, j, ci)
2095 stmt_info = ci->stmt ? vinfo_for_stmt (ci->stmt) : NULL;
2096 (void) add_stmt_cost (target_cost_data, ci->count, ci->kind,
2097 stmt_info, ci->misalign, vect_body);
2101 /* Calculate scalar cost. */
2102 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2104 stmt = gsi_stmt (si);
2105 stmt_info = vinfo_for_stmt (stmt);
2107 if (!stmt_info || !STMT_VINFO_VECTORIZABLE (stmt_info)
2108 || !PURE_SLP_STMT (stmt_info))
2111 if (STMT_VINFO_DATA_REF (stmt_info))
2113 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2114 stmt_cost = vect_get_stmt_cost (scalar_load);
2116 stmt_cost = vect_get_stmt_cost (scalar_store);
2119 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2121 scalar_cost += stmt_cost;
2124 /* Complete the target-specific cost calculation. */
2125 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2126 &vec_inside_cost, &vec_epilogue_cost);
2128 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2130 if (dump_enabled_p ())
2132 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2133 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2135 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2136 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2137 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d", scalar_cost);
2140 /* Vectorization is profitable if its cost is less than the cost of scalar
2142 if (vec_outside_cost + vec_inside_cost >= scalar_cost)
2148 /* Check if the basic block can be vectorized. */
2151 vect_slp_analyze_bb_1 (basic_block bb)
2153 bb_vec_info bb_vinfo;
2154 vec<slp_instance> slp_instances;
2155 slp_instance instance;
2159 bb_vinfo = new_bb_vec_info (bb);
2163 if (!vect_analyze_data_refs (NULL, bb_vinfo, &min_vf))
2165 if (dump_enabled_p ())
2166 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2167 "not vectorized: unhandled data-ref in basic "
2170 destroy_bb_vec_info (bb_vinfo);
2174 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2176 if (dump_enabled_p ())
2177 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2178 "not vectorized: not enough data-refs in "
2181 destroy_bb_vec_info (bb_vinfo);
2185 if (!vect_analyze_data_ref_accesses (NULL, bb_vinfo))
2187 if (dump_enabled_p ())
2188 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2189 "not vectorized: unhandled data access in "
2192 destroy_bb_vec_info (bb_vinfo);
2196 vect_pattern_recog (NULL, bb_vinfo);
2198 if (!vect_slp_analyze_data_ref_dependences (bb_vinfo))
2200 if (dump_enabled_p ())
2201 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2202 "not vectorized: unhandled data dependence "
2203 "in basic block.\n");
2205 destroy_bb_vec_info (bb_vinfo);
2209 if (!vect_analyze_data_refs_alignment (NULL, bb_vinfo))
2211 if (dump_enabled_p ())
2212 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2213 "not vectorized: bad data alignment in basic "
2216 destroy_bb_vec_info (bb_vinfo);
2220 /* Check the SLP opportunities in the basic block, analyze and build SLP
2222 if (!vect_analyze_slp (NULL, bb_vinfo))
2224 if (dump_enabled_p ())
2225 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2226 "not vectorized: failed to find SLP opportunities "
2227 "in basic block.\n");
2229 destroy_bb_vec_info (bb_vinfo);
2233 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2235 /* Mark all the statements that we want to vectorize as pure SLP and
2237 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2239 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2240 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2243 if (!vect_verify_datarefs_alignment (NULL, bb_vinfo))
2245 if (dump_enabled_p ())
2246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2247 "not vectorized: unsupported alignment in basic "
2249 destroy_bb_vec_info (bb_vinfo);
2253 if (!vect_slp_analyze_operations (bb_vinfo))
2255 if (dump_enabled_p ())
2256 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2257 "not vectorized: bad operation in basic block.\n");
2259 destroy_bb_vec_info (bb_vinfo);
2263 /* Cost model: check if the vectorization is worthwhile. */
2264 if (flag_vect_cost_model
2265 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2267 if (dump_enabled_p ())
2268 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2269 "not vectorized: vectorization is not "
2272 destroy_bb_vec_info (bb_vinfo);
2276 if (dump_enabled_p ())
2277 dump_printf_loc (MSG_NOTE, vect_location,
2278 "Basic block will be vectorized using SLP\n");
2285 vect_slp_analyze_bb (basic_block bb)
2287 bb_vec_info bb_vinfo;
2289 gimple_stmt_iterator gsi;
2290 unsigned int vector_sizes;
2292 if (dump_enabled_p ())
2293 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2295 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2297 gimple stmt = gsi_stmt (gsi);
2298 if (!is_gimple_debug (stmt)
2299 && !gimple_nop_p (stmt)
2300 && gimple_code (stmt) != GIMPLE_LABEL)
2304 if (insns > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2306 if (dump_enabled_p ())
2307 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2308 "not vectorized: too many instructions in "
2314 /* Autodetect first vector size we try. */
2315 current_vector_size = 0;
2316 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2320 bb_vinfo = vect_slp_analyze_bb_1 (bb);
2324 destroy_bb_vec_info (bb_vinfo);
2326 vector_sizes &= ~current_vector_size;
2327 if (vector_sizes == 0
2328 || current_vector_size == 0)
2331 /* Try the next biggest vector size. */
2332 current_vector_size = 1 << floor_log2 (vector_sizes);
2333 if (dump_enabled_p ())
2334 dump_printf_loc (MSG_NOTE, vect_location,
2335 "***** Re-trying analysis with "
2336 "vector size %d\n", current_vector_size);
2341 /* SLP costs are calculated according to SLP instance unrolling factor (i.e.,
2342 the number of created vector stmts depends on the unrolling factor).
2343 However, the actual number of vector stmts for every SLP node depends on
2344 VF which is set later in vect_analyze_operations (). Hence, SLP costs
2345 should be updated. In this function we assume that the inside costs
2346 calculated in vect_model_xxx_cost are linear in ncopies. */
2349 vect_update_slp_costs_according_to_vf (loop_vec_info loop_vinfo)
2351 unsigned int i, j, vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2352 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2353 slp_instance instance;
2354 stmt_vector_for_cost body_cost_vec;
2355 stmt_info_for_cost *si;
2356 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2358 if (dump_enabled_p ())
2359 dump_printf_loc (MSG_NOTE, vect_location,
2360 "=== vect_update_slp_costs_according_to_vf ===");
2362 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2364 /* We assume that costs are linear in ncopies. */
2365 int ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (instance);
2367 /* Record the instance's instructions in the target cost model.
2368 This was delayed until here because the count of instructions
2369 isn't known beforehand. */
2370 body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
2372 FOR_EACH_VEC_ELT (body_cost_vec, j, si)
2373 (void) add_stmt_cost (data, si->count * ncopies, si->kind,
2374 vinfo_for_stmt (si->stmt), si->misalign,
2380 /* For constant and loop invariant defs of SLP_NODE this function returns
2381 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2382 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2383 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2384 REDUC_INDEX is the index of the reduction operand in the statements, unless
2388 vect_get_constant_vectors (tree op, slp_tree slp_node,
2389 vec<tree> *vec_oprnds,
2390 unsigned int op_num, unsigned int number_of_vectors,
2393 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2394 gimple stmt = stmts[0];
2395 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2399 unsigned j, number_of_places_left_in_vector;
2402 int group_size = stmts.length ();
2403 unsigned int vec_num, i;
2404 unsigned number_of_copies = 1;
2406 voprnds.create (number_of_vectors);
2407 bool constant_p, is_store;
2408 tree neutral_op = NULL;
2409 enum tree_code code = gimple_expr_code (stmt);
2412 gimple_seq ctor_seq = NULL;
2414 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
2415 && reduc_index != -1)
2417 op_num = reduc_index - 1;
2418 op = gimple_op (stmt, reduc_index);
2419 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
2420 we need either neutral operands or the original operands. See
2421 get_initial_def_for_reduction() for details. */
2424 case WIDEN_SUM_EXPR:
2430 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2431 neutral_op = build_real (TREE_TYPE (op), dconst0);
2433 neutral_op = build_int_cst (TREE_TYPE (op), 0);
2438 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2439 neutral_op = build_real (TREE_TYPE (op), dconst1);
2441 neutral_op = build_int_cst (TREE_TYPE (op), 1);
2446 neutral_op = build_int_cst (TREE_TYPE (op), -1);
2451 def_stmt = SSA_NAME_DEF_STMT (op);
2452 loop = (gimple_bb (stmt))->loop_father;
2453 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2454 loop_preheader_edge (loop));
2462 if (STMT_VINFO_DATA_REF (stmt_vinfo))
2465 op = gimple_assign_rhs1 (stmt);
2472 if (CONSTANT_CLASS_P (op))
2477 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
2478 gcc_assert (vector_type);
2479 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
2481 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
2482 created vectors. It is greater than 1 if unrolling is performed.
2484 For example, we have two scalar operands, s1 and s2 (e.g., group of
2485 strided accesses of size two), while NUNITS is four (i.e., four scalars
2486 of this type can be packed in a vector). The output vector will contain
2487 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
2490 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
2491 containing the operands.
2493 For example, NUNITS is four as before, and the group size is 8
2494 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
2495 {s5, s6, s7, s8}. */
2497 number_of_copies = least_common_multiple (nunits, group_size) / group_size;
2499 number_of_places_left_in_vector = nunits;
2500 elts = XALLOCAVEC (tree, nunits);
2501 for (j = 0; j < number_of_copies; j++)
2503 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
2506 op = gimple_assign_rhs1 (stmt);
2512 if (op_num == 0 || op_num == 1)
2514 tree cond = gimple_assign_rhs1 (stmt);
2515 op = TREE_OPERAND (cond, op_num);
2520 op = gimple_assign_rhs2 (stmt);
2522 op = gimple_assign_rhs3 (stmt);
2527 op = gimple_call_arg (stmt, op_num);
2534 op = gimple_op (stmt, op_num + 1);
2535 /* Unlike the other binary operators, shifts/rotates have
2536 the shift count being int, instead of the same type as
2537 the lhs, so make sure the scalar is the right type if
2538 we are dealing with vectors of
2539 long long/long/short/char. */
2540 if (op_num == 1 && constant_p)
2541 op = fold_convert (TREE_TYPE (vector_type), op);
2545 op = gimple_op (stmt, op_num + 1);
2550 if (reduc_index != -1)
2552 loop = (gimple_bb (stmt))->loop_father;
2553 def_stmt = SSA_NAME_DEF_STMT (op);
2557 /* Get the def before the loop. In reduction chain we have only
2558 one initial value. */
2559 if ((j != (number_of_copies - 1)
2560 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2565 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2566 loop_preheader_edge (loop));
2569 /* Create 'vect_ = {op0,op1,...,opn}'. */
2570 number_of_places_left_in_vector--;
2571 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
2575 op = fold_unary (VIEW_CONVERT_EXPR,
2576 TREE_TYPE (vector_type), op);
2577 gcc_assert (op && CONSTANT_CLASS_P (op));
2582 = make_ssa_name (TREE_TYPE (vector_type), NULL);
2584 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
2587 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR,
2588 new_temp, op, NULL_TREE);
2589 gimple_seq_add_stmt (&ctor_seq, init_stmt);
2593 elts[number_of_places_left_in_vector] = op;
2595 if (number_of_places_left_in_vector == 0)
2597 number_of_places_left_in_vector = nunits;
2600 vec_cst = build_vector (vector_type, elts);
2603 vec<constructor_elt, va_gc> *v;
2605 vec_alloc (v, nunits);
2606 for (k = 0; k < nunits; ++k)
2607 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
2608 vec_cst = build_constructor (vector_type, v);
2610 voprnds.quick_push (vect_init_vector (stmt, vec_cst,
2611 vector_type, NULL));
2612 if (ctor_seq != NULL)
2614 gimple init_stmt = SSA_NAME_DEF_STMT (voprnds.last ());
2615 gimple_stmt_iterator gsi = gsi_for_stmt (init_stmt);
2616 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
2624 /* Since the vectors are created in the reverse order, we should invert
2626 vec_num = voprnds.length ();
2627 for (j = vec_num; j != 0; j--)
2629 vop = voprnds[j - 1];
2630 vec_oprnds->quick_push (vop);
2635 /* In case that VF is greater than the unrolling factor needed for the SLP
2636 group of stmts, NUMBER_OF_VECTORS to be created is greater than
2637 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
2638 to replicate the vectors. */
2639 while (number_of_vectors > vec_oprnds->length ())
2641 tree neutral_vec = NULL;
2646 neutral_vec = build_vector_from_val (vector_type, neutral_op);
2648 vec_oprnds->quick_push (neutral_vec);
2652 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
2653 vec_oprnds->quick_push (vop);
2659 /* Get vectorized definitions from SLP_NODE that contains corresponding
2660 vectorized def-stmts. */
2663 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
2666 gimple vec_def_stmt;
2669 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
2671 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
2673 gcc_assert (vec_def_stmt);
2674 vec_oprnd = gimple_get_lhs (vec_def_stmt);
2675 vec_oprnds->quick_push (vec_oprnd);
2680 /* Get vectorized definitions for SLP_NODE.
2681 If the scalar definitions are loop invariants or constants, collect them and
2682 call vect_get_constant_vectors() to create vector stmts.
2683 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
2684 must be stored in the corresponding child of SLP_NODE, and we call
2685 vect_get_slp_vect_defs () to retrieve them. */
2688 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
2689 vec<vec<tree> > *vec_oprnds, int reduc_index)
2692 int number_of_vects = 0, i;
2693 unsigned int child_index = 0;
2694 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
2695 slp_tree child = NULL;
2698 bool vectorized_defs;
2700 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2701 FOR_EACH_VEC_ELT (ops, i, oprnd)
2703 /* For each operand we check if it has vectorized definitions in a child
2704 node or we need to create them (for invariants and constants). We
2705 check if the LHS of the first stmt of the next child matches OPRND.
2706 If it does, we found the correct child. Otherwise, we call
2707 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
2708 to check this child node for the next operand. */
2709 vectorized_defs = false;
2710 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
2712 child = (slp_tree) SLP_TREE_CHILDREN (slp_node)[child_index];
2714 /* We have to check both pattern and original def, if available. */
2715 gimple first_def = SLP_TREE_SCALAR_STMTS (child)[0];
2716 gimple related = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
2718 if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0)
2720 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
2722 /* The number of vector defs is determined by the number of
2723 vector statements in the node from which we get those
2725 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
2726 vectorized_defs = true;
2731 if (!vectorized_defs)
2735 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
2736 /* Number of vector stmts was calculated according to LHS in
2737 vect_schedule_slp_instance (), fix it by replacing LHS with
2738 RHS, if necessary. See vect_get_smallest_scalar_type () for
2740 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
2742 if (rhs_size_unit != lhs_size_unit)
2744 number_of_vects *= rhs_size_unit;
2745 number_of_vects /= lhs_size_unit;
2750 /* Allocate memory for vectorized defs. */
2752 vec_defs.create (number_of_vects);
2754 /* For reduction defs we call vect_get_constant_vectors (), since we are
2755 looking for initial loop invariant values. */
2756 if (vectorized_defs && reduc_index == -1)
2757 /* The defs are already vectorized. */
2758 vect_get_slp_vect_defs (child, &vec_defs);
2760 /* Build vectors from scalar defs. */
2761 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
2762 number_of_vects, reduc_index);
2764 vec_oprnds->quick_push (vec_defs);
2766 /* For reductions, we only need initial values. */
2767 if (reduc_index != -1)
2773 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
2774 building a vector of type MASK_TYPE from it) and two input vectors placed in
2775 DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
2776 shifting by STRIDE elements of DR_CHAIN for every copy.
2777 (STRIDE is the number of vectorized stmts for NODE divided by the number of
2779 VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
2780 the created stmts must be inserted. */
2783 vect_create_mask_and_perm (gimple stmt, gimple next_scalar_stmt,
2784 tree mask, int first_vec_indx, int second_vec_indx,
2785 gimple_stmt_iterator *gsi, slp_tree node,
2786 tree vectype, vec<tree> dr_chain,
2787 int ncopies, int vect_stmts_counter)
2790 gimple perm_stmt = NULL;
2791 stmt_vec_info next_stmt_info;
2793 tree first_vec, second_vec, data_ref;
2795 stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
2797 /* Initialize the vect stmts of NODE to properly insert the generated
2799 for (i = SLP_TREE_VEC_STMTS (node).length ();
2800 i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
2801 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
2803 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
2804 for (i = 0; i < ncopies; i++)
2806 first_vec = dr_chain[first_vec_indx];
2807 second_vec = dr_chain[second_vec_indx];
2809 /* Generate the permute statement. */
2810 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, perm_dest,
2811 first_vec, second_vec, mask);
2812 data_ref = make_ssa_name (perm_dest, perm_stmt);
2813 gimple_set_lhs (perm_stmt, data_ref);
2814 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
2816 /* Store the vector statement in NODE. */
2817 SLP_TREE_VEC_STMTS (node)[stride * i + vect_stmts_counter] = perm_stmt;
2819 first_vec_indx += stride;
2820 second_vec_indx += stride;
2823 /* Mark the scalar stmt as vectorized. */
2824 next_stmt_info = vinfo_for_stmt (next_scalar_stmt);
2825 STMT_VINFO_VEC_STMT (next_stmt_info) = perm_stmt;
2829 /* Given FIRST_MASK_ELEMENT - the mask element in element representation,
2830 return in CURRENT_MASK_ELEMENT its equivalent in target specific
2831 representation. Check that the mask is valid and return FALSE if not.
2832 Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
2833 the next vector, i.e., the current first vector is not needed. */
2836 vect_get_mask_element (gimple stmt, int first_mask_element, int m,
2837 int mask_nunits, bool only_one_vec, int index,
2838 unsigned char *mask, int *current_mask_element,
2839 bool *need_next_vector, int *number_of_mask_fixes,
2840 bool *mask_fixed, bool *needs_first_vector)
2844 /* Convert to target specific representation. */
2845 *current_mask_element = first_mask_element + m;
2846 /* Adjust the value in case it's a mask for second and third vectors. */
2847 *current_mask_element -= mask_nunits * (*number_of_mask_fixes - 1);
2849 if (*current_mask_element < mask_nunits)
2850 *needs_first_vector = true;
2852 /* We have only one input vector to permute but the mask accesses values in
2853 the next vector as well. */
2854 if (only_one_vec && *current_mask_element >= mask_nunits)
2856 if (dump_enabled_p ())
2858 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2859 "permutation requires at least two vectors ");
2860 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2866 /* The mask requires the next vector. */
2867 if (*current_mask_element >= mask_nunits * 2)
2869 if (*needs_first_vector || *mask_fixed)
2871 /* We either need the first vector too or have already moved to the
2872 next vector. In both cases, this permutation needs three
2874 if (dump_enabled_p ())
2876 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2877 "permutation requires at "
2878 "least three vectors ");
2879 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2885 /* We move to the next vector, dropping the first one and working with
2886 the second and the third - we need to adjust the values of the mask
2888 *current_mask_element -= mask_nunits * *number_of_mask_fixes;
2890 for (i = 0; i < index; i++)
2891 mask[i] -= mask_nunits * *number_of_mask_fixes;
2893 (*number_of_mask_fixes)++;
2897 *need_next_vector = *mask_fixed;
2899 /* This was the last element of this mask. Start a new one. */
2900 if (index == mask_nunits - 1)
2902 *number_of_mask_fixes = 1;
2903 *mask_fixed = false;
2904 *needs_first_vector = false;
2911 /* Generate vector permute statements from a list of loads in DR_CHAIN.
2912 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
2913 permute statements for SLP_NODE_INSTANCE. */
2915 vect_transform_slp_perm_load (gimple stmt, vec<tree> dr_chain,
2916 gimple_stmt_iterator *gsi, int vf,
2917 slp_instance slp_node_instance, bool analyze_only)
2919 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2920 tree mask_element_type = NULL_TREE, mask_type;
2921 int i, j, k, nunits, vec_index = 0, scalar_index;
2923 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2924 gimple next_scalar_stmt;
2925 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
2926 int first_mask_element;
2927 int index, unroll_factor, current_mask_element, ncopies;
2928 unsigned char *mask;
2929 bool only_one_vec = false, need_next_vector = false;
2930 int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
2931 int number_of_mask_fixes = 1;
2932 bool mask_fixed = false;
2933 bool needs_first_vector = false;
2934 enum machine_mode mode;
2936 mode = TYPE_MODE (vectype);
2938 if (!can_vec_perm_p (mode, false, NULL))
2940 if (dump_enabled_p ())
2942 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2943 "no vect permute for ");
2944 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2949 /* The generic VEC_PERM_EXPR code always uses an integral type of the
2950 same size as the vector element being permuted. */
2951 mask_element_type = lang_hooks.types.type_for_mode
2952 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
2953 mask_type = get_vectype_for_scalar_type (mask_element_type);
2954 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2955 mask = XALLOCAVEC (unsigned char, nunits);
2956 unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
2958 /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
2959 unrolling factor. */
2960 orig_vec_stmts_num = group_size *
2961 SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance) / nunits;
2962 if (orig_vec_stmts_num == 1)
2963 only_one_vec = true;
2965 /* Number of copies is determined by the final vectorization factor
2966 relatively to SLP_NODE_INSTANCE unrolling factor. */
2967 ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
2969 /* Generate permutation masks for every NODE. Number of masks for each NODE
2970 is equal to GROUP_SIZE.
2971 E.g., we have a group of three nodes with three loads from the same
2972 location in each node, and the vector size is 4. I.e., we have a
2973 a0b0c0a1b1c1... sequence and we need to create the following vectors:
2974 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
2975 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
2978 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
2979 The last mask is illegal since we assume two operands for permute
2980 operation, and the mask element values can't be outside that range.
2981 Hence, the last mask must be converted into {2,5,5,5}.
2982 For the first two permutations we need the first and the second input
2983 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
2984 we need the second and the third vectors: {b1,c1,a2,b2} and
2987 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_node_instance), i, node)
2991 vect_stmts_counter = 0;
2993 first_vec_index = vec_index++;
2995 second_vec_index = first_vec_index;
2997 second_vec_index = vec_index++;
2999 for (j = 0; j < unroll_factor; j++)
3001 for (k = 0; k < group_size; k++)
3003 first_mask_element = i + j * group_size;
3004 if (!vect_get_mask_element (stmt, first_mask_element, 0,
3005 nunits, only_one_vec, index,
3006 mask, ¤t_mask_element,
3008 &number_of_mask_fixes, &mask_fixed,
3009 &needs_first_vector))
3011 mask[index++] = current_mask_element;
3013 if (index == nunits)
3015 tree mask_vec, *mask_elts;
3018 if (!can_vec_perm_p (mode, false, mask))
3020 if (dump_enabled_p ())
3022 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3024 "unsupported vect permute { ");
3025 for (i = 0; i < nunits; ++i)
3026 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ",
3028 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3033 mask_elts = XALLOCAVEC (tree, nunits);
3034 for (l = 0; l < nunits; ++l)
3035 mask_elts[l] = build_int_cst (mask_element_type, mask[l]);
3036 mask_vec = build_vector (mask_type, mask_elts);
3041 if (need_next_vector)
3043 first_vec_index = second_vec_index;
3044 second_vec_index = vec_index;
3048 = SLP_TREE_SCALAR_STMTS (node)[scalar_index++];
3050 vect_create_mask_and_perm (stmt, next_scalar_stmt,
3051 mask_vec, first_vec_index, second_vec_index,
3052 gsi, node, vectype, dr_chain,
3053 ncopies, vect_stmts_counter++);
3065 /* Vectorize SLP instance tree in postorder. */
3068 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3069 unsigned int vectorization_factor)
3072 bool grouped_store, is_store;
3073 gimple_stmt_iterator si;
3074 stmt_vec_info stmt_info;
3075 unsigned int vec_stmts_size, nunits, group_size;
3078 slp_tree loads_node;
3084 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3085 vect_schedule_slp_instance (child, instance, vectorization_factor);
3087 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3088 stmt_info = vinfo_for_stmt (stmt);
3090 /* VECTYPE is the type of the destination. */
3091 vectype = STMT_VINFO_VECTYPE (stmt_info);
3092 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3093 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3095 /* For each SLP instance calculate number of vector stmts to be created
3096 for the scalar stmts in each node of the SLP tree. Number of vector
3097 elements in one vector iteration is the number of scalar elements in
3098 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3100 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3102 /* In case of load permutation we have to allocate vectorized statements for
3103 all the nodes that participate in that permutation. */
3104 if (SLP_INSTANCE_LOAD_PERMUTATION (instance).exists ())
3106 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, loads_node)
3108 if (!SLP_TREE_VEC_STMTS (loads_node).exists ())
3110 SLP_TREE_VEC_STMTS (loads_node).create (vec_stmts_size);
3111 SLP_TREE_NUMBER_OF_VEC_STMTS (loads_node) = vec_stmts_size;
3116 if (!SLP_TREE_VEC_STMTS (node).exists ())
3118 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3119 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3122 if (dump_enabled_p ())
3124 dump_printf_loc (MSG_NOTE,vect_location,
3125 "------>vectorizing SLP node starting from: ");
3126 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3129 /* Loads should be inserted before the first load. */
3130 if (SLP_INSTANCE_FIRST_LOAD_STMT (instance)
3131 && STMT_VINFO_GROUPED_ACCESS (stmt_info)
3132 && !REFERENCE_CLASS_P (gimple_get_lhs (stmt))
3133 && SLP_INSTANCE_LOAD_PERMUTATION (instance).exists ())
3134 si = gsi_for_stmt (SLP_INSTANCE_FIRST_LOAD_STMT (instance));
3135 else if (is_pattern_stmt_p (stmt_info))
3136 si = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
3138 si = gsi_for_stmt (stmt);
3140 /* Stores should be inserted just before the last store. */
3141 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
3142 && REFERENCE_CLASS_P (gimple_get_lhs (stmt)))
3144 gimple last_store = vect_find_last_store_in_slp_instance (instance);
3145 if (is_pattern_stmt_p (vinfo_for_stmt (last_store)))
3146 last_store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (last_store));
3147 si = gsi_for_stmt (last_store);
3150 /* Mark the first element of the reduction chain as reduction to properly
3151 transform the node. In the analysis phase only the last element of the
3152 chain is marked as reduction. */
3153 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3154 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3156 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3157 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3160 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3164 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3165 For loop vectorization this is done in vectorizable_call, but for SLP
3166 it needs to be deferred until end of vect_schedule_slp, because multiple
3167 SLP instances may refer to the same scalar stmt. */
3170 vect_remove_slp_scalar_calls (slp_tree node)
3172 gimple stmt, new_stmt;
3173 gimple_stmt_iterator gsi;
3177 stmt_vec_info stmt_info;
3182 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3183 vect_remove_slp_scalar_calls (child);
3185 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3187 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3189 stmt_info = vinfo_for_stmt (stmt);
3190 if (stmt_info == NULL
3191 || is_pattern_stmt_p (stmt_info)
3192 || !PURE_SLP_STMT (stmt_info))
3194 lhs = gimple_call_lhs (stmt);
3195 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3196 set_vinfo_for_stmt (new_stmt, stmt_info);
3197 set_vinfo_for_stmt (stmt, NULL);
3198 STMT_VINFO_STMT (stmt_info) = new_stmt;
3199 gsi = gsi_for_stmt (stmt);
3200 gsi_replace (&gsi, new_stmt, false);
3201 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3205 /* Generate vector code for all SLP instances in the loop/basic block. */
3208 vect_schedule_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
3210 vec<slp_instance> slp_instances;
3211 slp_instance instance;
3212 slp_tree loads_node;
3213 unsigned int i, j, vf;
3214 bool is_store = false;
3218 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3219 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3223 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
3227 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3229 /* Schedule the tree of INSTANCE. */
3230 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3233 /* Clear STMT_VINFO_VEC_STMT of all loads. With shared loads
3234 between SLP instances we fail to properly initialize the
3235 vectorized SLP stmts and confuse different load permutations. */
3236 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), j, loads_node)
3238 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (loads_node)[0])) = NULL;
3240 if (dump_enabled_p ())
3241 dump_printf_loc (MSG_NOTE, vect_location,
3242 "vectorizing stmts using SLP.");
3245 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3247 slp_tree root = SLP_INSTANCE_TREE (instance);
3250 gimple_stmt_iterator gsi;
3252 /* Remove scalar call stmts. Do not do this for basic-block
3253 vectorization as not all uses may be vectorized.
3254 ??? Why should this be necessary? DCE should be able to
3255 remove the stmts itself.
3256 ??? For BB vectorization we can as well remove scalar
3257 stmts starting from the SLP tree root if they have no
3260 vect_remove_slp_scalar_calls (root);
3262 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3263 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3265 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3268 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3269 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3270 /* Free the attached stmt_vec_info and remove the stmt. */
3271 gsi = gsi_for_stmt (store);
3272 unlink_stmt_vdef (store);
3273 gsi_remove (&gsi, true);
3274 release_defs (store);
3275 free_stmt_vec_info (store);
3283 /* Vectorize the basic block. */
3286 vect_slp_transform_bb (basic_block bb)
3288 bb_vec_info bb_vinfo = vec_info_for_bb (bb);
3289 gimple_stmt_iterator si;
3291 gcc_assert (bb_vinfo);
3293 if (dump_enabled_p ())
3294 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB\n");
3296 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
3298 gimple stmt = gsi_stmt (si);
3299 stmt_vec_info stmt_info;
3301 if (dump_enabled_p ())
3303 dump_printf_loc (MSG_NOTE, vect_location,
3304 "------>SLPing statement: ");
3305 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3308 stmt_info = vinfo_for_stmt (stmt);
3309 gcc_assert (stmt_info);
3311 /* Schedule all the SLP instances when the first SLP stmt is reached. */
3312 if (STMT_SLP_TYPE (stmt_info))
3314 vect_schedule_slp (NULL, bb_vinfo);
3319 if (dump_enabled_p ())
3320 dump_printf (MSG_OPTIMIZED_LOCATIONS, "BASIC BLOCK VECTORIZED\n");
3322 destroy_bb_vec_info (bb_vinfo);