re PR tree-optimization/59287 (points-to analysis confused by union accesses)
[official-gcc.git] / gcc / tree-vect-stmts.c
blob72dfacd31519c8127463703cfb09489656e0508a
1 /* Statement Analysis and Transformation for Vectorization
2 Copyright (C) 2003-2013 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 "tree.h"
28 #include "stor-layout.h"
29 #include "target.h"
30 #include "basic-block.h"
31 #include "gimple-pretty-print.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "tree-eh.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "gimple-ssa.h"
42 #include "tree-cfg.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-ssa-loop-manip.h"
48 #include "cfgloop.h"
49 #include "expr.h"
50 #include "recog.h" /* FIXME: for insn_data */
51 #include "optabs.h"
52 #include "diagnostic-core.h"
53 #include "tree-vectorizer.h"
54 #include "dumpfile.h"
56 /* For lang_hooks.types.type_for_mode. */
57 #include "langhooks.h"
59 /* Return the vectorized type for the given statement. */
61 tree
62 stmt_vectype (struct _stmt_vec_info *stmt_info)
64 return STMT_VINFO_VECTYPE (stmt_info);
67 /* Return TRUE iff the given statement is in an inner loop relative to
68 the loop being vectorized. */
69 bool
70 stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
72 gimple stmt = STMT_VINFO_STMT (stmt_info);
73 basic_block bb = gimple_bb (stmt);
74 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
75 struct loop* loop;
77 if (!loop_vinfo)
78 return false;
80 loop = LOOP_VINFO_LOOP (loop_vinfo);
82 return (bb->loop_father == loop->inner);
85 /* Record the cost of a statement, either by directly informing the
86 target model or by saving it in a vector for later processing.
87 Return a preliminary estimate of the statement's cost. */
89 unsigned
90 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
91 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
92 int misalign, enum vect_cost_model_location where)
94 if (body_cost_vec)
96 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
97 add_stmt_info_to_vec (body_cost_vec, count, kind,
98 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
99 misalign);
100 return (unsigned)
101 (builtin_vectorization_cost (kind, vectype, misalign) * count);
104 else
106 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
107 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
108 void *target_cost_data;
110 if (loop_vinfo)
111 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
112 else
113 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
115 return add_stmt_cost (target_cost_data, count, kind, stmt_info,
116 misalign, where);
120 /* Return a variable of type ELEM_TYPE[NELEMS]. */
122 static tree
123 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
125 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
126 "vect_array");
129 /* ARRAY is an array of vectors created by create_vector_array.
130 Return an SSA_NAME for the vector in index N. The reference
131 is part of the vectorization of STMT and the vector is associated
132 with scalar destination SCALAR_DEST. */
134 static tree
135 read_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
136 tree array, unsigned HOST_WIDE_INT n)
138 tree vect_type, vect, vect_name, array_ref;
139 gimple new_stmt;
141 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
142 vect_type = TREE_TYPE (TREE_TYPE (array));
143 vect = vect_create_destination_var (scalar_dest, vect_type);
144 array_ref = build4 (ARRAY_REF, vect_type, array,
145 build_int_cst (size_type_node, n),
146 NULL_TREE, NULL_TREE);
148 new_stmt = gimple_build_assign (vect, array_ref);
149 vect_name = make_ssa_name (vect, new_stmt);
150 gimple_assign_set_lhs (new_stmt, vect_name);
151 vect_finish_stmt_generation (stmt, new_stmt, gsi);
153 return vect_name;
156 /* ARRAY is an array of vectors created by create_vector_array.
157 Emit code to store SSA_NAME VECT in index N of the array.
158 The store is part of the vectorization of STMT. */
160 static void
161 write_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree vect,
162 tree array, unsigned HOST_WIDE_INT n)
164 tree array_ref;
165 gimple new_stmt;
167 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
168 build_int_cst (size_type_node, n),
169 NULL_TREE, NULL_TREE);
171 new_stmt = gimple_build_assign (array_ref, vect);
172 vect_finish_stmt_generation (stmt, new_stmt, gsi);
175 /* PTR is a pointer to an array of type TYPE. Return a representation
176 of *PTR. The memory reference replaces those in FIRST_DR
177 (and its group). */
179 static tree
180 create_array_ref (tree type, tree ptr, struct data_reference *first_dr)
182 tree mem_ref, alias_ptr_type;
184 alias_ptr_type = reference_alias_ptr_type (DR_REF (first_dr));
185 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
186 /* Arrays have the same alignment as their type. */
187 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
188 return mem_ref;
191 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
193 /* Function vect_mark_relevant.
195 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
197 static void
198 vect_mark_relevant (vec<gimple> *worklist, gimple stmt,
199 enum vect_relevant relevant, bool live_p,
200 bool used_in_pattern)
202 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
203 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
204 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
205 gimple pattern_stmt;
207 if (dump_enabled_p ())
208 dump_printf_loc (MSG_NOTE, vect_location,
209 "mark relevant %d, live %d.\n", relevant, live_p);
211 /* If this stmt is an original stmt in a pattern, we might need to mark its
212 related pattern stmt instead of the original stmt. However, such stmts
213 may have their own uses that are not in any pattern, in such cases the
214 stmt itself should be marked. */
215 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
217 bool found = false;
218 if (!used_in_pattern)
220 imm_use_iterator imm_iter;
221 use_operand_p use_p;
222 gimple use_stmt;
223 tree lhs;
224 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
225 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
227 if (is_gimple_assign (stmt))
228 lhs = gimple_assign_lhs (stmt);
229 else
230 lhs = gimple_call_lhs (stmt);
232 /* This use is out of pattern use, if LHS has other uses that are
233 pattern uses, we should mark the stmt itself, and not the pattern
234 stmt. */
235 if (TREE_CODE (lhs) == SSA_NAME)
236 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
238 if (is_gimple_debug (USE_STMT (use_p)))
239 continue;
240 use_stmt = USE_STMT (use_p);
242 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
243 continue;
245 if (vinfo_for_stmt (use_stmt)
246 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
248 found = true;
249 break;
254 if (!found)
256 /* This is the last stmt in a sequence that was detected as a
257 pattern that can potentially be vectorized. Don't mark the stmt
258 as relevant/live because it's not going to be vectorized.
259 Instead mark the pattern-stmt that replaces it. */
261 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
263 if (dump_enabled_p ())
264 dump_printf_loc (MSG_NOTE, vect_location,
265 "last stmt in pattern. don't mark"
266 " relevant/live.\n");
267 stmt_info = vinfo_for_stmt (pattern_stmt);
268 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
269 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
270 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
271 stmt = pattern_stmt;
275 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
276 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
277 STMT_VINFO_RELEVANT (stmt_info) = relevant;
279 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
280 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
282 if (dump_enabled_p ())
283 dump_printf_loc (MSG_NOTE, vect_location,
284 "already marked relevant/live.\n");
285 return;
288 worklist->safe_push (stmt);
292 /* Function vect_stmt_relevant_p.
294 Return true if STMT in loop that is represented by LOOP_VINFO is
295 "relevant for vectorization".
297 A stmt is considered "relevant for vectorization" if:
298 - it has uses outside the loop.
299 - it has vdefs (it alters memory).
300 - control stmts in the loop (except for the exit condition).
302 CHECKME: what other side effects would the vectorizer allow? */
304 static bool
305 vect_stmt_relevant_p (gimple stmt, loop_vec_info loop_vinfo,
306 enum vect_relevant *relevant, bool *live_p)
308 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
309 ssa_op_iter op_iter;
310 imm_use_iterator imm_iter;
311 use_operand_p use_p;
312 def_operand_p def_p;
314 *relevant = vect_unused_in_scope;
315 *live_p = false;
317 /* cond stmt other than loop exit cond. */
318 if (is_ctrl_stmt (stmt)
319 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
320 != loop_exit_ctrl_vec_info_type)
321 *relevant = vect_used_in_scope;
323 /* changing memory. */
324 if (gimple_code (stmt) != GIMPLE_PHI)
325 if (gimple_vdef (stmt))
327 if (dump_enabled_p ())
328 dump_printf_loc (MSG_NOTE, vect_location,
329 "vec_stmt_relevant_p: stmt has vdefs.\n");
330 *relevant = vect_used_in_scope;
333 /* uses outside the loop. */
334 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
336 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
338 basic_block bb = gimple_bb (USE_STMT (use_p));
339 if (!flow_bb_inside_loop_p (loop, bb))
341 if (dump_enabled_p ())
342 dump_printf_loc (MSG_NOTE, vect_location,
343 "vec_stmt_relevant_p: used out of loop.\n");
345 if (is_gimple_debug (USE_STMT (use_p)))
346 continue;
348 /* We expect all such uses to be in the loop exit phis
349 (because of loop closed form) */
350 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
351 gcc_assert (bb == single_exit (loop)->dest);
353 *live_p = true;
358 return (*live_p || *relevant);
362 /* Function exist_non_indexing_operands_for_use_p
364 USE is one of the uses attached to STMT. Check if USE is
365 used in STMT for anything other than indexing an array. */
367 static bool
368 exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
370 tree operand;
371 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
373 /* USE corresponds to some operand in STMT. If there is no data
374 reference in STMT, then any operand that corresponds to USE
375 is not indexing an array. */
376 if (!STMT_VINFO_DATA_REF (stmt_info))
377 return true;
379 /* STMT has a data_ref. FORNOW this means that its of one of
380 the following forms:
381 -1- ARRAY_REF = var
382 -2- var = ARRAY_REF
383 (This should have been verified in analyze_data_refs).
385 'var' in the second case corresponds to a def, not a use,
386 so USE cannot correspond to any operands that are not used
387 for array indexing.
389 Therefore, all we need to check is if STMT falls into the
390 first case, and whether var corresponds to USE. */
392 if (!gimple_assign_copy_p (stmt))
393 return false;
394 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
395 return false;
396 operand = gimple_assign_rhs1 (stmt);
397 if (TREE_CODE (operand) != SSA_NAME)
398 return false;
400 if (operand == use)
401 return true;
403 return false;
408 Function process_use.
410 Inputs:
411 - a USE in STMT in a loop represented by LOOP_VINFO
412 - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
413 that defined USE. This is done by calling mark_relevant and passing it
414 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
415 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
416 be performed.
418 Outputs:
419 Generally, LIVE_P and RELEVANT are used to define the liveness and
420 relevance info of the DEF_STMT of this USE:
421 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
422 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
423 Exceptions:
424 - case 1: If USE is used only for address computations (e.g. array indexing),
425 which does not need to be directly vectorized, then the liveness/relevance
426 of the respective DEF_STMT is left unchanged.
427 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
428 skip DEF_STMT cause it had already been processed.
429 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
430 be modified accordingly.
432 Return true if everything is as expected. Return false otherwise. */
434 static bool
435 process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
436 enum vect_relevant relevant, vec<gimple> *worklist,
437 bool force)
439 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
440 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
441 stmt_vec_info dstmt_vinfo;
442 basic_block bb, def_bb;
443 tree def;
444 gimple def_stmt;
445 enum vect_def_type dt;
447 /* case 1: we are only interested in uses that need to be vectorized. Uses
448 that are used for address computation are not considered relevant. */
449 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
450 return true;
452 if (!vect_is_simple_use (use, stmt, loop_vinfo, NULL, &def_stmt, &def, &dt))
454 if (dump_enabled_p ())
455 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
456 "not vectorized: unsupported use in stmt.\n");
457 return false;
460 if (!def_stmt || gimple_nop_p (def_stmt))
461 return true;
463 def_bb = gimple_bb (def_stmt);
464 if (!flow_bb_inside_loop_p (loop, def_bb))
466 if (dump_enabled_p ())
467 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
468 return true;
471 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
472 DEF_STMT must have already been processed, because this should be the
473 only way that STMT, which is a reduction-phi, was put in the worklist,
474 as there should be no other uses for DEF_STMT in the loop. So we just
475 check that everything is as expected, and we are done. */
476 dstmt_vinfo = vinfo_for_stmt (def_stmt);
477 bb = gimple_bb (stmt);
478 if (gimple_code (stmt) == GIMPLE_PHI
479 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
480 && gimple_code (def_stmt) != GIMPLE_PHI
481 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
482 && bb->loop_father == def_bb->loop_father)
484 if (dump_enabled_p ())
485 dump_printf_loc (MSG_NOTE, vect_location,
486 "reduc-stmt defining reduc-phi in the same nest.\n");
487 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
488 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
489 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
490 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
491 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
492 return true;
495 /* case 3a: outer-loop stmt defining an inner-loop stmt:
496 outer-loop-header-bb:
497 d = def_stmt
498 inner-loop:
499 stmt # use (d)
500 outer-loop-tail-bb:
501 ... */
502 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
504 if (dump_enabled_p ())
505 dump_printf_loc (MSG_NOTE, vect_location,
506 "outer-loop def-stmt defining inner-loop stmt.\n");
508 switch (relevant)
510 case vect_unused_in_scope:
511 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
512 vect_used_in_scope : vect_unused_in_scope;
513 break;
515 case vect_used_in_outer_by_reduction:
516 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
517 relevant = vect_used_by_reduction;
518 break;
520 case vect_used_in_outer:
521 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
522 relevant = vect_used_in_scope;
523 break;
525 case vect_used_in_scope:
526 break;
528 default:
529 gcc_unreachable ();
533 /* case 3b: inner-loop stmt defining an outer-loop stmt:
534 outer-loop-header-bb:
536 inner-loop:
537 d = def_stmt
538 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
539 stmt # use (d) */
540 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
542 if (dump_enabled_p ())
543 dump_printf_loc (MSG_NOTE, vect_location,
544 "inner-loop def-stmt defining outer-loop stmt.\n");
546 switch (relevant)
548 case vect_unused_in_scope:
549 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
550 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
551 vect_used_in_outer_by_reduction : vect_unused_in_scope;
552 break;
554 case vect_used_by_reduction:
555 relevant = vect_used_in_outer_by_reduction;
556 break;
558 case vect_used_in_scope:
559 relevant = vect_used_in_outer;
560 break;
562 default:
563 gcc_unreachable ();
567 vect_mark_relevant (worklist, def_stmt, relevant, live_p,
568 is_pattern_stmt_p (stmt_vinfo));
569 return true;
573 /* Function vect_mark_stmts_to_be_vectorized.
575 Not all stmts in the loop need to be vectorized. For example:
577 for i...
578 for j...
579 1. T0 = i + j
580 2. T1 = a[T0]
582 3. j = j + 1
584 Stmt 1 and 3 do not need to be vectorized, because loop control and
585 addressing of vectorized data-refs are handled differently.
587 This pass detects such stmts. */
589 bool
590 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
592 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
593 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
594 unsigned int nbbs = loop->num_nodes;
595 gimple_stmt_iterator si;
596 gimple stmt;
597 unsigned int i;
598 stmt_vec_info stmt_vinfo;
599 basic_block bb;
600 gimple phi;
601 bool live_p;
602 enum vect_relevant relevant, tmp_relevant;
603 enum vect_def_type def_type;
605 if (dump_enabled_p ())
606 dump_printf_loc (MSG_NOTE, vect_location,
607 "=== vect_mark_stmts_to_be_vectorized ===\n");
609 stack_vec<gimple, 64> worklist;
611 /* 1. Init worklist. */
612 for (i = 0; i < nbbs; i++)
614 bb = bbs[i];
615 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
617 phi = gsi_stmt (si);
618 if (dump_enabled_p ())
620 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
621 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
622 dump_printf (MSG_NOTE, "\n");
625 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
626 vect_mark_relevant (&worklist, phi, relevant, live_p, false);
628 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
630 stmt = gsi_stmt (si);
631 if (dump_enabled_p ())
633 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
634 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
635 dump_printf (MSG_NOTE, "\n");
638 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
639 vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
643 /* 2. Process_worklist */
644 while (worklist.length () > 0)
646 use_operand_p use_p;
647 ssa_op_iter iter;
649 stmt = worklist.pop ();
650 if (dump_enabled_p ())
652 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
653 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
654 dump_printf (MSG_NOTE, "\n");
657 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
658 (DEF_STMT) as relevant/irrelevant and live/dead according to the
659 liveness and relevance properties of STMT. */
660 stmt_vinfo = vinfo_for_stmt (stmt);
661 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
662 live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
664 /* Generally, the liveness and relevance properties of STMT are
665 propagated as is to the DEF_STMTs of its USEs:
666 live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
667 relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
669 One exception is when STMT has been identified as defining a reduction
670 variable; in this case we set the liveness/relevance as follows:
671 live_p = false
672 relevant = vect_used_by_reduction
673 This is because we distinguish between two kinds of relevant stmts -
674 those that are used by a reduction computation, and those that are
675 (also) used by a regular computation. This allows us later on to
676 identify stmts that are used solely by a reduction, and therefore the
677 order of the results that they produce does not have to be kept. */
679 def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
680 tmp_relevant = relevant;
681 switch (def_type)
683 case vect_reduction_def:
684 switch (tmp_relevant)
686 case vect_unused_in_scope:
687 relevant = vect_used_by_reduction;
688 break;
690 case vect_used_by_reduction:
691 if (gimple_code (stmt) == GIMPLE_PHI)
692 break;
693 /* fall through */
695 default:
696 if (dump_enabled_p ())
697 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
698 "unsupported use of reduction.\n");
699 return false;
702 live_p = false;
703 break;
705 case vect_nested_cycle:
706 if (tmp_relevant != vect_unused_in_scope
707 && tmp_relevant != vect_used_in_outer_by_reduction
708 && tmp_relevant != vect_used_in_outer)
710 if (dump_enabled_p ())
711 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
712 "unsupported use of nested cycle.\n");
714 return false;
717 live_p = false;
718 break;
720 case vect_double_reduction_def:
721 if (tmp_relevant != vect_unused_in_scope
722 && tmp_relevant != vect_used_by_reduction)
724 if (dump_enabled_p ())
725 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
726 "unsupported use of double reduction.\n");
728 return false;
731 live_p = false;
732 break;
734 default:
735 break;
738 if (is_pattern_stmt_p (stmt_vinfo))
740 /* Pattern statements are not inserted into the code, so
741 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
742 have to scan the RHS or function arguments instead. */
743 if (is_gimple_assign (stmt))
745 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
746 tree op = gimple_assign_rhs1 (stmt);
748 i = 1;
749 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
751 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
752 live_p, relevant, &worklist, false)
753 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
754 live_p, relevant, &worklist, false))
755 return false;
756 i = 2;
758 for (; i < gimple_num_ops (stmt); i++)
760 op = gimple_op (stmt, i);
761 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
762 &worklist, false))
763 return false;
766 else if (is_gimple_call (stmt))
768 for (i = 0; i < gimple_call_num_args (stmt); i++)
770 tree arg = gimple_call_arg (stmt, i);
771 if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
772 &worklist, false))
773 return false;
777 else
778 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
780 tree op = USE_FROM_PTR (use_p);
781 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
782 &worklist, false))
783 return false;
786 if (STMT_VINFO_GATHER_P (stmt_vinfo))
788 tree off;
789 tree decl = vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
790 gcc_assert (decl);
791 if (!process_use (stmt, off, loop_vinfo, live_p, relevant,
792 &worklist, true))
793 return false;
795 } /* while worklist */
797 return true;
801 /* Function vect_model_simple_cost.
803 Models cost for simple operations, i.e. those that only emit ncopies of a
804 single op. Right now, this does not account for multiple insns that could
805 be generated for the single vector op. We will handle that shortly. */
807 void
808 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
809 enum vect_def_type *dt,
810 stmt_vector_for_cost *prologue_cost_vec,
811 stmt_vector_for_cost *body_cost_vec)
813 int i;
814 int inside_cost = 0, prologue_cost = 0;
816 /* The SLP costs were already calculated during SLP tree build. */
817 if (PURE_SLP_STMT (stmt_info))
818 return;
820 /* FORNOW: Assuming maximum 2 args per stmts. */
821 for (i = 0; i < 2; i++)
822 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
823 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
824 stmt_info, 0, vect_prologue);
826 /* Pass the inside-of-loop statements to the target-specific cost model. */
827 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
828 stmt_info, 0, vect_body);
830 if (dump_enabled_p ())
831 dump_printf_loc (MSG_NOTE, vect_location,
832 "vect_model_simple_cost: inside_cost = %d, "
833 "prologue_cost = %d .\n", inside_cost, prologue_cost);
837 /* Model cost for type demotion and promotion operations. PWR is normally
838 zero for single-step promotions and demotions. It will be one if
839 two-step promotion/demotion is required, and so on. Each additional
840 step doubles the number of instructions required. */
842 static void
843 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
844 enum vect_def_type *dt, int pwr)
846 int i, tmp;
847 int inside_cost = 0, prologue_cost = 0;
848 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
849 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
850 void *target_cost_data;
852 /* The SLP costs were already calculated during SLP tree build. */
853 if (PURE_SLP_STMT (stmt_info))
854 return;
856 if (loop_vinfo)
857 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
858 else
859 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
861 for (i = 0; i < pwr + 1; i++)
863 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
864 (i + 1) : i;
865 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
866 vec_promote_demote, stmt_info, 0,
867 vect_body);
870 /* FORNOW: Assuming maximum 2 args per stmts. */
871 for (i = 0; i < 2; i++)
872 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
873 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
874 stmt_info, 0, vect_prologue);
876 if (dump_enabled_p ())
877 dump_printf_loc (MSG_NOTE, vect_location,
878 "vect_model_promotion_demotion_cost: inside_cost = %d, "
879 "prologue_cost = %d .\n", inside_cost, prologue_cost);
882 /* Function vect_cost_group_size
884 For grouped load or store, return the group_size only if it is the first
885 load or store of a group, else return 1. This ensures that group size is
886 only returned once per group. */
888 static int
889 vect_cost_group_size (stmt_vec_info stmt_info)
891 gimple first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
893 if (first_stmt == STMT_VINFO_STMT (stmt_info))
894 return GROUP_SIZE (stmt_info);
896 return 1;
900 /* Function vect_model_store_cost
902 Models cost for stores. In the case of grouped accesses, one access
903 has the overhead of the grouped access attributed to it. */
905 void
906 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
907 bool store_lanes_p, enum vect_def_type dt,
908 slp_tree slp_node,
909 stmt_vector_for_cost *prologue_cost_vec,
910 stmt_vector_for_cost *body_cost_vec)
912 int group_size;
913 unsigned int inside_cost = 0, prologue_cost = 0;
914 struct data_reference *first_dr;
915 gimple first_stmt;
917 /* The SLP costs were already calculated during SLP tree build. */
918 if (PURE_SLP_STMT (stmt_info))
919 return;
921 if (dt == vect_constant_def || dt == vect_external_def)
922 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
923 stmt_info, 0, vect_prologue);
925 /* Grouped access? */
926 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
928 if (slp_node)
930 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
931 group_size = 1;
933 else
935 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
936 group_size = vect_cost_group_size (stmt_info);
939 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
941 /* Not a grouped access. */
942 else
944 group_size = 1;
945 first_dr = STMT_VINFO_DATA_REF (stmt_info);
948 /* We assume that the cost of a single store-lanes instruction is
949 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
950 access is instead being provided by a permute-and-store operation,
951 include the cost of the permutes. */
952 if (!store_lanes_p && group_size > 1)
954 /* Uses a high and low interleave operation for each needed permute. */
956 int nstmts = ncopies * exact_log2 (group_size) * group_size;
957 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
958 stmt_info, 0, vect_body);
960 if (dump_enabled_p ())
961 dump_printf_loc (MSG_NOTE, vect_location,
962 "vect_model_store_cost: strided group_size = %d .\n",
963 group_size);
966 /* Costs of the stores. */
967 vect_get_store_cost (first_dr, ncopies, &inside_cost, body_cost_vec);
969 if (dump_enabled_p ())
970 dump_printf_loc (MSG_NOTE, vect_location,
971 "vect_model_store_cost: inside_cost = %d, "
972 "prologue_cost = %d .\n", inside_cost, prologue_cost);
976 /* Calculate cost of DR's memory access. */
977 void
978 vect_get_store_cost (struct data_reference *dr, int ncopies,
979 unsigned int *inside_cost,
980 stmt_vector_for_cost *body_cost_vec)
982 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
983 gimple stmt = DR_STMT (dr);
984 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
986 switch (alignment_support_scheme)
988 case dr_aligned:
990 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
991 vector_store, stmt_info, 0,
992 vect_body);
994 if (dump_enabled_p ())
995 dump_printf_loc (MSG_NOTE, vect_location,
996 "vect_model_store_cost: aligned.\n");
997 break;
1000 case dr_unaligned_supported:
1002 /* Here, we assign an additional cost for the unaligned store. */
1003 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1004 unaligned_store, stmt_info,
1005 DR_MISALIGNMENT (dr), vect_body);
1006 if (dump_enabled_p ())
1007 dump_printf_loc (MSG_NOTE, vect_location,
1008 "vect_model_store_cost: unaligned supported by "
1009 "hardware.\n");
1010 break;
1013 case dr_unaligned_unsupported:
1015 *inside_cost = VECT_MAX_COST;
1017 if (dump_enabled_p ())
1018 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1019 "vect_model_store_cost: unsupported access.\n");
1020 break;
1023 default:
1024 gcc_unreachable ();
1029 /* Function vect_model_load_cost
1031 Models cost for loads. In the case of grouped accesses, the last access
1032 has the overhead of the grouped access attributed to it. Since unaligned
1033 accesses are supported for loads, we also account for the costs of the
1034 access scheme chosen. */
1036 void
1037 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1038 bool load_lanes_p, slp_tree slp_node,
1039 stmt_vector_for_cost *prologue_cost_vec,
1040 stmt_vector_for_cost *body_cost_vec)
1042 int group_size;
1043 gimple first_stmt;
1044 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
1045 unsigned int inside_cost = 0, prologue_cost = 0;
1047 /* The SLP costs were already calculated during SLP tree build. */
1048 if (PURE_SLP_STMT (stmt_info))
1049 return;
1051 /* Grouped accesses? */
1052 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1053 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && first_stmt && !slp_node)
1055 group_size = vect_cost_group_size (stmt_info);
1056 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1058 /* Not a grouped access. */
1059 else
1061 group_size = 1;
1062 first_dr = dr;
1065 /* We assume that the cost of a single load-lanes instruction is
1066 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1067 access is instead being provided by a load-and-permute operation,
1068 include the cost of the permutes. */
1069 if (!load_lanes_p && group_size > 1)
1071 /* Uses an even and odd extract operations for each needed permute. */
1072 int nstmts = ncopies * exact_log2 (group_size) * group_size;
1073 inside_cost += record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1074 stmt_info, 0, vect_body);
1076 if (dump_enabled_p ())
1077 dump_printf_loc (MSG_NOTE, vect_location,
1078 "vect_model_load_cost: strided group_size = %d .\n",
1079 group_size);
1082 /* The loads themselves. */
1083 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1085 /* N scalar loads plus gathering them into a vector. */
1086 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1087 inside_cost += record_stmt_cost (body_cost_vec,
1088 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1089 scalar_load, stmt_info, 0, vect_body);
1090 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1091 stmt_info, 0, vect_body);
1093 else
1094 vect_get_load_cost (first_dr, ncopies,
1095 ((!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1096 || group_size > 1 || slp_node),
1097 &inside_cost, &prologue_cost,
1098 prologue_cost_vec, body_cost_vec, true);
1100 if (dump_enabled_p ())
1101 dump_printf_loc (MSG_NOTE, vect_location,
1102 "vect_model_load_cost: inside_cost = %d, "
1103 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1107 /* Calculate cost of DR's memory access. */
1108 void
1109 vect_get_load_cost (struct data_reference *dr, int ncopies,
1110 bool add_realign_cost, unsigned int *inside_cost,
1111 unsigned int *prologue_cost,
1112 stmt_vector_for_cost *prologue_cost_vec,
1113 stmt_vector_for_cost *body_cost_vec,
1114 bool record_prologue_costs)
1116 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1117 gimple stmt = DR_STMT (dr);
1118 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1120 switch (alignment_support_scheme)
1122 case dr_aligned:
1124 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1125 stmt_info, 0, vect_body);
1127 if (dump_enabled_p ())
1128 dump_printf_loc (MSG_NOTE, vect_location,
1129 "vect_model_load_cost: aligned.\n");
1131 break;
1133 case dr_unaligned_supported:
1135 /* Here, we assign an additional cost for the unaligned load. */
1136 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1137 unaligned_load, stmt_info,
1138 DR_MISALIGNMENT (dr), vect_body);
1140 if (dump_enabled_p ())
1141 dump_printf_loc (MSG_NOTE, vect_location,
1142 "vect_model_load_cost: unaligned supported by "
1143 "hardware.\n");
1145 break;
1147 case dr_explicit_realign:
1149 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1150 vector_load, stmt_info, 0, vect_body);
1151 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1152 vec_perm, stmt_info, 0, vect_body);
1154 /* FIXME: If the misalignment remains fixed across the iterations of
1155 the containing loop, the following cost should be added to the
1156 prologue costs. */
1157 if (targetm.vectorize.builtin_mask_for_load)
1158 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1159 stmt_info, 0, vect_body);
1161 if (dump_enabled_p ())
1162 dump_printf_loc (MSG_NOTE, vect_location,
1163 "vect_model_load_cost: explicit realign\n");
1165 break;
1167 case dr_explicit_realign_optimized:
1169 if (dump_enabled_p ())
1170 dump_printf_loc (MSG_NOTE, vect_location,
1171 "vect_model_load_cost: unaligned software "
1172 "pipelined.\n");
1174 /* Unaligned software pipeline has a load of an address, an initial
1175 load, and possibly a mask operation to "prime" the loop. However,
1176 if this is an access in a group of loads, which provide grouped
1177 access, then the above cost should only be considered for one
1178 access in the group. Inside the loop, there is a load op
1179 and a realignment op. */
1181 if (add_realign_cost && record_prologue_costs)
1183 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1184 vector_stmt, stmt_info,
1185 0, vect_prologue);
1186 if (targetm.vectorize.builtin_mask_for_load)
1187 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1188 vector_stmt, stmt_info,
1189 0, vect_prologue);
1192 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1193 stmt_info, 0, vect_body);
1194 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1195 stmt_info, 0, vect_body);
1197 if (dump_enabled_p ())
1198 dump_printf_loc (MSG_NOTE, vect_location,
1199 "vect_model_load_cost: explicit realign optimized"
1200 "\n");
1202 break;
1205 case dr_unaligned_unsupported:
1207 *inside_cost = VECT_MAX_COST;
1209 if (dump_enabled_p ())
1210 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1211 "vect_model_load_cost: unsupported access.\n");
1212 break;
1215 default:
1216 gcc_unreachable ();
1220 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1221 the loop preheader for the vectorized stmt STMT. */
1223 static void
1224 vect_init_vector_1 (gimple stmt, gimple new_stmt, gimple_stmt_iterator *gsi)
1226 if (gsi)
1227 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1228 else
1230 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1231 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1233 if (loop_vinfo)
1235 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1236 basic_block new_bb;
1237 edge pe;
1239 if (nested_in_vect_loop_p (loop, stmt))
1240 loop = loop->inner;
1242 pe = loop_preheader_edge (loop);
1243 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1244 gcc_assert (!new_bb);
1246 else
1248 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1249 basic_block bb;
1250 gimple_stmt_iterator gsi_bb_start;
1252 gcc_assert (bb_vinfo);
1253 bb = BB_VINFO_BB (bb_vinfo);
1254 gsi_bb_start = gsi_after_labels (bb);
1255 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1259 if (dump_enabled_p ())
1261 dump_printf_loc (MSG_NOTE, vect_location,
1262 "created new init_stmt: ");
1263 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1264 dump_printf (MSG_NOTE, "\n");
1268 /* Function vect_init_vector.
1270 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1271 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1272 vector type a vector with all elements equal to VAL is created first.
1273 Place the initialization at BSI if it is not NULL. Otherwise, place the
1274 initialization at the loop preheader.
1275 Return the DEF of INIT_STMT.
1276 It will be used in the vectorization of STMT. */
1278 tree
1279 vect_init_vector (gimple stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1281 tree new_var;
1282 gimple init_stmt;
1283 tree vec_oprnd;
1284 tree new_temp;
1286 if (TREE_CODE (type) == VECTOR_TYPE
1287 && TREE_CODE (TREE_TYPE (val)) != VECTOR_TYPE)
1289 if (!types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1291 if (CONSTANT_CLASS_P (val))
1292 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (type), val);
1293 else
1295 new_temp = make_ssa_name (TREE_TYPE (type), NULL);
1296 init_stmt = gimple_build_assign_with_ops (NOP_EXPR,
1297 new_temp, val,
1298 NULL_TREE);
1299 vect_init_vector_1 (stmt, init_stmt, gsi);
1300 val = new_temp;
1303 val = build_vector_from_val (type, val);
1306 new_var = vect_get_new_vect_var (type, vect_simple_var, "cst_");
1307 init_stmt = gimple_build_assign (new_var, val);
1308 new_temp = make_ssa_name (new_var, init_stmt);
1309 gimple_assign_set_lhs (init_stmt, new_temp);
1310 vect_init_vector_1 (stmt, init_stmt, gsi);
1311 vec_oprnd = gimple_assign_lhs (init_stmt);
1312 return vec_oprnd;
1316 /* Function vect_get_vec_def_for_operand.
1318 OP is an operand in STMT. This function returns a (vector) def that will be
1319 used in the vectorized stmt for STMT.
1321 In the case that OP is an SSA_NAME which is defined in the loop, then
1322 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1324 In case OP is an invariant or constant, a new stmt that creates a vector def
1325 needs to be introduced. */
1327 tree
1328 vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
1330 tree vec_oprnd;
1331 gimple vec_stmt;
1332 gimple def_stmt;
1333 stmt_vec_info def_stmt_info = NULL;
1334 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1335 unsigned int nunits;
1336 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1337 tree def;
1338 enum vect_def_type dt;
1339 bool is_simple_use;
1340 tree vector_type;
1342 if (dump_enabled_p ())
1344 dump_printf_loc (MSG_NOTE, vect_location,
1345 "vect_get_vec_def_for_operand: ");
1346 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1347 dump_printf (MSG_NOTE, "\n");
1350 is_simple_use = vect_is_simple_use (op, stmt, loop_vinfo, NULL,
1351 &def_stmt, &def, &dt);
1352 gcc_assert (is_simple_use);
1353 if (dump_enabled_p ())
1355 int loc_printed = 0;
1356 if (def)
1358 dump_printf_loc (MSG_NOTE, vect_location, "def = ");
1359 loc_printed = 1;
1360 dump_generic_expr (MSG_NOTE, TDF_SLIM, def);
1361 dump_printf (MSG_NOTE, "\n");
1363 if (def_stmt)
1365 if (loc_printed)
1366 dump_printf (MSG_NOTE, " def_stmt = ");
1367 else
1368 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1369 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1370 dump_printf (MSG_NOTE, "\n");
1374 switch (dt)
1376 /* Case 1: operand is a constant. */
1377 case vect_constant_def:
1379 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1380 gcc_assert (vector_type);
1381 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
1383 if (scalar_def)
1384 *scalar_def = op;
1386 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1387 if (dump_enabled_p ())
1388 dump_printf_loc (MSG_NOTE, vect_location,
1389 "Create vector_cst. nunits = %d\n", nunits);
1391 return vect_init_vector (stmt, op, vector_type, NULL);
1394 /* Case 2: operand is defined outside the loop - loop invariant. */
1395 case vect_external_def:
1397 vector_type = get_vectype_for_scalar_type (TREE_TYPE (def));
1398 gcc_assert (vector_type);
1400 if (scalar_def)
1401 *scalar_def = def;
1403 /* Create 'vec_inv = {inv,inv,..,inv}' */
1404 if (dump_enabled_p ())
1405 dump_printf_loc (MSG_NOTE, vect_location, "Create vector_inv.\n");
1407 return vect_init_vector (stmt, def, vector_type, NULL);
1410 /* Case 3: operand is defined inside the loop. */
1411 case vect_internal_def:
1413 if (scalar_def)
1414 *scalar_def = NULL/* FIXME tuples: def_stmt*/;
1416 /* Get the def from the vectorized stmt. */
1417 def_stmt_info = vinfo_for_stmt (def_stmt);
1419 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1420 /* Get vectorized pattern statement. */
1421 if (!vec_stmt
1422 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1423 && !STMT_VINFO_RELEVANT (def_stmt_info))
1424 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1425 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1426 gcc_assert (vec_stmt);
1427 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1428 vec_oprnd = PHI_RESULT (vec_stmt);
1429 else if (is_gimple_call (vec_stmt))
1430 vec_oprnd = gimple_call_lhs (vec_stmt);
1431 else
1432 vec_oprnd = gimple_assign_lhs (vec_stmt);
1433 return vec_oprnd;
1436 /* Case 4: operand is defined by a loop header phi - reduction */
1437 case vect_reduction_def:
1438 case vect_double_reduction_def:
1439 case vect_nested_cycle:
1441 struct loop *loop;
1443 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1444 loop = (gimple_bb (def_stmt))->loop_father;
1446 /* Get the def before the loop */
1447 op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
1448 return get_initial_def_for_reduction (stmt, op, scalar_def);
1451 /* Case 5: operand is defined by loop-header phi - induction. */
1452 case vect_induction_def:
1454 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1456 /* Get the def from the vectorized stmt. */
1457 def_stmt_info = vinfo_for_stmt (def_stmt);
1458 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1459 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1460 vec_oprnd = PHI_RESULT (vec_stmt);
1461 else
1462 vec_oprnd = gimple_get_lhs (vec_stmt);
1463 return vec_oprnd;
1466 default:
1467 gcc_unreachable ();
1472 /* Function vect_get_vec_def_for_stmt_copy
1474 Return a vector-def for an operand. This function is used when the
1475 vectorized stmt to be created (by the caller to this function) is a "copy"
1476 created in case the vectorized result cannot fit in one vector, and several
1477 copies of the vector-stmt are required. In this case the vector-def is
1478 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1479 of the stmt that defines VEC_OPRND.
1480 DT is the type of the vector def VEC_OPRND.
1482 Context:
1483 In case the vectorization factor (VF) is bigger than the number
1484 of elements that can fit in a vectype (nunits), we have to generate
1485 more than one vector stmt to vectorize the scalar stmt. This situation
1486 arises when there are multiple data-types operated upon in the loop; the
1487 smallest data-type determines the VF, and as a result, when vectorizing
1488 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1489 vector stmt (each computing a vector of 'nunits' results, and together
1490 computing 'VF' results in each iteration). This function is called when
1491 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1492 which VF=16 and nunits=4, so the number of copies required is 4):
1494 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1496 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1497 VS1.1: vx.1 = memref1 VS1.2
1498 VS1.2: vx.2 = memref2 VS1.3
1499 VS1.3: vx.3 = memref3
1501 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1502 VSnew.1: vz1 = vx.1 + ... VSnew.2
1503 VSnew.2: vz2 = vx.2 + ... VSnew.3
1504 VSnew.3: vz3 = vx.3 + ...
1506 The vectorization of S1 is explained in vectorizable_load.
1507 The vectorization of S2:
1508 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1509 the function 'vect_get_vec_def_for_operand' is called to
1510 get the relevant vector-def for each operand of S2. For operand x it
1511 returns the vector-def 'vx.0'.
1513 To create the remaining copies of the vector-stmt (VSnew.j), this
1514 function is called to get the relevant vector-def for each operand. It is
1515 obtained from the respective VS1.j stmt, which is recorded in the
1516 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1518 For example, to obtain the vector-def 'vx.1' in order to create the
1519 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1520 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1521 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1522 and return its def ('vx.1').
1523 Overall, to create the above sequence this function will be called 3 times:
1524 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1525 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1526 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1528 tree
1529 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1531 gimple vec_stmt_for_operand;
1532 stmt_vec_info def_stmt_info;
1534 /* Do nothing; can reuse same def. */
1535 if (dt == vect_external_def || dt == vect_constant_def )
1536 return vec_oprnd;
1538 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1539 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1540 gcc_assert (def_stmt_info);
1541 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1542 gcc_assert (vec_stmt_for_operand);
1543 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1544 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1545 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1546 else
1547 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1548 return vec_oprnd;
1552 /* Get vectorized definitions for the operands to create a copy of an original
1553 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1555 static void
1556 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1557 vec<tree> *vec_oprnds0,
1558 vec<tree> *vec_oprnds1)
1560 tree vec_oprnd = vec_oprnds0->pop ();
1562 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1563 vec_oprnds0->quick_push (vec_oprnd);
1565 if (vec_oprnds1 && vec_oprnds1->length ())
1567 vec_oprnd = vec_oprnds1->pop ();
1568 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1569 vec_oprnds1->quick_push (vec_oprnd);
1574 /* Get vectorized definitions for OP0 and OP1.
1575 REDUC_INDEX is the index of reduction operand in case of reduction,
1576 and -1 otherwise. */
1578 void
1579 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
1580 vec<tree> *vec_oprnds0,
1581 vec<tree> *vec_oprnds1,
1582 slp_tree slp_node, int reduc_index)
1584 if (slp_node)
1586 int nops = (op1 == NULL_TREE) ? 1 : 2;
1587 auto_vec<tree> ops (nops);
1588 auto_vec<vec<tree> > vec_defs (nops);
1590 ops.quick_push (op0);
1591 if (op1)
1592 ops.quick_push (op1);
1594 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1596 *vec_oprnds0 = vec_defs[0];
1597 if (op1)
1598 *vec_oprnds1 = vec_defs[1];
1600 else
1602 tree vec_oprnd;
1604 vec_oprnds0->create (1);
1605 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);
1606 vec_oprnds0->quick_push (vec_oprnd);
1608 if (op1)
1610 vec_oprnds1->create (1);
1611 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);
1612 vec_oprnds1->quick_push (vec_oprnd);
1618 /* Function vect_finish_stmt_generation.
1620 Insert a new stmt. */
1622 void
1623 vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
1624 gimple_stmt_iterator *gsi)
1626 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1627 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1628 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1630 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1632 if (!gsi_end_p (*gsi)
1633 && gimple_has_mem_ops (vec_stmt))
1635 gimple at_stmt = gsi_stmt (*gsi);
1636 tree vuse = gimple_vuse (at_stmt);
1637 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1639 tree vdef = gimple_vdef (at_stmt);
1640 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1641 /* If we have an SSA vuse and insert a store, update virtual
1642 SSA form to avoid triggering the renamer. Do so only
1643 if we can easily see all uses - which is what almost always
1644 happens with the way vectorized stmts are inserted. */
1645 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1646 && ((is_gimple_assign (vec_stmt)
1647 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1648 || (is_gimple_call (vec_stmt)
1649 && !(gimple_call_flags (vec_stmt)
1650 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1652 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1653 gimple_set_vdef (vec_stmt, new_vdef);
1654 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1658 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1660 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo,
1661 bb_vinfo));
1663 if (dump_enabled_p ())
1665 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1666 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1667 dump_printf (MSG_NOTE, "\n");
1670 gimple_set_location (vec_stmt, gimple_location (stmt));
1673 /* Checks if CALL can be vectorized in type VECTYPE. Returns
1674 a function declaration if the target has a vectorized version
1675 of the function, or NULL_TREE if the function cannot be vectorized. */
1677 tree
1678 vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
1680 tree fndecl = gimple_call_fndecl (call);
1682 /* We only handle functions that do not read or clobber memory -- i.e.
1683 const or novops ones. */
1684 if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
1685 return NULL_TREE;
1687 if (!fndecl
1688 || TREE_CODE (fndecl) != FUNCTION_DECL
1689 || !DECL_BUILT_IN (fndecl))
1690 return NULL_TREE;
1692 return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
1693 vectype_in);
1696 /* Function vectorizable_call.
1698 Check if STMT performs a function call that can be vectorized.
1699 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1700 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
1701 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1703 static bool
1704 vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
1705 slp_tree slp_node)
1707 tree vec_dest;
1708 tree scalar_dest;
1709 tree op, type;
1710 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
1711 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
1712 tree vectype_out, vectype_in;
1713 int nunits_in;
1714 int nunits_out;
1715 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1716 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1717 tree fndecl, new_temp, def, rhs_type;
1718 gimple def_stmt;
1719 enum vect_def_type dt[3]
1720 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
1721 gimple new_stmt = NULL;
1722 int ncopies, j;
1723 vec<tree> vargs = vNULL;
1724 enum { NARROW, NONE, WIDEN } modifier;
1725 size_t i, nargs;
1726 tree lhs;
1728 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
1729 return false;
1731 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
1732 return false;
1734 /* Is STMT a vectorizable call? */
1735 if (!is_gimple_call (stmt))
1736 return false;
1738 if (TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
1739 return false;
1741 if (stmt_can_throw_internal (stmt))
1742 return false;
1744 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
1746 /* Process function arguments. */
1747 rhs_type = NULL_TREE;
1748 vectype_in = NULL_TREE;
1749 nargs = gimple_call_num_args (stmt);
1751 /* Bail out if the function has more than three arguments, we do not have
1752 interesting builtin functions to vectorize with more than two arguments
1753 except for fma. No arguments is also not good. */
1754 if (nargs == 0 || nargs > 3)
1755 return false;
1757 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
1758 if (gimple_call_internal_p (stmt)
1759 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
1761 nargs = 0;
1762 rhs_type = unsigned_type_node;
1765 for (i = 0; i < nargs; i++)
1767 tree opvectype;
1769 op = gimple_call_arg (stmt, i);
1771 /* We can only handle calls with arguments of the same type. */
1772 if (rhs_type
1773 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
1775 if (dump_enabled_p ())
1776 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1777 "argument types differ.\n");
1778 return false;
1780 if (!rhs_type)
1781 rhs_type = TREE_TYPE (op);
1783 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
1784 &def_stmt, &def, &dt[i], &opvectype))
1786 if (dump_enabled_p ())
1787 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1788 "use not simple.\n");
1789 return false;
1792 if (!vectype_in)
1793 vectype_in = opvectype;
1794 else if (opvectype
1795 && opvectype != vectype_in)
1797 if (dump_enabled_p ())
1798 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1799 "argument vector types differ.\n");
1800 return false;
1803 /* If all arguments are external or constant defs use a vector type with
1804 the same size as the output vector type. */
1805 if (!vectype_in)
1806 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
1807 if (vec_stmt)
1808 gcc_assert (vectype_in);
1809 if (!vectype_in)
1811 if (dump_enabled_p ())
1813 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1814 "no vectype for scalar type ");
1815 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
1816 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1819 return false;
1822 /* FORNOW */
1823 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
1824 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
1825 if (nunits_in == nunits_out / 2)
1826 modifier = NARROW;
1827 else if (nunits_out == nunits_in)
1828 modifier = NONE;
1829 else if (nunits_out == nunits_in / 2)
1830 modifier = WIDEN;
1831 else
1832 return false;
1834 /* For now, we only vectorize functions if a target specific builtin
1835 is available. TODO -- in some cases, it might be profitable to
1836 insert the calls for pieces of the vector, in order to be able
1837 to vectorize other operations in the loop. */
1838 fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
1839 if (fndecl == NULL_TREE)
1841 if (gimple_call_internal_p (stmt)
1842 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
1843 && !slp_node
1844 && loop_vinfo
1845 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
1846 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
1847 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
1848 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
1850 /* We can handle IFN_GOMP_SIMD_LANE by returning a
1851 { 0, 1, 2, ... vf - 1 } vector. */
1852 gcc_assert (nargs == 0);
1854 else
1856 if (dump_enabled_p ())
1857 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1858 "function is not vectorizable.\n");
1859 return false;
1863 gcc_assert (!gimple_vuse (stmt));
1865 if (slp_node || PURE_SLP_STMT (stmt_info))
1866 ncopies = 1;
1867 else if (modifier == NARROW)
1868 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
1869 else
1870 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
1872 /* Sanity check: make sure that at least one copy of the vectorized stmt
1873 needs to be generated. */
1874 gcc_assert (ncopies >= 1);
1876 if (!vec_stmt) /* transformation not required. */
1878 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
1879 if (dump_enabled_p ())
1880 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
1881 "\n");
1882 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
1883 return true;
1886 /** Transform. **/
1888 if (dump_enabled_p ())
1889 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
1891 /* Handle def. */
1892 scalar_dest = gimple_call_lhs (stmt);
1893 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
1895 prev_stmt_info = NULL;
1896 switch (modifier)
1898 case NONE:
1899 for (j = 0; j < ncopies; ++j)
1901 /* Build argument list for the vectorized call. */
1902 if (j == 0)
1903 vargs.create (nargs);
1904 else
1905 vargs.truncate (0);
1907 if (slp_node)
1909 auto_vec<vec<tree> > vec_defs (nargs);
1910 vec<tree> vec_oprnds0;
1912 for (i = 0; i < nargs; i++)
1913 vargs.quick_push (gimple_call_arg (stmt, i));
1914 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
1915 vec_oprnds0 = vec_defs[0];
1917 /* Arguments are ready. Create the new vector stmt. */
1918 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
1920 size_t k;
1921 for (k = 0; k < nargs; k++)
1923 vec<tree> vec_oprndsk = vec_defs[k];
1924 vargs[k] = vec_oprndsk[i];
1926 new_stmt = gimple_build_call_vec (fndecl, vargs);
1927 new_temp = make_ssa_name (vec_dest, new_stmt);
1928 gimple_call_set_lhs (new_stmt, new_temp);
1929 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1930 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
1933 for (i = 0; i < nargs; i++)
1935 vec<tree> vec_oprndsi = vec_defs[i];
1936 vec_oprndsi.release ();
1938 continue;
1941 for (i = 0; i < nargs; i++)
1943 op = gimple_call_arg (stmt, i);
1944 if (j == 0)
1945 vec_oprnd0
1946 = vect_get_vec_def_for_operand (op, stmt, NULL);
1947 else
1949 vec_oprnd0 = gimple_call_arg (new_stmt, i);
1950 vec_oprnd0
1951 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
1954 vargs.quick_push (vec_oprnd0);
1957 if (gimple_call_internal_p (stmt)
1958 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
1960 tree *v = XALLOCAVEC (tree, nunits_out);
1961 int k;
1962 for (k = 0; k < nunits_out; ++k)
1963 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
1964 tree cst = build_vector (vectype_out, v);
1965 tree new_var
1966 = vect_get_new_vect_var (vectype_out, vect_simple_var, "cst_");
1967 gimple init_stmt = gimple_build_assign (new_var, cst);
1968 new_temp = make_ssa_name (new_var, init_stmt);
1969 gimple_assign_set_lhs (init_stmt, new_temp);
1970 vect_init_vector_1 (stmt, init_stmt, NULL);
1971 new_temp = make_ssa_name (vec_dest, NULL);
1972 new_stmt = gimple_build_assign (new_temp,
1973 gimple_assign_lhs (init_stmt));
1975 else
1977 new_stmt = gimple_build_call_vec (fndecl, vargs);
1978 new_temp = make_ssa_name (vec_dest, new_stmt);
1979 gimple_call_set_lhs (new_stmt, new_temp);
1981 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1983 if (j == 0)
1984 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
1985 else
1986 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
1988 prev_stmt_info = vinfo_for_stmt (new_stmt);
1991 break;
1993 case NARROW:
1994 for (j = 0; j < ncopies; ++j)
1996 /* Build argument list for the vectorized call. */
1997 if (j == 0)
1998 vargs.create (nargs * 2);
1999 else
2000 vargs.truncate (0);
2002 if (slp_node)
2004 auto_vec<vec<tree> > vec_defs (nargs);
2005 vec<tree> vec_oprnds0;
2007 for (i = 0; i < nargs; i++)
2008 vargs.quick_push (gimple_call_arg (stmt, i));
2009 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2010 vec_oprnds0 = vec_defs[0];
2012 /* Arguments are ready. Create the new vector stmt. */
2013 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2015 size_t k;
2016 vargs.truncate (0);
2017 for (k = 0; k < nargs; k++)
2019 vec<tree> vec_oprndsk = vec_defs[k];
2020 vargs.quick_push (vec_oprndsk[i]);
2021 vargs.quick_push (vec_oprndsk[i + 1]);
2023 new_stmt = gimple_build_call_vec (fndecl, vargs);
2024 new_temp = make_ssa_name (vec_dest, new_stmt);
2025 gimple_call_set_lhs (new_stmt, new_temp);
2026 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2027 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2030 for (i = 0; i < nargs; i++)
2032 vec<tree> vec_oprndsi = vec_defs[i];
2033 vec_oprndsi.release ();
2035 continue;
2038 for (i = 0; i < nargs; i++)
2040 op = gimple_call_arg (stmt, i);
2041 if (j == 0)
2043 vec_oprnd0
2044 = vect_get_vec_def_for_operand (op, stmt, NULL);
2045 vec_oprnd1
2046 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2048 else
2050 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2051 vec_oprnd0
2052 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2053 vec_oprnd1
2054 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2057 vargs.quick_push (vec_oprnd0);
2058 vargs.quick_push (vec_oprnd1);
2061 new_stmt = gimple_build_call_vec (fndecl, vargs);
2062 new_temp = make_ssa_name (vec_dest, new_stmt);
2063 gimple_call_set_lhs (new_stmt, new_temp);
2064 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2066 if (j == 0)
2067 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2068 else
2069 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2071 prev_stmt_info = vinfo_for_stmt (new_stmt);
2074 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2076 break;
2078 case WIDEN:
2079 /* No current target implements this case. */
2080 return false;
2083 vargs.release ();
2085 /* Update the exception handling table with the vector stmt if necessary. */
2086 if (maybe_clean_or_replace_eh_stmt (stmt, *vec_stmt))
2087 gimple_purge_dead_eh_edges (gimple_bb (stmt));
2089 /* The call in STMT might prevent it from being removed in dce.
2090 We however cannot remove it here, due to the way the ssa name
2091 it defines is mapped to the new definition. So just replace
2092 rhs of the statement with something harmless. */
2094 if (slp_node)
2095 return true;
2097 type = TREE_TYPE (scalar_dest);
2098 if (is_pattern_stmt_p (stmt_info))
2099 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2100 else
2101 lhs = gimple_call_lhs (stmt);
2102 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2103 set_vinfo_for_stmt (new_stmt, stmt_info);
2104 set_vinfo_for_stmt (stmt, NULL);
2105 STMT_VINFO_STMT (stmt_info) = new_stmt;
2106 gsi_replace (gsi, new_stmt, false);
2108 return true;
2112 /* Function vect_gen_widened_results_half
2114 Create a vector stmt whose code, type, number of arguments, and result
2115 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
2116 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
2117 In the case that CODE is a CALL_EXPR, this means that a call to DECL
2118 needs to be created (DECL is a function-decl of a target-builtin).
2119 STMT is the original scalar stmt that we are vectorizing. */
2121 static gimple
2122 vect_gen_widened_results_half (enum tree_code code,
2123 tree decl,
2124 tree vec_oprnd0, tree vec_oprnd1, int op_type,
2125 tree vec_dest, gimple_stmt_iterator *gsi,
2126 gimple stmt)
2128 gimple new_stmt;
2129 tree new_temp;
2131 /* Generate half of the widened result: */
2132 if (code == CALL_EXPR)
2134 /* Target specific support */
2135 if (op_type == binary_op)
2136 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
2137 else
2138 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
2139 new_temp = make_ssa_name (vec_dest, new_stmt);
2140 gimple_call_set_lhs (new_stmt, new_temp);
2142 else
2144 /* Generic support */
2145 gcc_assert (op_type == TREE_CODE_LENGTH (code));
2146 if (op_type != binary_op)
2147 vec_oprnd1 = NULL;
2148 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vec_oprnd0,
2149 vec_oprnd1);
2150 new_temp = make_ssa_name (vec_dest, new_stmt);
2151 gimple_assign_set_lhs (new_stmt, new_temp);
2153 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2155 return new_stmt;
2159 /* Get vectorized definitions for loop-based vectorization. For the first
2160 operand we call vect_get_vec_def_for_operand() (with OPRND containing
2161 scalar operand), and for the rest we get a copy with
2162 vect_get_vec_def_for_stmt_copy() using the previous vector definition
2163 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
2164 The vectors are collected into VEC_OPRNDS. */
2166 static void
2167 vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
2168 vec<tree> *vec_oprnds, int multi_step_cvt)
2170 tree vec_oprnd;
2172 /* Get first vector operand. */
2173 /* All the vector operands except the very first one (that is scalar oprnd)
2174 are stmt copies. */
2175 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
2176 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
2177 else
2178 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
2180 vec_oprnds->quick_push (vec_oprnd);
2182 /* Get second vector operand. */
2183 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
2184 vec_oprnds->quick_push (vec_oprnd);
2186 *oprnd = vec_oprnd;
2188 /* For conversion in multiple steps, continue to get operands
2189 recursively. */
2190 if (multi_step_cvt)
2191 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
2195 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
2196 For multi-step conversions store the resulting vectors and call the function
2197 recursively. */
2199 static void
2200 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
2201 int multi_step_cvt, gimple stmt,
2202 vec<tree> vec_dsts,
2203 gimple_stmt_iterator *gsi,
2204 slp_tree slp_node, enum tree_code code,
2205 stmt_vec_info *prev_stmt_info)
2207 unsigned int i;
2208 tree vop0, vop1, new_tmp, vec_dest;
2209 gimple new_stmt;
2210 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2212 vec_dest = vec_dsts.pop ();
2214 for (i = 0; i < vec_oprnds->length (); i += 2)
2216 /* Create demotion operation. */
2217 vop0 = (*vec_oprnds)[i];
2218 vop1 = (*vec_oprnds)[i + 1];
2219 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
2220 new_tmp = make_ssa_name (vec_dest, new_stmt);
2221 gimple_assign_set_lhs (new_stmt, new_tmp);
2222 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2224 if (multi_step_cvt)
2225 /* Store the resulting vector for next recursive call. */
2226 (*vec_oprnds)[i/2] = new_tmp;
2227 else
2229 /* This is the last step of the conversion sequence. Store the
2230 vectors in SLP_NODE or in vector info of the scalar statement
2231 (or in STMT_VINFO_RELATED_STMT chain). */
2232 if (slp_node)
2233 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2234 else
2236 if (!*prev_stmt_info)
2237 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2238 else
2239 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
2241 *prev_stmt_info = vinfo_for_stmt (new_stmt);
2246 /* For multi-step demotion operations we first generate demotion operations
2247 from the source type to the intermediate types, and then combine the
2248 results (stored in VEC_OPRNDS) in demotion operation to the destination
2249 type. */
2250 if (multi_step_cvt)
2252 /* At each level of recursion we have half of the operands we had at the
2253 previous level. */
2254 vec_oprnds->truncate ((i+1)/2);
2255 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
2256 stmt, vec_dsts, gsi, slp_node,
2257 VEC_PACK_TRUNC_EXPR,
2258 prev_stmt_info);
2261 vec_dsts.quick_push (vec_dest);
2265 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
2266 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
2267 the resulting vectors and call the function recursively. */
2269 static void
2270 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
2271 vec<tree> *vec_oprnds1,
2272 gimple stmt, tree vec_dest,
2273 gimple_stmt_iterator *gsi,
2274 enum tree_code code1,
2275 enum tree_code code2, tree decl1,
2276 tree decl2, int op_type)
2278 int i;
2279 tree vop0, vop1, new_tmp1, new_tmp2;
2280 gimple new_stmt1, new_stmt2;
2281 vec<tree> vec_tmp = vNULL;
2283 vec_tmp.create (vec_oprnds0->length () * 2);
2284 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
2286 if (op_type == binary_op)
2287 vop1 = (*vec_oprnds1)[i];
2288 else
2289 vop1 = NULL_TREE;
2291 /* Generate the two halves of promotion operation. */
2292 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
2293 op_type, vec_dest, gsi, stmt);
2294 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
2295 op_type, vec_dest, gsi, stmt);
2296 if (is_gimple_call (new_stmt1))
2298 new_tmp1 = gimple_call_lhs (new_stmt1);
2299 new_tmp2 = gimple_call_lhs (new_stmt2);
2301 else
2303 new_tmp1 = gimple_assign_lhs (new_stmt1);
2304 new_tmp2 = gimple_assign_lhs (new_stmt2);
2307 /* Store the results for the next step. */
2308 vec_tmp.quick_push (new_tmp1);
2309 vec_tmp.quick_push (new_tmp2);
2312 vec_oprnds0->release ();
2313 *vec_oprnds0 = vec_tmp;
2317 /* Check if STMT performs a conversion operation, that can be vectorized.
2318 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2319 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
2320 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2322 static bool
2323 vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
2324 gimple *vec_stmt, slp_tree slp_node)
2326 tree vec_dest;
2327 tree scalar_dest;
2328 tree op0, op1 = NULL_TREE;
2329 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2330 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2331 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2332 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
2333 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
2334 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
2335 tree new_temp;
2336 tree def;
2337 gimple def_stmt;
2338 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
2339 gimple new_stmt = NULL;
2340 stmt_vec_info prev_stmt_info;
2341 int nunits_in;
2342 int nunits_out;
2343 tree vectype_out, vectype_in;
2344 int ncopies, i, j;
2345 tree lhs_type, rhs_type;
2346 enum { NARROW, NONE, WIDEN } modifier;
2347 vec<tree> vec_oprnds0 = vNULL;
2348 vec<tree> vec_oprnds1 = vNULL;
2349 tree vop0;
2350 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2351 int multi_step_cvt = 0;
2352 vec<tree> vec_dsts = vNULL;
2353 vec<tree> interm_types = vNULL;
2354 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
2355 int op_type;
2356 enum machine_mode rhs_mode;
2357 unsigned short fltsz;
2359 /* Is STMT a vectorizable conversion? */
2361 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2362 return false;
2364 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2365 return false;
2367 if (!is_gimple_assign (stmt))
2368 return false;
2370 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2371 return false;
2373 code = gimple_assign_rhs_code (stmt);
2374 if (!CONVERT_EXPR_CODE_P (code)
2375 && code != FIX_TRUNC_EXPR
2376 && code != FLOAT_EXPR
2377 && code != WIDEN_MULT_EXPR
2378 && code != WIDEN_LSHIFT_EXPR)
2379 return false;
2381 op_type = TREE_CODE_LENGTH (code);
2383 /* Check types of lhs and rhs. */
2384 scalar_dest = gimple_assign_lhs (stmt);
2385 lhs_type = TREE_TYPE (scalar_dest);
2386 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2388 op0 = gimple_assign_rhs1 (stmt);
2389 rhs_type = TREE_TYPE (op0);
2391 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
2392 && !((INTEGRAL_TYPE_P (lhs_type)
2393 && INTEGRAL_TYPE_P (rhs_type))
2394 || (SCALAR_FLOAT_TYPE_P (lhs_type)
2395 && SCALAR_FLOAT_TYPE_P (rhs_type))))
2396 return false;
2398 if ((INTEGRAL_TYPE_P (lhs_type)
2399 && (TYPE_PRECISION (lhs_type)
2400 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
2401 || (INTEGRAL_TYPE_P (rhs_type)
2402 && (TYPE_PRECISION (rhs_type)
2403 != GET_MODE_PRECISION (TYPE_MODE (rhs_type)))))
2405 if (dump_enabled_p ())
2406 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2407 "type conversion to/from bit-precision unsupported."
2408 "\n");
2409 return false;
2412 /* Check the operands of the operation. */
2413 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
2414 &def_stmt, &def, &dt[0], &vectype_in))
2416 if (dump_enabled_p ())
2417 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2418 "use not simple.\n");
2419 return false;
2421 if (op_type == binary_op)
2423 bool ok;
2425 op1 = gimple_assign_rhs2 (stmt);
2426 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
2427 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
2428 OP1. */
2429 if (CONSTANT_CLASS_P (op0))
2430 ok = vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo,
2431 &def_stmt, &def, &dt[1], &vectype_in);
2432 else
2433 ok = vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
2434 &def, &dt[1]);
2436 if (!ok)
2438 if (dump_enabled_p ())
2439 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2440 "use not simple.\n");
2441 return false;
2445 /* If op0 is an external or constant defs use a vector type of
2446 the same size as the output vector type. */
2447 if (!vectype_in)
2448 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
2449 if (vec_stmt)
2450 gcc_assert (vectype_in);
2451 if (!vectype_in)
2453 if (dump_enabled_p ())
2455 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2456 "no vectype for scalar type ");
2457 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
2458 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2461 return false;
2464 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
2465 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
2466 if (nunits_in < nunits_out)
2467 modifier = NARROW;
2468 else if (nunits_out == nunits_in)
2469 modifier = NONE;
2470 else
2471 modifier = WIDEN;
2473 /* Multiple types in SLP are handled by creating the appropriate number of
2474 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
2475 case of SLP. */
2476 if (slp_node || PURE_SLP_STMT (stmt_info))
2477 ncopies = 1;
2478 else if (modifier == NARROW)
2479 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
2480 else
2481 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
2483 /* Sanity check: make sure that at least one copy of the vectorized stmt
2484 needs to be generated. */
2485 gcc_assert (ncopies >= 1);
2487 /* Supportable by target? */
2488 switch (modifier)
2490 case NONE:
2491 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
2492 return false;
2493 if (supportable_convert_operation (code, vectype_out, vectype_in,
2494 &decl1, &code1))
2495 break;
2496 /* FALLTHRU */
2497 unsupported:
2498 if (dump_enabled_p ())
2499 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2500 "conversion not supported by target.\n");
2501 return false;
2503 case WIDEN:
2504 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
2505 &code1, &code2, &multi_step_cvt,
2506 &interm_types))
2508 /* Binary widening operation can only be supported directly by the
2509 architecture. */
2510 gcc_assert (!(multi_step_cvt && op_type == binary_op));
2511 break;
2514 if (code != FLOAT_EXPR
2515 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
2516 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
2517 goto unsupported;
2519 rhs_mode = TYPE_MODE (rhs_type);
2520 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
2521 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
2522 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
2523 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
2525 cvt_type
2526 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
2527 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
2528 if (cvt_type == NULL_TREE)
2529 goto unsupported;
2531 if (GET_MODE_SIZE (rhs_mode) == fltsz)
2533 if (!supportable_convert_operation (code, vectype_out,
2534 cvt_type, &decl1, &codecvt1))
2535 goto unsupported;
2537 else if (!supportable_widening_operation (code, stmt, vectype_out,
2538 cvt_type, &codecvt1,
2539 &codecvt2, &multi_step_cvt,
2540 &interm_types))
2541 continue;
2542 else
2543 gcc_assert (multi_step_cvt == 0);
2545 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
2546 vectype_in, &code1, &code2,
2547 &multi_step_cvt, &interm_types))
2548 break;
2551 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
2552 goto unsupported;
2554 if (GET_MODE_SIZE (rhs_mode) == fltsz)
2555 codecvt2 = ERROR_MARK;
2556 else
2558 multi_step_cvt++;
2559 interm_types.safe_push (cvt_type);
2560 cvt_type = NULL_TREE;
2562 break;
2564 case NARROW:
2565 gcc_assert (op_type == unary_op);
2566 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
2567 &code1, &multi_step_cvt,
2568 &interm_types))
2569 break;
2571 if (code != FIX_TRUNC_EXPR
2572 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
2573 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
2574 goto unsupported;
2576 rhs_mode = TYPE_MODE (rhs_type);
2577 cvt_type
2578 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
2579 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
2580 if (cvt_type == NULL_TREE)
2581 goto unsupported;
2582 if (!supportable_convert_operation (code, cvt_type, vectype_in,
2583 &decl1, &codecvt1))
2584 goto unsupported;
2585 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
2586 &code1, &multi_step_cvt,
2587 &interm_types))
2588 break;
2589 goto unsupported;
2591 default:
2592 gcc_unreachable ();
2595 if (!vec_stmt) /* transformation not required. */
2597 if (dump_enabled_p ())
2598 dump_printf_loc (MSG_NOTE, vect_location,
2599 "=== vectorizable_conversion ===\n");
2600 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
2602 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
2603 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
2605 else if (modifier == NARROW)
2607 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
2608 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
2610 else
2612 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
2613 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
2615 interm_types.release ();
2616 return true;
2619 /** Transform. **/
2620 if (dump_enabled_p ())
2621 dump_printf_loc (MSG_NOTE, vect_location,
2622 "transform conversion. ncopies = %d.\n", ncopies);
2624 if (op_type == binary_op)
2626 if (CONSTANT_CLASS_P (op0))
2627 op0 = fold_convert (TREE_TYPE (op1), op0);
2628 else if (CONSTANT_CLASS_P (op1))
2629 op1 = fold_convert (TREE_TYPE (op0), op1);
2632 /* In case of multi-step conversion, we first generate conversion operations
2633 to the intermediate types, and then from that types to the final one.
2634 We create vector destinations for the intermediate type (TYPES) received
2635 from supportable_*_operation, and store them in the correct order
2636 for future use in vect_create_vectorized_*_stmts (). */
2637 vec_dsts.create (multi_step_cvt + 1);
2638 vec_dest = vect_create_destination_var (scalar_dest,
2639 (cvt_type && modifier == WIDEN)
2640 ? cvt_type : vectype_out);
2641 vec_dsts.quick_push (vec_dest);
2643 if (multi_step_cvt)
2645 for (i = interm_types.length () - 1;
2646 interm_types.iterate (i, &intermediate_type); i--)
2648 vec_dest = vect_create_destination_var (scalar_dest,
2649 intermediate_type);
2650 vec_dsts.quick_push (vec_dest);
2654 if (cvt_type)
2655 vec_dest = vect_create_destination_var (scalar_dest,
2656 modifier == WIDEN
2657 ? vectype_out : cvt_type);
2659 if (!slp_node)
2661 if (modifier == WIDEN)
2663 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
2664 if (op_type == binary_op)
2665 vec_oprnds1.create (1);
2667 else if (modifier == NARROW)
2668 vec_oprnds0.create (
2669 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
2671 else if (code == WIDEN_LSHIFT_EXPR)
2672 vec_oprnds1.create (slp_node->vec_stmts_size);
2674 last_oprnd = op0;
2675 prev_stmt_info = NULL;
2676 switch (modifier)
2678 case NONE:
2679 for (j = 0; j < ncopies; j++)
2681 if (j == 0)
2682 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
2683 -1);
2684 else
2685 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
2687 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
2689 /* Arguments are ready, create the new vector stmt. */
2690 if (code1 == CALL_EXPR)
2692 new_stmt = gimple_build_call (decl1, 1, vop0);
2693 new_temp = make_ssa_name (vec_dest, new_stmt);
2694 gimple_call_set_lhs (new_stmt, new_temp);
2696 else
2698 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
2699 new_stmt = gimple_build_assign_with_ops (code1, vec_dest,
2700 vop0, NULL);
2701 new_temp = make_ssa_name (vec_dest, new_stmt);
2702 gimple_assign_set_lhs (new_stmt, new_temp);
2705 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2706 if (slp_node)
2707 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2710 if (j == 0)
2711 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2712 else
2713 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2714 prev_stmt_info = vinfo_for_stmt (new_stmt);
2716 break;
2718 case WIDEN:
2719 /* In case the vectorization factor (VF) is bigger than the number
2720 of elements that we can fit in a vectype (nunits), we have to
2721 generate more than one vector stmt - i.e - we need to "unroll"
2722 the vector stmt by a factor VF/nunits. */
2723 for (j = 0; j < ncopies; j++)
2725 /* Handle uses. */
2726 if (j == 0)
2728 if (slp_node)
2730 if (code == WIDEN_LSHIFT_EXPR)
2732 unsigned int k;
2734 vec_oprnd1 = op1;
2735 /* Store vec_oprnd1 for every vector stmt to be created
2736 for SLP_NODE. We check during the analysis that all
2737 the shift arguments are the same. */
2738 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
2739 vec_oprnds1.quick_push (vec_oprnd1);
2741 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
2742 slp_node, -1);
2744 else
2745 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
2746 &vec_oprnds1, slp_node, -1);
2748 else
2750 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
2751 vec_oprnds0.quick_push (vec_oprnd0);
2752 if (op_type == binary_op)
2754 if (code == WIDEN_LSHIFT_EXPR)
2755 vec_oprnd1 = op1;
2756 else
2757 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt,
2758 NULL);
2759 vec_oprnds1.quick_push (vec_oprnd1);
2763 else
2765 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
2766 vec_oprnds0.truncate (0);
2767 vec_oprnds0.quick_push (vec_oprnd0);
2768 if (op_type == binary_op)
2770 if (code == WIDEN_LSHIFT_EXPR)
2771 vec_oprnd1 = op1;
2772 else
2773 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
2774 vec_oprnd1);
2775 vec_oprnds1.truncate (0);
2776 vec_oprnds1.quick_push (vec_oprnd1);
2780 /* Arguments are ready. Create the new vector stmts. */
2781 for (i = multi_step_cvt; i >= 0; i--)
2783 tree this_dest = vec_dsts[i];
2784 enum tree_code c1 = code1, c2 = code2;
2785 if (i == 0 && codecvt2 != ERROR_MARK)
2787 c1 = codecvt1;
2788 c2 = codecvt2;
2790 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
2791 &vec_oprnds1,
2792 stmt, this_dest, gsi,
2793 c1, c2, decl1, decl2,
2794 op_type);
2797 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
2799 if (cvt_type)
2801 if (codecvt1 == CALL_EXPR)
2803 new_stmt = gimple_build_call (decl1, 1, vop0);
2804 new_temp = make_ssa_name (vec_dest, new_stmt);
2805 gimple_call_set_lhs (new_stmt, new_temp);
2807 else
2809 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
2810 new_temp = make_ssa_name (vec_dest, NULL);
2811 new_stmt = gimple_build_assign_with_ops (codecvt1,
2812 new_temp,
2813 vop0, NULL);
2816 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2818 else
2819 new_stmt = SSA_NAME_DEF_STMT (vop0);
2821 if (slp_node)
2822 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2823 else
2825 if (!prev_stmt_info)
2826 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2827 else
2828 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2829 prev_stmt_info = vinfo_for_stmt (new_stmt);
2834 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2835 break;
2837 case NARROW:
2838 /* In case the vectorization factor (VF) is bigger than the number
2839 of elements that we can fit in a vectype (nunits), we have to
2840 generate more than one vector stmt - i.e - we need to "unroll"
2841 the vector stmt by a factor VF/nunits. */
2842 for (j = 0; j < ncopies; j++)
2844 /* Handle uses. */
2845 if (slp_node)
2846 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
2847 slp_node, -1);
2848 else
2850 vec_oprnds0.truncate (0);
2851 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
2852 vect_pow2 (multi_step_cvt) - 1);
2855 /* Arguments are ready. Create the new vector stmts. */
2856 if (cvt_type)
2857 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
2859 if (codecvt1 == CALL_EXPR)
2861 new_stmt = gimple_build_call (decl1, 1, vop0);
2862 new_temp = make_ssa_name (vec_dest, new_stmt);
2863 gimple_call_set_lhs (new_stmt, new_temp);
2865 else
2867 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
2868 new_temp = make_ssa_name (vec_dest, NULL);
2869 new_stmt = gimple_build_assign_with_ops (codecvt1, new_temp,
2870 vop0, NULL);
2873 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2874 vec_oprnds0[i] = new_temp;
2877 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
2878 stmt, vec_dsts, gsi,
2879 slp_node, code1,
2880 &prev_stmt_info);
2883 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2884 break;
2887 vec_oprnds0.release ();
2888 vec_oprnds1.release ();
2889 vec_dsts.release ();
2890 interm_types.release ();
2892 return true;
2896 /* Function vectorizable_assignment.
2898 Check if STMT performs an assignment (copy) that can be vectorized.
2899 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2900 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2901 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2903 static bool
2904 vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
2905 gimple *vec_stmt, slp_tree slp_node)
2907 tree vec_dest;
2908 tree scalar_dest;
2909 tree op;
2910 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2911 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2912 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2913 tree new_temp;
2914 tree def;
2915 gimple def_stmt;
2916 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
2917 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
2918 int ncopies;
2919 int i, j;
2920 vec<tree> vec_oprnds = vNULL;
2921 tree vop;
2922 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2923 gimple new_stmt = NULL;
2924 stmt_vec_info prev_stmt_info = NULL;
2925 enum tree_code code;
2926 tree vectype_in;
2928 /* Multiple types in SLP are handled by creating the appropriate number of
2929 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
2930 case of SLP. */
2931 if (slp_node || PURE_SLP_STMT (stmt_info))
2932 ncopies = 1;
2933 else
2934 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2936 gcc_assert (ncopies >= 1);
2938 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2939 return false;
2941 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2942 return false;
2944 /* Is vectorizable assignment? */
2945 if (!is_gimple_assign (stmt))
2946 return false;
2948 scalar_dest = gimple_assign_lhs (stmt);
2949 if (TREE_CODE (scalar_dest) != SSA_NAME)
2950 return false;
2952 code = gimple_assign_rhs_code (stmt);
2953 if (gimple_assign_single_p (stmt)
2954 || code == PAREN_EXPR
2955 || CONVERT_EXPR_CODE_P (code))
2956 op = gimple_assign_rhs1 (stmt);
2957 else
2958 return false;
2960 if (code == VIEW_CONVERT_EXPR)
2961 op = TREE_OPERAND (op, 0);
2963 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2964 &def_stmt, &def, &dt[0], &vectype_in))
2966 if (dump_enabled_p ())
2967 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2968 "use not simple.\n");
2969 return false;
2972 /* We can handle NOP_EXPR conversions that do not change the number
2973 of elements or the vector size. */
2974 if ((CONVERT_EXPR_CODE_P (code)
2975 || code == VIEW_CONVERT_EXPR)
2976 && (!vectype_in
2977 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
2978 || (GET_MODE_SIZE (TYPE_MODE (vectype))
2979 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
2980 return false;
2982 /* We do not handle bit-precision changes. */
2983 if ((CONVERT_EXPR_CODE_P (code)
2984 || code == VIEW_CONVERT_EXPR)
2985 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
2986 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
2987 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
2988 || ((TYPE_PRECISION (TREE_TYPE (op))
2989 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
2990 /* But a conversion that does not change the bit-pattern is ok. */
2991 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
2992 > TYPE_PRECISION (TREE_TYPE (op)))
2993 && TYPE_UNSIGNED (TREE_TYPE (op))))
2995 if (dump_enabled_p ())
2996 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2997 "type conversion to/from bit-precision "
2998 "unsupported.\n");
2999 return false;
3002 if (!vec_stmt) /* transformation not required. */
3004 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
3005 if (dump_enabled_p ())
3006 dump_printf_loc (MSG_NOTE, vect_location,
3007 "=== vectorizable_assignment ===\n");
3008 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3009 return true;
3012 /** Transform. **/
3013 if (dump_enabled_p ())
3014 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
3016 /* Handle def. */
3017 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3019 /* Handle use. */
3020 for (j = 0; j < ncopies; j++)
3022 /* Handle uses. */
3023 if (j == 0)
3024 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
3025 else
3026 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
3028 /* Arguments are ready. create the new vector stmt. */
3029 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3031 if (CONVERT_EXPR_CODE_P (code)
3032 || code == VIEW_CONVERT_EXPR)
3033 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
3034 new_stmt = gimple_build_assign (vec_dest, vop);
3035 new_temp = make_ssa_name (vec_dest, new_stmt);
3036 gimple_assign_set_lhs (new_stmt, new_temp);
3037 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3038 if (slp_node)
3039 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3042 if (slp_node)
3043 continue;
3045 if (j == 0)
3046 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3047 else
3048 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3050 prev_stmt_info = vinfo_for_stmt (new_stmt);
3053 vec_oprnds.release ();
3054 return true;
3058 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
3059 either as shift by a scalar or by a vector. */
3061 bool
3062 vect_supportable_shift (enum tree_code code, tree scalar_type)
3065 enum machine_mode vec_mode;
3066 optab optab;
3067 int icode;
3068 tree vectype;
3070 vectype = get_vectype_for_scalar_type (scalar_type);
3071 if (!vectype)
3072 return false;
3074 optab = optab_for_tree_code (code, vectype, optab_scalar);
3075 if (!optab
3076 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
3078 optab = optab_for_tree_code (code, vectype, optab_vector);
3079 if (!optab
3080 || (optab_handler (optab, TYPE_MODE (vectype))
3081 == CODE_FOR_nothing))
3082 return false;
3085 vec_mode = TYPE_MODE (vectype);
3086 icode = (int) optab_handler (optab, vec_mode);
3087 if (icode == CODE_FOR_nothing)
3088 return false;
3090 return true;
3094 /* Function vectorizable_shift.
3096 Check if STMT performs a shift operation that can be vectorized.
3097 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3098 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3099 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3101 static bool
3102 vectorizable_shift (gimple stmt, gimple_stmt_iterator *gsi,
3103 gimple *vec_stmt, slp_tree slp_node)
3105 tree vec_dest;
3106 tree scalar_dest;
3107 tree op0, op1 = NULL;
3108 tree vec_oprnd1 = NULL_TREE;
3109 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3110 tree vectype;
3111 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3112 enum tree_code code;
3113 enum machine_mode vec_mode;
3114 tree new_temp;
3115 optab optab;
3116 int icode;
3117 enum machine_mode optab_op2_mode;
3118 tree def;
3119 gimple def_stmt;
3120 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3121 gimple new_stmt = NULL;
3122 stmt_vec_info prev_stmt_info;
3123 int nunits_in;
3124 int nunits_out;
3125 tree vectype_out;
3126 tree op1_vectype;
3127 int ncopies;
3128 int j, i;
3129 vec<tree> vec_oprnds0 = vNULL;
3130 vec<tree> vec_oprnds1 = vNULL;
3131 tree vop0, vop1;
3132 unsigned int k;
3133 bool scalar_shift_arg = true;
3134 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3135 int vf;
3137 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3138 return false;
3140 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3141 return false;
3143 /* Is STMT a vectorizable binary/unary operation? */
3144 if (!is_gimple_assign (stmt))
3145 return false;
3147 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3148 return false;
3150 code = gimple_assign_rhs_code (stmt);
3152 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
3153 || code == RROTATE_EXPR))
3154 return false;
3156 scalar_dest = gimple_assign_lhs (stmt);
3157 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3158 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
3159 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
3161 if (dump_enabled_p ())
3162 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3163 "bit-precision shifts not supported.\n");
3164 return false;
3167 op0 = gimple_assign_rhs1 (stmt);
3168 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3169 &def_stmt, &def, &dt[0], &vectype))
3171 if (dump_enabled_p ())
3172 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3173 "use not simple.\n");
3174 return false;
3176 /* If op0 is an external or constant def use a vector type with
3177 the same size as the output vector type. */
3178 if (!vectype)
3179 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
3180 if (vec_stmt)
3181 gcc_assert (vectype);
3182 if (!vectype)
3184 if (dump_enabled_p ())
3185 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3186 "no vectype for scalar type\n");
3187 return false;
3190 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3191 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
3192 if (nunits_out != nunits_in)
3193 return false;
3195 op1 = gimple_assign_rhs2 (stmt);
3196 if (!vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3197 &def, &dt[1], &op1_vectype))
3199 if (dump_enabled_p ())
3200 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3201 "use not simple.\n");
3202 return false;
3205 if (loop_vinfo)
3206 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3207 else
3208 vf = 1;
3210 /* Multiple types in SLP are handled by creating the appropriate number of
3211 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3212 case of SLP. */
3213 if (slp_node || PURE_SLP_STMT (stmt_info))
3214 ncopies = 1;
3215 else
3216 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3218 gcc_assert (ncopies >= 1);
3220 /* Determine whether the shift amount is a vector, or scalar. If the
3221 shift/rotate amount is a vector, use the vector/vector shift optabs. */
3223 if (dt[1] == vect_internal_def && !slp_node)
3224 scalar_shift_arg = false;
3225 else if (dt[1] == vect_constant_def
3226 || dt[1] == vect_external_def
3227 || dt[1] == vect_internal_def)
3229 /* In SLP, need to check whether the shift count is the same,
3230 in loops if it is a constant or invariant, it is always
3231 a scalar shift. */
3232 if (slp_node)
3234 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
3235 gimple slpstmt;
3237 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
3238 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
3239 scalar_shift_arg = false;
3242 else
3244 if (dump_enabled_p ())
3245 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3246 "operand mode requires invariant argument.\n");
3247 return false;
3250 /* Vector shifted by vector. */
3251 if (!scalar_shift_arg)
3253 optab = optab_for_tree_code (code, vectype, optab_vector);
3254 if (dump_enabled_p ())
3255 dump_printf_loc (MSG_NOTE, vect_location,
3256 "vector/vector shift/rotate found.\n");
3258 if (!op1_vectype)
3259 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
3260 if (op1_vectype == NULL_TREE
3261 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
3263 if (dump_enabled_p ())
3264 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3265 "unusable type for last operand in"
3266 " vector/vector shift/rotate.\n");
3267 return false;
3270 /* See if the machine has a vector shifted by scalar insn and if not
3271 then see if it has a vector shifted by vector insn. */
3272 else
3274 optab = optab_for_tree_code (code, vectype, optab_scalar);
3275 if (optab
3276 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
3278 if (dump_enabled_p ())
3279 dump_printf_loc (MSG_NOTE, vect_location,
3280 "vector/scalar shift/rotate found.\n");
3282 else
3284 optab = optab_for_tree_code (code, vectype, optab_vector);
3285 if (optab
3286 && (optab_handler (optab, TYPE_MODE (vectype))
3287 != CODE_FOR_nothing))
3289 scalar_shift_arg = false;
3291 if (dump_enabled_p ())
3292 dump_printf_loc (MSG_NOTE, vect_location,
3293 "vector/vector shift/rotate found.\n");
3295 /* Unlike the other binary operators, shifts/rotates have
3296 the rhs being int, instead of the same type as the lhs,
3297 so make sure the scalar is the right type if we are
3298 dealing with vectors of long long/long/short/char. */
3299 if (dt[1] == vect_constant_def)
3300 op1 = fold_convert (TREE_TYPE (vectype), op1);
3301 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
3302 TREE_TYPE (op1)))
3304 if (slp_node
3305 && TYPE_MODE (TREE_TYPE (vectype))
3306 != TYPE_MODE (TREE_TYPE (op1)))
3308 if (dump_enabled_p ())
3309 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3310 "unusable type for last operand in"
3311 " vector/vector shift/rotate.\n");
3312 return false;
3314 if (vec_stmt && !slp_node)
3316 op1 = fold_convert (TREE_TYPE (vectype), op1);
3317 op1 = vect_init_vector (stmt, op1,
3318 TREE_TYPE (vectype), NULL);
3325 /* Supportable by target? */
3326 if (!optab)
3328 if (dump_enabled_p ())
3329 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3330 "no optab.\n");
3331 return false;
3333 vec_mode = TYPE_MODE (vectype);
3334 icode = (int) optab_handler (optab, vec_mode);
3335 if (icode == CODE_FOR_nothing)
3337 if (dump_enabled_p ())
3338 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3339 "op not supported by target.\n");
3340 /* Check only during analysis. */
3341 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
3342 || (vf < vect_min_worthwhile_factor (code)
3343 && !vec_stmt))
3344 return false;
3345 if (dump_enabled_p ())
3346 dump_printf_loc (MSG_NOTE, vect_location,
3347 "proceeding using word mode.\n");
3350 /* Worthwhile without SIMD support? Check only during analysis. */
3351 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
3352 && vf < vect_min_worthwhile_factor (code)
3353 && !vec_stmt)
3355 if (dump_enabled_p ())
3356 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3357 "not worthwhile without SIMD support.\n");
3358 return false;
3361 if (!vec_stmt) /* transformation not required. */
3363 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
3364 if (dump_enabled_p ())
3365 dump_printf_loc (MSG_NOTE, vect_location,
3366 "=== vectorizable_shift ===\n");
3367 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3368 return true;
3371 /** Transform. **/
3373 if (dump_enabled_p ())
3374 dump_printf_loc (MSG_NOTE, vect_location,
3375 "transform binary/unary operation.\n");
3377 /* Handle def. */
3378 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3380 prev_stmt_info = NULL;
3381 for (j = 0; j < ncopies; j++)
3383 /* Handle uses. */
3384 if (j == 0)
3386 if (scalar_shift_arg)
3388 /* Vector shl and shr insn patterns can be defined with scalar
3389 operand 2 (shift operand). In this case, use constant or loop
3390 invariant op1 directly, without extending it to vector mode
3391 first. */
3392 optab_op2_mode = insn_data[icode].operand[2].mode;
3393 if (!VECTOR_MODE_P (optab_op2_mode))
3395 if (dump_enabled_p ())
3396 dump_printf_loc (MSG_NOTE, vect_location,
3397 "operand 1 using scalar mode.\n");
3398 vec_oprnd1 = op1;
3399 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
3400 vec_oprnds1.quick_push (vec_oprnd1);
3401 if (slp_node)
3403 /* Store vec_oprnd1 for every vector stmt to be created
3404 for SLP_NODE. We check during the analysis that all
3405 the shift arguments are the same.
3406 TODO: Allow different constants for different vector
3407 stmts generated for an SLP instance. */
3408 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
3409 vec_oprnds1.quick_push (vec_oprnd1);
3414 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
3415 (a special case for certain kind of vector shifts); otherwise,
3416 operand 1 should be of a vector type (the usual case). */
3417 if (vec_oprnd1)
3418 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3419 slp_node, -1);
3420 else
3421 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
3422 slp_node, -1);
3424 else
3425 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
3427 /* Arguments are ready. Create the new vector stmt. */
3428 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3430 vop1 = vec_oprnds1[i];
3431 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
3432 new_temp = make_ssa_name (vec_dest, new_stmt);
3433 gimple_assign_set_lhs (new_stmt, new_temp);
3434 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3435 if (slp_node)
3436 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3439 if (slp_node)
3440 continue;
3442 if (j == 0)
3443 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3444 else
3445 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3446 prev_stmt_info = vinfo_for_stmt (new_stmt);
3449 vec_oprnds0.release ();
3450 vec_oprnds1.release ();
3452 return true;
3456 static tree permute_vec_elements (tree, tree, tree, gimple,
3457 gimple_stmt_iterator *);
3460 /* Function vectorizable_operation.
3462 Check if STMT performs a binary, unary or ternary operation that can
3463 be vectorized.
3464 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3465 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3466 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3468 static bool
3469 vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
3470 gimple *vec_stmt, slp_tree slp_node)
3472 tree vec_dest;
3473 tree scalar_dest;
3474 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
3475 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3476 tree vectype;
3477 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3478 enum tree_code code;
3479 enum machine_mode vec_mode;
3480 tree new_temp;
3481 int op_type;
3482 optab optab;
3483 int icode;
3484 tree def;
3485 gimple def_stmt;
3486 enum vect_def_type dt[3]
3487 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
3488 gimple new_stmt = NULL;
3489 stmt_vec_info prev_stmt_info;
3490 int nunits_in;
3491 int nunits_out;
3492 tree vectype_out;
3493 int ncopies;
3494 int j, i;
3495 vec<tree> vec_oprnds0 = vNULL;
3496 vec<tree> vec_oprnds1 = vNULL;
3497 vec<tree> vec_oprnds2 = vNULL;
3498 tree vop0, vop1, vop2;
3499 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3500 int vf;
3502 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3503 return false;
3505 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3506 return false;
3508 /* Is STMT a vectorizable binary/unary operation? */
3509 if (!is_gimple_assign (stmt))
3510 return false;
3512 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3513 return false;
3515 code = gimple_assign_rhs_code (stmt);
3517 /* For pointer addition, we should use the normal plus for
3518 the vector addition. */
3519 if (code == POINTER_PLUS_EXPR)
3520 code = PLUS_EXPR;
3522 /* Support only unary or binary operations. */
3523 op_type = TREE_CODE_LENGTH (code);
3524 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
3526 if (dump_enabled_p ())
3527 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3528 "num. args = %d (not unary/binary/ternary op).\n",
3529 op_type);
3530 return false;
3533 scalar_dest = gimple_assign_lhs (stmt);
3534 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3536 /* Most operations cannot handle bit-precision types without extra
3537 truncations. */
3538 if ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
3539 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
3540 /* Exception are bitwise binary operations. */
3541 && code != BIT_IOR_EXPR
3542 && code != BIT_XOR_EXPR
3543 && code != BIT_AND_EXPR)
3545 if (dump_enabled_p ())
3546 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3547 "bit-precision arithmetic not supported.\n");
3548 return false;
3551 op0 = gimple_assign_rhs1 (stmt);
3552 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3553 &def_stmt, &def, &dt[0], &vectype))
3555 if (dump_enabled_p ())
3556 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3557 "use not simple.\n");
3558 return false;
3560 /* If op0 is an external or constant def use a vector type with
3561 the same size as the output vector type. */
3562 if (!vectype)
3563 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
3564 if (vec_stmt)
3565 gcc_assert (vectype);
3566 if (!vectype)
3568 if (dump_enabled_p ())
3570 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3571 "no vectype for scalar type ");
3572 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3573 TREE_TYPE (op0));
3574 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3577 return false;
3580 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3581 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
3582 if (nunits_out != nunits_in)
3583 return false;
3585 if (op_type == binary_op || op_type == ternary_op)
3587 op1 = gimple_assign_rhs2 (stmt);
3588 if (!vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3589 &def, &dt[1]))
3591 if (dump_enabled_p ())
3592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3593 "use not simple.\n");
3594 return false;
3597 if (op_type == ternary_op)
3599 op2 = gimple_assign_rhs3 (stmt);
3600 if (!vect_is_simple_use (op2, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3601 &def, &dt[2]))
3603 if (dump_enabled_p ())
3604 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3605 "use not simple.\n");
3606 return false;
3610 if (loop_vinfo)
3611 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3612 else
3613 vf = 1;
3615 /* Multiple types in SLP are handled by creating the appropriate number of
3616 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3617 case of SLP. */
3618 if (slp_node || PURE_SLP_STMT (stmt_info))
3619 ncopies = 1;
3620 else
3621 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3623 gcc_assert (ncopies >= 1);
3625 /* Shifts are handled in vectorizable_shift (). */
3626 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
3627 || code == RROTATE_EXPR)
3628 return false;
3630 /* Supportable by target? */
3632 vec_mode = TYPE_MODE (vectype);
3633 if (code == MULT_HIGHPART_EXPR)
3635 if (can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype)))
3636 icode = LAST_INSN_CODE;
3637 else
3638 icode = CODE_FOR_nothing;
3640 else
3642 optab = optab_for_tree_code (code, vectype, optab_default);
3643 if (!optab)
3645 if (dump_enabled_p ())
3646 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3647 "no optab.\n");
3648 return false;
3650 icode = (int) optab_handler (optab, vec_mode);
3653 if (icode == CODE_FOR_nothing)
3655 if (dump_enabled_p ())
3656 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3657 "op not supported by target.\n");
3658 /* Check only during analysis. */
3659 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
3660 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
3661 return false;
3662 if (dump_enabled_p ())
3663 dump_printf_loc (MSG_NOTE, vect_location,
3664 "proceeding using word mode.\n");
3667 /* Worthwhile without SIMD support? Check only during analysis. */
3668 if (!VECTOR_MODE_P (vec_mode)
3669 && !vec_stmt
3670 && vf < vect_min_worthwhile_factor (code))
3672 if (dump_enabled_p ())
3673 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3674 "not worthwhile without SIMD support.\n");
3675 return false;
3678 if (!vec_stmt) /* transformation not required. */
3680 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
3681 if (dump_enabled_p ())
3682 dump_printf_loc (MSG_NOTE, vect_location,
3683 "=== vectorizable_operation ===\n");
3684 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3685 return true;
3688 /** Transform. **/
3690 if (dump_enabled_p ())
3691 dump_printf_loc (MSG_NOTE, vect_location,
3692 "transform binary/unary operation.\n");
3694 /* Handle def. */
3695 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3697 /* In case the vectorization factor (VF) is bigger than the number
3698 of elements that we can fit in a vectype (nunits), we have to generate
3699 more than one vector stmt - i.e - we need to "unroll" the
3700 vector stmt by a factor VF/nunits. In doing so, we record a pointer
3701 from one copy of the vector stmt to the next, in the field
3702 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
3703 stages to find the correct vector defs to be used when vectorizing
3704 stmts that use the defs of the current stmt. The example below
3705 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
3706 we need to create 4 vectorized stmts):
3708 before vectorization:
3709 RELATED_STMT VEC_STMT
3710 S1: x = memref - -
3711 S2: z = x + 1 - -
3713 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
3714 there):
3715 RELATED_STMT VEC_STMT
3716 VS1_0: vx0 = memref0 VS1_1 -
3717 VS1_1: vx1 = memref1 VS1_2 -
3718 VS1_2: vx2 = memref2 VS1_3 -
3719 VS1_3: vx3 = memref3 - -
3720 S1: x = load - VS1_0
3721 S2: z = x + 1 - -
3723 step2: vectorize stmt S2 (done here):
3724 To vectorize stmt S2 we first need to find the relevant vector
3725 def for the first operand 'x'. This is, as usual, obtained from
3726 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
3727 that defines 'x' (S1). This way we find the stmt VS1_0, and the
3728 relevant vector def 'vx0'. Having found 'vx0' we can generate
3729 the vector stmt VS2_0, and as usual, record it in the
3730 STMT_VINFO_VEC_STMT of stmt S2.
3731 When creating the second copy (VS2_1), we obtain the relevant vector
3732 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
3733 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
3734 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
3735 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
3736 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
3737 chain of stmts and pointers:
3738 RELATED_STMT VEC_STMT
3739 VS1_0: vx0 = memref0 VS1_1 -
3740 VS1_1: vx1 = memref1 VS1_2 -
3741 VS1_2: vx2 = memref2 VS1_3 -
3742 VS1_3: vx3 = memref3 - -
3743 S1: x = load - VS1_0
3744 VS2_0: vz0 = vx0 + v1 VS2_1 -
3745 VS2_1: vz1 = vx1 + v1 VS2_2 -
3746 VS2_2: vz2 = vx2 + v1 VS2_3 -
3747 VS2_3: vz3 = vx3 + v1 - -
3748 S2: z = x + 1 - VS2_0 */
3750 prev_stmt_info = NULL;
3751 for (j = 0; j < ncopies; j++)
3753 /* Handle uses. */
3754 if (j == 0)
3756 if (op_type == binary_op || op_type == ternary_op)
3757 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
3758 slp_node, -1);
3759 else
3760 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3761 slp_node, -1);
3762 if (op_type == ternary_op)
3764 vec_oprnds2.create (1);
3765 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
3766 stmt,
3767 NULL));
3770 else
3772 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
3773 if (op_type == ternary_op)
3775 tree vec_oprnd = vec_oprnds2.pop ();
3776 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
3777 vec_oprnd));
3781 /* Arguments are ready. Create the new vector stmt. */
3782 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3784 vop1 = ((op_type == binary_op || op_type == ternary_op)
3785 ? vec_oprnds1[i] : NULL_TREE);
3786 vop2 = ((op_type == ternary_op)
3787 ? vec_oprnds2[i] : NULL_TREE);
3788 new_stmt = gimple_build_assign_with_ops (code, vec_dest,
3789 vop0, vop1, vop2);
3790 new_temp = make_ssa_name (vec_dest, new_stmt);
3791 gimple_assign_set_lhs (new_stmt, new_temp);
3792 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3793 if (slp_node)
3794 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3797 if (slp_node)
3798 continue;
3800 if (j == 0)
3801 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3802 else
3803 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3804 prev_stmt_info = vinfo_for_stmt (new_stmt);
3807 vec_oprnds0.release ();
3808 vec_oprnds1.release ();
3809 vec_oprnds2.release ();
3811 return true;
3814 /* A helper function to ensure data reference DR's base alignment
3815 for STMT_INFO. */
3817 static void
3818 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
3820 if (!dr->aux)
3821 return;
3823 if (((dataref_aux *)dr->aux)->base_misaligned)
3825 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3826 tree base_decl = ((dataref_aux *)dr->aux)->base_decl;
3828 DECL_ALIGN (base_decl) = TYPE_ALIGN (vectype);
3829 DECL_USER_ALIGN (base_decl) = 1;
3830 ((dataref_aux *)dr->aux)->base_misaligned = false;
3835 /* Function vectorizable_store.
3837 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
3838 can be vectorized.
3839 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3840 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3841 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3843 static bool
3844 vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
3845 slp_tree slp_node)
3847 tree scalar_dest;
3848 tree data_ref;
3849 tree op;
3850 tree vec_oprnd = NULL_TREE;
3851 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3852 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
3853 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3854 tree elem_type;
3855 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3856 struct loop *loop = NULL;
3857 enum machine_mode vec_mode;
3858 tree dummy;
3859 enum dr_alignment_support alignment_support_scheme;
3860 tree def;
3861 gimple def_stmt;
3862 enum vect_def_type dt;
3863 stmt_vec_info prev_stmt_info = NULL;
3864 tree dataref_ptr = NULL_TREE;
3865 tree dataref_offset = NULL_TREE;
3866 gimple ptr_incr = NULL;
3867 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
3868 int ncopies;
3869 int j;
3870 gimple next_stmt, first_stmt = NULL;
3871 bool grouped_store = false;
3872 bool store_lanes_p = false;
3873 unsigned int group_size, i;
3874 vec<tree> dr_chain = vNULL;
3875 vec<tree> oprnds = vNULL;
3876 vec<tree> result_chain = vNULL;
3877 bool inv_p;
3878 vec<tree> vec_oprnds = vNULL;
3879 bool slp = (slp_node != NULL);
3880 unsigned int vec_num;
3881 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3882 tree aggr_type;
3884 if (loop_vinfo)
3885 loop = LOOP_VINFO_LOOP (loop_vinfo);
3887 /* Multiple types in SLP are handled by creating the appropriate number of
3888 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3889 case of SLP. */
3890 if (slp || PURE_SLP_STMT (stmt_info))
3891 ncopies = 1;
3892 else
3893 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3895 gcc_assert (ncopies >= 1);
3897 /* FORNOW. This restriction should be relaxed. */
3898 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
3900 if (dump_enabled_p ())
3901 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3902 "multiple types in nested loop.\n");
3903 return false;
3906 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3907 return false;
3909 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3910 return false;
3912 /* Is vectorizable store? */
3914 if (!is_gimple_assign (stmt))
3915 return false;
3917 scalar_dest = gimple_assign_lhs (stmt);
3918 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
3919 && is_pattern_stmt_p (stmt_info))
3920 scalar_dest = TREE_OPERAND (scalar_dest, 0);
3921 if (TREE_CODE (scalar_dest) != ARRAY_REF
3922 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
3923 && TREE_CODE (scalar_dest) != INDIRECT_REF
3924 && TREE_CODE (scalar_dest) != COMPONENT_REF
3925 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
3926 && TREE_CODE (scalar_dest) != REALPART_EXPR
3927 && TREE_CODE (scalar_dest) != MEM_REF)
3928 return false;
3930 gcc_assert (gimple_assign_single_p (stmt));
3931 op = gimple_assign_rhs1 (stmt);
3932 if (!vect_is_simple_use (op, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3933 &def, &dt))
3935 if (dump_enabled_p ())
3936 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3937 "use not simple.\n");
3938 return false;
3941 elem_type = TREE_TYPE (vectype);
3942 vec_mode = TYPE_MODE (vectype);
3944 /* FORNOW. In some cases can vectorize even if data-type not supported
3945 (e.g. - array initialization with 0). */
3946 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
3947 return false;
3949 if (!STMT_VINFO_DATA_REF (stmt_info))
3950 return false;
3952 if (tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
3953 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
3954 size_zero_node) < 0)
3956 if (dump_enabled_p ())
3957 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3958 "negative step for store.\n");
3959 return false;
3962 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
3964 grouped_store = true;
3965 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
3966 if (!slp && !PURE_SLP_STMT (stmt_info))
3968 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
3969 if (vect_store_lanes_supported (vectype, group_size))
3970 store_lanes_p = true;
3971 else if (!vect_grouped_store_supported (vectype, group_size))
3972 return false;
3975 if (first_stmt == stmt)
3977 /* STMT is the leader of the group. Check the operands of all the
3978 stmts of the group. */
3979 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
3980 while (next_stmt)
3982 gcc_assert (gimple_assign_single_p (next_stmt));
3983 op = gimple_assign_rhs1 (next_stmt);
3984 if (!vect_is_simple_use (op, next_stmt, loop_vinfo, bb_vinfo,
3985 &def_stmt, &def, &dt))
3987 if (dump_enabled_p ())
3988 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3989 "use not simple.\n");
3990 return false;
3992 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
3997 if (!vec_stmt) /* transformation not required. */
3999 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
4000 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
4001 NULL, NULL, NULL);
4002 return true;
4005 /** Transform. **/
4007 ensure_base_align (stmt_info, dr);
4009 if (grouped_store)
4011 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
4012 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4014 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
4016 /* FORNOW */
4017 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
4019 /* We vectorize all the stmts of the interleaving group when we
4020 reach the last stmt in the group. */
4021 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
4022 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
4023 && !slp)
4025 *vec_stmt = NULL;
4026 return true;
4029 if (slp)
4031 grouped_store = false;
4032 /* VEC_NUM is the number of vect stmts to be created for this
4033 group. */
4034 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
4035 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
4036 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
4037 op = gimple_assign_rhs1 (first_stmt);
4039 else
4040 /* VEC_NUM is the number of vect stmts to be created for this
4041 group. */
4042 vec_num = group_size;
4044 else
4046 first_stmt = stmt;
4047 first_dr = dr;
4048 group_size = vec_num = 1;
4051 if (dump_enabled_p ())
4052 dump_printf_loc (MSG_NOTE, vect_location,
4053 "transform store. ncopies = %d\n", ncopies);
4055 dr_chain.create (group_size);
4056 oprnds.create (group_size);
4058 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
4059 gcc_assert (alignment_support_scheme);
4060 /* Targets with store-lane instructions must not require explicit
4061 realignment. */
4062 gcc_assert (!store_lanes_p
4063 || alignment_support_scheme == dr_aligned
4064 || alignment_support_scheme == dr_unaligned_supported);
4066 if (store_lanes_p)
4067 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
4068 else
4069 aggr_type = vectype;
4071 /* In case the vectorization factor (VF) is bigger than the number
4072 of elements that we can fit in a vectype (nunits), we have to generate
4073 more than one vector stmt - i.e - we need to "unroll" the
4074 vector stmt by a factor VF/nunits. For more details see documentation in
4075 vect_get_vec_def_for_copy_stmt. */
4077 /* In case of interleaving (non-unit grouped access):
4079 S1: &base + 2 = x2
4080 S2: &base = x0
4081 S3: &base + 1 = x1
4082 S4: &base + 3 = x3
4084 We create vectorized stores starting from base address (the access of the
4085 first stmt in the chain (S2 in the above example), when the last store stmt
4086 of the chain (S4) is reached:
4088 VS1: &base = vx2
4089 VS2: &base + vec_size*1 = vx0
4090 VS3: &base + vec_size*2 = vx1
4091 VS4: &base + vec_size*3 = vx3
4093 Then permutation statements are generated:
4095 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
4096 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
4099 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
4100 (the order of the data-refs in the output of vect_permute_store_chain
4101 corresponds to the order of scalar stmts in the interleaving chain - see
4102 the documentation of vect_permute_store_chain()).
4104 In case of both multiple types and interleaving, above vector stores and
4105 permutation stmts are created for every copy. The result vector stmts are
4106 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
4107 STMT_VINFO_RELATED_STMT for the next copies.
4110 prev_stmt_info = NULL;
4111 for (j = 0; j < ncopies; j++)
4113 gimple new_stmt;
4115 if (j == 0)
4117 if (slp)
4119 /* Get vectorized arguments for SLP_NODE. */
4120 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
4121 NULL, slp_node, -1);
4123 vec_oprnd = vec_oprnds[0];
4125 else
4127 /* For interleaved stores we collect vectorized defs for all the
4128 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
4129 used as an input to vect_permute_store_chain(), and OPRNDS as
4130 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
4132 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
4133 OPRNDS are of size 1. */
4134 next_stmt = first_stmt;
4135 for (i = 0; i < group_size; i++)
4137 /* Since gaps are not supported for interleaved stores,
4138 GROUP_SIZE is the exact number of stmts in the chain.
4139 Therefore, NEXT_STMT can't be NULL_TREE. In case that
4140 there is no interleaving, GROUP_SIZE is 1, and only one
4141 iteration of the loop will be executed. */
4142 gcc_assert (next_stmt
4143 && gimple_assign_single_p (next_stmt));
4144 op = gimple_assign_rhs1 (next_stmt);
4146 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt,
4147 NULL);
4148 dr_chain.quick_push (vec_oprnd);
4149 oprnds.quick_push (vec_oprnd);
4150 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4154 /* We should have catched mismatched types earlier. */
4155 gcc_assert (useless_type_conversion_p (vectype,
4156 TREE_TYPE (vec_oprnd)));
4157 bool simd_lane_access_p
4158 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
4159 if (simd_lane_access_p
4160 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
4161 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
4162 && integer_zerop (DR_OFFSET (first_dr))
4163 && integer_zerop (DR_INIT (first_dr))
4164 && alias_sets_conflict_p (get_alias_set (aggr_type),
4165 get_alias_set (DR_REF (first_dr))))
4167 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
4168 dataref_offset = build_int_cst (reference_alias_ptr_type
4169 (DR_REF (first_dr)), 0);
4170 inv_p = false;
4172 else
4173 dataref_ptr
4174 = vect_create_data_ref_ptr (first_stmt, aggr_type,
4175 simd_lane_access_p ? loop : NULL,
4176 NULL_TREE, &dummy, gsi, &ptr_incr,
4177 simd_lane_access_p, &inv_p);
4178 gcc_assert (bb_vinfo || !inv_p);
4180 else
4182 /* For interleaved stores we created vectorized defs for all the
4183 defs stored in OPRNDS in the previous iteration (previous copy).
4184 DR_CHAIN is then used as an input to vect_permute_store_chain(),
4185 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
4186 next copy.
4187 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
4188 OPRNDS are of size 1. */
4189 for (i = 0; i < group_size; i++)
4191 op = oprnds[i];
4192 vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo, &def_stmt,
4193 &def, &dt);
4194 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
4195 dr_chain[i] = vec_oprnd;
4196 oprnds[i] = vec_oprnd;
4198 if (dataref_offset)
4199 dataref_offset
4200 = int_const_binop (PLUS_EXPR, dataref_offset,
4201 TYPE_SIZE_UNIT (aggr_type));
4202 else
4203 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
4204 TYPE_SIZE_UNIT (aggr_type));
4207 if (store_lanes_p)
4209 tree vec_array;
4211 /* Combine all the vectors into an array. */
4212 vec_array = create_vector_array (vectype, vec_num);
4213 for (i = 0; i < vec_num; i++)
4215 vec_oprnd = dr_chain[i];
4216 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
4219 /* Emit:
4220 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
4221 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
4222 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
4223 gimple_call_set_lhs (new_stmt, data_ref);
4224 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4226 else
4228 new_stmt = NULL;
4229 if (grouped_store)
4231 if (j == 0)
4232 result_chain.create (group_size);
4233 /* Permute. */
4234 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
4235 &result_chain);
4238 next_stmt = first_stmt;
4239 for (i = 0; i < vec_num; i++)
4241 unsigned align, misalign;
4243 if (i > 0)
4244 /* Bump the vector pointer. */
4245 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
4246 stmt, NULL_TREE);
4248 if (slp)
4249 vec_oprnd = vec_oprnds[i];
4250 else if (grouped_store)
4251 /* For grouped stores vectorized defs are interleaved in
4252 vect_permute_store_chain(). */
4253 vec_oprnd = result_chain[i];
4255 data_ref = build2 (MEM_REF, TREE_TYPE (vec_oprnd), dataref_ptr,
4256 dataref_offset
4257 ? dataref_offset
4258 : build_int_cst (reference_alias_ptr_type
4259 (DR_REF (first_dr)), 0));
4260 align = TYPE_ALIGN_UNIT (vectype);
4261 if (aligned_access_p (first_dr))
4262 misalign = 0;
4263 else if (DR_MISALIGNMENT (first_dr) == -1)
4265 TREE_TYPE (data_ref)
4266 = build_aligned_type (TREE_TYPE (data_ref),
4267 TYPE_ALIGN (elem_type));
4268 align = TYPE_ALIGN_UNIT (elem_type);
4269 misalign = 0;
4271 else
4273 TREE_TYPE (data_ref)
4274 = build_aligned_type (TREE_TYPE (data_ref),
4275 TYPE_ALIGN (elem_type));
4276 misalign = DR_MISALIGNMENT (first_dr);
4278 if (dataref_offset == NULL_TREE)
4279 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
4280 misalign);
4282 /* Arguments are ready. Create the new vector stmt. */
4283 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
4284 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4286 if (slp)
4287 continue;
4289 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4290 if (!next_stmt)
4291 break;
4294 if (!slp)
4296 if (j == 0)
4297 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4298 else
4299 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4300 prev_stmt_info = vinfo_for_stmt (new_stmt);
4304 dr_chain.release ();
4305 oprnds.release ();
4306 result_chain.release ();
4307 vec_oprnds.release ();
4309 return true;
4312 /* Given a vector type VECTYPE and permutation SEL returns
4313 the VECTOR_CST mask that implements the permutation of the
4314 vector elements. If that is impossible to do, returns NULL. */
4316 tree
4317 vect_gen_perm_mask (tree vectype, unsigned char *sel)
4319 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
4320 int i, nunits;
4322 nunits = TYPE_VECTOR_SUBPARTS (vectype);
4324 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
4325 return NULL;
4327 mask_elt_type = lang_hooks.types.type_for_mode
4328 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
4329 mask_type = get_vectype_for_scalar_type (mask_elt_type);
4331 mask_elts = XALLOCAVEC (tree, nunits);
4332 for (i = nunits - 1; i >= 0; i--)
4333 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
4334 mask_vec = build_vector (mask_type, mask_elts);
4336 return mask_vec;
4339 /* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
4340 reversal of the vector elements. If that is impossible to do,
4341 returns NULL. */
4343 static tree
4344 perm_mask_for_reverse (tree vectype)
4346 int i, nunits;
4347 unsigned char *sel;
4349 nunits = TYPE_VECTOR_SUBPARTS (vectype);
4350 sel = XALLOCAVEC (unsigned char, nunits);
4352 for (i = 0; i < nunits; ++i)
4353 sel[i] = nunits - 1 - i;
4355 return vect_gen_perm_mask (vectype, sel);
4358 /* Given a vector variable X and Y, that was generated for the scalar
4359 STMT, generate instructions to permute the vector elements of X and Y
4360 using permutation mask MASK_VEC, insert them at *GSI and return the
4361 permuted vector variable. */
4363 static tree
4364 permute_vec_elements (tree x, tree y, tree mask_vec, gimple stmt,
4365 gimple_stmt_iterator *gsi)
4367 tree vectype = TREE_TYPE (x);
4368 tree perm_dest, data_ref;
4369 gimple perm_stmt;
4371 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
4372 data_ref = make_ssa_name (perm_dest, NULL);
4374 /* Generate the permute statement. */
4375 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4376 x, y, mask_vec);
4377 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4379 return data_ref;
4382 /* vectorizable_load.
4384 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
4385 can be vectorized.
4386 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4387 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4388 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4390 static bool
4391 vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
4392 slp_tree slp_node, slp_instance slp_node_instance)
4394 tree scalar_dest;
4395 tree vec_dest = NULL;
4396 tree data_ref = NULL;
4397 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4398 stmt_vec_info prev_stmt_info;
4399 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4400 struct loop *loop = NULL;
4401 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4402 bool nested_in_vect_loop = false;
4403 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
4404 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4405 tree elem_type;
4406 tree new_temp;
4407 enum machine_mode mode;
4408 gimple new_stmt = NULL;
4409 tree dummy;
4410 enum dr_alignment_support alignment_support_scheme;
4411 tree dataref_ptr = NULL_TREE;
4412 tree dataref_offset = NULL_TREE;
4413 gimple ptr_incr = NULL;
4414 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4415 int ncopies;
4416 int i, j, group_size, group_gap;
4417 tree msq = NULL_TREE, lsq;
4418 tree offset = NULL_TREE;
4419 tree realignment_token = NULL_TREE;
4420 gimple phi = NULL;
4421 vec<tree> dr_chain = vNULL;
4422 bool grouped_load = false;
4423 bool load_lanes_p = false;
4424 gimple first_stmt;
4425 bool inv_p;
4426 bool negative = false;
4427 bool compute_in_loop = false;
4428 struct loop *at_loop;
4429 int vec_num;
4430 bool slp = (slp_node != NULL);
4431 bool slp_perm = false;
4432 enum tree_code code;
4433 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4434 int vf;
4435 tree aggr_type;
4436 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
4437 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
4438 int gather_scale = 1;
4439 enum vect_def_type gather_dt = vect_unknown_def_type;
4441 if (loop_vinfo)
4443 loop = LOOP_VINFO_LOOP (loop_vinfo);
4444 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4445 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4447 else
4448 vf = 1;
4450 /* Multiple types in SLP are handled by creating the appropriate number of
4451 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4452 case of SLP. */
4453 if (slp || PURE_SLP_STMT (stmt_info))
4454 ncopies = 1;
4455 else
4456 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4458 gcc_assert (ncopies >= 1);
4460 /* FORNOW. This restriction should be relaxed. */
4461 if (nested_in_vect_loop && ncopies > 1)
4463 if (dump_enabled_p ())
4464 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4465 "multiple types in nested loop.\n");
4466 return false;
4469 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4470 return false;
4472 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4473 return false;
4475 /* Is vectorizable load? */
4476 if (!is_gimple_assign (stmt))
4477 return false;
4479 scalar_dest = gimple_assign_lhs (stmt);
4480 if (TREE_CODE (scalar_dest) != SSA_NAME)
4481 return false;
4483 code = gimple_assign_rhs_code (stmt);
4484 if (code != ARRAY_REF
4485 && code != BIT_FIELD_REF
4486 && code != INDIRECT_REF
4487 && code != COMPONENT_REF
4488 && code != IMAGPART_EXPR
4489 && code != REALPART_EXPR
4490 && code != MEM_REF
4491 && TREE_CODE_CLASS (code) != tcc_declaration)
4492 return false;
4494 if (!STMT_VINFO_DATA_REF (stmt_info))
4495 return false;
4497 elem_type = TREE_TYPE (vectype);
4498 mode = TYPE_MODE (vectype);
4500 /* FORNOW. In some cases can vectorize even if data-type not supported
4501 (e.g. - data copies). */
4502 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
4504 if (dump_enabled_p ())
4505 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4506 "Aligned load, but unsupported type.\n");
4507 return false;
4510 /* Check if the load is a part of an interleaving chain. */
4511 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
4513 grouped_load = true;
4514 /* FORNOW */
4515 gcc_assert (! nested_in_vect_loop && !STMT_VINFO_GATHER_P (stmt_info));
4517 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
4518 if (!slp && !PURE_SLP_STMT (stmt_info))
4520 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4521 if (vect_load_lanes_supported (vectype, group_size))
4522 load_lanes_p = true;
4523 else if (!vect_grouped_load_supported (vectype, group_size))
4524 return false;
4529 if (STMT_VINFO_GATHER_P (stmt_info))
4531 gimple def_stmt;
4532 tree def;
4533 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
4534 &gather_off, &gather_scale);
4535 gcc_assert (gather_decl);
4536 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, bb_vinfo,
4537 &def_stmt, &def, &gather_dt,
4538 &gather_off_vectype))
4540 if (dump_enabled_p ())
4541 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4542 "gather index use not simple.\n");
4543 return false;
4546 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
4548 else
4550 negative = tree_int_cst_compare (nested_in_vect_loop
4551 ? STMT_VINFO_DR_STEP (stmt_info)
4552 : DR_STEP (dr),
4553 size_zero_node) < 0;
4554 if (negative && ncopies > 1)
4556 if (dump_enabled_p ())
4557 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4558 "multiple types with negative step.\n");
4559 return false;
4562 if (negative)
4564 if (grouped_load)
4566 if (dump_enabled_p ())
4567 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4568 "negative step for group load not supported"
4569 "\n");
4570 return false;
4572 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
4573 if (alignment_support_scheme != dr_aligned
4574 && alignment_support_scheme != dr_unaligned_supported)
4576 if (dump_enabled_p ())
4577 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4578 "negative step but alignment required.\n");
4579 return false;
4581 if (!perm_mask_for_reverse (vectype))
4583 if (dump_enabled_p ())
4584 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4585 "negative step and reversing not supported."
4586 "\n");
4587 return false;
4592 if (!vec_stmt) /* transformation not required. */
4594 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
4595 vect_model_load_cost (stmt_info, ncopies, load_lanes_p, NULL, NULL, NULL);
4596 return true;
4599 if (dump_enabled_p ())
4600 dump_printf_loc (MSG_NOTE, vect_location,
4601 "transform load. ncopies = %d\n", ncopies);
4603 /** Transform. **/
4605 ensure_base_align (stmt_info, dr);
4607 if (STMT_VINFO_GATHER_P (stmt_info))
4609 tree vec_oprnd0 = NULL_TREE, op;
4610 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
4611 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
4612 tree ptr, mask, var, scale, perm_mask = NULL_TREE, prev_res = NULL_TREE;
4613 edge pe = loop_preheader_edge (loop);
4614 gimple_seq seq;
4615 basic_block new_bb;
4616 enum { NARROW, NONE, WIDEN } modifier;
4617 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
4619 if (nunits == gather_off_nunits)
4620 modifier = NONE;
4621 else if (nunits == gather_off_nunits / 2)
4623 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
4624 modifier = WIDEN;
4626 for (i = 0; i < gather_off_nunits; ++i)
4627 sel[i] = i | nunits;
4629 perm_mask = vect_gen_perm_mask (gather_off_vectype, sel);
4630 gcc_assert (perm_mask != NULL_TREE);
4632 else if (nunits == gather_off_nunits * 2)
4634 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
4635 modifier = NARROW;
4637 for (i = 0; i < nunits; ++i)
4638 sel[i] = i < gather_off_nunits
4639 ? i : i + nunits - gather_off_nunits;
4641 perm_mask = vect_gen_perm_mask (vectype, sel);
4642 gcc_assert (perm_mask != NULL_TREE);
4643 ncopies *= 2;
4645 else
4646 gcc_unreachable ();
4648 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
4649 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
4650 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
4651 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
4652 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
4653 scaletype = TREE_VALUE (arglist);
4654 gcc_checking_assert (types_compatible_p (srctype, rettype)
4655 && types_compatible_p (srctype, masktype));
4657 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4659 ptr = fold_convert (ptrtype, gather_base);
4660 if (!is_gimple_min_invariant (ptr))
4662 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
4663 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
4664 gcc_assert (!new_bb);
4667 /* Currently we support only unconditional gather loads,
4668 so mask should be all ones. */
4669 if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
4670 mask = build_int_cst (TREE_TYPE (masktype), -1);
4671 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
4673 REAL_VALUE_TYPE r;
4674 long tmp[6];
4675 for (j = 0; j < 6; ++j)
4676 tmp[j] = -1;
4677 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
4678 mask = build_real (TREE_TYPE (masktype), r);
4680 else
4681 gcc_unreachable ();
4682 mask = build_vector_from_val (masktype, mask);
4683 mask = vect_init_vector (stmt, mask, masktype, NULL);
4685 scale = build_int_cst (scaletype, gather_scale);
4687 prev_stmt_info = NULL;
4688 for (j = 0; j < ncopies; ++j)
4690 if (modifier == WIDEN && (j & 1))
4691 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
4692 perm_mask, stmt, gsi);
4693 else if (j == 0)
4694 op = vec_oprnd0
4695 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
4696 else
4697 op = vec_oprnd0
4698 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
4700 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
4702 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
4703 == TYPE_VECTOR_SUBPARTS (idxtype));
4704 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
4705 var = make_ssa_name (var, NULL);
4706 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
4707 new_stmt
4708 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var,
4709 op, NULL_TREE);
4710 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4711 op = var;
4714 new_stmt
4715 = gimple_build_call (gather_decl, 5, mask, ptr, op, mask, scale);
4717 if (!useless_type_conversion_p (vectype, rettype))
4719 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
4720 == TYPE_VECTOR_SUBPARTS (rettype));
4721 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
4722 op = make_ssa_name (var, new_stmt);
4723 gimple_call_set_lhs (new_stmt, op);
4724 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4725 var = make_ssa_name (vec_dest, NULL);
4726 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
4727 new_stmt
4728 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var, op,
4729 NULL_TREE);
4731 else
4733 var = make_ssa_name (vec_dest, new_stmt);
4734 gimple_call_set_lhs (new_stmt, var);
4737 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4739 if (modifier == NARROW)
4741 if ((j & 1) == 0)
4743 prev_res = var;
4744 continue;
4746 var = permute_vec_elements (prev_res, var,
4747 perm_mask, stmt, gsi);
4748 new_stmt = SSA_NAME_DEF_STMT (var);
4751 if (prev_stmt_info == NULL)
4752 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4753 else
4754 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4755 prev_stmt_info = vinfo_for_stmt (new_stmt);
4757 return true;
4759 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
4761 gimple_stmt_iterator incr_gsi;
4762 bool insert_after;
4763 gimple incr;
4764 tree offvar;
4765 tree ivstep;
4766 tree running_off;
4767 vec<constructor_elt, va_gc> *v = NULL;
4768 gimple_seq stmts = NULL;
4769 tree stride_base, stride_step, alias_off;
4771 gcc_assert (!nested_in_vect_loop);
4773 stride_base
4774 = fold_build_pointer_plus
4775 (unshare_expr (DR_BASE_ADDRESS (dr)),
4776 size_binop (PLUS_EXPR,
4777 convert_to_ptrofftype (unshare_expr (DR_OFFSET (dr))),
4778 convert_to_ptrofftype (DR_INIT (dr))));
4779 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (dr)));
4781 /* For a load with loop-invariant (but other than power-of-2)
4782 stride (i.e. not a grouped access) like so:
4784 for (i = 0; i < n; i += stride)
4785 ... = array[i];
4787 we generate a new induction variable and new accesses to
4788 form a new vector (or vectors, depending on ncopies):
4790 for (j = 0; ; j += VF*stride)
4791 tmp1 = array[j];
4792 tmp2 = array[j + stride];
4794 vectemp = {tmp1, tmp2, ...}
4797 ivstep = stride_step;
4798 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
4799 build_int_cst (TREE_TYPE (ivstep), vf));
4801 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4803 create_iv (stride_base, ivstep, NULL,
4804 loop, &incr_gsi, insert_after,
4805 &offvar, NULL);
4806 incr = gsi_stmt (incr_gsi);
4807 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
4809 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
4810 if (stmts)
4811 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
4813 prev_stmt_info = NULL;
4814 running_off = offvar;
4815 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0);
4816 for (j = 0; j < ncopies; j++)
4818 tree vec_inv;
4820 vec_alloc (v, nunits);
4821 for (i = 0; i < nunits; i++)
4823 tree newref, newoff;
4824 gimple incr;
4825 newref = build2 (MEM_REF, TREE_TYPE (vectype),
4826 running_off, alias_off);
4828 newref = force_gimple_operand_gsi (gsi, newref, true,
4829 NULL_TREE, true,
4830 GSI_SAME_STMT);
4831 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
4832 newoff = copy_ssa_name (running_off, NULL);
4833 incr = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, newoff,
4834 running_off, stride_step);
4835 vect_finish_stmt_generation (stmt, incr, gsi);
4837 running_off = newoff;
4840 vec_inv = build_constructor (vectype, v);
4841 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
4842 new_stmt = SSA_NAME_DEF_STMT (new_temp);
4844 if (j == 0)
4845 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4846 else
4847 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4848 prev_stmt_info = vinfo_for_stmt (new_stmt);
4850 return true;
4853 if (grouped_load)
4855 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
4856 if (slp
4857 && !SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
4858 && first_stmt != SLP_TREE_SCALAR_STMTS (slp_node)[0])
4859 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
4861 /* Check if the chain of loads is already vectorized. */
4862 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
4863 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
4864 ??? But we can only do so if there is exactly one
4865 as we have no way to get at the rest. Leave the CSE
4866 opportunity alone.
4867 ??? With the group load eventually participating
4868 in multiple different permutations (having multiple
4869 slp nodes which refer to the same group) the CSE
4870 is even wrong code. See PR56270. */
4871 && !slp)
4873 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4874 return true;
4876 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
4877 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4879 /* VEC_NUM is the number of vect stmts to be created for this group. */
4880 if (slp)
4882 grouped_load = false;
4883 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
4884 if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
4885 slp_perm = true;
4886 group_gap = GROUP_GAP (vinfo_for_stmt (first_stmt));
4888 else
4890 vec_num = group_size;
4891 group_gap = 0;
4894 else
4896 first_stmt = stmt;
4897 first_dr = dr;
4898 group_size = vec_num = 1;
4899 group_gap = 0;
4902 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
4903 gcc_assert (alignment_support_scheme);
4904 /* Targets with load-lane instructions must not require explicit
4905 realignment. */
4906 gcc_assert (!load_lanes_p
4907 || alignment_support_scheme == dr_aligned
4908 || alignment_support_scheme == dr_unaligned_supported);
4910 /* In case the vectorization factor (VF) is bigger than the number
4911 of elements that we can fit in a vectype (nunits), we have to generate
4912 more than one vector stmt - i.e - we need to "unroll" the
4913 vector stmt by a factor VF/nunits. In doing so, we record a pointer
4914 from one copy of the vector stmt to the next, in the field
4915 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4916 stages to find the correct vector defs to be used when vectorizing
4917 stmts that use the defs of the current stmt. The example below
4918 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
4919 need to create 4 vectorized stmts):
4921 before vectorization:
4922 RELATED_STMT VEC_STMT
4923 S1: x = memref - -
4924 S2: z = x + 1 - -
4926 step 1: vectorize stmt S1:
4927 We first create the vector stmt VS1_0, and, as usual, record a
4928 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
4929 Next, we create the vector stmt VS1_1, and record a pointer to
4930 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
4931 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
4932 stmts and pointers:
4933 RELATED_STMT VEC_STMT
4934 VS1_0: vx0 = memref0 VS1_1 -
4935 VS1_1: vx1 = memref1 VS1_2 -
4936 VS1_2: vx2 = memref2 VS1_3 -
4937 VS1_3: vx3 = memref3 - -
4938 S1: x = load - VS1_0
4939 S2: z = x + 1 - -
4941 See in documentation in vect_get_vec_def_for_stmt_copy for how the
4942 information we recorded in RELATED_STMT field is used to vectorize
4943 stmt S2. */
4945 /* In case of interleaving (non-unit grouped access):
4947 S1: x2 = &base + 2
4948 S2: x0 = &base
4949 S3: x1 = &base + 1
4950 S4: x3 = &base + 3
4952 Vectorized loads are created in the order of memory accesses
4953 starting from the access of the first stmt of the chain:
4955 VS1: vx0 = &base
4956 VS2: vx1 = &base + vec_size*1
4957 VS3: vx3 = &base + vec_size*2
4958 VS4: vx4 = &base + vec_size*3
4960 Then permutation statements are generated:
4962 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
4963 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
4966 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
4967 (the order of the data-refs in the output of vect_permute_load_chain
4968 corresponds to the order of scalar stmts in the interleaving chain - see
4969 the documentation of vect_permute_load_chain()).
4970 The generation of permutation stmts and recording them in
4971 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
4973 In case of both multiple types and interleaving, the vector loads and
4974 permutation stmts above are created for every copy. The result vector
4975 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
4976 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
4978 /* If the data reference is aligned (dr_aligned) or potentially unaligned
4979 on a target that supports unaligned accesses (dr_unaligned_supported)
4980 we generate the following code:
4981 p = initial_addr;
4982 indx = 0;
4983 loop {
4984 p = p + indx * vectype_size;
4985 vec_dest = *(p);
4986 indx = indx + 1;
4989 Otherwise, the data reference is potentially unaligned on a target that
4990 does not support unaligned accesses (dr_explicit_realign_optimized) -
4991 then generate the following code, in which the data in each iteration is
4992 obtained by two vector loads, one from the previous iteration, and one
4993 from the current iteration:
4994 p1 = initial_addr;
4995 msq_init = *(floor(p1))
4996 p2 = initial_addr + VS - 1;
4997 realignment_token = call target_builtin;
4998 indx = 0;
4999 loop {
5000 p2 = p2 + indx * vectype_size
5001 lsq = *(floor(p2))
5002 vec_dest = realign_load (msq, lsq, realignment_token)
5003 indx = indx + 1;
5004 msq = lsq;
5005 } */
5007 /* If the misalignment remains the same throughout the execution of the
5008 loop, we can create the init_addr and permutation mask at the loop
5009 preheader. Otherwise, it needs to be created inside the loop.
5010 This can only occur when vectorizing memory accesses in the inner-loop
5011 nested within an outer-loop that is being vectorized. */
5013 if (nested_in_vect_loop
5014 && (TREE_INT_CST_LOW (DR_STEP (dr))
5015 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
5017 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
5018 compute_in_loop = true;
5021 if ((alignment_support_scheme == dr_explicit_realign_optimized
5022 || alignment_support_scheme == dr_explicit_realign)
5023 && !compute_in_loop)
5025 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
5026 alignment_support_scheme, NULL_TREE,
5027 &at_loop);
5028 if (alignment_support_scheme == dr_explicit_realign_optimized)
5030 phi = SSA_NAME_DEF_STMT (msq);
5031 offset = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
5034 else
5035 at_loop = loop;
5037 if (negative)
5038 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
5040 if (load_lanes_p)
5041 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5042 else
5043 aggr_type = vectype;
5045 prev_stmt_info = NULL;
5046 for (j = 0; j < ncopies; j++)
5048 /* 1. Create the vector or array pointer update chain. */
5049 if (j == 0)
5051 bool simd_lane_access_p
5052 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5053 if (simd_lane_access_p
5054 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5055 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5056 && integer_zerop (DR_OFFSET (first_dr))
5057 && integer_zerop (DR_INIT (first_dr))
5058 && alias_sets_conflict_p (get_alias_set (aggr_type),
5059 get_alias_set (DR_REF (first_dr)))
5060 && (alignment_support_scheme == dr_aligned
5061 || alignment_support_scheme == dr_unaligned_supported))
5063 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5064 dataref_offset = build_int_cst (reference_alias_ptr_type
5065 (DR_REF (first_dr)), 0);
5066 inv_p = false;
5068 else
5069 dataref_ptr
5070 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
5071 offset, &dummy, gsi, &ptr_incr,
5072 simd_lane_access_p, &inv_p);
5074 else if (dataref_offset)
5075 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
5076 TYPE_SIZE_UNIT (aggr_type));
5077 else
5078 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5079 TYPE_SIZE_UNIT (aggr_type));
5081 if (grouped_load || slp_perm)
5082 dr_chain.create (vec_num);
5084 if (load_lanes_p)
5086 tree vec_array;
5088 vec_array = create_vector_array (vectype, vec_num);
5090 /* Emit:
5091 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
5092 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5093 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
5094 gimple_call_set_lhs (new_stmt, vec_array);
5095 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5097 /* Extract each vector into an SSA_NAME. */
5098 for (i = 0; i < vec_num; i++)
5100 new_temp = read_vector_array (stmt, gsi, scalar_dest,
5101 vec_array, i);
5102 dr_chain.quick_push (new_temp);
5105 /* Record the mapping between SSA_NAMEs and statements. */
5106 vect_record_grouped_load_vectors (stmt, dr_chain);
5108 else
5110 for (i = 0; i < vec_num; i++)
5112 if (i > 0)
5113 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5114 stmt, NULL_TREE);
5116 /* 2. Create the vector-load in the loop. */
5117 switch (alignment_support_scheme)
5119 case dr_aligned:
5120 case dr_unaligned_supported:
5122 unsigned int align, misalign;
5124 data_ref
5125 = build2 (MEM_REF, vectype, dataref_ptr,
5126 dataref_offset
5127 ? dataref_offset
5128 : build_int_cst (reference_alias_ptr_type
5129 (DR_REF (first_dr)), 0));
5130 align = TYPE_ALIGN_UNIT (vectype);
5131 if (alignment_support_scheme == dr_aligned)
5133 gcc_assert (aligned_access_p (first_dr));
5134 misalign = 0;
5136 else if (DR_MISALIGNMENT (first_dr) == -1)
5138 TREE_TYPE (data_ref)
5139 = build_aligned_type (TREE_TYPE (data_ref),
5140 TYPE_ALIGN (elem_type));
5141 align = TYPE_ALIGN_UNIT (elem_type);
5142 misalign = 0;
5144 else
5146 TREE_TYPE (data_ref)
5147 = build_aligned_type (TREE_TYPE (data_ref),
5148 TYPE_ALIGN (elem_type));
5149 misalign = DR_MISALIGNMENT (first_dr);
5151 if (dataref_offset == NULL_TREE)
5152 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
5153 align, misalign);
5154 break;
5156 case dr_explicit_realign:
5158 tree ptr, bump;
5159 tree vs_minus_1;
5161 vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
5163 if (compute_in_loop)
5164 msq = vect_setup_realignment (first_stmt, gsi,
5165 &realignment_token,
5166 dr_explicit_realign,
5167 dataref_ptr, NULL);
5169 ptr = copy_ssa_name (dataref_ptr, NULL);
5170 new_stmt = gimple_build_assign_with_ops
5171 (BIT_AND_EXPR, ptr, dataref_ptr,
5172 build_int_cst
5173 (TREE_TYPE (dataref_ptr),
5174 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5175 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5176 data_ref
5177 = build2 (MEM_REF, vectype, ptr,
5178 build_int_cst (reference_alias_ptr_type
5179 (DR_REF (first_dr)), 0));
5180 vec_dest = vect_create_destination_var (scalar_dest,
5181 vectype);
5182 new_stmt = gimple_build_assign (vec_dest, data_ref);
5183 new_temp = make_ssa_name (vec_dest, new_stmt);
5184 gimple_assign_set_lhs (new_stmt, new_temp);
5185 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
5186 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
5187 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5188 msq = new_temp;
5190 bump = size_binop (MULT_EXPR, vs_minus_1,
5191 TYPE_SIZE_UNIT (elem_type));
5192 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
5193 new_stmt = gimple_build_assign_with_ops
5194 (BIT_AND_EXPR, NULL_TREE, ptr,
5195 build_int_cst
5196 (TREE_TYPE (ptr),
5197 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5198 ptr = copy_ssa_name (dataref_ptr, new_stmt);
5199 gimple_assign_set_lhs (new_stmt, ptr);
5200 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5201 data_ref
5202 = build2 (MEM_REF, vectype, ptr,
5203 build_int_cst (reference_alias_ptr_type
5204 (DR_REF (first_dr)), 0));
5205 break;
5207 case dr_explicit_realign_optimized:
5208 new_temp = copy_ssa_name (dataref_ptr, NULL);
5209 new_stmt = gimple_build_assign_with_ops
5210 (BIT_AND_EXPR, new_temp, dataref_ptr,
5211 build_int_cst
5212 (TREE_TYPE (dataref_ptr),
5213 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5214 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5215 data_ref
5216 = build2 (MEM_REF, vectype, new_temp,
5217 build_int_cst (reference_alias_ptr_type
5218 (DR_REF (first_dr)), 0));
5219 break;
5220 default:
5221 gcc_unreachable ();
5223 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5224 new_stmt = gimple_build_assign (vec_dest, data_ref);
5225 new_temp = make_ssa_name (vec_dest, new_stmt);
5226 gimple_assign_set_lhs (new_stmt, new_temp);
5227 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5229 /* 3. Handle explicit realignment if necessary/supported.
5230 Create in loop:
5231 vec_dest = realign_load (msq, lsq, realignment_token) */
5232 if (alignment_support_scheme == dr_explicit_realign_optimized
5233 || alignment_support_scheme == dr_explicit_realign)
5235 lsq = gimple_assign_lhs (new_stmt);
5236 if (!realignment_token)
5237 realignment_token = dataref_ptr;
5238 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5239 new_stmt
5240 = gimple_build_assign_with_ops (REALIGN_LOAD_EXPR,
5241 vec_dest, msq, lsq,
5242 realignment_token);
5243 new_temp = make_ssa_name (vec_dest, new_stmt);
5244 gimple_assign_set_lhs (new_stmt, new_temp);
5245 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5247 if (alignment_support_scheme == dr_explicit_realign_optimized)
5249 gcc_assert (phi);
5250 if (i == vec_num - 1 && j == ncopies - 1)
5251 add_phi_arg (phi, lsq,
5252 loop_latch_edge (containing_loop),
5253 UNKNOWN_LOCATION);
5254 msq = lsq;
5258 /* 4. Handle invariant-load. */
5259 if (inv_p && !bb_vinfo)
5261 gimple_stmt_iterator gsi2 = *gsi;
5262 gcc_assert (!grouped_load);
5263 gsi_next (&gsi2);
5264 new_temp = vect_init_vector (stmt, scalar_dest,
5265 vectype, &gsi2);
5266 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5269 if (negative)
5271 tree perm_mask = perm_mask_for_reverse (vectype);
5272 new_temp = permute_vec_elements (new_temp, new_temp,
5273 perm_mask, stmt, gsi);
5274 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5277 /* Collect vector loads and later create their permutation in
5278 vect_transform_grouped_load (). */
5279 if (grouped_load || slp_perm)
5280 dr_chain.quick_push (new_temp);
5282 /* Store vector loads in the corresponding SLP_NODE. */
5283 if (slp && !slp_perm)
5284 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5286 /* Bump the vector pointer to account for a gap. */
5287 if (slp && group_gap != 0)
5289 tree bump = size_binop (MULT_EXPR,
5290 TYPE_SIZE_UNIT (elem_type),
5291 size_int (group_gap));
5292 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5293 stmt, bump);
5297 if (slp && !slp_perm)
5298 continue;
5300 if (slp_perm)
5302 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
5303 slp_node_instance, false))
5305 dr_chain.release ();
5306 return false;
5309 else
5311 if (grouped_load)
5313 if (!load_lanes_p)
5314 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
5315 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
5317 else
5319 if (j == 0)
5320 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5321 else
5322 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5323 prev_stmt_info = vinfo_for_stmt (new_stmt);
5326 dr_chain.release ();
5329 return true;
5332 /* Function vect_is_simple_cond.
5334 Input:
5335 LOOP - the loop that is being vectorized.
5336 COND - Condition that is checked for simple use.
5338 Output:
5339 *COMP_VECTYPE - the vector type for the comparison.
5341 Returns whether a COND can be vectorized. Checks whether
5342 condition operands are supportable using vec_is_simple_use. */
5344 static bool
5345 vect_is_simple_cond (tree cond, gimple stmt, loop_vec_info loop_vinfo,
5346 bb_vec_info bb_vinfo, tree *comp_vectype)
5348 tree lhs, rhs;
5349 tree def;
5350 enum vect_def_type dt;
5351 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
5353 if (!COMPARISON_CLASS_P (cond))
5354 return false;
5356 lhs = TREE_OPERAND (cond, 0);
5357 rhs = TREE_OPERAND (cond, 1);
5359 if (TREE_CODE (lhs) == SSA_NAME)
5361 gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
5362 if (!vect_is_simple_use_1 (lhs, stmt, loop_vinfo, bb_vinfo,
5363 &lhs_def_stmt, &def, &dt, &vectype1))
5364 return false;
5366 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
5367 && TREE_CODE (lhs) != FIXED_CST)
5368 return false;
5370 if (TREE_CODE (rhs) == SSA_NAME)
5372 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
5373 if (!vect_is_simple_use_1 (rhs, stmt, loop_vinfo, bb_vinfo,
5374 &rhs_def_stmt, &def, &dt, &vectype2))
5375 return false;
5377 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
5378 && TREE_CODE (rhs) != FIXED_CST)
5379 return false;
5381 *comp_vectype = vectype1 ? vectype1 : vectype2;
5382 return true;
5385 /* vectorizable_condition.
5387 Check if STMT is conditional modify expression that can be vectorized.
5388 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5389 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
5390 at GSI.
5392 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
5393 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
5394 else caluse if it is 2).
5396 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5398 bool
5399 vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
5400 gimple *vec_stmt, tree reduc_def, int reduc_index,
5401 slp_tree slp_node)
5403 tree scalar_dest = NULL_TREE;
5404 tree vec_dest = NULL_TREE;
5405 tree cond_expr, then_clause, else_clause;
5406 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5407 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5408 tree comp_vectype = NULL_TREE;
5409 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
5410 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
5411 tree vec_compare, vec_cond_expr;
5412 tree new_temp;
5413 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5414 tree def;
5415 enum vect_def_type dt, dts[4];
5416 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5417 int ncopies;
5418 enum tree_code code;
5419 stmt_vec_info prev_stmt_info = NULL;
5420 int i, j;
5421 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5422 vec<tree> vec_oprnds0 = vNULL;
5423 vec<tree> vec_oprnds1 = vNULL;
5424 vec<tree> vec_oprnds2 = vNULL;
5425 vec<tree> vec_oprnds3 = vNULL;
5426 tree vec_cmp_type;
5428 if (slp_node || PURE_SLP_STMT (stmt_info))
5429 ncopies = 1;
5430 else
5431 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5433 gcc_assert (ncopies >= 1);
5434 if (reduc_index && ncopies > 1)
5435 return false; /* FORNOW */
5437 if (reduc_index && STMT_SLP_TYPE (stmt_info))
5438 return false;
5440 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5441 return false;
5443 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5444 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
5445 && reduc_def))
5446 return false;
5448 /* FORNOW: not yet supported. */
5449 if (STMT_VINFO_LIVE_P (stmt_info))
5451 if (dump_enabled_p ())
5452 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5453 "value used after loop.\n");
5454 return false;
5457 /* Is vectorizable conditional operation? */
5458 if (!is_gimple_assign (stmt))
5459 return false;
5461 code = gimple_assign_rhs_code (stmt);
5463 if (code != COND_EXPR)
5464 return false;
5466 cond_expr = gimple_assign_rhs1 (stmt);
5467 then_clause = gimple_assign_rhs2 (stmt);
5468 else_clause = gimple_assign_rhs3 (stmt);
5470 if (!vect_is_simple_cond (cond_expr, stmt, loop_vinfo, bb_vinfo,
5471 &comp_vectype)
5472 || !comp_vectype)
5473 return false;
5475 if (TREE_CODE (then_clause) == SSA_NAME)
5477 gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
5478 if (!vect_is_simple_use (then_clause, stmt, loop_vinfo, bb_vinfo,
5479 &then_def_stmt, &def, &dt))
5480 return false;
5482 else if (TREE_CODE (then_clause) != INTEGER_CST
5483 && TREE_CODE (then_clause) != REAL_CST
5484 && TREE_CODE (then_clause) != FIXED_CST)
5485 return false;
5487 if (TREE_CODE (else_clause) == SSA_NAME)
5489 gimple else_def_stmt = SSA_NAME_DEF_STMT (else_clause);
5490 if (!vect_is_simple_use (else_clause, stmt, loop_vinfo, bb_vinfo,
5491 &else_def_stmt, &def, &dt))
5492 return false;
5494 else if (TREE_CODE (else_clause) != INTEGER_CST
5495 && TREE_CODE (else_clause) != REAL_CST
5496 && TREE_CODE (else_clause) != FIXED_CST)
5497 return false;
5499 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype)));
5500 /* The result of a vector comparison should be signed type. */
5501 tree cmp_type = build_nonstandard_integer_type (prec, 0);
5502 vec_cmp_type = get_same_sized_vectype (cmp_type, vectype);
5503 if (vec_cmp_type == NULL_TREE)
5504 return false;
5506 if (!vec_stmt)
5508 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
5509 return expand_vec_cond_expr_p (vectype, comp_vectype);
5512 /* Transform. */
5514 if (!slp_node)
5516 vec_oprnds0.create (1);
5517 vec_oprnds1.create (1);
5518 vec_oprnds2.create (1);
5519 vec_oprnds3.create (1);
5522 /* Handle def. */
5523 scalar_dest = gimple_assign_lhs (stmt);
5524 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5526 /* Handle cond expr. */
5527 for (j = 0; j < ncopies; j++)
5529 gimple new_stmt = NULL;
5530 if (j == 0)
5532 if (slp_node)
5534 stack_vec<tree, 4> ops;
5535 stack_vec<vec<tree>, 4> vec_defs;
5537 ops.safe_push (TREE_OPERAND (cond_expr, 0));
5538 ops.safe_push (TREE_OPERAND (cond_expr, 1));
5539 ops.safe_push (then_clause);
5540 ops.safe_push (else_clause);
5541 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
5542 vec_oprnds3 = vec_defs.pop ();
5543 vec_oprnds2 = vec_defs.pop ();
5544 vec_oprnds1 = vec_defs.pop ();
5545 vec_oprnds0 = vec_defs.pop ();
5547 ops.release ();
5548 vec_defs.release ();
5550 else
5552 gimple gtemp;
5553 vec_cond_lhs =
5554 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
5555 stmt, NULL);
5556 vect_is_simple_use (TREE_OPERAND (cond_expr, 0), stmt,
5557 loop_vinfo, NULL, &gtemp, &def, &dts[0]);
5559 vec_cond_rhs =
5560 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
5561 stmt, NULL);
5562 vect_is_simple_use (TREE_OPERAND (cond_expr, 1), stmt,
5563 loop_vinfo, NULL, &gtemp, &def, &dts[1]);
5564 if (reduc_index == 1)
5565 vec_then_clause = reduc_def;
5566 else
5568 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
5569 stmt, NULL);
5570 vect_is_simple_use (then_clause, stmt, loop_vinfo,
5571 NULL, &gtemp, &def, &dts[2]);
5573 if (reduc_index == 2)
5574 vec_else_clause = reduc_def;
5575 else
5577 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
5578 stmt, NULL);
5579 vect_is_simple_use (else_clause, stmt, loop_vinfo,
5580 NULL, &gtemp, &def, &dts[3]);
5584 else
5586 vec_cond_lhs = vect_get_vec_def_for_stmt_copy (dts[0],
5587 vec_oprnds0.pop ());
5588 vec_cond_rhs = vect_get_vec_def_for_stmt_copy (dts[1],
5589 vec_oprnds1.pop ());
5590 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
5591 vec_oprnds2.pop ());
5592 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
5593 vec_oprnds3.pop ());
5596 if (!slp_node)
5598 vec_oprnds0.quick_push (vec_cond_lhs);
5599 vec_oprnds1.quick_push (vec_cond_rhs);
5600 vec_oprnds2.quick_push (vec_then_clause);
5601 vec_oprnds3.quick_push (vec_else_clause);
5604 /* Arguments are ready. Create the new vector stmt. */
5605 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
5607 vec_cond_rhs = vec_oprnds1[i];
5608 vec_then_clause = vec_oprnds2[i];
5609 vec_else_clause = vec_oprnds3[i];
5611 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
5612 vec_cond_lhs, vec_cond_rhs);
5613 vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
5614 vec_compare, vec_then_clause, vec_else_clause);
5616 new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
5617 new_temp = make_ssa_name (vec_dest, new_stmt);
5618 gimple_assign_set_lhs (new_stmt, new_temp);
5619 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5620 if (slp_node)
5621 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5624 if (slp_node)
5625 continue;
5627 if (j == 0)
5628 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5629 else
5630 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5632 prev_stmt_info = vinfo_for_stmt (new_stmt);
5635 vec_oprnds0.release ();
5636 vec_oprnds1.release ();
5637 vec_oprnds2.release ();
5638 vec_oprnds3.release ();
5640 return true;
5644 /* Make sure the statement is vectorizable. */
5646 bool
5647 vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
5649 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5650 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5651 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
5652 bool ok;
5653 tree scalar_type, vectype;
5654 gimple pattern_stmt;
5655 gimple_seq pattern_def_seq;
5657 if (dump_enabled_p ())
5659 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
5660 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
5661 dump_printf (MSG_NOTE, "\n");
5664 if (gimple_has_volatile_ops (stmt))
5666 if (dump_enabled_p ())
5667 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5668 "not vectorized: stmt has volatile operands\n");
5670 return false;
5673 /* Skip stmts that do not need to be vectorized. In loops this is expected
5674 to include:
5675 - the COND_EXPR which is the loop exit condition
5676 - any LABEL_EXPRs in the loop
5677 - computations that are used only for array indexing or loop control.
5678 In basic blocks we only analyze statements that are a part of some SLP
5679 instance, therefore, all the statements are relevant.
5681 Pattern statement needs to be analyzed instead of the original statement
5682 if the original statement is not relevant. Otherwise, we analyze both
5683 statements. In basic blocks we are called from some SLP instance
5684 traversal, don't analyze pattern stmts instead, the pattern stmts
5685 already will be part of SLP instance. */
5687 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
5688 if (!STMT_VINFO_RELEVANT_P (stmt_info)
5689 && !STMT_VINFO_LIVE_P (stmt_info))
5691 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
5692 && pattern_stmt
5693 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
5694 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
5696 /* Analyze PATTERN_STMT instead of the original stmt. */
5697 stmt = pattern_stmt;
5698 stmt_info = vinfo_for_stmt (pattern_stmt);
5699 if (dump_enabled_p ())
5701 dump_printf_loc (MSG_NOTE, vect_location,
5702 "==> examining pattern statement: ");
5703 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
5704 dump_printf (MSG_NOTE, "\n");
5707 else
5709 if (dump_enabled_p ())
5710 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
5712 return true;
5715 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
5716 && node == NULL
5717 && pattern_stmt
5718 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
5719 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
5721 /* Analyze PATTERN_STMT too. */
5722 if (dump_enabled_p ())
5724 dump_printf_loc (MSG_NOTE, vect_location,
5725 "==> examining pattern statement: ");
5726 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
5727 dump_printf (MSG_NOTE, "\n");
5730 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
5731 return false;
5734 if (is_pattern_stmt_p (stmt_info)
5735 && node == NULL
5736 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
5738 gimple_stmt_iterator si;
5740 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
5742 gimple pattern_def_stmt = gsi_stmt (si);
5743 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
5744 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
5746 /* Analyze def stmt of STMT if it's a pattern stmt. */
5747 if (dump_enabled_p ())
5749 dump_printf_loc (MSG_NOTE, vect_location,
5750 "==> examining pattern def statement: ");
5751 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
5752 dump_printf (MSG_NOTE, "\n");
5755 if (!vect_analyze_stmt (pattern_def_stmt,
5756 need_to_vectorize, node))
5757 return false;
5762 switch (STMT_VINFO_DEF_TYPE (stmt_info))
5764 case vect_internal_def:
5765 break;
5767 case vect_reduction_def:
5768 case vect_nested_cycle:
5769 gcc_assert (!bb_vinfo && (relevance == vect_used_in_outer
5770 || relevance == vect_used_in_outer_by_reduction
5771 || relevance == vect_unused_in_scope));
5772 break;
5774 case vect_induction_def:
5775 case vect_constant_def:
5776 case vect_external_def:
5777 case vect_unknown_def_type:
5778 default:
5779 gcc_unreachable ();
5782 if (bb_vinfo)
5784 gcc_assert (PURE_SLP_STMT (stmt_info));
5786 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
5787 if (dump_enabled_p ())
5789 dump_printf_loc (MSG_NOTE, vect_location,
5790 "get vectype for scalar type: ");
5791 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
5792 dump_printf (MSG_NOTE, "\n");
5795 vectype = get_vectype_for_scalar_type (scalar_type);
5796 if (!vectype)
5798 if (dump_enabled_p ())
5800 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5801 "not SLPed: unsupported data-type ");
5802 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
5803 scalar_type);
5804 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5806 return false;
5809 if (dump_enabled_p ())
5811 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
5812 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
5813 dump_printf (MSG_NOTE, "\n");
5816 STMT_VINFO_VECTYPE (stmt_info) = vectype;
5819 if (STMT_VINFO_RELEVANT_P (stmt_info))
5821 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
5822 gcc_assert (STMT_VINFO_VECTYPE (stmt_info));
5823 *need_to_vectorize = true;
5826 ok = true;
5827 if (!bb_vinfo
5828 && (STMT_VINFO_RELEVANT_P (stmt_info)
5829 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
5830 ok = (vectorizable_conversion (stmt, NULL, NULL, NULL)
5831 || vectorizable_shift (stmt, NULL, NULL, NULL)
5832 || vectorizable_operation (stmt, NULL, NULL, NULL)
5833 || vectorizable_assignment (stmt, NULL, NULL, NULL)
5834 || vectorizable_load (stmt, NULL, NULL, NULL, NULL)
5835 || vectorizable_call (stmt, NULL, NULL, NULL)
5836 || vectorizable_store (stmt, NULL, NULL, NULL)
5837 || vectorizable_reduction (stmt, NULL, NULL, NULL)
5838 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, NULL));
5839 else
5841 if (bb_vinfo)
5842 ok = (vectorizable_conversion (stmt, NULL, NULL, node)
5843 || vectorizable_shift (stmt, NULL, NULL, node)
5844 || vectorizable_operation (stmt, NULL, NULL, node)
5845 || vectorizable_assignment (stmt, NULL, NULL, node)
5846 || vectorizable_load (stmt, NULL, NULL, node, NULL)
5847 || vectorizable_call (stmt, NULL, NULL, node)
5848 || vectorizable_store (stmt, NULL, NULL, node)
5849 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node));
5852 if (!ok)
5854 if (dump_enabled_p ())
5856 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5857 "not vectorized: relevant stmt not ");
5858 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
5859 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
5860 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5863 return false;
5866 if (bb_vinfo)
5867 return true;
5869 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
5870 need extra handling, except for vectorizable reductions. */
5871 if (STMT_VINFO_LIVE_P (stmt_info)
5872 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
5873 ok = vectorizable_live_operation (stmt, NULL, NULL);
5875 if (!ok)
5877 if (dump_enabled_p ())
5879 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5880 "not vectorized: live stmt not ");
5881 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
5882 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
5883 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
5886 return false;
5889 return true;
5893 /* Function vect_transform_stmt.
5895 Create a vectorized stmt to replace STMT, and insert it at BSI. */
5897 bool
5898 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
5899 bool *grouped_store, slp_tree slp_node,
5900 slp_instance slp_node_instance)
5902 bool is_store = false;
5903 gimple vec_stmt = NULL;
5904 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5905 bool done;
5907 switch (STMT_VINFO_TYPE (stmt_info))
5909 case type_demotion_vec_info_type:
5910 case type_promotion_vec_info_type:
5911 case type_conversion_vec_info_type:
5912 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
5913 gcc_assert (done);
5914 break;
5916 case induc_vec_info_type:
5917 gcc_assert (!slp_node);
5918 done = vectorizable_induction (stmt, gsi, &vec_stmt);
5919 gcc_assert (done);
5920 break;
5922 case shift_vec_info_type:
5923 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
5924 gcc_assert (done);
5925 break;
5927 case op_vec_info_type:
5928 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
5929 gcc_assert (done);
5930 break;
5932 case assignment_vec_info_type:
5933 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
5934 gcc_assert (done);
5935 break;
5937 case load_vec_info_type:
5938 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
5939 slp_node_instance);
5940 gcc_assert (done);
5941 break;
5943 case store_vec_info_type:
5944 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
5945 gcc_assert (done);
5946 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
5948 /* In case of interleaving, the whole chain is vectorized when the
5949 last store in the chain is reached. Store stmts before the last
5950 one are skipped, and there vec_stmt_info shouldn't be freed
5951 meanwhile. */
5952 *grouped_store = true;
5953 if (STMT_VINFO_VEC_STMT (stmt_info))
5954 is_store = true;
5956 else
5957 is_store = true;
5958 break;
5960 case condition_vec_info_type:
5961 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
5962 gcc_assert (done);
5963 break;
5965 case call_vec_info_type:
5966 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
5967 stmt = gsi_stmt (*gsi);
5968 break;
5970 case reduc_vec_info_type:
5971 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
5972 gcc_assert (done);
5973 break;
5975 default:
5976 if (!STMT_VINFO_LIVE_P (stmt_info))
5978 if (dump_enabled_p ())
5979 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5980 "stmt not supported.\n");
5981 gcc_unreachable ();
5985 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
5986 is being vectorized, but outside the immediately enclosing loop. */
5987 if (vec_stmt
5988 && STMT_VINFO_LOOP_VINFO (stmt_info)
5989 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
5990 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
5991 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
5992 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
5993 || STMT_VINFO_RELEVANT (stmt_info) ==
5994 vect_used_in_outer_by_reduction))
5996 struct loop *innerloop = LOOP_VINFO_LOOP (
5997 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
5998 imm_use_iterator imm_iter;
5999 use_operand_p use_p;
6000 tree scalar_dest;
6001 gimple exit_phi;
6003 if (dump_enabled_p ())
6004 dump_printf_loc (MSG_NOTE, vect_location,
6005 "Record the vdef for outer-loop vectorization.\n");
6007 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
6008 (to be used when vectorizing outer-loop stmts that use the DEF of
6009 STMT). */
6010 if (gimple_code (stmt) == GIMPLE_PHI)
6011 scalar_dest = PHI_RESULT (stmt);
6012 else
6013 scalar_dest = gimple_assign_lhs (stmt);
6015 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
6017 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
6019 exit_phi = USE_STMT (use_p);
6020 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
6025 /* Handle stmts whose DEF is used outside the loop-nest that is
6026 being vectorized. */
6027 if (STMT_VINFO_LIVE_P (stmt_info)
6028 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
6030 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
6031 gcc_assert (done);
6034 if (vec_stmt)
6035 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
6037 return is_store;
6041 /* Remove a group of stores (for SLP or interleaving), free their
6042 stmt_vec_info. */
6044 void
6045 vect_remove_stores (gimple first_stmt)
6047 gimple next = first_stmt;
6048 gimple tmp;
6049 gimple_stmt_iterator next_si;
6051 while (next)
6053 stmt_vec_info stmt_info = vinfo_for_stmt (next);
6055 tmp = GROUP_NEXT_ELEMENT (stmt_info);
6056 if (is_pattern_stmt_p (stmt_info))
6057 next = STMT_VINFO_RELATED_STMT (stmt_info);
6058 /* Free the attached stmt_vec_info and remove the stmt. */
6059 next_si = gsi_for_stmt (next);
6060 unlink_stmt_vdef (next);
6061 gsi_remove (&next_si, true);
6062 release_defs (next);
6063 free_stmt_vec_info (next);
6064 next = tmp;
6069 /* Function new_stmt_vec_info.
6071 Create and initialize a new stmt_vec_info struct for STMT. */
6073 stmt_vec_info
6074 new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
6075 bb_vec_info bb_vinfo)
6077 stmt_vec_info res;
6078 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
6080 STMT_VINFO_TYPE (res) = undef_vec_info_type;
6081 STMT_VINFO_STMT (res) = stmt;
6082 STMT_VINFO_LOOP_VINFO (res) = loop_vinfo;
6083 STMT_VINFO_BB_VINFO (res) = bb_vinfo;
6084 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
6085 STMT_VINFO_LIVE_P (res) = false;
6086 STMT_VINFO_VECTYPE (res) = NULL;
6087 STMT_VINFO_VEC_STMT (res) = NULL;
6088 STMT_VINFO_VECTORIZABLE (res) = true;
6089 STMT_VINFO_IN_PATTERN_P (res) = false;
6090 STMT_VINFO_RELATED_STMT (res) = NULL;
6091 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
6092 STMT_VINFO_DATA_REF (res) = NULL;
6094 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
6095 STMT_VINFO_DR_OFFSET (res) = NULL;
6096 STMT_VINFO_DR_INIT (res) = NULL;
6097 STMT_VINFO_DR_STEP (res) = NULL;
6098 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
6100 if (gimple_code (stmt) == GIMPLE_PHI
6101 && is_loop_header_bb_p (gimple_bb (stmt)))
6102 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
6103 else
6104 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
6106 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
6107 STMT_SLP_TYPE (res) = loop_vect;
6108 GROUP_FIRST_ELEMENT (res) = NULL;
6109 GROUP_NEXT_ELEMENT (res) = NULL;
6110 GROUP_SIZE (res) = 0;
6111 GROUP_STORE_COUNT (res) = 0;
6112 GROUP_GAP (res) = 0;
6113 GROUP_SAME_DR_STMT (res) = NULL;
6115 return res;
6119 /* Create a hash table for stmt_vec_info. */
6121 void
6122 init_stmt_vec_info_vec (void)
6124 gcc_assert (!stmt_vec_info_vec.exists ());
6125 stmt_vec_info_vec.create (50);
6129 /* Free hash table for stmt_vec_info. */
6131 void
6132 free_stmt_vec_info_vec (void)
6134 unsigned int i;
6135 vec_void_p info;
6136 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
6137 if (info != NULL)
6138 free_stmt_vec_info (STMT_VINFO_STMT ((stmt_vec_info) info));
6139 gcc_assert (stmt_vec_info_vec.exists ());
6140 stmt_vec_info_vec.release ();
6144 /* Free stmt vectorization related info. */
6146 void
6147 free_stmt_vec_info (gimple stmt)
6149 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6151 if (!stmt_info)
6152 return;
6154 /* Check if this statement has a related "pattern stmt"
6155 (introduced by the vectorizer during the pattern recognition
6156 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
6157 too. */
6158 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
6160 stmt_vec_info patt_info
6161 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
6162 if (patt_info)
6164 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
6165 if (seq)
6167 gimple_stmt_iterator si;
6168 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
6169 free_stmt_vec_info (gsi_stmt (si));
6171 free_stmt_vec_info (STMT_VINFO_RELATED_STMT (stmt_info));
6175 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
6176 set_vinfo_for_stmt (stmt, NULL);
6177 free (stmt_info);
6181 /* Function get_vectype_for_scalar_type_and_size.
6183 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
6184 by the target. */
6186 static tree
6187 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
6189 enum machine_mode inner_mode = TYPE_MODE (scalar_type);
6190 enum machine_mode simd_mode;
6191 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
6192 int nunits;
6193 tree vectype;
6195 if (nbytes == 0)
6196 return NULL_TREE;
6198 if (GET_MODE_CLASS (inner_mode) != MODE_INT
6199 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
6200 return NULL_TREE;
6202 /* For vector types of elements whose mode precision doesn't
6203 match their types precision we use a element type of mode
6204 precision. The vectorization routines will have to make sure
6205 they support the proper result truncation/extension.
6206 We also make sure to build vector types with INTEGER_TYPE
6207 component type only. */
6208 if (INTEGRAL_TYPE_P (scalar_type)
6209 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
6210 || TREE_CODE (scalar_type) != INTEGER_TYPE))
6211 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
6212 TYPE_UNSIGNED (scalar_type));
6214 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
6215 When the component mode passes the above test simply use a type
6216 corresponding to that mode. The theory is that any use that
6217 would cause problems with this will disable vectorization anyway. */
6218 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
6219 && !INTEGRAL_TYPE_P (scalar_type))
6220 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
6222 /* We can't build a vector type of elements with alignment bigger than
6223 their size. */
6224 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
6225 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
6226 TYPE_UNSIGNED (scalar_type));
6228 /* If we felt back to using the mode fail if there was
6229 no scalar type for it. */
6230 if (scalar_type == NULL_TREE)
6231 return NULL_TREE;
6233 /* If no size was supplied use the mode the target prefers. Otherwise
6234 lookup a vector mode of the specified size. */
6235 if (size == 0)
6236 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
6237 else
6238 simd_mode = mode_for_vector (inner_mode, size / nbytes);
6239 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
6240 if (nunits <= 1)
6241 return NULL_TREE;
6243 vectype = build_vector_type (scalar_type, nunits);
6245 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
6246 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
6247 return NULL_TREE;
6249 return vectype;
6252 unsigned int current_vector_size;
6254 /* Function get_vectype_for_scalar_type.
6256 Returns the vector type corresponding to SCALAR_TYPE as supported
6257 by the target. */
6259 tree
6260 get_vectype_for_scalar_type (tree scalar_type)
6262 tree vectype;
6263 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
6264 current_vector_size);
6265 if (vectype
6266 && current_vector_size == 0)
6267 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
6268 return vectype;
6271 /* Function get_same_sized_vectype
6273 Returns a vector type corresponding to SCALAR_TYPE of size
6274 VECTOR_TYPE if supported by the target. */
6276 tree
6277 get_same_sized_vectype (tree scalar_type, tree vector_type)
6279 return get_vectype_for_scalar_type_and_size
6280 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
6283 /* Function vect_is_simple_use.
6285 Input:
6286 LOOP_VINFO - the vect info of the loop that is being vectorized.
6287 BB_VINFO - the vect info of the basic block that is being vectorized.
6288 OPERAND - operand of STMT in the loop or bb.
6289 DEF - the defining stmt in case OPERAND is an SSA_NAME.
6291 Returns whether a stmt with OPERAND can be vectorized.
6292 For loops, supportable operands are constants, loop invariants, and operands
6293 that are defined by the current iteration of the loop. Unsupportable
6294 operands are those that are defined by a previous iteration of the loop (as
6295 is the case in reduction/induction computations).
6296 For basic blocks, supportable operands are constants and bb invariants.
6297 For now, operands defined outside the basic block are not supported. */
6299 bool
6300 vect_is_simple_use (tree operand, gimple stmt, loop_vec_info loop_vinfo,
6301 bb_vec_info bb_vinfo, gimple *def_stmt,
6302 tree *def, enum vect_def_type *dt)
6304 basic_block bb;
6305 stmt_vec_info stmt_vinfo;
6306 struct loop *loop = NULL;
6308 if (loop_vinfo)
6309 loop = LOOP_VINFO_LOOP (loop_vinfo);
6311 *def_stmt = NULL;
6312 *def = NULL_TREE;
6314 if (dump_enabled_p ())
6316 dump_printf_loc (MSG_NOTE, vect_location,
6317 "vect_is_simple_use: operand ");
6318 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
6319 dump_printf (MSG_NOTE, "\n");
6322 if (CONSTANT_CLASS_P (operand))
6324 *dt = vect_constant_def;
6325 return true;
6328 if (is_gimple_min_invariant (operand))
6330 *def = operand;
6331 *dt = vect_external_def;
6332 return true;
6335 if (TREE_CODE (operand) == PAREN_EXPR)
6337 if (dump_enabled_p ())
6338 dump_printf_loc (MSG_NOTE, vect_location, "non-associatable copy.\n");
6339 operand = TREE_OPERAND (operand, 0);
6342 if (TREE_CODE (operand) != SSA_NAME)
6344 if (dump_enabled_p ())
6345 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6346 "not ssa-name.\n");
6347 return false;
6350 *def_stmt = SSA_NAME_DEF_STMT (operand);
6351 if (*def_stmt == NULL)
6353 if (dump_enabled_p ())
6354 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6355 "no def_stmt.\n");
6356 return false;
6359 if (dump_enabled_p ())
6361 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
6362 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
6363 dump_printf (MSG_NOTE, "\n");
6366 /* Empty stmt is expected only in case of a function argument.
6367 (Otherwise - we expect a phi_node or a GIMPLE_ASSIGN). */
6368 if (gimple_nop_p (*def_stmt))
6370 *def = operand;
6371 *dt = vect_external_def;
6372 return true;
6375 bb = gimple_bb (*def_stmt);
6377 if ((loop && !flow_bb_inside_loop_p (loop, bb))
6378 || (!loop && bb != BB_VINFO_BB (bb_vinfo))
6379 || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI))
6380 *dt = vect_external_def;
6381 else
6383 stmt_vinfo = vinfo_for_stmt (*def_stmt);
6384 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
6387 if (*dt == vect_unknown_def_type
6388 || (stmt
6389 && *dt == vect_double_reduction_def
6390 && gimple_code (stmt) != GIMPLE_PHI))
6392 if (dump_enabled_p ())
6393 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6394 "Unsupported pattern.\n");
6395 return false;
6398 if (dump_enabled_p ())
6399 dump_printf_loc (MSG_NOTE, vect_location, "type of def: %d.\n", *dt);
6401 switch (gimple_code (*def_stmt))
6403 case GIMPLE_PHI:
6404 *def = gimple_phi_result (*def_stmt);
6405 break;
6407 case GIMPLE_ASSIGN:
6408 *def = gimple_assign_lhs (*def_stmt);
6409 break;
6411 case GIMPLE_CALL:
6412 *def = gimple_call_lhs (*def_stmt);
6413 if (*def != NULL)
6414 break;
6415 /* FALLTHRU */
6416 default:
6417 if (dump_enabled_p ())
6418 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6419 "unsupported defining stmt:\n");
6420 return false;
6423 return true;
6426 /* Function vect_is_simple_use_1.
6428 Same as vect_is_simple_use_1 but also determines the vector operand
6429 type of OPERAND and stores it to *VECTYPE. If the definition of
6430 OPERAND is vect_uninitialized_def, vect_constant_def or
6431 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
6432 is responsible to compute the best suited vector type for the
6433 scalar operand. */
6435 bool
6436 vect_is_simple_use_1 (tree operand, gimple stmt, loop_vec_info loop_vinfo,
6437 bb_vec_info bb_vinfo, gimple *def_stmt,
6438 tree *def, enum vect_def_type *dt, tree *vectype)
6440 if (!vect_is_simple_use (operand, stmt, loop_vinfo, bb_vinfo, def_stmt,
6441 def, dt))
6442 return false;
6444 /* Now get a vector type if the def is internal, otherwise supply
6445 NULL_TREE and leave it up to the caller to figure out a proper
6446 type for the use stmt. */
6447 if (*dt == vect_internal_def
6448 || *dt == vect_induction_def
6449 || *dt == vect_reduction_def
6450 || *dt == vect_double_reduction_def
6451 || *dt == vect_nested_cycle)
6453 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
6455 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6456 && !STMT_VINFO_RELEVANT (stmt_info)
6457 && !STMT_VINFO_LIVE_P (stmt_info))
6458 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
6460 *vectype = STMT_VINFO_VECTYPE (stmt_info);
6461 gcc_assert (*vectype != NULL_TREE);
6463 else if (*dt == vect_uninitialized_def
6464 || *dt == vect_constant_def
6465 || *dt == vect_external_def)
6466 *vectype = NULL_TREE;
6467 else
6468 gcc_unreachable ();
6470 return true;
6474 /* Function supportable_widening_operation
6476 Check whether an operation represented by the code CODE is a
6477 widening operation that is supported by the target platform in
6478 vector form (i.e., when operating on arguments of type VECTYPE_IN
6479 producing a result of type VECTYPE_OUT).
6481 Widening operations we currently support are NOP (CONVERT), FLOAT
6482 and WIDEN_MULT. This function checks if these operations are supported
6483 by the target platform either directly (via vector tree-codes), or via
6484 target builtins.
6486 Output:
6487 - CODE1 and CODE2 are codes of vector operations to be used when
6488 vectorizing the operation, if available.
6489 - MULTI_STEP_CVT determines the number of required intermediate steps in
6490 case of multi-step conversion (like char->short->int - in that case
6491 MULTI_STEP_CVT will be 1).
6492 - INTERM_TYPES contains the intermediate type required to perform the
6493 widening operation (short in the above example). */
6495 bool
6496 supportable_widening_operation (enum tree_code code, gimple stmt,
6497 tree vectype_out, tree vectype_in,
6498 enum tree_code *code1, enum tree_code *code2,
6499 int *multi_step_cvt,
6500 vec<tree> *interm_types)
6502 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6503 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
6504 struct loop *vect_loop = NULL;
6505 enum machine_mode vec_mode;
6506 enum insn_code icode1, icode2;
6507 optab optab1, optab2;
6508 tree vectype = vectype_in;
6509 tree wide_vectype = vectype_out;
6510 enum tree_code c1, c2;
6511 int i;
6512 tree prev_type, intermediate_type;
6513 enum machine_mode intermediate_mode, prev_mode;
6514 optab optab3, optab4;
6516 *multi_step_cvt = 0;
6517 if (loop_info)
6518 vect_loop = LOOP_VINFO_LOOP (loop_info);
6520 switch (code)
6522 case WIDEN_MULT_EXPR:
6523 /* The result of a vectorized widening operation usually requires
6524 two vectors (because the widened results do not fit into one vector).
6525 The generated vector results would normally be expected to be
6526 generated in the same order as in the original scalar computation,
6527 i.e. if 8 results are generated in each vector iteration, they are
6528 to be organized as follows:
6529 vect1: [res1,res2,res3,res4],
6530 vect2: [res5,res6,res7,res8].
6532 However, in the special case that the result of the widening
6533 operation is used in a reduction computation only, the order doesn't
6534 matter (because when vectorizing a reduction we change the order of
6535 the computation). Some targets can take advantage of this and
6536 generate more efficient code. For example, targets like Altivec,
6537 that support widen_mult using a sequence of {mult_even,mult_odd}
6538 generate the following vectors:
6539 vect1: [res1,res3,res5,res7],
6540 vect2: [res2,res4,res6,res8].
6542 When vectorizing outer-loops, we execute the inner-loop sequentially
6543 (each vectorized inner-loop iteration contributes to VF outer-loop
6544 iterations in parallel). We therefore don't allow to change the
6545 order of the computation in the inner-loop during outer-loop
6546 vectorization. */
6547 /* TODO: Another case in which order doesn't *really* matter is when we
6548 widen and then contract again, e.g. (short)((int)x * y >> 8).
6549 Normally, pack_trunc performs an even/odd permute, whereas the
6550 repack from an even/odd expansion would be an interleave, which
6551 would be significantly simpler for e.g. AVX2. */
6552 /* In any case, in order to avoid duplicating the code below, recurse
6553 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
6554 are properly set up for the caller. If we fail, we'll continue with
6555 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
6556 if (vect_loop
6557 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
6558 && !nested_in_vect_loop_p (vect_loop, stmt)
6559 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
6560 stmt, vectype_out, vectype_in,
6561 code1, code2, multi_step_cvt,
6562 interm_types))
6563 return true;
6564 c1 = VEC_WIDEN_MULT_LO_EXPR;
6565 c2 = VEC_WIDEN_MULT_HI_EXPR;
6566 break;
6568 case VEC_WIDEN_MULT_EVEN_EXPR:
6569 /* Support the recursion induced just above. */
6570 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
6571 c2 = VEC_WIDEN_MULT_ODD_EXPR;
6572 break;
6574 case WIDEN_LSHIFT_EXPR:
6575 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
6576 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
6577 break;
6579 CASE_CONVERT:
6580 c1 = VEC_UNPACK_LO_EXPR;
6581 c2 = VEC_UNPACK_HI_EXPR;
6582 break;
6584 case FLOAT_EXPR:
6585 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
6586 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
6587 break;
6589 case FIX_TRUNC_EXPR:
6590 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
6591 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
6592 computing the operation. */
6593 return false;
6595 default:
6596 gcc_unreachable ();
6599 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
6601 enum tree_code ctmp = c1;
6602 c1 = c2;
6603 c2 = ctmp;
6606 if (code == FIX_TRUNC_EXPR)
6608 /* The signedness is determined from output operand. */
6609 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
6610 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
6612 else
6614 optab1 = optab_for_tree_code (c1, vectype, optab_default);
6615 optab2 = optab_for_tree_code (c2, vectype, optab_default);
6618 if (!optab1 || !optab2)
6619 return false;
6621 vec_mode = TYPE_MODE (vectype);
6622 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
6623 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
6624 return false;
6626 *code1 = c1;
6627 *code2 = c2;
6629 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
6630 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
6631 return true;
6633 /* Check if it's a multi-step conversion that can be done using intermediate
6634 types. */
6636 prev_type = vectype;
6637 prev_mode = vec_mode;
6639 if (!CONVERT_EXPR_CODE_P (code))
6640 return false;
6642 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
6643 intermediate steps in promotion sequence. We try
6644 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
6645 not. */
6646 interm_types->create (MAX_INTERM_CVT_STEPS);
6647 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
6649 intermediate_mode = insn_data[icode1].operand[0].mode;
6650 intermediate_type
6651 = lang_hooks.types.type_for_mode (intermediate_mode,
6652 TYPE_UNSIGNED (prev_type));
6653 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
6654 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
6656 if (!optab3 || !optab4
6657 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
6658 || insn_data[icode1].operand[0].mode != intermediate_mode
6659 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
6660 || insn_data[icode2].operand[0].mode != intermediate_mode
6661 || ((icode1 = optab_handler (optab3, intermediate_mode))
6662 == CODE_FOR_nothing)
6663 || ((icode2 = optab_handler (optab4, intermediate_mode))
6664 == CODE_FOR_nothing))
6665 break;
6667 interm_types->quick_push (intermediate_type);
6668 (*multi_step_cvt)++;
6670 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
6671 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
6672 return true;
6674 prev_type = intermediate_type;
6675 prev_mode = intermediate_mode;
6678 interm_types->release ();
6679 return false;
6683 /* Function supportable_narrowing_operation
6685 Check whether an operation represented by the code CODE is a
6686 narrowing operation that is supported by the target platform in
6687 vector form (i.e., when operating on arguments of type VECTYPE_IN
6688 and producing a result of type VECTYPE_OUT).
6690 Narrowing operations we currently support are NOP (CONVERT) and
6691 FIX_TRUNC. This function checks if these operations are supported by
6692 the target platform directly via vector tree-codes.
6694 Output:
6695 - CODE1 is the code of a vector operation to be used when
6696 vectorizing the operation, if available.
6697 - MULTI_STEP_CVT determines the number of required intermediate steps in
6698 case of multi-step conversion (like int->short->char - in that case
6699 MULTI_STEP_CVT will be 1).
6700 - INTERM_TYPES contains the intermediate type required to perform the
6701 narrowing operation (short in the above example). */
6703 bool
6704 supportable_narrowing_operation (enum tree_code code,
6705 tree vectype_out, tree vectype_in,
6706 enum tree_code *code1, int *multi_step_cvt,
6707 vec<tree> *interm_types)
6709 enum machine_mode vec_mode;
6710 enum insn_code icode1;
6711 optab optab1, interm_optab;
6712 tree vectype = vectype_in;
6713 tree narrow_vectype = vectype_out;
6714 enum tree_code c1;
6715 tree intermediate_type;
6716 enum machine_mode intermediate_mode, prev_mode;
6717 int i;
6718 bool uns;
6720 *multi_step_cvt = 0;
6721 switch (code)
6723 CASE_CONVERT:
6724 c1 = VEC_PACK_TRUNC_EXPR;
6725 break;
6727 case FIX_TRUNC_EXPR:
6728 c1 = VEC_PACK_FIX_TRUNC_EXPR;
6729 break;
6731 case FLOAT_EXPR:
6732 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
6733 tree code and optabs used for computing the operation. */
6734 return false;
6736 default:
6737 gcc_unreachable ();
6740 if (code == FIX_TRUNC_EXPR)
6741 /* The signedness is determined from output operand. */
6742 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
6743 else
6744 optab1 = optab_for_tree_code (c1, vectype, optab_default);
6746 if (!optab1)
6747 return false;
6749 vec_mode = TYPE_MODE (vectype);
6750 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
6751 return false;
6753 *code1 = c1;
6755 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
6756 return true;
6758 /* Check if it's a multi-step conversion that can be done using intermediate
6759 types. */
6760 prev_mode = vec_mode;
6761 if (code == FIX_TRUNC_EXPR)
6762 uns = TYPE_UNSIGNED (vectype_out);
6763 else
6764 uns = TYPE_UNSIGNED (vectype);
6766 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
6767 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
6768 costly than signed. */
6769 if (code == FIX_TRUNC_EXPR && uns)
6771 enum insn_code icode2;
6773 intermediate_type
6774 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
6775 interm_optab
6776 = optab_for_tree_code (c1, intermediate_type, optab_default);
6777 if (interm_optab != unknown_optab
6778 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
6779 && insn_data[icode1].operand[0].mode
6780 == insn_data[icode2].operand[0].mode)
6782 uns = false;
6783 optab1 = interm_optab;
6784 icode1 = icode2;
6788 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
6789 intermediate steps in promotion sequence. We try
6790 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
6791 interm_types->create (MAX_INTERM_CVT_STEPS);
6792 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
6794 intermediate_mode = insn_data[icode1].operand[0].mode;
6795 intermediate_type
6796 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
6797 interm_optab
6798 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
6799 optab_default);
6800 if (!interm_optab
6801 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
6802 || insn_data[icode1].operand[0].mode != intermediate_mode
6803 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
6804 == CODE_FOR_nothing))
6805 break;
6807 interm_types->quick_push (intermediate_type);
6808 (*multi_step_cvt)++;
6810 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
6811 return true;
6813 prev_mode = intermediate_mode;
6814 optab1 = interm_optab;
6817 interm_types->release ();
6818 return false;