]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-vect-stmts.c
[11/46] Pass back a stmt_vec_info from vect_is_simple_use
[thirdparty/gcc.git] / gcc / tree-vect-stmts.c
CommitLineData
ebfd146a 1/* Statement Analysis and Transformation for Vectorization
85ec4feb 2 Copyright (C) 2003-2018 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"
5ebaa477 51#include "tree-vector-builder.h"
f151c9e1 52#include "vec-perm-indices.h"
7cfb4d93
RS
53#include "tree-ssa-loop-niter.h"
54#include "gimple-fold.h"
ebfd146a 55
7ee2468b
SB
56/* For lang_hooks.types.type_for_mode. */
57#include "langhooks.h"
ebfd146a 58
c3e7ee41
BS
59/* Return the vectorized type for the given statement. */
60
61tree
62stmt_vectype (struct _stmt_vec_info *stmt_info)
63{
64 return STMT_VINFO_VECTYPE (stmt_info);
65}
66
67/* Return TRUE iff the given statement is in an inner loop relative to
68 the loop being vectorized. */
69bool
70stmt_in_inner_loop_p (struct _stmt_vec_info *stmt_info)
71{
355fe088 72 gimple *stmt = STMT_VINFO_STMT (stmt_info);
c3e7ee41
BS
73 basic_block bb = gimple_bb (stmt);
74 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
75 struct loop* loop;
76
77 if (!loop_vinfo)
78 return false;
79
80 loop = LOOP_VINFO_LOOP (loop_vinfo);
81
82 return (bb->loop_father == loop->inner);
83}
84
85/* Record the cost of a statement, either by directly informing the
86 target model or by saving it in a vector for later processing.
87 Return a preliminary estimate of the statement's cost. */
88
89unsigned
92345349 90record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
c3e7ee41 91 enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
92345349 92 int misalign, enum vect_cost_model_location where)
c3e7ee41 93{
cc9fe6bb
JH
94 if ((kind == vector_load || kind == unaligned_load)
95 && STMT_VINFO_GATHER_SCATTER_P (stmt_info))
96 kind = vector_gather_load;
97 if ((kind == vector_store || kind == unaligned_store)
98 && STMT_VINFO_GATHER_SCATTER_P (stmt_info))
99 kind = vector_scatter_store;
68435eb2
RB
100
101 stmt_info_for_cost si = { count, kind, where,
102 stmt_info ? STMT_VINFO_STMT (stmt_info) : NULL,
103 misalign };
104 body_cost_vec->safe_push (si);
105
106 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
107 return (unsigned)
108 (builtin_vectorization_cost (kind, vectype, misalign) * count);
c3e7ee41
BS
109}
110
272c6793
RS
111/* Return a variable of type ELEM_TYPE[NELEMS]. */
112
113static tree
114create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
115{
116 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
117 "vect_array");
118}
119
120/* ARRAY is an array of vectors created by create_vector_array.
121 Return an SSA_NAME for the vector in index N. The reference
122 is part of the vectorization of STMT and the vector is associated
123 with scalar destination SCALAR_DEST. */
124
125static tree
355fe088 126read_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree scalar_dest,
272c6793
RS
127 tree array, unsigned HOST_WIDE_INT n)
128{
129 tree vect_type, vect, vect_name, array_ref;
355fe088 130 gimple *new_stmt;
272c6793
RS
131
132 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
133 vect_type = TREE_TYPE (TREE_TYPE (array));
134 vect = vect_create_destination_var (scalar_dest, vect_type);
135 array_ref = build4 (ARRAY_REF, vect_type, array,
136 build_int_cst (size_type_node, n),
137 NULL_TREE, NULL_TREE);
138
139 new_stmt = gimple_build_assign (vect, array_ref);
140 vect_name = make_ssa_name (vect, new_stmt);
141 gimple_assign_set_lhs (new_stmt, vect_name);
142 vect_finish_stmt_generation (stmt, new_stmt, gsi);
272c6793
RS
143
144 return vect_name;
145}
146
147/* ARRAY is an array of vectors created by create_vector_array.
148 Emit code to store SSA_NAME VECT in index N of the array.
149 The store is part of the vectorization of STMT. */
150
151static void
355fe088 152write_vector_array (gimple *stmt, gimple_stmt_iterator *gsi, tree vect,
272c6793
RS
153 tree array, unsigned HOST_WIDE_INT n)
154{
155 tree array_ref;
355fe088 156 gimple *new_stmt;
272c6793
RS
157
158 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
159 build_int_cst (size_type_node, n),
160 NULL_TREE, NULL_TREE);
161
162 new_stmt = gimple_build_assign (array_ref, vect);
163 vect_finish_stmt_generation (stmt, new_stmt, gsi);
272c6793
RS
164}
165
166/* PTR is a pointer to an array of type TYPE. Return a representation
167 of *PTR. The memory reference replaces those in FIRST_DR
168 (and its group). */
169
170static tree
44fc7854 171create_array_ref (tree type, tree ptr, tree alias_ptr_type)
272c6793 172{
44fc7854 173 tree mem_ref;
272c6793 174
272c6793
RS
175 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
176 /* Arrays have the same alignment as their type. */
644ffefd 177 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
272c6793
RS
178 return mem_ref;
179}
180
3ba4ff41
RS
181/* Add a clobber of variable VAR to the vectorization of STMT.
182 Emit the clobber before *GSI. */
183
184static void
185vect_clobber_variable (gimple *stmt, gimple_stmt_iterator *gsi, tree var)
186{
187 tree clobber = build_clobber (TREE_TYPE (var));
188 gimple *new_stmt = gimple_build_assign (var, clobber);
189 vect_finish_stmt_generation (stmt, new_stmt, gsi);
190}
191
ebfd146a
IR
192/* Utility functions used by vect_mark_stmts_to_be_vectorized. */
193
194/* Function vect_mark_relevant.
195
196 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
197
198static void
355fe088 199vect_mark_relevant (vec<gimple *> *worklist, gimple *stmt,
97ecdb46 200 enum vect_relevant relevant, bool live_p)
ebfd146a
IR
201{
202 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
203 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
204 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
355fe088 205 gimple *pattern_stmt;
ebfd146a 206
73fbfcad 207 if (dump_enabled_p ())
66c16fd9
RB
208 {
209 dump_printf_loc (MSG_NOTE, vect_location,
210 "mark relevant %d, live %d: ", relevant, live_p);
211 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
212 }
ebfd146a 213
83197f37
IR
214 /* If this stmt is an original stmt in a pattern, we might need to mark its
215 related pattern stmt instead of the original stmt. However, such stmts
216 may have their own uses that are not in any pattern, in such cases the
217 stmt itself should be marked. */
ebfd146a
IR
218 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
219 {
97ecdb46
JJ
220 /* This is the last stmt in a sequence that was detected as a
221 pattern that can potentially be vectorized. Don't mark the stmt
222 as relevant/live because it's not going to be vectorized.
223 Instead mark the pattern-stmt that replaces it. */
83197f37 224
97ecdb46
JJ
225 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
226
227 if (dump_enabled_p ())
228 dump_printf_loc (MSG_NOTE, vect_location,
229 "last stmt in pattern. don't mark"
230 " relevant/live.\n");
231 stmt_info = vinfo_for_stmt (pattern_stmt);
232 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == stmt);
233 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
234 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
235 stmt = pattern_stmt;
ebfd146a
IR
236 }
237
238 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
239 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
240 STMT_VINFO_RELEVANT (stmt_info) = relevant;
241
242 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
243 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
244 {
73fbfcad 245 if (dump_enabled_p ())
78c60e3d 246 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 247 "already marked relevant/live.\n");
ebfd146a
IR
248 return;
249 }
250
9771b263 251 worklist->safe_push (stmt);
ebfd146a
IR
252}
253
254
b28ead45
AH
255/* Function is_simple_and_all_uses_invariant
256
257 Return true if STMT is simple and all uses of it are invariant. */
258
259bool
260is_simple_and_all_uses_invariant (gimple *stmt, loop_vec_info loop_vinfo)
261{
262 tree op;
b28ead45
AH
263 ssa_op_iter iter;
264
265 if (!is_gimple_assign (stmt))
266 return false;
267
268 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
269 {
270 enum vect_def_type dt = vect_uninitialized_def;
271
894dd753 272 if (!vect_is_simple_use (op, loop_vinfo, &dt))
b28ead45
AH
273 {
274 if (dump_enabled_p ())
275 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
276 "use not simple.\n");
277 return false;
278 }
279
280 if (dt != vect_external_def && dt != vect_constant_def)
281 return false;
282 }
283 return true;
284}
285
ebfd146a
IR
286/* Function vect_stmt_relevant_p.
287
288 Return true if STMT in loop that is represented by LOOP_VINFO is
289 "relevant for vectorization".
290
291 A stmt is considered "relevant for vectorization" if:
292 - it has uses outside the loop.
293 - it has vdefs (it alters memory).
294 - control stmts in the loop (except for the exit condition).
295
296 CHECKME: what other side effects would the vectorizer allow? */
297
298static bool
355fe088 299vect_stmt_relevant_p (gimple *stmt, loop_vec_info loop_vinfo,
ebfd146a
IR
300 enum vect_relevant *relevant, bool *live_p)
301{
302 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
303 ssa_op_iter op_iter;
304 imm_use_iterator imm_iter;
305 use_operand_p use_p;
306 def_operand_p def_p;
307
8644a673 308 *relevant = vect_unused_in_scope;
ebfd146a
IR
309 *live_p = false;
310
311 /* cond stmt other than loop exit cond. */
b8698a0f
L
312 if (is_ctrl_stmt (stmt)
313 && STMT_VINFO_TYPE (vinfo_for_stmt (stmt))
314 != loop_exit_ctrl_vec_info_type)
8644a673 315 *relevant = vect_used_in_scope;
ebfd146a
IR
316
317 /* changing memory. */
318 if (gimple_code (stmt) != GIMPLE_PHI)
ac6aeab4
RB
319 if (gimple_vdef (stmt)
320 && !gimple_clobber_p (stmt))
ebfd146a 321 {
73fbfcad 322 if (dump_enabled_p ())
78c60e3d 323 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 324 "vec_stmt_relevant_p: stmt has vdefs.\n");
8644a673 325 *relevant = vect_used_in_scope;
ebfd146a
IR
326 }
327
328 /* uses outside the loop. */
329 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
330 {
331 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
332 {
333 basic_block bb = gimple_bb (USE_STMT (use_p));
334 if (!flow_bb_inside_loop_p (loop, bb))
335 {
73fbfcad 336 if (dump_enabled_p ())
78c60e3d 337 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 338 "vec_stmt_relevant_p: used out of loop.\n");
ebfd146a 339
3157b0c2
AO
340 if (is_gimple_debug (USE_STMT (use_p)))
341 continue;
342
ebfd146a
IR
343 /* We expect all such uses to be in the loop exit phis
344 (because of loop closed form) */
345 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
346 gcc_assert (bb == single_exit (loop)->dest);
347
348 *live_p = true;
349 }
350 }
351 }
352
3a2edf4c
AH
353 if (*live_p && *relevant == vect_unused_in_scope
354 && !is_simple_and_all_uses_invariant (stmt, loop_vinfo))
b28ead45
AH
355 {
356 if (dump_enabled_p ())
357 dump_printf_loc (MSG_NOTE, vect_location,
358 "vec_stmt_relevant_p: stmt live but not relevant.\n");
359 *relevant = vect_used_only_live;
360 }
361
ebfd146a
IR
362 return (*live_p || *relevant);
363}
364
365
b8698a0f 366/* Function exist_non_indexing_operands_for_use_p
ebfd146a 367
ff802fa1 368 USE is one of the uses attached to STMT. Check if USE is
ebfd146a
IR
369 used in STMT for anything other than indexing an array. */
370
371static bool
355fe088 372exist_non_indexing_operands_for_use_p (tree use, gimple *stmt)
ebfd146a
IR
373{
374 tree operand;
375 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
59a05b0c 376
ff802fa1 377 /* USE corresponds to some operand in STMT. If there is no data
ebfd146a
IR
378 reference in STMT, then any operand that corresponds to USE
379 is not indexing an array. */
380 if (!STMT_VINFO_DATA_REF (stmt_info))
381 return true;
59a05b0c 382
ebfd146a
IR
383 /* STMT has a data_ref. FORNOW this means that its of one of
384 the following forms:
385 -1- ARRAY_REF = var
386 -2- var = ARRAY_REF
387 (This should have been verified in analyze_data_refs).
388
389 'var' in the second case corresponds to a def, not a use,
b8698a0f 390 so USE cannot correspond to any operands that are not used
ebfd146a
IR
391 for array indexing.
392
393 Therefore, all we need to check is if STMT falls into the
394 first case, and whether var corresponds to USE. */
ebfd146a
IR
395
396 if (!gimple_assign_copy_p (stmt))
5ce9450f
JJ
397 {
398 if (is_gimple_call (stmt)
399 && gimple_call_internal_p (stmt))
bfaa08b7
RS
400 {
401 internal_fn ifn = gimple_call_internal_fn (stmt);
402 int mask_index = internal_fn_mask_index (ifn);
403 if (mask_index >= 0
404 && use == gimple_call_arg (stmt, mask_index))
405 return true;
f307441a
RS
406 int stored_value_index = internal_fn_stored_value_index (ifn);
407 if (stored_value_index >= 0
408 && use == gimple_call_arg (stmt, stored_value_index))
409 return true;
bfaa08b7
RS
410 if (internal_gather_scatter_fn_p (ifn)
411 && use == gimple_call_arg (stmt, 1))
412 return true;
bfaa08b7 413 }
5ce9450f
JJ
414 return false;
415 }
416
59a05b0c
EB
417 if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
418 return false;
ebfd146a 419 operand = gimple_assign_rhs1 (stmt);
ebfd146a
IR
420 if (TREE_CODE (operand) != SSA_NAME)
421 return false;
422
423 if (operand == use)
424 return true;
425
426 return false;
427}
428
429
b8698a0f 430/*
ebfd146a
IR
431 Function process_use.
432
433 Inputs:
434 - a USE in STMT in a loop represented by LOOP_VINFO
b28ead45 435 - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
ff802fa1 436 that defined USE. This is done by calling mark_relevant and passing it
ebfd146a 437 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
aec7ae7d
JJ
438 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
439 be performed.
ebfd146a
IR
440
441 Outputs:
442 Generally, LIVE_P and RELEVANT are used to define the liveness and
443 relevance info of the DEF_STMT of this USE:
444 STMT_VINFO_LIVE_P (DEF_STMT_info) <-- live_p
445 STMT_VINFO_RELEVANT (DEF_STMT_info) <-- relevant
446 Exceptions:
447 - case 1: If USE is used only for address computations (e.g. array indexing),
b8698a0f 448 which does not need to be directly vectorized, then the liveness/relevance
ebfd146a 449 of the respective DEF_STMT is left unchanged.
b8698a0f
L
450 - case 2: If STMT is a reduction phi and DEF_STMT is a reduction stmt, we
451 skip DEF_STMT cause it had already been processed.
ebfd146a
IR
452 - case 3: If DEF_STMT and STMT are in different nests, then "relevant" will
453 be modified accordingly.
454
455 Return true if everything is as expected. Return false otherwise. */
456
457static bool
b28ead45 458process_use (gimple *stmt, tree use, loop_vec_info loop_vinfo,
355fe088 459 enum vect_relevant relevant, vec<gimple *> *worklist,
aec7ae7d 460 bool force)
ebfd146a 461{
ebfd146a
IR
462 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
463 stmt_vec_info dstmt_vinfo;
464 basic_block bb, def_bb;
ebfd146a
IR
465 enum vect_def_type dt;
466
b8698a0f 467 /* case 1: we are only interested in uses that need to be vectorized. Uses
ebfd146a 468 that are used for address computation are not considered relevant. */
aec7ae7d 469 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt))
ebfd146a
IR
470 return true;
471
fef96d8e 472 if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
b8698a0f 473 {
73fbfcad 474 if (dump_enabled_p ())
78c60e3d 475 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 476 "not vectorized: unsupported use in stmt.\n");
ebfd146a
IR
477 return false;
478 }
479
fef96d8e 480 if (!dstmt_vinfo)
ebfd146a
IR
481 return true;
482
fef96d8e 483 def_bb = gimple_bb (dstmt_vinfo->stmt);
ebfd146a 484
fef96d8e
RS
485 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
486 DSTMT_VINFO must have already been processed, because this should be the
b8698a0f 487 only way that STMT, which is a reduction-phi, was put in the worklist,
fef96d8e 488 as there should be no other uses for DSTMT_VINFO in the loop. So we just
ebfd146a 489 check that everything is as expected, and we are done. */
ebfd146a
IR
490 bb = gimple_bb (stmt);
491 if (gimple_code (stmt) == GIMPLE_PHI
492 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
fef96d8e 493 && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
ebfd146a
IR
494 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
495 && bb->loop_father == def_bb->loop_father)
496 {
73fbfcad 497 if (dump_enabled_p ())
78c60e3d 498 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 499 "reduc-stmt defining reduc-phi in the same nest.\n");
ebfd146a 500 gcc_assert (STMT_VINFO_RELEVANT (dstmt_vinfo) < vect_used_by_reduction);
b8698a0f 501 gcc_assert (STMT_VINFO_LIVE_P (dstmt_vinfo)
8644a673 502 || STMT_VINFO_RELEVANT (dstmt_vinfo) > vect_unused_in_scope);
ebfd146a
IR
503 return true;
504 }
505
506 /* case 3a: outer-loop stmt defining an inner-loop stmt:
507 outer-loop-header-bb:
fef96d8e 508 d = dstmt_vinfo
ebfd146a
IR
509 inner-loop:
510 stmt # use (d)
511 outer-loop-tail-bb:
512 ... */
513 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
514 {
73fbfcad 515 if (dump_enabled_p ())
78c60e3d 516 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 517 "outer-loop def-stmt defining inner-loop stmt.\n");
7c5222ff 518
ebfd146a
IR
519 switch (relevant)
520 {
8644a673 521 case vect_unused_in_scope:
7c5222ff
IR
522 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
523 vect_used_in_scope : vect_unused_in_scope;
ebfd146a 524 break;
7c5222ff 525
ebfd146a 526 case vect_used_in_outer_by_reduction:
7c5222ff 527 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
ebfd146a
IR
528 relevant = vect_used_by_reduction;
529 break;
7c5222ff 530
ebfd146a 531 case vect_used_in_outer:
7c5222ff 532 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
8644a673 533 relevant = vect_used_in_scope;
ebfd146a 534 break;
7c5222ff 535
8644a673 536 case vect_used_in_scope:
ebfd146a
IR
537 break;
538
539 default:
540 gcc_unreachable ();
b8698a0f 541 }
ebfd146a
IR
542 }
543
544 /* case 3b: inner-loop stmt defining an outer-loop stmt:
545 outer-loop-header-bb:
546 ...
547 inner-loop:
fef96d8e 548 d = dstmt_vinfo
06066f92 549 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
ebfd146a
IR
550 stmt # use (d) */
551 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
552 {
73fbfcad 553 if (dump_enabled_p ())
78c60e3d 554 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 555 "inner-loop def-stmt defining outer-loop stmt.\n");
7c5222ff 556
ebfd146a
IR
557 switch (relevant)
558 {
8644a673 559 case vect_unused_in_scope:
b8698a0f 560 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
06066f92 561 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
a70d6342 562 vect_used_in_outer_by_reduction : vect_unused_in_scope;
ebfd146a
IR
563 break;
564
ebfd146a 565 case vect_used_by_reduction:
b28ead45 566 case vect_used_only_live:
ebfd146a
IR
567 relevant = vect_used_in_outer_by_reduction;
568 break;
569
8644a673 570 case vect_used_in_scope:
ebfd146a
IR
571 relevant = vect_used_in_outer;
572 break;
573
574 default:
575 gcc_unreachable ();
576 }
577 }
643a9684
RB
578 /* We are also not interested in uses on loop PHI backedges that are
579 inductions. Otherwise we'll needlessly vectorize the IV increment
e294f495
RB
580 and cause hybrid SLP for SLP inductions. Unless the PHI is live
581 of course. */
643a9684
RB
582 else if (gimple_code (stmt) == GIMPLE_PHI
583 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
e294f495 584 && ! STMT_VINFO_LIVE_P (stmt_vinfo)
643a9684
RB
585 && (PHI_ARG_DEF_FROM_EDGE (stmt, loop_latch_edge (bb->loop_father))
586 == use))
587 {
588 if (dump_enabled_p ())
589 dump_printf_loc (MSG_NOTE, vect_location,
590 "induction value on backedge.\n");
591 return true;
592 }
593
ebfd146a 594
fef96d8e 595 vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
ebfd146a
IR
596 return true;
597}
598
599
600/* Function vect_mark_stmts_to_be_vectorized.
601
602 Not all stmts in the loop need to be vectorized. For example:
603
604 for i...
605 for j...
606 1. T0 = i + j
607 2. T1 = a[T0]
608
609 3. j = j + 1
610
611 Stmt 1 and 3 do not need to be vectorized, because loop control and
612 addressing of vectorized data-refs are handled differently.
613
614 This pass detects such stmts. */
615
616bool
617vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
618{
ebfd146a
IR
619 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
620 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
621 unsigned int nbbs = loop->num_nodes;
622 gimple_stmt_iterator si;
355fe088 623 gimple *stmt;
ebfd146a
IR
624 unsigned int i;
625 stmt_vec_info stmt_vinfo;
626 basic_block bb;
355fe088 627 gimple *phi;
ebfd146a 628 bool live_p;
b28ead45 629 enum vect_relevant relevant;
ebfd146a 630
adac3a68 631 DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
ebfd146a 632
355fe088 633 auto_vec<gimple *, 64> worklist;
ebfd146a
IR
634
635 /* 1. Init worklist. */
636 for (i = 0; i < nbbs; i++)
637 {
638 bb = bbs[i];
639 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
b8698a0f 640 {
ebfd146a 641 phi = gsi_stmt (si);
73fbfcad 642 if (dump_enabled_p ())
ebfd146a 643 {
78c60e3d
SS
644 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? ");
645 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
ebfd146a
IR
646 }
647
648 if (vect_stmt_relevant_p (phi, loop_vinfo, &relevant, &live_p))
97ecdb46 649 vect_mark_relevant (&worklist, phi, relevant, live_p);
ebfd146a
IR
650 }
651 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
652 {
653 stmt = gsi_stmt (si);
73fbfcad 654 if (dump_enabled_p ())
ebfd146a 655 {
78c60e3d
SS
656 dump_printf_loc (MSG_NOTE, vect_location, "init: stmt relevant? ");
657 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
b8698a0f 658 }
ebfd146a
IR
659
660 if (vect_stmt_relevant_p (stmt, loop_vinfo, &relevant, &live_p))
97ecdb46 661 vect_mark_relevant (&worklist, stmt, relevant, live_p);
ebfd146a
IR
662 }
663 }
664
665 /* 2. Process_worklist */
9771b263 666 while (worklist.length () > 0)
ebfd146a
IR
667 {
668 use_operand_p use_p;
669 ssa_op_iter iter;
670
9771b263 671 stmt = worklist.pop ();
73fbfcad 672 if (dump_enabled_p ())
ebfd146a 673 {
78c60e3d
SS
674 dump_printf_loc (MSG_NOTE, vect_location, "worklist: examine stmt: ");
675 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
ebfd146a
IR
676 }
677
b8698a0f 678 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
b28ead45
AH
679 (DEF_STMT) as relevant/irrelevant according to the relevance property
680 of STMT. */
ebfd146a
IR
681 stmt_vinfo = vinfo_for_stmt (stmt);
682 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
ebfd146a 683
b28ead45
AH
684 /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
685 propagated as is to the DEF_STMTs of its USEs.
ebfd146a
IR
686
687 One exception is when STMT has been identified as defining a reduction
b28ead45 688 variable; in this case we set the relevance to vect_used_by_reduction.
ebfd146a 689 This is because we distinguish between two kinds of relevant stmts -
b8698a0f 690 those that are used by a reduction computation, and those that are
ff802fa1 691 (also) used by a regular computation. This allows us later on to
b8698a0f 692 identify stmts that are used solely by a reduction, and therefore the
7c5222ff 693 order of the results that they produce does not have to be kept. */
ebfd146a 694
b28ead45 695 switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
ebfd146a 696 {
06066f92 697 case vect_reduction_def:
b28ead45
AH
698 gcc_assert (relevant != vect_unused_in_scope);
699 if (relevant != vect_unused_in_scope
700 && relevant != vect_used_in_scope
701 && relevant != vect_used_by_reduction
702 && relevant != vect_used_only_live)
06066f92 703 {
b28ead45
AH
704 if (dump_enabled_p ())
705 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
706 "unsupported use of reduction.\n");
707 return false;
06066f92 708 }
06066f92 709 break;
b8698a0f 710
06066f92 711 case vect_nested_cycle:
b28ead45
AH
712 if (relevant != vect_unused_in_scope
713 && relevant != vect_used_in_outer_by_reduction
714 && relevant != vect_used_in_outer)
06066f92 715 {
73fbfcad 716 if (dump_enabled_p ())
78c60e3d 717 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 718 "unsupported use of nested cycle.\n");
7c5222ff 719
06066f92
IR
720 return false;
721 }
b8698a0f
L
722 break;
723
06066f92 724 case vect_double_reduction_def:
b28ead45
AH
725 if (relevant != vect_unused_in_scope
726 && relevant != vect_used_by_reduction
727 && relevant != vect_used_only_live)
06066f92 728 {
73fbfcad 729 if (dump_enabled_p ())
78c60e3d 730 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 731 "unsupported use of double reduction.\n");
7c5222ff 732
7c5222ff 733 return false;
06066f92 734 }
b8698a0f 735 break;
7c5222ff 736
06066f92
IR
737 default:
738 break;
7c5222ff 739 }
b8698a0f 740
aec7ae7d 741 if (is_pattern_stmt_p (stmt_vinfo))
9d5e7640
IR
742 {
743 /* Pattern statements are not inserted into the code, so
744 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
745 have to scan the RHS or function arguments instead. */
746 if (is_gimple_assign (stmt))
747 {
69d2aade
JJ
748 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
749 tree op = gimple_assign_rhs1 (stmt);
750
751 i = 1;
752 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
753 {
754 if (!process_use (stmt, TREE_OPERAND (op, 0), loop_vinfo,
b28ead45 755 relevant, &worklist, false)
69d2aade 756 || !process_use (stmt, TREE_OPERAND (op, 1), loop_vinfo,
b28ead45 757 relevant, &worklist, false))
566d377a 758 return false;
69d2aade
JJ
759 i = 2;
760 }
761 for (; i < gimple_num_ops (stmt); i++)
9d5e7640 762 {
69d2aade 763 op = gimple_op (stmt, i);
afbe6325 764 if (TREE_CODE (op) == SSA_NAME
b28ead45 765 && !process_use (stmt, op, loop_vinfo, relevant,
afbe6325 766 &worklist, false))
07687835 767 return false;
9d5e7640
IR
768 }
769 }
770 else if (is_gimple_call (stmt))
771 {
772 for (i = 0; i < gimple_call_num_args (stmt); i++)
773 {
774 tree arg = gimple_call_arg (stmt, i);
b28ead45 775 if (!process_use (stmt, arg, loop_vinfo, relevant,
aec7ae7d 776 &worklist, false))
07687835 777 return false;
9d5e7640
IR
778 }
779 }
780 }
781 else
782 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
783 {
784 tree op = USE_FROM_PTR (use_p);
b28ead45 785 if (!process_use (stmt, op, loop_vinfo, relevant,
aec7ae7d 786 &worklist, false))
07687835 787 return false;
9d5e7640 788 }
aec7ae7d 789
3bab6342 790 if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
aec7ae7d 791 {
134c85ca
RS
792 gather_scatter_info gs_info;
793 if (!vect_check_gather_scatter (stmt, loop_vinfo, &gs_info))
794 gcc_unreachable ();
795 if (!process_use (stmt, gs_info.offset, loop_vinfo, relevant,
796 &worklist, true))
566d377a 797 return false;
aec7ae7d 798 }
ebfd146a
IR
799 } /* while worklist */
800
ebfd146a
IR
801 return true;
802}
803
68435eb2
RB
804/* Compute the prologue cost for invariant or constant operands. */
805
806static unsigned
807vect_prologue_cost_for_slp_op (slp_tree node, stmt_vec_info stmt_info,
808 unsigned opno, enum vect_def_type dt,
809 stmt_vector_for_cost *cost_vec)
810{
811 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
812 tree op = gimple_op (stmt, opno);
813 unsigned prologue_cost = 0;
814
815 /* Without looking at the actual initializer a vector of
816 constants can be implemented as load from the constant pool.
817 When all elements are the same we can use a splat. */
818 tree vectype = get_vectype_for_scalar_type (TREE_TYPE (op));
819 unsigned group_size = SLP_TREE_SCALAR_STMTS (node).length ();
820 unsigned num_vects_to_check;
821 unsigned HOST_WIDE_INT const_nunits;
822 unsigned nelt_limit;
823 if (TYPE_VECTOR_SUBPARTS (vectype).is_constant (&const_nunits)
824 && ! multiple_p (const_nunits, group_size))
825 {
826 num_vects_to_check = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
827 nelt_limit = const_nunits;
828 }
829 else
830 {
831 /* If either the vector has variable length or the vectors
832 are composed of repeated whole groups we only need to
833 cost construction once. All vectors will be the same. */
834 num_vects_to_check = 1;
835 nelt_limit = group_size;
836 }
837 tree elt = NULL_TREE;
838 unsigned nelt = 0;
839 for (unsigned j = 0; j < num_vects_to_check * nelt_limit; ++j)
840 {
841 unsigned si = j % group_size;
842 if (nelt == 0)
843 elt = gimple_op (SLP_TREE_SCALAR_STMTS (node)[si], opno);
844 /* ??? We're just tracking whether all operands of a single
845 vector initializer are the same, ideally we'd check if
846 we emitted the same one already. */
847 else if (elt != gimple_op (SLP_TREE_SCALAR_STMTS (node)[si],
848 opno))
849 elt = NULL_TREE;
850 nelt++;
851 if (nelt == nelt_limit)
852 {
853 /* ??? We need to pass down stmt_info for a vector type
854 even if it points to the wrong stmt. */
855 prologue_cost += record_stmt_cost
856 (cost_vec, 1,
857 dt == vect_external_def
858 ? (elt ? scalar_to_vec : vec_construct)
859 : vector_load,
860 stmt_info, 0, vect_prologue);
861 nelt = 0;
862 }
863 }
864
865 return prologue_cost;
866}
ebfd146a 867
b8698a0f 868/* Function vect_model_simple_cost.
ebfd146a 869
b8698a0f 870 Models cost for simple operations, i.e. those that only emit ncopies of a
ebfd146a
IR
871 single op. Right now, this does not account for multiple insns that could
872 be generated for the single vector op. We will handle that shortly. */
873
68435eb2 874static void
b8698a0f 875vect_model_simple_cost (stmt_vec_info stmt_info, int ncopies,
92345349 876 enum vect_def_type *dt,
4fc5ebf1 877 int ndts,
68435eb2
RB
878 slp_tree node,
879 stmt_vector_for_cost *cost_vec)
ebfd146a 880{
92345349 881 int inside_cost = 0, prologue_cost = 0;
ebfd146a 882
68435eb2 883 gcc_assert (cost_vec != NULL);
ebfd146a 884
68435eb2
RB
885 /* ??? Somehow we need to fix this at the callers. */
886 if (node)
887 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
888
889 if (node)
890 {
891 /* Scan operands and account for prologue cost of constants/externals.
892 ??? This over-estimates cost for multiple uses and should be
893 re-engineered. */
894 gimple *stmt = SLP_TREE_SCALAR_STMTS (node)[0];
895 tree lhs = gimple_get_lhs (stmt);
896 for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
897 {
898 tree op = gimple_op (stmt, i);
68435eb2
RB
899 enum vect_def_type dt;
900 if (!op || op == lhs)
901 continue;
894dd753 902 if (vect_is_simple_use (op, stmt_info->vinfo, &dt)
68435eb2
RB
903 && (dt == vect_constant_def || dt == vect_external_def))
904 prologue_cost += vect_prologue_cost_for_slp_op (node, stmt_info,
905 i, dt, cost_vec);
906 }
907 }
908 else
909 /* Cost the "broadcast" of a scalar operand in to a vector operand.
910 Use scalar_to_vec to cost the broadcast, as elsewhere in the vector
911 cost model. */
912 for (int i = 0; i < ndts; i++)
913 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
914 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
915 stmt_info, 0, vect_prologue);
916
917 /* Adjust for two-operator SLP nodes. */
918 if (node && SLP_TREE_TWO_OPERATORS (node))
919 {
920 ncopies *= 2;
921 inside_cost += record_stmt_cost (cost_vec, ncopies, vec_perm,
922 stmt_info, 0, vect_body);
923 }
c3e7ee41
BS
924
925 /* Pass the inside-of-loop statements to the target-specific cost model. */
68435eb2
RB
926 inside_cost += record_stmt_cost (cost_vec, ncopies, vector_stmt,
927 stmt_info, 0, vect_body);
c3e7ee41 928
73fbfcad 929 if (dump_enabled_p ())
78c60e3d
SS
930 dump_printf_loc (MSG_NOTE, vect_location,
931 "vect_model_simple_cost: inside_cost = %d, "
e645e942 932 "prologue_cost = %d .\n", inside_cost, prologue_cost);
ebfd146a
IR
933}
934
935
8bd37302
BS
936/* Model cost for type demotion and promotion operations. PWR is normally
937 zero for single-step promotions and demotions. It will be one if
938 two-step promotion/demotion is required, and so on. Each additional
939 step doubles the number of instructions required. */
940
941static void
942vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
68435eb2
RB
943 enum vect_def_type *dt, int pwr,
944 stmt_vector_for_cost *cost_vec)
8bd37302
BS
945{
946 int i, tmp;
92345349 947 int inside_cost = 0, prologue_cost = 0;
c3e7ee41 948
8bd37302
BS
949 for (i = 0; i < pwr + 1; i++)
950 {
951 tmp = (STMT_VINFO_TYPE (stmt_info) == type_promotion_vec_info_type) ?
952 (i + 1) : i;
68435eb2
RB
953 inside_cost += record_stmt_cost (cost_vec, vect_pow2 (tmp),
954 vec_promote_demote, stmt_info, 0,
955 vect_body);
8bd37302
BS
956 }
957
958 /* FORNOW: Assuming maximum 2 args per stmts. */
959 for (i = 0; i < 2; i++)
92345349 960 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
68435eb2
RB
961 prologue_cost += record_stmt_cost (cost_vec, 1, vector_stmt,
962 stmt_info, 0, vect_prologue);
8bd37302 963
73fbfcad 964 if (dump_enabled_p ())
78c60e3d
SS
965 dump_printf_loc (MSG_NOTE, vect_location,
966 "vect_model_promotion_demotion_cost: inside_cost = %d, "
e645e942 967 "prologue_cost = %d .\n", inside_cost, prologue_cost);
8bd37302
BS
968}
969
ebfd146a
IR
970/* Function vect_model_store_cost
971
0d0293ac
MM
972 Models cost for stores. In the case of grouped accesses, one access
973 has the overhead of the grouped access attributed to it. */
ebfd146a 974
68435eb2 975static void
b8698a0f 976vect_model_store_cost (stmt_vec_info stmt_info, int ncopies,
68435eb2 977 enum vect_def_type dt,
2de001ee 978 vect_memory_access_type memory_access_type,
9ce4345a 979 vec_load_store_type vls_type, slp_tree slp_node,
68435eb2 980 stmt_vector_for_cost *cost_vec)
ebfd146a 981{
92345349 982 unsigned int inside_cost = 0, prologue_cost = 0;
892a981f
RS
983 gimple *first_stmt = STMT_VINFO_STMT (stmt_info);
984 bool grouped_access_p = STMT_VINFO_GROUPED_ACCESS (stmt_info);
ebfd146a 985
68435eb2
RB
986 /* ??? Somehow we need to fix this at the callers. */
987 if (slp_node)
988 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
989
9ce4345a 990 if (vls_type == VLS_STORE_INVARIANT)
68435eb2
RB
991 {
992 if (slp_node)
993 prologue_cost += vect_prologue_cost_for_slp_op (slp_node, stmt_info,
994 1, dt, cost_vec);
995 else
996 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
997 stmt_info, 0, vect_prologue);
998 }
ebfd146a 999
892a981f
RS
1000 /* Grouped stores update all elements in the group at once,
1001 so we want the DR for the first statement. */
1002 if (!slp_node && grouped_access_p)
57c454d2 1003 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
ebfd146a 1004
892a981f
RS
1005 /* True if we should include any once-per-group costs as well as
1006 the cost of the statement itself. For SLP we only get called
1007 once per group anyhow. */
1008 bool first_stmt_p = (first_stmt == STMT_VINFO_STMT (stmt_info));
1009
272c6793 1010 /* We assume that the cost of a single store-lanes instruction is
2c53b149 1011 equivalent to the cost of DR_GROUP_SIZE separate stores. If a grouped
272c6793 1012 access is instead being provided by a permute-and-store operation,
2de001ee
RS
1013 include the cost of the permutes. */
1014 if (first_stmt_p
1015 && memory_access_type == VMAT_CONTIGUOUS_PERMUTE)
ebfd146a 1016 {
e1377713
ES
1017 /* Uses a high and low interleave or shuffle operations for each
1018 needed permute. */
2c53b149 1019 int group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
e1377713 1020 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
68435eb2 1021 inside_cost = record_stmt_cost (cost_vec, nstmts, vec_perm,
92345349 1022 stmt_info, 0, vect_body);
ebfd146a 1023
73fbfcad 1024 if (dump_enabled_p ())
78c60e3d 1025 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 1026 "vect_model_store_cost: strided group_size = %d .\n",
78c60e3d 1027 group_size);
ebfd146a
IR
1028 }
1029
cee62fee 1030 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
ebfd146a 1031 /* Costs of the stores. */
067bc855
RB
1032 if (memory_access_type == VMAT_ELEMENTWISE
1033 || memory_access_type == VMAT_GATHER_SCATTER)
c5126ce8
RS
1034 {
1035 /* N scalar stores plus extracting the elements. */
1036 unsigned int assumed_nunits = vect_nunits_for_cost (vectype);
68435eb2 1037 inside_cost += record_stmt_cost (cost_vec,
c5126ce8
RS
1038 ncopies * assumed_nunits,
1039 scalar_store, stmt_info, 0, vect_body);
1040 }
f2e2a985 1041 else
57c454d2 1042 vect_get_store_cost (stmt_info, ncopies, &inside_cost, cost_vec);
ebfd146a 1043
2de001ee
RS
1044 if (memory_access_type == VMAT_ELEMENTWISE
1045 || memory_access_type == VMAT_STRIDED_SLP)
c5126ce8
RS
1046 {
1047 /* N scalar stores plus extracting the elements. */
1048 unsigned int assumed_nunits = vect_nunits_for_cost (vectype);
68435eb2 1049 inside_cost += record_stmt_cost (cost_vec,
c5126ce8
RS
1050 ncopies * assumed_nunits,
1051 vec_to_scalar, stmt_info, 0, vect_body);
1052 }
cee62fee 1053
73fbfcad 1054 if (dump_enabled_p ())
78c60e3d
SS
1055 dump_printf_loc (MSG_NOTE, vect_location,
1056 "vect_model_store_cost: inside_cost = %d, "
e645e942 1057 "prologue_cost = %d .\n", inside_cost, prologue_cost);
ebfd146a
IR
1058}
1059
1060
720f5239
IR
1061/* Calculate cost of DR's memory access. */
1062void
57c454d2 1063vect_get_store_cost (stmt_vec_info stmt_info, int ncopies,
c3e7ee41 1064 unsigned int *inside_cost,
92345349 1065 stmt_vector_for_cost *body_cost_vec)
720f5239 1066{
57c454d2 1067 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
720f5239
IR
1068 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1069
1070 switch (alignment_support_scheme)
1071 {
1072 case dr_aligned:
1073 {
92345349
BS
1074 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1075 vector_store, stmt_info, 0,
1076 vect_body);
720f5239 1077
73fbfcad 1078 if (dump_enabled_p ())
78c60e3d 1079 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 1080 "vect_model_store_cost: aligned.\n");
720f5239
IR
1081 break;
1082 }
1083
1084 case dr_unaligned_supported:
1085 {
720f5239 1086 /* Here, we assign an additional cost for the unaligned store. */
92345349 1087 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
c3e7ee41 1088 unaligned_store, stmt_info,
92345349 1089 DR_MISALIGNMENT (dr), vect_body);
73fbfcad 1090 if (dump_enabled_p ())
78c60e3d
SS
1091 dump_printf_loc (MSG_NOTE, vect_location,
1092 "vect_model_store_cost: unaligned supported by "
e645e942 1093 "hardware.\n");
720f5239
IR
1094 break;
1095 }
1096
38eec4c6
UW
1097 case dr_unaligned_unsupported:
1098 {
1099 *inside_cost = VECT_MAX_COST;
1100
73fbfcad 1101 if (dump_enabled_p ())
78c60e3d 1102 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 1103 "vect_model_store_cost: unsupported access.\n");
38eec4c6
UW
1104 break;
1105 }
1106
720f5239
IR
1107 default:
1108 gcc_unreachable ();
1109 }
1110}
1111
1112
ebfd146a
IR
1113/* Function vect_model_load_cost
1114
892a981f
RS
1115 Models cost for loads. In the case of grouped accesses, one access has
1116 the overhead of the grouped access attributed to it. Since unaligned
b8698a0f 1117 accesses are supported for loads, we also account for the costs of the
ebfd146a
IR
1118 access scheme chosen. */
1119
68435eb2
RB
1120static void
1121vect_model_load_cost (stmt_vec_info stmt_info, unsigned ncopies,
2de001ee 1122 vect_memory_access_type memory_access_type,
68435eb2 1123 slp_instance instance,
2de001ee 1124 slp_tree slp_node,
68435eb2 1125 stmt_vector_for_cost *cost_vec)
ebfd146a 1126{
892a981f 1127 gimple *first_stmt = STMT_VINFO_STMT (stmt_info);
92345349 1128 unsigned int inside_cost = 0, prologue_cost = 0;
892a981f 1129 bool grouped_access_p = STMT_VINFO_GROUPED_ACCESS (stmt_info);
ebfd146a 1130
68435eb2
RB
1131 gcc_assert (cost_vec);
1132
1133 /* ??? Somehow we need to fix this at the callers. */
1134 if (slp_node)
1135 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
1136
1137 if (slp_node && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
1138 {
1139 /* If the load is permuted then the alignment is determined by
1140 the first group element not by the first scalar stmt DR. */
2c53b149 1141 gimple *stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
68435eb2
RB
1142 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1143 /* Record the cost for the permutation. */
1144 unsigned n_perms;
1145 unsigned assumed_nunits
1146 = vect_nunits_for_cost (STMT_VINFO_VECTYPE (stmt_info));
1147 unsigned slp_vf = (ncopies * assumed_nunits) / instance->group_size;
1148 vect_transform_slp_perm_load (slp_node, vNULL, NULL,
1149 slp_vf, instance, true,
1150 &n_perms);
1151 inside_cost += record_stmt_cost (cost_vec, n_perms, vec_perm,
1152 stmt_info, 0, vect_body);
1153 /* And adjust the number of loads performed. This handles
1154 redundancies as well as loads that are later dead. */
2c53b149 1155 auto_sbitmap perm (DR_GROUP_SIZE (stmt_info));
68435eb2
RB
1156 bitmap_clear (perm);
1157 for (unsigned i = 0;
1158 i < SLP_TREE_LOAD_PERMUTATION (slp_node).length (); ++i)
1159 bitmap_set_bit (perm, SLP_TREE_LOAD_PERMUTATION (slp_node)[i]);
1160 ncopies = 0;
1161 bool load_seen = false;
2c53b149 1162 for (unsigned i = 0; i < DR_GROUP_SIZE (stmt_info); ++i)
68435eb2
RB
1163 {
1164 if (i % assumed_nunits == 0)
1165 {
1166 if (load_seen)
1167 ncopies++;
1168 load_seen = false;
1169 }
1170 if (bitmap_bit_p (perm, i))
1171 load_seen = true;
1172 }
1173 if (load_seen)
1174 ncopies++;
1175 gcc_assert (ncopies
2c53b149 1176 <= (DR_GROUP_SIZE (stmt_info) - DR_GROUP_GAP (stmt_info)
68435eb2
RB
1177 + assumed_nunits - 1) / assumed_nunits);
1178 }
1179
892a981f
RS
1180 /* Grouped loads read all elements in the group at once,
1181 so we want the DR for the first statement. */
1182 if (!slp_node && grouped_access_p)
57c454d2 1183 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
ebfd146a 1184
892a981f
RS
1185 /* True if we should include any once-per-group costs as well as
1186 the cost of the statement itself. For SLP we only get called
1187 once per group anyhow. */
1188 bool first_stmt_p = (first_stmt == STMT_VINFO_STMT (stmt_info));
1189
272c6793 1190 /* We assume that the cost of a single load-lanes instruction is
2c53b149 1191 equivalent to the cost of DR_GROUP_SIZE separate loads. If a grouped
272c6793 1192 access is instead being provided by a load-and-permute operation,
2de001ee
RS
1193 include the cost of the permutes. */
1194 if (first_stmt_p
1195 && memory_access_type == VMAT_CONTIGUOUS_PERMUTE)
ebfd146a 1196 {
2c23db6d
ES
1197 /* Uses an even and odd extract operations or shuffle operations
1198 for each needed permute. */
2c53b149 1199 int group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
2c23db6d 1200 int nstmts = ncopies * ceil_log2 (group_size) * group_size;
68435eb2
RB
1201 inside_cost += record_stmt_cost (cost_vec, nstmts, vec_perm,
1202 stmt_info, 0, vect_body);
ebfd146a 1203
73fbfcad 1204 if (dump_enabled_p ())
e645e942
TJ
1205 dump_printf_loc (MSG_NOTE, vect_location,
1206 "vect_model_load_cost: strided group_size = %d .\n",
78c60e3d 1207 group_size);
ebfd146a
IR
1208 }
1209
1210 /* The loads themselves. */
067bc855
RB
1211 if (memory_access_type == VMAT_ELEMENTWISE
1212 || memory_access_type == VMAT_GATHER_SCATTER)
a82960aa 1213 {
a21892ad
BS
1214 /* N scalar loads plus gathering them into a vector. */
1215 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
c5126ce8 1216 unsigned int assumed_nunits = vect_nunits_for_cost (vectype);
68435eb2 1217 inside_cost += record_stmt_cost (cost_vec,
c5126ce8 1218 ncopies * assumed_nunits,
92345349 1219 scalar_load, stmt_info, 0, vect_body);
a82960aa
RG
1220 }
1221 else
57c454d2 1222 vect_get_load_cost (stmt_info, ncopies, first_stmt_p,
92345349 1223 &inside_cost, &prologue_cost,
68435eb2 1224 cost_vec, cost_vec, true);
2de001ee
RS
1225 if (memory_access_type == VMAT_ELEMENTWISE
1226 || memory_access_type == VMAT_STRIDED_SLP)
68435eb2 1227 inside_cost += record_stmt_cost (cost_vec, ncopies, vec_construct,
892a981f 1228 stmt_info, 0, vect_body);
720f5239 1229
73fbfcad 1230 if (dump_enabled_p ())
78c60e3d
SS
1231 dump_printf_loc (MSG_NOTE, vect_location,
1232 "vect_model_load_cost: inside_cost = %d, "
e645e942 1233 "prologue_cost = %d .\n", inside_cost, prologue_cost);
720f5239
IR
1234}
1235
1236
1237/* Calculate cost of DR's memory access. */
1238void
57c454d2 1239vect_get_load_cost (stmt_vec_info stmt_info, int ncopies,
c3e7ee41 1240 bool add_realign_cost, unsigned int *inside_cost,
92345349
BS
1241 unsigned int *prologue_cost,
1242 stmt_vector_for_cost *prologue_cost_vec,
1243 stmt_vector_for_cost *body_cost_vec,
1244 bool record_prologue_costs)
720f5239 1245{
57c454d2 1246 data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
720f5239
IR
1247 int alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
1248
1249 switch (alignment_support_scheme)
ebfd146a
IR
1250 {
1251 case dr_aligned:
1252 {
92345349
BS
1253 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1254 stmt_info, 0, vect_body);
ebfd146a 1255
73fbfcad 1256 if (dump_enabled_p ())
78c60e3d 1257 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 1258 "vect_model_load_cost: aligned.\n");
ebfd146a
IR
1259
1260 break;
1261 }
1262 case dr_unaligned_supported:
1263 {
720f5239 1264 /* Here, we assign an additional cost for the unaligned load. */
92345349 1265 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
c3e7ee41 1266 unaligned_load, stmt_info,
92345349 1267 DR_MISALIGNMENT (dr), vect_body);
c3e7ee41 1268
73fbfcad 1269 if (dump_enabled_p ())
78c60e3d
SS
1270 dump_printf_loc (MSG_NOTE, vect_location,
1271 "vect_model_load_cost: unaligned supported by "
e645e942 1272 "hardware.\n");
ebfd146a
IR
1273
1274 break;
1275 }
1276 case dr_explicit_realign:
1277 {
92345349
BS
1278 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1279 vector_load, stmt_info, 0, vect_body);
1280 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1281 vec_perm, stmt_info, 0, vect_body);
ebfd146a
IR
1282
1283 /* FIXME: If the misalignment remains fixed across the iterations of
1284 the containing loop, the following cost should be added to the
92345349 1285 prologue costs. */
ebfd146a 1286 if (targetm.vectorize.builtin_mask_for_load)
92345349
BS
1287 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1288 stmt_info, 0, vect_body);
ebfd146a 1289
73fbfcad 1290 if (dump_enabled_p ())
e645e942
TJ
1291 dump_printf_loc (MSG_NOTE, vect_location,
1292 "vect_model_load_cost: explicit realign\n");
8bd37302 1293
ebfd146a
IR
1294 break;
1295 }
1296 case dr_explicit_realign_optimized:
1297 {
73fbfcad 1298 if (dump_enabled_p ())
e645e942 1299 dump_printf_loc (MSG_NOTE, vect_location,
78c60e3d 1300 "vect_model_load_cost: unaligned software "
e645e942 1301 "pipelined.\n");
ebfd146a
IR
1302
1303 /* Unaligned software pipeline has a load of an address, an initial
ff802fa1 1304 load, and possibly a mask operation to "prime" the loop. However,
0d0293ac 1305 if this is an access in a group of loads, which provide grouped
ebfd146a 1306 access, then the above cost should only be considered for one
ff802fa1 1307 access in the group. Inside the loop, there is a load op
ebfd146a
IR
1308 and a realignment op. */
1309
92345349 1310 if (add_realign_cost && record_prologue_costs)
ebfd146a 1311 {
92345349
BS
1312 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1313 vector_stmt, stmt_info,
1314 0, vect_prologue);
ebfd146a 1315 if (targetm.vectorize.builtin_mask_for_load)
92345349
BS
1316 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1317 vector_stmt, stmt_info,
1318 0, vect_prologue);
ebfd146a
IR
1319 }
1320
92345349
BS
1321 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1322 stmt_info, 0, vect_body);
1323 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1324 stmt_info, 0, vect_body);
8bd37302 1325
73fbfcad 1326 if (dump_enabled_p ())
78c60e3d 1327 dump_printf_loc (MSG_NOTE, vect_location,
e645e942
TJ
1328 "vect_model_load_cost: explicit realign optimized"
1329 "\n");
8bd37302 1330
ebfd146a
IR
1331 break;
1332 }
1333
38eec4c6
UW
1334 case dr_unaligned_unsupported:
1335 {
1336 *inside_cost = VECT_MAX_COST;
1337
73fbfcad 1338 if (dump_enabled_p ())
78c60e3d 1339 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 1340 "vect_model_load_cost: unsupported access.\n");
38eec4c6
UW
1341 break;
1342 }
1343
ebfd146a
IR
1344 default:
1345 gcc_unreachable ();
1346 }
ebfd146a
IR
1347}
1348
418b7df3
RG
1349/* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1350 the loop preheader for the vectorized stmt STMT. */
ebfd146a 1351
418b7df3 1352static void
355fe088 1353vect_init_vector_1 (gimple *stmt, gimple *new_stmt, gimple_stmt_iterator *gsi)
ebfd146a 1354{
ebfd146a 1355 if (gsi)
418b7df3 1356 vect_finish_stmt_generation (stmt, new_stmt, gsi);
ebfd146a
IR
1357 else
1358 {
418b7df3 1359 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
ebfd146a 1360 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
b8698a0f 1361
a70d6342
IR
1362 if (loop_vinfo)
1363 {
1364 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
418b7df3
RG
1365 basic_block new_bb;
1366 edge pe;
a70d6342
IR
1367
1368 if (nested_in_vect_loop_p (loop, stmt))
1369 loop = loop->inner;
b8698a0f 1370
a70d6342 1371 pe = loop_preheader_edge (loop);
418b7df3 1372 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
a70d6342
IR
1373 gcc_assert (!new_bb);
1374 }
1375 else
1376 {
1377 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_vinfo);
1378 basic_block bb;
1379 gimple_stmt_iterator gsi_bb_start;
1380
1381 gcc_assert (bb_vinfo);
1382 bb = BB_VINFO_BB (bb_vinfo);
12aaf609 1383 gsi_bb_start = gsi_after_labels (bb);
418b7df3 1384 gsi_insert_before (&gsi_bb_start, new_stmt, GSI_SAME_STMT);
a70d6342 1385 }
ebfd146a
IR
1386 }
1387
73fbfcad 1388 if (dump_enabled_p ())
ebfd146a 1389 {
78c60e3d
SS
1390 dump_printf_loc (MSG_NOTE, vect_location,
1391 "created new init_stmt: ");
1392 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
ebfd146a 1393 }
418b7df3
RG
1394}
1395
1396/* Function vect_init_vector.
ebfd146a 1397
5467ee52
RG
1398 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1399 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1400 vector type a vector with all elements equal to VAL is created first.
1401 Place the initialization at BSI if it is not NULL. Otherwise, place the
1402 initialization at the loop preheader.
418b7df3
RG
1403 Return the DEF of INIT_STMT.
1404 It will be used in the vectorization of STMT. */
1405
1406tree
355fe088 1407vect_init_vector (gimple *stmt, tree val, tree type, gimple_stmt_iterator *gsi)
418b7df3 1408{
355fe088 1409 gimple *init_stmt;
418b7df3
RG
1410 tree new_temp;
1411
e412ece4
RB
1412 /* We abuse this function to push sth to a SSA name with initial 'val'. */
1413 if (! useless_type_conversion_p (type, TREE_TYPE (val)))
418b7df3 1414 {
e412ece4
RB
1415 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
1416 if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
418b7df3 1417 {
5a308cf1
IE
1418 /* Scalar boolean value should be transformed into
1419 all zeros or all ones value before building a vector. */
1420 if (VECTOR_BOOLEAN_TYPE_P (type))
1421 {
b3d51f23
IE
1422 tree true_val = build_all_ones_cst (TREE_TYPE (type));
1423 tree false_val = build_zero_cst (TREE_TYPE (type));
5a308cf1
IE
1424
1425 if (CONSTANT_CLASS_P (val))
1426 val = integer_zerop (val) ? false_val : true_val;
1427 else
1428 {
1429 new_temp = make_ssa_name (TREE_TYPE (type));
1430 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1431 val, true_val, false_val);
1432 vect_init_vector_1 (stmt, init_stmt, gsi);
1433 val = new_temp;
1434 }
1435 }
1436 else if (CONSTANT_CLASS_P (val))
42fd8198 1437 val = fold_convert (TREE_TYPE (type), val);
418b7df3
RG
1438 else
1439 {
b731b390 1440 new_temp = make_ssa_name (TREE_TYPE (type));
e412ece4
RB
1441 if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1442 init_stmt = gimple_build_assign (new_temp,
1443 fold_build1 (VIEW_CONVERT_EXPR,
1444 TREE_TYPE (type),
1445 val));
1446 else
1447 init_stmt = gimple_build_assign (new_temp, NOP_EXPR, val);
418b7df3 1448 vect_init_vector_1 (stmt, init_stmt, gsi);
5467ee52 1449 val = new_temp;
418b7df3
RG
1450 }
1451 }
5467ee52 1452 val = build_vector_from_val (type, val);
418b7df3
RG
1453 }
1454
0e22bb5a
RB
1455 new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1456 init_stmt = gimple_build_assign (new_temp, val);
418b7df3 1457 vect_init_vector_1 (stmt, init_stmt, gsi);
0e22bb5a 1458 return new_temp;
ebfd146a
IR
1459}
1460
c83a894c 1461/* Function vect_get_vec_def_for_operand_1.
a70d6342 1462
c83a894c
AH
1463 For a defining stmt DEF_STMT of a scalar stmt, return a vector def with type
1464 DT that will be used in the vectorized stmt. */
ebfd146a
IR
1465
1466tree
c83a894c 1467vect_get_vec_def_for_operand_1 (gimple *def_stmt, enum vect_def_type dt)
ebfd146a
IR
1468{
1469 tree vec_oprnd;
355fe088 1470 gimple *vec_stmt;
ebfd146a 1471 stmt_vec_info def_stmt_info = NULL;
ebfd146a
IR
1472
1473 switch (dt)
1474 {
81c40241 1475 /* operand is a constant or a loop invariant. */
ebfd146a 1476 case vect_constant_def:
81c40241 1477 case vect_external_def:
c83a894c
AH
1478 /* Code should use vect_get_vec_def_for_operand. */
1479 gcc_unreachable ();
ebfd146a 1480
81c40241 1481 /* operand is defined inside the loop. */
8644a673 1482 case vect_internal_def:
ebfd146a 1483 {
ebfd146a
IR
1484 /* Get the def from the vectorized stmt. */
1485 def_stmt_info = vinfo_for_stmt (def_stmt);
83197f37 1486
ebfd146a 1487 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
83197f37
IR
1488 /* Get vectorized pattern statement. */
1489 if (!vec_stmt
1490 && STMT_VINFO_IN_PATTERN_P (def_stmt_info)
1491 && !STMT_VINFO_RELEVANT (def_stmt_info))
1492 vec_stmt = STMT_VINFO_VEC_STMT (vinfo_for_stmt (
1493 STMT_VINFO_RELATED_STMT (def_stmt_info)));
ebfd146a
IR
1494 gcc_assert (vec_stmt);
1495 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1496 vec_oprnd = PHI_RESULT (vec_stmt);
1497 else if (is_gimple_call (vec_stmt))
1498 vec_oprnd = gimple_call_lhs (vec_stmt);
1499 else
1500 vec_oprnd = gimple_assign_lhs (vec_stmt);
1501 return vec_oprnd;
1502 }
1503
c78e3652 1504 /* operand is defined by a loop header phi. */
ebfd146a 1505 case vect_reduction_def:
06066f92 1506 case vect_double_reduction_def:
7c5222ff 1507 case vect_nested_cycle:
ebfd146a
IR
1508 case vect_induction_def:
1509 {
1510 gcc_assert (gimple_code (def_stmt) == GIMPLE_PHI);
1511
1512 /* Get the def from the vectorized stmt. */
1513 def_stmt_info = vinfo_for_stmt (def_stmt);
1514 vec_stmt = STMT_VINFO_VEC_STMT (def_stmt_info);
6dbbece6
RG
1515 if (gimple_code (vec_stmt) == GIMPLE_PHI)
1516 vec_oprnd = PHI_RESULT (vec_stmt);
1517 else
1518 vec_oprnd = gimple_get_lhs (vec_stmt);
ebfd146a
IR
1519 return vec_oprnd;
1520 }
1521
1522 default:
1523 gcc_unreachable ();
1524 }
1525}
1526
1527
c83a894c
AH
1528/* Function vect_get_vec_def_for_operand.
1529
1530 OP is an operand in STMT. This function returns a (vector) def that will be
1531 used in the vectorized stmt for STMT.
1532
1533 In the case that OP is an SSA_NAME which is defined in the loop, then
1534 STMT_VINFO_VEC_STMT of the defining stmt holds the relevant def.
1535
1536 In case OP is an invariant or constant, a new stmt that creates a vector def
1537 needs to be introduced. VECTYPE may be used to specify a required type for
1538 vector invariant. */
1539
1540tree
1541vect_get_vec_def_for_operand (tree op, gimple *stmt, tree vectype)
1542{
1543 gimple *def_stmt;
1544 enum vect_def_type dt;
1545 bool is_simple_use;
1546 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
1547 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
1548
1549 if (dump_enabled_p ())
1550 {
1551 dump_printf_loc (MSG_NOTE, vect_location,
1552 "vect_get_vec_def_for_operand: ");
1553 dump_generic_expr (MSG_NOTE, TDF_SLIM, op);
1554 dump_printf (MSG_NOTE, "\n");
1555 }
1556
fef96d8e
RS
1557 stmt_vec_info def_stmt_info;
1558 is_simple_use = vect_is_simple_use (op, loop_vinfo, &dt,
1559 &def_stmt_info, &def_stmt);
c83a894c
AH
1560 gcc_assert (is_simple_use);
1561 if (def_stmt && dump_enabled_p ())
1562 {
1563 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = ");
1564 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
1565 }
1566
1567 if (dt == vect_constant_def || dt == vect_external_def)
1568 {
1569 tree stmt_vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1570 tree vector_type;
1571
1572 if (vectype)
1573 vector_type = vectype;
2568d8a1 1574 else if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
c83a894c
AH
1575 && VECTOR_BOOLEAN_TYPE_P (stmt_vectype))
1576 vector_type = build_same_sized_truth_vector_type (stmt_vectype);
1577 else
1578 vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
1579
1580 gcc_assert (vector_type);
1581 return vect_init_vector (stmt, op, vector_type, NULL);
1582 }
1583 else
fef96d8e 1584 return vect_get_vec_def_for_operand_1 (def_stmt_info, dt);
c83a894c
AH
1585}
1586
1587
ebfd146a
IR
1588/* Function vect_get_vec_def_for_stmt_copy
1589
ff802fa1 1590 Return a vector-def for an operand. This function is used when the
b8698a0f
L
1591 vectorized stmt to be created (by the caller to this function) is a "copy"
1592 created in case the vectorized result cannot fit in one vector, and several
ff802fa1 1593 copies of the vector-stmt are required. In this case the vector-def is
ebfd146a 1594 retrieved from the vector stmt recorded in the STMT_VINFO_RELATED_STMT field
b8698a0f 1595 of the stmt that defines VEC_OPRND.
ebfd146a
IR
1596 DT is the type of the vector def VEC_OPRND.
1597
1598 Context:
1599 In case the vectorization factor (VF) is bigger than the number
1600 of elements that can fit in a vectype (nunits), we have to generate
ff802fa1 1601 more than one vector stmt to vectorize the scalar stmt. This situation
b8698a0f 1602 arises when there are multiple data-types operated upon in the loop; the
ebfd146a
IR
1603 smallest data-type determines the VF, and as a result, when vectorizing
1604 stmts operating on wider types we need to create 'VF/nunits' "copies" of the
1605 vector stmt (each computing a vector of 'nunits' results, and together
b8698a0f 1606 computing 'VF' results in each iteration). This function is called when
ebfd146a
IR
1607 vectorizing such a stmt (e.g. vectorizing S2 in the illustration below, in
1608 which VF=16 and nunits=4, so the number of copies required is 4):
1609
1610 scalar stmt: vectorized into: STMT_VINFO_RELATED_STMT
b8698a0f 1611
ebfd146a
IR
1612 S1: x = load VS1.0: vx.0 = memref0 VS1.1
1613 VS1.1: vx.1 = memref1 VS1.2
1614 VS1.2: vx.2 = memref2 VS1.3
b8698a0f 1615 VS1.3: vx.3 = memref3
ebfd146a
IR
1616
1617 S2: z = x + ... VSnew.0: vz0 = vx.0 + ... VSnew.1
1618 VSnew.1: vz1 = vx.1 + ... VSnew.2
1619 VSnew.2: vz2 = vx.2 + ... VSnew.3
1620 VSnew.3: vz3 = vx.3 + ...
1621
1622 The vectorization of S1 is explained in vectorizable_load.
1623 The vectorization of S2:
b8698a0f
L
1624 To create the first vector-stmt out of the 4 copies - VSnew.0 -
1625 the function 'vect_get_vec_def_for_operand' is called to
ff802fa1 1626 get the relevant vector-def for each operand of S2. For operand x it
ebfd146a
IR
1627 returns the vector-def 'vx.0'.
1628
b8698a0f
L
1629 To create the remaining copies of the vector-stmt (VSnew.j), this
1630 function is called to get the relevant vector-def for each operand. It is
1631 obtained from the respective VS1.j stmt, which is recorded in the
ebfd146a
IR
1632 STMT_VINFO_RELATED_STMT field of the stmt that defines VEC_OPRND.
1633
b8698a0f
L
1634 For example, to obtain the vector-def 'vx.1' in order to create the
1635 vector stmt 'VSnew.1', this function is called with VEC_OPRND='vx.0'.
1636 Given 'vx0' we obtain the stmt that defines it ('VS1.0'); from the
ebfd146a
IR
1637 STMT_VINFO_RELATED_STMT field of 'VS1.0' we obtain the next copy - 'VS1.1',
1638 and return its def ('vx.1').
1639 Overall, to create the above sequence this function will be called 3 times:
1640 vx.1 = vect_get_vec_def_for_stmt_copy (dt, vx.0);
1641 vx.2 = vect_get_vec_def_for_stmt_copy (dt, vx.1);
1642 vx.3 = vect_get_vec_def_for_stmt_copy (dt, vx.2); */
1643
1644tree
1645vect_get_vec_def_for_stmt_copy (enum vect_def_type dt, tree vec_oprnd)
1646{
355fe088 1647 gimple *vec_stmt_for_operand;
ebfd146a
IR
1648 stmt_vec_info def_stmt_info;
1649
1650 /* Do nothing; can reuse same def. */
8644a673 1651 if (dt == vect_external_def || dt == vect_constant_def )
ebfd146a
IR
1652 return vec_oprnd;
1653
1654 vec_stmt_for_operand = SSA_NAME_DEF_STMT (vec_oprnd);
1655 def_stmt_info = vinfo_for_stmt (vec_stmt_for_operand);
1656 gcc_assert (def_stmt_info);
1657 vec_stmt_for_operand = STMT_VINFO_RELATED_STMT (def_stmt_info);
1658 gcc_assert (vec_stmt_for_operand);
ebfd146a
IR
1659 if (gimple_code (vec_stmt_for_operand) == GIMPLE_PHI)
1660 vec_oprnd = PHI_RESULT (vec_stmt_for_operand);
1661 else
1662 vec_oprnd = gimple_get_lhs (vec_stmt_for_operand);
1663 return vec_oprnd;
1664}
1665
1666
1667/* Get vectorized definitions for the operands to create a copy of an original
ff802fa1 1668 stmt. See vect_get_vec_def_for_stmt_copy () for details. */
ebfd146a 1669
c78e3652 1670void
b8698a0f 1671vect_get_vec_defs_for_stmt_copy (enum vect_def_type *dt,
9771b263
DN
1672 vec<tree> *vec_oprnds0,
1673 vec<tree> *vec_oprnds1)
ebfd146a 1674{
9771b263 1675 tree vec_oprnd = vec_oprnds0->pop ();
ebfd146a
IR
1676
1677 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd);
9771b263 1678 vec_oprnds0->quick_push (vec_oprnd);
ebfd146a 1679
9771b263 1680 if (vec_oprnds1 && vec_oprnds1->length ())
ebfd146a 1681 {
9771b263 1682 vec_oprnd = vec_oprnds1->pop ();
ebfd146a 1683 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt[1], vec_oprnd);
9771b263 1684 vec_oprnds1->quick_push (vec_oprnd);
ebfd146a
IR
1685 }
1686}
1687
1688
c78e3652 1689/* Get vectorized definitions for OP0 and OP1. */
ebfd146a 1690
c78e3652 1691void
355fe088 1692vect_get_vec_defs (tree op0, tree op1, gimple *stmt,
9771b263
DN
1693 vec<tree> *vec_oprnds0,
1694 vec<tree> *vec_oprnds1,
306b0c92 1695 slp_tree slp_node)
ebfd146a
IR
1696{
1697 if (slp_node)
d092494c
IR
1698 {
1699 int nops = (op1 == NULL_TREE) ? 1 : 2;
ef062b13
TS
1700 auto_vec<tree> ops (nops);
1701 auto_vec<vec<tree> > vec_defs (nops);
d092494c 1702
9771b263 1703 ops.quick_push (op0);
d092494c 1704 if (op1)
9771b263 1705 ops.quick_push (op1);
d092494c 1706
306b0c92 1707 vect_get_slp_defs (ops, slp_node, &vec_defs);
d092494c 1708
37b5ec8f 1709 *vec_oprnds0 = vec_defs[0];
d092494c 1710 if (op1)
37b5ec8f 1711 *vec_oprnds1 = vec_defs[1];
d092494c 1712 }
ebfd146a
IR
1713 else
1714 {
1715 tree vec_oprnd;
1716
9771b263 1717 vec_oprnds0->create (1);
81c40241 1718 vec_oprnd = vect_get_vec_def_for_operand (op0, stmt);
9771b263 1719 vec_oprnds0->quick_push (vec_oprnd);
ebfd146a
IR
1720
1721 if (op1)
1722 {
9771b263 1723 vec_oprnds1->create (1);
81c40241 1724 vec_oprnd = vect_get_vec_def_for_operand (op1, stmt);
9771b263 1725 vec_oprnds1->quick_push (vec_oprnd);
ebfd146a
IR
1726 }
1727 }
1728}
1729
bb6c2b68
RS
1730/* Helper function called by vect_finish_replace_stmt and
1731 vect_finish_stmt_generation. Set the location of the new
1732 statement and create a stmt_vec_info for it. */
1733
1734static void
1735vect_finish_stmt_generation_1 (gimple *stmt, gimple *vec_stmt)
1736{
1737 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1738 vec_info *vinfo = stmt_info->vinfo;
1739
4fbeb363 1740 vinfo->add_stmt (vec_stmt);
bb6c2b68
RS
1741
1742 if (dump_enabled_p ())
1743 {
1744 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: ");
1745 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vec_stmt, 0);
1746 }
1747
1748 gimple_set_location (vec_stmt, gimple_location (stmt));
1749
1750 /* While EH edges will generally prevent vectorization, stmt might
1751 e.g. be in a must-not-throw region. Ensure newly created stmts
1752 that could throw are part of the same region. */
1753 int lp_nr = lookup_stmt_eh_lp (stmt);
1754 if (lp_nr != 0 && stmt_could_throw_p (vec_stmt))
1755 add_stmt_to_eh_lp (vec_stmt, lp_nr);
1756}
1757
1758/* Replace the scalar statement STMT with a new vector statement VEC_STMT,
1759 which sets the same scalar result as STMT did. */
1760
1761void
1762vect_finish_replace_stmt (gimple *stmt, gimple *vec_stmt)
1763{
1764 gcc_assert (gimple_get_lhs (stmt) == gimple_get_lhs (vec_stmt));
1765
1766 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1767 gsi_replace (&gsi, vec_stmt, false);
1768
1769 vect_finish_stmt_generation_1 (stmt, vec_stmt);
1770}
ebfd146a
IR
1771
1772/* Function vect_finish_stmt_generation.
1773
1774 Insert a new stmt. */
1775
1776void
355fe088 1777vect_finish_stmt_generation (gimple *stmt, gimple *vec_stmt,
ebfd146a
IR
1778 gimple_stmt_iterator *gsi)
1779{
ebfd146a
IR
1780 gcc_assert (gimple_code (stmt) != GIMPLE_LABEL);
1781
54e8e2c3
RG
1782 if (!gsi_end_p (*gsi)
1783 && gimple_has_mem_ops (vec_stmt))
1784 {
355fe088 1785 gimple *at_stmt = gsi_stmt (*gsi);
54e8e2c3
RG
1786 tree vuse = gimple_vuse (at_stmt);
1787 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1788 {
1789 tree vdef = gimple_vdef (at_stmt);
1790 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1791 /* If we have an SSA vuse and insert a store, update virtual
1792 SSA form to avoid triggering the renamer. Do so only
1793 if we can easily see all uses - which is what almost always
1794 happens with the way vectorized stmts are inserted. */
1795 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1796 && ((is_gimple_assign (vec_stmt)
1797 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1798 || (is_gimple_call (vec_stmt)
1799 && !(gimple_call_flags (vec_stmt)
1800 & (ECF_CONST|ECF_PURE|ECF_NOVOPS)))))
1801 {
1802 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1803 gimple_set_vdef (vec_stmt, new_vdef);
1804 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1805 }
1806 }
1807 }
ebfd146a 1808 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
bb6c2b68 1809 vect_finish_stmt_generation_1 (stmt, vec_stmt);
ebfd146a
IR
1810}
1811
70439f0d
RS
1812/* We want to vectorize a call to combined function CFN with function
1813 decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1814 as the types of all inputs. Check whether this is possible using
1815 an internal function, returning its code if so or IFN_LAST if not. */
ebfd146a 1816
70439f0d
RS
1817static internal_fn
1818vectorizable_internal_function (combined_fn cfn, tree fndecl,
1819 tree vectype_out, tree vectype_in)
ebfd146a 1820{
70439f0d
RS
1821 internal_fn ifn;
1822 if (internal_fn_p (cfn))
1823 ifn = as_internal_fn (cfn);
1824 else
1825 ifn = associated_internal_fn (fndecl);
1826 if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1827 {
1828 const direct_internal_fn_info &info = direct_internal_fn (ifn);
1829 if (info.vectorizable)
1830 {
1831 tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1832 tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
d95ab70a
RS
1833 if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1834 OPTIMIZE_FOR_SPEED))
70439f0d
RS
1835 return ifn;
1836 }
1837 }
1838 return IFN_LAST;
ebfd146a
IR
1839}
1840
5ce9450f 1841
355fe088 1842static tree permute_vec_elements (tree, tree, tree, gimple *,
5ce9450f
JJ
1843 gimple_stmt_iterator *);
1844
7cfb4d93
RS
1845/* Check whether a load or store statement in the loop described by
1846 LOOP_VINFO is possible in a fully-masked loop. This is testing
1847 whether the vectorizer pass has the appropriate support, as well as
1848 whether the target does.
1849
1850 VLS_TYPE says whether the statement is a load or store and VECTYPE
1851 is the type of the vector being loaded or stored. MEMORY_ACCESS_TYPE
1852 says how the load or store is going to be implemented and GROUP_SIZE
1853 is the number of load or store statements in the containing group.
bfaa08b7
RS
1854 If the access is a gather load or scatter store, GS_INFO describes
1855 its arguments.
7cfb4d93
RS
1856
1857 Clear LOOP_VINFO_CAN_FULLY_MASK_P if a fully-masked loop is not
1858 supported, otherwise record the required mask types. */
1859
1860static void
1861check_load_store_masking (loop_vec_info loop_vinfo, tree vectype,
1862 vec_load_store_type vls_type, int group_size,
bfaa08b7
RS
1863 vect_memory_access_type memory_access_type,
1864 gather_scatter_info *gs_info)
7cfb4d93
RS
1865{
1866 /* Invariant loads need no special support. */
1867 if (memory_access_type == VMAT_INVARIANT)
1868 return;
1869
1870 vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1871 machine_mode vecmode = TYPE_MODE (vectype);
1872 bool is_load = (vls_type == VLS_LOAD);
1873 if (memory_access_type == VMAT_LOAD_STORE_LANES)
1874 {
1875 if (is_load
1876 ? !vect_load_lanes_supported (vectype, group_size, true)
1877 : !vect_store_lanes_supported (vectype, group_size, true))
1878 {
1879 if (dump_enabled_p ())
1880 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1881 "can't use a fully-masked loop because the"
1882 " target doesn't have an appropriate masked"
1883 " load/store-lanes instruction.\n");
1884 LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo) = false;
1885 return;
1886 }
1887 unsigned int ncopies = vect_get_num_copies (loop_vinfo, vectype);
1888 vect_record_loop_mask (loop_vinfo, masks, ncopies, vectype);
1889 return;
1890 }
1891
bfaa08b7
RS
1892 if (memory_access_type == VMAT_GATHER_SCATTER)
1893 {
f307441a
RS
1894 internal_fn ifn = (is_load
1895 ? IFN_MASK_GATHER_LOAD
1896 : IFN_MASK_SCATTER_STORE);
bfaa08b7 1897 tree offset_type = TREE_TYPE (gs_info->offset);
f307441a 1898 if (!internal_gather_scatter_fn_supported_p (ifn, vectype,
bfaa08b7
RS
1899 gs_info->memory_type,
1900 TYPE_SIGN (offset_type),
1901 gs_info->scale))
1902 {
1903 if (dump_enabled_p ())
1904 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1905 "can't use a fully-masked loop because the"
1906 " target doesn't have an appropriate masked"
f307441a 1907 " gather load or scatter store instruction.\n");
bfaa08b7
RS
1908 LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo) = false;
1909 return;
1910 }
1911 unsigned int ncopies = vect_get_num_copies (loop_vinfo, vectype);
1912 vect_record_loop_mask (loop_vinfo, masks, ncopies, vectype);
1913 return;
1914 }
1915
7cfb4d93
RS
1916 if (memory_access_type != VMAT_CONTIGUOUS
1917 && memory_access_type != VMAT_CONTIGUOUS_PERMUTE)
1918 {
1919 /* Element X of the data must come from iteration i * VF + X of the
1920 scalar loop. We need more work to support other mappings. */
1921 if (dump_enabled_p ())
1922 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1923 "can't use a fully-masked loop because an access"
1924 " isn't contiguous.\n");
1925 LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo) = false;
1926 return;
1927 }
1928
1929 machine_mode mask_mode;
1930 if (!(targetm.vectorize.get_mask_mode
1931 (GET_MODE_NUNITS (vecmode),
1932 GET_MODE_SIZE (vecmode)).exists (&mask_mode))
1933 || !can_vec_mask_load_store_p (vecmode, mask_mode, is_load))
1934 {
1935 if (dump_enabled_p ())
1936 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1937 "can't use a fully-masked loop because the target"
1938 " doesn't have the appropriate masked load or"
1939 " store.\n");
1940 LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo) = false;
1941 return;
1942 }
1943 /* We might load more scalars than we need for permuting SLP loads.
1944 We checked in get_group_load_store_type that the extra elements
1945 don't leak into a new vector. */
1946 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1947 poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1948 unsigned int nvectors;
1949 if (can_div_away_from_zero_p (group_size * vf, nunits, &nvectors))
1950 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype);
1951 else
1952 gcc_unreachable ();
1953}
1954
1955/* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1956 form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1957 that needs to be applied to all loads and stores in a vectorized loop.
1958 Return VEC_MASK if LOOP_MASK is null, otherwise return VEC_MASK & LOOP_MASK.
1959
1960 MASK_TYPE is the type of both masks. If new statements are needed,
1961 insert them before GSI. */
1962
1963static tree
1964prepare_load_store_mask (tree mask_type, tree loop_mask, tree vec_mask,
1965 gimple_stmt_iterator *gsi)
1966{
1967 gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1968 if (!loop_mask)
1969 return vec_mask;
1970
1971 gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1972 tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1973 gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1974 vec_mask, loop_mask);
1975 gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1976 return and_res;
1977}
1978
429ef523
RS
1979/* Determine whether we can use a gather load or scatter store to vectorize
1980 strided load or store STMT by truncating the current offset to a smaller
1981 width. We need to be able to construct an offset vector:
1982
1983 { 0, X, X*2, X*3, ... }
1984
1985 without loss of precision, where X is STMT's DR_STEP.
1986
1987 Return true if this is possible, describing the gather load or scatter
1988 store in GS_INFO. MASKED_P is true if the load or store is conditional. */
1989
1990static bool
1991vect_truncate_gather_scatter_offset (gimple *stmt, loop_vec_info loop_vinfo,
1992 bool masked_p,
1993 gather_scatter_info *gs_info)
1994{
1995 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1996 data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
1997 tree step = DR_STEP (dr);
1998 if (TREE_CODE (step) != INTEGER_CST)
1999 {
2000 /* ??? Perhaps we could use range information here? */
2001 if (dump_enabled_p ())
2002 dump_printf_loc (MSG_NOTE, vect_location,
2003 "cannot truncate variable step.\n");
2004 return false;
2005 }
2006
2007 /* Get the number of bits in an element. */
2008 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2009 scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
2010 unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
2011
2012 /* Set COUNT to the upper limit on the number of elements - 1.
2013 Start with the maximum vectorization factor. */
2014 unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
2015
2016 /* Try lowering COUNT to the number of scalar latch iterations. */
2017 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2018 widest_int max_iters;
2019 if (max_loop_iterations (loop, &max_iters)
2020 && max_iters < count)
2021 count = max_iters.to_shwi ();
2022
2023 /* Try scales of 1 and the element size. */
2024 int scales[] = { 1, vect_get_scalar_dr_size (dr) };
4a669ac3 2025 wi::overflow_type overflow = wi::OVF_NONE;
429ef523
RS
2026 for (int i = 0; i < 2; ++i)
2027 {
2028 int scale = scales[i];
2029 widest_int factor;
2030 if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
2031 continue;
2032
2033 /* See whether we can calculate (COUNT - 1) * STEP / SCALE
2034 in OFFSET_BITS bits. */
4a669ac3
AH
2035 widest_int range = wi::mul (count, factor, SIGNED, &overflow);
2036 if (overflow)
429ef523
RS
2037 continue;
2038 signop sign = range >= 0 ? UNSIGNED : SIGNED;
2039 if (wi::min_precision (range, sign) > element_bits)
2040 {
4a669ac3 2041 overflow = wi::OVF_UNKNOWN;
429ef523
RS
2042 continue;
2043 }
2044
2045 /* See whether the target supports the operation. */
2046 tree memory_type = TREE_TYPE (DR_REF (dr));
2047 if (!vect_gather_scatter_fn_p (DR_IS_READ (dr), masked_p, vectype,
2048 memory_type, element_bits, sign, scale,
2049 &gs_info->ifn, &gs_info->element_type))
2050 continue;
2051
2052 tree offset_type = build_nonstandard_integer_type (element_bits,
2053 sign == UNSIGNED);
2054
2055 gs_info->decl = NULL_TREE;
2056 /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
2057 but we don't need to store that here. */
2058 gs_info->base = NULL_TREE;
2059 gs_info->offset = fold_convert (offset_type, step);
929b4411 2060 gs_info->offset_dt = vect_constant_def;
429ef523
RS
2061 gs_info->offset_vectype = NULL_TREE;
2062 gs_info->scale = scale;
2063 gs_info->memory_type = memory_type;
2064 return true;
2065 }
2066
4a669ac3 2067 if (overflow && dump_enabled_p ())
429ef523
RS
2068 dump_printf_loc (MSG_NOTE, vect_location,
2069 "truncating gather/scatter offset to %d bits"
2070 " might change its value.\n", element_bits);
2071
2072 return false;
2073}
2074
ab2fc782
RS
2075/* Return true if we can use gather/scatter internal functions to
2076 vectorize STMT, which is a grouped or strided load or store.
429ef523
RS
2077 MASKED_P is true if load or store is conditional. When returning
2078 true, fill in GS_INFO with the information required to perform the
2079 operation. */
ab2fc782
RS
2080
2081static bool
2082vect_use_strided_gather_scatters_p (gimple *stmt, loop_vec_info loop_vinfo,
429ef523 2083 bool masked_p,
ab2fc782
RS
2084 gather_scatter_info *gs_info)
2085{
2086 if (!vect_check_gather_scatter (stmt, loop_vinfo, gs_info)
2087 || gs_info->decl)
429ef523
RS
2088 return vect_truncate_gather_scatter_offset (stmt, loop_vinfo,
2089 masked_p, gs_info);
ab2fc782
RS
2090
2091 scalar_mode element_mode = SCALAR_TYPE_MODE (gs_info->element_type);
2092 unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
2093 tree offset_type = TREE_TYPE (gs_info->offset);
2094 unsigned int offset_bits = TYPE_PRECISION (offset_type);
2095
2096 /* Enforced by vect_check_gather_scatter. */
2097 gcc_assert (element_bits >= offset_bits);
2098
2099 /* If the elements are wider than the offset, convert the offset to the
2100 same width, without changing its sign. */
2101 if (element_bits > offset_bits)
2102 {
2103 bool unsigned_p = TYPE_UNSIGNED (offset_type);
2104 offset_type = build_nonstandard_integer_type (element_bits, unsigned_p);
2105 gs_info->offset = fold_convert (offset_type, gs_info->offset);
2106 }
2107
2108 if (dump_enabled_p ())
2109 dump_printf_loc (MSG_NOTE, vect_location,
2110 "using gather/scatter for strided/grouped access,"
2111 " scale = %d\n", gs_info->scale);
2112
2113 return true;
2114}
2115
62da9e14
RS
2116/* STMT is a non-strided load or store, meaning that it accesses
2117 elements with a known constant step. Return -1 if that step
2118 is negative, 0 if it is zero, and 1 if it is greater than zero. */
2119
2120static int
2121compare_step_with_zero (gimple *stmt)
2122{
2123 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3f5e8a76
RS
2124 data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2125 return tree_int_cst_compare (vect_dr_behavior (dr)->step,
2126 size_zero_node);
62da9e14
RS
2127}
2128
2129/* If the target supports a permute mask that reverses the elements in
2130 a vector of type VECTYPE, return that mask, otherwise return null. */
2131
2132static tree
2133perm_mask_for_reverse (tree vectype)
2134{
928686b1 2135 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
62da9e14 2136
d980067b
RS
2137 /* The encoding has a single stepped pattern. */
2138 vec_perm_builder sel (nunits, 1, 3);
928686b1 2139 for (int i = 0; i < 3; ++i)
908a1a16 2140 sel.quick_push (nunits - 1 - i);
62da9e14 2141
e3342de4
RS
2142 vec_perm_indices indices (sel, 1, nunits);
2143 if (!can_vec_perm_const_p (TYPE_MODE (vectype), indices))
62da9e14 2144 return NULL_TREE;
e3342de4 2145 return vect_gen_perm_mask_checked (vectype, indices);
62da9e14 2146}
5ce9450f 2147
c3a8f964
RS
2148/* STMT is either a masked or unconditional store. Return the value
2149 being stored. */
2150
f307441a 2151tree
c3a8f964
RS
2152vect_get_store_rhs (gimple *stmt)
2153{
2154 if (gassign *assign = dyn_cast <gassign *> (stmt))
2155 {
2156 gcc_assert (gimple_assign_single_p (assign));
2157 return gimple_assign_rhs1 (assign);
2158 }
2159 if (gcall *call = dyn_cast <gcall *> (stmt))
2160 {
2161 internal_fn ifn = gimple_call_internal_fn (call);
f307441a
RS
2162 int index = internal_fn_stored_value_index (ifn);
2163 gcc_assert (index >= 0);
2164 return gimple_call_arg (stmt, index);
c3a8f964
RS
2165 }
2166 gcc_unreachable ();
2167}
2168
2de001ee
RS
2169/* A subroutine of get_load_store_type, with a subset of the same
2170 arguments. Handle the case where STMT is part of a grouped load
2171 or store.
2172
2173 For stores, the statements in the group are all consecutive
2174 and there is no gap at the end. For loads, the statements in the
2175 group might not be consecutive; there can be gaps between statements
2176 as well as at the end. */
2177
2178static bool
2179get_group_load_store_type (gimple *stmt, tree vectype, bool slp,
7e11fc7f 2180 bool masked_p, vec_load_store_type vls_type,
429ef523
RS
2181 vect_memory_access_type *memory_access_type,
2182 gather_scatter_info *gs_info)
2de001ee
RS
2183{
2184 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2185 vec_info *vinfo = stmt_info->vinfo;
2186 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2187 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2c53b149 2188 gimple *first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
f702e7d4 2189 data_reference *first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
2c53b149 2190 unsigned int group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
2de001ee 2191 bool single_element_p = (stmt == first_stmt
2c53b149
RB
2192 && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2193 unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (vinfo_for_stmt (first_stmt));
928686b1 2194 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2de001ee
RS
2195
2196 /* True if the vectorized statements would access beyond the last
2197 statement in the group. */
2198 bool overrun_p = false;
2199
2200 /* True if we can cope with such overrun by peeling for gaps, so that
2201 there is at least one final scalar iteration after the vector loop. */
7e11fc7f
RS
2202 bool can_overrun_p = (!masked_p
2203 && vls_type == VLS_LOAD
2204 && loop_vinfo
2205 && !loop->inner);
2de001ee
RS
2206
2207 /* There can only be a gap at the end of the group if the stride is
2208 known at compile time. */
2209 gcc_assert (!STMT_VINFO_STRIDED_P (stmt_info) || gap == 0);
2210
2211 /* Stores can't yet have gaps. */
2212 gcc_assert (slp || vls_type == VLS_LOAD || gap == 0);
2213
2214 if (slp)
2215 {
2216 if (STMT_VINFO_STRIDED_P (stmt_info))
2217 {
2c53b149 2218 /* Try to use consecutive accesses of DR_GROUP_SIZE elements,
2de001ee
RS
2219 separated by the stride, until we have a complete vector.
2220 Fall back to scalar accesses if that isn't possible. */
928686b1 2221 if (multiple_p (nunits, group_size))
2de001ee
RS
2222 *memory_access_type = VMAT_STRIDED_SLP;
2223 else
2224 *memory_access_type = VMAT_ELEMENTWISE;
2225 }
2226 else
2227 {
2228 overrun_p = loop_vinfo && gap != 0;
2229 if (overrun_p && vls_type != VLS_LOAD)
2230 {
2231 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2232 "Grouped store with gaps requires"
2233 " non-consecutive accesses\n");
2234 return false;
2235 }
f702e7d4
RS
2236 /* An overrun is fine if the trailing elements are smaller
2237 than the alignment boundary B. Every vector access will
2238 be a multiple of B and so we are guaranteed to access a
2239 non-gap element in the same B-sized block. */
f9ef2c76 2240 if (overrun_p
f702e7d4
RS
2241 && gap < (vect_known_alignment_in_bytes (first_dr)
2242 / vect_get_scalar_dr_size (first_dr)))
f9ef2c76 2243 overrun_p = false;
2de001ee
RS
2244 if (overrun_p && !can_overrun_p)
2245 {
2246 if (dump_enabled_p ())
2247 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2248 "Peeling for outer loop is not supported\n");
2249 return false;
2250 }
2251 *memory_access_type = VMAT_CONTIGUOUS;
2252 }
2253 }
2254 else
2255 {
2256 /* We can always handle this case using elementwise accesses,
2257 but see if something more efficient is available. */
2258 *memory_access_type = VMAT_ELEMENTWISE;
2259
2260 /* If there is a gap at the end of the group then these optimizations
2261 would access excess elements in the last iteration. */
2262 bool would_overrun_p = (gap != 0);
f702e7d4
RS
2263 /* An overrun is fine if the trailing elements are smaller than the
2264 alignment boundary B. Every vector access will be a multiple of B
2265 and so we are guaranteed to access a non-gap element in the
2266 same B-sized block. */
f9ef2c76 2267 if (would_overrun_p
7e11fc7f 2268 && !masked_p
f702e7d4
RS
2269 && gap < (vect_known_alignment_in_bytes (first_dr)
2270 / vect_get_scalar_dr_size (first_dr)))
f9ef2c76 2271 would_overrun_p = false;
f702e7d4 2272
2de001ee 2273 if (!STMT_VINFO_STRIDED_P (stmt_info)
62da9e14
RS
2274 && (can_overrun_p || !would_overrun_p)
2275 && compare_step_with_zero (stmt) > 0)
2de001ee 2276 {
6737facb
RS
2277 /* First cope with the degenerate case of a single-element
2278 vector. */
2279 if (known_eq (TYPE_VECTOR_SUBPARTS (vectype), 1U))
2280 *memory_access_type = VMAT_CONTIGUOUS;
2281
2282 /* Otherwise try using LOAD/STORE_LANES. */
2283 if (*memory_access_type == VMAT_ELEMENTWISE
2284 && (vls_type == VLS_LOAD
7e11fc7f
RS
2285 ? vect_load_lanes_supported (vectype, group_size, masked_p)
2286 : vect_store_lanes_supported (vectype, group_size,
2287 masked_p)))
2de001ee
RS
2288 {
2289 *memory_access_type = VMAT_LOAD_STORE_LANES;
2290 overrun_p = would_overrun_p;
2291 }
2292
2293 /* If that fails, try using permuting loads. */
2294 if (*memory_access_type == VMAT_ELEMENTWISE
2295 && (vls_type == VLS_LOAD
2296 ? vect_grouped_load_supported (vectype, single_element_p,
2297 group_size)
2298 : vect_grouped_store_supported (vectype, group_size)))
2299 {
2300 *memory_access_type = VMAT_CONTIGUOUS_PERMUTE;
2301 overrun_p = would_overrun_p;
2302 }
2303 }
429ef523
RS
2304
2305 /* As a last resort, trying using a gather load or scatter store.
2306
2307 ??? Although the code can handle all group sizes correctly,
2308 it probably isn't a win to use separate strided accesses based
2309 on nearby locations. Or, even if it's a win over scalar code,
2310 it might not be a win over vectorizing at a lower VF, if that
2311 allows us to use contiguous accesses. */
2312 if (*memory_access_type == VMAT_ELEMENTWISE
2313 && single_element_p
2314 && loop_vinfo
2315 && vect_use_strided_gather_scatters_p (stmt, loop_vinfo,
2316 masked_p, gs_info))
2317 *memory_access_type = VMAT_GATHER_SCATTER;
2de001ee
RS
2318 }
2319
2320 if (vls_type != VLS_LOAD && first_stmt == stmt)
2321 {
2322 /* STMT is the leader of the group. Check the operands of all the
2323 stmts of the group. */
2c53b149 2324 gimple *next_stmt = DR_GROUP_NEXT_ELEMENT (stmt_info);
2de001ee
RS
2325 while (next_stmt)
2326 {
7e11fc7f 2327 tree op = vect_get_store_rhs (next_stmt);
2de001ee 2328 enum vect_def_type dt;
894dd753 2329 if (!vect_is_simple_use (op, vinfo, &dt))
2de001ee
RS
2330 {
2331 if (dump_enabled_p ())
2332 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2333 "use not simple.\n");
2334 return false;
2335 }
2c53b149 2336 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2de001ee
RS
2337 }
2338 }
2339
2340 if (overrun_p)
2341 {
2342 gcc_assert (can_overrun_p);
2343 if (dump_enabled_p ())
2344 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2345 "Data access with gaps requires scalar "
2346 "epilogue loop\n");
2347 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2348 }
2349
2350 return true;
2351}
2352
62da9e14
RS
2353/* A subroutine of get_load_store_type, with a subset of the same
2354 arguments. Handle the case where STMT is a load or store that
2355 accesses consecutive elements with a negative step. */
2356
2357static vect_memory_access_type
2358get_negative_load_store_type (gimple *stmt, tree vectype,
2359 vec_load_store_type vls_type,
2360 unsigned int ncopies)
2361{
2362 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2363 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2364 dr_alignment_support alignment_support_scheme;
2365
2366 if (ncopies > 1)
2367 {
2368 if (dump_enabled_p ())
2369 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2370 "multiple types with negative step.\n");
2371 return VMAT_ELEMENTWISE;
2372 }
2373
2374 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
2375 if (alignment_support_scheme != dr_aligned
2376 && alignment_support_scheme != dr_unaligned_supported)
2377 {
2378 if (dump_enabled_p ())
2379 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2380 "negative step but alignment required.\n");
2381 return VMAT_ELEMENTWISE;
2382 }
2383
2384 if (vls_type == VLS_STORE_INVARIANT)
2385 {
2386 if (dump_enabled_p ())
2387 dump_printf_loc (MSG_NOTE, vect_location,
2388 "negative step with invariant source;"
2389 " no permute needed.\n");
2390 return VMAT_CONTIGUOUS_DOWN;
2391 }
2392
2393 if (!perm_mask_for_reverse (vectype))
2394 {
2395 if (dump_enabled_p ())
2396 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2397 "negative step and reversing not supported.\n");
2398 return VMAT_ELEMENTWISE;
2399 }
2400
2401 return VMAT_CONTIGUOUS_REVERSE;
2402}
2403
2de001ee
RS
2404/* Analyze load or store statement STMT of type VLS_TYPE. Return true
2405 if there is a memory access type that the vectorized form can use,
2406 storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2407 or scatters, fill in GS_INFO accordingly.
2408
2409 SLP says whether we're performing SLP rather than loop vectorization.
7e11fc7f 2410 MASKED_P is true if the statement is conditional on a vectorized mask.
62da9e14
RS
2411 VECTYPE is the vector type that the vectorized statements will use.
2412 NCOPIES is the number of vector statements that will be needed. */
2de001ee
RS
2413
2414static bool
7e11fc7f 2415get_load_store_type (gimple *stmt, tree vectype, bool slp, bool masked_p,
62da9e14 2416 vec_load_store_type vls_type, unsigned int ncopies,
2de001ee
RS
2417 vect_memory_access_type *memory_access_type,
2418 gather_scatter_info *gs_info)
2419{
2420 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2421 vec_info *vinfo = stmt_info->vinfo;
2422 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4d694b27 2423 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2de001ee
RS
2424 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2425 {
2426 *memory_access_type = VMAT_GATHER_SCATTER;
2de001ee
RS
2427 if (!vect_check_gather_scatter (stmt, loop_vinfo, gs_info))
2428 gcc_unreachable ();
894dd753 2429 else if (!vect_is_simple_use (gs_info->offset, vinfo,
2de001ee
RS
2430 &gs_info->offset_dt,
2431 &gs_info->offset_vectype))
2432 {
2433 if (dump_enabled_p ())
2434 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2435 "%s index use not simple.\n",
2436 vls_type == VLS_LOAD ? "gather" : "scatter");
2437 return false;
2438 }
2439 }
2440 else if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2441 {
7e11fc7f 2442 if (!get_group_load_store_type (stmt, vectype, slp, masked_p, vls_type,
429ef523 2443 memory_access_type, gs_info))
2de001ee
RS
2444 return false;
2445 }
2446 else if (STMT_VINFO_STRIDED_P (stmt_info))
2447 {
2448 gcc_assert (!slp);
ab2fc782 2449 if (loop_vinfo
429ef523
RS
2450 && vect_use_strided_gather_scatters_p (stmt, loop_vinfo,
2451 masked_p, gs_info))
ab2fc782
RS
2452 *memory_access_type = VMAT_GATHER_SCATTER;
2453 else
2454 *memory_access_type = VMAT_ELEMENTWISE;
2de001ee
RS
2455 }
2456 else
62da9e14
RS
2457 {
2458 int cmp = compare_step_with_zero (stmt);
2459 if (cmp < 0)
2460 *memory_access_type = get_negative_load_store_type
2461 (stmt, vectype, vls_type, ncopies);
2462 else if (cmp == 0)
2463 {
2464 gcc_assert (vls_type == VLS_LOAD);
2465 *memory_access_type = VMAT_INVARIANT;
2466 }
2467 else
2468 *memory_access_type = VMAT_CONTIGUOUS;
2469 }
2de001ee 2470
4d694b27
RS
2471 if ((*memory_access_type == VMAT_ELEMENTWISE
2472 || *memory_access_type == VMAT_STRIDED_SLP)
2473 && !nunits.is_constant ())
2474 {
2475 if (dump_enabled_p ())
2476 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2477 "Not using elementwise accesses due to variable "
2478 "vectorization factor.\n");
2479 return false;
2480 }
2481
2de001ee
RS
2482 /* FIXME: At the moment the cost model seems to underestimate the
2483 cost of using elementwise accesses. This check preserves the
2484 traditional behavior until that can be fixed. */
2485 if (*memory_access_type == VMAT_ELEMENTWISE
4aa157e8 2486 && !STMT_VINFO_STRIDED_P (stmt_info)
2c53b149
RB
2487 && !(stmt == DR_GROUP_FIRST_ELEMENT (stmt_info)
2488 && !DR_GROUP_NEXT_ELEMENT (stmt_info)
2489 && !pow2p_hwi (DR_GROUP_SIZE (stmt_info))))
2de001ee
RS
2490 {
2491 if (dump_enabled_p ())
2492 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2493 "not falling back to elementwise accesses\n");
2494 return false;
2495 }
2496 return true;
2497}
2498
aaeefd88 2499/* Return true if boolean argument MASK is suitable for vectorizing
929b4411
RS
2500 conditional load or store STMT. When returning true, store the type
2501 of the definition in *MASK_DT_OUT and the type of the vectorized mask
2502 in *MASK_VECTYPE_OUT. */
aaeefd88
RS
2503
2504static bool
929b4411
RS
2505vect_check_load_store_mask (gimple *stmt, tree mask,
2506 vect_def_type *mask_dt_out,
2507 tree *mask_vectype_out)
aaeefd88
RS
2508{
2509 if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (mask)))
2510 {
2511 if (dump_enabled_p ())
2512 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2513 "mask argument is not a boolean.\n");
2514 return false;
2515 }
2516
2517 if (TREE_CODE (mask) != SSA_NAME)
2518 {
2519 if (dump_enabled_p ())
2520 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2521 "mask argument is not an SSA name.\n");
2522 return false;
2523 }
2524
2525 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
929b4411 2526 enum vect_def_type mask_dt;
aaeefd88 2527 tree mask_vectype;
894dd753 2528 if (!vect_is_simple_use (mask, stmt_info->vinfo, &mask_dt, &mask_vectype))
aaeefd88
RS
2529 {
2530 if (dump_enabled_p ())
2531 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2532 "mask use not simple.\n");
2533 return false;
2534 }
2535
2536 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2537 if (!mask_vectype)
2538 mask_vectype = get_mask_type_for_scalar_type (TREE_TYPE (vectype));
2539
2540 if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2541 {
2542 if (dump_enabled_p ())
2543 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2544 "could not find an appropriate vector mask type.\n");
2545 return false;
2546 }
2547
2548 if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2549 TYPE_VECTOR_SUBPARTS (vectype)))
2550 {
2551 if (dump_enabled_p ())
2552 {
2553 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2554 "vector mask type ");
2555 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, mask_vectype);
2556 dump_printf (MSG_MISSED_OPTIMIZATION,
2557 " does not match vector data type ");
2558 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, vectype);
2559 dump_printf (MSG_MISSED_OPTIMIZATION, ".\n");
2560 }
2561 return false;
2562 }
2563
929b4411 2564 *mask_dt_out = mask_dt;
aaeefd88
RS
2565 *mask_vectype_out = mask_vectype;
2566 return true;
2567}
2568
3133c3b6
RS
2569/* Return true if stored value RHS is suitable for vectorizing store
2570 statement STMT. When returning true, store the type of the
929b4411
RS
2571 definition in *RHS_DT_OUT, the type of the vectorized store value in
2572 *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
3133c3b6
RS
2573
2574static bool
929b4411
RS
2575vect_check_store_rhs (gimple *stmt, tree rhs, vect_def_type *rhs_dt_out,
2576 tree *rhs_vectype_out, vec_load_store_type *vls_type_out)
3133c3b6
RS
2577{
2578 /* In the case this is a store from a constant make sure
2579 native_encode_expr can handle it. */
2580 if (CONSTANT_CLASS_P (rhs) && native_encode_expr (rhs, NULL, 64) == 0)
2581 {
2582 if (dump_enabled_p ())
2583 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2584 "cannot encode constant as a byte sequence.\n");
2585 return false;
2586 }
2587
2588 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
929b4411 2589 enum vect_def_type rhs_dt;
3133c3b6 2590 tree rhs_vectype;
894dd753 2591 if (!vect_is_simple_use (rhs, stmt_info->vinfo, &rhs_dt, &rhs_vectype))
3133c3b6
RS
2592 {
2593 if (dump_enabled_p ())
2594 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2595 "use not simple.\n");
2596 return false;
2597 }
2598
2599 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2600 if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2601 {
2602 if (dump_enabled_p ())
2603 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2604 "incompatible vector types.\n");
2605 return false;
2606 }
2607
929b4411 2608 *rhs_dt_out = rhs_dt;
3133c3b6 2609 *rhs_vectype_out = rhs_vectype;
929b4411 2610 if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
3133c3b6
RS
2611 *vls_type_out = VLS_STORE_INVARIANT;
2612 else
2613 *vls_type_out = VLS_STORE;
2614 return true;
2615}
2616
bc9587eb
RS
2617/* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT.
2618 Note that we support masks with floating-point type, in which case the
2619 floats are interpreted as a bitmask. */
2620
2621static tree
2622vect_build_all_ones_mask (gimple *stmt, tree masktype)
2623{
2624 if (TREE_CODE (masktype) == INTEGER_TYPE)
2625 return build_int_cst (masktype, -1);
2626 else if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2627 {
2628 tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2629 mask = build_vector_from_val (masktype, mask);
2630 return vect_init_vector (stmt, mask, masktype, NULL);
2631 }
2632 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2633 {
2634 REAL_VALUE_TYPE r;
2635 long tmp[6];
2636 for (int j = 0; j < 6; ++j)
2637 tmp[j] = -1;
2638 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2639 tree mask = build_real (TREE_TYPE (masktype), r);
2640 mask = build_vector_from_val (masktype, mask);
2641 return vect_init_vector (stmt, mask, masktype, NULL);
2642 }
2643 gcc_unreachable ();
2644}
2645
2646/* Build an all-zero merge value of type VECTYPE while vectorizing
2647 STMT as a gather load. */
2648
2649static tree
2650vect_build_zero_merge_argument (gimple *stmt, tree vectype)
2651{
2652 tree merge;
2653 if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2654 merge = build_int_cst (TREE_TYPE (vectype), 0);
2655 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2656 {
2657 REAL_VALUE_TYPE r;
2658 long tmp[6];
2659 for (int j = 0; j < 6; ++j)
2660 tmp[j] = 0;
2661 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2662 merge = build_real (TREE_TYPE (vectype), r);
2663 }
2664 else
2665 gcc_unreachable ();
2666 merge = build_vector_from_val (vectype, merge);
2667 return vect_init_vector (stmt, merge, vectype, NULL);
2668}
2669
c48d2d35
RS
2670/* Build a gather load call while vectorizing STMT. Insert new instructions
2671 before GSI and add them to VEC_STMT. GS_INFO describes the gather load
2672 operation. If the load is conditional, MASK is the unvectorized
929b4411 2673 condition and MASK_DT is its definition type, otherwise MASK is null. */
c48d2d35
RS
2674
2675static void
2676vect_build_gather_load_calls (gimple *stmt, gimple_stmt_iterator *gsi,
2677 gimple **vec_stmt, gather_scatter_info *gs_info,
929b4411 2678 tree mask, vect_def_type mask_dt)
c48d2d35
RS
2679{
2680 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2681 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2682 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2683 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2684 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2685 int ncopies = vect_get_num_copies (loop_vinfo, vectype);
2686 edge pe = loop_preheader_edge (loop);
2687 enum { NARROW, NONE, WIDEN } modifier;
2688 poly_uint64 gather_off_nunits
2689 = TYPE_VECTOR_SUBPARTS (gs_info->offset_vectype);
2690
2691 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info->decl));
2692 tree rettype = TREE_TYPE (TREE_TYPE (gs_info->decl));
2693 tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2694 tree ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2695 tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2696 tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2697 tree scaletype = TREE_VALUE (arglist);
2698 gcc_checking_assert (types_compatible_p (srctype, rettype)
2699 && (!mask || types_compatible_p (srctype, masktype)));
2700
2701 tree perm_mask = NULL_TREE;
2702 tree mask_perm_mask = NULL_TREE;
2703 if (known_eq (nunits, gather_off_nunits))
2704 modifier = NONE;
2705 else if (known_eq (nunits * 2, gather_off_nunits))
2706 {
2707 modifier = WIDEN;
2708
2709 /* Currently widening gathers and scatters are only supported for
2710 fixed-length vectors. */
2711 int count = gather_off_nunits.to_constant ();
2712 vec_perm_builder sel (count, count, 1);
2713 for (int i = 0; i < count; ++i)
2714 sel.quick_push (i | (count / 2));
2715
2716 vec_perm_indices indices (sel, 1, count);
2717 perm_mask = vect_gen_perm_mask_checked (gs_info->offset_vectype,
2718 indices);
2719 }
2720 else if (known_eq (nunits, gather_off_nunits * 2))
2721 {
2722 modifier = NARROW;
2723
2724 /* Currently narrowing gathers and scatters are only supported for
2725 fixed-length vectors. */
2726 int count = nunits.to_constant ();
2727 vec_perm_builder sel (count, count, 1);
2728 sel.quick_grow (count);
2729 for (int i = 0; i < count; ++i)
2730 sel[i] = i < count / 2 ? i : i + count / 2;
2731 vec_perm_indices indices (sel, 2, count);
2732 perm_mask = vect_gen_perm_mask_checked (vectype, indices);
2733
2734 ncopies *= 2;
2735
2736 if (mask)
2737 {
2738 for (int i = 0; i < count; ++i)
2739 sel[i] = i | (count / 2);
2740 indices.new_vector (sel, 2, count);
2741 mask_perm_mask = vect_gen_perm_mask_checked (masktype, indices);
2742 }
2743 }
2744 else
2745 gcc_unreachable ();
2746
2747 tree vec_dest = vect_create_destination_var (gimple_get_lhs (stmt),
2748 vectype);
2749
2750 tree ptr = fold_convert (ptrtype, gs_info->base);
2751 if (!is_gimple_min_invariant (ptr))
2752 {
2753 gimple_seq seq;
2754 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
2755 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
2756 gcc_assert (!new_bb);
2757 }
2758
2759 tree scale = build_int_cst (scaletype, gs_info->scale);
2760
2761 tree vec_oprnd0 = NULL_TREE;
2762 tree vec_mask = NULL_TREE;
2763 tree src_op = NULL_TREE;
2764 tree mask_op = NULL_TREE;
2765 tree prev_res = NULL_TREE;
2766 stmt_vec_info prev_stmt_info = NULL;
2767
2768 if (!mask)
2769 {
2770 src_op = vect_build_zero_merge_argument (stmt, rettype);
2771 mask_op = vect_build_all_ones_mask (stmt, masktype);
2772 }
2773
2774 for (int j = 0; j < ncopies; ++j)
2775 {
2776 tree op, var;
2777 gimple *new_stmt;
2778 if (modifier == WIDEN && (j & 1))
2779 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
2780 perm_mask, stmt, gsi);
2781 else if (j == 0)
2782 op = vec_oprnd0
2783 = vect_get_vec_def_for_operand (gs_info->offset, stmt);
2784 else
2785 op = vec_oprnd0
2786 = vect_get_vec_def_for_stmt_copy (gs_info->offset_dt, vec_oprnd0);
2787
2788 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
2789 {
2790 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
2791 TYPE_VECTOR_SUBPARTS (idxtype)));
2792 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
2793 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
2794 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2795 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2796 op = var;
2797 }
2798
2799 if (mask)
2800 {
2801 if (mask_perm_mask && (j & 1))
2802 mask_op = permute_vec_elements (mask_op, mask_op,
2803 mask_perm_mask, stmt, gsi);
2804 else
2805 {
2806 if (j == 0)
2807 vec_mask = vect_get_vec_def_for_operand (mask, stmt);
2808 else
929b4411 2809 vec_mask = vect_get_vec_def_for_stmt_copy (mask_dt, vec_mask);
c48d2d35
RS
2810
2811 mask_op = vec_mask;
2812 if (!useless_type_conversion_p (masktype, TREE_TYPE (vec_mask)))
2813 {
2814 gcc_assert
2815 (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask_op)),
2816 TYPE_VECTOR_SUBPARTS (masktype)));
2817 var = vect_get_new_ssa_name (masktype, vect_simple_var);
2818 mask_op = build1 (VIEW_CONVERT_EXPR, masktype, mask_op);
2819 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR,
2820 mask_op);
2821 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2822 mask_op = var;
2823 }
2824 }
2825 src_op = mask_op;
2826 }
2827
2828 new_stmt = gimple_build_call (gs_info->decl, 5, src_op, ptr, op,
2829 mask_op, scale);
2830
2831 if (!useless_type_conversion_p (vectype, rettype))
2832 {
2833 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
2834 TYPE_VECTOR_SUBPARTS (rettype)));
2835 op = vect_get_new_ssa_name (rettype, vect_simple_var);
2836 gimple_call_set_lhs (new_stmt, op);
2837 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2838 var = make_ssa_name (vec_dest);
2839 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
2840 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2841 }
2842 else
2843 {
2844 var = make_ssa_name (vec_dest, new_stmt);
2845 gimple_call_set_lhs (new_stmt, var);
2846 }
2847
2848 vect_finish_stmt_generation (stmt, new_stmt, gsi);
2849
2850 if (modifier == NARROW)
2851 {
2852 if ((j & 1) == 0)
2853 {
2854 prev_res = var;
2855 continue;
2856 }
2857 var = permute_vec_elements (prev_res, var, perm_mask, stmt, gsi);
2858 new_stmt = SSA_NAME_DEF_STMT (var);
2859 }
2860
dbe1b846 2861 if (prev_stmt_info == NULL_STMT_VEC_INFO)
c48d2d35
RS
2862 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
2863 else
2864 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
2865 prev_stmt_info = vinfo_for_stmt (new_stmt);
2866 }
2867}
2868
bfaa08b7
RS
2869/* Prepare the base and offset in GS_INFO for vectorization.
2870 Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
2871 to the vectorized offset argument for the first copy of STMT. STMT
2872 is the statement described by GS_INFO and LOOP is the containing loop. */
2873
2874static void
2875vect_get_gather_scatter_ops (struct loop *loop, gimple *stmt,
2876 gather_scatter_info *gs_info,
2877 tree *dataref_ptr, tree *vec_offset)
2878{
2879 gimple_seq stmts = NULL;
2880 *dataref_ptr = force_gimple_operand (gs_info->base, &stmts, true, NULL_TREE);
2881 if (stmts != NULL)
2882 {
2883 basic_block new_bb;
2884 edge pe = loop_preheader_edge (loop);
2885 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
2886 gcc_assert (!new_bb);
2887 }
2888 tree offset_type = TREE_TYPE (gs_info->offset);
2889 tree offset_vectype = get_vectype_for_scalar_type (offset_type);
2890 *vec_offset = vect_get_vec_def_for_operand (gs_info->offset, stmt,
2891 offset_vectype);
2892}
2893
ab2fc782
RS
2894/* Prepare to implement a grouped or strided load or store using
2895 the gather load or scatter store operation described by GS_INFO.
2896 STMT is the load or store statement.
2897
2898 Set *DATAREF_BUMP to the amount that should be added to the base
2899 address after each copy of the vectorized statement. Set *VEC_OFFSET
2900 to an invariant offset vector in which element I has the value
2901 I * DR_STEP / SCALE. */
2902
2903static void
2904vect_get_strided_load_store_ops (gimple *stmt, loop_vec_info loop_vinfo,
2905 gather_scatter_info *gs_info,
2906 tree *dataref_bump, tree *vec_offset)
2907{
2908 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2909 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2910 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2911 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2912 gimple_seq stmts;
2913
2914 tree bump = size_binop (MULT_EXPR,
2915 fold_convert (sizetype, DR_STEP (dr)),
2916 size_int (TYPE_VECTOR_SUBPARTS (vectype)));
2917 *dataref_bump = force_gimple_operand (bump, &stmts, true, NULL_TREE);
2918 if (stmts)
2919 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2920
2921 /* The offset given in GS_INFO can have pointer type, so use the element
2922 type of the vector instead. */
2923 tree offset_type = TREE_TYPE (gs_info->offset);
2924 tree offset_vectype = get_vectype_for_scalar_type (offset_type);
2925 offset_type = TREE_TYPE (offset_vectype);
2926
2927 /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
2928 tree step = size_binop (EXACT_DIV_EXPR, DR_STEP (dr),
2929 ssize_int (gs_info->scale));
2930 step = fold_convert (offset_type, step);
2931 step = force_gimple_operand (step, &stmts, true, NULL_TREE);
2932
2933 /* Create {0, X, X*2, X*3, ...}. */
2934 *vec_offset = gimple_build (&stmts, VEC_SERIES_EXPR, offset_vectype,
2935 build_zero_cst (offset_type), step);
2936 if (stmts)
2937 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
2938}
2939
2940/* Return the amount that should be added to a vector pointer to move
2941 to the next or previous copy of AGGR_TYPE. DR is the data reference
2942 being vectorized and MEMORY_ACCESS_TYPE describes the type of
2943 vectorization. */
2944
2945static tree
2946vect_get_data_ptr_increment (data_reference *dr, tree aggr_type,
2947 vect_memory_access_type memory_access_type)
2948{
2949 if (memory_access_type == VMAT_INVARIANT)
2950 return size_zero_node;
2951
2952 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
2953 tree step = vect_dr_behavior (dr)->step;
2954 if (tree_int_cst_sgn (step) == -1)
2955 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
2956 return iv_step;
2957}
2958
37b14185
RB
2959/* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64}. */
2960
2961static bool
2962vectorizable_bswap (gimple *stmt, gimple_stmt_iterator *gsi,
2963 gimple **vec_stmt, slp_tree slp_node,
68435eb2
RB
2964 tree vectype_in, enum vect_def_type *dt,
2965 stmt_vector_for_cost *cost_vec)
37b14185
RB
2966{
2967 tree op, vectype;
2968 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2969 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
928686b1
RS
2970 unsigned ncopies;
2971 unsigned HOST_WIDE_INT nunits, num_bytes;
37b14185
RB
2972
2973 op = gimple_call_arg (stmt, 0);
2974 vectype = STMT_VINFO_VECTYPE (stmt_info);
928686b1
RS
2975
2976 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
2977 return false;
37b14185
RB
2978
2979 /* Multiple types in SLP are handled by creating the appropriate number of
2980 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
2981 case of SLP. */
2982 if (slp_node)
2983 ncopies = 1;
2984 else
e8f142e2 2985 ncopies = vect_get_num_copies (loop_vinfo, vectype);
37b14185
RB
2986
2987 gcc_assert (ncopies >= 1);
2988
2989 tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
2990 if (! char_vectype)
2991 return false;
2992
928686b1
RS
2993 if (!TYPE_VECTOR_SUBPARTS (char_vectype).is_constant (&num_bytes))
2994 return false;
2995
794e3180 2996 unsigned word_bytes = num_bytes / nunits;
908a1a16 2997
d980067b
RS
2998 /* The encoding uses one stepped pattern for each byte in the word. */
2999 vec_perm_builder elts (num_bytes, word_bytes, 3);
3000 for (unsigned i = 0; i < 3; ++i)
37b14185 3001 for (unsigned j = 0; j < word_bytes; ++j)
908a1a16 3002 elts.quick_push ((i + 1) * word_bytes - j - 1);
37b14185 3003
e3342de4
RS
3004 vec_perm_indices indices (elts, 1, num_bytes);
3005 if (!can_vec_perm_const_p (TYPE_MODE (char_vectype), indices))
37b14185
RB
3006 return false;
3007
3008 if (! vec_stmt)
3009 {
3010 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
adac3a68 3011 DUMP_VECT_SCOPE ("vectorizable_bswap");
78604de0 3012 if (! slp_node)
37b14185 3013 {
68435eb2
RB
3014 record_stmt_cost (cost_vec,
3015 1, vector_stmt, stmt_info, 0, vect_prologue);
3016 record_stmt_cost (cost_vec,
3017 ncopies, vec_perm, stmt_info, 0, vect_body);
37b14185
RB
3018 }
3019 return true;
3020 }
3021
736d0f28 3022 tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
37b14185
RB
3023
3024 /* Transform. */
3025 vec<tree> vec_oprnds = vNULL;
3026 gimple *new_stmt = NULL;
3027 stmt_vec_info prev_stmt_info = NULL;
3028 for (unsigned j = 0; j < ncopies; j++)
3029 {
3030 /* Handle uses. */
3031 if (j == 0)
306b0c92 3032 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node);
37b14185
RB
3033 else
3034 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
3035
3036 /* Arguments are ready. create the new vector stmt. */
3037 unsigned i;
3038 tree vop;
3039 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3040 {
3041 tree tem = make_ssa_name (char_vectype);
3042 new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3043 char_vectype, vop));
3044 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3045 tree tem2 = make_ssa_name (char_vectype);
3046 new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3047 tem, tem, bswap_vconst);
3048 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3049 tem = make_ssa_name (vectype);
3050 new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3051 vectype, tem2));
3052 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3053 if (slp_node)
3054 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
3055 }
3056
3057 if (slp_node)
3058 continue;
3059
3060 if (j == 0)
3061 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3062 else
3063 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3064
3065 prev_stmt_info = vinfo_for_stmt (new_stmt);
3066 }
3067
3068 vec_oprnds.release ();
3069 return true;
3070}
3071
b1b6836e
RS
3072/* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3073 integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3074 in a single step. On success, store the binary pack code in
3075 *CONVERT_CODE. */
3076
3077static bool
3078simple_integer_narrowing (tree vectype_out, tree vectype_in,
3079 tree_code *convert_code)
3080{
3081 if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3082 || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3083 return false;
3084
3085 tree_code code;
3086 int multi_step_cvt = 0;
3087 auto_vec <tree, 8> interm_types;
3088 if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3089 &code, &multi_step_cvt,
3090 &interm_types)
3091 || multi_step_cvt)
3092 return false;
3093
3094 *convert_code = code;
3095 return true;
3096}
5ce9450f 3097
ebfd146a
IR
3098/* Function vectorizable_call.
3099
538dd0b7 3100 Check if GS performs a function call that can be vectorized.
b8698a0f 3101 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
3102 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3103 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3104
3105static bool
355fe088 3106vectorizable_call (gimple *gs, gimple_stmt_iterator *gsi, gimple **vec_stmt,
68435eb2 3107 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
ebfd146a 3108{
538dd0b7 3109 gcall *stmt;
ebfd146a
IR
3110 tree vec_dest;
3111 tree scalar_dest;
0267732b 3112 tree op;
ebfd146a 3113 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
538dd0b7 3114 stmt_vec_info stmt_info = vinfo_for_stmt (gs), prev_stmt_info;
ebfd146a 3115 tree vectype_out, vectype_in;
c7bda0f4
RS
3116 poly_uint64 nunits_in;
3117 poly_uint64 nunits_out;
ebfd146a 3118 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
190c2236 3119 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 3120 vec_info *vinfo = stmt_info->vinfo;
81c40241 3121 tree fndecl, new_temp, rhs_type;
2c58d42c
RS
3122 enum vect_def_type dt[4]
3123 = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3124 vect_unknown_def_type };
3125 int ndts = ARRAY_SIZE (dt);
355fe088 3126 gimple *new_stmt = NULL;
ebfd146a 3127 int ncopies, j;
2c58d42c
RS
3128 auto_vec<tree, 8> vargs;
3129 auto_vec<tree, 8> orig_vargs;
ebfd146a
IR
3130 enum { NARROW, NONE, WIDEN } modifier;
3131 size_t i, nargs;
9d5e7640 3132 tree lhs;
ebfd146a 3133
190c2236 3134 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
3135 return false;
3136
66c16fd9
RB
3137 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3138 && ! vec_stmt)
ebfd146a
IR
3139 return false;
3140
538dd0b7
DM
3141 /* Is GS a vectorizable call? */
3142 stmt = dyn_cast <gcall *> (gs);
3143 if (!stmt)
ebfd146a
IR
3144 return false;
3145
5ce9450f 3146 if (gimple_call_internal_p (stmt)
bfaa08b7 3147 && (internal_load_fn_p (gimple_call_internal_fn (stmt))
f307441a 3148 || internal_store_fn_p (gimple_call_internal_fn (stmt))))
c3a8f964
RS
3149 /* Handled by vectorizable_load and vectorizable_store. */
3150 return false;
5ce9450f 3151
0136f8f0
AH
3152 if (gimple_call_lhs (stmt) == NULL_TREE
3153 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
ebfd146a
IR
3154 return false;
3155
0136f8f0 3156 gcc_checking_assert (!stmt_can_throw_internal (stmt));
5a2c1986 3157
b690cc0f
RG
3158 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3159
ebfd146a
IR
3160 /* Process function arguments. */
3161 rhs_type = NULL_TREE;
b690cc0f 3162 vectype_in = NULL_TREE;
ebfd146a
IR
3163 nargs = gimple_call_num_args (stmt);
3164
1b1562a5
MM
3165 /* Bail out if the function has more than three arguments, we do not have
3166 interesting builtin functions to vectorize with more than two arguments
3167 except for fma. No arguments is also not good. */
2c58d42c 3168 if (nargs == 0 || nargs > 4)
ebfd146a
IR
3169 return false;
3170
74bf76ed 3171 /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */
2c58d42c
RS
3172 combined_fn cfn = gimple_call_combined_fn (stmt);
3173 if (cfn == CFN_GOMP_SIMD_LANE)
74bf76ed
JJ
3174 {
3175 nargs = 0;
3176 rhs_type = unsigned_type_node;
3177 }
3178
2c58d42c
RS
3179 int mask_opno = -1;
3180 if (internal_fn_p (cfn))
3181 mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3182
ebfd146a
IR
3183 for (i = 0; i < nargs; i++)
3184 {
b690cc0f
RG
3185 tree opvectype;
3186
ebfd146a 3187 op = gimple_call_arg (stmt, i);
2c58d42c
RS
3188 if (!vect_is_simple_use (op, vinfo, &dt[i], &opvectype))
3189 {
3190 if (dump_enabled_p ())
3191 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3192 "use not simple.\n");
3193 return false;
3194 }
3195
3196 /* Skip the mask argument to an internal function. This operand
3197 has been converted via a pattern if necessary. */
3198 if ((int) i == mask_opno)
3199 continue;
ebfd146a
IR
3200
3201 /* We can only handle calls with arguments of the same type. */
3202 if (rhs_type
8533c9d8 3203 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
ebfd146a 3204 {
73fbfcad 3205 if (dump_enabled_p ())
78c60e3d 3206 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3207 "argument types differ.\n");
ebfd146a
IR
3208 return false;
3209 }
b690cc0f
RG
3210 if (!rhs_type)
3211 rhs_type = TREE_TYPE (op);
ebfd146a 3212
b690cc0f
RG
3213 if (!vectype_in)
3214 vectype_in = opvectype;
3215 else if (opvectype
3216 && opvectype != vectype_in)
3217 {
73fbfcad 3218 if (dump_enabled_p ())
78c60e3d 3219 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3220 "argument vector types differ.\n");
b690cc0f
RG
3221 return false;
3222 }
3223 }
3224 /* If all arguments are external or constant defs use a vector type with
3225 the same size as the output vector type. */
ebfd146a 3226 if (!vectype_in)
b690cc0f 3227 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
7d8930a0
IR
3228 if (vec_stmt)
3229 gcc_assert (vectype_in);
3230 if (!vectype_in)
3231 {
73fbfcad 3232 if (dump_enabled_p ())
7d8930a0 3233 {
78c60e3d
SS
3234 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3235 "no vectype for scalar type ");
3236 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
e645e942 3237 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
7d8930a0
IR
3238 }
3239
3240 return false;
3241 }
ebfd146a
IR
3242
3243 /* FORNOW */
b690cc0f
RG
3244 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3245 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
c7bda0f4 3246 if (known_eq (nunits_in * 2, nunits_out))
ebfd146a 3247 modifier = NARROW;
c7bda0f4 3248 else if (known_eq (nunits_out, nunits_in))
ebfd146a 3249 modifier = NONE;
c7bda0f4 3250 else if (known_eq (nunits_out * 2, nunits_in))
ebfd146a
IR
3251 modifier = WIDEN;
3252 else
3253 return false;
3254
70439f0d
RS
3255 /* We only handle functions that do not read or clobber memory. */
3256 if (gimple_vuse (stmt))
3257 {
3258 if (dump_enabled_p ())
3259 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3260 "function reads from or writes to memory.\n");
3261 return false;
3262 }
3263
ebfd146a
IR
3264 /* For now, we only vectorize functions if a target specific builtin
3265 is available. TODO -- in some cases, it might be profitable to
3266 insert the calls for pieces of the vector, in order to be able
3267 to vectorize other operations in the loop. */
70439f0d
RS
3268 fndecl = NULL_TREE;
3269 internal_fn ifn = IFN_LAST;
70439f0d
RS
3270 tree callee = gimple_call_fndecl (stmt);
3271
3272 /* First try using an internal function. */
b1b6836e
RS
3273 tree_code convert_code = ERROR_MARK;
3274 if (cfn != CFN_LAST
3275 && (modifier == NONE
3276 || (modifier == NARROW
3277 && simple_integer_narrowing (vectype_out, vectype_in,
3278 &convert_code))))
70439f0d
RS
3279 ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3280 vectype_in);
3281
3282 /* If that fails, try asking for a target-specific built-in function. */
3283 if (ifn == IFN_LAST)
3284 {
3285 if (cfn != CFN_LAST)
3286 fndecl = targetm.vectorize.builtin_vectorized_function
3287 (cfn, vectype_out, vectype_in);
7672aa9b 3288 else if (callee)
70439f0d
RS
3289 fndecl = targetm.vectorize.builtin_md_vectorized_function
3290 (callee, vectype_out, vectype_in);
3291 }
3292
3293 if (ifn == IFN_LAST && !fndecl)
ebfd146a 3294 {
70439f0d 3295 if (cfn == CFN_GOMP_SIMD_LANE
74bf76ed
JJ
3296 && !slp_node
3297 && loop_vinfo
3298 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3299 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3300 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3301 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3302 {
3303 /* We can handle IFN_GOMP_SIMD_LANE by returning a
3304 { 0, 1, 2, ... vf - 1 } vector. */
3305 gcc_assert (nargs == 0);
3306 }
37b14185
RB
3307 else if (modifier == NONE
3308 && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3309 || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3310 || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)))
3311 return vectorizable_bswap (stmt, gsi, vec_stmt, slp_node,
68435eb2 3312 vectype_in, dt, cost_vec);
74bf76ed
JJ
3313 else
3314 {
3315 if (dump_enabled_p ())
3316 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3317 "function is not vectorizable.\n");
74bf76ed
JJ
3318 return false;
3319 }
ebfd146a
IR
3320 }
3321
fce57248 3322 if (slp_node)
190c2236 3323 ncopies = 1;
b1b6836e 3324 else if (modifier == NARROW && ifn == IFN_LAST)
e8f142e2 3325 ncopies = vect_get_num_copies (loop_vinfo, vectype_out);
ebfd146a 3326 else
e8f142e2 3327 ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
ebfd146a
IR
3328
3329 /* Sanity check: make sure that at least one copy of the vectorized stmt
3330 needs to be generated. */
3331 gcc_assert (ncopies >= 1);
3332
ed623edb 3333 vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
ebfd146a
IR
3334 if (!vec_stmt) /* transformation not required. */
3335 {
3336 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
adac3a68 3337 DUMP_VECT_SCOPE ("vectorizable_call");
68435eb2
RB
3338 vect_model_simple_cost (stmt_info, ncopies, dt, ndts, slp_node, cost_vec);
3339 if (ifn != IFN_LAST && modifier == NARROW && !slp_node)
3340 record_stmt_cost (cost_vec, ncopies / 2,
3341 vec_promote_demote, stmt_info, 0, vect_body);
b1b6836e 3342
2c58d42c
RS
3343 if (loop_vinfo && mask_opno >= 0)
3344 {
3345 unsigned int nvectors = (slp_node
3346 ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node)
3347 : ncopies);
3348 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out);
3349 }
ebfd146a
IR
3350 return true;
3351 }
3352
67b8dbac 3353 /* Transform. */
ebfd146a 3354
73fbfcad 3355 if (dump_enabled_p ())
e645e942 3356 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
ebfd146a
IR
3357
3358 /* Handle def. */
3359 scalar_dest = gimple_call_lhs (stmt);
3360 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3361
2c58d42c
RS
3362 bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3363
ebfd146a 3364 prev_stmt_info = NULL;
b1b6836e 3365 if (modifier == NONE || ifn != IFN_LAST)
ebfd146a 3366 {
b1b6836e 3367 tree prev_res = NULL_TREE;
2c58d42c
RS
3368 vargs.safe_grow (nargs);
3369 orig_vargs.safe_grow (nargs);
ebfd146a
IR
3370 for (j = 0; j < ncopies; ++j)
3371 {
3372 /* Build argument list for the vectorized call. */
190c2236
JJ
3373 if (slp_node)
3374 {
ef062b13 3375 auto_vec<vec<tree> > vec_defs (nargs);
9771b263 3376 vec<tree> vec_oprnds0;
190c2236
JJ
3377
3378 for (i = 0; i < nargs; i++)
2c58d42c 3379 vargs[i] = gimple_call_arg (stmt, i);
306b0c92 3380 vect_get_slp_defs (vargs, slp_node, &vec_defs);
37b5ec8f 3381 vec_oprnds0 = vec_defs[0];
190c2236
JJ
3382
3383 /* Arguments are ready. Create the new vector stmt. */
9771b263 3384 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
190c2236
JJ
3385 {
3386 size_t k;
3387 for (k = 0; k < nargs; k++)
3388 {
37b5ec8f 3389 vec<tree> vec_oprndsk = vec_defs[k];
9771b263 3390 vargs[k] = vec_oprndsk[i];
190c2236 3391 }
b1b6836e
RS
3392 if (modifier == NARROW)
3393 {
2c58d42c
RS
3394 /* We don't define any narrowing conditional functions
3395 at present. */
3396 gcc_assert (mask_opno < 0);
b1b6836e 3397 tree half_res = make_ssa_name (vectype_in);
a844293d
RS
3398 gcall *call
3399 = gimple_build_call_internal_vec (ifn, vargs);
3400 gimple_call_set_lhs (call, half_res);
3401 gimple_call_set_nothrow (call, true);
3402 new_stmt = call;
b1b6836e
RS
3403 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3404 if ((i & 1) == 0)
3405 {
3406 prev_res = half_res;
3407 continue;
3408 }
3409 new_temp = make_ssa_name (vec_dest);
3410 new_stmt = gimple_build_assign (new_temp, convert_code,
3411 prev_res, half_res);
3412 }
70439f0d 3413 else
b1b6836e 3414 {
2c58d42c
RS
3415 if (mask_opno >= 0 && masked_loop_p)
3416 {
3417 unsigned int vec_num = vec_oprnds0.length ();
3418 /* Always true for SLP. */
3419 gcc_assert (ncopies == 1);
3420 tree mask = vect_get_loop_mask (gsi, masks, vec_num,
3421 vectype_out, i);
3422 vargs[mask_opno] = prepare_load_store_mask
3423 (TREE_TYPE (mask), mask, vargs[mask_opno], gsi);
3424 }
3425
a844293d 3426 gcall *call;
b1b6836e 3427 if (ifn != IFN_LAST)
a844293d 3428 call = gimple_build_call_internal_vec (ifn, vargs);
b1b6836e 3429 else
a844293d
RS
3430 call = gimple_build_call_vec (fndecl, vargs);
3431 new_temp = make_ssa_name (vec_dest, call);
3432 gimple_call_set_lhs (call, new_temp);
3433 gimple_call_set_nothrow (call, true);
3434 new_stmt = call;
b1b6836e 3435 }
190c2236 3436 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9771b263 3437 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
190c2236
JJ
3438 }
3439
3440 for (i = 0; i < nargs; i++)
3441 {
37b5ec8f 3442 vec<tree> vec_oprndsi = vec_defs[i];
9771b263 3443 vec_oprndsi.release ();
190c2236 3444 }
190c2236
JJ
3445 continue;
3446 }
3447
ebfd146a
IR
3448 for (i = 0; i < nargs; i++)
3449 {
3450 op = gimple_call_arg (stmt, i);
3451 if (j == 0)
3452 vec_oprnd0
81c40241 3453 = vect_get_vec_def_for_operand (op, stmt);
ebfd146a 3454 else
2c58d42c
RS
3455 vec_oprnd0
3456 = vect_get_vec_def_for_stmt_copy (dt[i], orig_vargs[i]);
3457
3458 orig_vargs[i] = vargs[i] = vec_oprnd0;
3459 }
ebfd146a 3460
2c58d42c
RS
3461 if (mask_opno >= 0 && masked_loop_p)
3462 {
3463 tree mask = vect_get_loop_mask (gsi, masks, ncopies,
3464 vectype_out, j);
3465 vargs[mask_opno]
3466 = prepare_load_store_mask (TREE_TYPE (mask), mask,
3467 vargs[mask_opno], gsi);
ebfd146a
IR
3468 }
3469
2c58d42c 3470 if (cfn == CFN_GOMP_SIMD_LANE)
74bf76ed 3471 {
c7bda0f4 3472 tree cst = build_index_vector (vectype_out, j * nunits_out, 1);
74bf76ed 3473 tree new_var
0e22bb5a 3474 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
355fe088 3475 gimple *init_stmt = gimple_build_assign (new_var, cst);
74bf76ed 3476 vect_init_vector_1 (stmt, init_stmt, NULL);
b731b390 3477 new_temp = make_ssa_name (vec_dest);
0e22bb5a 3478 new_stmt = gimple_build_assign (new_temp, new_var);
74bf76ed 3479 }
b1b6836e
RS
3480 else if (modifier == NARROW)
3481 {
2c58d42c
RS
3482 /* We don't define any narrowing conditional functions at
3483 present. */
3484 gcc_assert (mask_opno < 0);
b1b6836e 3485 tree half_res = make_ssa_name (vectype_in);
a844293d
RS
3486 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3487 gimple_call_set_lhs (call, half_res);
3488 gimple_call_set_nothrow (call, true);
3489 new_stmt = call;
b1b6836e
RS
3490 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3491 if ((j & 1) == 0)
3492 {
3493 prev_res = half_res;
3494 continue;
3495 }
3496 new_temp = make_ssa_name (vec_dest);
3497 new_stmt = gimple_build_assign (new_temp, convert_code,
3498 prev_res, half_res);
3499 }
74bf76ed
JJ
3500 else
3501 {
a844293d 3502 gcall *call;
70439f0d 3503 if (ifn != IFN_LAST)
a844293d 3504 call = gimple_build_call_internal_vec (ifn, vargs);
70439f0d 3505 else
a844293d 3506 call = gimple_build_call_vec (fndecl, vargs);
74bf76ed 3507 new_temp = make_ssa_name (vec_dest, new_stmt);
a844293d
RS
3508 gimple_call_set_lhs (call, new_temp);
3509 gimple_call_set_nothrow (call, true);
3510 new_stmt = call;
74bf76ed 3511 }
ebfd146a
IR
3512 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3513
b1b6836e 3514 if (j == (modifier == NARROW ? 1 : 0))
ebfd146a
IR
3515 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3516 else
3517 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3518
3519 prev_stmt_info = vinfo_for_stmt (new_stmt);
3520 }
b1b6836e
RS
3521 }
3522 else if (modifier == NARROW)
3523 {
2c58d42c
RS
3524 /* We don't define any narrowing conditional functions at present. */
3525 gcc_assert (mask_opno < 0);
ebfd146a
IR
3526 for (j = 0; j < ncopies; ++j)
3527 {
3528 /* Build argument list for the vectorized call. */
3529 if (j == 0)
9771b263 3530 vargs.create (nargs * 2);
ebfd146a 3531 else
9771b263 3532 vargs.truncate (0);
ebfd146a 3533
190c2236
JJ
3534 if (slp_node)
3535 {
ef062b13 3536 auto_vec<vec<tree> > vec_defs (nargs);
9771b263 3537 vec<tree> vec_oprnds0;
190c2236
JJ
3538
3539 for (i = 0; i < nargs; i++)
9771b263 3540 vargs.quick_push (gimple_call_arg (stmt, i));
306b0c92 3541 vect_get_slp_defs (vargs, slp_node, &vec_defs);
37b5ec8f 3542 vec_oprnds0 = vec_defs[0];
190c2236
JJ
3543
3544 /* Arguments are ready. Create the new vector stmt. */
9771b263 3545 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
190c2236
JJ
3546 {
3547 size_t k;
9771b263 3548 vargs.truncate (0);
190c2236
JJ
3549 for (k = 0; k < nargs; k++)
3550 {
37b5ec8f 3551 vec<tree> vec_oprndsk = vec_defs[k];
9771b263
DN
3552 vargs.quick_push (vec_oprndsk[i]);
3553 vargs.quick_push (vec_oprndsk[i + 1]);
190c2236 3554 }
a844293d 3555 gcall *call;
70439f0d 3556 if (ifn != IFN_LAST)
a844293d 3557 call = gimple_build_call_internal_vec (ifn, vargs);
70439f0d 3558 else
a844293d
RS
3559 call = gimple_build_call_vec (fndecl, vargs);
3560 new_temp = make_ssa_name (vec_dest, call);
3561 gimple_call_set_lhs (call, new_temp);
3562 gimple_call_set_nothrow (call, true);
3563 new_stmt = call;
190c2236 3564 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9771b263 3565 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
190c2236
JJ
3566 }
3567
3568 for (i = 0; i < nargs; i++)
3569 {
37b5ec8f 3570 vec<tree> vec_oprndsi = vec_defs[i];
9771b263 3571 vec_oprndsi.release ();
190c2236 3572 }
190c2236
JJ
3573 continue;
3574 }
3575
ebfd146a
IR
3576 for (i = 0; i < nargs; i++)
3577 {
3578 op = gimple_call_arg (stmt, i);
3579 if (j == 0)
3580 {
3581 vec_oprnd0
81c40241 3582 = vect_get_vec_def_for_operand (op, stmt);
ebfd146a 3583 vec_oprnd1
63827fb8 3584 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
ebfd146a
IR
3585 }
3586 else
3587 {
336ecb65 3588 vec_oprnd1 = gimple_call_arg (new_stmt, 2*i + 1);
ebfd146a 3589 vec_oprnd0
63827fb8 3590 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd1);
ebfd146a 3591 vec_oprnd1
63827fb8 3592 = vect_get_vec_def_for_stmt_copy (dt[i], vec_oprnd0);
ebfd146a
IR
3593 }
3594
9771b263
DN
3595 vargs.quick_push (vec_oprnd0);
3596 vargs.quick_push (vec_oprnd1);
ebfd146a
IR
3597 }
3598
b1b6836e 3599 new_stmt = gimple_build_call_vec (fndecl, vargs);
ebfd146a
IR
3600 new_temp = make_ssa_name (vec_dest, new_stmt);
3601 gimple_call_set_lhs (new_stmt, new_temp);
ebfd146a
IR
3602 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3603
3604 if (j == 0)
3605 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3606 else
3607 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3608
3609 prev_stmt_info = vinfo_for_stmt (new_stmt);
3610 }
3611
3612 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
ebfd146a 3613 }
b1b6836e
RS
3614 else
3615 /* No current target implements this case. */
3616 return false;
ebfd146a 3617
9771b263 3618 vargs.release ();
ebfd146a 3619
ebfd146a
IR
3620 /* The call in STMT might prevent it from being removed in dce.
3621 We however cannot remove it here, due to the way the ssa name
3622 it defines is mapped to the new definition. So just replace
3623 rhs of the statement with something harmless. */
3624
dd34c087
JJ
3625 if (slp_node)
3626 return true;
3627
9d5e7640 3628 if (is_pattern_stmt_p (stmt_info))
ed7b8123
RS
3629 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
3630 lhs = gimple_get_lhs (stmt_info->stmt);
3cc2fa2a 3631
0267732b 3632 new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
ebfd146a 3633 set_vinfo_for_stmt (new_stmt, stmt_info);
ed7b8123 3634 set_vinfo_for_stmt (stmt_info->stmt, NULL);
ebfd146a
IR
3635 STMT_VINFO_STMT (stmt_info) = new_stmt;
3636 gsi_replace (gsi, new_stmt, false);
ebfd146a
IR
3637
3638 return true;
3639}
3640
3641
0136f8f0
AH
3642struct simd_call_arg_info
3643{
3644 tree vectype;
3645 tree op;
0136f8f0 3646 HOST_WIDE_INT linear_step;
34e82342 3647 enum vect_def_type dt;
0136f8f0 3648 unsigned int align;
17b658af 3649 bool simd_lane_linear;
0136f8f0
AH
3650};
3651
17b658af
JJ
3652/* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
3653 is linear within simd lane (but not within whole loop), note it in
3654 *ARGINFO. */
3655
3656static void
3657vect_simd_lane_linear (tree op, struct loop *loop,
3658 struct simd_call_arg_info *arginfo)
3659{
355fe088 3660 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
17b658af
JJ
3661
3662 if (!is_gimple_assign (def_stmt)
3663 || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
3664 || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
3665 return;
3666
3667 tree base = gimple_assign_rhs1 (def_stmt);
3668 HOST_WIDE_INT linear_step = 0;
3669 tree v = gimple_assign_rhs2 (def_stmt);
3670 while (TREE_CODE (v) == SSA_NAME)
3671 {
3672 tree t;
3673 def_stmt = SSA_NAME_DEF_STMT (v);
3674 if (is_gimple_assign (def_stmt))
3675 switch (gimple_assign_rhs_code (def_stmt))
3676 {
3677 case PLUS_EXPR:
3678 t = gimple_assign_rhs2 (def_stmt);
3679 if (linear_step || TREE_CODE (t) != INTEGER_CST)
3680 return;
3681 base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
3682 v = gimple_assign_rhs1 (def_stmt);
3683 continue;
3684 case MULT_EXPR:
3685 t = gimple_assign_rhs2 (def_stmt);
3686 if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
3687 return;
3688 linear_step = tree_to_shwi (t);
3689 v = gimple_assign_rhs1 (def_stmt);
3690 continue;
3691 CASE_CONVERT:
3692 t = gimple_assign_rhs1 (def_stmt);
3693 if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
3694 || (TYPE_PRECISION (TREE_TYPE (v))
3695 < TYPE_PRECISION (TREE_TYPE (t))))
3696 return;
3697 if (!linear_step)
3698 linear_step = 1;
3699 v = t;
3700 continue;
3701 default:
3702 return;
3703 }
8e4284d0 3704 else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
17b658af
JJ
3705 && loop->simduid
3706 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
3707 && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
3708 == loop->simduid))
3709 {
3710 if (!linear_step)
3711 linear_step = 1;
3712 arginfo->linear_step = linear_step;
3713 arginfo->op = base;
3714 arginfo->simd_lane_linear = true;
3715 return;
3716 }
3717 }
3718}
3719
cf1b2ba4
RS
3720/* Return the number of elements in vector type VECTYPE, which is associated
3721 with a SIMD clone. At present these vectors always have a constant
3722 length. */
3723
3724static unsigned HOST_WIDE_INT
3725simd_clone_subparts (tree vectype)
3726{
928686b1 3727 return TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
cf1b2ba4
RS
3728}
3729
0136f8f0
AH
3730/* Function vectorizable_simd_clone_call.
3731
3732 Check if STMT performs a function call that can be vectorized
3733 by calling a simd clone of the function.
3734 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3735 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
3736 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3737
3738static bool
355fe088 3739vectorizable_simd_clone_call (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
3740 gimple **vec_stmt, slp_tree slp_node,
3741 stmt_vector_for_cost *)
0136f8f0
AH
3742{
3743 tree vec_dest;
3744 tree scalar_dest;
3745 tree op, type;
3746 tree vec_oprnd0 = NULL_TREE;
3747 stmt_vec_info stmt_info = vinfo_for_stmt (stmt), prev_stmt_info;
3748 tree vectype;
3749 unsigned int nunits;
3750 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3751 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 3752 vec_info *vinfo = stmt_info->vinfo;
0136f8f0 3753 struct loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
81c40241 3754 tree fndecl, new_temp;
355fe088 3755 gimple *new_stmt = NULL;
0136f8f0 3756 int ncopies, j;
00426f9a 3757 auto_vec<simd_call_arg_info> arginfo;
0136f8f0
AH
3758 vec<tree> vargs = vNULL;
3759 size_t i, nargs;
3760 tree lhs, rtype, ratype;
e7a74006 3761 vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
0136f8f0
AH
3762
3763 /* Is STMT a vectorizable call? */
3764 if (!is_gimple_call (stmt))
3765 return false;
3766
3767 fndecl = gimple_call_fndecl (stmt);
3768 if (fndecl == NULL_TREE)
3769 return false;
3770
d52f5295 3771 struct cgraph_node *node = cgraph_node::get (fndecl);
0136f8f0
AH
3772 if (node == NULL || node->simd_clones == NULL)
3773 return false;
3774
3775 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3776 return false;
3777
66c16fd9
RB
3778 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3779 && ! vec_stmt)
0136f8f0
AH
3780 return false;
3781
3782 if (gimple_call_lhs (stmt)
3783 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3784 return false;
3785
3786 gcc_checking_assert (!stmt_can_throw_internal (stmt));
3787
3788 vectype = STMT_VINFO_VECTYPE (stmt_info);
3789
3790 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt))
3791 return false;
3792
3793 /* FORNOW */
fce57248 3794 if (slp_node)
0136f8f0
AH
3795 return false;
3796
3797 /* Process function arguments. */
3798 nargs = gimple_call_num_args (stmt);
3799
3800 /* Bail out if the function has zero arguments. */
3801 if (nargs == 0)
3802 return false;
3803
00426f9a 3804 arginfo.reserve (nargs, true);
0136f8f0
AH
3805
3806 for (i = 0; i < nargs; i++)
3807 {
3808 simd_call_arg_info thisarginfo;
3809 affine_iv iv;
3810
3811 thisarginfo.linear_step = 0;
3812 thisarginfo.align = 0;
3813 thisarginfo.op = NULL_TREE;
17b658af 3814 thisarginfo.simd_lane_linear = false;
0136f8f0
AH
3815
3816 op = gimple_call_arg (stmt, i);
894dd753 3817 if (!vect_is_simple_use (op, vinfo, &thisarginfo.dt,
81c40241 3818 &thisarginfo.vectype)
0136f8f0
AH
3819 || thisarginfo.dt == vect_uninitialized_def)
3820 {
3821 if (dump_enabled_p ())
3822 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3823 "use not simple.\n");
0136f8f0
AH
3824 return false;
3825 }
3826
3827 if (thisarginfo.dt == vect_constant_def
3828 || thisarginfo.dt == vect_external_def)
3829 gcc_assert (thisarginfo.vectype == NULL_TREE);
3830 else
3831 gcc_assert (thisarginfo.vectype != NULL_TREE);
3832
6c9e85fb
JJ
3833 /* For linear arguments, the analyze phase should have saved
3834 the base and step in STMT_VINFO_SIMD_CLONE_INFO. */
17b658af
JJ
3835 if (i * 3 + 4 <= STMT_VINFO_SIMD_CLONE_INFO (stmt_info).length ()
3836 && STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2])
6c9e85fb
JJ
3837 {
3838 gcc_assert (vec_stmt);
3839 thisarginfo.linear_step
17b658af 3840 = tree_to_shwi (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2]);
6c9e85fb 3841 thisarginfo.op
17b658af
JJ
3842 = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 1];
3843 thisarginfo.simd_lane_linear
3844 = (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 3]
3845 == boolean_true_node);
6c9e85fb
JJ
3846 /* If loop has been peeled for alignment, we need to adjust it. */
3847 tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
3848 tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
17b658af 3849 if (n1 != n2 && !thisarginfo.simd_lane_linear)
6c9e85fb
JJ
3850 {
3851 tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
17b658af 3852 tree step = STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[i * 3 + 2];
6c9e85fb
JJ
3853 tree opt = TREE_TYPE (thisarginfo.op);
3854 bias = fold_convert (TREE_TYPE (step), bias);
3855 bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
3856 thisarginfo.op
3857 = fold_build2 (POINTER_TYPE_P (opt)
3858 ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
3859 thisarginfo.op, bias);
3860 }
3861 }
3862 else if (!vec_stmt
3863 && thisarginfo.dt != vect_constant_def
3864 && thisarginfo.dt != vect_external_def
3865 && loop_vinfo
3866 && TREE_CODE (op) == SSA_NAME
3867 && simple_iv (loop, loop_containing_stmt (stmt), op,
3868 &iv, false)
3869 && tree_fits_shwi_p (iv.step))
0136f8f0
AH
3870 {
3871 thisarginfo.linear_step = tree_to_shwi (iv.step);
3872 thisarginfo.op = iv.base;
3873 }
3874 else if ((thisarginfo.dt == vect_constant_def
3875 || thisarginfo.dt == vect_external_def)
3876 && POINTER_TYPE_P (TREE_TYPE (op)))
3877 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
17b658af
JJ
3878 /* Addresses of array elements indexed by GOMP_SIMD_LANE are
3879 linear too. */
3880 if (POINTER_TYPE_P (TREE_TYPE (op))
3881 && !thisarginfo.linear_step
3882 && !vec_stmt
3883 && thisarginfo.dt != vect_constant_def
3884 && thisarginfo.dt != vect_external_def
3885 && loop_vinfo
3886 && !slp_node
3887 && TREE_CODE (op) == SSA_NAME)
3888 vect_simd_lane_linear (op, loop, &thisarginfo);
0136f8f0
AH
3889
3890 arginfo.quick_push (thisarginfo);
3891 }
3892
d9f21f6a
RS
3893 unsigned HOST_WIDE_INT vf;
3894 if (!LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&vf))
3895 {
3896 if (dump_enabled_p ())
3897 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3898 "not considering SIMD clones; not yet supported"
3899 " for variable-width vectors.\n");
3900 return NULL;
3901 }
3902
0136f8f0
AH
3903 unsigned int badness = 0;
3904 struct cgraph_node *bestn = NULL;
6c9e85fb
JJ
3905 if (STMT_VINFO_SIMD_CLONE_INFO (stmt_info).exists ())
3906 bestn = cgraph_node::get (STMT_VINFO_SIMD_CLONE_INFO (stmt_info)[0]);
0136f8f0
AH
3907 else
3908 for (struct cgraph_node *n = node->simd_clones; n != NULL;
3909 n = n->simdclone->next_clone)
3910 {
3911 unsigned int this_badness = 0;
d9f21f6a 3912 if (n->simdclone->simdlen > vf
0136f8f0
AH
3913 || n->simdclone->nargs != nargs)
3914 continue;
d9f21f6a
RS
3915 if (n->simdclone->simdlen < vf)
3916 this_badness += (exact_log2 (vf)
0136f8f0
AH
3917 - exact_log2 (n->simdclone->simdlen)) * 1024;
3918 if (n->simdclone->inbranch)
3919 this_badness += 2048;
3920 int target_badness = targetm.simd_clone.usable (n);
3921 if (target_badness < 0)
3922 continue;
3923 this_badness += target_badness * 512;
3924 /* FORNOW: Have to add code to add the mask argument. */
3925 if (n->simdclone->inbranch)
3926 continue;
3927 for (i = 0; i < nargs; i++)
3928 {
3929 switch (n->simdclone->args[i].arg_type)
3930 {
3931 case SIMD_CLONE_ARG_TYPE_VECTOR:
3932 if (!useless_type_conversion_p
3933 (n->simdclone->args[i].orig_type,
3934 TREE_TYPE (gimple_call_arg (stmt, i))))
3935 i = -1;
3936 else if (arginfo[i].dt == vect_constant_def
3937 || arginfo[i].dt == vect_external_def
3938 || arginfo[i].linear_step)
3939 this_badness += 64;
3940 break;
3941 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3942 if (arginfo[i].dt != vect_constant_def
3943 && arginfo[i].dt != vect_external_def)
3944 i = -1;
3945 break;
3946 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
d9a6bd32 3947 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
0136f8f0
AH
3948 if (arginfo[i].dt == vect_constant_def
3949 || arginfo[i].dt == vect_external_def
3950 || (arginfo[i].linear_step
3951 != n->simdclone->args[i].linear_step))
3952 i = -1;
3953 break;
3954 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
d9a6bd32
JJ
3955 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
3956 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
e01d41e5
JJ
3957 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
3958 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
3959 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
0136f8f0
AH
3960 /* FORNOW */
3961 i = -1;
3962 break;
3963 case SIMD_CLONE_ARG_TYPE_MASK:
3964 gcc_unreachable ();
3965 }
3966 if (i == (size_t) -1)
3967 break;
3968 if (n->simdclone->args[i].alignment > arginfo[i].align)
3969 {
3970 i = -1;
3971 break;
3972 }
3973 if (arginfo[i].align)
3974 this_badness += (exact_log2 (arginfo[i].align)
3975 - exact_log2 (n->simdclone->args[i].alignment));
3976 }
3977 if (i == (size_t) -1)
3978 continue;
3979 if (bestn == NULL || this_badness < badness)
3980 {
3981 bestn = n;
3982 badness = this_badness;
3983 }
3984 }
3985
3986 if (bestn == NULL)
00426f9a 3987 return false;
0136f8f0
AH
3988
3989 for (i = 0; i < nargs; i++)
3990 if ((arginfo[i].dt == vect_constant_def
3991 || arginfo[i].dt == vect_external_def)
3992 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
3993 {
3994 arginfo[i].vectype
3995 = get_vectype_for_scalar_type (TREE_TYPE (gimple_call_arg (stmt,
3996 i)));
3997 if (arginfo[i].vectype == NULL
cf1b2ba4 3998 || (simd_clone_subparts (arginfo[i].vectype)
0136f8f0 3999 > bestn->simdclone->simdlen))
00426f9a 4000 return false;
0136f8f0
AH
4001 }
4002
4003 fndecl = bestn->decl;
4004 nunits = bestn->simdclone->simdlen;
d9f21f6a 4005 ncopies = vf / nunits;
0136f8f0
AH
4006
4007 /* If the function isn't const, only allow it in simd loops where user
4008 has asserted that at least nunits consecutive iterations can be
4009 performed using SIMD instructions. */
4010 if ((loop == NULL || (unsigned) loop->safelen < nunits)
4011 && gimple_vuse (stmt))
00426f9a 4012 return false;
0136f8f0
AH
4013
4014 /* Sanity check: make sure that at least one copy of the vectorized stmt
4015 needs to be generated. */
4016 gcc_assert (ncopies >= 1);
4017
4018 if (!vec_stmt) /* transformation not required. */
4019 {
6c9e85fb
JJ
4020 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (bestn->decl);
4021 for (i = 0; i < nargs; i++)
7adb26f2
JJ
4022 if ((bestn->simdclone->args[i].arg_type
4023 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
4024 || (bestn->simdclone->args[i].arg_type
4025 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP))
6c9e85fb 4026 {
17b658af 4027 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_grow_cleared (i * 3
6c9e85fb
JJ
4028 + 1);
4029 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (arginfo[i].op);
4030 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4031 ? size_type_node : TREE_TYPE (arginfo[i].op);
4032 tree ls = build_int_cst (lst, arginfo[i].linear_step);
4033 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (ls);
17b658af
JJ
4034 tree sll = arginfo[i].simd_lane_linear
4035 ? boolean_true_node : boolean_false_node;
4036 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (sll);
6c9e85fb 4037 }
0136f8f0 4038 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
adac3a68 4039 DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
68435eb2 4040/* vect_model_simple_cost (stmt_info, ncopies, dt, slp_node, cost_vec); */
0136f8f0
AH
4041 return true;
4042 }
4043
67b8dbac 4044 /* Transform. */
0136f8f0
AH
4045
4046 if (dump_enabled_p ())
4047 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4048
4049 /* Handle def. */
4050 scalar_dest = gimple_call_lhs (stmt);
4051 vec_dest = NULL_TREE;
4052 rtype = NULL_TREE;
4053 ratype = NULL_TREE;
4054 if (scalar_dest)
4055 {
4056 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4057 rtype = TREE_TYPE (TREE_TYPE (fndecl));
4058 if (TREE_CODE (rtype) == ARRAY_TYPE)
4059 {
4060 ratype = rtype;
4061 rtype = TREE_TYPE (ratype);
4062 }
4063 }
4064
4065 prev_stmt_info = NULL;
4066 for (j = 0; j < ncopies; ++j)
4067 {
4068 /* Build argument list for the vectorized call. */
4069 if (j == 0)
4070 vargs.create (nargs);
4071 else
4072 vargs.truncate (0);
4073
4074 for (i = 0; i < nargs; i++)
4075 {
4076 unsigned int k, l, m, o;
4077 tree atype;
4078 op = gimple_call_arg (stmt, i);
4079 switch (bestn->simdclone->args[i].arg_type)
4080 {
4081 case SIMD_CLONE_ARG_TYPE_VECTOR:
4082 atype = bestn->simdclone->args[i].vector_type;
cf1b2ba4 4083 o = nunits / simd_clone_subparts (atype);
0136f8f0
AH
4084 for (m = j * o; m < (j + 1) * o; m++)
4085 {
cf1b2ba4
RS
4086 if (simd_clone_subparts (atype)
4087 < simd_clone_subparts (arginfo[i].vectype))
0136f8f0 4088 {
73a699ae 4089 poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
cf1b2ba4
RS
4090 k = (simd_clone_subparts (arginfo[i].vectype)
4091 / simd_clone_subparts (atype));
0136f8f0
AH
4092 gcc_assert ((k & (k - 1)) == 0);
4093 if (m == 0)
4094 vec_oprnd0
81c40241 4095 = vect_get_vec_def_for_operand (op, stmt);
0136f8f0
AH
4096 else
4097 {
4098 vec_oprnd0 = arginfo[i].op;
4099 if ((m & (k - 1)) == 0)
4100 vec_oprnd0
4101 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
4102 vec_oprnd0);
4103 }
4104 arginfo[i].op = vec_oprnd0;
4105 vec_oprnd0
4106 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
92e29a5e 4107 bitsize_int (prec),
0136f8f0
AH
4108 bitsize_int ((m & (k - 1)) * prec));
4109 new_stmt
b731b390 4110 = gimple_build_assign (make_ssa_name (atype),
0136f8f0
AH
4111 vec_oprnd0);
4112 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4113 vargs.safe_push (gimple_assign_lhs (new_stmt));
4114 }
4115 else
4116 {
cf1b2ba4
RS
4117 k = (simd_clone_subparts (atype)
4118 / simd_clone_subparts (arginfo[i].vectype));
0136f8f0
AH
4119 gcc_assert ((k & (k - 1)) == 0);
4120 vec<constructor_elt, va_gc> *ctor_elts;
4121 if (k != 1)
4122 vec_alloc (ctor_elts, k);
4123 else
4124 ctor_elts = NULL;
4125 for (l = 0; l < k; l++)
4126 {
4127 if (m == 0 && l == 0)
4128 vec_oprnd0
81c40241 4129 = vect_get_vec_def_for_operand (op, stmt);
0136f8f0
AH
4130 else
4131 vec_oprnd0
4132 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
4133 arginfo[i].op);
4134 arginfo[i].op = vec_oprnd0;
4135 if (k == 1)
4136 break;
4137 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4138 vec_oprnd0);
4139 }
4140 if (k == 1)
4141 vargs.safe_push (vec_oprnd0);
4142 else
4143 {
4144 vec_oprnd0 = build_constructor (atype, ctor_elts);
4145 new_stmt
b731b390 4146 = gimple_build_assign (make_ssa_name (atype),
0136f8f0
AH
4147 vec_oprnd0);
4148 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4149 vargs.safe_push (gimple_assign_lhs (new_stmt));
4150 }
4151 }
4152 }
4153 break;
4154 case SIMD_CLONE_ARG_TYPE_UNIFORM:
4155 vargs.safe_push (op);
4156 break;
4157 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
7adb26f2 4158 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
0136f8f0
AH
4159 if (j == 0)
4160 {
4161 gimple_seq stmts;
4162 arginfo[i].op
4163 = force_gimple_operand (arginfo[i].op, &stmts, true,
4164 NULL_TREE);
4165 if (stmts != NULL)
4166 {
4167 basic_block new_bb;
4168 edge pe = loop_preheader_edge (loop);
4169 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4170 gcc_assert (!new_bb);
4171 }
17b658af
JJ
4172 if (arginfo[i].simd_lane_linear)
4173 {
4174 vargs.safe_push (arginfo[i].op);
4175 break;
4176 }
b731b390 4177 tree phi_res = copy_ssa_name (op);
538dd0b7 4178 gphi *new_phi = create_phi_node (phi_res, loop->header);
4fbeb363 4179 loop_vinfo->add_stmt (new_phi);
0136f8f0
AH
4180 add_phi_arg (new_phi, arginfo[i].op,
4181 loop_preheader_edge (loop), UNKNOWN_LOCATION);
4182 enum tree_code code
4183 = POINTER_TYPE_P (TREE_TYPE (op))
4184 ? POINTER_PLUS_EXPR : PLUS_EXPR;
4185 tree type = POINTER_TYPE_P (TREE_TYPE (op))
4186 ? sizetype : TREE_TYPE (op);
807e902e
KZ
4187 widest_int cst
4188 = wi::mul (bestn->simdclone->args[i].linear_step,
4189 ncopies * nunits);
4190 tree tcst = wide_int_to_tree (type, cst);
b731b390 4191 tree phi_arg = copy_ssa_name (op);
0d0e4a03
JJ
4192 new_stmt
4193 = gimple_build_assign (phi_arg, code, phi_res, tcst);
0136f8f0
AH
4194 gimple_stmt_iterator si = gsi_after_labels (loop->header);
4195 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
4fbeb363 4196 loop_vinfo->add_stmt (new_stmt);
0136f8f0
AH
4197 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
4198 UNKNOWN_LOCATION);
4199 arginfo[i].op = phi_res;
4200 vargs.safe_push (phi_res);
4201 }
4202 else
4203 {
4204 enum tree_code code
4205 = POINTER_TYPE_P (TREE_TYPE (op))
4206 ? POINTER_PLUS_EXPR : PLUS_EXPR;
4207 tree type = POINTER_TYPE_P (TREE_TYPE (op))
4208 ? sizetype : TREE_TYPE (op);
807e902e
KZ
4209 widest_int cst
4210 = wi::mul (bestn->simdclone->args[i].linear_step,
4211 j * nunits);
4212 tree tcst = wide_int_to_tree (type, cst);
b731b390 4213 new_temp = make_ssa_name (TREE_TYPE (op));
0d0e4a03
JJ
4214 new_stmt = gimple_build_assign (new_temp, code,
4215 arginfo[i].op, tcst);
0136f8f0
AH
4216 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4217 vargs.safe_push (new_temp);
4218 }
4219 break;
7adb26f2
JJ
4220 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4221 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
0136f8f0 4222 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
e01d41e5
JJ
4223 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4224 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4225 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
0136f8f0
AH
4226 default:
4227 gcc_unreachable ();
4228 }
4229 }
4230
4231 new_stmt = gimple_build_call_vec (fndecl, vargs);
4232 if (vec_dest)
4233 {
cf1b2ba4 4234 gcc_assert (ratype || simd_clone_subparts (rtype) == nunits);
0136f8f0 4235 if (ratype)
b731b390 4236 new_temp = create_tmp_var (ratype);
cf1b2ba4
RS
4237 else if (simd_clone_subparts (vectype)
4238 == simd_clone_subparts (rtype))
0136f8f0
AH
4239 new_temp = make_ssa_name (vec_dest, new_stmt);
4240 else
4241 new_temp = make_ssa_name (rtype, new_stmt);
4242 gimple_call_set_lhs (new_stmt, new_temp);
4243 }
4244 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4245
4246 if (vec_dest)
4247 {
cf1b2ba4 4248 if (simd_clone_subparts (vectype) < nunits)
0136f8f0
AH
4249 {
4250 unsigned int k, l;
73a699ae
RS
4251 poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
4252 poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
cf1b2ba4 4253 k = nunits / simd_clone_subparts (vectype);
0136f8f0
AH
4254 gcc_assert ((k & (k - 1)) == 0);
4255 for (l = 0; l < k; l++)
4256 {
4257 tree t;
4258 if (ratype)
4259 {
4260 t = build_fold_addr_expr (new_temp);
4261 t = build2 (MEM_REF, vectype, t,
73a699ae 4262 build_int_cst (TREE_TYPE (t), l * bytes));
0136f8f0
AH
4263 }
4264 else
4265 t = build3 (BIT_FIELD_REF, vectype, new_temp,
92e29a5e 4266 bitsize_int (prec), bitsize_int (l * prec));
0136f8f0 4267 new_stmt
b731b390 4268 = gimple_build_assign (make_ssa_name (vectype), t);
0136f8f0
AH
4269 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4270 if (j == 0 && l == 0)
4271 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4272 else
4273 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4274
4275 prev_stmt_info = vinfo_for_stmt (new_stmt);
4276 }
4277
4278 if (ratype)
3ba4ff41 4279 vect_clobber_variable (stmt, gsi, new_temp);
0136f8f0
AH
4280 continue;
4281 }
cf1b2ba4 4282 else if (simd_clone_subparts (vectype) > nunits)
0136f8f0 4283 {
cf1b2ba4
RS
4284 unsigned int k = (simd_clone_subparts (vectype)
4285 / simd_clone_subparts (rtype));
0136f8f0
AH
4286 gcc_assert ((k & (k - 1)) == 0);
4287 if ((j & (k - 1)) == 0)
4288 vec_alloc (ret_ctor_elts, k);
4289 if (ratype)
4290 {
cf1b2ba4 4291 unsigned int m, o = nunits / simd_clone_subparts (rtype);
0136f8f0
AH
4292 for (m = 0; m < o; m++)
4293 {
4294 tree tem = build4 (ARRAY_REF, rtype, new_temp,
4295 size_int (m), NULL_TREE, NULL_TREE);
4296 new_stmt
b731b390 4297 = gimple_build_assign (make_ssa_name (rtype), tem);
0136f8f0
AH
4298 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4299 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
4300 gimple_assign_lhs (new_stmt));
4301 }
3ba4ff41 4302 vect_clobber_variable (stmt, gsi, new_temp);
0136f8f0
AH
4303 }
4304 else
4305 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
4306 if ((j & (k - 1)) != k - 1)
4307 continue;
4308 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
4309 new_stmt
b731b390 4310 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
0136f8f0
AH
4311 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4312
4313 if ((unsigned) j == k - 1)
4314 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4315 else
4316 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4317
4318 prev_stmt_info = vinfo_for_stmt (new_stmt);
4319 continue;
4320 }
4321 else if (ratype)
4322 {
4323 tree t = build_fold_addr_expr (new_temp);
4324 t = build2 (MEM_REF, vectype, t,
4325 build_int_cst (TREE_TYPE (t), 0));
4326 new_stmt
b731b390 4327 = gimple_build_assign (make_ssa_name (vec_dest), t);
0136f8f0 4328 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3ba4ff41 4329 vect_clobber_variable (stmt, gsi, new_temp);
0136f8f0
AH
4330 }
4331 }
4332
4333 if (j == 0)
4334 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4335 else
4336 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4337
4338 prev_stmt_info = vinfo_for_stmt (new_stmt);
4339 }
4340
4341 vargs.release ();
4342
4343 /* The call in STMT might prevent it from being removed in dce.
4344 We however cannot remove it here, due to the way the ssa name
4345 it defines is mapped to the new definition. So just replace
4346 rhs of the statement with something harmless. */
4347
4348 if (slp_node)
4349 return true;
4350
4351 if (scalar_dest)
4352 {
4353 type = TREE_TYPE (scalar_dest);
4354 if (is_pattern_stmt_p (stmt_info))
4355 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
4356 else
4357 lhs = gimple_call_lhs (stmt);
4358 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
4359 }
4360 else
4361 new_stmt = gimple_build_nop ();
4362 set_vinfo_for_stmt (new_stmt, stmt_info);
4363 set_vinfo_for_stmt (stmt, NULL);
4364 STMT_VINFO_STMT (stmt_info) = new_stmt;
2865f32a 4365 gsi_replace (gsi, new_stmt, true);
0136f8f0
AH
4366 unlink_stmt_vdef (stmt);
4367
4368 return true;
4369}
4370
4371
ebfd146a
IR
4372/* Function vect_gen_widened_results_half
4373
4374 Create a vector stmt whose code, type, number of arguments, and result
b8698a0f 4375 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
ff802fa1 4376 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
ebfd146a
IR
4377 In the case that CODE is a CALL_EXPR, this means that a call to DECL
4378 needs to be created (DECL is a function-decl of a target-builtin).
4379 STMT is the original scalar stmt that we are vectorizing. */
4380
355fe088 4381static gimple *
ebfd146a
IR
4382vect_gen_widened_results_half (enum tree_code code,
4383 tree decl,
4384 tree vec_oprnd0, tree vec_oprnd1, int op_type,
4385 tree vec_dest, gimple_stmt_iterator *gsi,
355fe088 4386 gimple *stmt)
b8698a0f 4387{
355fe088 4388 gimple *new_stmt;
b8698a0f
L
4389 tree new_temp;
4390
4391 /* Generate half of the widened result: */
4392 if (code == CALL_EXPR)
4393 {
4394 /* Target specific support */
ebfd146a
IR
4395 if (op_type == binary_op)
4396 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
4397 else
4398 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
4399 new_temp = make_ssa_name (vec_dest, new_stmt);
4400 gimple_call_set_lhs (new_stmt, new_temp);
b8698a0f
L
4401 }
4402 else
ebfd146a 4403 {
b8698a0f
L
4404 /* Generic support */
4405 gcc_assert (op_type == TREE_CODE_LENGTH (code));
ebfd146a
IR
4406 if (op_type != binary_op)
4407 vec_oprnd1 = NULL;
0d0e4a03 4408 new_stmt = gimple_build_assign (vec_dest, code, vec_oprnd0, vec_oprnd1);
ebfd146a
IR
4409 new_temp = make_ssa_name (vec_dest, new_stmt);
4410 gimple_assign_set_lhs (new_stmt, new_temp);
b8698a0f 4411 }
ebfd146a
IR
4412 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4413
ebfd146a
IR
4414 return new_stmt;
4415}
4416
4a00c761
JJ
4417
4418/* Get vectorized definitions for loop-based vectorization. For the first
4419 operand we call vect_get_vec_def_for_operand() (with OPRND containing
4420 scalar operand), and for the rest we get a copy with
4421 vect_get_vec_def_for_stmt_copy() using the previous vector definition
4422 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
4423 The vectors are collected into VEC_OPRNDS. */
4424
4425static void
355fe088 4426vect_get_loop_based_defs (tree *oprnd, gimple *stmt, enum vect_def_type dt,
9771b263 4427 vec<tree> *vec_oprnds, int multi_step_cvt)
4a00c761
JJ
4428{
4429 tree vec_oprnd;
4430
4431 /* Get first vector operand. */
4432 /* All the vector operands except the very first one (that is scalar oprnd)
4433 are stmt copies. */
4434 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
81c40241 4435 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt);
4a00c761
JJ
4436 else
4437 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
4438
9771b263 4439 vec_oprnds->quick_push (vec_oprnd);
4a00c761
JJ
4440
4441 /* Get second vector operand. */
4442 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
9771b263 4443 vec_oprnds->quick_push (vec_oprnd);
4a00c761
JJ
4444
4445 *oprnd = vec_oprnd;
4446
4447 /* For conversion in multiple steps, continue to get operands
4448 recursively. */
4449 if (multi_step_cvt)
4450 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
4451}
4452
4453
4454/* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
4455 For multi-step conversions store the resulting vectors and call the function
4456 recursively. */
4457
4458static void
9771b263 4459vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
355fe088 4460 int multi_step_cvt, gimple *stmt,
9771b263 4461 vec<tree> vec_dsts,
4a00c761
JJ
4462 gimple_stmt_iterator *gsi,
4463 slp_tree slp_node, enum tree_code code,
4464 stmt_vec_info *prev_stmt_info)
4465{
4466 unsigned int i;
4467 tree vop0, vop1, new_tmp, vec_dest;
355fe088 4468 gimple *new_stmt;
4a00c761
JJ
4469 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4470
9771b263 4471 vec_dest = vec_dsts.pop ();
4a00c761 4472
9771b263 4473 for (i = 0; i < vec_oprnds->length (); i += 2)
4a00c761
JJ
4474 {
4475 /* Create demotion operation. */
9771b263
DN
4476 vop0 = (*vec_oprnds)[i];
4477 vop1 = (*vec_oprnds)[i + 1];
0d0e4a03 4478 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
4a00c761
JJ
4479 new_tmp = make_ssa_name (vec_dest, new_stmt);
4480 gimple_assign_set_lhs (new_stmt, new_tmp);
4481 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4482
4483 if (multi_step_cvt)
4484 /* Store the resulting vector for next recursive call. */
9771b263 4485 (*vec_oprnds)[i/2] = new_tmp;
4a00c761
JJ
4486 else
4487 {
4488 /* This is the last step of the conversion sequence. Store the
4489 vectors in SLP_NODE or in vector info of the scalar statement
4490 (or in STMT_VINFO_RELATED_STMT chain). */
4491 if (slp_node)
9771b263 4492 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4a00c761 4493 else
c689ce1e
RB
4494 {
4495 if (!*prev_stmt_info)
4496 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
4497 else
4498 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
4a00c761 4499
c689ce1e
RB
4500 *prev_stmt_info = vinfo_for_stmt (new_stmt);
4501 }
4a00c761
JJ
4502 }
4503 }
4504
4505 /* For multi-step demotion operations we first generate demotion operations
4506 from the source type to the intermediate types, and then combine the
4507 results (stored in VEC_OPRNDS) in demotion operation to the destination
4508 type. */
4509 if (multi_step_cvt)
4510 {
4511 /* At each level of recursion we have half of the operands we had at the
4512 previous level. */
9771b263 4513 vec_oprnds->truncate ((i+1)/2);
4a00c761
JJ
4514 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
4515 stmt, vec_dsts, gsi, slp_node,
4516 VEC_PACK_TRUNC_EXPR,
4517 prev_stmt_info);
4518 }
4519
9771b263 4520 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
4521}
4522
4523
4524/* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
4525 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
4526 the resulting vectors and call the function recursively. */
4527
4528static void
9771b263
DN
4529vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
4530 vec<tree> *vec_oprnds1,
355fe088 4531 gimple *stmt, tree vec_dest,
4a00c761
JJ
4532 gimple_stmt_iterator *gsi,
4533 enum tree_code code1,
4534 enum tree_code code2, tree decl1,
4535 tree decl2, int op_type)
4536{
4537 int i;
4538 tree vop0, vop1, new_tmp1, new_tmp2;
355fe088 4539 gimple *new_stmt1, *new_stmt2;
6e1aa848 4540 vec<tree> vec_tmp = vNULL;
4a00c761 4541
9771b263
DN
4542 vec_tmp.create (vec_oprnds0->length () * 2);
4543 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
4a00c761
JJ
4544 {
4545 if (op_type == binary_op)
9771b263 4546 vop1 = (*vec_oprnds1)[i];
4a00c761
JJ
4547 else
4548 vop1 = NULL_TREE;
4549
4550 /* Generate the two halves of promotion operation. */
4551 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
4552 op_type, vec_dest, gsi, stmt);
4553 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
4554 op_type, vec_dest, gsi, stmt);
4555 if (is_gimple_call (new_stmt1))
4556 {
4557 new_tmp1 = gimple_call_lhs (new_stmt1);
4558 new_tmp2 = gimple_call_lhs (new_stmt2);
4559 }
4560 else
4561 {
4562 new_tmp1 = gimple_assign_lhs (new_stmt1);
4563 new_tmp2 = gimple_assign_lhs (new_stmt2);
4564 }
4565
4566 /* Store the results for the next step. */
9771b263
DN
4567 vec_tmp.quick_push (new_tmp1);
4568 vec_tmp.quick_push (new_tmp2);
4a00c761
JJ
4569 }
4570
689eaba3 4571 vec_oprnds0->release ();
4a00c761
JJ
4572 *vec_oprnds0 = vec_tmp;
4573}
4574
4575
b8698a0f
L
4576/* Check if STMT performs a conversion operation, that can be vectorized.
4577 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4a00c761 4578 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
ebfd146a
IR
4579 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4580
4581static bool
355fe088 4582vectorizable_conversion (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
4583 gimple **vec_stmt, slp_tree slp_node,
4584 stmt_vector_for_cost *cost_vec)
ebfd146a
IR
4585{
4586 tree vec_dest;
4587 tree scalar_dest;
4a00c761 4588 tree op0, op1 = NULL_TREE;
ebfd146a
IR
4589 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
4590 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4591 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4592 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
4a00c761 4593 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
ebfd146a
IR
4594 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
4595 tree new_temp;
ebfd146a 4596 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4fc5ebf1 4597 int ndts = 2;
355fe088 4598 gimple *new_stmt = NULL;
ebfd146a 4599 stmt_vec_info prev_stmt_info;
062d5ccc
RS
4600 poly_uint64 nunits_in;
4601 poly_uint64 nunits_out;
ebfd146a 4602 tree vectype_out, vectype_in;
4a00c761
JJ
4603 int ncopies, i, j;
4604 tree lhs_type, rhs_type;
ebfd146a 4605 enum { NARROW, NONE, WIDEN } modifier;
6e1aa848
DN
4606 vec<tree> vec_oprnds0 = vNULL;
4607 vec<tree> vec_oprnds1 = vNULL;
ebfd146a 4608 tree vop0;
4a00c761 4609 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 4610 vec_info *vinfo = stmt_info->vinfo;
4a00c761 4611 int multi_step_cvt = 0;
6e1aa848 4612 vec<tree> interm_types = vNULL;
4a00c761
JJ
4613 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
4614 int op_type;
4a00c761 4615 unsigned short fltsz;
ebfd146a
IR
4616
4617 /* Is STMT a vectorizable conversion? */
4618
4a00c761 4619 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
4620 return false;
4621
66c16fd9
RB
4622 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4623 && ! vec_stmt)
ebfd146a
IR
4624 return false;
4625
4626 if (!is_gimple_assign (stmt))
4627 return false;
4628
4629 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4630 return false;
4631
4632 code = gimple_assign_rhs_code (stmt);
4a00c761
JJ
4633 if (!CONVERT_EXPR_CODE_P (code)
4634 && code != FIX_TRUNC_EXPR
4635 && code != FLOAT_EXPR
4636 && code != WIDEN_MULT_EXPR
4637 && code != WIDEN_LSHIFT_EXPR)
ebfd146a
IR
4638 return false;
4639
4a00c761
JJ
4640 op_type = TREE_CODE_LENGTH (code);
4641
ebfd146a 4642 /* Check types of lhs and rhs. */
b690cc0f 4643 scalar_dest = gimple_assign_lhs (stmt);
4a00c761 4644 lhs_type = TREE_TYPE (scalar_dest);
b690cc0f
RG
4645 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4646
ebfd146a
IR
4647 op0 = gimple_assign_rhs1 (stmt);
4648 rhs_type = TREE_TYPE (op0);
4a00c761
JJ
4649
4650 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
4651 && !((INTEGRAL_TYPE_P (lhs_type)
4652 && INTEGRAL_TYPE_P (rhs_type))
4653 || (SCALAR_FLOAT_TYPE_P (lhs_type)
4654 && SCALAR_FLOAT_TYPE_P (rhs_type))))
4655 return false;
4656
e6f5c25d
IE
4657 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
4658 && ((INTEGRAL_TYPE_P (lhs_type)
2be65d9e 4659 && !type_has_mode_precision_p (lhs_type))
e6f5c25d 4660 || (INTEGRAL_TYPE_P (rhs_type)
2be65d9e 4661 && !type_has_mode_precision_p (rhs_type))))
4a00c761 4662 {
73fbfcad 4663 if (dump_enabled_p ())
78c60e3d 4664 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942
TJ
4665 "type conversion to/from bit-precision unsupported."
4666 "\n");
4a00c761
JJ
4667 return false;
4668 }
4669
b690cc0f 4670 /* Check the operands of the operation. */
894dd753 4671 if (!vect_is_simple_use (op0, vinfo, &dt[0], &vectype_in))
b690cc0f 4672 {
73fbfcad 4673 if (dump_enabled_p ())
78c60e3d 4674 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4675 "use not simple.\n");
b690cc0f
RG
4676 return false;
4677 }
4a00c761
JJ
4678 if (op_type == binary_op)
4679 {
4680 bool ok;
4681
4682 op1 = gimple_assign_rhs2 (stmt);
4683 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
4684 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
4685 OP1. */
4686 if (CONSTANT_CLASS_P (op0))
894dd753 4687 ok = vect_is_simple_use (op1, vinfo, &dt[1], &vectype_in);
4a00c761 4688 else
894dd753 4689 ok = vect_is_simple_use (op1, vinfo, &dt[1]);
4a00c761
JJ
4690
4691 if (!ok)
4692 {
73fbfcad 4693 if (dump_enabled_p ())
78c60e3d 4694 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4695 "use not simple.\n");
4a00c761
JJ
4696 return false;
4697 }
4698 }
4699
b690cc0f
RG
4700 /* If op0 is an external or constant defs use a vector type of
4701 the same size as the output vector type. */
ebfd146a 4702 if (!vectype_in)
b690cc0f 4703 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
7d8930a0
IR
4704 if (vec_stmt)
4705 gcc_assert (vectype_in);
4706 if (!vectype_in)
4707 {
73fbfcad 4708 if (dump_enabled_p ())
4a00c761 4709 {
78c60e3d
SS
4710 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4711 "no vectype for scalar type ");
4712 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
e645e942 4713 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4a00c761 4714 }
7d8930a0
IR
4715
4716 return false;
4717 }
ebfd146a 4718
e6f5c25d
IE
4719 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
4720 && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
4721 {
4722 if (dump_enabled_p ())
4723 {
4724 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4725 "can't convert between boolean and non "
4726 "boolean vectors");
4727 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
4728 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4729 }
4730
4731 return false;
4732 }
4733
b690cc0f
RG
4734 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
4735 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
062d5ccc 4736 if (known_eq (nunits_out, nunits_in))
ebfd146a 4737 modifier = NONE;
062d5ccc
RS
4738 else if (multiple_p (nunits_out, nunits_in))
4739 modifier = NARROW;
ebfd146a 4740 else
062d5ccc
RS
4741 {
4742 gcc_checking_assert (multiple_p (nunits_in, nunits_out));
4743 modifier = WIDEN;
4744 }
ebfd146a 4745
ff802fa1
IR
4746 /* Multiple types in SLP are handled by creating the appropriate number of
4747 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4748 case of SLP. */
fce57248 4749 if (slp_node)
ebfd146a 4750 ncopies = 1;
4a00c761 4751 else if (modifier == NARROW)
e8f142e2 4752 ncopies = vect_get_num_copies (loop_vinfo, vectype_out);
4a00c761 4753 else
e8f142e2 4754 ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
b8698a0f 4755
ebfd146a
IR
4756 /* Sanity check: make sure that at least one copy of the vectorized stmt
4757 needs to be generated. */
4758 gcc_assert (ncopies >= 1);
4759
16d22000
RS
4760 bool found_mode = false;
4761 scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
4762 scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
4763 opt_scalar_mode rhs_mode_iter;
b397965c 4764
ebfd146a 4765 /* Supportable by target? */
4a00c761 4766 switch (modifier)
ebfd146a 4767 {
4a00c761
JJ
4768 case NONE:
4769 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
4770 return false;
4771 if (supportable_convert_operation (code, vectype_out, vectype_in,
4772 &decl1, &code1))
4773 break;
4774 /* FALLTHRU */
4775 unsupported:
73fbfcad 4776 if (dump_enabled_p ())
78c60e3d 4777 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4778 "conversion not supported by target.\n");
ebfd146a 4779 return false;
ebfd146a 4780
4a00c761
JJ
4781 case WIDEN:
4782 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
a86ec597
RH
4783 &code1, &code2, &multi_step_cvt,
4784 &interm_types))
4a00c761
JJ
4785 {
4786 /* Binary widening operation can only be supported directly by the
4787 architecture. */
4788 gcc_assert (!(multi_step_cvt && op_type == binary_op));
4789 break;
4790 }
4791
4792 if (code != FLOAT_EXPR
b397965c 4793 || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
4a00c761
JJ
4794 goto unsupported;
4795
b397965c 4796 fltsz = GET_MODE_SIZE (lhs_mode);
16d22000 4797 FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
4a00c761 4798 {
16d22000 4799 rhs_mode = rhs_mode_iter.require ();
c94843d2
RS
4800 if (GET_MODE_SIZE (rhs_mode) > fltsz)
4801 break;
4802
4a00c761
JJ
4803 cvt_type
4804 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
4805 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
4806 if (cvt_type == NULL_TREE)
4807 goto unsupported;
4808
4809 if (GET_MODE_SIZE (rhs_mode) == fltsz)
4810 {
4811 if (!supportable_convert_operation (code, vectype_out,
4812 cvt_type, &decl1, &codecvt1))
4813 goto unsupported;
4814 }
4815 else if (!supportable_widening_operation (code, stmt, vectype_out,
a86ec597
RH
4816 cvt_type, &codecvt1,
4817 &codecvt2, &multi_step_cvt,
4a00c761
JJ
4818 &interm_types))
4819 continue;
4820 else
4821 gcc_assert (multi_step_cvt == 0);
4822
4823 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
a86ec597
RH
4824 vectype_in, &code1, &code2,
4825 &multi_step_cvt, &interm_types))
16d22000
RS
4826 {
4827 found_mode = true;
4828 break;
4829 }
4a00c761
JJ
4830 }
4831
16d22000 4832 if (!found_mode)
4a00c761
JJ
4833 goto unsupported;
4834
4835 if (GET_MODE_SIZE (rhs_mode) == fltsz)
4836 codecvt2 = ERROR_MARK;
4837 else
4838 {
4839 multi_step_cvt++;
9771b263 4840 interm_types.safe_push (cvt_type);
4a00c761
JJ
4841 cvt_type = NULL_TREE;
4842 }
4843 break;
4844
4845 case NARROW:
4846 gcc_assert (op_type == unary_op);
4847 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
4848 &code1, &multi_step_cvt,
4849 &interm_types))
4850 break;
4851
4852 if (code != FIX_TRUNC_EXPR
b397965c 4853 || GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
4a00c761
JJ
4854 goto unsupported;
4855
4a00c761
JJ
4856 cvt_type
4857 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
4858 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
4859 if (cvt_type == NULL_TREE)
4860 goto unsupported;
4861 if (!supportable_convert_operation (code, cvt_type, vectype_in,
4862 &decl1, &codecvt1))
4863 goto unsupported;
4864 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
4865 &code1, &multi_step_cvt,
4866 &interm_types))
4867 break;
4868 goto unsupported;
4869
4870 default:
4871 gcc_unreachable ();
ebfd146a
IR
4872 }
4873
4874 if (!vec_stmt) /* transformation not required. */
4875 {
adac3a68 4876 DUMP_VECT_SCOPE ("vectorizable_conversion");
4a00c761 4877 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
8bd37302
BS
4878 {
4879 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
68435eb2
RB
4880 vect_model_simple_cost (stmt_info, ncopies, dt, ndts, slp_node,
4881 cost_vec);
8bd37302 4882 }
4a00c761
JJ
4883 else if (modifier == NARROW)
4884 {
4885 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
68435eb2
RB
4886 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt,
4887 cost_vec);
4a00c761
JJ
4888 }
4889 else
4890 {
4891 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
68435eb2
RB
4892 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt,
4893 cost_vec);
4a00c761 4894 }
9771b263 4895 interm_types.release ();
ebfd146a
IR
4896 return true;
4897 }
4898
67b8dbac 4899 /* Transform. */
73fbfcad 4900 if (dump_enabled_p ())
78c60e3d 4901 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4902 "transform conversion. ncopies = %d.\n", ncopies);
ebfd146a 4903
4a00c761
JJ
4904 if (op_type == binary_op)
4905 {
4906 if (CONSTANT_CLASS_P (op0))
4907 op0 = fold_convert (TREE_TYPE (op1), op0);
4908 else if (CONSTANT_CLASS_P (op1))
4909 op1 = fold_convert (TREE_TYPE (op0), op1);
4910 }
4911
4912 /* In case of multi-step conversion, we first generate conversion operations
4913 to the intermediate types, and then from that types to the final one.
4914 We create vector destinations for the intermediate type (TYPES) received
4915 from supportable_*_operation, and store them in the correct order
4916 for future use in vect_create_vectorized_*_stmts (). */
8c681247 4917 auto_vec<tree> vec_dsts (multi_step_cvt + 1);
82294ec1
JJ
4918 vec_dest = vect_create_destination_var (scalar_dest,
4919 (cvt_type && modifier == WIDEN)
4920 ? cvt_type : vectype_out);
9771b263 4921 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
4922
4923 if (multi_step_cvt)
4924 {
9771b263
DN
4925 for (i = interm_types.length () - 1;
4926 interm_types.iterate (i, &intermediate_type); i--)
4a00c761
JJ
4927 {
4928 vec_dest = vect_create_destination_var (scalar_dest,
4929 intermediate_type);
9771b263 4930 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
4931 }
4932 }
ebfd146a 4933
4a00c761 4934 if (cvt_type)
82294ec1
JJ
4935 vec_dest = vect_create_destination_var (scalar_dest,
4936 modifier == WIDEN
4937 ? vectype_out : cvt_type);
4a00c761
JJ
4938
4939 if (!slp_node)
4940 {
30862efc 4941 if (modifier == WIDEN)
4a00c761 4942 {
c3284718 4943 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
4a00c761 4944 if (op_type == binary_op)
9771b263 4945 vec_oprnds1.create (1);
4a00c761 4946 }
30862efc 4947 else if (modifier == NARROW)
9771b263
DN
4948 vec_oprnds0.create (
4949 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
4a00c761
JJ
4950 }
4951 else if (code == WIDEN_LSHIFT_EXPR)
9771b263 4952 vec_oprnds1.create (slp_node->vec_stmts_size);
ebfd146a 4953
4a00c761 4954 last_oprnd = op0;
ebfd146a
IR
4955 prev_stmt_info = NULL;
4956 switch (modifier)
4957 {
4958 case NONE:
4959 for (j = 0; j < ncopies; j++)
4960 {
ebfd146a 4961 if (j == 0)
306b0c92 4962 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node);
ebfd146a
IR
4963 else
4964 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
4965
9771b263 4966 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
4967 {
4968 /* Arguments are ready, create the new vector stmt. */
4969 if (code1 == CALL_EXPR)
4970 {
4971 new_stmt = gimple_build_call (decl1, 1, vop0);
4972 new_temp = make_ssa_name (vec_dest, new_stmt);
4973 gimple_call_set_lhs (new_stmt, new_temp);
4974 }
4975 else
4976 {
4977 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
0d0e4a03 4978 new_stmt = gimple_build_assign (vec_dest, code1, vop0);
4a00c761
JJ
4979 new_temp = make_ssa_name (vec_dest, new_stmt);
4980 gimple_assign_set_lhs (new_stmt, new_temp);
4981 }
4982
4983 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4984 if (slp_node)
9771b263 4985 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
225ce44b
RB
4986 else
4987 {
4988 if (!prev_stmt_info)
4989 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4990 else
4991 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4992 prev_stmt_info = vinfo_for_stmt (new_stmt);
4993 }
4a00c761 4994 }
ebfd146a
IR
4995 }
4996 break;
4997
4998 case WIDEN:
4999 /* In case the vectorization factor (VF) is bigger than the number
5000 of elements that we can fit in a vectype (nunits), we have to
5001 generate more than one vector stmt - i.e - we need to "unroll"
5002 the vector stmt by a factor VF/nunits. */
5003 for (j = 0; j < ncopies; j++)
5004 {
4a00c761 5005 /* Handle uses. */
ebfd146a 5006 if (j == 0)
4a00c761
JJ
5007 {
5008 if (slp_node)
5009 {
5010 if (code == WIDEN_LSHIFT_EXPR)
5011 {
5012 unsigned int k;
ebfd146a 5013
4a00c761
JJ
5014 vec_oprnd1 = op1;
5015 /* Store vec_oprnd1 for every vector stmt to be created
5016 for SLP_NODE. We check during the analysis that all
5017 the shift arguments are the same. */
5018 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
9771b263 5019 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
5020
5021 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
306b0c92 5022 slp_node);
4a00c761
JJ
5023 }
5024 else
5025 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
306b0c92 5026 &vec_oprnds1, slp_node);
4a00c761
JJ
5027 }
5028 else
5029 {
81c40241 5030 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt);
9771b263 5031 vec_oprnds0.quick_push (vec_oprnd0);
4a00c761
JJ
5032 if (op_type == binary_op)
5033 {
5034 if (code == WIDEN_LSHIFT_EXPR)
5035 vec_oprnd1 = op1;
5036 else
81c40241 5037 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt);
9771b263 5038 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
5039 }
5040 }
5041 }
ebfd146a 5042 else
4a00c761
JJ
5043 {
5044 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
9771b263
DN
5045 vec_oprnds0.truncate (0);
5046 vec_oprnds0.quick_push (vec_oprnd0);
4a00c761
JJ
5047 if (op_type == binary_op)
5048 {
5049 if (code == WIDEN_LSHIFT_EXPR)
5050 vec_oprnd1 = op1;
5051 else
5052 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
5053 vec_oprnd1);
9771b263
DN
5054 vec_oprnds1.truncate (0);
5055 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
5056 }
5057 }
ebfd146a 5058
4a00c761
JJ
5059 /* Arguments are ready. Create the new vector stmts. */
5060 for (i = multi_step_cvt; i >= 0; i--)
5061 {
9771b263 5062 tree this_dest = vec_dsts[i];
4a00c761
JJ
5063 enum tree_code c1 = code1, c2 = code2;
5064 if (i == 0 && codecvt2 != ERROR_MARK)
5065 {
5066 c1 = codecvt1;
5067 c2 = codecvt2;
5068 }
5069 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
5070 &vec_oprnds1,
5071 stmt, this_dest, gsi,
5072 c1, c2, decl1, decl2,
5073 op_type);
5074 }
5075
9771b263 5076 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
5077 {
5078 if (cvt_type)
5079 {
5080 if (codecvt1 == CALL_EXPR)
5081 {
5082 new_stmt = gimple_build_call (decl1, 1, vop0);
5083 new_temp = make_ssa_name (vec_dest, new_stmt);
5084 gimple_call_set_lhs (new_stmt, new_temp);
5085 }
5086 else
5087 {
5088 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
b731b390 5089 new_temp = make_ssa_name (vec_dest);
0d0e4a03
JJ
5090 new_stmt = gimple_build_assign (new_temp, codecvt1,
5091 vop0);
4a00c761
JJ
5092 }
5093
5094 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5095 }
5096 else
5097 new_stmt = SSA_NAME_DEF_STMT (vop0);
5098
5099 if (slp_node)
9771b263 5100 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4a00c761 5101 else
c689ce1e
RB
5102 {
5103 if (!prev_stmt_info)
5104 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
5105 else
5106 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5107 prev_stmt_info = vinfo_for_stmt (new_stmt);
5108 }
4a00c761 5109 }
ebfd146a 5110 }
4a00c761
JJ
5111
5112 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
ebfd146a
IR
5113 break;
5114
5115 case NARROW:
5116 /* In case the vectorization factor (VF) is bigger than the number
5117 of elements that we can fit in a vectype (nunits), we have to
5118 generate more than one vector stmt - i.e - we need to "unroll"
5119 the vector stmt by a factor VF/nunits. */
5120 for (j = 0; j < ncopies; j++)
5121 {
5122 /* Handle uses. */
4a00c761
JJ
5123 if (slp_node)
5124 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
306b0c92 5125 slp_node);
ebfd146a
IR
5126 else
5127 {
9771b263 5128 vec_oprnds0.truncate (0);
4a00c761
JJ
5129 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
5130 vect_pow2 (multi_step_cvt) - 1);
ebfd146a
IR
5131 }
5132
4a00c761
JJ
5133 /* Arguments are ready. Create the new vector stmts. */
5134 if (cvt_type)
9771b263 5135 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
5136 {
5137 if (codecvt1 == CALL_EXPR)
5138 {
5139 new_stmt = gimple_build_call (decl1, 1, vop0);
5140 new_temp = make_ssa_name (vec_dest, new_stmt);
5141 gimple_call_set_lhs (new_stmt, new_temp);
5142 }
5143 else
5144 {
5145 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
b731b390 5146 new_temp = make_ssa_name (vec_dest);
0d0e4a03
JJ
5147 new_stmt = gimple_build_assign (new_temp, codecvt1,
5148 vop0);
4a00c761 5149 }
ebfd146a 5150
4a00c761 5151 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9771b263 5152 vec_oprnds0[i] = new_temp;
4a00c761 5153 }
ebfd146a 5154
4a00c761
JJ
5155 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
5156 stmt, vec_dsts, gsi,
5157 slp_node, code1,
5158 &prev_stmt_info);
ebfd146a
IR
5159 }
5160
5161 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4a00c761 5162 break;
ebfd146a
IR
5163 }
5164
9771b263
DN
5165 vec_oprnds0.release ();
5166 vec_oprnds1.release ();
9771b263 5167 interm_types.release ();
ebfd146a
IR
5168
5169 return true;
5170}
ff802fa1
IR
5171
5172
ebfd146a
IR
5173/* Function vectorizable_assignment.
5174
b8698a0f
L
5175 Check if STMT performs an assignment (copy) that can be vectorized.
5176 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
5177 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5178 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5179
5180static bool
355fe088 5181vectorizable_assignment (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
5182 gimple **vec_stmt, slp_tree slp_node,
5183 stmt_vector_for_cost *cost_vec)
ebfd146a
IR
5184{
5185 tree vec_dest;
5186 tree scalar_dest;
5187 tree op;
5188 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
ebfd146a
IR
5189 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5190 tree new_temp;
4fc5ebf1
JG
5191 enum vect_def_type dt[1] = {vect_unknown_def_type};
5192 int ndts = 1;
ebfd146a 5193 int ncopies;
f18b55bd 5194 int i, j;
6e1aa848 5195 vec<tree> vec_oprnds = vNULL;
ebfd146a 5196 tree vop;
a70d6342 5197 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 5198 vec_info *vinfo = stmt_info->vinfo;
355fe088 5199 gimple *new_stmt = NULL;
f18b55bd 5200 stmt_vec_info prev_stmt_info = NULL;
fde9c428
RG
5201 enum tree_code code;
5202 tree vectype_in;
ebfd146a 5203
a70d6342 5204 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
5205 return false;
5206
66c16fd9
RB
5207 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5208 && ! vec_stmt)
ebfd146a
IR
5209 return false;
5210
5211 /* Is vectorizable assignment? */
5212 if (!is_gimple_assign (stmt))
5213 return false;
5214
5215 scalar_dest = gimple_assign_lhs (stmt);
5216 if (TREE_CODE (scalar_dest) != SSA_NAME)
5217 return false;
5218
fde9c428 5219 code = gimple_assign_rhs_code (stmt);
ebfd146a 5220 if (gimple_assign_single_p (stmt)
fde9c428
RG
5221 || code == PAREN_EXPR
5222 || CONVERT_EXPR_CODE_P (code))
ebfd146a
IR
5223 op = gimple_assign_rhs1 (stmt);
5224 else
5225 return false;
5226
7b7ec6c5
RG
5227 if (code == VIEW_CONVERT_EXPR)
5228 op = TREE_OPERAND (op, 0);
5229
465c8c19 5230 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
928686b1 5231 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
465c8c19
JJ
5232
5233 /* Multiple types in SLP are handled by creating the appropriate number of
5234 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5235 case of SLP. */
fce57248 5236 if (slp_node)
465c8c19
JJ
5237 ncopies = 1;
5238 else
e8f142e2 5239 ncopies = vect_get_num_copies (loop_vinfo, vectype);
465c8c19
JJ
5240
5241 gcc_assert (ncopies >= 1);
5242
894dd753 5243 if (!vect_is_simple_use (op, vinfo, &dt[0], &vectype_in))
ebfd146a 5244 {
73fbfcad 5245 if (dump_enabled_p ())
78c60e3d 5246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5247 "use not simple.\n");
ebfd146a
IR
5248 return false;
5249 }
5250
fde9c428
RG
5251 /* We can handle NOP_EXPR conversions that do not change the number
5252 of elements or the vector size. */
7b7ec6c5
RG
5253 if ((CONVERT_EXPR_CODE_P (code)
5254 || code == VIEW_CONVERT_EXPR)
fde9c428 5255 && (!vectype_in
928686b1 5256 || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
cf098191
RS
5257 || maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
5258 GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
fde9c428
RG
5259 return false;
5260
7b7b1813
RG
5261 /* We do not handle bit-precision changes. */
5262 if ((CONVERT_EXPR_CODE_P (code)
5263 || code == VIEW_CONVERT_EXPR)
5264 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
2be65d9e
RS
5265 && (!type_has_mode_precision_p (TREE_TYPE (scalar_dest))
5266 || !type_has_mode_precision_p (TREE_TYPE (op)))
7b7b1813
RG
5267 /* But a conversion that does not change the bit-pattern is ok. */
5268 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
5269 > TYPE_PRECISION (TREE_TYPE (op)))
2dab46d5
IE
5270 && TYPE_UNSIGNED (TREE_TYPE (op)))
5271 /* Conversion between boolean types of different sizes is
5272 a simple assignment in case their vectypes are same
5273 boolean vectors. */
5274 && (!VECTOR_BOOLEAN_TYPE_P (vectype)
5275 || !VECTOR_BOOLEAN_TYPE_P (vectype_in)))
7b7b1813 5276 {
73fbfcad 5277 if (dump_enabled_p ())
78c60e3d
SS
5278 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5279 "type conversion to/from bit-precision "
e645e942 5280 "unsupported.\n");
7b7b1813
RG
5281 return false;
5282 }
5283
ebfd146a
IR
5284 if (!vec_stmt) /* transformation not required. */
5285 {
5286 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
adac3a68 5287 DUMP_VECT_SCOPE ("vectorizable_assignment");
68435eb2 5288 vect_model_simple_cost (stmt_info, ncopies, dt, ndts, slp_node, cost_vec);
ebfd146a
IR
5289 return true;
5290 }
5291
67b8dbac 5292 /* Transform. */
73fbfcad 5293 if (dump_enabled_p ())
e645e942 5294 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
ebfd146a
IR
5295
5296 /* Handle def. */
5297 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5298
5299 /* Handle use. */
f18b55bd 5300 for (j = 0; j < ncopies; j++)
ebfd146a 5301 {
f18b55bd
IR
5302 /* Handle uses. */
5303 if (j == 0)
306b0c92 5304 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node);
f18b55bd
IR
5305 else
5306 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
5307
5308 /* Arguments are ready. create the new vector stmt. */
9771b263 5309 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
f18b55bd 5310 {
7b7ec6c5
RG
5311 if (CONVERT_EXPR_CODE_P (code)
5312 || code == VIEW_CONVERT_EXPR)
4a73490d 5313 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
f18b55bd
IR
5314 new_stmt = gimple_build_assign (vec_dest, vop);
5315 new_temp = make_ssa_name (vec_dest, new_stmt);
5316 gimple_assign_set_lhs (new_stmt, new_temp);
5317 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5318 if (slp_node)
9771b263 5319 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
f18b55bd 5320 }
ebfd146a
IR
5321
5322 if (slp_node)
f18b55bd
IR
5323 continue;
5324
5325 if (j == 0)
5326 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5327 else
5328 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5329
5330 prev_stmt_info = vinfo_for_stmt (new_stmt);
5331 }
b8698a0f 5332
9771b263 5333 vec_oprnds.release ();
ebfd146a
IR
5334 return true;
5335}
5336
9dc3f7de 5337
1107f3ae
IR
5338/* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
5339 either as shift by a scalar or by a vector. */
5340
5341bool
5342vect_supportable_shift (enum tree_code code, tree scalar_type)
5343{
5344
ef4bddc2 5345 machine_mode vec_mode;
1107f3ae
IR
5346 optab optab;
5347 int icode;
5348 tree vectype;
5349
5350 vectype = get_vectype_for_scalar_type (scalar_type);
5351 if (!vectype)
5352 return false;
5353
5354 optab = optab_for_tree_code (code, vectype, optab_scalar);
5355 if (!optab
5356 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
5357 {
5358 optab = optab_for_tree_code (code, vectype, optab_vector);
5359 if (!optab
5360 || (optab_handler (optab, TYPE_MODE (vectype))
5361 == CODE_FOR_nothing))
5362 return false;
5363 }
5364
5365 vec_mode = TYPE_MODE (vectype);
5366 icode = (int) optab_handler (optab, vec_mode);
5367 if (icode == CODE_FOR_nothing)
5368 return false;
5369
5370 return true;
5371}
5372
5373
9dc3f7de
IR
5374/* Function vectorizable_shift.
5375
5376 Check if STMT performs a shift operation that can be vectorized.
5377 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5378 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5379 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5380
5381static bool
355fe088 5382vectorizable_shift (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
5383 gimple **vec_stmt, slp_tree slp_node,
5384 stmt_vector_for_cost *cost_vec)
9dc3f7de
IR
5385{
5386 tree vec_dest;
5387 tree scalar_dest;
5388 tree op0, op1 = NULL;
5389 tree vec_oprnd1 = NULL_TREE;
5390 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5391 tree vectype;
5392 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5393 enum tree_code code;
ef4bddc2 5394 machine_mode vec_mode;
9dc3f7de
IR
5395 tree new_temp;
5396 optab optab;
5397 int icode;
ef4bddc2 5398 machine_mode optab_op2_mode;
9dc3f7de 5399 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
4fc5ebf1 5400 int ndts = 2;
355fe088 5401 gimple *new_stmt = NULL;
9dc3f7de 5402 stmt_vec_info prev_stmt_info;
928686b1
RS
5403 poly_uint64 nunits_in;
5404 poly_uint64 nunits_out;
9dc3f7de 5405 tree vectype_out;
cede2577 5406 tree op1_vectype;
9dc3f7de
IR
5407 int ncopies;
5408 int j, i;
6e1aa848
DN
5409 vec<tree> vec_oprnds0 = vNULL;
5410 vec<tree> vec_oprnds1 = vNULL;
9dc3f7de
IR
5411 tree vop0, vop1;
5412 unsigned int k;
49eab32e 5413 bool scalar_shift_arg = true;
9dc3f7de 5414 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 5415 vec_info *vinfo = stmt_info->vinfo;
9dc3f7de
IR
5416
5417 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5418 return false;
5419
66c16fd9
RB
5420 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5421 && ! vec_stmt)
9dc3f7de
IR
5422 return false;
5423
5424 /* Is STMT a vectorizable binary/unary operation? */
5425 if (!is_gimple_assign (stmt))
5426 return false;
5427
5428 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
5429 return false;
5430
5431 code = gimple_assign_rhs_code (stmt);
5432
5433 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
5434 || code == RROTATE_EXPR))
5435 return false;
5436
5437 scalar_dest = gimple_assign_lhs (stmt);
5438 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
2be65d9e 5439 if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
7b7b1813 5440 {
73fbfcad 5441 if (dump_enabled_p ())
78c60e3d 5442 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5443 "bit-precision shifts not supported.\n");
7b7b1813
RG
5444 return false;
5445 }
9dc3f7de
IR
5446
5447 op0 = gimple_assign_rhs1 (stmt);
894dd753 5448 if (!vect_is_simple_use (op0, vinfo, &dt[0], &vectype))
9dc3f7de 5449 {
73fbfcad 5450 if (dump_enabled_p ())
78c60e3d 5451 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5452 "use not simple.\n");
9dc3f7de
IR
5453 return false;
5454 }
5455 /* If op0 is an external or constant def use a vector type with
5456 the same size as the output vector type. */
5457 if (!vectype)
5458 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
5459 if (vec_stmt)
5460 gcc_assert (vectype);
5461 if (!vectype)
5462 {
73fbfcad 5463 if (dump_enabled_p ())
78c60e3d 5464 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5465 "no vectype for scalar type\n");
9dc3f7de
IR
5466 return false;
5467 }
5468
5469 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5470 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
928686b1 5471 if (maybe_ne (nunits_out, nunits_in))
9dc3f7de
IR
5472 return false;
5473
5474 op1 = gimple_assign_rhs2 (stmt);
fef96d8e
RS
5475 stmt_vec_info op1_def_stmt_info;
5476 if (!vect_is_simple_use (op1, vinfo, &dt[1], &op1_vectype,
5477 &op1_def_stmt_info))
9dc3f7de 5478 {
73fbfcad 5479 if (dump_enabled_p ())
78c60e3d 5480 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5481 "use not simple.\n");
9dc3f7de
IR
5482 return false;
5483 }
5484
9dc3f7de
IR
5485 /* Multiple types in SLP are handled by creating the appropriate number of
5486 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5487 case of SLP. */
fce57248 5488 if (slp_node)
9dc3f7de
IR
5489 ncopies = 1;
5490 else
e8f142e2 5491 ncopies = vect_get_num_copies (loop_vinfo, vectype);
9dc3f7de
IR
5492
5493 gcc_assert (ncopies >= 1);
5494
5495 /* Determine whether the shift amount is a vector, or scalar. If the
5496 shift/rotate amount is a vector, use the vector/vector shift optabs. */
5497
dbfa87aa
YR
5498 if ((dt[1] == vect_internal_def
5499 || dt[1] == vect_induction_def)
5500 && !slp_node)
49eab32e
JJ
5501 scalar_shift_arg = false;
5502 else if (dt[1] == vect_constant_def
5503 || dt[1] == vect_external_def
5504 || dt[1] == vect_internal_def)
5505 {
5506 /* In SLP, need to check whether the shift count is the same,
5507 in loops if it is a constant or invariant, it is always
5508 a scalar shift. */
5509 if (slp_node)
5510 {
355fe088
TS
5511 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
5512 gimple *slpstmt;
49eab32e 5513
9771b263 5514 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
49eab32e
JJ
5515 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
5516 scalar_shift_arg = false;
5517 }
60d393e8
RB
5518
5519 /* If the shift amount is computed by a pattern stmt we cannot
5520 use the scalar amount directly thus give up and use a vector
5521 shift. */
fef96d8e
RS
5522 if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
5523 scalar_shift_arg = false;
49eab32e
JJ
5524 }
5525 else
5526 {
73fbfcad 5527 if (dump_enabled_p ())
78c60e3d 5528 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5529 "operand mode requires invariant argument.\n");
49eab32e
JJ
5530 return false;
5531 }
5532
9dc3f7de 5533 /* Vector shifted by vector. */
49eab32e 5534 if (!scalar_shift_arg)
9dc3f7de
IR
5535 {
5536 optab = optab_for_tree_code (code, vectype, optab_vector);
73fbfcad 5537 if (dump_enabled_p ())
78c60e3d 5538 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5539 "vector/vector shift/rotate found.\n");
78c60e3d 5540
aa948027
JJ
5541 if (!op1_vectype)
5542 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
5543 if (op1_vectype == NULL_TREE
5544 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
cede2577 5545 {
73fbfcad 5546 if (dump_enabled_p ())
78c60e3d
SS
5547 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5548 "unusable type for last operand in"
e645e942 5549 " vector/vector shift/rotate.\n");
cede2577
JJ
5550 return false;
5551 }
9dc3f7de
IR
5552 }
5553 /* See if the machine has a vector shifted by scalar insn and if not
5554 then see if it has a vector shifted by vector insn. */
49eab32e 5555 else
9dc3f7de
IR
5556 {
5557 optab = optab_for_tree_code (code, vectype, optab_scalar);
5558 if (optab
5559 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
5560 {
73fbfcad 5561 if (dump_enabled_p ())
78c60e3d 5562 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5563 "vector/scalar shift/rotate found.\n");
9dc3f7de
IR
5564 }
5565 else
5566 {
5567 optab = optab_for_tree_code (code, vectype, optab_vector);
5568 if (optab
5569 && (optab_handler (optab, TYPE_MODE (vectype))
5570 != CODE_FOR_nothing))
5571 {
49eab32e
JJ
5572 scalar_shift_arg = false;
5573
73fbfcad 5574 if (dump_enabled_p ())
78c60e3d 5575 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5576 "vector/vector shift/rotate found.\n");
9dc3f7de
IR
5577
5578 /* Unlike the other binary operators, shifts/rotates have
5579 the rhs being int, instead of the same type as the lhs,
5580 so make sure the scalar is the right type if we are
aa948027 5581 dealing with vectors of long long/long/short/char. */
9dc3f7de
IR
5582 if (dt[1] == vect_constant_def)
5583 op1 = fold_convert (TREE_TYPE (vectype), op1);
aa948027
JJ
5584 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
5585 TREE_TYPE (op1)))
5586 {
5587 if (slp_node
5588 && TYPE_MODE (TREE_TYPE (vectype))
5589 != TYPE_MODE (TREE_TYPE (op1)))
5590 {
73fbfcad 5591 if (dump_enabled_p ())
78c60e3d
SS
5592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5593 "unusable type for last operand in"
e645e942 5594 " vector/vector shift/rotate.\n");
21c0a521 5595 return false;
aa948027
JJ
5596 }
5597 if (vec_stmt && !slp_node)
5598 {
5599 op1 = fold_convert (TREE_TYPE (vectype), op1);
5600 op1 = vect_init_vector (stmt, op1,
5601 TREE_TYPE (vectype), NULL);
5602 }
5603 }
9dc3f7de
IR
5604 }
5605 }
5606 }
9dc3f7de
IR
5607
5608 /* Supportable by target? */
5609 if (!optab)
5610 {
73fbfcad 5611 if (dump_enabled_p ())
78c60e3d 5612 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5613 "no optab.\n");
9dc3f7de
IR
5614 return false;
5615 }
5616 vec_mode = TYPE_MODE (vectype);
5617 icode = (int) optab_handler (optab, vec_mode);
5618 if (icode == CODE_FOR_nothing)
5619 {
73fbfcad 5620 if (dump_enabled_p ())
78c60e3d 5621 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5622 "op not supported by target.\n");
9dc3f7de 5623 /* Check only during analysis. */
cf098191 5624 if (maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD)
ca09abcb
RS
5625 || (!vec_stmt
5626 && !vect_worthwhile_without_simd_p (vinfo, code)))
9dc3f7de 5627 return false;
73fbfcad 5628 if (dump_enabled_p ())
e645e942
TJ
5629 dump_printf_loc (MSG_NOTE, vect_location,
5630 "proceeding using word mode.\n");
9dc3f7de
IR
5631 }
5632
5633 /* Worthwhile without SIMD support? Check only during analysis. */
ca09abcb
RS
5634 if (!vec_stmt
5635 && !VECTOR_MODE_P (TYPE_MODE (vectype))
5636 && !vect_worthwhile_without_simd_p (vinfo, code))
9dc3f7de 5637 {
73fbfcad 5638 if (dump_enabled_p ())
78c60e3d 5639 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5640 "not worthwhile without SIMD support.\n");
9dc3f7de
IR
5641 return false;
5642 }
5643
5644 if (!vec_stmt) /* transformation not required. */
5645 {
5646 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
adac3a68 5647 DUMP_VECT_SCOPE ("vectorizable_shift");
68435eb2 5648 vect_model_simple_cost (stmt_info, ncopies, dt, ndts, slp_node, cost_vec);
9dc3f7de
IR
5649 return true;
5650 }
5651
67b8dbac 5652 /* Transform. */
9dc3f7de 5653
73fbfcad 5654 if (dump_enabled_p ())
78c60e3d 5655 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5656 "transform binary/unary operation.\n");
9dc3f7de
IR
5657
5658 /* Handle def. */
5659 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5660
9dc3f7de
IR
5661 prev_stmt_info = NULL;
5662 for (j = 0; j < ncopies; j++)
5663 {
5664 /* Handle uses. */
5665 if (j == 0)
5666 {
5667 if (scalar_shift_arg)
5668 {
5669 /* Vector shl and shr insn patterns can be defined with scalar
5670 operand 2 (shift operand). In this case, use constant or loop
5671 invariant op1 directly, without extending it to vector mode
5672 first. */
5673 optab_op2_mode = insn_data[icode].operand[2].mode;
5674 if (!VECTOR_MODE_P (optab_op2_mode))
5675 {
73fbfcad 5676 if (dump_enabled_p ())
78c60e3d 5677 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5678 "operand 1 using scalar mode.\n");
9dc3f7de 5679 vec_oprnd1 = op1;
8930f723 5680 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
9771b263 5681 vec_oprnds1.quick_push (vec_oprnd1);
9dc3f7de
IR
5682 if (slp_node)
5683 {
5684 /* Store vec_oprnd1 for every vector stmt to be created
5685 for SLP_NODE. We check during the analysis that all
5686 the shift arguments are the same.
5687 TODO: Allow different constants for different vector
5688 stmts generated for an SLP instance. */
5689 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
9771b263 5690 vec_oprnds1.quick_push (vec_oprnd1);
9dc3f7de
IR
5691 }
5692 }
5693 }
5694
5695 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
5696 (a special case for certain kind of vector shifts); otherwise,
5697 operand 1 should be of a vector type (the usual case). */
5698 if (vec_oprnd1)
5699 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
306b0c92 5700 slp_node);
9dc3f7de
IR
5701 else
5702 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
306b0c92 5703 slp_node);
9dc3f7de
IR
5704 }
5705 else
5706 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
5707
5708 /* Arguments are ready. Create the new vector stmt. */
9771b263 5709 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
9dc3f7de 5710 {
9771b263 5711 vop1 = vec_oprnds1[i];
0d0e4a03 5712 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
9dc3f7de
IR
5713 new_temp = make_ssa_name (vec_dest, new_stmt);
5714 gimple_assign_set_lhs (new_stmt, new_temp);
5715 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5716 if (slp_node)
9771b263 5717 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
9dc3f7de
IR
5718 }
5719
5720 if (slp_node)
5721 continue;
5722
5723 if (j == 0)
5724 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5725 else
5726 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5727 prev_stmt_info = vinfo_for_stmt (new_stmt);
5728 }
5729
9771b263
DN
5730 vec_oprnds0.release ();
5731 vec_oprnds1.release ();
9dc3f7de
IR
5732
5733 return true;
5734}
5735
5736
ebfd146a
IR
5737/* Function vectorizable_operation.
5738
16949072
RG
5739 Check if STMT performs a binary, unary or ternary operation that can
5740 be vectorized.
b8698a0f 5741 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
5742 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5743 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5744
5745static bool
355fe088 5746vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
5747 gimple **vec_stmt, slp_tree slp_node,
5748 stmt_vector_for_cost *cost_vec)
ebfd146a 5749{
00f07b86 5750 tree vec_dest;
ebfd146a 5751 tree scalar_dest;
16949072 5752 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
ebfd146a 5753 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
00f07b86 5754 tree vectype;
ebfd146a 5755 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
0eb952ea 5756 enum tree_code code, orig_code;
ef4bddc2 5757 machine_mode vec_mode;
ebfd146a
IR
5758 tree new_temp;
5759 int op_type;
00f07b86 5760 optab optab;
523ba738 5761 bool target_support_p;
16949072
RG
5762 enum vect_def_type dt[3]
5763 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
4fc5ebf1 5764 int ndts = 3;
355fe088 5765 gimple *new_stmt = NULL;
ebfd146a 5766 stmt_vec_info prev_stmt_info;
928686b1
RS
5767 poly_uint64 nunits_in;
5768 poly_uint64 nunits_out;
ebfd146a
IR
5769 tree vectype_out;
5770 int ncopies;
5771 int j, i;
6e1aa848
DN
5772 vec<tree> vec_oprnds0 = vNULL;
5773 vec<tree> vec_oprnds1 = vNULL;
5774 vec<tree> vec_oprnds2 = vNULL;
16949072 5775 tree vop0, vop1, vop2;
a70d6342 5776 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 5777 vec_info *vinfo = stmt_info->vinfo;
a70d6342 5778
a70d6342 5779 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
5780 return false;
5781
66c16fd9
RB
5782 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5783 && ! vec_stmt)
ebfd146a
IR
5784 return false;
5785
5786 /* Is STMT a vectorizable binary/unary operation? */
5787 if (!is_gimple_assign (stmt))
5788 return false;
5789
5790 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
5791 return false;
5792
0eb952ea 5793 orig_code = code = gimple_assign_rhs_code (stmt);
ebfd146a 5794
1af4ebf5
MG
5795 /* For pointer addition and subtraction, we should use the normal
5796 plus and minus for the vector operation. */
ebfd146a
IR
5797 if (code == POINTER_PLUS_EXPR)
5798 code = PLUS_EXPR;
1af4ebf5
MG
5799 if (code == POINTER_DIFF_EXPR)
5800 code = MINUS_EXPR;
ebfd146a
IR
5801
5802 /* Support only unary or binary operations. */
5803 op_type = TREE_CODE_LENGTH (code);
16949072 5804 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
ebfd146a 5805 {
73fbfcad 5806 if (dump_enabled_p ())
78c60e3d 5807 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5808 "num. args = %d (not unary/binary/ternary op).\n",
78c60e3d 5809 op_type);
ebfd146a
IR
5810 return false;
5811 }
5812
b690cc0f
RG
5813 scalar_dest = gimple_assign_lhs (stmt);
5814 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
5815
7b7b1813
RG
5816 /* Most operations cannot handle bit-precision types without extra
5817 truncations. */
045c1278 5818 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
2be65d9e 5819 && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
7b7b1813
RG
5820 /* Exception are bitwise binary operations. */
5821 && code != BIT_IOR_EXPR
5822 && code != BIT_XOR_EXPR
5823 && code != BIT_AND_EXPR)
5824 {
73fbfcad 5825 if (dump_enabled_p ())
78c60e3d 5826 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5827 "bit-precision arithmetic not supported.\n");
7b7b1813
RG
5828 return false;
5829 }
5830
ebfd146a 5831 op0 = gimple_assign_rhs1 (stmt);
894dd753 5832 if (!vect_is_simple_use (op0, vinfo, &dt[0], &vectype))
ebfd146a 5833 {
73fbfcad 5834 if (dump_enabled_p ())
78c60e3d 5835 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5836 "use not simple.\n");
ebfd146a
IR
5837 return false;
5838 }
b690cc0f
RG
5839 /* If op0 is an external or constant def use a vector type with
5840 the same size as the output vector type. */
5841 if (!vectype)
b036c6c5
IE
5842 {
5843 /* For boolean type we cannot determine vectype by
5844 invariant value (don't know whether it is a vector
5845 of booleans or vector of integers). We use output
5846 vectype because operations on boolean don't change
5847 type. */
2568d8a1 5848 if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
b036c6c5 5849 {
2568d8a1 5850 if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
b036c6c5
IE
5851 {
5852 if (dump_enabled_p ())
5853 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5854 "not supported operation on bool value.\n");
5855 return false;
5856 }
5857 vectype = vectype_out;
5858 }
5859 else
5860 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
5861 }
7d8930a0
IR
5862 if (vec_stmt)
5863 gcc_assert (vectype);
5864 if (!vectype)
5865 {
73fbfcad 5866 if (dump_enabled_p ())
7d8930a0 5867 {
78c60e3d
SS
5868 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5869 "no vectype for scalar type ");
5870 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
5871 TREE_TYPE (op0));
e645e942 5872 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
7d8930a0
IR
5873 }
5874
5875 return false;
5876 }
b690cc0f
RG
5877
5878 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5879 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
928686b1 5880 if (maybe_ne (nunits_out, nunits_in))
b690cc0f 5881 return false;
ebfd146a 5882
16949072 5883 if (op_type == binary_op || op_type == ternary_op)
ebfd146a
IR
5884 {
5885 op1 = gimple_assign_rhs2 (stmt);
894dd753 5886 if (!vect_is_simple_use (op1, vinfo, &dt[1]))
ebfd146a 5887 {
73fbfcad 5888 if (dump_enabled_p ())
78c60e3d 5889 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5890 "use not simple.\n");
ebfd146a
IR
5891 return false;
5892 }
5893 }
16949072
RG
5894 if (op_type == ternary_op)
5895 {
5896 op2 = gimple_assign_rhs3 (stmt);
894dd753 5897 if (!vect_is_simple_use (op2, vinfo, &dt[2]))
16949072 5898 {
73fbfcad 5899 if (dump_enabled_p ())
78c60e3d 5900 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5901 "use not simple.\n");
16949072
RG
5902 return false;
5903 }
5904 }
ebfd146a 5905
b690cc0f 5906 /* Multiple types in SLP are handled by creating the appropriate number of
ff802fa1 5907 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
b690cc0f 5908 case of SLP. */
fce57248 5909 if (slp_node)
b690cc0f
RG
5910 ncopies = 1;
5911 else
e8f142e2 5912 ncopies = vect_get_num_copies (loop_vinfo, vectype);
b690cc0f
RG
5913
5914 gcc_assert (ncopies >= 1);
5915
9dc3f7de 5916 /* Shifts are handled in vectorizable_shift (). */
ebfd146a
IR
5917 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
5918 || code == RROTATE_EXPR)
9dc3f7de 5919 return false;
ebfd146a 5920
ebfd146a 5921 /* Supportable by target? */
00f07b86
RH
5922
5923 vec_mode = TYPE_MODE (vectype);
5924 if (code == MULT_HIGHPART_EXPR)
523ba738 5925 target_support_p = can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype));
00f07b86
RH
5926 else
5927 {
5928 optab = optab_for_tree_code (code, vectype, optab_default);
5929 if (!optab)
5deb57cb 5930 {
73fbfcad 5931 if (dump_enabled_p ())
78c60e3d 5932 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5933 "no optab.\n");
00f07b86 5934 return false;
5deb57cb 5935 }
523ba738
RS
5936 target_support_p = (optab_handler (optab, vec_mode)
5937 != CODE_FOR_nothing);
5deb57cb
JJ
5938 }
5939
523ba738 5940 if (!target_support_p)
ebfd146a 5941 {
73fbfcad 5942 if (dump_enabled_p ())
78c60e3d 5943 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5944 "op not supported by target.\n");
ebfd146a 5945 /* Check only during analysis. */
cf098191 5946 if (maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD)
ca09abcb 5947 || (!vec_stmt && !vect_worthwhile_without_simd_p (vinfo, code)))
ebfd146a 5948 return false;
73fbfcad 5949 if (dump_enabled_p ())
e645e942
TJ
5950 dump_printf_loc (MSG_NOTE, vect_location,
5951 "proceeding using word mode.\n");
383d9c83
IR
5952 }
5953
4a00c761 5954 /* Worthwhile without SIMD support? Check only during analysis. */
5deb57cb
JJ
5955 if (!VECTOR_MODE_P (vec_mode)
5956 && !vec_stmt
ca09abcb 5957 && !vect_worthwhile_without_simd_p (vinfo, code))
7d8930a0 5958 {
73fbfcad 5959 if (dump_enabled_p ())
78c60e3d 5960 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5961 "not worthwhile without SIMD support.\n");
e34842c6 5962 return false;
7d8930a0 5963 }
ebfd146a 5964
ebfd146a
IR
5965 if (!vec_stmt) /* transformation not required. */
5966 {
4a00c761 5967 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
adac3a68 5968 DUMP_VECT_SCOPE ("vectorizable_operation");
68435eb2 5969 vect_model_simple_cost (stmt_info, ncopies, dt, ndts, slp_node, cost_vec);
ebfd146a
IR
5970 return true;
5971 }
5972
67b8dbac 5973 /* Transform. */
ebfd146a 5974
73fbfcad 5975 if (dump_enabled_p ())
78c60e3d 5976 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5977 "transform binary/unary operation.\n");
383d9c83 5978
0eb952ea
JJ
5979 /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
5980 vectors with unsigned elements, but the result is signed. So, we
5981 need to compute the MINUS_EXPR into vectype temporary and
5982 VIEW_CONVERT_EXPR it into the final vectype_out result. */
5983 tree vec_cvt_dest = NULL_TREE;
5984 if (orig_code == POINTER_DIFF_EXPR)
7b76867b
RB
5985 {
5986 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5987 vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
5988 }
5989 /* Handle def. */
5990 else
5991 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
0eb952ea 5992
ebfd146a
IR
5993 /* In case the vectorization factor (VF) is bigger than the number
5994 of elements that we can fit in a vectype (nunits), we have to generate
5995 more than one vector stmt - i.e - we need to "unroll" the
4a00c761
JJ
5996 vector stmt by a factor VF/nunits. In doing so, we record a pointer
5997 from one copy of the vector stmt to the next, in the field
5998 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
5999 stages to find the correct vector defs to be used when vectorizing
6000 stmts that use the defs of the current stmt. The example below
6001 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
6002 we need to create 4 vectorized stmts):
6003
6004 before vectorization:
6005 RELATED_STMT VEC_STMT
6006 S1: x = memref - -
6007 S2: z = x + 1 - -
6008
6009 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
6010 there):
6011 RELATED_STMT VEC_STMT
6012 VS1_0: vx0 = memref0 VS1_1 -
6013 VS1_1: vx1 = memref1 VS1_2 -
6014 VS1_2: vx2 = memref2 VS1_3 -
6015 VS1_3: vx3 = memref3 - -
6016 S1: x = load - VS1_0
6017 S2: z = x + 1 - -
6018
6019 step2: vectorize stmt S2 (done here):
6020 To vectorize stmt S2 we first need to find the relevant vector
6021 def for the first operand 'x'. This is, as usual, obtained from
6022 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
6023 that defines 'x' (S1). This way we find the stmt VS1_0, and the
6024 relevant vector def 'vx0'. Having found 'vx0' we can generate
6025 the vector stmt VS2_0, and as usual, record it in the
6026 STMT_VINFO_VEC_STMT of stmt S2.
6027 When creating the second copy (VS2_1), we obtain the relevant vector
6028 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
6029 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
6030 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
6031 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
6032 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
6033 chain of stmts and pointers:
6034 RELATED_STMT VEC_STMT
6035 VS1_0: vx0 = memref0 VS1_1 -
6036 VS1_1: vx1 = memref1 VS1_2 -
6037 VS1_2: vx2 = memref2 VS1_3 -
6038 VS1_3: vx3 = memref3 - -
6039 S1: x = load - VS1_0
6040 VS2_0: vz0 = vx0 + v1 VS2_1 -
6041 VS2_1: vz1 = vx1 + v1 VS2_2 -
6042 VS2_2: vz2 = vx2 + v1 VS2_3 -
6043 VS2_3: vz3 = vx3 + v1 - -
6044 S2: z = x + 1 - VS2_0 */
ebfd146a
IR
6045
6046 prev_stmt_info = NULL;
6047 for (j = 0; j < ncopies; j++)
6048 {
6049 /* Handle uses. */
6050 if (j == 0)
4a00c761 6051 {
d6476f90 6052 if (op_type == binary_op)
4a00c761 6053 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
306b0c92 6054 slp_node);
d6476f90
RB
6055 else if (op_type == ternary_op)
6056 {
6057 if (slp_node)
6058 {
6059 auto_vec<tree> ops(3);
6060 ops.quick_push (op0);
6061 ops.quick_push (op1);
6062 ops.quick_push (op2);
6063 auto_vec<vec<tree> > vec_defs(3);
6064 vect_get_slp_defs (ops, slp_node, &vec_defs);
6065 vec_oprnds0 = vec_defs[0];
6066 vec_oprnds1 = vec_defs[1];
6067 vec_oprnds2 = vec_defs[2];
6068 }
6069 else
6070 {
6071 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
6072 NULL);
6073 vect_get_vec_defs (op2, NULL_TREE, stmt, &vec_oprnds2, NULL,
6074 NULL);
6075 }
6076 }
4a00c761
JJ
6077 else
6078 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
306b0c92 6079 slp_node);
4a00c761 6080 }
ebfd146a 6081 else
4a00c761
JJ
6082 {
6083 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
6084 if (op_type == ternary_op)
6085 {
9771b263
DN
6086 tree vec_oprnd = vec_oprnds2.pop ();
6087 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
6088 vec_oprnd));
4a00c761
JJ
6089 }
6090 }
6091
6092 /* Arguments are ready. Create the new vector stmt. */
9771b263 6093 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
ebfd146a 6094 {
4a00c761 6095 vop1 = ((op_type == binary_op || op_type == ternary_op)
9771b263 6096 ? vec_oprnds1[i] : NULL_TREE);
4a00c761 6097 vop2 = ((op_type == ternary_op)
9771b263 6098 ? vec_oprnds2[i] : NULL_TREE);
0d0e4a03 6099 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
4a00c761
JJ
6100 new_temp = make_ssa_name (vec_dest, new_stmt);
6101 gimple_assign_set_lhs (new_stmt, new_temp);
6102 vect_finish_stmt_generation (stmt, new_stmt, gsi);
0eb952ea
JJ
6103 if (vec_cvt_dest)
6104 {
6105 new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
6106 new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
6107 new_temp);
6108 new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
6109 gimple_assign_set_lhs (new_stmt, new_temp);
6110 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6111 }
4a00c761 6112 if (slp_node)
9771b263 6113 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
ebfd146a
IR
6114 }
6115
4a00c761
JJ
6116 if (slp_node)
6117 continue;
6118
6119 if (j == 0)
6120 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6121 else
6122 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6123 prev_stmt_info = vinfo_for_stmt (new_stmt);
ebfd146a
IR
6124 }
6125
9771b263
DN
6126 vec_oprnds0.release ();
6127 vec_oprnds1.release ();
6128 vec_oprnds2.release ();
ebfd146a 6129
ebfd146a
IR
6130 return true;
6131}
6132
f702e7d4 6133/* A helper function to ensure data reference DR's base alignment. */
c716e67f
XDL
6134
6135static void
f702e7d4 6136ensure_base_align (struct data_reference *dr)
c716e67f 6137{
ca823c85 6138 if (DR_VECT_AUX (dr)->misalignment == DR_MISALIGNMENT_UNINITIALIZED)
c716e67f
XDL
6139 return;
6140
52639a61 6141 if (DR_VECT_AUX (dr)->base_misaligned)
c716e67f 6142 {
52639a61 6143 tree base_decl = DR_VECT_AUX (dr)->base_decl;
c716e67f 6144
f702e7d4
RS
6145 unsigned int align_base_to = DR_TARGET_ALIGNMENT (dr) * BITS_PER_UNIT;
6146
428f0c67 6147 if (decl_in_symtab_p (base_decl))
f702e7d4 6148 symtab_node::get (base_decl)->increase_alignment (align_base_to);
428f0c67
JH
6149 else
6150 {
f702e7d4 6151 SET_DECL_ALIGN (base_decl, align_base_to);
428f0c67
JH
6152 DECL_USER_ALIGN (base_decl) = 1;
6153 }
52639a61 6154 DR_VECT_AUX (dr)->base_misaligned = false;
c716e67f
XDL
6155 }
6156}
6157
ebfd146a 6158
44fc7854
BE
6159/* Function get_group_alias_ptr_type.
6160
6161 Return the alias type for the group starting at FIRST_STMT. */
6162
6163static tree
6164get_group_alias_ptr_type (gimple *first_stmt)
6165{
6166 struct data_reference *first_dr, *next_dr;
6167 gimple *next_stmt;
6168
6169 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
2c53b149 6170 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (first_stmt));
44fc7854
BE
6171 while (next_stmt)
6172 {
6173 next_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (next_stmt));
6174 if (get_alias_set (DR_REF (first_dr))
6175 != get_alias_set (DR_REF (next_dr)))
6176 {
6177 if (dump_enabled_p ())
6178 dump_printf_loc (MSG_NOTE, vect_location,
6179 "conflicting alias set types.\n");
6180 return ptr_type_node;
6181 }
2c53b149 6182 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
44fc7854
BE
6183 }
6184 return reference_alias_ptr_type (DR_REF (first_dr));
6185}
6186
6187
ebfd146a
IR
6188/* Function vectorizable_store.
6189
b8698a0f
L
6190 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
6191 can be vectorized.
6192 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
6193 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
6194 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6195
6196static bool
355fe088 6197vectorizable_store (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
68435eb2 6198 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
ebfd146a 6199{
ebfd146a
IR
6200 tree data_ref;
6201 tree op;
6202 tree vec_oprnd = NULL_TREE;
6203 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6204 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
272c6793 6205 tree elem_type;
ebfd146a 6206 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
a70d6342 6207 struct loop *loop = NULL;
ef4bddc2 6208 machine_mode vec_mode;
ebfd146a
IR
6209 tree dummy;
6210 enum dr_alignment_support alignment_support_scheme;
929b4411
RS
6211 enum vect_def_type rhs_dt = vect_unknown_def_type;
6212 enum vect_def_type mask_dt = vect_unknown_def_type;
ebfd146a
IR
6213 stmt_vec_info prev_stmt_info = NULL;
6214 tree dataref_ptr = NULL_TREE;
74bf76ed 6215 tree dataref_offset = NULL_TREE;
355fe088 6216 gimple *ptr_incr = NULL;
ebfd146a
IR
6217 int ncopies;
6218 int j;
2de001ee
RS
6219 gimple *next_stmt, *first_stmt;
6220 bool grouped_store;
ebfd146a 6221 unsigned int group_size, i;
6e1aa848
DN
6222 vec<tree> oprnds = vNULL;
6223 vec<tree> result_chain = vNULL;
ebfd146a 6224 bool inv_p;
09dfa495 6225 tree offset = NULL_TREE;
6e1aa848 6226 vec<tree> vec_oprnds = vNULL;
ebfd146a 6227 bool slp = (slp_node != NULL);
ebfd146a 6228 unsigned int vec_num;
a70d6342 6229 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 6230 vec_info *vinfo = stmt_info->vinfo;
272c6793 6231 tree aggr_type;
134c85ca 6232 gather_scatter_info gs_info;
355fe088 6233 gimple *new_stmt;
d9f21f6a 6234 poly_uint64 vf;
2de001ee 6235 vec_load_store_type vls_type;
44fc7854 6236 tree ref_type;
a70d6342 6237
a70d6342 6238 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
6239 return false;
6240
66c16fd9
RB
6241 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6242 && ! vec_stmt)
ebfd146a
IR
6243 return false;
6244
6245 /* Is vectorizable store? */
6246
c3a8f964
RS
6247 tree mask = NULL_TREE, mask_vectype = NULL_TREE;
6248 if (is_gimple_assign (stmt))
6249 {
6250 tree scalar_dest = gimple_assign_lhs (stmt);
6251 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
6252 && is_pattern_stmt_p (stmt_info))
6253 scalar_dest = TREE_OPERAND (scalar_dest, 0);
6254 if (TREE_CODE (scalar_dest) != ARRAY_REF
6255 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
6256 && TREE_CODE (scalar_dest) != INDIRECT_REF
6257 && TREE_CODE (scalar_dest) != COMPONENT_REF
6258 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
6259 && TREE_CODE (scalar_dest) != REALPART_EXPR
6260 && TREE_CODE (scalar_dest) != MEM_REF)
6261 return false;
6262 }
6263 else
6264 {
6265 gcall *call = dyn_cast <gcall *> (stmt);
f307441a
RS
6266 if (!call || !gimple_call_internal_p (call))
6267 return false;
6268
6269 internal_fn ifn = gimple_call_internal_fn (call);
6270 if (!internal_store_fn_p (ifn))
c3a8f964 6271 return false;
ebfd146a 6272
c3a8f964
RS
6273 if (slp_node != NULL)
6274 {
6275 if (dump_enabled_p ())
6276 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6277 "SLP of masked stores not supported.\n");
6278 return false;
6279 }
6280
f307441a
RS
6281 int mask_index = internal_fn_mask_index (ifn);
6282 if (mask_index >= 0)
6283 {
6284 mask = gimple_call_arg (call, mask_index);
929b4411
RS
6285 if (!vect_check_load_store_mask (stmt, mask, &mask_dt,
6286 &mask_vectype))
f307441a
RS
6287 return false;
6288 }
c3a8f964
RS
6289 }
6290
6291 op = vect_get_store_rhs (stmt);
ebfd146a 6292
fce57248
RS
6293 /* Cannot have hybrid store SLP -- that would mean storing to the
6294 same location twice. */
6295 gcc_assert (slp == PURE_SLP_STMT (stmt_info));
6296
f4d09712 6297 tree vectype = STMT_VINFO_VECTYPE (stmt_info), rhs_vectype = NULL_TREE;
4d694b27 6298 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
465c8c19
JJ
6299
6300 if (loop_vinfo)
b17dc4d4
RB
6301 {
6302 loop = LOOP_VINFO_LOOP (loop_vinfo);
6303 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6304 }
6305 else
6306 vf = 1;
465c8c19
JJ
6307
6308 /* Multiple types in SLP are handled by creating the appropriate number of
6309 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6310 case of SLP. */
fce57248 6311 if (slp)
465c8c19
JJ
6312 ncopies = 1;
6313 else
e8f142e2 6314 ncopies = vect_get_num_copies (loop_vinfo, vectype);
465c8c19
JJ
6315
6316 gcc_assert (ncopies >= 1);
6317
6318 /* FORNOW. This restriction should be relaxed. */
6319 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
6320 {
6321 if (dump_enabled_p ())
6322 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6323 "multiple types in nested loop.\n");
6324 return false;
6325 }
6326
929b4411 6327 if (!vect_check_store_rhs (stmt, op, &rhs_dt, &rhs_vectype, &vls_type))
f4d09712
KY
6328 return false;
6329
272c6793 6330 elem_type = TREE_TYPE (vectype);
ebfd146a 6331 vec_mode = TYPE_MODE (vectype);
7b7b1813 6332
ebfd146a
IR
6333 if (!STMT_VINFO_DATA_REF (stmt_info))
6334 return false;
6335
2de001ee 6336 vect_memory_access_type memory_access_type;
7e11fc7f 6337 if (!get_load_store_type (stmt, vectype, slp, mask, vls_type, ncopies,
2de001ee
RS
6338 &memory_access_type, &gs_info))
6339 return false;
3bab6342 6340
c3a8f964
RS
6341 if (mask)
6342 {
7e11fc7f
RS
6343 if (memory_access_type == VMAT_CONTIGUOUS)
6344 {
6345 if (!VECTOR_MODE_P (vec_mode)
6346 || !can_vec_mask_load_store_p (vec_mode,
6347 TYPE_MODE (mask_vectype), false))
6348 return false;
6349 }
f307441a
RS
6350 else if (memory_access_type != VMAT_LOAD_STORE_LANES
6351 && (memory_access_type != VMAT_GATHER_SCATTER || gs_info.decl))
c3a8f964
RS
6352 {
6353 if (dump_enabled_p ())
6354 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6355 "unsupported access type for masked store.\n");
6356 return false;
6357 }
c3a8f964
RS
6358 }
6359 else
6360 {
6361 /* FORNOW. In some cases can vectorize even if data-type not supported
6362 (e.g. - array initialization with 0). */
6363 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
6364 return false;
6365 }
6366
f307441a 6367 grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
b5ec4de7
RS
6368 && memory_access_type != VMAT_GATHER_SCATTER
6369 && (slp || memory_access_type != VMAT_CONTIGUOUS));
7cfb4d93
RS
6370 if (grouped_store)
6371 {
2c53b149 6372 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
7cfb4d93 6373 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
2c53b149 6374 group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
7cfb4d93
RS
6375 }
6376 else
6377 {
6378 first_stmt = stmt;
6379 first_dr = dr;
6380 group_size = vec_num = 1;
6381 }
6382
ebfd146a
IR
6383 if (!vec_stmt) /* transformation not required. */
6384 {
2de001ee 6385 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
7cfb4d93
RS
6386
6387 if (loop_vinfo
6388 && LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo))
6389 check_load_store_masking (loop_vinfo, vectype, vls_type, group_size,
bfaa08b7 6390 memory_access_type, &gs_info);
7cfb4d93 6391
ebfd146a 6392 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
68435eb2
RB
6393 vect_model_store_cost (stmt_info, ncopies, rhs_dt, memory_access_type,
6394 vls_type, slp_node, cost_vec);
ebfd146a
IR
6395 return true;
6396 }
2de001ee 6397 gcc_assert (memory_access_type == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
ebfd146a 6398
67b8dbac 6399 /* Transform. */
ebfd146a 6400
f702e7d4 6401 ensure_base_align (dr);
c716e67f 6402
f307441a 6403 if (memory_access_type == VMAT_GATHER_SCATTER && gs_info.decl)
3bab6342 6404 {
c3a8f964 6405 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE, src;
134c85ca 6406 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
3bab6342
AT
6407 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
6408 tree ptr, mask, var, scale, perm_mask = NULL_TREE;
6409 edge pe = loop_preheader_edge (loop);
6410 gimple_seq seq;
6411 basic_block new_bb;
6412 enum { NARROW, NONE, WIDEN } modifier;
4d694b27
RS
6413 poly_uint64 scatter_off_nunits
6414 = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
3bab6342 6415
4d694b27 6416 if (known_eq (nunits, scatter_off_nunits))
3bab6342 6417 modifier = NONE;
4d694b27 6418 else if (known_eq (nunits * 2, scatter_off_nunits))
3bab6342 6419 {
3bab6342
AT
6420 modifier = WIDEN;
6421
4d694b27
RS
6422 /* Currently gathers and scatters are only supported for
6423 fixed-length vectors. */
6424 unsigned int count = scatter_off_nunits.to_constant ();
6425 vec_perm_builder sel (count, count, 1);
6426 for (i = 0; i < (unsigned int) count; ++i)
6427 sel.quick_push (i | (count / 2));
3bab6342 6428
4d694b27 6429 vec_perm_indices indices (sel, 1, count);
e3342de4
RS
6430 perm_mask = vect_gen_perm_mask_checked (gs_info.offset_vectype,
6431 indices);
3bab6342
AT
6432 gcc_assert (perm_mask != NULL_TREE);
6433 }
4d694b27 6434 else if (known_eq (nunits, scatter_off_nunits * 2))
3bab6342 6435 {
3bab6342
AT
6436 modifier = NARROW;
6437
4d694b27
RS
6438 /* Currently gathers and scatters are only supported for
6439 fixed-length vectors. */
6440 unsigned int count = nunits.to_constant ();
6441 vec_perm_builder sel (count, count, 1);
6442 for (i = 0; i < (unsigned int) count; ++i)
6443 sel.quick_push (i | (count / 2));
3bab6342 6444
4d694b27 6445 vec_perm_indices indices (sel, 2, count);
e3342de4 6446 perm_mask = vect_gen_perm_mask_checked (vectype, indices);
3bab6342
AT
6447 gcc_assert (perm_mask != NULL_TREE);
6448 ncopies *= 2;
6449 }
6450 else
6451 gcc_unreachable ();
6452
134c85ca 6453 rettype = TREE_TYPE (TREE_TYPE (gs_info.decl));
3bab6342
AT
6454 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6455 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6456 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6457 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6458 scaletype = TREE_VALUE (arglist);
6459
6460 gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
6461 && TREE_CODE (rettype) == VOID_TYPE);
6462
134c85ca 6463 ptr = fold_convert (ptrtype, gs_info.base);
3bab6342
AT
6464 if (!is_gimple_min_invariant (ptr))
6465 {
6466 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
6467 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
6468 gcc_assert (!new_bb);
6469 }
6470
6471 /* Currently we support only unconditional scatter stores,
6472 so mask should be all ones. */
6473 mask = build_int_cst (masktype, -1);
6474 mask = vect_init_vector (stmt, mask, masktype, NULL);
6475
134c85ca 6476 scale = build_int_cst (scaletype, gs_info.scale);
3bab6342
AT
6477
6478 prev_stmt_info = NULL;
6479 for (j = 0; j < ncopies; ++j)
6480 {
6481 if (j == 0)
6482 {
6483 src = vec_oprnd1
c3a8f964 6484 = vect_get_vec_def_for_operand (op, stmt);
3bab6342 6485 op = vec_oprnd0
134c85ca 6486 = vect_get_vec_def_for_operand (gs_info.offset, stmt);
3bab6342
AT
6487 }
6488 else if (modifier != NONE && (j & 1))
6489 {
6490 if (modifier == WIDEN)
6491 {
6492 src = vec_oprnd1
929b4411 6493 = vect_get_vec_def_for_stmt_copy (rhs_dt, vec_oprnd1);
3bab6342
AT
6494 op = permute_vec_elements (vec_oprnd0, vec_oprnd0, perm_mask,
6495 stmt, gsi);
6496 }
6497 else if (modifier == NARROW)
6498 {
6499 src = permute_vec_elements (vec_oprnd1, vec_oprnd1, perm_mask,
6500 stmt, gsi);
6501 op = vec_oprnd0
134c85ca
RS
6502 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
6503 vec_oprnd0);
3bab6342
AT
6504 }
6505 else
6506 gcc_unreachable ();
6507 }
6508 else
6509 {
6510 src = vec_oprnd1
929b4411 6511 = vect_get_vec_def_for_stmt_copy (rhs_dt, vec_oprnd1);
3bab6342 6512 op = vec_oprnd0
134c85ca
RS
6513 = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
6514 vec_oprnd0);
3bab6342
AT
6515 }
6516
6517 if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
6518 {
928686b1
RS
6519 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
6520 TYPE_VECTOR_SUBPARTS (srctype)));
0e22bb5a 6521 var = vect_get_new_ssa_name (srctype, vect_simple_var);
3bab6342
AT
6522 src = build1 (VIEW_CONVERT_EXPR, srctype, src);
6523 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
6524 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6525 src = var;
6526 }
6527
6528 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
6529 {
928686b1
RS
6530 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
6531 TYPE_VECTOR_SUBPARTS (idxtype)));
0e22bb5a 6532 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3bab6342
AT
6533 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
6534 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
6535 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6536 op = var;
6537 }
6538
6539 new_stmt
134c85ca 6540 = gimple_build_call (gs_info.decl, 5, ptr, mask, op, src, scale);
3bab6342
AT
6541
6542 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6543
dbe1b846 6544 if (prev_stmt_info == NULL_STMT_VEC_INFO)
3bab6342
AT
6545 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6546 else
6547 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6548 prev_stmt_info = vinfo_for_stmt (new_stmt);
6549 }
6550 return true;
6551 }
6552
f307441a 6553 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a 6554 {
2c53b149
RB
6555 gimple *group_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
6556 DR_GROUP_STORE_COUNT (vinfo_for_stmt (group_stmt))++;
f307441a 6557 }
ebfd146a 6558
f307441a
RS
6559 if (grouped_store)
6560 {
ebfd146a 6561 /* FORNOW */
a70d6342 6562 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
ebfd146a
IR
6563
6564 /* We vectorize all the stmts of the interleaving group when we
6565 reach the last stmt in the group. */
2c53b149
RB
6566 if (DR_GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
6567 < DR_GROUP_SIZE (vinfo_for_stmt (first_stmt))
ebfd146a
IR
6568 && !slp)
6569 {
6570 *vec_stmt = NULL;
6571 return true;
6572 }
6573
6574 if (slp)
4b5caab7 6575 {
0d0293ac 6576 grouped_store = false;
4b5caab7
IR
6577 /* VEC_NUM is the number of vect stmts to be created for this
6578 group. */
6579 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
9771b263 6580 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2c53b149 6581 gcc_assert (DR_GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt)) == first_stmt);
4b5caab7 6582 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
c3a8f964 6583 op = vect_get_store_rhs (first_stmt);
4b5caab7 6584 }
ebfd146a 6585 else
4b5caab7
IR
6586 /* VEC_NUM is the number of vect stmts to be created for this
6587 group. */
ebfd146a 6588 vec_num = group_size;
44fc7854
BE
6589
6590 ref_type = get_group_alias_ptr_type (first_stmt);
ebfd146a 6591 }
b8698a0f 6592 else
7cfb4d93 6593 ref_type = reference_alias_ptr_type (DR_REF (first_dr));
b8698a0f 6594
73fbfcad 6595 if (dump_enabled_p ())
78c60e3d 6596 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 6597 "transform store. ncopies = %d\n", ncopies);
ebfd146a 6598
2de001ee
RS
6599 if (memory_access_type == VMAT_ELEMENTWISE
6600 || memory_access_type == VMAT_STRIDED_SLP)
f2e2a985
MM
6601 {
6602 gimple_stmt_iterator incr_gsi;
6603 bool insert_after;
355fe088 6604 gimple *incr;
f2e2a985
MM
6605 tree offvar;
6606 tree ivstep;
6607 tree running_off;
f2e2a985
MM
6608 tree stride_base, stride_step, alias_off;
6609 tree vec_oprnd;
f502d50e 6610 unsigned int g;
4d694b27
RS
6611 /* Checked by get_load_store_type. */
6612 unsigned int const_nunits = nunits.to_constant ();
f2e2a985 6613
7cfb4d93 6614 gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
f2e2a985
MM
6615 gcc_assert (!nested_in_vect_loop_p (loop, stmt));
6616
6617 stride_base
6618 = fold_build_pointer_plus
b210f45f 6619 (DR_BASE_ADDRESS (first_dr),
f2e2a985 6620 size_binop (PLUS_EXPR,
b210f45f 6621 convert_to_ptrofftype (DR_OFFSET (first_dr)),
44fc7854 6622 convert_to_ptrofftype (DR_INIT (first_dr))));
b210f45f 6623 stride_step = fold_convert (sizetype, DR_STEP (first_dr));
f2e2a985
MM
6624
6625 /* For a store with loop-invariant (but other than power-of-2)
6626 stride (i.e. not a grouped access) like so:
6627
6628 for (i = 0; i < n; i += stride)
6629 array[i] = ...;
6630
6631 we generate a new induction variable and new stores from
6632 the components of the (vectorized) rhs:
6633
6634 for (j = 0; ; j += VF*stride)
6635 vectemp = ...;
6636 tmp1 = vectemp[0];
6637 array[j] = tmp1;
6638 tmp2 = vectemp[1];
6639 array[j + stride] = tmp2;
6640 ...
6641 */
6642
4d694b27 6643 unsigned nstores = const_nunits;
b17dc4d4 6644 unsigned lnel = 1;
cee62fee 6645 tree ltype = elem_type;
04199738 6646 tree lvectype = vectype;
cee62fee
MM
6647 if (slp)
6648 {
4d694b27
RS
6649 if (group_size < const_nunits
6650 && const_nunits % group_size == 0)
b17dc4d4 6651 {
4d694b27 6652 nstores = const_nunits / group_size;
b17dc4d4
RB
6653 lnel = group_size;
6654 ltype = build_vector_type (elem_type, group_size);
04199738
RB
6655 lvectype = vectype;
6656
6657 /* First check if vec_extract optab doesn't support extraction
6658 of vector elts directly. */
b397965c 6659 scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
9da15d40
RS
6660 machine_mode vmode;
6661 if (!mode_for_vector (elmode, group_size).exists (&vmode)
6662 || !VECTOR_MODE_P (vmode)
414fef4e 6663 || !targetm.vector_mode_supported_p (vmode)
04199738
RB
6664 || (convert_optab_handler (vec_extract_optab,
6665 TYPE_MODE (vectype), vmode)
6666 == CODE_FOR_nothing))
6667 {
6668 /* Try to avoid emitting an extract of vector elements
6669 by performing the extracts using an integer type of the
6670 same size, extracting from a vector of those and then
6671 re-interpreting it as the original vector type if
6672 supported. */
6673 unsigned lsize
6674 = group_size * GET_MODE_BITSIZE (elmode);
fffbab82 6675 elmode = int_mode_for_size (lsize, 0).require ();
4d694b27 6676 unsigned int lnunits = const_nunits / group_size;
04199738
RB
6677 /* If we can't construct such a vector fall back to
6678 element extracts from the original vector type and
6679 element size stores. */
4d694b27 6680 if (mode_for_vector (elmode, lnunits).exists (&vmode)
9da15d40 6681 && VECTOR_MODE_P (vmode)
414fef4e 6682 && targetm.vector_mode_supported_p (vmode)
04199738
RB
6683 && (convert_optab_handler (vec_extract_optab,
6684 vmode, elmode)
6685 != CODE_FOR_nothing))
6686 {
4d694b27 6687 nstores = lnunits;
04199738
RB
6688 lnel = group_size;
6689 ltype = build_nonstandard_integer_type (lsize, 1);
6690 lvectype = build_vector_type (ltype, nstores);
6691 }
6692 /* Else fall back to vector extraction anyway.
6693 Fewer stores are more important than avoiding spilling
6694 of the vector we extract from. Compared to the
6695 construction case in vectorizable_load no store-forwarding
6696 issue exists here for reasonable archs. */
6697 }
b17dc4d4 6698 }
4d694b27
RS
6699 else if (group_size >= const_nunits
6700 && group_size % const_nunits == 0)
b17dc4d4
RB
6701 {
6702 nstores = 1;
4d694b27 6703 lnel = const_nunits;
b17dc4d4 6704 ltype = vectype;
04199738 6705 lvectype = vectype;
b17dc4d4 6706 }
cee62fee
MM
6707 ltype = build_aligned_type (ltype, TYPE_ALIGN (elem_type));
6708 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6709 }
6710
f2e2a985
MM
6711 ivstep = stride_step;
6712 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
b17dc4d4 6713 build_int_cst (TREE_TYPE (ivstep), vf));
f2e2a985
MM
6714
6715 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6716
b210f45f
RB
6717 stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
6718 ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
f2e2a985
MM
6719 create_iv (stride_base, ivstep, NULL,
6720 loop, &incr_gsi, insert_after,
6721 &offvar, NULL);
6722 incr = gsi_stmt (incr_gsi);
4fbeb363 6723 loop_vinfo->add_stmt (incr);
f2e2a985 6724
b210f45f 6725 stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
f2e2a985
MM
6726
6727 prev_stmt_info = NULL;
44fc7854 6728 alias_off = build_int_cst (ref_type, 0);
f502d50e
MM
6729 next_stmt = first_stmt;
6730 for (g = 0; g < group_size; g++)
f2e2a985 6731 {
f502d50e
MM
6732 running_off = offvar;
6733 if (g)
f2e2a985 6734 {
f502d50e
MM
6735 tree size = TYPE_SIZE_UNIT (ltype);
6736 tree pos = fold_build2 (MULT_EXPR, sizetype, size_int (g),
f2e2a985 6737 size);
f502d50e 6738 tree newoff = copy_ssa_name (running_off, NULL);
f2e2a985 6739 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
f502d50e 6740 running_off, pos);
f2e2a985 6741 vect_finish_stmt_generation (stmt, incr, gsi);
f2e2a985 6742 running_off = newoff;
f502d50e 6743 }
b17dc4d4
RB
6744 unsigned int group_el = 0;
6745 unsigned HOST_WIDE_INT
6746 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
f502d50e
MM
6747 for (j = 0; j < ncopies; j++)
6748 {
c3a8f964 6749 /* We've set op and dt above, from vect_get_store_rhs,
f502d50e
MM
6750 and first_stmt == stmt. */
6751 if (j == 0)
6752 {
6753 if (slp)
6754 {
6755 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds, NULL,
306b0c92 6756 slp_node);
f502d50e
MM
6757 vec_oprnd = vec_oprnds[0];
6758 }
6759 else
6760 {
c3a8f964 6761 op = vect_get_store_rhs (next_stmt);
81c40241 6762 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
f502d50e
MM
6763 }
6764 }
f2e2a985 6765 else
f502d50e
MM
6766 {
6767 if (slp)
6768 vec_oprnd = vec_oprnds[j];
6769 else
c079cbac 6770 {
894dd753 6771 vect_is_simple_use (op, vinfo, &rhs_dt);
929b4411
RS
6772 vec_oprnd = vect_get_vec_def_for_stmt_copy (rhs_dt,
6773 vec_oprnd);
c079cbac 6774 }
f502d50e 6775 }
04199738
RB
6776 /* Pun the vector to extract from if necessary. */
6777 if (lvectype != vectype)
6778 {
6779 tree tem = make_ssa_name (lvectype);
6780 gimple *pun
6781 = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
6782 lvectype, vec_oprnd));
6783 vect_finish_stmt_generation (stmt, pun, gsi);
6784 vec_oprnd = tem;
6785 }
f502d50e
MM
6786 for (i = 0; i < nstores; i++)
6787 {
6788 tree newref, newoff;
355fe088 6789 gimple *incr, *assign;
f502d50e
MM
6790 tree size = TYPE_SIZE (ltype);
6791 /* Extract the i'th component. */
6792 tree pos = fold_build2 (MULT_EXPR, bitsizetype,
6793 bitsize_int (i), size);
6794 tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
6795 size, pos);
6796
6797 elem = force_gimple_operand_gsi (gsi, elem, true,
6798 NULL_TREE, true,
6799 GSI_SAME_STMT);
6800
b17dc4d4
RB
6801 tree this_off = build_int_cst (TREE_TYPE (alias_off),
6802 group_el * elsz);
f502d50e 6803 newref = build2 (MEM_REF, ltype,
b17dc4d4 6804 running_off, this_off);
19986382 6805 vect_copy_ref_info (newref, DR_REF (first_dr));
f502d50e
MM
6806
6807 /* And store it to *running_off. */
6808 assign = gimple_build_assign (newref, elem);
6809 vect_finish_stmt_generation (stmt, assign, gsi);
6810
b17dc4d4
RB
6811 group_el += lnel;
6812 if (! slp
6813 || group_el == group_size)
6814 {
6815 newoff = copy_ssa_name (running_off, NULL);
6816 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6817 running_off, stride_step);
6818 vect_finish_stmt_generation (stmt, incr, gsi);
f502d50e 6819
b17dc4d4
RB
6820 running_off = newoff;
6821 group_el = 0;
6822 }
225ce44b
RB
6823 if (g == group_size - 1
6824 && !slp)
f502d50e
MM
6825 {
6826 if (j == 0 && i == 0)
225ce44b
RB
6827 STMT_VINFO_VEC_STMT (stmt_info)
6828 = *vec_stmt = assign;
f502d50e
MM
6829 else
6830 STMT_VINFO_RELATED_STMT (prev_stmt_info) = assign;
6831 prev_stmt_info = vinfo_for_stmt (assign);
6832 }
6833 }
f2e2a985 6834 }
2c53b149 6835 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
b17dc4d4
RB
6836 if (slp)
6837 break;
f2e2a985 6838 }
778dd3b6
RB
6839
6840 vec_oprnds.release ();
f2e2a985
MM
6841 return true;
6842 }
6843
8c681247 6844 auto_vec<tree> dr_chain (group_size);
9771b263 6845 oprnds.create (group_size);
ebfd146a 6846
720f5239 6847 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
ebfd146a 6848 gcc_assert (alignment_support_scheme);
70088b95
RS
6849 vec_loop_masks *loop_masks
6850 = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
6851 ? &LOOP_VINFO_MASKS (loop_vinfo)
6852 : NULL);
272c6793 6853 /* Targets with store-lane instructions must not require explicit
c3a8f964
RS
6854 realignment. vect_supportable_dr_alignment always returns either
6855 dr_aligned or dr_unaligned_supported for masked operations. */
7cfb4d93
RS
6856 gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
6857 && !mask
70088b95 6858 && !loop_masks)
272c6793
RS
6859 || alignment_support_scheme == dr_aligned
6860 || alignment_support_scheme == dr_unaligned_supported);
6861
62da9e14
RS
6862 if (memory_access_type == VMAT_CONTIGUOUS_DOWN
6863 || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
09dfa495
BM
6864 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
6865
f307441a
RS
6866 tree bump;
6867 tree vec_offset = NULL_TREE;
6868 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
6869 {
6870 aggr_type = NULL_TREE;
6871 bump = NULL_TREE;
6872 }
6873 else if (memory_access_type == VMAT_GATHER_SCATTER)
6874 {
6875 aggr_type = elem_type;
6876 vect_get_strided_load_store_ops (stmt, loop_vinfo, &gs_info,
6877 &bump, &vec_offset);
6878 }
272c6793 6879 else
f307441a
RS
6880 {
6881 if (memory_access_type == VMAT_LOAD_STORE_LANES)
6882 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
6883 else
6884 aggr_type = vectype;
6885 bump = vect_get_data_ptr_increment (dr, aggr_type, memory_access_type);
6886 }
ebfd146a 6887
c3a8f964
RS
6888 if (mask)
6889 LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
6890
ebfd146a
IR
6891 /* In case the vectorization factor (VF) is bigger than the number
6892 of elements that we can fit in a vectype (nunits), we have to generate
6893 more than one vector stmt - i.e - we need to "unroll" the
b8698a0f 6894 vector stmt by a factor VF/nunits. For more details see documentation in
ebfd146a
IR
6895 vect_get_vec_def_for_copy_stmt. */
6896
0d0293ac 6897 /* In case of interleaving (non-unit grouped access):
ebfd146a
IR
6898
6899 S1: &base + 2 = x2
6900 S2: &base = x0
6901 S3: &base + 1 = x1
6902 S4: &base + 3 = x3
6903
6904 We create vectorized stores starting from base address (the access of the
6905 first stmt in the chain (S2 in the above example), when the last store stmt
6906 of the chain (S4) is reached:
6907
6908 VS1: &base = vx2
6909 VS2: &base + vec_size*1 = vx0
6910 VS3: &base + vec_size*2 = vx1
6911 VS4: &base + vec_size*3 = vx3
6912
6913 Then permutation statements are generated:
6914
3fcc1b55
JJ
6915 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
6916 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
ebfd146a 6917 ...
b8698a0f 6918
ebfd146a
IR
6919 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6920 (the order of the data-refs in the output of vect_permute_store_chain
6921 corresponds to the order of scalar stmts in the interleaving chain - see
6922 the documentation of vect_permute_store_chain()).
6923
6924 In case of both multiple types and interleaving, above vector stores and
ff802fa1 6925 permutation stmts are created for every copy. The result vector stmts are
ebfd146a 6926 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
b8698a0f 6927 STMT_VINFO_RELATED_STMT for the next copies.
ebfd146a
IR
6928 */
6929
6930 prev_stmt_info = NULL;
c3a8f964 6931 tree vec_mask = NULL_TREE;
ebfd146a
IR
6932 for (j = 0; j < ncopies; j++)
6933 {
ebfd146a
IR
6934
6935 if (j == 0)
6936 {
6937 if (slp)
6938 {
6939 /* Get vectorized arguments for SLP_NODE. */
d092494c 6940 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
306b0c92 6941 NULL, slp_node);
ebfd146a 6942
9771b263 6943 vec_oprnd = vec_oprnds[0];
ebfd146a
IR
6944 }
6945 else
6946 {
b8698a0f
L
6947 /* For interleaved stores we collect vectorized defs for all the
6948 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
6949 used as an input to vect_permute_store_chain(), and OPRNDS as
ebfd146a
IR
6950 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
6951
2c53b149 6952 If the store is not grouped, DR_GROUP_SIZE is 1, and DR_CHAIN and
ebfd146a 6953 OPRNDS are of size 1. */
b8698a0f 6954 next_stmt = first_stmt;
ebfd146a
IR
6955 for (i = 0; i < group_size; i++)
6956 {
b8698a0f 6957 /* Since gaps are not supported for interleaved stores,
2c53b149 6958 DR_GROUP_SIZE is the exact number of stmts in the chain.
b8698a0f 6959 Therefore, NEXT_STMT can't be NULL_TREE. In case that
2c53b149 6960 there is no interleaving, DR_GROUP_SIZE is 1, and only one
ebfd146a 6961 iteration of the loop will be executed. */
c3a8f964 6962 op = vect_get_store_rhs (next_stmt);
81c40241 6963 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
9771b263
DN
6964 dr_chain.quick_push (vec_oprnd);
6965 oprnds.quick_push (vec_oprnd);
2c53b149 6966 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
ebfd146a 6967 }
c3a8f964
RS
6968 if (mask)
6969 vec_mask = vect_get_vec_def_for_operand (mask, stmt,
6970 mask_vectype);
ebfd146a
IR
6971 }
6972
6973 /* We should have catched mismatched types earlier. */
6974 gcc_assert (useless_type_conversion_p (vectype,
6975 TREE_TYPE (vec_oprnd)));
74bf76ed
JJ
6976 bool simd_lane_access_p
6977 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
6978 if (simd_lane_access_p
6979 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
6980 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
6981 && integer_zerop (DR_OFFSET (first_dr))
6982 && integer_zerop (DR_INIT (first_dr))
6983 && alias_sets_conflict_p (get_alias_set (aggr_type),
44fc7854 6984 get_alias_set (TREE_TYPE (ref_type))))
74bf76ed
JJ
6985 {
6986 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
44fc7854 6987 dataref_offset = build_int_cst (ref_type, 0);
8928eff3 6988 inv_p = false;
74bf76ed 6989 }
f307441a
RS
6990 else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
6991 {
6992 vect_get_gather_scatter_ops (loop, stmt, &gs_info,
6993 &dataref_ptr, &vec_offset);
6994 inv_p = false;
6995 }
74bf76ed
JJ
6996 else
6997 dataref_ptr
6998 = vect_create_data_ref_ptr (first_stmt, aggr_type,
6999 simd_lane_access_p ? loop : NULL,
09dfa495 7000 offset, &dummy, gsi, &ptr_incr,
f307441a
RS
7001 simd_lane_access_p, &inv_p,
7002 NULL_TREE, bump);
a70d6342 7003 gcc_assert (bb_vinfo || !inv_p);
ebfd146a 7004 }
b8698a0f 7005 else
ebfd146a 7006 {
b8698a0f
L
7007 /* For interleaved stores we created vectorized defs for all the
7008 defs stored in OPRNDS in the previous iteration (previous copy).
7009 DR_CHAIN is then used as an input to vect_permute_store_chain(),
ebfd146a
IR
7010 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
7011 next copy.
2c53b149 7012 If the store is not grouped, DR_GROUP_SIZE is 1, and DR_CHAIN and
ebfd146a
IR
7013 OPRNDS are of size 1. */
7014 for (i = 0; i < group_size; i++)
7015 {
9771b263 7016 op = oprnds[i];
894dd753 7017 vect_is_simple_use (op, vinfo, &rhs_dt);
929b4411 7018 vec_oprnd = vect_get_vec_def_for_stmt_copy (rhs_dt, op);
9771b263
DN
7019 dr_chain[i] = vec_oprnd;
7020 oprnds[i] = vec_oprnd;
ebfd146a 7021 }
c3a8f964 7022 if (mask)
929b4411 7023 vec_mask = vect_get_vec_def_for_stmt_copy (mask_dt, vec_mask);
74bf76ed
JJ
7024 if (dataref_offset)
7025 dataref_offset
f307441a
RS
7026 = int_const_binop (PLUS_EXPR, dataref_offset, bump);
7027 else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
929b4411
RS
7028 vec_offset = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
7029 vec_offset);
74bf76ed
JJ
7030 else
7031 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
f307441a 7032 bump);
ebfd146a
IR
7033 }
7034
2de001ee 7035 if (memory_access_type == VMAT_LOAD_STORE_LANES)
ebfd146a 7036 {
272c6793 7037 tree vec_array;
267d3070 7038
3ba4ff41 7039 /* Get an array into which we can store the individual vectors. */
272c6793 7040 vec_array = create_vector_array (vectype, vec_num);
3ba4ff41
RS
7041
7042 /* Invalidate the current contents of VEC_ARRAY. This should
7043 become an RTL clobber too, which prevents the vector registers
7044 from being upward-exposed. */
7045 vect_clobber_variable (stmt, gsi, vec_array);
7046
7047 /* Store the individual vectors into the array. */
272c6793 7048 for (i = 0; i < vec_num; i++)
c2d7ab2a 7049 {
9771b263 7050 vec_oprnd = dr_chain[i];
272c6793 7051 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
267d3070 7052 }
b8698a0f 7053
7cfb4d93 7054 tree final_mask = NULL;
70088b95
RS
7055 if (loop_masks)
7056 final_mask = vect_get_loop_mask (gsi, loop_masks, ncopies,
7057 vectype, j);
7cfb4d93
RS
7058 if (vec_mask)
7059 final_mask = prepare_load_store_mask (mask_vectype, final_mask,
7060 vec_mask, gsi);
7061
7e11fc7f 7062 gcall *call;
7cfb4d93 7063 if (final_mask)
7e11fc7f
RS
7064 {
7065 /* Emit:
7066 MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
7067 VEC_ARRAY). */
7068 unsigned int align = TYPE_ALIGN_UNIT (TREE_TYPE (vectype));
7069 tree alias_ptr = build_int_cst (ref_type, align);
7070 call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
7071 dataref_ptr, alias_ptr,
7cfb4d93 7072 final_mask, vec_array);
7e11fc7f
RS
7073 }
7074 else
7075 {
7076 /* Emit:
7077 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
7078 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
7079 call = gimple_build_call_internal (IFN_STORE_LANES, 1,
7080 vec_array);
7081 gimple_call_set_lhs (call, data_ref);
7082 }
a844293d
RS
7083 gimple_call_set_nothrow (call, true);
7084 new_stmt = call;
267d3070 7085 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3ba4ff41
RS
7086
7087 /* Record that VEC_ARRAY is now dead. */
7088 vect_clobber_variable (stmt, gsi, vec_array);
272c6793
RS
7089 }
7090 else
7091 {
7092 new_stmt = NULL;
0d0293ac 7093 if (grouped_store)
272c6793 7094 {
b6b9227d
JJ
7095 if (j == 0)
7096 result_chain.create (group_size);
272c6793
RS
7097 /* Permute. */
7098 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
7099 &result_chain);
7100 }
c2d7ab2a 7101
272c6793
RS
7102 next_stmt = first_stmt;
7103 for (i = 0; i < vec_num; i++)
7104 {
644ffefd 7105 unsigned align, misalign;
272c6793 7106
7cfb4d93 7107 tree final_mask = NULL_TREE;
70088b95
RS
7108 if (loop_masks)
7109 final_mask = vect_get_loop_mask (gsi, loop_masks,
7110 vec_num * ncopies,
7cfb4d93
RS
7111 vectype, vec_num * j + i);
7112 if (vec_mask)
7113 final_mask = prepare_load_store_mask (mask_vectype, final_mask,
7114 vec_mask, gsi);
7115
f307441a
RS
7116 if (memory_access_type == VMAT_GATHER_SCATTER)
7117 {
7118 tree scale = size_int (gs_info.scale);
7119 gcall *call;
70088b95 7120 if (loop_masks)
f307441a
RS
7121 call = gimple_build_call_internal
7122 (IFN_MASK_SCATTER_STORE, 5, dataref_ptr, vec_offset,
7123 scale, vec_oprnd, final_mask);
7124 else
7125 call = gimple_build_call_internal
7126 (IFN_SCATTER_STORE, 4, dataref_ptr, vec_offset,
7127 scale, vec_oprnd);
7128 gimple_call_set_nothrow (call, true);
7129 new_stmt = call;
7130 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7131 break;
7132 }
7133
272c6793
RS
7134 if (i > 0)
7135 /* Bump the vector pointer. */
7136 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
f307441a 7137 stmt, bump);
272c6793
RS
7138
7139 if (slp)
9771b263 7140 vec_oprnd = vec_oprnds[i];
0d0293ac
MM
7141 else if (grouped_store)
7142 /* For grouped stores vectorized defs are interleaved in
272c6793 7143 vect_permute_store_chain(). */
9771b263 7144 vec_oprnd = result_chain[i];
272c6793 7145
f702e7d4 7146 align = DR_TARGET_ALIGNMENT (first_dr);
272c6793 7147 if (aligned_access_p (first_dr))
644ffefd 7148 misalign = 0;
272c6793
RS
7149 else if (DR_MISALIGNMENT (first_dr) == -1)
7150 {
25f68d90 7151 align = dr_alignment (vect_dr_behavior (first_dr));
52639a61 7152 misalign = 0;
272c6793
RS
7153 }
7154 else
c3a8f964 7155 misalign = DR_MISALIGNMENT (first_dr);
aed93b23
RB
7156 if (dataref_offset == NULL_TREE
7157 && TREE_CODE (dataref_ptr) == SSA_NAME)
74bf76ed
JJ
7158 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
7159 misalign);
c2d7ab2a 7160
62da9e14 7161 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
09dfa495
BM
7162 {
7163 tree perm_mask = perm_mask_for_reverse (vectype);
7164 tree perm_dest
c3a8f964 7165 = vect_create_destination_var (vect_get_store_rhs (stmt),
09dfa495 7166 vectype);
b731b390 7167 tree new_temp = make_ssa_name (perm_dest);
09dfa495
BM
7168
7169 /* Generate the permute statement. */
355fe088 7170 gimple *perm_stmt
0d0e4a03
JJ
7171 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
7172 vec_oprnd, perm_mask);
09dfa495
BM
7173 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
7174
7175 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
7176 vec_oprnd = new_temp;
7177 }
7178
272c6793 7179 /* Arguments are ready. Create the new vector stmt. */
7cfb4d93 7180 if (final_mask)
c3a8f964
RS
7181 {
7182 align = least_bit_hwi (misalign | align);
7183 tree ptr = build_int_cst (ref_type, align);
7184 gcall *call
7185 = gimple_build_call_internal (IFN_MASK_STORE, 4,
7186 dataref_ptr, ptr,
7cfb4d93 7187 final_mask, vec_oprnd);
c3a8f964
RS
7188 gimple_call_set_nothrow (call, true);
7189 new_stmt = call;
7190 }
7191 else
7192 {
7193 data_ref = fold_build2 (MEM_REF, vectype,
7194 dataref_ptr,
7195 dataref_offset
7196 ? dataref_offset
7197 : build_int_cst (ref_type, 0));
7198 if (aligned_access_p (first_dr))
7199 ;
7200 else if (DR_MISALIGNMENT (first_dr) == -1)
7201 TREE_TYPE (data_ref)
7202 = build_aligned_type (TREE_TYPE (data_ref),
7203 align * BITS_PER_UNIT);
7204 else
7205 TREE_TYPE (data_ref)
7206 = build_aligned_type (TREE_TYPE (data_ref),
7207 TYPE_ALIGN (elem_type));
19986382 7208 vect_copy_ref_info (data_ref, DR_REF (first_dr));
c3a8f964
RS
7209 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
7210 }
272c6793 7211 vect_finish_stmt_generation (stmt, new_stmt, gsi);
272c6793
RS
7212
7213 if (slp)
7214 continue;
7215
2c53b149 7216 next_stmt = DR_GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
272c6793
RS
7217 if (!next_stmt)
7218 break;
7219 }
ebfd146a 7220 }
1da0876c
RS
7221 if (!slp)
7222 {
7223 if (j == 0)
7224 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7225 else
7226 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7227 prev_stmt_info = vinfo_for_stmt (new_stmt);
7228 }
ebfd146a
IR
7229 }
7230
9771b263
DN
7231 oprnds.release ();
7232 result_chain.release ();
7233 vec_oprnds.release ();
ebfd146a
IR
7234
7235 return true;
7236}
7237
557be5a8
AL
7238/* Given a vector type VECTYPE, turns permutation SEL into the equivalent
7239 VECTOR_CST mask. No checks are made that the target platform supports the
7ac7e286 7240 mask, so callers may wish to test can_vec_perm_const_p separately, or use
557be5a8 7241 vect_gen_perm_mask_checked. */
a1e53f3f 7242
3fcc1b55 7243tree
4aae3cb3 7244vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
a1e53f3f 7245{
b00cb3bf 7246 tree mask_type;
a1e53f3f 7247
0ecc2b7d
RS
7248 poly_uint64 nunits = sel.length ();
7249 gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
b00cb3bf
RS
7250
7251 mask_type = build_vector_type (ssizetype, nunits);
736d0f28 7252 return vec_perm_indices_to_tree (mask_type, sel);
a1e53f3f
L
7253}
7254
7ac7e286 7255/* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
cf7aa6a3 7256 i.e. that the target supports the pattern _for arbitrary input vectors_. */
557be5a8
AL
7257
7258tree
4aae3cb3 7259vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
557be5a8 7260{
7ac7e286 7261 gcc_assert (can_vec_perm_const_p (TYPE_MODE (vectype), sel));
557be5a8
AL
7262 return vect_gen_perm_mask_any (vectype, sel);
7263}
7264
aec7ae7d
JJ
7265/* Given a vector variable X and Y, that was generated for the scalar
7266 STMT, generate instructions to permute the vector elements of X and Y
7267 using permutation mask MASK_VEC, insert them at *GSI and return the
7268 permuted vector variable. */
a1e53f3f
L
7269
7270static tree
355fe088 7271permute_vec_elements (tree x, tree y, tree mask_vec, gimple *stmt,
aec7ae7d 7272 gimple_stmt_iterator *gsi)
a1e53f3f
L
7273{
7274 tree vectype = TREE_TYPE (x);
aec7ae7d 7275 tree perm_dest, data_ref;
355fe088 7276 gimple *perm_stmt;
a1e53f3f 7277
7ad429a4
RS
7278 tree scalar_dest = gimple_get_lhs (stmt);
7279 if (TREE_CODE (scalar_dest) == SSA_NAME)
7280 perm_dest = vect_create_destination_var (scalar_dest, vectype);
7281 else
7282 perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
b731b390 7283 data_ref = make_ssa_name (perm_dest);
a1e53f3f
L
7284
7285 /* Generate the permute statement. */
0d0e4a03 7286 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
a1e53f3f
L
7287 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
7288
7289 return data_ref;
7290}
7291
6b916b36
RB
7292/* Hoist the definitions of all SSA uses on STMT out of the loop LOOP,
7293 inserting them on the loops preheader edge. Returns true if we
7294 were successful in doing so (and thus STMT can be moved then),
7295 otherwise returns false. */
7296
7297static bool
355fe088 7298hoist_defs_of_uses (gimple *stmt, struct loop *loop)
6b916b36
RB
7299{
7300 ssa_op_iter i;
7301 tree op;
7302 bool any = false;
7303
7304 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
7305 {
355fe088 7306 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6b916b36
RB
7307 if (!gimple_nop_p (def_stmt)
7308 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
7309 {
7310 /* Make sure we don't need to recurse. While we could do
7311 so in simple cases when there are more complex use webs
7312 we don't have an easy way to preserve stmt order to fulfil
7313 dependencies within them. */
7314 tree op2;
7315 ssa_op_iter i2;
d1417442
JJ
7316 if (gimple_code (def_stmt) == GIMPLE_PHI)
7317 return false;
6b916b36
RB
7318 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
7319 {
355fe088 7320 gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
6b916b36
RB
7321 if (!gimple_nop_p (def_stmt2)
7322 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
7323 return false;
7324 }
7325 any = true;
7326 }
7327 }
7328
7329 if (!any)
7330 return true;
7331
7332 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
7333 {
355fe088 7334 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6b916b36
RB
7335 if (!gimple_nop_p (def_stmt)
7336 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
7337 {
7338 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
7339 gsi_remove (&gsi, false);
7340 gsi_insert_on_edge_immediate (loop_preheader_edge (loop), def_stmt);
7341 }
7342 }
7343
7344 return true;
7345}
7346
ebfd146a
IR
7347/* vectorizable_load.
7348
b8698a0f
L
7349 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
7350 can be vectorized.
7351 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
7352 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
7353 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7354
7355static bool
355fe088 7356vectorizable_load (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
68435eb2
RB
7357 slp_tree slp_node, slp_instance slp_node_instance,
7358 stmt_vector_for_cost *cost_vec)
ebfd146a
IR
7359{
7360 tree scalar_dest;
7361 tree vec_dest = NULL;
7362 tree data_ref = NULL;
7363 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
b8698a0f 7364 stmt_vec_info prev_stmt_info;
ebfd146a 7365 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
a70d6342 7366 struct loop *loop = NULL;
ebfd146a 7367 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
a70d6342 7368 bool nested_in_vect_loop = false;
c716e67f 7369 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
272c6793 7370 tree elem_type;
ebfd146a 7371 tree new_temp;
ef4bddc2 7372 machine_mode mode;
355fe088 7373 gimple *new_stmt = NULL;
ebfd146a
IR
7374 tree dummy;
7375 enum dr_alignment_support alignment_support_scheme;
7376 tree dataref_ptr = NULL_TREE;
74bf76ed 7377 tree dataref_offset = NULL_TREE;
355fe088 7378 gimple *ptr_incr = NULL;
ebfd146a 7379 int ncopies;
4d694b27
RS
7380 int i, j;
7381 unsigned int group_size;
7382 poly_uint64 group_gap_adj;
ebfd146a
IR
7383 tree msq = NULL_TREE, lsq;
7384 tree offset = NULL_TREE;
356bbc4c 7385 tree byte_offset = NULL_TREE;
ebfd146a 7386 tree realignment_token = NULL_TREE;
538dd0b7 7387 gphi *phi = NULL;
6e1aa848 7388 vec<tree> dr_chain = vNULL;
0d0293ac 7389 bool grouped_load = false;
355fe088 7390 gimple *first_stmt;
4f0a0218 7391 gimple *first_stmt_for_drptr = NULL;
ebfd146a
IR
7392 bool inv_p;
7393 bool compute_in_loop = false;
7394 struct loop *at_loop;
7395 int vec_num;
7396 bool slp = (slp_node != NULL);
7397 bool slp_perm = false;
a70d6342 7398 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
d9f21f6a 7399 poly_uint64 vf;
272c6793 7400 tree aggr_type;
134c85ca 7401 gather_scatter_info gs_info;
310213d4 7402 vec_info *vinfo = stmt_info->vinfo;
44fc7854 7403 tree ref_type;
929b4411 7404 enum vect_def_type mask_dt = vect_unknown_def_type;
a70d6342 7405
465c8c19
JJ
7406 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7407 return false;
7408
66c16fd9
RB
7409 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7410 && ! vec_stmt)
465c8c19
JJ
7411 return false;
7412
c3a8f964
RS
7413 tree mask = NULL_TREE, mask_vectype = NULL_TREE;
7414 if (is_gimple_assign (stmt))
7415 {
7416 scalar_dest = gimple_assign_lhs (stmt);
7417 if (TREE_CODE (scalar_dest) != SSA_NAME)
7418 return false;
465c8c19 7419
c3a8f964
RS
7420 tree_code code = gimple_assign_rhs_code (stmt);
7421 if (code != ARRAY_REF
7422 && code != BIT_FIELD_REF
7423 && code != INDIRECT_REF
7424 && code != COMPONENT_REF
7425 && code != IMAGPART_EXPR
7426 && code != REALPART_EXPR
7427 && code != MEM_REF
7428 && TREE_CODE_CLASS (code) != tcc_declaration)
7429 return false;
7430 }
7431 else
7432 {
7433 gcall *call = dyn_cast <gcall *> (stmt);
bfaa08b7
RS
7434 if (!call || !gimple_call_internal_p (call))
7435 return false;
7436
7437 internal_fn ifn = gimple_call_internal_fn (call);
7438 if (!internal_load_fn_p (ifn))
c3a8f964 7439 return false;
465c8c19 7440
c3a8f964
RS
7441 scalar_dest = gimple_call_lhs (call);
7442 if (!scalar_dest)
7443 return false;
7444
7445 if (slp_node != NULL)
7446 {
7447 if (dump_enabled_p ())
7448 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7449 "SLP of masked loads not supported.\n");
7450 return false;
7451 }
7452
bfaa08b7
RS
7453 int mask_index = internal_fn_mask_index (ifn);
7454 if (mask_index >= 0)
7455 {
7456 mask = gimple_call_arg (call, mask_index);
929b4411
RS
7457 if (!vect_check_load_store_mask (stmt, mask, &mask_dt,
7458 &mask_vectype))
bfaa08b7
RS
7459 return false;
7460 }
c3a8f964 7461 }
465c8c19
JJ
7462
7463 if (!STMT_VINFO_DATA_REF (stmt_info))
7464 return false;
7465
7466 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4d694b27 7467 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
465c8c19 7468
a70d6342
IR
7469 if (loop_vinfo)
7470 {
7471 loop = LOOP_VINFO_LOOP (loop_vinfo);
7472 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
7473 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
7474 }
7475 else
3533e503 7476 vf = 1;
ebfd146a
IR
7477
7478 /* Multiple types in SLP are handled by creating the appropriate number of
ff802fa1 7479 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
ebfd146a 7480 case of SLP. */
fce57248 7481 if (slp)
ebfd146a
IR
7482 ncopies = 1;
7483 else
e8f142e2 7484 ncopies = vect_get_num_copies (loop_vinfo, vectype);
ebfd146a
IR
7485
7486 gcc_assert (ncopies >= 1);
7487
7488 /* FORNOW. This restriction should be relaxed. */
7489 if (nested_in_vect_loop && ncopies > 1)
7490 {
73fbfcad 7491 if (dump_enabled_p ())
78c60e3d 7492 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 7493 "multiple types in nested loop.\n");
ebfd146a
IR
7494 return false;
7495 }
7496
f2556b68
RB
7497 /* Invalidate assumptions made by dependence analysis when vectorization
7498 on the unrolled body effectively re-orders stmts. */
7499 if (ncopies > 1
7500 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
d9f21f6a
RS
7501 && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
7502 STMT_VINFO_MIN_NEG_DIST (stmt_info)))
f2556b68
RB
7503 {
7504 if (dump_enabled_p ())
7505 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7506 "cannot perform implicit CSE when unrolling "
7507 "with negative dependence distance\n");
7508 return false;
7509 }
7510
7b7b1813 7511 elem_type = TREE_TYPE (vectype);
947131ba 7512 mode = TYPE_MODE (vectype);
ebfd146a
IR
7513
7514 /* FORNOW. In some cases can vectorize even if data-type not supported
7515 (e.g. - data copies). */
947131ba 7516 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
ebfd146a 7517 {
73fbfcad 7518 if (dump_enabled_p ())
78c60e3d 7519 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 7520 "Aligned load, but unsupported type.\n");
ebfd146a
IR
7521 return false;
7522 }
7523
ebfd146a 7524 /* Check if the load is a part of an interleaving chain. */
0d0293ac 7525 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a 7526 {
0d0293ac 7527 grouped_load = true;
ebfd146a 7528 /* FORNOW */
2de001ee
RS
7529 gcc_assert (!nested_in_vect_loop);
7530 gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
ebfd146a 7531
2c53b149
RB
7532 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
7533 group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
d5f035ea 7534
b1af7da6
RB
7535 if (slp && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
7536 slp_perm = true;
7537
f2556b68
RB
7538 /* Invalidate assumptions made by dependence analysis when vectorization
7539 on the unrolled body effectively re-orders stmts. */
7540 if (!PURE_SLP_STMT (stmt_info)
7541 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
d9f21f6a
RS
7542 && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
7543 STMT_VINFO_MIN_NEG_DIST (stmt_info)))
f2556b68
RB
7544 {
7545 if (dump_enabled_p ())
7546 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7547 "cannot perform implicit CSE when performing "
7548 "group loads with negative dependence distance\n");
7549 return false;
7550 }
96bb56b2
RB
7551
7552 /* Similarly when the stmt is a load that is both part of a SLP
7553 instance and a loop vectorized stmt via the same-dr mechanism
7554 we have to give up. */
2c53b149 7555 if (DR_GROUP_SAME_DR_STMT (stmt_info)
96bb56b2
RB
7556 && (STMT_SLP_TYPE (stmt_info)
7557 != STMT_SLP_TYPE (vinfo_for_stmt
2c53b149 7558 (DR_GROUP_SAME_DR_STMT (stmt_info)))))
96bb56b2
RB
7559 {
7560 if (dump_enabled_p ())
7561 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7562 "conflicting SLP types for CSEd load\n");
7563 return false;
7564 }
ebfd146a 7565 }
7cfb4d93
RS
7566 else
7567 group_size = 1;
ebfd146a 7568
2de001ee 7569 vect_memory_access_type memory_access_type;
7e11fc7f 7570 if (!get_load_store_type (stmt, vectype, slp, mask, VLS_LOAD, ncopies,
2de001ee
RS
7571 &memory_access_type, &gs_info))
7572 return false;
a1e53f3f 7573
c3a8f964
RS
7574 if (mask)
7575 {
7576 if (memory_access_type == VMAT_CONTIGUOUS)
7577 {
7e11fc7f
RS
7578 machine_mode vec_mode = TYPE_MODE (vectype);
7579 if (!VECTOR_MODE_P (vec_mode)
7580 || !can_vec_mask_load_store_p (vec_mode,
c3a8f964
RS
7581 TYPE_MODE (mask_vectype), true))
7582 return false;
7583 }
bfaa08b7 7584 else if (memory_access_type == VMAT_GATHER_SCATTER && gs_info.decl)
c3a8f964
RS
7585 {
7586 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info.decl));
7587 tree masktype
7588 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
7589 if (TREE_CODE (masktype) == INTEGER_TYPE)
7590 {
7591 if (dump_enabled_p ())
7592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7593 "masked gather with integer mask not"
7594 " supported.");
7595 return false;
7596 }
7597 }
bfaa08b7
RS
7598 else if (memory_access_type != VMAT_LOAD_STORE_LANES
7599 && memory_access_type != VMAT_GATHER_SCATTER)
c3a8f964
RS
7600 {
7601 if (dump_enabled_p ())
7602 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7603 "unsupported access type for masked load.\n");
7604 return false;
7605 }
7606 }
7607
ebfd146a
IR
7608 if (!vec_stmt) /* transformation not required. */
7609 {
2de001ee
RS
7610 if (!slp)
7611 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
7cfb4d93
RS
7612
7613 if (loop_vinfo
7614 && LOOP_VINFO_CAN_FULLY_MASK_P (loop_vinfo))
7615 check_load_store_masking (loop_vinfo, vectype, VLS_LOAD, group_size,
bfaa08b7 7616 memory_access_type, &gs_info);
7cfb4d93 7617
ebfd146a 7618 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
68435eb2
RB
7619 vect_model_load_cost (stmt_info, ncopies, memory_access_type,
7620 slp_node_instance, slp_node, cost_vec);
ebfd146a
IR
7621 return true;
7622 }
7623
2de001ee
RS
7624 if (!slp)
7625 gcc_assert (memory_access_type
7626 == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
7627
73fbfcad 7628 if (dump_enabled_p ())
78c60e3d 7629 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 7630 "transform load. ncopies = %d\n", ncopies);
ebfd146a 7631
67b8dbac 7632 /* Transform. */
ebfd146a 7633
f702e7d4 7634 ensure_base_align (dr);
c716e67f 7635
bfaa08b7 7636 if (memory_access_type == VMAT_GATHER_SCATTER && gs_info.decl)
aec7ae7d 7637 {
929b4411
RS
7638 vect_build_gather_load_calls (stmt, gsi, vec_stmt, &gs_info, mask,
7639 mask_dt);
aec7ae7d
JJ
7640 return true;
7641 }
2de001ee
RS
7642
7643 if (memory_access_type == VMAT_ELEMENTWISE
7644 || memory_access_type == VMAT_STRIDED_SLP)
7d75abc8
MM
7645 {
7646 gimple_stmt_iterator incr_gsi;
7647 bool insert_after;
355fe088 7648 gimple *incr;
7d75abc8 7649 tree offvar;
7d75abc8
MM
7650 tree ivstep;
7651 tree running_off;
9771b263 7652 vec<constructor_elt, va_gc> *v = NULL;
14ac6aa2 7653 tree stride_base, stride_step, alias_off;
4d694b27
RS
7654 /* Checked by get_load_store_type. */
7655 unsigned int const_nunits = nunits.to_constant ();
b210f45f 7656 unsigned HOST_WIDE_INT cst_offset = 0;
14ac6aa2 7657
7cfb4d93 7658 gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
14ac6aa2 7659 gcc_assert (!nested_in_vect_loop);
7d75abc8 7660
b210f45f 7661 if (grouped_load)
44fc7854 7662 {
2c53b149 7663 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
44fc7854 7664 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
44fc7854 7665 }
ab313a8c 7666 else
44fc7854
BE
7667 {
7668 first_stmt = stmt;
7669 first_dr = dr;
b210f45f
RB
7670 }
7671 if (slp && grouped_load)
7672 {
2c53b149 7673 group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
b210f45f
RB
7674 ref_type = get_group_alias_ptr_type (first_stmt);
7675 }
7676 else
7677 {
7678 if (grouped_load)
7679 cst_offset
7680 = (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)))
7681 * vect_get_place_in_interleaving_chain (stmt, first_stmt));
44fc7854 7682 group_size = 1;
b210f45f 7683 ref_type = reference_alias_ptr_type (DR_REF (dr));
44fc7854 7684 }
ab313a8c 7685
14ac6aa2
RB
7686 stride_base
7687 = fold_build_pointer_plus
ab313a8c 7688 (DR_BASE_ADDRESS (first_dr),
14ac6aa2 7689 size_binop (PLUS_EXPR,
ab313a8c
RB
7690 convert_to_ptrofftype (DR_OFFSET (first_dr)),
7691 convert_to_ptrofftype (DR_INIT (first_dr))));
7692 stride_step = fold_convert (sizetype, DR_STEP (first_dr));
7d75abc8
MM
7693
7694 /* For a load with loop-invariant (but other than power-of-2)
7695 stride (i.e. not a grouped access) like so:
7696
7697 for (i = 0; i < n; i += stride)
7698 ... = array[i];
7699
7700 we generate a new induction variable and new accesses to
7701 form a new vector (or vectors, depending on ncopies):
7702
7703 for (j = 0; ; j += VF*stride)
7704 tmp1 = array[j];
7705 tmp2 = array[j + stride];
7706 ...
7707 vectemp = {tmp1, tmp2, ...}
7708 */
7709
ab313a8c
RB
7710 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
7711 build_int_cst (TREE_TYPE (stride_step), vf));
7d75abc8
MM
7712
7713 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
7714
b210f45f
RB
7715 stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
7716 ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
7717 create_iv (stride_base, ivstep, NULL,
7d75abc8
MM
7718 loop, &incr_gsi, insert_after,
7719 &offvar, NULL);
7720 incr = gsi_stmt (incr_gsi);
4fbeb363 7721 loop_vinfo->add_stmt (incr);
7d75abc8 7722
b210f45f 7723 stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
7d75abc8
MM
7724
7725 prev_stmt_info = NULL;
7726 running_off = offvar;
44fc7854 7727 alias_off = build_int_cst (ref_type, 0);
4d694b27 7728 int nloads = const_nunits;
e09b4c37 7729 int lnel = 1;
7b5fc413 7730 tree ltype = TREE_TYPE (vectype);
ea60dd34 7731 tree lvectype = vectype;
b266b968 7732 auto_vec<tree> dr_chain;
2de001ee 7733 if (memory_access_type == VMAT_STRIDED_SLP)
7b5fc413 7734 {
4d694b27 7735 if (group_size < const_nunits)
e09b4c37 7736 {
ff03930a
JJ
7737 /* First check if vec_init optab supports construction from
7738 vector elts directly. */
b397965c 7739 scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
9da15d40
RS
7740 machine_mode vmode;
7741 if (mode_for_vector (elmode, group_size).exists (&vmode)
7742 && VECTOR_MODE_P (vmode)
414fef4e 7743 && targetm.vector_mode_supported_p (vmode)
ff03930a
JJ
7744 && (convert_optab_handler (vec_init_optab,
7745 TYPE_MODE (vectype), vmode)
7746 != CODE_FOR_nothing))
ea60dd34 7747 {
4d694b27 7748 nloads = const_nunits / group_size;
ea60dd34 7749 lnel = group_size;
ff03930a
JJ
7750 ltype = build_vector_type (TREE_TYPE (vectype), group_size);
7751 }
7752 else
7753 {
7754 /* Otherwise avoid emitting a constructor of vector elements
7755 by performing the loads using an integer type of the same
7756 size, constructing a vector of those and then
7757 re-interpreting it as the original vector type.
7758 This avoids a huge runtime penalty due to the general
7759 inability to perform store forwarding from smaller stores
7760 to a larger load. */
7761 unsigned lsize
7762 = group_size * TYPE_PRECISION (TREE_TYPE (vectype));
fffbab82 7763 elmode = int_mode_for_size (lsize, 0).require ();
4d694b27 7764 unsigned int lnunits = const_nunits / group_size;
ff03930a
JJ
7765 /* If we can't construct such a vector fall back to
7766 element loads of the original vector type. */
4d694b27 7767 if (mode_for_vector (elmode, lnunits).exists (&vmode)
9da15d40 7768 && VECTOR_MODE_P (vmode)
414fef4e 7769 && targetm.vector_mode_supported_p (vmode)
ff03930a
JJ
7770 && (convert_optab_handler (vec_init_optab, vmode, elmode)
7771 != CODE_FOR_nothing))
7772 {
4d694b27 7773 nloads = lnunits;
ff03930a
JJ
7774 lnel = group_size;
7775 ltype = build_nonstandard_integer_type (lsize, 1);
7776 lvectype = build_vector_type (ltype, nloads);
7777 }
ea60dd34 7778 }
e09b4c37 7779 }
2de001ee 7780 else
e09b4c37 7781 {
ea60dd34 7782 nloads = 1;
4d694b27 7783 lnel = const_nunits;
e09b4c37 7784 ltype = vectype;
e09b4c37 7785 }
2de001ee
RS
7786 ltype = build_aligned_type (ltype, TYPE_ALIGN (TREE_TYPE (vectype)));
7787 }
bb4e4747
BC
7788 /* Load vector(1) scalar_type if it's 1 element-wise vectype. */
7789 else if (nloads == 1)
7790 ltype = vectype;
7791
2de001ee
RS
7792 if (slp)
7793 {
66c16fd9
RB
7794 /* For SLP permutation support we need to load the whole group,
7795 not only the number of vector stmts the permutation result
7796 fits in. */
b266b968 7797 if (slp_perm)
66c16fd9 7798 {
d9f21f6a
RS
7799 /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
7800 variable VF. */
7801 unsigned int const_vf = vf.to_constant ();
4d694b27 7802 ncopies = CEIL (group_size * const_vf, const_nunits);
66c16fd9
RB
7803 dr_chain.create (ncopies);
7804 }
7805 else
7806 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
7b5fc413 7807 }
4d694b27 7808 unsigned int group_el = 0;
e09b4c37
RB
7809 unsigned HOST_WIDE_INT
7810 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
7d75abc8
MM
7811 for (j = 0; j < ncopies; j++)
7812 {
7b5fc413 7813 if (nloads > 1)
e09b4c37
RB
7814 vec_alloc (v, nloads);
7815 for (i = 0; i < nloads; i++)
7b5fc413 7816 {
e09b4c37 7817 tree this_off = build_int_cst (TREE_TYPE (alias_off),
b210f45f 7818 group_el * elsz + cst_offset);
19986382
RB
7819 tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
7820 vect_copy_ref_info (data_ref, DR_REF (first_dr));
7821 new_stmt = gimple_build_assign (make_ssa_name (ltype), data_ref);
e09b4c37
RB
7822 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7823 if (nloads > 1)
7824 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7825 gimple_assign_lhs (new_stmt));
7826
7827 group_el += lnel;
7828 if (! slp
7829 || group_el == group_size)
7b5fc413 7830 {
e09b4c37
RB
7831 tree newoff = copy_ssa_name (running_off);
7832 gimple *incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
7833 running_off, stride_step);
7b5fc413
RB
7834 vect_finish_stmt_generation (stmt, incr, gsi);
7835
7836 running_off = newoff;
e09b4c37 7837 group_el = 0;
7b5fc413 7838 }
7b5fc413 7839 }
e09b4c37 7840 if (nloads > 1)
7d75abc8 7841 {
ea60dd34
RB
7842 tree vec_inv = build_constructor (lvectype, v);
7843 new_temp = vect_init_vector (stmt, vec_inv, lvectype, gsi);
e09b4c37 7844 new_stmt = SSA_NAME_DEF_STMT (new_temp);
ea60dd34
RB
7845 if (lvectype != vectype)
7846 {
7847 new_stmt = gimple_build_assign (make_ssa_name (vectype),
7848 VIEW_CONVERT_EXPR,
7849 build1 (VIEW_CONVERT_EXPR,
7850 vectype, new_temp));
7851 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7852 }
7d75abc8
MM
7853 }
7854
7b5fc413 7855 if (slp)
b266b968 7856 {
b266b968
RB
7857 if (slp_perm)
7858 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
66c16fd9
RB
7859 else
7860 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
b266b968 7861 }
7d75abc8 7862 else
225ce44b
RB
7863 {
7864 if (j == 0)
7865 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7866 else
7867 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7868 prev_stmt_info = vinfo_for_stmt (new_stmt);
7869 }
7d75abc8 7870 }
b266b968 7871 if (slp_perm)
29afecdf
RB
7872 {
7873 unsigned n_perms;
7874 vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
7875 slp_node_instance, false, &n_perms);
7876 }
7d75abc8
MM
7877 return true;
7878 }
aec7ae7d 7879
b5ec4de7
RS
7880 if (memory_access_type == VMAT_GATHER_SCATTER
7881 || (!slp && memory_access_type == VMAT_CONTIGUOUS))
ab2fc782
RS
7882 grouped_load = false;
7883
0d0293ac 7884 if (grouped_load)
ebfd146a 7885 {
2c53b149
RB
7886 first_stmt = DR_GROUP_FIRST_ELEMENT (stmt_info);
7887 group_size = DR_GROUP_SIZE (vinfo_for_stmt (first_stmt));
4f0a0218 7888 /* For SLP vectorization we directly vectorize a subchain
52eab378
RB
7889 without permutation. */
7890 if (slp && ! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
4f0a0218
RB
7891 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
7892 /* For BB vectorization always use the first stmt to base
7893 the data ref pointer on. */
7894 if (bb_vinfo)
7895 first_stmt_for_drptr = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6aa904c4 7896
ebfd146a 7897 /* Check if the chain of loads is already vectorized. */
01d8bf07
RB
7898 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
7899 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
7900 ??? But we can only do so if there is exactly one
7901 as we have no way to get at the rest. Leave the CSE
7902 opportunity alone.
7903 ??? With the group load eventually participating
7904 in multiple different permutations (having multiple
7905 slp nodes which refer to the same group) the CSE
7906 is even wrong code. See PR56270. */
7907 && !slp)
ebfd146a
IR
7908 {
7909 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
7910 return true;
7911 }
7912 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
9b999e8c 7913 group_gap_adj = 0;
ebfd146a
IR
7914
7915 /* VEC_NUM is the number of vect stmts to be created for this group. */
7916 if (slp)
7917 {
0d0293ac 7918 grouped_load = false;
91ff1504
RB
7919 /* For SLP permutation support we need to load the whole group,
7920 not only the number of vector stmts the permutation result
7921 fits in. */
7922 if (slp_perm)
b267968e 7923 {
d9f21f6a
RS
7924 /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
7925 variable VF. */
7926 unsigned int const_vf = vf.to_constant ();
4d694b27
RS
7927 unsigned int const_nunits = nunits.to_constant ();
7928 vec_num = CEIL (group_size * const_vf, const_nunits);
b267968e
RB
7929 group_gap_adj = vf * group_size - nunits * vec_num;
7930 }
91ff1504 7931 else
b267968e
RB
7932 {
7933 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
796bd467
RB
7934 group_gap_adj
7935 = group_size - SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
b267968e 7936 }
a70d6342 7937 }
ebfd146a 7938 else
9b999e8c 7939 vec_num = group_size;
44fc7854
BE
7940
7941 ref_type = get_group_alias_ptr_type (first_stmt);
ebfd146a
IR
7942 }
7943 else
7944 {
7945 first_stmt = stmt;
7946 first_dr = dr;
7947 group_size = vec_num = 1;
9b999e8c 7948 group_gap_adj = 0;
44fc7854 7949 ref_type = reference_alias_ptr_type (DR_REF (first_dr));
ebfd146a
IR
7950 }
7951
720f5239 7952 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
ebfd146a 7953 gcc_assert (alignment_support_scheme);
70088b95
RS
7954 vec_loop_masks *loop_masks
7955 = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7956 ? &LOOP_VINFO_MASKS (loop_vinfo)
7957 : NULL);
7cfb4d93
RS
7958 /* Targets with store-lane instructions must not require explicit
7959 realignment. vect_supportable_dr_alignment always returns either
7960 dr_aligned or dr_unaligned_supported for masked operations. */
7961 gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
7962 && !mask
70088b95 7963 && !loop_masks)
272c6793
RS
7964 || alignment_support_scheme == dr_aligned
7965 || alignment_support_scheme == dr_unaligned_supported);
ebfd146a
IR
7966
7967 /* In case the vectorization factor (VF) is bigger than the number
7968 of elements that we can fit in a vectype (nunits), we have to generate
7969 more than one vector stmt - i.e - we need to "unroll" the
ff802fa1 7970 vector stmt by a factor VF/nunits. In doing so, we record a pointer
ebfd146a 7971 from one copy of the vector stmt to the next, in the field
ff802fa1 7972 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
ebfd146a 7973 stages to find the correct vector defs to be used when vectorizing
ff802fa1
IR
7974 stmts that use the defs of the current stmt. The example below
7975 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
7976 need to create 4 vectorized stmts):
ebfd146a
IR
7977
7978 before vectorization:
7979 RELATED_STMT VEC_STMT
7980 S1: x = memref - -
7981 S2: z = x + 1 - -
7982
7983 step 1: vectorize stmt S1:
7984 We first create the vector stmt VS1_0, and, as usual, record a
7985 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
7986 Next, we create the vector stmt VS1_1, and record a pointer to
7987 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
ff802fa1 7988 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
ebfd146a
IR
7989 stmts and pointers:
7990 RELATED_STMT VEC_STMT
7991 VS1_0: vx0 = memref0 VS1_1 -
7992 VS1_1: vx1 = memref1 VS1_2 -
7993 VS1_2: vx2 = memref2 VS1_3 -
7994 VS1_3: vx3 = memref3 - -
7995 S1: x = load - VS1_0
7996 S2: z = x + 1 - -
7997
b8698a0f
L
7998 See in documentation in vect_get_vec_def_for_stmt_copy for how the
7999 information we recorded in RELATED_STMT field is used to vectorize
ebfd146a
IR
8000 stmt S2. */
8001
0d0293ac 8002 /* In case of interleaving (non-unit grouped access):
ebfd146a
IR
8003
8004 S1: x2 = &base + 2
8005 S2: x0 = &base
8006 S3: x1 = &base + 1
8007 S4: x3 = &base + 3
8008
b8698a0f 8009 Vectorized loads are created in the order of memory accesses
ebfd146a
IR
8010 starting from the access of the first stmt of the chain:
8011
8012 VS1: vx0 = &base
8013 VS2: vx1 = &base + vec_size*1
8014 VS3: vx3 = &base + vec_size*2
8015 VS4: vx4 = &base + vec_size*3
8016
8017 Then permutation statements are generated:
8018
e2c83630
RH
8019 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
8020 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
ebfd146a
IR
8021 ...
8022
8023 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
8024 (the order of the data-refs in the output of vect_permute_load_chain
8025 corresponds to the order of scalar stmts in the interleaving chain - see
8026 the documentation of vect_permute_load_chain()).
8027 The generation of permutation stmts and recording them in
0d0293ac 8028 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
ebfd146a 8029
b8698a0f 8030 In case of both multiple types and interleaving, the vector loads and
ff802fa1
IR
8031 permutation stmts above are created for every copy. The result vector
8032 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
8033 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
ebfd146a
IR
8034
8035 /* If the data reference is aligned (dr_aligned) or potentially unaligned
8036 on a target that supports unaligned accesses (dr_unaligned_supported)
8037 we generate the following code:
8038 p = initial_addr;
8039 indx = 0;
8040 loop {
8041 p = p + indx * vectype_size;
8042 vec_dest = *(p);
8043 indx = indx + 1;
8044 }
8045
8046 Otherwise, the data reference is potentially unaligned on a target that
b8698a0f 8047 does not support unaligned accesses (dr_explicit_realign_optimized) -
ebfd146a
IR
8048 then generate the following code, in which the data in each iteration is
8049 obtained by two vector loads, one from the previous iteration, and one
8050 from the current iteration:
8051 p1 = initial_addr;
8052 msq_init = *(floor(p1))
8053 p2 = initial_addr + VS - 1;
8054 realignment_token = call target_builtin;
8055 indx = 0;
8056 loop {
8057 p2 = p2 + indx * vectype_size
8058 lsq = *(floor(p2))
8059 vec_dest = realign_load (msq, lsq, realignment_token)
8060 indx = indx + 1;
8061 msq = lsq;
8062 } */
8063
8064 /* If the misalignment remains the same throughout the execution of the
8065 loop, we can create the init_addr and permutation mask at the loop
ff802fa1 8066 preheader. Otherwise, it needs to be created inside the loop.
ebfd146a
IR
8067 This can only occur when vectorizing memory accesses in the inner-loop
8068 nested within an outer-loop that is being vectorized. */
8069
d1e4b493 8070 if (nested_in_vect_loop
cf098191
RS
8071 && !multiple_p (DR_STEP_ALIGNMENT (dr),
8072 GET_MODE_SIZE (TYPE_MODE (vectype))))
ebfd146a
IR
8073 {
8074 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
8075 compute_in_loop = true;
8076 }
8077
8078 if ((alignment_support_scheme == dr_explicit_realign_optimized
8079 || alignment_support_scheme == dr_explicit_realign)
59fd17e3 8080 && !compute_in_loop)
ebfd146a
IR
8081 {
8082 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
8083 alignment_support_scheme, NULL_TREE,
8084 &at_loop);
8085 if (alignment_support_scheme == dr_explicit_realign_optimized)
8086 {
538dd0b7 8087 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (msq));
356bbc4c
JJ
8088 byte_offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
8089 size_one_node);
ebfd146a
IR
8090 }
8091 }
8092 else
8093 at_loop = loop;
8094
62da9e14 8095 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
a1e53f3f
L
8096 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
8097
ab2fc782
RS
8098 tree bump;
8099 tree vec_offset = NULL_TREE;
8100 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8101 {
8102 aggr_type = NULL_TREE;
8103 bump = NULL_TREE;
8104 }
8105 else if (memory_access_type == VMAT_GATHER_SCATTER)
8106 {
8107 aggr_type = elem_type;
8108 vect_get_strided_load_store_ops (stmt, loop_vinfo, &gs_info,
8109 &bump, &vec_offset);
8110 }
272c6793 8111 else
ab2fc782
RS
8112 {
8113 if (memory_access_type == VMAT_LOAD_STORE_LANES)
8114 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
8115 else
8116 aggr_type = vectype;
8117 bump = vect_get_data_ptr_increment (dr, aggr_type, memory_access_type);
8118 }
272c6793 8119
c3a8f964 8120 tree vec_mask = NULL_TREE;
ebfd146a 8121 prev_stmt_info = NULL;
4d694b27 8122 poly_uint64 group_elt = 0;
ebfd146a 8123 for (j = 0; j < ncopies; j++)
b8698a0f 8124 {
272c6793 8125 /* 1. Create the vector or array pointer update chain. */
ebfd146a 8126 if (j == 0)
74bf76ed
JJ
8127 {
8128 bool simd_lane_access_p
8129 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
8130 if (simd_lane_access_p
8131 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
8132 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
8133 && integer_zerop (DR_OFFSET (first_dr))
8134 && integer_zerop (DR_INIT (first_dr))
8135 && alias_sets_conflict_p (get_alias_set (aggr_type),
44fc7854 8136 get_alias_set (TREE_TYPE (ref_type)))
74bf76ed
JJ
8137 && (alignment_support_scheme == dr_aligned
8138 || alignment_support_scheme == dr_unaligned_supported))
8139 {
8140 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
44fc7854 8141 dataref_offset = build_int_cst (ref_type, 0);
8928eff3 8142 inv_p = false;
74bf76ed 8143 }
4f0a0218
RB
8144 else if (first_stmt_for_drptr
8145 && first_stmt != first_stmt_for_drptr)
8146 {
8147 dataref_ptr
8148 = vect_create_data_ref_ptr (first_stmt_for_drptr, aggr_type,
8149 at_loop, offset, &dummy, gsi,
8150 &ptr_incr, simd_lane_access_p,
ab2fc782 8151 &inv_p, byte_offset, bump);
4f0a0218
RB
8152 /* Adjust the pointer by the difference to first_stmt. */
8153 data_reference_p ptrdr
8154 = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt_for_drptr));
8155 tree diff = fold_convert (sizetype,
8156 size_binop (MINUS_EXPR,
8157 DR_INIT (first_dr),
8158 DR_INIT (ptrdr)));
8159 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
8160 stmt, diff);
8161 }
bfaa08b7
RS
8162 else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8163 {
8164 vect_get_gather_scatter_ops (loop, stmt, &gs_info,
8165 &dataref_ptr, &vec_offset);
8166 inv_p = false;
8167 }
74bf76ed
JJ
8168 else
8169 dataref_ptr
8170 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
8171 offset, &dummy, gsi, &ptr_incr,
356bbc4c 8172 simd_lane_access_p, &inv_p,
ab2fc782 8173 byte_offset, bump);
c3a8f964
RS
8174 if (mask)
8175 vec_mask = vect_get_vec_def_for_operand (mask, stmt,
8176 mask_vectype);
74bf76ed 8177 }
ebfd146a 8178 else
c3a8f964
RS
8179 {
8180 if (dataref_offset)
8181 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
ab2fc782 8182 bump);
bfaa08b7 8183 else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
929b4411
RS
8184 vec_offset = vect_get_vec_def_for_stmt_copy (gs_info.offset_dt,
8185 vec_offset);
c3a8f964 8186 else
ab2fc782
RS
8187 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
8188 stmt, bump);
c3a8f964 8189 if (mask)
929b4411 8190 vec_mask = vect_get_vec_def_for_stmt_copy (mask_dt, vec_mask);
c3a8f964 8191 }
ebfd146a 8192
0d0293ac 8193 if (grouped_load || slp_perm)
9771b263 8194 dr_chain.create (vec_num);
5ce1ee7f 8195
2de001ee 8196 if (memory_access_type == VMAT_LOAD_STORE_LANES)
ebfd146a 8197 {
272c6793
RS
8198 tree vec_array;
8199
8200 vec_array = create_vector_array (vectype, vec_num);
8201
7cfb4d93 8202 tree final_mask = NULL_TREE;
70088b95
RS
8203 if (loop_masks)
8204 final_mask = vect_get_loop_mask (gsi, loop_masks, ncopies,
8205 vectype, j);
7cfb4d93
RS
8206 if (vec_mask)
8207 final_mask = prepare_load_store_mask (mask_vectype, final_mask,
8208 vec_mask, gsi);
8209
7e11fc7f 8210 gcall *call;
7cfb4d93 8211 if (final_mask)
7e11fc7f
RS
8212 {
8213 /* Emit:
8214 VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
8215 VEC_MASK). */
8216 unsigned int align = TYPE_ALIGN_UNIT (TREE_TYPE (vectype));
8217 tree alias_ptr = build_int_cst (ref_type, align);
8218 call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 3,
8219 dataref_ptr, alias_ptr,
7cfb4d93 8220 final_mask);
7e11fc7f
RS
8221 }
8222 else
8223 {
8224 /* Emit:
8225 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
8226 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
8227 call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
8228 }
a844293d
RS
8229 gimple_call_set_lhs (call, vec_array);
8230 gimple_call_set_nothrow (call, true);
8231 new_stmt = call;
272c6793 8232 vect_finish_stmt_generation (stmt, new_stmt, gsi);
ebfd146a 8233
272c6793
RS
8234 /* Extract each vector into an SSA_NAME. */
8235 for (i = 0; i < vec_num; i++)
ebfd146a 8236 {
272c6793
RS
8237 new_temp = read_vector_array (stmt, gsi, scalar_dest,
8238 vec_array, i);
9771b263 8239 dr_chain.quick_push (new_temp);
272c6793
RS
8240 }
8241
8242 /* Record the mapping between SSA_NAMEs and statements. */
0d0293ac 8243 vect_record_grouped_load_vectors (stmt, dr_chain);
3ba4ff41
RS
8244
8245 /* Record that VEC_ARRAY is now dead. */
8246 vect_clobber_variable (stmt, gsi, vec_array);
272c6793
RS
8247 }
8248 else
8249 {
8250 for (i = 0; i < vec_num; i++)
8251 {
7cfb4d93 8252 tree final_mask = NULL_TREE;
70088b95 8253 if (loop_masks
7cfb4d93 8254 && memory_access_type != VMAT_INVARIANT)
70088b95
RS
8255 final_mask = vect_get_loop_mask (gsi, loop_masks,
8256 vec_num * ncopies,
7cfb4d93
RS
8257 vectype, vec_num * j + i);
8258 if (vec_mask)
8259 final_mask = prepare_load_store_mask (mask_vectype, final_mask,
8260 vec_mask, gsi);
8261
272c6793
RS
8262 if (i > 0)
8263 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
ab2fc782 8264 stmt, bump);
272c6793
RS
8265
8266 /* 2. Create the vector-load in the loop. */
8267 switch (alignment_support_scheme)
8268 {
8269 case dr_aligned:
8270 case dr_unaligned_supported:
be1ac4ec 8271 {
644ffefd
MJ
8272 unsigned int align, misalign;
8273
bfaa08b7
RS
8274 if (memory_access_type == VMAT_GATHER_SCATTER)
8275 {
8276 tree scale = size_int (gs_info.scale);
8277 gcall *call;
70088b95 8278 if (loop_masks)
bfaa08b7
RS
8279 call = gimple_build_call_internal
8280 (IFN_MASK_GATHER_LOAD, 4, dataref_ptr,
8281 vec_offset, scale, final_mask);
8282 else
8283 call = gimple_build_call_internal
8284 (IFN_GATHER_LOAD, 3, dataref_ptr,
8285 vec_offset, scale);
8286 gimple_call_set_nothrow (call, true);
8287 new_stmt = call;
8288 data_ref = NULL_TREE;
8289 break;
8290 }
8291
f702e7d4 8292 align = DR_TARGET_ALIGNMENT (dr);
272c6793
RS
8293 if (alignment_support_scheme == dr_aligned)
8294 {
8295 gcc_assert (aligned_access_p (first_dr));
644ffefd 8296 misalign = 0;
272c6793
RS
8297 }
8298 else if (DR_MISALIGNMENT (first_dr) == -1)
8299 {
25f68d90 8300 align = dr_alignment (vect_dr_behavior (first_dr));
52639a61 8301 misalign = 0;
272c6793
RS
8302 }
8303 else
c3a8f964 8304 misalign = DR_MISALIGNMENT (first_dr);
aed93b23
RB
8305 if (dataref_offset == NULL_TREE
8306 && TREE_CODE (dataref_ptr) == SSA_NAME)
74bf76ed
JJ
8307 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
8308 align, misalign);
c3a8f964 8309
7cfb4d93 8310 if (final_mask)
c3a8f964
RS
8311 {
8312 align = least_bit_hwi (misalign | align);
8313 tree ptr = build_int_cst (ref_type, align);
8314 gcall *call
8315 = gimple_build_call_internal (IFN_MASK_LOAD, 3,
8316 dataref_ptr, ptr,
7cfb4d93 8317 final_mask);
c3a8f964
RS
8318 gimple_call_set_nothrow (call, true);
8319 new_stmt = call;
8320 data_ref = NULL_TREE;
8321 }
8322 else
8323 {
8324 data_ref
8325 = fold_build2 (MEM_REF, vectype, dataref_ptr,
8326 dataref_offset
8327 ? dataref_offset
8328 : build_int_cst (ref_type, 0));
8329 if (alignment_support_scheme == dr_aligned)
8330 ;
8331 else if (DR_MISALIGNMENT (first_dr) == -1)
8332 TREE_TYPE (data_ref)
8333 = build_aligned_type (TREE_TYPE (data_ref),
8334 align * BITS_PER_UNIT);
8335 else
8336 TREE_TYPE (data_ref)
8337 = build_aligned_type (TREE_TYPE (data_ref),
8338 TYPE_ALIGN (elem_type));
8339 }
272c6793 8340 break;
be1ac4ec 8341 }
272c6793 8342 case dr_explicit_realign:
267d3070 8343 {
272c6793 8344 tree ptr, bump;
272c6793 8345
d88981fc 8346 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
272c6793
RS
8347
8348 if (compute_in_loop)
8349 msq = vect_setup_realignment (first_stmt, gsi,
8350 &realignment_token,
8351 dr_explicit_realign,
8352 dataref_ptr, NULL);
8353
aed93b23
RB
8354 if (TREE_CODE (dataref_ptr) == SSA_NAME)
8355 ptr = copy_ssa_name (dataref_ptr);
8356 else
8357 ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
f702e7d4 8358 unsigned int align = DR_TARGET_ALIGNMENT (first_dr);
0d0e4a03
JJ
8359 new_stmt = gimple_build_assign
8360 (ptr, BIT_AND_EXPR, dataref_ptr,
272c6793
RS
8361 build_int_cst
8362 (TREE_TYPE (dataref_ptr),
f702e7d4 8363 -(HOST_WIDE_INT) align));
272c6793
RS
8364 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8365 data_ref
8366 = build2 (MEM_REF, vectype, ptr,
44fc7854 8367 build_int_cst (ref_type, 0));
19986382 8368 vect_copy_ref_info (data_ref, DR_REF (first_dr));
272c6793
RS
8369 vec_dest = vect_create_destination_var (scalar_dest,
8370 vectype);
8371 new_stmt = gimple_build_assign (vec_dest, data_ref);
8372 new_temp = make_ssa_name (vec_dest, new_stmt);
8373 gimple_assign_set_lhs (new_stmt, new_temp);
8374 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
8375 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
8376 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8377 msq = new_temp;
8378
d88981fc 8379 bump = size_binop (MULT_EXPR, vs,
7b7b1813 8380 TYPE_SIZE_UNIT (elem_type));
d88981fc 8381 bump = size_binop (MINUS_EXPR, bump, size_one_node);
272c6793 8382 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
0d0e4a03
JJ
8383 new_stmt = gimple_build_assign
8384 (NULL_TREE, BIT_AND_EXPR, ptr,
272c6793 8385 build_int_cst
f702e7d4 8386 (TREE_TYPE (ptr), -(HOST_WIDE_INT) align));
aed93b23 8387 ptr = copy_ssa_name (ptr, new_stmt);
272c6793
RS
8388 gimple_assign_set_lhs (new_stmt, ptr);
8389 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8390 data_ref
8391 = build2 (MEM_REF, vectype, ptr,
44fc7854 8392 build_int_cst (ref_type, 0));
272c6793 8393 break;
267d3070 8394 }
272c6793 8395 case dr_explicit_realign_optimized:
f702e7d4
RS
8396 {
8397 if (TREE_CODE (dataref_ptr) == SSA_NAME)
8398 new_temp = copy_ssa_name (dataref_ptr);
8399 else
8400 new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
8401 unsigned int align = DR_TARGET_ALIGNMENT (first_dr);
8402 new_stmt = gimple_build_assign
8403 (new_temp, BIT_AND_EXPR, dataref_ptr,
8404 build_int_cst (TREE_TYPE (dataref_ptr),
8405 -(HOST_WIDE_INT) align));
8406 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8407 data_ref
8408 = build2 (MEM_REF, vectype, new_temp,
8409 build_int_cst (ref_type, 0));
8410 break;
8411 }
272c6793
RS
8412 default:
8413 gcc_unreachable ();
8414 }
ebfd146a 8415 vec_dest = vect_create_destination_var (scalar_dest, vectype);
c3a8f964
RS
8416 /* DATA_REF is null if we've already built the statement. */
8417 if (data_ref)
19986382
RB
8418 {
8419 vect_copy_ref_info (data_ref, DR_REF (first_dr));
8420 new_stmt = gimple_build_assign (vec_dest, data_ref);
8421 }
ebfd146a 8422 new_temp = make_ssa_name (vec_dest, new_stmt);
c3a8f964 8423 gimple_set_lhs (new_stmt, new_temp);
ebfd146a
IR
8424 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8425
272c6793
RS
8426 /* 3. Handle explicit realignment if necessary/supported.
8427 Create in loop:
8428 vec_dest = realign_load (msq, lsq, realignment_token) */
8429 if (alignment_support_scheme == dr_explicit_realign_optimized
8430 || alignment_support_scheme == dr_explicit_realign)
ebfd146a 8431 {
272c6793
RS
8432 lsq = gimple_assign_lhs (new_stmt);
8433 if (!realignment_token)
8434 realignment_token = dataref_ptr;
8435 vec_dest = vect_create_destination_var (scalar_dest, vectype);
0d0e4a03
JJ
8436 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR,
8437 msq, lsq, realignment_token);
272c6793
RS
8438 new_temp = make_ssa_name (vec_dest, new_stmt);
8439 gimple_assign_set_lhs (new_stmt, new_temp);
8440 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8441
8442 if (alignment_support_scheme == dr_explicit_realign_optimized)
8443 {
8444 gcc_assert (phi);
8445 if (i == vec_num - 1 && j == ncopies - 1)
8446 add_phi_arg (phi, lsq,
8447 loop_latch_edge (containing_loop),
9e227d60 8448 UNKNOWN_LOCATION);
272c6793
RS
8449 msq = lsq;
8450 }
ebfd146a 8451 }
ebfd146a 8452
59fd17e3
RB
8453 /* 4. Handle invariant-load. */
8454 if (inv_p && !bb_vinfo)
8455 {
59fd17e3 8456 gcc_assert (!grouped_load);
d1417442
JJ
8457 /* If we have versioned for aliasing or the loop doesn't
8458 have any data dependencies that would preclude this,
8459 then we are sure this is a loop invariant load and
8460 thus we can insert it on the preheader edge. */
8461 if (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
8462 && !nested_in_vect_loop
6b916b36 8463 && hoist_defs_of_uses (stmt, loop))
a0e35eb0
RB
8464 {
8465 if (dump_enabled_p ())
8466 {
8467 dump_printf_loc (MSG_NOTE, vect_location,
8468 "hoisting out of the vectorized "
8469 "loop: ");
8470 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
a0e35eb0 8471 }
b731b390 8472 tree tem = copy_ssa_name (scalar_dest);
a0e35eb0
RB
8473 gsi_insert_on_edge_immediate
8474 (loop_preheader_edge (loop),
8475 gimple_build_assign (tem,
8476 unshare_expr
8477 (gimple_assign_rhs1 (stmt))));
8478 new_temp = vect_init_vector (stmt, tem, vectype, NULL);
34cd48e5 8479 new_stmt = SSA_NAME_DEF_STMT (new_temp);
4fbeb363 8480 vinfo->add_stmt (new_stmt);
a0e35eb0
RB
8481 }
8482 else
8483 {
8484 gimple_stmt_iterator gsi2 = *gsi;
8485 gsi_next (&gsi2);
8486 new_temp = vect_init_vector (stmt, scalar_dest,
8487 vectype, &gsi2);
34cd48e5 8488 new_stmt = SSA_NAME_DEF_STMT (new_temp);
a0e35eb0 8489 }
59fd17e3
RB
8490 }
8491
62da9e14 8492 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
272c6793 8493 {
aec7ae7d
JJ
8494 tree perm_mask = perm_mask_for_reverse (vectype);
8495 new_temp = permute_vec_elements (new_temp, new_temp,
8496 perm_mask, stmt, gsi);
ebfd146a
IR
8497 new_stmt = SSA_NAME_DEF_STMT (new_temp);
8498 }
267d3070 8499
272c6793 8500 /* Collect vector loads and later create their permutation in
0d0293ac
MM
8501 vect_transform_grouped_load (). */
8502 if (grouped_load || slp_perm)
9771b263 8503 dr_chain.quick_push (new_temp);
267d3070 8504
272c6793
RS
8505 /* Store vector loads in the corresponding SLP_NODE. */
8506 if (slp && !slp_perm)
9771b263 8507 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
b267968e
RB
8508
8509 /* With SLP permutation we load the gaps as well, without
8510 we need to skip the gaps after we manage to fully load
2c53b149 8511 all elements. group_gap_adj is DR_GROUP_SIZE here. */
b267968e 8512 group_elt += nunits;
d9f21f6a
RS
8513 if (maybe_ne (group_gap_adj, 0U)
8514 && !slp_perm
8515 && known_eq (group_elt, group_size - group_gap_adj))
b267968e 8516 {
d9f21f6a
RS
8517 poly_wide_int bump_val
8518 = (wi::to_wide (TYPE_SIZE_UNIT (elem_type))
8519 * group_gap_adj);
8e6cdc90 8520 tree bump = wide_int_to_tree (sizetype, bump_val);
b267968e
RB
8521 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
8522 stmt, bump);
8523 group_elt = 0;
8524 }
272c6793 8525 }
9b999e8c
RB
8526 /* Bump the vector pointer to account for a gap or for excess
8527 elements loaded for a permuted SLP load. */
d9f21f6a 8528 if (maybe_ne (group_gap_adj, 0U) && slp_perm)
a64b9c26 8529 {
d9f21f6a
RS
8530 poly_wide_int bump_val
8531 = (wi::to_wide (TYPE_SIZE_UNIT (elem_type))
8532 * group_gap_adj);
8e6cdc90 8533 tree bump = wide_int_to_tree (sizetype, bump_val);
a64b9c26
RB
8534 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
8535 stmt, bump);
8536 }
ebfd146a
IR
8537 }
8538
8539 if (slp && !slp_perm)
8540 continue;
8541
8542 if (slp_perm)
8543 {
29afecdf 8544 unsigned n_perms;
01d8bf07 8545 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
29afecdf
RB
8546 slp_node_instance, false,
8547 &n_perms))
ebfd146a 8548 {
9771b263 8549 dr_chain.release ();
ebfd146a
IR
8550 return false;
8551 }
8552 }
8553 else
8554 {
0d0293ac 8555 if (grouped_load)
ebfd146a 8556 {
2de001ee 8557 if (memory_access_type != VMAT_LOAD_STORE_LANES)
0d0293ac 8558 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
ebfd146a 8559 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
ebfd146a
IR
8560 }
8561 else
8562 {
8563 if (j == 0)
8564 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
8565 else
8566 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
8567 prev_stmt_info = vinfo_for_stmt (new_stmt);
8568 }
8569 }
9771b263 8570 dr_chain.release ();
ebfd146a
IR
8571 }
8572
ebfd146a
IR
8573 return true;
8574}
8575
8576/* Function vect_is_simple_cond.
b8698a0f 8577
ebfd146a
IR
8578 Input:
8579 LOOP - the loop that is being vectorized.
8580 COND - Condition that is checked for simple use.
8581
e9e1d143
RG
8582 Output:
8583 *COMP_VECTYPE - the vector type for the comparison.
4fc5ebf1 8584 *DTS - The def types for the arguments of the comparison
e9e1d143 8585
ebfd146a
IR
8586 Returns whether a COND can be vectorized. Checks whether
8587 condition operands are supportable using vec_is_simple_use. */
8588
87aab9b2 8589static bool
4fc5ebf1 8590vect_is_simple_cond (tree cond, vec_info *vinfo,
8da4c8d8
RB
8591 tree *comp_vectype, enum vect_def_type *dts,
8592 tree vectype)
ebfd146a
IR
8593{
8594 tree lhs, rhs;
e9e1d143 8595 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
ebfd146a 8596
a414c77f
IE
8597 /* Mask case. */
8598 if (TREE_CODE (cond) == SSA_NAME
2568d8a1 8599 && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
a414c77f 8600 {
894dd753 8601 if (!vect_is_simple_use (cond, vinfo, &dts[0], comp_vectype)
a414c77f
IE
8602 || !*comp_vectype
8603 || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
8604 return false;
8605 return true;
8606 }
8607
ebfd146a
IR
8608 if (!COMPARISON_CLASS_P (cond))
8609 return false;
8610
8611 lhs = TREE_OPERAND (cond, 0);
8612 rhs = TREE_OPERAND (cond, 1);
8613
8614 if (TREE_CODE (lhs) == SSA_NAME)
8615 {
894dd753 8616 if (!vect_is_simple_use (lhs, vinfo, &dts[0], &vectype1))
ebfd146a
IR
8617 return false;
8618 }
4fc5ebf1
JG
8619 else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
8620 || TREE_CODE (lhs) == FIXED_CST)
8621 dts[0] = vect_constant_def;
8622 else
ebfd146a
IR
8623 return false;
8624
8625 if (TREE_CODE (rhs) == SSA_NAME)
8626 {
894dd753 8627 if (!vect_is_simple_use (rhs, vinfo, &dts[1], &vectype2))
ebfd146a
IR
8628 return false;
8629 }
4fc5ebf1
JG
8630 else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
8631 || TREE_CODE (rhs) == FIXED_CST)
8632 dts[1] = vect_constant_def;
8633 else
ebfd146a
IR
8634 return false;
8635
28b33016 8636 if (vectype1 && vectype2
928686b1
RS
8637 && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
8638 TYPE_VECTOR_SUBPARTS (vectype2)))
28b33016
IE
8639 return false;
8640
e9e1d143 8641 *comp_vectype = vectype1 ? vectype1 : vectype2;
8da4c8d8 8642 /* Invariant comparison. */
4515e413 8643 if (! *comp_vectype && vectype)
8da4c8d8
RB
8644 {
8645 tree scalar_type = TREE_TYPE (lhs);
8646 /* If we can widen the comparison to match vectype do so. */
8647 if (INTEGRAL_TYPE_P (scalar_type)
8648 && tree_int_cst_lt (TYPE_SIZE (scalar_type),
8649 TYPE_SIZE (TREE_TYPE (vectype))))
8650 scalar_type = build_nonstandard_integer_type
8651 (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype))),
8652 TYPE_UNSIGNED (scalar_type));
8653 *comp_vectype = get_vectype_for_scalar_type (scalar_type);
8654 }
8655
ebfd146a
IR
8656 return true;
8657}
8658
8659/* vectorizable_condition.
8660
b8698a0f
L
8661 Check if STMT is conditional modify expression that can be vectorized.
8662 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
8663 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
4bbe8262
IR
8664 at GSI.
8665
8666 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
8667 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
0ad23163 8668 else clause if it is 2).
ebfd146a
IR
8669
8670 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
8671
4bbe8262 8672bool
355fe088
TS
8673vectorizable_condition (gimple *stmt, gimple_stmt_iterator *gsi,
8674 gimple **vec_stmt, tree reduc_def, int reduc_index,
68435eb2 8675 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
ebfd146a
IR
8676{
8677 tree scalar_dest = NULL_TREE;
8678 tree vec_dest = NULL_TREE;
01216d27
JJ
8679 tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
8680 tree then_clause, else_clause;
ebfd146a 8681 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
df11cc78 8682 tree comp_vectype = NULL_TREE;
ff802fa1
IR
8683 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
8684 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
5958f9e2 8685 tree vec_compare;
ebfd146a
IR
8686 tree new_temp;
8687 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4fc5ebf1
JG
8688 enum vect_def_type dts[4]
8689 = {vect_unknown_def_type, vect_unknown_def_type,
8690 vect_unknown_def_type, vect_unknown_def_type};
8691 int ndts = 4;
f7e531cf 8692 int ncopies;
01216d27 8693 enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
a855b1b1 8694 stmt_vec_info prev_stmt_info = NULL;
f7e531cf
IR
8695 int i, j;
8696 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6e1aa848
DN
8697 vec<tree> vec_oprnds0 = vNULL;
8698 vec<tree> vec_oprnds1 = vNULL;
8699 vec<tree> vec_oprnds2 = vNULL;
8700 vec<tree> vec_oprnds3 = vNULL;
74946978 8701 tree vec_cmp_type;
a414c77f 8702 bool masked = false;
b8698a0f 8703
f7e531cf
IR
8704 if (reduc_index && STMT_SLP_TYPE (stmt_info))
8705 return false;
8706
bb6c2b68
RS
8707 vect_reduction_type reduction_type
8708 = STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info);
8709 if (reduction_type == TREE_CODE_REDUCTION)
af29617a
AH
8710 {
8711 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
8712 return false;
ebfd146a 8713
af29617a
AH
8714 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
8715 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
8716 && reduc_def))
8717 return false;
ebfd146a 8718
af29617a
AH
8719 /* FORNOW: not yet supported. */
8720 if (STMT_VINFO_LIVE_P (stmt_info))
8721 {
8722 if (dump_enabled_p ())
8723 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8724 "value used after loop.\n");
8725 return false;
8726 }
ebfd146a
IR
8727 }
8728
8729 /* Is vectorizable conditional operation? */
8730 if (!is_gimple_assign (stmt))
8731 return false;
8732
8733 code = gimple_assign_rhs_code (stmt);
8734
8735 if (code != COND_EXPR)
8736 return false;
8737
465c8c19 8738 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2947d3b2 8739 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
465c8c19 8740
fce57248 8741 if (slp_node)
465c8c19
JJ
8742 ncopies = 1;
8743 else
e8f142e2 8744 ncopies = vect_get_num_copies (loop_vinfo, vectype);
465c8c19
JJ
8745
8746 gcc_assert (ncopies >= 1);
8747 if (reduc_index && ncopies > 1)
8748 return false; /* FORNOW */
8749
4e71066d
RG
8750 cond_expr = gimple_assign_rhs1 (stmt);
8751 then_clause = gimple_assign_rhs2 (stmt);
8752 else_clause = gimple_assign_rhs3 (stmt);
ebfd146a 8753
4fc5ebf1 8754 if (!vect_is_simple_cond (cond_expr, stmt_info->vinfo,
4515e413 8755 &comp_vectype, &dts[0], slp_node ? NULL : vectype)
e9e1d143 8756 || !comp_vectype)
ebfd146a
IR
8757 return false;
8758
894dd753 8759 if (!vect_is_simple_use (then_clause, stmt_info->vinfo, &dts[2], &vectype1))
2947d3b2 8760 return false;
894dd753 8761 if (!vect_is_simple_use (else_clause, stmt_info->vinfo, &dts[3], &vectype2))
ebfd146a 8762 return false;
2947d3b2
IE
8763
8764 if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
8765 return false;
8766
8767 if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
ebfd146a
IR
8768 return false;
8769
28b33016
IE
8770 masked = !COMPARISON_CLASS_P (cond_expr);
8771 vec_cmp_type = build_same_sized_truth_vector_type (comp_vectype);
8772
74946978
MP
8773 if (vec_cmp_type == NULL_TREE)
8774 return false;
784fb9b3 8775
01216d27
JJ
8776 cond_code = TREE_CODE (cond_expr);
8777 if (!masked)
8778 {
8779 cond_expr0 = TREE_OPERAND (cond_expr, 0);
8780 cond_expr1 = TREE_OPERAND (cond_expr, 1);
8781 }
8782
8783 if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
8784 {
8785 /* Boolean values may have another representation in vectors
8786 and therefore we prefer bit operations over comparison for
8787 them (which also works for scalar masks). We store opcodes
8788 to use in bitop1 and bitop2. Statement is vectorized as
8789 BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
8790 depending on bitop1 and bitop2 arity. */
8791 switch (cond_code)
8792 {
8793 case GT_EXPR:
8794 bitop1 = BIT_NOT_EXPR;
8795 bitop2 = BIT_AND_EXPR;
8796 break;
8797 case GE_EXPR:
8798 bitop1 = BIT_NOT_EXPR;
8799 bitop2 = BIT_IOR_EXPR;
8800 break;
8801 case LT_EXPR:
8802 bitop1 = BIT_NOT_EXPR;
8803 bitop2 = BIT_AND_EXPR;
8804 std::swap (cond_expr0, cond_expr1);
8805 break;
8806 case LE_EXPR:
8807 bitop1 = BIT_NOT_EXPR;
8808 bitop2 = BIT_IOR_EXPR;
8809 std::swap (cond_expr0, cond_expr1);
8810 break;
8811 case NE_EXPR:
8812 bitop1 = BIT_XOR_EXPR;
8813 break;
8814 case EQ_EXPR:
8815 bitop1 = BIT_XOR_EXPR;
8816 bitop2 = BIT_NOT_EXPR;
8817 break;
8818 default:
8819 return false;
8820 }
8821 cond_code = SSA_NAME;
8822 }
8823
b8698a0f 8824 if (!vec_stmt)
ebfd146a 8825 {
01216d27
JJ
8826 if (bitop1 != NOP_EXPR)
8827 {
8828 machine_mode mode = TYPE_MODE (comp_vectype);
8829 optab optab;
8830
8831 optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
8832 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
8833 return false;
8834
8835 if (bitop2 != NOP_EXPR)
8836 {
8837 optab = optab_for_tree_code (bitop2, comp_vectype,
8838 optab_default);
8839 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
8840 return false;
8841 }
8842 }
4fc5ebf1
JG
8843 if (expand_vec_cond_expr_p (vectype, comp_vectype,
8844 cond_code))
8845 {
68435eb2
RB
8846 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
8847 vect_model_simple_cost (stmt_info, ncopies, dts, ndts, slp_node,
8848 cost_vec);
4fc5ebf1
JG
8849 return true;
8850 }
8851 return false;
ebfd146a
IR
8852 }
8853
f7e531cf
IR
8854 /* Transform. */
8855
8856 if (!slp_node)
8857 {
9771b263
DN
8858 vec_oprnds0.create (1);
8859 vec_oprnds1.create (1);
8860 vec_oprnds2.create (1);
8861 vec_oprnds3.create (1);
f7e531cf 8862 }
ebfd146a
IR
8863
8864 /* Handle def. */
8865 scalar_dest = gimple_assign_lhs (stmt);
bb6c2b68
RS
8866 if (reduction_type != EXTRACT_LAST_REDUCTION)
8867 vec_dest = vect_create_destination_var (scalar_dest, vectype);
ebfd146a
IR
8868
8869 /* Handle cond expr. */
a855b1b1
MM
8870 for (j = 0; j < ncopies; j++)
8871 {
bb6c2b68 8872 gimple *new_stmt = NULL;
a855b1b1
MM
8873 if (j == 0)
8874 {
f7e531cf
IR
8875 if (slp_node)
8876 {
00f96dc9
TS
8877 auto_vec<tree, 4> ops;
8878 auto_vec<vec<tree>, 4> vec_defs;
9771b263 8879
a414c77f 8880 if (masked)
01216d27 8881 ops.safe_push (cond_expr);
a414c77f
IE
8882 else
8883 {
01216d27
JJ
8884 ops.safe_push (cond_expr0);
8885 ops.safe_push (cond_expr1);
a414c77f 8886 }
9771b263
DN
8887 ops.safe_push (then_clause);
8888 ops.safe_push (else_clause);
306b0c92 8889 vect_get_slp_defs (ops, slp_node, &vec_defs);
37b5ec8f
JJ
8890 vec_oprnds3 = vec_defs.pop ();
8891 vec_oprnds2 = vec_defs.pop ();
a414c77f
IE
8892 if (!masked)
8893 vec_oprnds1 = vec_defs.pop ();
37b5ec8f 8894 vec_oprnds0 = vec_defs.pop ();
f7e531cf
IR
8895 }
8896 else
8897 {
a414c77f
IE
8898 if (masked)
8899 {
8900 vec_cond_lhs
8901 = vect_get_vec_def_for_operand (cond_expr, stmt,
8902 comp_vectype);
894dd753 8903 vect_is_simple_use (cond_expr, stmt_info->vinfo, &dts[0]);
a414c77f
IE
8904 }
8905 else
8906 {
01216d27
JJ
8907 vec_cond_lhs
8908 = vect_get_vec_def_for_operand (cond_expr0,
8909 stmt, comp_vectype);
894dd753 8910 vect_is_simple_use (cond_expr0, loop_vinfo, &dts[0]);
01216d27
JJ
8911
8912 vec_cond_rhs
8913 = vect_get_vec_def_for_operand (cond_expr1,
8914 stmt, comp_vectype);
894dd753 8915 vect_is_simple_use (cond_expr1, loop_vinfo, &dts[1]);
a414c77f 8916 }
f7e531cf
IR
8917 if (reduc_index == 1)
8918 vec_then_clause = reduc_def;
8919 else
8920 {
8921 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
81c40241 8922 stmt);
894dd753 8923 vect_is_simple_use (then_clause, loop_vinfo, &dts[2]);
f7e531cf
IR
8924 }
8925 if (reduc_index == 2)
8926 vec_else_clause = reduc_def;
8927 else
8928 {
8929 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
81c40241 8930 stmt);
894dd753 8931 vect_is_simple_use (else_clause, loop_vinfo, &dts[3]);
f7e531cf 8932 }
a855b1b1
MM
8933 }
8934 }
8935 else
8936 {
a414c77f
IE
8937 vec_cond_lhs
8938 = vect_get_vec_def_for_stmt_copy (dts[0],
8939 vec_oprnds0.pop ());
8940 if (!masked)
8941 vec_cond_rhs
8942 = vect_get_vec_def_for_stmt_copy (dts[1],
8943 vec_oprnds1.pop ());
8944
a855b1b1 8945 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
9771b263 8946 vec_oprnds2.pop ());
a855b1b1 8947 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
9771b263 8948 vec_oprnds3.pop ());
f7e531cf
IR
8949 }
8950
8951 if (!slp_node)
8952 {
9771b263 8953 vec_oprnds0.quick_push (vec_cond_lhs);
a414c77f
IE
8954 if (!masked)
8955 vec_oprnds1.quick_push (vec_cond_rhs);
9771b263
DN
8956 vec_oprnds2.quick_push (vec_then_clause);
8957 vec_oprnds3.quick_push (vec_else_clause);
a855b1b1
MM
8958 }
8959
9dc3f7de 8960 /* Arguments are ready. Create the new vector stmt. */
9771b263 8961 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
f7e531cf 8962 {
9771b263
DN
8963 vec_then_clause = vec_oprnds2[i];
8964 vec_else_clause = vec_oprnds3[i];
a855b1b1 8965
a414c77f
IE
8966 if (masked)
8967 vec_compare = vec_cond_lhs;
8968 else
8969 {
8970 vec_cond_rhs = vec_oprnds1[i];
01216d27
JJ
8971 if (bitop1 == NOP_EXPR)
8972 vec_compare = build2 (cond_code, vec_cmp_type,
8973 vec_cond_lhs, vec_cond_rhs);
8974 else
8975 {
8976 new_temp = make_ssa_name (vec_cmp_type);
8977 if (bitop1 == BIT_NOT_EXPR)
8978 new_stmt = gimple_build_assign (new_temp, bitop1,
8979 vec_cond_rhs);
8980 else
8981 new_stmt
8982 = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
8983 vec_cond_rhs);
8984 vect_finish_stmt_generation (stmt, new_stmt, gsi);
8985 if (bitop2 == NOP_EXPR)
8986 vec_compare = new_temp;
8987 else if (bitop2 == BIT_NOT_EXPR)
8988 {
8989 /* Instead of doing ~x ? y : z do x ? z : y. */
8990 vec_compare = new_temp;
8991 std::swap (vec_then_clause, vec_else_clause);
8992 }
8993 else
8994 {
8995 vec_compare = make_ssa_name (vec_cmp_type);
8996 new_stmt
8997 = gimple_build_assign (vec_compare, bitop2,
8998 vec_cond_lhs, new_temp);
8999 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9000 }
9001 }
a414c77f 9002 }
bb6c2b68
RS
9003 if (reduction_type == EXTRACT_LAST_REDUCTION)
9004 {
9005 if (!is_gimple_val (vec_compare))
9006 {
9007 tree vec_compare_name = make_ssa_name (vec_cmp_type);
9008 new_stmt = gimple_build_assign (vec_compare_name,
9009 vec_compare);
9010 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9011 vec_compare = vec_compare_name;
9012 }
9013 gcc_assert (reduc_index == 2);
9014 new_stmt = gimple_build_call_internal
9015 (IFN_FOLD_EXTRACT_LAST, 3, else_clause, vec_compare,
9016 vec_then_clause);
9017 gimple_call_set_lhs (new_stmt, scalar_dest);
9018 SSA_NAME_DEF_STMT (scalar_dest) = new_stmt;
9019 if (stmt == gsi_stmt (*gsi))
9020 vect_finish_replace_stmt (stmt, new_stmt);
9021 else
9022 {
9023 /* In this case we're moving the definition to later in the
9024 block. That doesn't matter because the only uses of the
9025 lhs are in phi statements. */
9026 gimple_stmt_iterator old_gsi = gsi_for_stmt (stmt);
9027 gsi_remove (&old_gsi, true);
9028 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9029 }
9030 }
9031 else
9032 {
9033 new_temp = make_ssa_name (vec_dest);
9034 new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR,
9035 vec_compare, vec_then_clause,
9036 vec_else_clause);
9037 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9038 }
f7e531cf 9039 if (slp_node)
9771b263 9040 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
f7e531cf
IR
9041 }
9042
9043 if (slp_node)
9044 continue;
9045
9046 if (j == 0)
9047 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
9048 else
9049 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
9050
9051 prev_stmt_info = vinfo_for_stmt (new_stmt);
a855b1b1 9052 }
b8698a0f 9053
9771b263
DN
9054 vec_oprnds0.release ();
9055 vec_oprnds1.release ();
9056 vec_oprnds2.release ();
9057 vec_oprnds3.release ();
f7e531cf 9058
ebfd146a
IR
9059 return true;
9060}
9061
42fd8198
IE
9062/* vectorizable_comparison.
9063
9064 Check if STMT is comparison expression that can be vectorized.
9065 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
9066 comparison, put it in VEC_STMT, and insert it at GSI.
9067
9068 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
9069
fce57248 9070static bool
42fd8198
IE
9071vectorizable_comparison (gimple *stmt, gimple_stmt_iterator *gsi,
9072 gimple **vec_stmt, tree reduc_def,
68435eb2 9073 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
42fd8198
IE
9074{
9075 tree lhs, rhs1, rhs2;
9076 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
9077 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
9078 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
9079 tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
9080 tree new_temp;
9081 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
9082 enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
4fc5ebf1 9083 int ndts = 2;
928686b1 9084 poly_uint64 nunits;
42fd8198 9085 int ncopies;
49e76ff1 9086 enum tree_code code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
42fd8198
IE
9087 stmt_vec_info prev_stmt_info = NULL;
9088 int i, j;
9089 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
9090 vec<tree> vec_oprnds0 = vNULL;
9091 vec<tree> vec_oprnds1 = vNULL;
42fd8198
IE
9092 tree mask_type;
9093 tree mask;
9094
c245362b
IE
9095 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
9096 return false;
9097
30480bcd 9098 if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
42fd8198
IE
9099 return false;
9100
9101 mask_type = vectype;
9102 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9103
fce57248 9104 if (slp_node)
42fd8198
IE
9105 ncopies = 1;
9106 else
e8f142e2 9107 ncopies = vect_get_num_copies (loop_vinfo, vectype);
42fd8198
IE
9108
9109 gcc_assert (ncopies >= 1);
42fd8198
IE
9110 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
9111 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
9112 && reduc_def))
9113 return false;
9114
9115 if (STMT_VINFO_LIVE_P (stmt_info))
9116 {
9117 if (dump_enabled_p ())
9118 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9119 "value used after loop.\n");
9120 return false;
9121 }
9122
9123 if (!is_gimple_assign (stmt))
9124 return false;
9125
9126 code = gimple_assign_rhs_code (stmt);
9127
9128 if (TREE_CODE_CLASS (code) != tcc_comparison)
9129 return false;
9130
9131 rhs1 = gimple_assign_rhs1 (stmt);
9132 rhs2 = gimple_assign_rhs2 (stmt);
9133
894dd753 9134 if (!vect_is_simple_use (rhs1, stmt_info->vinfo, &dts[0], &vectype1))
42fd8198
IE
9135 return false;
9136
894dd753 9137 if (!vect_is_simple_use (rhs2, stmt_info->vinfo, &dts[1], &vectype2))
42fd8198
IE
9138 return false;
9139
9140 if (vectype1 && vectype2
928686b1
RS
9141 && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
9142 TYPE_VECTOR_SUBPARTS (vectype2)))
42fd8198
IE
9143 return false;
9144
9145 vectype = vectype1 ? vectype1 : vectype2;
9146
9147 /* Invariant comparison. */
9148 if (!vectype)
9149 {
69a9a66f 9150 vectype = get_vectype_for_scalar_type (TREE_TYPE (rhs1));
928686b1 9151 if (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
42fd8198
IE
9152 return false;
9153 }
928686b1 9154 else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
42fd8198
IE
9155 return false;
9156
49e76ff1
IE
9157 /* Can't compare mask and non-mask types. */
9158 if (vectype1 && vectype2
9159 && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
9160 return false;
9161
9162 /* Boolean values may have another representation in vectors
9163 and therefore we prefer bit operations over comparison for
9164 them (which also works for scalar masks). We store opcodes
9165 to use in bitop1 and bitop2. Statement is vectorized as
9166 BITOP2 (rhs1 BITOP1 rhs2) or
9167 rhs1 BITOP2 (BITOP1 rhs2)
9168 depending on bitop1 and bitop2 arity. */
9169 if (VECTOR_BOOLEAN_TYPE_P (vectype))
9170 {
9171 if (code == GT_EXPR)
9172 {
9173 bitop1 = BIT_NOT_EXPR;
9174 bitop2 = BIT_AND_EXPR;
9175 }
9176 else if (code == GE_EXPR)
9177 {
9178 bitop1 = BIT_NOT_EXPR;
9179 bitop2 = BIT_IOR_EXPR;
9180 }
9181 else if (code == LT_EXPR)
9182 {
9183 bitop1 = BIT_NOT_EXPR;
9184 bitop2 = BIT_AND_EXPR;
9185 std::swap (rhs1, rhs2);
264d951a 9186 std::swap (dts[0], dts[1]);
49e76ff1
IE
9187 }
9188 else if (code == LE_EXPR)
9189 {
9190 bitop1 = BIT_NOT_EXPR;
9191 bitop2 = BIT_IOR_EXPR;
9192 std::swap (rhs1, rhs2);
264d951a 9193 std::swap (dts[0], dts[1]);
49e76ff1
IE
9194 }
9195 else
9196 {
9197 bitop1 = BIT_XOR_EXPR;
9198 if (code == EQ_EXPR)
9199 bitop2 = BIT_NOT_EXPR;
9200 }
9201 }
9202
42fd8198
IE
9203 if (!vec_stmt)
9204 {
49e76ff1 9205 if (bitop1 == NOP_EXPR)
68435eb2
RB
9206 {
9207 if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
9208 return false;
9209 }
49e76ff1
IE
9210 else
9211 {
9212 machine_mode mode = TYPE_MODE (vectype);
9213 optab optab;
9214
9215 optab = optab_for_tree_code (bitop1, vectype, optab_default);
9216 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
9217 return false;
9218
9219 if (bitop2 != NOP_EXPR)
9220 {
9221 optab = optab_for_tree_code (bitop2, vectype, optab_default);
9222 if (!optab || optab_handler (optab, mode) == CODE_FOR_nothing)
9223 return false;
9224 }
49e76ff1 9225 }
68435eb2
RB
9226
9227 STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
9228 vect_model_simple_cost (stmt_info, ncopies * (1 + (bitop2 != NOP_EXPR)),
9229 dts, ndts, slp_node, cost_vec);
9230 return true;
42fd8198
IE
9231 }
9232
9233 /* Transform. */
9234 if (!slp_node)
9235 {
9236 vec_oprnds0.create (1);
9237 vec_oprnds1.create (1);
9238 }
9239
9240 /* Handle def. */
9241 lhs = gimple_assign_lhs (stmt);
9242 mask = vect_create_destination_var (lhs, mask_type);
9243
9244 /* Handle cmp expr. */
9245 for (j = 0; j < ncopies; j++)
9246 {
9247 gassign *new_stmt = NULL;
9248 if (j == 0)
9249 {
9250 if (slp_node)
9251 {
9252 auto_vec<tree, 2> ops;
9253 auto_vec<vec<tree>, 2> vec_defs;
9254
9255 ops.safe_push (rhs1);
9256 ops.safe_push (rhs2);
306b0c92 9257 vect_get_slp_defs (ops, slp_node, &vec_defs);
42fd8198
IE
9258 vec_oprnds1 = vec_defs.pop ();
9259 vec_oprnds0 = vec_defs.pop ();
9260 }
9261 else
9262 {
e4af0bc4
IE
9263 vec_rhs1 = vect_get_vec_def_for_operand (rhs1, stmt, vectype);
9264 vec_rhs2 = vect_get_vec_def_for_operand (rhs2, stmt, vectype);
42fd8198
IE
9265 }
9266 }
9267 else
9268 {
9269 vec_rhs1 = vect_get_vec_def_for_stmt_copy (dts[0],
9270 vec_oprnds0.pop ());
9271 vec_rhs2 = vect_get_vec_def_for_stmt_copy (dts[1],
9272 vec_oprnds1.pop ());
9273 }
9274
9275 if (!slp_node)
9276 {
9277 vec_oprnds0.quick_push (vec_rhs1);
9278 vec_oprnds1.quick_push (vec_rhs2);
9279 }
9280
9281 /* Arguments are ready. Create the new vector stmt. */
9282 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
9283 {
9284 vec_rhs2 = vec_oprnds1[i];
9285
9286 new_temp = make_ssa_name (mask);
49e76ff1
IE
9287 if (bitop1 == NOP_EXPR)
9288 {
9289 new_stmt = gimple_build_assign (new_temp, code,
9290 vec_rhs1, vec_rhs2);
9291 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9292 }
9293 else
9294 {
9295 if (bitop1 == BIT_NOT_EXPR)
9296 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
9297 else
9298 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
9299 vec_rhs2);
9300 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9301 if (bitop2 != NOP_EXPR)
9302 {
9303 tree res = make_ssa_name (mask);
9304 if (bitop2 == BIT_NOT_EXPR)
9305 new_stmt = gimple_build_assign (res, bitop2, new_temp);
9306 else
9307 new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
9308 new_temp);
9309 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9310 }
9311 }
42fd8198
IE
9312 if (slp_node)
9313 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
9314 }
9315
9316 if (slp_node)
9317 continue;
9318
9319 if (j == 0)
9320 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
9321 else
9322 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
9323
9324 prev_stmt_info = vinfo_for_stmt (new_stmt);
9325 }
9326
9327 vec_oprnds0.release ();
9328 vec_oprnds1.release ();
9329
9330 return true;
9331}
ebfd146a 9332
68a0f2ff
RS
9333/* If SLP_NODE is nonnull, return true if vectorizable_live_operation
9334 can handle all live statements in the node. Otherwise return true
9335 if STMT is not live or if vectorizable_live_operation can handle it.
9336 GSI and VEC_STMT are as for vectorizable_live_operation. */
9337
9338static bool
9339can_vectorize_live_stmts (gimple *stmt, gimple_stmt_iterator *gsi,
68435eb2
RB
9340 slp_tree slp_node, gimple **vec_stmt,
9341 stmt_vector_for_cost *cost_vec)
68a0f2ff
RS
9342{
9343 if (slp_node)
9344 {
9345 gimple *slp_stmt;
9346 unsigned int i;
9347 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt)
9348 {
9349 stmt_vec_info slp_stmt_info = vinfo_for_stmt (slp_stmt);
9350 if (STMT_VINFO_LIVE_P (slp_stmt_info)
9351 && !vectorizable_live_operation (slp_stmt, gsi, slp_node, i,
68435eb2 9352 vec_stmt, cost_vec))
68a0f2ff
RS
9353 return false;
9354 }
9355 }
9356 else if (STMT_VINFO_LIVE_P (vinfo_for_stmt (stmt))
68435eb2
RB
9357 && !vectorizable_live_operation (stmt, gsi, slp_node, -1, vec_stmt,
9358 cost_vec))
68a0f2ff
RS
9359 return false;
9360
9361 return true;
9362}
9363
8644a673 9364/* Make sure the statement is vectorizable. */
ebfd146a
IR
9365
9366bool
891ad31c 9367vect_analyze_stmt (gimple *stmt, bool *need_to_vectorize, slp_tree node,
68435eb2 9368 slp_instance node_instance, stmt_vector_for_cost *cost_vec)
ebfd146a 9369{
8644a673 9370 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6585ff8f 9371 vec_info *vinfo = stmt_info->vinfo;
a70d6342 9372 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
b8698a0f 9373 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
ebfd146a 9374 bool ok;
355fe088 9375 gimple *pattern_stmt;
363477c0 9376 gimple_seq pattern_def_seq;
ebfd146a 9377
73fbfcad 9378 if (dump_enabled_p ())
ebfd146a 9379 {
78c60e3d
SS
9380 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
9381 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
8644a673 9382 }
ebfd146a 9383
1825a1f3 9384 if (gimple_has_volatile_ops (stmt))
b8698a0f 9385 {
73fbfcad 9386 if (dump_enabled_p ())
78c60e3d 9387 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 9388 "not vectorized: stmt has volatile operands\n");
1825a1f3
IR
9389
9390 return false;
9391 }
b8698a0f 9392
d54a098e
RS
9393 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
9394 && node == NULL
9395 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
9396 {
9397 gimple_stmt_iterator si;
9398
9399 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
9400 {
9401 gimple *pattern_def_stmt = gsi_stmt (si);
6585ff8f
RS
9402 stmt_vec_info pattern_def_stmt_info
9403 = vinfo->lookup_stmt (gsi_stmt (si));
9404 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
9405 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
d54a098e
RS
9406 {
9407 /* Analyze def stmt of STMT if it's a pattern stmt. */
9408 if (dump_enabled_p ())
9409 {
9410 dump_printf_loc (MSG_NOTE, vect_location,
9411 "==> examining pattern def statement: ");
9412 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
9413 }
9414
9415 if (!vect_analyze_stmt (pattern_def_stmt,
9416 need_to_vectorize, node, node_instance,
9417 cost_vec))
9418 return false;
9419 }
9420 }
9421 }
9422
b8698a0f 9423 /* Skip stmts that do not need to be vectorized. In loops this is expected
8644a673
IR
9424 to include:
9425 - the COND_EXPR which is the loop exit condition
9426 - any LABEL_EXPRs in the loop
b8698a0f 9427 - computations that are used only for array indexing or loop control.
8644a673 9428 In basic blocks we only analyze statements that are a part of some SLP
83197f37 9429 instance, therefore, all the statements are relevant.
ebfd146a 9430
d092494c 9431 Pattern statement needs to be analyzed instead of the original statement
83197f37 9432 if the original statement is not relevant. Otherwise, we analyze both
079c527f
JJ
9433 statements. In basic blocks we are called from some SLP instance
9434 traversal, don't analyze pattern stmts instead, the pattern stmts
9435 already will be part of SLP instance. */
83197f37
IR
9436
9437 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
b8698a0f 9438 if (!STMT_VINFO_RELEVANT_P (stmt_info)
8644a673 9439 && !STMT_VINFO_LIVE_P (stmt_info))
ebfd146a 9440 {
9d5e7640 9441 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
83197f37 9442 && pattern_stmt
9d5e7640
IR
9443 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
9444 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
9445 {
83197f37 9446 /* Analyze PATTERN_STMT instead of the original stmt. */
9d5e7640
IR
9447 stmt = pattern_stmt;
9448 stmt_info = vinfo_for_stmt (pattern_stmt);
73fbfcad 9449 if (dump_enabled_p ())
9d5e7640 9450 {
78c60e3d
SS
9451 dump_printf_loc (MSG_NOTE, vect_location,
9452 "==> examining pattern statement: ");
9453 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
9d5e7640
IR
9454 }
9455 }
9456 else
9457 {
73fbfcad 9458 if (dump_enabled_p ())
e645e942 9459 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
ebfd146a 9460
9d5e7640
IR
9461 return true;
9462 }
8644a673 9463 }
83197f37 9464 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
079c527f 9465 && node == NULL
83197f37
IR
9466 && pattern_stmt
9467 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
9468 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
9469 {
9470 /* Analyze PATTERN_STMT too. */
73fbfcad 9471 if (dump_enabled_p ())
83197f37 9472 {
78c60e3d
SS
9473 dump_printf_loc (MSG_NOTE, vect_location,
9474 "==> examining pattern statement: ");
9475 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
83197f37
IR
9476 }
9477
891ad31c 9478 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node,
68435eb2 9479 node_instance, cost_vec))
83197f37
IR
9480 return false;
9481 }
ebfd146a 9482
8644a673
IR
9483 switch (STMT_VINFO_DEF_TYPE (stmt_info))
9484 {
9485 case vect_internal_def:
9486 break;
ebfd146a 9487
8644a673 9488 case vect_reduction_def:
7c5222ff 9489 case vect_nested_cycle:
14a61437
RB
9490 gcc_assert (!bb_vinfo
9491 && (relevance == vect_used_in_outer
9492 || relevance == vect_used_in_outer_by_reduction
9493 || relevance == vect_used_by_reduction
b28ead45
AH
9494 || relevance == vect_unused_in_scope
9495 || relevance == vect_used_only_live));
8644a673
IR
9496 break;
9497
9498 case vect_induction_def:
e7baeb39
RB
9499 gcc_assert (!bb_vinfo);
9500 break;
9501
8644a673
IR
9502 case vect_constant_def:
9503 case vect_external_def:
9504 case vect_unknown_def_type:
9505 default:
9506 gcc_unreachable ();
9507 }
ebfd146a 9508
8644a673 9509 if (STMT_VINFO_RELEVANT_P (stmt_info))
ebfd146a 9510 {
8644a673 9511 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
0136f8f0
AH
9512 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
9513 || (is_gimple_call (stmt)
9514 && gimple_call_lhs (stmt) == NULL_TREE));
8644a673 9515 *need_to_vectorize = true;
ebfd146a
IR
9516 }
9517
b1af7da6
RB
9518 if (PURE_SLP_STMT (stmt_info) && !node)
9519 {
9520 dump_printf_loc (MSG_NOTE, vect_location,
9521 "handled only by SLP analysis\n");
9522 return true;
9523 }
9524
9525 ok = true;
9526 if (!bb_vinfo
9527 && (STMT_VINFO_RELEVANT_P (stmt_info)
9528 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
68435eb2
RB
9529 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node, cost_vec)
9530 || vectorizable_conversion (stmt, NULL, NULL, node, cost_vec)
9531 || vectorizable_shift (stmt, NULL, NULL, node, cost_vec)
9532 || vectorizable_operation (stmt, NULL, NULL, node, cost_vec)
9533 || vectorizable_assignment (stmt, NULL, NULL, node, cost_vec)
9534 || vectorizable_load (stmt, NULL, NULL, node, node_instance, cost_vec)
9535 || vectorizable_call (stmt, NULL, NULL, node, cost_vec)
9536 || vectorizable_store (stmt, NULL, NULL, node, cost_vec)
9537 || vectorizable_reduction (stmt, NULL, NULL, node, node_instance,
9538 cost_vec)
9539 || vectorizable_induction (stmt, NULL, NULL, node, cost_vec)
9540 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node, cost_vec)
9541 || vectorizable_comparison (stmt, NULL, NULL, NULL, node, cost_vec));
b1af7da6
RB
9542 else
9543 {
9544 if (bb_vinfo)
68435eb2
RB
9545 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node, cost_vec)
9546 || vectorizable_conversion (stmt, NULL, NULL, node, cost_vec)
9547 || vectorizable_shift (stmt, NULL, NULL, node, cost_vec)
9548 || vectorizable_operation (stmt, NULL, NULL, node, cost_vec)
9549 || vectorizable_assignment (stmt, NULL, NULL, node, cost_vec)
9550 || vectorizable_load (stmt, NULL, NULL, node, node_instance,
9551 cost_vec)
9552 || vectorizable_call (stmt, NULL, NULL, node, cost_vec)
9553 || vectorizable_store (stmt, NULL, NULL, node, cost_vec)
9554 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node,
9555 cost_vec)
9556 || vectorizable_comparison (stmt, NULL, NULL, NULL, node,
9557 cost_vec));
b1af7da6 9558 }
8644a673
IR
9559
9560 if (!ok)
ebfd146a 9561 {
73fbfcad 9562 if (dump_enabled_p ())
8644a673 9563 {
78c60e3d
SS
9564 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9565 "not vectorized: relevant stmt not ");
9566 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
9567 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8644a673 9568 }
b8698a0f 9569
ebfd146a
IR
9570 return false;
9571 }
9572
8644a673
IR
9573 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
9574 need extra handling, except for vectorizable reductions. */
68435eb2
RB
9575 if (!bb_vinfo
9576 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
9577 && !can_vectorize_live_stmts (stmt, NULL, node, NULL, cost_vec))
ebfd146a 9578 {
73fbfcad 9579 if (dump_enabled_p ())
8644a673 9580 {
78c60e3d 9581 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
68a0f2ff 9582 "not vectorized: live stmt not supported: ");
78c60e3d 9583 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8644a673 9584 }
b8698a0f 9585
8644a673 9586 return false;
ebfd146a
IR
9587 }
9588
ebfd146a
IR
9589 return true;
9590}
9591
9592
9593/* Function vect_transform_stmt.
9594
9595 Create a vectorized stmt to replace STMT, and insert it at BSI. */
9596
9597bool
355fe088 9598vect_transform_stmt (gimple *stmt, gimple_stmt_iterator *gsi,
0d0293ac 9599 bool *grouped_store, slp_tree slp_node,
ebfd146a
IR
9600 slp_instance slp_node_instance)
9601{
6585ff8f
RS
9602 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
9603 vec_info *vinfo = stmt_info->vinfo;
ebfd146a 9604 bool is_store = false;
355fe088 9605 gimple *vec_stmt = NULL;
ebfd146a 9606 bool done;
ebfd146a 9607
fce57248 9608 gcc_assert (slp_node || !PURE_SLP_STMT (stmt_info));
355fe088 9609 gimple *old_vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
225ce44b 9610
e57d9a82
RB
9611 bool nested_p = (STMT_VINFO_LOOP_VINFO (stmt_info)
9612 && nested_in_vect_loop_p
9613 (LOOP_VINFO_LOOP (STMT_VINFO_LOOP_VINFO (stmt_info)),
9614 stmt));
9615
ebfd146a
IR
9616 switch (STMT_VINFO_TYPE (stmt_info))
9617 {
9618 case type_demotion_vec_info_type:
ebfd146a 9619 case type_promotion_vec_info_type:
ebfd146a 9620 case type_conversion_vec_info_type:
68435eb2 9621 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node, NULL);
ebfd146a
IR
9622 gcc_assert (done);
9623 break;
9624
9625 case induc_vec_info_type:
68435eb2 9626 done = vectorizable_induction (stmt, gsi, &vec_stmt, slp_node, NULL);
ebfd146a
IR
9627 gcc_assert (done);
9628 break;
9629
9dc3f7de 9630 case shift_vec_info_type:
68435eb2 9631 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node, NULL);
9dc3f7de
IR
9632 gcc_assert (done);
9633 break;
9634
ebfd146a 9635 case op_vec_info_type:
68435eb2 9636 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node, NULL);
ebfd146a
IR
9637 gcc_assert (done);
9638 break;
9639
9640 case assignment_vec_info_type:
68435eb2 9641 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node, NULL);
ebfd146a
IR
9642 gcc_assert (done);
9643 break;
9644
9645 case load_vec_info_type:
b8698a0f 9646 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
68435eb2 9647 slp_node_instance, NULL);
ebfd146a
IR
9648 gcc_assert (done);
9649 break;
9650
9651 case store_vec_info_type:
68435eb2 9652 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node, NULL);
ebfd146a 9653 gcc_assert (done);
0d0293ac 9654 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
ebfd146a
IR
9655 {
9656 /* In case of interleaving, the whole chain is vectorized when the
ff802fa1 9657 last store in the chain is reached. Store stmts before the last
ebfd146a
IR
9658 one are skipped, and there vec_stmt_info shouldn't be freed
9659 meanwhile. */
0d0293ac 9660 *grouped_store = true;
f307441a 9661 stmt_vec_info group_info
2c53b149
RB
9662 = vinfo_for_stmt (DR_GROUP_FIRST_ELEMENT (stmt_info));
9663 if (DR_GROUP_STORE_COUNT (group_info) == DR_GROUP_SIZE (group_info))
ebfd146a 9664 is_store = true;
f307441a 9665 }
ebfd146a
IR
9666 else
9667 is_store = true;
9668 break;
9669
9670 case condition_vec_info_type:
68435eb2 9671 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node, NULL);
ebfd146a
IR
9672 gcc_assert (done);
9673 break;
9674
42fd8198 9675 case comparison_vec_info_type:
68435eb2 9676 done = vectorizable_comparison (stmt, gsi, &vec_stmt, NULL, slp_node, NULL);
42fd8198
IE
9677 gcc_assert (done);
9678 break;
9679
ebfd146a 9680 case call_vec_info_type:
68435eb2 9681 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node, NULL);
039d9ea1 9682 stmt = gsi_stmt (*gsi);
ebfd146a
IR
9683 break;
9684
0136f8f0 9685 case call_simd_clone_vec_info_type:
68435eb2 9686 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node, NULL);
0136f8f0
AH
9687 stmt = gsi_stmt (*gsi);
9688 break;
9689
ebfd146a 9690 case reduc_vec_info_type:
891ad31c 9691 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node,
68435eb2 9692 slp_node_instance, NULL);
ebfd146a
IR
9693 gcc_assert (done);
9694 break;
9695
9696 default:
9697 if (!STMT_VINFO_LIVE_P (stmt_info))
9698 {
73fbfcad 9699 if (dump_enabled_p ())
78c60e3d 9700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 9701 "stmt not supported.\n");
ebfd146a
IR
9702 gcc_unreachable ();
9703 }
9704 }
9705
225ce44b
RB
9706 /* Verify SLP vectorization doesn't mess with STMT_VINFO_VEC_STMT.
9707 This would break hybrid SLP vectorization. */
9708 if (slp_node)
d90f8440
RB
9709 gcc_assert (!vec_stmt
9710 && STMT_VINFO_VEC_STMT (stmt_info) == old_vec_stmt);
225ce44b 9711
ebfd146a
IR
9712 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
9713 is being vectorized, but outside the immediately enclosing loop. */
9714 if (vec_stmt
e57d9a82 9715 && nested_p
ebfd146a
IR
9716 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
9717 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
b8698a0f 9718 || STMT_VINFO_RELEVANT (stmt_info) ==
a70d6342 9719 vect_used_in_outer_by_reduction))
ebfd146a 9720 {
a70d6342
IR
9721 struct loop *innerloop = LOOP_VINFO_LOOP (
9722 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
ebfd146a
IR
9723 imm_use_iterator imm_iter;
9724 use_operand_p use_p;
9725 tree scalar_dest;
ebfd146a 9726
73fbfcad 9727 if (dump_enabled_p ())
78c60e3d 9728 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 9729 "Record the vdef for outer-loop vectorization.\n");
ebfd146a
IR
9730
9731 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
9732 (to be used when vectorizing outer-loop stmts that use the DEF of
9733 STMT). */
9734 if (gimple_code (stmt) == GIMPLE_PHI)
9735 scalar_dest = PHI_RESULT (stmt);
9736 else
9737 scalar_dest = gimple_assign_lhs (stmt);
9738
9739 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
6585ff8f
RS
9740 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
9741 {
9742 stmt_vec_info exit_phi_info
9743 = vinfo->lookup_stmt (USE_STMT (use_p));
9744 STMT_VINFO_VEC_STMT (exit_phi_info) = vec_stmt;
9745 }
ebfd146a
IR
9746 }
9747
9748 /* Handle stmts whose DEF is used outside the loop-nest that is
9749 being vectorized. */
68a0f2ff 9750 if (STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
ebfd146a 9751 {
68435eb2 9752 done = can_vectorize_live_stmts (stmt, gsi, slp_node, &vec_stmt, NULL);
ebfd146a
IR
9753 gcc_assert (done);
9754 }
9755
9756 if (vec_stmt)
83197f37 9757 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
ebfd146a 9758
b8698a0f 9759 return is_store;
ebfd146a
IR
9760}
9761
9762
b8698a0f 9763/* Remove a group of stores (for SLP or interleaving), free their
ebfd146a
IR
9764 stmt_vec_info. */
9765
9766void
355fe088 9767vect_remove_stores (gimple *first_stmt)
ebfd146a 9768{
355fe088
TS
9769 gimple *next = first_stmt;
9770 gimple *tmp;
ebfd146a
IR
9771 gimple_stmt_iterator next_si;
9772
9773 while (next)
9774 {
78048b1c
JJ
9775 stmt_vec_info stmt_info = vinfo_for_stmt (next);
9776
2c53b149 9777 tmp = DR_GROUP_NEXT_ELEMENT (stmt_info);
78048b1c
JJ
9778 if (is_pattern_stmt_p (stmt_info))
9779 next = STMT_VINFO_RELATED_STMT (stmt_info);
ebfd146a
IR
9780 /* Free the attached stmt_vec_info and remove the stmt. */
9781 next_si = gsi_for_stmt (next);
3d3f2249 9782 unlink_stmt_vdef (next);
ebfd146a 9783 gsi_remove (&next_si, true);
3d3f2249 9784 release_defs (next);
ebfd146a
IR
9785 free_stmt_vec_info (next);
9786 next = tmp;
9787 }
9788}
9789
9790
9791/* Function new_stmt_vec_info.
9792
9793 Create and initialize a new stmt_vec_info struct for STMT. */
9794
9795stmt_vec_info
310213d4 9796new_stmt_vec_info (gimple *stmt, vec_info *vinfo)
ebfd146a
IR
9797{
9798 stmt_vec_info res;
dbe1b846 9799 res = (_stmt_vec_info *) xcalloc (1, sizeof (struct _stmt_vec_info));
ebfd146a
IR
9800
9801 STMT_VINFO_TYPE (res) = undef_vec_info_type;
9802 STMT_VINFO_STMT (res) = stmt;
310213d4 9803 res->vinfo = vinfo;
8644a673 9804 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
ebfd146a
IR
9805 STMT_VINFO_LIVE_P (res) = false;
9806 STMT_VINFO_VECTYPE (res) = NULL;
9807 STMT_VINFO_VEC_STMT (res) = NULL;
4b5caab7 9808 STMT_VINFO_VECTORIZABLE (res) = true;
ebfd146a
IR
9809 STMT_VINFO_IN_PATTERN_P (res) = false;
9810 STMT_VINFO_RELATED_STMT (res) = NULL;
363477c0 9811 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
ebfd146a 9812 STMT_VINFO_DATA_REF (res) = NULL;
af29617a 9813 STMT_VINFO_VEC_REDUCTION_TYPE (res) = TREE_CODE_REDUCTION;
7e16ce79 9814 STMT_VINFO_VEC_CONST_COND_REDUC_CODE (res) = ERROR_MARK;
ebfd146a 9815
ebfd146a
IR
9816 if (gimple_code (stmt) == GIMPLE_PHI
9817 && is_loop_header_bb_p (gimple_bb (stmt)))
9818 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
9819 else
8644a673
IR
9820 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
9821
9771b263 9822 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
32e8bb8e 9823 STMT_SLP_TYPE (res) = loop_vect;
78810bd3
RB
9824 STMT_VINFO_NUM_SLP_USES (res) = 0;
9825
2c53b149
RB
9826 res->first_element = NULL; /* GROUP_FIRST_ELEMENT */
9827 res->next_element = NULL; /* GROUP_NEXT_ELEMENT */
9828 res->size = 0; /* GROUP_SIZE */
9829 res->store_count = 0; /* GROUP_STORE_COUNT */
9830 res->gap = 0; /* GROUP_GAP */
9831 res->same_dr_stmt = NULL; /* GROUP_SAME_DR_STMT */
ebfd146a 9832
ca823c85
RB
9833 /* This is really "uninitialized" until vect_compute_data_ref_alignment. */
9834 res->dr_aux.misalignment = DR_MISALIGNMENT_UNINITIALIZED;
9835
ebfd146a
IR
9836 return res;
9837}
9838
9839
f8c0baaf 9840/* Set the current stmt_vec_info vector to V. */
ebfd146a
IR
9841
9842void
f8c0baaf 9843set_stmt_vec_info_vec (vec<stmt_vec_info> *v)
ebfd146a 9844{
f8c0baaf 9845 stmt_vec_info_vec = v;
ebfd146a
IR
9846}
9847
f8c0baaf 9848/* Free the stmt_vec_info entries in V and release V. */
ebfd146a
IR
9849
9850void
f8c0baaf 9851free_stmt_vec_infos (vec<stmt_vec_info> *v)
ebfd146a 9852{
93675444 9853 unsigned int i;
3161455c 9854 stmt_vec_info info;
f8c0baaf 9855 FOR_EACH_VEC_ELT (*v, i, info)
dbe1b846 9856 if (info != NULL_STMT_VEC_INFO)
3161455c 9857 free_stmt_vec_info (STMT_VINFO_STMT (info));
f8c0baaf
RB
9858 if (v == stmt_vec_info_vec)
9859 stmt_vec_info_vec = NULL;
9860 v->release ();
ebfd146a
IR
9861}
9862
9863
9864/* Free stmt vectorization related info. */
9865
9866void
355fe088 9867free_stmt_vec_info (gimple *stmt)
ebfd146a
IR
9868{
9869 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
9870
9871 if (!stmt_info)
9872 return;
9873
78048b1c
JJ
9874 /* Check if this statement has a related "pattern stmt"
9875 (introduced by the vectorizer during the pattern recognition
9876 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
9877 too. */
9878 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
9879 {
e3947d80
RS
9880 if (gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info))
9881 for (gimple_stmt_iterator si = gsi_start (seq);
9882 !gsi_end_p (si); gsi_next (&si))
9883 {
9884 gimple *seq_stmt = gsi_stmt (si);
9885 gimple_set_bb (seq_stmt, NULL);
9886 tree lhs = gimple_get_lhs (seq_stmt);
9887 if (lhs && TREE_CODE (lhs) == SSA_NAME)
9888 release_ssa_name (lhs);
9889 free_stmt_vec_info (seq_stmt);
9890 }
78048b1c
JJ
9891 stmt_vec_info patt_info
9892 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
9893 if (patt_info)
9894 {
355fe088 9895 gimple *patt_stmt = STMT_VINFO_STMT (patt_info);
f0281fde
RB
9896 gimple_set_bb (patt_stmt, NULL);
9897 tree lhs = gimple_get_lhs (patt_stmt);
e6f5c25d 9898 if (lhs && TREE_CODE (lhs) == SSA_NAME)
f0281fde 9899 release_ssa_name (lhs);
f0281fde 9900 free_stmt_vec_info (patt_stmt);
78048b1c
JJ
9901 }
9902 }
9903
9771b263 9904 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
6c9e85fb 9905 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
ebfd146a
IR
9906 set_vinfo_for_stmt (stmt, NULL);
9907 free (stmt_info);
9908}
9909
9910
bb67d9c7 9911/* Function get_vectype_for_scalar_type_and_size.
ebfd146a 9912
bb67d9c7 9913 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
ebfd146a
IR
9914 by the target. */
9915
c803b2a9 9916tree
86e36728 9917get_vectype_for_scalar_type_and_size (tree scalar_type, poly_uint64 size)
ebfd146a 9918{
c7d97b28 9919 tree orig_scalar_type = scalar_type;
3bd8f481 9920 scalar_mode inner_mode;
ef4bddc2 9921 machine_mode simd_mode;
86e36728 9922 poly_uint64 nunits;
ebfd146a
IR
9923 tree vectype;
9924
3bd8f481
RS
9925 if (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
9926 && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode))
ebfd146a
IR
9927 return NULL_TREE;
9928
3bd8f481 9929 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
48f2e373 9930
7b7b1813
RG
9931 /* For vector types of elements whose mode precision doesn't
9932 match their types precision we use a element type of mode
9933 precision. The vectorization routines will have to make sure
48f2e373
RB
9934 they support the proper result truncation/extension.
9935 We also make sure to build vector types with INTEGER_TYPE
9936 component type only. */
6d7971b8 9937 if (INTEGRAL_TYPE_P (scalar_type)
48f2e373
RB
9938 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
9939 || TREE_CODE (scalar_type) != INTEGER_TYPE))
7b7b1813
RG
9940 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
9941 TYPE_UNSIGNED (scalar_type));
6d7971b8 9942
ccbf5bb4
RG
9943 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
9944 When the component mode passes the above test simply use a type
9945 corresponding to that mode. The theory is that any use that
9946 would cause problems with this will disable vectorization anyway. */
dfc2e2ac 9947 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
e67f39f7 9948 && !INTEGRAL_TYPE_P (scalar_type))
60b95d28
RB
9949 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
9950
9951 /* We can't build a vector type of elements with alignment bigger than
9952 their size. */
dfc2e2ac 9953 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
aca43c6c
JJ
9954 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
9955 TYPE_UNSIGNED (scalar_type));
ccbf5bb4 9956
dfc2e2ac
RB
9957 /* If we felt back to using the mode fail if there was
9958 no scalar type for it. */
9959 if (scalar_type == NULL_TREE)
9960 return NULL_TREE;
9961
bb67d9c7
RG
9962 /* If no size was supplied use the mode the target prefers. Otherwise
9963 lookup a vector mode of the specified size. */
86e36728 9964 if (known_eq (size, 0U))
bb67d9c7 9965 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
86e36728
RS
9966 else if (!multiple_p (size, nbytes, &nunits)
9967 || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
9da15d40 9968 return NULL_TREE;
4c8fd8ac 9969 /* NOTE: nunits == 1 is allowed to support single element vector types. */
86e36728 9970 if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits))
cc4b5170 9971 return NULL_TREE;
ebfd146a
IR
9972
9973 vectype = build_vector_type (scalar_type, nunits);
ebfd146a
IR
9974
9975 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
9976 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
451dabda 9977 return NULL_TREE;
ebfd146a 9978
c7d97b28
RB
9979 /* Re-attach the address-space qualifier if we canonicalized the scalar
9980 type. */
9981 if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
9982 return build_qualified_type
9983 (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
9984
ebfd146a
IR
9985 return vectype;
9986}
9987
86e36728 9988poly_uint64 current_vector_size;
bb67d9c7
RG
9989
9990/* Function get_vectype_for_scalar_type.
9991
9992 Returns the vector type corresponding to SCALAR_TYPE as supported
9993 by the target. */
9994
9995tree
9996get_vectype_for_scalar_type (tree scalar_type)
9997{
9998 tree vectype;
9999 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
10000 current_vector_size);
10001 if (vectype
86e36728 10002 && known_eq (current_vector_size, 0U))
bb67d9c7
RG
10003 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
10004 return vectype;
10005}
10006
42fd8198
IE
10007/* Function get_mask_type_for_scalar_type.
10008
10009 Returns the mask type corresponding to a result of comparison
10010 of vectors of specified SCALAR_TYPE as supported by target. */
10011
10012tree
10013get_mask_type_for_scalar_type (tree scalar_type)
10014{
10015 tree vectype = get_vectype_for_scalar_type (scalar_type);
10016
10017 if (!vectype)
10018 return NULL;
10019
10020 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype),
10021 current_vector_size);
10022}
10023
b690cc0f
RG
10024/* Function get_same_sized_vectype
10025
10026 Returns a vector type corresponding to SCALAR_TYPE of size
10027 VECTOR_TYPE if supported by the target. */
10028
10029tree
bb67d9c7 10030get_same_sized_vectype (tree scalar_type, tree vector_type)
b690cc0f 10031{
2568d8a1 10032 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
9f47c7e5
IE
10033 return build_same_sized_truth_vector_type (vector_type);
10034
bb67d9c7
RG
10035 return get_vectype_for_scalar_type_and_size
10036 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
b690cc0f
RG
10037}
10038
ebfd146a
IR
10039/* Function vect_is_simple_use.
10040
10041 Input:
81c40241
RB
10042 VINFO - the vect info of the loop or basic block that is being vectorized.
10043 OPERAND - operand in the loop or bb.
10044 Output:
fef96d8e
RS
10045 DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
10046 case OPERAND is an SSA_NAME that is defined in the vectorizable region
10047 DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
10048 the definition could be anywhere in the function
81c40241 10049 DT - the type of definition
ebfd146a
IR
10050
10051 Returns whether a stmt with OPERAND can be vectorized.
b8698a0f 10052 For loops, supportable operands are constants, loop invariants, and operands
ff802fa1 10053 that are defined by the current iteration of the loop. Unsupportable
b8698a0f 10054 operands are those that are defined by a previous iteration of the loop (as
a70d6342
IR
10055 is the case in reduction/induction computations).
10056 For basic blocks, supportable operands are constants and bb invariants.
10057 For now, operands defined outside the basic block are not supported. */
ebfd146a
IR
10058
10059bool
894dd753 10060vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
fef96d8e 10061 stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
b8698a0f 10062{
fef96d8e
RS
10063 if (def_stmt_info_out)
10064 *def_stmt_info_out = NULL;
894dd753
RS
10065 if (def_stmt_out)
10066 *def_stmt_out = NULL;
3fc356dc 10067 *dt = vect_unknown_def_type;
b8698a0f 10068
73fbfcad 10069 if (dump_enabled_p ())
ebfd146a 10070 {
78c60e3d
SS
10071 dump_printf_loc (MSG_NOTE, vect_location,
10072 "vect_is_simple_use: operand ");
30f502ed
RB
10073 if (TREE_CODE (operand) == SSA_NAME
10074 && !SSA_NAME_IS_DEFAULT_DEF (operand))
10075 dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
10076 else
10077 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
ebfd146a 10078 }
b8698a0f 10079
b758f602 10080 if (CONSTANT_CLASS_P (operand))
30f502ed
RB
10081 *dt = vect_constant_def;
10082 else if (is_gimple_min_invariant (operand))
10083 *dt = vect_external_def;
10084 else if (TREE_CODE (operand) != SSA_NAME)
10085 *dt = vect_unknown_def_type;
10086 else if (SSA_NAME_IS_DEFAULT_DEF (operand))
8644a673 10087 *dt = vect_external_def;
ebfd146a
IR
10088 else
10089 {
30f502ed 10090 gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
c98d0595
RS
10091 stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
10092 if (!stmt_vinfo)
30f502ed
RB
10093 *dt = vect_external_def;
10094 else
0f8c840c 10095 {
30f502ed
RB
10096 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
10097 {
10098 def_stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
10099 stmt_vinfo = vinfo_for_stmt (def_stmt);
10100 }
10101 switch (gimple_code (def_stmt))
10102 {
10103 case GIMPLE_PHI:
10104 case GIMPLE_ASSIGN:
10105 case GIMPLE_CALL:
10106 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
10107 break;
10108 default:
10109 *dt = vect_unknown_def_type;
10110 break;
10111 }
fef96d8e
RS
10112 if (def_stmt_info_out)
10113 *def_stmt_info_out = stmt_vinfo;
0f8c840c 10114 }
30f502ed
RB
10115 if (def_stmt_out)
10116 *def_stmt_out = def_stmt;
ebfd146a
IR
10117 }
10118
2e8ab70c
RB
10119 if (dump_enabled_p ())
10120 {
30f502ed 10121 dump_printf (MSG_NOTE, ", type of def: ");
2e8ab70c
RB
10122 switch (*dt)
10123 {
10124 case vect_uninitialized_def:
10125 dump_printf (MSG_NOTE, "uninitialized\n");
10126 break;
10127 case vect_constant_def:
10128 dump_printf (MSG_NOTE, "constant\n");
10129 break;
10130 case vect_external_def:
10131 dump_printf (MSG_NOTE, "external\n");
10132 break;
10133 case vect_internal_def:
10134 dump_printf (MSG_NOTE, "internal\n");
10135 break;
10136 case vect_induction_def:
10137 dump_printf (MSG_NOTE, "induction\n");
10138 break;
10139 case vect_reduction_def:
10140 dump_printf (MSG_NOTE, "reduction\n");
10141 break;
10142 case vect_double_reduction_def:
10143 dump_printf (MSG_NOTE, "double reduction\n");
10144 break;
10145 case vect_nested_cycle:
10146 dump_printf (MSG_NOTE, "nested cycle\n");
10147 break;
10148 case vect_unknown_def_type:
10149 dump_printf (MSG_NOTE, "unknown\n");
10150 break;
10151 }
10152 }
10153
81c40241 10154 if (*dt == vect_unknown_def_type)
ebfd146a 10155 {
73fbfcad 10156 if (dump_enabled_p ())
78c60e3d 10157 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 10158 "Unsupported pattern.\n");
ebfd146a
IR
10159 return false;
10160 }
10161
ebfd146a
IR
10162 return true;
10163}
10164
81c40241 10165/* Function vect_is_simple_use.
b690cc0f 10166
81c40241 10167 Same as vect_is_simple_use but also determines the vector operand
b690cc0f
RG
10168 type of OPERAND and stores it to *VECTYPE. If the definition of
10169 OPERAND is vect_uninitialized_def, vect_constant_def or
10170 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
10171 is responsible to compute the best suited vector type for the
10172 scalar operand. */
10173
10174bool
894dd753 10175vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
fef96d8e
RS
10176 tree *vectype, stmt_vec_info *def_stmt_info_out,
10177 gimple **def_stmt_out)
b690cc0f 10178{
fef96d8e 10179 stmt_vec_info def_stmt_info;
894dd753 10180 gimple *def_stmt;
fef96d8e 10181 if (!vect_is_simple_use (operand, vinfo, dt, &def_stmt_info, &def_stmt))
b690cc0f
RG
10182 return false;
10183
894dd753
RS
10184 if (def_stmt_out)
10185 *def_stmt_out = def_stmt;
fef96d8e
RS
10186 if (def_stmt_info_out)
10187 *def_stmt_info_out = def_stmt_info;
894dd753 10188
b690cc0f
RG
10189 /* Now get a vector type if the def is internal, otherwise supply
10190 NULL_TREE and leave it up to the caller to figure out a proper
10191 type for the use stmt. */
10192 if (*dt == vect_internal_def
10193 || *dt == vect_induction_def
10194 || *dt == vect_reduction_def
10195 || *dt == vect_double_reduction_def
10196 || *dt == vect_nested_cycle)
10197 {
fef96d8e 10198 *vectype = STMT_VINFO_VECTYPE (def_stmt_info);
b690cc0f 10199 gcc_assert (*vectype != NULL_TREE);
30f502ed
RB
10200 if (dump_enabled_p ())
10201 {
10202 dump_printf_loc (MSG_NOTE, vect_location,
10203 "vect_is_simple_use: vectype ");
10204 dump_generic_expr (MSG_NOTE, TDF_SLIM, *vectype);
10205 dump_printf (MSG_NOTE, "\n");
10206 }
b690cc0f
RG
10207 }
10208 else if (*dt == vect_uninitialized_def
10209 || *dt == vect_constant_def
10210 || *dt == vect_external_def)
10211 *vectype = NULL_TREE;
10212 else
10213 gcc_unreachable ();
10214
10215 return true;
10216}
10217
ebfd146a
IR
10218
10219/* Function supportable_widening_operation
10220
b8698a0f
L
10221 Check whether an operation represented by the code CODE is a
10222 widening operation that is supported by the target platform in
b690cc0f
RG
10223 vector form (i.e., when operating on arguments of type VECTYPE_IN
10224 producing a result of type VECTYPE_OUT).
b8698a0f 10225
1bda738b
JJ
10226 Widening operations we currently support are NOP (CONVERT), FLOAT,
10227 FIX_TRUNC and WIDEN_MULT. This function checks if these operations
10228 are supported by the target platform either directly (via vector
10229 tree-codes), or via target builtins.
ebfd146a
IR
10230
10231 Output:
b8698a0f
L
10232 - CODE1 and CODE2 are codes of vector operations to be used when
10233 vectorizing the operation, if available.
ebfd146a
IR
10234 - MULTI_STEP_CVT determines the number of required intermediate steps in
10235 case of multi-step conversion (like char->short->int - in that case
10236 MULTI_STEP_CVT will be 1).
b8698a0f
L
10237 - INTERM_TYPES contains the intermediate type required to perform the
10238 widening operation (short in the above example). */
ebfd146a
IR
10239
10240bool
355fe088 10241supportable_widening_operation (enum tree_code code, gimple *stmt,
b690cc0f 10242 tree vectype_out, tree vectype_in,
ebfd146a
IR
10243 enum tree_code *code1, enum tree_code *code2,
10244 int *multi_step_cvt,
9771b263 10245 vec<tree> *interm_types)
ebfd146a
IR
10246{
10247 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
10248 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
4ef69dfc 10249 struct loop *vect_loop = NULL;
ef4bddc2 10250 machine_mode vec_mode;
81f40b79 10251 enum insn_code icode1, icode2;
ebfd146a 10252 optab optab1, optab2;
b690cc0f
RG
10253 tree vectype = vectype_in;
10254 tree wide_vectype = vectype_out;
ebfd146a 10255 enum tree_code c1, c2;
4a00c761
JJ
10256 int i;
10257 tree prev_type, intermediate_type;
ef4bddc2 10258 machine_mode intermediate_mode, prev_mode;
4a00c761 10259 optab optab3, optab4;
ebfd146a 10260
4a00c761 10261 *multi_step_cvt = 0;
4ef69dfc
IR
10262 if (loop_info)
10263 vect_loop = LOOP_VINFO_LOOP (loop_info);
10264
ebfd146a
IR
10265 switch (code)
10266 {
10267 case WIDEN_MULT_EXPR:
6ae6116f
RH
10268 /* The result of a vectorized widening operation usually requires
10269 two vectors (because the widened results do not fit into one vector).
10270 The generated vector results would normally be expected to be
10271 generated in the same order as in the original scalar computation,
10272 i.e. if 8 results are generated in each vector iteration, they are
10273 to be organized as follows:
10274 vect1: [res1,res2,res3,res4],
10275 vect2: [res5,res6,res7,res8].
10276
10277 However, in the special case that the result of the widening
10278 operation is used in a reduction computation only, the order doesn't
10279 matter (because when vectorizing a reduction we change the order of
10280 the computation). Some targets can take advantage of this and
10281 generate more efficient code. For example, targets like Altivec,
10282 that support widen_mult using a sequence of {mult_even,mult_odd}
10283 generate the following vectors:
10284 vect1: [res1,res3,res5,res7],
10285 vect2: [res2,res4,res6,res8].
10286
10287 When vectorizing outer-loops, we execute the inner-loop sequentially
10288 (each vectorized inner-loop iteration contributes to VF outer-loop
10289 iterations in parallel). We therefore don't allow to change the
10290 order of the computation in the inner-loop during outer-loop
10291 vectorization. */
10292 /* TODO: Another case in which order doesn't *really* matter is when we
10293 widen and then contract again, e.g. (short)((int)x * y >> 8).
10294 Normally, pack_trunc performs an even/odd permute, whereas the
10295 repack from an even/odd expansion would be an interleave, which
10296 would be significantly simpler for e.g. AVX2. */
10297 /* In any case, in order to avoid duplicating the code below, recurse
10298 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
10299 are properly set up for the caller. If we fail, we'll continue with
10300 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
10301 if (vect_loop
10302 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
10303 && !nested_in_vect_loop_p (vect_loop, stmt)
10304 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
10305 stmt, vectype_out, vectype_in,
a86ec597
RH
10306 code1, code2, multi_step_cvt,
10307 interm_types))
ebc047a2
CH
10308 {
10309 /* Elements in a vector with vect_used_by_reduction property cannot
10310 be reordered if the use chain with this property does not have the
10311 same operation. One such an example is s += a * b, where elements
10312 in a and b cannot be reordered. Here we check if the vector defined
10313 by STMT is only directly used in the reduction statement. */
0d0a4e20
RS
10314 tree lhs = gimple_assign_lhs (stmt);
10315 stmt_vec_info use_stmt_info = loop_info->lookup_single_use (lhs);
10316 if (use_stmt_info
10317 && STMT_VINFO_DEF_TYPE (use_stmt_info) == vect_reduction_def)
10318 return true;
ebc047a2 10319 }
4a00c761
JJ
10320 c1 = VEC_WIDEN_MULT_LO_EXPR;
10321 c2 = VEC_WIDEN_MULT_HI_EXPR;
ebfd146a
IR
10322 break;
10323
81c40241
RB
10324 case DOT_PROD_EXPR:
10325 c1 = DOT_PROD_EXPR;
10326 c2 = DOT_PROD_EXPR;
10327 break;
10328
10329 case SAD_EXPR:
10330 c1 = SAD_EXPR;
10331 c2 = SAD_EXPR;
10332 break;
10333
6ae6116f
RH
10334 case VEC_WIDEN_MULT_EVEN_EXPR:
10335 /* Support the recursion induced just above. */
10336 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
10337 c2 = VEC_WIDEN_MULT_ODD_EXPR;
10338 break;
10339
36ba4aae 10340 case WIDEN_LSHIFT_EXPR:
4a00c761
JJ
10341 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
10342 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
36ba4aae
IR
10343 break;
10344
ebfd146a 10345 CASE_CONVERT:
4a00c761
JJ
10346 c1 = VEC_UNPACK_LO_EXPR;
10347 c2 = VEC_UNPACK_HI_EXPR;
ebfd146a
IR
10348 break;
10349
10350 case FLOAT_EXPR:
4a00c761
JJ
10351 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
10352 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
ebfd146a
IR
10353 break;
10354
10355 case FIX_TRUNC_EXPR:
1bda738b
JJ
10356 c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
10357 c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
10358 break;
ebfd146a
IR
10359
10360 default:
10361 gcc_unreachable ();
10362 }
10363
6ae6116f 10364 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
6b4db501 10365 std::swap (c1, c2);
4a00c761 10366
ebfd146a
IR
10367 if (code == FIX_TRUNC_EXPR)
10368 {
10369 /* The signedness is determined from output operand. */
b690cc0f
RG
10370 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
10371 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
ebfd146a
IR
10372 }
10373 else
10374 {
10375 optab1 = optab_for_tree_code (c1, vectype, optab_default);
10376 optab2 = optab_for_tree_code (c2, vectype, optab_default);
10377 }
10378
10379 if (!optab1 || !optab2)
10380 return false;
10381
10382 vec_mode = TYPE_MODE (vectype);
947131ba
RS
10383 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
10384 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
ebfd146a
IR
10385 return false;
10386
4a00c761
JJ
10387 *code1 = c1;
10388 *code2 = c2;
10389
10390 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
10391 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
5e8d6dff
IE
10392 /* For scalar masks we may have different boolean
10393 vector types having the same QImode. Thus we
10394 add additional check for elements number. */
10395 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
928686b1
RS
10396 || known_eq (TYPE_VECTOR_SUBPARTS (vectype),
10397 TYPE_VECTOR_SUBPARTS (wide_vectype) * 2));
4a00c761 10398
b8698a0f 10399 /* Check if it's a multi-step conversion that can be done using intermediate
ebfd146a 10400 types. */
ebfd146a 10401
4a00c761
JJ
10402 prev_type = vectype;
10403 prev_mode = vec_mode;
b8698a0f 10404
4a00c761
JJ
10405 if (!CONVERT_EXPR_CODE_P (code))
10406 return false;
b8698a0f 10407
4a00c761
JJ
10408 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
10409 intermediate steps in promotion sequence. We try
10410 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
10411 not. */
9771b263 10412 interm_types->create (MAX_INTERM_CVT_STEPS);
4a00c761
JJ
10413 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
10414 {
10415 intermediate_mode = insn_data[icode1].operand[0].mode;
3ae0661a
IE
10416 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
10417 {
7cfb4d93 10418 intermediate_type = vect_halve_mask_nunits (prev_type);
3ae0661a
IE
10419 if (intermediate_mode != TYPE_MODE (intermediate_type))
10420 return false;
10421 }
10422 else
10423 intermediate_type
10424 = lang_hooks.types.type_for_mode (intermediate_mode,
10425 TYPE_UNSIGNED (prev_type));
10426
4a00c761
JJ
10427 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
10428 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
10429
10430 if (!optab3 || !optab4
10431 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
10432 || insn_data[icode1].operand[0].mode != intermediate_mode
10433 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
10434 || insn_data[icode2].operand[0].mode != intermediate_mode
10435 || ((icode1 = optab_handler (optab3, intermediate_mode))
10436 == CODE_FOR_nothing)
10437 || ((icode2 = optab_handler (optab4, intermediate_mode))
10438 == CODE_FOR_nothing))
10439 break;
ebfd146a 10440
9771b263 10441 interm_types->quick_push (intermediate_type);
4a00c761
JJ
10442 (*multi_step_cvt)++;
10443
10444 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
10445 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
5e8d6dff 10446 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
928686b1
RS
10447 || known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
10448 TYPE_VECTOR_SUBPARTS (wide_vectype) * 2));
4a00c761
JJ
10449
10450 prev_type = intermediate_type;
10451 prev_mode = intermediate_mode;
ebfd146a
IR
10452 }
10453
9771b263 10454 interm_types->release ();
4a00c761 10455 return false;
ebfd146a
IR
10456}
10457
10458
10459/* Function supportable_narrowing_operation
10460
b8698a0f
L
10461 Check whether an operation represented by the code CODE is a
10462 narrowing operation that is supported by the target platform in
b690cc0f
RG
10463 vector form (i.e., when operating on arguments of type VECTYPE_IN
10464 and producing a result of type VECTYPE_OUT).
b8698a0f 10465
1bda738b
JJ
10466 Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
10467 and FLOAT. This function checks if these operations are supported by
ebfd146a
IR
10468 the target platform directly via vector tree-codes.
10469
10470 Output:
b8698a0f
L
10471 - CODE1 is the code of a vector operation to be used when
10472 vectorizing the operation, if available.
ebfd146a
IR
10473 - MULTI_STEP_CVT determines the number of required intermediate steps in
10474 case of multi-step conversion (like int->short->char - in that case
10475 MULTI_STEP_CVT will be 1).
10476 - INTERM_TYPES contains the intermediate type required to perform the
b8698a0f 10477 narrowing operation (short in the above example). */
ebfd146a
IR
10478
10479bool
10480supportable_narrowing_operation (enum tree_code code,
b690cc0f 10481 tree vectype_out, tree vectype_in,
ebfd146a 10482 enum tree_code *code1, int *multi_step_cvt,
9771b263 10483 vec<tree> *interm_types)
ebfd146a 10484{
ef4bddc2 10485 machine_mode vec_mode;
ebfd146a
IR
10486 enum insn_code icode1;
10487 optab optab1, interm_optab;
b690cc0f
RG
10488 tree vectype = vectype_in;
10489 tree narrow_vectype = vectype_out;
ebfd146a 10490 enum tree_code c1;
3ae0661a 10491 tree intermediate_type, prev_type;
ef4bddc2 10492 machine_mode intermediate_mode, prev_mode;
ebfd146a 10493 int i;
4a00c761 10494 bool uns;
ebfd146a 10495
4a00c761 10496 *multi_step_cvt = 0;
ebfd146a
IR
10497 switch (code)
10498 {
10499 CASE_CONVERT:
10500 c1 = VEC_PACK_TRUNC_EXPR;
10501 break;
10502
10503 case FIX_TRUNC_EXPR:
10504 c1 = VEC_PACK_FIX_TRUNC_EXPR;
10505 break;
10506
10507 case FLOAT_EXPR:
1bda738b
JJ
10508 c1 = VEC_PACK_FLOAT_EXPR;
10509 break;
ebfd146a
IR
10510
10511 default:
10512 gcc_unreachable ();
10513 }
10514
10515 if (code == FIX_TRUNC_EXPR)
10516 /* The signedness is determined from output operand. */
b690cc0f 10517 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
ebfd146a
IR
10518 else
10519 optab1 = optab_for_tree_code (c1, vectype, optab_default);
10520
10521 if (!optab1)
10522 return false;
10523
10524 vec_mode = TYPE_MODE (vectype);
947131ba 10525 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
ebfd146a
IR
10526 return false;
10527
4a00c761
JJ
10528 *code1 = c1;
10529
10530 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
5e8d6dff
IE
10531 /* For scalar masks we may have different boolean
10532 vector types having the same QImode. Thus we
10533 add additional check for elements number. */
10534 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
928686b1
RS
10535 || known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
10536 TYPE_VECTOR_SUBPARTS (narrow_vectype)));
4a00c761 10537
1bda738b
JJ
10538 if (code == FLOAT_EXPR)
10539 return false;
10540
ebfd146a
IR
10541 /* Check if it's a multi-step conversion that can be done using intermediate
10542 types. */
4a00c761 10543 prev_mode = vec_mode;
3ae0661a 10544 prev_type = vectype;
4a00c761
JJ
10545 if (code == FIX_TRUNC_EXPR)
10546 uns = TYPE_UNSIGNED (vectype_out);
10547 else
10548 uns = TYPE_UNSIGNED (vectype);
10549
10550 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
10551 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
10552 costly than signed. */
10553 if (code == FIX_TRUNC_EXPR && uns)
10554 {
10555 enum insn_code icode2;
10556
10557 intermediate_type
10558 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
10559 interm_optab
10560 = optab_for_tree_code (c1, intermediate_type, optab_default);
2225b9f2 10561 if (interm_optab != unknown_optab
4a00c761
JJ
10562 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
10563 && insn_data[icode1].operand[0].mode
10564 == insn_data[icode2].operand[0].mode)
10565 {
10566 uns = false;
10567 optab1 = interm_optab;
10568 icode1 = icode2;
10569 }
10570 }
ebfd146a 10571
4a00c761
JJ
10572 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
10573 intermediate steps in promotion sequence. We try
10574 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
9771b263 10575 interm_types->create (MAX_INTERM_CVT_STEPS);
4a00c761
JJ
10576 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
10577 {
10578 intermediate_mode = insn_data[icode1].operand[0].mode;
3ae0661a
IE
10579 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
10580 {
7cfb4d93 10581 intermediate_type = vect_double_mask_nunits (prev_type);
3ae0661a 10582 if (intermediate_mode != TYPE_MODE (intermediate_type))
7cfb4d93 10583 return false;
3ae0661a
IE
10584 }
10585 else
10586 intermediate_type
10587 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
4a00c761
JJ
10588 interm_optab
10589 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
10590 optab_default);
10591 if (!interm_optab
10592 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
10593 || insn_data[icode1].operand[0].mode != intermediate_mode
10594 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
10595 == CODE_FOR_nothing))
10596 break;
10597
9771b263 10598 interm_types->quick_push (intermediate_type);
4a00c761
JJ
10599 (*multi_step_cvt)++;
10600
10601 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
5e8d6dff 10602 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
928686b1
RS
10603 || known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
10604 TYPE_VECTOR_SUBPARTS (narrow_vectype)));
4a00c761
JJ
10605
10606 prev_mode = intermediate_mode;
3ae0661a 10607 prev_type = intermediate_type;
4a00c761 10608 optab1 = interm_optab;
ebfd146a
IR
10609 }
10610
9771b263 10611 interm_types->release ();
4a00c761 10612 return false;
ebfd146a 10613}
7cfb4d93
RS
10614
10615/* Generate and return a statement that sets vector mask MASK such that
10616 MASK[I] is true iff J + START_INDEX < END_INDEX for all J <= I. */
10617
10618gcall *
10619vect_gen_while (tree mask, tree start_index, tree end_index)
10620{
10621 tree cmp_type = TREE_TYPE (start_index);
10622 tree mask_type = TREE_TYPE (mask);
10623 gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
10624 cmp_type, mask_type,
10625 OPTIMIZE_FOR_SPEED));
10626 gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
10627 start_index, end_index,
10628 build_zero_cst (mask_type));
10629 gimple_call_set_lhs (call, mask);
10630 return call;
10631}
535e7c11
RS
10632
10633/* Generate a vector mask of type MASK_TYPE for which index I is false iff
10634 J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
10635
10636tree
10637vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
10638 tree end_index)
10639{
10640 tree tmp = make_ssa_name (mask_type);
10641 gcall *call = vect_gen_while (tmp, start_index, end_index);
10642 gimple_seq_add_stmt (seq, call);
10643 return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
10644}
1f3cb663
RS
10645
10646/* Try to compute the vector types required to vectorize STMT_INFO,
10647 returning true on success and false if vectorization isn't possible.
10648
10649 On success:
10650
10651 - Set *STMT_VECTYPE_OUT to:
10652 - NULL_TREE if the statement doesn't need to be vectorized;
10653 - boolean_type_node if the statement is a boolean operation whose
10654 vector type can only be determined once all the other vector types
10655 are known; and
10656 - the equivalent of STMT_VINFO_VECTYPE otherwise.
10657
10658 - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
10659 number of units needed to vectorize STMT_INFO, or NULL_TREE if the
10660 statement does not help to determine the overall number of units. */
10661
10662bool
10663vect_get_vector_types_for_stmt (stmt_vec_info stmt_info,
10664 tree *stmt_vectype_out,
10665 tree *nunits_vectype_out)
10666{
10667 gimple *stmt = stmt_info->stmt;
10668
10669 *stmt_vectype_out = NULL_TREE;
10670 *nunits_vectype_out = NULL_TREE;
10671
10672 if (gimple_get_lhs (stmt) == NULL_TREE
10673 /* MASK_STORE has no lhs, but is ok. */
10674 && !gimple_call_internal_p (stmt, IFN_MASK_STORE))
10675 {
10676 if (is_a <gcall *> (stmt))
10677 {
10678 /* Ignore calls with no lhs. These must be calls to
10679 #pragma omp simd functions, and what vectorization factor
10680 it really needs can't be determined until
10681 vectorizable_simd_clone_call. */
10682 if (dump_enabled_p ())
10683 dump_printf_loc (MSG_NOTE, vect_location,
10684 "defer to SIMD clone analysis.\n");
10685 return true;
10686 }
10687
10688 if (dump_enabled_p ())
10689 {
10690 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10691 "not vectorized: irregular stmt.");
10692 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
10693 }
10694 return false;
10695 }
10696
10697 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))))
10698 {
10699 if (dump_enabled_p ())
10700 {
10701 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10702 "not vectorized: vector stmt in loop:");
10703 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
10704 }
10705 return false;
10706 }
10707
10708 tree vectype;
10709 tree scalar_type = NULL_TREE;
10710 if (STMT_VINFO_VECTYPE (stmt_info))
10711 *stmt_vectype_out = vectype = STMT_VINFO_VECTYPE (stmt_info);
10712 else
10713 {
10714 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info));
10715 if (gimple_call_internal_p (stmt, IFN_MASK_STORE))
10716 scalar_type = TREE_TYPE (gimple_call_arg (stmt, 3));
10717 else
10718 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
10719
10720 /* Pure bool ops don't participate in number-of-units computation.
10721 For comparisons use the types being compared. */
10722 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type)
10723 && is_gimple_assign (stmt)
10724 && gimple_assign_rhs_code (stmt) != COND_EXPR)
10725 {
10726 *stmt_vectype_out = boolean_type_node;
10727
10728 tree rhs1 = gimple_assign_rhs1 (stmt);
10729 if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison
10730 && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (rhs1)))
10731 scalar_type = TREE_TYPE (rhs1);
10732 else
10733 {
10734 if (dump_enabled_p ())
10735 dump_printf_loc (MSG_NOTE, vect_location,
10736 "pure bool operation.\n");
10737 return true;
10738 }
10739 }
10740
10741 if (dump_enabled_p ())
10742 {
10743 dump_printf_loc (MSG_NOTE, vect_location,
10744 "get vectype for scalar type: ");
10745 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
10746 dump_printf (MSG_NOTE, "\n");
10747 }
10748 vectype = get_vectype_for_scalar_type (scalar_type);
10749 if (!vectype)
10750 {
10751 if (dump_enabled_p ())
10752 {
10753 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10754 "not vectorized: unsupported data-type ");
10755 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
10756 scalar_type);
10757 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
10758 }
10759 return false;
10760 }
10761
10762 if (!*stmt_vectype_out)
10763 *stmt_vectype_out = vectype;
10764
10765 if (dump_enabled_p ())
10766 {
10767 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
10768 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
10769 dump_printf (MSG_NOTE, "\n");
10770 }
10771 }
10772
10773 /* Don't try to compute scalar types if the stmt produces a boolean
10774 vector; use the existing vector type instead. */
10775 tree nunits_vectype;
10776 if (VECTOR_BOOLEAN_TYPE_P (vectype))
10777 nunits_vectype = vectype;
10778 else
10779 {
10780 /* The number of units is set according to the smallest scalar
10781 type (or the largest vector size, but we only support one
10782 vector size per vectorization). */
10783 if (*stmt_vectype_out != boolean_type_node)
10784 {
10785 HOST_WIDE_INT dummy;
10786 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
10787 }
10788 if (dump_enabled_p ())
10789 {
10790 dump_printf_loc (MSG_NOTE, vect_location,
10791 "get vectype for scalar type: ");
10792 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
10793 dump_printf (MSG_NOTE, "\n");
10794 }
10795 nunits_vectype = get_vectype_for_scalar_type (scalar_type);
10796 }
10797 if (!nunits_vectype)
10798 {
10799 if (dump_enabled_p ())
10800 {
10801 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10802 "not vectorized: unsupported data-type ");
10803 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
10804 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
10805 }
10806 return false;
10807 }
10808
10809 if (maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
10810 GET_MODE_SIZE (TYPE_MODE (nunits_vectype))))
10811 {
10812 if (dump_enabled_p ())
10813 {
10814 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10815 "not vectorized: different sized vector "
10816 "types in statement, ");
10817 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, vectype);
10818 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
10819 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, nunits_vectype);
10820 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
10821 }
10822 return false;
10823 }
10824
10825 if (dump_enabled_p ())
10826 {
10827 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
10828 dump_generic_expr (MSG_NOTE, TDF_SLIM, nunits_vectype);
10829 dump_printf (MSG_NOTE, "\n");
10830
10831 dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
10832 dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
10833 dump_printf (MSG_NOTE, "\n");
10834 }
10835
10836 *nunits_vectype_out = nunits_vectype;
10837 return true;
10838}
10839
10840/* Try to determine the correct vector type for STMT_INFO, which is a
10841 statement that produces a scalar boolean result. Return the vector
10842 type on success, otherwise return NULL_TREE. */
10843
10844tree
10845vect_get_mask_type_for_stmt (stmt_vec_info stmt_info)
10846{
10847 gimple *stmt = stmt_info->stmt;
10848 tree mask_type = NULL;
10849 tree vectype, scalar_type;
10850
10851 if (is_gimple_assign (stmt)
10852 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison
10853 && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt))))
10854 {
10855 scalar_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
10856 mask_type = get_mask_type_for_scalar_type (scalar_type);
10857
10858 if (!mask_type)
10859 {
10860 if (dump_enabled_p ())
10861 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10862 "not vectorized: unsupported mask\n");
10863 return NULL_TREE;
10864 }
10865 }
10866 else
10867 {
10868 tree rhs;
10869 ssa_op_iter iter;
1f3cb663
RS
10870 enum vect_def_type dt;
10871
10872 FOR_EACH_SSA_TREE_OPERAND (rhs, stmt, iter, SSA_OP_USE)
10873 {
894dd753 10874 if (!vect_is_simple_use (rhs, stmt_info->vinfo, &dt, &vectype))
1f3cb663
RS
10875 {
10876 if (dump_enabled_p ())
10877 {
10878 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10879 "not vectorized: can't compute mask type "
10880 "for statement, ");
10881 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt,
10882 0);
10883 }
10884 return NULL_TREE;
10885 }
10886
10887 /* No vectype probably means external definition.
10888 Allow it in case there is another operand which
10889 allows to determine mask type. */
10890 if (!vectype)
10891 continue;
10892
10893 if (!mask_type)
10894 mask_type = vectype;
10895 else if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_type),
10896 TYPE_VECTOR_SUBPARTS (vectype)))
10897 {
10898 if (dump_enabled_p ())
10899 {
10900 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10901 "not vectorized: different sized masks "
10902 "types in statement, ");
10903 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
10904 mask_type);
10905 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
10906 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
10907 vectype);
10908 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
10909 }
10910 return NULL_TREE;
10911 }
10912 else if (VECTOR_BOOLEAN_TYPE_P (mask_type)
10913 != VECTOR_BOOLEAN_TYPE_P (vectype))
10914 {
10915 if (dump_enabled_p ())
10916 {
10917 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10918 "not vectorized: mixed mask and "
10919 "nonmask vector types in statement, ");
10920 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
10921 mask_type);
10922 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
10923 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
10924 vectype);
10925 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
10926 }
10927 return NULL_TREE;
10928 }
10929 }
10930
10931 /* We may compare boolean value loaded as vector of integers.
10932 Fix mask_type in such case. */
10933 if (mask_type
10934 && !VECTOR_BOOLEAN_TYPE_P (mask_type)
10935 && gimple_code (stmt) == GIMPLE_ASSIGN
10936 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10937 mask_type = build_same_sized_truth_vector_type (mask_type);
10938 }
10939
10940 /* No mask_type should mean loop invariant predicate.
10941 This is probably a subject for optimization in if-conversion. */
10942 if (!mask_type && dump_enabled_p ())
10943 {
10944 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10945 "not vectorized: can't compute mask type "
10946 "for statement, ");
10947 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
10948 }
10949 return mask_type;
10950}