]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-vect-stmts.c
re PR c++/55992 (constexpr static member function not recognised in templated using...
[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
fce57248 2345 if (slp_node)
190c2236 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 */
fce57248 2795 if (slp_node)
0136f8f0
AH
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++)
7adb26f2
JJ
3015 if ((bestn->simdclone->args[i].arg_type
3016 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
3017 || (bestn->simdclone->args[i].arg_type
3018 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP))
6c9e85fb 3019 {
17b658af 3020 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_grow_cleared (i * 3
6c9e85fb
JJ
3021 + 1);
3022 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (arginfo[i].op);
3023 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
3024 ? size_type_node : TREE_TYPE (arginfo[i].op);
3025 tree ls = build_int_cst (lst, arginfo[i].linear_step);
3026 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (ls);
17b658af
JJ
3027 tree sll = arginfo[i].simd_lane_linear
3028 ? boolean_true_node : boolean_false_node;
3029 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).safe_push (sll);
6c9e85fb 3030 }
0136f8f0
AH
3031 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
3032 if (dump_enabled_p ())
3033 dump_printf_loc (MSG_NOTE, vect_location,
3034 "=== vectorizable_simd_clone_call ===\n");
3035/* vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL); */
0136f8f0
AH
3036 return true;
3037 }
3038
3039 /** Transform. **/
3040
3041 if (dump_enabled_p ())
3042 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3043
3044 /* Handle def. */
3045 scalar_dest = gimple_call_lhs (stmt);
3046 vec_dest = NULL_TREE;
3047 rtype = NULL_TREE;
3048 ratype = NULL_TREE;
3049 if (scalar_dest)
3050 {
3051 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3052 rtype = TREE_TYPE (TREE_TYPE (fndecl));
3053 if (TREE_CODE (rtype) == ARRAY_TYPE)
3054 {
3055 ratype = rtype;
3056 rtype = TREE_TYPE (ratype);
3057 }
3058 }
3059
3060 prev_stmt_info = NULL;
3061 for (j = 0; j < ncopies; ++j)
3062 {
3063 /* Build argument list for the vectorized call. */
3064 if (j == 0)
3065 vargs.create (nargs);
3066 else
3067 vargs.truncate (0);
3068
3069 for (i = 0; i < nargs; i++)
3070 {
3071 unsigned int k, l, m, o;
3072 tree atype;
3073 op = gimple_call_arg (stmt, i);
3074 switch (bestn->simdclone->args[i].arg_type)
3075 {
3076 case SIMD_CLONE_ARG_TYPE_VECTOR:
3077 atype = bestn->simdclone->args[i].vector_type;
3078 o = nunits / TYPE_VECTOR_SUBPARTS (atype);
3079 for (m = j * o; m < (j + 1) * o; m++)
3080 {
3081 if (TYPE_VECTOR_SUBPARTS (atype)
3082 < TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
3083 {
3084 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
3085 k = (TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)
3086 / TYPE_VECTOR_SUBPARTS (atype));
3087 gcc_assert ((k & (k - 1)) == 0);
3088 if (m == 0)
3089 vec_oprnd0
81c40241 3090 = vect_get_vec_def_for_operand (op, stmt);
0136f8f0
AH
3091 else
3092 {
3093 vec_oprnd0 = arginfo[i].op;
3094 if ((m & (k - 1)) == 0)
3095 vec_oprnd0
3096 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3097 vec_oprnd0);
3098 }
3099 arginfo[i].op = vec_oprnd0;
3100 vec_oprnd0
3101 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
3102 size_int (prec),
3103 bitsize_int ((m & (k - 1)) * prec));
3104 new_stmt
b731b390 3105 = gimple_build_assign (make_ssa_name (atype),
0136f8f0
AH
3106 vec_oprnd0);
3107 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3108 vargs.safe_push (gimple_assign_lhs (new_stmt));
3109 }
3110 else
3111 {
3112 k = (TYPE_VECTOR_SUBPARTS (atype)
3113 / TYPE_VECTOR_SUBPARTS (arginfo[i].vectype));
3114 gcc_assert ((k & (k - 1)) == 0);
3115 vec<constructor_elt, va_gc> *ctor_elts;
3116 if (k != 1)
3117 vec_alloc (ctor_elts, k);
3118 else
3119 ctor_elts = NULL;
3120 for (l = 0; l < k; l++)
3121 {
3122 if (m == 0 && l == 0)
3123 vec_oprnd0
81c40241 3124 = vect_get_vec_def_for_operand (op, stmt);
0136f8f0
AH
3125 else
3126 vec_oprnd0
3127 = vect_get_vec_def_for_stmt_copy (arginfo[i].dt,
3128 arginfo[i].op);
3129 arginfo[i].op = vec_oprnd0;
3130 if (k == 1)
3131 break;
3132 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
3133 vec_oprnd0);
3134 }
3135 if (k == 1)
3136 vargs.safe_push (vec_oprnd0);
3137 else
3138 {
3139 vec_oprnd0 = build_constructor (atype, ctor_elts);
3140 new_stmt
b731b390 3141 = gimple_build_assign (make_ssa_name (atype),
0136f8f0
AH
3142 vec_oprnd0);
3143 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3144 vargs.safe_push (gimple_assign_lhs (new_stmt));
3145 }
3146 }
3147 }
3148 break;
3149 case SIMD_CLONE_ARG_TYPE_UNIFORM:
3150 vargs.safe_push (op);
3151 break;
3152 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
7adb26f2 3153 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
0136f8f0
AH
3154 if (j == 0)
3155 {
3156 gimple_seq stmts;
3157 arginfo[i].op
3158 = force_gimple_operand (arginfo[i].op, &stmts, true,
3159 NULL_TREE);
3160 if (stmts != NULL)
3161 {
3162 basic_block new_bb;
3163 edge pe = loop_preheader_edge (loop);
3164 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3165 gcc_assert (!new_bb);
3166 }
17b658af
JJ
3167 if (arginfo[i].simd_lane_linear)
3168 {
3169 vargs.safe_push (arginfo[i].op);
3170 break;
3171 }
b731b390 3172 tree phi_res = copy_ssa_name (op);
538dd0b7 3173 gphi *new_phi = create_phi_node (phi_res, loop->header);
0136f8f0 3174 set_vinfo_for_stmt (new_phi,
310213d4 3175 new_stmt_vec_info (new_phi, loop_vinfo));
0136f8f0
AH
3176 add_phi_arg (new_phi, arginfo[i].op,
3177 loop_preheader_edge (loop), UNKNOWN_LOCATION);
3178 enum tree_code code
3179 = POINTER_TYPE_P (TREE_TYPE (op))
3180 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3181 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3182 ? sizetype : TREE_TYPE (op);
807e902e
KZ
3183 widest_int cst
3184 = wi::mul (bestn->simdclone->args[i].linear_step,
3185 ncopies * nunits);
3186 tree tcst = wide_int_to_tree (type, cst);
b731b390 3187 tree phi_arg = copy_ssa_name (op);
0d0e4a03
JJ
3188 new_stmt
3189 = gimple_build_assign (phi_arg, code, phi_res, tcst);
0136f8f0
AH
3190 gimple_stmt_iterator si = gsi_after_labels (loop->header);
3191 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
3192 set_vinfo_for_stmt (new_stmt,
310213d4 3193 new_stmt_vec_info (new_stmt, loop_vinfo));
0136f8f0
AH
3194 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
3195 UNKNOWN_LOCATION);
3196 arginfo[i].op = phi_res;
3197 vargs.safe_push (phi_res);
3198 }
3199 else
3200 {
3201 enum tree_code code
3202 = POINTER_TYPE_P (TREE_TYPE (op))
3203 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3204 tree type = POINTER_TYPE_P (TREE_TYPE (op))
3205 ? sizetype : TREE_TYPE (op);
807e902e
KZ
3206 widest_int cst
3207 = wi::mul (bestn->simdclone->args[i].linear_step,
3208 j * nunits);
3209 tree tcst = wide_int_to_tree (type, cst);
b731b390 3210 new_temp = make_ssa_name (TREE_TYPE (op));
0d0e4a03
JJ
3211 new_stmt = gimple_build_assign (new_temp, code,
3212 arginfo[i].op, tcst);
0136f8f0
AH
3213 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3214 vargs.safe_push (new_temp);
3215 }
3216 break;
7adb26f2
JJ
3217 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
3218 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
0136f8f0 3219 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
e01d41e5
JJ
3220 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
3221 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
3222 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
0136f8f0
AH
3223 default:
3224 gcc_unreachable ();
3225 }
3226 }
3227
3228 new_stmt = gimple_build_call_vec (fndecl, vargs);
3229 if (vec_dest)
3230 {
3231 gcc_assert (ratype || TYPE_VECTOR_SUBPARTS (rtype) == nunits);
3232 if (ratype)
b731b390 3233 new_temp = create_tmp_var (ratype);
0136f8f0
AH
3234 else if (TYPE_VECTOR_SUBPARTS (vectype)
3235 == TYPE_VECTOR_SUBPARTS (rtype))
3236 new_temp = make_ssa_name (vec_dest, new_stmt);
3237 else
3238 new_temp = make_ssa_name (rtype, new_stmt);
3239 gimple_call_set_lhs (new_stmt, new_temp);
3240 }
3241 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3242
3243 if (vec_dest)
3244 {
3245 if (TYPE_VECTOR_SUBPARTS (vectype) < nunits)
3246 {
3247 unsigned int k, l;
3248 unsigned int prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
3249 k = nunits / TYPE_VECTOR_SUBPARTS (vectype);
3250 gcc_assert ((k & (k - 1)) == 0);
3251 for (l = 0; l < k; l++)
3252 {
3253 tree t;
3254 if (ratype)
3255 {
3256 t = build_fold_addr_expr (new_temp);
3257 t = build2 (MEM_REF, vectype, t,
3258 build_int_cst (TREE_TYPE (t),
3259 l * prec / BITS_PER_UNIT));
3260 }
3261 else
3262 t = build3 (BIT_FIELD_REF, vectype, new_temp,
3263 size_int (prec), bitsize_int (l * prec));
3264 new_stmt
b731b390 3265 = gimple_build_assign (make_ssa_name (vectype), t);
0136f8f0
AH
3266 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3267 if (j == 0 && l == 0)
3268 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3269 else
3270 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3271
3272 prev_stmt_info = vinfo_for_stmt (new_stmt);
3273 }
3274
3275 if (ratype)
3276 {
3277 tree clobber = build_constructor (ratype, NULL);
3278 TREE_THIS_VOLATILE (clobber) = 1;
3279 new_stmt = gimple_build_assign (new_temp, clobber);
3280 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3281 }
3282 continue;
3283 }
3284 else if (TYPE_VECTOR_SUBPARTS (vectype) > nunits)
3285 {
3286 unsigned int k = (TYPE_VECTOR_SUBPARTS (vectype)
3287 / TYPE_VECTOR_SUBPARTS (rtype));
3288 gcc_assert ((k & (k - 1)) == 0);
3289 if ((j & (k - 1)) == 0)
3290 vec_alloc (ret_ctor_elts, k);
3291 if (ratype)
3292 {
3293 unsigned int m, o = nunits / TYPE_VECTOR_SUBPARTS (rtype);
3294 for (m = 0; m < o; m++)
3295 {
3296 tree tem = build4 (ARRAY_REF, rtype, new_temp,
3297 size_int (m), NULL_TREE, NULL_TREE);
3298 new_stmt
b731b390 3299 = gimple_build_assign (make_ssa_name (rtype), tem);
0136f8f0
AH
3300 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3301 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
3302 gimple_assign_lhs (new_stmt));
3303 }
3304 tree clobber = build_constructor (ratype, NULL);
3305 TREE_THIS_VOLATILE (clobber) = 1;
3306 new_stmt = gimple_build_assign (new_temp, clobber);
3307 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3308 }
3309 else
3310 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
3311 if ((j & (k - 1)) != k - 1)
3312 continue;
3313 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
3314 new_stmt
b731b390 3315 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
0136f8f0
AH
3316 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3317
3318 if ((unsigned) j == k - 1)
3319 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3320 else
3321 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3322
3323 prev_stmt_info = vinfo_for_stmt (new_stmt);
3324 continue;
3325 }
3326 else if (ratype)
3327 {
3328 tree t = build_fold_addr_expr (new_temp);
3329 t = build2 (MEM_REF, vectype, t,
3330 build_int_cst (TREE_TYPE (t), 0));
3331 new_stmt
b731b390 3332 = gimple_build_assign (make_ssa_name (vec_dest), t);
0136f8f0
AH
3333 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3334 tree clobber = build_constructor (ratype, NULL);
3335 TREE_THIS_VOLATILE (clobber) = 1;
3336 vect_finish_stmt_generation (stmt,
3337 gimple_build_assign (new_temp,
3338 clobber), gsi);
3339 }
3340 }
3341
3342 if (j == 0)
3343 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3344 else
3345 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3346
3347 prev_stmt_info = vinfo_for_stmt (new_stmt);
3348 }
3349
3350 vargs.release ();
3351
3352 /* The call in STMT might prevent it from being removed in dce.
3353 We however cannot remove it here, due to the way the ssa name
3354 it defines is mapped to the new definition. So just replace
3355 rhs of the statement with something harmless. */
3356
3357 if (slp_node)
3358 return true;
3359
3360 if (scalar_dest)
3361 {
3362 type = TREE_TYPE (scalar_dest);
3363 if (is_pattern_stmt_p (stmt_info))
3364 lhs = gimple_call_lhs (STMT_VINFO_RELATED_STMT (stmt_info));
3365 else
3366 lhs = gimple_call_lhs (stmt);
3367 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
3368 }
3369 else
3370 new_stmt = gimple_build_nop ();
3371 set_vinfo_for_stmt (new_stmt, stmt_info);
3372 set_vinfo_for_stmt (stmt, NULL);
3373 STMT_VINFO_STMT (stmt_info) = new_stmt;
2865f32a 3374 gsi_replace (gsi, new_stmt, true);
0136f8f0
AH
3375 unlink_stmt_vdef (stmt);
3376
3377 return true;
3378}
3379
3380
ebfd146a
IR
3381/* Function vect_gen_widened_results_half
3382
3383 Create a vector stmt whose code, type, number of arguments, and result
b8698a0f 3384 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
ff802fa1 3385 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at BSI.
ebfd146a
IR
3386 In the case that CODE is a CALL_EXPR, this means that a call to DECL
3387 needs to be created (DECL is a function-decl of a target-builtin).
3388 STMT is the original scalar stmt that we are vectorizing. */
3389
355fe088 3390static gimple *
ebfd146a
IR
3391vect_gen_widened_results_half (enum tree_code code,
3392 tree decl,
3393 tree vec_oprnd0, tree vec_oprnd1, int op_type,
3394 tree vec_dest, gimple_stmt_iterator *gsi,
355fe088 3395 gimple *stmt)
b8698a0f 3396{
355fe088 3397 gimple *new_stmt;
b8698a0f
L
3398 tree new_temp;
3399
3400 /* Generate half of the widened result: */
3401 if (code == CALL_EXPR)
3402 {
3403 /* Target specific support */
ebfd146a
IR
3404 if (op_type == binary_op)
3405 new_stmt = gimple_build_call (decl, 2, vec_oprnd0, vec_oprnd1);
3406 else
3407 new_stmt = gimple_build_call (decl, 1, vec_oprnd0);
3408 new_temp = make_ssa_name (vec_dest, new_stmt);
3409 gimple_call_set_lhs (new_stmt, new_temp);
b8698a0f
L
3410 }
3411 else
ebfd146a 3412 {
b8698a0f
L
3413 /* Generic support */
3414 gcc_assert (op_type == TREE_CODE_LENGTH (code));
ebfd146a
IR
3415 if (op_type != binary_op)
3416 vec_oprnd1 = NULL;
0d0e4a03 3417 new_stmt = gimple_build_assign (vec_dest, code, vec_oprnd0, vec_oprnd1);
ebfd146a
IR
3418 new_temp = make_ssa_name (vec_dest, new_stmt);
3419 gimple_assign_set_lhs (new_stmt, new_temp);
b8698a0f 3420 }
ebfd146a
IR
3421 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3422
ebfd146a
IR
3423 return new_stmt;
3424}
3425
4a00c761
JJ
3426
3427/* Get vectorized definitions for loop-based vectorization. For the first
3428 operand we call vect_get_vec_def_for_operand() (with OPRND containing
3429 scalar operand), and for the rest we get a copy with
3430 vect_get_vec_def_for_stmt_copy() using the previous vector definition
3431 (stored in OPRND). See vect_get_vec_def_for_stmt_copy() for details.
3432 The vectors are collected into VEC_OPRNDS. */
3433
3434static void
355fe088 3435vect_get_loop_based_defs (tree *oprnd, gimple *stmt, enum vect_def_type dt,
9771b263 3436 vec<tree> *vec_oprnds, int multi_step_cvt)
4a00c761
JJ
3437{
3438 tree vec_oprnd;
3439
3440 /* Get first vector operand. */
3441 /* All the vector operands except the very first one (that is scalar oprnd)
3442 are stmt copies. */
3443 if (TREE_CODE (TREE_TYPE (*oprnd)) != VECTOR_TYPE)
81c40241 3444 vec_oprnd = vect_get_vec_def_for_operand (*oprnd, stmt);
4a00c761
JJ
3445 else
3446 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, *oprnd);
3447
9771b263 3448 vec_oprnds->quick_push (vec_oprnd);
4a00c761
JJ
3449
3450 /* Get second vector operand. */
3451 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
9771b263 3452 vec_oprnds->quick_push (vec_oprnd);
4a00c761
JJ
3453
3454 *oprnd = vec_oprnd;
3455
3456 /* For conversion in multiple steps, continue to get operands
3457 recursively. */
3458 if (multi_step_cvt)
3459 vect_get_loop_based_defs (oprnd, stmt, dt, vec_oprnds, multi_step_cvt - 1);
3460}
3461
3462
3463/* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
3464 For multi-step conversions store the resulting vectors and call the function
3465 recursively. */
3466
3467static void
9771b263 3468vect_create_vectorized_demotion_stmts (vec<tree> *vec_oprnds,
355fe088 3469 int multi_step_cvt, gimple *stmt,
9771b263 3470 vec<tree> vec_dsts,
4a00c761
JJ
3471 gimple_stmt_iterator *gsi,
3472 slp_tree slp_node, enum tree_code code,
3473 stmt_vec_info *prev_stmt_info)
3474{
3475 unsigned int i;
3476 tree vop0, vop1, new_tmp, vec_dest;
355fe088 3477 gimple *new_stmt;
4a00c761
JJ
3478 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3479
9771b263 3480 vec_dest = vec_dsts.pop ();
4a00c761 3481
9771b263 3482 for (i = 0; i < vec_oprnds->length (); i += 2)
4a00c761
JJ
3483 {
3484 /* Create demotion operation. */
9771b263
DN
3485 vop0 = (*vec_oprnds)[i];
3486 vop1 = (*vec_oprnds)[i + 1];
0d0e4a03 3487 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
4a00c761
JJ
3488 new_tmp = make_ssa_name (vec_dest, new_stmt);
3489 gimple_assign_set_lhs (new_stmt, new_tmp);
3490 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3491
3492 if (multi_step_cvt)
3493 /* Store the resulting vector for next recursive call. */
9771b263 3494 (*vec_oprnds)[i/2] = new_tmp;
4a00c761
JJ
3495 else
3496 {
3497 /* This is the last step of the conversion sequence. Store the
3498 vectors in SLP_NODE or in vector info of the scalar statement
3499 (or in STMT_VINFO_RELATED_STMT chain). */
3500 if (slp_node)
9771b263 3501 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4a00c761 3502 else
c689ce1e
RB
3503 {
3504 if (!*prev_stmt_info)
3505 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
3506 else
3507 STMT_VINFO_RELATED_STMT (*prev_stmt_info) = new_stmt;
4a00c761 3508
c689ce1e
RB
3509 *prev_stmt_info = vinfo_for_stmt (new_stmt);
3510 }
4a00c761
JJ
3511 }
3512 }
3513
3514 /* For multi-step demotion operations we first generate demotion operations
3515 from the source type to the intermediate types, and then combine the
3516 results (stored in VEC_OPRNDS) in demotion operation to the destination
3517 type. */
3518 if (multi_step_cvt)
3519 {
3520 /* At each level of recursion we have half of the operands we had at the
3521 previous level. */
9771b263 3522 vec_oprnds->truncate ((i+1)/2);
4a00c761
JJ
3523 vect_create_vectorized_demotion_stmts (vec_oprnds, multi_step_cvt - 1,
3524 stmt, vec_dsts, gsi, slp_node,
3525 VEC_PACK_TRUNC_EXPR,
3526 prev_stmt_info);
3527 }
3528
9771b263 3529 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
3530}
3531
3532
3533/* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
3534 and VEC_OPRNDS1 (for binary operations). For multi-step conversions store
3535 the resulting vectors and call the function recursively. */
3536
3537static void
9771b263
DN
3538vect_create_vectorized_promotion_stmts (vec<tree> *vec_oprnds0,
3539 vec<tree> *vec_oprnds1,
355fe088 3540 gimple *stmt, tree vec_dest,
4a00c761
JJ
3541 gimple_stmt_iterator *gsi,
3542 enum tree_code code1,
3543 enum tree_code code2, tree decl1,
3544 tree decl2, int op_type)
3545{
3546 int i;
3547 tree vop0, vop1, new_tmp1, new_tmp2;
355fe088 3548 gimple *new_stmt1, *new_stmt2;
6e1aa848 3549 vec<tree> vec_tmp = vNULL;
4a00c761 3550
9771b263
DN
3551 vec_tmp.create (vec_oprnds0->length () * 2);
3552 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
4a00c761
JJ
3553 {
3554 if (op_type == binary_op)
9771b263 3555 vop1 = (*vec_oprnds1)[i];
4a00c761
JJ
3556 else
3557 vop1 = NULL_TREE;
3558
3559 /* Generate the two halves of promotion operation. */
3560 new_stmt1 = vect_gen_widened_results_half (code1, decl1, vop0, vop1,
3561 op_type, vec_dest, gsi, stmt);
3562 new_stmt2 = vect_gen_widened_results_half (code2, decl2, vop0, vop1,
3563 op_type, vec_dest, gsi, stmt);
3564 if (is_gimple_call (new_stmt1))
3565 {
3566 new_tmp1 = gimple_call_lhs (new_stmt1);
3567 new_tmp2 = gimple_call_lhs (new_stmt2);
3568 }
3569 else
3570 {
3571 new_tmp1 = gimple_assign_lhs (new_stmt1);
3572 new_tmp2 = gimple_assign_lhs (new_stmt2);
3573 }
3574
3575 /* Store the results for the next step. */
9771b263
DN
3576 vec_tmp.quick_push (new_tmp1);
3577 vec_tmp.quick_push (new_tmp2);
4a00c761
JJ
3578 }
3579
689eaba3 3580 vec_oprnds0->release ();
4a00c761
JJ
3581 *vec_oprnds0 = vec_tmp;
3582}
3583
3584
b8698a0f
L
3585/* Check if STMT performs a conversion operation, that can be vectorized.
3586 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4a00c761 3587 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
ebfd146a
IR
3588 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3589
3590static bool
355fe088
TS
3591vectorizable_conversion (gimple *stmt, gimple_stmt_iterator *gsi,
3592 gimple **vec_stmt, slp_tree slp_node)
ebfd146a
IR
3593{
3594 tree vec_dest;
3595 tree scalar_dest;
4a00c761 3596 tree op0, op1 = NULL_TREE;
ebfd146a
IR
3597 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3598 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3599 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3600 enum tree_code code, code1 = ERROR_MARK, code2 = ERROR_MARK;
4a00c761 3601 enum tree_code codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
ebfd146a
IR
3602 tree decl1 = NULL_TREE, decl2 = NULL_TREE;
3603 tree new_temp;
355fe088 3604 gimple *def_stmt;
ebfd146a 3605 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
355fe088 3606 gimple *new_stmt = NULL;
ebfd146a
IR
3607 stmt_vec_info prev_stmt_info;
3608 int nunits_in;
3609 int nunits_out;
3610 tree vectype_out, vectype_in;
4a00c761
JJ
3611 int ncopies, i, j;
3612 tree lhs_type, rhs_type;
ebfd146a 3613 enum { NARROW, NONE, WIDEN } modifier;
6e1aa848
DN
3614 vec<tree> vec_oprnds0 = vNULL;
3615 vec<tree> vec_oprnds1 = vNULL;
ebfd146a 3616 tree vop0;
4a00c761 3617 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 3618 vec_info *vinfo = stmt_info->vinfo;
4a00c761 3619 int multi_step_cvt = 0;
6e1aa848
DN
3620 vec<tree> vec_dsts = vNULL;
3621 vec<tree> interm_types = vNULL;
4a00c761
JJ
3622 tree last_oprnd, intermediate_type, cvt_type = NULL_TREE;
3623 int op_type;
ef4bddc2 3624 machine_mode rhs_mode;
4a00c761 3625 unsigned short fltsz;
ebfd146a
IR
3626
3627 /* Is STMT a vectorizable conversion? */
3628
4a00c761 3629 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
3630 return false;
3631
66c16fd9
RB
3632 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3633 && ! vec_stmt)
ebfd146a
IR
3634 return false;
3635
3636 if (!is_gimple_assign (stmt))
3637 return false;
3638
3639 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
3640 return false;
3641
3642 code = gimple_assign_rhs_code (stmt);
4a00c761
JJ
3643 if (!CONVERT_EXPR_CODE_P (code)
3644 && code != FIX_TRUNC_EXPR
3645 && code != FLOAT_EXPR
3646 && code != WIDEN_MULT_EXPR
3647 && code != WIDEN_LSHIFT_EXPR)
ebfd146a
IR
3648 return false;
3649
4a00c761
JJ
3650 op_type = TREE_CODE_LENGTH (code);
3651
ebfd146a 3652 /* Check types of lhs and rhs. */
b690cc0f 3653 scalar_dest = gimple_assign_lhs (stmt);
4a00c761 3654 lhs_type = TREE_TYPE (scalar_dest);
b690cc0f
RG
3655 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3656
ebfd146a
IR
3657 op0 = gimple_assign_rhs1 (stmt);
3658 rhs_type = TREE_TYPE (op0);
4a00c761
JJ
3659
3660 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3661 && !((INTEGRAL_TYPE_P (lhs_type)
3662 && INTEGRAL_TYPE_P (rhs_type))
3663 || (SCALAR_FLOAT_TYPE_P (lhs_type)
3664 && SCALAR_FLOAT_TYPE_P (rhs_type))))
3665 return false;
3666
e6f5c25d
IE
3667 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
3668 && ((INTEGRAL_TYPE_P (lhs_type)
3669 && (TYPE_PRECISION (lhs_type)
3670 != GET_MODE_PRECISION (TYPE_MODE (lhs_type))))
3671 || (INTEGRAL_TYPE_P (rhs_type)
3672 && (TYPE_PRECISION (rhs_type)
3673 != GET_MODE_PRECISION (TYPE_MODE (rhs_type))))))
4a00c761 3674 {
73fbfcad 3675 if (dump_enabled_p ())
78c60e3d 3676 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942
TJ
3677 "type conversion to/from bit-precision unsupported."
3678 "\n");
4a00c761
JJ
3679 return false;
3680 }
3681
b690cc0f 3682 /* Check the operands of the operation. */
81c40241 3683 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype_in))
b690cc0f 3684 {
73fbfcad 3685 if (dump_enabled_p ())
78c60e3d 3686 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3687 "use not simple.\n");
b690cc0f
RG
3688 return false;
3689 }
4a00c761
JJ
3690 if (op_type == binary_op)
3691 {
3692 bool ok;
3693
3694 op1 = gimple_assign_rhs2 (stmt);
3695 gcc_assert (code == WIDEN_MULT_EXPR || code == WIDEN_LSHIFT_EXPR);
3696 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
3697 OP1. */
3698 if (CONSTANT_CLASS_P (op0))
81c40241 3699 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &vectype_in);
4a00c761 3700 else
81c40241 3701 ok = vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]);
4a00c761
JJ
3702
3703 if (!ok)
3704 {
73fbfcad 3705 if (dump_enabled_p ())
78c60e3d 3706 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3707 "use not simple.\n");
4a00c761
JJ
3708 return false;
3709 }
3710 }
3711
b690cc0f
RG
3712 /* If op0 is an external or constant defs use a vector type of
3713 the same size as the output vector type. */
ebfd146a 3714 if (!vectype_in)
b690cc0f 3715 vectype_in = get_same_sized_vectype (rhs_type, vectype_out);
7d8930a0
IR
3716 if (vec_stmt)
3717 gcc_assert (vectype_in);
3718 if (!vectype_in)
3719 {
73fbfcad 3720 if (dump_enabled_p ())
4a00c761 3721 {
78c60e3d
SS
3722 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3723 "no vectype for scalar type ");
3724 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
e645e942 3725 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
4a00c761 3726 }
7d8930a0
IR
3727
3728 return false;
3729 }
ebfd146a 3730
e6f5c25d
IE
3731 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3732 && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
3733 {
3734 if (dump_enabled_p ())
3735 {
3736 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3737 "can't convert between boolean and non "
3738 "boolean vectors");
3739 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, rhs_type);
3740 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3741 }
3742
3743 return false;
3744 }
3745
b690cc0f
RG
3746 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3747 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4a00c761 3748 if (nunits_in < nunits_out)
ebfd146a
IR
3749 modifier = NARROW;
3750 else if (nunits_out == nunits_in)
3751 modifier = NONE;
ebfd146a 3752 else
4a00c761 3753 modifier = WIDEN;
ebfd146a 3754
ff802fa1
IR
3755 /* Multiple types in SLP are handled by creating the appropriate number of
3756 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3757 case of SLP. */
fce57248 3758 if (slp_node)
ebfd146a 3759 ncopies = 1;
4a00c761
JJ
3760 else if (modifier == NARROW)
3761 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_out;
3762 else
3763 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
b8698a0f 3764
ebfd146a
IR
3765 /* Sanity check: make sure that at least one copy of the vectorized stmt
3766 needs to be generated. */
3767 gcc_assert (ncopies >= 1);
3768
ebfd146a 3769 /* Supportable by target? */
4a00c761 3770 switch (modifier)
ebfd146a 3771 {
4a00c761
JJ
3772 case NONE:
3773 if (code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
3774 return false;
3775 if (supportable_convert_operation (code, vectype_out, vectype_in,
3776 &decl1, &code1))
3777 break;
3778 /* FALLTHRU */
3779 unsupported:
73fbfcad 3780 if (dump_enabled_p ())
78c60e3d 3781 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 3782 "conversion not supported by target.\n");
ebfd146a 3783 return false;
ebfd146a 3784
4a00c761
JJ
3785 case WIDEN:
3786 if (supportable_widening_operation (code, stmt, vectype_out, vectype_in,
a86ec597
RH
3787 &code1, &code2, &multi_step_cvt,
3788 &interm_types))
4a00c761
JJ
3789 {
3790 /* Binary widening operation can only be supported directly by the
3791 architecture. */
3792 gcc_assert (!(multi_step_cvt && op_type == binary_op));
3793 break;
3794 }
3795
3796 if (code != FLOAT_EXPR
3797 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3798 <= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3799 goto unsupported;
3800
3801 rhs_mode = TYPE_MODE (rhs_type);
3802 fltsz = GET_MODE_SIZE (TYPE_MODE (lhs_type));
3803 for (rhs_mode = GET_MODE_2XWIDER_MODE (TYPE_MODE (rhs_type));
3804 rhs_mode != VOIDmode && GET_MODE_SIZE (rhs_mode) <= fltsz;
3805 rhs_mode = GET_MODE_2XWIDER_MODE (rhs_mode))
3806 {
3807 cvt_type
3808 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3809 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3810 if (cvt_type == NULL_TREE)
3811 goto unsupported;
3812
3813 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3814 {
3815 if (!supportable_convert_operation (code, vectype_out,
3816 cvt_type, &decl1, &codecvt1))
3817 goto unsupported;
3818 }
3819 else if (!supportable_widening_operation (code, stmt, vectype_out,
a86ec597
RH
3820 cvt_type, &codecvt1,
3821 &codecvt2, &multi_step_cvt,
4a00c761
JJ
3822 &interm_types))
3823 continue;
3824 else
3825 gcc_assert (multi_step_cvt == 0);
3826
3827 if (supportable_widening_operation (NOP_EXPR, stmt, cvt_type,
a86ec597
RH
3828 vectype_in, &code1, &code2,
3829 &multi_step_cvt, &interm_types))
4a00c761
JJ
3830 break;
3831 }
3832
3833 if (rhs_mode == VOIDmode || GET_MODE_SIZE (rhs_mode) > fltsz)
3834 goto unsupported;
3835
3836 if (GET_MODE_SIZE (rhs_mode) == fltsz)
3837 codecvt2 = ERROR_MARK;
3838 else
3839 {
3840 multi_step_cvt++;
9771b263 3841 interm_types.safe_push (cvt_type);
4a00c761
JJ
3842 cvt_type = NULL_TREE;
3843 }
3844 break;
3845
3846 case NARROW:
3847 gcc_assert (op_type == unary_op);
3848 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
3849 &code1, &multi_step_cvt,
3850 &interm_types))
3851 break;
3852
3853 if (code != FIX_TRUNC_EXPR
3854 || (GET_MODE_SIZE (TYPE_MODE (lhs_type))
3855 >= GET_MODE_SIZE (TYPE_MODE (rhs_type))))
3856 goto unsupported;
3857
3858 rhs_mode = TYPE_MODE (rhs_type);
3859 cvt_type
3860 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
3861 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
3862 if (cvt_type == NULL_TREE)
3863 goto unsupported;
3864 if (!supportable_convert_operation (code, cvt_type, vectype_in,
3865 &decl1, &codecvt1))
3866 goto unsupported;
3867 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
3868 &code1, &multi_step_cvt,
3869 &interm_types))
3870 break;
3871 goto unsupported;
3872
3873 default:
3874 gcc_unreachable ();
ebfd146a
IR
3875 }
3876
3877 if (!vec_stmt) /* transformation not required. */
3878 {
73fbfcad 3879 if (dump_enabled_p ())
78c60e3d 3880 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 3881 "=== vectorizable_conversion ===\n");
4a00c761 3882 if (code == FIX_TRUNC_EXPR || code == FLOAT_EXPR)
8bd37302
BS
3883 {
3884 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
c3e7ee41 3885 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
8bd37302 3886 }
4a00c761
JJ
3887 else if (modifier == NARROW)
3888 {
3889 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
8bd37302 3890 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
4a00c761
JJ
3891 }
3892 else
3893 {
3894 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
8bd37302 3895 vect_model_promotion_demotion_cost (stmt_info, dt, multi_step_cvt);
4a00c761 3896 }
9771b263 3897 interm_types.release ();
ebfd146a
IR
3898 return true;
3899 }
3900
3901 /** Transform. **/
73fbfcad 3902 if (dump_enabled_p ())
78c60e3d 3903 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 3904 "transform conversion. ncopies = %d.\n", ncopies);
ebfd146a 3905
4a00c761
JJ
3906 if (op_type == binary_op)
3907 {
3908 if (CONSTANT_CLASS_P (op0))
3909 op0 = fold_convert (TREE_TYPE (op1), op0);
3910 else if (CONSTANT_CLASS_P (op1))
3911 op1 = fold_convert (TREE_TYPE (op0), op1);
3912 }
3913
3914 /* In case of multi-step conversion, we first generate conversion operations
3915 to the intermediate types, and then from that types to the final one.
3916 We create vector destinations for the intermediate type (TYPES) received
3917 from supportable_*_operation, and store them in the correct order
3918 for future use in vect_create_vectorized_*_stmts (). */
9771b263 3919 vec_dsts.create (multi_step_cvt + 1);
82294ec1
JJ
3920 vec_dest = vect_create_destination_var (scalar_dest,
3921 (cvt_type && modifier == WIDEN)
3922 ? cvt_type : vectype_out);
9771b263 3923 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
3924
3925 if (multi_step_cvt)
3926 {
9771b263
DN
3927 for (i = interm_types.length () - 1;
3928 interm_types.iterate (i, &intermediate_type); i--)
4a00c761
JJ
3929 {
3930 vec_dest = vect_create_destination_var (scalar_dest,
3931 intermediate_type);
9771b263 3932 vec_dsts.quick_push (vec_dest);
4a00c761
JJ
3933 }
3934 }
ebfd146a 3935
4a00c761 3936 if (cvt_type)
82294ec1
JJ
3937 vec_dest = vect_create_destination_var (scalar_dest,
3938 modifier == WIDEN
3939 ? vectype_out : cvt_type);
4a00c761
JJ
3940
3941 if (!slp_node)
3942 {
30862efc 3943 if (modifier == WIDEN)
4a00c761 3944 {
c3284718 3945 vec_oprnds0.create (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1);
4a00c761 3946 if (op_type == binary_op)
9771b263 3947 vec_oprnds1.create (1);
4a00c761 3948 }
30862efc 3949 else if (modifier == NARROW)
9771b263
DN
3950 vec_oprnds0.create (
3951 2 * (multi_step_cvt ? vect_pow2 (multi_step_cvt) : 1));
4a00c761
JJ
3952 }
3953 else if (code == WIDEN_LSHIFT_EXPR)
9771b263 3954 vec_oprnds1.create (slp_node->vec_stmts_size);
ebfd146a 3955
4a00c761 3956 last_oprnd = op0;
ebfd146a
IR
3957 prev_stmt_info = NULL;
3958 switch (modifier)
3959 {
3960 case NONE:
3961 for (j = 0; j < ncopies; j++)
3962 {
ebfd146a 3963 if (j == 0)
d092494c
IR
3964 vect_get_vec_defs (op0, NULL, stmt, &vec_oprnds0, NULL, slp_node,
3965 -1);
ebfd146a
IR
3966 else
3967 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, NULL);
3968
9771b263 3969 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
3970 {
3971 /* Arguments are ready, create the new vector stmt. */
3972 if (code1 == CALL_EXPR)
3973 {
3974 new_stmt = gimple_build_call (decl1, 1, vop0);
3975 new_temp = make_ssa_name (vec_dest, new_stmt);
3976 gimple_call_set_lhs (new_stmt, new_temp);
3977 }
3978 else
3979 {
3980 gcc_assert (TREE_CODE_LENGTH (code1) == unary_op);
0d0e4a03 3981 new_stmt = gimple_build_assign (vec_dest, code1, vop0);
4a00c761
JJ
3982 new_temp = make_ssa_name (vec_dest, new_stmt);
3983 gimple_assign_set_lhs (new_stmt, new_temp);
3984 }
3985
3986 vect_finish_stmt_generation (stmt, new_stmt, gsi);
3987 if (slp_node)
9771b263 3988 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
225ce44b
RB
3989 else
3990 {
3991 if (!prev_stmt_info)
3992 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
3993 else
3994 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
3995 prev_stmt_info = vinfo_for_stmt (new_stmt);
3996 }
4a00c761 3997 }
ebfd146a
IR
3998 }
3999 break;
4000
4001 case WIDEN:
4002 /* In case the vectorization factor (VF) is bigger than the number
4003 of elements that we can fit in a vectype (nunits), we have to
4004 generate more than one vector stmt - i.e - we need to "unroll"
4005 the vector stmt by a factor VF/nunits. */
4006 for (j = 0; j < ncopies; j++)
4007 {
4a00c761 4008 /* Handle uses. */
ebfd146a 4009 if (j == 0)
4a00c761
JJ
4010 {
4011 if (slp_node)
4012 {
4013 if (code == WIDEN_LSHIFT_EXPR)
4014 {
4015 unsigned int k;
ebfd146a 4016
4a00c761
JJ
4017 vec_oprnd1 = op1;
4018 /* Store vec_oprnd1 for every vector stmt to be created
4019 for SLP_NODE. We check during the analysis that all
4020 the shift arguments are the same. */
4021 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
9771b263 4022 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
4023
4024 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4025 slp_node, -1);
4026 }
4027 else
4028 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0,
4029 &vec_oprnds1, slp_node, -1);
4030 }
4031 else
4032 {
81c40241 4033 vec_oprnd0 = vect_get_vec_def_for_operand (op0, stmt);
9771b263 4034 vec_oprnds0.quick_push (vec_oprnd0);
4a00c761
JJ
4035 if (op_type == binary_op)
4036 {
4037 if (code == WIDEN_LSHIFT_EXPR)
4038 vec_oprnd1 = op1;
4039 else
81c40241 4040 vec_oprnd1 = vect_get_vec_def_for_operand (op1, stmt);
9771b263 4041 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
4042 }
4043 }
4044 }
ebfd146a 4045 else
4a00c761
JJ
4046 {
4047 vec_oprnd0 = vect_get_vec_def_for_stmt_copy (dt[0], vec_oprnd0);
9771b263
DN
4048 vec_oprnds0.truncate (0);
4049 vec_oprnds0.quick_push (vec_oprnd0);
4a00c761
JJ
4050 if (op_type == binary_op)
4051 {
4052 if (code == WIDEN_LSHIFT_EXPR)
4053 vec_oprnd1 = op1;
4054 else
4055 vec_oprnd1 = vect_get_vec_def_for_stmt_copy (dt[1],
4056 vec_oprnd1);
9771b263
DN
4057 vec_oprnds1.truncate (0);
4058 vec_oprnds1.quick_push (vec_oprnd1);
4a00c761
JJ
4059 }
4060 }
ebfd146a 4061
4a00c761
JJ
4062 /* Arguments are ready. Create the new vector stmts. */
4063 for (i = multi_step_cvt; i >= 0; i--)
4064 {
9771b263 4065 tree this_dest = vec_dsts[i];
4a00c761
JJ
4066 enum tree_code c1 = code1, c2 = code2;
4067 if (i == 0 && codecvt2 != ERROR_MARK)
4068 {
4069 c1 = codecvt1;
4070 c2 = codecvt2;
4071 }
4072 vect_create_vectorized_promotion_stmts (&vec_oprnds0,
4073 &vec_oprnds1,
4074 stmt, this_dest, gsi,
4075 c1, c2, decl1, decl2,
4076 op_type);
4077 }
4078
9771b263 4079 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
4080 {
4081 if (cvt_type)
4082 {
4083 if (codecvt1 == CALL_EXPR)
4084 {
4085 new_stmt = gimple_build_call (decl1, 1, vop0);
4086 new_temp = make_ssa_name (vec_dest, new_stmt);
4087 gimple_call_set_lhs (new_stmt, new_temp);
4088 }
4089 else
4090 {
4091 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
b731b390 4092 new_temp = make_ssa_name (vec_dest);
0d0e4a03
JJ
4093 new_stmt = gimple_build_assign (new_temp, codecvt1,
4094 vop0);
4a00c761
JJ
4095 }
4096
4097 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4098 }
4099 else
4100 new_stmt = SSA_NAME_DEF_STMT (vop0);
4101
4102 if (slp_node)
9771b263 4103 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
4a00c761 4104 else
c689ce1e
RB
4105 {
4106 if (!prev_stmt_info)
4107 STMT_VINFO_VEC_STMT (stmt_info) = new_stmt;
4108 else
4109 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4110 prev_stmt_info = vinfo_for_stmt (new_stmt);
4111 }
4a00c761 4112 }
ebfd146a 4113 }
4a00c761
JJ
4114
4115 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
ebfd146a
IR
4116 break;
4117
4118 case NARROW:
4119 /* In case the vectorization factor (VF) is bigger than the number
4120 of elements that we can fit in a vectype (nunits), we have to
4121 generate more than one vector stmt - i.e - we need to "unroll"
4122 the vector stmt by a factor VF/nunits. */
4123 for (j = 0; j < ncopies; j++)
4124 {
4125 /* Handle uses. */
4a00c761
JJ
4126 if (slp_node)
4127 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
4128 slp_node, -1);
ebfd146a
IR
4129 else
4130 {
9771b263 4131 vec_oprnds0.truncate (0);
4a00c761
JJ
4132 vect_get_loop_based_defs (&last_oprnd, stmt, dt[0], &vec_oprnds0,
4133 vect_pow2 (multi_step_cvt) - 1);
ebfd146a
IR
4134 }
4135
4a00c761
JJ
4136 /* Arguments are ready. Create the new vector stmts. */
4137 if (cvt_type)
9771b263 4138 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
4a00c761
JJ
4139 {
4140 if (codecvt1 == CALL_EXPR)
4141 {
4142 new_stmt = gimple_build_call (decl1, 1, vop0);
4143 new_temp = make_ssa_name (vec_dest, new_stmt);
4144 gimple_call_set_lhs (new_stmt, new_temp);
4145 }
4146 else
4147 {
4148 gcc_assert (TREE_CODE_LENGTH (codecvt1) == unary_op);
b731b390 4149 new_temp = make_ssa_name (vec_dest);
0d0e4a03
JJ
4150 new_stmt = gimple_build_assign (new_temp, codecvt1,
4151 vop0);
4a00c761 4152 }
ebfd146a 4153
4a00c761 4154 vect_finish_stmt_generation (stmt, new_stmt, gsi);
9771b263 4155 vec_oprnds0[i] = new_temp;
4a00c761 4156 }
ebfd146a 4157
4a00c761
JJ
4158 vect_create_vectorized_demotion_stmts (&vec_oprnds0, multi_step_cvt,
4159 stmt, vec_dsts, gsi,
4160 slp_node, code1,
4161 &prev_stmt_info);
ebfd146a
IR
4162 }
4163
4164 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4a00c761 4165 break;
ebfd146a
IR
4166 }
4167
9771b263
DN
4168 vec_oprnds0.release ();
4169 vec_oprnds1.release ();
4170 vec_dsts.release ();
4171 interm_types.release ();
ebfd146a
IR
4172
4173 return true;
4174}
ff802fa1
IR
4175
4176
ebfd146a
IR
4177/* Function vectorizable_assignment.
4178
b8698a0f
L
4179 Check if STMT performs an assignment (copy) that can be vectorized.
4180 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
4181 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4182 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4183
4184static bool
355fe088
TS
4185vectorizable_assignment (gimple *stmt, gimple_stmt_iterator *gsi,
4186 gimple **vec_stmt, slp_tree slp_node)
ebfd146a
IR
4187{
4188 tree vec_dest;
4189 tree scalar_dest;
4190 tree op;
4191 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
ebfd146a
IR
4192 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4193 tree new_temp;
355fe088 4194 gimple *def_stmt;
ebfd146a 4195 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
ebfd146a 4196 int ncopies;
f18b55bd 4197 int i, j;
6e1aa848 4198 vec<tree> vec_oprnds = vNULL;
ebfd146a 4199 tree vop;
a70d6342 4200 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 4201 vec_info *vinfo = stmt_info->vinfo;
355fe088 4202 gimple *new_stmt = NULL;
f18b55bd 4203 stmt_vec_info prev_stmt_info = NULL;
fde9c428
RG
4204 enum tree_code code;
4205 tree vectype_in;
ebfd146a 4206
a70d6342 4207 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
4208 return false;
4209
66c16fd9
RB
4210 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4211 && ! vec_stmt)
ebfd146a
IR
4212 return false;
4213
4214 /* Is vectorizable assignment? */
4215 if (!is_gimple_assign (stmt))
4216 return false;
4217
4218 scalar_dest = gimple_assign_lhs (stmt);
4219 if (TREE_CODE (scalar_dest) != SSA_NAME)
4220 return false;
4221
fde9c428 4222 code = gimple_assign_rhs_code (stmt);
ebfd146a 4223 if (gimple_assign_single_p (stmt)
fde9c428
RG
4224 || code == PAREN_EXPR
4225 || CONVERT_EXPR_CODE_P (code))
ebfd146a
IR
4226 op = gimple_assign_rhs1 (stmt);
4227 else
4228 return false;
4229
7b7ec6c5
RG
4230 if (code == VIEW_CONVERT_EXPR)
4231 op = TREE_OPERAND (op, 0);
4232
465c8c19
JJ
4233 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4234 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
4235
4236 /* Multiple types in SLP are handled by creating the appropriate number of
4237 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4238 case of SLP. */
fce57248 4239 if (slp_node)
465c8c19
JJ
4240 ncopies = 1;
4241 else
4242 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
4243
4244 gcc_assert (ncopies >= 1);
4245
81c40241 4246 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt[0], &vectype_in))
ebfd146a 4247 {
73fbfcad 4248 if (dump_enabled_p ())
78c60e3d 4249 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4250 "use not simple.\n");
ebfd146a
IR
4251 return false;
4252 }
4253
fde9c428
RG
4254 /* We can handle NOP_EXPR conversions that do not change the number
4255 of elements or the vector size. */
7b7ec6c5
RG
4256 if ((CONVERT_EXPR_CODE_P (code)
4257 || code == VIEW_CONVERT_EXPR)
fde9c428
RG
4258 && (!vectype_in
4259 || TYPE_VECTOR_SUBPARTS (vectype_in) != nunits
4260 || (GET_MODE_SIZE (TYPE_MODE (vectype))
4261 != GET_MODE_SIZE (TYPE_MODE (vectype_in)))))
4262 return false;
4263
7b7b1813
RG
4264 /* We do not handle bit-precision changes. */
4265 if ((CONVERT_EXPR_CODE_P (code)
4266 || code == VIEW_CONVERT_EXPR)
4267 && INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
4268 && ((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4269 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4270 || ((TYPE_PRECISION (TREE_TYPE (op))
4271 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op))))))
4272 /* But a conversion that does not change the bit-pattern is ok. */
4273 && !((TYPE_PRECISION (TREE_TYPE (scalar_dest))
4274 > TYPE_PRECISION (TREE_TYPE (op)))
2dab46d5
IE
4275 && TYPE_UNSIGNED (TREE_TYPE (op)))
4276 /* Conversion between boolean types of different sizes is
4277 a simple assignment in case their vectypes are same
4278 boolean vectors. */
4279 && (!VECTOR_BOOLEAN_TYPE_P (vectype)
4280 || !VECTOR_BOOLEAN_TYPE_P (vectype_in)))
7b7b1813 4281 {
73fbfcad 4282 if (dump_enabled_p ())
78c60e3d
SS
4283 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4284 "type conversion to/from bit-precision "
e645e942 4285 "unsupported.\n");
7b7b1813
RG
4286 return false;
4287 }
4288
ebfd146a
IR
4289 if (!vec_stmt) /* transformation not required. */
4290 {
4291 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
73fbfcad 4292 if (dump_enabled_p ())
78c60e3d 4293 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4294 "=== vectorizable_assignment ===\n");
c3e7ee41 4295 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
ebfd146a
IR
4296 return true;
4297 }
4298
4299 /** Transform. **/
73fbfcad 4300 if (dump_enabled_p ())
e645e942 4301 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
ebfd146a
IR
4302
4303 /* Handle def. */
4304 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4305
4306 /* Handle use. */
f18b55bd 4307 for (j = 0; j < ncopies; j++)
ebfd146a 4308 {
f18b55bd
IR
4309 /* Handle uses. */
4310 if (j == 0)
d092494c 4311 vect_get_vec_defs (op, NULL, stmt, &vec_oprnds, NULL, slp_node, -1);
f18b55bd
IR
4312 else
4313 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds, NULL);
4314
4315 /* Arguments are ready. create the new vector stmt. */
9771b263 4316 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
f18b55bd 4317 {
7b7ec6c5
RG
4318 if (CONVERT_EXPR_CODE_P (code)
4319 || code == VIEW_CONVERT_EXPR)
4a73490d 4320 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
f18b55bd
IR
4321 new_stmt = gimple_build_assign (vec_dest, vop);
4322 new_temp = make_ssa_name (vec_dest, new_stmt);
4323 gimple_assign_set_lhs (new_stmt, new_temp);
4324 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4325 if (slp_node)
9771b263 4326 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
f18b55bd 4327 }
ebfd146a
IR
4328
4329 if (slp_node)
f18b55bd
IR
4330 continue;
4331
4332 if (j == 0)
4333 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4334 else
4335 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4336
4337 prev_stmt_info = vinfo_for_stmt (new_stmt);
4338 }
b8698a0f 4339
9771b263 4340 vec_oprnds.release ();
ebfd146a
IR
4341 return true;
4342}
4343
9dc3f7de 4344
1107f3ae
IR
4345/* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
4346 either as shift by a scalar or by a vector. */
4347
4348bool
4349vect_supportable_shift (enum tree_code code, tree scalar_type)
4350{
4351
ef4bddc2 4352 machine_mode vec_mode;
1107f3ae
IR
4353 optab optab;
4354 int icode;
4355 tree vectype;
4356
4357 vectype = get_vectype_for_scalar_type (scalar_type);
4358 if (!vectype)
4359 return false;
4360
4361 optab = optab_for_tree_code (code, vectype, optab_scalar);
4362 if (!optab
4363 || optab_handler (optab, TYPE_MODE (vectype)) == CODE_FOR_nothing)
4364 {
4365 optab = optab_for_tree_code (code, vectype, optab_vector);
4366 if (!optab
4367 || (optab_handler (optab, TYPE_MODE (vectype))
4368 == CODE_FOR_nothing))
4369 return false;
4370 }
4371
4372 vec_mode = TYPE_MODE (vectype);
4373 icode = (int) optab_handler (optab, vec_mode);
4374 if (icode == CODE_FOR_nothing)
4375 return false;
4376
4377 return true;
4378}
4379
4380
9dc3f7de
IR
4381/* Function vectorizable_shift.
4382
4383 Check if STMT performs a shift operation that can be vectorized.
4384 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4385 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4386 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4387
4388static bool
355fe088
TS
4389vectorizable_shift (gimple *stmt, gimple_stmt_iterator *gsi,
4390 gimple **vec_stmt, slp_tree slp_node)
9dc3f7de
IR
4391{
4392 tree vec_dest;
4393 tree scalar_dest;
4394 tree op0, op1 = NULL;
4395 tree vec_oprnd1 = NULL_TREE;
4396 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4397 tree vectype;
4398 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4399 enum tree_code code;
ef4bddc2 4400 machine_mode vec_mode;
9dc3f7de
IR
4401 tree new_temp;
4402 optab optab;
4403 int icode;
ef4bddc2 4404 machine_mode optab_op2_mode;
355fe088 4405 gimple *def_stmt;
9dc3f7de 4406 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
355fe088 4407 gimple *new_stmt = NULL;
9dc3f7de
IR
4408 stmt_vec_info prev_stmt_info;
4409 int nunits_in;
4410 int nunits_out;
4411 tree vectype_out;
cede2577 4412 tree op1_vectype;
9dc3f7de
IR
4413 int ncopies;
4414 int j, i;
6e1aa848
DN
4415 vec<tree> vec_oprnds0 = vNULL;
4416 vec<tree> vec_oprnds1 = vNULL;
9dc3f7de
IR
4417 tree vop0, vop1;
4418 unsigned int k;
49eab32e 4419 bool scalar_shift_arg = true;
9dc3f7de 4420 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 4421 vec_info *vinfo = stmt_info->vinfo;
9dc3f7de
IR
4422 int vf;
4423
4424 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4425 return false;
4426
66c16fd9
RB
4427 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4428 && ! vec_stmt)
9dc3f7de
IR
4429 return false;
4430
4431 /* Is STMT a vectorizable binary/unary operation? */
4432 if (!is_gimple_assign (stmt))
4433 return false;
4434
4435 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4436 return false;
4437
4438 code = gimple_assign_rhs_code (stmt);
4439
4440 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4441 || code == RROTATE_EXPR))
4442 return false;
4443
4444 scalar_dest = gimple_assign_lhs (stmt);
4445 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
7b7b1813
RG
4446 if (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4447 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
4448 {
73fbfcad 4449 if (dump_enabled_p ())
78c60e3d 4450 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4451 "bit-precision shifts not supported.\n");
7b7b1813
RG
4452 return false;
4453 }
9dc3f7de
IR
4454
4455 op0 = gimple_assign_rhs1 (stmt);
81c40241 4456 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
9dc3f7de 4457 {
73fbfcad 4458 if (dump_enabled_p ())
78c60e3d 4459 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4460 "use not simple.\n");
9dc3f7de
IR
4461 return false;
4462 }
4463 /* If op0 is an external or constant def use a vector type with
4464 the same size as the output vector type. */
4465 if (!vectype)
4466 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4467 if (vec_stmt)
4468 gcc_assert (vectype);
4469 if (!vectype)
4470 {
73fbfcad 4471 if (dump_enabled_p ())
78c60e3d 4472 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4473 "no vectype for scalar type\n");
9dc3f7de
IR
4474 return false;
4475 }
4476
4477 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4478 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4479 if (nunits_out != nunits_in)
4480 return false;
4481
4482 op1 = gimple_assign_rhs2 (stmt);
81c40241 4483 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1], &op1_vectype))
9dc3f7de 4484 {
73fbfcad 4485 if (dump_enabled_p ())
78c60e3d 4486 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4487 "use not simple.\n");
9dc3f7de
IR
4488 return false;
4489 }
4490
4491 if (loop_vinfo)
4492 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4493 else
4494 vf = 1;
4495
4496 /* Multiple types in SLP are handled by creating the appropriate number of
4497 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
4498 case of SLP. */
fce57248 4499 if (slp_node)
9dc3f7de
IR
4500 ncopies = 1;
4501 else
4502 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4503
4504 gcc_assert (ncopies >= 1);
4505
4506 /* Determine whether the shift amount is a vector, or scalar. If the
4507 shift/rotate amount is a vector, use the vector/vector shift optabs. */
4508
dbfa87aa
YR
4509 if ((dt[1] == vect_internal_def
4510 || dt[1] == vect_induction_def)
4511 && !slp_node)
49eab32e
JJ
4512 scalar_shift_arg = false;
4513 else if (dt[1] == vect_constant_def
4514 || dt[1] == vect_external_def
4515 || dt[1] == vect_internal_def)
4516 {
4517 /* In SLP, need to check whether the shift count is the same,
4518 in loops if it is a constant or invariant, it is always
4519 a scalar shift. */
4520 if (slp_node)
4521 {
355fe088
TS
4522 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4523 gimple *slpstmt;
49eab32e 4524
9771b263 4525 FOR_EACH_VEC_ELT (stmts, k, slpstmt)
49eab32e
JJ
4526 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
4527 scalar_shift_arg = false;
4528 }
60d393e8
RB
4529
4530 /* If the shift amount is computed by a pattern stmt we cannot
4531 use the scalar amount directly thus give up and use a vector
4532 shift. */
4533 if (dt[1] == vect_internal_def)
4534 {
4535 gimple *def = SSA_NAME_DEF_STMT (op1);
4536 if (is_pattern_stmt_p (vinfo_for_stmt (def)))
4537 scalar_shift_arg = false;
4538 }
49eab32e
JJ
4539 }
4540 else
4541 {
73fbfcad 4542 if (dump_enabled_p ())
78c60e3d 4543 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4544 "operand mode requires invariant argument.\n");
49eab32e
JJ
4545 return false;
4546 }
4547
9dc3f7de 4548 /* Vector shifted by vector. */
49eab32e 4549 if (!scalar_shift_arg)
9dc3f7de
IR
4550 {
4551 optab = optab_for_tree_code (code, vectype, optab_vector);
73fbfcad 4552 if (dump_enabled_p ())
78c60e3d 4553 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4554 "vector/vector shift/rotate found.\n");
78c60e3d 4555
aa948027
JJ
4556 if (!op1_vectype)
4557 op1_vectype = get_same_sized_vectype (TREE_TYPE (op1), vectype_out);
4558 if (op1_vectype == NULL_TREE
4559 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype))
cede2577 4560 {
73fbfcad 4561 if (dump_enabled_p ())
78c60e3d
SS
4562 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4563 "unusable type for last operand in"
e645e942 4564 " vector/vector shift/rotate.\n");
cede2577
JJ
4565 return false;
4566 }
9dc3f7de
IR
4567 }
4568 /* See if the machine has a vector shifted by scalar insn and if not
4569 then see if it has a vector shifted by vector insn. */
49eab32e 4570 else
9dc3f7de
IR
4571 {
4572 optab = optab_for_tree_code (code, vectype, optab_scalar);
4573 if (optab
4574 && optab_handler (optab, TYPE_MODE (vectype)) != CODE_FOR_nothing)
4575 {
73fbfcad 4576 if (dump_enabled_p ())
78c60e3d 4577 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4578 "vector/scalar shift/rotate found.\n");
9dc3f7de
IR
4579 }
4580 else
4581 {
4582 optab = optab_for_tree_code (code, vectype, optab_vector);
4583 if (optab
4584 && (optab_handler (optab, TYPE_MODE (vectype))
4585 != CODE_FOR_nothing))
4586 {
49eab32e
JJ
4587 scalar_shift_arg = false;
4588
73fbfcad 4589 if (dump_enabled_p ())
78c60e3d 4590 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4591 "vector/vector shift/rotate found.\n");
9dc3f7de
IR
4592
4593 /* Unlike the other binary operators, shifts/rotates have
4594 the rhs being int, instead of the same type as the lhs,
4595 so make sure the scalar is the right type if we are
aa948027 4596 dealing with vectors of long long/long/short/char. */
9dc3f7de
IR
4597 if (dt[1] == vect_constant_def)
4598 op1 = fold_convert (TREE_TYPE (vectype), op1);
aa948027
JJ
4599 else if (!useless_type_conversion_p (TREE_TYPE (vectype),
4600 TREE_TYPE (op1)))
4601 {
4602 if (slp_node
4603 && TYPE_MODE (TREE_TYPE (vectype))
4604 != TYPE_MODE (TREE_TYPE (op1)))
4605 {
73fbfcad 4606 if (dump_enabled_p ())
78c60e3d
SS
4607 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4608 "unusable type for last operand in"
e645e942 4609 " vector/vector shift/rotate.\n");
21c0a521 4610 return false;
aa948027
JJ
4611 }
4612 if (vec_stmt && !slp_node)
4613 {
4614 op1 = fold_convert (TREE_TYPE (vectype), op1);
4615 op1 = vect_init_vector (stmt, op1,
4616 TREE_TYPE (vectype), NULL);
4617 }
4618 }
9dc3f7de
IR
4619 }
4620 }
4621 }
9dc3f7de
IR
4622
4623 /* Supportable by target? */
4624 if (!optab)
4625 {
73fbfcad 4626 if (dump_enabled_p ())
78c60e3d 4627 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4628 "no optab.\n");
9dc3f7de
IR
4629 return false;
4630 }
4631 vec_mode = TYPE_MODE (vectype);
4632 icode = (int) optab_handler (optab, vec_mode);
4633 if (icode == CODE_FOR_nothing)
4634 {
73fbfcad 4635 if (dump_enabled_p ())
78c60e3d 4636 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4637 "op not supported by target.\n");
9dc3f7de
IR
4638 /* Check only during analysis. */
4639 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4640 || (vf < vect_min_worthwhile_factor (code)
4641 && !vec_stmt))
4642 return false;
73fbfcad 4643 if (dump_enabled_p ())
e645e942
TJ
4644 dump_printf_loc (MSG_NOTE, vect_location,
4645 "proceeding using word mode.\n");
9dc3f7de
IR
4646 }
4647
4648 /* Worthwhile without SIMD support? Check only during analysis. */
4649 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
4650 && vf < vect_min_worthwhile_factor (code)
4651 && !vec_stmt)
4652 {
73fbfcad 4653 if (dump_enabled_p ())
78c60e3d 4654 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4655 "not worthwhile without SIMD support.\n");
9dc3f7de
IR
4656 return false;
4657 }
4658
4659 if (!vec_stmt) /* transformation not required. */
4660 {
4661 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
73fbfcad 4662 if (dump_enabled_p ())
e645e942
TJ
4663 dump_printf_loc (MSG_NOTE, vect_location,
4664 "=== vectorizable_shift ===\n");
c3e7ee41 4665 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
9dc3f7de
IR
4666 return true;
4667 }
4668
4669 /** Transform. **/
4670
73fbfcad 4671 if (dump_enabled_p ())
78c60e3d 4672 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4673 "transform binary/unary operation.\n");
9dc3f7de
IR
4674
4675 /* Handle def. */
4676 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4677
9dc3f7de
IR
4678 prev_stmt_info = NULL;
4679 for (j = 0; j < ncopies; j++)
4680 {
4681 /* Handle uses. */
4682 if (j == 0)
4683 {
4684 if (scalar_shift_arg)
4685 {
4686 /* Vector shl and shr insn patterns can be defined with scalar
4687 operand 2 (shift operand). In this case, use constant or loop
4688 invariant op1 directly, without extending it to vector mode
4689 first. */
4690 optab_op2_mode = insn_data[icode].operand[2].mode;
4691 if (!VECTOR_MODE_P (optab_op2_mode))
4692 {
73fbfcad 4693 if (dump_enabled_p ())
78c60e3d 4694 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4695 "operand 1 using scalar mode.\n");
9dc3f7de 4696 vec_oprnd1 = op1;
8930f723 4697 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : 1);
9771b263 4698 vec_oprnds1.quick_push (vec_oprnd1);
9dc3f7de
IR
4699 if (slp_node)
4700 {
4701 /* Store vec_oprnd1 for every vector stmt to be created
4702 for SLP_NODE. We check during the analysis that all
4703 the shift arguments are the same.
4704 TODO: Allow different constants for different vector
4705 stmts generated for an SLP instance. */
4706 for (k = 0; k < slp_node->vec_stmts_size - 1; k++)
9771b263 4707 vec_oprnds1.quick_push (vec_oprnd1);
9dc3f7de
IR
4708 }
4709 }
4710 }
4711
4712 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
4713 (a special case for certain kind of vector shifts); otherwise,
4714 operand 1 should be of a vector type (the usual case). */
4715 if (vec_oprnd1)
4716 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
d092494c 4717 slp_node, -1);
9dc3f7de
IR
4718 else
4719 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
d092494c 4720 slp_node, -1);
9dc3f7de
IR
4721 }
4722 else
4723 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
4724
4725 /* Arguments are ready. Create the new vector stmt. */
9771b263 4726 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
9dc3f7de 4727 {
9771b263 4728 vop1 = vec_oprnds1[i];
0d0e4a03 4729 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
9dc3f7de
IR
4730 new_temp = make_ssa_name (vec_dest, new_stmt);
4731 gimple_assign_set_lhs (new_stmt, new_temp);
4732 vect_finish_stmt_generation (stmt, new_stmt, gsi);
4733 if (slp_node)
9771b263 4734 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
9dc3f7de
IR
4735 }
4736
4737 if (slp_node)
4738 continue;
4739
4740 if (j == 0)
4741 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
4742 else
4743 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
4744 prev_stmt_info = vinfo_for_stmt (new_stmt);
4745 }
4746
9771b263
DN
4747 vec_oprnds0.release ();
4748 vec_oprnds1.release ();
9dc3f7de
IR
4749
4750 return true;
4751}
4752
4753
ebfd146a
IR
4754/* Function vectorizable_operation.
4755
16949072
RG
4756 Check if STMT performs a binary, unary or ternary operation that can
4757 be vectorized.
b8698a0f 4758 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
4759 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
4760 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
4761
4762static bool
355fe088
TS
4763vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi,
4764 gimple **vec_stmt, slp_tree slp_node)
ebfd146a 4765{
00f07b86 4766 tree vec_dest;
ebfd146a 4767 tree scalar_dest;
16949072 4768 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
ebfd146a 4769 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
00f07b86 4770 tree vectype;
ebfd146a
IR
4771 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4772 enum tree_code code;
ef4bddc2 4773 machine_mode vec_mode;
ebfd146a
IR
4774 tree new_temp;
4775 int op_type;
00f07b86 4776 optab optab;
523ba738 4777 bool target_support_p;
355fe088 4778 gimple *def_stmt;
16949072
RG
4779 enum vect_def_type dt[3]
4780 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
355fe088 4781 gimple *new_stmt = NULL;
ebfd146a 4782 stmt_vec_info prev_stmt_info;
b690cc0f 4783 int nunits_in;
ebfd146a
IR
4784 int nunits_out;
4785 tree vectype_out;
4786 int ncopies;
4787 int j, i;
6e1aa848
DN
4788 vec<tree> vec_oprnds0 = vNULL;
4789 vec<tree> vec_oprnds1 = vNULL;
4790 vec<tree> vec_oprnds2 = vNULL;
16949072 4791 tree vop0, vop1, vop2;
a70d6342 4792 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 4793 vec_info *vinfo = stmt_info->vinfo;
a70d6342
IR
4794 int vf;
4795
a70d6342 4796 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
4797 return false;
4798
66c16fd9
RB
4799 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4800 && ! vec_stmt)
ebfd146a
IR
4801 return false;
4802
4803 /* Is STMT a vectorizable binary/unary operation? */
4804 if (!is_gimple_assign (stmt))
4805 return false;
4806
4807 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
4808 return false;
4809
ebfd146a
IR
4810 code = gimple_assign_rhs_code (stmt);
4811
4812 /* For pointer addition, we should use the normal plus for
4813 the vector addition. */
4814 if (code == POINTER_PLUS_EXPR)
4815 code = PLUS_EXPR;
4816
4817 /* Support only unary or binary operations. */
4818 op_type = TREE_CODE_LENGTH (code);
16949072 4819 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
ebfd146a 4820 {
73fbfcad 4821 if (dump_enabled_p ())
78c60e3d 4822 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4823 "num. args = %d (not unary/binary/ternary op).\n",
78c60e3d 4824 op_type);
ebfd146a
IR
4825 return false;
4826 }
4827
b690cc0f
RG
4828 scalar_dest = gimple_assign_lhs (stmt);
4829 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4830
7b7b1813
RG
4831 /* Most operations cannot handle bit-precision types without extra
4832 truncations. */
045c1278
IE
4833 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
4834 && (TYPE_PRECISION (TREE_TYPE (scalar_dest))
4835 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (scalar_dest))))
7b7b1813
RG
4836 /* Exception are bitwise binary operations. */
4837 && code != BIT_IOR_EXPR
4838 && code != BIT_XOR_EXPR
4839 && code != BIT_AND_EXPR)
4840 {
73fbfcad 4841 if (dump_enabled_p ())
78c60e3d 4842 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4843 "bit-precision arithmetic not supported.\n");
7b7b1813
RG
4844 return false;
4845 }
4846
ebfd146a 4847 op0 = gimple_assign_rhs1 (stmt);
81c40241 4848 if (!vect_is_simple_use (op0, vinfo, &def_stmt, &dt[0], &vectype))
ebfd146a 4849 {
73fbfcad 4850 if (dump_enabled_p ())
78c60e3d 4851 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4852 "use not simple.\n");
ebfd146a
IR
4853 return false;
4854 }
b690cc0f
RG
4855 /* If op0 is an external or constant def use a vector type with
4856 the same size as the output vector type. */
4857 if (!vectype)
b036c6c5
IE
4858 {
4859 /* For boolean type we cannot determine vectype by
4860 invariant value (don't know whether it is a vector
4861 of booleans or vector of integers). We use output
4862 vectype because operations on boolean don't change
4863 type. */
4864 if (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE)
4865 {
4866 if (TREE_CODE (TREE_TYPE (scalar_dest)) != BOOLEAN_TYPE)
4867 {
4868 if (dump_enabled_p ())
4869 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4870 "not supported operation on bool value.\n");
4871 return false;
4872 }
4873 vectype = vectype_out;
4874 }
4875 else
4876 vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out);
4877 }
7d8930a0
IR
4878 if (vec_stmt)
4879 gcc_assert (vectype);
4880 if (!vectype)
4881 {
73fbfcad 4882 if (dump_enabled_p ())
7d8930a0 4883 {
78c60e3d
SS
4884 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4885 "no vectype for scalar type ");
4886 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
4887 TREE_TYPE (op0));
e645e942 4888 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
7d8930a0
IR
4889 }
4890
4891 return false;
4892 }
b690cc0f
RG
4893
4894 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
4895 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
4896 if (nunits_out != nunits_in)
4897 return false;
ebfd146a 4898
16949072 4899 if (op_type == binary_op || op_type == ternary_op)
ebfd146a
IR
4900 {
4901 op1 = gimple_assign_rhs2 (stmt);
81c40241 4902 if (!vect_is_simple_use (op1, vinfo, &def_stmt, &dt[1]))
ebfd146a 4903 {
73fbfcad 4904 if (dump_enabled_p ())
78c60e3d 4905 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4906 "use not simple.\n");
ebfd146a
IR
4907 return false;
4908 }
4909 }
16949072
RG
4910 if (op_type == ternary_op)
4911 {
4912 op2 = gimple_assign_rhs3 (stmt);
81c40241 4913 if (!vect_is_simple_use (op2, vinfo, &def_stmt, &dt[2]))
16949072 4914 {
73fbfcad 4915 if (dump_enabled_p ())
78c60e3d 4916 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4917 "use not simple.\n");
16949072
RG
4918 return false;
4919 }
4920 }
ebfd146a 4921
b690cc0f
RG
4922 if (loop_vinfo)
4923 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
4924 else
4925 vf = 1;
4926
4927 /* Multiple types in SLP are handled by creating the appropriate number of
ff802fa1 4928 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
b690cc0f 4929 case of SLP. */
fce57248 4930 if (slp_node)
b690cc0f
RG
4931 ncopies = 1;
4932 else
4933 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits_in;
4934
4935 gcc_assert (ncopies >= 1);
4936
9dc3f7de 4937 /* Shifts are handled in vectorizable_shift (). */
ebfd146a
IR
4938 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
4939 || code == RROTATE_EXPR)
9dc3f7de 4940 return false;
ebfd146a 4941
ebfd146a 4942 /* Supportable by target? */
00f07b86
RH
4943
4944 vec_mode = TYPE_MODE (vectype);
4945 if (code == MULT_HIGHPART_EXPR)
523ba738 4946 target_support_p = can_mult_highpart_p (vec_mode, TYPE_UNSIGNED (vectype));
00f07b86
RH
4947 else
4948 {
4949 optab = optab_for_tree_code (code, vectype, optab_default);
4950 if (!optab)
5deb57cb 4951 {
73fbfcad 4952 if (dump_enabled_p ())
78c60e3d 4953 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4954 "no optab.\n");
00f07b86 4955 return false;
5deb57cb 4956 }
523ba738
RS
4957 target_support_p = (optab_handler (optab, vec_mode)
4958 != CODE_FOR_nothing);
5deb57cb
JJ
4959 }
4960
523ba738 4961 if (!target_support_p)
ebfd146a 4962 {
73fbfcad 4963 if (dump_enabled_p ())
78c60e3d 4964 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4965 "op not supported by target.\n");
ebfd146a
IR
4966 /* Check only during analysis. */
4967 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
5deb57cb 4968 || (!vec_stmt && vf < vect_min_worthwhile_factor (code)))
ebfd146a 4969 return false;
73fbfcad 4970 if (dump_enabled_p ())
e645e942
TJ
4971 dump_printf_loc (MSG_NOTE, vect_location,
4972 "proceeding using word mode.\n");
383d9c83
IR
4973 }
4974
4a00c761 4975 /* Worthwhile without SIMD support? Check only during analysis. */
5deb57cb
JJ
4976 if (!VECTOR_MODE_P (vec_mode)
4977 && !vec_stmt
4978 && vf < vect_min_worthwhile_factor (code))
7d8930a0 4979 {
73fbfcad 4980 if (dump_enabled_p ())
78c60e3d 4981 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 4982 "not worthwhile without SIMD support.\n");
e34842c6 4983 return false;
7d8930a0 4984 }
ebfd146a 4985
ebfd146a
IR
4986 if (!vec_stmt) /* transformation not required. */
4987 {
4a00c761 4988 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
73fbfcad 4989 if (dump_enabled_p ())
78c60e3d 4990 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 4991 "=== vectorizable_operation ===\n");
c3e7ee41 4992 vect_model_simple_cost (stmt_info, ncopies, dt, NULL, NULL);
ebfd146a
IR
4993 return true;
4994 }
4995
4996 /** Transform. **/
4997
73fbfcad 4998 if (dump_enabled_p ())
78c60e3d 4999 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5000 "transform binary/unary operation.\n");
383d9c83 5001
ebfd146a 5002 /* Handle def. */
00f07b86 5003 vec_dest = vect_create_destination_var (scalar_dest, vectype);
b8698a0f 5004
ebfd146a
IR
5005 /* In case the vectorization factor (VF) is bigger than the number
5006 of elements that we can fit in a vectype (nunits), we have to generate
5007 more than one vector stmt - i.e - we need to "unroll" the
4a00c761
JJ
5008 vector stmt by a factor VF/nunits. In doing so, we record a pointer
5009 from one copy of the vector stmt to the next, in the field
5010 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
5011 stages to find the correct vector defs to be used when vectorizing
5012 stmts that use the defs of the current stmt. The example below
5013 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
5014 we need to create 4 vectorized stmts):
5015
5016 before vectorization:
5017 RELATED_STMT VEC_STMT
5018 S1: x = memref - -
5019 S2: z = x + 1 - -
5020
5021 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
5022 there):
5023 RELATED_STMT VEC_STMT
5024 VS1_0: vx0 = memref0 VS1_1 -
5025 VS1_1: vx1 = memref1 VS1_2 -
5026 VS1_2: vx2 = memref2 VS1_3 -
5027 VS1_3: vx3 = memref3 - -
5028 S1: x = load - VS1_0
5029 S2: z = x + 1 - -
5030
5031 step2: vectorize stmt S2 (done here):
5032 To vectorize stmt S2 we first need to find the relevant vector
5033 def for the first operand 'x'. This is, as usual, obtained from
5034 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
5035 that defines 'x' (S1). This way we find the stmt VS1_0, and the
5036 relevant vector def 'vx0'. Having found 'vx0' we can generate
5037 the vector stmt VS2_0, and as usual, record it in the
5038 STMT_VINFO_VEC_STMT of stmt S2.
5039 When creating the second copy (VS2_1), we obtain the relevant vector
5040 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
5041 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
5042 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
5043 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
5044 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
5045 chain of stmts and pointers:
5046 RELATED_STMT VEC_STMT
5047 VS1_0: vx0 = memref0 VS1_1 -
5048 VS1_1: vx1 = memref1 VS1_2 -
5049 VS1_2: vx2 = memref2 VS1_3 -
5050 VS1_3: vx3 = memref3 - -
5051 S1: x = load - VS1_0
5052 VS2_0: vz0 = vx0 + v1 VS2_1 -
5053 VS2_1: vz1 = vx1 + v1 VS2_2 -
5054 VS2_2: vz2 = vx2 + v1 VS2_3 -
5055 VS2_3: vz3 = vx3 + v1 - -
5056 S2: z = x + 1 - VS2_0 */
ebfd146a
IR
5057
5058 prev_stmt_info = NULL;
5059 for (j = 0; j < ncopies; j++)
5060 {
5061 /* Handle uses. */
5062 if (j == 0)
4a00c761
JJ
5063 {
5064 if (op_type == binary_op || op_type == ternary_op)
5065 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
5066 slp_node, -1);
5067 else
5068 vect_get_vec_defs (op0, NULL_TREE, stmt, &vec_oprnds0, NULL,
5069 slp_node, -1);
5070 if (op_type == ternary_op)
36ba4aae 5071 {
9771b263
DN
5072 vec_oprnds2.create (1);
5073 vec_oprnds2.quick_push (vect_get_vec_def_for_operand (op2,
81c40241 5074 stmt));
36ba4aae 5075 }
4a00c761 5076 }
ebfd146a 5077 else
4a00c761
JJ
5078 {
5079 vect_get_vec_defs_for_stmt_copy (dt, &vec_oprnds0, &vec_oprnds1);
5080 if (op_type == ternary_op)
5081 {
9771b263
DN
5082 tree vec_oprnd = vec_oprnds2.pop ();
5083 vec_oprnds2.quick_push (vect_get_vec_def_for_stmt_copy (dt[2],
5084 vec_oprnd));
4a00c761
JJ
5085 }
5086 }
5087
5088 /* Arguments are ready. Create the new vector stmt. */
9771b263 5089 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
ebfd146a 5090 {
4a00c761 5091 vop1 = ((op_type == binary_op || op_type == ternary_op)
9771b263 5092 ? vec_oprnds1[i] : NULL_TREE);
4a00c761 5093 vop2 = ((op_type == ternary_op)
9771b263 5094 ? vec_oprnds2[i] : NULL_TREE);
0d0e4a03 5095 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
4a00c761
JJ
5096 new_temp = make_ssa_name (vec_dest, new_stmt);
5097 gimple_assign_set_lhs (new_stmt, new_temp);
5098 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5099 if (slp_node)
9771b263 5100 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
ebfd146a
IR
5101 }
5102
4a00c761
JJ
5103 if (slp_node)
5104 continue;
5105
5106 if (j == 0)
5107 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5108 else
5109 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5110 prev_stmt_info = vinfo_for_stmt (new_stmt);
ebfd146a
IR
5111 }
5112
9771b263
DN
5113 vec_oprnds0.release ();
5114 vec_oprnds1.release ();
5115 vec_oprnds2.release ();
ebfd146a 5116
ebfd146a
IR
5117 return true;
5118}
5119
c716e67f
XDL
5120/* A helper function to ensure data reference DR's base alignment
5121 for STMT_INFO. */
5122
5123static void
5124ensure_base_align (stmt_vec_info stmt_info, struct data_reference *dr)
5125{
5126 if (!dr->aux)
5127 return;
5128
52639a61 5129 if (DR_VECT_AUX (dr)->base_misaligned)
c716e67f
XDL
5130 {
5131 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
52639a61 5132 tree base_decl = DR_VECT_AUX (dr)->base_decl;
c716e67f 5133
428f0c67
JH
5134 if (decl_in_symtab_p (base_decl))
5135 symtab_node::get (base_decl)->increase_alignment (TYPE_ALIGN (vectype));
5136 else
5137 {
fe37c7af 5138 SET_DECL_ALIGN (base_decl, TYPE_ALIGN (vectype));
428f0c67
JH
5139 DECL_USER_ALIGN (base_decl) = 1;
5140 }
52639a61 5141 DR_VECT_AUX (dr)->base_misaligned = false;
c716e67f
XDL
5142 }
5143}
5144
ebfd146a 5145
09dfa495
BM
5146/* Given a vector type VECTYPE returns the VECTOR_CST mask that implements
5147 reversal of the vector elements. If that is impossible to do,
5148 returns NULL. */
5149
5150static tree
5151perm_mask_for_reverse (tree vectype)
5152{
5153 int i, nunits;
5154 unsigned char *sel;
5155
5156 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5157 sel = XALLOCAVEC (unsigned char, nunits);
5158
5159 for (i = 0; i < nunits; ++i)
5160 sel[i] = nunits - 1 - i;
5161
557be5a8
AL
5162 if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
5163 return NULL_TREE;
5164 return vect_gen_perm_mask_checked (vectype, sel);
09dfa495
BM
5165}
5166
ebfd146a
IR
5167/* Function vectorizable_store.
5168
b8698a0f
L
5169 Check if STMT defines a non scalar data-ref (array/pointer/structure) that
5170 can be vectorized.
5171 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
5172 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
5173 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5174
5175static bool
355fe088 5176vectorizable_store (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
c716e67f 5177 slp_tree slp_node)
ebfd146a
IR
5178{
5179 tree scalar_dest;
5180 tree data_ref;
5181 tree op;
5182 tree vec_oprnd = NULL_TREE;
5183 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5184 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
272c6793 5185 tree elem_type;
ebfd146a 5186 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
a70d6342 5187 struct loop *loop = NULL;
ef4bddc2 5188 machine_mode vec_mode;
ebfd146a
IR
5189 tree dummy;
5190 enum dr_alignment_support alignment_support_scheme;
355fe088 5191 gimple *def_stmt;
ebfd146a
IR
5192 enum vect_def_type dt;
5193 stmt_vec_info prev_stmt_info = NULL;
5194 tree dataref_ptr = NULL_TREE;
74bf76ed 5195 tree dataref_offset = NULL_TREE;
355fe088 5196 gimple *ptr_incr = NULL;
ebfd146a
IR
5197 int ncopies;
5198 int j;
355fe088 5199 gimple *next_stmt, *first_stmt = NULL;
0d0293ac 5200 bool grouped_store = false;
272c6793 5201 bool store_lanes_p = false;
ebfd146a 5202 unsigned int group_size, i;
6e1aa848
DN
5203 vec<tree> dr_chain = vNULL;
5204 vec<tree> oprnds = vNULL;
5205 vec<tree> result_chain = vNULL;
ebfd146a 5206 bool inv_p;
09dfa495
BM
5207 bool negative = false;
5208 tree offset = NULL_TREE;
6e1aa848 5209 vec<tree> vec_oprnds = vNULL;
ebfd146a 5210 bool slp = (slp_node != NULL);
ebfd146a 5211 unsigned int vec_num;
a70d6342 5212 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
310213d4 5213 vec_info *vinfo = stmt_info->vinfo;
272c6793 5214 tree aggr_type;
3bab6342
AT
5215 tree scatter_base = NULL_TREE, scatter_off = NULL_TREE;
5216 tree scatter_off_vectype = NULL_TREE, scatter_decl = NULL_TREE;
5217 int scatter_scale = 1;
5218 enum vect_def_type scatter_idx_dt = vect_unknown_def_type;
5219 enum vect_def_type scatter_src_dt = vect_unknown_def_type;
355fe088 5220 gimple *new_stmt;
a70d6342 5221
a70d6342 5222 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
ebfd146a
IR
5223 return false;
5224
66c16fd9
RB
5225 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5226 && ! vec_stmt)
ebfd146a
IR
5227 return false;
5228
5229 /* Is vectorizable store? */
5230
5231 if (!is_gimple_assign (stmt))
5232 return false;
5233
5234 scalar_dest = gimple_assign_lhs (stmt);
ab0ef706
JJ
5235 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
5236 && is_pattern_stmt_p (stmt_info))
5237 scalar_dest = TREE_OPERAND (scalar_dest, 0);
ebfd146a 5238 if (TREE_CODE (scalar_dest) != ARRAY_REF
38000232 5239 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
ebfd146a 5240 && TREE_CODE (scalar_dest) != INDIRECT_REF
e9dbe7bb
IR
5241 && TREE_CODE (scalar_dest) != COMPONENT_REF
5242 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
70f34814
RG
5243 && TREE_CODE (scalar_dest) != REALPART_EXPR
5244 && TREE_CODE (scalar_dest) != MEM_REF)
ebfd146a
IR
5245 return false;
5246
fce57248
RS
5247 /* Cannot have hybrid store SLP -- that would mean storing to the
5248 same location twice. */
5249 gcc_assert (slp == PURE_SLP_STMT (stmt_info));
5250
ebfd146a 5251 gcc_assert (gimple_assign_single_p (stmt));
465c8c19 5252
f4d09712 5253 tree vectype = STMT_VINFO_VECTYPE (stmt_info), rhs_vectype = NULL_TREE;
465c8c19
JJ
5254 unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5255
5256 if (loop_vinfo)
5257 loop = LOOP_VINFO_LOOP (loop_vinfo);
5258
5259 /* Multiple types in SLP are handled by creating the appropriate number of
5260 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5261 case of SLP. */
fce57248 5262 if (slp)
465c8c19
JJ
5263 ncopies = 1;
5264 else
5265 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5266
5267 gcc_assert (ncopies >= 1);
5268
5269 /* FORNOW. This restriction should be relaxed. */
5270 if (loop && nested_in_vect_loop_p (loop, stmt) && ncopies > 1)
5271 {
5272 if (dump_enabled_p ())
5273 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5274 "multiple types in nested loop.\n");
5275 return false;
5276 }
5277
ebfd146a 5278 op = gimple_assign_rhs1 (stmt);
f4d09712
KY
5279
5280 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt, &rhs_vectype))
ebfd146a 5281 {
73fbfcad 5282 if (dump_enabled_p ())
78c60e3d 5283 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5284 "use not simple.\n");
ebfd146a
IR
5285 return false;
5286 }
5287
f4d09712
KY
5288 if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
5289 return false;
5290
272c6793 5291 elem_type = TREE_TYPE (vectype);
ebfd146a 5292 vec_mode = TYPE_MODE (vectype);
7b7b1813 5293
ebfd146a
IR
5294 /* FORNOW. In some cases can vectorize even if data-type not supported
5295 (e.g. - array initialization with 0). */
947131ba 5296 if (optab_handler (mov_optab, vec_mode) == CODE_FOR_nothing)
ebfd146a
IR
5297 return false;
5298
5299 if (!STMT_VINFO_DATA_REF (stmt_info))
5300 return false;
5301
f2e2a985 5302 if (!STMT_VINFO_STRIDED_P (stmt_info))
09dfa495 5303 {
f2e2a985
MM
5304 negative =
5305 tree_int_cst_compare (loop && nested_in_vect_loop_p (loop, stmt)
5306 ? STMT_VINFO_DR_STEP (stmt_info) : DR_STEP (dr),
5307 size_zero_node) < 0;
5308 if (negative && ncopies > 1)
09dfa495
BM
5309 {
5310 if (dump_enabled_p ())
5311 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
f2e2a985 5312 "multiple types with negative step.\n");
09dfa495
BM
5313 return false;
5314 }
f2e2a985 5315 if (negative)
09dfa495 5316 {
f2e2a985
MM
5317 gcc_assert (!grouped_store);
5318 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
5319 if (alignment_support_scheme != dr_aligned
5320 && alignment_support_scheme != dr_unaligned_supported)
5321 {
5322 if (dump_enabled_p ())
5323 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5324 "negative step but alignment required.\n");
5325 return false;
5326 }
5327 if (dt != vect_constant_def
5328 && dt != vect_external_def
5329 && !perm_mask_for_reverse (vectype))
5330 {
5331 if (dump_enabled_p ())
5332 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5333 "negative step and reversing not supported.\n");
5334 return false;
5335 }
09dfa495
BM
5336 }
5337 }
5338
0d0293ac 5339 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a 5340 {
0d0293ac 5341 grouped_store = true;
e14c1050 5342 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
cee62fee 5343 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
fce57248 5344 if (!slp && !STMT_VINFO_STRIDED_P (stmt_info))
b602d918 5345 {
272c6793
RS
5346 if (vect_store_lanes_supported (vectype, group_size))
5347 store_lanes_p = true;
0d0293ac 5348 else if (!vect_grouped_store_supported (vectype, group_size))
b602d918
RS
5349 return false;
5350 }
b8698a0f 5351
cee62fee 5352 if (STMT_VINFO_STRIDED_P (stmt_info)
fce57248 5353 && slp
cee62fee
MM
5354 && (group_size > nunits
5355 || nunits % group_size != 0))
5356 {
5357 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5358 "unhandled strided group store\n");
5359 return false;
5360 }
5361
ebfd146a
IR
5362 if (first_stmt == stmt)
5363 {
5364 /* STMT is the leader of the group. Check the operands of all the
5365 stmts of the group. */
e14c1050 5366 next_stmt = GROUP_NEXT_ELEMENT (stmt_info);
ebfd146a
IR
5367 while (next_stmt)
5368 {
5369 gcc_assert (gimple_assign_single_p (next_stmt));
5370 op = gimple_assign_rhs1 (next_stmt);
81c40241 5371 if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt))
ebfd146a 5372 {
73fbfcad 5373 if (dump_enabled_p ())
78c60e3d 5374 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 5375 "use not simple.\n");
ebfd146a
IR
5376 return false;
5377 }
e14c1050 5378 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
ebfd146a
IR
5379 }
5380 }
5381 }
5382
3bab6342
AT
5383 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
5384 {
355fe088 5385 gimple *def_stmt;
3bab6342
AT
5386 scatter_decl = vect_check_gather_scatter (stmt, loop_vinfo, &scatter_base,
5387 &scatter_off, &scatter_scale);
5388 gcc_assert (scatter_decl);
81c40241
RB
5389 if (!vect_is_simple_use (scatter_off, vinfo, &def_stmt, &scatter_idx_dt,
5390 &scatter_off_vectype))
3bab6342
AT
5391 {
5392 if (dump_enabled_p ())
5393 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5394 "scatter index use not simple.");
5395 return false;
5396 }
5397 }
5398
ebfd146a
IR
5399 if (!vec_stmt) /* transformation not required. */
5400 {
5401 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
2e8ab70c
RB
5402 /* The SLP costs are calculated during SLP analysis. */
5403 if (!PURE_SLP_STMT (stmt_info))
5404 vect_model_store_cost (stmt_info, ncopies, store_lanes_p, dt,
5405 NULL, NULL, NULL);
ebfd146a
IR
5406 return true;
5407 }
5408
5409 /** Transform. **/
5410
c716e67f
XDL
5411 ensure_base_align (stmt_info, dr);
5412
3bab6342
AT
5413 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
5414 {
5415 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE, op, src;
5416 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (scatter_decl));
5417 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
5418 tree ptr, mask, var, scale, perm_mask = NULL_TREE;
5419 edge pe = loop_preheader_edge (loop);
5420 gimple_seq seq;
5421 basic_block new_bb;
5422 enum { NARROW, NONE, WIDEN } modifier;
5423 int scatter_off_nunits = TYPE_VECTOR_SUBPARTS (scatter_off_vectype);
5424
5425 if (nunits == (unsigned int) scatter_off_nunits)
5426 modifier = NONE;
5427 else if (nunits == (unsigned int) scatter_off_nunits / 2)
5428 {
5429 unsigned char *sel = XALLOCAVEC (unsigned char, scatter_off_nunits);
5430 modifier = WIDEN;
5431
5432 for (i = 0; i < (unsigned int) scatter_off_nunits; ++i)
5433 sel[i] = i | nunits;
5434
5435 perm_mask = vect_gen_perm_mask_checked (scatter_off_vectype, sel);
5436 gcc_assert (perm_mask != NULL_TREE);
5437 }
5438 else if (nunits == (unsigned int) scatter_off_nunits * 2)
5439 {
5440 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
5441 modifier = NARROW;
5442
5443 for (i = 0; i < (unsigned int) nunits; ++i)
5444 sel[i] = i | scatter_off_nunits;
5445
5446 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
5447 gcc_assert (perm_mask != NULL_TREE);
5448 ncopies *= 2;
5449 }
5450 else
5451 gcc_unreachable ();
5452
5453 rettype = TREE_TYPE (TREE_TYPE (scatter_decl));
5454 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5455 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5456 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5457 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
5458 scaletype = TREE_VALUE (arglist);
5459
5460 gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
5461 && TREE_CODE (rettype) == VOID_TYPE);
5462
5463 ptr = fold_convert (ptrtype, scatter_base);
5464 if (!is_gimple_min_invariant (ptr))
5465 {
5466 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
5467 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
5468 gcc_assert (!new_bb);
5469 }
5470
5471 /* Currently we support only unconditional scatter stores,
5472 so mask should be all ones. */
5473 mask = build_int_cst (masktype, -1);
5474 mask = vect_init_vector (stmt, mask, masktype, NULL);
5475
5476 scale = build_int_cst (scaletype, scatter_scale);
5477
5478 prev_stmt_info = NULL;
5479 for (j = 0; j < ncopies; ++j)
5480 {
5481 if (j == 0)
5482 {
5483 src = vec_oprnd1
81c40241 5484 = vect_get_vec_def_for_operand (gimple_assign_rhs1 (stmt), stmt);
3bab6342 5485 op = vec_oprnd0
81c40241 5486 = vect_get_vec_def_for_operand (scatter_off, stmt);
3bab6342
AT
5487 }
5488 else if (modifier != NONE && (j & 1))
5489 {
5490 if (modifier == WIDEN)
5491 {
5492 src = vec_oprnd1
5493 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5494 op = permute_vec_elements (vec_oprnd0, vec_oprnd0, perm_mask,
5495 stmt, gsi);
5496 }
5497 else if (modifier == NARROW)
5498 {
5499 src = permute_vec_elements (vec_oprnd1, vec_oprnd1, perm_mask,
5500 stmt, gsi);
5501 op = vec_oprnd0
5502 = vect_get_vec_def_for_stmt_copy (scatter_idx_dt, vec_oprnd0);
5503 }
5504 else
5505 gcc_unreachable ();
5506 }
5507 else
5508 {
5509 src = vec_oprnd1
5510 = vect_get_vec_def_for_stmt_copy (scatter_src_dt, vec_oprnd1);
5511 op = vec_oprnd0
5512 = vect_get_vec_def_for_stmt_copy (scatter_idx_dt, vec_oprnd0);
5513 }
5514
5515 if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
5516 {
5517 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src))
5518 == TYPE_VECTOR_SUBPARTS (srctype));
0e22bb5a 5519 var = vect_get_new_ssa_name (srctype, vect_simple_var);
3bab6342
AT
5520 src = build1 (VIEW_CONVERT_EXPR, srctype, src);
5521 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
5522 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5523 src = var;
5524 }
5525
5526 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
5527 {
5528 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
5529 == TYPE_VECTOR_SUBPARTS (idxtype));
0e22bb5a 5530 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3bab6342
AT
5531 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
5532 new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
5533 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5534 op = var;
5535 }
5536
5537 new_stmt
5538 = gimple_build_call (scatter_decl, 5, ptr, mask, op, src, scale);
5539
5540 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5541
5542 if (prev_stmt_info == NULL)
5543 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5544 else
5545 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5546 prev_stmt_info = vinfo_for_stmt (new_stmt);
5547 }
5548 return true;
5549 }
5550
0d0293ac 5551 if (grouped_store)
ebfd146a
IR
5552 {
5553 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
e14c1050 5554 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
ebfd146a 5555
e14c1050 5556 GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))++;
ebfd146a
IR
5557
5558 /* FORNOW */
a70d6342 5559 gcc_assert (!loop || !nested_in_vect_loop_p (loop, stmt));
ebfd146a
IR
5560
5561 /* We vectorize all the stmts of the interleaving group when we
5562 reach the last stmt in the group. */
e14c1050
IR
5563 if (GROUP_STORE_COUNT (vinfo_for_stmt (first_stmt))
5564 < GROUP_SIZE (vinfo_for_stmt (first_stmt))
ebfd146a
IR
5565 && !slp)
5566 {
5567 *vec_stmt = NULL;
5568 return true;
5569 }
5570
5571 if (slp)
4b5caab7 5572 {
0d0293ac 5573 grouped_store = false;
4b5caab7
IR
5574 /* VEC_NUM is the number of vect stmts to be created for this
5575 group. */
5576 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
9771b263 5577 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
52eab378 5578 gcc_assert (GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_stmt)) == first_stmt);
4b5caab7 5579 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
d092494c 5580 op = gimple_assign_rhs1 (first_stmt);
4b5caab7 5581 }
ebfd146a 5582 else
4b5caab7
IR
5583 /* VEC_NUM is the number of vect stmts to be created for this
5584 group. */
ebfd146a
IR
5585 vec_num = group_size;
5586 }
b8698a0f 5587 else
ebfd146a
IR
5588 {
5589 first_stmt = stmt;
5590 first_dr = dr;
5591 group_size = vec_num = 1;
ebfd146a 5592 }
b8698a0f 5593
73fbfcad 5594 if (dump_enabled_p ())
78c60e3d 5595 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 5596 "transform store. ncopies = %d\n", ncopies);
ebfd146a 5597
f2e2a985
MM
5598 if (STMT_VINFO_STRIDED_P (stmt_info))
5599 {
5600 gimple_stmt_iterator incr_gsi;
5601 bool insert_after;
355fe088 5602 gimple *incr;
f2e2a985
MM
5603 tree offvar;
5604 tree ivstep;
5605 tree running_off;
5606 gimple_seq stmts = NULL;
5607 tree stride_base, stride_step, alias_off;
5608 tree vec_oprnd;
f502d50e 5609 unsigned int g;
f2e2a985
MM
5610
5611 gcc_assert (!nested_in_vect_loop_p (loop, stmt));
5612
5613 stride_base
5614 = fold_build_pointer_plus
f502d50e 5615 (unshare_expr (DR_BASE_ADDRESS (first_dr)),
f2e2a985 5616 size_binop (PLUS_EXPR,
f502d50e
MM
5617 convert_to_ptrofftype (unshare_expr (DR_OFFSET (first_dr))),
5618 convert_to_ptrofftype (DR_INIT(first_dr))));
5619 stride_step = fold_convert (sizetype, unshare_expr (DR_STEP (first_dr)));
f2e2a985
MM
5620
5621 /* For a store with loop-invariant (but other than power-of-2)
5622 stride (i.e. not a grouped access) like so:
5623
5624 for (i = 0; i < n; i += stride)
5625 array[i] = ...;
5626
5627 we generate a new induction variable and new stores from
5628 the components of the (vectorized) rhs:
5629
5630 for (j = 0; ; j += VF*stride)
5631 vectemp = ...;
5632 tmp1 = vectemp[0];
5633 array[j] = tmp1;
5634 tmp2 = vectemp[1];
5635 array[j + stride] = tmp2;
5636 ...
5637 */
5638
cee62fee
MM
5639 unsigned nstores = nunits;
5640 tree ltype = elem_type;
5641 if (slp)
5642 {
5643 nstores = nunits / group_size;
5644 if (group_size < nunits)
5645 ltype = build_vector_type (elem_type, group_size);
5646 else
5647 ltype = vectype;
5648 ltype = build_aligned_type (ltype, TYPE_ALIGN (elem_type));
5649 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
f502d50e 5650 group_size = 1;
cee62fee
MM
5651 }
5652
f2e2a985
MM
5653 ivstep = stride_step;
5654 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
5655 build_int_cst (TREE_TYPE (ivstep),
cee62fee 5656 ncopies * nstores));
f2e2a985
MM
5657
5658 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
5659
5660 create_iv (stride_base, ivstep, NULL,
5661 loop, &incr_gsi, insert_after,
5662 &offvar, NULL);
5663 incr = gsi_stmt (incr_gsi);
310213d4 5664 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
f2e2a985
MM
5665
5666 stride_step = force_gimple_operand (stride_step, &stmts, true, NULL_TREE);
5667 if (stmts)
5668 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
5669
5670 prev_stmt_info = NULL;
f502d50e
MM
5671 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (first_dr)), 0);
5672 next_stmt = first_stmt;
5673 for (g = 0; g < group_size; g++)
f2e2a985 5674 {
f502d50e
MM
5675 running_off = offvar;
5676 if (g)
f2e2a985 5677 {
f502d50e
MM
5678 tree size = TYPE_SIZE_UNIT (ltype);
5679 tree pos = fold_build2 (MULT_EXPR, sizetype, size_int (g),
f2e2a985 5680 size);
f502d50e 5681 tree newoff = copy_ssa_name (running_off, NULL);
f2e2a985 5682 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
f502d50e 5683 running_off, pos);
f2e2a985 5684 vect_finish_stmt_generation (stmt, incr, gsi);
f2e2a985 5685 running_off = newoff;
f502d50e
MM
5686 }
5687 for (j = 0; j < ncopies; j++)
5688 {
5689 /* We've set op and dt above, from gimple_assign_rhs1(stmt),
5690 and first_stmt == stmt. */
5691 if (j == 0)
5692 {
5693 if (slp)
5694 {
5695 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds, NULL,
5696 slp_node, -1);
5697 vec_oprnd = vec_oprnds[0];
5698 }
5699 else
5700 {
5701 gcc_assert (gimple_assign_single_p (next_stmt));
5702 op = gimple_assign_rhs1 (next_stmt);
81c40241 5703 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
f502d50e
MM
5704 }
5705 }
f2e2a985 5706 else
f502d50e
MM
5707 {
5708 if (slp)
5709 vec_oprnd = vec_oprnds[j];
5710 else
c079cbac 5711 {
81c40241 5712 vect_is_simple_use (vec_oprnd, vinfo, &def_stmt, &dt);
c079cbac
RB
5713 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, vec_oprnd);
5714 }
f502d50e
MM
5715 }
5716
5717 for (i = 0; i < nstores; i++)
5718 {
5719 tree newref, newoff;
355fe088 5720 gimple *incr, *assign;
f502d50e
MM
5721 tree size = TYPE_SIZE (ltype);
5722 /* Extract the i'th component. */
5723 tree pos = fold_build2 (MULT_EXPR, bitsizetype,
5724 bitsize_int (i), size);
5725 tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
5726 size, pos);
5727
5728 elem = force_gimple_operand_gsi (gsi, elem, true,
5729 NULL_TREE, true,
5730 GSI_SAME_STMT);
5731
5732 newref = build2 (MEM_REF, ltype,
5733 running_off, alias_off);
5734
5735 /* And store it to *running_off. */
5736 assign = gimple_build_assign (newref, elem);
5737 vect_finish_stmt_generation (stmt, assign, gsi);
5738
5739 newoff = copy_ssa_name (running_off, NULL);
5740 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
5741 running_off, stride_step);
5742 vect_finish_stmt_generation (stmt, incr, gsi);
5743
5744 running_off = newoff;
225ce44b
RB
5745 if (g == group_size - 1
5746 && !slp)
f502d50e
MM
5747 {
5748 if (j == 0 && i == 0)
225ce44b
RB
5749 STMT_VINFO_VEC_STMT (stmt_info)
5750 = *vec_stmt = assign;
f502d50e
MM
5751 else
5752 STMT_VINFO_RELATED_STMT (prev_stmt_info) = assign;
5753 prev_stmt_info = vinfo_for_stmt (assign);
5754 }
5755 }
f2e2a985 5756 }
f502d50e 5757 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
f2e2a985
MM
5758 }
5759 return true;
5760 }
5761
9771b263
DN
5762 dr_chain.create (group_size);
5763 oprnds.create (group_size);
ebfd146a 5764
720f5239 5765 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
ebfd146a 5766 gcc_assert (alignment_support_scheme);
272c6793
RS
5767 /* Targets with store-lane instructions must not require explicit
5768 realignment. */
5769 gcc_assert (!store_lanes_p
5770 || alignment_support_scheme == dr_aligned
5771 || alignment_support_scheme == dr_unaligned_supported);
5772
09dfa495
BM
5773 if (negative)
5774 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
5775
272c6793
RS
5776 if (store_lanes_p)
5777 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
5778 else
5779 aggr_type = vectype;
ebfd146a
IR
5780
5781 /* In case the vectorization factor (VF) is bigger than the number
5782 of elements that we can fit in a vectype (nunits), we have to generate
5783 more than one vector stmt - i.e - we need to "unroll" the
b8698a0f 5784 vector stmt by a factor VF/nunits. For more details see documentation in
ebfd146a
IR
5785 vect_get_vec_def_for_copy_stmt. */
5786
0d0293ac 5787 /* In case of interleaving (non-unit grouped access):
ebfd146a
IR
5788
5789 S1: &base + 2 = x2
5790 S2: &base = x0
5791 S3: &base + 1 = x1
5792 S4: &base + 3 = x3
5793
5794 We create vectorized stores starting from base address (the access of the
5795 first stmt in the chain (S2 in the above example), when the last store stmt
5796 of the chain (S4) is reached:
5797
5798 VS1: &base = vx2
5799 VS2: &base + vec_size*1 = vx0
5800 VS3: &base + vec_size*2 = vx1
5801 VS4: &base + vec_size*3 = vx3
5802
5803 Then permutation statements are generated:
5804
3fcc1b55
JJ
5805 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
5806 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
ebfd146a 5807 ...
b8698a0f 5808
ebfd146a
IR
5809 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
5810 (the order of the data-refs in the output of vect_permute_store_chain
5811 corresponds to the order of scalar stmts in the interleaving chain - see
5812 the documentation of vect_permute_store_chain()).
5813
5814 In case of both multiple types and interleaving, above vector stores and
ff802fa1 5815 permutation stmts are created for every copy. The result vector stmts are
ebfd146a 5816 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
b8698a0f 5817 STMT_VINFO_RELATED_STMT for the next copies.
ebfd146a
IR
5818 */
5819
5820 prev_stmt_info = NULL;
5821 for (j = 0; j < ncopies; j++)
5822 {
ebfd146a
IR
5823
5824 if (j == 0)
5825 {
5826 if (slp)
5827 {
5828 /* Get vectorized arguments for SLP_NODE. */
d092494c
IR
5829 vect_get_vec_defs (op, NULL_TREE, stmt, &vec_oprnds,
5830 NULL, slp_node, -1);
ebfd146a 5831
9771b263 5832 vec_oprnd = vec_oprnds[0];
ebfd146a
IR
5833 }
5834 else
5835 {
b8698a0f
L
5836 /* For interleaved stores we collect vectorized defs for all the
5837 stores in the group in DR_CHAIN and OPRNDS. DR_CHAIN is then
5838 used as an input to vect_permute_store_chain(), and OPRNDS as
ebfd146a
IR
5839 an input to vect_get_vec_def_for_stmt_copy() for the next copy.
5840
0d0293ac 5841 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
ebfd146a 5842 OPRNDS are of size 1. */
b8698a0f 5843 next_stmt = first_stmt;
ebfd146a
IR
5844 for (i = 0; i < group_size; i++)
5845 {
b8698a0f
L
5846 /* Since gaps are not supported for interleaved stores,
5847 GROUP_SIZE is the exact number of stmts in the chain.
5848 Therefore, NEXT_STMT can't be NULL_TREE. In case that
5849 there is no interleaving, GROUP_SIZE is 1, and only one
ebfd146a
IR
5850 iteration of the loop will be executed. */
5851 gcc_assert (next_stmt
5852 && gimple_assign_single_p (next_stmt));
5853 op = gimple_assign_rhs1 (next_stmt);
5854
81c40241 5855 vec_oprnd = vect_get_vec_def_for_operand (op, next_stmt);
9771b263
DN
5856 dr_chain.quick_push (vec_oprnd);
5857 oprnds.quick_push (vec_oprnd);
e14c1050 5858 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
ebfd146a
IR
5859 }
5860 }
5861
5862 /* We should have catched mismatched types earlier. */
5863 gcc_assert (useless_type_conversion_p (vectype,
5864 TREE_TYPE (vec_oprnd)));
74bf76ed
JJ
5865 bool simd_lane_access_p
5866 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
5867 if (simd_lane_access_p
5868 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
5869 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
5870 && integer_zerop (DR_OFFSET (first_dr))
5871 && integer_zerop (DR_INIT (first_dr))
5872 && alias_sets_conflict_p (get_alias_set (aggr_type),
5873 get_alias_set (DR_REF (first_dr))))
5874 {
5875 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
5876 dataref_offset = build_int_cst (reference_alias_ptr_type
5877 (DR_REF (first_dr)), 0);
8928eff3 5878 inv_p = false;
74bf76ed
JJ
5879 }
5880 else
5881 dataref_ptr
5882 = vect_create_data_ref_ptr (first_stmt, aggr_type,
5883 simd_lane_access_p ? loop : NULL,
09dfa495 5884 offset, &dummy, gsi, &ptr_incr,
74bf76ed 5885 simd_lane_access_p, &inv_p);
a70d6342 5886 gcc_assert (bb_vinfo || !inv_p);
ebfd146a 5887 }
b8698a0f 5888 else
ebfd146a 5889 {
b8698a0f
L
5890 /* For interleaved stores we created vectorized defs for all the
5891 defs stored in OPRNDS in the previous iteration (previous copy).
5892 DR_CHAIN is then used as an input to vect_permute_store_chain(),
ebfd146a
IR
5893 and OPRNDS as an input to vect_get_vec_def_for_stmt_copy() for the
5894 next copy.
0d0293ac 5895 If the store is not grouped, GROUP_SIZE is 1, and DR_CHAIN and
ebfd146a
IR
5896 OPRNDS are of size 1. */
5897 for (i = 0; i < group_size; i++)
5898 {
9771b263 5899 op = oprnds[i];
81c40241 5900 vect_is_simple_use (op, vinfo, &def_stmt, &dt);
b8698a0f 5901 vec_oprnd = vect_get_vec_def_for_stmt_copy (dt, op);
9771b263
DN
5902 dr_chain[i] = vec_oprnd;
5903 oprnds[i] = vec_oprnd;
ebfd146a 5904 }
74bf76ed
JJ
5905 if (dataref_offset)
5906 dataref_offset
5907 = int_const_binop (PLUS_EXPR, dataref_offset,
5908 TYPE_SIZE_UNIT (aggr_type));
5909 else
5910 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
5911 TYPE_SIZE_UNIT (aggr_type));
ebfd146a
IR
5912 }
5913
272c6793 5914 if (store_lanes_p)
ebfd146a 5915 {
272c6793 5916 tree vec_array;
267d3070 5917
272c6793
RS
5918 /* Combine all the vectors into an array. */
5919 vec_array = create_vector_array (vectype, vec_num);
5920 for (i = 0; i < vec_num; i++)
c2d7ab2a 5921 {
9771b263 5922 vec_oprnd = dr_chain[i];
272c6793 5923 write_vector_array (stmt, gsi, vec_oprnd, vec_array, i);
267d3070 5924 }
b8698a0f 5925
272c6793
RS
5926 /* Emit:
5927 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
5928 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
5929 new_stmt = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
5930 gimple_call_set_lhs (new_stmt, data_ref);
267d3070 5931 vect_finish_stmt_generation (stmt, new_stmt, gsi);
272c6793
RS
5932 }
5933 else
5934 {
5935 new_stmt = NULL;
0d0293ac 5936 if (grouped_store)
272c6793 5937 {
b6b9227d
JJ
5938 if (j == 0)
5939 result_chain.create (group_size);
272c6793
RS
5940 /* Permute. */
5941 vect_permute_store_chain (dr_chain, group_size, stmt, gsi,
5942 &result_chain);
5943 }
c2d7ab2a 5944
272c6793
RS
5945 next_stmt = first_stmt;
5946 for (i = 0; i < vec_num; i++)
5947 {
644ffefd 5948 unsigned align, misalign;
272c6793
RS
5949
5950 if (i > 0)
5951 /* Bump the vector pointer. */
5952 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
5953 stmt, NULL_TREE);
5954
5955 if (slp)
9771b263 5956 vec_oprnd = vec_oprnds[i];
0d0293ac
MM
5957 else if (grouped_store)
5958 /* For grouped stores vectorized defs are interleaved in
272c6793 5959 vect_permute_store_chain(). */
9771b263 5960 vec_oprnd = result_chain[i];
272c6793 5961
aed93b23
RB
5962 data_ref = fold_build2 (MEM_REF, TREE_TYPE (vec_oprnd),
5963 dataref_ptr,
5964 dataref_offset
5965 ? dataref_offset
5966 : build_int_cst (reference_alias_ptr_type
5967 (DR_REF (first_dr)), 0));
644ffefd 5968 align = TYPE_ALIGN_UNIT (vectype);
272c6793 5969 if (aligned_access_p (first_dr))
644ffefd 5970 misalign = 0;
272c6793
RS
5971 else if (DR_MISALIGNMENT (first_dr) == -1)
5972 {
52639a61
RB
5973 if (DR_VECT_AUX (first_dr)->base_element_aligned)
5974 align = TYPE_ALIGN_UNIT (elem_type);
5975 else
5976 align = get_object_alignment (DR_REF (first_dr))
5977 / BITS_PER_UNIT;
5978 misalign = 0;
272c6793
RS
5979 TREE_TYPE (data_ref)
5980 = build_aligned_type (TREE_TYPE (data_ref),
52639a61 5981 align * BITS_PER_UNIT);
272c6793
RS
5982 }
5983 else
5984 {
5985 TREE_TYPE (data_ref)
5986 = build_aligned_type (TREE_TYPE (data_ref),
5987 TYPE_ALIGN (elem_type));
644ffefd 5988 misalign = DR_MISALIGNMENT (first_dr);
272c6793 5989 }
aed93b23
RB
5990 if (dataref_offset == NULL_TREE
5991 && TREE_CODE (dataref_ptr) == SSA_NAME)
74bf76ed
JJ
5992 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
5993 misalign);
c2d7ab2a 5994
f234d260
BM
5995 if (negative
5996 && dt != vect_constant_def
5997 && dt != vect_external_def)
09dfa495
BM
5998 {
5999 tree perm_mask = perm_mask_for_reverse (vectype);
6000 tree perm_dest
6001 = vect_create_destination_var (gimple_assign_rhs1 (stmt),
6002 vectype);
b731b390 6003 tree new_temp = make_ssa_name (perm_dest);
09dfa495
BM
6004
6005 /* Generate the permute statement. */
355fe088 6006 gimple *perm_stmt
0d0e4a03
JJ
6007 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
6008 vec_oprnd, perm_mask);
09dfa495
BM
6009 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
6010
6011 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
6012 vec_oprnd = new_temp;
6013 }
6014
272c6793
RS
6015 /* Arguments are ready. Create the new vector stmt. */
6016 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
6017 vect_finish_stmt_generation (stmt, new_stmt, gsi);
272c6793
RS
6018
6019 if (slp)
6020 continue;
6021
e14c1050 6022 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
272c6793
RS
6023 if (!next_stmt)
6024 break;
6025 }
ebfd146a 6026 }
1da0876c
RS
6027 if (!slp)
6028 {
6029 if (j == 0)
6030 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6031 else
6032 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6033 prev_stmt_info = vinfo_for_stmt (new_stmt);
6034 }
ebfd146a
IR
6035 }
6036
9771b263
DN
6037 dr_chain.release ();
6038 oprnds.release ();
6039 result_chain.release ();
6040 vec_oprnds.release ();
ebfd146a
IR
6041
6042 return true;
6043}
6044
557be5a8
AL
6045/* Given a vector type VECTYPE, turns permutation SEL into the equivalent
6046 VECTOR_CST mask. No checks are made that the target platform supports the
6047 mask, so callers may wish to test can_vec_perm_p separately, or use
6048 vect_gen_perm_mask_checked. */
a1e53f3f 6049
3fcc1b55 6050tree
557be5a8 6051vect_gen_perm_mask_any (tree vectype, const unsigned char *sel)
a1e53f3f 6052{
d2a12ae7 6053 tree mask_elt_type, mask_type, mask_vec, *mask_elts;
2635892a 6054 int i, nunits;
a1e53f3f 6055
22e4dee7 6056 nunits = TYPE_VECTOR_SUBPARTS (vectype);
22e4dee7 6057
96f9265a
RG
6058 mask_elt_type = lang_hooks.types.type_for_mode
6059 (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
22e4dee7 6060 mask_type = get_vectype_for_scalar_type (mask_elt_type);
a1e53f3f 6061
d2a12ae7 6062 mask_elts = XALLOCAVEC (tree, nunits);
aec7ae7d 6063 for (i = nunits - 1; i >= 0; i--)
d2a12ae7
RG
6064 mask_elts[i] = build_int_cst (mask_elt_type, sel[i]);
6065 mask_vec = build_vector (mask_type, mask_elts);
a1e53f3f 6066
2635892a 6067 return mask_vec;
a1e53f3f
L
6068}
6069
cf7aa6a3
AL
6070/* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_p,
6071 i.e. that the target supports the pattern _for arbitrary input vectors_. */
557be5a8
AL
6072
6073tree
6074vect_gen_perm_mask_checked (tree vectype, const unsigned char *sel)
6075{
6076 gcc_assert (can_vec_perm_p (TYPE_MODE (vectype), false, sel));
6077 return vect_gen_perm_mask_any (vectype, sel);
6078}
6079
aec7ae7d
JJ
6080/* Given a vector variable X and Y, that was generated for the scalar
6081 STMT, generate instructions to permute the vector elements of X and Y
6082 using permutation mask MASK_VEC, insert them at *GSI and return the
6083 permuted vector variable. */
a1e53f3f
L
6084
6085static tree
355fe088 6086permute_vec_elements (tree x, tree y, tree mask_vec, gimple *stmt,
aec7ae7d 6087 gimple_stmt_iterator *gsi)
a1e53f3f
L
6088{
6089 tree vectype = TREE_TYPE (x);
aec7ae7d 6090 tree perm_dest, data_ref;
355fe088 6091 gimple *perm_stmt;
a1e53f3f 6092
acdcd61b 6093 perm_dest = vect_create_destination_var (gimple_get_lhs (stmt), vectype);
b731b390 6094 data_ref = make_ssa_name (perm_dest);
a1e53f3f
L
6095
6096 /* Generate the permute statement. */
0d0e4a03 6097 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
a1e53f3f
L
6098 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
6099
6100 return data_ref;
6101}
6102
6b916b36
RB
6103/* Hoist the definitions of all SSA uses on STMT out of the loop LOOP,
6104 inserting them on the loops preheader edge. Returns true if we
6105 were successful in doing so (and thus STMT can be moved then),
6106 otherwise returns false. */
6107
6108static bool
355fe088 6109hoist_defs_of_uses (gimple *stmt, struct loop *loop)
6b916b36
RB
6110{
6111 ssa_op_iter i;
6112 tree op;
6113 bool any = false;
6114
6115 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6116 {
355fe088 6117 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6b916b36
RB
6118 if (!gimple_nop_p (def_stmt)
6119 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6120 {
6121 /* Make sure we don't need to recurse. While we could do
6122 so in simple cases when there are more complex use webs
6123 we don't have an easy way to preserve stmt order to fulfil
6124 dependencies within them. */
6125 tree op2;
6126 ssa_op_iter i2;
d1417442
JJ
6127 if (gimple_code (def_stmt) == GIMPLE_PHI)
6128 return false;
6b916b36
RB
6129 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
6130 {
355fe088 6131 gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
6b916b36
RB
6132 if (!gimple_nop_p (def_stmt2)
6133 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
6134 return false;
6135 }
6136 any = true;
6137 }
6138 }
6139
6140 if (!any)
6141 return true;
6142
6143 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6144 {
355fe088 6145 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
6b916b36
RB
6146 if (!gimple_nop_p (def_stmt)
6147 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
6148 {
6149 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
6150 gsi_remove (&gsi, false);
6151 gsi_insert_on_edge_immediate (loop_preheader_edge (loop), def_stmt);
6152 }
6153 }
6154
6155 return true;
6156}
6157
ebfd146a
IR
6158/* vectorizable_load.
6159
b8698a0f
L
6160 Check if STMT reads a non scalar data-ref (array/pointer/structure) that
6161 can be vectorized.
6162 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
ebfd146a
IR
6163 stmt to replace it, put it in VEC_STMT, and insert it at BSI.
6164 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6165
6166static bool
355fe088 6167vectorizable_load (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
c716e67f 6168 slp_tree slp_node, slp_instance slp_node_instance)
ebfd146a
IR
6169{
6170 tree scalar_dest;
6171 tree vec_dest = NULL;
6172 tree data_ref = NULL;
6173 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
b8698a0f 6174 stmt_vec_info prev_stmt_info;
ebfd146a 6175 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
a70d6342 6176 struct loop *loop = NULL;
ebfd146a 6177 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
a70d6342 6178 bool nested_in_vect_loop = false;
c716e67f 6179 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info), *first_dr = NULL;
272c6793 6180 tree elem_type;
ebfd146a 6181 tree new_temp;
ef4bddc2 6182 machine_mode mode;
355fe088 6183 gimple *new_stmt = NULL;
ebfd146a
IR
6184 tree dummy;
6185 enum dr_alignment_support alignment_support_scheme;
6186 tree dataref_ptr = NULL_TREE;
74bf76ed 6187 tree dataref_offset = NULL_TREE;
355fe088 6188 gimple *ptr_incr = NULL;
ebfd146a 6189 int ncopies;
9b999e8c 6190 int i, j, group_size = -1, group_gap_adj;
ebfd146a
IR
6191 tree msq = NULL_TREE, lsq;
6192 tree offset = NULL_TREE;
356bbc4c 6193 tree byte_offset = NULL_TREE;
ebfd146a 6194 tree realignment_token = NULL_TREE;
538dd0b7 6195 gphi *phi = NULL;
6e1aa848 6196 vec<tree> dr_chain = vNULL;
0d0293ac 6197 bool grouped_load = false;
272c6793 6198 bool load_lanes_p = false;
355fe088 6199 gimple *first_stmt;
4f0a0218 6200 gimple *first_stmt_for_drptr = NULL;
ebfd146a 6201 bool inv_p;
319e6439 6202 bool negative = false;
ebfd146a
IR
6203 bool compute_in_loop = false;
6204 struct loop *at_loop;
6205 int vec_num;
6206 bool slp = (slp_node != NULL);
6207 bool slp_perm = false;
6208 enum tree_code code;
a70d6342
IR
6209 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6210 int vf;
272c6793 6211 tree aggr_type;
aec7ae7d
JJ
6212 tree gather_base = NULL_TREE, gather_off = NULL_TREE;
6213 tree gather_off_vectype = NULL_TREE, gather_decl = NULL_TREE;
6214 int gather_scale = 1;
6215 enum vect_def_type gather_dt = vect_unknown_def_type;
310213d4 6216 vec_info *vinfo = stmt_info->vinfo;
a70d6342 6217
465c8c19
JJ
6218 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6219 return false;
6220
66c16fd9
RB
6221 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6222 && ! vec_stmt)
465c8c19
JJ
6223 return false;
6224
6225 /* Is vectorizable load? */
6226 if (!is_gimple_assign (stmt))
6227 return false;
6228
6229 scalar_dest = gimple_assign_lhs (stmt);
6230 if (TREE_CODE (scalar_dest) != SSA_NAME)
6231 return false;
6232
6233 code = gimple_assign_rhs_code (stmt);
6234 if (code != ARRAY_REF
6235 && code != BIT_FIELD_REF
6236 && code != INDIRECT_REF
6237 && code != COMPONENT_REF
6238 && code != IMAGPART_EXPR
6239 && code != REALPART_EXPR
6240 && code != MEM_REF
6241 && TREE_CODE_CLASS (code) != tcc_declaration)
6242 return false;
6243
6244 if (!STMT_VINFO_DATA_REF (stmt_info))
6245 return false;
6246
6247 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6248 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6249
a70d6342
IR
6250 if (loop_vinfo)
6251 {
6252 loop = LOOP_VINFO_LOOP (loop_vinfo);
6253 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
6254 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6255 }
6256 else
3533e503 6257 vf = 1;
ebfd146a
IR
6258
6259 /* Multiple types in SLP are handled by creating the appropriate number of
ff802fa1 6260 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
ebfd146a 6261 case of SLP. */
fce57248 6262 if (slp)
ebfd146a
IR
6263 ncopies = 1;
6264 else
6265 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6266
6267 gcc_assert (ncopies >= 1);
6268
6269 /* FORNOW. This restriction should be relaxed. */
6270 if (nested_in_vect_loop && ncopies > 1)
6271 {
73fbfcad 6272 if (dump_enabled_p ())
78c60e3d 6273 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 6274 "multiple types in nested loop.\n");
ebfd146a
IR
6275 return false;
6276 }
6277
f2556b68
RB
6278 /* Invalidate assumptions made by dependence analysis when vectorization
6279 on the unrolled body effectively re-orders stmts. */
6280 if (ncopies > 1
6281 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6282 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6283 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6284 {
6285 if (dump_enabled_p ())
6286 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6287 "cannot perform implicit CSE when unrolling "
6288 "with negative dependence distance\n");
6289 return false;
6290 }
6291
7b7b1813 6292 elem_type = TREE_TYPE (vectype);
947131ba 6293 mode = TYPE_MODE (vectype);
ebfd146a
IR
6294
6295 /* FORNOW. In some cases can vectorize even if data-type not supported
6296 (e.g. - data copies). */
947131ba 6297 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
ebfd146a 6298 {
73fbfcad 6299 if (dump_enabled_p ())
78c60e3d 6300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 6301 "Aligned load, but unsupported type.\n");
ebfd146a
IR
6302 return false;
6303 }
6304
ebfd146a 6305 /* Check if the load is a part of an interleaving chain. */
0d0293ac 6306 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
ebfd146a 6307 {
0d0293ac 6308 grouped_load = true;
ebfd146a 6309 /* FORNOW */
3bab6342 6310 gcc_assert (!nested_in_vect_loop && !STMT_VINFO_GATHER_SCATTER_P (stmt_info));
ebfd146a 6311
e14c1050 6312 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
d3465d72
RS
6313 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
6314
fce57248 6315 if (!slp && !STMT_VINFO_STRIDED_P (stmt_info))
d3465d72
RS
6316 {
6317 if (vect_load_lanes_supported (vectype, group_size))
6318 load_lanes_p = true;
6319 else if (!vect_grouped_load_supported (vectype, group_size))
6320 return false;
6321 }
d5f035ea
RB
6322
6323 /* If this is single-element interleaving with an element distance
6324 that leaves unused vector loads around punt - we at least create
6325 very sub-optimal code in that case (and blow up memory,
6326 see PR65518). */
6327 if (first_stmt == stmt
72c0f643
RB
6328 && !GROUP_NEXT_ELEMENT (stmt_info))
6329 {
6330 if (GROUP_SIZE (stmt_info) > TYPE_VECTOR_SUBPARTS (vectype))
6331 {
6332 if (dump_enabled_p ())
6333 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6334 "single-element interleaving not supported "
6335 "for not adjacent vector loads\n");
6336 return false;
6337 }
6338
6339 /* Single-element interleaving requires peeling for gaps. */
836dbb1a 6340 gcc_assert (GROUP_GAP (stmt_info));
72c0f643
RB
6341 }
6342
6343 /* If there is a gap in the end of the group or the group size cannot
6344 be made a multiple of the vector element count then we access excess
6345 elements in the last iteration and thus need to peel that off. */
6346 if (loop_vinfo
6347 && ! STMT_VINFO_STRIDED_P (stmt_info)
836dbb1a 6348 && (GROUP_GAP (vinfo_for_stmt (first_stmt)) != 0
d3465d72 6349 || (!slp && !load_lanes_p && vf % group_size != 0)))
d5f035ea
RB
6350 {
6351 if (dump_enabled_p ())
6352 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
72c0f643
RB
6353 "Data access with gaps requires scalar "
6354 "epilogue loop\n");
6355 if (loop->inner)
6356 {
6357 if (dump_enabled_p ())
6358 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6359 "Peeling for outer loop is not supported\n");
6360 return false;
6361 }
6362
6363 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
d5f035ea
RB
6364 }
6365
b1af7da6
RB
6366 if (slp && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
6367 slp_perm = true;
6368
47d3fdb2
RB
6369 /* ??? The following is overly pessimistic (as well as the loop
6370 case above) in the case we can statically determine the excess
6371 elements loaded are within the bounds of a decl that is accessed.
6372 Likewise for BB vectorizations using masked loads is a possibility. */
6373 if (bb_vinfo && slp_perm && group_size % nunits != 0)
6374 {
6375 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6376 "BB vectorization with gaps at the end of a load "
6377 "is not supported\n");
6378 return false;
6379 }
6380
f2556b68
RB
6381 /* Invalidate assumptions made by dependence analysis when vectorization
6382 on the unrolled body effectively re-orders stmts. */
6383 if (!PURE_SLP_STMT (stmt_info)
6384 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
6385 && ((unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo)
6386 > STMT_VINFO_MIN_NEG_DIST (stmt_info)))
6387 {
6388 if (dump_enabled_p ())
6389 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6390 "cannot perform implicit CSE when performing "
6391 "group loads with negative dependence distance\n");
6392 return false;
6393 }
96bb56b2
RB
6394
6395 /* Similarly when the stmt is a load that is both part of a SLP
6396 instance and a loop vectorized stmt via the same-dr mechanism
6397 we have to give up. */
6398 if (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)
6399 && (STMT_SLP_TYPE (stmt_info)
6400 != STMT_SLP_TYPE (vinfo_for_stmt
6401 (STMT_VINFO_GROUP_SAME_DR_STMT (stmt_info)))))
6402 {
6403 if (dump_enabled_p ())
6404 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6405 "conflicting SLP types for CSEd load\n");
6406 return false;
6407 }
ebfd146a
IR
6408 }
6409
a1e53f3f 6410
3bab6342 6411 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
aec7ae7d 6412 {
355fe088 6413 gimple *def_stmt;
3bab6342
AT
6414 gather_decl = vect_check_gather_scatter (stmt, loop_vinfo, &gather_base,
6415 &gather_off, &gather_scale);
aec7ae7d 6416 gcc_assert (gather_decl);
81c40241
RB
6417 if (!vect_is_simple_use (gather_off, vinfo, &def_stmt, &gather_dt,
6418 &gather_off_vectype))
aec7ae7d 6419 {
73fbfcad 6420 if (dump_enabled_p ())
78c60e3d 6421 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 6422 "gather index use not simple.\n");
aec7ae7d
JJ
6423 return false;
6424 }
6425 }
f2e2a985 6426 else if (STMT_VINFO_STRIDED_P (stmt_info))
7b5fc413 6427 {
fce57248
RS
6428 if (grouped_load
6429 && slp
7b5fc413 6430 && (group_size > nunits
b266b968 6431 || nunits % group_size != 0))
7b5fc413
RB
6432 {
6433 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6434 "unhandled strided group load\n");
6435 return false;
6436 }
6437 }
319e6439
RG
6438 else
6439 {
6440 negative = tree_int_cst_compare (nested_in_vect_loop
6441 ? STMT_VINFO_DR_STEP (stmt_info)
6442 : DR_STEP (dr),
6443 size_zero_node) < 0;
6444 if (negative && ncopies > 1)
6445 {
73fbfcad 6446 if (dump_enabled_p ())
78c60e3d 6447 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 6448 "multiple types with negative step.\n");
319e6439
RG
6449 return false;
6450 }
6451
6452 if (negative)
6453 {
08940f33
RB
6454 if (grouped_load)
6455 {
6456 if (dump_enabled_p ())
6457 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942
TJ
6458 "negative step for group load not supported"
6459 "\n");
08940f33
RB
6460 return false;
6461 }
319e6439
RG
6462 alignment_support_scheme = vect_supportable_dr_alignment (dr, false);
6463 if (alignment_support_scheme != dr_aligned
6464 && alignment_support_scheme != dr_unaligned_supported)
6465 {
73fbfcad 6466 if (dump_enabled_p ())
78c60e3d 6467 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 6468 "negative step but alignment required.\n");
319e6439
RG
6469 return false;
6470 }
6471 if (!perm_mask_for_reverse (vectype))
6472 {
73fbfcad 6473 if (dump_enabled_p ())
78c60e3d 6474 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942
TJ
6475 "negative step and reversing not supported."
6476 "\n");
319e6439
RG
6477 return false;
6478 }
6479 }
7d75abc8 6480 }
aec7ae7d 6481
ebfd146a
IR
6482 if (!vec_stmt) /* transformation not required. */
6483 {
6484 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
2e8ab70c
RB
6485 /* The SLP costs are calculated during SLP analysis. */
6486 if (!PURE_SLP_STMT (stmt_info))
6487 vect_model_load_cost (stmt_info, ncopies, load_lanes_p,
6488 NULL, NULL, NULL);
ebfd146a
IR
6489 return true;
6490 }
6491
73fbfcad 6492 if (dump_enabled_p ())
78c60e3d 6493 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 6494 "transform load. ncopies = %d\n", ncopies);
ebfd146a
IR
6495
6496 /** Transform. **/
6497
c716e67f
XDL
6498 ensure_base_align (stmt_info, dr);
6499
3bab6342 6500 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
aec7ae7d
JJ
6501 {
6502 tree vec_oprnd0 = NULL_TREE, op;
6503 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gather_decl));
6504 tree rettype, srctype, ptrtype, idxtype, masktype, scaletype;
d3c2fee0 6505 tree ptr, mask, var, scale, merge, perm_mask = NULL_TREE, prev_res = NULL_TREE;
aec7ae7d
JJ
6506 edge pe = loop_preheader_edge (loop);
6507 gimple_seq seq;
6508 basic_block new_bb;
6509 enum { NARROW, NONE, WIDEN } modifier;
6510 int gather_off_nunits = TYPE_VECTOR_SUBPARTS (gather_off_vectype);
6511
6512 if (nunits == gather_off_nunits)
6513 modifier = NONE;
6514 else if (nunits == gather_off_nunits / 2)
6515 {
6516 unsigned char *sel = XALLOCAVEC (unsigned char, gather_off_nunits);
6517 modifier = WIDEN;
6518
6519 for (i = 0; i < gather_off_nunits; ++i)
6520 sel[i] = i | nunits;
6521
557be5a8 6522 perm_mask = vect_gen_perm_mask_checked (gather_off_vectype, sel);
aec7ae7d
JJ
6523 }
6524 else if (nunits == gather_off_nunits * 2)
6525 {
6526 unsigned char *sel = XALLOCAVEC (unsigned char, nunits);
6527 modifier = NARROW;
6528
6529 for (i = 0; i < nunits; ++i)
6530 sel[i] = i < gather_off_nunits
6531 ? i : i + nunits - gather_off_nunits;
6532
557be5a8 6533 perm_mask = vect_gen_perm_mask_checked (vectype, sel);
aec7ae7d
JJ
6534 ncopies *= 2;
6535 }
6536 else
6537 gcc_unreachable ();
6538
6539 rettype = TREE_TYPE (TREE_TYPE (gather_decl));
6540 srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6541 ptrtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6542 idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6543 masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
6544 scaletype = TREE_VALUE (arglist);
d3c2fee0 6545 gcc_checking_assert (types_compatible_p (srctype, rettype));
aec7ae7d
JJ
6546
6547 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6548
6549 ptr = fold_convert (ptrtype, gather_base);
6550 if (!is_gimple_min_invariant (ptr))
6551 {
6552 ptr = force_gimple_operand (ptr, &seq, true, NULL_TREE);
6553 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
6554 gcc_assert (!new_bb);
6555 }
6556
6557 /* Currently we support only unconditional gather loads,
6558 so mask should be all ones. */
d3c2fee0
AI
6559 if (TREE_CODE (masktype) == INTEGER_TYPE)
6560 mask = build_int_cst (masktype, -1);
6561 else if (TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
6562 {
6563 mask = build_int_cst (TREE_TYPE (masktype), -1);
6564 mask = build_vector_from_val (masktype, mask);
03b9e8e4 6565 mask = vect_init_vector (stmt, mask, masktype, NULL);
d3c2fee0 6566 }
aec7ae7d
JJ
6567 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
6568 {
6569 REAL_VALUE_TYPE r;
6570 long tmp[6];
6571 for (j = 0; j < 6; ++j)
6572 tmp[j] = -1;
6573 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
6574 mask = build_real (TREE_TYPE (masktype), r);
d3c2fee0 6575 mask = build_vector_from_val (masktype, mask);
03b9e8e4 6576 mask = vect_init_vector (stmt, mask, masktype, NULL);
aec7ae7d
JJ
6577 }
6578 else
6579 gcc_unreachable ();
aec7ae7d
JJ
6580
6581 scale = build_int_cst (scaletype, gather_scale);
6582
d3c2fee0
AI
6583 if (TREE_CODE (TREE_TYPE (rettype)) == INTEGER_TYPE)
6584 merge = build_int_cst (TREE_TYPE (rettype), 0);
6585 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rettype)))
6586 {
6587 REAL_VALUE_TYPE r;
6588 long tmp[6];
6589 for (j = 0; j < 6; ++j)
6590 tmp[j] = 0;
6591 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (rettype)));
6592 merge = build_real (TREE_TYPE (rettype), r);
6593 }
6594 else
6595 gcc_unreachable ();
6596 merge = build_vector_from_val (rettype, merge);
6597 merge = vect_init_vector (stmt, merge, rettype, NULL);
6598
aec7ae7d
JJ
6599 prev_stmt_info = NULL;
6600 for (j = 0; j < ncopies; ++j)
6601 {
6602 if (modifier == WIDEN && (j & 1))
6603 op = permute_vec_elements (vec_oprnd0, vec_oprnd0,
6604 perm_mask, stmt, gsi);
6605 else if (j == 0)
6606 op = vec_oprnd0
81c40241 6607 = vect_get_vec_def_for_operand (gather_off, stmt);
aec7ae7d
JJ
6608 else
6609 op = vec_oprnd0
6610 = vect_get_vec_def_for_stmt_copy (gather_dt, vec_oprnd0);
6611
6612 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
6613 {
6614 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op))
6615 == TYPE_VECTOR_SUBPARTS (idxtype));
0e22bb5a 6616 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
aec7ae7d
JJ
6617 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
6618 new_stmt
0d0e4a03 6619 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
aec7ae7d
JJ
6620 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6621 op = var;
6622 }
6623
6624 new_stmt
d3c2fee0 6625 = gimple_build_call (gather_decl, 5, merge, ptr, op, mask, scale);
aec7ae7d
JJ
6626
6627 if (!useless_type_conversion_p (vectype, rettype))
6628 {
6629 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype)
6630 == TYPE_VECTOR_SUBPARTS (rettype));
0e22bb5a 6631 op = vect_get_new_ssa_name (rettype, vect_simple_var);
aec7ae7d
JJ
6632 gimple_call_set_lhs (new_stmt, op);
6633 vect_finish_stmt_generation (stmt, new_stmt, gsi);
b731b390 6634 var = make_ssa_name (vec_dest);
aec7ae7d
JJ
6635 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
6636 new_stmt
0d0e4a03 6637 = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
aec7ae7d
JJ
6638 }
6639 else
6640 {
6641 var = make_ssa_name (vec_dest, new_stmt);
6642 gimple_call_set_lhs (new_stmt, var);
6643 }
6644
6645 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6646
6647 if (modifier == NARROW)
6648 {
6649 if ((j & 1) == 0)
6650 {
6651 prev_res = var;
6652 continue;
6653 }
6654 var = permute_vec_elements (prev_res, var,
6655 perm_mask, stmt, gsi);
6656 new_stmt = SSA_NAME_DEF_STMT (var);
6657 }
6658
6659 if (prev_stmt_info == NULL)
6660 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6661 else
6662 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6663 prev_stmt_info = vinfo_for_stmt (new_stmt);
6664 }
6665 return true;
6666 }
f2e2a985 6667 else if (STMT_VINFO_STRIDED_P (stmt_info))
7d75abc8
MM
6668 {
6669 gimple_stmt_iterator incr_gsi;
6670 bool insert_after;
355fe088 6671 gimple *incr;
7d75abc8 6672 tree offvar;
7d75abc8
MM
6673 tree ivstep;
6674 tree running_off;
9771b263 6675 vec<constructor_elt, va_gc> *v = NULL;
7d75abc8 6676 gimple_seq stmts = NULL;
14ac6aa2
RB
6677 tree stride_base, stride_step, alias_off;
6678
6679 gcc_assert (!nested_in_vect_loop);
7d75abc8 6680
f502d50e 6681 if (slp && grouped_load)
ab313a8c
RB
6682 first_dr = STMT_VINFO_DATA_REF
6683 (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
6684 else
6685 first_dr = dr;
6686
14ac6aa2
RB
6687 stride_base
6688 = fold_build_pointer_plus
ab313a8c 6689 (DR_BASE_ADDRESS (first_dr),
14ac6aa2 6690 size_binop (PLUS_EXPR,
ab313a8c
RB
6691 convert_to_ptrofftype (DR_OFFSET (first_dr)),
6692 convert_to_ptrofftype (DR_INIT (first_dr))));
6693 stride_step = fold_convert (sizetype, DR_STEP (first_dr));
7d75abc8
MM
6694
6695 /* For a load with loop-invariant (but other than power-of-2)
6696 stride (i.e. not a grouped access) like so:
6697
6698 for (i = 0; i < n; i += stride)
6699 ... = array[i];
6700
6701 we generate a new induction variable and new accesses to
6702 form a new vector (or vectors, depending on ncopies):
6703
6704 for (j = 0; ; j += VF*stride)
6705 tmp1 = array[j];
6706 tmp2 = array[j + stride];
6707 ...
6708 vectemp = {tmp1, tmp2, ...}
6709 */
6710
ab313a8c
RB
6711 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
6712 build_int_cst (TREE_TYPE (stride_step), vf));
7d75abc8
MM
6713
6714 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
6715
ab313a8c 6716 create_iv (unshare_expr (stride_base), unshare_expr (ivstep), NULL,
7d75abc8
MM
6717 loop, &incr_gsi, insert_after,
6718 &offvar, NULL);
6719 incr = gsi_stmt (incr_gsi);
310213d4 6720 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo));
7d75abc8 6721
ab313a8c
RB
6722 stride_step = force_gimple_operand (unshare_expr (stride_step),
6723 &stmts, true, NULL_TREE);
7d75abc8
MM
6724 if (stmts)
6725 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
6726
6727 prev_stmt_info = NULL;
6728 running_off = offvar;
ab313a8c 6729 alias_off = build_int_cst (reference_alias_ptr_type (DR_REF (first_dr)), 0);
7b5fc413
RB
6730 int nloads = nunits;
6731 tree ltype = TREE_TYPE (vectype);
b266b968 6732 auto_vec<tree> dr_chain;
7b5fc413
RB
6733 if (slp)
6734 {
6735 nloads = nunits / group_size;
6736 if (group_size < nunits)
6737 ltype = build_vector_type (TREE_TYPE (vectype), group_size);
6738 else
6739 ltype = vectype;
6740 ltype = build_aligned_type (ltype, TYPE_ALIGN (TREE_TYPE (vectype)));
66c16fd9
RB
6741 /* For SLP permutation support we need to load the whole group,
6742 not only the number of vector stmts the permutation result
6743 fits in. */
b266b968 6744 if (slp_perm)
66c16fd9
RB
6745 {
6746 ncopies = (group_size * vf + nunits - 1) / nunits;
6747 dr_chain.create (ncopies);
6748 }
6749 else
6750 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
7b5fc413 6751 }
7d75abc8
MM
6752 for (j = 0; j < ncopies; j++)
6753 {
6754 tree vec_inv;
6755
7b5fc413
RB
6756 if (nloads > 1)
6757 {
6758 vec_alloc (v, nloads);
6759 for (i = 0; i < nloads; i++)
6760 {
6761 tree newref, newoff;
355fe088 6762 gimple *incr;
7b5fc413
RB
6763 newref = build2 (MEM_REF, ltype, running_off, alias_off);
6764
6765 newref = force_gimple_operand_gsi (gsi, newref, true,
6766 NULL_TREE, true,
6767 GSI_SAME_STMT);
6768 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, newref);
6769 newoff = copy_ssa_name (running_off);
6770 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
6771 running_off, stride_step);
6772 vect_finish_stmt_generation (stmt, incr, gsi);
6773
6774 running_off = newoff;
6775 }
6776
6777 vec_inv = build_constructor (vectype, v);
6778 new_temp = vect_init_vector (stmt, vec_inv, vectype, gsi);
6779 new_stmt = SSA_NAME_DEF_STMT (new_temp);
6780 }
6781 else
7d75abc8 6782 {
7b5fc413
RB
6783 new_stmt = gimple_build_assign (make_ssa_name (ltype),
6784 build2 (MEM_REF, ltype,
6785 running_off, alias_off));
6786 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6787
6788 tree newoff = copy_ssa_name (running_off);
355fe088 6789 gimple *incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
0d0e4a03 6790 running_off, stride_step);
7d75abc8
MM
6791 vect_finish_stmt_generation (stmt, incr, gsi);
6792
6793 running_off = newoff;
6794 }
6795
7b5fc413 6796 if (slp)
b266b968 6797 {
b266b968
RB
6798 if (slp_perm)
6799 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
66c16fd9
RB
6800 else
6801 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
b266b968 6802 }
7d75abc8 6803 else
225ce44b
RB
6804 {
6805 if (j == 0)
6806 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6807 else
6808 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6809 prev_stmt_info = vinfo_for_stmt (new_stmt);
6810 }
7d75abc8 6811 }
b266b968
RB
6812 if (slp_perm)
6813 vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
6814 slp_node_instance, false);
7d75abc8
MM
6815 return true;
6816 }
aec7ae7d 6817
0d0293ac 6818 if (grouped_load)
ebfd146a 6819 {
e14c1050 6820 first_stmt = GROUP_FIRST_ELEMENT (stmt_info);
4f0a0218 6821 /* For SLP vectorization we directly vectorize a subchain
52eab378
RB
6822 without permutation. */
6823 if (slp && ! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
4f0a0218
RB
6824 first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6825 /* For BB vectorization always use the first stmt to base
6826 the data ref pointer on. */
6827 if (bb_vinfo)
6828 first_stmt_for_drptr = SLP_TREE_SCALAR_STMTS (slp_node)[0];
6aa904c4 6829
ebfd146a 6830 /* Check if the chain of loads is already vectorized. */
01d8bf07
RB
6831 if (STMT_VINFO_VEC_STMT (vinfo_for_stmt (first_stmt))
6832 /* For SLP we would need to copy over SLP_TREE_VEC_STMTS.
6833 ??? But we can only do so if there is exactly one
6834 as we have no way to get at the rest. Leave the CSE
6835 opportunity alone.
6836 ??? With the group load eventually participating
6837 in multiple different permutations (having multiple
6838 slp nodes which refer to the same group) the CSE
6839 is even wrong code. See PR56270. */
6840 && !slp)
ebfd146a
IR
6841 {
6842 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
6843 return true;
6844 }
6845 first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt));
e14c1050 6846 group_size = GROUP_SIZE (vinfo_for_stmt (first_stmt));
9b999e8c 6847 group_gap_adj = 0;
ebfd146a
IR
6848
6849 /* VEC_NUM is the number of vect stmts to be created for this group. */
6850 if (slp)
6851 {
0d0293ac 6852 grouped_load = false;
91ff1504
RB
6853 /* For SLP permutation support we need to load the whole group,
6854 not only the number of vector stmts the permutation result
6855 fits in. */
6856 if (slp_perm)
6857 vec_num = (group_size * vf + nunits - 1) / nunits;
6858 else
6859 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
9b999e8c 6860 group_gap_adj = vf * group_size - nunits * vec_num;
a70d6342 6861 }
ebfd146a 6862 else
9b999e8c 6863 vec_num = group_size;
ebfd146a
IR
6864 }
6865 else
6866 {
6867 first_stmt = stmt;
6868 first_dr = dr;
6869 group_size = vec_num = 1;
9b999e8c 6870 group_gap_adj = 0;
ebfd146a
IR
6871 }
6872
720f5239 6873 alignment_support_scheme = vect_supportable_dr_alignment (first_dr, false);
ebfd146a 6874 gcc_assert (alignment_support_scheme);
272c6793
RS
6875 /* Targets with load-lane instructions must not require explicit
6876 realignment. */
6877 gcc_assert (!load_lanes_p
6878 || alignment_support_scheme == dr_aligned
6879 || alignment_support_scheme == dr_unaligned_supported);
ebfd146a
IR
6880
6881 /* In case the vectorization factor (VF) is bigger than the number
6882 of elements that we can fit in a vectype (nunits), we have to generate
6883 more than one vector stmt - i.e - we need to "unroll" the
ff802fa1 6884 vector stmt by a factor VF/nunits. In doing so, we record a pointer
ebfd146a 6885 from one copy of the vector stmt to the next, in the field
ff802fa1 6886 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
ebfd146a 6887 stages to find the correct vector defs to be used when vectorizing
ff802fa1
IR
6888 stmts that use the defs of the current stmt. The example below
6889 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
6890 need to create 4 vectorized stmts):
ebfd146a
IR
6891
6892 before vectorization:
6893 RELATED_STMT VEC_STMT
6894 S1: x = memref - -
6895 S2: z = x + 1 - -
6896
6897 step 1: vectorize stmt S1:
6898 We first create the vector stmt VS1_0, and, as usual, record a
6899 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
6900 Next, we create the vector stmt VS1_1, and record a pointer to
6901 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
ff802fa1 6902 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
ebfd146a
IR
6903 stmts and pointers:
6904 RELATED_STMT VEC_STMT
6905 VS1_0: vx0 = memref0 VS1_1 -
6906 VS1_1: vx1 = memref1 VS1_2 -
6907 VS1_2: vx2 = memref2 VS1_3 -
6908 VS1_3: vx3 = memref3 - -
6909 S1: x = load - VS1_0
6910 S2: z = x + 1 - -
6911
b8698a0f
L
6912 See in documentation in vect_get_vec_def_for_stmt_copy for how the
6913 information we recorded in RELATED_STMT field is used to vectorize
ebfd146a
IR
6914 stmt S2. */
6915
0d0293ac 6916 /* In case of interleaving (non-unit grouped access):
ebfd146a
IR
6917
6918 S1: x2 = &base + 2
6919 S2: x0 = &base
6920 S3: x1 = &base + 1
6921 S4: x3 = &base + 3
6922
b8698a0f 6923 Vectorized loads are created in the order of memory accesses
ebfd146a
IR
6924 starting from the access of the first stmt of the chain:
6925
6926 VS1: vx0 = &base
6927 VS2: vx1 = &base + vec_size*1
6928 VS3: vx3 = &base + vec_size*2
6929 VS4: vx4 = &base + vec_size*3
6930
6931 Then permutation statements are generated:
6932
e2c83630
RH
6933 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
6934 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
ebfd146a
IR
6935 ...
6936
6937 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
6938 (the order of the data-refs in the output of vect_permute_load_chain
6939 corresponds to the order of scalar stmts in the interleaving chain - see
6940 the documentation of vect_permute_load_chain()).
6941 The generation of permutation stmts and recording them in
0d0293ac 6942 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
ebfd146a 6943
b8698a0f 6944 In case of both multiple types and interleaving, the vector loads and
ff802fa1
IR
6945 permutation stmts above are created for every copy. The result vector
6946 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
6947 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
ebfd146a
IR
6948
6949 /* If the data reference is aligned (dr_aligned) or potentially unaligned
6950 on a target that supports unaligned accesses (dr_unaligned_supported)
6951 we generate the following code:
6952 p = initial_addr;
6953 indx = 0;
6954 loop {
6955 p = p + indx * vectype_size;
6956 vec_dest = *(p);
6957 indx = indx + 1;
6958 }
6959
6960 Otherwise, the data reference is potentially unaligned on a target that
b8698a0f 6961 does not support unaligned accesses (dr_explicit_realign_optimized) -
ebfd146a
IR
6962 then generate the following code, in which the data in each iteration is
6963 obtained by two vector loads, one from the previous iteration, and one
6964 from the current iteration:
6965 p1 = initial_addr;
6966 msq_init = *(floor(p1))
6967 p2 = initial_addr + VS - 1;
6968 realignment_token = call target_builtin;
6969 indx = 0;
6970 loop {
6971 p2 = p2 + indx * vectype_size
6972 lsq = *(floor(p2))
6973 vec_dest = realign_load (msq, lsq, realignment_token)
6974 indx = indx + 1;
6975 msq = lsq;
6976 } */
6977
6978 /* If the misalignment remains the same throughout the execution of the
6979 loop, we can create the init_addr and permutation mask at the loop
ff802fa1 6980 preheader. Otherwise, it needs to be created inside the loop.
ebfd146a
IR
6981 This can only occur when vectorizing memory accesses in the inner-loop
6982 nested within an outer-loop that is being vectorized. */
6983
d1e4b493 6984 if (nested_in_vect_loop
211bea38 6985 && (TREE_INT_CST_LOW (DR_STEP (dr))
ebfd146a
IR
6986 % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0))
6987 {
6988 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
6989 compute_in_loop = true;
6990 }
6991
6992 if ((alignment_support_scheme == dr_explicit_realign_optimized
6993 || alignment_support_scheme == dr_explicit_realign)
59fd17e3 6994 && !compute_in_loop)
ebfd146a
IR
6995 {
6996 msq = vect_setup_realignment (first_stmt, gsi, &realignment_token,
6997 alignment_support_scheme, NULL_TREE,
6998 &at_loop);
6999 if (alignment_support_scheme == dr_explicit_realign_optimized)
7000 {
538dd0b7 7001 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (msq));
356bbc4c
JJ
7002 byte_offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
7003 size_one_node);
ebfd146a
IR
7004 }
7005 }
7006 else
7007 at_loop = loop;
7008
a1e53f3f
L
7009 if (negative)
7010 offset = size_int (-TYPE_VECTOR_SUBPARTS (vectype) + 1);
7011
272c6793
RS
7012 if (load_lanes_p)
7013 aggr_type = build_array_type_nelts (elem_type, vec_num * nunits);
7014 else
7015 aggr_type = vectype;
7016
ebfd146a
IR
7017 prev_stmt_info = NULL;
7018 for (j = 0; j < ncopies; j++)
b8698a0f 7019 {
272c6793 7020 /* 1. Create the vector or array pointer update chain. */
ebfd146a 7021 if (j == 0)
74bf76ed
JJ
7022 {
7023 bool simd_lane_access_p
7024 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info);
7025 if (simd_lane_access_p
7026 && TREE_CODE (DR_BASE_ADDRESS (first_dr)) == ADDR_EXPR
7027 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr), 0))
7028 && integer_zerop (DR_OFFSET (first_dr))
7029 && integer_zerop (DR_INIT (first_dr))
7030 && alias_sets_conflict_p (get_alias_set (aggr_type),
7031 get_alias_set (DR_REF (first_dr)))
7032 && (alignment_support_scheme == dr_aligned
7033 || alignment_support_scheme == dr_unaligned_supported))
7034 {
7035 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr));
7036 dataref_offset = build_int_cst (reference_alias_ptr_type
7037 (DR_REF (first_dr)), 0);
8928eff3 7038 inv_p = false;
74bf76ed 7039 }
4f0a0218
RB
7040 else if (first_stmt_for_drptr
7041 && first_stmt != first_stmt_for_drptr)
7042 {
7043 dataref_ptr
7044 = vect_create_data_ref_ptr (first_stmt_for_drptr, aggr_type,
7045 at_loop, offset, &dummy, gsi,
7046 &ptr_incr, simd_lane_access_p,
7047 &inv_p, byte_offset);
7048 /* Adjust the pointer by the difference to first_stmt. */
7049 data_reference_p ptrdr
7050 = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_stmt_for_drptr));
7051 tree diff = fold_convert (sizetype,
7052 size_binop (MINUS_EXPR,
7053 DR_INIT (first_dr),
7054 DR_INIT (ptrdr)));
7055 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7056 stmt, diff);
7057 }
74bf76ed
JJ
7058 else
7059 dataref_ptr
7060 = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop,
7061 offset, &dummy, gsi, &ptr_incr,
356bbc4c
JJ
7062 simd_lane_access_p, &inv_p,
7063 byte_offset);
74bf76ed
JJ
7064 }
7065 else if (dataref_offset)
7066 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
7067 TYPE_SIZE_UNIT (aggr_type));
ebfd146a 7068 else
272c6793
RS
7069 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt,
7070 TYPE_SIZE_UNIT (aggr_type));
ebfd146a 7071
0d0293ac 7072 if (grouped_load || slp_perm)
9771b263 7073 dr_chain.create (vec_num);
5ce1ee7f 7074
272c6793 7075 if (load_lanes_p)
ebfd146a 7076 {
272c6793
RS
7077 tree vec_array;
7078
7079 vec_array = create_vector_array (vectype, vec_num);
7080
7081 /* Emit:
7082 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
7083 data_ref = create_array_ref (aggr_type, dataref_ptr, first_dr);
7084 new_stmt = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
7085 gimple_call_set_lhs (new_stmt, vec_array);
7086 vect_finish_stmt_generation (stmt, new_stmt, gsi);
ebfd146a 7087
272c6793
RS
7088 /* Extract each vector into an SSA_NAME. */
7089 for (i = 0; i < vec_num; i++)
ebfd146a 7090 {
272c6793
RS
7091 new_temp = read_vector_array (stmt, gsi, scalar_dest,
7092 vec_array, i);
9771b263 7093 dr_chain.quick_push (new_temp);
272c6793
RS
7094 }
7095
7096 /* Record the mapping between SSA_NAMEs and statements. */
0d0293ac 7097 vect_record_grouped_load_vectors (stmt, dr_chain);
272c6793
RS
7098 }
7099 else
7100 {
7101 for (i = 0; i < vec_num; i++)
7102 {
7103 if (i > 0)
7104 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7105 stmt, NULL_TREE);
7106
7107 /* 2. Create the vector-load in the loop. */
7108 switch (alignment_support_scheme)
7109 {
7110 case dr_aligned:
7111 case dr_unaligned_supported:
be1ac4ec 7112 {
644ffefd
MJ
7113 unsigned int align, misalign;
7114
272c6793 7115 data_ref
aed93b23
RB
7116 = fold_build2 (MEM_REF, vectype, dataref_ptr,
7117 dataref_offset
7118 ? dataref_offset
7119 : build_int_cst (reference_alias_ptr_type
7120 (DR_REF (first_dr)), 0));
644ffefd 7121 align = TYPE_ALIGN_UNIT (vectype);
272c6793
RS
7122 if (alignment_support_scheme == dr_aligned)
7123 {
7124 gcc_assert (aligned_access_p (first_dr));
644ffefd 7125 misalign = 0;
272c6793
RS
7126 }
7127 else if (DR_MISALIGNMENT (first_dr) == -1)
7128 {
52639a61
RB
7129 if (DR_VECT_AUX (first_dr)->base_element_aligned)
7130 align = TYPE_ALIGN_UNIT (elem_type);
7131 else
7132 align = (get_object_alignment (DR_REF (first_dr))
7133 / BITS_PER_UNIT);
7134 misalign = 0;
272c6793
RS
7135 TREE_TYPE (data_ref)
7136 = build_aligned_type (TREE_TYPE (data_ref),
52639a61 7137 align * BITS_PER_UNIT);
272c6793
RS
7138 }
7139 else
7140 {
7141 TREE_TYPE (data_ref)
7142 = build_aligned_type (TREE_TYPE (data_ref),
7143 TYPE_ALIGN (elem_type));
644ffefd 7144 misalign = DR_MISALIGNMENT (first_dr);
272c6793 7145 }
aed93b23
RB
7146 if (dataref_offset == NULL_TREE
7147 && TREE_CODE (dataref_ptr) == SSA_NAME)
74bf76ed
JJ
7148 set_ptr_info_alignment (get_ptr_info (dataref_ptr),
7149 align, misalign);
272c6793 7150 break;
be1ac4ec 7151 }
272c6793 7152 case dr_explicit_realign:
267d3070 7153 {
272c6793 7154 tree ptr, bump;
272c6793 7155
d88981fc 7156 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
272c6793
RS
7157
7158 if (compute_in_loop)
7159 msq = vect_setup_realignment (first_stmt, gsi,
7160 &realignment_token,
7161 dr_explicit_realign,
7162 dataref_ptr, NULL);
7163
aed93b23
RB
7164 if (TREE_CODE (dataref_ptr) == SSA_NAME)
7165 ptr = copy_ssa_name (dataref_ptr);
7166 else
7167 ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
0d0e4a03
JJ
7168 new_stmt = gimple_build_assign
7169 (ptr, BIT_AND_EXPR, dataref_ptr,
272c6793
RS
7170 build_int_cst
7171 (TREE_TYPE (dataref_ptr),
7172 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
272c6793
RS
7173 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7174 data_ref
7175 = build2 (MEM_REF, vectype, ptr,
7176 build_int_cst (reference_alias_ptr_type
7177 (DR_REF (first_dr)), 0));
7178 vec_dest = vect_create_destination_var (scalar_dest,
7179 vectype);
7180 new_stmt = gimple_build_assign (vec_dest, data_ref);
7181 new_temp = make_ssa_name (vec_dest, new_stmt);
7182 gimple_assign_set_lhs (new_stmt, new_temp);
7183 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
7184 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
7185 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7186 msq = new_temp;
7187
d88981fc 7188 bump = size_binop (MULT_EXPR, vs,
7b7b1813 7189 TYPE_SIZE_UNIT (elem_type));
d88981fc 7190 bump = size_binop (MINUS_EXPR, bump, size_one_node);
272c6793 7191 ptr = bump_vector_ptr (dataref_ptr, NULL, gsi, stmt, bump);
0d0e4a03
JJ
7192 new_stmt = gimple_build_assign
7193 (NULL_TREE, BIT_AND_EXPR, ptr,
272c6793
RS
7194 build_int_cst
7195 (TREE_TYPE (ptr),
7196 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
aed93b23 7197 ptr = copy_ssa_name (ptr, new_stmt);
272c6793
RS
7198 gimple_assign_set_lhs (new_stmt, ptr);
7199 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7200 data_ref
7201 = build2 (MEM_REF, vectype, ptr,
7202 build_int_cst (reference_alias_ptr_type
7203 (DR_REF (first_dr)), 0));
7204 break;
267d3070 7205 }
272c6793 7206 case dr_explicit_realign_optimized:
aed93b23
RB
7207 if (TREE_CODE (dataref_ptr) == SSA_NAME)
7208 new_temp = copy_ssa_name (dataref_ptr);
7209 else
7210 new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
0d0e4a03
JJ
7211 new_stmt = gimple_build_assign
7212 (new_temp, BIT_AND_EXPR, dataref_ptr,
272c6793
RS
7213 build_int_cst
7214 (TREE_TYPE (dataref_ptr),
7215 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
272c6793
RS
7216 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7217 data_ref
7218 = build2 (MEM_REF, vectype, new_temp,
7219 build_int_cst (reference_alias_ptr_type
7220 (DR_REF (first_dr)), 0));
7221 break;
7222 default:
7223 gcc_unreachable ();
7224 }
ebfd146a 7225 vec_dest = vect_create_destination_var (scalar_dest, vectype);
272c6793 7226 new_stmt = gimple_build_assign (vec_dest, data_ref);
ebfd146a
IR
7227 new_temp = make_ssa_name (vec_dest, new_stmt);
7228 gimple_assign_set_lhs (new_stmt, new_temp);
7229 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7230
272c6793
RS
7231 /* 3. Handle explicit realignment if necessary/supported.
7232 Create in loop:
7233 vec_dest = realign_load (msq, lsq, realignment_token) */
7234 if (alignment_support_scheme == dr_explicit_realign_optimized
7235 || alignment_support_scheme == dr_explicit_realign)
ebfd146a 7236 {
272c6793
RS
7237 lsq = gimple_assign_lhs (new_stmt);
7238 if (!realignment_token)
7239 realignment_token = dataref_ptr;
7240 vec_dest = vect_create_destination_var (scalar_dest, vectype);
0d0e4a03
JJ
7241 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR,
7242 msq, lsq, realignment_token);
272c6793
RS
7243 new_temp = make_ssa_name (vec_dest, new_stmt);
7244 gimple_assign_set_lhs (new_stmt, new_temp);
7245 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7246
7247 if (alignment_support_scheme == dr_explicit_realign_optimized)
7248 {
7249 gcc_assert (phi);
7250 if (i == vec_num - 1 && j == ncopies - 1)
7251 add_phi_arg (phi, lsq,
7252 loop_latch_edge (containing_loop),
9e227d60 7253 UNKNOWN_LOCATION);
272c6793
RS
7254 msq = lsq;
7255 }
ebfd146a 7256 }
ebfd146a 7257
59fd17e3
RB
7258 /* 4. Handle invariant-load. */
7259 if (inv_p && !bb_vinfo)
7260 {
59fd17e3 7261 gcc_assert (!grouped_load);
d1417442
JJ
7262 /* If we have versioned for aliasing or the loop doesn't
7263 have any data dependencies that would preclude this,
7264 then we are sure this is a loop invariant load and
7265 thus we can insert it on the preheader edge. */
7266 if (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
7267 && !nested_in_vect_loop
6b916b36 7268 && hoist_defs_of_uses (stmt, loop))
a0e35eb0
RB
7269 {
7270 if (dump_enabled_p ())
7271 {
7272 dump_printf_loc (MSG_NOTE, vect_location,
7273 "hoisting out of the vectorized "
7274 "loop: ");
7275 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
a0e35eb0 7276 }
b731b390 7277 tree tem = copy_ssa_name (scalar_dest);
a0e35eb0
RB
7278 gsi_insert_on_edge_immediate
7279 (loop_preheader_edge (loop),
7280 gimple_build_assign (tem,
7281 unshare_expr
7282 (gimple_assign_rhs1 (stmt))));
7283 new_temp = vect_init_vector (stmt, tem, vectype, NULL);
34cd48e5
RB
7284 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7285 set_vinfo_for_stmt (new_stmt,
7286 new_stmt_vec_info (new_stmt, vinfo));
a0e35eb0
RB
7287 }
7288 else
7289 {
7290 gimple_stmt_iterator gsi2 = *gsi;
7291 gsi_next (&gsi2);
7292 new_temp = vect_init_vector (stmt, scalar_dest,
7293 vectype, &gsi2);
34cd48e5 7294 new_stmt = SSA_NAME_DEF_STMT (new_temp);
a0e35eb0 7295 }
59fd17e3
RB
7296 }
7297
272c6793
RS
7298 if (negative)
7299 {
aec7ae7d
JJ
7300 tree perm_mask = perm_mask_for_reverse (vectype);
7301 new_temp = permute_vec_elements (new_temp, new_temp,
7302 perm_mask, stmt, gsi);
ebfd146a
IR
7303 new_stmt = SSA_NAME_DEF_STMT (new_temp);
7304 }
267d3070 7305
272c6793 7306 /* Collect vector loads and later create their permutation in
0d0293ac
MM
7307 vect_transform_grouped_load (). */
7308 if (grouped_load || slp_perm)
9771b263 7309 dr_chain.quick_push (new_temp);
267d3070 7310
272c6793
RS
7311 /* Store vector loads in the corresponding SLP_NODE. */
7312 if (slp && !slp_perm)
9771b263 7313 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
272c6793 7314 }
9b999e8c
RB
7315 /* Bump the vector pointer to account for a gap or for excess
7316 elements loaded for a permuted SLP load. */
7317 if (group_gap_adj != 0)
a64b9c26 7318 {
9b999e8c
RB
7319 bool ovf;
7320 tree bump
7321 = wide_int_to_tree (sizetype,
7322 wi::smul (TYPE_SIZE_UNIT (elem_type),
7323 group_gap_adj, &ovf));
a64b9c26
RB
7324 dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi,
7325 stmt, bump);
7326 }
ebfd146a
IR
7327 }
7328
7329 if (slp && !slp_perm)
7330 continue;
7331
7332 if (slp_perm)
7333 {
01d8bf07 7334 if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
ebfd146a
IR
7335 slp_node_instance, false))
7336 {
9771b263 7337 dr_chain.release ();
ebfd146a
IR
7338 return false;
7339 }
7340 }
7341 else
7342 {
0d0293ac 7343 if (grouped_load)
ebfd146a 7344 {
272c6793 7345 if (!load_lanes_p)
0d0293ac 7346 vect_transform_grouped_load (stmt, dr_chain, group_size, gsi);
ebfd146a 7347 *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
ebfd146a
IR
7348 }
7349 else
7350 {
7351 if (j == 0)
7352 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7353 else
7354 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7355 prev_stmt_info = vinfo_for_stmt (new_stmt);
7356 }
7357 }
9771b263 7358 dr_chain.release ();
ebfd146a
IR
7359 }
7360
ebfd146a
IR
7361 return true;
7362}
7363
7364/* Function vect_is_simple_cond.
b8698a0f 7365
ebfd146a
IR
7366 Input:
7367 LOOP - the loop that is being vectorized.
7368 COND - Condition that is checked for simple use.
7369
e9e1d143
RG
7370 Output:
7371 *COMP_VECTYPE - the vector type for the comparison.
7372
ebfd146a
IR
7373 Returns whether a COND can be vectorized. Checks whether
7374 condition operands are supportable using vec_is_simple_use. */
7375
87aab9b2 7376static bool
81c40241 7377vect_is_simple_cond (tree cond, vec_info *vinfo, tree *comp_vectype)
ebfd146a
IR
7378{
7379 tree lhs, rhs;
ebfd146a 7380 enum vect_def_type dt;
e9e1d143 7381 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
ebfd146a 7382
a414c77f
IE
7383 /* Mask case. */
7384 if (TREE_CODE (cond) == SSA_NAME
7385 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE)
7386 {
7387 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (cond);
7388 if (!vect_is_simple_use (cond, vinfo, &lhs_def_stmt,
7389 &dt, comp_vectype)
7390 || !*comp_vectype
7391 || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
7392 return false;
7393 return true;
7394 }
7395
ebfd146a
IR
7396 if (!COMPARISON_CLASS_P (cond))
7397 return false;
7398
7399 lhs = TREE_OPERAND (cond, 0);
7400 rhs = TREE_OPERAND (cond, 1);
7401
7402 if (TREE_CODE (lhs) == SSA_NAME)
7403 {
355fe088 7404 gimple *lhs_def_stmt = SSA_NAME_DEF_STMT (lhs);
81c40241 7405 if (!vect_is_simple_use (lhs, vinfo, &lhs_def_stmt, &dt, &vectype1))
ebfd146a
IR
7406 return false;
7407 }
7408 else if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (lhs) != REAL_CST
7409 && TREE_CODE (lhs) != FIXED_CST)
7410 return false;
7411
7412 if (TREE_CODE (rhs) == SSA_NAME)
7413 {
355fe088 7414 gimple *rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
81c40241 7415 if (!vect_is_simple_use (rhs, vinfo, &rhs_def_stmt, &dt, &vectype2))
ebfd146a
IR
7416 return false;
7417 }
f7e531cf 7418 else if (TREE_CODE (rhs) != INTEGER_CST && TREE_CODE (rhs) != REAL_CST
ebfd146a
IR
7419 && TREE_CODE (rhs) != FIXED_CST)
7420 return false;
7421
28b33016
IE
7422 if (vectype1 && vectype2
7423 && TYPE_VECTOR_SUBPARTS (vectype1) != TYPE_VECTOR_SUBPARTS (vectype2))
7424 return false;
7425
e9e1d143 7426 *comp_vectype = vectype1 ? vectype1 : vectype2;
ebfd146a
IR
7427 return true;
7428}
7429
7430/* vectorizable_condition.
7431
b8698a0f
L
7432 Check if STMT is conditional modify expression that can be vectorized.
7433 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7434 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
4bbe8262
IR
7435 at GSI.
7436
7437 When STMT is vectorized as nested cycle, REDUC_DEF is the vector variable
7438 to be used at REDUC_INDEX (in then clause if REDUC_INDEX is 1, and in
0ad23163 7439 else clause if it is 2).
ebfd146a
IR
7440
7441 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7442
4bbe8262 7443bool
355fe088
TS
7444vectorizable_condition (gimple *stmt, gimple_stmt_iterator *gsi,
7445 gimple **vec_stmt, tree reduc_def, int reduc_index,
f7e531cf 7446 slp_tree slp_node)
ebfd146a
IR
7447{
7448 tree scalar_dest = NULL_TREE;
7449 tree vec_dest = NULL_TREE;
ebfd146a
IR
7450 tree cond_expr, then_clause, else_clause;
7451 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
df11cc78 7452 tree comp_vectype = NULL_TREE;
ff802fa1
IR
7453 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
7454 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
5958f9e2 7455 tree vec_compare;
ebfd146a
IR
7456 tree new_temp;
7457 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
a855b1b1 7458 enum vect_def_type dt, dts[4];
f7e531cf 7459 int ncopies;
ebfd146a 7460 enum tree_code code;
a855b1b1 7461 stmt_vec_info prev_stmt_info = NULL;
f7e531cf
IR
7462 int i, j;
7463 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
6e1aa848
DN
7464 vec<tree> vec_oprnds0 = vNULL;
7465 vec<tree> vec_oprnds1 = vNULL;
7466 vec<tree> vec_oprnds2 = vNULL;
7467 vec<tree> vec_oprnds3 = vNULL;
74946978 7468 tree vec_cmp_type;
a414c77f 7469 bool masked = false;
b8698a0f 7470
f7e531cf
IR
7471 if (reduc_index && STMT_SLP_TYPE (stmt_info))
7472 return false;
7473
af29617a
AH
7474 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == TREE_CODE_REDUCTION)
7475 {
7476 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7477 return false;
ebfd146a 7478
af29617a
AH
7479 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7480 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7481 && reduc_def))
7482 return false;
ebfd146a 7483
af29617a
AH
7484 /* FORNOW: not yet supported. */
7485 if (STMT_VINFO_LIVE_P (stmt_info))
7486 {
7487 if (dump_enabled_p ())
7488 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7489 "value used after loop.\n");
7490 return false;
7491 }
ebfd146a
IR
7492 }
7493
7494 /* Is vectorizable conditional operation? */
7495 if (!is_gimple_assign (stmt))
7496 return false;
7497
7498 code = gimple_assign_rhs_code (stmt);
7499
7500 if (code != COND_EXPR)
7501 return false;
7502
465c8c19
JJ
7503 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7504 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
2947d3b2 7505 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
465c8c19 7506
fce57248 7507 if (slp_node)
465c8c19
JJ
7508 ncopies = 1;
7509 else
7510 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7511
7512 gcc_assert (ncopies >= 1);
7513 if (reduc_index && ncopies > 1)
7514 return false; /* FORNOW */
7515
4e71066d
RG
7516 cond_expr = gimple_assign_rhs1 (stmt);
7517 then_clause = gimple_assign_rhs2 (stmt);
7518 else_clause = gimple_assign_rhs3 (stmt);
ebfd146a 7519
81c40241 7520 if (!vect_is_simple_cond (cond_expr, stmt_info->vinfo, &comp_vectype)
e9e1d143 7521 || !comp_vectype)
ebfd146a
IR
7522 return false;
7523
81c40241 7524 gimple *def_stmt;
2947d3b2
IE
7525 if (!vect_is_simple_use (then_clause, stmt_info->vinfo, &def_stmt, &dt,
7526 &vectype1))
7527 return false;
7528 if (!vect_is_simple_use (else_clause, stmt_info->vinfo, &def_stmt, &dt,
7529 &vectype2))
ebfd146a 7530 return false;
2947d3b2
IE
7531
7532 if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
7533 return false;
7534
7535 if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
ebfd146a
IR
7536 return false;
7537
28b33016
IE
7538 masked = !COMPARISON_CLASS_P (cond_expr);
7539 vec_cmp_type = build_same_sized_truth_vector_type (comp_vectype);
7540
74946978
MP
7541 if (vec_cmp_type == NULL_TREE)
7542 return false;
784fb9b3 7543
b8698a0f 7544 if (!vec_stmt)
ebfd146a
IR
7545 {
7546 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
e9e1d143 7547 return expand_vec_cond_expr_p (vectype, comp_vectype);
ebfd146a
IR
7548 }
7549
f7e531cf
IR
7550 /* Transform. */
7551
7552 if (!slp_node)
7553 {
9771b263
DN
7554 vec_oprnds0.create (1);
7555 vec_oprnds1.create (1);
7556 vec_oprnds2.create (1);
7557 vec_oprnds3.create (1);
f7e531cf 7558 }
ebfd146a
IR
7559
7560 /* Handle def. */
7561 scalar_dest = gimple_assign_lhs (stmt);
7562 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7563
7564 /* Handle cond expr. */
a855b1b1
MM
7565 for (j = 0; j < ncopies; j++)
7566 {
538dd0b7 7567 gassign *new_stmt = NULL;
a855b1b1
MM
7568 if (j == 0)
7569 {
f7e531cf
IR
7570 if (slp_node)
7571 {
00f96dc9
TS
7572 auto_vec<tree, 4> ops;
7573 auto_vec<vec<tree>, 4> vec_defs;
9771b263 7574
a414c77f
IE
7575 if (masked)
7576 ops.safe_push (cond_expr);
7577 else
7578 {
7579 ops.safe_push (TREE_OPERAND (cond_expr, 0));
7580 ops.safe_push (TREE_OPERAND (cond_expr, 1));
7581 }
9771b263
DN
7582 ops.safe_push (then_clause);
7583 ops.safe_push (else_clause);
f7e531cf 7584 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
37b5ec8f
JJ
7585 vec_oprnds3 = vec_defs.pop ();
7586 vec_oprnds2 = vec_defs.pop ();
a414c77f
IE
7587 if (!masked)
7588 vec_oprnds1 = vec_defs.pop ();
37b5ec8f 7589 vec_oprnds0 = vec_defs.pop ();
f7e531cf 7590
9771b263
DN
7591 ops.release ();
7592 vec_defs.release ();
f7e531cf
IR
7593 }
7594 else
7595 {
355fe088 7596 gimple *gtemp;
a414c77f
IE
7597 if (masked)
7598 {
7599 vec_cond_lhs
7600 = vect_get_vec_def_for_operand (cond_expr, stmt,
7601 comp_vectype);
7602 vect_is_simple_use (cond_expr, stmt_info->vinfo,
7603 &gtemp, &dts[0]);
7604 }
7605 else
7606 {
7607 vec_cond_lhs =
7608 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 0),
7609 stmt, comp_vectype);
7610 vect_is_simple_use (TREE_OPERAND (cond_expr, 0),
7611 loop_vinfo, &gtemp, &dts[0]);
7612
7613 vec_cond_rhs =
7614 vect_get_vec_def_for_operand (TREE_OPERAND (cond_expr, 1),
7615 stmt, comp_vectype);
7616 vect_is_simple_use (TREE_OPERAND (cond_expr, 1),
7617 loop_vinfo, &gtemp, &dts[1]);
7618 }
f7e531cf
IR
7619 if (reduc_index == 1)
7620 vec_then_clause = reduc_def;
7621 else
7622 {
7623 vec_then_clause = vect_get_vec_def_for_operand (then_clause,
81c40241
RB
7624 stmt);
7625 vect_is_simple_use (then_clause, loop_vinfo,
7626 &gtemp, &dts[2]);
f7e531cf
IR
7627 }
7628 if (reduc_index == 2)
7629 vec_else_clause = reduc_def;
7630 else
7631 {
7632 vec_else_clause = vect_get_vec_def_for_operand (else_clause,
81c40241
RB
7633 stmt);
7634 vect_is_simple_use (else_clause, loop_vinfo, &gtemp, &dts[3]);
f7e531cf 7635 }
a855b1b1
MM
7636 }
7637 }
7638 else
7639 {
a414c77f
IE
7640 vec_cond_lhs
7641 = vect_get_vec_def_for_stmt_copy (dts[0],
7642 vec_oprnds0.pop ());
7643 if (!masked)
7644 vec_cond_rhs
7645 = vect_get_vec_def_for_stmt_copy (dts[1],
7646 vec_oprnds1.pop ());
7647
a855b1b1 7648 vec_then_clause = vect_get_vec_def_for_stmt_copy (dts[2],
9771b263 7649 vec_oprnds2.pop ());
a855b1b1 7650 vec_else_clause = vect_get_vec_def_for_stmt_copy (dts[3],
9771b263 7651 vec_oprnds3.pop ());
f7e531cf
IR
7652 }
7653
7654 if (!slp_node)
7655 {
9771b263 7656 vec_oprnds0.quick_push (vec_cond_lhs);
a414c77f
IE
7657 if (!masked)
7658 vec_oprnds1.quick_push (vec_cond_rhs);
9771b263
DN
7659 vec_oprnds2.quick_push (vec_then_clause);
7660 vec_oprnds3.quick_push (vec_else_clause);
a855b1b1
MM
7661 }
7662
9dc3f7de 7663 /* Arguments are ready. Create the new vector stmt. */
9771b263 7664 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
f7e531cf 7665 {
9771b263
DN
7666 vec_then_clause = vec_oprnds2[i];
7667 vec_else_clause = vec_oprnds3[i];
a855b1b1 7668
a414c77f
IE
7669 if (masked)
7670 vec_compare = vec_cond_lhs;
7671 else
7672 {
7673 vec_cond_rhs = vec_oprnds1[i];
7674 vec_compare = build2 (TREE_CODE (cond_expr), vec_cmp_type,
7675 vec_cond_lhs, vec_cond_rhs);
7676 }
5958f9e2
JJ
7677 new_temp = make_ssa_name (vec_dest);
7678 new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR,
7679 vec_compare, vec_then_clause,
7680 vec_else_clause);
f7e531cf
IR
7681 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7682 if (slp_node)
9771b263 7683 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
f7e531cf
IR
7684 }
7685
7686 if (slp_node)
7687 continue;
7688
7689 if (j == 0)
7690 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7691 else
7692 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7693
7694 prev_stmt_info = vinfo_for_stmt (new_stmt);
a855b1b1 7695 }
b8698a0f 7696
9771b263
DN
7697 vec_oprnds0.release ();
7698 vec_oprnds1.release ();
7699 vec_oprnds2.release ();
7700 vec_oprnds3.release ();
f7e531cf 7701
ebfd146a
IR
7702 return true;
7703}
7704
42fd8198
IE
7705/* vectorizable_comparison.
7706
7707 Check if STMT is comparison expression that can be vectorized.
7708 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
7709 comparison, put it in VEC_STMT, and insert it at GSI.
7710
7711 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
7712
fce57248 7713static bool
42fd8198
IE
7714vectorizable_comparison (gimple *stmt, gimple_stmt_iterator *gsi,
7715 gimple **vec_stmt, tree reduc_def,
7716 slp_tree slp_node)
7717{
7718 tree lhs, rhs1, rhs2;
7719 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
7720 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
7721 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7722 tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
7723 tree new_temp;
7724 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
7725 enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
7726 unsigned nunits;
7727 int ncopies;
7728 enum tree_code code;
7729 stmt_vec_info prev_stmt_info = NULL;
7730 int i, j;
7731 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
7732 vec<tree> vec_oprnds0 = vNULL;
7733 vec<tree> vec_oprnds1 = vNULL;
7734 gimple *def_stmt;
7735 tree mask_type;
7736 tree mask;
7737
c245362b
IE
7738 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7739 return false;
7740
30480bcd 7741 if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
42fd8198
IE
7742 return false;
7743
7744 mask_type = vectype;
7745 nunits = TYPE_VECTOR_SUBPARTS (vectype);
7746
fce57248 7747 if (slp_node)
42fd8198
IE
7748 ncopies = 1;
7749 else
7750 ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
7751
7752 gcc_assert (ncopies >= 1);
42fd8198
IE
7753 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7754 && !(STMT_VINFO_DEF_TYPE (stmt_info) == vect_nested_cycle
7755 && reduc_def))
7756 return false;
7757
7758 if (STMT_VINFO_LIVE_P (stmt_info))
7759 {
7760 if (dump_enabled_p ())
7761 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7762 "value used after loop.\n");
7763 return false;
7764 }
7765
7766 if (!is_gimple_assign (stmt))
7767 return false;
7768
7769 code = gimple_assign_rhs_code (stmt);
7770
7771 if (TREE_CODE_CLASS (code) != tcc_comparison)
7772 return false;
7773
7774 rhs1 = gimple_assign_rhs1 (stmt);
7775 rhs2 = gimple_assign_rhs2 (stmt);
7776
7777 if (!vect_is_simple_use (rhs1, stmt_info->vinfo, &def_stmt,
7778 &dts[0], &vectype1))
7779 return false;
7780
7781 if (!vect_is_simple_use (rhs2, stmt_info->vinfo, &def_stmt,
7782 &dts[1], &vectype2))
7783 return false;
7784
7785 if (vectype1 && vectype2
7786 && TYPE_VECTOR_SUBPARTS (vectype1) != TYPE_VECTOR_SUBPARTS (vectype2))
7787 return false;
7788
7789 vectype = vectype1 ? vectype1 : vectype2;
7790
7791 /* Invariant comparison. */
7792 if (!vectype)
7793 {
69a9a66f
RB
7794 vectype = get_vectype_for_scalar_type (TREE_TYPE (rhs1));
7795 if (TYPE_VECTOR_SUBPARTS (vectype) != nunits)
42fd8198
IE
7796 return false;
7797 }
7798 else if (nunits != TYPE_VECTOR_SUBPARTS (vectype))
7799 return false;
7800
7801 if (!vec_stmt)
7802 {
7803 STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
7804 vect_model_simple_cost (stmt_info, ncopies, dts, NULL, NULL);
7805 return expand_vec_cmp_expr_p (vectype, mask_type);
7806 }
7807
7808 /* Transform. */
7809 if (!slp_node)
7810 {
7811 vec_oprnds0.create (1);
7812 vec_oprnds1.create (1);
7813 }
7814
7815 /* Handle def. */
7816 lhs = gimple_assign_lhs (stmt);
7817 mask = vect_create_destination_var (lhs, mask_type);
7818
7819 /* Handle cmp expr. */
7820 for (j = 0; j < ncopies; j++)
7821 {
7822 gassign *new_stmt = NULL;
7823 if (j == 0)
7824 {
7825 if (slp_node)
7826 {
7827 auto_vec<tree, 2> ops;
7828 auto_vec<vec<tree>, 2> vec_defs;
7829
7830 ops.safe_push (rhs1);
7831 ops.safe_push (rhs2);
7832 vect_get_slp_defs (ops, slp_node, &vec_defs, -1);
7833 vec_oprnds1 = vec_defs.pop ();
7834 vec_oprnds0 = vec_defs.pop ();
7835 }
7836 else
7837 {
e4af0bc4
IE
7838 vec_rhs1 = vect_get_vec_def_for_operand (rhs1, stmt, vectype);
7839 vec_rhs2 = vect_get_vec_def_for_operand (rhs2, stmt, vectype);
42fd8198
IE
7840 }
7841 }
7842 else
7843 {
7844 vec_rhs1 = vect_get_vec_def_for_stmt_copy (dts[0],
7845 vec_oprnds0.pop ());
7846 vec_rhs2 = vect_get_vec_def_for_stmt_copy (dts[1],
7847 vec_oprnds1.pop ());
7848 }
7849
7850 if (!slp_node)
7851 {
7852 vec_oprnds0.quick_push (vec_rhs1);
7853 vec_oprnds1.quick_push (vec_rhs2);
7854 }
7855
7856 /* Arguments are ready. Create the new vector stmt. */
7857 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
7858 {
7859 vec_rhs2 = vec_oprnds1[i];
7860
7861 new_temp = make_ssa_name (mask);
7862 new_stmt = gimple_build_assign (new_temp, code, vec_rhs1, vec_rhs2);
7863 vect_finish_stmt_generation (stmt, new_stmt, gsi);
7864 if (slp_node)
7865 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
7866 }
7867
7868 if (slp_node)
7869 continue;
7870
7871 if (j == 0)
7872 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
7873 else
7874 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
7875
7876 prev_stmt_info = vinfo_for_stmt (new_stmt);
7877 }
7878
7879 vec_oprnds0.release ();
7880 vec_oprnds1.release ();
7881
7882 return true;
7883}
ebfd146a 7884
8644a673 7885/* Make sure the statement is vectorizable. */
ebfd146a
IR
7886
7887bool
355fe088 7888vect_analyze_stmt (gimple *stmt, bool *need_to_vectorize, slp_tree node)
ebfd146a 7889{
8644a673 7890 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
a70d6342 7891 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
b8698a0f 7892 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
ebfd146a 7893 bool ok;
a70d6342 7894 tree scalar_type, vectype;
355fe088 7895 gimple *pattern_stmt;
363477c0 7896 gimple_seq pattern_def_seq;
ebfd146a 7897
73fbfcad 7898 if (dump_enabled_p ())
ebfd146a 7899 {
78c60e3d
SS
7900 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: ");
7901 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
8644a673 7902 }
ebfd146a 7903
1825a1f3 7904 if (gimple_has_volatile_ops (stmt))
b8698a0f 7905 {
73fbfcad 7906 if (dump_enabled_p ())
78c60e3d 7907 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 7908 "not vectorized: stmt has volatile operands\n");
1825a1f3
IR
7909
7910 return false;
7911 }
b8698a0f
L
7912
7913 /* Skip stmts that do not need to be vectorized. In loops this is expected
8644a673
IR
7914 to include:
7915 - the COND_EXPR which is the loop exit condition
7916 - any LABEL_EXPRs in the loop
b8698a0f 7917 - computations that are used only for array indexing or loop control.
8644a673 7918 In basic blocks we only analyze statements that are a part of some SLP
83197f37 7919 instance, therefore, all the statements are relevant.
ebfd146a 7920
d092494c 7921 Pattern statement needs to be analyzed instead of the original statement
83197f37 7922 if the original statement is not relevant. Otherwise, we analyze both
079c527f
JJ
7923 statements. In basic blocks we are called from some SLP instance
7924 traversal, don't analyze pattern stmts instead, the pattern stmts
7925 already will be part of SLP instance. */
83197f37
IR
7926
7927 pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
b8698a0f 7928 if (!STMT_VINFO_RELEVANT_P (stmt_info)
8644a673 7929 && !STMT_VINFO_LIVE_P (stmt_info))
ebfd146a 7930 {
9d5e7640 7931 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
83197f37 7932 && pattern_stmt
9d5e7640
IR
7933 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7934 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7935 {
83197f37 7936 /* Analyze PATTERN_STMT instead of the original stmt. */
9d5e7640
IR
7937 stmt = pattern_stmt;
7938 stmt_info = vinfo_for_stmt (pattern_stmt);
73fbfcad 7939 if (dump_enabled_p ())
9d5e7640 7940 {
78c60e3d
SS
7941 dump_printf_loc (MSG_NOTE, vect_location,
7942 "==> examining pattern statement: ");
7943 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
9d5e7640
IR
7944 }
7945 }
7946 else
7947 {
73fbfcad 7948 if (dump_enabled_p ())
e645e942 7949 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
ebfd146a 7950
9d5e7640
IR
7951 return true;
7952 }
8644a673 7953 }
83197f37 7954 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
079c527f 7955 && node == NULL
83197f37
IR
7956 && pattern_stmt
7957 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7958 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7959 {
7960 /* Analyze PATTERN_STMT too. */
73fbfcad 7961 if (dump_enabled_p ())
83197f37 7962 {
78c60e3d
SS
7963 dump_printf_loc (MSG_NOTE, vect_location,
7964 "==> examining pattern statement: ");
7965 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
83197f37
IR
7966 }
7967
7968 if (!vect_analyze_stmt (pattern_stmt, need_to_vectorize, node))
7969 return false;
7970 }
ebfd146a 7971
1107f3ae 7972 if (is_pattern_stmt_p (stmt_info)
079c527f 7973 && node == NULL
363477c0 7974 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
1107f3ae 7975 {
363477c0 7976 gimple_stmt_iterator si;
1107f3ae 7977
363477c0
JJ
7978 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
7979 {
355fe088 7980 gimple *pattern_def_stmt = gsi_stmt (si);
363477c0
JJ
7981 if (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_def_stmt))
7982 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_def_stmt)))
7983 {
7984 /* Analyze def stmt of STMT if it's a pattern stmt. */
73fbfcad 7985 if (dump_enabled_p ())
363477c0 7986 {
78c60e3d
SS
7987 dump_printf_loc (MSG_NOTE, vect_location,
7988 "==> examining pattern def statement: ");
7989 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, pattern_def_stmt, 0);
363477c0 7990 }
1107f3ae 7991
363477c0
JJ
7992 if (!vect_analyze_stmt (pattern_def_stmt,
7993 need_to_vectorize, node))
7994 return false;
7995 }
7996 }
7997 }
1107f3ae 7998
8644a673
IR
7999 switch (STMT_VINFO_DEF_TYPE (stmt_info))
8000 {
8001 case vect_internal_def:
8002 break;
ebfd146a 8003
8644a673 8004 case vect_reduction_def:
7c5222ff 8005 case vect_nested_cycle:
14a61437
RB
8006 gcc_assert (!bb_vinfo
8007 && (relevance == vect_used_in_outer
8008 || relevance == vect_used_in_outer_by_reduction
8009 || relevance == vect_used_by_reduction
8010 || relevance == vect_unused_in_scope));
8644a673
IR
8011 break;
8012
8013 case vect_induction_def:
8014 case vect_constant_def:
8015 case vect_external_def:
8016 case vect_unknown_def_type:
8017 default:
8018 gcc_unreachable ();
8019 }
ebfd146a 8020
a70d6342
IR
8021 if (bb_vinfo)
8022 {
8023 gcc_assert (PURE_SLP_STMT (stmt_info));
8024
b690cc0f 8025 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
73fbfcad 8026 if (dump_enabled_p ())
a70d6342 8027 {
78c60e3d
SS
8028 dump_printf_loc (MSG_NOTE, vect_location,
8029 "get vectype for scalar type: ");
8030 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
e645e942 8031 dump_printf (MSG_NOTE, "\n");
a70d6342
IR
8032 }
8033
8034 vectype = get_vectype_for_scalar_type (scalar_type);
8035 if (!vectype)
8036 {
73fbfcad 8037 if (dump_enabled_p ())
a70d6342 8038 {
78c60e3d
SS
8039 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8040 "not SLPed: unsupported data-type ");
8041 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
8042 scalar_type);
e645e942 8043 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
a70d6342
IR
8044 }
8045 return false;
8046 }
8047
73fbfcad 8048 if (dump_enabled_p ())
a70d6342 8049 {
78c60e3d
SS
8050 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
8051 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
e645e942 8052 dump_printf (MSG_NOTE, "\n");
a70d6342
IR
8053 }
8054
8055 STMT_VINFO_VECTYPE (stmt_info) = vectype;
8056 }
8057
8644a673 8058 if (STMT_VINFO_RELEVANT_P (stmt_info))
ebfd146a 8059 {
8644a673 8060 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))));
0136f8f0
AH
8061 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
8062 || (is_gimple_call (stmt)
8063 && gimple_call_lhs (stmt) == NULL_TREE));
8644a673 8064 *need_to_vectorize = true;
ebfd146a
IR
8065 }
8066
b1af7da6
RB
8067 if (PURE_SLP_STMT (stmt_info) && !node)
8068 {
8069 dump_printf_loc (MSG_NOTE, vect_location,
8070 "handled only by SLP analysis\n");
8071 return true;
8072 }
8073
8074 ok = true;
8075 if (!bb_vinfo
8076 && (STMT_VINFO_RELEVANT_P (stmt_info)
8077 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
8078 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
8079 || vectorizable_conversion (stmt, NULL, NULL, node)
8080 || vectorizable_shift (stmt, NULL, NULL, node)
8081 || vectorizable_operation (stmt, NULL, NULL, node)
8082 || vectorizable_assignment (stmt, NULL, NULL, node)
8083 || vectorizable_load (stmt, NULL, NULL, node, NULL)
8084 || vectorizable_call (stmt, NULL, NULL, node)
8085 || vectorizable_store (stmt, NULL, NULL, node)
8086 || vectorizable_reduction (stmt, NULL, NULL, node)
42fd8198
IE
8087 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
8088 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
b1af7da6
RB
8089 else
8090 {
8091 if (bb_vinfo)
8092 ok = (vectorizable_simd_clone_call (stmt, NULL, NULL, node)
8093 || vectorizable_conversion (stmt, NULL, NULL, node)
8094 || vectorizable_shift (stmt, NULL, NULL, node)
8095 || vectorizable_operation (stmt, NULL, NULL, node)
8096 || vectorizable_assignment (stmt, NULL, NULL, node)
8097 || vectorizable_load (stmt, NULL, NULL, node, NULL)
8098 || vectorizable_call (stmt, NULL, NULL, node)
8099 || vectorizable_store (stmt, NULL, NULL, node)
42fd8198
IE
8100 || vectorizable_condition (stmt, NULL, NULL, NULL, 0, node)
8101 || vectorizable_comparison (stmt, NULL, NULL, NULL, node));
b1af7da6 8102 }
8644a673
IR
8103
8104 if (!ok)
ebfd146a 8105 {
73fbfcad 8106 if (dump_enabled_p ())
8644a673 8107 {
78c60e3d
SS
8108 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8109 "not vectorized: relevant stmt not ");
8110 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
8111 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8644a673 8112 }
b8698a0f 8113
ebfd146a
IR
8114 return false;
8115 }
8116
a70d6342
IR
8117 if (bb_vinfo)
8118 return true;
8119
8644a673
IR
8120 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
8121 need extra handling, except for vectorizable reductions. */
8122 if (STMT_VINFO_LIVE_P (stmt_info)
8123 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
8124 ok = vectorizable_live_operation (stmt, NULL, NULL);
ebfd146a 8125
8644a673 8126 if (!ok)
ebfd146a 8127 {
73fbfcad 8128 if (dump_enabled_p ())
8644a673 8129 {
78c60e3d
SS
8130 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8131 "not vectorized: live stmt not ");
8132 dump_printf (MSG_MISSED_OPTIMIZATION, "supported: ");
8133 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
8644a673 8134 }
b8698a0f 8135
8644a673 8136 return false;
ebfd146a
IR
8137 }
8138
ebfd146a
IR
8139 return true;
8140}
8141
8142
8143/* Function vect_transform_stmt.
8144
8145 Create a vectorized stmt to replace STMT, and insert it at BSI. */
8146
8147bool
355fe088 8148vect_transform_stmt (gimple *stmt, gimple_stmt_iterator *gsi,
0d0293ac 8149 bool *grouped_store, slp_tree slp_node,
ebfd146a
IR
8150 slp_instance slp_node_instance)
8151{
8152 bool is_store = false;
355fe088 8153 gimple *vec_stmt = NULL;
ebfd146a 8154 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
ebfd146a 8155 bool done;
ebfd146a 8156
fce57248 8157 gcc_assert (slp_node || !PURE_SLP_STMT (stmt_info));
355fe088 8158 gimple *old_vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
225ce44b 8159
ebfd146a
IR
8160 switch (STMT_VINFO_TYPE (stmt_info))
8161 {
8162 case type_demotion_vec_info_type:
ebfd146a 8163 case type_promotion_vec_info_type:
ebfd146a
IR
8164 case type_conversion_vec_info_type:
8165 done = vectorizable_conversion (stmt, gsi, &vec_stmt, slp_node);
8166 gcc_assert (done);
8167 break;
8168
8169 case induc_vec_info_type:
8170 gcc_assert (!slp_node);
8171 done = vectorizable_induction (stmt, gsi, &vec_stmt);
8172 gcc_assert (done);
8173 break;
8174
9dc3f7de
IR
8175 case shift_vec_info_type:
8176 done = vectorizable_shift (stmt, gsi, &vec_stmt, slp_node);
8177 gcc_assert (done);
8178 break;
8179
ebfd146a
IR
8180 case op_vec_info_type:
8181 done = vectorizable_operation (stmt, gsi, &vec_stmt, slp_node);
8182 gcc_assert (done);
8183 break;
8184
8185 case assignment_vec_info_type:
8186 done = vectorizable_assignment (stmt, gsi, &vec_stmt, slp_node);
8187 gcc_assert (done);
8188 break;
8189
8190 case load_vec_info_type:
b8698a0f 8191 done = vectorizable_load (stmt, gsi, &vec_stmt, slp_node,
ebfd146a
IR
8192 slp_node_instance);
8193 gcc_assert (done);
8194 break;
8195
8196 case store_vec_info_type:
8197 done = vectorizable_store (stmt, gsi, &vec_stmt, slp_node);
8198 gcc_assert (done);
0d0293ac 8199 if (STMT_VINFO_GROUPED_ACCESS (stmt_info) && !slp_node)
ebfd146a
IR
8200 {
8201 /* In case of interleaving, the whole chain is vectorized when the
ff802fa1 8202 last store in the chain is reached. Store stmts before the last
ebfd146a
IR
8203 one are skipped, and there vec_stmt_info shouldn't be freed
8204 meanwhile. */
0d0293ac 8205 *grouped_store = true;
ebfd146a
IR
8206 if (STMT_VINFO_VEC_STMT (stmt_info))
8207 is_store = true;
8208 }
8209 else
8210 is_store = true;
8211 break;
8212
8213 case condition_vec_info_type:
f7e531cf 8214 done = vectorizable_condition (stmt, gsi, &vec_stmt, NULL, 0, slp_node);
ebfd146a
IR
8215 gcc_assert (done);
8216 break;
8217
42fd8198
IE
8218 case comparison_vec_info_type:
8219 done = vectorizable_comparison (stmt, gsi, &vec_stmt, NULL, slp_node);
8220 gcc_assert (done);
8221 break;
8222
ebfd146a 8223 case call_vec_info_type:
190c2236 8224 done = vectorizable_call (stmt, gsi, &vec_stmt, slp_node);
039d9ea1 8225 stmt = gsi_stmt (*gsi);
5ce9450f
JJ
8226 if (is_gimple_call (stmt)
8227 && gimple_call_internal_p (stmt)
8228 && gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
8229 is_store = true;
ebfd146a
IR
8230 break;
8231
0136f8f0
AH
8232 case call_simd_clone_vec_info_type:
8233 done = vectorizable_simd_clone_call (stmt, gsi, &vec_stmt, slp_node);
8234 stmt = gsi_stmt (*gsi);
8235 break;
8236
ebfd146a 8237 case reduc_vec_info_type:
b5aeb3bb 8238 done = vectorizable_reduction (stmt, gsi, &vec_stmt, slp_node);
ebfd146a
IR
8239 gcc_assert (done);
8240 break;
8241
8242 default:
8243 if (!STMT_VINFO_LIVE_P (stmt_info))
8244 {
73fbfcad 8245 if (dump_enabled_p ())
78c60e3d 8246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 8247 "stmt not supported.\n");
ebfd146a
IR
8248 gcc_unreachable ();
8249 }
8250 }
8251
225ce44b
RB
8252 /* Verify SLP vectorization doesn't mess with STMT_VINFO_VEC_STMT.
8253 This would break hybrid SLP vectorization. */
8254 if (slp_node)
d90f8440
RB
8255 gcc_assert (!vec_stmt
8256 && STMT_VINFO_VEC_STMT (stmt_info) == old_vec_stmt);
225ce44b 8257
ebfd146a
IR
8258 /* Handle inner-loop stmts whose DEF is used in the loop-nest that
8259 is being vectorized, but outside the immediately enclosing loop. */
8260 if (vec_stmt
a70d6342
IR
8261 && STMT_VINFO_LOOP_VINFO (stmt_info)
8262 && nested_in_vect_loop_p (LOOP_VINFO_LOOP (
8263 STMT_VINFO_LOOP_VINFO (stmt_info)), stmt)
ebfd146a
IR
8264 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
8265 && (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_outer
b8698a0f 8266 || STMT_VINFO_RELEVANT (stmt_info) ==
a70d6342 8267 vect_used_in_outer_by_reduction))
ebfd146a 8268 {
a70d6342
IR
8269 struct loop *innerloop = LOOP_VINFO_LOOP (
8270 STMT_VINFO_LOOP_VINFO (stmt_info))->inner;
ebfd146a
IR
8271 imm_use_iterator imm_iter;
8272 use_operand_p use_p;
8273 tree scalar_dest;
355fe088 8274 gimple *exit_phi;
ebfd146a 8275
73fbfcad 8276 if (dump_enabled_p ())
78c60e3d 8277 dump_printf_loc (MSG_NOTE, vect_location,
e645e942 8278 "Record the vdef for outer-loop vectorization.\n");
ebfd146a
IR
8279
8280 /* Find the relevant loop-exit phi-node, and reord the vec_stmt there
8281 (to be used when vectorizing outer-loop stmts that use the DEF of
8282 STMT). */
8283 if (gimple_code (stmt) == GIMPLE_PHI)
8284 scalar_dest = PHI_RESULT (stmt);
8285 else
8286 scalar_dest = gimple_assign_lhs (stmt);
8287
8288 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
8289 {
8290 if (!flow_bb_inside_loop_p (innerloop, gimple_bb (USE_STMT (use_p))))
8291 {
8292 exit_phi = USE_STMT (use_p);
8293 STMT_VINFO_VEC_STMT (vinfo_for_stmt (exit_phi)) = vec_stmt;
8294 }
8295 }
8296 }
8297
8298 /* Handle stmts whose DEF is used outside the loop-nest that is
8299 being vectorized. */
8300 if (STMT_VINFO_LIVE_P (stmt_info)
8301 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type)
8302 {
8303 done = vectorizable_live_operation (stmt, gsi, &vec_stmt);
8304 gcc_assert (done);
8305 }
8306
8307 if (vec_stmt)
83197f37 8308 STMT_VINFO_VEC_STMT (stmt_info) = vec_stmt;
ebfd146a 8309
b8698a0f 8310 return is_store;
ebfd146a
IR
8311}
8312
8313
b8698a0f 8314/* Remove a group of stores (for SLP or interleaving), free their
ebfd146a
IR
8315 stmt_vec_info. */
8316
8317void
355fe088 8318vect_remove_stores (gimple *first_stmt)
ebfd146a 8319{
355fe088
TS
8320 gimple *next = first_stmt;
8321 gimple *tmp;
ebfd146a
IR
8322 gimple_stmt_iterator next_si;
8323
8324 while (next)
8325 {
78048b1c
JJ
8326 stmt_vec_info stmt_info = vinfo_for_stmt (next);
8327
8328 tmp = GROUP_NEXT_ELEMENT (stmt_info);
8329 if (is_pattern_stmt_p (stmt_info))
8330 next = STMT_VINFO_RELATED_STMT (stmt_info);
ebfd146a
IR
8331 /* Free the attached stmt_vec_info and remove the stmt. */
8332 next_si = gsi_for_stmt (next);
3d3f2249 8333 unlink_stmt_vdef (next);
ebfd146a 8334 gsi_remove (&next_si, true);
3d3f2249 8335 release_defs (next);
ebfd146a
IR
8336 free_stmt_vec_info (next);
8337 next = tmp;
8338 }
8339}
8340
8341
8342/* Function new_stmt_vec_info.
8343
8344 Create and initialize a new stmt_vec_info struct for STMT. */
8345
8346stmt_vec_info
310213d4 8347new_stmt_vec_info (gimple *stmt, vec_info *vinfo)
ebfd146a
IR
8348{
8349 stmt_vec_info res;
8350 res = (stmt_vec_info) xcalloc (1, sizeof (struct _stmt_vec_info));
8351
8352 STMT_VINFO_TYPE (res) = undef_vec_info_type;
8353 STMT_VINFO_STMT (res) = stmt;
310213d4 8354 res->vinfo = vinfo;
8644a673 8355 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
ebfd146a
IR
8356 STMT_VINFO_LIVE_P (res) = false;
8357 STMT_VINFO_VECTYPE (res) = NULL;
8358 STMT_VINFO_VEC_STMT (res) = NULL;
4b5caab7 8359 STMT_VINFO_VECTORIZABLE (res) = true;
ebfd146a
IR
8360 STMT_VINFO_IN_PATTERN_P (res) = false;
8361 STMT_VINFO_RELATED_STMT (res) = NULL;
363477c0 8362 STMT_VINFO_PATTERN_DEF_SEQ (res) = NULL;
ebfd146a 8363 STMT_VINFO_DATA_REF (res) = NULL;
af29617a 8364 STMT_VINFO_VEC_REDUCTION_TYPE (res) = TREE_CODE_REDUCTION;
ebfd146a
IR
8365
8366 STMT_VINFO_DR_BASE_ADDRESS (res) = NULL;
8367 STMT_VINFO_DR_OFFSET (res) = NULL;
8368 STMT_VINFO_DR_INIT (res) = NULL;
8369 STMT_VINFO_DR_STEP (res) = NULL;
8370 STMT_VINFO_DR_ALIGNED_TO (res) = NULL;
8371
8372 if (gimple_code (stmt) == GIMPLE_PHI
8373 && is_loop_header_bb_p (gimple_bb (stmt)))
8374 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
8375 else
8644a673
IR
8376 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
8377
9771b263 8378 STMT_VINFO_SAME_ALIGN_REFS (res).create (0);
32e8bb8e 8379 STMT_SLP_TYPE (res) = loop_vect;
78810bd3
RB
8380 STMT_VINFO_NUM_SLP_USES (res) = 0;
8381
e14c1050
IR
8382 GROUP_FIRST_ELEMENT (res) = NULL;
8383 GROUP_NEXT_ELEMENT (res) = NULL;
8384 GROUP_SIZE (res) = 0;
8385 GROUP_STORE_COUNT (res) = 0;
8386 GROUP_GAP (res) = 0;
8387 GROUP_SAME_DR_STMT (res) = NULL;
ebfd146a
IR
8388
8389 return res;
8390}
8391
8392
8393/* Create a hash table for stmt_vec_info. */
8394
8395void
8396init_stmt_vec_info_vec (void)
8397{
9771b263
DN
8398 gcc_assert (!stmt_vec_info_vec.exists ());
8399 stmt_vec_info_vec.create (50);
ebfd146a
IR
8400}
8401
8402
8403/* Free hash table for stmt_vec_info. */
8404
8405void
8406free_stmt_vec_info_vec (void)
8407{
93675444 8408 unsigned int i;
3161455c 8409 stmt_vec_info info;
93675444
JJ
8410 FOR_EACH_VEC_ELT (stmt_vec_info_vec, i, info)
8411 if (info != NULL)
3161455c 8412 free_stmt_vec_info (STMT_VINFO_STMT (info));
9771b263
DN
8413 gcc_assert (stmt_vec_info_vec.exists ());
8414 stmt_vec_info_vec.release ();
ebfd146a
IR
8415}
8416
8417
8418/* Free stmt vectorization related info. */
8419
8420void
355fe088 8421free_stmt_vec_info (gimple *stmt)
ebfd146a
IR
8422{
8423 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8424
8425 if (!stmt_info)
8426 return;
8427
78048b1c
JJ
8428 /* Check if this statement has a related "pattern stmt"
8429 (introduced by the vectorizer during the pattern recognition
8430 pass). Free pattern's stmt_vec_info and def stmt's stmt_vec_info
8431 too. */
8432 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
8433 {
8434 stmt_vec_info patt_info
8435 = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
8436 if (patt_info)
8437 {
363477c0 8438 gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (patt_info);
355fe088 8439 gimple *patt_stmt = STMT_VINFO_STMT (patt_info);
f0281fde
RB
8440 gimple_set_bb (patt_stmt, NULL);
8441 tree lhs = gimple_get_lhs (patt_stmt);
e6f5c25d 8442 if (lhs && TREE_CODE (lhs) == SSA_NAME)
f0281fde 8443 release_ssa_name (lhs);
363477c0
JJ
8444 if (seq)
8445 {
8446 gimple_stmt_iterator si;
8447 for (si = gsi_start (seq); !gsi_end_p (si); gsi_next (&si))
f0281fde 8448 {
355fe088 8449 gimple *seq_stmt = gsi_stmt (si);
f0281fde 8450 gimple_set_bb (seq_stmt, NULL);
7532abf2 8451 lhs = gimple_get_lhs (seq_stmt);
e6f5c25d 8452 if (lhs && TREE_CODE (lhs) == SSA_NAME)
f0281fde
RB
8453 release_ssa_name (lhs);
8454 free_stmt_vec_info (seq_stmt);
8455 }
363477c0 8456 }
f0281fde 8457 free_stmt_vec_info (patt_stmt);
78048b1c
JJ
8458 }
8459 }
8460
9771b263 8461 STMT_VINFO_SAME_ALIGN_REFS (stmt_info).release ();
6c9e85fb 8462 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
ebfd146a
IR
8463 set_vinfo_for_stmt (stmt, NULL);
8464 free (stmt_info);
8465}
8466
8467
bb67d9c7 8468/* Function get_vectype_for_scalar_type_and_size.
ebfd146a 8469
bb67d9c7 8470 Returns the vector type corresponding to SCALAR_TYPE and SIZE as supported
ebfd146a
IR
8471 by the target. */
8472
bb67d9c7
RG
8473static tree
8474get_vectype_for_scalar_type_and_size (tree scalar_type, unsigned size)
ebfd146a 8475{
ef4bddc2
RS
8476 machine_mode inner_mode = TYPE_MODE (scalar_type);
8477 machine_mode simd_mode;
2f816591 8478 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
ebfd146a
IR
8479 int nunits;
8480 tree vectype;
8481
cc4b5170 8482 if (nbytes == 0)
ebfd146a
IR
8483 return NULL_TREE;
8484
48f2e373
RB
8485 if (GET_MODE_CLASS (inner_mode) != MODE_INT
8486 && GET_MODE_CLASS (inner_mode) != MODE_FLOAT)
8487 return NULL_TREE;
8488
7b7b1813
RG
8489 /* For vector types of elements whose mode precision doesn't
8490 match their types precision we use a element type of mode
8491 precision. The vectorization routines will have to make sure
48f2e373
RB
8492 they support the proper result truncation/extension.
8493 We also make sure to build vector types with INTEGER_TYPE
8494 component type only. */
6d7971b8 8495 if (INTEGRAL_TYPE_P (scalar_type)
48f2e373
RB
8496 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
8497 || TREE_CODE (scalar_type) != INTEGER_TYPE))
7b7b1813
RG
8498 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
8499 TYPE_UNSIGNED (scalar_type));
6d7971b8 8500
ccbf5bb4
RG
8501 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
8502 When the component mode passes the above test simply use a type
8503 corresponding to that mode. The theory is that any use that
8504 would cause problems with this will disable vectorization anyway. */
dfc2e2ac 8505 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
e67f39f7 8506 && !INTEGRAL_TYPE_P (scalar_type))
60b95d28
RB
8507 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
8508
8509 /* We can't build a vector type of elements with alignment bigger than
8510 their size. */
dfc2e2ac 8511 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
aca43c6c
JJ
8512 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
8513 TYPE_UNSIGNED (scalar_type));
ccbf5bb4 8514
dfc2e2ac
RB
8515 /* If we felt back to using the mode fail if there was
8516 no scalar type for it. */
8517 if (scalar_type == NULL_TREE)
8518 return NULL_TREE;
8519
bb67d9c7
RG
8520 /* If no size was supplied use the mode the target prefers. Otherwise
8521 lookup a vector mode of the specified size. */
8522 if (size == 0)
8523 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
8524 else
8525 simd_mode = mode_for_vector (inner_mode, size / nbytes);
cc4b5170
RG
8526 nunits = GET_MODE_SIZE (simd_mode) / nbytes;
8527 if (nunits <= 1)
8528 return NULL_TREE;
ebfd146a
IR
8529
8530 vectype = build_vector_type (scalar_type, nunits);
ebfd146a
IR
8531
8532 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8533 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
451dabda 8534 return NULL_TREE;
ebfd146a
IR
8535
8536 return vectype;
8537}
8538
bb67d9c7
RG
8539unsigned int current_vector_size;
8540
8541/* Function get_vectype_for_scalar_type.
8542
8543 Returns the vector type corresponding to SCALAR_TYPE as supported
8544 by the target. */
8545
8546tree
8547get_vectype_for_scalar_type (tree scalar_type)
8548{
8549 tree vectype;
8550 vectype = get_vectype_for_scalar_type_and_size (scalar_type,
8551 current_vector_size);
8552 if (vectype
8553 && current_vector_size == 0)
8554 current_vector_size = GET_MODE_SIZE (TYPE_MODE (vectype));
8555 return vectype;
8556}
8557
42fd8198
IE
8558/* Function get_mask_type_for_scalar_type.
8559
8560 Returns the mask type corresponding to a result of comparison
8561 of vectors of specified SCALAR_TYPE as supported by target. */
8562
8563tree
8564get_mask_type_for_scalar_type (tree scalar_type)
8565{
8566 tree vectype = get_vectype_for_scalar_type (scalar_type);
8567
8568 if (!vectype)
8569 return NULL;
8570
8571 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype),
8572 current_vector_size);
8573}
8574
b690cc0f
RG
8575/* Function get_same_sized_vectype
8576
8577 Returns a vector type corresponding to SCALAR_TYPE of size
8578 VECTOR_TYPE if supported by the target. */
8579
8580tree
bb67d9c7 8581get_same_sized_vectype (tree scalar_type, tree vector_type)
b690cc0f 8582{
9f47c7e5
IE
8583 if (TREE_CODE (scalar_type) == BOOLEAN_TYPE)
8584 return build_same_sized_truth_vector_type (vector_type);
8585
bb67d9c7
RG
8586 return get_vectype_for_scalar_type_and_size
8587 (scalar_type, GET_MODE_SIZE (TYPE_MODE (vector_type)));
b690cc0f
RG
8588}
8589
ebfd146a
IR
8590/* Function vect_is_simple_use.
8591
8592 Input:
81c40241
RB
8593 VINFO - the vect info of the loop or basic block that is being vectorized.
8594 OPERAND - operand in the loop or bb.
8595 Output:
8596 DEF_STMT - the defining stmt in case OPERAND is an SSA_NAME.
8597 DT - the type of definition
ebfd146a
IR
8598
8599 Returns whether a stmt with OPERAND can be vectorized.
b8698a0f 8600 For loops, supportable operands are constants, loop invariants, and operands
ff802fa1 8601 that are defined by the current iteration of the loop. Unsupportable
b8698a0f 8602 operands are those that are defined by a previous iteration of the loop (as
a70d6342
IR
8603 is the case in reduction/induction computations).
8604 For basic blocks, supportable operands are constants and bb invariants.
8605 For now, operands defined outside the basic block are not supported. */
ebfd146a
IR
8606
8607bool
81c40241
RB
8608vect_is_simple_use (tree operand, vec_info *vinfo,
8609 gimple **def_stmt, enum vect_def_type *dt)
b8698a0f 8610{
ebfd146a 8611 *def_stmt = NULL;
3fc356dc 8612 *dt = vect_unknown_def_type;
b8698a0f 8613
73fbfcad 8614 if (dump_enabled_p ())
ebfd146a 8615 {
78c60e3d
SS
8616 dump_printf_loc (MSG_NOTE, vect_location,
8617 "vect_is_simple_use: operand ");
8618 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
e645e942 8619 dump_printf (MSG_NOTE, "\n");
ebfd146a 8620 }
b8698a0f 8621
b758f602 8622 if (CONSTANT_CLASS_P (operand))
ebfd146a
IR
8623 {
8624 *dt = vect_constant_def;
8625 return true;
8626 }
b8698a0f 8627
ebfd146a
IR
8628 if (is_gimple_min_invariant (operand))
8629 {
8644a673 8630 *dt = vect_external_def;
ebfd146a
IR
8631 return true;
8632 }
8633
ebfd146a
IR
8634 if (TREE_CODE (operand) != SSA_NAME)
8635 {
73fbfcad 8636 if (dump_enabled_p ())
af29617a
AH
8637 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8638 "not ssa-name.\n");
ebfd146a
IR
8639 return false;
8640 }
b8698a0f 8641
3fc356dc 8642 if (SSA_NAME_IS_DEFAULT_DEF (operand))
ebfd146a 8643 {
3fc356dc
RB
8644 *dt = vect_external_def;
8645 return true;
ebfd146a
IR
8646 }
8647
3fc356dc 8648 *def_stmt = SSA_NAME_DEF_STMT (operand);
73fbfcad 8649 if (dump_enabled_p ())
ebfd146a 8650 {
78c60e3d
SS
8651 dump_printf_loc (MSG_NOTE, vect_location, "def_stmt: ");
8652 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, *def_stmt, 0);
ebfd146a
IR
8653 }
8654
61d371eb 8655 if (! vect_stmt_in_region_p (vinfo, *def_stmt))
8644a673 8656 *dt = vect_external_def;
ebfd146a
IR
8657 else
8658 {
3fc356dc 8659 stmt_vec_info stmt_vinfo = vinfo_for_stmt (*def_stmt);
603cca93 8660 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
ebfd146a
IR
8661 }
8662
2e8ab70c
RB
8663 if (dump_enabled_p ())
8664 {
8665 dump_printf_loc (MSG_NOTE, vect_location, "type of def: ");
8666 switch (*dt)
8667 {
8668 case vect_uninitialized_def:
8669 dump_printf (MSG_NOTE, "uninitialized\n");
8670 break;
8671 case vect_constant_def:
8672 dump_printf (MSG_NOTE, "constant\n");
8673 break;
8674 case vect_external_def:
8675 dump_printf (MSG_NOTE, "external\n");
8676 break;
8677 case vect_internal_def:
8678 dump_printf (MSG_NOTE, "internal\n");
8679 break;
8680 case vect_induction_def:
8681 dump_printf (MSG_NOTE, "induction\n");
8682 break;
8683 case vect_reduction_def:
8684 dump_printf (MSG_NOTE, "reduction\n");
8685 break;
8686 case vect_double_reduction_def:
8687 dump_printf (MSG_NOTE, "double reduction\n");
8688 break;
8689 case vect_nested_cycle:
8690 dump_printf (MSG_NOTE, "nested cycle\n");
8691 break;
8692 case vect_unknown_def_type:
8693 dump_printf (MSG_NOTE, "unknown\n");
8694 break;
8695 }
8696 }
8697
81c40241 8698 if (*dt == vect_unknown_def_type)
ebfd146a 8699 {
73fbfcad 8700 if (dump_enabled_p ())
78c60e3d 8701 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 8702 "Unsupported pattern.\n");
ebfd146a
IR
8703 return false;
8704 }
8705
ebfd146a
IR
8706 switch (gimple_code (*def_stmt))
8707 {
8708 case GIMPLE_PHI:
ebfd146a 8709 case GIMPLE_ASSIGN:
ebfd146a 8710 case GIMPLE_CALL:
81c40241 8711 break;
ebfd146a 8712 default:
73fbfcad 8713 if (dump_enabled_p ())
78c60e3d 8714 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
e645e942 8715 "unsupported defining stmt:\n");
ebfd146a
IR
8716 return false;
8717 }
8718
8719 return true;
8720}
8721
81c40241 8722/* Function vect_is_simple_use.
b690cc0f 8723
81c40241 8724 Same as vect_is_simple_use but also determines the vector operand
b690cc0f
RG
8725 type of OPERAND and stores it to *VECTYPE. If the definition of
8726 OPERAND is vect_uninitialized_def, vect_constant_def or
8727 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
8728 is responsible to compute the best suited vector type for the
8729 scalar operand. */
8730
8731bool
81c40241
RB
8732vect_is_simple_use (tree operand, vec_info *vinfo,
8733 gimple **def_stmt, enum vect_def_type *dt, tree *vectype)
b690cc0f 8734{
81c40241 8735 if (!vect_is_simple_use (operand, vinfo, def_stmt, dt))
b690cc0f
RG
8736 return false;
8737
8738 /* Now get a vector type if the def is internal, otherwise supply
8739 NULL_TREE and leave it up to the caller to figure out a proper
8740 type for the use stmt. */
8741 if (*dt == vect_internal_def
8742 || *dt == vect_induction_def
8743 || *dt == vect_reduction_def
8744 || *dt == vect_double_reduction_def
8745 || *dt == vect_nested_cycle)
8746 {
8747 stmt_vec_info stmt_info = vinfo_for_stmt (*def_stmt);
83197f37
IR
8748
8749 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
8750 && !STMT_VINFO_RELEVANT (stmt_info)
8751 && !STMT_VINFO_LIVE_P (stmt_info))
b690cc0f 8752 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
83197f37 8753
b690cc0f
RG
8754 *vectype = STMT_VINFO_VECTYPE (stmt_info);
8755 gcc_assert (*vectype != NULL_TREE);
8756 }
8757 else if (*dt == vect_uninitialized_def
8758 || *dt == vect_constant_def
8759 || *dt == vect_external_def)
8760 *vectype = NULL_TREE;
8761 else
8762 gcc_unreachable ();
8763
8764 return true;
8765}
8766
ebfd146a
IR
8767
8768/* Function supportable_widening_operation
8769
b8698a0f
L
8770 Check whether an operation represented by the code CODE is a
8771 widening operation that is supported by the target platform in
b690cc0f
RG
8772 vector form (i.e., when operating on arguments of type VECTYPE_IN
8773 producing a result of type VECTYPE_OUT).
b8698a0f 8774
ebfd146a
IR
8775 Widening operations we currently support are NOP (CONVERT), FLOAT
8776 and WIDEN_MULT. This function checks if these operations are supported
8777 by the target platform either directly (via vector tree-codes), or via
8778 target builtins.
8779
8780 Output:
b8698a0f
L
8781 - CODE1 and CODE2 are codes of vector operations to be used when
8782 vectorizing the operation, if available.
ebfd146a
IR
8783 - MULTI_STEP_CVT determines the number of required intermediate steps in
8784 case of multi-step conversion (like char->short->int - in that case
8785 MULTI_STEP_CVT will be 1).
b8698a0f
L
8786 - INTERM_TYPES contains the intermediate type required to perform the
8787 widening operation (short in the above example). */
ebfd146a
IR
8788
8789bool
355fe088 8790supportable_widening_operation (enum tree_code code, gimple *stmt,
b690cc0f 8791 tree vectype_out, tree vectype_in,
ebfd146a
IR
8792 enum tree_code *code1, enum tree_code *code2,
8793 int *multi_step_cvt,
9771b263 8794 vec<tree> *interm_types)
ebfd146a
IR
8795{
8796 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
8797 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_info);
4ef69dfc 8798 struct loop *vect_loop = NULL;
ef4bddc2 8799 machine_mode vec_mode;
81f40b79 8800 enum insn_code icode1, icode2;
ebfd146a 8801 optab optab1, optab2;
b690cc0f
RG
8802 tree vectype = vectype_in;
8803 tree wide_vectype = vectype_out;
ebfd146a 8804 enum tree_code c1, c2;
4a00c761
JJ
8805 int i;
8806 tree prev_type, intermediate_type;
ef4bddc2 8807 machine_mode intermediate_mode, prev_mode;
4a00c761 8808 optab optab3, optab4;
ebfd146a 8809
4a00c761 8810 *multi_step_cvt = 0;
4ef69dfc
IR
8811 if (loop_info)
8812 vect_loop = LOOP_VINFO_LOOP (loop_info);
8813
ebfd146a
IR
8814 switch (code)
8815 {
8816 case WIDEN_MULT_EXPR:
6ae6116f
RH
8817 /* The result of a vectorized widening operation usually requires
8818 two vectors (because the widened results do not fit into one vector).
8819 The generated vector results would normally be expected to be
8820 generated in the same order as in the original scalar computation,
8821 i.e. if 8 results are generated in each vector iteration, they are
8822 to be organized as follows:
8823 vect1: [res1,res2,res3,res4],
8824 vect2: [res5,res6,res7,res8].
8825
8826 However, in the special case that the result of the widening
8827 operation is used in a reduction computation only, the order doesn't
8828 matter (because when vectorizing a reduction we change the order of
8829 the computation). Some targets can take advantage of this and
8830 generate more efficient code. For example, targets like Altivec,
8831 that support widen_mult using a sequence of {mult_even,mult_odd}
8832 generate the following vectors:
8833 vect1: [res1,res3,res5,res7],
8834 vect2: [res2,res4,res6,res8].
8835
8836 When vectorizing outer-loops, we execute the inner-loop sequentially
8837 (each vectorized inner-loop iteration contributes to VF outer-loop
8838 iterations in parallel). We therefore don't allow to change the
8839 order of the computation in the inner-loop during outer-loop
8840 vectorization. */
8841 /* TODO: Another case in which order doesn't *really* matter is when we
8842 widen and then contract again, e.g. (short)((int)x * y >> 8).
8843 Normally, pack_trunc performs an even/odd permute, whereas the
8844 repack from an even/odd expansion would be an interleave, which
8845 would be significantly simpler for e.g. AVX2. */
8846 /* In any case, in order to avoid duplicating the code below, recurse
8847 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
8848 are properly set up for the caller. If we fail, we'll continue with
8849 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
8850 if (vect_loop
8851 && STMT_VINFO_RELEVANT (stmt_info) == vect_used_by_reduction
8852 && !nested_in_vect_loop_p (vect_loop, stmt)
8853 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
8854 stmt, vectype_out, vectype_in,
a86ec597
RH
8855 code1, code2, multi_step_cvt,
8856 interm_types))
ebc047a2
CH
8857 {
8858 /* Elements in a vector with vect_used_by_reduction property cannot
8859 be reordered if the use chain with this property does not have the
8860 same operation. One such an example is s += a * b, where elements
8861 in a and b cannot be reordered. Here we check if the vector defined
8862 by STMT is only directly used in the reduction statement. */
8863 tree lhs = gimple_assign_lhs (stmt);
8864 use_operand_p dummy;
355fe088 8865 gimple *use_stmt;
ebc047a2
CH
8866 stmt_vec_info use_stmt_info = NULL;
8867 if (single_imm_use (lhs, &dummy, &use_stmt)
8868 && (use_stmt_info = vinfo_for_stmt (use_stmt))
8869 && STMT_VINFO_DEF_TYPE (use_stmt_info) == vect_reduction_def)
8870 return true;
8871 }
4a00c761
JJ
8872 c1 = VEC_WIDEN_MULT_LO_EXPR;
8873 c2 = VEC_WIDEN_MULT_HI_EXPR;
ebfd146a
IR
8874 break;
8875
81c40241
RB
8876 case DOT_PROD_EXPR:
8877 c1 = DOT_PROD_EXPR;
8878 c2 = DOT_PROD_EXPR;
8879 break;
8880
8881 case SAD_EXPR:
8882 c1 = SAD_EXPR;
8883 c2 = SAD_EXPR;
8884 break;
8885
6ae6116f
RH
8886 case VEC_WIDEN_MULT_EVEN_EXPR:
8887 /* Support the recursion induced just above. */
8888 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
8889 c2 = VEC_WIDEN_MULT_ODD_EXPR;
8890 break;
8891
36ba4aae 8892 case WIDEN_LSHIFT_EXPR:
4a00c761
JJ
8893 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
8894 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
36ba4aae
IR
8895 break;
8896
ebfd146a 8897 CASE_CONVERT:
4a00c761
JJ
8898 c1 = VEC_UNPACK_LO_EXPR;
8899 c2 = VEC_UNPACK_HI_EXPR;
ebfd146a
IR
8900 break;
8901
8902 case FLOAT_EXPR:
4a00c761
JJ
8903 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
8904 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
ebfd146a
IR
8905 break;
8906
8907 case FIX_TRUNC_EXPR:
8908 /* ??? Not yet implemented due to missing VEC_UNPACK_FIX_TRUNC_HI_EXPR/
8909 VEC_UNPACK_FIX_TRUNC_LO_EXPR tree codes and optabs used for
8910 computing the operation. */
8911 return false;
8912
8913 default:
8914 gcc_unreachable ();
8915 }
8916
6ae6116f 8917 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
6b4db501 8918 std::swap (c1, c2);
4a00c761 8919
ebfd146a
IR
8920 if (code == FIX_TRUNC_EXPR)
8921 {
8922 /* The signedness is determined from output operand. */
b690cc0f
RG
8923 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
8924 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
ebfd146a
IR
8925 }
8926 else
8927 {
8928 optab1 = optab_for_tree_code (c1, vectype, optab_default);
8929 optab2 = optab_for_tree_code (c2, vectype, optab_default);
8930 }
8931
8932 if (!optab1 || !optab2)
8933 return false;
8934
8935 vec_mode = TYPE_MODE (vectype);
947131ba
RS
8936 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
8937 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
ebfd146a
IR
8938 return false;
8939
4a00c761
JJ
8940 *code1 = c1;
8941 *code2 = c2;
8942
8943 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
8944 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
5e8d6dff
IE
8945 /* For scalar masks we may have different boolean
8946 vector types having the same QImode. Thus we
8947 add additional check for elements number. */
8948 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
8949 || (TYPE_VECTOR_SUBPARTS (vectype) / 2
8950 == TYPE_VECTOR_SUBPARTS (wide_vectype)));
4a00c761 8951
b8698a0f 8952 /* Check if it's a multi-step conversion that can be done using intermediate
ebfd146a 8953 types. */
ebfd146a 8954
4a00c761
JJ
8955 prev_type = vectype;
8956 prev_mode = vec_mode;
b8698a0f 8957
4a00c761
JJ
8958 if (!CONVERT_EXPR_CODE_P (code))
8959 return false;
b8698a0f 8960
4a00c761
JJ
8961 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
8962 intermediate steps in promotion sequence. We try
8963 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
8964 not. */
9771b263 8965 interm_types->create (MAX_INTERM_CVT_STEPS);
4a00c761
JJ
8966 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
8967 {
8968 intermediate_mode = insn_data[icode1].operand[0].mode;
3ae0661a
IE
8969 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
8970 {
8971 intermediate_type
8972 = build_truth_vector_type (TYPE_VECTOR_SUBPARTS (prev_type) / 2,
8973 current_vector_size);
8974 if (intermediate_mode != TYPE_MODE (intermediate_type))
8975 return false;
8976 }
8977 else
8978 intermediate_type
8979 = lang_hooks.types.type_for_mode (intermediate_mode,
8980 TYPE_UNSIGNED (prev_type));
8981
4a00c761
JJ
8982 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
8983 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
8984
8985 if (!optab3 || !optab4
8986 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
8987 || insn_data[icode1].operand[0].mode != intermediate_mode
8988 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
8989 || insn_data[icode2].operand[0].mode != intermediate_mode
8990 || ((icode1 = optab_handler (optab3, intermediate_mode))
8991 == CODE_FOR_nothing)
8992 || ((icode2 = optab_handler (optab4, intermediate_mode))
8993 == CODE_FOR_nothing))
8994 break;
ebfd146a 8995
9771b263 8996 interm_types->quick_push (intermediate_type);
4a00c761
JJ
8997 (*multi_step_cvt)++;
8998
8999 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
9000 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
5e8d6dff
IE
9001 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9002 || (TYPE_VECTOR_SUBPARTS (intermediate_type) / 2
9003 == TYPE_VECTOR_SUBPARTS (wide_vectype)));
4a00c761
JJ
9004
9005 prev_type = intermediate_type;
9006 prev_mode = intermediate_mode;
ebfd146a
IR
9007 }
9008
9771b263 9009 interm_types->release ();
4a00c761 9010 return false;
ebfd146a
IR
9011}
9012
9013
9014/* Function supportable_narrowing_operation
9015
b8698a0f
L
9016 Check whether an operation represented by the code CODE is a
9017 narrowing operation that is supported by the target platform in
b690cc0f
RG
9018 vector form (i.e., when operating on arguments of type VECTYPE_IN
9019 and producing a result of type VECTYPE_OUT).
b8698a0f 9020
ebfd146a 9021 Narrowing operations we currently support are NOP (CONVERT) and
ff802fa1 9022 FIX_TRUNC. This function checks if these operations are supported by
ebfd146a
IR
9023 the target platform directly via vector tree-codes.
9024
9025 Output:
b8698a0f
L
9026 - CODE1 is the code of a vector operation to be used when
9027 vectorizing the operation, if available.
ebfd146a
IR
9028 - MULTI_STEP_CVT determines the number of required intermediate steps in
9029 case of multi-step conversion (like int->short->char - in that case
9030 MULTI_STEP_CVT will be 1).
9031 - INTERM_TYPES contains the intermediate type required to perform the
b8698a0f 9032 narrowing operation (short in the above example). */
ebfd146a
IR
9033
9034bool
9035supportable_narrowing_operation (enum tree_code code,
b690cc0f 9036 tree vectype_out, tree vectype_in,
ebfd146a 9037 enum tree_code *code1, int *multi_step_cvt,
9771b263 9038 vec<tree> *interm_types)
ebfd146a 9039{
ef4bddc2 9040 machine_mode vec_mode;
ebfd146a
IR
9041 enum insn_code icode1;
9042 optab optab1, interm_optab;
b690cc0f
RG
9043 tree vectype = vectype_in;
9044 tree narrow_vectype = vectype_out;
ebfd146a 9045 enum tree_code c1;
3ae0661a 9046 tree intermediate_type, prev_type;
ef4bddc2 9047 machine_mode intermediate_mode, prev_mode;
ebfd146a 9048 int i;
4a00c761 9049 bool uns;
ebfd146a 9050
4a00c761 9051 *multi_step_cvt = 0;
ebfd146a
IR
9052 switch (code)
9053 {
9054 CASE_CONVERT:
9055 c1 = VEC_PACK_TRUNC_EXPR;
9056 break;
9057
9058 case FIX_TRUNC_EXPR:
9059 c1 = VEC_PACK_FIX_TRUNC_EXPR;
9060 break;
9061
9062 case FLOAT_EXPR:
9063 /* ??? Not yet implemented due to missing VEC_PACK_FLOAT_EXPR
9064 tree code and optabs used for computing the operation. */
9065 return false;
9066
9067 default:
9068 gcc_unreachable ();
9069 }
9070
9071 if (code == FIX_TRUNC_EXPR)
9072 /* The signedness is determined from output operand. */
b690cc0f 9073 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
ebfd146a
IR
9074 else
9075 optab1 = optab_for_tree_code (c1, vectype, optab_default);
9076
9077 if (!optab1)
9078 return false;
9079
9080 vec_mode = TYPE_MODE (vectype);
947131ba 9081 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
ebfd146a
IR
9082 return false;
9083
4a00c761
JJ
9084 *code1 = c1;
9085
9086 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
5e8d6dff
IE
9087 /* For scalar masks we may have different boolean
9088 vector types having the same QImode. Thus we
9089 add additional check for elements number. */
9090 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9091 || (TYPE_VECTOR_SUBPARTS (vectype) * 2
9092 == TYPE_VECTOR_SUBPARTS (narrow_vectype)));
4a00c761 9093
ebfd146a
IR
9094 /* Check if it's a multi-step conversion that can be done using intermediate
9095 types. */
4a00c761 9096 prev_mode = vec_mode;
3ae0661a 9097 prev_type = vectype;
4a00c761
JJ
9098 if (code == FIX_TRUNC_EXPR)
9099 uns = TYPE_UNSIGNED (vectype_out);
9100 else
9101 uns = TYPE_UNSIGNED (vectype);
9102
9103 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
9104 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
9105 costly than signed. */
9106 if (code == FIX_TRUNC_EXPR && uns)
9107 {
9108 enum insn_code icode2;
9109
9110 intermediate_type
9111 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
9112 interm_optab
9113 = optab_for_tree_code (c1, intermediate_type, optab_default);
2225b9f2 9114 if (interm_optab != unknown_optab
4a00c761
JJ
9115 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
9116 && insn_data[icode1].operand[0].mode
9117 == insn_data[icode2].operand[0].mode)
9118 {
9119 uns = false;
9120 optab1 = interm_optab;
9121 icode1 = icode2;
9122 }
9123 }
ebfd146a 9124
4a00c761
JJ
9125 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
9126 intermediate steps in promotion sequence. We try
9127 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
9771b263 9128 interm_types->create (MAX_INTERM_CVT_STEPS);
4a00c761
JJ
9129 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
9130 {
9131 intermediate_mode = insn_data[icode1].operand[0].mode;
3ae0661a
IE
9132 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
9133 {
9134 intermediate_type
9135 = build_truth_vector_type (TYPE_VECTOR_SUBPARTS (prev_type) * 2,
9136 current_vector_size);
9137 if (intermediate_mode != TYPE_MODE (intermediate_type))
9138 return false;
9139 }
9140 else
9141 intermediate_type
9142 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
4a00c761
JJ
9143 interm_optab
9144 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
9145 optab_default);
9146 if (!interm_optab
9147 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
9148 || insn_data[icode1].operand[0].mode != intermediate_mode
9149 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
9150 == CODE_FOR_nothing))
9151 break;
9152
9771b263 9153 interm_types->quick_push (intermediate_type);
4a00c761
JJ
9154 (*multi_step_cvt)++;
9155
9156 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
5e8d6dff
IE
9157 return (!VECTOR_BOOLEAN_TYPE_P (vectype)
9158 || (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2
9159 == TYPE_VECTOR_SUBPARTS (narrow_vectype)));
4a00c761
JJ
9160
9161 prev_mode = intermediate_mode;
3ae0661a 9162 prev_type = intermediate_type;
4a00c761 9163 optab1 = interm_optab;
ebfd146a
IR
9164 }
9165
9771b263 9166 interm_types->release ();
4a00c761 9167 return false;
ebfd146a 9168}