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