* cp-tree.h (struct lang_type): Remove template_info field.
[official-gcc.git] / gcc / tree-vect-slp.c
blob04ecaab7fc35c98d1c5cc787dd8bb457b39eb2ef
1 /* SLP - Basic Block Vectorization
2 Copyright (C) 2007-2017 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "insn-config.h"
34 #include "recog.h" /* FIXME: for insn_data */
35 #include "params.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "gimple-iterator.h"
39 #include "cfgloop.h"
40 #include "tree-vectorizer.h"
41 #include "langhooks.h"
42 #include "gimple-walk.h"
43 #include "dbgcnt.h"
46 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
48 static void
49 vect_free_slp_tree (slp_tree node)
51 int i;
52 slp_tree child;
54 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
55 vect_free_slp_tree (child);
57 gimple *stmt;
58 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
59 /* After transform some stmts are removed and thus their vinfo is gone. */
60 if (vinfo_for_stmt (stmt))
62 gcc_assert (STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt)) > 0);
63 STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt))--;
66 SLP_TREE_CHILDREN (node).release ();
67 SLP_TREE_SCALAR_STMTS (node).release ();
68 SLP_TREE_VEC_STMTS (node).release ();
69 SLP_TREE_LOAD_PERMUTATION (node).release ();
71 free (node);
75 /* Free the memory allocated for the SLP instance. */
77 void
78 vect_free_slp_instance (slp_instance instance)
80 vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
81 SLP_INSTANCE_LOADS (instance).release ();
82 free (instance);
86 /* Create an SLP node for SCALAR_STMTS. */
88 static slp_tree
89 vect_create_new_slp_node (vec<gimple *> scalar_stmts)
91 slp_tree node;
92 gimple *stmt = scalar_stmts[0];
93 unsigned int nops;
95 if (is_gimple_call (stmt))
96 nops = gimple_call_num_args (stmt);
97 else if (is_gimple_assign (stmt))
99 nops = gimple_num_ops (stmt) - 1;
100 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
101 nops++;
103 else if (gimple_code (stmt) == GIMPLE_PHI)
104 nops = 0;
105 else
106 return NULL;
108 node = XNEW (struct _slp_tree);
109 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
110 SLP_TREE_VEC_STMTS (node).create (0);
111 SLP_TREE_CHILDREN (node).create (nops);
112 SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
113 SLP_TREE_TWO_OPERATORS (node) = false;
114 SLP_TREE_DEF_TYPE (node) = vect_internal_def;
116 unsigned i;
117 FOR_EACH_VEC_ELT (scalar_stmts, i, stmt)
118 STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt))++;
120 return node;
124 /* This structure is used in creation of an SLP tree. Each instance
125 corresponds to the same operand in a group of scalar stmts in an SLP
126 node. */
127 typedef struct _slp_oprnd_info
129 /* Def-stmts for the operands. */
130 vec<gimple *> def_stmts;
131 /* Information about the first statement, its vector def-type, type, the
132 operand itself in case it's constant, and an indication if it's a pattern
133 stmt. */
134 tree first_op_type;
135 enum vect_def_type first_dt;
136 bool first_pattern;
137 bool second_pattern;
138 } *slp_oprnd_info;
141 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
142 operand. */
143 static vec<slp_oprnd_info>
144 vect_create_oprnd_info (int nops, int group_size)
146 int i;
147 slp_oprnd_info oprnd_info;
148 vec<slp_oprnd_info> oprnds_info;
150 oprnds_info.create (nops);
151 for (i = 0; i < nops; i++)
153 oprnd_info = XNEW (struct _slp_oprnd_info);
154 oprnd_info->def_stmts.create (group_size);
155 oprnd_info->first_dt = vect_uninitialized_def;
156 oprnd_info->first_op_type = NULL_TREE;
157 oprnd_info->first_pattern = false;
158 oprnd_info->second_pattern = false;
159 oprnds_info.quick_push (oprnd_info);
162 return oprnds_info;
166 /* Free operands info. */
168 static void
169 vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
171 int i;
172 slp_oprnd_info oprnd_info;
174 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
176 oprnd_info->def_stmts.release ();
177 XDELETE (oprnd_info);
180 oprnds_info.release ();
184 /* Find the place of the data-ref in STMT in the interleaving chain that starts
185 from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
187 static int
188 vect_get_place_in_interleaving_chain (gimple *stmt, gimple *first_stmt)
190 gimple *next_stmt = first_stmt;
191 int result = 0;
193 if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
194 return -1;
198 if (next_stmt == stmt)
199 return result;
200 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
201 if (next_stmt)
202 result += GROUP_GAP (vinfo_for_stmt (next_stmt));
204 while (next_stmt);
206 return -1;
210 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
211 they are of a valid type and that they match the defs of the first stmt of
212 the SLP group (stored in OPRNDS_INFO). This function tries to match stmts
213 by swapping operands of STMT when possible. Non-zero *SWAP indicates swap
214 is required for cond_expr stmts. Specifically, *SWAP is 1 if STMT is cond
215 and operands of comparison need to be swapped; *SWAP is 2 if STMT is cond
216 and code of comparison needs to be inverted. If there is any operand swap
217 in this function, *SWAP is set to non-zero value.
218 If there was a fatal error return -1; if the error could be corrected by
219 swapping operands of father node of this one, return 1; if everything is
220 ok return 0. */
222 static int
223 vect_get_and_check_slp_defs (vec_info *vinfo, unsigned char *swap,
224 gimple *stmt, unsigned stmt_num,
225 vec<slp_oprnd_info> *oprnds_info)
227 tree oprnd;
228 unsigned int i, number_of_oprnds;
229 gimple *def_stmt;
230 enum vect_def_type dt = vect_uninitialized_def;
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 (is_gimple_call (stmt))
241 number_of_oprnds = gimple_call_num_args (stmt);
242 first_op_idx = 3;
244 else if (is_gimple_assign (stmt))
246 enum tree_code code = gimple_assign_rhs_code (stmt);
247 number_of_oprnds = gimple_num_ops (stmt) - 1;
248 /* Swap can only be done for cond_expr if asked to, otherwise we
249 could result in different comparison code to the first stmt. */
250 if (gimple_assign_rhs_code (stmt) == COND_EXPR
251 && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt)))
253 first_op_cond = true;
254 number_of_oprnds++;
256 else
257 commutative = commutative_tree_code (code);
259 else
260 return -1;
262 bool swapped = (*swap != 0);
263 gcc_assert (!swapped || first_op_cond);
264 for (i = 0; i < number_of_oprnds; i++)
266 again:
267 if (first_op_cond)
269 /* Map indicating how operands of cond_expr should be swapped. */
270 int maps[3][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 1, 3, 2}};
271 int *map = maps[*swap];
273 if (i < 2)
274 oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx), map[i]);
275 else
276 oprnd = gimple_op (stmt, map[i]);
278 else
279 oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i));
281 oprnd_info = (*oprnds_info)[i];
283 if (!vect_is_simple_use (oprnd, vinfo, &def_stmt, &dt))
285 if (dump_enabled_p ())
287 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
288 "Build SLP failed: can't analyze def for ");
289 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
290 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
293 return -1;
296 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
297 from the pattern. Check that all the stmts of the node are in the
298 pattern. */
299 if (def_stmt && gimple_bb (def_stmt)
300 && vect_stmt_in_region_p (vinfo, def_stmt)
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 case GIMPLE_ASSIGN:
348 break;
350 default:
351 if (dump_enabled_p ())
352 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
353 "unsupported defining stmt:\n");
354 return -1;
358 if (second)
359 oprnd_info->second_pattern = pattern;
361 if (first)
363 oprnd_info->first_dt = dt;
364 oprnd_info->first_pattern = pattern;
365 oprnd_info->first_op_type = TREE_TYPE (oprnd);
367 else
369 /* Not first stmt of the group, check that the def-stmt/s match
370 the def-stmt/s of the first stmt. Allow different definition
371 types for reduction chains: the first stmt must be a
372 vect_reduction_def (a phi node), and the rest
373 vect_internal_def. */
374 if (((oprnd_info->first_dt != dt
375 && !(oprnd_info->first_dt == vect_reduction_def
376 && dt == vect_internal_def)
377 && !((oprnd_info->first_dt == vect_external_def
378 || oprnd_info->first_dt == vect_constant_def)
379 && (dt == vect_external_def
380 || dt == vect_constant_def)))
381 || !types_compatible_p (oprnd_info->first_op_type,
382 TREE_TYPE (oprnd))))
384 /* Try swapping operands if we got a mismatch. */
385 if (i == 0
386 && !swapped
387 && commutative)
389 swapped = true;
390 goto again;
393 if (dump_enabled_p ())
394 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
395 "Build SLP failed: different types\n");
397 return 1;
401 /* Check the types of the definitions. */
402 switch (dt)
404 case vect_constant_def:
405 case vect_external_def:
406 break;
408 case vect_reduction_def:
409 case vect_induction_def:
410 case vect_internal_def:
411 oprnd_info->def_stmts.quick_push (def_stmt);
412 break;
414 default:
415 /* FORNOW: Not supported. */
416 if (dump_enabled_p ())
418 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
419 "Build SLP failed: illegal type of def ");
420 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
421 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
424 return -1;
428 /* Swap operands. */
429 if (swapped)
431 /* If there are already uses of this stmt in a SLP instance then
432 we've committed to the operand order and can't swap it. */
433 if (STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmt)) != 0)
435 if (dump_enabled_p ())
437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
438 "Build SLP failed: cannot swap operands of "
439 "shared stmt ");
440 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
442 return -1;
445 if (first_op_cond)
447 tree cond = gimple_assign_rhs1 (stmt);
448 enum tree_code code = TREE_CODE (cond);
450 /* Swap. */
451 if (*swap == 1)
453 swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
454 &TREE_OPERAND (cond, 1));
455 TREE_SET_CODE (cond, swap_tree_comparison (code));
457 /* Invert. */
458 else
460 swap_ssa_operands (stmt, gimple_assign_rhs2_ptr (stmt),
461 gimple_assign_rhs3_ptr (stmt));
462 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond, 0));
463 code = invert_tree_comparison (TREE_CODE (cond), honor_nans);
464 gcc_assert (code != ERROR_MARK);
465 TREE_SET_CODE (cond, code);
468 else
469 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
470 gimple_assign_rhs2_ptr (stmt));
471 if (dump_enabled_p ())
473 dump_printf_loc (MSG_NOTE, vect_location,
474 "swapped operands to match def types in ");
475 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
479 *swap = swapped;
480 return 0;
484 /* Verify if the scalar stmts STMTS are isomorphic, require data
485 permutation or are of unsupported types of operation. Return
486 true if they are, otherwise return false and indicate in *MATCHES
487 which stmts are not isomorphic to the first one. If MATCHES[0]
488 is false then this indicates the comparison could not be
489 carried out or the stmts will never be vectorized by SLP.
491 Note COND_EXPR is possibly ismorphic to another one after swapping its
492 operands. Set SWAP[i] to 1 if stmt I is COND_EXPR and isomorphic to
493 the first stmt by swapping the two operands of comparison; set SWAP[i]
494 to 2 if stmt I is isormorphic to the first stmt by inverting the code
495 of comparison. Take A1 >= B1 ? X1 : Y1 as an exmple, it can be swapped
496 to (B1 <= A1 ? X1 : Y1); or be inverted to (A1 < B1) ? Y1 : X1. */
498 static bool
499 vect_build_slp_tree_1 (vec_info *vinfo, unsigned char *swap,
500 vec<gimple *> stmts, unsigned int group_size,
501 unsigned nops, unsigned int *max_nunits,
502 bool *matches, bool *two_operators)
504 unsigned int i;
505 gimple *first_stmt = stmts[0], *stmt = stmts[0];
506 enum tree_code first_stmt_code = ERROR_MARK;
507 enum tree_code alt_stmt_code = ERROR_MARK;
508 enum tree_code rhs_code = ERROR_MARK;
509 enum tree_code first_cond_code = ERROR_MARK;
510 tree lhs;
511 bool need_same_oprnds = false;
512 tree vectype = NULL_TREE, scalar_type, first_op1 = NULL_TREE;
513 optab optab;
514 int icode;
515 machine_mode optab_op2_mode;
516 machine_mode vec_mode;
517 HOST_WIDE_INT dummy;
518 gimple *first_load = NULL, *prev_first_load = NULL;
520 /* For every stmt in NODE find its def stmt/s. */
521 FOR_EACH_VEC_ELT (stmts, i, stmt)
523 swap[i] = 0;
524 matches[i] = false;
526 if (dump_enabled_p ())
528 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
529 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
532 /* Fail to vectorize statements marked as unvectorizable. */
533 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
535 if (dump_enabled_p ())
537 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
538 "Build SLP failed: unvectorizable statement ");
539 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
541 /* Fatal mismatch. */
542 matches[0] = false;
543 return false;
546 lhs = gimple_get_lhs (stmt);
547 if (lhs == NULL_TREE)
549 if (dump_enabled_p ())
551 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
552 "Build SLP failed: not GIMPLE_ASSIGN nor "
553 "GIMPLE_CALL ");
554 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
556 /* Fatal mismatch. */
557 matches[0] = false;
558 return false;
561 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
562 vectype = get_vectype_for_scalar_type (scalar_type);
563 if (!vectype)
565 if (dump_enabled_p ())
567 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
568 "Build SLP failed: unsupported data-type ");
569 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
570 scalar_type);
571 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
573 /* Fatal mismatch. */
574 matches[0] = false;
575 return false;
578 /* If populating the vector type requires unrolling then fail
579 before adjusting *max_nunits for basic-block vectorization. */
580 if (is_a <bb_vec_info> (vinfo)
581 && TYPE_VECTOR_SUBPARTS (vectype) > group_size)
583 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
584 "Build SLP failed: unrolling required "
585 "in basic block SLP\n");
586 /* Fatal mismatch. */
587 matches[0] = false;
588 return false;
591 /* In case of multiple types we need to detect the smallest type. */
592 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
593 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
595 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
597 rhs_code = CALL_EXPR;
598 if (gimple_call_internal_p (call_stmt)
599 || gimple_call_tail_p (call_stmt)
600 || gimple_call_noreturn_p (call_stmt)
601 || !gimple_call_nothrow_p (call_stmt)
602 || gimple_call_chain (call_stmt))
604 if (dump_enabled_p ())
606 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
607 "Build SLP failed: unsupported call type ");
608 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
609 call_stmt, 0);
611 /* Fatal mismatch. */
612 matches[0] = false;
613 return false;
616 else
617 rhs_code = gimple_assign_rhs_code (stmt);
619 /* Check the operation. */
620 if (i == 0)
622 first_stmt_code = rhs_code;
624 /* Shift arguments should be equal in all the packed stmts for a
625 vector shift with scalar shift operand. */
626 if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
627 || rhs_code == LROTATE_EXPR
628 || rhs_code == RROTATE_EXPR)
630 vec_mode = TYPE_MODE (vectype);
632 /* First see if we have a vector/vector shift. */
633 optab = optab_for_tree_code (rhs_code, vectype,
634 optab_vector);
636 if (!optab
637 || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
639 /* No vector/vector shift, try for a vector/scalar shift. */
640 optab = optab_for_tree_code (rhs_code, vectype,
641 optab_scalar);
643 if (!optab)
645 if (dump_enabled_p ())
646 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
647 "Build SLP failed: no optab.\n");
648 /* Fatal mismatch. */
649 matches[0] = false;
650 return false;
652 icode = (int) optab_handler (optab, vec_mode);
653 if (icode == CODE_FOR_nothing)
655 if (dump_enabled_p ())
656 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
657 "Build SLP failed: "
658 "op not supported by target.\n");
659 /* Fatal mismatch. */
660 matches[0] = false;
661 return false;
663 optab_op2_mode = insn_data[icode].operand[2].mode;
664 if (!VECTOR_MODE_P (optab_op2_mode))
666 need_same_oprnds = true;
667 first_op1 = gimple_assign_rhs2 (stmt);
671 else if (rhs_code == WIDEN_LSHIFT_EXPR)
673 need_same_oprnds = true;
674 first_op1 = gimple_assign_rhs2 (stmt);
677 else
679 if (first_stmt_code != rhs_code
680 && alt_stmt_code == ERROR_MARK)
681 alt_stmt_code = rhs_code;
682 if (first_stmt_code != rhs_code
683 && (first_stmt_code != IMAGPART_EXPR
684 || rhs_code != REALPART_EXPR)
685 && (first_stmt_code != REALPART_EXPR
686 || rhs_code != IMAGPART_EXPR)
687 /* Handle mismatches in plus/minus by computing both
688 and merging the results. */
689 && !((first_stmt_code == PLUS_EXPR
690 || first_stmt_code == MINUS_EXPR)
691 && (alt_stmt_code == PLUS_EXPR
692 || alt_stmt_code == MINUS_EXPR)
693 && rhs_code == alt_stmt_code)
694 && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
695 && (first_stmt_code == ARRAY_REF
696 || first_stmt_code == BIT_FIELD_REF
697 || first_stmt_code == INDIRECT_REF
698 || first_stmt_code == COMPONENT_REF
699 || first_stmt_code == MEM_REF)))
701 if (dump_enabled_p ())
703 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
704 "Build SLP failed: different operation "
705 "in stmt ");
706 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
707 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
708 "original stmt ");
709 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
710 first_stmt, 0);
712 /* Mismatch. */
713 continue;
716 if (need_same_oprnds
717 && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
719 if (dump_enabled_p ())
721 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
722 "Build SLP failed: different shift "
723 "arguments in ");
724 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
726 /* Mismatch. */
727 continue;
730 if (rhs_code == CALL_EXPR)
732 gimple *first_stmt = stmts[0];
733 if (gimple_call_num_args (stmt) != nops
734 || !operand_equal_p (gimple_call_fn (first_stmt),
735 gimple_call_fn (stmt), 0)
736 || gimple_call_fntype (first_stmt)
737 != gimple_call_fntype (stmt))
739 if (dump_enabled_p ())
741 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
742 "Build SLP failed: different calls in ");
743 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
744 stmt, 0);
746 /* Mismatch. */
747 continue;
752 /* Grouped store or load. */
753 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
755 if (REFERENCE_CLASS_P (lhs))
757 /* Store. */
760 else
762 /* Load. */
763 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
764 if (prev_first_load)
766 /* Check that there are no loads from different interleaving
767 chains in the same node. */
768 if (prev_first_load != first_load)
770 if (dump_enabled_p ())
772 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
773 vect_location,
774 "Build SLP failed: different "
775 "interleaving chains in one node ");
776 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
777 stmt, 0);
779 /* Mismatch. */
780 continue;
783 else
784 prev_first_load = first_load;
786 } /* Grouped access. */
787 else
789 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
791 /* Not grouped load. */
792 if (dump_enabled_p ())
794 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
795 "Build SLP failed: not grouped load ");
796 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
799 /* FORNOW: Not grouped loads are not supported. */
800 /* Fatal mismatch. */
801 matches[0] = false;
802 return false;
805 /* Not memory operation. */
806 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
807 && TREE_CODE_CLASS (rhs_code) != tcc_unary
808 && TREE_CODE_CLASS (rhs_code) != tcc_expression
809 && TREE_CODE_CLASS (rhs_code) != tcc_comparison
810 && rhs_code != CALL_EXPR)
812 if (dump_enabled_p ())
814 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
815 "Build SLP failed: operation");
816 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
817 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
819 /* Fatal mismatch. */
820 matches[0] = false;
821 return false;
824 if (rhs_code == COND_EXPR)
826 tree cond_expr = gimple_assign_rhs1 (stmt);
827 enum tree_code cond_code = TREE_CODE (cond_expr);
828 enum tree_code swap_code = ERROR_MARK;
829 enum tree_code invert_code = ERROR_MARK;
831 if (i == 0)
832 first_cond_code = TREE_CODE (cond_expr);
833 else if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
835 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
836 swap_code = swap_tree_comparison (cond_code);
837 invert_code = invert_tree_comparison (cond_code, honor_nans);
840 if (first_cond_code == cond_code)
842 /* Isomorphic can be achieved by swapping. */
843 else if (first_cond_code == swap_code)
844 swap[i] = 1;
845 /* Isomorphic can be achieved by inverting. */
846 else if (first_cond_code == invert_code)
847 swap[i] = 2;
848 else
850 if (dump_enabled_p ())
852 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
853 "Build SLP failed: different"
854 " operation");
855 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
856 stmt, 0);
858 /* Mismatch. */
859 continue;
864 matches[i] = true;
867 for (i = 0; i < group_size; ++i)
868 if (!matches[i])
869 return false;
871 /* If we allowed a two-operation SLP node verify the target can cope
872 with the permute we are going to use. */
873 if (alt_stmt_code != ERROR_MARK
874 && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference)
876 unsigned char *sel
877 = XALLOCAVEC (unsigned char, TYPE_VECTOR_SUBPARTS (vectype));
878 for (i = 0; i < TYPE_VECTOR_SUBPARTS (vectype); ++i)
880 sel[i] = i;
881 if (gimple_assign_rhs_code (stmts[i % group_size]) == alt_stmt_code)
882 sel[i] += TYPE_VECTOR_SUBPARTS (vectype);
884 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
886 for (i = 0; i < group_size; ++i)
887 if (gimple_assign_rhs_code (stmts[i]) == alt_stmt_code)
889 matches[i] = false;
890 if (dump_enabled_p ())
892 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
893 "Build SLP failed: different operation "
894 "in stmt ");
895 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
896 stmts[i], 0);
897 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
898 "original stmt ");
899 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
900 first_stmt, 0);
903 return false;
905 *two_operators = true;
908 return true;
911 /* Traits for the hash_set to record failed SLP builds for a stmt set.
912 Note we never remove apart from at destruction time so we do not
913 need a special value for deleted that differs from empty. */
914 struct bst_traits
916 typedef vec <gimple *> value_type;
917 typedef vec <gimple *> compare_type;
918 static inline hashval_t hash (value_type);
919 static inline bool equal (value_type existing, value_type candidate);
920 static inline bool is_empty (value_type x) { return !x.exists (); }
921 static inline bool is_deleted (value_type x) { return !x.exists (); }
922 static inline void mark_empty (value_type &x) { x.release (); }
923 static inline void mark_deleted (value_type &x) { x.release (); }
924 static inline void remove (value_type &x) { x.release (); }
926 inline hashval_t
927 bst_traits::hash (value_type x)
929 inchash::hash h;
930 for (unsigned i = 0; i < x.length (); ++i)
931 h.add_int (gimple_uid (x[i]));
932 return h.end ();
934 inline bool
935 bst_traits::equal (value_type existing, value_type candidate)
937 if (existing.length () != candidate.length ())
938 return false;
939 for (unsigned i = 0; i < existing.length (); ++i)
940 if (existing[i] != candidate[i])
941 return false;
942 return true;
945 static hash_set <vec <gimple *>, bst_traits> *bst_fail;
947 static slp_tree
948 vect_build_slp_tree_2 (vec_info *vinfo,
949 vec<gimple *> stmts, unsigned int group_size,
950 unsigned int *max_nunits,
951 vec<slp_tree> *loads,
952 bool *matches, unsigned *npermutes, unsigned *tree_size,
953 unsigned max_tree_size);
955 static slp_tree
956 vect_build_slp_tree (vec_info *vinfo,
957 vec<gimple *> stmts, unsigned int group_size,
958 unsigned int *max_nunits,
959 vec<slp_tree> *loads,
960 bool *matches, unsigned *npermutes, unsigned *tree_size,
961 unsigned max_tree_size)
963 if (bst_fail->contains (stmts))
964 return NULL;
965 slp_tree res = vect_build_slp_tree_2 (vinfo, stmts, group_size, max_nunits,
966 loads, matches, npermutes, tree_size,
967 max_tree_size);
968 /* When SLP build fails for stmts record this, otherwise SLP build
969 can be exponential in time when we allow to construct parts from
970 scalars, see PR81723. */
971 if (! res)
973 vec <gimple *> x;
974 x.create (stmts.length ());
975 x.splice (stmts);
976 bst_fail->add (x);
978 return res;
981 /* Recursively build an SLP tree starting from NODE.
982 Fail (and return a value not equal to zero) if def-stmts are not
983 isomorphic, require data permutation or are of unsupported types of
984 operation. Otherwise, return 0.
985 The value returned is the depth in the SLP tree where a mismatch
986 was found. */
988 static slp_tree
989 vect_build_slp_tree_2 (vec_info *vinfo,
990 vec<gimple *> stmts, unsigned int group_size,
991 unsigned int *max_nunits,
992 vec<slp_tree> *loads,
993 bool *matches, unsigned *npermutes, unsigned *tree_size,
994 unsigned max_tree_size)
996 unsigned nops, i, this_tree_size = 0, this_max_nunits = *max_nunits;
997 gimple *stmt;
998 slp_tree node;
1000 matches[0] = false;
1002 stmt = stmts[0];
1003 if (is_gimple_call (stmt))
1004 nops = gimple_call_num_args (stmt);
1005 else if (is_gimple_assign (stmt))
1007 nops = gimple_num_ops (stmt) - 1;
1008 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
1009 nops++;
1011 else if (gimple_code (stmt) == GIMPLE_PHI)
1012 nops = 0;
1013 else
1014 return NULL;
1016 /* If the SLP node is a PHI (induction or reduction), terminate
1017 the recursion. */
1018 if (gimple_code (stmt) == GIMPLE_PHI)
1020 vect_def_type def_type = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt));
1021 /* Induction from different IVs is not supported. */
1022 if (def_type == vect_induction_def)
1024 FOR_EACH_VEC_ELT (stmts, i, stmt)
1025 if (stmt != stmts[0])
1026 return NULL;
1028 else
1030 /* Else def types have to match. */
1031 FOR_EACH_VEC_ELT (stmts, i, stmt)
1033 /* But for reduction chains only check on the first stmt. */
1034 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
1035 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != stmt)
1036 continue;
1037 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) != def_type)
1038 return NULL;
1041 node = vect_create_new_slp_node (stmts);
1042 return node;
1046 bool two_operators = false;
1047 unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
1048 if (!vect_build_slp_tree_1 (vinfo, swap,
1049 stmts, group_size, nops,
1050 &this_max_nunits, matches, &two_operators))
1051 return NULL;
1053 /* If the SLP node is a load, terminate the recursion. */
1054 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
1055 && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
1057 *max_nunits = this_max_nunits;
1058 node = vect_create_new_slp_node (stmts);
1059 loads->safe_push (node);
1060 return node;
1063 /* Get at the operands, verifying they are compatible. */
1064 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
1065 slp_oprnd_info oprnd_info;
1066 FOR_EACH_VEC_ELT (stmts, i, stmt)
1068 int res = vect_get_and_check_slp_defs (vinfo, &swap[i],
1069 stmt, i, &oprnds_info);
1070 if (res != 0)
1071 matches[(res == -1) ? 0 : i] = false;
1072 if (!matches[0])
1073 break;
1075 for (i = 0; i < group_size; ++i)
1076 if (!matches[i])
1078 vect_free_oprnd_info (oprnds_info);
1079 return NULL;
1082 auto_vec<slp_tree, 4> children;
1083 auto_vec<slp_tree> this_loads;
1085 stmt = stmts[0];
1087 if (tree_size)
1088 max_tree_size -= *tree_size;
1090 /* Create SLP_TREE nodes for the definition node/s. */
1091 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
1093 slp_tree child;
1094 unsigned old_nloads = this_loads.length ();
1095 unsigned old_tree_size = this_tree_size;
1096 unsigned int j;
1098 if (oprnd_info->first_dt != vect_internal_def
1099 && oprnd_info->first_dt != vect_reduction_def
1100 && oprnd_info->first_dt != vect_induction_def)
1101 continue;
1103 if (++this_tree_size > max_tree_size)
1105 FOR_EACH_VEC_ELT (children, j, child)
1106 vect_free_slp_tree (child);
1107 vect_free_oprnd_info (oprnds_info);
1108 return NULL;
1111 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1112 group_size, &this_max_nunits,
1113 &this_loads, matches, npermutes,
1114 &this_tree_size,
1115 max_tree_size)) != NULL)
1117 /* If we have all children of child built up from scalars then just
1118 throw that away and build it up this node from scalars. */
1119 if (!SLP_TREE_CHILDREN (child).is_empty ()
1120 /* ??? Rejecting patterns this way doesn't work. We'd have to
1121 do extra work to cancel the pattern so the uses see the
1122 scalar version. */
1123 && !is_pattern_stmt_p
1124 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0])))
1126 slp_tree grandchild;
1128 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1129 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
1130 break;
1131 if (!grandchild)
1133 /* Roll back. */
1134 this_loads.truncate (old_nloads);
1135 this_tree_size = old_tree_size;
1136 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1137 vect_free_slp_tree (grandchild);
1138 SLP_TREE_CHILDREN (child).truncate (0);
1140 dump_printf_loc (MSG_NOTE, vect_location,
1141 "Building parent vector operands from "
1142 "scalars instead\n");
1143 oprnd_info->def_stmts = vNULL;
1144 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1145 children.safe_push (child);
1146 continue;
1150 oprnd_info->def_stmts = vNULL;
1151 children.safe_push (child);
1152 continue;
1155 /* If the SLP build failed fatally and we analyze a basic-block
1156 simply treat nodes we fail to build as externally defined
1157 (and thus build vectors from the scalar defs).
1158 The cost model will reject outright expensive cases.
1159 ??? This doesn't treat cases where permutation ultimatively
1160 fails (or we don't try permutation below). Ideally we'd
1161 even compute a permutation that will end up with the maximum
1162 SLP tree size... */
1163 if (is_a <bb_vec_info> (vinfo)
1164 && !matches[0]
1165 /* ??? Rejecting patterns this way doesn't work. We'd have to
1166 do extra work to cancel the pattern so the uses see the
1167 scalar version. */
1168 && !is_pattern_stmt_p (vinfo_for_stmt (stmt)))
1170 dump_printf_loc (MSG_NOTE, vect_location,
1171 "Building vector operands from scalars\n");
1172 child = vect_create_new_slp_node (oprnd_info->def_stmts);
1173 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1174 children.safe_push (child);
1175 oprnd_info->def_stmts = vNULL;
1176 continue;
1179 /* If the SLP build for operand zero failed and operand zero
1180 and one can be commutated try that for the scalar stmts
1181 that failed the match. */
1182 if (i == 0
1183 /* A first scalar stmt mismatch signals a fatal mismatch. */
1184 && matches[0]
1185 /* ??? For COND_EXPRs we can swap the comparison operands
1186 as well as the arms under some constraints. */
1187 && nops == 2
1188 && oprnds_info[1]->first_dt == vect_internal_def
1189 && is_gimple_assign (stmt)
1190 && commutative_tree_code (gimple_assign_rhs_code (stmt))
1191 && ! two_operators
1192 /* Do so only if the number of not successful permutes was nor more
1193 than a cut-ff as re-trying the recursive match on
1194 possibly each level of the tree would expose exponential
1195 behavior. */
1196 && *npermutes < 4)
1198 /* Verify if we can safely swap or if we committed to a specific
1199 operand order already. */
1200 for (j = 0; j < group_size; ++j)
1201 if (!matches[j]
1202 && (swap[j] != 0
1203 || STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmts[j]))))
1205 if (dump_enabled_p ())
1207 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1208 "Build SLP failed: cannot swap operands "
1209 "of shared stmt ");
1210 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1211 stmts[j], 0);
1213 goto fail;
1216 /* Swap mismatched definition stmts. */
1217 dump_printf_loc (MSG_NOTE, vect_location,
1218 "Re-trying with swapped operands of stmts ");
1219 for (j = 0; j < group_size; ++j)
1220 if (!matches[j])
1222 std::swap (oprnds_info[0]->def_stmts[j],
1223 oprnds_info[1]->def_stmts[j]);
1224 dump_printf (MSG_NOTE, "%d ", j);
1226 dump_printf (MSG_NOTE, "\n");
1227 /* And try again with scratch 'matches' ... */
1228 bool *tem = XALLOCAVEC (bool, group_size);
1229 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1230 group_size, &this_max_nunits,
1231 &this_loads, tem, npermutes,
1232 &this_tree_size,
1233 max_tree_size)) != NULL)
1235 /* ... so if successful we can apply the operand swapping
1236 to the GIMPLE IL. This is necessary because for example
1237 vect_get_slp_defs uses operand indexes and thus expects
1238 canonical operand order. This is also necessary even
1239 if we end up building the operand from scalars as
1240 we'll continue to process swapped operand two. */
1241 for (j = 0; j < group_size; ++j)
1243 gimple *stmt = stmts[j];
1244 gimple_set_plf (stmt, GF_PLF_1, false);
1246 for (j = 0; j < group_size; ++j)
1248 gimple *stmt = stmts[j];
1249 if (!matches[j])
1251 /* Avoid swapping operands twice. */
1252 if (gimple_plf (stmt, GF_PLF_1))
1253 continue;
1254 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1255 gimple_assign_rhs2_ptr (stmt));
1256 gimple_set_plf (stmt, GF_PLF_1, true);
1259 /* Verify we swap all duplicates or none. */
1260 if (flag_checking)
1261 for (j = 0; j < group_size; ++j)
1263 gimple *stmt = stmts[j];
1264 gcc_assert (gimple_plf (stmt, GF_PLF_1) == ! matches[j]);
1267 /* If we have all children of child built up from scalars then
1268 just throw that away and build it up this node from scalars. */
1269 if (!SLP_TREE_CHILDREN (child).is_empty ()
1270 /* ??? Rejecting patterns this way doesn't work. We'd have
1271 to do extra work to cancel the pattern so the uses see the
1272 scalar version. */
1273 && !is_pattern_stmt_p
1274 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0])))
1276 unsigned int j;
1277 slp_tree grandchild;
1279 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1280 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
1281 break;
1282 if (!grandchild)
1284 /* Roll back. */
1285 this_loads.truncate (old_nloads);
1286 this_tree_size = old_tree_size;
1287 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1288 vect_free_slp_tree (grandchild);
1289 SLP_TREE_CHILDREN (child).truncate (0);
1291 dump_printf_loc (MSG_NOTE, vect_location,
1292 "Building parent vector operands from "
1293 "scalars instead\n");
1294 oprnd_info->def_stmts = vNULL;
1295 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1296 children.safe_push (child);
1297 continue;
1301 oprnd_info->def_stmts = vNULL;
1302 children.safe_push (child);
1303 continue;
1306 ++*npermutes;
1309 fail:
1310 gcc_assert (child == NULL);
1311 FOR_EACH_VEC_ELT (children, j, child)
1312 vect_free_slp_tree (child);
1313 vect_free_oprnd_info (oprnds_info);
1314 return NULL;
1317 vect_free_oprnd_info (oprnds_info);
1319 if (tree_size)
1320 *tree_size += this_tree_size;
1321 *max_nunits = this_max_nunits;
1322 loads->safe_splice (this_loads);
1324 node = vect_create_new_slp_node (stmts);
1325 SLP_TREE_TWO_OPERATORS (node) = two_operators;
1326 SLP_TREE_CHILDREN (node).splice (children);
1327 return node;
1330 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
1332 static void
1333 vect_print_slp_tree (dump_flags_t dump_kind, location_t loc, slp_tree node)
1335 int i;
1336 gimple *stmt;
1337 slp_tree child;
1339 dump_printf_loc (dump_kind, loc, "node%s\n",
1340 SLP_TREE_DEF_TYPE (node) != vect_internal_def
1341 ? " (external)" : "");
1342 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1344 dump_printf_loc (dump_kind, loc, "\tstmt %d ", i);
1345 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
1347 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1348 vect_print_slp_tree (dump_kind, loc, child);
1352 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1353 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
1354 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1355 stmts in NODE are to be marked. */
1357 static void
1358 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1360 int i;
1361 gimple *stmt;
1362 slp_tree child;
1364 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
1365 return;
1367 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1368 if (j < 0 || i == j)
1369 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1371 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1372 vect_mark_slp_stmts (child, mark, j);
1376 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1378 static void
1379 vect_mark_slp_stmts_relevant (slp_tree node)
1381 int i;
1382 gimple *stmt;
1383 stmt_vec_info stmt_info;
1384 slp_tree child;
1386 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
1387 return;
1389 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1391 stmt_info = vinfo_for_stmt (stmt);
1392 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1393 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1394 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1397 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1398 vect_mark_slp_stmts_relevant (child);
1402 /* Rearrange the statements of NODE according to PERMUTATION. */
1404 static void
1405 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1406 vec<unsigned> permutation)
1408 gimple *stmt;
1409 vec<gimple *> tmp_stmts;
1410 unsigned int i;
1411 slp_tree child;
1413 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1414 vect_slp_rearrange_stmts (child, group_size, permutation);
1416 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1417 tmp_stmts.create (group_size);
1418 tmp_stmts.quick_grow_cleared (group_size);
1420 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1421 tmp_stmts[permutation[i]] = stmt;
1423 SLP_TREE_SCALAR_STMTS (node).release ();
1424 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1428 /* Attempt to reorder stmts in a reduction chain so that we don't
1429 require any load permutation. Return true if that was possible,
1430 otherwise return false. */
1432 static bool
1433 vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1435 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1436 unsigned int i, j;
1437 unsigned int lidx;
1438 slp_tree node, load;
1440 /* Compare all the permutation sequences to the first one. We know
1441 that at least one load is permuted. */
1442 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1443 if (!node->load_permutation.exists ())
1444 return false;
1445 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1447 if (!load->load_permutation.exists ())
1448 return false;
1449 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1450 if (lidx != node->load_permutation[j])
1451 return false;
1454 /* Check that the loads in the first sequence are different and there
1455 are no gaps between them. */
1456 auto_sbitmap load_index (group_size);
1457 bitmap_clear (load_index);
1458 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1460 if (lidx >= group_size)
1461 return false;
1462 if (bitmap_bit_p (load_index, lidx))
1463 return false;
1465 bitmap_set_bit (load_index, lidx);
1467 for (i = 0; i < group_size; i++)
1468 if (!bitmap_bit_p (load_index, i))
1469 return false;
1471 /* This permutation is valid for reduction. Since the order of the
1472 statements in the nodes is not important unless they are memory
1473 accesses, we can rearrange the statements in all the nodes
1474 according to the order of the loads. */
1475 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1476 node->load_permutation);
1478 /* We are done, no actual permutations need to be generated. */
1479 unsigned int unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_instn);
1480 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1482 gimple *first_stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1483 first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt));
1484 /* But we have to keep those permutations that are required because
1485 of handling of gaps. */
1486 if (unrolling_factor == 1
1487 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1488 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0))
1489 SLP_TREE_LOAD_PERMUTATION (node).release ();
1490 else
1491 for (j = 0; j < SLP_TREE_LOAD_PERMUTATION (node).length (); ++j)
1492 SLP_TREE_LOAD_PERMUTATION (node)[j] = j;
1495 return true;
1498 /* Check if the required load permutations in the SLP instance
1499 SLP_INSTN are supported. */
1501 static bool
1502 vect_supported_load_permutation_p (slp_instance slp_instn)
1504 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1505 unsigned int i, j, k, next;
1506 slp_tree node;
1507 gimple *stmt, *load, *next_load;
1509 if (dump_enabled_p ())
1511 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1512 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1513 if (node->load_permutation.exists ())
1514 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1515 dump_printf (MSG_NOTE, "%d ", next);
1516 else
1517 for (k = 0; k < group_size; ++k)
1518 dump_printf (MSG_NOTE, "%d ", k);
1519 dump_printf (MSG_NOTE, "\n");
1522 /* In case of reduction every load permutation is allowed, since the order
1523 of the reduction statements is not important (as opposed to the case of
1524 grouped stores). The only condition we need to check is that all the
1525 load nodes are of the same size and have the same permutation (and then
1526 rearrange all the nodes of the SLP instance according to this
1527 permutation). */
1529 /* Check that all the load nodes are of the same size. */
1530 /* ??? Can't we assert this? */
1531 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1532 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1533 return false;
1535 node = SLP_INSTANCE_TREE (slp_instn);
1536 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1538 /* Reduction (there are no data-refs in the root).
1539 In reduction chain the order of the loads is not important. */
1540 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1541 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1542 vect_attempt_slp_rearrange_stmts (slp_instn);
1544 /* In basic block vectorization we allow any subchain of an interleaving
1545 chain.
1546 FORNOW: not supported in loop SLP because of realignment compications. */
1547 if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
1549 /* Check whether the loads in an instance form a subchain and thus
1550 no permutation is necessary. */
1551 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1553 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1554 continue;
1555 bool subchain_p = true;
1556 next_load = NULL;
1557 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1559 if (j != 0
1560 && (next_load != load
1561 || GROUP_GAP (vinfo_for_stmt (load)) != 1))
1563 subchain_p = false;
1564 break;
1566 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1568 if (subchain_p)
1569 SLP_TREE_LOAD_PERMUTATION (node).release ();
1570 else
1572 stmt_vec_info group_info
1573 = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1574 group_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (group_info));
1575 unsigned nunits
1576 = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (group_info));
1577 unsigned k, maxk = 0;
1578 FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), j, k)
1579 if (k > maxk)
1580 maxk = k;
1581 /* In BB vectorization we may not actually use a loaded vector
1582 accessing elements in excess of GROUP_SIZE. */
1583 if (maxk >= (GROUP_SIZE (group_info) & ~(nunits - 1)))
1585 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1586 "BB vectorization with gaps at the end of "
1587 "a load is not supported\n");
1588 return false;
1591 /* Verify the permutation can be generated. */
1592 vec<tree> tem;
1593 unsigned n_perms;
1594 if (!vect_transform_slp_perm_load (node, tem, NULL,
1595 1, slp_instn, true, &n_perms))
1597 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1598 vect_location,
1599 "unsupported load permutation\n");
1600 return false;
1604 return true;
1607 /* For loop vectorization verify we can generate the permutation. */
1608 unsigned n_perms;
1609 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1610 if (node->load_permutation.exists ()
1611 && !vect_transform_slp_perm_load
1612 (node, vNULL, NULL,
1613 SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true,
1614 &n_perms))
1615 return false;
1617 return true;
1621 /* Find the last store in SLP INSTANCE. */
1623 gimple *
1624 vect_find_last_scalar_stmt_in_slp (slp_tree node)
1626 gimple *last = NULL, *stmt;
1628 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++)
1630 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1631 if (is_pattern_stmt_p (stmt_vinfo))
1632 last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last);
1633 else
1634 last = get_later_stmt (stmt, last);
1637 return last;
1640 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1642 static void
1643 vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node,
1644 stmt_vector_for_cost *prologue_cost_vec,
1645 stmt_vector_for_cost *body_cost_vec,
1646 unsigned ncopies_for_cost)
1648 unsigned i, j;
1649 slp_tree child;
1650 gimple *stmt;
1651 stmt_vec_info stmt_info;
1652 tree lhs;
1654 /* Recurse down the SLP tree. */
1655 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1656 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
1657 vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec,
1658 body_cost_vec, ncopies_for_cost);
1660 /* Look at the first scalar stmt to determine the cost. */
1661 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1662 stmt_info = vinfo_for_stmt (stmt);
1663 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1665 vect_memory_access_type memory_access_type
1666 = (STMT_VINFO_STRIDED_P (stmt_info)
1667 ? VMAT_STRIDED_SLP
1668 : VMAT_CONTIGUOUS);
1669 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1670 vect_model_store_cost (stmt_info, ncopies_for_cost,
1671 memory_access_type, vect_uninitialized_def,
1672 node, prologue_cost_vec, body_cost_vec);
1673 else
1675 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1676 if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
1678 /* If the load is permuted then the alignment is determined by
1679 the first group element not by the first scalar stmt DR. */
1680 stmt = GROUP_FIRST_ELEMENT (stmt_info);
1681 stmt_info = vinfo_for_stmt (stmt);
1682 /* Record the cost for the permutation. */
1683 unsigned n_perms;
1684 vect_transform_slp_perm_load (node, vNULL, NULL,
1685 ncopies_for_cost, instance, true,
1686 &n_perms);
1687 record_stmt_cost (body_cost_vec, n_perms, vec_perm,
1688 stmt_info, 0, vect_body);
1689 unsigned nunits
1690 = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1691 /* And adjust the number of loads performed. This handles
1692 redundancies as well as loads that are later dead. */
1693 auto_sbitmap perm (GROUP_SIZE (stmt_info));
1694 bitmap_clear (perm);
1695 for (i = 0; i < SLP_TREE_LOAD_PERMUTATION (node).length (); ++i)
1696 bitmap_set_bit (perm, SLP_TREE_LOAD_PERMUTATION (node)[i]);
1697 ncopies_for_cost = 0;
1698 bool load_seen = false;
1699 for (i = 0; i < GROUP_SIZE (stmt_info); ++i)
1701 if (i % nunits == 0)
1703 if (load_seen)
1704 ncopies_for_cost++;
1705 load_seen = false;
1707 if (bitmap_bit_p (perm, i))
1708 load_seen = true;
1710 if (load_seen)
1711 ncopies_for_cost++;
1712 gcc_assert (ncopies_for_cost
1713 <= (GROUP_SIZE (stmt_info) - GROUP_GAP (stmt_info)
1714 + nunits - 1) / nunits);
1715 ncopies_for_cost *= SLP_INSTANCE_UNROLLING_FACTOR (instance);
1717 /* Record the cost for the vector loads. */
1718 vect_model_load_cost (stmt_info, ncopies_for_cost,
1719 memory_access_type, node, prologue_cost_vec,
1720 body_cost_vec);
1721 return;
1724 else if (STMT_VINFO_TYPE (stmt_info) == induc_vec_info_type)
1726 /* ncopies_for_cost is the number of IVs we generate. */
1727 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1728 stmt_info, 0, vect_body);
1730 /* Prologue cost for the initial values and step vector. */
1731 record_stmt_cost (prologue_cost_vec, ncopies_for_cost,
1732 CONSTANT_CLASS_P
1733 (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED
1734 (stmt_info))
1735 ? vector_load : vec_construct,
1736 stmt_info, 0, vect_prologue);
1737 record_stmt_cost (prologue_cost_vec, 1,
1738 CONSTANT_CLASS_P
1739 (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info))
1740 ? vector_load : vec_construct,
1741 stmt_info, 0, vect_prologue);
1743 /* ??? No easy way to get at the actual number of vector stmts
1744 to be geneated and thus the derived IVs. */
1746 else
1748 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1749 stmt_info, 0, vect_body);
1750 if (SLP_TREE_TWO_OPERATORS (node))
1752 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1753 stmt_info, 0, vect_body);
1754 record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm,
1755 stmt_info, 0, vect_body);
1759 /* Push SLP node def-type to stmts. */
1760 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1761 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
1762 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
1763 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child);
1765 /* Scan operands and account for prologue cost of constants/externals.
1766 ??? This over-estimates cost for multiple uses and should be
1767 re-engineered. */
1768 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1769 lhs = gimple_get_lhs (stmt);
1770 for (i = 0; i < gimple_num_ops (stmt); ++i)
1772 tree op = gimple_op (stmt, i);
1773 gimple *def_stmt;
1774 enum vect_def_type dt;
1775 if (!op || op == lhs)
1776 continue;
1777 if (vect_is_simple_use (op, stmt_info->vinfo, &def_stmt, &dt))
1779 /* Without looking at the actual initializer a vector of
1780 constants can be implemented as load from the constant pool.
1781 ??? We need to pass down stmt_info for a vector type
1782 even if it points to the wrong stmt. */
1783 if (dt == vect_constant_def)
1784 record_stmt_cost (prologue_cost_vec, 1, vector_load,
1785 stmt_info, 0, vect_prologue);
1786 else if (dt == vect_external_def)
1787 record_stmt_cost (prologue_cost_vec, 1, vec_construct,
1788 stmt_info, 0, vect_prologue);
1792 /* Restore stmt def-types. */
1793 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1794 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
1795 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
1796 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def;
1799 /* Compute the cost for the SLP instance INSTANCE. */
1801 static void
1802 vect_analyze_slp_cost (slp_instance instance, void *data)
1804 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1805 unsigned ncopies_for_cost;
1806 stmt_info_for_cost *si;
1807 unsigned i;
1809 if (dump_enabled_p ())
1810 dump_printf_loc (MSG_NOTE, vect_location,
1811 "=== vect_analyze_slp_cost ===\n");
1813 /* Calculate the number of vector stmts to create based on the unrolling
1814 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1815 GROUP_SIZE / NUNITS otherwise. */
1816 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1817 slp_tree node = SLP_INSTANCE_TREE (instance);
1818 stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1819 /* Adjust the group_size by the vectorization factor which is always one
1820 for basic-block vectorization. */
1821 if (STMT_VINFO_LOOP_VINFO (stmt_info))
1822 group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info));
1823 unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1824 /* For reductions look at a reduction operand in case the reduction
1825 operation is widening like DOT_PROD or SAD. */
1826 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1828 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1829 switch (gimple_assign_rhs_code (stmt))
1831 case DOT_PROD_EXPR:
1832 case SAD_EXPR:
1833 nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type
1834 (TREE_TYPE (gimple_assign_rhs1 (stmt))));
1835 break;
1836 default:;
1839 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1841 prologue_cost_vec.create (10);
1842 body_cost_vec.create (10);
1843 vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance),
1844 &prologue_cost_vec, &body_cost_vec,
1845 ncopies_for_cost);
1847 /* Record the prologue costs, which were delayed until we were
1848 sure that SLP was successful. */
1849 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1851 struct _stmt_vec_info *stmt_info
1852 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1853 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1854 si->misalign, vect_prologue);
1857 /* Record the instance's instructions in the target cost model. */
1858 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1860 struct _stmt_vec_info *stmt_info
1861 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1862 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1863 si->misalign, vect_body);
1866 prologue_cost_vec.release ();
1867 body_cost_vec.release ();
1870 /* Splits a group of stores, currently beginning at FIRST_STMT, into two groups:
1871 one (still beginning at FIRST_STMT) of size GROUP1_SIZE (also containing
1872 the first GROUP1_SIZE stmts, since stores are consecutive), the second
1873 containing the remainder.
1874 Return the first stmt in the second group. */
1876 static gimple *
1877 vect_split_slp_store_group (gimple *first_stmt, unsigned group1_size)
1879 stmt_vec_info first_vinfo = vinfo_for_stmt (first_stmt);
1880 gcc_assert (GROUP_FIRST_ELEMENT (first_vinfo) == first_stmt);
1881 gcc_assert (group1_size > 0);
1882 int group2_size = GROUP_SIZE (first_vinfo) - group1_size;
1883 gcc_assert (group2_size > 0);
1884 GROUP_SIZE (first_vinfo) = group1_size;
1886 gimple *stmt = first_stmt;
1887 for (unsigned i = group1_size; i > 1; i--)
1889 stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1890 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1892 /* STMT is now the last element of the first group. */
1893 gimple *group2 = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1894 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)) = 0;
1896 GROUP_SIZE (vinfo_for_stmt (group2)) = group2_size;
1897 for (stmt = group2; stmt; stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)))
1899 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = group2;
1900 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1903 /* For the second group, the GROUP_GAP is that before the original group,
1904 plus skipping over the first vector. */
1905 GROUP_GAP (vinfo_for_stmt (group2)) =
1906 GROUP_GAP (first_vinfo) + group1_size;
1908 /* GROUP_GAP of the first group now has to skip over the second group too. */
1909 GROUP_GAP (first_vinfo) += group2_size;
1911 if (dump_enabled_p ())
1912 dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
1913 group1_size, group2_size);
1915 return group2;
1918 /* Analyze an SLP instance starting from a group of grouped stores. Call
1919 vect_build_slp_tree to build a tree of packed stmts if possible.
1920 Return FALSE if it's impossible to SLP any stmt in the loop. */
1922 static bool
1923 vect_analyze_slp_instance (vec_info *vinfo,
1924 gimple *stmt, unsigned max_tree_size)
1926 slp_instance new_instance;
1927 slp_tree node;
1928 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1929 unsigned int unrolling_factor = 1, nunits;
1930 tree vectype, scalar_type = NULL_TREE;
1931 gimple *next;
1932 unsigned int i;
1933 unsigned int max_nunits = 0;
1934 vec<slp_tree> loads;
1935 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1936 vec<gimple *> scalar_stmts;
1938 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1940 if (dr)
1942 scalar_type = TREE_TYPE (DR_REF (dr));
1943 vectype = get_vectype_for_scalar_type (scalar_type);
1945 else
1947 gcc_assert (is_a <loop_vec_info> (vinfo));
1948 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1951 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1953 else
1955 gcc_assert (is_a <loop_vec_info> (vinfo));
1956 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1957 group_size = as_a <loop_vec_info> (vinfo)->reductions.length ();
1960 if (!vectype)
1962 if (dump_enabled_p ())
1964 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1965 "Build SLP failed: unsupported data-type ");
1966 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1967 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1970 return false;
1972 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1974 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1975 scalar_stmts.create (group_size);
1976 next = stmt;
1977 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1979 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1980 while (next)
1982 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1983 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1984 scalar_stmts.safe_push (
1985 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1986 else
1987 scalar_stmts.safe_push (next);
1988 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1990 /* Mark the first element of the reduction chain as reduction to properly
1991 transform the node. In the reduction analysis phase only the last
1992 element of the chain is marked as reduction. */
1993 if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1994 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def;
1996 else
1998 /* Collect reduction statements. */
1999 vec<gimple *> reductions = as_a <loop_vec_info> (vinfo)->reductions;
2000 for (i = 0; reductions.iterate (i, &next); i++)
2001 scalar_stmts.safe_push (next);
2004 loads.create (group_size);
2006 /* Build the tree for the SLP instance. */
2007 bool *matches = XALLOCAVEC (bool, group_size);
2008 unsigned npermutes = 0;
2009 bst_fail = new hash_set <vec <gimple *>, bst_traits> ();
2010 node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
2011 &max_nunits, &loads, matches, &npermutes,
2012 NULL, max_tree_size);
2013 delete bst_fail;
2014 if (node != NULL)
2016 /* Calculate the unrolling factor based on the smallest type. */
2017 unrolling_factor
2018 = least_common_multiple (max_nunits, group_size) / group_size;
2020 if (unrolling_factor != 1
2021 && is_a <bb_vec_info> (vinfo))
2024 if (max_nunits > group_size)
2026 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2027 "Build SLP failed: store group "
2028 "size not a multiple of the vector size "
2029 "in basic block SLP\n");
2030 vect_free_slp_tree (node);
2031 loads.release ();
2032 return false;
2034 /* Fatal mismatch. */
2035 matches[group_size/max_nunits * max_nunits] = false;
2036 vect_free_slp_tree (node);
2037 loads.release ();
2039 else
2041 /* Create a new SLP instance. */
2042 new_instance = XNEW (struct _slp_instance);
2043 SLP_INSTANCE_TREE (new_instance) = node;
2044 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
2045 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
2046 SLP_INSTANCE_LOADS (new_instance) = loads;
2048 /* Compute the load permutation. */
2049 slp_tree load_node;
2050 bool loads_permuted = false;
2051 FOR_EACH_VEC_ELT (loads, i, load_node)
2053 vec<unsigned> load_permutation;
2054 int j;
2055 gimple *load, *first_stmt;
2056 bool this_load_permuted = false;
2057 load_permutation.create (group_size);
2058 first_stmt = GROUP_FIRST_ELEMENT
2059 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
2060 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
2062 int load_place = vect_get_place_in_interleaving_chain
2063 (load, first_stmt);
2064 gcc_assert (load_place != -1);
2065 if (load_place != j)
2066 this_load_permuted = true;
2067 load_permutation.safe_push (load_place);
2069 if (!this_load_permuted
2070 /* The load requires permutation when unrolling exposes
2071 a gap either because the group is larger than the SLP
2072 group-size or because there is a gap between the groups. */
2073 && (unrolling_factor == 1
2074 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
2075 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)))
2077 load_permutation.release ();
2078 continue;
2080 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
2081 loads_permuted = true;
2084 if (loads_permuted)
2086 if (!vect_supported_load_permutation_p (new_instance))
2088 if (dump_enabled_p ())
2090 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2091 "Build SLP failed: unsupported load "
2092 "permutation ");
2093 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION,
2094 TDF_SLIM, stmt, 0);
2096 vect_free_slp_instance (new_instance);
2097 return false;
2101 /* If the loads and stores can be handled with load/store-lan
2102 instructions do not generate this SLP instance. */
2103 if (is_a <loop_vec_info> (vinfo)
2104 && loads_permuted
2105 && dr && vect_store_lanes_supported (vectype, group_size))
2107 slp_tree load_node;
2108 FOR_EACH_VEC_ELT (loads, i, load_node)
2110 gimple *first_stmt = GROUP_FIRST_ELEMENT
2111 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
2112 stmt_vec_info stmt_vinfo = vinfo_for_stmt (first_stmt);
2113 /* Use SLP for strided accesses (or if we
2114 can't load-lanes). */
2115 if (STMT_VINFO_STRIDED_P (stmt_vinfo)
2116 || ! vect_load_lanes_supported
2117 (STMT_VINFO_VECTYPE (stmt_vinfo),
2118 GROUP_SIZE (stmt_vinfo)))
2119 break;
2121 if (i == loads.length ())
2123 if (dump_enabled_p ())
2124 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2125 "Built SLP cancelled: can use "
2126 "load/store-lanes\n");
2127 vect_free_slp_instance (new_instance);
2128 return false;
2132 vinfo->slp_instances.safe_push (new_instance);
2134 if (dump_enabled_p ())
2136 dump_printf_loc (MSG_NOTE, vect_location,
2137 "Final SLP tree for instance:\n");
2138 vect_print_slp_tree (MSG_NOTE, vect_location, node);
2141 return true;
2144 else
2146 /* Failed to SLP. */
2147 /* Free the allocated memory. */
2148 scalar_stmts.release ();
2149 loads.release ();
2152 /* For basic block SLP, try to break the group up into multiples of the
2153 vector size. */
2154 if (is_a <bb_vec_info> (vinfo)
2155 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2156 && STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
2158 /* We consider breaking the group only on VF boundaries from the existing
2159 start. */
2160 for (i = 0; i < group_size; i++)
2161 if (!matches[i]) break;
2163 if (i >= nunits && i < group_size)
2165 /* Split into two groups at the first vector boundary before i. */
2166 gcc_assert ((nunits & (nunits - 1)) == 0);
2167 unsigned group1_size = i & ~(nunits - 1);
2169 gimple *rest = vect_split_slp_store_group (stmt, group1_size);
2170 bool res = vect_analyze_slp_instance (vinfo, stmt, max_tree_size);
2171 /* If the first non-match was in the middle of a vector,
2172 skip the rest of that vector. */
2173 if (group1_size < i)
2175 i = group1_size + nunits;
2176 if (i < group_size)
2177 rest = vect_split_slp_store_group (rest, nunits);
2179 if (i < group_size)
2180 res |= vect_analyze_slp_instance (vinfo, rest, max_tree_size);
2181 return res;
2183 /* Even though the first vector did not all match, we might be able to SLP
2184 (some) of the remainder. FORNOW ignore this possibility. */
2187 return false;
2191 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
2192 trees of packed scalar stmts if SLP is possible. */
2194 bool
2195 vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size)
2197 unsigned int i;
2198 gimple *first_element;
2200 if (dump_enabled_p ())
2201 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
2203 /* Find SLP sequences starting from groups of grouped stores. */
2204 FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
2205 vect_analyze_slp_instance (vinfo, first_element, max_tree_size);
2207 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
2209 if (loop_vinfo->reduction_chains.length () > 0)
2211 /* Find SLP sequences starting from reduction chains. */
2212 FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
2213 if (! vect_analyze_slp_instance (vinfo, first_element,
2214 max_tree_size))
2216 /* Dissolve reduction chain group. */
2217 gimple *next, *stmt = first_element;
2218 while (stmt)
2220 stmt_vec_info vinfo = vinfo_for_stmt (stmt);
2221 next = GROUP_NEXT_ELEMENT (vinfo);
2222 GROUP_FIRST_ELEMENT (vinfo) = NULL;
2223 GROUP_NEXT_ELEMENT (vinfo) = NULL;
2224 stmt = next;
2226 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (first_element))
2227 = vect_internal_def;
2231 /* Find SLP sequences starting from groups of reductions. */
2232 if (loop_vinfo->reductions.length () > 1)
2233 vect_analyze_slp_instance (vinfo, loop_vinfo->reductions[0],
2234 max_tree_size);
2237 return true;
2241 /* For each possible SLP instance decide whether to SLP it and calculate overall
2242 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
2243 least one instance. */
2245 bool
2246 vect_make_slp_decision (loop_vec_info loop_vinfo)
2248 unsigned int i, unrolling_factor = 1;
2249 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2250 slp_instance instance;
2251 int decided_to_slp = 0;
2253 if (dump_enabled_p ())
2254 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
2255 "\n");
2257 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2259 /* FORNOW: SLP if you can. */
2260 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
2261 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
2263 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
2264 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
2265 loop-based vectorization. Such stmts will be marked as HYBRID. */
2266 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2267 decided_to_slp++;
2270 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
2272 if (decided_to_slp && dump_enabled_p ())
2273 dump_printf_loc (MSG_NOTE, vect_location,
2274 "Decided to SLP %d instances. Unrolling factor %d\n",
2275 decided_to_slp, unrolling_factor);
2277 return (decided_to_slp > 0);
2281 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
2282 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
2284 static void
2285 vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
2287 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[i];
2288 imm_use_iterator imm_iter;
2289 gimple *use_stmt;
2290 stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
2291 slp_tree child;
2292 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
2293 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2294 int j;
2296 /* Propagate hybrid down the SLP tree. */
2297 if (stype == hybrid)
2299 else if (HYBRID_SLP_STMT (stmt_vinfo))
2300 stype = hybrid;
2301 else
2303 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
2304 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
2305 /* If we get a pattern stmt here we have to use the LHS of the
2306 original stmt for immediate uses. */
2307 if (! STMT_VINFO_IN_PATTERN_P (stmt_vinfo)
2308 && STMT_VINFO_RELATED_STMT (stmt_vinfo))
2309 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
2310 tree def;
2311 if (gimple_code (stmt) == GIMPLE_PHI)
2312 def = gimple_phi_result (stmt);
2313 else
2314 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
2315 if (def)
2316 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2318 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2319 continue;
2320 use_vinfo = vinfo_for_stmt (use_stmt);
2321 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
2322 && STMT_VINFO_RELATED_STMT (use_vinfo))
2323 use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
2324 if (!STMT_SLP_TYPE (use_vinfo)
2325 && (STMT_VINFO_RELEVANT (use_vinfo)
2326 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2327 && !(gimple_code (use_stmt) == GIMPLE_PHI
2328 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2330 if (dump_enabled_p ())
2332 dump_printf_loc (MSG_NOTE, vect_location, "use of SLP "
2333 "def in non-SLP stmt: ");
2334 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0);
2336 stype = hybrid;
2341 if (stype == hybrid
2342 && !HYBRID_SLP_STMT (stmt_vinfo))
2344 if (dump_enabled_p ())
2346 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2347 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2349 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2352 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2353 if (SLP_TREE_DEF_TYPE (child) != vect_external_def)
2354 vect_detect_hybrid_slp_stmts (child, i, stype);
2357 /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
2359 static tree
2360 vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2362 walk_stmt_info *wi = (walk_stmt_info *)data;
2363 struct loop *loopp = (struct loop *)wi->info;
2365 if (wi->is_lhs)
2366 return NULL_TREE;
2368 if (TREE_CODE (*tp) == SSA_NAME
2369 && !SSA_NAME_IS_DEFAULT_DEF (*tp))
2371 gimple *def_stmt = SSA_NAME_DEF_STMT (*tp);
2372 if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
2373 && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
2375 if (dump_enabled_p ())
2377 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2378 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2380 STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
2384 return NULL_TREE;
2387 static tree
2388 vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
2389 walk_stmt_info *)
2391 stmt_vec_info use_vinfo = vinfo_for_stmt (gsi_stmt (*gsi));
2392 /* If the stmt is in a SLP instance then this isn't a reason
2393 to mark use definitions in other SLP instances as hybrid. */
2394 if (! STMT_SLP_TYPE (use_vinfo)
2395 && (STMT_VINFO_RELEVANT (use_vinfo)
2396 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2397 && ! (gimple_code (gsi_stmt (*gsi)) == GIMPLE_PHI
2398 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2400 else
2401 *handled = true;
2402 return NULL_TREE;
2405 /* Find stmts that must be both vectorized and SLPed. */
2407 void
2408 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2410 unsigned int i;
2411 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2412 slp_instance instance;
2414 if (dump_enabled_p ())
2415 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
2416 "\n");
2418 /* First walk all pattern stmt in the loop and mark defs of uses as
2419 hybrid because immediate uses in them are not recorded. */
2420 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2422 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2423 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2424 gsi_next (&gsi))
2426 gimple *stmt = gsi_stmt (gsi);
2427 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2428 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2430 walk_stmt_info wi;
2431 memset (&wi, 0, sizeof (wi));
2432 wi.info = LOOP_VINFO_LOOP (loop_vinfo);
2433 gimple_stmt_iterator gsi2
2434 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2435 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2436 vect_detect_hybrid_slp_1, &wi);
2437 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2438 vect_detect_hybrid_slp_2,
2439 vect_detect_hybrid_slp_1, &wi);
2444 /* Then walk the SLP instance trees marking stmts with uses in
2445 non-SLP stmts as hybrid, also propagating hybrid down the
2446 SLP tree, collecting the above info on-the-fly. */
2447 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2449 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2450 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2451 i, pure_slp);
2456 /* Initialize a bb_vec_info struct for the statements between
2457 REGION_BEGIN_IN (inclusive) and REGION_END_IN (exclusive). */
2459 _bb_vec_info::_bb_vec_info (gimple_stmt_iterator region_begin_in,
2460 gimple_stmt_iterator region_end_in)
2461 : vec_info (vec_info::bb, init_cost (NULL)),
2462 bb (gsi_bb (region_begin_in)),
2463 region_begin (region_begin_in),
2464 region_end (region_end_in)
2466 gimple_stmt_iterator gsi;
2468 for (gsi = region_begin; gsi_stmt (gsi) != gsi_stmt (region_end);
2469 gsi_next (&gsi))
2471 gimple *stmt = gsi_stmt (gsi);
2472 gimple_set_uid (stmt, 0);
2473 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, this));
2476 bb->aux = this;
2480 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2481 stmts in the basic block. */
2483 _bb_vec_info::~_bb_vec_info ()
2485 for (gimple_stmt_iterator si = region_begin;
2486 gsi_stmt (si) != gsi_stmt (region_end); gsi_next (&si))
2488 gimple *stmt = gsi_stmt (si);
2489 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2491 if (stmt_info)
2492 /* Free stmt_vec_info. */
2493 free_stmt_vec_info (stmt);
2495 /* Reset region marker. */
2496 gimple_set_uid (stmt, -1);
2499 bb->aux = NULL;
2503 /* Analyze statements contained in SLP tree node after recursively analyzing
2504 the subtree. Return TRUE if the operations are supported. */
2506 static bool
2507 vect_slp_analyze_node_operations (slp_tree node, slp_instance node_instance)
2509 bool dummy;
2510 int i, j;
2511 gimple *stmt;
2512 slp_tree child;
2514 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
2515 return true;
2517 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2518 if (!vect_slp_analyze_node_operations (child, node_instance))
2519 return false;
2521 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
2522 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2523 gcc_assert (stmt_info);
2524 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2526 /* For BB vectorization vector types are assigned here.
2527 Memory accesses already got their vector type assigned
2528 in vect_analyze_data_refs. */
2529 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2530 if (bb_vinfo
2531 && ! STMT_VINFO_DATA_REF (stmt_info))
2533 gcc_assert (PURE_SLP_STMT (stmt_info));
2535 tree scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
2536 if (dump_enabled_p ())
2538 dump_printf_loc (MSG_NOTE, vect_location,
2539 "get vectype for scalar type: ");
2540 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
2541 dump_printf (MSG_NOTE, "\n");
2544 tree vectype = get_vectype_for_scalar_type (scalar_type);
2545 if (!vectype)
2547 if (dump_enabled_p ())
2549 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2550 "not SLPed: unsupported data-type ");
2551 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2552 scalar_type);
2553 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2555 return false;
2558 if (dump_enabled_p ())
2560 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
2561 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
2562 dump_printf (MSG_NOTE, "\n");
2565 gimple *sstmt;
2566 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, sstmt)
2567 STMT_VINFO_VECTYPE (vinfo_for_stmt (sstmt)) = vectype;
2570 /* Push SLP node def-type to stmt operands. */
2571 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2572 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
2573 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0]))
2574 = SLP_TREE_DEF_TYPE (child);
2575 bool res = vect_analyze_stmt (stmt, &dummy, node, node_instance);
2576 /* Restore def-types. */
2577 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2578 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
2579 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0]))
2580 = vect_internal_def;
2581 if (! res)
2582 return false;
2584 return true;
2588 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2589 operations are supported. */
2591 bool
2592 vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data)
2594 slp_instance instance;
2595 int i;
2597 if (dump_enabled_p ())
2598 dump_printf_loc (MSG_NOTE, vect_location,
2599 "=== vect_slp_analyze_operations ===\n");
2601 for (i = 0; slp_instances.iterate (i, &instance); )
2603 if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance),
2604 instance))
2606 dump_printf_loc (MSG_NOTE, vect_location,
2607 "removing SLP instance operations starting from: ");
2608 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2609 SLP_TREE_SCALAR_STMTS
2610 (SLP_INSTANCE_TREE (instance))[0], 0);
2611 vect_free_slp_instance (instance);
2612 slp_instances.ordered_remove (i);
2614 else
2616 /* Compute the costs of the SLP instance. */
2617 vect_analyze_slp_cost (instance, data);
2618 i++;
2622 if (!slp_instances.length ())
2623 return false;
2625 return true;
2629 /* Compute the scalar cost of the SLP node NODE and its children
2630 and return it. Do not account defs that are marked in LIFE and
2631 update LIFE according to uses of NODE. */
2633 static unsigned
2634 vect_bb_slp_scalar_cost (basic_block bb,
2635 slp_tree node, vec<bool, va_heap> *life)
2637 unsigned scalar_cost = 0;
2638 unsigned i;
2639 gimple *stmt;
2640 slp_tree child;
2642 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2644 unsigned stmt_cost;
2645 ssa_op_iter op_iter;
2646 def_operand_p def_p;
2647 stmt_vec_info stmt_info;
2649 if ((*life)[i])
2650 continue;
2652 /* If there is a non-vectorized use of the defs then the scalar
2653 stmt is kept live in which case we do not account it or any
2654 required defs in the SLP children in the scalar cost. This
2655 way we make the vectorization more costly when compared to
2656 the scalar cost. */
2657 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2659 imm_use_iterator use_iter;
2660 gimple *use_stmt;
2661 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
2662 if (!is_gimple_debug (use_stmt)
2663 && (! vect_stmt_in_region_p (vinfo_for_stmt (stmt)->vinfo,
2664 use_stmt)
2665 || ! PURE_SLP_STMT (vinfo_for_stmt (use_stmt))))
2667 (*life)[i] = true;
2668 BREAK_FROM_IMM_USE_STMT (use_iter);
2671 if ((*life)[i])
2672 continue;
2674 /* Count scalar stmts only once. */
2675 if (gimple_visited_p (stmt))
2676 continue;
2677 gimple_set_visited (stmt, true);
2679 stmt_info = vinfo_for_stmt (stmt);
2680 if (STMT_VINFO_DATA_REF (stmt_info))
2682 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2683 stmt_cost = vect_get_stmt_cost (scalar_load);
2684 else
2685 stmt_cost = vect_get_stmt_cost (scalar_store);
2687 else
2688 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2690 scalar_cost += stmt_cost;
2693 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2694 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
2695 scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
2697 return scalar_cost;
2700 /* Check if vectorization of the basic block is profitable. */
2702 static bool
2703 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2705 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2706 slp_instance instance;
2707 int i;
2708 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2709 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2711 /* Calculate scalar cost. */
2712 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2714 auto_vec<bool, 20> life;
2715 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
2716 scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2717 SLP_INSTANCE_TREE (instance),
2718 &life);
2721 /* Unset visited flag. */
2722 for (gimple_stmt_iterator gsi = bb_vinfo->region_begin;
2723 gsi_stmt (gsi) != gsi_stmt (bb_vinfo->region_end); gsi_next (&gsi))
2724 gimple_set_visited (gsi_stmt (gsi), false);
2726 /* Complete the target-specific cost calculation. */
2727 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2728 &vec_inside_cost, &vec_epilogue_cost);
2730 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2732 if (dump_enabled_p ())
2734 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2735 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2736 vec_inside_cost);
2737 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2738 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2739 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
2742 /* Vectorization is profitable if its cost is more than the cost of scalar
2743 version. Note that we err on the vector side for equal cost because
2744 the cost estimate is otherwise quite pessimistic (constant uses are
2745 free on the scalar side but cost a load on the vector side for
2746 example). */
2747 if (vec_outside_cost + vec_inside_cost > scalar_cost)
2748 return false;
2750 return true;
2753 /* Check if the basic block can be vectorized. Returns a bb_vec_info
2754 if so and sets fatal to true if failure is independent of
2755 current_vector_size. */
2757 static bb_vec_info
2758 vect_slp_analyze_bb_1 (gimple_stmt_iterator region_begin,
2759 gimple_stmt_iterator region_end,
2760 vec<data_reference_p> datarefs, int n_stmts,
2761 bool &fatal)
2763 bb_vec_info bb_vinfo;
2764 slp_instance instance;
2765 int i;
2766 int min_vf = 2;
2768 /* The first group of checks is independent of the vector size. */
2769 fatal = true;
2771 if (n_stmts > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2773 if (dump_enabled_p ())
2774 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2775 "not vectorized: too many instructions in "
2776 "basic block.\n");
2777 free_data_refs (datarefs);
2778 return NULL;
2781 bb_vinfo = new _bb_vec_info (region_begin, region_end);
2782 if (!bb_vinfo)
2783 return NULL;
2785 BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
2787 /* Analyze the data references. */
2789 if (!vect_analyze_data_refs (bb_vinfo, &min_vf))
2791 if (dump_enabled_p ())
2792 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2793 "not vectorized: unhandled data-ref in basic "
2794 "block.\n");
2796 delete bb_vinfo;
2797 return NULL;
2800 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2802 if (dump_enabled_p ())
2803 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2804 "not vectorized: not enough data-refs in "
2805 "basic block.\n");
2807 delete bb_vinfo;
2808 return NULL;
2811 if (!vect_analyze_data_ref_accesses (bb_vinfo))
2813 if (dump_enabled_p ())
2814 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2815 "not vectorized: unhandled data access in "
2816 "basic block.\n");
2818 delete bb_vinfo;
2819 return NULL;
2822 /* If there are no grouped stores in the region there is no need
2823 to continue with pattern recog as vect_analyze_slp will fail
2824 anyway. */
2825 if (bb_vinfo->grouped_stores.is_empty ())
2827 if (dump_enabled_p ())
2828 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2829 "not vectorized: no grouped stores in "
2830 "basic block.\n");
2832 delete bb_vinfo;
2833 return NULL;
2836 /* While the rest of the analysis below depends on it in some way. */
2837 fatal = false;
2839 vect_pattern_recog (bb_vinfo);
2841 /* Check the SLP opportunities in the basic block, analyze and build SLP
2842 trees. */
2843 if (!vect_analyze_slp (bb_vinfo, n_stmts))
2845 if (dump_enabled_p ())
2847 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2848 "Failed to SLP the basic block.\n");
2849 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2850 "not vectorized: failed to find SLP opportunities "
2851 "in basic block.\n");
2854 delete bb_vinfo;
2855 return NULL;
2858 vect_record_base_alignments (bb_vinfo);
2860 /* Analyze and verify the alignment of data references and the
2861 dependence in the SLP instances. */
2862 for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
2864 if (! vect_slp_analyze_and_verify_instance_alignment (instance)
2865 || ! vect_slp_analyze_instance_dependence (instance))
2867 dump_printf_loc (MSG_NOTE, vect_location,
2868 "removing SLP instance operations starting from: ");
2869 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2870 SLP_TREE_SCALAR_STMTS
2871 (SLP_INSTANCE_TREE (instance))[0], 0);
2872 vect_free_slp_instance (instance);
2873 BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
2874 continue;
2877 /* Mark all the statements that we want to vectorize as pure SLP and
2878 relevant. */
2879 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2880 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2882 i++;
2884 if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
2886 delete bb_vinfo;
2887 return NULL;
2890 if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo),
2891 BB_VINFO_TARGET_COST_DATA (bb_vinfo)))
2893 if (dump_enabled_p ())
2894 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2895 "not vectorized: bad operation in basic block.\n");
2897 delete bb_vinfo;
2898 return NULL;
2901 /* Cost model: check if the vectorization is worthwhile. */
2902 if (!unlimited_cost_model (NULL)
2903 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2905 if (dump_enabled_p ())
2906 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2907 "not vectorized: vectorization is not "
2908 "profitable.\n");
2910 delete bb_vinfo;
2911 return NULL;
2914 if (dump_enabled_p ())
2915 dump_printf_loc (MSG_NOTE, vect_location,
2916 "Basic block will be vectorized using SLP\n");
2918 return bb_vinfo;
2922 /* Main entry for the BB vectorizer. Analyze and transform BB, returns
2923 true if anything in the basic-block was vectorized. */
2925 bool
2926 vect_slp_bb (basic_block bb)
2928 bb_vec_info bb_vinfo;
2929 gimple_stmt_iterator gsi;
2930 unsigned int vector_sizes;
2931 bool any_vectorized = false;
2933 if (dump_enabled_p ())
2934 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2936 /* Autodetect first vector size we try. */
2937 current_vector_size = 0;
2938 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2940 gsi = gsi_start_bb (bb);
2942 while (1)
2944 if (gsi_end_p (gsi))
2945 break;
2947 gimple_stmt_iterator region_begin = gsi;
2948 vec<data_reference_p> datarefs = vNULL;
2949 int insns = 0;
2951 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2953 gimple *stmt = gsi_stmt (gsi);
2954 if (is_gimple_debug (stmt))
2955 continue;
2956 insns++;
2958 if (gimple_location (stmt) != UNKNOWN_LOCATION)
2959 vect_location = gimple_location (stmt);
2961 if (!find_data_references_in_stmt (NULL, stmt, &datarefs))
2962 break;
2965 /* Skip leading unhandled stmts. */
2966 if (gsi_stmt (region_begin) == gsi_stmt (gsi))
2968 gsi_next (&gsi);
2969 continue;
2972 gimple_stmt_iterator region_end = gsi;
2974 bool vectorized = false;
2975 bool fatal = false;
2976 bb_vinfo = vect_slp_analyze_bb_1 (region_begin, region_end,
2977 datarefs, insns, fatal);
2978 if (bb_vinfo
2979 && dbg_cnt (vect_slp))
2981 if (dump_enabled_p ())
2982 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
2984 vect_schedule_slp (bb_vinfo);
2986 if (dump_enabled_p ())
2987 dump_printf_loc (MSG_NOTE, vect_location,
2988 "basic block part vectorized\n");
2990 vectorized = true;
2992 delete bb_vinfo;
2994 any_vectorized |= vectorized;
2996 vector_sizes &= ~current_vector_size;
2997 if (vectorized
2998 || vector_sizes == 0
2999 || current_vector_size == 0
3000 /* If vect_slp_analyze_bb_1 signaled that analysis for all
3001 vector sizes will fail do not bother iterating. */
3002 || fatal)
3004 if (gsi_end_p (region_end))
3005 break;
3007 /* Skip the unhandled stmt. */
3008 gsi_next (&gsi);
3010 /* And reset vector sizes. */
3011 current_vector_size = 0;
3012 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
3014 else
3016 /* Try the next biggest vector size. */
3017 current_vector_size = 1 << floor_log2 (vector_sizes);
3018 if (dump_enabled_p ())
3019 dump_printf_loc (MSG_NOTE, vect_location,
3020 "***** Re-trying analysis with "
3021 "vector size %d\n", current_vector_size);
3023 /* Start over. */
3024 gsi = region_begin;
3028 return any_vectorized;
3032 /* Return 1 if vector type of boolean constant which is OPNUM
3033 operand in statement STMT is a boolean vector. */
3035 static bool
3036 vect_mask_constant_operand_p (gimple *stmt, int opnum)
3038 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3039 enum tree_code code = gimple_expr_code (stmt);
3040 tree op, vectype;
3041 gimple *def_stmt;
3042 enum vect_def_type dt;
3044 /* For comparison and COND_EXPR type is chosen depending
3045 on the other comparison operand. */
3046 if (TREE_CODE_CLASS (code) == tcc_comparison)
3048 if (opnum)
3049 op = gimple_assign_rhs1 (stmt);
3050 else
3051 op = gimple_assign_rhs2 (stmt);
3053 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
3054 &dt, &vectype))
3055 gcc_unreachable ();
3057 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
3060 if (code == COND_EXPR)
3062 tree cond = gimple_assign_rhs1 (stmt);
3064 if (TREE_CODE (cond) == SSA_NAME)
3065 op = cond;
3066 else if (opnum)
3067 op = TREE_OPERAND (cond, 1);
3068 else
3069 op = TREE_OPERAND (cond, 0);
3071 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
3072 &dt, &vectype))
3073 gcc_unreachable ();
3075 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
3078 return VECTOR_BOOLEAN_TYPE_P (STMT_VINFO_VECTYPE (stmt_vinfo));
3082 /* For constant and loop invariant defs of SLP_NODE this function returns
3083 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
3084 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
3085 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
3086 REDUC_INDEX is the index of the reduction operand in the statements, unless
3087 it is -1. */
3089 static void
3090 vect_get_constant_vectors (tree op, slp_tree slp_node,
3091 vec<tree> *vec_oprnds,
3092 unsigned int op_num, unsigned int number_of_vectors)
3094 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
3095 gimple *stmt = stmts[0];
3096 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3097 unsigned nunits;
3098 tree vec_cst;
3099 tree *elts;
3100 unsigned j, number_of_places_left_in_vector;
3101 tree vector_type;
3102 tree vop;
3103 int group_size = stmts.length ();
3104 unsigned int vec_num, i;
3105 unsigned number_of_copies = 1;
3106 vec<tree> voprnds;
3107 voprnds.create (number_of_vectors);
3108 bool constant_p, is_store;
3109 tree neutral_op = NULL;
3110 enum tree_code code = gimple_expr_code (stmt);
3111 gimple_seq ctor_seq = NULL;
3113 /* Check if vector type is a boolean vector. */
3114 if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
3115 && vect_mask_constant_operand_p (stmt, op_num))
3116 vector_type
3117 = build_same_sized_truth_vector_type (STMT_VINFO_VECTYPE (stmt_vinfo));
3118 else
3119 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
3120 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
3122 if (STMT_VINFO_DATA_REF (stmt_vinfo))
3124 is_store = true;
3125 op = gimple_assign_rhs1 (stmt);
3127 else
3128 is_store = false;
3130 gcc_assert (op);
3132 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
3133 created vectors. It is greater than 1 if unrolling is performed.
3135 For example, we have two scalar operands, s1 and s2 (e.g., group of
3136 strided accesses of size two), while NUNITS is four (i.e., four scalars
3137 of this type can be packed in a vector). The output vector will contain
3138 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
3139 will be 2).
3141 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
3142 containing the operands.
3144 For example, NUNITS is four as before, and the group size is 8
3145 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
3146 {s5, s6, s7, s8}. */
3148 number_of_copies = nunits * number_of_vectors / group_size;
3150 number_of_places_left_in_vector = nunits;
3151 constant_p = true;
3152 elts = XALLOCAVEC (tree, nunits);
3153 bool place_after_defs = false;
3154 for (j = 0; j < number_of_copies; j++)
3156 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
3158 if (is_store)
3159 op = gimple_assign_rhs1 (stmt);
3160 else
3162 switch (code)
3164 case COND_EXPR:
3166 tree cond = gimple_assign_rhs1 (stmt);
3167 if (TREE_CODE (cond) == SSA_NAME)
3168 op = gimple_op (stmt, op_num + 1);
3169 else if (op_num == 0 || op_num == 1)
3170 op = TREE_OPERAND (cond, op_num);
3171 else
3173 if (op_num == 2)
3174 op = gimple_assign_rhs2 (stmt);
3175 else
3176 op = gimple_assign_rhs3 (stmt);
3179 break;
3181 case CALL_EXPR:
3182 op = gimple_call_arg (stmt, op_num);
3183 break;
3185 case LSHIFT_EXPR:
3186 case RSHIFT_EXPR:
3187 case LROTATE_EXPR:
3188 case RROTATE_EXPR:
3189 op = gimple_op (stmt, op_num + 1);
3190 /* Unlike the other binary operators, shifts/rotates have
3191 the shift count being int, instead of the same type as
3192 the lhs, so make sure the scalar is the right type if
3193 we are dealing with vectors of
3194 long long/long/short/char. */
3195 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
3196 op = fold_convert (TREE_TYPE (vector_type), op);
3197 break;
3199 default:
3200 op = gimple_op (stmt, op_num + 1);
3201 break;
3205 /* Create 'vect_ = {op0,op1,...,opn}'. */
3206 number_of_places_left_in_vector--;
3207 tree orig_op = op;
3208 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
3210 if (CONSTANT_CLASS_P (op))
3212 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3214 /* Can't use VIEW_CONVERT_EXPR for booleans because
3215 of possibly different sizes of scalar value and
3216 vector element. */
3217 if (integer_zerop (op))
3218 op = build_int_cst (TREE_TYPE (vector_type), 0);
3219 else if (integer_onep (op))
3220 op = build_all_ones_cst (TREE_TYPE (vector_type));
3221 else
3222 gcc_unreachable ();
3224 else
3225 op = fold_unary (VIEW_CONVERT_EXPR,
3226 TREE_TYPE (vector_type), op);
3227 gcc_assert (op && CONSTANT_CLASS_P (op));
3229 else
3231 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
3232 gimple *init_stmt;
3233 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3235 tree true_val
3236 = build_all_ones_cst (TREE_TYPE (vector_type));
3237 tree false_val
3238 = build_zero_cst (TREE_TYPE (vector_type));
3239 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
3240 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
3241 op, true_val,
3242 false_val);
3244 else
3246 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
3247 op);
3248 init_stmt
3249 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
3250 op);
3252 gimple_seq_add_stmt (&ctor_seq, init_stmt);
3253 op = new_temp;
3256 elts[number_of_places_left_in_vector] = op;
3257 if (!CONSTANT_CLASS_P (op))
3258 constant_p = false;
3259 if (TREE_CODE (orig_op) == SSA_NAME
3260 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
3261 && STMT_VINFO_BB_VINFO (stmt_vinfo)
3262 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
3263 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
3264 place_after_defs = true;
3266 if (number_of_places_left_in_vector == 0)
3268 if (constant_p)
3269 vec_cst = build_vector (vector_type, elts);
3270 else
3272 vec<constructor_elt, va_gc> *v;
3273 unsigned k;
3274 vec_alloc (v, nunits);
3275 for (k = 0; k < nunits; ++k)
3276 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
3277 vec_cst = build_constructor (vector_type, v);
3279 tree init;
3280 gimple_stmt_iterator gsi;
3281 if (place_after_defs)
3283 gsi = gsi_for_stmt
3284 (vect_find_last_scalar_stmt_in_slp (slp_node));
3285 init = vect_init_vector (stmt, vec_cst, vector_type, &gsi);
3287 else
3288 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
3289 if (ctor_seq != NULL)
3291 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
3292 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
3293 GSI_SAME_STMT);
3294 ctor_seq = NULL;
3296 voprnds.quick_push (init);
3297 place_after_defs = false;
3298 number_of_places_left_in_vector = nunits;
3299 constant_p = true;
3304 /* Since the vectors are created in the reverse order, we should invert
3305 them. */
3306 vec_num = voprnds.length ();
3307 for (j = vec_num; j != 0; j--)
3309 vop = voprnds[j - 1];
3310 vec_oprnds->quick_push (vop);
3313 voprnds.release ();
3315 /* In case that VF is greater than the unrolling factor needed for the SLP
3316 group of stmts, NUMBER_OF_VECTORS to be created is greater than
3317 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
3318 to replicate the vectors. */
3319 while (number_of_vectors > vec_oprnds->length ())
3321 tree neutral_vec = NULL;
3323 if (neutral_op)
3325 if (!neutral_vec)
3326 neutral_vec = build_vector_from_val (vector_type, neutral_op);
3328 vec_oprnds->quick_push (neutral_vec);
3330 else
3332 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
3333 vec_oprnds->quick_push (vop);
3339 /* Get vectorized definitions from SLP_NODE that contains corresponding
3340 vectorized def-stmts. */
3342 static void
3343 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
3345 tree vec_oprnd;
3346 gimple *vec_def_stmt;
3347 unsigned int i;
3349 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
3351 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
3353 gcc_assert (vec_def_stmt);
3354 if (gimple_code (vec_def_stmt) == GIMPLE_PHI)
3355 vec_oprnd = gimple_phi_result (vec_def_stmt);
3356 else
3357 vec_oprnd = gimple_get_lhs (vec_def_stmt);
3358 vec_oprnds->quick_push (vec_oprnd);
3363 /* Get vectorized definitions for SLP_NODE.
3364 If the scalar definitions are loop invariants or constants, collect them and
3365 call vect_get_constant_vectors() to create vector stmts.
3366 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
3367 must be stored in the corresponding child of SLP_NODE, and we call
3368 vect_get_slp_vect_defs () to retrieve them. */
3370 void
3371 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
3372 vec<vec<tree> > *vec_oprnds)
3374 gimple *first_stmt;
3375 int number_of_vects = 0, i;
3376 unsigned int child_index = 0;
3377 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
3378 slp_tree child = NULL;
3379 vec<tree> vec_defs;
3380 tree oprnd;
3381 bool vectorized_defs;
3383 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
3384 FOR_EACH_VEC_ELT (ops, i, oprnd)
3386 /* For each operand we check if it has vectorized definitions in a child
3387 node or we need to create them (for invariants and constants). We
3388 check if the LHS of the first stmt of the next child matches OPRND.
3389 If it does, we found the correct child. Otherwise, we call
3390 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
3391 to check this child node for the next operand. */
3392 vectorized_defs = false;
3393 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
3395 child = SLP_TREE_CHILDREN (slp_node)[child_index];
3397 /* We have to check both pattern and original def, if available. */
3398 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3400 gimple *first_def = SLP_TREE_SCALAR_STMTS (child)[0];
3401 gimple *related
3402 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
3403 tree first_def_op;
3405 if (gimple_code (first_def) == GIMPLE_PHI)
3406 first_def_op = gimple_phi_result (first_def);
3407 else
3408 first_def_op = gimple_get_lhs (first_def);
3409 if (operand_equal_p (oprnd, first_def_op, 0)
3410 || (related
3411 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
3413 /* The number of vector defs is determined by the number of
3414 vector statements in the node from which we get those
3415 statements. */
3416 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
3417 vectorized_defs = true;
3418 child_index++;
3421 else
3422 child_index++;
3425 if (!vectorized_defs)
3427 if (i == 0)
3429 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
3430 /* Number of vector stmts was calculated according to LHS in
3431 vect_schedule_slp_instance (), fix it by replacing LHS with
3432 RHS, if necessary. See vect_get_smallest_scalar_type () for
3433 details. */
3434 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
3435 &rhs_size_unit);
3436 if (rhs_size_unit != lhs_size_unit)
3438 number_of_vects *= rhs_size_unit;
3439 number_of_vects /= lhs_size_unit;
3444 /* Allocate memory for vectorized defs. */
3445 vec_defs = vNULL;
3446 vec_defs.create (number_of_vects);
3448 /* For reduction defs we call vect_get_constant_vectors (), since we are
3449 looking for initial loop invariant values. */
3450 if (vectorized_defs)
3451 /* The defs are already vectorized. */
3452 vect_get_slp_vect_defs (child, &vec_defs);
3453 else
3454 /* Build vectors from scalar defs. */
3455 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
3456 number_of_vects);
3458 vec_oprnds->quick_push (vec_defs);
3462 /* Generate vector permute statements from a list of loads in DR_CHAIN.
3463 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
3464 permute statements for the SLP node NODE of the SLP instance
3465 SLP_NODE_INSTANCE. */
3467 bool
3468 vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
3469 gimple_stmt_iterator *gsi, int vf,
3470 slp_instance slp_node_instance, bool analyze_only,
3471 unsigned *n_perms)
3473 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3474 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3475 tree mask_element_type = NULL_TREE, mask_type;
3476 int nunits, vec_index = 0;
3477 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3478 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
3479 int mask_element;
3480 unsigned char *mask;
3481 machine_mode mode;
3483 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3484 return false;
3486 stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info));
3488 mode = TYPE_MODE (vectype);
3490 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3491 same size as the vector element being permuted. */
3492 mask_element_type = lang_hooks.types.type_for_mode
3493 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
3494 mask_type = get_vectype_for_scalar_type (mask_element_type);
3495 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3496 mask = XALLOCAVEC (unsigned char, nunits);
3498 /* Initialize the vect stmts of NODE to properly insert the generated
3499 stmts later. */
3500 if (! analyze_only)
3501 for (unsigned i = SLP_TREE_VEC_STMTS (node).length ();
3502 i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3503 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
3505 /* Generate permutation masks for every NODE. Number of masks for each NODE
3506 is equal to GROUP_SIZE.
3507 E.g., we have a group of three nodes with three loads from the same
3508 location in each node, and the vector size is 4. I.e., we have a
3509 a0b0c0a1b1c1... sequence and we need to create the following vectors:
3510 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3511 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3514 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
3515 The last mask is illegal since we assume two operands for permute
3516 operation, and the mask element values can't be outside that range.
3517 Hence, the last mask must be converted into {2,5,5,5}.
3518 For the first two permutations we need the first and the second input
3519 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
3520 we need the second and the third vectors: {b1,c1,a2,b2} and
3521 {c2,a3,b3,c3}. */
3523 int vect_stmts_counter = 0;
3524 int index = 0;
3525 int first_vec_index = -1;
3526 int second_vec_index = -1;
3527 bool noop_p = true;
3528 *n_perms = 0;
3530 for (int j = 0; j < vf; j++)
3532 for (int k = 0; k < group_size; k++)
3534 int i = (SLP_TREE_LOAD_PERMUTATION (node)[k]
3535 + j * STMT_VINFO_GROUP_SIZE (stmt_info));
3536 vec_index = i / nunits;
3537 mask_element = i % nunits;
3538 if (vec_index == first_vec_index
3539 || first_vec_index == -1)
3541 first_vec_index = vec_index;
3543 else if (vec_index == second_vec_index
3544 || second_vec_index == -1)
3546 second_vec_index = vec_index;
3547 mask_element += nunits;
3549 else
3551 if (dump_enabled_p ())
3553 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3554 "permutation requires at "
3555 "least three vectors ");
3556 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3557 stmt, 0);
3559 return false;
3562 gcc_assert (mask_element >= 0
3563 && mask_element < 2 * nunits);
3564 if (mask_element != index)
3565 noop_p = false;
3566 mask[index++] = mask_element;
3568 if (index == nunits)
3570 if (! noop_p
3571 && ! can_vec_perm_p (mode, false, mask))
3573 if (dump_enabled_p ())
3575 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3576 vect_location,
3577 "unsupported vect permute { ");
3578 for (i = 0; i < nunits; ++i)
3579 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ", mask[i]);
3580 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3582 return false;
3585 if (! noop_p)
3586 ++*n_perms;
3588 if (!analyze_only)
3590 tree mask_vec = NULL_TREE;
3592 if (! noop_p)
3594 tree *mask_elts = XALLOCAVEC (tree, nunits);
3595 for (int l = 0; l < nunits; ++l)
3596 mask_elts[l] = build_int_cst (mask_element_type,
3597 mask[l]);
3598 mask_vec = build_vector (mask_type, mask_elts);
3601 if (second_vec_index == -1)
3602 second_vec_index = first_vec_index;
3604 /* Generate the permute statement if necessary. */
3605 tree first_vec = dr_chain[first_vec_index];
3606 tree second_vec = dr_chain[second_vec_index];
3607 gimple *perm_stmt;
3608 if (! noop_p)
3610 tree perm_dest
3611 = vect_create_destination_var (gimple_assign_lhs (stmt),
3612 vectype);
3613 perm_dest = make_ssa_name (perm_dest);
3614 perm_stmt = gimple_build_assign (perm_dest,
3615 VEC_PERM_EXPR,
3616 first_vec, second_vec,
3617 mask_vec);
3618 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
3620 else
3621 /* If mask was NULL_TREE generate the requested
3622 identity transform. */
3623 perm_stmt = SSA_NAME_DEF_STMT (first_vec);
3625 /* Store the vector statement in NODE. */
3626 SLP_TREE_VEC_STMTS (node)[vect_stmts_counter++] = perm_stmt;
3629 index = 0;
3630 first_vec_index = -1;
3631 second_vec_index = -1;
3632 noop_p = true;
3637 return true;
3642 /* Vectorize SLP instance tree in postorder. */
3644 static bool
3645 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3646 unsigned int vectorization_factor)
3648 gimple *stmt;
3649 bool grouped_store, is_store;
3650 gimple_stmt_iterator si;
3651 stmt_vec_info stmt_info;
3652 unsigned int vec_stmts_size, nunits, group_size;
3653 tree vectype;
3654 int i, j;
3655 slp_tree child;
3657 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3658 return false;
3660 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3661 vect_schedule_slp_instance (child, instance, vectorization_factor);
3663 /* Push SLP node def-type to stmts. */
3664 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3665 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
3666 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
3667 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child);
3669 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3670 stmt_info = vinfo_for_stmt (stmt);
3672 /* VECTYPE is the type of the destination. */
3673 vectype = STMT_VINFO_VECTYPE (stmt_info);
3674 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3675 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3677 /* For each SLP instance calculate number of vector stmts to be created
3678 for the scalar stmts in each node of the SLP tree. Number of vector
3679 elements in one vector iteration is the number of scalar elements in
3680 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3681 size.
3682 Unless this is a SLP reduction in which case the number of vector
3683 stmts is equal to the number of vector stmts of the children. */
3684 if (GROUP_FIRST_ELEMENT (stmt_info)
3685 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
3686 vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
3687 else
3688 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3690 if (!SLP_TREE_VEC_STMTS (node).exists ())
3692 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3693 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3696 if (dump_enabled_p ())
3698 dump_printf_loc (MSG_NOTE,vect_location,
3699 "------>vectorizing SLP node starting from: ");
3700 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3703 /* Vectorized stmts go before the last scalar stmt which is where
3704 all uses are ready. */
3705 si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node));
3707 /* Mark the first element of the reduction chain as reduction to properly
3708 transform the node. In the analysis phase only the last element of the
3709 chain is marked as reduction. */
3710 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3711 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3713 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3714 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3717 /* Handle two-operation SLP nodes by vectorizing the group with
3718 both operations and then performing a merge. */
3719 if (SLP_TREE_TWO_OPERATORS (node))
3721 enum tree_code code0 = gimple_assign_rhs_code (stmt);
3722 enum tree_code ocode = ERROR_MARK;
3723 gimple *ostmt;
3724 unsigned char *mask = XALLOCAVEC (unsigned char, group_size);
3725 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt)
3726 if (gimple_assign_rhs_code (ostmt) != code0)
3728 mask[i] = 1;
3729 ocode = gimple_assign_rhs_code (ostmt);
3731 else
3732 mask[i] = 0;
3733 if (ocode != ERROR_MARK)
3735 vec<gimple *> v0;
3736 vec<gimple *> v1;
3737 unsigned j;
3738 tree tmask = NULL_TREE;
3739 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3740 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3741 SLP_TREE_VEC_STMTS (node).truncate (0);
3742 gimple_assign_set_rhs_code (stmt, ocode);
3743 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3744 gimple_assign_set_rhs_code (stmt, code0);
3745 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3746 SLP_TREE_VEC_STMTS (node).truncate (0);
3747 tree meltype = build_nonstandard_integer_type
3748 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1);
3749 tree mvectype = get_same_sized_vectype (meltype, vectype);
3750 unsigned k = 0, l;
3751 for (j = 0; j < v0.length (); ++j)
3753 tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype));
3754 for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l)
3756 if (k >= group_size)
3757 k = 0;
3758 melts[l] = build_int_cst
3759 (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l);
3761 tmask = build_vector (mvectype, melts);
3763 /* ??? Not all targets support a VEC_PERM_EXPR with a
3764 constant mask that would translate to a vec_merge RTX
3765 (with their vec_perm_const_ok). We can either not
3766 vectorize in that case or let veclower do its job.
3767 Unfortunately that isn't too great and at least for
3768 plus/minus we'd eventually like to match targets
3769 vector addsub instructions. */
3770 gimple *vstmt;
3771 vstmt = gimple_build_assign (make_ssa_name (vectype),
3772 VEC_PERM_EXPR,
3773 gimple_assign_lhs (v0[j]),
3774 gimple_assign_lhs (v1[j]), tmask);
3775 vect_finish_stmt_generation (stmt, vstmt, &si);
3776 SLP_TREE_VEC_STMTS (node).quick_push (vstmt);
3778 v0.release ();
3779 v1.release ();
3780 return false;
3783 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3785 /* Restore stmt def-types. */
3786 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3787 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
3788 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
3789 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def;
3791 return is_store;
3794 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3795 For loop vectorization this is done in vectorizable_call, but for SLP
3796 it needs to be deferred until end of vect_schedule_slp, because multiple
3797 SLP instances may refer to the same scalar stmt. */
3799 static void
3800 vect_remove_slp_scalar_calls (slp_tree node)
3802 gimple *stmt, *new_stmt;
3803 gimple_stmt_iterator gsi;
3804 int i;
3805 slp_tree child;
3806 tree lhs;
3807 stmt_vec_info stmt_info;
3809 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3810 return;
3812 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3813 vect_remove_slp_scalar_calls (child);
3815 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3817 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3818 continue;
3819 stmt_info = vinfo_for_stmt (stmt);
3820 if (stmt_info == NULL
3821 || is_pattern_stmt_p (stmt_info)
3822 || !PURE_SLP_STMT (stmt_info))
3823 continue;
3824 lhs = gimple_call_lhs (stmt);
3825 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3826 set_vinfo_for_stmt (new_stmt, stmt_info);
3827 set_vinfo_for_stmt (stmt, NULL);
3828 STMT_VINFO_STMT (stmt_info) = new_stmt;
3829 gsi = gsi_for_stmt (stmt);
3830 gsi_replace (&gsi, new_stmt, false);
3831 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3835 /* Generate vector code for all SLP instances in the loop/basic block. */
3837 bool
3838 vect_schedule_slp (vec_info *vinfo)
3840 vec<slp_instance> slp_instances;
3841 slp_instance instance;
3842 unsigned int i, vf;
3843 bool is_store = false;
3845 slp_instances = vinfo->slp_instances;
3846 if (is_a <loop_vec_info> (vinfo))
3847 vf = as_a <loop_vec_info> (vinfo)->vectorization_factor;
3848 else
3849 vf = 1;
3851 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3853 /* Schedule the tree of INSTANCE. */
3854 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3855 instance, vf);
3856 if (dump_enabled_p ())
3857 dump_printf_loc (MSG_NOTE, vect_location,
3858 "vectorizing stmts using SLP.\n");
3861 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3863 slp_tree root = SLP_INSTANCE_TREE (instance);
3864 gimple *store;
3865 unsigned int j;
3866 gimple_stmt_iterator gsi;
3868 /* Remove scalar call stmts. Do not do this for basic-block
3869 vectorization as not all uses may be vectorized.
3870 ??? Why should this be necessary? DCE should be able to
3871 remove the stmts itself.
3872 ??? For BB vectorization we can as well remove scalar
3873 stmts starting from the SLP tree root if they have no
3874 uses. */
3875 if (is_a <loop_vec_info> (vinfo))
3876 vect_remove_slp_scalar_calls (root);
3878 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3879 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3881 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3882 break;
3884 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3885 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3886 /* Free the attached stmt_vec_info and remove the stmt. */
3887 gsi = gsi_for_stmt (store);
3888 unlink_stmt_vdef (store);
3889 gsi_remove (&gsi, true);
3890 release_defs (store);
3891 free_stmt_vec_info (store);
3895 return is_store;