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