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