/cp
[official-gcc.git] / gcc / tree-vect-slp.c
bloba240227607279735b2877766311e226b43fc63e1
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 "dumpfile.h"
26 #include "backend.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "rtl.h"
30 #include "ssa.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "target.h"
35 #include "gimple-pretty-print.h"
36 #include "internal-fn.h"
37 #include "gimple-iterator.h"
38 #include "tree-pass.h"
39 #include "cfgloop.h"
40 #include "flags.h"
41 #include "insn-config.h"
42 #include "expmed.h"
43 #include "dojump.h"
44 #include "explow.h"
45 #include "calls.h"
46 #include "emit-rtl.h"
47 #include "varasm.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "recog.h" /* FIXME: for insn_data */
51 #include "insn-codes.h"
52 #include "optabs.h"
53 #include "tree-vectorizer.h"
54 #include "langhooks.h"
55 #include "gimple-walk.h"
57 /* Extract the location of the basic block in the source code.
58 Return the basic block location if succeed and NULL if not. */
60 source_location
61 find_bb_location (basic_block bb)
63 gimple stmt = NULL;
64 gimple_stmt_iterator si;
66 if (!bb)
67 return UNKNOWN_LOCATION;
69 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
71 stmt = gsi_stmt (si);
72 if (gimple_location (stmt) != UNKNOWN_LOCATION)
73 return gimple_location (stmt);
76 return UNKNOWN_LOCATION;
80 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
82 static void
83 vect_free_slp_tree (slp_tree node)
85 int i;
86 slp_tree child;
88 if (!node)
89 return;
91 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
92 vect_free_slp_tree (child);
94 SLP_TREE_CHILDREN (node).release ();
95 SLP_TREE_SCALAR_STMTS (node).release ();
96 SLP_TREE_VEC_STMTS (node).release ();
97 SLP_TREE_LOAD_PERMUTATION (node).release ();
99 free (node);
103 /* Free the memory allocated for the SLP instance. */
105 void
106 vect_free_slp_instance (slp_instance instance)
108 vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
109 SLP_INSTANCE_LOADS (instance).release ();
110 free (instance);
114 /* Create an SLP node for SCALAR_STMTS. */
116 static slp_tree
117 vect_create_new_slp_node (vec<gimple> scalar_stmts)
119 slp_tree node;
120 gimple stmt = scalar_stmts[0];
121 unsigned int nops;
123 if (is_gimple_call (stmt))
124 nops = gimple_call_num_args (stmt);
125 else if (is_gimple_assign (stmt))
127 nops = gimple_num_ops (stmt) - 1;
128 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
129 nops++;
131 else
132 return NULL;
134 node = XNEW (struct _slp_tree);
135 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
136 SLP_TREE_VEC_STMTS (node).create (0);
137 SLP_TREE_CHILDREN (node).create (nops);
138 SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
139 SLP_TREE_TWO_OPERATORS (node) = false;
141 return node;
145 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
146 operand. */
147 static vec<slp_oprnd_info>
148 vect_create_oprnd_info (int nops, int group_size)
150 int i;
151 slp_oprnd_info oprnd_info;
152 vec<slp_oprnd_info> oprnds_info;
154 oprnds_info.create (nops);
155 for (i = 0; i < nops; i++)
157 oprnd_info = XNEW (struct _slp_oprnd_info);
158 oprnd_info->def_stmts.create (group_size);
159 oprnd_info->first_dt = vect_uninitialized_def;
160 oprnd_info->first_op_type = NULL_TREE;
161 oprnd_info->first_pattern = false;
162 oprnd_info->second_pattern = false;
163 oprnds_info.quick_push (oprnd_info);
166 return oprnds_info;
170 /* Free operands info. */
172 static void
173 vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
175 int i;
176 slp_oprnd_info oprnd_info;
178 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
180 oprnd_info->def_stmts.release ();
181 XDELETE (oprnd_info);
184 oprnds_info.release ();
188 /* Find the place of the data-ref in STMT in the interleaving chain that starts
189 from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
191 static int
192 vect_get_place_in_interleaving_chain (gimple stmt, gimple first_stmt)
194 gimple next_stmt = first_stmt;
195 int result = 0;
197 if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
198 return -1;
202 if (next_stmt == stmt)
203 return result;
204 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
205 if (next_stmt)
206 result += GROUP_GAP (vinfo_for_stmt (next_stmt));
208 while (next_stmt);
210 return -1;
214 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
215 they are of a valid type and that they match the defs of the first stmt of
216 the SLP group (stored in OPRNDS_INFO). If there was a fatal error
217 return -1, if the error could be corrected by swapping operands of the
218 operation return 1, if everything is ok return 0. */
220 static int
221 vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
222 gimple stmt, unsigned stmt_num,
223 vec<slp_oprnd_info> *oprnds_info)
225 tree oprnd;
226 unsigned int i, number_of_oprnds;
227 tree def;
228 gimple def_stmt;
229 enum vect_def_type dt = vect_uninitialized_def;
230 struct loop *loop = NULL;
231 bool pattern = false;
232 slp_oprnd_info oprnd_info;
233 int first_op_idx = 1;
234 bool commutative = false;
235 bool first_op_cond = false;
236 bool first = stmt_num == 0;
237 bool second = stmt_num == 1;
239 if (loop_vinfo)
240 loop = LOOP_VINFO_LOOP (loop_vinfo);
242 if (is_gimple_call (stmt))
244 number_of_oprnds = gimple_call_num_args (stmt);
245 first_op_idx = 3;
247 else if (is_gimple_assign (stmt))
249 enum tree_code code = gimple_assign_rhs_code (stmt);
250 number_of_oprnds = gimple_num_ops (stmt) - 1;
251 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
253 first_op_cond = true;
254 commutative = true;
255 number_of_oprnds++;
257 else
258 commutative = commutative_tree_code (code);
260 else
261 return -1;
263 bool swapped = false;
264 for (i = 0; i < number_of_oprnds; i++)
266 again:
267 if (first_op_cond)
269 if (i == 0 || i == 1)
270 oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx),
271 swapped ? !i : i);
272 else
273 oprnd = gimple_op (stmt, first_op_idx + i - 1);
275 else
276 oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i));
278 oprnd_info = (*oprnds_info)[i];
280 if (!vect_is_simple_use (oprnd, NULL, loop_vinfo, bb_vinfo, &def_stmt,
281 &def, &dt))
283 if (dump_enabled_p ())
285 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
286 "Build SLP failed: can't analyze def for ");
287 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
288 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
291 return -1;
294 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
295 from the pattern. Check that all the stmts of the node are in the
296 pattern. */
297 if (def_stmt && gimple_bb (def_stmt)
298 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
299 || (!loop && gimple_bb (def_stmt) == BB_VINFO_BB (bb_vinfo)
300 && gimple_code (def_stmt) != GIMPLE_PHI))
301 && vinfo_for_stmt (def_stmt)
302 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
303 && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
304 && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
306 pattern = true;
307 if (!first && !oprnd_info->first_pattern
308 /* Allow different pattern state for the defs of the
309 first stmt in reduction chains. */
310 && (oprnd_info->first_dt != vect_reduction_def
311 || (!second && !oprnd_info->second_pattern)))
313 if (i == 0
314 && !swapped
315 && commutative)
317 swapped = true;
318 goto again;
321 if (dump_enabled_p ())
323 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
324 "Build SLP failed: some of the stmts"
325 " are in a pattern, and others are not ");
326 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
327 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
330 return 1;
333 def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
334 dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
336 if (dt == vect_unknown_def_type)
338 if (dump_enabled_p ())
339 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
340 "Unsupported pattern.\n");
341 return -1;
344 switch (gimple_code (def_stmt))
346 case GIMPLE_PHI:
347 def = gimple_phi_result (def_stmt);
348 break;
350 case GIMPLE_ASSIGN:
351 def = gimple_assign_lhs (def_stmt);
352 break;
354 default:
355 if (dump_enabled_p ())
356 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
357 "unsupported defining stmt:\n");
358 return -1;
362 if (second)
363 oprnd_info->second_pattern = pattern;
365 if (first)
367 oprnd_info->first_dt = dt;
368 oprnd_info->first_pattern = pattern;
369 oprnd_info->first_op_type = TREE_TYPE (oprnd);
371 else
373 /* Not first stmt of the group, check that the def-stmt/s match
374 the def-stmt/s of the first stmt. Allow different definition
375 types for reduction chains: the first stmt must be a
376 vect_reduction_def (a phi node), and the rest
377 vect_internal_def. */
378 if (((oprnd_info->first_dt != dt
379 && !(oprnd_info->first_dt == vect_reduction_def
380 && dt == vect_internal_def)
381 && !((oprnd_info->first_dt == vect_external_def
382 || oprnd_info->first_dt == vect_constant_def)
383 && (dt == vect_external_def
384 || dt == vect_constant_def)))
385 || !types_compatible_p (oprnd_info->first_op_type,
386 TREE_TYPE (oprnd))))
388 /* Try swapping operands if we got a mismatch. */
389 if (i == 0
390 && !swapped
391 && commutative)
393 swapped = true;
394 goto again;
397 if (dump_enabled_p ())
398 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
399 "Build SLP failed: different types\n");
401 return 1;
405 /* Check the types of the definitions. */
406 switch (dt)
408 case vect_constant_def:
409 case vect_external_def:
410 case vect_reduction_def:
411 break;
413 case vect_internal_def:
414 oprnd_info->def_stmts.quick_push (def_stmt);
415 break;
417 default:
418 /* FORNOW: Not supported. */
419 if (dump_enabled_p ())
421 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
422 "Build SLP failed: illegal type of def ");
423 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, def);
424 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
427 return -1;
431 /* Swap operands. */
432 if (swapped)
434 if (first_op_cond)
436 tree cond = gimple_assign_rhs1 (stmt);
437 swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
438 &TREE_OPERAND (cond, 1));
439 TREE_SET_CODE (cond, swap_tree_comparison (TREE_CODE (cond)));
441 else
442 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
443 gimple_assign_rhs2_ptr (stmt));
446 return 0;
450 /* Verify if the scalar stmts STMTS are isomorphic, require data
451 permutation or are of unsupported types of operation. Return
452 true if they are, otherwise return false and indicate in *MATCHES
453 which stmts are not isomorphic to the first one. If MATCHES[0]
454 is false then this indicates the comparison could not be
455 carried out or the stmts will never be vectorized by SLP. */
457 static bool
458 vect_build_slp_tree_1 (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
459 vec<gimple> stmts, unsigned int group_size,
460 unsigned nops, unsigned int *max_nunits,
461 unsigned int vectorization_factor, bool *matches,
462 bool *two_operators)
464 unsigned int i;
465 gimple first_stmt = stmts[0], stmt = stmts[0];
466 enum tree_code first_stmt_code = ERROR_MARK;
467 enum tree_code alt_stmt_code = ERROR_MARK;
468 enum tree_code rhs_code = ERROR_MARK;
469 enum tree_code first_cond_code = ERROR_MARK;
470 tree lhs;
471 bool need_same_oprnds = false;
472 tree vectype = NULL_TREE, scalar_type, first_op1 = NULL_TREE;
473 optab optab;
474 int icode;
475 machine_mode optab_op2_mode;
476 machine_mode vec_mode;
477 HOST_WIDE_INT dummy;
478 gimple first_load = NULL, prev_first_load = NULL;
479 tree cond;
481 /* For every stmt in NODE find its def stmt/s. */
482 FOR_EACH_VEC_ELT (stmts, i, stmt)
484 matches[i] = false;
486 if (dump_enabled_p ())
488 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
489 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
490 dump_printf (MSG_NOTE, "\n");
493 /* Fail to vectorize statements marked as unvectorizable. */
494 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
496 if (dump_enabled_p ())
498 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
499 "Build SLP failed: unvectorizable statement ");
500 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
501 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
503 /* Fatal mismatch. */
504 matches[0] = false;
505 return false;
508 lhs = gimple_get_lhs (stmt);
509 if (lhs == NULL_TREE)
511 if (dump_enabled_p ())
513 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
514 "Build SLP failed: not GIMPLE_ASSIGN nor "
515 "GIMPLE_CALL ");
516 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
517 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
519 /* Fatal mismatch. */
520 matches[0] = false;
521 return false;
524 if (is_gimple_assign (stmt)
525 && gimple_assign_rhs_code (stmt) == COND_EXPR
526 && (cond = gimple_assign_rhs1 (stmt))
527 && !COMPARISON_CLASS_P (cond))
529 if (dump_enabled_p ())
531 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
532 "Build SLP failed: condition is not "
533 "comparison ");
534 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
535 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
537 /* Fatal mismatch. */
538 matches[0] = false;
539 return false;
542 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
543 vectype = get_vectype_for_scalar_type (scalar_type);
544 if (!vectype)
546 if (dump_enabled_p ())
548 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
549 "Build SLP failed: unsupported data-type ");
550 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
551 scalar_type);
552 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
554 /* Fatal mismatch. */
555 matches[0] = false;
556 return false;
559 /* If populating the vector type requires unrolling then fail
560 before adjusting *max_nunits for basic-block vectorization. */
561 if (bb_vinfo
562 && TYPE_VECTOR_SUBPARTS (vectype) > group_size)
564 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
565 "Build SLP failed: unrolling required "
566 "in basic block SLP\n");
567 /* Fatal mismatch. */
568 matches[0] = false;
569 return false;
572 /* In case of multiple types we need to detect the smallest type. */
573 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
575 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
576 if (bb_vinfo)
577 vectorization_factor = *max_nunits;
580 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
582 rhs_code = CALL_EXPR;
583 if (gimple_call_internal_p (call_stmt)
584 || gimple_call_tail_p (call_stmt)
585 || gimple_call_noreturn_p (call_stmt)
586 || !gimple_call_nothrow_p (call_stmt)
587 || gimple_call_chain (call_stmt))
589 if (dump_enabled_p ())
591 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
592 "Build SLP failed: unsupported call type ");
593 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
594 call_stmt, 0);
595 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
597 /* Fatal mismatch. */
598 matches[0] = false;
599 return false;
602 else
603 rhs_code = gimple_assign_rhs_code (stmt);
605 /* Check the operation. */
606 if (i == 0)
608 first_stmt_code = rhs_code;
610 /* Shift arguments should be equal in all the packed stmts for a
611 vector shift with scalar shift operand. */
612 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
613 || rhs_code == LROTATE_EXPR
614 || rhs_code == RROTATE_EXPR)
616 vec_mode = TYPE_MODE (vectype);
618 /* First see if we have a vector/vector shift. */
619 optab = optab_for_tree_code (rhs_code, vectype,
620 optab_vector);
622 if (!optab
623 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
625 /* No vector/vector shift, try for a vector/scalar shift. */
626 optab = optab_for_tree_code (rhs_code, vectype,
627 optab_scalar);
629 if (!optab)
631 if (dump_enabled_p ())
632 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
633 "Build SLP failed: no optab.\n");
634 /* Fatal mismatch. */
635 matches[0] = false;
636 return false;
638 icode = (int) optab_handler (optab, vec_mode);
639 if (icode == CODE_FOR_nothing)
641 if (dump_enabled_p ())
642 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
643 "Build SLP failed: "
644 "op not supported by target.\n");
645 /* Fatal mismatch. */
646 matches[0] = false;
647 return false;
649 optab_op2_mode = insn_data[icode].operand[2].mode;
650 if (!VECTOR_MODE_P (optab_op2_mode))
652 need_same_oprnds = true;
653 first_op1 = gimple_assign_rhs2 (stmt);
657 else if (rhs_code == WIDEN_LSHIFT_EXPR)
659 need_same_oprnds = true;
660 first_op1 = gimple_assign_rhs2 (stmt);
663 else
665 if (first_stmt_code != rhs_code
666 && alt_stmt_code == ERROR_MARK)
667 alt_stmt_code = rhs_code;
668 if (first_stmt_code != rhs_code
669 && (first_stmt_code != IMAGPART_EXPR
670 || rhs_code != REALPART_EXPR)
671 && (first_stmt_code != REALPART_EXPR
672 || rhs_code != IMAGPART_EXPR)
673 /* Handle mismatches in plus/minus by computing both
674 and merging the results. */
675 && !((first_stmt_code == PLUS_EXPR
676 || first_stmt_code == MINUS_EXPR)
677 && (alt_stmt_code == PLUS_EXPR
678 || alt_stmt_code == MINUS_EXPR)
679 && rhs_code == alt_stmt_code)
680 && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
681 && (first_stmt_code == ARRAY_REF
682 || first_stmt_code == BIT_FIELD_REF
683 || first_stmt_code == INDIRECT_REF
684 || first_stmt_code == COMPONENT_REF
685 || first_stmt_code == MEM_REF)))
687 if (dump_enabled_p ())
689 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
690 "Build SLP failed: different operation "
691 "in stmt ");
692 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
693 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
694 "original stmt ");
695 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
696 first_stmt, 0);
698 /* Mismatch. */
699 continue;
702 if (need_same_oprnds
703 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
705 if (dump_enabled_p ())
707 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
708 "Build SLP failed: different shift "
709 "arguments in ");
710 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
711 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
713 /* Mismatch. */
714 continue;
717 if (rhs_code == CALL_EXPR)
719 gimple first_stmt = stmts[0];
720 if (gimple_call_num_args (stmt) != nops
721 || !operand_equal_p (gimple_call_fn (first_stmt),
722 gimple_call_fn (stmt), 0)
723 || gimple_call_fntype (first_stmt)
724 != gimple_call_fntype (stmt))
726 if (dump_enabled_p ())
728 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
729 "Build SLP failed: different calls in ");
730 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
731 stmt, 0);
732 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
734 /* Mismatch. */
735 continue;
740 /* Grouped store or load. */
741 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
743 if (REFERENCE_CLASS_P (lhs))
745 /* Store. */
748 else
750 /* Load. */
751 /* Check that the size of interleaved loads group is not
752 greater than the SLP group size. */
753 unsigned ncopies
754 = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
755 if (loop_vinfo
756 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
757 && ((GROUP_SIZE (vinfo_for_stmt (stmt))
758 - GROUP_GAP (vinfo_for_stmt (stmt)))
759 > ncopies * group_size))
761 if (dump_enabled_p ())
763 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
764 "Build SLP failed: the number "
765 "of interleaved loads is greater than "
766 "the SLP group size ");
767 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
768 stmt, 0);
769 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
771 /* Fatal mismatch. */
772 matches[0] = false;
773 return false;
776 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
777 if (prev_first_load)
779 /* Check that there are no loads from different interleaving
780 chains in the same node. */
781 if (prev_first_load != first_load)
783 if (dump_enabled_p ())
785 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
786 vect_location,
787 "Build SLP failed: different "
788 "interleaving chains in one node ");
789 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
790 stmt, 0);
791 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
793 /* Mismatch. */
794 continue;
797 else
798 prev_first_load = first_load;
800 } /* Grouped access. */
801 else
803 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
805 /* Not grouped load. */
806 if (dump_enabled_p ())
808 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
809 "Build SLP failed: not grouped load ");
810 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
811 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
814 /* FORNOW: Not grouped loads are not supported. */
815 /* Fatal mismatch. */
816 matches[0] = false;
817 return false;
820 /* Not memory operation. */
821 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
822 && TREE_CODE_CLASS (rhs_code) != tcc_unary
823 && TREE_CODE_CLASS (rhs_code) != tcc_expression
824 && rhs_code != CALL_EXPR)
826 if (dump_enabled_p ())
828 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
829 "Build SLP failed: operation");
830 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
831 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
832 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
834 /* Fatal mismatch. */
835 matches[0] = false;
836 return false;
839 if (rhs_code == COND_EXPR)
841 tree cond_expr = gimple_assign_rhs1 (stmt);
843 if (i == 0)
844 first_cond_code = TREE_CODE (cond_expr);
845 else if (first_cond_code != TREE_CODE (cond_expr))
847 if (dump_enabled_p ())
849 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
850 "Build SLP failed: different"
851 " operation");
852 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
853 stmt, 0);
854 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
856 /* Mismatch. */
857 continue;
862 matches[i] = true;
865 for (i = 0; i < group_size; ++i)
866 if (!matches[i])
867 return false;
869 /* If we allowed a two-operation SLP node verify the target can cope
870 with the permute we are going to use. */
871 if (alt_stmt_code != ERROR_MARK
872 && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference)
874 unsigned char *sel
875 = XALLOCAVEC (unsigned char, TYPE_VECTOR_SUBPARTS (vectype));
876 for (i = 0; i < TYPE_VECTOR_SUBPARTS (vectype); ++i)
878 sel[i] = i;
879 if (gimple_assign_rhs_code (stmts[i % group_size]) == alt_stmt_code)
880 sel[i] += TYPE_VECTOR_SUBPARTS (vectype);
882 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
884 for (i = 0; i < group_size; ++i)
885 if (gimple_assign_rhs_code (stmts[i]) == alt_stmt_code)
887 matches[i] = false;
888 if (dump_enabled_p ())
890 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
891 "Build SLP failed: different operation "
892 "in stmt ");
893 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
894 stmts[i], 0);
895 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
896 "original stmt ");
897 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
898 first_stmt, 0);
901 return false;
903 *two_operators = true;
906 return true;
909 /* Recursively build an SLP tree starting from NODE.
910 Fail (and return a value not equal to zero) if def-stmts are not
911 isomorphic, require data permutation or are of unsupported types of
912 operation. Otherwise, return 0.
913 The value returned is the depth in the SLP tree where a mismatch
914 was found. */
916 static bool
917 vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
918 slp_tree *node, unsigned int group_size,
919 unsigned int *max_nunits,
920 vec<slp_tree> *loads,
921 unsigned int vectorization_factor,
922 bool *matches, unsigned *npermutes, unsigned *tree_size,
923 unsigned max_tree_size)
925 unsigned nops, i, this_tree_size = 0;
926 gimple stmt;
928 matches[0] = false;
930 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
931 if (is_gimple_call (stmt))
932 nops = gimple_call_num_args (stmt);
933 else if (is_gimple_assign (stmt))
935 nops = gimple_num_ops (stmt) - 1;
936 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
937 nops++;
939 else
940 return false;
942 bool two_operators = false;
943 if (!vect_build_slp_tree_1 (loop_vinfo, bb_vinfo,
944 SLP_TREE_SCALAR_STMTS (*node), group_size, nops,
945 max_nunits, vectorization_factor, matches,
946 &two_operators))
947 return false;
948 SLP_TREE_TWO_OPERATORS (*node) = two_operators;
950 /* If the SLP node is a load, terminate the recursion. */
951 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
952 && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
954 loads->safe_push (*node);
955 return true;
958 /* Get at the operands, verifying they are compatible. */
959 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
960 slp_oprnd_info oprnd_info;
961 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (*node), i, stmt)
963 switch (vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo,
964 stmt, i, &oprnds_info))
966 case 0:
967 break;
968 case -1:
969 matches[0] = false;
970 vect_free_oprnd_info (oprnds_info);
971 return false;
972 case 1:
973 matches[i] = false;
974 break;
977 for (i = 0; i < group_size; ++i)
978 if (!matches[i])
980 vect_free_oprnd_info (oprnds_info);
981 return false;
984 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
986 /* Create SLP_TREE nodes for the definition node/s. */
987 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
989 slp_tree child;
990 unsigned old_nloads = loads->length ();
991 unsigned old_max_nunits = *max_nunits;
993 if (oprnd_info->first_dt != vect_internal_def)
994 continue;
996 if (++this_tree_size > max_tree_size)
998 vect_free_oprnd_info (oprnds_info);
999 return false;
1002 child = vect_create_new_slp_node (oprnd_info->def_stmts);
1003 if (!child)
1005 vect_free_oprnd_info (oprnds_info);
1006 return false;
1009 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
1010 group_size, max_nunits, loads,
1011 vectorization_factor, matches,
1012 npermutes, &this_tree_size, max_tree_size))
1014 /* If we have all children of child built up from scalars then just
1015 throw that away and build it up this node from scalars. */
1016 if (!SLP_TREE_CHILDREN (child).is_empty ())
1018 unsigned int j;
1019 slp_tree grandchild;
1021 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1022 if (grandchild != NULL)
1023 break;
1024 if (!grandchild)
1026 /* Roll back. */
1027 *max_nunits = old_max_nunits;
1028 loads->truncate (old_nloads);
1029 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1030 vect_free_slp_tree (grandchild);
1031 SLP_TREE_CHILDREN (child).truncate (0);
1033 dump_printf_loc (MSG_NOTE, vect_location,
1034 "Building parent vector operands from "
1035 "scalars instead\n");
1036 oprnd_info->def_stmts = vNULL;
1037 vect_free_slp_tree (child);
1038 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1039 continue;
1043 oprnd_info->def_stmts = vNULL;
1044 SLP_TREE_CHILDREN (*node).quick_push (child);
1045 continue;
1048 /* If the SLP build failed fatally and we analyze a basic-block
1049 simply treat nodes we fail to build as externally defined
1050 (and thus build vectors from the scalar defs).
1051 The cost model will reject outright expensive cases.
1052 ??? This doesn't treat cases where permutation ultimatively
1053 fails (or we don't try permutation below). Ideally we'd
1054 even compute a permutation that will end up with the maximum
1055 SLP tree size... */
1056 if (bb_vinfo
1057 && !matches[0]
1058 /* ??? Rejecting patterns this way doesn't work. We'd have to
1059 do extra work to cancel the pattern so the uses see the
1060 scalar version. */
1061 && !is_pattern_stmt_p (vinfo_for_stmt (stmt)))
1063 unsigned int j;
1064 slp_tree grandchild;
1066 /* Roll back. */
1067 *max_nunits = old_max_nunits;
1068 loads->truncate (old_nloads);
1069 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1070 vect_free_slp_tree (grandchild);
1071 SLP_TREE_CHILDREN (child).truncate (0);
1073 dump_printf_loc (MSG_NOTE, vect_location,
1074 "Building vector operands from scalars\n");
1075 oprnd_info->def_stmts = vNULL;
1076 vect_free_slp_tree (child);
1077 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1078 continue;
1081 /* If the SLP build for operand zero failed and operand zero
1082 and one can be commutated try that for the scalar stmts
1083 that failed the match. */
1084 if (i == 0
1085 /* A first scalar stmt mismatch signals a fatal mismatch. */
1086 && matches[0]
1087 /* ??? For COND_EXPRs we can swap the comparison operands
1088 as well as the arms under some constraints. */
1089 && nops == 2
1090 && oprnds_info[1]->first_dt == vect_internal_def
1091 && is_gimple_assign (stmt)
1092 && commutative_tree_code (gimple_assign_rhs_code (stmt))
1093 && !SLP_TREE_TWO_OPERATORS (*node)
1094 /* Do so only if the number of not successful permutes was nor more
1095 than a cut-ff as re-trying the recursive match on
1096 possibly each level of the tree would expose exponential
1097 behavior. */
1098 && *npermutes < 4)
1100 unsigned int j;
1101 slp_tree grandchild;
1103 /* Roll back. */
1104 *max_nunits = old_max_nunits;
1105 loads->truncate (old_nloads);
1106 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1107 vect_free_slp_tree (grandchild);
1108 SLP_TREE_CHILDREN (child).truncate (0);
1110 /* Swap mismatched definition stmts. */
1111 dump_printf_loc (MSG_NOTE, vect_location,
1112 "Re-trying with swapped operands of stmts ");
1113 for (j = 0; j < group_size; ++j)
1114 if (!matches[j])
1116 std::swap (oprnds_info[0]->def_stmts[j],
1117 oprnds_info[1]->def_stmts[j]);
1118 dump_printf (MSG_NOTE, "%d ", j);
1120 dump_printf (MSG_NOTE, "\n");
1121 /* And try again with scratch 'matches' ... */
1122 bool *tem = XALLOCAVEC (bool, group_size);
1123 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
1124 group_size, max_nunits, loads,
1125 vectorization_factor,
1126 tem, npermutes, &this_tree_size,
1127 max_tree_size))
1129 /* ... so if successful we can apply the operand swapping
1130 to the GIMPLE IL. This is necessary because for example
1131 vect_get_slp_defs uses operand indexes and thus expects
1132 canonical operand order. */
1133 for (j = 0; j < group_size; ++j)
1134 if (!matches[j])
1136 gimple stmt = SLP_TREE_SCALAR_STMTS (*node)[j];
1137 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1138 gimple_assign_rhs2_ptr (stmt));
1140 oprnd_info->def_stmts = vNULL;
1141 SLP_TREE_CHILDREN (*node).quick_push (child);
1142 continue;
1145 ++*npermutes;
1148 oprnd_info->def_stmts = vNULL;
1149 vect_free_slp_tree (child);
1150 vect_free_oprnd_info (oprnds_info);
1151 return false;
1154 if (tree_size)
1155 *tree_size += this_tree_size;
1157 vect_free_oprnd_info (oprnds_info);
1158 return true;
1161 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
1163 static void
1164 vect_print_slp_tree (int dump_kind, slp_tree node)
1166 int i;
1167 gimple stmt;
1168 slp_tree child;
1170 if (!node)
1171 return;
1173 dump_printf (dump_kind, "node ");
1174 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1176 dump_printf (dump_kind, "\n\tstmt %d ", i);
1177 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
1179 dump_printf (dump_kind, "\n");
1181 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1182 vect_print_slp_tree (dump_kind, child);
1186 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1187 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
1188 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1189 stmts in NODE are to be marked. */
1191 static void
1192 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1194 int i;
1195 gimple stmt;
1196 slp_tree child;
1198 if (!node)
1199 return;
1201 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1202 if (j < 0 || i == j)
1203 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1205 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1206 vect_mark_slp_stmts (child, mark, j);
1210 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1212 static void
1213 vect_mark_slp_stmts_relevant (slp_tree node)
1215 int i;
1216 gimple stmt;
1217 stmt_vec_info stmt_info;
1218 slp_tree child;
1220 if (!node)
1221 return;
1223 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1225 stmt_info = vinfo_for_stmt (stmt);
1226 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1227 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1228 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1231 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1232 vect_mark_slp_stmts_relevant (child);
1236 /* Rearrange the statements of NODE according to PERMUTATION. */
1238 static void
1239 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1240 vec<unsigned> permutation)
1242 gimple stmt;
1243 vec<gimple> tmp_stmts;
1244 unsigned int i;
1245 slp_tree child;
1247 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1248 vect_slp_rearrange_stmts (child, group_size, permutation);
1250 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1251 tmp_stmts.create (group_size);
1252 tmp_stmts.quick_grow_cleared (group_size);
1254 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1255 tmp_stmts[permutation[i]] = stmt;
1257 SLP_TREE_SCALAR_STMTS (node).release ();
1258 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1262 /* Attempt to reorder stmts in a reduction chain so that we don't
1263 require any load permutation. Return true if that was possible,
1264 otherwise return false. */
1266 static bool
1267 vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1269 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1270 unsigned int i, j;
1271 sbitmap load_index;
1272 unsigned int lidx;
1273 slp_tree node, load;
1275 /* Compare all the permutation sequences to the first one. We know
1276 that at least one load is permuted. */
1277 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1278 if (!node->load_permutation.exists ())
1279 return false;
1280 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1282 if (!load->load_permutation.exists ())
1283 return false;
1284 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1285 if (lidx != node->load_permutation[j])
1286 return false;
1289 /* Check that the loads in the first sequence are different and there
1290 are no gaps between them. */
1291 load_index = sbitmap_alloc (group_size);
1292 bitmap_clear (load_index);
1293 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1295 if (bitmap_bit_p (load_index, lidx))
1297 sbitmap_free (load_index);
1298 return false;
1300 bitmap_set_bit (load_index, lidx);
1302 for (i = 0; i < group_size; i++)
1303 if (!bitmap_bit_p (load_index, i))
1305 sbitmap_free (load_index);
1306 return false;
1308 sbitmap_free (load_index);
1310 /* This permutation is valid for reduction. Since the order of the
1311 statements in the nodes is not important unless they are memory
1312 accesses, we can rearrange the statements in all the nodes
1313 according to the order of the loads. */
1314 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1315 node->load_permutation);
1317 /* We are done, no actual permutations need to be generated. */
1318 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1319 SLP_TREE_LOAD_PERMUTATION (node).release ();
1320 return true;
1323 /* Check if the required load permutations in the SLP instance
1324 SLP_INSTN are supported. */
1326 static bool
1327 vect_supported_load_permutation_p (slp_instance slp_instn)
1329 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1330 unsigned int i, j, k, next;
1331 slp_tree node;
1332 gimple stmt, load, next_load, first_load;
1333 struct data_reference *dr;
1335 if (dump_enabled_p ())
1337 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1338 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1339 if (node->load_permutation.exists ())
1340 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1341 dump_printf (MSG_NOTE, "%d ", next);
1342 else
1343 for (k = 0; k < group_size; ++k)
1344 dump_printf (MSG_NOTE, "%d ", k);
1345 dump_printf (MSG_NOTE, "\n");
1348 /* In case of reduction every load permutation is allowed, since the order
1349 of the reduction statements is not important (as opposed to the case of
1350 grouped stores). The only condition we need to check is that all the
1351 load nodes are of the same size and have the same permutation (and then
1352 rearrange all the nodes of the SLP instance according to this
1353 permutation). */
1355 /* Check that all the load nodes are of the same size. */
1356 /* ??? Can't we assert this? */
1357 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1358 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1359 return false;
1361 node = SLP_INSTANCE_TREE (slp_instn);
1362 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1364 /* Reduction (there are no data-refs in the root).
1365 In reduction chain the order of the loads is not important. */
1366 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1367 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1369 if (vect_attempt_slp_rearrange_stmts (slp_instn))
1370 return true;
1372 /* Fallthru to general load permutation handling. */
1375 /* In basic block vectorization we allow any subchain of an interleaving
1376 chain.
1377 FORNOW: not supported in loop SLP because of realignment compications. */
1378 if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
1380 /* Check whether the loads in an instance form a subchain and thus
1381 no permutation is necessary. */
1382 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1384 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1385 continue;
1386 bool subchain_p = true;
1387 next_load = NULL;
1388 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1390 if (j != 0
1391 && (next_load != load
1392 || GROUP_GAP (vinfo_for_stmt (load)) != 1))
1394 subchain_p = false;
1395 break;
1397 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1399 if (subchain_p)
1400 SLP_TREE_LOAD_PERMUTATION (node).release ();
1401 else
1403 /* Verify the permutation can be generated. */
1404 vec<tree> tem;
1405 if (!vect_transform_slp_perm_load (node, tem, NULL,
1406 1, slp_instn, true))
1408 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1409 vect_location,
1410 "unsupported load permutation\n");
1411 return false;
1416 /* Check that the alignment of the first load in every subchain, i.e.,
1417 the first statement in every load node, is supported.
1418 ??? This belongs in alignment checking. */
1419 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1421 first_load = SLP_TREE_SCALAR_STMTS (node)[0];
1422 if (first_load != GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_load)))
1424 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_load));
1425 if (vect_supportable_dr_alignment (dr, false)
1426 == dr_unaligned_unsupported)
1428 if (dump_enabled_p ())
1430 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1431 vect_location,
1432 "unsupported unaligned load ");
1433 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1434 first_load, 0);
1435 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1437 return false;
1442 return true;
1445 /* For loop vectorization verify we can generate the permutation. */
1446 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1447 if (node->load_permutation.exists ()
1448 && !vect_transform_slp_perm_load
1449 (node, vNULL, NULL,
1450 SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true))
1451 return false;
1453 return true;
1457 /* Find the last store in SLP INSTANCE. */
1459 static gimple
1460 vect_find_last_scalar_stmt_in_slp (slp_tree node)
1462 gimple last = NULL, stmt;
1464 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++)
1466 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1467 if (is_pattern_stmt_p (stmt_vinfo))
1468 last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last);
1469 else
1470 last = get_later_stmt (stmt, last);
1473 return last;
1476 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1478 static void
1479 vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node,
1480 stmt_vector_for_cost *prologue_cost_vec,
1481 stmt_vector_for_cost *body_cost_vec,
1482 unsigned ncopies_for_cost)
1484 unsigned i;
1485 slp_tree child;
1486 gimple stmt, s;
1487 stmt_vec_info stmt_info;
1488 tree lhs;
1489 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1491 /* Recurse down the SLP tree. */
1492 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1493 if (child)
1494 vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec,
1495 body_cost_vec, ncopies_for_cost);
1497 /* Look at the first scalar stmt to determine the cost. */
1498 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1499 stmt_info = vinfo_for_stmt (stmt);
1500 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1502 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1503 vect_model_store_cost (stmt_info, ncopies_for_cost, false,
1504 vect_uninitialized_def,
1505 node, prologue_cost_vec, body_cost_vec);
1506 else
1508 int i;
1509 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1510 vect_model_load_cost (stmt_info, ncopies_for_cost, false,
1511 node, prologue_cost_vec, body_cost_vec);
1512 /* If the load is permuted record the cost for the permutation.
1513 ??? Loads from multiple chains are let through here only
1514 for a single special case involving complex numbers where
1515 in the end no permutation is necessary. */
1516 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, s)
1517 if ((STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo_for_stmt (s))
1518 == STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info))
1519 && vect_get_place_in_interleaving_chain
1520 (s, STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info)) != i)
1522 record_stmt_cost (body_cost_vec, group_size, vec_perm,
1523 stmt_info, 0, vect_body);
1524 break;
1528 else
1530 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1531 stmt_info, 0, vect_body);
1532 if (SLP_TREE_TWO_OPERATORS (node))
1534 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1535 stmt_info, 0, vect_body);
1536 record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm,
1537 stmt_info, 0, vect_body);
1541 /* Scan operands and account for prologue cost of constants/externals.
1542 ??? This over-estimates cost for multiple uses and should be
1543 re-engineered. */
1544 lhs = gimple_get_lhs (stmt);
1545 for (i = 0; i < gimple_num_ops (stmt); ++i)
1547 tree def, op = gimple_op (stmt, i);
1548 gimple def_stmt;
1549 enum vect_def_type dt;
1550 if (!op || op == lhs)
1551 continue;
1552 if (vect_is_simple_use (op, NULL, STMT_VINFO_LOOP_VINFO (stmt_info),
1553 STMT_VINFO_BB_VINFO (stmt_info),
1554 &def_stmt, &def, &dt))
1556 /* Without looking at the actual initializer a vector of
1557 constants can be implemented as load from the constant pool.
1558 ??? We need to pass down stmt_info for a vector type
1559 even if it points to the wrong stmt. */
1560 if (dt == vect_constant_def)
1561 record_stmt_cost (prologue_cost_vec, 1, vector_load,
1562 stmt_info, 0, vect_prologue);
1563 else if (dt == vect_external_def)
1564 record_stmt_cost (prologue_cost_vec, 1, vec_construct,
1565 stmt_info, 0, vect_prologue);
1570 /* Compute the cost for the SLP instance INSTANCE. */
1572 static void
1573 vect_analyze_slp_cost (slp_instance instance, void *data)
1575 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1576 unsigned ncopies_for_cost;
1577 stmt_info_for_cost *si;
1578 unsigned i;
1580 /* Calculate the number of vector stmts to create based on the unrolling
1581 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1582 GROUP_SIZE / NUNITS otherwise. */
1583 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1584 slp_tree node = SLP_INSTANCE_TREE (instance);
1585 stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1586 /* Adjust the group_size by the vectorization factor which is always one
1587 for basic-block vectorization. */
1588 if (STMT_VINFO_LOOP_VINFO (stmt_info))
1589 group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info));
1590 unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1591 /* For reductions look at a reduction operand in case the reduction
1592 operation is widening like DOT_PROD or SAD. */
1593 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1595 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1596 switch (gimple_assign_rhs_code (stmt))
1598 case DOT_PROD_EXPR:
1599 case SAD_EXPR:
1600 nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type
1601 (TREE_TYPE (gimple_assign_rhs1 (stmt))));
1602 break;
1603 default:;
1606 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1608 prologue_cost_vec.create (10);
1609 body_cost_vec.create (10);
1610 vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance),
1611 &prologue_cost_vec, &body_cost_vec,
1612 ncopies_for_cost);
1614 /* Record the prologue costs, which were delayed until we were
1615 sure that SLP was successful. */
1616 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1618 struct _stmt_vec_info *stmt_info
1619 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1620 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1621 si->misalign, vect_prologue);
1624 /* Record the instance's instructions in the target cost model. */
1625 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1627 struct _stmt_vec_info *stmt_info
1628 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1629 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1630 si->misalign, vect_body);
1633 prologue_cost_vec.release ();
1634 body_cost_vec.release ();
1637 /* Analyze an SLP instance starting from a group of grouped stores. Call
1638 vect_build_slp_tree to build a tree of packed stmts if possible.
1639 Return FALSE if it's impossible to SLP any stmt in the loop. */
1641 static bool
1642 vect_analyze_slp_instance (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1643 gimple stmt, unsigned max_tree_size)
1645 slp_instance new_instance;
1646 slp_tree node;
1647 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1648 unsigned int unrolling_factor = 1, nunits;
1649 tree vectype, scalar_type = NULL_TREE;
1650 gimple next;
1651 unsigned int vectorization_factor = 0;
1652 int i;
1653 unsigned int max_nunits = 0;
1654 vec<slp_tree> loads;
1655 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1656 vec<gimple> scalar_stmts;
1658 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1660 if (dr)
1662 scalar_type = TREE_TYPE (DR_REF (dr));
1663 vectype = get_vectype_for_scalar_type (scalar_type);
1665 else
1667 gcc_assert (loop_vinfo);
1668 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1671 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1673 else
1675 gcc_assert (loop_vinfo);
1676 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1677 group_size = LOOP_VINFO_REDUCTIONS (loop_vinfo).length ();
1680 if (!vectype)
1682 if (dump_enabled_p ())
1684 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1685 "Build SLP failed: unsupported data-type ");
1686 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1687 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1690 return false;
1693 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1694 if (loop_vinfo)
1695 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1696 else
1697 vectorization_factor = nunits;
1699 /* Calculate the unrolling factor. */
1700 unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
1701 if (unrolling_factor != 1 && !loop_vinfo)
1703 if (dump_enabled_p ())
1704 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1705 "Build SLP failed: unrolling required in basic"
1706 " block SLP\n");
1708 return false;
1711 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1712 scalar_stmts.create (group_size);
1713 next = stmt;
1714 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1716 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1717 while (next)
1719 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1720 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1721 scalar_stmts.safe_push (
1722 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1723 else
1724 scalar_stmts.safe_push (next);
1725 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1727 /* Mark the first element of the reduction chain as reduction to properly
1728 transform the node. In the reduction analysis phase only the last
1729 element of the chain is marked as reduction. */
1730 if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1731 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def;
1733 else
1735 /* Collect reduction statements. */
1736 vec<gimple> reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1737 for (i = 0; reductions.iterate (i, &next); i++)
1738 scalar_stmts.safe_push (next);
1741 node = vect_create_new_slp_node (scalar_stmts);
1743 loads.create (group_size);
1745 /* Build the tree for the SLP instance. */
1746 bool *matches = XALLOCAVEC (bool, group_size);
1747 unsigned npermutes = 0;
1748 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &node, group_size,
1749 &max_nunits, &loads,
1750 vectorization_factor, matches, &npermutes, NULL,
1751 max_tree_size))
1753 /* Calculate the unrolling factor based on the smallest type. */
1754 if (max_nunits > nunits)
1755 unrolling_factor = least_common_multiple (max_nunits, group_size)
1756 / group_size;
1758 if (unrolling_factor != 1 && !loop_vinfo)
1760 if (dump_enabled_p ())
1761 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1762 "Build SLP failed: unrolling required in basic"
1763 " block SLP\n");
1764 vect_free_slp_tree (node);
1765 loads.release ();
1766 return false;
1769 /* Create a new SLP instance. */
1770 new_instance = XNEW (struct _slp_instance);
1771 SLP_INSTANCE_TREE (new_instance) = node;
1772 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1773 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1774 SLP_INSTANCE_LOADS (new_instance) = loads;
1776 /* Compute the load permutation. */
1777 slp_tree load_node;
1778 bool loads_permuted = false;
1779 FOR_EACH_VEC_ELT (loads, i, load_node)
1781 vec<unsigned> load_permutation;
1782 int j;
1783 gimple load, first_stmt;
1784 bool this_load_permuted = false;
1785 load_permutation.create (group_size);
1786 first_stmt = GROUP_FIRST_ELEMENT
1787 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
1788 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
1790 int load_place
1791 = vect_get_place_in_interleaving_chain (load, first_stmt);
1792 gcc_assert (load_place != -1);
1793 if (load_place != j)
1794 this_load_permuted = true;
1795 load_permutation.safe_push (load_place);
1797 if (!this_load_permuted
1798 /* The load requires permutation when unrolling exposes
1799 a gap either because the group is larger than the SLP
1800 group-size or because there is a gap between the groups. */
1801 && (unrolling_factor == 1
1802 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1803 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)))
1805 load_permutation.release ();
1806 continue;
1808 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
1809 loads_permuted = true;
1812 if (loads_permuted)
1814 if (!vect_supported_load_permutation_p (new_instance))
1816 if (dump_enabled_p ())
1818 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1819 "Build SLP failed: unsupported load "
1820 "permutation ");
1821 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
1822 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1824 vect_free_slp_instance (new_instance);
1825 return false;
1830 if (loop_vinfo)
1831 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).safe_push (new_instance);
1832 else
1833 BB_VINFO_SLP_INSTANCES (bb_vinfo).safe_push (new_instance);
1835 if (dump_enabled_p ())
1836 vect_print_slp_tree (MSG_NOTE, node);
1838 return true;
1841 /* Failed to SLP. */
1842 /* Free the allocated memory. */
1843 vect_free_slp_tree (node);
1844 loads.release ();
1846 return false;
1850 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
1851 trees of packed scalar stmts if SLP is possible. */
1853 bool
1854 vect_analyze_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1855 unsigned max_tree_size)
1857 unsigned int i;
1858 vec<gimple> grouped_stores;
1859 vec<gimple> reductions = vNULL;
1860 vec<gimple> reduc_chains = vNULL;
1861 gimple first_element;
1862 bool ok = false;
1864 if (dump_enabled_p ())
1865 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
1867 if (loop_vinfo)
1869 grouped_stores = LOOP_VINFO_GROUPED_STORES (loop_vinfo);
1870 reduc_chains = LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo);
1871 reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1873 else
1874 grouped_stores = BB_VINFO_GROUPED_STORES (bb_vinfo);
1876 /* Find SLP sequences starting from groups of grouped stores. */
1877 FOR_EACH_VEC_ELT (grouped_stores, i, first_element)
1878 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
1879 max_tree_size))
1880 ok = true;
1882 if (reduc_chains.length () > 0)
1884 /* Find SLP sequences starting from reduction chains. */
1885 FOR_EACH_VEC_ELT (reduc_chains, i, first_element)
1886 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
1887 max_tree_size))
1888 ok = true;
1889 else
1890 return false;
1892 /* Don't try to vectorize SLP reductions if reduction chain was
1893 detected. */
1894 return ok;
1897 /* Find SLP sequences starting from groups of reductions. */
1898 if (reductions.length () > 1
1899 && vect_analyze_slp_instance (loop_vinfo, bb_vinfo, reductions[0],
1900 max_tree_size))
1901 ok = true;
1903 return true;
1907 /* For each possible SLP instance decide whether to SLP it and calculate overall
1908 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
1909 least one instance. */
1911 bool
1912 vect_make_slp_decision (loop_vec_info loop_vinfo)
1914 unsigned int i, unrolling_factor = 1;
1915 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1916 slp_instance instance;
1917 int decided_to_slp = 0;
1919 if (dump_enabled_p ())
1920 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
1921 "\n");
1923 FOR_EACH_VEC_ELT (slp_instances, i, instance)
1925 /* FORNOW: SLP if you can. */
1926 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
1927 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
1929 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
1930 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
1931 loop-based vectorization. Such stmts will be marked as HYBRID. */
1932 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
1933 decided_to_slp++;
1936 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
1938 if (decided_to_slp && dump_enabled_p ())
1939 dump_printf_loc (MSG_NOTE, vect_location,
1940 "Decided to SLP %d instances. Unrolling factor %d\n",
1941 decided_to_slp, unrolling_factor);
1943 return (decided_to_slp > 0);
1947 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
1948 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
1950 static void
1951 vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
1953 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[i];
1954 imm_use_iterator imm_iter;
1955 gimple use_stmt;
1956 stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
1957 slp_tree child;
1958 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1959 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1960 int j;
1962 /* Propagate hybrid down the SLP tree. */
1963 if (stype == hybrid)
1965 else if (HYBRID_SLP_STMT (stmt_vinfo))
1966 stype = hybrid;
1967 else
1969 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
1970 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
1971 /* We always get the pattern stmt here, but for immediate
1972 uses we have to use the LHS of the original stmt. */
1973 gcc_checking_assert (!STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
1974 if (STMT_VINFO_RELATED_STMT (stmt_vinfo))
1975 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
1976 if (TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
1977 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
1979 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
1980 continue;
1981 use_vinfo = vinfo_for_stmt (use_stmt);
1982 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
1983 && STMT_VINFO_RELATED_STMT (use_vinfo))
1984 use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
1985 if (!STMT_SLP_TYPE (use_vinfo)
1986 && (STMT_VINFO_RELEVANT (use_vinfo)
1987 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
1988 && !(gimple_code (use_stmt) == GIMPLE_PHI
1989 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
1991 if (dump_enabled_p ())
1993 dump_printf_loc (MSG_NOTE, vect_location, "use of SLP "
1994 "def in non-SLP stmt: ");
1995 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0);
1997 stype = hybrid;
2002 if (stype == hybrid
2003 && !HYBRID_SLP_STMT (stmt_vinfo))
2005 if (dump_enabled_p ())
2007 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2008 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2010 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2013 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2014 if (child)
2015 vect_detect_hybrid_slp_stmts (child, i, stype);
2018 /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
2020 static tree
2021 vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2023 walk_stmt_info *wi = (walk_stmt_info *)data;
2024 struct loop *loopp = (struct loop *)wi->info;
2026 if (wi->is_lhs)
2027 return NULL_TREE;
2029 if (TREE_CODE (*tp) == SSA_NAME
2030 && !SSA_NAME_IS_DEFAULT_DEF (*tp))
2032 gimple def_stmt = SSA_NAME_DEF_STMT (*tp);
2033 if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
2034 && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
2036 if (dump_enabled_p ())
2038 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2039 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2041 STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
2045 return NULL_TREE;
2048 static tree
2049 vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
2050 walk_stmt_info *)
2052 /* If the stmt is in a SLP instance then this isn't a reason
2053 to mark use definitions in other SLP instances as hybrid. */
2054 if (STMT_SLP_TYPE (vinfo_for_stmt (gsi_stmt (*gsi))) != loop_vect)
2055 *handled = true;
2056 return NULL_TREE;
2059 /* Find stmts that must be both vectorized and SLPed. */
2061 void
2062 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2064 unsigned int i;
2065 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2066 slp_instance instance;
2068 if (dump_enabled_p ())
2069 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
2070 "\n");
2072 /* First walk all pattern stmt in the loop and mark defs of uses as
2073 hybrid because immediate uses in them are not recorded. */
2074 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2076 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2077 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2078 gsi_next (&gsi))
2080 gimple stmt = gsi_stmt (gsi);
2081 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2082 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2084 walk_stmt_info wi;
2085 memset (&wi, 0, sizeof (wi));
2086 wi.info = LOOP_VINFO_LOOP (loop_vinfo);
2087 gimple_stmt_iterator gsi2
2088 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2089 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2090 vect_detect_hybrid_slp_1, &wi);
2091 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2092 vect_detect_hybrid_slp_2,
2093 vect_detect_hybrid_slp_1, &wi);
2098 /* Then walk the SLP instance trees marking stmts with uses in
2099 non-SLP stmts as hybrid, also propagating hybrid down the
2100 SLP tree, collecting the above info on-the-fly. */
2101 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2103 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2104 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2105 i, pure_slp);
2110 /* Create and initialize a new bb_vec_info struct for BB, as well as
2111 stmt_vec_info structs for all the stmts in it. */
2113 static bb_vec_info
2114 new_bb_vec_info (basic_block bb)
2116 bb_vec_info res = NULL;
2117 gimple_stmt_iterator gsi;
2119 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
2120 BB_VINFO_BB (res) = bb;
2122 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2124 gimple stmt = gsi_stmt (gsi);
2125 gimple_set_uid (stmt, 0);
2126 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, NULL, res));
2129 BB_VINFO_GROUPED_STORES (res).create (10);
2130 BB_VINFO_SLP_INSTANCES (res).create (2);
2131 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
2133 bb->aux = res;
2134 return res;
2138 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2139 stmts in the basic block. */
2141 static void
2142 destroy_bb_vec_info (bb_vec_info bb_vinfo)
2144 vec<slp_instance> slp_instances;
2145 slp_instance instance;
2146 basic_block bb;
2147 gimple_stmt_iterator si;
2148 unsigned i;
2150 if (!bb_vinfo)
2151 return;
2153 bb = BB_VINFO_BB (bb_vinfo);
2155 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2157 gimple stmt = gsi_stmt (si);
2158 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2160 if (stmt_info)
2161 /* Free stmt_vec_info. */
2162 free_stmt_vec_info (stmt);
2165 vect_destroy_datarefs (NULL, bb_vinfo);
2166 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
2167 BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
2168 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2169 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2170 vect_free_slp_instance (instance);
2171 BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
2172 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
2173 free (bb_vinfo);
2174 bb->aux = NULL;
2178 /* Analyze statements contained in SLP tree node after recursively analyzing
2179 the subtree. Return TRUE if the operations are supported. */
2181 static bool
2182 vect_slp_analyze_node_operations (slp_tree node)
2184 bool dummy;
2185 int i;
2186 gimple stmt;
2187 slp_tree child;
2189 if (!node)
2190 return true;
2192 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2193 if (!vect_slp_analyze_node_operations (child))
2194 return false;
2196 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2198 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2199 gcc_assert (stmt_info);
2200 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2202 if (!vect_analyze_stmt (stmt, &dummy, node))
2203 return false;
2206 return true;
2210 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2211 operations are supported. */
2213 bool
2214 vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data)
2216 slp_instance instance;
2217 int i;
2219 if (dump_enabled_p ())
2220 dump_printf_loc (MSG_NOTE, vect_location,
2221 "=== vect_slp_analyze_operations ===\n");
2223 for (i = 0; slp_instances.iterate (i, &instance); )
2225 if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance)))
2227 dump_printf_loc (MSG_NOTE, vect_location,
2228 "removing SLP instance operations starting from: ");
2229 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2230 SLP_TREE_SCALAR_STMTS
2231 (SLP_INSTANCE_TREE (instance))[0], 0);
2232 vect_free_slp_instance (instance);
2233 slp_instances.ordered_remove (i);
2235 else
2237 /* Compute the costs of the SLP instance. */
2238 vect_analyze_slp_cost (instance, data);
2239 i++;
2243 if (!slp_instances.length ())
2244 return false;
2246 return true;
2250 /* Compute the scalar cost of the SLP node NODE and its children
2251 and return it. Do not account defs that are marked in LIFE and
2252 update LIFE according to uses of NODE. */
2254 static unsigned
2255 vect_bb_slp_scalar_cost (basic_block bb,
2256 slp_tree node, vec<bool, va_heap> *life)
2258 unsigned scalar_cost = 0;
2259 unsigned i;
2260 gimple stmt;
2261 slp_tree child;
2263 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2265 unsigned stmt_cost;
2266 ssa_op_iter op_iter;
2267 def_operand_p def_p;
2268 stmt_vec_info stmt_info;
2270 if ((*life)[i])
2271 continue;
2273 /* If there is a non-vectorized use of the defs then the scalar
2274 stmt is kept live in which case we do not account it or any
2275 required defs in the SLP children in the scalar cost. This
2276 way we make the vectorization more costly when compared to
2277 the scalar cost. */
2278 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2280 imm_use_iterator use_iter;
2281 gimple use_stmt;
2282 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
2283 if (!is_gimple_debug (use_stmt)
2284 && (gimple_code (use_stmt) == GIMPLE_PHI
2285 || gimple_bb (use_stmt) != bb
2286 || !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (use_stmt))))
2288 (*life)[i] = true;
2289 BREAK_FROM_IMM_USE_STMT (use_iter);
2292 if ((*life)[i])
2293 continue;
2295 stmt_info = vinfo_for_stmt (stmt);
2296 if (STMT_VINFO_DATA_REF (stmt_info))
2298 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2299 stmt_cost = vect_get_stmt_cost (scalar_load);
2300 else
2301 stmt_cost = vect_get_stmt_cost (scalar_store);
2303 else
2304 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2306 scalar_cost += stmt_cost;
2309 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2310 if (child)
2311 scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
2313 return scalar_cost;
2316 /* Check if vectorization of the basic block is profitable. */
2318 static bool
2319 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2321 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2322 slp_instance instance;
2323 int i;
2324 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2325 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2327 /* Calculate scalar cost. */
2328 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2330 auto_vec<bool, 20> life;
2331 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
2332 scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2333 SLP_INSTANCE_TREE (instance),
2334 &life);
2337 /* Complete the target-specific cost calculation. */
2338 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2339 &vec_inside_cost, &vec_epilogue_cost);
2341 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2343 if (dump_enabled_p ())
2345 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2346 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2347 vec_inside_cost);
2348 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2349 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2350 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
2353 /* Vectorization is profitable if its cost is less than the cost of scalar
2354 version. */
2355 if (vec_outside_cost + vec_inside_cost >= scalar_cost)
2356 return false;
2358 return true;
2361 /* Check if the basic block can be vectorized. */
2363 static bb_vec_info
2364 vect_slp_analyze_bb_1 (basic_block bb)
2366 bb_vec_info bb_vinfo;
2367 vec<slp_instance> slp_instances;
2368 slp_instance instance;
2369 int i;
2370 int min_vf = 2;
2371 unsigned n_stmts = 0;
2373 bb_vinfo = new_bb_vec_info (bb);
2374 if (!bb_vinfo)
2375 return NULL;
2377 if (!vect_analyze_data_refs (NULL, bb_vinfo, &min_vf, &n_stmts))
2379 if (dump_enabled_p ())
2380 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2381 "not vectorized: unhandled data-ref in basic "
2382 "block.\n");
2384 destroy_bb_vec_info (bb_vinfo);
2385 return NULL;
2388 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2390 if (dump_enabled_p ())
2391 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2392 "not vectorized: not enough data-refs in "
2393 "basic block.\n");
2395 destroy_bb_vec_info (bb_vinfo);
2396 return NULL;
2399 if (!vect_analyze_data_ref_accesses (NULL, bb_vinfo))
2401 if (dump_enabled_p ())
2402 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2403 "not vectorized: unhandled data access in "
2404 "basic block.\n");
2406 destroy_bb_vec_info (bb_vinfo);
2407 return NULL;
2410 vect_pattern_recog (NULL, bb_vinfo);
2412 if (!vect_analyze_data_refs_alignment (NULL, bb_vinfo))
2414 if (dump_enabled_p ())
2415 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2416 "not vectorized: bad data alignment in basic "
2417 "block.\n");
2419 destroy_bb_vec_info (bb_vinfo);
2420 return NULL;
2423 /* Check the SLP opportunities in the basic block, analyze and build SLP
2424 trees. */
2425 if (!vect_analyze_slp (NULL, bb_vinfo, n_stmts))
2427 if (dump_enabled_p ())
2429 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2430 "Failed to SLP the basic block.\n");
2431 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2432 "not vectorized: failed to find SLP opportunities "
2433 "in basic block.\n");
2436 destroy_bb_vec_info (bb_vinfo);
2437 return NULL;
2440 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2442 /* Mark all the statements that we want to vectorize as pure SLP and
2443 relevant. */
2444 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2446 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2447 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2450 /* Mark all the statements that we do not want to vectorize. */
2451 for (gimple_stmt_iterator gsi = gsi_start_bb (BB_VINFO_BB (bb_vinfo));
2452 !gsi_end_p (gsi); gsi_next (&gsi))
2454 stmt_vec_info vinfo = vinfo_for_stmt (gsi_stmt (gsi));
2455 if (STMT_SLP_TYPE (vinfo) != pure_slp)
2456 STMT_VINFO_VECTORIZABLE (vinfo) = false;
2459 /* Analyze dependences. At this point all stmts not participating in
2460 vectorization have to be marked. Dependence analysis assumes
2461 that we either vectorize all SLP instances or none at all. */
2462 if (!vect_slp_analyze_data_ref_dependences (bb_vinfo))
2464 if (dump_enabled_p ())
2465 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2466 "not vectorized: unhandled data dependence "
2467 "in basic block.\n");
2469 destroy_bb_vec_info (bb_vinfo);
2470 return NULL;
2473 if (!vect_verify_datarefs_alignment (NULL, bb_vinfo))
2475 if (dump_enabled_p ())
2476 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2477 "not vectorized: unsupported alignment in basic "
2478 "block.\n");
2479 destroy_bb_vec_info (bb_vinfo);
2480 return NULL;
2483 if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo),
2484 BB_VINFO_TARGET_COST_DATA (bb_vinfo)))
2486 if (dump_enabled_p ())
2487 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2488 "not vectorized: bad operation in basic block.\n");
2490 destroy_bb_vec_info (bb_vinfo);
2491 return NULL;
2494 /* Cost model: check if the vectorization is worthwhile. */
2495 if (!unlimited_cost_model (NULL)
2496 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2498 if (dump_enabled_p ())
2499 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2500 "not vectorized: vectorization is not "
2501 "profitable.\n");
2503 destroy_bb_vec_info (bb_vinfo);
2504 return NULL;
2507 if (dump_enabled_p ())
2508 dump_printf_loc (MSG_NOTE, vect_location,
2509 "Basic block will be vectorized using SLP\n");
2511 return bb_vinfo;
2515 bb_vec_info
2516 vect_slp_analyze_bb (basic_block bb)
2518 bb_vec_info bb_vinfo;
2519 int insns = 0;
2520 gimple_stmt_iterator gsi;
2521 unsigned int vector_sizes;
2523 if (dump_enabled_p ())
2524 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2526 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2528 gimple stmt = gsi_stmt (gsi);
2529 if (!is_gimple_debug (stmt)
2530 && !gimple_nop_p (stmt)
2531 && gimple_code (stmt) != GIMPLE_LABEL)
2532 insns++;
2535 if (insns > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2537 if (dump_enabled_p ())
2538 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2539 "not vectorized: too many instructions in "
2540 "basic block.\n");
2542 return NULL;
2545 /* Autodetect first vector size we try. */
2546 current_vector_size = 0;
2547 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2549 while (1)
2551 bb_vinfo = vect_slp_analyze_bb_1 (bb);
2552 if (bb_vinfo)
2553 return bb_vinfo;
2555 destroy_bb_vec_info (bb_vinfo);
2557 vector_sizes &= ~current_vector_size;
2558 if (vector_sizes == 0
2559 || current_vector_size == 0)
2560 return NULL;
2562 /* Try the next biggest vector size. */
2563 current_vector_size = 1 << floor_log2 (vector_sizes);
2564 if (dump_enabled_p ())
2565 dump_printf_loc (MSG_NOTE, vect_location,
2566 "***** Re-trying analysis with "
2567 "vector size %d\n", current_vector_size);
2572 /* For constant and loop invariant defs of SLP_NODE this function returns
2573 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2574 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2575 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2576 REDUC_INDEX is the index of the reduction operand in the statements, unless
2577 it is -1. */
2579 static void
2580 vect_get_constant_vectors (tree op, slp_tree slp_node,
2581 vec<tree> *vec_oprnds,
2582 unsigned int op_num, unsigned int number_of_vectors,
2583 int reduc_index)
2585 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2586 gimple stmt = stmts[0];
2587 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2588 unsigned nunits;
2589 tree vec_cst;
2590 tree *elts;
2591 unsigned j, number_of_places_left_in_vector;
2592 tree vector_type;
2593 tree vop;
2594 int group_size = stmts.length ();
2595 unsigned int vec_num, i;
2596 unsigned number_of_copies = 1;
2597 vec<tree> voprnds;
2598 voprnds.create (number_of_vectors);
2599 bool constant_p, is_store;
2600 tree neutral_op = NULL;
2601 enum tree_code code = gimple_expr_code (stmt);
2602 gimple def_stmt;
2603 struct loop *loop;
2604 gimple_seq ctor_seq = NULL;
2606 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
2607 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
2609 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
2610 && reduc_index != -1)
2612 op_num = reduc_index;
2613 op = gimple_op (stmt, op_num + 1);
2614 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
2615 we need either neutral operands or the original operands. See
2616 get_initial_def_for_reduction() for details. */
2617 switch (code)
2619 case WIDEN_SUM_EXPR:
2620 case DOT_PROD_EXPR:
2621 case SAD_EXPR:
2622 case PLUS_EXPR:
2623 case MINUS_EXPR:
2624 case BIT_IOR_EXPR:
2625 case BIT_XOR_EXPR:
2626 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2627 neutral_op = build_real (TREE_TYPE (op), dconst0);
2628 else
2629 neutral_op = build_int_cst (TREE_TYPE (op), 0);
2631 break;
2633 case MULT_EXPR:
2634 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2635 neutral_op = build_real (TREE_TYPE (op), dconst1);
2636 else
2637 neutral_op = build_int_cst (TREE_TYPE (op), 1);
2639 break;
2641 case BIT_AND_EXPR:
2642 neutral_op = build_int_cst (TREE_TYPE (op), -1);
2643 break;
2645 /* For MIN/MAX we don't have an easy neutral operand but
2646 the initial values can be used fine here. Only for
2647 a reduction chain we have to force a neutral element. */
2648 case MAX_EXPR:
2649 case MIN_EXPR:
2650 if (!GROUP_FIRST_ELEMENT (stmt_vinfo))
2651 neutral_op = NULL;
2652 else
2654 def_stmt = SSA_NAME_DEF_STMT (op);
2655 loop = (gimple_bb (stmt))->loop_father;
2656 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2657 loop_preheader_edge (loop));
2659 break;
2661 default:
2662 gcc_assert (!GROUP_FIRST_ELEMENT (stmt_vinfo));
2663 neutral_op = NULL;
2667 if (STMT_VINFO_DATA_REF (stmt_vinfo))
2669 is_store = true;
2670 op = gimple_assign_rhs1 (stmt);
2672 else
2673 is_store = false;
2675 gcc_assert (op);
2677 if (CONSTANT_CLASS_P (op))
2678 constant_p = true;
2679 else
2680 constant_p = false;
2682 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
2683 created vectors. It is greater than 1 if unrolling is performed.
2685 For example, we have two scalar operands, s1 and s2 (e.g., group of
2686 strided accesses of size two), while NUNITS is four (i.e., four scalars
2687 of this type can be packed in a vector). The output vector will contain
2688 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
2689 will be 2).
2691 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
2692 containing the operands.
2694 For example, NUNITS is four as before, and the group size is 8
2695 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
2696 {s5, s6, s7, s8}. */
2698 number_of_copies = nunits * number_of_vectors / group_size;
2700 number_of_places_left_in_vector = nunits;
2701 elts = XALLOCAVEC (tree, nunits);
2702 bool place_after_defs = false;
2703 for (j = 0; j < number_of_copies; j++)
2705 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
2707 if (is_store)
2708 op = gimple_assign_rhs1 (stmt);
2709 else
2711 switch (code)
2713 case COND_EXPR:
2714 if (op_num == 0 || op_num == 1)
2716 tree cond = gimple_assign_rhs1 (stmt);
2717 op = TREE_OPERAND (cond, op_num);
2719 else
2721 if (op_num == 2)
2722 op = gimple_assign_rhs2 (stmt);
2723 else
2724 op = gimple_assign_rhs3 (stmt);
2726 break;
2728 case CALL_EXPR:
2729 op = gimple_call_arg (stmt, op_num);
2730 break;
2732 case LSHIFT_EXPR:
2733 case RSHIFT_EXPR:
2734 case LROTATE_EXPR:
2735 case RROTATE_EXPR:
2736 op = gimple_op (stmt, op_num + 1);
2737 /* Unlike the other binary operators, shifts/rotates have
2738 the shift count being int, instead of the same type as
2739 the lhs, so make sure the scalar is the right type if
2740 we are dealing with vectors of
2741 long long/long/short/char. */
2742 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
2743 op = fold_convert (TREE_TYPE (vector_type), op);
2744 break;
2746 default:
2747 op = gimple_op (stmt, op_num + 1);
2748 break;
2752 if (reduc_index != -1)
2754 loop = (gimple_bb (stmt))->loop_father;
2755 def_stmt = SSA_NAME_DEF_STMT (op);
2757 gcc_assert (loop);
2759 /* Get the def before the loop. In reduction chain we have only
2760 one initial value. */
2761 if ((j != (number_of_copies - 1)
2762 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2763 && i != 0))
2764 && neutral_op)
2765 op = neutral_op;
2766 else
2767 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2768 loop_preheader_edge (loop));
2771 /* Create 'vect_ = {op0,op1,...,opn}'. */
2772 number_of_places_left_in_vector--;
2773 tree orig_op = op;
2774 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
2776 if (CONSTANT_CLASS_P (op))
2778 op = fold_unary (VIEW_CONVERT_EXPR,
2779 TREE_TYPE (vector_type), op);
2780 gcc_assert (op && CONSTANT_CLASS_P (op));
2782 else
2784 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
2785 gimple init_stmt;
2786 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), op);
2787 init_stmt
2788 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR, op);
2789 gimple_seq_add_stmt (&ctor_seq, init_stmt);
2790 op = new_temp;
2793 elts[number_of_places_left_in_vector] = op;
2794 if (!CONSTANT_CLASS_P (op))
2795 constant_p = false;
2796 if (TREE_CODE (orig_op) == SSA_NAME
2797 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
2798 && STMT_VINFO_BB_VINFO (stmt_vinfo)
2799 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
2800 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
2801 place_after_defs = true;
2803 if (number_of_places_left_in_vector == 0)
2805 number_of_places_left_in_vector = nunits;
2807 if (constant_p)
2808 vec_cst = build_vector (vector_type, elts);
2809 else
2811 vec<constructor_elt, va_gc> *v;
2812 unsigned k;
2813 vec_alloc (v, nunits);
2814 for (k = 0; k < nunits; ++k)
2815 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
2816 vec_cst = build_constructor (vector_type, v);
2818 tree init;
2819 gimple_stmt_iterator gsi;
2820 if (place_after_defs)
2822 gsi = gsi_for_stmt
2823 (vect_find_last_scalar_stmt_in_slp (slp_node));
2824 init = vect_init_vector (stmt, vec_cst, vector_type, &gsi);
2826 else
2827 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
2828 if (ctor_seq != NULL)
2830 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
2831 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
2832 GSI_SAME_STMT);
2833 ctor_seq = NULL;
2835 voprnds.quick_push (init);
2836 place_after_defs = false;
2841 /* Since the vectors are created in the reverse order, we should invert
2842 them. */
2843 vec_num = voprnds.length ();
2844 for (j = vec_num; j != 0; j--)
2846 vop = voprnds[j - 1];
2847 vec_oprnds->quick_push (vop);
2850 voprnds.release ();
2852 /* In case that VF is greater than the unrolling factor needed for the SLP
2853 group of stmts, NUMBER_OF_VECTORS to be created is greater than
2854 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
2855 to replicate the vectors. */
2856 while (number_of_vectors > vec_oprnds->length ())
2858 tree neutral_vec = NULL;
2860 if (neutral_op)
2862 if (!neutral_vec)
2863 neutral_vec = build_vector_from_val (vector_type, neutral_op);
2865 vec_oprnds->quick_push (neutral_vec);
2867 else
2869 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
2870 vec_oprnds->quick_push (vop);
2876 /* Get vectorized definitions from SLP_NODE that contains corresponding
2877 vectorized def-stmts. */
2879 static void
2880 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
2882 tree vec_oprnd;
2883 gimple vec_def_stmt;
2884 unsigned int i;
2886 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
2888 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
2890 gcc_assert (vec_def_stmt);
2891 vec_oprnd = gimple_get_lhs (vec_def_stmt);
2892 vec_oprnds->quick_push (vec_oprnd);
2897 /* Get vectorized definitions for SLP_NODE.
2898 If the scalar definitions are loop invariants or constants, collect them and
2899 call vect_get_constant_vectors() to create vector stmts.
2900 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
2901 must be stored in the corresponding child of SLP_NODE, and we call
2902 vect_get_slp_vect_defs () to retrieve them. */
2904 void
2905 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
2906 vec<vec<tree> > *vec_oprnds, int reduc_index)
2908 gimple first_stmt;
2909 int number_of_vects = 0, i;
2910 unsigned int child_index = 0;
2911 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
2912 slp_tree child = NULL;
2913 vec<tree> vec_defs;
2914 tree oprnd;
2915 bool vectorized_defs;
2917 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2918 FOR_EACH_VEC_ELT (ops, i, oprnd)
2920 /* For each operand we check if it has vectorized definitions in a child
2921 node or we need to create them (for invariants and constants). We
2922 check if the LHS of the first stmt of the next child matches OPRND.
2923 If it does, we found the correct child. Otherwise, we call
2924 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
2925 to check this child node for the next operand. */
2926 vectorized_defs = false;
2927 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
2929 child = SLP_TREE_CHILDREN (slp_node)[child_index];
2931 /* We have to check both pattern and original def, if available. */
2932 if (child)
2934 gimple first_def = SLP_TREE_SCALAR_STMTS (child)[0];
2935 gimple related
2936 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
2938 if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0)
2939 || (related
2940 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
2942 /* The number of vector defs is determined by the number of
2943 vector statements in the node from which we get those
2944 statements. */
2945 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
2946 vectorized_defs = true;
2947 child_index++;
2950 else
2951 child_index++;
2954 if (!vectorized_defs)
2956 if (i == 0)
2958 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
2959 /* Number of vector stmts was calculated according to LHS in
2960 vect_schedule_slp_instance (), fix it by replacing LHS with
2961 RHS, if necessary. See vect_get_smallest_scalar_type () for
2962 details. */
2963 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
2964 &rhs_size_unit);
2965 if (rhs_size_unit != lhs_size_unit)
2967 number_of_vects *= rhs_size_unit;
2968 number_of_vects /= lhs_size_unit;
2973 /* Allocate memory for vectorized defs. */
2974 vec_defs = vNULL;
2975 vec_defs.create (number_of_vects);
2977 /* For reduction defs we call vect_get_constant_vectors (), since we are
2978 looking for initial loop invariant values. */
2979 if (vectorized_defs && reduc_index == -1)
2980 /* The defs are already vectorized. */
2981 vect_get_slp_vect_defs (child, &vec_defs);
2982 else
2983 /* Build vectors from scalar defs. */
2984 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
2985 number_of_vects, reduc_index);
2987 vec_oprnds->quick_push (vec_defs);
2989 /* For reductions, we only need initial values. */
2990 if (reduc_index != -1)
2991 return;
2996 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
2997 building a vector of type MASK_TYPE from it) and two input vectors placed in
2998 DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
2999 shifting by STRIDE elements of DR_CHAIN for every copy.
3000 (STRIDE is the number of vectorized stmts for NODE divided by the number of
3001 copies).
3002 VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
3003 the created stmts must be inserted. */
3005 static inline void
3006 vect_create_mask_and_perm (gimple stmt,
3007 tree mask, int first_vec_indx, int second_vec_indx,
3008 gimple_stmt_iterator *gsi, slp_tree node,
3009 tree vectype, vec<tree> dr_chain,
3010 int ncopies, int vect_stmts_counter)
3012 tree perm_dest;
3013 gimple perm_stmt = NULL;
3014 int i, stride;
3015 tree first_vec, second_vec, data_ref;
3017 stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
3019 /* Initialize the vect stmts of NODE to properly insert the generated
3020 stmts later. */
3021 for (i = SLP_TREE_VEC_STMTS (node).length ();
3022 i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3023 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
3025 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
3026 for (i = 0; i < ncopies; i++)
3028 first_vec = dr_chain[first_vec_indx];
3029 second_vec = dr_chain[second_vec_indx];
3031 /* Generate the permute statement. */
3032 perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
3033 first_vec, second_vec, mask);
3034 data_ref = make_ssa_name (perm_dest, perm_stmt);
3035 gimple_set_lhs (perm_stmt, data_ref);
3036 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
3038 /* Store the vector statement in NODE. */
3039 SLP_TREE_VEC_STMTS (node)[stride * i + vect_stmts_counter] = perm_stmt;
3041 first_vec_indx += stride;
3042 second_vec_indx += stride;
3047 /* Given FIRST_MASK_ELEMENT - the mask element in element representation,
3048 return in CURRENT_MASK_ELEMENT its equivalent in target specific
3049 representation. Check that the mask is valid and return FALSE if not.
3050 Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
3051 the next vector, i.e., the current first vector is not needed. */
3053 static bool
3054 vect_get_mask_element (gimple stmt, int first_mask_element, int m,
3055 int mask_nunits, bool only_one_vec, int index,
3056 unsigned char *mask, int *current_mask_element,
3057 bool *need_next_vector, int *number_of_mask_fixes,
3058 bool *mask_fixed, bool *needs_first_vector)
3060 int i;
3062 /* Convert to target specific representation. */
3063 *current_mask_element = first_mask_element + m;
3064 /* Adjust the value in case it's a mask for second and third vectors. */
3065 *current_mask_element -= mask_nunits * (*number_of_mask_fixes - 1);
3067 if (*current_mask_element < 0)
3069 if (dump_enabled_p ())
3071 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3072 "permutation requires past vector ");
3073 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3074 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3076 return false;
3079 if (*current_mask_element < mask_nunits)
3080 *needs_first_vector = true;
3082 /* We have only one input vector to permute but the mask accesses values in
3083 the next vector as well. */
3084 if (only_one_vec && *current_mask_element >= mask_nunits)
3086 if (dump_enabled_p ())
3088 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3089 "permutation requires at least two vectors ");
3090 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3091 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3094 return false;
3097 /* The mask requires the next vector. */
3098 while (*current_mask_element >= mask_nunits * 2)
3100 if (*needs_first_vector || *mask_fixed)
3102 /* We either need the first vector too or have already moved to the
3103 next vector. In both cases, this permutation needs three
3104 vectors. */
3105 if (dump_enabled_p ())
3107 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3108 "permutation requires at "
3109 "least three vectors ");
3110 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3111 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3114 return false;
3117 /* We move to the next vector, dropping the first one and working with
3118 the second and the third - we need to adjust the values of the mask
3119 accordingly. */
3120 *current_mask_element -= mask_nunits * *number_of_mask_fixes;
3122 for (i = 0; i < index; i++)
3123 mask[i] -= mask_nunits * *number_of_mask_fixes;
3125 (*number_of_mask_fixes)++;
3126 *mask_fixed = true;
3129 *need_next_vector = *mask_fixed;
3131 /* This was the last element of this mask. Start a new one. */
3132 if (index == mask_nunits - 1)
3134 *number_of_mask_fixes = 1;
3135 *mask_fixed = false;
3136 *needs_first_vector = false;
3139 return true;
3143 /* Generate vector permute statements from a list of loads in DR_CHAIN.
3144 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
3145 permute statements for the SLP node NODE of the SLP instance
3146 SLP_NODE_INSTANCE. */
3148 bool
3149 vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
3150 gimple_stmt_iterator *gsi, int vf,
3151 slp_instance slp_node_instance, bool analyze_only)
3153 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3154 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3155 tree mask_element_type = NULL_TREE, mask_type;
3156 int i, j, k, nunits, vec_index = 0;
3157 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3158 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
3159 int first_mask_element;
3160 int index, unroll_factor, current_mask_element, ncopies;
3161 unsigned char *mask;
3162 bool only_one_vec = false, need_next_vector = false;
3163 int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
3164 int number_of_mask_fixes = 1;
3165 bool mask_fixed = false;
3166 bool needs_first_vector = false;
3167 machine_mode mode;
3169 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3170 return false;
3172 stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info));
3174 mode = TYPE_MODE (vectype);
3176 if (!can_vec_perm_p (mode, false, NULL))
3178 if (dump_enabled_p ())
3180 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3181 "no vect permute for ");
3182 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3183 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3185 return false;
3188 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3189 same size as the vector element being permuted. */
3190 mask_element_type = lang_hooks.types.type_for_mode
3191 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
3192 mask_type = get_vectype_for_scalar_type (mask_element_type);
3193 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3194 mask = XALLOCAVEC (unsigned char, nunits);
3195 unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3197 /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
3198 unrolling factor. */
3199 orig_vec_stmts_num
3200 = (STMT_VINFO_GROUP_SIZE (stmt_info)
3201 * SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance)
3202 + nunits - 1) / nunits;
3203 if (orig_vec_stmts_num == 1)
3204 only_one_vec = true;
3206 /* Number of copies is determined by the final vectorization factor
3207 relatively to SLP_NODE_INSTANCE unrolling factor. */
3208 ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3210 /* Generate permutation masks for every NODE. Number of masks for each NODE
3211 is equal to GROUP_SIZE.
3212 E.g., we have a group of three nodes with three loads from the same
3213 location in each node, and the vector size is 4. I.e., we have a
3214 a0b0c0a1b1c1... sequence and we need to create the following vectors:
3215 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3216 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3219 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
3220 The last mask is illegal since we assume two operands for permute
3221 operation, and the mask element values can't be outside that range.
3222 Hence, the last mask must be converted into {2,5,5,5}.
3223 For the first two permutations we need the first and the second input
3224 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
3225 we need the second and the third vectors: {b1,c1,a2,b2} and
3226 {c2,a3,b3,c3}. */
3229 index = 0;
3230 vect_stmts_counter = 0;
3231 vec_index = 0;
3232 first_vec_index = vec_index++;
3233 if (only_one_vec)
3234 second_vec_index = first_vec_index;
3235 else
3236 second_vec_index = vec_index++;
3238 for (j = 0; j < unroll_factor; j++)
3240 for (k = 0; k < group_size; k++)
3242 i = SLP_TREE_LOAD_PERMUTATION (node)[k];
3243 first_mask_element = i + j * STMT_VINFO_GROUP_SIZE (stmt_info);
3244 if (!vect_get_mask_element (stmt, first_mask_element, 0,
3245 nunits, only_one_vec, index,
3246 mask, &current_mask_element,
3247 &need_next_vector,
3248 &number_of_mask_fixes, &mask_fixed,
3249 &needs_first_vector))
3250 return false;
3251 gcc_assert (current_mask_element >= 0
3252 && current_mask_element < 2 * nunits);
3253 mask[index++] = current_mask_element;
3255 if (index == nunits)
3257 index = 0;
3258 if (!can_vec_perm_p (mode, false, mask))
3260 if (dump_enabled_p ())
3262 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3263 vect_location,
3264 "unsupported vect permute { ");
3265 for (i = 0; i < nunits; ++i)
3266 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ",
3267 mask[i]);
3268 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3270 return false;
3273 if (!analyze_only)
3275 int l;
3276 tree mask_vec, *mask_elts;
3277 mask_elts = XALLOCAVEC (tree, nunits);
3278 for (l = 0; l < nunits; ++l)
3279 mask_elts[l] = build_int_cst (mask_element_type,
3280 mask[l]);
3281 mask_vec = build_vector (mask_type, mask_elts);
3283 if (need_next_vector)
3285 first_vec_index = second_vec_index;
3286 second_vec_index = vec_index;
3289 vect_create_mask_and_perm (stmt,
3290 mask_vec, first_vec_index, second_vec_index,
3291 gsi, node, vectype, dr_chain,
3292 ncopies, vect_stmts_counter++);
3299 return true;
3304 /* Vectorize SLP instance tree in postorder. */
3306 static bool
3307 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3308 unsigned int vectorization_factor)
3310 gimple stmt;
3311 bool grouped_store, is_store;
3312 gimple_stmt_iterator si;
3313 stmt_vec_info stmt_info;
3314 unsigned int vec_stmts_size, nunits, group_size;
3315 tree vectype;
3316 int i;
3317 slp_tree child;
3319 if (!node)
3320 return false;
3322 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3323 vect_schedule_slp_instance (child, instance, vectorization_factor);
3325 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3326 stmt_info = vinfo_for_stmt (stmt);
3328 /* VECTYPE is the type of the destination. */
3329 vectype = STMT_VINFO_VECTYPE (stmt_info);
3330 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3331 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3333 /* For each SLP instance calculate number of vector stmts to be created
3334 for the scalar stmts in each node of the SLP tree. Number of vector
3335 elements in one vector iteration is the number of scalar elements in
3336 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3337 size.
3338 Unless this is a SLP reduction in which case the number of vector
3339 stmts is equal to the number of vector stmts of the children. */
3340 if (GROUP_FIRST_ELEMENT (stmt_info)
3341 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
3342 vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
3343 else
3344 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3346 if (!SLP_TREE_VEC_STMTS (node).exists ())
3348 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3349 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3352 if (dump_enabled_p ())
3354 dump_printf_loc (MSG_NOTE,vect_location,
3355 "------>vectorizing SLP node starting from: ");
3356 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3357 dump_printf (MSG_NOTE, "\n");
3360 /* Vectorized stmts go before the last scalar stmt which is where
3361 all uses are ready. */
3362 si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node));
3364 /* Mark the first element of the reduction chain as reduction to properly
3365 transform the node. In the analysis phase only the last element of the
3366 chain is marked as reduction. */
3367 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3368 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3370 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3371 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3374 /* Handle two-operation SLP nodes by vectorizing the group with
3375 both operations and then performing a merge. */
3376 if (SLP_TREE_TWO_OPERATORS (node))
3378 enum tree_code code0 = gimple_assign_rhs_code (stmt);
3379 enum tree_code ocode;
3380 gimple ostmt;
3381 unsigned char *mask = XALLOCAVEC (unsigned char, group_size);
3382 bool allsame = true;
3383 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt)
3384 if (gimple_assign_rhs_code (ostmt) != code0)
3386 mask[i] = 1;
3387 allsame = false;
3388 ocode = gimple_assign_rhs_code (ostmt);
3390 else
3391 mask[i] = 0;
3392 if (!allsame)
3394 vec<gimple> v0;
3395 vec<gimple> v1;
3396 unsigned j;
3397 tree tmask = NULL_TREE;
3398 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3399 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3400 SLP_TREE_VEC_STMTS (node).truncate (0);
3401 gimple_assign_set_rhs_code (stmt, ocode);
3402 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3403 gimple_assign_set_rhs_code (stmt, code0);
3404 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3405 SLP_TREE_VEC_STMTS (node).truncate (0);
3406 tree meltype = build_nonstandard_integer_type
3407 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1);
3408 tree mvectype = get_same_sized_vectype (meltype, vectype);
3409 unsigned k = 0, l;
3410 for (j = 0; j < v0.length (); ++j)
3412 tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype));
3413 for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l)
3415 if (k >= group_size)
3416 k = 0;
3417 melts[l] = build_int_cst
3418 (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l);
3420 tmask = build_vector (mvectype, melts);
3422 /* ??? Not all targets support a VEC_PERM_EXPR with a
3423 constant mask that would translate to a vec_merge RTX
3424 (with their vec_perm_const_ok). We can either not
3425 vectorize in that case or let veclower do its job.
3426 Unfortunately that isn't too great and at least for
3427 plus/minus we'd eventually like to match targets
3428 vector addsub instructions. */
3429 gimple vstmt;
3430 vstmt = gimple_build_assign (make_ssa_name (vectype),
3431 VEC_PERM_EXPR,
3432 gimple_assign_lhs (v0[j]),
3433 gimple_assign_lhs (v1[j]), tmask);
3434 vect_finish_stmt_generation (stmt, vstmt, &si);
3435 SLP_TREE_VEC_STMTS (node).quick_push (vstmt);
3437 v0.release ();
3438 v1.release ();
3439 return false;
3442 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3443 return is_store;
3446 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3447 For loop vectorization this is done in vectorizable_call, but for SLP
3448 it needs to be deferred until end of vect_schedule_slp, because multiple
3449 SLP instances may refer to the same scalar stmt. */
3451 static void
3452 vect_remove_slp_scalar_calls (slp_tree node)
3454 gimple stmt, new_stmt;
3455 gimple_stmt_iterator gsi;
3456 int i;
3457 slp_tree child;
3458 tree lhs;
3459 stmt_vec_info stmt_info;
3461 if (!node)
3462 return;
3464 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3465 vect_remove_slp_scalar_calls (child);
3467 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3469 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3470 continue;
3471 stmt_info = vinfo_for_stmt (stmt);
3472 if (stmt_info == NULL
3473 || is_pattern_stmt_p (stmt_info)
3474 || !PURE_SLP_STMT (stmt_info))
3475 continue;
3476 lhs = gimple_call_lhs (stmt);
3477 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3478 set_vinfo_for_stmt (new_stmt, stmt_info);
3479 set_vinfo_for_stmt (stmt, NULL);
3480 STMT_VINFO_STMT (stmt_info) = new_stmt;
3481 gsi = gsi_for_stmt (stmt);
3482 gsi_replace (&gsi, new_stmt, false);
3483 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3487 /* Generate vector code for all SLP instances in the loop/basic block. */
3489 bool
3490 vect_schedule_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
3492 vec<slp_instance> slp_instances;
3493 slp_instance instance;
3494 unsigned int i, vf;
3495 bool is_store = false;
3497 if (loop_vinfo)
3499 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3500 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3502 else
3504 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
3505 vf = 1;
3508 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3510 /* Schedule the tree of INSTANCE. */
3511 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3512 instance, vf);
3513 if (dump_enabled_p ())
3514 dump_printf_loc (MSG_NOTE, vect_location,
3515 "vectorizing stmts using SLP.\n");
3518 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3520 slp_tree root = SLP_INSTANCE_TREE (instance);
3521 gimple store;
3522 unsigned int j;
3523 gimple_stmt_iterator gsi;
3525 /* Remove scalar call stmts. Do not do this for basic-block
3526 vectorization as not all uses may be vectorized.
3527 ??? Why should this be necessary? DCE should be able to
3528 remove the stmts itself.
3529 ??? For BB vectorization we can as well remove scalar
3530 stmts starting from the SLP tree root if they have no
3531 uses. */
3532 if (loop_vinfo)
3533 vect_remove_slp_scalar_calls (root);
3535 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3536 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3538 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3539 break;
3541 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3542 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3543 /* Free the attached stmt_vec_info and remove the stmt. */
3544 gsi = gsi_for_stmt (store);
3545 unlink_stmt_vdef (store);
3546 gsi_remove (&gsi, true);
3547 release_defs (store);
3548 free_stmt_vec_info (store);
3552 return is_store;
3556 /* Vectorize the basic block. */
3558 void
3559 vect_slp_transform_bb (basic_block bb)
3561 bb_vec_info bb_vinfo = vec_info_for_bb (bb);
3562 gimple_stmt_iterator si;
3564 gcc_assert (bb_vinfo);
3566 if (dump_enabled_p ())
3567 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB\n");
3569 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
3571 gimple stmt = gsi_stmt (si);
3572 stmt_vec_info stmt_info;
3574 if (dump_enabled_p ())
3576 dump_printf_loc (MSG_NOTE, vect_location,
3577 "------>SLPing statement: ");
3578 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3579 dump_printf (MSG_NOTE, "\n");
3582 stmt_info = vinfo_for_stmt (stmt);
3583 gcc_assert (stmt_info);
3585 /* Schedule all the SLP instances when the first SLP stmt is reached. */
3586 if (STMT_SLP_TYPE (stmt_info))
3588 vect_schedule_slp (NULL, bb_vinfo);
3589 break;
3593 if (dump_enabled_p ())
3594 dump_printf_loc (MSG_NOTE, vect_location,
3595 "BASIC BLOCK VECTORIZED\n");
3597 destroy_bb_vec_info (bb_vinfo);