Implement P0258R2 - helper for C++17
[official-gcc.git] / gcc / tree-vect-stmts.c
blobd698785a547cf82fb642415e98982f217e58b6e3
1 /* Statement Analysis and Transformation for Vectorization
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "ssa.h"
31 #include "optabs-tree.h"
32 #include "insn-config.h"
33 #include "recog.h" /* FIXME: for insn_data */
34 #include "cgraph.h"
35 #include "dumpfile.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "tree-eh.h"
40 #include "gimplify.h"
41 #include "gimple-iterator.h"
42 #include "gimplify-me.h"
43 #include "tree-cfg.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "cfgloop.h"
46 #include "tree-ssa-loop.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-vectorizer.h"
49 #include "builtins.h"
50 #include "internal-fn.h"
52 /* For lang_hooks.types.type_for_mode. */
53 #include "langhooks.h"
55 /* Says whether a statement is a load, a store of a vectorized statement
56 result, or a store of an invariant value. */
57 enum vec_load_store_type {
58 VLS_LOAD,
59 VLS_STORE,
60 VLS_STORE_INVARIANT
63 /* Return the vectorized type for the given statement. */
65 tree
66 stmt_vectype (struct _stmt_vec_info *stmt_info)
68 return STMT_VINFO_VECTYPE (stmt_info);
71 /* Return TRUE iff the given statement is in an inner loop relative to
72 the loop being vectorized. */
73 bool
74 stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
76 gimple *stmt = STMT_VINFO_STMT (stmt_info);
77 basic_block bb = gimple_bb (stmt);
78 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
79 struct loop* loop;
81 if (!loop_vinfo)
82 return false;
84 loop = LOOP_VINFO_LOOP (loop_vinfo);
86 return (bb->loop_father == loop->inner);
89 /* Record the cost of a statement, either by directly informing the
90 target model or by saving it in a vector for later processing.
91 Return a preliminary estimate of the statement's cost. */
93 unsigned
94 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
95 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
96 int misalign, enum vect_cost_model_location where)
98 if (body_cost_vec)
100 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
101 stmt_info_for_cost si = { count, kind,
102 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
103 misalign };
104 body_cost_vec->safe_push (si);
105 return (unsigned)
106 (builtin_vectorization_cost (kind, vectype, misalign) * count);
108 else
109 return add_stmt_cost (stmt_info->vinfo->target_cost_data,
110 count, kind, stmt_info, misalign, where);
113 /* Return a variable of type ELEM_TYPE[NELEMS]. */
115 static tree
116 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
118 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
119 "vect_array");
122 /* ARRAY is an array of vectors created by create_vector_array.
123 Return an SSA_NAME for the vector in index N. The reference
124 is part of the vectorization of STMT and the vector is associated
125 with scalar destination SCALAR_DEST. */
127 static tree
128 read_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
129 tree array, unsigned HOST_WIDE_INT n)
131 tree vect_type, vect, vect_name, array_ref;
132 gimple *new_stmt;
134 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
135 vect_type = TREE_TYPE (TREE_TYPE (array));
136 vect = vect_create_destination_var (scalar_dest, vect_type);
137 array_ref = build4 (ARRAY_REF, vect_type, array,
138 build_int_cst (size_type_node, n),
139 NULL_TREE, NULL_TREE);
141 new_stmt = gimple_build_assign (vect, array_ref);
142 vect_name = make_ssa_name (vect, new_stmt);
143 gimple_assign_set_lhs (new_stmt, vect_name);
144 vect_finish_stmt_generation (stmt, new_stmt, gsi);
146 return vect_name;
149 /* ARRAY is an array of vectors created by create_vector_array.
150 Emit code to store SSA_NAME VECT in index N of the array.
151 The store is part of the vectorization of STMT. */
153 static void
154 write_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree vect,
155 tree array, unsigned HOST_WIDE_INT n)
157 tree array_ref;
158 gimple *new_stmt;
160 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
161 build_int_cst (size_type_node, n),
162 NULL_TREE, NULL_TREE);
164 new_stmt = gimple_build_assign (array_ref, vect);
165 vect_finish_stmt_generation (stmt, new_stmt, gsi);
168 /* PTR is a pointer to an array of type TYPE. Return a representation
169 of *PTR. The memory reference replaces those in FIRST_DR
170 (and its group). */
172 static tree
173 create_array_ref (tree type, tree ptr, tree alias_ptr_type)
175 tree mem_ref;
177 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
178 /* Arrays have the same alignment as their type. */
179 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
180 return mem_ref;
183 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
185 /* Function vect_mark_relevant.
187 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
189 static void
190 vect_mark_relevant (vec<gimple *> *worklist, gimple *stmt,
191 enum vect_relevant relevant, bool live_p)
193 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
194 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
195 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
196 gimple *pattern_stmt;
198 if (dump_enabled_p ())
200 dump_printf_loc (MSG_NOTE, vect_location,
201 "mark relevant %d, live %d: ", relevant, live_p);
202 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
205 /* If this stmt is an original stmt in a pattern, we might need to mark its
206 related pattern stmt instead of the original stmt. However, such stmts
207 may have their own uses that are not in any pattern, in such cases the
208 stmt itself should be marked. */
209 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
211 /* This is the last stmt in a sequence that was detected as a
212 pattern that can potentially be vectorized. Don't mark the stmt
213 as relevant/live because it's not going to be vectorized.
214 Instead mark the pattern-stmt that replaces it. */
216 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
218 if (dump_enabled_p ())
219 dump_printf_loc (MSG_NOTE, vect_location,
220 "last stmt in pattern. don't mark"
221 " relevant/live.\n");
222 stmt_info = vinfo_for_stmt (pattern_stmt);
223 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
224 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
225 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
226 stmt = pattern_stmt;
229 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
230 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
231 STMT_VINFO_RELEVANT (stmt_info) = relevant;
233 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
234 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
236 if (dump_enabled_p ())
237 dump_printf_loc (MSG_NOTE, vect_location,
238 "already marked relevant/live.\n");
239 return;
242 worklist->safe_push (stmt);
246 /* Function is_simple_and_all_uses_invariant
248 Return true if STMT is simple and all uses of it are invariant. */
250 bool
251 is_simple_and_all_uses_invariant (gimple *stmt, loop_vec_info loop_vinfo)
253 tree op;
254 gimple *def_stmt;
255 ssa_op_iter iter;
257 if (!is_gimple_assign (stmt))
258 return false;
260 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
262 enum vect_def_type dt = vect_uninitialized_def;
264 if (!vect_is_simple_use (op, loop_vinfo, &def_stmt, &dt))
266 if (dump_enabled_p ())
267 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
268 "use not simple.\n");
269 return false;
272 if (dt != vect_external_def && dt != vect_constant_def)
273 return false;
275 return true;
278 /* Function vect_stmt_relevant_p.
280 Return true if STMT in loop that is represented by LOOP_VINFO is
281 "relevant for vectorization".
283 A stmt is considered "relevant for vectorization" if:
284 - it has uses outside the loop.
285 - it has vdefs (it alters memory).
286 - control stmts in the loop (except for the exit condition).
288 CHECKME: what other side effects would the vectorizer allow? */
290 static bool
291 vect_stmt_relevant_p (gimple *stmt, loop_vec_info loop_vinfo,
292 enum vect_relevant *relevant, bool *live_p)
294 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
295 ssa_op_iter op_iter;
296 imm_use_iterator imm_iter;
297 use_operand_p use_p;
298 def_operand_p def_p;
300 *relevant = vect_unused_in_scope;
301 *live_p = false;
303 /* cond stmt other than loop exit cond. */
304 if (is_ctrl_stmt (stmt)
305 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
306 != loop_exit_ctrl_vec_info_type)
307 *relevant = vect_used_in_scope;
309 /* changing memory. */
310 if (gimple_code (stmt) != GIMPLE_PHI)
311 if (gimple_vdef (stmt)
312 && !gimple_clobber_p (stmt))
314 if (dump_enabled_p ())
315 dump_printf_loc (MSG_NOTE, vect_location,
316 "vec_stmt_relevant_p: stmt has vdefs.\n");
317 *relevant = vect_used_in_scope;
320 /* uses outside the loop. */
321 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
323 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
325 basic_block bb = gimple_bb (USE_STMT (use_p));
326 if (!flow_bb_inside_loop_p (loop, bb))
328 if (dump_enabled_p ())
329 dump_printf_loc (MSG_NOTE, vect_location,
330 "vec_stmt_relevant_p: used out of loop.\n");
332 if (is_gimple_debug (USE_STMT (use_p)))
333 continue;
335 /* We expect all such uses to be in the loop exit phis
336 (because of loop closed form) */
337 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
338 gcc_assert (bb == single_exit (loop)->dest);
340 *live_p = true;
345 if (*live_p && *relevant == vect_unused_in_scope
346 && !is_simple_and_all_uses_invariant (stmt, loop_vinfo))
348 if (dump_enabled_p ())
349 dump_printf_loc (MSG_NOTE, vect_location,
350 "vec_stmt_relevant_p: stmt live but not relevant.\n");
351 *relevant = vect_used_only_live;
354 return (*live_p || *relevant);
358 /* Function exist_non_indexing_operands_for_use_p
360 USE is one of the uses attached to STMT. Check if USE is
361 used in STMT for anything other than indexing an array. */
363 static bool
364 exist_non_indexing_operands_for_use_p (tree use, gimple *stmt)
366 tree operand;
367 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
369 /* USE corresponds to some operand in STMT. If there is no data
370 reference in STMT, then any operand that corresponds to USE
371 is not indexing an array. */
372 if (!STMT_VINFO_DATA_REF (stmt_info))
373 return true;
375 /* STMT has a data_ref. FORNOW this means that its of one of
376 the following forms:
377 -1- ARRAY_REF = var
378 -2- var = ARRAY_REF
379 (This should have been verified in analyze_data_refs).
381 'var' in the second case corresponds to a def, not a use,
382 so USE cannot correspond to any operands that are not used
383 for array indexing.
385 Therefore, all we need to check is if STMT falls into the
386 first case, and whether var corresponds to USE. */
388 if (!gimple_assign_copy_p (stmt))
390 if (is_gimple_call (stmt)
391 && gimple_call_internal_p (stmt))
392 switch (gimple_call_internal_fn (stmt))
394 case IFN_MASK_STORE:
395 operand = gimple_call_arg (stmt, 3);
396 if (operand == use)
397 return true;
398 /* FALLTHRU */
399 case IFN_MASK_LOAD:
400 operand = gimple_call_arg (stmt, 2);
401 if (operand == use)
402 return true;
403 break;
404 default:
405 break;
407 return false;
410 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
411 return false;
412 operand = gimple_assign_rhs1 (stmt);
413 if (TREE_CODE (operand) != SSA_NAME)
414 return false;
416 if (operand == use)
417 return true;
419 return false;
424 Function process_use.
426 Inputs:
427 - a USE in STMT in a loop represented by LOOP_VINFO
428 - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
429 that defined USE. This is done by calling mark_relevant and passing it
430 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
431 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
432 be performed.
434 Outputs:
435 Generally, LIVE_P and RELEVANT are used to define the liveness and
436 relevance info of the DEF_STMT of this USE:
437 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
438 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
439 Exceptions:
440 - case 1: If USE is used only for address computations (e.g. array indexing),
441 which does not need to be directly vectorized, then the liveness/relevance
442 of the respective DEF_STMT is left unchanged.
443 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
444 skip DEF_STMT cause it had already been processed.
445 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
446 be modified accordingly.
448 Return true if everything is as expected. Return false otherwise. */
450 static bool
451 process_use (gimple *stmt, tree use, loop_vec_info loop_vinfo,
452 enum vect_relevant relevant, vec<gimple *> *worklist,
453 bool force)
455 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
456 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
457 stmt_vec_info dstmt_vinfo;
458 basic_block bb, def_bb;
459 gimple *def_stmt;
460 enum vect_def_type dt;
462 /* case 1: we are only interested in uses that need to be vectorized. Uses
463 that are used for address computation are not considered relevant. */
464 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
465 return true;
467 if (!vect_is_simple_use (use, loop_vinfo, &def_stmt, &dt))
469 if (dump_enabled_p ())
470 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
471 "not vectorized: unsupported use in stmt.\n");
472 return false;
475 if (!def_stmt || gimple_nop_p (def_stmt))
476 return true;
478 def_bb = gimple_bb (def_stmt);
479 if (!flow_bb_inside_loop_p (loop, def_bb))
481 if (dump_enabled_p ())
482 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
483 return true;
486 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
487 DEF_STMT must have already been processed, because this should be the
488 only way that STMT, which is a reduction-phi, was put in the worklist,
489 as there should be no other uses for DEF_STMT in the loop. So we just
490 check that everything is as expected, and we are done. */
491 dstmt_vinfo = vinfo_for_stmt (def_stmt);
492 bb = gimple_bb (stmt);
493 if (gimple_code (stmt) == GIMPLE_PHI
494 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
495 && gimple_code (def_stmt) != GIMPLE_PHI
496 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
497 && bb->loop_father == def_bb->loop_father)
499 if (dump_enabled_p ())
500 dump_printf_loc (MSG_NOTE, vect_location,
501 "reduc-stmt defining reduc-phi in the same nest.\n");
502 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
503 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
504 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
505 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
506 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
507 return true;
510 /* case 3a: outer-loop stmt defining an inner-loop stmt:
511 outer-loop-header-bb:
512 d = def_stmt
513 inner-loop:
514 stmt # use (d)
515 outer-loop-tail-bb:
516 ... */
517 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
519 if (dump_enabled_p ())
520 dump_printf_loc (MSG_NOTE, vect_location,
521 "outer-loop def-stmt defining inner-loop stmt.\n");
523 switch (relevant)
525 case vect_unused_in_scope:
526 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
527 vect_used_in_scope : vect_unused_in_scope;
528 break;
530 case vect_used_in_outer_by_reduction:
531 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
532 relevant = vect_used_by_reduction;
533 break;
535 case vect_used_in_outer:
536 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
537 relevant = vect_used_in_scope;
538 break;
540 case vect_used_in_scope:
541 break;
543 default:
544 gcc_unreachable ();
548 /* case 3b: inner-loop stmt defining an outer-loop stmt:
549 outer-loop-header-bb:
551 inner-loop:
552 d = def_stmt
553 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
554 stmt # use (d) */
555 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
557 if (dump_enabled_p ())
558 dump_printf_loc (MSG_NOTE, vect_location,
559 "inner-loop def-stmt defining outer-loop stmt.\n");
561 switch (relevant)
563 case vect_unused_in_scope:
564 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
565 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
566 vect_used_in_outer_by_reduction : vect_unused_in_scope;
567 break;
569 case vect_used_by_reduction:
570 case vect_used_only_live:
571 relevant = vect_used_in_outer_by_reduction;
572 break;
574 case vect_used_in_scope:
575 relevant = vect_used_in_outer;
576 break;
578 default:
579 gcc_unreachable ();
583 vect_mark_relevant (worklist, def_stmt, relevant, false);
584 return true;
588 /* Function vect_mark_stmts_to_be_vectorized.
590 Not all stmts in the loop need to be vectorized. For example:
592 for i...
593 for j...
594 1. T0 = i + j
595 2. T1 = a[T0]
597 3. j = j + 1
599 Stmt 1 and 3 do not need to be vectorized, because loop control and
600 addressing of vectorized data-refs are handled differently.
602 This pass detects such stmts. */
604 bool
605 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
607 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
608 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
609 unsigned int nbbs = loop->num_nodes;
610 gimple_stmt_iterator si;
611 gimple *stmt;
612 unsigned int i;
613 stmt_vec_info stmt_vinfo;
614 basic_block bb;
615 gimple *phi;
616 bool live_p;
617 enum vect_relevant relevant;
619 if (dump_enabled_p ())
620 dump_printf_loc (MSG_NOTE, vect_location,
621 "=== vect_mark_stmts_to_be_vectorized ===\n");
623 auto_vec<gimple *, 64> worklist;
625 /* 1. Init worklist. */
626 for (i = 0; i < nbbs; i++)
628 bb = bbs[i];
629 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
631 phi = gsi_stmt (si);
632 if (dump_enabled_p ())
634 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
635 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
638 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
639 vect_mark_relevant (&worklist, phi, relevant, live_p);
641 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
643 stmt = gsi_stmt (si);
644 if (dump_enabled_p ())
646 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
647 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
650 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
651 vect_mark_relevant (&worklist, stmt, relevant, live_p);
655 /* 2. Process_worklist */
656 while (worklist.length () > 0)
658 use_operand_p use_p;
659 ssa_op_iter iter;
661 stmt = worklist.pop ();
662 if (dump_enabled_p ())
664 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
665 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
668 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
669 (DEF_STMT) as relevant/irrelevant according to the relevance property
670 of STMT. */
671 stmt_vinfo = vinfo_for_stmt (stmt);
672 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
674 /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
675 propagated as is to the DEF_STMTs of its USEs.
677 One exception is when STMT has been identified as defining a reduction
678 variable; in this case we set the relevance to vect_used_by_reduction.
679 This is because we distinguish between two kinds of relevant stmts -
680 those that are used by a reduction computation, and those that are
681 (also) used by a regular computation. This allows us later on to
682 identify stmts that are used solely by a reduction, and therefore the
683 order of the results that they produce does not have to be kept. */
685 switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
687 case vect_reduction_def:
688 gcc_assert (relevant != vect_unused_in_scope);
689 if (relevant != vect_unused_in_scope
690 && relevant != vect_used_in_scope
691 && relevant != vect_used_by_reduction
692 && relevant != vect_used_only_live)
694 if (dump_enabled_p ())
695 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
696 "unsupported use of reduction.\n");
697 return false;
699 break;
701 case vect_nested_cycle:
702 if (relevant != vect_unused_in_scope
703 && relevant != vect_used_in_outer_by_reduction
704 && relevant != vect_used_in_outer)
706 if (dump_enabled_p ())
707 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
708 "unsupported use of nested cycle.\n");
710 return false;
712 break;
714 case vect_double_reduction_def:
715 if (relevant != vect_unused_in_scope
716 && relevant != vect_used_by_reduction
717 && relevant != vect_used_only_live)
719 if (dump_enabled_p ())
720 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
721 "unsupported use of double reduction.\n");
723 return false;
725 break;
727 default:
728 break;
731 if (is_pattern_stmt_p (stmt_vinfo))
733 /* Pattern statements are not inserted into the code, so
734 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
735 have to scan the RHS or function arguments instead. */
736 if (is_gimple_assign (stmt))
738 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
739 tree op = gimple_assign_rhs1 (stmt);
741 i = 1;
742 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
744 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
745 relevant, &worklist, false)
746 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
747 relevant, &worklist, false))
748 return false;
749 i = 2;
751 for (; i < gimple_num_ops (stmt); i++)
753 op = gimple_op (stmt, i);
754 if (TREE_CODE (op) == SSA_NAME
755 && !process_use (stmt, op, loop_vinfo, relevant,
756 &worklist, false))
757 return false;
760 else if (is_gimple_call (stmt))
762 for (i = 0; i < gimple_call_num_args (stmt); i++)
764 tree arg = gimple_call_arg (stmt, i);
765 if (!process_use (stmt, arg, loop_vinfo, relevant,
766 &worklist, false))
767 return false;
771 else
772 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
774 tree op = USE_FROM_PTR (use_p);
775 if (!process_use (stmt, op, loop_vinfo, relevant,
776 &worklist, false))
777 return false;
780 if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
782 gather_scatter_info gs_info;
783 if (!vect_check_gather_scatter (stmt, loop_vinfo, &gs_info))
784 gcc_unreachable ();
785 if (!process_use (stmt, gs_info.offset, loop_vinfo, relevant,
786 &worklist, true))
787 return false;
789 } /* while worklist */
791 return true;
795 /* Function vect_model_simple_cost.
797 Models cost for simple operations, i.e. those that only emit ncopies of a
798 single op. Right now, this does not account for multiple insns that could
799 be generated for the single vector op. We will handle that shortly. */
801 void
802 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
803 enum vect_def_type *dt,
804 stmt_vector_for_cost *prologue_cost_vec,
805 stmt_vector_for_cost *body_cost_vec)
807 int i;
808 int inside_cost = 0, prologue_cost = 0;
810 /* The SLP costs were already calculated during SLP tree build. */
811 if (PURE_SLP_STMT (stmt_info))
812 return;
814 /* FORNOW: Assuming maximum 2 args per stmts. */
815 for (i = 0; i < 2; i++)
816 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
817 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
818 stmt_info, 0, vect_prologue);
820 /* Pass the inside-of-loop statements to the target-specific cost model. */
821 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
822 stmt_info, 0, vect_body);
824 if (dump_enabled_p ())
825 dump_printf_loc (MSG_NOTE, vect_location,
826 "vect_model_simple_cost: inside_cost = %d, "
827 "prologue_cost = %d .\n", inside_cost, prologue_cost);
831 /* Model cost for type demotion and promotion operations. PWR is normally
832 zero for single-step promotions and demotions. It will be one if
833 two-step promotion/demotion is required, and so on. Each additional
834 step doubles the number of instructions required. */
836 static void
837 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
838 enum vect_def_type *dt, int pwr)
840 int i, tmp;
841 int inside_cost = 0, prologue_cost = 0;
842 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
843 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
844 void *target_cost_data;
846 /* The SLP costs were already calculated during SLP tree build. */
847 if (PURE_SLP_STMT (stmt_info))
848 return;
850 if (loop_vinfo)
851 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
852 else
853 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
855 for (i = 0; i < pwr + 1; i++)
857 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
858 (i + 1) : i;
859 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
860 vec_promote_demote, stmt_info, 0,
861 vect_body);
864 /* FORNOW: Assuming maximum 2 args per stmts. */
865 for (i = 0; i < 2; i++)
866 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
867 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
868 stmt_info, 0, vect_prologue);
870 if (dump_enabled_p ())
871 dump_printf_loc (MSG_NOTE, vect_location,
872 "vect_model_promotion_demotion_cost: inside_cost = %d, "
873 "prologue_cost = %d .\n", inside_cost, prologue_cost);
876 /* Function vect_model_store_cost
878 Models cost for stores. In the case of grouped accesses, one access
879 has the overhead of the grouped access attributed to it. */
881 void
882 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
883 vect_memory_access_type memory_access_type,
884 enum vect_def_type dt, slp_tree slp_node,
885 stmt_vector_for_cost *prologue_cost_vec,
886 stmt_vector_for_cost *body_cost_vec)
888 unsigned int inside_cost = 0, prologue_cost = 0;
889 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
890 gimple *first_stmt = STMT_VINFO_STMT (stmt_info);
891 bool grouped_access_p = STMT_VINFO_GROUPED_ACCESS (stmt_info);
893 if (dt == vect_constant_def || dt == vect_external_def)
894 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
895 stmt_info, 0, vect_prologue);
897 /* Grouped stores update all elements in the group at once,
898 so we want the DR for the first statement. */
899 if (!slp_node && grouped_access_p)
901 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
902 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
905 /* True if we should include any once-per-group costs as well as
906 the cost of the statement itself. For SLP we only get called
907 once per group anyhow. */
908 bool first_stmt_p = (first_stmt == STMT_VINFO_STMT (stmt_info));
910 /* We assume that the cost of a single store-lanes instruction is
911 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
912 access is instead being provided by a permute-and-store operation,
913 include the cost of the permutes. */
914 if (first_stmt_p
915 && memory_access_type == VMAT_CONTIGUOUS_PERMUTE)
917 /* Uses a high and low interleave or shuffle operations for each
918 needed permute. */
919 int group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
920 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
921 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
922 stmt_info, 0, vect_body);
924 if (dump_enabled_p ())
925 dump_printf_loc (MSG_NOTE, vect_location,
926 "vect_model_store_cost: strided group_size = %d .\n",
927 group_size);
930 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
931 /* Costs of the stores. */
932 if (memory_access_type == VMAT_ELEMENTWISE)
933 /* N scalar stores plus extracting the elements. */
934 inside_cost += record_stmt_cost (body_cost_vec,
935 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
936 scalar_store, stmt_info, 0, vect_body);
937 else
938 vect_get_store_cost (dr, ncopies, &inside_cost, body_cost_vec);
940 if (memory_access_type == VMAT_ELEMENTWISE
941 || memory_access_type == VMAT_STRIDED_SLP)
942 inside_cost += record_stmt_cost (body_cost_vec,
943 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
944 vec_to_scalar, stmt_info, 0, vect_body);
946 if (dump_enabled_p ())
947 dump_printf_loc (MSG_NOTE, vect_location,
948 "vect_model_store_cost: inside_cost = %d, "
949 "prologue_cost = %d .\n", inside_cost, prologue_cost);
953 /* Calculate cost of DR's memory access. */
954 void
955 vect_get_store_cost (struct data_reference *dr, int ncopies,
956 unsigned int *inside_cost,
957 stmt_vector_for_cost *body_cost_vec)
959 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
960 gimple *stmt = DR_STMT (dr);
961 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
963 switch (alignment_support_scheme)
965 case dr_aligned:
967 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
968 vector_store, stmt_info, 0,
969 vect_body);
971 if (dump_enabled_p ())
972 dump_printf_loc (MSG_NOTE, vect_location,
973 "vect_model_store_cost: aligned.\n");
974 break;
977 case dr_unaligned_supported:
979 /* Here, we assign an additional cost for the unaligned store. */
980 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
981 unaligned_store, stmt_info,
982 DR_MISALIGNMENT (dr), vect_body);
983 if (dump_enabled_p ())
984 dump_printf_loc (MSG_NOTE, vect_location,
985 "vect_model_store_cost: unaligned supported by "
986 "hardware.\n");
987 break;
990 case dr_unaligned_unsupported:
992 *inside_cost = VECT_MAX_COST;
994 if (dump_enabled_p ())
995 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
996 "vect_model_store_cost: unsupported access.\n");
997 break;
1000 default:
1001 gcc_unreachable ();
1006 /* Function vect_model_load_cost
1008 Models cost for loads. In the case of grouped accesses, one access has
1009 the overhead of the grouped access attributed to it. Since unaligned
1010 accesses are supported for loads, we also account for the costs of the
1011 access scheme chosen. */
1013 void
1014 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1015 vect_memory_access_type memory_access_type,
1016 slp_tree slp_node,
1017 stmt_vector_for_cost *prologue_cost_vec,
1018 stmt_vector_for_cost *body_cost_vec)
1020 gimple *first_stmt = STMT_VINFO_STMT (stmt_info);
1021 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1022 unsigned int inside_cost = 0, prologue_cost = 0;
1023 bool grouped_access_p = STMT_VINFO_GROUPED_ACCESS (stmt_info);
1025 /* Grouped loads read all elements in the group at once,
1026 so we want the DR for the first statement. */
1027 if (!slp_node && grouped_access_p)
1029 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1030 dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1033 /* True if we should include any once-per-group costs as well as
1034 the cost of the statement itself. For SLP we only get called
1035 once per group anyhow. */
1036 bool first_stmt_p = (first_stmt == STMT_VINFO_STMT (stmt_info));
1038 /* We assume that the cost of a single load-lanes instruction is
1039 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1040 access is instead being provided by a load-and-permute operation,
1041 include the cost of the permutes. */
1042 if (first_stmt_p
1043 && memory_access_type == VMAT_CONTIGUOUS_PERMUTE)
1045 /* Uses an even and odd extract operations or shuffle operations
1046 for each needed permute. */
1047 int group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
1048 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
1049 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1050 stmt_info, 0, vect_body);
1052 if (dump_enabled_p ())
1053 dump_printf_loc (MSG_NOTE, vect_location,
1054 "vect_model_load_cost: strided group_size = %d .\n",
1055 group_size);
1058 /* The loads themselves. */
1059 if (memory_access_type == VMAT_ELEMENTWISE)
1061 /* N scalar loads plus gathering them into a vector. */
1062 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1063 inside_cost += record_stmt_cost (body_cost_vec,
1064 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1065 scalar_load, stmt_info, 0, vect_body);
1067 else
1068 vect_get_load_cost (dr, ncopies, first_stmt_p,
1069 &inside_cost, &prologue_cost,
1070 prologue_cost_vec, body_cost_vec, true);
1071 if (memory_access_type == VMAT_ELEMENTWISE
1072 || memory_access_type == VMAT_STRIDED_SLP)
1073 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1074 stmt_info, 0, vect_body);
1076 if (dump_enabled_p ())
1077 dump_printf_loc (MSG_NOTE, vect_location,
1078 "vect_model_load_cost: inside_cost = %d, "
1079 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1083 /* Calculate cost of DR's memory access. */
1084 void
1085 vect_get_load_cost (struct data_reference *dr, int ncopies,
1086 bool add_realign_cost, unsigned int *inside_cost,
1087 unsigned int *prologue_cost,
1088 stmt_vector_for_cost *prologue_cost_vec,
1089 stmt_vector_for_cost *body_cost_vec,
1090 bool record_prologue_costs)
1092 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1093 gimple *stmt = DR_STMT (dr);
1094 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1096 switch (alignment_support_scheme)
1098 case dr_aligned:
1100 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1101 stmt_info, 0, vect_body);
1103 if (dump_enabled_p ())
1104 dump_printf_loc (MSG_NOTE, vect_location,
1105 "vect_model_load_cost: aligned.\n");
1107 break;
1109 case dr_unaligned_supported:
1111 /* Here, we assign an additional cost for the unaligned load. */
1112 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1113 unaligned_load, stmt_info,
1114 DR_MISALIGNMENT (dr), vect_body);
1116 if (dump_enabled_p ())
1117 dump_printf_loc (MSG_NOTE, vect_location,
1118 "vect_model_load_cost: unaligned supported by "
1119 "hardware.\n");
1121 break;
1123 case dr_explicit_realign:
1125 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1126 vector_load, stmt_info, 0, vect_body);
1127 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1128 vec_perm, stmt_info, 0, vect_body);
1130 /* FIXME: If the misalignment remains fixed across the iterations of
1131 the containing loop, the following cost should be added to the
1132 prologue costs. */
1133 if (targetm.vectorize.builtin_mask_for_load)
1134 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1135 stmt_info, 0, vect_body);
1137 if (dump_enabled_p ())
1138 dump_printf_loc (MSG_NOTE, vect_location,
1139 "vect_model_load_cost: explicit realign\n");
1141 break;
1143 case dr_explicit_realign_optimized:
1145 if (dump_enabled_p ())
1146 dump_printf_loc (MSG_NOTE, vect_location,
1147 "vect_model_load_cost: unaligned software "
1148 "pipelined.\n");
1150 /* Unaligned software pipeline has a load of an address, an initial
1151 load, and possibly a mask operation to "prime" the loop. However,
1152 if this is an access in a group of loads, which provide grouped
1153 access, then the above cost should only be considered for one
1154 access in the group. Inside the loop, there is a load op
1155 and a realignment op. */
1157 if (add_realign_cost && record_prologue_costs)
1159 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1160 vector_stmt, stmt_info,
1161 0, vect_prologue);
1162 if (targetm.vectorize.builtin_mask_for_load)
1163 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1164 vector_stmt, stmt_info,
1165 0, vect_prologue);
1168 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1169 stmt_info, 0, vect_body);
1170 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1171 stmt_info, 0, vect_body);
1173 if (dump_enabled_p ())
1174 dump_printf_loc (MSG_NOTE, vect_location,
1175 "vect_model_load_cost: explicit realign optimized"
1176 "\n");
1178 break;
1181 case dr_unaligned_unsupported:
1183 *inside_cost = VECT_MAX_COST;
1185 if (dump_enabled_p ())
1186 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1187 "vect_model_load_cost: unsupported access.\n");
1188 break;
1191 default:
1192 gcc_unreachable ();
1196 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1197 the loop preheader for the vectorized stmt STMT. */
1199 static void
1200 vect_init_vector_1 (gimple *stmt, gimple *new_stmt, gimple_stmt_iterator *gsi)
1202 if (gsi)
1203 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1204 else
1206 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1207 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1209 if (loop_vinfo)
1211 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1212 basic_block new_bb;
1213 edge pe;
1215 if (nested_in_vect_loop_p (loop, stmt))
1216 loop = loop->inner;
1218 pe = loop_preheader_edge (loop);
1219 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1220 gcc_assert (!new_bb);
1222 else
1224 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1225 basic_block bb;
1226 gimple_stmt_iterator gsi_bb_start;
1228 gcc_assert (bb_vinfo);
1229 bb = BB_VINFO_BB (bb_vinfo);
1230 gsi_bb_start = gsi_after_labels (bb);
1231 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1235 if (dump_enabled_p ())
1237 dump_printf_loc (MSG_NOTE, vect_location,
1238 "created new init_stmt: ");
1239 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1243 /* Function vect_init_vector.
1245 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1246 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1247 vector type a vector with all elements equal to VAL is created first.
1248 Place the initialization at BSI if it is not NULL. Otherwise, place the
1249 initialization at the loop preheader.
1250 Return the DEF of INIT_STMT.
1251 It will be used in the vectorization of STMT. */
1253 tree
1254 vect_init_vector (gimple *stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1256 gimple *init_stmt;
1257 tree new_temp;
1259 /* We abuse this function to push sth to a SSA name with initial 'val'. */
1260 if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1262 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
1263 if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1265 /* Scalar boolean value should be transformed into
1266 all zeros or all ones value before building a vector. */
1267 if (VECTOR_BOOLEAN_TYPE_P (type))
1269 tree true_val = build_all_ones_cst (TREE_TYPE (type));
1270 tree false_val = build_zero_cst (TREE_TYPE (type));
1272 if (CONSTANT_CLASS_P (val))
1273 val = integer_zerop (val) ? false_val : true_val;
1274 else
1276 new_temp = make_ssa_name (TREE_TYPE (type));
1277 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1278 val, true_val, false_val);
1279 vect_init_vector_1 (stmt, init_stmt, gsi);
1280 val = new_temp;
1283 else if (CONSTANT_CLASS_P (val))
1284 val = fold_convert (TREE_TYPE (type), val);
1285 else
1287 new_temp = make_ssa_name (TREE_TYPE (type));
1288 if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1289 init_stmt = gimple_build_assign (new_temp,
1290 fold_build1 (VIEW_CONVERT_EXPR,
1291 TREE_TYPE (type),
1292 val));
1293 else
1294 init_stmt = gimple_build_assign (new_temp, NOP_EXPR, val);
1295 vect_init_vector_1 (stmt, init_stmt, gsi);
1296 val = new_temp;
1299 val = build_vector_from_val (type, val);
1302 new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1303 init_stmt = gimple_build_assign (new_temp, val);
1304 vect_init_vector_1 (stmt, init_stmt, gsi);
1305 return new_temp;
1308 /* Function vect_get_vec_def_for_operand_1.
1310 For a defining stmt DEF_STMT of a scalar stmt, return a vector def with type
1311 DT that will be used in the vectorized stmt. */
1313 tree
1314 vect_get_vec_def_for_operand_1 (gimple *def_stmt, enum vect_def_type dt)
1316 tree vec_oprnd;
1317 gimple *vec_stmt;
1318 stmt_vec_info def_stmt_info = NULL;
1320 switch (dt)
1322 /* operand is a constant or a loop invariant. */
1323 case vect_constant_def:
1324 case vect_external_def:
1325 /* Code should use vect_get_vec_def_for_operand. */
1326 gcc_unreachable ();
1328 /* operand is defined inside the loop. */
1329 case vect_internal_def:
1331 /* Get the def from the vectorized stmt. */
1332 def_stmt_info = vinfo_for_stmt (def_stmt);
1334 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1335 /* Get vectorized pattern statement. */
1336 if (!vec_stmt
1337 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1338 && !STMT_VINFO_RELEVANT (def_stmt_info))
1339 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1340 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1341 gcc_assert (vec_stmt);
1342 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1343 vec_oprnd = PHI_RESULT (vec_stmt);
1344 else if (is_gimple_call (vec_stmt))
1345 vec_oprnd = gimple_call_lhs (vec_stmt);
1346 else
1347 vec_oprnd = gimple_assign_lhs (vec_stmt);
1348 return vec_oprnd;
1351 /* operand is defined by a loop header phi - reduction */
1352 case vect_reduction_def:
1353 case vect_double_reduction_def:
1354 case vect_nested_cycle:
1355 /* Code should use get_initial_def_for_reduction. */
1356 gcc_unreachable ();
1358 /* operand is defined by loop-header phi - induction. */
1359 case vect_induction_def:
1361 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1363 /* Get the def from the vectorized stmt. */
1364 def_stmt_info = vinfo_for_stmt (def_stmt);
1365 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1366 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1367 vec_oprnd = PHI_RESULT (vec_stmt);
1368 else
1369 vec_oprnd = gimple_get_lhs (vec_stmt);
1370 return vec_oprnd;
1373 default:
1374 gcc_unreachable ();
1379 /* Function vect_get_vec_def_for_operand.
1381 OP is an operand in STMT. This function returns a (vector) def that will be
1382 used in the vectorized stmt for STMT.
1384 In the case that OP is an SSA_NAME which is defined in the loop, then
1385 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1387 In case OP is an invariant or constant, a new stmt that creates a vector def
1388 needs to be introduced. VECTYPE may be used to specify a required type for
1389 vector invariant. */
1391 tree
1392 vect_get_vec_def_for_operand (tree op, gimple *stmt, tree vectype)
1394 gimple *def_stmt;
1395 enum vect_def_type dt;
1396 bool is_simple_use;
1397 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1398 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1400 if (dump_enabled_p ())
1402 dump_printf_loc (MSG_NOTE, vect_location,
1403 "vect_get_vec_def_for_operand: ");
1404 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1405 dump_printf (MSG_NOTE, "\n");
1408 is_simple_use = vect_is_simple_use (op, loop_vinfo, &def_stmt, &dt);
1409 gcc_assert (is_simple_use);
1410 if (def_stmt && dump_enabled_p ())
1412 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1413 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1416 if (dt == vect_constant_def || dt == vect_external_def)
1418 tree stmt_vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1419 tree vector_type;
1421 if (vectype)
1422 vector_type = vectype;
1423 else if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE
1424 && VECTOR_BOOLEAN_TYPE_P (stmt_vectype))
1425 vector_type = build_same_sized_truth_vector_type (stmt_vectype);
1426 else
1427 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1429 gcc_assert (vector_type);
1430 return vect_init_vector (stmt, op, vector_type, NULL);
1432 else
1433 return vect_get_vec_def_for_operand_1 (def_stmt, dt);
1437 /* Function vect_get_vec_def_for_stmt_copy
1439 Return a vector-def for an operand. This function is used when the
1440 vectorized stmt to be created (by the caller to this function) is a "copy"
1441 created in case the vectorized result cannot fit in one vector, and several
1442 copies of the vector-stmt are required. In this case the vector-def is
1443 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1444 of the stmt that defines VEC_OPRND.
1445 DT is the type of the vector def VEC_OPRND.
1447 Context:
1448 In case the vectorization factor (VF) is bigger than the number
1449 of elements that can fit in a vectype (nunits), we have to generate
1450 more than one vector stmt to vectorize the scalar stmt. This situation
1451 arises when there are multiple data-types operated upon in the loop; the
1452 smallest data-type determines the VF, and as a result, when vectorizing
1453 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1454 vector stmt (each computing a vector of 'nunits' results, and together
1455 computing 'VF' results in each iteration). This function is called when
1456 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1457 which VF=16 and nunits=4, so the number of copies required is 4):
1459 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1461 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1462 VS1.1: vx.1 = memref1 VS1.2
1463 VS1.2: vx.2 = memref2 VS1.3
1464 VS1.3: vx.3 = memref3
1466 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1467 VSnew.1: vz1 = vx.1 + ... VSnew.2
1468 VSnew.2: vz2 = vx.2 + ... VSnew.3
1469 VSnew.3: vz3 = vx.3 + ...
1471 The vectorization of S1 is explained in vectorizable_load.
1472 The vectorization of S2:
1473 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1474 the function 'vect_get_vec_def_for_operand' is called to
1475 get the relevant vector-def for each operand of S2. For operand x it
1476 returns the vector-def 'vx.0'.
1478 To create the remaining copies of the vector-stmt (VSnew.j), this
1479 function is called to get the relevant vector-def for each operand. It is
1480 obtained from the respective VS1.j stmt, which is recorded in the
1481 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1483 For example, to obtain the vector-def 'vx.1' in order to create the
1484 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1485 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1486 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1487 and return its def ('vx.1').
1488 Overall, to create the above sequence this function will be called 3 times:
1489 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1490 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1491 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1493 tree
1494 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1496 gimple *vec_stmt_for_operand;
1497 stmt_vec_info def_stmt_info;
1499 /* Do nothing; can reuse same def. */
1500 if (dt == vect_external_def || dt == vect_constant_def )
1501 return vec_oprnd;
1503 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1504 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1505 gcc_assert (def_stmt_info);
1506 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1507 gcc_assert (vec_stmt_for_operand);
1508 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1509 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1510 else
1511 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1512 return vec_oprnd;
1516 /* Get vectorized definitions for the operands to create a copy of an original
1517 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1519 static void
1520 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1521 vec<tree> *vec_oprnds0,
1522 vec<tree> *vec_oprnds1)
1524 tree vec_oprnd = vec_oprnds0->pop ();
1526 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1527 vec_oprnds0->quick_push (vec_oprnd);
1529 if (vec_oprnds1 && vec_oprnds1->length ())
1531 vec_oprnd = vec_oprnds1->pop ();
1532 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1533 vec_oprnds1->quick_push (vec_oprnd);
1538 /* Get vectorized definitions for OP0 and OP1.
1539 REDUC_INDEX is the index of reduction operand in case of reduction,
1540 and -1 otherwise. */
1542 void
1543 vect_get_vec_defs (tree op0, tree op1, gimple *stmt,
1544 vec<tree> *vec_oprnds0,
1545 vec<tree> *vec_oprnds1,
1546 slp_tree slp_node, int reduc_index)
1548 if (slp_node)
1550 int nops = (op1 == NULL_TREE) ? 1 : 2;
1551 auto_vec<tree> ops (nops);
1552 auto_vec<vec<tree> > vec_defs (nops);
1554 ops.quick_push (op0);
1555 if (op1)
1556 ops.quick_push (op1);
1558 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1560 *vec_oprnds0 = vec_defs[0];
1561 if (op1)
1562 *vec_oprnds1 = vec_defs[1];
1564 else
1566 tree vec_oprnd;
1568 vec_oprnds0->create (1);
1569 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt);
1570 vec_oprnds0->quick_push (vec_oprnd);
1572 if (op1)
1574 vec_oprnds1->create (1);
1575 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt);
1576 vec_oprnds1->quick_push (vec_oprnd);
1582 /* Function vect_finish_stmt_generation.
1584 Insert a new stmt. */
1586 void
1587 vect_finish_stmt_generation (gimple *stmt, gimple *vec_stmt,
1588 gimple_stmt_iterator *gsi)
1590 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1591 vec_info *vinfo = stmt_info->vinfo;
1593 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1595 if (!gsi_end_p (*gsi)
1596 && gimple_has_mem_ops (vec_stmt))
1598 gimple *at_stmt = gsi_stmt (*gsi);
1599 tree vuse = gimple_vuse (at_stmt);
1600 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1602 tree vdef = gimple_vdef (at_stmt);
1603 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1604 /* If we have an SSA vuse and insert a store, update virtual
1605 SSA form to avoid triggering the renamer. Do so only
1606 if we can easily see all uses - which is what almost always
1607 happens with the way vectorized stmts are inserted. */
1608 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1609 && ((is_gimple_assign (vec_stmt)
1610 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1611 || (is_gimple_call (vec_stmt)
1612 && !(gimple_call_flags (vec_stmt)
1613 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1615 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1616 gimple_set_vdef (vec_stmt, new_vdef);
1617 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1621 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1623 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, vinfo));
1625 if (dump_enabled_p ())
1627 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1628 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1631 gimple_set_location (vec_stmt, gimple_location (stmt));
1633 /* While EH edges will generally prevent vectorization, stmt might
1634 e.g. be in a must-not-throw region. Ensure newly created stmts
1635 that could throw are part of the same region. */
1636 int lp_nr = lookup_stmt_eh_lp (stmt);
1637 if (lp_nr != 0 && stmt_could_throw_p (vec_stmt))
1638 add_stmt_to_eh_lp (vec_stmt, lp_nr);
1641 /* We want to vectorize a call to combined function CFN with function
1642 decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1643 as the types of all inputs. Check whether this is possible using
1644 an internal function, returning its code if so or IFN_LAST if not. */
1646 static internal_fn
1647 vectorizable_internal_function (combined_fn cfn, tree fndecl,
1648 tree vectype_out, tree vectype_in)
1650 internal_fn ifn;
1651 if (internal_fn_p (cfn))
1652 ifn = as_internal_fn (cfn);
1653 else
1654 ifn = associated_internal_fn (fndecl);
1655 if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1657 const direct_internal_fn_info &info = direct_internal_fn (ifn);
1658 if (info.vectorizable)
1660 tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1661 tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
1662 if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1663 OPTIMIZE_FOR_SPEED))
1664 return ifn;
1667 return IFN_LAST;
1671 static tree permute_vec_elements (tree, tree, tree, gimple *,
1672 gimple_stmt_iterator *);
1674 /* STMT is a non-strided load or store, meaning that it accesses
1675 elements with a known constant step. Return -1 if that step
1676 is negative, 0 if it is zero, and 1 if it is greater than zero. */
1678 static int
1679 compare_step_with_zero (gimple *stmt)
1681 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1682 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1683 tree step;
1684 if (loop_vinfo && nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo), stmt))
1685 step = STMT_VINFO_DR_STEP (stmt_info);
1686 else
1687 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
1688 return tree_int_cst_compare (step, size_zero_node);
1691 /* If the target supports a permute mask that reverses the elements in
1692 a vector of type VECTYPE, return that mask, otherwise return null. */
1694 static tree
1695 perm_mask_for_reverse (tree vectype)
1697 int i, nunits;
1698 unsigned char *sel;
1700 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1701 sel = XALLOCAVEC (unsigned char, nunits);
1703 for (i = 0; i < nunits; ++i)
1704 sel[i] = nunits - 1 - i;
1706 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
1707 return NULL_TREE;
1708 return vect_gen_perm_mask_checked (vectype, sel);
1711 /* A subroutine of get_load_store_type, with a subset of the same
1712 arguments. Handle the case where STMT is part of a grouped load
1713 or store.
1715 For stores, the statements in the group are all consecutive
1716 and there is no gap at the end. For loads, the statements in the
1717 group might not be consecutive; there can be gaps between statements
1718 as well as at the end. */
1720 static bool
1721 get_group_load_store_type (gimple *stmt, tree vectype, bool slp,
1722 vec_load_store_type vls_type,
1723 vect_memory_access_type *memory_access_type)
1725 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1726 vec_info *vinfo = stmt_info->vinfo;
1727 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1728 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
1729 gimple *first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1730 unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
1731 bool single_element_p = (stmt == first_stmt
1732 && !GROUP_NEXT_ELEMENT (stmt_info));
1733 unsigned HOST_WIDE_INT gap = GROUP_GAP (vinfo_for_stmt (first_stmt));
1734 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1736 /* True if the vectorized statements would access beyond the last
1737 statement in the group. */
1738 bool overrun_p = false;
1740 /* True if we can cope with such overrun by peeling for gaps, so that
1741 there is at least one final scalar iteration after the vector loop. */
1742 bool can_overrun_p = (vls_type == VLS_LOAD && loop_vinfo && !loop->inner);
1744 /* There can only be a gap at the end of the group if the stride is
1745 known at compile time. */
1746 gcc_assert (!STMT_VINFO_STRIDED_P (stmt_info) || gap == 0);
1748 /* Stores can't yet have gaps. */
1749 gcc_assert (slp || vls_type == VLS_LOAD || gap == 0);
1751 if (slp)
1753 if (STMT_VINFO_STRIDED_P (stmt_info))
1755 /* Try to use consecutive accesses of GROUP_SIZE elements,
1756 separated by the stride, until we have a complete vector.
1757 Fall back to scalar accesses if that isn't possible. */
1758 if (nunits % group_size == 0)
1759 *memory_access_type = VMAT_STRIDED_SLP;
1760 else
1761 *memory_access_type = VMAT_ELEMENTWISE;
1763 else
1765 overrun_p = loop_vinfo && gap != 0;
1766 if (overrun_p && vls_type != VLS_LOAD)
1768 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1769 "Grouped store with gaps requires"
1770 " non-consecutive accesses\n");
1771 return false;
1773 if (overrun_p && !can_overrun_p)
1775 if (dump_enabled_p ())
1776 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1777 "Peeling for outer loop is not supported\n");
1778 return false;
1780 *memory_access_type = VMAT_CONTIGUOUS;
1783 else
1785 /* We can always handle this case using elementwise accesses,
1786 but see if something more efficient is available. */
1787 *memory_access_type = VMAT_ELEMENTWISE;
1789 /* If there is a gap at the end of the group then these optimizations
1790 would access excess elements in the last iteration. */
1791 bool would_overrun_p = (gap != 0);
1792 if (!STMT_VINFO_STRIDED_P (stmt_info)
1793 && (can_overrun_p || !would_overrun_p)
1794 && compare_step_with_zero (stmt) > 0)
1796 /* First try using LOAD/STORE_LANES. */
1797 if (vls_type == VLS_LOAD
1798 ? vect_load_lanes_supported (vectype, group_size)
1799 : vect_store_lanes_supported (vectype, group_size))
1801 *memory_access_type = VMAT_LOAD_STORE_LANES;
1802 overrun_p = would_overrun_p;
1805 /* If that fails, try using permuting loads. */
1806 if (*memory_access_type == VMAT_ELEMENTWISE
1807 && (vls_type == VLS_LOAD
1808 ? vect_grouped_load_supported (vectype, single_element_p,
1809 group_size)
1810 : vect_grouped_store_supported (vectype, group_size)))
1812 *memory_access_type = VMAT_CONTIGUOUS_PERMUTE;
1813 overrun_p = would_overrun_p;
1818 if (vls_type != VLS_LOAD && first_stmt == stmt)
1820 /* STMT is the leader of the group. Check the operands of all the
1821 stmts of the group. */
1822 gimple *next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
1823 while (next_stmt)
1825 gcc_assert (gimple_assign_single_p (next_stmt));
1826 tree op = gimple_assign_rhs1 (next_stmt);
1827 gimple *def_stmt;
1828 enum vect_def_type dt;
1829 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt))
1831 if (dump_enabled_p ())
1832 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1833 "use not simple.\n");
1834 return false;
1836 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
1840 if (overrun_p)
1842 gcc_assert (can_overrun_p);
1843 if (dump_enabled_p ())
1844 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1845 "Data access with gaps requires scalar "
1846 "epilogue loop\n");
1847 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
1850 return true;
1853 /* A subroutine of get_load_store_type, with a subset of the same
1854 arguments. Handle the case where STMT is a load or store that
1855 accesses consecutive elements with a negative step. */
1857 static vect_memory_access_type
1858 get_negative_load_store_type (gimple *stmt, tree vectype,
1859 vec_load_store_type vls_type,
1860 unsigned int ncopies)
1862 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1863 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1864 dr_alignment_support alignment_support_scheme;
1866 if (ncopies > 1)
1868 if (dump_enabled_p ())
1869 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1870 "multiple types with negative step.\n");
1871 return VMAT_ELEMENTWISE;
1874 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1875 if (alignment_support_scheme != dr_aligned
1876 && alignment_support_scheme != dr_unaligned_supported)
1878 if (dump_enabled_p ())
1879 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1880 "negative step but alignment required.\n");
1881 return VMAT_ELEMENTWISE;
1884 if (vls_type == VLS_STORE_INVARIANT)
1886 if (dump_enabled_p ())
1887 dump_printf_loc (MSG_NOTE, vect_location,
1888 "negative step with invariant source;"
1889 " no permute needed.\n");
1890 return VMAT_CONTIGUOUS_DOWN;
1893 if (!perm_mask_for_reverse (vectype))
1895 if (dump_enabled_p ())
1896 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1897 "negative step and reversing not supported.\n");
1898 return VMAT_ELEMENTWISE;
1901 return VMAT_CONTIGUOUS_REVERSE;
1904 /* Analyze load or store statement STMT of type VLS_TYPE. Return true
1905 if there is a memory access type that the vectorized form can use,
1906 storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
1907 or scatters, fill in GS_INFO accordingly.
1909 SLP says whether we're performing SLP rather than loop vectorization.
1910 VECTYPE is the vector type that the vectorized statements will use.
1911 NCOPIES is the number of vector statements that will be needed. */
1913 static bool
1914 get_load_store_type (gimple *stmt, tree vectype, bool slp,
1915 vec_load_store_type vls_type, unsigned int ncopies,
1916 vect_memory_access_type *memory_access_type,
1917 gather_scatter_info *gs_info)
1919 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1920 vec_info *vinfo = stmt_info->vinfo;
1921 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1922 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1924 *memory_access_type = VMAT_GATHER_SCATTER;
1925 gimple *def_stmt;
1926 if (!vect_check_gather_scatter (stmt, loop_vinfo, gs_info))
1927 gcc_unreachable ();
1928 else if (!vect_is_simple_use (gs_info->offset, vinfo, &def_stmt,
1929 &gs_info->offset_dt,
1930 &gs_info->offset_vectype))
1932 if (dump_enabled_p ())
1933 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1934 "%s index use not simple.\n",
1935 vls_type == VLS_LOAD ? "gather" : "scatter");
1936 return false;
1939 else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1941 if (!get_group_load_store_type (stmt, vectype, slp, vls_type,
1942 memory_access_type))
1943 return false;
1945 else if (STMT_VINFO_STRIDED_P (stmt_info))
1947 gcc_assert (!slp);
1948 *memory_access_type = VMAT_ELEMENTWISE;
1950 else
1952 int cmp = compare_step_with_zero (stmt);
1953 if (cmp < 0)
1954 *memory_access_type = get_negative_load_store_type
1955 (stmt, vectype, vls_type, ncopies);
1956 else if (cmp == 0)
1958 gcc_assert (vls_type == VLS_LOAD);
1959 *memory_access_type = VMAT_INVARIANT;
1961 else
1962 *memory_access_type = VMAT_CONTIGUOUS;
1965 /* FIXME: At the moment the cost model seems to underestimate the
1966 cost of using elementwise accesses. This check preserves the
1967 traditional behavior until that can be fixed. */
1968 if (*memory_access_type == VMAT_ELEMENTWISE
1969 && !STMT_VINFO_STRIDED_P (stmt_info))
1971 if (dump_enabled_p ())
1972 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1973 "not falling back to elementwise accesses\n");
1974 return false;
1976 return true;
1979 /* Function vectorizable_mask_load_store.
1981 Check if STMT performs a conditional load or store that can be vectorized.
1982 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1983 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
1984 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1986 static bool
1987 vectorizable_mask_load_store (gimple *stmt, gimple_stmt_iterator *gsi,
1988 gimple **vec_stmt, slp_tree slp_node)
1990 tree vec_dest = NULL;
1991 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1992 stmt_vec_info prev_stmt_info;
1993 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1994 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1995 bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
1996 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1997 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1998 tree rhs_vectype = NULL_TREE;
1999 tree mask_vectype;
2000 tree elem_type;
2001 gimple *new_stmt;
2002 tree dummy;
2003 tree dataref_ptr = NULL_TREE;
2004 gimple *ptr_incr;
2005 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
2006 int ncopies;
2007 int i, j;
2008 bool inv_p;
2009 gather_scatter_info gs_info;
2010 vec_load_store_type vls_type;
2011 tree mask;
2012 gimple *def_stmt;
2013 enum vect_def_type dt;
2015 if (slp_node != NULL)
2016 return false;
2018 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2019 gcc_assert (ncopies >= 1);
2021 mask = gimple_call_arg (stmt, 2);
2023 if (TREE_CODE (TREE_TYPE (mask)) != BOOLEAN_TYPE)
2024 return false;
2026 /* FORNOW. This restriction should be relaxed. */
2027 if (nested_in_vect_loop && ncopies > 1)
2029 if (dump_enabled_p ())
2030 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2031 "multiple types in nested loop.");
2032 return false;
2035 if (!STMT_VINFO_RELEVANT_P (stmt_info))
2036 return false;
2038 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
2039 && ! vec_stmt)
2040 return false;
2042 if (!STMT_VINFO_DATA_REF (stmt_info))
2043 return false;
2045 elem_type = TREE_TYPE (vectype);
2047 if (TREE_CODE (mask) != SSA_NAME)
2048 return false;
2050 if (!vect_is_simple_use (mask, loop_vinfo, &def_stmt, &dt, &mask_vectype))
2051 return false;
2053 if (!mask_vectype)
2054 mask_vectype = get_mask_type_for_scalar_type (TREE_TYPE (vectype));
2056 if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype)
2057 || TYPE_VECTOR_SUBPARTS (mask_vectype) != TYPE_VECTOR_SUBPARTS (vectype))
2058 return false;
2060 if (gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
2062 tree rhs = gimple_call_arg (stmt, 3);
2063 if (!vect_is_simple_use (rhs, loop_vinfo, &def_stmt, &dt, &rhs_vectype))
2064 return false;
2065 if (dt == vect_constant_def || dt == vect_external_def)
2066 vls_type = VLS_STORE_INVARIANT;
2067 else
2068 vls_type = VLS_STORE;
2070 else
2071 vls_type = VLS_LOAD;
2073 vect_memory_access_type memory_access_type;
2074 if (!get_load_store_type (stmt, vectype, false, vls_type, ncopies,
2075 &memory_access_type, &gs_info))
2076 return false;
2078 if (memory_access_type == VMAT_GATHER_SCATTER)
2080 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
2081 tree masktype
2082 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
2083 if (TREE_CODE (masktype) == INTEGER_TYPE)
2085 if (dump_enabled_p ())
2086 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2087 "masked gather with integer mask not supported.");
2088 return false;
2091 else if (memory_access_type != VMAT_CONTIGUOUS)
2093 if (dump_enabled_p ())
2094 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2095 "unsupported access type for masked %s.\n",
2096 vls_type == VLS_LOAD ? "load" : "store");
2097 return false;
2099 else if (!VECTOR_MODE_P (TYPE_MODE (vectype))
2100 || !can_vec_mask_load_store_p (TYPE_MODE (vectype),
2101 TYPE_MODE (mask_vectype),
2102 vls_type == VLS_LOAD)
2103 || (rhs_vectype
2104 && !useless_type_conversion_p (vectype, rhs_vectype)))
2105 return false;
2107 if (!vec_stmt) /* transformation not required. */
2109 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
2110 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
2111 if (vls_type == VLS_LOAD)
2112 vect_model_load_cost (stmt_info, ncopies, memory_access_type,
2113 NULL, NULL, NULL);
2114 else
2115 vect_model_store_cost (stmt_info, ncopies, memory_access_type,
2116 dt, NULL, NULL, NULL);
2117 return true;
2119 gcc_assert (memory_access_type == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
2121 /** Transform. **/
2123 if (memory_access_type == VMAT_GATHER_SCATTER)
2125 tree vec_oprnd0 = NULL_TREE, op;
2126 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
2127 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
2128 tree ptr, vec_mask = NULL_TREE, mask_op = NULL_TREE, var, scale;
2129 tree perm_mask = NULL_TREE, prev_res = NULL_TREE;
2130 tree mask_perm_mask = NULL_TREE;
2131 edge pe = loop_preheader_edge (loop);
2132 gimple_seq seq;
2133 basic_block new_bb;
2134 enum { NARROW, NONE, WIDEN } modifier;
2135 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
2137 rettype = TREE_TYPE (TREE_TYPE (gs_info.decl));
2138 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2139 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2140 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2141 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2142 scaletype = TREE_VALUE (arglist);
2143 gcc_checking_assert (types_compatible_p (srctype, rettype)
2144 && types_compatible_p (srctype, masktype));
2146 if (nunits == gather_off_nunits)
2147 modifier = NONE;
2148 else if (nunits == gather_off_nunits / 2)
2150 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
2151 modifier = WIDEN;
2153 for (i = 0; i < gather_off_nunits; ++i)
2154 sel[i] = i | nunits;
2156 perm_mask = vect_gen_perm_mask_checked (gs_info.offset_vectype, sel);
2158 else if (nunits == gather_off_nunits * 2)
2160 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
2161 modifier = NARROW;
2163 for (i = 0; i < nunits; ++i)
2164 sel[i] = i < gather_off_nunits
2165 ? i : i + nunits - gather_off_nunits;
2167 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
2168 ncopies *= 2;
2169 for (i = 0; i < nunits; ++i)
2170 sel[i] = i | gather_off_nunits;
2171 mask_perm_mask = vect_gen_perm_mask_checked (masktype, sel);
2173 else
2174 gcc_unreachable ();
2176 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
2178 ptr = fold_convert (ptrtype, gs_info.base);
2179 if (!is_gimple_min_invariant (ptr))
2181 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
2182 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
2183 gcc_assert (!new_bb);
2186 scale = build_int_cst (scaletype, gs_info.scale);
2188 prev_stmt_info = NULL;
2189 for (j = 0; j < ncopies; ++j)
2191 if (modifier == WIDEN && (j & 1))
2192 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
2193 perm_mask, stmt, gsi);
2194 else if (j == 0)
2195 op = vec_oprnd0
2196 = vect_get_vec_def_for_operand (gs_info.offset, stmt);
2197 else
2198 op = vec_oprnd0
2199 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt, vec_oprnd0);
2201 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
2203 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
2204 == TYPE_VECTOR_SUBPARTS (idxtype));
2205 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
2206 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
2207 new_stmt
2208 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2209 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2210 op = var;
2213 if (mask_perm_mask && (j & 1))
2214 mask_op = permute_vec_elements (mask_op, mask_op,
2215 mask_perm_mask, stmt, gsi);
2216 else
2218 if (j == 0)
2219 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2220 else
2222 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
2223 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2226 mask_op = vec_mask;
2227 if (!useless_type_conversion_p (masktype, TREE_TYPE (vec_mask)))
2229 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask_op))
2230 == TYPE_VECTOR_SUBPARTS (masktype));
2231 var = vect_get_new_ssa_name (masktype, vect_simple_var);
2232 mask_op = build1 (VIEW_CONVERT_EXPR, masktype, mask_op);
2233 new_stmt
2234 = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_op);
2235 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2236 mask_op = var;
2240 new_stmt
2241 = gimple_build_call (gs_info.decl, 5, mask_op, ptr, op, mask_op,
2242 scale);
2244 if (!useless_type_conversion_p (vectype, rettype))
2246 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
2247 == TYPE_VECTOR_SUBPARTS (rettype));
2248 op = vect_get_new_ssa_name (rettype, vect_simple_var);
2249 gimple_call_set_lhs (new_stmt, op);
2250 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2251 var = make_ssa_name (vec_dest);
2252 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
2253 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2255 else
2257 var = make_ssa_name (vec_dest, new_stmt);
2258 gimple_call_set_lhs (new_stmt, var);
2261 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2263 if (modifier == NARROW)
2265 if ((j & 1) == 0)
2267 prev_res = var;
2268 continue;
2270 var = permute_vec_elements (prev_res, var,
2271 perm_mask, stmt, gsi);
2272 new_stmt = SSA_NAME_DEF_STMT (var);
2275 if (prev_stmt_info == NULL)
2276 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2277 else
2278 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2279 prev_stmt_info = vinfo_for_stmt (new_stmt);
2282 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
2283 from the IL. */
2284 if (STMT_VINFO_RELATED_STMT (stmt_info))
2286 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
2287 stmt_info = vinfo_for_stmt (stmt);
2289 tree lhs = gimple_call_lhs (stmt);
2290 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2291 set_vinfo_for_stmt (new_stmt, stmt_info);
2292 set_vinfo_for_stmt (stmt, NULL);
2293 STMT_VINFO_STMT (stmt_info) = new_stmt;
2294 gsi_replace (gsi, new_stmt, true);
2295 return true;
2297 else if (vls_type != VLS_LOAD)
2299 tree vec_rhs = NULL_TREE, vec_mask = NULL_TREE;
2300 prev_stmt_info = NULL;
2301 LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
2302 for (i = 0; i < ncopies; i++)
2304 unsigned align, misalign;
2306 if (i == 0)
2308 tree rhs = gimple_call_arg (stmt, 3);
2309 vec_rhs = vect_get_vec_def_for_operand (rhs, stmt);
2310 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2311 /* We should have catched mismatched types earlier. */
2312 gcc_assert (useless_type_conversion_p (vectype,
2313 TREE_TYPE (vec_rhs)));
2314 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2315 NULL_TREE, &dummy, gsi,
2316 &ptr_incr, false, &inv_p);
2317 gcc_assert (!inv_p);
2319 else
2321 vect_is_simple_use (vec_rhs, loop_vinfo, &def_stmt, &dt);
2322 vec_rhs = vect_get_vec_def_for_stmt_copy (dt, vec_rhs);
2323 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
2324 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2325 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2326 TYPE_SIZE_UNIT (vectype));
2329 align = TYPE_ALIGN_UNIT (vectype);
2330 if (aligned_access_p (dr))
2331 misalign = 0;
2332 else if (DR_MISALIGNMENT (dr) == -1)
2334 align = TYPE_ALIGN_UNIT (elem_type);
2335 misalign = 0;
2337 else
2338 misalign = DR_MISALIGNMENT (dr);
2339 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2340 misalign);
2341 tree ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)),
2342 misalign ? least_bit_hwi (misalign) : align);
2343 new_stmt
2344 = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
2345 ptr, vec_mask, vec_rhs);
2346 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2347 if (i == 0)
2348 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2349 else
2350 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2351 prev_stmt_info = vinfo_for_stmt (new_stmt);
2354 else
2356 tree vec_mask = NULL_TREE;
2357 prev_stmt_info = NULL;
2358 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
2359 for (i = 0; i < ncopies; i++)
2361 unsigned align, misalign;
2363 if (i == 0)
2365 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2366 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2367 NULL_TREE, &dummy, gsi,
2368 &ptr_incr, false, &inv_p);
2369 gcc_assert (!inv_p);
2371 else
2373 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
2374 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2375 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2376 TYPE_SIZE_UNIT (vectype));
2379 align = TYPE_ALIGN_UNIT (vectype);
2380 if (aligned_access_p (dr))
2381 misalign = 0;
2382 else if (DR_MISALIGNMENT (dr) == -1)
2384 align = TYPE_ALIGN_UNIT (elem_type);
2385 misalign = 0;
2387 else
2388 misalign = DR_MISALIGNMENT (dr);
2389 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2390 misalign);
2391 tree ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)),
2392 misalign ? least_bit_hwi (misalign) : align);
2393 new_stmt
2394 = gimple_build_call_internal (IFN_MASK_LOAD, 3, dataref_ptr,
2395 ptr, vec_mask);
2396 gimple_call_set_lhs (new_stmt, make_ssa_name (vec_dest));
2397 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2398 if (i == 0)
2399 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2400 else
2401 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2402 prev_stmt_info = vinfo_for_stmt (new_stmt);
2406 if (vls_type == VLS_LOAD)
2408 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
2409 from the IL. */
2410 if (STMT_VINFO_RELATED_STMT (stmt_info))
2412 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
2413 stmt_info = vinfo_for_stmt (stmt);
2415 tree lhs = gimple_call_lhs (stmt);
2416 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2417 set_vinfo_for_stmt (new_stmt, stmt_info);
2418 set_vinfo_for_stmt (stmt, NULL);
2419 STMT_VINFO_STMT (stmt_info) = new_stmt;
2420 gsi_replace (gsi, new_stmt, true);
2423 return true;
2426 /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
2427 integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
2428 in a single step. On success, store the binary pack code in
2429 *CONVERT_CODE. */
2431 static bool
2432 simple_integer_narrowing (tree vectype_out, tree vectype_in,
2433 tree_code *convert_code)
2435 if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
2436 || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
2437 return false;
2439 tree_code code;
2440 int multi_step_cvt = 0;
2441 auto_vec <tree, 8> interm_types;
2442 if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
2443 &code, &multi_step_cvt,
2444 &interm_types)
2445 || multi_step_cvt)
2446 return false;
2448 *convert_code = code;
2449 return true;
2452 /* Function vectorizable_call.
2454 Check if GS performs a function call that can be vectorized.
2455 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2456 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2457 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2459 static bool
2460 vectorizable_call (gimple *gs, gimple_stmt_iterator *gsi, gimple **vec_stmt,
2461 slp_tree slp_node)
2463 gcall *stmt;
2464 tree vec_dest;
2465 tree scalar_dest;
2466 tree op, type;
2467 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2468 stmt_vec_info stmt_info = vinfo_for_stmt (gs), prev_stmt_info;
2469 tree vectype_out, vectype_in;
2470 int nunits_in;
2471 int nunits_out;
2472 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2473 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2474 vec_info *vinfo = stmt_info->vinfo;
2475 tree fndecl, new_temp, rhs_type;
2476 gimple *def_stmt;
2477 enum vect_def_type dt[3]
2478 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
2479 gimple *new_stmt = NULL;
2480 int ncopies, j;
2481 vec<tree> vargs = vNULL;
2482 enum { NARROW, NONE, WIDEN } modifier;
2483 size_t i, nargs;
2484 tree lhs;
2486 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2487 return false;
2489 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
2490 && ! vec_stmt)
2491 return false;
2493 /* Is GS a vectorizable call? */
2494 stmt = dyn_cast <gcall *> (gs);
2495 if (!stmt)
2496 return false;
2498 if (gimple_call_internal_p (stmt)
2499 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2500 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
2501 return vectorizable_mask_load_store (stmt, gsi, vec_stmt,
2502 slp_node);
2504 if (gimple_call_lhs (stmt) == NULL_TREE
2505 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2506 return false;
2508 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2510 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2512 /* Process function arguments. */
2513 rhs_type = NULL_TREE;
2514 vectype_in = NULL_TREE;
2515 nargs = gimple_call_num_args (stmt);
2517 /* Bail out if the function has more than three arguments, we do not have
2518 interesting builtin functions to vectorize with more than two arguments
2519 except for fma. No arguments is also not good. */
2520 if (nargs == 0 || nargs > 3)
2521 return false;
2523 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
2524 if (gimple_call_internal_p (stmt)
2525 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2527 nargs = 0;
2528 rhs_type = unsigned_type_node;
2531 for (i = 0; i < nargs; i++)
2533 tree opvectype;
2535 op = gimple_call_arg (stmt, i);
2537 /* We can only handle calls with arguments of the same type. */
2538 if (rhs_type
2539 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
2541 if (dump_enabled_p ())
2542 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2543 "argument types differ.\n");
2544 return false;
2546 if (!rhs_type)
2547 rhs_type = TREE_TYPE (op);
2549 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt[i], &opvectype))
2551 if (dump_enabled_p ())
2552 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2553 "use not simple.\n");
2554 return false;
2557 if (!vectype_in)
2558 vectype_in = opvectype;
2559 else if (opvectype
2560 && opvectype != vectype_in)
2562 if (dump_enabled_p ())
2563 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2564 "argument vector types differ.\n");
2565 return false;
2568 /* If all arguments are external or constant defs use a vector type with
2569 the same size as the output vector type. */
2570 if (!vectype_in)
2571 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
2572 if (vec_stmt)
2573 gcc_assert (vectype_in);
2574 if (!vectype_in)
2576 if (dump_enabled_p ())
2578 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2579 "no vectype for scalar type ");
2580 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
2581 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2584 return false;
2587 /* FORNOW */
2588 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
2589 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
2590 if (nunits_in == nunits_out / 2)
2591 modifier = NARROW;
2592 else if (nunits_out == nunits_in)
2593 modifier = NONE;
2594 else if (nunits_out == nunits_in / 2)
2595 modifier = WIDEN;
2596 else
2597 return false;
2599 /* We only handle functions that do not read or clobber memory. */
2600 if (gimple_vuse (stmt))
2602 if (dump_enabled_p ())
2603 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2604 "function reads from or writes to memory.\n");
2605 return false;
2608 /* For now, we only vectorize functions if a target specific builtin
2609 is available. TODO -- in some cases, it might be profitable to
2610 insert the calls for pieces of the vector, in order to be able
2611 to vectorize other operations in the loop. */
2612 fndecl = NULL_TREE;
2613 internal_fn ifn = IFN_LAST;
2614 combined_fn cfn = gimple_call_combined_fn (stmt);
2615 tree callee = gimple_call_fndecl (stmt);
2617 /* First try using an internal function. */
2618 tree_code convert_code = ERROR_MARK;
2619 if (cfn != CFN_LAST
2620 && (modifier == NONE
2621 || (modifier == NARROW
2622 && simple_integer_narrowing (vectype_out, vectype_in,
2623 &convert_code))))
2624 ifn = vectorizable_internal_function (cfn, callee, vectype_out,
2625 vectype_in);
2627 /* If that fails, try asking for a target-specific built-in function. */
2628 if (ifn == IFN_LAST)
2630 if (cfn != CFN_LAST)
2631 fndecl = targetm.vectorize.builtin_vectorized_function
2632 (cfn, vectype_out, vectype_in);
2633 else
2634 fndecl = targetm.vectorize.builtin_md_vectorized_function
2635 (callee, vectype_out, vectype_in);
2638 if (ifn == IFN_LAST && !fndecl)
2640 if (cfn == CFN_GOMP_SIMD_LANE
2641 && !slp_node
2642 && loop_vinfo
2643 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2644 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
2645 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2646 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
2648 /* We can handle IFN_GOMP_SIMD_LANE by returning a
2649 { 0, 1, 2, ... vf - 1 } vector. */
2650 gcc_assert (nargs == 0);
2652 else
2654 if (dump_enabled_p ())
2655 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2656 "function is not vectorizable.\n");
2657 return false;
2661 if (slp_node)
2662 ncopies = 1;
2663 else if (modifier == NARROW && ifn == IFN_LAST)
2664 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
2665 else
2666 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
2668 /* Sanity check: make sure that at least one copy of the vectorized stmt
2669 needs to be generated. */
2670 gcc_assert (ncopies >= 1);
2672 if (!vec_stmt) /* transformation not required. */
2674 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
2675 if (dump_enabled_p ())
2676 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
2677 "\n");
2678 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
2679 if (ifn != IFN_LAST && modifier == NARROW && !slp_node)
2680 add_stmt_cost (stmt_info->vinfo->target_cost_data, ncopies / 2,
2681 vec_promote_demote, stmt_info, 0, vect_body);
2683 return true;
2686 /** Transform. **/
2688 if (dump_enabled_p ())
2689 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2691 /* Handle def. */
2692 scalar_dest = gimple_call_lhs (stmt);
2693 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
2695 prev_stmt_info = NULL;
2696 if (modifier == NONE || ifn != IFN_LAST)
2698 tree prev_res = NULL_TREE;
2699 for (j = 0; j < ncopies; ++j)
2701 /* Build argument list for the vectorized call. */
2702 if (j == 0)
2703 vargs.create (nargs);
2704 else
2705 vargs.truncate (0);
2707 if (slp_node)
2709 auto_vec<vec<tree> > vec_defs (nargs);
2710 vec<tree> vec_oprnds0;
2712 for (i = 0; i < nargs; i++)
2713 vargs.quick_push (gimple_call_arg (stmt, i));
2714 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2715 vec_oprnds0 = vec_defs[0];
2717 /* Arguments are ready. Create the new vector stmt. */
2718 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
2720 size_t k;
2721 for (k = 0; k < nargs; k++)
2723 vec<tree> vec_oprndsk = vec_defs[k];
2724 vargs[k] = vec_oprndsk[i];
2726 if (modifier == NARROW)
2728 tree half_res = make_ssa_name (vectype_in);
2729 new_stmt = gimple_build_call_internal_vec (ifn, vargs);
2730 gimple_call_set_lhs (new_stmt, half_res);
2731 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2732 if ((i & 1) == 0)
2734 prev_res = half_res;
2735 continue;
2737 new_temp = make_ssa_name (vec_dest);
2738 new_stmt = gimple_build_assign (new_temp, convert_code,
2739 prev_res, half_res);
2741 else
2743 if (ifn != IFN_LAST)
2744 new_stmt = gimple_build_call_internal_vec (ifn, vargs);
2745 else
2746 new_stmt = gimple_build_call_vec (fndecl, vargs);
2747 new_temp = make_ssa_name (vec_dest, new_stmt);
2748 gimple_call_set_lhs (new_stmt, new_temp);
2750 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2751 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2754 for (i = 0; i < nargs; i++)
2756 vec<tree> vec_oprndsi = vec_defs[i];
2757 vec_oprndsi.release ();
2759 continue;
2762 for (i = 0; i < nargs; i++)
2764 op = gimple_call_arg (stmt, i);
2765 if (j == 0)
2766 vec_oprnd0
2767 = vect_get_vec_def_for_operand (op, stmt);
2768 else
2770 vec_oprnd0 = gimple_call_arg (new_stmt, i);
2771 vec_oprnd0
2772 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2775 vargs.quick_push (vec_oprnd0);
2778 if (gimple_call_internal_p (stmt)
2779 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2781 tree *v = XALLOCAVEC (tree, nunits_out);
2782 int k;
2783 for (k = 0; k < nunits_out; ++k)
2784 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
2785 tree cst = build_vector (vectype_out, v);
2786 tree new_var
2787 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
2788 gimple *init_stmt = gimple_build_assign (new_var, cst);
2789 vect_init_vector_1 (stmt, init_stmt, NULL);
2790 new_temp = make_ssa_name (vec_dest);
2791 new_stmt = gimple_build_assign (new_temp, new_var);
2793 else if (modifier == NARROW)
2795 tree half_res = make_ssa_name (vectype_in);
2796 new_stmt = gimple_build_call_internal_vec (ifn, vargs);
2797 gimple_call_set_lhs (new_stmt, half_res);
2798 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2799 if ((j & 1) == 0)
2801 prev_res = half_res;
2802 continue;
2804 new_temp = make_ssa_name (vec_dest);
2805 new_stmt = gimple_build_assign (new_temp, convert_code,
2806 prev_res, half_res);
2808 else
2810 if (ifn != IFN_LAST)
2811 new_stmt = gimple_build_call_internal_vec (ifn, vargs);
2812 else
2813 new_stmt = gimple_build_call_vec (fndecl, vargs);
2814 new_temp = make_ssa_name (vec_dest, new_stmt);
2815 gimple_call_set_lhs (new_stmt, new_temp);
2817 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2819 if (j == (modifier == NARROW ? 1 : 0))
2820 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2821 else
2822 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2824 prev_stmt_info = vinfo_for_stmt (new_stmt);
2827 else if (modifier == NARROW)
2829 for (j = 0; j < ncopies; ++j)
2831 /* Build argument list for the vectorized call. */
2832 if (j == 0)
2833 vargs.create (nargs * 2);
2834 else
2835 vargs.truncate (0);
2837 if (slp_node)
2839 auto_vec<vec<tree> > vec_defs (nargs);
2840 vec<tree> vec_oprnds0;
2842 for (i = 0; i < nargs; i++)
2843 vargs.quick_push (gimple_call_arg (stmt, i));
2844 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2845 vec_oprnds0 = vec_defs[0];
2847 /* Arguments are ready. Create the new vector stmt. */
2848 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2850 size_t k;
2851 vargs.truncate (0);
2852 for (k = 0; k < nargs; k++)
2854 vec<tree> vec_oprndsk = vec_defs[k];
2855 vargs.quick_push (vec_oprndsk[i]);
2856 vargs.quick_push (vec_oprndsk[i + 1]);
2858 if (ifn != IFN_LAST)
2859 new_stmt = gimple_build_call_internal_vec (ifn, vargs);
2860 else
2861 new_stmt = gimple_build_call_vec (fndecl, vargs);
2862 new_temp = make_ssa_name (vec_dest, new_stmt);
2863 gimple_call_set_lhs (new_stmt, new_temp);
2864 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2865 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2868 for (i = 0; i < nargs; i++)
2870 vec<tree> vec_oprndsi = vec_defs[i];
2871 vec_oprndsi.release ();
2873 continue;
2876 for (i = 0; i < nargs; i++)
2878 op = gimple_call_arg (stmt, i);
2879 if (j == 0)
2881 vec_oprnd0
2882 = vect_get_vec_def_for_operand (op, stmt);
2883 vec_oprnd1
2884 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2886 else
2888 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2889 vec_oprnd0
2890 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2891 vec_oprnd1
2892 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2895 vargs.quick_push (vec_oprnd0);
2896 vargs.quick_push (vec_oprnd1);
2899 new_stmt = gimple_build_call_vec (fndecl, vargs);
2900 new_temp = make_ssa_name (vec_dest, new_stmt);
2901 gimple_call_set_lhs (new_stmt, new_temp);
2902 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2904 if (j == 0)
2905 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2906 else
2907 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2909 prev_stmt_info = vinfo_for_stmt (new_stmt);
2912 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2914 else
2915 /* No current target implements this case. */
2916 return false;
2918 vargs.release ();
2920 /* The call in STMT might prevent it from being removed in dce.
2921 We however cannot remove it here, due to the way the ssa name
2922 it defines is mapped to the new definition. So just replace
2923 rhs of the statement with something harmless. */
2925 if (slp_node)
2926 return true;
2928 type = TREE_TYPE (scalar_dest);
2929 if (is_pattern_stmt_p (stmt_info))
2930 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2931 else
2932 lhs = gimple_call_lhs (stmt);
2934 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2935 set_vinfo_for_stmt (new_stmt, stmt_info);
2936 set_vinfo_for_stmt (stmt, NULL);
2937 STMT_VINFO_STMT (stmt_info) = new_stmt;
2938 gsi_replace (gsi, new_stmt, false);
2940 return true;
2944 struct simd_call_arg_info
2946 tree vectype;
2947 tree op;
2948 enum vect_def_type dt;
2949 HOST_WIDE_INT linear_step;
2950 unsigned int align;
2951 bool simd_lane_linear;
2954 /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
2955 is linear within simd lane (but not within whole loop), note it in
2956 *ARGINFO. */
2958 static void
2959 vect_simd_lane_linear (tree op, struct loop *loop,
2960 struct simd_call_arg_info *arginfo)
2962 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
2964 if (!is_gimple_assign (def_stmt)
2965 || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
2966 || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
2967 return;
2969 tree base = gimple_assign_rhs1 (def_stmt);
2970 HOST_WIDE_INT linear_step = 0;
2971 tree v = gimple_assign_rhs2 (def_stmt);
2972 while (TREE_CODE (v) == SSA_NAME)
2974 tree t;
2975 def_stmt = SSA_NAME_DEF_STMT (v);
2976 if (is_gimple_assign (def_stmt))
2977 switch (gimple_assign_rhs_code (def_stmt))
2979 case PLUS_EXPR:
2980 t = gimple_assign_rhs2 (def_stmt);
2981 if (linear_step || TREE_CODE (t) != INTEGER_CST)
2982 return;
2983 base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
2984 v = gimple_assign_rhs1 (def_stmt);
2985 continue;
2986 case MULT_EXPR:
2987 t = gimple_assign_rhs2 (def_stmt);
2988 if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
2989 return;
2990 linear_step = tree_to_shwi (t);
2991 v = gimple_assign_rhs1 (def_stmt);
2992 continue;
2993 CASE_CONVERT:
2994 t = gimple_assign_rhs1 (def_stmt);
2995 if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
2996 || (TYPE_PRECISION (TREE_TYPE (v))
2997 < TYPE_PRECISION (TREE_TYPE (t))))
2998 return;
2999 if (!linear_step)
3000 linear_step = 1;
3001 v = t;
3002 continue;
3003 default:
3004 return;
3006 else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
3007 && loop->simduid
3008 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
3009 && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
3010 == loop->simduid))
3012 if (!linear_step)
3013 linear_step = 1;
3014 arginfo->linear_step = linear_step;
3015 arginfo->op = base;
3016 arginfo->simd_lane_linear = true;
3017 return;
3022 /* Function vectorizable_simd_clone_call.
3024 Check if STMT performs a function call that can be vectorized
3025 by calling a simd clone of the function.
3026 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3027 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3028 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3030 static bool
3031 vectorizable_simd_clone_call (gimple *stmt, gimple_stmt_iterator *gsi,
3032 gimple **vec_stmt, slp_tree slp_node)
3034 tree vec_dest;
3035 tree scalar_dest;
3036 tree op, type;
3037 tree vec_oprnd0 = NULL_TREE;
3038 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
3039 tree vectype;
3040 unsigned int nunits;
3041 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3042 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3043 vec_info *vinfo = stmt_info->vinfo;
3044 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
3045 tree fndecl, new_temp;
3046 gimple *def_stmt;
3047 gimple *new_stmt = NULL;
3048 int ncopies, j;
3049 auto_vec<simd_call_arg_info> arginfo;
3050 vec<tree> vargs = vNULL;
3051 size_t i, nargs;
3052 tree lhs, rtype, ratype;
3053 vec<constructor_elt, va_gc> *ret_ctor_elts;
3055 /* Is STMT a vectorizable call? */
3056 if (!is_gimple_call (stmt))
3057 return false;
3059 fndecl = gimple_call_fndecl (stmt);
3060 if (fndecl == NULL_TREE)
3061 return false;
3063 struct cgraph_node *node = cgraph_node::get (fndecl);
3064 if (node == NULL || node->simd_clones == NULL)
3065 return false;
3067 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3068 return false;
3070 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3071 && ! vec_stmt)
3072 return false;
3074 if (gimple_call_lhs (stmt)
3075 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3076 return false;
3078 gcc_checking_assert (!stmt_can_throw_internal (stmt));
3080 vectype = STMT_VINFO_VECTYPE (stmt_info);
3082 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
3083 return false;
3085 /* FORNOW */
3086 if (slp_node)
3087 return false;
3089 /* Process function arguments. */
3090 nargs = gimple_call_num_args (stmt);
3092 /* Bail out if the function has zero arguments. */
3093 if (nargs == 0)
3094 return false;
3096 arginfo.reserve (nargs, true);
3098 for (i = 0; i < nargs; i++)
3100 simd_call_arg_info thisarginfo;
3101 affine_iv iv;
3103 thisarginfo.linear_step = 0;
3104 thisarginfo.align = 0;
3105 thisarginfo.op = NULL_TREE;
3106 thisarginfo.simd_lane_linear = false;
3108 op = gimple_call_arg (stmt, i);
3109 if (!vect_is_simple_use (op, vinfo, &def_stmt, &thisarginfo.dt,
3110 &thisarginfo.vectype)
3111 || thisarginfo.dt == vect_uninitialized_def)
3113 if (dump_enabled_p ())
3114 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3115 "use not simple.\n");
3116 return false;
3119 if (thisarginfo.dt == vect_constant_def
3120 || thisarginfo.dt == vect_external_def)
3121 gcc_assert (thisarginfo.vectype == NULL_TREE);
3122 else
3123 gcc_assert (thisarginfo.vectype != NULL_TREE);
3125 /* For linear arguments, the analyze phase should have saved
3126 the base and step in STMT_VINFO_SIMD_CLONE_INFO. */
3127 if (i * 3 + 4 <= STMT_VINFO_SIMD_CLONE_INFO (stmt_info).length ()
3128 && STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2])
3130 gcc_assert (vec_stmt);
3131 thisarginfo.linear_step
3132 = tree_to_shwi (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2]);
3133 thisarginfo.op
3134 = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 1];
3135 thisarginfo.simd_lane_linear
3136 = (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 3]
3137 == boolean_true_node);
3138 /* If loop has been peeled for alignment, we need to adjust it. */
3139 tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
3140 tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
3141 if (n1 != n2 && !thisarginfo.simd_lane_linear)
3143 tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
3144 tree step = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2];
3145 tree opt = TREE_TYPE (thisarginfo.op);
3146 bias = fold_convert (TREE_TYPE (step), bias);
3147 bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
3148 thisarginfo.op
3149 = fold_build2 (POINTER_TYPE_P (opt)
3150 ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
3151 thisarginfo.op, bias);
3154 else if (!vec_stmt
3155 && thisarginfo.dt != vect_constant_def
3156 && thisarginfo.dt != vect_external_def
3157 && loop_vinfo
3158 && TREE_CODE (op) == SSA_NAME
3159 && simple_iv (loop, loop_containing_stmt (stmt), op,
3160 &iv, false)
3161 && tree_fits_shwi_p (iv.step))
3163 thisarginfo.linear_step = tree_to_shwi (iv.step);
3164 thisarginfo.op = iv.base;
3166 else if ((thisarginfo.dt == vect_constant_def
3167 || thisarginfo.dt == vect_external_def)
3168 && POINTER_TYPE_P (TREE_TYPE (op)))
3169 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
3170 /* Addresses of array elements indexed by GOMP_SIMD_LANE are
3171 linear too. */
3172 if (POINTER_TYPE_P (TREE_TYPE (op))
3173 && !thisarginfo.linear_step
3174 && !vec_stmt
3175 && thisarginfo.dt != vect_constant_def
3176 && thisarginfo.dt != vect_external_def
3177 && loop_vinfo
3178 && !slp_node
3179 && TREE_CODE (op) == SSA_NAME)
3180 vect_simd_lane_linear (op, loop, &thisarginfo);
3182 arginfo.quick_push (thisarginfo);
3185 unsigned int badness = 0;
3186 struct cgraph_node *bestn = NULL;
3187 if (STMT_VINFO_SIMD_CLONE_INFO (stmt_info).exists ())
3188 bestn = cgraph_node::get (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[0]);
3189 else
3190 for (struct cgraph_node *n = node->simd_clones; n != NULL;
3191 n = n->simdclone->next_clone)
3193 unsigned int this_badness = 0;
3194 if (n->simdclone->simdlen
3195 > (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo)
3196 || n->simdclone->nargs != nargs)
3197 continue;
3198 if (n->simdclone->simdlen
3199 < (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo))
3200 this_badness += (exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
3201 - exact_log2 (n->simdclone->simdlen)) * 1024;
3202 if (n->simdclone->inbranch)
3203 this_badness += 2048;
3204 int target_badness = targetm.simd_clone.usable (n);
3205 if (target_badness < 0)
3206 continue;
3207 this_badness += target_badness * 512;
3208 /* FORNOW: Have to add code to add the mask argument. */
3209 if (n->simdclone->inbranch)
3210 continue;
3211 for (i = 0; i < nargs; i++)
3213 switch (n->simdclone->args[i].arg_type)
3215 case SIMD_CLONE_ARG_TYPE_VECTOR:
3216 if (!useless_type_conversion_p
3217 (n->simdclone->args[i].orig_type,
3218 TREE_TYPE (gimple_call_arg (stmt, i))))
3219 i = -1;
3220 else if (arginfo[i].dt == vect_constant_def
3221 || arginfo[i].dt == vect_external_def
3222 || arginfo[i].linear_step)
3223 this_badness += 64;
3224 break;
3225 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3226 if (arginfo[i].dt != vect_constant_def
3227 && arginfo[i].dt != vect_external_def)
3228 i = -1;
3229 break;
3230 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
3231 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
3232 if (arginfo[i].dt == vect_constant_def
3233 || arginfo[i].dt == vect_external_def
3234 || (arginfo[i].linear_step
3235 != n->simdclone->args[i].linear_step))
3236 i = -1;
3237 break;
3238 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
3239 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
3240 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
3241 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
3242 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
3243 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
3244 /* FORNOW */
3245 i = -1;
3246 break;
3247 case SIMD_CLONE_ARG_TYPE_MASK:
3248 gcc_unreachable ();
3250 if (i == (size_t) -1)
3251 break;
3252 if (n->simdclone->args[i].alignment > arginfo[i].align)
3254 i = -1;
3255 break;
3257 if (arginfo[i].align)
3258 this_badness += (exact_log2 (arginfo[i].align)
3259 - exact_log2 (n->simdclone->args[i].alignment));
3261 if (i == (size_t) -1)
3262 continue;
3263 if (bestn == NULL || this_badness < badness)
3265 bestn = n;
3266 badness = this_badness;
3270 if (bestn == NULL)
3271 return false;
3273 for (i = 0; i < nargs; i++)
3274 if ((arginfo[i].dt == vect_constant_def
3275 || arginfo[i].dt == vect_external_def)
3276 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
3278 arginfo[i].vectype
3279 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
3280 i)));
3281 if (arginfo[i].vectype == NULL
3282 || (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
3283 > bestn->simdclone->simdlen))
3284 return false;
3287 fndecl = bestn->decl;
3288 nunits = bestn->simdclone->simdlen;
3289 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3291 /* If the function isn't const, only allow it in simd loops where user
3292 has asserted that at least nunits consecutive iterations can be
3293 performed using SIMD instructions. */
3294 if ((loop == NULL || (unsigned) loop->safelen < nunits)
3295 && gimple_vuse (stmt))
3296 return false;
3298 /* Sanity check: make sure that at least one copy of the vectorized stmt
3299 needs to be generated. */
3300 gcc_assert (ncopies >= 1);
3302 if (!vec_stmt) /* transformation not required. */
3304 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (bestn->decl);
3305 for (i = 0; i < nargs; i++)
3306 if ((bestn->simdclone->args[i].arg_type
3307 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
3308 || (bestn->simdclone->args[i].arg_type
3309 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP))
3311 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_grow_cleared (i * 3
3312 + 1);
3313 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (arginfo[i].op);
3314 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
3315 ? size_type_node : TREE_TYPE (arginfo[i].op);
3316 tree ls = build_int_cst (lst, arginfo[i].linear_step);
3317 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (ls);
3318 tree sll = arginfo[i].simd_lane_linear
3319 ? boolean_true_node : boolean_false_node;
3320 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (sll);
3322 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
3323 if (dump_enabled_p ())
3324 dump_printf_loc (MSG_NOTE, vect_location,
3325 "=== vectorizable_simd_clone_call ===\n");
3326 /* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
3327 return true;
3330 /** Transform. **/
3332 if (dump_enabled_p ())
3333 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3335 /* Handle def. */
3336 scalar_dest = gimple_call_lhs (stmt);
3337 vec_dest = NULL_TREE;
3338 rtype = NULL_TREE;
3339 ratype = NULL_TREE;
3340 if (scalar_dest)
3342 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3343 rtype = TREE_TYPE (TREE_TYPE (fndecl));
3344 if (TREE_CODE (rtype) == ARRAY_TYPE)
3346 ratype = rtype;
3347 rtype = TREE_TYPE (ratype);
3351 prev_stmt_info = NULL;
3352 for (j = 0; j < ncopies; ++j)
3354 /* Build argument list for the vectorized call. */
3355 if (j == 0)
3356 vargs.create (nargs);
3357 else
3358 vargs.truncate (0);
3360 for (i = 0; i < nargs; i++)
3362 unsigned int k, l, m, o;
3363 tree atype;
3364 op = gimple_call_arg (stmt, i);
3365 switch (bestn->simdclone->args[i].arg_type)
3367 case SIMD_CLONE_ARG_TYPE_VECTOR:
3368 atype = bestn->simdclone->args[i].vector_type;
3369 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
3370 for (m = j * o; m < (j + 1) * o; m++)
3372 if (TYPE_VECTOR_SUBPARTS (atype)
3373 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
3375 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
3376 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
3377 / TYPE_VECTOR_SUBPARTS (atype));
3378 gcc_assert ((k & (k - 1)) == 0);
3379 if (m == 0)
3380 vec_oprnd0
3381 = vect_get_vec_def_for_operand (op, stmt);
3382 else
3384 vec_oprnd0 = arginfo[i].op;
3385 if ((m & (k - 1)) == 0)
3386 vec_oprnd0
3387 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3388 vec_oprnd0);
3390 arginfo[i].op = vec_oprnd0;
3391 vec_oprnd0
3392 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
3393 size_int (prec),
3394 bitsize_int ((m & (k - 1)) * prec));
3395 new_stmt
3396 = gimple_build_assign (make_ssa_name (atype),
3397 vec_oprnd0);
3398 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3399 vargs.safe_push (gimple_assign_lhs (new_stmt));
3401 else
3403 k = (TYPE_VECTOR_SUBPARTS (atype)
3404 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
3405 gcc_assert ((k & (k - 1)) == 0);
3406 vec<constructor_elt, va_gc> *ctor_elts;
3407 if (k != 1)
3408 vec_alloc (ctor_elts, k);
3409 else
3410 ctor_elts = NULL;
3411 for (l = 0; l < k; l++)
3413 if (m == 0 && l == 0)
3414 vec_oprnd0
3415 = vect_get_vec_def_for_operand (op, stmt);
3416 else
3417 vec_oprnd0
3418 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3419 arginfo[i].op);
3420 arginfo[i].op = vec_oprnd0;
3421 if (k == 1)
3422 break;
3423 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
3424 vec_oprnd0);
3426 if (k == 1)
3427 vargs.safe_push (vec_oprnd0);
3428 else
3430 vec_oprnd0 = build_constructor (atype, ctor_elts);
3431 new_stmt
3432 = gimple_build_assign (make_ssa_name (atype),
3433 vec_oprnd0);
3434 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3435 vargs.safe_push (gimple_assign_lhs (new_stmt));
3439 break;
3440 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3441 vargs.safe_push (op);
3442 break;
3443 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
3444 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
3445 if (j == 0)
3447 gimple_seq stmts;
3448 arginfo[i].op
3449 = force_gimple_operand (arginfo[i].op, &stmts, true,
3450 NULL_TREE);
3451 if (stmts != NULL)
3453 basic_block new_bb;
3454 edge pe = loop_preheader_edge (loop);
3455 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3456 gcc_assert (!new_bb);
3458 if (arginfo[i].simd_lane_linear)
3460 vargs.safe_push (arginfo[i].op);
3461 break;
3463 tree phi_res = copy_ssa_name (op);
3464 gphi *new_phi = create_phi_node (phi_res, loop->header);
3465 set_vinfo_for_stmt (new_phi,
3466 new_stmt_vec_info (new_phi, loop_vinfo));
3467 add_phi_arg (new_phi, arginfo[i].op,
3468 loop_preheader_edge (loop), UNKNOWN_LOCATION);
3469 enum tree_code code
3470 = POINTER_TYPE_P (TREE_TYPE (op))
3471 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3472 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3473 ? sizetype : TREE_TYPE (op);
3474 widest_int cst
3475 = wi::mul (bestn->simdclone->args[i].linear_step,
3476 ncopies * nunits);
3477 tree tcst = wide_int_to_tree (type, cst);
3478 tree phi_arg = copy_ssa_name (op);
3479 new_stmt
3480 = gimple_build_assign (phi_arg, code, phi_res, tcst);
3481 gimple_stmt_iterator si = gsi_after_labels (loop->header);
3482 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
3483 set_vinfo_for_stmt (new_stmt,
3484 new_stmt_vec_info (new_stmt, loop_vinfo));
3485 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
3486 UNKNOWN_LOCATION);
3487 arginfo[i].op = phi_res;
3488 vargs.safe_push (phi_res);
3490 else
3492 enum tree_code code
3493 = POINTER_TYPE_P (TREE_TYPE (op))
3494 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3495 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3496 ? sizetype : TREE_TYPE (op);
3497 widest_int cst
3498 = wi::mul (bestn->simdclone->args[i].linear_step,
3499 j * nunits);
3500 tree tcst = wide_int_to_tree (type, cst);
3501 new_temp = make_ssa_name (TREE_TYPE (op));
3502 new_stmt = gimple_build_assign (new_temp, code,
3503 arginfo[i].op, tcst);
3504 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3505 vargs.safe_push (new_temp);
3507 break;
3508 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
3509 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
3510 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
3511 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
3512 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
3513 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
3514 default:
3515 gcc_unreachable ();
3519 new_stmt = gimple_build_call_vec (fndecl, vargs);
3520 if (vec_dest)
3522 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
3523 if (ratype)
3524 new_temp = create_tmp_var (ratype);
3525 else if (TYPE_VECTOR_SUBPARTS (vectype)
3526 == TYPE_VECTOR_SUBPARTS (rtype))
3527 new_temp = make_ssa_name (vec_dest, new_stmt);
3528 else
3529 new_temp = make_ssa_name (rtype, new_stmt);
3530 gimple_call_set_lhs (new_stmt, new_temp);
3532 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3534 if (vec_dest)
3536 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
3538 unsigned int k, l;
3539 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
3540 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
3541 gcc_assert ((k & (k - 1)) == 0);
3542 for (l = 0; l < k; l++)
3544 tree t;
3545 if (ratype)
3547 t = build_fold_addr_expr (new_temp);
3548 t = build2 (MEM_REF, vectype, t,
3549 build_int_cst (TREE_TYPE (t),
3550 l * prec / BITS_PER_UNIT));
3552 else
3553 t = build3 (BIT_FIELD_REF, vectype, new_temp,
3554 size_int (prec), bitsize_int (l * prec));
3555 new_stmt
3556 = gimple_build_assign (make_ssa_name (vectype), t);
3557 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3558 if (j == 0 && l == 0)
3559 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3560 else
3561 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3563 prev_stmt_info = vinfo_for_stmt (new_stmt);
3566 if (ratype)
3568 tree clobber = build_constructor (ratype, NULL);
3569 TREE_THIS_VOLATILE (clobber) = 1;
3570 new_stmt = gimple_build_assign (new_temp, clobber);
3571 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3573 continue;
3575 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
3577 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
3578 / TYPE_VECTOR_SUBPARTS (rtype));
3579 gcc_assert ((k & (k - 1)) == 0);
3580 if ((j & (k - 1)) == 0)
3581 vec_alloc (ret_ctor_elts, k);
3582 if (ratype)
3584 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
3585 for (m = 0; m < o; m++)
3587 tree tem = build4 (ARRAY_REF, rtype, new_temp,
3588 size_int (m), NULL_TREE, NULL_TREE);
3589 new_stmt
3590 = gimple_build_assign (make_ssa_name (rtype), tem);
3591 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3592 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
3593 gimple_assign_lhs (new_stmt));
3595 tree clobber = build_constructor (ratype, NULL);
3596 TREE_THIS_VOLATILE (clobber) = 1;
3597 new_stmt = gimple_build_assign (new_temp, clobber);
3598 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3600 else
3601 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
3602 if ((j & (k - 1)) != k - 1)
3603 continue;
3604 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
3605 new_stmt
3606 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
3607 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3609 if ((unsigned) j == k - 1)
3610 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3611 else
3612 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3614 prev_stmt_info = vinfo_for_stmt (new_stmt);
3615 continue;
3617 else if (ratype)
3619 tree t = build_fold_addr_expr (new_temp);
3620 t = build2 (MEM_REF, vectype, t,
3621 build_int_cst (TREE_TYPE (t), 0));
3622 new_stmt
3623 = gimple_build_assign (make_ssa_name (vec_dest), t);
3624 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3625 tree clobber = build_constructor (ratype, NULL);
3626 TREE_THIS_VOLATILE (clobber) = 1;
3627 vect_finish_stmt_generation (stmt,
3628 gimple_build_assign (new_temp,
3629 clobber), gsi);
3633 if (j == 0)
3634 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3635 else
3636 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3638 prev_stmt_info = vinfo_for_stmt (new_stmt);
3641 vargs.release ();
3643 /* The call in STMT might prevent it from being removed in dce.
3644 We however cannot remove it here, due to the way the ssa name
3645 it defines is mapped to the new definition. So just replace
3646 rhs of the statement with something harmless. */
3648 if (slp_node)
3649 return true;
3651 if (scalar_dest)
3653 type = TREE_TYPE (scalar_dest);
3654 if (is_pattern_stmt_p (stmt_info))
3655 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
3656 else
3657 lhs = gimple_call_lhs (stmt);
3658 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
3660 else
3661 new_stmt = gimple_build_nop ();
3662 set_vinfo_for_stmt (new_stmt, stmt_info);
3663 set_vinfo_for_stmt (stmt, NULL);
3664 STMT_VINFO_STMT (stmt_info) = new_stmt;
3665 gsi_replace (gsi, new_stmt, true);
3666 unlink_stmt_vdef (stmt);
3668 return true;
3672 /* Function vect_gen_widened_results_half
3674 Create a vector stmt whose code, type, number of arguments, and result
3675 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
3676 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
3677 In the case that CODE is a CALL_EXPR, this means that a call to DECL
3678 needs to be created (DECL is a function-decl of a target-builtin).
3679 STMT is the original scalar stmt that we are vectorizing. */
3681 static gimple *
3682 vect_gen_widened_results_half (enum tree_code code,
3683 tree decl,
3684 tree vec_oprnd0, tree vec_oprnd1, int op_type,
3685 tree vec_dest, gimple_stmt_iterator *gsi,
3686 gimple *stmt)
3688 gimple *new_stmt;
3689 tree new_temp;
3691 /* Generate half of the widened result: */
3692 if (code == CALL_EXPR)
3694 /* Target specific support */
3695 if (op_type == binary_op)
3696 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3697 else
3698 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3699 new_temp = make_ssa_name (vec_dest, new_stmt);
3700 gimple_call_set_lhs (new_stmt, new_temp);
3702 else
3704 /* Generic support */
3705 gcc_assert (op_type == TREE_CODE_LENGTH (code));
3706 if (op_type != binary_op)
3707 vec_oprnd1 = NULL;
3708 new_stmt = gimple_build_assign (vec_dest, code, vec_oprnd0, vec_oprnd1);
3709 new_temp = make_ssa_name (vec_dest, new_stmt);
3710 gimple_assign_set_lhs (new_stmt, new_temp);
3712 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3714 return new_stmt;
3718 /* Get vectorized definitions for loop-based vectorization. For the first
3719 operand we call vect_get_vec_def_for_operand() (with OPRND containing
3720 scalar operand), and for the rest we get a copy with
3721 vect_get_vec_def_for_stmt_copy() using the previous vector definition
3722 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
3723 The vectors are collected into VEC_OPRNDS. */
3725 static void
3726 vect_get_loop_based_defs (tree *oprnd, gimple *stmt, enum vect_def_type dt,
3727 vec<tree> *vec_oprnds, int multi_step_cvt)
3729 tree vec_oprnd;
3731 /* Get first vector operand. */
3732 /* All the vector operands except the very first one (that is scalar oprnd)
3733 are stmt copies. */
3734 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
3735 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt);
3736 else
3737 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
3739 vec_oprnds->quick_push (vec_oprnd);
3741 /* Get second vector operand. */
3742 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
3743 vec_oprnds->quick_push (vec_oprnd);
3745 *oprnd = vec_oprnd;
3747 /* For conversion in multiple steps, continue to get operands
3748 recursively. */
3749 if (multi_step_cvt)
3750 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
3754 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
3755 For multi-step conversions store the resulting vectors and call the function
3756 recursively. */
3758 static void
3759 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
3760 int multi_step_cvt, gimple *stmt,
3761 vec<tree> vec_dsts,
3762 gimple_stmt_iterator *gsi,
3763 slp_tree slp_node, enum tree_code code,
3764 stmt_vec_info *prev_stmt_info)
3766 unsigned int i;
3767 tree vop0, vop1, new_tmp, vec_dest;
3768 gimple *new_stmt;
3769 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3771 vec_dest = vec_dsts.pop ();
3773 for (i = 0; i < vec_oprnds->length (); i += 2)
3775 /* Create demotion operation. */
3776 vop0 = (*vec_oprnds)[i];
3777 vop1 = (*vec_oprnds)[i + 1];
3778 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
3779 new_tmp = make_ssa_name (vec_dest, new_stmt);
3780 gimple_assign_set_lhs (new_stmt, new_tmp);
3781 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3783 if (multi_step_cvt)
3784 /* Store the resulting vector for next recursive call. */
3785 (*vec_oprnds)[i/2] = new_tmp;
3786 else
3788 /* This is the last step of the conversion sequence. Store the
3789 vectors in SLP_NODE or in vector info of the scalar statement
3790 (or in STMT_VINFO_RELATED_STMT chain). */
3791 if (slp_node)
3792 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3793 else
3795 if (!*prev_stmt_info)
3796 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3797 else
3798 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
3800 *prev_stmt_info = vinfo_for_stmt (new_stmt);
3805 /* For multi-step demotion operations we first generate demotion operations
3806 from the source type to the intermediate types, and then combine the
3807 results (stored in VEC_OPRNDS) in demotion operation to the destination
3808 type. */
3809 if (multi_step_cvt)
3811 /* At each level of recursion we have half of the operands we had at the
3812 previous level. */
3813 vec_oprnds->truncate ((i+1)/2);
3814 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
3815 stmt, vec_dsts, gsi, slp_node,
3816 VEC_PACK_TRUNC_EXPR,
3817 prev_stmt_info);
3820 vec_dsts.quick_push (vec_dest);
3824 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
3825 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
3826 the resulting vectors and call the function recursively. */
3828 static void
3829 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
3830 vec<tree> *vec_oprnds1,
3831 gimple *stmt, tree vec_dest,
3832 gimple_stmt_iterator *gsi,
3833 enum tree_code code1,
3834 enum tree_code code2, tree decl1,
3835 tree decl2, int op_type)
3837 int i;
3838 tree vop0, vop1, new_tmp1, new_tmp2;
3839 gimple *new_stmt1, *new_stmt2;
3840 vec<tree> vec_tmp = vNULL;
3842 vec_tmp.create (vec_oprnds0->length () * 2);
3843 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
3845 if (op_type == binary_op)
3846 vop1 = (*vec_oprnds1)[i];
3847 else
3848 vop1 = NULL_TREE;
3850 /* Generate the two halves of promotion operation. */
3851 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
3852 op_type, vec_dest, gsi, stmt);
3853 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
3854 op_type, vec_dest, gsi, stmt);
3855 if (is_gimple_call (new_stmt1))
3857 new_tmp1 = gimple_call_lhs (new_stmt1);
3858 new_tmp2 = gimple_call_lhs (new_stmt2);
3860 else
3862 new_tmp1 = gimple_assign_lhs (new_stmt1);
3863 new_tmp2 = gimple_assign_lhs (new_stmt2);
3866 /* Store the results for the next step. */
3867 vec_tmp.quick_push (new_tmp1);
3868 vec_tmp.quick_push (new_tmp2);
3871 vec_oprnds0->release ();
3872 *vec_oprnds0 = vec_tmp;
3876 /* Check if STMT performs a conversion operation, that can be vectorized.
3877 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3878 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3879 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3881 static bool
3882 vectorizable_conversion (gimple *stmt, gimple_stmt_iterator *gsi,
3883 gimple **vec_stmt, slp_tree slp_node)
3885 tree vec_dest;
3886 tree scalar_dest;
3887 tree op0, op1 = NULL_TREE;
3888 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3889 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3890 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3891 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
3892 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
3893 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3894 tree new_temp;
3895 gimple *def_stmt;
3896 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3897 gimple *new_stmt = NULL;
3898 stmt_vec_info prev_stmt_info;
3899 int nunits_in;
3900 int nunits_out;
3901 tree vectype_out, vectype_in;
3902 int ncopies, i, j;
3903 tree lhs_type, rhs_type;
3904 enum { NARROW, NONE, WIDEN } modifier;
3905 vec<tree> vec_oprnds0 = vNULL;
3906 vec<tree> vec_oprnds1 = vNULL;
3907 tree vop0;
3908 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3909 vec_info *vinfo = stmt_info->vinfo;
3910 int multi_step_cvt = 0;
3911 vec<tree> interm_types = vNULL;
3912 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
3913 int op_type;
3914 machine_mode rhs_mode;
3915 unsigned short fltsz;
3917 /* Is STMT a vectorizable conversion? */
3919 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3920 return false;
3922 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3923 && ! vec_stmt)
3924 return false;
3926 if (!is_gimple_assign (stmt))
3927 return false;
3929 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3930 return false;
3932 code = gimple_assign_rhs_code (stmt);
3933 if (!CONVERT_EXPR_CODE_P (code)
3934 && code != FIX_TRUNC_EXPR
3935 && code != FLOAT_EXPR
3936 && code != WIDEN_MULT_EXPR
3937 && code != WIDEN_LSHIFT_EXPR)
3938 return false;
3940 op_type = TREE_CODE_LENGTH (code);
3942 /* Check types of lhs and rhs. */
3943 scalar_dest = gimple_assign_lhs (stmt);
3944 lhs_type = TREE_TYPE (scalar_dest);
3945 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3947 op0 = gimple_assign_rhs1 (stmt);
3948 rhs_type = TREE_TYPE (op0);
3950 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3951 && !((INTEGRAL_TYPE_P (lhs_type)
3952 && INTEGRAL_TYPE_P (rhs_type))
3953 || (SCALAR_FLOAT_TYPE_P (lhs_type)
3954 && SCALAR_FLOAT_TYPE_P (rhs_type))))
3955 return false;
3957 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
3958 && ((INTEGRAL_TYPE_P (lhs_type)
3959 && (TYPE_PRECISION (lhs_type)
3960 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
3961 || (INTEGRAL_TYPE_P (rhs_type)
3962 && (TYPE_PRECISION (rhs_type)
3963 != GET_MODE_PRECISION (TYPE_MODE (rhs_type))))))
3965 if (dump_enabled_p ())
3966 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3967 "type conversion to/from bit-precision unsupported."
3968 "\n");
3969 return false;
3972 /* Check the operands of the operation. */
3973 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype_in))
3975 if (dump_enabled_p ())
3976 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3977 "use not simple.\n");
3978 return false;
3980 if (op_type == binary_op)
3982 bool ok;
3984 op1 = gimple_assign_rhs2 (stmt);
3985 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3986 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3987 OP1. */
3988 if (CONSTANT_CLASS_P (op0))
3989 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &vectype_in);
3990 else
3991 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]);
3993 if (!ok)
3995 if (dump_enabled_p ())
3996 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3997 "use not simple.\n");
3998 return false;
4002 /* If op0 is an external or constant defs use a vector type of
4003 the same size as the output vector type. */
4004 if (!vectype_in)
4005 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
4006 if (vec_stmt)
4007 gcc_assert (vectype_in);
4008 if (!vectype_in)
4010 if (dump_enabled_p ())
4012 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4013 "no vectype for scalar type ");
4014 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
4015 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4018 return false;
4021 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
4022 && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
4024 if (dump_enabled_p ())
4026 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4027 "can't convert between boolean and non "
4028 "boolean vectors");
4029 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
4030 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4033 return false;
4036 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
4037 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4038 if (nunits_in < nunits_out)
4039 modifier = NARROW;
4040 else if (nunits_out == nunits_in)
4041 modifier = NONE;
4042 else
4043 modifier = WIDEN;
4045 /* Multiple types in SLP are handled by creating the appropriate number of
4046 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4047 case of SLP. */
4048 if (slp_node)
4049 ncopies = 1;
4050 else if (modifier == NARROW)
4051 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
4052 else
4053 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4055 /* Sanity check: make sure that at least one copy of the vectorized stmt
4056 needs to be generated. */
4057 gcc_assert (ncopies >= 1);
4059 /* Supportable by target? */
4060 switch (modifier)
4062 case NONE:
4063 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
4064 return false;
4065 if (supportable_convert_operation (code, vectype_out, vectype_in,
4066 &decl1, &code1))
4067 break;
4068 /* FALLTHRU */
4069 unsupported:
4070 if (dump_enabled_p ())
4071 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4072 "conversion not supported by target.\n");
4073 return false;
4075 case WIDEN:
4076 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
4077 &code1, &code2, &multi_step_cvt,
4078 &interm_types))
4080 /* Binary widening operation can only be supported directly by the
4081 architecture. */
4082 gcc_assert (!(multi_step_cvt && op_type == binary_op));
4083 break;
4086 if (code != FLOAT_EXPR
4087 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
4088 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
4089 goto unsupported;
4091 rhs_mode = TYPE_MODE (rhs_type);
4092 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
4093 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
4094 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
4095 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
4097 cvt_type
4098 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
4099 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
4100 if (cvt_type == NULL_TREE)
4101 goto unsupported;
4103 if (GET_MODE_SIZE (rhs_mode) == fltsz)
4105 if (!supportable_convert_operation (code, vectype_out,
4106 cvt_type, &decl1, &codecvt1))
4107 goto unsupported;
4109 else if (!supportable_widening_operation (code, stmt, vectype_out,
4110 cvt_type, &codecvt1,
4111 &codecvt2, &multi_step_cvt,
4112 &interm_types))
4113 continue;
4114 else
4115 gcc_assert (multi_step_cvt == 0);
4117 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
4118 vectype_in, &code1, &code2,
4119 &multi_step_cvt, &interm_types))
4120 break;
4123 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
4124 goto unsupported;
4126 if (GET_MODE_SIZE (rhs_mode) == fltsz)
4127 codecvt2 = ERROR_MARK;
4128 else
4130 multi_step_cvt++;
4131 interm_types.safe_push (cvt_type);
4132 cvt_type = NULL_TREE;
4134 break;
4136 case NARROW:
4137 gcc_assert (op_type == unary_op);
4138 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
4139 &code1, &multi_step_cvt,
4140 &interm_types))
4141 break;
4143 if (code != FIX_TRUNC_EXPR
4144 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
4145 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
4146 goto unsupported;
4148 rhs_mode = TYPE_MODE (rhs_type);
4149 cvt_type
4150 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
4151 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
4152 if (cvt_type == NULL_TREE)
4153 goto unsupported;
4154 if (!supportable_convert_operation (code, cvt_type, vectype_in,
4155 &decl1, &codecvt1))
4156 goto unsupported;
4157 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
4158 &code1, &multi_step_cvt,
4159 &interm_types))
4160 break;
4161 goto unsupported;
4163 default:
4164 gcc_unreachable ();
4167 if (!vec_stmt) /* transformation not required. */
4169 if (dump_enabled_p ())
4170 dump_printf_loc (MSG_NOTE, vect_location,
4171 "=== vectorizable_conversion ===\n");
4172 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
4174 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
4175 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4177 else if (modifier == NARROW)
4179 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
4180 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
4182 else
4184 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
4185 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
4187 interm_types.release ();
4188 return true;
4191 /** Transform. **/
4192 if (dump_enabled_p ())
4193 dump_printf_loc (MSG_NOTE, vect_location,
4194 "transform conversion. ncopies = %d.\n", ncopies);
4196 if (op_type == binary_op)
4198 if (CONSTANT_CLASS_P (op0))
4199 op0 = fold_convert (TREE_TYPE (op1), op0);
4200 else if (CONSTANT_CLASS_P (op1))
4201 op1 = fold_convert (TREE_TYPE (op0), op1);
4204 /* In case of multi-step conversion, we first generate conversion operations
4205 to the intermediate types, and then from that types to the final one.
4206 We create vector destinations for the intermediate type (TYPES) received
4207 from supportable_*_operation, and store them in the correct order
4208 for future use in vect_create_vectorized_*_stmts (). */
4209 auto_vec<tree> vec_dsts (multi_step_cvt + 1);
4210 vec_dest = vect_create_destination_var (scalar_dest,
4211 (cvt_type && modifier == WIDEN)
4212 ? cvt_type : vectype_out);
4213 vec_dsts.quick_push (vec_dest);
4215 if (multi_step_cvt)
4217 for (i = interm_types.length () - 1;
4218 interm_types.iterate (i, &intermediate_type); i--)
4220 vec_dest = vect_create_destination_var (scalar_dest,
4221 intermediate_type);
4222 vec_dsts.quick_push (vec_dest);
4226 if (cvt_type)
4227 vec_dest = vect_create_destination_var (scalar_dest,
4228 modifier == WIDEN
4229 ? vectype_out : cvt_type);
4231 if (!slp_node)
4233 if (modifier == WIDEN)
4235 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
4236 if (op_type == binary_op)
4237 vec_oprnds1.create (1);
4239 else if (modifier == NARROW)
4240 vec_oprnds0.create (
4241 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
4243 else if (code == WIDEN_LSHIFT_EXPR)
4244 vec_oprnds1.create (slp_node->vec_stmts_size);
4246 last_oprnd = op0;
4247 prev_stmt_info = NULL;
4248 switch (modifier)
4250 case NONE:
4251 for (j = 0; j < ncopies; j++)
4253 if (j == 0)
4254 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
4255 -1);
4256 else
4257 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
4259 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4261 /* Arguments are ready, create the new vector stmt. */
4262 if (code1 == CALL_EXPR)
4264 new_stmt = gimple_build_call (decl1, 1, vop0);
4265 new_temp = make_ssa_name (vec_dest, new_stmt);
4266 gimple_call_set_lhs (new_stmt, new_temp);
4268 else
4270 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
4271 new_stmt = gimple_build_assign (vec_dest, code1, vop0);
4272 new_temp = make_ssa_name (vec_dest, new_stmt);
4273 gimple_assign_set_lhs (new_stmt, new_temp);
4276 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4277 if (slp_node)
4278 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4279 else
4281 if (!prev_stmt_info)
4282 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4283 else
4284 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4285 prev_stmt_info = vinfo_for_stmt (new_stmt);
4289 break;
4291 case WIDEN:
4292 /* In case the vectorization factor (VF) is bigger than the number
4293 of elements that we can fit in a vectype (nunits), we have to
4294 generate more than one vector stmt - i.e - we need to "unroll"
4295 the vector stmt by a factor VF/nunits. */
4296 for (j = 0; j < ncopies; j++)
4298 /* Handle uses. */
4299 if (j == 0)
4301 if (slp_node)
4303 if (code == WIDEN_LSHIFT_EXPR)
4305 unsigned int k;
4307 vec_oprnd1 = op1;
4308 /* Store vec_oprnd1 for every vector stmt to be created
4309 for SLP_NODE. We check during the analysis that all
4310 the shift arguments are the same. */
4311 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4312 vec_oprnds1.quick_push (vec_oprnd1);
4314 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4315 slp_node, -1);
4317 else
4318 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
4319 &vec_oprnds1, slp_node, -1);
4321 else
4323 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt);
4324 vec_oprnds0.quick_push (vec_oprnd0);
4325 if (op_type == binary_op)
4327 if (code == WIDEN_LSHIFT_EXPR)
4328 vec_oprnd1 = op1;
4329 else
4330 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt);
4331 vec_oprnds1.quick_push (vec_oprnd1);
4335 else
4337 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
4338 vec_oprnds0.truncate (0);
4339 vec_oprnds0.quick_push (vec_oprnd0);
4340 if (op_type == binary_op)
4342 if (code == WIDEN_LSHIFT_EXPR)
4343 vec_oprnd1 = op1;
4344 else
4345 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
4346 vec_oprnd1);
4347 vec_oprnds1.truncate (0);
4348 vec_oprnds1.quick_push (vec_oprnd1);
4352 /* Arguments are ready. Create the new vector stmts. */
4353 for (i = multi_step_cvt; i >= 0; i--)
4355 tree this_dest = vec_dsts[i];
4356 enum tree_code c1 = code1, c2 = code2;
4357 if (i == 0 && codecvt2 != ERROR_MARK)
4359 c1 = codecvt1;
4360 c2 = codecvt2;
4362 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
4363 &vec_oprnds1,
4364 stmt, this_dest, gsi,
4365 c1, c2, decl1, decl2,
4366 op_type);
4369 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4371 if (cvt_type)
4373 if (codecvt1 == CALL_EXPR)
4375 new_stmt = gimple_build_call (decl1, 1, vop0);
4376 new_temp = make_ssa_name (vec_dest, new_stmt);
4377 gimple_call_set_lhs (new_stmt, new_temp);
4379 else
4381 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
4382 new_temp = make_ssa_name (vec_dest);
4383 new_stmt = gimple_build_assign (new_temp, codecvt1,
4384 vop0);
4387 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4389 else
4390 new_stmt = SSA_NAME_DEF_STMT (vop0);
4392 if (slp_node)
4393 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4394 else
4396 if (!prev_stmt_info)
4397 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
4398 else
4399 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4400 prev_stmt_info = vinfo_for_stmt (new_stmt);
4405 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4406 break;
4408 case NARROW:
4409 /* In case the vectorization factor (VF) is bigger than the number
4410 of elements that we can fit in a vectype (nunits), we have to
4411 generate more than one vector stmt - i.e - we need to "unroll"
4412 the vector stmt by a factor VF/nunits. */
4413 for (j = 0; j < ncopies; j++)
4415 /* Handle uses. */
4416 if (slp_node)
4417 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4418 slp_node, -1);
4419 else
4421 vec_oprnds0.truncate (0);
4422 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
4423 vect_pow2 (multi_step_cvt) - 1);
4426 /* Arguments are ready. Create the new vector stmts. */
4427 if (cvt_type)
4428 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4430 if (codecvt1 == CALL_EXPR)
4432 new_stmt = gimple_build_call (decl1, 1, vop0);
4433 new_temp = make_ssa_name (vec_dest, new_stmt);
4434 gimple_call_set_lhs (new_stmt, new_temp);
4436 else
4438 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
4439 new_temp = make_ssa_name (vec_dest);
4440 new_stmt = gimple_build_assign (new_temp, codecvt1,
4441 vop0);
4444 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4445 vec_oprnds0[i] = new_temp;
4448 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
4449 stmt, vec_dsts, gsi,
4450 slp_node, code1,
4451 &prev_stmt_info);
4454 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4455 break;
4458 vec_oprnds0.release ();
4459 vec_oprnds1.release ();
4460 interm_types.release ();
4462 return true;
4466 /* Function vectorizable_assignment.
4468 Check if STMT performs an assignment (copy) that can be vectorized.
4469 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4470 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4471 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4473 static bool
4474 vectorizable_assignment (gimple *stmt, gimple_stmt_iterator *gsi,
4475 gimple **vec_stmt, slp_tree slp_node)
4477 tree vec_dest;
4478 tree scalar_dest;
4479 tree op;
4480 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4481 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4482 tree new_temp;
4483 gimple *def_stmt;
4484 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4485 int ncopies;
4486 int i, j;
4487 vec<tree> vec_oprnds = vNULL;
4488 tree vop;
4489 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4490 vec_info *vinfo = stmt_info->vinfo;
4491 gimple *new_stmt = NULL;
4492 stmt_vec_info prev_stmt_info = NULL;
4493 enum tree_code code;
4494 tree vectype_in;
4496 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4497 return false;
4499 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4500 && ! vec_stmt)
4501 return false;
4503 /* Is vectorizable assignment? */
4504 if (!is_gimple_assign (stmt))
4505 return false;
4507 scalar_dest = gimple_assign_lhs (stmt);
4508 if (TREE_CODE (scalar_dest) != SSA_NAME)
4509 return false;
4511 code = gimple_assign_rhs_code (stmt);
4512 if (gimple_assign_single_p (stmt)
4513 || code == PAREN_EXPR
4514 || CONVERT_EXPR_CODE_P (code))
4515 op = gimple_assign_rhs1 (stmt);
4516 else
4517 return false;
4519 if (code == VIEW_CONVERT_EXPR)
4520 op = TREE_OPERAND (op, 0);
4522 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4523 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4525 /* Multiple types in SLP are handled by creating the appropriate number of
4526 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4527 case of SLP. */
4528 if (slp_node)
4529 ncopies = 1;
4530 else
4531 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4533 gcc_assert (ncopies >= 1);
4535 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt[0], &vectype_in))
4537 if (dump_enabled_p ())
4538 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4539 "use not simple.\n");
4540 return false;
4543 /* We can handle NOP_EXPR conversions that do not change the number
4544 of elements or the vector size. */
4545 if ((CONVERT_EXPR_CODE_P (code)
4546 || code == VIEW_CONVERT_EXPR)
4547 && (!vectype_in
4548 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
4549 || (GET_MODE_SIZE (TYPE_MODE (vectype))
4550 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
4551 return false;
4553 /* We do not handle bit-precision changes. */
4554 if ((CONVERT_EXPR_CODE_P (code)
4555 || code == VIEW_CONVERT_EXPR)
4556 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4557 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4558 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4559 || ((TYPE_PRECISION (TREE_TYPE (op))
4560 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
4561 /* But a conversion that does not change the bit-pattern is ok. */
4562 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4563 > TYPE_PRECISION (TREE_TYPE (op)))
4564 && TYPE_UNSIGNED (TREE_TYPE (op)))
4565 /* Conversion between boolean types of different sizes is
4566 a simple assignment in case their vectypes are same
4567 boolean vectors. */
4568 && (!VECTOR_BOOLEAN_TYPE_P (vectype)
4569 || !VECTOR_BOOLEAN_TYPE_P (vectype_in)))
4571 if (dump_enabled_p ())
4572 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4573 "type conversion to/from bit-precision "
4574 "unsupported.\n");
4575 return false;
4578 if (!vec_stmt) /* transformation not required. */
4580 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
4581 if (dump_enabled_p ())
4582 dump_printf_loc (MSG_NOTE, vect_location,
4583 "=== vectorizable_assignment ===\n");
4584 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4585 return true;
4588 /** Transform. **/
4589 if (dump_enabled_p ())
4590 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
4592 /* Handle def. */
4593 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4595 /* Handle use. */
4596 for (j = 0; j < ncopies; j++)
4598 /* Handle uses. */
4599 if (j == 0)
4600 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
4601 else
4602 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
4604 /* Arguments are ready. create the new vector stmt. */
4605 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
4607 if (CONVERT_EXPR_CODE_P (code)
4608 || code == VIEW_CONVERT_EXPR)
4609 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
4610 new_stmt = gimple_build_assign (vec_dest, vop);
4611 new_temp = make_ssa_name (vec_dest, new_stmt);
4612 gimple_assign_set_lhs (new_stmt, new_temp);
4613 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4614 if (slp_node)
4615 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4618 if (slp_node)
4619 continue;
4621 if (j == 0)
4622 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4623 else
4624 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4626 prev_stmt_info = vinfo_for_stmt (new_stmt);
4629 vec_oprnds.release ();
4630 return true;
4634 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
4635 either as shift by a scalar or by a vector. */
4637 bool
4638 vect_supportable_shift (enum tree_code code, tree scalar_type)
4641 machine_mode vec_mode;
4642 optab optab;
4643 int icode;
4644 tree vectype;
4646 vectype = get_vectype_for_scalar_type (scalar_type);
4647 if (!vectype)
4648 return false;
4650 optab = optab_for_tree_code (code, vectype, optab_scalar);
4651 if (!optab
4652 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
4654 optab = optab_for_tree_code (code, vectype, optab_vector);
4655 if (!optab
4656 || (optab_handler (optab, TYPE_MODE (vectype))
4657 == CODE_FOR_nothing))
4658 return false;
4661 vec_mode = TYPE_MODE (vectype);
4662 icode = (int) optab_handler (optab, vec_mode);
4663 if (icode == CODE_FOR_nothing)
4664 return false;
4666 return true;
4670 /* Function vectorizable_shift.
4672 Check if STMT performs a shift operation that can be vectorized.
4673 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4674 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4675 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4677 static bool
4678 vectorizable_shift (gimple *stmt, gimple_stmt_iterator *gsi,
4679 gimple **vec_stmt, slp_tree slp_node)
4681 tree vec_dest;
4682 tree scalar_dest;
4683 tree op0, op1 = NULL;
4684 tree vec_oprnd1 = NULL_TREE;
4685 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4686 tree vectype;
4687 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4688 enum tree_code code;
4689 machine_mode vec_mode;
4690 tree new_temp;
4691 optab optab;
4692 int icode;
4693 machine_mode optab_op2_mode;
4694 gimple *def_stmt;
4695 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4696 gimple *new_stmt = NULL;
4697 stmt_vec_info prev_stmt_info;
4698 int nunits_in;
4699 int nunits_out;
4700 tree vectype_out;
4701 tree op1_vectype;
4702 int ncopies;
4703 int j, i;
4704 vec<tree> vec_oprnds0 = vNULL;
4705 vec<tree> vec_oprnds1 = vNULL;
4706 tree vop0, vop1;
4707 unsigned int k;
4708 bool scalar_shift_arg = true;
4709 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4710 vec_info *vinfo = stmt_info->vinfo;
4711 int vf;
4713 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4714 return false;
4716 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4717 && ! vec_stmt)
4718 return false;
4720 /* Is STMT a vectorizable binary/unary operation? */
4721 if (!is_gimple_assign (stmt))
4722 return false;
4724 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4725 return false;
4727 code = gimple_assign_rhs_code (stmt);
4729 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4730 || code == RROTATE_EXPR))
4731 return false;
4733 scalar_dest = gimple_assign_lhs (stmt);
4734 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4735 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4736 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4738 if (dump_enabled_p ())
4739 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4740 "bit-precision shifts not supported.\n");
4741 return false;
4744 op0 = gimple_assign_rhs1 (stmt);
4745 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
4747 if (dump_enabled_p ())
4748 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4749 "use not simple.\n");
4750 return false;
4752 /* If op0 is an external or constant def use a vector type with
4753 the same size as the output vector type. */
4754 if (!vectype)
4755 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4756 if (vec_stmt)
4757 gcc_assert (vectype);
4758 if (!vectype)
4760 if (dump_enabled_p ())
4761 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4762 "no vectype for scalar type\n");
4763 return false;
4766 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4767 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4768 if (nunits_out != nunits_in)
4769 return false;
4771 op1 = gimple_assign_rhs2 (stmt);
4772 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &op1_vectype))
4774 if (dump_enabled_p ())
4775 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4776 "use not simple.\n");
4777 return false;
4780 if (loop_vinfo)
4781 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4782 else
4783 vf = 1;
4785 /* Multiple types in SLP are handled by creating the appropriate number of
4786 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4787 case of SLP. */
4788 if (slp_node)
4789 ncopies = 1;
4790 else
4791 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4793 gcc_assert (ncopies >= 1);
4795 /* Determine whether the shift amount is a vector, or scalar. If the
4796 shift/rotate amount is a vector, use the vector/vector shift optabs. */
4798 if ((dt[1] == vect_internal_def
4799 || dt[1] == vect_induction_def)
4800 && !slp_node)
4801 scalar_shift_arg = false;
4802 else if (dt[1] == vect_constant_def
4803 || dt[1] == vect_external_def
4804 || dt[1] == vect_internal_def)
4806 /* In SLP, need to check whether the shift count is the same,
4807 in loops if it is a constant or invariant, it is always
4808 a scalar shift. */
4809 if (slp_node)
4811 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4812 gimple *slpstmt;
4814 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
4815 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
4816 scalar_shift_arg = false;
4819 /* If the shift amount is computed by a pattern stmt we cannot
4820 use the scalar amount directly thus give up and use a vector
4821 shift. */
4822 if (dt[1] == vect_internal_def)
4824 gimple *def = SSA_NAME_DEF_STMT (op1);
4825 if (is_pattern_stmt_p (vinfo_for_stmt (def)))
4826 scalar_shift_arg = false;
4829 else
4831 if (dump_enabled_p ())
4832 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4833 "operand mode requires invariant argument.\n");
4834 return false;
4837 /* Vector shifted by vector. */
4838 if (!scalar_shift_arg)
4840 optab = optab_for_tree_code (code, vectype, optab_vector);
4841 if (dump_enabled_p ())
4842 dump_printf_loc (MSG_NOTE, vect_location,
4843 "vector/vector shift/rotate found.\n");
4845 if (!op1_vectype)
4846 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
4847 if (op1_vectype == NULL_TREE
4848 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
4850 if (dump_enabled_p ())
4851 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4852 "unusable type for last operand in"
4853 " vector/vector shift/rotate.\n");
4854 return false;
4857 /* See if the machine has a vector shifted by scalar insn and if not
4858 then see if it has a vector shifted by vector insn. */
4859 else
4861 optab = optab_for_tree_code (code, vectype, optab_scalar);
4862 if (optab
4863 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
4865 if (dump_enabled_p ())
4866 dump_printf_loc (MSG_NOTE, vect_location,
4867 "vector/scalar shift/rotate found.\n");
4869 else
4871 optab = optab_for_tree_code (code, vectype, optab_vector);
4872 if (optab
4873 && (optab_handler (optab, TYPE_MODE (vectype))
4874 != CODE_FOR_nothing))
4876 scalar_shift_arg = false;
4878 if (dump_enabled_p ())
4879 dump_printf_loc (MSG_NOTE, vect_location,
4880 "vector/vector shift/rotate found.\n");
4882 /* Unlike the other binary operators, shifts/rotates have
4883 the rhs being int, instead of the same type as the lhs,
4884 so make sure the scalar is the right type if we are
4885 dealing with vectors of long long/long/short/char. */
4886 if (dt[1] == vect_constant_def)
4887 op1 = fold_convert (TREE_TYPE (vectype), op1);
4888 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
4889 TREE_TYPE (op1)))
4891 if (slp_node
4892 && TYPE_MODE (TREE_TYPE (vectype))
4893 != TYPE_MODE (TREE_TYPE (op1)))
4895 if (dump_enabled_p ())
4896 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4897 "unusable type for last operand in"
4898 " vector/vector shift/rotate.\n");
4899 return false;
4901 if (vec_stmt && !slp_node)
4903 op1 = fold_convert (TREE_TYPE (vectype), op1);
4904 op1 = vect_init_vector (stmt, op1,
4905 TREE_TYPE (vectype), NULL);
4912 /* Supportable by target? */
4913 if (!optab)
4915 if (dump_enabled_p ())
4916 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4917 "no optab.\n");
4918 return false;
4920 vec_mode = TYPE_MODE (vectype);
4921 icode = (int) optab_handler (optab, vec_mode);
4922 if (icode == CODE_FOR_nothing)
4924 if (dump_enabled_p ())
4925 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4926 "op not supported by target.\n");
4927 /* Check only during analysis. */
4928 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4929 || (vf < vect_min_worthwhile_factor (code)
4930 && !vec_stmt))
4931 return false;
4932 if (dump_enabled_p ())
4933 dump_printf_loc (MSG_NOTE, vect_location,
4934 "proceeding using word mode.\n");
4937 /* Worthwhile without SIMD support? Check only during analysis. */
4938 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4939 && vf < vect_min_worthwhile_factor (code)
4940 && !vec_stmt)
4942 if (dump_enabled_p ())
4943 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4944 "not worthwhile without SIMD support.\n");
4945 return false;
4948 if (!vec_stmt) /* transformation not required. */
4950 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
4951 if (dump_enabled_p ())
4952 dump_printf_loc (MSG_NOTE, vect_location,
4953 "=== vectorizable_shift ===\n");
4954 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4955 return true;
4958 /** Transform. **/
4960 if (dump_enabled_p ())
4961 dump_printf_loc (MSG_NOTE, vect_location,
4962 "transform binary/unary operation.\n");
4964 /* Handle def. */
4965 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4967 prev_stmt_info = NULL;
4968 for (j = 0; j < ncopies; j++)
4970 /* Handle uses. */
4971 if (j == 0)
4973 if (scalar_shift_arg)
4975 /* Vector shl and shr insn patterns can be defined with scalar
4976 operand 2 (shift operand). In this case, use constant or loop
4977 invariant op1 directly, without extending it to vector mode
4978 first. */
4979 optab_op2_mode = insn_data[icode].operand[2].mode;
4980 if (!VECTOR_MODE_P (optab_op2_mode))
4982 if (dump_enabled_p ())
4983 dump_printf_loc (MSG_NOTE, vect_location,
4984 "operand 1 using scalar mode.\n");
4985 vec_oprnd1 = op1;
4986 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
4987 vec_oprnds1.quick_push (vec_oprnd1);
4988 if (slp_node)
4990 /* Store vec_oprnd1 for every vector stmt to be created
4991 for SLP_NODE. We check during the analysis that all
4992 the shift arguments are the same.
4993 TODO: Allow different constants for different vector
4994 stmts generated for an SLP instance. */
4995 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4996 vec_oprnds1.quick_push (vec_oprnd1);
5001 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
5002 (a special case for certain kind of vector shifts); otherwise,
5003 operand 1 should be of a vector type (the usual case). */
5004 if (vec_oprnd1)
5005 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
5006 slp_node, -1);
5007 else
5008 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
5009 slp_node, -1);
5011 else
5012 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
5014 /* Arguments are ready. Create the new vector stmt. */
5015 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5017 vop1 = vec_oprnds1[i];
5018 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
5019 new_temp = make_ssa_name (vec_dest, new_stmt);
5020 gimple_assign_set_lhs (new_stmt, new_temp);
5021 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5022 if (slp_node)
5023 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5026 if (slp_node)
5027 continue;
5029 if (j == 0)
5030 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5031 else
5032 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5033 prev_stmt_info = vinfo_for_stmt (new_stmt);
5036 vec_oprnds0.release ();
5037 vec_oprnds1.release ();
5039 return true;
5043 /* Function vectorizable_operation.
5045 Check if STMT performs a binary, unary or ternary operation that can
5046 be vectorized.
5047 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5048 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5049 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5051 static bool
5052 vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi,
5053 gimple **vec_stmt, slp_tree slp_node)
5055 tree vec_dest;
5056 tree scalar_dest;
5057 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
5058 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5059 tree vectype;
5060 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5061 enum tree_code code;
5062 machine_mode vec_mode;
5063 tree new_temp;
5064 int op_type;
5065 optab optab;
5066 bool target_support_p;
5067 gimple *def_stmt;
5068 enum vect_def_type dt[3]
5069 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
5070 gimple *new_stmt = NULL;
5071 stmt_vec_info prev_stmt_info;
5072 int nunits_in;
5073 int nunits_out;
5074 tree vectype_out;
5075 int ncopies;
5076 int j, i;
5077 vec<tree> vec_oprnds0 = vNULL;
5078 vec<tree> vec_oprnds1 = vNULL;
5079 vec<tree> vec_oprnds2 = vNULL;
5080 tree vop0, vop1, vop2;
5081 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5082 vec_info *vinfo = stmt_info->vinfo;
5083 int vf;
5085 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5086 return false;
5088 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5089 && ! vec_stmt)
5090 return false;
5092 /* Is STMT a vectorizable binary/unary operation? */
5093 if (!is_gimple_assign (stmt))
5094 return false;
5096 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
5097 return false;
5099 code = gimple_assign_rhs_code (stmt);
5101 /* For pointer addition, we should use the normal plus for
5102 the vector addition. */
5103 if (code == POINTER_PLUS_EXPR)
5104 code = PLUS_EXPR;
5106 /* Support only unary or binary operations. */
5107 op_type = TREE_CODE_LENGTH (code);
5108 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
5110 if (dump_enabled_p ())
5111 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5112 "num. args = %d (not unary/binary/ternary op).\n",
5113 op_type);
5114 return false;
5117 scalar_dest = gimple_assign_lhs (stmt);
5118 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
5120 /* Most operations cannot handle bit-precision types without extra
5121 truncations. */
5122 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5123 && (TYPE_PRECISION (TREE_TYPE (scalar_dest))
5124 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
5125 /* Exception are bitwise binary operations. */
5126 && code != BIT_IOR_EXPR
5127 && code != BIT_XOR_EXPR
5128 && code != BIT_AND_EXPR)
5130 if (dump_enabled_p ())
5131 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5132 "bit-precision arithmetic not supported.\n");
5133 return false;
5136 op0 = gimple_assign_rhs1 (stmt);
5137 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
5139 if (dump_enabled_p ())
5140 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5141 "use not simple.\n");
5142 return false;
5144 /* If op0 is an external or constant def use a vector type with
5145 the same size as the output vector type. */
5146 if (!vectype)
5148 /* For boolean type we cannot determine vectype by
5149 invariant value (don't know whether it is a vector
5150 of booleans or vector of integers). We use output
5151 vectype because operations on boolean don't change
5152 type. */
5153 if (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE)
5155 if (TREE_CODE (TREE_TYPE (scalar_dest)) != BOOLEAN_TYPE)
5157 if (dump_enabled_p ())
5158 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5159 "not supported operation on bool value.\n");
5160 return false;
5162 vectype = vectype_out;
5164 else
5165 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
5167 if (vec_stmt)
5168 gcc_assert (vectype);
5169 if (!vectype)
5171 if (dump_enabled_p ())
5173 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5174 "no vectype for scalar type ");
5175 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
5176 TREE_TYPE (op0));
5177 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5180 return false;
5183 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5184 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
5185 if (nunits_out != nunits_in)
5186 return false;
5188 if (op_type == binary_op || op_type == ternary_op)
5190 op1 = gimple_assign_rhs2 (stmt);
5191 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]))
5193 if (dump_enabled_p ())
5194 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5195 "use not simple.\n");
5196 return false;
5199 if (op_type == ternary_op)
5201 op2 = gimple_assign_rhs3 (stmt);
5202 if (!vect_is_simple_use (op2, vinfo, &def_stmt, &dt[2]))
5204 if (dump_enabled_p ())
5205 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5206 "use not simple.\n");
5207 return false;
5211 if (loop_vinfo)
5212 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5213 else
5214 vf = 1;
5216 /* Multiple types in SLP are handled by creating the appropriate number of
5217 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5218 case of SLP. */
5219 if (slp_node)
5220 ncopies = 1;
5221 else
5222 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
5224 gcc_assert (ncopies >= 1);
5226 /* Shifts are handled in vectorizable_shift (). */
5227 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
5228 || code == RROTATE_EXPR)
5229 return false;
5231 /* Supportable by target? */
5233 vec_mode = TYPE_MODE (vectype);
5234 if (code == MULT_HIGHPART_EXPR)
5235 target_support_p = can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype));
5236 else
5238 optab = optab_for_tree_code (code, vectype, optab_default);
5239 if (!optab)
5241 if (dump_enabled_p ())
5242 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5243 "no optab.\n");
5244 return false;
5246 target_support_p = (optab_handler (optab, vec_mode)
5247 != CODE_FOR_nothing);
5250 if (!target_support_p)
5252 if (dump_enabled_p ())
5253 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5254 "op not supported by target.\n");
5255 /* Check only during analysis. */
5256 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
5257 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
5258 return false;
5259 if (dump_enabled_p ())
5260 dump_printf_loc (MSG_NOTE, vect_location,
5261 "proceeding using word mode.\n");
5264 /* Worthwhile without SIMD support? Check only during analysis. */
5265 if (!VECTOR_MODE_P (vec_mode)
5266 && !vec_stmt
5267 && vf < vect_min_worthwhile_factor (code))
5269 if (dump_enabled_p ())
5270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5271 "not worthwhile without SIMD support.\n");
5272 return false;
5275 if (!vec_stmt) /* transformation not required. */
5277 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
5278 if (dump_enabled_p ())
5279 dump_printf_loc (MSG_NOTE, vect_location,
5280 "=== vectorizable_operation ===\n");
5281 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
5282 return true;
5285 /** Transform. **/
5287 if (dump_enabled_p ())
5288 dump_printf_loc (MSG_NOTE, vect_location,
5289 "transform binary/unary operation.\n");
5291 /* Handle def. */
5292 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5294 /* In case the vectorization factor (VF) is bigger than the number
5295 of elements that we can fit in a vectype (nunits), we have to generate
5296 more than one vector stmt - i.e - we need to "unroll" the
5297 vector stmt by a factor VF/nunits. In doing so, we record a pointer
5298 from one copy of the vector stmt to the next, in the field
5299 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
5300 stages to find the correct vector defs to be used when vectorizing
5301 stmts that use the defs of the current stmt. The example below
5302 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
5303 we need to create 4 vectorized stmts):
5305 before vectorization:
5306 RELATED_STMT VEC_STMT
5307 S1: x = memref - -
5308 S2: z = x + 1 - -
5310 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
5311 there):
5312 RELATED_STMT VEC_STMT
5313 VS1_0: vx0 = memref0 VS1_1 -
5314 VS1_1: vx1 = memref1 VS1_2 -
5315 VS1_2: vx2 = memref2 VS1_3 -
5316 VS1_3: vx3 = memref3 - -
5317 S1: x = load - VS1_0
5318 S2: z = x + 1 - -
5320 step2: vectorize stmt S2 (done here):
5321 To vectorize stmt S2 we first need to find the relevant vector
5322 def for the first operand 'x'. This is, as usual, obtained from
5323 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
5324 that defines 'x' (S1). This way we find the stmt VS1_0, and the
5325 relevant vector def 'vx0'. Having found 'vx0' we can generate
5326 the vector stmt VS2_0, and as usual, record it in the
5327 STMT_VINFO_VEC_STMT of stmt S2.
5328 When creating the second copy (VS2_1), we obtain the relevant vector
5329 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
5330 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
5331 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
5332 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
5333 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
5334 chain of stmts and pointers:
5335 RELATED_STMT VEC_STMT
5336 VS1_0: vx0 = memref0 VS1_1 -
5337 VS1_1: vx1 = memref1 VS1_2 -
5338 VS1_2: vx2 = memref2 VS1_3 -
5339 VS1_3: vx3 = memref3 - -
5340 S1: x = load - VS1_0
5341 VS2_0: vz0 = vx0 + v1 VS2_1 -
5342 VS2_1: vz1 = vx1 + v1 VS2_2 -
5343 VS2_2: vz2 = vx2 + v1 VS2_3 -
5344 VS2_3: vz3 = vx3 + v1 - -
5345 S2: z = x + 1 - VS2_0 */
5347 prev_stmt_info = NULL;
5348 for (j = 0; j < ncopies; j++)
5350 /* Handle uses. */
5351 if (j == 0)
5353 if (op_type == binary_op || op_type == ternary_op)
5354 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
5355 slp_node, -1);
5356 else
5357 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
5358 slp_node, -1);
5359 if (op_type == ternary_op)
5360 vect_get_vec_defs (op2, NULL_TREE, stmt, &vec_oprnds2, NULL,
5361 slp_node, -1);
5363 else
5365 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
5366 if (op_type == ternary_op)
5368 tree vec_oprnd = vec_oprnds2.pop ();
5369 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
5370 vec_oprnd));
5374 /* Arguments are ready. Create the new vector stmt. */
5375 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5377 vop1 = ((op_type == binary_op || op_type == ternary_op)
5378 ? vec_oprnds1[i] : NULL_TREE);
5379 vop2 = ((op_type == ternary_op)
5380 ? vec_oprnds2[i] : NULL_TREE);
5381 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
5382 new_temp = make_ssa_name (vec_dest, new_stmt);
5383 gimple_assign_set_lhs (new_stmt, new_temp);
5384 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5385 if (slp_node)
5386 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5389 if (slp_node)
5390 continue;
5392 if (j == 0)
5393 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5394 else
5395 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5396 prev_stmt_info = vinfo_for_stmt (new_stmt);
5399 vec_oprnds0.release ();
5400 vec_oprnds1.release ();
5401 vec_oprnds2.release ();
5403 return true;
5406 /* A helper function to ensure data reference DR's base alignment
5407 for STMT_INFO. */
5409 static void
5410 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
5412 if (!dr->aux)
5413 return;
5415 if (DR_VECT_AUX (dr)->base_misaligned)
5417 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5418 tree base_decl = DR_VECT_AUX (dr)->base_decl;
5420 if (decl_in_symtab_p (base_decl))
5421 symtab_node::get (base_decl)->increase_alignment (TYPE_ALIGN (vectype));
5422 else
5424 SET_DECL_ALIGN (base_decl, TYPE_ALIGN (vectype));
5425 DECL_USER_ALIGN (base_decl) = 1;
5427 DR_VECT_AUX (dr)->base_misaligned = false;
5432 /* Function get_group_alias_ptr_type.
5434 Return the alias type for the group starting at FIRST_STMT. */
5436 static tree
5437 get_group_alias_ptr_type (gimple *first_stmt)
5439 struct data_reference *first_dr, *next_dr;
5440 gimple *next_stmt;
5442 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5443 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (first_stmt));
5444 while (next_stmt)
5446 next_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (next_stmt));
5447 if (get_alias_set (DR_REF (first_dr))
5448 != get_alias_set (DR_REF (next_dr)))
5450 if (dump_enabled_p ())
5451 dump_printf_loc (MSG_NOTE, vect_location,
5452 "conflicting alias set types.\n");
5453 return ptr_type_node;
5455 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5457 return reference_alias_ptr_type (DR_REF (first_dr));
5461 /* Function vectorizable_store.
5463 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
5464 can be vectorized.
5465 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5466 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5467 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5469 static bool
5470 vectorizable_store (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
5471 slp_tree slp_node)
5473 tree scalar_dest;
5474 tree data_ref;
5475 tree op;
5476 tree vec_oprnd = NULL_TREE;
5477 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5478 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5479 tree elem_type;
5480 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5481 struct loop *loop = NULL;
5482 machine_mode vec_mode;
5483 tree dummy;
5484 enum dr_alignment_support alignment_support_scheme;
5485 gimple *def_stmt;
5486 enum vect_def_type dt;
5487 stmt_vec_info prev_stmt_info = NULL;
5488 tree dataref_ptr = NULL_TREE;
5489 tree dataref_offset = NULL_TREE;
5490 gimple *ptr_incr = NULL;
5491 int ncopies;
5492 int j;
5493 gimple *next_stmt, *first_stmt;
5494 bool grouped_store;
5495 unsigned int group_size, i;
5496 vec<tree> oprnds = vNULL;
5497 vec<tree> result_chain = vNULL;
5498 bool inv_p;
5499 tree offset = NULL_TREE;
5500 vec<tree> vec_oprnds = vNULL;
5501 bool slp = (slp_node != NULL);
5502 unsigned int vec_num;
5503 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5504 vec_info *vinfo = stmt_info->vinfo;
5505 tree aggr_type;
5506 gather_scatter_info gs_info;
5507 enum vect_def_type scatter_src_dt = vect_unknown_def_type;
5508 gimple *new_stmt;
5509 int vf;
5510 vec_load_store_type vls_type;
5511 tree ref_type;
5513 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5514 return false;
5516 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5517 && ! vec_stmt)
5518 return false;
5520 /* Is vectorizable store? */
5522 if (!is_gimple_assign (stmt))
5523 return false;
5525 scalar_dest = gimple_assign_lhs (stmt);
5526 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
5527 && is_pattern_stmt_p (stmt_info))
5528 scalar_dest = TREE_OPERAND (scalar_dest, 0);
5529 if (TREE_CODE (scalar_dest) != ARRAY_REF
5530 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
5531 && TREE_CODE (scalar_dest) != INDIRECT_REF
5532 && TREE_CODE (scalar_dest) != COMPONENT_REF
5533 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
5534 && TREE_CODE (scalar_dest) != REALPART_EXPR
5535 && TREE_CODE (scalar_dest) != MEM_REF)
5536 return false;
5538 /* Cannot have hybrid store SLP -- that would mean storing to the
5539 same location twice. */
5540 gcc_assert (slp == PURE_SLP_STMT (stmt_info));
5542 gcc_assert (gimple_assign_single_p (stmt));
5544 tree vectype = STMT_VINFO_VECTYPE (stmt_info), rhs_vectype = NULL_TREE;
5545 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5547 if (loop_vinfo)
5549 loop = LOOP_VINFO_LOOP (loop_vinfo);
5550 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5552 else
5553 vf = 1;
5555 /* Multiple types in SLP are handled by creating the appropriate number of
5556 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5557 case of SLP. */
5558 if (slp)
5559 ncopies = 1;
5560 else
5561 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5563 gcc_assert (ncopies >= 1);
5565 /* FORNOW. This restriction should be relaxed. */
5566 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
5568 if (dump_enabled_p ())
5569 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5570 "multiple types in nested loop.\n");
5571 return false;
5574 op = gimple_assign_rhs1 (stmt);
5576 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt, &rhs_vectype))
5578 if (dump_enabled_p ())
5579 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5580 "use not simple.\n");
5581 return false;
5584 if (dt == vect_constant_def || dt == vect_external_def)
5585 vls_type = VLS_STORE_INVARIANT;
5586 else
5587 vls_type = VLS_STORE;
5589 if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
5590 return false;
5592 elem_type = TREE_TYPE (vectype);
5593 vec_mode = TYPE_MODE (vectype);
5595 /* FORNOW. In some cases can vectorize even if data-type not supported
5596 (e.g. - array initialization with 0). */
5597 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
5598 return false;
5600 if (!STMT_VINFO_DATA_REF (stmt_info))
5601 return false;
5603 vect_memory_access_type memory_access_type;
5604 if (!get_load_store_type (stmt, vectype, slp, vls_type, ncopies,
5605 &memory_access_type, &gs_info))
5606 return false;
5608 if (!vec_stmt) /* transformation not required. */
5610 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
5611 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
5612 /* The SLP costs are calculated during SLP analysis. */
5613 if (!PURE_SLP_STMT (stmt_info))
5614 vect_model_store_cost (stmt_info, ncopies, memory_access_type, dt,
5615 NULL, NULL, NULL);
5616 return true;
5618 gcc_assert (memory_access_type == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
5620 /** Transform. **/
5622 ensure_base_align (stmt_info, dr);
5624 if (memory_access_type == VMAT_GATHER_SCATTER)
5626 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE, op, src;
5627 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
5628 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5629 tree ptr, mask, var, scale, perm_mask = NULL_TREE;
5630 edge pe = loop_preheader_edge (loop);
5631 gimple_seq seq;
5632 basic_block new_bb;
5633 enum { NARROW, NONE, WIDEN } modifier;
5634 int scatter_off_nunits = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
5636 if (nunits == (unsigned int) scatter_off_nunits)
5637 modifier = NONE;
5638 else if (nunits == (unsigned int) scatter_off_nunits / 2)
5640 unsigned char *sel = XALLOCAVEC (unsigned char, scatter_off_nunits);
5641 modifier = WIDEN;
5643 for (i = 0; i < (unsigned int) scatter_off_nunits; ++i)
5644 sel[i] = i | nunits;
5646 perm_mask = vect_gen_perm_mask_checked (gs_info.offset_vectype, sel);
5647 gcc_assert (perm_mask != NULL_TREE);
5649 else if (nunits == (unsigned int) scatter_off_nunits * 2)
5651 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5652 modifier = NARROW;
5654 for (i = 0; i < (unsigned int) nunits; ++i)
5655 sel[i] = i | scatter_off_nunits;
5657 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
5658 gcc_assert (perm_mask != NULL_TREE);
5659 ncopies *= 2;
5661 else
5662 gcc_unreachable ();
5664 rettype = TREE_TYPE (TREE_TYPE (gs_info.decl));
5665 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5666 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5667 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5668 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5669 scaletype = TREE_VALUE (arglist);
5671 gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
5672 && TREE_CODE (rettype) == VOID_TYPE);
5674 ptr = fold_convert (ptrtype, gs_info.base);
5675 if (!is_gimple_min_invariant (ptr))
5677 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5678 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5679 gcc_assert (!new_bb);
5682 /* Currently we support only unconditional scatter stores,
5683 so mask should be all ones. */
5684 mask = build_int_cst (masktype, -1);
5685 mask = vect_init_vector (stmt, mask, masktype, NULL);
5687 scale = build_int_cst (scaletype, gs_info.scale);
5689 prev_stmt_info = NULL;
5690 for (j = 0; j < ncopies; ++j)
5692 if (j == 0)
5694 src = vec_oprnd1
5695 = vect_get_vec_def_for_operand (gimple_assign_rhs1 (stmt), stmt);
5696 op = vec_oprnd0
5697 = vect_get_vec_def_for_operand (gs_info.offset, stmt);
5699 else if (modifier != NONE && (j & 1))
5701 if (modifier == WIDEN)
5703 src = vec_oprnd1
5704 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5705 op = permute_vec_elements (vec_oprnd0, vec_oprnd0, perm_mask,
5706 stmt, gsi);
5708 else if (modifier == NARROW)
5710 src = permute_vec_elements (vec_oprnd1, vec_oprnd1, perm_mask,
5711 stmt, gsi);
5712 op = vec_oprnd0
5713 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
5714 vec_oprnd0);
5716 else
5717 gcc_unreachable ();
5719 else
5721 src = vec_oprnd1
5722 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5723 op = vec_oprnd0
5724 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
5725 vec_oprnd0);
5728 if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
5730 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src))
5731 == TYPE_VECTOR_SUBPARTS (srctype));
5732 var = vect_get_new_ssa_name (srctype, vect_simple_var);
5733 src = build1 (VIEW_CONVERT_EXPR, srctype, src);
5734 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
5735 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5736 src = var;
5739 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
5741 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
5742 == TYPE_VECTOR_SUBPARTS (idxtype));
5743 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
5744 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
5745 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
5746 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5747 op = var;
5750 new_stmt
5751 = gimple_build_call (gs_info.decl, 5, ptr, mask, op, src, scale);
5753 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5755 if (prev_stmt_info == NULL)
5756 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5757 else
5758 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5759 prev_stmt_info = vinfo_for_stmt (new_stmt);
5761 return true;
5764 grouped_store = STMT_VINFO_GROUPED_ACCESS (stmt_info);
5765 if (grouped_store)
5767 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5768 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5769 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5771 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
5773 /* FORNOW */
5774 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
5776 /* We vectorize all the stmts of the interleaving group when we
5777 reach the last stmt in the group. */
5778 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
5779 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
5780 && !slp)
5782 *vec_stmt = NULL;
5783 return true;
5786 if (slp)
5788 grouped_store = false;
5789 /* VEC_NUM is the number of vect stmts to be created for this
5790 group. */
5791 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5792 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5793 gcc_assert (GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt)) == first_stmt);
5794 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5795 op = gimple_assign_rhs1 (first_stmt);
5797 else
5798 /* VEC_NUM is the number of vect stmts to be created for this
5799 group. */
5800 vec_num = group_size;
5802 ref_type = get_group_alias_ptr_type (first_stmt);
5804 else
5806 first_stmt = stmt;
5807 first_dr = dr;
5808 group_size = vec_num = 1;
5809 ref_type = reference_alias_ptr_type (DR_REF (first_dr));
5812 if (dump_enabled_p ())
5813 dump_printf_loc (MSG_NOTE, vect_location,
5814 "transform store. ncopies = %d\n", ncopies);
5816 if (memory_access_type == VMAT_ELEMENTWISE
5817 || memory_access_type == VMAT_STRIDED_SLP)
5819 gimple_stmt_iterator incr_gsi;
5820 bool insert_after;
5821 gimple *incr;
5822 tree offvar;
5823 tree ivstep;
5824 tree running_off;
5825 gimple_seq stmts = NULL;
5826 tree stride_base, stride_step, alias_off;
5827 tree vec_oprnd;
5828 unsigned int g;
5830 gcc_assert (!nested_in_vect_loop_p (loop, stmt));
5832 stride_base
5833 = fold_build_pointer_plus
5834 (unshare_expr (DR_BASE_ADDRESS (first_dr)),
5835 size_binop (PLUS_EXPR,
5836 convert_to_ptrofftype (unshare_expr (DR_OFFSET (first_dr))),
5837 convert_to_ptrofftype (DR_INIT (first_dr))));
5838 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (first_dr)));
5840 /* For a store with loop-invariant (but other than power-of-2)
5841 stride (i.e. not a grouped access) like so:
5843 for (i = 0; i < n; i += stride)
5844 array[i] = ...;
5846 we generate a new induction variable and new stores from
5847 the components of the (vectorized) rhs:
5849 for (j = 0; ; j += VF*stride)
5850 vectemp = ...;
5851 tmp1 = vectemp[0];
5852 array[j] = tmp1;
5853 tmp2 = vectemp[1];
5854 array[j + stride] = tmp2;
5858 unsigned nstores = nunits;
5859 unsigned lnel = 1;
5860 tree ltype = elem_type;
5861 if (slp)
5863 if (group_size < nunits
5864 && nunits % group_size == 0)
5866 nstores = nunits / group_size;
5867 lnel = group_size;
5868 ltype = build_vector_type (elem_type, group_size);
5870 else if (group_size >= nunits
5871 && group_size % nunits == 0)
5873 nstores = 1;
5874 lnel = nunits;
5875 ltype = vectype;
5877 ltype = build_aligned_type (ltype, TYPE_ALIGN (elem_type));
5878 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5881 ivstep = stride_step;
5882 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
5883 build_int_cst (TREE_TYPE (ivstep), vf));
5885 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5887 create_iv (stride_base, ivstep, NULL,
5888 loop, &incr_gsi, insert_after,
5889 &offvar, NULL);
5890 incr = gsi_stmt (incr_gsi);
5891 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
5893 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
5894 if (stmts)
5895 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5897 prev_stmt_info = NULL;
5898 alias_off = build_int_cst (ref_type, 0);
5899 next_stmt = first_stmt;
5900 for (g = 0; g < group_size; g++)
5902 running_off = offvar;
5903 if (g)
5905 tree size = TYPE_SIZE_UNIT (ltype);
5906 tree pos = fold_build2 (MULT_EXPR, sizetype, size_int (g),
5907 size);
5908 tree newoff = copy_ssa_name (running_off, NULL);
5909 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
5910 running_off, pos);
5911 vect_finish_stmt_generation (stmt, incr, gsi);
5912 running_off = newoff;
5914 unsigned int group_el = 0;
5915 unsigned HOST_WIDE_INT
5916 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
5917 for (j = 0; j < ncopies; j++)
5919 /* We've set op and dt above, from gimple_assign_rhs1(stmt),
5920 and first_stmt == stmt. */
5921 if (j == 0)
5923 if (slp)
5925 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds, NULL,
5926 slp_node, -1);
5927 vec_oprnd = vec_oprnds[0];
5929 else
5931 gcc_assert (gimple_assign_single_p (next_stmt));
5932 op = gimple_assign_rhs1 (next_stmt);
5933 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
5936 else
5938 if (slp)
5939 vec_oprnd = vec_oprnds[j];
5940 else
5942 vect_is_simple_use (vec_oprnd, vinfo, &def_stmt, &dt);
5943 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
5947 for (i = 0; i < nstores; i++)
5949 tree newref, newoff;
5950 gimple *incr, *assign;
5951 tree size = TYPE_SIZE (ltype);
5952 /* Extract the i'th component. */
5953 tree pos = fold_build2 (MULT_EXPR, bitsizetype,
5954 bitsize_int (i), size);
5955 tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
5956 size, pos);
5958 elem = force_gimple_operand_gsi (gsi, elem, true,
5959 NULL_TREE, true,
5960 GSI_SAME_STMT);
5962 tree this_off = build_int_cst (TREE_TYPE (alias_off),
5963 group_el * elsz);
5964 newref = build2 (MEM_REF, ltype,
5965 running_off, this_off);
5967 /* And store it to *running_off. */
5968 assign = gimple_build_assign (newref, elem);
5969 vect_finish_stmt_generation (stmt, assign, gsi);
5971 group_el += lnel;
5972 if (! slp
5973 || group_el == group_size)
5975 newoff = copy_ssa_name (running_off, NULL);
5976 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
5977 running_off, stride_step);
5978 vect_finish_stmt_generation (stmt, incr, gsi);
5980 running_off = newoff;
5981 group_el = 0;
5983 if (g == group_size - 1
5984 && !slp)
5986 if (j == 0 && i == 0)
5987 STMT_VINFO_VEC_STMT (stmt_info)
5988 = *vec_stmt = assign;
5989 else
5990 STMT_VINFO_RELATED_STMT (prev_stmt_info) = assign;
5991 prev_stmt_info = vinfo_for_stmt (assign);
5995 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5996 if (slp)
5997 break;
5999 return true;
6002 auto_vec<tree> dr_chain (group_size);
6003 oprnds.create (group_size);
6005 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
6006 gcc_assert (alignment_support_scheme);
6007 /* Targets with store-lane instructions must not require explicit
6008 realignment. */
6009 gcc_assert (memory_access_type != VMAT_LOAD_STORE_LANES
6010 || alignment_support_scheme == dr_aligned
6011 || alignment_support_scheme == dr_unaligned_supported);
6013 if (memory_access_type == VMAT_CONTIGUOUS_DOWN
6014 || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
6015 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
6017 if (memory_access_type == VMAT_LOAD_STORE_LANES)
6018 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
6019 else
6020 aggr_type = vectype;
6022 /* In case the vectorization factor (VF) is bigger than the number
6023 of elements that we can fit in a vectype (nunits), we have to generate
6024 more than one vector stmt - i.e - we need to "unroll" the
6025 vector stmt by a factor VF/nunits. For more details see documentation in
6026 vect_get_vec_def_for_copy_stmt. */
6028 /* In case of interleaving (non-unit grouped access):
6030 S1: &base + 2 = x2
6031 S2: &base = x0
6032 S3: &base + 1 = x1
6033 S4: &base + 3 = x3
6035 We create vectorized stores starting from base address (the access of the
6036 first stmt in the chain (S2 in the above example), when the last store stmt
6037 of the chain (S4) is reached:
6039 VS1: &base = vx2
6040 VS2: &base + vec_size*1 = vx0
6041 VS3: &base + vec_size*2 = vx1
6042 VS4: &base + vec_size*3 = vx3
6044 Then permutation statements are generated:
6046 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
6047 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
6050 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6051 (the order of the data-refs in the output of vect_permute_store_chain
6052 corresponds to the order of scalar stmts in the interleaving chain - see
6053 the documentation of vect_permute_store_chain()).
6055 In case of both multiple types and interleaving, above vector stores and
6056 permutation stmts are created for every copy. The result vector stmts are
6057 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
6058 STMT_VINFO_RELATED_STMT for the next copies.
6061 prev_stmt_info = NULL;
6062 for (j = 0; j < ncopies; j++)
6065 if (j == 0)
6067 if (slp)
6069 /* Get vectorized arguments for SLP_NODE. */
6070 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
6071 NULL, slp_node, -1);
6073 vec_oprnd = vec_oprnds[0];
6075 else
6077 /* For interleaved stores we collect vectorized defs for all the
6078 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
6079 used as an input to vect_permute_store_chain(), and OPRNDS as
6080 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
6082 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
6083 OPRNDS are of size 1. */
6084 next_stmt = first_stmt;
6085 for (i = 0; i < group_size; i++)
6087 /* Since gaps are not supported for interleaved stores,
6088 GROUP_SIZE is the exact number of stmts in the chain.
6089 Therefore, NEXT_STMT can't be NULL_TREE. In case that
6090 there is no interleaving, GROUP_SIZE is 1, and only one
6091 iteration of the loop will be executed. */
6092 gcc_assert (next_stmt
6093 && gimple_assign_single_p (next_stmt));
6094 op = gimple_assign_rhs1 (next_stmt);
6096 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
6097 dr_chain.quick_push (vec_oprnd);
6098 oprnds.quick_push (vec_oprnd);
6099 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
6103 /* We should have catched mismatched types earlier. */
6104 gcc_assert (useless_type_conversion_p (vectype,
6105 TREE_TYPE (vec_oprnd)));
6106 bool simd_lane_access_p
6107 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
6108 if (simd_lane_access_p
6109 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
6110 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
6111 && integer_zerop (DR_OFFSET (first_dr))
6112 && integer_zerop (DR_INIT (first_dr))
6113 && alias_sets_conflict_p (get_alias_set (aggr_type),
6114 get_alias_set (TREE_TYPE (ref_type))))
6116 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
6117 dataref_offset = build_int_cst (ref_type, 0);
6118 inv_p = false;
6120 else
6121 dataref_ptr
6122 = vect_create_data_ref_ptr (first_stmt, aggr_type,
6123 simd_lane_access_p ? loop : NULL,
6124 offset, &dummy, gsi, &ptr_incr,
6125 simd_lane_access_p, &inv_p);
6126 gcc_assert (bb_vinfo || !inv_p);
6128 else
6130 /* For interleaved stores we created vectorized defs for all the
6131 defs stored in OPRNDS in the previous iteration (previous copy).
6132 DR_CHAIN is then used as an input to vect_permute_store_chain(),
6133 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
6134 next copy.
6135 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
6136 OPRNDS are of size 1. */
6137 for (i = 0; i < group_size; i++)
6139 op = oprnds[i];
6140 vect_is_simple_use (op, vinfo, &def_stmt, &dt);
6141 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
6142 dr_chain[i] = vec_oprnd;
6143 oprnds[i] = vec_oprnd;
6145 if (dataref_offset)
6146 dataref_offset
6147 = int_const_binop (PLUS_EXPR, dataref_offset,
6148 TYPE_SIZE_UNIT (aggr_type));
6149 else
6150 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
6151 TYPE_SIZE_UNIT (aggr_type));
6154 if (memory_access_type == VMAT_LOAD_STORE_LANES)
6156 tree vec_array;
6158 /* Combine all the vectors into an array. */
6159 vec_array = create_vector_array (vectype, vec_num);
6160 for (i = 0; i < vec_num; i++)
6162 vec_oprnd = dr_chain[i];
6163 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
6166 /* Emit:
6167 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
6168 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
6169 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
6170 gimple_call_set_lhs (new_stmt, data_ref);
6171 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6173 else
6175 new_stmt = NULL;
6176 if (grouped_store)
6178 if (j == 0)
6179 result_chain.create (group_size);
6180 /* Permute. */
6181 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
6182 &result_chain);
6185 next_stmt = first_stmt;
6186 for (i = 0; i < vec_num; i++)
6188 unsigned align, misalign;
6190 if (i > 0)
6191 /* Bump the vector pointer. */
6192 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6193 stmt, NULL_TREE);
6195 if (slp)
6196 vec_oprnd = vec_oprnds[i];
6197 else if (grouped_store)
6198 /* For grouped stores vectorized defs are interleaved in
6199 vect_permute_store_chain(). */
6200 vec_oprnd = result_chain[i];
6202 data_ref = fold_build2 (MEM_REF, TREE_TYPE (vec_oprnd),
6203 dataref_ptr,
6204 dataref_offset
6205 ? dataref_offset
6206 : build_int_cst (ref_type, 0));
6207 align = TYPE_ALIGN_UNIT (vectype);
6208 if (aligned_access_p (first_dr))
6209 misalign = 0;
6210 else if (DR_MISALIGNMENT (first_dr) == -1)
6212 if (DR_VECT_AUX (first_dr)->base_element_aligned)
6213 align = TYPE_ALIGN_UNIT (elem_type);
6214 else
6215 align = get_object_alignment (DR_REF (first_dr))
6216 / BITS_PER_UNIT;
6217 misalign = 0;
6218 TREE_TYPE (data_ref)
6219 = build_aligned_type (TREE_TYPE (data_ref),
6220 align * BITS_PER_UNIT);
6222 else
6224 TREE_TYPE (data_ref)
6225 = build_aligned_type (TREE_TYPE (data_ref),
6226 TYPE_ALIGN (elem_type));
6227 misalign = DR_MISALIGNMENT (first_dr);
6229 if (dataref_offset == NULL_TREE
6230 && TREE_CODE (dataref_ptr) == SSA_NAME)
6231 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
6232 misalign);
6234 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
6236 tree perm_mask = perm_mask_for_reverse (vectype);
6237 tree perm_dest
6238 = vect_create_destination_var (gimple_assign_rhs1 (stmt),
6239 vectype);
6240 tree new_temp = make_ssa_name (perm_dest);
6242 /* Generate the permute statement. */
6243 gimple *perm_stmt
6244 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
6245 vec_oprnd, perm_mask);
6246 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
6248 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
6249 vec_oprnd = new_temp;
6252 /* Arguments are ready. Create the new vector stmt. */
6253 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
6254 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6256 if (slp)
6257 continue;
6259 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
6260 if (!next_stmt)
6261 break;
6264 if (!slp)
6266 if (j == 0)
6267 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6268 else
6269 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6270 prev_stmt_info = vinfo_for_stmt (new_stmt);
6274 oprnds.release ();
6275 result_chain.release ();
6276 vec_oprnds.release ();
6278 return true;
6281 /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
6282 VECTOR_CST mask. No checks are made that the target platform supports the
6283 mask, so callers may wish to test can_vec_perm_p separately, or use
6284 vect_gen_perm_mask_checked. */
6286 tree
6287 vect_gen_perm_mask_any (tree vectype, const unsigned char *sel)
6289 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
6290 int i, nunits;
6292 nunits = TYPE_VECTOR_SUBPARTS (vectype);
6294 mask_elt_type = lang_hooks.types.type_for_mode
6295 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
6296 mask_type = get_vectype_for_scalar_type (mask_elt_type);
6298 mask_elts = XALLOCAVEC (tree, nunits);
6299 for (i = nunits - 1; i >= 0; i--)
6300 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
6301 mask_vec = build_vector (mask_type, mask_elts);
6303 return mask_vec;
6306 /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_p,
6307 i.e. that the target supports the pattern _for arbitrary input vectors_. */
6309 tree
6310 vect_gen_perm_mask_checked (tree vectype, const unsigned char *sel)
6312 gcc_assert (can_vec_perm_p (TYPE_MODE (vectype), false, sel));
6313 return vect_gen_perm_mask_any (vectype, sel);
6316 /* Given a vector variable X and Y, that was generated for the scalar
6317 STMT, generate instructions to permute the vector elements of X and Y
6318 using permutation mask MASK_VEC, insert them at *GSI and return the
6319 permuted vector variable. */
6321 static tree
6322 permute_vec_elements (tree x, tree y, tree mask_vec, gimple *stmt,
6323 gimple_stmt_iterator *gsi)
6325 tree vectype = TREE_TYPE (x);
6326 tree perm_dest, data_ref;
6327 gimple *perm_stmt;
6329 perm_dest = vect_create_destination_var (gimple_get_lhs (stmt), vectype);
6330 data_ref = make_ssa_name (perm_dest);
6332 /* Generate the permute statement. */
6333 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
6334 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
6336 return data_ref;
6339 /* Hoist the definitions of all SSA uses on STMT out of the loop LOOP,
6340 inserting them on the loops preheader edge. Returns true if we
6341 were successful in doing so (and thus STMT can be moved then),
6342 otherwise returns false. */
6344 static bool
6345 hoist_defs_of_uses (gimple *stmt, struct loop *loop)
6347 ssa_op_iter i;
6348 tree op;
6349 bool any = false;
6351 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6353 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6354 if (!gimple_nop_p (def_stmt)
6355 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6357 /* Make sure we don't need to recurse. While we could do
6358 so in simple cases when there are more complex use webs
6359 we don't have an easy way to preserve stmt order to fulfil
6360 dependencies within them. */
6361 tree op2;
6362 ssa_op_iter i2;
6363 if (gimple_code (def_stmt) == GIMPLE_PHI)
6364 return false;
6365 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
6367 gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
6368 if (!gimple_nop_p (def_stmt2)
6369 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
6370 return false;
6372 any = true;
6376 if (!any)
6377 return true;
6379 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6381 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6382 if (!gimple_nop_p (def_stmt)
6383 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6385 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
6386 gsi_remove (&gsi, false);
6387 gsi_insert_on_edge_immediate (loop_preheader_edge (loop), def_stmt);
6391 return true;
6394 /* vectorizable_load.
6396 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
6397 can be vectorized.
6398 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
6399 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
6400 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6402 static bool
6403 vectorizable_load (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
6404 slp_tree slp_node, slp_instance slp_node_instance)
6406 tree scalar_dest;
6407 tree vec_dest = NULL;
6408 tree data_ref = NULL;
6409 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6410 stmt_vec_info prev_stmt_info;
6411 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6412 struct loop *loop = NULL;
6413 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
6414 bool nested_in_vect_loop = false;
6415 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
6416 tree elem_type;
6417 tree new_temp;
6418 machine_mode mode;
6419 gimple *new_stmt = NULL;
6420 tree dummy;
6421 enum dr_alignment_support alignment_support_scheme;
6422 tree dataref_ptr = NULL_TREE;
6423 tree dataref_offset = NULL_TREE;
6424 gimple *ptr_incr = NULL;
6425 int ncopies;
6426 int i, j, group_size, group_gap_adj;
6427 tree msq = NULL_TREE, lsq;
6428 tree offset = NULL_TREE;
6429 tree byte_offset = NULL_TREE;
6430 tree realignment_token = NULL_TREE;
6431 gphi *phi = NULL;
6432 vec<tree> dr_chain = vNULL;
6433 bool grouped_load = false;
6434 gimple *first_stmt;
6435 gimple *first_stmt_for_drptr = NULL;
6436 bool inv_p;
6437 bool compute_in_loop = false;
6438 struct loop *at_loop;
6439 int vec_num;
6440 bool slp = (slp_node != NULL);
6441 bool slp_perm = false;
6442 enum tree_code code;
6443 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6444 int vf;
6445 tree aggr_type;
6446 gather_scatter_info gs_info;
6447 vec_info *vinfo = stmt_info->vinfo;
6448 tree ref_type;
6450 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6451 return false;
6453 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6454 && ! vec_stmt)
6455 return false;
6457 /* Is vectorizable load? */
6458 if (!is_gimple_assign (stmt))
6459 return false;
6461 scalar_dest = gimple_assign_lhs (stmt);
6462 if (TREE_CODE (scalar_dest) != SSA_NAME)
6463 return false;
6465 code = gimple_assign_rhs_code (stmt);
6466 if (code != ARRAY_REF
6467 && code != BIT_FIELD_REF
6468 && code != INDIRECT_REF
6469 && code != COMPONENT_REF
6470 && code != IMAGPART_EXPR
6471 && code != REALPART_EXPR
6472 && code != MEM_REF
6473 && TREE_CODE_CLASS (code) != tcc_declaration)
6474 return false;
6476 if (!STMT_VINFO_DATA_REF (stmt_info))
6477 return false;
6479 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6480 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6482 if (loop_vinfo)
6484 loop = LOOP_VINFO_LOOP (loop_vinfo);
6485 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
6486 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6488 else
6489 vf = 1;
6491 /* Multiple types in SLP are handled by creating the appropriate number of
6492 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6493 case of SLP. */
6494 if (slp)
6495 ncopies = 1;
6496 else
6497 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6499 gcc_assert (ncopies >= 1);
6501 /* FORNOW. This restriction should be relaxed. */
6502 if (nested_in_vect_loop && ncopies > 1)
6504 if (dump_enabled_p ())
6505 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6506 "multiple types in nested loop.\n");
6507 return false;
6510 /* Invalidate assumptions made by dependence analysis when vectorization
6511 on the unrolled body effectively re-orders stmts. */
6512 if (ncopies > 1
6513 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6514 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6515 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6517 if (dump_enabled_p ())
6518 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6519 "cannot perform implicit CSE when unrolling "
6520 "with negative dependence distance\n");
6521 return false;
6524 elem_type = TREE_TYPE (vectype);
6525 mode = TYPE_MODE (vectype);
6527 /* FORNOW. In some cases can vectorize even if data-type not supported
6528 (e.g. - data copies). */
6529 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
6531 if (dump_enabled_p ())
6532 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6533 "Aligned load, but unsupported type.\n");
6534 return false;
6537 /* Check if the load is a part of an interleaving chain. */
6538 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
6540 grouped_load = true;
6541 /* FORNOW */
6542 gcc_assert (!nested_in_vect_loop);
6543 gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
6545 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6546 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6548 if (slp && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
6549 slp_perm = true;
6551 /* ??? The following is overly pessimistic (as well as the loop
6552 case above) in the case we can statically determine the excess
6553 elements loaded are within the bounds of a decl that is accessed.
6554 Likewise for BB vectorizations using masked loads is a possibility. */
6555 if (bb_vinfo && slp_perm && group_size % nunits != 0)
6557 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6558 "BB vectorization with gaps at the end of a load "
6559 "is not supported\n");
6560 return false;
6563 /* Invalidate assumptions made by dependence analysis when vectorization
6564 on the unrolled body effectively re-orders stmts. */
6565 if (!PURE_SLP_STMT (stmt_info)
6566 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6567 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6568 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6570 if (dump_enabled_p ())
6571 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6572 "cannot perform implicit CSE when performing "
6573 "group loads with negative dependence distance\n");
6574 return false;
6577 /* Similarly when the stmt is a load that is both part of a SLP
6578 instance and a loop vectorized stmt via the same-dr mechanism
6579 we have to give up. */
6580 if (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)
6581 && (STMT_SLP_TYPE (stmt_info)
6582 != STMT_SLP_TYPE (vinfo_for_stmt
6583 (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)))))
6585 if (dump_enabled_p ())
6586 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6587 "conflicting SLP types for CSEd load\n");
6588 return false;
6592 vect_memory_access_type memory_access_type;
6593 if (!get_load_store_type (stmt, vectype, slp, VLS_LOAD, ncopies,
6594 &memory_access_type, &gs_info))
6595 return false;
6597 if (!vec_stmt) /* transformation not required. */
6599 if (!slp)
6600 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
6601 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
6602 /* The SLP costs are calculated during SLP analysis. */
6603 if (!PURE_SLP_STMT (stmt_info))
6604 vect_model_load_cost (stmt_info, ncopies, memory_access_type,
6605 NULL, NULL, NULL);
6606 return true;
6609 if (!slp)
6610 gcc_assert (memory_access_type
6611 == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
6613 if (dump_enabled_p ())
6614 dump_printf_loc (MSG_NOTE, vect_location,
6615 "transform load. ncopies = %d\n", ncopies);
6617 /** Transform. **/
6619 ensure_base_align (stmt_info, dr);
6621 if (memory_access_type == VMAT_GATHER_SCATTER)
6623 tree vec_oprnd0 = NULL_TREE, op;
6624 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
6625 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
6626 tree ptr, mask, var, scale, merge, perm_mask = NULL_TREE, prev_res = NULL_TREE;
6627 edge pe = loop_preheader_edge (loop);
6628 gimple_seq seq;
6629 basic_block new_bb;
6630 enum { NARROW, NONE, WIDEN } modifier;
6631 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
6633 if (nunits == gather_off_nunits)
6634 modifier = NONE;
6635 else if (nunits == gather_off_nunits / 2)
6637 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
6638 modifier = WIDEN;
6640 for (i = 0; i < gather_off_nunits; ++i)
6641 sel[i] = i | nunits;
6643 perm_mask = vect_gen_perm_mask_checked (gs_info.offset_vectype, sel);
6645 else if (nunits == gather_off_nunits * 2)
6647 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
6648 modifier = NARROW;
6650 for (i = 0; i < nunits; ++i)
6651 sel[i] = i < gather_off_nunits
6652 ? i : i + nunits - gather_off_nunits;
6654 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
6655 ncopies *= 2;
6657 else
6658 gcc_unreachable ();
6660 rettype = TREE_TYPE (TREE_TYPE (gs_info.decl));
6661 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6662 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6663 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6664 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6665 scaletype = TREE_VALUE (arglist);
6666 gcc_checking_assert (types_compatible_p (srctype, rettype));
6668 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6670 ptr = fold_convert (ptrtype, gs_info.base);
6671 if (!is_gimple_min_invariant (ptr))
6673 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
6674 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
6675 gcc_assert (!new_bb);
6678 /* Currently we support only unconditional gather loads,
6679 so mask should be all ones. */
6680 if (TREE_CODE (masktype) == INTEGER_TYPE)
6681 mask = build_int_cst (masktype, -1);
6682 else if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
6684 mask = build_int_cst (TREE_TYPE (masktype), -1);
6685 mask = build_vector_from_val (masktype, mask);
6686 mask = vect_init_vector (stmt, mask, masktype, NULL);
6688 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
6690 REAL_VALUE_TYPE r;
6691 long tmp[6];
6692 for (j = 0; j < 6; ++j)
6693 tmp[j] = -1;
6694 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
6695 mask = build_real (TREE_TYPE (masktype), r);
6696 mask = build_vector_from_val (masktype, mask);
6697 mask = vect_init_vector (stmt, mask, masktype, NULL);
6699 else
6700 gcc_unreachable ();
6702 scale = build_int_cst (scaletype, gs_info.scale);
6704 if (TREE_CODE (TREE_TYPE (rettype)) == INTEGER_TYPE)
6705 merge = build_int_cst (TREE_TYPE (rettype), 0);
6706 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rettype)))
6708 REAL_VALUE_TYPE r;
6709 long tmp[6];
6710 for (j = 0; j < 6; ++j)
6711 tmp[j] = 0;
6712 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (rettype)));
6713 merge = build_real (TREE_TYPE (rettype), r);
6715 else
6716 gcc_unreachable ();
6717 merge = build_vector_from_val (rettype, merge);
6718 merge = vect_init_vector (stmt, merge, rettype, NULL);
6720 prev_stmt_info = NULL;
6721 for (j = 0; j < ncopies; ++j)
6723 if (modifier == WIDEN && (j & 1))
6724 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
6725 perm_mask, stmt, gsi);
6726 else if (j == 0)
6727 op = vec_oprnd0
6728 = vect_get_vec_def_for_operand (gs_info.offset, stmt);
6729 else
6730 op = vec_oprnd0
6731 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt, vec_oprnd0);
6733 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
6735 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
6736 == TYPE_VECTOR_SUBPARTS (idxtype));
6737 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
6738 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
6739 new_stmt
6740 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6741 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6742 op = var;
6745 new_stmt
6746 = gimple_build_call (gs_info.decl, 5, merge, ptr, op, mask, scale);
6748 if (!useless_type_conversion_p (vectype, rettype))
6750 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
6751 == TYPE_VECTOR_SUBPARTS (rettype));
6752 op = vect_get_new_ssa_name (rettype, vect_simple_var);
6753 gimple_call_set_lhs (new_stmt, op);
6754 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6755 var = make_ssa_name (vec_dest);
6756 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
6757 new_stmt
6758 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6760 else
6762 var = make_ssa_name (vec_dest, new_stmt);
6763 gimple_call_set_lhs (new_stmt, var);
6766 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6768 if (modifier == NARROW)
6770 if ((j & 1) == 0)
6772 prev_res = var;
6773 continue;
6775 var = permute_vec_elements (prev_res, var,
6776 perm_mask, stmt, gsi);
6777 new_stmt = SSA_NAME_DEF_STMT (var);
6780 if (prev_stmt_info == NULL)
6781 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6782 else
6783 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6784 prev_stmt_info = vinfo_for_stmt (new_stmt);
6786 return true;
6789 if (memory_access_type == VMAT_ELEMENTWISE
6790 || memory_access_type == VMAT_STRIDED_SLP)
6792 gimple_stmt_iterator incr_gsi;
6793 bool insert_after;
6794 gimple *incr;
6795 tree offvar;
6796 tree ivstep;
6797 tree running_off;
6798 vec<constructor_elt, va_gc> *v = NULL;
6799 gimple_seq stmts = NULL;
6800 tree stride_base, stride_step, alias_off;
6802 gcc_assert (!nested_in_vect_loop);
6804 if (slp && grouped_load)
6806 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6807 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
6808 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6809 ref_type = get_group_alias_ptr_type (first_stmt);
6811 else
6813 first_stmt = stmt;
6814 first_dr = dr;
6815 group_size = 1;
6816 ref_type = reference_alias_ptr_type (DR_REF (first_dr));
6819 stride_base
6820 = fold_build_pointer_plus
6821 (DR_BASE_ADDRESS (first_dr),
6822 size_binop (PLUS_EXPR,
6823 convert_to_ptrofftype (DR_OFFSET (first_dr)),
6824 convert_to_ptrofftype (DR_INIT (first_dr))));
6825 stride_step = fold_convert (sizetype, DR_STEP (first_dr));
6827 /* For a load with loop-invariant (but other than power-of-2)
6828 stride (i.e. not a grouped access) like so:
6830 for (i = 0; i < n; i += stride)
6831 ... = array[i];
6833 we generate a new induction variable and new accesses to
6834 form a new vector (or vectors, depending on ncopies):
6836 for (j = 0; ; j += VF*stride)
6837 tmp1 = array[j];
6838 tmp2 = array[j + stride];
6840 vectemp = {tmp1, tmp2, ...}
6843 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
6844 build_int_cst (TREE_TYPE (stride_step), vf));
6846 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6848 create_iv (unshare_expr (stride_base), unshare_expr (ivstep), NULL,
6849 loop, &incr_gsi, insert_after,
6850 &offvar, NULL);
6851 incr = gsi_stmt (incr_gsi);
6852 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
6854 stride_step = force_gimple_operand (unshare_expr (stride_step),
6855 &stmts, true, NULL_TREE);
6856 if (stmts)
6857 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
6859 prev_stmt_info = NULL;
6860 running_off = offvar;
6861 alias_off = build_int_cst (ref_type, 0);
6862 int nloads = nunits;
6863 int lnel = 1;
6864 tree ltype = TREE_TYPE (vectype);
6865 tree lvectype = vectype;
6866 auto_vec<tree> dr_chain;
6867 if (memory_access_type == VMAT_STRIDED_SLP)
6869 if (group_size < nunits)
6871 /* Avoid emitting a constructor of vector elements by performing
6872 the loads using an integer type of the same size,
6873 constructing a vector of those and then re-interpreting it
6874 as the original vector type. This works around the fact
6875 that the vec_init optab was only designed for scalar
6876 element modes and thus expansion goes through memory.
6877 This avoids a huge runtime penalty due to the general
6878 inability to perform store forwarding from smaller stores
6879 to a larger load. */
6880 unsigned lsize
6881 = group_size * TYPE_PRECISION (TREE_TYPE (vectype));
6882 enum machine_mode elmode = mode_for_size (lsize, MODE_INT, 0);
6883 enum machine_mode vmode = mode_for_vector (elmode,
6884 nunits / group_size);
6885 /* If we can't construct such a vector fall back to
6886 element loads of the original vector type. */
6887 if (VECTOR_MODE_P (vmode)
6888 && optab_handler (vec_init_optab, vmode) != CODE_FOR_nothing)
6890 nloads = nunits / group_size;
6891 lnel = group_size;
6892 ltype = build_nonstandard_integer_type (lsize, 1);
6893 lvectype = build_vector_type (ltype, nloads);
6896 else
6898 nloads = 1;
6899 lnel = nunits;
6900 ltype = vectype;
6902 ltype = build_aligned_type (ltype, TYPE_ALIGN (TREE_TYPE (vectype)));
6904 if (slp)
6906 /* For SLP permutation support we need to load the whole group,
6907 not only the number of vector stmts the permutation result
6908 fits in. */
6909 if (slp_perm)
6911 ncopies = (group_size * vf + nunits - 1) / nunits;
6912 dr_chain.create (ncopies);
6914 else
6915 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6917 int group_el = 0;
6918 unsigned HOST_WIDE_INT
6919 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
6920 for (j = 0; j < ncopies; j++)
6922 if (nloads > 1)
6923 vec_alloc (v, nloads);
6924 for (i = 0; i < nloads; i++)
6926 tree this_off = build_int_cst (TREE_TYPE (alias_off),
6927 group_el * elsz);
6928 new_stmt = gimple_build_assign (make_ssa_name (ltype),
6929 build2 (MEM_REF, ltype,
6930 running_off, this_off));
6931 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6932 if (nloads > 1)
6933 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
6934 gimple_assign_lhs (new_stmt));
6936 group_el += lnel;
6937 if (! slp
6938 || group_el == group_size)
6940 tree newoff = copy_ssa_name (running_off);
6941 gimple *incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6942 running_off, stride_step);
6943 vect_finish_stmt_generation (stmt, incr, gsi);
6945 running_off = newoff;
6946 group_el = 0;
6949 if (nloads > 1)
6951 tree vec_inv = build_constructor (lvectype, v);
6952 new_temp = vect_init_vector (stmt, vec_inv, lvectype, gsi);
6953 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6954 if (lvectype != vectype)
6956 new_stmt = gimple_build_assign (make_ssa_name (vectype),
6957 VIEW_CONVERT_EXPR,
6958 build1 (VIEW_CONVERT_EXPR,
6959 vectype, new_temp));
6960 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6964 if (slp)
6966 if (slp_perm)
6967 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
6968 else
6969 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6971 else
6973 if (j == 0)
6974 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6975 else
6976 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6977 prev_stmt_info = vinfo_for_stmt (new_stmt);
6980 if (slp_perm)
6981 vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
6982 slp_node_instance, false);
6983 return true;
6986 if (grouped_load)
6988 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6989 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6990 /* For SLP vectorization we directly vectorize a subchain
6991 without permutation. */
6992 if (slp && ! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
6993 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6994 /* For BB vectorization always use the first stmt to base
6995 the data ref pointer on. */
6996 if (bb_vinfo)
6997 first_stmt_for_drptr = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6999 /* Check if the chain of loads is already vectorized. */
7000 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
7001 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
7002 ??? But we can only do so if there is exactly one
7003 as we have no way to get at the rest. Leave the CSE
7004 opportunity alone.
7005 ??? With the group load eventually participating
7006 in multiple different permutations (having multiple
7007 slp nodes which refer to the same group) the CSE
7008 is even wrong code. See PR56270. */
7009 && !slp)
7011 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
7012 return true;
7014 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
7015 group_gap_adj = 0;
7017 /* VEC_NUM is the number of vect stmts to be created for this group. */
7018 if (slp)
7020 grouped_load = false;
7021 /* For SLP permutation support we need to load the whole group,
7022 not only the number of vector stmts the permutation result
7023 fits in. */
7024 if (slp_perm)
7025 vec_num = (group_size * vf + nunits - 1) / nunits;
7026 else
7027 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
7028 group_gap_adj = vf * group_size - nunits * vec_num;
7030 else
7031 vec_num = group_size;
7033 ref_type = get_group_alias_ptr_type (first_stmt);
7035 else
7037 first_stmt = stmt;
7038 first_dr = dr;
7039 group_size = vec_num = 1;
7040 group_gap_adj = 0;
7041 ref_type = reference_alias_ptr_type (DR_REF (first_dr));
7044 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
7045 gcc_assert (alignment_support_scheme);
7046 /* Targets with load-lane instructions must not require explicit
7047 realignment. */
7048 gcc_assert (memory_access_type != VMAT_LOAD_STORE_LANES
7049 || alignment_support_scheme == dr_aligned
7050 || alignment_support_scheme == dr_unaligned_supported);
7052 /* In case the vectorization factor (VF) is bigger than the number
7053 of elements that we can fit in a vectype (nunits), we have to generate
7054 more than one vector stmt - i.e - we need to "unroll" the
7055 vector stmt by a factor VF/nunits. In doing so, we record a pointer
7056 from one copy of the vector stmt to the next, in the field
7057 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
7058 stages to find the correct vector defs to be used when vectorizing
7059 stmts that use the defs of the current stmt. The example below
7060 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
7061 need to create 4 vectorized stmts):
7063 before vectorization:
7064 RELATED_STMT VEC_STMT
7065 S1: x = memref - -
7066 S2: z = x + 1 - -
7068 step 1: vectorize stmt S1:
7069 We first create the vector stmt VS1_0, and, as usual, record a
7070 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
7071 Next, we create the vector stmt VS1_1, and record a pointer to
7072 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
7073 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
7074 stmts and pointers:
7075 RELATED_STMT VEC_STMT
7076 VS1_0: vx0 = memref0 VS1_1 -
7077 VS1_1: vx1 = memref1 VS1_2 -
7078 VS1_2: vx2 = memref2 VS1_3 -
7079 VS1_3: vx3 = memref3 - -
7080 S1: x = load - VS1_0
7081 S2: z = x + 1 - -
7083 See in documentation in vect_get_vec_def_for_stmt_copy for how the
7084 information we recorded in RELATED_STMT field is used to vectorize
7085 stmt S2. */
7087 /* In case of interleaving (non-unit grouped access):
7089 S1: x2 = &base + 2
7090 S2: x0 = &base
7091 S3: x1 = &base + 1
7092 S4: x3 = &base + 3
7094 Vectorized loads are created in the order of memory accesses
7095 starting from the access of the first stmt of the chain:
7097 VS1: vx0 = &base
7098 VS2: vx1 = &base + vec_size*1
7099 VS3: vx3 = &base + vec_size*2
7100 VS4: vx4 = &base + vec_size*3
7102 Then permutation statements are generated:
7104 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
7105 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
7108 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
7109 (the order of the data-refs in the output of vect_permute_load_chain
7110 corresponds to the order of scalar stmts in the interleaving chain - see
7111 the documentation of vect_permute_load_chain()).
7112 The generation of permutation stmts and recording them in
7113 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
7115 In case of both multiple types and interleaving, the vector loads and
7116 permutation stmts above are created for every copy. The result vector
7117 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
7118 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
7120 /* If the data reference is aligned (dr_aligned) or potentially unaligned
7121 on a target that supports unaligned accesses (dr_unaligned_supported)
7122 we generate the following code:
7123 p = initial_addr;
7124 indx = 0;
7125 loop {
7126 p = p + indx * vectype_size;
7127 vec_dest = *(p);
7128 indx = indx + 1;
7131 Otherwise, the data reference is potentially unaligned on a target that
7132 does not support unaligned accesses (dr_explicit_realign_optimized) -
7133 then generate the following code, in which the data in each iteration is
7134 obtained by two vector loads, one from the previous iteration, and one
7135 from the current iteration:
7136 p1 = initial_addr;
7137 msq_init = *(floor(p1))
7138 p2 = initial_addr + VS - 1;
7139 realignment_token = call target_builtin;
7140 indx = 0;
7141 loop {
7142 p2 = p2 + indx * vectype_size
7143 lsq = *(floor(p2))
7144 vec_dest = realign_load (msq, lsq, realignment_token)
7145 indx = indx + 1;
7146 msq = lsq;
7147 } */
7149 /* If the misalignment remains the same throughout the execution of the
7150 loop, we can create the init_addr and permutation mask at the loop
7151 preheader. Otherwise, it needs to be created inside the loop.
7152 This can only occur when vectorizing memory accesses in the inner-loop
7153 nested within an outer-loop that is being vectorized. */
7155 if (nested_in_vect_loop
7156 && (TREE_INT_CST_LOW (DR_STEP (dr))
7157 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
7159 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
7160 compute_in_loop = true;
7163 if ((alignment_support_scheme == dr_explicit_realign_optimized
7164 || alignment_support_scheme == dr_explicit_realign)
7165 && !compute_in_loop)
7167 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
7168 alignment_support_scheme, NULL_TREE,
7169 &at_loop);
7170 if (alignment_support_scheme == dr_explicit_realign_optimized)
7172 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (msq));
7173 byte_offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
7174 size_one_node);
7177 else
7178 at_loop = loop;
7180 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
7181 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
7183 if (memory_access_type == VMAT_LOAD_STORE_LANES)
7184 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
7185 else
7186 aggr_type = vectype;
7188 prev_stmt_info = NULL;
7189 for (j = 0; j < ncopies; j++)
7191 /* 1. Create the vector or array pointer update chain. */
7192 if (j == 0)
7194 bool simd_lane_access_p
7195 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
7196 if (simd_lane_access_p
7197 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
7198 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
7199 && integer_zerop (DR_OFFSET (first_dr))
7200 && integer_zerop (DR_INIT (first_dr))
7201 && alias_sets_conflict_p (get_alias_set (aggr_type),
7202 get_alias_set (TREE_TYPE (ref_type)))
7203 && (alignment_support_scheme == dr_aligned
7204 || alignment_support_scheme == dr_unaligned_supported))
7206 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
7207 dataref_offset = build_int_cst (ref_type, 0);
7208 inv_p = false;
7210 else if (first_stmt_for_drptr
7211 && first_stmt != first_stmt_for_drptr)
7213 dataref_ptr
7214 = vect_create_data_ref_ptr (first_stmt_for_drptr, aggr_type,
7215 at_loop, offset, &dummy, gsi,
7216 &ptr_incr, simd_lane_access_p,
7217 &inv_p, byte_offset);
7218 /* Adjust the pointer by the difference to first_stmt. */
7219 data_reference_p ptrdr
7220 = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt_for_drptr));
7221 tree diff = fold_convert (sizetype,
7222 size_binop (MINUS_EXPR,
7223 DR_INIT (first_dr),
7224 DR_INIT (ptrdr)));
7225 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7226 stmt, diff);
7228 else
7229 dataref_ptr
7230 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
7231 offset, &dummy, gsi, &ptr_incr,
7232 simd_lane_access_p, &inv_p,
7233 byte_offset);
7235 else if (dataref_offset)
7236 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
7237 TYPE_SIZE_UNIT (aggr_type));
7238 else
7239 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
7240 TYPE_SIZE_UNIT (aggr_type));
7242 if (grouped_load || slp_perm)
7243 dr_chain.create (vec_num);
7245 if (memory_access_type == VMAT_LOAD_STORE_LANES)
7247 tree vec_array;
7249 vec_array = create_vector_array (vectype, vec_num);
7251 /* Emit:
7252 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
7253 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
7254 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
7255 gimple_call_set_lhs (new_stmt, vec_array);
7256 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7258 /* Extract each vector into an SSA_NAME. */
7259 for (i = 0; i < vec_num; i++)
7261 new_temp = read_vector_array (stmt, gsi, scalar_dest,
7262 vec_array, i);
7263 dr_chain.quick_push (new_temp);
7266 /* Record the mapping between SSA_NAMEs and statements. */
7267 vect_record_grouped_load_vectors (stmt, dr_chain);
7269 else
7271 for (i = 0; i < vec_num; i++)
7273 if (i > 0)
7274 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7275 stmt, NULL_TREE);
7277 /* 2. Create the vector-load in the loop. */
7278 switch (alignment_support_scheme)
7280 case dr_aligned:
7281 case dr_unaligned_supported:
7283 unsigned int align, misalign;
7285 data_ref
7286 = fold_build2 (MEM_REF, vectype, dataref_ptr,
7287 dataref_offset
7288 ? dataref_offset
7289 : build_int_cst (ref_type, 0));
7290 align = TYPE_ALIGN_UNIT (vectype);
7291 if (alignment_support_scheme == dr_aligned)
7293 gcc_assert (aligned_access_p (first_dr));
7294 misalign = 0;
7296 else if (DR_MISALIGNMENT (first_dr) == -1)
7298 if (DR_VECT_AUX (first_dr)->base_element_aligned)
7299 align = TYPE_ALIGN_UNIT (elem_type);
7300 else
7301 align = (get_object_alignment (DR_REF (first_dr))
7302 / BITS_PER_UNIT);
7303 misalign = 0;
7304 TREE_TYPE (data_ref)
7305 = build_aligned_type (TREE_TYPE (data_ref),
7306 align * BITS_PER_UNIT);
7308 else
7310 TREE_TYPE (data_ref)
7311 = build_aligned_type (TREE_TYPE (data_ref),
7312 TYPE_ALIGN (elem_type));
7313 misalign = DR_MISALIGNMENT (first_dr);
7315 if (dataref_offset == NULL_TREE
7316 && TREE_CODE (dataref_ptr) == SSA_NAME)
7317 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
7318 align, misalign);
7319 break;
7321 case dr_explicit_realign:
7323 tree ptr, bump;
7325 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
7327 if (compute_in_loop)
7328 msq = vect_setup_realignment (first_stmt, gsi,
7329 &realignment_token,
7330 dr_explicit_realign,
7331 dataref_ptr, NULL);
7333 if (TREE_CODE (dataref_ptr) == SSA_NAME)
7334 ptr = copy_ssa_name (dataref_ptr);
7335 else
7336 ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
7337 new_stmt = gimple_build_assign
7338 (ptr, BIT_AND_EXPR, dataref_ptr,
7339 build_int_cst
7340 (TREE_TYPE (dataref_ptr),
7341 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
7342 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7343 data_ref
7344 = build2 (MEM_REF, vectype, ptr,
7345 build_int_cst (ref_type, 0));
7346 vec_dest = vect_create_destination_var (scalar_dest,
7347 vectype);
7348 new_stmt = gimple_build_assign (vec_dest, data_ref);
7349 new_temp = make_ssa_name (vec_dest, new_stmt);
7350 gimple_assign_set_lhs (new_stmt, new_temp);
7351 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
7352 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
7353 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7354 msq = new_temp;
7356 bump = size_binop (MULT_EXPR, vs,
7357 TYPE_SIZE_UNIT (elem_type));
7358 bump = size_binop (MINUS_EXPR, bump, size_one_node);
7359 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
7360 new_stmt = gimple_build_assign
7361 (NULL_TREE, BIT_AND_EXPR, ptr,
7362 build_int_cst
7363 (TREE_TYPE (ptr),
7364 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
7365 ptr = copy_ssa_name (ptr, new_stmt);
7366 gimple_assign_set_lhs (new_stmt, ptr);
7367 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7368 data_ref
7369 = build2 (MEM_REF, vectype, ptr,
7370 build_int_cst (ref_type, 0));
7371 break;
7373 case dr_explicit_realign_optimized:
7374 if (TREE_CODE (dataref_ptr) == SSA_NAME)
7375 new_temp = copy_ssa_name (dataref_ptr);
7376 else
7377 new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
7378 new_stmt = gimple_build_assign
7379 (new_temp, BIT_AND_EXPR, dataref_ptr,
7380 build_int_cst
7381 (TREE_TYPE (dataref_ptr),
7382 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
7383 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7384 data_ref
7385 = build2 (MEM_REF, vectype, new_temp,
7386 build_int_cst (ref_type, 0));
7387 break;
7388 default:
7389 gcc_unreachable ();
7391 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7392 new_stmt = gimple_build_assign (vec_dest, data_ref);
7393 new_temp = make_ssa_name (vec_dest, new_stmt);
7394 gimple_assign_set_lhs (new_stmt, new_temp);
7395 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7397 /* 3. Handle explicit realignment if necessary/supported.
7398 Create in loop:
7399 vec_dest = realign_load (msq, lsq, realignment_token) */
7400 if (alignment_support_scheme == dr_explicit_realign_optimized
7401 || alignment_support_scheme == dr_explicit_realign)
7403 lsq = gimple_assign_lhs (new_stmt);
7404 if (!realignment_token)
7405 realignment_token = dataref_ptr;
7406 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7407 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR,
7408 msq, lsq, realignment_token);
7409 new_temp = make_ssa_name (vec_dest, new_stmt);
7410 gimple_assign_set_lhs (new_stmt, new_temp);
7411 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7413 if (alignment_support_scheme == dr_explicit_realign_optimized)
7415 gcc_assert (phi);
7416 if (i == vec_num - 1 && j == ncopies - 1)
7417 add_phi_arg (phi, lsq,
7418 loop_latch_edge (containing_loop),
7419 UNKNOWN_LOCATION);
7420 msq = lsq;
7424 /* 4. Handle invariant-load. */
7425 if (inv_p && !bb_vinfo)
7427 gcc_assert (!grouped_load);
7428 /* If we have versioned for aliasing or the loop doesn't
7429 have any data dependencies that would preclude this,
7430 then we are sure this is a loop invariant load and
7431 thus we can insert it on the preheader edge. */
7432 if (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
7433 && !nested_in_vect_loop
7434 && hoist_defs_of_uses (stmt, loop))
7436 if (dump_enabled_p ())
7438 dump_printf_loc (MSG_NOTE, vect_location,
7439 "hoisting out of the vectorized "
7440 "loop: ");
7441 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7443 tree tem = copy_ssa_name (scalar_dest);
7444 gsi_insert_on_edge_immediate
7445 (loop_preheader_edge (loop),
7446 gimple_build_assign (tem,
7447 unshare_expr
7448 (gimple_assign_rhs1 (stmt))));
7449 new_temp = vect_init_vector (stmt, tem, vectype, NULL);
7450 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7451 set_vinfo_for_stmt (new_stmt,
7452 new_stmt_vec_info (new_stmt, vinfo));
7454 else
7456 gimple_stmt_iterator gsi2 = *gsi;
7457 gsi_next (&gsi2);
7458 new_temp = vect_init_vector (stmt, scalar_dest,
7459 vectype, &gsi2);
7460 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7464 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
7466 tree perm_mask = perm_mask_for_reverse (vectype);
7467 new_temp = permute_vec_elements (new_temp, new_temp,
7468 perm_mask, stmt, gsi);
7469 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7472 /* Collect vector loads and later create their permutation in
7473 vect_transform_grouped_load (). */
7474 if (grouped_load || slp_perm)
7475 dr_chain.quick_push (new_temp);
7477 /* Store vector loads in the corresponding SLP_NODE. */
7478 if (slp && !slp_perm)
7479 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7481 /* Bump the vector pointer to account for a gap or for excess
7482 elements loaded for a permuted SLP load. */
7483 if (group_gap_adj != 0)
7485 bool ovf;
7486 tree bump
7487 = wide_int_to_tree (sizetype,
7488 wi::smul (TYPE_SIZE_UNIT (elem_type),
7489 group_gap_adj, &ovf));
7490 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7491 stmt, bump);
7495 if (slp && !slp_perm)
7496 continue;
7498 if (slp_perm)
7500 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
7501 slp_node_instance, false))
7503 dr_chain.release ();
7504 return false;
7507 else
7509 if (grouped_load)
7511 if (memory_access_type != VMAT_LOAD_STORE_LANES)
7512 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
7513 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
7515 else
7517 if (j == 0)
7518 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7519 else
7520 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7521 prev_stmt_info = vinfo_for_stmt (new_stmt);
7524 dr_chain.release ();
7527 return true;
7530 /* Function vect_is_simple_cond.
7532 Input:
7533 LOOP - the loop that is being vectorized.
7534 COND - Condition that is checked for simple use.
7536 Output:
7537 *COMP_VECTYPE - the vector type for the comparison.
7539 Returns whether a COND can be vectorized. Checks whether
7540 condition operands are supportable using vec_is_simple_use. */
7542 static bool
7543 vect_is_simple_cond (tree cond, vec_info *vinfo, tree *comp_vectype)
7545 tree lhs, rhs;
7546 enum vect_def_type dt;
7547 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7549 /* Mask case. */
7550 if (TREE_CODE (cond) == SSA_NAME
7551 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)
7553 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (cond);
7554 if (!vect_is_simple_use (cond, vinfo, &lhs_def_stmt,
7555 &dt, comp_vectype)
7556 || !*comp_vectype
7557 || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
7558 return false;
7559 return true;
7562 if (!COMPARISON_CLASS_P (cond))
7563 return false;
7565 lhs = TREE_OPERAND (cond, 0);
7566 rhs = TREE_OPERAND (cond, 1);
7568 if (TREE_CODE (lhs) == SSA_NAME)
7570 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
7571 if (!vect_is_simple_use (lhs, vinfo, &lhs_def_stmt, &dt, &vectype1))
7572 return false;
7574 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
7575 && TREE_CODE (lhs) != FIXED_CST)
7576 return false;
7578 if (TREE_CODE (rhs) == SSA_NAME)
7580 gimple *rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
7581 if (!vect_is_simple_use (rhs, vinfo, &rhs_def_stmt, &dt, &vectype2))
7582 return false;
7584 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
7585 && TREE_CODE (rhs) != FIXED_CST)
7586 return false;
7588 if (vectype1 && vectype2
7589 && TYPE_VECTOR_SUBPARTS (vectype1) != TYPE_VECTOR_SUBPARTS (vectype2))
7590 return false;
7592 *comp_vectype = vectype1 ? vectype1 : vectype2;
7593 return true;
7596 /* vectorizable_condition.
7598 Check if STMT is conditional modify expression that can be vectorized.
7599 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7600 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
7601 at GSI.
7603 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
7604 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
7605 else clause if it is 2).
7607 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7609 bool
7610 vectorizable_condition (gimple *stmt, gimple_stmt_iterator *gsi,
7611 gimple **vec_stmt, tree reduc_def, int reduc_index,
7612 slp_tree slp_node)
7614 tree scalar_dest = NULL_TREE;
7615 tree vec_dest = NULL_TREE;
7616 tree cond_expr, then_clause, else_clause;
7617 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7618 tree comp_vectype = NULL_TREE;
7619 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
7620 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
7621 tree vec_compare;
7622 tree new_temp;
7623 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
7624 enum vect_def_type dt, dts[4];
7625 int ncopies;
7626 enum tree_code code;
7627 stmt_vec_info prev_stmt_info = NULL;
7628 int i, j;
7629 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7630 vec<tree> vec_oprnds0 = vNULL;
7631 vec<tree> vec_oprnds1 = vNULL;
7632 vec<tree> vec_oprnds2 = vNULL;
7633 vec<tree> vec_oprnds3 = vNULL;
7634 tree vec_cmp_type;
7635 bool masked = false;
7637 if (reduc_index && STMT_SLP_TYPE (stmt_info))
7638 return false;
7640 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == TREE_CODE_REDUCTION)
7642 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7643 return false;
7645 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7646 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7647 && reduc_def))
7648 return false;
7650 /* FORNOW: not yet supported. */
7651 if (STMT_VINFO_LIVE_P (stmt_info))
7653 if (dump_enabled_p ())
7654 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7655 "value used after loop.\n");
7656 return false;
7660 /* Is vectorizable conditional operation? */
7661 if (!is_gimple_assign (stmt))
7662 return false;
7664 code = gimple_assign_rhs_code (stmt);
7666 if (code != COND_EXPR)
7667 return false;
7669 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7670 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
7671 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7673 if (slp_node)
7674 ncopies = 1;
7675 else
7676 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7678 gcc_assert (ncopies >= 1);
7679 if (reduc_index && ncopies > 1)
7680 return false; /* FORNOW */
7682 cond_expr = gimple_assign_rhs1 (stmt);
7683 then_clause = gimple_assign_rhs2 (stmt);
7684 else_clause = gimple_assign_rhs3 (stmt);
7686 if (!vect_is_simple_cond (cond_expr, stmt_info->vinfo, &comp_vectype)
7687 || !comp_vectype)
7688 return false;
7690 gimple *def_stmt;
7691 if (!vect_is_simple_use (then_clause, stmt_info->vinfo, &def_stmt, &dt,
7692 &vectype1))
7693 return false;
7694 if (!vect_is_simple_use (else_clause, stmt_info->vinfo, &def_stmt, &dt,
7695 &vectype2))
7696 return false;
7698 if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
7699 return false;
7701 if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
7702 return false;
7704 masked = !COMPARISON_CLASS_P (cond_expr);
7705 vec_cmp_type = build_same_sized_truth_vector_type (comp_vectype);
7707 if (vec_cmp_type == NULL_TREE)
7708 return false;
7710 if (!vec_stmt)
7712 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
7713 return expand_vec_cond_expr_p (vectype, comp_vectype);
7716 /* Transform. */
7718 if (!slp_node)
7720 vec_oprnds0.create (1);
7721 vec_oprnds1.create (1);
7722 vec_oprnds2.create (1);
7723 vec_oprnds3.create (1);
7726 /* Handle def. */
7727 scalar_dest = gimple_assign_lhs (stmt);
7728 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7730 /* Handle cond expr. */
7731 for (j = 0; j < ncopies; j++)
7733 gassign *new_stmt = NULL;
7734 if (j == 0)
7736 if (slp_node)
7738 auto_vec<tree, 4> ops;
7739 auto_vec<vec<tree>, 4> vec_defs;
7741 if (masked)
7742 ops.safe_push (cond_expr);
7743 else
7745 ops.safe_push (TREE_OPERAND (cond_expr, 0));
7746 ops.safe_push (TREE_OPERAND (cond_expr, 1));
7748 ops.safe_push (then_clause);
7749 ops.safe_push (else_clause);
7750 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
7751 vec_oprnds3 = vec_defs.pop ();
7752 vec_oprnds2 = vec_defs.pop ();
7753 if (!masked)
7754 vec_oprnds1 = vec_defs.pop ();
7755 vec_oprnds0 = vec_defs.pop ();
7757 else
7759 gimple *gtemp;
7760 if (masked)
7762 vec_cond_lhs
7763 = vect_get_vec_def_for_operand (cond_expr, stmt,
7764 comp_vectype);
7765 vect_is_simple_use (cond_expr, stmt_info->vinfo,
7766 &gtemp, &dts[0]);
7768 else
7770 vec_cond_lhs =
7771 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
7772 stmt, comp_vectype);
7773 vect_is_simple_use (TREE_OPERAND (cond_expr, 0),
7774 loop_vinfo, &gtemp, &dts[0]);
7776 vec_cond_rhs =
7777 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
7778 stmt, comp_vectype);
7779 vect_is_simple_use (TREE_OPERAND (cond_expr, 1),
7780 loop_vinfo, &gtemp, &dts[1]);
7782 if (reduc_index == 1)
7783 vec_then_clause = reduc_def;
7784 else
7786 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
7787 stmt);
7788 vect_is_simple_use (then_clause, loop_vinfo,
7789 &gtemp, &dts[2]);
7791 if (reduc_index == 2)
7792 vec_else_clause = reduc_def;
7793 else
7795 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
7796 stmt);
7797 vect_is_simple_use (else_clause, loop_vinfo, &gtemp, &dts[3]);
7801 else
7803 vec_cond_lhs
7804 = vect_get_vec_def_for_stmt_copy (dts[0],
7805 vec_oprnds0.pop ());
7806 if (!masked)
7807 vec_cond_rhs
7808 = vect_get_vec_def_for_stmt_copy (dts[1],
7809 vec_oprnds1.pop ());
7811 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
7812 vec_oprnds2.pop ());
7813 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
7814 vec_oprnds3.pop ());
7817 if (!slp_node)
7819 vec_oprnds0.quick_push (vec_cond_lhs);
7820 if (!masked)
7821 vec_oprnds1.quick_push (vec_cond_rhs);
7822 vec_oprnds2.quick_push (vec_then_clause);
7823 vec_oprnds3.quick_push (vec_else_clause);
7826 /* Arguments are ready. Create the new vector stmt. */
7827 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
7829 vec_then_clause = vec_oprnds2[i];
7830 vec_else_clause = vec_oprnds3[i];
7832 if (masked)
7833 vec_compare = vec_cond_lhs;
7834 else
7836 vec_cond_rhs = vec_oprnds1[i];
7837 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
7838 vec_cond_lhs, vec_cond_rhs);
7840 new_temp = make_ssa_name (vec_dest);
7841 new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR,
7842 vec_compare, vec_then_clause,
7843 vec_else_clause);
7844 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7845 if (slp_node)
7846 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7849 if (slp_node)
7850 continue;
7852 if (j == 0)
7853 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7854 else
7855 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7857 prev_stmt_info = vinfo_for_stmt (new_stmt);
7860 vec_oprnds0.release ();
7861 vec_oprnds1.release ();
7862 vec_oprnds2.release ();
7863 vec_oprnds3.release ();
7865 return true;
7868 /* vectorizable_comparison.
7870 Check if STMT is comparison expression that can be vectorized.
7871 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7872 comparison, put it in VEC_STMT, and insert it at GSI.
7874 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7876 static bool
7877 vectorizable_comparison (gimple *stmt, gimple_stmt_iterator *gsi,
7878 gimple **vec_stmt, tree reduc_def,
7879 slp_tree slp_node)
7881 tree lhs, rhs1, rhs2;
7882 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7883 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7884 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7885 tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
7886 tree new_temp;
7887 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
7888 enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
7889 unsigned nunits;
7890 int ncopies;
7891 enum tree_code code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
7892 stmt_vec_info prev_stmt_info = NULL;
7893 int i, j;
7894 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7895 vec<tree> vec_oprnds0 = vNULL;
7896 vec<tree> vec_oprnds1 = vNULL;
7897 gimple *def_stmt;
7898 tree mask_type;
7899 tree mask;
7901 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7902 return false;
7904 if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
7905 return false;
7907 mask_type = vectype;
7908 nunits = TYPE_VECTOR_SUBPARTS (vectype);
7910 if (slp_node)
7911 ncopies = 1;
7912 else
7913 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7915 gcc_assert (ncopies >= 1);
7916 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7917 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7918 && reduc_def))
7919 return false;
7921 if (STMT_VINFO_LIVE_P (stmt_info))
7923 if (dump_enabled_p ())
7924 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7925 "value used after loop.\n");
7926 return false;
7929 if (!is_gimple_assign (stmt))
7930 return false;
7932 code = gimple_assign_rhs_code (stmt);
7934 if (TREE_CODE_CLASS (code) != tcc_comparison)
7935 return false;
7937 rhs1 = gimple_assign_rhs1 (stmt);
7938 rhs2 = gimple_assign_rhs2 (stmt);
7940 if (!vect_is_simple_use (rhs1, stmt_info->vinfo, &def_stmt,
7941 &dts[0], &vectype1))
7942 return false;
7944 if (!vect_is_simple_use (rhs2, stmt_info->vinfo, &def_stmt,
7945 &dts[1], &vectype2))
7946 return false;
7948 if (vectype1 && vectype2
7949 && TYPE_VECTOR_SUBPARTS (vectype1) != TYPE_VECTOR_SUBPARTS (vectype2))
7950 return false;
7952 vectype = vectype1 ? vectype1 : vectype2;
7954 /* Invariant comparison. */
7955 if (!vectype)
7957 vectype = get_vectype_for_scalar_type (TREE_TYPE (rhs1));
7958 if (TYPE_VECTOR_SUBPARTS (vectype) != nunits)
7959 return false;
7961 else if (nunits != TYPE_VECTOR_SUBPARTS (vectype))
7962 return false;
7964 /* Can't compare mask and non-mask types. */
7965 if (vectype1 && vectype2
7966 && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
7967 return false;
7969 /* Boolean values may have another representation in vectors
7970 and therefore we prefer bit operations over comparison for
7971 them (which also works for scalar masks). We store opcodes
7972 to use in bitop1 and bitop2. Statement is vectorized as
7973 BITOP2 (rhs1 BITOP1 rhs2) or
7974 rhs1 BITOP2 (BITOP1 rhs2)
7975 depending on bitop1 and bitop2 arity. */
7976 if (VECTOR_BOOLEAN_TYPE_P (vectype))
7978 if (code == GT_EXPR)
7980 bitop1 = BIT_NOT_EXPR;
7981 bitop2 = BIT_AND_EXPR;
7983 else if (code == GE_EXPR)
7985 bitop1 = BIT_NOT_EXPR;
7986 bitop2 = BIT_IOR_EXPR;
7988 else if (code == LT_EXPR)
7990 bitop1 = BIT_NOT_EXPR;
7991 bitop2 = BIT_AND_EXPR;
7992 std::swap (rhs1, rhs2);
7993 std::swap (dts[0], dts[1]);
7995 else if (code == LE_EXPR)
7997 bitop1 = BIT_NOT_EXPR;
7998 bitop2 = BIT_IOR_EXPR;
7999 std::swap (rhs1, rhs2);
8000 std::swap (dts[0], dts[1]);
8002 else
8004 bitop1 = BIT_XOR_EXPR;
8005 if (code == EQ_EXPR)
8006 bitop2 = BIT_NOT_EXPR;
8010 if (!vec_stmt)
8012 STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
8013 vect_model_simple_cost (stmt_info, ncopies * (1 + (bitop2 != NOP_EXPR)),
8014 dts, NULL, NULL);
8015 if (bitop1 == NOP_EXPR)
8016 return expand_vec_cmp_expr_p (vectype, mask_type);
8017 else
8019 machine_mode mode = TYPE_MODE (vectype);
8020 optab optab;
8022 optab = optab_for_tree_code (bitop1, vectype, optab_default);
8023 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
8024 return false;
8026 if (bitop2 != NOP_EXPR)
8028 optab = optab_for_tree_code (bitop2, vectype, optab_default);
8029 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
8030 return false;
8032 return true;
8036 /* Transform. */
8037 if (!slp_node)
8039 vec_oprnds0.create (1);
8040 vec_oprnds1.create (1);
8043 /* Handle def. */
8044 lhs = gimple_assign_lhs (stmt);
8045 mask = vect_create_destination_var (lhs, mask_type);
8047 /* Handle cmp expr. */
8048 for (j = 0; j < ncopies; j++)
8050 gassign *new_stmt = NULL;
8051 if (j == 0)
8053 if (slp_node)
8055 auto_vec<tree, 2> ops;
8056 auto_vec<vec<tree>, 2> vec_defs;
8058 ops.safe_push (rhs1);
8059 ops.safe_push (rhs2);
8060 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
8061 vec_oprnds1 = vec_defs.pop ();
8062 vec_oprnds0 = vec_defs.pop ();
8064 else
8066 vec_rhs1 = vect_get_vec_def_for_operand (rhs1, stmt, vectype);
8067 vec_rhs2 = vect_get_vec_def_for_operand (rhs2, stmt, vectype);
8070 else
8072 vec_rhs1 = vect_get_vec_def_for_stmt_copy (dts[0],
8073 vec_oprnds0.pop ());
8074 vec_rhs2 = vect_get_vec_def_for_stmt_copy (dts[1],
8075 vec_oprnds1.pop ());
8078 if (!slp_node)
8080 vec_oprnds0.quick_push (vec_rhs1);
8081 vec_oprnds1.quick_push (vec_rhs2);
8084 /* Arguments are ready. Create the new vector stmt. */
8085 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
8087 vec_rhs2 = vec_oprnds1[i];
8089 new_temp = make_ssa_name (mask);
8090 if (bitop1 == NOP_EXPR)
8092 new_stmt = gimple_build_assign (new_temp, code,
8093 vec_rhs1, vec_rhs2);
8094 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8096 else
8098 if (bitop1 == BIT_NOT_EXPR)
8099 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
8100 else
8101 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
8102 vec_rhs2);
8103 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8104 if (bitop2 != NOP_EXPR)
8106 tree res = make_ssa_name (mask);
8107 if (bitop2 == BIT_NOT_EXPR)
8108 new_stmt = gimple_build_assign (res, bitop2, new_temp);
8109 else
8110 new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
8111 new_temp);
8112 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8115 if (slp_node)
8116 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
8119 if (slp_node)
8120 continue;
8122 if (j == 0)
8123 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
8124 else
8125 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
8127 prev_stmt_info = vinfo_for_stmt (new_stmt);
8130 vec_oprnds0.release ();
8131 vec_oprnds1.release ();
8133 return true;
8136 /* Make sure the statement is vectorizable. */
8138 bool
8139 vect_analyze_stmt (gimple *stmt, bool *need_to_vectorize, slp_tree node)
8141 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8142 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
8143 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
8144 bool ok;
8145 tree scalar_type, vectype;
8146 gimple *pattern_stmt;
8147 gimple_seq pattern_def_seq;
8149 if (dump_enabled_p ())
8151 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
8152 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
8155 if (gimple_has_volatile_ops (stmt))
8157 if (dump_enabled_p ())
8158 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8159 "not vectorized: stmt has volatile operands\n");
8161 return false;
8164 /* Skip stmts that do not need to be vectorized. In loops this is expected
8165 to include:
8166 - the COND_EXPR which is the loop exit condition
8167 - any LABEL_EXPRs in the loop
8168 - computations that are used only for array indexing or loop control.
8169 In basic blocks we only analyze statements that are a part of some SLP
8170 instance, therefore, all the statements are relevant.
8172 Pattern statement needs to be analyzed instead of the original statement
8173 if the original statement is not relevant. Otherwise, we analyze both
8174 statements. In basic blocks we are called from some SLP instance
8175 traversal, don't analyze pattern stmts instead, the pattern stmts
8176 already will be part of SLP instance. */
8178 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
8179 if (!STMT_VINFO_RELEVANT_P (stmt_info)
8180 && !STMT_VINFO_LIVE_P (stmt_info))
8182 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
8183 && pattern_stmt
8184 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
8185 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
8187 /* Analyze PATTERN_STMT instead of the original stmt. */
8188 stmt = pattern_stmt;
8189 stmt_info = vinfo_for_stmt (pattern_stmt);
8190 if (dump_enabled_p ())
8192 dump_printf_loc (MSG_NOTE, vect_location,
8193 "==> examining pattern statement: ");
8194 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
8197 else
8199 if (dump_enabled_p ())
8200 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
8202 return true;
8205 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
8206 && node == NULL
8207 && pattern_stmt
8208 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
8209 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
8211 /* Analyze PATTERN_STMT too. */
8212 if (dump_enabled_p ())
8214 dump_printf_loc (MSG_NOTE, vect_location,
8215 "==> examining pattern statement: ");
8216 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
8219 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
8220 return false;
8223 if (is_pattern_stmt_p (stmt_info)
8224 && node == NULL
8225 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
8227 gimple_stmt_iterator si;
8229 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
8231 gimple *pattern_def_stmt = gsi_stmt (si);
8232 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
8233 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
8235 /* Analyze def stmt of STMT if it's a pattern stmt. */
8236 if (dump_enabled_p ())
8238 dump_printf_loc (MSG_NOTE, vect_location,
8239 "==> examining pattern def statement: ");
8240 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
8243 if (!vect_analyze_stmt (pattern_def_stmt,
8244 need_to_vectorize, node))
8245 return false;
8250 switch (STMT_VINFO_DEF_TYPE (stmt_info))
8252 case vect_internal_def:
8253 break;
8255 case vect_reduction_def:
8256 case vect_nested_cycle:
8257 gcc_assert (!bb_vinfo
8258 && (relevance == vect_used_in_outer
8259 || relevance == vect_used_in_outer_by_reduction
8260 || relevance == vect_used_by_reduction
8261 || relevance == vect_unused_in_scope
8262 || relevance == vect_used_only_live));
8263 break;
8265 case vect_induction_def:
8266 case vect_constant_def:
8267 case vect_external_def:
8268 case vect_unknown_def_type:
8269 default:
8270 gcc_unreachable ();
8273 if (bb_vinfo)
8275 gcc_assert (PURE_SLP_STMT (stmt_info));
8277 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
8278 if (dump_enabled_p ())
8280 dump_printf_loc (MSG_NOTE, vect_location,
8281 "get vectype for scalar type: ");
8282 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
8283 dump_printf (MSG_NOTE, "\n");
8286 vectype = get_vectype_for_scalar_type (scalar_type);
8287 if (!vectype)
8289 if (dump_enabled_p ())
8291 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8292 "not SLPed: unsupported data-type ");
8293 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
8294 scalar_type);
8295 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
8297 return false;
8300 if (dump_enabled_p ())
8302 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
8303 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
8304 dump_printf (MSG_NOTE, "\n");
8307 STMT_VINFO_VECTYPE (stmt_info) = vectype;
8310 if (STMT_VINFO_RELEVANT_P (stmt_info))
8312 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
8313 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
8314 || (is_gimple_call (stmt)
8315 && gimple_call_lhs (stmt) == NULL_TREE));
8316 *need_to_vectorize = true;
8319 if (PURE_SLP_STMT (stmt_info) && !node)
8321 dump_printf_loc (MSG_NOTE, vect_location,
8322 "handled only by SLP analysis\n");
8323 return true;
8326 ok = true;
8327 if (!bb_vinfo
8328 && (STMT_VINFO_RELEVANT_P (stmt_info)
8329 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
8330 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
8331 || vectorizable_conversion (stmt, NULL, NULL, node)
8332 || vectorizable_shift (stmt, NULL, NULL, node)
8333 || vectorizable_operation (stmt, NULL, NULL, node)
8334 || vectorizable_assignment (stmt, NULL, NULL, node)
8335 || vectorizable_load (stmt, NULL, NULL, node, NULL)
8336 || vectorizable_call (stmt, NULL, NULL, node)
8337 || vectorizable_store (stmt, NULL, NULL, node)
8338 || vectorizable_reduction (stmt, NULL, NULL, node)
8339 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
8340 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
8341 else
8343 if (bb_vinfo)
8344 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
8345 || vectorizable_conversion (stmt, NULL, NULL, node)
8346 || vectorizable_shift (stmt, NULL, NULL, node)
8347 || vectorizable_operation (stmt, NULL, NULL, node)
8348 || vectorizable_assignment (stmt, NULL, NULL, node)
8349 || vectorizable_load (stmt, NULL, NULL, node, NULL)
8350 || vectorizable_call (stmt, NULL, NULL, node)
8351 || vectorizable_store (stmt, NULL, NULL, node)
8352 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
8353 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
8356 if (!ok)
8358 if (dump_enabled_p ())
8360 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8361 "not vectorized: relevant stmt not ");
8362 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
8363 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8366 return false;
8369 if (bb_vinfo)
8370 return true;
8372 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
8373 need extra handling, except for vectorizable reductions. */
8374 if (STMT_VINFO_LIVE_P (stmt_info)
8375 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
8376 ok = vectorizable_live_operation (stmt, NULL, NULL, -1, NULL);
8378 if (!ok)
8380 if (dump_enabled_p ())
8382 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8383 "not vectorized: live stmt not ");
8384 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
8385 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8388 return false;
8391 return true;
8395 /* Function vect_transform_stmt.
8397 Create a vectorized stmt to replace STMT, and insert it at BSI. */
8399 bool
8400 vect_transform_stmt (gimple *stmt, gimple_stmt_iterator *gsi,
8401 bool *grouped_store, slp_tree slp_node,
8402 slp_instance slp_node_instance)
8404 bool is_store = false;
8405 gimple *vec_stmt = NULL;
8406 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8407 bool done;
8409 gcc_assert (slp_node || !PURE_SLP_STMT (stmt_info));
8410 gimple *old_vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
8412 switch (STMT_VINFO_TYPE (stmt_info))
8414 case type_demotion_vec_info_type:
8415 case type_promotion_vec_info_type:
8416 case type_conversion_vec_info_type:
8417 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
8418 gcc_assert (done);
8419 break;
8421 case induc_vec_info_type:
8422 gcc_assert (!slp_node);
8423 done = vectorizable_induction (stmt, gsi, &vec_stmt);
8424 gcc_assert (done);
8425 break;
8427 case shift_vec_info_type:
8428 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
8429 gcc_assert (done);
8430 break;
8432 case op_vec_info_type:
8433 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
8434 gcc_assert (done);
8435 break;
8437 case assignment_vec_info_type:
8438 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
8439 gcc_assert (done);
8440 break;
8442 case load_vec_info_type:
8443 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
8444 slp_node_instance);
8445 gcc_assert (done);
8446 break;
8448 case store_vec_info_type:
8449 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
8450 gcc_assert (done);
8451 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
8453 /* In case of interleaving, the whole chain is vectorized when the
8454 last store in the chain is reached. Store stmts before the last
8455 one are skipped, and there vec_stmt_info shouldn't be freed
8456 meanwhile. */
8457 *grouped_store = true;
8458 if (STMT_VINFO_VEC_STMT (stmt_info))
8459 is_store = true;
8461 else
8462 is_store = true;
8463 break;
8465 case condition_vec_info_type:
8466 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
8467 gcc_assert (done);
8468 break;
8470 case comparison_vec_info_type:
8471 done = vectorizable_comparison (stmt, gsi, &vec_stmt, NULL, slp_node);
8472 gcc_assert (done);
8473 break;
8475 case call_vec_info_type:
8476 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
8477 stmt = gsi_stmt (*gsi);
8478 if (gimple_call_internal_p (stmt, IFN_MASK_STORE))
8479 is_store = true;
8480 break;
8482 case call_simd_clone_vec_info_type:
8483 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
8484 stmt = gsi_stmt (*gsi);
8485 break;
8487 case reduc_vec_info_type:
8488 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
8489 gcc_assert (done);
8490 break;
8492 default:
8493 if (!STMT_VINFO_LIVE_P (stmt_info))
8495 if (dump_enabled_p ())
8496 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8497 "stmt not supported.\n");
8498 gcc_unreachable ();
8502 /* Verify SLP vectorization doesn't mess with STMT_VINFO_VEC_STMT.
8503 This would break hybrid SLP vectorization. */
8504 if (slp_node)
8505 gcc_assert (!vec_stmt
8506 && STMT_VINFO_VEC_STMT (stmt_info) == old_vec_stmt);
8508 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
8509 is being vectorized, but outside the immediately enclosing loop. */
8510 if (vec_stmt
8511 && STMT_VINFO_LOOP_VINFO (stmt_info)
8512 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
8513 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
8514 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
8515 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
8516 || STMT_VINFO_RELEVANT (stmt_info) ==
8517 vect_used_in_outer_by_reduction))
8519 struct loop *innerloop = LOOP_VINFO_LOOP (
8520 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
8521 imm_use_iterator imm_iter;
8522 use_operand_p use_p;
8523 tree scalar_dest;
8524 gimple *exit_phi;
8526 if (dump_enabled_p ())
8527 dump_printf_loc (MSG_NOTE, vect_location,
8528 "Record the vdef for outer-loop vectorization.\n");
8530 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
8531 (to be used when vectorizing outer-loop stmts that use the DEF of
8532 STMT). */
8533 if (gimple_code (stmt) == GIMPLE_PHI)
8534 scalar_dest = PHI_RESULT (stmt);
8535 else
8536 scalar_dest = gimple_assign_lhs (stmt);
8538 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
8540 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
8542 exit_phi = USE_STMT (use_p);
8543 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
8548 /* Handle stmts whose DEF is used outside the loop-nest that is
8549 being vectorized. */
8550 if (slp_node)
8552 gimple *slp_stmt;
8553 int i;
8554 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt)
8556 stmt_vec_info slp_stmt_info = vinfo_for_stmt (slp_stmt);
8557 if (STMT_VINFO_LIVE_P (slp_stmt_info)
8558 && STMT_VINFO_TYPE (slp_stmt_info) != reduc_vec_info_type)
8560 done = vectorizable_live_operation (slp_stmt, gsi, slp_node, i,
8561 &vec_stmt);
8562 gcc_assert (done);
8566 else if (STMT_VINFO_LIVE_P (stmt_info)
8567 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
8569 done = vectorizable_live_operation (stmt, gsi, slp_node, -1, &vec_stmt);
8570 gcc_assert (done);
8573 if (vec_stmt)
8574 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
8576 return is_store;
8580 /* Remove a group of stores (for SLP or interleaving), free their
8581 stmt_vec_info. */
8583 void
8584 vect_remove_stores (gimple *first_stmt)
8586 gimple *next = first_stmt;
8587 gimple *tmp;
8588 gimple_stmt_iterator next_si;
8590 while (next)
8592 stmt_vec_info stmt_info = vinfo_for_stmt (next);
8594 tmp = GROUP_NEXT_ELEMENT (stmt_info);
8595 if (is_pattern_stmt_p (stmt_info))
8596 next = STMT_VINFO_RELATED_STMT (stmt_info);
8597 /* Free the attached stmt_vec_info and remove the stmt. */
8598 next_si = gsi_for_stmt (next);
8599 unlink_stmt_vdef (next);
8600 gsi_remove (&next_si, true);
8601 release_defs (next);
8602 free_stmt_vec_info (next);
8603 next = tmp;
8608 /* Function new_stmt_vec_info.
8610 Create and initialize a new stmt_vec_info struct for STMT. */
8612 stmt_vec_info
8613 new_stmt_vec_info (gimple *stmt, vec_info *vinfo)
8615 stmt_vec_info res;
8616 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
8618 STMT_VINFO_TYPE (res) = undef_vec_info_type;
8619 STMT_VINFO_STMT (res) = stmt;
8620 res->vinfo = vinfo;
8621 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
8622 STMT_VINFO_LIVE_P (res) = false;
8623 STMT_VINFO_VECTYPE (res) = NULL;
8624 STMT_VINFO_VEC_STMT (res) = NULL;
8625 STMT_VINFO_VECTORIZABLE (res) = true;
8626 STMT_VINFO_IN_PATTERN_P (res) = false;
8627 STMT_VINFO_RELATED_STMT (res) = NULL;
8628 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
8629 STMT_VINFO_DATA_REF (res) = NULL;
8630 STMT_VINFO_VEC_REDUCTION_TYPE (res) = TREE_CODE_REDUCTION;
8631 STMT_VINFO_VEC_CONST_COND_REDUC_CODE (res) = ERROR_MARK;
8633 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
8634 STMT_VINFO_DR_OFFSET (res) = NULL;
8635 STMT_VINFO_DR_INIT (res) = NULL;
8636 STMT_VINFO_DR_STEP (res) = NULL;
8637 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
8639 if (gimple_code (stmt) == GIMPLE_PHI
8640 && is_loop_header_bb_p (gimple_bb (stmt)))
8641 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
8642 else
8643 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
8645 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
8646 STMT_SLP_TYPE (res) = loop_vect;
8647 STMT_VINFO_NUM_SLP_USES (res) = 0;
8649 GROUP_FIRST_ELEMENT (res) = NULL;
8650 GROUP_NEXT_ELEMENT (res) = NULL;
8651 GROUP_SIZE (res) = 0;
8652 GROUP_STORE_COUNT (res) = 0;
8653 GROUP_GAP (res) = 0;
8654 GROUP_SAME_DR_STMT (res) = NULL;
8656 return res;
8660 /* Create a hash table for stmt_vec_info. */
8662 void
8663 init_stmt_vec_info_vec (void)
8665 gcc_assert (!stmt_vec_info_vec.exists ());
8666 stmt_vec_info_vec.create (50);
8670 /* Free hash table for stmt_vec_info. */
8672 void
8673 free_stmt_vec_info_vec (void)
8675 unsigned int i;
8676 stmt_vec_info info;
8677 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
8678 if (info != NULL)
8679 free_stmt_vec_info (STMT_VINFO_STMT (info));
8680 gcc_assert (stmt_vec_info_vec.exists ());
8681 stmt_vec_info_vec.release ();
8685 /* Free stmt vectorization related info. */
8687 void
8688 free_stmt_vec_info (gimple *stmt)
8690 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8692 if (!stmt_info)
8693 return;
8695 /* Check if this statement has a related "pattern stmt"
8696 (introduced by the vectorizer during the pattern recognition
8697 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
8698 too. */
8699 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
8701 stmt_vec_info patt_info
8702 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
8703 if (patt_info)
8705 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
8706 gimple *patt_stmt = STMT_VINFO_STMT (patt_info);
8707 gimple_set_bb (patt_stmt, NULL);
8708 tree lhs = gimple_get_lhs (patt_stmt);
8709 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8710 release_ssa_name (lhs);
8711 if (seq)
8713 gimple_stmt_iterator si;
8714 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
8716 gimple *seq_stmt = gsi_stmt (si);
8717 gimple_set_bb (seq_stmt, NULL);
8718 lhs = gimple_get_lhs (seq_stmt);
8719 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8720 release_ssa_name (lhs);
8721 free_stmt_vec_info (seq_stmt);
8724 free_stmt_vec_info (patt_stmt);
8728 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
8729 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
8730 set_vinfo_for_stmt (stmt, NULL);
8731 free (stmt_info);
8735 /* Function get_vectype_for_scalar_type_and_size.
8737 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
8738 by the target. */
8740 static tree
8741 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
8743 machine_mode inner_mode = TYPE_MODE (scalar_type);
8744 machine_mode simd_mode;
8745 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
8746 int nunits;
8747 tree vectype;
8749 if (nbytes == 0)
8750 return NULL_TREE;
8752 if (GET_MODE_CLASS (inner_mode) != MODE_INT
8753 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
8754 return NULL_TREE;
8756 /* For vector types of elements whose mode precision doesn't
8757 match their types precision we use a element type of mode
8758 precision. The vectorization routines will have to make sure
8759 they support the proper result truncation/extension.
8760 We also make sure to build vector types with INTEGER_TYPE
8761 component type only. */
8762 if (INTEGRAL_TYPE_P (scalar_type)
8763 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
8764 || TREE_CODE (scalar_type) != INTEGER_TYPE))
8765 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
8766 TYPE_UNSIGNED (scalar_type));
8768 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
8769 When the component mode passes the above test simply use a type
8770 corresponding to that mode. The theory is that any use that
8771 would cause problems with this will disable vectorization anyway. */
8772 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
8773 && !INTEGRAL_TYPE_P (scalar_type))
8774 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
8776 /* We can't build a vector type of elements with alignment bigger than
8777 their size. */
8778 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
8779 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
8780 TYPE_UNSIGNED (scalar_type));
8782 /* If we felt back to using the mode fail if there was
8783 no scalar type for it. */
8784 if (scalar_type == NULL_TREE)
8785 return NULL_TREE;
8787 /* If no size was supplied use the mode the target prefers. Otherwise
8788 lookup a vector mode of the specified size. */
8789 if (size == 0)
8790 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
8791 else
8792 simd_mode = mode_for_vector (inner_mode, size / nbytes);
8793 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
8794 if (nunits <= 1)
8795 return NULL_TREE;
8797 vectype = build_vector_type (scalar_type, nunits);
8799 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8800 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
8801 return NULL_TREE;
8803 return vectype;
8806 unsigned int current_vector_size;
8808 /* Function get_vectype_for_scalar_type.
8810 Returns the vector type corresponding to SCALAR_TYPE as supported
8811 by the target. */
8813 tree
8814 get_vectype_for_scalar_type (tree scalar_type)
8816 tree vectype;
8817 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
8818 current_vector_size);
8819 if (vectype
8820 && current_vector_size == 0)
8821 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
8822 return vectype;
8825 /* Function get_mask_type_for_scalar_type.
8827 Returns the mask type corresponding to a result of comparison
8828 of vectors of specified SCALAR_TYPE as supported by target. */
8830 tree
8831 get_mask_type_for_scalar_type (tree scalar_type)
8833 tree vectype = get_vectype_for_scalar_type (scalar_type);
8835 if (!vectype)
8836 return NULL;
8838 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype),
8839 current_vector_size);
8842 /* Function get_same_sized_vectype
8844 Returns a vector type corresponding to SCALAR_TYPE of size
8845 VECTOR_TYPE if supported by the target. */
8847 tree
8848 get_same_sized_vectype (tree scalar_type, tree vector_type)
8850 if (TREE_CODE (scalar_type) == BOOLEAN_TYPE)
8851 return build_same_sized_truth_vector_type (vector_type);
8853 return get_vectype_for_scalar_type_and_size
8854 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
8857 /* Function vect_is_simple_use.
8859 Input:
8860 VINFO - the vect info of the loop or basic block that is being vectorized.
8861 OPERAND - operand in the loop or bb.
8862 Output:
8863 DEF_STMT - the defining stmt in case OPERAND is an SSA_NAME.
8864 DT - the type of definition
8866 Returns whether a stmt with OPERAND can be vectorized.
8867 For loops, supportable operands are constants, loop invariants, and operands
8868 that are defined by the current iteration of the loop. Unsupportable
8869 operands are those that are defined by a previous iteration of the loop (as
8870 is the case in reduction/induction computations).
8871 For basic blocks, supportable operands are constants and bb invariants.
8872 For now, operands defined outside the basic block are not supported. */
8874 bool
8875 vect_is_simple_use (tree operand, vec_info *vinfo,
8876 gimple **def_stmt, enum vect_def_type *dt)
8878 *def_stmt = NULL;
8879 *dt = vect_unknown_def_type;
8881 if (dump_enabled_p ())
8883 dump_printf_loc (MSG_NOTE, vect_location,
8884 "vect_is_simple_use: operand ");
8885 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
8886 dump_printf (MSG_NOTE, "\n");
8889 if (CONSTANT_CLASS_P (operand))
8891 *dt = vect_constant_def;
8892 return true;
8895 if (is_gimple_min_invariant (operand))
8897 *dt = vect_external_def;
8898 return true;
8901 if (TREE_CODE (operand) != SSA_NAME)
8903 if (dump_enabled_p ())
8904 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8905 "not ssa-name.\n");
8906 return false;
8909 if (SSA_NAME_IS_DEFAULT_DEF (operand))
8911 *dt = vect_external_def;
8912 return true;
8915 *def_stmt = SSA_NAME_DEF_STMT (operand);
8916 if (dump_enabled_p ())
8918 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
8919 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
8922 if (! vect_stmt_in_region_p (vinfo, *def_stmt))
8923 *dt = vect_external_def;
8924 else
8926 stmt_vec_info stmt_vinfo = vinfo_for_stmt (*def_stmt);
8927 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
8930 if (dump_enabled_p ())
8932 dump_printf_loc (MSG_NOTE, vect_location, "type of def: ");
8933 switch (*dt)
8935 case vect_uninitialized_def:
8936 dump_printf (MSG_NOTE, "uninitialized\n");
8937 break;
8938 case vect_constant_def:
8939 dump_printf (MSG_NOTE, "constant\n");
8940 break;
8941 case vect_external_def:
8942 dump_printf (MSG_NOTE, "external\n");
8943 break;
8944 case vect_internal_def:
8945 dump_printf (MSG_NOTE, "internal\n");
8946 break;
8947 case vect_induction_def:
8948 dump_printf (MSG_NOTE, "induction\n");
8949 break;
8950 case vect_reduction_def:
8951 dump_printf (MSG_NOTE, "reduction\n");
8952 break;
8953 case vect_double_reduction_def:
8954 dump_printf (MSG_NOTE, "double reduction\n");
8955 break;
8956 case vect_nested_cycle:
8957 dump_printf (MSG_NOTE, "nested cycle\n");
8958 break;
8959 case vect_unknown_def_type:
8960 dump_printf (MSG_NOTE, "unknown\n");
8961 break;
8965 if (*dt == vect_unknown_def_type)
8967 if (dump_enabled_p ())
8968 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8969 "Unsupported pattern.\n");
8970 return false;
8973 switch (gimple_code (*def_stmt))
8975 case GIMPLE_PHI:
8976 case GIMPLE_ASSIGN:
8977 case GIMPLE_CALL:
8978 break;
8979 default:
8980 if (dump_enabled_p ())
8981 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8982 "unsupported defining stmt:\n");
8983 return false;
8986 return true;
8989 /* Function vect_is_simple_use.
8991 Same as vect_is_simple_use but also determines the vector operand
8992 type of OPERAND and stores it to *VECTYPE. If the definition of
8993 OPERAND is vect_uninitialized_def, vect_constant_def or
8994 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
8995 is responsible to compute the best suited vector type for the
8996 scalar operand. */
8998 bool
8999 vect_is_simple_use (tree operand, vec_info *vinfo,
9000 gimple **def_stmt, enum vect_def_type *dt, tree *vectype)
9002 if (!vect_is_simple_use (operand, vinfo, def_stmt, dt))
9003 return false;
9005 /* Now get a vector type if the def is internal, otherwise supply
9006 NULL_TREE and leave it up to the caller to figure out a proper
9007 type for the use stmt. */
9008 if (*dt == vect_internal_def
9009 || *dt == vect_induction_def
9010 || *dt == vect_reduction_def
9011 || *dt == vect_double_reduction_def
9012 || *dt == vect_nested_cycle)
9014 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
9016 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
9017 && !STMT_VINFO_RELEVANT (stmt_info)
9018 && !STMT_VINFO_LIVE_P (stmt_info))
9019 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
9021 *vectype = STMT_VINFO_VECTYPE (stmt_info);
9022 gcc_assert (*vectype != NULL_TREE);
9024 else if (*dt == vect_uninitialized_def
9025 || *dt == vect_constant_def
9026 || *dt == vect_external_def)
9027 *vectype = NULL_TREE;
9028 else
9029 gcc_unreachable ();
9031 return true;
9035 /* Function supportable_widening_operation
9037 Check whether an operation represented by the code CODE is a
9038 widening operation that is supported by the target platform in
9039 vector form (i.e., when operating on arguments of type VECTYPE_IN
9040 producing a result of type VECTYPE_OUT).
9042 Widening operations we currently support are NOP (CONVERT), FLOAT
9043 and WIDEN_MULT. This function checks if these operations are supported
9044 by the target platform either directly (via vector tree-codes), or via
9045 target builtins.
9047 Output:
9048 - CODE1 and CODE2 are codes of vector operations to be used when
9049 vectorizing the operation, if available.
9050 - MULTI_STEP_CVT determines the number of required intermediate steps in
9051 case of multi-step conversion (like char->short->int - in that case
9052 MULTI_STEP_CVT will be 1).
9053 - INTERM_TYPES contains the intermediate type required to perform the
9054 widening operation (short in the above example). */
9056 bool
9057 supportable_widening_operation (enum tree_code code, gimple *stmt,
9058 tree vectype_out, tree vectype_in,
9059 enum tree_code *code1, enum tree_code *code2,
9060 int *multi_step_cvt,
9061 vec<tree> *interm_types)
9063 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
9064 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
9065 struct loop *vect_loop = NULL;
9066 machine_mode vec_mode;
9067 enum insn_code icode1, icode2;
9068 optab optab1, optab2;
9069 tree vectype = vectype_in;
9070 tree wide_vectype = vectype_out;
9071 enum tree_code c1, c2;
9072 int i;
9073 tree prev_type, intermediate_type;
9074 machine_mode intermediate_mode, prev_mode;
9075 optab optab3, optab4;
9077 *multi_step_cvt = 0;
9078 if (loop_info)
9079 vect_loop = LOOP_VINFO_LOOP (loop_info);
9081 switch (code)
9083 case WIDEN_MULT_EXPR:
9084 /* The result of a vectorized widening operation usually requires
9085 two vectors (because the widened results do not fit into one vector).
9086 The generated vector results would normally be expected to be
9087 generated in the same order as in the original scalar computation,
9088 i.e. if 8 results are generated in each vector iteration, they are
9089 to be organized as follows:
9090 vect1: [res1,res2,res3,res4],
9091 vect2: [res5,res6,res7,res8].
9093 However, in the special case that the result of the widening
9094 operation is used in a reduction computation only, the order doesn't
9095 matter (because when vectorizing a reduction we change the order of
9096 the computation). Some targets can take advantage of this and
9097 generate more efficient code. For example, targets like Altivec,
9098 that support widen_mult using a sequence of {mult_even,mult_odd}
9099 generate the following vectors:
9100 vect1: [res1,res3,res5,res7],
9101 vect2: [res2,res4,res6,res8].
9103 When vectorizing outer-loops, we execute the inner-loop sequentially
9104 (each vectorized inner-loop iteration contributes to VF outer-loop
9105 iterations in parallel). We therefore don't allow to change the
9106 order of the computation in the inner-loop during outer-loop
9107 vectorization. */
9108 /* TODO: Another case in which order doesn't *really* matter is when we
9109 widen and then contract again, e.g. (short)((int)x * y >> 8).
9110 Normally, pack_trunc performs an even/odd permute, whereas the
9111 repack from an even/odd expansion would be an interleave, which
9112 would be significantly simpler for e.g. AVX2. */
9113 /* In any case, in order to avoid duplicating the code below, recurse
9114 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
9115 are properly set up for the caller. If we fail, we'll continue with
9116 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
9117 if (vect_loop
9118 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
9119 && !nested_in_vect_loop_p (vect_loop, stmt)
9120 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
9121 stmt, vectype_out, vectype_in,
9122 code1, code2, multi_step_cvt,
9123 interm_types))
9125 /* Elements in a vector with vect_used_by_reduction property cannot
9126 be reordered if the use chain with this property does not have the
9127 same operation. One such an example is s += a * b, where elements
9128 in a and b cannot be reordered. Here we check if the vector defined
9129 by STMT is only directly used in the reduction statement. */
9130 tree lhs = gimple_assign_lhs (stmt);
9131 use_operand_p dummy;
9132 gimple *use_stmt;
9133 stmt_vec_info use_stmt_info = NULL;
9134 if (single_imm_use (lhs, &dummy, &use_stmt)
9135 && (use_stmt_info = vinfo_for_stmt (use_stmt))
9136 && STMT_VINFO_DEF_TYPE (use_stmt_info) == vect_reduction_def)
9137 return true;
9139 c1 = VEC_WIDEN_MULT_LO_EXPR;
9140 c2 = VEC_WIDEN_MULT_HI_EXPR;
9141 break;
9143 case DOT_PROD_EXPR:
9144 c1 = DOT_PROD_EXPR;
9145 c2 = DOT_PROD_EXPR;
9146 break;
9148 case SAD_EXPR:
9149 c1 = SAD_EXPR;
9150 c2 = SAD_EXPR;
9151 break;
9153 case VEC_WIDEN_MULT_EVEN_EXPR:
9154 /* Support the recursion induced just above. */
9155 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
9156 c2 = VEC_WIDEN_MULT_ODD_EXPR;
9157 break;
9159 case WIDEN_LSHIFT_EXPR:
9160 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
9161 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
9162 break;
9164 CASE_CONVERT:
9165 c1 = VEC_UNPACK_LO_EXPR;
9166 c2 = VEC_UNPACK_HI_EXPR;
9167 break;
9169 case FLOAT_EXPR:
9170 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
9171 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
9172 break;
9174 case FIX_TRUNC_EXPR:
9175 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
9176 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
9177 computing the operation. */
9178 return false;
9180 default:
9181 gcc_unreachable ();
9184 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
9185 std::swap (c1, c2);
9187 if (code == FIX_TRUNC_EXPR)
9189 /* The signedness is determined from output operand. */
9190 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
9191 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
9193 else
9195 optab1 = optab_for_tree_code (c1, vectype, optab_default);
9196 optab2 = optab_for_tree_code (c2, vectype, optab_default);
9199 if (!optab1 || !optab2)
9200 return false;
9202 vec_mode = TYPE_MODE (vectype);
9203 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
9204 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
9205 return false;
9207 *code1 = c1;
9208 *code2 = c2;
9210 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
9211 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
9212 /* For scalar masks we may have different boolean
9213 vector types having the same QImode. Thus we
9214 add additional check for elements number. */
9215 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9216 || (TYPE_VECTOR_SUBPARTS (vectype) / 2
9217 == TYPE_VECTOR_SUBPARTS (wide_vectype)));
9219 /* Check if it's a multi-step conversion that can be done using intermediate
9220 types. */
9222 prev_type = vectype;
9223 prev_mode = vec_mode;
9225 if (!CONVERT_EXPR_CODE_P (code))
9226 return false;
9228 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
9229 intermediate steps in promotion sequence. We try
9230 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
9231 not. */
9232 interm_types->create (MAX_INTERM_CVT_STEPS);
9233 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
9235 intermediate_mode = insn_data[icode1].operand[0].mode;
9236 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
9238 intermediate_type
9239 = build_truth_vector_type (TYPE_VECTOR_SUBPARTS (prev_type) / 2,
9240 current_vector_size);
9241 if (intermediate_mode != TYPE_MODE (intermediate_type))
9242 return false;
9244 else
9245 intermediate_type
9246 = lang_hooks.types.type_for_mode (intermediate_mode,
9247 TYPE_UNSIGNED (prev_type));
9249 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
9250 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
9252 if (!optab3 || !optab4
9253 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
9254 || insn_data[icode1].operand[0].mode != intermediate_mode
9255 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
9256 || insn_data[icode2].operand[0].mode != intermediate_mode
9257 || ((icode1 = optab_handler (optab3, intermediate_mode))
9258 == CODE_FOR_nothing)
9259 || ((icode2 = optab_handler (optab4, intermediate_mode))
9260 == CODE_FOR_nothing))
9261 break;
9263 interm_types->quick_push (intermediate_type);
9264 (*multi_step_cvt)++;
9266 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
9267 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
9268 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9269 || (TYPE_VECTOR_SUBPARTS (intermediate_type) / 2
9270 == TYPE_VECTOR_SUBPARTS (wide_vectype)));
9272 prev_type = intermediate_type;
9273 prev_mode = intermediate_mode;
9276 interm_types->release ();
9277 return false;
9281 /* Function supportable_narrowing_operation
9283 Check whether an operation represented by the code CODE is a
9284 narrowing operation that is supported by the target platform in
9285 vector form (i.e., when operating on arguments of type VECTYPE_IN
9286 and producing a result of type VECTYPE_OUT).
9288 Narrowing operations we currently support are NOP (CONVERT) and
9289 FIX_TRUNC. This function checks if these operations are supported by
9290 the target platform directly via vector tree-codes.
9292 Output:
9293 - CODE1 is the code of a vector operation to be used when
9294 vectorizing the operation, if available.
9295 - MULTI_STEP_CVT determines the number of required intermediate steps in
9296 case of multi-step conversion (like int->short->char - in that case
9297 MULTI_STEP_CVT will be 1).
9298 - INTERM_TYPES contains the intermediate type required to perform the
9299 narrowing operation (short in the above example). */
9301 bool
9302 supportable_narrowing_operation (enum tree_code code,
9303 tree vectype_out, tree vectype_in,
9304 enum tree_code *code1, int *multi_step_cvt,
9305 vec<tree> *interm_types)
9307 machine_mode vec_mode;
9308 enum insn_code icode1;
9309 optab optab1, interm_optab;
9310 tree vectype = vectype_in;
9311 tree narrow_vectype = vectype_out;
9312 enum tree_code c1;
9313 tree intermediate_type, prev_type;
9314 machine_mode intermediate_mode, prev_mode;
9315 int i;
9316 bool uns;
9318 *multi_step_cvt = 0;
9319 switch (code)
9321 CASE_CONVERT:
9322 c1 = VEC_PACK_TRUNC_EXPR;
9323 break;
9325 case FIX_TRUNC_EXPR:
9326 c1 = VEC_PACK_FIX_TRUNC_EXPR;
9327 break;
9329 case FLOAT_EXPR:
9330 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
9331 tree code and optabs used for computing the operation. */
9332 return false;
9334 default:
9335 gcc_unreachable ();
9338 if (code == FIX_TRUNC_EXPR)
9339 /* The signedness is determined from output operand. */
9340 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
9341 else
9342 optab1 = optab_for_tree_code (c1, vectype, optab_default);
9344 if (!optab1)
9345 return false;
9347 vec_mode = TYPE_MODE (vectype);
9348 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
9349 return false;
9351 *code1 = c1;
9353 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
9354 /* For scalar masks we may have different boolean
9355 vector types having the same QImode. Thus we
9356 add additional check for elements number. */
9357 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9358 || (TYPE_VECTOR_SUBPARTS (vectype) * 2
9359 == TYPE_VECTOR_SUBPARTS (narrow_vectype)));
9361 /* Check if it's a multi-step conversion that can be done using intermediate
9362 types. */
9363 prev_mode = vec_mode;
9364 prev_type = vectype;
9365 if (code == FIX_TRUNC_EXPR)
9366 uns = TYPE_UNSIGNED (vectype_out);
9367 else
9368 uns = TYPE_UNSIGNED (vectype);
9370 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
9371 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
9372 costly than signed. */
9373 if (code == FIX_TRUNC_EXPR && uns)
9375 enum insn_code icode2;
9377 intermediate_type
9378 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
9379 interm_optab
9380 = optab_for_tree_code (c1, intermediate_type, optab_default);
9381 if (interm_optab != unknown_optab
9382 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
9383 && insn_data[icode1].operand[0].mode
9384 == insn_data[icode2].operand[0].mode)
9386 uns = false;
9387 optab1 = interm_optab;
9388 icode1 = icode2;
9392 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
9393 intermediate steps in promotion sequence. We try
9394 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
9395 interm_types->create (MAX_INTERM_CVT_STEPS);
9396 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
9398 intermediate_mode = insn_data[icode1].operand[0].mode;
9399 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
9401 intermediate_type
9402 = build_truth_vector_type (TYPE_VECTOR_SUBPARTS (prev_type) * 2,
9403 current_vector_size);
9404 if (intermediate_mode != TYPE_MODE (intermediate_type))
9405 return false;
9407 else
9408 intermediate_type
9409 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
9410 interm_optab
9411 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
9412 optab_default);
9413 if (!interm_optab
9414 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
9415 || insn_data[icode1].operand[0].mode != intermediate_mode
9416 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
9417 == CODE_FOR_nothing))
9418 break;
9420 interm_types->quick_push (intermediate_type);
9421 (*multi_step_cvt)++;
9423 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
9424 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9425 || (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2
9426 == TYPE_VECTOR_SUBPARTS (narrow_vectype)));
9428 prev_mode = intermediate_mode;
9429 prev_type = intermediate_type;
9430 optab1 = interm_optab;
9433 interm_types->release ();
9434 return false;