Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / tree-vect-slp.c
blob880b245c92713bb69e4d7fe748cb57912335df78
1 /* SLP - Basic Block Vectorization
2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "target.h"
34 #include "predict.h"
35 #include "hard-reg-set.h"
36 #include "function.h"
37 #include "basic-block.h"
38 #include "gimple-pretty-print.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimple-ssa.h"
46 #include "tree-phinodes.h"
47 #include "ssa-iterators.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
50 #include "tree-pass.h"
51 #include "cfgloop.h"
52 #include "rtl.h"
53 #include "flags.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "recog.h" /* FIXME: for insn_data */
64 #include "insn-codes.h"
65 #include "optabs.h"
66 #include "tree-vectorizer.h"
67 #include "langhooks.h"
68 #include "gimple-walk.h"
70 /* Extract the location of the basic block in the source code.
71 Return the basic block location if succeed and NULL if not. */
73 source_location
74 find_bb_location (basic_block bb)
76 gimple stmt = NULL;
77 gimple_stmt_iterator si;
79 if (!bb)
80 return UNKNOWN_LOCATION;
82 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
84 stmt = gsi_stmt (si);
85 if (gimple_location (stmt) != UNKNOWN_LOCATION)
86 return gimple_location (stmt);
89 return UNKNOWN_LOCATION;
93 /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
95 static void
96 vect_free_slp_tree (slp_tree node)
98 int i;
99 slp_tree child;
101 if (!node)
102 return;
104 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
105 vect_free_slp_tree (child);
107 SLP_TREE_CHILDREN (node).release ();
108 SLP_TREE_SCALAR_STMTS (node).release ();
109 SLP_TREE_VEC_STMTS (node).release ();
110 SLP_TREE_LOAD_PERMUTATION (node).release ();
112 free (node);
116 /* Free the memory allocated for the SLP instance. */
118 void
119 vect_free_slp_instance (slp_instance instance)
121 vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
122 SLP_INSTANCE_LOADS (instance).release ();
123 free (instance);
127 /* Create an SLP node for SCALAR_STMTS. */
129 static slp_tree
130 vect_create_new_slp_node (vec<gimple> scalar_stmts)
132 slp_tree node;
133 gimple stmt = scalar_stmts[0];
134 unsigned int nops;
136 if (is_gimple_call (stmt))
137 nops = gimple_call_num_args (stmt);
138 else if (is_gimple_assign (stmt))
140 nops = gimple_num_ops (stmt) - 1;
141 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
142 nops++;
144 else
145 return NULL;
147 node = XNEW (struct _slp_tree);
148 SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
149 SLP_TREE_VEC_STMTS (node).create (0);
150 SLP_TREE_CHILDREN (node).create (nops);
151 SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
152 SLP_TREE_TWO_OPERATORS (node) = false;
154 return node;
158 /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
159 operand. */
160 static vec<slp_oprnd_info>
161 vect_create_oprnd_info (int nops, int group_size)
163 int i;
164 slp_oprnd_info oprnd_info;
165 vec<slp_oprnd_info> oprnds_info;
167 oprnds_info.create (nops);
168 for (i = 0; i < nops; i++)
170 oprnd_info = XNEW (struct _slp_oprnd_info);
171 oprnd_info->def_stmts.create (group_size);
172 oprnd_info->first_dt = vect_uninitialized_def;
173 oprnd_info->first_op_type = NULL_TREE;
174 oprnd_info->first_pattern = false;
175 oprnd_info->second_pattern = false;
176 oprnds_info.quick_push (oprnd_info);
179 return oprnds_info;
183 /* Free operands info. */
185 static void
186 vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
188 int i;
189 slp_oprnd_info oprnd_info;
191 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
193 oprnd_info->def_stmts.release ();
194 XDELETE (oprnd_info);
197 oprnds_info.release ();
201 /* Find the place of the data-ref in STMT in the interleaving chain that starts
202 from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
204 static int
205 vect_get_place_in_interleaving_chain (gimple stmt, gimple first_stmt)
207 gimple next_stmt = first_stmt;
208 int result = 0;
210 if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
211 return -1;
215 if (next_stmt == stmt)
216 return result;
217 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
218 if (next_stmt)
219 result += GROUP_GAP (vinfo_for_stmt (next_stmt));
221 while (next_stmt);
223 return -1;
227 /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
228 they are of a valid type and that they match the defs of the first stmt of
229 the SLP group (stored in OPRNDS_INFO). If there was a fatal error
230 return -1, if the error could be corrected by swapping operands of the
231 operation return 1, if everything is ok return 0. */
233 static int
234 vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
235 gimple stmt, unsigned stmt_num,
236 vec<slp_oprnd_info> *oprnds_info)
238 tree oprnd;
239 unsigned int i, number_of_oprnds;
240 tree def;
241 gimple def_stmt;
242 enum vect_def_type dt = vect_uninitialized_def;
243 struct loop *loop = NULL;
244 bool pattern = false;
245 slp_oprnd_info oprnd_info;
246 int first_op_idx = 1;
247 bool commutative = false;
248 bool first_op_cond = false;
249 bool first = stmt_num == 0;
250 bool second = stmt_num == 1;
252 if (loop_vinfo)
253 loop = LOOP_VINFO_LOOP (loop_vinfo);
255 if (is_gimple_call (stmt))
257 number_of_oprnds = gimple_call_num_args (stmt);
258 first_op_idx = 3;
260 else if (is_gimple_assign (stmt))
262 enum tree_code code = gimple_assign_rhs_code (stmt);
263 number_of_oprnds = gimple_num_ops (stmt) - 1;
264 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
266 first_op_cond = true;
267 commutative = true;
268 number_of_oprnds++;
270 else
271 commutative = commutative_tree_code (code);
273 else
274 return -1;
276 bool swapped = false;
277 for (i = 0; i < number_of_oprnds; i++)
279 again:
280 if (first_op_cond)
282 if (i == 0 || i == 1)
283 oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx),
284 swapped ? !i : i);
285 else
286 oprnd = gimple_op (stmt, first_op_idx + i - 1);
288 else
289 oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i));
291 oprnd_info = (*oprnds_info)[i];
293 if (!vect_is_simple_use (oprnd, NULL, loop_vinfo, bb_vinfo, &def_stmt,
294 &def, &dt))
296 if (dump_enabled_p ())
298 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
299 "Build SLP failed: can't analyze def for ");
300 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
301 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
304 return -1;
307 /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
308 from the pattern. Check that all the stmts of the node are in the
309 pattern. */
310 if (def_stmt && gimple_bb (def_stmt)
311 && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
312 || (!loop && gimple_bb (def_stmt) == BB_VINFO_BB (bb_vinfo)
313 && gimple_code (def_stmt) != GIMPLE_PHI))
314 && vinfo_for_stmt (def_stmt)
315 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
316 && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
317 && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
319 pattern = true;
320 if (!first && !oprnd_info->first_pattern
321 /* Allow different pattern state for the defs of the
322 first stmt in reduction chains. */
323 && (oprnd_info->first_dt != vect_reduction_def
324 || (!second && !oprnd_info->second_pattern)))
326 if (i == 0
327 && !swapped
328 && commutative)
330 swapped = true;
331 goto again;
334 if (dump_enabled_p ())
336 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
337 "Build SLP failed: some of the stmts"
338 " are in a pattern, and others are not ");
339 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
340 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
343 return 1;
346 def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
347 dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
349 if (dt == vect_unknown_def_type)
351 if (dump_enabled_p ())
352 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
353 "Unsupported pattern.\n");
354 return -1;
357 switch (gimple_code (def_stmt))
359 case GIMPLE_PHI:
360 def = gimple_phi_result (def_stmt);
361 break;
363 case GIMPLE_ASSIGN:
364 def = gimple_assign_lhs (def_stmt);
365 break;
367 default:
368 if (dump_enabled_p ())
369 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
370 "unsupported defining stmt:\n");
371 return -1;
375 if (second)
376 oprnd_info->second_pattern = pattern;
378 if (first)
380 oprnd_info->first_dt = dt;
381 oprnd_info->first_pattern = pattern;
382 oprnd_info->first_op_type = TREE_TYPE (oprnd);
384 else
386 /* Not first stmt of the group, check that the def-stmt/s match
387 the def-stmt/s of the first stmt. Allow different definition
388 types for reduction chains: the first stmt must be a
389 vect_reduction_def (a phi node), and the rest
390 vect_internal_def. */
391 if (((oprnd_info->first_dt != dt
392 && !(oprnd_info->first_dt == vect_reduction_def
393 && dt == vect_internal_def)
394 && !((oprnd_info->first_dt == vect_external_def
395 || oprnd_info->first_dt == vect_constant_def)
396 && (dt == vect_external_def
397 || dt == vect_constant_def)))
398 || !types_compatible_p (oprnd_info->first_op_type,
399 TREE_TYPE (oprnd))))
401 /* Try swapping operands if we got a mismatch. */
402 if (i == 0
403 && !swapped
404 && commutative)
406 swapped = true;
407 goto again;
410 if (dump_enabled_p ())
411 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
412 "Build SLP failed: different types\n");
414 return 1;
418 /* Check the types of the definitions. */
419 switch (dt)
421 case vect_constant_def:
422 case vect_external_def:
423 case vect_reduction_def:
424 break;
426 case vect_internal_def:
427 oprnd_info->def_stmts.quick_push (def_stmt);
428 break;
430 default:
431 /* FORNOW: Not supported. */
432 if (dump_enabled_p ())
434 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
435 "Build SLP failed: illegal type of def ");
436 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, def);
437 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
440 return -1;
444 /* Swap operands. */
445 if (swapped)
447 if (first_op_cond)
449 tree cond = gimple_assign_rhs1 (stmt);
450 swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
451 &TREE_OPERAND (cond, 1));
452 TREE_SET_CODE (cond, swap_tree_comparison (TREE_CODE (cond)));
454 else
455 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
456 gimple_assign_rhs2_ptr (stmt));
459 return 0;
463 /* Verify if the scalar stmts STMTS are isomorphic, require data
464 permutation or are of unsupported types of operation. Return
465 true if they are, otherwise return false and indicate in *MATCHES
466 which stmts are not isomorphic to the first one. If MATCHES[0]
467 is false then this indicates the comparison could not be
468 carried out or the stmts will never be vectorized by SLP. */
470 static bool
471 vect_build_slp_tree_1 (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
472 vec<gimple> stmts, unsigned int group_size,
473 unsigned nops, unsigned int *max_nunits,
474 unsigned int vectorization_factor, bool *matches,
475 bool *two_operators)
477 unsigned int i;
478 gimple first_stmt = stmts[0], stmt = stmts[0];
479 enum tree_code first_stmt_code = ERROR_MARK;
480 enum tree_code alt_stmt_code = ERROR_MARK;
481 enum tree_code rhs_code = ERROR_MARK;
482 enum tree_code first_cond_code = ERROR_MARK;
483 tree lhs;
484 bool need_same_oprnds = false;
485 tree vectype, scalar_type, first_op1 = NULL_TREE;
486 optab optab;
487 int icode;
488 machine_mode optab_op2_mode;
489 machine_mode vec_mode;
490 struct data_reference *first_dr;
491 HOST_WIDE_INT dummy;
492 gimple first_load = NULL, prev_first_load = NULL, old_first_load = NULL;
493 tree cond;
495 /* For every stmt in NODE find its def stmt/s. */
496 FOR_EACH_VEC_ELT (stmts, i, stmt)
498 matches[i] = false;
500 if (dump_enabled_p ())
502 dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
503 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
504 dump_printf (MSG_NOTE, "\n");
507 /* Fail to vectorize statements marked as unvectorizable. */
508 if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
510 if (dump_enabled_p ())
512 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
513 "Build SLP failed: unvectorizable statement ");
514 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
515 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
517 /* Fatal mismatch. */
518 matches[0] = false;
519 return false;
522 lhs = gimple_get_lhs (stmt);
523 if (lhs == NULL_TREE)
525 if (dump_enabled_p ())
527 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
528 "Build SLP failed: not GIMPLE_ASSIGN nor "
529 "GIMPLE_CALL ");
530 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
531 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
533 /* Fatal mismatch. */
534 matches[0] = false;
535 return false;
538 if (is_gimple_assign (stmt)
539 && gimple_assign_rhs_code (stmt) == COND_EXPR
540 && (cond = gimple_assign_rhs1 (stmt))
541 && !COMPARISON_CLASS_P (cond))
543 if (dump_enabled_p ())
545 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
546 "Build SLP failed: condition is not "
547 "comparison ");
548 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
549 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
551 /* Fatal mismatch. */
552 matches[0] = false;
553 return false;
556 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
557 vectype = get_vectype_for_scalar_type (scalar_type);
558 if (!vectype)
560 if (dump_enabled_p ())
562 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
563 "Build SLP failed: unsupported data-type ");
564 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
565 scalar_type);
566 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
568 /* Fatal mismatch. */
569 matches[0] = false;
570 return false;
573 /* If populating the vector type requires unrolling then fail
574 before adjusting *max_nunits for basic-block vectorization. */
575 if (bb_vinfo
576 && TYPE_VECTOR_SUBPARTS (vectype) > group_size)
578 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
579 "Build SLP failed: unrolling required "
580 "in basic block SLP\n");
581 /* Fatal mismatch. */
582 matches[0] = false;
583 return false;
586 /* In case of multiple types we need to detect the smallest type. */
587 if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
589 *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
590 if (bb_vinfo)
591 vectorization_factor = *max_nunits;
594 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
596 rhs_code = CALL_EXPR;
597 if (gimple_call_internal_p (call_stmt)
598 || gimple_call_tail_p (call_stmt)
599 || gimple_call_noreturn_p (call_stmt)
600 || !gimple_call_nothrow_p (call_stmt)
601 || gimple_call_chain (call_stmt))
603 if (dump_enabled_p ())
605 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
606 "Build SLP failed: unsupported call type ");
607 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
608 call_stmt, 0);
609 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
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);
725 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
727 /* Mismatch. */
728 continue;
731 if (rhs_code == CALL_EXPR)
733 gimple first_stmt = stmts[0];
734 if (gimple_call_num_args (stmt) != nops
735 || !operand_equal_p (gimple_call_fn (first_stmt),
736 gimple_call_fn (stmt), 0)
737 || gimple_call_fntype (first_stmt)
738 != gimple_call_fntype (stmt))
740 if (dump_enabled_p ())
742 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
743 "Build SLP failed: different calls in ");
744 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
745 stmt, 0);
746 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
748 /* Mismatch. */
749 continue;
754 /* Grouped store or load. */
755 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
757 if (REFERENCE_CLASS_P (lhs))
759 /* Store. */
762 else
764 /* Load. */
765 /* Check that the size of interleaved loads group is not
766 greater than the SLP group size. */
767 unsigned ncopies
768 = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
769 if (loop_vinfo
770 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
771 && ((GROUP_SIZE (vinfo_for_stmt (stmt))
772 - GROUP_GAP (vinfo_for_stmt (stmt)))
773 > ncopies * group_size))
775 if (dump_enabled_p ())
777 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
778 "Build SLP failed: the number "
779 "of interleaved loads is greater than "
780 "the SLP group size ");
781 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
782 stmt, 0);
783 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
785 /* Fatal mismatch. */
786 matches[0] = false;
787 return false;
790 old_first_load = first_load;
791 first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
792 if (prev_first_load)
794 /* Check that there are no loads from different interleaving
795 chains in the same node. */
796 if (prev_first_load != first_load)
798 if (dump_enabled_p ())
800 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
801 vect_location,
802 "Build SLP failed: different "
803 "interleaving chains in one node ");
804 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
805 stmt, 0);
806 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
808 /* Mismatch. */
809 continue;
812 else
813 prev_first_load = first_load;
815 /* In some cases a group of loads is just the same load
816 repeated N times. Only analyze its cost once. */
817 if (first_load == stmt && old_first_load != first_load)
819 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
820 if (vect_supportable_dr_alignment (first_dr, false)
821 == dr_unaligned_unsupported)
823 if (dump_enabled_p ())
825 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
826 vect_location,
827 "Build SLP failed: unsupported "
828 "unaligned load ");
829 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
830 stmt, 0);
831 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
833 /* Fatal mismatch. */
834 matches[0] = false;
835 return false;
839 } /* Grouped access. */
840 else
842 if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
844 /* Not grouped load. */
845 if (dump_enabled_p ())
847 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
848 "Build SLP failed: not grouped load ");
849 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
850 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
853 /* FORNOW: Not grouped loads are not supported. */
854 /* Fatal mismatch. */
855 matches[0] = false;
856 return false;
859 /* Not memory operation. */
860 if (TREE_CODE_CLASS (rhs_code) != tcc_binary
861 && TREE_CODE_CLASS (rhs_code) != tcc_unary
862 && TREE_CODE_CLASS (rhs_code) != tcc_expression
863 && rhs_code != CALL_EXPR)
865 if (dump_enabled_p ())
867 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
868 "Build SLP failed: operation");
869 dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
870 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
871 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
873 /* Fatal mismatch. */
874 matches[0] = false;
875 return false;
878 if (rhs_code == COND_EXPR)
880 tree cond_expr = gimple_assign_rhs1 (stmt);
882 if (i == 0)
883 first_cond_code = TREE_CODE (cond_expr);
884 else if (first_cond_code != TREE_CODE (cond_expr))
886 if (dump_enabled_p ())
888 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
889 "Build SLP failed: different"
890 " operation");
891 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
892 stmt, 0);
893 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
895 /* Mismatch. */
896 continue;
901 matches[i] = true;
904 for (i = 0; i < group_size; ++i)
905 if (!matches[i])
906 return false;
908 /* If we allowed a two-operation SLP node verify the target can cope
909 with the permute we are going to use. */
910 if (alt_stmt_code != ERROR_MARK
911 && TREE_CODE_CLASS (alt_stmt_code) != tcc_reference)
913 unsigned char *sel
914 = XALLOCAVEC (unsigned char, TYPE_VECTOR_SUBPARTS (vectype));
915 for (i = 0; i < TYPE_VECTOR_SUBPARTS (vectype); ++i)
917 sel[i] = i;
918 if (gimple_assign_rhs_code (stmts[i % group_size]) == alt_stmt_code)
919 sel[i] += TYPE_VECTOR_SUBPARTS (vectype);
921 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
923 for (i = 0; i < group_size; ++i)
924 if (gimple_assign_rhs_code (stmts[i]) == alt_stmt_code)
926 matches[i] = false;
927 if (dump_enabled_p ())
929 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
930 "Build SLP failed: different operation "
931 "in stmt ");
932 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
933 stmts[i], 0);
934 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
935 "original stmt ");
936 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
937 first_stmt, 0);
940 return false;
942 *two_operators = true;
945 return true;
948 /* Recursively build an SLP tree starting from NODE.
949 Fail (and return a value not equal to zero) if def-stmts are not
950 isomorphic, require data permutation or are of unsupported types of
951 operation. Otherwise, return 0.
952 The value returned is the depth in the SLP tree where a mismatch
953 was found. */
955 static bool
956 vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
957 slp_tree *node, unsigned int group_size,
958 unsigned int *max_nunits,
959 vec<slp_tree> *loads,
960 unsigned int vectorization_factor,
961 bool *matches, unsigned *npermutes, unsigned *tree_size,
962 unsigned max_tree_size)
964 unsigned nops, i, this_tree_size = 0;
965 gimple stmt;
967 matches[0] = false;
969 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
970 if (is_gimple_call (stmt))
971 nops = gimple_call_num_args (stmt);
972 else if (is_gimple_assign (stmt))
974 nops = gimple_num_ops (stmt) - 1;
975 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
976 nops++;
978 else
979 return false;
981 bool two_operators = false;
982 if (!vect_build_slp_tree_1 (loop_vinfo, bb_vinfo,
983 SLP_TREE_SCALAR_STMTS (*node), group_size, nops,
984 max_nunits, vectorization_factor, matches,
985 &two_operators))
986 return false;
987 SLP_TREE_TWO_OPERATORS (*node) = two_operators;
989 /* If the SLP node is a load, terminate the recursion. */
990 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
991 && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
993 loads->safe_push (*node);
994 return true;
997 /* Get at the operands, verifying they are compatible. */
998 vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
999 slp_oprnd_info oprnd_info;
1000 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (*node), i, stmt)
1002 switch (vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo,
1003 stmt, i, &oprnds_info))
1005 case 0:
1006 break;
1007 case -1:
1008 matches[0] = false;
1009 vect_free_oprnd_info (oprnds_info);
1010 return false;
1011 case 1:
1012 matches[i] = false;
1013 break;
1016 for (i = 0; i < group_size; ++i)
1017 if (!matches[i])
1019 vect_free_oprnd_info (oprnds_info);
1020 return false;
1023 stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
1025 /* Create SLP_TREE nodes for the definition node/s. */
1026 FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
1028 slp_tree child;
1029 unsigned old_nloads = loads->length ();
1030 unsigned old_max_nunits = *max_nunits;
1032 if (oprnd_info->first_dt != vect_internal_def)
1033 continue;
1035 if (++this_tree_size > max_tree_size)
1037 vect_free_oprnd_info (oprnds_info);
1038 return false;
1041 child = vect_create_new_slp_node (oprnd_info->def_stmts);
1042 if (!child)
1044 vect_free_oprnd_info (oprnds_info);
1045 return false;
1048 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
1049 group_size, max_nunits, loads,
1050 vectorization_factor, matches,
1051 npermutes, &this_tree_size, max_tree_size))
1053 /* If we have all children of child built up from scalars then just
1054 throw that away and build it up this node from scalars. */
1055 if (!SLP_TREE_CHILDREN (child).is_empty ())
1057 unsigned int j;
1058 slp_tree grandchild;
1060 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1061 if (grandchild != NULL)
1062 break;
1063 if (!grandchild)
1065 /* Roll back. */
1066 *max_nunits = old_max_nunits;
1067 loads->truncate (old_nloads);
1068 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1069 vect_free_slp_tree (grandchild);
1070 SLP_TREE_CHILDREN (child).truncate (0);
1072 dump_printf_loc (MSG_NOTE, vect_location,
1073 "Building parent vector operands from "
1074 "scalars instead\n");
1075 oprnd_info->def_stmts = vNULL;
1076 vect_free_slp_tree (child);
1077 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1078 continue;
1082 oprnd_info->def_stmts = vNULL;
1083 SLP_TREE_CHILDREN (*node).quick_push (child);
1084 continue;
1087 /* If the SLP build failed fatally and we analyze a basic-block
1088 simply treat nodes we fail to build as externally defined
1089 (and thus build vectors from the scalar defs).
1090 The cost model will reject outright expensive cases.
1091 ??? This doesn't treat cases where permutation ultimatively
1092 fails (or we don't try permutation below). Ideally we'd
1093 even compute a permutation that will end up with the maximum
1094 SLP tree size... */
1095 if (bb_vinfo
1096 && !matches[0]
1097 /* ??? Rejecting patterns this way doesn't work. We'd have to
1098 do extra work to cancel the pattern so the uses see the
1099 scalar version. */
1100 && !is_pattern_stmt_p (vinfo_for_stmt (stmt)))
1102 unsigned int j;
1103 slp_tree grandchild;
1105 /* Roll back. */
1106 *max_nunits = old_max_nunits;
1107 loads->truncate (old_nloads);
1108 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1109 vect_free_slp_tree (grandchild);
1110 SLP_TREE_CHILDREN (child).truncate (0);
1112 dump_printf_loc (MSG_NOTE, vect_location,
1113 "Building vector operands from scalars\n");
1114 oprnd_info->def_stmts = vNULL;
1115 vect_free_slp_tree (child);
1116 SLP_TREE_CHILDREN (*node).quick_push (NULL);
1117 continue;
1120 /* If the SLP build for operand zero failed and operand zero
1121 and one can be commutated try that for the scalar stmts
1122 that failed the match. */
1123 if (i == 0
1124 /* A first scalar stmt mismatch signals a fatal mismatch. */
1125 && matches[0]
1126 /* ??? For COND_EXPRs we can swap the comparison operands
1127 as well as the arms under some constraints. */
1128 && nops == 2
1129 && oprnds_info[1]->first_dt == vect_internal_def
1130 && is_gimple_assign (stmt)
1131 && commutative_tree_code (gimple_assign_rhs_code (stmt))
1132 && !SLP_TREE_TWO_OPERATORS (*node)
1133 /* Do so only if the number of not successful permutes was nor more
1134 than a cut-ff as re-trying the recursive match on
1135 possibly each level of the tree would expose exponential
1136 behavior. */
1137 && *npermutes < 4)
1139 unsigned int j;
1140 slp_tree grandchild;
1142 /* Roll back. */
1143 *max_nunits = old_max_nunits;
1144 loads->truncate (old_nloads);
1145 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
1146 vect_free_slp_tree (grandchild);
1147 SLP_TREE_CHILDREN (child).truncate (0);
1149 /* Swap mismatched definition stmts. */
1150 dump_printf_loc (MSG_NOTE, vect_location,
1151 "Re-trying with swapped operands of stmts ");
1152 for (j = 0; j < group_size; ++j)
1153 if (!matches[j])
1155 gimple tem = oprnds_info[0]->def_stmts[j];
1156 oprnds_info[0]->def_stmts[j] = oprnds_info[1]->def_stmts[j];
1157 oprnds_info[1]->def_stmts[j] = tem;
1158 dump_printf (MSG_NOTE, "%d ", j);
1160 dump_printf (MSG_NOTE, "\n");
1161 /* And try again with scratch 'matches' ... */
1162 bool *tem = XALLOCAVEC (bool, group_size);
1163 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
1164 group_size, max_nunits, loads,
1165 vectorization_factor,
1166 tem, npermutes, &this_tree_size,
1167 max_tree_size))
1169 /* ... so if successful we can apply the operand swapping
1170 to the GIMPLE IL. This is necessary because for example
1171 vect_get_slp_defs uses operand indexes and thus expects
1172 canonical operand order. */
1173 for (j = 0; j < group_size; ++j)
1174 if (!matches[j])
1176 gimple stmt = SLP_TREE_SCALAR_STMTS (*node)[j];
1177 swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
1178 gimple_assign_rhs2_ptr (stmt));
1180 oprnd_info->def_stmts = vNULL;
1181 SLP_TREE_CHILDREN (*node).quick_push (child);
1182 continue;
1185 ++*npermutes;
1188 oprnd_info->def_stmts = vNULL;
1189 vect_free_slp_tree (child);
1190 vect_free_oprnd_info (oprnds_info);
1191 return false;
1194 if (tree_size)
1195 *tree_size += this_tree_size;
1197 vect_free_oprnd_info (oprnds_info);
1198 return true;
1201 /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
1203 static void
1204 vect_print_slp_tree (int dump_kind, slp_tree node)
1206 int i;
1207 gimple stmt;
1208 slp_tree child;
1210 if (!node)
1211 return;
1213 dump_printf (dump_kind, "node ");
1214 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1216 dump_printf (dump_kind, "\n\tstmt %d ", i);
1217 dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
1219 dump_printf (dump_kind, "\n");
1221 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1222 vect_print_slp_tree (dump_kind, child);
1226 /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
1227 If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
1228 J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
1229 stmts in NODE are to be marked. */
1231 static void
1232 vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
1234 int i;
1235 gimple stmt;
1236 slp_tree child;
1238 if (!node)
1239 return;
1241 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1242 if (j < 0 || i == j)
1243 STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
1245 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1246 vect_mark_slp_stmts (child, mark, j);
1250 /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
1252 static void
1253 vect_mark_slp_stmts_relevant (slp_tree node)
1255 int i;
1256 gimple stmt;
1257 stmt_vec_info stmt_info;
1258 slp_tree child;
1260 if (!node)
1261 return;
1263 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1265 stmt_info = vinfo_for_stmt (stmt);
1266 gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
1267 || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
1268 STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
1271 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1272 vect_mark_slp_stmts_relevant (child);
1276 /* Rearrange the statements of NODE according to PERMUTATION. */
1278 static void
1279 vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
1280 vec<unsigned> permutation)
1282 gimple stmt;
1283 vec<gimple> tmp_stmts;
1284 unsigned int i;
1285 slp_tree child;
1287 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1288 vect_slp_rearrange_stmts (child, group_size, permutation);
1290 gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
1291 tmp_stmts.create (group_size);
1292 tmp_stmts.quick_grow_cleared (group_size);
1294 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
1295 tmp_stmts[permutation[i]] = stmt;
1297 SLP_TREE_SCALAR_STMTS (node).release ();
1298 SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
1302 /* Attempt to reorder stmts in a reduction chain so that we don't
1303 require any load permutation. Return true if that was possible,
1304 otherwise return false. */
1306 static bool
1307 vect_attempt_slp_rearrange_stmts (slp_instance slp_instn)
1309 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1310 unsigned int i, j;
1311 sbitmap load_index;
1312 unsigned int lidx;
1313 slp_tree node, load;
1315 /* Compare all the permutation sequences to the first one. We know
1316 that at least one load is permuted. */
1317 node = SLP_INSTANCE_LOADS (slp_instn)[0];
1318 if (!node->load_permutation.exists ())
1319 return false;
1320 for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
1322 if (!load->load_permutation.exists ())
1323 return false;
1324 FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
1325 if (lidx != node->load_permutation[j])
1326 return false;
1329 /* Check that the loads in the first sequence are different and there
1330 are no gaps between them. */
1331 load_index = sbitmap_alloc (group_size);
1332 bitmap_clear (load_index);
1333 FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
1335 if (bitmap_bit_p (load_index, lidx))
1337 sbitmap_free (load_index);
1338 return false;
1340 bitmap_set_bit (load_index, lidx);
1342 for (i = 0; i < group_size; i++)
1343 if (!bitmap_bit_p (load_index, i))
1345 sbitmap_free (load_index);
1346 return false;
1348 sbitmap_free (load_index);
1350 /* This permutation is valid for reduction. Since the order of the
1351 statements in the nodes is not important unless they are memory
1352 accesses, we can rearrange the statements in all the nodes
1353 according to the order of the loads. */
1354 vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
1355 node->load_permutation);
1357 /* We are done, no actual permutations need to be generated. */
1358 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1359 SLP_TREE_LOAD_PERMUTATION (node).release ();
1360 return true;
1363 /* Check if the required load permutations in the SLP instance
1364 SLP_INSTN are supported. */
1366 static bool
1367 vect_supported_load_permutation_p (slp_instance slp_instn)
1369 unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
1370 unsigned int i, j, k, next;
1371 slp_tree node;
1372 gimple stmt, load, next_load, first_load;
1373 struct data_reference *dr;
1375 if (dump_enabled_p ())
1377 dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
1378 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1379 if (node->load_permutation.exists ())
1380 FOR_EACH_VEC_ELT (node->load_permutation, j, next)
1381 dump_printf (MSG_NOTE, "%d ", next);
1382 else
1383 for (k = 0; k < group_size; ++k)
1384 dump_printf (MSG_NOTE, "%d ", k);
1385 dump_printf (MSG_NOTE, "\n");
1388 /* In case of reduction every load permutation is allowed, since the order
1389 of the reduction statements is not important (as opposed to the case of
1390 grouped stores). The only condition we need to check is that all the
1391 load nodes are of the same size and have the same permutation (and then
1392 rearrange all the nodes of the SLP instance according to this
1393 permutation). */
1395 /* Check that all the load nodes are of the same size. */
1396 /* ??? Can't we assert this? */
1397 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1398 if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
1399 return false;
1401 node = SLP_INSTANCE_TREE (slp_instn);
1402 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1404 /* Reduction (there are no data-refs in the root).
1405 In reduction chain the order of the loads is not important. */
1406 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
1407 && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1409 if (vect_attempt_slp_rearrange_stmts (slp_instn))
1410 return true;
1412 /* Fallthru to general load permutation handling. */
1415 /* In basic block vectorization we allow any subchain of an interleaving
1416 chain.
1417 FORNOW: not supported in loop SLP because of realignment compications. */
1418 if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
1420 /* Check whether the loads in an instance form a subchain and thus
1421 no permutation is necessary. */
1422 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1424 if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
1425 continue;
1426 bool subchain_p = true;
1427 next_load = NULL;
1428 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
1430 if (j != 0
1431 && (next_load != load
1432 || GROUP_GAP (vinfo_for_stmt (load)) != 1))
1434 subchain_p = false;
1435 break;
1437 next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
1439 if (subchain_p)
1440 SLP_TREE_LOAD_PERMUTATION (node).release ();
1441 else
1443 /* Verify the permutation can be generated. */
1444 vec<tree> tem;
1445 if (!vect_transform_slp_perm_load (node, tem, NULL,
1446 1, slp_instn, true))
1448 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1449 vect_location,
1450 "unsupported load permutation\n");
1451 return false;
1456 /* Check that the alignment of the first load in every subchain, i.e.,
1457 the first statement in every load node, is supported.
1458 ??? This belongs in alignment checking. */
1459 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1461 first_load = SLP_TREE_SCALAR_STMTS (node)[0];
1462 if (first_load != GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_load)))
1464 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_load));
1465 if (vect_supportable_dr_alignment (dr, false)
1466 == dr_unaligned_unsupported)
1468 if (dump_enabled_p ())
1470 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
1471 vect_location,
1472 "unsupported unaligned load ");
1473 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
1474 first_load, 0);
1475 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1477 return false;
1482 return true;
1485 /* For loop vectorization verify we can generate the permutation. */
1486 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
1487 if (node->load_permutation.exists ()
1488 && !vect_transform_slp_perm_load
1489 (node, vNULL, NULL,
1490 SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true))
1491 return false;
1493 return true;
1497 /* Find the last store in SLP INSTANCE. */
1499 static gimple
1500 vect_find_last_scalar_stmt_in_slp (slp_tree node)
1502 gimple last = NULL, stmt;
1504 for (int i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &stmt); i++)
1506 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1507 if (is_pattern_stmt_p (stmt_vinfo))
1508 last = get_later_stmt (STMT_VINFO_RELATED_STMT (stmt_vinfo), last);
1509 else
1510 last = get_later_stmt (stmt, last);
1513 return last;
1516 /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
1518 static void
1519 vect_analyze_slp_cost_1 (slp_instance instance, slp_tree node,
1520 stmt_vector_for_cost *prologue_cost_vec,
1521 stmt_vector_for_cost *body_cost_vec,
1522 unsigned ncopies_for_cost)
1524 unsigned i;
1525 slp_tree child;
1526 gimple stmt, s;
1527 stmt_vec_info stmt_info;
1528 tree lhs;
1529 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1531 /* Recurse down the SLP tree. */
1532 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
1533 if (child)
1534 vect_analyze_slp_cost_1 (instance, child, prologue_cost_vec,
1535 body_cost_vec, ncopies_for_cost);
1537 /* Look at the first scalar stmt to determine the cost. */
1538 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1539 stmt_info = vinfo_for_stmt (stmt);
1540 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1542 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
1543 vect_model_store_cost (stmt_info, ncopies_for_cost, false,
1544 vect_uninitialized_def,
1545 node, prologue_cost_vec, body_cost_vec);
1546 else
1548 int i;
1549 gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
1550 vect_model_load_cost (stmt_info, ncopies_for_cost, false,
1551 node, prologue_cost_vec, body_cost_vec);
1552 /* If the load is permuted record the cost for the permutation.
1553 ??? Loads from multiple chains are let through here only
1554 for a single special case involving complex numbers where
1555 in the end no permutation is necessary. */
1556 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, s)
1557 if ((STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo_for_stmt (s))
1558 == STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info))
1559 && vect_get_place_in_interleaving_chain
1560 (s, STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info)) != i)
1562 record_stmt_cost (body_cost_vec, group_size, vec_perm,
1563 stmt_info, 0, vect_body);
1564 break;
1568 else
1570 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1571 stmt_info, 0, vect_body);
1572 if (SLP_TREE_TWO_OPERATORS (node))
1574 record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
1575 stmt_info, 0, vect_body);
1576 record_stmt_cost (body_cost_vec, ncopies_for_cost, vec_perm,
1577 stmt_info, 0, vect_body);
1581 /* Scan operands and account for prologue cost of constants/externals.
1582 ??? This over-estimates cost for multiple uses and should be
1583 re-engineered. */
1584 lhs = gimple_get_lhs (stmt);
1585 for (i = 0; i < gimple_num_ops (stmt); ++i)
1587 tree def, op = gimple_op (stmt, i);
1588 gimple def_stmt;
1589 enum vect_def_type dt;
1590 if (!op || op == lhs)
1591 continue;
1592 if (vect_is_simple_use (op, NULL, STMT_VINFO_LOOP_VINFO (stmt_info),
1593 STMT_VINFO_BB_VINFO (stmt_info),
1594 &def_stmt, &def, &dt))
1596 /* Without looking at the actual initializer a vector of
1597 constants can be implemented as load from the constant pool.
1598 ??? We need to pass down stmt_info for a vector type
1599 even if it points to the wrong stmt. */
1600 if (dt == vect_constant_def)
1601 record_stmt_cost (prologue_cost_vec, 1, vector_load,
1602 stmt_info, 0, vect_prologue);
1603 else if (dt == vect_external_def)
1604 record_stmt_cost (prologue_cost_vec, 1, vec_construct,
1605 stmt_info, 0, vect_prologue);
1610 /* Compute the cost for the SLP instance INSTANCE. */
1612 static void
1613 vect_analyze_slp_cost (slp_instance instance, void *data)
1615 stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
1616 unsigned ncopies_for_cost;
1617 stmt_info_for_cost *si;
1618 unsigned i;
1620 /* Calculate the number of vector stmts to create based on the unrolling
1621 factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
1622 GROUP_SIZE / NUNITS otherwise. */
1623 unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
1624 slp_tree node = SLP_INSTANCE_TREE (instance);
1625 stmt_vec_info stmt_info = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
1626 /* Adjust the group_size by the vectorization factor which is always one
1627 for basic-block vectorization. */
1628 if (STMT_VINFO_LOOP_VINFO (stmt_info))
1629 group_size *= LOOP_VINFO_VECT_FACTOR (STMT_VINFO_LOOP_VINFO (stmt_info));
1630 unsigned nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1631 /* For reductions look at a reduction operand in case the reduction
1632 operation is widening like DOT_PROD or SAD. */
1633 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1635 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
1636 switch (gimple_assign_rhs_code (stmt))
1638 case DOT_PROD_EXPR:
1639 case SAD_EXPR:
1640 nunits = TYPE_VECTOR_SUBPARTS (get_vectype_for_scalar_type
1641 (TREE_TYPE (gimple_assign_rhs1 (stmt))));
1642 break;
1643 default:;
1646 ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
1648 prologue_cost_vec.create (10);
1649 body_cost_vec.create (10);
1650 vect_analyze_slp_cost_1 (instance, SLP_INSTANCE_TREE (instance),
1651 &prologue_cost_vec, &body_cost_vec,
1652 ncopies_for_cost);
1654 /* Record the prologue costs, which were delayed until we were
1655 sure that SLP was successful. */
1656 FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
1658 struct _stmt_vec_info *stmt_info
1659 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1660 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1661 si->misalign, vect_prologue);
1664 /* Record the instance's instructions in the target cost model. */
1665 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1667 struct _stmt_vec_info *stmt_info
1668 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1669 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1670 si->misalign, vect_body);
1673 prologue_cost_vec.release ();
1674 body_cost_vec.release ();
1677 /* Analyze an SLP instance starting from a group of grouped stores. Call
1678 vect_build_slp_tree to build a tree of packed stmts if possible.
1679 Return FALSE if it's impossible to SLP any stmt in the loop. */
1681 static bool
1682 vect_analyze_slp_instance (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1683 gimple stmt, unsigned max_tree_size)
1685 slp_instance new_instance;
1686 slp_tree node;
1687 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1688 unsigned int unrolling_factor = 1, nunits;
1689 tree vectype, scalar_type = NULL_TREE;
1690 gimple next;
1691 unsigned int vectorization_factor = 0;
1692 int i;
1693 unsigned int max_nunits = 0;
1694 vec<slp_tree> loads;
1695 struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
1696 vec<gimple> scalar_stmts;
1698 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1700 if (dr)
1702 scalar_type = TREE_TYPE (DR_REF (dr));
1703 vectype = get_vectype_for_scalar_type (scalar_type);
1705 else
1707 gcc_assert (loop_vinfo);
1708 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1711 group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
1713 else
1715 gcc_assert (loop_vinfo);
1716 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1717 group_size = LOOP_VINFO_REDUCTIONS (loop_vinfo).length ();
1720 if (!vectype)
1722 if (dump_enabled_p ())
1724 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1725 "Build SLP failed: unsupported data-type ");
1726 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
1727 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1730 return false;
1733 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1734 if (loop_vinfo)
1735 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1736 else
1737 vectorization_factor = nunits;
1739 /* Calculate the unrolling factor. */
1740 unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
1741 if (unrolling_factor != 1 && !loop_vinfo)
1743 if (dump_enabled_p ())
1744 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1745 "Build SLP failed: unrolling required in basic"
1746 " block SLP\n");
1748 return false;
1751 /* Create a node (a root of the SLP tree) for the packed grouped stores. */
1752 scalar_stmts.create (group_size);
1753 next = stmt;
1754 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
1756 /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
1757 while (next)
1759 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
1760 && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
1761 scalar_stmts.safe_push (
1762 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
1763 else
1764 scalar_stmts.safe_push (next);
1765 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
1767 /* Mark the first element of the reduction chain as reduction to properly
1768 transform the node. In the reduction analysis phase only the last
1769 element of the chain is marked as reduction. */
1770 if (!STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
1771 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) = vect_reduction_def;
1773 else
1775 /* Collect reduction statements. */
1776 vec<gimple> reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1777 for (i = 0; reductions.iterate (i, &next); i++)
1778 scalar_stmts.safe_push (next);
1781 node = vect_create_new_slp_node (scalar_stmts);
1783 loads.create (group_size);
1785 /* Build the tree for the SLP instance. */
1786 bool *matches = XALLOCAVEC (bool, group_size);
1787 unsigned npermutes = 0;
1788 if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &node, group_size,
1789 &max_nunits, &loads,
1790 vectorization_factor, matches, &npermutes, NULL,
1791 max_tree_size))
1793 /* Calculate the unrolling factor based on the smallest type. */
1794 if (max_nunits > nunits)
1795 unrolling_factor = least_common_multiple (max_nunits, group_size)
1796 / group_size;
1798 if (unrolling_factor != 1 && !loop_vinfo)
1800 if (dump_enabled_p ())
1801 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1802 "Build SLP failed: unrolling required in basic"
1803 " block SLP\n");
1804 vect_free_slp_tree (node);
1805 loads.release ();
1806 return false;
1809 /* Create a new SLP instance. */
1810 new_instance = XNEW (struct _slp_instance);
1811 SLP_INSTANCE_TREE (new_instance) = node;
1812 SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
1813 SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
1814 SLP_INSTANCE_LOADS (new_instance) = loads;
1816 /* Compute the load permutation. */
1817 slp_tree load_node;
1818 bool loads_permuted = false;
1819 FOR_EACH_VEC_ELT (loads, i, load_node)
1821 vec<unsigned> load_permutation;
1822 int j;
1823 gimple load, first_stmt;
1824 bool this_load_permuted = false;
1825 load_permutation.create (group_size);
1826 first_stmt = GROUP_FIRST_ELEMENT
1827 (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
1828 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
1830 int load_place
1831 = vect_get_place_in_interleaving_chain (load, first_stmt);
1832 gcc_assert (load_place != -1);
1833 if (load_place != j)
1834 this_load_permuted = true;
1835 load_permutation.safe_push (load_place);
1837 if (!this_load_permuted
1838 /* The load requires permutation when unrolling exposes
1839 a gap either because the group is larger than the SLP
1840 group-size or because there is a gap between the groups. */
1841 && (unrolling_factor == 1
1842 || (group_size == GROUP_SIZE (vinfo_for_stmt (first_stmt))
1843 && GROUP_GAP (vinfo_for_stmt (first_stmt)) == 0)))
1845 load_permutation.release ();
1846 continue;
1848 SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
1849 loads_permuted = true;
1852 if (loads_permuted)
1854 if (!vect_supported_load_permutation_p (new_instance))
1856 if (dump_enabled_p ())
1858 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1859 "Build SLP failed: unsupported load "
1860 "permutation ");
1861 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
1862 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1864 vect_free_slp_instance (new_instance);
1865 return false;
1870 if (loop_vinfo)
1871 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).safe_push (new_instance);
1872 else
1873 BB_VINFO_SLP_INSTANCES (bb_vinfo).safe_push (new_instance);
1875 if (dump_enabled_p ())
1876 vect_print_slp_tree (MSG_NOTE, node);
1878 return true;
1881 /* Failed to SLP. */
1882 /* Free the allocated memory. */
1883 vect_free_slp_tree (node);
1884 loads.release ();
1886 return false;
1890 /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
1891 trees of packed scalar stmts if SLP is possible. */
1893 bool
1894 vect_analyze_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
1895 unsigned max_tree_size)
1897 unsigned int i;
1898 vec<gimple> grouped_stores;
1899 vec<gimple> reductions = vNULL;
1900 vec<gimple> reduc_chains = vNULL;
1901 gimple first_element;
1902 bool ok = false;
1904 if (dump_enabled_p ())
1905 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
1907 if (loop_vinfo)
1909 grouped_stores = LOOP_VINFO_GROUPED_STORES (loop_vinfo);
1910 reduc_chains = LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo);
1911 reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
1913 else
1914 grouped_stores = BB_VINFO_GROUPED_STORES (bb_vinfo);
1916 /* Find SLP sequences starting from groups of grouped stores. */
1917 FOR_EACH_VEC_ELT (grouped_stores, i, first_element)
1918 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
1919 max_tree_size))
1920 ok = true;
1922 if (reduc_chains.length () > 0)
1924 /* Find SLP sequences starting from reduction chains. */
1925 FOR_EACH_VEC_ELT (reduc_chains, i, first_element)
1926 if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
1927 max_tree_size))
1928 ok = true;
1929 else
1930 return false;
1932 /* Don't try to vectorize SLP reductions if reduction chain was
1933 detected. */
1934 return ok;
1937 /* Find SLP sequences starting from groups of reductions. */
1938 if (reductions.length () > 1
1939 && vect_analyze_slp_instance (loop_vinfo, bb_vinfo, reductions[0],
1940 max_tree_size))
1941 ok = true;
1943 return true;
1947 /* For each possible SLP instance decide whether to SLP it and calculate overall
1948 unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
1949 least one instance. */
1951 bool
1952 vect_make_slp_decision (loop_vec_info loop_vinfo)
1954 unsigned int i, unrolling_factor = 1;
1955 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1956 slp_instance instance;
1957 int decided_to_slp = 0;
1959 if (dump_enabled_p ())
1960 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
1961 "\n");
1963 FOR_EACH_VEC_ELT (slp_instances, i, instance)
1965 /* FORNOW: SLP if you can. */
1966 if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
1967 unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
1969 /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
1970 call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
1971 loop-based vectorization. Such stmts will be marked as HYBRID. */
1972 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
1973 decided_to_slp++;
1976 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
1978 if (decided_to_slp && dump_enabled_p ())
1979 dump_printf_loc (MSG_NOTE, vect_location,
1980 "Decided to SLP %d instances. Unrolling factor %d\n",
1981 decided_to_slp, unrolling_factor);
1983 return (decided_to_slp > 0);
1987 /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
1988 can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
1990 static void
1991 vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
1993 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[i];
1994 imm_use_iterator imm_iter;
1995 gimple use_stmt;
1996 stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
1997 slp_tree child;
1998 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1999 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2000 int j;
2002 /* Propagate hybrid down the SLP tree. */
2003 if (stype == hybrid)
2005 else if (HYBRID_SLP_STMT (stmt_vinfo))
2006 stype = hybrid;
2007 else
2009 /* Check if a pure SLP stmt has uses in non-SLP stmts. */
2010 gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
2011 /* We always get the pattern stmt here, but for immediate
2012 uses we have to use the LHS of the original stmt. */
2013 gcc_checking_assert (!STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
2014 if (STMT_VINFO_RELATED_STMT (stmt_vinfo))
2015 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
2016 if (TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2017 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
2019 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2020 continue;
2021 use_vinfo = vinfo_for_stmt (use_stmt);
2022 if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
2023 && STMT_VINFO_RELATED_STMT (use_vinfo))
2024 use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
2025 if (!STMT_SLP_TYPE (use_vinfo)
2026 && (STMT_VINFO_RELEVANT (use_vinfo)
2027 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
2028 && !(gimple_code (use_stmt) == GIMPLE_PHI
2029 && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
2030 stype = hybrid;
2034 if (stype == hybrid)
2036 if (dump_enabled_p ())
2038 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2039 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
2041 STMT_SLP_TYPE (stmt_vinfo) = hybrid;
2044 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
2045 if (child)
2046 vect_detect_hybrid_slp_stmts (child, i, stype);
2049 /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
2051 static tree
2052 vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
2054 walk_stmt_info *wi = (walk_stmt_info *)data;
2055 struct loop *loopp = (struct loop *)wi->info;
2057 if (wi->is_lhs)
2058 return NULL_TREE;
2060 if (TREE_CODE (*tp) == SSA_NAME
2061 && !SSA_NAME_IS_DEFAULT_DEF (*tp))
2063 gimple def_stmt = SSA_NAME_DEF_STMT (*tp);
2064 if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
2065 && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
2067 if (dump_enabled_p ())
2069 dump_printf_loc (MSG_NOTE, vect_location, "marking hybrid: ");
2070 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2072 STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
2076 return NULL_TREE;
2079 static tree
2080 vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
2081 walk_stmt_info *)
2083 /* If the stmt is in a SLP instance then this isn't a reason
2084 to mark use definitions in other SLP instances as hybrid. */
2085 if (STMT_SLP_TYPE (vinfo_for_stmt (gsi_stmt (*gsi))) != loop_vect)
2086 *handled = true;
2087 return NULL_TREE;
2090 /* Find stmts that must be both vectorized and SLPed. */
2092 void
2093 vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
2095 unsigned int i;
2096 vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
2097 slp_instance instance;
2099 if (dump_enabled_p ())
2100 dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
2101 "\n");
2103 /* First walk all pattern stmt in the loop and mark defs of uses as
2104 hybrid because immediate uses in them are not recorded. */
2105 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2107 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2108 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2109 gsi_next (&gsi))
2111 gimple stmt = gsi_stmt (gsi);
2112 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2113 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2115 walk_stmt_info wi;
2116 memset (&wi, 0, sizeof (wi));
2117 wi.info = LOOP_VINFO_LOOP (loop_vinfo);
2118 gimple_stmt_iterator gsi2
2119 = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2120 walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
2121 vect_detect_hybrid_slp_1, &wi);
2122 walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
2123 vect_detect_hybrid_slp_2,
2124 vect_detect_hybrid_slp_1, &wi);
2129 /* Then walk the SLP instance trees marking stmts with uses in
2130 non-SLP stmts as hybrid, also propagating hybrid down the
2131 SLP tree, collecting the above info on-the-fly. */
2132 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2134 for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
2135 vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
2136 i, pure_slp);
2141 /* Create and initialize a new bb_vec_info struct for BB, as well as
2142 stmt_vec_info structs for all the stmts in it. */
2144 static bb_vec_info
2145 new_bb_vec_info (basic_block bb)
2147 bb_vec_info res = NULL;
2148 gimple_stmt_iterator gsi;
2150 res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
2151 BB_VINFO_BB (res) = bb;
2153 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2155 gimple stmt = gsi_stmt (gsi);
2156 gimple_set_uid (stmt, 0);
2157 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, NULL, res));
2160 BB_VINFO_GROUPED_STORES (res).create (10);
2161 BB_VINFO_SLP_INSTANCES (res).create (2);
2162 BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
2164 bb->aux = res;
2165 return res;
2169 /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
2170 stmts in the basic block. */
2172 static void
2173 destroy_bb_vec_info (bb_vec_info bb_vinfo)
2175 vec<slp_instance> slp_instances;
2176 slp_instance instance;
2177 basic_block bb;
2178 gimple_stmt_iterator si;
2179 unsigned i;
2181 if (!bb_vinfo)
2182 return;
2184 bb = BB_VINFO_BB (bb_vinfo);
2186 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2188 gimple stmt = gsi_stmt (si);
2189 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2191 if (stmt_info)
2192 /* Free stmt_vec_info. */
2193 free_stmt_vec_info (stmt);
2196 vect_destroy_datarefs (NULL, bb_vinfo);
2197 free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
2198 BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
2199 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2200 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2201 vect_free_slp_instance (instance);
2202 BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
2203 destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
2204 free (bb_vinfo);
2205 bb->aux = NULL;
2209 /* Analyze statements contained in SLP tree node after recursively analyzing
2210 the subtree. Return TRUE if the operations are supported. */
2212 static bool
2213 vect_slp_analyze_node_operations (slp_tree node)
2215 bool dummy;
2216 int i;
2217 gimple stmt;
2218 slp_tree child;
2220 if (!node)
2221 return true;
2223 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2224 if (!vect_slp_analyze_node_operations (child))
2225 return false;
2227 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2229 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2230 gcc_assert (stmt_info);
2231 gcc_assert (STMT_SLP_TYPE (stmt_info) != loop_vect);
2233 if (!vect_analyze_stmt (stmt, &dummy, node))
2234 return false;
2237 return true;
2241 /* Analyze statements in SLP instances of the basic block. Return TRUE if the
2242 operations are supported. */
2244 bool
2245 vect_slp_analyze_operations (vec<slp_instance> slp_instances, void *data)
2247 slp_instance instance;
2248 int i;
2250 if (dump_enabled_p ())
2251 dump_printf_loc (MSG_NOTE, vect_location,
2252 "=== vect_slp_analyze_operations ===\n");
2254 for (i = 0; slp_instances.iterate (i, &instance); )
2256 if (!vect_slp_analyze_node_operations (SLP_INSTANCE_TREE (instance)))
2258 dump_printf_loc (MSG_NOTE, vect_location,
2259 "removing SLP instance operations starting from: ");
2260 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
2261 SLP_TREE_SCALAR_STMTS
2262 (SLP_INSTANCE_TREE (instance))[0], 0);
2263 vect_free_slp_instance (instance);
2264 slp_instances.ordered_remove (i);
2266 else
2268 /* Compute the costs of the SLP instance. */
2269 vect_analyze_slp_cost (instance, data);
2270 i++;
2274 if (!slp_instances.length ())
2275 return false;
2277 return true;
2281 /* Compute the scalar cost of the SLP node NODE and its children
2282 and return it. Do not account defs that are marked in LIFE and
2283 update LIFE according to uses of NODE. */
2285 static unsigned
2286 vect_bb_slp_scalar_cost (basic_block bb,
2287 slp_tree node, vec<bool, va_heap> *life)
2289 unsigned scalar_cost = 0;
2290 unsigned i;
2291 gimple stmt;
2292 slp_tree child;
2294 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
2296 unsigned stmt_cost;
2297 ssa_op_iter op_iter;
2298 def_operand_p def_p;
2299 stmt_vec_info stmt_info;
2301 if ((*life)[i])
2302 continue;
2304 /* If there is a non-vectorized use of the defs then the scalar
2305 stmt is kept live in which case we do not account it or any
2306 required defs in the SLP children in the scalar cost. This
2307 way we make the vectorization more costly when compared to
2308 the scalar cost. */
2309 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
2311 imm_use_iterator use_iter;
2312 gimple use_stmt;
2313 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
2314 if (!is_gimple_debug (use_stmt)
2315 && (gimple_code (use_stmt) == GIMPLE_PHI
2316 || gimple_bb (use_stmt) != bb
2317 || !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (use_stmt))))
2319 (*life)[i] = true;
2320 BREAK_FROM_IMM_USE_STMT (use_iter);
2323 if ((*life)[i])
2324 continue;
2326 stmt_info = vinfo_for_stmt (stmt);
2327 if (STMT_VINFO_DATA_REF (stmt_info))
2329 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
2330 stmt_cost = vect_get_stmt_cost (scalar_load);
2331 else
2332 stmt_cost = vect_get_stmt_cost (scalar_store);
2334 else
2335 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2337 scalar_cost += stmt_cost;
2340 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
2341 if (child)
2342 scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
2344 return scalar_cost;
2347 /* Check if vectorization of the basic block is profitable. */
2349 static bool
2350 vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
2352 vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2353 slp_instance instance;
2354 int i;
2355 unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
2356 unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
2358 /* Calculate scalar cost. */
2359 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2361 auto_vec<bool, 20> life;
2362 life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
2363 scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
2364 SLP_INSTANCE_TREE (instance),
2365 &life);
2368 /* Complete the target-specific cost calculation. */
2369 finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
2370 &vec_inside_cost, &vec_epilogue_cost);
2372 vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
2374 if (dump_enabled_p ())
2376 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2377 dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
2378 vec_inside_cost);
2379 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
2380 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
2381 dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
2384 /* Vectorization is profitable if its cost is less than the cost of scalar
2385 version. */
2386 if (vec_outside_cost + vec_inside_cost >= scalar_cost)
2387 return false;
2389 return true;
2392 /* Check if the basic block can be vectorized. */
2394 static bb_vec_info
2395 vect_slp_analyze_bb_1 (basic_block bb)
2397 bb_vec_info bb_vinfo;
2398 vec<slp_instance> slp_instances;
2399 slp_instance instance;
2400 int i;
2401 int min_vf = 2;
2402 unsigned n_stmts = 0;
2404 bb_vinfo = new_bb_vec_info (bb);
2405 if (!bb_vinfo)
2406 return NULL;
2408 if (!vect_analyze_data_refs (NULL, bb_vinfo, &min_vf, &n_stmts))
2410 if (dump_enabled_p ())
2411 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2412 "not vectorized: unhandled data-ref in basic "
2413 "block.\n");
2415 destroy_bb_vec_info (bb_vinfo);
2416 return NULL;
2419 if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
2421 if (dump_enabled_p ())
2422 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2423 "not vectorized: not enough data-refs in "
2424 "basic block.\n");
2426 destroy_bb_vec_info (bb_vinfo);
2427 return NULL;
2430 if (!vect_analyze_data_ref_accesses (NULL, bb_vinfo))
2432 if (dump_enabled_p ())
2433 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2434 "not vectorized: unhandled data access in "
2435 "basic block.\n");
2437 destroy_bb_vec_info (bb_vinfo);
2438 return NULL;
2441 vect_pattern_recog (NULL, bb_vinfo);
2443 if (!vect_analyze_data_refs_alignment (NULL, bb_vinfo))
2445 if (dump_enabled_p ())
2446 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2447 "not vectorized: bad data alignment in basic "
2448 "block.\n");
2450 destroy_bb_vec_info (bb_vinfo);
2451 return NULL;
2454 /* Check the SLP opportunities in the basic block, analyze and build SLP
2455 trees. */
2456 if (!vect_analyze_slp (NULL, bb_vinfo, n_stmts))
2458 if (dump_enabled_p ())
2460 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2461 "Failed to SLP the basic block.\n");
2462 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2463 "not vectorized: failed to find SLP opportunities "
2464 "in basic block.\n");
2467 destroy_bb_vec_info (bb_vinfo);
2468 return NULL;
2471 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
2473 /* Mark all the statements that we want to vectorize as pure SLP and
2474 relevant. */
2475 FOR_EACH_VEC_ELT (slp_instances, i, instance)
2477 vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
2478 vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
2481 /* Mark all the statements that we do not want to vectorize. */
2482 for (gimple_stmt_iterator gsi = gsi_start_bb (BB_VINFO_BB (bb_vinfo));
2483 !gsi_end_p (gsi); gsi_next (&gsi))
2485 stmt_vec_info vinfo = vinfo_for_stmt (gsi_stmt (gsi));
2486 if (STMT_SLP_TYPE (vinfo) != pure_slp)
2487 STMT_VINFO_VECTORIZABLE (vinfo) = false;
2490 /* Analyze dependences. At this point all stmts not participating in
2491 vectorization have to be marked. Dependence analysis assumes
2492 that we either vectorize all SLP instances or none at all. */
2493 if (!vect_slp_analyze_data_ref_dependences (bb_vinfo))
2495 if (dump_enabled_p ())
2496 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2497 "not vectorized: unhandled data dependence "
2498 "in basic block.\n");
2500 destroy_bb_vec_info (bb_vinfo);
2501 return NULL;
2504 if (!vect_verify_datarefs_alignment (NULL, bb_vinfo))
2506 if (dump_enabled_p ())
2507 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2508 "not vectorized: unsupported alignment in basic "
2509 "block.\n");
2510 destroy_bb_vec_info (bb_vinfo);
2511 return NULL;
2514 if (!vect_slp_analyze_operations (BB_VINFO_SLP_INSTANCES (bb_vinfo),
2515 BB_VINFO_TARGET_COST_DATA (bb_vinfo)))
2517 if (dump_enabled_p ())
2518 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2519 "not vectorized: bad operation in basic block.\n");
2521 destroy_bb_vec_info (bb_vinfo);
2522 return NULL;
2525 /* Cost model: check if the vectorization is worthwhile. */
2526 if (!unlimited_cost_model (NULL)
2527 && !vect_bb_vectorization_profitable_p (bb_vinfo))
2529 if (dump_enabled_p ())
2530 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2531 "not vectorized: vectorization is not "
2532 "profitable.\n");
2534 destroy_bb_vec_info (bb_vinfo);
2535 return NULL;
2538 if (dump_enabled_p ())
2539 dump_printf_loc (MSG_NOTE, vect_location,
2540 "Basic block will be vectorized using SLP\n");
2542 return bb_vinfo;
2546 bb_vec_info
2547 vect_slp_analyze_bb (basic_block bb)
2549 bb_vec_info bb_vinfo;
2550 int insns = 0;
2551 gimple_stmt_iterator gsi;
2552 unsigned int vector_sizes;
2554 if (dump_enabled_p ())
2555 dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
2557 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2559 gimple stmt = gsi_stmt (gsi);
2560 if (!is_gimple_debug (stmt)
2561 && !gimple_nop_p (stmt)
2562 && gimple_code (stmt) != GIMPLE_LABEL)
2563 insns++;
2566 if (insns > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
2568 if (dump_enabled_p ())
2569 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2570 "not vectorized: too many instructions in "
2571 "basic block.\n");
2573 return NULL;
2576 /* Autodetect first vector size we try. */
2577 current_vector_size = 0;
2578 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2580 while (1)
2582 bb_vinfo = vect_slp_analyze_bb_1 (bb);
2583 if (bb_vinfo)
2584 return bb_vinfo;
2586 destroy_bb_vec_info (bb_vinfo);
2588 vector_sizes &= ~current_vector_size;
2589 if (vector_sizes == 0
2590 || current_vector_size == 0)
2591 return NULL;
2593 /* Try the next biggest vector size. */
2594 current_vector_size = 1 << floor_log2 (vector_sizes);
2595 if (dump_enabled_p ())
2596 dump_printf_loc (MSG_NOTE, vect_location,
2597 "***** Re-trying analysis with "
2598 "vector size %d\n", current_vector_size);
2603 /* For constant and loop invariant defs of SLP_NODE this function returns
2604 (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
2605 OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
2606 scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
2607 REDUC_INDEX is the index of the reduction operand in the statements, unless
2608 it is -1. */
2610 static void
2611 vect_get_constant_vectors (tree op, slp_tree slp_node,
2612 vec<tree> *vec_oprnds,
2613 unsigned int op_num, unsigned int number_of_vectors,
2614 int reduc_index)
2616 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
2617 gimple stmt = stmts[0];
2618 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
2619 unsigned nunits;
2620 tree vec_cst;
2621 tree *elts;
2622 unsigned j, number_of_places_left_in_vector;
2623 tree vector_type;
2624 tree vop;
2625 int group_size = stmts.length ();
2626 unsigned int vec_num, i;
2627 unsigned number_of_copies = 1;
2628 vec<tree> voprnds;
2629 voprnds.create (number_of_vectors);
2630 bool constant_p, is_store;
2631 tree neutral_op = NULL;
2632 enum tree_code code = gimple_expr_code (stmt);
2633 gimple def_stmt;
2634 struct loop *loop;
2635 gimple_seq ctor_seq = NULL;
2637 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
2638 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
2640 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
2641 && reduc_index != -1)
2643 op_num = reduc_index;
2644 op = gimple_op (stmt, op_num + 1);
2645 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
2646 we need either neutral operands or the original operands. See
2647 get_initial_def_for_reduction() for details. */
2648 switch (code)
2650 case WIDEN_SUM_EXPR:
2651 case DOT_PROD_EXPR:
2652 case SAD_EXPR:
2653 case PLUS_EXPR:
2654 case MINUS_EXPR:
2655 case BIT_IOR_EXPR:
2656 case BIT_XOR_EXPR:
2657 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2658 neutral_op = build_real (TREE_TYPE (op), dconst0);
2659 else
2660 neutral_op = build_int_cst (TREE_TYPE (op), 0);
2662 break;
2664 case MULT_EXPR:
2665 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
2666 neutral_op = build_real (TREE_TYPE (op), dconst1);
2667 else
2668 neutral_op = build_int_cst (TREE_TYPE (op), 1);
2670 break;
2672 case BIT_AND_EXPR:
2673 neutral_op = build_int_cst (TREE_TYPE (op), -1);
2674 break;
2676 /* For MIN/MAX we don't have an easy neutral operand but
2677 the initial values can be used fine here. Only for
2678 a reduction chain we have to force a neutral element. */
2679 case MAX_EXPR:
2680 case MIN_EXPR:
2681 if (!GROUP_FIRST_ELEMENT (stmt_vinfo))
2682 neutral_op = NULL;
2683 else
2685 def_stmt = SSA_NAME_DEF_STMT (op);
2686 loop = (gimple_bb (stmt))->loop_father;
2687 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2688 loop_preheader_edge (loop));
2690 break;
2692 default:
2693 gcc_assert (!GROUP_FIRST_ELEMENT (stmt_vinfo));
2694 neutral_op = NULL;
2698 if (STMT_VINFO_DATA_REF (stmt_vinfo))
2700 is_store = true;
2701 op = gimple_assign_rhs1 (stmt);
2703 else
2704 is_store = false;
2706 gcc_assert (op);
2708 if (CONSTANT_CLASS_P (op))
2709 constant_p = true;
2710 else
2711 constant_p = false;
2713 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
2714 created vectors. It is greater than 1 if unrolling is performed.
2716 For example, we have two scalar operands, s1 and s2 (e.g., group of
2717 strided accesses of size two), while NUNITS is four (i.e., four scalars
2718 of this type can be packed in a vector). The output vector will contain
2719 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
2720 will be 2).
2722 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
2723 containing the operands.
2725 For example, NUNITS is four as before, and the group size is 8
2726 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
2727 {s5, s6, s7, s8}. */
2729 number_of_copies = nunits * number_of_vectors / group_size;
2731 number_of_places_left_in_vector = nunits;
2732 elts = XALLOCAVEC (tree, nunits);
2733 bool place_after_defs = false;
2734 for (j = 0; j < number_of_copies; j++)
2736 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
2738 if (is_store)
2739 op = gimple_assign_rhs1 (stmt);
2740 else
2742 switch (code)
2744 case COND_EXPR:
2745 if (op_num == 0 || op_num == 1)
2747 tree cond = gimple_assign_rhs1 (stmt);
2748 op = TREE_OPERAND (cond, op_num);
2750 else
2752 if (op_num == 2)
2753 op = gimple_assign_rhs2 (stmt);
2754 else
2755 op = gimple_assign_rhs3 (stmt);
2757 break;
2759 case CALL_EXPR:
2760 op = gimple_call_arg (stmt, op_num);
2761 break;
2763 case LSHIFT_EXPR:
2764 case RSHIFT_EXPR:
2765 case LROTATE_EXPR:
2766 case RROTATE_EXPR:
2767 op = gimple_op (stmt, op_num + 1);
2768 /* Unlike the other binary operators, shifts/rotates have
2769 the shift count being int, instead of the same type as
2770 the lhs, so make sure the scalar is the right type if
2771 we are dealing with vectors of
2772 long long/long/short/char. */
2773 if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
2774 op = fold_convert (TREE_TYPE (vector_type), op);
2775 break;
2777 default:
2778 op = gimple_op (stmt, op_num + 1);
2779 break;
2783 if (reduc_index != -1)
2785 loop = (gimple_bb (stmt))->loop_father;
2786 def_stmt = SSA_NAME_DEF_STMT (op);
2788 gcc_assert (loop);
2790 /* Get the def before the loop. In reduction chain we have only
2791 one initial value. */
2792 if ((j != (number_of_copies - 1)
2793 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
2794 && i != 0))
2795 && neutral_op)
2796 op = neutral_op;
2797 else
2798 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
2799 loop_preheader_edge (loop));
2802 /* Create 'vect_ = {op0,op1,...,opn}'. */
2803 number_of_places_left_in_vector--;
2804 tree orig_op = op;
2805 if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
2807 if (CONSTANT_CLASS_P (op))
2809 op = fold_unary (VIEW_CONVERT_EXPR,
2810 TREE_TYPE (vector_type), op);
2811 gcc_assert (op && CONSTANT_CLASS_P (op));
2813 else
2815 tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
2816 gimple init_stmt;
2817 op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), op);
2818 init_stmt
2819 = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR, op);
2820 gimple_seq_add_stmt (&ctor_seq, init_stmt);
2821 op = new_temp;
2824 elts[number_of_places_left_in_vector] = op;
2825 if (!CONSTANT_CLASS_P (op))
2826 constant_p = false;
2827 if (TREE_CODE (orig_op) == SSA_NAME
2828 && !SSA_NAME_IS_DEFAULT_DEF (orig_op)
2829 && STMT_VINFO_BB_VINFO (stmt_vinfo)
2830 && (STMT_VINFO_BB_VINFO (stmt_vinfo)->bb
2831 == gimple_bb (SSA_NAME_DEF_STMT (orig_op))))
2832 place_after_defs = true;
2834 if (number_of_places_left_in_vector == 0)
2836 number_of_places_left_in_vector = nunits;
2838 if (constant_p)
2839 vec_cst = build_vector (vector_type, elts);
2840 else
2842 vec<constructor_elt, va_gc> *v;
2843 unsigned k;
2844 vec_alloc (v, nunits);
2845 for (k = 0; k < nunits; ++k)
2846 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
2847 vec_cst = build_constructor (vector_type, v);
2849 tree init;
2850 gimple_stmt_iterator gsi;
2851 if (place_after_defs)
2853 gsi = gsi_for_stmt
2854 (vect_find_last_scalar_stmt_in_slp (slp_node));
2855 init = vect_init_vector (stmt, vec_cst, vector_type, &gsi);
2857 else
2858 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
2859 if (ctor_seq != NULL)
2861 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
2862 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
2863 GSI_SAME_STMT);
2864 ctor_seq = NULL;
2866 voprnds.quick_push (init);
2867 place_after_defs = false;
2872 /* Since the vectors are created in the reverse order, we should invert
2873 them. */
2874 vec_num = voprnds.length ();
2875 for (j = vec_num; j != 0; j--)
2877 vop = voprnds[j - 1];
2878 vec_oprnds->quick_push (vop);
2881 voprnds.release ();
2883 /* In case that VF is greater than the unrolling factor needed for the SLP
2884 group of stmts, NUMBER_OF_VECTORS to be created is greater than
2885 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
2886 to replicate the vectors. */
2887 while (number_of_vectors > vec_oprnds->length ())
2889 tree neutral_vec = NULL;
2891 if (neutral_op)
2893 if (!neutral_vec)
2894 neutral_vec = build_vector_from_val (vector_type, neutral_op);
2896 vec_oprnds->quick_push (neutral_vec);
2898 else
2900 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
2901 vec_oprnds->quick_push (vop);
2907 /* Get vectorized definitions from SLP_NODE that contains corresponding
2908 vectorized def-stmts. */
2910 static void
2911 vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
2913 tree vec_oprnd;
2914 gimple vec_def_stmt;
2915 unsigned int i;
2917 gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
2919 FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
2921 gcc_assert (vec_def_stmt);
2922 vec_oprnd = gimple_get_lhs (vec_def_stmt);
2923 vec_oprnds->quick_push (vec_oprnd);
2928 /* Get vectorized definitions for SLP_NODE.
2929 If the scalar definitions are loop invariants or constants, collect them and
2930 call vect_get_constant_vectors() to create vector stmts.
2931 Otherwise, the def-stmts must be already vectorized and the vectorized stmts
2932 must be stored in the corresponding child of SLP_NODE, and we call
2933 vect_get_slp_vect_defs () to retrieve them. */
2935 void
2936 vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
2937 vec<vec<tree> > *vec_oprnds, int reduc_index)
2939 gimple first_stmt;
2940 int number_of_vects = 0, i;
2941 unsigned int child_index = 0;
2942 HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
2943 slp_tree child = NULL;
2944 vec<tree> vec_defs;
2945 tree oprnd;
2946 bool vectorized_defs;
2948 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2949 FOR_EACH_VEC_ELT (ops, i, oprnd)
2951 /* For each operand we check if it has vectorized definitions in a child
2952 node or we need to create them (for invariants and constants). We
2953 check if the LHS of the first stmt of the next child matches OPRND.
2954 If it does, we found the correct child. Otherwise, we call
2955 vect_get_constant_vectors (), and not advance CHILD_INDEX in order
2956 to check this child node for the next operand. */
2957 vectorized_defs = false;
2958 if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
2960 child = SLP_TREE_CHILDREN (slp_node)[child_index];
2962 /* We have to check both pattern and original def, if available. */
2963 if (child)
2965 gimple first_def = SLP_TREE_SCALAR_STMTS (child)[0];
2966 gimple related
2967 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
2969 if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0)
2970 || (related
2971 && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
2973 /* The number of vector defs is determined by the number of
2974 vector statements in the node from which we get those
2975 statements. */
2976 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
2977 vectorized_defs = true;
2978 child_index++;
2981 else
2982 child_index++;
2985 if (!vectorized_defs)
2987 if (i == 0)
2989 number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
2990 /* Number of vector stmts was calculated according to LHS in
2991 vect_schedule_slp_instance (), fix it by replacing LHS with
2992 RHS, if necessary. See vect_get_smallest_scalar_type () for
2993 details. */
2994 vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
2995 &rhs_size_unit);
2996 if (rhs_size_unit != lhs_size_unit)
2998 number_of_vects *= rhs_size_unit;
2999 number_of_vects /= lhs_size_unit;
3004 /* Allocate memory for vectorized defs. */
3005 vec_defs = vNULL;
3006 vec_defs.create (number_of_vects);
3008 /* For reduction defs we call vect_get_constant_vectors (), since we are
3009 looking for initial loop invariant values. */
3010 if (vectorized_defs && reduc_index == -1)
3011 /* The defs are already vectorized. */
3012 vect_get_slp_vect_defs (child, &vec_defs);
3013 else
3014 /* Build vectors from scalar defs. */
3015 vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
3016 number_of_vects, reduc_index);
3018 vec_oprnds->quick_push (vec_defs);
3020 /* For reductions, we only need initial values. */
3021 if (reduc_index != -1)
3022 return;
3027 /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
3028 building a vector of type MASK_TYPE from it) and two input vectors placed in
3029 DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
3030 shifting by STRIDE elements of DR_CHAIN for every copy.
3031 (STRIDE is the number of vectorized stmts for NODE divided by the number of
3032 copies).
3033 VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
3034 the created stmts must be inserted. */
3036 static inline void
3037 vect_create_mask_and_perm (gimple stmt, gimple next_scalar_stmt,
3038 tree mask, int first_vec_indx, int second_vec_indx,
3039 gimple_stmt_iterator *gsi, slp_tree node,
3040 tree vectype, vec<tree> dr_chain,
3041 int ncopies, int vect_stmts_counter)
3043 tree perm_dest;
3044 gimple perm_stmt = NULL;
3045 stmt_vec_info next_stmt_info;
3046 int i, stride;
3047 tree first_vec, second_vec, data_ref;
3049 stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
3051 /* Initialize the vect stmts of NODE to properly insert the generated
3052 stmts later. */
3053 for (i = SLP_TREE_VEC_STMTS (node).length ();
3054 i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
3055 SLP_TREE_VEC_STMTS (node).quick_push (NULL);
3057 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
3058 for (i = 0; i < ncopies; i++)
3060 first_vec = dr_chain[first_vec_indx];
3061 second_vec = dr_chain[second_vec_indx];
3063 /* Generate the permute statement. */
3064 perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
3065 first_vec, second_vec, mask);
3066 data_ref = make_ssa_name (perm_dest, perm_stmt);
3067 gimple_set_lhs (perm_stmt, data_ref);
3068 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
3070 /* Store the vector statement in NODE. */
3071 SLP_TREE_VEC_STMTS (node)[stride * i + vect_stmts_counter] = perm_stmt;
3073 first_vec_indx += stride;
3074 second_vec_indx += stride;
3077 /* Mark the scalar stmt as vectorized. */
3078 next_stmt_info = vinfo_for_stmt (next_scalar_stmt);
3079 STMT_VINFO_VEC_STMT (next_stmt_info) = perm_stmt;
3083 /* Given FIRST_MASK_ELEMENT - the mask element in element representation,
3084 return in CURRENT_MASK_ELEMENT its equivalent in target specific
3085 representation. Check that the mask is valid and return FALSE if not.
3086 Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
3087 the next vector, i.e., the current first vector is not needed. */
3089 static bool
3090 vect_get_mask_element (gimple stmt, int first_mask_element, int m,
3091 int mask_nunits, bool only_one_vec, int index,
3092 unsigned char *mask, int *current_mask_element,
3093 bool *need_next_vector, int *number_of_mask_fixes,
3094 bool *mask_fixed, bool *needs_first_vector)
3096 int i;
3098 /* Convert to target specific representation. */
3099 *current_mask_element = first_mask_element + m;
3100 /* Adjust the value in case it's a mask for second and third vectors. */
3101 *current_mask_element -= mask_nunits * (*number_of_mask_fixes - 1);
3103 if (*current_mask_element < 0)
3105 if (dump_enabled_p ())
3107 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3108 "permutation requires past vector ");
3109 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3110 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3112 return false;
3115 if (*current_mask_element < mask_nunits)
3116 *needs_first_vector = true;
3118 /* We have only one input vector to permute but the mask accesses values in
3119 the next vector as well. */
3120 if (only_one_vec && *current_mask_element >= mask_nunits)
3122 if (dump_enabled_p ())
3124 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3125 "permutation requires at least two vectors ");
3126 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3127 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3130 return false;
3133 /* The mask requires the next vector. */
3134 while (*current_mask_element >= mask_nunits * 2)
3136 if (*needs_first_vector || *mask_fixed)
3138 /* We either need the first vector too or have already moved to the
3139 next vector. In both cases, this permutation needs three
3140 vectors. */
3141 if (dump_enabled_p ())
3143 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3144 "permutation requires at "
3145 "least three vectors ");
3146 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3147 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3150 return false;
3153 /* We move to the next vector, dropping the first one and working with
3154 the second and the third - we need to adjust the values of the mask
3155 accordingly. */
3156 *current_mask_element -= mask_nunits * *number_of_mask_fixes;
3158 for (i = 0; i < index; i++)
3159 mask[i] -= mask_nunits * *number_of_mask_fixes;
3161 (*number_of_mask_fixes)++;
3162 *mask_fixed = true;
3165 *need_next_vector = *mask_fixed;
3167 /* This was the last element of this mask. Start a new one. */
3168 if (index == mask_nunits - 1)
3170 *number_of_mask_fixes = 1;
3171 *mask_fixed = false;
3172 *needs_first_vector = false;
3175 return true;
3179 /* Generate vector permute statements from a list of loads in DR_CHAIN.
3180 If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
3181 permute statements for the SLP node NODE of the SLP instance
3182 SLP_NODE_INSTANCE. */
3184 bool
3185 vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
3186 gimple_stmt_iterator *gsi, int vf,
3187 slp_instance slp_node_instance, bool analyze_only)
3189 gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3190 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3191 tree mask_element_type = NULL_TREE, mask_type;
3192 int i, j, k, nunits, vec_index = 0, scalar_index;
3193 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3194 gimple next_scalar_stmt;
3195 int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
3196 int first_mask_element;
3197 int index, unroll_factor, current_mask_element, ncopies;
3198 unsigned char *mask;
3199 bool only_one_vec = false, need_next_vector = false;
3200 int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
3201 int number_of_mask_fixes = 1;
3202 bool mask_fixed = false;
3203 bool needs_first_vector = false;
3204 machine_mode mode;
3206 mode = TYPE_MODE (vectype);
3208 if (!can_vec_perm_p (mode, false, NULL))
3210 if (dump_enabled_p ())
3212 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3213 "no vect permute for ");
3214 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3215 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3217 return false;
3220 /* The generic VEC_PERM_EXPR code always uses an integral type of the
3221 same size as the vector element being permuted. */
3222 mask_element_type = lang_hooks.types.type_for_mode
3223 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
3224 mask_type = get_vectype_for_scalar_type (mask_element_type);
3225 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3226 mask = XALLOCAVEC (unsigned char, nunits);
3227 unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3229 /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
3230 unrolling factor. */
3231 orig_vec_stmts_num = group_size *
3232 SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance) / nunits;
3233 if (orig_vec_stmts_num == 1)
3234 only_one_vec = true;
3236 /* Number of copies is determined by the final vectorization factor
3237 relatively to SLP_NODE_INSTANCE unrolling factor. */
3238 ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
3240 if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
3241 return false;
3243 stmt_info = vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info));
3245 /* Generate permutation masks for every NODE. Number of masks for each NODE
3246 is equal to GROUP_SIZE.
3247 E.g., we have a group of three nodes with three loads from the same
3248 location in each node, and the vector size is 4. I.e., we have a
3249 a0b0c0a1b1c1... sequence and we need to create the following vectors:
3250 for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
3251 for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
3254 The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
3255 The last mask is illegal since we assume two operands for permute
3256 operation, and the mask element values can't be outside that range.
3257 Hence, the last mask must be converted into {2,5,5,5}.
3258 For the first two permutations we need the first and the second input
3259 vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
3260 we need the second and the third vectors: {b1,c1,a2,b2} and
3261 {c2,a3,b3,c3}. */
3264 scalar_index = 0;
3265 index = 0;
3266 vect_stmts_counter = 0;
3267 vec_index = 0;
3268 first_vec_index = vec_index++;
3269 if (only_one_vec)
3270 second_vec_index = first_vec_index;
3271 else
3272 second_vec_index = vec_index++;
3274 for (j = 0; j < unroll_factor; j++)
3276 for (k = 0; k < group_size; k++)
3278 i = SLP_TREE_LOAD_PERMUTATION (node)[k];
3279 first_mask_element = i + j * STMT_VINFO_GROUP_SIZE (stmt_info);
3280 if (!vect_get_mask_element (stmt, first_mask_element, 0,
3281 nunits, only_one_vec, index,
3282 mask, &current_mask_element,
3283 &need_next_vector,
3284 &number_of_mask_fixes, &mask_fixed,
3285 &needs_first_vector))
3286 return false;
3287 gcc_assert (current_mask_element >= 0
3288 && current_mask_element < 2 * nunits);
3289 mask[index++] = current_mask_element;
3291 if (index == nunits)
3293 index = 0;
3294 if (!can_vec_perm_p (mode, false, mask))
3296 if (dump_enabled_p ())
3298 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
3299 vect_location,
3300 "unsupported vect permute { ");
3301 for (i = 0; i < nunits; ++i)
3302 dump_printf (MSG_MISSED_OPTIMIZATION, "%d ",
3303 mask[i]);
3304 dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
3306 return false;
3309 if (!analyze_only)
3311 int l;
3312 tree mask_vec, *mask_elts;
3313 mask_elts = XALLOCAVEC (tree, nunits);
3314 for (l = 0; l < nunits; ++l)
3315 mask_elts[l] = build_int_cst (mask_element_type,
3316 mask[l]);
3317 mask_vec = build_vector (mask_type, mask_elts);
3319 if (need_next_vector)
3321 first_vec_index = second_vec_index;
3322 second_vec_index = vec_index;
3325 next_scalar_stmt
3326 = SLP_TREE_SCALAR_STMTS (node)[scalar_index++];
3328 vect_create_mask_and_perm (stmt, next_scalar_stmt,
3329 mask_vec, first_vec_index, second_vec_index,
3330 gsi, node, vectype, dr_chain,
3331 ncopies, vect_stmts_counter++);
3338 return true;
3343 /* Vectorize SLP instance tree in postorder. */
3345 static bool
3346 vect_schedule_slp_instance (slp_tree node, slp_instance instance,
3347 unsigned int vectorization_factor)
3349 gimple stmt;
3350 bool grouped_store, is_store;
3351 gimple_stmt_iterator si;
3352 stmt_vec_info stmt_info;
3353 unsigned int vec_stmts_size, nunits, group_size;
3354 tree vectype;
3355 int i;
3356 slp_tree child;
3358 if (!node)
3359 return false;
3361 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3362 vect_schedule_slp_instance (child, instance, vectorization_factor);
3364 stmt = SLP_TREE_SCALAR_STMTS (node)[0];
3365 stmt_info = vinfo_for_stmt (stmt);
3367 /* VECTYPE is the type of the destination. */
3368 vectype = STMT_VINFO_VECTYPE (stmt_info);
3369 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
3370 group_size = SLP_INSTANCE_GROUP_SIZE (instance);
3372 /* For each SLP instance calculate number of vector stmts to be created
3373 for the scalar stmts in each node of the SLP tree. Number of vector
3374 elements in one vector iteration is the number of scalar elements in
3375 one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
3376 size.
3377 Unless this is a SLP reduction in which case the number of vector
3378 stmts is equal to the number of vector stmts of the children. */
3379 if (GROUP_FIRST_ELEMENT (stmt_info)
3380 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
3381 vec_stmts_size = SLP_TREE_NUMBER_OF_VEC_STMTS (SLP_TREE_CHILDREN (node)[0]);
3382 else
3383 vec_stmts_size = (vectorization_factor * group_size) / nunits;
3385 if (!SLP_TREE_VEC_STMTS (node).exists ())
3387 SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
3388 SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
3391 if (dump_enabled_p ())
3393 dump_printf_loc (MSG_NOTE,vect_location,
3394 "------>vectorizing SLP node starting from: ");
3395 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3396 dump_printf (MSG_NOTE, "\n");
3399 /* Vectorized stmts go before the last scalar stmt which is where
3400 all uses are ready. */
3401 si = gsi_for_stmt (vect_find_last_scalar_stmt_in_slp (node));
3403 /* Mark the first element of the reduction chain as reduction to properly
3404 transform the node. In the analysis phase only the last element of the
3405 chain is marked as reduction. */
3406 if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
3407 && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
3409 STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
3410 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
3413 /* Handle two-operation SLP nodes by vectorizing the group with
3414 both operations and then performing a merge. */
3415 if (SLP_TREE_TWO_OPERATORS (node))
3417 enum tree_code code0 = gimple_assign_rhs_code (stmt);
3418 enum tree_code ocode;
3419 gimple ostmt;
3420 unsigned char *mask = XALLOCAVEC (unsigned char, group_size);
3421 bool allsame = true;
3422 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, ostmt)
3423 if (gimple_assign_rhs_code (ostmt) != code0)
3425 mask[i] = 1;
3426 allsame = false;
3427 ocode = gimple_assign_rhs_code (ostmt);
3429 else
3430 mask[i] = 0;
3431 if (!allsame)
3433 vec<gimple> v0;
3434 vec<gimple> v1;
3435 unsigned j;
3436 tree tmask = NULL_TREE;
3437 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3438 v0 = SLP_TREE_VEC_STMTS (node).copy ();
3439 SLP_TREE_VEC_STMTS (node).truncate (0);
3440 gimple_assign_set_rhs_code (stmt, ocode);
3441 vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3442 gimple_assign_set_rhs_code (stmt, code0);
3443 v1 = SLP_TREE_VEC_STMTS (node).copy ();
3444 SLP_TREE_VEC_STMTS (node).truncate (0);
3445 tree meltype = build_nonstandard_integer_type
3446 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))), 1);
3447 tree mvectype = get_same_sized_vectype (meltype, vectype);
3448 unsigned k = 0, l;
3449 for (j = 0; j < v0.length (); ++j)
3451 tree *melts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (vectype));
3452 for (l = 0; l < TYPE_VECTOR_SUBPARTS (vectype); ++l)
3454 if (k >= group_size)
3455 k = 0;
3456 melts[l] = build_int_cst
3457 (meltype, mask[k++] * TYPE_VECTOR_SUBPARTS (vectype) + l);
3459 tmask = build_vector (mvectype, melts);
3461 /* ??? Not all targets support a VEC_PERM_EXPR with a
3462 constant mask that would translate to a vec_merge RTX
3463 (with their vec_perm_const_ok). We can either not
3464 vectorize in that case or let veclower do its job.
3465 Unfortunately that isn't too great and at least for
3466 plus/minus we'd eventually like to match targets
3467 vector addsub instructions. */
3468 gimple vstmt;
3469 vstmt = gimple_build_assign (make_ssa_name (vectype),
3470 VEC_PERM_EXPR,
3471 gimple_assign_lhs (v0[j]),
3472 gimple_assign_lhs (v1[j]), tmask);
3473 vect_finish_stmt_generation (stmt, vstmt, &si);
3474 SLP_TREE_VEC_STMTS (node).quick_push (vstmt);
3476 v0.release ();
3477 v1.release ();
3478 return false;
3481 is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
3482 return is_store;
3485 /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
3486 For loop vectorization this is done in vectorizable_call, but for SLP
3487 it needs to be deferred until end of vect_schedule_slp, because multiple
3488 SLP instances may refer to the same scalar stmt. */
3490 static void
3491 vect_remove_slp_scalar_calls (slp_tree node)
3493 gimple stmt, new_stmt;
3494 gimple_stmt_iterator gsi;
3495 int i;
3496 slp_tree child;
3497 tree lhs;
3498 stmt_vec_info stmt_info;
3500 if (!node)
3501 return;
3503 FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
3504 vect_remove_slp_scalar_calls (child);
3506 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
3508 if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
3509 continue;
3510 stmt_info = vinfo_for_stmt (stmt);
3511 if (stmt_info == NULL
3512 || is_pattern_stmt_p (stmt_info)
3513 || !PURE_SLP_STMT (stmt_info))
3514 continue;
3515 lhs = gimple_call_lhs (stmt);
3516 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3517 set_vinfo_for_stmt (new_stmt, stmt_info);
3518 set_vinfo_for_stmt (stmt, NULL);
3519 STMT_VINFO_STMT (stmt_info) = new_stmt;
3520 gsi = gsi_for_stmt (stmt);
3521 gsi_replace (&gsi, new_stmt, false);
3522 SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
3526 /* Generate vector code for all SLP instances in the loop/basic block. */
3528 bool
3529 vect_schedule_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
3531 vec<slp_instance> slp_instances;
3532 slp_instance instance;
3533 unsigned int i, vf;
3534 bool is_store = false;
3536 if (loop_vinfo)
3538 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
3539 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3541 else
3543 slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
3544 vf = 1;
3547 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3549 /* Schedule the tree of INSTANCE. */
3550 is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
3551 instance, vf);
3552 if (dump_enabled_p ())
3553 dump_printf_loc (MSG_NOTE, vect_location,
3554 "vectorizing stmts using SLP.\n");
3557 FOR_EACH_VEC_ELT (slp_instances, i, instance)
3559 slp_tree root = SLP_INSTANCE_TREE (instance);
3560 gimple store;
3561 unsigned int j;
3562 gimple_stmt_iterator gsi;
3564 /* Remove scalar call stmts. Do not do this for basic-block
3565 vectorization as not all uses may be vectorized.
3566 ??? Why should this be necessary? DCE should be able to
3567 remove the stmts itself.
3568 ??? For BB vectorization we can as well remove scalar
3569 stmts starting from the SLP tree root if they have no
3570 uses. */
3571 if (loop_vinfo)
3572 vect_remove_slp_scalar_calls (root);
3574 for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
3575 && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
3577 if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
3578 break;
3580 if (is_pattern_stmt_p (vinfo_for_stmt (store)))
3581 store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
3582 /* Free the attached stmt_vec_info and remove the stmt. */
3583 gsi = gsi_for_stmt (store);
3584 unlink_stmt_vdef (store);
3585 gsi_remove (&gsi, true);
3586 release_defs (store);
3587 free_stmt_vec_info (store);
3591 return is_store;
3595 /* Vectorize the basic block. */
3597 void
3598 vect_slp_transform_bb (basic_block bb)
3600 bb_vec_info bb_vinfo = vec_info_for_bb (bb);
3601 gimple_stmt_iterator si;
3603 gcc_assert (bb_vinfo);
3605 if (dump_enabled_p ())
3606 dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB\n");
3608 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
3610 gimple stmt = gsi_stmt (si);
3611 stmt_vec_info stmt_info;
3613 if (dump_enabled_p ())
3615 dump_printf_loc (MSG_NOTE, vect_location,
3616 "------>SLPing statement: ");
3617 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3618 dump_printf (MSG_NOTE, "\n");
3621 stmt_info = vinfo_for_stmt (stmt);
3622 gcc_assert (stmt_info);
3624 /* Schedule all the SLP instances when the first SLP stmt is reached. */
3625 if (STMT_SLP_TYPE (stmt_info))
3627 vect_schedule_slp (NULL, bb_vinfo);
3628 break;
3632 if (dump_enabled_p ())
3633 dump_printf_loc (MSG_NOTE, vect_location,
3634 "BASIC BLOCK VECTORIZED\n");
3636 destroy_bb_vec_info (bb_vinfo);