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