inclhack.def (suse_linux_vt_cxx): New fix for linux/vt.h being not compatible with...
[official-gcc.git] / gcc / tree-vect-stmts.c
blobff7b59aa52d34cbd01a1505311f727dd22b4601c
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 "tree-ssa-loop.h"
50 #include "tree-scalar-evolution.h"
51 #include "expr.h"
52 #include "recog.h" /* FIXME: for insn_data */
53 #include "optabs.h"
54 #include "diagnostic-core.h"
55 #include "tree-vectorizer.h"
56 #include "dumpfile.h"
57 #include "cgraph.h"
59 /* For lang_hooks.types.type_for_mode. */
60 #include "langhooks.h"
62 /* Return the vectorized type for the given statement. */
64 tree
65 stmt_vectype (struct _stmt_vec_info *stmt_info)
67 return STMT_VINFO_VECTYPE (stmt_info);
70 /* Return TRUE iff the given statement is in an inner loop relative to
71 the loop being vectorized. */
72 bool
73 stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
75 gimple stmt = STMT_VINFO_STMT (stmt_info);
76 basic_block bb = gimple_bb (stmt);
77 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
78 struct loop* loop;
80 if (!loop_vinfo)
81 return false;
83 loop = LOOP_VINFO_LOOP (loop_vinfo);
85 return (bb->loop_father == loop->inner);
88 /* Record the cost of a statement, either by directly informing the
89 target model or by saving it in a vector for later processing.
90 Return a preliminary estimate of the statement's cost. */
92 unsigned
93 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
94 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
95 int misalign, enum vect_cost_model_location where)
97 if (body_cost_vec)
99 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
100 add_stmt_info_to_vec (body_cost_vec, count, kind,
101 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
102 misalign);
103 return (unsigned)
104 (builtin_vectorization_cost (kind, vectype, misalign) * count);
107 else
109 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
110 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
111 void *target_cost_data;
113 if (loop_vinfo)
114 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
115 else
116 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
118 return add_stmt_cost (target_cost_data, count, kind, stmt_info,
119 misalign, where);
123 /* Return a variable of type ELEM_TYPE[NELEMS]. */
125 static tree
126 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
128 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
129 "vect_array");
132 /* ARRAY is an array of vectors created by create_vector_array.
133 Return an SSA_NAME for the vector in index N. The reference
134 is part of the vectorization of STMT and the vector is associated
135 with scalar destination SCALAR_DEST. */
137 static tree
138 read_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
139 tree array, unsigned HOST_WIDE_INT n)
141 tree vect_type, vect, vect_name, array_ref;
142 gimple new_stmt;
144 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
145 vect_type = TREE_TYPE (TREE_TYPE (array));
146 vect = vect_create_destination_var (scalar_dest, vect_type);
147 array_ref = build4 (ARRAY_REF, vect_type, array,
148 build_int_cst (size_type_node, n),
149 NULL_TREE, NULL_TREE);
151 new_stmt = gimple_build_assign (vect, array_ref);
152 vect_name = make_ssa_name (vect, new_stmt);
153 gimple_assign_set_lhs (new_stmt, vect_name);
154 vect_finish_stmt_generation (stmt, new_stmt, gsi);
156 return vect_name;
159 /* ARRAY is an array of vectors created by create_vector_array.
160 Emit code to store SSA_NAME VECT in index N of the array.
161 The store is part of the vectorization of STMT. */
163 static void
164 write_vector_array (gimple stmt, gimple_stmt_iterator *gsi, tree vect,
165 tree array, unsigned HOST_WIDE_INT n)
167 tree array_ref;
168 gimple new_stmt;
170 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
171 build_int_cst (size_type_node, n),
172 NULL_TREE, NULL_TREE);
174 new_stmt = gimple_build_assign (array_ref, vect);
175 vect_finish_stmt_generation (stmt, new_stmt, gsi);
178 /* PTR is a pointer to an array of type TYPE. Return a representation
179 of *PTR. The memory reference replaces those in FIRST_DR
180 (and its group). */
182 static tree
183 create_array_ref (tree type, tree ptr, struct data_reference *first_dr)
185 tree mem_ref, alias_ptr_type;
187 alias_ptr_type = reference_alias_ptr_type (DR_REF (first_dr));
188 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
189 /* Arrays have the same alignment as their type. */
190 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
191 return mem_ref;
194 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
196 /* Function vect_mark_relevant.
198 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
200 static void
201 vect_mark_relevant (vec<gimple> *worklist, gimple stmt,
202 enum vect_relevant relevant, bool live_p,
203 bool used_in_pattern)
205 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
206 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
207 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
208 gimple pattern_stmt;
210 if (dump_enabled_p ())
211 dump_printf_loc (MSG_NOTE, vect_location,
212 "mark relevant %d, live %d.\n", relevant, live_p);
214 /* If this stmt is an original stmt in a pattern, we might need to mark its
215 related pattern stmt instead of the original stmt. However, such stmts
216 may have their own uses that are not in any pattern, in such cases the
217 stmt itself should be marked. */
218 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
220 bool found = false;
221 if (!used_in_pattern)
223 imm_use_iterator imm_iter;
224 use_operand_p use_p;
225 gimple use_stmt;
226 tree lhs;
227 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
228 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
230 if (is_gimple_assign (stmt))
231 lhs = gimple_assign_lhs (stmt);
232 else
233 lhs = gimple_call_lhs (stmt);
235 /* This use is out of pattern use, if LHS has other uses that are
236 pattern uses, we should mark the stmt itself, and not the pattern
237 stmt. */
238 if (TREE_CODE (lhs) == SSA_NAME)
239 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
241 if (is_gimple_debug (USE_STMT (use_p)))
242 continue;
243 use_stmt = USE_STMT (use_p);
245 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
246 continue;
248 if (vinfo_for_stmt (use_stmt)
249 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
251 found = true;
252 break;
257 if (!found)
259 /* This is the last stmt in a sequence that was detected as a
260 pattern that can potentially be vectorized. Don't mark the stmt
261 as relevant/live because it's not going to be vectorized.
262 Instead mark the pattern-stmt that replaces it. */
264 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
266 if (dump_enabled_p ())
267 dump_printf_loc (MSG_NOTE, vect_location,
268 "last stmt in pattern. don't mark"
269 " relevant/live.\n");
270 stmt_info = vinfo_for_stmt (pattern_stmt);
271 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
272 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
273 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
274 stmt = pattern_stmt;
278 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
279 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
280 STMT_VINFO_RELEVANT (stmt_info) = relevant;
282 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
283 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
285 if (dump_enabled_p ())
286 dump_printf_loc (MSG_NOTE, vect_location,
287 "already marked relevant/live.\n");
288 return;
291 worklist->safe_push (stmt);
295 /* Function vect_stmt_relevant_p.
297 Return true if STMT in loop that is represented by LOOP_VINFO is
298 "relevant for vectorization".
300 A stmt is considered "relevant for vectorization" if:
301 - it has uses outside the loop.
302 - it has vdefs (it alters memory).
303 - control stmts in the loop (except for the exit condition).
305 CHECKME: what other side effects would the vectorizer allow? */
307 static bool
308 vect_stmt_relevant_p (gimple stmt, loop_vec_info loop_vinfo,
309 enum vect_relevant *relevant, bool *live_p)
311 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
312 ssa_op_iter op_iter;
313 imm_use_iterator imm_iter;
314 use_operand_p use_p;
315 def_operand_p def_p;
317 *relevant = vect_unused_in_scope;
318 *live_p = false;
320 /* cond stmt other than loop exit cond. */
321 if (is_ctrl_stmt (stmt)
322 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
323 != loop_exit_ctrl_vec_info_type)
324 *relevant = vect_used_in_scope;
326 /* changing memory. */
327 if (gimple_code (stmt) != GIMPLE_PHI)
328 if (gimple_vdef (stmt))
330 if (dump_enabled_p ())
331 dump_printf_loc (MSG_NOTE, vect_location,
332 "vec_stmt_relevant_p: stmt has vdefs.\n");
333 *relevant = vect_used_in_scope;
336 /* uses outside the loop. */
337 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
339 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
341 basic_block bb = gimple_bb (USE_STMT (use_p));
342 if (!flow_bb_inside_loop_p (loop, bb))
344 if (dump_enabled_p ())
345 dump_printf_loc (MSG_NOTE, vect_location,
346 "vec_stmt_relevant_p: used out of loop.\n");
348 if (is_gimple_debug (USE_STMT (use_p)))
349 continue;
351 /* We expect all such uses to be in the loop exit phis
352 (because of loop closed form) */
353 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
354 gcc_assert (bb == single_exit (loop)->dest);
356 *live_p = true;
361 return (*live_p || *relevant);
365 /* Function exist_non_indexing_operands_for_use_p
367 USE is one of the uses attached to STMT. Check if USE is
368 used in STMT for anything other than indexing an array. */
370 static bool
371 exist_non_indexing_operands_for_use_p (tree use, gimple stmt)
373 tree operand;
374 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
376 /* USE corresponds to some operand in STMT. If there is no data
377 reference in STMT, then any operand that corresponds to USE
378 is not indexing an array. */
379 if (!STMT_VINFO_DATA_REF (stmt_info))
380 return true;
382 /* STMT has a data_ref. FORNOW this means that its of one of
383 the following forms:
384 -1- ARRAY_REF = var
385 -2- var = ARRAY_REF
386 (This should have been verified in analyze_data_refs).
388 'var' in the second case corresponds to a def, not a use,
389 so USE cannot correspond to any operands that are not used
390 for array indexing.
392 Therefore, all we need to check is if STMT falls into the
393 first case, and whether var corresponds to USE. */
395 if (!gimple_assign_copy_p (stmt))
396 return false;
397 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
398 return false;
399 operand = gimple_assign_rhs1 (stmt);
400 if (TREE_CODE (operand) != SSA_NAME)
401 return false;
403 if (operand == use)
404 return true;
406 return false;
411 Function process_use.
413 Inputs:
414 - a USE in STMT in a loop represented by LOOP_VINFO
415 - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
416 that defined USE. This is done by calling mark_relevant and passing it
417 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
418 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
419 be performed.
421 Outputs:
422 Generally, LIVE_P and RELEVANT are used to define the liveness and
423 relevance info of the DEF_STMT of this USE:
424 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
425 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
426 Exceptions:
427 - case 1: If USE is used only for address computations (e.g. array indexing),
428 which does not need to be directly vectorized, then the liveness/relevance
429 of the respective DEF_STMT is left unchanged.
430 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
431 skip DEF_STMT cause it had already been processed.
432 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
433 be modified accordingly.
435 Return true if everything is as expected. Return false otherwise. */
437 static bool
438 process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
439 enum vect_relevant relevant, vec<gimple> *worklist,
440 bool force)
442 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
443 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
444 stmt_vec_info dstmt_vinfo;
445 basic_block bb, def_bb;
446 tree def;
447 gimple def_stmt;
448 enum vect_def_type dt;
450 /* case 1: we are only interested in uses that need to be vectorized. Uses
451 that are used for address computation are not considered relevant. */
452 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
453 return true;
455 if (!vect_is_simple_use (use, stmt, loop_vinfo, NULL, &def_stmt, &def, &dt))
457 if (dump_enabled_p ())
458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
459 "not vectorized: unsupported use in stmt.\n");
460 return false;
463 if (!def_stmt || gimple_nop_p (def_stmt))
464 return true;
466 def_bb = gimple_bb (def_stmt);
467 if (!flow_bb_inside_loop_p (loop, def_bb))
469 if (dump_enabled_p ())
470 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
471 return true;
474 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
475 DEF_STMT must have already been processed, because this should be the
476 only way that STMT, which is a reduction-phi, was put in the worklist,
477 as there should be no other uses for DEF_STMT in the loop. So we just
478 check that everything is as expected, and we are done. */
479 dstmt_vinfo = vinfo_for_stmt (def_stmt);
480 bb = gimple_bb (stmt);
481 if (gimple_code (stmt) == GIMPLE_PHI
482 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
483 && gimple_code (def_stmt) != GIMPLE_PHI
484 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
485 && bb->loop_father == def_bb->loop_father)
487 if (dump_enabled_p ())
488 dump_printf_loc (MSG_NOTE, vect_location,
489 "reduc-stmt defining reduc-phi in the same nest.\n");
490 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
491 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
492 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
493 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
494 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
495 return true;
498 /* case 3a: outer-loop stmt defining an inner-loop stmt:
499 outer-loop-header-bb:
500 d = def_stmt
501 inner-loop:
502 stmt # use (d)
503 outer-loop-tail-bb:
504 ... */
505 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
507 if (dump_enabled_p ())
508 dump_printf_loc (MSG_NOTE, vect_location,
509 "outer-loop def-stmt defining inner-loop stmt.\n");
511 switch (relevant)
513 case vect_unused_in_scope:
514 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
515 vect_used_in_scope : vect_unused_in_scope;
516 break;
518 case vect_used_in_outer_by_reduction:
519 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
520 relevant = vect_used_by_reduction;
521 break;
523 case vect_used_in_outer:
524 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
525 relevant = vect_used_in_scope;
526 break;
528 case vect_used_in_scope:
529 break;
531 default:
532 gcc_unreachable ();
536 /* case 3b: inner-loop stmt defining an outer-loop stmt:
537 outer-loop-header-bb:
539 inner-loop:
540 d = def_stmt
541 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
542 stmt # use (d) */
543 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
545 if (dump_enabled_p ())
546 dump_printf_loc (MSG_NOTE, vect_location,
547 "inner-loop def-stmt defining outer-loop stmt.\n");
549 switch (relevant)
551 case vect_unused_in_scope:
552 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
553 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
554 vect_used_in_outer_by_reduction : vect_unused_in_scope;
555 break;
557 case vect_used_by_reduction:
558 relevant = vect_used_in_outer_by_reduction;
559 break;
561 case vect_used_in_scope:
562 relevant = vect_used_in_outer;
563 break;
565 default:
566 gcc_unreachable ();
570 vect_mark_relevant (worklist, def_stmt, relevant, live_p,
571 is_pattern_stmt_p (stmt_vinfo));
572 return true;
576 /* Function vect_mark_stmts_to_be_vectorized.
578 Not all stmts in the loop need to be vectorized. For example:
580 for i...
581 for j...
582 1. T0 = i + j
583 2. T1 = a[T0]
585 3. j = j + 1
587 Stmt 1 and 3 do not need to be vectorized, because loop control and
588 addressing of vectorized data-refs are handled differently.
590 This pass detects such stmts. */
592 bool
593 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
595 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
596 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
597 unsigned int nbbs = loop->num_nodes;
598 gimple_stmt_iterator si;
599 gimple stmt;
600 unsigned int i;
601 stmt_vec_info stmt_vinfo;
602 basic_block bb;
603 gimple phi;
604 bool live_p;
605 enum vect_relevant relevant, tmp_relevant;
606 enum vect_def_type def_type;
608 if (dump_enabled_p ())
609 dump_printf_loc (MSG_NOTE, vect_location,
610 "=== vect_mark_stmts_to_be_vectorized ===\n");
612 stack_vec<gimple, 64> worklist;
614 /* 1. Init worklist. */
615 for (i = 0; i < nbbs; i++)
617 bb = bbs[i];
618 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
620 phi = gsi_stmt (si);
621 if (dump_enabled_p ())
623 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
624 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
625 dump_printf (MSG_NOTE, "\n");
628 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
629 vect_mark_relevant (&worklist, phi, relevant, live_p, false);
631 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
633 stmt = gsi_stmt (si);
634 if (dump_enabled_p ())
636 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
637 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
638 dump_printf (MSG_NOTE, "\n");
641 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
642 vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
646 /* 2. Process_worklist */
647 while (worklist.length () > 0)
649 use_operand_p use_p;
650 ssa_op_iter iter;
652 stmt = worklist.pop ();
653 if (dump_enabled_p ())
655 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
656 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
657 dump_printf (MSG_NOTE, "\n");
660 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
661 (DEF_STMT) as relevant/irrelevant and live/dead according to the
662 liveness and relevance properties of STMT. */
663 stmt_vinfo = vinfo_for_stmt (stmt);
664 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
665 live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
667 /* Generally, the liveness and relevance properties of STMT are
668 propagated as is to the DEF_STMTs of its USEs:
669 live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
670 relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
672 One exception is when STMT has been identified as defining a reduction
673 variable; in this case we set the liveness/relevance as follows:
674 live_p = false
675 relevant = vect_used_by_reduction
676 This is because we distinguish between two kinds of relevant stmts -
677 those that are used by a reduction computation, and those that are
678 (also) used by a regular computation. This allows us later on to
679 identify stmts that are used solely by a reduction, and therefore the
680 order of the results that they produce does not have to be kept. */
682 def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
683 tmp_relevant = relevant;
684 switch (def_type)
686 case vect_reduction_def:
687 switch (tmp_relevant)
689 case vect_unused_in_scope:
690 relevant = vect_used_by_reduction;
691 break;
693 case vect_used_by_reduction:
694 if (gimple_code (stmt) == GIMPLE_PHI)
695 break;
696 /* fall through */
698 default:
699 if (dump_enabled_p ())
700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
701 "unsupported use of reduction.\n");
702 return false;
705 live_p = false;
706 break;
708 case vect_nested_cycle:
709 if (tmp_relevant != vect_unused_in_scope
710 && tmp_relevant != vect_used_in_outer_by_reduction
711 && tmp_relevant != vect_used_in_outer)
713 if (dump_enabled_p ())
714 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
715 "unsupported use of nested cycle.\n");
717 return false;
720 live_p = false;
721 break;
723 case vect_double_reduction_def:
724 if (tmp_relevant != vect_unused_in_scope
725 && tmp_relevant != vect_used_by_reduction)
727 if (dump_enabled_p ())
728 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
729 "unsupported use of double reduction.\n");
731 return false;
734 live_p = false;
735 break;
737 default:
738 break;
741 if (is_pattern_stmt_p (stmt_vinfo))
743 /* Pattern statements are not inserted into the code, so
744 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
745 have to scan the RHS or function arguments instead. */
746 if (is_gimple_assign (stmt))
748 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
749 tree op = gimple_assign_rhs1 (stmt);
751 i = 1;
752 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
754 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
755 live_p, relevant, &worklist, false)
756 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
757 live_p, relevant, &worklist, false))
758 return false;
759 i = 2;
761 for (; i < gimple_num_ops (stmt); i++)
763 op = gimple_op (stmt, i);
764 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
765 &worklist, false))
766 return false;
769 else if (is_gimple_call (stmt))
771 for (i = 0; i < gimple_call_num_args (stmt); i++)
773 tree arg = gimple_call_arg (stmt, i);
774 if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
775 &worklist, false))
776 return false;
780 else
781 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
783 tree op = USE_FROM_PTR (use_p);
784 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
785 &worklist, false))
786 return false;
789 if (STMT_VINFO_GATHER_P (stmt_vinfo))
791 tree off;
792 tree decl = vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
793 gcc_assert (decl);
794 if (!process_use (stmt, off, loop_vinfo, live_p, relevant,
795 &worklist, true))
796 return false;
798 } /* while worklist */
800 return true;
804 /* Function vect_model_simple_cost.
806 Models cost for simple operations, i.e. those that only emit ncopies of a
807 single op. Right now, this does not account for multiple insns that could
808 be generated for the single vector op. We will handle that shortly. */
810 void
811 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
812 enum vect_def_type *dt,
813 stmt_vector_for_cost *prologue_cost_vec,
814 stmt_vector_for_cost *body_cost_vec)
816 int i;
817 int inside_cost = 0, prologue_cost = 0;
819 /* The SLP costs were already calculated during SLP tree build. */
820 if (PURE_SLP_STMT (stmt_info))
821 return;
823 /* FORNOW: Assuming maximum 2 args per stmts. */
824 for (i = 0; i < 2; i++)
825 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
826 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
827 stmt_info, 0, vect_prologue);
829 /* Pass the inside-of-loop statements to the target-specific cost model. */
830 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
831 stmt_info, 0, vect_body);
833 if (dump_enabled_p ())
834 dump_printf_loc (MSG_NOTE, vect_location,
835 "vect_model_simple_cost: inside_cost = %d, "
836 "prologue_cost = %d .\n", inside_cost, prologue_cost);
840 /* Model cost for type demotion and promotion operations. PWR is normally
841 zero for single-step promotions and demotions. It will be one if
842 two-step promotion/demotion is required, and so on. Each additional
843 step doubles the number of instructions required. */
845 static void
846 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
847 enum vect_def_type *dt, int pwr)
849 int i, tmp;
850 int inside_cost = 0, prologue_cost = 0;
851 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
852 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
853 void *target_cost_data;
855 /* The SLP costs were already calculated during SLP tree build. */
856 if (PURE_SLP_STMT (stmt_info))
857 return;
859 if (loop_vinfo)
860 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
861 else
862 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
864 for (i = 0; i < pwr + 1; i++)
866 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
867 (i + 1) : i;
868 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
869 vec_promote_demote, stmt_info, 0,
870 vect_body);
873 /* FORNOW: Assuming maximum 2 args per stmts. */
874 for (i = 0; i < 2; i++)
875 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
876 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
877 stmt_info, 0, vect_prologue);
879 if (dump_enabled_p ())
880 dump_printf_loc (MSG_NOTE, vect_location,
881 "vect_model_promotion_demotion_cost: inside_cost = %d, "
882 "prologue_cost = %d .\n", inside_cost, prologue_cost);
885 /* Function vect_cost_group_size
887 For grouped load or store, return the group_size only if it is the first
888 load or store of a group, else return 1. This ensures that group size is
889 only returned once per group. */
891 static int
892 vect_cost_group_size (stmt_vec_info stmt_info)
894 gimple first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
896 if (first_stmt == STMT_VINFO_STMT (stmt_info))
897 return GROUP_SIZE (stmt_info);
899 return 1;
903 /* Function vect_model_store_cost
905 Models cost for stores. In the case of grouped accesses, one access
906 has the overhead of the grouped access attributed to it. */
908 void
909 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
910 bool store_lanes_p, enum vect_def_type dt,
911 slp_tree slp_node,
912 stmt_vector_for_cost *prologue_cost_vec,
913 stmt_vector_for_cost *body_cost_vec)
915 int group_size;
916 unsigned int inside_cost = 0, prologue_cost = 0;
917 struct data_reference *first_dr;
918 gimple first_stmt;
920 /* The SLP costs were already calculated during SLP tree build. */
921 if (PURE_SLP_STMT (stmt_info))
922 return;
924 if (dt == vect_constant_def || dt == vect_external_def)
925 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
926 stmt_info, 0, vect_prologue);
928 /* Grouped access? */
929 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
931 if (slp_node)
933 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
934 group_size = 1;
936 else
938 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
939 group_size = vect_cost_group_size (stmt_info);
942 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
944 /* Not a grouped access. */
945 else
947 group_size = 1;
948 first_dr = STMT_VINFO_DATA_REF (stmt_info);
951 /* We assume that the cost of a single store-lanes instruction is
952 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
953 access is instead being provided by a permute-and-store operation,
954 include the cost of the permutes. */
955 if (!store_lanes_p && group_size > 1)
957 /* Uses a high and low interleave operation for each needed permute. */
959 int nstmts = ncopies * exact_log2 (group_size) * group_size;
960 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
961 stmt_info, 0, vect_body);
963 if (dump_enabled_p ())
964 dump_printf_loc (MSG_NOTE, vect_location,
965 "vect_model_store_cost: strided group_size = %d .\n",
966 group_size);
969 /* Costs of the stores. */
970 vect_get_store_cost (first_dr, ncopies, &inside_cost, body_cost_vec);
972 if (dump_enabled_p ())
973 dump_printf_loc (MSG_NOTE, vect_location,
974 "vect_model_store_cost: inside_cost = %d, "
975 "prologue_cost = %d .\n", inside_cost, prologue_cost);
979 /* Calculate cost of DR's memory access. */
980 void
981 vect_get_store_cost (struct data_reference *dr, int ncopies,
982 unsigned int *inside_cost,
983 stmt_vector_for_cost *body_cost_vec)
985 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
986 gimple stmt = DR_STMT (dr);
987 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
989 switch (alignment_support_scheme)
991 case dr_aligned:
993 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
994 vector_store, stmt_info, 0,
995 vect_body);
997 if (dump_enabled_p ())
998 dump_printf_loc (MSG_NOTE, vect_location,
999 "vect_model_store_cost: aligned.\n");
1000 break;
1003 case dr_unaligned_supported:
1005 /* Here, we assign an additional cost for the unaligned store. */
1006 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1007 unaligned_store, stmt_info,
1008 DR_MISALIGNMENT (dr), vect_body);
1009 if (dump_enabled_p ())
1010 dump_printf_loc (MSG_NOTE, vect_location,
1011 "vect_model_store_cost: unaligned supported by "
1012 "hardware.\n");
1013 break;
1016 case dr_unaligned_unsupported:
1018 *inside_cost = VECT_MAX_COST;
1020 if (dump_enabled_p ())
1021 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1022 "vect_model_store_cost: unsupported access.\n");
1023 break;
1026 default:
1027 gcc_unreachable ();
1032 /* Function vect_model_load_cost
1034 Models cost for loads. In the case of grouped accesses, the last access
1035 has the overhead of the grouped access attributed to it. Since unaligned
1036 accesses are supported for loads, we also account for the costs of the
1037 access scheme chosen. */
1039 void
1040 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1041 bool load_lanes_p, slp_tree slp_node,
1042 stmt_vector_for_cost *prologue_cost_vec,
1043 stmt_vector_for_cost *body_cost_vec)
1045 int group_size;
1046 gimple first_stmt;
1047 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
1048 unsigned int inside_cost = 0, prologue_cost = 0;
1050 /* The SLP costs were already calculated during SLP tree build. */
1051 if (PURE_SLP_STMT (stmt_info))
1052 return;
1054 /* Grouped accesses? */
1055 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1056 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && first_stmt && !slp_node)
1058 group_size = vect_cost_group_size (stmt_info);
1059 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1061 /* Not a grouped access. */
1062 else
1064 group_size = 1;
1065 first_dr = dr;
1068 /* We assume that the cost of a single load-lanes instruction is
1069 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1070 access is instead being provided by a load-and-permute operation,
1071 include the cost of the permutes. */
1072 if (!load_lanes_p && group_size > 1)
1074 /* Uses an even and odd extract operations for each needed permute. */
1075 int nstmts = ncopies * exact_log2 (group_size) * group_size;
1076 inside_cost += record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1077 stmt_info, 0, vect_body);
1079 if (dump_enabled_p ())
1080 dump_printf_loc (MSG_NOTE, vect_location,
1081 "vect_model_load_cost: strided group_size = %d .\n",
1082 group_size);
1085 /* The loads themselves. */
1086 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1088 /* N scalar loads plus gathering them into a vector. */
1089 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1090 inside_cost += record_stmt_cost (body_cost_vec,
1091 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1092 scalar_load, stmt_info, 0, vect_body);
1093 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1094 stmt_info, 0, vect_body);
1096 else
1097 vect_get_load_cost (first_dr, ncopies,
1098 ((!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1099 || group_size > 1 || slp_node),
1100 &inside_cost, &prologue_cost,
1101 prologue_cost_vec, body_cost_vec, true);
1103 if (dump_enabled_p ())
1104 dump_printf_loc (MSG_NOTE, vect_location,
1105 "vect_model_load_cost: inside_cost = %d, "
1106 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1110 /* Calculate cost of DR's memory access. */
1111 void
1112 vect_get_load_cost (struct data_reference *dr, int ncopies,
1113 bool add_realign_cost, unsigned int *inside_cost,
1114 unsigned int *prologue_cost,
1115 stmt_vector_for_cost *prologue_cost_vec,
1116 stmt_vector_for_cost *body_cost_vec,
1117 bool record_prologue_costs)
1119 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1120 gimple stmt = DR_STMT (dr);
1121 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1123 switch (alignment_support_scheme)
1125 case dr_aligned:
1127 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1128 stmt_info, 0, vect_body);
1130 if (dump_enabled_p ())
1131 dump_printf_loc (MSG_NOTE, vect_location,
1132 "vect_model_load_cost: aligned.\n");
1134 break;
1136 case dr_unaligned_supported:
1138 /* Here, we assign an additional cost for the unaligned load. */
1139 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1140 unaligned_load, stmt_info,
1141 DR_MISALIGNMENT (dr), vect_body);
1143 if (dump_enabled_p ())
1144 dump_printf_loc (MSG_NOTE, vect_location,
1145 "vect_model_load_cost: unaligned supported by "
1146 "hardware.\n");
1148 break;
1150 case dr_explicit_realign:
1152 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1153 vector_load, stmt_info, 0, vect_body);
1154 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1155 vec_perm, stmt_info, 0, vect_body);
1157 /* FIXME: If the misalignment remains fixed across the iterations of
1158 the containing loop, the following cost should be added to the
1159 prologue costs. */
1160 if (targetm.vectorize.builtin_mask_for_load)
1161 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1162 stmt_info, 0, vect_body);
1164 if (dump_enabled_p ())
1165 dump_printf_loc (MSG_NOTE, vect_location,
1166 "vect_model_load_cost: explicit realign\n");
1168 break;
1170 case dr_explicit_realign_optimized:
1172 if (dump_enabled_p ())
1173 dump_printf_loc (MSG_NOTE, vect_location,
1174 "vect_model_load_cost: unaligned software "
1175 "pipelined.\n");
1177 /* Unaligned software pipeline has a load of an address, an initial
1178 load, and possibly a mask operation to "prime" the loop. However,
1179 if this is an access in a group of loads, which provide grouped
1180 access, then the above cost should only be considered for one
1181 access in the group. Inside the loop, there is a load op
1182 and a realignment op. */
1184 if (add_realign_cost && record_prologue_costs)
1186 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1187 vector_stmt, stmt_info,
1188 0, vect_prologue);
1189 if (targetm.vectorize.builtin_mask_for_load)
1190 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1191 vector_stmt, stmt_info,
1192 0, vect_prologue);
1195 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1196 stmt_info, 0, vect_body);
1197 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1198 stmt_info, 0, vect_body);
1200 if (dump_enabled_p ())
1201 dump_printf_loc (MSG_NOTE, vect_location,
1202 "vect_model_load_cost: explicit realign optimized"
1203 "\n");
1205 break;
1208 case dr_unaligned_unsupported:
1210 *inside_cost = VECT_MAX_COST;
1212 if (dump_enabled_p ())
1213 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1214 "vect_model_load_cost: unsupported access.\n");
1215 break;
1218 default:
1219 gcc_unreachable ();
1223 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1224 the loop preheader for the vectorized stmt STMT. */
1226 static void
1227 vect_init_vector_1 (gimple stmt, gimple new_stmt, gimple_stmt_iterator *gsi)
1229 if (gsi)
1230 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1231 else
1233 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1234 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1236 if (loop_vinfo)
1238 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1239 basic_block new_bb;
1240 edge pe;
1242 if (nested_in_vect_loop_p (loop, stmt))
1243 loop = loop->inner;
1245 pe = loop_preheader_edge (loop);
1246 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1247 gcc_assert (!new_bb);
1249 else
1251 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1252 basic_block bb;
1253 gimple_stmt_iterator gsi_bb_start;
1255 gcc_assert (bb_vinfo);
1256 bb = BB_VINFO_BB (bb_vinfo);
1257 gsi_bb_start = gsi_after_labels (bb);
1258 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1262 if (dump_enabled_p ())
1264 dump_printf_loc (MSG_NOTE, vect_location,
1265 "created new init_stmt: ");
1266 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1267 dump_printf (MSG_NOTE, "\n");
1271 /* Function vect_init_vector.
1273 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1274 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1275 vector type a vector with all elements equal to VAL is created first.
1276 Place the initialization at BSI if it is not NULL. Otherwise, place the
1277 initialization at the loop preheader.
1278 Return the DEF of INIT_STMT.
1279 It will be used in the vectorization of STMT. */
1281 tree
1282 vect_init_vector (gimple stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1284 tree new_var;
1285 gimple init_stmt;
1286 tree vec_oprnd;
1287 tree new_temp;
1289 if (TREE_CODE (type) == VECTOR_TYPE
1290 && TREE_CODE (TREE_TYPE (val)) != VECTOR_TYPE)
1292 if (!types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1294 if (CONSTANT_CLASS_P (val))
1295 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (type), val);
1296 else
1298 new_temp = make_ssa_name (TREE_TYPE (type), NULL);
1299 init_stmt = gimple_build_assign_with_ops (NOP_EXPR,
1300 new_temp, val,
1301 NULL_TREE);
1302 vect_init_vector_1 (stmt, init_stmt, gsi);
1303 val = new_temp;
1306 val = build_vector_from_val (type, val);
1309 new_var = vect_get_new_vect_var (type, vect_simple_var, "cst_");
1310 init_stmt = gimple_build_assign (new_var, val);
1311 new_temp = make_ssa_name (new_var, init_stmt);
1312 gimple_assign_set_lhs (init_stmt, new_temp);
1313 vect_init_vector_1 (stmt, init_stmt, gsi);
1314 vec_oprnd = gimple_assign_lhs (init_stmt);
1315 return vec_oprnd;
1319 /* Function vect_get_vec_def_for_operand.
1321 OP is an operand in STMT. This function returns a (vector) def that will be
1322 used in the vectorized stmt for STMT.
1324 In the case that OP is an SSA_NAME which is defined in the loop, then
1325 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1327 In case OP is an invariant or constant, a new stmt that creates a vector def
1328 needs to be introduced. */
1330 tree
1331 vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
1333 tree vec_oprnd;
1334 gimple vec_stmt;
1335 gimple def_stmt;
1336 stmt_vec_info def_stmt_info = NULL;
1337 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1338 unsigned int nunits;
1339 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1340 tree def;
1341 enum vect_def_type dt;
1342 bool is_simple_use;
1343 tree vector_type;
1345 if (dump_enabled_p ())
1347 dump_printf_loc (MSG_NOTE, vect_location,
1348 "vect_get_vec_def_for_operand: ");
1349 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1350 dump_printf (MSG_NOTE, "\n");
1353 is_simple_use = vect_is_simple_use (op, stmt, loop_vinfo, NULL,
1354 &def_stmt, &def, &dt);
1355 gcc_assert (is_simple_use);
1356 if (dump_enabled_p ())
1358 int loc_printed = 0;
1359 if (def)
1361 dump_printf_loc (MSG_NOTE, vect_location, "def = ");
1362 loc_printed = 1;
1363 dump_generic_expr (MSG_NOTE, TDF_SLIM, def);
1364 dump_printf (MSG_NOTE, "\n");
1366 if (def_stmt)
1368 if (loc_printed)
1369 dump_printf (MSG_NOTE, " def_stmt = ");
1370 else
1371 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1372 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1373 dump_printf (MSG_NOTE, "\n");
1377 switch (dt)
1379 /* Case 1: operand is a constant. */
1380 case vect_constant_def:
1382 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1383 gcc_assert (vector_type);
1384 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
1386 if (scalar_def)
1387 *scalar_def = op;
1389 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1390 if (dump_enabled_p ())
1391 dump_printf_loc (MSG_NOTE, vect_location,
1392 "Create vector_cst. nunits = %d\n", nunits);
1394 return vect_init_vector (stmt, op, vector_type, NULL);
1397 /* Case 2: operand is defined outside the loop - loop invariant. */
1398 case vect_external_def:
1400 vector_type = get_vectype_for_scalar_type (TREE_TYPE (def));
1401 gcc_assert (vector_type);
1403 if (scalar_def)
1404 *scalar_def = def;
1406 /* Create 'vec_inv = {inv,inv,..,inv}' */
1407 if (dump_enabled_p ())
1408 dump_printf_loc (MSG_NOTE, vect_location, "Create vector_inv.\n");
1410 return vect_init_vector (stmt, def, vector_type, NULL);
1413 /* Case 3: operand is defined inside the loop. */
1414 case vect_internal_def:
1416 if (scalar_def)
1417 *scalar_def = NULL/* FIXME tuples: def_stmt*/;
1419 /* Get the def from the vectorized stmt. */
1420 def_stmt_info = vinfo_for_stmt (def_stmt);
1422 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1423 /* Get vectorized pattern statement. */
1424 if (!vec_stmt
1425 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1426 && !STMT_VINFO_RELEVANT (def_stmt_info))
1427 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1428 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1429 gcc_assert (vec_stmt);
1430 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1431 vec_oprnd = PHI_RESULT (vec_stmt);
1432 else if (is_gimple_call (vec_stmt))
1433 vec_oprnd = gimple_call_lhs (vec_stmt);
1434 else
1435 vec_oprnd = gimple_assign_lhs (vec_stmt);
1436 return vec_oprnd;
1439 /* Case 4: operand is defined by a loop header phi - reduction */
1440 case vect_reduction_def:
1441 case vect_double_reduction_def:
1442 case vect_nested_cycle:
1444 struct loop *loop;
1446 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1447 loop = (gimple_bb (def_stmt))->loop_father;
1449 /* Get the def before the loop */
1450 op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
1451 return get_initial_def_for_reduction (stmt, op, scalar_def);
1454 /* Case 5: operand is defined by loop-header phi - induction. */
1455 case vect_induction_def:
1457 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1459 /* Get the def from the vectorized stmt. */
1460 def_stmt_info = vinfo_for_stmt (def_stmt);
1461 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1462 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1463 vec_oprnd = PHI_RESULT (vec_stmt);
1464 else
1465 vec_oprnd = gimple_get_lhs (vec_stmt);
1466 return vec_oprnd;
1469 default:
1470 gcc_unreachable ();
1475 /* Function vect_get_vec_def_for_stmt_copy
1477 Return a vector-def for an operand. This function is used when the
1478 vectorized stmt to be created (by the caller to this function) is a "copy"
1479 created in case the vectorized result cannot fit in one vector, and several
1480 copies of the vector-stmt are required. In this case the vector-def is
1481 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1482 of the stmt that defines VEC_OPRND.
1483 DT is the type of the vector def VEC_OPRND.
1485 Context:
1486 In case the vectorization factor (VF) is bigger than the number
1487 of elements that can fit in a vectype (nunits), we have to generate
1488 more than one vector stmt to vectorize the scalar stmt. This situation
1489 arises when there are multiple data-types operated upon in the loop; the
1490 smallest data-type determines the VF, and as a result, when vectorizing
1491 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1492 vector stmt (each computing a vector of 'nunits' results, and together
1493 computing 'VF' results in each iteration). This function is called when
1494 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1495 which VF=16 and nunits=4, so the number of copies required is 4):
1497 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1499 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1500 VS1.1: vx.1 = memref1 VS1.2
1501 VS1.2: vx.2 = memref2 VS1.3
1502 VS1.3: vx.3 = memref3
1504 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1505 VSnew.1: vz1 = vx.1 + ... VSnew.2
1506 VSnew.2: vz2 = vx.2 + ... VSnew.3
1507 VSnew.3: vz3 = vx.3 + ...
1509 The vectorization of S1 is explained in vectorizable_load.
1510 The vectorization of S2:
1511 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1512 the function 'vect_get_vec_def_for_operand' is called to
1513 get the relevant vector-def for each operand of S2. For operand x it
1514 returns the vector-def 'vx.0'.
1516 To create the remaining copies of the vector-stmt (VSnew.j), this
1517 function is called to get the relevant vector-def for each operand. It is
1518 obtained from the respective VS1.j stmt, which is recorded in the
1519 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1521 For example, to obtain the vector-def 'vx.1' in order to create the
1522 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1523 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1524 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1525 and return its def ('vx.1').
1526 Overall, to create the above sequence this function will be called 3 times:
1527 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1528 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1529 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1531 tree
1532 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1534 gimple vec_stmt_for_operand;
1535 stmt_vec_info def_stmt_info;
1537 /* Do nothing; can reuse same def. */
1538 if (dt == vect_external_def || dt == vect_constant_def )
1539 return vec_oprnd;
1541 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1542 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1543 gcc_assert (def_stmt_info);
1544 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1545 gcc_assert (vec_stmt_for_operand);
1546 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1547 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1548 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1549 else
1550 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1551 return vec_oprnd;
1555 /* Get vectorized definitions for the operands to create a copy of an original
1556 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1558 static void
1559 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1560 vec<tree> *vec_oprnds0,
1561 vec<tree> *vec_oprnds1)
1563 tree vec_oprnd = vec_oprnds0->pop ();
1565 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1566 vec_oprnds0->quick_push (vec_oprnd);
1568 if (vec_oprnds1 && vec_oprnds1->length ())
1570 vec_oprnd = vec_oprnds1->pop ();
1571 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1572 vec_oprnds1->quick_push (vec_oprnd);
1577 /* Get vectorized definitions for OP0 and OP1.
1578 REDUC_INDEX is the index of reduction operand in case of reduction,
1579 and -1 otherwise. */
1581 void
1582 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
1583 vec<tree> *vec_oprnds0,
1584 vec<tree> *vec_oprnds1,
1585 slp_tree slp_node, int reduc_index)
1587 if (slp_node)
1589 int nops = (op1 == NULL_TREE) ? 1 : 2;
1590 auto_vec<tree> ops (nops);
1591 auto_vec<vec<tree> > vec_defs (nops);
1593 ops.quick_push (op0);
1594 if (op1)
1595 ops.quick_push (op1);
1597 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1599 *vec_oprnds0 = vec_defs[0];
1600 if (op1)
1601 *vec_oprnds1 = vec_defs[1];
1603 else
1605 tree vec_oprnd;
1607 vec_oprnds0->create (1);
1608 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);
1609 vec_oprnds0->quick_push (vec_oprnd);
1611 if (op1)
1613 vec_oprnds1->create (1);
1614 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);
1615 vec_oprnds1->quick_push (vec_oprnd);
1621 /* Function vect_finish_stmt_generation.
1623 Insert a new stmt. */
1625 void
1626 vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
1627 gimple_stmt_iterator *gsi)
1629 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1630 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1631 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1633 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1635 if (!gsi_end_p (*gsi)
1636 && gimple_has_mem_ops (vec_stmt))
1638 gimple at_stmt = gsi_stmt (*gsi);
1639 tree vuse = gimple_vuse (at_stmt);
1640 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1642 tree vdef = gimple_vdef (at_stmt);
1643 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1644 /* If we have an SSA vuse and insert a store, update virtual
1645 SSA form to avoid triggering the renamer. Do so only
1646 if we can easily see all uses - which is what almost always
1647 happens with the way vectorized stmts are inserted. */
1648 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1649 && ((is_gimple_assign (vec_stmt)
1650 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1651 || (is_gimple_call (vec_stmt)
1652 && !(gimple_call_flags (vec_stmt)
1653 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1655 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1656 gimple_set_vdef (vec_stmt, new_vdef);
1657 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1661 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1663 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo,
1664 bb_vinfo));
1666 if (dump_enabled_p ())
1668 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1669 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1670 dump_printf (MSG_NOTE, "\n");
1673 gimple_set_location (vec_stmt, gimple_location (stmt));
1676 /* Checks if CALL can be vectorized in type VECTYPE. Returns
1677 a function declaration if the target has a vectorized version
1678 of the function, or NULL_TREE if the function cannot be vectorized. */
1680 tree
1681 vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
1683 tree fndecl = gimple_call_fndecl (call);
1685 /* We only handle functions that do not read or clobber memory -- i.e.
1686 const or novops ones. */
1687 if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
1688 return NULL_TREE;
1690 if (!fndecl
1691 || TREE_CODE (fndecl) != FUNCTION_DECL
1692 || !DECL_BUILT_IN (fndecl))
1693 return NULL_TREE;
1695 return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
1696 vectype_in);
1699 /* Function vectorizable_call.
1701 Check if STMT performs a function call that can be vectorized.
1702 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1703 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
1704 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1706 static bool
1707 vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
1708 slp_tree slp_node)
1710 tree vec_dest;
1711 tree scalar_dest;
1712 tree op, type;
1713 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
1714 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
1715 tree vectype_out, vectype_in;
1716 int nunits_in;
1717 int nunits_out;
1718 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1719 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1720 tree fndecl, new_temp, def, rhs_type;
1721 gimple def_stmt;
1722 enum vect_def_type dt[3]
1723 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
1724 gimple new_stmt = NULL;
1725 int ncopies, j;
1726 vec<tree> vargs = vNULL;
1727 enum { NARROW, NONE, WIDEN } modifier;
1728 size_t i, nargs;
1729 tree lhs;
1731 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
1732 return false;
1734 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
1735 return false;
1737 /* Is STMT a vectorizable call? */
1738 if (!is_gimple_call (stmt))
1739 return false;
1741 if (gimple_call_lhs (stmt) == NULL_TREE
1742 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
1743 return false;
1745 gcc_checking_assert (!stmt_can_throw_internal (stmt));
1747 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
1749 /* Process function arguments. */
1750 rhs_type = NULL_TREE;
1751 vectype_in = NULL_TREE;
1752 nargs = gimple_call_num_args (stmt);
1754 /* Bail out if the function has more than three arguments, we do not have
1755 interesting builtin functions to vectorize with more than two arguments
1756 except for fma. No arguments is also not good. */
1757 if (nargs == 0 || nargs > 3)
1758 return false;
1760 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
1761 if (gimple_call_internal_p (stmt)
1762 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
1764 nargs = 0;
1765 rhs_type = unsigned_type_node;
1768 for (i = 0; i < nargs; i++)
1770 tree opvectype;
1772 op = gimple_call_arg (stmt, i);
1774 /* We can only handle calls with arguments of the same type. */
1775 if (rhs_type
1776 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
1778 if (dump_enabled_p ())
1779 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1780 "argument types differ.\n");
1781 return false;
1783 if (!rhs_type)
1784 rhs_type = TREE_TYPE (op);
1786 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
1787 &def_stmt, &def, &dt[i], &opvectype))
1789 if (dump_enabled_p ())
1790 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1791 "use not simple.\n");
1792 return false;
1795 if (!vectype_in)
1796 vectype_in = opvectype;
1797 else if (opvectype
1798 && opvectype != vectype_in)
1800 if (dump_enabled_p ())
1801 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1802 "argument vector types differ.\n");
1803 return false;
1806 /* If all arguments are external or constant defs use a vector type with
1807 the same size as the output vector type. */
1808 if (!vectype_in)
1809 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
1810 if (vec_stmt)
1811 gcc_assert (vectype_in);
1812 if (!vectype_in)
1814 if (dump_enabled_p ())
1816 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1817 "no vectype for scalar type ");
1818 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
1819 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1822 return false;
1825 /* FORNOW */
1826 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
1827 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
1828 if (nunits_in == nunits_out / 2)
1829 modifier = NARROW;
1830 else if (nunits_out == nunits_in)
1831 modifier = NONE;
1832 else if (nunits_out == nunits_in / 2)
1833 modifier = WIDEN;
1834 else
1835 return false;
1837 /* For now, we only vectorize functions if a target specific builtin
1838 is available. TODO -- in some cases, it might be profitable to
1839 insert the calls for pieces of the vector, in order to be able
1840 to vectorize other operations in the loop. */
1841 fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
1842 if (fndecl == NULL_TREE)
1844 if (gimple_call_internal_p (stmt)
1845 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
1846 && !slp_node
1847 && loop_vinfo
1848 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
1849 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
1850 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
1851 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
1853 /* We can handle IFN_GOMP_SIMD_LANE by returning a
1854 { 0, 1, 2, ... vf - 1 } vector. */
1855 gcc_assert (nargs == 0);
1857 else
1859 if (dump_enabled_p ())
1860 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1861 "function is not vectorizable.\n");
1862 return false;
1866 gcc_assert (!gimple_vuse (stmt));
1868 if (slp_node || PURE_SLP_STMT (stmt_info))
1869 ncopies = 1;
1870 else if (modifier == NARROW)
1871 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
1872 else
1873 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
1875 /* Sanity check: make sure that at least one copy of the vectorized stmt
1876 needs to be generated. */
1877 gcc_assert (ncopies >= 1);
1879 if (!vec_stmt) /* transformation not required. */
1881 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
1882 if (dump_enabled_p ())
1883 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
1884 "\n");
1885 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
1886 return true;
1889 /** Transform. **/
1891 if (dump_enabled_p ())
1892 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
1894 /* Handle def. */
1895 scalar_dest = gimple_call_lhs (stmt);
1896 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
1898 prev_stmt_info = NULL;
1899 switch (modifier)
1901 case NONE:
1902 for (j = 0; j < ncopies; ++j)
1904 /* Build argument list for the vectorized call. */
1905 if (j == 0)
1906 vargs.create (nargs);
1907 else
1908 vargs.truncate (0);
1910 if (slp_node)
1912 auto_vec<vec<tree> > vec_defs (nargs);
1913 vec<tree> vec_oprnds0;
1915 for (i = 0; i < nargs; i++)
1916 vargs.quick_push (gimple_call_arg (stmt, i));
1917 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
1918 vec_oprnds0 = vec_defs[0];
1920 /* Arguments are ready. Create the new vector stmt. */
1921 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
1923 size_t k;
1924 for (k = 0; k < nargs; k++)
1926 vec<tree> vec_oprndsk = vec_defs[k];
1927 vargs[k] = vec_oprndsk[i];
1929 new_stmt = gimple_build_call_vec (fndecl, vargs);
1930 new_temp = make_ssa_name (vec_dest, new_stmt);
1931 gimple_call_set_lhs (new_stmt, new_temp);
1932 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1933 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
1936 for (i = 0; i < nargs; i++)
1938 vec<tree> vec_oprndsi = vec_defs[i];
1939 vec_oprndsi.release ();
1941 continue;
1944 for (i = 0; i < nargs; i++)
1946 op = gimple_call_arg (stmt, i);
1947 if (j == 0)
1948 vec_oprnd0
1949 = vect_get_vec_def_for_operand (op, stmt, NULL);
1950 else
1952 vec_oprnd0 = gimple_call_arg (new_stmt, i);
1953 vec_oprnd0
1954 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
1957 vargs.quick_push (vec_oprnd0);
1960 if (gimple_call_internal_p (stmt)
1961 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
1963 tree *v = XALLOCAVEC (tree, nunits_out);
1964 int k;
1965 for (k = 0; k < nunits_out; ++k)
1966 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
1967 tree cst = build_vector (vectype_out, v);
1968 tree new_var
1969 = vect_get_new_vect_var (vectype_out, vect_simple_var, "cst_");
1970 gimple init_stmt = gimple_build_assign (new_var, cst);
1971 new_temp = make_ssa_name (new_var, init_stmt);
1972 gimple_assign_set_lhs (init_stmt, new_temp);
1973 vect_init_vector_1 (stmt, init_stmt, NULL);
1974 new_temp = make_ssa_name (vec_dest, NULL);
1975 new_stmt = gimple_build_assign (new_temp,
1976 gimple_assign_lhs (init_stmt));
1978 else
1980 new_stmt = gimple_build_call_vec (fndecl, vargs);
1981 new_temp = make_ssa_name (vec_dest, new_stmt);
1982 gimple_call_set_lhs (new_stmt, new_temp);
1984 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1986 if (j == 0)
1987 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
1988 else
1989 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
1991 prev_stmt_info = vinfo_for_stmt (new_stmt);
1994 break;
1996 case NARROW:
1997 for (j = 0; j < ncopies; ++j)
1999 /* Build argument list for the vectorized call. */
2000 if (j == 0)
2001 vargs.create (nargs * 2);
2002 else
2003 vargs.truncate (0);
2005 if (slp_node)
2007 auto_vec<vec<tree> > vec_defs (nargs);
2008 vec<tree> vec_oprnds0;
2010 for (i = 0; i < nargs; i++)
2011 vargs.quick_push (gimple_call_arg (stmt, i));
2012 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2013 vec_oprnds0 = vec_defs[0];
2015 /* Arguments are ready. Create the new vector stmt. */
2016 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2018 size_t k;
2019 vargs.truncate (0);
2020 for (k = 0; k < nargs; k++)
2022 vec<tree> vec_oprndsk = vec_defs[k];
2023 vargs.quick_push (vec_oprndsk[i]);
2024 vargs.quick_push (vec_oprndsk[i + 1]);
2026 new_stmt = gimple_build_call_vec (fndecl, vargs);
2027 new_temp = make_ssa_name (vec_dest, new_stmt);
2028 gimple_call_set_lhs (new_stmt, new_temp);
2029 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2030 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2033 for (i = 0; i < nargs; i++)
2035 vec<tree> vec_oprndsi = vec_defs[i];
2036 vec_oprndsi.release ();
2038 continue;
2041 for (i = 0; i < nargs; i++)
2043 op = gimple_call_arg (stmt, i);
2044 if (j == 0)
2046 vec_oprnd0
2047 = vect_get_vec_def_for_operand (op, stmt, NULL);
2048 vec_oprnd1
2049 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2051 else
2053 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2054 vec_oprnd0
2055 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2056 vec_oprnd1
2057 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2060 vargs.quick_push (vec_oprnd0);
2061 vargs.quick_push (vec_oprnd1);
2064 new_stmt = gimple_build_call_vec (fndecl, vargs);
2065 new_temp = make_ssa_name (vec_dest, new_stmt);
2066 gimple_call_set_lhs (new_stmt, new_temp);
2067 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2069 if (j == 0)
2070 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2071 else
2072 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2074 prev_stmt_info = vinfo_for_stmt (new_stmt);
2077 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2079 break;
2081 case WIDEN:
2082 /* No current target implements this case. */
2083 return false;
2086 vargs.release ();
2088 /* The call in STMT might prevent it from being removed in dce.
2089 We however cannot remove it here, due to the way the ssa name
2090 it defines is mapped to the new definition. So just replace
2091 rhs of the statement with something harmless. */
2093 if (slp_node)
2094 return true;
2096 type = TREE_TYPE (scalar_dest);
2097 if (is_pattern_stmt_p (stmt_info))
2098 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2099 else
2100 lhs = gimple_call_lhs (stmt);
2101 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2102 set_vinfo_for_stmt (new_stmt, stmt_info);
2103 set_vinfo_for_stmt (stmt, NULL);
2104 STMT_VINFO_STMT (stmt_info) = new_stmt;
2105 gsi_replace (gsi, new_stmt, false);
2107 return true;
2111 struct simd_call_arg_info
2113 tree vectype;
2114 tree op;
2115 enum vect_def_type dt;
2116 HOST_WIDE_INT linear_step;
2117 unsigned int align;
2120 /* Function vectorizable_simd_clone_call.
2122 Check if STMT performs a function call that can be vectorized
2123 by calling a simd clone of the function.
2124 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2125 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2126 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2128 static bool
2129 vectorizable_simd_clone_call (gimple stmt, gimple_stmt_iterator *gsi,
2130 gimple *vec_stmt, slp_tree slp_node)
2132 tree vec_dest;
2133 tree scalar_dest;
2134 tree op, type;
2135 tree vec_oprnd0 = NULL_TREE;
2136 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
2137 tree vectype;
2138 unsigned int nunits;
2139 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2140 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2141 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2142 tree fndecl, new_temp, def;
2143 gimple def_stmt;
2144 gimple new_stmt = NULL;
2145 int ncopies, j;
2146 vec<simd_call_arg_info> arginfo = vNULL;
2147 vec<tree> vargs = vNULL;
2148 size_t i, nargs;
2149 tree lhs, rtype, ratype;
2150 vec<constructor_elt, va_gc> *ret_ctor_elts;
2152 /* Is STMT a vectorizable call? */
2153 if (!is_gimple_call (stmt))
2154 return false;
2156 fndecl = gimple_call_fndecl (stmt);
2157 if (fndecl == NULL_TREE)
2158 return false;
2160 struct cgraph_node *node = cgraph_get_node (fndecl);
2161 if (node == NULL || node->simd_clones == NULL)
2162 return false;
2164 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2165 return false;
2167 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2168 return false;
2170 if (gimple_call_lhs (stmt)
2171 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2172 return false;
2174 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2176 vectype = STMT_VINFO_VECTYPE (stmt_info);
2178 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
2179 return false;
2181 /* FORNOW */
2182 if (slp_node || PURE_SLP_STMT (stmt_info))
2183 return false;
2185 /* Process function arguments. */
2186 nargs = gimple_call_num_args (stmt);
2188 /* Bail out if the function has zero arguments. */
2189 if (nargs == 0)
2190 return false;
2192 arginfo.create (nargs);
2194 for (i = 0; i < nargs; i++)
2196 simd_call_arg_info thisarginfo;
2197 affine_iv iv;
2199 thisarginfo.linear_step = 0;
2200 thisarginfo.align = 0;
2201 thisarginfo.op = NULL_TREE;
2203 op = gimple_call_arg (stmt, i);
2204 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2205 &def_stmt, &def, &thisarginfo.dt,
2206 &thisarginfo.vectype)
2207 || thisarginfo.dt == vect_uninitialized_def)
2209 if (dump_enabled_p ())
2210 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2211 "use not simple.\n");
2212 arginfo.release ();
2213 return false;
2216 if (thisarginfo.dt == vect_constant_def
2217 || thisarginfo.dt == vect_external_def)
2218 gcc_assert (thisarginfo.vectype == NULL_TREE);
2219 else
2220 gcc_assert (thisarginfo.vectype != NULL_TREE);
2222 if (thisarginfo.dt != vect_constant_def
2223 && thisarginfo.dt != vect_external_def
2224 && loop_vinfo
2225 && TREE_CODE (op) == SSA_NAME
2226 && simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false)
2227 && tree_fits_shwi_p (iv.step))
2229 thisarginfo.linear_step = tree_to_shwi (iv.step);
2230 thisarginfo.op = iv.base;
2232 else if ((thisarginfo.dt == vect_constant_def
2233 || thisarginfo.dt == vect_external_def)
2234 && POINTER_TYPE_P (TREE_TYPE (op)))
2235 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
2237 arginfo.quick_push (thisarginfo);
2240 unsigned int badness = 0;
2241 struct cgraph_node *bestn = NULL;
2242 if (STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info))
2243 bestn = cgraph_get_node (STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info));
2244 else
2245 for (struct cgraph_node *n = node->simd_clones; n != NULL;
2246 n = n->simdclone->next_clone)
2248 unsigned int this_badness = 0;
2249 if (n->simdclone->simdlen
2250 > (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2251 || n->simdclone->nargs != nargs)
2252 continue;
2253 if (n->simdclone->simdlen
2254 < (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2255 this_badness += (exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2256 - exact_log2 (n->simdclone->simdlen)) * 1024;
2257 if (n->simdclone->inbranch)
2258 this_badness += 2048;
2259 int target_badness = targetm.simd_clone.usable (n);
2260 if (target_badness < 0)
2261 continue;
2262 this_badness += target_badness * 512;
2263 /* FORNOW: Have to add code to add the mask argument. */
2264 if (n->simdclone->inbranch)
2265 continue;
2266 for (i = 0; i < nargs; i++)
2268 switch (n->simdclone->args[i].arg_type)
2270 case SIMD_CLONE_ARG_TYPE_VECTOR:
2271 if (!useless_type_conversion_p
2272 (n->simdclone->args[i].orig_type,
2273 TREE_TYPE (gimple_call_arg (stmt, i))))
2274 i = -1;
2275 else if (arginfo[i].dt == vect_constant_def
2276 || arginfo[i].dt == vect_external_def
2277 || arginfo[i].linear_step)
2278 this_badness += 64;
2279 break;
2280 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2281 if (arginfo[i].dt != vect_constant_def
2282 && arginfo[i].dt != vect_external_def)
2283 i = -1;
2284 break;
2285 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2286 if (arginfo[i].dt == vect_constant_def
2287 || arginfo[i].dt == vect_external_def
2288 || (arginfo[i].linear_step
2289 != n->simdclone->args[i].linear_step))
2290 i = -1;
2291 break;
2292 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2293 /* FORNOW */
2294 i = -1;
2295 break;
2296 case SIMD_CLONE_ARG_TYPE_MASK:
2297 gcc_unreachable ();
2299 if (i == (size_t) -1)
2300 break;
2301 if (n->simdclone->args[i].alignment > arginfo[i].align)
2303 i = -1;
2304 break;
2306 if (arginfo[i].align)
2307 this_badness += (exact_log2 (arginfo[i].align)
2308 - exact_log2 (n->simdclone->args[i].alignment));
2310 if (i == (size_t) -1)
2311 continue;
2312 if (bestn == NULL || this_badness < badness)
2314 bestn = n;
2315 badness = this_badness;
2319 if (bestn == NULL)
2321 arginfo.release ();
2322 return false;
2325 for (i = 0; i < nargs; i++)
2326 if ((arginfo[i].dt == vect_constant_def
2327 || arginfo[i].dt == vect_external_def)
2328 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
2330 arginfo[i].vectype
2331 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
2332 i)));
2333 if (arginfo[i].vectype == NULL
2334 || (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2335 > bestn->simdclone->simdlen))
2337 arginfo.release ();
2338 return false;
2342 fndecl = bestn->decl;
2343 nunits = bestn->simdclone->simdlen;
2344 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2346 /* If the function isn't const, only allow it in simd loops where user
2347 has asserted that at least nunits consecutive iterations can be
2348 performed using SIMD instructions. */
2349 if ((loop == NULL || (unsigned) loop->safelen < nunits)
2350 && gimple_vuse (stmt))
2352 arginfo.release ();
2353 return false;
2356 /* Sanity check: make sure that at least one copy of the vectorized stmt
2357 needs to be generated. */
2358 gcc_assert (ncopies >= 1);
2360 if (!vec_stmt) /* transformation not required. */
2362 STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info) = bestn->decl;
2363 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
2364 if (dump_enabled_p ())
2365 dump_printf_loc (MSG_NOTE, vect_location,
2366 "=== vectorizable_simd_clone_call ===\n");
2367 /* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
2368 arginfo.release ();
2369 return true;
2372 /** Transform. **/
2374 if (dump_enabled_p ())
2375 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2377 /* Handle def. */
2378 scalar_dest = gimple_call_lhs (stmt);
2379 vec_dest = NULL_TREE;
2380 rtype = NULL_TREE;
2381 ratype = NULL_TREE;
2382 if (scalar_dest)
2384 vec_dest = vect_create_destination_var (scalar_dest, vectype);
2385 rtype = TREE_TYPE (TREE_TYPE (fndecl));
2386 if (TREE_CODE (rtype) == ARRAY_TYPE)
2388 ratype = rtype;
2389 rtype = TREE_TYPE (ratype);
2393 prev_stmt_info = NULL;
2394 for (j = 0; j < ncopies; ++j)
2396 /* Build argument list for the vectorized call. */
2397 if (j == 0)
2398 vargs.create (nargs);
2399 else
2400 vargs.truncate (0);
2402 for (i = 0; i < nargs; i++)
2404 unsigned int k, l, m, o;
2405 tree atype;
2406 op = gimple_call_arg (stmt, i);
2407 switch (bestn->simdclone->args[i].arg_type)
2409 case SIMD_CLONE_ARG_TYPE_VECTOR:
2410 atype = bestn->simdclone->args[i].vector_type;
2411 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
2412 for (m = j * o; m < (j + 1) * o; m++)
2414 if (TYPE_VECTOR_SUBPARTS (atype)
2415 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
2417 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
2418 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2419 / TYPE_VECTOR_SUBPARTS (atype));
2420 gcc_assert ((k & (k - 1)) == 0);
2421 if (m == 0)
2422 vec_oprnd0
2423 = vect_get_vec_def_for_operand (op, stmt, NULL);
2424 else
2426 vec_oprnd0 = arginfo[i].op;
2427 if ((m & (k - 1)) == 0)
2428 vec_oprnd0
2429 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2430 vec_oprnd0);
2432 arginfo[i].op = vec_oprnd0;
2433 vec_oprnd0
2434 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
2435 size_int (prec),
2436 bitsize_int ((m & (k - 1)) * prec));
2437 new_stmt
2438 = gimple_build_assign (make_ssa_name (atype, NULL),
2439 vec_oprnd0);
2440 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2441 vargs.safe_push (gimple_assign_lhs (new_stmt));
2443 else
2445 k = (TYPE_VECTOR_SUBPARTS (atype)
2446 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
2447 gcc_assert ((k & (k - 1)) == 0);
2448 vec<constructor_elt, va_gc> *ctor_elts;
2449 if (k != 1)
2450 vec_alloc (ctor_elts, k);
2451 else
2452 ctor_elts = NULL;
2453 for (l = 0; l < k; l++)
2455 if (m == 0 && l == 0)
2456 vec_oprnd0
2457 = vect_get_vec_def_for_operand (op, stmt, NULL);
2458 else
2459 vec_oprnd0
2460 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2461 arginfo[i].op);
2462 arginfo[i].op = vec_oprnd0;
2463 if (k == 1)
2464 break;
2465 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
2466 vec_oprnd0);
2468 if (k == 1)
2469 vargs.safe_push (vec_oprnd0);
2470 else
2472 vec_oprnd0 = build_constructor (atype, ctor_elts);
2473 new_stmt
2474 = gimple_build_assign (make_ssa_name (atype, NULL),
2475 vec_oprnd0);
2476 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2477 vargs.safe_push (gimple_assign_lhs (new_stmt));
2481 break;
2482 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2483 vargs.safe_push (op);
2484 break;
2485 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2486 if (j == 0)
2488 gimple_seq stmts;
2489 arginfo[i].op
2490 = force_gimple_operand (arginfo[i].op, &stmts, true,
2491 NULL_TREE);
2492 if (stmts != NULL)
2494 basic_block new_bb;
2495 edge pe = loop_preheader_edge (loop);
2496 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
2497 gcc_assert (!new_bb);
2499 tree phi_res = copy_ssa_name (op, NULL);
2500 gimple new_phi = create_phi_node (phi_res, loop->header);
2501 set_vinfo_for_stmt (new_phi,
2502 new_stmt_vec_info (new_phi, loop_vinfo,
2503 NULL));
2504 add_phi_arg (new_phi, arginfo[i].op,
2505 loop_preheader_edge (loop), UNKNOWN_LOCATION);
2506 enum tree_code code
2507 = POINTER_TYPE_P (TREE_TYPE (op))
2508 ? POINTER_PLUS_EXPR : PLUS_EXPR;
2509 tree type = POINTER_TYPE_P (TREE_TYPE (op))
2510 ? sizetype : TREE_TYPE (op);
2511 double_int cst
2512 = double_int::from_shwi
2513 (bestn->simdclone->args[i].linear_step);
2514 cst *= double_int::from_uhwi (ncopies * nunits);
2515 tree tcst = double_int_to_tree (type, cst);
2516 tree phi_arg = copy_ssa_name (op, NULL);
2517 new_stmt = gimple_build_assign_with_ops (code, phi_arg,
2518 phi_res, tcst);
2519 gimple_stmt_iterator si = gsi_after_labels (loop->header);
2520 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
2521 set_vinfo_for_stmt (new_stmt,
2522 new_stmt_vec_info (new_stmt, loop_vinfo,
2523 NULL));
2524 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
2525 UNKNOWN_LOCATION);
2526 arginfo[i].op = phi_res;
2527 vargs.safe_push (phi_res);
2529 else
2531 enum tree_code code
2532 = POINTER_TYPE_P (TREE_TYPE (op))
2533 ? POINTER_PLUS_EXPR : PLUS_EXPR;
2534 tree type = POINTER_TYPE_P (TREE_TYPE (op))
2535 ? sizetype : TREE_TYPE (op);
2536 double_int cst
2537 = double_int::from_shwi
2538 (bestn->simdclone->args[i].linear_step);
2539 cst *= double_int::from_uhwi (j * nunits);
2540 tree tcst = double_int_to_tree (type, cst);
2541 new_temp = make_ssa_name (TREE_TYPE (op), NULL);
2542 new_stmt
2543 = gimple_build_assign_with_ops (code, new_temp,
2544 arginfo[i].op, tcst);
2545 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2546 vargs.safe_push (new_temp);
2548 break;
2549 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2550 default:
2551 gcc_unreachable ();
2555 new_stmt = gimple_build_call_vec (fndecl, vargs);
2556 if (vec_dest)
2558 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
2559 if (ratype)
2560 new_temp = create_tmp_var (ratype, NULL);
2561 else if (TYPE_VECTOR_SUBPARTS (vectype)
2562 == TYPE_VECTOR_SUBPARTS (rtype))
2563 new_temp = make_ssa_name (vec_dest, new_stmt);
2564 else
2565 new_temp = make_ssa_name (rtype, new_stmt);
2566 gimple_call_set_lhs (new_stmt, new_temp);
2568 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2570 if (vec_dest)
2572 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
2574 unsigned int k, l;
2575 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
2576 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
2577 gcc_assert ((k & (k - 1)) == 0);
2578 for (l = 0; l < k; l++)
2580 tree t;
2581 if (ratype)
2583 t = build_fold_addr_expr (new_temp);
2584 t = build2 (MEM_REF, vectype, t,
2585 build_int_cst (TREE_TYPE (t),
2586 l * prec / BITS_PER_UNIT));
2588 else
2589 t = build3 (BIT_FIELD_REF, vectype, new_temp,
2590 size_int (prec), bitsize_int (l * prec));
2591 new_stmt
2592 = gimple_build_assign (make_ssa_name (vectype, NULL), t);
2593 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2594 if (j == 0 && l == 0)
2595 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2596 else
2597 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2599 prev_stmt_info = vinfo_for_stmt (new_stmt);
2602 if (ratype)
2604 tree clobber = build_constructor (ratype, NULL);
2605 TREE_THIS_VOLATILE (clobber) = 1;
2606 new_stmt = gimple_build_assign (new_temp, clobber);
2607 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2609 continue;
2611 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
2613 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
2614 / TYPE_VECTOR_SUBPARTS (rtype));
2615 gcc_assert ((k & (k - 1)) == 0);
2616 if ((j & (k - 1)) == 0)
2617 vec_alloc (ret_ctor_elts, k);
2618 if (ratype)
2620 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
2621 for (m = 0; m < o; m++)
2623 tree tem = build4 (ARRAY_REF, rtype, new_temp,
2624 size_int (m), NULL_TREE, NULL_TREE);
2625 new_stmt
2626 = gimple_build_assign (make_ssa_name (rtype, NULL),
2627 tem);
2628 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2629 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
2630 gimple_assign_lhs (new_stmt));
2632 tree clobber = build_constructor (ratype, NULL);
2633 TREE_THIS_VOLATILE (clobber) = 1;
2634 new_stmt = gimple_build_assign (new_temp, clobber);
2635 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2637 else
2638 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
2639 if ((j & (k - 1)) != k - 1)
2640 continue;
2641 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
2642 new_stmt
2643 = gimple_build_assign (make_ssa_name (vec_dest, NULL),
2644 vec_oprnd0);
2645 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2647 if ((unsigned) j == k - 1)
2648 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2649 else
2650 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2652 prev_stmt_info = vinfo_for_stmt (new_stmt);
2653 continue;
2655 else if (ratype)
2657 tree t = build_fold_addr_expr (new_temp);
2658 t = build2 (MEM_REF, vectype, t,
2659 build_int_cst (TREE_TYPE (t), 0));
2660 new_stmt
2661 = gimple_build_assign (make_ssa_name (vec_dest, NULL), t);
2662 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2663 tree clobber = build_constructor (ratype, NULL);
2664 TREE_THIS_VOLATILE (clobber) = 1;
2665 vect_finish_stmt_generation (stmt,
2666 gimple_build_assign (new_temp,
2667 clobber), gsi);
2671 if (j == 0)
2672 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2673 else
2674 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2676 prev_stmt_info = vinfo_for_stmt (new_stmt);
2679 vargs.release ();
2681 /* The call in STMT might prevent it from being removed in dce.
2682 We however cannot remove it here, due to the way the ssa name
2683 it defines is mapped to the new definition. So just replace
2684 rhs of the statement with something harmless. */
2686 if (slp_node)
2687 return true;
2689 if (scalar_dest)
2691 type = TREE_TYPE (scalar_dest);
2692 if (is_pattern_stmt_p (stmt_info))
2693 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2694 else
2695 lhs = gimple_call_lhs (stmt);
2696 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2698 else
2699 new_stmt = gimple_build_nop ();
2700 set_vinfo_for_stmt (new_stmt, stmt_info);
2701 set_vinfo_for_stmt (stmt, NULL);
2702 STMT_VINFO_STMT (stmt_info) = new_stmt;
2703 gsi_replace (gsi, new_stmt, false);
2704 unlink_stmt_vdef (stmt);
2706 return true;
2710 /* Function vect_gen_widened_results_half
2712 Create a vector stmt whose code, type, number of arguments, and result
2713 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
2714 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
2715 In the case that CODE is a CALL_EXPR, this means that a call to DECL
2716 needs to be created (DECL is a function-decl of a target-builtin).
2717 STMT is the original scalar stmt that we are vectorizing. */
2719 static gimple
2720 vect_gen_widened_results_half (enum tree_code code,
2721 tree decl,
2722 tree vec_oprnd0, tree vec_oprnd1, int op_type,
2723 tree vec_dest, gimple_stmt_iterator *gsi,
2724 gimple stmt)
2726 gimple new_stmt;
2727 tree new_temp;
2729 /* Generate half of the widened result: */
2730 if (code == CALL_EXPR)
2732 /* Target specific support */
2733 if (op_type == binary_op)
2734 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
2735 else
2736 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
2737 new_temp = make_ssa_name (vec_dest, new_stmt);
2738 gimple_call_set_lhs (new_stmt, new_temp);
2740 else
2742 /* Generic support */
2743 gcc_assert (op_type == TREE_CODE_LENGTH (code));
2744 if (op_type != binary_op)
2745 vec_oprnd1 = NULL;
2746 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vec_oprnd0,
2747 vec_oprnd1);
2748 new_temp = make_ssa_name (vec_dest, new_stmt);
2749 gimple_assign_set_lhs (new_stmt, new_temp);
2751 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2753 return new_stmt;
2757 /* Get vectorized definitions for loop-based vectorization. For the first
2758 operand we call vect_get_vec_def_for_operand() (with OPRND containing
2759 scalar operand), and for the rest we get a copy with
2760 vect_get_vec_def_for_stmt_copy() using the previous vector definition
2761 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
2762 The vectors are collected into VEC_OPRNDS. */
2764 static void
2765 vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
2766 vec<tree> *vec_oprnds, int multi_step_cvt)
2768 tree vec_oprnd;
2770 /* Get first vector operand. */
2771 /* All the vector operands except the very first one (that is scalar oprnd)
2772 are stmt copies. */
2773 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
2774 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
2775 else
2776 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
2778 vec_oprnds->quick_push (vec_oprnd);
2780 /* Get second vector operand. */
2781 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
2782 vec_oprnds->quick_push (vec_oprnd);
2784 *oprnd = vec_oprnd;
2786 /* For conversion in multiple steps, continue to get operands
2787 recursively. */
2788 if (multi_step_cvt)
2789 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
2793 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
2794 For multi-step conversions store the resulting vectors and call the function
2795 recursively. */
2797 static void
2798 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
2799 int multi_step_cvt, gimple stmt,
2800 vec<tree> vec_dsts,
2801 gimple_stmt_iterator *gsi,
2802 slp_tree slp_node, enum tree_code code,
2803 stmt_vec_info *prev_stmt_info)
2805 unsigned int i;
2806 tree vop0, vop1, new_tmp, vec_dest;
2807 gimple new_stmt;
2808 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2810 vec_dest = vec_dsts.pop ();
2812 for (i = 0; i < vec_oprnds->length (); i += 2)
2814 /* Create demotion operation. */
2815 vop0 = (*vec_oprnds)[i];
2816 vop1 = (*vec_oprnds)[i + 1];
2817 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
2818 new_tmp = make_ssa_name (vec_dest, new_stmt);
2819 gimple_assign_set_lhs (new_stmt, new_tmp);
2820 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2822 if (multi_step_cvt)
2823 /* Store the resulting vector for next recursive call. */
2824 (*vec_oprnds)[i/2] = new_tmp;
2825 else
2827 /* This is the last step of the conversion sequence. Store the
2828 vectors in SLP_NODE or in vector info of the scalar statement
2829 (or in STMT_VINFO_RELATED_STMT chain). */
2830 if (slp_node)
2831 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2832 else
2834 if (!*prev_stmt_info)
2835 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2836 else
2837 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
2839 *prev_stmt_info = vinfo_for_stmt (new_stmt);
2844 /* For multi-step demotion operations we first generate demotion operations
2845 from the source type to the intermediate types, and then combine the
2846 results (stored in VEC_OPRNDS) in demotion operation to the destination
2847 type. */
2848 if (multi_step_cvt)
2850 /* At each level of recursion we have half of the operands we had at the
2851 previous level. */
2852 vec_oprnds->truncate ((i+1)/2);
2853 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
2854 stmt, vec_dsts, gsi, slp_node,
2855 VEC_PACK_TRUNC_EXPR,
2856 prev_stmt_info);
2859 vec_dsts.quick_push (vec_dest);
2863 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
2864 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
2865 the resulting vectors and call the function recursively. */
2867 static void
2868 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
2869 vec<tree> *vec_oprnds1,
2870 gimple stmt, tree vec_dest,
2871 gimple_stmt_iterator *gsi,
2872 enum tree_code code1,
2873 enum tree_code code2, tree decl1,
2874 tree decl2, int op_type)
2876 int i;
2877 tree vop0, vop1, new_tmp1, new_tmp2;
2878 gimple new_stmt1, new_stmt2;
2879 vec<tree> vec_tmp = vNULL;
2881 vec_tmp.create (vec_oprnds0->length () * 2);
2882 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
2884 if (op_type == binary_op)
2885 vop1 = (*vec_oprnds1)[i];
2886 else
2887 vop1 = NULL_TREE;
2889 /* Generate the two halves of promotion operation. */
2890 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
2891 op_type, vec_dest, gsi, stmt);
2892 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
2893 op_type, vec_dest, gsi, stmt);
2894 if (is_gimple_call (new_stmt1))
2896 new_tmp1 = gimple_call_lhs (new_stmt1);
2897 new_tmp2 = gimple_call_lhs (new_stmt2);
2899 else
2901 new_tmp1 = gimple_assign_lhs (new_stmt1);
2902 new_tmp2 = gimple_assign_lhs (new_stmt2);
2905 /* Store the results for the next step. */
2906 vec_tmp.quick_push (new_tmp1);
2907 vec_tmp.quick_push (new_tmp2);
2910 vec_oprnds0->release ();
2911 *vec_oprnds0 = vec_tmp;
2915 /* Check if STMT performs a conversion operation, that can be vectorized.
2916 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2917 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
2918 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2920 static bool
2921 vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
2922 gimple *vec_stmt, slp_tree slp_node)
2924 tree vec_dest;
2925 tree scalar_dest;
2926 tree op0, op1 = NULL_TREE;
2927 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2928 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2929 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2930 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
2931 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
2932 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
2933 tree new_temp;
2934 tree def;
2935 gimple def_stmt;
2936 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
2937 gimple new_stmt = NULL;
2938 stmt_vec_info prev_stmt_info;
2939 int nunits_in;
2940 int nunits_out;
2941 tree vectype_out, vectype_in;
2942 int ncopies, i, j;
2943 tree lhs_type, rhs_type;
2944 enum { NARROW, NONE, WIDEN } modifier;
2945 vec<tree> vec_oprnds0 = vNULL;
2946 vec<tree> vec_oprnds1 = vNULL;
2947 tree vop0;
2948 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2949 int multi_step_cvt = 0;
2950 vec<tree> vec_dsts = vNULL;
2951 vec<tree> interm_types = vNULL;
2952 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
2953 int op_type;
2954 enum machine_mode rhs_mode;
2955 unsigned short fltsz;
2957 /* Is STMT a vectorizable conversion? */
2959 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2960 return false;
2962 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2963 return false;
2965 if (!is_gimple_assign (stmt))
2966 return false;
2968 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2969 return false;
2971 code = gimple_assign_rhs_code (stmt);
2972 if (!CONVERT_EXPR_CODE_P (code)
2973 && code != FIX_TRUNC_EXPR
2974 && code != FLOAT_EXPR
2975 && code != WIDEN_MULT_EXPR
2976 && code != WIDEN_LSHIFT_EXPR)
2977 return false;
2979 op_type = TREE_CODE_LENGTH (code);
2981 /* Check types of lhs and rhs. */
2982 scalar_dest = gimple_assign_lhs (stmt);
2983 lhs_type = TREE_TYPE (scalar_dest);
2984 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2986 op0 = gimple_assign_rhs1 (stmt);
2987 rhs_type = TREE_TYPE (op0);
2989 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
2990 && !((INTEGRAL_TYPE_P (lhs_type)
2991 && INTEGRAL_TYPE_P (rhs_type))
2992 || (SCALAR_FLOAT_TYPE_P (lhs_type)
2993 && SCALAR_FLOAT_TYPE_P (rhs_type))))
2994 return false;
2996 if ((INTEGRAL_TYPE_P (lhs_type)
2997 && (TYPE_PRECISION (lhs_type)
2998 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
2999 || (INTEGRAL_TYPE_P (rhs_type)
3000 && (TYPE_PRECISION (rhs_type)
3001 != GET_MODE_PRECISION (TYPE_MODE (rhs_type)))))
3003 if (dump_enabled_p ())
3004 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3005 "type conversion to/from bit-precision unsupported."
3006 "\n");
3007 return false;
3010 /* Check the operands of the operation. */
3011 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3012 &def_stmt, &def, &dt[0], &vectype_in))
3014 if (dump_enabled_p ())
3015 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3016 "use not simple.\n");
3017 return false;
3019 if (op_type == binary_op)
3021 bool ok;
3023 op1 = gimple_assign_rhs2 (stmt);
3024 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3025 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3026 OP1. */
3027 if (CONSTANT_CLASS_P (op0))
3028 ok = vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo,
3029 &def_stmt, &def, &dt[1], &vectype_in);
3030 else
3031 ok = vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3032 &def, &dt[1]);
3034 if (!ok)
3036 if (dump_enabled_p ())
3037 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3038 "use not simple.\n");
3039 return false;
3043 /* If op0 is an external or constant defs use a vector type of
3044 the same size as the output vector type. */
3045 if (!vectype_in)
3046 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
3047 if (vec_stmt)
3048 gcc_assert (vectype_in);
3049 if (!vectype_in)
3051 if (dump_enabled_p ())
3053 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3054 "no vectype for scalar type ");
3055 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3056 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3059 return false;
3062 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3063 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3064 if (nunits_in < nunits_out)
3065 modifier = NARROW;
3066 else if (nunits_out == nunits_in)
3067 modifier = NONE;
3068 else
3069 modifier = WIDEN;
3071 /* Multiple types in SLP are handled by creating the appropriate number of
3072 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3073 case of SLP. */
3074 if (slp_node || PURE_SLP_STMT (stmt_info))
3075 ncopies = 1;
3076 else if (modifier == NARROW)
3077 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3078 else
3079 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3081 /* Sanity check: make sure that at least one copy of the vectorized stmt
3082 needs to be generated. */
3083 gcc_assert (ncopies >= 1);
3085 /* Supportable by target? */
3086 switch (modifier)
3088 case NONE:
3089 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3090 return false;
3091 if (supportable_convert_operation (code, vectype_out, vectype_in,
3092 &decl1, &code1))
3093 break;
3094 /* FALLTHRU */
3095 unsupported:
3096 if (dump_enabled_p ())
3097 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3098 "conversion not supported by target.\n");
3099 return false;
3101 case WIDEN:
3102 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
3103 &code1, &code2, &multi_step_cvt,
3104 &interm_types))
3106 /* Binary widening operation can only be supported directly by the
3107 architecture. */
3108 gcc_assert (!(multi_step_cvt && op_type == binary_op));
3109 break;
3112 if (code != FLOAT_EXPR
3113 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3114 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3115 goto unsupported;
3117 rhs_mode = TYPE_MODE (rhs_type);
3118 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
3119 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
3120 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
3121 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
3123 cvt_type
3124 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3125 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3126 if (cvt_type == NULL_TREE)
3127 goto unsupported;
3129 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3131 if (!supportable_convert_operation (code, vectype_out,
3132 cvt_type, &decl1, &codecvt1))
3133 goto unsupported;
3135 else if (!supportable_widening_operation (code, stmt, vectype_out,
3136 cvt_type, &codecvt1,
3137 &codecvt2, &multi_step_cvt,
3138 &interm_types))
3139 continue;
3140 else
3141 gcc_assert (multi_step_cvt == 0);
3143 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
3144 vectype_in, &code1, &code2,
3145 &multi_step_cvt, &interm_types))
3146 break;
3149 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
3150 goto unsupported;
3152 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3153 codecvt2 = ERROR_MARK;
3154 else
3156 multi_step_cvt++;
3157 interm_types.safe_push (cvt_type);
3158 cvt_type = NULL_TREE;
3160 break;
3162 case NARROW:
3163 gcc_assert (op_type == unary_op);
3164 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
3165 &code1, &multi_step_cvt,
3166 &interm_types))
3167 break;
3169 if (code != FIX_TRUNC_EXPR
3170 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3171 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3172 goto unsupported;
3174 rhs_mode = TYPE_MODE (rhs_type);
3175 cvt_type
3176 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3177 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3178 if (cvt_type == NULL_TREE)
3179 goto unsupported;
3180 if (!supportable_convert_operation (code, cvt_type, vectype_in,
3181 &decl1, &codecvt1))
3182 goto unsupported;
3183 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
3184 &code1, &multi_step_cvt,
3185 &interm_types))
3186 break;
3187 goto unsupported;
3189 default:
3190 gcc_unreachable ();
3193 if (!vec_stmt) /* transformation not required. */
3195 if (dump_enabled_p ())
3196 dump_printf_loc (MSG_NOTE, vect_location,
3197 "=== vectorizable_conversion ===\n");
3198 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
3200 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
3201 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3203 else if (modifier == NARROW)
3205 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
3206 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3208 else
3210 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
3211 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3213 interm_types.release ();
3214 return true;
3217 /** Transform. **/
3218 if (dump_enabled_p ())
3219 dump_printf_loc (MSG_NOTE, vect_location,
3220 "transform conversion. ncopies = %d.\n", ncopies);
3222 if (op_type == binary_op)
3224 if (CONSTANT_CLASS_P (op0))
3225 op0 = fold_convert (TREE_TYPE (op1), op0);
3226 else if (CONSTANT_CLASS_P (op1))
3227 op1 = fold_convert (TREE_TYPE (op0), op1);
3230 /* In case of multi-step conversion, we first generate conversion operations
3231 to the intermediate types, and then from that types to the final one.
3232 We create vector destinations for the intermediate type (TYPES) received
3233 from supportable_*_operation, and store them in the correct order
3234 for future use in vect_create_vectorized_*_stmts (). */
3235 vec_dsts.create (multi_step_cvt + 1);
3236 vec_dest = vect_create_destination_var (scalar_dest,
3237 (cvt_type && modifier == WIDEN)
3238 ? cvt_type : vectype_out);
3239 vec_dsts.quick_push (vec_dest);
3241 if (multi_step_cvt)
3243 for (i = interm_types.length () - 1;
3244 interm_types.iterate (i, &intermediate_type); i--)
3246 vec_dest = vect_create_destination_var (scalar_dest,
3247 intermediate_type);
3248 vec_dsts.quick_push (vec_dest);
3252 if (cvt_type)
3253 vec_dest = vect_create_destination_var (scalar_dest,
3254 modifier == WIDEN
3255 ? vectype_out : cvt_type);
3257 if (!slp_node)
3259 if (modifier == WIDEN)
3261 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
3262 if (op_type == binary_op)
3263 vec_oprnds1.create (1);
3265 else if (modifier == NARROW)
3266 vec_oprnds0.create (
3267 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
3269 else if (code == WIDEN_LSHIFT_EXPR)
3270 vec_oprnds1.create (slp_node->vec_stmts_size);
3272 last_oprnd = op0;
3273 prev_stmt_info = NULL;
3274 switch (modifier)
3276 case NONE:
3277 for (j = 0; j < ncopies; j++)
3279 if (j == 0)
3280 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
3281 -1);
3282 else
3283 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3285 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3287 /* Arguments are ready, create the new vector stmt. */
3288 if (code1 == CALL_EXPR)
3290 new_stmt = gimple_build_call (decl1, 1, vop0);
3291 new_temp = make_ssa_name (vec_dest, new_stmt);
3292 gimple_call_set_lhs (new_stmt, new_temp);
3294 else
3296 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
3297 new_stmt = gimple_build_assign_with_ops (code1, vec_dest,
3298 vop0, NULL);
3299 new_temp = make_ssa_name (vec_dest, new_stmt);
3300 gimple_assign_set_lhs (new_stmt, new_temp);
3303 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3304 if (slp_node)
3305 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3308 if (j == 0)
3309 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3310 else
3311 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3312 prev_stmt_info = vinfo_for_stmt (new_stmt);
3314 break;
3316 case WIDEN:
3317 /* In case the vectorization factor (VF) is bigger than the number
3318 of elements that we can fit in a vectype (nunits), we have to
3319 generate more than one vector stmt - i.e - we need to "unroll"
3320 the vector stmt by a factor VF/nunits. */
3321 for (j = 0; j < ncopies; j++)
3323 /* Handle uses. */
3324 if (j == 0)
3326 if (slp_node)
3328 if (code == WIDEN_LSHIFT_EXPR)
3330 unsigned int k;
3332 vec_oprnd1 = op1;
3333 /* Store vec_oprnd1 for every vector stmt to be created
3334 for SLP_NODE. We check during the analysis that all
3335 the shift arguments are the same. */
3336 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
3337 vec_oprnds1.quick_push (vec_oprnd1);
3339 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3340 slp_node, -1);
3342 else
3343 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
3344 &vec_oprnds1, slp_node, -1);
3346 else
3348 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
3349 vec_oprnds0.quick_push (vec_oprnd0);
3350 if (op_type == binary_op)
3352 if (code == WIDEN_LSHIFT_EXPR)
3353 vec_oprnd1 = op1;
3354 else
3355 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt,
3356 NULL);
3357 vec_oprnds1.quick_push (vec_oprnd1);
3361 else
3363 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3364 vec_oprnds0.truncate (0);
3365 vec_oprnds0.quick_push (vec_oprnd0);
3366 if (op_type == binary_op)
3368 if (code == WIDEN_LSHIFT_EXPR)
3369 vec_oprnd1 = op1;
3370 else
3371 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
3372 vec_oprnd1);
3373 vec_oprnds1.truncate (0);
3374 vec_oprnds1.quick_push (vec_oprnd1);
3378 /* Arguments are ready. Create the new vector stmts. */
3379 for (i = multi_step_cvt; i >= 0; i--)
3381 tree this_dest = vec_dsts[i];
3382 enum tree_code c1 = code1, c2 = code2;
3383 if (i == 0 && codecvt2 != ERROR_MARK)
3385 c1 = codecvt1;
3386 c2 = codecvt2;
3388 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
3389 &vec_oprnds1,
3390 stmt, this_dest, gsi,
3391 c1, c2, decl1, decl2,
3392 op_type);
3395 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3397 if (cvt_type)
3399 if (codecvt1 == CALL_EXPR)
3401 new_stmt = gimple_build_call (decl1, 1, vop0);
3402 new_temp = make_ssa_name (vec_dest, new_stmt);
3403 gimple_call_set_lhs (new_stmt, new_temp);
3405 else
3407 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3408 new_temp = make_ssa_name (vec_dest, NULL);
3409 new_stmt = gimple_build_assign_with_ops (codecvt1,
3410 new_temp,
3411 vop0, NULL);
3414 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3416 else
3417 new_stmt = SSA_NAME_DEF_STMT (vop0);
3419 if (slp_node)
3420 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3421 else
3423 if (!prev_stmt_info)
3424 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3425 else
3426 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3427 prev_stmt_info = vinfo_for_stmt (new_stmt);
3432 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3433 break;
3435 case NARROW:
3436 /* In case the vectorization factor (VF) is bigger than the number
3437 of elements that we can fit in a vectype (nunits), we have to
3438 generate more than one vector stmt - i.e - we need to "unroll"
3439 the vector stmt by a factor VF/nunits. */
3440 for (j = 0; j < ncopies; j++)
3442 /* Handle uses. */
3443 if (slp_node)
3444 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3445 slp_node, -1);
3446 else
3448 vec_oprnds0.truncate (0);
3449 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
3450 vect_pow2 (multi_step_cvt) - 1);
3453 /* Arguments are ready. Create the new vector stmts. */
3454 if (cvt_type)
3455 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3457 if (codecvt1 == CALL_EXPR)
3459 new_stmt = gimple_build_call (decl1, 1, vop0);
3460 new_temp = make_ssa_name (vec_dest, new_stmt);
3461 gimple_call_set_lhs (new_stmt, new_temp);
3463 else
3465 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3466 new_temp = make_ssa_name (vec_dest, NULL);
3467 new_stmt = gimple_build_assign_with_ops (codecvt1, new_temp,
3468 vop0, NULL);
3471 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3472 vec_oprnds0[i] = new_temp;
3475 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
3476 stmt, vec_dsts, gsi,
3477 slp_node, code1,
3478 &prev_stmt_info);
3481 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3482 break;
3485 vec_oprnds0.release ();
3486 vec_oprnds1.release ();
3487 vec_dsts.release ();
3488 interm_types.release ();
3490 return true;
3494 /* Function vectorizable_assignment.
3496 Check if STMT performs an assignment (copy) that can be vectorized.
3497 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3498 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3499 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3501 static bool
3502 vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
3503 gimple *vec_stmt, slp_tree slp_node)
3505 tree vec_dest;
3506 tree scalar_dest;
3507 tree op;
3508 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3509 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3510 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3511 tree new_temp;
3512 tree def;
3513 gimple def_stmt;
3514 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3515 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
3516 int ncopies;
3517 int i, j;
3518 vec<tree> vec_oprnds = vNULL;
3519 tree vop;
3520 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3521 gimple new_stmt = NULL;
3522 stmt_vec_info prev_stmt_info = NULL;
3523 enum tree_code code;
3524 tree vectype_in;
3526 /* Multiple types in SLP are handled by creating the appropriate number of
3527 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3528 case of SLP. */
3529 if (slp_node || PURE_SLP_STMT (stmt_info))
3530 ncopies = 1;
3531 else
3532 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3534 gcc_assert (ncopies >= 1);
3536 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3537 return false;
3539 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3540 return false;
3542 /* Is vectorizable assignment? */
3543 if (!is_gimple_assign (stmt))
3544 return false;
3546 scalar_dest = gimple_assign_lhs (stmt);
3547 if (TREE_CODE (scalar_dest) != SSA_NAME)
3548 return false;
3550 code = gimple_assign_rhs_code (stmt);
3551 if (gimple_assign_single_p (stmt)
3552 || code == PAREN_EXPR
3553 || CONVERT_EXPR_CODE_P (code))
3554 op = gimple_assign_rhs1 (stmt);
3555 else
3556 return false;
3558 if (code == VIEW_CONVERT_EXPR)
3559 op = TREE_OPERAND (op, 0);
3561 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
3562 &def_stmt, &def, &dt[0], &vectype_in))
3564 if (dump_enabled_p ())
3565 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3566 "use not simple.\n");
3567 return false;
3570 /* We can handle NOP_EXPR conversions that do not change the number
3571 of elements or the vector size. */
3572 if ((CONVERT_EXPR_CODE_P (code)
3573 || code == VIEW_CONVERT_EXPR)
3574 && (!vectype_in
3575 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
3576 || (GET_MODE_SIZE (TYPE_MODE (vectype))
3577 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
3578 return false;
3580 /* We do not handle bit-precision changes. */
3581 if ((CONVERT_EXPR_CODE_P (code)
3582 || code == VIEW_CONVERT_EXPR)
3583 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
3584 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
3585 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
3586 || ((TYPE_PRECISION (TREE_TYPE (op))
3587 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
3588 /* But a conversion that does not change the bit-pattern is ok. */
3589 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
3590 > TYPE_PRECISION (TREE_TYPE (op)))
3591 && TYPE_UNSIGNED (TREE_TYPE (op))))
3593 if (dump_enabled_p ())
3594 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3595 "type conversion to/from bit-precision "
3596 "unsupported.\n");
3597 return false;
3600 if (!vec_stmt) /* transformation not required. */
3602 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
3603 if (dump_enabled_p ())
3604 dump_printf_loc (MSG_NOTE, vect_location,
3605 "=== vectorizable_assignment ===\n");
3606 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3607 return true;
3610 /** Transform. **/
3611 if (dump_enabled_p ())
3612 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
3614 /* Handle def. */
3615 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3617 /* Handle use. */
3618 for (j = 0; j < ncopies; j++)
3620 /* Handle uses. */
3621 if (j == 0)
3622 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
3623 else
3624 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
3626 /* Arguments are ready. create the new vector stmt. */
3627 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3629 if (CONVERT_EXPR_CODE_P (code)
3630 || code == VIEW_CONVERT_EXPR)
3631 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
3632 new_stmt = gimple_build_assign (vec_dest, vop);
3633 new_temp = make_ssa_name (vec_dest, new_stmt);
3634 gimple_assign_set_lhs (new_stmt, new_temp);
3635 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3636 if (slp_node)
3637 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3640 if (slp_node)
3641 continue;
3643 if (j == 0)
3644 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3645 else
3646 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3648 prev_stmt_info = vinfo_for_stmt (new_stmt);
3651 vec_oprnds.release ();
3652 return true;
3656 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
3657 either as shift by a scalar or by a vector. */
3659 bool
3660 vect_supportable_shift (enum tree_code code, tree scalar_type)
3663 enum machine_mode vec_mode;
3664 optab optab;
3665 int icode;
3666 tree vectype;
3668 vectype = get_vectype_for_scalar_type (scalar_type);
3669 if (!vectype)
3670 return false;
3672 optab = optab_for_tree_code (code, vectype, optab_scalar);
3673 if (!optab
3674 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
3676 optab = optab_for_tree_code (code, vectype, optab_vector);
3677 if (!optab
3678 || (optab_handler (optab, TYPE_MODE (vectype))
3679 == CODE_FOR_nothing))
3680 return false;
3683 vec_mode = TYPE_MODE (vectype);
3684 icode = (int) optab_handler (optab, vec_mode);
3685 if (icode == CODE_FOR_nothing)
3686 return false;
3688 return true;
3692 /* Function vectorizable_shift.
3694 Check if STMT performs a shift operation that can be vectorized.
3695 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3696 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3697 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3699 static bool
3700 vectorizable_shift (gimple stmt, gimple_stmt_iterator *gsi,
3701 gimple *vec_stmt, slp_tree slp_node)
3703 tree vec_dest;
3704 tree scalar_dest;
3705 tree op0, op1 = NULL;
3706 tree vec_oprnd1 = NULL_TREE;
3707 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3708 tree vectype;
3709 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3710 enum tree_code code;
3711 enum machine_mode vec_mode;
3712 tree new_temp;
3713 optab optab;
3714 int icode;
3715 enum machine_mode optab_op2_mode;
3716 tree def;
3717 gimple def_stmt;
3718 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3719 gimple new_stmt = NULL;
3720 stmt_vec_info prev_stmt_info;
3721 int nunits_in;
3722 int nunits_out;
3723 tree vectype_out;
3724 tree op1_vectype;
3725 int ncopies;
3726 int j, i;
3727 vec<tree> vec_oprnds0 = vNULL;
3728 vec<tree> vec_oprnds1 = vNULL;
3729 tree vop0, vop1;
3730 unsigned int k;
3731 bool scalar_shift_arg = true;
3732 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3733 int vf;
3735 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3736 return false;
3738 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3739 return false;
3741 /* Is STMT a vectorizable binary/unary operation? */
3742 if (!is_gimple_assign (stmt))
3743 return false;
3745 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3746 return false;
3748 code = gimple_assign_rhs_code (stmt);
3750 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
3751 || code == RROTATE_EXPR))
3752 return false;
3754 scalar_dest = gimple_assign_lhs (stmt);
3755 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3756 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
3757 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
3759 if (dump_enabled_p ())
3760 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3761 "bit-precision shifts not supported.\n");
3762 return false;
3765 op0 = gimple_assign_rhs1 (stmt);
3766 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3767 &def_stmt, &def, &dt[0], &vectype))
3769 if (dump_enabled_p ())
3770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3771 "use not simple.\n");
3772 return false;
3774 /* If op0 is an external or constant def use a vector type with
3775 the same size as the output vector type. */
3776 if (!vectype)
3777 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
3778 if (vec_stmt)
3779 gcc_assert (vectype);
3780 if (!vectype)
3782 if (dump_enabled_p ())
3783 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3784 "no vectype for scalar type\n");
3785 return false;
3788 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3789 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
3790 if (nunits_out != nunits_in)
3791 return false;
3793 op1 = gimple_assign_rhs2 (stmt);
3794 if (!vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3795 &def, &dt[1], &op1_vectype))
3797 if (dump_enabled_p ())
3798 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3799 "use not simple.\n");
3800 return false;
3803 if (loop_vinfo)
3804 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3805 else
3806 vf = 1;
3808 /* Multiple types in SLP are handled by creating the appropriate number of
3809 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3810 case of SLP. */
3811 if (slp_node || PURE_SLP_STMT (stmt_info))
3812 ncopies = 1;
3813 else
3814 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3816 gcc_assert (ncopies >= 1);
3818 /* Determine whether the shift amount is a vector, or scalar. If the
3819 shift/rotate amount is a vector, use the vector/vector shift optabs. */
3821 if (dt[1] == vect_internal_def && !slp_node)
3822 scalar_shift_arg = false;
3823 else if (dt[1] == vect_constant_def
3824 || dt[1] == vect_external_def
3825 || dt[1] == vect_internal_def)
3827 /* In SLP, need to check whether the shift count is the same,
3828 in loops if it is a constant or invariant, it is always
3829 a scalar shift. */
3830 if (slp_node)
3832 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
3833 gimple slpstmt;
3835 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
3836 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
3837 scalar_shift_arg = false;
3840 else
3842 if (dump_enabled_p ())
3843 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3844 "operand mode requires invariant argument.\n");
3845 return false;
3848 /* Vector shifted by vector. */
3849 if (!scalar_shift_arg)
3851 optab = optab_for_tree_code (code, vectype, optab_vector);
3852 if (dump_enabled_p ())
3853 dump_printf_loc (MSG_NOTE, vect_location,
3854 "vector/vector shift/rotate found.\n");
3856 if (!op1_vectype)
3857 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
3858 if (op1_vectype == NULL_TREE
3859 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
3861 if (dump_enabled_p ())
3862 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3863 "unusable type for last operand in"
3864 " vector/vector shift/rotate.\n");
3865 return false;
3868 /* See if the machine has a vector shifted by scalar insn and if not
3869 then see if it has a vector shifted by vector insn. */
3870 else
3872 optab = optab_for_tree_code (code, vectype, optab_scalar);
3873 if (optab
3874 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
3876 if (dump_enabled_p ())
3877 dump_printf_loc (MSG_NOTE, vect_location,
3878 "vector/scalar shift/rotate found.\n");
3880 else
3882 optab = optab_for_tree_code (code, vectype, optab_vector);
3883 if (optab
3884 && (optab_handler (optab, TYPE_MODE (vectype))
3885 != CODE_FOR_nothing))
3887 scalar_shift_arg = false;
3889 if (dump_enabled_p ())
3890 dump_printf_loc (MSG_NOTE, vect_location,
3891 "vector/vector shift/rotate found.\n");
3893 /* Unlike the other binary operators, shifts/rotates have
3894 the rhs being int, instead of the same type as the lhs,
3895 so make sure the scalar is the right type if we are
3896 dealing with vectors of long long/long/short/char. */
3897 if (dt[1] == vect_constant_def)
3898 op1 = fold_convert (TREE_TYPE (vectype), op1);
3899 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
3900 TREE_TYPE (op1)))
3902 if (slp_node
3903 && TYPE_MODE (TREE_TYPE (vectype))
3904 != TYPE_MODE (TREE_TYPE (op1)))
3906 if (dump_enabled_p ())
3907 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3908 "unusable type for last operand in"
3909 " vector/vector shift/rotate.\n");
3910 return false;
3912 if (vec_stmt && !slp_node)
3914 op1 = fold_convert (TREE_TYPE (vectype), op1);
3915 op1 = vect_init_vector (stmt, op1,
3916 TREE_TYPE (vectype), NULL);
3923 /* Supportable by target? */
3924 if (!optab)
3926 if (dump_enabled_p ())
3927 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3928 "no optab.\n");
3929 return false;
3931 vec_mode = TYPE_MODE (vectype);
3932 icode = (int) optab_handler (optab, vec_mode);
3933 if (icode == CODE_FOR_nothing)
3935 if (dump_enabled_p ())
3936 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3937 "op not supported by target.\n");
3938 /* Check only during analysis. */
3939 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
3940 || (vf < vect_min_worthwhile_factor (code)
3941 && !vec_stmt))
3942 return false;
3943 if (dump_enabled_p ())
3944 dump_printf_loc (MSG_NOTE, vect_location,
3945 "proceeding using word mode.\n");
3948 /* Worthwhile without SIMD support? Check only during analysis. */
3949 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
3950 && vf < vect_min_worthwhile_factor (code)
3951 && !vec_stmt)
3953 if (dump_enabled_p ())
3954 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3955 "not worthwhile without SIMD support.\n");
3956 return false;
3959 if (!vec_stmt) /* transformation not required. */
3961 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
3962 if (dump_enabled_p ())
3963 dump_printf_loc (MSG_NOTE, vect_location,
3964 "=== vectorizable_shift ===\n");
3965 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3966 return true;
3969 /** Transform. **/
3971 if (dump_enabled_p ())
3972 dump_printf_loc (MSG_NOTE, vect_location,
3973 "transform binary/unary operation.\n");
3975 /* Handle def. */
3976 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3978 prev_stmt_info = NULL;
3979 for (j = 0; j < ncopies; j++)
3981 /* Handle uses. */
3982 if (j == 0)
3984 if (scalar_shift_arg)
3986 /* Vector shl and shr insn patterns can be defined with scalar
3987 operand 2 (shift operand). In this case, use constant or loop
3988 invariant op1 directly, without extending it to vector mode
3989 first. */
3990 optab_op2_mode = insn_data[icode].operand[2].mode;
3991 if (!VECTOR_MODE_P (optab_op2_mode))
3993 if (dump_enabled_p ())
3994 dump_printf_loc (MSG_NOTE, vect_location,
3995 "operand 1 using scalar mode.\n");
3996 vec_oprnd1 = op1;
3997 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
3998 vec_oprnds1.quick_push (vec_oprnd1);
3999 if (slp_node)
4001 /* Store vec_oprnd1 for every vector stmt to be created
4002 for SLP_NODE. We check during the analysis that all
4003 the shift arguments are the same.
4004 TODO: Allow different constants for different vector
4005 stmts generated for an SLP instance. */
4006 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4007 vec_oprnds1.quick_push (vec_oprnd1);
4012 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
4013 (a special case for certain kind of vector shifts); otherwise,
4014 operand 1 should be of a vector type (the usual case). */
4015 if (vec_oprnd1)
4016 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4017 slp_node, -1);
4018 else
4019 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4020 slp_node, -1);
4022 else
4023 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4025 /* Arguments are ready. Create the new vector stmt. */
4026 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4028 vop1 = vec_oprnds1[i];
4029 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
4030 new_temp = make_ssa_name (vec_dest, new_stmt);
4031 gimple_assign_set_lhs (new_stmt, new_temp);
4032 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4033 if (slp_node)
4034 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4037 if (slp_node)
4038 continue;
4040 if (j == 0)
4041 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4042 else
4043 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4044 prev_stmt_info = vinfo_for_stmt (new_stmt);
4047 vec_oprnds0.release ();
4048 vec_oprnds1.release ();
4050 return true;
4054 static tree permute_vec_elements (tree, tree, tree, gimple,
4055 gimple_stmt_iterator *);
4058 /* Function vectorizable_operation.
4060 Check if STMT performs a binary, unary or ternary operation that can
4061 be vectorized.
4062 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4063 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4064 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4066 static bool
4067 vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
4068 gimple *vec_stmt, slp_tree slp_node)
4070 tree vec_dest;
4071 tree scalar_dest;
4072 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
4073 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4074 tree vectype;
4075 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4076 enum tree_code code;
4077 enum machine_mode vec_mode;
4078 tree new_temp;
4079 int op_type;
4080 optab optab;
4081 int icode;
4082 tree def;
4083 gimple def_stmt;
4084 enum vect_def_type dt[3]
4085 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
4086 gimple new_stmt = NULL;
4087 stmt_vec_info prev_stmt_info;
4088 int nunits_in;
4089 int nunits_out;
4090 tree vectype_out;
4091 int ncopies;
4092 int j, i;
4093 vec<tree> vec_oprnds0 = vNULL;
4094 vec<tree> vec_oprnds1 = vNULL;
4095 vec<tree> vec_oprnds2 = vNULL;
4096 tree vop0, vop1, vop2;
4097 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4098 int vf;
4100 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4101 return false;
4103 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4104 return false;
4106 /* Is STMT a vectorizable binary/unary operation? */
4107 if (!is_gimple_assign (stmt))
4108 return false;
4110 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4111 return false;
4113 code = gimple_assign_rhs_code (stmt);
4115 /* For pointer addition, we should use the normal plus for
4116 the vector addition. */
4117 if (code == POINTER_PLUS_EXPR)
4118 code = PLUS_EXPR;
4120 /* Support only unary or binary operations. */
4121 op_type = TREE_CODE_LENGTH (code);
4122 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
4124 if (dump_enabled_p ())
4125 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4126 "num. args = %d (not unary/binary/ternary op).\n",
4127 op_type);
4128 return false;
4131 scalar_dest = gimple_assign_lhs (stmt);
4132 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4134 /* Most operations cannot handle bit-precision types without extra
4135 truncations. */
4136 if ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4137 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4138 /* Exception are bitwise binary operations. */
4139 && code != BIT_IOR_EXPR
4140 && code != BIT_XOR_EXPR
4141 && code != BIT_AND_EXPR)
4143 if (dump_enabled_p ())
4144 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4145 "bit-precision arithmetic not supported.\n");
4146 return false;
4149 op0 = gimple_assign_rhs1 (stmt);
4150 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
4151 &def_stmt, &def, &dt[0], &vectype))
4153 if (dump_enabled_p ())
4154 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4155 "use not simple.\n");
4156 return false;
4158 /* If op0 is an external or constant def use a vector type with
4159 the same size as the output vector type. */
4160 if (!vectype)
4161 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4162 if (vec_stmt)
4163 gcc_assert (vectype);
4164 if (!vectype)
4166 if (dump_enabled_p ())
4168 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4169 "no vectype for scalar type ");
4170 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
4171 TREE_TYPE (op0));
4172 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4175 return false;
4178 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4179 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4180 if (nunits_out != nunits_in)
4181 return false;
4183 if (op_type == binary_op || op_type == ternary_op)
4185 op1 = gimple_assign_rhs2 (stmt);
4186 if (!vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4187 &def, &dt[1]))
4189 if (dump_enabled_p ())
4190 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4191 "use not simple.\n");
4192 return false;
4195 if (op_type == ternary_op)
4197 op2 = gimple_assign_rhs3 (stmt);
4198 if (!vect_is_simple_use (op2, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4199 &def, &dt[2]))
4201 if (dump_enabled_p ())
4202 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4203 "use not simple.\n");
4204 return false;
4208 if (loop_vinfo)
4209 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4210 else
4211 vf = 1;
4213 /* Multiple types in SLP are handled by creating the appropriate number of
4214 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4215 case of SLP. */
4216 if (slp_node || PURE_SLP_STMT (stmt_info))
4217 ncopies = 1;
4218 else
4219 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4221 gcc_assert (ncopies >= 1);
4223 /* Shifts are handled in vectorizable_shift (). */
4224 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4225 || code == RROTATE_EXPR)
4226 return false;
4228 /* Supportable by target? */
4230 vec_mode = TYPE_MODE (vectype);
4231 if (code == MULT_HIGHPART_EXPR)
4233 if (can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype)))
4234 icode = LAST_INSN_CODE;
4235 else
4236 icode = CODE_FOR_nothing;
4238 else
4240 optab = optab_for_tree_code (code, vectype, optab_default);
4241 if (!optab)
4243 if (dump_enabled_p ())
4244 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4245 "no optab.\n");
4246 return false;
4248 icode = (int) optab_handler (optab, vec_mode);
4251 if (icode == CODE_FOR_nothing)
4253 if (dump_enabled_p ())
4254 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4255 "op not supported by target.\n");
4256 /* Check only during analysis. */
4257 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4258 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
4259 return false;
4260 if (dump_enabled_p ())
4261 dump_printf_loc (MSG_NOTE, vect_location,
4262 "proceeding using word mode.\n");
4265 /* Worthwhile without SIMD support? Check only during analysis. */
4266 if (!VECTOR_MODE_P (vec_mode)
4267 && !vec_stmt
4268 && vf < vect_min_worthwhile_factor (code))
4270 if (dump_enabled_p ())
4271 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4272 "not worthwhile without SIMD support.\n");
4273 return false;
4276 if (!vec_stmt) /* transformation not required. */
4278 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
4279 if (dump_enabled_p ())
4280 dump_printf_loc (MSG_NOTE, vect_location,
4281 "=== vectorizable_operation ===\n");
4282 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4283 return true;
4286 /** Transform. **/
4288 if (dump_enabled_p ())
4289 dump_printf_loc (MSG_NOTE, vect_location,
4290 "transform binary/unary operation.\n");
4292 /* Handle def. */
4293 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4295 /* In case the vectorization factor (VF) is bigger than the number
4296 of elements that we can fit in a vectype (nunits), we have to generate
4297 more than one vector stmt - i.e - we need to "unroll" the
4298 vector stmt by a factor VF/nunits. In doing so, we record a pointer
4299 from one copy of the vector stmt to the next, in the field
4300 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4301 stages to find the correct vector defs to be used when vectorizing
4302 stmts that use the defs of the current stmt. The example below
4303 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
4304 we need to create 4 vectorized stmts):
4306 before vectorization:
4307 RELATED_STMT VEC_STMT
4308 S1: x = memref - -
4309 S2: z = x + 1 - -
4311 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
4312 there):
4313 RELATED_STMT VEC_STMT
4314 VS1_0: vx0 = memref0 VS1_1 -
4315 VS1_1: vx1 = memref1 VS1_2 -
4316 VS1_2: vx2 = memref2 VS1_3 -
4317 VS1_3: vx3 = memref3 - -
4318 S1: x = load - VS1_0
4319 S2: z = x + 1 - -
4321 step2: vectorize stmt S2 (done here):
4322 To vectorize stmt S2 we first need to find the relevant vector
4323 def for the first operand 'x'. This is, as usual, obtained from
4324 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
4325 that defines 'x' (S1). This way we find the stmt VS1_0, and the
4326 relevant vector def 'vx0'. Having found 'vx0' we can generate
4327 the vector stmt VS2_0, and as usual, record it in the
4328 STMT_VINFO_VEC_STMT of stmt S2.
4329 When creating the second copy (VS2_1), we obtain the relevant vector
4330 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
4331 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
4332 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
4333 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
4334 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
4335 chain of stmts and pointers:
4336 RELATED_STMT VEC_STMT
4337 VS1_0: vx0 = memref0 VS1_1 -
4338 VS1_1: vx1 = memref1 VS1_2 -
4339 VS1_2: vx2 = memref2 VS1_3 -
4340 VS1_3: vx3 = memref3 - -
4341 S1: x = load - VS1_0
4342 VS2_0: vz0 = vx0 + v1 VS2_1 -
4343 VS2_1: vz1 = vx1 + v1 VS2_2 -
4344 VS2_2: vz2 = vx2 + v1 VS2_3 -
4345 VS2_3: vz3 = vx3 + v1 - -
4346 S2: z = x + 1 - VS2_0 */
4348 prev_stmt_info = NULL;
4349 for (j = 0; j < ncopies; j++)
4351 /* Handle uses. */
4352 if (j == 0)
4354 if (op_type == binary_op || op_type == ternary_op)
4355 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4356 slp_node, -1);
4357 else
4358 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4359 slp_node, -1);
4360 if (op_type == ternary_op)
4362 vec_oprnds2.create (1);
4363 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
4364 stmt,
4365 NULL));
4368 else
4370 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4371 if (op_type == ternary_op)
4373 tree vec_oprnd = vec_oprnds2.pop ();
4374 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
4375 vec_oprnd));
4379 /* Arguments are ready. Create the new vector stmt. */
4380 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4382 vop1 = ((op_type == binary_op || op_type == ternary_op)
4383 ? vec_oprnds1[i] : NULL_TREE);
4384 vop2 = ((op_type == ternary_op)
4385 ? vec_oprnds2[i] : NULL_TREE);
4386 new_stmt = gimple_build_assign_with_ops (code, vec_dest,
4387 vop0, vop1, vop2);
4388 new_temp = make_ssa_name (vec_dest, new_stmt);
4389 gimple_assign_set_lhs (new_stmt, new_temp);
4390 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4391 if (slp_node)
4392 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4395 if (slp_node)
4396 continue;
4398 if (j == 0)
4399 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4400 else
4401 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4402 prev_stmt_info = vinfo_for_stmt (new_stmt);
4405 vec_oprnds0.release ();
4406 vec_oprnds1.release ();
4407 vec_oprnds2.release ();
4409 return true;
4412 /* A helper function to ensure data reference DR's base alignment
4413 for STMT_INFO. */
4415 static void
4416 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
4418 if (!dr->aux)
4419 return;
4421 if (((dataref_aux *)dr->aux)->base_misaligned)
4423 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4424 tree base_decl = ((dataref_aux *)dr->aux)->base_decl;
4426 DECL_ALIGN (base_decl) = TYPE_ALIGN (vectype);
4427 DECL_USER_ALIGN (base_decl) = 1;
4428 ((dataref_aux *)dr->aux)->base_misaligned = false;
4433 /* Function vectorizable_store.
4435 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
4436 can be vectorized.
4437 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4438 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4439 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4441 static bool
4442 vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
4443 slp_tree slp_node)
4445 tree scalar_dest;
4446 tree data_ref;
4447 tree op;
4448 tree vec_oprnd = NULL_TREE;
4449 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4450 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
4451 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4452 tree elem_type;
4453 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4454 struct loop *loop = NULL;
4455 enum machine_mode vec_mode;
4456 tree dummy;
4457 enum dr_alignment_support alignment_support_scheme;
4458 tree def;
4459 gimple def_stmt;
4460 enum vect_def_type dt;
4461 stmt_vec_info prev_stmt_info = NULL;
4462 tree dataref_ptr = NULL_TREE;
4463 tree dataref_offset = NULL_TREE;
4464 gimple ptr_incr = NULL;
4465 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4466 int ncopies;
4467 int j;
4468 gimple next_stmt, first_stmt = NULL;
4469 bool grouped_store = false;
4470 bool store_lanes_p = false;
4471 unsigned int group_size, i;
4472 vec<tree> dr_chain = vNULL;
4473 vec<tree> oprnds = vNULL;
4474 vec<tree> result_chain = vNULL;
4475 bool inv_p;
4476 vec<tree> vec_oprnds = vNULL;
4477 bool slp = (slp_node != NULL);
4478 unsigned int vec_num;
4479 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4480 tree aggr_type;
4482 if (loop_vinfo)
4483 loop = LOOP_VINFO_LOOP (loop_vinfo);
4485 /* Multiple types in SLP are handled by creating the appropriate number of
4486 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4487 case of SLP. */
4488 if (slp || PURE_SLP_STMT (stmt_info))
4489 ncopies = 1;
4490 else
4491 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4493 gcc_assert (ncopies >= 1);
4495 /* FORNOW. This restriction should be relaxed. */
4496 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
4498 if (dump_enabled_p ())
4499 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4500 "multiple types in nested loop.\n");
4501 return false;
4504 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4505 return false;
4507 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4508 return false;
4510 /* Is vectorizable store? */
4512 if (!is_gimple_assign (stmt))
4513 return false;
4515 scalar_dest = gimple_assign_lhs (stmt);
4516 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
4517 && is_pattern_stmt_p (stmt_info))
4518 scalar_dest = TREE_OPERAND (scalar_dest, 0);
4519 if (TREE_CODE (scalar_dest) != ARRAY_REF
4520 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
4521 && TREE_CODE (scalar_dest) != INDIRECT_REF
4522 && TREE_CODE (scalar_dest) != COMPONENT_REF
4523 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
4524 && TREE_CODE (scalar_dest) != REALPART_EXPR
4525 && TREE_CODE (scalar_dest) != MEM_REF)
4526 return false;
4528 gcc_assert (gimple_assign_single_p (stmt));
4529 op = gimple_assign_rhs1 (stmt);
4530 if (!vect_is_simple_use (op, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4531 &def, &dt))
4533 if (dump_enabled_p ())
4534 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4535 "use not simple.\n");
4536 return false;
4539 elem_type = TREE_TYPE (vectype);
4540 vec_mode = TYPE_MODE (vectype);
4542 /* FORNOW. In some cases can vectorize even if data-type not supported
4543 (e.g. - array initialization with 0). */
4544 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
4545 return false;
4547 if (!STMT_VINFO_DATA_REF (stmt_info))
4548 return false;
4550 if (tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
4551 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
4552 size_zero_node) < 0)
4554 if (dump_enabled_p ())
4555 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4556 "negative step for store.\n");
4557 return false;
4560 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
4562 grouped_store = true;
4563 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
4564 if (!slp && !PURE_SLP_STMT (stmt_info))
4566 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4567 if (vect_store_lanes_supported (vectype, group_size))
4568 store_lanes_p = true;
4569 else if (!vect_grouped_store_supported (vectype, group_size))
4570 return false;
4573 if (first_stmt == stmt)
4575 /* STMT is the leader of the group. Check the operands of all the
4576 stmts of the group. */
4577 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
4578 while (next_stmt)
4580 gcc_assert (gimple_assign_single_p (next_stmt));
4581 op = gimple_assign_rhs1 (next_stmt);
4582 if (!vect_is_simple_use (op, next_stmt, loop_vinfo, bb_vinfo,
4583 &def_stmt, &def, &dt))
4585 if (dump_enabled_p ())
4586 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4587 "use not simple.\n");
4588 return false;
4590 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4595 if (!vec_stmt) /* transformation not required. */
4597 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
4598 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
4599 NULL, NULL, NULL);
4600 return true;
4603 /** Transform. **/
4605 ensure_base_align (stmt_info, dr);
4607 if (grouped_store)
4609 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
4610 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4612 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
4614 /* FORNOW */
4615 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
4617 /* We vectorize all the stmts of the interleaving group when we
4618 reach the last stmt in the group. */
4619 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
4620 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
4621 && !slp)
4623 *vec_stmt = NULL;
4624 return true;
4627 if (slp)
4629 grouped_store = false;
4630 /* VEC_NUM is the number of vect stmts to be created for this
4631 group. */
4632 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
4633 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
4634 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
4635 op = gimple_assign_rhs1 (first_stmt);
4637 else
4638 /* VEC_NUM is the number of vect stmts to be created for this
4639 group. */
4640 vec_num = group_size;
4642 else
4644 first_stmt = stmt;
4645 first_dr = dr;
4646 group_size = vec_num = 1;
4649 if (dump_enabled_p ())
4650 dump_printf_loc (MSG_NOTE, vect_location,
4651 "transform store. ncopies = %d\n", ncopies);
4653 dr_chain.create (group_size);
4654 oprnds.create (group_size);
4656 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
4657 gcc_assert (alignment_support_scheme);
4658 /* Targets with store-lane instructions must not require explicit
4659 realignment. */
4660 gcc_assert (!store_lanes_p
4661 || alignment_support_scheme == dr_aligned
4662 || alignment_support_scheme == dr_unaligned_supported);
4664 if (store_lanes_p)
4665 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
4666 else
4667 aggr_type = vectype;
4669 /* In case the vectorization factor (VF) is bigger than the number
4670 of elements that we can fit in a vectype (nunits), we have to generate
4671 more than one vector stmt - i.e - we need to "unroll" the
4672 vector stmt by a factor VF/nunits. For more details see documentation in
4673 vect_get_vec_def_for_copy_stmt. */
4675 /* In case of interleaving (non-unit grouped access):
4677 S1: &base + 2 = x2
4678 S2: &base = x0
4679 S3: &base + 1 = x1
4680 S4: &base + 3 = x3
4682 We create vectorized stores starting from base address (the access of the
4683 first stmt in the chain (S2 in the above example), when the last store stmt
4684 of the chain (S4) is reached:
4686 VS1: &base = vx2
4687 VS2: &base + vec_size*1 = vx0
4688 VS3: &base + vec_size*2 = vx1
4689 VS4: &base + vec_size*3 = vx3
4691 Then permutation statements are generated:
4693 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
4694 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
4697 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
4698 (the order of the data-refs in the output of vect_permute_store_chain
4699 corresponds to the order of scalar stmts in the interleaving chain - see
4700 the documentation of vect_permute_store_chain()).
4702 In case of both multiple types and interleaving, above vector stores and
4703 permutation stmts are created for every copy. The result vector stmts are
4704 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
4705 STMT_VINFO_RELATED_STMT for the next copies.
4708 prev_stmt_info = NULL;
4709 for (j = 0; j < ncopies; j++)
4711 gimple new_stmt;
4713 if (j == 0)
4715 if (slp)
4717 /* Get vectorized arguments for SLP_NODE. */
4718 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
4719 NULL, slp_node, -1);
4721 vec_oprnd = vec_oprnds[0];
4723 else
4725 /* For interleaved stores we collect vectorized defs for all the
4726 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
4727 used as an input to vect_permute_store_chain(), and OPRNDS as
4728 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
4730 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
4731 OPRNDS are of size 1. */
4732 next_stmt = first_stmt;
4733 for (i = 0; i < group_size; i++)
4735 /* Since gaps are not supported for interleaved stores,
4736 GROUP_SIZE is the exact number of stmts in the chain.
4737 Therefore, NEXT_STMT can't be NULL_TREE. In case that
4738 there is no interleaving, GROUP_SIZE is 1, and only one
4739 iteration of the loop will be executed. */
4740 gcc_assert (next_stmt
4741 && gimple_assign_single_p (next_stmt));
4742 op = gimple_assign_rhs1 (next_stmt);
4744 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt,
4745 NULL);
4746 dr_chain.quick_push (vec_oprnd);
4747 oprnds.quick_push (vec_oprnd);
4748 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4752 /* We should have catched mismatched types earlier. */
4753 gcc_assert (useless_type_conversion_p (vectype,
4754 TREE_TYPE (vec_oprnd)));
4755 bool simd_lane_access_p
4756 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
4757 if (simd_lane_access_p
4758 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
4759 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
4760 && integer_zerop (DR_OFFSET (first_dr))
4761 && integer_zerop (DR_INIT (first_dr))
4762 && alias_sets_conflict_p (get_alias_set (aggr_type),
4763 get_alias_set (DR_REF (first_dr))))
4765 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
4766 dataref_offset = build_int_cst (reference_alias_ptr_type
4767 (DR_REF (first_dr)), 0);
4768 inv_p = false;
4770 else
4771 dataref_ptr
4772 = vect_create_data_ref_ptr (first_stmt, aggr_type,
4773 simd_lane_access_p ? loop : NULL,
4774 NULL_TREE, &dummy, gsi, &ptr_incr,
4775 simd_lane_access_p, &inv_p);
4776 gcc_assert (bb_vinfo || !inv_p);
4778 else
4780 /* For interleaved stores we created vectorized defs for all the
4781 defs stored in OPRNDS in the previous iteration (previous copy).
4782 DR_CHAIN is then used as an input to vect_permute_store_chain(),
4783 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
4784 next copy.
4785 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
4786 OPRNDS are of size 1. */
4787 for (i = 0; i < group_size; i++)
4789 op = oprnds[i];
4790 vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo, &def_stmt,
4791 &def, &dt);
4792 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
4793 dr_chain[i] = vec_oprnd;
4794 oprnds[i] = vec_oprnd;
4796 if (dataref_offset)
4797 dataref_offset
4798 = int_const_binop (PLUS_EXPR, dataref_offset,
4799 TYPE_SIZE_UNIT (aggr_type));
4800 else
4801 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
4802 TYPE_SIZE_UNIT (aggr_type));
4805 if (store_lanes_p)
4807 tree vec_array;
4809 /* Combine all the vectors into an array. */
4810 vec_array = create_vector_array (vectype, vec_num);
4811 for (i = 0; i < vec_num; i++)
4813 vec_oprnd = dr_chain[i];
4814 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
4817 /* Emit:
4818 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
4819 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
4820 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
4821 gimple_call_set_lhs (new_stmt, data_ref);
4822 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4824 else
4826 new_stmt = NULL;
4827 if (grouped_store)
4829 if (j == 0)
4830 result_chain.create (group_size);
4831 /* Permute. */
4832 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
4833 &result_chain);
4836 next_stmt = first_stmt;
4837 for (i = 0; i < vec_num; i++)
4839 unsigned align, misalign;
4841 if (i > 0)
4842 /* Bump the vector pointer. */
4843 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
4844 stmt, NULL_TREE);
4846 if (slp)
4847 vec_oprnd = vec_oprnds[i];
4848 else if (grouped_store)
4849 /* For grouped stores vectorized defs are interleaved in
4850 vect_permute_store_chain(). */
4851 vec_oprnd = result_chain[i];
4853 data_ref = build2 (MEM_REF, TREE_TYPE (vec_oprnd), dataref_ptr,
4854 dataref_offset
4855 ? dataref_offset
4856 : build_int_cst (reference_alias_ptr_type
4857 (DR_REF (first_dr)), 0));
4858 align = TYPE_ALIGN_UNIT (vectype);
4859 if (aligned_access_p (first_dr))
4860 misalign = 0;
4861 else if (DR_MISALIGNMENT (first_dr) == -1)
4863 TREE_TYPE (data_ref)
4864 = build_aligned_type (TREE_TYPE (data_ref),
4865 TYPE_ALIGN (elem_type));
4866 align = TYPE_ALIGN_UNIT (elem_type);
4867 misalign = 0;
4869 else
4871 TREE_TYPE (data_ref)
4872 = build_aligned_type (TREE_TYPE (data_ref),
4873 TYPE_ALIGN (elem_type));
4874 misalign = DR_MISALIGNMENT (first_dr);
4876 if (dataref_offset == NULL_TREE)
4877 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
4878 misalign);
4880 /* Arguments are ready. Create the new vector stmt. */
4881 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
4882 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4884 if (slp)
4885 continue;
4887 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4888 if (!next_stmt)
4889 break;
4892 if (!slp)
4894 if (j == 0)
4895 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4896 else
4897 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4898 prev_stmt_info = vinfo_for_stmt (new_stmt);
4902 dr_chain.release ();
4903 oprnds.release ();
4904 result_chain.release ();
4905 vec_oprnds.release ();
4907 return true;
4910 /* Given a vector type VECTYPE and permutation SEL returns
4911 the VECTOR_CST mask that implements the permutation of the
4912 vector elements. If that is impossible to do, returns NULL. */
4914 tree
4915 vect_gen_perm_mask (tree vectype, unsigned char *sel)
4917 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
4918 int i, nunits;
4920 nunits = TYPE_VECTOR_SUBPARTS (vectype);
4922 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
4923 return NULL;
4925 mask_elt_type = lang_hooks.types.type_for_mode
4926 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
4927 mask_type = get_vectype_for_scalar_type (mask_elt_type);
4929 mask_elts = XALLOCAVEC (tree, nunits);
4930 for (i = nunits - 1; i >= 0; i--)
4931 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
4932 mask_vec = build_vector (mask_type, mask_elts);
4934 return mask_vec;
4937 /* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
4938 reversal of the vector elements. If that is impossible to do,
4939 returns NULL. */
4941 static tree
4942 perm_mask_for_reverse (tree vectype)
4944 int i, nunits;
4945 unsigned char *sel;
4947 nunits = TYPE_VECTOR_SUBPARTS (vectype);
4948 sel = XALLOCAVEC (unsigned char, nunits);
4950 for (i = 0; i < nunits; ++i)
4951 sel[i] = nunits - 1 - i;
4953 return vect_gen_perm_mask (vectype, sel);
4956 /* Given a vector variable X and Y, that was generated for the scalar
4957 STMT, generate instructions to permute the vector elements of X and Y
4958 using permutation mask MASK_VEC, insert them at *GSI and return the
4959 permuted vector variable. */
4961 static tree
4962 permute_vec_elements (tree x, tree y, tree mask_vec, gimple stmt,
4963 gimple_stmt_iterator *gsi)
4965 tree vectype = TREE_TYPE (x);
4966 tree perm_dest, data_ref;
4967 gimple perm_stmt;
4969 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
4970 data_ref = make_ssa_name (perm_dest, NULL);
4972 /* Generate the permute statement. */
4973 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4974 x, y, mask_vec);
4975 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4977 return data_ref;
4980 /* vectorizable_load.
4982 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
4983 can be vectorized.
4984 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4985 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4986 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4988 static bool
4989 vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
4990 slp_tree slp_node, slp_instance slp_node_instance)
4992 tree scalar_dest;
4993 tree vec_dest = NULL;
4994 tree data_ref = NULL;
4995 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4996 stmt_vec_info prev_stmt_info;
4997 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4998 struct loop *loop = NULL;
4999 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
5000 bool nested_in_vect_loop = false;
5001 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5002 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5003 tree elem_type;
5004 tree new_temp;
5005 enum machine_mode mode;
5006 gimple new_stmt = NULL;
5007 tree dummy;
5008 enum dr_alignment_support alignment_support_scheme;
5009 tree dataref_ptr = NULL_TREE;
5010 tree dataref_offset = NULL_TREE;
5011 gimple ptr_incr = NULL;
5012 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5013 int ncopies;
5014 int i, j, group_size, group_gap;
5015 tree msq = NULL_TREE, lsq;
5016 tree offset = NULL_TREE;
5017 tree realignment_token = NULL_TREE;
5018 gimple phi = NULL;
5019 vec<tree> dr_chain = vNULL;
5020 bool grouped_load = false;
5021 bool load_lanes_p = false;
5022 gimple first_stmt;
5023 bool inv_p;
5024 bool negative = false;
5025 bool compute_in_loop = false;
5026 struct loop *at_loop;
5027 int vec_num;
5028 bool slp = (slp_node != NULL);
5029 bool slp_perm = false;
5030 enum tree_code code;
5031 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5032 int vf;
5033 tree aggr_type;
5034 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
5035 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
5036 int gather_scale = 1;
5037 enum vect_def_type gather_dt = vect_unknown_def_type;
5039 if (loop_vinfo)
5041 loop = LOOP_VINFO_LOOP (loop_vinfo);
5042 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
5043 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5045 else
5046 vf = 1;
5048 /* Multiple types in SLP are handled by creating the appropriate number of
5049 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5050 case of SLP. */
5051 if (slp || PURE_SLP_STMT (stmt_info))
5052 ncopies = 1;
5053 else
5054 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5056 gcc_assert (ncopies >= 1);
5058 /* FORNOW. This restriction should be relaxed. */
5059 if (nested_in_vect_loop && ncopies > 1)
5061 if (dump_enabled_p ())
5062 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5063 "multiple types in nested loop.\n");
5064 return false;
5067 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5068 return false;
5070 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
5071 return false;
5073 /* Is vectorizable load? */
5074 if (!is_gimple_assign (stmt))
5075 return false;
5077 scalar_dest = gimple_assign_lhs (stmt);
5078 if (TREE_CODE (scalar_dest) != SSA_NAME)
5079 return false;
5081 code = gimple_assign_rhs_code (stmt);
5082 if (code != ARRAY_REF
5083 && code != BIT_FIELD_REF
5084 && code != INDIRECT_REF
5085 && code != COMPONENT_REF
5086 && code != IMAGPART_EXPR
5087 && code != REALPART_EXPR
5088 && code != MEM_REF
5089 && TREE_CODE_CLASS (code) != tcc_declaration)
5090 return false;
5092 if (!STMT_VINFO_DATA_REF (stmt_info))
5093 return false;
5095 elem_type = TREE_TYPE (vectype);
5096 mode = TYPE_MODE (vectype);
5098 /* FORNOW. In some cases can vectorize even if data-type not supported
5099 (e.g. - data copies). */
5100 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
5102 if (dump_enabled_p ())
5103 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5104 "Aligned load, but unsupported type.\n");
5105 return false;
5108 /* Check if the load is a part of an interleaving chain. */
5109 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5111 grouped_load = true;
5112 /* FORNOW */
5113 gcc_assert (! nested_in_vect_loop && !STMT_VINFO_GATHER_P (stmt_info));
5115 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5116 if (!slp && !PURE_SLP_STMT (stmt_info))
5118 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5119 if (vect_load_lanes_supported (vectype, group_size))
5120 load_lanes_p = true;
5121 else if (!vect_grouped_load_supported (vectype, group_size))
5122 return false;
5127 if (STMT_VINFO_GATHER_P (stmt_info))
5129 gimple def_stmt;
5130 tree def;
5131 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
5132 &gather_off, &gather_scale);
5133 gcc_assert (gather_decl);
5134 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, bb_vinfo,
5135 &def_stmt, &def, &gather_dt,
5136 &gather_off_vectype))
5138 if (dump_enabled_p ())
5139 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5140 "gather index use not simple.\n");
5141 return false;
5144 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
5146 else
5148 negative = tree_int_cst_compare (nested_in_vect_loop
5149 ? STMT_VINFO_DR_STEP (stmt_info)
5150 : DR_STEP (dr),
5151 size_zero_node) < 0;
5152 if (negative && ncopies > 1)
5154 if (dump_enabled_p ())
5155 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5156 "multiple types with negative step.\n");
5157 return false;
5160 if (negative)
5162 if (grouped_load)
5164 if (dump_enabled_p ())
5165 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5166 "negative step for group load not supported"
5167 "\n");
5168 return false;
5170 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5171 if (alignment_support_scheme != dr_aligned
5172 && alignment_support_scheme != dr_unaligned_supported)
5174 if (dump_enabled_p ())
5175 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5176 "negative step but alignment required.\n");
5177 return false;
5179 if (!perm_mask_for_reverse (vectype))
5181 if (dump_enabled_p ())
5182 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5183 "negative step and reversing not supported."
5184 "\n");
5185 return false;
5190 if (!vec_stmt) /* transformation not required. */
5192 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
5193 vect_model_load_cost (stmt_info, ncopies, load_lanes_p, NULL, NULL, NULL);
5194 return true;
5197 if (dump_enabled_p ())
5198 dump_printf_loc (MSG_NOTE, vect_location,
5199 "transform load. ncopies = %d\n", ncopies);
5201 /** Transform. **/
5203 ensure_base_align (stmt_info, dr);
5205 if (STMT_VINFO_GATHER_P (stmt_info))
5207 tree vec_oprnd0 = NULL_TREE, op;
5208 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
5209 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5210 tree ptr, mask, var, scale, perm_mask = NULL_TREE, prev_res = NULL_TREE;
5211 edge pe = loop_preheader_edge (loop);
5212 gimple_seq seq;
5213 basic_block new_bb;
5214 enum { NARROW, NONE, WIDEN } modifier;
5215 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
5217 if (nunits == gather_off_nunits)
5218 modifier = NONE;
5219 else if (nunits == gather_off_nunits / 2)
5221 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
5222 modifier = WIDEN;
5224 for (i = 0; i < gather_off_nunits; ++i)
5225 sel[i] = i | nunits;
5227 perm_mask = vect_gen_perm_mask (gather_off_vectype, sel);
5228 gcc_assert (perm_mask != NULL_TREE);
5230 else if (nunits == gather_off_nunits * 2)
5232 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5233 modifier = NARROW;
5235 for (i = 0; i < nunits; ++i)
5236 sel[i] = i < gather_off_nunits
5237 ? i : i + nunits - gather_off_nunits;
5239 perm_mask = vect_gen_perm_mask (vectype, sel);
5240 gcc_assert (perm_mask != NULL_TREE);
5241 ncopies *= 2;
5243 else
5244 gcc_unreachable ();
5246 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
5247 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5248 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5249 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5250 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5251 scaletype = TREE_VALUE (arglist);
5252 gcc_checking_assert (types_compatible_p (srctype, rettype)
5253 && types_compatible_p (srctype, masktype));
5255 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5257 ptr = fold_convert (ptrtype, gather_base);
5258 if (!is_gimple_min_invariant (ptr))
5260 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5261 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5262 gcc_assert (!new_bb);
5265 /* Currently we support only unconditional gather loads,
5266 so mask should be all ones. */
5267 if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
5268 mask = build_int_cst (TREE_TYPE (masktype), -1);
5269 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
5271 REAL_VALUE_TYPE r;
5272 long tmp[6];
5273 for (j = 0; j < 6; ++j)
5274 tmp[j] = -1;
5275 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
5276 mask = build_real (TREE_TYPE (masktype), r);
5278 else
5279 gcc_unreachable ();
5280 mask = build_vector_from_val (masktype, mask);
5281 mask = vect_init_vector (stmt, mask, masktype, NULL);
5283 scale = build_int_cst (scaletype, gather_scale);
5285 prev_stmt_info = NULL;
5286 for (j = 0; j < ncopies; ++j)
5288 if (modifier == WIDEN && (j & 1))
5289 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
5290 perm_mask, stmt, gsi);
5291 else if (j == 0)
5292 op = vec_oprnd0
5293 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
5294 else
5295 op = vec_oprnd0
5296 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
5298 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
5300 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
5301 == TYPE_VECTOR_SUBPARTS (idxtype));
5302 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
5303 var = make_ssa_name (var, NULL);
5304 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
5305 new_stmt
5306 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var,
5307 op, NULL_TREE);
5308 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5309 op = var;
5312 new_stmt
5313 = gimple_build_call (gather_decl, 5, mask, ptr, op, mask, scale);
5315 if (!useless_type_conversion_p (vectype, rettype))
5317 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
5318 == TYPE_VECTOR_SUBPARTS (rettype));
5319 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
5320 op = make_ssa_name (var, new_stmt);
5321 gimple_call_set_lhs (new_stmt, op);
5322 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5323 var = make_ssa_name (vec_dest, NULL);
5324 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
5325 new_stmt
5326 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var, op,
5327 NULL_TREE);
5329 else
5331 var = make_ssa_name (vec_dest, new_stmt);
5332 gimple_call_set_lhs (new_stmt, var);
5335 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5337 if (modifier == NARROW)
5339 if ((j & 1) == 0)
5341 prev_res = var;
5342 continue;
5344 var = permute_vec_elements (prev_res, var,
5345 perm_mask, stmt, gsi);
5346 new_stmt = SSA_NAME_DEF_STMT (var);
5349 if (prev_stmt_info == NULL)
5350 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5351 else
5352 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5353 prev_stmt_info = vinfo_for_stmt (new_stmt);
5355 return true;
5357 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
5359 gimple_stmt_iterator incr_gsi;
5360 bool insert_after;
5361 gimple incr;
5362 tree offvar;
5363 tree ivstep;
5364 tree running_off;
5365 vec<constructor_elt, va_gc> *v = NULL;
5366 gimple_seq stmts = NULL;
5367 tree stride_base, stride_step, alias_off;
5369 gcc_assert (!nested_in_vect_loop);
5371 stride_base
5372 = fold_build_pointer_plus
5373 (unshare_expr (DR_BASE_ADDRESS (dr)),
5374 size_binop (PLUS_EXPR,
5375 convert_to_ptrofftype (unshare_expr (DR_OFFSET (dr))),
5376 convert_to_ptrofftype (DR_INIT (dr))));
5377 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (dr)));
5379 /* For a load with loop-invariant (but other than power-of-2)
5380 stride (i.e. not a grouped access) like so:
5382 for (i = 0; i < n; i += stride)
5383 ... = array[i];
5385 we generate a new induction variable and new accesses to
5386 form a new vector (or vectors, depending on ncopies):
5388 for (j = 0; ; j += VF*stride)
5389 tmp1 = array[j];
5390 tmp2 = array[j + stride];
5392 vectemp = {tmp1, tmp2, ...}
5395 ivstep = stride_step;
5396 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
5397 build_int_cst (TREE_TYPE (ivstep), vf));
5399 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5401 create_iv (stride_base, ivstep, NULL,
5402 loop, &incr_gsi, insert_after,
5403 &offvar, NULL);
5404 incr = gsi_stmt (incr_gsi);
5405 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
5407 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
5408 if (stmts)
5409 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5411 prev_stmt_info = NULL;
5412 running_off = offvar;
5413 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0);
5414 for (j = 0; j < ncopies; j++)
5416 tree vec_inv;
5418 vec_alloc (v, nunits);
5419 for (i = 0; i < nunits; i++)
5421 tree newref, newoff;
5422 gimple incr;
5423 newref = build2 (MEM_REF, TREE_TYPE (vectype),
5424 running_off, alias_off);
5426 newref = force_gimple_operand_gsi (gsi, newref, true,
5427 NULL_TREE, true,
5428 GSI_SAME_STMT);
5429 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
5430 newoff = copy_ssa_name (running_off, NULL);
5431 incr = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, newoff,
5432 running_off, stride_step);
5433 vect_finish_stmt_generation (stmt, incr, gsi);
5435 running_off = newoff;
5438 vec_inv = build_constructor (vectype, v);
5439 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
5440 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5442 if (j == 0)
5443 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5444 else
5445 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5446 prev_stmt_info = vinfo_for_stmt (new_stmt);
5448 return true;
5451 if (grouped_load)
5453 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5454 if (slp
5455 && !SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
5456 && first_stmt != SLP_TREE_SCALAR_STMTS (slp_node)[0])
5457 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5459 /* Check if the chain of loads is already vectorized. */
5460 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
5461 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
5462 ??? But we can only do so if there is exactly one
5463 as we have no way to get at the rest. Leave the CSE
5464 opportunity alone.
5465 ??? With the group load eventually participating
5466 in multiple different permutations (having multiple
5467 slp nodes which refer to the same group) the CSE
5468 is even wrong code. See PR56270. */
5469 && !slp)
5471 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
5472 return true;
5474 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5475 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5477 /* VEC_NUM is the number of vect stmts to be created for this group. */
5478 if (slp)
5480 grouped_load = false;
5481 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5482 if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
5483 slp_perm = true;
5484 group_gap = GROUP_GAP (vinfo_for_stmt (first_stmt));
5486 else
5488 vec_num = group_size;
5489 group_gap = 0;
5492 else
5494 first_stmt = stmt;
5495 first_dr = dr;
5496 group_size = vec_num = 1;
5497 group_gap = 0;
5500 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
5501 gcc_assert (alignment_support_scheme);
5502 /* Targets with load-lane instructions must not require explicit
5503 realignment. */
5504 gcc_assert (!load_lanes_p
5505 || alignment_support_scheme == dr_aligned
5506 || alignment_support_scheme == dr_unaligned_supported);
5508 /* In case the vectorization factor (VF) is bigger than the number
5509 of elements that we can fit in a vectype (nunits), we have to generate
5510 more than one vector stmt - i.e - we need to "unroll" the
5511 vector stmt by a factor VF/nunits. In doing so, we record a pointer
5512 from one copy of the vector stmt to the next, in the field
5513 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
5514 stages to find the correct vector defs to be used when vectorizing
5515 stmts that use the defs of the current stmt. The example below
5516 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
5517 need to create 4 vectorized stmts):
5519 before vectorization:
5520 RELATED_STMT VEC_STMT
5521 S1: x = memref - -
5522 S2: z = x + 1 - -
5524 step 1: vectorize stmt S1:
5525 We first create the vector stmt VS1_0, and, as usual, record a
5526 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
5527 Next, we create the vector stmt VS1_1, and record a pointer to
5528 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
5529 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
5530 stmts and pointers:
5531 RELATED_STMT VEC_STMT
5532 VS1_0: vx0 = memref0 VS1_1 -
5533 VS1_1: vx1 = memref1 VS1_2 -
5534 VS1_2: vx2 = memref2 VS1_3 -
5535 VS1_3: vx3 = memref3 - -
5536 S1: x = load - VS1_0
5537 S2: z = x + 1 - -
5539 See in documentation in vect_get_vec_def_for_stmt_copy for how the
5540 information we recorded in RELATED_STMT field is used to vectorize
5541 stmt S2. */
5543 /* In case of interleaving (non-unit grouped access):
5545 S1: x2 = &base + 2
5546 S2: x0 = &base
5547 S3: x1 = &base + 1
5548 S4: x3 = &base + 3
5550 Vectorized loads are created in the order of memory accesses
5551 starting from the access of the first stmt of the chain:
5553 VS1: vx0 = &base
5554 VS2: vx1 = &base + vec_size*1
5555 VS3: vx3 = &base + vec_size*2
5556 VS4: vx4 = &base + vec_size*3
5558 Then permutation statements are generated:
5560 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
5561 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
5564 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5565 (the order of the data-refs in the output of vect_permute_load_chain
5566 corresponds to the order of scalar stmts in the interleaving chain - see
5567 the documentation of vect_permute_load_chain()).
5568 The generation of permutation stmts and recording them in
5569 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
5571 In case of both multiple types and interleaving, the vector loads and
5572 permutation stmts above are created for every copy. The result vector
5573 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
5574 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
5576 /* If the data reference is aligned (dr_aligned) or potentially unaligned
5577 on a target that supports unaligned accesses (dr_unaligned_supported)
5578 we generate the following code:
5579 p = initial_addr;
5580 indx = 0;
5581 loop {
5582 p = p + indx * vectype_size;
5583 vec_dest = *(p);
5584 indx = indx + 1;
5587 Otherwise, the data reference is potentially unaligned on a target that
5588 does not support unaligned accesses (dr_explicit_realign_optimized) -
5589 then generate the following code, in which the data in each iteration is
5590 obtained by two vector loads, one from the previous iteration, and one
5591 from the current iteration:
5592 p1 = initial_addr;
5593 msq_init = *(floor(p1))
5594 p2 = initial_addr + VS - 1;
5595 realignment_token = call target_builtin;
5596 indx = 0;
5597 loop {
5598 p2 = p2 + indx * vectype_size
5599 lsq = *(floor(p2))
5600 vec_dest = realign_load (msq, lsq, realignment_token)
5601 indx = indx + 1;
5602 msq = lsq;
5603 } */
5605 /* If the misalignment remains the same throughout the execution of the
5606 loop, we can create the init_addr and permutation mask at the loop
5607 preheader. Otherwise, it needs to be created inside the loop.
5608 This can only occur when vectorizing memory accesses in the inner-loop
5609 nested within an outer-loop that is being vectorized. */
5611 if (nested_in_vect_loop
5612 && (TREE_INT_CST_LOW (DR_STEP (dr))
5613 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
5615 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
5616 compute_in_loop = true;
5619 if ((alignment_support_scheme == dr_explicit_realign_optimized
5620 || alignment_support_scheme == dr_explicit_realign)
5621 && !compute_in_loop)
5623 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
5624 alignment_support_scheme, NULL_TREE,
5625 &at_loop);
5626 if (alignment_support_scheme == dr_explicit_realign_optimized)
5628 phi = SSA_NAME_DEF_STMT (msq);
5629 offset = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
5632 else
5633 at_loop = loop;
5635 if (negative)
5636 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
5638 if (load_lanes_p)
5639 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5640 else
5641 aggr_type = vectype;
5643 prev_stmt_info = NULL;
5644 for (j = 0; j < ncopies; j++)
5646 /* 1. Create the vector or array pointer update chain. */
5647 if (j == 0)
5649 bool simd_lane_access_p
5650 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5651 if (simd_lane_access_p
5652 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5653 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5654 && integer_zerop (DR_OFFSET (first_dr))
5655 && integer_zerop (DR_INIT (first_dr))
5656 && alias_sets_conflict_p (get_alias_set (aggr_type),
5657 get_alias_set (DR_REF (first_dr)))
5658 && (alignment_support_scheme == dr_aligned
5659 || alignment_support_scheme == dr_unaligned_supported))
5661 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5662 dataref_offset = build_int_cst (reference_alias_ptr_type
5663 (DR_REF (first_dr)), 0);
5664 inv_p = false;
5666 else
5667 dataref_ptr
5668 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
5669 offset, &dummy, gsi, &ptr_incr,
5670 simd_lane_access_p, &inv_p);
5672 else if (dataref_offset)
5673 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
5674 TYPE_SIZE_UNIT (aggr_type));
5675 else
5676 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5677 TYPE_SIZE_UNIT (aggr_type));
5679 if (grouped_load || slp_perm)
5680 dr_chain.create (vec_num);
5682 if (load_lanes_p)
5684 tree vec_array;
5686 vec_array = create_vector_array (vectype, vec_num);
5688 /* Emit:
5689 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
5690 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5691 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
5692 gimple_call_set_lhs (new_stmt, vec_array);
5693 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5695 /* Extract each vector into an SSA_NAME. */
5696 for (i = 0; i < vec_num; i++)
5698 new_temp = read_vector_array (stmt, gsi, scalar_dest,
5699 vec_array, i);
5700 dr_chain.quick_push (new_temp);
5703 /* Record the mapping between SSA_NAMEs and statements. */
5704 vect_record_grouped_load_vectors (stmt, dr_chain);
5706 else
5708 for (i = 0; i < vec_num; i++)
5710 if (i > 0)
5711 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5712 stmt, NULL_TREE);
5714 /* 2. Create the vector-load in the loop. */
5715 switch (alignment_support_scheme)
5717 case dr_aligned:
5718 case dr_unaligned_supported:
5720 unsigned int align, misalign;
5722 data_ref
5723 = build2 (MEM_REF, vectype, dataref_ptr,
5724 dataref_offset
5725 ? dataref_offset
5726 : build_int_cst (reference_alias_ptr_type
5727 (DR_REF (first_dr)), 0));
5728 align = TYPE_ALIGN_UNIT (vectype);
5729 if (alignment_support_scheme == dr_aligned)
5731 gcc_assert (aligned_access_p (first_dr));
5732 misalign = 0;
5734 else if (DR_MISALIGNMENT (first_dr) == -1)
5736 TREE_TYPE (data_ref)
5737 = build_aligned_type (TREE_TYPE (data_ref),
5738 TYPE_ALIGN (elem_type));
5739 align = TYPE_ALIGN_UNIT (elem_type);
5740 misalign = 0;
5742 else
5744 TREE_TYPE (data_ref)
5745 = build_aligned_type (TREE_TYPE (data_ref),
5746 TYPE_ALIGN (elem_type));
5747 misalign = DR_MISALIGNMENT (first_dr);
5749 if (dataref_offset == NULL_TREE)
5750 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
5751 align, misalign);
5752 break;
5754 case dr_explicit_realign:
5756 tree ptr, bump;
5757 tree vs_minus_1;
5759 vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
5761 if (compute_in_loop)
5762 msq = vect_setup_realignment (first_stmt, gsi,
5763 &realignment_token,
5764 dr_explicit_realign,
5765 dataref_ptr, NULL);
5767 ptr = copy_ssa_name (dataref_ptr, NULL);
5768 new_stmt = gimple_build_assign_with_ops
5769 (BIT_AND_EXPR, ptr, dataref_ptr,
5770 build_int_cst
5771 (TREE_TYPE (dataref_ptr),
5772 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5773 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5774 data_ref
5775 = build2 (MEM_REF, vectype, ptr,
5776 build_int_cst (reference_alias_ptr_type
5777 (DR_REF (first_dr)), 0));
5778 vec_dest = vect_create_destination_var (scalar_dest,
5779 vectype);
5780 new_stmt = gimple_build_assign (vec_dest, data_ref);
5781 new_temp = make_ssa_name (vec_dest, new_stmt);
5782 gimple_assign_set_lhs (new_stmt, new_temp);
5783 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
5784 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
5785 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5786 msq = new_temp;
5788 bump = size_binop (MULT_EXPR, vs_minus_1,
5789 TYPE_SIZE_UNIT (elem_type));
5790 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
5791 new_stmt = gimple_build_assign_with_ops
5792 (BIT_AND_EXPR, NULL_TREE, ptr,
5793 build_int_cst
5794 (TREE_TYPE (ptr),
5795 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5796 ptr = copy_ssa_name (dataref_ptr, new_stmt);
5797 gimple_assign_set_lhs (new_stmt, ptr);
5798 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5799 data_ref
5800 = build2 (MEM_REF, vectype, ptr,
5801 build_int_cst (reference_alias_ptr_type
5802 (DR_REF (first_dr)), 0));
5803 break;
5805 case dr_explicit_realign_optimized:
5806 new_temp = copy_ssa_name (dataref_ptr, NULL);
5807 new_stmt = gimple_build_assign_with_ops
5808 (BIT_AND_EXPR, new_temp, dataref_ptr,
5809 build_int_cst
5810 (TREE_TYPE (dataref_ptr),
5811 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
5812 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5813 data_ref
5814 = build2 (MEM_REF, vectype, new_temp,
5815 build_int_cst (reference_alias_ptr_type
5816 (DR_REF (first_dr)), 0));
5817 break;
5818 default:
5819 gcc_unreachable ();
5821 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5822 new_stmt = gimple_build_assign (vec_dest, data_ref);
5823 new_temp = make_ssa_name (vec_dest, new_stmt);
5824 gimple_assign_set_lhs (new_stmt, new_temp);
5825 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5827 /* 3. Handle explicit realignment if necessary/supported.
5828 Create in loop:
5829 vec_dest = realign_load (msq, lsq, realignment_token) */
5830 if (alignment_support_scheme == dr_explicit_realign_optimized
5831 || alignment_support_scheme == dr_explicit_realign)
5833 lsq = gimple_assign_lhs (new_stmt);
5834 if (!realignment_token)
5835 realignment_token = dataref_ptr;
5836 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5837 new_stmt
5838 = gimple_build_assign_with_ops (REALIGN_LOAD_EXPR,
5839 vec_dest, msq, lsq,
5840 realignment_token);
5841 new_temp = make_ssa_name (vec_dest, new_stmt);
5842 gimple_assign_set_lhs (new_stmt, new_temp);
5843 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5845 if (alignment_support_scheme == dr_explicit_realign_optimized)
5847 gcc_assert (phi);
5848 if (i == vec_num - 1 && j == ncopies - 1)
5849 add_phi_arg (phi, lsq,
5850 loop_latch_edge (containing_loop),
5851 UNKNOWN_LOCATION);
5852 msq = lsq;
5856 /* 4. Handle invariant-load. */
5857 if (inv_p && !bb_vinfo)
5859 gimple_stmt_iterator gsi2 = *gsi;
5860 gcc_assert (!grouped_load);
5861 gsi_next (&gsi2);
5862 new_temp = vect_init_vector (stmt, scalar_dest,
5863 vectype, &gsi2);
5864 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5867 if (negative)
5869 tree perm_mask = perm_mask_for_reverse (vectype);
5870 new_temp = permute_vec_elements (new_temp, new_temp,
5871 perm_mask, stmt, gsi);
5872 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5875 /* Collect vector loads and later create their permutation in
5876 vect_transform_grouped_load (). */
5877 if (grouped_load || slp_perm)
5878 dr_chain.quick_push (new_temp);
5880 /* Store vector loads in the corresponding SLP_NODE. */
5881 if (slp && !slp_perm)
5882 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5884 /* Bump the vector pointer to account for a gap. */
5885 if (slp && group_gap != 0)
5887 tree bump = size_binop (MULT_EXPR,
5888 TYPE_SIZE_UNIT (elem_type),
5889 size_int (group_gap));
5890 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5891 stmt, bump);
5895 if (slp && !slp_perm)
5896 continue;
5898 if (slp_perm)
5900 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
5901 slp_node_instance, false))
5903 dr_chain.release ();
5904 return false;
5907 else
5909 if (grouped_load)
5911 if (!load_lanes_p)
5912 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
5913 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
5915 else
5917 if (j == 0)
5918 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5919 else
5920 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5921 prev_stmt_info = vinfo_for_stmt (new_stmt);
5924 dr_chain.release ();
5927 return true;
5930 /* Function vect_is_simple_cond.
5932 Input:
5933 LOOP - the loop that is being vectorized.
5934 COND - Condition that is checked for simple use.
5936 Output:
5937 *COMP_VECTYPE - the vector type for the comparison.
5939 Returns whether a COND can be vectorized. Checks whether
5940 condition operands are supportable using vec_is_simple_use. */
5942 static bool
5943 vect_is_simple_cond (tree cond, gimple stmt, loop_vec_info loop_vinfo,
5944 bb_vec_info bb_vinfo, tree *comp_vectype)
5946 tree lhs, rhs;
5947 tree def;
5948 enum vect_def_type dt;
5949 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
5951 if (!COMPARISON_CLASS_P (cond))
5952 return false;
5954 lhs = TREE_OPERAND (cond, 0);
5955 rhs = TREE_OPERAND (cond, 1);
5957 if (TREE_CODE (lhs) == SSA_NAME)
5959 gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
5960 if (!vect_is_simple_use_1 (lhs, stmt, loop_vinfo, bb_vinfo,
5961 &lhs_def_stmt, &def, &dt, &vectype1))
5962 return false;
5964 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
5965 && TREE_CODE (lhs) != FIXED_CST)
5966 return false;
5968 if (TREE_CODE (rhs) == SSA_NAME)
5970 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
5971 if (!vect_is_simple_use_1 (rhs, stmt, loop_vinfo, bb_vinfo,
5972 &rhs_def_stmt, &def, &dt, &vectype2))
5973 return false;
5975 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
5976 && TREE_CODE (rhs) != FIXED_CST)
5977 return false;
5979 *comp_vectype = vectype1 ? vectype1 : vectype2;
5980 return true;
5983 /* vectorizable_condition.
5985 Check if STMT is conditional modify expression that can be vectorized.
5986 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5987 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
5988 at GSI.
5990 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
5991 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
5992 else caluse if it is 2).
5994 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5996 bool
5997 vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
5998 gimple *vec_stmt, tree reduc_def, int reduc_index,
5999 slp_tree slp_node)
6001 tree scalar_dest = NULL_TREE;
6002 tree vec_dest = NULL_TREE;
6003 tree cond_expr, then_clause, else_clause;
6004 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6005 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6006 tree comp_vectype = NULL_TREE;
6007 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
6008 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
6009 tree vec_compare, vec_cond_expr;
6010 tree new_temp;
6011 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6012 tree def;
6013 enum vect_def_type dt, dts[4];
6014 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6015 int ncopies;
6016 enum tree_code code;
6017 stmt_vec_info prev_stmt_info = NULL;
6018 int i, j;
6019 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6020 vec<tree> vec_oprnds0 = vNULL;
6021 vec<tree> vec_oprnds1 = vNULL;
6022 vec<tree> vec_oprnds2 = vNULL;
6023 vec<tree> vec_oprnds3 = vNULL;
6024 tree vec_cmp_type;
6026 if (slp_node || PURE_SLP_STMT (stmt_info))
6027 ncopies = 1;
6028 else
6029 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6031 gcc_assert (ncopies >= 1);
6032 if (reduc_index && ncopies > 1)
6033 return false; /* FORNOW */
6035 if (reduc_index && STMT_SLP_TYPE (stmt_info))
6036 return false;
6038 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6039 return false;
6041 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6042 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
6043 && reduc_def))
6044 return false;
6046 /* FORNOW: not yet supported. */
6047 if (STMT_VINFO_LIVE_P (stmt_info))
6049 if (dump_enabled_p ())
6050 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6051 "value used after loop.\n");
6052 return false;
6055 /* Is vectorizable conditional operation? */
6056 if (!is_gimple_assign (stmt))
6057 return false;
6059 code = gimple_assign_rhs_code (stmt);
6061 if (code != COND_EXPR)
6062 return false;
6064 cond_expr = gimple_assign_rhs1 (stmt);
6065 then_clause = gimple_assign_rhs2 (stmt);
6066 else_clause = gimple_assign_rhs3 (stmt);
6068 if (!vect_is_simple_cond (cond_expr, stmt, loop_vinfo, bb_vinfo,
6069 &comp_vectype)
6070 || !comp_vectype)
6071 return false;
6073 if (TREE_CODE (then_clause) == SSA_NAME)
6075 gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
6076 if (!vect_is_simple_use (then_clause, stmt, loop_vinfo, bb_vinfo,
6077 &then_def_stmt, &def, &dt))
6078 return false;
6080 else if (TREE_CODE (then_clause) != INTEGER_CST
6081 && TREE_CODE (then_clause) != REAL_CST
6082 && TREE_CODE (then_clause) != FIXED_CST)
6083 return false;
6085 if (TREE_CODE (else_clause) == SSA_NAME)
6087 gimple else_def_stmt = SSA_NAME_DEF_STMT (else_clause);
6088 if (!vect_is_simple_use (else_clause, stmt, loop_vinfo, bb_vinfo,
6089 &else_def_stmt, &def, &dt))
6090 return false;
6092 else if (TREE_CODE (else_clause) != INTEGER_CST
6093 && TREE_CODE (else_clause) != REAL_CST
6094 && TREE_CODE (else_clause) != FIXED_CST)
6095 return false;
6097 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype)));
6098 /* The result of a vector comparison should be signed type. */
6099 tree cmp_type = build_nonstandard_integer_type (prec, 0);
6100 vec_cmp_type = get_same_sized_vectype (cmp_type, vectype);
6101 if (vec_cmp_type == NULL_TREE)
6102 return false;
6104 if (!vec_stmt)
6106 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
6107 return expand_vec_cond_expr_p (vectype, comp_vectype);
6110 /* Transform. */
6112 if (!slp_node)
6114 vec_oprnds0.create (1);
6115 vec_oprnds1.create (1);
6116 vec_oprnds2.create (1);
6117 vec_oprnds3.create (1);
6120 /* Handle def. */
6121 scalar_dest = gimple_assign_lhs (stmt);
6122 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6124 /* Handle cond expr. */
6125 for (j = 0; j < ncopies; j++)
6127 gimple new_stmt = NULL;
6128 if (j == 0)
6130 if (slp_node)
6132 stack_vec<tree, 4> ops;
6133 stack_vec<vec<tree>, 4> vec_defs;
6135 ops.safe_push (TREE_OPERAND (cond_expr, 0));
6136 ops.safe_push (TREE_OPERAND (cond_expr, 1));
6137 ops.safe_push (then_clause);
6138 ops.safe_push (else_clause);
6139 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
6140 vec_oprnds3 = vec_defs.pop ();
6141 vec_oprnds2 = vec_defs.pop ();
6142 vec_oprnds1 = vec_defs.pop ();
6143 vec_oprnds0 = vec_defs.pop ();
6145 ops.release ();
6146 vec_defs.release ();
6148 else
6150 gimple gtemp;
6151 vec_cond_lhs =
6152 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
6153 stmt, NULL);
6154 vect_is_simple_use (TREE_OPERAND (cond_expr, 0), stmt,
6155 loop_vinfo, NULL, &gtemp, &def, &dts[0]);
6157 vec_cond_rhs =
6158 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
6159 stmt, NULL);
6160 vect_is_simple_use (TREE_OPERAND (cond_expr, 1), stmt,
6161 loop_vinfo, NULL, &gtemp, &def, &dts[1]);
6162 if (reduc_index == 1)
6163 vec_then_clause = reduc_def;
6164 else
6166 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
6167 stmt, NULL);
6168 vect_is_simple_use (then_clause, stmt, loop_vinfo,
6169 NULL, &gtemp, &def, &dts[2]);
6171 if (reduc_index == 2)
6172 vec_else_clause = reduc_def;
6173 else
6175 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
6176 stmt, NULL);
6177 vect_is_simple_use (else_clause, stmt, loop_vinfo,
6178 NULL, &gtemp, &def, &dts[3]);
6182 else
6184 vec_cond_lhs = vect_get_vec_def_for_stmt_copy (dts[0],
6185 vec_oprnds0.pop ());
6186 vec_cond_rhs = vect_get_vec_def_for_stmt_copy (dts[1],
6187 vec_oprnds1.pop ());
6188 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
6189 vec_oprnds2.pop ());
6190 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
6191 vec_oprnds3.pop ());
6194 if (!slp_node)
6196 vec_oprnds0.quick_push (vec_cond_lhs);
6197 vec_oprnds1.quick_push (vec_cond_rhs);
6198 vec_oprnds2.quick_push (vec_then_clause);
6199 vec_oprnds3.quick_push (vec_else_clause);
6202 /* Arguments are ready. Create the new vector stmt. */
6203 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
6205 vec_cond_rhs = vec_oprnds1[i];
6206 vec_then_clause = vec_oprnds2[i];
6207 vec_else_clause = vec_oprnds3[i];
6209 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
6210 vec_cond_lhs, vec_cond_rhs);
6211 vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
6212 vec_compare, vec_then_clause, vec_else_clause);
6214 new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
6215 new_temp = make_ssa_name (vec_dest, new_stmt);
6216 gimple_assign_set_lhs (new_stmt, new_temp);
6217 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6218 if (slp_node)
6219 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6222 if (slp_node)
6223 continue;
6225 if (j == 0)
6226 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6227 else
6228 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6230 prev_stmt_info = vinfo_for_stmt (new_stmt);
6233 vec_oprnds0.release ();
6234 vec_oprnds1.release ();
6235 vec_oprnds2.release ();
6236 vec_oprnds3.release ();
6238 return true;
6242 /* Make sure the statement is vectorizable. */
6244 bool
6245 vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
6247 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6248 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6249 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
6250 bool ok;
6251 tree scalar_type, vectype;
6252 gimple pattern_stmt;
6253 gimple_seq pattern_def_seq;
6255 if (dump_enabled_p ())
6257 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
6258 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6259 dump_printf (MSG_NOTE, "\n");
6262 if (gimple_has_volatile_ops (stmt))
6264 if (dump_enabled_p ())
6265 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6266 "not vectorized: stmt has volatile operands\n");
6268 return false;
6271 /* Skip stmts that do not need to be vectorized. In loops this is expected
6272 to include:
6273 - the COND_EXPR which is the loop exit condition
6274 - any LABEL_EXPRs in the loop
6275 - computations that are used only for array indexing or loop control.
6276 In basic blocks we only analyze statements that are a part of some SLP
6277 instance, therefore, all the statements are relevant.
6279 Pattern statement needs to be analyzed instead of the original statement
6280 if the original statement is not relevant. Otherwise, we analyze both
6281 statements. In basic blocks we are called from some SLP instance
6282 traversal, don't analyze pattern stmts instead, the pattern stmts
6283 already will be part of SLP instance. */
6285 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
6286 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6287 && !STMT_VINFO_LIVE_P (stmt_info))
6289 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6290 && pattern_stmt
6291 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6292 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6294 /* Analyze PATTERN_STMT instead of the original stmt. */
6295 stmt = pattern_stmt;
6296 stmt_info = vinfo_for_stmt (pattern_stmt);
6297 if (dump_enabled_p ())
6299 dump_printf_loc (MSG_NOTE, vect_location,
6300 "==> examining pattern statement: ");
6301 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6302 dump_printf (MSG_NOTE, "\n");
6305 else
6307 if (dump_enabled_p ())
6308 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
6310 return true;
6313 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6314 && node == NULL
6315 && pattern_stmt
6316 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6317 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6319 /* Analyze PATTERN_STMT too. */
6320 if (dump_enabled_p ())
6322 dump_printf_loc (MSG_NOTE, vect_location,
6323 "==> examining pattern statement: ");
6324 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6325 dump_printf (MSG_NOTE, "\n");
6328 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
6329 return false;
6332 if (is_pattern_stmt_p (stmt_info)
6333 && node == NULL
6334 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
6336 gimple_stmt_iterator si;
6338 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
6340 gimple pattern_def_stmt = gsi_stmt (si);
6341 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
6342 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
6344 /* Analyze def stmt of STMT if it's a pattern stmt. */
6345 if (dump_enabled_p ())
6347 dump_printf_loc (MSG_NOTE, vect_location,
6348 "==> examining pattern def statement: ");
6349 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
6350 dump_printf (MSG_NOTE, "\n");
6353 if (!vect_analyze_stmt (pattern_def_stmt,
6354 need_to_vectorize, node))
6355 return false;
6360 switch (STMT_VINFO_DEF_TYPE (stmt_info))
6362 case vect_internal_def:
6363 break;
6365 case vect_reduction_def:
6366 case vect_nested_cycle:
6367 gcc_assert (!bb_vinfo && (relevance == vect_used_in_outer
6368 || relevance == vect_used_in_outer_by_reduction
6369 || relevance == vect_unused_in_scope));
6370 break;
6372 case vect_induction_def:
6373 case vect_constant_def:
6374 case vect_external_def:
6375 case vect_unknown_def_type:
6376 default:
6377 gcc_unreachable ();
6380 if (bb_vinfo)
6382 gcc_assert (PURE_SLP_STMT (stmt_info));
6384 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
6385 if (dump_enabled_p ())
6387 dump_printf_loc (MSG_NOTE, vect_location,
6388 "get vectype for scalar type: ");
6389 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
6390 dump_printf (MSG_NOTE, "\n");
6393 vectype = get_vectype_for_scalar_type (scalar_type);
6394 if (!vectype)
6396 if (dump_enabled_p ())
6398 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6399 "not SLPed: unsupported data-type ");
6400 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
6401 scalar_type);
6402 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6404 return false;
6407 if (dump_enabled_p ())
6409 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
6410 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
6411 dump_printf (MSG_NOTE, "\n");
6414 STMT_VINFO_VECTYPE (stmt_info) = vectype;
6417 if (STMT_VINFO_RELEVANT_P (stmt_info))
6419 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
6420 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
6421 || (is_gimple_call (stmt)
6422 && gimple_call_lhs (stmt) == NULL_TREE));
6423 *need_to_vectorize = true;
6426 ok = true;
6427 if (!bb_vinfo
6428 && (STMT_VINFO_RELEVANT_P (stmt_info)
6429 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
6430 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, NULL)
6431 || vectorizable_conversion (stmt, NULL, NULL, NULL)
6432 || vectorizable_shift (stmt, NULL, NULL, NULL)
6433 || vectorizable_operation (stmt, NULL, NULL, NULL)
6434 || vectorizable_assignment (stmt, NULL, NULL, NULL)
6435 || vectorizable_load (stmt, NULL, NULL, NULL, NULL)
6436 || vectorizable_call (stmt, NULL, NULL, NULL)
6437 || vectorizable_store (stmt, NULL, NULL, NULL)
6438 || vectorizable_reduction (stmt, NULL, NULL, NULL)
6439 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, NULL));
6440 else
6442 if (bb_vinfo)
6443 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
6444 || vectorizable_conversion (stmt, NULL, NULL, node)
6445 || vectorizable_shift (stmt, NULL, NULL, node)
6446 || vectorizable_operation (stmt, NULL, NULL, node)
6447 || vectorizable_assignment (stmt, NULL, NULL, node)
6448 || vectorizable_load (stmt, NULL, NULL, node, NULL)
6449 || vectorizable_call (stmt, NULL, NULL, node)
6450 || vectorizable_store (stmt, NULL, NULL, node)
6451 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node));
6454 if (!ok)
6456 if (dump_enabled_p ())
6458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6459 "not vectorized: relevant stmt not ");
6460 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
6461 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
6462 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6465 return false;
6468 if (bb_vinfo)
6469 return true;
6471 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
6472 need extra handling, except for vectorizable reductions. */
6473 if (STMT_VINFO_LIVE_P (stmt_info)
6474 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
6475 ok = vectorizable_live_operation (stmt, NULL, NULL);
6477 if (!ok)
6479 if (dump_enabled_p ())
6481 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6482 "not vectorized: live stmt not ");
6483 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
6484 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
6485 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6488 return false;
6491 return true;
6495 /* Function vect_transform_stmt.
6497 Create a vectorized stmt to replace STMT, and insert it at BSI. */
6499 bool
6500 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
6501 bool *grouped_store, slp_tree slp_node,
6502 slp_instance slp_node_instance)
6504 bool is_store = false;
6505 gimple vec_stmt = NULL;
6506 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6507 bool done;
6509 switch (STMT_VINFO_TYPE (stmt_info))
6511 case type_demotion_vec_info_type:
6512 case type_promotion_vec_info_type:
6513 case type_conversion_vec_info_type:
6514 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
6515 gcc_assert (done);
6516 break;
6518 case induc_vec_info_type:
6519 gcc_assert (!slp_node);
6520 done = vectorizable_induction (stmt, gsi, &vec_stmt);
6521 gcc_assert (done);
6522 break;
6524 case shift_vec_info_type:
6525 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
6526 gcc_assert (done);
6527 break;
6529 case op_vec_info_type:
6530 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
6531 gcc_assert (done);
6532 break;
6534 case assignment_vec_info_type:
6535 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
6536 gcc_assert (done);
6537 break;
6539 case load_vec_info_type:
6540 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
6541 slp_node_instance);
6542 gcc_assert (done);
6543 break;
6545 case store_vec_info_type:
6546 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
6547 gcc_assert (done);
6548 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
6550 /* In case of interleaving, the whole chain is vectorized when the
6551 last store in the chain is reached. Store stmts before the last
6552 one are skipped, and there vec_stmt_info shouldn't be freed
6553 meanwhile. */
6554 *grouped_store = true;
6555 if (STMT_VINFO_VEC_STMT (stmt_info))
6556 is_store = true;
6558 else
6559 is_store = true;
6560 break;
6562 case condition_vec_info_type:
6563 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
6564 gcc_assert (done);
6565 break;
6567 case call_vec_info_type:
6568 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
6569 stmt = gsi_stmt (*gsi);
6570 break;
6572 case call_simd_clone_vec_info_type:
6573 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
6574 stmt = gsi_stmt (*gsi);
6575 break;
6577 case reduc_vec_info_type:
6578 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
6579 gcc_assert (done);
6580 break;
6582 default:
6583 if (!STMT_VINFO_LIVE_P (stmt_info))
6585 if (dump_enabled_p ())
6586 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6587 "stmt not supported.\n");
6588 gcc_unreachable ();
6592 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
6593 is being vectorized, but outside the immediately enclosing loop. */
6594 if (vec_stmt
6595 && STMT_VINFO_LOOP_VINFO (stmt_info)
6596 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
6597 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
6598 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
6599 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
6600 || STMT_VINFO_RELEVANT (stmt_info) ==
6601 vect_used_in_outer_by_reduction))
6603 struct loop *innerloop = LOOP_VINFO_LOOP (
6604 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
6605 imm_use_iterator imm_iter;
6606 use_operand_p use_p;
6607 tree scalar_dest;
6608 gimple exit_phi;
6610 if (dump_enabled_p ())
6611 dump_printf_loc (MSG_NOTE, vect_location,
6612 "Record the vdef for outer-loop vectorization.\n");
6614 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
6615 (to be used when vectorizing outer-loop stmts that use the DEF of
6616 STMT). */
6617 if (gimple_code (stmt) == GIMPLE_PHI)
6618 scalar_dest = PHI_RESULT (stmt);
6619 else
6620 scalar_dest = gimple_assign_lhs (stmt);
6622 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
6624 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
6626 exit_phi = USE_STMT (use_p);
6627 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
6632 /* Handle stmts whose DEF is used outside the loop-nest that is
6633 being vectorized. */
6634 if (STMT_VINFO_LIVE_P (stmt_info)
6635 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
6637 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
6638 gcc_assert (done);
6641 if (vec_stmt)
6642 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
6644 return is_store;
6648 /* Remove a group of stores (for SLP or interleaving), free their
6649 stmt_vec_info. */
6651 void
6652 vect_remove_stores (gimple first_stmt)
6654 gimple next = first_stmt;
6655 gimple tmp;
6656 gimple_stmt_iterator next_si;
6658 while (next)
6660 stmt_vec_info stmt_info = vinfo_for_stmt (next);
6662 tmp = GROUP_NEXT_ELEMENT (stmt_info);
6663 if (is_pattern_stmt_p (stmt_info))
6664 next = STMT_VINFO_RELATED_STMT (stmt_info);
6665 /* Free the attached stmt_vec_info and remove the stmt. */
6666 next_si = gsi_for_stmt (next);
6667 unlink_stmt_vdef (next);
6668 gsi_remove (&next_si, true);
6669 release_defs (next);
6670 free_stmt_vec_info (next);
6671 next = tmp;
6676 /* Function new_stmt_vec_info.
6678 Create and initialize a new stmt_vec_info struct for STMT. */
6680 stmt_vec_info
6681 new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
6682 bb_vec_info bb_vinfo)
6684 stmt_vec_info res;
6685 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
6687 STMT_VINFO_TYPE (res) = undef_vec_info_type;
6688 STMT_VINFO_STMT (res) = stmt;
6689 STMT_VINFO_LOOP_VINFO (res) = loop_vinfo;
6690 STMT_VINFO_BB_VINFO (res) = bb_vinfo;
6691 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
6692 STMT_VINFO_LIVE_P (res) = false;
6693 STMT_VINFO_VECTYPE (res) = NULL;
6694 STMT_VINFO_VEC_STMT (res) = NULL;
6695 STMT_VINFO_VECTORIZABLE (res) = true;
6696 STMT_VINFO_IN_PATTERN_P (res) = false;
6697 STMT_VINFO_RELATED_STMT (res) = NULL;
6698 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
6699 STMT_VINFO_DATA_REF (res) = NULL;
6701 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
6702 STMT_VINFO_DR_OFFSET (res) = NULL;
6703 STMT_VINFO_DR_INIT (res) = NULL;
6704 STMT_VINFO_DR_STEP (res) = NULL;
6705 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
6707 if (gimple_code (stmt) == GIMPLE_PHI
6708 && is_loop_header_bb_p (gimple_bb (stmt)))
6709 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
6710 else
6711 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
6713 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
6714 STMT_SLP_TYPE (res) = loop_vect;
6715 GROUP_FIRST_ELEMENT (res) = NULL;
6716 GROUP_NEXT_ELEMENT (res) = NULL;
6717 GROUP_SIZE (res) = 0;
6718 GROUP_STORE_COUNT (res) = 0;
6719 GROUP_GAP (res) = 0;
6720 GROUP_SAME_DR_STMT (res) = NULL;
6722 return res;
6726 /* Create a hash table for stmt_vec_info. */
6728 void
6729 init_stmt_vec_info_vec (void)
6731 gcc_assert (!stmt_vec_info_vec.exists ());
6732 stmt_vec_info_vec.create (50);
6736 /* Free hash table for stmt_vec_info. */
6738 void
6739 free_stmt_vec_info_vec (void)
6741 unsigned int i;
6742 vec_void_p info;
6743 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
6744 if (info != NULL)
6745 free_stmt_vec_info (STMT_VINFO_STMT ((stmt_vec_info) info));
6746 gcc_assert (stmt_vec_info_vec.exists ());
6747 stmt_vec_info_vec.release ();
6751 /* Free stmt vectorization related info. */
6753 void
6754 free_stmt_vec_info (gimple stmt)
6756 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6758 if (!stmt_info)
6759 return;
6761 /* Check if this statement has a related "pattern stmt"
6762 (introduced by the vectorizer during the pattern recognition
6763 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
6764 too. */
6765 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
6767 stmt_vec_info patt_info
6768 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
6769 if (patt_info)
6771 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
6772 if (seq)
6774 gimple_stmt_iterator si;
6775 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
6776 free_stmt_vec_info (gsi_stmt (si));
6778 free_stmt_vec_info (STMT_VINFO_RELATED_STMT (stmt_info));
6782 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
6783 set_vinfo_for_stmt (stmt, NULL);
6784 free (stmt_info);
6788 /* Function get_vectype_for_scalar_type_and_size.
6790 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
6791 by the target. */
6793 static tree
6794 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
6796 enum machine_mode inner_mode = TYPE_MODE (scalar_type);
6797 enum machine_mode simd_mode;
6798 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
6799 int nunits;
6800 tree vectype;
6802 if (nbytes == 0)
6803 return NULL_TREE;
6805 if (GET_MODE_CLASS (inner_mode) != MODE_INT
6806 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
6807 return NULL_TREE;
6809 /* For vector types of elements whose mode precision doesn't
6810 match their types precision we use a element type of mode
6811 precision. The vectorization routines will have to make sure
6812 they support the proper result truncation/extension.
6813 We also make sure to build vector types with INTEGER_TYPE
6814 component type only. */
6815 if (INTEGRAL_TYPE_P (scalar_type)
6816 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
6817 || TREE_CODE (scalar_type) != INTEGER_TYPE))
6818 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
6819 TYPE_UNSIGNED (scalar_type));
6821 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
6822 When the component mode passes the above test simply use a type
6823 corresponding to that mode. The theory is that any use that
6824 would cause problems with this will disable vectorization anyway. */
6825 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
6826 && !INTEGRAL_TYPE_P (scalar_type))
6827 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
6829 /* We can't build a vector type of elements with alignment bigger than
6830 their size. */
6831 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
6832 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
6833 TYPE_UNSIGNED (scalar_type));
6835 /* If we felt back to using the mode fail if there was
6836 no scalar type for it. */
6837 if (scalar_type == NULL_TREE)
6838 return NULL_TREE;
6840 /* If no size was supplied use the mode the target prefers. Otherwise
6841 lookup a vector mode of the specified size. */
6842 if (size == 0)
6843 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
6844 else
6845 simd_mode = mode_for_vector (inner_mode, size / nbytes);
6846 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
6847 if (nunits <= 1)
6848 return NULL_TREE;
6850 vectype = build_vector_type (scalar_type, nunits);
6852 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
6853 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
6854 return NULL_TREE;
6856 return vectype;
6859 unsigned int current_vector_size;
6861 /* Function get_vectype_for_scalar_type.
6863 Returns the vector type corresponding to SCALAR_TYPE as supported
6864 by the target. */
6866 tree
6867 get_vectype_for_scalar_type (tree scalar_type)
6869 tree vectype;
6870 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
6871 current_vector_size);
6872 if (vectype
6873 && current_vector_size == 0)
6874 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
6875 return vectype;
6878 /* Function get_same_sized_vectype
6880 Returns a vector type corresponding to SCALAR_TYPE of size
6881 VECTOR_TYPE if supported by the target. */
6883 tree
6884 get_same_sized_vectype (tree scalar_type, tree vector_type)
6886 return get_vectype_for_scalar_type_and_size
6887 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
6890 /* Function vect_is_simple_use.
6892 Input:
6893 LOOP_VINFO - the vect info of the loop that is being vectorized.
6894 BB_VINFO - the vect info of the basic block that is being vectorized.
6895 OPERAND - operand of STMT in the loop or bb.
6896 DEF - the defining stmt in case OPERAND is an SSA_NAME.
6898 Returns whether a stmt with OPERAND can be vectorized.
6899 For loops, supportable operands are constants, loop invariants, and operands
6900 that are defined by the current iteration of the loop. Unsupportable
6901 operands are those that are defined by a previous iteration of the loop (as
6902 is the case in reduction/induction computations).
6903 For basic blocks, supportable operands are constants and bb invariants.
6904 For now, operands defined outside the basic block are not supported. */
6906 bool
6907 vect_is_simple_use (tree operand, gimple stmt, loop_vec_info loop_vinfo,
6908 bb_vec_info bb_vinfo, gimple *def_stmt,
6909 tree *def, enum vect_def_type *dt)
6911 basic_block bb;
6912 stmt_vec_info stmt_vinfo;
6913 struct loop *loop = NULL;
6915 if (loop_vinfo)
6916 loop = LOOP_VINFO_LOOP (loop_vinfo);
6918 *def_stmt = NULL;
6919 *def = NULL_TREE;
6921 if (dump_enabled_p ())
6923 dump_printf_loc (MSG_NOTE, vect_location,
6924 "vect_is_simple_use: operand ");
6925 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
6926 dump_printf (MSG_NOTE, "\n");
6929 if (CONSTANT_CLASS_P (operand))
6931 *dt = vect_constant_def;
6932 return true;
6935 if (is_gimple_min_invariant (operand))
6937 *def = operand;
6938 *dt = vect_external_def;
6939 return true;
6942 if (TREE_CODE (operand) == PAREN_EXPR)
6944 if (dump_enabled_p ())
6945 dump_printf_loc (MSG_NOTE, vect_location, "non-associatable copy.\n");
6946 operand = TREE_OPERAND (operand, 0);
6949 if (TREE_CODE (operand) != SSA_NAME)
6951 if (dump_enabled_p ())
6952 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6953 "not ssa-name.\n");
6954 return false;
6957 *def_stmt = SSA_NAME_DEF_STMT (operand);
6958 if (*def_stmt == NULL)
6960 if (dump_enabled_p ())
6961 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6962 "no def_stmt.\n");
6963 return false;
6966 if (dump_enabled_p ())
6968 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
6969 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
6970 dump_printf (MSG_NOTE, "\n");
6973 /* Empty stmt is expected only in case of a function argument.
6974 (Otherwise - we expect a phi_node or a GIMPLE_ASSIGN). */
6975 if (gimple_nop_p (*def_stmt))
6977 *def = operand;
6978 *dt = vect_external_def;
6979 return true;
6982 bb = gimple_bb (*def_stmt);
6984 if ((loop && !flow_bb_inside_loop_p (loop, bb))
6985 || (!loop && bb != BB_VINFO_BB (bb_vinfo))
6986 || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI))
6987 *dt = vect_external_def;
6988 else
6990 stmt_vinfo = vinfo_for_stmt (*def_stmt);
6991 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
6994 if (*dt == vect_unknown_def_type
6995 || (stmt
6996 && *dt == vect_double_reduction_def
6997 && gimple_code (stmt) != GIMPLE_PHI))
6999 if (dump_enabled_p ())
7000 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7001 "Unsupported pattern.\n");
7002 return false;
7005 if (dump_enabled_p ())
7006 dump_printf_loc (MSG_NOTE, vect_location, "type of def: %d.\n", *dt);
7008 switch (gimple_code (*def_stmt))
7010 case GIMPLE_PHI:
7011 *def = gimple_phi_result (*def_stmt);
7012 break;
7014 case GIMPLE_ASSIGN:
7015 *def = gimple_assign_lhs (*def_stmt);
7016 break;
7018 case GIMPLE_CALL:
7019 *def = gimple_call_lhs (*def_stmt);
7020 if (*def != NULL)
7021 break;
7022 /* FALLTHRU */
7023 default:
7024 if (dump_enabled_p ())
7025 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7026 "unsupported defining stmt:\n");
7027 return false;
7030 return true;
7033 /* Function vect_is_simple_use_1.
7035 Same as vect_is_simple_use_1 but also determines the vector operand
7036 type of OPERAND and stores it to *VECTYPE. If the definition of
7037 OPERAND is vect_uninitialized_def, vect_constant_def or
7038 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
7039 is responsible to compute the best suited vector type for the
7040 scalar operand. */
7042 bool
7043 vect_is_simple_use_1 (tree operand, gimple stmt, loop_vec_info loop_vinfo,
7044 bb_vec_info bb_vinfo, gimple *def_stmt,
7045 tree *def, enum vect_def_type *dt, tree *vectype)
7047 if (!vect_is_simple_use (operand, stmt, loop_vinfo, bb_vinfo, def_stmt,
7048 def, dt))
7049 return false;
7051 /* Now get a vector type if the def is internal, otherwise supply
7052 NULL_TREE and leave it up to the caller to figure out a proper
7053 type for the use stmt. */
7054 if (*dt == vect_internal_def
7055 || *dt == vect_induction_def
7056 || *dt == vect_reduction_def
7057 || *dt == vect_double_reduction_def
7058 || *dt == vect_nested_cycle)
7060 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
7062 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7063 && !STMT_VINFO_RELEVANT (stmt_info)
7064 && !STMT_VINFO_LIVE_P (stmt_info))
7065 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
7067 *vectype = STMT_VINFO_VECTYPE (stmt_info);
7068 gcc_assert (*vectype != NULL_TREE);
7070 else if (*dt == vect_uninitialized_def
7071 || *dt == vect_constant_def
7072 || *dt == vect_external_def)
7073 *vectype = NULL_TREE;
7074 else
7075 gcc_unreachable ();
7077 return true;
7081 /* Function supportable_widening_operation
7083 Check whether an operation represented by the code CODE is a
7084 widening operation that is supported by the target platform in
7085 vector form (i.e., when operating on arguments of type VECTYPE_IN
7086 producing a result of type VECTYPE_OUT).
7088 Widening operations we currently support are NOP (CONVERT), FLOAT
7089 and WIDEN_MULT. This function checks if these operations are supported
7090 by the target platform either directly (via vector tree-codes), or via
7091 target builtins.
7093 Output:
7094 - CODE1 and CODE2 are codes of vector operations to be used when
7095 vectorizing the operation, if available.
7096 - MULTI_STEP_CVT determines the number of required intermediate steps in
7097 case of multi-step conversion (like char->short->int - in that case
7098 MULTI_STEP_CVT will be 1).
7099 - INTERM_TYPES contains the intermediate type required to perform the
7100 widening operation (short in the above example). */
7102 bool
7103 supportable_widening_operation (enum tree_code code, gimple stmt,
7104 tree vectype_out, tree vectype_in,
7105 enum tree_code *code1, enum tree_code *code2,
7106 int *multi_step_cvt,
7107 vec<tree> *interm_types)
7109 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7110 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
7111 struct loop *vect_loop = NULL;
7112 enum machine_mode vec_mode;
7113 enum insn_code icode1, icode2;
7114 optab optab1, optab2;
7115 tree vectype = vectype_in;
7116 tree wide_vectype = vectype_out;
7117 enum tree_code c1, c2;
7118 int i;
7119 tree prev_type, intermediate_type;
7120 enum machine_mode intermediate_mode, prev_mode;
7121 optab optab3, optab4;
7123 *multi_step_cvt = 0;
7124 if (loop_info)
7125 vect_loop = LOOP_VINFO_LOOP (loop_info);
7127 switch (code)
7129 case WIDEN_MULT_EXPR:
7130 /* The result of a vectorized widening operation usually requires
7131 two vectors (because the widened results do not fit into one vector).
7132 The generated vector results would normally be expected to be
7133 generated in the same order as in the original scalar computation,
7134 i.e. if 8 results are generated in each vector iteration, they are
7135 to be organized as follows:
7136 vect1: [res1,res2,res3,res4],
7137 vect2: [res5,res6,res7,res8].
7139 However, in the special case that the result of the widening
7140 operation is used in a reduction computation only, the order doesn't
7141 matter (because when vectorizing a reduction we change the order of
7142 the computation). Some targets can take advantage of this and
7143 generate more efficient code. For example, targets like Altivec,
7144 that support widen_mult using a sequence of {mult_even,mult_odd}
7145 generate the following vectors:
7146 vect1: [res1,res3,res5,res7],
7147 vect2: [res2,res4,res6,res8].
7149 When vectorizing outer-loops, we execute the inner-loop sequentially
7150 (each vectorized inner-loop iteration contributes to VF outer-loop
7151 iterations in parallel). We therefore don't allow to change the
7152 order of the computation in the inner-loop during outer-loop
7153 vectorization. */
7154 /* TODO: Another case in which order doesn't *really* matter is when we
7155 widen and then contract again, e.g. (short)((int)x * y >> 8).
7156 Normally, pack_trunc performs an even/odd permute, whereas the
7157 repack from an even/odd expansion would be an interleave, which
7158 would be significantly simpler for e.g. AVX2. */
7159 /* In any case, in order to avoid duplicating the code below, recurse
7160 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
7161 are properly set up for the caller. If we fail, we'll continue with
7162 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
7163 if (vect_loop
7164 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
7165 && !nested_in_vect_loop_p (vect_loop, stmt)
7166 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
7167 stmt, vectype_out, vectype_in,
7168 code1, code2, multi_step_cvt,
7169 interm_types))
7170 return true;
7171 c1 = VEC_WIDEN_MULT_LO_EXPR;
7172 c2 = VEC_WIDEN_MULT_HI_EXPR;
7173 break;
7175 case VEC_WIDEN_MULT_EVEN_EXPR:
7176 /* Support the recursion induced just above. */
7177 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
7178 c2 = VEC_WIDEN_MULT_ODD_EXPR;
7179 break;
7181 case WIDEN_LSHIFT_EXPR:
7182 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
7183 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
7184 break;
7186 CASE_CONVERT:
7187 c1 = VEC_UNPACK_LO_EXPR;
7188 c2 = VEC_UNPACK_HI_EXPR;
7189 break;
7191 case FLOAT_EXPR:
7192 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
7193 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
7194 break;
7196 case FIX_TRUNC_EXPR:
7197 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
7198 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
7199 computing the operation. */
7200 return false;
7202 default:
7203 gcc_unreachable ();
7206 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
7208 enum tree_code ctmp = c1;
7209 c1 = c2;
7210 c2 = ctmp;
7213 if (code == FIX_TRUNC_EXPR)
7215 /* The signedness is determined from output operand. */
7216 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
7217 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
7219 else
7221 optab1 = optab_for_tree_code (c1, vectype, optab_default);
7222 optab2 = optab_for_tree_code (c2, vectype, optab_default);
7225 if (!optab1 || !optab2)
7226 return false;
7228 vec_mode = TYPE_MODE (vectype);
7229 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
7230 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
7231 return false;
7233 *code1 = c1;
7234 *code2 = c2;
7236 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
7237 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
7238 return true;
7240 /* Check if it's a multi-step conversion that can be done using intermediate
7241 types. */
7243 prev_type = vectype;
7244 prev_mode = vec_mode;
7246 if (!CONVERT_EXPR_CODE_P (code))
7247 return false;
7249 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
7250 intermediate steps in promotion sequence. We try
7251 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
7252 not. */
7253 interm_types->create (MAX_INTERM_CVT_STEPS);
7254 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
7256 intermediate_mode = insn_data[icode1].operand[0].mode;
7257 intermediate_type
7258 = lang_hooks.types.type_for_mode (intermediate_mode,
7259 TYPE_UNSIGNED (prev_type));
7260 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
7261 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
7263 if (!optab3 || !optab4
7264 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
7265 || insn_data[icode1].operand[0].mode != intermediate_mode
7266 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
7267 || insn_data[icode2].operand[0].mode != intermediate_mode
7268 || ((icode1 = optab_handler (optab3, intermediate_mode))
7269 == CODE_FOR_nothing)
7270 || ((icode2 = optab_handler (optab4, intermediate_mode))
7271 == CODE_FOR_nothing))
7272 break;
7274 interm_types->quick_push (intermediate_type);
7275 (*multi_step_cvt)++;
7277 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
7278 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
7279 return true;
7281 prev_type = intermediate_type;
7282 prev_mode = intermediate_mode;
7285 interm_types->release ();
7286 return false;
7290 /* Function supportable_narrowing_operation
7292 Check whether an operation represented by the code CODE is a
7293 narrowing operation that is supported by the target platform in
7294 vector form (i.e., when operating on arguments of type VECTYPE_IN
7295 and producing a result of type VECTYPE_OUT).
7297 Narrowing operations we currently support are NOP (CONVERT) and
7298 FIX_TRUNC. This function checks if these operations are supported by
7299 the target platform directly via vector tree-codes.
7301 Output:
7302 - CODE1 is the code of a vector operation to be used when
7303 vectorizing the operation, if available.
7304 - MULTI_STEP_CVT determines the number of required intermediate steps in
7305 case of multi-step conversion (like int->short->char - in that case
7306 MULTI_STEP_CVT will be 1).
7307 - INTERM_TYPES contains the intermediate type required to perform the
7308 narrowing operation (short in the above example). */
7310 bool
7311 supportable_narrowing_operation (enum tree_code code,
7312 tree vectype_out, tree vectype_in,
7313 enum tree_code *code1, int *multi_step_cvt,
7314 vec<tree> *interm_types)
7316 enum machine_mode vec_mode;
7317 enum insn_code icode1;
7318 optab optab1, interm_optab;
7319 tree vectype = vectype_in;
7320 tree narrow_vectype = vectype_out;
7321 enum tree_code c1;
7322 tree intermediate_type;
7323 enum machine_mode intermediate_mode, prev_mode;
7324 int i;
7325 bool uns;
7327 *multi_step_cvt = 0;
7328 switch (code)
7330 CASE_CONVERT:
7331 c1 = VEC_PACK_TRUNC_EXPR;
7332 break;
7334 case FIX_TRUNC_EXPR:
7335 c1 = VEC_PACK_FIX_TRUNC_EXPR;
7336 break;
7338 case FLOAT_EXPR:
7339 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
7340 tree code and optabs used for computing the operation. */
7341 return false;
7343 default:
7344 gcc_unreachable ();
7347 if (code == FIX_TRUNC_EXPR)
7348 /* The signedness is determined from output operand. */
7349 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
7350 else
7351 optab1 = optab_for_tree_code (c1, vectype, optab_default);
7353 if (!optab1)
7354 return false;
7356 vec_mode = TYPE_MODE (vectype);
7357 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
7358 return false;
7360 *code1 = c1;
7362 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
7363 return true;
7365 /* Check if it's a multi-step conversion that can be done using intermediate
7366 types. */
7367 prev_mode = vec_mode;
7368 if (code == FIX_TRUNC_EXPR)
7369 uns = TYPE_UNSIGNED (vectype_out);
7370 else
7371 uns = TYPE_UNSIGNED (vectype);
7373 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
7374 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
7375 costly than signed. */
7376 if (code == FIX_TRUNC_EXPR && uns)
7378 enum insn_code icode2;
7380 intermediate_type
7381 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
7382 interm_optab
7383 = optab_for_tree_code (c1, intermediate_type, optab_default);
7384 if (interm_optab != unknown_optab
7385 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
7386 && insn_data[icode1].operand[0].mode
7387 == insn_data[icode2].operand[0].mode)
7389 uns = false;
7390 optab1 = interm_optab;
7391 icode1 = icode2;
7395 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
7396 intermediate steps in promotion sequence. We try
7397 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
7398 interm_types->create (MAX_INTERM_CVT_STEPS);
7399 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
7401 intermediate_mode = insn_data[icode1].operand[0].mode;
7402 intermediate_type
7403 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
7404 interm_optab
7405 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
7406 optab_default);
7407 if (!interm_optab
7408 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
7409 || insn_data[icode1].operand[0].mode != intermediate_mode
7410 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
7411 == CODE_FOR_nothing))
7412 break;
7414 interm_types->quick_push (intermediate_type);
7415 (*multi_step_cvt)++;
7417 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
7418 return true;
7420 prev_mode = intermediate_mode;
7421 optab1 = interm_optab;
7424 interm_types->release ();
7425 return false;