[gcc]
[official-gcc.git] / gcc / tree-vect-slp.c
blobeb7bc55c43fbf56c1712b0225a4bcb72b64dbfc0
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 case vect_reduction_def:
407 break;
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 /* Recursively build an SLP tree starting from NODE.
912 Fail (and return a value not equal to zero) if def-stmts are not
913 isomorphic, require data permutation or are of unsupported types of
914 operation. Otherwise, return 0.
915 The value returned is the depth in the SLP tree where a mismatch
916 was found. */
918 static slp_tree
919 vect_build_slp_tree (vec_info *vinfo,
920 vec<gimple *> stmts, unsigned int group_size,
921 unsigned int *max_nunits,
922 vec<slp_tree> *loads,
923 bool *matches, unsigned *npermutes, unsigned *tree_size,
924 unsigned max_tree_size)
926 unsigned nops, i, this_tree_size = 0, this_max_nunits = *max_nunits;
927 gimple *stmt;
928 slp_tree node;
930 matches[0] = false;
932 stmt = stmts[0];
933 if (is_gimple_call (stmt))
934 nops = gimple_call_num_args (stmt);
935 else if (is_gimple_assign (stmt))
937 nops = gimple_num_ops (stmt) - 1;
938 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
939 nops++;
941 else if (gimple_code (stmt) == GIMPLE_PHI)
942 nops = 0;
943 else
944 return NULL;
946 /* If the SLP node is a PHI (induction), terminate the recursion. */
947 if (gimple_code (stmt) == GIMPLE_PHI)
949 FOR_EACH_VEC_ELT (stmts, i, stmt)
950 if (stmt != stmts[0])
951 /* Induction from different IVs is not supported. */
952 return NULL;
953 node = vect_create_new_slp_node (stmts);
954 return node;
958 bool two_operators = false;
959 unsigned char *swap = XALLOCAVEC (unsigned char, group_size);
960 if (!vect_build_slp_tree_1 (vinfo, swap,
961 stmts, group_size, nops,
962 &this_max_nunits, matches, &two_operators))
963 return NULL;
965 /* If the SLP node is a load, terminate the recursion. */
966 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
967 && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
969 *max_nunits = this_max_nunits;
970 node = vect_create_new_slp_node (stmts);
971 loads->safe_push (node);
972 return node;
975 /* Get at the operands, verifying they are compatible. */
976 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
977 slp_oprnd_info oprnd_info;
978 FOR_EACH_VEC_ELT (stmts, i, stmt)
980 int res = vect_get_and_check_slp_defs (vinfo, &swap[i],
981 stmt, i, &oprnds_info);
982 if (res != 0)
983 matches[(res == -1) ? 0 : i] = false;
984 if (!matches[0])
985 break;
987 for (i = 0; i < group_size; ++i)
988 if (!matches[i])
990 vect_free_oprnd_info (oprnds_info);
991 return NULL;
994 auto_vec<slp_tree, 4> children;
995 auto_vec<slp_tree> this_loads;
997 stmt = stmts[0];
999 /* Create SLP_TREE nodes for the definition node/s. */
1000 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
1002 slp_tree child;
1003 unsigned old_nloads = this_loads.length ();
1004 unsigned old_tree_size = this_tree_size;
1005 unsigned int j;
1007 if (oprnd_info->first_dt != vect_internal_def
1008 && oprnd_info->first_dt != vect_induction_def)
1009 continue;
1011 if (++this_tree_size > max_tree_size)
1013 FOR_EACH_VEC_ELT (children, j, child)
1014 vect_free_slp_tree (child);
1015 vect_free_oprnd_info (oprnds_info);
1016 return NULL;
1019 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1020 group_size, &this_max_nunits,
1021 &this_loads, matches, npermutes,
1022 &this_tree_size,
1023 max_tree_size)) != NULL)
1025 /* If we have all children of child built up from scalars then just
1026 throw that away and build it up this node from scalars. */
1027 if (!SLP_TREE_CHILDREN (child).is_empty ()
1028 /* ??? Rejecting patterns this way doesn't work. We'd have to
1029 do extra work to cancel the pattern so the uses see the
1030 scalar version. */
1031 && !is_pattern_stmt_p
1032 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0])))
1034 slp_tree grandchild;
1036 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1037 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
1038 break;
1039 if (!grandchild)
1041 /* Roll back. */
1042 this_loads.truncate (old_nloads);
1043 this_tree_size = old_tree_size;
1044 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1045 vect_free_slp_tree (grandchild);
1046 SLP_TREE_CHILDREN (child).truncate (0);
1048 dump_printf_loc (MSG_NOTE, vect_location,
1049 "Building parent vector operands from "
1050 "scalars instead\n");
1051 oprnd_info->def_stmts = vNULL;
1052 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1053 children.safe_push (child);
1054 continue;
1058 oprnd_info->def_stmts = vNULL;
1059 children.safe_push (child);
1060 continue;
1063 /* If the SLP build failed fatally and we analyze a basic-block
1064 simply treat nodes we fail to build as externally defined
1065 (and thus build vectors from the scalar defs).
1066 The cost model will reject outright expensive cases.
1067 ??? This doesn't treat cases where permutation ultimatively
1068 fails (or we don't try permutation below). Ideally we'd
1069 even compute a permutation that will end up with the maximum
1070 SLP tree size... */
1071 if (is_a <bb_vec_info> (vinfo)
1072 && !matches[0]
1073 /* ??? Rejecting patterns this way doesn't work. We'd have to
1074 do extra work to cancel the pattern so the uses see the
1075 scalar version. */
1076 && !is_pattern_stmt_p (vinfo_for_stmt (stmt)))
1078 dump_printf_loc (MSG_NOTE, vect_location,
1079 "Building vector operands from scalars\n");
1080 child = vect_create_new_slp_node (oprnd_info->def_stmts);
1081 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1082 children.safe_push (child);
1083 oprnd_info->def_stmts = vNULL;
1084 continue;
1087 /* If the SLP build for operand zero failed and operand zero
1088 and one can be commutated try that for the scalar stmts
1089 that failed the match. */
1090 if (i == 0
1091 /* A first scalar stmt mismatch signals a fatal mismatch. */
1092 && matches[0]
1093 /* ??? For COND_EXPRs we can swap the comparison operands
1094 as well as the arms under some constraints. */
1095 && nops == 2
1096 && oprnds_info[1]->first_dt == vect_internal_def
1097 && is_gimple_assign (stmt)
1098 && commutative_tree_code (gimple_assign_rhs_code (stmt))
1099 && ! two_operators
1100 /* Do so only if the number of not successful permutes was nor more
1101 than a cut-ff as re-trying the recursive match on
1102 possibly each level of the tree would expose exponential
1103 behavior. */
1104 && *npermutes < 4)
1106 /* Verify if we can safely swap or if we committed to a specific
1107 operand order already. */
1108 for (j = 0; j < group_size; ++j)
1109 if (!matches[j]
1110 && (swap[j] != 0
1111 || STMT_VINFO_NUM_SLP_USES (vinfo_for_stmt (stmts[j]))))
1113 if (dump_enabled_p ())
1115 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1116 "Build SLP failed: cannot swap operands "
1117 "of shared stmt ");
1118 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1119 stmts[j], 0);
1121 goto fail;
1124 /* Swap mismatched definition stmts. */
1125 dump_printf_loc (MSG_NOTE, vect_location,
1126 "Re-trying with swapped operands of stmts ");
1127 for (j = 0; j < group_size; ++j)
1128 if (!matches[j])
1130 std::swap (oprnds_info[0]->def_stmts[j],
1131 oprnds_info[1]->def_stmts[j]);
1132 dump_printf (MSG_NOTE, "%d ", j);
1134 dump_printf (MSG_NOTE, "\n");
1135 /* And try again with scratch 'matches' ... */
1136 bool *tem = XALLOCAVEC (bool, group_size);
1137 if ((child = vect_build_slp_tree (vinfo, oprnd_info->def_stmts,
1138 group_size, &this_max_nunits,
1139 &this_loads, tem, npermutes,
1140 &this_tree_size,
1141 max_tree_size)) != NULL)
1143 /* ... so if successful we can apply the operand swapping
1144 to the GIMPLE IL. This is necessary because for example
1145 vect_get_slp_defs uses operand indexes and thus expects
1146 canonical operand order. This is also necessary even
1147 if we end up building the operand from scalars as
1148 we'll continue to process swapped operand two. */
1149 for (j = 0; j < group_size; ++j)
1151 gimple *stmt = stmts[j];
1152 gimple_set_plf (stmt, GF_PLF_1, false);
1154 for (j = 0; j < group_size; ++j)
1156 gimple *stmt = stmts[j];
1157 if (!matches[j])
1159 /* Avoid swapping operands twice. */
1160 if (gimple_plf (stmt, GF_PLF_1))
1161 continue;
1162 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1163 gimple_assign_rhs2_ptr (stmt));
1164 gimple_set_plf (stmt, GF_PLF_1, true);
1167 /* Verify we swap all duplicates or none. */
1168 if (flag_checking)
1169 for (j = 0; j < group_size; ++j)
1171 gimple *stmt = stmts[j];
1172 gcc_assert (gimple_plf (stmt, GF_PLF_1) == ! matches[j]);
1175 /* If we have all children of child built up from scalars then
1176 just throw that away and build it up this node from scalars. */
1177 if (!SLP_TREE_CHILDREN (child).is_empty ()
1178 /* ??? Rejecting patterns this way doesn't work. We'd have
1179 to do extra work to cancel the pattern so the uses see the
1180 scalar version. */
1181 && !is_pattern_stmt_p
1182 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[0])))
1184 unsigned int j;
1185 slp_tree grandchild;
1187 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1188 if (SLP_TREE_DEF_TYPE (grandchild) == vect_internal_def)
1189 break;
1190 if (!grandchild)
1192 /* Roll back. */
1193 this_loads.truncate (old_nloads);
1194 this_tree_size = old_tree_size;
1195 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1196 vect_free_slp_tree (grandchild);
1197 SLP_TREE_CHILDREN (child).truncate (0);
1199 dump_printf_loc (MSG_NOTE, vect_location,
1200 "Building parent vector operands from "
1201 "scalars instead\n");
1202 oprnd_info->def_stmts = vNULL;
1203 SLP_TREE_DEF_TYPE (child) = vect_external_def;
1204 children.safe_push (child);
1205 continue;
1209 oprnd_info->def_stmts = vNULL;
1210 children.safe_push (child);
1211 continue;
1214 ++*npermutes;
1217 fail:
1218 gcc_assert (child == NULL);
1219 FOR_EACH_VEC_ELT (children, j, child)
1220 vect_free_slp_tree (child);
1221 vect_free_oprnd_info (oprnds_info);
1222 return NULL;
1225 vect_free_oprnd_info (oprnds_info);
1227 if (tree_size)
1228 *tree_size += this_tree_size;
1229 *max_nunits = this_max_nunits;
1230 loads->safe_splice (this_loads);
1232 node = vect_create_new_slp_node (stmts);
1233 SLP_TREE_TWO_OPERATORS (node) = two_operators;
1234 SLP_TREE_CHILDREN (node).splice (children);
1235 return node;
1238 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
1240 static void
1241 vect_print_slp_tree (dump_flags_t dump_kind, location_t loc, slp_tree node)
1243 int i;
1244 gimple *stmt;
1245 slp_tree child;
1247 dump_printf_loc (dump_kind, loc, "node%s\n",
1248 SLP_TREE_DEF_TYPE (node) != vect_internal_def
1249 ? " (external)" : "");
1250 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1252 dump_printf_loc (dump_kind, loc, "\tstmt %d ", i);
1253 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
1255 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1256 vect_print_slp_tree (dump_kind, loc, child);
1260 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1261 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
1262 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1263 stmts in NODE are to be marked. */
1265 static void
1266 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1268 int i;
1269 gimple *stmt;
1270 slp_tree child;
1272 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
1273 return;
1275 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1276 if (j < 0 || i == j)
1277 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1279 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1280 vect_mark_slp_stmts (child, mark, j);
1284 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1286 static void
1287 vect_mark_slp_stmts_relevant (slp_tree node)
1289 int i;
1290 gimple *stmt;
1291 stmt_vec_info stmt_info;
1292 slp_tree child;
1294 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
1295 return;
1297 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1299 stmt_info = vinfo_for_stmt (stmt);
1300 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1301 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1302 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1305 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1306 vect_mark_slp_stmts_relevant (child);
1310 /* Rearrange the statements of NODE according to PERMUTATION. */
1312 static void
1313 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1314 vec<unsigned> permutation)
1316 gimple *stmt;
1317 vec<gimple *> tmp_stmts;
1318 unsigned int i;
1319 slp_tree child;
1321 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1322 vect_slp_rearrange_stmts (child, group_size, permutation);
1324 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1325 tmp_stmts.create (group_size);
1326 tmp_stmts.quick_grow_cleared (group_size);
1328 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1329 tmp_stmts[permutation[i]] = stmt;
1331 SLP_TREE_SCALAR_STMTS (node).release ();
1332 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1336 /* Attempt to reorder stmts in a reduction chain so that we don't
1337 require any load permutation. Return true if that was possible,
1338 otherwise return false. */
1340 static bool
1341 vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1343 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1344 unsigned int i, j;
1345 unsigned int lidx;
1346 slp_tree node, load;
1348 /* Compare all the permutation sequences to the first one. We know
1349 that at least one load is permuted. */
1350 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1351 if (!node->load_permutation.exists ())
1352 return false;
1353 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1355 if (!load->load_permutation.exists ())
1356 return false;
1357 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1358 if (lidx != node->load_permutation[j])
1359 return false;
1362 /* Check that the loads in the first sequence are different and there
1363 are no gaps between them. */
1364 auto_sbitmap load_index (group_size);
1365 bitmap_clear (load_index);
1366 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1368 if (lidx >= group_size)
1369 return false;
1370 if (bitmap_bit_p (load_index, lidx))
1371 return false;
1373 bitmap_set_bit (load_index, lidx);
1375 for (i = 0; i < group_size; i++)
1376 if (!bitmap_bit_p (load_index, i))
1377 return false;
1379 /* This permutation is valid for reduction. Since the order of the
1380 statements in the nodes is not important unless they are memory
1381 accesses, we can rearrange the statements in all the nodes
1382 according to the order of the loads. */
1383 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1384 node->load_permutation);
1386 /* We are done, no actual permutations need to be generated. */
1387 unsigned int unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_instn);
1388 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1390 gimple *first_stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1391 first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt));
1392 /* But we have to keep those permutations that are required because
1393 of handling of gaps. */
1394 if (unrolling_factor == 1
1395 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1396 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0))
1397 SLP_TREE_LOAD_PERMUTATION (node).release ();
1398 else
1399 for (j = 0; j < SLP_TREE_LOAD_PERMUTATION (node).length (); ++j)
1400 SLP_TREE_LOAD_PERMUTATION (node)[j] = j;
1403 return true;
1406 /* Check if the required load permutations in the SLP instance
1407 SLP_INSTN are supported. */
1409 static bool
1410 vect_supported_load_permutation_p (slp_instance slp_instn)
1412 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1413 unsigned int i, j, k, next;
1414 slp_tree node;
1415 gimple *stmt, *load, *next_load;
1417 if (dump_enabled_p ())
1419 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1420 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1421 if (node->load_permutation.exists ())
1422 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1423 dump_printf (MSG_NOTE, "%d ", next);
1424 else
1425 for (k = 0; k < group_size; ++k)
1426 dump_printf (MSG_NOTE, "%d ", k);
1427 dump_printf (MSG_NOTE, "\n");
1430 /* In case of reduction every load permutation is allowed, since the order
1431 of the reduction statements is not important (as opposed to the case of
1432 grouped stores). The only condition we need to check is that all the
1433 load nodes are of the same size and have the same permutation (and then
1434 rearrange all the nodes of the SLP instance according to this
1435 permutation). */
1437 /* Check that all the load nodes are of the same size. */
1438 /* ??? Can't we assert this? */
1439 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1440 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1441 return false;
1443 node = SLP_INSTANCE_TREE (slp_instn);
1444 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1446 /* Reduction (there are no data-refs in the root).
1447 In reduction chain the order of the loads is not important. */
1448 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1449 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1450 vect_attempt_slp_rearrange_stmts (slp_instn);
1452 /* In basic block vectorization we allow any subchain of an interleaving
1453 chain.
1454 FORNOW: not supported in loop SLP because of realignment compications. */
1455 if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
1457 /* Check whether the loads in an instance form a subchain and thus
1458 no permutation is necessary. */
1459 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1461 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1462 continue;
1463 bool subchain_p = true;
1464 next_load = NULL;
1465 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1467 if (j != 0
1468 && (next_load != load
1469 || GROUP_GAP (vinfo_for_stmt (load)) != 1))
1471 subchain_p = false;
1472 break;
1474 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1476 if (subchain_p)
1477 SLP_TREE_LOAD_PERMUTATION (node).release ();
1478 else
1480 stmt_vec_info group_info
1481 = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1482 group_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (group_info));
1483 unsigned nunits
1484 = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (group_info));
1485 unsigned k, maxk = 0;
1486 FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (node), j, k)
1487 if (k > maxk)
1488 maxk = k;
1489 /* In BB vectorization we may not actually use a loaded vector
1490 accessing elements in excess of GROUP_SIZE. */
1491 if (maxk >= (GROUP_SIZE (group_info) & ~(nunits - 1)))
1493 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1494 "BB vectorization with gaps at the end of "
1495 "a load is not supported\n");
1496 return false;
1499 /* Verify the permutation can be generated. */
1500 vec<tree> tem;
1501 unsigned n_perms;
1502 if (!vect_transform_slp_perm_load (node, tem, NULL,
1503 1, slp_instn, true, &n_perms))
1505 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1506 vect_location,
1507 "unsupported load permutation\n");
1508 return false;
1512 return true;
1515 /* For loop vectorization verify we can generate the permutation. */
1516 unsigned n_perms;
1517 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1518 if (node->load_permutation.exists ()
1519 && !vect_transform_slp_perm_load
1520 (node, vNULL, NULL,
1521 SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true,
1522 &n_perms))
1523 return false;
1525 return true;
1529 /* Find the last store in SLP INSTANCE. */
1531 gimple *
1532 vect_find_last_scalar_stmt_in_slp (slp_tree node)
1534 gimple *last = NULL, *stmt;
1536 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++)
1538 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1539 if (is_pattern_stmt_p (stmt_vinfo))
1540 last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last);
1541 else
1542 last = get_later_stmt (stmt, last);
1545 return last;
1548 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1550 static void
1551 vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node,
1552 stmt_vector_for_cost *prologue_cost_vec,
1553 stmt_vector_for_cost *body_cost_vec,
1554 unsigned ncopies_for_cost)
1556 unsigned i, j;
1557 slp_tree child;
1558 gimple *stmt;
1559 stmt_vec_info stmt_info;
1560 tree lhs;
1562 /* Recurse down the SLP tree. */
1563 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1564 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
1565 vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec,
1566 body_cost_vec, ncopies_for_cost);
1568 /* Look at the first scalar stmt to determine the cost. */
1569 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1570 stmt_info = vinfo_for_stmt (stmt);
1571 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1573 vect_memory_access_type memory_access_type
1574 = (STMT_VINFO_STRIDED_P (stmt_info)
1575 ? VMAT_STRIDED_SLP
1576 : VMAT_CONTIGUOUS);
1577 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1578 vect_model_store_cost (stmt_info, ncopies_for_cost,
1579 memory_access_type, vect_uninitialized_def,
1580 node, prologue_cost_vec, body_cost_vec);
1581 else
1583 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1584 if (SLP_TREE_LOAD_PERMUTATION (node).exists ())
1586 /* If the load is permuted then the alignment is determined by
1587 the first group element not by the first scalar stmt DR. */
1588 stmt = GROUP_FIRST_ELEMENT (stmt_info);
1589 stmt_info = vinfo_for_stmt (stmt);
1590 /* Record the cost for the permutation. */
1591 unsigned n_perms;
1592 vect_transform_slp_perm_load (node, vNULL, NULL,
1593 ncopies_for_cost, instance, true,
1594 &n_perms);
1595 record_stmt_cost (body_cost_vec, n_perms, vec_perm,
1596 stmt_info, 0, vect_body);
1597 unsigned nunits
1598 = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1599 /* And adjust the number of loads performed. This handles
1600 redundancies as well as loads that are later dead. */
1601 auto_sbitmap perm (GROUP_SIZE (stmt_info));
1602 bitmap_clear (perm);
1603 for (i = 0; i < SLP_TREE_LOAD_PERMUTATION (node).length (); ++i)
1604 bitmap_set_bit (perm, SLP_TREE_LOAD_PERMUTATION (node)[i]);
1605 ncopies_for_cost = 0;
1606 bool load_seen = false;
1607 for (i = 0; i < GROUP_SIZE (stmt_info); ++i)
1609 if (i % nunits == 0)
1611 if (load_seen)
1612 ncopies_for_cost++;
1613 load_seen = false;
1615 if (bitmap_bit_p (perm, i))
1616 load_seen = true;
1618 if (load_seen)
1619 ncopies_for_cost++;
1620 gcc_assert (ncopies_for_cost
1621 <= (GROUP_SIZE (stmt_info) - GROUP_GAP (stmt_info)
1622 + nunits - 1) / nunits);
1623 ncopies_for_cost *= SLP_INSTANCE_UNROLLING_FACTOR (instance);
1625 /* Record the cost for the vector loads. */
1626 vect_model_load_cost (stmt_info, ncopies_for_cost,
1627 memory_access_type, node, prologue_cost_vec,
1628 body_cost_vec);
1629 return;
1632 else if (STMT_VINFO_TYPE (stmt_info) == induc_vec_info_type)
1634 /* ncopies_for_cost is the number of IVs we generate. */
1635 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1636 stmt_info, 0, vect_body);
1638 /* Prologue cost for the initial values and step vector. */
1639 record_stmt_cost (prologue_cost_vec, ncopies_for_cost,
1640 CONSTANT_CLASS_P
1641 (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED
1642 (stmt_info))
1643 ? vector_load : vec_construct,
1644 stmt_info, 0, vect_prologue);
1645 record_stmt_cost (prologue_cost_vec, 1,
1646 CONSTANT_CLASS_P
1647 (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info))
1648 ? vector_load : vec_construct,
1649 stmt_info, 0, vect_prologue);
1651 /* ??? No easy way to get at the actual number of vector stmts
1652 to be geneated and thus the derived IVs. */
1654 else
1656 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1657 stmt_info, 0, vect_body);
1658 if (SLP_TREE_TWO_OPERATORS (node))
1660 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1661 stmt_info, 0, vect_body);
1662 record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm,
1663 stmt_info, 0, vect_body);
1667 /* Push SLP node def-type to stmts. */
1668 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1669 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
1670 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
1671 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child);
1673 /* Scan operands and account for prologue cost of constants/externals.
1674 ??? This over-estimates cost for multiple uses and should be
1675 re-engineered. */
1676 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1677 lhs = gimple_get_lhs (stmt);
1678 for (i = 0; i < gimple_num_ops (stmt); ++i)
1680 tree op = gimple_op (stmt, i);
1681 gimple *def_stmt;
1682 enum vect_def_type dt;
1683 if (!op || op == lhs)
1684 continue;
1685 if (vect_is_simple_use (op, stmt_info->vinfo, &def_stmt, &dt))
1687 /* Without looking at the actual initializer a vector of
1688 constants can be implemented as load from the constant pool.
1689 ??? We need to pass down stmt_info for a vector type
1690 even if it points to the wrong stmt. */
1691 if (dt == vect_constant_def)
1692 record_stmt_cost (prologue_cost_vec, 1, vector_load,
1693 stmt_info, 0, vect_prologue);
1694 else if (dt == vect_external_def)
1695 record_stmt_cost (prologue_cost_vec, 1, vec_construct,
1696 stmt_info, 0, vect_prologue);
1700 /* Restore stmt def-types. */
1701 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1702 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
1703 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
1704 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def;
1707 /* Compute the cost for the SLP instance INSTANCE. */
1709 static void
1710 vect_analyze_slp_cost (slp_instance instance, void *data)
1712 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1713 unsigned ncopies_for_cost;
1714 stmt_info_for_cost *si;
1715 unsigned i;
1717 if (dump_enabled_p ())
1718 dump_printf_loc (MSG_NOTE, vect_location,
1719 "=== vect_analyze_slp_cost ===\n");
1721 /* Calculate the number of vector stmts to create based on the unrolling
1722 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1723 GROUP_SIZE / NUNITS otherwise. */
1724 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1725 slp_tree node = SLP_INSTANCE_TREE (instance);
1726 stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1727 /* Adjust the group_size by the vectorization factor which is always one
1728 for basic-block vectorization. */
1729 if (STMT_VINFO_LOOP_VINFO (stmt_info))
1730 group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info));
1731 unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1732 /* For reductions look at a reduction operand in case the reduction
1733 operation is widening like DOT_PROD or SAD. */
1734 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1736 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1737 switch (gimple_assign_rhs_code (stmt))
1739 case DOT_PROD_EXPR:
1740 case SAD_EXPR:
1741 nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type
1742 (TREE_TYPE (gimple_assign_rhs1 (stmt))));
1743 break;
1744 default:;
1747 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1749 prologue_cost_vec.create (10);
1750 body_cost_vec.create (10);
1751 vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance),
1752 &prologue_cost_vec, &body_cost_vec,
1753 ncopies_for_cost);
1755 /* Record the prologue costs, which were delayed until we were
1756 sure that SLP was successful. */
1757 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1759 struct _stmt_vec_info *stmt_info
1760 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1761 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1762 si->misalign, vect_prologue);
1765 /* Record the instance's instructions in the target cost model. */
1766 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1768 struct _stmt_vec_info *stmt_info
1769 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1770 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1771 si->misalign, vect_body);
1774 prologue_cost_vec.release ();
1775 body_cost_vec.release ();
1778 /* Splits a group of stores, currently beginning at FIRST_STMT, into two groups:
1779 one (still beginning at FIRST_STMT) of size GROUP1_SIZE (also containing
1780 the first GROUP1_SIZE stmts, since stores are consecutive), the second
1781 containing the remainder.
1782 Return the first stmt in the second group. */
1784 static gimple *
1785 vect_split_slp_store_group (gimple *first_stmt, unsigned group1_size)
1787 stmt_vec_info first_vinfo = vinfo_for_stmt (first_stmt);
1788 gcc_assert (GROUP_FIRST_ELEMENT (first_vinfo) == first_stmt);
1789 gcc_assert (group1_size > 0);
1790 int group2_size = GROUP_SIZE (first_vinfo) - group1_size;
1791 gcc_assert (group2_size > 0);
1792 GROUP_SIZE (first_vinfo) = group1_size;
1794 gimple *stmt = first_stmt;
1795 for (unsigned i = group1_size; i > 1; i--)
1797 stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1798 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1800 /* STMT is now the last element of the first group. */
1801 gimple *group2 = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
1802 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)) = 0;
1804 GROUP_SIZE (vinfo_for_stmt (group2)) = group2_size;
1805 for (stmt = group2; stmt; stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt)))
1807 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = group2;
1808 gcc_assert (GROUP_GAP (vinfo_for_stmt (stmt)) == 1);
1811 /* For the second group, the GROUP_GAP is that before the original group,
1812 plus skipping over the first vector. */
1813 GROUP_GAP (vinfo_for_stmt (group2)) =
1814 GROUP_GAP (first_vinfo) + group1_size;
1816 /* GROUP_GAP of the first group now has to skip over the second group too. */
1817 GROUP_GAP (first_vinfo) += group2_size;
1819 if (dump_enabled_p ())
1820 dump_printf_loc (MSG_NOTE, vect_location, "Split group into %d and %d\n",
1821 group1_size, group2_size);
1823 return group2;
1826 /* Analyze an SLP instance starting from a group of grouped stores. Call
1827 vect_build_slp_tree to build a tree of packed stmts if possible.
1828 Return FALSE if it's impossible to SLP any stmt in the loop. */
1830 static bool
1831 vect_analyze_slp_instance (vec_info *vinfo,
1832 gimple *stmt, unsigned max_tree_size)
1834 slp_instance new_instance;
1835 slp_tree node;
1836 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1837 unsigned int unrolling_factor = 1, nunits;
1838 tree vectype, scalar_type = NULL_TREE;
1839 gimple *next;
1840 unsigned int i;
1841 unsigned int max_nunits = 0;
1842 vec<slp_tree> loads;
1843 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1844 vec<gimple *> scalar_stmts;
1846 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1848 if (dr)
1850 scalar_type = TREE_TYPE (DR_REF (dr));
1851 vectype = get_vectype_for_scalar_type (scalar_type);
1853 else
1855 gcc_assert (is_a <loop_vec_info> (vinfo));
1856 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1859 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1861 else
1863 gcc_assert (is_a <loop_vec_info> (vinfo));
1864 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1865 group_size = as_a <loop_vec_info> (vinfo)->reductions.length ();
1868 if (!vectype)
1870 if (dump_enabled_p ())
1872 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1873 "Build SLP failed: unsupported data-type ");
1874 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1875 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1878 return false;
1880 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1882 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1883 scalar_stmts.create (group_size);
1884 next = stmt;
1885 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1887 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1888 while (next)
1890 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1891 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1892 scalar_stmts.safe_push (
1893 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1894 else
1895 scalar_stmts.safe_push (next);
1896 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1898 /* Mark the first element of the reduction chain as reduction to properly
1899 transform the node. In the reduction analysis phase only the last
1900 element of the chain is marked as reduction. */
1901 if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1902 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def;
1904 else
1906 /* Collect reduction statements. */
1907 vec<gimple *> reductions = as_a <loop_vec_info> (vinfo)->reductions;
1908 for (i = 0; reductions.iterate (i, &next); i++)
1909 scalar_stmts.safe_push (next);
1912 loads.create (group_size);
1914 /* Build the tree for the SLP instance. */
1915 bool *matches = XALLOCAVEC (bool, group_size);
1916 unsigned npermutes = 0;
1917 node = vect_build_slp_tree (vinfo, scalar_stmts, group_size,
1918 &max_nunits, &loads, matches, &npermutes,
1919 NULL, max_tree_size);
1920 if (node != NULL)
1922 /* Calculate the unrolling factor based on the smallest type. */
1923 unrolling_factor
1924 = least_common_multiple (max_nunits, group_size) / group_size;
1926 if (unrolling_factor != 1
1927 && is_a <bb_vec_info> (vinfo))
1930 if (max_nunits > group_size)
1932 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1933 "Build SLP failed: store group "
1934 "size not a multiple of the vector size "
1935 "in basic block SLP\n");
1936 vect_free_slp_tree (node);
1937 loads.release ();
1938 return false;
1940 /* Fatal mismatch. */
1941 matches[group_size/max_nunits * max_nunits] = false;
1942 vect_free_slp_tree (node);
1943 loads.release ();
1945 else
1947 /* Create a new SLP instance. */
1948 new_instance = XNEW (struct _slp_instance);
1949 SLP_INSTANCE_TREE (new_instance) = node;
1950 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1951 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1952 SLP_INSTANCE_LOADS (new_instance) = loads;
1954 /* Compute the load permutation. */
1955 slp_tree load_node;
1956 bool loads_permuted = false;
1957 FOR_EACH_VEC_ELT (loads, i, load_node)
1959 vec<unsigned> load_permutation;
1960 int j;
1961 gimple *load, *first_stmt;
1962 bool this_load_permuted = false;
1963 load_permutation.create (group_size);
1964 first_stmt = GROUP_FIRST_ELEMENT
1965 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
1966 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
1968 int load_place = vect_get_place_in_interleaving_chain
1969 (load, first_stmt);
1970 gcc_assert (load_place != -1);
1971 if (load_place != j)
1972 this_load_permuted = true;
1973 load_permutation.safe_push (load_place);
1975 if (!this_load_permuted
1976 /* The load requires permutation when unrolling exposes
1977 a gap either because the group is larger than the SLP
1978 group-size or because there is a gap between the groups. */
1979 && (unrolling_factor == 1
1980 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1981 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)))
1983 load_permutation.release ();
1984 continue;
1986 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
1987 loads_permuted = true;
1990 if (loads_permuted)
1992 if (!vect_supported_load_permutation_p (new_instance))
1994 if (dump_enabled_p ())
1996 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1997 "Build SLP failed: unsupported load "
1998 "permutation ");
1999 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION,
2000 TDF_SLIM, stmt, 0);
2002 vect_free_slp_instance (new_instance);
2003 return false;
2007 /* If the loads and stores can be handled with load/store-lan
2008 instructions do not generate this SLP instance. */
2009 if (is_a <loop_vec_info> (vinfo)
2010 && loads_permuted
2011 && dr && vect_store_lanes_supported (vectype, group_size))
2013 slp_tree load_node;
2014 FOR_EACH_VEC_ELT (loads, i, load_node)
2016 gimple *first_stmt = GROUP_FIRST_ELEMENT
2017 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
2018 stmt_vec_info stmt_vinfo = vinfo_for_stmt (first_stmt);
2019 /* Use SLP for strided accesses (or if we
2020 can't load-lanes). */
2021 if (STMT_VINFO_STRIDED_P (stmt_vinfo)
2022 || ! vect_load_lanes_supported
2023 (STMT_VINFO_VECTYPE (stmt_vinfo),
2024 GROUP_SIZE (stmt_vinfo)))
2025 break;
2027 if (i == loads.length ())
2029 if (dump_enabled_p ())
2030 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2031 "Built SLP cancelled: can use "
2032 "load/store-lanes\n");
2033 vect_free_slp_instance (new_instance);
2034 return false;
2038 vinfo->slp_instances.safe_push (new_instance);
2040 if (dump_enabled_p ())
2042 dump_printf_loc (MSG_NOTE, vect_location,
2043 "Final SLP tree for instance:\n");
2044 vect_print_slp_tree (MSG_NOTE, vect_location, node);
2047 return true;
2050 else
2052 /* Failed to SLP. */
2053 /* Free the allocated memory. */
2054 scalar_stmts.release ();
2055 loads.release ();
2058 /* For basic block SLP, try to break the group up into multiples of the
2059 vector size. */
2060 if (is_a <bb_vec_info> (vinfo)
2061 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2062 && STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
2064 /* We consider breaking the group only on VF boundaries from the existing
2065 start. */
2066 for (i = 0; i < group_size; i++)
2067 if (!matches[i]) break;
2069 if (i >= nunits && i < group_size)
2071 /* Split into two groups at the first vector boundary before i. */
2072 gcc_assert ((nunits & (nunits - 1)) == 0);
2073 unsigned group1_size = i & ~(nunits - 1);
2075 gimple *rest = vect_split_slp_store_group (stmt, group1_size);
2076 bool res = vect_analyze_slp_instance (vinfo, stmt, max_tree_size);
2077 /* If the first non-match was in the middle of a vector,
2078 skip the rest of that vector. */
2079 if (group1_size < i)
2081 i = group1_size + nunits;
2082 if (i < group_size)
2083 rest = vect_split_slp_store_group (rest, nunits);
2085 if (i < group_size)
2086 res |= vect_analyze_slp_instance (vinfo, rest, max_tree_size);
2087 return res;
2089 /* Even though the first vector did not all match, we might be able to SLP
2090 (some) of the remainder. FORNOW ignore this possibility. */
2093 return false;
2097 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
2098 trees of packed scalar stmts if SLP is possible. */
2100 bool
2101 vect_analyze_slp (vec_info *vinfo, unsigned max_tree_size)
2103 unsigned int i;
2104 gimple *first_element;
2105 bool ok = false;
2107 if (dump_enabled_p ())
2108 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
2110 /* Find SLP sequences starting from groups of grouped stores. */
2111 FOR_EACH_VEC_ELT (vinfo->grouped_stores, i, first_element)
2112 if (vect_analyze_slp_instance (vinfo, first_element, max_tree_size))
2113 ok = true;
2115 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
2117 if (loop_vinfo->reduction_chains.length () > 0)
2119 /* Find SLP sequences starting from reduction chains. */
2120 FOR_EACH_VEC_ELT (loop_vinfo->reduction_chains, i, first_element)
2121 if (vect_analyze_slp_instance (vinfo, first_element,
2122 max_tree_size))
2123 ok = true;
2124 else
2125 return false;
2127 /* Don't try to vectorize SLP reductions if reduction chain was
2128 detected. */
2129 return ok;
2132 /* Find SLP sequences starting from groups of reductions. */
2133 if (loop_vinfo->reductions.length () > 1
2134 && vect_analyze_slp_instance (vinfo, loop_vinfo->reductions[0],
2135 max_tree_size))
2136 ok = true;
2139 return true;
2143 /* For each possible SLP instance decide whether to SLP it and calculate overall
2144 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
2145 least one instance. */
2147 bool
2148 vect_make_slp_decision (loop_vec_info loop_vinfo)
2150 unsigned int i, unrolling_factor = 1;
2151 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2152 slp_instance instance;
2153 int decided_to_slp = 0;
2155 if (dump_enabled_p ())
2156 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
2157 "\n");
2159 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2161 /* FORNOW: SLP if you can. */
2162 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
2163 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
2165 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
2166 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
2167 loop-based vectorization. Such stmts will be marked as HYBRID. */
2168 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2169 decided_to_slp++;
2172 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
2174 if (decided_to_slp && dump_enabled_p ())
2175 dump_printf_loc (MSG_NOTE, vect_location,
2176 "Decided to SLP %d instances. Unrolling factor %d\n",
2177 decided_to_slp, unrolling_factor);
2179 return (decided_to_slp > 0);
2183 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
2184 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
2186 static void
2187 vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
2189 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[i];
2190 imm_use_iterator imm_iter;
2191 gimple *use_stmt;
2192 stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
2193 slp_tree child;
2194 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
2195 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2196 int j;
2198 /* Propagate hybrid down the SLP tree. */
2199 if (stype == hybrid)
2201 else if (HYBRID_SLP_STMT (stmt_vinfo))
2202 stype = hybrid;
2203 else
2205 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
2206 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
2207 /* If we get a pattern stmt here we have to use the LHS of the
2208 original stmt for immediate uses. */
2209 if (! STMT_VINFO_IN_PATTERN_P (stmt_vinfo)
2210 && STMT_VINFO_RELATED_STMT (stmt_vinfo))
2211 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
2212 tree def;
2213 if (gimple_code (stmt) == GIMPLE_PHI)
2214 def = gimple_phi_result (stmt);
2215 else
2216 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
2217 if (def)
2218 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2220 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2221 continue;
2222 use_vinfo = vinfo_for_stmt (use_stmt);
2223 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
2224 && STMT_VINFO_RELATED_STMT (use_vinfo))
2225 use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
2226 if (!STMT_SLP_TYPE (use_vinfo)
2227 && (STMT_VINFO_RELEVANT (use_vinfo)
2228 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2229 && !(gimple_code (use_stmt) == GIMPLE_PHI
2230 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2232 if (dump_enabled_p ())
2234 dump_printf_loc (MSG_NOTE, vect_location, "use of SLP "
2235 "def in non-SLP stmt: ");
2236 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, use_stmt, 0);
2238 stype = hybrid;
2243 if (stype == hybrid
2244 && !HYBRID_SLP_STMT (stmt_vinfo))
2246 if (dump_enabled_p ())
2248 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2249 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2251 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2254 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2255 if (SLP_TREE_DEF_TYPE (child) != vect_external_def)
2256 vect_detect_hybrid_slp_stmts (child, i, stype);
2259 /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
2261 static tree
2262 vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2264 walk_stmt_info *wi = (walk_stmt_info *)data;
2265 struct loop *loopp = (struct loop *)wi->info;
2267 if (wi->is_lhs)
2268 return NULL_TREE;
2270 if (TREE_CODE (*tp) == SSA_NAME
2271 && !SSA_NAME_IS_DEFAULT_DEF (*tp))
2273 gimple *def_stmt = SSA_NAME_DEF_STMT (*tp);
2274 if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
2275 && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
2277 if (dump_enabled_p ())
2279 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2280 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2282 STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
2286 return NULL_TREE;
2289 static tree
2290 vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
2291 walk_stmt_info *)
2293 stmt_vec_info use_vinfo = vinfo_for_stmt (gsi_stmt (*gsi));
2294 /* If the stmt is in a SLP instance then this isn't a reason
2295 to mark use definitions in other SLP instances as hybrid. */
2296 if (! STMT_SLP_TYPE (use_vinfo)
2297 && (STMT_VINFO_RELEVANT (use_vinfo)
2298 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2299 && ! (gimple_code (gsi_stmt (*gsi)) == GIMPLE_PHI
2300 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2302 else
2303 *handled = true;
2304 return NULL_TREE;
2307 /* Find stmts that must be both vectorized and SLPed. */
2309 void
2310 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2312 unsigned int i;
2313 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2314 slp_instance instance;
2316 if (dump_enabled_p ())
2317 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
2318 "\n");
2320 /* First walk all pattern stmt in the loop and mark defs of uses as
2321 hybrid because immediate uses in them are not recorded. */
2322 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2324 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2325 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2326 gsi_next (&gsi))
2328 gimple *stmt = gsi_stmt (gsi);
2329 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2330 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2332 walk_stmt_info wi;
2333 memset (&wi, 0, sizeof (wi));
2334 wi.info = LOOP_VINFO_LOOP (loop_vinfo);
2335 gimple_stmt_iterator gsi2
2336 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2337 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2338 vect_detect_hybrid_slp_1, &wi);
2339 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2340 vect_detect_hybrid_slp_2,
2341 vect_detect_hybrid_slp_1, &wi);
2346 /* Then walk the SLP instance trees marking stmts with uses in
2347 non-SLP stmts as hybrid, also propagating hybrid down the
2348 SLP tree, collecting the above info on-the-fly. */
2349 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2351 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2352 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2353 i, pure_slp);
2358 /* Create and initialize a new bb_vec_info struct for BB, as well as
2359 stmt_vec_info structs for all the stmts in it. */
2361 static bb_vec_info
2362 new_bb_vec_info (gimple_stmt_iterator region_begin,
2363 gimple_stmt_iterator region_end)
2365 basic_block bb = gsi_bb (region_begin);
2366 bb_vec_info res = NULL;
2367 gimple_stmt_iterator gsi;
2369 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
2370 res->kind = vec_info::bb;
2371 BB_VINFO_BB (res) = bb;
2372 res->region_begin = region_begin;
2373 res->region_end = region_end;
2375 for (gsi = region_begin; gsi_stmt (gsi) != gsi_stmt (region_end);
2376 gsi_next (&gsi))
2378 gimple *stmt = gsi_stmt (gsi);
2379 gimple_set_uid (stmt, 0);
2380 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res));
2383 BB_VINFO_GROUPED_STORES (res).create (10);
2384 BB_VINFO_SLP_INSTANCES (res).create (2);
2385 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
2387 bb->aux = res;
2388 return res;
2392 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2393 stmts in the basic block. */
2395 static void
2396 destroy_bb_vec_info (bb_vec_info bb_vinfo)
2398 slp_instance instance;
2399 unsigned i;
2401 if (!bb_vinfo)
2402 return;
2404 vect_destroy_datarefs (bb_vinfo);
2405 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
2406 BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
2407 FOR_EACH_VEC_ELT (BB_VINFO_SLP_INSTANCES (bb_vinfo), i, instance)
2408 vect_free_slp_instance (instance);
2409 BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
2410 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
2412 for (gimple_stmt_iterator si = bb_vinfo->region_begin;
2413 gsi_stmt (si) != gsi_stmt (bb_vinfo->region_end); gsi_next (&si))
2415 gimple *stmt = gsi_stmt (si);
2416 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2418 if (stmt_info)
2419 /* Free stmt_vec_info. */
2420 free_stmt_vec_info (stmt);
2422 /* Reset region marker. */
2423 gimple_set_uid (stmt, -1);
2426 BB_VINFO_BB (bb_vinfo)->aux = NULL;
2427 free (bb_vinfo);
2431 /* Analyze statements contained in SLP tree node after recursively analyzing
2432 the subtree. Return TRUE if the operations are supported. */
2434 static bool
2435 vect_slp_analyze_node_operations (slp_tree node)
2437 bool dummy;
2438 int i, j;
2439 gimple *stmt;
2440 slp_tree child;
2442 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
2443 return true;
2445 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2446 if (!vect_slp_analyze_node_operations (child))
2447 return false;
2449 bool res = true;
2450 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2452 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2453 gcc_assert (stmt_info);
2454 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2456 /* Push SLP node def-type to stmt operands. */
2457 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2458 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
2459 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[i]))
2460 = SLP_TREE_DEF_TYPE (child);
2461 res = vect_analyze_stmt (stmt, &dummy, node);
2462 /* Restore def-types. */
2463 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2464 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
2465 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (child)[i]))
2466 = vect_internal_def;
2467 if (! res)
2468 break;
2471 return res;
2475 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2476 operations are supported. */
2478 bool
2479 vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data)
2481 slp_instance instance;
2482 int i;
2484 if (dump_enabled_p ())
2485 dump_printf_loc (MSG_NOTE, vect_location,
2486 "=== vect_slp_analyze_operations ===\n");
2488 for (i = 0; slp_instances.iterate (i, &instance); )
2490 if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance)))
2492 dump_printf_loc (MSG_NOTE, vect_location,
2493 "removing SLP instance operations starting from: ");
2494 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2495 SLP_TREE_SCALAR_STMTS
2496 (SLP_INSTANCE_TREE (instance))[0], 0);
2497 vect_free_slp_instance (instance);
2498 slp_instances.ordered_remove (i);
2500 else
2502 /* Compute the costs of the SLP instance. */
2503 vect_analyze_slp_cost (instance, data);
2504 i++;
2508 if (!slp_instances.length ())
2509 return false;
2511 return true;
2515 /* Compute the scalar cost of the SLP node NODE and its children
2516 and return it. Do not account defs that are marked in LIFE and
2517 update LIFE according to uses of NODE. */
2519 static unsigned
2520 vect_bb_slp_scalar_cost (basic_block bb,
2521 slp_tree node, vec<bool, va_heap> *life)
2523 unsigned scalar_cost = 0;
2524 unsigned i;
2525 gimple *stmt;
2526 slp_tree child;
2528 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2530 unsigned stmt_cost;
2531 ssa_op_iter op_iter;
2532 def_operand_p def_p;
2533 stmt_vec_info stmt_info;
2535 if ((*life)[i])
2536 continue;
2538 /* If there is a non-vectorized use of the defs then the scalar
2539 stmt is kept live in which case we do not account it or any
2540 required defs in the SLP children in the scalar cost. This
2541 way we make the vectorization more costly when compared to
2542 the scalar cost. */
2543 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2545 imm_use_iterator use_iter;
2546 gimple *use_stmt;
2547 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
2548 if (!is_gimple_debug (use_stmt)
2549 && (! vect_stmt_in_region_p (vinfo_for_stmt (stmt)->vinfo,
2550 use_stmt)
2551 || ! PURE_SLP_STMT (vinfo_for_stmt (use_stmt))))
2553 (*life)[i] = true;
2554 BREAK_FROM_IMM_USE_STMT (use_iter);
2557 if ((*life)[i])
2558 continue;
2560 /* Count scalar stmts only once. */
2561 if (gimple_visited_p (stmt))
2562 continue;
2563 gimple_set_visited (stmt, true);
2565 stmt_info = vinfo_for_stmt (stmt);
2566 if (STMT_VINFO_DATA_REF (stmt_info))
2568 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2569 stmt_cost = vect_get_stmt_cost (scalar_load);
2570 else
2571 stmt_cost = vect_get_stmt_cost (scalar_store);
2573 else
2574 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2576 scalar_cost += stmt_cost;
2579 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2580 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
2581 scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
2583 return scalar_cost;
2586 /* Check if vectorization of the basic block is profitable. */
2588 static bool
2589 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2591 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2592 slp_instance instance;
2593 int i;
2594 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2595 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2597 /* Calculate scalar cost. */
2598 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2600 auto_vec<bool, 20> life;
2601 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
2602 scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2603 SLP_INSTANCE_TREE (instance),
2604 &life);
2607 /* Unset visited flag. */
2608 for (gimple_stmt_iterator gsi = bb_vinfo->region_begin;
2609 gsi_stmt (gsi) != gsi_stmt (bb_vinfo->region_end); gsi_next (&gsi))
2610 gimple_set_visited (gsi_stmt (gsi), false);
2612 /* Complete the target-specific cost calculation. */
2613 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2614 &vec_inside_cost, &vec_epilogue_cost);
2616 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2618 if (dump_enabled_p ())
2620 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2621 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2622 vec_inside_cost);
2623 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2624 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2625 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
2628 /* Vectorization is profitable if its cost is more than the cost of scalar
2629 version. Note that we err on the vector side for equal cost because
2630 the cost estimate is otherwise quite pessimistic (constant uses are
2631 free on the scalar side but cost a load on the vector side for
2632 example). */
2633 if (vec_outside_cost + vec_inside_cost > scalar_cost)
2634 return false;
2636 return true;
2639 /* Check if the basic block can be vectorized. Returns a bb_vec_info
2640 if so and sets fatal to true if failure is independent of
2641 current_vector_size. */
2643 static bb_vec_info
2644 vect_slp_analyze_bb_1 (gimple_stmt_iterator region_begin,
2645 gimple_stmt_iterator region_end,
2646 vec<data_reference_p> datarefs, int n_stmts,
2647 bool &fatal)
2649 bb_vec_info bb_vinfo;
2650 slp_instance instance;
2651 int i;
2652 int min_vf = 2;
2654 /* The first group of checks is independent of the vector size. */
2655 fatal = true;
2657 if (n_stmts > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2659 if (dump_enabled_p ())
2660 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2661 "not vectorized: too many instructions in "
2662 "basic block.\n");
2663 free_data_refs (datarefs);
2664 return NULL;
2667 bb_vinfo = new_bb_vec_info (region_begin, region_end);
2668 if (!bb_vinfo)
2669 return NULL;
2671 BB_VINFO_DATAREFS (bb_vinfo) = datarefs;
2673 /* Analyze the data references. */
2675 if (!vect_analyze_data_refs (bb_vinfo, &min_vf))
2677 if (dump_enabled_p ())
2678 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2679 "not vectorized: unhandled data-ref in basic "
2680 "block.\n");
2682 destroy_bb_vec_info (bb_vinfo);
2683 return NULL;
2686 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2688 if (dump_enabled_p ())
2689 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2690 "not vectorized: not enough data-refs in "
2691 "basic block.\n");
2693 destroy_bb_vec_info (bb_vinfo);
2694 return NULL;
2697 if (!vect_analyze_data_ref_accesses (bb_vinfo))
2699 if (dump_enabled_p ())
2700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2701 "not vectorized: unhandled data access in "
2702 "basic block.\n");
2704 destroy_bb_vec_info (bb_vinfo);
2705 return NULL;
2708 /* If there are no grouped stores in the region there is no need
2709 to continue with pattern recog as vect_analyze_slp will fail
2710 anyway. */
2711 if (bb_vinfo->grouped_stores.is_empty ())
2713 if (dump_enabled_p ())
2714 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2715 "not vectorized: no grouped stores in "
2716 "basic block.\n");
2718 destroy_bb_vec_info (bb_vinfo);
2719 return NULL;
2722 /* While the rest of the analysis below depends on it in some way. */
2723 fatal = false;
2725 vect_pattern_recog (bb_vinfo);
2727 /* Check the SLP opportunities in the basic block, analyze and build SLP
2728 trees. */
2729 if (!vect_analyze_slp (bb_vinfo, n_stmts))
2731 if (dump_enabled_p ())
2733 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2734 "Failed to SLP the basic block.\n");
2735 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2736 "not vectorized: failed to find SLP opportunities "
2737 "in basic block.\n");
2740 destroy_bb_vec_info (bb_vinfo);
2741 return NULL;
2744 /* Analyze and verify the alignment of data references and the
2745 dependence in the SLP instances. */
2746 for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); )
2748 if (! vect_slp_analyze_and_verify_instance_alignment (instance)
2749 || ! vect_slp_analyze_instance_dependence (instance))
2751 dump_printf_loc (MSG_NOTE, vect_location,
2752 "removing SLP instance operations starting from: ");
2753 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2754 SLP_TREE_SCALAR_STMTS
2755 (SLP_INSTANCE_TREE (instance))[0], 0);
2756 vect_free_slp_instance (instance);
2757 BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
2758 continue;
2761 /* Mark all the statements that we want to vectorize as pure SLP and
2762 relevant. */
2763 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2764 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2766 i++;
2768 if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
2770 destroy_bb_vec_info (bb_vinfo);
2771 return NULL;
2774 if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo),
2775 BB_VINFO_TARGET_COST_DATA (bb_vinfo)))
2777 if (dump_enabled_p ())
2778 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2779 "not vectorized: bad operation in basic block.\n");
2781 destroy_bb_vec_info (bb_vinfo);
2782 return NULL;
2785 /* Cost model: check if the vectorization is worthwhile. */
2786 if (!unlimited_cost_model (NULL)
2787 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2789 if (dump_enabled_p ())
2790 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2791 "not vectorized: vectorization is not "
2792 "profitable.\n");
2794 destroy_bb_vec_info (bb_vinfo);
2795 return NULL;
2798 if (dump_enabled_p ())
2799 dump_printf_loc (MSG_NOTE, vect_location,
2800 "Basic block will be vectorized using SLP\n");
2802 return bb_vinfo;
2806 /* Main entry for the BB vectorizer. Analyze and transform BB, returns
2807 true if anything in the basic-block was vectorized. */
2809 bool
2810 vect_slp_bb (basic_block bb)
2812 bb_vec_info bb_vinfo;
2813 gimple_stmt_iterator gsi;
2814 unsigned int vector_sizes;
2815 bool any_vectorized = false;
2817 if (dump_enabled_p ())
2818 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2820 /* Autodetect first vector size we try. */
2821 current_vector_size = 0;
2822 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2824 gsi = gsi_start_bb (bb);
2826 while (1)
2828 if (gsi_end_p (gsi))
2829 break;
2831 gimple_stmt_iterator region_begin = gsi;
2832 vec<data_reference_p> datarefs = vNULL;
2833 int insns = 0;
2835 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2837 gimple *stmt = gsi_stmt (gsi);
2838 if (is_gimple_debug (stmt))
2839 continue;
2840 insns++;
2842 if (gimple_location (stmt) != UNKNOWN_LOCATION)
2843 vect_location = gimple_location (stmt);
2845 if (!find_data_references_in_stmt (NULL, stmt, &datarefs))
2846 break;
2849 /* Skip leading unhandled stmts. */
2850 if (gsi_stmt (region_begin) == gsi_stmt (gsi))
2852 gsi_next (&gsi);
2853 continue;
2856 gimple_stmt_iterator region_end = gsi;
2858 bool vectorized = false;
2859 bool fatal = false;
2860 bb_vinfo = vect_slp_analyze_bb_1 (region_begin, region_end,
2861 datarefs, insns, fatal);
2862 if (bb_vinfo
2863 && dbg_cnt (vect_slp))
2865 if (dump_enabled_p ())
2866 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB part\n");
2868 vect_schedule_slp (bb_vinfo);
2870 if (dump_enabled_p ())
2871 dump_printf_loc (MSG_NOTE, vect_location,
2872 "basic block part vectorized\n");
2874 destroy_bb_vec_info (bb_vinfo);
2876 vectorized = true;
2878 else
2879 destroy_bb_vec_info (bb_vinfo);
2881 any_vectorized |= vectorized;
2883 vector_sizes &= ~current_vector_size;
2884 if (vectorized
2885 || vector_sizes == 0
2886 || current_vector_size == 0
2887 /* If vect_slp_analyze_bb_1 signaled that analysis for all
2888 vector sizes will fail do not bother iterating. */
2889 || fatal)
2891 if (gsi_end_p (region_end))
2892 break;
2894 /* Skip the unhandled stmt. */
2895 gsi_next (&gsi);
2897 /* And reset vector sizes. */
2898 current_vector_size = 0;
2899 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2901 else
2903 /* Try the next biggest vector size. */
2904 current_vector_size = 1 << floor_log2 (vector_sizes);
2905 if (dump_enabled_p ())
2906 dump_printf_loc (MSG_NOTE, vect_location,
2907 "***** Re-trying analysis with "
2908 "vector size %d\n", current_vector_size);
2910 /* Start over. */
2911 gsi = region_begin;
2915 return any_vectorized;
2919 /* Return 1 if vector type of boolean constant which is OPNUM
2920 operand in statement STMT is a boolean vector. */
2922 static bool
2923 vect_mask_constant_operand_p (gimple *stmt, int opnum)
2925 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2926 enum tree_code code = gimple_expr_code (stmt);
2927 tree op, vectype;
2928 gimple *def_stmt;
2929 enum vect_def_type dt;
2931 /* For comparison and COND_EXPR type is chosen depending
2932 on the other comparison operand. */
2933 if (TREE_CODE_CLASS (code) == tcc_comparison)
2935 if (opnum)
2936 op = gimple_assign_rhs1 (stmt);
2937 else
2938 op = gimple_assign_rhs2 (stmt);
2940 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
2941 &dt, &vectype))
2942 gcc_unreachable ();
2944 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
2947 if (code == COND_EXPR)
2949 tree cond = gimple_assign_rhs1 (stmt);
2951 if (TREE_CODE (cond) == SSA_NAME)
2952 op = cond;
2953 else if (opnum)
2954 op = TREE_OPERAND (cond, 1);
2955 else
2956 op = TREE_OPERAND (cond, 0);
2958 if (!vect_is_simple_use (op, stmt_vinfo->vinfo, &def_stmt,
2959 &dt, &vectype))
2960 gcc_unreachable ();
2962 return !vectype || VECTOR_BOOLEAN_TYPE_P (vectype);
2965 return VECTOR_BOOLEAN_TYPE_P (STMT_VINFO_VECTYPE (stmt_vinfo));
2969 /* For constant and loop invariant defs of SLP_NODE this function returns
2970 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2971 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2972 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2973 REDUC_INDEX is the index of the reduction operand in the statements, unless
2974 it is -1. */
2976 static void
2977 vect_get_constant_vectors (tree op, slp_tree slp_node,
2978 vec<tree> *vec_oprnds,
2979 unsigned int op_num, unsigned int number_of_vectors)
2981 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2982 gimple *stmt = stmts[0];
2983 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2984 unsigned nunits;
2985 tree vec_cst;
2986 tree *elts;
2987 unsigned j, number_of_places_left_in_vector;
2988 tree vector_type;
2989 tree vop;
2990 int group_size = stmts.length ();
2991 unsigned int vec_num, i;
2992 unsigned number_of_copies = 1;
2993 vec<tree> voprnds;
2994 voprnds.create (number_of_vectors);
2995 bool constant_p, is_store;
2996 tree neutral_op = NULL;
2997 enum tree_code code = gimple_expr_code (stmt);
2998 gimple_seq ctor_seq = NULL;
3000 /* Check if vector type is a boolean vector. */
3001 if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
3002 && vect_mask_constant_operand_p (stmt, op_num))
3003 vector_type
3004 = build_same_sized_truth_vector_type (STMT_VINFO_VECTYPE (stmt_vinfo));
3005 else
3006 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
3007 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
3009 if (STMT_VINFO_DATA_REF (stmt_vinfo))
3011 is_store = true;
3012 op = gimple_assign_rhs1 (stmt);
3014 else
3015 is_store = false;
3017 gcc_assert (op);
3019 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
3020 created vectors. It is greater than 1 if unrolling is performed.
3022 For example, we have two scalar operands, s1 and s2 (e.g., group of
3023 strided accesses of size two), while NUNITS is four (i.e., four scalars
3024 of this type can be packed in a vector). The output vector will contain
3025 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
3026 will be 2).
3028 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
3029 containing the operands.
3031 For example, NUNITS is four as before, and the group size is 8
3032 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
3033 {s5, s6, s7, s8}. */
3035 number_of_copies = nunits * number_of_vectors / group_size;
3037 number_of_places_left_in_vector = nunits;
3038 constant_p = true;
3039 elts = XALLOCAVEC (tree, nunits);
3040 bool place_after_defs = false;
3041 for (j = 0; j < number_of_copies; j++)
3043 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
3045 if (is_store)
3046 op = gimple_assign_rhs1 (stmt);
3047 else
3049 switch (code)
3051 case COND_EXPR:
3053 tree cond = gimple_assign_rhs1 (stmt);
3054 if (TREE_CODE (cond) == SSA_NAME)
3055 op = gimple_op (stmt, op_num + 1);
3056 else if (op_num == 0 || op_num == 1)
3057 op = TREE_OPERAND (cond, op_num);
3058 else
3060 if (op_num == 2)
3061 op = gimple_assign_rhs2 (stmt);
3062 else
3063 op = gimple_assign_rhs3 (stmt);
3066 break;
3068 case CALL_EXPR:
3069 op = gimple_call_arg (stmt, op_num);
3070 break;
3072 case LSHIFT_EXPR:
3073 case RSHIFT_EXPR:
3074 case LROTATE_EXPR:
3075 case RROTATE_EXPR:
3076 op = gimple_op (stmt, op_num + 1);
3077 /* Unlike the other binary operators, shifts/rotates have
3078 the shift count being int, instead of the same type as
3079 the lhs, so make sure the scalar is the right type if
3080 we are dealing with vectors of
3081 long long/long/short/char. */
3082 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
3083 op = fold_convert (TREE_TYPE (vector_type), op);
3084 break;
3086 default:
3087 op = gimple_op (stmt, op_num + 1);
3088 break;
3092 /* Create 'vect_ = {op0,op1,...,opn}'. */
3093 number_of_places_left_in_vector--;
3094 tree orig_op = op;
3095 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
3097 if (CONSTANT_CLASS_P (op))
3099 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3101 /* Can't use VIEW_CONVERT_EXPR for booleans because
3102 of possibly different sizes of scalar value and
3103 vector element. */
3104 if (integer_zerop (op))
3105 op = build_int_cst (TREE_TYPE (vector_type), 0);
3106 else if (integer_onep (op))
3107 op = build_all_ones_cst (TREE_TYPE (vector_type));
3108 else
3109 gcc_unreachable ();
3111 else
3112 op = fold_unary (VIEW_CONVERT_EXPR,
3113 TREE_TYPE (vector_type), op);
3114 gcc_assert (op && CONSTANT_CLASS_P (op));
3116 else
3118 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
3119 gimple *init_stmt;
3120 if (VECTOR_BOOLEAN_TYPE_P (vector_type))
3122 tree true_val
3123 = build_all_ones_cst (TREE_TYPE (vector_type));
3124 tree false_val
3125 = build_zero_cst (TREE_TYPE (vector_type));
3126 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
3127 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
3128 op, true_val,
3129 false_val);
3131 else
3133 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type),
3134 op);
3135 init_stmt
3136 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR,
3137 op);
3139 gimple_seq_add_stmt (&ctor_seq, init_stmt);
3140 op = new_temp;
3143 elts[number_of_places_left_in_vector] = op;
3144 if (!CONSTANT_CLASS_P (op))
3145 constant_p = false;
3146 if (TREE_CODE (orig_op) == SSA_NAME
3147 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
3148 && STMT_VINFO_BB_VINFO (stmt_vinfo)
3149 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
3150 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
3151 place_after_defs = true;
3153 if (number_of_places_left_in_vector == 0)
3155 if (constant_p)
3156 vec_cst = build_vector (vector_type, elts);
3157 else
3159 vec<constructor_elt, va_gc> *v;
3160 unsigned k;
3161 vec_alloc (v, nunits);
3162 for (k = 0; k < nunits; ++k)
3163 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
3164 vec_cst = build_constructor (vector_type, v);
3166 tree init;
3167 gimple_stmt_iterator gsi;
3168 if (place_after_defs)
3170 gsi = gsi_for_stmt
3171 (vect_find_last_scalar_stmt_in_slp (slp_node));
3172 init = vect_init_vector (stmt, vec_cst, vector_type, &gsi);
3174 else
3175 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
3176 if (ctor_seq != NULL)
3178 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
3179 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
3180 GSI_SAME_STMT);
3181 ctor_seq = NULL;
3183 voprnds.quick_push (init);
3184 place_after_defs = false;
3185 number_of_places_left_in_vector = nunits;
3186 constant_p = true;
3191 /* Since the vectors are created in the reverse order, we should invert
3192 them. */
3193 vec_num = voprnds.length ();
3194 for (j = vec_num; j != 0; j--)
3196 vop = voprnds[j - 1];
3197 vec_oprnds->quick_push (vop);
3200 voprnds.release ();
3202 /* In case that VF is greater than the unrolling factor needed for the SLP
3203 group of stmts, NUMBER_OF_VECTORS to be created is greater than
3204 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
3205 to replicate the vectors. */
3206 while (number_of_vectors > vec_oprnds->length ())
3208 tree neutral_vec = NULL;
3210 if (neutral_op)
3212 if (!neutral_vec)
3213 neutral_vec = build_vector_from_val (vector_type, neutral_op);
3215 vec_oprnds->quick_push (neutral_vec);
3217 else
3219 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
3220 vec_oprnds->quick_push (vop);
3226 /* Get vectorized definitions from SLP_NODE that contains corresponding
3227 vectorized def-stmts. */
3229 static void
3230 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
3232 tree vec_oprnd;
3233 gimple *vec_def_stmt;
3234 unsigned int i;
3236 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
3238 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
3240 gcc_assert (vec_def_stmt);
3241 if (gimple_code (vec_def_stmt) == GIMPLE_PHI)
3242 vec_oprnd = gimple_phi_result (vec_def_stmt);
3243 else
3244 vec_oprnd = gimple_get_lhs (vec_def_stmt);
3245 vec_oprnds->quick_push (vec_oprnd);
3250 /* Get vectorized definitions for SLP_NODE.
3251 If the scalar definitions are loop invariants or constants, collect them and
3252 call vect_get_constant_vectors() to create vector stmts.
3253 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
3254 must be stored in the corresponding child of SLP_NODE, and we call
3255 vect_get_slp_vect_defs () to retrieve them. */
3257 void
3258 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
3259 vec<vec<tree> > *vec_oprnds)
3261 gimple *first_stmt;
3262 int number_of_vects = 0, i;
3263 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
3264 slp_tree child = NULL;
3265 vec<tree> vec_defs;
3266 tree oprnd;
3267 bool first_iteration = true;
3269 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
3270 FOR_EACH_VEC_ELT (ops, i, oprnd)
3272 bool vectorized_defs = false;
3274 if (oprnd == NULL)
3276 vec_defs = vNULL;
3277 vec_defs.create (0);
3278 vec_oprnds->quick_push (vec_defs);
3279 continue;
3282 /* For each operand we check if it has vectorized definitions in a child
3283 node or we need to create them (for invariants and constants). We
3284 check if the LHS of the first stmt of the next child matches OPRND.
3285 If it does, we found the correct child. Otherwise, we call
3286 vect_get_constant_vectors (). */
3287 for (unsigned int child_index = 0;
3288 child_index < SLP_TREE_CHILDREN (slp_node).length (); child_index++)
3290 child = SLP_TREE_CHILDREN (slp_node)[child_index];
3292 /* We have to check both pattern and original def, if available. */
3293 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
3295 gimple *first_def = SLP_TREE_SCALAR_STMTS (child)[0];
3296 gimple *related
3297 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
3298 tree first_def_op;
3300 if (gimple_code (first_def) == GIMPLE_PHI)
3301 first_def_op = gimple_phi_result (first_def);
3302 else
3303 first_def_op = gimple_get_lhs (first_def);
3304 if (operand_equal_p (oprnd, first_def_op, 0)
3305 || (related
3306 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
3308 /* The number of vector defs is determined by the number of
3309 vector statements in the node from which we get those
3310 statements. */
3311 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
3312 vectorized_defs = true;
3313 break;
3318 if (!vectorized_defs && first_iteration)
3320 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
3321 /* Number of vector stmts was calculated according to LHS in
3322 vect_schedule_slp_instance (), fix it by replacing LHS with
3323 RHS, if necessary. See vect_get_smallest_scalar_type () for
3324 details. */
3325 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
3326 &rhs_size_unit);
3327 if (rhs_size_unit != lhs_size_unit)
3329 number_of_vects *= rhs_size_unit;
3330 number_of_vects /= lhs_size_unit;
3334 /* Allocate memory for vectorized defs. */
3335 vec_defs = vNULL;
3336 vec_defs.create (number_of_vects);
3338 /* For reduction defs we call vect_get_constant_vectors (), since we are
3339 looking for initial loop invariant values. */
3340 if (vectorized_defs)
3341 /* The defs are already vectorized. */
3342 vect_get_slp_vect_defs (child, &vec_defs);
3343 else
3344 /* Build vectors from scalar defs. */
3345 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
3346 number_of_vects);
3348 vec_oprnds->quick_push (vec_defs);
3350 first_iteration = false;
3354 /* Generate vector permute statements from a list of loads in DR_CHAIN.
3355 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
3356 permute statements for the SLP node NODE of the SLP instance
3357 SLP_NODE_INSTANCE. */
3359 bool
3360 vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
3361 gimple_stmt_iterator *gsi, int vf,
3362 slp_instance slp_node_instance, bool analyze_only,
3363 unsigned *n_perms)
3365 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3366 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3367 tree mask_element_type = NULL_TREE, mask_type;
3368 int nunits, vec_index = 0;
3369 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3370 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
3371 int mask_element;
3372 unsigned char *mask;
3373 machine_mode mode;
3375 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3376 return false;
3378 stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info));
3380 mode = TYPE_MODE (vectype);
3382 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3383 same size as the vector element being permuted. */
3384 mask_element_type = lang_hooks.types.type_for_mode
3385 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
3386 mask_type = get_vectype_for_scalar_type (mask_element_type);
3387 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3388 mask = XALLOCAVEC (unsigned char, nunits);
3390 /* Initialize the vect stmts of NODE to properly insert the generated
3391 stmts later. */
3392 if (! analyze_only)
3393 for (unsigned i = SLP_TREE_VEC_STMTS (node).length ();
3394 i < SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3395 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
3397 /* Generate permutation masks for every NODE. Number of masks for each NODE
3398 is equal to GROUP_SIZE.
3399 E.g., we have a group of three nodes with three loads from the same
3400 location in each node, and the vector size is 4. I.e., we have a
3401 a0b0c0a1b1c1... sequence and we need to create the following vectors:
3402 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3403 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3406 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
3407 The last mask is illegal since we assume two operands for permute
3408 operation, and the mask element values can't be outside that range.
3409 Hence, the last mask must be converted into {2,5,5,5}.
3410 For the first two permutations we need the first and the second input
3411 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
3412 we need the second and the third vectors: {b1,c1,a2,b2} and
3413 {c2,a3,b3,c3}. */
3415 int vect_stmts_counter = 0;
3416 int index = 0;
3417 int first_vec_index = -1;
3418 int second_vec_index = -1;
3419 bool noop_p = true;
3420 *n_perms = 0;
3422 for (int j = 0; j < vf; j++)
3424 for (int k = 0; k < group_size; k++)
3426 int i = (SLP_TREE_LOAD_PERMUTATION (node)[k]
3427 + j * STMT_VINFO_GROUP_SIZE (stmt_info));
3428 vec_index = i / nunits;
3429 mask_element = i % nunits;
3430 if (vec_index == first_vec_index
3431 || first_vec_index == -1)
3433 first_vec_index = vec_index;
3435 else if (vec_index == second_vec_index
3436 || second_vec_index == -1)
3438 second_vec_index = vec_index;
3439 mask_element += nunits;
3441 else
3443 if (dump_enabled_p ())
3445 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3446 "permutation requires at "
3447 "least three vectors ");
3448 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3449 stmt, 0);
3451 return false;
3454 gcc_assert (mask_element >= 0
3455 && mask_element < 2 * nunits);
3456 if (mask_element != index)
3457 noop_p = false;
3458 mask[index++] = mask_element;
3460 if (index == nunits)
3462 if (! noop_p
3463 && ! can_vec_perm_p (mode, false, mask))
3465 if (dump_enabled_p ())
3467 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3468 vect_location,
3469 "unsupported vect permute { ");
3470 for (i = 0; i < nunits; ++i)
3471 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ", mask[i]);
3472 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3474 return false;
3477 if (! noop_p)
3478 ++*n_perms;
3480 if (!analyze_only)
3482 tree mask_vec = NULL_TREE;
3484 if (! noop_p)
3486 tree *mask_elts = XALLOCAVEC (tree, nunits);
3487 for (int l = 0; l < nunits; ++l)
3488 mask_elts[l] = build_int_cst (mask_element_type,
3489 mask[l]);
3490 mask_vec = build_vector (mask_type, mask_elts);
3493 if (second_vec_index == -1)
3494 second_vec_index = first_vec_index;
3496 /* Generate the permute statement if necessary. */
3497 tree first_vec = dr_chain[first_vec_index];
3498 tree second_vec = dr_chain[second_vec_index];
3499 gimple *perm_stmt;
3500 if (! noop_p)
3502 tree perm_dest
3503 = vect_create_destination_var (gimple_assign_lhs (stmt),
3504 vectype);
3505 perm_dest = make_ssa_name (perm_dest);
3506 perm_stmt = gimple_build_assign (perm_dest,
3507 VEC_PERM_EXPR,
3508 first_vec, second_vec,
3509 mask_vec);
3510 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
3512 else
3513 /* If mask was NULL_TREE generate the requested
3514 identity transform. */
3515 perm_stmt = SSA_NAME_DEF_STMT (first_vec);
3517 /* Store the vector statement in NODE. */
3518 SLP_TREE_VEC_STMTS (node)[vect_stmts_counter++] = perm_stmt;
3521 index = 0;
3522 first_vec_index = -1;
3523 second_vec_index = -1;
3524 noop_p = true;
3529 return true;
3534 /* Vectorize SLP instance tree in postorder. */
3536 static bool
3537 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3538 unsigned int vectorization_factor)
3540 gimple *stmt;
3541 bool grouped_store, is_store;
3542 gimple_stmt_iterator si;
3543 stmt_vec_info stmt_info;
3544 unsigned int vec_stmts_size, nunits, group_size;
3545 tree vectype;
3546 int i, j;
3547 slp_tree child;
3549 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3550 return false;
3552 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3553 vect_schedule_slp_instance (child, instance, vectorization_factor);
3555 /* Push SLP node def-type to stmts. */
3556 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3557 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
3558 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
3559 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = SLP_TREE_DEF_TYPE (child);
3561 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3562 stmt_info = vinfo_for_stmt (stmt);
3564 /* VECTYPE is the type of the destination. */
3565 vectype = STMT_VINFO_VECTYPE (stmt_info);
3566 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3567 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3569 /* For each SLP instance calculate number of vector stmts to be created
3570 for the scalar stmts in each node of the SLP tree. Number of vector
3571 elements in one vector iteration is the number of scalar elements in
3572 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3573 size.
3574 Unless this is a SLP reduction in which case the number of vector
3575 stmts is equal to the number of vector stmts of the children. */
3576 if (GROUP_FIRST_ELEMENT (stmt_info)
3577 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
3578 vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
3579 else
3580 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3582 if (!SLP_TREE_VEC_STMTS (node).exists ())
3584 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3585 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3588 if (dump_enabled_p ())
3590 dump_printf_loc (MSG_NOTE,vect_location,
3591 "------>vectorizing SLP node starting from: ");
3592 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3595 /* Vectorized stmts go before the last scalar stmt which is where
3596 all uses are ready. */
3597 si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node));
3599 /* Mark the first element of the reduction chain as reduction to properly
3600 transform the node. In the analysis phase only the last element of the
3601 chain is marked as reduction. */
3602 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3603 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3605 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3606 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3609 /* Handle two-operation SLP nodes by vectorizing the group with
3610 both operations and then performing a merge. */
3611 if (SLP_TREE_TWO_OPERATORS (node))
3613 enum tree_code code0 = gimple_assign_rhs_code (stmt);
3614 enum tree_code ocode = ERROR_MARK;
3615 gimple *ostmt;
3616 unsigned char *mask = XALLOCAVEC (unsigned char, group_size);
3617 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt)
3618 if (gimple_assign_rhs_code (ostmt) != code0)
3620 mask[i] = 1;
3621 ocode = gimple_assign_rhs_code (ostmt);
3623 else
3624 mask[i] = 0;
3625 if (ocode != ERROR_MARK)
3627 vec<gimple *> v0;
3628 vec<gimple *> v1;
3629 unsigned j;
3630 tree tmask = NULL_TREE;
3631 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3632 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3633 SLP_TREE_VEC_STMTS (node).truncate (0);
3634 gimple_assign_set_rhs_code (stmt, ocode);
3635 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3636 gimple_assign_set_rhs_code (stmt, code0);
3637 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3638 SLP_TREE_VEC_STMTS (node).truncate (0);
3639 tree meltype = build_nonstandard_integer_type
3640 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1);
3641 tree mvectype = get_same_sized_vectype (meltype, vectype);
3642 unsigned k = 0, l;
3643 for (j = 0; j < v0.length (); ++j)
3645 tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype));
3646 for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l)
3648 if (k >= group_size)
3649 k = 0;
3650 melts[l] = build_int_cst
3651 (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l);
3653 tmask = build_vector (mvectype, melts);
3655 /* ??? Not all targets support a VEC_PERM_EXPR with a
3656 constant mask that would translate to a vec_merge RTX
3657 (with their vec_perm_const_ok). We can either not
3658 vectorize in that case or let veclower do its job.
3659 Unfortunately that isn't too great and at least for
3660 plus/minus we'd eventually like to match targets
3661 vector addsub instructions. */
3662 gimple *vstmt;
3663 vstmt = gimple_build_assign (make_ssa_name (vectype),
3664 VEC_PERM_EXPR,
3665 gimple_assign_lhs (v0[j]),
3666 gimple_assign_lhs (v1[j]), tmask);
3667 vect_finish_stmt_generation (stmt, vstmt, &si);
3668 SLP_TREE_VEC_STMTS (node).quick_push (vstmt);
3670 v0.release ();
3671 v1.release ();
3672 return false;
3675 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3677 /* Restore stmt def-types. */
3678 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3679 if (SLP_TREE_DEF_TYPE (child) != vect_internal_def)
3680 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (child), j, stmt)
3681 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_internal_def;
3683 return is_store;
3686 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3687 For loop vectorization this is done in vectorizable_call, but for SLP
3688 it needs to be deferred until end of vect_schedule_slp, because multiple
3689 SLP instances may refer to the same scalar stmt. */
3691 static void
3692 vect_remove_slp_scalar_calls (slp_tree node)
3694 gimple *stmt, *new_stmt;
3695 gimple_stmt_iterator gsi;
3696 int i;
3697 slp_tree child;
3698 tree lhs;
3699 stmt_vec_info stmt_info;
3701 if (SLP_TREE_DEF_TYPE (node) != vect_internal_def)
3702 return;
3704 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3705 vect_remove_slp_scalar_calls (child);
3707 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3709 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3710 continue;
3711 stmt_info = vinfo_for_stmt (stmt);
3712 if (stmt_info == NULL
3713 || is_pattern_stmt_p (stmt_info)
3714 || !PURE_SLP_STMT (stmt_info))
3715 continue;
3716 lhs = gimple_call_lhs (stmt);
3717 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3718 set_vinfo_for_stmt (new_stmt, stmt_info);
3719 set_vinfo_for_stmt (stmt, NULL);
3720 STMT_VINFO_STMT (stmt_info) = new_stmt;
3721 gsi = gsi_for_stmt (stmt);
3722 gsi_replace (&gsi, new_stmt, false);
3723 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3727 /* Generate vector code for all SLP instances in the loop/basic block. */
3729 bool
3730 vect_schedule_slp (vec_info *vinfo)
3732 vec<slp_instance> slp_instances;
3733 slp_instance instance;
3734 unsigned int i, vf;
3735 bool is_store = false;
3737 slp_instances = vinfo->slp_instances;
3738 if (is_a <loop_vec_info> (vinfo))
3739 vf = as_a <loop_vec_info> (vinfo)->vectorization_factor;
3740 else
3741 vf = 1;
3743 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3745 /* Schedule the tree of INSTANCE. */
3746 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3747 instance, vf);
3748 if (dump_enabled_p ())
3749 dump_printf_loc (MSG_NOTE, vect_location,
3750 "vectorizing stmts using SLP.\n");
3753 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3755 slp_tree root = SLP_INSTANCE_TREE (instance);
3756 gimple *store;
3757 unsigned int j;
3758 gimple_stmt_iterator gsi;
3760 /* Remove scalar call stmts. Do not do this for basic-block
3761 vectorization as not all uses may be vectorized.
3762 ??? Why should this be necessary? DCE should be able to
3763 remove the stmts itself.
3764 ??? For BB vectorization we can as well remove scalar
3765 stmts starting from the SLP tree root if they have no
3766 uses. */
3767 if (is_a <loop_vec_info> (vinfo))
3768 vect_remove_slp_scalar_calls (root);
3770 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3771 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3773 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3774 break;
3776 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3777 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3778 /* Free the attached stmt_vec_info and remove the stmt. */
3779 gsi = gsi_for_stmt (store);
3780 unlink_stmt_vdef (store);
3781 gsi_remove (&gsi, true);
3782 release_defs (store);
3783 free_stmt_vec_info (store);
3787 return is_store;