optabs.c (expand_binop_directly): Allow scalar mode for vec_pack_trunc_optab.
[gcc.git] / gcc / tree-vect-stmts.c
1 /* Statement Analysis and Transformation for Vectorization
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "ssa.h"
31 #include "optabs-tree.h"
32 #include "insn-config.h"
33 #include "recog.h" /* FIXME: for insn_data */
34 #include "cgraph.h"
35 #include "dumpfile.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "tree-eh.h"
40 #include "gimplify.h"
41 #include "gimple-iterator.h"
42 #include "gimplify-me.h"
43 #include "tree-cfg.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "cfgloop.h"
46 #include "tree-ssa-loop.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-vectorizer.h"
49 #include "builtins.h"
50
51 /* For lang_hooks.types.type_for_mode. */
52 #include "langhooks.h"
53
54 /* Return the vectorized type for the given statement. */
55
56 tree
57 stmt_vectype (struct _stmt_vec_info *stmt_info)
58 {
59 return STMT_VINFO_VECTYPE (stmt_info);
60 }
61
62 /* Return TRUE iff the given statement is in an inner loop relative to
63 the loop being vectorized. */
64 bool
65 stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
66 {
67 gimple *stmt = STMT_VINFO_STMT (stmt_info);
68 basic_block bb = gimple_bb (stmt);
69 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
70 struct loop* loop;
71
72 if (!loop_vinfo)
73 return false;
74
75 loop = LOOP_VINFO_LOOP (loop_vinfo);
76
77 return (bb->loop_father == loop->inner);
78 }
79
80 /* Record the cost of a statement, either by directly informing the
81 target model or by saving it in a vector for later processing.
82 Return a preliminary estimate of the statement's cost. */
83
84 unsigned
85 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
86 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
87 int misalign, enum vect_cost_model_location where)
88 {
89 if (body_cost_vec)
90 {
91 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
92 stmt_info_for_cost si = { count, kind,
93 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
94 misalign };
95 body_cost_vec->safe_push (si);
96 return (unsigned)
97 (builtin_vectorization_cost (kind, vectype, misalign) * count);
98 }
99 else
100 return add_stmt_cost (stmt_info->vinfo->target_cost_data,
101 count, kind, stmt_info, misalign, where);
102 }
103
104 /* Return a variable of type ELEM_TYPE[NELEMS]. */
105
106 static tree
107 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
108 {
109 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
110 "vect_array");
111 }
112
113 /* ARRAY is an array of vectors created by create_vector_array.
114 Return an SSA_NAME for the vector in index N. The reference
115 is part of the vectorization of STMT and the vector is associated
116 with scalar destination SCALAR_DEST. */
117
118 static tree
119 read_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
120 tree array, unsigned HOST_WIDE_INT n)
121 {
122 tree vect_type, vect, vect_name, array_ref;
123 gimple *new_stmt;
124
125 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
126 vect_type = TREE_TYPE (TREE_TYPE (array));
127 vect = vect_create_destination_var (scalar_dest, vect_type);
128 array_ref = build4 (ARRAY_REF, vect_type, array,
129 build_int_cst (size_type_node, n),
130 NULL_TREE, NULL_TREE);
131
132 new_stmt = gimple_build_assign (vect, array_ref);
133 vect_name = make_ssa_name (vect, new_stmt);
134 gimple_assign_set_lhs (new_stmt, vect_name);
135 vect_finish_stmt_generation (stmt, new_stmt, gsi);
136
137 return vect_name;
138 }
139
140 /* ARRAY is an array of vectors created by create_vector_array.
141 Emit code to store SSA_NAME VECT in index N of the array.
142 The store is part of the vectorization of STMT. */
143
144 static void
145 write_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree vect,
146 tree array, unsigned HOST_WIDE_INT n)
147 {
148 tree array_ref;
149 gimple *new_stmt;
150
151 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
152 build_int_cst (size_type_node, n),
153 NULL_TREE, NULL_TREE);
154
155 new_stmt = gimple_build_assign (array_ref, vect);
156 vect_finish_stmt_generation (stmt, new_stmt, gsi);
157 }
158
159 /* PTR is a pointer to an array of type TYPE. Return a representation
160 of *PTR. The memory reference replaces those in FIRST_DR
161 (and its group). */
162
163 static tree
164 create_array_ref (tree type, tree ptr, struct data_reference *first_dr)
165 {
166 tree mem_ref, alias_ptr_type;
167
168 alias_ptr_type = reference_alias_ptr_type (DR_REF (first_dr));
169 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
170 /* Arrays have the same alignment as their type. */
171 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
172 return mem_ref;
173 }
174
175 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
176
177 /* Function vect_mark_relevant.
178
179 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
180
181 static void
182 vect_mark_relevant (vec<gimple *> *worklist, gimple *stmt,
183 enum vect_relevant relevant, bool live_p,
184 bool used_in_pattern)
185 {
186 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
187 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
188 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
189 gimple *pattern_stmt;
190
191 if (dump_enabled_p ())
192 dump_printf_loc (MSG_NOTE, vect_location,
193 "mark relevant %d, live %d.\n", relevant, live_p);
194
195 /* If this stmt is an original stmt in a pattern, we might need to mark its
196 related pattern stmt instead of the original stmt. However, such stmts
197 may have their own uses that are not in any pattern, in such cases the
198 stmt itself should be marked. */
199 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
200 {
201 bool found = false;
202 if (!used_in_pattern)
203 {
204 imm_use_iterator imm_iter;
205 use_operand_p use_p;
206 gimple *use_stmt;
207 tree lhs;
208 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
209 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
210
211 if (is_gimple_assign (stmt))
212 lhs = gimple_assign_lhs (stmt);
213 else
214 lhs = gimple_call_lhs (stmt);
215
216 /* This use is out of pattern use, if LHS has other uses that are
217 pattern uses, we should mark the stmt itself, and not the pattern
218 stmt. */
219 if (lhs && TREE_CODE (lhs) == SSA_NAME)
220 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
221 {
222 if (is_gimple_debug (USE_STMT (use_p)))
223 continue;
224 use_stmt = USE_STMT (use_p);
225
226 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
227 continue;
228
229 if (vinfo_for_stmt (use_stmt)
230 && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
231 {
232 found = true;
233 break;
234 }
235 }
236 }
237
238 if (!found)
239 {
240 /* This is the last stmt in a sequence that was detected as a
241 pattern that can potentially be vectorized. Don't mark the stmt
242 as relevant/live because it's not going to be vectorized.
243 Instead mark the pattern-stmt that replaces it. */
244
245 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
246
247 if (dump_enabled_p ())
248 dump_printf_loc (MSG_NOTE, vect_location,
249 "last stmt in pattern. don't mark"
250 " relevant/live.\n");
251 stmt_info = vinfo_for_stmt (pattern_stmt);
252 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
253 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
254 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
255 stmt = pattern_stmt;
256 }
257 }
258
259 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
260 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
261 STMT_VINFO_RELEVANT (stmt_info) = relevant;
262
263 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
264 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
265 {
266 if (dump_enabled_p ())
267 dump_printf_loc (MSG_NOTE, vect_location,
268 "already marked relevant/live.\n");
269 return;
270 }
271
272 worklist->safe_push (stmt);
273 }
274
275
276 /* Function vect_stmt_relevant_p.
277
278 Return true if STMT in loop that is represented by LOOP_VINFO is
279 "relevant for vectorization".
280
281 A stmt is considered "relevant for vectorization" if:
282 - it has uses outside the loop.
283 - it has vdefs (it alters memory).
284 - control stmts in the loop (except for the exit condition).
285
286 CHECKME: what other side effects would the vectorizer allow? */
287
288 static bool
289 vect_stmt_relevant_p (gimple *stmt, loop_vec_info loop_vinfo,
290 enum vect_relevant *relevant, bool *live_p)
291 {
292 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
293 ssa_op_iter op_iter;
294 imm_use_iterator imm_iter;
295 use_operand_p use_p;
296 def_operand_p def_p;
297
298 *relevant = vect_unused_in_scope;
299 *live_p = false;
300
301 /* cond stmt other than loop exit cond. */
302 if (is_ctrl_stmt (stmt)
303 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
304 != loop_exit_ctrl_vec_info_type)
305 *relevant = vect_used_in_scope;
306
307 /* changing memory. */
308 if (gimple_code (stmt) != GIMPLE_PHI)
309 if (gimple_vdef (stmt)
310 && !gimple_clobber_p (stmt))
311 {
312 if (dump_enabled_p ())
313 dump_printf_loc (MSG_NOTE, vect_location,
314 "vec_stmt_relevant_p: stmt has vdefs.\n");
315 *relevant = vect_used_in_scope;
316 }
317
318 /* uses outside the loop. */
319 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
320 {
321 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
322 {
323 basic_block bb = gimple_bb (USE_STMT (use_p));
324 if (!flow_bb_inside_loop_p (loop, bb))
325 {
326 if (dump_enabled_p ())
327 dump_printf_loc (MSG_NOTE, vect_location,
328 "vec_stmt_relevant_p: used out of loop.\n");
329
330 if (is_gimple_debug (USE_STMT (use_p)))
331 continue;
332
333 /* We expect all such uses to be in the loop exit phis
334 (because of loop closed form) */
335 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
336 gcc_assert (bb == single_exit (loop)->dest);
337
338 *live_p = true;
339 }
340 }
341 }
342
343 return (*live_p || *relevant);
344 }
345
346
347 /* Function exist_non_indexing_operands_for_use_p
348
349 USE is one of the uses attached to STMT. Check if USE is
350 used in STMT for anything other than indexing an array. */
351
352 static bool
353 exist_non_indexing_operands_for_use_p (tree use, gimple *stmt)
354 {
355 tree operand;
356 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
357
358 /* USE corresponds to some operand in STMT. If there is no data
359 reference in STMT, then any operand that corresponds to USE
360 is not indexing an array. */
361 if (!STMT_VINFO_DATA_REF (stmt_info))
362 return true;
363
364 /* STMT has a data_ref. FORNOW this means that its of one of
365 the following forms:
366 -1- ARRAY_REF = var
367 -2- var = ARRAY_REF
368 (This should have been verified in analyze_data_refs).
369
370 'var' in the second case corresponds to a def, not a use,
371 so USE cannot correspond to any operands that are not used
372 for array indexing.
373
374 Therefore, all we need to check is if STMT falls into the
375 first case, and whether var corresponds to USE. */
376
377 if (!gimple_assign_copy_p (stmt))
378 {
379 if (is_gimple_call (stmt)
380 && gimple_call_internal_p (stmt))
381 switch (gimple_call_internal_fn (stmt))
382 {
383 case IFN_MASK_STORE:
384 operand = gimple_call_arg (stmt, 3);
385 if (operand == use)
386 return true;
387 /* FALLTHRU */
388 case IFN_MASK_LOAD:
389 operand = gimple_call_arg (stmt, 2);
390 if (operand == use)
391 return true;
392 break;
393 default:
394 break;
395 }
396 return false;
397 }
398
399 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
400 return false;
401 operand = gimple_assign_rhs1 (stmt);
402 if (TREE_CODE (operand) != SSA_NAME)
403 return false;
404
405 if (operand == use)
406 return true;
407
408 return false;
409 }
410
411
412 /*
413 Function process_use.
414
415 Inputs:
416 - a USE in STMT in a loop represented by LOOP_VINFO
417 - LIVE_P, RELEVANT - enum values to be set in the STMT_VINFO of the stmt
418 that defined USE. This is done by calling mark_relevant and passing it
419 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
420 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
421 be performed.
422
423 Outputs:
424 Generally, LIVE_P and RELEVANT are used to define the liveness and
425 relevance info of the DEF_STMT of this USE:
426 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
427 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
428 Exceptions:
429 - case 1: If USE is used only for address computations (e.g. array indexing),
430 which does not need to be directly vectorized, then the liveness/relevance
431 of the respective DEF_STMT is left unchanged.
432 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
433 skip DEF_STMT cause it had already been processed.
434 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
435 be modified accordingly.
436
437 Return true if everything is as expected. Return false otherwise. */
438
439 static bool
440 process_use (gimple *stmt, tree use, loop_vec_info loop_vinfo, bool live_p,
441 enum vect_relevant relevant, vec<gimple *> *worklist,
442 bool force)
443 {
444 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
445 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
446 stmt_vec_info dstmt_vinfo;
447 basic_block bb, def_bb;
448 gimple *def_stmt;
449 enum vect_def_type dt;
450
451 /* case 1: we are only interested in uses that need to be vectorized. Uses
452 that are used for address computation are not considered relevant. */
453 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
454 return true;
455
456 if (!vect_is_simple_use (use, loop_vinfo, &def_stmt, &dt))
457 {
458 if (dump_enabled_p ())
459 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
460 "not vectorized: unsupported use in stmt.\n");
461 return false;
462 }
463
464 if (!def_stmt || gimple_nop_p (def_stmt))
465 return true;
466
467 def_bb = gimple_bb (def_stmt);
468 if (!flow_bb_inside_loop_p (loop, def_bb))
469 {
470 if (dump_enabled_p ())
471 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt is out of loop.\n");
472 return true;
473 }
474
475 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DEF_STMT).
476 DEF_STMT must have already been processed, because this should be the
477 only way that STMT, which is a reduction-phi, was put in the worklist,
478 as there should be no other uses for DEF_STMT in the loop. So we just
479 check that everything is as expected, and we are done. */
480 dstmt_vinfo = vinfo_for_stmt (def_stmt);
481 bb = gimple_bb (stmt);
482 if (gimple_code (stmt) == GIMPLE_PHI
483 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
484 && gimple_code (def_stmt) != GIMPLE_PHI
485 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
486 && bb->loop_father == def_bb->loop_father)
487 {
488 if (dump_enabled_p ())
489 dump_printf_loc (MSG_NOTE, vect_location,
490 "reduc-stmt defining reduc-phi in the same nest.\n");
491 if (STMT_VINFO_IN_PATTERN_P (dstmt_vinfo))
492 dstmt_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (dstmt_vinfo));
493 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
494 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
495 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
496 return true;
497 }
498
499 /* case 3a: outer-loop stmt defining an inner-loop stmt:
500 outer-loop-header-bb:
501 d = def_stmt
502 inner-loop:
503 stmt # use (d)
504 outer-loop-tail-bb:
505 ... */
506 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
507 {
508 if (dump_enabled_p ())
509 dump_printf_loc (MSG_NOTE, vect_location,
510 "outer-loop def-stmt defining inner-loop stmt.\n");
511
512 switch (relevant)
513 {
514 case vect_unused_in_scope:
515 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
516 vect_used_in_scope : vect_unused_in_scope;
517 break;
518
519 case vect_used_in_outer_by_reduction:
520 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
521 relevant = vect_used_by_reduction;
522 break;
523
524 case vect_used_in_outer:
525 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
526 relevant = vect_used_in_scope;
527 break;
528
529 case vect_used_in_scope:
530 break;
531
532 default:
533 gcc_unreachable ();
534 }
535 }
536
537 /* case 3b: inner-loop stmt defining an outer-loop stmt:
538 outer-loop-header-bb:
539 ...
540 inner-loop:
541 d = def_stmt
542 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
543 stmt # use (d) */
544 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
545 {
546 if (dump_enabled_p ())
547 dump_printf_loc (MSG_NOTE, vect_location,
548 "inner-loop def-stmt defining outer-loop stmt.\n");
549
550 switch (relevant)
551 {
552 case vect_unused_in_scope:
553 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
554 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
555 vect_used_in_outer_by_reduction : vect_unused_in_scope;
556 break;
557
558 case vect_used_by_reduction:
559 relevant = vect_used_in_outer_by_reduction;
560 break;
561
562 case vect_used_in_scope:
563 relevant = vect_used_in_outer;
564 break;
565
566 default:
567 gcc_unreachable ();
568 }
569 }
570
571 vect_mark_relevant (worklist, def_stmt, relevant, live_p,
572 is_pattern_stmt_p (stmt_vinfo));
573 return true;
574 }
575
576
577 /* Function vect_mark_stmts_to_be_vectorized.
578
579 Not all stmts in the loop need to be vectorized. For example:
580
581 for i...
582 for j...
583 1. T0 = i + j
584 2. T1 = a[T0]
585
586 3. j = j + 1
587
588 Stmt 1 and 3 do not need to be vectorized, because loop control and
589 addressing of vectorized data-refs are handled differently.
590
591 This pass detects such stmts. */
592
593 bool
594 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
595 {
596 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
597 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
598 unsigned int nbbs = loop->num_nodes;
599 gimple_stmt_iterator si;
600 gimple *stmt;
601 unsigned int i;
602 stmt_vec_info stmt_vinfo;
603 basic_block bb;
604 gimple *phi;
605 bool live_p;
606 enum vect_relevant relevant, tmp_relevant;
607 enum vect_def_type def_type;
608
609 if (dump_enabled_p ())
610 dump_printf_loc (MSG_NOTE, vect_location,
611 "=== vect_mark_stmts_to_be_vectorized ===\n");
612
613 auto_vec<gimple *, 64> worklist;
614
615 /* 1. Init worklist. */
616 for (i = 0; i < nbbs; i++)
617 {
618 bb = bbs[i];
619 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
620 {
621 phi = gsi_stmt (si);
622 if (dump_enabled_p ())
623 {
624 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
625 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
626 }
627
628 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
629 vect_mark_relevant (&worklist, phi, relevant, live_p, false);
630 }
631 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
632 {
633 stmt = gsi_stmt (si);
634 if (dump_enabled_p ())
635 {
636 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
637 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
638 }
639
640 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
641 vect_mark_relevant (&worklist, stmt, relevant, live_p, false);
642 }
643 }
644
645 /* 2. Process_worklist */
646 while (worklist.length () > 0)
647 {
648 use_operand_p use_p;
649 ssa_op_iter iter;
650
651 stmt = worklist.pop ();
652 if (dump_enabled_p ())
653 {
654 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
655 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
656 }
657
658 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
659 (DEF_STMT) as relevant/irrelevant and live/dead according to the
660 liveness and relevance properties of STMT. */
661 stmt_vinfo = vinfo_for_stmt (stmt);
662 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
663 live_p = STMT_VINFO_LIVE_P (stmt_vinfo);
664
665 /* Generally, the liveness and relevance properties of STMT are
666 propagated as is to the DEF_STMTs of its USEs:
667 live_p <-- STMT_VINFO_LIVE_P (STMT_VINFO)
668 relevant <-- STMT_VINFO_RELEVANT (STMT_VINFO)
669
670 One exception is when STMT has been identified as defining a reduction
671 variable; in this case we set the liveness/relevance as follows:
672 live_p = false
673 relevant = vect_used_by_reduction
674 This is because we distinguish between two kinds of relevant stmts -
675 those that are used by a reduction computation, and those that are
676 (also) used by a regular computation. This allows us later on to
677 identify stmts that are used solely by a reduction, and therefore the
678 order of the results that they produce does not have to be kept. */
679
680 def_type = STMT_VINFO_DEF_TYPE (stmt_vinfo);
681 tmp_relevant = relevant;
682 switch (def_type)
683 {
684 case vect_reduction_def:
685 switch (tmp_relevant)
686 {
687 case vect_unused_in_scope:
688 relevant = vect_used_by_reduction;
689 break;
690
691 case vect_used_by_reduction:
692 if (gimple_code (stmt) == GIMPLE_PHI)
693 break;
694 /* fall through */
695
696 default:
697 if (dump_enabled_p ())
698 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
699 "unsupported use of reduction.\n");
700 return false;
701 }
702
703 live_p = false;
704 break;
705
706 case vect_nested_cycle:
707 if (tmp_relevant != vect_unused_in_scope
708 && tmp_relevant != vect_used_in_outer_by_reduction
709 && tmp_relevant != vect_used_in_outer)
710 {
711 if (dump_enabled_p ())
712 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
713 "unsupported use of nested cycle.\n");
714
715 return false;
716 }
717
718 live_p = false;
719 break;
720
721 case vect_double_reduction_def:
722 if (tmp_relevant != vect_unused_in_scope
723 && tmp_relevant != vect_used_by_reduction)
724 {
725 if (dump_enabled_p ())
726 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
727 "unsupported use of double reduction.\n");
728
729 return false;
730 }
731
732 live_p = false;
733 break;
734
735 default:
736 break;
737 }
738
739 if (is_pattern_stmt_p (stmt_vinfo))
740 {
741 /* Pattern statements are not inserted into the code, so
742 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
743 have to scan the RHS or function arguments instead. */
744 if (is_gimple_assign (stmt))
745 {
746 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
747 tree op = gimple_assign_rhs1 (stmt);
748
749 i = 1;
750 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
751 {
752 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
753 live_p, relevant, &worklist, false)
754 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
755 live_p, relevant, &worklist, false))
756 return false;
757 i = 2;
758 }
759 for (; i < gimple_num_ops (stmt); i++)
760 {
761 op = gimple_op (stmt, i);
762 if (TREE_CODE (op) == SSA_NAME
763 && !process_use (stmt, op, loop_vinfo, live_p, relevant,
764 &worklist, false))
765 return false;
766 }
767 }
768 else if (is_gimple_call (stmt))
769 {
770 for (i = 0; i < gimple_call_num_args (stmt); i++)
771 {
772 tree arg = gimple_call_arg (stmt, i);
773 if (!process_use (stmt, arg, loop_vinfo, live_p, relevant,
774 &worklist, false))
775 return false;
776 }
777 }
778 }
779 else
780 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
781 {
782 tree op = USE_FROM_PTR (use_p);
783 if (!process_use (stmt, op, loop_vinfo, live_p, relevant,
784 &worklist, false))
785 return false;
786 }
787
788 if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
789 {
790 tree off;
791 tree decl = vect_check_gather_scatter (stmt, loop_vinfo, NULL, &off, NULL);
792 gcc_assert (decl);
793 if (!process_use (stmt, off, loop_vinfo, live_p, relevant,
794 &worklist, true))
795 return false;
796 }
797 } /* while worklist */
798
799 return true;
800 }
801
802
803 /* Function vect_model_simple_cost.
804
805 Models cost for simple operations, i.e. those that only emit ncopies of a
806 single op. Right now, this does not account for multiple insns that could
807 be generated for the single vector op. We will handle that shortly. */
808
809 void
810 vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
811 enum vect_def_type *dt,
812 stmt_vector_for_cost *prologue_cost_vec,
813 stmt_vector_for_cost *body_cost_vec)
814 {
815 int i;
816 int inside_cost = 0, prologue_cost = 0;
817
818 /* The SLP costs were already calculated during SLP tree build. */
819 if (PURE_SLP_STMT (stmt_info))
820 return;
821
822 /* FORNOW: Assuming maximum 2 args per stmts. */
823 for (i = 0; i < 2; i++)
824 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
825 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
826 stmt_info, 0, vect_prologue);
827
828 /* Pass the inside-of-loop statements to the target-specific cost model. */
829 inside_cost = record_stmt_cost (body_cost_vec, ncopies, vector_stmt,
830 stmt_info, 0, vect_body);
831
832 if (dump_enabled_p ())
833 dump_printf_loc (MSG_NOTE, vect_location,
834 "vect_model_simple_cost: inside_cost = %d, "
835 "prologue_cost = %d .\n", inside_cost, prologue_cost);
836 }
837
838
839 /* Model cost for type demotion and promotion operations. PWR is normally
840 zero for single-step promotions and demotions. It will be one if
841 two-step promotion/demotion is required, and so on. Each additional
842 step doubles the number of instructions required. */
843
844 static void
845 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
846 enum vect_def_type *dt, int pwr)
847 {
848 int i, tmp;
849 int inside_cost = 0, prologue_cost = 0;
850 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
851 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
852 void *target_cost_data;
853
854 /* The SLP costs were already calculated during SLP tree build. */
855 if (PURE_SLP_STMT (stmt_info))
856 return;
857
858 if (loop_vinfo)
859 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
860 else
861 target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
862
863 for (i = 0; i < pwr + 1; i++)
864 {
865 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
866 (i + 1) : i;
867 inside_cost += add_stmt_cost (target_cost_data, vect_pow2 (tmp),
868 vec_promote_demote, stmt_info, 0,
869 vect_body);
870 }
871
872 /* FORNOW: Assuming maximum 2 args per stmts. */
873 for (i = 0; i < 2; i++)
874 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
875 prologue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
876 stmt_info, 0, vect_prologue);
877
878 if (dump_enabled_p ())
879 dump_printf_loc (MSG_NOTE, vect_location,
880 "vect_model_promotion_demotion_cost: inside_cost = %d, "
881 "prologue_cost = %d .\n", inside_cost, prologue_cost);
882 }
883
884 /* Function vect_cost_group_size
885
886 For grouped load or store, return the group_size only if it is the first
887 load or store of a group, else return 1. This ensures that group size is
888 only returned once per group. */
889
890 static int
891 vect_cost_group_size (stmt_vec_info stmt_info)
892 {
893 gimple *first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
894
895 if (first_stmt == STMT_VINFO_STMT (stmt_info))
896 return GROUP_SIZE (stmt_info);
897
898 return 1;
899 }
900
901
902 /* Function vect_model_store_cost
903
904 Models cost for stores. In the case of grouped accesses, one access
905 has the overhead of the grouped access attributed to it. */
906
907 void
908 vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
909 bool store_lanes_p, enum vect_def_type dt,
910 slp_tree slp_node,
911 stmt_vector_for_cost *prologue_cost_vec,
912 stmt_vector_for_cost *body_cost_vec)
913 {
914 int group_size;
915 unsigned int inside_cost = 0, prologue_cost = 0;
916 struct data_reference *first_dr;
917 gimple *first_stmt;
918
919 if (dt == vect_constant_def || dt == vect_external_def)
920 prologue_cost += record_stmt_cost (prologue_cost_vec, 1, scalar_to_vec,
921 stmt_info, 0, vect_prologue);
922
923 /* Grouped access? */
924 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
925 {
926 if (slp_node)
927 {
928 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
929 group_size = 1;
930 }
931 else
932 {
933 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
934 group_size = vect_cost_group_size (stmt_info);
935 }
936
937 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
938 }
939 /* Not a grouped access. */
940 else
941 {
942 group_size = 1;
943 first_dr = STMT_VINFO_DATA_REF (stmt_info);
944 }
945
946 /* We assume that the cost of a single store-lanes instruction is
947 equivalent to the cost of GROUP_SIZE separate stores. If a grouped
948 access is instead being provided by a permute-and-store operation,
949 include the cost of the permutes. */
950 if (!store_lanes_p && group_size > 1
951 && !STMT_VINFO_STRIDED_P (stmt_info))
952 {
953 /* Uses a high and low interleave or shuffle operations for each
954 needed permute. */
955 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
956 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
957 stmt_info, 0, vect_body);
958
959 if (dump_enabled_p ())
960 dump_printf_loc (MSG_NOTE, vect_location,
961 "vect_model_store_cost: strided group_size = %d .\n",
962 group_size);
963 }
964
965 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
966 /* Costs of the stores. */
967 if (STMT_VINFO_STRIDED_P (stmt_info)
968 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
969 {
970 /* N scalar stores plus extracting the elements. */
971 inside_cost += record_stmt_cost (body_cost_vec,
972 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
973 scalar_store, stmt_info, 0, vect_body);
974 }
975 else
976 vect_get_store_cost (first_dr, ncopies, &inside_cost, body_cost_vec);
977
978 if (STMT_VINFO_STRIDED_P (stmt_info))
979 inside_cost += record_stmt_cost (body_cost_vec,
980 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
981 vec_to_scalar, stmt_info, 0, vect_body);
982
983 if (dump_enabled_p ())
984 dump_printf_loc (MSG_NOTE, vect_location,
985 "vect_model_store_cost: inside_cost = %d, "
986 "prologue_cost = %d .\n", inside_cost, prologue_cost);
987 }
988
989
990 /* Calculate cost of DR's memory access. */
991 void
992 vect_get_store_cost (struct data_reference *dr, int ncopies,
993 unsigned int *inside_cost,
994 stmt_vector_for_cost *body_cost_vec)
995 {
996 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
997 gimple *stmt = DR_STMT (dr);
998 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
999
1000 switch (alignment_support_scheme)
1001 {
1002 case dr_aligned:
1003 {
1004 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1005 vector_store, stmt_info, 0,
1006 vect_body);
1007
1008 if (dump_enabled_p ())
1009 dump_printf_loc (MSG_NOTE, vect_location,
1010 "vect_model_store_cost: aligned.\n");
1011 break;
1012 }
1013
1014 case dr_unaligned_supported:
1015 {
1016 /* Here, we assign an additional cost for the unaligned store. */
1017 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1018 unaligned_store, stmt_info,
1019 DR_MISALIGNMENT (dr), vect_body);
1020 if (dump_enabled_p ())
1021 dump_printf_loc (MSG_NOTE, vect_location,
1022 "vect_model_store_cost: unaligned supported by "
1023 "hardware.\n");
1024 break;
1025 }
1026
1027 case dr_unaligned_unsupported:
1028 {
1029 *inside_cost = VECT_MAX_COST;
1030
1031 if (dump_enabled_p ())
1032 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1033 "vect_model_store_cost: unsupported access.\n");
1034 break;
1035 }
1036
1037 default:
1038 gcc_unreachable ();
1039 }
1040 }
1041
1042
1043 /* Function vect_model_load_cost
1044
1045 Models cost for loads. In the case of grouped accesses, the last access
1046 has the overhead of the grouped access attributed to it. Since unaligned
1047 accesses are supported for loads, we also account for the costs of the
1048 access scheme chosen. */
1049
1050 void
1051 vect_model_load_cost (stmt_vec_info stmt_info, int ncopies,
1052 bool load_lanes_p, slp_tree slp_node,
1053 stmt_vector_for_cost *prologue_cost_vec,
1054 stmt_vector_for_cost *body_cost_vec)
1055 {
1056 int group_size;
1057 gimple *first_stmt;
1058 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr;
1059 unsigned int inside_cost = 0, prologue_cost = 0;
1060
1061 /* Grouped accesses? */
1062 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
1063 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && first_stmt && !slp_node)
1064 {
1065 group_size = vect_cost_group_size (stmt_info);
1066 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
1067 }
1068 /* Not a grouped access. */
1069 else
1070 {
1071 group_size = 1;
1072 first_dr = dr;
1073 }
1074
1075 /* We assume that the cost of a single load-lanes instruction is
1076 equivalent to the cost of GROUP_SIZE separate loads. If a grouped
1077 access is instead being provided by a load-and-permute operation,
1078 include the cost of the permutes. */
1079 if (!load_lanes_p && group_size > 1
1080 && !STMT_VINFO_STRIDED_P (stmt_info))
1081 {
1082 /* Uses an even and odd extract operations or shuffle operations
1083 for each needed permute. */
1084 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
1085 inside_cost = record_stmt_cost (body_cost_vec, nstmts, vec_perm,
1086 stmt_info, 0, vect_body);
1087
1088 if (dump_enabled_p ())
1089 dump_printf_loc (MSG_NOTE, vect_location,
1090 "vect_model_load_cost: strided group_size = %d .\n",
1091 group_size);
1092 }
1093
1094 /* The loads themselves. */
1095 if (STMT_VINFO_STRIDED_P (stmt_info)
1096 && !STMT_VINFO_GROUPED_ACCESS (stmt_info))
1097 {
1098 /* N scalar loads plus gathering them into a vector. */
1099 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1100 inside_cost += record_stmt_cost (body_cost_vec,
1101 ncopies * TYPE_VECTOR_SUBPARTS (vectype),
1102 scalar_load, stmt_info, 0, vect_body);
1103 }
1104 else
1105 vect_get_load_cost (first_dr, ncopies,
1106 ((!STMT_VINFO_GROUPED_ACCESS (stmt_info))
1107 || group_size > 1 || slp_node),
1108 &inside_cost, &prologue_cost,
1109 prologue_cost_vec, body_cost_vec, true);
1110 if (STMT_VINFO_STRIDED_P (stmt_info))
1111 inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_construct,
1112 stmt_info, 0, vect_body);
1113
1114 if (dump_enabled_p ())
1115 dump_printf_loc (MSG_NOTE, vect_location,
1116 "vect_model_load_cost: inside_cost = %d, "
1117 "prologue_cost = %d .\n", inside_cost, prologue_cost);
1118 }
1119
1120
1121 /* Calculate cost of DR's memory access. */
1122 void
1123 vect_get_load_cost (struct data_reference *dr, int ncopies,
1124 bool add_realign_cost, unsigned int *inside_cost,
1125 unsigned int *prologue_cost,
1126 stmt_vector_for_cost *prologue_cost_vec,
1127 stmt_vector_for_cost *body_cost_vec,
1128 bool record_prologue_costs)
1129 {
1130 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1131 gimple *stmt = DR_STMT (dr);
1132 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1133
1134 switch (alignment_support_scheme)
1135 {
1136 case dr_aligned:
1137 {
1138 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1139 stmt_info, 0, vect_body);
1140
1141 if (dump_enabled_p ())
1142 dump_printf_loc (MSG_NOTE, vect_location,
1143 "vect_model_load_cost: aligned.\n");
1144
1145 break;
1146 }
1147 case dr_unaligned_supported:
1148 {
1149 /* Here, we assign an additional cost for the unaligned load. */
1150 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1151 unaligned_load, stmt_info,
1152 DR_MISALIGNMENT (dr), vect_body);
1153
1154 if (dump_enabled_p ())
1155 dump_printf_loc (MSG_NOTE, vect_location,
1156 "vect_model_load_cost: unaligned supported by "
1157 "hardware.\n");
1158
1159 break;
1160 }
1161 case dr_explicit_realign:
1162 {
1163 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1164 vector_load, stmt_info, 0, vect_body);
1165 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1166 vec_perm, stmt_info, 0, vect_body);
1167
1168 /* FIXME: If the misalignment remains fixed across the iterations of
1169 the containing loop, the following cost should be added to the
1170 prologue costs. */
1171 if (targetm.vectorize.builtin_mask_for_load)
1172 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1173 stmt_info, 0, vect_body);
1174
1175 if (dump_enabled_p ())
1176 dump_printf_loc (MSG_NOTE, vect_location,
1177 "vect_model_load_cost: explicit realign\n");
1178
1179 break;
1180 }
1181 case dr_explicit_realign_optimized:
1182 {
1183 if (dump_enabled_p ())
1184 dump_printf_loc (MSG_NOTE, vect_location,
1185 "vect_model_load_cost: unaligned software "
1186 "pipelined.\n");
1187
1188 /* Unaligned software pipeline has a load of an address, an initial
1189 load, and possibly a mask operation to "prime" the loop. However,
1190 if this is an access in a group of loads, which provide grouped
1191 access, then the above cost should only be considered for one
1192 access in the group. Inside the loop, there is a load op
1193 and a realignment op. */
1194
1195 if (add_realign_cost && record_prologue_costs)
1196 {
1197 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1198 vector_stmt, stmt_info,
1199 0, vect_prologue);
1200 if (targetm.vectorize.builtin_mask_for_load)
1201 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1202 vector_stmt, stmt_info,
1203 0, vect_prologue);
1204 }
1205
1206 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1207 stmt_info, 0, vect_body);
1208 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1209 stmt_info, 0, vect_body);
1210
1211 if (dump_enabled_p ())
1212 dump_printf_loc (MSG_NOTE, vect_location,
1213 "vect_model_load_cost: explicit realign optimized"
1214 "\n");
1215
1216 break;
1217 }
1218
1219 case dr_unaligned_unsupported:
1220 {
1221 *inside_cost = VECT_MAX_COST;
1222
1223 if (dump_enabled_p ())
1224 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1225 "vect_model_load_cost: unsupported access.\n");
1226 break;
1227 }
1228
1229 default:
1230 gcc_unreachable ();
1231 }
1232 }
1233
1234 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1235 the loop preheader for the vectorized stmt STMT. */
1236
1237 static void
1238 vect_init_vector_1 (gimple *stmt, gimple *new_stmt, gimple_stmt_iterator *gsi)
1239 {
1240 if (gsi)
1241 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1242 else
1243 {
1244 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1245 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1246
1247 if (loop_vinfo)
1248 {
1249 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1250 basic_block new_bb;
1251 edge pe;
1252
1253 if (nested_in_vect_loop_p (loop, stmt))
1254 loop = loop->inner;
1255
1256 pe = loop_preheader_edge (loop);
1257 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
1258 gcc_assert (!new_bb);
1259 }
1260 else
1261 {
1262 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1263 basic_block bb;
1264 gimple_stmt_iterator gsi_bb_start;
1265
1266 gcc_assert (bb_vinfo);
1267 bb = BB_VINFO_BB (bb_vinfo);
1268 gsi_bb_start = gsi_after_labels (bb);
1269 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
1270 }
1271 }
1272
1273 if (dump_enabled_p ())
1274 {
1275 dump_printf_loc (MSG_NOTE, vect_location,
1276 "created new init_stmt: ");
1277 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
1278 }
1279 }
1280
1281 /* Function vect_init_vector.
1282
1283 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1284 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1285 vector type a vector with all elements equal to VAL is created first.
1286 Place the initialization at BSI if it is not NULL. Otherwise, place the
1287 initialization at the loop preheader.
1288 Return the DEF of INIT_STMT.
1289 It will be used in the vectorization of STMT. */
1290
1291 tree
1292 vect_init_vector (gimple *stmt, tree val, tree type, gimple_stmt_iterator *gsi)
1293 {
1294 gimple *init_stmt;
1295 tree new_temp;
1296
1297 if (TREE_CODE (type) == VECTOR_TYPE
1298 && TREE_CODE (TREE_TYPE (val)) != VECTOR_TYPE)
1299 {
1300 if (!types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1301 {
1302 if (CONSTANT_CLASS_P (val))
1303 val = fold_convert (TREE_TYPE (type), val);
1304 else
1305 {
1306 new_temp = make_ssa_name (TREE_TYPE (type));
1307 init_stmt = gimple_build_assign (new_temp, NOP_EXPR, val);
1308 vect_init_vector_1 (stmt, init_stmt, gsi);
1309 val = new_temp;
1310 }
1311 }
1312 val = build_vector_from_val (type, val);
1313 }
1314
1315 new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1316 init_stmt = gimple_build_assign (new_temp, val);
1317 vect_init_vector_1 (stmt, init_stmt, gsi);
1318 return new_temp;
1319 }
1320
1321
1322 /* Function vect_get_vec_def_for_operand.
1323
1324 OP is an operand in STMT. This function returns a (vector) def that will be
1325 used in the vectorized stmt for STMT.
1326
1327 In the case that OP is an SSA_NAME which is defined in the loop, then
1328 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1329
1330 In case OP is an invariant or constant, a new stmt that creates a vector def
1331 needs to be introduced. VECTYPE may be used to specify a required type for
1332 vector invariant. */
1333
1334 tree
1335 vect_get_vec_def_for_operand (tree op, gimple *stmt, tree vectype)
1336 {
1337 tree vec_oprnd;
1338 gimple *vec_stmt;
1339 gimple *def_stmt;
1340 stmt_vec_info def_stmt_info = NULL;
1341 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1342 tree stmt_vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1343 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1344 enum vect_def_type dt;
1345 bool is_simple_use;
1346 tree vector_type;
1347
1348 if (dump_enabled_p ())
1349 {
1350 dump_printf_loc (MSG_NOTE, vect_location,
1351 "vect_get_vec_def_for_operand: ");
1352 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1353 dump_printf (MSG_NOTE, "\n");
1354 }
1355
1356 is_simple_use = vect_is_simple_use (op, loop_vinfo, &def_stmt, &dt);
1357 gcc_assert (is_simple_use);
1358 if (dump_enabled_p ())
1359 {
1360 int loc_printed = 0;
1361 if (def_stmt)
1362 {
1363 if (loc_printed)
1364 dump_printf (MSG_NOTE, " def_stmt = ");
1365 else
1366 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1367 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1368 }
1369 }
1370
1371 switch (dt)
1372 {
1373 /* operand is a constant or a loop invariant. */
1374 case vect_constant_def:
1375 case vect_external_def:
1376 {
1377 if (vectype)
1378 vector_type = vectype;
1379 else if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE
1380 && VECTOR_BOOLEAN_TYPE_P (stmt_vectype))
1381 vector_type = build_same_sized_truth_vector_type (stmt_vectype);
1382 else
1383 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1384
1385 gcc_assert (vector_type);
1386 return vect_init_vector (stmt, op, vector_type, NULL);
1387 }
1388
1389 /* operand is defined inside the loop. */
1390 case vect_internal_def:
1391 {
1392 /* Get the def from the vectorized stmt. */
1393 def_stmt_info = vinfo_for_stmt (def_stmt);
1394
1395 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1396 /* Get vectorized pattern statement. */
1397 if (!vec_stmt
1398 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1399 && !STMT_VINFO_RELEVANT (def_stmt_info))
1400 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1401 STMT_VINFO_RELATED_STMT (def_stmt_info)));
1402 gcc_assert (vec_stmt);
1403 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1404 vec_oprnd = PHI_RESULT (vec_stmt);
1405 else if (is_gimple_call (vec_stmt))
1406 vec_oprnd = gimple_call_lhs (vec_stmt);
1407 else
1408 vec_oprnd = gimple_assign_lhs (vec_stmt);
1409 return vec_oprnd;
1410 }
1411
1412 /* operand is defined by a loop header phi - reduction */
1413 case vect_reduction_def:
1414 case vect_double_reduction_def:
1415 case vect_nested_cycle:
1416 /* Code should use get_initial_def_for_reduction. */
1417 gcc_unreachable ();
1418
1419 /* operand is defined by loop-header phi - induction. */
1420 case vect_induction_def:
1421 {
1422 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1423
1424 /* Get the def from the vectorized stmt. */
1425 def_stmt_info = vinfo_for_stmt (def_stmt);
1426 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
1427 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1428 vec_oprnd = PHI_RESULT (vec_stmt);
1429 else
1430 vec_oprnd = gimple_get_lhs (vec_stmt);
1431 return vec_oprnd;
1432 }
1433
1434 default:
1435 gcc_unreachable ();
1436 }
1437 }
1438
1439
1440 /* Function vect_get_vec_def_for_stmt_copy
1441
1442 Return a vector-def for an operand. This function is used when the
1443 vectorized stmt to be created (by the caller to this function) is a "copy"
1444 created in case the vectorized result cannot fit in one vector, and several
1445 copies of the vector-stmt are required. In this case the vector-def is
1446 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
1447 of the stmt that defines VEC_OPRND.
1448 DT is the type of the vector def VEC_OPRND.
1449
1450 Context:
1451 In case the vectorization factor (VF) is bigger than the number
1452 of elements that can fit in a vectype (nunits), we have to generate
1453 more than one vector stmt to vectorize the scalar stmt. This situation
1454 arises when there are multiple data-types operated upon in the loop; the
1455 smallest data-type determines the VF, and as a result, when vectorizing
1456 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1457 vector stmt (each computing a vector of 'nunits' results, and together
1458 computing 'VF' results in each iteration). This function is called when
1459 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1460 which VF=16 and nunits=4, so the number of copies required is 4):
1461
1462 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
1463
1464 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1465 VS1.1: vx.1 = memref1 VS1.2
1466 VS1.2: vx.2 = memref2 VS1.3
1467 VS1.3: vx.3 = memref3
1468
1469 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1470 VSnew.1: vz1 = vx.1 + ... VSnew.2
1471 VSnew.2: vz2 = vx.2 + ... VSnew.3
1472 VSnew.3: vz3 = vx.3 + ...
1473
1474 The vectorization of S1 is explained in vectorizable_load.
1475 The vectorization of S2:
1476 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1477 the function 'vect_get_vec_def_for_operand' is called to
1478 get the relevant vector-def for each operand of S2. For operand x it
1479 returns the vector-def 'vx.0'.
1480
1481 To create the remaining copies of the vector-stmt (VSnew.j), this
1482 function is called to get the relevant vector-def for each operand. It is
1483 obtained from the respective VS1.j stmt, which is recorded in the
1484 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1485
1486 For example, to obtain the vector-def 'vx.1' in order to create the
1487 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1488 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
1489 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1490 and return its def ('vx.1').
1491 Overall, to create the above sequence this function will be called 3 times:
1492 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1493 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1494 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1495
1496 tree
1497 vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1498 {
1499 gimple *vec_stmt_for_operand;
1500 stmt_vec_info def_stmt_info;
1501
1502 /* Do nothing; can reuse same def. */
1503 if (dt == vect_external_def || dt == vect_constant_def )
1504 return vec_oprnd;
1505
1506 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1507 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1508 gcc_assert (def_stmt_info);
1509 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1510 gcc_assert (vec_stmt_for_operand);
1511 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1512 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1513 else
1514 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1515 return vec_oprnd;
1516 }
1517
1518
1519 /* Get vectorized definitions for the operands to create a copy of an original
1520 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
1521
1522 static void
1523 vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
1524 vec<tree> *vec_oprnds0,
1525 vec<tree> *vec_oprnds1)
1526 {
1527 tree vec_oprnd = vec_oprnds0->pop ();
1528
1529 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
1530 vec_oprnds0->quick_push (vec_oprnd);
1531
1532 if (vec_oprnds1 && vec_oprnds1->length ())
1533 {
1534 vec_oprnd = vec_oprnds1->pop ();
1535 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
1536 vec_oprnds1->quick_push (vec_oprnd);
1537 }
1538 }
1539
1540
1541 /* Get vectorized definitions for OP0 and OP1.
1542 REDUC_INDEX is the index of reduction operand in case of reduction,
1543 and -1 otherwise. */
1544
1545 void
1546 vect_get_vec_defs (tree op0, tree op1, gimple *stmt,
1547 vec<tree> *vec_oprnds0,
1548 vec<tree> *vec_oprnds1,
1549 slp_tree slp_node, int reduc_index)
1550 {
1551 if (slp_node)
1552 {
1553 int nops = (op1 == NULL_TREE) ? 1 : 2;
1554 auto_vec<tree> ops (nops);
1555 auto_vec<vec<tree> > vec_defs (nops);
1556
1557 ops.quick_push (op0);
1558 if (op1)
1559 ops.quick_push (op1);
1560
1561 vect_get_slp_defs (ops, slp_node, &vec_defs, reduc_index);
1562
1563 *vec_oprnds0 = vec_defs[0];
1564 if (op1)
1565 *vec_oprnds1 = vec_defs[1];
1566 }
1567 else
1568 {
1569 tree vec_oprnd;
1570
1571 vec_oprnds0->create (1);
1572 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt);
1573 vec_oprnds0->quick_push (vec_oprnd);
1574
1575 if (op1)
1576 {
1577 vec_oprnds1->create (1);
1578 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt);
1579 vec_oprnds1->quick_push (vec_oprnd);
1580 }
1581 }
1582 }
1583
1584
1585 /* Function vect_finish_stmt_generation.
1586
1587 Insert a new stmt. */
1588
1589 void
1590 vect_finish_stmt_generation (gimple *stmt, gimple *vec_stmt,
1591 gimple_stmt_iterator *gsi)
1592 {
1593 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1594 vec_info *vinfo = stmt_info->vinfo;
1595
1596 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1597
1598 if (!gsi_end_p (*gsi)
1599 && gimple_has_mem_ops (vec_stmt))
1600 {
1601 gimple *at_stmt = gsi_stmt (*gsi);
1602 tree vuse = gimple_vuse (at_stmt);
1603 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1604 {
1605 tree vdef = gimple_vdef (at_stmt);
1606 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1607 /* If we have an SSA vuse and insert a store, update virtual
1608 SSA form to avoid triggering the renamer. Do so only
1609 if we can easily see all uses - which is what almost always
1610 happens with the way vectorized stmts are inserted. */
1611 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1612 && ((is_gimple_assign (vec_stmt)
1613 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1614 || (is_gimple_call (vec_stmt)
1615 && !(gimple_call_flags (vec_stmt)
1616 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1617 {
1618 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1619 gimple_set_vdef (vec_stmt, new_vdef);
1620 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1621 }
1622 }
1623 }
1624 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1625
1626 set_vinfo_for_stmt (vec_stmt, new_stmt_vec_info (vec_stmt, vinfo));
1627
1628 if (dump_enabled_p ())
1629 {
1630 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1631 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1632 }
1633
1634 gimple_set_location (vec_stmt, gimple_location (stmt));
1635
1636 /* While EH edges will generally prevent vectorization, stmt might
1637 e.g. be in a must-not-throw region. Ensure newly created stmts
1638 that could throw are part of the same region. */
1639 int lp_nr = lookup_stmt_eh_lp (stmt);
1640 if (lp_nr != 0 && stmt_could_throw_p (vec_stmt))
1641 add_stmt_to_eh_lp (vec_stmt, lp_nr);
1642 }
1643
1644 /* Checks if CALL can be vectorized in type VECTYPE. Returns
1645 a function declaration if the target has a vectorized version
1646 of the function, or NULL_TREE if the function cannot be vectorized. */
1647
1648 tree
1649 vectorizable_function (gcall *call, tree vectype_out, tree vectype_in)
1650 {
1651 tree fndecl = gimple_call_fndecl (call);
1652
1653 /* We only handle functions that do not read or clobber memory -- i.e.
1654 const or novops ones. */
1655 if (!(gimple_call_flags (call) & (ECF_CONST | ECF_NOVOPS)))
1656 return NULL_TREE;
1657
1658 if (!fndecl
1659 || TREE_CODE (fndecl) != FUNCTION_DECL
1660 || !DECL_BUILT_IN (fndecl))
1661 return NULL_TREE;
1662
1663 return targetm.vectorize.builtin_vectorized_function (fndecl, vectype_out,
1664 vectype_in);
1665 }
1666
1667
1668 static tree permute_vec_elements (tree, tree, tree, gimple *,
1669 gimple_stmt_iterator *);
1670
1671
1672 /* Function vectorizable_mask_load_store.
1673
1674 Check if STMT performs a conditional load or store that can be vectorized.
1675 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
1676 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
1677 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
1678
1679 static bool
1680 vectorizable_mask_load_store (gimple *stmt, gimple_stmt_iterator *gsi,
1681 gimple **vec_stmt, slp_tree slp_node)
1682 {
1683 tree vec_dest = NULL;
1684 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1685 stmt_vec_info prev_stmt_info;
1686 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1687 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1688 bool nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
1689 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1690 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1691 tree mask_vectype;
1692 tree elem_type;
1693 gimple *new_stmt;
1694 tree dummy;
1695 tree dataref_ptr = NULL_TREE;
1696 gimple *ptr_incr;
1697 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
1698 int ncopies;
1699 int i, j;
1700 bool inv_p;
1701 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
1702 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
1703 int gather_scale = 1;
1704 enum vect_def_type gather_dt = vect_unknown_def_type;
1705 bool is_store;
1706 tree mask;
1707 gimple *def_stmt;
1708 enum vect_def_type dt;
1709
1710 if (slp_node != NULL)
1711 return false;
1712
1713 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
1714 gcc_assert (ncopies >= 1);
1715
1716 is_store = gimple_call_internal_fn (stmt) == IFN_MASK_STORE;
1717 mask = gimple_call_arg (stmt, 2);
1718
1719 if (TREE_CODE (TREE_TYPE (mask)) != BOOLEAN_TYPE)
1720 return false;
1721
1722 /* FORNOW. This restriction should be relaxed. */
1723 if (nested_in_vect_loop && ncopies > 1)
1724 {
1725 if (dump_enabled_p ())
1726 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1727 "multiple types in nested loop.");
1728 return false;
1729 }
1730
1731 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1732 return false;
1733
1734 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
1735 return false;
1736
1737 if (!STMT_VINFO_DATA_REF (stmt_info))
1738 return false;
1739
1740 elem_type = TREE_TYPE (vectype);
1741
1742 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1743 return false;
1744
1745 if (STMT_VINFO_STRIDED_P (stmt_info))
1746 return false;
1747
1748 if (TREE_CODE (mask) != SSA_NAME)
1749 return false;
1750
1751 if (!vect_is_simple_use (mask, loop_vinfo, &def_stmt, &dt, &mask_vectype))
1752 return false;
1753
1754 if (!mask_vectype)
1755 mask_vectype = get_mask_type_for_scalar_type (TREE_TYPE (vectype));
1756
1757 if (!mask_vectype)
1758 return false;
1759
1760 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1761 {
1762 gimple *def_stmt;
1763 gather_decl = vect_check_gather_scatter (stmt, loop_vinfo, &gather_base,
1764 &gather_off, &gather_scale);
1765 gcc_assert (gather_decl);
1766 if (!vect_is_simple_use (gather_off, loop_vinfo, &def_stmt, &gather_dt,
1767 &gather_off_vectype))
1768 {
1769 if (dump_enabled_p ())
1770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1771 "gather index use not simple.");
1772 return false;
1773 }
1774
1775 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
1776 tree masktype
1777 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
1778 if (TREE_CODE (masktype) == INTEGER_TYPE)
1779 {
1780 if (dump_enabled_p ())
1781 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1782 "masked gather with integer mask not supported.");
1783 return false;
1784 }
1785 }
1786 else if (tree_int_cst_compare (nested_in_vect_loop
1787 ? STMT_VINFO_DR_STEP (stmt_info)
1788 : DR_STEP (dr), size_zero_node) <= 0)
1789 return false;
1790 else if (!VECTOR_MODE_P (TYPE_MODE (vectype))
1791 || !can_vec_mask_load_store_p (TYPE_MODE (vectype),
1792 TYPE_MODE (mask_vectype),
1793 !is_store))
1794 return false;
1795
1796 if (is_store)
1797 {
1798 tree rhs = gimple_call_arg (stmt, 3);
1799 if (!vect_is_simple_use (rhs, loop_vinfo, &def_stmt, &dt))
1800 return false;
1801 }
1802
1803 if (!vec_stmt) /* transformation not required. */
1804 {
1805 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
1806 if (is_store)
1807 vect_model_store_cost (stmt_info, ncopies, false, dt,
1808 NULL, NULL, NULL);
1809 else
1810 vect_model_load_cost (stmt_info, ncopies, false, NULL, NULL, NULL);
1811 return true;
1812 }
1813
1814 /** Transform. **/
1815
1816 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
1817 {
1818 tree vec_oprnd0 = NULL_TREE, op;
1819 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
1820 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
1821 tree ptr, vec_mask = NULL_TREE, mask_op = NULL_TREE, var, scale;
1822 tree perm_mask = NULL_TREE, prev_res = NULL_TREE;
1823 tree mask_perm_mask = NULL_TREE;
1824 edge pe = loop_preheader_edge (loop);
1825 gimple_seq seq;
1826 basic_block new_bb;
1827 enum { NARROW, NONE, WIDEN } modifier;
1828 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
1829
1830 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
1831 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1832 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1833 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1834 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
1835 scaletype = TREE_VALUE (arglist);
1836 gcc_checking_assert (types_compatible_p (srctype, rettype)
1837 && types_compatible_p (srctype, masktype));
1838
1839 if (nunits == gather_off_nunits)
1840 modifier = NONE;
1841 else if (nunits == gather_off_nunits / 2)
1842 {
1843 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
1844 modifier = WIDEN;
1845
1846 for (i = 0; i < gather_off_nunits; ++i)
1847 sel[i] = i | nunits;
1848
1849 perm_mask = vect_gen_perm_mask_checked (gather_off_vectype, sel);
1850 }
1851 else if (nunits == gather_off_nunits * 2)
1852 {
1853 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
1854 modifier = NARROW;
1855
1856 for (i = 0; i < nunits; ++i)
1857 sel[i] = i < gather_off_nunits
1858 ? i : i + nunits - gather_off_nunits;
1859
1860 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
1861 ncopies *= 2;
1862 for (i = 0; i < nunits; ++i)
1863 sel[i] = i | gather_off_nunits;
1864 mask_perm_mask = vect_gen_perm_mask_checked (masktype, sel);
1865 }
1866 else
1867 gcc_unreachable ();
1868
1869 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
1870
1871 ptr = fold_convert (ptrtype, gather_base);
1872 if (!is_gimple_min_invariant (ptr))
1873 {
1874 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
1875 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
1876 gcc_assert (!new_bb);
1877 }
1878
1879 scale = build_int_cst (scaletype, gather_scale);
1880
1881 prev_stmt_info = NULL;
1882 for (j = 0; j < ncopies; ++j)
1883 {
1884 if (modifier == WIDEN && (j & 1))
1885 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
1886 perm_mask, stmt, gsi);
1887 else if (j == 0)
1888 op = vec_oprnd0
1889 = vect_get_vec_def_for_operand (gather_off, stmt);
1890 else
1891 op = vec_oprnd0
1892 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
1893
1894 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
1895 {
1896 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
1897 == TYPE_VECTOR_SUBPARTS (idxtype));
1898 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
1899 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
1900 new_stmt
1901 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
1902 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1903 op = var;
1904 }
1905
1906 if (mask_perm_mask && (j & 1))
1907 mask_op = permute_vec_elements (mask_op, mask_op,
1908 mask_perm_mask, stmt, gsi);
1909 else
1910 {
1911 if (j == 0)
1912 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
1913 else
1914 {
1915 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
1916 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
1917 }
1918
1919 mask_op = vec_mask;
1920 if (!useless_type_conversion_p (masktype, TREE_TYPE (vec_mask)))
1921 {
1922 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask_op))
1923 == TYPE_VECTOR_SUBPARTS (masktype));
1924 var = vect_get_new_ssa_name (masktype, vect_simple_var);
1925 mask_op = build1 (VIEW_CONVERT_EXPR, masktype, mask_op);
1926 new_stmt
1927 = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_op);
1928 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1929 mask_op = var;
1930 }
1931 }
1932
1933 new_stmt
1934 = gimple_build_call (gather_decl, 5, mask_op, ptr, op, mask_op,
1935 scale);
1936
1937 if (!useless_type_conversion_p (vectype, rettype))
1938 {
1939 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
1940 == TYPE_VECTOR_SUBPARTS (rettype));
1941 op = vect_get_new_ssa_name (rettype, vect_simple_var);
1942 gimple_call_set_lhs (new_stmt, op);
1943 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1944 var = make_ssa_name (vec_dest);
1945 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
1946 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
1947 }
1948 else
1949 {
1950 var = make_ssa_name (vec_dest, new_stmt);
1951 gimple_call_set_lhs (new_stmt, var);
1952 }
1953
1954 vect_finish_stmt_generation (stmt, new_stmt, gsi);
1955
1956 if (modifier == NARROW)
1957 {
1958 if ((j & 1) == 0)
1959 {
1960 prev_res = var;
1961 continue;
1962 }
1963 var = permute_vec_elements (prev_res, var,
1964 perm_mask, stmt, gsi);
1965 new_stmt = SSA_NAME_DEF_STMT (var);
1966 }
1967
1968 if (prev_stmt_info == NULL)
1969 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
1970 else
1971 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
1972 prev_stmt_info = vinfo_for_stmt (new_stmt);
1973 }
1974
1975 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
1976 from the IL. */
1977 if (STMT_VINFO_RELATED_STMT (stmt_info))
1978 {
1979 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
1980 stmt_info = vinfo_for_stmt (stmt);
1981 }
1982 tree lhs = gimple_call_lhs (stmt);
1983 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
1984 set_vinfo_for_stmt (new_stmt, stmt_info);
1985 set_vinfo_for_stmt (stmt, NULL);
1986 STMT_VINFO_STMT (stmt_info) = new_stmt;
1987 gsi_replace (gsi, new_stmt, true);
1988 return true;
1989 }
1990 else if (is_store)
1991 {
1992 tree vec_rhs = NULL_TREE, vec_mask = NULL_TREE;
1993 prev_stmt_info = NULL;
1994 for (i = 0; i < ncopies; i++)
1995 {
1996 unsigned align, misalign;
1997
1998 if (i == 0)
1999 {
2000 tree rhs = gimple_call_arg (stmt, 3);
2001 vec_rhs = vect_get_vec_def_for_operand (rhs, stmt);
2002 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2003 /* We should have catched mismatched types earlier. */
2004 gcc_assert (useless_type_conversion_p (vectype,
2005 TREE_TYPE (vec_rhs)));
2006 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2007 NULL_TREE, &dummy, gsi,
2008 &ptr_incr, false, &inv_p);
2009 gcc_assert (!inv_p);
2010 }
2011 else
2012 {
2013 vect_is_simple_use (vec_rhs, loop_vinfo, &def_stmt, &dt);
2014 vec_rhs = vect_get_vec_def_for_stmt_copy (dt, vec_rhs);
2015 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
2016 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2017 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2018 TYPE_SIZE_UNIT (vectype));
2019 }
2020
2021 align = TYPE_ALIGN_UNIT (vectype);
2022 if (aligned_access_p (dr))
2023 misalign = 0;
2024 else if (DR_MISALIGNMENT (dr) == -1)
2025 {
2026 align = TYPE_ALIGN_UNIT (elem_type);
2027 misalign = 0;
2028 }
2029 else
2030 misalign = DR_MISALIGNMENT (dr);
2031 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2032 misalign);
2033 new_stmt
2034 = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
2035 gimple_call_arg (stmt, 1),
2036 vec_mask, vec_rhs);
2037 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2038 if (i == 0)
2039 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2040 else
2041 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2042 prev_stmt_info = vinfo_for_stmt (new_stmt);
2043 }
2044 }
2045 else
2046 {
2047 tree vec_mask = NULL_TREE;
2048 prev_stmt_info = NULL;
2049 vec_dest = vect_create_destination_var (gimple_call_lhs (stmt), vectype);
2050 for (i = 0; i < ncopies; i++)
2051 {
2052 unsigned align, misalign;
2053
2054 if (i == 0)
2055 {
2056 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2057 dataref_ptr = vect_create_data_ref_ptr (stmt, vectype, NULL,
2058 NULL_TREE, &dummy, gsi,
2059 &ptr_incr, false, &inv_p);
2060 gcc_assert (!inv_p);
2061 }
2062 else
2063 {
2064 vect_is_simple_use (vec_mask, loop_vinfo, &def_stmt, &dt);
2065 vec_mask = vect_get_vec_def_for_stmt_copy (dt, vec_mask);
2066 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
2067 TYPE_SIZE_UNIT (vectype));
2068 }
2069
2070 align = TYPE_ALIGN_UNIT (vectype);
2071 if (aligned_access_p (dr))
2072 misalign = 0;
2073 else if (DR_MISALIGNMENT (dr) == -1)
2074 {
2075 align = TYPE_ALIGN_UNIT (elem_type);
2076 misalign = 0;
2077 }
2078 else
2079 misalign = DR_MISALIGNMENT (dr);
2080 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
2081 misalign);
2082 new_stmt
2083 = gimple_build_call_internal (IFN_MASK_LOAD, 3, dataref_ptr,
2084 gimple_call_arg (stmt, 1),
2085 vec_mask);
2086 gimple_call_set_lhs (new_stmt, make_ssa_name (vec_dest));
2087 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2088 if (i == 0)
2089 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2090 else
2091 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2092 prev_stmt_info = vinfo_for_stmt (new_stmt);
2093 }
2094 }
2095
2096 if (!is_store)
2097 {
2098 /* Ensure that even with -fno-tree-dce the scalar MASK_LOAD is removed
2099 from the IL. */
2100 if (STMT_VINFO_RELATED_STMT (stmt_info))
2101 {
2102 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
2103 stmt_info = vinfo_for_stmt (stmt);
2104 }
2105 tree lhs = gimple_call_lhs (stmt);
2106 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2107 set_vinfo_for_stmt (new_stmt, stmt_info);
2108 set_vinfo_for_stmt (stmt, NULL);
2109 STMT_VINFO_STMT (stmt_info) = new_stmt;
2110 gsi_replace (gsi, new_stmt, true);
2111 }
2112
2113 return true;
2114 }
2115
2116
2117 /* Function vectorizable_call.
2118
2119 Check if GS performs a function call that can be vectorized.
2120 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2121 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2122 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2123
2124 static bool
2125 vectorizable_call (gimple *gs, gimple_stmt_iterator *gsi, gimple **vec_stmt,
2126 slp_tree slp_node)
2127 {
2128 gcall *stmt;
2129 tree vec_dest;
2130 tree scalar_dest;
2131 tree op, type;
2132 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
2133 stmt_vec_info stmt_info = vinfo_for_stmt (gs), prev_stmt_info;
2134 tree vectype_out, vectype_in;
2135 int nunits_in;
2136 int nunits_out;
2137 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2138 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2139 vec_info *vinfo = stmt_info->vinfo;
2140 tree fndecl, new_temp, rhs_type;
2141 gimple *def_stmt;
2142 enum vect_def_type dt[3]
2143 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
2144 gimple *new_stmt = NULL;
2145 int ncopies, j;
2146 vec<tree> vargs = vNULL;
2147 enum { NARROW, NONE, WIDEN } modifier;
2148 size_t i, nargs;
2149 tree lhs;
2150
2151 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2152 return false;
2153
2154 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2155 return false;
2156
2157 /* Is GS a vectorizable call? */
2158 stmt = dyn_cast <gcall *> (gs);
2159 if (!stmt)
2160 return false;
2161
2162 if (gimple_call_internal_p (stmt)
2163 && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
2164 || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
2165 return vectorizable_mask_load_store (stmt, gsi, vec_stmt,
2166 slp_node);
2167
2168 if (gimple_call_lhs (stmt) == NULL_TREE
2169 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2170 return false;
2171
2172 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2173
2174 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2175
2176 /* Process function arguments. */
2177 rhs_type = NULL_TREE;
2178 vectype_in = NULL_TREE;
2179 nargs = gimple_call_num_args (stmt);
2180
2181 /* Bail out if the function has more than three arguments, we do not have
2182 interesting builtin functions to vectorize with more than two arguments
2183 except for fma. No arguments is also not good. */
2184 if (nargs == 0 || nargs > 3)
2185 return false;
2186
2187 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
2188 if (gimple_call_internal_p (stmt)
2189 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2190 {
2191 nargs = 0;
2192 rhs_type = unsigned_type_node;
2193 }
2194
2195 for (i = 0; i < nargs; i++)
2196 {
2197 tree opvectype;
2198
2199 op = gimple_call_arg (stmt, i);
2200
2201 /* We can only handle calls with arguments of the same type. */
2202 if (rhs_type
2203 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
2204 {
2205 if (dump_enabled_p ())
2206 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2207 "argument types differ.\n");
2208 return false;
2209 }
2210 if (!rhs_type)
2211 rhs_type = TREE_TYPE (op);
2212
2213 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt[i], &opvectype))
2214 {
2215 if (dump_enabled_p ())
2216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2217 "use not simple.\n");
2218 return false;
2219 }
2220
2221 if (!vectype_in)
2222 vectype_in = opvectype;
2223 else if (opvectype
2224 && opvectype != vectype_in)
2225 {
2226 if (dump_enabled_p ())
2227 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2228 "argument vector types differ.\n");
2229 return false;
2230 }
2231 }
2232 /* If all arguments are external or constant defs use a vector type with
2233 the same size as the output vector type. */
2234 if (!vectype_in)
2235 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
2236 if (vec_stmt)
2237 gcc_assert (vectype_in);
2238 if (!vectype_in)
2239 {
2240 if (dump_enabled_p ())
2241 {
2242 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2243 "no vectype for scalar type ");
2244 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
2245 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2246 }
2247
2248 return false;
2249 }
2250
2251 /* FORNOW */
2252 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
2253 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
2254 if (nunits_in == nunits_out / 2)
2255 modifier = NARROW;
2256 else if (nunits_out == nunits_in)
2257 modifier = NONE;
2258 else if (nunits_out == nunits_in / 2)
2259 modifier = WIDEN;
2260 else
2261 return false;
2262
2263 /* For now, we only vectorize functions if a target specific builtin
2264 is available. TODO -- in some cases, it might be profitable to
2265 insert the calls for pieces of the vector, in order to be able
2266 to vectorize other operations in the loop. */
2267 fndecl = vectorizable_function (stmt, vectype_out, vectype_in);
2268 if (fndecl == NULL_TREE)
2269 {
2270 if (gimple_call_internal_p (stmt)
2271 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
2272 && !slp_node
2273 && loop_vinfo
2274 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2275 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
2276 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
2277 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
2278 {
2279 /* We can handle IFN_GOMP_SIMD_LANE by returning a
2280 { 0, 1, 2, ... vf - 1 } vector. */
2281 gcc_assert (nargs == 0);
2282 }
2283 else
2284 {
2285 if (dump_enabled_p ())
2286 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2287 "function is not vectorizable.\n");
2288 return false;
2289 }
2290 }
2291
2292 gcc_assert (!gimple_vuse (stmt));
2293
2294 if (slp_node || PURE_SLP_STMT (stmt_info))
2295 ncopies = 1;
2296 else if (modifier == NARROW)
2297 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
2298 else
2299 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
2300
2301 /* Sanity check: make sure that at least one copy of the vectorized stmt
2302 needs to be generated. */
2303 gcc_assert (ncopies >= 1);
2304
2305 if (!vec_stmt) /* transformation not required. */
2306 {
2307 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
2308 if (dump_enabled_p ())
2309 dump_printf_loc (MSG_NOTE, vect_location, "=== vectorizable_call ==="
2310 "\n");
2311 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
2312 return true;
2313 }
2314
2315 /** Transform. **/
2316
2317 if (dump_enabled_p ())
2318 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2319
2320 /* Handle def. */
2321 scalar_dest = gimple_call_lhs (stmt);
2322 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
2323
2324 prev_stmt_info = NULL;
2325 switch (modifier)
2326 {
2327 case NONE:
2328 for (j = 0; j < ncopies; ++j)
2329 {
2330 /* Build argument list for the vectorized call. */
2331 if (j == 0)
2332 vargs.create (nargs);
2333 else
2334 vargs.truncate (0);
2335
2336 if (slp_node)
2337 {
2338 auto_vec<vec<tree> > vec_defs (nargs);
2339 vec<tree> vec_oprnds0;
2340
2341 for (i = 0; i < nargs; i++)
2342 vargs.quick_push (gimple_call_arg (stmt, i));
2343 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2344 vec_oprnds0 = vec_defs[0];
2345
2346 /* Arguments are ready. Create the new vector stmt. */
2347 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
2348 {
2349 size_t k;
2350 for (k = 0; k < nargs; k++)
2351 {
2352 vec<tree> vec_oprndsk = vec_defs[k];
2353 vargs[k] = vec_oprndsk[i];
2354 }
2355 new_stmt = gimple_build_call_vec (fndecl, vargs);
2356 new_temp = make_ssa_name (vec_dest, new_stmt);
2357 gimple_call_set_lhs (new_stmt, new_temp);
2358 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2359 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2360 }
2361
2362 for (i = 0; i < nargs; i++)
2363 {
2364 vec<tree> vec_oprndsi = vec_defs[i];
2365 vec_oprndsi.release ();
2366 }
2367 continue;
2368 }
2369
2370 for (i = 0; i < nargs; i++)
2371 {
2372 op = gimple_call_arg (stmt, i);
2373 if (j == 0)
2374 vec_oprnd0
2375 = vect_get_vec_def_for_operand (op, stmt);
2376 else
2377 {
2378 vec_oprnd0 = gimple_call_arg (new_stmt, i);
2379 vec_oprnd0
2380 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2381 }
2382
2383 vargs.quick_push (vec_oprnd0);
2384 }
2385
2386 if (gimple_call_internal_p (stmt)
2387 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2388 {
2389 tree *v = XALLOCAVEC (tree, nunits_out);
2390 int k;
2391 for (k = 0; k < nunits_out; ++k)
2392 v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k);
2393 tree cst = build_vector (vectype_out, v);
2394 tree new_var
2395 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
2396 gimple *init_stmt = gimple_build_assign (new_var, cst);
2397 vect_init_vector_1 (stmt, init_stmt, NULL);
2398 new_temp = make_ssa_name (vec_dest);
2399 new_stmt = gimple_build_assign (new_temp, new_var);
2400 }
2401 else
2402 {
2403 new_stmt = gimple_build_call_vec (fndecl, vargs);
2404 new_temp = make_ssa_name (vec_dest, new_stmt);
2405 gimple_call_set_lhs (new_stmt, new_temp);
2406 }
2407 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2408
2409 if (j == 0)
2410 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2411 else
2412 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2413
2414 prev_stmt_info = vinfo_for_stmt (new_stmt);
2415 }
2416
2417 break;
2418
2419 case NARROW:
2420 for (j = 0; j < ncopies; ++j)
2421 {
2422 /* Build argument list for the vectorized call. */
2423 if (j == 0)
2424 vargs.create (nargs * 2);
2425 else
2426 vargs.truncate (0);
2427
2428 if (slp_node)
2429 {
2430 auto_vec<vec<tree> > vec_defs (nargs);
2431 vec<tree> vec_oprnds0;
2432
2433 for (i = 0; i < nargs; i++)
2434 vargs.quick_push (gimple_call_arg (stmt, i));
2435 vect_get_slp_defs (vargs, slp_node, &vec_defs, -1);
2436 vec_oprnds0 = vec_defs[0];
2437
2438 /* Arguments are ready. Create the new vector stmt. */
2439 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
2440 {
2441 size_t k;
2442 vargs.truncate (0);
2443 for (k = 0; k < nargs; k++)
2444 {
2445 vec<tree> vec_oprndsk = vec_defs[k];
2446 vargs.quick_push (vec_oprndsk[i]);
2447 vargs.quick_push (vec_oprndsk[i + 1]);
2448 }
2449 new_stmt = gimple_build_call_vec (fndecl, vargs);
2450 new_temp = make_ssa_name (vec_dest, new_stmt);
2451 gimple_call_set_lhs (new_stmt, new_temp);
2452 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2453 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
2454 }
2455
2456 for (i = 0; i < nargs; i++)
2457 {
2458 vec<tree> vec_oprndsi = vec_defs[i];
2459 vec_oprndsi.release ();
2460 }
2461 continue;
2462 }
2463
2464 for (i = 0; i < nargs; i++)
2465 {
2466 op = gimple_call_arg (stmt, i);
2467 if (j == 0)
2468 {
2469 vec_oprnd0
2470 = vect_get_vec_def_for_operand (op, stmt);
2471 vec_oprnd1
2472 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2473 }
2474 else
2475 {
2476 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
2477 vec_oprnd0
2478 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
2479 vec_oprnd1
2480 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
2481 }
2482
2483 vargs.quick_push (vec_oprnd0);
2484 vargs.quick_push (vec_oprnd1);
2485 }
2486
2487 new_stmt = gimple_build_call_vec (fndecl, vargs);
2488 new_temp = make_ssa_name (vec_dest, new_stmt);
2489 gimple_call_set_lhs (new_stmt, new_temp);
2490 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2491
2492 if (j == 0)
2493 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
2494 else
2495 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2496
2497 prev_stmt_info = vinfo_for_stmt (new_stmt);
2498 }
2499
2500 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
2501
2502 break;
2503
2504 case WIDEN:
2505 /* No current target implements this case. */
2506 return false;
2507 }
2508
2509 vargs.release ();
2510
2511 /* The call in STMT might prevent it from being removed in dce.
2512 We however cannot remove it here, due to the way the ssa name
2513 it defines is mapped to the new definition. So just replace
2514 rhs of the statement with something harmless. */
2515
2516 if (slp_node)
2517 return true;
2518
2519 type = TREE_TYPE (scalar_dest);
2520 if (is_pattern_stmt_p (stmt_info))
2521 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
2522 else
2523 lhs = gimple_call_lhs (stmt);
2524
2525 if (gimple_call_internal_p (stmt)
2526 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
2527 {
2528 /* Replace uses of the lhs of GOMP_SIMD_LANE call outside the loop
2529 with vf - 1 rather than 0, that is the last iteration of the
2530 vectorized loop. */
2531 imm_use_iterator iter;
2532 use_operand_p use_p;
2533 gimple *use_stmt;
2534 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2535 {
2536 basic_block use_bb = gimple_bb (use_stmt);
2537 if (use_bb
2538 && !flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo), use_bb))
2539 {
2540 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2541 SET_USE (use_p, build_int_cst (TREE_TYPE (lhs),
2542 ncopies * nunits_out - 1));
2543 update_stmt (use_stmt);
2544 }
2545 }
2546 }
2547
2548 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
2549 set_vinfo_for_stmt (new_stmt, stmt_info);
2550 set_vinfo_for_stmt (stmt, NULL);
2551 STMT_VINFO_STMT (stmt_info) = new_stmt;
2552 gsi_replace (gsi, new_stmt, false);
2553
2554 return true;
2555 }
2556
2557
2558 struct simd_call_arg_info
2559 {
2560 tree vectype;
2561 tree op;
2562 enum vect_def_type dt;
2563 HOST_WIDE_INT linear_step;
2564 unsigned int align;
2565 bool simd_lane_linear;
2566 };
2567
2568 /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
2569 is linear within simd lane (but not within whole loop), note it in
2570 *ARGINFO. */
2571
2572 static void
2573 vect_simd_lane_linear (tree op, struct loop *loop,
2574 struct simd_call_arg_info *arginfo)
2575 {
2576 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
2577
2578 if (!is_gimple_assign (def_stmt)
2579 || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
2580 || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
2581 return;
2582
2583 tree base = gimple_assign_rhs1 (def_stmt);
2584 HOST_WIDE_INT linear_step = 0;
2585 tree v = gimple_assign_rhs2 (def_stmt);
2586 while (TREE_CODE (v) == SSA_NAME)
2587 {
2588 tree t;
2589 def_stmt = SSA_NAME_DEF_STMT (v);
2590 if (is_gimple_assign (def_stmt))
2591 switch (gimple_assign_rhs_code (def_stmt))
2592 {
2593 case PLUS_EXPR:
2594 t = gimple_assign_rhs2 (def_stmt);
2595 if (linear_step || TREE_CODE (t) != INTEGER_CST)
2596 return;
2597 base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
2598 v = gimple_assign_rhs1 (def_stmt);
2599 continue;
2600 case MULT_EXPR:
2601 t = gimple_assign_rhs2 (def_stmt);
2602 if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
2603 return;
2604 linear_step = tree_to_shwi (t);
2605 v = gimple_assign_rhs1 (def_stmt);
2606 continue;
2607 CASE_CONVERT:
2608 t = gimple_assign_rhs1 (def_stmt);
2609 if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
2610 || (TYPE_PRECISION (TREE_TYPE (v))
2611 < TYPE_PRECISION (TREE_TYPE (t))))
2612 return;
2613 if (!linear_step)
2614 linear_step = 1;
2615 v = t;
2616 continue;
2617 default:
2618 return;
2619 }
2620 else if (is_gimple_call (def_stmt)
2621 && gimple_call_internal_p (def_stmt)
2622 && gimple_call_internal_fn (def_stmt) == IFN_GOMP_SIMD_LANE
2623 && loop->simduid
2624 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
2625 && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
2626 == loop->simduid))
2627 {
2628 if (!linear_step)
2629 linear_step = 1;
2630 arginfo->linear_step = linear_step;
2631 arginfo->op = base;
2632 arginfo->simd_lane_linear = true;
2633 return;
2634 }
2635 }
2636 }
2637
2638 /* Function vectorizable_simd_clone_call.
2639
2640 Check if STMT performs a function call that can be vectorized
2641 by calling a simd clone of the function.
2642 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
2643 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
2644 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
2645
2646 static bool
2647 vectorizable_simd_clone_call (gimple *stmt, gimple_stmt_iterator *gsi,
2648 gimple **vec_stmt, slp_tree slp_node)
2649 {
2650 tree vec_dest;
2651 tree scalar_dest;
2652 tree op, type;
2653 tree vec_oprnd0 = NULL_TREE;
2654 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
2655 tree vectype;
2656 unsigned int nunits;
2657 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2658 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2659 vec_info *vinfo = stmt_info->vinfo;
2660 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2661 tree fndecl, new_temp;
2662 gimple *def_stmt;
2663 gimple *new_stmt = NULL;
2664 int ncopies, j;
2665 vec<simd_call_arg_info> arginfo = vNULL;
2666 vec<tree> vargs = vNULL;
2667 size_t i, nargs;
2668 tree lhs, rtype, ratype;
2669 vec<constructor_elt, va_gc> *ret_ctor_elts;
2670
2671 /* Is STMT a vectorizable call? */
2672 if (!is_gimple_call (stmt))
2673 return false;
2674
2675 fndecl = gimple_call_fndecl (stmt);
2676 if (fndecl == NULL_TREE)
2677 return false;
2678
2679 struct cgraph_node *node = cgraph_node::get (fndecl);
2680 if (node == NULL || node->simd_clones == NULL)
2681 return false;
2682
2683 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
2684 return false;
2685
2686 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
2687 return false;
2688
2689 if (gimple_call_lhs (stmt)
2690 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
2691 return false;
2692
2693 gcc_checking_assert (!stmt_can_throw_internal (stmt));
2694
2695 vectype = STMT_VINFO_VECTYPE (stmt_info);
2696
2697 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
2698 return false;
2699
2700 /* FORNOW */
2701 if (slp_node || PURE_SLP_STMT (stmt_info))
2702 return false;
2703
2704 /* Process function arguments. */
2705 nargs = gimple_call_num_args (stmt);
2706
2707 /* Bail out if the function has zero arguments. */
2708 if (nargs == 0)
2709 return false;
2710
2711 arginfo.create (nargs);
2712
2713 for (i = 0; i < nargs; i++)
2714 {
2715 simd_call_arg_info thisarginfo;
2716 affine_iv iv;
2717
2718 thisarginfo.linear_step = 0;
2719 thisarginfo.align = 0;
2720 thisarginfo.op = NULL_TREE;
2721 thisarginfo.simd_lane_linear = false;
2722
2723 op = gimple_call_arg (stmt, i);
2724 if (!vect_is_simple_use (op, vinfo, &def_stmt, &thisarginfo.dt,
2725 &thisarginfo.vectype)
2726 || thisarginfo.dt == vect_uninitialized_def)
2727 {
2728 if (dump_enabled_p ())
2729 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2730 "use not simple.\n");
2731 arginfo.release ();
2732 return false;
2733 }
2734
2735 if (thisarginfo.dt == vect_constant_def
2736 || thisarginfo.dt == vect_external_def)
2737 gcc_assert (thisarginfo.vectype == NULL_TREE);
2738 else
2739 gcc_assert (thisarginfo.vectype != NULL_TREE);
2740
2741 /* For linear arguments, the analyze phase should have saved
2742 the base and step in STMT_VINFO_SIMD_CLONE_INFO. */
2743 if (i * 3 + 4 <= STMT_VINFO_SIMD_CLONE_INFO (stmt_info).length ()
2744 && STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2])
2745 {
2746 gcc_assert (vec_stmt);
2747 thisarginfo.linear_step
2748 = tree_to_shwi (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2]);
2749 thisarginfo.op
2750 = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 1];
2751 thisarginfo.simd_lane_linear
2752 = (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 3]
2753 == boolean_true_node);
2754 /* If loop has been peeled for alignment, we need to adjust it. */
2755 tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
2756 tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
2757 if (n1 != n2 && !thisarginfo.simd_lane_linear)
2758 {
2759 tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
2760 tree step = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2];
2761 tree opt = TREE_TYPE (thisarginfo.op);
2762 bias = fold_convert (TREE_TYPE (step), bias);
2763 bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
2764 thisarginfo.op
2765 = fold_build2 (POINTER_TYPE_P (opt)
2766 ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
2767 thisarginfo.op, bias);
2768 }
2769 }
2770 else if (!vec_stmt
2771 && thisarginfo.dt != vect_constant_def
2772 && thisarginfo.dt != vect_external_def
2773 && loop_vinfo
2774 && TREE_CODE (op) == SSA_NAME
2775 && simple_iv (loop, loop_containing_stmt (stmt), op,
2776 &iv, false)
2777 && tree_fits_shwi_p (iv.step))
2778 {
2779 thisarginfo.linear_step = tree_to_shwi (iv.step);
2780 thisarginfo.op = iv.base;
2781 }
2782 else if ((thisarginfo.dt == vect_constant_def
2783 || thisarginfo.dt == vect_external_def)
2784 && POINTER_TYPE_P (TREE_TYPE (op)))
2785 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
2786 /* Addresses of array elements indexed by GOMP_SIMD_LANE are
2787 linear too. */
2788 if (POINTER_TYPE_P (TREE_TYPE (op))
2789 && !thisarginfo.linear_step
2790 && !vec_stmt
2791 && thisarginfo.dt != vect_constant_def
2792 && thisarginfo.dt != vect_external_def
2793 && loop_vinfo
2794 && !slp_node
2795 && TREE_CODE (op) == SSA_NAME)
2796 vect_simd_lane_linear (op, loop, &thisarginfo);
2797
2798 arginfo.quick_push (thisarginfo);
2799 }
2800
2801 unsigned int badness = 0;
2802 struct cgraph_node *bestn = NULL;
2803 if (STMT_VINFO_SIMD_CLONE_INFO (stmt_info).exists ())
2804 bestn = cgraph_node::get (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[0]);
2805 else
2806 for (struct cgraph_node *n = node->simd_clones; n != NULL;
2807 n = n->simdclone->next_clone)
2808 {
2809 unsigned int this_badness = 0;
2810 if (n->simdclone->simdlen
2811 > (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo)
2812 || n->simdclone->nargs != nargs)
2813 continue;
2814 if (n->simdclone->simdlen
2815 < (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2816 this_badness += (exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2817 - exact_log2 (n->simdclone->simdlen)) * 1024;
2818 if (n->simdclone->inbranch)
2819 this_badness += 2048;
2820 int target_badness = targetm.simd_clone.usable (n);
2821 if (target_badness < 0)
2822 continue;
2823 this_badness += target_badness * 512;
2824 /* FORNOW: Have to add code to add the mask argument. */
2825 if (n->simdclone->inbranch)
2826 continue;
2827 for (i = 0; i < nargs; i++)
2828 {
2829 switch (n->simdclone->args[i].arg_type)
2830 {
2831 case SIMD_CLONE_ARG_TYPE_VECTOR:
2832 if (!useless_type_conversion_p
2833 (n->simdclone->args[i].orig_type,
2834 TREE_TYPE (gimple_call_arg (stmt, i))))
2835 i = -1;
2836 else if (arginfo[i].dt == vect_constant_def
2837 || arginfo[i].dt == vect_external_def
2838 || arginfo[i].linear_step)
2839 this_badness += 64;
2840 break;
2841 case SIMD_CLONE_ARG_TYPE_UNIFORM:
2842 if (arginfo[i].dt != vect_constant_def
2843 && arginfo[i].dt != vect_external_def)
2844 i = -1;
2845 break;
2846 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
2847 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
2848 if (arginfo[i].dt == vect_constant_def
2849 || arginfo[i].dt == vect_external_def
2850 || (arginfo[i].linear_step
2851 != n->simdclone->args[i].linear_step))
2852 i = -1;
2853 break;
2854 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
2855 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
2856 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
2857 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
2858 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
2859 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
2860 /* FORNOW */
2861 i = -1;
2862 break;
2863 case SIMD_CLONE_ARG_TYPE_MASK:
2864 gcc_unreachable ();
2865 }
2866 if (i == (size_t) -1)
2867 break;
2868 if (n->simdclone->args[i].alignment > arginfo[i].align)
2869 {
2870 i = -1;
2871 break;
2872 }
2873 if (arginfo[i].align)
2874 this_badness += (exact_log2 (arginfo[i].align)
2875 - exact_log2 (n->simdclone->args[i].alignment));
2876 }
2877 if (i == (size_t) -1)
2878 continue;
2879 if (bestn == NULL || this_badness < badness)
2880 {
2881 bestn = n;
2882 badness = this_badness;
2883 }
2884 }
2885
2886 if (bestn == NULL)
2887 {
2888 arginfo.release ();
2889 return false;
2890 }
2891
2892 for (i = 0; i < nargs; i++)
2893 if ((arginfo[i].dt == vect_constant_def
2894 || arginfo[i].dt == vect_external_def)
2895 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
2896 {
2897 arginfo[i].vectype
2898 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
2899 i)));
2900 if (arginfo[i].vectype == NULL
2901 || (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
2902 > bestn->simdclone->simdlen))
2903 {
2904 arginfo.release ();
2905 return false;
2906 }
2907 }
2908
2909 fndecl = bestn->decl;
2910 nunits = bestn->simdclone->simdlen;
2911 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
2912
2913 /* If the function isn't const, only allow it in simd loops where user
2914 has asserted that at least nunits consecutive iterations can be
2915 performed using SIMD instructions. */
2916 if ((loop == NULL || (unsigned) loop->safelen < nunits)
2917 && gimple_vuse (stmt))
2918 {
2919 arginfo.release ();
2920 return false;
2921 }
2922
2923 /* Sanity check: make sure that at least one copy of the vectorized stmt
2924 needs to be generated. */
2925 gcc_assert (ncopies >= 1);
2926
2927 if (!vec_stmt) /* transformation not required. */
2928 {
2929 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (bestn->decl);
2930 for (i = 0; i < nargs; i++)
2931 if (bestn->simdclone->args[i].arg_type
2932 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
2933 {
2934 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_grow_cleared (i * 3
2935 + 1);
2936 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (arginfo[i].op);
2937 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
2938 ? size_type_node : TREE_TYPE (arginfo[i].op);
2939 tree ls = build_int_cst (lst, arginfo[i].linear_step);
2940 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (ls);
2941 tree sll = arginfo[i].simd_lane_linear
2942 ? boolean_true_node : boolean_false_node;
2943 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (sll);
2944 }
2945 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
2946 if (dump_enabled_p ())
2947 dump_printf_loc (MSG_NOTE, vect_location,
2948 "=== vectorizable_simd_clone_call ===\n");
2949 /* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
2950 arginfo.release ();
2951 return true;
2952 }
2953
2954 /** Transform. **/
2955
2956 if (dump_enabled_p ())
2957 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
2958
2959 /* Handle def. */
2960 scalar_dest = gimple_call_lhs (stmt);
2961 vec_dest = NULL_TREE;
2962 rtype = NULL_TREE;
2963 ratype = NULL_TREE;
2964 if (scalar_dest)
2965 {
2966 vec_dest = vect_create_destination_var (scalar_dest, vectype);
2967 rtype = TREE_TYPE (TREE_TYPE (fndecl));
2968 if (TREE_CODE (rtype) == ARRAY_TYPE)
2969 {
2970 ratype = rtype;
2971 rtype = TREE_TYPE (ratype);
2972 }
2973 }
2974
2975 prev_stmt_info = NULL;
2976 for (j = 0; j < ncopies; ++j)
2977 {
2978 /* Build argument list for the vectorized call. */
2979 if (j == 0)
2980 vargs.create (nargs);
2981 else
2982 vargs.truncate (0);
2983
2984 for (i = 0; i < nargs; i++)
2985 {
2986 unsigned int k, l, m, o;
2987 tree atype;
2988 op = gimple_call_arg (stmt, i);
2989 switch (bestn->simdclone->args[i].arg_type)
2990 {
2991 case SIMD_CLONE_ARG_TYPE_VECTOR:
2992 atype = bestn->simdclone->args[i].vector_type;
2993 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
2994 for (m = j * o; m < (j + 1) * o; m++)
2995 {
2996 if (TYPE_VECTOR_SUBPARTS (atype)
2997 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
2998 {
2999 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
3000 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
3001 / TYPE_VECTOR_SUBPARTS (atype));
3002 gcc_assert ((k & (k - 1)) == 0);
3003 if (m == 0)
3004 vec_oprnd0
3005 = vect_get_vec_def_for_operand (op, stmt);
3006 else
3007 {
3008 vec_oprnd0 = arginfo[i].op;
3009 if ((m & (k - 1)) == 0)
3010 vec_oprnd0
3011 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3012 vec_oprnd0);
3013 }
3014 arginfo[i].op = vec_oprnd0;
3015 vec_oprnd0
3016 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
3017 size_int (prec),
3018 bitsize_int ((m & (k - 1)) * prec));
3019 new_stmt
3020 = gimple_build_assign (make_ssa_name (atype),
3021 vec_oprnd0);
3022 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3023 vargs.safe_push (gimple_assign_lhs (new_stmt));
3024 }
3025 else
3026 {
3027 k = (TYPE_VECTOR_SUBPARTS (atype)
3028 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
3029 gcc_assert ((k & (k - 1)) == 0);
3030 vec<constructor_elt, va_gc> *ctor_elts;
3031 if (k != 1)
3032 vec_alloc (ctor_elts, k);
3033 else
3034 ctor_elts = NULL;
3035 for (l = 0; l < k; l++)
3036 {
3037 if (m == 0 && l == 0)
3038 vec_oprnd0
3039 = vect_get_vec_def_for_operand (op, stmt);
3040 else
3041 vec_oprnd0
3042 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3043 arginfo[i].op);
3044 arginfo[i].op = vec_oprnd0;
3045 if (k == 1)
3046 break;
3047 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
3048 vec_oprnd0);
3049 }
3050 if (k == 1)
3051 vargs.safe_push (vec_oprnd0);
3052 else
3053 {
3054 vec_oprnd0 = build_constructor (atype, ctor_elts);
3055 new_stmt
3056 = gimple_build_assign (make_ssa_name (atype),
3057 vec_oprnd0);
3058 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3059 vargs.safe_push (gimple_assign_lhs (new_stmt));
3060 }
3061 }
3062 }
3063 break;
3064 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3065 vargs.safe_push (op);
3066 break;
3067 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
3068 if (j == 0)
3069 {
3070 gimple_seq stmts;
3071 arginfo[i].op
3072 = force_gimple_operand (arginfo[i].op, &stmts, true,
3073 NULL_TREE);
3074 if (stmts != NULL)
3075 {
3076 basic_block new_bb;
3077 edge pe = loop_preheader_edge (loop);
3078 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3079 gcc_assert (!new_bb);
3080 }
3081 if (arginfo[i].simd_lane_linear)
3082 {
3083 vargs.safe_push (arginfo[i].op);
3084 break;
3085 }
3086 tree phi_res = copy_ssa_name (op);
3087 gphi *new_phi = create_phi_node (phi_res, loop->header);
3088 set_vinfo_for_stmt (new_phi,
3089 new_stmt_vec_info (new_phi, loop_vinfo));
3090 add_phi_arg (new_phi, arginfo[i].op,
3091 loop_preheader_edge (loop), UNKNOWN_LOCATION);
3092 enum tree_code code
3093 = POINTER_TYPE_P (TREE_TYPE (op))
3094 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3095 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3096 ? sizetype : TREE_TYPE (op);
3097 widest_int cst
3098 = wi::mul (bestn->simdclone->args[i].linear_step,
3099 ncopies * nunits);
3100 tree tcst = wide_int_to_tree (type, cst);
3101 tree phi_arg = copy_ssa_name (op);
3102 new_stmt
3103 = gimple_build_assign (phi_arg, code, phi_res, tcst);
3104 gimple_stmt_iterator si = gsi_after_labels (loop->header);
3105 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
3106 set_vinfo_for_stmt (new_stmt,
3107 new_stmt_vec_info (new_stmt, loop_vinfo));
3108 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
3109 UNKNOWN_LOCATION);
3110 arginfo[i].op = phi_res;
3111 vargs.safe_push (phi_res);
3112 }
3113 else
3114 {
3115 enum tree_code code
3116 = POINTER_TYPE_P (TREE_TYPE (op))
3117 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3118 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3119 ? sizetype : TREE_TYPE (op);
3120 widest_int cst
3121 = wi::mul (bestn->simdclone->args[i].linear_step,
3122 j * nunits);
3123 tree tcst = wide_int_to_tree (type, cst);
3124 new_temp = make_ssa_name (TREE_TYPE (op));
3125 new_stmt = gimple_build_assign (new_temp, code,
3126 arginfo[i].op, tcst);
3127 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3128 vargs.safe_push (new_temp);
3129 }
3130 break;
3131 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
3132 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
3133 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
3134 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
3135 default:
3136 gcc_unreachable ();
3137 }
3138 }
3139
3140 new_stmt = gimple_build_call_vec (fndecl, vargs);
3141 if (vec_dest)
3142 {
3143 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
3144 if (ratype)
3145 new_temp = create_tmp_var (ratype);
3146 else if (TYPE_VECTOR_SUBPARTS (vectype)
3147 == TYPE_VECTOR_SUBPARTS (rtype))
3148 new_temp = make_ssa_name (vec_dest, new_stmt);
3149 else
3150 new_temp = make_ssa_name (rtype, new_stmt);
3151 gimple_call_set_lhs (new_stmt, new_temp);
3152 }
3153 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3154
3155 if (vec_dest)
3156 {
3157 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
3158 {
3159 unsigned int k, l;
3160 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
3161 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
3162 gcc_assert ((k & (k - 1)) == 0);
3163 for (l = 0; l < k; l++)
3164 {
3165 tree t;
3166 if (ratype)
3167 {
3168 t = build_fold_addr_expr (new_temp);
3169 t = build2 (MEM_REF, vectype, t,
3170 build_int_cst (TREE_TYPE (t),
3171 l * prec / BITS_PER_UNIT));
3172 }
3173 else
3174 t = build3 (BIT_FIELD_REF, vectype, new_temp,
3175 size_int (prec), bitsize_int (l * prec));
3176 new_stmt
3177 = gimple_build_assign (make_ssa_name (vectype), t);
3178 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3179 if (j == 0 && l == 0)
3180 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3181 else
3182 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3183
3184 prev_stmt_info = vinfo_for_stmt (new_stmt);
3185 }
3186
3187 if (ratype)
3188 {
3189 tree clobber = build_constructor (ratype, NULL);
3190 TREE_THIS_VOLATILE (clobber) = 1;
3191 new_stmt = gimple_build_assign (new_temp, clobber);
3192 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3193 }
3194 continue;
3195 }
3196 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
3197 {
3198 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
3199 / TYPE_VECTOR_SUBPARTS (rtype));
3200 gcc_assert ((k & (k - 1)) == 0);
3201 if ((j & (k - 1)) == 0)
3202 vec_alloc (ret_ctor_elts, k);
3203 if (ratype)
3204 {
3205 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
3206 for (m = 0; m < o; m++)
3207 {
3208 tree tem = build4 (ARRAY_REF, rtype, new_temp,
3209 size_int (m), NULL_TREE, NULL_TREE);
3210 new_stmt
3211 = gimple_build_assign (make_ssa_name (rtype), tem);
3212 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3213 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
3214 gimple_assign_lhs (new_stmt));
3215 }
3216 tree clobber = build_constructor (ratype, NULL);
3217 TREE_THIS_VOLATILE (clobber) = 1;
3218 new_stmt = gimple_build_assign (new_temp, clobber);
3219 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3220 }
3221 else
3222 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
3223 if ((j & (k - 1)) != k - 1)
3224 continue;
3225 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
3226 new_stmt
3227 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
3228 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3229
3230 if ((unsigned) j == k - 1)
3231 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3232 else
3233 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3234
3235 prev_stmt_info = vinfo_for_stmt (new_stmt);
3236 continue;
3237 }
3238 else if (ratype)
3239 {
3240 tree t = build_fold_addr_expr (new_temp);
3241 t = build2 (MEM_REF, vectype, t,
3242 build_int_cst (TREE_TYPE (t), 0));
3243 new_stmt
3244 = gimple_build_assign (make_ssa_name (vec_dest), t);
3245 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3246 tree clobber = build_constructor (ratype, NULL);
3247 TREE_THIS_VOLATILE (clobber) = 1;
3248 vect_finish_stmt_generation (stmt,
3249 gimple_build_assign (new_temp,
3250 clobber), gsi);
3251 }
3252 }
3253
3254 if (j == 0)
3255 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3256 else
3257 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3258
3259 prev_stmt_info = vinfo_for_stmt (new_stmt);
3260 }
3261
3262 vargs.release ();
3263
3264 /* The call in STMT might prevent it from being removed in dce.
3265 We however cannot remove it here, due to the way the ssa name
3266 it defines is mapped to the new definition. So just replace
3267 rhs of the statement with something harmless. */
3268
3269 if (slp_node)
3270 return true;
3271
3272 if (scalar_dest)
3273 {
3274 type = TREE_TYPE (scalar_dest);
3275 if (is_pattern_stmt_p (stmt_info))
3276 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
3277 else
3278 lhs = gimple_call_lhs (stmt);
3279 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
3280 }
3281 else
3282 new_stmt = gimple_build_nop ();
3283 set_vinfo_for_stmt (new_stmt, stmt_info);
3284 set_vinfo_for_stmt (stmt, NULL);
3285 STMT_VINFO_STMT (stmt_info) = new_stmt;
3286 gsi_replace (gsi, new_stmt, true);
3287 unlink_stmt_vdef (stmt);
3288
3289 return true;
3290 }
3291
3292
3293 /* Function vect_gen_widened_results_half
3294
3295 Create a vector stmt whose code, type, number of arguments, and result
3296 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
3297 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
3298 In the case that CODE is a CALL_EXPR, this means that a call to DECL
3299 needs to be created (DECL is a function-decl of a target-builtin).
3300 STMT is the original scalar stmt that we are vectorizing. */
3301
3302 static gimple *
3303 vect_gen_widened_results_half (enum tree_code code,
3304 tree decl,
3305 tree vec_oprnd0, tree vec_oprnd1, int op_type,
3306 tree vec_dest, gimple_stmt_iterator *gsi,
3307 gimple *stmt)
3308 {
3309 gimple *new_stmt;
3310 tree new_temp;
3311
3312 /* Generate half of the widened result: */
3313 if (code == CALL_EXPR)
3314 {
3315 /* Target specific support */
3316 if (op_type == binary_op)
3317 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3318 else
3319 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3320 new_temp = make_ssa_name (vec_dest, new_stmt);
3321 gimple_call_set_lhs (new_stmt, new_temp);
3322 }
3323 else
3324 {
3325 /* Generic support */
3326 gcc_assert (op_type == TREE_CODE_LENGTH (code));
3327 if (op_type != binary_op)
3328 vec_oprnd1 = NULL;
3329 new_stmt = gimple_build_assign (vec_dest, code, vec_oprnd0, vec_oprnd1);
3330 new_temp = make_ssa_name (vec_dest, new_stmt);
3331 gimple_assign_set_lhs (new_stmt, new_temp);
3332 }
3333 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3334
3335 return new_stmt;
3336 }
3337
3338
3339 /* Get vectorized definitions for loop-based vectorization. For the first
3340 operand we call vect_get_vec_def_for_operand() (with OPRND containing
3341 scalar operand), and for the rest we get a copy with
3342 vect_get_vec_def_for_stmt_copy() using the previous vector definition
3343 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
3344 The vectors are collected into VEC_OPRNDS. */
3345
3346 static void
3347 vect_get_loop_based_defs (tree *oprnd, gimple *stmt, enum vect_def_type dt,
3348 vec<tree> *vec_oprnds, int multi_step_cvt)
3349 {
3350 tree vec_oprnd;
3351
3352 /* Get first vector operand. */
3353 /* All the vector operands except the very first one (that is scalar oprnd)
3354 are stmt copies. */
3355 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
3356 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt);
3357 else
3358 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
3359
3360 vec_oprnds->quick_push (vec_oprnd);
3361
3362 /* Get second vector operand. */
3363 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
3364 vec_oprnds->quick_push (vec_oprnd);
3365
3366 *oprnd = vec_oprnd;
3367
3368 /* For conversion in multiple steps, continue to get operands
3369 recursively. */
3370 if (multi_step_cvt)
3371 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
3372 }
3373
3374
3375 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
3376 For multi-step conversions store the resulting vectors and call the function
3377 recursively. */
3378
3379 static void
3380 vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
3381 int multi_step_cvt, gimple *stmt,
3382 vec<tree> vec_dsts,
3383 gimple_stmt_iterator *gsi,
3384 slp_tree slp_node, enum tree_code code,
3385 stmt_vec_info *prev_stmt_info)
3386 {
3387 unsigned int i;
3388 tree vop0, vop1, new_tmp, vec_dest;
3389 gimple *new_stmt;
3390 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3391
3392 vec_dest = vec_dsts.pop ();
3393
3394 for (i = 0; i < vec_oprnds->length (); i += 2)
3395 {
3396 /* Create demotion operation. */
3397 vop0 = (*vec_oprnds)[i];
3398 vop1 = (*vec_oprnds)[i + 1];
3399 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
3400 new_tmp = make_ssa_name (vec_dest, new_stmt);
3401 gimple_assign_set_lhs (new_stmt, new_tmp);
3402 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3403
3404 if (multi_step_cvt)
3405 /* Store the resulting vector for next recursive call. */
3406 (*vec_oprnds)[i/2] = new_tmp;
3407 else
3408 {
3409 /* This is the last step of the conversion sequence. Store the
3410 vectors in SLP_NODE or in vector info of the scalar statement
3411 (or in STMT_VINFO_RELATED_STMT chain). */
3412 if (slp_node)
3413 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3414 else
3415 {
3416 if (!*prev_stmt_info)
3417 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3418 else
3419 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
3420
3421 *prev_stmt_info = vinfo_for_stmt (new_stmt);
3422 }
3423 }
3424 }
3425
3426 /* For multi-step demotion operations we first generate demotion operations
3427 from the source type to the intermediate types, and then combine the
3428 results (stored in VEC_OPRNDS) in demotion operation to the destination
3429 type. */
3430 if (multi_step_cvt)
3431 {
3432 /* At each level of recursion we have half of the operands we had at the
3433 previous level. */
3434 vec_oprnds->truncate ((i+1)/2);
3435 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
3436 stmt, vec_dsts, gsi, slp_node,
3437 VEC_PACK_TRUNC_EXPR,
3438 prev_stmt_info);
3439 }
3440
3441 vec_dsts.quick_push (vec_dest);
3442 }
3443
3444
3445 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
3446 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
3447 the resulting vectors and call the function recursively. */
3448
3449 static void
3450 vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
3451 vec<tree> *vec_oprnds1,
3452 gimple *stmt, tree vec_dest,
3453 gimple_stmt_iterator *gsi,
3454 enum tree_code code1,
3455 enum tree_code code2, tree decl1,
3456 tree decl2, int op_type)
3457 {
3458 int i;
3459 tree vop0, vop1, new_tmp1, new_tmp2;
3460 gimple *new_stmt1, *new_stmt2;
3461 vec<tree> vec_tmp = vNULL;
3462
3463 vec_tmp.create (vec_oprnds0->length () * 2);
3464 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
3465 {
3466 if (op_type == binary_op)
3467 vop1 = (*vec_oprnds1)[i];
3468 else
3469 vop1 = NULL_TREE;
3470
3471 /* Generate the two halves of promotion operation. */
3472 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
3473 op_type, vec_dest, gsi, stmt);
3474 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
3475 op_type, vec_dest, gsi, stmt);
3476 if (is_gimple_call (new_stmt1))
3477 {
3478 new_tmp1 = gimple_call_lhs (new_stmt1);
3479 new_tmp2 = gimple_call_lhs (new_stmt2);
3480 }
3481 else
3482 {
3483 new_tmp1 = gimple_assign_lhs (new_stmt1);
3484 new_tmp2 = gimple_assign_lhs (new_stmt2);
3485 }
3486
3487 /* Store the results for the next step. */
3488 vec_tmp.quick_push (new_tmp1);
3489 vec_tmp.quick_push (new_tmp2);
3490 }
3491
3492 vec_oprnds0->release ();
3493 *vec_oprnds0 = vec_tmp;
3494 }
3495
3496
3497 /* Check if STMT performs a conversion operation, that can be vectorized.
3498 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3499 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3500 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3501
3502 static bool
3503 vectorizable_conversion (gimple *stmt, gimple_stmt_iterator *gsi,
3504 gimple **vec_stmt, slp_tree slp_node)
3505 {
3506 tree vec_dest;
3507 tree scalar_dest;
3508 tree op0, op1 = NULL_TREE;
3509 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3510 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3511 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3512 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
3513 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
3514 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3515 tree new_temp;
3516 gimple *def_stmt;
3517 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
3518 gimple *new_stmt = NULL;
3519 stmt_vec_info prev_stmt_info;
3520 int nunits_in;
3521 int nunits_out;
3522 tree vectype_out, vectype_in;
3523 int ncopies, i, j;
3524 tree lhs_type, rhs_type;
3525 enum { NARROW, NONE, WIDEN } modifier;
3526 vec<tree> vec_oprnds0 = vNULL;
3527 vec<tree> vec_oprnds1 = vNULL;
3528 tree vop0;
3529 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3530 vec_info *vinfo = stmt_info->vinfo;
3531 int multi_step_cvt = 0;
3532 vec<tree> vec_dsts = vNULL;
3533 vec<tree> interm_types = vNULL;
3534 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
3535 int op_type;
3536 machine_mode rhs_mode;
3537 unsigned short fltsz;
3538
3539 /* Is STMT a vectorizable conversion? */
3540
3541 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3542 return false;
3543
3544 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
3545 return false;
3546
3547 if (!is_gimple_assign (stmt))
3548 return false;
3549
3550 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3551 return false;
3552
3553 code = gimple_assign_rhs_code (stmt);
3554 if (!CONVERT_EXPR_CODE_P (code)
3555 && code != FIX_TRUNC_EXPR
3556 && code != FLOAT_EXPR
3557 && code != WIDEN_MULT_EXPR
3558 && code != WIDEN_LSHIFT_EXPR)
3559 return false;
3560
3561 op_type = TREE_CODE_LENGTH (code);
3562
3563 /* Check types of lhs and rhs. */
3564 scalar_dest = gimple_assign_lhs (stmt);
3565 lhs_type = TREE_TYPE (scalar_dest);
3566 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3567
3568 op0 = gimple_assign_rhs1 (stmt);
3569 rhs_type = TREE_TYPE (op0);
3570
3571 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3572 && !((INTEGRAL_TYPE_P (lhs_type)
3573 && INTEGRAL_TYPE_P (rhs_type))
3574 || (SCALAR_FLOAT_TYPE_P (lhs_type)
3575 && SCALAR_FLOAT_TYPE_P (rhs_type))))
3576 return false;
3577
3578 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
3579 && ((INTEGRAL_TYPE_P (lhs_type)
3580 && (TYPE_PRECISION (lhs_type)
3581 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
3582 || (INTEGRAL_TYPE_P (rhs_type)
3583 && (TYPE_PRECISION (rhs_type)
3584 != GET_MODE_PRECISION (TYPE_MODE (rhs_type))))))
3585 {
3586 if (dump_enabled_p ())
3587 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3588 "type conversion to/from bit-precision unsupported."
3589 "\n");
3590 return false;
3591 }
3592
3593 /* Check the operands of the operation. */
3594 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype_in))
3595 {
3596 if (dump_enabled_p ())
3597 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3598 "use not simple.\n");
3599 return false;
3600 }
3601 if (op_type == binary_op)
3602 {
3603 bool ok;
3604
3605 op1 = gimple_assign_rhs2 (stmt);
3606 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3607 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3608 OP1. */
3609 if (CONSTANT_CLASS_P (op0))
3610 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &vectype_in);
3611 else
3612 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]);
3613
3614 if (!ok)
3615 {
3616 if (dump_enabled_p ())
3617 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3618 "use not simple.\n");
3619 return false;
3620 }
3621 }
3622
3623 /* If op0 is an external or constant defs use a vector type of
3624 the same size as the output vector type. */
3625 if (!vectype_in)
3626 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
3627 if (vec_stmt)
3628 gcc_assert (vectype_in);
3629 if (!vectype_in)
3630 {
3631 if (dump_enabled_p ())
3632 {
3633 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3634 "no vectype for scalar type ");
3635 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3636 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3637 }
3638
3639 return false;
3640 }
3641
3642 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3643 && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
3644 {
3645 if (dump_enabled_p ())
3646 {
3647 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3648 "can't convert between boolean and non "
3649 "boolean vectors");
3650 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3651 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3652 }
3653
3654 return false;
3655 }
3656
3657 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3658 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3659 if (nunits_in < nunits_out)
3660 modifier = NARROW;
3661 else if (nunits_out == nunits_in)
3662 modifier = NONE;
3663 else
3664 modifier = WIDEN;
3665
3666 /* Multiple types in SLP are handled by creating the appropriate number of
3667 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3668 case of SLP. */
3669 if (slp_node || PURE_SLP_STMT (stmt_info))
3670 ncopies = 1;
3671 else if (modifier == NARROW)
3672 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3673 else
3674 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
3675
3676 /* Sanity check: make sure that at least one copy of the vectorized stmt
3677 needs to be generated. */
3678 gcc_assert (ncopies >= 1);
3679
3680 /* Supportable by target? */
3681 switch (modifier)
3682 {
3683 case NONE:
3684 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3685 return false;
3686 if (supportable_convert_operation (code, vectype_out, vectype_in,
3687 &decl1, &code1))
3688 break;
3689 /* FALLTHRU */
3690 unsupported:
3691 if (dump_enabled_p ())
3692 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3693 "conversion not supported by target.\n");
3694 return false;
3695
3696 case WIDEN:
3697 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
3698 &code1, &code2, &multi_step_cvt,
3699 &interm_types))
3700 {
3701 /* Binary widening operation can only be supported directly by the
3702 architecture. */
3703 gcc_assert (!(multi_step_cvt && op_type == binary_op));
3704 break;
3705 }
3706
3707 if (code != FLOAT_EXPR
3708 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3709 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3710 goto unsupported;
3711
3712 rhs_mode = TYPE_MODE (rhs_type);
3713 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
3714 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
3715 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
3716 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
3717 {
3718 cvt_type
3719 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3720 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3721 if (cvt_type == NULL_TREE)
3722 goto unsupported;
3723
3724 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3725 {
3726 if (!supportable_convert_operation (code, vectype_out,
3727 cvt_type, &decl1, &codecvt1))
3728 goto unsupported;
3729 }
3730 else if (!supportable_widening_operation (code, stmt, vectype_out,
3731 cvt_type, &codecvt1,
3732 &codecvt2, &multi_step_cvt,
3733 &interm_types))
3734 continue;
3735 else
3736 gcc_assert (multi_step_cvt == 0);
3737
3738 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
3739 vectype_in, &code1, &code2,
3740 &multi_step_cvt, &interm_types))
3741 break;
3742 }
3743
3744 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
3745 goto unsupported;
3746
3747 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3748 codecvt2 = ERROR_MARK;
3749 else
3750 {
3751 multi_step_cvt++;
3752 interm_types.safe_push (cvt_type);
3753 cvt_type = NULL_TREE;
3754 }
3755 break;
3756
3757 case NARROW:
3758 gcc_assert (op_type == unary_op);
3759 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
3760 &code1, &multi_step_cvt,
3761 &interm_types))
3762 break;
3763
3764 if (code != FIX_TRUNC_EXPR
3765 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3766 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3767 goto unsupported;
3768
3769 rhs_mode = TYPE_MODE (rhs_type);
3770 cvt_type
3771 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3772 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3773 if (cvt_type == NULL_TREE)
3774 goto unsupported;
3775 if (!supportable_convert_operation (code, cvt_type, vectype_in,
3776 &decl1, &codecvt1))
3777 goto unsupported;
3778 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
3779 &code1, &multi_step_cvt,
3780 &interm_types))
3781 break;
3782 goto unsupported;
3783
3784 default:
3785 gcc_unreachable ();
3786 }
3787
3788 if (!vec_stmt) /* transformation not required. */
3789 {
3790 if (dump_enabled_p ())
3791 dump_printf_loc (MSG_NOTE, vect_location,
3792 "=== vectorizable_conversion ===\n");
3793 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
3794 {
3795 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
3796 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
3797 }
3798 else if (modifier == NARROW)
3799 {
3800 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
3801 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3802 }
3803 else
3804 {
3805 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
3806 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
3807 }
3808 interm_types.release ();
3809 return true;
3810 }
3811
3812 /** Transform. **/
3813 if (dump_enabled_p ())
3814 dump_printf_loc (MSG_NOTE, vect_location,
3815 "transform conversion. ncopies = %d.\n", ncopies);
3816
3817 if (op_type == binary_op)
3818 {
3819 if (CONSTANT_CLASS_P (op0))
3820 op0 = fold_convert (TREE_TYPE (op1), op0);
3821 else if (CONSTANT_CLASS_P (op1))
3822 op1 = fold_convert (TREE_TYPE (op0), op1);
3823 }
3824
3825 /* In case of multi-step conversion, we first generate conversion operations
3826 to the intermediate types, and then from that types to the final one.
3827 We create vector destinations for the intermediate type (TYPES) received
3828 from supportable_*_operation, and store them in the correct order
3829 for future use in vect_create_vectorized_*_stmts (). */
3830 vec_dsts.create (multi_step_cvt + 1);
3831 vec_dest = vect_create_destination_var (scalar_dest,
3832 (cvt_type && modifier == WIDEN)
3833 ? cvt_type : vectype_out);
3834 vec_dsts.quick_push (vec_dest);
3835
3836 if (multi_step_cvt)
3837 {
3838 for (i = interm_types.length () - 1;
3839 interm_types.iterate (i, &intermediate_type); i--)
3840 {
3841 vec_dest = vect_create_destination_var (scalar_dest,
3842 intermediate_type);
3843 vec_dsts.quick_push (vec_dest);
3844 }
3845 }
3846
3847 if (cvt_type)
3848 vec_dest = vect_create_destination_var (scalar_dest,
3849 modifier == WIDEN
3850 ? vectype_out : cvt_type);
3851
3852 if (!slp_node)
3853 {
3854 if (modifier == WIDEN)
3855 {
3856 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
3857 if (op_type == binary_op)
3858 vec_oprnds1.create (1);
3859 }
3860 else if (modifier == NARROW)
3861 vec_oprnds0.create (
3862 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
3863 }
3864 else if (code == WIDEN_LSHIFT_EXPR)
3865 vec_oprnds1.create (slp_node->vec_stmts_size);
3866
3867 last_oprnd = op0;
3868 prev_stmt_info = NULL;
3869 switch (modifier)
3870 {
3871 case NONE:
3872 for (j = 0; j < ncopies; j++)
3873 {
3874 if (j == 0)
3875 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
3876 -1);
3877 else
3878 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3879
3880 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3881 {
3882 /* Arguments are ready, create the new vector stmt. */
3883 if (code1 == CALL_EXPR)
3884 {
3885 new_stmt = gimple_build_call (decl1, 1, vop0);
3886 new_temp = make_ssa_name (vec_dest, new_stmt);
3887 gimple_call_set_lhs (new_stmt, new_temp);
3888 }
3889 else
3890 {
3891 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
3892 new_stmt = gimple_build_assign (vec_dest, code1, vop0);
3893 new_temp = make_ssa_name (vec_dest, new_stmt);
3894 gimple_assign_set_lhs (new_stmt, new_temp);
3895 }
3896
3897 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3898 if (slp_node)
3899 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3900 else
3901 {
3902 if (!prev_stmt_info)
3903 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3904 else
3905 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3906 prev_stmt_info = vinfo_for_stmt (new_stmt);
3907 }
3908 }
3909 }
3910 break;
3911
3912 case WIDEN:
3913 /* In case the vectorization factor (VF) is bigger than the number
3914 of elements that we can fit in a vectype (nunits), we have to
3915 generate more than one vector stmt - i.e - we need to "unroll"
3916 the vector stmt by a factor VF/nunits. */
3917 for (j = 0; j < ncopies; j++)
3918 {
3919 /* Handle uses. */
3920 if (j == 0)
3921 {
3922 if (slp_node)
3923 {
3924 if (code == WIDEN_LSHIFT_EXPR)
3925 {
3926 unsigned int k;
3927
3928 vec_oprnd1 = op1;
3929 /* Store vec_oprnd1 for every vector stmt to be created
3930 for SLP_NODE. We check during the analysis that all
3931 the shift arguments are the same. */
3932 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
3933 vec_oprnds1.quick_push (vec_oprnd1);
3934
3935 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
3936 slp_node, -1);
3937 }
3938 else
3939 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
3940 &vec_oprnds1, slp_node, -1);
3941 }
3942 else
3943 {
3944 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt);
3945 vec_oprnds0.quick_push (vec_oprnd0);
3946 if (op_type == binary_op)
3947 {
3948 if (code == WIDEN_LSHIFT_EXPR)
3949 vec_oprnd1 = op1;
3950 else
3951 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt);
3952 vec_oprnds1.quick_push (vec_oprnd1);
3953 }
3954 }
3955 }
3956 else
3957 {
3958 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
3959 vec_oprnds0.truncate (0);
3960 vec_oprnds0.quick_push (vec_oprnd0);
3961 if (op_type == binary_op)
3962 {
3963 if (code == WIDEN_LSHIFT_EXPR)
3964 vec_oprnd1 = op1;
3965 else
3966 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
3967 vec_oprnd1);
3968 vec_oprnds1.truncate (0);
3969 vec_oprnds1.quick_push (vec_oprnd1);
3970 }
3971 }
3972
3973 /* Arguments are ready. Create the new vector stmts. */
3974 for (i = multi_step_cvt; i >= 0; i--)
3975 {
3976 tree this_dest = vec_dsts[i];
3977 enum tree_code c1 = code1, c2 = code2;
3978 if (i == 0 && codecvt2 != ERROR_MARK)
3979 {
3980 c1 = codecvt1;
3981 c2 = codecvt2;
3982 }
3983 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
3984 &vec_oprnds1,
3985 stmt, this_dest, gsi,
3986 c1, c2, decl1, decl2,
3987 op_type);
3988 }
3989
3990 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
3991 {
3992 if (cvt_type)
3993 {
3994 if (codecvt1 == CALL_EXPR)
3995 {
3996 new_stmt = gimple_build_call (decl1, 1, vop0);
3997 new_temp = make_ssa_name (vec_dest, new_stmt);
3998 gimple_call_set_lhs (new_stmt, new_temp);
3999 }
4000 else
4001 {
4002 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
4003 new_temp = make_ssa_name (vec_dest);
4004 new_stmt = gimple_build_assign (new_temp, codecvt1,
4005 vop0);
4006 }
4007
4008 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4009 }
4010 else
4011 new_stmt = SSA_NAME_DEF_STMT (vop0);
4012
4013 if (slp_node)
4014 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4015 else
4016 {
4017 if (!prev_stmt_info)
4018 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
4019 else
4020 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4021 prev_stmt_info = vinfo_for_stmt (new_stmt);
4022 }
4023 }
4024 }
4025
4026 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4027 break;
4028
4029 case NARROW:
4030 /* In case the vectorization factor (VF) is bigger than the number
4031 of elements that we can fit in a vectype (nunits), we have to
4032 generate more than one vector stmt - i.e - we need to "unroll"
4033 the vector stmt by a factor VF/nunits. */
4034 for (j = 0; j < ncopies; j++)
4035 {
4036 /* Handle uses. */
4037 if (slp_node)
4038 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4039 slp_node, -1);
4040 else
4041 {
4042 vec_oprnds0.truncate (0);
4043 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
4044 vect_pow2 (multi_step_cvt) - 1);
4045 }
4046
4047 /* Arguments are ready. Create the new vector stmts. */
4048 if (cvt_type)
4049 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4050 {
4051 if (codecvt1 == CALL_EXPR)
4052 {
4053 new_stmt = gimple_build_call (decl1, 1, vop0);
4054 new_temp = make_ssa_name (vec_dest, new_stmt);
4055 gimple_call_set_lhs (new_stmt, new_temp);
4056 }
4057 else
4058 {
4059 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
4060 new_temp = make_ssa_name (vec_dest);
4061 new_stmt = gimple_build_assign (new_temp, codecvt1,
4062 vop0);
4063 }
4064
4065 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4066 vec_oprnds0[i] = new_temp;
4067 }
4068
4069 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
4070 stmt, vec_dsts, gsi,
4071 slp_node, code1,
4072 &prev_stmt_info);
4073 }
4074
4075 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4076 break;
4077 }
4078
4079 vec_oprnds0.release ();
4080 vec_oprnds1.release ();
4081 vec_dsts.release ();
4082 interm_types.release ();
4083
4084 return true;
4085 }
4086
4087
4088 /* Function vectorizable_assignment.
4089
4090 Check if STMT performs an assignment (copy) that can be vectorized.
4091 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4092 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4093 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4094
4095 static bool
4096 vectorizable_assignment (gimple *stmt, gimple_stmt_iterator *gsi,
4097 gimple **vec_stmt, slp_tree slp_node)
4098 {
4099 tree vec_dest;
4100 tree scalar_dest;
4101 tree op;
4102 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4103 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4104 tree new_temp;
4105 gimple *def_stmt;
4106 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4107 int ncopies;
4108 int i, j;
4109 vec<tree> vec_oprnds = vNULL;
4110 tree vop;
4111 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4112 vec_info *vinfo = stmt_info->vinfo;
4113 gimple *new_stmt = NULL;
4114 stmt_vec_info prev_stmt_info = NULL;
4115 enum tree_code code;
4116 tree vectype_in;
4117
4118 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4119 return false;
4120
4121 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4122 return false;
4123
4124 /* Is vectorizable assignment? */
4125 if (!is_gimple_assign (stmt))
4126 return false;
4127
4128 scalar_dest = gimple_assign_lhs (stmt);
4129 if (TREE_CODE (scalar_dest) != SSA_NAME)
4130 return false;
4131
4132 code = gimple_assign_rhs_code (stmt);
4133 if (gimple_assign_single_p (stmt)
4134 || code == PAREN_EXPR
4135 || CONVERT_EXPR_CODE_P (code))
4136 op = gimple_assign_rhs1 (stmt);
4137 else
4138 return false;
4139
4140 if (code == VIEW_CONVERT_EXPR)
4141 op = TREE_OPERAND (op, 0);
4142
4143 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4144 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4145
4146 /* Multiple types in SLP are handled by creating the appropriate number of
4147 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4148 case of SLP. */
4149 if (slp_node || PURE_SLP_STMT (stmt_info))
4150 ncopies = 1;
4151 else
4152 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4153
4154 gcc_assert (ncopies >= 1);
4155
4156 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt[0], &vectype_in))
4157 {
4158 if (dump_enabled_p ())
4159 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4160 "use not simple.\n");
4161 return false;
4162 }
4163
4164 /* We can handle NOP_EXPR conversions that do not change the number
4165 of elements or the vector size. */
4166 if ((CONVERT_EXPR_CODE_P (code)
4167 || code == VIEW_CONVERT_EXPR)
4168 && (!vectype_in
4169 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
4170 || (GET_MODE_SIZE (TYPE_MODE (vectype))
4171 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
4172 return false;
4173
4174 /* We do not handle bit-precision changes. */
4175 if ((CONVERT_EXPR_CODE_P (code)
4176 || code == VIEW_CONVERT_EXPR)
4177 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4178 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4179 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4180 || ((TYPE_PRECISION (TREE_TYPE (op))
4181 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
4182 /* But a conversion that does not change the bit-pattern is ok. */
4183 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4184 > TYPE_PRECISION (TREE_TYPE (op)))
4185 && TYPE_UNSIGNED (TREE_TYPE (op))))
4186 {
4187 if (dump_enabled_p ())
4188 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4189 "type conversion to/from bit-precision "
4190 "unsupported.\n");
4191 return false;
4192 }
4193
4194 if (!vec_stmt) /* transformation not required. */
4195 {
4196 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
4197 if (dump_enabled_p ())
4198 dump_printf_loc (MSG_NOTE, vect_location,
4199 "=== vectorizable_assignment ===\n");
4200 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4201 return true;
4202 }
4203
4204 /** Transform. **/
4205 if (dump_enabled_p ())
4206 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
4207
4208 /* Handle def. */
4209 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4210
4211 /* Handle use. */
4212 for (j = 0; j < ncopies; j++)
4213 {
4214 /* Handle uses. */
4215 if (j == 0)
4216 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
4217 else
4218 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
4219
4220 /* Arguments are ready. create the new vector stmt. */
4221 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
4222 {
4223 if (CONVERT_EXPR_CODE_P (code)
4224 || code == VIEW_CONVERT_EXPR)
4225 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
4226 new_stmt = gimple_build_assign (vec_dest, vop);
4227 new_temp = make_ssa_name (vec_dest, new_stmt);
4228 gimple_assign_set_lhs (new_stmt, new_temp);
4229 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4230 if (slp_node)
4231 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4232 }
4233
4234 if (slp_node)
4235 continue;
4236
4237 if (j == 0)
4238 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4239 else
4240 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4241
4242 prev_stmt_info = vinfo_for_stmt (new_stmt);
4243 }
4244
4245 vec_oprnds.release ();
4246 return true;
4247 }
4248
4249
4250 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
4251 either as shift by a scalar or by a vector. */
4252
4253 bool
4254 vect_supportable_shift (enum tree_code code, tree scalar_type)
4255 {
4256
4257 machine_mode vec_mode;
4258 optab optab;
4259 int icode;
4260 tree vectype;
4261
4262 vectype = get_vectype_for_scalar_type (scalar_type);
4263 if (!vectype)
4264 return false;
4265
4266 optab = optab_for_tree_code (code, vectype, optab_scalar);
4267 if (!optab
4268 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
4269 {
4270 optab = optab_for_tree_code (code, vectype, optab_vector);
4271 if (!optab
4272 || (optab_handler (optab, TYPE_MODE (vectype))
4273 == CODE_FOR_nothing))
4274 return false;
4275 }
4276
4277 vec_mode = TYPE_MODE (vectype);
4278 icode = (int) optab_handler (optab, vec_mode);
4279 if (icode == CODE_FOR_nothing)
4280 return false;
4281
4282 return true;
4283 }
4284
4285
4286 /* Function vectorizable_shift.
4287
4288 Check if STMT performs a shift operation that can be vectorized.
4289 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4290 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4291 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4292
4293 static bool
4294 vectorizable_shift (gimple *stmt, gimple_stmt_iterator *gsi,
4295 gimple **vec_stmt, slp_tree slp_node)
4296 {
4297 tree vec_dest;
4298 tree scalar_dest;
4299 tree op0, op1 = NULL;
4300 tree vec_oprnd1 = NULL_TREE;
4301 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4302 tree vectype;
4303 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4304 enum tree_code code;
4305 machine_mode vec_mode;
4306 tree new_temp;
4307 optab optab;
4308 int icode;
4309 machine_mode optab_op2_mode;
4310 gimple *def_stmt;
4311 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4312 gimple *new_stmt = NULL;
4313 stmt_vec_info prev_stmt_info;
4314 int nunits_in;
4315 int nunits_out;
4316 tree vectype_out;
4317 tree op1_vectype;
4318 int ncopies;
4319 int j, i;
4320 vec<tree> vec_oprnds0 = vNULL;
4321 vec<tree> vec_oprnds1 = vNULL;
4322 tree vop0, vop1;
4323 unsigned int k;
4324 bool scalar_shift_arg = true;
4325 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4326 vec_info *vinfo = stmt_info->vinfo;
4327 int vf;
4328
4329 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4330 return false;
4331
4332 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4333 return false;
4334
4335 /* Is STMT a vectorizable binary/unary operation? */
4336 if (!is_gimple_assign (stmt))
4337 return false;
4338
4339 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4340 return false;
4341
4342 code = gimple_assign_rhs_code (stmt);
4343
4344 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4345 || code == RROTATE_EXPR))
4346 return false;
4347
4348 scalar_dest = gimple_assign_lhs (stmt);
4349 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4350 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4351 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4352 {
4353 if (dump_enabled_p ())
4354 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4355 "bit-precision shifts not supported.\n");
4356 return false;
4357 }
4358
4359 op0 = gimple_assign_rhs1 (stmt);
4360 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
4361 {
4362 if (dump_enabled_p ())
4363 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4364 "use not simple.\n");
4365 return false;
4366 }
4367 /* If op0 is an external or constant def use a vector type with
4368 the same size as the output vector type. */
4369 if (!vectype)
4370 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4371 if (vec_stmt)
4372 gcc_assert (vectype);
4373 if (!vectype)
4374 {
4375 if (dump_enabled_p ())
4376 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4377 "no vectype for scalar type\n");
4378 return false;
4379 }
4380
4381 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4382 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4383 if (nunits_out != nunits_in)
4384 return false;
4385
4386 op1 = gimple_assign_rhs2 (stmt);
4387 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &op1_vectype))
4388 {
4389 if (dump_enabled_p ())
4390 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4391 "use not simple.\n");
4392 return false;
4393 }
4394
4395 if (loop_vinfo)
4396 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4397 else
4398 vf = 1;
4399
4400 /* Multiple types in SLP are handled by creating the appropriate number of
4401 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4402 case of SLP. */
4403 if (slp_node || PURE_SLP_STMT (stmt_info))
4404 ncopies = 1;
4405 else
4406 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4407
4408 gcc_assert (ncopies >= 1);
4409
4410 /* Determine whether the shift amount is a vector, or scalar. If the
4411 shift/rotate amount is a vector, use the vector/vector shift optabs. */
4412
4413 if ((dt[1] == vect_internal_def
4414 || dt[1] == vect_induction_def)
4415 && !slp_node)
4416 scalar_shift_arg = false;
4417 else if (dt[1] == vect_constant_def
4418 || dt[1] == vect_external_def
4419 || dt[1] == vect_internal_def)
4420 {
4421 /* In SLP, need to check whether the shift count is the same,
4422 in loops if it is a constant or invariant, it is always
4423 a scalar shift. */
4424 if (slp_node)
4425 {
4426 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4427 gimple *slpstmt;
4428
4429 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
4430 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
4431 scalar_shift_arg = false;
4432 }
4433 }
4434 else
4435 {
4436 if (dump_enabled_p ())
4437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4438 "operand mode requires invariant argument.\n");
4439 return false;
4440 }
4441
4442 /* Vector shifted by vector. */
4443 if (!scalar_shift_arg)
4444 {
4445 optab = optab_for_tree_code (code, vectype, optab_vector);
4446 if (dump_enabled_p ())
4447 dump_printf_loc (MSG_NOTE, vect_location,
4448 "vector/vector shift/rotate found.\n");
4449
4450 if (!op1_vectype)
4451 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
4452 if (op1_vectype == NULL_TREE
4453 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
4454 {
4455 if (dump_enabled_p ())
4456 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4457 "unusable type for last operand in"
4458 " vector/vector shift/rotate.\n");
4459 return false;
4460 }
4461 }
4462 /* See if the machine has a vector shifted by scalar insn and if not
4463 then see if it has a vector shifted by vector insn. */
4464 else
4465 {
4466 optab = optab_for_tree_code (code, vectype, optab_scalar);
4467 if (optab
4468 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
4469 {
4470 if (dump_enabled_p ())
4471 dump_printf_loc (MSG_NOTE, vect_location,
4472 "vector/scalar shift/rotate found.\n");
4473 }
4474 else
4475 {
4476 optab = optab_for_tree_code (code, vectype, optab_vector);
4477 if (optab
4478 && (optab_handler (optab, TYPE_MODE (vectype))
4479 != CODE_FOR_nothing))
4480 {
4481 scalar_shift_arg = false;
4482
4483 if (dump_enabled_p ())
4484 dump_printf_loc (MSG_NOTE, vect_location,
4485 "vector/vector shift/rotate found.\n");
4486
4487 /* Unlike the other binary operators, shifts/rotates have
4488 the rhs being int, instead of the same type as the lhs,
4489 so make sure the scalar is the right type if we are
4490 dealing with vectors of long long/long/short/char. */
4491 if (dt[1] == vect_constant_def)
4492 op1 = fold_convert (TREE_TYPE (vectype), op1);
4493 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
4494 TREE_TYPE (op1)))
4495 {
4496 if (slp_node
4497 && TYPE_MODE (TREE_TYPE (vectype))
4498 != TYPE_MODE (TREE_TYPE (op1)))
4499 {
4500 if (dump_enabled_p ())
4501 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4502 "unusable type for last operand in"
4503 " vector/vector shift/rotate.\n");
4504 return false;
4505 }
4506 if (vec_stmt && !slp_node)
4507 {
4508 op1 = fold_convert (TREE_TYPE (vectype), op1);
4509 op1 = vect_init_vector (stmt, op1,
4510 TREE_TYPE (vectype), NULL);
4511 }
4512 }
4513 }
4514 }
4515 }
4516
4517 /* Supportable by target? */
4518 if (!optab)
4519 {
4520 if (dump_enabled_p ())
4521 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4522 "no optab.\n");
4523 return false;
4524 }
4525 vec_mode = TYPE_MODE (vectype);
4526 icode = (int) optab_handler (optab, vec_mode);
4527 if (icode == CODE_FOR_nothing)
4528 {
4529 if (dump_enabled_p ())
4530 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4531 "op not supported by target.\n");
4532 /* Check only during analysis. */
4533 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4534 || (vf < vect_min_worthwhile_factor (code)
4535 && !vec_stmt))
4536 return false;
4537 if (dump_enabled_p ())
4538 dump_printf_loc (MSG_NOTE, vect_location,
4539 "proceeding using word mode.\n");
4540 }
4541
4542 /* Worthwhile without SIMD support? Check only during analysis. */
4543 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4544 && vf < vect_min_worthwhile_factor (code)
4545 && !vec_stmt)
4546 {
4547 if (dump_enabled_p ())
4548 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4549 "not worthwhile without SIMD support.\n");
4550 return false;
4551 }
4552
4553 if (!vec_stmt) /* transformation not required. */
4554 {
4555 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
4556 if (dump_enabled_p ())
4557 dump_printf_loc (MSG_NOTE, vect_location,
4558 "=== vectorizable_shift ===\n");
4559 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4560 return true;
4561 }
4562
4563 /** Transform. **/
4564
4565 if (dump_enabled_p ())
4566 dump_printf_loc (MSG_NOTE, vect_location,
4567 "transform binary/unary operation.\n");
4568
4569 /* Handle def. */
4570 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4571
4572 prev_stmt_info = NULL;
4573 for (j = 0; j < ncopies; j++)
4574 {
4575 /* Handle uses. */
4576 if (j == 0)
4577 {
4578 if (scalar_shift_arg)
4579 {
4580 /* Vector shl and shr insn patterns can be defined with scalar
4581 operand 2 (shift operand). In this case, use constant or loop
4582 invariant op1 directly, without extending it to vector mode
4583 first. */
4584 optab_op2_mode = insn_data[icode].operand[2].mode;
4585 if (!VECTOR_MODE_P (optab_op2_mode))
4586 {
4587 if (dump_enabled_p ())
4588 dump_printf_loc (MSG_NOTE, vect_location,
4589 "operand 1 using scalar mode.\n");
4590 vec_oprnd1 = op1;
4591 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
4592 vec_oprnds1.quick_push (vec_oprnd1);
4593 if (slp_node)
4594 {
4595 /* Store vec_oprnd1 for every vector stmt to be created
4596 for SLP_NODE. We check during the analysis that all
4597 the shift arguments are the same.
4598 TODO: Allow different constants for different vector
4599 stmts generated for an SLP instance. */
4600 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
4601 vec_oprnds1.quick_push (vec_oprnd1);
4602 }
4603 }
4604 }
4605
4606 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
4607 (a special case for certain kind of vector shifts); otherwise,
4608 operand 1 should be of a vector type (the usual case). */
4609 if (vec_oprnd1)
4610 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4611 slp_node, -1);
4612 else
4613 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4614 slp_node, -1);
4615 }
4616 else
4617 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4618
4619 /* Arguments are ready. Create the new vector stmt. */
4620 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4621 {
4622 vop1 = vec_oprnds1[i];
4623 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
4624 new_temp = make_ssa_name (vec_dest, new_stmt);
4625 gimple_assign_set_lhs (new_stmt, new_temp);
4626 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4627 if (slp_node)
4628 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4629 }
4630
4631 if (slp_node)
4632 continue;
4633
4634 if (j == 0)
4635 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4636 else
4637 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4638 prev_stmt_info = vinfo_for_stmt (new_stmt);
4639 }
4640
4641 vec_oprnds0.release ();
4642 vec_oprnds1.release ();
4643
4644 return true;
4645 }
4646
4647
4648 /* Function vectorizable_operation.
4649
4650 Check if STMT performs a binary, unary or ternary operation that can
4651 be vectorized.
4652 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4653 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4654 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4655
4656 static bool
4657 vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi,
4658 gimple **vec_stmt, slp_tree slp_node)
4659 {
4660 tree vec_dest;
4661 tree scalar_dest;
4662 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
4663 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4664 tree vectype;
4665 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4666 enum tree_code code;
4667 machine_mode vec_mode;
4668 tree new_temp;
4669 int op_type;
4670 optab optab;
4671 bool target_support_p;
4672 gimple *def_stmt;
4673 enum vect_def_type dt[3]
4674 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
4675 gimple *new_stmt = NULL;
4676 stmt_vec_info prev_stmt_info;
4677 int nunits_in;
4678 int nunits_out;
4679 tree vectype_out;
4680 int ncopies;
4681 int j, i;
4682 vec<tree> vec_oprnds0 = vNULL;
4683 vec<tree> vec_oprnds1 = vNULL;
4684 vec<tree> vec_oprnds2 = vNULL;
4685 tree vop0, vop1, vop2;
4686 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
4687 vec_info *vinfo = stmt_info->vinfo;
4688 int vf;
4689
4690 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4691 return false;
4692
4693 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
4694 return false;
4695
4696 /* Is STMT a vectorizable binary/unary operation? */
4697 if (!is_gimple_assign (stmt))
4698 return false;
4699
4700 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4701 return false;
4702
4703 code = gimple_assign_rhs_code (stmt);
4704
4705 /* For pointer addition, we should use the normal plus for
4706 the vector addition. */
4707 if (code == POINTER_PLUS_EXPR)
4708 code = PLUS_EXPR;
4709
4710 /* Support only unary or binary operations. */
4711 op_type = TREE_CODE_LENGTH (code);
4712 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
4713 {
4714 if (dump_enabled_p ())
4715 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4716 "num. args = %d (not unary/binary/ternary op).\n",
4717 op_type);
4718 return false;
4719 }
4720
4721 scalar_dest = gimple_assign_lhs (stmt);
4722 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4723
4724 /* Most operations cannot handle bit-precision types without extra
4725 truncations. */
4726 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
4727 && (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4728 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4729 /* Exception are bitwise binary operations. */
4730 && code != BIT_IOR_EXPR
4731 && code != BIT_XOR_EXPR
4732 && code != BIT_AND_EXPR)
4733 {
4734 if (dump_enabled_p ())
4735 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4736 "bit-precision arithmetic not supported.\n");
4737 return false;
4738 }
4739
4740 op0 = gimple_assign_rhs1 (stmt);
4741 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
4742 {
4743 if (dump_enabled_p ())
4744 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4745 "use not simple.\n");
4746 return false;
4747 }
4748 /* If op0 is an external or constant def use a vector type with
4749 the same size as the output vector type. */
4750 if (!vectype)
4751 {
4752 /* For boolean type we cannot determine vectype by
4753 invariant value (don't know whether it is a vector
4754 of booleans or vector of integers). We use output
4755 vectype because operations on boolean don't change
4756 type. */
4757 if (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE)
4758 {
4759 if (TREE_CODE (TREE_TYPE (scalar_dest)) != BOOLEAN_TYPE)
4760 {
4761 if (dump_enabled_p ())
4762 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4763 "not supported operation on bool value.\n");
4764 return false;
4765 }
4766 vectype = vectype_out;
4767 }
4768 else
4769 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4770 }
4771 if (vec_stmt)
4772 gcc_assert (vectype);
4773 if (!vectype)
4774 {
4775 if (dump_enabled_p ())
4776 {
4777 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4778 "no vectype for scalar type ");
4779 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
4780 TREE_TYPE (op0));
4781 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4782 }
4783
4784 return false;
4785 }
4786
4787 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4788 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4789 if (nunits_out != nunits_in)
4790 return false;
4791
4792 if (op_type == binary_op || op_type == ternary_op)
4793 {
4794 op1 = gimple_assign_rhs2 (stmt);
4795 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]))
4796 {
4797 if (dump_enabled_p ())
4798 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4799 "use not simple.\n");
4800 return false;
4801 }
4802 }
4803 if (op_type == ternary_op)
4804 {
4805 op2 = gimple_assign_rhs3 (stmt);
4806 if (!vect_is_simple_use (op2, vinfo, &def_stmt, &dt[2]))
4807 {
4808 if (dump_enabled_p ())
4809 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4810 "use not simple.\n");
4811 return false;
4812 }
4813 }
4814
4815 if (loop_vinfo)
4816 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4817 else
4818 vf = 1;
4819
4820 /* Multiple types in SLP are handled by creating the appropriate number of
4821 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4822 case of SLP. */
4823 if (slp_node || PURE_SLP_STMT (stmt_info))
4824 ncopies = 1;
4825 else
4826 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4827
4828 gcc_assert (ncopies >= 1);
4829
4830 /* Shifts are handled in vectorizable_shift (). */
4831 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4832 || code == RROTATE_EXPR)
4833 return false;
4834
4835 /* Supportable by target? */
4836
4837 vec_mode = TYPE_MODE (vectype);
4838 if (code == MULT_HIGHPART_EXPR)
4839 target_support_p = can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype));
4840 else
4841 {
4842 optab = optab_for_tree_code (code, vectype, optab_default);
4843 if (!optab)
4844 {
4845 if (dump_enabled_p ())
4846 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4847 "no optab.\n");
4848 return false;
4849 }
4850 target_support_p = (optab_handler (optab, vec_mode)
4851 != CODE_FOR_nothing);
4852 }
4853
4854 if (!target_support_p)
4855 {
4856 if (dump_enabled_p ())
4857 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4858 "op not supported by target.\n");
4859 /* Check only during analysis. */
4860 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4861 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
4862 return false;
4863 if (dump_enabled_p ())
4864 dump_printf_loc (MSG_NOTE, vect_location,
4865 "proceeding using word mode.\n");
4866 }
4867
4868 /* Worthwhile without SIMD support? Check only during analysis. */
4869 if (!VECTOR_MODE_P (vec_mode)
4870 && !vec_stmt
4871 && vf < vect_min_worthwhile_factor (code))
4872 {
4873 if (dump_enabled_p ())
4874 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4875 "not worthwhile without SIMD support.\n");
4876 return false;
4877 }
4878
4879 if (!vec_stmt) /* transformation not required. */
4880 {
4881 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
4882 if (dump_enabled_p ())
4883 dump_printf_loc (MSG_NOTE, vect_location,
4884 "=== vectorizable_operation ===\n");
4885 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
4886 return true;
4887 }
4888
4889 /** Transform. **/
4890
4891 if (dump_enabled_p ())
4892 dump_printf_loc (MSG_NOTE, vect_location,
4893 "transform binary/unary operation.\n");
4894
4895 /* Handle def. */
4896 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4897
4898 /* In case the vectorization factor (VF) is bigger than the number
4899 of elements that we can fit in a vectype (nunits), we have to generate
4900 more than one vector stmt - i.e - we need to "unroll" the
4901 vector stmt by a factor VF/nunits. In doing so, we record a pointer
4902 from one copy of the vector stmt to the next, in the field
4903 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
4904 stages to find the correct vector defs to be used when vectorizing
4905 stmts that use the defs of the current stmt. The example below
4906 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
4907 we need to create 4 vectorized stmts):
4908
4909 before vectorization:
4910 RELATED_STMT VEC_STMT
4911 S1: x = memref - -
4912 S2: z = x + 1 - -
4913
4914 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
4915 there):
4916 RELATED_STMT VEC_STMT
4917 VS1_0: vx0 = memref0 VS1_1 -
4918 VS1_1: vx1 = memref1 VS1_2 -
4919 VS1_2: vx2 = memref2 VS1_3 -
4920 VS1_3: vx3 = memref3 - -
4921 S1: x = load - VS1_0
4922 S2: z = x + 1 - -
4923
4924 step2: vectorize stmt S2 (done here):
4925 To vectorize stmt S2 we first need to find the relevant vector
4926 def for the first operand 'x'. This is, as usual, obtained from
4927 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
4928 that defines 'x' (S1). This way we find the stmt VS1_0, and the
4929 relevant vector def 'vx0'. Having found 'vx0' we can generate
4930 the vector stmt VS2_0, and as usual, record it in the
4931 STMT_VINFO_VEC_STMT of stmt S2.
4932 When creating the second copy (VS2_1), we obtain the relevant vector
4933 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
4934 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
4935 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
4936 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
4937 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
4938 chain of stmts and pointers:
4939 RELATED_STMT VEC_STMT
4940 VS1_0: vx0 = memref0 VS1_1 -
4941 VS1_1: vx1 = memref1 VS1_2 -
4942 VS1_2: vx2 = memref2 VS1_3 -
4943 VS1_3: vx3 = memref3 - -
4944 S1: x = load - VS1_0
4945 VS2_0: vz0 = vx0 + v1 VS2_1 -
4946 VS2_1: vz1 = vx1 + v1 VS2_2 -
4947 VS2_2: vz2 = vx2 + v1 VS2_3 -
4948 VS2_3: vz3 = vx3 + v1 - -
4949 S2: z = x + 1 - VS2_0 */
4950
4951 prev_stmt_info = NULL;
4952 for (j = 0; j < ncopies; j++)
4953 {
4954 /* Handle uses. */
4955 if (j == 0)
4956 {
4957 if (op_type == binary_op || op_type == ternary_op)
4958 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
4959 slp_node, -1);
4960 else
4961 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4962 slp_node, -1);
4963 if (op_type == ternary_op)
4964 {
4965 vec_oprnds2.create (1);
4966 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
4967 stmt));
4968 }
4969 }
4970 else
4971 {
4972 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4973 if (op_type == ternary_op)
4974 {
4975 tree vec_oprnd = vec_oprnds2.pop ();
4976 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
4977 vec_oprnd));
4978 }
4979 }
4980
4981 /* Arguments are ready. Create the new vector stmt. */
4982 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4983 {
4984 vop1 = ((op_type == binary_op || op_type == ternary_op)
4985 ? vec_oprnds1[i] : NULL_TREE);
4986 vop2 = ((op_type == ternary_op)
4987 ? vec_oprnds2[i] : NULL_TREE);
4988 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
4989 new_temp = make_ssa_name (vec_dest, new_stmt);
4990 gimple_assign_set_lhs (new_stmt, new_temp);
4991 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4992 if (slp_node)
4993 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4994 }
4995
4996 if (slp_node)
4997 continue;
4998
4999 if (j == 0)
5000 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5001 else
5002 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5003 prev_stmt_info = vinfo_for_stmt (new_stmt);
5004 }
5005
5006 vec_oprnds0.release ();
5007 vec_oprnds1.release ();
5008 vec_oprnds2.release ();
5009
5010 return true;
5011 }
5012
5013 /* A helper function to ensure data reference DR's base alignment
5014 for STMT_INFO. */
5015
5016 static void
5017 ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
5018 {
5019 if (!dr->aux)
5020 return;
5021
5022 if (DR_VECT_AUX (dr)->base_misaligned)
5023 {
5024 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5025 tree base_decl = DR_VECT_AUX (dr)->base_decl;
5026
5027 if (decl_in_symtab_p (base_decl))
5028 symtab_node::get (base_decl)->increase_alignment (TYPE_ALIGN (vectype));
5029 else
5030 {
5031 DECL_ALIGN (base_decl) = TYPE_ALIGN (vectype);
5032 DECL_USER_ALIGN (base_decl) = 1;
5033 }
5034 DR_VECT_AUX (dr)->base_misaligned = false;
5035 }
5036 }
5037
5038
5039 /* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
5040 reversal of the vector elements. If that is impossible to do,
5041 returns NULL. */
5042
5043 static tree
5044 perm_mask_for_reverse (tree vectype)
5045 {
5046 int i, nunits;
5047 unsigned char *sel;
5048
5049 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5050 sel = XALLOCAVEC (unsigned char, nunits);
5051
5052 for (i = 0; i < nunits; ++i)
5053 sel[i] = nunits - 1 - i;
5054
5055 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5056 return NULL_TREE;
5057 return vect_gen_perm_mask_checked (vectype, sel);
5058 }
5059
5060 /* Function vectorizable_store.
5061
5062 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
5063 can be vectorized.
5064 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5065 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5066 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5067
5068 static bool
5069 vectorizable_store (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
5070 slp_tree slp_node)
5071 {
5072 tree scalar_dest;
5073 tree data_ref;
5074 tree op;
5075 tree vec_oprnd = NULL_TREE;
5076 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5077 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
5078 tree elem_type;
5079 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5080 struct loop *loop = NULL;
5081 machine_mode vec_mode;
5082 tree dummy;
5083 enum dr_alignment_support alignment_support_scheme;
5084 gimple *def_stmt;
5085 enum vect_def_type dt;
5086 stmt_vec_info prev_stmt_info = NULL;
5087 tree dataref_ptr = NULL_TREE;
5088 tree dataref_offset = NULL_TREE;
5089 gimple *ptr_incr = NULL;
5090 int ncopies;
5091 int j;
5092 gimple *next_stmt, *first_stmt = NULL;
5093 bool grouped_store = false;
5094 bool store_lanes_p = false;
5095 unsigned int group_size, i;
5096 vec<tree> dr_chain = vNULL;
5097 vec<tree> oprnds = vNULL;
5098 vec<tree> result_chain = vNULL;
5099 bool inv_p;
5100 bool negative = false;
5101 tree offset = NULL_TREE;
5102 vec<tree> vec_oprnds = vNULL;
5103 bool slp = (slp_node != NULL);
5104 unsigned int vec_num;
5105 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
5106 vec_info *vinfo = stmt_info->vinfo;
5107 tree aggr_type;
5108 tree scatter_base = NULL_TREE, scatter_off = NULL_TREE;
5109 tree scatter_off_vectype = NULL_TREE, scatter_decl = NULL_TREE;
5110 int scatter_scale = 1;
5111 enum vect_def_type scatter_idx_dt = vect_unknown_def_type;
5112 enum vect_def_type scatter_src_dt = vect_unknown_def_type;
5113 gimple *new_stmt;
5114
5115 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5116 return false;
5117
5118 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
5119 return false;
5120
5121 /* Is vectorizable store? */
5122
5123 if (!is_gimple_assign (stmt))
5124 return false;
5125
5126 scalar_dest = gimple_assign_lhs (stmt);
5127 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
5128 && is_pattern_stmt_p (stmt_info))
5129 scalar_dest = TREE_OPERAND (scalar_dest, 0);
5130 if (TREE_CODE (scalar_dest) != ARRAY_REF
5131 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
5132 && TREE_CODE (scalar_dest) != INDIRECT_REF
5133 && TREE_CODE (scalar_dest) != COMPONENT_REF
5134 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
5135 && TREE_CODE (scalar_dest) != REALPART_EXPR
5136 && TREE_CODE (scalar_dest) != MEM_REF)
5137 return false;
5138
5139 gcc_assert (gimple_assign_single_p (stmt));
5140
5141 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5142 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5143
5144 if (loop_vinfo)
5145 loop = LOOP_VINFO_LOOP (loop_vinfo);
5146
5147 /* Multiple types in SLP are handled by creating the appropriate number of
5148 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5149 case of SLP. */
5150 if (slp || PURE_SLP_STMT (stmt_info))
5151 ncopies = 1;
5152 else
5153 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5154
5155 gcc_assert (ncopies >= 1);
5156
5157 /* FORNOW. This restriction should be relaxed. */
5158 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
5159 {
5160 if (dump_enabled_p ())
5161 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5162 "multiple types in nested loop.\n");
5163 return false;
5164 }
5165
5166 op = gimple_assign_rhs1 (stmt);
5167 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt))
5168 {
5169 if (dump_enabled_p ())
5170 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5171 "use not simple.\n");
5172 return false;
5173 }
5174
5175 elem_type = TREE_TYPE (vectype);
5176 vec_mode = TYPE_MODE (vectype);
5177
5178 /* FORNOW. In some cases can vectorize even if data-type not supported
5179 (e.g. - array initialization with 0). */
5180 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
5181 return false;
5182
5183 if (!STMT_VINFO_DATA_REF (stmt_info))
5184 return false;
5185
5186 if (!STMT_VINFO_STRIDED_P (stmt_info))
5187 {
5188 negative =
5189 tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
5190 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
5191 size_zero_node) < 0;
5192 if (negative && ncopies > 1)
5193 {
5194 if (dump_enabled_p ())
5195 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5196 "multiple types with negative step.\n");
5197 return false;
5198 }
5199 if (negative)
5200 {
5201 gcc_assert (!grouped_store);
5202 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5203 if (alignment_support_scheme != dr_aligned
5204 && alignment_support_scheme != dr_unaligned_supported)
5205 {
5206 if (dump_enabled_p ())
5207 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5208 "negative step but alignment required.\n");
5209 return false;
5210 }
5211 if (dt != vect_constant_def
5212 && dt != vect_external_def
5213 && !perm_mask_for_reverse (vectype))
5214 {
5215 if (dump_enabled_p ())
5216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5217 "negative step and reversing not supported.\n");
5218 return false;
5219 }
5220 }
5221 }
5222
5223 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5224 {
5225 grouped_store = true;
5226 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
5227 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5228 if (!slp
5229 && !PURE_SLP_STMT (stmt_info)
5230 && !STMT_VINFO_STRIDED_P (stmt_info))
5231 {
5232 if (vect_store_lanes_supported (vectype, group_size))
5233 store_lanes_p = true;
5234 else if (!vect_grouped_store_supported (vectype, group_size))
5235 return false;
5236 }
5237
5238 if (STMT_VINFO_STRIDED_P (stmt_info)
5239 && (slp || PURE_SLP_STMT (stmt_info))
5240 && (group_size > nunits
5241 || nunits % group_size != 0))
5242 {
5243 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5244 "unhandled strided group store\n");
5245 return false;
5246 }
5247
5248 if (first_stmt == stmt)
5249 {
5250 /* STMT is the leader of the group. Check the operands of all the
5251 stmts of the group. */
5252 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
5253 while (next_stmt)
5254 {
5255 gcc_assert (gimple_assign_single_p (next_stmt));
5256 op = gimple_assign_rhs1 (next_stmt);
5257 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt))
5258 {
5259 if (dump_enabled_p ())
5260 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5261 "use not simple.\n");
5262 return false;
5263 }
5264 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5265 }
5266 }
5267 }
5268
5269 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
5270 {
5271 gimple *def_stmt;
5272 scatter_decl = vect_check_gather_scatter (stmt, loop_vinfo, &scatter_base,
5273 &scatter_off, &scatter_scale);
5274 gcc_assert (scatter_decl);
5275 if (!vect_is_simple_use (scatter_off, vinfo, &def_stmt, &scatter_idx_dt,
5276 &scatter_off_vectype))
5277 {
5278 if (dump_enabled_p ())
5279 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5280 "scatter index use not simple.");
5281 return false;
5282 }
5283 }
5284
5285 if (!vec_stmt) /* transformation not required. */
5286 {
5287 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
5288 /* The SLP costs are calculated during SLP analysis. */
5289 if (!PURE_SLP_STMT (stmt_info))
5290 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
5291 NULL, NULL, NULL);
5292 return true;
5293 }
5294
5295 /** Transform. **/
5296
5297 ensure_base_align (stmt_info, dr);
5298
5299 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
5300 {
5301 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE, op, src;
5302 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (scatter_decl));
5303 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5304 tree ptr, mask, var, scale, perm_mask = NULL_TREE;
5305 edge pe = loop_preheader_edge (loop);
5306 gimple_seq seq;
5307 basic_block new_bb;
5308 enum { NARROW, NONE, WIDEN } modifier;
5309 int scatter_off_nunits = TYPE_VECTOR_SUBPARTS (scatter_off_vectype);
5310
5311 if (nunits == (unsigned int) scatter_off_nunits)
5312 modifier = NONE;
5313 else if (nunits == (unsigned int) scatter_off_nunits / 2)
5314 {
5315 unsigned char *sel = XALLOCAVEC (unsigned char, scatter_off_nunits);
5316 modifier = WIDEN;
5317
5318 for (i = 0; i < (unsigned int) scatter_off_nunits; ++i)
5319 sel[i] = i | nunits;
5320
5321 perm_mask = vect_gen_perm_mask_checked (scatter_off_vectype, sel);
5322 gcc_assert (perm_mask != NULL_TREE);
5323 }
5324 else if (nunits == (unsigned int) scatter_off_nunits * 2)
5325 {
5326 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5327 modifier = NARROW;
5328
5329 for (i = 0; i < (unsigned int) nunits; ++i)
5330 sel[i] = i | scatter_off_nunits;
5331
5332 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
5333 gcc_assert (perm_mask != NULL_TREE);
5334 ncopies *= 2;
5335 }
5336 else
5337 gcc_unreachable ();
5338
5339 rettype = TREE_TYPE (TREE_TYPE (scatter_decl));
5340 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5341 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5342 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5343 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5344 scaletype = TREE_VALUE (arglist);
5345
5346 gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
5347 && TREE_CODE (rettype) == VOID_TYPE);
5348
5349 ptr = fold_convert (ptrtype, scatter_base);
5350 if (!is_gimple_min_invariant (ptr))
5351 {
5352 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5353 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5354 gcc_assert (!new_bb);
5355 }
5356
5357 /* Currently we support only unconditional scatter stores,
5358 so mask should be all ones. */
5359 mask = build_int_cst (masktype, -1);
5360 mask = vect_init_vector (stmt, mask, masktype, NULL);
5361
5362 scale = build_int_cst (scaletype, scatter_scale);
5363
5364 prev_stmt_info = NULL;
5365 for (j = 0; j < ncopies; ++j)
5366 {
5367 if (j == 0)
5368 {
5369 src = vec_oprnd1
5370 = vect_get_vec_def_for_operand (gimple_assign_rhs1 (stmt), stmt);
5371 op = vec_oprnd0
5372 = vect_get_vec_def_for_operand (scatter_off, stmt);
5373 }
5374 else if (modifier != NONE && (j & 1))
5375 {
5376 if (modifier == WIDEN)
5377 {
5378 src = vec_oprnd1
5379 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5380 op = permute_vec_elements (vec_oprnd0, vec_oprnd0, perm_mask,
5381 stmt, gsi);
5382 }
5383 else if (modifier == NARROW)
5384 {
5385 src = permute_vec_elements (vec_oprnd1, vec_oprnd1, perm_mask,
5386 stmt, gsi);
5387 op = vec_oprnd0
5388 = vect_get_vec_def_for_stmt_copy (scatter_idx_dt, vec_oprnd0);
5389 }
5390 else
5391 gcc_unreachable ();
5392 }
5393 else
5394 {
5395 src = vec_oprnd1
5396 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5397 op = vec_oprnd0
5398 = vect_get_vec_def_for_stmt_copy (scatter_idx_dt, vec_oprnd0);
5399 }
5400
5401 if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
5402 {
5403 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src))
5404 == TYPE_VECTOR_SUBPARTS (srctype));
5405 var = vect_get_new_ssa_name (srctype, vect_simple_var);
5406 src = build1 (VIEW_CONVERT_EXPR, srctype, src);
5407 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
5408 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5409 src = var;
5410 }
5411
5412 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
5413 {
5414 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
5415 == TYPE_VECTOR_SUBPARTS (idxtype));
5416 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
5417 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
5418 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
5419 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5420 op = var;
5421 }
5422
5423 new_stmt
5424 = gimple_build_call (scatter_decl, 5, ptr, mask, op, src, scale);
5425
5426 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5427
5428 if (prev_stmt_info == NULL)
5429 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5430 else
5431 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5432 prev_stmt_info = vinfo_for_stmt (new_stmt);
5433 }
5434 return true;
5435 }
5436
5437 if (grouped_store)
5438 {
5439 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5440 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
5441
5442 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
5443
5444 /* FORNOW */
5445 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
5446
5447 /* We vectorize all the stmts of the interleaving group when we
5448 reach the last stmt in the group. */
5449 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
5450 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
5451 && !slp)
5452 {
5453 *vec_stmt = NULL;
5454 return true;
5455 }
5456
5457 if (slp)
5458 {
5459 grouped_store = false;
5460 /* VEC_NUM is the number of vect stmts to be created for this
5461 group. */
5462 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5463 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
5464 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
5465 op = gimple_assign_rhs1 (first_stmt);
5466 }
5467 else
5468 /* VEC_NUM is the number of vect stmts to be created for this
5469 group. */
5470 vec_num = group_size;
5471 }
5472 else
5473 {
5474 first_stmt = stmt;
5475 first_dr = dr;
5476 group_size = vec_num = 1;
5477 }
5478
5479 if (dump_enabled_p ())
5480 dump_printf_loc (MSG_NOTE, vect_location,
5481 "transform store. ncopies = %d\n", ncopies);
5482
5483 if (STMT_VINFO_STRIDED_P (stmt_info))
5484 {
5485 gimple_stmt_iterator incr_gsi;
5486 bool insert_after;
5487 gimple *incr;
5488 tree offvar;
5489 tree ivstep;
5490 tree running_off;
5491 gimple_seq stmts = NULL;
5492 tree stride_base, stride_step, alias_off;
5493 tree vec_oprnd;
5494 unsigned int g;
5495
5496 gcc_assert (!nested_in_vect_loop_p (loop, stmt));
5497
5498 stride_base
5499 = fold_build_pointer_plus
5500 (unshare_expr (DR_BASE_ADDRESS (first_dr)),
5501 size_binop (PLUS_EXPR,
5502 convert_to_ptrofftype (unshare_expr (DR_OFFSET (first_dr))),
5503 convert_to_ptrofftype (DR_INIT(first_dr))));
5504 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (first_dr)));
5505
5506 /* For a store with loop-invariant (but other than power-of-2)
5507 stride (i.e. not a grouped access) like so:
5508
5509 for (i = 0; i < n; i += stride)
5510 array[i] = ...;
5511
5512 we generate a new induction variable and new stores from
5513 the components of the (vectorized) rhs:
5514
5515 for (j = 0; ; j += VF*stride)
5516 vectemp = ...;
5517 tmp1 = vectemp[0];
5518 array[j] = tmp1;
5519 tmp2 = vectemp[1];
5520 array[j + stride] = tmp2;
5521 ...
5522 */
5523
5524 unsigned nstores = nunits;
5525 tree ltype = elem_type;
5526 if (slp)
5527 {
5528 nstores = nunits / group_size;
5529 if (group_size < nunits)
5530 ltype = build_vector_type (elem_type, group_size);
5531 else
5532 ltype = vectype;
5533 ltype = build_aligned_type (ltype, TYPE_ALIGN (elem_type));
5534 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5535 group_size = 1;
5536 }
5537
5538 ivstep = stride_step;
5539 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
5540 build_int_cst (TREE_TYPE (ivstep),
5541 ncopies * nstores));
5542
5543 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5544
5545 create_iv (stride_base, ivstep, NULL,
5546 loop, &incr_gsi, insert_after,
5547 &offvar, NULL);
5548 incr = gsi_stmt (incr_gsi);
5549 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
5550
5551 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
5552 if (stmts)
5553 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5554
5555 prev_stmt_info = NULL;
5556 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (first_dr)), 0);
5557 next_stmt = first_stmt;
5558 for (g = 0; g < group_size; g++)
5559 {
5560 running_off = offvar;
5561 if (g)
5562 {
5563 tree size = TYPE_SIZE_UNIT (ltype);
5564 tree pos = fold_build2 (MULT_EXPR, sizetype, size_int (g),
5565 size);
5566 tree newoff = copy_ssa_name (running_off, NULL);
5567 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
5568 running_off, pos);
5569 vect_finish_stmt_generation (stmt, incr, gsi);
5570 running_off = newoff;
5571 }
5572 for (j = 0; j < ncopies; j++)
5573 {
5574 /* We've set op and dt above, from gimple_assign_rhs1(stmt),
5575 and first_stmt == stmt. */
5576 if (j == 0)
5577 {
5578 if (slp)
5579 {
5580 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds, NULL,
5581 slp_node, -1);
5582 vec_oprnd = vec_oprnds[0];
5583 }
5584 else
5585 {
5586 gcc_assert (gimple_assign_single_p (next_stmt));
5587 op = gimple_assign_rhs1 (next_stmt);
5588 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
5589 }
5590 }
5591 else
5592 {
5593 if (slp)
5594 vec_oprnd = vec_oprnds[j];
5595 else
5596 {
5597 vect_is_simple_use (vec_oprnd, vinfo, &def_stmt, &dt);
5598 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
5599 }
5600 }
5601
5602 for (i = 0; i < nstores; i++)
5603 {
5604 tree newref, newoff;
5605 gimple *incr, *assign;
5606 tree size = TYPE_SIZE (ltype);
5607 /* Extract the i'th component. */
5608 tree pos = fold_build2 (MULT_EXPR, bitsizetype,
5609 bitsize_int (i), size);
5610 tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
5611 size, pos);
5612
5613 elem = force_gimple_operand_gsi (gsi, elem, true,
5614 NULL_TREE, true,
5615 GSI_SAME_STMT);
5616
5617 newref = build2 (MEM_REF, ltype,
5618 running_off, alias_off);
5619
5620 /* And store it to *running_off. */
5621 assign = gimple_build_assign (newref, elem);
5622 vect_finish_stmt_generation (stmt, assign, gsi);
5623
5624 newoff = copy_ssa_name (running_off, NULL);
5625 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
5626 running_off, stride_step);
5627 vect_finish_stmt_generation (stmt, incr, gsi);
5628
5629 running_off = newoff;
5630 if (g == group_size - 1
5631 && !slp)
5632 {
5633 if (j == 0 && i == 0)
5634 STMT_VINFO_VEC_STMT (stmt_info)
5635 = *vec_stmt = assign;
5636 else
5637 STMT_VINFO_RELATED_STMT (prev_stmt_info) = assign;
5638 prev_stmt_info = vinfo_for_stmt (assign);
5639 }
5640 }
5641 }
5642 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5643 }
5644 return true;
5645 }
5646
5647 dr_chain.create (group_size);
5648 oprnds.create (group_size);
5649
5650 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
5651 gcc_assert (alignment_support_scheme);
5652 /* Targets with store-lane instructions must not require explicit
5653 realignment. */
5654 gcc_assert (!store_lanes_p
5655 || alignment_support_scheme == dr_aligned
5656 || alignment_support_scheme == dr_unaligned_supported);
5657
5658 if (negative)
5659 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
5660
5661 if (store_lanes_p)
5662 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5663 else
5664 aggr_type = vectype;
5665
5666 /* In case the vectorization factor (VF) is bigger than the number
5667 of elements that we can fit in a vectype (nunits), we have to generate
5668 more than one vector stmt - i.e - we need to "unroll" the
5669 vector stmt by a factor VF/nunits. For more details see documentation in
5670 vect_get_vec_def_for_copy_stmt. */
5671
5672 /* In case of interleaving (non-unit grouped access):
5673
5674 S1: &base + 2 = x2
5675 S2: &base = x0
5676 S3: &base + 1 = x1
5677 S4: &base + 3 = x3
5678
5679 We create vectorized stores starting from base address (the access of the
5680 first stmt in the chain (S2 in the above example), when the last store stmt
5681 of the chain (S4) is reached:
5682
5683 VS1: &base = vx2
5684 VS2: &base + vec_size*1 = vx0
5685 VS3: &base + vec_size*2 = vx1
5686 VS4: &base + vec_size*3 = vx3
5687
5688 Then permutation statements are generated:
5689
5690 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
5691 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
5692 ...
5693
5694 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5695 (the order of the data-refs in the output of vect_permute_store_chain
5696 corresponds to the order of scalar stmts in the interleaving chain - see
5697 the documentation of vect_permute_store_chain()).
5698
5699 In case of both multiple types and interleaving, above vector stores and
5700 permutation stmts are created for every copy. The result vector stmts are
5701 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
5702 STMT_VINFO_RELATED_STMT for the next copies.
5703 */
5704
5705 prev_stmt_info = NULL;
5706 for (j = 0; j < ncopies; j++)
5707 {
5708
5709 if (j == 0)
5710 {
5711 if (slp)
5712 {
5713 /* Get vectorized arguments for SLP_NODE. */
5714 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
5715 NULL, slp_node, -1);
5716
5717 vec_oprnd = vec_oprnds[0];
5718 }
5719 else
5720 {
5721 /* For interleaved stores we collect vectorized defs for all the
5722 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
5723 used as an input to vect_permute_store_chain(), and OPRNDS as
5724 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
5725
5726 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5727 OPRNDS are of size 1. */
5728 next_stmt = first_stmt;
5729 for (i = 0; i < group_size; i++)
5730 {
5731 /* Since gaps are not supported for interleaved stores,
5732 GROUP_SIZE is the exact number of stmts in the chain.
5733 Therefore, NEXT_STMT can't be NULL_TREE. In case that
5734 there is no interleaving, GROUP_SIZE is 1, and only one
5735 iteration of the loop will be executed. */
5736 gcc_assert (next_stmt
5737 && gimple_assign_single_p (next_stmt));
5738 op = gimple_assign_rhs1 (next_stmt);
5739
5740 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
5741 dr_chain.quick_push (vec_oprnd);
5742 oprnds.quick_push (vec_oprnd);
5743 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5744 }
5745 }
5746
5747 /* We should have catched mismatched types earlier. */
5748 gcc_assert (useless_type_conversion_p (vectype,
5749 TREE_TYPE (vec_oprnd)));
5750 bool simd_lane_access_p
5751 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5752 if (simd_lane_access_p
5753 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5754 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5755 && integer_zerop (DR_OFFSET (first_dr))
5756 && integer_zerop (DR_INIT (first_dr))
5757 && alias_sets_conflict_p (get_alias_set (aggr_type),
5758 get_alias_set (DR_REF (first_dr))))
5759 {
5760 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5761 dataref_offset = build_int_cst (reference_alias_ptr_type
5762 (DR_REF (first_dr)), 0);
5763 inv_p = false;
5764 }
5765 else
5766 dataref_ptr
5767 = vect_create_data_ref_ptr (first_stmt, aggr_type,
5768 simd_lane_access_p ? loop : NULL,
5769 offset, &dummy, gsi, &ptr_incr,
5770 simd_lane_access_p, &inv_p);
5771 gcc_assert (bb_vinfo || !inv_p);
5772 }
5773 else
5774 {
5775 /* For interleaved stores we created vectorized defs for all the
5776 defs stored in OPRNDS in the previous iteration (previous copy).
5777 DR_CHAIN is then used as an input to vect_permute_store_chain(),
5778 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
5779 next copy.
5780 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
5781 OPRNDS are of size 1. */
5782 for (i = 0; i < group_size; i++)
5783 {
5784 op = oprnds[i];
5785 vect_is_simple_use (op, vinfo, &def_stmt, &dt);
5786 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
5787 dr_chain[i] = vec_oprnd;
5788 oprnds[i] = vec_oprnd;
5789 }
5790 if (dataref_offset)
5791 dataref_offset
5792 = int_const_binop (PLUS_EXPR, dataref_offset,
5793 TYPE_SIZE_UNIT (aggr_type));
5794 else
5795 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5796 TYPE_SIZE_UNIT (aggr_type));
5797 }
5798
5799 if (store_lanes_p)
5800 {
5801 tree vec_array;
5802
5803 /* Combine all the vectors into an array. */
5804 vec_array = create_vector_array (vectype, vec_num);
5805 for (i = 0; i < vec_num; i++)
5806 {
5807 vec_oprnd = dr_chain[i];
5808 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
5809 }
5810
5811 /* Emit:
5812 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
5813 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5814 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
5815 gimple_call_set_lhs (new_stmt, data_ref);
5816 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5817 }
5818 else
5819 {
5820 new_stmt = NULL;
5821 if (grouped_store)
5822 {
5823 if (j == 0)
5824 result_chain.create (group_size);
5825 /* Permute. */
5826 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
5827 &result_chain);
5828 }
5829
5830 next_stmt = first_stmt;
5831 for (i = 0; i < vec_num; i++)
5832 {
5833 unsigned align, misalign;
5834
5835 if (i > 0)
5836 /* Bump the vector pointer. */
5837 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5838 stmt, NULL_TREE);
5839
5840 if (slp)
5841 vec_oprnd = vec_oprnds[i];
5842 else if (grouped_store)
5843 /* For grouped stores vectorized defs are interleaved in
5844 vect_permute_store_chain(). */
5845 vec_oprnd = result_chain[i];
5846
5847 data_ref = fold_build2 (MEM_REF, TREE_TYPE (vec_oprnd),
5848 dataref_ptr,
5849 dataref_offset
5850 ? dataref_offset
5851 : build_int_cst (reference_alias_ptr_type
5852 (DR_REF (first_dr)), 0));
5853 align = TYPE_ALIGN_UNIT (vectype);
5854 if (aligned_access_p (first_dr))
5855 misalign = 0;
5856 else if (DR_MISALIGNMENT (first_dr) == -1)
5857 {
5858 if (DR_VECT_AUX (first_dr)->base_element_aligned)
5859 align = TYPE_ALIGN_UNIT (elem_type);
5860 else
5861 align = get_object_alignment (DR_REF (first_dr))
5862 / BITS_PER_UNIT;
5863 misalign = 0;
5864 TREE_TYPE (data_ref)
5865 = build_aligned_type (TREE_TYPE (data_ref),
5866 align * BITS_PER_UNIT);
5867 }
5868 else
5869 {
5870 TREE_TYPE (data_ref)
5871 = build_aligned_type (TREE_TYPE (data_ref),
5872 TYPE_ALIGN (elem_type));
5873 misalign = DR_MISALIGNMENT (first_dr);
5874 }
5875 if (dataref_offset == NULL_TREE
5876 && TREE_CODE (dataref_ptr) == SSA_NAME)
5877 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
5878 misalign);
5879
5880 if (negative
5881 && dt != vect_constant_def
5882 && dt != vect_external_def)
5883 {
5884 tree perm_mask = perm_mask_for_reverse (vectype);
5885 tree perm_dest
5886 = vect_create_destination_var (gimple_assign_rhs1 (stmt),
5887 vectype);
5888 tree new_temp = make_ssa_name (perm_dest);
5889
5890 /* Generate the permute statement. */
5891 gimple *perm_stmt
5892 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
5893 vec_oprnd, perm_mask);
5894 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5895
5896 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
5897 vec_oprnd = new_temp;
5898 }
5899
5900 /* Arguments are ready. Create the new vector stmt. */
5901 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
5902 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5903
5904 if (slp)
5905 continue;
5906
5907 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
5908 if (!next_stmt)
5909 break;
5910 }
5911 }
5912 if (!slp)
5913 {
5914 if (j == 0)
5915 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5916 else
5917 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5918 prev_stmt_info = vinfo_for_stmt (new_stmt);
5919 }
5920 }
5921
5922 dr_chain.release ();
5923 oprnds.release ();
5924 result_chain.release ();
5925 vec_oprnds.release ();
5926
5927 return true;
5928 }
5929
5930 /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
5931 VECTOR_CST mask. No checks are made that the target platform supports the
5932 mask, so callers may wish to test can_vec_perm_p separately, or use
5933 vect_gen_perm_mask_checked. */
5934
5935 tree
5936 vect_gen_perm_mask_any (tree vectype, const unsigned char *sel)
5937 {
5938 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
5939 int i, nunits;
5940
5941 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5942
5943 mask_elt_type = lang_hooks.types.type_for_mode
5944 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
5945 mask_type = get_vectype_for_scalar_type (mask_elt_type);
5946
5947 mask_elts = XALLOCAVEC (tree, nunits);
5948 for (i = nunits - 1; i >= 0; i--)
5949 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
5950 mask_vec = build_vector (mask_type, mask_elts);
5951
5952 return mask_vec;
5953 }
5954
5955 /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_p,
5956 i.e. that the target supports the pattern _for arbitrary input vectors_. */
5957
5958 tree
5959 vect_gen_perm_mask_checked (tree vectype, const unsigned char *sel)
5960 {
5961 gcc_assert (can_vec_perm_p (TYPE_MODE (vectype), false, sel));
5962 return vect_gen_perm_mask_any (vectype, sel);
5963 }
5964
5965 /* Given a vector variable X and Y, that was generated for the scalar
5966 STMT, generate instructions to permute the vector elements of X and Y
5967 using permutation mask MASK_VEC, insert them at *GSI and return the
5968 permuted vector variable. */
5969
5970 static tree
5971 permute_vec_elements (tree x, tree y, tree mask_vec, gimple *stmt,
5972 gimple_stmt_iterator *gsi)
5973 {
5974 tree vectype = TREE_TYPE (x);
5975 tree perm_dest, data_ref;
5976 gimple *perm_stmt;
5977
5978 perm_dest = vect_create_destination_var (gimple_get_lhs (stmt), vectype);
5979 data_ref = make_ssa_name (perm_dest);
5980
5981 /* Generate the permute statement. */
5982 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
5983 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
5984
5985 return data_ref;
5986 }
5987
5988 /* Hoist the definitions of all SSA uses on STMT out of the loop LOOP,
5989 inserting them on the loops preheader edge. Returns true if we
5990 were successful in doing so (and thus STMT can be moved then),
5991 otherwise returns false. */
5992
5993 static bool
5994 hoist_defs_of_uses (gimple *stmt, struct loop *loop)
5995 {
5996 ssa_op_iter i;
5997 tree op;
5998 bool any = false;
5999
6000 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6001 {
6002 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6003 if (!gimple_nop_p (def_stmt)
6004 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6005 {
6006 /* Make sure we don't need to recurse. While we could do
6007 so in simple cases when there are more complex use webs
6008 we don't have an easy way to preserve stmt order to fulfil
6009 dependencies within them. */
6010 tree op2;
6011 ssa_op_iter i2;
6012 if (gimple_code (def_stmt) == GIMPLE_PHI)
6013 return false;
6014 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
6015 {
6016 gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
6017 if (!gimple_nop_p (def_stmt2)
6018 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
6019 return false;
6020 }
6021 any = true;
6022 }
6023 }
6024
6025 if (!any)
6026 return true;
6027
6028 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6029 {
6030 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6031 if (!gimple_nop_p (def_stmt)
6032 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6033 {
6034 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
6035 gsi_remove (&gsi, false);
6036 gsi_insert_on_edge_immediate (loop_preheader_edge (loop), def_stmt);
6037 }
6038 }
6039
6040 return true;
6041 }
6042
6043 /* vectorizable_load.
6044
6045 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
6046 can be vectorized.
6047 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
6048 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
6049 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6050
6051 static bool
6052 vectorizable_load (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
6053 slp_tree slp_node, slp_instance slp_node_instance)
6054 {
6055 tree scalar_dest;
6056 tree vec_dest = NULL;
6057 tree data_ref = NULL;
6058 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6059 stmt_vec_info prev_stmt_info;
6060 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6061 struct loop *loop = NULL;
6062 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
6063 bool nested_in_vect_loop = false;
6064 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
6065 tree elem_type;
6066 tree new_temp;
6067 machine_mode mode;
6068 gimple *new_stmt = NULL;
6069 tree dummy;
6070 enum dr_alignment_support alignment_support_scheme;
6071 tree dataref_ptr = NULL_TREE;
6072 tree dataref_offset = NULL_TREE;
6073 gimple *ptr_incr = NULL;
6074 int ncopies;
6075 int i, j, group_size = -1, group_gap_adj;
6076 tree msq = NULL_TREE, lsq;
6077 tree offset = NULL_TREE;
6078 tree byte_offset = NULL_TREE;
6079 tree realignment_token = NULL_TREE;
6080 gphi *phi = NULL;
6081 vec<tree> dr_chain = vNULL;
6082 bool grouped_load = false;
6083 bool load_lanes_p = false;
6084 gimple *first_stmt;
6085 bool inv_p;
6086 bool negative = false;
6087 bool compute_in_loop = false;
6088 struct loop *at_loop;
6089 int vec_num;
6090 bool slp = (slp_node != NULL);
6091 bool slp_perm = false;
6092 enum tree_code code;
6093 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6094 int vf;
6095 tree aggr_type;
6096 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
6097 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
6098 int gather_scale = 1;
6099 enum vect_def_type gather_dt = vect_unknown_def_type;
6100 vec_info *vinfo = stmt_info->vinfo;
6101
6102 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6103 return false;
6104
6105 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
6106 return false;
6107
6108 /* Is vectorizable load? */
6109 if (!is_gimple_assign (stmt))
6110 return false;
6111
6112 scalar_dest = gimple_assign_lhs (stmt);
6113 if (TREE_CODE (scalar_dest) != SSA_NAME)
6114 return false;
6115
6116 code = gimple_assign_rhs_code (stmt);
6117 if (code != ARRAY_REF
6118 && code != BIT_FIELD_REF
6119 && code != INDIRECT_REF
6120 && code != COMPONENT_REF
6121 && code != IMAGPART_EXPR
6122 && code != REALPART_EXPR
6123 && code != MEM_REF
6124 && TREE_CODE_CLASS (code) != tcc_declaration)
6125 return false;
6126
6127 if (!STMT_VINFO_DATA_REF (stmt_info))
6128 return false;
6129
6130 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6131 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6132
6133 if (loop_vinfo)
6134 {
6135 loop = LOOP_VINFO_LOOP (loop_vinfo);
6136 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
6137 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6138 }
6139 else
6140 vf = 1;
6141
6142 /* Multiple types in SLP are handled by creating the appropriate number of
6143 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6144 case of SLP. */
6145 if (slp || PURE_SLP_STMT (stmt_info))
6146 ncopies = 1;
6147 else
6148 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6149
6150 gcc_assert (ncopies >= 1);
6151
6152 /* FORNOW. This restriction should be relaxed. */
6153 if (nested_in_vect_loop && ncopies > 1)
6154 {
6155 if (dump_enabled_p ())
6156 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6157 "multiple types in nested loop.\n");
6158 return false;
6159 }
6160
6161 /* Invalidate assumptions made by dependence analysis when vectorization
6162 on the unrolled body effectively re-orders stmts. */
6163 if (ncopies > 1
6164 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6165 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6166 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6167 {
6168 if (dump_enabled_p ())
6169 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6170 "cannot perform implicit CSE when unrolling "
6171 "with negative dependence distance\n");
6172 return false;
6173 }
6174
6175 elem_type = TREE_TYPE (vectype);
6176 mode = TYPE_MODE (vectype);
6177
6178 /* FORNOW. In some cases can vectorize even if data-type not supported
6179 (e.g. - data copies). */
6180 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
6181 {
6182 if (dump_enabled_p ())
6183 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6184 "Aligned load, but unsupported type.\n");
6185 return false;
6186 }
6187
6188 /* Check if the load is a part of an interleaving chain. */
6189 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
6190 {
6191 grouped_load = true;
6192 /* FORNOW */
6193 gcc_assert (!nested_in_vect_loop && !STMT_VINFO_GATHER_SCATTER_P (stmt_info));
6194
6195 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6196
6197 /* If this is single-element interleaving with an element distance
6198 that leaves unused vector loads around punt - we at least create
6199 very sub-optimal code in that case (and blow up memory,
6200 see PR65518). */
6201 if (first_stmt == stmt
6202 && !GROUP_NEXT_ELEMENT (stmt_info)
6203 && GROUP_SIZE (stmt_info) > TYPE_VECTOR_SUBPARTS (vectype))
6204 {
6205 if (dump_enabled_p ())
6206 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6207 "single-element interleaving not supported "
6208 "for not adjacent vector loads\n");
6209 return false;
6210 }
6211
6212 if (slp && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
6213 slp_perm = true;
6214
6215 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6216 if (!slp
6217 && !PURE_SLP_STMT (stmt_info)
6218 && !STMT_VINFO_STRIDED_P (stmt_info))
6219 {
6220 if (vect_load_lanes_supported (vectype, group_size))
6221 load_lanes_p = true;
6222 else if (!vect_grouped_load_supported (vectype, group_size))
6223 return false;
6224 }
6225
6226 /* Invalidate assumptions made by dependence analysis when vectorization
6227 on the unrolled body effectively re-orders stmts. */
6228 if (!PURE_SLP_STMT (stmt_info)
6229 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6230 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6231 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6232 {
6233 if (dump_enabled_p ())
6234 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6235 "cannot perform implicit CSE when performing "
6236 "group loads with negative dependence distance\n");
6237 return false;
6238 }
6239
6240 /* Similarly when the stmt is a load that is both part of a SLP
6241 instance and a loop vectorized stmt via the same-dr mechanism
6242 we have to give up. */
6243 if (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)
6244 && (STMT_SLP_TYPE (stmt_info)
6245 != STMT_SLP_TYPE (vinfo_for_stmt
6246 (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)))))
6247 {
6248 if (dump_enabled_p ())
6249 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6250 "conflicting SLP types for CSEd load\n");
6251 return false;
6252 }
6253 }
6254
6255
6256 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
6257 {
6258 gimple *def_stmt;
6259 gather_decl = vect_check_gather_scatter (stmt, loop_vinfo, &gather_base,
6260 &gather_off, &gather_scale);
6261 gcc_assert (gather_decl);
6262 if (!vect_is_simple_use (gather_off, vinfo, &def_stmt, &gather_dt,
6263 &gather_off_vectype))
6264 {
6265 if (dump_enabled_p ())
6266 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6267 "gather index use not simple.\n");
6268 return false;
6269 }
6270 }
6271 else if (STMT_VINFO_STRIDED_P (stmt_info))
6272 {
6273 if ((grouped_load
6274 && (slp || PURE_SLP_STMT (stmt_info)))
6275 && (group_size > nunits
6276 || nunits % group_size != 0))
6277 {
6278 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6279 "unhandled strided group load\n");
6280 return false;
6281 }
6282 }
6283 else
6284 {
6285 negative = tree_int_cst_compare (nested_in_vect_loop
6286 ? STMT_VINFO_DR_STEP (stmt_info)
6287 : DR_STEP (dr),
6288 size_zero_node) < 0;
6289 if (negative && ncopies > 1)
6290 {
6291 if (dump_enabled_p ())
6292 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6293 "multiple types with negative step.\n");
6294 return false;
6295 }
6296
6297 if (negative)
6298 {
6299 if (grouped_load)
6300 {
6301 if (dump_enabled_p ())
6302 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6303 "negative step for group load not supported"
6304 "\n");
6305 return false;
6306 }
6307 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
6308 if (alignment_support_scheme != dr_aligned
6309 && alignment_support_scheme != dr_unaligned_supported)
6310 {
6311 if (dump_enabled_p ())
6312 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6313 "negative step but alignment required.\n");
6314 return false;
6315 }
6316 if (!perm_mask_for_reverse (vectype))
6317 {
6318 if (dump_enabled_p ())
6319 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6320 "negative step and reversing not supported."
6321 "\n");
6322 return false;
6323 }
6324 }
6325 }
6326
6327 if (!vec_stmt) /* transformation not required. */
6328 {
6329 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
6330 /* The SLP costs are calculated during SLP analysis. */
6331 if (!PURE_SLP_STMT (stmt_info))
6332 vect_model_load_cost (stmt_info, ncopies, load_lanes_p,
6333 NULL, NULL, NULL);
6334 return true;
6335 }
6336
6337 if (dump_enabled_p ())
6338 dump_printf_loc (MSG_NOTE, vect_location,
6339 "transform load. ncopies = %d\n", ncopies);
6340
6341 /** Transform. **/
6342
6343 ensure_base_align (stmt_info, dr);
6344
6345 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
6346 {
6347 tree vec_oprnd0 = NULL_TREE, op;
6348 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
6349 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
6350 tree ptr, mask, var, scale, merge, perm_mask = NULL_TREE, prev_res = NULL_TREE;
6351 edge pe = loop_preheader_edge (loop);
6352 gimple_seq seq;
6353 basic_block new_bb;
6354 enum { NARROW, NONE, WIDEN } modifier;
6355 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
6356
6357 if (nunits == gather_off_nunits)
6358 modifier = NONE;
6359 else if (nunits == gather_off_nunits / 2)
6360 {
6361 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
6362 modifier = WIDEN;
6363
6364 for (i = 0; i < gather_off_nunits; ++i)
6365 sel[i] = i | nunits;
6366
6367 perm_mask = vect_gen_perm_mask_checked (gather_off_vectype, sel);
6368 }
6369 else if (nunits == gather_off_nunits * 2)
6370 {
6371 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
6372 modifier = NARROW;
6373
6374 for (i = 0; i < nunits; ++i)
6375 sel[i] = i < gather_off_nunits
6376 ? i : i + nunits - gather_off_nunits;
6377
6378 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
6379 ncopies *= 2;
6380 }
6381 else
6382 gcc_unreachable ();
6383
6384 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
6385 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6386 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6387 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6388 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6389 scaletype = TREE_VALUE (arglist);
6390 gcc_checking_assert (types_compatible_p (srctype, rettype));
6391
6392 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6393
6394 ptr = fold_convert (ptrtype, gather_base);
6395 if (!is_gimple_min_invariant (ptr))
6396 {
6397 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
6398 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
6399 gcc_assert (!new_bb);
6400 }
6401
6402 /* Currently we support only unconditional gather loads,
6403 so mask should be all ones. */
6404 if (TREE_CODE (masktype) == INTEGER_TYPE)
6405 mask = build_int_cst (masktype, -1);
6406 else if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
6407 {
6408 mask = build_int_cst (TREE_TYPE (masktype), -1);
6409 mask = build_vector_from_val (masktype, mask);
6410 mask = vect_init_vector (stmt, mask, masktype, NULL);
6411 }
6412 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
6413 {
6414 REAL_VALUE_TYPE r;
6415 long tmp[6];
6416 for (j = 0; j < 6; ++j)
6417 tmp[j] = -1;
6418 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
6419 mask = build_real (TREE_TYPE (masktype), r);
6420 mask = build_vector_from_val (masktype, mask);
6421 mask = vect_init_vector (stmt, mask, masktype, NULL);
6422 }
6423 else
6424 gcc_unreachable ();
6425
6426 scale = build_int_cst (scaletype, gather_scale);
6427
6428 if (TREE_CODE (TREE_TYPE (rettype)) == INTEGER_TYPE)
6429 merge = build_int_cst (TREE_TYPE (rettype), 0);
6430 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rettype)))
6431 {
6432 REAL_VALUE_TYPE r;
6433 long tmp[6];
6434 for (j = 0; j < 6; ++j)
6435 tmp[j] = 0;
6436 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (rettype)));
6437 merge = build_real (TREE_TYPE (rettype), r);
6438 }
6439 else
6440 gcc_unreachable ();
6441 merge = build_vector_from_val (rettype, merge);
6442 merge = vect_init_vector (stmt, merge, rettype, NULL);
6443
6444 prev_stmt_info = NULL;
6445 for (j = 0; j < ncopies; ++j)
6446 {
6447 if (modifier == WIDEN && (j & 1))
6448 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
6449 perm_mask, stmt, gsi);
6450 else if (j == 0)
6451 op = vec_oprnd0
6452 = vect_get_vec_def_for_operand (gather_off, stmt);
6453 else
6454 op = vec_oprnd0
6455 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
6456
6457 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
6458 {
6459 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
6460 == TYPE_VECTOR_SUBPARTS (idxtype));
6461 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
6462 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
6463 new_stmt
6464 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6465 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6466 op = var;
6467 }
6468
6469 new_stmt
6470 = gimple_build_call (gather_decl, 5, merge, ptr, op, mask, scale);
6471
6472 if (!useless_type_conversion_p (vectype, rettype))
6473 {
6474 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
6475 == TYPE_VECTOR_SUBPARTS (rettype));
6476 op = vect_get_new_ssa_name (rettype, vect_simple_var);
6477 gimple_call_set_lhs (new_stmt, op);
6478 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6479 var = make_ssa_name (vec_dest);
6480 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
6481 new_stmt
6482 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6483 }
6484 else
6485 {
6486 var = make_ssa_name (vec_dest, new_stmt);
6487 gimple_call_set_lhs (new_stmt, var);
6488 }
6489
6490 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6491
6492 if (modifier == NARROW)
6493 {
6494 if ((j & 1) == 0)
6495 {
6496 prev_res = var;
6497 continue;
6498 }
6499 var = permute_vec_elements (prev_res, var,
6500 perm_mask, stmt, gsi);
6501 new_stmt = SSA_NAME_DEF_STMT (var);
6502 }
6503
6504 if (prev_stmt_info == NULL)
6505 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6506 else
6507 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6508 prev_stmt_info = vinfo_for_stmt (new_stmt);
6509 }
6510 return true;
6511 }
6512 else if (STMT_VINFO_STRIDED_P (stmt_info))
6513 {
6514 gimple_stmt_iterator incr_gsi;
6515 bool insert_after;
6516 gimple *incr;
6517 tree offvar;
6518 tree ivstep;
6519 tree running_off;
6520 vec<constructor_elt, va_gc> *v = NULL;
6521 gimple_seq stmts = NULL;
6522 tree stride_base, stride_step, alias_off;
6523
6524 gcc_assert (!nested_in_vect_loop);
6525
6526 if (slp && grouped_load)
6527 first_dr = STMT_VINFO_DATA_REF
6528 (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
6529 else
6530 first_dr = dr;
6531
6532 stride_base
6533 = fold_build_pointer_plus
6534 (DR_BASE_ADDRESS (first_dr),
6535 size_binop (PLUS_EXPR,
6536 convert_to_ptrofftype (DR_OFFSET (first_dr)),
6537 convert_to_ptrofftype (DR_INIT (first_dr))));
6538 stride_step = fold_convert (sizetype, DR_STEP (first_dr));
6539
6540 /* For a load with loop-invariant (but other than power-of-2)
6541 stride (i.e. not a grouped access) like so:
6542
6543 for (i = 0; i < n; i += stride)
6544 ... = array[i];
6545
6546 we generate a new induction variable and new accesses to
6547 form a new vector (or vectors, depending on ncopies):
6548
6549 for (j = 0; ; j += VF*stride)
6550 tmp1 = array[j];
6551 tmp2 = array[j + stride];
6552 ...
6553 vectemp = {tmp1, tmp2, ...}
6554 */
6555
6556 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
6557 build_int_cst (TREE_TYPE (stride_step), vf));
6558
6559 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6560
6561 create_iv (unshare_expr (stride_base), unshare_expr (ivstep), NULL,
6562 loop, &incr_gsi, insert_after,
6563 &offvar, NULL);
6564 incr = gsi_stmt (incr_gsi);
6565 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
6566
6567 stride_step = force_gimple_operand (unshare_expr (stride_step),
6568 &stmts, true, NULL_TREE);
6569 if (stmts)
6570 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
6571
6572 prev_stmt_info = NULL;
6573 running_off = offvar;
6574 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (first_dr)), 0);
6575 int nloads = nunits;
6576 tree ltype = TREE_TYPE (vectype);
6577 auto_vec<tree> dr_chain;
6578 if (slp)
6579 {
6580 nloads = nunits / group_size;
6581 if (group_size < nunits)
6582 ltype = build_vector_type (TREE_TYPE (vectype), group_size);
6583 else
6584 ltype = vectype;
6585 ltype = build_aligned_type (ltype, TYPE_ALIGN (TREE_TYPE (vectype)));
6586 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6587 if (slp_perm)
6588 dr_chain.create (ncopies);
6589 }
6590 for (j = 0; j < ncopies; j++)
6591 {
6592 tree vec_inv;
6593
6594 if (nloads > 1)
6595 {
6596 vec_alloc (v, nloads);
6597 for (i = 0; i < nloads; i++)
6598 {
6599 tree newref, newoff;
6600 gimple *incr;
6601 newref = build2 (MEM_REF, ltype, running_off, alias_off);
6602
6603 newref = force_gimple_operand_gsi (gsi, newref, true,
6604 NULL_TREE, true,
6605 GSI_SAME_STMT);
6606 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
6607 newoff = copy_ssa_name (running_off);
6608 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6609 running_off, stride_step);
6610 vect_finish_stmt_generation (stmt, incr, gsi);
6611
6612 running_off = newoff;
6613 }
6614
6615 vec_inv = build_constructor (vectype, v);
6616 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
6617 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6618 }
6619 else
6620 {
6621 new_stmt = gimple_build_assign (make_ssa_name (ltype),
6622 build2 (MEM_REF, ltype,
6623 running_off, alias_off));
6624 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6625
6626 tree newoff = copy_ssa_name (running_off);
6627 gimple *incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6628 running_off, stride_step);
6629 vect_finish_stmt_generation (stmt, incr, gsi);
6630
6631 running_off = newoff;
6632 }
6633
6634 if (slp)
6635 {
6636 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6637 if (slp_perm)
6638 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
6639 }
6640 else
6641 {
6642 if (j == 0)
6643 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6644 else
6645 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6646 prev_stmt_info = vinfo_for_stmt (new_stmt);
6647 }
6648 }
6649 if (slp_perm)
6650 vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
6651 slp_node_instance, false);
6652 return true;
6653 }
6654
6655 if (grouped_load)
6656 {
6657 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
6658 if (slp
6659 && !SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
6660 && first_stmt != SLP_TREE_SCALAR_STMTS (slp_node)[0])
6661 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6662
6663 /* Check if the chain of loads is already vectorized. */
6664 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
6665 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
6666 ??? But we can only do so if there is exactly one
6667 as we have no way to get at the rest. Leave the CSE
6668 opportunity alone.
6669 ??? With the group load eventually participating
6670 in multiple different permutations (having multiple
6671 slp nodes which refer to the same group) the CSE
6672 is even wrong code. See PR56270. */
6673 && !slp)
6674 {
6675 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6676 return true;
6677 }
6678 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
6679 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6680 group_gap_adj = 0;
6681
6682 /* VEC_NUM is the number of vect stmts to be created for this group. */
6683 if (slp)
6684 {
6685 grouped_load = false;
6686 /* For SLP permutation support we need to load the whole group,
6687 not only the number of vector stmts the permutation result
6688 fits in. */
6689 if (slp_perm)
6690 vec_num = (group_size * vf + nunits - 1) / nunits;
6691 else
6692 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6693 group_gap_adj = vf * group_size - nunits * vec_num;
6694 }
6695 else
6696 vec_num = group_size;
6697 }
6698 else
6699 {
6700 first_stmt = stmt;
6701 first_dr = dr;
6702 group_size = vec_num = 1;
6703 group_gap_adj = 0;
6704 }
6705
6706 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
6707 gcc_assert (alignment_support_scheme);
6708 /* Targets with load-lane instructions must not require explicit
6709 realignment. */
6710 gcc_assert (!load_lanes_p
6711 || alignment_support_scheme == dr_aligned
6712 || alignment_support_scheme == dr_unaligned_supported);
6713
6714 /* In case the vectorization factor (VF) is bigger than the number
6715 of elements that we can fit in a vectype (nunits), we have to generate
6716 more than one vector stmt - i.e - we need to "unroll" the
6717 vector stmt by a factor VF/nunits. In doing so, we record a pointer
6718 from one copy of the vector stmt to the next, in the field
6719 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
6720 stages to find the correct vector defs to be used when vectorizing
6721 stmts that use the defs of the current stmt. The example below
6722 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
6723 need to create 4 vectorized stmts):
6724
6725 before vectorization:
6726 RELATED_STMT VEC_STMT
6727 S1: x = memref - -
6728 S2: z = x + 1 - -
6729
6730 step 1: vectorize stmt S1:
6731 We first create the vector stmt VS1_0, and, as usual, record a
6732 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
6733 Next, we create the vector stmt VS1_1, and record a pointer to
6734 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
6735 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
6736 stmts and pointers:
6737 RELATED_STMT VEC_STMT
6738 VS1_0: vx0 = memref0 VS1_1 -
6739 VS1_1: vx1 = memref1 VS1_2 -
6740 VS1_2: vx2 = memref2 VS1_3 -
6741 VS1_3: vx3 = memref3 - -
6742 S1: x = load - VS1_0
6743 S2: z = x + 1 - -
6744
6745 See in documentation in vect_get_vec_def_for_stmt_copy for how the
6746 information we recorded in RELATED_STMT field is used to vectorize
6747 stmt S2. */
6748
6749 /* In case of interleaving (non-unit grouped access):
6750
6751 S1: x2 = &base + 2
6752 S2: x0 = &base
6753 S3: x1 = &base + 1
6754 S4: x3 = &base + 3
6755
6756 Vectorized loads are created in the order of memory accesses
6757 starting from the access of the first stmt of the chain:
6758
6759 VS1: vx0 = &base
6760 VS2: vx1 = &base + vec_size*1
6761 VS3: vx3 = &base + vec_size*2
6762 VS4: vx4 = &base + vec_size*3
6763
6764 Then permutation statements are generated:
6765
6766 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
6767 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
6768 ...
6769
6770 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6771 (the order of the data-refs in the output of vect_permute_load_chain
6772 corresponds to the order of scalar stmts in the interleaving chain - see
6773 the documentation of vect_permute_load_chain()).
6774 The generation of permutation stmts and recording them in
6775 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
6776
6777 In case of both multiple types and interleaving, the vector loads and
6778 permutation stmts above are created for every copy. The result vector
6779 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
6780 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
6781
6782 /* If the data reference is aligned (dr_aligned) or potentially unaligned
6783 on a target that supports unaligned accesses (dr_unaligned_supported)
6784 we generate the following code:
6785 p = initial_addr;
6786 indx = 0;
6787 loop {
6788 p = p + indx * vectype_size;
6789 vec_dest = *(p);
6790 indx = indx + 1;
6791 }
6792
6793 Otherwise, the data reference is potentially unaligned on a target that
6794 does not support unaligned accesses (dr_explicit_realign_optimized) -
6795 then generate the following code, in which the data in each iteration is
6796 obtained by two vector loads, one from the previous iteration, and one
6797 from the current iteration:
6798 p1 = initial_addr;
6799 msq_init = *(floor(p1))
6800 p2 = initial_addr + VS - 1;
6801 realignment_token = call target_builtin;
6802 indx = 0;
6803 loop {
6804 p2 = p2 + indx * vectype_size
6805 lsq = *(floor(p2))
6806 vec_dest = realign_load (msq, lsq, realignment_token)
6807 indx = indx + 1;
6808 msq = lsq;
6809 } */
6810
6811 /* If the misalignment remains the same throughout the execution of the
6812 loop, we can create the init_addr and permutation mask at the loop
6813 preheader. Otherwise, it needs to be created inside the loop.
6814 This can only occur when vectorizing memory accesses in the inner-loop
6815 nested within an outer-loop that is being vectorized. */
6816
6817 if (nested_in_vect_loop
6818 && (TREE_INT_CST_LOW (DR_STEP (dr))
6819 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
6820 {
6821 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
6822 compute_in_loop = true;
6823 }
6824
6825 if ((alignment_support_scheme == dr_explicit_realign_optimized
6826 || alignment_support_scheme == dr_explicit_realign)
6827 && !compute_in_loop)
6828 {
6829 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
6830 alignment_support_scheme, NULL_TREE,
6831 &at_loop);
6832 if (alignment_support_scheme == dr_explicit_realign_optimized)
6833 {
6834 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (msq));
6835 byte_offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
6836 size_one_node);
6837 }
6838 }
6839 else
6840 at_loop = loop;
6841
6842 if (negative)
6843 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
6844
6845 if (load_lanes_p)
6846 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
6847 else
6848 aggr_type = vectype;
6849
6850 prev_stmt_info = NULL;
6851 for (j = 0; j < ncopies; j++)
6852 {
6853 /* 1. Create the vector or array pointer update chain. */
6854 if (j == 0)
6855 {
6856 bool simd_lane_access_p
6857 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
6858 if (simd_lane_access_p
6859 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
6860 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
6861 && integer_zerop (DR_OFFSET (first_dr))
6862 && integer_zerop (DR_INIT (first_dr))
6863 && alias_sets_conflict_p (get_alias_set (aggr_type),
6864 get_alias_set (DR_REF (first_dr)))
6865 && (alignment_support_scheme == dr_aligned
6866 || alignment_support_scheme == dr_unaligned_supported))
6867 {
6868 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
6869 dataref_offset = build_int_cst (reference_alias_ptr_type
6870 (DR_REF (first_dr)), 0);
6871 inv_p = false;
6872 }
6873 else
6874 dataref_ptr
6875 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
6876 offset, &dummy, gsi, &ptr_incr,
6877 simd_lane_access_p, &inv_p,
6878 byte_offset);
6879 }
6880 else if (dataref_offset)
6881 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
6882 TYPE_SIZE_UNIT (aggr_type));
6883 else
6884 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
6885 TYPE_SIZE_UNIT (aggr_type));
6886
6887 if (grouped_load || slp_perm)
6888 dr_chain.create (vec_num);
6889
6890 if (load_lanes_p)
6891 {
6892 tree vec_array;
6893
6894 vec_array = create_vector_array (vectype, vec_num);
6895
6896 /* Emit:
6897 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
6898 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
6899 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
6900 gimple_call_set_lhs (new_stmt, vec_array);
6901 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6902
6903 /* Extract each vector into an SSA_NAME. */
6904 for (i = 0; i < vec_num; i++)
6905 {
6906 new_temp = read_vector_array (stmt, gsi, scalar_dest,
6907 vec_array, i);
6908 dr_chain.quick_push (new_temp);
6909 }
6910
6911 /* Record the mapping between SSA_NAMEs and statements. */
6912 vect_record_grouped_load_vectors (stmt, dr_chain);
6913 }
6914 else
6915 {
6916 for (i = 0; i < vec_num; i++)
6917 {
6918 if (i > 0)
6919 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
6920 stmt, NULL_TREE);
6921
6922 /* 2. Create the vector-load in the loop. */
6923 switch (alignment_support_scheme)
6924 {
6925 case dr_aligned:
6926 case dr_unaligned_supported:
6927 {
6928 unsigned int align, misalign;
6929
6930 data_ref
6931 = fold_build2 (MEM_REF, vectype, dataref_ptr,
6932 dataref_offset
6933 ? dataref_offset
6934 : build_int_cst (reference_alias_ptr_type
6935 (DR_REF (first_dr)), 0));
6936 align = TYPE_ALIGN_UNIT (vectype);
6937 if (alignment_support_scheme == dr_aligned)
6938 {
6939 gcc_assert (aligned_access_p (first_dr));
6940 misalign = 0;
6941 }
6942 else if (DR_MISALIGNMENT (first_dr) == -1)
6943 {
6944 if (DR_VECT_AUX (first_dr)->base_element_aligned)
6945 align = TYPE_ALIGN_UNIT (elem_type);
6946 else
6947 align = (get_object_alignment (DR_REF (first_dr))
6948 / BITS_PER_UNIT);
6949 misalign = 0;
6950 TREE_TYPE (data_ref)
6951 = build_aligned_type (TREE_TYPE (data_ref),
6952 align * BITS_PER_UNIT);
6953 }
6954 else
6955 {
6956 TREE_TYPE (data_ref)
6957 = build_aligned_type (TREE_TYPE (data_ref),
6958 TYPE_ALIGN (elem_type));
6959 misalign = DR_MISALIGNMENT (first_dr);
6960 }
6961 if (dataref_offset == NULL_TREE
6962 && TREE_CODE (dataref_ptr) == SSA_NAME)
6963 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
6964 align, misalign);
6965 break;
6966 }
6967 case dr_explicit_realign:
6968 {
6969 tree ptr, bump;
6970
6971 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
6972
6973 if (compute_in_loop)
6974 msq = vect_setup_realignment (first_stmt, gsi,
6975 &realignment_token,
6976 dr_explicit_realign,
6977 dataref_ptr, NULL);
6978
6979 if (TREE_CODE (dataref_ptr) == SSA_NAME)
6980 ptr = copy_ssa_name (dataref_ptr);
6981 else
6982 ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
6983 new_stmt = gimple_build_assign
6984 (ptr, BIT_AND_EXPR, dataref_ptr,
6985 build_int_cst
6986 (TREE_TYPE (dataref_ptr),
6987 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
6988 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6989 data_ref
6990 = build2 (MEM_REF, vectype, ptr,
6991 build_int_cst (reference_alias_ptr_type
6992 (DR_REF (first_dr)), 0));
6993 vec_dest = vect_create_destination_var (scalar_dest,
6994 vectype);
6995 new_stmt = gimple_build_assign (vec_dest, data_ref);
6996 new_temp = make_ssa_name (vec_dest, new_stmt);
6997 gimple_assign_set_lhs (new_stmt, new_temp);
6998 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
6999 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
7000 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7001 msq = new_temp;
7002
7003 bump = size_binop (MULT_EXPR, vs,
7004 TYPE_SIZE_UNIT (elem_type));
7005 bump = size_binop (MINUS_EXPR, bump, size_one_node);
7006 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
7007 new_stmt = gimple_build_assign
7008 (NULL_TREE, BIT_AND_EXPR, ptr,
7009 build_int_cst
7010 (TREE_TYPE (ptr),
7011 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
7012 ptr = copy_ssa_name (ptr, new_stmt);
7013 gimple_assign_set_lhs (new_stmt, ptr);
7014 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7015 data_ref
7016 = build2 (MEM_REF, vectype, ptr,
7017 build_int_cst (reference_alias_ptr_type
7018 (DR_REF (first_dr)), 0));
7019 break;
7020 }
7021 case dr_explicit_realign_optimized:
7022 if (TREE_CODE (dataref_ptr) == SSA_NAME)
7023 new_temp = copy_ssa_name (dataref_ptr);
7024 else
7025 new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
7026 new_stmt = gimple_build_assign
7027 (new_temp, BIT_AND_EXPR, dataref_ptr,
7028 build_int_cst
7029 (TREE_TYPE (dataref_ptr),
7030 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
7031 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7032 data_ref
7033 = build2 (MEM_REF, vectype, new_temp,
7034 build_int_cst (reference_alias_ptr_type
7035 (DR_REF (first_dr)), 0));
7036 break;
7037 default:
7038 gcc_unreachable ();
7039 }
7040 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7041 new_stmt = gimple_build_assign (vec_dest, data_ref);
7042 new_temp = make_ssa_name (vec_dest, new_stmt);
7043 gimple_assign_set_lhs (new_stmt, new_temp);
7044 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7045
7046 /* 3. Handle explicit realignment if necessary/supported.
7047 Create in loop:
7048 vec_dest = realign_load (msq, lsq, realignment_token) */
7049 if (alignment_support_scheme == dr_explicit_realign_optimized
7050 || alignment_support_scheme == dr_explicit_realign)
7051 {
7052 lsq = gimple_assign_lhs (new_stmt);
7053 if (!realignment_token)
7054 realignment_token = dataref_ptr;
7055 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7056 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR,
7057 msq, lsq, realignment_token);
7058 new_temp = make_ssa_name (vec_dest, new_stmt);
7059 gimple_assign_set_lhs (new_stmt, new_temp);
7060 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7061
7062 if (alignment_support_scheme == dr_explicit_realign_optimized)
7063 {
7064 gcc_assert (phi);
7065 if (i == vec_num - 1 && j == ncopies - 1)
7066 add_phi_arg (phi, lsq,
7067 loop_latch_edge (containing_loop),
7068 UNKNOWN_LOCATION);
7069 msq = lsq;
7070 }
7071 }
7072
7073 /* 4. Handle invariant-load. */
7074 if (inv_p && !bb_vinfo)
7075 {
7076 gcc_assert (!grouped_load);
7077 /* If we have versioned for aliasing or the loop doesn't
7078 have any data dependencies that would preclude this,
7079 then we are sure this is a loop invariant load and
7080 thus we can insert it on the preheader edge. */
7081 if (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
7082 && !nested_in_vect_loop
7083 && hoist_defs_of_uses (stmt, loop))
7084 {
7085 if (dump_enabled_p ())
7086 {
7087 dump_printf_loc (MSG_NOTE, vect_location,
7088 "hoisting out of the vectorized "
7089 "loop: ");
7090 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7091 }
7092 tree tem = copy_ssa_name (scalar_dest);
7093 gsi_insert_on_edge_immediate
7094 (loop_preheader_edge (loop),
7095 gimple_build_assign (tem,
7096 unshare_expr
7097 (gimple_assign_rhs1 (stmt))));
7098 new_temp = vect_init_vector (stmt, tem, vectype, NULL);
7099 }
7100 else
7101 {
7102 gimple_stmt_iterator gsi2 = *gsi;
7103 gsi_next (&gsi2);
7104 new_temp = vect_init_vector (stmt, scalar_dest,
7105 vectype, &gsi2);
7106 }
7107 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7108 set_vinfo_for_stmt (new_stmt,
7109 new_stmt_vec_info (new_stmt, vinfo));
7110 }
7111
7112 if (negative)
7113 {
7114 tree perm_mask = perm_mask_for_reverse (vectype);
7115 new_temp = permute_vec_elements (new_temp, new_temp,
7116 perm_mask, stmt, gsi);
7117 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7118 }
7119
7120 /* Collect vector loads and later create their permutation in
7121 vect_transform_grouped_load (). */
7122 if (grouped_load || slp_perm)
7123 dr_chain.quick_push (new_temp);
7124
7125 /* Store vector loads in the corresponding SLP_NODE. */
7126 if (slp && !slp_perm)
7127 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7128 }
7129 /* Bump the vector pointer to account for a gap or for excess
7130 elements loaded for a permuted SLP load. */
7131 if (group_gap_adj != 0)
7132 {
7133 bool ovf;
7134 tree bump
7135 = wide_int_to_tree (sizetype,
7136 wi::smul (TYPE_SIZE_UNIT (elem_type),
7137 group_gap_adj, &ovf));
7138 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7139 stmt, bump);
7140 }
7141 }
7142
7143 if (slp && !slp_perm)
7144 continue;
7145
7146 if (slp_perm)
7147 {
7148 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
7149 slp_node_instance, false))
7150 {
7151 dr_chain.release ();
7152 return false;
7153 }
7154 }
7155 else
7156 {
7157 if (grouped_load)
7158 {
7159 if (!load_lanes_p)
7160 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
7161 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
7162 }
7163 else
7164 {
7165 if (j == 0)
7166 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7167 else
7168 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7169 prev_stmt_info = vinfo_for_stmt (new_stmt);
7170 }
7171 }
7172 dr_chain.release ();
7173 }
7174
7175 return true;
7176 }
7177
7178 /* Function vect_is_simple_cond.
7179
7180 Input:
7181 LOOP - the loop that is being vectorized.
7182 COND - Condition that is checked for simple use.
7183
7184 Output:
7185 *COMP_VECTYPE - the vector type for the comparison.
7186
7187 Returns whether a COND can be vectorized. Checks whether
7188 condition operands are supportable using vec_is_simple_use. */
7189
7190 static bool
7191 vect_is_simple_cond (tree cond, vec_info *vinfo, tree *comp_vectype)
7192 {
7193 tree lhs, rhs;
7194 enum vect_def_type dt;
7195 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7196
7197 /* Mask case. */
7198 if (TREE_CODE (cond) == SSA_NAME
7199 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)
7200 {
7201 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (cond);
7202 if (!vect_is_simple_use (cond, vinfo, &lhs_def_stmt,
7203 &dt, comp_vectype)
7204 || !*comp_vectype
7205 || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
7206 return false;
7207 return true;
7208 }
7209
7210 if (!COMPARISON_CLASS_P (cond))
7211 return false;
7212
7213 lhs = TREE_OPERAND (cond, 0);
7214 rhs = TREE_OPERAND (cond, 1);
7215
7216 if (TREE_CODE (lhs) == SSA_NAME)
7217 {
7218 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
7219 if (!vect_is_simple_use (lhs, vinfo, &lhs_def_stmt, &dt, &vectype1))
7220 return false;
7221 }
7222 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
7223 && TREE_CODE (lhs) != FIXED_CST)
7224 return false;
7225
7226 if (TREE_CODE (rhs) == SSA_NAME)
7227 {
7228 gimple *rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
7229 if (!vect_is_simple_use (rhs, vinfo, &rhs_def_stmt, &dt, &vectype2))
7230 return false;
7231 }
7232 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
7233 && TREE_CODE (rhs) != FIXED_CST)
7234 return false;
7235
7236 *comp_vectype = vectype1 ? vectype1 : vectype2;
7237 return true;
7238 }
7239
7240 /* vectorizable_condition.
7241
7242 Check if STMT is conditional modify expression that can be vectorized.
7243 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7244 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
7245 at GSI.
7246
7247 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
7248 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
7249 else clause if it is 2).
7250
7251 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7252
7253 bool
7254 vectorizable_condition (gimple *stmt, gimple_stmt_iterator *gsi,
7255 gimple **vec_stmt, tree reduc_def, int reduc_index,
7256 slp_tree slp_node)
7257 {
7258 tree scalar_dest = NULL_TREE;
7259 tree vec_dest = NULL_TREE;
7260 tree cond_expr, then_clause, else_clause;
7261 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7262 tree comp_vectype = NULL_TREE;
7263 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
7264 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
7265 tree vec_compare, vec_cond_expr;
7266 tree new_temp;
7267 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
7268 enum vect_def_type dt, dts[4];
7269 int ncopies;
7270 enum tree_code code;
7271 stmt_vec_info prev_stmt_info = NULL;
7272 int i, j;
7273 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7274 vec<tree> vec_oprnds0 = vNULL;
7275 vec<tree> vec_oprnds1 = vNULL;
7276 vec<tree> vec_oprnds2 = vNULL;
7277 vec<tree> vec_oprnds3 = vNULL;
7278 tree vec_cmp_type;
7279 bool masked = false;
7280
7281 if (reduc_index && STMT_SLP_TYPE (stmt_info))
7282 return false;
7283
7284 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == TREE_CODE_REDUCTION)
7285 {
7286 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7287 return false;
7288
7289 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7290 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7291 && reduc_def))
7292 return false;
7293
7294 /* FORNOW: not yet supported. */
7295 if (STMT_VINFO_LIVE_P (stmt_info))
7296 {
7297 if (dump_enabled_p ())
7298 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7299 "value used after loop.\n");
7300 return false;
7301 }
7302 }
7303
7304 /* Is vectorizable conditional operation? */
7305 if (!is_gimple_assign (stmt))
7306 return false;
7307
7308 code = gimple_assign_rhs_code (stmt);
7309
7310 if (code != COND_EXPR)
7311 return false;
7312
7313 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7314 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
7315
7316 if (slp_node || PURE_SLP_STMT (stmt_info))
7317 ncopies = 1;
7318 else
7319 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7320
7321 gcc_assert (ncopies >= 1);
7322 if (reduc_index && ncopies > 1)
7323 return false; /* FORNOW */
7324
7325 cond_expr = gimple_assign_rhs1 (stmt);
7326 then_clause = gimple_assign_rhs2 (stmt);
7327 else_clause = gimple_assign_rhs3 (stmt);
7328
7329 if (!vect_is_simple_cond (cond_expr, stmt_info->vinfo, &comp_vectype)
7330 || !comp_vectype)
7331 return false;
7332
7333 gimple *def_stmt;
7334 if (!vect_is_simple_use (then_clause, stmt_info->vinfo, &def_stmt, &dt))
7335 return false;
7336 if (!vect_is_simple_use (else_clause, stmt_info->vinfo, &def_stmt, &dt))
7337 return false;
7338
7339 if (VECTOR_BOOLEAN_TYPE_P (comp_vectype))
7340 {
7341 vec_cmp_type = comp_vectype;
7342 masked = true;
7343 }
7344 else
7345 vec_cmp_type = build_same_sized_truth_vector_type (comp_vectype);
7346 if (vec_cmp_type == NULL_TREE)
7347 return false;
7348
7349 if (!vec_stmt)
7350 {
7351 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
7352 return expand_vec_cond_expr_p (vectype, comp_vectype);
7353 }
7354
7355 /* Transform. */
7356
7357 if (!slp_node)
7358 {
7359 vec_oprnds0.create (1);
7360 vec_oprnds1.create (1);
7361 vec_oprnds2.create (1);
7362 vec_oprnds3.create (1);
7363 }
7364
7365 /* Handle def. */
7366 scalar_dest = gimple_assign_lhs (stmt);
7367 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7368
7369 /* Handle cond expr. */
7370 for (j = 0; j < ncopies; j++)
7371 {
7372 gassign *new_stmt = NULL;
7373 if (j == 0)
7374 {
7375 if (slp_node)
7376 {
7377 auto_vec<tree, 4> ops;
7378 auto_vec<vec<tree>, 4> vec_defs;
7379
7380 if (masked)
7381 ops.safe_push (cond_expr);
7382 else
7383 {
7384 ops.safe_push (TREE_OPERAND (cond_expr, 0));
7385 ops.safe_push (TREE_OPERAND (cond_expr, 1));
7386 }
7387 ops.safe_push (then_clause);
7388 ops.safe_push (else_clause);
7389 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
7390 vec_oprnds3 = vec_defs.pop ();
7391 vec_oprnds2 = vec_defs.pop ();
7392 if (!masked)
7393 vec_oprnds1 = vec_defs.pop ();
7394 vec_oprnds0 = vec_defs.pop ();
7395
7396 ops.release ();
7397 vec_defs.release ();
7398 }
7399 else
7400 {
7401 gimple *gtemp;
7402 if (masked)
7403 {
7404 vec_cond_lhs
7405 = vect_get_vec_def_for_operand (cond_expr, stmt,
7406 comp_vectype);
7407 vect_is_simple_use (cond_expr, stmt_info->vinfo,
7408 &gtemp, &dts[0]);
7409 }
7410 else
7411 {
7412 vec_cond_lhs =
7413 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
7414 stmt, comp_vectype);
7415 vect_is_simple_use (TREE_OPERAND (cond_expr, 0),
7416 loop_vinfo, &gtemp, &dts[0]);
7417
7418 vec_cond_rhs =
7419 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
7420 stmt, comp_vectype);
7421 vect_is_simple_use (TREE_OPERAND (cond_expr, 1),
7422 loop_vinfo, &gtemp, &dts[1]);
7423 }
7424 if (reduc_index == 1)
7425 vec_then_clause = reduc_def;
7426 else
7427 {
7428 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
7429 stmt);
7430 vect_is_simple_use (then_clause, loop_vinfo,
7431 &gtemp, &dts[2]);
7432 }
7433 if (reduc_index == 2)
7434 vec_else_clause = reduc_def;
7435 else
7436 {
7437 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
7438 stmt);
7439 vect_is_simple_use (else_clause, loop_vinfo, &gtemp, &dts[3]);
7440 }
7441 }
7442 }
7443 else
7444 {
7445 vec_cond_lhs
7446 = vect_get_vec_def_for_stmt_copy (dts[0],
7447 vec_oprnds0.pop ());
7448 if (!masked)
7449 vec_cond_rhs
7450 = vect_get_vec_def_for_stmt_copy (dts[1],
7451 vec_oprnds1.pop ());
7452
7453 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
7454 vec_oprnds2.pop ());
7455 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
7456 vec_oprnds3.pop ());
7457 }
7458
7459 if (!slp_node)
7460 {
7461 vec_oprnds0.quick_push (vec_cond_lhs);
7462 if (!masked)
7463 vec_oprnds1.quick_push (vec_cond_rhs);
7464 vec_oprnds2.quick_push (vec_then_clause);
7465 vec_oprnds3.quick_push (vec_else_clause);
7466 }
7467
7468 /* Arguments are ready. Create the new vector stmt. */
7469 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
7470 {
7471 vec_then_clause = vec_oprnds2[i];
7472 vec_else_clause = vec_oprnds3[i];
7473
7474 if (masked)
7475 vec_compare = vec_cond_lhs;
7476 else
7477 {
7478 vec_cond_rhs = vec_oprnds1[i];
7479 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
7480 vec_cond_lhs, vec_cond_rhs);
7481 }
7482 vec_cond_expr = build3 (VEC_COND_EXPR, vectype,
7483 vec_compare, vec_then_clause, vec_else_clause);
7484
7485 new_stmt = gimple_build_assign (vec_dest, vec_cond_expr);
7486 new_temp = make_ssa_name (vec_dest, new_stmt);
7487 gimple_assign_set_lhs (new_stmt, new_temp);
7488 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7489 if (slp_node)
7490 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7491 }
7492
7493 if (slp_node)
7494 continue;
7495
7496 if (j == 0)
7497 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7498 else
7499 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7500
7501 prev_stmt_info = vinfo_for_stmt (new_stmt);
7502 }
7503
7504 vec_oprnds0.release ();
7505 vec_oprnds1.release ();
7506 vec_oprnds2.release ();
7507 vec_oprnds3.release ();
7508
7509 return true;
7510 }
7511
7512 /* vectorizable_comparison.
7513
7514 Check if STMT is comparison expression that can be vectorized.
7515 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7516 comparison, put it in VEC_STMT, and insert it at GSI.
7517
7518 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7519
7520 bool
7521 vectorizable_comparison (gimple *stmt, gimple_stmt_iterator *gsi,
7522 gimple **vec_stmt, tree reduc_def,
7523 slp_tree slp_node)
7524 {
7525 tree lhs, rhs1, rhs2;
7526 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7527 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7528 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7529 tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
7530 tree new_temp;
7531 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
7532 enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
7533 unsigned nunits;
7534 int ncopies;
7535 enum tree_code code;
7536 stmt_vec_info prev_stmt_info = NULL;
7537 int i, j;
7538 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7539 vec<tree> vec_oprnds0 = vNULL;
7540 vec<tree> vec_oprnds1 = vNULL;
7541 gimple *def_stmt;
7542 tree mask_type;
7543 tree mask;
7544
7545 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
7546 return false;
7547
7548 mask_type = vectype;
7549 nunits = TYPE_VECTOR_SUBPARTS (vectype);
7550
7551 if (slp_node || PURE_SLP_STMT (stmt_info))
7552 ncopies = 1;
7553 else
7554 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7555
7556 gcc_assert (ncopies >= 1);
7557 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7558 return false;
7559
7560 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7561 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7562 && reduc_def))
7563 return false;
7564
7565 if (STMT_VINFO_LIVE_P (stmt_info))
7566 {
7567 if (dump_enabled_p ())
7568 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7569 "value used after loop.\n");
7570 return false;
7571 }
7572
7573 if (!is_gimple_assign (stmt))
7574 return false;
7575
7576 code = gimple_assign_rhs_code (stmt);
7577
7578 if (TREE_CODE_CLASS (code) != tcc_comparison)
7579 return false;
7580
7581 rhs1 = gimple_assign_rhs1 (stmt);
7582 rhs2 = gimple_assign_rhs2 (stmt);
7583
7584 if (!vect_is_simple_use (rhs1, stmt_info->vinfo, &def_stmt,
7585 &dts[0], &vectype1))
7586 return false;
7587
7588 if (!vect_is_simple_use (rhs2, stmt_info->vinfo, &def_stmt,
7589 &dts[1], &vectype2))
7590 return false;
7591
7592 if (vectype1 && vectype2
7593 && TYPE_VECTOR_SUBPARTS (vectype1) != TYPE_VECTOR_SUBPARTS (vectype2))
7594 return false;
7595
7596 vectype = vectype1 ? vectype1 : vectype2;
7597
7598 /* Invariant comparison. */
7599 if (!vectype)
7600 {
7601 vectype = build_vector_type (TREE_TYPE (rhs1), nunits);
7602 if (tree_to_shwi (TYPE_SIZE_UNIT (vectype)) != current_vector_size)
7603 return false;
7604 }
7605 else if (nunits != TYPE_VECTOR_SUBPARTS (vectype))
7606 return false;
7607
7608 if (!vec_stmt)
7609 {
7610 STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
7611 vect_model_simple_cost (stmt_info, ncopies, dts, NULL, NULL);
7612 return expand_vec_cmp_expr_p (vectype, mask_type);
7613 }
7614
7615 /* Transform. */
7616 if (!slp_node)
7617 {
7618 vec_oprnds0.create (1);
7619 vec_oprnds1.create (1);
7620 }
7621
7622 /* Handle def. */
7623 lhs = gimple_assign_lhs (stmt);
7624 mask = vect_create_destination_var (lhs, mask_type);
7625
7626 /* Handle cmp expr. */
7627 for (j = 0; j < ncopies; j++)
7628 {
7629 gassign *new_stmt = NULL;
7630 if (j == 0)
7631 {
7632 if (slp_node)
7633 {
7634 auto_vec<tree, 2> ops;
7635 auto_vec<vec<tree>, 2> vec_defs;
7636
7637 ops.safe_push (rhs1);
7638 ops.safe_push (rhs2);
7639 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
7640 vec_oprnds1 = vec_defs.pop ();
7641 vec_oprnds0 = vec_defs.pop ();
7642 }
7643 else
7644 {
7645 vec_rhs1 = vect_get_vec_def_for_operand (rhs1, stmt, NULL);
7646 vec_rhs2 = vect_get_vec_def_for_operand (rhs2, stmt, NULL);
7647 }
7648 }
7649 else
7650 {
7651 vec_rhs1 = vect_get_vec_def_for_stmt_copy (dts[0],
7652 vec_oprnds0.pop ());
7653 vec_rhs2 = vect_get_vec_def_for_stmt_copy (dts[1],
7654 vec_oprnds1.pop ());
7655 }
7656
7657 if (!slp_node)
7658 {
7659 vec_oprnds0.quick_push (vec_rhs1);
7660 vec_oprnds1.quick_push (vec_rhs2);
7661 }
7662
7663 /* Arguments are ready. Create the new vector stmt. */
7664 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
7665 {
7666 vec_rhs2 = vec_oprnds1[i];
7667
7668 new_temp = make_ssa_name (mask);
7669 new_stmt = gimple_build_assign (new_temp, code, vec_rhs1, vec_rhs2);
7670 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7671 if (slp_node)
7672 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7673 }
7674
7675 if (slp_node)
7676 continue;
7677
7678 if (j == 0)
7679 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7680 else
7681 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7682
7683 prev_stmt_info = vinfo_for_stmt (new_stmt);
7684 }
7685
7686 vec_oprnds0.release ();
7687 vec_oprnds1.release ();
7688
7689 return true;
7690 }
7691
7692 /* Make sure the statement is vectorizable. */
7693
7694 bool
7695 vect_analyze_stmt (gimple *stmt, bool *need_to_vectorize, slp_tree node)
7696 {
7697 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7698 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7699 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
7700 bool ok;
7701 tree scalar_type, vectype;
7702 gimple *pattern_stmt;
7703 gimple_seq pattern_def_seq;
7704
7705 if (dump_enabled_p ())
7706 {
7707 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
7708 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7709 }
7710
7711 if (gimple_has_volatile_ops (stmt))
7712 {
7713 if (dump_enabled_p ())
7714 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7715 "not vectorized: stmt has volatile operands\n");
7716
7717 return false;
7718 }
7719
7720 /* Skip stmts that do not need to be vectorized. In loops this is expected
7721 to include:
7722 - the COND_EXPR which is the loop exit condition
7723 - any LABEL_EXPRs in the loop
7724 - computations that are used only for array indexing or loop control.
7725 In basic blocks we only analyze statements that are a part of some SLP
7726 instance, therefore, all the statements are relevant.
7727
7728 Pattern statement needs to be analyzed instead of the original statement
7729 if the original statement is not relevant. Otherwise, we analyze both
7730 statements. In basic blocks we are called from some SLP instance
7731 traversal, don't analyze pattern stmts instead, the pattern stmts
7732 already will be part of SLP instance. */
7733
7734 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
7735 if (!STMT_VINFO_RELEVANT_P (stmt_info)
7736 && !STMT_VINFO_LIVE_P (stmt_info))
7737 {
7738 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7739 && pattern_stmt
7740 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7741 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7742 {
7743 /* Analyze PATTERN_STMT instead of the original stmt. */
7744 stmt = pattern_stmt;
7745 stmt_info = vinfo_for_stmt (pattern_stmt);
7746 if (dump_enabled_p ())
7747 {
7748 dump_printf_loc (MSG_NOTE, vect_location,
7749 "==> examining pattern statement: ");
7750 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7751 }
7752 }
7753 else
7754 {
7755 if (dump_enabled_p ())
7756 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
7757
7758 return true;
7759 }
7760 }
7761 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7762 && node == NULL
7763 && pattern_stmt
7764 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7765 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7766 {
7767 /* Analyze PATTERN_STMT too. */
7768 if (dump_enabled_p ())
7769 {
7770 dump_printf_loc (MSG_NOTE, vect_location,
7771 "==> examining pattern statement: ");
7772 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7773 }
7774
7775 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
7776 return false;
7777 }
7778
7779 if (is_pattern_stmt_p (stmt_info)
7780 && node == NULL
7781 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
7782 {
7783 gimple_stmt_iterator si;
7784
7785 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
7786 {
7787 gimple *pattern_def_stmt = gsi_stmt (si);
7788 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
7789 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
7790 {
7791 /* Analyze def stmt of STMT if it's a pattern stmt. */
7792 if (dump_enabled_p ())
7793 {
7794 dump_printf_loc (MSG_NOTE, vect_location,
7795 "==> examining pattern def statement: ");
7796 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
7797 }
7798
7799 if (!vect_analyze_stmt (pattern_def_stmt,
7800 need_to_vectorize, node))
7801 return false;
7802 }
7803 }
7804 }
7805
7806 switch (STMT_VINFO_DEF_TYPE (stmt_info))
7807 {
7808 case vect_internal_def:
7809 break;
7810
7811 case vect_reduction_def:
7812 case vect_nested_cycle:
7813 gcc_assert (!bb_vinfo
7814 && (relevance == vect_used_in_outer
7815 || relevance == vect_used_in_outer_by_reduction
7816 || relevance == vect_used_by_reduction
7817 || relevance == vect_unused_in_scope));
7818 break;
7819
7820 case vect_induction_def:
7821 case vect_constant_def:
7822 case vect_external_def:
7823 case vect_unknown_def_type:
7824 default:
7825 gcc_unreachable ();
7826 }
7827
7828 if (bb_vinfo)
7829 {
7830 gcc_assert (PURE_SLP_STMT (stmt_info));
7831
7832 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
7833 if (dump_enabled_p ())
7834 {
7835 dump_printf_loc (MSG_NOTE, vect_location,
7836 "get vectype for scalar type: ");
7837 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
7838 dump_printf (MSG_NOTE, "\n");
7839 }
7840
7841 vectype = get_vectype_for_scalar_type (scalar_type);
7842 if (!vectype)
7843 {
7844 if (dump_enabled_p ())
7845 {
7846 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7847 "not SLPed: unsupported data-type ");
7848 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
7849 scalar_type);
7850 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
7851 }
7852 return false;
7853 }
7854
7855 if (dump_enabled_p ())
7856 {
7857 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
7858 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
7859 dump_printf (MSG_NOTE, "\n");
7860 }
7861
7862 STMT_VINFO_VECTYPE (stmt_info) = vectype;
7863 }
7864
7865 if (STMT_VINFO_RELEVANT_P (stmt_info))
7866 {
7867 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
7868 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
7869 || (is_gimple_call (stmt)
7870 && gimple_call_lhs (stmt) == NULL_TREE));
7871 *need_to_vectorize = true;
7872 }
7873
7874 if (PURE_SLP_STMT (stmt_info) && !node)
7875 {
7876 dump_printf_loc (MSG_NOTE, vect_location,
7877 "handled only by SLP analysis\n");
7878 return true;
7879 }
7880
7881 ok = true;
7882 if (!bb_vinfo
7883 && (STMT_VINFO_RELEVANT_P (stmt_info)
7884 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
7885 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
7886 || vectorizable_conversion (stmt, NULL, NULL, node)
7887 || vectorizable_shift (stmt, NULL, NULL, node)
7888 || vectorizable_operation (stmt, NULL, NULL, node)
7889 || vectorizable_assignment (stmt, NULL, NULL, node)
7890 || vectorizable_load (stmt, NULL, NULL, node, NULL)
7891 || vectorizable_call (stmt, NULL, NULL, node)
7892 || vectorizable_store (stmt, NULL, NULL, node)
7893 || vectorizable_reduction (stmt, NULL, NULL, node)
7894 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
7895 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
7896 else
7897 {
7898 if (bb_vinfo)
7899 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
7900 || vectorizable_conversion (stmt, NULL, NULL, node)
7901 || vectorizable_shift (stmt, NULL, NULL, node)
7902 || vectorizable_operation (stmt, NULL, NULL, node)
7903 || vectorizable_assignment (stmt, NULL, NULL, node)
7904 || vectorizable_load (stmt, NULL, NULL, node, NULL)
7905 || vectorizable_call (stmt, NULL, NULL, node)
7906 || vectorizable_store (stmt, NULL, NULL, node)
7907 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
7908 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
7909 }
7910
7911 if (!ok)
7912 {
7913 if (dump_enabled_p ())
7914 {
7915 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7916 "not vectorized: relevant stmt not ");
7917 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
7918 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
7919 }
7920
7921 return false;
7922 }
7923
7924 if (bb_vinfo)
7925 return true;
7926
7927 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
7928 need extra handling, except for vectorizable reductions. */
7929 if (STMT_VINFO_LIVE_P (stmt_info)
7930 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
7931 ok = vectorizable_live_operation (stmt, NULL, NULL);
7932
7933 if (!ok)
7934 {
7935 if (dump_enabled_p ())
7936 {
7937 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7938 "not vectorized: live stmt not ");
7939 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
7940 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
7941 }
7942
7943 return false;
7944 }
7945
7946 return true;
7947 }
7948
7949
7950 /* Function vect_transform_stmt.
7951
7952 Create a vectorized stmt to replace STMT, and insert it at BSI. */
7953
7954 bool
7955 vect_transform_stmt (gimple *stmt, gimple_stmt_iterator *gsi,
7956 bool *grouped_store, slp_tree slp_node,
7957 slp_instance slp_node_instance)
7958 {
7959 bool is_store = false;
7960 gimple *vec_stmt = NULL;
7961 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7962 bool done;
7963
7964 gimple *old_vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
7965
7966 switch (STMT_VINFO_TYPE (stmt_info))
7967 {
7968 case type_demotion_vec_info_type:
7969 case type_promotion_vec_info_type:
7970 case type_conversion_vec_info_type:
7971 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
7972 gcc_assert (done);
7973 break;
7974
7975 case induc_vec_info_type:
7976 gcc_assert (!slp_node);
7977 done = vectorizable_induction (stmt, gsi, &vec_stmt);
7978 gcc_assert (done);
7979 break;
7980
7981 case shift_vec_info_type:
7982 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
7983 gcc_assert (done);
7984 break;
7985
7986 case op_vec_info_type:
7987 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
7988 gcc_assert (done);
7989 break;
7990
7991 case assignment_vec_info_type:
7992 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
7993 gcc_assert (done);
7994 break;
7995
7996 case load_vec_info_type:
7997 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
7998 slp_node_instance);
7999 gcc_assert (done);
8000 break;
8001
8002 case store_vec_info_type:
8003 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
8004 gcc_assert (done);
8005 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
8006 {
8007 /* In case of interleaving, the whole chain is vectorized when the
8008 last store in the chain is reached. Store stmts before the last
8009 one are skipped, and there vec_stmt_info shouldn't be freed
8010 meanwhile. */
8011 *grouped_store = true;
8012 if (STMT_VINFO_VEC_STMT (stmt_info))
8013 is_store = true;
8014 }
8015 else
8016 is_store = true;
8017 break;
8018
8019 case condition_vec_info_type:
8020 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
8021 gcc_assert (done);
8022 break;
8023
8024 case comparison_vec_info_type:
8025 done = vectorizable_comparison (stmt, gsi, &vec_stmt, NULL, slp_node);
8026 gcc_assert (done);
8027 break;
8028
8029 case call_vec_info_type:
8030 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
8031 stmt = gsi_stmt (*gsi);
8032 if (is_gimple_call (stmt)
8033 && gimple_call_internal_p (stmt)
8034 && gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
8035 is_store = true;
8036 break;
8037
8038 case call_simd_clone_vec_info_type:
8039 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
8040 stmt = gsi_stmt (*gsi);
8041 break;
8042
8043 case reduc_vec_info_type:
8044 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
8045 gcc_assert (done);
8046 break;
8047
8048 default:
8049 if (!STMT_VINFO_LIVE_P (stmt_info))
8050 {
8051 if (dump_enabled_p ())
8052 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8053 "stmt not supported.\n");
8054 gcc_unreachable ();
8055 }
8056 }
8057
8058 /* Verify SLP vectorization doesn't mess with STMT_VINFO_VEC_STMT.
8059 This would break hybrid SLP vectorization. */
8060 if (slp_node)
8061 gcc_assert (!vec_stmt
8062 && STMT_VINFO_VEC_STMT (stmt_info) == old_vec_stmt);
8063
8064 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
8065 is being vectorized, but outside the immediately enclosing loop. */
8066 if (vec_stmt
8067 && STMT_VINFO_LOOP_VINFO (stmt_info)
8068 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
8069 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
8070 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
8071 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
8072 || STMT_VINFO_RELEVANT (stmt_info) ==
8073 vect_used_in_outer_by_reduction))
8074 {
8075 struct loop *innerloop = LOOP_VINFO_LOOP (
8076 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
8077 imm_use_iterator imm_iter;
8078 use_operand_p use_p;
8079 tree scalar_dest;
8080 gimple *exit_phi;
8081
8082 if (dump_enabled_p ())
8083 dump_printf_loc (MSG_NOTE, vect_location,
8084 "Record the vdef for outer-loop vectorization.\n");
8085
8086 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
8087 (to be used when vectorizing outer-loop stmts that use the DEF of
8088 STMT). */
8089 if (gimple_code (stmt) == GIMPLE_PHI)
8090 scalar_dest = PHI_RESULT (stmt);
8091 else
8092 scalar_dest = gimple_assign_lhs (stmt);
8093
8094 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
8095 {
8096 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
8097 {
8098 exit_phi = USE_STMT (use_p);
8099 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
8100 }
8101 }
8102 }
8103
8104 /* Handle stmts whose DEF is used outside the loop-nest that is
8105 being vectorized. */
8106 if (STMT_VINFO_LIVE_P (stmt_info)
8107 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
8108 {
8109 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
8110 gcc_assert (done);
8111 }
8112
8113 if (vec_stmt)
8114 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
8115
8116 return is_store;
8117 }
8118
8119
8120 /* Remove a group of stores (for SLP or interleaving), free their
8121 stmt_vec_info. */
8122
8123 void
8124 vect_remove_stores (gimple *first_stmt)
8125 {
8126 gimple *next = first_stmt;
8127 gimple *tmp;
8128 gimple_stmt_iterator next_si;
8129
8130 while (next)
8131 {
8132 stmt_vec_info stmt_info = vinfo_for_stmt (next);
8133
8134 tmp = GROUP_NEXT_ELEMENT (stmt_info);
8135 if (is_pattern_stmt_p (stmt_info))
8136 next = STMT_VINFO_RELATED_STMT (stmt_info);
8137 /* Free the attached stmt_vec_info and remove the stmt. */
8138 next_si = gsi_for_stmt (next);
8139 unlink_stmt_vdef (next);
8140 gsi_remove (&next_si, true);
8141 release_defs (next);
8142 free_stmt_vec_info (next);
8143 next = tmp;
8144 }
8145 }
8146
8147
8148 /* Function new_stmt_vec_info.
8149
8150 Create and initialize a new stmt_vec_info struct for STMT. */
8151
8152 stmt_vec_info
8153 new_stmt_vec_info (gimple *stmt, vec_info *vinfo)
8154 {
8155 stmt_vec_info res;
8156 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
8157
8158 STMT_VINFO_TYPE (res) = undef_vec_info_type;
8159 STMT_VINFO_STMT (res) = stmt;
8160 res->vinfo = vinfo;
8161 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
8162 STMT_VINFO_LIVE_P (res) = false;
8163 STMT_VINFO_VECTYPE (res) = NULL;
8164 STMT_VINFO_VEC_STMT (res) = NULL;
8165 STMT_VINFO_VECTORIZABLE (res) = true;
8166 STMT_VINFO_IN_PATTERN_P (res) = false;
8167 STMT_VINFO_RELATED_STMT (res) = NULL;
8168 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
8169 STMT_VINFO_DATA_REF (res) = NULL;
8170 STMT_VINFO_VEC_REDUCTION_TYPE (res) = TREE_CODE_REDUCTION;
8171
8172 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
8173 STMT_VINFO_DR_OFFSET (res) = NULL;
8174 STMT_VINFO_DR_INIT (res) = NULL;
8175 STMT_VINFO_DR_STEP (res) = NULL;
8176 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
8177
8178 if (gimple_code (stmt) == GIMPLE_PHI
8179 && is_loop_header_bb_p (gimple_bb (stmt)))
8180 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
8181 else
8182 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
8183
8184 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
8185 STMT_SLP_TYPE (res) = loop_vect;
8186 GROUP_FIRST_ELEMENT (res) = NULL;
8187 GROUP_NEXT_ELEMENT (res) = NULL;
8188 GROUP_SIZE (res) = 0;
8189 GROUP_STORE_COUNT (res) = 0;
8190 GROUP_GAP (res) = 0;
8191 GROUP_SAME_DR_STMT (res) = NULL;
8192
8193 return res;
8194 }
8195
8196
8197 /* Create a hash table for stmt_vec_info. */
8198
8199 void
8200 init_stmt_vec_info_vec (void)
8201 {
8202 gcc_assert (!stmt_vec_info_vec.exists ());
8203 stmt_vec_info_vec.create (50);
8204 }
8205
8206
8207 /* Free hash table for stmt_vec_info. */
8208
8209 void
8210 free_stmt_vec_info_vec (void)
8211 {
8212 unsigned int i;
8213 stmt_vec_info info;
8214 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
8215 if (info != NULL)
8216 free_stmt_vec_info (STMT_VINFO_STMT (info));
8217 gcc_assert (stmt_vec_info_vec.exists ());
8218 stmt_vec_info_vec.release ();
8219 }
8220
8221
8222 /* Free stmt vectorization related info. */
8223
8224 void
8225 free_stmt_vec_info (gimple *stmt)
8226 {
8227 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8228
8229 if (!stmt_info)
8230 return;
8231
8232 /* Check if this statement has a related "pattern stmt"
8233 (introduced by the vectorizer during the pattern recognition
8234 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
8235 too. */
8236 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
8237 {
8238 stmt_vec_info patt_info
8239 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
8240 if (patt_info)
8241 {
8242 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
8243 gimple *patt_stmt = STMT_VINFO_STMT (patt_info);
8244 gimple_set_bb (patt_stmt, NULL);
8245 tree lhs = gimple_get_lhs (patt_stmt);
8246 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8247 release_ssa_name (lhs);
8248 if (seq)
8249 {
8250 gimple_stmt_iterator si;
8251 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
8252 {
8253 gimple *seq_stmt = gsi_stmt (si);
8254 gimple_set_bb (seq_stmt, NULL);
8255 lhs = gimple_get_lhs (seq_stmt);
8256 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8257 release_ssa_name (lhs);
8258 free_stmt_vec_info (seq_stmt);
8259 }
8260 }
8261 free_stmt_vec_info (patt_stmt);
8262 }
8263 }
8264
8265 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
8266 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
8267 set_vinfo_for_stmt (stmt, NULL);
8268 free (stmt_info);
8269 }
8270
8271
8272 /* Function get_vectype_for_scalar_type_and_size.
8273
8274 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
8275 by the target. */
8276
8277 static tree
8278 get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
8279 {
8280 machine_mode inner_mode = TYPE_MODE (scalar_type);
8281 machine_mode simd_mode;
8282 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
8283 int nunits;
8284 tree vectype;
8285
8286 if (nbytes == 0)
8287 return NULL_TREE;
8288
8289 if (GET_MODE_CLASS (inner_mode) != MODE_INT
8290 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
8291 return NULL_TREE;
8292
8293 /* For vector types of elements whose mode precision doesn't
8294 match their types precision we use a element type of mode
8295 precision. The vectorization routines will have to make sure
8296 they support the proper result truncation/extension.
8297 We also make sure to build vector types with INTEGER_TYPE
8298 component type only. */
8299 if (INTEGRAL_TYPE_P (scalar_type)
8300 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
8301 || TREE_CODE (scalar_type) != INTEGER_TYPE))
8302 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
8303 TYPE_UNSIGNED (scalar_type));
8304
8305 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
8306 When the component mode passes the above test simply use a type
8307 corresponding to that mode. The theory is that any use that
8308 would cause problems with this will disable vectorization anyway. */
8309 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
8310 && !INTEGRAL_TYPE_P (scalar_type))
8311 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
8312
8313 /* We can't build a vector type of elements with alignment bigger than
8314 their size. */
8315 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
8316 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
8317 TYPE_UNSIGNED (scalar_type));
8318
8319 /* If we felt back to using the mode fail if there was
8320 no scalar type for it. */
8321 if (scalar_type == NULL_TREE)
8322 return NULL_TREE;
8323
8324 /* If no size was supplied use the mode the target prefers. Otherwise
8325 lookup a vector mode of the specified size. */
8326 if (size == 0)
8327 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
8328 else
8329 simd_mode = mode_for_vector (inner_mode, size / nbytes);
8330 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
8331 if (nunits <= 1)
8332 return NULL_TREE;
8333
8334 vectype = build_vector_type (scalar_type, nunits);
8335
8336 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8337 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
8338 return NULL_TREE;
8339
8340 return vectype;
8341 }
8342
8343 unsigned int current_vector_size;
8344
8345 /* Function get_vectype_for_scalar_type.
8346
8347 Returns the vector type corresponding to SCALAR_TYPE as supported
8348 by the target. */
8349
8350 tree
8351 get_vectype_for_scalar_type (tree scalar_type)
8352 {
8353 tree vectype;
8354 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
8355 current_vector_size);
8356 if (vectype
8357 && current_vector_size == 0)
8358 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
8359 return vectype;
8360 }
8361
8362 /* Function get_mask_type_for_scalar_type.
8363
8364 Returns the mask type corresponding to a result of comparison
8365 of vectors of specified SCALAR_TYPE as supported by target. */
8366
8367 tree
8368 get_mask_type_for_scalar_type (tree scalar_type)
8369 {
8370 tree vectype = get_vectype_for_scalar_type (scalar_type);
8371
8372 if (!vectype)
8373 return NULL;
8374
8375 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype),
8376 current_vector_size);
8377 }
8378
8379 /* Function get_same_sized_vectype
8380
8381 Returns a vector type corresponding to SCALAR_TYPE of size
8382 VECTOR_TYPE if supported by the target. */
8383
8384 tree
8385 get_same_sized_vectype (tree scalar_type, tree vector_type)
8386 {
8387 if (TREE_CODE (scalar_type) == BOOLEAN_TYPE)
8388 return build_same_sized_truth_vector_type (vector_type);
8389
8390 return get_vectype_for_scalar_type_and_size
8391 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
8392 }
8393
8394 /* Function vect_is_simple_use.
8395
8396 Input:
8397 VINFO - the vect info of the loop or basic block that is being vectorized.
8398 OPERAND - operand in the loop or bb.
8399 Output:
8400 DEF_STMT - the defining stmt in case OPERAND is an SSA_NAME.
8401 DT - the type of definition
8402
8403 Returns whether a stmt with OPERAND can be vectorized.
8404 For loops, supportable operands are constants, loop invariants, and operands
8405 that are defined by the current iteration of the loop. Unsupportable
8406 operands are those that are defined by a previous iteration of the loop (as
8407 is the case in reduction/induction computations).
8408 For basic blocks, supportable operands are constants and bb invariants.
8409 For now, operands defined outside the basic block are not supported. */
8410
8411 bool
8412 vect_is_simple_use (tree operand, vec_info *vinfo,
8413 gimple **def_stmt, enum vect_def_type *dt)
8414 {
8415 *def_stmt = NULL;
8416 *dt = vect_unknown_def_type;
8417
8418 if (dump_enabled_p ())
8419 {
8420 dump_printf_loc (MSG_NOTE, vect_location,
8421 "vect_is_simple_use: operand ");
8422 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
8423 dump_printf (MSG_NOTE, "\n");
8424 }
8425
8426 if (CONSTANT_CLASS_P (operand))
8427 {
8428 *dt = vect_constant_def;
8429 return true;
8430 }
8431
8432 if (is_gimple_min_invariant (operand))
8433 {
8434 *dt = vect_external_def;
8435 return true;
8436 }
8437
8438 if (TREE_CODE (operand) != SSA_NAME)
8439 {
8440 if (dump_enabled_p ())
8441 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8442 "not ssa-name.\n");
8443 return false;
8444 }
8445
8446 if (SSA_NAME_IS_DEFAULT_DEF (operand))
8447 {
8448 *dt = vect_external_def;
8449 return true;
8450 }
8451
8452 *def_stmt = SSA_NAME_DEF_STMT (operand);
8453 if (dump_enabled_p ())
8454 {
8455 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
8456 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
8457 }
8458
8459 if (! vect_stmt_in_region_p (vinfo, *def_stmt))
8460 *dt = vect_external_def;
8461 else
8462 {
8463 stmt_vec_info stmt_vinfo = vinfo_for_stmt (*def_stmt);
8464 if (is_a <bb_vec_info> (vinfo) && !STMT_VINFO_VECTORIZABLE (stmt_vinfo))
8465 *dt = vect_external_def;
8466 else
8467 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
8468 }
8469
8470 if (dump_enabled_p ())
8471 {
8472 dump_printf_loc (MSG_NOTE, vect_location, "type of def: ");
8473 switch (*dt)
8474 {
8475 case vect_uninitialized_def:
8476 dump_printf (MSG_NOTE, "uninitialized\n");
8477 break;
8478 case vect_constant_def:
8479 dump_printf (MSG_NOTE, "constant\n");
8480 break;
8481 case vect_external_def:
8482 dump_printf (MSG_NOTE, "external\n");
8483 break;
8484 case vect_internal_def:
8485 dump_printf (MSG_NOTE, "internal\n");
8486 break;
8487 case vect_induction_def:
8488 dump_printf (MSG_NOTE, "induction\n");
8489 break;
8490 case vect_reduction_def:
8491 dump_printf (MSG_NOTE, "reduction\n");
8492 break;
8493 case vect_double_reduction_def:
8494 dump_printf (MSG_NOTE, "double reduction\n");
8495 break;
8496 case vect_nested_cycle:
8497 dump_printf (MSG_NOTE, "nested cycle\n");
8498 break;
8499 case vect_unknown_def_type:
8500 dump_printf (MSG_NOTE, "unknown\n");
8501 break;
8502 }
8503 }
8504
8505 if (*dt == vect_unknown_def_type)
8506 {
8507 if (dump_enabled_p ())
8508 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8509 "Unsupported pattern.\n");
8510 return false;
8511 }
8512
8513 switch (gimple_code (*def_stmt))
8514 {
8515 case GIMPLE_PHI:
8516 case GIMPLE_ASSIGN:
8517 case GIMPLE_CALL:
8518 break;
8519 default:
8520 if (dump_enabled_p ())
8521 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8522 "unsupported defining stmt:\n");
8523 return false;
8524 }
8525
8526 return true;
8527 }
8528
8529 /* Function vect_is_simple_use.
8530
8531 Same as vect_is_simple_use but also determines the vector operand
8532 type of OPERAND and stores it to *VECTYPE. If the definition of
8533 OPERAND is vect_uninitialized_def, vect_constant_def or
8534 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
8535 is responsible to compute the best suited vector type for the
8536 scalar operand. */
8537
8538 bool
8539 vect_is_simple_use (tree operand, vec_info *vinfo,
8540 gimple **def_stmt, enum vect_def_type *dt, tree *vectype)
8541 {
8542 if (!vect_is_simple_use (operand, vinfo, def_stmt, dt))
8543 return false;
8544
8545 /* Now get a vector type if the def is internal, otherwise supply
8546 NULL_TREE and leave it up to the caller to figure out a proper
8547 type for the use stmt. */
8548 if (*dt == vect_internal_def
8549 || *dt == vect_induction_def
8550 || *dt == vect_reduction_def
8551 || *dt == vect_double_reduction_def
8552 || *dt == vect_nested_cycle)
8553 {
8554 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
8555
8556 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
8557 && !STMT_VINFO_RELEVANT (stmt_info)
8558 && !STMT_VINFO_LIVE_P (stmt_info))
8559 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
8560
8561 *vectype = STMT_VINFO_VECTYPE (stmt_info);
8562 gcc_assert (*vectype != NULL_TREE);
8563 }
8564 else if (*dt == vect_uninitialized_def
8565 || *dt == vect_constant_def
8566 || *dt == vect_external_def)
8567 *vectype = NULL_TREE;
8568 else
8569 gcc_unreachable ();
8570
8571 return true;
8572 }
8573
8574
8575 /* Function supportable_widening_operation
8576
8577 Check whether an operation represented by the code CODE is a
8578 widening operation that is supported by the target platform in
8579 vector form (i.e., when operating on arguments of type VECTYPE_IN
8580 producing a result of type VECTYPE_OUT).
8581
8582 Widening operations we currently support are NOP (CONVERT), FLOAT
8583 and WIDEN_MULT. This function checks if these operations are supported
8584 by the target platform either directly (via vector tree-codes), or via
8585 target builtins.
8586
8587 Output:
8588 - CODE1 and CODE2 are codes of vector operations to be used when
8589 vectorizing the operation, if available.
8590 - MULTI_STEP_CVT determines the number of required intermediate steps in
8591 case of multi-step conversion (like char->short->int - in that case
8592 MULTI_STEP_CVT will be 1).
8593 - INTERM_TYPES contains the intermediate type required to perform the
8594 widening operation (short in the above example). */
8595
8596 bool
8597 supportable_widening_operation (enum tree_code code, gimple *stmt,
8598 tree vectype_out, tree vectype_in,
8599 enum tree_code *code1, enum tree_code *code2,
8600 int *multi_step_cvt,
8601 vec<tree> *interm_types)
8602 {
8603 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8604 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
8605 struct loop *vect_loop = NULL;
8606 machine_mode vec_mode;
8607 enum insn_code icode1, icode2;
8608 optab optab1, optab2;
8609 tree vectype = vectype_in;
8610 tree wide_vectype = vectype_out;
8611 enum tree_code c1, c2;
8612 int i;
8613 tree prev_type, intermediate_type;
8614 machine_mode intermediate_mode, prev_mode;
8615 optab optab3, optab4;
8616
8617 *multi_step_cvt = 0;
8618 if (loop_info)
8619 vect_loop = LOOP_VINFO_LOOP (loop_info);
8620
8621 switch (code)
8622 {
8623 case WIDEN_MULT_EXPR:
8624 /* The result of a vectorized widening operation usually requires
8625 two vectors (because the widened results do not fit into one vector).
8626 The generated vector results would normally be expected to be
8627 generated in the same order as in the original scalar computation,
8628 i.e. if 8 results are generated in each vector iteration, they are
8629 to be organized as follows:
8630 vect1: [res1,res2,res3,res4],
8631 vect2: [res5,res6,res7,res8].
8632
8633 However, in the special case that the result of the widening
8634 operation is used in a reduction computation only, the order doesn't
8635 matter (because when vectorizing a reduction we change the order of
8636 the computation). Some targets can take advantage of this and
8637 generate more efficient code. For example, targets like Altivec,
8638 that support widen_mult using a sequence of {mult_even,mult_odd}
8639 generate the following vectors:
8640 vect1: [res1,res3,res5,res7],
8641 vect2: [res2,res4,res6,res8].
8642
8643 When vectorizing outer-loops, we execute the inner-loop sequentially
8644 (each vectorized inner-loop iteration contributes to VF outer-loop
8645 iterations in parallel). We therefore don't allow to change the
8646 order of the computation in the inner-loop during outer-loop
8647 vectorization. */
8648 /* TODO: Another case in which order doesn't *really* matter is when we
8649 widen and then contract again, e.g. (short)((int)x * y >> 8).
8650 Normally, pack_trunc performs an even/odd permute, whereas the
8651 repack from an even/odd expansion would be an interleave, which
8652 would be significantly simpler for e.g. AVX2. */
8653 /* In any case, in order to avoid duplicating the code below, recurse
8654 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
8655 are properly set up for the caller. If we fail, we'll continue with
8656 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
8657 if (vect_loop
8658 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
8659 && !nested_in_vect_loop_p (vect_loop, stmt)
8660 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
8661 stmt, vectype_out, vectype_in,
8662 code1, code2, multi_step_cvt,
8663 interm_types))
8664 {
8665 /* Elements in a vector with vect_used_by_reduction property cannot
8666 be reordered if the use chain with this property does not have the
8667 same operation. One such an example is s += a * b, where elements
8668 in a and b cannot be reordered. Here we check if the vector defined
8669 by STMT is only directly used in the reduction statement. */
8670 tree lhs = gimple_assign_lhs (stmt);
8671 use_operand_p dummy;
8672 gimple *use_stmt;
8673 stmt_vec_info use_stmt_info = NULL;
8674 if (single_imm_use (lhs, &dummy, &use_stmt)
8675 && (use_stmt_info = vinfo_for_stmt (use_stmt))
8676 && STMT_VINFO_DEF_TYPE (use_stmt_info) == vect_reduction_def)
8677 return true;
8678 }
8679 c1 = VEC_WIDEN_MULT_LO_EXPR;
8680 c2 = VEC_WIDEN_MULT_HI_EXPR;
8681 break;
8682
8683 case DOT_PROD_EXPR:
8684 c1 = DOT_PROD_EXPR;
8685 c2 = DOT_PROD_EXPR;
8686 break;
8687
8688 case SAD_EXPR:
8689 c1 = SAD_EXPR;
8690 c2 = SAD_EXPR;
8691 break;
8692
8693 case VEC_WIDEN_MULT_EVEN_EXPR:
8694 /* Support the recursion induced just above. */
8695 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
8696 c2 = VEC_WIDEN_MULT_ODD_EXPR;
8697 break;
8698
8699 case WIDEN_LSHIFT_EXPR:
8700 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
8701 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
8702 break;
8703
8704 CASE_CONVERT:
8705 c1 = VEC_UNPACK_LO_EXPR;
8706 c2 = VEC_UNPACK_HI_EXPR;
8707 break;
8708
8709 case FLOAT_EXPR:
8710 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
8711 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
8712 break;
8713
8714 case FIX_TRUNC_EXPR:
8715 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
8716 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
8717 computing the operation. */
8718 return false;
8719
8720 default:
8721 gcc_unreachable ();
8722 }
8723
8724 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
8725 std::swap (c1, c2);
8726
8727 if (code == FIX_TRUNC_EXPR)
8728 {
8729 /* The signedness is determined from output operand. */
8730 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
8731 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
8732 }
8733 else
8734 {
8735 optab1 = optab_for_tree_code (c1, vectype, optab_default);
8736 optab2 = optab_for_tree_code (c2, vectype, optab_default);
8737 }
8738
8739 if (!optab1 || !optab2)
8740 return false;
8741
8742 vec_mode = TYPE_MODE (vectype);
8743 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
8744 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
8745 return false;
8746
8747 *code1 = c1;
8748 *code2 = c2;
8749
8750 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
8751 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
8752 return true;
8753
8754 /* Check if it's a multi-step conversion that can be done using intermediate
8755 types. */
8756
8757 prev_type = vectype;
8758 prev_mode = vec_mode;
8759
8760 if (!CONVERT_EXPR_CODE_P (code))
8761 return false;
8762
8763 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
8764 intermediate steps in promotion sequence. We try
8765 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
8766 not. */
8767 interm_types->create (MAX_INTERM_CVT_STEPS);
8768 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
8769 {
8770 intermediate_mode = insn_data[icode1].operand[0].mode;
8771 intermediate_type
8772 = lang_hooks.types.type_for_mode (intermediate_mode,
8773 TYPE_UNSIGNED (prev_type));
8774 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
8775 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
8776
8777 if (!optab3 || !optab4
8778 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
8779 || insn_data[icode1].operand[0].mode != intermediate_mode
8780 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
8781 || insn_data[icode2].operand[0].mode != intermediate_mode
8782 || ((icode1 = optab_handler (optab3, intermediate_mode))
8783 == CODE_FOR_nothing)
8784 || ((icode2 = optab_handler (optab4, intermediate_mode))
8785 == CODE_FOR_nothing))
8786 break;
8787
8788 interm_types->quick_push (intermediate_type);
8789 (*multi_step_cvt)++;
8790
8791 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
8792 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
8793 return true;
8794
8795 prev_type = intermediate_type;
8796 prev_mode = intermediate_mode;
8797 }
8798
8799 interm_types->release ();
8800 return false;
8801 }
8802
8803
8804 /* Function supportable_narrowing_operation
8805
8806 Check whether an operation represented by the code CODE is a
8807 narrowing operation that is supported by the target platform in
8808 vector form (i.e., when operating on arguments of type VECTYPE_IN
8809 and producing a result of type VECTYPE_OUT).
8810
8811 Narrowing operations we currently support are NOP (CONVERT) and
8812 FIX_TRUNC. This function checks if these operations are supported by
8813 the target platform directly via vector tree-codes.
8814
8815 Output:
8816 - CODE1 is the code of a vector operation to be used when
8817 vectorizing the operation, if available.
8818 - MULTI_STEP_CVT determines the number of required intermediate steps in
8819 case of multi-step conversion (like int->short->char - in that case
8820 MULTI_STEP_CVT will be 1).
8821 - INTERM_TYPES contains the intermediate type required to perform the
8822 narrowing operation (short in the above example). */
8823
8824 bool
8825 supportable_narrowing_operation (enum tree_code code,
8826 tree vectype_out, tree vectype_in,
8827 enum tree_code *code1, int *multi_step_cvt,
8828 vec<tree> *interm_types)
8829 {
8830 machine_mode vec_mode;
8831 enum insn_code icode1;
8832 optab optab1, interm_optab;
8833 tree vectype = vectype_in;
8834 tree narrow_vectype = vectype_out;
8835 enum tree_code c1;
8836 tree intermediate_type;
8837 machine_mode intermediate_mode, prev_mode;
8838 int i;
8839 bool uns;
8840
8841 *multi_step_cvt = 0;
8842 switch (code)
8843 {
8844 CASE_CONVERT:
8845 c1 = VEC_PACK_TRUNC_EXPR;
8846 break;
8847
8848 case FIX_TRUNC_EXPR:
8849 c1 = VEC_PACK_FIX_TRUNC_EXPR;
8850 break;
8851
8852 case FLOAT_EXPR:
8853 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
8854 tree code and optabs used for computing the operation. */
8855 return false;
8856
8857 default:
8858 gcc_unreachable ();
8859 }
8860
8861 if (code == FIX_TRUNC_EXPR)
8862 /* The signedness is determined from output operand. */
8863 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
8864 else
8865 optab1 = optab_for_tree_code (c1, vectype, optab_default);
8866
8867 if (!optab1)
8868 return false;
8869
8870 vec_mode = TYPE_MODE (vectype);
8871 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
8872 return false;
8873
8874 *code1 = c1;
8875
8876 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
8877 return true;
8878
8879 /* Check if it's a multi-step conversion that can be done using intermediate
8880 types. */
8881 prev_mode = vec_mode;
8882 if (code == FIX_TRUNC_EXPR)
8883 uns = TYPE_UNSIGNED (vectype_out);
8884 else
8885 uns = TYPE_UNSIGNED (vectype);
8886
8887 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
8888 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
8889 costly than signed. */
8890 if (code == FIX_TRUNC_EXPR && uns)
8891 {
8892 enum insn_code icode2;
8893
8894 intermediate_type
8895 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
8896 interm_optab
8897 = optab_for_tree_code (c1, intermediate_type, optab_default);
8898 if (interm_optab != unknown_optab
8899 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
8900 && insn_data[icode1].operand[0].mode
8901 == insn_data[icode2].operand[0].mode)
8902 {
8903 uns = false;
8904 optab1 = interm_optab;
8905 icode1 = icode2;
8906 }
8907 }
8908
8909 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
8910 intermediate steps in promotion sequence. We try
8911 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
8912 interm_types->create (MAX_INTERM_CVT_STEPS);
8913 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
8914 {
8915 intermediate_mode = insn_data[icode1].operand[0].mode;
8916 intermediate_type
8917 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
8918 interm_optab
8919 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
8920 optab_default);
8921 if (!interm_optab
8922 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
8923 || insn_data[icode1].operand[0].mode != intermediate_mode
8924 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
8925 == CODE_FOR_nothing))
8926 break;
8927
8928 interm_types->quick_push (intermediate_type);
8929 (*multi_step_cvt)++;
8930
8931 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
8932 return true;
8933
8934 prev_mode = intermediate_mode;
8935 optab1 = interm_optab;
8936 }
8937
8938 interm_types->release ();
8939 return false;
8940 }