Merge trunk version 222452 into gupc branch.
[official-gcc.git] / gcc / tree-vect-stmts.c
blob4496293fb4616a27f02953e97ec5ad08e0460f5a
1 /* Statement Analysis and Transformation for Vectorization
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "target.h"
40 #include "predict.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "basic-block.h"
46 #include "gimple-pretty-print.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimplify.h"
54 #include "gimple-iterator.h"
55 #include "gimplify-me.h"
56 #include "gimple-ssa.h"
57 #include "tree-cfg.h"
58 #include "tree-phinodes.h"
59 #include "ssa-iterators.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "tree-ssa-loop-manip.h"
63 #include "cfgloop.h"
64 #include "tree-ssa-loop.h"
65 #include "tree-scalar-evolution.h"
66 #include "hashtab.h"
67 #include "rtl.h"
68 #include "flags.h"
69 #include "statistics.h"
70 #include "real.h"
71 #include "fixed-value.h"
72 #include "insn-config.h"
73 #include "expmed.h"
74 #include "dojump.h"
75 #include "explow.h"
76 #include "calls.h"
77 #include "emit-rtl.h"
78 #include "varasm.h"
79 #include "stmt.h"
80 #include "expr.h"
81 #include "recog.h" /* FIXME: for insn_data */
82 #include "insn-codes.h"
83 #include "optabs.h"
84 #include "diagnostic-core.h"
85 #include "tree-vectorizer.h"
86 #include "hash-map.h"
87 #include "plugin-api.h"
88 #include "ipa-ref.h"
89 #include "cgraph.h"
90 #include "builtins.h"
92 /* For lang_hooks.types.type_for_mode. */
93 #include "langhooks.h"
95 /* Return the vectorized type for the given statement. */
97 tree
98 stmt_vectype (struct _stmt_vec_info *stmt_info)
100 return STMT_VINFO_VECTYPE (stmt_info);
103 /* Return TRUE iff the given statement is in an inner loop relative to
104 the loop being vectorized. */
105 bool
106 stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
108 gimple stmt = STMT_VINFO_STMT (stmt_info);
109 basic_block bb = gimple_bb (stmt);
110 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
111 struct loop* loop;
113 if (!loop_vinfo)
114 return false;
116 loop = LOOP_VINFO_LOOP (loop_vinfo);
118 return (bb->loop_father == loop->inner);
121 /* Record the cost of a statement, either by directly informing the
122 target model or by saving it in a vector for later processing.
123 Return a preliminary estimate of the statement's cost. */
125 unsigned
126 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
127 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
128 int misalign, enum vect_cost_model_location where)
130 if (body_cost_vec)
132 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
133 add_stmt_info_to_vec (body_cost_vec, count, kind,
134 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
135 misalign);
136 return (unsigned)
137 (builtin_vectorization_cost (kind, vectype, misalign) * count);
140 else
142 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
143 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
144 void *target_cost_data;
146 if (loop_vinfo)
147 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
148 else
149 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
151 return add_stmt_cost (target_cost_data, count, kind, stmt_info,
152 misalign, where);
156 /* Return a variable of type ELEM_TYPE[NELEMS]. */
158 static tree
159 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
161 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
162 "vect_array");
165 /* ARRAY is an array of vectors created by create_vector_array.
166 Return an SSA_NAME for the vector in index N. The reference
167 is part of the vectorization of STMT and the vector is associated
168 with scalar destination SCALAR_DEST. */
170 static tree
171 read_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
172 tree array, unsigned HOST_WIDE_INT n)
174 tree vect_type, vect, vect_name, array_ref;
175 gimple new_stmt;
177 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
178 vect_type = TREE_TYPE (TREE_TYPE (array));
179 vect = vect_create_destination_var (scalar_dest, vect_type);
180 array_ref = build4 (ARRAY_REF, vect_type, array,
181 build_int_cst (size_type_node, n),
182 NULL_TREE, NULL_TREE);
184 new_stmt = gimple_build_assign (vect, array_ref);
185 vect_name = make_ssa_name (vect, new_stmt);
186 gimple_assign_set_lhs (new_stmt, vect_name);
187 vect_finish_stmt_generation (stmt, new_stmt, gsi);
189 return vect_name;
192 /* ARRAY is an array of vectors created by create_vector_array.
193 Emit code to store SSA_NAME VECT in index N of the array.
194 The store is part of the vectorization of STMT. */
196 static void
197 write_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree vect,
198 tree array, unsigned HOST_WIDE_INT n)
200 tree array_ref;
201 gimple new_stmt;
203 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
204 build_int_cst (size_type_node, n),
205 NULL_TREE, NULL_TREE);
207 new_stmt = gimple_build_assign (array_ref, vect);
208 vect_finish_stmt_generation (stmt, new_stmt, gsi);
211 /* PTR is a pointer to an array of type TYPE. Return a representation
212 of *PTR. The memory reference replaces those in FIRST_DR
213 (and its group). */
215 static tree
216 create_array_ref (tree type, tree ptr, struct data_reference *first_dr)
218 tree mem_ref, alias_ptr_type;
220 alias_ptr_type = reference_alias_ptr_type (DR_REF (first_dr));
221 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
222 /* Arrays have the same alignment as their type. */
223 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
224 return mem_ref;
227 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
229 /* Function vect_mark_relevant.
231 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
233 static void
234 vect_mark_relevant (vec<gimple> *worklist, gimple stmt,
235 enum vect_relevant relevant, bool live_p,
236 bool used_in_pattern)
238 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
239 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
240 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
241 gimple pattern_stmt;
243 if (dump_enabled_p ())
244 dump_printf_loc (MSG_NOTE, vect_location,
245 "mark relevant %d, live %d.\n", relevant, live_p);
247 /* If this stmt is an original stmt in a pattern, we might need to mark its
248 related pattern stmt instead of the original stmt. However, such stmts
249 may have their own uses that are not in any pattern, in such cases the
250 stmt itself should be marked. */
251 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
253 bool found = false;
254 if (!used_in_pattern)
256 imm_use_iterator imm_iter;
257 use_operand_p use_p;
258 gimple use_stmt;
259 tree lhs;
260 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
261 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
263 if (is_gimple_assign (stmt))
264 lhs = gimple_assign_lhs (stmt);
265 else
266 lhs = gimple_call_lhs (stmt);
268 /* This use is out of pattern use, if LHS has other uses that are
269 pattern uses, we should mark the stmt itself, and not the pattern
270 stmt. */
271 if (lhs && TREE_CODE (lhs) == SSA_NAME)
272 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
274 if (is_gimple_debug (USE_STMT (use_p)))
275 continue;
276 use_stmt = USE_STMT (use_p);
278 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
279 continue;
281 if (vinfo_for_stmt (use_stmt)
282 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
284 found = true;
285 break;
290 if (!found)
292 /* This is the last stmt in a sequence that was detected as a
293 pattern that can potentially be vectorized. Don't mark the stmt
294 as relevant/live because it's not going to be vectorized.
295 Instead mark the pattern-stmt that replaces it. */
297 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
299 if (dump_enabled_p ())
300 dump_printf_loc (MSG_NOTE, vect_location,
301 "last stmt in pattern. don't mark"
302 " relevant/live.\n");
303 stmt_info = vinfo_for_stmt (pattern_stmt);
304 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
305 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
306 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
307 stmt = pattern_stmt;
311 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
312 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
313 STMT_VINFO_RELEVANT (stmt_info) = relevant;
315 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
316 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
318 if (dump_enabled_p ())
319 dump_printf_loc (MSG_NOTE, vect_location,
320 "already marked relevant/live.\n");
321 return;
324 worklist->safe_push (stmt);
328 /* Function vect_stmt_relevant_p.
330 Return true if STMT in loop that is represented by LOOP_VINFO is
331 "relevant for vectorization".
333 A stmt is considered "relevant for vectorization" if:
334 - it has uses outside the loop.
335 - it has vdefs (it alters memory).
336 - control stmts in the loop (except for the exit condition).
338 CHECKME: what other side effects would the vectorizer allow? */
340 static bool
341 vect_stmt_relevant_p (gimple stmt, loop_vec_info loop_vinfo,
342 enum vect_relevant *relevant, bool *live_p)
344 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
345 ssa_op_iter op_iter;
346 imm_use_iterator imm_iter;
347 use_operand_p use_p;
348 def_operand_p def_p;
350 *relevant = vect_unused_in_scope;
351 *live_p = false;
353 /* cond stmt other than loop exit cond. */
354 if (is_ctrl_stmt (stmt)
355 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
356 != loop_exit_ctrl_vec_info_type)
357 *relevant = vect_used_in_scope;
359 /* changing memory. */
360 if (gimple_code (stmt) != GIMPLE_PHI)
361 if (gimple_vdef (stmt)
362 && !gimple_clobber_p (stmt))
364 if (dump_enabled_p ())
365 dump_printf_loc (MSG_NOTE, vect_location,
366 "vec_stmt_relevant_p: stmt has vdefs.\n");
367 *relevant = vect_used_in_scope;
370 /* uses outside the loop. */
371 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
373 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
375 basic_block bb = gimple_bb (USE_STMT (use_p));
376 if (!flow_bb_inside_loop_p (loop, bb))
378 if (dump_enabled_p ())
379 dump_printf_loc (MSG_NOTE, vect_location,
380 "vec_stmt_relevant_p: used out of loop.\n");
382 if (is_gimple_debug (USE_STMT (use_p)))
383 continue;
385 /* We expect all such uses to be in the loop exit phis
386 (because of loop closed form) */
387 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
388 gcc_assert (bb == single_exit (loop)->dest);
390 *live_p = true;
395 return (*live_p || *relevant);
399 /* Function exist_non_indexing_operands_for_use_p
401 USE is one of the uses attached to STMT. Check if USE is
402 used in STMT for anything other than indexing an array. */
404 static bool
405 exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
407 tree operand;
408 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
410 /* USE corresponds to some operand in STMT. If there is no data
411 reference in STMT, then any operand that corresponds to USE
412 is not indexing an array. */
413 if (!STMT_VINFO_DATA_REF (stmt_info))
414 return true;
416 /* STMT has a data_ref. FORNOW this means that its of one of
417 the following forms:
418 -1- ARRAY_REF = var
419 -2- var = ARRAY_REF
420 (This should have been verified in analyze_data_refs).
422 'var' in the second case corresponds to a def, not a use,
423 so USE cannot correspond to any operands that are not used
424 for array indexing.
426 Therefore, all we need to check is if STMT falls into the
427 first case, and whether var corresponds to USE. */
429 if (!gimple_assign_copy_p (stmt))
431 if (is_gimple_call (stmt)
432 && gimple_call_internal_p (stmt))
433 switch (gimple_call_internal_fn (stmt))
435 case IFN_MASK_STORE:
436 operand = gimple_call_arg (stmt, 3);
437 if (operand == use)
438 return true;
439 /* FALLTHRU */
440 case IFN_MASK_LOAD:
441 operand = gimple_call_arg (stmt, 2);
442 if (operand == use)
443 return true;
444 break;
445 default:
446 break;
448 return false;
451 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
452 return false;
453 operand = gimple_assign_rhs1 (stmt);
454 if (TREE_CODE (operand) != SSA_NAME)
455 return false;
457 if (operand == use)
458 return true;
460 return false;
465 Function process_use.
467 Inputs:
468 - a USE in STMT in a loop represented by LOOP_VINFO
469 - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
470 that defined USE. This is done by calling mark_relevant and passing it
471 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
472 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
473 be performed.
475 Outputs:
476 Generally, LIVE_P and RELEVANT are used to define the liveness and
477 relevance info of the DEF_STMT of this USE:
478 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
479 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
480 Exceptions:
481 - case 1: If USE is used only for address computations (e.g. array indexing),
482 which does not need to be directly vectorized, then the liveness/relevance
483 of the respective DEF_STMT is left unchanged.
484 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
485 skip DEF_STMT cause it had already been processed.
486 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
487 be modified accordingly.
489 Return true if everything is as expected. Return false otherwise. */
491 static bool
492 process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
493 enum vect_relevant relevant, vec<gimple> *worklist,
494 bool force)
496 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
497 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
498 stmt_vec_info dstmt_vinfo;
499 basic_block bb, def_bb;
500 tree def;
501 gimple def_stmt;
502 enum vect_def_type dt;
504 /* case 1: we are only interested in uses that need to be vectorized. Uses
505 that are used for address computation are not considered relevant. */
506 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
507 return true;
509 if (!vect_is_simple_use (use, stmt, loop_vinfo, NULL, &def_stmt, &def, &dt))
511 if (dump_enabled_p ())
512 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
513 "not vectorized: unsupported use in stmt.\n");
514 return false;
517 if (!def_stmt || gimple_nop_p (def_stmt))
518 return true;
520 def_bb = gimple_bb (def_stmt);
521 if (!flow_bb_inside_loop_p (loop, def_bb))
523 if (dump_enabled_p ())
524 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
525 return true;
528 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
529 DEF_STMT must have already been processed, because this should be the
530 only way that STMT, which is a reduction-phi, was put in the worklist,
531 as there should be no other uses for DEF_STMT in the loop. So we just
532 check that everything is as expected, and we are done. */
533 dstmt_vinfo = vinfo_for_stmt (def_stmt);
534 bb = gimple_bb (stmt);
535 if (gimple_code (stmt) == GIMPLE_PHI
536 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
537 && gimple_code (def_stmt) != GIMPLE_PHI
538 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
539 && bb->loop_father == def_bb->loop_father)
541 if (dump_enabled_p ())
542 dump_printf_loc (MSG_NOTE, vect_location,
543 "reduc-stmt defining reduc-phi in the same nest.\n");
544 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
545 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
546 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
547 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
548 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
549 return true;
552 /* case 3a: outer-loop stmt defining an inner-loop stmt:
553 outer-loop-header-bb:
554 d = def_stmt
555 inner-loop:
556 stmt # use (d)
557 outer-loop-tail-bb:
558 ... */
559 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
561 if (dump_enabled_p ())
562 dump_printf_loc (MSG_NOTE, vect_location,
563 "outer-loop def-stmt defining inner-loop stmt.\n");
565 switch (relevant)
567 case vect_unused_in_scope:
568 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
569 vect_used_in_scope : vect_unused_in_scope;
570 break;
572 case vect_used_in_outer_by_reduction:
573 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
574 relevant = vect_used_by_reduction;
575 break;
577 case vect_used_in_outer:
578 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
579 relevant = vect_used_in_scope;
580 break;
582 case vect_used_in_scope:
583 break;
585 default:
586 gcc_unreachable ();
590 /* case 3b: inner-loop stmt defining an outer-loop stmt:
591 outer-loop-header-bb:
593 inner-loop:
594 d = def_stmt
595 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
596 stmt # use (d) */
597 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
599 if (dump_enabled_p ())
600 dump_printf_loc (MSG_NOTE, vect_location,
601 "inner-loop def-stmt defining outer-loop stmt.\n");
603 switch (relevant)
605 case vect_unused_in_scope:
606 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
607 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
608 vect_used_in_outer_by_reduction : vect_unused_in_scope;
609 break;
611 case vect_used_by_reduction:
612 relevant = vect_used_in_outer_by_reduction;
613 break;
615 case vect_used_in_scope:
616 relevant = vect_used_in_outer;
617 break;
619 default:
620 gcc_unreachable ();
624 vect_mark_relevant (worklist, def_stmt, relevant, live_p,
625 is_pattern_stmt_p (stmt_vinfo));
626 return true;
630 /* Function vect_mark_stmts_to_be_vectorized.
632 Not all stmts in the loop need to be vectorized. For example:
634 for i...
635 for j...
636 1. T0 = i + j
637 2. T1 = a[T0]
639 3. j = j + 1
641 Stmt 1 and 3 do not need to be vectorized, because loop control and
642 addressing of vectorized data-refs are handled differently.
644 This pass detects such stmts. */
646 bool
647 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
649 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
650 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
651 unsigned int nbbs = loop->num_nodes;
652 gimple_stmt_iterator si;
653 gimple stmt;
654 unsigned int i;
655 stmt_vec_info stmt_vinfo;
656 basic_block bb;
657 gimple phi;
658 bool live_p;
659 enum vect_relevant relevant, tmp_relevant;
660 enum vect_def_type def_type;
662 if (dump_enabled_p ())
663 dump_printf_loc (MSG_NOTE, vect_location,
664 "=== vect_mark_stmts_to_be_vectorized ===\n");
666 auto_vec<gimple, 64> worklist;
668 /* 1. Init worklist. */
669 for (i = 0; i < nbbs; i++)
671 bb = bbs[i];
672 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
674 phi = gsi_stmt (si);
675 if (dump_enabled_p ())
677 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
678 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
681 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
682 vect_mark_relevant (&worklist, phi, relevant, live_p, false);
684 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
686 stmt = gsi_stmt (si);
687 if (dump_enabled_p ())
689 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
690 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
693 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
694 vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
698 /* 2. Process_worklist */
699 while (worklist.length () > 0)
701 use_operand_p use_p;
702 ssa_op_iter iter;
704 stmt = worklist.pop ();
705 if (dump_enabled_p ())
707 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
708 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
711 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
712 (DEF_STMT) as relevant/irrelevant and live/dead according to the
713 liveness and relevance properties of STMT. */
714 stmt_vinfo = vinfo_for_stmt (stmt);
715 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
716 live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
718 /* Generally, the liveness and relevance properties of STMT are
719 propagated as is to the DEF_STMTs of its USEs:
720 live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
721 relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
723 One exception is when STMT has been identified as defining a reduction
724 variable; in this case we set the liveness/relevance as follows:
725 live_p = false
726 relevant = vect_used_by_reduction
727 This is because we distinguish between two kinds of relevant stmts -
728 those that are used by a reduction computation, and those that are
729 (also) used by a regular computation. This allows us later on to
730 identify stmts that are used solely by a reduction, and therefore the
731 order of the results that they produce does not have to be kept. */
733 def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
734 tmp_relevant = relevant;
735 switch (def_type)
737 case vect_reduction_def:
738 switch (tmp_relevant)
740 case vect_unused_in_scope:
741 relevant = vect_used_by_reduction;
742 break;
744 case vect_used_by_reduction:
745 if (gimple_code (stmt) == GIMPLE_PHI)
746 break;
747 /* fall through */
749 default:
750 if (dump_enabled_p ())
751 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
752 "unsupported use of reduction.\n");
753 return false;
756 live_p = false;
757 break;
759 case vect_nested_cycle:
760 if (tmp_relevant != vect_unused_in_scope
761 && tmp_relevant != vect_used_in_outer_by_reduction
762 && tmp_relevant != vect_used_in_outer)
764 if (dump_enabled_p ())
765 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
766 "unsupported use of nested cycle.\n");
768 return false;
771 live_p = false;
772 break;
774 case vect_double_reduction_def:
775 if (tmp_relevant != vect_unused_in_scope
776 && tmp_relevant != vect_used_by_reduction)
778 if (dump_enabled_p ())
779 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
780 "unsupported use of double reduction.\n");
782 return false;
785 live_p = false;
786 break;
788 default:
789 break;
792 if (is_pattern_stmt_p (stmt_vinfo))
794 /* Pattern statements are not inserted into the code, so
795 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
796 have to scan the RHS or function arguments instead. */
797 if (is_gimple_assign (stmt))
799 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
800 tree op = gimple_assign_rhs1 (stmt);
802 i = 1;
803 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
805 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
806 live_p, relevant, &worklist, false)
807 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
808 live_p, relevant, &worklist, false))
809 return false;
810 i = 2;
812 for (; i < gimple_num_ops (stmt); i++)
814 op = gimple_op (stmt, i);
815 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
816 &worklist, false))
817 return false;
820 else if (is_gimple_call (stmt))
822 for (i = 0; i < gimple_call_num_args (stmt); i++)
824 tree arg = gimple_call_arg (stmt, i);
825 if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
826 &worklist, false))
827 return false;
831 else
832 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
834 tree op = USE_FROM_PTR (use_p);
835 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
836 &worklist, false))
837 return false;
840 if (STMT_VINFO_GATHER_P (stmt_vinfo))
842 tree off;
843 tree decl = vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
844 gcc_assert (decl);
845 if (!process_use (stmt, off, loop_vinfo, live_p, relevant,
846 &worklist, true))
847 return false;
849 } /* while worklist */
851 return true;
855 /* Function vect_model_simple_cost.
857 Models cost for simple operations, i.e. those that only emit ncopies of a
858 single op. Right now, this does not account for multiple insns that could
859 be generated for the single vector op. We will handle that shortly. */
861 void
862 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
863 enum vect_def_type *dt,
864 stmt_vector_for_cost *prologue_cost_vec,
865 stmt_vector_for_cost *body_cost_vec)
867 int i;
868 int inside_cost = 0, prologue_cost = 0;
870 /* The SLP costs were already calculated during SLP tree build. */
871 if (PURE_SLP_STMT (stmt_info))
872 return;
874 /* FORNOW: Assuming maximum 2 args per stmts. */
875 for (i = 0; i < 2; i++)
876 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
877 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
878 stmt_info, 0, vect_prologue);
880 /* Pass the inside-of-loop statements to the target-specific cost model. */
881 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
882 stmt_info, 0, vect_body);
884 if (dump_enabled_p ())
885 dump_printf_loc (MSG_NOTE, vect_location,
886 "vect_model_simple_cost: inside_cost = %d, "
887 "prologue_cost = %d .\n", inside_cost, prologue_cost);
891 /* Model cost for type demotion and promotion operations. PWR is normally
892 zero for single-step promotions and demotions. It will be one if
893 two-step promotion/demotion is required, and so on. Each additional
894 step doubles the number of instructions required. */
896 static void
897 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
898 enum vect_def_type *dt, int pwr)
900 int i, tmp;
901 int inside_cost = 0, prologue_cost = 0;
902 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
903 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
904 void *target_cost_data;
906 /* The SLP costs were already calculated during SLP tree build. */
907 if (PURE_SLP_STMT (stmt_info))
908 return;
910 if (loop_vinfo)
911 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
912 else
913 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
915 for (i = 0; i < pwr + 1; i++)
917 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
918 (i + 1) : i;
919 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
920 vec_promote_demote, stmt_info, 0,
921 vect_body);
924 /* FORNOW: Assuming maximum 2 args per stmts. */
925 for (i = 0; i < 2; i++)
926 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
927 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
928 stmt_info, 0, vect_prologue);
930 if (dump_enabled_p ())
931 dump_printf_loc (MSG_NOTE, vect_location,
932 "vect_model_promotion_demotion_cost: inside_cost = %d, "
933 "prologue_cost = %d .\n", inside_cost, prologue_cost);
936 /* Function vect_cost_group_size
938 For grouped load or store, return the group_size only if it is the first
939 load or store of a group, else return 1. This ensures that group size is
940 only returned once per group. */
942 static int
943 vect_cost_group_size (stmt_vec_info stmt_info)
945 gimple first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
947 if (first_stmt == STMT_VINFO_STMT (stmt_info))
948 return GROUP_SIZE (stmt_info);
950 return 1;
954 /* Function vect_model_store_cost
956 Models cost for stores. In the case of grouped accesses, one access
957 has the overhead of the grouped access attributed to it. */
959 void
960 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
961 bool store_lanes_p, enum vect_def_type dt,
962 slp_tree slp_node,
963 stmt_vector_for_cost *prologue_cost_vec,
964 stmt_vector_for_cost *body_cost_vec)
966 int group_size;
967 unsigned int inside_cost = 0, prologue_cost = 0;
968 struct data_reference *first_dr;
969 gimple first_stmt;
971 if (dt == vect_constant_def || dt == vect_external_def)
972 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
973 stmt_info, 0, vect_prologue);
975 /* Grouped access? */
976 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
978 if (slp_node)
980 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
981 group_size = 1;
983 else
985 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
986 group_size = vect_cost_group_size (stmt_info);
989 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
991 /* Not a grouped access. */
992 else
994 group_size = 1;
995 first_dr = STMT_VINFO_DATA_REF (stmt_info);
998 /* We assume that the cost of a single store-lanes instruction is
999 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
1000 access is instead being provided by a permute-and-store operation,
1001 include the cost of the permutes. */
1002 if (!store_lanes_p && group_size > 1)
1004 /* Uses a high and low interleave or shuffle operations for each
1005 needed permute. */
1006 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
1007 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1008 stmt_info, 0, vect_body);
1010 if (dump_enabled_p ())
1011 dump_printf_loc (MSG_NOTE, vect_location,
1012 "vect_model_store_cost: strided group_size = %d .\n",
1013 group_size);
1016 /* Costs of the stores. */
1017 vect_get_store_cost (first_dr, ncopies, &inside_cost, body_cost_vec);
1019 if (dump_enabled_p ())
1020 dump_printf_loc (MSG_NOTE, vect_location,
1021 "vect_model_store_cost: inside_cost = %d, "
1022 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1026 /* Calculate cost of DR's memory access. */
1027 void
1028 vect_get_store_cost (struct data_reference *dr, int ncopies,
1029 unsigned int *inside_cost,
1030 stmt_vector_for_cost *body_cost_vec)
1032 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1033 gimple stmt = DR_STMT (dr);
1034 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1036 switch (alignment_support_scheme)
1038 case dr_aligned:
1040 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1041 vector_store, stmt_info, 0,
1042 vect_body);
1044 if (dump_enabled_p ())
1045 dump_printf_loc (MSG_NOTE, vect_location,
1046 "vect_model_store_cost: aligned.\n");
1047 break;
1050 case dr_unaligned_supported:
1052 /* Here, we assign an additional cost for the unaligned store. */
1053 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1054 unaligned_store, stmt_info,
1055 DR_MISALIGNMENT (dr), vect_body);
1056 if (dump_enabled_p ())
1057 dump_printf_loc (MSG_NOTE, vect_location,
1058 "vect_model_store_cost: unaligned supported by "
1059 "hardware.\n");
1060 break;
1063 case dr_unaligned_unsupported:
1065 *inside_cost = VECT_MAX_COST;
1067 if (dump_enabled_p ())
1068 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1069 "vect_model_store_cost: unsupported access.\n");
1070 break;
1073 default:
1074 gcc_unreachable ();
1079 /* Function vect_model_load_cost
1081 Models cost for loads. In the case of grouped accesses, the last access
1082 has the overhead of the grouped access attributed to it. Since unaligned
1083 accesses are supported for loads, we also account for the costs of the
1084 access scheme chosen. */
1086 void
1087 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1088 bool load_lanes_p, slp_tree slp_node,
1089 stmt_vector_for_cost *prologue_cost_vec,
1090 stmt_vector_for_cost *body_cost_vec)
1092 int group_size;
1093 gimple first_stmt;
1094 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
1095 unsigned int inside_cost = 0, prologue_cost = 0;
1097 /* Grouped accesses? */
1098 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1099 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && first_stmt && !slp_node)
1101 group_size = vect_cost_group_size (stmt_info);
1102 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1104 /* Not a grouped access. */
1105 else
1107 group_size = 1;
1108 first_dr = dr;
1111 /* We assume that the cost of a single load-lanes instruction is
1112 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1113 access is instead being provided by a load-and-permute operation,
1114 include the cost of the permutes. */
1115 if (!load_lanes_p && group_size > 1)
1117 /* Uses an even and odd extract operations or shuffle operations
1118 for each needed permute. */
1119 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
1120 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1121 stmt_info, 0, vect_body);
1123 if (dump_enabled_p ())
1124 dump_printf_loc (MSG_NOTE, vect_location,
1125 "vect_model_load_cost: strided group_size = %d .\n",
1126 group_size);
1129 /* The loads themselves. */
1130 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1132 /* N scalar loads plus gathering them into a vector. */
1133 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1134 inside_cost += record_stmt_cost (body_cost_vec,
1135 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1136 scalar_load, stmt_info, 0, vect_body);
1137 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1138 stmt_info, 0, vect_body);
1140 else
1141 vect_get_load_cost (first_dr, ncopies,
1142 ((!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1143 || group_size > 1 || slp_node),
1144 &inside_cost, &prologue_cost,
1145 prologue_cost_vec, body_cost_vec, true);
1147 if (dump_enabled_p ())
1148 dump_printf_loc (MSG_NOTE, vect_location,
1149 "vect_model_load_cost: inside_cost = %d, "
1150 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1154 /* Calculate cost of DR's memory access. */
1155 void
1156 vect_get_load_cost (struct data_reference *dr, int ncopies,
1157 bool add_realign_cost, unsigned int *inside_cost,
1158 unsigned int *prologue_cost,
1159 stmt_vector_for_cost *prologue_cost_vec,
1160 stmt_vector_for_cost *body_cost_vec,
1161 bool record_prologue_costs)
1163 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1164 gimple stmt = DR_STMT (dr);
1165 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1167 switch (alignment_support_scheme)
1169 case dr_aligned:
1171 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1172 stmt_info, 0, vect_body);
1174 if (dump_enabled_p ())
1175 dump_printf_loc (MSG_NOTE, vect_location,
1176 "vect_model_load_cost: aligned.\n");
1178 break;
1180 case dr_unaligned_supported:
1182 /* Here, we assign an additional cost for the unaligned load. */
1183 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1184 unaligned_load, stmt_info,
1185 DR_MISALIGNMENT (dr), vect_body);
1187 if (dump_enabled_p ())
1188 dump_printf_loc (MSG_NOTE, vect_location,
1189 "vect_model_load_cost: unaligned supported by "
1190 "hardware.\n");
1192 break;
1194 case dr_explicit_realign:
1196 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1197 vector_load, stmt_info, 0, vect_body);
1198 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1199 vec_perm, stmt_info, 0, vect_body);
1201 /* FIXME: If the misalignment remains fixed across the iterations of
1202 the containing loop, the following cost should be added to the
1203 prologue costs. */
1204 if (targetm.vectorize.builtin_mask_for_load)
1205 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1206 stmt_info, 0, vect_body);
1208 if (dump_enabled_p ())
1209 dump_printf_loc (MSG_NOTE, vect_location,
1210 "vect_model_load_cost: explicit realign\n");
1212 break;
1214 case dr_explicit_realign_optimized:
1216 if (dump_enabled_p ())
1217 dump_printf_loc (MSG_NOTE, vect_location,
1218 "vect_model_load_cost: unaligned software "
1219 "pipelined.\n");
1221 /* Unaligned software pipeline has a load of an address, an initial
1222 load, and possibly a mask operation to "prime" the loop. However,
1223 if this is an access in a group of loads, which provide grouped
1224 access, then the above cost should only be considered for one
1225 access in the group. Inside the loop, there is a load op
1226 and a realignment op. */
1228 if (add_realign_cost && record_prologue_costs)
1230 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1231 vector_stmt, stmt_info,
1232 0, vect_prologue);
1233 if (targetm.vectorize.builtin_mask_for_load)
1234 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1235 vector_stmt, stmt_info,
1236 0, vect_prologue);
1239 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1240 stmt_info, 0, vect_body);
1241 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1242 stmt_info, 0, vect_body);
1244 if (dump_enabled_p ())
1245 dump_printf_loc (MSG_NOTE, vect_location,
1246 "vect_model_load_cost: explicit realign optimized"
1247 "\n");
1249 break;
1252 case dr_unaligned_unsupported:
1254 *inside_cost = VECT_MAX_COST;
1256 if (dump_enabled_p ())
1257 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1258 "vect_model_load_cost: unsupported access.\n");
1259 break;
1262 default:
1263 gcc_unreachable ();
1267 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1268 the loop preheader for the vectorized stmt STMT. */
1270 static void
1271 vect_init_vector_1 (gimple stmt, gimple new_stmt, gimple_stmt_iterator *gsi)
1273 if (gsi)
1274 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1275 else
1277 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1278 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1280 if (loop_vinfo)
1282 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1283 basic_block new_bb;
1284 edge pe;
1286 if (nested_in_vect_loop_p (loop, stmt))
1287 loop = loop->inner;
1289 pe = loop_preheader_edge (loop);
1290 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1291 gcc_assert (!new_bb);
1293 else
1295 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1296 basic_block bb;
1297 gimple_stmt_iterator gsi_bb_start;
1299 gcc_assert (bb_vinfo);
1300 bb = BB_VINFO_BB (bb_vinfo);
1301 gsi_bb_start = gsi_after_labels (bb);
1302 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1306 if (dump_enabled_p ())
1308 dump_printf_loc (MSG_NOTE, vect_location,
1309 "created new init_stmt: ");
1310 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1314 /* Function vect_init_vector.
1316 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1317 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1318 vector type a vector with all elements equal to VAL is created first.
1319 Place the initialization at BSI if it is not NULL. Otherwise, place the
1320 initialization at the loop preheader.
1321 Return the DEF of INIT_STMT.
1322 It will be used in the vectorization of STMT. */
1324 tree
1325 vect_init_vector (gimple stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1327 tree new_var;
1328 gimple init_stmt;
1329 tree vec_oprnd;
1330 tree new_temp;
1332 if (TREE_CODE (type) == VECTOR_TYPE
1333 && TREE_CODE (TREE_TYPE (val)) != VECTOR_TYPE)
1335 if (!types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1337 if (CONSTANT_CLASS_P (val))
1338 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (type), val);
1339 else
1341 new_temp = make_ssa_name (TREE_TYPE (type));
1342 init_stmt = gimple_build_assign (new_temp, NOP_EXPR, val);
1343 vect_init_vector_1 (stmt, init_stmt, gsi);
1344 val = new_temp;
1347 val = build_vector_from_val (type, val);
1350 new_var = vect_get_new_vect_var (type, vect_simple_var, "cst_");
1351 init_stmt = gimple_build_assign (new_var, val);
1352 new_temp = make_ssa_name (new_var, init_stmt);
1353 gimple_assign_set_lhs (init_stmt, new_temp);
1354 vect_init_vector_1 (stmt, init_stmt, gsi);
1355 vec_oprnd = gimple_assign_lhs (init_stmt);
1356 return vec_oprnd;
1360 /* Function vect_get_vec_def_for_operand.
1362 OP is an operand in STMT. This function returns a (vector) def that will be
1363 used in the vectorized stmt for STMT.
1365 In the case that OP is an SSA_NAME which is defined in the loop, then
1366 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1368 In case OP is an invariant or constant, a new stmt that creates a vector def
1369 needs to be introduced. */
1371 tree
1372 vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
1374 tree vec_oprnd;
1375 gimple vec_stmt;
1376 gimple def_stmt;
1377 stmt_vec_info def_stmt_info = NULL;
1378 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1379 unsigned int nunits;
1380 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1381 tree def;
1382 enum vect_def_type dt;
1383 bool is_simple_use;
1384 tree vector_type;
1386 if (dump_enabled_p ())
1388 dump_printf_loc (MSG_NOTE, vect_location,
1389 "vect_get_vec_def_for_operand: ");
1390 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1391 dump_printf (MSG_NOTE, "\n");
1394 is_simple_use = vect_is_simple_use (op, stmt, loop_vinfo, NULL,
1395 &def_stmt, &def, &dt);
1396 gcc_assert (is_simple_use);
1397 if (dump_enabled_p ())
1399 int loc_printed = 0;
1400 if (def)
1402 dump_printf_loc (MSG_NOTE, vect_location, "def = ");
1403 loc_printed = 1;
1404 dump_generic_expr (MSG_NOTE, TDF_SLIM, def);
1405 dump_printf (MSG_NOTE, "\n");
1407 if (def_stmt)
1409 if (loc_printed)
1410 dump_printf (MSG_NOTE, " def_stmt = ");
1411 else
1412 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1413 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1417 switch (dt)
1419 /* Case 1: operand is a constant. */
1420 case vect_constant_def:
1422 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1423 gcc_assert (vector_type);
1424 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
1426 if (scalar_def)
1427 *scalar_def = op;
1429 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1430 if (dump_enabled_p ())
1431 dump_printf_loc (MSG_NOTE, vect_location,
1432 "Create vector_cst. nunits = %d\n", nunits);
1434 return vect_init_vector (stmt, op, vector_type, NULL);
1437 /* Case 2: operand is defined outside the loop - loop invariant. */
1438 case vect_external_def:
1440 vector_type = get_vectype_for_scalar_type (TREE_TYPE (def));
1441 gcc_assert (vector_type);
1443 if (scalar_def)
1444 *scalar_def = def;
1446 /* Create 'vec_inv = {inv,inv,..,inv}' */
1447 if (dump_enabled_p ())
1448 dump_printf_loc (MSG_NOTE, vect_location, "Create vector_inv.\n");
1450 return vect_init_vector (stmt, def, vector_type, NULL);
1453 /* Case 3: operand is defined inside the loop. */
1454 case vect_internal_def:
1456 if (scalar_def)
1457 *scalar_def = NULL/* FIXME tuples: def_stmt*/;
1459 /* Get the def from the vectorized stmt. */
1460 def_stmt_info = vinfo_for_stmt (def_stmt);
1462 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1463 /* Get vectorized pattern statement. */
1464 if (!vec_stmt
1465 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1466 && !STMT_VINFO_RELEVANT (def_stmt_info))
1467 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1468 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1469 gcc_assert (vec_stmt);
1470 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1471 vec_oprnd = PHI_RESULT (vec_stmt);
1472 else if (is_gimple_call (vec_stmt))
1473 vec_oprnd = gimple_call_lhs (vec_stmt);
1474 else
1475 vec_oprnd = gimple_assign_lhs (vec_stmt);
1476 return vec_oprnd;
1479 /* Case 4: operand is defined by a loop header phi - reduction */
1480 case vect_reduction_def:
1481 case vect_double_reduction_def:
1482 case vect_nested_cycle:
1484 struct loop *loop;
1486 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1487 loop = (gimple_bb (def_stmt))->loop_father;
1489 /* Get the def before the loop */
1490 op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
1491 return get_initial_def_for_reduction (stmt, op, scalar_def);
1494 /* Case 5: operand is defined by loop-header phi - induction. */
1495 case vect_induction_def:
1497 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1499 /* Get the def from the vectorized stmt. */
1500 def_stmt_info = vinfo_for_stmt (def_stmt);
1501 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1502 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1503 vec_oprnd = PHI_RESULT (vec_stmt);
1504 else
1505 vec_oprnd = gimple_get_lhs (vec_stmt);
1506 return vec_oprnd;
1509 default:
1510 gcc_unreachable ();
1515 /* Function vect_get_vec_def_for_stmt_copy
1517 Return a vector-def for an operand. This function is used when the
1518 vectorized stmt to be created (by the caller to this function) is a "copy"
1519 created in case the vectorized result cannot fit in one vector, and several
1520 copies of the vector-stmt are required. In this case the vector-def is
1521 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1522 of the stmt that defines VEC_OPRND.
1523 DT is the type of the vector def VEC_OPRND.
1525 Context:
1526 In case the vectorization factor (VF) is bigger than the number
1527 of elements that can fit in a vectype (nunits), we have to generate
1528 more than one vector stmt to vectorize the scalar stmt. This situation
1529 arises when there are multiple data-types operated upon in the loop; the
1530 smallest data-type determines the VF, and as a result, when vectorizing
1531 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1532 vector stmt (each computing a vector of 'nunits' results, and together
1533 computing 'VF' results in each iteration). This function is called when
1534 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1535 which VF=16 and nunits=4, so the number of copies required is 4):
1537 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1539 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1540 VS1.1: vx.1 = memref1 VS1.2
1541 VS1.2: vx.2 = memref2 VS1.3
1542 VS1.3: vx.3 = memref3
1544 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1545 VSnew.1: vz1 = vx.1 + ... VSnew.2
1546 VSnew.2: vz2 = vx.2 + ... VSnew.3
1547 VSnew.3: vz3 = vx.3 + ...
1549 The vectorization of S1 is explained in vectorizable_load.
1550 The vectorization of S2:
1551 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1552 the function 'vect_get_vec_def_for_operand' is called to
1553 get the relevant vector-def for each operand of S2. For operand x it
1554 returns the vector-def 'vx.0'.
1556 To create the remaining copies of the vector-stmt (VSnew.j), this
1557 function is called to get the relevant vector-def for each operand. It is
1558 obtained from the respective VS1.j stmt, which is recorded in the
1559 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1561 For example, to obtain the vector-def 'vx.1' in order to create the
1562 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1563 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1564 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1565 and return its def ('vx.1').
1566 Overall, to create the above sequence this function will be called 3 times:
1567 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1568 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1569 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1571 tree
1572 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1574 gimple vec_stmt_for_operand;
1575 stmt_vec_info def_stmt_info;
1577 /* Do nothing; can reuse same def. */
1578 if (dt == vect_external_def || dt == vect_constant_def )
1579 return vec_oprnd;
1581 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1582 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1583 gcc_assert (def_stmt_info);
1584 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1585 gcc_assert (vec_stmt_for_operand);
1586 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1587 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1588 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1589 else
1590 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1591 return vec_oprnd;
1595 /* Get vectorized definitions for the operands to create a copy of an original
1596 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1598 static void
1599 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1600 vec<tree> *vec_oprnds0,
1601 vec<tree> *vec_oprnds1)
1603 tree vec_oprnd = vec_oprnds0->pop ();
1605 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1606 vec_oprnds0->quick_push (vec_oprnd);
1608 if (vec_oprnds1 && vec_oprnds1->length ())
1610 vec_oprnd = vec_oprnds1->pop ();
1611 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1612 vec_oprnds1->quick_push (vec_oprnd);
1617 /* Get vectorized definitions for OP0 and OP1.
1618 REDUC_INDEX is the index of reduction operand in case of reduction,
1619 and -1 otherwise. */
1621 void
1622 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
1623 vec<tree> *vec_oprnds0,
1624 vec<tree> *vec_oprnds1,
1625 slp_tree slp_node, int reduc_index)
1627 if (slp_node)
1629 int nops = (op1 == NULL_TREE) ? 1 : 2;
1630 auto_vec<tree> ops (nops);
1631 auto_vec<vec<tree> > vec_defs (nops);
1633 ops.quick_push (op0);
1634 if (op1)
1635 ops.quick_push (op1);
1637 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1639 *vec_oprnds0 = vec_defs[0];
1640 if (op1)
1641 *vec_oprnds1 = vec_defs[1];
1643 else
1645 tree vec_oprnd;
1647 vec_oprnds0->create (1);
1648 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);
1649 vec_oprnds0->quick_push (vec_oprnd);
1651 if (op1)
1653 vec_oprnds1->create (1);
1654 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);
1655 vec_oprnds1->quick_push (vec_oprnd);
1661 /* Function vect_finish_stmt_generation.
1663 Insert a new stmt. */
1665 void
1666 vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
1667 gimple_stmt_iterator *gsi)
1669 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1670 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1671 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1673 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1675 if (!gsi_end_p (*gsi)
1676 && gimple_has_mem_ops (vec_stmt))
1678 gimple at_stmt = gsi_stmt (*gsi);
1679 tree vuse = gimple_vuse (at_stmt);
1680 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1682 tree vdef = gimple_vdef (at_stmt);
1683 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1684 /* If we have an SSA vuse and insert a store, update virtual
1685 SSA form to avoid triggering the renamer. Do so only
1686 if we can easily see all uses - which is what almost always
1687 happens with the way vectorized stmts are inserted. */
1688 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1689 && ((is_gimple_assign (vec_stmt)
1690 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1691 || (is_gimple_call (vec_stmt)
1692 && !(gimple_call_flags (vec_stmt)
1693 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1695 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1696 gimple_set_vdef (vec_stmt, new_vdef);
1697 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1701 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1703 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo,
1704 bb_vinfo));
1706 if (dump_enabled_p ())
1708 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1709 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1712 gimple_set_location (vec_stmt, gimple_location (stmt));
1714 /* While EH edges will generally prevent vectorization, stmt might
1715 e.g. be in a must-not-throw region. Ensure newly created stmts
1716 that could throw are part of the same region. */
1717 int lp_nr = lookup_stmt_eh_lp (stmt);
1718 if (lp_nr != 0 && stmt_could_throw_p (vec_stmt))
1719 add_stmt_to_eh_lp (vec_stmt, lp_nr);
1722 /* Checks if CALL can be vectorized in type VECTYPE. Returns
1723 a function declaration if the target has a vectorized version
1724 of the function, or NULL_TREE if the function cannot be vectorized. */
1726 tree
1727 vectorizable_function (gcall *call, tree vectype_out, tree vectype_in)
1729 tree fndecl = gimple_call_fndecl (call);
1731 /* We only handle functions that do not read or clobber memory -- i.e.
1732 const or novops ones. */
1733 if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
1734 return NULL_TREE;
1736 if (!fndecl
1737 || TREE_CODE (fndecl) != FUNCTION_DECL
1738 || !DECL_BUILT_IN (fndecl))
1739 return NULL_TREE;
1741 return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
1742 vectype_in);
1746 static tree permute_vec_elements (tree, tree, tree, gimple,
1747 gimple_stmt_iterator *);
1750 /* Function vectorizable_mask_load_store.
1752 Check if STMT performs a conditional load or store that can be vectorized.
1753 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1754 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
1755 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1757 static bool
1758 vectorizable_mask_load_store (gimple stmt, gimple_stmt_iterator *gsi,
1759 gimple *vec_stmt, slp_tree slp_node)
1761 tree vec_dest = NULL;
1762 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1763 stmt_vec_info prev_stmt_info;
1764 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1765 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1766 bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
1767 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1768 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1769 tree elem_type;
1770 gimple new_stmt;
1771 tree dummy;
1772 tree dataref_ptr = NULL_TREE;
1773 gimple ptr_incr;
1774 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1775 int ncopies;
1776 int i, j;
1777 bool inv_p;
1778 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
1779 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
1780 int gather_scale = 1;
1781 enum vect_def_type gather_dt = vect_unknown_def_type;
1782 bool is_store;
1783 tree mask;
1784 gimple def_stmt;
1785 tree def;
1786 enum vect_def_type dt;
1788 if (slp_node != NULL)
1789 return false;
1791 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
1792 gcc_assert (ncopies >= 1);
1794 is_store = gimple_call_internal_fn (stmt) == IFN_MASK_STORE;
1795 mask = gimple_call_arg (stmt, 2);
1796 if (TYPE_PRECISION (TREE_TYPE (mask))
1797 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))))
1798 return false;
1800 /* FORNOW. This restriction should be relaxed. */
1801 if (nested_in_vect_loop && ncopies > 1)
1803 if (dump_enabled_p ())
1804 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1805 "multiple types in nested loop.");
1806 return false;
1809 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1810 return false;
1812 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
1813 return false;
1815 if (!STMT_VINFO_DATA_REF (stmt_info))
1816 return false;
1818 elem_type = TREE_TYPE (vectype);
1820 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1821 return false;
1823 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1824 return false;
1826 if (STMT_VINFO_GATHER_P (stmt_info))
1828 gimple def_stmt;
1829 tree def;
1830 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
1831 &gather_off, &gather_scale);
1832 gcc_assert (gather_decl);
1833 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, NULL,
1834 &def_stmt, &def, &gather_dt,
1835 &gather_off_vectype))
1837 if (dump_enabled_p ())
1838 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1839 "gather index use not simple.");
1840 return false;
1843 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
1844 tree masktype
1845 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
1846 if (TREE_CODE (masktype) == INTEGER_TYPE)
1848 if (dump_enabled_p ())
1849 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1850 "masked gather with integer mask not supported.");
1851 return false;
1854 else if (tree_int_cst_compare (nested_in_vect_loop
1855 ? STMT_VINFO_DR_STEP (stmt_info)
1856 : DR_STEP (dr), size_zero_node) <= 0)
1857 return false;
1858 else if (!VECTOR_MODE_P (TYPE_MODE (vectype))
1859 || !can_vec_mask_load_store_p (TYPE_MODE (vectype), !is_store))
1860 return false;
1862 if (TREE_CODE (mask) != SSA_NAME)
1863 return false;
1865 if (!vect_is_simple_use (mask, stmt, loop_vinfo, NULL,
1866 &def_stmt, &def, &dt))
1867 return false;
1869 if (is_store)
1871 tree rhs = gimple_call_arg (stmt, 3);
1872 if (!vect_is_simple_use (rhs, stmt, loop_vinfo, NULL,
1873 &def_stmt, &def, &dt))
1874 return false;
1877 if (!vec_stmt) /* transformation not required. */
1879 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
1880 if (is_store)
1881 vect_model_store_cost (stmt_info, ncopies, false, dt,
1882 NULL, NULL, NULL);
1883 else
1884 vect_model_load_cost (stmt_info, ncopies, false, NULL, NULL, NULL);
1885 return true;
1888 /** Transform. **/
1890 if (STMT_VINFO_GATHER_P (stmt_info))
1892 tree vec_oprnd0 = NULL_TREE, op;
1893 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
1894 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
1895 tree ptr, vec_mask = NULL_TREE, mask_op = NULL_TREE, var, scale;
1896 tree perm_mask = NULL_TREE, prev_res = NULL_TREE;
1897 tree mask_perm_mask = NULL_TREE;
1898 edge pe = loop_preheader_edge (loop);
1899 gimple_seq seq;
1900 basic_block new_bb;
1901 enum { NARROW, NONE, WIDEN } modifier;
1902 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
1904 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
1905 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1906 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1907 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1908 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1909 scaletype = TREE_VALUE (arglist);
1910 gcc_checking_assert (types_compatible_p (srctype, rettype)
1911 && types_compatible_p (srctype, masktype));
1913 if (nunits == gather_off_nunits)
1914 modifier = NONE;
1915 else if (nunits == gather_off_nunits / 2)
1917 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
1918 modifier = WIDEN;
1920 for (i = 0; i < gather_off_nunits; ++i)
1921 sel[i] = i | nunits;
1923 perm_mask = vect_gen_perm_mask_checked (gather_off_vectype, sel);
1925 else if (nunits == gather_off_nunits * 2)
1927 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
1928 modifier = NARROW;
1930 for (i = 0; i < nunits; ++i)
1931 sel[i] = i < gather_off_nunits
1932 ? i : i + nunits - gather_off_nunits;
1934 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
1935 ncopies *= 2;
1936 for (i = 0; i < nunits; ++i)
1937 sel[i] = i | gather_off_nunits;
1938 mask_perm_mask = vect_gen_perm_mask_checked (masktype, sel);
1940 else
1941 gcc_unreachable ();
1943 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
1945 ptr = fold_convert (ptrtype, gather_base);
1946 if (!is_gimple_min_invariant (ptr))
1948 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
1949 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
1950 gcc_assert (!new_bb);
1953 scale = build_int_cst (scaletype, gather_scale);
1955 prev_stmt_info = NULL;
1956 for (j = 0; j < ncopies; ++j)
1958 if (modifier == WIDEN && (j & 1))
1959 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
1960 perm_mask, stmt, gsi);
1961 else if (j == 0)
1962 op = vec_oprnd0
1963 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
1964 else
1965 op = vec_oprnd0
1966 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
1968 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
1970 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
1971 == TYPE_VECTOR_SUBPARTS (idxtype));
1972 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
1973 var = make_ssa_name (var);
1974 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
1975 new_stmt
1976 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
1977 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1978 op = var;
1981 if (mask_perm_mask && (j & 1))
1982 mask_op = permute_vec_elements (mask_op, mask_op,
1983 mask_perm_mask, stmt, gsi);
1984 else
1986 if (j == 0)
1987 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
1988 else
1990 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL,
1991 &def_stmt, &def, &dt);
1992 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
1995 mask_op = vec_mask;
1996 if (!useless_type_conversion_p (masktype, TREE_TYPE (vec_mask)))
1998 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask_op))
1999 == TYPE_VECTOR_SUBPARTS (masktype));
2000 var = vect_get_new_vect_var (masktype, vect_simple_var,
2001 NULL);
2002 var = make_ssa_name (var);
2003 mask_op = build1 (VIEW_CONVERT_EXPR, masktype, mask_op);
2004 new_stmt
2005 = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_op);
2006 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2007 mask_op = var;
2011 new_stmt
2012 = gimple_build_call (gather_decl, 5, mask_op, ptr, op, mask_op,
2013 scale);
2015 if (!useless_type_conversion_p (vectype, rettype))
2017 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
2018 == TYPE_VECTOR_SUBPARTS (rettype));
2019 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
2020 op = make_ssa_name (var, new_stmt);
2021 gimple_call_set_lhs (new_stmt, op);
2022 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2023 var = make_ssa_name (vec_dest);
2024 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
2025 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2027 else
2029 var = make_ssa_name (vec_dest, new_stmt);
2030 gimple_call_set_lhs (new_stmt, var);
2033 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2035 if (modifier == NARROW)
2037 if ((j & 1) == 0)
2039 prev_res = var;
2040 continue;
2042 var = permute_vec_elements (prev_res, var,
2043 perm_mask, stmt, gsi);
2044 new_stmt = SSA_NAME_DEF_STMT (var);
2047 if (prev_stmt_info == NULL)
2048 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2049 else
2050 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2051 prev_stmt_info = vinfo_for_stmt (new_stmt);
2054 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
2055 from the IL. */
2056 tree lhs = gimple_call_lhs (stmt);
2057 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2058 set_vinfo_for_stmt (new_stmt, stmt_info);
2059 set_vinfo_for_stmt (stmt, NULL);
2060 STMT_VINFO_STMT (stmt_info) = new_stmt;
2061 gsi_replace (gsi, new_stmt, true);
2062 return true;
2064 else if (is_store)
2066 tree vec_rhs = NULL_TREE, vec_mask = NULL_TREE;
2067 prev_stmt_info = NULL;
2068 for (i = 0; i < ncopies; i++)
2070 unsigned align, misalign;
2072 if (i == 0)
2074 tree rhs = gimple_call_arg (stmt, 3);
2075 vec_rhs = vect_get_vec_def_for_operand (rhs, stmt, NULL);
2076 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
2077 /* We should have catched mismatched types earlier. */
2078 gcc_assert (useless_type_conversion_p (vectype,
2079 TREE_TYPE (vec_rhs)));
2080 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2081 NULL_TREE, &dummy, gsi,
2082 &ptr_incr, false, &inv_p);
2083 gcc_assert (!inv_p);
2085 else
2087 vect_is_simple_use (vec_rhs, NULL, loop_vinfo, NULL, &def_stmt,
2088 &def, &dt);
2089 vec_rhs = vect_get_vec_def_for_stmt_copy (dt, vec_rhs);
2090 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL, &def_stmt,
2091 &def, &dt);
2092 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2093 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2094 TYPE_SIZE_UNIT (vectype));
2097 align = TYPE_ALIGN_UNIT (vectype);
2098 if (aligned_access_p (dr))
2099 misalign = 0;
2100 else if (DR_MISALIGNMENT (dr) == -1)
2102 align = TYPE_ALIGN_UNIT (elem_type);
2103 misalign = 0;
2105 else
2106 misalign = DR_MISALIGNMENT (dr);
2107 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2108 misalign);
2109 new_stmt
2110 = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
2111 gimple_call_arg (stmt, 1),
2112 vec_mask, vec_rhs);
2113 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2114 if (i == 0)
2115 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2116 else
2117 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2118 prev_stmt_info = vinfo_for_stmt (new_stmt);
2121 else
2123 tree vec_mask = NULL_TREE;
2124 prev_stmt_info = NULL;
2125 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
2126 for (i = 0; i < ncopies; i++)
2128 unsigned align, misalign;
2130 if (i == 0)
2132 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
2133 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2134 NULL_TREE, &dummy, gsi,
2135 &ptr_incr, false, &inv_p);
2136 gcc_assert (!inv_p);
2138 else
2140 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL, &def_stmt,
2141 &def, &dt);
2142 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2143 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2144 TYPE_SIZE_UNIT (vectype));
2147 align = TYPE_ALIGN_UNIT (vectype);
2148 if (aligned_access_p (dr))
2149 misalign = 0;
2150 else if (DR_MISALIGNMENT (dr) == -1)
2152 align = TYPE_ALIGN_UNIT (elem_type);
2153 misalign = 0;
2155 else
2156 misalign = DR_MISALIGNMENT (dr);
2157 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2158 misalign);
2159 new_stmt
2160 = gimple_build_call_internal (IFN_MASK_LOAD, 3, dataref_ptr,
2161 gimple_call_arg (stmt, 1),
2162 vec_mask);
2163 gimple_call_set_lhs (new_stmt, make_ssa_name (vec_dest));
2164 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2165 if (i == 0)
2166 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2167 else
2168 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2169 prev_stmt_info = vinfo_for_stmt (new_stmt);
2173 if (!is_store)
2175 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
2176 from the IL. */
2177 tree lhs = gimple_call_lhs (stmt);
2178 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2179 set_vinfo_for_stmt (new_stmt, stmt_info);
2180 set_vinfo_for_stmt (stmt, NULL);
2181 STMT_VINFO_STMT (stmt_info) = new_stmt;
2182 gsi_replace (gsi, new_stmt, true);
2185 return true;
2189 /* Function vectorizable_call.
2191 Check if GS performs a function call that can be vectorized.
2192 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2193 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2194 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2196 static bool
2197 vectorizable_call (gimple gs, gimple_stmt_iterator *gsi, gimple *vec_stmt,
2198 slp_tree slp_node)
2200 gcall *stmt;
2201 tree vec_dest;
2202 tree scalar_dest;
2203 tree op, type;
2204 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2205 stmt_vec_info stmt_info = vinfo_for_stmt (gs), prev_stmt_info;
2206 tree vectype_out, vectype_in;
2207 int nunits_in;
2208 int nunits_out;
2209 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2210 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2211 tree fndecl, new_temp, def, rhs_type;
2212 gimple def_stmt;
2213 enum vect_def_type dt[3]
2214 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
2215 gimple new_stmt = NULL;
2216 int ncopies, j;
2217 vec<tree> vargs = vNULL;
2218 enum { NARROW, NONE, WIDEN } modifier;
2219 size_t i, nargs;
2220 tree lhs;
2222 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2223 return false;
2225 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2226 return false;
2228 /* Is GS a vectorizable call? */
2229 stmt = dyn_cast <gcall *> (gs);
2230 if (!stmt)
2231 return false;
2233 if (gimple_call_internal_p (stmt)
2234 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2235 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
2236 return vectorizable_mask_load_store (stmt, gsi, vec_stmt,
2237 slp_node);
2239 if (gimple_call_lhs (stmt) == NULL_TREE
2240 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2241 return false;
2243 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2245 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2247 /* Process function arguments. */
2248 rhs_type = NULL_TREE;
2249 vectype_in = NULL_TREE;
2250 nargs = gimple_call_num_args (stmt);
2252 /* Bail out if the function has more than three arguments, we do not have
2253 interesting builtin functions to vectorize with more than two arguments
2254 except for fma. No arguments is also not good. */
2255 if (nargs == 0 || nargs > 3)
2256 return false;
2258 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
2259 if (gimple_call_internal_p (stmt)
2260 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2262 nargs = 0;
2263 rhs_type = unsigned_type_node;
2266 for (i = 0; i < nargs; i++)
2268 tree opvectype;
2270 op = gimple_call_arg (stmt, i);
2272 /* We can only handle calls with arguments of the same type. */
2273 if (rhs_type
2274 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
2276 if (dump_enabled_p ())
2277 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2278 "argument types differ.\n");
2279 return false;
2281 if (!rhs_type)
2282 rhs_type = TREE_TYPE (op);
2284 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2285 &def_stmt, &def, &dt[i], &opvectype))
2287 if (dump_enabled_p ())
2288 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2289 "use not simple.\n");
2290 return false;
2293 if (!vectype_in)
2294 vectype_in = opvectype;
2295 else if (opvectype
2296 && opvectype != vectype_in)
2298 if (dump_enabled_p ())
2299 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2300 "argument vector types differ.\n");
2301 return false;
2304 /* If all arguments are external or constant defs use a vector type with
2305 the same size as the output vector type. */
2306 if (!vectype_in)
2307 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
2308 if (vec_stmt)
2309 gcc_assert (vectype_in);
2310 if (!vectype_in)
2312 if (dump_enabled_p ())
2314 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2315 "no vectype for scalar type ");
2316 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
2317 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2320 return false;
2323 /* FORNOW */
2324 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
2325 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
2326 if (nunits_in == nunits_out / 2)
2327 modifier = NARROW;
2328 else if (nunits_out == nunits_in)
2329 modifier = NONE;
2330 else if (nunits_out == nunits_in / 2)
2331 modifier = WIDEN;
2332 else
2333 return false;
2335 /* For now, we only vectorize functions if a target specific builtin
2336 is available. TODO -- in some cases, it might be profitable to
2337 insert the calls for pieces of the vector, in order to be able
2338 to vectorize other operations in the loop. */
2339 fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
2340 if (fndecl == NULL_TREE)
2342 if (gimple_call_internal_p (stmt)
2343 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
2344 && !slp_node
2345 && loop_vinfo
2346 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2347 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
2348 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2349 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
2351 /* We can handle IFN_GOMP_SIMD_LANE by returning a
2352 { 0, 1, 2, ... vf - 1 } vector. */
2353 gcc_assert (nargs == 0);
2355 else
2357 if (dump_enabled_p ())
2358 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2359 "function is not vectorizable.\n");
2360 return false;
2364 gcc_assert (!gimple_vuse (stmt));
2366 if (slp_node || PURE_SLP_STMT (stmt_info))
2367 ncopies = 1;
2368 else if (modifier == NARROW)
2369 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
2370 else
2371 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
2373 /* Sanity check: make sure that at least one copy of the vectorized stmt
2374 needs to be generated. */
2375 gcc_assert (ncopies >= 1);
2377 if (!vec_stmt) /* transformation not required. */
2379 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
2380 if (dump_enabled_p ())
2381 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
2382 "\n");
2383 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
2384 return true;
2387 /** Transform. **/
2389 if (dump_enabled_p ())
2390 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2392 /* Handle def. */
2393 scalar_dest = gimple_call_lhs (stmt);
2394 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
2396 prev_stmt_info = NULL;
2397 switch (modifier)
2399 case NONE:
2400 for (j = 0; j < ncopies; ++j)
2402 /* Build argument list for the vectorized call. */
2403 if (j == 0)
2404 vargs.create (nargs);
2405 else
2406 vargs.truncate (0);
2408 if (slp_node)
2410 auto_vec<vec<tree> > vec_defs (nargs);
2411 vec<tree> vec_oprnds0;
2413 for (i = 0; i < nargs; i++)
2414 vargs.quick_push (gimple_call_arg (stmt, i));
2415 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2416 vec_oprnds0 = vec_defs[0];
2418 /* Arguments are ready. Create the new vector stmt. */
2419 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
2421 size_t k;
2422 for (k = 0; k < nargs; k++)
2424 vec<tree> vec_oprndsk = vec_defs[k];
2425 vargs[k] = vec_oprndsk[i];
2427 new_stmt = gimple_build_call_vec (fndecl, vargs);
2428 new_temp = make_ssa_name (vec_dest, new_stmt);
2429 gimple_call_set_lhs (new_stmt, new_temp);
2430 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2431 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2434 for (i = 0; i < nargs; i++)
2436 vec<tree> vec_oprndsi = vec_defs[i];
2437 vec_oprndsi.release ();
2439 continue;
2442 for (i = 0; i < nargs; i++)
2444 op = gimple_call_arg (stmt, i);
2445 if (j == 0)
2446 vec_oprnd0
2447 = vect_get_vec_def_for_operand (op, stmt, NULL);
2448 else
2450 vec_oprnd0 = gimple_call_arg (new_stmt, i);
2451 vec_oprnd0
2452 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2455 vargs.quick_push (vec_oprnd0);
2458 if (gimple_call_internal_p (stmt)
2459 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2461 tree *v = XALLOCAVEC (tree, nunits_out);
2462 int k;
2463 for (k = 0; k < nunits_out; ++k)
2464 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
2465 tree cst = build_vector (vectype_out, v);
2466 tree new_var
2467 = vect_get_new_vect_var (vectype_out, vect_simple_var, "cst_");
2468 gimple init_stmt = gimple_build_assign (new_var, cst);
2469 new_temp = make_ssa_name (new_var, init_stmt);
2470 gimple_assign_set_lhs (init_stmt, new_temp);
2471 vect_init_vector_1 (stmt, init_stmt, NULL);
2472 new_temp = make_ssa_name (vec_dest);
2473 new_stmt = gimple_build_assign (new_temp,
2474 gimple_assign_lhs (init_stmt));
2476 else
2478 new_stmt = gimple_build_call_vec (fndecl, vargs);
2479 new_temp = make_ssa_name (vec_dest, new_stmt);
2480 gimple_call_set_lhs (new_stmt, new_temp);
2482 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2484 if (j == 0)
2485 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2486 else
2487 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2489 prev_stmt_info = vinfo_for_stmt (new_stmt);
2492 break;
2494 case NARROW:
2495 for (j = 0; j < ncopies; ++j)
2497 /* Build argument list for the vectorized call. */
2498 if (j == 0)
2499 vargs.create (nargs * 2);
2500 else
2501 vargs.truncate (0);
2503 if (slp_node)
2505 auto_vec<vec<tree> > vec_defs (nargs);
2506 vec<tree> vec_oprnds0;
2508 for (i = 0; i < nargs; i++)
2509 vargs.quick_push (gimple_call_arg (stmt, i));
2510 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2511 vec_oprnds0 = vec_defs[0];
2513 /* Arguments are ready. Create the new vector stmt. */
2514 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2516 size_t k;
2517 vargs.truncate (0);
2518 for (k = 0; k < nargs; k++)
2520 vec<tree> vec_oprndsk = vec_defs[k];
2521 vargs.quick_push (vec_oprndsk[i]);
2522 vargs.quick_push (vec_oprndsk[i + 1]);
2524 new_stmt = gimple_build_call_vec (fndecl, vargs);
2525 new_temp = make_ssa_name (vec_dest, new_stmt);
2526 gimple_call_set_lhs (new_stmt, new_temp);
2527 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2528 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2531 for (i = 0; i < nargs; i++)
2533 vec<tree> vec_oprndsi = vec_defs[i];
2534 vec_oprndsi.release ();
2536 continue;
2539 for (i = 0; i < nargs; i++)
2541 op = gimple_call_arg (stmt, i);
2542 if (j == 0)
2544 vec_oprnd0
2545 = vect_get_vec_def_for_operand (op, stmt, NULL);
2546 vec_oprnd1
2547 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2549 else
2551 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2552 vec_oprnd0
2553 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2554 vec_oprnd1
2555 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2558 vargs.quick_push (vec_oprnd0);
2559 vargs.quick_push (vec_oprnd1);
2562 new_stmt = gimple_build_call_vec (fndecl, vargs);
2563 new_temp = make_ssa_name (vec_dest, new_stmt);
2564 gimple_call_set_lhs (new_stmt, new_temp);
2565 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2567 if (j == 0)
2568 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2569 else
2570 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2572 prev_stmt_info = vinfo_for_stmt (new_stmt);
2575 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2577 break;
2579 case WIDEN:
2580 /* No current target implements this case. */
2581 return false;
2584 vargs.release ();
2586 /* The call in STMT might prevent it from being removed in dce.
2587 We however cannot remove it here, due to the way the ssa name
2588 it defines is mapped to the new definition. So just replace
2589 rhs of the statement with something harmless. */
2591 if (slp_node)
2592 return true;
2594 type = TREE_TYPE (scalar_dest);
2595 if (is_pattern_stmt_p (stmt_info))
2596 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2597 else
2598 lhs = gimple_call_lhs (stmt);
2599 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2600 set_vinfo_for_stmt (new_stmt, stmt_info);
2601 set_vinfo_for_stmt (stmt, NULL);
2602 STMT_VINFO_STMT (stmt_info) = new_stmt;
2603 gsi_replace (gsi, new_stmt, false);
2605 return true;
2609 struct simd_call_arg_info
2611 tree vectype;
2612 tree op;
2613 enum vect_def_type dt;
2614 HOST_WIDE_INT linear_step;
2615 unsigned int align;
2618 /* Function vectorizable_simd_clone_call.
2620 Check if STMT performs a function call that can be vectorized
2621 by calling a simd clone of the function.
2622 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2623 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2624 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2626 static bool
2627 vectorizable_simd_clone_call (gimple stmt, gimple_stmt_iterator *gsi,
2628 gimple *vec_stmt, slp_tree slp_node)
2630 tree vec_dest;
2631 tree scalar_dest;
2632 tree op, type;
2633 tree vec_oprnd0 = NULL_TREE;
2634 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
2635 tree vectype;
2636 unsigned int nunits;
2637 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2638 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2639 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2640 tree fndecl, new_temp, def;
2641 gimple def_stmt;
2642 gimple new_stmt = NULL;
2643 int ncopies, j;
2644 vec<simd_call_arg_info> arginfo = vNULL;
2645 vec<tree> vargs = vNULL;
2646 size_t i, nargs;
2647 tree lhs, rtype, ratype;
2648 vec<constructor_elt, va_gc> *ret_ctor_elts;
2650 /* Is STMT a vectorizable call? */
2651 if (!is_gimple_call (stmt))
2652 return false;
2654 fndecl = gimple_call_fndecl (stmt);
2655 if (fndecl == NULL_TREE)
2656 return false;
2658 struct cgraph_node *node = cgraph_node::get (fndecl);
2659 if (node == NULL || node->simd_clones == NULL)
2660 return false;
2662 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2663 return false;
2665 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2666 return false;
2668 if (gimple_call_lhs (stmt)
2669 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2670 return false;
2672 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2674 vectype = STMT_VINFO_VECTYPE (stmt_info);
2676 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
2677 return false;
2679 /* FORNOW */
2680 if (slp_node || PURE_SLP_STMT (stmt_info))
2681 return false;
2683 /* Process function arguments. */
2684 nargs = gimple_call_num_args (stmt);
2686 /* Bail out if the function has zero arguments. */
2687 if (nargs == 0)
2688 return false;
2690 arginfo.create (nargs);
2692 for (i = 0; i < nargs; i++)
2694 simd_call_arg_info thisarginfo;
2695 affine_iv iv;
2697 thisarginfo.linear_step = 0;
2698 thisarginfo.align = 0;
2699 thisarginfo.op = NULL_TREE;
2701 op = gimple_call_arg (stmt, i);
2702 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2703 &def_stmt, &def, &thisarginfo.dt,
2704 &thisarginfo.vectype)
2705 || thisarginfo.dt == vect_uninitialized_def)
2707 if (dump_enabled_p ())
2708 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2709 "use not simple.\n");
2710 arginfo.release ();
2711 return false;
2714 if (thisarginfo.dt == vect_constant_def
2715 || thisarginfo.dt == vect_external_def)
2716 gcc_assert (thisarginfo.vectype == NULL_TREE);
2717 else
2718 gcc_assert (thisarginfo.vectype != NULL_TREE);
2720 /* For linear arguments, the analyze phase should have saved
2721 the base and step in STMT_VINFO_SIMD_CLONE_INFO. */
2722 if (i * 2 + 3 <= STMT_VINFO_SIMD_CLONE_INFO (stmt_info).length ()
2723 && STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 2 + 2])
2725 gcc_assert (vec_stmt);
2726 thisarginfo.linear_step
2727 = tree_to_shwi (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 2 + 2]);
2728 thisarginfo.op
2729 = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 2 + 1];
2730 /* If loop has been peeled for alignment, we need to adjust it. */
2731 tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
2732 tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
2733 if (n1 != n2)
2735 tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
2736 tree step = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 2 + 2];
2737 tree opt = TREE_TYPE (thisarginfo.op);
2738 bias = fold_convert (TREE_TYPE (step), bias);
2739 bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
2740 thisarginfo.op
2741 = fold_build2 (POINTER_TYPE_P (opt)
2742 ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
2743 thisarginfo.op, bias);
2746 else if (!vec_stmt
2747 && thisarginfo.dt != vect_constant_def
2748 && thisarginfo.dt != vect_external_def
2749 && loop_vinfo
2750 && TREE_CODE (op) == SSA_NAME
2751 && simple_iv (loop, loop_containing_stmt (stmt), op,
2752 &iv, false)
2753 && tree_fits_shwi_p (iv.step))
2755 thisarginfo.linear_step = tree_to_shwi (iv.step);
2756 thisarginfo.op = iv.base;
2758 else if ((thisarginfo.dt == vect_constant_def
2759 || thisarginfo.dt == vect_external_def)
2760 && POINTER_TYPE_P (TREE_TYPE (op)))
2761 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
2763 arginfo.quick_push (thisarginfo);
2766 unsigned int badness = 0;
2767 struct cgraph_node *bestn = NULL;
2768 if (STMT_VINFO_SIMD_CLONE_INFO (stmt_info).exists ())
2769 bestn = cgraph_node::get (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[0]);
2770 else
2771 for (struct cgraph_node *n = node->simd_clones; n != NULL;
2772 n = n->simdclone->next_clone)
2774 unsigned int this_badness = 0;
2775 if (n->simdclone->simdlen
2776 > (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2777 || n->simdclone->nargs != nargs)
2778 continue;
2779 if (n->simdclone->simdlen
2780 < (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2781 this_badness += (exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2782 - exact_log2 (n->simdclone->simdlen)) * 1024;
2783 if (n->simdclone->inbranch)
2784 this_badness += 2048;
2785 int target_badness = targetm.simd_clone.usable (n);
2786 if (target_badness < 0)
2787 continue;
2788 this_badness += target_badness * 512;
2789 /* FORNOW: Have to add code to add the mask argument. */
2790 if (n->simdclone->inbranch)
2791 continue;
2792 for (i = 0; i < nargs; i++)
2794 switch (n->simdclone->args[i].arg_type)
2796 case SIMD_CLONE_ARG_TYPE_VECTOR:
2797 if (!useless_type_conversion_p
2798 (n->simdclone->args[i].orig_type,
2799 TREE_TYPE (gimple_call_arg (stmt, i))))
2800 i = -1;
2801 else if (arginfo[i].dt == vect_constant_def
2802 || arginfo[i].dt == vect_external_def
2803 || arginfo[i].linear_step)
2804 this_badness += 64;
2805 break;
2806 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2807 if (arginfo[i].dt != vect_constant_def
2808 && arginfo[i].dt != vect_external_def)
2809 i = -1;
2810 break;
2811 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2812 if (arginfo[i].dt == vect_constant_def
2813 || arginfo[i].dt == vect_external_def
2814 || (arginfo[i].linear_step
2815 != n->simdclone->args[i].linear_step))
2816 i = -1;
2817 break;
2818 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2819 /* FORNOW */
2820 i = -1;
2821 break;
2822 case SIMD_CLONE_ARG_TYPE_MASK:
2823 gcc_unreachable ();
2825 if (i == (size_t) -1)
2826 break;
2827 if (n->simdclone->args[i].alignment > arginfo[i].align)
2829 i = -1;
2830 break;
2832 if (arginfo[i].align)
2833 this_badness += (exact_log2 (arginfo[i].align)
2834 - exact_log2 (n->simdclone->args[i].alignment));
2836 if (i == (size_t) -1)
2837 continue;
2838 if (bestn == NULL || this_badness < badness)
2840 bestn = n;
2841 badness = this_badness;
2845 if (bestn == NULL)
2847 arginfo.release ();
2848 return false;
2851 for (i = 0; i < nargs; i++)
2852 if ((arginfo[i].dt == vect_constant_def
2853 || arginfo[i].dt == vect_external_def)
2854 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
2856 arginfo[i].vectype
2857 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
2858 i)));
2859 if (arginfo[i].vectype == NULL
2860 || (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2861 > bestn->simdclone->simdlen))
2863 arginfo.release ();
2864 return false;
2868 fndecl = bestn->decl;
2869 nunits = bestn->simdclone->simdlen;
2870 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2872 /* If the function isn't const, only allow it in simd loops where user
2873 has asserted that at least nunits consecutive iterations can be
2874 performed using SIMD instructions. */
2875 if ((loop == NULL || (unsigned) loop->safelen < nunits)
2876 && gimple_vuse (stmt))
2878 arginfo.release ();
2879 return false;
2882 /* Sanity check: make sure that at least one copy of the vectorized stmt
2883 needs to be generated. */
2884 gcc_assert (ncopies >= 1);
2886 if (!vec_stmt) /* transformation not required. */
2888 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (bestn->decl);
2889 for (i = 0; i < nargs; i++)
2890 if (bestn->simdclone->args[i].arg_type
2891 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
2893 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_grow_cleared (i * 2
2894 + 1);
2895 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (arginfo[i].op);
2896 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
2897 ? size_type_node : TREE_TYPE (arginfo[i].op);
2898 tree ls = build_int_cst (lst, arginfo[i].linear_step);
2899 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (ls);
2901 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
2902 if (dump_enabled_p ())
2903 dump_printf_loc (MSG_NOTE, vect_location,
2904 "=== vectorizable_simd_clone_call ===\n");
2905 /* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
2906 arginfo.release ();
2907 return true;
2910 /** Transform. **/
2912 if (dump_enabled_p ())
2913 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2915 /* Handle def. */
2916 scalar_dest = gimple_call_lhs (stmt);
2917 vec_dest = NULL_TREE;
2918 rtype = NULL_TREE;
2919 ratype = NULL_TREE;
2920 if (scalar_dest)
2922 vec_dest = vect_create_destination_var (scalar_dest, vectype);
2923 rtype = TREE_TYPE (TREE_TYPE (fndecl));
2924 if (TREE_CODE (rtype) == ARRAY_TYPE)
2926 ratype = rtype;
2927 rtype = TREE_TYPE (ratype);
2931 prev_stmt_info = NULL;
2932 for (j = 0; j < ncopies; ++j)
2934 /* Build argument list for the vectorized call. */
2935 if (j == 0)
2936 vargs.create (nargs);
2937 else
2938 vargs.truncate (0);
2940 for (i = 0; i < nargs; i++)
2942 unsigned int k, l, m, o;
2943 tree atype;
2944 op = gimple_call_arg (stmt, i);
2945 switch (bestn->simdclone->args[i].arg_type)
2947 case SIMD_CLONE_ARG_TYPE_VECTOR:
2948 atype = bestn->simdclone->args[i].vector_type;
2949 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
2950 for (m = j * o; m < (j + 1) * o; m++)
2952 if (TYPE_VECTOR_SUBPARTS (atype)
2953 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
2955 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
2956 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2957 / TYPE_VECTOR_SUBPARTS (atype));
2958 gcc_assert ((k & (k - 1)) == 0);
2959 if (m == 0)
2960 vec_oprnd0
2961 = vect_get_vec_def_for_operand (op, stmt, NULL);
2962 else
2964 vec_oprnd0 = arginfo[i].op;
2965 if ((m & (k - 1)) == 0)
2966 vec_oprnd0
2967 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2968 vec_oprnd0);
2970 arginfo[i].op = vec_oprnd0;
2971 vec_oprnd0
2972 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
2973 size_int (prec),
2974 bitsize_int ((m & (k - 1)) * prec));
2975 new_stmt
2976 = gimple_build_assign (make_ssa_name (atype),
2977 vec_oprnd0);
2978 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2979 vargs.safe_push (gimple_assign_lhs (new_stmt));
2981 else
2983 k = (TYPE_VECTOR_SUBPARTS (atype)
2984 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
2985 gcc_assert ((k & (k - 1)) == 0);
2986 vec<constructor_elt, va_gc> *ctor_elts;
2987 if (k != 1)
2988 vec_alloc (ctor_elts, k);
2989 else
2990 ctor_elts = NULL;
2991 for (l = 0; l < k; l++)
2993 if (m == 0 && l == 0)
2994 vec_oprnd0
2995 = vect_get_vec_def_for_operand (op, stmt, NULL);
2996 else
2997 vec_oprnd0
2998 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2999 arginfo[i].op);
3000 arginfo[i].op = vec_oprnd0;
3001 if (k == 1)
3002 break;
3003 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
3004 vec_oprnd0);
3006 if (k == 1)
3007 vargs.safe_push (vec_oprnd0);
3008 else
3010 vec_oprnd0 = build_constructor (atype, ctor_elts);
3011 new_stmt
3012 = gimple_build_assign (make_ssa_name (atype),
3013 vec_oprnd0);
3014 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3015 vargs.safe_push (gimple_assign_lhs (new_stmt));
3019 break;
3020 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3021 vargs.safe_push (op);
3022 break;
3023 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
3024 if (j == 0)
3026 gimple_seq stmts;
3027 arginfo[i].op
3028 = force_gimple_operand (arginfo[i].op, &stmts, true,
3029 NULL_TREE);
3030 if (stmts != NULL)
3032 basic_block new_bb;
3033 edge pe = loop_preheader_edge (loop);
3034 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3035 gcc_assert (!new_bb);
3037 tree phi_res = copy_ssa_name (op);
3038 gphi *new_phi = create_phi_node (phi_res, loop->header);
3039 set_vinfo_for_stmt (new_phi,
3040 new_stmt_vec_info (new_phi, loop_vinfo,
3041 NULL));
3042 add_phi_arg (new_phi, arginfo[i].op,
3043 loop_preheader_edge (loop), UNKNOWN_LOCATION);
3044 enum tree_code code
3045 = POINTER_TYPE_P (TREE_TYPE (op))
3046 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3047 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3048 ? sizetype : TREE_TYPE (op);
3049 widest_int cst
3050 = wi::mul (bestn->simdclone->args[i].linear_step,
3051 ncopies * nunits);
3052 tree tcst = wide_int_to_tree (type, cst);
3053 tree phi_arg = copy_ssa_name (op);
3054 new_stmt
3055 = gimple_build_assign (phi_arg, code, phi_res, tcst);
3056 gimple_stmt_iterator si = gsi_after_labels (loop->header);
3057 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
3058 set_vinfo_for_stmt (new_stmt,
3059 new_stmt_vec_info (new_stmt, loop_vinfo,
3060 NULL));
3061 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
3062 UNKNOWN_LOCATION);
3063 arginfo[i].op = phi_res;
3064 vargs.safe_push (phi_res);
3066 else
3068 enum tree_code code
3069 = POINTER_TYPE_P (TREE_TYPE (op))
3070 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3071 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3072 ? sizetype : TREE_TYPE (op);
3073 widest_int cst
3074 = wi::mul (bestn->simdclone->args[i].linear_step,
3075 j * nunits);
3076 tree tcst = wide_int_to_tree (type, cst);
3077 new_temp = make_ssa_name (TREE_TYPE (op));
3078 new_stmt = gimple_build_assign (new_temp, code,
3079 arginfo[i].op, tcst);
3080 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3081 vargs.safe_push (new_temp);
3083 break;
3084 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
3085 default:
3086 gcc_unreachable ();
3090 new_stmt = gimple_build_call_vec (fndecl, vargs);
3091 if (vec_dest)
3093 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
3094 if (ratype)
3095 new_temp = create_tmp_var (ratype);
3096 else if (TYPE_VECTOR_SUBPARTS (vectype)
3097 == TYPE_VECTOR_SUBPARTS (rtype))
3098 new_temp = make_ssa_name (vec_dest, new_stmt);
3099 else
3100 new_temp = make_ssa_name (rtype, new_stmt);
3101 gimple_call_set_lhs (new_stmt, new_temp);
3103 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3105 if (vec_dest)
3107 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
3109 unsigned int k, l;
3110 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
3111 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
3112 gcc_assert ((k & (k - 1)) == 0);
3113 for (l = 0; l < k; l++)
3115 tree t;
3116 if (ratype)
3118 t = build_fold_addr_expr (new_temp);
3119 t = build2 (MEM_REF, vectype, t,
3120 build_int_cst (TREE_TYPE (t),
3121 l * prec / BITS_PER_UNIT));
3123 else
3124 t = build3 (BIT_FIELD_REF, vectype, new_temp,
3125 size_int (prec), bitsize_int (l * prec));
3126 new_stmt
3127 = gimple_build_assign (make_ssa_name (vectype), t);
3128 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3129 if (j == 0 && l == 0)
3130 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3131 else
3132 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3134 prev_stmt_info = vinfo_for_stmt (new_stmt);
3137 if (ratype)
3139 tree clobber = build_constructor (ratype, NULL);
3140 TREE_THIS_VOLATILE (clobber) = 1;
3141 new_stmt = gimple_build_assign (new_temp, clobber);
3142 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3144 continue;
3146 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
3148 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
3149 / TYPE_VECTOR_SUBPARTS (rtype));
3150 gcc_assert ((k & (k - 1)) == 0);
3151 if ((j & (k - 1)) == 0)
3152 vec_alloc (ret_ctor_elts, k);
3153 if (ratype)
3155 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
3156 for (m = 0; m < o; m++)
3158 tree tem = build4 (ARRAY_REF, rtype, new_temp,
3159 size_int (m), NULL_TREE, NULL_TREE);
3160 new_stmt
3161 = gimple_build_assign (make_ssa_name (rtype), tem);
3162 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3163 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
3164 gimple_assign_lhs (new_stmt));
3166 tree clobber = build_constructor (ratype, NULL);
3167 TREE_THIS_VOLATILE (clobber) = 1;
3168 new_stmt = gimple_build_assign (new_temp, clobber);
3169 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3171 else
3172 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
3173 if ((j & (k - 1)) != k - 1)
3174 continue;
3175 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
3176 new_stmt
3177 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
3178 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3180 if ((unsigned) j == k - 1)
3181 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3182 else
3183 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3185 prev_stmt_info = vinfo_for_stmt (new_stmt);
3186 continue;
3188 else if (ratype)
3190 tree t = build_fold_addr_expr (new_temp);
3191 t = build2 (MEM_REF, vectype, t,
3192 build_int_cst (TREE_TYPE (t), 0));
3193 new_stmt
3194 = gimple_build_assign (make_ssa_name (vec_dest), t);
3195 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3196 tree clobber = build_constructor (ratype, NULL);
3197 TREE_THIS_VOLATILE (clobber) = 1;
3198 vect_finish_stmt_generation (stmt,
3199 gimple_build_assign (new_temp,
3200 clobber), gsi);
3204 if (j == 0)
3205 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3206 else
3207 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3209 prev_stmt_info = vinfo_for_stmt (new_stmt);
3212 vargs.release ();
3214 /* The call in STMT might prevent it from being removed in dce.
3215 We however cannot remove it here, due to the way the ssa name
3216 it defines is mapped to the new definition. So just replace
3217 rhs of the statement with something harmless. */
3219 if (slp_node)
3220 return true;
3222 if (scalar_dest)
3224 type = TREE_TYPE (scalar_dest);
3225 if (is_pattern_stmt_p (stmt_info))
3226 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
3227 else
3228 lhs = gimple_call_lhs (stmt);
3229 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
3231 else
3232 new_stmt = gimple_build_nop ();
3233 set_vinfo_for_stmt (new_stmt, stmt_info);
3234 set_vinfo_for_stmt (stmt, NULL);
3235 STMT_VINFO_STMT (stmt_info) = new_stmt;
3236 gsi_replace (gsi, new_stmt, true);
3237 unlink_stmt_vdef (stmt);
3239 return true;
3243 /* Function vect_gen_widened_results_half
3245 Create a vector stmt whose code, type, number of arguments, and result
3246 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
3247 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
3248 In the case that CODE is a CALL_EXPR, this means that a call to DECL
3249 needs to be created (DECL is a function-decl of a target-builtin).
3250 STMT is the original scalar stmt that we are vectorizing. */
3252 static gimple
3253 vect_gen_widened_results_half (enum tree_code code,
3254 tree decl,
3255 tree vec_oprnd0, tree vec_oprnd1, int op_type,
3256 tree vec_dest, gimple_stmt_iterator *gsi,
3257 gimple stmt)
3259 gimple new_stmt;
3260 tree new_temp;
3262 /* Generate half of the widened result: */
3263 if (code == CALL_EXPR)
3265 /* Target specific support */
3266 if (op_type == binary_op)
3267 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3268 else
3269 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3270 new_temp = make_ssa_name (vec_dest, new_stmt);
3271 gimple_call_set_lhs (new_stmt, new_temp);
3273 else
3275 /* Generic support */
3276 gcc_assert (op_type == TREE_CODE_LENGTH (code));
3277 if (op_type != binary_op)
3278 vec_oprnd1 = NULL;
3279 new_stmt = gimple_build_assign (vec_dest, code, vec_oprnd0, vec_oprnd1);
3280 new_temp = make_ssa_name (vec_dest, new_stmt);
3281 gimple_assign_set_lhs (new_stmt, new_temp);
3283 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3285 return new_stmt;
3289 /* Get vectorized definitions for loop-based vectorization. For the first
3290 operand we call vect_get_vec_def_for_operand() (with OPRND containing
3291 scalar operand), and for the rest we get a copy with
3292 vect_get_vec_def_for_stmt_copy() using the previous vector definition
3293 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
3294 The vectors are collected into VEC_OPRNDS. */
3296 static void
3297 vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
3298 vec<tree> *vec_oprnds, int multi_step_cvt)
3300 tree vec_oprnd;
3302 /* Get first vector operand. */
3303 /* All the vector operands except the very first one (that is scalar oprnd)
3304 are stmt copies. */
3305 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
3306 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
3307 else
3308 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
3310 vec_oprnds->quick_push (vec_oprnd);
3312 /* Get second vector operand. */
3313 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
3314 vec_oprnds->quick_push (vec_oprnd);
3316 *oprnd = vec_oprnd;
3318 /* For conversion in multiple steps, continue to get operands
3319 recursively. */
3320 if (multi_step_cvt)
3321 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
3325 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
3326 For multi-step conversions store the resulting vectors and call the function
3327 recursively. */
3329 static void
3330 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
3331 int multi_step_cvt, gimple stmt,
3332 vec<tree> vec_dsts,
3333 gimple_stmt_iterator *gsi,
3334 slp_tree slp_node, enum tree_code code,
3335 stmt_vec_info *prev_stmt_info)
3337 unsigned int i;
3338 tree vop0, vop1, new_tmp, vec_dest;
3339 gimple new_stmt;
3340 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3342 vec_dest = vec_dsts.pop ();
3344 for (i = 0; i < vec_oprnds->length (); i += 2)
3346 /* Create demotion operation. */
3347 vop0 = (*vec_oprnds)[i];
3348 vop1 = (*vec_oprnds)[i + 1];
3349 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
3350 new_tmp = make_ssa_name (vec_dest, new_stmt);
3351 gimple_assign_set_lhs (new_stmt, new_tmp);
3352 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3354 if (multi_step_cvt)
3355 /* Store the resulting vector for next recursive call. */
3356 (*vec_oprnds)[i/2] = new_tmp;
3357 else
3359 /* This is the last step of the conversion sequence. Store the
3360 vectors in SLP_NODE or in vector info of the scalar statement
3361 (or in STMT_VINFO_RELATED_STMT chain). */
3362 if (slp_node)
3363 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3364 else
3366 if (!*prev_stmt_info)
3367 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3368 else
3369 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
3371 *prev_stmt_info = vinfo_for_stmt (new_stmt);
3376 /* For multi-step demotion operations we first generate demotion operations
3377 from the source type to the intermediate types, and then combine the
3378 results (stored in VEC_OPRNDS) in demotion operation to the destination
3379 type. */
3380 if (multi_step_cvt)
3382 /* At each level of recursion we have half of the operands we had at the
3383 previous level. */
3384 vec_oprnds->truncate ((i+1)/2);
3385 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
3386 stmt, vec_dsts, gsi, slp_node,
3387 VEC_PACK_TRUNC_EXPR,
3388 prev_stmt_info);
3391 vec_dsts.quick_push (vec_dest);
3395 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
3396 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
3397 the resulting vectors and call the function recursively. */
3399 static void
3400 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
3401 vec<tree> *vec_oprnds1,
3402 gimple stmt, tree vec_dest,
3403 gimple_stmt_iterator *gsi,
3404 enum tree_code code1,
3405 enum tree_code code2, tree decl1,
3406 tree decl2, int op_type)
3408 int i;
3409 tree vop0, vop1, new_tmp1, new_tmp2;
3410 gimple new_stmt1, new_stmt2;
3411 vec<tree> vec_tmp = vNULL;
3413 vec_tmp.create (vec_oprnds0->length () * 2);
3414 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
3416 if (op_type == binary_op)
3417 vop1 = (*vec_oprnds1)[i];
3418 else
3419 vop1 = NULL_TREE;
3421 /* Generate the two halves of promotion operation. */
3422 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
3423 op_type, vec_dest, gsi, stmt);
3424 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
3425 op_type, vec_dest, gsi, stmt);
3426 if (is_gimple_call (new_stmt1))
3428 new_tmp1 = gimple_call_lhs (new_stmt1);
3429 new_tmp2 = gimple_call_lhs (new_stmt2);
3431 else
3433 new_tmp1 = gimple_assign_lhs (new_stmt1);
3434 new_tmp2 = gimple_assign_lhs (new_stmt2);
3437 /* Store the results for the next step. */
3438 vec_tmp.quick_push (new_tmp1);
3439 vec_tmp.quick_push (new_tmp2);
3442 vec_oprnds0->release ();
3443 *vec_oprnds0 = vec_tmp;
3447 /* Check if STMT performs a conversion operation, that can be vectorized.
3448 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3449 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3450 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3452 static bool
3453 vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
3454 gimple *vec_stmt, slp_tree slp_node)
3456 tree vec_dest;
3457 tree scalar_dest;
3458 tree op0, op1 = NULL_TREE;
3459 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3460 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3461 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3462 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
3463 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
3464 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3465 tree new_temp;
3466 tree def;
3467 gimple def_stmt;
3468 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3469 gimple new_stmt = NULL;
3470 stmt_vec_info prev_stmt_info;
3471 int nunits_in;
3472 int nunits_out;
3473 tree vectype_out, vectype_in;
3474 int ncopies, i, j;
3475 tree lhs_type, rhs_type;
3476 enum { NARROW, NONE, WIDEN } modifier;
3477 vec<tree> vec_oprnds0 = vNULL;
3478 vec<tree> vec_oprnds1 = vNULL;
3479 tree vop0;
3480 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3481 int multi_step_cvt = 0;
3482 vec<tree> vec_dsts = vNULL;
3483 vec<tree> interm_types = vNULL;
3484 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
3485 int op_type;
3486 machine_mode rhs_mode;
3487 unsigned short fltsz;
3489 /* Is STMT a vectorizable conversion? */
3491 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3492 return false;
3494 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3495 return false;
3497 if (!is_gimple_assign (stmt))
3498 return false;
3500 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3501 return false;
3503 code = gimple_assign_rhs_code (stmt);
3504 if (!CONVERT_EXPR_CODE_P (code)
3505 && code != FIX_TRUNC_EXPR
3506 && code != FLOAT_EXPR
3507 && code != WIDEN_MULT_EXPR
3508 && code != WIDEN_LSHIFT_EXPR)
3509 return false;
3511 op_type = TREE_CODE_LENGTH (code);
3513 /* Check types of lhs and rhs. */
3514 scalar_dest = gimple_assign_lhs (stmt);
3515 lhs_type = TREE_TYPE (scalar_dest);
3516 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3518 op0 = gimple_assign_rhs1 (stmt);
3519 rhs_type = TREE_TYPE (op0);
3521 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3522 && !((INTEGRAL_TYPE_P (lhs_type)
3523 && INTEGRAL_TYPE_P (rhs_type))
3524 || (SCALAR_FLOAT_TYPE_P (lhs_type)
3525 && SCALAR_FLOAT_TYPE_P (rhs_type))))
3526 return false;
3528 if ((INTEGRAL_TYPE_P (lhs_type)
3529 && (TYPE_PRECISION (lhs_type)
3530 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
3531 || (INTEGRAL_TYPE_P (rhs_type)
3532 && (TYPE_PRECISION (rhs_type)
3533 != GET_MODE_PRECISION (TYPE_MODE (rhs_type)))))
3535 if (dump_enabled_p ())
3536 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3537 "type conversion to/from bit-precision unsupported."
3538 "\n");
3539 return false;
3542 /* Check the operands of the operation. */
3543 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3544 &def_stmt, &def, &dt[0], &vectype_in))
3546 if (dump_enabled_p ())
3547 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3548 "use not simple.\n");
3549 return false;
3551 if (op_type == binary_op)
3553 bool ok;
3555 op1 = gimple_assign_rhs2 (stmt);
3556 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3557 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3558 OP1. */
3559 if (CONSTANT_CLASS_P (op0))
3560 ok = vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo,
3561 &def_stmt, &def, &dt[1], &vectype_in);
3562 else
3563 ok = vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3564 &def, &dt[1]);
3566 if (!ok)
3568 if (dump_enabled_p ())
3569 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3570 "use not simple.\n");
3571 return false;
3575 /* If op0 is an external or constant defs use a vector type of
3576 the same size as the output vector type. */
3577 if (!vectype_in)
3578 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
3579 if (vec_stmt)
3580 gcc_assert (vectype_in);
3581 if (!vectype_in)
3583 if (dump_enabled_p ())
3585 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3586 "no vectype for scalar type ");
3587 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3588 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3591 return false;
3594 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3595 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3596 if (nunits_in < nunits_out)
3597 modifier = NARROW;
3598 else if (nunits_out == nunits_in)
3599 modifier = NONE;
3600 else
3601 modifier = WIDEN;
3603 /* Multiple types in SLP are handled by creating the appropriate number of
3604 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3605 case of SLP. */
3606 if (slp_node || PURE_SLP_STMT (stmt_info))
3607 ncopies = 1;
3608 else if (modifier == NARROW)
3609 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3610 else
3611 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3613 /* Sanity check: make sure that at least one copy of the vectorized stmt
3614 needs to be generated. */
3615 gcc_assert (ncopies >= 1);
3617 /* Supportable by target? */
3618 switch (modifier)
3620 case NONE:
3621 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3622 return false;
3623 if (supportable_convert_operation (code, vectype_out, vectype_in,
3624 &decl1, &code1))
3625 break;
3626 /* FALLTHRU */
3627 unsupported:
3628 if (dump_enabled_p ())
3629 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3630 "conversion not supported by target.\n");
3631 return false;
3633 case WIDEN:
3634 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
3635 &code1, &code2, &multi_step_cvt,
3636 &interm_types))
3638 /* Binary widening operation can only be supported directly by the
3639 architecture. */
3640 gcc_assert (!(multi_step_cvt && op_type == binary_op));
3641 break;
3644 if (code != FLOAT_EXPR
3645 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3646 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3647 goto unsupported;
3649 rhs_mode = TYPE_MODE (rhs_type);
3650 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
3651 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
3652 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
3653 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
3655 cvt_type
3656 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3657 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3658 if (cvt_type == NULL_TREE)
3659 goto unsupported;
3661 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3663 if (!supportable_convert_operation (code, vectype_out,
3664 cvt_type, &decl1, &codecvt1))
3665 goto unsupported;
3667 else if (!supportable_widening_operation (code, stmt, vectype_out,
3668 cvt_type, &codecvt1,
3669 &codecvt2, &multi_step_cvt,
3670 &interm_types))
3671 continue;
3672 else
3673 gcc_assert (multi_step_cvt == 0);
3675 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
3676 vectype_in, &code1, &code2,
3677 &multi_step_cvt, &interm_types))
3678 break;
3681 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
3682 goto unsupported;
3684 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3685 codecvt2 = ERROR_MARK;
3686 else
3688 multi_step_cvt++;
3689 interm_types.safe_push (cvt_type);
3690 cvt_type = NULL_TREE;
3692 break;
3694 case NARROW:
3695 gcc_assert (op_type == unary_op);
3696 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
3697 &code1, &multi_step_cvt,
3698 &interm_types))
3699 break;
3701 if (code != FIX_TRUNC_EXPR
3702 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3703 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3704 goto unsupported;
3706 rhs_mode = TYPE_MODE (rhs_type);
3707 cvt_type
3708 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3709 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3710 if (cvt_type == NULL_TREE)
3711 goto unsupported;
3712 if (!supportable_convert_operation (code, cvt_type, vectype_in,
3713 &decl1, &codecvt1))
3714 goto unsupported;
3715 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
3716 &code1, &multi_step_cvt,
3717 &interm_types))
3718 break;
3719 goto unsupported;
3721 default:
3722 gcc_unreachable ();
3725 if (!vec_stmt) /* transformation not required. */
3727 if (dump_enabled_p ())
3728 dump_printf_loc (MSG_NOTE, vect_location,
3729 "=== vectorizable_conversion ===\n");
3730 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
3732 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
3733 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3735 else if (modifier == NARROW)
3737 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
3738 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3740 else
3742 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
3743 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3745 interm_types.release ();
3746 return true;
3749 /** Transform. **/
3750 if (dump_enabled_p ())
3751 dump_printf_loc (MSG_NOTE, vect_location,
3752 "transform conversion. ncopies = %d.\n", ncopies);
3754 if (op_type == binary_op)
3756 if (CONSTANT_CLASS_P (op0))
3757 op0 = fold_convert (TREE_TYPE (op1), op0);
3758 else if (CONSTANT_CLASS_P (op1))
3759 op1 = fold_convert (TREE_TYPE (op0), op1);
3762 /* In case of multi-step conversion, we first generate conversion operations
3763 to the intermediate types, and then from that types to the final one.
3764 We create vector destinations for the intermediate type (TYPES) received
3765 from supportable_*_operation, and store them in the correct order
3766 for future use in vect_create_vectorized_*_stmts (). */
3767 vec_dsts.create (multi_step_cvt + 1);
3768 vec_dest = vect_create_destination_var (scalar_dest,
3769 (cvt_type && modifier == WIDEN)
3770 ? cvt_type : vectype_out);
3771 vec_dsts.quick_push (vec_dest);
3773 if (multi_step_cvt)
3775 for (i = interm_types.length () - 1;
3776 interm_types.iterate (i, &intermediate_type); i--)
3778 vec_dest = vect_create_destination_var (scalar_dest,
3779 intermediate_type);
3780 vec_dsts.quick_push (vec_dest);
3784 if (cvt_type)
3785 vec_dest = vect_create_destination_var (scalar_dest,
3786 modifier == WIDEN
3787 ? vectype_out : cvt_type);
3789 if (!slp_node)
3791 if (modifier == WIDEN)
3793 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
3794 if (op_type == binary_op)
3795 vec_oprnds1.create (1);
3797 else if (modifier == NARROW)
3798 vec_oprnds0.create (
3799 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
3801 else if (code == WIDEN_LSHIFT_EXPR)
3802 vec_oprnds1.create (slp_node->vec_stmts_size);
3804 last_oprnd = op0;
3805 prev_stmt_info = NULL;
3806 switch (modifier)
3808 case NONE:
3809 for (j = 0; j < ncopies; j++)
3811 if (j == 0)
3812 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
3813 -1);
3814 else
3815 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3817 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3819 /* Arguments are ready, create the new vector stmt. */
3820 if (code1 == CALL_EXPR)
3822 new_stmt = gimple_build_call (decl1, 1, vop0);
3823 new_temp = make_ssa_name (vec_dest, new_stmt);
3824 gimple_call_set_lhs (new_stmt, new_temp);
3826 else
3828 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
3829 new_stmt = gimple_build_assign (vec_dest, code1, vop0);
3830 new_temp = make_ssa_name (vec_dest, new_stmt);
3831 gimple_assign_set_lhs (new_stmt, new_temp);
3834 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3835 if (slp_node)
3836 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3839 if (j == 0)
3840 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3841 else
3842 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3843 prev_stmt_info = vinfo_for_stmt (new_stmt);
3845 break;
3847 case WIDEN:
3848 /* In case the vectorization factor (VF) is bigger than the number
3849 of elements that we can fit in a vectype (nunits), we have to
3850 generate more than one vector stmt - i.e - we need to "unroll"
3851 the vector stmt by a factor VF/nunits. */
3852 for (j = 0; j < ncopies; j++)
3854 /* Handle uses. */
3855 if (j == 0)
3857 if (slp_node)
3859 if (code == WIDEN_LSHIFT_EXPR)
3861 unsigned int k;
3863 vec_oprnd1 = op1;
3864 /* Store vec_oprnd1 for every vector stmt to be created
3865 for SLP_NODE. We check during the analysis that all
3866 the shift arguments are the same. */
3867 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
3868 vec_oprnds1.quick_push (vec_oprnd1);
3870 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3871 slp_node, -1);
3873 else
3874 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
3875 &vec_oprnds1, slp_node, -1);
3877 else
3879 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
3880 vec_oprnds0.quick_push (vec_oprnd0);
3881 if (op_type == binary_op)
3883 if (code == WIDEN_LSHIFT_EXPR)
3884 vec_oprnd1 = op1;
3885 else
3886 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt,
3887 NULL);
3888 vec_oprnds1.quick_push (vec_oprnd1);
3892 else
3894 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3895 vec_oprnds0.truncate (0);
3896 vec_oprnds0.quick_push (vec_oprnd0);
3897 if (op_type == binary_op)
3899 if (code == WIDEN_LSHIFT_EXPR)
3900 vec_oprnd1 = op1;
3901 else
3902 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
3903 vec_oprnd1);
3904 vec_oprnds1.truncate (0);
3905 vec_oprnds1.quick_push (vec_oprnd1);
3909 /* Arguments are ready. Create the new vector stmts. */
3910 for (i = multi_step_cvt; i >= 0; i--)
3912 tree this_dest = vec_dsts[i];
3913 enum tree_code c1 = code1, c2 = code2;
3914 if (i == 0 && codecvt2 != ERROR_MARK)
3916 c1 = codecvt1;
3917 c2 = codecvt2;
3919 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
3920 &vec_oprnds1,
3921 stmt, this_dest, gsi,
3922 c1, c2, decl1, decl2,
3923 op_type);
3926 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3928 if (cvt_type)
3930 if (codecvt1 == CALL_EXPR)
3932 new_stmt = gimple_build_call (decl1, 1, vop0);
3933 new_temp = make_ssa_name (vec_dest, new_stmt);
3934 gimple_call_set_lhs (new_stmt, new_temp);
3936 else
3938 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3939 new_temp = make_ssa_name (vec_dest);
3940 new_stmt = gimple_build_assign (new_temp, codecvt1,
3941 vop0);
3944 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3946 else
3947 new_stmt = SSA_NAME_DEF_STMT (vop0);
3949 if (slp_node)
3950 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3951 else
3953 if (!prev_stmt_info)
3954 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3955 else
3956 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3957 prev_stmt_info = vinfo_for_stmt (new_stmt);
3962 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3963 break;
3965 case NARROW:
3966 /* In case the vectorization factor (VF) is bigger than the number
3967 of elements that we can fit in a vectype (nunits), we have to
3968 generate more than one vector stmt - i.e - we need to "unroll"
3969 the vector stmt by a factor VF/nunits. */
3970 for (j = 0; j < ncopies; j++)
3972 /* Handle uses. */
3973 if (slp_node)
3974 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3975 slp_node, -1);
3976 else
3978 vec_oprnds0.truncate (0);
3979 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
3980 vect_pow2 (multi_step_cvt) - 1);
3983 /* Arguments are ready. Create the new vector stmts. */
3984 if (cvt_type)
3985 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3987 if (codecvt1 == CALL_EXPR)
3989 new_stmt = gimple_build_call (decl1, 1, vop0);
3990 new_temp = make_ssa_name (vec_dest, new_stmt);
3991 gimple_call_set_lhs (new_stmt, new_temp);
3993 else
3995 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3996 new_temp = make_ssa_name (vec_dest);
3997 new_stmt = gimple_build_assign (new_temp, codecvt1,
3998 vop0);
4001 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4002 vec_oprnds0[i] = new_temp;
4005 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
4006 stmt, vec_dsts, gsi,
4007 slp_node, code1,
4008 &prev_stmt_info);
4011 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4012 break;
4015 vec_oprnds0.release ();
4016 vec_oprnds1.release ();
4017 vec_dsts.release ();
4018 interm_types.release ();
4020 return true;
4024 /* Function vectorizable_assignment.
4026 Check if STMT performs an assignment (copy) that can be vectorized.
4027 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4028 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4029 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4031 static bool
4032 vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
4033 gimple *vec_stmt, slp_tree slp_node)
4035 tree vec_dest;
4036 tree scalar_dest;
4037 tree op;
4038 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4039 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4040 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4041 tree new_temp;
4042 tree def;
4043 gimple def_stmt;
4044 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4045 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4046 int ncopies;
4047 int i, j;
4048 vec<tree> vec_oprnds = vNULL;
4049 tree vop;
4050 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4051 gimple new_stmt = NULL;
4052 stmt_vec_info prev_stmt_info = NULL;
4053 enum tree_code code;
4054 tree vectype_in;
4056 /* Multiple types in SLP are handled by creating the appropriate number of
4057 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4058 case of SLP. */
4059 if (slp_node || PURE_SLP_STMT (stmt_info))
4060 ncopies = 1;
4061 else
4062 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4064 gcc_assert (ncopies >= 1);
4066 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4067 return false;
4069 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4070 return false;
4072 /* Is vectorizable assignment? */
4073 if (!is_gimple_assign (stmt))
4074 return false;
4076 scalar_dest = gimple_assign_lhs (stmt);
4077 if (TREE_CODE (scalar_dest) != SSA_NAME)
4078 return false;
4080 code = gimple_assign_rhs_code (stmt);
4081 if (gimple_assign_single_p (stmt)
4082 || code == PAREN_EXPR
4083 || CONVERT_EXPR_CODE_P (code))
4084 op = gimple_assign_rhs1 (stmt);
4085 else
4086 return false;
4088 if (code == VIEW_CONVERT_EXPR)
4089 op = TREE_OPERAND (op, 0);
4091 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
4092 &def_stmt, &def, &dt[0], &vectype_in))
4094 if (dump_enabled_p ())
4095 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4096 "use not simple.\n");
4097 return false;
4100 /* We can handle NOP_EXPR conversions that do not change the number
4101 of elements or the vector size. */
4102 if ((CONVERT_EXPR_CODE_P (code)
4103 || code == VIEW_CONVERT_EXPR)
4104 && (!vectype_in
4105 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
4106 || (GET_MODE_SIZE (TYPE_MODE (vectype))
4107 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
4108 return false;
4110 /* We do not handle bit-precision changes. */
4111 if ((CONVERT_EXPR_CODE_P (code)
4112 || code == VIEW_CONVERT_EXPR)
4113 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4114 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4115 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4116 || ((TYPE_PRECISION (TREE_TYPE (op))
4117 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
4118 /* But a conversion that does not change the bit-pattern is ok. */
4119 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4120 > TYPE_PRECISION (TREE_TYPE (op)))
4121 && TYPE_UNSIGNED (TREE_TYPE (op))))
4123 if (dump_enabled_p ())
4124 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4125 "type conversion to/from bit-precision "
4126 "unsupported.\n");
4127 return false;
4130 if (!vec_stmt) /* transformation not required. */
4132 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
4133 if (dump_enabled_p ())
4134 dump_printf_loc (MSG_NOTE, vect_location,
4135 "=== vectorizable_assignment ===\n");
4136 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4137 return true;
4140 /** Transform. **/
4141 if (dump_enabled_p ())
4142 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
4144 /* Handle def. */
4145 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4147 /* Handle use. */
4148 for (j = 0; j < ncopies; j++)
4150 /* Handle uses. */
4151 if (j == 0)
4152 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
4153 else
4154 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
4156 /* Arguments are ready. create the new vector stmt. */
4157 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
4159 if (CONVERT_EXPR_CODE_P (code)
4160 || code == VIEW_CONVERT_EXPR)
4161 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
4162 new_stmt = gimple_build_assign (vec_dest, vop);
4163 new_temp = make_ssa_name (vec_dest, new_stmt);
4164 gimple_assign_set_lhs (new_stmt, new_temp);
4165 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4166 if (slp_node)
4167 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4170 if (slp_node)
4171 continue;
4173 if (j == 0)
4174 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4175 else
4176 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4178 prev_stmt_info = vinfo_for_stmt (new_stmt);
4181 vec_oprnds.release ();
4182 return true;
4186 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
4187 either as shift by a scalar or by a vector. */
4189 bool
4190 vect_supportable_shift (enum tree_code code, tree scalar_type)
4193 machine_mode vec_mode;
4194 optab optab;
4195 int icode;
4196 tree vectype;
4198 vectype = get_vectype_for_scalar_type (scalar_type);
4199 if (!vectype)
4200 return false;
4202 optab = optab_for_tree_code (code, vectype, optab_scalar);
4203 if (!optab
4204 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
4206 optab = optab_for_tree_code (code, vectype, optab_vector);
4207 if (!optab
4208 || (optab_handler (optab, TYPE_MODE (vectype))
4209 == CODE_FOR_nothing))
4210 return false;
4213 vec_mode = TYPE_MODE (vectype);
4214 icode = (int) optab_handler (optab, vec_mode);
4215 if (icode == CODE_FOR_nothing)
4216 return false;
4218 return true;
4222 /* Function vectorizable_shift.
4224 Check if STMT performs a shift operation that can be vectorized.
4225 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4226 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4227 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4229 static bool
4230 vectorizable_shift (gimple stmt, gimple_stmt_iterator *gsi,
4231 gimple *vec_stmt, slp_tree slp_node)
4233 tree vec_dest;
4234 tree scalar_dest;
4235 tree op0, op1 = NULL;
4236 tree vec_oprnd1 = NULL_TREE;
4237 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4238 tree vectype;
4239 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4240 enum tree_code code;
4241 machine_mode vec_mode;
4242 tree new_temp;
4243 optab optab;
4244 int icode;
4245 machine_mode optab_op2_mode;
4246 tree def;
4247 gimple def_stmt;
4248 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4249 gimple new_stmt = NULL;
4250 stmt_vec_info prev_stmt_info;
4251 int nunits_in;
4252 int nunits_out;
4253 tree vectype_out;
4254 tree op1_vectype;
4255 int ncopies;
4256 int j, i;
4257 vec<tree> vec_oprnds0 = vNULL;
4258 vec<tree> vec_oprnds1 = vNULL;
4259 tree vop0, vop1;
4260 unsigned int k;
4261 bool scalar_shift_arg = true;
4262 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4263 int vf;
4265 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4266 return false;
4268 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4269 return false;
4271 /* Is STMT a vectorizable binary/unary operation? */
4272 if (!is_gimple_assign (stmt))
4273 return false;
4275 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4276 return false;
4278 code = gimple_assign_rhs_code (stmt);
4280 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4281 || code == RROTATE_EXPR))
4282 return false;
4284 scalar_dest = gimple_assign_lhs (stmt);
4285 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4286 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4287 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4289 if (dump_enabled_p ())
4290 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4291 "bit-precision shifts not supported.\n");
4292 return false;
4295 op0 = gimple_assign_rhs1 (stmt);
4296 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
4297 &def_stmt, &def, &dt[0], &vectype))
4299 if (dump_enabled_p ())
4300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4301 "use not simple.\n");
4302 return false;
4304 /* If op0 is an external or constant def use a vector type with
4305 the same size as the output vector type. */
4306 if (!vectype)
4307 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4308 if (vec_stmt)
4309 gcc_assert (vectype);
4310 if (!vectype)
4312 if (dump_enabled_p ())
4313 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4314 "no vectype for scalar type\n");
4315 return false;
4318 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4319 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4320 if (nunits_out != nunits_in)
4321 return false;
4323 op1 = gimple_assign_rhs2 (stmt);
4324 if (!vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4325 &def, &dt[1], &op1_vectype))
4327 if (dump_enabled_p ())
4328 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4329 "use not simple.\n");
4330 return false;
4333 if (loop_vinfo)
4334 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4335 else
4336 vf = 1;
4338 /* Multiple types in SLP are handled by creating the appropriate number of
4339 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4340 case of SLP. */
4341 if (slp_node || PURE_SLP_STMT (stmt_info))
4342 ncopies = 1;
4343 else
4344 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4346 gcc_assert (ncopies >= 1);
4348 /* Determine whether the shift amount is a vector, or scalar. If the
4349 shift/rotate amount is a vector, use the vector/vector shift optabs. */
4351 if (dt[1] == vect_internal_def && !slp_node)
4352 scalar_shift_arg = false;
4353 else if (dt[1] == vect_constant_def
4354 || dt[1] == vect_external_def
4355 || dt[1] == vect_internal_def)
4357 /* In SLP, need to check whether the shift count is the same,
4358 in loops if it is a constant or invariant, it is always
4359 a scalar shift. */
4360 if (slp_node)
4362 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4363 gimple slpstmt;
4365 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
4366 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
4367 scalar_shift_arg = false;
4370 else
4372 if (dump_enabled_p ())
4373 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4374 "operand mode requires invariant argument.\n");
4375 return false;
4378 /* Vector shifted by vector. */
4379 if (!scalar_shift_arg)
4381 optab = optab_for_tree_code (code, vectype, optab_vector);
4382 if (dump_enabled_p ())
4383 dump_printf_loc (MSG_NOTE, vect_location,
4384 "vector/vector shift/rotate found.\n");
4386 if (!op1_vectype)
4387 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
4388 if (op1_vectype == NULL_TREE
4389 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
4391 if (dump_enabled_p ())
4392 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4393 "unusable type for last operand in"
4394 " vector/vector shift/rotate.\n");
4395 return false;
4398 /* See if the machine has a vector shifted by scalar insn and if not
4399 then see if it has a vector shifted by vector insn. */
4400 else
4402 optab = optab_for_tree_code (code, vectype, optab_scalar);
4403 if (optab
4404 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
4406 if (dump_enabled_p ())
4407 dump_printf_loc (MSG_NOTE, vect_location,
4408 "vector/scalar shift/rotate found.\n");
4410 else
4412 optab = optab_for_tree_code (code, vectype, optab_vector);
4413 if (optab
4414 && (optab_handler (optab, TYPE_MODE (vectype))
4415 != CODE_FOR_nothing))
4417 scalar_shift_arg = false;
4419 if (dump_enabled_p ())
4420 dump_printf_loc (MSG_NOTE, vect_location,
4421 "vector/vector shift/rotate found.\n");
4423 /* Unlike the other binary operators, shifts/rotates have
4424 the rhs being int, instead of the same type as the lhs,
4425 so make sure the scalar is the right type if we are
4426 dealing with vectors of long long/long/short/char. */
4427 if (dt[1] == vect_constant_def)
4428 op1 = fold_convert (TREE_TYPE (vectype), op1);
4429 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
4430 TREE_TYPE (op1)))
4432 if (slp_node
4433 && TYPE_MODE (TREE_TYPE (vectype))
4434 != TYPE_MODE (TREE_TYPE (op1)))
4436 if (dump_enabled_p ())
4437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4438 "unusable type for last operand in"
4439 " vector/vector shift/rotate.\n");
4440 return false;
4442 if (vec_stmt && !slp_node)
4444 op1 = fold_convert (TREE_TYPE (vectype), op1);
4445 op1 = vect_init_vector (stmt, op1,
4446 TREE_TYPE (vectype), NULL);
4453 /* Supportable by target? */
4454 if (!optab)
4456 if (dump_enabled_p ())
4457 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4458 "no optab.\n");
4459 return false;
4461 vec_mode = TYPE_MODE (vectype);
4462 icode = (int) optab_handler (optab, vec_mode);
4463 if (icode == CODE_FOR_nothing)
4465 if (dump_enabled_p ())
4466 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4467 "op not supported by target.\n");
4468 /* Check only during analysis. */
4469 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4470 || (vf < vect_min_worthwhile_factor (code)
4471 && !vec_stmt))
4472 return false;
4473 if (dump_enabled_p ())
4474 dump_printf_loc (MSG_NOTE, vect_location,
4475 "proceeding using word mode.\n");
4478 /* Worthwhile without SIMD support? Check only during analysis. */
4479 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4480 && vf < vect_min_worthwhile_factor (code)
4481 && !vec_stmt)
4483 if (dump_enabled_p ())
4484 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4485 "not worthwhile without SIMD support.\n");
4486 return false;
4489 if (!vec_stmt) /* transformation not required. */
4491 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
4492 if (dump_enabled_p ())
4493 dump_printf_loc (MSG_NOTE, vect_location,
4494 "=== vectorizable_shift ===\n");
4495 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4496 return true;
4499 /** Transform. **/
4501 if (dump_enabled_p ())
4502 dump_printf_loc (MSG_NOTE, vect_location,
4503 "transform binary/unary operation.\n");
4505 /* Handle def. */
4506 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4508 prev_stmt_info = NULL;
4509 for (j = 0; j < ncopies; j++)
4511 /* Handle uses. */
4512 if (j == 0)
4514 if (scalar_shift_arg)
4516 /* Vector shl and shr insn patterns can be defined with scalar
4517 operand 2 (shift operand). In this case, use constant or loop
4518 invariant op1 directly, without extending it to vector mode
4519 first. */
4520 optab_op2_mode = insn_data[icode].operand[2].mode;
4521 if (!VECTOR_MODE_P (optab_op2_mode))
4523 if (dump_enabled_p ())
4524 dump_printf_loc (MSG_NOTE, vect_location,
4525 "operand 1 using scalar mode.\n");
4526 vec_oprnd1 = op1;
4527 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
4528 vec_oprnds1.quick_push (vec_oprnd1);
4529 if (slp_node)
4531 /* Store vec_oprnd1 for every vector stmt to be created
4532 for SLP_NODE. We check during the analysis that all
4533 the shift arguments are the same.
4534 TODO: Allow different constants for different vector
4535 stmts generated for an SLP instance. */
4536 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4537 vec_oprnds1.quick_push (vec_oprnd1);
4542 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
4543 (a special case for certain kind of vector shifts); otherwise,
4544 operand 1 should be of a vector type (the usual case). */
4545 if (vec_oprnd1)
4546 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4547 slp_node, -1);
4548 else
4549 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4550 slp_node, -1);
4552 else
4553 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4555 /* Arguments are ready. Create the new vector stmt. */
4556 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4558 vop1 = vec_oprnds1[i];
4559 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
4560 new_temp = make_ssa_name (vec_dest, new_stmt);
4561 gimple_assign_set_lhs (new_stmt, new_temp);
4562 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4563 if (slp_node)
4564 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4567 if (slp_node)
4568 continue;
4570 if (j == 0)
4571 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4572 else
4573 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4574 prev_stmt_info = vinfo_for_stmt (new_stmt);
4577 vec_oprnds0.release ();
4578 vec_oprnds1.release ();
4580 return true;
4584 /* Function vectorizable_operation.
4586 Check if STMT performs a binary, unary or ternary operation that can
4587 be vectorized.
4588 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4589 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4590 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4592 static bool
4593 vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
4594 gimple *vec_stmt, slp_tree slp_node)
4596 tree vec_dest;
4597 tree scalar_dest;
4598 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
4599 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4600 tree vectype;
4601 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4602 enum tree_code code;
4603 machine_mode vec_mode;
4604 tree new_temp;
4605 int op_type;
4606 optab optab;
4607 int icode;
4608 tree def;
4609 gimple def_stmt;
4610 enum vect_def_type dt[3]
4611 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
4612 gimple new_stmt = NULL;
4613 stmt_vec_info prev_stmt_info;
4614 int nunits_in;
4615 int nunits_out;
4616 tree vectype_out;
4617 int ncopies;
4618 int j, i;
4619 vec<tree> vec_oprnds0 = vNULL;
4620 vec<tree> vec_oprnds1 = vNULL;
4621 vec<tree> vec_oprnds2 = vNULL;
4622 tree vop0, vop1, vop2;
4623 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4624 int vf;
4626 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4627 return false;
4629 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4630 return false;
4632 /* Is STMT a vectorizable binary/unary operation? */
4633 if (!is_gimple_assign (stmt))
4634 return false;
4636 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4637 return false;
4639 code = gimple_assign_rhs_code (stmt);
4641 /* For pointer addition, we should use the normal plus for
4642 the vector addition. */
4643 if (code == POINTER_PLUS_EXPR)
4644 code = PLUS_EXPR;
4646 /* Support only unary or binary operations. */
4647 op_type = TREE_CODE_LENGTH (code);
4648 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
4650 if (dump_enabled_p ())
4651 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4652 "num. args = %d (not unary/binary/ternary op).\n",
4653 op_type);
4654 return false;
4657 scalar_dest = gimple_assign_lhs (stmt);
4658 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4660 /* Most operations cannot handle bit-precision types without extra
4661 truncations. */
4662 if ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4663 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4664 /* Exception are bitwise binary operations. */
4665 && code != BIT_IOR_EXPR
4666 && code != BIT_XOR_EXPR
4667 && code != BIT_AND_EXPR)
4669 if (dump_enabled_p ())
4670 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4671 "bit-precision arithmetic not supported.\n");
4672 return false;
4675 op0 = gimple_assign_rhs1 (stmt);
4676 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
4677 &def_stmt, &def, &dt[0], &vectype))
4679 if (dump_enabled_p ())
4680 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4681 "use not simple.\n");
4682 return false;
4684 /* If op0 is an external or constant def use a vector type with
4685 the same size as the output vector type. */
4686 if (!vectype)
4687 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4688 if (vec_stmt)
4689 gcc_assert (vectype);
4690 if (!vectype)
4692 if (dump_enabled_p ())
4694 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4695 "no vectype for scalar type ");
4696 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
4697 TREE_TYPE (op0));
4698 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4701 return false;
4704 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4705 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4706 if (nunits_out != nunits_in)
4707 return false;
4709 if (op_type == binary_op || op_type == ternary_op)
4711 op1 = gimple_assign_rhs2 (stmt);
4712 if (!vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4713 &def, &dt[1]))
4715 if (dump_enabled_p ())
4716 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4717 "use not simple.\n");
4718 return false;
4721 if (op_type == ternary_op)
4723 op2 = gimple_assign_rhs3 (stmt);
4724 if (!vect_is_simple_use (op2, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4725 &def, &dt[2]))
4727 if (dump_enabled_p ())
4728 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4729 "use not simple.\n");
4730 return false;
4734 if (loop_vinfo)
4735 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4736 else
4737 vf = 1;
4739 /* Multiple types in SLP are handled by creating the appropriate number of
4740 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4741 case of SLP. */
4742 if (slp_node || PURE_SLP_STMT (stmt_info))
4743 ncopies = 1;
4744 else
4745 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4747 gcc_assert (ncopies >= 1);
4749 /* Shifts are handled in vectorizable_shift (). */
4750 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4751 || code == RROTATE_EXPR)
4752 return false;
4754 /* Supportable by target? */
4756 vec_mode = TYPE_MODE (vectype);
4757 if (code == MULT_HIGHPART_EXPR)
4759 if (can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype)))
4760 icode = LAST_INSN_CODE;
4761 else
4762 icode = CODE_FOR_nothing;
4764 else
4766 optab = optab_for_tree_code (code, vectype, optab_default);
4767 if (!optab)
4769 if (dump_enabled_p ())
4770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4771 "no optab.\n");
4772 return false;
4774 icode = (int) optab_handler (optab, vec_mode);
4777 if (icode == CODE_FOR_nothing)
4779 if (dump_enabled_p ())
4780 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4781 "op not supported by target.\n");
4782 /* Check only during analysis. */
4783 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4784 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
4785 return false;
4786 if (dump_enabled_p ())
4787 dump_printf_loc (MSG_NOTE, vect_location,
4788 "proceeding using word mode.\n");
4791 /* Worthwhile without SIMD support? Check only during analysis. */
4792 if (!VECTOR_MODE_P (vec_mode)
4793 && !vec_stmt
4794 && vf < vect_min_worthwhile_factor (code))
4796 if (dump_enabled_p ())
4797 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4798 "not worthwhile without SIMD support.\n");
4799 return false;
4802 if (!vec_stmt) /* transformation not required. */
4804 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
4805 if (dump_enabled_p ())
4806 dump_printf_loc (MSG_NOTE, vect_location,
4807 "=== vectorizable_operation ===\n");
4808 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4809 return true;
4812 /** Transform. **/
4814 if (dump_enabled_p ())
4815 dump_printf_loc (MSG_NOTE, vect_location,
4816 "transform binary/unary operation.\n");
4818 /* Handle def. */
4819 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4821 /* In case the vectorization factor (VF) is bigger than the number
4822 of elements that we can fit in a vectype (nunits), we have to generate
4823 more than one vector stmt - i.e - we need to "unroll" the
4824 vector stmt by a factor VF/nunits. In doing so, we record a pointer
4825 from one copy of the vector stmt to the next, in the field
4826 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4827 stages to find the correct vector defs to be used when vectorizing
4828 stmts that use the defs of the current stmt. The example below
4829 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
4830 we need to create 4 vectorized stmts):
4832 before vectorization:
4833 RELATED_STMT VEC_STMT
4834 S1: x = memref - -
4835 S2: z = x + 1 - -
4837 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
4838 there):
4839 RELATED_STMT VEC_STMT
4840 VS1_0: vx0 = memref0 VS1_1 -
4841 VS1_1: vx1 = memref1 VS1_2 -
4842 VS1_2: vx2 = memref2 VS1_3 -
4843 VS1_3: vx3 = memref3 - -
4844 S1: x = load - VS1_0
4845 S2: z = x + 1 - -
4847 step2: vectorize stmt S2 (done here):
4848 To vectorize stmt S2 we first need to find the relevant vector
4849 def for the first operand 'x'. This is, as usual, obtained from
4850 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
4851 that defines 'x' (S1). This way we find the stmt VS1_0, and the
4852 relevant vector def 'vx0'. Having found 'vx0' we can generate
4853 the vector stmt VS2_0, and as usual, record it in the
4854 STMT_VINFO_VEC_STMT of stmt S2.
4855 When creating the second copy (VS2_1), we obtain the relevant vector
4856 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
4857 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
4858 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
4859 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
4860 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
4861 chain of stmts and pointers:
4862 RELATED_STMT VEC_STMT
4863 VS1_0: vx0 = memref0 VS1_1 -
4864 VS1_1: vx1 = memref1 VS1_2 -
4865 VS1_2: vx2 = memref2 VS1_3 -
4866 VS1_3: vx3 = memref3 - -
4867 S1: x = load - VS1_0
4868 VS2_0: vz0 = vx0 + v1 VS2_1 -
4869 VS2_1: vz1 = vx1 + v1 VS2_2 -
4870 VS2_2: vz2 = vx2 + v1 VS2_3 -
4871 VS2_3: vz3 = vx3 + v1 - -
4872 S2: z = x + 1 - VS2_0 */
4874 prev_stmt_info = NULL;
4875 for (j = 0; j < ncopies; j++)
4877 /* Handle uses. */
4878 if (j == 0)
4880 if (op_type == binary_op || op_type == ternary_op)
4881 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4882 slp_node, -1);
4883 else
4884 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4885 slp_node, -1);
4886 if (op_type == ternary_op)
4888 vec_oprnds2.create (1);
4889 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
4890 stmt,
4891 NULL));
4894 else
4896 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4897 if (op_type == ternary_op)
4899 tree vec_oprnd = vec_oprnds2.pop ();
4900 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
4901 vec_oprnd));
4905 /* Arguments are ready. Create the new vector stmt. */
4906 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4908 vop1 = ((op_type == binary_op || op_type == ternary_op)
4909 ? vec_oprnds1[i] : NULL_TREE);
4910 vop2 = ((op_type == ternary_op)
4911 ? vec_oprnds2[i] : NULL_TREE);
4912 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
4913 new_temp = make_ssa_name (vec_dest, new_stmt);
4914 gimple_assign_set_lhs (new_stmt, new_temp);
4915 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4916 if (slp_node)
4917 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4920 if (slp_node)
4921 continue;
4923 if (j == 0)
4924 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4925 else
4926 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4927 prev_stmt_info = vinfo_for_stmt (new_stmt);
4930 vec_oprnds0.release ();
4931 vec_oprnds1.release ();
4932 vec_oprnds2.release ();
4934 return true;
4937 /* A helper function to ensure data reference DR's base alignment
4938 for STMT_INFO. */
4940 static void
4941 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
4943 if (!dr->aux)
4944 return;
4946 if (((dataref_aux *)dr->aux)->base_misaligned)
4948 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4949 tree base_decl = ((dataref_aux *)dr->aux)->base_decl;
4951 if (decl_in_symtab_p (base_decl))
4952 symtab_node::get (base_decl)->increase_alignment (TYPE_ALIGN (vectype));
4953 else
4955 DECL_ALIGN (base_decl) = TYPE_ALIGN (vectype);
4956 DECL_USER_ALIGN (base_decl) = 1;
4958 ((dataref_aux *)dr->aux)->base_misaligned = false;
4963 /* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
4964 reversal of the vector elements. If that is impossible to do,
4965 returns NULL. */
4967 static tree
4968 perm_mask_for_reverse (tree vectype)
4970 int i, nunits;
4971 unsigned char *sel;
4973 nunits = TYPE_VECTOR_SUBPARTS (vectype);
4974 sel = XALLOCAVEC (unsigned char, nunits);
4976 for (i = 0; i < nunits; ++i)
4977 sel[i] = nunits - 1 - i;
4979 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
4980 return NULL_TREE;
4981 return vect_gen_perm_mask_checked (vectype, sel);
4984 /* Function vectorizable_store.
4986 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
4987 can be vectorized.
4988 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4989 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4990 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4992 static bool
4993 vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
4994 slp_tree slp_node)
4996 tree scalar_dest;
4997 tree data_ref;
4998 tree op;
4999 tree vec_oprnd = NULL_TREE;
5000 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5001 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5002 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5003 tree elem_type;
5004 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5005 struct loop *loop = NULL;
5006 machine_mode vec_mode;
5007 tree dummy;
5008 enum dr_alignment_support alignment_support_scheme;
5009 tree def;
5010 gimple def_stmt;
5011 enum vect_def_type dt;
5012 stmt_vec_info prev_stmt_info = NULL;
5013 tree dataref_ptr = NULL_TREE;
5014 tree dataref_offset = NULL_TREE;
5015 gimple ptr_incr = NULL;
5016 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5017 int ncopies;
5018 int j;
5019 gimple next_stmt, first_stmt = NULL;
5020 bool grouped_store = false;
5021 bool store_lanes_p = false;
5022 unsigned int group_size, i;
5023 vec<tree> dr_chain = vNULL;
5024 vec<tree> oprnds = vNULL;
5025 vec<tree> result_chain = vNULL;
5026 bool inv_p;
5027 bool negative = false;
5028 tree offset = NULL_TREE;
5029 vec<tree> vec_oprnds = vNULL;
5030 bool slp = (slp_node != NULL);
5031 unsigned int vec_num;
5032 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5033 tree aggr_type;
5035 if (loop_vinfo)
5036 loop = LOOP_VINFO_LOOP (loop_vinfo);
5038 /* Multiple types in SLP are handled by creating the appropriate number of
5039 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5040 case of SLP. */
5041 if (slp || PURE_SLP_STMT (stmt_info))
5042 ncopies = 1;
5043 else
5044 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5046 gcc_assert (ncopies >= 1);
5048 /* FORNOW. This restriction should be relaxed. */
5049 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
5051 if (dump_enabled_p ())
5052 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5053 "multiple types in nested loop.\n");
5054 return false;
5057 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5058 return false;
5060 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
5061 return false;
5063 /* Is vectorizable store? */
5065 if (!is_gimple_assign (stmt))
5066 return false;
5068 scalar_dest = gimple_assign_lhs (stmt);
5069 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
5070 && is_pattern_stmt_p (stmt_info))
5071 scalar_dest = TREE_OPERAND (scalar_dest, 0);
5072 if (TREE_CODE (scalar_dest) != ARRAY_REF
5073 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
5074 && TREE_CODE (scalar_dest) != INDIRECT_REF
5075 && TREE_CODE (scalar_dest) != COMPONENT_REF
5076 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
5077 && TREE_CODE (scalar_dest) != REALPART_EXPR
5078 && TREE_CODE (scalar_dest) != MEM_REF)
5079 return false;
5081 gcc_assert (gimple_assign_single_p (stmt));
5082 op = gimple_assign_rhs1 (stmt);
5083 if (!vect_is_simple_use (op, stmt, loop_vinfo, bb_vinfo, &def_stmt,
5084 &def, &dt))
5086 if (dump_enabled_p ())
5087 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5088 "use not simple.\n");
5089 return false;
5092 elem_type = TREE_TYPE (vectype);
5093 vec_mode = TYPE_MODE (vectype);
5095 /* FORNOW. In some cases can vectorize even if data-type not supported
5096 (e.g. - array initialization with 0). */
5097 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
5098 return false;
5100 if (!STMT_VINFO_DATA_REF (stmt_info))
5101 return false;
5103 negative =
5104 tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
5105 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
5106 size_zero_node) < 0;
5107 if (negative && ncopies > 1)
5109 if (dump_enabled_p ())
5110 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5111 "multiple types with negative step.\n");
5112 return false;
5115 if (negative)
5117 gcc_assert (!grouped_store);
5118 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5119 if (alignment_support_scheme != dr_aligned
5120 && alignment_support_scheme != dr_unaligned_supported)
5122 if (dump_enabled_p ())
5123 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5124 "negative step but alignment required.\n");
5125 return false;
5127 if (dt != vect_constant_def
5128 && dt != vect_external_def
5129 && !perm_mask_for_reverse (vectype))
5131 if (dump_enabled_p ())
5132 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5133 "negative step and reversing not supported.\n");
5134 return false;
5138 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5140 grouped_store = true;
5141 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5142 if (!slp && !PURE_SLP_STMT (stmt_info))
5144 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5145 if (vect_store_lanes_supported (vectype, group_size))
5146 store_lanes_p = true;
5147 else if (!vect_grouped_store_supported (vectype, group_size))
5148 return false;
5151 if (first_stmt == stmt)
5153 /* STMT is the leader of the group. Check the operands of all the
5154 stmts of the group. */
5155 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
5156 while (next_stmt)
5158 gcc_assert (gimple_assign_single_p (next_stmt));
5159 op = gimple_assign_rhs1 (next_stmt);
5160 if (!vect_is_simple_use (op, next_stmt, loop_vinfo, bb_vinfo,
5161 &def_stmt, &def, &dt))
5163 if (dump_enabled_p ())
5164 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5165 "use not simple.\n");
5166 return false;
5168 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5173 if (!vec_stmt) /* transformation not required. */
5175 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
5176 /* The SLP costs are calculated during SLP analysis. */
5177 if (!PURE_SLP_STMT (stmt_info))
5178 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
5179 NULL, NULL, NULL);
5180 return true;
5183 /** Transform. **/
5185 ensure_base_align (stmt_info, dr);
5187 if (grouped_store)
5189 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5190 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5192 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
5194 /* FORNOW */
5195 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
5197 /* We vectorize all the stmts of the interleaving group when we
5198 reach the last stmt in the group. */
5199 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
5200 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
5201 && !slp)
5203 *vec_stmt = NULL;
5204 return true;
5207 if (slp)
5209 grouped_store = false;
5210 /* VEC_NUM is the number of vect stmts to be created for this
5211 group. */
5212 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5213 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5214 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5215 op = gimple_assign_rhs1 (first_stmt);
5217 else
5218 /* VEC_NUM is the number of vect stmts to be created for this
5219 group. */
5220 vec_num = group_size;
5222 else
5224 first_stmt = stmt;
5225 first_dr = dr;
5226 group_size = vec_num = 1;
5229 if (dump_enabled_p ())
5230 dump_printf_loc (MSG_NOTE, vect_location,
5231 "transform store. ncopies = %d\n", ncopies);
5233 dr_chain.create (group_size);
5234 oprnds.create (group_size);
5236 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
5237 gcc_assert (alignment_support_scheme);
5238 /* Targets with store-lane instructions must not require explicit
5239 realignment. */
5240 gcc_assert (!store_lanes_p
5241 || alignment_support_scheme == dr_aligned
5242 || alignment_support_scheme == dr_unaligned_supported);
5244 if (negative)
5245 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
5247 if (store_lanes_p)
5248 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5249 else
5250 aggr_type = vectype;
5252 /* In case the vectorization factor (VF) is bigger than the number
5253 of elements that we can fit in a vectype (nunits), we have to generate
5254 more than one vector stmt - i.e - we need to "unroll" the
5255 vector stmt by a factor VF/nunits. For more details see documentation in
5256 vect_get_vec_def_for_copy_stmt. */
5258 /* In case of interleaving (non-unit grouped access):
5260 S1: &base + 2 = x2
5261 S2: &base = x0
5262 S3: &base + 1 = x1
5263 S4: &base + 3 = x3
5265 We create vectorized stores starting from base address (the access of the
5266 first stmt in the chain (S2 in the above example), when the last store stmt
5267 of the chain (S4) is reached:
5269 VS1: &base = vx2
5270 VS2: &base + vec_size*1 = vx0
5271 VS3: &base + vec_size*2 = vx1
5272 VS4: &base + vec_size*3 = vx3
5274 Then permutation statements are generated:
5276 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
5277 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
5280 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5281 (the order of the data-refs in the output of vect_permute_store_chain
5282 corresponds to the order of scalar stmts in the interleaving chain - see
5283 the documentation of vect_permute_store_chain()).
5285 In case of both multiple types and interleaving, above vector stores and
5286 permutation stmts are created for every copy. The result vector stmts are
5287 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
5288 STMT_VINFO_RELATED_STMT for the next copies.
5291 prev_stmt_info = NULL;
5292 for (j = 0; j < ncopies; j++)
5294 gimple new_stmt;
5296 if (j == 0)
5298 if (slp)
5300 /* Get vectorized arguments for SLP_NODE. */
5301 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
5302 NULL, slp_node, -1);
5304 vec_oprnd = vec_oprnds[0];
5306 else
5308 /* For interleaved stores we collect vectorized defs for all the
5309 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
5310 used as an input to vect_permute_store_chain(), and OPRNDS as
5311 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
5313 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5314 OPRNDS are of size 1. */
5315 next_stmt = first_stmt;
5316 for (i = 0; i < group_size; i++)
5318 /* Since gaps are not supported for interleaved stores,
5319 GROUP_SIZE is the exact number of stmts in the chain.
5320 Therefore, NEXT_STMT can't be NULL_TREE. In case that
5321 there is no interleaving, GROUP_SIZE is 1, and only one
5322 iteration of the loop will be executed. */
5323 gcc_assert (next_stmt
5324 && gimple_assign_single_p (next_stmt));
5325 op = gimple_assign_rhs1 (next_stmt);
5327 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt,
5328 NULL);
5329 dr_chain.quick_push (vec_oprnd);
5330 oprnds.quick_push (vec_oprnd);
5331 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5335 /* We should have catched mismatched types earlier. */
5336 gcc_assert (useless_type_conversion_p (vectype,
5337 TREE_TYPE (vec_oprnd)));
5338 bool simd_lane_access_p
5339 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5340 if (simd_lane_access_p
5341 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5342 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5343 && integer_zerop (DR_OFFSET (first_dr))
5344 && integer_zerop (DR_INIT (first_dr))
5345 && alias_sets_conflict_p (get_alias_set (aggr_type),
5346 get_alias_set (DR_REF (first_dr))))
5348 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5349 dataref_offset = build_int_cst (reference_alias_ptr_type
5350 (DR_REF (first_dr)), 0);
5351 inv_p = false;
5353 else
5354 dataref_ptr
5355 = vect_create_data_ref_ptr (first_stmt, aggr_type,
5356 simd_lane_access_p ? loop : NULL,
5357 offset, &dummy, gsi, &ptr_incr,
5358 simd_lane_access_p, &inv_p);
5359 gcc_assert (bb_vinfo || !inv_p);
5361 else
5363 /* For interleaved stores we created vectorized defs for all the
5364 defs stored in OPRNDS in the previous iteration (previous copy).
5365 DR_CHAIN is then used as an input to vect_permute_store_chain(),
5366 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
5367 next copy.
5368 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5369 OPRNDS are of size 1. */
5370 for (i = 0; i < group_size; i++)
5372 op = oprnds[i];
5373 vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo, &def_stmt,
5374 &def, &dt);
5375 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
5376 dr_chain[i] = vec_oprnd;
5377 oprnds[i] = vec_oprnd;
5379 if (dataref_offset)
5380 dataref_offset
5381 = int_const_binop (PLUS_EXPR, dataref_offset,
5382 TYPE_SIZE_UNIT (aggr_type));
5383 else
5384 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5385 TYPE_SIZE_UNIT (aggr_type));
5388 if (store_lanes_p)
5390 tree vec_array;
5392 /* Combine all the vectors into an array. */
5393 vec_array = create_vector_array (vectype, vec_num);
5394 for (i = 0; i < vec_num; i++)
5396 vec_oprnd = dr_chain[i];
5397 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
5400 /* Emit:
5401 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
5402 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5403 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
5404 gimple_call_set_lhs (new_stmt, data_ref);
5405 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5407 else
5409 new_stmt = NULL;
5410 if (grouped_store)
5412 if (j == 0)
5413 result_chain.create (group_size);
5414 /* Permute. */
5415 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
5416 &result_chain);
5419 next_stmt = first_stmt;
5420 for (i = 0; i < vec_num; i++)
5422 unsigned align, misalign;
5424 if (i > 0)
5425 /* Bump the vector pointer. */
5426 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5427 stmt, NULL_TREE);
5429 if (slp)
5430 vec_oprnd = vec_oprnds[i];
5431 else if (grouped_store)
5432 /* For grouped stores vectorized defs are interleaved in
5433 vect_permute_store_chain(). */
5434 vec_oprnd = result_chain[i];
5436 data_ref = build2 (MEM_REF, TREE_TYPE (vec_oprnd), dataref_ptr,
5437 dataref_offset
5438 ? dataref_offset
5439 : build_int_cst (reference_alias_ptr_type
5440 (DR_REF (first_dr)), 0));
5441 align = TYPE_ALIGN_UNIT (vectype);
5442 if (aligned_access_p (first_dr))
5443 misalign = 0;
5444 else if (DR_MISALIGNMENT (first_dr) == -1)
5446 TREE_TYPE (data_ref)
5447 = build_aligned_type (TREE_TYPE (data_ref),
5448 TYPE_ALIGN (elem_type));
5449 align = TYPE_ALIGN_UNIT (elem_type);
5450 misalign = 0;
5452 else
5454 TREE_TYPE (data_ref)
5455 = build_aligned_type (TREE_TYPE (data_ref),
5456 TYPE_ALIGN (elem_type));
5457 misalign = DR_MISALIGNMENT (first_dr);
5459 if (dataref_offset == NULL_TREE)
5460 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
5461 misalign);
5463 if (negative
5464 && dt != vect_constant_def
5465 && dt != vect_external_def)
5467 tree perm_mask = perm_mask_for_reverse (vectype);
5468 tree perm_dest
5469 = vect_create_destination_var (gimple_assign_rhs1 (stmt),
5470 vectype);
5471 tree new_temp = make_ssa_name (perm_dest);
5473 /* Generate the permute statement. */
5474 gimple perm_stmt
5475 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
5476 vec_oprnd, perm_mask);
5477 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5479 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
5480 vec_oprnd = new_temp;
5483 /* Arguments are ready. Create the new vector stmt. */
5484 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
5485 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5487 if (slp)
5488 continue;
5490 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5491 if (!next_stmt)
5492 break;
5495 if (!slp)
5497 if (j == 0)
5498 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5499 else
5500 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5501 prev_stmt_info = vinfo_for_stmt (new_stmt);
5505 dr_chain.release ();
5506 oprnds.release ();
5507 result_chain.release ();
5508 vec_oprnds.release ();
5510 return true;
5513 /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
5514 VECTOR_CST mask. No checks are made that the target platform supports the
5515 mask, so callers may wish to test can_vec_perm_p separately, or use
5516 vect_gen_perm_mask_checked. */
5518 tree
5519 vect_gen_perm_mask_any (tree vectype, const unsigned char *sel)
5521 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
5522 int i, nunits;
5524 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5526 mask_elt_type = lang_hooks.types.type_for_mode
5527 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
5528 mask_type = get_vectype_for_scalar_type (mask_elt_type);
5530 mask_elts = XALLOCAVEC (tree, nunits);
5531 for (i = nunits - 1; i >= 0; i--)
5532 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
5533 mask_vec = build_vector (mask_type, mask_elts);
5535 return mask_vec;
5538 /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_p,
5539 i.e. that the target supports the pattern _for arbitrary input vectors_. */
5541 tree
5542 vect_gen_perm_mask_checked (tree vectype, const unsigned char *sel)
5544 gcc_assert (can_vec_perm_p (TYPE_MODE (vectype), false, sel));
5545 return vect_gen_perm_mask_any (vectype, sel);
5548 /* Given a vector variable X and Y, that was generated for the scalar
5549 STMT, generate instructions to permute the vector elements of X and Y
5550 using permutation mask MASK_VEC, insert them at *GSI and return the
5551 permuted vector variable. */
5553 static tree
5554 permute_vec_elements (tree x, tree y, tree mask_vec, gimple stmt,
5555 gimple_stmt_iterator *gsi)
5557 tree vectype = TREE_TYPE (x);
5558 tree perm_dest, data_ref;
5559 gimple perm_stmt;
5561 perm_dest = vect_create_destination_var (gimple_get_lhs (stmt), vectype);
5562 data_ref = make_ssa_name (perm_dest);
5564 /* Generate the permute statement. */
5565 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
5566 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5568 return data_ref;
5571 /* Hoist the definitions of all SSA uses on STMT out of the loop LOOP,
5572 inserting them on the loops preheader edge. Returns true if we
5573 were successful in doing so (and thus STMT can be moved then),
5574 otherwise returns false. */
5576 static bool
5577 hoist_defs_of_uses (gimple stmt, struct loop *loop)
5579 ssa_op_iter i;
5580 tree op;
5581 bool any = false;
5583 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5585 gimple def_stmt = SSA_NAME_DEF_STMT (op);
5586 if (!gimple_nop_p (def_stmt)
5587 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
5589 /* Make sure we don't need to recurse. While we could do
5590 so in simple cases when there are more complex use webs
5591 we don't have an easy way to preserve stmt order to fulfil
5592 dependencies within them. */
5593 tree op2;
5594 ssa_op_iter i2;
5595 if (gimple_code (def_stmt) == GIMPLE_PHI)
5596 return false;
5597 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
5599 gimple def_stmt2 = SSA_NAME_DEF_STMT (op2);
5600 if (!gimple_nop_p (def_stmt2)
5601 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
5602 return false;
5604 any = true;
5608 if (!any)
5609 return true;
5611 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5613 gimple def_stmt = SSA_NAME_DEF_STMT (op);
5614 if (!gimple_nop_p (def_stmt)
5615 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
5617 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
5618 gsi_remove (&gsi, false);
5619 gsi_insert_on_edge_immediate (loop_preheader_edge (loop), def_stmt);
5623 return true;
5626 /* vectorizable_load.
5628 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
5629 can be vectorized.
5630 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5631 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5632 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5634 static bool
5635 vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
5636 slp_tree slp_node, slp_instance slp_node_instance)
5638 tree scalar_dest;
5639 tree vec_dest = NULL;
5640 tree data_ref = NULL;
5641 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5642 stmt_vec_info prev_stmt_info;
5643 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5644 struct loop *loop = NULL;
5645 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
5646 bool nested_in_vect_loop = false;
5647 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5648 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5649 tree elem_type;
5650 tree new_temp;
5651 machine_mode mode;
5652 gimple new_stmt = NULL;
5653 tree dummy;
5654 enum dr_alignment_support alignment_support_scheme;
5655 tree dataref_ptr = NULL_TREE;
5656 tree dataref_offset = NULL_TREE;
5657 gimple ptr_incr = NULL;
5658 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5659 int ncopies;
5660 int i, j, group_size, group_gap;
5661 tree msq = NULL_TREE, lsq;
5662 tree offset = NULL_TREE;
5663 tree byte_offset = NULL_TREE;
5664 tree realignment_token = NULL_TREE;
5665 gphi *phi = NULL;
5666 vec<tree> dr_chain = vNULL;
5667 bool grouped_load = false;
5668 bool load_lanes_p = false;
5669 gimple first_stmt;
5670 bool inv_p;
5671 bool negative = false;
5672 bool compute_in_loop = false;
5673 struct loop *at_loop;
5674 int vec_num;
5675 bool slp = (slp_node != NULL);
5676 bool slp_perm = false;
5677 enum tree_code code;
5678 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5679 int vf;
5680 tree aggr_type;
5681 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
5682 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
5683 int gather_scale = 1;
5684 enum vect_def_type gather_dt = vect_unknown_def_type;
5686 if (loop_vinfo)
5688 loop = LOOP_VINFO_LOOP (loop_vinfo);
5689 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
5690 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5692 else
5693 vf = 1;
5695 /* Multiple types in SLP are handled by creating the appropriate number of
5696 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5697 case of SLP. */
5698 if (slp || PURE_SLP_STMT (stmt_info))
5699 ncopies = 1;
5700 else
5701 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5703 gcc_assert (ncopies >= 1);
5705 /* FORNOW. This restriction should be relaxed. */
5706 if (nested_in_vect_loop && ncopies > 1)
5708 if (dump_enabled_p ())
5709 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5710 "multiple types in nested loop.\n");
5711 return false;
5714 /* Invalidate assumptions made by dependence analysis when vectorization
5715 on the unrolled body effectively re-orders stmts. */
5716 if (ncopies > 1
5717 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
5718 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5719 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
5721 if (dump_enabled_p ())
5722 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5723 "cannot perform implicit CSE when unrolling "
5724 "with negative dependence distance\n");
5725 return false;
5728 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5729 return false;
5731 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
5732 return false;
5734 /* Is vectorizable load? */
5735 if (!is_gimple_assign (stmt))
5736 return false;
5738 scalar_dest = gimple_assign_lhs (stmt);
5739 if (TREE_CODE (scalar_dest) != SSA_NAME)
5740 return false;
5742 code = gimple_assign_rhs_code (stmt);
5743 if (code != ARRAY_REF
5744 && code != BIT_FIELD_REF
5745 && code != INDIRECT_REF
5746 && code != COMPONENT_REF
5747 && code != IMAGPART_EXPR
5748 && code != REALPART_EXPR
5749 && code != MEM_REF
5750 && TREE_CODE_CLASS (code) != tcc_declaration)
5751 return false;
5753 if (!STMT_VINFO_DATA_REF (stmt_info))
5754 return false;
5756 elem_type = TREE_TYPE (vectype);
5757 mode = TYPE_MODE (vectype);
5759 /* FORNOW. In some cases can vectorize even if data-type not supported
5760 (e.g. - data copies). */
5761 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
5763 if (dump_enabled_p ())
5764 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5765 "Aligned load, but unsupported type.\n");
5766 return false;
5769 /* Check if the load is a part of an interleaving chain. */
5770 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5772 grouped_load = true;
5773 /* FORNOW */
5774 gcc_assert (! nested_in_vect_loop && !STMT_VINFO_GATHER_P (stmt_info));
5776 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5778 /* If this is single-element interleaving with an element distance
5779 that leaves unused vector loads around punt - we at least create
5780 very sub-optimal code in that case (and blow up memory,
5781 see PR65518). */
5782 if (first_stmt == stmt
5783 && !GROUP_NEXT_ELEMENT (stmt_info)
5784 && GROUP_SIZE (stmt_info) > TYPE_VECTOR_SUBPARTS (vectype))
5786 if (dump_enabled_p ())
5787 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5788 "single-element interleaving not supported "
5789 "for not adjacent vector loads\n");
5790 return false;
5793 if (!slp && !PURE_SLP_STMT (stmt_info))
5795 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5796 if (vect_load_lanes_supported (vectype, group_size))
5797 load_lanes_p = true;
5798 else if (!vect_grouped_load_supported (vectype, group_size))
5799 return false;
5802 /* Invalidate assumptions made by dependence analysis when vectorization
5803 on the unrolled body effectively re-orders stmts. */
5804 if (!PURE_SLP_STMT (stmt_info)
5805 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
5806 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5807 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
5809 if (dump_enabled_p ())
5810 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5811 "cannot perform implicit CSE when performing "
5812 "group loads with negative dependence distance\n");
5813 return false;
5816 /* Similarly when the stmt is a load that is both part of a SLP
5817 instance and a loop vectorized stmt via the same-dr mechanism
5818 we have to give up. */
5819 if (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)
5820 && (STMT_SLP_TYPE (stmt_info)
5821 != STMT_SLP_TYPE (vinfo_for_stmt
5822 (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)))))
5824 if (dump_enabled_p ())
5825 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5826 "conflicting SLP types for CSEd load\n");
5827 return false;
5832 if (STMT_VINFO_GATHER_P (stmt_info))
5834 gimple def_stmt;
5835 tree def;
5836 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
5837 &gather_off, &gather_scale);
5838 gcc_assert (gather_decl);
5839 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, bb_vinfo,
5840 &def_stmt, &def, &gather_dt,
5841 &gather_off_vectype))
5843 if (dump_enabled_p ())
5844 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5845 "gather index use not simple.\n");
5846 return false;
5849 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
5851 else
5853 negative = tree_int_cst_compare (nested_in_vect_loop
5854 ? STMT_VINFO_DR_STEP (stmt_info)
5855 : DR_STEP (dr),
5856 size_zero_node) < 0;
5857 if (negative && ncopies > 1)
5859 if (dump_enabled_p ())
5860 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5861 "multiple types with negative step.\n");
5862 return false;
5865 if (negative)
5867 if (grouped_load)
5869 if (dump_enabled_p ())
5870 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5871 "negative step for group load not supported"
5872 "\n");
5873 return false;
5875 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5876 if (alignment_support_scheme != dr_aligned
5877 && alignment_support_scheme != dr_unaligned_supported)
5879 if (dump_enabled_p ())
5880 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5881 "negative step but alignment required.\n");
5882 return false;
5884 if (!perm_mask_for_reverse (vectype))
5886 if (dump_enabled_p ())
5887 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5888 "negative step and reversing not supported."
5889 "\n");
5890 return false;
5895 if (!vec_stmt) /* transformation not required. */
5897 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
5898 /* The SLP costs are calculated during SLP analysis. */
5899 if (!PURE_SLP_STMT (stmt_info))
5900 vect_model_load_cost (stmt_info, ncopies, load_lanes_p,
5901 NULL, NULL, NULL);
5902 return true;
5905 if (dump_enabled_p ())
5906 dump_printf_loc (MSG_NOTE, vect_location,
5907 "transform load. ncopies = %d\n", ncopies);
5909 /** Transform. **/
5911 ensure_base_align (stmt_info, dr);
5913 if (STMT_VINFO_GATHER_P (stmt_info))
5915 tree vec_oprnd0 = NULL_TREE, op;
5916 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
5917 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5918 tree ptr, mask, var, scale, merge, perm_mask = NULL_TREE, prev_res = NULL_TREE;
5919 edge pe = loop_preheader_edge (loop);
5920 gimple_seq seq;
5921 basic_block new_bb;
5922 enum { NARROW, NONE, WIDEN } modifier;
5923 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
5925 if (nunits == gather_off_nunits)
5926 modifier = NONE;
5927 else if (nunits == gather_off_nunits / 2)
5929 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
5930 modifier = WIDEN;
5932 for (i = 0; i < gather_off_nunits; ++i)
5933 sel[i] = i | nunits;
5935 perm_mask = vect_gen_perm_mask_checked (gather_off_vectype, sel);
5937 else if (nunits == gather_off_nunits * 2)
5939 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5940 modifier = NARROW;
5942 for (i = 0; i < nunits; ++i)
5943 sel[i] = i < gather_off_nunits
5944 ? i : i + nunits - gather_off_nunits;
5946 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
5947 ncopies *= 2;
5949 else
5950 gcc_unreachable ();
5952 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
5953 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5954 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5955 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5956 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5957 scaletype = TREE_VALUE (arglist);
5958 gcc_checking_assert (types_compatible_p (srctype, rettype));
5960 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5962 ptr = fold_convert (ptrtype, gather_base);
5963 if (!is_gimple_min_invariant (ptr))
5965 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5966 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5967 gcc_assert (!new_bb);
5970 /* Currently we support only unconditional gather loads,
5971 so mask should be all ones. */
5972 if (TREE_CODE (masktype) == INTEGER_TYPE)
5973 mask = build_int_cst (masktype, -1);
5974 else if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
5976 mask = build_int_cst (TREE_TYPE (masktype), -1);
5977 mask = build_vector_from_val (masktype, mask);
5978 mask = vect_init_vector (stmt, mask, masktype, NULL);
5980 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
5982 REAL_VALUE_TYPE r;
5983 long tmp[6];
5984 for (j = 0; j < 6; ++j)
5985 tmp[j] = -1;
5986 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
5987 mask = build_real (TREE_TYPE (masktype), r);
5988 mask = build_vector_from_val (masktype, mask);
5989 mask = vect_init_vector (stmt, mask, masktype, NULL);
5991 else
5992 gcc_unreachable ();
5994 scale = build_int_cst (scaletype, gather_scale);
5996 if (TREE_CODE (TREE_TYPE (rettype)) == INTEGER_TYPE)
5997 merge = build_int_cst (TREE_TYPE (rettype), 0);
5998 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rettype)))
6000 REAL_VALUE_TYPE r;
6001 long tmp[6];
6002 for (j = 0; j < 6; ++j)
6003 tmp[j] = 0;
6004 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (rettype)));
6005 merge = build_real (TREE_TYPE (rettype), r);
6007 else
6008 gcc_unreachable ();
6009 merge = build_vector_from_val (rettype, merge);
6010 merge = vect_init_vector (stmt, merge, rettype, NULL);
6012 prev_stmt_info = NULL;
6013 for (j = 0; j < ncopies; ++j)
6015 if (modifier == WIDEN && (j & 1))
6016 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
6017 perm_mask, stmt, gsi);
6018 else if (j == 0)
6019 op = vec_oprnd0
6020 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
6021 else
6022 op = vec_oprnd0
6023 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
6025 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
6027 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
6028 == TYPE_VECTOR_SUBPARTS (idxtype));
6029 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
6030 var = make_ssa_name (var);
6031 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
6032 new_stmt
6033 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6034 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6035 op = var;
6038 new_stmt
6039 = gimple_build_call (gather_decl, 5, merge, ptr, op, mask, scale);
6041 if (!useless_type_conversion_p (vectype, rettype))
6043 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
6044 == TYPE_VECTOR_SUBPARTS (rettype));
6045 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
6046 op = make_ssa_name (var, new_stmt);
6047 gimple_call_set_lhs (new_stmt, op);
6048 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6049 var = make_ssa_name (vec_dest);
6050 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
6051 new_stmt
6052 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6054 else
6056 var = make_ssa_name (vec_dest, new_stmt);
6057 gimple_call_set_lhs (new_stmt, var);
6060 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6062 if (modifier == NARROW)
6064 if ((j & 1) == 0)
6066 prev_res = var;
6067 continue;
6069 var = permute_vec_elements (prev_res, var,
6070 perm_mask, stmt, gsi);
6071 new_stmt = SSA_NAME_DEF_STMT (var);
6074 if (prev_stmt_info == NULL)
6075 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6076 else
6077 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6078 prev_stmt_info = vinfo_for_stmt (new_stmt);
6080 return true;
6082 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
6084 gimple_stmt_iterator incr_gsi;
6085 bool insert_after;
6086 gimple incr;
6087 tree offvar;
6088 tree ivstep;
6089 tree running_off;
6090 vec<constructor_elt, va_gc> *v = NULL;
6091 gimple_seq stmts = NULL;
6092 tree stride_base, stride_step, alias_off;
6094 gcc_assert (!nested_in_vect_loop);
6096 stride_base
6097 = fold_build_pointer_plus
6098 (unshare_expr (DR_BASE_ADDRESS (dr)),
6099 size_binop (PLUS_EXPR,
6100 convert_to_ptrofftype (unshare_expr (DR_OFFSET (dr))),
6101 convert_to_ptrofftype (DR_INIT (dr))));
6102 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (dr)));
6104 /* For a load with loop-invariant (but other than power-of-2)
6105 stride (i.e. not a grouped access) like so:
6107 for (i = 0; i < n; i += stride)
6108 ... = array[i];
6110 we generate a new induction variable and new accesses to
6111 form a new vector (or vectors, depending on ncopies):
6113 for (j = 0; ; j += VF*stride)
6114 tmp1 = array[j];
6115 tmp2 = array[j + stride];
6117 vectemp = {tmp1, tmp2, ...}
6120 ivstep = stride_step;
6121 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
6122 build_int_cst (TREE_TYPE (ivstep), vf));
6124 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6126 create_iv (stride_base, ivstep, NULL,
6127 loop, &incr_gsi, insert_after,
6128 &offvar, NULL);
6129 incr = gsi_stmt (incr_gsi);
6130 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
6132 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
6133 if (stmts)
6134 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
6136 prev_stmt_info = NULL;
6137 running_off = offvar;
6138 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0);
6139 for (j = 0; j < ncopies; j++)
6141 tree vec_inv;
6143 vec_alloc (v, nunits);
6144 for (i = 0; i < nunits; i++)
6146 tree newref, newoff;
6147 gimple incr;
6148 newref = build2 (MEM_REF, TREE_TYPE (vectype),
6149 running_off, alias_off);
6151 newref = force_gimple_operand_gsi (gsi, newref, true,
6152 NULL_TREE, true,
6153 GSI_SAME_STMT);
6154 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
6155 newoff = copy_ssa_name (running_off);
6156 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6157 running_off, stride_step);
6158 vect_finish_stmt_generation (stmt, incr, gsi);
6160 running_off = newoff;
6163 vec_inv = build_constructor (vectype, v);
6164 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
6165 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6167 if (j == 0)
6168 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6169 else
6170 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6171 prev_stmt_info = vinfo_for_stmt (new_stmt);
6173 return true;
6176 if (grouped_load)
6178 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6179 if (slp
6180 && !SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
6181 && first_stmt != SLP_TREE_SCALAR_STMTS (slp_node)[0])
6182 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6184 /* Check if the chain of loads is already vectorized. */
6185 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
6186 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
6187 ??? But we can only do so if there is exactly one
6188 as we have no way to get at the rest. Leave the CSE
6189 opportunity alone.
6190 ??? With the group load eventually participating
6191 in multiple different permutations (having multiple
6192 slp nodes which refer to the same group) the CSE
6193 is even wrong code. See PR56270. */
6194 && !slp)
6196 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6197 return true;
6199 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
6200 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6202 /* VEC_NUM is the number of vect stmts to be created for this group. */
6203 if (slp)
6205 grouped_load = false;
6206 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6207 if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
6208 slp_perm = true;
6209 group_gap = GROUP_GAP (vinfo_for_stmt (first_stmt));
6211 else
6213 vec_num = group_size;
6214 group_gap = 0;
6217 else
6219 first_stmt = stmt;
6220 first_dr = dr;
6221 group_size = vec_num = 1;
6222 group_gap = 0;
6225 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
6226 gcc_assert (alignment_support_scheme);
6227 /* Targets with load-lane instructions must not require explicit
6228 realignment. */
6229 gcc_assert (!load_lanes_p
6230 || alignment_support_scheme == dr_aligned
6231 || alignment_support_scheme == dr_unaligned_supported);
6233 /* In case the vectorization factor (VF) is bigger than the number
6234 of elements that we can fit in a vectype (nunits), we have to generate
6235 more than one vector stmt - i.e - we need to "unroll" the
6236 vector stmt by a factor VF/nunits. In doing so, we record a pointer
6237 from one copy of the vector stmt to the next, in the field
6238 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
6239 stages to find the correct vector defs to be used when vectorizing
6240 stmts that use the defs of the current stmt. The example below
6241 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
6242 need to create 4 vectorized stmts):
6244 before vectorization:
6245 RELATED_STMT VEC_STMT
6246 S1: x = memref - -
6247 S2: z = x + 1 - -
6249 step 1: vectorize stmt S1:
6250 We first create the vector stmt VS1_0, and, as usual, record a
6251 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
6252 Next, we create the vector stmt VS1_1, and record a pointer to
6253 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
6254 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
6255 stmts and pointers:
6256 RELATED_STMT VEC_STMT
6257 VS1_0: vx0 = memref0 VS1_1 -
6258 VS1_1: vx1 = memref1 VS1_2 -
6259 VS1_2: vx2 = memref2 VS1_3 -
6260 VS1_3: vx3 = memref3 - -
6261 S1: x = load - VS1_0
6262 S2: z = x + 1 - -
6264 See in documentation in vect_get_vec_def_for_stmt_copy for how the
6265 information we recorded in RELATED_STMT field is used to vectorize
6266 stmt S2. */
6268 /* In case of interleaving (non-unit grouped access):
6270 S1: x2 = &base + 2
6271 S2: x0 = &base
6272 S3: x1 = &base + 1
6273 S4: x3 = &base + 3
6275 Vectorized loads are created in the order of memory accesses
6276 starting from the access of the first stmt of the chain:
6278 VS1: vx0 = &base
6279 VS2: vx1 = &base + vec_size*1
6280 VS3: vx3 = &base + vec_size*2
6281 VS4: vx4 = &base + vec_size*3
6283 Then permutation statements are generated:
6285 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
6286 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
6289 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6290 (the order of the data-refs in the output of vect_permute_load_chain
6291 corresponds to the order of scalar stmts in the interleaving chain - see
6292 the documentation of vect_permute_load_chain()).
6293 The generation of permutation stmts and recording them in
6294 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
6296 In case of both multiple types and interleaving, the vector loads and
6297 permutation stmts above are created for every copy. The result vector
6298 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
6299 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
6301 /* If the data reference is aligned (dr_aligned) or potentially unaligned
6302 on a target that supports unaligned accesses (dr_unaligned_supported)
6303 we generate the following code:
6304 p = initial_addr;
6305 indx = 0;
6306 loop {
6307 p = p + indx * vectype_size;
6308 vec_dest = *(p);
6309 indx = indx + 1;
6312 Otherwise, the data reference is potentially unaligned on a target that
6313 does not support unaligned accesses (dr_explicit_realign_optimized) -
6314 then generate the following code, in which the data in each iteration is
6315 obtained by two vector loads, one from the previous iteration, and one
6316 from the current iteration:
6317 p1 = initial_addr;
6318 msq_init = *(floor(p1))
6319 p2 = initial_addr + VS - 1;
6320 realignment_token = call target_builtin;
6321 indx = 0;
6322 loop {
6323 p2 = p2 + indx * vectype_size
6324 lsq = *(floor(p2))
6325 vec_dest = realign_load (msq, lsq, realignment_token)
6326 indx = indx + 1;
6327 msq = lsq;
6328 } */
6330 /* If the misalignment remains the same throughout the execution of the
6331 loop, we can create the init_addr and permutation mask at the loop
6332 preheader. Otherwise, it needs to be created inside the loop.
6333 This can only occur when vectorizing memory accesses in the inner-loop
6334 nested within an outer-loop that is being vectorized. */
6336 if (nested_in_vect_loop
6337 && (TREE_INT_CST_LOW (DR_STEP (dr))
6338 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
6340 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
6341 compute_in_loop = true;
6344 if ((alignment_support_scheme == dr_explicit_realign_optimized
6345 || alignment_support_scheme == dr_explicit_realign)
6346 && !compute_in_loop)
6348 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
6349 alignment_support_scheme, NULL_TREE,
6350 &at_loop);
6351 if (alignment_support_scheme == dr_explicit_realign_optimized)
6353 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (msq));
6354 byte_offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
6355 size_one_node);
6358 else
6359 at_loop = loop;
6361 if (negative)
6362 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
6364 if (load_lanes_p)
6365 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
6366 else
6367 aggr_type = vectype;
6369 prev_stmt_info = NULL;
6370 for (j = 0; j < ncopies; j++)
6372 /* 1. Create the vector or array pointer update chain. */
6373 if (j == 0)
6375 bool simd_lane_access_p
6376 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
6377 if (simd_lane_access_p
6378 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
6379 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
6380 && integer_zerop (DR_OFFSET (first_dr))
6381 && integer_zerop (DR_INIT (first_dr))
6382 && alias_sets_conflict_p (get_alias_set (aggr_type),
6383 get_alias_set (DR_REF (first_dr)))
6384 && (alignment_support_scheme == dr_aligned
6385 || alignment_support_scheme == dr_unaligned_supported))
6387 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
6388 dataref_offset = build_int_cst (reference_alias_ptr_type
6389 (DR_REF (first_dr)), 0);
6390 inv_p = false;
6392 else
6393 dataref_ptr
6394 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
6395 offset, &dummy, gsi, &ptr_incr,
6396 simd_lane_access_p, &inv_p,
6397 byte_offset);
6399 else if (dataref_offset)
6400 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
6401 TYPE_SIZE_UNIT (aggr_type));
6402 else
6403 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
6404 TYPE_SIZE_UNIT (aggr_type));
6406 if (grouped_load || slp_perm)
6407 dr_chain.create (vec_num);
6409 if (load_lanes_p)
6411 tree vec_array;
6413 vec_array = create_vector_array (vectype, vec_num);
6415 /* Emit:
6416 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
6417 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
6418 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
6419 gimple_call_set_lhs (new_stmt, vec_array);
6420 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6422 /* Extract each vector into an SSA_NAME. */
6423 for (i = 0; i < vec_num; i++)
6425 new_temp = read_vector_array (stmt, gsi, scalar_dest,
6426 vec_array, i);
6427 dr_chain.quick_push (new_temp);
6430 /* Record the mapping between SSA_NAMEs and statements. */
6431 vect_record_grouped_load_vectors (stmt, dr_chain);
6433 else
6435 for (i = 0; i < vec_num; i++)
6437 if (i > 0)
6438 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6439 stmt, NULL_TREE);
6441 /* 2. Create the vector-load in the loop. */
6442 switch (alignment_support_scheme)
6444 case dr_aligned:
6445 case dr_unaligned_supported:
6447 unsigned int align, misalign;
6449 data_ref
6450 = build2 (MEM_REF, vectype, dataref_ptr,
6451 dataref_offset
6452 ? dataref_offset
6453 : build_int_cst (reference_alias_ptr_type
6454 (DR_REF (first_dr)), 0));
6455 align = TYPE_ALIGN_UNIT (vectype);
6456 if (alignment_support_scheme == dr_aligned)
6458 gcc_assert (aligned_access_p (first_dr));
6459 misalign = 0;
6461 else if (DR_MISALIGNMENT (first_dr) == -1)
6463 TREE_TYPE (data_ref)
6464 = build_aligned_type (TREE_TYPE (data_ref),
6465 TYPE_ALIGN (elem_type));
6466 align = TYPE_ALIGN_UNIT (elem_type);
6467 misalign = 0;
6469 else
6471 TREE_TYPE (data_ref)
6472 = build_aligned_type (TREE_TYPE (data_ref),
6473 TYPE_ALIGN (elem_type));
6474 misalign = DR_MISALIGNMENT (first_dr);
6476 if (dataref_offset == NULL_TREE)
6477 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
6478 align, misalign);
6479 break;
6481 case dr_explicit_realign:
6483 tree ptr, bump;
6485 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
6487 if (compute_in_loop)
6488 msq = vect_setup_realignment (first_stmt, gsi,
6489 &realignment_token,
6490 dr_explicit_realign,
6491 dataref_ptr, NULL);
6493 ptr = copy_ssa_name (dataref_ptr);
6494 new_stmt = gimple_build_assign
6495 (ptr, BIT_AND_EXPR, dataref_ptr,
6496 build_int_cst
6497 (TREE_TYPE (dataref_ptr),
6498 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6499 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6500 data_ref
6501 = build2 (MEM_REF, vectype, ptr,
6502 build_int_cst (reference_alias_ptr_type
6503 (DR_REF (first_dr)), 0));
6504 vec_dest = vect_create_destination_var (scalar_dest,
6505 vectype);
6506 new_stmt = gimple_build_assign (vec_dest, data_ref);
6507 new_temp = make_ssa_name (vec_dest, new_stmt);
6508 gimple_assign_set_lhs (new_stmt, new_temp);
6509 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
6510 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
6511 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6512 msq = new_temp;
6514 bump = size_binop (MULT_EXPR, vs,
6515 TYPE_SIZE_UNIT (elem_type));
6516 bump = size_binop (MINUS_EXPR, bump, size_one_node);
6517 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
6518 new_stmt = gimple_build_assign
6519 (NULL_TREE, BIT_AND_EXPR, ptr,
6520 build_int_cst
6521 (TREE_TYPE (ptr),
6522 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6523 ptr = copy_ssa_name (dataref_ptr, new_stmt);
6524 gimple_assign_set_lhs (new_stmt, ptr);
6525 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6526 data_ref
6527 = build2 (MEM_REF, vectype, ptr,
6528 build_int_cst (reference_alias_ptr_type
6529 (DR_REF (first_dr)), 0));
6530 break;
6532 case dr_explicit_realign_optimized:
6533 new_temp = copy_ssa_name (dataref_ptr);
6534 new_stmt = gimple_build_assign
6535 (new_temp, BIT_AND_EXPR, dataref_ptr,
6536 build_int_cst
6537 (TREE_TYPE (dataref_ptr),
6538 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6539 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6540 data_ref
6541 = build2 (MEM_REF, vectype, new_temp,
6542 build_int_cst (reference_alias_ptr_type
6543 (DR_REF (first_dr)), 0));
6544 break;
6545 default:
6546 gcc_unreachable ();
6548 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6549 new_stmt = gimple_build_assign (vec_dest, data_ref);
6550 new_temp = make_ssa_name (vec_dest, new_stmt);
6551 gimple_assign_set_lhs (new_stmt, new_temp);
6552 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6554 /* 3. Handle explicit realignment if necessary/supported.
6555 Create in loop:
6556 vec_dest = realign_load (msq, lsq, realignment_token) */
6557 if (alignment_support_scheme == dr_explicit_realign_optimized
6558 || alignment_support_scheme == dr_explicit_realign)
6560 lsq = gimple_assign_lhs (new_stmt);
6561 if (!realignment_token)
6562 realignment_token = dataref_ptr;
6563 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6564 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR,
6565 msq, lsq, realignment_token);
6566 new_temp = make_ssa_name (vec_dest, new_stmt);
6567 gimple_assign_set_lhs (new_stmt, new_temp);
6568 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6570 if (alignment_support_scheme == dr_explicit_realign_optimized)
6572 gcc_assert (phi);
6573 if (i == vec_num - 1 && j == ncopies - 1)
6574 add_phi_arg (phi, lsq,
6575 loop_latch_edge (containing_loop),
6576 UNKNOWN_LOCATION);
6577 msq = lsq;
6581 /* 4. Handle invariant-load. */
6582 if (inv_p && !bb_vinfo)
6584 gcc_assert (!grouped_load);
6585 /* If we have versioned for aliasing or the loop doesn't
6586 have any data dependencies that would preclude this,
6587 then we are sure this is a loop invariant load and
6588 thus we can insert it on the preheader edge. */
6589 if (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
6590 && !nested_in_vect_loop
6591 && hoist_defs_of_uses (stmt, loop))
6593 if (dump_enabled_p ())
6595 dump_printf_loc (MSG_NOTE, vect_location,
6596 "hoisting out of the vectorized "
6597 "loop: ");
6598 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6600 tree tem = copy_ssa_name (scalar_dest);
6601 gsi_insert_on_edge_immediate
6602 (loop_preheader_edge (loop),
6603 gimple_build_assign (tem,
6604 unshare_expr
6605 (gimple_assign_rhs1 (stmt))));
6606 new_temp = vect_init_vector (stmt, tem, vectype, NULL);
6608 else
6610 gimple_stmt_iterator gsi2 = *gsi;
6611 gsi_next (&gsi2);
6612 new_temp = vect_init_vector (stmt, scalar_dest,
6613 vectype, &gsi2);
6615 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6616 set_vinfo_for_stmt (new_stmt,
6617 new_stmt_vec_info (new_stmt, loop_vinfo,
6618 bb_vinfo));
6621 if (negative)
6623 tree perm_mask = perm_mask_for_reverse (vectype);
6624 new_temp = permute_vec_elements (new_temp, new_temp,
6625 perm_mask, stmt, gsi);
6626 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6629 /* Collect vector loads and later create their permutation in
6630 vect_transform_grouped_load (). */
6631 if (grouped_load || slp_perm)
6632 dr_chain.quick_push (new_temp);
6634 /* Store vector loads in the corresponding SLP_NODE. */
6635 if (slp && !slp_perm)
6636 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6638 /* Bump the vector pointer to account for a gap. */
6639 if (slp && group_gap != 0)
6641 tree bump = size_binop (MULT_EXPR,
6642 TYPE_SIZE_UNIT (elem_type),
6643 size_int (group_gap));
6644 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6645 stmt, bump);
6649 if (slp && !slp_perm)
6650 continue;
6652 if (slp_perm)
6654 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
6655 slp_node_instance, false))
6657 dr_chain.release ();
6658 return false;
6661 else
6663 if (grouped_load)
6665 if (!load_lanes_p)
6666 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
6667 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6669 else
6671 if (j == 0)
6672 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6673 else
6674 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6675 prev_stmt_info = vinfo_for_stmt (new_stmt);
6678 dr_chain.release ();
6681 return true;
6684 /* Function vect_is_simple_cond.
6686 Input:
6687 LOOP - the loop that is being vectorized.
6688 COND - Condition that is checked for simple use.
6690 Output:
6691 *COMP_VECTYPE - the vector type for the comparison.
6693 Returns whether a COND can be vectorized. Checks whether
6694 condition operands are supportable using vec_is_simple_use. */
6696 static bool
6697 vect_is_simple_cond (tree cond, gimple stmt, loop_vec_info loop_vinfo,
6698 bb_vec_info bb_vinfo, tree *comp_vectype)
6700 tree lhs, rhs;
6701 tree def;
6702 enum vect_def_type dt;
6703 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
6705 if (!COMPARISON_CLASS_P (cond))
6706 return false;
6708 lhs = TREE_OPERAND (cond, 0);
6709 rhs = TREE_OPERAND (cond, 1);
6711 if (TREE_CODE (lhs) == SSA_NAME)
6713 gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
6714 if (!vect_is_simple_use_1 (lhs, stmt, loop_vinfo, bb_vinfo,
6715 &lhs_def_stmt, &def, &dt, &vectype1))
6716 return false;
6718 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
6719 && TREE_CODE (lhs) != FIXED_CST)
6720 return false;
6722 if (TREE_CODE (rhs) == SSA_NAME)
6724 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
6725 if (!vect_is_simple_use_1 (rhs, stmt, loop_vinfo, bb_vinfo,
6726 &rhs_def_stmt, &def, &dt, &vectype2))
6727 return false;
6729 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
6730 && TREE_CODE (rhs) != FIXED_CST)
6731 return false;
6733 *comp_vectype = vectype1 ? vectype1 : vectype2;
6734 return true;
6737 /* vectorizable_condition.
6739 Check if STMT is conditional modify expression that can be vectorized.
6740 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
6741 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
6742 at GSI.
6744 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
6745 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
6746 else caluse if it is 2).
6748 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6750 bool
6751 vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
6752 gimple *vec_stmt, tree reduc_def, int reduc_index,
6753 slp_tree slp_node)
6755 tree scalar_dest = NULL_TREE;
6756 tree vec_dest = NULL_TREE;
6757 tree cond_expr, then_clause, else_clause;
6758 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6759 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6760 tree comp_vectype = NULL_TREE;
6761 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
6762 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
6763 tree vec_compare, vec_cond_expr;
6764 tree new_temp;
6765 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6766 tree def;
6767 enum vect_def_type dt, dts[4];
6768 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6769 int ncopies;
6770 enum tree_code code;
6771 stmt_vec_info prev_stmt_info = NULL;
6772 int i, j;
6773 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6774 vec<tree> vec_oprnds0 = vNULL;
6775 vec<tree> vec_oprnds1 = vNULL;
6776 vec<tree> vec_oprnds2 = vNULL;
6777 vec<tree> vec_oprnds3 = vNULL;
6778 tree vec_cmp_type;
6780 if (slp_node || PURE_SLP_STMT (stmt_info))
6781 ncopies = 1;
6782 else
6783 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6785 gcc_assert (ncopies >= 1);
6786 if (reduc_index && ncopies > 1)
6787 return false; /* FORNOW */
6789 if (reduc_index && STMT_SLP_TYPE (stmt_info))
6790 return false;
6792 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6793 return false;
6795 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6796 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
6797 && reduc_def))
6798 return false;
6800 /* FORNOW: not yet supported. */
6801 if (STMT_VINFO_LIVE_P (stmt_info))
6803 if (dump_enabled_p ())
6804 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6805 "value used after loop.\n");
6806 return false;
6809 /* Is vectorizable conditional operation? */
6810 if (!is_gimple_assign (stmt))
6811 return false;
6813 code = gimple_assign_rhs_code (stmt);
6815 if (code != COND_EXPR)
6816 return false;
6818 cond_expr = gimple_assign_rhs1 (stmt);
6819 then_clause = gimple_assign_rhs2 (stmt);
6820 else_clause = gimple_assign_rhs3 (stmt);
6822 if (!vect_is_simple_cond (cond_expr, stmt, loop_vinfo, bb_vinfo,
6823 &comp_vectype)
6824 || !comp_vectype)
6825 return false;
6827 if (TREE_CODE (then_clause) == SSA_NAME)
6829 gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
6830 if (!vect_is_simple_use (then_clause, stmt, loop_vinfo, bb_vinfo,
6831 &then_def_stmt, &def, &dt))
6832 return false;
6834 else if (TREE_CODE (then_clause) != INTEGER_CST
6835 && TREE_CODE (then_clause) != REAL_CST
6836 && TREE_CODE (then_clause) != FIXED_CST)
6837 return false;
6839 if (TREE_CODE (else_clause) == SSA_NAME)
6841 gimple else_def_stmt = SSA_NAME_DEF_STMT (else_clause);
6842 if (!vect_is_simple_use (else_clause, stmt, loop_vinfo, bb_vinfo,
6843 &else_def_stmt, &def, &dt))
6844 return false;
6846 else if (TREE_CODE (else_clause) != INTEGER_CST
6847 && TREE_CODE (else_clause) != REAL_CST
6848 && TREE_CODE (else_clause) != FIXED_CST)
6849 return false;
6851 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype)));
6852 /* The result of a vector comparison should be signed type. */
6853 tree cmp_type = build_nonstandard_integer_type (prec, 0);
6854 vec_cmp_type = get_same_sized_vectype (cmp_type, vectype);
6855 if (vec_cmp_type == NULL_TREE)
6856 return false;
6858 if (!vec_stmt)
6860 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
6861 return expand_vec_cond_expr_p (vectype, comp_vectype);
6864 /* Transform. */
6866 if (!slp_node)
6868 vec_oprnds0.create (1);
6869 vec_oprnds1.create (1);
6870 vec_oprnds2.create (1);
6871 vec_oprnds3.create (1);
6874 /* Handle def. */
6875 scalar_dest = gimple_assign_lhs (stmt);
6876 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6878 /* Handle cond expr. */
6879 for (j = 0; j < ncopies; j++)
6881 gassign *new_stmt = NULL;
6882 if (j == 0)
6884 if (slp_node)
6886 auto_vec<tree, 4> ops;
6887 auto_vec<vec<tree>, 4> vec_defs;
6889 ops.safe_push (TREE_OPERAND (cond_expr, 0));
6890 ops.safe_push (TREE_OPERAND (cond_expr, 1));
6891 ops.safe_push (then_clause);
6892 ops.safe_push (else_clause);
6893 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
6894 vec_oprnds3 = vec_defs.pop ();
6895 vec_oprnds2 = vec_defs.pop ();
6896 vec_oprnds1 = vec_defs.pop ();
6897 vec_oprnds0 = vec_defs.pop ();
6899 ops.release ();
6900 vec_defs.release ();
6902 else
6904 gimple gtemp;
6905 vec_cond_lhs =
6906 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
6907 stmt, NULL);
6908 vect_is_simple_use (TREE_OPERAND (cond_expr, 0), stmt,
6909 loop_vinfo, NULL, &gtemp, &def, &dts[0]);
6911 vec_cond_rhs =
6912 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
6913 stmt, NULL);
6914 vect_is_simple_use (TREE_OPERAND (cond_expr, 1), stmt,
6915 loop_vinfo, NULL, &gtemp, &def, &dts[1]);
6916 if (reduc_index == 1)
6917 vec_then_clause = reduc_def;
6918 else
6920 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
6921 stmt, NULL);
6922 vect_is_simple_use (then_clause, stmt, loop_vinfo,
6923 NULL, &gtemp, &def, &dts[2]);
6925 if (reduc_index == 2)
6926 vec_else_clause = reduc_def;
6927 else
6929 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
6930 stmt, NULL);
6931 vect_is_simple_use (else_clause, stmt, loop_vinfo,
6932 NULL, &gtemp, &def, &dts[3]);
6936 else
6938 vec_cond_lhs = vect_get_vec_def_for_stmt_copy (dts[0],
6939 vec_oprnds0.pop ());
6940 vec_cond_rhs = vect_get_vec_def_for_stmt_copy (dts[1],
6941 vec_oprnds1.pop ());
6942 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
6943 vec_oprnds2.pop ());
6944 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
6945 vec_oprnds3.pop ());
6948 if (!slp_node)
6950 vec_oprnds0.quick_push (vec_cond_lhs);
6951 vec_oprnds1.quick_push (vec_cond_rhs);
6952 vec_oprnds2.quick_push (vec_then_clause);
6953 vec_oprnds3.quick_push (vec_else_clause);
6956 /* Arguments are ready. Create the new vector stmt. */
6957 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
6959 vec_cond_rhs = vec_oprnds1[i];
6960 vec_then_clause = vec_oprnds2[i];
6961 vec_else_clause = vec_oprnds3[i];
6963 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
6964 vec_cond_lhs, vec_cond_rhs);
6965 vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
6966 vec_compare, vec_then_clause, vec_else_clause);
6968 new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
6969 new_temp = make_ssa_name (vec_dest, new_stmt);
6970 gimple_assign_set_lhs (new_stmt, new_temp);
6971 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6972 if (slp_node)
6973 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6976 if (slp_node)
6977 continue;
6979 if (j == 0)
6980 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6981 else
6982 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6984 prev_stmt_info = vinfo_for_stmt (new_stmt);
6987 vec_oprnds0.release ();
6988 vec_oprnds1.release ();
6989 vec_oprnds2.release ();
6990 vec_oprnds3.release ();
6992 return true;
6996 /* Make sure the statement is vectorizable. */
6998 bool
6999 vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
7001 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7002 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7003 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
7004 bool ok;
7005 tree scalar_type, vectype;
7006 gimple pattern_stmt;
7007 gimple_seq pattern_def_seq;
7009 if (dump_enabled_p ())
7011 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
7012 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7015 if (gimple_has_volatile_ops (stmt))
7017 if (dump_enabled_p ())
7018 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7019 "not vectorized: stmt has volatile operands\n");
7021 return false;
7024 /* Skip stmts that do not need to be vectorized. In loops this is expected
7025 to include:
7026 - the COND_EXPR which is the loop exit condition
7027 - any LABEL_EXPRs in the loop
7028 - computations that are used only for array indexing or loop control.
7029 In basic blocks we only analyze statements that are a part of some SLP
7030 instance, therefore, all the statements are relevant.
7032 Pattern statement needs to be analyzed instead of the original statement
7033 if the original statement is not relevant. Otherwise, we analyze both
7034 statements. In basic blocks we are called from some SLP instance
7035 traversal, don't analyze pattern stmts instead, the pattern stmts
7036 already will be part of SLP instance. */
7038 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
7039 if (!STMT_VINFO_RELEVANT_P (stmt_info)
7040 && !STMT_VINFO_LIVE_P (stmt_info))
7042 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7043 && pattern_stmt
7044 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7045 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7047 /* Analyze PATTERN_STMT instead of the original stmt. */
7048 stmt = pattern_stmt;
7049 stmt_info = vinfo_for_stmt (pattern_stmt);
7050 if (dump_enabled_p ())
7052 dump_printf_loc (MSG_NOTE, vect_location,
7053 "==> examining pattern statement: ");
7054 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7057 else
7059 if (dump_enabled_p ())
7060 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
7062 return true;
7065 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7066 && node == NULL
7067 && pattern_stmt
7068 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7069 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7071 /* Analyze PATTERN_STMT too. */
7072 if (dump_enabled_p ())
7074 dump_printf_loc (MSG_NOTE, vect_location,
7075 "==> examining pattern statement: ");
7076 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7079 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
7080 return false;
7083 if (is_pattern_stmt_p (stmt_info)
7084 && node == NULL
7085 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
7087 gimple_stmt_iterator si;
7089 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
7091 gimple pattern_def_stmt = gsi_stmt (si);
7092 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
7093 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
7095 /* Analyze def stmt of STMT if it's a pattern stmt. */
7096 if (dump_enabled_p ())
7098 dump_printf_loc (MSG_NOTE, vect_location,
7099 "==> examining pattern def statement: ");
7100 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
7103 if (!vect_analyze_stmt (pattern_def_stmt,
7104 need_to_vectorize, node))
7105 return false;
7110 switch (STMT_VINFO_DEF_TYPE (stmt_info))
7112 case vect_internal_def:
7113 break;
7115 case vect_reduction_def:
7116 case vect_nested_cycle:
7117 gcc_assert (!bb_vinfo && (relevance == vect_used_in_outer
7118 || relevance == vect_used_in_outer_by_reduction
7119 || relevance == vect_unused_in_scope));
7120 break;
7122 case vect_induction_def:
7123 case vect_constant_def:
7124 case vect_external_def:
7125 case vect_unknown_def_type:
7126 default:
7127 gcc_unreachable ();
7130 if (bb_vinfo)
7132 gcc_assert (PURE_SLP_STMT (stmt_info));
7134 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
7135 if (dump_enabled_p ())
7137 dump_printf_loc (MSG_NOTE, vect_location,
7138 "get vectype for scalar type: ");
7139 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
7140 dump_printf (MSG_NOTE, "\n");
7143 vectype = get_vectype_for_scalar_type (scalar_type);
7144 if (!vectype)
7146 if (dump_enabled_p ())
7148 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7149 "not SLPed: unsupported data-type ");
7150 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
7151 scalar_type);
7152 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
7154 return false;
7157 if (dump_enabled_p ())
7159 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
7160 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
7161 dump_printf (MSG_NOTE, "\n");
7164 STMT_VINFO_VECTYPE (stmt_info) = vectype;
7167 if (STMT_VINFO_RELEVANT_P (stmt_info))
7169 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
7170 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
7171 || (is_gimple_call (stmt)
7172 && gimple_call_lhs (stmt) == NULL_TREE));
7173 *need_to_vectorize = true;
7176 ok = true;
7177 if (!bb_vinfo
7178 && (STMT_VINFO_RELEVANT_P (stmt_info)
7179 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
7180 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, NULL)
7181 || vectorizable_conversion (stmt, NULL, NULL, NULL)
7182 || vectorizable_shift (stmt, NULL, NULL, NULL)
7183 || vectorizable_operation (stmt, NULL, NULL, NULL)
7184 || vectorizable_assignment (stmt, NULL, NULL, NULL)
7185 || vectorizable_load (stmt, NULL, NULL, NULL, NULL)
7186 || vectorizable_call (stmt, NULL, NULL, NULL)
7187 || vectorizable_store (stmt, NULL, NULL, NULL)
7188 || vectorizable_reduction (stmt, NULL, NULL, NULL)
7189 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, NULL));
7190 else
7192 if (bb_vinfo)
7193 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
7194 || vectorizable_conversion (stmt, NULL, NULL, node)
7195 || vectorizable_shift (stmt, NULL, NULL, node)
7196 || vectorizable_operation (stmt, NULL, NULL, node)
7197 || vectorizable_assignment (stmt, NULL, NULL, node)
7198 || vectorizable_load (stmt, NULL, NULL, node, NULL)
7199 || vectorizable_call (stmt, NULL, NULL, node)
7200 || vectorizable_store (stmt, NULL, NULL, node)
7201 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node));
7204 if (!ok)
7206 if (dump_enabled_p ())
7208 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7209 "not vectorized: relevant stmt not ");
7210 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
7211 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
7214 return false;
7217 if (bb_vinfo)
7218 return true;
7220 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
7221 need extra handling, except for vectorizable reductions. */
7222 if (STMT_VINFO_LIVE_P (stmt_info)
7223 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
7224 ok = vectorizable_live_operation (stmt, NULL, NULL);
7226 if (!ok)
7228 if (dump_enabled_p ())
7230 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7231 "not vectorized: live stmt not ");
7232 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
7233 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
7236 return false;
7239 return true;
7243 /* Function vect_transform_stmt.
7245 Create a vectorized stmt to replace STMT, and insert it at BSI. */
7247 bool
7248 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
7249 bool *grouped_store, slp_tree slp_node,
7250 slp_instance slp_node_instance)
7252 bool is_store = false;
7253 gimple vec_stmt = NULL;
7254 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7255 bool done;
7257 switch (STMT_VINFO_TYPE (stmt_info))
7259 case type_demotion_vec_info_type:
7260 case type_promotion_vec_info_type:
7261 case type_conversion_vec_info_type:
7262 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
7263 gcc_assert (done);
7264 break;
7266 case induc_vec_info_type:
7267 gcc_assert (!slp_node);
7268 done = vectorizable_induction (stmt, gsi, &vec_stmt);
7269 gcc_assert (done);
7270 break;
7272 case shift_vec_info_type:
7273 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
7274 gcc_assert (done);
7275 break;
7277 case op_vec_info_type:
7278 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
7279 gcc_assert (done);
7280 break;
7282 case assignment_vec_info_type:
7283 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
7284 gcc_assert (done);
7285 break;
7287 case load_vec_info_type:
7288 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
7289 slp_node_instance);
7290 gcc_assert (done);
7291 break;
7293 case store_vec_info_type:
7294 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
7295 gcc_assert (done);
7296 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
7298 /* In case of interleaving, the whole chain is vectorized when the
7299 last store in the chain is reached. Store stmts before the last
7300 one are skipped, and there vec_stmt_info shouldn't be freed
7301 meanwhile. */
7302 *grouped_store = true;
7303 if (STMT_VINFO_VEC_STMT (stmt_info))
7304 is_store = true;
7306 else
7307 is_store = true;
7308 break;
7310 case condition_vec_info_type:
7311 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
7312 gcc_assert (done);
7313 break;
7315 case call_vec_info_type:
7316 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
7317 stmt = gsi_stmt (*gsi);
7318 if (is_gimple_call (stmt)
7319 && gimple_call_internal_p (stmt)
7320 && gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
7321 is_store = true;
7322 break;
7324 case call_simd_clone_vec_info_type:
7325 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
7326 stmt = gsi_stmt (*gsi);
7327 break;
7329 case reduc_vec_info_type:
7330 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
7331 gcc_assert (done);
7332 break;
7334 default:
7335 if (!STMT_VINFO_LIVE_P (stmt_info))
7337 if (dump_enabled_p ())
7338 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7339 "stmt not supported.\n");
7340 gcc_unreachable ();
7344 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
7345 is being vectorized, but outside the immediately enclosing loop. */
7346 if (vec_stmt
7347 && STMT_VINFO_LOOP_VINFO (stmt_info)
7348 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
7349 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
7350 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
7351 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
7352 || STMT_VINFO_RELEVANT (stmt_info) ==
7353 vect_used_in_outer_by_reduction))
7355 struct loop *innerloop = LOOP_VINFO_LOOP (
7356 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
7357 imm_use_iterator imm_iter;
7358 use_operand_p use_p;
7359 tree scalar_dest;
7360 gimple exit_phi;
7362 if (dump_enabled_p ())
7363 dump_printf_loc (MSG_NOTE, vect_location,
7364 "Record the vdef for outer-loop vectorization.\n");
7366 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
7367 (to be used when vectorizing outer-loop stmts that use the DEF of
7368 STMT). */
7369 if (gimple_code (stmt) == GIMPLE_PHI)
7370 scalar_dest = PHI_RESULT (stmt);
7371 else
7372 scalar_dest = gimple_assign_lhs (stmt);
7374 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
7376 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
7378 exit_phi = USE_STMT (use_p);
7379 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
7384 /* Handle stmts whose DEF is used outside the loop-nest that is
7385 being vectorized. */
7386 if (STMT_VINFO_LIVE_P (stmt_info)
7387 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
7389 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
7390 gcc_assert (done);
7393 if (vec_stmt)
7394 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
7396 return is_store;
7400 /* Remove a group of stores (for SLP or interleaving), free their
7401 stmt_vec_info. */
7403 void
7404 vect_remove_stores (gimple first_stmt)
7406 gimple next = first_stmt;
7407 gimple tmp;
7408 gimple_stmt_iterator next_si;
7410 while (next)
7412 stmt_vec_info stmt_info = vinfo_for_stmt (next);
7414 tmp = GROUP_NEXT_ELEMENT (stmt_info);
7415 if (is_pattern_stmt_p (stmt_info))
7416 next = STMT_VINFO_RELATED_STMT (stmt_info);
7417 /* Free the attached stmt_vec_info and remove the stmt. */
7418 next_si = gsi_for_stmt (next);
7419 unlink_stmt_vdef (next);
7420 gsi_remove (&next_si, true);
7421 release_defs (next);
7422 free_stmt_vec_info (next);
7423 next = tmp;
7428 /* Function new_stmt_vec_info.
7430 Create and initialize a new stmt_vec_info struct for STMT. */
7432 stmt_vec_info
7433 new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
7434 bb_vec_info bb_vinfo)
7436 stmt_vec_info res;
7437 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
7439 STMT_VINFO_TYPE (res) = undef_vec_info_type;
7440 STMT_VINFO_STMT (res) = stmt;
7441 STMT_VINFO_LOOP_VINFO (res) = loop_vinfo;
7442 STMT_VINFO_BB_VINFO (res) = bb_vinfo;
7443 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
7444 STMT_VINFO_LIVE_P (res) = false;
7445 STMT_VINFO_VECTYPE (res) = NULL;
7446 STMT_VINFO_VEC_STMT (res) = NULL;
7447 STMT_VINFO_VECTORIZABLE (res) = true;
7448 STMT_VINFO_IN_PATTERN_P (res) = false;
7449 STMT_VINFO_RELATED_STMT (res) = NULL;
7450 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
7451 STMT_VINFO_DATA_REF (res) = NULL;
7453 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
7454 STMT_VINFO_DR_OFFSET (res) = NULL;
7455 STMT_VINFO_DR_INIT (res) = NULL;
7456 STMT_VINFO_DR_STEP (res) = NULL;
7457 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
7459 if (gimple_code (stmt) == GIMPLE_PHI
7460 && is_loop_header_bb_p (gimple_bb (stmt)))
7461 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
7462 else
7463 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
7465 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
7466 STMT_SLP_TYPE (res) = loop_vect;
7467 GROUP_FIRST_ELEMENT (res) = NULL;
7468 GROUP_NEXT_ELEMENT (res) = NULL;
7469 GROUP_SIZE (res) = 0;
7470 GROUP_STORE_COUNT (res) = 0;
7471 GROUP_GAP (res) = 0;
7472 GROUP_SAME_DR_STMT (res) = NULL;
7474 return res;
7478 /* Create a hash table for stmt_vec_info. */
7480 void
7481 init_stmt_vec_info_vec (void)
7483 gcc_assert (!stmt_vec_info_vec.exists ());
7484 stmt_vec_info_vec.create (50);
7488 /* Free hash table for stmt_vec_info. */
7490 void
7491 free_stmt_vec_info_vec (void)
7493 unsigned int i;
7494 vec_void_p info;
7495 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
7496 if (info != NULL)
7497 free_stmt_vec_info (STMT_VINFO_STMT ((stmt_vec_info) info));
7498 gcc_assert (stmt_vec_info_vec.exists ());
7499 stmt_vec_info_vec.release ();
7503 /* Free stmt vectorization related info. */
7505 void
7506 free_stmt_vec_info (gimple stmt)
7508 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7510 if (!stmt_info)
7511 return;
7513 /* Check if this statement has a related "pattern stmt"
7514 (introduced by the vectorizer during the pattern recognition
7515 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
7516 too. */
7517 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
7519 stmt_vec_info patt_info
7520 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
7521 if (patt_info)
7523 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
7524 gimple patt_stmt = STMT_VINFO_STMT (patt_info);
7525 gimple_set_bb (patt_stmt, NULL);
7526 tree lhs = gimple_get_lhs (patt_stmt);
7527 if (TREE_CODE (lhs) == SSA_NAME)
7528 release_ssa_name (lhs);
7529 if (seq)
7531 gimple_stmt_iterator si;
7532 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
7534 gimple seq_stmt = gsi_stmt (si);
7535 gimple_set_bb (seq_stmt, NULL);
7536 lhs = gimple_get_lhs (patt_stmt);
7537 if (TREE_CODE (lhs) == SSA_NAME)
7538 release_ssa_name (lhs);
7539 free_stmt_vec_info (seq_stmt);
7542 free_stmt_vec_info (patt_stmt);
7546 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
7547 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
7548 set_vinfo_for_stmt (stmt, NULL);
7549 free (stmt_info);
7553 /* Function get_vectype_for_scalar_type_and_size.
7555 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
7556 by the target. */
7558 static tree
7559 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
7561 machine_mode inner_mode = TYPE_MODE (scalar_type);
7562 machine_mode simd_mode;
7563 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
7564 int nunits;
7565 tree vectype;
7567 if (nbytes == 0)
7568 return NULL_TREE;
7570 if (GET_MODE_CLASS (inner_mode) != MODE_INT
7571 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
7572 return NULL_TREE;
7574 /* For vector types of elements whose mode precision doesn't
7575 match their types precision we use a element type of mode
7576 precision. The vectorization routines will have to make sure
7577 they support the proper result truncation/extension.
7578 We also make sure to build vector types with INTEGER_TYPE
7579 component type only. */
7580 if (INTEGRAL_TYPE_P (scalar_type)
7581 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
7582 || TREE_CODE (scalar_type) != INTEGER_TYPE))
7583 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
7584 TYPE_UNSIGNED (scalar_type));
7586 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
7587 When the component mode passes the above test simply use a type
7588 corresponding to that mode. The theory is that any use that
7589 would cause problems with this will disable vectorization anyway. */
7590 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
7591 && !INTEGRAL_TYPE_P (scalar_type))
7592 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
7594 /* We can't build a vector type of elements with alignment bigger than
7595 their size. */
7596 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
7597 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
7598 TYPE_UNSIGNED (scalar_type));
7600 /* If we felt back to using the mode fail if there was
7601 no scalar type for it. */
7602 if (scalar_type == NULL_TREE)
7603 return NULL_TREE;
7605 /* If no size was supplied use the mode the target prefers. Otherwise
7606 lookup a vector mode of the specified size. */
7607 if (size == 0)
7608 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
7609 else
7610 simd_mode = mode_for_vector (inner_mode, size / nbytes);
7611 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
7612 if (nunits <= 1)
7613 return NULL_TREE;
7615 vectype = build_vector_type (scalar_type, nunits);
7617 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
7618 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
7619 return NULL_TREE;
7621 return vectype;
7624 unsigned int current_vector_size;
7626 /* Function get_vectype_for_scalar_type.
7628 Returns the vector type corresponding to SCALAR_TYPE as supported
7629 by the target. */
7631 tree
7632 get_vectype_for_scalar_type (tree scalar_type)
7634 tree vectype;
7635 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
7636 current_vector_size);
7637 if (vectype
7638 && current_vector_size == 0)
7639 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
7640 return vectype;
7643 /* Function get_same_sized_vectype
7645 Returns a vector type corresponding to SCALAR_TYPE of size
7646 VECTOR_TYPE if supported by the target. */
7648 tree
7649 get_same_sized_vectype (tree scalar_type, tree vector_type)
7651 return get_vectype_for_scalar_type_and_size
7652 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
7655 /* Function vect_is_simple_use.
7657 Input:
7658 LOOP_VINFO - the vect info of the loop that is being vectorized.
7659 BB_VINFO - the vect info of the basic block that is being vectorized.
7660 OPERAND - operand of STMT in the loop or bb.
7661 DEF - the defining stmt in case OPERAND is an SSA_NAME.
7663 Returns whether a stmt with OPERAND can be vectorized.
7664 For loops, supportable operands are constants, loop invariants, and operands
7665 that are defined by the current iteration of the loop. Unsupportable
7666 operands are those that are defined by a previous iteration of the loop (as
7667 is the case in reduction/induction computations).
7668 For basic blocks, supportable operands are constants and bb invariants.
7669 For now, operands defined outside the basic block are not supported. */
7671 bool
7672 vect_is_simple_use (tree operand, gimple stmt, loop_vec_info loop_vinfo,
7673 bb_vec_info bb_vinfo, gimple *def_stmt,
7674 tree *def, enum vect_def_type *dt)
7676 basic_block bb;
7677 stmt_vec_info stmt_vinfo;
7678 struct loop *loop = NULL;
7680 if (loop_vinfo)
7681 loop = LOOP_VINFO_LOOP (loop_vinfo);
7683 *def_stmt = NULL;
7684 *def = NULL_TREE;
7686 if (dump_enabled_p ())
7688 dump_printf_loc (MSG_NOTE, vect_location,
7689 "vect_is_simple_use: operand ");
7690 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
7691 dump_printf (MSG_NOTE, "\n");
7694 if (CONSTANT_CLASS_P (operand))
7696 *dt = vect_constant_def;
7697 return true;
7700 if (is_gimple_min_invariant (operand))
7702 *def = operand;
7703 *dt = vect_external_def;
7704 return true;
7707 if (TREE_CODE (operand) == PAREN_EXPR)
7709 if (dump_enabled_p ())
7710 dump_printf_loc (MSG_NOTE, vect_location, "non-associatable copy.\n");
7711 operand = TREE_OPERAND (operand, 0);
7714 if (TREE_CODE (operand) != SSA_NAME)
7716 if (dump_enabled_p ())
7717 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7718 "not ssa-name.\n");
7719 return false;
7722 *def_stmt = SSA_NAME_DEF_STMT (operand);
7723 if (*def_stmt == NULL)
7725 if (dump_enabled_p ())
7726 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7727 "no def_stmt.\n");
7728 return false;
7731 if (dump_enabled_p ())
7733 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
7734 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
7737 /* Empty stmt is expected only in case of a function argument.
7738 (Otherwise - we expect a phi_node or a GIMPLE_ASSIGN). */
7739 if (gimple_nop_p (*def_stmt))
7741 *def = operand;
7742 *dt = vect_external_def;
7743 return true;
7746 bb = gimple_bb (*def_stmt);
7748 if ((loop && !flow_bb_inside_loop_p (loop, bb))
7749 || (!loop && bb != BB_VINFO_BB (bb_vinfo))
7750 || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI))
7751 *dt = vect_external_def;
7752 else
7754 stmt_vinfo = vinfo_for_stmt (*def_stmt);
7755 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
7758 if (dump_enabled_p ())
7760 dump_printf_loc (MSG_NOTE, vect_location, "type of def: ");
7761 switch (*dt)
7763 case vect_uninitialized_def:
7764 dump_printf (MSG_NOTE, "uninitialized\n");
7765 break;
7766 case vect_constant_def:
7767 dump_printf (MSG_NOTE, "constant\n");
7768 break;
7769 case vect_external_def:
7770 dump_printf (MSG_NOTE, "external\n");
7771 break;
7772 case vect_internal_def:
7773 dump_printf (MSG_NOTE, "internal\n");
7774 break;
7775 case vect_induction_def:
7776 dump_printf (MSG_NOTE, "induction\n");
7777 break;
7778 case vect_reduction_def:
7779 dump_printf (MSG_NOTE, "reduction\n");
7780 break;
7781 case vect_double_reduction_def:
7782 dump_printf (MSG_NOTE, "double reduction\n");
7783 break;
7784 case vect_nested_cycle:
7785 dump_printf (MSG_NOTE, "nested cycle\n");
7786 break;
7787 case vect_unknown_def_type:
7788 dump_printf (MSG_NOTE, "unknown\n");
7789 break;
7793 if (*dt == vect_unknown_def_type
7794 || (stmt
7795 && *dt == vect_double_reduction_def
7796 && gimple_code (stmt) != GIMPLE_PHI))
7798 if (dump_enabled_p ())
7799 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7800 "Unsupported pattern.\n");
7801 return false;
7804 switch (gimple_code (*def_stmt))
7806 case GIMPLE_PHI:
7807 *def = gimple_phi_result (*def_stmt);
7808 break;
7810 case GIMPLE_ASSIGN:
7811 *def = gimple_assign_lhs (*def_stmt);
7812 break;
7814 case GIMPLE_CALL:
7815 *def = gimple_call_lhs (*def_stmt);
7816 if (*def != NULL)
7817 break;
7818 /* FALLTHRU */
7819 default:
7820 if (dump_enabled_p ())
7821 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7822 "unsupported defining stmt:\n");
7823 return false;
7826 return true;
7829 /* Function vect_is_simple_use_1.
7831 Same as vect_is_simple_use_1 but also determines the vector operand
7832 type of OPERAND and stores it to *VECTYPE. If the definition of
7833 OPERAND is vect_uninitialized_def, vect_constant_def or
7834 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
7835 is responsible to compute the best suited vector type for the
7836 scalar operand. */
7838 bool
7839 vect_is_simple_use_1 (tree operand, gimple stmt, loop_vec_info loop_vinfo,
7840 bb_vec_info bb_vinfo, gimple *def_stmt,
7841 tree *def, enum vect_def_type *dt, tree *vectype)
7843 if (!vect_is_simple_use (operand, stmt, loop_vinfo, bb_vinfo, def_stmt,
7844 def, dt))
7845 return false;
7847 /* Now get a vector type if the def is internal, otherwise supply
7848 NULL_TREE and leave it up to the caller to figure out a proper
7849 type for the use stmt. */
7850 if (*dt == vect_internal_def
7851 || *dt == vect_induction_def
7852 || *dt == vect_reduction_def
7853 || *dt == vect_double_reduction_def
7854 || *dt == vect_nested_cycle)
7856 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
7858 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7859 && !STMT_VINFO_RELEVANT (stmt_info)
7860 && !STMT_VINFO_LIVE_P (stmt_info))
7861 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
7863 *vectype = STMT_VINFO_VECTYPE (stmt_info);
7864 gcc_assert (*vectype != NULL_TREE);
7866 else if (*dt == vect_uninitialized_def
7867 || *dt == vect_constant_def
7868 || *dt == vect_external_def)
7869 *vectype = NULL_TREE;
7870 else
7871 gcc_unreachable ();
7873 return true;
7877 /* Function supportable_widening_operation
7879 Check whether an operation represented by the code CODE is a
7880 widening operation that is supported by the target platform in
7881 vector form (i.e., when operating on arguments of type VECTYPE_IN
7882 producing a result of type VECTYPE_OUT).
7884 Widening operations we currently support are NOP (CONVERT), FLOAT
7885 and WIDEN_MULT. This function checks if these operations are supported
7886 by the target platform either directly (via vector tree-codes), or via
7887 target builtins.
7889 Output:
7890 - CODE1 and CODE2 are codes of vector operations to be used when
7891 vectorizing the operation, if available.
7892 - MULTI_STEP_CVT determines the number of required intermediate steps in
7893 case of multi-step conversion (like char->short->int - in that case
7894 MULTI_STEP_CVT will be 1).
7895 - INTERM_TYPES contains the intermediate type required to perform the
7896 widening operation (short in the above example). */
7898 bool
7899 supportable_widening_operation (enum tree_code code, gimple stmt,
7900 tree vectype_out, tree vectype_in,
7901 enum tree_code *code1, enum tree_code *code2,
7902 int *multi_step_cvt,
7903 vec<tree> *interm_types)
7905 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7906 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
7907 struct loop *vect_loop = NULL;
7908 machine_mode vec_mode;
7909 enum insn_code icode1, icode2;
7910 optab optab1, optab2;
7911 tree vectype = vectype_in;
7912 tree wide_vectype = vectype_out;
7913 enum tree_code c1, c2;
7914 int i;
7915 tree prev_type, intermediate_type;
7916 machine_mode intermediate_mode, prev_mode;
7917 optab optab3, optab4;
7919 *multi_step_cvt = 0;
7920 if (loop_info)
7921 vect_loop = LOOP_VINFO_LOOP (loop_info);
7923 switch (code)
7925 case WIDEN_MULT_EXPR:
7926 /* The result of a vectorized widening operation usually requires
7927 two vectors (because the widened results do not fit into one vector).
7928 The generated vector results would normally be expected to be
7929 generated in the same order as in the original scalar computation,
7930 i.e. if 8 results are generated in each vector iteration, they are
7931 to be organized as follows:
7932 vect1: [res1,res2,res3,res4],
7933 vect2: [res5,res6,res7,res8].
7935 However, in the special case that the result of the widening
7936 operation is used in a reduction computation only, the order doesn't
7937 matter (because when vectorizing a reduction we change the order of
7938 the computation). Some targets can take advantage of this and
7939 generate more efficient code. For example, targets like Altivec,
7940 that support widen_mult using a sequence of {mult_even,mult_odd}
7941 generate the following vectors:
7942 vect1: [res1,res3,res5,res7],
7943 vect2: [res2,res4,res6,res8].
7945 When vectorizing outer-loops, we execute the inner-loop sequentially
7946 (each vectorized inner-loop iteration contributes to VF outer-loop
7947 iterations in parallel). We therefore don't allow to change the
7948 order of the computation in the inner-loop during outer-loop
7949 vectorization. */
7950 /* TODO: Another case in which order doesn't *really* matter is when we
7951 widen and then contract again, e.g. (short)((int)x * y >> 8).
7952 Normally, pack_trunc performs an even/odd permute, whereas the
7953 repack from an even/odd expansion would be an interleave, which
7954 would be significantly simpler for e.g. AVX2. */
7955 /* In any case, in order to avoid duplicating the code below, recurse
7956 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
7957 are properly set up for the caller. If we fail, we'll continue with
7958 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
7959 if (vect_loop
7960 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
7961 && !nested_in_vect_loop_p (vect_loop, stmt)
7962 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
7963 stmt, vectype_out, vectype_in,
7964 code1, code2, multi_step_cvt,
7965 interm_types))
7967 /* Elements in a vector with vect_used_by_reduction property cannot
7968 be reordered if the use chain with this property does not have the
7969 same operation. One such an example is s += a * b, where elements
7970 in a and b cannot be reordered. Here we check if the vector defined
7971 by STMT is only directly used in the reduction statement. */
7972 tree lhs = gimple_assign_lhs (stmt);
7973 use_operand_p dummy;
7974 gimple use_stmt;
7975 stmt_vec_info use_stmt_info = NULL;
7976 if (single_imm_use (lhs, &dummy, &use_stmt)
7977 && (use_stmt_info = vinfo_for_stmt (use_stmt))
7978 && STMT_VINFO_DEF_TYPE (use_stmt_info) == vect_reduction_def)
7979 return true;
7981 c1 = VEC_WIDEN_MULT_LO_EXPR;
7982 c2 = VEC_WIDEN_MULT_HI_EXPR;
7983 break;
7985 case VEC_WIDEN_MULT_EVEN_EXPR:
7986 /* Support the recursion induced just above. */
7987 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
7988 c2 = VEC_WIDEN_MULT_ODD_EXPR;
7989 break;
7991 case WIDEN_LSHIFT_EXPR:
7992 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
7993 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
7994 break;
7996 CASE_CONVERT:
7997 c1 = VEC_UNPACK_LO_EXPR;
7998 c2 = VEC_UNPACK_HI_EXPR;
7999 break;
8001 case FLOAT_EXPR:
8002 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
8003 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
8004 break;
8006 case FIX_TRUNC_EXPR:
8007 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
8008 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
8009 computing the operation. */
8010 return false;
8012 default:
8013 gcc_unreachable ();
8016 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
8018 enum tree_code ctmp = c1;
8019 c1 = c2;
8020 c2 = ctmp;
8023 if (code == FIX_TRUNC_EXPR)
8025 /* The signedness is determined from output operand. */
8026 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
8027 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
8029 else
8031 optab1 = optab_for_tree_code (c1, vectype, optab_default);
8032 optab2 = optab_for_tree_code (c2, vectype, optab_default);
8035 if (!optab1 || !optab2)
8036 return false;
8038 vec_mode = TYPE_MODE (vectype);
8039 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
8040 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
8041 return false;
8043 *code1 = c1;
8044 *code2 = c2;
8046 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
8047 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
8048 return true;
8050 /* Check if it's a multi-step conversion that can be done using intermediate
8051 types. */
8053 prev_type = vectype;
8054 prev_mode = vec_mode;
8056 if (!CONVERT_EXPR_CODE_P (code))
8057 return false;
8059 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
8060 intermediate steps in promotion sequence. We try
8061 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
8062 not. */
8063 interm_types->create (MAX_INTERM_CVT_STEPS);
8064 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
8066 intermediate_mode = insn_data[icode1].operand[0].mode;
8067 intermediate_type
8068 = lang_hooks.types.type_for_mode (intermediate_mode,
8069 TYPE_UNSIGNED (prev_type));
8070 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
8071 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
8073 if (!optab3 || !optab4
8074 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
8075 || insn_data[icode1].operand[0].mode != intermediate_mode
8076 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
8077 || insn_data[icode2].operand[0].mode != intermediate_mode
8078 || ((icode1 = optab_handler (optab3, intermediate_mode))
8079 == CODE_FOR_nothing)
8080 || ((icode2 = optab_handler (optab4, intermediate_mode))
8081 == CODE_FOR_nothing))
8082 break;
8084 interm_types->quick_push (intermediate_type);
8085 (*multi_step_cvt)++;
8087 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
8088 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
8089 return true;
8091 prev_type = intermediate_type;
8092 prev_mode = intermediate_mode;
8095 interm_types->release ();
8096 return false;
8100 /* Function supportable_narrowing_operation
8102 Check whether an operation represented by the code CODE is a
8103 narrowing operation that is supported by the target platform in
8104 vector form (i.e., when operating on arguments of type VECTYPE_IN
8105 and producing a result of type VECTYPE_OUT).
8107 Narrowing operations we currently support are NOP (CONVERT) and
8108 FIX_TRUNC. This function checks if these operations are supported by
8109 the target platform directly via vector tree-codes.
8111 Output:
8112 - CODE1 is the code of a vector operation to be used when
8113 vectorizing the operation, if available.
8114 - MULTI_STEP_CVT determines the number of required intermediate steps in
8115 case of multi-step conversion (like int->short->char - in that case
8116 MULTI_STEP_CVT will be 1).
8117 - INTERM_TYPES contains the intermediate type required to perform the
8118 narrowing operation (short in the above example). */
8120 bool
8121 supportable_narrowing_operation (enum tree_code code,
8122 tree vectype_out, tree vectype_in,
8123 enum tree_code *code1, int *multi_step_cvt,
8124 vec<tree> *interm_types)
8126 machine_mode vec_mode;
8127 enum insn_code icode1;
8128 optab optab1, interm_optab;
8129 tree vectype = vectype_in;
8130 tree narrow_vectype = vectype_out;
8131 enum tree_code c1;
8132 tree intermediate_type;
8133 machine_mode intermediate_mode, prev_mode;
8134 int i;
8135 bool uns;
8137 *multi_step_cvt = 0;
8138 switch (code)
8140 CASE_CONVERT:
8141 c1 = VEC_PACK_TRUNC_EXPR;
8142 break;
8144 case FIX_TRUNC_EXPR:
8145 c1 = VEC_PACK_FIX_TRUNC_EXPR;
8146 break;
8148 case FLOAT_EXPR:
8149 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
8150 tree code and optabs used for computing the operation. */
8151 return false;
8153 default:
8154 gcc_unreachable ();
8157 if (code == FIX_TRUNC_EXPR)
8158 /* The signedness is determined from output operand. */
8159 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
8160 else
8161 optab1 = optab_for_tree_code (c1, vectype, optab_default);
8163 if (!optab1)
8164 return false;
8166 vec_mode = TYPE_MODE (vectype);
8167 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
8168 return false;
8170 *code1 = c1;
8172 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
8173 return true;
8175 /* Check if it's a multi-step conversion that can be done using intermediate
8176 types. */
8177 prev_mode = vec_mode;
8178 if (code == FIX_TRUNC_EXPR)
8179 uns = TYPE_UNSIGNED (vectype_out);
8180 else
8181 uns = TYPE_UNSIGNED (vectype);
8183 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
8184 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
8185 costly than signed. */
8186 if (code == FIX_TRUNC_EXPR && uns)
8188 enum insn_code icode2;
8190 intermediate_type
8191 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
8192 interm_optab
8193 = optab_for_tree_code (c1, intermediate_type, optab_default);
8194 if (interm_optab != unknown_optab
8195 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
8196 && insn_data[icode1].operand[0].mode
8197 == insn_data[icode2].operand[0].mode)
8199 uns = false;
8200 optab1 = interm_optab;
8201 icode1 = icode2;
8205 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
8206 intermediate steps in promotion sequence. We try
8207 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
8208 interm_types->create (MAX_INTERM_CVT_STEPS);
8209 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
8211 intermediate_mode = insn_data[icode1].operand[0].mode;
8212 intermediate_type
8213 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
8214 interm_optab
8215 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
8216 optab_default);
8217 if (!interm_optab
8218 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
8219 || insn_data[icode1].operand[0].mode != intermediate_mode
8220 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
8221 == CODE_FOR_nothing))
8222 break;
8224 interm_types->quick_push (intermediate_type);
8225 (*multi_step_cvt)++;
8227 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
8228 return true;
8230 prev_mode = intermediate_mode;
8231 optab1 = interm_optab;
8234 interm_types->release ();
8235 return false;