* gcc-interface/trans.c (gigi): Fix initialization order.
[official-gcc.git] / gcc / tree-vect-slp.c
blob5693ca5e35eb2fa8cad5e3b71d8bcccc4e4ba7d6
1 /* SLP - Basic Block Vectorization
2 Copyright (C) 2007-2015 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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "insn-config.h"
34 #include "recog.h" /* FIXME: for insn_data */
35 #include "params.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "gimple-iterator.h"
39 #include "cfgloop.h"
40 #include "tree-vectorizer.h"
41 #include "langhooks.h"
42 #include "gimple-walk.h"
43 #include "dbgcnt.h"
46 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
48 static void
49 vect_free_slp_tree (slp_tree node)
51 int i;
52 slp_tree child;
54 if (!node)
55 return;
57 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
58 vect_free_slp_tree (child);
60 SLP_TREE_CHILDREN (node).release ();
61 SLP_TREE_SCALAR_STMTS (node).release ();
62 SLP_TREE_VEC_STMTS (node).release ();
63 SLP_TREE_LOAD_PERMUTATION (node).release ();
65 free (node);
69 /* Free the memory allocated for the SLP instance. */
71 void
72 vect_free_slp_instance (slp_instance instance)
74 vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
75 SLP_INSTANCE_LOADS (instance).release ();
76 free (instance);
80 /* Create an SLP node for SCALAR_STMTS. */
82 static slp_tree
83 vect_create_new_slp_node (vec<gimple *> scalar_stmts)
85 slp_tree node;
86 gimple *stmt = scalar_stmts[0];
87 unsigned int nops;
89 if (is_gimple_call (stmt))
90 nops = gimple_call_num_args (stmt);
91 else if (is_gimple_assign (stmt))
93 nops = gimple_num_ops (stmt) - 1;
94 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
95 nops++;
97 else
98 return NULL;
100 node = XNEW (struct _slp_tree);
101 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
102 SLP_TREE_VEC_STMTS (node).create (0);
103 SLP_TREE_CHILDREN (node).create (nops);
104 SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
105 SLP_TREE_TWO_OPERATORS (node) = false;
107 return node;
111 /* This structure is used in creation of an SLP tree. Each instance
112 corresponds to the same operand in a group of scalar stmts in an SLP
113 node. */
114 typedef struct _slp_oprnd_info
116 /* Def-stmts for the operands. */
117 vec<gimple *> def_stmts;
118 /* Information about the first statement, its vector def-type, type, the
119 operand itself in case it's constant, and an indication if it's a pattern
120 stmt. */
121 enum vect_def_type first_dt;
122 tree first_op_type;
123 bool first_pattern;
124 bool second_pattern;
125 } *slp_oprnd_info;
128 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
129 operand. */
130 static vec<slp_oprnd_info>
131 vect_create_oprnd_info (int nops, int group_size)
133 int i;
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_op_type = NULL_TREE;
144 oprnd_info->first_pattern = false;
145 oprnd_info->second_pattern = false;
146 oprnds_info.quick_push (oprnd_info);
149 return oprnds_info;
153 /* Free operands info. */
155 static void
156 vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
158 int i;
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. */
174 static int
175 vect_get_place_in_interleaving_chain (gimple *stmt, gimple *first_stmt)
177 gimple *next_stmt = first_stmt;
178 int result = 0;
180 if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
181 return -1;
185 if (next_stmt == stmt)
186 return result;
187 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
188 if (next_stmt)
189 result += GROUP_GAP (vinfo_for_stmt (next_stmt));
191 while (next_stmt);
193 return -1;
197 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
198 they are of a valid type and that they match the defs of the first stmt of
199 the SLP group (stored in OPRNDS_INFO). If there was a fatal error
200 return -1, if the error could be corrected by swapping operands of the
201 operation return 1, if everything is ok return 0. */
203 static int
204 vect_get_and_check_slp_defs (vec_info *vinfo,
205 gimple *stmt, unsigned stmt_num,
206 vec<slp_oprnd_info> *oprnds_info)
208 tree oprnd;
209 unsigned int i, number_of_oprnds;
210 gimple *def_stmt;
211 enum vect_def_type dt = vect_uninitialized_def;
212 bool pattern = false;
213 slp_oprnd_info oprnd_info;
214 int first_op_idx = 1;
215 bool commutative = false;
216 bool first_op_cond = false;
217 bool first = stmt_num == 0;
218 bool second = stmt_num == 1;
220 if (is_gimple_call (stmt))
222 number_of_oprnds = gimple_call_num_args (stmt);
223 first_op_idx = 3;
225 else if (is_gimple_assign (stmt))
227 enum tree_code code = gimple_assign_rhs_code (stmt);
228 number_of_oprnds = gimple_num_ops (stmt) - 1;
229 if (gimple_assign_rhs_code (stmt) == COND_EXPR
230 && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt)))
232 first_op_cond = true;
233 commutative = true;
234 number_of_oprnds++;
236 else
237 commutative = commutative_tree_code (code);
239 else
240 return -1;
242 bool swapped = false;
243 for (i = 0; i < number_of_oprnds; i++)
245 again:
246 if (first_op_cond)
248 if (i == 0 || i == 1)
249 oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx),
250 swapped ? !i : i);
251 else
252 oprnd = gimple_op (stmt, first_op_idx + i - 1);
254 else
255 oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i));
257 oprnd_info = (*oprnds_info)[i];
259 if (!vect_is_simple_use (oprnd, vinfo, &def_stmt, &dt))
261 if (dump_enabled_p ())
263 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
264 "Build SLP failed: can't analyze def for ");
265 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
266 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
269 return -1;
272 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
273 from the pattern. Check that all the stmts of the node are in the
274 pattern. */
275 if (def_stmt && gimple_bb (def_stmt)
276 && vect_stmt_in_region_p (vinfo, def_stmt)
277 && vinfo_for_stmt (def_stmt)
278 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
279 && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
280 && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
282 pattern = true;
283 if (!first && !oprnd_info->first_pattern
284 /* Allow different pattern state for the defs of the
285 first stmt in reduction chains. */
286 && (oprnd_info->first_dt != vect_reduction_def
287 || (!second && !oprnd_info->second_pattern)))
289 if (i == 0
290 && !swapped
291 && commutative)
293 swapped = true;
294 goto again;
297 if (dump_enabled_p ())
299 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
300 "Build SLP failed: some of the stmts"
301 " are in a pattern, and others are not ");
302 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
303 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
306 return 1;
309 def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
310 dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
312 if (dt == vect_unknown_def_type)
314 if (dump_enabled_p ())
315 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
316 "Unsupported pattern.\n");
317 return -1;
320 switch (gimple_code (def_stmt))
322 case GIMPLE_PHI:
323 case GIMPLE_ASSIGN:
324 break;
326 default:
327 if (dump_enabled_p ())
328 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
329 "unsupported defining stmt:\n");
330 return -1;
334 if (second)
335 oprnd_info->second_pattern = pattern;
337 if (first)
339 oprnd_info->first_dt = dt;
340 oprnd_info->first_pattern = pattern;
341 oprnd_info->first_op_type = TREE_TYPE (oprnd);
343 else
345 /* Not first stmt of the group, check that the def-stmt/s match
346 the def-stmt/s of the first stmt. Allow different definition
347 types for reduction chains: the first stmt must be a
348 vect_reduction_def (a phi node), and the rest
349 vect_internal_def. */
350 if (((oprnd_info->first_dt != dt
351 && !(oprnd_info->first_dt == vect_reduction_def
352 && dt == vect_internal_def)
353 && !((oprnd_info->first_dt == vect_external_def
354 || oprnd_info->first_dt == vect_constant_def)
355 && (dt == vect_external_def
356 || dt == vect_constant_def)))
357 || !types_compatible_p (oprnd_info->first_op_type,
358 TREE_TYPE (oprnd))))
360 /* Try swapping operands if we got a mismatch. */
361 if (i == 0
362 && !swapped
363 && commutative)
365 swapped = true;
366 goto again;
369 if (dump_enabled_p ())
370 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
371 "Build SLP failed: different types\n");
373 return 1;
377 /* Check the types of the definitions. */
378 switch (dt)
380 case vect_constant_def:
381 case vect_external_def:
382 case vect_reduction_def:
383 break;
385 case vect_internal_def:
386 oprnd_info->def_stmts.quick_push (def_stmt);
387 break;
389 default:
390 /* FORNOW: Not supported. */
391 if (dump_enabled_p ())
393 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
394 "Build SLP failed: illegal type of def ");
395 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
396 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
399 return -1;
403 /* Swap operands. */
404 if (swapped)
406 if (first_op_cond)
408 tree cond = gimple_assign_rhs1 (stmt);
409 swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
410 &TREE_OPERAND (cond, 1));
411 TREE_SET_CODE (cond, swap_tree_comparison (TREE_CODE (cond)));
413 else
414 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
415 gimple_assign_rhs2_ptr (stmt));
418 return 0;
422 /* Verify if the scalar stmts STMTS are isomorphic, require data
423 permutation or are of unsupported types of operation. Return
424 true if they are, otherwise return false and indicate in *MATCHES
425 which stmts are not isomorphic to the first one. If MATCHES[0]
426 is false then this indicates the comparison could not be
427 carried out or the stmts will never be vectorized by SLP. */
429 static bool
430 vect_build_slp_tree_1 (vec_info *vinfo,
431 vec<gimple *> stmts, unsigned int group_size,
432 unsigned nops, unsigned int *max_nunits,
433 unsigned int vectorization_factor, bool *matches,
434 bool *two_operators)
436 unsigned int i;
437 gimple *first_stmt = stmts[0], *stmt = stmts[0];
438 enum tree_code first_stmt_code = ERROR_MARK;
439 enum tree_code alt_stmt_code = ERROR_MARK;
440 enum tree_code rhs_code = ERROR_MARK;
441 enum tree_code first_cond_code = ERROR_MARK;
442 tree lhs;
443 bool need_same_oprnds = false;
444 tree vectype = NULL_TREE, scalar_type, first_op1 = NULL_TREE;
445 optab optab;
446 int icode;
447 machine_mode optab_op2_mode;
448 machine_mode vec_mode;
449 HOST_WIDE_INT dummy;
450 gimple *first_load = NULL, *prev_first_load = NULL;
452 /* For every stmt in NODE find its def stmt/s. */
453 FOR_EACH_VEC_ELT (stmts, i, stmt)
455 matches[i] = false;
457 if (dump_enabled_p ())
459 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
460 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
463 /* Fail to vectorize statements marked as unvectorizable. */
464 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
466 if (dump_enabled_p ())
468 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
469 "Build SLP failed: unvectorizable statement ");
470 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
471 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
473 /* Fatal mismatch. */
474 matches[0] = false;
475 return false;
478 lhs = gimple_get_lhs (stmt);
479 if (lhs == NULL_TREE)
481 if (dump_enabled_p ())
483 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
484 "Build SLP failed: not GIMPLE_ASSIGN nor "
485 "GIMPLE_CALL ");
486 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
487 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
489 /* Fatal mismatch. */
490 matches[0] = false;
491 return false;
494 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
495 vectype = get_vectype_for_scalar_type (scalar_type);
496 if (!vectype)
498 if (dump_enabled_p ())
500 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
501 "Build SLP failed: unsupported data-type ");
502 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
503 scalar_type);
504 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
506 /* Fatal mismatch. */
507 matches[0] = false;
508 return false;
511 /* If populating the vector type requires unrolling then fail
512 before adjusting *max_nunits for basic-block vectorization. */
513 if (is_a <bb_vec_info> (vinfo)
514 && TYPE_VECTOR_SUBPARTS (vectype) > group_size)
516 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
517 "Build SLP failed: unrolling required "
518 "in basic block SLP\n");
519 /* Fatal mismatch. */
520 matches[0] = false;
521 return false;
524 /* In case of multiple types we need to detect the smallest type. */
525 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
527 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
528 if (is_a <bb_vec_info> (vinfo))
529 vectorization_factor = *max_nunits;
532 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
534 rhs_code = CALL_EXPR;
535 if (gimple_call_internal_p (call_stmt)
536 || gimple_call_tail_p (call_stmt)
537 || gimple_call_noreturn_p (call_stmt)
538 || !gimple_call_nothrow_p (call_stmt)
539 || gimple_call_chain (call_stmt))
541 if (dump_enabled_p ())
543 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
544 "Build SLP failed: unsupported call type ");
545 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
546 call_stmt, 0);
547 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
549 /* Fatal mismatch. */
550 matches[0] = false;
551 return false;
554 else
555 rhs_code = gimple_assign_rhs_code (stmt);
557 /* Check the operation. */
558 if (i == 0)
560 first_stmt_code = rhs_code;
562 /* Shift arguments should be equal in all the packed stmts for a
563 vector shift with scalar shift operand. */
564 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
565 || rhs_code == LROTATE_EXPR
566 || rhs_code == RROTATE_EXPR)
568 vec_mode = TYPE_MODE (vectype);
570 /* First see if we have a vector/vector shift. */
571 optab = optab_for_tree_code (rhs_code, vectype,
572 optab_vector);
574 if (!optab
575 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
577 /* No vector/vector shift, try for a vector/scalar shift. */
578 optab = optab_for_tree_code (rhs_code, vectype,
579 optab_scalar);
581 if (!optab)
583 if (dump_enabled_p ())
584 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
585 "Build SLP failed: no optab.\n");
586 /* Fatal mismatch. */
587 matches[0] = false;
588 return false;
590 icode = (int) optab_handler (optab, vec_mode);
591 if (icode == CODE_FOR_nothing)
593 if (dump_enabled_p ())
594 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
595 "Build SLP failed: "
596 "op not supported by target.\n");
597 /* Fatal mismatch. */
598 matches[0] = false;
599 return false;
601 optab_op2_mode = insn_data[icode].operand[2].mode;
602 if (!VECTOR_MODE_P (optab_op2_mode))
604 need_same_oprnds = true;
605 first_op1 = gimple_assign_rhs2 (stmt);
609 else if (rhs_code == WIDEN_LSHIFT_EXPR)
611 need_same_oprnds = true;
612 first_op1 = gimple_assign_rhs2 (stmt);
615 else
617 if (first_stmt_code != rhs_code
618 && alt_stmt_code == ERROR_MARK)
619 alt_stmt_code = rhs_code;
620 if (first_stmt_code != rhs_code
621 && (first_stmt_code != IMAGPART_EXPR
622 || rhs_code != REALPART_EXPR)
623 && (first_stmt_code != REALPART_EXPR
624 || rhs_code != IMAGPART_EXPR)
625 /* Handle mismatches in plus/minus by computing both
626 and merging the results. */
627 && !((first_stmt_code == PLUS_EXPR
628 || first_stmt_code == MINUS_EXPR)
629 && (alt_stmt_code == PLUS_EXPR
630 || alt_stmt_code == MINUS_EXPR)
631 && rhs_code == alt_stmt_code)
632 && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
633 && (first_stmt_code == ARRAY_REF
634 || first_stmt_code == BIT_FIELD_REF
635 || first_stmt_code == INDIRECT_REF
636 || first_stmt_code == COMPONENT_REF
637 || first_stmt_code == MEM_REF)))
639 if (dump_enabled_p ())
641 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
642 "Build SLP failed: different operation "
643 "in stmt ");
644 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
645 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
646 "original stmt ");
647 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
648 first_stmt, 0);
650 /* Mismatch. */
651 continue;
654 if (need_same_oprnds
655 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
657 if (dump_enabled_p ())
659 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
660 "Build SLP failed: different shift "
661 "arguments in ");
662 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
663 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
665 /* Mismatch. */
666 continue;
669 if (rhs_code == CALL_EXPR)
671 gimple *first_stmt = stmts[0];
672 if (gimple_call_num_args (stmt) != nops
673 || !operand_equal_p (gimple_call_fn (first_stmt),
674 gimple_call_fn (stmt), 0)
675 || gimple_call_fntype (first_stmt)
676 != gimple_call_fntype (stmt))
678 if (dump_enabled_p ())
680 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
681 "Build SLP failed: different calls in ");
682 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
683 stmt, 0);
684 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
686 /* Mismatch. */
687 continue;
692 /* Grouped store or load. */
693 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
695 if (REFERENCE_CLASS_P (lhs))
697 /* Store. */
700 else
702 /* Load. */
703 /* Check that the size of interleaved loads group is not
704 greater than the SLP group size. */
705 unsigned ncopies
706 = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
707 if (is_a <loop_vec_info> (vinfo)
708 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
709 && ((GROUP_SIZE (vinfo_for_stmt (stmt))
710 - GROUP_GAP (vinfo_for_stmt (stmt)))
711 > ncopies * group_size))
713 if (dump_enabled_p ())
715 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
716 "Build SLP failed: the number "
717 "of interleaved loads is greater than "
718 "the SLP group size ");
719 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
720 stmt, 0);
721 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
723 /* Fatal mismatch. */
724 matches[0] = false;
725 return false;
728 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
729 if (prev_first_load)
731 /* Check that there are no loads from different interleaving
732 chains in the same node. */
733 if (prev_first_load != first_load)
735 if (dump_enabled_p ())
737 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
738 vect_location,
739 "Build SLP failed: different "
740 "interleaving chains in one node ");
741 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
742 stmt, 0);
743 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
745 /* Mismatch. */
746 continue;
749 else
750 prev_first_load = first_load;
752 } /* Grouped access. */
753 else
755 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
757 /* Not grouped load. */
758 if (dump_enabled_p ())
760 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
761 "Build SLP failed: not grouped load ");
762 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
763 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
766 /* FORNOW: Not grouped loads are not supported. */
767 /* Fatal mismatch. */
768 matches[0] = false;
769 return false;
772 /* Not memory operation. */
773 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
774 && TREE_CODE_CLASS (rhs_code) != tcc_unary
775 && TREE_CODE_CLASS (rhs_code) != tcc_expression
776 && TREE_CODE_CLASS (rhs_code) != tcc_comparison
777 && rhs_code != CALL_EXPR)
779 if (dump_enabled_p ())
781 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
782 "Build SLP failed: operation");
783 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
784 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
785 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
787 /* Fatal mismatch. */
788 matches[0] = false;
789 return false;
792 if (rhs_code == COND_EXPR)
794 tree cond_expr = gimple_assign_rhs1 (stmt);
796 if (i == 0)
797 first_cond_code = TREE_CODE (cond_expr);
798 else if (first_cond_code != TREE_CODE (cond_expr))
800 if (dump_enabled_p ())
802 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
803 "Build SLP failed: different"
804 " operation");
805 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
806 stmt, 0);
807 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
809 /* Mismatch. */
810 continue;
815 matches[i] = true;
818 for (i = 0; i < group_size; ++i)
819 if (!matches[i])
820 return false;
822 /* If we allowed a two-operation SLP node verify the target can cope
823 with the permute we are going to use. */
824 if (alt_stmt_code != ERROR_MARK
825 && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference)
827 unsigned char *sel
828 = XALLOCAVEC (unsigned char, TYPE_VECTOR_SUBPARTS (vectype));
829 for (i = 0; i < TYPE_VECTOR_SUBPARTS (vectype); ++i)
831 sel[i] = i;
832 if (gimple_assign_rhs_code (stmts[i % group_size]) == alt_stmt_code)
833 sel[i] += TYPE_VECTOR_SUBPARTS (vectype);
835 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
837 for (i = 0; i < group_size; ++i)
838 if (gimple_assign_rhs_code (stmts[i]) == alt_stmt_code)
840 matches[i] = false;
841 if (dump_enabled_p ())
843 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
844 "Build SLP failed: different operation "
845 "in stmt ");
846 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
847 stmts[i], 0);
848 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
849 "original stmt ");
850 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
851 first_stmt, 0);
854 return false;
856 *two_operators = true;
859 return true;
862 /* Recursively build an SLP tree starting from NODE.
863 Fail (and return a value not equal to zero) if def-stmts are not
864 isomorphic, require data permutation or are of unsupported types of
865 operation. Otherwise, return 0.
866 The value returned is the depth in the SLP tree where a mismatch
867 was found. */
869 static bool
870 vect_build_slp_tree (vec_info *vinfo,
871 slp_tree *node, unsigned int group_size,
872 unsigned int *max_nunits,
873 vec<slp_tree> *loads,
874 unsigned int vectorization_factor,
875 bool *matches, unsigned *npermutes, unsigned *tree_size,
876 unsigned max_tree_size)
878 unsigned nops, i, this_tree_size = 0;
879 gimple *stmt;
881 matches[0] = false;
883 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
884 if (is_gimple_call (stmt))
885 nops = gimple_call_num_args (stmt);
886 else if (is_gimple_assign (stmt))
888 nops = gimple_num_ops (stmt) - 1;
889 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
890 nops++;
892 else
893 return false;
895 bool two_operators = false;
896 if (!vect_build_slp_tree_1 (vinfo,
897 SLP_TREE_SCALAR_STMTS (*node), group_size, nops,
898 max_nunits, vectorization_factor, matches,
899 &two_operators))
900 return false;
901 SLP_TREE_TWO_OPERATORS (*node) = two_operators;
903 /* If the SLP node is a load, terminate the recursion. */
904 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
905 && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
907 loads->safe_push (*node);
908 return true;
911 /* Get at the operands, verifying they are compatible. */
912 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
913 slp_oprnd_info oprnd_info;
914 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (*node), i, stmt)
916 switch (vect_get_and_check_slp_defs (vinfo, stmt, i, &oprnds_info))
918 case 0:
919 break;
920 case -1:
921 matches[0] = false;
922 vect_free_oprnd_info (oprnds_info);
923 return false;
924 case 1:
925 matches[i] = false;
926 break;
929 for (i = 0; i < group_size; ++i)
930 if (!matches[i])
932 vect_free_oprnd_info (oprnds_info);
933 return false;
936 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
938 /* Create SLP_TREE nodes for the definition node/s. */
939 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
941 slp_tree child;
942 unsigned old_nloads = loads->length ();
943 unsigned old_max_nunits = *max_nunits;
945 if (oprnd_info->first_dt != vect_internal_def)
946 continue;
948 if (++this_tree_size > max_tree_size)
950 vect_free_oprnd_info (oprnds_info);
951 return false;
954 child = vect_create_new_slp_node (oprnd_info->def_stmts);
955 if (!child)
957 vect_free_oprnd_info (oprnds_info);
958 return false;
961 if (vect_build_slp_tree (vinfo, &child,
962 group_size, max_nunits, loads,
963 vectorization_factor, matches,
964 npermutes, &this_tree_size, max_tree_size))
966 /* If we have all children of child built up from scalars then just
967 throw that away and build it up this node from scalars. */
968 if (!SLP_TREE_CHILDREN (child).is_empty ())
970 unsigned int j;
971 slp_tree grandchild;
973 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
974 if (grandchild != NULL)
975 break;
976 if (!grandchild)
978 /* Roll back. */
979 *max_nunits = old_max_nunits;
980 loads->truncate (old_nloads);
981 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
982 vect_free_slp_tree (grandchild);
983 SLP_TREE_CHILDREN (child).truncate (0);
985 dump_printf_loc (MSG_NOTE, vect_location,
986 "Building parent vector operands from "
987 "scalars instead\n");
988 oprnd_info->def_stmts = vNULL;
989 vect_free_slp_tree (child);
990 SLP_TREE_CHILDREN (*node).quick_push (NULL);
991 continue;
995 oprnd_info->def_stmts = vNULL;
996 SLP_TREE_CHILDREN (*node).quick_push (child);
997 continue;
1000 /* If the SLP build failed fatally and we analyze a basic-block
1001 simply treat nodes we fail to build as externally defined
1002 (and thus build vectors from the scalar defs).
1003 The cost model will reject outright expensive cases.
1004 ??? This doesn't treat cases where permutation ultimatively
1005 fails (or we don't try permutation below). Ideally we'd
1006 even compute a permutation that will end up with the maximum
1007 SLP tree size... */
1008 if (is_a <bb_vec_info> (vinfo)
1009 && !matches[0]
1010 /* ??? Rejecting patterns this way doesn't work. We'd have to
1011 do extra work to cancel the pattern so the uses see the
1012 scalar version. */
1013 && !is_pattern_stmt_p (vinfo_for_stmt (stmt)))
1015 unsigned int j;
1016 slp_tree grandchild;
1018 /* Roll back. */
1019 *max_nunits = old_max_nunits;
1020 loads->truncate (old_nloads);
1021 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1022 vect_free_slp_tree (grandchild);
1023 SLP_TREE_CHILDREN (child).truncate (0);
1025 dump_printf_loc (MSG_NOTE, vect_location,
1026 "Building vector operands from scalars\n");
1027 oprnd_info->def_stmts = vNULL;
1028 vect_free_slp_tree (child);
1029 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1030 continue;
1033 /* If the SLP build for operand zero failed and operand zero
1034 and one can be commutated try that for the scalar stmts
1035 that failed the match. */
1036 if (i == 0
1037 /* A first scalar stmt mismatch signals a fatal mismatch. */
1038 && matches[0]
1039 /* ??? For COND_EXPRs we can swap the comparison operands
1040 as well as the arms under some constraints. */
1041 && nops == 2
1042 && oprnds_info[1]->first_dt == vect_internal_def
1043 && is_gimple_assign (stmt)
1044 && commutative_tree_code (gimple_assign_rhs_code (stmt))
1045 && !SLP_TREE_TWO_OPERATORS (*node)
1046 /* Do so only if the number of not successful permutes was nor more
1047 than a cut-ff as re-trying the recursive match on
1048 possibly each level of the tree would expose exponential
1049 behavior. */
1050 && *npermutes < 4)
1052 unsigned int j;
1053 slp_tree grandchild;
1055 /* Roll back. */
1056 *max_nunits = old_max_nunits;
1057 loads->truncate (old_nloads);
1058 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1059 vect_free_slp_tree (grandchild);
1060 SLP_TREE_CHILDREN (child).truncate (0);
1062 /* Swap mismatched definition stmts. */
1063 dump_printf_loc (MSG_NOTE, vect_location,
1064 "Re-trying with swapped operands of stmts ");
1065 for (j = 0; j < group_size; ++j)
1066 if (!matches[j])
1068 std::swap (oprnds_info[0]->def_stmts[j],
1069 oprnds_info[1]->def_stmts[j]);
1070 dump_printf (MSG_NOTE, "%d ", j);
1072 dump_printf (MSG_NOTE, "\n");
1073 /* And try again with scratch 'matches' ... */
1074 bool *tem = XALLOCAVEC (bool, group_size);
1075 if (vect_build_slp_tree (vinfo, &child,
1076 group_size, max_nunits, loads,
1077 vectorization_factor,
1078 tem, npermutes, &this_tree_size,
1079 max_tree_size))
1081 /* If we have all children of child built up from scalars then
1082 just throw that away and build it up this node from scalars. */
1083 if (!SLP_TREE_CHILDREN (child).is_empty ())
1085 unsigned int j;
1086 slp_tree grandchild;
1088 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1089 if (grandchild != NULL)
1090 break;
1091 if (!grandchild)
1093 /* Roll back. */
1094 *max_nunits = old_max_nunits;
1095 loads->truncate (old_nloads);
1096 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1097 vect_free_slp_tree (grandchild);
1098 SLP_TREE_CHILDREN (child).truncate (0);
1100 dump_printf_loc (MSG_NOTE, vect_location,
1101 "Building parent vector operands from "
1102 "scalars instead\n");
1103 oprnd_info->def_stmts = vNULL;
1104 vect_free_slp_tree (child);
1105 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1106 continue;
1110 /* ... so if successful we can apply the operand swapping
1111 to the GIMPLE IL. This is necessary because for example
1112 vect_get_slp_defs uses operand indexes and thus expects
1113 canonical operand order. */
1114 for (j = 0; j < group_size; ++j)
1115 if (!matches[j])
1117 gimple *stmt = SLP_TREE_SCALAR_STMTS (*node)[j];
1118 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1119 gimple_assign_rhs2_ptr (stmt));
1121 oprnd_info->def_stmts = vNULL;
1122 SLP_TREE_CHILDREN (*node).quick_push (child);
1123 continue;
1126 ++*npermutes;
1129 oprnd_info->def_stmts = vNULL;
1130 vect_free_slp_tree (child);
1131 vect_free_oprnd_info (oprnds_info);
1132 return false;
1135 if (tree_size)
1136 *tree_size += this_tree_size;
1138 vect_free_oprnd_info (oprnds_info);
1139 return true;
1142 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
1144 static void
1145 vect_print_slp_tree (int dump_kind, location_t loc, slp_tree node)
1147 int i;
1148 gimple *stmt;
1149 slp_tree child;
1151 if (!node)
1152 return;
1154 dump_printf_loc (dump_kind, loc, "node\n");
1155 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1157 dump_printf_loc (dump_kind, loc, "\tstmt %d ", i);
1158 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
1160 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1161 vect_print_slp_tree (dump_kind, loc, child);
1165 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1166 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
1167 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1168 stmts in NODE are to be marked. */
1170 static void
1171 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1173 int i;
1174 gimple *stmt;
1175 slp_tree child;
1177 if (!node)
1178 return;
1180 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1181 if (j < 0 || i == j)
1182 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1184 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1185 vect_mark_slp_stmts (child, mark, j);
1189 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1191 static void
1192 vect_mark_slp_stmts_relevant (slp_tree node)
1194 int i;
1195 gimple *stmt;
1196 stmt_vec_info stmt_info;
1197 slp_tree child;
1199 if (!node)
1200 return;
1202 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1204 stmt_info = vinfo_for_stmt (stmt);
1205 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1206 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1207 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1210 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1211 vect_mark_slp_stmts_relevant (child);
1215 /* Rearrange the statements of NODE according to PERMUTATION. */
1217 static void
1218 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1219 vec<unsigned> permutation)
1221 gimple *stmt;
1222 vec<gimple *> tmp_stmts;
1223 unsigned int i;
1224 slp_tree child;
1226 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1227 vect_slp_rearrange_stmts (child, group_size, permutation);
1229 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1230 tmp_stmts.create (group_size);
1231 tmp_stmts.quick_grow_cleared (group_size);
1233 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1234 tmp_stmts[permutation[i]] = stmt;
1236 SLP_TREE_SCALAR_STMTS (node).release ();
1237 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1241 /* Attempt to reorder stmts in a reduction chain so that we don't
1242 require any load permutation. Return true if that was possible,
1243 otherwise return false. */
1245 static bool
1246 vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1248 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1249 unsigned int i, j;
1250 sbitmap load_index;
1251 unsigned int lidx;
1252 slp_tree node, load;
1254 /* Compare all the permutation sequences to the first one. We know
1255 that at least one load is permuted. */
1256 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1257 if (!node->load_permutation.exists ())
1258 return false;
1259 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1261 if (!load->load_permutation.exists ())
1262 return false;
1263 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1264 if (lidx != node->load_permutation[j])
1265 return false;
1268 /* Check that the loads in the first sequence are different and there
1269 are no gaps between them. */
1270 load_index = sbitmap_alloc (group_size);
1271 bitmap_clear (load_index);
1272 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1274 if (bitmap_bit_p (load_index, lidx))
1276 sbitmap_free (load_index);
1277 return false;
1279 bitmap_set_bit (load_index, lidx);
1281 for (i = 0; i < group_size; i++)
1282 if (!bitmap_bit_p (load_index, i))
1284 sbitmap_free (load_index);
1285 return false;
1287 sbitmap_free (load_index);
1289 /* This permutation is valid for reduction. Since the order of the
1290 statements in the nodes is not important unless they are memory
1291 accesses, we can rearrange the statements in all the nodes
1292 according to the order of the loads. */
1293 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1294 node->load_permutation);
1296 /* We are done, no actual permutations need to be generated. */
1297 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1298 SLP_TREE_LOAD_PERMUTATION (node).release ();
1299 return true;
1302 /* Check if the required load permutations in the SLP instance
1303 SLP_INSTN are supported. */
1305 static bool
1306 vect_supported_load_permutation_p (slp_instance slp_instn)
1308 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1309 unsigned int i, j, k, next;
1310 slp_tree node;
1311 gimple *stmt, *load, *next_load;
1313 if (dump_enabled_p ())
1315 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1316 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1317 if (node->load_permutation.exists ())
1318 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1319 dump_printf (MSG_NOTE, "%d ", next);
1320 else
1321 for (k = 0; k < group_size; ++k)
1322 dump_printf (MSG_NOTE, "%d ", k);
1323 dump_printf (MSG_NOTE, "\n");
1326 /* In case of reduction every load permutation is allowed, since the order
1327 of the reduction statements is not important (as opposed to the case of
1328 grouped stores). The only condition we need to check is that all the
1329 load nodes are of the same size and have the same permutation (and then
1330 rearrange all the nodes of the SLP instance according to this
1331 permutation). */
1333 /* Check that all the load nodes are of the same size. */
1334 /* ??? Can't we assert this? */
1335 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1336 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1337 return false;
1339 node = SLP_INSTANCE_TREE (slp_instn);
1340 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1342 /* Reduction (there are no data-refs in the root).
1343 In reduction chain the order of the loads is not important. */
1344 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1345 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1347 if (vect_attempt_slp_rearrange_stmts (slp_instn))
1348 return true;
1350 /* Fallthru to general load permutation handling. */
1353 /* In basic block vectorization we allow any subchain of an interleaving
1354 chain.
1355 FORNOW: not supported in loop SLP because of realignment compications. */
1356 if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
1358 /* Check whether the loads in an instance form a subchain and thus
1359 no permutation is necessary. */
1360 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1362 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1363 continue;
1364 bool subchain_p = true;
1365 next_load = NULL;
1366 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1368 if (j != 0
1369 && (next_load != load
1370 || GROUP_GAP (vinfo_for_stmt (load)) != 1))
1372 subchain_p = false;
1373 break;
1375 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1377 if (subchain_p)
1378 SLP_TREE_LOAD_PERMUTATION (node).release ();
1379 else
1381 /* Verify the permutation can be generated. */
1382 vec<tree> tem;
1383 if (!vect_transform_slp_perm_load (node, tem, NULL,
1384 1, slp_instn, true))
1386 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1387 vect_location,
1388 "unsupported load permutation\n");
1389 return false;
1393 return true;
1396 /* For loop vectorization verify we can generate the permutation. */
1397 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1398 if (node->load_permutation.exists ()
1399 && !vect_transform_slp_perm_load
1400 (node, vNULL, NULL,
1401 SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true))
1402 return false;
1404 return true;
1408 /* Find the last store in SLP INSTANCE. */
1410 gimple *
1411 vect_find_last_scalar_stmt_in_slp (slp_tree node)
1413 gimple *last = NULL, *stmt;
1415 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++)
1417 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1418 if (is_pattern_stmt_p (stmt_vinfo))
1419 last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last);
1420 else
1421 last = get_later_stmt (stmt, last);
1424 return last;
1427 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1429 static void
1430 vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node,
1431 stmt_vector_for_cost *prologue_cost_vec,
1432 stmt_vector_for_cost *body_cost_vec,
1433 unsigned ncopies_for_cost)
1435 unsigned i;
1436 slp_tree child;
1437 gimple *stmt, *s;
1438 stmt_vec_info stmt_info;
1439 tree lhs;
1440 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1442 /* Recurse down the SLP tree. */
1443 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1444 if (child)
1445 vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec,
1446 body_cost_vec, ncopies_for_cost);
1448 /* Look at the first scalar stmt to determine the cost. */
1449 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1450 stmt_info = vinfo_for_stmt (stmt);
1451 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1453 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1454 vect_model_store_cost (stmt_info, ncopies_for_cost, false,
1455 vect_uninitialized_def,
1456 node, prologue_cost_vec, body_cost_vec);
1457 else
1459 int i;
1460 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1461 /* If the load is permuted then the alignment is determined by
1462 the first group element not by the first scalar stmt DR. */
1463 if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
1465 stmt = GROUP_FIRST_ELEMENT (stmt_info);
1466 stmt_info = vinfo_for_stmt (stmt);
1468 vect_model_load_cost (stmt_info, ncopies_for_cost, false,
1469 node, prologue_cost_vec, body_cost_vec);
1470 /* If the load is permuted record the cost for the permutation.
1471 ??? Loads from multiple chains are let through here only
1472 for a single special case involving complex numbers where
1473 in the end no permutation is necessary. */
1474 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, s)
1475 if ((STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo_for_stmt (s))
1476 == STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info))
1477 && vect_get_place_in_interleaving_chain
1478 (s, STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info)) != i)
1480 record_stmt_cost (body_cost_vec, group_size, vec_perm,
1481 stmt_info, 0, vect_body);
1482 break;
1486 else
1488 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1489 stmt_info, 0, vect_body);
1490 if (SLP_TREE_TWO_OPERATORS (node))
1492 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1493 stmt_info, 0, vect_body);
1494 record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm,
1495 stmt_info, 0, vect_body);
1499 /* Scan operands and account for prologue cost of constants/externals.
1500 ??? This over-estimates cost for multiple uses and should be
1501 re-engineered. */
1502 lhs = gimple_get_lhs (stmt);
1503 for (i = 0; i < gimple_num_ops (stmt); ++i)
1505 tree op = gimple_op (stmt, i);
1506 gimple *def_stmt;
1507 enum vect_def_type dt;
1508 if (!op || op == lhs)
1509 continue;
1510 if (vect_is_simple_use (op, stmt_info->vinfo, &def_stmt, &dt))
1512 /* Without looking at the actual initializer a vector of
1513 constants can be implemented as load from the constant pool.
1514 ??? We need to pass down stmt_info for a vector type
1515 even if it points to the wrong stmt. */
1516 if (dt == vect_constant_def)
1517 record_stmt_cost (prologue_cost_vec, 1, vector_load,
1518 stmt_info, 0, vect_prologue);
1519 else if (dt == vect_external_def)
1520 record_stmt_cost (prologue_cost_vec, 1, vec_construct,
1521 stmt_info, 0, vect_prologue);
1526 /* Compute the cost for the SLP instance INSTANCE. */
1528 static void
1529 vect_analyze_slp_cost (slp_instance instance, void *data)
1531 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1532 unsigned ncopies_for_cost;
1533 stmt_info_for_cost *si;
1534 unsigned i;
1536 if (dump_enabled_p ())
1537 dump_printf_loc (MSG_NOTE, vect_location,
1538 "=== vect_analyze_slp_cost ===\n");
1540 /* Calculate the number of vector stmts to create based on the unrolling
1541 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1542 GROUP_SIZE / NUNITS otherwise. */
1543 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1544 slp_tree node = SLP_INSTANCE_TREE (instance);
1545 stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1546 /* Adjust the group_size by the vectorization factor which is always one
1547 for basic-block vectorization. */
1548 if (STMT_VINFO_LOOP_VINFO (stmt_info))
1549 group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info));
1550 unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1551 /* For reductions look at a reduction operand in case the reduction
1552 operation is widening like DOT_PROD or SAD. */
1553 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1555 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1556 switch (gimple_assign_rhs_code (stmt))
1558 case DOT_PROD_EXPR:
1559 case SAD_EXPR:
1560 nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type
1561 (TREE_TYPE (gimple_assign_rhs1 (stmt))));
1562 break;
1563 default:;
1566 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1568 prologue_cost_vec.create (10);
1569 body_cost_vec.create (10);
1570 vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance),
1571 &prologue_cost_vec, &body_cost_vec,
1572 ncopies_for_cost);
1574 /* Record the prologue costs, which were delayed until we were
1575 sure that SLP was successful. */
1576 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1578 struct _stmt_vec_info *stmt_info
1579 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1580 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1581 si->misalign, vect_prologue);
1584 /* Record the instance's instructions in the target cost model. */
1585 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1587 struct _stmt_vec_info *stmt_info
1588 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1589 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1590 si->misalign, vect_body);
1593 prologue_cost_vec.release ();
1594 body_cost_vec.release ();
1597 /* Splits a group of stores, currently beginning at FIRST_STMT, into two groups:
1598 one (still beginning at FIRST_STMT) of size GROUP1_SIZE (also containing
1599 the first GROUP1_SIZE stmts, since stores are consecutive), the second
1600 containing the remainder.
1601 Return the first stmt in the second group. */
1603 static gimple *
1604 vect_split_slp_store_group (gimple *first_stmt, unsigned group1_size)
1606 stmt_vec_info first_vinfo = vinfo_for_stmt (first_stmt);
1607 gcc_assert (GROUP_FIRST_ELEMENT (first_vinfo) == first_stmt);
1608 gcc_assert (group1_size > 0);
1609 int group2_size = GROUP_SIZE (first_vinfo) - group1_size;
1610 gcc_assert (group2_size > 0);
1611 GROUP_SIZE (first_vinfo) = group1_size;
1613 gimple *stmt = first_stmt;
1614 for (unsigned i = group1_size; i > 1; i--)
1616 stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1617 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1619 /* STMT is now the last element of the first group. */
1620 gimple *group2 = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1621 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)) = 0;
1623 GROUP_SIZE (vinfo_for_stmt (group2)) = group2_size;
1624 for (stmt = group2; stmt; stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)))
1626 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = group2;
1627 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1630 /* For the second group, the GROUP_GAP is that before the original group,
1631 plus skipping over the first vector. */
1632 GROUP_GAP (vinfo_for_stmt (group2)) =
1633 GROUP_GAP (first_vinfo) + group1_size;
1635 /* GROUP_GAP of the first group now has to skip over the second group too. */
1636 GROUP_GAP (first_vinfo) += group2_size;
1638 if (dump_enabled_p ())
1639 dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
1640 group1_size, group2_size);
1642 return group2;
1645 /* Analyze an SLP instance starting from a group of grouped stores. Call
1646 vect_build_slp_tree to build a tree of packed stmts if possible.
1647 Return FALSE if it's impossible to SLP any stmt in the loop. */
1649 static bool
1650 vect_analyze_slp_instance (vec_info *vinfo,
1651 gimple *stmt, unsigned max_tree_size)
1653 slp_instance new_instance;
1654 slp_tree node;
1655 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1656 unsigned int unrolling_factor = 1, nunits;
1657 tree vectype, scalar_type = NULL_TREE;
1658 gimple *next;
1659 unsigned int vectorization_factor = 0;
1660 unsigned int i;
1661 unsigned int max_nunits = 0;
1662 vec<slp_tree> loads;
1663 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1664 vec<gimple *> scalar_stmts;
1666 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1668 if (dr)
1670 scalar_type = TREE_TYPE (DR_REF (dr));
1671 vectype = get_vectype_for_scalar_type (scalar_type);
1673 else
1675 gcc_assert (is_a <loop_vec_info> (vinfo));
1676 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1679 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1681 else
1683 gcc_assert (is_a <loop_vec_info> (vinfo));
1684 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1685 group_size = as_a <loop_vec_info> (vinfo)->reductions.length ();
1688 if (!vectype)
1690 if (dump_enabled_p ())
1692 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1693 "Build SLP failed: unsupported data-type ");
1694 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1695 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1698 return false;
1701 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1702 if (is_a <loop_vec_info> (vinfo))
1703 vectorization_factor = as_a <loop_vec_info> (vinfo)->vectorization_factor;
1704 else
1705 vectorization_factor = nunits;
1707 /* Calculate the unrolling factor. */
1708 unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
1709 if (unrolling_factor != 1 && is_a <bb_vec_info> (vinfo))
1711 if (dump_enabled_p ())
1712 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1713 "Build SLP failed: unrolling required in basic"
1714 " block SLP\n");
1716 return false;
1719 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1720 scalar_stmts.create (group_size);
1721 next = stmt;
1722 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1724 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1725 while (next)
1727 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1728 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1729 scalar_stmts.safe_push (
1730 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1731 else
1732 scalar_stmts.safe_push (next);
1733 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1735 /* Mark the first element of the reduction chain as reduction to properly
1736 transform the node. In the reduction analysis phase only the last
1737 element of the chain is marked as reduction. */
1738 if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1739 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def;
1741 else
1743 /* Collect reduction statements. */
1744 vec<gimple *> reductions = as_a <loop_vec_info> (vinfo)->reductions;
1745 for (i = 0; reductions.iterate (i, &next); i++)
1746 scalar_stmts.safe_push (next);
1749 node = vect_create_new_slp_node (scalar_stmts);
1751 loads.create (group_size);
1753 /* Build the tree for the SLP instance. */
1754 bool *matches = XALLOCAVEC (bool, group_size);
1755 unsigned npermutes = 0;
1756 if (vect_build_slp_tree (vinfo, &node, group_size,
1757 &max_nunits, &loads,
1758 vectorization_factor, matches, &npermutes, NULL,
1759 max_tree_size))
1761 /* Calculate the unrolling factor based on the smallest type. */
1762 if (max_nunits > nunits)
1763 unrolling_factor = least_common_multiple (max_nunits, group_size)
1764 / group_size;
1766 if (unrolling_factor != 1 && is_a <bb_vec_info> (vinfo))
1768 if (dump_enabled_p ())
1769 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1770 "Build SLP failed: unrolling required in basic"
1771 " block SLP\n");
1772 vect_free_slp_tree (node);
1773 loads.release ();
1774 return false;
1777 /* Create a new SLP instance. */
1778 new_instance = XNEW (struct _slp_instance);
1779 SLP_INSTANCE_TREE (new_instance) = node;
1780 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1781 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1782 SLP_INSTANCE_LOADS (new_instance) = loads;
1784 /* Compute the load permutation. */
1785 slp_tree load_node;
1786 bool loads_permuted = false;
1787 FOR_EACH_VEC_ELT (loads, i, load_node)
1789 vec<unsigned> load_permutation;
1790 int j;
1791 gimple *load, *first_stmt;
1792 bool this_load_permuted = false;
1793 load_permutation.create (group_size);
1794 first_stmt = GROUP_FIRST_ELEMENT
1795 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
1796 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
1798 int load_place
1799 = vect_get_place_in_interleaving_chain (load, first_stmt);
1800 gcc_assert (load_place != -1);
1801 if (load_place != j)
1802 this_load_permuted = true;
1803 load_permutation.safe_push (load_place);
1805 if (!this_load_permuted
1806 /* The load requires permutation when unrolling exposes
1807 a gap either because the group is larger than the SLP
1808 group-size or because there is a gap between the groups. */
1809 && (unrolling_factor == 1
1810 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1811 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)))
1813 load_permutation.release ();
1814 continue;
1816 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
1817 loads_permuted = true;
1820 if (loads_permuted)
1822 if (!vect_supported_load_permutation_p (new_instance))
1824 if (dump_enabled_p ())
1826 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1827 "Build SLP failed: unsupported load "
1828 "permutation ");
1829 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
1830 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1832 vect_free_slp_instance (new_instance);
1833 return false;
1837 vinfo->slp_instances.safe_push (new_instance);
1839 if (dump_enabled_p ())
1841 dump_printf_loc (MSG_NOTE, vect_location,
1842 "Final SLP tree for instance:\n");
1843 vect_print_slp_tree (MSG_NOTE, vect_location, node);
1846 return true;
1849 /* Failed to SLP. */
1850 /* Free the allocated memory. */
1851 vect_free_slp_tree (node);
1852 loads.release ();
1854 /* For basic block SLP, try to break the group up into multiples of the
1855 vectorization factor. */
1856 if (is_a <bb_vec_info> (vinfo)
1857 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
1858 && STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1860 /* We consider breaking the group only on VF boundaries from the existing
1861 start. */
1862 for (i = 0; i < group_size; i++)
1863 if (!matches[i]) break;
1865 if (i >= vectorization_factor && i < group_size)
1867 /* Split into two groups at the first vector boundary before i. */
1868 gcc_assert ((vectorization_factor & (vectorization_factor - 1)) == 0);
1869 unsigned group1_size = i & ~(vectorization_factor - 1);
1871 gimple *rest = vect_split_slp_store_group (stmt, group1_size);
1872 bool res = vect_analyze_slp_instance (vinfo, stmt, max_tree_size);
1873 /* If the first non-match was in the middle of a vector,
1874 skip the rest of that vector. */
1875 if (group1_size < i)
1877 i = group1_size + vectorization_factor;
1878 if (i < group_size)
1879 rest = vect_split_slp_store_group (rest, vectorization_factor);
1881 if (i < group_size)
1882 res |= vect_analyze_slp_instance (vinfo, rest, max_tree_size);
1883 return res;
1885 /* Even though the first vector did not all match, we might be able to SLP
1886 (some) of the remainder. FORNOW ignore this possibility. */
1889 return false;
1893 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
1894 trees of packed scalar stmts if SLP is possible. */
1896 bool
1897 vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size)
1899 unsigned int i;
1900 gimple *first_element;
1901 bool ok = false;
1903 if (dump_enabled_p ())
1904 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
1906 /* Find SLP sequences starting from groups of grouped stores. */
1907 FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
1908 if (vect_analyze_slp_instance (vinfo, first_element, max_tree_size))
1909 ok = true;
1911 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
1913 if (loop_vinfo->reduction_chains.length () > 0)
1915 /* Find SLP sequences starting from reduction chains. */
1916 FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
1917 if (vect_analyze_slp_instance (vinfo, first_element,
1918 max_tree_size))
1919 ok = true;
1920 else
1921 return false;
1923 /* Don't try to vectorize SLP reductions if reduction chain was
1924 detected. */
1925 return ok;
1928 /* Find SLP sequences starting from groups of reductions. */
1929 if (loop_vinfo->reductions.length () > 1
1930 && vect_analyze_slp_instance (vinfo, loop_vinfo->reductions[0],
1931 max_tree_size))
1932 ok = true;
1935 return true;
1939 /* For each possible SLP instance decide whether to SLP it and calculate overall
1940 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
1941 least one instance. */
1943 bool
1944 vect_make_slp_decision (loop_vec_info loop_vinfo)
1946 unsigned int i, unrolling_factor = 1;
1947 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1948 slp_instance instance;
1949 int decided_to_slp = 0;
1951 if (dump_enabled_p ())
1952 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
1953 "\n");
1955 FOR_EACH_VEC_ELT (slp_instances, i, instance)
1957 /* FORNOW: SLP if you can. */
1958 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
1959 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
1961 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
1962 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
1963 loop-based vectorization. Such stmts will be marked as HYBRID. */
1964 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
1965 decided_to_slp++;
1968 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
1970 if (decided_to_slp && dump_enabled_p ())
1971 dump_printf_loc (MSG_NOTE, vect_location,
1972 "Decided to SLP %d instances. Unrolling factor %d\n",
1973 decided_to_slp, unrolling_factor);
1975 return (decided_to_slp > 0);
1979 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
1980 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
1982 static void
1983 vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
1985 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[i];
1986 imm_use_iterator imm_iter;
1987 gimple *use_stmt;
1988 stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
1989 slp_tree child;
1990 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1991 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1992 int j;
1994 /* Propagate hybrid down the SLP tree. */
1995 if (stype == hybrid)
1997 else if (HYBRID_SLP_STMT (stmt_vinfo))
1998 stype = hybrid;
1999 else
2001 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
2002 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
2003 /* We always get the pattern stmt here, but for immediate
2004 uses we have to use the LHS of the original stmt. */
2005 gcc_checking_assert (!STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
2006 if (STMT_VINFO_RELATED_STMT (stmt_vinfo))
2007 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
2008 if (TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2009 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
2011 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2012 continue;
2013 use_vinfo = vinfo_for_stmt (use_stmt);
2014 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
2015 && STMT_VINFO_RELATED_STMT (use_vinfo))
2016 use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
2017 if (!STMT_SLP_TYPE (use_vinfo)
2018 && (STMT_VINFO_RELEVANT (use_vinfo)
2019 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2020 && !(gimple_code (use_stmt) == GIMPLE_PHI
2021 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2023 if (dump_enabled_p ())
2025 dump_printf_loc (MSG_NOTE, vect_location, "use of SLP "
2026 "def in non-SLP stmt: ");
2027 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0);
2029 stype = hybrid;
2034 if (stype == hybrid
2035 && !HYBRID_SLP_STMT (stmt_vinfo))
2037 if (dump_enabled_p ())
2039 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2040 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2042 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2045 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2046 if (child)
2047 vect_detect_hybrid_slp_stmts (child, i, stype);
2050 /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
2052 static tree
2053 vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2055 walk_stmt_info *wi = (walk_stmt_info *)data;
2056 struct loop *loopp = (struct loop *)wi->info;
2058 if (wi->is_lhs)
2059 return NULL_TREE;
2061 if (TREE_CODE (*tp) == SSA_NAME
2062 && !SSA_NAME_IS_DEFAULT_DEF (*tp))
2064 gimple *def_stmt = SSA_NAME_DEF_STMT (*tp);
2065 if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
2066 && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
2068 if (dump_enabled_p ())
2070 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2071 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2073 STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
2077 return NULL_TREE;
2080 static tree
2081 vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
2082 walk_stmt_info *)
2084 /* If the stmt is in a SLP instance then this isn't a reason
2085 to mark use definitions in other SLP instances as hybrid. */
2086 if (STMT_SLP_TYPE (vinfo_for_stmt (gsi_stmt (*gsi))) != loop_vect)
2087 *handled = true;
2088 return NULL_TREE;
2091 /* Find stmts that must be both vectorized and SLPed. */
2093 void
2094 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2096 unsigned int i;
2097 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2098 slp_instance instance;
2100 if (dump_enabled_p ())
2101 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
2102 "\n");
2104 /* First walk all pattern stmt in the loop and mark defs of uses as
2105 hybrid because immediate uses in them are not recorded. */
2106 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2108 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2109 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2110 gsi_next (&gsi))
2112 gimple *stmt = gsi_stmt (gsi);
2113 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2114 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2116 walk_stmt_info wi;
2117 memset (&wi, 0, sizeof (wi));
2118 wi.info = LOOP_VINFO_LOOP (loop_vinfo);
2119 gimple_stmt_iterator gsi2
2120 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2121 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2122 vect_detect_hybrid_slp_1, &wi);
2123 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2124 vect_detect_hybrid_slp_2,
2125 vect_detect_hybrid_slp_1, &wi);
2130 /* Then walk the SLP instance trees marking stmts with uses in
2131 non-SLP stmts as hybrid, also propagating hybrid down the
2132 SLP tree, collecting the above info on-the-fly. */
2133 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2135 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2136 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2137 i, pure_slp);
2142 /* Create and initialize a new bb_vec_info struct for BB, as well as
2143 stmt_vec_info structs for all the stmts in it. */
2145 static bb_vec_info
2146 new_bb_vec_info (gimple_stmt_iterator region_begin,
2147 gimple_stmt_iterator region_end)
2149 basic_block bb = gsi_bb (region_begin);
2150 bb_vec_info res = NULL;
2151 gimple_stmt_iterator gsi;
2153 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
2154 res->kind = vec_info::bb;
2155 BB_VINFO_BB (res) = bb;
2156 res->region_begin = region_begin;
2157 res->region_end = region_end;
2159 for (gsi = region_begin; gsi_stmt (gsi) != gsi_stmt (region_end);
2160 gsi_next (&gsi))
2162 gimple *stmt = gsi_stmt (gsi);
2163 gimple_set_uid (stmt, 0);
2164 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res));
2167 BB_VINFO_GROUPED_STORES (res).create (10);
2168 BB_VINFO_SLP_INSTANCES (res).create (2);
2169 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
2171 bb->aux = res;
2172 return res;
2176 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2177 stmts in the basic block. */
2179 static void
2180 destroy_bb_vec_info (bb_vec_info bb_vinfo)
2182 vec<slp_instance> slp_instances;
2183 slp_instance instance;
2184 basic_block bb;
2185 gimple_stmt_iterator si;
2186 unsigned i;
2188 if (!bb_vinfo)
2189 return;
2191 bb = BB_VINFO_BB (bb_vinfo);
2193 for (si = bb_vinfo->region_begin;
2194 gsi_stmt (si) != gsi_stmt (bb_vinfo->region_end); gsi_next (&si))
2196 gimple *stmt = gsi_stmt (si);
2197 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2199 if (stmt_info)
2200 /* Free stmt_vec_info. */
2201 free_stmt_vec_info (stmt);
2203 /* Reset region marker. */
2204 gimple_set_uid (stmt, -1);
2207 vect_destroy_datarefs (bb_vinfo);
2208 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
2209 BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
2210 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2211 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2212 vect_free_slp_instance (instance);
2213 BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
2214 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
2215 free (bb_vinfo);
2216 bb->aux = NULL;
2220 /* Analyze statements contained in SLP tree node after recursively analyzing
2221 the subtree. Return TRUE if the operations are supported. */
2223 static bool
2224 vect_slp_analyze_node_operations (slp_tree node)
2226 bool dummy;
2227 int i;
2228 gimple *stmt;
2229 slp_tree child;
2231 if (!node)
2232 return true;
2234 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2235 if (!vect_slp_analyze_node_operations (child))
2236 return false;
2238 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2240 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2241 gcc_assert (stmt_info);
2242 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2244 if (!vect_analyze_stmt (stmt, &dummy, node))
2245 return false;
2248 return true;
2252 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2253 operations are supported. */
2255 bool
2256 vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data)
2258 slp_instance instance;
2259 int i;
2261 if (dump_enabled_p ())
2262 dump_printf_loc (MSG_NOTE, vect_location,
2263 "=== vect_slp_analyze_operations ===\n");
2265 for (i = 0; slp_instances.iterate (i, &instance); )
2267 if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance)))
2269 dump_printf_loc (MSG_NOTE, vect_location,
2270 "removing SLP instance operations starting from: ");
2271 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2272 SLP_TREE_SCALAR_STMTS
2273 (SLP_INSTANCE_TREE (instance))[0], 0);
2274 vect_free_slp_instance (instance);
2275 slp_instances.ordered_remove (i);
2277 else
2279 /* Compute the costs of the SLP instance. */
2280 vect_analyze_slp_cost (instance, data);
2281 i++;
2285 if (!slp_instances.length ())
2286 return false;
2288 return true;
2292 /* Compute the scalar cost of the SLP node NODE and its children
2293 and return it. Do not account defs that are marked in LIFE and
2294 update LIFE according to uses of NODE. */
2296 static unsigned
2297 vect_bb_slp_scalar_cost (basic_block bb,
2298 slp_tree node, vec<bool, va_heap> *life)
2300 unsigned scalar_cost = 0;
2301 unsigned i;
2302 gimple *stmt;
2303 slp_tree child;
2305 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2307 unsigned stmt_cost;
2308 ssa_op_iter op_iter;
2309 def_operand_p def_p;
2310 stmt_vec_info stmt_info;
2312 if ((*life)[i])
2313 continue;
2315 /* If there is a non-vectorized use of the defs then the scalar
2316 stmt is kept live in which case we do not account it or any
2317 required defs in the SLP children in the scalar cost. This
2318 way we make the vectorization more costly when compared to
2319 the scalar cost. */
2320 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2322 imm_use_iterator use_iter;
2323 gimple *use_stmt;
2324 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
2325 if (!is_gimple_debug (use_stmt)
2326 && (! vect_stmt_in_region_p (vinfo_for_stmt (stmt)->vinfo,
2327 use_stmt)
2328 || !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (use_stmt))))
2330 (*life)[i] = true;
2331 BREAK_FROM_IMM_USE_STMT (use_iter);
2334 if ((*life)[i])
2335 continue;
2337 stmt_info = vinfo_for_stmt (stmt);
2338 if (STMT_VINFO_DATA_REF (stmt_info))
2340 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2341 stmt_cost = vect_get_stmt_cost (scalar_load);
2342 else
2343 stmt_cost = vect_get_stmt_cost (scalar_store);
2345 else
2346 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2348 scalar_cost += stmt_cost;
2351 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2352 if (child)
2353 scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
2355 return scalar_cost;
2358 /* Check if vectorization of the basic block is profitable. */
2360 static bool
2361 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2363 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2364 slp_instance instance;
2365 int i;
2366 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2367 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2369 /* Calculate scalar cost. */
2370 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2372 auto_vec<bool, 20> life;
2373 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
2374 scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2375 SLP_INSTANCE_TREE (instance),
2376 &life);
2379 /* Complete the target-specific cost calculation. */
2380 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2381 &vec_inside_cost, &vec_epilogue_cost);
2383 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2385 if (dump_enabled_p ())
2387 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2388 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2389 vec_inside_cost);
2390 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2391 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2392 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
2395 /* Vectorization is profitable if its cost is more than the cost of scalar
2396 version. Note that we err on the vector side for equal cost because
2397 the cost estimate is otherwise quite pessimistic (constant uses are
2398 free on the scalar side but cost a load on the vector side for
2399 example). */
2400 if (vec_outside_cost + vec_inside_cost > scalar_cost)
2401 return false;
2403 return true;
2406 /* Check if the basic block can be vectorized. Returns a bb_vec_info
2407 if so and sets fatal to true if failure is independent of
2408 current_vector_size. */
2410 static bb_vec_info
2411 vect_slp_analyze_bb_1 (gimple_stmt_iterator region_begin,
2412 gimple_stmt_iterator region_end,
2413 vec<data_reference_p> datarefs, int n_stmts,
2414 bool &fatal)
2416 bb_vec_info bb_vinfo;
2417 slp_instance instance;
2418 int i;
2419 int min_vf = 2;
2421 /* The first group of checks is independent of the vector size. */
2422 fatal = true;
2424 if (n_stmts > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2426 if (dump_enabled_p ())
2427 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2428 "not vectorized: too many instructions in "
2429 "basic block.\n");
2430 free_data_refs (datarefs);
2431 return NULL;
2434 bb_vinfo = new_bb_vec_info (region_begin, region_end);
2435 if (!bb_vinfo)
2436 return NULL;
2438 BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
2440 /* Analyze the data references. */
2442 if (!vect_analyze_data_refs (bb_vinfo, &min_vf))
2444 if (dump_enabled_p ())
2445 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2446 "not vectorized: unhandled data-ref in basic "
2447 "block.\n");
2449 destroy_bb_vec_info (bb_vinfo);
2450 return NULL;
2453 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2455 if (dump_enabled_p ())
2456 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2457 "not vectorized: not enough data-refs in "
2458 "basic block.\n");
2460 destroy_bb_vec_info (bb_vinfo);
2461 return NULL;
2464 if (!vect_analyze_data_ref_accesses (bb_vinfo))
2466 if (dump_enabled_p ())
2467 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2468 "not vectorized: unhandled data access in "
2469 "basic block.\n");
2471 destroy_bb_vec_info (bb_vinfo);
2472 return NULL;
2475 /* If there are no grouped stores in the region there is no need
2476 to continue with pattern recog as vect_analyze_slp will fail
2477 anyway. */
2478 if (bb_vinfo->grouped_stores.is_empty ())
2480 if (dump_enabled_p ())
2481 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2482 "not vectorized: no grouped stores in "
2483 "basic block.\n");
2485 destroy_bb_vec_info (bb_vinfo);
2486 return NULL;
2489 /* While the rest of the analysis below depends on it in some way. */
2490 fatal = false;
2492 vect_pattern_recog (bb_vinfo);
2494 /* Check the SLP opportunities in the basic block, analyze and build SLP
2495 trees. */
2496 if (!vect_analyze_slp (bb_vinfo, n_stmts))
2498 if (dump_enabled_p ())
2500 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2501 "Failed to SLP the basic block.\n");
2502 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2503 "not vectorized: failed to find SLP opportunities "
2504 "in basic block.\n");
2507 destroy_bb_vec_info (bb_vinfo);
2508 return NULL;
2511 /* Analyze and verify the alignment of data references and the
2512 dependence in the SLP instances. */
2513 for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
2515 if (! vect_slp_analyze_and_verify_instance_alignment (instance)
2516 || ! vect_slp_analyze_instance_dependence (instance))
2518 dump_printf_loc (MSG_NOTE, vect_location,
2519 "removing SLP instance operations starting from: ");
2520 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2521 SLP_TREE_SCALAR_STMTS
2522 (SLP_INSTANCE_TREE (instance))[0], 0);
2523 vect_free_slp_instance (instance);
2524 BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
2525 continue;
2528 /* Mark all the statements that we want to vectorize as pure SLP and
2529 relevant. */
2530 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2531 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2533 i++;
2535 if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
2537 destroy_bb_vec_info (bb_vinfo);
2538 return NULL;
2541 /* Mark all the statements that we do not want to vectorize. */
2542 for (gimple_stmt_iterator gsi = bb_vinfo->region_begin;
2543 gsi_stmt (gsi) != gsi_stmt (bb_vinfo->region_end); gsi_next (&gsi))
2545 stmt_vec_info vinfo = vinfo_for_stmt (gsi_stmt (gsi));
2546 if (STMT_SLP_TYPE (vinfo) != pure_slp)
2547 STMT_VINFO_VECTORIZABLE (vinfo) = false;
2550 if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo),
2551 BB_VINFO_TARGET_COST_DATA (bb_vinfo)))
2553 if (dump_enabled_p ())
2554 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2555 "not vectorized: bad operation in basic block.\n");
2557 destroy_bb_vec_info (bb_vinfo);
2558 return NULL;
2561 /* Cost model: check if the vectorization is worthwhile. */
2562 if (!unlimited_cost_model (NULL)
2563 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2565 if (dump_enabled_p ())
2566 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2567 "not vectorized: vectorization is not "
2568 "profitable.\n");
2570 destroy_bb_vec_info (bb_vinfo);
2571 return NULL;
2574 if (dump_enabled_p ())
2575 dump_printf_loc (MSG_NOTE, vect_location,
2576 "Basic block will be vectorized using SLP\n");
2578 return bb_vinfo;
2582 /* Main entry for the BB vectorizer. Analyze and transform BB, returns
2583 true if anything in the basic-block was vectorized. */
2585 bool
2586 vect_slp_bb (basic_block bb)
2588 bb_vec_info bb_vinfo;
2589 gimple_stmt_iterator gsi;
2590 unsigned int vector_sizes;
2591 bool any_vectorized = false;
2593 if (dump_enabled_p ())
2594 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2596 /* Autodetect first vector size we try. */
2597 current_vector_size = 0;
2598 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2600 gsi = gsi_start_bb (bb);
2602 while (1)
2604 if (gsi_end_p (gsi))
2605 break;
2607 gimple_stmt_iterator region_begin = gsi;
2608 vec<data_reference_p> datarefs = vNULL;
2609 int insns = 0;
2611 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2613 gimple *stmt = gsi_stmt (gsi);
2614 if (is_gimple_debug (stmt))
2615 continue;
2616 insns++;
2618 if (gimple_location (stmt) != UNKNOWN_LOCATION)
2619 vect_location = gimple_location (stmt);
2621 if (!find_data_references_in_stmt (NULL, stmt, &datarefs))
2622 break;
2625 /* Skip leading unhandled stmts. */
2626 if (gsi_stmt (region_begin) == gsi_stmt (gsi))
2628 gsi_next (&gsi);
2629 continue;
2632 gimple_stmt_iterator region_end = gsi;
2634 bool vectorized = false;
2635 bool fatal = false;
2636 bb_vinfo = vect_slp_analyze_bb_1 (region_begin, region_end,
2637 datarefs, insns, fatal);
2638 if (bb_vinfo
2639 && dbg_cnt (vect_slp))
2641 if (dump_enabled_p ())
2642 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
2644 vect_schedule_slp (bb_vinfo);
2646 if (dump_enabled_p ())
2647 dump_printf_loc (MSG_NOTE, vect_location,
2648 "basic block part vectorized\n");
2650 destroy_bb_vec_info (bb_vinfo);
2652 vectorized = true;
2654 else
2655 destroy_bb_vec_info (bb_vinfo);
2657 any_vectorized |= vectorized;
2659 vector_sizes &= ~current_vector_size;
2660 if (vectorized
2661 || vector_sizes == 0
2662 || current_vector_size == 0
2663 /* If vect_slp_analyze_bb_1 signaled that analysis for all
2664 vector sizes will fail do not bother iterating. */
2665 || fatal)
2667 if (gsi_end_p (region_end))
2668 break;
2670 /* Skip the unhandled stmt. */
2671 gsi_next (&gsi);
2673 /* And reset vector sizes. */
2674 current_vector_size = 0;
2675 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2677 else
2679 /* Try the next biggest vector size. */
2680 current_vector_size = 1 << floor_log2 (vector_sizes);
2681 if (dump_enabled_p ())
2682 dump_printf_loc (MSG_NOTE, vect_location,
2683 "***** Re-trying analysis with "
2684 "vector size %d\n", current_vector_size);
2686 /* Start over. */
2687 gsi = region_begin;
2691 return any_vectorized;
2695 /* Return 1 if vector type of boolean constant which is OPNUM
2696 operand in statement STMT is a boolean vector. */
2698 static bool
2699 vect_mask_constant_operand_p (gimple *stmt, int opnum)
2701 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2702 enum tree_code code = gimple_expr_code (stmt);
2703 tree op, vectype;
2704 gimple *def_stmt;
2705 enum vect_def_type dt;
2707 /* For comparison and COND_EXPR type is chosen depending
2708 on the other comparison operand. */
2709 if (TREE_CODE_CLASS (code) == tcc_comparison)
2711 if (opnum)
2712 op = gimple_assign_rhs1 (stmt);
2713 else
2714 op = gimple_assign_rhs2 (stmt);
2716 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
2717 &dt, &vectype))
2718 gcc_unreachable ();
2720 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
2723 if (code == COND_EXPR)
2725 tree cond = gimple_assign_rhs1 (stmt);
2727 if (TREE_CODE (cond) == SSA_NAME)
2728 return false;
2730 if (opnum)
2731 op = TREE_OPERAND (cond, 1);
2732 else
2733 op = TREE_OPERAND (cond, 0);
2735 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
2736 &dt, &vectype))
2737 gcc_unreachable ();
2739 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
2742 return VECTOR_BOOLEAN_TYPE_P (STMT_VINFO_VECTYPE (stmt_vinfo));
2746 /* For constant and loop invariant defs of SLP_NODE this function returns
2747 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2748 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2749 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2750 REDUC_INDEX is the index of the reduction operand in the statements, unless
2751 it is -1. */
2753 static void
2754 vect_get_constant_vectors (tree op, slp_tree slp_node,
2755 vec<tree> *vec_oprnds,
2756 unsigned int op_num, unsigned int number_of_vectors,
2757 int reduc_index)
2759 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2760 gimple *stmt = stmts[0];
2761 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2762 unsigned nunits;
2763 tree vec_cst;
2764 tree *elts;
2765 unsigned j, number_of_places_left_in_vector;
2766 tree vector_type;
2767 tree vop;
2768 int group_size = stmts.length ();
2769 unsigned int vec_num, i;
2770 unsigned number_of_copies = 1;
2771 vec<tree> voprnds;
2772 voprnds.create (number_of_vectors);
2773 bool constant_p, is_store;
2774 tree neutral_op = NULL;
2775 enum tree_code code = gimple_expr_code (stmt);
2776 gimple *def_stmt;
2777 struct loop *loop;
2778 gimple_seq ctor_seq = NULL;
2780 /* Check if vector type is a boolean vector. */
2781 if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE
2782 && vect_mask_constant_operand_p (stmt, op_num))
2783 vector_type
2784 = build_same_sized_truth_vector_type (STMT_VINFO_VECTYPE (stmt_vinfo));
2785 else
2786 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
2787 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
2789 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
2790 && reduc_index != -1)
2792 op_num = reduc_index;
2793 op = gimple_op (stmt, op_num + 1);
2794 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
2795 we need either neutral operands or the original operands. See
2796 get_initial_def_for_reduction() for details. */
2797 switch (code)
2799 case WIDEN_SUM_EXPR:
2800 case DOT_PROD_EXPR:
2801 case SAD_EXPR:
2802 case PLUS_EXPR:
2803 case MINUS_EXPR:
2804 case BIT_IOR_EXPR:
2805 case BIT_XOR_EXPR:
2806 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2807 neutral_op = build_real (TREE_TYPE (op), dconst0);
2808 else
2809 neutral_op = build_int_cst (TREE_TYPE (op), 0);
2811 break;
2813 case MULT_EXPR:
2814 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2815 neutral_op = build_real (TREE_TYPE (op), dconst1);
2816 else
2817 neutral_op = build_int_cst (TREE_TYPE (op), 1);
2819 break;
2821 case BIT_AND_EXPR:
2822 neutral_op = build_int_cst (TREE_TYPE (op), -1);
2823 break;
2825 /* For MIN/MAX we don't have an easy neutral operand but
2826 the initial values can be used fine here. Only for
2827 a reduction chain we have to force a neutral element. */
2828 case MAX_EXPR:
2829 case MIN_EXPR:
2830 if (!GROUP_FIRST_ELEMENT (stmt_vinfo))
2831 neutral_op = NULL;
2832 else
2834 def_stmt = SSA_NAME_DEF_STMT (op);
2835 loop = (gimple_bb (stmt))->loop_father;
2836 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2837 loop_preheader_edge (loop));
2839 break;
2841 default:
2842 gcc_assert (!GROUP_FIRST_ELEMENT (stmt_vinfo));
2843 neutral_op = NULL;
2847 if (STMT_VINFO_DATA_REF (stmt_vinfo))
2849 is_store = true;
2850 op = gimple_assign_rhs1 (stmt);
2852 else
2853 is_store = false;
2855 gcc_assert (op);
2857 if (CONSTANT_CLASS_P (op))
2858 constant_p = true;
2859 else
2860 constant_p = false;
2862 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
2863 created vectors. It is greater than 1 if unrolling is performed.
2865 For example, we have two scalar operands, s1 and s2 (e.g., group of
2866 strided accesses of size two), while NUNITS is four (i.e., four scalars
2867 of this type can be packed in a vector). The output vector will contain
2868 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
2869 will be 2).
2871 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
2872 containing the operands.
2874 For example, NUNITS is four as before, and the group size is 8
2875 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
2876 {s5, s6, s7, s8}. */
2878 number_of_copies = nunits * number_of_vectors / group_size;
2880 number_of_places_left_in_vector = nunits;
2881 elts = XALLOCAVEC (tree, nunits);
2882 bool place_after_defs = false;
2883 for (j = 0; j < number_of_copies; j++)
2885 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
2887 if (is_store)
2888 op = gimple_assign_rhs1 (stmt);
2889 else
2891 switch (code)
2893 case COND_EXPR:
2895 tree cond = gimple_assign_rhs1 (stmt);
2896 if (TREE_CODE (cond) == SSA_NAME)
2897 op = gimple_op (stmt, op_num + 1);
2898 else if (op_num == 0 || op_num == 1)
2899 op = TREE_OPERAND (cond, op_num);
2900 else
2902 if (op_num == 2)
2903 op = gimple_assign_rhs2 (stmt);
2904 else
2905 op = gimple_assign_rhs3 (stmt);
2908 break;
2910 case CALL_EXPR:
2911 op = gimple_call_arg (stmt, op_num);
2912 break;
2914 case LSHIFT_EXPR:
2915 case RSHIFT_EXPR:
2916 case LROTATE_EXPR:
2917 case RROTATE_EXPR:
2918 op = gimple_op (stmt, op_num + 1);
2919 /* Unlike the other binary operators, shifts/rotates have
2920 the shift count being int, instead of the same type as
2921 the lhs, so make sure the scalar is the right type if
2922 we are dealing with vectors of
2923 long long/long/short/char. */
2924 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
2925 op = fold_convert (TREE_TYPE (vector_type), op);
2926 break;
2928 default:
2929 op = gimple_op (stmt, op_num + 1);
2930 break;
2934 if (reduc_index != -1)
2936 loop = (gimple_bb (stmt))->loop_father;
2937 def_stmt = SSA_NAME_DEF_STMT (op);
2939 gcc_assert (loop);
2941 /* Get the def before the loop. In reduction chain we have only
2942 one initial value. */
2943 if ((j != (number_of_copies - 1)
2944 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2945 && i != 0))
2946 && neutral_op)
2947 op = neutral_op;
2948 else
2949 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2950 loop_preheader_edge (loop));
2953 /* Create 'vect_ = {op0,op1,...,opn}'. */
2954 number_of_places_left_in_vector--;
2955 tree orig_op = op;
2956 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
2958 if (CONSTANT_CLASS_P (op))
2960 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
2962 /* Can't use VIEW_CONVERT_EXPR for booleans because
2963 of possibly different sizes of scalar value and
2964 vector element. */
2965 if (integer_zerop (op))
2966 op = build_int_cst (TREE_TYPE (vector_type), 0);
2967 else if (integer_onep (op))
2968 op = build_int_cst (TREE_TYPE (vector_type), 1);
2969 else
2970 gcc_unreachable ();
2972 else
2973 op = fold_unary (VIEW_CONVERT_EXPR,
2974 TREE_TYPE (vector_type), op);
2975 gcc_assert (op && CONSTANT_CLASS_P (op));
2977 else
2979 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
2980 gimple *init_stmt;
2981 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), op);
2982 init_stmt
2983 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR, op);
2984 gimple_seq_add_stmt (&ctor_seq, init_stmt);
2985 op = new_temp;
2988 elts[number_of_places_left_in_vector] = op;
2989 if (!CONSTANT_CLASS_P (op))
2990 constant_p = false;
2991 if (TREE_CODE (orig_op) == SSA_NAME
2992 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
2993 && STMT_VINFO_BB_VINFO (stmt_vinfo)
2994 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
2995 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
2996 place_after_defs = true;
2998 if (number_of_places_left_in_vector == 0)
3000 number_of_places_left_in_vector = nunits;
3002 if (constant_p)
3003 vec_cst = build_vector (vector_type, elts);
3004 else
3006 vec<constructor_elt, va_gc> *v;
3007 unsigned k;
3008 vec_alloc (v, nunits);
3009 for (k = 0; k < nunits; ++k)
3010 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
3011 vec_cst = build_constructor (vector_type, v);
3013 tree init;
3014 gimple_stmt_iterator gsi;
3015 if (place_after_defs)
3017 gsi = gsi_for_stmt
3018 (vect_find_last_scalar_stmt_in_slp (slp_node));
3019 init = vect_init_vector (stmt, vec_cst, vector_type, &gsi);
3021 else
3022 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
3023 if (ctor_seq != NULL)
3025 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
3026 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
3027 GSI_SAME_STMT);
3028 ctor_seq = NULL;
3030 voprnds.quick_push (init);
3031 place_after_defs = false;
3036 /* Since the vectors are created in the reverse order, we should invert
3037 them. */
3038 vec_num = voprnds.length ();
3039 for (j = vec_num; j != 0; j--)
3041 vop = voprnds[j - 1];
3042 vec_oprnds->quick_push (vop);
3045 voprnds.release ();
3047 /* In case that VF is greater than the unrolling factor needed for the SLP
3048 group of stmts, NUMBER_OF_VECTORS to be created is greater than
3049 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
3050 to replicate the vectors. */
3051 while (number_of_vectors > vec_oprnds->length ())
3053 tree neutral_vec = NULL;
3055 if (neutral_op)
3057 if (!neutral_vec)
3058 neutral_vec = build_vector_from_val (vector_type, neutral_op);
3060 vec_oprnds->quick_push (neutral_vec);
3062 else
3064 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
3065 vec_oprnds->quick_push (vop);
3071 /* Get vectorized definitions from SLP_NODE that contains corresponding
3072 vectorized def-stmts. */
3074 static void
3075 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
3077 tree vec_oprnd;
3078 gimple *vec_def_stmt;
3079 unsigned int i;
3081 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
3083 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
3085 gcc_assert (vec_def_stmt);
3086 vec_oprnd = gimple_get_lhs (vec_def_stmt);
3087 vec_oprnds->quick_push (vec_oprnd);
3092 /* Get vectorized definitions for SLP_NODE.
3093 If the scalar definitions are loop invariants or constants, collect them and
3094 call vect_get_constant_vectors() to create vector stmts.
3095 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
3096 must be stored in the corresponding child of SLP_NODE, and we call
3097 vect_get_slp_vect_defs () to retrieve them. */
3099 void
3100 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
3101 vec<vec<tree> > *vec_oprnds, int reduc_index)
3103 gimple *first_stmt;
3104 int number_of_vects = 0, i;
3105 unsigned int child_index = 0;
3106 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
3107 slp_tree child = NULL;
3108 vec<tree> vec_defs;
3109 tree oprnd;
3110 bool vectorized_defs;
3112 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
3113 FOR_EACH_VEC_ELT (ops, i, oprnd)
3115 /* For each operand we check if it has vectorized definitions in a child
3116 node or we need to create them (for invariants and constants). We
3117 check if the LHS of the first stmt of the next child matches OPRND.
3118 If it does, we found the correct child. Otherwise, we call
3119 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
3120 to check this child node for the next operand. */
3121 vectorized_defs = false;
3122 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
3124 child = SLP_TREE_CHILDREN (slp_node)[child_index];
3126 /* We have to check both pattern and original def, if available. */
3127 if (child)
3129 gimple *first_def = SLP_TREE_SCALAR_STMTS (child)[0];
3130 gimple *related
3131 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
3133 if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0)
3134 || (related
3135 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
3137 /* The number of vector defs is determined by the number of
3138 vector statements in the node from which we get those
3139 statements. */
3140 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
3141 vectorized_defs = true;
3142 child_index++;
3145 else
3146 child_index++;
3149 if (!vectorized_defs)
3151 if (i == 0)
3153 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
3154 /* Number of vector stmts was calculated according to LHS in
3155 vect_schedule_slp_instance (), fix it by replacing LHS with
3156 RHS, if necessary. See vect_get_smallest_scalar_type () for
3157 details. */
3158 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
3159 &rhs_size_unit);
3160 if (rhs_size_unit != lhs_size_unit)
3162 number_of_vects *= rhs_size_unit;
3163 number_of_vects /= lhs_size_unit;
3168 /* Allocate memory for vectorized defs. */
3169 vec_defs = vNULL;
3170 vec_defs.create (number_of_vects);
3172 /* For reduction defs we call vect_get_constant_vectors (), since we are
3173 looking for initial loop invariant values. */
3174 if (vectorized_defs && reduc_index == -1)
3175 /* The defs are already vectorized. */
3176 vect_get_slp_vect_defs (child, &vec_defs);
3177 else
3178 /* Build vectors from scalar defs. */
3179 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
3180 number_of_vects, reduc_index);
3182 vec_oprnds->quick_push (vec_defs);
3184 /* For reductions, we only need initial values. */
3185 if (reduc_index != -1)
3186 return;
3191 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
3192 building a vector of type MASK_TYPE from it) and two input vectors placed in
3193 DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
3194 shifting by STRIDE elements of DR_CHAIN for every copy.
3195 (STRIDE is the number of vectorized stmts for NODE divided by the number of
3196 copies).
3197 VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
3198 the created stmts must be inserted. */
3200 static inline void
3201 vect_create_mask_and_perm (gimple *stmt,
3202 tree mask, int first_vec_indx, int second_vec_indx,
3203 gimple_stmt_iterator *gsi, slp_tree node,
3204 tree vectype, vec<tree> dr_chain,
3205 int ncopies, int vect_stmts_counter)
3207 tree perm_dest;
3208 gimple *perm_stmt = NULL;
3209 int i, stride_in, stride_out;
3210 tree first_vec, second_vec, data_ref;
3212 stride_out = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
3213 stride_in = dr_chain.length () / ncopies;
3215 /* Initialize the vect stmts of NODE to properly insert the generated
3216 stmts later. */
3217 for (i = SLP_TREE_VEC_STMTS (node).length ();
3218 i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3219 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
3221 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
3222 for (i = 0; i < ncopies; i++)
3224 first_vec = dr_chain[first_vec_indx];
3225 second_vec = dr_chain[second_vec_indx];
3227 /* Generate the permute statement if necessary. */
3228 if (mask)
3230 perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
3231 first_vec, second_vec, mask);
3232 data_ref = make_ssa_name (perm_dest, perm_stmt);
3233 gimple_set_lhs (perm_stmt, data_ref);
3234 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
3236 else
3237 /* If mask was NULL_TREE generate the requested identity transform. */
3238 perm_stmt = SSA_NAME_DEF_STMT (first_vec);
3240 /* Store the vector statement in NODE. */
3241 SLP_TREE_VEC_STMTS (node)[stride_out * i + vect_stmts_counter]
3242 = perm_stmt;
3244 first_vec_indx += stride_in;
3245 second_vec_indx += stride_in;
3250 /* Generate vector permute statements from a list of loads in DR_CHAIN.
3251 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
3252 permute statements for the SLP node NODE of the SLP instance
3253 SLP_NODE_INSTANCE. */
3255 bool
3256 vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
3257 gimple_stmt_iterator *gsi, int vf,
3258 slp_instance slp_node_instance, bool analyze_only)
3260 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3261 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3262 tree mask_element_type = NULL_TREE, mask_type;
3263 int nunits, vec_index = 0;
3264 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3265 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
3266 int unroll_factor, mask_element, ncopies;
3267 unsigned char *mask;
3268 machine_mode mode;
3270 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3271 return false;
3273 stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info));
3275 mode = TYPE_MODE (vectype);
3277 if (!can_vec_perm_p (mode, false, NULL))
3279 if (dump_enabled_p ())
3281 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3282 "no vect permute for ");
3283 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3284 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3286 return false;
3289 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3290 same size as the vector element being permuted. */
3291 mask_element_type = lang_hooks.types.type_for_mode
3292 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
3293 mask_type = get_vectype_for_scalar_type (mask_element_type);
3294 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3295 mask = XALLOCAVEC (unsigned char, nunits);
3296 unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3298 /* Number of copies is determined by the final vectorization factor
3299 relatively to SLP_NODE_INSTANCE unrolling factor. */
3300 ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3302 /* Generate permutation masks for every NODE. Number of masks for each NODE
3303 is equal to GROUP_SIZE.
3304 E.g., we have a group of three nodes with three loads from the same
3305 location in each node, and the vector size is 4. I.e., we have a
3306 a0b0c0a1b1c1... sequence and we need to create the following vectors:
3307 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3308 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3311 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
3312 The last mask is illegal since we assume two operands for permute
3313 operation, and the mask element values can't be outside that range.
3314 Hence, the last mask must be converted into {2,5,5,5}.
3315 For the first two permutations we need the first and the second input
3316 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
3317 we need the second and the third vectors: {b1,c1,a2,b2} and
3318 {c2,a3,b3,c3}. */
3320 int vect_stmts_counter = 0;
3321 int index = 0;
3322 int first_vec_index = -1;
3323 int second_vec_index = -1;
3324 bool noop_p = true;
3326 for (int j = 0; j < unroll_factor; j++)
3328 for (int k = 0; k < group_size; k++)
3330 int i = (SLP_TREE_LOAD_PERMUTATION (node)[k]
3331 + j * STMT_VINFO_GROUP_SIZE (stmt_info));
3332 vec_index = i / nunits;
3333 mask_element = i % nunits;
3334 if (vec_index == first_vec_index
3335 || first_vec_index == -1)
3337 first_vec_index = vec_index;
3339 else if (vec_index == second_vec_index
3340 || second_vec_index == -1)
3342 second_vec_index = vec_index;
3343 mask_element += nunits;
3345 else
3347 if (dump_enabled_p ())
3349 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3350 "permutation requires at "
3351 "least three vectors ");
3352 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3353 stmt, 0);
3354 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3356 return false;
3359 gcc_assert (mask_element >= 0
3360 && mask_element < 2 * nunits);
3361 if (mask_element != index)
3362 noop_p = false;
3363 mask[index++] = mask_element;
3365 if (index == nunits)
3367 if (! noop_p
3368 && ! can_vec_perm_p (mode, false, mask))
3370 if (dump_enabled_p ())
3372 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3373 vect_location,
3374 "unsupported vect permute { ");
3375 for (i = 0; i < nunits; ++i)
3376 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ", mask[i]);
3377 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3379 return false;
3382 if (!analyze_only)
3384 tree mask_vec = NULL_TREE;
3386 if (! noop_p)
3388 tree *mask_elts = XALLOCAVEC (tree, nunits);
3389 for (int l = 0; l < nunits; ++l)
3390 mask_elts[l] = build_int_cst (mask_element_type,
3391 mask[l]);
3392 mask_vec = build_vector (mask_type, mask_elts);
3395 if (second_vec_index == -1)
3396 second_vec_index = first_vec_index;
3397 vect_create_mask_and_perm (stmt, mask_vec, first_vec_index,
3398 second_vec_index,
3399 gsi, node, vectype, dr_chain,
3400 ncopies, vect_stmts_counter++);
3403 index = 0;
3404 first_vec_index = -1;
3405 second_vec_index = -1;
3406 noop_p = true;
3411 return true;
3416 /* Vectorize SLP instance tree in postorder. */
3418 static bool
3419 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3420 unsigned int vectorization_factor)
3422 gimple *stmt;
3423 bool grouped_store, is_store;
3424 gimple_stmt_iterator si;
3425 stmt_vec_info stmt_info;
3426 unsigned int vec_stmts_size, nunits, group_size;
3427 tree vectype;
3428 int i;
3429 slp_tree child;
3431 if (!node)
3432 return false;
3434 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3435 vect_schedule_slp_instance (child, instance, vectorization_factor);
3437 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3438 stmt_info = vinfo_for_stmt (stmt);
3440 /* VECTYPE is the type of the destination. */
3441 vectype = STMT_VINFO_VECTYPE (stmt_info);
3442 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3443 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3445 /* For each SLP instance calculate number of vector stmts to be created
3446 for the scalar stmts in each node of the SLP tree. Number of vector
3447 elements in one vector iteration is the number of scalar elements in
3448 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3449 size.
3450 Unless this is a SLP reduction in which case the number of vector
3451 stmts is equal to the number of vector stmts of the children. */
3452 if (GROUP_FIRST_ELEMENT (stmt_info)
3453 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
3454 vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
3455 else
3456 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3458 if (!SLP_TREE_VEC_STMTS (node).exists ())
3460 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3461 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3464 if (dump_enabled_p ())
3466 dump_printf_loc (MSG_NOTE,vect_location,
3467 "------>vectorizing SLP node starting from: ");
3468 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3469 dump_printf (MSG_NOTE, "\n");
3472 /* Vectorized stmts go before the last scalar stmt which is where
3473 all uses are ready. */
3474 si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node));
3476 /* Mark the first element of the reduction chain as reduction to properly
3477 transform the node. In the analysis phase only the last element of the
3478 chain is marked as reduction. */
3479 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3480 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3482 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3483 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3486 /* Handle two-operation SLP nodes by vectorizing the group with
3487 both operations and then performing a merge. */
3488 if (SLP_TREE_TWO_OPERATORS (node))
3490 enum tree_code code0 = gimple_assign_rhs_code (stmt);
3491 enum tree_code ocode;
3492 gimple *ostmt;
3493 unsigned char *mask = XALLOCAVEC (unsigned char, group_size);
3494 bool allsame = true;
3495 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt)
3496 if (gimple_assign_rhs_code (ostmt) != code0)
3498 mask[i] = 1;
3499 allsame = false;
3500 ocode = gimple_assign_rhs_code (ostmt);
3502 else
3503 mask[i] = 0;
3504 if (!allsame)
3506 vec<gimple *> v0;
3507 vec<gimple *> v1;
3508 unsigned j;
3509 tree tmask = NULL_TREE;
3510 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3511 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3512 SLP_TREE_VEC_STMTS (node).truncate (0);
3513 gimple_assign_set_rhs_code (stmt, ocode);
3514 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3515 gimple_assign_set_rhs_code (stmt, code0);
3516 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3517 SLP_TREE_VEC_STMTS (node).truncate (0);
3518 tree meltype = build_nonstandard_integer_type
3519 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1);
3520 tree mvectype = get_same_sized_vectype (meltype, vectype);
3521 unsigned k = 0, l;
3522 for (j = 0; j < v0.length (); ++j)
3524 tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype));
3525 for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l)
3527 if (k >= group_size)
3528 k = 0;
3529 melts[l] = build_int_cst
3530 (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l);
3532 tmask = build_vector (mvectype, melts);
3534 /* ??? Not all targets support a VEC_PERM_EXPR with a
3535 constant mask that would translate to a vec_merge RTX
3536 (with their vec_perm_const_ok). We can either not
3537 vectorize in that case or let veclower do its job.
3538 Unfortunately that isn't too great and at least for
3539 plus/minus we'd eventually like to match targets
3540 vector addsub instructions. */
3541 gimple *vstmt;
3542 vstmt = gimple_build_assign (make_ssa_name (vectype),
3543 VEC_PERM_EXPR,
3544 gimple_assign_lhs (v0[j]),
3545 gimple_assign_lhs (v1[j]), tmask);
3546 vect_finish_stmt_generation (stmt, vstmt, &si);
3547 SLP_TREE_VEC_STMTS (node).quick_push (vstmt);
3549 v0.release ();
3550 v1.release ();
3551 return false;
3554 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3555 return is_store;
3558 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3559 For loop vectorization this is done in vectorizable_call, but for SLP
3560 it needs to be deferred until end of vect_schedule_slp, because multiple
3561 SLP instances may refer to the same scalar stmt. */
3563 static void
3564 vect_remove_slp_scalar_calls (slp_tree node)
3566 gimple *stmt, *new_stmt;
3567 gimple_stmt_iterator gsi;
3568 int i;
3569 slp_tree child;
3570 tree lhs;
3571 stmt_vec_info stmt_info;
3573 if (!node)
3574 return;
3576 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3577 vect_remove_slp_scalar_calls (child);
3579 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3581 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3582 continue;
3583 stmt_info = vinfo_for_stmt (stmt);
3584 if (stmt_info == NULL
3585 || is_pattern_stmt_p (stmt_info)
3586 || !PURE_SLP_STMT (stmt_info))
3587 continue;
3588 lhs = gimple_call_lhs (stmt);
3589 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3590 set_vinfo_for_stmt (new_stmt, stmt_info);
3591 set_vinfo_for_stmt (stmt, NULL);
3592 STMT_VINFO_STMT (stmt_info) = new_stmt;
3593 gsi = gsi_for_stmt (stmt);
3594 gsi_replace (&gsi, new_stmt, false);
3595 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3599 /* Generate vector code for all SLP instances in the loop/basic block. */
3601 bool
3602 vect_schedule_slp (vec_info *vinfo)
3604 vec<slp_instance> slp_instances;
3605 slp_instance instance;
3606 unsigned int i, vf;
3607 bool is_store = false;
3609 slp_instances = vinfo->slp_instances;
3610 if (is_a <loop_vec_info> (vinfo))
3611 vf = as_a <loop_vec_info> (vinfo)->vectorization_factor;
3612 else
3613 vf = 1;
3615 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3617 /* Schedule the tree of INSTANCE. */
3618 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3619 instance, vf);
3620 if (dump_enabled_p ())
3621 dump_printf_loc (MSG_NOTE, vect_location,
3622 "vectorizing stmts using SLP.\n");
3625 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3627 slp_tree root = SLP_INSTANCE_TREE (instance);
3628 gimple *store;
3629 unsigned int j;
3630 gimple_stmt_iterator gsi;
3632 /* Remove scalar call stmts. Do not do this for basic-block
3633 vectorization as not all uses may be vectorized.
3634 ??? Why should this be necessary? DCE should be able to
3635 remove the stmts itself.
3636 ??? For BB vectorization we can as well remove scalar
3637 stmts starting from the SLP tree root if they have no
3638 uses. */
3639 if (is_a <loop_vec_info> (vinfo))
3640 vect_remove_slp_scalar_calls (root);
3642 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3643 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3645 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3646 break;
3648 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3649 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3650 /* Free the attached stmt_vec_info and remove the stmt. */
3651 gsi = gsi_for_stmt (store);
3652 unlink_stmt_vdef (store);
3653 gsi_remove (&gsi, true);
3654 release_defs (store);
3655 free_stmt_vec_info (store);
3659 return is_store;