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