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