[ARM 2/5 big.LITTLE] Allow tuning parameters without unique tuning targets.
[official-gcc.git] / gcc / tree-vect-stmts.c
blob99f6b1f1ccb1f444fd1d1ca46605ae7910cdc1ba
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 (lhs && 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))
397 if (is_gimple_call (stmt)
398 && gimple_call_internal_p (stmt))
399 switch (gimple_call_internal_fn (stmt))
401 case IFN_MASK_STORE:
402 operand = gimple_call_arg (stmt, 3);
403 if (operand == use)
404 return true;
405 /* FALLTHRU */
406 case IFN_MASK_LOAD:
407 operand = gimple_call_arg (stmt, 2);
408 if (operand == use)
409 return true;
410 break;
411 default:
412 break;
414 return false;
417 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
418 return false;
419 operand = gimple_assign_rhs1 (stmt);
420 if (TREE_CODE (operand) != SSA_NAME)
421 return false;
423 if (operand == use)
424 return true;
426 return false;
431 Function process_use.
433 Inputs:
434 - a USE in STMT in a loop represented by LOOP_VINFO
435 - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
436 that defined USE. This is done by calling mark_relevant and passing it
437 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
438 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
439 be performed.
441 Outputs:
442 Generally, LIVE_P and RELEVANT are used to define the liveness and
443 relevance info of the DEF_STMT of this USE:
444 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
445 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
446 Exceptions:
447 - case 1: If USE is used only for address computations (e.g. array indexing),
448 which does not need to be directly vectorized, then the liveness/relevance
449 of the respective DEF_STMT is left unchanged.
450 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
451 skip DEF_STMT cause it had already been processed.
452 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
453 be modified accordingly.
455 Return true if everything is as expected. Return false otherwise. */
457 static bool
458 process_use (gimple stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
459 enum vect_relevant relevant, vec<gimple> *worklist,
460 bool force)
462 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
463 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
464 stmt_vec_info dstmt_vinfo;
465 basic_block bb, def_bb;
466 tree def;
467 gimple def_stmt;
468 enum vect_def_type dt;
470 /* case 1: we are only interested in uses that need to be vectorized. Uses
471 that are used for address computation are not considered relevant. */
472 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
473 return true;
475 if (!vect_is_simple_use (use, stmt, loop_vinfo, NULL, &def_stmt, &def, &dt))
477 if (dump_enabled_p ())
478 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
479 "not vectorized: unsupported use in stmt.\n");
480 return false;
483 if (!def_stmt || gimple_nop_p (def_stmt))
484 return true;
486 def_bb = gimple_bb (def_stmt);
487 if (!flow_bb_inside_loop_p (loop, def_bb))
489 if (dump_enabled_p ())
490 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
491 return true;
494 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
495 DEF_STMT must have already been processed, because this should be the
496 only way that STMT, which is a reduction-phi, was put in the worklist,
497 as there should be no other uses for DEF_STMT in the loop. So we just
498 check that everything is as expected, and we are done. */
499 dstmt_vinfo = vinfo_for_stmt (def_stmt);
500 bb = gimple_bb (stmt);
501 if (gimple_code (stmt) == GIMPLE_PHI
502 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
503 && gimple_code (def_stmt) != GIMPLE_PHI
504 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
505 && bb->loop_father == def_bb->loop_father)
507 if (dump_enabled_p ())
508 dump_printf_loc (MSG_NOTE, vect_location,
509 "reduc-stmt defining reduc-phi in the same nest.\n");
510 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
511 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
512 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
513 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
514 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
515 return true;
518 /* case 3a: outer-loop stmt defining an inner-loop stmt:
519 outer-loop-header-bb:
520 d = def_stmt
521 inner-loop:
522 stmt # use (d)
523 outer-loop-tail-bb:
524 ... */
525 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
527 if (dump_enabled_p ())
528 dump_printf_loc (MSG_NOTE, vect_location,
529 "outer-loop def-stmt defining inner-loop stmt.\n");
531 switch (relevant)
533 case vect_unused_in_scope:
534 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
535 vect_used_in_scope : vect_unused_in_scope;
536 break;
538 case vect_used_in_outer_by_reduction:
539 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
540 relevant = vect_used_by_reduction;
541 break;
543 case vect_used_in_outer:
544 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
545 relevant = vect_used_in_scope;
546 break;
548 case vect_used_in_scope:
549 break;
551 default:
552 gcc_unreachable ();
556 /* case 3b: inner-loop stmt defining an outer-loop stmt:
557 outer-loop-header-bb:
559 inner-loop:
560 d = def_stmt
561 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
562 stmt # use (d) */
563 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
565 if (dump_enabled_p ())
566 dump_printf_loc (MSG_NOTE, vect_location,
567 "inner-loop def-stmt defining outer-loop stmt.\n");
569 switch (relevant)
571 case vect_unused_in_scope:
572 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
573 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
574 vect_used_in_outer_by_reduction : vect_unused_in_scope;
575 break;
577 case vect_used_by_reduction:
578 relevant = vect_used_in_outer_by_reduction;
579 break;
581 case vect_used_in_scope:
582 relevant = vect_used_in_outer;
583 break;
585 default:
586 gcc_unreachable ();
590 vect_mark_relevant (worklist, def_stmt, relevant, live_p,
591 is_pattern_stmt_p (stmt_vinfo));
592 return true;
596 /* Function vect_mark_stmts_to_be_vectorized.
598 Not all stmts in the loop need to be vectorized. For example:
600 for i...
601 for j...
602 1. T0 = i + j
603 2. T1 = a[T0]
605 3. j = j + 1
607 Stmt 1 and 3 do not need to be vectorized, because loop control and
608 addressing of vectorized data-refs are handled differently.
610 This pass detects such stmts. */
612 bool
613 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
615 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
616 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
617 unsigned int nbbs = loop->num_nodes;
618 gimple_stmt_iterator si;
619 gimple stmt;
620 unsigned int i;
621 stmt_vec_info stmt_vinfo;
622 basic_block bb;
623 gimple phi;
624 bool live_p;
625 enum vect_relevant relevant, tmp_relevant;
626 enum vect_def_type def_type;
628 if (dump_enabled_p ())
629 dump_printf_loc (MSG_NOTE, vect_location,
630 "=== vect_mark_stmts_to_be_vectorized ===\n");
632 stack_vec<gimple, 64> worklist;
634 /* 1. Init worklist. */
635 for (i = 0; i < nbbs; i++)
637 bb = bbs[i];
638 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
640 phi = gsi_stmt (si);
641 if (dump_enabled_p ())
643 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
644 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
645 dump_printf (MSG_NOTE, "\n");
648 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
649 vect_mark_relevant (&worklist, phi, relevant, live_p, false);
651 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
653 stmt = gsi_stmt (si);
654 if (dump_enabled_p ())
656 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
657 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
658 dump_printf (MSG_NOTE, "\n");
661 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
662 vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
666 /* 2. Process_worklist */
667 while (worklist.length () > 0)
669 use_operand_p use_p;
670 ssa_op_iter iter;
672 stmt = worklist.pop ();
673 if (dump_enabled_p ())
675 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
676 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
677 dump_printf (MSG_NOTE, "\n");
680 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
681 (DEF_STMT) as relevant/irrelevant and live/dead according to the
682 liveness and relevance properties of STMT. */
683 stmt_vinfo = vinfo_for_stmt (stmt);
684 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
685 live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
687 /* Generally, the liveness and relevance properties of STMT are
688 propagated as is to the DEF_STMTs of its USEs:
689 live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
690 relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
692 One exception is when STMT has been identified as defining a reduction
693 variable; in this case we set the liveness/relevance as follows:
694 live_p = false
695 relevant = vect_used_by_reduction
696 This is because we distinguish between two kinds of relevant stmts -
697 those that are used by a reduction computation, and those that are
698 (also) used by a regular computation. This allows us later on to
699 identify stmts that are used solely by a reduction, and therefore the
700 order of the results that they produce does not have to be kept. */
702 def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
703 tmp_relevant = relevant;
704 switch (def_type)
706 case vect_reduction_def:
707 switch (tmp_relevant)
709 case vect_unused_in_scope:
710 relevant = vect_used_by_reduction;
711 break;
713 case vect_used_by_reduction:
714 if (gimple_code (stmt) == GIMPLE_PHI)
715 break;
716 /* fall through */
718 default:
719 if (dump_enabled_p ())
720 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
721 "unsupported use of reduction.\n");
722 return false;
725 live_p = false;
726 break;
728 case vect_nested_cycle:
729 if (tmp_relevant != vect_unused_in_scope
730 && tmp_relevant != vect_used_in_outer_by_reduction
731 && tmp_relevant != vect_used_in_outer)
733 if (dump_enabled_p ())
734 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
735 "unsupported use of nested cycle.\n");
737 return false;
740 live_p = false;
741 break;
743 case vect_double_reduction_def:
744 if (tmp_relevant != vect_unused_in_scope
745 && tmp_relevant != vect_used_by_reduction)
747 if (dump_enabled_p ())
748 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
749 "unsupported use of double reduction.\n");
751 return false;
754 live_p = false;
755 break;
757 default:
758 break;
761 if (is_pattern_stmt_p (stmt_vinfo))
763 /* Pattern statements are not inserted into the code, so
764 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
765 have to scan the RHS or function arguments instead. */
766 if (is_gimple_assign (stmt))
768 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
769 tree op = gimple_assign_rhs1 (stmt);
771 i = 1;
772 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
774 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
775 live_p, relevant, &worklist, false)
776 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
777 live_p, relevant, &worklist, false))
778 return false;
779 i = 2;
781 for (; i < gimple_num_ops (stmt); i++)
783 op = gimple_op (stmt, i);
784 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
785 &worklist, false))
786 return false;
789 else if (is_gimple_call (stmt))
791 for (i = 0; i < gimple_call_num_args (stmt); i++)
793 tree arg = gimple_call_arg (stmt, i);
794 if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
795 &worklist, false))
796 return false;
800 else
801 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
803 tree op = USE_FROM_PTR (use_p);
804 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
805 &worklist, false))
806 return false;
809 if (STMT_VINFO_GATHER_P (stmt_vinfo))
811 tree off;
812 tree decl = vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
813 gcc_assert (decl);
814 if (!process_use (stmt, off, loop_vinfo, live_p, relevant,
815 &worklist, true))
816 return false;
818 } /* while worklist */
820 return true;
824 /* Function vect_model_simple_cost.
826 Models cost for simple operations, i.e. those that only emit ncopies of a
827 single op. Right now, this does not account for multiple insns that could
828 be generated for the single vector op. We will handle that shortly. */
830 void
831 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
832 enum vect_def_type *dt,
833 stmt_vector_for_cost *prologue_cost_vec,
834 stmt_vector_for_cost *body_cost_vec)
836 int i;
837 int inside_cost = 0, prologue_cost = 0;
839 /* The SLP costs were already calculated during SLP tree build. */
840 if (PURE_SLP_STMT (stmt_info))
841 return;
843 /* FORNOW: Assuming maximum 2 args per stmts. */
844 for (i = 0; i < 2; i++)
845 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
846 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
847 stmt_info, 0, vect_prologue);
849 /* Pass the inside-of-loop statements to the target-specific cost model. */
850 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
851 stmt_info, 0, vect_body);
853 if (dump_enabled_p ())
854 dump_printf_loc (MSG_NOTE, vect_location,
855 "vect_model_simple_cost: inside_cost = %d, "
856 "prologue_cost = %d .\n", inside_cost, prologue_cost);
860 /* Model cost for type demotion and promotion operations. PWR is normally
861 zero for single-step promotions and demotions. It will be one if
862 two-step promotion/demotion is required, and so on. Each additional
863 step doubles the number of instructions required. */
865 static void
866 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
867 enum vect_def_type *dt, int pwr)
869 int i, tmp;
870 int inside_cost = 0, prologue_cost = 0;
871 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
872 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
873 void *target_cost_data;
875 /* The SLP costs were already calculated during SLP tree build. */
876 if (PURE_SLP_STMT (stmt_info))
877 return;
879 if (loop_vinfo)
880 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
881 else
882 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
884 for (i = 0; i < pwr + 1; i++)
886 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
887 (i + 1) : i;
888 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
889 vec_promote_demote, stmt_info, 0,
890 vect_body);
893 /* FORNOW: Assuming maximum 2 args per stmts. */
894 for (i = 0; i < 2; i++)
895 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
896 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
897 stmt_info, 0, vect_prologue);
899 if (dump_enabled_p ())
900 dump_printf_loc (MSG_NOTE, vect_location,
901 "vect_model_promotion_demotion_cost: inside_cost = %d, "
902 "prologue_cost = %d .\n", inside_cost, prologue_cost);
905 /* Function vect_cost_group_size
907 For grouped load or store, return the group_size only if it is the first
908 load or store of a group, else return 1. This ensures that group size is
909 only returned once per group. */
911 static int
912 vect_cost_group_size (stmt_vec_info stmt_info)
914 gimple first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
916 if (first_stmt == STMT_VINFO_STMT (stmt_info))
917 return GROUP_SIZE (stmt_info);
919 return 1;
923 /* Function vect_model_store_cost
925 Models cost for stores. In the case of grouped accesses, one access
926 has the overhead of the grouped access attributed to it. */
928 void
929 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
930 bool store_lanes_p, enum vect_def_type dt,
931 slp_tree slp_node,
932 stmt_vector_for_cost *prologue_cost_vec,
933 stmt_vector_for_cost *body_cost_vec)
935 int group_size;
936 unsigned int inside_cost = 0, prologue_cost = 0;
937 struct data_reference *first_dr;
938 gimple first_stmt;
940 /* The SLP costs were already calculated during SLP tree build. */
941 if (PURE_SLP_STMT (stmt_info))
942 return;
944 if (dt == vect_constant_def || dt == vect_external_def)
945 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
946 stmt_info, 0, vect_prologue);
948 /* Grouped access? */
949 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
951 if (slp_node)
953 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
954 group_size = 1;
956 else
958 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
959 group_size = vect_cost_group_size (stmt_info);
962 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
964 /* Not a grouped access. */
965 else
967 group_size = 1;
968 first_dr = STMT_VINFO_DATA_REF (stmt_info);
971 /* We assume that the cost of a single store-lanes instruction is
972 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
973 access is instead being provided by a permute-and-store operation,
974 include the cost of the permutes. */
975 if (!store_lanes_p && group_size > 1)
977 /* Uses a high and low interleave operation for each needed permute. */
979 int nstmts = ncopies * exact_log2 (group_size) * group_size;
980 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
981 stmt_info, 0, vect_body);
983 if (dump_enabled_p ())
984 dump_printf_loc (MSG_NOTE, vect_location,
985 "vect_model_store_cost: strided group_size = %d .\n",
986 group_size);
989 /* Costs of the stores. */
990 vect_get_store_cost (first_dr, ncopies, &inside_cost, body_cost_vec);
992 if (dump_enabled_p ())
993 dump_printf_loc (MSG_NOTE, vect_location,
994 "vect_model_store_cost: inside_cost = %d, "
995 "prologue_cost = %d .\n", inside_cost, prologue_cost);
999 /* Calculate cost of DR's memory access. */
1000 void
1001 vect_get_store_cost (struct data_reference *dr, int ncopies,
1002 unsigned int *inside_cost,
1003 stmt_vector_for_cost *body_cost_vec)
1005 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1006 gimple stmt = DR_STMT (dr);
1007 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1009 switch (alignment_support_scheme)
1011 case dr_aligned:
1013 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1014 vector_store, stmt_info, 0,
1015 vect_body);
1017 if (dump_enabled_p ())
1018 dump_printf_loc (MSG_NOTE, vect_location,
1019 "vect_model_store_cost: aligned.\n");
1020 break;
1023 case dr_unaligned_supported:
1025 /* Here, we assign an additional cost for the unaligned store. */
1026 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1027 unaligned_store, stmt_info,
1028 DR_MISALIGNMENT (dr), vect_body);
1029 if (dump_enabled_p ())
1030 dump_printf_loc (MSG_NOTE, vect_location,
1031 "vect_model_store_cost: unaligned supported by "
1032 "hardware.\n");
1033 break;
1036 case dr_unaligned_unsupported:
1038 *inside_cost = VECT_MAX_COST;
1040 if (dump_enabled_p ())
1041 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1042 "vect_model_store_cost: unsupported access.\n");
1043 break;
1046 default:
1047 gcc_unreachable ();
1052 /* Function vect_model_load_cost
1054 Models cost for loads. In the case of grouped accesses, the last access
1055 has the overhead of the grouped access attributed to it. Since unaligned
1056 accesses are supported for loads, we also account for the costs of the
1057 access scheme chosen. */
1059 void
1060 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1061 bool load_lanes_p, slp_tree slp_node,
1062 stmt_vector_for_cost *prologue_cost_vec,
1063 stmt_vector_for_cost *body_cost_vec)
1065 int group_size;
1066 gimple first_stmt;
1067 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
1068 unsigned int inside_cost = 0, prologue_cost = 0;
1070 /* The SLP costs were already calculated during SLP tree build. */
1071 if (PURE_SLP_STMT (stmt_info))
1072 return;
1074 /* Grouped accesses? */
1075 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1076 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && first_stmt && !slp_node)
1078 group_size = vect_cost_group_size (stmt_info);
1079 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1081 /* Not a grouped access. */
1082 else
1084 group_size = 1;
1085 first_dr = dr;
1088 /* We assume that the cost of a single load-lanes instruction is
1089 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1090 access is instead being provided by a load-and-permute operation,
1091 include the cost of the permutes. */
1092 if (!load_lanes_p && group_size > 1)
1094 /* Uses an even and odd extract operations for each needed permute. */
1095 int nstmts = ncopies * exact_log2 (group_size) * group_size;
1096 inside_cost += record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1097 stmt_info, 0, vect_body);
1099 if (dump_enabled_p ())
1100 dump_printf_loc (MSG_NOTE, vect_location,
1101 "vect_model_load_cost: strided group_size = %d .\n",
1102 group_size);
1105 /* The loads themselves. */
1106 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1108 /* N scalar loads plus gathering them into a vector. */
1109 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1110 inside_cost += record_stmt_cost (body_cost_vec,
1111 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1112 scalar_load, stmt_info, 0, vect_body);
1113 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1114 stmt_info, 0, vect_body);
1116 else
1117 vect_get_load_cost (first_dr, ncopies,
1118 ((!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1119 || group_size > 1 || slp_node),
1120 &inside_cost, &prologue_cost,
1121 prologue_cost_vec, body_cost_vec, true);
1123 if (dump_enabled_p ())
1124 dump_printf_loc (MSG_NOTE, vect_location,
1125 "vect_model_load_cost: inside_cost = %d, "
1126 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1130 /* Calculate cost of DR's memory access. */
1131 void
1132 vect_get_load_cost (struct data_reference *dr, int ncopies,
1133 bool add_realign_cost, unsigned int *inside_cost,
1134 unsigned int *prologue_cost,
1135 stmt_vector_for_cost *prologue_cost_vec,
1136 stmt_vector_for_cost *body_cost_vec,
1137 bool record_prologue_costs)
1139 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1140 gimple stmt = DR_STMT (dr);
1141 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1143 switch (alignment_support_scheme)
1145 case dr_aligned:
1147 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1148 stmt_info, 0, vect_body);
1150 if (dump_enabled_p ())
1151 dump_printf_loc (MSG_NOTE, vect_location,
1152 "vect_model_load_cost: aligned.\n");
1154 break;
1156 case dr_unaligned_supported:
1158 /* Here, we assign an additional cost for the unaligned load. */
1159 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1160 unaligned_load, stmt_info,
1161 DR_MISALIGNMENT (dr), vect_body);
1163 if (dump_enabled_p ())
1164 dump_printf_loc (MSG_NOTE, vect_location,
1165 "vect_model_load_cost: unaligned supported by "
1166 "hardware.\n");
1168 break;
1170 case dr_explicit_realign:
1172 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1173 vector_load, stmt_info, 0, vect_body);
1174 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1175 vec_perm, stmt_info, 0, vect_body);
1177 /* FIXME: If the misalignment remains fixed across the iterations of
1178 the containing loop, the following cost should be added to the
1179 prologue costs. */
1180 if (targetm.vectorize.builtin_mask_for_load)
1181 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1182 stmt_info, 0, vect_body);
1184 if (dump_enabled_p ())
1185 dump_printf_loc (MSG_NOTE, vect_location,
1186 "vect_model_load_cost: explicit realign\n");
1188 break;
1190 case dr_explicit_realign_optimized:
1192 if (dump_enabled_p ())
1193 dump_printf_loc (MSG_NOTE, vect_location,
1194 "vect_model_load_cost: unaligned software "
1195 "pipelined.\n");
1197 /* Unaligned software pipeline has a load of an address, an initial
1198 load, and possibly a mask operation to "prime" the loop. However,
1199 if this is an access in a group of loads, which provide grouped
1200 access, then the above cost should only be considered for one
1201 access in the group. Inside the loop, there is a load op
1202 and a realignment op. */
1204 if (add_realign_cost && record_prologue_costs)
1206 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1207 vector_stmt, stmt_info,
1208 0, vect_prologue);
1209 if (targetm.vectorize.builtin_mask_for_load)
1210 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1211 vector_stmt, stmt_info,
1212 0, vect_prologue);
1215 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1216 stmt_info, 0, vect_body);
1217 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1218 stmt_info, 0, vect_body);
1220 if (dump_enabled_p ())
1221 dump_printf_loc (MSG_NOTE, vect_location,
1222 "vect_model_load_cost: explicit realign optimized"
1223 "\n");
1225 break;
1228 case dr_unaligned_unsupported:
1230 *inside_cost = VECT_MAX_COST;
1232 if (dump_enabled_p ())
1233 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1234 "vect_model_load_cost: unsupported access.\n");
1235 break;
1238 default:
1239 gcc_unreachable ();
1243 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1244 the loop preheader for the vectorized stmt STMT. */
1246 static void
1247 vect_init_vector_1 (gimple stmt, gimple new_stmt, gimple_stmt_iterator *gsi)
1249 if (gsi)
1250 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1251 else
1253 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1254 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1256 if (loop_vinfo)
1258 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1259 basic_block new_bb;
1260 edge pe;
1262 if (nested_in_vect_loop_p (loop, stmt))
1263 loop = loop->inner;
1265 pe = loop_preheader_edge (loop);
1266 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1267 gcc_assert (!new_bb);
1269 else
1271 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1272 basic_block bb;
1273 gimple_stmt_iterator gsi_bb_start;
1275 gcc_assert (bb_vinfo);
1276 bb = BB_VINFO_BB (bb_vinfo);
1277 gsi_bb_start = gsi_after_labels (bb);
1278 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1282 if (dump_enabled_p ())
1284 dump_printf_loc (MSG_NOTE, vect_location,
1285 "created new init_stmt: ");
1286 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1287 dump_printf (MSG_NOTE, "\n");
1291 /* Function vect_init_vector.
1293 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1294 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1295 vector type a vector with all elements equal to VAL is created first.
1296 Place the initialization at BSI if it is not NULL. Otherwise, place the
1297 initialization at the loop preheader.
1298 Return the DEF of INIT_STMT.
1299 It will be used in the vectorization of STMT. */
1301 tree
1302 vect_init_vector (gimple stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1304 tree new_var;
1305 gimple init_stmt;
1306 tree vec_oprnd;
1307 tree new_temp;
1309 if (TREE_CODE (type) == VECTOR_TYPE
1310 && TREE_CODE (TREE_TYPE (val)) != VECTOR_TYPE)
1312 if (!types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1314 if (CONSTANT_CLASS_P (val))
1315 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (type), val);
1316 else
1318 new_temp = make_ssa_name (TREE_TYPE (type), NULL);
1319 init_stmt = gimple_build_assign_with_ops (NOP_EXPR,
1320 new_temp, val,
1321 NULL_TREE);
1322 vect_init_vector_1 (stmt, init_stmt, gsi);
1323 val = new_temp;
1326 val = build_vector_from_val (type, val);
1329 new_var = vect_get_new_vect_var (type, vect_simple_var, "cst_");
1330 init_stmt = gimple_build_assign (new_var, val);
1331 new_temp = make_ssa_name (new_var, init_stmt);
1332 gimple_assign_set_lhs (init_stmt, new_temp);
1333 vect_init_vector_1 (stmt, init_stmt, gsi);
1334 vec_oprnd = gimple_assign_lhs (init_stmt);
1335 return vec_oprnd;
1339 /* Function vect_get_vec_def_for_operand.
1341 OP is an operand in STMT. This function returns a (vector) def that will be
1342 used in the vectorized stmt for STMT.
1344 In the case that OP is an SSA_NAME which is defined in the loop, then
1345 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1347 In case OP is an invariant or constant, a new stmt that creates a vector def
1348 needs to be introduced. */
1350 tree
1351 vect_get_vec_def_for_operand (tree op, gimple stmt, tree *scalar_def)
1353 tree vec_oprnd;
1354 gimple vec_stmt;
1355 gimple def_stmt;
1356 stmt_vec_info def_stmt_info = NULL;
1357 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1358 unsigned int nunits;
1359 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1360 tree def;
1361 enum vect_def_type dt;
1362 bool is_simple_use;
1363 tree vector_type;
1365 if (dump_enabled_p ())
1367 dump_printf_loc (MSG_NOTE, vect_location,
1368 "vect_get_vec_def_for_operand: ");
1369 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1370 dump_printf (MSG_NOTE, "\n");
1373 is_simple_use = vect_is_simple_use (op, stmt, loop_vinfo, NULL,
1374 &def_stmt, &def, &dt);
1375 gcc_assert (is_simple_use);
1376 if (dump_enabled_p ())
1378 int loc_printed = 0;
1379 if (def)
1381 dump_printf_loc (MSG_NOTE, vect_location, "def = ");
1382 loc_printed = 1;
1383 dump_generic_expr (MSG_NOTE, TDF_SLIM, def);
1384 dump_printf (MSG_NOTE, "\n");
1386 if (def_stmt)
1388 if (loc_printed)
1389 dump_printf (MSG_NOTE, " def_stmt = ");
1390 else
1391 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1392 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1393 dump_printf (MSG_NOTE, "\n");
1397 switch (dt)
1399 /* Case 1: operand is a constant. */
1400 case vect_constant_def:
1402 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1403 gcc_assert (vector_type);
1404 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
1406 if (scalar_def)
1407 *scalar_def = op;
1409 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1410 if (dump_enabled_p ())
1411 dump_printf_loc (MSG_NOTE, vect_location,
1412 "Create vector_cst. nunits = %d\n", nunits);
1414 return vect_init_vector (stmt, op, vector_type, NULL);
1417 /* Case 2: operand is defined outside the loop - loop invariant. */
1418 case vect_external_def:
1420 vector_type = get_vectype_for_scalar_type (TREE_TYPE (def));
1421 gcc_assert (vector_type);
1423 if (scalar_def)
1424 *scalar_def = def;
1426 /* Create 'vec_inv = {inv,inv,..,inv}' */
1427 if (dump_enabled_p ())
1428 dump_printf_loc (MSG_NOTE, vect_location, "Create vector_inv.\n");
1430 return vect_init_vector (stmt, def, vector_type, NULL);
1433 /* Case 3: operand is defined inside the loop. */
1434 case vect_internal_def:
1436 if (scalar_def)
1437 *scalar_def = NULL/* FIXME tuples: def_stmt*/;
1439 /* Get the def from the vectorized stmt. */
1440 def_stmt_info = vinfo_for_stmt (def_stmt);
1442 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1443 /* Get vectorized pattern statement. */
1444 if (!vec_stmt
1445 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1446 && !STMT_VINFO_RELEVANT (def_stmt_info))
1447 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1448 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1449 gcc_assert (vec_stmt);
1450 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1451 vec_oprnd = PHI_RESULT (vec_stmt);
1452 else if (is_gimple_call (vec_stmt))
1453 vec_oprnd = gimple_call_lhs (vec_stmt);
1454 else
1455 vec_oprnd = gimple_assign_lhs (vec_stmt);
1456 return vec_oprnd;
1459 /* Case 4: operand is defined by a loop header phi - reduction */
1460 case vect_reduction_def:
1461 case vect_double_reduction_def:
1462 case vect_nested_cycle:
1464 struct loop *loop;
1466 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1467 loop = (gimple_bb (def_stmt))->loop_father;
1469 /* Get the def before the loop */
1470 op = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
1471 return get_initial_def_for_reduction (stmt, op, scalar_def);
1474 /* Case 5: operand is defined by loop-header phi - induction. */
1475 case vect_induction_def:
1477 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1479 /* Get the def from the vectorized stmt. */
1480 def_stmt_info = vinfo_for_stmt (def_stmt);
1481 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1482 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1483 vec_oprnd = PHI_RESULT (vec_stmt);
1484 else
1485 vec_oprnd = gimple_get_lhs (vec_stmt);
1486 return vec_oprnd;
1489 default:
1490 gcc_unreachable ();
1495 /* Function vect_get_vec_def_for_stmt_copy
1497 Return a vector-def for an operand. This function is used when the
1498 vectorized stmt to be created (by the caller to this function) is a "copy"
1499 created in case the vectorized result cannot fit in one vector, and several
1500 copies of the vector-stmt are required. In this case the vector-def is
1501 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1502 of the stmt that defines VEC_OPRND.
1503 DT is the type of the vector def VEC_OPRND.
1505 Context:
1506 In case the vectorization factor (VF) is bigger than the number
1507 of elements that can fit in a vectype (nunits), we have to generate
1508 more than one vector stmt to vectorize the scalar stmt. This situation
1509 arises when there are multiple data-types operated upon in the loop; the
1510 smallest data-type determines the VF, and as a result, when vectorizing
1511 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1512 vector stmt (each computing a vector of 'nunits' results, and together
1513 computing 'VF' results in each iteration). This function is called when
1514 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1515 which VF=16 and nunits=4, so the number of copies required is 4):
1517 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1519 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1520 VS1.1: vx.1 = memref1 VS1.2
1521 VS1.2: vx.2 = memref2 VS1.3
1522 VS1.3: vx.3 = memref3
1524 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1525 VSnew.1: vz1 = vx.1 + ... VSnew.2
1526 VSnew.2: vz2 = vx.2 + ... VSnew.3
1527 VSnew.3: vz3 = vx.3 + ...
1529 The vectorization of S1 is explained in vectorizable_load.
1530 The vectorization of S2:
1531 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1532 the function 'vect_get_vec_def_for_operand' is called to
1533 get the relevant vector-def for each operand of S2. For operand x it
1534 returns the vector-def 'vx.0'.
1536 To create the remaining copies of the vector-stmt (VSnew.j), this
1537 function is called to get the relevant vector-def for each operand. It is
1538 obtained from the respective VS1.j stmt, which is recorded in the
1539 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1541 For example, to obtain the vector-def 'vx.1' in order to create the
1542 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1543 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1544 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1545 and return its def ('vx.1').
1546 Overall, to create the above sequence this function will be called 3 times:
1547 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1548 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1549 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1551 tree
1552 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1554 gimple vec_stmt_for_operand;
1555 stmt_vec_info def_stmt_info;
1557 /* Do nothing; can reuse same def. */
1558 if (dt == vect_external_def || dt == vect_constant_def )
1559 return vec_oprnd;
1561 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1562 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1563 gcc_assert (def_stmt_info);
1564 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1565 gcc_assert (vec_stmt_for_operand);
1566 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1567 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1568 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1569 else
1570 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1571 return vec_oprnd;
1575 /* Get vectorized definitions for the operands to create a copy of an original
1576 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1578 static void
1579 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1580 vec<tree> *vec_oprnds0,
1581 vec<tree> *vec_oprnds1)
1583 tree vec_oprnd = vec_oprnds0->pop ();
1585 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1586 vec_oprnds0->quick_push (vec_oprnd);
1588 if (vec_oprnds1 && vec_oprnds1->length ())
1590 vec_oprnd = vec_oprnds1->pop ();
1591 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1592 vec_oprnds1->quick_push (vec_oprnd);
1597 /* Get vectorized definitions for OP0 and OP1.
1598 REDUC_INDEX is the index of reduction operand in case of reduction,
1599 and -1 otherwise. */
1601 void
1602 vect_get_vec_defs (tree op0, tree op1, gimple stmt,
1603 vec<tree> *vec_oprnds0,
1604 vec<tree> *vec_oprnds1,
1605 slp_tree slp_node, int reduc_index)
1607 if (slp_node)
1609 int nops = (op1 == NULL_TREE) ? 1 : 2;
1610 auto_vec<tree> ops (nops);
1611 auto_vec<vec<tree> > vec_defs (nops);
1613 ops.quick_push (op0);
1614 if (op1)
1615 ops.quick_push (op1);
1617 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1619 *vec_oprnds0 = vec_defs[0];
1620 if (op1)
1621 *vec_oprnds1 = vec_defs[1];
1623 else
1625 tree vec_oprnd;
1627 vec_oprnds0->create (1);
1628 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt, NULL);
1629 vec_oprnds0->quick_push (vec_oprnd);
1631 if (op1)
1633 vec_oprnds1->create (1);
1634 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt, NULL);
1635 vec_oprnds1->quick_push (vec_oprnd);
1641 /* Function vect_finish_stmt_generation.
1643 Insert a new stmt. */
1645 void
1646 vect_finish_stmt_generation (gimple stmt, gimple vec_stmt,
1647 gimple_stmt_iterator *gsi)
1649 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1650 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1651 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
1653 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1655 if (!gsi_end_p (*gsi)
1656 && gimple_has_mem_ops (vec_stmt))
1658 gimple at_stmt = gsi_stmt (*gsi);
1659 tree vuse = gimple_vuse (at_stmt);
1660 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1662 tree vdef = gimple_vdef (at_stmt);
1663 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1664 /* If we have an SSA vuse and insert a store, update virtual
1665 SSA form to avoid triggering the renamer. Do so only
1666 if we can easily see all uses - which is what almost always
1667 happens with the way vectorized stmts are inserted. */
1668 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1669 && ((is_gimple_assign (vec_stmt)
1670 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1671 || (is_gimple_call (vec_stmt)
1672 && !(gimple_call_flags (vec_stmt)
1673 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1675 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1676 gimple_set_vdef (vec_stmt, new_vdef);
1677 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1681 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1683 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, loop_vinfo,
1684 bb_vinfo));
1686 if (dump_enabled_p ())
1688 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1689 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1690 dump_printf (MSG_NOTE, "\n");
1693 gimple_set_location (vec_stmt, gimple_location (stmt));
1696 /* Checks if CALL can be vectorized in type VECTYPE. Returns
1697 a function declaration if the target has a vectorized version
1698 of the function, or NULL_TREE if the function cannot be vectorized. */
1700 tree
1701 vectorizable_function (gimple call, tree vectype_out, tree vectype_in)
1703 tree fndecl = gimple_call_fndecl (call);
1705 /* We only handle functions that do not read or clobber memory -- i.e.
1706 const or novops ones. */
1707 if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
1708 return NULL_TREE;
1710 if (!fndecl
1711 || TREE_CODE (fndecl) != FUNCTION_DECL
1712 || !DECL_BUILT_IN (fndecl))
1713 return NULL_TREE;
1715 return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
1716 vectype_in);
1720 static tree permute_vec_elements (tree, tree, tree, gimple,
1721 gimple_stmt_iterator *);
1724 /* Function vectorizable_mask_load_store.
1726 Check if STMT performs a conditional load or store that can be vectorized.
1727 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1728 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
1729 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1731 static bool
1732 vectorizable_mask_load_store (gimple stmt, gimple_stmt_iterator *gsi,
1733 gimple *vec_stmt, slp_tree slp_node)
1735 tree vec_dest = NULL;
1736 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1737 stmt_vec_info prev_stmt_info;
1738 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1739 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1740 bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
1741 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1742 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1743 tree elem_type;
1744 gimple new_stmt;
1745 tree dummy;
1746 tree dataref_ptr = NULL_TREE;
1747 gimple ptr_incr;
1748 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1749 int ncopies;
1750 int i, j;
1751 bool inv_p;
1752 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
1753 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
1754 int gather_scale = 1;
1755 enum vect_def_type gather_dt = vect_unknown_def_type;
1756 bool is_store;
1757 tree mask;
1758 gimple def_stmt;
1759 tree def;
1760 enum vect_def_type dt;
1762 if (slp_node != NULL)
1763 return false;
1765 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
1766 gcc_assert (ncopies >= 1);
1768 is_store = gimple_call_internal_fn (stmt) == IFN_MASK_STORE;
1769 mask = gimple_call_arg (stmt, 2);
1770 if (TYPE_PRECISION (TREE_TYPE (mask))
1771 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype))))
1772 return false;
1774 /* FORNOW. This restriction should be relaxed. */
1775 if (nested_in_vect_loop && ncopies > 1)
1777 if (dump_enabled_p ())
1778 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1779 "multiple types in nested loop.");
1780 return false;
1783 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1784 return false;
1786 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
1787 return false;
1789 if (!STMT_VINFO_DATA_REF (stmt_info))
1790 return false;
1792 elem_type = TREE_TYPE (vectype);
1794 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1795 return false;
1797 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1798 return false;
1800 if (STMT_VINFO_GATHER_P (stmt_info))
1802 gimple def_stmt;
1803 tree def;
1804 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
1805 &gather_off, &gather_scale);
1806 gcc_assert (gather_decl);
1807 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, NULL,
1808 &def_stmt, &def, &gather_dt,
1809 &gather_off_vectype))
1811 if (dump_enabled_p ())
1812 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1813 "gather index use not simple.");
1814 return false;
1817 else if (tree_int_cst_compare (nested_in_vect_loop
1818 ? STMT_VINFO_DR_STEP (stmt_info)
1819 : DR_STEP (dr), size_zero_node) <= 0)
1820 return false;
1821 else if (!VECTOR_MODE_P (TYPE_MODE (vectype))
1822 || !can_vec_mask_load_store_p (TYPE_MODE (vectype), !is_store))
1823 return false;
1825 if (TREE_CODE (mask) != SSA_NAME)
1826 return false;
1828 if (!vect_is_simple_use (mask, stmt, loop_vinfo, NULL,
1829 &def_stmt, &def, &dt))
1830 return false;
1832 if (is_store)
1834 tree rhs = gimple_call_arg (stmt, 3);
1835 if (!vect_is_simple_use (rhs, stmt, loop_vinfo, NULL,
1836 &def_stmt, &def, &dt))
1837 return false;
1840 if (!vec_stmt) /* transformation not required. */
1842 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
1843 if (is_store)
1844 vect_model_store_cost (stmt_info, ncopies, false, dt,
1845 NULL, NULL, NULL);
1846 else
1847 vect_model_load_cost (stmt_info, ncopies, false, NULL, NULL, NULL);
1848 return true;
1851 /** Transform. **/
1853 if (STMT_VINFO_GATHER_P (stmt_info))
1855 tree vec_oprnd0 = NULL_TREE, op;
1856 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
1857 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
1858 tree ptr, vec_mask = NULL_TREE, mask_op, var, scale;
1859 tree perm_mask = NULL_TREE, prev_res = NULL_TREE;
1860 edge pe = loop_preheader_edge (loop);
1861 gimple_seq seq;
1862 basic_block new_bb;
1863 enum { NARROW, NONE, WIDEN } modifier;
1864 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
1866 if (nunits == gather_off_nunits)
1867 modifier = NONE;
1868 else if (nunits == gather_off_nunits / 2)
1870 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
1871 modifier = WIDEN;
1873 for (i = 0; i < gather_off_nunits; ++i)
1874 sel[i] = i | nunits;
1876 perm_mask = vect_gen_perm_mask (gather_off_vectype, sel);
1877 gcc_assert (perm_mask != NULL_TREE);
1879 else if (nunits == gather_off_nunits * 2)
1881 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
1882 modifier = NARROW;
1884 for (i = 0; i < nunits; ++i)
1885 sel[i] = i < gather_off_nunits
1886 ? i : i + nunits - gather_off_nunits;
1888 perm_mask = vect_gen_perm_mask (vectype, sel);
1889 gcc_assert (perm_mask != NULL_TREE);
1890 ncopies *= 2;
1892 else
1893 gcc_unreachable ();
1895 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
1896 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1897 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1898 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1899 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1900 scaletype = TREE_VALUE (arglist);
1901 gcc_checking_assert (types_compatible_p (srctype, rettype)
1902 && types_compatible_p (srctype, masktype));
1904 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
1906 ptr = fold_convert (ptrtype, gather_base);
1907 if (!is_gimple_min_invariant (ptr))
1909 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
1910 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
1911 gcc_assert (!new_bb);
1914 scale = build_int_cst (scaletype, gather_scale);
1916 prev_stmt_info = NULL;
1917 for (j = 0; j < ncopies; ++j)
1919 if (modifier == WIDEN && (j & 1))
1920 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
1921 perm_mask, stmt, gsi);
1922 else if (j == 0)
1923 op = vec_oprnd0
1924 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
1925 else
1926 op = vec_oprnd0
1927 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
1929 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
1931 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
1932 == TYPE_VECTOR_SUBPARTS (idxtype));
1933 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
1934 var = make_ssa_name (var, NULL);
1935 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
1936 new_stmt
1937 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var,
1938 op, NULL_TREE);
1939 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1940 op = var;
1943 if (j == 0)
1944 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
1945 else
1947 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL, &def_stmt,
1948 &def, &dt);
1949 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
1952 mask_op = vec_mask;
1953 if (!useless_type_conversion_p (masktype, TREE_TYPE (vec_mask)))
1955 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask_op))
1956 == TYPE_VECTOR_SUBPARTS (masktype));
1957 var = vect_get_new_vect_var (masktype, vect_simple_var, NULL);
1958 var = make_ssa_name (var, NULL);
1959 mask_op = build1 (VIEW_CONVERT_EXPR, masktype, mask_op);
1960 new_stmt
1961 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var,
1962 mask_op, NULL_TREE);
1963 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1964 mask_op = var;
1967 new_stmt
1968 = gimple_build_call (gather_decl, 5, mask_op, ptr, op, mask_op,
1969 scale);
1971 if (!useless_type_conversion_p (vectype, rettype))
1973 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
1974 == TYPE_VECTOR_SUBPARTS (rettype));
1975 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
1976 op = make_ssa_name (var, new_stmt);
1977 gimple_call_set_lhs (new_stmt, op);
1978 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1979 var = make_ssa_name (vec_dest, NULL);
1980 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
1981 new_stmt
1982 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var, op,
1983 NULL_TREE);
1985 else
1987 var = make_ssa_name (vec_dest, new_stmt);
1988 gimple_call_set_lhs (new_stmt, var);
1991 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1993 if (modifier == NARROW)
1995 if ((j & 1) == 0)
1997 prev_res = var;
1998 continue;
2000 var = permute_vec_elements (prev_res, var,
2001 perm_mask, stmt, gsi);
2002 new_stmt = SSA_NAME_DEF_STMT (var);
2005 if (prev_stmt_info == NULL)
2006 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2007 else
2008 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2009 prev_stmt_info = vinfo_for_stmt (new_stmt);
2011 return true;
2013 else if (is_store)
2015 tree vec_rhs = NULL_TREE, vec_mask = NULL_TREE;
2016 prev_stmt_info = NULL;
2017 for (i = 0; i < ncopies; i++)
2019 unsigned align, misalign;
2021 if (i == 0)
2023 tree rhs = gimple_call_arg (stmt, 3);
2024 vec_rhs = vect_get_vec_def_for_operand (rhs, stmt, NULL);
2025 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
2026 /* We should have catched mismatched types earlier. */
2027 gcc_assert (useless_type_conversion_p (vectype,
2028 TREE_TYPE (vec_rhs)));
2029 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2030 NULL_TREE, &dummy, gsi,
2031 &ptr_incr, false, &inv_p);
2032 gcc_assert (!inv_p);
2034 else
2036 vect_is_simple_use (vec_rhs, NULL, loop_vinfo, NULL, &def_stmt,
2037 &def, &dt);
2038 vec_rhs = vect_get_vec_def_for_stmt_copy (dt, vec_rhs);
2039 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL, &def_stmt,
2040 &def, &dt);
2041 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2042 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2043 TYPE_SIZE_UNIT (vectype));
2046 align = TYPE_ALIGN_UNIT (vectype);
2047 if (aligned_access_p (dr))
2048 misalign = 0;
2049 else if (DR_MISALIGNMENT (dr) == -1)
2051 align = TYPE_ALIGN_UNIT (elem_type);
2052 misalign = 0;
2054 else
2055 misalign = DR_MISALIGNMENT (dr);
2056 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2057 misalign);
2058 new_stmt
2059 = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
2060 gimple_call_arg (stmt, 1),
2061 vec_mask, vec_rhs);
2062 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2063 if (i == 0)
2064 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2065 else
2066 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2067 prev_stmt_info = vinfo_for_stmt (new_stmt);
2070 else
2072 tree vec_mask = NULL_TREE;
2073 prev_stmt_info = NULL;
2074 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
2075 for (i = 0; i < ncopies; i++)
2077 unsigned align, misalign;
2079 if (i == 0)
2081 vec_mask = vect_get_vec_def_for_operand (mask, stmt, NULL);
2082 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2083 NULL_TREE, &dummy, gsi,
2084 &ptr_incr, false, &inv_p);
2085 gcc_assert (!inv_p);
2087 else
2089 vect_is_simple_use (vec_mask, NULL, loop_vinfo, NULL, &def_stmt,
2090 &def, &dt);
2091 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2092 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2093 TYPE_SIZE_UNIT (vectype));
2096 align = TYPE_ALIGN_UNIT (vectype);
2097 if (aligned_access_p (dr))
2098 misalign = 0;
2099 else if (DR_MISALIGNMENT (dr) == -1)
2101 align = TYPE_ALIGN_UNIT (elem_type);
2102 misalign = 0;
2104 else
2105 misalign = DR_MISALIGNMENT (dr);
2106 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2107 misalign);
2108 new_stmt
2109 = gimple_build_call_internal (IFN_MASK_LOAD, 3, dataref_ptr,
2110 gimple_call_arg (stmt, 1),
2111 vec_mask);
2112 gimple_call_set_lhs (new_stmt, make_ssa_name (vec_dest, NULL));
2113 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2114 if (i == 0)
2115 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2116 else
2117 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2118 prev_stmt_info = vinfo_for_stmt (new_stmt);
2122 return true;
2126 /* Function vectorizable_call.
2128 Check if STMT performs a function call that can be vectorized.
2129 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2130 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2131 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2133 static bool
2134 vectorizable_call (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
2135 slp_tree slp_node)
2137 tree vec_dest;
2138 tree scalar_dest;
2139 tree op, type;
2140 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2141 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
2142 tree vectype_out, vectype_in;
2143 int nunits_in;
2144 int nunits_out;
2145 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2146 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2147 tree fndecl, new_temp, def, rhs_type;
2148 gimple def_stmt;
2149 enum vect_def_type dt[3]
2150 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
2151 gimple new_stmt = NULL;
2152 int ncopies, j;
2153 vec<tree> vargs = vNULL;
2154 enum { NARROW, NONE, WIDEN } modifier;
2155 size_t i, nargs;
2156 tree lhs;
2158 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2159 return false;
2161 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2162 return false;
2164 /* Is STMT a vectorizable call? */
2165 if (!is_gimple_call (stmt))
2166 return false;
2168 if (gimple_call_internal_p (stmt)
2169 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2170 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
2171 return vectorizable_mask_load_store (stmt, gsi, vec_stmt,
2172 slp_node);
2174 if (gimple_call_lhs (stmt) == NULL_TREE
2175 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2176 return false;
2178 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2180 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2182 /* Process function arguments. */
2183 rhs_type = NULL_TREE;
2184 vectype_in = NULL_TREE;
2185 nargs = gimple_call_num_args (stmt);
2187 /* Bail out if the function has more than three arguments, we do not have
2188 interesting builtin functions to vectorize with more than two arguments
2189 except for fma. No arguments is also not good. */
2190 if (nargs == 0 || nargs > 3)
2191 return false;
2193 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
2194 if (gimple_call_internal_p (stmt)
2195 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2197 nargs = 0;
2198 rhs_type = unsigned_type_node;
2201 for (i = 0; i < nargs; i++)
2203 tree opvectype;
2205 op = gimple_call_arg (stmt, i);
2207 /* We can only handle calls with arguments of the same type. */
2208 if (rhs_type
2209 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
2211 if (dump_enabled_p ())
2212 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2213 "argument types differ.\n");
2214 return false;
2216 if (!rhs_type)
2217 rhs_type = TREE_TYPE (op);
2219 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2220 &def_stmt, &def, &dt[i], &opvectype))
2222 if (dump_enabled_p ())
2223 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2224 "use not simple.\n");
2225 return false;
2228 if (!vectype_in)
2229 vectype_in = opvectype;
2230 else if (opvectype
2231 && opvectype != vectype_in)
2233 if (dump_enabled_p ())
2234 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2235 "argument vector types differ.\n");
2236 return false;
2239 /* If all arguments are external or constant defs use a vector type with
2240 the same size as the output vector type. */
2241 if (!vectype_in)
2242 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
2243 if (vec_stmt)
2244 gcc_assert (vectype_in);
2245 if (!vectype_in)
2247 if (dump_enabled_p ())
2249 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2250 "no vectype for scalar type ");
2251 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
2252 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2255 return false;
2258 /* FORNOW */
2259 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
2260 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
2261 if (nunits_in == nunits_out / 2)
2262 modifier = NARROW;
2263 else if (nunits_out == nunits_in)
2264 modifier = NONE;
2265 else if (nunits_out == nunits_in / 2)
2266 modifier = WIDEN;
2267 else
2268 return false;
2270 /* For now, we only vectorize functions if a target specific builtin
2271 is available. TODO -- in some cases, it might be profitable to
2272 insert the calls for pieces of the vector, in order to be able
2273 to vectorize other operations in the loop. */
2274 fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
2275 if (fndecl == NULL_TREE)
2277 if (gimple_call_internal_p (stmt)
2278 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
2279 && !slp_node
2280 && loop_vinfo
2281 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2282 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
2283 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2284 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
2286 /* We can handle IFN_GOMP_SIMD_LANE by returning a
2287 { 0, 1, 2, ... vf - 1 } vector. */
2288 gcc_assert (nargs == 0);
2290 else
2292 if (dump_enabled_p ())
2293 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2294 "function is not vectorizable.\n");
2295 return false;
2299 gcc_assert (!gimple_vuse (stmt));
2301 if (slp_node || PURE_SLP_STMT (stmt_info))
2302 ncopies = 1;
2303 else if (modifier == NARROW)
2304 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
2305 else
2306 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
2308 /* Sanity check: make sure that at least one copy of the vectorized stmt
2309 needs to be generated. */
2310 gcc_assert (ncopies >= 1);
2312 if (!vec_stmt) /* transformation not required. */
2314 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
2315 if (dump_enabled_p ())
2316 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
2317 "\n");
2318 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
2319 return true;
2322 /** Transform. **/
2324 if (dump_enabled_p ())
2325 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2327 /* Handle def. */
2328 scalar_dest = gimple_call_lhs (stmt);
2329 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
2331 prev_stmt_info = NULL;
2332 switch (modifier)
2334 case NONE:
2335 for (j = 0; j < ncopies; ++j)
2337 /* Build argument list for the vectorized call. */
2338 if (j == 0)
2339 vargs.create (nargs);
2340 else
2341 vargs.truncate (0);
2343 if (slp_node)
2345 auto_vec<vec<tree> > vec_defs (nargs);
2346 vec<tree> vec_oprnds0;
2348 for (i = 0; i < nargs; i++)
2349 vargs.quick_push (gimple_call_arg (stmt, i));
2350 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2351 vec_oprnds0 = vec_defs[0];
2353 /* Arguments are ready. Create the new vector stmt. */
2354 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
2356 size_t k;
2357 for (k = 0; k < nargs; k++)
2359 vec<tree> vec_oprndsk = vec_defs[k];
2360 vargs[k] = vec_oprndsk[i];
2362 new_stmt = gimple_build_call_vec (fndecl, vargs);
2363 new_temp = make_ssa_name (vec_dest, new_stmt);
2364 gimple_call_set_lhs (new_stmt, new_temp);
2365 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2366 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2369 for (i = 0; i < nargs; i++)
2371 vec<tree> vec_oprndsi = vec_defs[i];
2372 vec_oprndsi.release ();
2374 continue;
2377 for (i = 0; i < nargs; i++)
2379 op = gimple_call_arg (stmt, i);
2380 if (j == 0)
2381 vec_oprnd0
2382 = vect_get_vec_def_for_operand (op, stmt, NULL);
2383 else
2385 vec_oprnd0 = gimple_call_arg (new_stmt, i);
2386 vec_oprnd0
2387 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2390 vargs.quick_push (vec_oprnd0);
2393 if (gimple_call_internal_p (stmt)
2394 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2396 tree *v = XALLOCAVEC (tree, nunits_out);
2397 int k;
2398 for (k = 0; k < nunits_out; ++k)
2399 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
2400 tree cst = build_vector (vectype_out, v);
2401 tree new_var
2402 = vect_get_new_vect_var (vectype_out, vect_simple_var, "cst_");
2403 gimple init_stmt = gimple_build_assign (new_var, cst);
2404 new_temp = make_ssa_name (new_var, init_stmt);
2405 gimple_assign_set_lhs (init_stmt, new_temp);
2406 vect_init_vector_1 (stmt, init_stmt, NULL);
2407 new_temp = make_ssa_name (vec_dest, NULL);
2408 new_stmt = gimple_build_assign (new_temp,
2409 gimple_assign_lhs (init_stmt));
2411 else
2413 new_stmt = gimple_build_call_vec (fndecl, vargs);
2414 new_temp = make_ssa_name (vec_dest, new_stmt);
2415 gimple_call_set_lhs (new_stmt, new_temp);
2417 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2419 if (j == 0)
2420 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2421 else
2422 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2424 prev_stmt_info = vinfo_for_stmt (new_stmt);
2427 break;
2429 case NARROW:
2430 for (j = 0; j < ncopies; ++j)
2432 /* Build argument list for the vectorized call. */
2433 if (j == 0)
2434 vargs.create (nargs * 2);
2435 else
2436 vargs.truncate (0);
2438 if (slp_node)
2440 auto_vec<vec<tree> > vec_defs (nargs);
2441 vec<tree> vec_oprnds0;
2443 for (i = 0; i < nargs; i++)
2444 vargs.quick_push (gimple_call_arg (stmt, i));
2445 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2446 vec_oprnds0 = vec_defs[0];
2448 /* Arguments are ready. Create the new vector stmt. */
2449 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2451 size_t k;
2452 vargs.truncate (0);
2453 for (k = 0; k < nargs; k++)
2455 vec<tree> vec_oprndsk = vec_defs[k];
2456 vargs.quick_push (vec_oprndsk[i]);
2457 vargs.quick_push (vec_oprndsk[i + 1]);
2459 new_stmt = gimple_build_call_vec (fndecl, vargs);
2460 new_temp = make_ssa_name (vec_dest, new_stmt);
2461 gimple_call_set_lhs (new_stmt, new_temp);
2462 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2463 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2466 for (i = 0; i < nargs; i++)
2468 vec<tree> vec_oprndsi = vec_defs[i];
2469 vec_oprndsi.release ();
2471 continue;
2474 for (i = 0; i < nargs; i++)
2476 op = gimple_call_arg (stmt, i);
2477 if (j == 0)
2479 vec_oprnd0
2480 = vect_get_vec_def_for_operand (op, stmt, NULL);
2481 vec_oprnd1
2482 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2484 else
2486 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2487 vec_oprnd0
2488 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2489 vec_oprnd1
2490 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2493 vargs.quick_push (vec_oprnd0);
2494 vargs.quick_push (vec_oprnd1);
2497 new_stmt = gimple_build_call_vec (fndecl, vargs);
2498 new_temp = make_ssa_name (vec_dest, new_stmt);
2499 gimple_call_set_lhs (new_stmt, new_temp);
2500 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2502 if (j == 0)
2503 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2504 else
2505 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2507 prev_stmt_info = vinfo_for_stmt (new_stmt);
2510 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2512 break;
2514 case WIDEN:
2515 /* No current target implements this case. */
2516 return false;
2519 vargs.release ();
2521 /* The call in STMT might prevent it from being removed in dce.
2522 We however cannot remove it here, due to the way the ssa name
2523 it defines is mapped to the new definition. So just replace
2524 rhs of the statement with something harmless. */
2526 if (slp_node)
2527 return true;
2529 type = TREE_TYPE (scalar_dest);
2530 if (is_pattern_stmt_p (stmt_info))
2531 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2532 else
2533 lhs = gimple_call_lhs (stmt);
2534 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2535 set_vinfo_for_stmt (new_stmt, stmt_info);
2536 set_vinfo_for_stmt (stmt, NULL);
2537 STMT_VINFO_STMT (stmt_info) = new_stmt;
2538 gsi_replace (gsi, new_stmt, false);
2540 return true;
2544 struct simd_call_arg_info
2546 tree vectype;
2547 tree op;
2548 enum vect_def_type dt;
2549 HOST_WIDE_INT linear_step;
2550 unsigned int align;
2553 /* Function vectorizable_simd_clone_call.
2555 Check if STMT performs a function call that can be vectorized
2556 by calling a simd clone of the function.
2557 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2558 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2559 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2561 static bool
2562 vectorizable_simd_clone_call (gimple stmt, gimple_stmt_iterator *gsi,
2563 gimple *vec_stmt, slp_tree slp_node)
2565 tree vec_dest;
2566 tree scalar_dest;
2567 tree op, type;
2568 tree vec_oprnd0 = NULL_TREE;
2569 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
2570 tree vectype;
2571 unsigned int nunits;
2572 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2573 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2574 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2575 tree fndecl, new_temp, def;
2576 gimple def_stmt;
2577 gimple new_stmt = NULL;
2578 int ncopies, j;
2579 vec<simd_call_arg_info> arginfo = vNULL;
2580 vec<tree> vargs = vNULL;
2581 size_t i, nargs;
2582 tree lhs, rtype, ratype;
2583 vec<constructor_elt, va_gc> *ret_ctor_elts;
2585 /* Is STMT a vectorizable call? */
2586 if (!is_gimple_call (stmt))
2587 return false;
2589 fndecl = gimple_call_fndecl (stmt);
2590 if (fndecl == NULL_TREE)
2591 return false;
2593 struct cgraph_node *node = cgraph_get_node (fndecl);
2594 if (node == NULL || node->simd_clones == NULL)
2595 return false;
2597 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2598 return false;
2600 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2601 return false;
2603 if (gimple_call_lhs (stmt)
2604 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2605 return false;
2607 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2609 vectype = STMT_VINFO_VECTYPE (stmt_info);
2611 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
2612 return false;
2614 /* FORNOW */
2615 if (slp_node || PURE_SLP_STMT (stmt_info))
2616 return false;
2618 /* Process function arguments. */
2619 nargs = gimple_call_num_args (stmt);
2621 /* Bail out if the function has zero arguments. */
2622 if (nargs == 0)
2623 return false;
2625 arginfo.create (nargs);
2627 for (i = 0; i < nargs; i++)
2629 simd_call_arg_info thisarginfo;
2630 affine_iv iv;
2632 thisarginfo.linear_step = 0;
2633 thisarginfo.align = 0;
2634 thisarginfo.op = NULL_TREE;
2636 op = gimple_call_arg (stmt, i);
2637 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
2638 &def_stmt, &def, &thisarginfo.dt,
2639 &thisarginfo.vectype)
2640 || thisarginfo.dt == vect_uninitialized_def)
2642 if (dump_enabled_p ())
2643 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2644 "use not simple.\n");
2645 arginfo.release ();
2646 return false;
2649 if (thisarginfo.dt == vect_constant_def
2650 || thisarginfo.dt == vect_external_def)
2651 gcc_assert (thisarginfo.vectype == NULL_TREE);
2652 else
2653 gcc_assert (thisarginfo.vectype != NULL_TREE);
2655 if (thisarginfo.dt != vect_constant_def
2656 && thisarginfo.dt != vect_external_def
2657 && loop_vinfo
2658 && TREE_CODE (op) == SSA_NAME
2659 && simple_iv (loop, loop_containing_stmt (stmt), op, &iv, false)
2660 && tree_fits_shwi_p (iv.step))
2662 thisarginfo.linear_step = tree_to_shwi (iv.step);
2663 thisarginfo.op = iv.base;
2665 else if ((thisarginfo.dt == vect_constant_def
2666 || thisarginfo.dt == vect_external_def)
2667 && POINTER_TYPE_P (TREE_TYPE (op)))
2668 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
2670 arginfo.quick_push (thisarginfo);
2673 unsigned int badness = 0;
2674 struct cgraph_node *bestn = NULL;
2675 if (STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info))
2676 bestn = cgraph_get_node (STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info));
2677 else
2678 for (struct cgraph_node *n = node->simd_clones; n != NULL;
2679 n = n->simdclone->next_clone)
2681 unsigned int this_badness = 0;
2682 if (n->simdclone->simdlen
2683 > (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2684 || n->simdclone->nargs != nargs)
2685 continue;
2686 if (n->simdclone->simdlen
2687 < (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2688 this_badness += (exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2689 - exact_log2 (n->simdclone->simdlen)) * 1024;
2690 if (n->simdclone->inbranch)
2691 this_badness += 2048;
2692 int target_badness = targetm.simd_clone.usable (n);
2693 if (target_badness < 0)
2694 continue;
2695 this_badness += target_badness * 512;
2696 /* FORNOW: Have to add code to add the mask argument. */
2697 if (n->simdclone->inbranch)
2698 continue;
2699 for (i = 0; i < nargs; i++)
2701 switch (n->simdclone->args[i].arg_type)
2703 case SIMD_CLONE_ARG_TYPE_VECTOR:
2704 if (!useless_type_conversion_p
2705 (n->simdclone->args[i].orig_type,
2706 TREE_TYPE (gimple_call_arg (stmt, i))))
2707 i = -1;
2708 else if (arginfo[i].dt == vect_constant_def
2709 || arginfo[i].dt == vect_external_def
2710 || arginfo[i].linear_step)
2711 this_badness += 64;
2712 break;
2713 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2714 if (arginfo[i].dt != vect_constant_def
2715 && arginfo[i].dt != vect_external_def)
2716 i = -1;
2717 break;
2718 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2719 if (arginfo[i].dt == vect_constant_def
2720 || arginfo[i].dt == vect_external_def
2721 || (arginfo[i].linear_step
2722 != n->simdclone->args[i].linear_step))
2723 i = -1;
2724 break;
2725 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2726 /* FORNOW */
2727 i = -1;
2728 break;
2729 case SIMD_CLONE_ARG_TYPE_MASK:
2730 gcc_unreachable ();
2732 if (i == (size_t) -1)
2733 break;
2734 if (n->simdclone->args[i].alignment > arginfo[i].align)
2736 i = -1;
2737 break;
2739 if (arginfo[i].align)
2740 this_badness += (exact_log2 (arginfo[i].align)
2741 - exact_log2 (n->simdclone->args[i].alignment));
2743 if (i == (size_t) -1)
2744 continue;
2745 if (bestn == NULL || this_badness < badness)
2747 bestn = n;
2748 badness = this_badness;
2752 if (bestn == NULL)
2754 arginfo.release ();
2755 return false;
2758 for (i = 0; i < nargs; i++)
2759 if ((arginfo[i].dt == vect_constant_def
2760 || arginfo[i].dt == vect_external_def)
2761 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
2763 arginfo[i].vectype
2764 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
2765 i)));
2766 if (arginfo[i].vectype == NULL
2767 || (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2768 > bestn->simdclone->simdlen))
2770 arginfo.release ();
2771 return false;
2775 fndecl = bestn->decl;
2776 nunits = bestn->simdclone->simdlen;
2777 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2779 /* If the function isn't const, only allow it in simd loops where user
2780 has asserted that at least nunits consecutive iterations can be
2781 performed using SIMD instructions. */
2782 if ((loop == NULL || (unsigned) loop->safelen < nunits)
2783 && gimple_vuse (stmt))
2785 arginfo.release ();
2786 return false;
2789 /* Sanity check: make sure that at least one copy of the vectorized stmt
2790 needs to be generated. */
2791 gcc_assert (ncopies >= 1);
2793 if (!vec_stmt) /* transformation not required. */
2795 STMT_VINFO_SIMD_CLONE_FNDECL (stmt_info) = bestn->decl;
2796 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
2797 if (dump_enabled_p ())
2798 dump_printf_loc (MSG_NOTE, vect_location,
2799 "=== vectorizable_simd_clone_call ===\n");
2800 /* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
2801 arginfo.release ();
2802 return true;
2805 /** Transform. **/
2807 if (dump_enabled_p ())
2808 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2810 /* Handle def. */
2811 scalar_dest = gimple_call_lhs (stmt);
2812 vec_dest = NULL_TREE;
2813 rtype = NULL_TREE;
2814 ratype = NULL_TREE;
2815 if (scalar_dest)
2817 vec_dest = vect_create_destination_var (scalar_dest, vectype);
2818 rtype = TREE_TYPE (TREE_TYPE (fndecl));
2819 if (TREE_CODE (rtype) == ARRAY_TYPE)
2821 ratype = rtype;
2822 rtype = TREE_TYPE (ratype);
2826 prev_stmt_info = NULL;
2827 for (j = 0; j < ncopies; ++j)
2829 /* Build argument list for the vectorized call. */
2830 if (j == 0)
2831 vargs.create (nargs);
2832 else
2833 vargs.truncate (0);
2835 for (i = 0; i < nargs; i++)
2837 unsigned int k, l, m, o;
2838 tree atype;
2839 op = gimple_call_arg (stmt, i);
2840 switch (bestn->simdclone->args[i].arg_type)
2842 case SIMD_CLONE_ARG_TYPE_VECTOR:
2843 atype = bestn->simdclone->args[i].vector_type;
2844 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
2845 for (m = j * o; m < (j + 1) * o; m++)
2847 if (TYPE_VECTOR_SUBPARTS (atype)
2848 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
2850 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
2851 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2852 / TYPE_VECTOR_SUBPARTS (atype));
2853 gcc_assert ((k & (k - 1)) == 0);
2854 if (m == 0)
2855 vec_oprnd0
2856 = vect_get_vec_def_for_operand (op, stmt, NULL);
2857 else
2859 vec_oprnd0 = arginfo[i].op;
2860 if ((m & (k - 1)) == 0)
2861 vec_oprnd0
2862 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2863 vec_oprnd0);
2865 arginfo[i].op = vec_oprnd0;
2866 vec_oprnd0
2867 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
2868 size_int (prec),
2869 bitsize_int ((m & (k - 1)) * prec));
2870 new_stmt
2871 = gimple_build_assign (make_ssa_name (atype, NULL),
2872 vec_oprnd0);
2873 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2874 vargs.safe_push (gimple_assign_lhs (new_stmt));
2876 else
2878 k = (TYPE_VECTOR_SUBPARTS (atype)
2879 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
2880 gcc_assert ((k & (k - 1)) == 0);
2881 vec<constructor_elt, va_gc> *ctor_elts;
2882 if (k != 1)
2883 vec_alloc (ctor_elts, k);
2884 else
2885 ctor_elts = NULL;
2886 for (l = 0; l < k; l++)
2888 if (m == 0 && l == 0)
2889 vec_oprnd0
2890 = vect_get_vec_def_for_operand (op, stmt, NULL);
2891 else
2892 vec_oprnd0
2893 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
2894 arginfo[i].op);
2895 arginfo[i].op = vec_oprnd0;
2896 if (k == 1)
2897 break;
2898 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
2899 vec_oprnd0);
2901 if (k == 1)
2902 vargs.safe_push (vec_oprnd0);
2903 else
2905 vec_oprnd0 = build_constructor (atype, ctor_elts);
2906 new_stmt
2907 = gimple_build_assign (make_ssa_name (atype, NULL),
2908 vec_oprnd0);
2909 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2910 vargs.safe_push (gimple_assign_lhs (new_stmt));
2914 break;
2915 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2916 vargs.safe_push (op);
2917 break;
2918 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2919 if (j == 0)
2921 gimple_seq stmts;
2922 arginfo[i].op
2923 = force_gimple_operand (arginfo[i].op, &stmts, true,
2924 NULL_TREE);
2925 if (stmts != NULL)
2927 basic_block new_bb;
2928 edge pe = loop_preheader_edge (loop);
2929 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
2930 gcc_assert (!new_bb);
2932 tree phi_res = copy_ssa_name (op, NULL);
2933 gimple new_phi = create_phi_node (phi_res, loop->header);
2934 set_vinfo_for_stmt (new_phi,
2935 new_stmt_vec_info (new_phi, loop_vinfo,
2936 NULL));
2937 add_phi_arg (new_phi, arginfo[i].op,
2938 loop_preheader_edge (loop), UNKNOWN_LOCATION);
2939 enum tree_code code
2940 = POINTER_TYPE_P (TREE_TYPE (op))
2941 ? POINTER_PLUS_EXPR : PLUS_EXPR;
2942 tree type = POINTER_TYPE_P (TREE_TYPE (op))
2943 ? sizetype : TREE_TYPE (op);
2944 double_int cst
2945 = double_int::from_shwi
2946 (bestn->simdclone->args[i].linear_step);
2947 cst *= double_int::from_uhwi (ncopies * nunits);
2948 tree tcst = double_int_to_tree (type, cst);
2949 tree phi_arg = copy_ssa_name (op, NULL);
2950 new_stmt = gimple_build_assign_with_ops (code, phi_arg,
2951 phi_res, tcst);
2952 gimple_stmt_iterator si = gsi_after_labels (loop->header);
2953 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
2954 set_vinfo_for_stmt (new_stmt,
2955 new_stmt_vec_info (new_stmt, loop_vinfo,
2956 NULL));
2957 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
2958 UNKNOWN_LOCATION);
2959 arginfo[i].op = phi_res;
2960 vargs.safe_push (phi_res);
2962 else
2964 enum tree_code code
2965 = POINTER_TYPE_P (TREE_TYPE (op))
2966 ? POINTER_PLUS_EXPR : PLUS_EXPR;
2967 tree type = POINTER_TYPE_P (TREE_TYPE (op))
2968 ? sizetype : TREE_TYPE (op);
2969 double_int cst
2970 = double_int::from_shwi
2971 (bestn->simdclone->args[i].linear_step);
2972 cst *= double_int::from_uhwi (j * nunits);
2973 tree tcst = double_int_to_tree (type, cst);
2974 new_temp = make_ssa_name (TREE_TYPE (op), NULL);
2975 new_stmt
2976 = gimple_build_assign_with_ops (code, new_temp,
2977 arginfo[i].op, tcst);
2978 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2979 vargs.safe_push (new_temp);
2981 break;
2982 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2983 default:
2984 gcc_unreachable ();
2988 new_stmt = gimple_build_call_vec (fndecl, vargs);
2989 if (vec_dest)
2991 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
2992 if (ratype)
2993 new_temp = create_tmp_var (ratype, NULL);
2994 else if (TYPE_VECTOR_SUBPARTS (vectype)
2995 == TYPE_VECTOR_SUBPARTS (rtype))
2996 new_temp = make_ssa_name (vec_dest, new_stmt);
2997 else
2998 new_temp = make_ssa_name (rtype, new_stmt);
2999 gimple_call_set_lhs (new_stmt, new_temp);
3001 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3003 if (vec_dest)
3005 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
3007 unsigned int k, l;
3008 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
3009 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
3010 gcc_assert ((k & (k - 1)) == 0);
3011 for (l = 0; l < k; l++)
3013 tree t;
3014 if (ratype)
3016 t = build_fold_addr_expr (new_temp);
3017 t = build2 (MEM_REF, vectype, t,
3018 build_int_cst (TREE_TYPE (t),
3019 l * prec / BITS_PER_UNIT));
3021 else
3022 t = build3 (BIT_FIELD_REF, vectype, new_temp,
3023 size_int (prec), bitsize_int (l * prec));
3024 new_stmt
3025 = gimple_build_assign (make_ssa_name (vectype, NULL), t);
3026 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3027 if (j == 0 && l == 0)
3028 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3029 else
3030 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3032 prev_stmt_info = vinfo_for_stmt (new_stmt);
3035 if (ratype)
3037 tree clobber = build_constructor (ratype, NULL);
3038 TREE_THIS_VOLATILE (clobber) = 1;
3039 new_stmt = gimple_build_assign (new_temp, clobber);
3040 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3042 continue;
3044 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
3046 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
3047 / TYPE_VECTOR_SUBPARTS (rtype));
3048 gcc_assert ((k & (k - 1)) == 0);
3049 if ((j & (k - 1)) == 0)
3050 vec_alloc (ret_ctor_elts, k);
3051 if (ratype)
3053 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
3054 for (m = 0; m < o; m++)
3056 tree tem = build4 (ARRAY_REF, rtype, new_temp,
3057 size_int (m), NULL_TREE, NULL_TREE);
3058 new_stmt
3059 = gimple_build_assign (make_ssa_name (rtype, NULL),
3060 tem);
3061 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3062 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
3063 gimple_assign_lhs (new_stmt));
3065 tree clobber = build_constructor (ratype, NULL);
3066 TREE_THIS_VOLATILE (clobber) = 1;
3067 new_stmt = gimple_build_assign (new_temp, clobber);
3068 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3070 else
3071 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
3072 if ((j & (k - 1)) != k - 1)
3073 continue;
3074 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
3075 new_stmt
3076 = gimple_build_assign (make_ssa_name (vec_dest, NULL),
3077 vec_oprnd0);
3078 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3080 if ((unsigned) j == k - 1)
3081 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3082 else
3083 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3085 prev_stmt_info = vinfo_for_stmt (new_stmt);
3086 continue;
3088 else if (ratype)
3090 tree t = build_fold_addr_expr (new_temp);
3091 t = build2 (MEM_REF, vectype, t,
3092 build_int_cst (TREE_TYPE (t), 0));
3093 new_stmt
3094 = gimple_build_assign (make_ssa_name (vec_dest, NULL), t);
3095 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3096 tree clobber = build_constructor (ratype, NULL);
3097 TREE_THIS_VOLATILE (clobber) = 1;
3098 vect_finish_stmt_generation (stmt,
3099 gimple_build_assign (new_temp,
3100 clobber), gsi);
3104 if (j == 0)
3105 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3106 else
3107 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3109 prev_stmt_info = vinfo_for_stmt (new_stmt);
3112 vargs.release ();
3114 /* The call in STMT might prevent it from being removed in dce.
3115 We however cannot remove it here, due to the way the ssa name
3116 it defines is mapped to the new definition. So just replace
3117 rhs of the statement with something harmless. */
3119 if (slp_node)
3120 return true;
3122 if (scalar_dest)
3124 type = TREE_TYPE (scalar_dest);
3125 if (is_pattern_stmt_p (stmt_info))
3126 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
3127 else
3128 lhs = gimple_call_lhs (stmt);
3129 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
3131 else
3132 new_stmt = gimple_build_nop ();
3133 set_vinfo_for_stmt (new_stmt, stmt_info);
3134 set_vinfo_for_stmt (stmt, NULL);
3135 STMT_VINFO_STMT (stmt_info) = new_stmt;
3136 gsi_replace (gsi, new_stmt, false);
3137 unlink_stmt_vdef (stmt);
3139 return true;
3143 /* Function vect_gen_widened_results_half
3145 Create a vector stmt whose code, type, number of arguments, and result
3146 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
3147 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
3148 In the case that CODE is a CALL_EXPR, this means that a call to DECL
3149 needs to be created (DECL is a function-decl of a target-builtin).
3150 STMT is the original scalar stmt that we are vectorizing. */
3152 static gimple
3153 vect_gen_widened_results_half (enum tree_code code,
3154 tree decl,
3155 tree vec_oprnd0, tree vec_oprnd1, int op_type,
3156 tree vec_dest, gimple_stmt_iterator *gsi,
3157 gimple stmt)
3159 gimple new_stmt;
3160 tree new_temp;
3162 /* Generate half of the widened result: */
3163 if (code == CALL_EXPR)
3165 /* Target specific support */
3166 if (op_type == binary_op)
3167 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3168 else
3169 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3170 new_temp = make_ssa_name (vec_dest, new_stmt);
3171 gimple_call_set_lhs (new_stmt, new_temp);
3173 else
3175 /* Generic support */
3176 gcc_assert (op_type == TREE_CODE_LENGTH (code));
3177 if (op_type != binary_op)
3178 vec_oprnd1 = NULL;
3179 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vec_oprnd0,
3180 vec_oprnd1);
3181 new_temp = make_ssa_name (vec_dest, new_stmt);
3182 gimple_assign_set_lhs (new_stmt, new_temp);
3184 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3186 return new_stmt;
3190 /* Get vectorized definitions for loop-based vectorization. For the first
3191 operand we call vect_get_vec_def_for_operand() (with OPRND containing
3192 scalar operand), and for the rest we get a copy with
3193 vect_get_vec_def_for_stmt_copy() using the previous vector definition
3194 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
3195 The vectors are collected into VEC_OPRNDS. */
3197 static void
3198 vect_get_loop_based_defs (tree *oprnd, gimple stmt, enum vect_def_type dt,
3199 vec<tree> *vec_oprnds, int multi_step_cvt)
3201 tree vec_oprnd;
3203 /* Get first vector operand. */
3204 /* All the vector operands except the very first one (that is scalar oprnd)
3205 are stmt copies. */
3206 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
3207 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt, NULL);
3208 else
3209 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
3211 vec_oprnds->quick_push (vec_oprnd);
3213 /* Get second vector operand. */
3214 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
3215 vec_oprnds->quick_push (vec_oprnd);
3217 *oprnd = vec_oprnd;
3219 /* For conversion in multiple steps, continue to get operands
3220 recursively. */
3221 if (multi_step_cvt)
3222 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
3226 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
3227 For multi-step conversions store the resulting vectors and call the function
3228 recursively. */
3230 static void
3231 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
3232 int multi_step_cvt, gimple stmt,
3233 vec<tree> vec_dsts,
3234 gimple_stmt_iterator *gsi,
3235 slp_tree slp_node, enum tree_code code,
3236 stmt_vec_info *prev_stmt_info)
3238 unsigned int i;
3239 tree vop0, vop1, new_tmp, vec_dest;
3240 gimple new_stmt;
3241 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3243 vec_dest = vec_dsts.pop ();
3245 for (i = 0; i < vec_oprnds->length (); i += 2)
3247 /* Create demotion operation. */
3248 vop0 = (*vec_oprnds)[i];
3249 vop1 = (*vec_oprnds)[i + 1];
3250 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
3251 new_tmp = make_ssa_name (vec_dest, new_stmt);
3252 gimple_assign_set_lhs (new_stmt, new_tmp);
3253 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3255 if (multi_step_cvt)
3256 /* Store the resulting vector for next recursive call. */
3257 (*vec_oprnds)[i/2] = new_tmp;
3258 else
3260 /* This is the last step of the conversion sequence. Store the
3261 vectors in SLP_NODE or in vector info of the scalar statement
3262 (or in STMT_VINFO_RELATED_STMT chain). */
3263 if (slp_node)
3264 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3265 else
3267 if (!*prev_stmt_info)
3268 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3269 else
3270 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
3272 *prev_stmt_info = vinfo_for_stmt (new_stmt);
3277 /* For multi-step demotion operations we first generate demotion operations
3278 from the source type to the intermediate types, and then combine the
3279 results (stored in VEC_OPRNDS) in demotion operation to the destination
3280 type. */
3281 if (multi_step_cvt)
3283 /* At each level of recursion we have half of the operands we had at the
3284 previous level. */
3285 vec_oprnds->truncate ((i+1)/2);
3286 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
3287 stmt, vec_dsts, gsi, slp_node,
3288 VEC_PACK_TRUNC_EXPR,
3289 prev_stmt_info);
3292 vec_dsts.quick_push (vec_dest);
3296 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
3297 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
3298 the resulting vectors and call the function recursively. */
3300 static void
3301 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
3302 vec<tree> *vec_oprnds1,
3303 gimple stmt, tree vec_dest,
3304 gimple_stmt_iterator *gsi,
3305 enum tree_code code1,
3306 enum tree_code code2, tree decl1,
3307 tree decl2, int op_type)
3309 int i;
3310 tree vop0, vop1, new_tmp1, new_tmp2;
3311 gimple new_stmt1, new_stmt2;
3312 vec<tree> vec_tmp = vNULL;
3314 vec_tmp.create (vec_oprnds0->length () * 2);
3315 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
3317 if (op_type == binary_op)
3318 vop1 = (*vec_oprnds1)[i];
3319 else
3320 vop1 = NULL_TREE;
3322 /* Generate the two halves of promotion operation. */
3323 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
3324 op_type, vec_dest, gsi, stmt);
3325 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
3326 op_type, vec_dest, gsi, stmt);
3327 if (is_gimple_call (new_stmt1))
3329 new_tmp1 = gimple_call_lhs (new_stmt1);
3330 new_tmp2 = gimple_call_lhs (new_stmt2);
3332 else
3334 new_tmp1 = gimple_assign_lhs (new_stmt1);
3335 new_tmp2 = gimple_assign_lhs (new_stmt2);
3338 /* Store the results for the next step. */
3339 vec_tmp.quick_push (new_tmp1);
3340 vec_tmp.quick_push (new_tmp2);
3343 vec_oprnds0->release ();
3344 *vec_oprnds0 = vec_tmp;
3348 /* Check if STMT performs a conversion operation, that can be vectorized.
3349 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3350 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3351 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3353 static bool
3354 vectorizable_conversion (gimple stmt, gimple_stmt_iterator *gsi,
3355 gimple *vec_stmt, slp_tree slp_node)
3357 tree vec_dest;
3358 tree scalar_dest;
3359 tree op0, op1 = NULL_TREE;
3360 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3361 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3362 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3363 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
3364 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
3365 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3366 tree new_temp;
3367 tree def;
3368 gimple def_stmt;
3369 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3370 gimple new_stmt = NULL;
3371 stmt_vec_info prev_stmt_info;
3372 int nunits_in;
3373 int nunits_out;
3374 tree vectype_out, vectype_in;
3375 int ncopies, i, j;
3376 tree lhs_type, rhs_type;
3377 enum { NARROW, NONE, WIDEN } modifier;
3378 vec<tree> vec_oprnds0 = vNULL;
3379 vec<tree> vec_oprnds1 = vNULL;
3380 tree vop0;
3381 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3382 int multi_step_cvt = 0;
3383 vec<tree> vec_dsts = vNULL;
3384 vec<tree> interm_types = vNULL;
3385 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
3386 int op_type;
3387 enum machine_mode rhs_mode;
3388 unsigned short fltsz;
3390 /* Is STMT a vectorizable conversion? */
3392 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3393 return false;
3395 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3396 return false;
3398 if (!is_gimple_assign (stmt))
3399 return false;
3401 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3402 return false;
3404 code = gimple_assign_rhs_code (stmt);
3405 if (!CONVERT_EXPR_CODE_P (code)
3406 && code != FIX_TRUNC_EXPR
3407 && code != FLOAT_EXPR
3408 && code != WIDEN_MULT_EXPR
3409 && code != WIDEN_LSHIFT_EXPR)
3410 return false;
3412 op_type = TREE_CODE_LENGTH (code);
3414 /* Check types of lhs and rhs. */
3415 scalar_dest = gimple_assign_lhs (stmt);
3416 lhs_type = TREE_TYPE (scalar_dest);
3417 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3419 op0 = gimple_assign_rhs1 (stmt);
3420 rhs_type = TREE_TYPE (op0);
3422 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3423 && !((INTEGRAL_TYPE_P (lhs_type)
3424 && INTEGRAL_TYPE_P (rhs_type))
3425 || (SCALAR_FLOAT_TYPE_P (lhs_type)
3426 && SCALAR_FLOAT_TYPE_P (rhs_type))))
3427 return false;
3429 if ((INTEGRAL_TYPE_P (lhs_type)
3430 && (TYPE_PRECISION (lhs_type)
3431 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
3432 || (INTEGRAL_TYPE_P (rhs_type)
3433 && (TYPE_PRECISION (rhs_type)
3434 != GET_MODE_PRECISION (TYPE_MODE (rhs_type)))))
3436 if (dump_enabled_p ())
3437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3438 "type conversion to/from bit-precision unsupported."
3439 "\n");
3440 return false;
3443 /* Check the operands of the operation. */
3444 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
3445 &def_stmt, &def, &dt[0], &vectype_in))
3447 if (dump_enabled_p ())
3448 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3449 "use not simple.\n");
3450 return false;
3452 if (op_type == binary_op)
3454 bool ok;
3456 op1 = gimple_assign_rhs2 (stmt);
3457 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3458 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3459 OP1. */
3460 if (CONSTANT_CLASS_P (op0))
3461 ok = vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo,
3462 &def_stmt, &def, &dt[1], &vectype_in);
3463 else
3464 ok = vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
3465 &def, &dt[1]);
3467 if (!ok)
3469 if (dump_enabled_p ())
3470 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3471 "use not simple.\n");
3472 return false;
3476 /* If op0 is an external or constant defs use a vector type of
3477 the same size as the output vector type. */
3478 if (!vectype_in)
3479 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
3480 if (vec_stmt)
3481 gcc_assert (vectype_in);
3482 if (!vectype_in)
3484 if (dump_enabled_p ())
3486 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3487 "no vectype for scalar type ");
3488 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3489 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3492 return false;
3495 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3496 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3497 if (nunits_in < nunits_out)
3498 modifier = NARROW;
3499 else if (nunits_out == nunits_in)
3500 modifier = NONE;
3501 else
3502 modifier = WIDEN;
3504 /* Multiple types in SLP are handled by creating the appropriate number of
3505 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3506 case of SLP. */
3507 if (slp_node || PURE_SLP_STMT (stmt_info))
3508 ncopies = 1;
3509 else if (modifier == NARROW)
3510 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3511 else
3512 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3514 /* Sanity check: make sure that at least one copy of the vectorized stmt
3515 needs to be generated. */
3516 gcc_assert (ncopies >= 1);
3518 /* Supportable by target? */
3519 switch (modifier)
3521 case NONE:
3522 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3523 return false;
3524 if (supportable_convert_operation (code, vectype_out, vectype_in,
3525 &decl1, &code1))
3526 break;
3527 /* FALLTHRU */
3528 unsupported:
3529 if (dump_enabled_p ())
3530 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3531 "conversion not supported by target.\n");
3532 return false;
3534 case WIDEN:
3535 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
3536 &code1, &code2, &multi_step_cvt,
3537 &interm_types))
3539 /* Binary widening operation can only be supported directly by the
3540 architecture. */
3541 gcc_assert (!(multi_step_cvt && op_type == binary_op));
3542 break;
3545 if (code != FLOAT_EXPR
3546 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3547 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3548 goto unsupported;
3550 rhs_mode = TYPE_MODE (rhs_type);
3551 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
3552 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
3553 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
3554 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
3556 cvt_type
3557 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3558 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3559 if (cvt_type == NULL_TREE)
3560 goto unsupported;
3562 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3564 if (!supportable_convert_operation (code, vectype_out,
3565 cvt_type, &decl1, &codecvt1))
3566 goto unsupported;
3568 else if (!supportable_widening_operation (code, stmt, vectype_out,
3569 cvt_type, &codecvt1,
3570 &codecvt2, &multi_step_cvt,
3571 &interm_types))
3572 continue;
3573 else
3574 gcc_assert (multi_step_cvt == 0);
3576 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
3577 vectype_in, &code1, &code2,
3578 &multi_step_cvt, &interm_types))
3579 break;
3582 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
3583 goto unsupported;
3585 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3586 codecvt2 = ERROR_MARK;
3587 else
3589 multi_step_cvt++;
3590 interm_types.safe_push (cvt_type);
3591 cvt_type = NULL_TREE;
3593 break;
3595 case NARROW:
3596 gcc_assert (op_type == unary_op);
3597 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
3598 &code1, &multi_step_cvt,
3599 &interm_types))
3600 break;
3602 if (code != FIX_TRUNC_EXPR
3603 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3604 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3605 goto unsupported;
3607 rhs_mode = TYPE_MODE (rhs_type);
3608 cvt_type
3609 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3610 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3611 if (cvt_type == NULL_TREE)
3612 goto unsupported;
3613 if (!supportable_convert_operation (code, cvt_type, vectype_in,
3614 &decl1, &codecvt1))
3615 goto unsupported;
3616 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
3617 &code1, &multi_step_cvt,
3618 &interm_types))
3619 break;
3620 goto unsupported;
3622 default:
3623 gcc_unreachable ();
3626 if (!vec_stmt) /* transformation not required. */
3628 if (dump_enabled_p ())
3629 dump_printf_loc (MSG_NOTE, vect_location,
3630 "=== vectorizable_conversion ===\n");
3631 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
3633 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
3634 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3636 else if (modifier == NARROW)
3638 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
3639 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3641 else
3643 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
3644 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3646 interm_types.release ();
3647 return true;
3650 /** Transform. **/
3651 if (dump_enabled_p ())
3652 dump_printf_loc (MSG_NOTE, vect_location,
3653 "transform conversion. ncopies = %d.\n", ncopies);
3655 if (op_type == binary_op)
3657 if (CONSTANT_CLASS_P (op0))
3658 op0 = fold_convert (TREE_TYPE (op1), op0);
3659 else if (CONSTANT_CLASS_P (op1))
3660 op1 = fold_convert (TREE_TYPE (op0), op1);
3663 /* In case of multi-step conversion, we first generate conversion operations
3664 to the intermediate types, and then from that types to the final one.
3665 We create vector destinations for the intermediate type (TYPES) received
3666 from supportable_*_operation, and store them in the correct order
3667 for future use in vect_create_vectorized_*_stmts (). */
3668 vec_dsts.create (multi_step_cvt + 1);
3669 vec_dest = vect_create_destination_var (scalar_dest,
3670 (cvt_type && modifier == WIDEN)
3671 ? cvt_type : vectype_out);
3672 vec_dsts.quick_push (vec_dest);
3674 if (multi_step_cvt)
3676 for (i = interm_types.length () - 1;
3677 interm_types.iterate (i, &intermediate_type); i--)
3679 vec_dest = vect_create_destination_var (scalar_dest,
3680 intermediate_type);
3681 vec_dsts.quick_push (vec_dest);
3685 if (cvt_type)
3686 vec_dest = vect_create_destination_var (scalar_dest,
3687 modifier == WIDEN
3688 ? vectype_out : cvt_type);
3690 if (!slp_node)
3692 if (modifier == WIDEN)
3694 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
3695 if (op_type == binary_op)
3696 vec_oprnds1.create (1);
3698 else if (modifier == NARROW)
3699 vec_oprnds0.create (
3700 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
3702 else if (code == WIDEN_LSHIFT_EXPR)
3703 vec_oprnds1.create (slp_node->vec_stmts_size);
3705 last_oprnd = op0;
3706 prev_stmt_info = NULL;
3707 switch (modifier)
3709 case NONE:
3710 for (j = 0; j < ncopies; j++)
3712 if (j == 0)
3713 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
3714 -1);
3715 else
3716 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3718 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3720 /* Arguments are ready, create the new vector stmt. */
3721 if (code1 == CALL_EXPR)
3723 new_stmt = gimple_build_call (decl1, 1, vop0);
3724 new_temp = make_ssa_name (vec_dest, new_stmt);
3725 gimple_call_set_lhs (new_stmt, new_temp);
3727 else
3729 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
3730 new_stmt = gimple_build_assign_with_ops (code1, vec_dest,
3731 vop0, NULL);
3732 new_temp = make_ssa_name (vec_dest, new_stmt);
3733 gimple_assign_set_lhs (new_stmt, new_temp);
3736 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3737 if (slp_node)
3738 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3741 if (j == 0)
3742 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3743 else
3744 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3745 prev_stmt_info = vinfo_for_stmt (new_stmt);
3747 break;
3749 case WIDEN:
3750 /* In case the vectorization factor (VF) is bigger than the number
3751 of elements that we can fit in a vectype (nunits), we have to
3752 generate more than one vector stmt - i.e - we need to "unroll"
3753 the vector stmt by a factor VF/nunits. */
3754 for (j = 0; j < ncopies; j++)
3756 /* Handle uses. */
3757 if (j == 0)
3759 if (slp_node)
3761 if (code == WIDEN_LSHIFT_EXPR)
3763 unsigned int k;
3765 vec_oprnd1 = op1;
3766 /* Store vec_oprnd1 for every vector stmt to be created
3767 for SLP_NODE. We check during the analysis that all
3768 the shift arguments are the same. */
3769 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
3770 vec_oprnds1.quick_push (vec_oprnd1);
3772 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3773 slp_node, -1);
3775 else
3776 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
3777 &vec_oprnds1, slp_node, -1);
3779 else
3781 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt, NULL);
3782 vec_oprnds0.quick_push (vec_oprnd0);
3783 if (op_type == binary_op)
3785 if (code == WIDEN_LSHIFT_EXPR)
3786 vec_oprnd1 = op1;
3787 else
3788 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt,
3789 NULL);
3790 vec_oprnds1.quick_push (vec_oprnd1);
3794 else
3796 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3797 vec_oprnds0.truncate (0);
3798 vec_oprnds0.quick_push (vec_oprnd0);
3799 if (op_type == binary_op)
3801 if (code == WIDEN_LSHIFT_EXPR)
3802 vec_oprnd1 = op1;
3803 else
3804 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
3805 vec_oprnd1);
3806 vec_oprnds1.truncate (0);
3807 vec_oprnds1.quick_push (vec_oprnd1);
3811 /* Arguments are ready. Create the new vector stmts. */
3812 for (i = multi_step_cvt; i >= 0; i--)
3814 tree this_dest = vec_dsts[i];
3815 enum tree_code c1 = code1, c2 = code2;
3816 if (i == 0 && codecvt2 != ERROR_MARK)
3818 c1 = codecvt1;
3819 c2 = codecvt2;
3821 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
3822 &vec_oprnds1,
3823 stmt, this_dest, gsi,
3824 c1, c2, decl1, decl2,
3825 op_type);
3828 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3830 if (cvt_type)
3832 if (codecvt1 == CALL_EXPR)
3834 new_stmt = gimple_build_call (decl1, 1, vop0);
3835 new_temp = make_ssa_name (vec_dest, new_stmt);
3836 gimple_call_set_lhs (new_stmt, new_temp);
3838 else
3840 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3841 new_temp = make_ssa_name (vec_dest, NULL);
3842 new_stmt = gimple_build_assign_with_ops (codecvt1,
3843 new_temp,
3844 vop0, NULL);
3847 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3849 else
3850 new_stmt = SSA_NAME_DEF_STMT (vop0);
3852 if (slp_node)
3853 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3854 else
3856 if (!prev_stmt_info)
3857 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3858 else
3859 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3860 prev_stmt_info = vinfo_for_stmt (new_stmt);
3865 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3866 break;
3868 case NARROW:
3869 /* In case the vectorization factor (VF) is bigger than the number
3870 of elements that we can fit in a vectype (nunits), we have to
3871 generate more than one vector stmt - i.e - we need to "unroll"
3872 the vector stmt by a factor VF/nunits. */
3873 for (j = 0; j < ncopies; j++)
3875 /* Handle uses. */
3876 if (slp_node)
3877 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3878 slp_node, -1);
3879 else
3881 vec_oprnds0.truncate (0);
3882 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
3883 vect_pow2 (multi_step_cvt) - 1);
3886 /* Arguments are ready. Create the new vector stmts. */
3887 if (cvt_type)
3888 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3890 if (codecvt1 == CALL_EXPR)
3892 new_stmt = gimple_build_call (decl1, 1, vop0);
3893 new_temp = make_ssa_name (vec_dest, new_stmt);
3894 gimple_call_set_lhs (new_stmt, new_temp);
3896 else
3898 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
3899 new_temp = make_ssa_name (vec_dest, NULL);
3900 new_stmt = gimple_build_assign_with_ops (codecvt1, new_temp,
3901 vop0, NULL);
3904 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3905 vec_oprnds0[i] = new_temp;
3908 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
3909 stmt, vec_dsts, gsi,
3910 slp_node, code1,
3911 &prev_stmt_info);
3914 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
3915 break;
3918 vec_oprnds0.release ();
3919 vec_oprnds1.release ();
3920 vec_dsts.release ();
3921 interm_types.release ();
3923 return true;
3927 /* Function vectorizable_assignment.
3929 Check if STMT performs an assignment (copy) that can be vectorized.
3930 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3931 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3932 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3934 static bool
3935 vectorizable_assignment (gimple stmt, gimple_stmt_iterator *gsi,
3936 gimple *vec_stmt, slp_tree slp_node)
3938 tree vec_dest;
3939 tree scalar_dest;
3940 tree op;
3941 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3942 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3943 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3944 tree new_temp;
3945 tree def;
3946 gimple def_stmt;
3947 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3948 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
3949 int ncopies;
3950 int i, j;
3951 vec<tree> vec_oprnds = vNULL;
3952 tree vop;
3953 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3954 gimple new_stmt = NULL;
3955 stmt_vec_info prev_stmt_info = NULL;
3956 enum tree_code code;
3957 tree vectype_in;
3959 /* Multiple types in SLP are handled by creating the appropriate number of
3960 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3961 case of SLP. */
3962 if (slp_node || PURE_SLP_STMT (stmt_info))
3963 ncopies = 1;
3964 else
3965 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
3967 gcc_assert (ncopies >= 1);
3969 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3970 return false;
3972 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3973 return false;
3975 /* Is vectorizable assignment? */
3976 if (!is_gimple_assign (stmt))
3977 return false;
3979 scalar_dest = gimple_assign_lhs (stmt);
3980 if (TREE_CODE (scalar_dest) != SSA_NAME)
3981 return false;
3983 code = gimple_assign_rhs_code (stmt);
3984 if (gimple_assign_single_p (stmt)
3985 || code == PAREN_EXPR
3986 || CONVERT_EXPR_CODE_P (code))
3987 op = gimple_assign_rhs1 (stmt);
3988 else
3989 return false;
3991 if (code == VIEW_CONVERT_EXPR)
3992 op = TREE_OPERAND (op, 0);
3994 if (!vect_is_simple_use_1 (op, stmt, loop_vinfo, bb_vinfo,
3995 &def_stmt, &def, &dt[0], &vectype_in))
3997 if (dump_enabled_p ())
3998 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3999 "use not simple.\n");
4000 return false;
4003 /* We can handle NOP_EXPR conversions that do not change the number
4004 of elements or the vector size. */
4005 if ((CONVERT_EXPR_CODE_P (code)
4006 || code == VIEW_CONVERT_EXPR)
4007 && (!vectype_in
4008 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
4009 || (GET_MODE_SIZE (TYPE_MODE (vectype))
4010 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
4011 return false;
4013 /* We do not handle bit-precision changes. */
4014 if ((CONVERT_EXPR_CODE_P (code)
4015 || code == VIEW_CONVERT_EXPR)
4016 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4017 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4018 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4019 || ((TYPE_PRECISION (TREE_TYPE (op))
4020 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
4021 /* But a conversion that does not change the bit-pattern is ok. */
4022 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4023 > TYPE_PRECISION (TREE_TYPE (op)))
4024 && TYPE_UNSIGNED (TREE_TYPE (op))))
4026 if (dump_enabled_p ())
4027 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4028 "type conversion to/from bit-precision "
4029 "unsupported.\n");
4030 return false;
4033 if (!vec_stmt) /* transformation not required. */
4035 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
4036 if (dump_enabled_p ())
4037 dump_printf_loc (MSG_NOTE, vect_location,
4038 "=== vectorizable_assignment ===\n");
4039 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4040 return true;
4043 /** Transform. **/
4044 if (dump_enabled_p ())
4045 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
4047 /* Handle def. */
4048 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4050 /* Handle use. */
4051 for (j = 0; j < ncopies; j++)
4053 /* Handle uses. */
4054 if (j == 0)
4055 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
4056 else
4057 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
4059 /* Arguments are ready. create the new vector stmt. */
4060 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
4062 if (CONVERT_EXPR_CODE_P (code)
4063 || code == VIEW_CONVERT_EXPR)
4064 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
4065 new_stmt = gimple_build_assign (vec_dest, vop);
4066 new_temp = make_ssa_name (vec_dest, new_stmt);
4067 gimple_assign_set_lhs (new_stmt, new_temp);
4068 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4069 if (slp_node)
4070 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4073 if (slp_node)
4074 continue;
4076 if (j == 0)
4077 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4078 else
4079 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4081 prev_stmt_info = vinfo_for_stmt (new_stmt);
4084 vec_oprnds.release ();
4085 return true;
4089 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
4090 either as shift by a scalar or by a vector. */
4092 bool
4093 vect_supportable_shift (enum tree_code code, tree scalar_type)
4096 enum machine_mode vec_mode;
4097 optab optab;
4098 int icode;
4099 tree vectype;
4101 vectype = get_vectype_for_scalar_type (scalar_type);
4102 if (!vectype)
4103 return false;
4105 optab = optab_for_tree_code (code, vectype, optab_scalar);
4106 if (!optab
4107 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
4109 optab = optab_for_tree_code (code, vectype, optab_vector);
4110 if (!optab
4111 || (optab_handler (optab, TYPE_MODE (vectype))
4112 == CODE_FOR_nothing))
4113 return false;
4116 vec_mode = TYPE_MODE (vectype);
4117 icode = (int) optab_handler (optab, vec_mode);
4118 if (icode == CODE_FOR_nothing)
4119 return false;
4121 return true;
4125 /* Function vectorizable_shift.
4127 Check if STMT performs a shift operation that can be vectorized.
4128 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4129 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4130 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4132 static bool
4133 vectorizable_shift (gimple stmt, gimple_stmt_iterator *gsi,
4134 gimple *vec_stmt, slp_tree slp_node)
4136 tree vec_dest;
4137 tree scalar_dest;
4138 tree op0, op1 = NULL;
4139 tree vec_oprnd1 = NULL_TREE;
4140 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4141 tree vectype;
4142 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4143 enum tree_code code;
4144 enum machine_mode vec_mode;
4145 tree new_temp;
4146 optab optab;
4147 int icode;
4148 enum machine_mode optab_op2_mode;
4149 tree def;
4150 gimple def_stmt;
4151 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4152 gimple new_stmt = NULL;
4153 stmt_vec_info prev_stmt_info;
4154 int nunits_in;
4155 int nunits_out;
4156 tree vectype_out;
4157 tree op1_vectype;
4158 int ncopies;
4159 int j, i;
4160 vec<tree> vec_oprnds0 = vNULL;
4161 vec<tree> vec_oprnds1 = vNULL;
4162 tree vop0, vop1;
4163 unsigned int k;
4164 bool scalar_shift_arg = true;
4165 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4166 int vf;
4168 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4169 return false;
4171 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4172 return false;
4174 /* Is STMT a vectorizable binary/unary operation? */
4175 if (!is_gimple_assign (stmt))
4176 return false;
4178 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4179 return false;
4181 code = gimple_assign_rhs_code (stmt);
4183 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4184 || code == RROTATE_EXPR))
4185 return false;
4187 scalar_dest = gimple_assign_lhs (stmt);
4188 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4189 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4190 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4192 if (dump_enabled_p ())
4193 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4194 "bit-precision shifts not supported.\n");
4195 return false;
4198 op0 = gimple_assign_rhs1 (stmt);
4199 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
4200 &def_stmt, &def, &dt[0], &vectype))
4202 if (dump_enabled_p ())
4203 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4204 "use not simple.\n");
4205 return false;
4207 /* If op0 is an external or constant def use a vector type with
4208 the same size as the output vector type. */
4209 if (!vectype)
4210 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4211 if (vec_stmt)
4212 gcc_assert (vectype);
4213 if (!vectype)
4215 if (dump_enabled_p ())
4216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4217 "no vectype for scalar type\n");
4218 return false;
4221 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4222 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4223 if (nunits_out != nunits_in)
4224 return false;
4226 op1 = gimple_assign_rhs2 (stmt);
4227 if (!vect_is_simple_use_1 (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4228 &def, &dt[1], &op1_vectype))
4230 if (dump_enabled_p ())
4231 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4232 "use not simple.\n");
4233 return false;
4236 if (loop_vinfo)
4237 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4238 else
4239 vf = 1;
4241 /* Multiple types in SLP are handled by creating the appropriate number of
4242 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4243 case of SLP. */
4244 if (slp_node || PURE_SLP_STMT (stmt_info))
4245 ncopies = 1;
4246 else
4247 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4249 gcc_assert (ncopies >= 1);
4251 /* Determine whether the shift amount is a vector, or scalar. If the
4252 shift/rotate amount is a vector, use the vector/vector shift optabs. */
4254 if (dt[1] == vect_internal_def && !slp_node)
4255 scalar_shift_arg = false;
4256 else if (dt[1] == vect_constant_def
4257 || dt[1] == vect_external_def
4258 || dt[1] == vect_internal_def)
4260 /* In SLP, need to check whether the shift count is the same,
4261 in loops if it is a constant or invariant, it is always
4262 a scalar shift. */
4263 if (slp_node)
4265 vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4266 gimple slpstmt;
4268 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
4269 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
4270 scalar_shift_arg = false;
4273 else
4275 if (dump_enabled_p ())
4276 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4277 "operand mode requires invariant argument.\n");
4278 return false;
4281 /* Vector shifted by vector. */
4282 if (!scalar_shift_arg)
4284 optab = optab_for_tree_code (code, vectype, optab_vector);
4285 if (dump_enabled_p ())
4286 dump_printf_loc (MSG_NOTE, vect_location,
4287 "vector/vector shift/rotate found.\n");
4289 if (!op1_vectype)
4290 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
4291 if (op1_vectype == NULL_TREE
4292 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
4294 if (dump_enabled_p ())
4295 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4296 "unusable type for last operand in"
4297 " vector/vector shift/rotate.\n");
4298 return false;
4301 /* See if the machine has a vector shifted by scalar insn and if not
4302 then see if it has a vector shifted by vector insn. */
4303 else
4305 optab = optab_for_tree_code (code, vectype, optab_scalar);
4306 if (optab
4307 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
4309 if (dump_enabled_p ())
4310 dump_printf_loc (MSG_NOTE, vect_location,
4311 "vector/scalar shift/rotate found.\n");
4313 else
4315 optab = optab_for_tree_code (code, vectype, optab_vector);
4316 if (optab
4317 && (optab_handler (optab, TYPE_MODE (vectype))
4318 != CODE_FOR_nothing))
4320 scalar_shift_arg = false;
4322 if (dump_enabled_p ())
4323 dump_printf_loc (MSG_NOTE, vect_location,
4324 "vector/vector shift/rotate found.\n");
4326 /* Unlike the other binary operators, shifts/rotates have
4327 the rhs being int, instead of the same type as the lhs,
4328 so make sure the scalar is the right type if we are
4329 dealing with vectors of long long/long/short/char. */
4330 if (dt[1] == vect_constant_def)
4331 op1 = fold_convert (TREE_TYPE (vectype), op1);
4332 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
4333 TREE_TYPE (op1)))
4335 if (slp_node
4336 && TYPE_MODE (TREE_TYPE (vectype))
4337 != TYPE_MODE (TREE_TYPE (op1)))
4339 if (dump_enabled_p ())
4340 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4341 "unusable type for last operand in"
4342 " vector/vector shift/rotate.\n");
4343 return false;
4345 if (vec_stmt && !slp_node)
4347 op1 = fold_convert (TREE_TYPE (vectype), op1);
4348 op1 = vect_init_vector (stmt, op1,
4349 TREE_TYPE (vectype), NULL);
4356 /* Supportable by target? */
4357 if (!optab)
4359 if (dump_enabled_p ())
4360 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4361 "no optab.\n");
4362 return false;
4364 vec_mode = TYPE_MODE (vectype);
4365 icode = (int) optab_handler (optab, vec_mode);
4366 if (icode == CODE_FOR_nothing)
4368 if (dump_enabled_p ())
4369 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4370 "op not supported by target.\n");
4371 /* Check only during analysis. */
4372 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4373 || (vf < vect_min_worthwhile_factor (code)
4374 && !vec_stmt))
4375 return false;
4376 if (dump_enabled_p ())
4377 dump_printf_loc (MSG_NOTE, vect_location,
4378 "proceeding using word mode.\n");
4381 /* Worthwhile without SIMD support? Check only during analysis. */
4382 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4383 && vf < vect_min_worthwhile_factor (code)
4384 && !vec_stmt)
4386 if (dump_enabled_p ())
4387 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4388 "not worthwhile without SIMD support.\n");
4389 return false;
4392 if (!vec_stmt) /* transformation not required. */
4394 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
4395 if (dump_enabled_p ())
4396 dump_printf_loc (MSG_NOTE, vect_location,
4397 "=== vectorizable_shift ===\n");
4398 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4399 return true;
4402 /** Transform. **/
4404 if (dump_enabled_p ())
4405 dump_printf_loc (MSG_NOTE, vect_location,
4406 "transform binary/unary operation.\n");
4408 /* Handle def. */
4409 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4411 prev_stmt_info = NULL;
4412 for (j = 0; j < ncopies; j++)
4414 /* Handle uses. */
4415 if (j == 0)
4417 if (scalar_shift_arg)
4419 /* Vector shl and shr insn patterns can be defined with scalar
4420 operand 2 (shift operand). In this case, use constant or loop
4421 invariant op1 directly, without extending it to vector mode
4422 first. */
4423 optab_op2_mode = insn_data[icode].operand[2].mode;
4424 if (!VECTOR_MODE_P (optab_op2_mode))
4426 if (dump_enabled_p ())
4427 dump_printf_loc (MSG_NOTE, vect_location,
4428 "operand 1 using scalar mode.\n");
4429 vec_oprnd1 = op1;
4430 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
4431 vec_oprnds1.quick_push (vec_oprnd1);
4432 if (slp_node)
4434 /* Store vec_oprnd1 for every vector stmt to be created
4435 for SLP_NODE. We check during the analysis that all
4436 the shift arguments are the same.
4437 TODO: Allow different constants for different vector
4438 stmts generated for an SLP instance. */
4439 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4440 vec_oprnds1.quick_push (vec_oprnd1);
4445 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
4446 (a special case for certain kind of vector shifts); otherwise,
4447 operand 1 should be of a vector type (the usual case). */
4448 if (vec_oprnd1)
4449 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4450 slp_node, -1);
4451 else
4452 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4453 slp_node, -1);
4455 else
4456 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4458 /* Arguments are ready. Create the new vector stmt. */
4459 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4461 vop1 = vec_oprnds1[i];
4462 new_stmt = gimple_build_assign_with_ops (code, vec_dest, vop0, vop1);
4463 new_temp = make_ssa_name (vec_dest, new_stmt);
4464 gimple_assign_set_lhs (new_stmt, new_temp);
4465 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4466 if (slp_node)
4467 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4470 if (slp_node)
4471 continue;
4473 if (j == 0)
4474 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4475 else
4476 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4477 prev_stmt_info = vinfo_for_stmt (new_stmt);
4480 vec_oprnds0.release ();
4481 vec_oprnds1.release ();
4483 return true;
4487 /* Function vectorizable_operation.
4489 Check if STMT performs a binary, unary or ternary operation that can
4490 be vectorized.
4491 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4492 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4493 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4495 static bool
4496 vectorizable_operation (gimple stmt, gimple_stmt_iterator *gsi,
4497 gimple *vec_stmt, slp_tree slp_node)
4499 tree vec_dest;
4500 tree scalar_dest;
4501 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
4502 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4503 tree vectype;
4504 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4505 enum tree_code code;
4506 enum machine_mode vec_mode;
4507 tree new_temp;
4508 int op_type;
4509 optab optab;
4510 int icode;
4511 tree def;
4512 gimple def_stmt;
4513 enum vect_def_type dt[3]
4514 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
4515 gimple new_stmt = NULL;
4516 stmt_vec_info prev_stmt_info;
4517 int nunits_in;
4518 int nunits_out;
4519 tree vectype_out;
4520 int ncopies;
4521 int j, i;
4522 vec<tree> vec_oprnds0 = vNULL;
4523 vec<tree> vec_oprnds1 = vNULL;
4524 vec<tree> vec_oprnds2 = vNULL;
4525 tree vop0, vop1, vop2;
4526 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4527 int vf;
4529 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4530 return false;
4532 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4533 return false;
4535 /* Is STMT a vectorizable binary/unary operation? */
4536 if (!is_gimple_assign (stmt))
4537 return false;
4539 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4540 return false;
4542 code = gimple_assign_rhs_code (stmt);
4544 /* For pointer addition, we should use the normal plus for
4545 the vector addition. */
4546 if (code == POINTER_PLUS_EXPR)
4547 code = PLUS_EXPR;
4549 /* Support only unary or binary operations. */
4550 op_type = TREE_CODE_LENGTH (code);
4551 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
4553 if (dump_enabled_p ())
4554 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4555 "num. args = %d (not unary/binary/ternary op).\n",
4556 op_type);
4557 return false;
4560 scalar_dest = gimple_assign_lhs (stmt);
4561 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4563 /* Most operations cannot handle bit-precision types without extra
4564 truncations. */
4565 if ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4566 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4567 /* Exception are bitwise binary operations. */
4568 && code != BIT_IOR_EXPR
4569 && code != BIT_XOR_EXPR
4570 && code != BIT_AND_EXPR)
4572 if (dump_enabled_p ())
4573 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4574 "bit-precision arithmetic not supported.\n");
4575 return false;
4578 op0 = gimple_assign_rhs1 (stmt);
4579 if (!vect_is_simple_use_1 (op0, stmt, loop_vinfo, bb_vinfo,
4580 &def_stmt, &def, &dt[0], &vectype))
4582 if (dump_enabled_p ())
4583 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4584 "use not simple.\n");
4585 return false;
4587 /* If op0 is an external or constant def use a vector type with
4588 the same size as the output vector type. */
4589 if (!vectype)
4590 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4591 if (vec_stmt)
4592 gcc_assert (vectype);
4593 if (!vectype)
4595 if (dump_enabled_p ())
4597 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4598 "no vectype for scalar type ");
4599 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
4600 TREE_TYPE (op0));
4601 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4604 return false;
4607 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4608 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4609 if (nunits_out != nunits_in)
4610 return false;
4612 if (op_type == binary_op || op_type == ternary_op)
4614 op1 = gimple_assign_rhs2 (stmt);
4615 if (!vect_is_simple_use (op1, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4616 &def, &dt[1]))
4618 if (dump_enabled_p ())
4619 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4620 "use not simple.\n");
4621 return false;
4624 if (op_type == ternary_op)
4626 op2 = gimple_assign_rhs3 (stmt);
4627 if (!vect_is_simple_use (op2, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4628 &def, &dt[2]))
4630 if (dump_enabled_p ())
4631 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4632 "use not simple.\n");
4633 return false;
4637 if (loop_vinfo)
4638 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4639 else
4640 vf = 1;
4642 /* Multiple types in SLP are handled by creating the appropriate number of
4643 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4644 case of SLP. */
4645 if (slp_node || PURE_SLP_STMT (stmt_info))
4646 ncopies = 1;
4647 else
4648 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4650 gcc_assert (ncopies >= 1);
4652 /* Shifts are handled in vectorizable_shift (). */
4653 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4654 || code == RROTATE_EXPR)
4655 return false;
4657 /* Supportable by target? */
4659 vec_mode = TYPE_MODE (vectype);
4660 if (code == MULT_HIGHPART_EXPR)
4662 if (can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype)))
4663 icode = LAST_INSN_CODE;
4664 else
4665 icode = CODE_FOR_nothing;
4667 else
4669 optab = optab_for_tree_code (code, vectype, optab_default);
4670 if (!optab)
4672 if (dump_enabled_p ())
4673 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4674 "no optab.\n");
4675 return false;
4677 icode = (int) optab_handler (optab, vec_mode);
4680 if (icode == CODE_FOR_nothing)
4682 if (dump_enabled_p ())
4683 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4684 "op not supported by target.\n");
4685 /* Check only during analysis. */
4686 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4687 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
4688 return false;
4689 if (dump_enabled_p ())
4690 dump_printf_loc (MSG_NOTE, vect_location,
4691 "proceeding using word mode.\n");
4694 /* Worthwhile without SIMD support? Check only during analysis. */
4695 if (!VECTOR_MODE_P (vec_mode)
4696 && !vec_stmt
4697 && vf < vect_min_worthwhile_factor (code))
4699 if (dump_enabled_p ())
4700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4701 "not worthwhile without SIMD support.\n");
4702 return false;
4705 if (!vec_stmt) /* transformation not required. */
4707 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
4708 if (dump_enabled_p ())
4709 dump_printf_loc (MSG_NOTE, vect_location,
4710 "=== vectorizable_operation ===\n");
4711 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4712 return true;
4715 /** Transform. **/
4717 if (dump_enabled_p ())
4718 dump_printf_loc (MSG_NOTE, vect_location,
4719 "transform binary/unary operation.\n");
4721 /* Handle def. */
4722 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4724 /* In case the vectorization factor (VF) is bigger than the number
4725 of elements that we can fit in a vectype (nunits), we have to generate
4726 more than one vector stmt - i.e - we need to "unroll" the
4727 vector stmt by a factor VF/nunits. In doing so, we record a pointer
4728 from one copy of the vector stmt to the next, in the field
4729 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4730 stages to find the correct vector defs to be used when vectorizing
4731 stmts that use the defs of the current stmt. The example below
4732 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
4733 we need to create 4 vectorized stmts):
4735 before vectorization:
4736 RELATED_STMT VEC_STMT
4737 S1: x = memref - -
4738 S2: z = x + 1 - -
4740 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
4741 there):
4742 RELATED_STMT VEC_STMT
4743 VS1_0: vx0 = memref0 VS1_1 -
4744 VS1_1: vx1 = memref1 VS1_2 -
4745 VS1_2: vx2 = memref2 VS1_3 -
4746 VS1_3: vx3 = memref3 - -
4747 S1: x = load - VS1_0
4748 S2: z = x + 1 - -
4750 step2: vectorize stmt S2 (done here):
4751 To vectorize stmt S2 we first need to find the relevant vector
4752 def for the first operand 'x'. This is, as usual, obtained from
4753 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
4754 that defines 'x' (S1). This way we find the stmt VS1_0, and the
4755 relevant vector def 'vx0'. Having found 'vx0' we can generate
4756 the vector stmt VS2_0, and as usual, record it in the
4757 STMT_VINFO_VEC_STMT of stmt S2.
4758 When creating the second copy (VS2_1), we obtain the relevant vector
4759 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
4760 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
4761 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
4762 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
4763 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
4764 chain of stmts and pointers:
4765 RELATED_STMT VEC_STMT
4766 VS1_0: vx0 = memref0 VS1_1 -
4767 VS1_1: vx1 = memref1 VS1_2 -
4768 VS1_2: vx2 = memref2 VS1_3 -
4769 VS1_3: vx3 = memref3 - -
4770 S1: x = load - VS1_0
4771 VS2_0: vz0 = vx0 + v1 VS2_1 -
4772 VS2_1: vz1 = vx1 + v1 VS2_2 -
4773 VS2_2: vz2 = vx2 + v1 VS2_3 -
4774 VS2_3: vz3 = vx3 + v1 - -
4775 S2: z = x + 1 - VS2_0 */
4777 prev_stmt_info = NULL;
4778 for (j = 0; j < ncopies; j++)
4780 /* Handle uses. */
4781 if (j == 0)
4783 if (op_type == binary_op || op_type == ternary_op)
4784 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4785 slp_node, -1);
4786 else
4787 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4788 slp_node, -1);
4789 if (op_type == ternary_op)
4791 vec_oprnds2.create (1);
4792 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
4793 stmt,
4794 NULL));
4797 else
4799 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4800 if (op_type == ternary_op)
4802 tree vec_oprnd = vec_oprnds2.pop ();
4803 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
4804 vec_oprnd));
4808 /* Arguments are ready. Create the new vector stmt. */
4809 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4811 vop1 = ((op_type == binary_op || op_type == ternary_op)
4812 ? vec_oprnds1[i] : NULL_TREE);
4813 vop2 = ((op_type == ternary_op)
4814 ? vec_oprnds2[i] : NULL_TREE);
4815 new_stmt = gimple_build_assign_with_ops (code, vec_dest,
4816 vop0, vop1, vop2);
4817 new_temp = make_ssa_name (vec_dest, new_stmt);
4818 gimple_assign_set_lhs (new_stmt, new_temp);
4819 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4820 if (slp_node)
4821 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4824 if (slp_node)
4825 continue;
4827 if (j == 0)
4828 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4829 else
4830 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4831 prev_stmt_info = vinfo_for_stmt (new_stmt);
4834 vec_oprnds0.release ();
4835 vec_oprnds1.release ();
4836 vec_oprnds2.release ();
4838 return true;
4841 /* A helper function to ensure data reference DR's base alignment
4842 for STMT_INFO. */
4844 static void
4845 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
4847 if (!dr->aux)
4848 return;
4850 if (((dataref_aux *)dr->aux)->base_misaligned)
4852 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4853 tree base_decl = ((dataref_aux *)dr->aux)->base_decl;
4855 DECL_ALIGN (base_decl) = TYPE_ALIGN (vectype);
4856 DECL_USER_ALIGN (base_decl) = 1;
4857 ((dataref_aux *)dr->aux)->base_misaligned = false;
4862 /* Function vectorizable_store.
4864 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
4865 can be vectorized.
4866 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4867 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4868 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4870 static bool
4871 vectorizable_store (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
4872 slp_tree slp_node)
4874 tree scalar_dest;
4875 tree data_ref;
4876 tree op;
4877 tree vec_oprnd = NULL_TREE;
4878 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4879 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
4880 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4881 tree elem_type;
4882 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4883 struct loop *loop = NULL;
4884 enum machine_mode vec_mode;
4885 tree dummy;
4886 enum dr_alignment_support alignment_support_scheme;
4887 tree def;
4888 gimple def_stmt;
4889 enum vect_def_type dt;
4890 stmt_vec_info prev_stmt_info = NULL;
4891 tree dataref_ptr = NULL_TREE;
4892 tree dataref_offset = NULL_TREE;
4893 gimple ptr_incr = NULL;
4894 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4895 int ncopies;
4896 int j;
4897 gimple next_stmt, first_stmt = NULL;
4898 bool grouped_store = false;
4899 bool store_lanes_p = false;
4900 unsigned int group_size, i;
4901 vec<tree> dr_chain = vNULL;
4902 vec<tree> oprnds = vNULL;
4903 vec<tree> result_chain = vNULL;
4904 bool inv_p;
4905 vec<tree> vec_oprnds = vNULL;
4906 bool slp = (slp_node != NULL);
4907 unsigned int vec_num;
4908 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4909 tree aggr_type;
4911 if (loop_vinfo)
4912 loop = LOOP_VINFO_LOOP (loop_vinfo);
4914 /* Multiple types in SLP are handled by creating the appropriate number of
4915 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4916 case of SLP. */
4917 if (slp || PURE_SLP_STMT (stmt_info))
4918 ncopies = 1;
4919 else
4920 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4922 gcc_assert (ncopies >= 1);
4924 /* FORNOW. This restriction should be relaxed. */
4925 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
4927 if (dump_enabled_p ())
4928 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4929 "multiple types in nested loop.\n");
4930 return false;
4933 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4934 return false;
4936 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4937 return false;
4939 /* Is vectorizable store? */
4941 if (!is_gimple_assign (stmt))
4942 return false;
4944 scalar_dest = gimple_assign_lhs (stmt);
4945 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
4946 && is_pattern_stmt_p (stmt_info))
4947 scalar_dest = TREE_OPERAND (scalar_dest, 0);
4948 if (TREE_CODE (scalar_dest) != ARRAY_REF
4949 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
4950 && TREE_CODE (scalar_dest) != INDIRECT_REF
4951 && TREE_CODE (scalar_dest) != COMPONENT_REF
4952 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
4953 && TREE_CODE (scalar_dest) != REALPART_EXPR
4954 && TREE_CODE (scalar_dest) != MEM_REF)
4955 return false;
4957 gcc_assert (gimple_assign_single_p (stmt));
4958 op = gimple_assign_rhs1 (stmt);
4959 if (!vect_is_simple_use (op, stmt, loop_vinfo, bb_vinfo, &def_stmt,
4960 &def, &dt))
4962 if (dump_enabled_p ())
4963 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4964 "use not simple.\n");
4965 return false;
4968 elem_type = TREE_TYPE (vectype);
4969 vec_mode = TYPE_MODE (vectype);
4971 /* FORNOW. In some cases can vectorize even if data-type not supported
4972 (e.g. - array initialization with 0). */
4973 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
4974 return false;
4976 if (!STMT_VINFO_DATA_REF (stmt_info))
4977 return false;
4979 if (tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
4980 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
4981 size_zero_node) < 0)
4983 if (dump_enabled_p ())
4984 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4985 "negative step for store.\n");
4986 return false;
4989 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
4991 grouped_store = true;
4992 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
4993 if (!slp && !PURE_SLP_STMT (stmt_info))
4995 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
4996 if (vect_store_lanes_supported (vectype, group_size))
4997 store_lanes_p = true;
4998 else if (!vect_grouped_store_supported (vectype, group_size))
4999 return false;
5002 if (first_stmt == stmt)
5004 /* STMT is the leader of the group. Check the operands of all the
5005 stmts of the group. */
5006 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
5007 while (next_stmt)
5009 gcc_assert (gimple_assign_single_p (next_stmt));
5010 op = gimple_assign_rhs1 (next_stmt);
5011 if (!vect_is_simple_use (op, next_stmt, loop_vinfo, bb_vinfo,
5012 &def_stmt, &def, &dt))
5014 if (dump_enabled_p ())
5015 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5016 "use not simple.\n");
5017 return false;
5019 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5024 if (!vec_stmt) /* transformation not required. */
5026 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
5027 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
5028 NULL, NULL, NULL);
5029 return true;
5032 /** Transform. **/
5034 ensure_base_align (stmt_info, dr);
5036 if (grouped_store)
5038 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5039 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5041 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
5043 /* FORNOW */
5044 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
5046 /* We vectorize all the stmts of the interleaving group when we
5047 reach the last stmt in the group. */
5048 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
5049 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
5050 && !slp)
5052 *vec_stmt = NULL;
5053 return true;
5056 if (slp)
5058 grouped_store = false;
5059 /* VEC_NUM is the number of vect stmts to be created for this
5060 group. */
5061 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5062 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5063 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5064 op = gimple_assign_rhs1 (first_stmt);
5066 else
5067 /* VEC_NUM is the number of vect stmts to be created for this
5068 group. */
5069 vec_num = group_size;
5071 else
5073 first_stmt = stmt;
5074 first_dr = dr;
5075 group_size = vec_num = 1;
5078 if (dump_enabled_p ())
5079 dump_printf_loc (MSG_NOTE, vect_location,
5080 "transform store. ncopies = %d\n", ncopies);
5082 dr_chain.create (group_size);
5083 oprnds.create (group_size);
5085 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
5086 gcc_assert (alignment_support_scheme);
5087 /* Targets with store-lane instructions must not require explicit
5088 realignment. */
5089 gcc_assert (!store_lanes_p
5090 || alignment_support_scheme == dr_aligned
5091 || alignment_support_scheme == dr_unaligned_supported);
5093 if (store_lanes_p)
5094 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5095 else
5096 aggr_type = vectype;
5098 /* In case the vectorization factor (VF) is bigger than the number
5099 of elements that we can fit in a vectype (nunits), we have to generate
5100 more than one vector stmt - i.e - we need to "unroll" the
5101 vector stmt by a factor VF/nunits. For more details see documentation in
5102 vect_get_vec_def_for_copy_stmt. */
5104 /* In case of interleaving (non-unit grouped access):
5106 S1: &base + 2 = x2
5107 S2: &base = x0
5108 S3: &base + 1 = x1
5109 S4: &base + 3 = x3
5111 We create vectorized stores starting from base address (the access of the
5112 first stmt in the chain (S2 in the above example), when the last store stmt
5113 of the chain (S4) is reached:
5115 VS1: &base = vx2
5116 VS2: &base + vec_size*1 = vx0
5117 VS3: &base + vec_size*2 = vx1
5118 VS4: &base + vec_size*3 = vx3
5120 Then permutation statements are generated:
5122 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
5123 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
5126 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5127 (the order of the data-refs in the output of vect_permute_store_chain
5128 corresponds to the order of scalar stmts in the interleaving chain - see
5129 the documentation of vect_permute_store_chain()).
5131 In case of both multiple types and interleaving, above vector stores and
5132 permutation stmts are created for every copy. The result vector stmts are
5133 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
5134 STMT_VINFO_RELATED_STMT for the next copies.
5137 prev_stmt_info = NULL;
5138 for (j = 0; j < ncopies; j++)
5140 gimple new_stmt;
5142 if (j == 0)
5144 if (slp)
5146 /* Get vectorized arguments for SLP_NODE. */
5147 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
5148 NULL, slp_node, -1);
5150 vec_oprnd = vec_oprnds[0];
5152 else
5154 /* For interleaved stores we collect vectorized defs for all the
5155 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
5156 used as an input to vect_permute_store_chain(), and OPRNDS as
5157 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
5159 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5160 OPRNDS are of size 1. */
5161 next_stmt = first_stmt;
5162 for (i = 0; i < group_size; i++)
5164 /* Since gaps are not supported for interleaved stores,
5165 GROUP_SIZE is the exact number of stmts in the chain.
5166 Therefore, NEXT_STMT can't be NULL_TREE. In case that
5167 there is no interleaving, GROUP_SIZE is 1, and only one
5168 iteration of the loop will be executed. */
5169 gcc_assert (next_stmt
5170 && gimple_assign_single_p (next_stmt));
5171 op = gimple_assign_rhs1 (next_stmt);
5173 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt,
5174 NULL);
5175 dr_chain.quick_push (vec_oprnd);
5176 oprnds.quick_push (vec_oprnd);
5177 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5181 /* We should have catched mismatched types earlier. */
5182 gcc_assert (useless_type_conversion_p (vectype,
5183 TREE_TYPE (vec_oprnd)));
5184 bool simd_lane_access_p
5185 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5186 if (simd_lane_access_p
5187 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5188 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5189 && integer_zerop (DR_OFFSET (first_dr))
5190 && integer_zerop (DR_INIT (first_dr))
5191 && alias_sets_conflict_p (get_alias_set (aggr_type),
5192 get_alias_set (DR_REF (first_dr))))
5194 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5195 dataref_offset = build_int_cst (reference_alias_ptr_type
5196 (DR_REF (first_dr)), 0);
5197 inv_p = false;
5199 else
5200 dataref_ptr
5201 = vect_create_data_ref_ptr (first_stmt, aggr_type,
5202 simd_lane_access_p ? loop : NULL,
5203 NULL_TREE, &dummy, gsi, &ptr_incr,
5204 simd_lane_access_p, &inv_p);
5205 gcc_assert (bb_vinfo || !inv_p);
5207 else
5209 /* For interleaved stores we created vectorized defs for all the
5210 defs stored in OPRNDS in the previous iteration (previous copy).
5211 DR_CHAIN is then used as an input to vect_permute_store_chain(),
5212 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
5213 next copy.
5214 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5215 OPRNDS are of size 1. */
5216 for (i = 0; i < group_size; i++)
5218 op = oprnds[i];
5219 vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo, &def_stmt,
5220 &def, &dt);
5221 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
5222 dr_chain[i] = vec_oprnd;
5223 oprnds[i] = vec_oprnd;
5225 if (dataref_offset)
5226 dataref_offset
5227 = int_const_binop (PLUS_EXPR, dataref_offset,
5228 TYPE_SIZE_UNIT (aggr_type));
5229 else
5230 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5231 TYPE_SIZE_UNIT (aggr_type));
5234 if (store_lanes_p)
5236 tree vec_array;
5238 /* Combine all the vectors into an array. */
5239 vec_array = create_vector_array (vectype, vec_num);
5240 for (i = 0; i < vec_num; i++)
5242 vec_oprnd = dr_chain[i];
5243 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
5246 /* Emit:
5247 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
5248 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5249 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
5250 gimple_call_set_lhs (new_stmt, data_ref);
5251 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5253 else
5255 new_stmt = NULL;
5256 if (grouped_store)
5258 if (j == 0)
5259 result_chain.create (group_size);
5260 /* Permute. */
5261 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
5262 &result_chain);
5265 next_stmt = first_stmt;
5266 for (i = 0; i < vec_num; i++)
5268 unsigned align, misalign;
5270 if (i > 0)
5271 /* Bump the vector pointer. */
5272 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5273 stmt, NULL_TREE);
5275 if (slp)
5276 vec_oprnd = vec_oprnds[i];
5277 else if (grouped_store)
5278 /* For grouped stores vectorized defs are interleaved in
5279 vect_permute_store_chain(). */
5280 vec_oprnd = result_chain[i];
5282 data_ref = build2 (MEM_REF, TREE_TYPE (vec_oprnd), dataref_ptr,
5283 dataref_offset
5284 ? dataref_offset
5285 : build_int_cst (reference_alias_ptr_type
5286 (DR_REF (first_dr)), 0));
5287 align = TYPE_ALIGN_UNIT (vectype);
5288 if (aligned_access_p (first_dr))
5289 misalign = 0;
5290 else if (DR_MISALIGNMENT (first_dr) == -1)
5292 TREE_TYPE (data_ref)
5293 = build_aligned_type (TREE_TYPE (data_ref),
5294 TYPE_ALIGN (elem_type));
5295 align = TYPE_ALIGN_UNIT (elem_type);
5296 misalign = 0;
5298 else
5300 TREE_TYPE (data_ref)
5301 = build_aligned_type (TREE_TYPE (data_ref),
5302 TYPE_ALIGN (elem_type));
5303 misalign = DR_MISALIGNMENT (first_dr);
5305 if (dataref_offset == NULL_TREE)
5306 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
5307 misalign);
5309 /* Arguments are ready. Create the new vector stmt. */
5310 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
5311 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5313 if (slp)
5314 continue;
5316 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5317 if (!next_stmt)
5318 break;
5321 if (!slp)
5323 if (j == 0)
5324 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5325 else
5326 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5327 prev_stmt_info = vinfo_for_stmt (new_stmt);
5331 dr_chain.release ();
5332 oprnds.release ();
5333 result_chain.release ();
5334 vec_oprnds.release ();
5336 return true;
5339 /* Given a vector type VECTYPE and permutation SEL returns
5340 the VECTOR_CST mask that implements the permutation of the
5341 vector elements. If that is impossible to do, returns NULL. */
5343 tree
5344 vect_gen_perm_mask (tree vectype, unsigned char *sel)
5346 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
5347 int i, nunits;
5349 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5351 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5352 return NULL;
5354 mask_elt_type = lang_hooks.types.type_for_mode
5355 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
5356 mask_type = get_vectype_for_scalar_type (mask_elt_type);
5358 mask_elts = XALLOCAVEC (tree, nunits);
5359 for (i = nunits - 1; i >= 0; i--)
5360 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
5361 mask_vec = build_vector (mask_type, mask_elts);
5363 return mask_vec;
5366 /* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
5367 reversal of the vector elements. If that is impossible to do,
5368 returns NULL. */
5370 static tree
5371 perm_mask_for_reverse (tree vectype)
5373 int i, nunits;
5374 unsigned char *sel;
5376 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5377 sel = XALLOCAVEC (unsigned char, nunits);
5379 for (i = 0; i < nunits; ++i)
5380 sel[i] = nunits - 1 - i;
5382 return vect_gen_perm_mask (vectype, sel);
5385 /* Given a vector variable X and Y, that was generated for the scalar
5386 STMT, generate instructions to permute the vector elements of X and Y
5387 using permutation mask MASK_VEC, insert them at *GSI and return the
5388 permuted vector variable. */
5390 static tree
5391 permute_vec_elements (tree x, tree y, tree mask_vec, gimple stmt,
5392 gimple_stmt_iterator *gsi)
5394 tree vectype = TREE_TYPE (x);
5395 tree perm_dest, data_ref;
5396 gimple perm_stmt;
5398 perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
5399 data_ref = make_ssa_name (perm_dest, NULL);
5401 /* Generate the permute statement. */
5402 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
5403 x, y, mask_vec);
5404 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5406 return data_ref;
5409 /* vectorizable_load.
5411 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
5412 can be vectorized.
5413 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5414 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5415 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5417 static bool
5418 vectorizable_load (gimple stmt, gimple_stmt_iterator *gsi, gimple *vec_stmt,
5419 slp_tree slp_node, slp_instance slp_node_instance)
5421 tree scalar_dest;
5422 tree vec_dest = NULL;
5423 tree data_ref = NULL;
5424 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5425 stmt_vec_info prev_stmt_info;
5426 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5427 struct loop *loop = NULL;
5428 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
5429 bool nested_in_vect_loop = false;
5430 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5431 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5432 tree elem_type;
5433 tree new_temp;
5434 enum machine_mode mode;
5435 gimple new_stmt = NULL;
5436 tree dummy;
5437 enum dr_alignment_support alignment_support_scheme;
5438 tree dataref_ptr = NULL_TREE;
5439 tree dataref_offset = NULL_TREE;
5440 gimple ptr_incr = NULL;
5441 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5442 int ncopies;
5443 int i, j, group_size, group_gap;
5444 tree msq = NULL_TREE, lsq;
5445 tree offset = NULL_TREE;
5446 tree realignment_token = NULL_TREE;
5447 gimple phi = NULL;
5448 vec<tree> dr_chain = vNULL;
5449 bool grouped_load = false;
5450 bool load_lanes_p = false;
5451 gimple first_stmt;
5452 bool inv_p;
5453 bool negative = false;
5454 bool compute_in_loop = false;
5455 struct loop *at_loop;
5456 int vec_num;
5457 bool slp = (slp_node != NULL);
5458 bool slp_perm = false;
5459 enum tree_code code;
5460 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5461 int vf;
5462 tree aggr_type;
5463 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
5464 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
5465 int gather_scale = 1;
5466 enum vect_def_type gather_dt = vect_unknown_def_type;
5468 if (loop_vinfo)
5470 loop = LOOP_VINFO_LOOP (loop_vinfo);
5471 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
5472 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5474 else
5475 vf = 1;
5477 /* Multiple types in SLP are handled by creating the appropriate number of
5478 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5479 case of SLP. */
5480 if (slp || PURE_SLP_STMT (stmt_info))
5481 ncopies = 1;
5482 else
5483 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5485 gcc_assert (ncopies >= 1);
5487 /* FORNOW. This restriction should be relaxed. */
5488 if (nested_in_vect_loop && ncopies > 1)
5490 if (dump_enabled_p ())
5491 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5492 "multiple types in nested loop.\n");
5493 return false;
5496 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5497 return false;
5499 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
5500 return false;
5502 /* Is vectorizable load? */
5503 if (!is_gimple_assign (stmt))
5504 return false;
5506 scalar_dest = gimple_assign_lhs (stmt);
5507 if (TREE_CODE (scalar_dest) != SSA_NAME)
5508 return false;
5510 code = gimple_assign_rhs_code (stmt);
5511 if (code != ARRAY_REF
5512 && code != BIT_FIELD_REF
5513 && code != INDIRECT_REF
5514 && code != COMPONENT_REF
5515 && code != IMAGPART_EXPR
5516 && code != REALPART_EXPR
5517 && code != MEM_REF
5518 && TREE_CODE_CLASS (code) != tcc_declaration)
5519 return false;
5521 if (!STMT_VINFO_DATA_REF (stmt_info))
5522 return false;
5524 elem_type = TREE_TYPE (vectype);
5525 mode = TYPE_MODE (vectype);
5527 /* FORNOW. In some cases can vectorize even if data-type not supported
5528 (e.g. - data copies). */
5529 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
5531 if (dump_enabled_p ())
5532 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5533 "Aligned load, but unsupported type.\n");
5534 return false;
5537 /* Check if the load is a part of an interleaving chain. */
5538 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5540 grouped_load = true;
5541 /* FORNOW */
5542 gcc_assert (! nested_in_vect_loop && !STMT_VINFO_GATHER_P (stmt_info));
5544 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5545 if (!slp && !PURE_SLP_STMT (stmt_info))
5547 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5548 if (vect_load_lanes_supported (vectype, group_size))
5549 load_lanes_p = true;
5550 else if (!vect_grouped_load_supported (vectype, group_size))
5551 return false;
5556 if (STMT_VINFO_GATHER_P (stmt_info))
5558 gimple def_stmt;
5559 tree def;
5560 gather_decl = vect_check_gather (stmt, loop_vinfo, &gather_base,
5561 &gather_off, &gather_scale);
5562 gcc_assert (gather_decl);
5563 if (!vect_is_simple_use_1 (gather_off, NULL, loop_vinfo, bb_vinfo,
5564 &def_stmt, &def, &gather_dt,
5565 &gather_off_vectype))
5567 if (dump_enabled_p ())
5568 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5569 "gather index use not simple.\n");
5570 return false;
5573 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
5575 else
5577 negative = tree_int_cst_compare (nested_in_vect_loop
5578 ? STMT_VINFO_DR_STEP (stmt_info)
5579 : DR_STEP (dr),
5580 size_zero_node) < 0;
5581 if (negative && ncopies > 1)
5583 if (dump_enabled_p ())
5584 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5585 "multiple types with negative step.\n");
5586 return false;
5589 if (negative)
5591 if (grouped_load)
5593 if (dump_enabled_p ())
5594 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5595 "negative step for group load not supported"
5596 "\n");
5597 return false;
5599 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5600 if (alignment_support_scheme != dr_aligned
5601 && alignment_support_scheme != dr_unaligned_supported)
5603 if (dump_enabled_p ())
5604 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5605 "negative step but alignment required.\n");
5606 return false;
5608 if (!perm_mask_for_reverse (vectype))
5610 if (dump_enabled_p ())
5611 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5612 "negative step and reversing not supported."
5613 "\n");
5614 return false;
5619 if (!vec_stmt) /* transformation not required. */
5621 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
5622 vect_model_load_cost (stmt_info, ncopies, load_lanes_p, NULL, NULL, NULL);
5623 return true;
5626 if (dump_enabled_p ())
5627 dump_printf_loc (MSG_NOTE, vect_location,
5628 "transform load. ncopies = %d\n", ncopies);
5630 /** Transform. **/
5632 ensure_base_align (stmt_info, dr);
5634 if (STMT_VINFO_GATHER_P (stmt_info))
5636 tree vec_oprnd0 = NULL_TREE, op;
5637 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
5638 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5639 tree ptr, mask, var, scale, perm_mask = NULL_TREE, prev_res = NULL_TREE;
5640 edge pe = loop_preheader_edge (loop);
5641 gimple_seq seq;
5642 basic_block new_bb;
5643 enum { NARROW, NONE, WIDEN } modifier;
5644 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
5646 if (nunits == gather_off_nunits)
5647 modifier = NONE;
5648 else if (nunits == gather_off_nunits / 2)
5650 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
5651 modifier = WIDEN;
5653 for (i = 0; i < gather_off_nunits; ++i)
5654 sel[i] = i | nunits;
5656 perm_mask = vect_gen_perm_mask (gather_off_vectype, sel);
5657 gcc_assert (perm_mask != NULL_TREE);
5659 else if (nunits == gather_off_nunits * 2)
5661 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5662 modifier = NARROW;
5664 for (i = 0; i < nunits; ++i)
5665 sel[i] = i < gather_off_nunits
5666 ? i : i + nunits - gather_off_nunits;
5668 perm_mask = vect_gen_perm_mask (vectype, sel);
5669 gcc_assert (perm_mask != NULL_TREE);
5670 ncopies *= 2;
5672 else
5673 gcc_unreachable ();
5675 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
5676 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5677 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5678 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5679 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5680 scaletype = TREE_VALUE (arglist);
5681 gcc_checking_assert (types_compatible_p (srctype, rettype)
5682 && types_compatible_p (srctype, masktype));
5684 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5686 ptr = fold_convert (ptrtype, gather_base);
5687 if (!is_gimple_min_invariant (ptr))
5689 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5690 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5691 gcc_assert (!new_bb);
5694 /* Currently we support only unconditional gather loads,
5695 so mask should be all ones. */
5696 if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
5697 mask = build_int_cst (TREE_TYPE (masktype), -1);
5698 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
5700 REAL_VALUE_TYPE r;
5701 long tmp[6];
5702 for (j = 0; j < 6; ++j)
5703 tmp[j] = -1;
5704 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
5705 mask = build_real (TREE_TYPE (masktype), r);
5707 else
5708 gcc_unreachable ();
5709 mask = build_vector_from_val (masktype, mask);
5710 mask = vect_init_vector (stmt, mask, masktype, NULL);
5712 scale = build_int_cst (scaletype, gather_scale);
5714 prev_stmt_info = NULL;
5715 for (j = 0; j < ncopies; ++j)
5717 if (modifier == WIDEN && (j & 1))
5718 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
5719 perm_mask, stmt, gsi);
5720 else if (j == 0)
5721 op = vec_oprnd0
5722 = vect_get_vec_def_for_operand (gather_off, stmt, NULL);
5723 else
5724 op = vec_oprnd0
5725 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
5727 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
5729 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
5730 == TYPE_VECTOR_SUBPARTS (idxtype));
5731 var = vect_get_new_vect_var (idxtype, vect_simple_var, NULL);
5732 var = make_ssa_name (var, NULL);
5733 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
5734 new_stmt
5735 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var,
5736 op, NULL_TREE);
5737 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5738 op = var;
5741 new_stmt
5742 = gimple_build_call (gather_decl, 5, mask, ptr, op, mask, scale);
5744 if (!useless_type_conversion_p (vectype, rettype))
5746 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
5747 == TYPE_VECTOR_SUBPARTS (rettype));
5748 var = vect_get_new_vect_var (rettype, vect_simple_var, NULL);
5749 op = make_ssa_name (var, new_stmt);
5750 gimple_call_set_lhs (new_stmt, op);
5751 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5752 var = make_ssa_name (vec_dest, NULL);
5753 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
5754 new_stmt
5755 = gimple_build_assign_with_ops (VIEW_CONVERT_EXPR, var, op,
5756 NULL_TREE);
5758 else
5760 var = make_ssa_name (vec_dest, new_stmt);
5761 gimple_call_set_lhs (new_stmt, var);
5764 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5766 if (modifier == NARROW)
5768 if ((j & 1) == 0)
5770 prev_res = var;
5771 continue;
5773 var = permute_vec_elements (prev_res, var,
5774 perm_mask, stmt, gsi);
5775 new_stmt = SSA_NAME_DEF_STMT (var);
5778 if (prev_stmt_info == NULL)
5779 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5780 else
5781 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5782 prev_stmt_info = vinfo_for_stmt (new_stmt);
5784 return true;
5786 else if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
5788 gimple_stmt_iterator incr_gsi;
5789 bool insert_after;
5790 gimple incr;
5791 tree offvar;
5792 tree ivstep;
5793 tree running_off;
5794 vec<constructor_elt, va_gc> *v = NULL;
5795 gimple_seq stmts = NULL;
5796 tree stride_base, stride_step, alias_off;
5798 gcc_assert (!nested_in_vect_loop);
5800 stride_base
5801 = fold_build_pointer_plus
5802 (unshare_expr (DR_BASE_ADDRESS (dr)),
5803 size_binop (PLUS_EXPR,
5804 convert_to_ptrofftype (unshare_expr (DR_OFFSET (dr))),
5805 convert_to_ptrofftype (DR_INIT (dr))));
5806 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (dr)));
5808 /* For a load with loop-invariant (but other than power-of-2)
5809 stride (i.e. not a grouped access) like so:
5811 for (i = 0; i < n; i += stride)
5812 ... = array[i];
5814 we generate a new induction variable and new accesses to
5815 form a new vector (or vectors, depending on ncopies):
5817 for (j = 0; ; j += VF*stride)
5818 tmp1 = array[j];
5819 tmp2 = array[j + stride];
5821 vectemp = {tmp1, tmp2, ...}
5824 ivstep = stride_step;
5825 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
5826 build_int_cst (TREE_TYPE (ivstep), vf));
5828 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5830 create_iv (stride_base, ivstep, NULL,
5831 loop, &incr_gsi, insert_after,
5832 &offvar, NULL);
5833 incr = gsi_stmt (incr_gsi);
5834 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
5836 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
5837 if (stmts)
5838 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5840 prev_stmt_info = NULL;
5841 running_off = offvar;
5842 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0);
5843 for (j = 0; j < ncopies; j++)
5845 tree vec_inv;
5847 vec_alloc (v, nunits);
5848 for (i = 0; i < nunits; i++)
5850 tree newref, newoff;
5851 gimple incr;
5852 newref = build2 (MEM_REF, TREE_TYPE (vectype),
5853 running_off, alias_off);
5855 newref = force_gimple_operand_gsi (gsi, newref, true,
5856 NULL_TREE, true,
5857 GSI_SAME_STMT);
5858 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
5859 newoff = copy_ssa_name (running_off, NULL);
5860 incr = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, newoff,
5861 running_off, stride_step);
5862 vect_finish_stmt_generation (stmt, incr, gsi);
5864 running_off = newoff;
5867 vec_inv = build_constructor (vectype, v);
5868 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
5869 new_stmt = SSA_NAME_DEF_STMT (new_temp);
5871 if (j == 0)
5872 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5873 else
5874 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5875 prev_stmt_info = vinfo_for_stmt (new_stmt);
5877 return true;
5880 if (grouped_load)
5882 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5883 if (slp
5884 && !SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
5885 && first_stmt != SLP_TREE_SCALAR_STMTS (slp_node)[0])
5886 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5888 /* Check if the chain of loads is already vectorized. */
5889 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
5890 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
5891 ??? But we can only do so if there is exactly one
5892 as we have no way to get at the rest. Leave the CSE
5893 opportunity alone.
5894 ??? With the group load eventually participating
5895 in multiple different permutations (having multiple
5896 slp nodes which refer to the same group) the CSE
5897 is even wrong code. See PR56270. */
5898 && !slp)
5900 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
5901 return true;
5903 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5904 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5906 /* VEC_NUM is the number of vect stmts to be created for this group. */
5907 if (slp)
5909 grouped_load = false;
5910 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5911 if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
5912 slp_perm = true;
5913 group_gap = GROUP_GAP (vinfo_for_stmt (first_stmt));
5915 else
5917 vec_num = group_size;
5918 group_gap = 0;
5921 else
5923 first_stmt = stmt;
5924 first_dr = dr;
5925 group_size = vec_num = 1;
5926 group_gap = 0;
5929 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
5930 gcc_assert (alignment_support_scheme);
5931 /* Targets with load-lane instructions must not require explicit
5932 realignment. */
5933 gcc_assert (!load_lanes_p
5934 || alignment_support_scheme == dr_aligned
5935 || alignment_support_scheme == dr_unaligned_supported);
5937 /* In case the vectorization factor (VF) is bigger than the number
5938 of elements that we can fit in a vectype (nunits), we have to generate
5939 more than one vector stmt - i.e - we need to "unroll" the
5940 vector stmt by a factor VF/nunits. In doing so, we record a pointer
5941 from one copy of the vector stmt to the next, in the field
5942 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
5943 stages to find the correct vector defs to be used when vectorizing
5944 stmts that use the defs of the current stmt. The example below
5945 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
5946 need to create 4 vectorized stmts):
5948 before vectorization:
5949 RELATED_STMT VEC_STMT
5950 S1: x = memref - -
5951 S2: z = x + 1 - -
5953 step 1: vectorize stmt S1:
5954 We first create the vector stmt VS1_0, and, as usual, record a
5955 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
5956 Next, we create the vector stmt VS1_1, and record a pointer to
5957 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
5958 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
5959 stmts and pointers:
5960 RELATED_STMT VEC_STMT
5961 VS1_0: vx0 = memref0 VS1_1 -
5962 VS1_1: vx1 = memref1 VS1_2 -
5963 VS1_2: vx2 = memref2 VS1_3 -
5964 VS1_3: vx3 = memref3 - -
5965 S1: x = load - VS1_0
5966 S2: z = x + 1 - -
5968 See in documentation in vect_get_vec_def_for_stmt_copy for how the
5969 information we recorded in RELATED_STMT field is used to vectorize
5970 stmt S2. */
5972 /* In case of interleaving (non-unit grouped access):
5974 S1: x2 = &base + 2
5975 S2: x0 = &base
5976 S3: x1 = &base + 1
5977 S4: x3 = &base + 3
5979 Vectorized loads are created in the order of memory accesses
5980 starting from the access of the first stmt of the chain:
5982 VS1: vx0 = &base
5983 VS2: vx1 = &base + vec_size*1
5984 VS3: vx3 = &base + vec_size*2
5985 VS4: vx4 = &base + vec_size*3
5987 Then permutation statements are generated:
5989 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
5990 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
5993 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5994 (the order of the data-refs in the output of vect_permute_load_chain
5995 corresponds to the order of scalar stmts in the interleaving chain - see
5996 the documentation of vect_permute_load_chain()).
5997 The generation of permutation stmts and recording them in
5998 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
6000 In case of both multiple types and interleaving, the vector loads and
6001 permutation stmts above are created for every copy. The result vector
6002 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
6003 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
6005 /* If the data reference is aligned (dr_aligned) or potentially unaligned
6006 on a target that supports unaligned accesses (dr_unaligned_supported)
6007 we generate the following code:
6008 p = initial_addr;
6009 indx = 0;
6010 loop {
6011 p = p + indx * vectype_size;
6012 vec_dest = *(p);
6013 indx = indx + 1;
6016 Otherwise, the data reference is potentially unaligned on a target that
6017 does not support unaligned accesses (dr_explicit_realign_optimized) -
6018 then generate the following code, in which the data in each iteration is
6019 obtained by two vector loads, one from the previous iteration, and one
6020 from the current iteration:
6021 p1 = initial_addr;
6022 msq_init = *(floor(p1))
6023 p2 = initial_addr + VS - 1;
6024 realignment_token = call target_builtin;
6025 indx = 0;
6026 loop {
6027 p2 = p2 + indx * vectype_size
6028 lsq = *(floor(p2))
6029 vec_dest = realign_load (msq, lsq, realignment_token)
6030 indx = indx + 1;
6031 msq = lsq;
6032 } */
6034 /* If the misalignment remains the same throughout the execution of the
6035 loop, we can create the init_addr and permutation mask at the loop
6036 preheader. Otherwise, it needs to be created inside the loop.
6037 This can only occur when vectorizing memory accesses in the inner-loop
6038 nested within an outer-loop that is being vectorized. */
6040 if (nested_in_vect_loop
6041 && (TREE_INT_CST_LOW (DR_STEP (dr))
6042 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
6044 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
6045 compute_in_loop = true;
6048 if ((alignment_support_scheme == dr_explicit_realign_optimized
6049 || alignment_support_scheme == dr_explicit_realign)
6050 && !compute_in_loop)
6052 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
6053 alignment_support_scheme, NULL_TREE,
6054 &at_loop);
6055 if (alignment_support_scheme == dr_explicit_realign_optimized)
6057 phi = SSA_NAME_DEF_STMT (msq);
6058 offset = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
6061 else
6062 at_loop = loop;
6064 if (negative)
6065 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
6067 if (load_lanes_p)
6068 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
6069 else
6070 aggr_type = vectype;
6072 prev_stmt_info = NULL;
6073 for (j = 0; j < ncopies; j++)
6075 /* 1. Create the vector or array pointer update chain. */
6076 if (j == 0)
6078 bool simd_lane_access_p
6079 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
6080 if (simd_lane_access_p
6081 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
6082 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
6083 && integer_zerop (DR_OFFSET (first_dr))
6084 && integer_zerop (DR_INIT (first_dr))
6085 && alias_sets_conflict_p (get_alias_set (aggr_type),
6086 get_alias_set (DR_REF (first_dr)))
6087 && (alignment_support_scheme == dr_aligned
6088 || alignment_support_scheme == dr_unaligned_supported))
6090 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
6091 dataref_offset = build_int_cst (reference_alias_ptr_type
6092 (DR_REF (first_dr)), 0);
6093 inv_p = false;
6095 else
6096 dataref_ptr
6097 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
6098 offset, &dummy, gsi, &ptr_incr,
6099 simd_lane_access_p, &inv_p);
6101 else if (dataref_offset)
6102 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
6103 TYPE_SIZE_UNIT (aggr_type));
6104 else
6105 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
6106 TYPE_SIZE_UNIT (aggr_type));
6108 if (grouped_load || slp_perm)
6109 dr_chain.create (vec_num);
6111 if (load_lanes_p)
6113 tree vec_array;
6115 vec_array = create_vector_array (vectype, vec_num);
6117 /* Emit:
6118 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
6119 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
6120 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
6121 gimple_call_set_lhs (new_stmt, vec_array);
6122 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6124 /* Extract each vector into an SSA_NAME. */
6125 for (i = 0; i < vec_num; i++)
6127 new_temp = read_vector_array (stmt, gsi, scalar_dest,
6128 vec_array, i);
6129 dr_chain.quick_push (new_temp);
6132 /* Record the mapping between SSA_NAMEs and statements. */
6133 vect_record_grouped_load_vectors (stmt, dr_chain);
6135 else
6137 for (i = 0; i < vec_num; i++)
6139 if (i > 0)
6140 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6141 stmt, NULL_TREE);
6143 /* 2. Create the vector-load in the loop. */
6144 switch (alignment_support_scheme)
6146 case dr_aligned:
6147 case dr_unaligned_supported:
6149 unsigned int align, misalign;
6151 data_ref
6152 = build2 (MEM_REF, vectype, dataref_ptr,
6153 dataref_offset
6154 ? dataref_offset
6155 : build_int_cst (reference_alias_ptr_type
6156 (DR_REF (first_dr)), 0));
6157 align = TYPE_ALIGN_UNIT (vectype);
6158 if (alignment_support_scheme == dr_aligned)
6160 gcc_assert (aligned_access_p (first_dr));
6161 misalign = 0;
6163 else if (DR_MISALIGNMENT (first_dr) == -1)
6165 TREE_TYPE (data_ref)
6166 = build_aligned_type (TREE_TYPE (data_ref),
6167 TYPE_ALIGN (elem_type));
6168 align = TYPE_ALIGN_UNIT (elem_type);
6169 misalign = 0;
6171 else
6173 TREE_TYPE (data_ref)
6174 = build_aligned_type (TREE_TYPE (data_ref),
6175 TYPE_ALIGN (elem_type));
6176 misalign = DR_MISALIGNMENT (first_dr);
6178 if (dataref_offset == NULL_TREE)
6179 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
6180 align, misalign);
6181 break;
6183 case dr_explicit_realign:
6185 tree ptr, bump;
6186 tree vs_minus_1;
6188 vs_minus_1 = size_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
6190 if (compute_in_loop)
6191 msq = vect_setup_realignment (first_stmt, gsi,
6192 &realignment_token,
6193 dr_explicit_realign,
6194 dataref_ptr, NULL);
6196 ptr = copy_ssa_name (dataref_ptr, NULL);
6197 new_stmt = gimple_build_assign_with_ops
6198 (BIT_AND_EXPR, ptr, dataref_ptr,
6199 build_int_cst
6200 (TREE_TYPE (dataref_ptr),
6201 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6202 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6203 data_ref
6204 = build2 (MEM_REF, vectype, ptr,
6205 build_int_cst (reference_alias_ptr_type
6206 (DR_REF (first_dr)), 0));
6207 vec_dest = vect_create_destination_var (scalar_dest,
6208 vectype);
6209 new_stmt = gimple_build_assign (vec_dest, data_ref);
6210 new_temp = make_ssa_name (vec_dest, new_stmt);
6211 gimple_assign_set_lhs (new_stmt, new_temp);
6212 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
6213 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
6214 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6215 msq = new_temp;
6217 bump = size_binop (MULT_EXPR, vs_minus_1,
6218 TYPE_SIZE_UNIT (elem_type));
6219 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
6220 new_stmt = gimple_build_assign_with_ops
6221 (BIT_AND_EXPR, NULL_TREE, ptr,
6222 build_int_cst
6223 (TREE_TYPE (ptr),
6224 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6225 ptr = copy_ssa_name (dataref_ptr, new_stmt);
6226 gimple_assign_set_lhs (new_stmt, ptr);
6227 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6228 data_ref
6229 = build2 (MEM_REF, vectype, ptr,
6230 build_int_cst (reference_alias_ptr_type
6231 (DR_REF (first_dr)), 0));
6232 break;
6234 case dr_explicit_realign_optimized:
6235 new_temp = copy_ssa_name (dataref_ptr, NULL);
6236 new_stmt = gimple_build_assign_with_ops
6237 (BIT_AND_EXPR, new_temp, dataref_ptr,
6238 build_int_cst
6239 (TREE_TYPE (dataref_ptr),
6240 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6241 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6242 data_ref
6243 = build2 (MEM_REF, vectype, new_temp,
6244 build_int_cst (reference_alias_ptr_type
6245 (DR_REF (first_dr)), 0));
6246 break;
6247 default:
6248 gcc_unreachable ();
6250 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6251 new_stmt = gimple_build_assign (vec_dest, data_ref);
6252 new_temp = make_ssa_name (vec_dest, new_stmt);
6253 gimple_assign_set_lhs (new_stmt, new_temp);
6254 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6256 /* 3. Handle explicit realignment if necessary/supported.
6257 Create in loop:
6258 vec_dest = realign_load (msq, lsq, realignment_token) */
6259 if (alignment_support_scheme == dr_explicit_realign_optimized
6260 || alignment_support_scheme == dr_explicit_realign)
6262 lsq = gimple_assign_lhs (new_stmt);
6263 if (!realignment_token)
6264 realignment_token = dataref_ptr;
6265 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6266 new_stmt
6267 = gimple_build_assign_with_ops (REALIGN_LOAD_EXPR,
6268 vec_dest, msq, lsq,
6269 realignment_token);
6270 new_temp = make_ssa_name (vec_dest, new_stmt);
6271 gimple_assign_set_lhs (new_stmt, new_temp);
6272 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6274 if (alignment_support_scheme == dr_explicit_realign_optimized)
6276 gcc_assert (phi);
6277 if (i == vec_num - 1 && j == ncopies - 1)
6278 add_phi_arg (phi, lsq,
6279 loop_latch_edge (containing_loop),
6280 UNKNOWN_LOCATION);
6281 msq = lsq;
6285 /* 4. Handle invariant-load. */
6286 if (inv_p && !bb_vinfo)
6288 gimple_stmt_iterator gsi2 = *gsi;
6289 gcc_assert (!grouped_load);
6290 gsi_next (&gsi2);
6291 new_temp = vect_init_vector (stmt, scalar_dest,
6292 vectype, &gsi2);
6293 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6296 if (negative)
6298 tree perm_mask = perm_mask_for_reverse (vectype);
6299 new_temp = permute_vec_elements (new_temp, new_temp,
6300 perm_mask, stmt, gsi);
6301 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6304 /* Collect vector loads and later create their permutation in
6305 vect_transform_grouped_load (). */
6306 if (grouped_load || slp_perm)
6307 dr_chain.quick_push (new_temp);
6309 /* Store vector loads in the corresponding SLP_NODE. */
6310 if (slp && !slp_perm)
6311 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6313 /* Bump the vector pointer to account for a gap. */
6314 if (slp && group_gap != 0)
6316 tree bump = size_binop (MULT_EXPR,
6317 TYPE_SIZE_UNIT (elem_type),
6318 size_int (group_gap));
6319 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6320 stmt, bump);
6324 if (slp && !slp_perm)
6325 continue;
6327 if (slp_perm)
6329 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
6330 slp_node_instance, false))
6332 dr_chain.release ();
6333 return false;
6336 else
6338 if (grouped_load)
6340 if (!load_lanes_p)
6341 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
6342 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6344 else
6346 if (j == 0)
6347 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6348 else
6349 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6350 prev_stmt_info = vinfo_for_stmt (new_stmt);
6353 dr_chain.release ();
6356 return true;
6359 /* Function vect_is_simple_cond.
6361 Input:
6362 LOOP - the loop that is being vectorized.
6363 COND - Condition that is checked for simple use.
6365 Output:
6366 *COMP_VECTYPE - the vector type for the comparison.
6368 Returns whether a COND can be vectorized. Checks whether
6369 condition operands are supportable using vec_is_simple_use. */
6371 static bool
6372 vect_is_simple_cond (tree cond, gimple stmt, loop_vec_info loop_vinfo,
6373 bb_vec_info bb_vinfo, tree *comp_vectype)
6375 tree lhs, rhs;
6376 tree def;
6377 enum vect_def_type dt;
6378 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
6380 if (!COMPARISON_CLASS_P (cond))
6381 return false;
6383 lhs = TREE_OPERAND (cond, 0);
6384 rhs = TREE_OPERAND (cond, 1);
6386 if (TREE_CODE (lhs) == SSA_NAME)
6388 gimple lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
6389 if (!vect_is_simple_use_1 (lhs, stmt, loop_vinfo, bb_vinfo,
6390 &lhs_def_stmt, &def, &dt, &vectype1))
6391 return false;
6393 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
6394 && TREE_CODE (lhs) != FIXED_CST)
6395 return false;
6397 if (TREE_CODE (rhs) == SSA_NAME)
6399 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
6400 if (!vect_is_simple_use_1 (rhs, stmt, loop_vinfo, bb_vinfo,
6401 &rhs_def_stmt, &def, &dt, &vectype2))
6402 return false;
6404 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
6405 && TREE_CODE (rhs) != FIXED_CST)
6406 return false;
6408 *comp_vectype = vectype1 ? vectype1 : vectype2;
6409 return true;
6412 /* vectorizable_condition.
6414 Check if STMT is conditional modify expression that can be vectorized.
6415 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
6416 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
6417 at GSI.
6419 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
6420 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
6421 else caluse if it is 2).
6423 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6425 bool
6426 vectorizable_condition (gimple stmt, gimple_stmt_iterator *gsi,
6427 gimple *vec_stmt, tree reduc_def, int reduc_index,
6428 slp_tree slp_node)
6430 tree scalar_dest = NULL_TREE;
6431 tree vec_dest = NULL_TREE;
6432 tree cond_expr, then_clause, else_clause;
6433 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6434 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6435 tree comp_vectype = NULL_TREE;
6436 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
6437 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
6438 tree vec_compare, vec_cond_expr;
6439 tree new_temp;
6440 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6441 tree def;
6442 enum vect_def_type dt, dts[4];
6443 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6444 int ncopies;
6445 enum tree_code code;
6446 stmt_vec_info prev_stmt_info = NULL;
6447 int i, j;
6448 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6449 vec<tree> vec_oprnds0 = vNULL;
6450 vec<tree> vec_oprnds1 = vNULL;
6451 vec<tree> vec_oprnds2 = vNULL;
6452 vec<tree> vec_oprnds3 = vNULL;
6453 tree vec_cmp_type;
6455 if (slp_node || PURE_SLP_STMT (stmt_info))
6456 ncopies = 1;
6457 else
6458 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6460 gcc_assert (ncopies >= 1);
6461 if (reduc_index && ncopies > 1)
6462 return false; /* FORNOW */
6464 if (reduc_index && STMT_SLP_TYPE (stmt_info))
6465 return false;
6467 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6468 return false;
6470 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6471 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
6472 && reduc_def))
6473 return false;
6475 /* FORNOW: not yet supported. */
6476 if (STMT_VINFO_LIVE_P (stmt_info))
6478 if (dump_enabled_p ())
6479 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6480 "value used after loop.\n");
6481 return false;
6484 /* Is vectorizable conditional operation? */
6485 if (!is_gimple_assign (stmt))
6486 return false;
6488 code = gimple_assign_rhs_code (stmt);
6490 if (code != COND_EXPR)
6491 return false;
6493 cond_expr = gimple_assign_rhs1 (stmt);
6494 then_clause = gimple_assign_rhs2 (stmt);
6495 else_clause = gimple_assign_rhs3 (stmt);
6497 if (!vect_is_simple_cond (cond_expr, stmt, loop_vinfo, bb_vinfo,
6498 &comp_vectype)
6499 || !comp_vectype)
6500 return false;
6502 if (TREE_CODE (then_clause) == SSA_NAME)
6504 gimple then_def_stmt = SSA_NAME_DEF_STMT (then_clause);
6505 if (!vect_is_simple_use (then_clause, stmt, loop_vinfo, bb_vinfo,
6506 &then_def_stmt, &def, &dt))
6507 return false;
6509 else if (TREE_CODE (then_clause) != INTEGER_CST
6510 && TREE_CODE (then_clause) != REAL_CST
6511 && TREE_CODE (then_clause) != FIXED_CST)
6512 return false;
6514 if (TREE_CODE (else_clause) == SSA_NAME)
6516 gimple else_def_stmt = SSA_NAME_DEF_STMT (else_clause);
6517 if (!vect_is_simple_use (else_clause, stmt, loop_vinfo, bb_vinfo,
6518 &else_def_stmt, &def, &dt))
6519 return false;
6521 else if (TREE_CODE (else_clause) != INTEGER_CST
6522 && TREE_CODE (else_clause) != REAL_CST
6523 && TREE_CODE (else_clause) != FIXED_CST)
6524 return false;
6526 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (vectype)));
6527 /* The result of a vector comparison should be signed type. */
6528 tree cmp_type = build_nonstandard_integer_type (prec, 0);
6529 vec_cmp_type = get_same_sized_vectype (cmp_type, vectype);
6530 if (vec_cmp_type == NULL_TREE)
6531 return false;
6533 if (!vec_stmt)
6535 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
6536 return expand_vec_cond_expr_p (vectype, comp_vectype);
6539 /* Transform. */
6541 if (!slp_node)
6543 vec_oprnds0.create (1);
6544 vec_oprnds1.create (1);
6545 vec_oprnds2.create (1);
6546 vec_oprnds3.create (1);
6549 /* Handle def. */
6550 scalar_dest = gimple_assign_lhs (stmt);
6551 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6553 /* Handle cond expr. */
6554 for (j = 0; j < ncopies; j++)
6556 gimple new_stmt = NULL;
6557 if (j == 0)
6559 if (slp_node)
6561 stack_vec<tree, 4> ops;
6562 stack_vec<vec<tree>, 4> vec_defs;
6564 ops.safe_push (TREE_OPERAND (cond_expr, 0));
6565 ops.safe_push (TREE_OPERAND (cond_expr, 1));
6566 ops.safe_push (then_clause);
6567 ops.safe_push (else_clause);
6568 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
6569 vec_oprnds3 = vec_defs.pop ();
6570 vec_oprnds2 = vec_defs.pop ();
6571 vec_oprnds1 = vec_defs.pop ();
6572 vec_oprnds0 = vec_defs.pop ();
6574 ops.release ();
6575 vec_defs.release ();
6577 else
6579 gimple gtemp;
6580 vec_cond_lhs =
6581 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
6582 stmt, NULL);
6583 vect_is_simple_use (TREE_OPERAND (cond_expr, 0), stmt,
6584 loop_vinfo, NULL, &gtemp, &def, &dts[0]);
6586 vec_cond_rhs =
6587 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
6588 stmt, NULL);
6589 vect_is_simple_use (TREE_OPERAND (cond_expr, 1), stmt,
6590 loop_vinfo, NULL, &gtemp, &def, &dts[1]);
6591 if (reduc_index == 1)
6592 vec_then_clause = reduc_def;
6593 else
6595 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
6596 stmt, NULL);
6597 vect_is_simple_use (then_clause, stmt, loop_vinfo,
6598 NULL, &gtemp, &def, &dts[2]);
6600 if (reduc_index == 2)
6601 vec_else_clause = reduc_def;
6602 else
6604 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
6605 stmt, NULL);
6606 vect_is_simple_use (else_clause, stmt, loop_vinfo,
6607 NULL, &gtemp, &def, &dts[3]);
6611 else
6613 vec_cond_lhs = vect_get_vec_def_for_stmt_copy (dts[0],
6614 vec_oprnds0.pop ());
6615 vec_cond_rhs = vect_get_vec_def_for_stmt_copy (dts[1],
6616 vec_oprnds1.pop ());
6617 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
6618 vec_oprnds2.pop ());
6619 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
6620 vec_oprnds3.pop ());
6623 if (!slp_node)
6625 vec_oprnds0.quick_push (vec_cond_lhs);
6626 vec_oprnds1.quick_push (vec_cond_rhs);
6627 vec_oprnds2.quick_push (vec_then_clause);
6628 vec_oprnds3.quick_push (vec_else_clause);
6631 /* Arguments are ready. Create the new vector stmt. */
6632 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
6634 vec_cond_rhs = vec_oprnds1[i];
6635 vec_then_clause = vec_oprnds2[i];
6636 vec_else_clause = vec_oprnds3[i];
6638 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
6639 vec_cond_lhs, vec_cond_rhs);
6640 vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
6641 vec_compare, vec_then_clause, vec_else_clause);
6643 new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
6644 new_temp = make_ssa_name (vec_dest, new_stmt);
6645 gimple_assign_set_lhs (new_stmt, new_temp);
6646 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6647 if (slp_node)
6648 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6651 if (slp_node)
6652 continue;
6654 if (j == 0)
6655 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6656 else
6657 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6659 prev_stmt_info = vinfo_for_stmt (new_stmt);
6662 vec_oprnds0.release ();
6663 vec_oprnds1.release ();
6664 vec_oprnds2.release ();
6665 vec_oprnds3.release ();
6667 return true;
6671 /* Make sure the statement is vectorizable. */
6673 bool
6674 vect_analyze_stmt (gimple stmt, bool *need_to_vectorize, slp_tree node)
6676 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6677 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6678 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
6679 bool ok;
6680 tree scalar_type, vectype;
6681 gimple pattern_stmt;
6682 gimple_seq pattern_def_seq;
6684 if (dump_enabled_p ())
6686 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
6687 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6688 dump_printf (MSG_NOTE, "\n");
6691 if (gimple_has_volatile_ops (stmt))
6693 if (dump_enabled_p ())
6694 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6695 "not vectorized: stmt has volatile operands\n");
6697 return false;
6700 /* Skip stmts that do not need to be vectorized. In loops this is expected
6701 to include:
6702 - the COND_EXPR which is the loop exit condition
6703 - any LABEL_EXPRs in the loop
6704 - computations that are used only for array indexing or loop control.
6705 In basic blocks we only analyze statements that are a part of some SLP
6706 instance, therefore, all the statements are relevant.
6708 Pattern statement needs to be analyzed instead of the original statement
6709 if the original statement is not relevant. Otherwise, we analyze both
6710 statements. In basic blocks we are called from some SLP instance
6711 traversal, don't analyze pattern stmts instead, the pattern stmts
6712 already will be part of SLP instance. */
6714 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
6715 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6716 && !STMT_VINFO_LIVE_P (stmt_info))
6718 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6719 && pattern_stmt
6720 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6721 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6723 /* Analyze PATTERN_STMT instead of the original stmt. */
6724 stmt = pattern_stmt;
6725 stmt_info = vinfo_for_stmt (pattern_stmt);
6726 if (dump_enabled_p ())
6728 dump_printf_loc (MSG_NOTE, vect_location,
6729 "==> examining pattern statement: ");
6730 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6731 dump_printf (MSG_NOTE, "\n");
6734 else
6736 if (dump_enabled_p ())
6737 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
6739 return true;
6742 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6743 && node == NULL
6744 && pattern_stmt
6745 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6746 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6748 /* Analyze PATTERN_STMT too. */
6749 if (dump_enabled_p ())
6751 dump_printf_loc (MSG_NOTE, vect_location,
6752 "==> examining pattern statement: ");
6753 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6754 dump_printf (MSG_NOTE, "\n");
6757 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
6758 return false;
6761 if (is_pattern_stmt_p (stmt_info)
6762 && node == NULL
6763 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
6765 gimple_stmt_iterator si;
6767 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
6769 gimple pattern_def_stmt = gsi_stmt (si);
6770 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
6771 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
6773 /* Analyze def stmt of STMT if it's a pattern stmt. */
6774 if (dump_enabled_p ())
6776 dump_printf_loc (MSG_NOTE, vect_location,
6777 "==> examining pattern def statement: ");
6778 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
6779 dump_printf (MSG_NOTE, "\n");
6782 if (!vect_analyze_stmt (pattern_def_stmt,
6783 need_to_vectorize, node))
6784 return false;
6789 switch (STMT_VINFO_DEF_TYPE (stmt_info))
6791 case vect_internal_def:
6792 break;
6794 case vect_reduction_def:
6795 case vect_nested_cycle:
6796 gcc_assert (!bb_vinfo && (relevance == vect_used_in_outer
6797 || relevance == vect_used_in_outer_by_reduction
6798 || relevance == vect_unused_in_scope));
6799 break;
6801 case vect_induction_def:
6802 case vect_constant_def:
6803 case vect_external_def:
6804 case vect_unknown_def_type:
6805 default:
6806 gcc_unreachable ();
6809 if (bb_vinfo)
6811 gcc_assert (PURE_SLP_STMT (stmt_info));
6813 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
6814 if (dump_enabled_p ())
6816 dump_printf_loc (MSG_NOTE, vect_location,
6817 "get vectype for scalar type: ");
6818 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
6819 dump_printf (MSG_NOTE, "\n");
6822 vectype = get_vectype_for_scalar_type (scalar_type);
6823 if (!vectype)
6825 if (dump_enabled_p ())
6827 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6828 "not SLPed: unsupported data-type ");
6829 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
6830 scalar_type);
6831 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6833 return false;
6836 if (dump_enabled_p ())
6838 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
6839 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
6840 dump_printf (MSG_NOTE, "\n");
6843 STMT_VINFO_VECTYPE (stmt_info) = vectype;
6846 if (STMT_VINFO_RELEVANT_P (stmt_info))
6848 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
6849 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
6850 || (is_gimple_call (stmt)
6851 && gimple_call_lhs (stmt) == NULL_TREE));
6852 *need_to_vectorize = true;
6855 ok = true;
6856 if (!bb_vinfo
6857 && (STMT_VINFO_RELEVANT_P (stmt_info)
6858 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
6859 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, NULL)
6860 || vectorizable_conversion (stmt, NULL, NULL, NULL)
6861 || vectorizable_shift (stmt, NULL, NULL, NULL)
6862 || vectorizable_operation (stmt, NULL, NULL, NULL)
6863 || vectorizable_assignment (stmt, NULL, NULL, NULL)
6864 || vectorizable_load (stmt, NULL, NULL, NULL, NULL)
6865 || vectorizable_call (stmt, NULL, NULL, NULL)
6866 || vectorizable_store (stmt, NULL, NULL, NULL)
6867 || vectorizable_reduction (stmt, NULL, NULL, NULL)
6868 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, NULL));
6869 else
6871 if (bb_vinfo)
6872 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
6873 || vectorizable_conversion (stmt, NULL, NULL, node)
6874 || vectorizable_shift (stmt, NULL, NULL, node)
6875 || vectorizable_operation (stmt, NULL, NULL, node)
6876 || vectorizable_assignment (stmt, NULL, NULL, node)
6877 || vectorizable_load (stmt, NULL, NULL, node, NULL)
6878 || vectorizable_call (stmt, NULL, NULL, node)
6879 || vectorizable_store (stmt, NULL, NULL, node)
6880 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node));
6883 if (!ok)
6885 if (dump_enabled_p ())
6887 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6888 "not vectorized: relevant stmt not ");
6889 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
6890 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
6891 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6894 return false;
6897 if (bb_vinfo)
6898 return true;
6900 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
6901 need extra handling, except for vectorizable reductions. */
6902 if (STMT_VINFO_LIVE_P (stmt_info)
6903 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
6904 ok = vectorizable_live_operation (stmt, NULL, NULL);
6906 if (!ok)
6908 if (dump_enabled_p ())
6910 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6911 "not vectorized: live stmt not ");
6912 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
6913 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
6914 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
6917 return false;
6920 return true;
6924 /* Function vect_transform_stmt.
6926 Create a vectorized stmt to replace STMT, and insert it at BSI. */
6928 bool
6929 vect_transform_stmt (gimple stmt, gimple_stmt_iterator *gsi,
6930 bool *grouped_store, slp_tree slp_node,
6931 slp_instance slp_node_instance)
6933 bool is_store = false;
6934 gimple vec_stmt = NULL;
6935 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6936 bool done;
6938 switch (STMT_VINFO_TYPE (stmt_info))
6940 case type_demotion_vec_info_type:
6941 case type_promotion_vec_info_type:
6942 case type_conversion_vec_info_type:
6943 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
6944 gcc_assert (done);
6945 break;
6947 case induc_vec_info_type:
6948 gcc_assert (!slp_node);
6949 done = vectorizable_induction (stmt, gsi, &vec_stmt);
6950 gcc_assert (done);
6951 break;
6953 case shift_vec_info_type:
6954 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
6955 gcc_assert (done);
6956 break;
6958 case op_vec_info_type:
6959 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
6960 gcc_assert (done);
6961 break;
6963 case assignment_vec_info_type:
6964 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
6965 gcc_assert (done);
6966 break;
6968 case load_vec_info_type:
6969 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
6970 slp_node_instance);
6971 gcc_assert (done);
6972 break;
6974 case store_vec_info_type:
6975 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
6976 gcc_assert (done);
6977 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
6979 /* In case of interleaving, the whole chain is vectorized when the
6980 last store in the chain is reached. Store stmts before the last
6981 one are skipped, and there vec_stmt_info shouldn't be freed
6982 meanwhile. */
6983 *grouped_store = true;
6984 if (STMT_VINFO_VEC_STMT (stmt_info))
6985 is_store = true;
6987 else
6988 is_store = true;
6989 break;
6991 case condition_vec_info_type:
6992 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
6993 gcc_assert (done);
6994 break;
6996 case call_vec_info_type:
6997 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
6998 stmt = gsi_stmt (*gsi);
6999 if (is_gimple_call (stmt)
7000 && gimple_call_internal_p (stmt)
7001 && gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
7002 is_store = true;
7003 break;
7005 case call_simd_clone_vec_info_type:
7006 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
7007 stmt = gsi_stmt (*gsi);
7008 break;
7010 case reduc_vec_info_type:
7011 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
7012 gcc_assert (done);
7013 break;
7015 default:
7016 if (!STMT_VINFO_LIVE_P (stmt_info))
7018 if (dump_enabled_p ())
7019 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7020 "stmt not supported.\n");
7021 gcc_unreachable ();
7025 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
7026 is being vectorized, but outside the immediately enclosing loop. */
7027 if (vec_stmt
7028 && STMT_VINFO_LOOP_VINFO (stmt_info)
7029 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
7030 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
7031 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
7032 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
7033 || STMT_VINFO_RELEVANT (stmt_info) ==
7034 vect_used_in_outer_by_reduction))
7036 struct loop *innerloop = LOOP_VINFO_LOOP (
7037 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
7038 imm_use_iterator imm_iter;
7039 use_operand_p use_p;
7040 tree scalar_dest;
7041 gimple exit_phi;
7043 if (dump_enabled_p ())
7044 dump_printf_loc (MSG_NOTE, vect_location,
7045 "Record the vdef for outer-loop vectorization.\n");
7047 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
7048 (to be used when vectorizing outer-loop stmts that use the DEF of
7049 STMT). */
7050 if (gimple_code (stmt) == GIMPLE_PHI)
7051 scalar_dest = PHI_RESULT (stmt);
7052 else
7053 scalar_dest = gimple_assign_lhs (stmt);
7055 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
7057 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
7059 exit_phi = USE_STMT (use_p);
7060 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
7065 /* Handle stmts whose DEF is used outside the loop-nest that is
7066 being vectorized. */
7067 if (STMT_VINFO_LIVE_P (stmt_info)
7068 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
7070 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
7071 gcc_assert (done);
7074 if (vec_stmt)
7075 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
7077 return is_store;
7081 /* Remove a group of stores (for SLP or interleaving), free their
7082 stmt_vec_info. */
7084 void
7085 vect_remove_stores (gimple first_stmt)
7087 gimple next = first_stmt;
7088 gimple tmp;
7089 gimple_stmt_iterator next_si;
7091 while (next)
7093 stmt_vec_info stmt_info = vinfo_for_stmt (next);
7095 tmp = GROUP_NEXT_ELEMENT (stmt_info);
7096 if (is_pattern_stmt_p (stmt_info))
7097 next = STMT_VINFO_RELATED_STMT (stmt_info);
7098 /* Free the attached stmt_vec_info and remove the stmt. */
7099 next_si = gsi_for_stmt (next);
7100 unlink_stmt_vdef (next);
7101 gsi_remove (&next_si, true);
7102 release_defs (next);
7103 free_stmt_vec_info (next);
7104 next = tmp;
7109 /* Function new_stmt_vec_info.
7111 Create and initialize a new stmt_vec_info struct for STMT. */
7113 stmt_vec_info
7114 new_stmt_vec_info (gimple stmt, loop_vec_info loop_vinfo,
7115 bb_vec_info bb_vinfo)
7117 stmt_vec_info res;
7118 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
7120 STMT_VINFO_TYPE (res) = undef_vec_info_type;
7121 STMT_VINFO_STMT (res) = stmt;
7122 STMT_VINFO_LOOP_VINFO (res) = loop_vinfo;
7123 STMT_VINFO_BB_VINFO (res) = bb_vinfo;
7124 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
7125 STMT_VINFO_LIVE_P (res) = false;
7126 STMT_VINFO_VECTYPE (res) = NULL;
7127 STMT_VINFO_VEC_STMT (res) = NULL;
7128 STMT_VINFO_VECTORIZABLE (res) = true;
7129 STMT_VINFO_IN_PATTERN_P (res) = false;
7130 STMT_VINFO_RELATED_STMT (res) = NULL;
7131 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
7132 STMT_VINFO_DATA_REF (res) = NULL;
7134 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
7135 STMT_VINFO_DR_OFFSET (res) = NULL;
7136 STMT_VINFO_DR_INIT (res) = NULL;
7137 STMT_VINFO_DR_STEP (res) = NULL;
7138 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
7140 if (gimple_code (stmt) == GIMPLE_PHI
7141 && is_loop_header_bb_p (gimple_bb (stmt)))
7142 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
7143 else
7144 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
7146 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
7147 STMT_SLP_TYPE (res) = loop_vect;
7148 GROUP_FIRST_ELEMENT (res) = NULL;
7149 GROUP_NEXT_ELEMENT (res) = NULL;
7150 GROUP_SIZE (res) = 0;
7151 GROUP_STORE_COUNT (res) = 0;
7152 GROUP_GAP (res) = 0;
7153 GROUP_SAME_DR_STMT (res) = NULL;
7155 return res;
7159 /* Create a hash table for stmt_vec_info. */
7161 void
7162 init_stmt_vec_info_vec (void)
7164 gcc_assert (!stmt_vec_info_vec.exists ());
7165 stmt_vec_info_vec.create (50);
7169 /* Free hash table for stmt_vec_info. */
7171 void
7172 free_stmt_vec_info_vec (void)
7174 unsigned int i;
7175 vec_void_p info;
7176 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
7177 if (info != NULL)
7178 free_stmt_vec_info (STMT_VINFO_STMT ((stmt_vec_info) info));
7179 gcc_assert (stmt_vec_info_vec.exists ());
7180 stmt_vec_info_vec.release ();
7184 /* Free stmt vectorization related info. */
7186 void
7187 free_stmt_vec_info (gimple stmt)
7189 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7191 if (!stmt_info)
7192 return;
7194 /* Check if this statement has a related "pattern stmt"
7195 (introduced by the vectorizer during the pattern recognition
7196 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
7197 too. */
7198 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
7200 stmt_vec_info patt_info
7201 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
7202 if (patt_info)
7204 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
7205 if (seq)
7207 gimple_stmt_iterator si;
7208 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
7209 free_stmt_vec_info (gsi_stmt (si));
7211 free_stmt_vec_info (STMT_VINFO_RELATED_STMT (stmt_info));
7215 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
7216 set_vinfo_for_stmt (stmt, NULL);
7217 free (stmt_info);
7221 /* Function get_vectype_for_scalar_type_and_size.
7223 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
7224 by the target. */
7226 static tree
7227 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
7229 enum machine_mode inner_mode = TYPE_MODE (scalar_type);
7230 enum machine_mode simd_mode;
7231 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
7232 int nunits;
7233 tree vectype;
7235 if (nbytes == 0)
7236 return NULL_TREE;
7238 if (GET_MODE_CLASS (inner_mode) != MODE_INT
7239 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
7240 return NULL_TREE;
7242 /* For vector types of elements whose mode precision doesn't
7243 match their types precision we use a element type of mode
7244 precision. The vectorization routines will have to make sure
7245 they support the proper result truncation/extension.
7246 We also make sure to build vector types with INTEGER_TYPE
7247 component type only. */
7248 if (INTEGRAL_TYPE_P (scalar_type)
7249 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
7250 || TREE_CODE (scalar_type) != INTEGER_TYPE))
7251 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
7252 TYPE_UNSIGNED (scalar_type));
7254 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
7255 When the component mode passes the above test simply use a type
7256 corresponding to that mode. The theory is that any use that
7257 would cause problems with this will disable vectorization anyway. */
7258 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
7259 && !INTEGRAL_TYPE_P (scalar_type))
7260 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
7262 /* We can't build a vector type of elements with alignment bigger than
7263 their size. */
7264 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
7265 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
7266 TYPE_UNSIGNED (scalar_type));
7268 /* If we felt back to using the mode fail if there was
7269 no scalar type for it. */
7270 if (scalar_type == NULL_TREE)
7271 return NULL_TREE;
7273 /* If no size was supplied use the mode the target prefers. Otherwise
7274 lookup a vector mode of the specified size. */
7275 if (size == 0)
7276 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
7277 else
7278 simd_mode = mode_for_vector (inner_mode, size / nbytes);
7279 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
7280 if (nunits <= 1)
7281 return NULL_TREE;
7283 vectype = build_vector_type (scalar_type, nunits);
7285 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
7286 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
7287 return NULL_TREE;
7289 return vectype;
7292 unsigned int current_vector_size;
7294 /* Function get_vectype_for_scalar_type.
7296 Returns the vector type corresponding to SCALAR_TYPE as supported
7297 by the target. */
7299 tree
7300 get_vectype_for_scalar_type (tree scalar_type)
7302 tree vectype;
7303 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
7304 current_vector_size);
7305 if (vectype
7306 && current_vector_size == 0)
7307 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
7308 return vectype;
7311 /* Function get_same_sized_vectype
7313 Returns a vector type corresponding to SCALAR_TYPE of size
7314 VECTOR_TYPE if supported by the target. */
7316 tree
7317 get_same_sized_vectype (tree scalar_type, tree vector_type)
7319 return get_vectype_for_scalar_type_and_size
7320 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
7323 /* Function vect_is_simple_use.
7325 Input:
7326 LOOP_VINFO - the vect info of the loop that is being vectorized.
7327 BB_VINFO - the vect info of the basic block that is being vectorized.
7328 OPERAND - operand of STMT in the loop or bb.
7329 DEF - the defining stmt in case OPERAND is an SSA_NAME.
7331 Returns whether a stmt with OPERAND can be vectorized.
7332 For loops, supportable operands are constants, loop invariants, and operands
7333 that are defined by the current iteration of the loop. Unsupportable
7334 operands are those that are defined by a previous iteration of the loop (as
7335 is the case in reduction/induction computations).
7336 For basic blocks, supportable operands are constants and bb invariants.
7337 For now, operands defined outside the basic block are not supported. */
7339 bool
7340 vect_is_simple_use (tree operand, gimple stmt, loop_vec_info loop_vinfo,
7341 bb_vec_info bb_vinfo, gimple *def_stmt,
7342 tree *def, enum vect_def_type *dt)
7344 basic_block bb;
7345 stmt_vec_info stmt_vinfo;
7346 struct loop *loop = NULL;
7348 if (loop_vinfo)
7349 loop = LOOP_VINFO_LOOP (loop_vinfo);
7351 *def_stmt = NULL;
7352 *def = NULL_TREE;
7354 if (dump_enabled_p ())
7356 dump_printf_loc (MSG_NOTE, vect_location,
7357 "vect_is_simple_use: operand ");
7358 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
7359 dump_printf (MSG_NOTE, "\n");
7362 if (CONSTANT_CLASS_P (operand))
7364 *dt = vect_constant_def;
7365 return true;
7368 if (is_gimple_min_invariant (operand))
7370 *def = operand;
7371 *dt = vect_external_def;
7372 return true;
7375 if (TREE_CODE (operand) == PAREN_EXPR)
7377 if (dump_enabled_p ())
7378 dump_printf_loc (MSG_NOTE, vect_location, "non-associatable copy.\n");
7379 operand = TREE_OPERAND (operand, 0);
7382 if (TREE_CODE (operand) != SSA_NAME)
7384 if (dump_enabled_p ())
7385 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7386 "not ssa-name.\n");
7387 return false;
7390 *def_stmt = SSA_NAME_DEF_STMT (operand);
7391 if (*def_stmt == NULL)
7393 if (dump_enabled_p ())
7394 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7395 "no def_stmt.\n");
7396 return false;
7399 if (dump_enabled_p ())
7401 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
7402 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
7403 dump_printf (MSG_NOTE, "\n");
7406 /* Empty stmt is expected only in case of a function argument.
7407 (Otherwise - we expect a phi_node or a GIMPLE_ASSIGN). */
7408 if (gimple_nop_p (*def_stmt))
7410 *def = operand;
7411 *dt = vect_external_def;
7412 return true;
7415 bb = gimple_bb (*def_stmt);
7417 if ((loop && !flow_bb_inside_loop_p (loop, bb))
7418 || (!loop && bb != BB_VINFO_BB (bb_vinfo))
7419 || (!loop && gimple_code (*def_stmt) == GIMPLE_PHI))
7420 *dt = vect_external_def;
7421 else
7423 stmt_vinfo = vinfo_for_stmt (*def_stmt);
7424 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
7427 if (*dt == vect_unknown_def_type
7428 || (stmt
7429 && *dt == vect_double_reduction_def
7430 && gimple_code (stmt) != GIMPLE_PHI))
7432 if (dump_enabled_p ())
7433 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7434 "Unsupported pattern.\n");
7435 return false;
7438 if (dump_enabled_p ())
7439 dump_printf_loc (MSG_NOTE, vect_location, "type of def: %d.\n", *dt);
7441 switch (gimple_code (*def_stmt))
7443 case GIMPLE_PHI:
7444 *def = gimple_phi_result (*def_stmt);
7445 break;
7447 case GIMPLE_ASSIGN:
7448 *def = gimple_assign_lhs (*def_stmt);
7449 break;
7451 case GIMPLE_CALL:
7452 *def = gimple_call_lhs (*def_stmt);
7453 if (*def != NULL)
7454 break;
7455 /* FALLTHRU */
7456 default:
7457 if (dump_enabled_p ())
7458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7459 "unsupported defining stmt:\n");
7460 return false;
7463 return true;
7466 /* Function vect_is_simple_use_1.
7468 Same as vect_is_simple_use_1 but also determines the vector operand
7469 type of OPERAND and stores it to *VECTYPE. If the definition of
7470 OPERAND is vect_uninitialized_def, vect_constant_def or
7471 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
7472 is responsible to compute the best suited vector type for the
7473 scalar operand. */
7475 bool
7476 vect_is_simple_use_1 (tree operand, gimple stmt, loop_vec_info loop_vinfo,
7477 bb_vec_info bb_vinfo, gimple *def_stmt,
7478 tree *def, enum vect_def_type *dt, tree *vectype)
7480 if (!vect_is_simple_use (operand, stmt, loop_vinfo, bb_vinfo, def_stmt,
7481 def, dt))
7482 return false;
7484 /* Now get a vector type if the def is internal, otherwise supply
7485 NULL_TREE and leave it up to the caller to figure out a proper
7486 type for the use stmt. */
7487 if (*dt == vect_internal_def
7488 || *dt == vect_induction_def
7489 || *dt == vect_reduction_def
7490 || *dt == vect_double_reduction_def
7491 || *dt == vect_nested_cycle)
7493 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
7495 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7496 && !STMT_VINFO_RELEVANT (stmt_info)
7497 && !STMT_VINFO_LIVE_P (stmt_info))
7498 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
7500 *vectype = STMT_VINFO_VECTYPE (stmt_info);
7501 gcc_assert (*vectype != NULL_TREE);
7503 else if (*dt == vect_uninitialized_def
7504 || *dt == vect_constant_def
7505 || *dt == vect_external_def)
7506 *vectype = NULL_TREE;
7507 else
7508 gcc_unreachable ();
7510 return true;
7514 /* Function supportable_widening_operation
7516 Check whether an operation represented by the code CODE is a
7517 widening operation that is supported by the target platform in
7518 vector form (i.e., when operating on arguments of type VECTYPE_IN
7519 producing a result of type VECTYPE_OUT).
7521 Widening operations we currently support are NOP (CONVERT), FLOAT
7522 and WIDEN_MULT. This function checks if these operations are supported
7523 by the target platform either directly (via vector tree-codes), or via
7524 target builtins.
7526 Output:
7527 - CODE1 and CODE2 are codes of vector operations to be used when
7528 vectorizing the operation, if available.
7529 - MULTI_STEP_CVT determines the number of required intermediate steps in
7530 case of multi-step conversion (like char->short->int - in that case
7531 MULTI_STEP_CVT will be 1).
7532 - INTERM_TYPES contains the intermediate type required to perform the
7533 widening operation (short in the above example). */
7535 bool
7536 supportable_widening_operation (enum tree_code code, gimple stmt,
7537 tree vectype_out, tree vectype_in,
7538 enum tree_code *code1, enum tree_code *code2,
7539 int *multi_step_cvt,
7540 vec<tree> *interm_types)
7542 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7543 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
7544 struct loop *vect_loop = NULL;
7545 enum machine_mode vec_mode;
7546 enum insn_code icode1, icode2;
7547 optab optab1, optab2;
7548 tree vectype = vectype_in;
7549 tree wide_vectype = vectype_out;
7550 enum tree_code c1, c2;
7551 int i;
7552 tree prev_type, intermediate_type;
7553 enum machine_mode intermediate_mode, prev_mode;
7554 optab optab3, optab4;
7556 *multi_step_cvt = 0;
7557 if (loop_info)
7558 vect_loop = LOOP_VINFO_LOOP (loop_info);
7560 switch (code)
7562 case WIDEN_MULT_EXPR:
7563 /* The result of a vectorized widening operation usually requires
7564 two vectors (because the widened results do not fit into one vector).
7565 The generated vector results would normally be expected to be
7566 generated in the same order as in the original scalar computation,
7567 i.e. if 8 results are generated in each vector iteration, they are
7568 to be organized as follows:
7569 vect1: [res1,res2,res3,res4],
7570 vect2: [res5,res6,res7,res8].
7572 However, in the special case that the result of the widening
7573 operation is used in a reduction computation only, the order doesn't
7574 matter (because when vectorizing a reduction we change the order of
7575 the computation). Some targets can take advantage of this and
7576 generate more efficient code. For example, targets like Altivec,
7577 that support widen_mult using a sequence of {mult_even,mult_odd}
7578 generate the following vectors:
7579 vect1: [res1,res3,res5,res7],
7580 vect2: [res2,res4,res6,res8].
7582 When vectorizing outer-loops, we execute the inner-loop sequentially
7583 (each vectorized inner-loop iteration contributes to VF outer-loop
7584 iterations in parallel). We therefore don't allow to change the
7585 order of the computation in the inner-loop during outer-loop
7586 vectorization. */
7587 /* TODO: Another case in which order doesn't *really* matter is when we
7588 widen and then contract again, e.g. (short)((int)x * y >> 8).
7589 Normally, pack_trunc performs an even/odd permute, whereas the
7590 repack from an even/odd expansion would be an interleave, which
7591 would be significantly simpler for e.g. AVX2. */
7592 /* In any case, in order to avoid duplicating the code below, recurse
7593 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
7594 are properly set up for the caller. If we fail, we'll continue with
7595 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
7596 if (vect_loop
7597 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
7598 && !nested_in_vect_loop_p (vect_loop, stmt)
7599 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
7600 stmt, vectype_out, vectype_in,
7601 code1, code2, multi_step_cvt,
7602 interm_types))
7603 return true;
7604 c1 = VEC_WIDEN_MULT_LO_EXPR;
7605 c2 = VEC_WIDEN_MULT_HI_EXPR;
7606 break;
7608 case VEC_WIDEN_MULT_EVEN_EXPR:
7609 /* Support the recursion induced just above. */
7610 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
7611 c2 = VEC_WIDEN_MULT_ODD_EXPR;
7612 break;
7614 case WIDEN_LSHIFT_EXPR:
7615 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
7616 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
7617 break;
7619 CASE_CONVERT:
7620 c1 = VEC_UNPACK_LO_EXPR;
7621 c2 = VEC_UNPACK_HI_EXPR;
7622 break;
7624 case FLOAT_EXPR:
7625 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
7626 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
7627 break;
7629 case FIX_TRUNC_EXPR:
7630 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
7631 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
7632 computing the operation. */
7633 return false;
7635 default:
7636 gcc_unreachable ();
7639 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
7641 enum tree_code ctmp = c1;
7642 c1 = c2;
7643 c2 = ctmp;
7646 if (code == FIX_TRUNC_EXPR)
7648 /* The signedness is determined from output operand. */
7649 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
7650 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
7652 else
7654 optab1 = optab_for_tree_code (c1, vectype, optab_default);
7655 optab2 = optab_for_tree_code (c2, vectype, optab_default);
7658 if (!optab1 || !optab2)
7659 return false;
7661 vec_mode = TYPE_MODE (vectype);
7662 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
7663 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
7664 return false;
7666 *code1 = c1;
7667 *code2 = c2;
7669 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
7670 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
7671 return true;
7673 /* Check if it's a multi-step conversion that can be done using intermediate
7674 types. */
7676 prev_type = vectype;
7677 prev_mode = vec_mode;
7679 if (!CONVERT_EXPR_CODE_P (code))
7680 return false;
7682 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
7683 intermediate steps in promotion sequence. We try
7684 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
7685 not. */
7686 interm_types->create (MAX_INTERM_CVT_STEPS);
7687 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
7689 intermediate_mode = insn_data[icode1].operand[0].mode;
7690 intermediate_type
7691 = lang_hooks.types.type_for_mode (intermediate_mode,
7692 TYPE_UNSIGNED (prev_type));
7693 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
7694 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
7696 if (!optab3 || !optab4
7697 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
7698 || insn_data[icode1].operand[0].mode != intermediate_mode
7699 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
7700 || insn_data[icode2].operand[0].mode != intermediate_mode
7701 || ((icode1 = optab_handler (optab3, intermediate_mode))
7702 == CODE_FOR_nothing)
7703 || ((icode2 = optab_handler (optab4, intermediate_mode))
7704 == CODE_FOR_nothing))
7705 break;
7707 interm_types->quick_push (intermediate_type);
7708 (*multi_step_cvt)++;
7710 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
7711 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
7712 return true;
7714 prev_type = intermediate_type;
7715 prev_mode = intermediate_mode;
7718 interm_types->release ();
7719 return false;
7723 /* Function supportable_narrowing_operation
7725 Check whether an operation represented by the code CODE is a
7726 narrowing operation that is supported by the target platform in
7727 vector form (i.e., when operating on arguments of type VECTYPE_IN
7728 and producing a result of type VECTYPE_OUT).
7730 Narrowing operations we currently support are NOP (CONVERT) and
7731 FIX_TRUNC. This function checks if these operations are supported by
7732 the target platform directly via vector tree-codes.
7734 Output:
7735 - CODE1 is the code of a vector operation to be used when
7736 vectorizing the operation, if available.
7737 - MULTI_STEP_CVT determines the number of required intermediate steps in
7738 case of multi-step conversion (like int->short->char - in that case
7739 MULTI_STEP_CVT will be 1).
7740 - INTERM_TYPES contains the intermediate type required to perform the
7741 narrowing operation (short in the above example). */
7743 bool
7744 supportable_narrowing_operation (enum tree_code code,
7745 tree vectype_out, tree vectype_in,
7746 enum tree_code *code1, int *multi_step_cvt,
7747 vec<tree> *interm_types)
7749 enum machine_mode vec_mode;
7750 enum insn_code icode1;
7751 optab optab1, interm_optab;
7752 tree vectype = vectype_in;
7753 tree narrow_vectype = vectype_out;
7754 enum tree_code c1;
7755 tree intermediate_type;
7756 enum machine_mode intermediate_mode, prev_mode;
7757 int i;
7758 bool uns;
7760 *multi_step_cvt = 0;
7761 switch (code)
7763 CASE_CONVERT:
7764 c1 = VEC_PACK_TRUNC_EXPR;
7765 break;
7767 case FIX_TRUNC_EXPR:
7768 c1 = VEC_PACK_FIX_TRUNC_EXPR;
7769 break;
7771 case FLOAT_EXPR:
7772 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
7773 tree code and optabs used for computing the operation. */
7774 return false;
7776 default:
7777 gcc_unreachable ();
7780 if (code == FIX_TRUNC_EXPR)
7781 /* The signedness is determined from output operand. */
7782 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
7783 else
7784 optab1 = optab_for_tree_code (c1, vectype, optab_default);
7786 if (!optab1)
7787 return false;
7789 vec_mode = TYPE_MODE (vectype);
7790 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
7791 return false;
7793 *code1 = c1;
7795 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
7796 return true;
7798 /* Check if it's a multi-step conversion that can be done using intermediate
7799 types. */
7800 prev_mode = vec_mode;
7801 if (code == FIX_TRUNC_EXPR)
7802 uns = TYPE_UNSIGNED (vectype_out);
7803 else
7804 uns = TYPE_UNSIGNED (vectype);
7806 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
7807 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
7808 costly than signed. */
7809 if (code == FIX_TRUNC_EXPR && uns)
7811 enum insn_code icode2;
7813 intermediate_type
7814 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
7815 interm_optab
7816 = optab_for_tree_code (c1, intermediate_type, optab_default);
7817 if (interm_optab != unknown_optab
7818 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
7819 && insn_data[icode1].operand[0].mode
7820 == insn_data[icode2].operand[0].mode)
7822 uns = false;
7823 optab1 = interm_optab;
7824 icode1 = icode2;
7828 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
7829 intermediate steps in promotion sequence. We try
7830 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
7831 interm_types->create (MAX_INTERM_CVT_STEPS);
7832 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
7834 intermediate_mode = insn_data[icode1].operand[0].mode;
7835 intermediate_type
7836 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
7837 interm_optab
7838 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
7839 optab_default);
7840 if (!interm_optab
7841 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
7842 || insn_data[icode1].operand[0].mode != intermediate_mode
7843 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
7844 == CODE_FOR_nothing))
7845 break;
7847 interm_types->quick_push (intermediate_type);
7848 (*multi_step_cvt)++;
7850 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
7851 return true;
7853 prev_mode = intermediate_mode;
7854 optab1 = interm_optab;
7857 interm_types->release ();
7858 return false;