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