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