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