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