Branch data Line data Source code
1 : : /* Statement Analysis and Transformation for Vectorization
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 : : and Ira Rosen <irar@il.ibm.com>
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify it under
9 : : the terms of the GNU General Public License as published by the Free
10 : : Software Foundation; either version 3, or (at your option) any later
11 : : version.
12 : :
13 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : : for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along 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"
25 : : #include "backend.h"
26 : : #include "target.h"
27 : : #include "rtl.h"
28 : : #include "tree.h"
29 : : #include "gimple.h"
30 : : #include "ssa.h"
31 : : #include "optabs-tree.h"
32 : : #include "insn-config.h"
33 : : #include "recog.h" /* FIXME: for insn_data */
34 : : #include "cgraph.h"
35 : : #include "dumpfile.h"
36 : : #include "alias.h"
37 : : #include "fold-const.h"
38 : : #include "stor-layout.h"
39 : : #include "tree-eh.h"
40 : : #include "gimplify.h"
41 : : #include "gimple-iterator.h"
42 : : #include "gimplify-me.h"
43 : : #include "tree-cfg.h"
44 : : #include "tree-ssa-loop-manip.h"
45 : : #include "cfgloop.h"
46 : : #include "explow.h"
47 : : #include "tree-ssa-loop.h"
48 : : #include "tree-scalar-evolution.h"
49 : : #include "tree-vectorizer.h"
50 : : #include "builtins.h"
51 : : #include "internal-fn.h"
52 : : #include "tree-vector-builder.h"
53 : : #include "vec-perm-indices.h"
54 : : #include "gimple-range.h"
55 : : #include "tree-ssa-loop-niter.h"
56 : : #include "gimple-fold.h"
57 : : #include "regs.h"
58 : : #include "attribs.h"
59 : : #include "optabs-libfuncs.h"
60 : : #include "tree-dfa.h"
61 : :
62 : : /* For lang_hooks.types.type_for_mode. */
63 : : #include "langhooks.h"
64 : :
65 : : static tree vector_vector_composition_type (tree, poly_uint64, tree *,
66 : : bool = false);
67 : :
68 : : /* Return TRUE iff the given statement is in an inner loop relative to
69 : : the loop being vectorized. */
70 : : bool
71 : 3808698 : stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
72 : : {
73 : 3808698 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
74 : 3808698 : basic_block bb = gimple_bb (stmt);
75 : 3808698 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
76 : 755286 : class loop* loop;
77 : :
78 : 755286 : if (!loop_vinfo)
79 : : return false;
80 : :
81 : 755286 : loop = LOOP_VINFO_LOOP (loop_vinfo);
82 : :
83 : 755286 : return (bb->loop_father == loop->inner);
84 : : }
85 : :
86 : : /* Record the cost of a statement, either by directly informing the
87 : : target model or by saving it in a vector for later processing.
88 : : Return a preliminary estimate of the statement's cost. */
89 : :
90 : : unsigned
91 : 9444264 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
92 : : enum vect_cost_for_stmt kind,
93 : : stmt_vec_info stmt_info, slp_tree node,
94 : : tree vectype, int misalign,
95 : : enum vect_cost_model_location where)
96 : : {
97 : 9444264 : if ((kind == vector_load || kind == unaligned_load)
98 : 1322735 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
99 : : kind = vector_gather_load;
100 : 9444264 : if ((kind == vector_store || kind == unaligned_store)
101 : 900041 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
102 : 9444264 : kind = vector_scatter_store;
103 : :
104 : 9444264 : stmt_info_for_cost si
105 : 9444264 : = { count, kind, where, stmt_info, node, vectype, misalign };
106 : 9444264 : body_cost_vec->safe_push (si);
107 : :
108 : 9444264 : return (unsigned)
109 : 9444264 : (builtin_vectorization_cost (kind, vectype, misalign) * count);
110 : : }
111 : :
112 : : unsigned
113 : 5138435 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
114 : : enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
115 : : tree vectype, int misalign,
116 : : enum vect_cost_model_location where)
117 : : {
118 : 5138435 : return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
119 : 5138435 : vectype, misalign, where);
120 : : }
121 : :
122 : : unsigned
123 : 1492533 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
124 : : enum vect_cost_for_stmt kind, slp_tree node,
125 : : tree vectype, int misalign,
126 : : enum vect_cost_model_location where)
127 : : {
128 : 1492533 : return record_stmt_cost (body_cost_vec, count, kind,
129 : : SLP_TREE_REPRESENTATIVE (node), node,
130 : 1492533 : vectype, misalign, where);
131 : : }
132 : :
133 : : unsigned
134 : 140097 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
135 : : enum vect_cost_for_stmt kind,
136 : : enum vect_cost_model_location where)
137 : : {
138 : 140097 : gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
139 : : || kind == scalar_stmt);
140 : 140097 : return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
141 : 140097 : NULL_TREE, 0, where);
142 : : }
143 : :
144 : : /* Return a variable of type ELEM_TYPE[NELEMS]. */
145 : :
146 : : static tree
147 : 0 : create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
148 : : {
149 : 0 : return create_tmp_var (build_array_type_nelts (elem_type, nelems),
150 : 0 : "vect_array");
151 : : }
152 : :
153 : : /* ARRAY is an array of vectors created by create_vector_array.
154 : : Return an SSA_NAME for the vector in index N. The reference
155 : : is part of the vectorization of STMT_INFO and the vector is associated
156 : : with scalar destination SCALAR_DEST.
157 : : If we need to ensure that inactive elements are set to zero,
158 : : NEED_ZEROING is true, MASK contains the loop mask to be used. */
159 : :
160 : : static tree
161 : 0 : read_vector_array (vec_info *vinfo,
162 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
163 : : tree scalar_dest, tree array, unsigned HOST_WIDE_INT n,
164 : : bool need_zeroing, tree mask)
165 : : {
166 : 0 : tree vect_type, vect, vect_name, tmp, tmp_name, array_ref;
167 : 0 : gimple *new_stmt;
168 : :
169 : 0 : gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
170 : 0 : vect_type = TREE_TYPE (TREE_TYPE (array));
171 : 0 : tmp = vect_create_destination_var (scalar_dest, vect_type);
172 : 0 : vect = vect_create_destination_var (scalar_dest, vect_type);
173 : 0 : array_ref = build4 (ARRAY_REF, vect_type, array,
174 : 0 : build_int_cst (size_type_node, n),
175 : : NULL_TREE, NULL_TREE);
176 : :
177 : 0 : new_stmt = gimple_build_assign (tmp, array_ref);
178 : 0 : tmp_name = make_ssa_name (vect, new_stmt);
179 : 0 : gimple_assign_set_lhs (new_stmt, tmp_name);
180 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
181 : :
182 : 0 : if (need_zeroing)
183 : : {
184 : 0 : tree vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
185 : : vect_type);
186 : 0 : vect_name = make_ssa_name (vect, new_stmt);
187 : 0 : new_stmt
188 : 0 : = gimple_build_assign (vect_name, VEC_COND_EXPR,
189 : : mask, tmp_name, vec_els);
190 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
191 : : }
192 : : else
193 : : vect_name = tmp_name;
194 : :
195 : 0 : return vect_name;
196 : : }
197 : :
198 : : /* ARRAY is an array of vectors created by create_vector_array.
199 : : Emit code to store SSA_NAME VECT in index N of the array.
200 : : The store is part of the vectorization of STMT_INFO. */
201 : :
202 : : static void
203 : 0 : write_vector_array (vec_info *vinfo,
204 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
205 : : tree vect, tree array, unsigned HOST_WIDE_INT n)
206 : : {
207 : 0 : tree array_ref;
208 : 0 : gimple *new_stmt;
209 : :
210 : 0 : array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
211 : 0 : build_int_cst (size_type_node, n),
212 : : NULL_TREE, NULL_TREE);
213 : :
214 : 0 : new_stmt = gimple_build_assign (array_ref, vect);
215 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
216 : 0 : }
217 : :
218 : : /* PTR is a pointer to an array of type TYPE. Return a representation
219 : : of *PTR. The memory reference replaces those in FIRST_DR
220 : : (and its group). */
221 : :
222 : : static tree
223 : 0 : create_array_ref (tree type, tree ptr, tree alias_ptr_type)
224 : : {
225 : 0 : tree mem_ref;
226 : :
227 : 0 : mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
228 : : /* Arrays have the same alignment as their type. */
229 : 0 : set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
230 : 0 : return mem_ref;
231 : : }
232 : :
233 : : /* Add a clobber of variable VAR to the vectorization of STMT_INFO.
234 : : Emit the clobber before *GSI. */
235 : :
236 : : static void
237 : 15 : vect_clobber_variable (vec_info *vinfo, stmt_vec_info stmt_info,
238 : : gimple_stmt_iterator *gsi, tree var)
239 : : {
240 : 15 : tree clobber = build_clobber (TREE_TYPE (var));
241 : 15 : gimple *new_stmt = gimple_build_assign (var, clobber);
242 : 15 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
243 : 15 : }
244 : :
245 : : /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
246 : :
247 : : /* Function vect_mark_relevant.
248 : :
249 : : Mark STMT_INFO as "relevant for vectorization" and add it to WORKLIST. */
250 : :
251 : : static void
252 : 2924361 : vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
253 : : enum vect_relevant relevant, bool live_p)
254 : : {
255 : 2924361 : enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
256 : 2924361 : bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
257 : :
258 : 2924361 : if (dump_enabled_p ())
259 : 157412 : dump_printf_loc (MSG_NOTE, vect_location,
260 : : "mark relevant %d, live %d: %G", relevant, live_p,
261 : : stmt_info->stmt);
262 : :
263 : : /* If this stmt is an original stmt in a pattern, we might need to mark its
264 : : related pattern stmt instead of the original stmt. However, such stmts
265 : : may have their own uses that are not in any pattern, in such cases the
266 : : stmt itself should be marked. */
267 : 2924361 : if (STMT_VINFO_IN_PATTERN_P (stmt_info))
268 : : {
269 : : /* This is the last stmt in a sequence that was detected as a
270 : : pattern that can potentially be vectorized. Don't mark the stmt
271 : : as relevant/live because it's not going to be vectorized.
272 : : Instead mark the pattern-stmt that replaces it. */
273 : :
274 : 166151 : if (dump_enabled_p ())
275 : 2278 : dump_printf_loc (MSG_NOTE, vect_location,
276 : : "last stmt in pattern. don't mark"
277 : : " relevant/live.\n");
278 : :
279 : 166151 : stmt_vec_info old_stmt_info = stmt_info;
280 : 166151 : stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
281 : 166151 : gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
282 : 166151 : save_relevant = STMT_VINFO_RELEVANT (stmt_info);
283 : 166151 : save_live_p = STMT_VINFO_LIVE_P (stmt_info);
284 : :
285 : 166151 : if (live_p && relevant == vect_unused_in_scope)
286 : : {
287 : 92 : if (dump_enabled_p ())
288 : 4 : dump_printf_loc (MSG_NOTE, vect_location,
289 : : "vec_stmt_relevant_p: forcing live pattern stmt "
290 : : "relevant.\n");
291 : : relevant = vect_used_only_live;
292 : : }
293 : :
294 : 166151 : if (dump_enabled_p ())
295 : 2278 : dump_printf_loc (MSG_NOTE, vect_location,
296 : : "mark relevant %d, live %d: %G", relevant, live_p,
297 : : stmt_info->stmt);
298 : : }
299 : :
300 : 2924361 : STMT_VINFO_LIVE_P (stmt_info) |= live_p;
301 : 2924361 : if (relevant > STMT_VINFO_RELEVANT (stmt_info))
302 : 2426232 : STMT_VINFO_RELEVANT (stmt_info) = relevant;
303 : :
304 : 2924361 : if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
305 : 498129 : && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
306 : : {
307 : 497491 : if (dump_enabled_p ())
308 : 20381 : dump_printf_loc (MSG_NOTE, vect_location,
309 : : "already marked relevant/live.\n");
310 : 497491 : return;
311 : : }
312 : :
313 : 2426870 : worklist->safe_push (stmt_info);
314 : : }
315 : :
316 : :
317 : : /* Function is_simple_and_all_uses_invariant
318 : :
319 : : Return true if STMT_INFO is simple and all uses of it are invariant. */
320 : :
321 : : bool
322 : 355134 : is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
323 : : loop_vec_info loop_vinfo)
324 : : {
325 : 355134 : tree op;
326 : 355134 : ssa_op_iter iter;
327 : :
328 : 466526 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
329 : 112122 : if (!stmt)
330 : : return false;
331 : :
332 : 117196 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
333 : : {
334 : 116466 : enum vect_def_type dt = vect_uninitialized_def;
335 : :
336 : 116466 : if (!vect_is_simple_use (op, loop_vinfo, &dt))
337 : : {
338 : 693 : if (dump_enabled_p ())
339 : 15 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
340 : : "use not simple.\n");
341 : 111392 : return false;
342 : : }
343 : :
344 : 115773 : if (dt != vect_external_def && dt != vect_constant_def)
345 : : return false;
346 : : }
347 : : return true;
348 : : }
349 : :
350 : : /* Function vect_stmt_relevant_p.
351 : :
352 : : Return true if STMT_INFO, in the loop that is represented by LOOP_VINFO,
353 : : is "relevant for vectorization".
354 : :
355 : : A stmt is considered "relevant for vectorization" if:
356 : : - it has uses outside the loop.
357 : : - it has vdefs (it alters memory).
358 : : - control stmts in the loop (except for the exit condition).
359 : : - it is an induction and we have multiple exits.
360 : :
361 : : CHECKME: what other side effects would the vectorizer allow? */
362 : :
363 : : static bool
364 : 3709935 : vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
365 : : enum vect_relevant *relevant, bool *live_p)
366 : : {
367 : 3709935 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
368 : 3709935 : ssa_op_iter op_iter;
369 : 3709935 : imm_use_iterator imm_iter;
370 : 3709935 : use_operand_p use_p;
371 : 3709935 : def_operand_p def_p;
372 : :
373 : 3709935 : *relevant = vect_unused_in_scope;
374 : 3709935 : *live_p = false;
375 : :
376 : : /* cond stmt other than loop exit cond. */
377 : 3709935 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
378 : 3709935 : if (is_ctrl_stmt (stmt)
379 : 465850 : && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
380 : 3871150 : && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
381 : 159590 : *relevant = vect_used_in_scope;
382 : :
383 : : /* changing memory. */
384 : 3709935 : if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
385 : 3079353 : if (gimple_vdef (stmt_info->stmt)
386 : 2613503 : && !gimple_clobber_p (stmt_info->stmt))
387 : : {
388 : 263187 : if (dump_enabled_p ())
389 : 25763 : dump_printf_loc (MSG_NOTE, vect_location,
390 : : "vec_stmt_relevant_p: stmt has vdefs.\n");
391 : 263187 : *relevant = vect_used_in_scope;
392 : 263187 : if (! STMT_VINFO_DATA_REF (stmt_info)
393 : 263187 : && zero_ssa_operands (stmt_info->stmt, SSA_OP_DEF))
394 : 20 : LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo).safe_push (stmt_info);
395 : : }
396 : :
397 : : /* uses outside the loop. */
398 : 10393431 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
399 : : {
400 : 7903085 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
401 : : {
402 : 4929524 : basic_block bb = gimple_bb (USE_STMT (use_p));
403 : 4929524 : if (!flow_bb_inside_loop_p (loop, bb))
404 : : {
405 : 156792 : if (is_gimple_debug (USE_STMT (use_p)))
406 : 624 : continue;
407 : :
408 : 156168 : if (dump_enabled_p ())
409 : 5262 : dump_printf_loc (MSG_NOTE, vect_location,
410 : : "vec_stmt_relevant_p: used out of loop.\n");
411 : :
412 : : /* We expect all such uses to be in the loop exit phis
413 : : (because of loop closed form) */
414 : 156168 : gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
415 : :
416 : 156168 : *live_p = true;
417 : : }
418 : : }
419 : : }
420 : :
421 : : /* Check if it's a not live PHI and multiple exits. In this case
422 : : there will be a usage later on after peeling which is needed for the
423 : : alternate exit.
424 : : ??? Unless the PHI was marked live because of early
425 : : break, which also needs the latch def live and vectorized. */
426 : 3709935 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
427 : 1323913 : && is_a <gphi *> (stmt)
428 : 239315 : && gimple_bb (stmt) == LOOP_VINFO_LOOP (loop_vinfo)->header
429 : 3949244 : && ((! VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
430 : 235850 : && ! *live_p)
431 : 30151 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def))
432 : : {
433 : 233582 : if (dump_enabled_p ())
434 : 2750 : dump_printf_loc (MSG_NOTE, vect_location,
435 : : "vec_stmt_relevant_p: PHI forced live for "
436 : : "early break.\n");
437 : 233582 : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo).safe_push (stmt_info);
438 : 233582 : *live_p = true;
439 : : }
440 : :
441 : 355136 : if (*live_p && *relevant == vect_unused_in_scope
442 : 4065069 : && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
443 : : {
444 : 354404 : if (dump_enabled_p ())
445 : 7673 : dump_printf_loc (MSG_NOTE, vect_location,
446 : : "vec_stmt_relevant_p: stmt live but not relevant.\n");
447 : 354404 : *relevant = vect_used_only_live;
448 : : }
449 : :
450 : 3709935 : return (*live_p || *relevant);
451 : : }
452 : :
453 : :
454 : : /* Function exist_non_indexing_operands_for_use_p
455 : :
456 : : USE is one of the uses attached to STMT_INFO. Check if USE is
457 : : used in STMT_INFO for anything other than indexing an array. */
458 : :
459 : : static bool
460 : 3702068 : exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
461 : : {
462 : 3702068 : tree operand;
463 : :
464 : : /* USE corresponds to some operand in STMT. If there is no data
465 : : reference in STMT, then any operand that corresponds to USE
466 : : is not indexing an array. */
467 : 3702068 : if (!STMT_VINFO_DATA_REF (stmt_info))
468 : : return true;
469 : :
470 : : /* STMT has a data_ref. FORNOW this means that its of one of
471 : : the following forms:
472 : : -1- ARRAY_REF = var
473 : : -2- var = ARRAY_REF
474 : : (This should have been verified in analyze_data_refs).
475 : :
476 : : 'var' in the second case corresponds to a def, not a use,
477 : : so USE cannot correspond to any operands that are not used
478 : : for array indexing.
479 : :
480 : : Therefore, all we need to check is if STMT falls into the
481 : : first case, and whether var corresponds to USE. */
482 : :
483 : 1106932 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
484 : 1095201 : if (!assign || !gimple_assign_copy_p (assign))
485 : : {
486 : 609629 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
487 : 11731 : if (call && gimple_call_internal_p (call))
488 : : {
489 : 11731 : internal_fn ifn = gimple_call_internal_fn (call);
490 : 11731 : int mask_index = internal_fn_mask_index (ifn);
491 : 11731 : if (mask_index >= 0
492 : 11731 : && use == gimple_call_arg (call, mask_index))
493 : : return true;
494 : 7658 : int els_index = internal_fn_else_index (ifn);
495 : 7658 : if (els_index >= 0
496 : 7658 : && use == gimple_call_arg (call, els_index))
497 : : return true;
498 : 6601 : int stored_value_index = internal_fn_stored_value_index (ifn);
499 : 6601 : if (stored_value_index >= 0
500 : 6601 : && use == gimple_call_arg (call, stored_value_index))
501 : : return true;
502 : 5316 : if (internal_gather_scatter_fn_p (ifn)
503 : 5316 : && use == gimple_call_arg (call, 1))
504 : : return true;
505 : : }
506 : 603214 : return false;
507 : : }
508 : :
509 : 497303 : if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
510 : : return false;
511 : 497303 : operand = gimple_assign_rhs1 (assign);
512 : 497303 : if (TREE_CODE (operand) != SSA_NAME)
513 : : return false;
514 : :
515 : 432339 : if (operand == use)
516 : : return true;
517 : :
518 : : return false;
519 : : }
520 : :
521 : :
522 : : /*
523 : : Function process_use.
524 : :
525 : : Inputs:
526 : : - a USE in STMT_VINFO in a loop represented by LOOP_VINFO
527 : : - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
528 : : that defined USE. This is done by calling mark_relevant and passing it
529 : : the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
530 : : - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
531 : : be performed.
532 : :
533 : : Outputs:
534 : : Generally, LIVE_P and RELEVANT are used to define the liveness and
535 : : relevance info of the DEF_STMT of this USE:
536 : : STMT_VINFO_LIVE_P (DEF_stmt_vinfo) <-- live_p
537 : : STMT_VINFO_RELEVANT (DEF_stmt_vinfo) <-- relevant
538 : : Exceptions:
539 : : - case 1: If USE is used only for address computations (e.g. array indexing),
540 : : which does not need to be directly vectorized, then the liveness/relevance
541 : : of the respective DEF_STMT is left unchanged.
542 : : - case 2: If STMT_VINFO is a reduction phi and DEF_STMT is a reduction stmt,
543 : : we skip DEF_STMT cause it had already been processed.
544 : : - case 3: If DEF_STMT and STMT_VINFO are in different nests, then
545 : : "relevant" will be modified accordingly.
546 : :
547 : : Return true if everything is as expected. Return false otherwise. */
548 : :
549 : : static opt_result
550 : 3731277 : process_use (stmt_vec_info stmt_vinfo, tree use, loop_vec_info loop_vinfo,
551 : : enum vect_relevant relevant, vec<stmt_vec_info> *worklist,
552 : : bool force)
553 : : {
554 : 3731277 : stmt_vec_info dstmt_vinfo;
555 : 3731277 : enum vect_def_type dt;
556 : :
557 : : /* case 1: we are only interested in uses that need to be vectorized. Uses
558 : : that are used for address computation are not considered relevant. */
559 : 3731277 : if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
560 : 896672 : return opt_result::success ();
561 : :
562 : 2834605 : if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
563 : 8542 : return opt_result::failure_at (stmt_vinfo->stmt,
564 : : "not vectorized:"
565 : : " unsupported use in stmt.\n");
566 : :
567 : 2826063 : if (!dstmt_vinfo)
568 : 622395 : return opt_result::success ();
569 : :
570 : 2203668 : basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
571 : 2203668 : basic_block bb = gimple_bb (stmt_vinfo->stmt);
572 : :
573 : : /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
574 : : We have to force the stmt live since the epilogue loop needs it to
575 : : continue computing the reduction. */
576 : 2203668 : if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
577 : 383339 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
578 : 57053 : && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
579 : 57053 : && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
580 : 2260721 : && bb->loop_father == def_bb->loop_father)
581 : : {
582 : 57053 : if (dump_enabled_p ())
583 : 3414 : dump_printf_loc (MSG_NOTE, vect_location,
584 : : "reduc-stmt defining reduc-phi in the same nest.\n");
585 : 57053 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
586 : 57053 : return opt_result::success ();
587 : : }
588 : :
589 : : /* case 3a: outer-loop stmt defining an inner-loop stmt:
590 : : outer-loop-header-bb:
591 : : d = dstmt_vinfo
592 : : inner-loop:
593 : : stmt # use (d)
594 : : outer-loop-tail-bb:
595 : : ... */
596 : 2146615 : if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
597 : : {
598 : 1841 : if (dump_enabled_p ())
599 : 279 : dump_printf_loc (MSG_NOTE, vect_location,
600 : : "outer-loop def-stmt defining inner-loop stmt.\n");
601 : :
602 : 1841 : switch (relevant)
603 : : {
604 : 0 : case vect_unused_in_scope:
605 : 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
606 : : vect_used_in_scope : vect_unused_in_scope;
607 : : break;
608 : :
609 : 622 : case vect_used_in_outer_by_reduction:
610 : 622 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
611 : : relevant = vect_used_by_reduction;
612 : : break;
613 : :
614 : 1007 : case vect_used_in_outer:
615 : 1007 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
616 : : relevant = vect_used_in_scope;
617 : : break;
618 : :
619 : : case vect_used_in_scope:
620 : : break;
621 : :
622 : 0 : default:
623 : 0 : gcc_unreachable ();
624 : : }
625 : : }
626 : :
627 : : /* case 3b: inner-loop stmt defining an outer-loop stmt:
628 : : outer-loop-header-bb:
629 : : ...
630 : : inner-loop:
631 : : d = dstmt_vinfo
632 : : outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
633 : : stmt # use (d) */
634 : 2144774 : else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
635 : : {
636 : 1735 : if (dump_enabled_p ())
637 : 534 : dump_printf_loc (MSG_NOTE, vect_location,
638 : : "inner-loop def-stmt defining outer-loop stmt.\n");
639 : :
640 : 1735 : switch (relevant)
641 : : {
642 : 0 : case vect_unused_in_scope:
643 : 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
644 : 0 : || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
645 : : vect_used_in_outer_by_reduction : vect_unused_in_scope;
646 : : break;
647 : :
648 : : case vect_used_by_reduction:
649 : : case vect_used_only_live:
650 : : relevant = vect_used_in_outer_by_reduction;
651 : : break;
652 : :
653 : : case vect_used_in_scope:
654 : 2094269 : relevant = vect_used_in_outer;
655 : : break;
656 : :
657 : 0 : default:
658 : 0 : gcc_unreachable ();
659 : : }
660 : : }
661 : : /* We are also not interested in uses on loop PHI backedges that are
662 : : inductions. Otherwise we'll needlessly vectorize the IV increment
663 : : and cause hybrid SLP for SLP inductions. */
664 : 2143039 : else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
665 : 323477 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
666 : 320777 : && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
667 : : loop_latch_edge (bb->loop_father))
668 : : == use)
669 : 2463816 : && (!LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
670 : 268431 : || (gimple_bb (stmt_vinfo->stmt)
671 : 268431 : != LOOP_VINFO_LOOP (loop_vinfo)->header)))
672 : : {
673 : 52346 : if (dump_enabled_p ())
674 : 3442 : dump_printf_loc (MSG_NOTE, vect_location,
675 : : "induction value on backedge.\n");
676 : 52346 : return opt_result::success ();
677 : : }
678 : :
679 : 2094269 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
680 : 2094269 : return opt_result::success ();
681 : : }
682 : :
683 : :
684 : : /* Function vect_mark_stmts_to_be_vectorized.
685 : :
686 : : Not all stmts in the loop need to be vectorized. For example:
687 : :
688 : : for i...
689 : : for j...
690 : : 1. T0 = i + j
691 : : 2. T1 = a[T0]
692 : :
693 : : 3. j = j + 1
694 : :
695 : : Stmt 1 and 3 do not need to be vectorized, because loop control and
696 : : addressing of vectorized data-refs are handled differently.
697 : :
698 : : This pass detects such stmts. */
699 : :
700 : : opt_result
701 : 309622 : vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
702 : : {
703 : 309622 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
704 : 309622 : basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
705 : 309622 : unsigned int nbbs = loop->num_nodes;
706 : 309622 : gimple_stmt_iterator si;
707 : 309622 : unsigned int i;
708 : 309622 : basic_block bb;
709 : 309622 : bool live_p;
710 : 309622 : enum vect_relevant relevant;
711 : :
712 : 309622 : DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
713 : :
714 : 309622 : auto_vec<stmt_vec_info, 64> worklist;
715 : :
716 : : /* 1. Init worklist. */
717 : 1083369 : for (i = 0; i < nbbs; i++)
718 : : {
719 : 778734 : bb = bbs[i];
720 : 1568311 : for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
721 : : {
722 : 1588898 : if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
723 : 163867 : continue;
724 : 630582 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
725 : 630582 : if (dump_enabled_p ())
726 : 37700 : dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
727 : : phi_info->stmt);
728 : :
729 : 630582 : if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
730 : : {
731 : 237164 : if (STMT_VINFO_DEF_TYPE (phi_info) == vect_unknown_def_type)
732 : 4872 : return opt_result::failure_at
733 : 4872 : (*si, "not vectorized: unhandled relevant PHI: %G", *si);
734 : 232292 : vect_mark_relevant (&worklist, phi_info, relevant, live_p);
735 : : }
736 : : }
737 : 5574098 : for (si = gsi_after_labels (bb); !gsi_end_p (si); gsi_next (&si))
738 : : {
739 : 4800351 : gimple *stmt = gsi_stmt (si);
740 : 4800351 : if (is_gimple_debug (stmt))
741 : 1720883 : continue;
742 : 3079468 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
743 : 3079468 : if (dump_enabled_p ())
744 : 201938 : dump_printf_loc (MSG_NOTE, vect_location,
745 : : "init: stmt relevant? %G", stmt);
746 : :
747 : 3079468 : if (gimple_get_lhs (stmt) == NULL_TREE
748 : 472053 : && !is_a <gcond *> (stmt)
749 : 3085671 : && !is_a <gcall *> (stmt))
750 : 115 : return opt_result::failure_at
751 : 115 : (stmt, "not vectorized: irregular stmt: %G", stmt);
752 : :
753 : 3079353 : if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
754 : 540747 : vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
755 : : }
756 : : }
757 : :
758 : : /* 2. Process_worklist */
759 : 2701608 : while (worklist.length () > 0)
760 : : {
761 : 2405517 : use_operand_p use_p;
762 : 2405517 : ssa_op_iter iter;
763 : :
764 : 2405517 : stmt_vec_info stmt_vinfo = worklist.pop ();
765 : 2405517 : if (dump_enabled_p ())
766 : 136689 : dump_printf_loc (MSG_NOTE, vect_location,
767 : : "worklist: examine stmt: %G", stmt_vinfo->stmt);
768 : :
769 : : /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
770 : : (DEF_STMT) as relevant/irrelevant according to the relevance property
771 : : of STMT. */
772 : 2405517 : relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
773 : :
774 : : /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
775 : : propagated as is to the DEF_STMTs of its USEs.
776 : :
777 : : One exception is when STMT has been identified as defining a reduction
778 : : variable; in this case we set the relevance to vect_used_by_reduction.
779 : : This is because we distinguish between two kinds of relevant stmts -
780 : : those that are used by a reduction computation, and those that are
781 : : (also) used by a regular computation. This allows us later on to
782 : : identify stmts that are used solely by a reduction, and therefore the
783 : : order of the results that they produce does not have to be kept. */
784 : :
785 : 2405517 : switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
786 : : {
787 : 114424 : case vect_reduction_def:
788 : 114424 : gcc_assert (relevant != vect_unused_in_scope);
789 : 114424 : if (relevant != vect_unused_in_scope
790 : 114424 : && relevant != vect_used_in_scope
791 : 114424 : && relevant != vect_used_by_reduction
792 : 114424 : && relevant != vect_used_only_live)
793 : 0 : return opt_result::failure_at
794 : 0 : (stmt_vinfo->stmt, "unsupported use of reduction.\n");
795 : : break;
796 : :
797 : 1914 : case vect_nested_cycle:
798 : 1914 : if (relevant != vect_unused_in_scope
799 : 1914 : && relevant != vect_used_in_outer_by_reduction
800 : 1420 : && relevant != vect_used_in_outer)
801 : 2 : return opt_result::failure_at
802 : 2 : (stmt_vinfo->stmt, "unsupported use of nested cycle.\n");
803 : : break;
804 : :
805 : 1001 : case vect_double_reduction_def:
806 : 1001 : if (relevant != vect_unused_in_scope
807 : 1001 : && relevant != vect_used_by_reduction
808 : 337 : && relevant != vect_used_only_live)
809 : 0 : return opt_result::failure_at
810 : 0 : (stmt_vinfo->stmt, "unsupported use of double reduction.\n");
811 : : break;
812 : :
813 : : default:
814 : : break;
815 : : }
816 : :
817 : 2405515 : if (is_pattern_stmt_p (stmt_vinfo))
818 : : {
819 : : /* Pattern statements are not inserted into the code, so
820 : : FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
821 : : have to scan the RHS or function arguments instead. */
822 : 447571 : if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
823 : : {
824 : 288650 : enum tree_code rhs_code = gimple_assign_rhs_code (assign);
825 : 288650 : tree op = gimple_assign_rhs1 (assign);
826 : :
827 : 288650 : i = 1;
828 : 288650 : if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
829 : : {
830 : 0 : opt_result res
831 : 0 : = process_use (stmt_vinfo, TREE_OPERAND (op, 0),
832 : : loop_vinfo, relevant, &worklist, false);
833 : 0 : if (!res)
834 : 0 : return res;
835 : 0 : res = process_use (stmt_vinfo, TREE_OPERAND (op, 1),
836 : : loop_vinfo, relevant, &worklist, false);
837 : 0 : if (!res)
838 : 0 : return res;
839 : : i = 2;
840 : : }
841 : 832796 : for (; i < gimple_num_ops (assign); i++)
842 : : {
843 : 544536 : op = gimple_op (assign, i);
844 : 544536 : if (TREE_CODE (op) == SSA_NAME)
845 : : {
846 : 416506 : opt_result res
847 : 416506 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
848 : : &worklist, false);
849 : 416506 : if (!res)
850 : 390 : return res;
851 : : }
852 : : }
853 : : }
854 : 158921 : else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
855 : : {
856 : 155048 : tree_code rhs_code = gimple_cond_code (cond);
857 : 155048 : gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
858 : 155048 : opt_result res
859 : 155048 : = process_use (stmt_vinfo, gimple_cond_lhs (cond),
860 : : loop_vinfo, relevant, &worklist, false);
861 : 155048 : if (!res)
862 : 8544 : return res;
863 : 155048 : res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
864 : : loop_vinfo, relevant, &worklist, false);
865 : 155048 : if (!res)
866 : 0 : return res;
867 : : }
868 : 3873 : else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
869 : : {
870 : 18613 : for (i = 0; i < gimple_call_num_args (call); i++)
871 : : {
872 : 14740 : tree arg = gimple_call_arg (call, i);
873 : 14740 : opt_result res
874 : 14740 : = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
875 : : &worklist, false);
876 : 14740 : if (!res)
877 : 0 : return res;
878 : : }
879 : : }
880 : : else
881 : 0 : gcc_unreachable ();
882 : : }
883 : : else
884 : 6869335 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
885 : : {
886 : 2960726 : tree op = USE_FROM_PTR (use_p);
887 : 2960726 : opt_result res
888 : 2960726 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
889 : : &worklist, false);
890 : 2960726 : if (!res)
891 : 7279 : return res;
892 : : }
893 : :
894 : 2397846 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
895 : : {
896 : 29209 : gather_scatter_info gs_info;
897 : 29209 : if (!vect_check_gather_scatter (stmt_vinfo,
898 : : STMT_VINFO_VECTYPE (stmt_vinfo),
899 : : loop_vinfo, &gs_info))
900 : 0 : gcc_unreachable ();
901 : 29209 : opt_result res
902 : 29209 : = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
903 : : &worklist, true);
904 : 29209 : if (!res)
905 : : {
906 : 873 : if (fatal)
907 : 873 : *fatal = false;
908 : 873 : return res;
909 : : }
910 : : }
911 : : } /* while worklist */
912 : :
913 : 296091 : return opt_result::success ();
914 : 309622 : }
915 : :
916 : : /* Function vect_model_simple_cost.
917 : :
918 : : Models cost for simple operations, i.e. those that only emit N operations
919 : : of the same KIND. */
920 : :
921 : : static void
922 : 601583 : vect_model_simple_cost (vec_info *vinfo, int n, slp_tree node,
923 : : stmt_vector_for_cost *cost_vec,
924 : : vect_cost_for_stmt kind = vector_stmt)
925 : : {
926 : 601583 : int inside_cost = 0, prologue_cost = 0;
927 : :
928 : 601583 : gcc_assert (cost_vec != NULL);
929 : :
930 : 601583 : n *= vect_get_num_copies (vinfo, node);
931 : :
932 : : /* Pass the inside-of-loop statements to the target-specific cost model. */
933 : 601583 : inside_cost += record_stmt_cost (cost_vec, n, kind, node, 0, vect_body);
934 : :
935 : 601583 : if (dump_enabled_p ())
936 : 31831 : dump_printf_loc (MSG_NOTE, vect_location,
937 : : "vect_model_simple_cost: inside_cost = %d, "
938 : : "prologue_cost = %d .\n", inside_cost, prologue_cost);
939 : 601583 : }
940 : :
941 : :
942 : : /* Model cost for type demotion and promotion operations. PWR is
943 : : normally zero for single-step promotions and demotions. It will be
944 : : one if two-step promotion/demotion is required, and so on. NCOPIES
945 : : is the number of vector results (and thus number of instructions)
946 : : for the narrowest end of the operation chain. Each additional
947 : : step doubles the number of instructions required. If WIDEN_ARITH
948 : : is true the stmt is doing widening arithmetic. */
949 : :
950 : : static void
951 : 53396 : vect_model_promotion_demotion_cost (slp_tree slp_node,
952 : : unsigned int ncopies, int pwr,
953 : : stmt_vector_for_cost *cost_vec,
954 : : bool widen_arith)
955 : : {
956 : 53396 : int i;
957 : 53396 : int inside_cost = 0, prologue_cost = 0;
958 : :
959 : 126085 : for (i = 0; i < pwr + 1; i++)
960 : : {
961 : 143399 : inside_cost += record_stmt_cost (cost_vec, ncopies,
962 : : widen_arith
963 : : ? vector_stmt : vec_promote_demote,
964 : : slp_node, 0, vect_body);
965 : 72689 : ncopies *= 2;
966 : : }
967 : :
968 : 53396 : if (dump_enabled_p ())
969 : 6120 : dump_printf_loc (MSG_NOTE, vect_location,
970 : : "vect_model_promotion_demotion_cost: inside_cost = %d, "
971 : : "prologue_cost = %d .\n", inside_cost, prologue_cost);
972 : 53396 : }
973 : :
974 : : /* Returns true if the current function returns DECL. */
975 : :
976 : : static bool
977 : 545395 : cfun_returns (tree decl)
978 : : {
979 : 545395 : edge_iterator ei;
980 : 545395 : edge e;
981 : 1073545 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
982 : : {
983 : 1079982 : greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
984 : 539991 : if (!ret)
985 : 0 : continue;
986 : 539991 : if (gimple_return_retval (ret) == decl)
987 : : return true;
988 : : /* We often end up with an aggregate copy to the result decl,
989 : : handle that case as well. First skip intermediate clobbers
990 : : though. */
991 : : gimple *def = ret;
992 : 1583829 : do
993 : : {
994 : 3167658 : def = SSA_NAME_DEF_STMT (gimple_vuse (def));
995 : : }
996 : 1583829 : while (gimple_clobber_p (def));
997 : 528796 : if (is_a <gassign *> (def)
998 : 62583 : && gimple_assign_lhs (def) == gimple_return_retval (ret)
999 : 536975 : && gimple_assign_rhs1 (def) == decl)
1000 : : return true;
1001 : : }
1002 : : return false;
1003 : : }
1004 : :
1005 : : /* Calculate cost of DR's memory access. */
1006 : : void
1007 : 889822 : vect_get_store_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1008 : : int ncopies, dr_alignment_support alignment_support_scheme,
1009 : : int misalignment,
1010 : : unsigned int *inside_cost,
1011 : : stmt_vector_for_cost *body_cost_vec)
1012 : : {
1013 : 889822 : tree vectype
1014 : 889822 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1015 : 889822 : switch (alignment_support_scheme)
1016 : : {
1017 : 471646 : case dr_aligned:
1018 : 471646 : {
1019 : 471646 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1020 : : vector_store, stmt_info, slp_node,
1021 : : vectype, 0, vect_body);
1022 : :
1023 : 471646 : if (dump_enabled_p ())
1024 : 13419 : dump_printf_loc (MSG_NOTE, vect_location,
1025 : : "vect_model_store_cost: aligned.\n");
1026 : : break;
1027 : : }
1028 : :
1029 : 418176 : case dr_unaligned_supported:
1030 : 418176 : {
1031 : : /* Here, we assign an additional cost for the unaligned store. */
1032 : 418176 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1033 : : unaligned_store, stmt_info, slp_node,
1034 : : vectype, misalignment, vect_body);
1035 : 418176 : if (dump_enabled_p ())
1036 : 12288 : dump_printf_loc (MSG_NOTE, vect_location,
1037 : : "vect_model_store_cost: unaligned supported by "
1038 : : "hardware.\n");
1039 : : break;
1040 : : }
1041 : :
1042 : 0 : case dr_unaligned_unsupported:
1043 : 0 : {
1044 : 0 : *inside_cost = VECT_MAX_COST;
1045 : :
1046 : 0 : if (dump_enabled_p ())
1047 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1048 : : "vect_model_store_cost: unsupported access.\n");
1049 : : break;
1050 : : }
1051 : :
1052 : 0 : default:
1053 : 0 : gcc_unreachable ();
1054 : : }
1055 : 889822 : }
1056 : :
1057 : : /* Calculate cost of DR's memory access. */
1058 : : void
1059 : 721443 : vect_get_load_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1060 : : int ncopies, dr_alignment_support alignment_support_scheme,
1061 : : int misalignment,
1062 : : bool add_realign_cost, unsigned int *inside_cost,
1063 : : unsigned int *prologue_cost,
1064 : : stmt_vector_for_cost *prologue_cost_vec,
1065 : : stmt_vector_for_cost *body_cost_vec,
1066 : : bool record_prologue_costs)
1067 : : {
1068 : 721443 : tree vectype
1069 : 721443 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1070 : 721443 : switch (alignment_support_scheme)
1071 : : {
1072 : 419338 : case dr_aligned:
1073 : 419338 : {
1074 : 419338 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1075 : : stmt_info, slp_node, vectype,
1076 : : 0, vect_body);
1077 : :
1078 : 419338 : if (dump_enabled_p ())
1079 : 17489 : dump_printf_loc (MSG_NOTE, vect_location,
1080 : : "vect_model_load_cost: aligned.\n");
1081 : :
1082 : : break;
1083 : : }
1084 : 251749 : case dr_unaligned_supported:
1085 : 251749 : {
1086 : : /* Here, we assign an additional cost for the unaligned load. */
1087 : 251749 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1088 : : unaligned_load, stmt_info, slp_node,
1089 : : vectype, misalignment, vect_body);
1090 : :
1091 : 251749 : if (dump_enabled_p ())
1092 : 20271 : dump_printf_loc (MSG_NOTE, vect_location,
1093 : : "vect_model_load_cost: unaligned supported by "
1094 : : "hardware.\n");
1095 : :
1096 : : break;
1097 : : }
1098 : 0 : case dr_explicit_realign:
1099 : 0 : {
1100 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1101 : : vector_load, stmt_info, slp_node,
1102 : : vectype, 0, vect_body);
1103 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1104 : : vec_perm, stmt_info, slp_node,
1105 : : vectype, 0, vect_body);
1106 : :
1107 : : /* FIXME: If the misalignment remains fixed across the iterations of
1108 : : the containing loop, the following cost should be added to the
1109 : : prologue costs. */
1110 : 0 : if (targetm.vectorize.builtin_mask_for_load)
1111 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1112 : : stmt_info, slp_node, vectype,
1113 : : 0, vect_body);
1114 : :
1115 : 0 : if (dump_enabled_p ())
1116 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1117 : : "vect_model_load_cost: explicit realign\n");
1118 : :
1119 : : break;
1120 : : }
1121 : 0 : case dr_explicit_realign_optimized:
1122 : 0 : {
1123 : 0 : if (dump_enabled_p ())
1124 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1125 : : "vect_model_load_cost: unaligned software "
1126 : : "pipelined.\n");
1127 : :
1128 : : /* Unaligned software pipeline has a load of an address, an initial
1129 : : load, and possibly a mask operation to "prime" the loop. However,
1130 : : if this is an access in a group of loads, which provide grouped
1131 : : access, then the above cost should only be considered for one
1132 : : access in the group. Inside the loop, there is a load op
1133 : : and a realignment op. */
1134 : :
1135 : 0 : if (add_realign_cost && record_prologue_costs)
1136 : : {
1137 : 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1138 : : vector_stmt, stmt_info,
1139 : : slp_node, vectype,
1140 : : 0, vect_prologue);
1141 : 0 : if (targetm.vectorize.builtin_mask_for_load)
1142 : 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1143 : : vector_stmt, stmt_info,
1144 : : slp_node, vectype,
1145 : : 0, vect_prologue);
1146 : : }
1147 : :
1148 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1149 : : stmt_info, slp_node, vectype,
1150 : : 0, vect_body);
1151 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1152 : : stmt_info, slp_node, vectype,
1153 : : 0, vect_body);
1154 : :
1155 : 0 : if (dump_enabled_p ())
1156 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1157 : : "vect_model_load_cost: explicit realign optimized"
1158 : : "\n");
1159 : :
1160 : : break;
1161 : : }
1162 : :
1163 : 50356 : case dr_unaligned_unsupported:
1164 : 50356 : {
1165 : 50356 : *inside_cost = VECT_MAX_COST;
1166 : :
1167 : 50356 : if (dump_enabled_p ())
1168 : 93 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1169 : : "vect_model_load_cost: unsupported access.\n");
1170 : : break;
1171 : : }
1172 : :
1173 : 0 : default:
1174 : 0 : gcc_unreachable ();
1175 : : }
1176 : 721443 : }
1177 : :
1178 : : /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1179 : : the loop preheader for the vectorized stmt STMT_VINFO. */
1180 : :
1181 : : static void
1182 : 6263 : vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
1183 : : gimple_stmt_iterator *gsi)
1184 : : {
1185 : 6263 : if (gsi)
1186 : 2998 : vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
1187 : : else
1188 : 3265 : vinfo->insert_on_entry (stmt_vinfo, new_stmt);
1189 : :
1190 : 6263 : if (dump_enabled_p ())
1191 : 1728 : dump_printf_loc (MSG_NOTE, vect_location,
1192 : : "created new init_stmt: %G", new_stmt);
1193 : 6263 : }
1194 : :
1195 : : /* Function vect_init_vector.
1196 : :
1197 : : Insert a new stmt (INIT_STMT) that initializes a new variable of type
1198 : : TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1199 : : vector type a vector with all elements equal to VAL is created first.
1200 : : Place the initialization at GSI if it is not NULL. Otherwise, place the
1201 : : initialization at the loop preheader.
1202 : : Return the DEF of INIT_STMT.
1203 : : It will be used in the vectorization of STMT_INFO. */
1204 : :
1205 : : tree
1206 : 4539 : vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
1207 : : gimple_stmt_iterator *gsi)
1208 : : {
1209 : 4539 : gimple *init_stmt;
1210 : 4539 : tree new_temp;
1211 : :
1212 : : /* We abuse this function to push sth to a SSA name with initial 'val'. */
1213 : 4539 : if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1214 : : {
1215 : 1349 : gcc_assert (VECTOR_TYPE_P (type));
1216 : 1349 : if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1217 : : {
1218 : : /* Scalar boolean value should be transformed into
1219 : : all zeros or all ones value before building a vector. */
1220 : 18 : if (VECTOR_BOOLEAN_TYPE_P (type))
1221 : : {
1222 : 0 : tree true_val = build_all_ones_cst (TREE_TYPE (type));
1223 : 0 : tree false_val = build_zero_cst (TREE_TYPE (type));
1224 : :
1225 : 0 : if (CONSTANT_CLASS_P (val))
1226 : 0 : val = integer_zerop (val) ? false_val : true_val;
1227 : : else
1228 : : {
1229 : 0 : new_temp = make_ssa_name (TREE_TYPE (type));
1230 : 0 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1231 : : val, true_val, false_val);
1232 : 0 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1233 : 0 : val = new_temp;
1234 : : }
1235 : : }
1236 : : else
1237 : : {
1238 : 18 : gimple_seq stmts = NULL;
1239 : 18 : if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1240 : 16 : val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
1241 : 16 : TREE_TYPE (type), val);
1242 : : else
1243 : : /* ??? Condition vectorization expects us to do
1244 : : promotion of invariant/external defs. */
1245 : 2 : val = gimple_convert (&stmts, TREE_TYPE (type), val);
1246 : 36 : for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
1247 : 36 : !gsi_end_p (gsi2); )
1248 : : {
1249 : 18 : init_stmt = gsi_stmt (gsi2);
1250 : 18 : gsi_remove (&gsi2, false);
1251 : 18 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1252 : : }
1253 : : }
1254 : : }
1255 : 1349 : val = build_vector_from_val (type, val);
1256 : : }
1257 : :
1258 : 4539 : new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1259 : 4539 : init_stmt = gimple_build_assign (new_temp, val);
1260 : 4539 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1261 : 4539 : return new_temp;
1262 : : }
1263 : :
1264 : :
1265 : : /* Get vectorized definitions for OP0 and OP1. */
1266 : :
1267 : : void
1268 : 178792 : vect_get_vec_defs (vec_info *, slp_tree slp_node,
1269 : : tree op0, vec<tree> *vec_oprnds0,
1270 : : tree op1, vec<tree> *vec_oprnds1,
1271 : : tree op2, vec<tree> *vec_oprnds2,
1272 : : tree op3, vec<tree> *vec_oprnds3)
1273 : : {
1274 : 178792 : if (op0)
1275 : 177352 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
1276 : 178792 : if (op1)
1277 : 133240 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
1278 : 178792 : if (op2)
1279 : 8571 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
1280 : 178792 : if (op3)
1281 : 0 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
1282 : 178792 : }
1283 : :
1284 : : /* Helper function called by vect_finish_replace_stmt and
1285 : : vect_finish_stmt_generation. Set the location of the new
1286 : : statement and create and return a stmt_vec_info for it. */
1287 : :
1288 : : static void
1289 : 1347164 : vect_finish_stmt_generation_1 (vec_info *,
1290 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1291 : : {
1292 : 1347164 : if (dump_enabled_p ())
1293 : 139262 : dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
1294 : :
1295 : 1347164 : if (stmt_info)
1296 : : {
1297 : 1316776 : gimple_set_location (vec_stmt, gimple_location (stmt_info->stmt));
1298 : :
1299 : : /* While EH edges will generally prevent vectorization, stmt might
1300 : : e.g. be in a must-not-throw region. Ensure newly created stmts
1301 : : that could throw are part of the same region. */
1302 : 1316776 : int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
1303 : 1316776 : if (lp_nr != 0 && stmt_could_throw_p (cfun, vec_stmt))
1304 : 48 : add_stmt_to_eh_lp (vec_stmt, lp_nr);
1305 : : }
1306 : : else
1307 : 30388 : gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
1308 : 1347164 : }
1309 : :
1310 : : /* Replace the scalar statement STMT_INFO with a new vector statement VEC_STMT,
1311 : : which sets the same scalar result as STMT_INFO did. Create and return a
1312 : : stmt_vec_info for VEC_STMT. */
1313 : :
1314 : : void
1315 : 840 : vect_finish_replace_stmt (vec_info *vinfo,
1316 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1317 : : {
1318 : 840 : gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
1319 : 840 : gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
1320 : :
1321 : 840 : gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
1322 : 840 : gsi_replace (&gsi, vec_stmt, true);
1323 : :
1324 : 840 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1325 : 840 : }
1326 : :
1327 : : /* Add VEC_STMT to the vectorized implementation of STMT_INFO and insert it
1328 : : before *GSI. Create and return a stmt_vec_info for VEC_STMT. */
1329 : :
1330 : : void
1331 : 1346324 : vect_finish_stmt_generation (vec_info *vinfo,
1332 : : stmt_vec_info stmt_info, gimple *vec_stmt,
1333 : : gimple_stmt_iterator *gsi)
1334 : : {
1335 : 1346324 : gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
1336 : :
1337 : 1346324 : if (!gsi_end_p (*gsi)
1338 : 2691603 : && gimple_has_mem_ops (vec_stmt))
1339 : : {
1340 : 1345279 : gimple *at_stmt = gsi_stmt (*gsi);
1341 : 1345279 : tree vuse = gimple_vuse (at_stmt);
1342 : 1338792 : if (vuse && TREE_CODE (vuse) == SSA_NAME)
1343 : : {
1344 : 1207721 : tree vdef = gimple_vdef (at_stmt);
1345 : 1207721 : gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1346 : 1207721 : gimple_set_modified (vec_stmt, true);
1347 : : /* If we have an SSA vuse and insert a store, update virtual
1348 : : SSA form to avoid triggering the renamer. Do so only
1349 : : if we can easily see all uses - which is what almost always
1350 : : happens with the way vectorized stmts are inserted. */
1351 : 748181 : if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1352 : 1955866 : && ((is_gimple_assign (vec_stmt)
1353 : 747316 : && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1354 : 63022 : || (is_gimple_call (vec_stmt)
1355 : 829 : && (!(gimple_call_flags (vec_stmt)
1356 : 829 : & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
1357 : 1 : || (gimple_call_lhs (vec_stmt)
1358 : 1 : && !is_gimple_reg (gimple_call_lhs (vec_stmt)))))))
1359 : : {
1360 : 685951 : tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1361 : 685951 : gimple_set_vdef (vec_stmt, new_vdef);
1362 : 685951 : SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1363 : : }
1364 : : }
1365 : : }
1366 : 1346324 : gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1367 : 1346324 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1368 : 1346324 : }
1369 : :
1370 : : /* We want to vectorize a call to combined function CFN with function
1371 : : decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1372 : : as the types of all inputs. Check whether this is possible using
1373 : : an internal function, returning its code if so or IFN_LAST if not. */
1374 : :
1375 : : static internal_fn
1376 : 11869 : vectorizable_internal_function (combined_fn cfn, tree fndecl,
1377 : : tree vectype_out, tree vectype_in)
1378 : : {
1379 : 11869 : internal_fn ifn;
1380 : 11869 : if (internal_fn_p (cfn))
1381 : 9532 : ifn = as_internal_fn (cfn);
1382 : : else
1383 : 2337 : ifn = associated_internal_fn (fndecl);
1384 : 11869 : if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1385 : : {
1386 : 8439 : const direct_internal_fn_info &info = direct_internal_fn (ifn);
1387 : 8439 : if (info.vectorizable)
1388 : : {
1389 : 8439 : bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
1390 : 8439 : tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1391 : 8439 : tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
1392 : :
1393 : : /* The type size of both the vectype_in and vectype_out should be
1394 : : exactly the same when vectype_out isn't participating the optab.
1395 : : While there is no restriction for type size when vectype_out
1396 : : is part of the optab query. */
1397 : 8439 : if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
1398 : : return IFN_LAST;
1399 : :
1400 : 8419 : if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1401 : : OPTIMIZE_FOR_SPEED))
1402 : : return ifn;
1403 : : }
1404 : : }
1405 : : return IFN_LAST;
1406 : : }
1407 : :
1408 : :
1409 : : static tree permute_vec_elements (vec_info *, tree, tree, tree, stmt_vec_info,
1410 : : gimple_stmt_iterator *);
1411 : :
1412 : : /* Check whether a load or store statement in the loop described by
1413 : : LOOP_VINFO is possible in a loop using partial vectors. This is
1414 : : testing whether the vectorizer pass has the appropriate support,
1415 : : as well as whether the target does.
1416 : :
1417 : : VLS_TYPE says whether the statement is a load or store and VECTYPE
1418 : : is the type of the vector being loaded or stored. SLP_NODE is the SLP
1419 : : node that contains the statement, or null if none. MEMORY_ACCESS_TYPE
1420 : : says how the load or store is going to be implemented and GROUP_SIZE
1421 : : is the number of load or store statements in the containing group.
1422 : : If the access is a gather load or scatter store, GS_INFO describes
1423 : : its arguments. If the load or store is conditional, SCALAR_MASK is the
1424 : : condition under which it occurs.
1425 : :
1426 : : Clear LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P if a loop using partial
1427 : : vectors is not supported, otherwise record the required rgroup control
1428 : : types.
1429 : :
1430 : : If partial vectors can be used and ELSVALS is nonzero the supported
1431 : : else values will be added to the vector ELSVALS points to. */
1432 : :
1433 : : static void
1434 : 127 : check_load_store_for_partial_vectors (loop_vec_info loop_vinfo, tree vectype,
1435 : : slp_tree slp_node,
1436 : : vec_load_store_type vls_type,
1437 : : int group_size,
1438 : : vect_load_store_data *ls,
1439 : : slp_tree mask_node,
1440 : : vec<int> *elsvals = nullptr)
1441 : : {
1442 : 127 : vect_memory_access_type memory_access_type = ls->memory_access_type;
1443 : :
1444 : : /* Invariant loads need no special support. */
1445 : 127 : if (memory_access_type == VMAT_INVARIANT)
1446 : 2 : return;
1447 : :
1448 : : /* Figure whether the mask is uniform. scalar_mask is used to
1449 : : populate the scalar_cond_masked_set. */
1450 : 125 : tree scalar_mask = NULL_TREE;
1451 : 125 : if (mask_node)
1452 : 20 : for (unsigned i = 0; i < SLP_TREE_LANES (mask_node); ++i)
1453 : : {
1454 : 14 : tree def = vect_get_slp_scalar_def (mask_node, i);
1455 : 14 : if (!def
1456 : 14 : || (scalar_mask && def != scalar_mask))
1457 : : {
1458 : : scalar_mask = NULL;
1459 : : break;
1460 : : }
1461 : : else
1462 : 10 : scalar_mask = def;
1463 : : }
1464 : :
1465 : 125 : unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node);
1466 : 125 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1467 : 125 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
1468 : 125 : machine_mode vecmode = TYPE_MODE (vectype);
1469 : 125 : bool is_load = (vls_type == VLS_LOAD);
1470 : 125 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
1471 : : {
1472 : 0 : nvectors /= group_size;
1473 : 0 : internal_fn ifn
1474 : 0 : = (is_load ? vect_load_lanes_supported (vectype, group_size, true,
1475 : : elsvals)
1476 : 0 : : vect_store_lanes_supported (vectype, group_size, true));
1477 : 0 : if (ifn == IFN_MASK_LEN_LOAD_LANES || ifn == IFN_MASK_LEN_STORE_LANES)
1478 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1479 : 0 : else if (ifn == IFN_MASK_LOAD_LANES || ifn == IFN_MASK_STORE_LANES)
1480 : 0 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1481 : : scalar_mask);
1482 : : else
1483 : : {
1484 : 0 : if (dump_enabled_p ())
1485 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1486 : : "can't operate on partial vectors because"
1487 : : " the target doesn't have an appropriate"
1488 : : " load/store-lanes instruction.\n");
1489 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1490 : : }
1491 : 0 : return;
1492 : : }
1493 : :
1494 : 125 : if (mat_gather_scatter_p (memory_access_type))
1495 : : {
1496 : 0 : internal_fn ifn = (is_load
1497 : 0 : ? IFN_MASK_GATHER_LOAD
1498 : : : IFN_MASK_SCATTER_STORE);
1499 : 0 : internal_fn len_ifn = (is_load
1500 : : ? IFN_MASK_LEN_GATHER_LOAD
1501 : : : IFN_MASK_LEN_SCATTER_STORE);
1502 : 0 : stmt_vec_info repr = SLP_TREE_REPRESENTATIVE (slp_node);
1503 : 0 : tree off_vectype = (STMT_VINFO_GATHER_SCATTER_P (repr)
1504 : 0 : ? SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (slp_node)[0])
1505 : 0 : : ls->strided_offset_vectype);
1506 : 0 : tree memory_type = TREE_TYPE (DR_REF (STMT_VINFO_DR_INFO (repr)->dr));
1507 : 0 : int scale = SLP_TREE_GS_SCALE (slp_node);
1508 : 0 : if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
1509 : : memory_type,
1510 : : off_vectype, scale,
1511 : : elsvals))
1512 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1513 : 0 : else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
1514 : : memory_type,
1515 : : off_vectype, scale,
1516 : : elsvals)
1517 : 0 : || memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
1518 : 0 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1519 : : scalar_mask);
1520 : : else
1521 : : {
1522 : 0 : if (dump_enabled_p ())
1523 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1524 : : "can't operate on partial vectors because"
1525 : : " the target doesn't have an appropriate"
1526 : : " gather load or scatter store instruction.\n");
1527 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1528 : : }
1529 : 0 : return;
1530 : : }
1531 : :
1532 : 125 : if (memory_access_type != VMAT_CONTIGUOUS)
1533 : : {
1534 : : /* Element X of the data must come from iteration i * VF + X of the
1535 : : scalar loop. We need more work to support other mappings. */
1536 : 0 : if (dump_enabled_p ())
1537 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1538 : : "can't operate on partial vectors because an"
1539 : : " access isn't contiguous.\n");
1540 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1541 : 0 : return;
1542 : : }
1543 : :
1544 : 125 : if (!VECTOR_MODE_P (vecmode))
1545 : : {
1546 : 0 : if (dump_enabled_p ())
1547 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1548 : : "can't operate on partial vectors when emulating"
1549 : : " vector operations.\n");
1550 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1551 : 0 : return;
1552 : : }
1553 : :
1554 : : /* We might load more scalars than we need for permuting SLP loads.
1555 : : We checked in get_load_store_type that the extra elements
1556 : : don't leak into a new vector. */
1557 : 211 : auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
1558 : : {
1559 : 86 : unsigned int nvectors;
1560 : 172 : if (can_div_away_from_zero_p (size, nunits, &nvectors))
1561 : 86 : return nvectors;
1562 : : gcc_unreachable ();
1563 : : };
1564 : :
1565 : 125 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1566 : 125 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1567 : 125 : machine_mode mask_mode;
1568 : 125 : machine_mode vmode;
1569 : 125 : bool using_partial_vectors_p = false;
1570 : 125 : if (get_len_load_store_mode
1571 : 125 : (vecmode, is_load, nullptr, elsvals).exists (&vmode))
1572 : : {
1573 : 0 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1574 : 0 : unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
1575 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
1576 : 0 : using_partial_vectors_p = true;
1577 : : }
1578 : 211 : else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
1579 : 125 : && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
1580 : : elsvals))
1581 : : {
1582 : 86 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1583 : 86 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
1584 : 86 : using_partial_vectors_p = true;
1585 : : }
1586 : :
1587 : 86 : if (!using_partial_vectors_p)
1588 : : {
1589 : 39 : if (dump_enabled_p ())
1590 : 18 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1591 : : "can't operate on partial vectors because the"
1592 : : " target doesn't have the appropriate partial"
1593 : : " vectorization load or store.\n");
1594 : 39 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1595 : : }
1596 : : }
1597 : :
1598 : : /* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1599 : : form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1600 : : that needs to be applied to all loads and stores in a vectorized loop.
1601 : : Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
1602 : : otherwise return VEC_MASK & LOOP_MASK.
1603 : :
1604 : : MASK_TYPE is the type of both masks. If new statements are needed,
1605 : : insert them before GSI. */
1606 : :
1607 : : tree
1608 : 1486 : prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
1609 : : tree vec_mask, gimple_stmt_iterator *gsi)
1610 : : {
1611 : 1486 : gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1612 : 1486 : if (!loop_mask)
1613 : : return vec_mask;
1614 : :
1615 : 102 : gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1616 : :
1617 : 102 : if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
1618 : : return vec_mask;
1619 : :
1620 : 102 : tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1621 : 102 : gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1622 : : vec_mask, loop_mask);
1623 : :
1624 : 102 : gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1625 : 102 : return and_res;
1626 : : }
1627 : :
1628 : : /* Determine whether we can use a gather load or scatter store to vectorize
1629 : : strided load or store STMT_INFO by truncating the current offset to a
1630 : : smaller width. We need to be able to construct an offset vector:
1631 : :
1632 : : { 0, X, X*2, X*3, ... }
1633 : :
1634 : : without loss of precision, where X is STMT_INFO's DR_STEP.
1635 : :
1636 : : Return true if this is possible, describing the gather load or scatter
1637 : : store in GS_INFO. MASKED_P is true if the load or store is conditional.
1638 : :
1639 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1640 : : else values will be stored in the vector ELSVALS points to. */
1641 : :
1642 : : static bool
1643 : 58993 : vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info, tree vectype,
1644 : : loop_vec_info loop_vinfo, bool masked_p,
1645 : : gather_scatter_info *gs_info,
1646 : : vec<int> *elsvals)
1647 : : {
1648 : 58993 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1649 : 58993 : data_reference *dr = dr_info->dr;
1650 : 58993 : tree step = DR_STEP (dr);
1651 : 58993 : if (TREE_CODE (step) != INTEGER_CST)
1652 : : {
1653 : : /* ??? Perhaps we could use range information here? */
1654 : 29017 : if (dump_enabled_p ())
1655 : 217 : dump_printf_loc (MSG_NOTE, vect_location,
1656 : : "cannot truncate variable step.\n");
1657 : 29017 : return false;
1658 : : }
1659 : :
1660 : : /* Get the number of bits in an element. */
1661 : 29976 : scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
1662 : 29976 : unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
1663 : :
1664 : : /* Set COUNT to the upper limit on the number of elements - 1.
1665 : : Start with the maximum vectorization factor. */
1666 : 29976 : unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
1667 : :
1668 : : /* Try lowering COUNT to the number of scalar latch iterations. */
1669 : 29976 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1670 : 29976 : widest_int max_iters;
1671 : 29976 : if (max_loop_iterations (loop, &max_iters)
1672 : 59952 : && max_iters < count)
1673 : 2316 : count = max_iters.to_shwi ();
1674 : :
1675 : : /* Try scales of 1 and the element size. */
1676 : 29976 : unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
1677 : 29976 : wi::overflow_type overflow = wi::OVF_NONE;
1678 : 89928 : for (int i = 0; i < 2; ++i)
1679 : : {
1680 : 59952 : unsigned int scale = scales[i];
1681 : 59952 : widest_int factor;
1682 : 59952 : if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
1683 : 0 : continue;
1684 : :
1685 : : /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE. */
1686 : 59952 : widest_int range = wi::mul (count, factor, SIGNED, &overflow);
1687 : 59952 : if (overflow)
1688 : 0 : continue;
1689 : 59952 : signop sign = range >= 0 ? UNSIGNED : SIGNED;
1690 : 59952 : unsigned int min_offset_bits = wi::min_precision (range, sign);
1691 : :
1692 : : /* Find the narrowest viable offset type. */
1693 : 59952 : unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
1694 : 59952 : tree offset_type = build_nonstandard_integer_type (offset_bits,
1695 : : sign == UNSIGNED);
1696 : :
1697 : : /* See whether the target supports the operation with an offset
1698 : : no narrower than OFFSET_TYPE. */
1699 : 59952 : tree memory_type = TREE_TYPE (DR_REF (dr));
1700 : 59952 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
1701 : : vectype, memory_type, offset_type, scale,
1702 : : &gs_info->ifn, &gs_info->offset_vectype,
1703 : : elsvals)
1704 : 59952 : || gs_info->ifn == IFN_LAST)
1705 : 59952 : continue;
1706 : :
1707 : 0 : gs_info->decl = NULL_TREE;
1708 : : /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
1709 : : but we don't need to store that here. */
1710 : 0 : gs_info->base = NULL_TREE;
1711 : 0 : gs_info->alias_ptr = build_int_cst
1712 : 0 : (reference_alias_ptr_type (DR_REF (dr)),
1713 : 0 : get_object_alignment (DR_REF (dr)));
1714 : 0 : gs_info->element_type = TREE_TYPE (vectype);
1715 : 0 : gs_info->offset = fold_convert (offset_type, step);
1716 : 0 : gs_info->scale = scale;
1717 : 0 : gs_info->memory_type = memory_type;
1718 : 0 : return true;
1719 : 119904 : }
1720 : :
1721 : 29976 : if (overflow && dump_enabled_p ())
1722 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1723 : : "truncating gather/scatter offset to %d bits"
1724 : : " might change its value.\n", element_bits);
1725 : :
1726 : : return false;
1727 : 29976 : }
1728 : :
1729 : : /* Return true if we can use gather/scatter or strided internal functions
1730 : : to vectorize STMT_INFO, which is a grouped or strided load or store
1731 : : with multiple lanes and will be implemented by a type-punned access
1732 : : of a vector with element size that matches the number of lanes.
1733 : :
1734 : : MASKED_P is true if load or store is conditional.
1735 : : When returning true, fill in GS_INFO with the information required to
1736 : : perform the operation. Also, store the punning type in PUNNED_VECTYPE.
1737 : :
1738 : : If successful and ELSVALS is nonzero the supported
1739 : : else values will be stored in the vector ELSVALS points to. */
1740 : :
1741 : : static bool
1742 : 3804 : vect_use_grouped_gather (dr_vec_info *dr_info, tree vectype,
1743 : : loop_vec_info loop_vinfo, bool masked_p,
1744 : : unsigned int nelts,
1745 : : gather_scatter_info *info, vec<int> *elsvals,
1746 : : tree *pun_vectype)
1747 : : {
1748 : 3804 : data_reference *dr = dr_info->dr;
1749 : :
1750 : : /* TODO: We can support nelts > BITS_PER_UNIT or non-power-of-two by
1751 : : multiple gathers/scatter. */
1752 : 7324 : if (nelts > BITS_PER_UNIT || !pow2p_hwi (nelts))
1753 : : return false;
1754 : :
1755 : : /* Pun the vectype with one of the same size but an element spanning
1756 : : NELTS elements of VECTYPE.
1757 : : The punned type of a V16QI with NELTS = 4 would be V4SI.
1758 : : */
1759 : 3290 : tree tmp;
1760 : 3290 : unsigned int pieces;
1761 : 3290 : if (!can_div_trunc_p (TYPE_VECTOR_SUBPARTS (vectype), nelts, &pieces)
1762 : 3290 : || !pieces)
1763 : 221 : return false;
1764 : :
1765 : 3069 : *pun_vectype = vector_vector_composition_type (vectype, pieces, &tmp, true);
1766 : :
1767 : 3069 : if (!*pun_vectype || !VECTOR_TYPE_P (*pun_vectype))
1768 : : return false;
1769 : :
1770 : 2649 : internal_fn ifn;
1771 : 2649 : tree offset_vectype = *pun_vectype;
1772 : :
1773 : 1579 : internal_fn strided_ifn = DR_IS_READ (dr)
1774 : 2649 : ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
1775 : :
1776 : : /* Check if we have a gather/scatter with the new type. We're just trying
1777 : : with the type itself as offset for now. If not, check if we have a
1778 : : strided load/store. These have fewer constraints (for example no offset
1779 : : type must exist) so it is possible that even though a gather/scatter is
1780 : : not available we still have a strided load/store. */
1781 : 2649 : bool ok = false;
1782 : 2649 : if (vect_gather_scatter_fn_p
1783 : 2649 : (loop_vinfo, DR_IS_READ (dr), masked_p, *pun_vectype,
1784 : 2649 : TREE_TYPE (*pun_vectype), *pun_vectype, 1, &ifn,
1785 : : &offset_vectype, elsvals))
1786 : : ok = true;
1787 : 2649 : else if (internal_strided_fn_supported_p (strided_ifn, *pun_vectype,
1788 : : elsvals))
1789 : : {
1790 : : /* Use gather/scatter IFNs, vect_get_strided_load_store_ops
1791 : : will switch back to the strided variants. */
1792 : 0 : ifn = DR_IS_READ (dr) ? IFN_MASK_LEN_GATHER_LOAD :
1793 : : IFN_MASK_LEN_SCATTER_STORE;
1794 : 0 : ok = true;
1795 : : }
1796 : :
1797 : 0 : if (ok)
1798 : : {
1799 : 0 : info->ifn = ifn;
1800 : 0 : info->decl = NULL_TREE;
1801 : 0 : info->base = dr->ref;
1802 : 0 : info->alias_ptr = build_int_cst
1803 : 0 : (reference_alias_ptr_type (DR_REF (dr)),
1804 : 0 : get_object_alignment (DR_REF (dr)));
1805 : 0 : info->element_type = TREE_TYPE (*pun_vectype);
1806 : 0 : info->offset_vectype = offset_vectype;
1807 : : /* No need to set the offset, vect_get_strided_load_store_ops
1808 : : will do that. */
1809 : 0 : info->scale = 1;
1810 : 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
1811 : 0 : return true;
1812 : : }
1813 : :
1814 : : return false;
1815 : : }
1816 : :
1817 : :
1818 : : /* Return true if we can use gather/scatter internal functions to
1819 : : vectorize STMT_INFO, which is a grouped or strided load or store.
1820 : : MASKED_P is true if load or store is conditional. When returning
1821 : : true, fill in GS_INFO with the information required to perform the
1822 : : operation.
1823 : :
1824 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1825 : : else values will be stored in the vector ELSVALS points to. */
1826 : :
1827 : : static bool
1828 : 58993 : vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info, tree vectype,
1829 : : loop_vec_info loop_vinfo, bool masked_p,
1830 : : gather_scatter_info *gs_info,
1831 : : vec<int> *elsvals,
1832 : : unsigned int group_size,
1833 : : bool single_element_p)
1834 : : {
1835 : 58993 : if (!vect_check_gather_scatter (stmt_info, vectype,
1836 : : loop_vinfo, gs_info, elsvals)
1837 : 58993 : || gs_info->ifn == IFN_LAST)
1838 : : {
1839 : 58993 : if (!vect_truncate_gather_scatter_offset (stmt_info, vectype, loop_vinfo,
1840 : : masked_p, gs_info, elsvals))
1841 : : return false;
1842 : : }
1843 : : else
1844 : : {
1845 : 0 : tree old_offset_type = TREE_TYPE (gs_info->offset);
1846 : 0 : tree new_offset_type = TREE_TYPE (gs_info->offset_vectype);
1847 : :
1848 : 0 : gcc_assert (TYPE_PRECISION (new_offset_type)
1849 : : >= TYPE_PRECISION (old_offset_type));
1850 : 0 : gs_info->offset = fold_convert (new_offset_type, gs_info->offset);
1851 : : }
1852 : :
1853 : 0 : if (!single_element_p
1854 : 0 : && !targetm.vectorize.prefer_gather_scatter (TYPE_MODE (vectype),
1855 : : gs_info->scale,
1856 : : group_size))
1857 : : return false;
1858 : :
1859 : 0 : if (dump_enabled_p ())
1860 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1861 : : "using gather/scatter for strided/grouped access,"
1862 : : " scale = %d\n", gs_info->scale);
1863 : :
1864 : : return true;
1865 : : }
1866 : :
1867 : : /* STMT_INFO is a non-strided load or store, meaning that it accesses
1868 : : elements with a known constant step. Return -1 if that step
1869 : : is negative, 0 if it is zero, and 1 if it is greater than zero. */
1870 : :
1871 : : int
1872 : 1332520 : compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
1873 : : {
1874 : 1332520 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1875 : 1332520 : return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
1876 : 1332520 : size_zero_node);
1877 : : }
1878 : :
1879 : : /* If the target supports a permute mask that reverses the elements in
1880 : : a vector of type VECTYPE, return that mask, otherwise return null. */
1881 : :
1882 : : tree
1883 : 8795 : perm_mask_for_reverse (tree vectype)
1884 : : {
1885 : 8795 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1886 : :
1887 : : /* The encoding has a single stepped pattern. */
1888 : 8795 : vec_perm_builder sel (nunits, 1, 3);
1889 : 35180 : for (int i = 0; i < 3; ++i)
1890 : 26385 : sel.quick_push (nunits - 1 - i);
1891 : :
1892 : 8795 : vec_perm_indices indices (sel, 1, nunits);
1893 : 8795 : if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
1894 : : indices))
1895 : : return NULL_TREE;
1896 : 7719 : return vect_gen_perm_mask_checked (vectype, indices);
1897 : 8795 : }
1898 : :
1899 : : /* A subroutine of get_load_store_type, with a subset of the same
1900 : : arguments. Handle the case where STMT_INFO is a load or store that
1901 : : accesses consecutive elements with a negative step. Sets *POFFSET
1902 : : to the offset to be applied to the DR for the first access. */
1903 : :
1904 : : static vect_memory_access_type
1905 : 10137 : get_negative_load_store_type (vec_info *vinfo,
1906 : : stmt_vec_info stmt_info, tree vectype,
1907 : : vec_load_store_type vls_type,
1908 : : unsigned int ncopies, poly_int64 *poffset)
1909 : : {
1910 : 10137 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1911 : 10137 : dr_alignment_support alignment_support_scheme;
1912 : :
1913 : 10137 : if (ncopies > 1)
1914 : : {
1915 : 0 : if (dump_enabled_p ())
1916 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1917 : : "multiple types with negative step.\n");
1918 : 0 : return VMAT_ELEMENTWISE;
1919 : : }
1920 : :
1921 : : /* For backward running DRs the first access in vectype actually is
1922 : : N-1 elements before the address of the DR. */
1923 : 10137 : *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
1924 : 10137 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1925 : :
1926 : 10137 : int misalignment = dr_misalignment (dr_info, vectype, *poffset);
1927 : 10137 : alignment_support_scheme
1928 : 10137 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
1929 : 10137 : if (alignment_support_scheme != dr_aligned
1930 : 10137 : && alignment_support_scheme != dr_unaligned_supported)
1931 : : {
1932 : 3176 : if (dump_enabled_p ())
1933 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1934 : : "negative step but alignment required.\n");
1935 : 3176 : *poffset = 0;
1936 : 3176 : return VMAT_ELEMENTWISE;
1937 : : }
1938 : :
1939 : 6961 : if (vls_type == VLS_STORE_INVARIANT)
1940 : : {
1941 : 711 : if (dump_enabled_p ())
1942 : 21 : dump_printf_loc (MSG_NOTE, vect_location,
1943 : : "negative step with invariant source;"
1944 : : " no permute needed.\n");
1945 : 711 : return VMAT_CONTIGUOUS_DOWN;
1946 : : }
1947 : :
1948 : 6250 : if (!perm_mask_for_reverse (vectype))
1949 : : {
1950 : 1076 : if (dump_enabled_p ())
1951 : 50 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1952 : : "negative step and reversing not supported.\n");
1953 : 1076 : *poffset = 0;
1954 : 1076 : return VMAT_ELEMENTWISE;
1955 : : }
1956 : :
1957 : : return VMAT_CONTIGUOUS_REVERSE;
1958 : : }
1959 : :
1960 : : /* STMT_INFO is either a masked or unconditional store. Return the value
1961 : : being stored. */
1962 : :
1963 : : tree
1964 : 0 : vect_get_store_rhs (stmt_vec_info stmt_info)
1965 : : {
1966 : 0 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
1967 : : {
1968 : 0 : gcc_assert (gimple_assign_single_p (assign));
1969 : 0 : return gimple_assign_rhs1 (assign);
1970 : : }
1971 : 0 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
1972 : : {
1973 : 0 : internal_fn ifn = gimple_call_internal_fn (call);
1974 : 0 : int index = internal_fn_stored_value_index (ifn);
1975 : 0 : gcc_assert (index >= 0);
1976 : 0 : return gimple_call_arg (call, index);
1977 : : }
1978 : 0 : gcc_unreachable ();
1979 : : }
1980 : :
1981 : : /* Function VECTOR_VECTOR_COMPOSITION_TYPE
1982 : :
1983 : : This function returns a vector type which can be composed with NELTS pieces,
1984 : : whose type is recorded in PTYPE. VTYPE should be a vector type, and has the
1985 : : same vector size as the return vector. It checks target whether supports
1986 : : pieces-size vector mode for construction firstly, if target fails to, check
1987 : : pieces-size scalar mode for construction further. It returns NULL_TREE if
1988 : : fails to find the available composition. If the caller only wants scalar
1989 : : pieces where PTYPE e.g. is a possible gather/scatter element type
1990 : : SCALAR_PTYPE_ONLY must be true.
1991 : :
1992 : : For example, for (vtype=V16QI, nelts=4), we can probably get:
1993 : : - V16QI with PTYPE V4QI.
1994 : : - V4SI with PTYPE SI.
1995 : : - NULL_TREE. */
1996 : :
1997 : : static tree
1998 : 12749 : vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype,
1999 : : bool scalar_ptype_only)
2000 : : {
2001 : 12749 : gcc_assert (VECTOR_TYPE_P (vtype));
2002 : 12749 : gcc_assert (known_gt (nelts, 0U));
2003 : :
2004 : 12749 : machine_mode vmode = TYPE_MODE (vtype);
2005 : 12749 : if (!VECTOR_MODE_P (vmode))
2006 : : return NULL_TREE;
2007 : :
2008 : : /* When we are asked to compose the vector from its components let
2009 : : that happen directly. */
2010 : 12749 : if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
2011 : : {
2012 : 5127 : *ptype = TREE_TYPE (vtype);
2013 : 5127 : return vtype;
2014 : : }
2015 : :
2016 : 15244 : poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
2017 : 7622 : unsigned int pbsize;
2018 : 7622 : if (constant_multiple_p (vbsize, nelts, &pbsize))
2019 : : {
2020 : : /* First check if vec_init optab supports construction from
2021 : : vector pieces directly. */
2022 : 7622 : scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
2023 : 15244 : poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
2024 : 7622 : machine_mode rmode;
2025 : 7622 : if (!scalar_ptype_only
2026 : 4553 : && related_vector_mode (vmode, elmode, inelts).exists (&rmode)
2027 : 11679 : && (convert_optab_handler (vec_init_optab, vmode, rmode)
2028 : : != CODE_FOR_nothing))
2029 : : {
2030 : 3401 : *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
2031 : 3401 : return vtype;
2032 : : }
2033 : :
2034 : : /* Otherwise check if exists an integer type of the same piece size and
2035 : : if vec_init optab supports construction from it directly. */
2036 : 4221 : if (int_mode_for_size (pbsize, 0).exists (&elmode)
2037 : 4221 : && related_vector_mode (vmode, elmode, nelts).exists (&rmode))
2038 : : {
2039 : 3753 : if (scalar_ptype_only
2040 : 3753 : || convert_optab_handler (vec_init_optab, rmode, elmode)
2041 : : != CODE_FOR_nothing)
2042 : : {
2043 : 3753 : *ptype = build_nonstandard_integer_type (pbsize, 1);
2044 : 3753 : return build_vector_type (*ptype, nelts);
2045 : : }
2046 : : }
2047 : : }
2048 : :
2049 : : return NULL_TREE;
2050 : : }
2051 : :
2052 : : /* Analyze load or store SLP_NODE of type VLS_TYPE. Return true
2053 : : if there is a memory access type that the vectorized form can use,
2054 : : storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2055 : : or scatters, fill in GS_INFO accordingly. In addition
2056 : : *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
2057 : : the target does not support the alignment scheme. *MISALIGNMENT
2058 : : is set according to the alignment of the access (including
2059 : : DR_MISALIGNMENT_UNKNOWN when it is unknown).
2060 : :
2061 : : MASKED_P is true if the statement is conditional on a vectorized mask.
2062 : : VECTYPE is the vector type that the vectorized statements will use.
2063 : :
2064 : : If ELSVALS is nonzero the supported else values will be stored in the
2065 : : vector ELSVALS points to. */
2066 : :
2067 : : static bool
2068 : 1241797 : get_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2069 : : tree vectype, slp_tree slp_node,
2070 : : bool masked_p, vec_load_store_type vls_type,
2071 : : vect_load_store_data *ls)
2072 : : {
2073 : 1241797 : vect_memory_access_type *memory_access_type = &ls->memory_access_type;
2074 : 1241797 : poly_int64 *poffset = &ls->poffset;
2075 : 1241797 : dr_alignment_support *alignment_support_scheme
2076 : : = &ls->alignment_support_scheme;
2077 : 1241797 : int *misalignment = &ls->misalignment;
2078 : 1241797 : internal_fn *lanes_ifn = &ls->lanes_ifn;
2079 : 1241797 : vec<int> *elsvals = &ls->elsvals;
2080 : 1241797 : tree *ls_type = &ls->ls_type;
2081 : 1241797 : bool *slp_perm = &ls->slp_perm;
2082 : 1241797 : unsigned *n_perms = &ls->n_perms;
2083 : 1241797 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2084 : 1241797 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2085 : 1241797 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2086 : 1241797 : stmt_vec_info first_stmt_info;
2087 : 1241797 : unsigned int group_size;
2088 : 1241797 : unsigned HOST_WIDE_INT gap;
2089 : 1241797 : bool single_element_p;
2090 : 1241797 : poly_int64 neg_ldst_offset = 0;
2091 : :
2092 : 1241797 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2093 : 1241797 : *poffset = 0;
2094 : 1241797 : *ls_type = NULL_TREE;
2095 : 1241797 : *slp_perm = false;
2096 : 1241797 : *n_perms = -1U;
2097 : :
2098 : 1241797 : bool perm_ok = true;
2099 : 1241797 : poly_int64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
2100 : :
2101 : 1241797 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2102 : 60615 : perm_ok = vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL,
2103 : 60615 : vf, true, n_perms);
2104 : :
2105 : 1241797 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2106 : : {
2107 : 864337 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2108 : 864337 : group_size = DR_GROUP_SIZE (first_stmt_info);
2109 : 864337 : gap = DR_GROUP_GAP (first_stmt_info);
2110 : 864337 : single_element_p = (stmt_info == first_stmt_info
2111 : 864337 : && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2112 : : }
2113 : : else
2114 : : {
2115 : : first_stmt_info = stmt_info;
2116 : : group_size = 1;
2117 : : gap = 0;
2118 : : single_element_p = true;
2119 : : }
2120 : 1241797 : dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
2121 : :
2122 : : /* True if the vectorized statements would access beyond the last
2123 : : statement in the group. */
2124 : 1241797 : bool overrun_p = false;
2125 : :
2126 : : /* True if we can cope with such overrun by peeling for gaps, so that
2127 : : there is at least one final scalar iteration after the vector loop. */
2128 : 2483594 : bool can_overrun_p = (!masked_p
2129 : 1241797 : && vls_type == VLS_LOAD
2130 : 462192 : && loop_vinfo
2131 : 1573519 : && !loop->inner);
2132 : :
2133 : : /* There can only be a gap at the end of the group if the stride is
2134 : : known at compile time. */
2135 : 1241797 : gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
2136 : :
2137 : : /* For SLP vectorization we directly vectorize a subchain
2138 : : without permutation. */
2139 : 1241797 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2140 : 1181182 : first_dr_info = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
2141 : :
2142 : 1241797 : if (STMT_VINFO_STRIDED_P (first_stmt_info))
2143 : : /* Try to use consecutive accesses of as many elements as possible,
2144 : : separated by the stride, until we have a complete vector.
2145 : : Fall back to scalar accesses if that isn't possible. */
2146 : 45226 : *memory_access_type = VMAT_STRIDED_SLP;
2147 : 1196571 : else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2148 : : {
2149 : 7367 : slp_tree offset_node = SLP_TREE_CHILDREN (slp_node)[0];
2150 : 7367 : tree offset_vectype = SLP_TREE_VECTYPE (offset_node);
2151 : 7367 : int scale = SLP_TREE_GS_SCALE (slp_node);
2152 : 7367 : tree memory_type = TREE_TYPE (DR_REF (first_dr_info->dr));
2153 : 7367 : tree tem;
2154 : 7367 : if (vect_gather_scatter_fn_p (loop_vinfo, vls_type == VLS_LOAD,
2155 : : masked_p, vectype,
2156 : : memory_type,
2157 : : offset_vectype, scale,
2158 : : &ls->gs.ifn, &tem,
2159 : : elsvals))
2160 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2161 : 7367 : else if (vls_type == VLS_LOAD
2162 : 7367 : ? (targetm.vectorize.builtin_gather
2163 : 4918 : && (ls->gs.decl
2164 : 4918 : = targetm.vectorize.builtin_gather (vectype,
2165 : 4918 : TREE_TYPE
2166 : : (offset_vectype),
2167 : : scale)))
2168 : 2449 : : (targetm.vectorize.builtin_scatter
2169 : 2449 : && (ls->gs.decl
2170 : 2449 : = targetm.vectorize.builtin_scatter (vectype,
2171 : 2449 : TREE_TYPE
2172 : : (offset_vectype),
2173 : : scale))))
2174 : 349 : *memory_access_type = VMAT_GATHER_SCATTER_LEGACY;
2175 : : else
2176 : : {
2177 : : /* GATHER_SCATTER_EMULATED_P. */
2178 : 7018 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
2179 : 7018 : || !TYPE_VECTOR_SUBPARTS (offset_vectype).is_constant ()
2180 : 7018 : || VECTOR_BOOLEAN_TYPE_P (offset_vectype)
2181 : 7018 : || !constant_multiple_p (TYPE_VECTOR_SUBPARTS (offset_vectype),
2182 : 7018 : TYPE_VECTOR_SUBPARTS (vectype)))
2183 : : {
2184 : 2546 : if (dump_enabled_p ())
2185 : 442 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2186 : : "unsupported vector types for emulated "
2187 : : "gather.\n");
2188 : 2546 : return false;
2189 : : }
2190 : 4472 : *memory_access_type = VMAT_GATHER_SCATTER_EMULATED;
2191 : : }
2192 : : }
2193 : : else
2194 : : {
2195 : 1189204 : int cmp = compare_step_with_zero (vinfo, stmt_info);
2196 : 1189204 : if (cmp < 0)
2197 : : {
2198 : 10293 : if (single_element_p)
2199 : : /* ??? The VMAT_CONTIGUOUS_REVERSE code generation is
2200 : : only correct for single element "interleaving" SLP. */
2201 : 10137 : *memory_access_type = get_negative_load_store_type
2202 : 10137 : (vinfo, stmt_info, vectype, vls_type, 1,
2203 : : &neg_ldst_offset);
2204 : : else
2205 : : /* We can fall back to VMAT_STRIDED_SLP since that does
2206 : : not care whether the stride between the group instances
2207 : : is positive or negative. */
2208 : 156 : *memory_access_type = VMAT_STRIDED_SLP;
2209 : : }
2210 : 1178911 : else if (cmp == 0 && loop_vinfo)
2211 : : {
2212 : 3004 : gcc_assert (vls_type == VLS_LOAD);
2213 : 3004 : *memory_access_type = VMAT_INVARIANT;
2214 : : }
2215 : : /* Try using LOAD/STORE_LANES. */
2216 : 1175907 : else if (slp_node->ldst_lanes
2217 : 1175907 : && (*lanes_ifn
2218 : 0 : = (vls_type == VLS_LOAD
2219 : 0 : ? vect_load_lanes_supported (vectype, group_size,
2220 : : masked_p, elsvals)
2221 : 0 : : vect_store_lanes_supported (vectype, group_size,
2222 : : masked_p))) != IFN_LAST)
2223 : 0 : *memory_access_type = VMAT_LOAD_STORE_LANES;
2224 : 1175907 : else if (!loop_vinfo && slp_node->avoid_stlf_fail)
2225 : : {
2226 : 72 : *memory_access_type = VMAT_ELEMENTWISE;
2227 : 72 : if (dump_enabled_p ())
2228 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2229 : : "using element-wise load to avoid disrupting "
2230 : : "cross iteration store-to-load forwarding\n");
2231 : : }
2232 : : else
2233 : 1175835 : *memory_access_type = VMAT_CONTIGUOUS;
2234 : :
2235 : : /* If this is single-element interleaving with an element
2236 : : distance that leaves unused vector loads around fall back
2237 : : to elementwise access if possible - we otherwise least
2238 : : create very sub-optimal code in that case (and
2239 : : blow up memory, see PR65518). */
2240 : 1189204 : if (loop_vinfo
2241 : 1189204 : && single_element_p
2242 : 356101 : && (*memory_access_type == VMAT_CONTIGUOUS
2243 : 13141 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2244 : 1545305 : && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
2245 : : {
2246 : 13330 : *memory_access_type = VMAT_ELEMENTWISE;
2247 : 13330 : if (dump_enabled_p ())
2248 : 193 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2249 : : "single-element interleaving not supported "
2250 : : "for not adjacent vector loads, using "
2251 : : "elementwise access\n");
2252 : : }
2253 : :
2254 : : /* Also fall back to elementwise access in case we did not lower a
2255 : : permutation and cannot code generate it. */
2256 : 1189204 : if (loop_vinfo
2257 : 405590 : && *memory_access_type != VMAT_ELEMENTWISE
2258 : 388008 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2259 : 1210513 : && !perm_ok)
2260 : : {
2261 : 2056 : *memory_access_type = VMAT_ELEMENTWISE;
2262 : 2056 : if (dump_enabled_p ())
2263 : 211 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2264 : : "permutation not supported, using elementwise "
2265 : : "access\n");
2266 : : }
2267 : :
2268 : 405590 : overrun_p = (loop_vinfo && gap != 0
2269 : 1232353 : && *memory_access_type != VMAT_ELEMENTWISE);
2270 : 1189204 : if (overrun_p && vls_type != VLS_LOAD)
2271 : : {
2272 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2273 : : "Grouped store with gaps requires"
2274 : : " non-consecutive accesses\n");
2275 : 9 : return false;
2276 : : }
2277 : :
2278 : 1189204 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (first_dr_info);
2279 : 1189204 : poly_int64 off = 0;
2280 : 1189204 : if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2281 : 5022 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2282 : :
2283 : : /* An overrun is fine if the trailing elements are smaller
2284 : : than the alignment boundary B. Every vector access will
2285 : : be a multiple of B and so we are guaranteed to access a
2286 : : non-gap element in the same B-sized block. */
2287 : 1189204 : if (overrun_p
2288 : 1189204 : && gap < (vect_known_alignment_in_bytes (first_dr_info,
2289 : 27726 : vectype, off) / dr_size))
2290 : : overrun_p = false;
2291 : :
2292 : : /* When we have a contiguous access across loop iterations
2293 : : but the access in the loop doesn't cover the full vector
2294 : : we can end up with no gap recorded but still excess
2295 : : elements accessed, see PR103116. Make sure we peel for
2296 : : gaps if necessary and sufficient and give up if not.
2297 : :
2298 : : If there is a combination of the access not covering the full
2299 : : vector and a gap recorded then we may need to peel twice. */
2300 : 1189204 : bool large_vector_overrun_p = false;
2301 : 1189204 : if (loop_vinfo
2302 : 405590 : && (*memory_access_type == VMAT_CONTIGUOUS
2303 : 28527 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2304 : 382085 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2305 : 1208187 : && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
2306 : : nunits))
2307 : : large_vector_overrun_p = overrun_p = true;
2308 : :
2309 : : /* If the gap splits the vector in half and the target
2310 : : can do half-vector operations avoid the epilogue peeling
2311 : : by simply loading half of the vector only. Usually
2312 : : the construction with an upper zero half will be elided. */
2313 : 1189204 : dr_alignment_support alss;
2314 : 1189204 : int misalign = dr_misalignment (first_dr_info, vectype, off);
2315 : 1189204 : tree half_vtype;
2316 : 1189204 : poly_uint64 remain;
2317 : 1189204 : unsigned HOST_WIDE_INT tem, num;
2318 : 1189204 : if (overrun_p
2319 : 1189204 : && !masked_p
2320 : 22279 : && *memory_access_type != VMAT_LOAD_STORE_LANES
2321 : 22279 : && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
2322 : : vectype, misalign)))
2323 : : == dr_aligned
2324 : 17361 : || alss == dr_unaligned_supported)
2325 : 11977 : && can_div_trunc_p (group_size
2326 : 11977 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2327 : : nunits, &tem, &remain)
2328 : 1201181 : && (known_eq (remain, 0u)
2329 : 7064 : || (known_ne (remain, 0u)
2330 : 5464 : && constant_multiple_p (nunits, remain, &num)
2331 : 1184291 : && (vector_vector_composition_type (vectype, num, &half_vtype)
2332 : : != NULL_TREE))))
2333 : 10377 : overrun_p = false;
2334 : :
2335 : 1189204 : if (overrun_p && !can_overrun_p)
2336 : : {
2337 : 6 : if (dump_enabled_p ())
2338 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2339 : : "Peeling for outer loop is not supported\n");
2340 : 6 : return false;
2341 : : }
2342 : :
2343 : : /* Peeling for gaps assumes that a single scalar iteration
2344 : : is enough to make sure the last vector iteration doesn't
2345 : : access excess elements. */
2346 : 1189198 : if (overrun_p
2347 : 1189198 : && (!can_div_trunc_p (group_size
2348 : 11896 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2349 : : nunits, &tem, &remain)
2350 : 11896 : || maybe_lt (remain + group_size, nunits)))
2351 : : {
2352 : : /* But peeling a single scalar iteration is enough if
2353 : : we can use the next power-of-two sized partial
2354 : : access and that is sufficiently small to be covered
2355 : : by the single scalar iteration. */
2356 : 17 : unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
2357 : 17 : if (masked_p
2358 : 17 : || !nunits.is_constant (&cnunits)
2359 : 17 : || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
2360 : 17 : || (((cremain = (group_size * cvf - gap) % cnunits), true)
2361 : 17 : && ((cpart_size = (1 << ceil_log2 (cremain))), true)
2362 : 17 : && (cremain + group_size < cpart_size
2363 : 14 : || (vector_vector_composition_type (vectype,
2364 : 14 : cnunits / cpart_size,
2365 : : &half_vtype)
2366 : : == NULL_TREE))))
2367 : : {
2368 : : /* If all fails we can still resort to niter masking unless
2369 : : the vectors used are too big, so enforce the use of
2370 : : partial vectors. */
2371 : 3 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
2372 : 0 : && !large_vector_overrun_p)
2373 : : {
2374 : 0 : if (dump_enabled_p ())
2375 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2376 : : "peeling for gaps insufficient for "
2377 : : "access unless using partial "
2378 : : "vectors\n");
2379 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2380 : : }
2381 : : else
2382 : : {
2383 : 3 : if (dump_enabled_p ())
2384 : 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2385 : : "peeling for gaps insufficient for "
2386 : : "access\n");
2387 : 3 : return false;
2388 : : }
2389 : : }
2390 : 14 : else if (large_vector_overrun_p)
2391 : : {
2392 : 14 : if (dump_enabled_p ())
2393 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2394 : : "can't operate on partial vectors because "
2395 : : "only unmasked loads handle access "
2396 : : "shortening required because of gaps at "
2397 : : "the end of the access\n");
2398 : 14 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
2399 : : }
2400 : : }
2401 : : }
2402 : :
2403 : : /* As a last resort, trying using a gather load or scatter store.
2404 : :
2405 : : ??? Although the code can handle all group sizes correctly,
2406 : : it probably isn't a win to use separate strided accesses based
2407 : : on nearby locations. Or, even if it's a win over scalar code,
2408 : : it might not be a win over vectorizing at a lower VF, if that
2409 : : allows us to use contiguous accesses. */
2410 : 1239242 : vect_memory_access_type grouped_gather_fallback = VMAT_UNINITIALIZED;
2411 : 1239242 : if (loop_vinfo
2412 : 455628 : && (*memory_access_type == VMAT_ELEMENTWISE
2413 : 455628 : || *memory_access_type == VMAT_STRIDED_SLP)
2414 : 65016 : && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2415 : : {
2416 : 65016 : gather_scatter_info gs_info;
2417 : 65016 : if (SLP_TREE_LANES (slp_node) == 1
2418 : 61018 : && (!SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2419 : 17071 : || single_element_p)
2420 : 124009 : && vect_use_strided_gather_scatters_p (stmt_info, vectype, loop_vinfo,
2421 : : masked_p, &gs_info, elsvals,
2422 : : group_size, single_element_p))
2423 : : {
2424 : 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2425 : 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2426 : 0 : ls->gs.ifn = gs_info.ifn;
2427 : 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2428 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2429 : : }
2430 : 65016 : else if (SLP_TREE_LANES (slp_node) > 1
2431 : : && !masked_p
2432 : 3998 : && !single_element_p
2433 : 68820 : && vect_use_grouped_gather (STMT_VINFO_DR_INFO (stmt_info),
2434 : : vectype, loop_vinfo,
2435 : : masked_p, group_size,
2436 : : &gs_info, elsvals, ls_type))
2437 : : {
2438 : 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2439 : 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2440 : 0 : grouped_gather_fallback = *memory_access_type;
2441 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2442 : 0 : ls->gs.ifn = gs_info.ifn;
2443 : 0 : vectype = *ls_type;
2444 : 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2445 : : }
2446 : : }
2447 : :
2448 : 1239242 : if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
2449 : 1239242 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2450 : 5729 : *poffset = neg_ldst_offset;
2451 : :
2452 : 1239242 : if (*memory_access_type == VMAT_ELEMENTWISE
2453 : 1219532 : || *memory_access_type == VMAT_GATHER_SCATTER_LEGACY
2454 : 1219183 : || *memory_access_type == VMAT_STRIDED_SLP
2455 : 1173805 : || *memory_access_type == VMAT_INVARIANT)
2456 : : {
2457 : 68441 : *alignment_support_scheme = dr_unaligned_supported;
2458 : 68441 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2459 : : }
2460 : : else
2461 : : {
2462 : 1170801 : if (mat_gather_scatter_p (*memory_access_type)
2463 : : && !first_dr_info)
2464 : : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2465 : : else
2466 : 1170801 : *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
2467 : 1170801 : *alignment_support_scheme
2468 : 1170801 : = vect_supportable_dr_alignment
2469 : 1170801 : (vinfo, first_dr_info, vectype, *misalignment,
2470 : 1170801 : mat_gather_scatter_p (*memory_access_type));
2471 : 1170801 : if (grouped_gather_fallback != VMAT_UNINITIALIZED
2472 : 0 : && *alignment_support_scheme != dr_aligned
2473 : 0 : && *alignment_support_scheme != dr_unaligned_supported)
2474 : : {
2475 : : /* No supportable alignment for a grouped gather, fall back to the
2476 : : original memory access type. Even though VMAT_STRIDED_SLP might
2477 : : also try aligned vector loads it can still choose vector
2478 : : construction from scalars. */
2479 : 0 : *memory_access_type = grouped_gather_fallback;
2480 : 0 : *alignment_support_scheme = dr_unaligned_supported;
2481 : 0 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2482 : : }
2483 : : }
2484 : :
2485 : 1239242 : if (overrun_p)
2486 : : {
2487 : 11893 : gcc_assert (can_overrun_p);
2488 : 11893 : if (dump_enabled_p ())
2489 : 495 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2490 : : "Data access with gaps requires scalar "
2491 : : "epilogue loop\n");
2492 : 11893 : LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2493 : : }
2494 : :
2495 : 1239242 : if ((*memory_access_type == VMAT_ELEMENTWISE
2496 : 1239242 : || *memory_access_type == VMAT_STRIDED_SLP)
2497 : : && !nunits.is_constant ())
2498 : : {
2499 : : if (dump_enabled_p ())
2500 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2501 : : "Not using elementwise accesses due to variable "
2502 : : "vectorization factor.\n");
2503 : : return false;
2504 : : }
2505 : :
2506 : : /* Checks if all scalar iterations are known to be inbounds. */
2507 : 1239242 : bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
2508 : :
2509 : : /* Check if we support the operation if early breaks are needed. Here we
2510 : : must ensure that we don't access any more than the scalar code would
2511 : : have. A masked operation would ensure this, so for these load types
2512 : : force masking. */
2513 : 1239242 : if (loop_vinfo
2514 : 455628 : && dr_safe_speculative_read_required (stmt_info)
2515 : 163948 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
2516 : 1403190 : && (mat_gather_scatter_p (*memory_access_type)
2517 : 163540 : || *memory_access_type == VMAT_STRIDED_SLP))
2518 : : {
2519 : 6228 : if (dump_enabled_p ())
2520 : 8 : dump_printf_loc (MSG_NOTE, vect_location,
2521 : : "early break not supported: cannot peel for "
2522 : : "alignment. With non-contiguous memory vectorization"
2523 : : " could read out of bounds at %G ",
2524 : : STMT_VINFO_STMT (stmt_info));
2525 : 6228 : if (inbounds)
2526 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2527 : : else
2528 : : return false;
2529 : : }
2530 : :
2531 : : /* If this DR needs alignment for correctness, we must ensure the target
2532 : : alignment is a constant power-of-two multiple of the amount read per
2533 : : vector iteration or force masking. */
2534 : 1233014 : if (dr_safe_speculative_read_required (stmt_info)
2535 : 1233014 : && (*alignment_support_scheme == dr_aligned
2536 : 86164 : && !mat_gather_scatter_p (*memory_access_type)))
2537 : : {
2538 : : /* We can only peel for loops, of course. */
2539 : 86164 : gcc_checking_assert (loop_vinfo);
2540 : :
2541 : 86164 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2542 : 86164 : poly_uint64 read_amount
2543 : 86164 : = vf * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2544 : 86164 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2545 : 86164 : read_amount *= group_size;
2546 : :
2547 : 86164 : auto target_alignment
2548 : 86164 : = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
2549 : 86164 : if (!multiple_p (target_alignment, read_amount))
2550 : : {
2551 : 5338 : if (dump_enabled_p ())
2552 : : {
2553 : 10 : dump_printf_loc (MSG_NOTE, vect_location,
2554 : : "desired alignment not met, target was ");
2555 : 10 : dump_dec (MSG_NOTE, target_alignment);
2556 : 10 : dump_printf (MSG_NOTE, " previously, but read amount is ");
2557 : 10 : dump_dec (MSG_NOTE, read_amount);
2558 : 10 : dump_printf (MSG_NOTE, " at %G.\n", STMT_VINFO_STMT (stmt_info));
2559 : : }
2560 : 6530 : return false;
2561 : : }
2562 : :
2563 : : /* When using a group access the first element may be aligned but the
2564 : : subsequent loads may not be. For LOAD_LANES since the loads are based
2565 : : on the first DR then all loads in the group are aligned. For
2566 : : non-LOAD_LANES this is not the case. In particular a load + blend when
2567 : : there are gaps can have the non first loads issued unaligned, even
2568 : : partially overlapping the memory of the first load in order to simplify
2569 : : the blend. This is what the x86_64 backend does for instance. As
2570 : : such only the first load in the group is aligned, the rest are not.
2571 : : Because of this the permutes may break the alignment requirements that
2572 : : have been set, and as such we should for now, reject them. */
2573 : 80826 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2574 : : {
2575 : 1192 : if (dump_enabled_p ())
2576 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2577 : : "loads with load permutations not supported for "
2578 : : "speculative early break loads for %G",
2579 : : STMT_VINFO_STMT (stmt_info));
2580 : 1192 : return false;
2581 : : }
2582 : :
2583 : : /* Reject vectorization if we know the read mount per vector iteration
2584 : : exceeds the min page size. */
2585 : 79634 : if (known_gt (read_amount, (unsigned) param_min_pagesize))
2586 : : {
2587 : 0 : if (dump_enabled_p ())
2588 : : {
2589 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2590 : : "alignment required for correctness (");
2591 : 0 : dump_dec (MSG_MISSED_OPTIMIZATION, read_amount);
2592 : 0 : dump_printf (MSG_NOTE, ") may exceed page size.\n");
2593 : : }
2594 : 0 : return false;
2595 : : }
2596 : :
2597 : 79634 : if (!vf.is_constant ())
2598 : : {
2599 : : /* For VLA modes, we need a runtime check to ensure any speculative
2600 : : read amount does not exceed the page size. Here we record the max
2601 : : possible read amount for the check. */
2602 : : if (maybe_gt (read_amount,
2603 : : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo)))
2604 : : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo) = read_amount;
2605 : :
2606 : : /* For VLA modes, we must use partial vectors. */
2607 : : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2608 : : }
2609 : : }
2610 : :
2611 : 1226484 : if (*alignment_support_scheme == dr_unaligned_unsupported)
2612 : : {
2613 : 58947 : if (dump_enabled_p ())
2614 : 270 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2615 : : "unsupported unaligned access\n");
2616 : 58947 : return false;
2617 : : }
2618 : :
2619 : : /* FIXME: At the moment the cost model seems to underestimate the
2620 : : cost of using elementwise accesses. This check preserves the
2621 : : traditional behavior until that can be fixed. */
2622 : 1167537 : if (*memory_access_type == VMAT_ELEMENTWISE
2623 : 19710 : && !STMT_VINFO_STRIDED_P (first_stmt_info)
2624 : 1187247 : && !(STMT_VINFO_GROUPED_ACCESS (stmt_info)
2625 : 15829 : && single_element_p
2626 : 15206 : && !pow2p_hwi (group_size)))
2627 : : {
2628 : 9279 : if (dump_enabled_p ())
2629 : 348 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2630 : : "not falling back to elementwise accesses\n");
2631 : 9279 : return false;
2632 : : }
2633 : :
2634 : : /* For BB vectorization build up the vector from existing scalar defs. */
2635 : 1158258 : if (!loop_vinfo && *memory_access_type == VMAT_ELEMENTWISE)
2636 : : return false;
2637 : :
2638 : : /* Some loads need to explicitly permute the loaded data if there
2639 : : is a load permutation. Among those are:
2640 : : - VMAT_ELEMENTWISE.
2641 : : - VMAT_STRIDED_SLP.
2642 : : - VMAT_GATHER_SCATTER:
2643 : : - Strided gather (fallback for VMAT_STRIDED_SLP if #lanes == 1).
2644 : : - Grouped strided gather (ditto but for #lanes > 1).
2645 : :
2646 : : For VMAT_ELEMENTWISE we can fold the load permutation into the
2647 : : individual indices we access directly, eliding the permutation.
2648 : : Strided gather only allows load permutations for the
2649 : : single-element case. */
2650 : :
2651 : 1158258 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2652 : 1158258 : && !(*memory_access_type == VMAT_ELEMENTWISE
2653 : 39035 : || (mat_gather_scatter_p (*memory_access_type)
2654 : 0 : && SLP_TREE_LANES (slp_node) == 1
2655 : 0 : && single_element_p)))
2656 : : {
2657 : 39035 : if (!loop_vinfo)
2658 : : {
2659 : : /* In BB vectorization we may not actually use a loaded vector
2660 : : accessing elements in excess of DR_GROUP_SIZE. */
2661 : 23546 : stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2662 : 23546 : group_info = DR_GROUP_FIRST_ELEMENT (group_info);
2663 : 23546 : unsigned HOST_WIDE_INT nunits;
2664 : 23546 : unsigned j, k, maxk = 0;
2665 : 84546 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
2666 : 61000 : if (k > maxk)
2667 : : maxk = k;
2668 : 23546 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2669 : 42573 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
2670 : 23546 : || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
2671 : : {
2672 : 4519 : if (dump_enabled_p ())
2673 : 31 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2674 : : "BB vectorization with gaps at the end of "
2675 : : "a load is not supported\n");
2676 : 4519 : return false;
2677 : : }
2678 : : }
2679 : :
2680 : 34516 : if (!perm_ok)
2681 : : {
2682 : 2011 : if (dump_enabled_p ())
2683 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
2684 : : vect_location,
2685 : : "unsupported load permutation\n");
2686 : 2011 : return false;
2687 : : }
2688 : :
2689 : 32505 : *slp_perm = true;
2690 : : }
2691 : :
2692 : : return true;
2693 : : }
2694 : :
2695 : : /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
2696 : : conditional operation STMT_INFO. When returning true, store the mask
2697 : : in *MASK_NODE, the type of its definition in *MASK_DT_OUT and the type of
2698 : : the vectorized mask in *MASK_VECTYPE_OUT. */
2699 : :
2700 : : static bool
2701 : 7602 : vect_check_scalar_mask (vec_info *vinfo,
2702 : : slp_tree slp_node, unsigned mask_index,
2703 : : slp_tree *mask_node,
2704 : : vect_def_type *mask_dt_out, tree *mask_vectype_out)
2705 : : {
2706 : 7602 : enum vect_def_type mask_dt;
2707 : 7602 : tree mask_vectype;
2708 : 7602 : slp_tree mask_node_1;
2709 : 7602 : tree mask_;
2710 : 7602 : if (!vect_is_simple_use (vinfo, slp_node, mask_index,
2711 : : &mask_, &mask_node_1, &mask_dt, &mask_vectype))
2712 : : {
2713 : 0 : if (dump_enabled_p ())
2714 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2715 : : "mask use not simple.\n");
2716 : 0 : return false;
2717 : : }
2718 : :
2719 : 7602 : if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
2720 : 7602 : && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (mask_)))
2721 : : {
2722 : 0 : if (dump_enabled_p ())
2723 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2724 : : "mask argument is not a boolean.\n");
2725 : 0 : return false;
2726 : : }
2727 : :
2728 : 7602 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2729 : 7602 : if (!mask_vectype)
2730 : 17 : mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
2731 : : mask_node_1);
2732 : :
2733 : 7602 : if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2734 : : {
2735 : 0 : if (dump_enabled_p ())
2736 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2737 : : "could not find an appropriate vector mask type.\n");
2738 : 0 : return false;
2739 : : }
2740 : :
2741 : 7602 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2742 : 15204 : TYPE_VECTOR_SUBPARTS (vectype)))
2743 : : {
2744 : 0 : if (dump_enabled_p ())
2745 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2746 : : "vector mask type %T"
2747 : : " does not match vector data type %T.\n",
2748 : : mask_vectype, vectype);
2749 : :
2750 : 0 : return false;
2751 : : }
2752 : :
2753 : 7602 : *mask_dt_out = mask_dt;
2754 : 7602 : *mask_vectype_out = mask_vectype;
2755 : 7602 : *mask_node = mask_node_1;
2756 : 7602 : return true;
2757 : : }
2758 : :
2759 : :
2760 : : /* Return true if stored value is suitable for vectorizing store
2761 : : statement STMT_INFO. When returning true, store the scalar stored
2762 : : in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
2763 : : the type of the vectorized store value in
2764 : : *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
2765 : :
2766 : : static bool
2767 : 1321704 : vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
2768 : : slp_tree slp_node, slp_tree *rhs_node,
2769 : : vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
2770 : : vec_load_store_type *vls_type_out)
2771 : : {
2772 : 1321704 : int op_no = 0;
2773 : 1321704 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2774 : : {
2775 : 1443 : if (gimple_call_internal_p (call)
2776 : 1443 : && internal_store_fn_p (gimple_call_internal_fn (call)))
2777 : 1443 : op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
2778 : : }
2779 : 1321704 : op_no = vect_slp_child_index_for_operand
2780 : 1321704 : (stmt_info->stmt, op_no, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
2781 : :
2782 : 1321704 : enum vect_def_type rhs_dt;
2783 : 1321704 : tree rhs_vectype;
2784 : 1321704 : tree rhs;
2785 : 1321704 : if (!vect_is_simple_use (vinfo, slp_node, op_no,
2786 : : &rhs, rhs_node, &rhs_dt, &rhs_vectype))
2787 : : {
2788 : 0 : if (dump_enabled_p ())
2789 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2790 : : "use not simple.\n");
2791 : 0 : return false;
2792 : : }
2793 : :
2794 : : /* In the case this is a store from a constant make sure
2795 : : native_encode_expr can handle it. */
2796 : 1321704 : if (rhs_dt == vect_constant_def
2797 : 1321704 : && CONSTANT_CLASS_P (rhs) && native_encode_expr (rhs, NULL, 64) == 0)
2798 : : {
2799 : 0 : if (dump_enabled_p ())
2800 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2801 : : "cannot encode constant as a byte sequence.\n");
2802 : 0 : return false;
2803 : : }
2804 : :
2805 : 1321704 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2806 : 1321704 : if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2807 : : {
2808 : 24 : if (dump_enabled_p ())
2809 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2810 : : "incompatible vector types.\n");
2811 : 24 : return false;
2812 : : }
2813 : :
2814 : 1321680 : *rhs_dt_out = rhs_dt;
2815 : 1321680 : *rhs_vectype_out = rhs_vectype;
2816 : 1321680 : if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
2817 : 998998 : *vls_type_out = VLS_STORE_INVARIANT;
2818 : : else
2819 : 322682 : *vls_type_out = VLS_STORE;
2820 : : return true;
2821 : : }
2822 : :
2823 : : /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
2824 : : Note that we support masks with floating-point type, in which case the
2825 : : floats are interpreted as a bitmask. */
2826 : :
2827 : : static tree
2828 : 165 : vect_build_all_ones_mask (vec_info *vinfo,
2829 : : stmt_vec_info stmt_info, tree masktype)
2830 : : {
2831 : 165 : if (TREE_CODE (masktype) == INTEGER_TYPE)
2832 : 98 : return build_int_cst (masktype, -1);
2833 : 67 : else if (VECTOR_BOOLEAN_TYPE_P (masktype)
2834 : 134 : || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2835 : : {
2836 : 14 : tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2837 : 14 : mask = build_vector_from_val (masktype, mask);
2838 : 14 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2839 : : }
2840 : 53 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2841 : : {
2842 : : REAL_VALUE_TYPE r;
2843 : : long tmp[6];
2844 : 371 : for (int j = 0; j < 6; ++j)
2845 : 318 : tmp[j] = -1;
2846 : 53 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2847 : 53 : tree mask = build_real (TREE_TYPE (masktype), r);
2848 : 53 : mask = build_vector_from_val (masktype, mask);
2849 : 53 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2850 : : }
2851 : 0 : gcc_unreachable ();
2852 : : }
2853 : :
2854 : : /* Build an all-zero merge value of type VECTYPE while vectorizing
2855 : : STMT_INFO as a gather load. */
2856 : :
2857 : : static tree
2858 : 158 : vect_build_zero_merge_argument (vec_info *vinfo,
2859 : : stmt_vec_info stmt_info, tree vectype)
2860 : : {
2861 : 158 : tree merge;
2862 : 158 : if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2863 : 49 : merge = build_int_cst (TREE_TYPE (vectype), 0);
2864 : 109 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2865 : : {
2866 : : REAL_VALUE_TYPE r;
2867 : : long tmp[6];
2868 : 763 : for (int j = 0; j < 6; ++j)
2869 : 654 : tmp[j] = 0;
2870 : 109 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2871 : 109 : merge = build_real (TREE_TYPE (vectype), r);
2872 : : }
2873 : : else
2874 : 0 : gcc_unreachable ();
2875 : 158 : merge = build_vector_from_val (vectype, merge);
2876 : 158 : return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
2877 : : }
2878 : :
2879 : : /* Return the corresponding else value for an else value constant
2880 : : ELSVAL with type TYPE. */
2881 : :
2882 : : tree
2883 : 1671 : vect_get_mask_load_else (int elsval, tree type)
2884 : : {
2885 : 1671 : tree els;
2886 : 1671 : if (elsval == MASK_LOAD_ELSE_UNDEFINED)
2887 : : {
2888 : 0 : tree tmp = create_tmp_var (type);
2889 : : /* No need to warn about anything. */
2890 : 0 : TREE_NO_WARNING (tmp) = 1;
2891 : 0 : els = get_or_create_ssa_default_def (cfun, tmp);
2892 : : }
2893 : 1671 : else if (elsval == MASK_LOAD_ELSE_M1)
2894 : 0 : els = build_minus_one_cst (type);
2895 : 1671 : else if (elsval == MASK_LOAD_ELSE_ZERO)
2896 : 1671 : els = build_zero_cst (type);
2897 : : else
2898 : 0 : gcc_unreachable ();
2899 : :
2900 : 1671 : return els;
2901 : : }
2902 : :
2903 : : /* Build a gather load call while vectorizing STMT_INFO. Insert new
2904 : : instructions before GSI and add them to VEC_STMT. GS_INFO describes
2905 : : the gather load operation. If the load is conditional, MASK is the
2906 : : vectorized condition, otherwise MASK is null. PTR is the base
2907 : : pointer and OFFSET is the vectorized offset. */
2908 : :
2909 : : static gimple *
2910 : 346 : vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
2911 : : slp_tree slp_node, tree vectype,
2912 : : gimple_stmt_iterator *gsi, tree decl,
2913 : : tree ptr, tree offset, tree mask)
2914 : : {
2915 : 346 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
2916 : 346 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
2917 : 346 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2918 : 346 : /* ptrtype */ arglist = TREE_CHAIN (arglist);
2919 : 346 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2920 : 346 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2921 : 346 : tree scaletype = TREE_VALUE (arglist);
2922 : 346 : tree var;
2923 : 346 : gcc_checking_assert (types_compatible_p (srctype, rettype)
2924 : : && (!mask
2925 : : || TREE_CODE (masktype) == INTEGER_TYPE
2926 : : || types_compatible_p (srctype, masktype)));
2927 : :
2928 : 346 : tree op = offset;
2929 : 346 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
2930 : : {
2931 : 100 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
2932 : : TYPE_VECTOR_SUBPARTS (idxtype)));
2933 : 100 : var = vect_get_new_ssa_name (idxtype, vect_simple_var);
2934 : 100 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
2935 : 100 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2936 : 100 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2937 : 100 : op = var;
2938 : : }
2939 : :
2940 : 346 : tree src_op = NULL_TREE;
2941 : 346 : tree mask_op = NULL_TREE;
2942 : 346 : if (mask)
2943 : : {
2944 : 188 : if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
2945 : : {
2946 : 188 : tree utype, optype = TREE_TYPE (mask);
2947 : 188 : if (VECTOR_TYPE_P (masktype)
2948 : 188 : || TYPE_MODE (masktype) == TYPE_MODE (optype))
2949 : : utype = masktype;
2950 : : else
2951 : 7 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
2952 : 188 : var = vect_get_new_ssa_name (utype, vect_scalar_var);
2953 : 188 : tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
2954 : 188 : gassign *new_stmt
2955 : 188 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
2956 : 188 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2957 : 188 : mask_arg = var;
2958 : 188 : if (!useless_type_conversion_p (masktype, utype))
2959 : : {
2960 : 7 : gcc_assert (TYPE_PRECISION (utype)
2961 : : <= TYPE_PRECISION (masktype));
2962 : 7 : var = vect_get_new_ssa_name (masktype, vect_scalar_var);
2963 : 7 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
2964 : 7 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2965 : 7 : mask_arg = var;
2966 : : }
2967 : 188 : src_op = build_zero_cst (srctype);
2968 : 188 : mask_op = mask_arg;
2969 : : }
2970 : : else
2971 : : {
2972 : : src_op = mask;
2973 : : mask_op = mask;
2974 : : }
2975 : : }
2976 : : else
2977 : : {
2978 : 158 : src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
2979 : 158 : mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
2980 : : }
2981 : :
2982 : 346 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
2983 : 346 : gimple *new_stmt = gimple_build_call (decl, 5, src_op, ptr, op,
2984 : : mask_op, scale);
2985 : :
2986 : 346 : if (!useless_type_conversion_p (vectype, rettype))
2987 : : {
2988 : 49 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
2989 : : TYPE_VECTOR_SUBPARTS (rettype)));
2990 : 49 : op = vect_get_new_ssa_name (rettype, vect_simple_var);
2991 : 49 : gimple_call_set_lhs (new_stmt, op);
2992 : 49 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2993 : 49 : op = build1 (VIEW_CONVERT_EXPR, vectype, op);
2994 : 49 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
2995 : : }
2996 : :
2997 : 346 : return new_stmt;
2998 : : }
2999 : :
3000 : : /* Build a scatter store call while vectorizing STMT_INFO. Insert new
3001 : : instructions before GSI. GS_INFO describes the scatter store operation.
3002 : : PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
3003 : : vectorized data to store.
3004 : : If the store is conditional, MASK is the vectorized condition, otherwise
3005 : : MASK is null. */
3006 : :
3007 : : static gimple *
3008 : 169 : vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
3009 : : slp_tree slp_node,
3010 : : gimple_stmt_iterator *gsi,
3011 : : tree decl,
3012 : : tree ptr, tree offset, tree oprnd, tree mask)
3013 : : {
3014 : 169 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
3015 : 169 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
3016 : 169 : /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
3017 : 169 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3018 : 169 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3019 : 169 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3020 : 169 : tree scaletype = TREE_VALUE (arglist);
3021 : 169 : gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
3022 : : && TREE_CODE (rettype) == VOID_TYPE);
3023 : :
3024 : 169 : tree mask_arg = NULL_TREE;
3025 : 169 : if (mask)
3026 : : {
3027 : 110 : mask_arg = mask;
3028 : 110 : tree optype = TREE_TYPE (mask_arg);
3029 : 110 : tree utype;
3030 : 110 : if (TYPE_MODE (masktype) == TYPE_MODE (optype))
3031 : : utype = masktype;
3032 : : else
3033 : 8 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3034 : 110 : tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
3035 : 110 : mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
3036 : 110 : gassign *new_stmt
3037 : 110 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3038 : 110 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3039 : 110 : mask_arg = var;
3040 : 110 : if (!useless_type_conversion_p (masktype, utype))
3041 : : {
3042 : 8 : gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
3043 : 8 : tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3044 : 8 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3045 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3046 : 8 : mask_arg = var;
3047 : : }
3048 : : }
3049 : : else
3050 : : {
3051 : 59 : mask_arg = build_int_cst (masktype, -1);
3052 : 59 : mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
3053 : : }
3054 : :
3055 : 169 : tree src = oprnd;
3056 : 169 : if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
3057 : : {
3058 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
3059 : : TYPE_VECTOR_SUBPARTS (srctype)));
3060 : 0 : tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
3061 : 0 : src = build1 (VIEW_CONVERT_EXPR, srctype, src);
3062 : 0 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
3063 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3064 : 0 : src = var;
3065 : : }
3066 : :
3067 : 169 : tree op = offset;
3068 : 169 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3069 : : {
3070 : 24 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3071 : : TYPE_VECTOR_SUBPARTS (idxtype)));
3072 : 24 : tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3073 : 24 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3074 : 24 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3075 : 24 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3076 : 24 : op = var;
3077 : : }
3078 : :
3079 : 169 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
3080 : 169 : gcall *new_stmt
3081 : 169 : = gimple_build_call (decl, 5, ptr, mask_arg, op, src, scale);
3082 : 169 : return new_stmt;
3083 : : }
3084 : :
3085 : : /* Prepare the base and offset in GS_INFO for vectorization.
3086 : : Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
3087 : : to the vectorized offset argument for the first copy of STMT_INFO.
3088 : : STMT_INFO is the statement described by GS_INFO and LOOP is the
3089 : : containing loop. */
3090 : :
3091 : : static void
3092 : 1221 : vect_get_gather_scatter_ops (class loop *loop, slp_tree slp_node,
3093 : : tree *dataref_ptr, vec<tree> *vec_offset)
3094 : : {
3095 : 1221 : gimple_seq stmts = NULL;
3096 : 1221 : *dataref_ptr = force_gimple_operand (SLP_TREE_GS_BASE (slp_node),
3097 : : &stmts, true, NULL_TREE);
3098 : 1221 : if (stmts != NULL)
3099 : : {
3100 : 986 : basic_block new_bb;
3101 : 986 : edge pe = loop_preheader_edge (loop);
3102 : 986 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3103 : 986 : gcc_assert (!new_bb);
3104 : : }
3105 : 1221 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
3106 : 1221 : }
3107 : :
3108 : : /* Prepare to implement a grouped or strided load or store using
3109 : : the gather load or scatter store operation described by GS_INFO.
3110 : : STMT_INFO is the load or store statement.
3111 : :
3112 : : Set *DATAREF_BUMP to the amount that should be added to the base
3113 : : address after each copy of the vectorized statement. Set *VEC_OFFSET
3114 : : to an invariant offset vector in which element I has the value
3115 : : I * DR_STEP / SCALE. */
3116 : :
3117 : : static void
3118 : 0 : vect_get_strided_load_store_ops (stmt_vec_info stmt_info, slp_tree node,
3119 : : tree vectype, tree offset_vectype,
3120 : : loop_vec_info loop_vinfo,
3121 : : gimple_stmt_iterator *gsi,
3122 : : tree *dataref_bump, tree *vec_offset,
3123 : : vec_loop_lens *loop_lens)
3124 : : {
3125 : 0 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3126 : :
3127 : 0 : if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3128 : : {
3129 : : /* _31 = .SELECT_VL (ivtmp_29, POLY_INT_CST [4, 4]);
3130 : : ivtmp_8 = _31 * 16 (step in bytes);
3131 : : .MASK_LEN_SCATTER_STORE (vectp_a.9_7, ... );
3132 : : vectp_a.9_26 = vectp_a.9_7 + ivtmp_8; */
3133 : 0 : tree loop_len
3134 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, vectype, 0, 0);
3135 : 0 : tree tmp
3136 : 0 : = fold_build2 (MULT_EXPR, sizetype,
3137 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3138 : : loop_len);
3139 : 0 : *dataref_bump = force_gimple_operand_gsi (gsi, tmp, true, NULL_TREE, true,
3140 : : GSI_SAME_STMT);
3141 : : }
3142 : : else
3143 : : {
3144 : 0 : tree bump
3145 : 0 : = size_binop (MULT_EXPR,
3146 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3147 : : size_int (TYPE_VECTOR_SUBPARTS (vectype)));
3148 : 0 : *dataref_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
3149 : : }
3150 : :
3151 : 0 : internal_fn ifn
3152 : 0 : = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
3153 : 0 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
3154 : : {
3155 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
3156 : : unshare_expr (DR_STEP (dr)));
3157 : 0 : return;
3158 : : }
3159 : :
3160 : : /* The offset given in GS_INFO can have pointer type, so use the element
3161 : : type of the vector instead. */
3162 : 0 : tree offset_type = TREE_TYPE (offset_vectype);
3163 : :
3164 : : /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
3165 : 0 : tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
3166 : : ssize_int (SLP_TREE_GS_SCALE (node)));
3167 : 0 : step = fold_convert (offset_type, step);
3168 : :
3169 : : /* Create {0, X, X*2, X*3, ...}. */
3170 : 0 : tree offset = fold_build2 (VEC_SERIES_EXPR, offset_vectype,
3171 : : build_zero_cst (offset_type), step);
3172 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
3173 : : }
3174 : :
3175 : : /* Prepare the pointer IVs which needs to be updated by a variable amount.
3176 : : Such variable amount is the outcome of .SELECT_VL. In this case, we can
3177 : : allow each iteration process the flexible number of elements as long as
3178 : : the number <= vf elments.
3179 : :
3180 : : Return data reference according to SELECT_VL.
3181 : : If new statements are needed, insert them before GSI. */
3182 : :
3183 : : static tree
3184 : 0 : vect_get_loop_variant_data_ptr_increment (
3185 : : vec_info *vinfo, tree aggr_type, gimple_stmt_iterator *gsi,
3186 : : vec_loop_lens *loop_lens, dr_vec_info *dr_info,
3187 : : vect_memory_access_type memory_access_type)
3188 : : {
3189 : 0 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3190 : 0 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3191 : :
3192 : : /* gather/scatter never reach here. */
3193 : 0 : gcc_assert (!mat_gather_scatter_p (memory_access_type));
3194 : :
3195 : : /* When we support SELECT_VL pattern, we dynamic adjust
3196 : : the memory address by .SELECT_VL result.
3197 : :
3198 : : The result of .SELECT_VL is the number of elements to
3199 : : be processed of each iteration. So the memory address
3200 : : adjustment operation should be:
3201 : :
3202 : : addr = addr + .SELECT_VL (ARG..) * step;
3203 : : */
3204 : 0 : tree loop_len
3205 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, aggr_type, 0, 0);
3206 : 0 : tree len_type = TREE_TYPE (loop_len);
3207 : : /* Since the outcome of .SELECT_VL is element size, we should adjust
3208 : : it into bytesize so that it can be used in address pointer variable
3209 : : amount IVs adjustment. */
3210 : 0 : tree tmp = fold_build2 (MULT_EXPR, len_type, loop_len,
3211 : : wide_int_to_tree (len_type, wi::to_widest (step)));
3212 : 0 : tree bump = make_temp_ssa_name (len_type, NULL, "ivtmp");
3213 : 0 : gassign *assign = gimple_build_assign (bump, tmp);
3214 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3215 : 0 : return bump;
3216 : : }
3217 : :
3218 : : /* Return the amount that should be added to a vector pointer to move
3219 : : to the next or previous copy of AGGR_TYPE. DR_INFO is the data reference
3220 : : being vectorized and MEMORY_ACCESS_TYPE describes the type of
3221 : : vectorization. */
3222 : :
3223 : : static tree
3224 : 698865 : vect_get_data_ptr_increment (vec_info *vinfo, gimple_stmt_iterator *gsi,
3225 : : dr_vec_info *dr_info, tree aggr_type,
3226 : : vect_memory_access_type memory_access_type,
3227 : : vec_loop_lens *loop_lens = nullptr)
3228 : : {
3229 : 698865 : if (memory_access_type == VMAT_INVARIANT)
3230 : 0 : return size_zero_node;
3231 : :
3232 : 698865 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3233 : 132190 : if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3234 : 0 : return vect_get_loop_variant_data_ptr_increment (vinfo, aggr_type, gsi,
3235 : : loop_lens, dr_info,
3236 : 0 : memory_access_type);
3237 : :
3238 : 698865 : tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3239 : 698865 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3240 : 698865 : if (tree_int_cst_sgn (step) == -1)
3241 : 2806 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3242 : : return iv_step;
3243 : : }
3244 : :
3245 : : /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}. */
3246 : :
3247 : : static bool
3248 : 196 : vectorizable_bswap (vec_info *vinfo,
3249 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3250 : : slp_tree slp_node,
3251 : : slp_tree *slp_op,
3252 : : tree vectype_in, stmt_vector_for_cost *cost_vec)
3253 : : {
3254 : 196 : tree op, vectype;
3255 : 196 : gcall *stmt = as_a <gcall *> (stmt_info->stmt);
3256 : :
3257 : 196 : op = gimple_call_arg (stmt, 0);
3258 : 196 : vectype = SLP_TREE_VECTYPE (slp_node);
3259 : 196 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3260 : :
3261 : 196 : if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
3262 : : {
3263 : 0 : if (dump_enabled_p ())
3264 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3265 : : "mismatched vector sizes %T and %T\n",
3266 : : vectype_in, vectype);
3267 : 0 : return false;
3268 : : }
3269 : :
3270 : 196 : tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
3271 : 196 : if (! char_vectype)
3272 : : return false;
3273 : :
3274 : 196 : poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
3275 : 196 : unsigned word_bytes;
3276 : 196 : if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
3277 : : return false;
3278 : :
3279 : : /* The encoding uses one stepped pattern for each byte in the word. */
3280 : 196 : vec_perm_builder elts (num_bytes, word_bytes, 3);
3281 : 784 : for (unsigned i = 0; i < 3; ++i)
3282 : 3228 : for (unsigned j = 0; j < word_bytes; ++j)
3283 : 2640 : elts.quick_push ((i + 1) * word_bytes - j - 1);
3284 : :
3285 : 196 : vec_perm_indices indices (elts, 1, num_bytes);
3286 : 196 : machine_mode vmode = TYPE_MODE (char_vectype);
3287 : 196 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3288 : : return false;
3289 : :
3290 : 152 : if (cost_vec)
3291 : : {
3292 : 140 : if (!vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
3293 : : {
3294 : 0 : if (dump_enabled_p ())
3295 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3296 : : "incompatible vector types for invariants\n");
3297 : 0 : return false;
3298 : : }
3299 : :
3300 : 140 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3301 : 140 : DUMP_VECT_SCOPE ("vectorizable_bswap");
3302 : 140 : record_stmt_cost (cost_vec,
3303 : : 1, vector_stmt, slp_node, 0, vect_prologue);
3304 : 140 : record_stmt_cost (cost_vec,
3305 : 140 : vect_get_num_copies (vinfo, slp_node),
3306 : : vec_perm, slp_node, 0, vect_body);
3307 : 140 : return true;
3308 : : }
3309 : :
3310 : 12 : tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
3311 : :
3312 : : /* Transform. */
3313 : 12 : vec<tree> vec_oprnds = vNULL;
3314 : 12 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
3315 : : /* Arguments are ready. create the new vector stmt. */
3316 : 12 : unsigned i;
3317 : 12 : tree vop;
3318 : 24 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3319 : : {
3320 : 12 : gimple *new_stmt;
3321 : 12 : tree tem = make_ssa_name (char_vectype);
3322 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3323 : : char_vectype, vop));
3324 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3325 : 12 : tree tem2 = make_ssa_name (char_vectype);
3326 : 12 : new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3327 : : tem, tem, bswap_vconst);
3328 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3329 : 12 : tem = make_ssa_name (vectype);
3330 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3331 : : vectype, tem2));
3332 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3333 : 12 : slp_node->push_vec_def (new_stmt);
3334 : : }
3335 : :
3336 : 12 : vec_oprnds.release ();
3337 : 12 : return true;
3338 : 196 : }
3339 : :
3340 : : /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3341 : : integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3342 : : in a single step. On success, store the binary pack code in
3343 : : *CONVERT_CODE. */
3344 : :
3345 : : static bool
3346 : 156 : simple_integer_narrowing (tree vectype_out, tree vectype_in,
3347 : : code_helper *convert_code)
3348 : : {
3349 : 312 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3350 : 312 : || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3351 : : return false;
3352 : :
3353 : 66 : code_helper code;
3354 : 66 : int multi_step_cvt = 0;
3355 : 66 : auto_vec <tree, 8> interm_types;
3356 : 97 : if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3357 : : &code, &multi_step_cvt, &interm_types)
3358 : 66 : || multi_step_cvt)
3359 : 31 : return false;
3360 : :
3361 : 35 : *convert_code = code;
3362 : 35 : return true;
3363 : 66 : }
3364 : :
3365 : : /* Function vectorizable_call.
3366 : :
3367 : : Check if STMT_INFO performs a function call that can be vectorized.
3368 : : If COST_VEC is passed, calculate costs but don't change anything,
3369 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
3370 : : it, and insert it at GSI.
3371 : : Return true if STMT_INFO is vectorizable in this way. */
3372 : :
3373 : : static bool
3374 : 2300382 : vectorizable_call (vec_info *vinfo,
3375 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3376 : : slp_tree slp_node,
3377 : : stmt_vector_for_cost *cost_vec)
3378 : : {
3379 : 2300382 : gcall *stmt;
3380 : 2300382 : tree vec_dest;
3381 : 2300382 : tree scalar_dest;
3382 : 2300382 : tree op;
3383 : 2300382 : tree vec_oprnd0 = NULL_TREE;
3384 : 2300382 : tree vectype_out, vectype_in;
3385 : 2300382 : poly_uint64 nunits_in;
3386 : 2300382 : poly_uint64 nunits_out;
3387 : 2300382 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3388 : 2300382 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3389 : 2300382 : tree fndecl, new_temp, rhs_type;
3390 : 2300382 : enum vect_def_type dt[4]
3391 : : = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3392 : : vect_unknown_def_type };
3393 : 2300382 : tree vectypes[ARRAY_SIZE (dt)] = {};
3394 : 2300382 : slp_tree slp_op[ARRAY_SIZE (dt)] = {};
3395 : 2300382 : auto_vec<tree, 8> vargs;
3396 : 2300382 : enum { NARROW, NONE, WIDEN } modifier;
3397 : 2300382 : size_t i, nargs;
3398 : 2300382 : tree clz_ctz_arg1 = NULL_TREE;
3399 : :
3400 : 2300382 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3401 : : return false;
3402 : :
3403 : 2300382 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3404 : 175983 : && cost_vec)
3405 : : return false;
3406 : :
3407 : : /* Is STMT_INFO a vectorizable call? */
3408 : 2309649 : stmt = dyn_cast <gcall *> (stmt_info->stmt);
3409 : 18839 : if (!stmt)
3410 : : return false;
3411 : :
3412 : 18839 : if (gimple_call_internal_p (stmt)
3413 : 18839 : && (internal_load_fn_p (gimple_call_internal_fn (stmt))
3414 : 11752 : || internal_store_fn_p (gimple_call_internal_fn (stmt))))
3415 : : /* Handled by vectorizable_load and vectorizable_store. */
3416 : 2635 : return false;
3417 : :
3418 : 16204 : if (gimple_call_lhs (stmt) == NULL_TREE
3419 : 16204 : || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3420 : : return false;
3421 : :
3422 : 16198 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
3423 : :
3424 : 16198 : vectype_out = SLP_TREE_VECTYPE (slp_node);
3425 : :
3426 : : /* Process function arguments. */
3427 : 16198 : rhs_type = NULL_TREE;
3428 : 16198 : vectype_in = NULL_TREE;
3429 : 16198 : nargs = gimple_call_num_args (stmt);
3430 : :
3431 : : /* Bail out if the function has more than four arguments, we do not have
3432 : : interesting builtin functions to vectorize with more than two arguments
3433 : : except for fma. No arguments is also not good. */
3434 : 16198 : if (nargs == 0 || nargs > 4)
3435 : : return false;
3436 : :
3437 : : /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic. */
3438 : 16118 : combined_fn cfn = gimple_call_combined_fn (stmt);
3439 : 16118 : if (cfn == CFN_GOMP_SIMD_LANE)
3440 : : {
3441 : 3203 : nargs = 0;
3442 : 3203 : rhs_type = unsigned_type_node;
3443 : : }
3444 : : /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
3445 : : argument just says whether it is well-defined at zero or not and what
3446 : : value should be returned for it. */
3447 : 16118 : if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
3448 : : {
3449 : 118 : nargs = 1;
3450 : 118 : clz_ctz_arg1 = gimple_call_arg (stmt, 1);
3451 : : }
3452 : :
3453 : 16118 : int mask_opno = -1;
3454 : 16118 : if (internal_fn_p (cfn))
3455 : : {
3456 : : /* We can only handle direct internal masked calls here,
3457 : : vectorizable_simd_clone_call is for the rest. */
3458 : 13308 : if (cfn == CFN_MASK_CALL)
3459 : : return false;
3460 : 13183 : mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3461 : : }
3462 : :
3463 : 44178 : for (i = 0; i < nargs; i++)
3464 : : {
3465 : 29239 : if ((int) i == mask_opno)
3466 : : {
3467 : 3825 : if (!vect_check_scalar_mask (vinfo, slp_node, mask_opno,
3468 : : &slp_op[i], &dt[i], &vectypes[i]))
3469 : : return false;
3470 : 3825 : continue;
3471 : : }
3472 : :
3473 : 25414 : if (!vect_is_simple_use (vinfo, slp_node,
3474 : : i, &op, &slp_op[i], &dt[i], &vectypes[i]))
3475 : : {
3476 : 0 : if (dump_enabled_p ())
3477 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3478 : : "use not simple.\n");
3479 : 0 : return false;
3480 : : }
3481 : :
3482 : : /* We can only handle calls with arguments of the same type. */
3483 : 25414 : if (rhs_type
3484 : 25414 : && !types_compatible_p (rhs_type, TREE_TYPE (op)))
3485 : : {
3486 : 1054 : if (dump_enabled_p ())
3487 : 200 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3488 : : "argument types differ.\n");
3489 : 1054 : return false;
3490 : : }
3491 : 24360 : if (!rhs_type)
3492 : 12790 : rhs_type = TREE_TYPE (op);
3493 : :
3494 : 24360 : if (!vectype_in)
3495 : 13262 : vectype_in = vectypes[i];
3496 : 11098 : else if (vectypes[i]
3497 : 11098 : && !types_compatible_p (vectypes[i], vectype_in))
3498 : : {
3499 : 0 : if (dump_enabled_p ())
3500 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3501 : : "argument vector types differ.\n");
3502 : 0 : return false;
3503 : : }
3504 : : }
3505 : : /* If all arguments are external or constant defs, infer the vector type
3506 : : from the scalar type. */
3507 : 14939 : if (!vectype_in)
3508 : 5530 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
3509 : 14939 : if (!cost_vec)
3510 : 4127 : gcc_assert (vectype_in);
3511 : 10812 : if (!vectype_in)
3512 : : {
3513 : 1062 : if (dump_enabled_p ())
3514 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3515 : : "no vectype for scalar type %T\n", rhs_type);
3516 : :
3517 : 1062 : return false;
3518 : : }
3519 : :
3520 : 27754 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3521 : 13877 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
3522 : : {
3523 : 12 : if (dump_enabled_p ())
3524 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3525 : : "mixed mask and nonmask vector types\n");
3526 : 12 : return false;
3527 : : }
3528 : :
3529 : 13865 : if (vect_emulated_vector_p (vectype_in)
3530 : 13865 : || vect_emulated_vector_p (vectype_out))
3531 : : {
3532 : 0 : if (dump_enabled_p ())
3533 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3534 : : "use emulated vector type for call\n");
3535 : 0 : return false;
3536 : : }
3537 : :
3538 : : /* FORNOW */
3539 : 13865 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3540 : 13865 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3541 : 13865 : if (known_eq (nunits_in * 2, nunits_out))
3542 : : modifier = NARROW;
3543 : 13467 : else if (known_eq (nunits_out, nunits_in))
3544 : : modifier = NONE;
3545 : 19 : else if (known_eq (nunits_out * 2, nunits_in))
3546 : : modifier = WIDEN;
3547 : : else
3548 : : return false;
3549 : :
3550 : : /* We only handle functions that do not read or clobber memory. */
3551 : 27730 : if (gimple_vuse (stmt))
3552 : : {
3553 : 1222 : if (dump_enabled_p ())
3554 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3555 : : "function reads from or writes to memory.\n");
3556 : 1222 : return false;
3557 : : }
3558 : :
3559 : : /* For now, we only vectorize functions if a target specific builtin
3560 : : is available. TODO -- in some cases, it might be profitable to
3561 : : insert the calls for pieces of the vector, in order to be able
3562 : : to vectorize other operations in the loop. */
3563 : 12643 : fndecl = NULL_TREE;
3564 : 12643 : internal_fn ifn = IFN_LAST;
3565 : 12643 : tree callee = gimple_call_fndecl (stmt);
3566 : :
3567 : : /* First try using an internal function. */
3568 : 12643 : code_helper convert_code = MAX_TREE_CODES;
3569 : 12643 : if (cfn != CFN_LAST
3570 : 12643 : && (modifier == NONE
3571 : 172 : || (modifier == NARROW
3572 : 156 : && simple_integer_narrowing (vectype_out, vectype_in,
3573 : : &convert_code))))
3574 : 11869 : ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3575 : : vectype_in);
3576 : :
3577 : : /* If that fails, try asking for a target-specific built-in function. */
3578 : 11869 : if (ifn == IFN_LAST)
3579 : : {
3580 : 6764 : if (cfn != CFN_LAST)
3581 : 6127 : fndecl = targetm.vectorize.builtin_vectorized_function
3582 : 6127 : (cfn, vectype_out, vectype_in);
3583 : 637 : else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
3584 : 24 : fndecl = targetm.vectorize.builtin_md_vectorized_function
3585 : 24 : (callee, vectype_out, vectype_in);
3586 : : }
3587 : :
3588 : 12643 : if (ifn == IFN_LAST && !fndecl)
3589 : : {
3590 : 6470 : if (cfn == CFN_GOMP_SIMD_LANE
3591 : 3203 : && SLP_TREE_LANES (slp_node) == 1
3592 : 3203 : && loop_vinfo
3593 : 3203 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3594 : 3203 : && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3595 : 12876 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3596 : 3203 : == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3597 : : {
3598 : : /* We can handle IFN_GOMP_SIMD_LANE by returning a
3599 : : { 0, 1, 2, ... vf - 1 } vector. */
3600 : 3203 : gcc_assert (nargs == 0);
3601 : : }
3602 : 3267 : else if (modifier == NONE
3603 : 3267 : && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3604 : 3117 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3605 : 2969 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
3606 : 2937 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
3607 : 196 : return vectorizable_bswap (vinfo, stmt_info, gsi, slp_node,
3608 : 196 : slp_op, vectype_in, cost_vec);
3609 : : else
3610 : : {
3611 : 3071 : if (dump_enabled_p ())
3612 : 237 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3613 : : "function is not vectorizable.\n");
3614 : 3071 : return false;
3615 : : }
3616 : : }
3617 : :
3618 : 9376 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
3619 : 9376 : internal_fn cond_fn = get_conditional_internal_fn (ifn);
3620 : 9376 : internal_fn cond_len_fn = get_len_internal_fn (ifn);
3621 : 9376 : int len_opno = internal_fn_len_index (cond_len_fn);
3622 : 9376 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
3623 : 7502 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
3624 : 9376 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
3625 : 9376 : if (cost_vec) /* transformation not required. */
3626 : : {
3627 : 15463 : for (i = 0; i < nargs; ++i)
3628 : 10202 : if (!vect_maybe_update_slp_op_vectype (slp_op[i],
3629 : 10202 : vectypes[i]
3630 : : ? vectypes[i] : vectype_in))
3631 : : {
3632 : 0 : if (dump_enabled_p ())
3633 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3634 : : "incompatible vector types for invariants\n");
3635 : 0 : return false;
3636 : : }
3637 : 5261 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3638 : 5261 : DUMP_VECT_SCOPE ("vectorizable_call");
3639 : 5261 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
3640 : :
3641 : 5261 : if (loop_vinfo
3642 : 4317 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
3643 : 19 : && (reduc_idx >= 0 || mask_opno >= 0))
3644 : : {
3645 : 18 : if (reduc_idx >= 0
3646 : 0 : && (cond_fn == IFN_LAST
3647 : 0 : || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3648 : : OPTIMIZE_FOR_SPEED))
3649 : 18 : && (cond_len_fn == IFN_LAST
3650 : 0 : || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3651 : : OPTIMIZE_FOR_SPEED)))
3652 : : {
3653 : 0 : if (dump_enabled_p ())
3654 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3655 : : "can't use a fully-masked loop because no"
3656 : : " conditional operation is available.\n");
3657 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3658 : : }
3659 : : else
3660 : : {
3661 : 18 : tree scalar_mask = NULL_TREE;
3662 : 18 : if (mask_opno >= 0)
3663 : 18 : scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
3664 : 18 : if (cond_len_fn != IFN_LAST
3665 : 18 : && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3666 : : OPTIMIZE_FOR_SPEED))
3667 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
3668 : : 1);
3669 : : else
3670 : 18 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
3671 : : scalar_mask);
3672 : : }
3673 : : }
3674 : 5261 : return true;
3675 : : }
3676 : :
3677 : : /* Transform. */
3678 : :
3679 : 4115 : if (dump_enabled_p ())
3680 : 414 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3681 : :
3682 : : /* Handle def. */
3683 : 4115 : scalar_dest = gimple_call_lhs (stmt);
3684 : 4115 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3685 : :
3686 : 4115 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3687 : 3185 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
3688 : 4115 : unsigned int vect_nargs = nargs;
3689 : 4115 : if (len_loop_p)
3690 : : {
3691 : 0 : if (len_opno >= 0)
3692 : : {
3693 : 0 : ifn = cond_len_fn;
3694 : : /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS. */
3695 : 0 : vect_nargs += 2;
3696 : : }
3697 : 0 : else if (reduc_idx >= 0)
3698 : 0 : gcc_unreachable ();
3699 : : }
3700 : 4115 : else if (masked_loop_p && reduc_idx >= 0)
3701 : : {
3702 : 0 : ifn = cond_fn;
3703 : 0 : vect_nargs += 2;
3704 : : }
3705 : 4115 : if (clz_ctz_arg1)
3706 : 59 : ++vect_nargs;
3707 : :
3708 : 4115 : if (modifier == NONE || ifn != IFN_LAST)
3709 : : {
3710 : 4083 : tree prev_res = NULL_TREE;
3711 : 4083 : vargs.safe_grow (vect_nargs, true);
3712 : 4083 : auto_vec<vec<tree> > vec_defs (nargs);
3713 : :
3714 : : /* Build argument list for the vectorized call. */
3715 : 4083 : if (cfn == CFN_GOMP_SIMD_LANE)
3716 : : {
3717 : 3304 : for (i = 0; i < nvectors; ++i)
3718 : : {
3719 : : /* ??? For multi-lane SLP we'd need to build
3720 : : { 0, 0, .., 1, 1, ... }. */
3721 : 1706 : tree cst = build_index_vector (vectype_out,
3722 : : i * nunits_out, 1);
3723 : 1706 : tree new_var
3724 : 1706 : = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
3725 : 1706 : gimple *init_stmt = gimple_build_assign (new_var, cst);
3726 : 1706 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3727 : 1706 : new_temp = make_ssa_name (vec_dest);
3728 : 1706 : gimple *new_stmt = gimple_build_assign (new_temp, new_var);
3729 : 1706 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3730 : 1706 : slp_node->push_vec_def (new_stmt);
3731 : : }
3732 : : }
3733 : : else
3734 : : {
3735 : 2485 : vec<tree> vec_oprnds0;
3736 : 2485 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3737 : 2485 : vec_oprnds0 = vec_defs[0];
3738 : :
3739 : : /* Arguments are ready. Create the new vector stmt. */
3740 : 5134 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
3741 : : {
3742 : 2649 : int varg = 0;
3743 : 2649 : if (masked_loop_p && reduc_idx >= 0)
3744 : : {
3745 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3746 : 0 : vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi, masks,
3747 : : vec_num, vectype_out, i);
3748 : : }
3749 : : size_t k;
3750 : 9348 : for (k = 0; k < nargs; k++)
3751 : : {
3752 : 6699 : vec<tree> vec_oprndsk = vec_defs[k];
3753 : 6699 : vargs[varg++] = vec_oprndsk[i];
3754 : : }
3755 : 2649 : if (masked_loop_p && reduc_idx >= 0)
3756 : 0 : vargs[varg++] = vargs[reduc_idx + 1];
3757 : 2649 : if (clz_ctz_arg1)
3758 : 59 : vargs[varg++] = clz_ctz_arg1;
3759 : :
3760 : 2649 : gimple *new_stmt;
3761 : 2649 : if (modifier == NARROW)
3762 : : {
3763 : : /* We don't define any narrowing conditional functions
3764 : : at present. */
3765 : 0 : gcc_assert (mask_opno < 0);
3766 : 0 : tree half_res = make_ssa_name (vectype_in);
3767 : 0 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3768 : 0 : gimple_call_set_lhs (call, half_res);
3769 : 0 : gimple_call_set_nothrow (call, true);
3770 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3771 : 0 : if ((i & 1) == 0)
3772 : : {
3773 : 0 : prev_res = half_res;
3774 : 0 : continue;
3775 : : }
3776 : 0 : new_temp = make_ssa_name (vec_dest);
3777 : 0 : new_stmt = vect_gimple_build (new_temp, convert_code,
3778 : : prev_res, half_res);
3779 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3780 : : }
3781 : : else
3782 : : {
3783 : 2649 : if (len_opno >= 0 && len_loop_p)
3784 : : {
3785 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3786 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
3787 : : vec_num, vectype_out, i, 1);
3788 : 0 : signed char biasval
3789 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3790 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
3791 : 0 : vargs[len_opno] = len;
3792 : 0 : vargs[len_opno + 1] = bias;
3793 : : }
3794 : 2649 : else if (mask_opno >= 0 && masked_loop_p)
3795 : : {
3796 : 36 : unsigned int vec_num = vec_oprnds0.length ();
3797 : 36 : tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
3798 : : vec_num, vectype_out, i);
3799 : 36 : vargs[mask_opno]
3800 : 72 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
3801 : 36 : vargs[mask_opno], gsi);
3802 : : }
3803 : :
3804 : 2649 : gcall *call;
3805 : 2649 : if (ifn != IFN_LAST)
3806 : 2568 : call = gimple_build_call_internal_vec (ifn, vargs);
3807 : : else
3808 : 81 : call = gimple_build_call_vec (fndecl, vargs);
3809 : 2649 : new_temp = make_ssa_name (vec_dest, call);
3810 : 2649 : gimple_call_set_lhs (call, new_temp);
3811 : 2649 : gimple_call_set_nothrow (call, true);
3812 : 2649 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3813 : 2649 : new_stmt = call;
3814 : : }
3815 : 2649 : slp_node->push_vec_def (new_stmt);
3816 : : }
3817 : : }
3818 : :
3819 : 10407 : for (i = 0; i < nargs; i++)
3820 : : {
3821 : 6324 : vec<tree> vec_oprndsi = vec_defs[i];
3822 : 6324 : vec_oprndsi.release ();
3823 : : }
3824 : 4083 : }
3825 : 32 : else if (modifier == NARROW)
3826 : : {
3827 : 32 : auto_vec<vec<tree> > vec_defs (nargs);
3828 : : /* We don't define any narrowing conditional functions at present. */
3829 : 32 : gcc_assert (mask_opno < 0);
3830 : :
3831 : : /* Build argument list for the vectorized call. */
3832 : 32 : vargs.create (nargs * 2);
3833 : :
3834 : 32 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3835 : 32 : vec<tree> vec_oprnds0 = vec_defs[0];
3836 : :
3837 : : /* Arguments are ready. Create the new vector stmt. */
3838 : 64 : for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
3839 : : {
3840 : 32 : size_t k;
3841 : 32 : vargs.truncate (0);
3842 : 64 : for (k = 0; k < nargs; k++)
3843 : : {
3844 : 32 : vec<tree> vec_oprndsk = vec_defs[k];
3845 : 32 : vargs.quick_push (vec_oprndsk[i]);
3846 : 32 : vargs.quick_push (vec_oprndsk[i + 1]);
3847 : : }
3848 : 32 : gcall *call;
3849 : 32 : if (ifn != IFN_LAST)
3850 : : call = gimple_build_call_internal_vec (ifn, vargs);
3851 : : else
3852 : 32 : call = gimple_build_call_vec (fndecl, vargs);
3853 : 32 : new_temp = make_ssa_name (vec_dest, call);
3854 : 32 : gimple_call_set_lhs (call, new_temp);
3855 : 32 : gimple_call_set_nothrow (call, true);
3856 : 32 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3857 : 32 : slp_node->push_vec_def (call);
3858 : : }
3859 : :
3860 : 64 : for (i = 0; i < nargs; i++)
3861 : : {
3862 : 32 : vec<tree> vec_oprndsi = vec_defs[i];
3863 : 32 : vec_oprndsi.release ();
3864 : : }
3865 : 32 : }
3866 : : else
3867 : : /* No current target implements this case. */
3868 : : return false;
3869 : :
3870 : 4115 : vargs.release ();
3871 : :
3872 : 4115 : return true;
3873 : 2300382 : }
3874 : :
3875 : :
3876 : : struct simd_call_arg_info
3877 : : {
3878 : : tree vectype;
3879 : : tree op;
3880 : : HOST_WIDE_INT linear_step;
3881 : : enum vect_def_type dt;
3882 : : unsigned int align;
3883 : : bool simd_lane_linear;
3884 : : };
3885 : :
3886 : : /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
3887 : : is linear within simd lane (but not within whole loop), note it in
3888 : : *ARGINFO. */
3889 : :
3890 : : static void
3891 : 15 : vect_simd_lane_linear (tree op, class loop *loop,
3892 : : struct simd_call_arg_info *arginfo)
3893 : : {
3894 : 15 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
3895 : :
3896 : 15 : if (!is_gimple_assign (def_stmt)
3897 : 15 : || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
3898 : 27 : || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
3899 : 3 : return;
3900 : :
3901 : 12 : tree base = gimple_assign_rhs1 (def_stmt);
3902 : 12 : HOST_WIDE_INT linear_step = 0;
3903 : 12 : tree v = gimple_assign_rhs2 (def_stmt);
3904 : 48 : while (TREE_CODE (v) == SSA_NAME)
3905 : : {
3906 : 36 : tree t;
3907 : 36 : def_stmt = SSA_NAME_DEF_STMT (v);
3908 : 36 : if (is_gimple_assign (def_stmt))
3909 : 24 : switch (gimple_assign_rhs_code (def_stmt))
3910 : : {
3911 : 0 : case PLUS_EXPR:
3912 : 0 : t = gimple_assign_rhs2 (def_stmt);
3913 : 0 : if (linear_step || TREE_CODE (t) != INTEGER_CST)
3914 : : return;
3915 : 0 : base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
3916 : 0 : v = gimple_assign_rhs1 (def_stmt);
3917 : 0 : continue;
3918 : 12 : case MULT_EXPR:
3919 : 12 : t = gimple_assign_rhs2 (def_stmt);
3920 : 12 : if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
3921 : 0 : return;
3922 : 12 : linear_step = tree_to_shwi (t);
3923 : 12 : v = gimple_assign_rhs1 (def_stmt);
3924 : 12 : continue;
3925 : 12 : CASE_CONVERT:
3926 : 12 : t = gimple_assign_rhs1 (def_stmt);
3927 : 12 : if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
3928 : 12 : || (TYPE_PRECISION (TREE_TYPE (v))
3929 : 12 : < TYPE_PRECISION (TREE_TYPE (t))))
3930 : : return;
3931 : 12 : if (!linear_step)
3932 : 0 : linear_step = 1;
3933 : 12 : v = t;
3934 : 12 : continue;
3935 : : default:
3936 : : return;
3937 : : }
3938 : 12 : else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
3939 : 12 : && loop->simduid
3940 : 12 : && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
3941 : 24 : && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
3942 : : == loop->simduid))
3943 : : {
3944 : 12 : if (!linear_step)
3945 : 0 : linear_step = 1;
3946 : 12 : arginfo->linear_step = linear_step;
3947 : 12 : arginfo->op = base;
3948 : 12 : arginfo->simd_lane_linear = true;
3949 : 12 : return;
3950 : : }
3951 : : }
3952 : : }
3953 : :
3954 : : /* Function vectorizable_simd_clone_call.
3955 : :
3956 : : Check if STMT_INFO performs a function call that can be vectorized
3957 : : by calling a simd clone of the function.
3958 : : If COST_VEC is passed, calculate costs but don't change anything,
3959 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
3960 : : it, and insert it at GSI.
3961 : : Return true if STMT_INFO is vectorizable in this way. */
3962 : :
3963 : : static bool
3964 : 2291196 : vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
3965 : : gimple_stmt_iterator *gsi,
3966 : : slp_tree slp_node,
3967 : : stmt_vector_for_cost *cost_vec)
3968 : : {
3969 : 2291196 : tree vec_dest;
3970 : 2291196 : tree scalar_dest;
3971 : 2291196 : tree op;
3972 : 2291196 : tree vec_oprnd0 = NULL_TREE;
3973 : 2291196 : tree vectype;
3974 : 2291196 : poly_uint64 nunits;
3975 : 2291196 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3976 : 2291196 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3977 : 2291196 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
3978 : 2291196 : tree fndecl, new_temp;
3979 : 2291196 : int ncopies, j;
3980 : 2291196 : auto_vec<simd_call_arg_info> arginfo;
3981 : 2291196 : vec<tree> vargs = vNULL;
3982 : 2291196 : size_t i, nargs;
3983 : 2291196 : tree rtype, ratype;
3984 : 2291196 : vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
3985 : 2291196 : int masked_call_offset = 0;
3986 : :
3987 : : /* Is STMT a vectorizable call? */
3988 : 2291196 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
3989 : 10268 : if (!stmt)
3990 : : return false;
3991 : :
3992 : 10268 : fndecl = gimple_call_fndecl (stmt);
3993 : 10268 : if (fndecl == NULL_TREE
3994 : 10268 : && gimple_call_internal_p (stmt, IFN_MASK_CALL))
3995 : : {
3996 : 186 : fndecl = gimple_call_arg (stmt, 0);
3997 : 186 : gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
3998 : 186 : fndecl = TREE_OPERAND (fndecl, 0);
3999 : 186 : gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
4000 : : masked_call_offset = 1;
4001 : : }
4002 : 10082 : if (fndecl == NULL_TREE)
4003 : : return false;
4004 : :
4005 : 4329 : struct cgraph_node *node = cgraph_node::get (fndecl);
4006 : 4329 : if (node == NULL || node->simd_clones == NULL)
4007 : : return false;
4008 : :
4009 : 1396 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4010 : : return false;
4011 : :
4012 : 1396 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4013 : 0 : && cost_vec)
4014 : : return false;
4015 : :
4016 : 1396 : if (gimple_call_lhs (stmt)
4017 : 1396 : && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
4018 : : return false;
4019 : :
4020 : 1396 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
4021 : :
4022 : 1396 : vectype = SLP_TREE_VECTYPE (slp_node);
4023 : :
4024 : 2291260 : if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
4025 : : return false;
4026 : :
4027 : : /* Process function arguments. */
4028 : 1396 : nargs = gimple_call_num_args (stmt) - masked_call_offset;
4029 : :
4030 : : /* Bail out if the function has zero arguments. */
4031 : 1396 : if (nargs == 0)
4032 : : return false;
4033 : :
4034 : 1332 : vect_simd_clone_data _data;
4035 : 1332 : vect_simd_clone_data &data = slp_node->get_data (_data);
4036 : 1332 : vec<tree>& simd_clone_info = data.simd_clone_info;
4037 : 1332 : arginfo.reserve (nargs, true);
4038 : 1332 : auto_vec<slp_tree> slp_op;
4039 : 1332 : slp_op.safe_grow_cleared (nargs);
4040 : :
4041 : 3836 : for (i = 0; i < nargs; i++)
4042 : : {
4043 : 2504 : simd_call_arg_info thisarginfo;
4044 : 2504 : affine_iv iv;
4045 : :
4046 : 2504 : thisarginfo.linear_step = 0;
4047 : 2504 : thisarginfo.align = 0;
4048 : 2504 : thisarginfo.op = NULL_TREE;
4049 : 2504 : thisarginfo.simd_lane_linear = false;
4050 : :
4051 : 5008 : int op_no = vect_slp_child_index_for_operand (stmt,
4052 : 2504 : i + masked_call_offset,
4053 : : false);
4054 : 5008 : if (!vect_is_simple_use (vinfo, slp_node,
4055 : 2504 : op_no, &op, &slp_op[i],
4056 : : &thisarginfo.dt, &thisarginfo.vectype)
4057 : 2504 : || thisarginfo.dt == vect_uninitialized_def)
4058 : : {
4059 : 0 : if (dump_enabled_p ())
4060 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4061 : : "use not simple.\n");
4062 : 0 : return false;
4063 : : }
4064 : :
4065 : 2504 : if (thisarginfo.dt == vect_constant_def
4066 : 2504 : || thisarginfo.dt == vect_external_def)
4067 : : {
4068 : : /* With SLP we determine the vector type of constants/externals
4069 : : at analysis time, handling conflicts via
4070 : : vect_maybe_update_slp_op_vectype. At transform time
4071 : : we have a vector type recorded for SLP. */
4072 : 697 : gcc_assert (cost_vec
4073 : : || thisarginfo.vectype != NULL_TREE);
4074 : : if (cost_vec)
4075 : 567 : thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
4076 : 567 : TREE_TYPE (op),
4077 : : slp_node);
4078 : : }
4079 : : else
4080 : 1807 : gcc_assert (thisarginfo.vectype != NULL_TREE);
4081 : :
4082 : : /* For linear arguments, the analyze phase should have saved
4083 : : the base and step in {STMT_VINFO,SLP_TREE}_SIMD_CLONE_INFO. */
4084 : 2374 : if (!cost_vec
4085 : 1542 : && i * 3 + 4 <= simd_clone_info.length ()
4086 : 2584 : && simd_clone_info[i * 3 + 2])
4087 : : {
4088 : 118 : thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
4089 : 118 : thisarginfo.op = simd_clone_info[i * 3 + 1];
4090 : 118 : thisarginfo.simd_lane_linear
4091 : 118 : = (simd_clone_info[i * 3 + 3] == boolean_true_node);
4092 : : /* If loop has been peeled for alignment, we need to adjust it. */
4093 : 118 : tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
4094 : 118 : tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
4095 : 118 : if (n1 != n2 && !thisarginfo.simd_lane_linear)
4096 : : {
4097 : 0 : tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
4098 : 0 : tree step = simd_clone_info[i * 3 + 2];
4099 : 0 : tree opt = TREE_TYPE (thisarginfo.op);
4100 : 0 : bias = fold_convert (TREE_TYPE (step), bias);
4101 : 0 : bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
4102 : 0 : thisarginfo.op
4103 : 0 : = fold_build2 (POINTER_TYPE_P (opt)
4104 : : ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
4105 : : thisarginfo.op, bias);
4106 : : }
4107 : : }
4108 : 2386 : else if (cost_vec
4109 : 1733 : && thisarginfo.dt != vect_constant_def
4110 : 1610 : && thisarginfo.dt != vect_external_def
4111 : 1166 : && loop_vinfo
4112 : 1163 : && TREE_CODE (op) == SSA_NAME
4113 : 2326 : && simple_iv (loop, loop_containing_stmt (stmt), op,
4114 : : &iv, false)
4115 : 2590 : && tree_fits_shwi_p (iv.step))
4116 : : {
4117 : 204 : thisarginfo.linear_step = tree_to_shwi (iv.step);
4118 : 204 : thisarginfo.op = iv.base;
4119 : : }
4120 : 2182 : else if ((thisarginfo.dt == vect_constant_def
4121 : 2182 : || thisarginfo.dt == vect_external_def)
4122 : 2182 : && POINTER_TYPE_P (TREE_TYPE (op)))
4123 : 479 : thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
4124 : : /* Addresses of array elements indexed by GOMP_SIMD_LANE are
4125 : : linear too. */
4126 : 4419 : if (POINTER_TYPE_P (TREE_TYPE (op))
4127 : 589 : && !thisarginfo.linear_step
4128 : 505 : && cost_vec
4129 : 451 : && thisarginfo.dt != vect_constant_def
4130 : 451 : && thisarginfo.dt != vect_external_def
4131 : 15 : && loop_vinfo
4132 : 2519 : && TREE_CODE (op) == SSA_NAME)
4133 : 15 : vect_simd_lane_linear (op, loop, &thisarginfo);
4134 : :
4135 : 2504 : arginfo.quick_push (thisarginfo);
4136 : : }
4137 : :
4138 : 1332 : poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
4139 : 1332 : unsigned group_size = SLP_TREE_LANES (slp_node);
4140 : 1332 : unsigned int badness = 0;
4141 : 1332 : struct cgraph_node *bestn = NULL;
4142 : 1332 : if (!cost_vec)
4143 : 342 : bestn = cgraph_node::get (simd_clone_info[0]);
4144 : : else
4145 : 5692 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
4146 : 4702 : n = n->simdclone->next_clone)
4147 : : {
4148 : 4702 : unsigned int this_badness = 0;
4149 : 4702 : unsigned int num_calls;
4150 : : /* The number of arguments in the call and the number of parameters in
4151 : : the simdclone should match. However, when the simdclone is
4152 : : 'inbranch', it could have one more paramater than nargs when using
4153 : : an inbranch simdclone to call a non-inbranch call, either in a
4154 : : non-masked loop using a all true constant mask, or inside a masked
4155 : : loop using it's mask. */
4156 : 4702 : size_t simd_nargs = n->simdclone->nargs;
4157 : 4702 : if (!masked_call_offset && n->simdclone->inbranch)
4158 : 2297 : simd_nargs--;
4159 : 4702 : if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
4160 : : &num_calls)
4161 : 1808 : || (!n->simdclone->inbranch && (masked_call_offset > 0))
4162 : 1658 : || (nargs != simd_nargs))
4163 : 3044 : continue;
4164 : 1658 : if (num_calls != 1)
4165 : 1060 : this_badness += floor_log2 (num_calls) * 4096;
4166 : 1658 : if (n->simdclone->inbranch)
4167 : 684 : this_badness += 8192;
4168 : :
4169 : : /* If SLP_TREE_VECTYPE has not been set yet pass the general vector
4170 : : mode, which for targets that use it will determine what ISA we can
4171 : : vectorize this code with. */
4172 : 1658 : machine_mode vector_mode = vinfo->vector_mode;
4173 : 1658 : if (vectype)
4174 : 1638 : vector_mode = TYPE_MODE (vectype);
4175 : 1658 : int target_badness = targetm.simd_clone.usable (n, vector_mode);
4176 : 1658 : if (target_badness < 0)
4177 : 364 : continue;
4178 : 1294 : this_badness += target_badness * 512;
4179 : 4077 : for (i = 0; i < nargs; i++)
4180 : : {
4181 : 2840 : switch (n->simdclone->args[i].arg_type)
4182 : : {
4183 : 1956 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4184 : 1956 : if (!useless_type_conversion_p
4185 : 1956 : (n->simdclone->args[i].orig_type,
4186 : 1956 : TREE_TYPE (gimple_call_arg (stmt,
4187 : : i + masked_call_offset))))
4188 : : i = -1;
4189 : 1956 : else if (arginfo[i].dt == vect_constant_def
4190 : 1851 : || arginfo[i].dt == vect_external_def
4191 : 3745 : || arginfo[i].linear_step)
4192 : 386 : this_badness += 64;
4193 : : break;
4194 : 314 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4195 : 314 : if (arginfo[i].dt != vect_constant_def
4196 : 314 : && arginfo[i].dt != vect_external_def)
4197 : : i = -1;
4198 : : break;
4199 : 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4200 : 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4201 : 324 : if (arginfo[i].dt == vect_constant_def
4202 : 324 : || arginfo[i].dt == vect_external_def
4203 : 324 : || (arginfo[i].linear_step
4204 : 324 : != n->simdclone->args[i].linear_step))
4205 : : i = -1;
4206 : : break;
4207 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4208 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4209 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4210 : : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4211 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4212 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4213 : : /* FORNOW */
4214 : : i = -1;
4215 : : break;
4216 : 246 : case SIMD_CLONE_ARG_TYPE_MASK:
4217 : : /* While we can create a traditional data vector from
4218 : : an incoming integer mode mask we have no good way to
4219 : : force generate an integer mode mask from a traditional
4220 : : boolean vector input. */
4221 : 246 : if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4222 : 246 : && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4223 : : i = -1;
4224 : 240 : else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4225 : 240 : && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4226 : 45 : this_badness += 2048;
4227 : : break;
4228 : : }
4229 : 2783 : if (i == (size_t) -1)
4230 : : break;
4231 : 2783 : if (n->simdclone->args[i].alignment > arginfo[i].align)
4232 : : {
4233 : : i = -1;
4234 : : break;
4235 : : }
4236 : 2783 : if (arginfo[i].align)
4237 : 110 : this_badness += (exact_log2 (arginfo[i].align)
4238 : 160 : - exact_log2 (n->simdclone->args[i].alignment));
4239 : : }
4240 : 1294 : if (i == (size_t) -1)
4241 : 57 : continue;
4242 : 1237 : if (masked_call_offset == 0
4243 : 997 : && n->simdclone->inbranch
4244 : 316 : && n->simdclone->nargs > nargs)
4245 : : {
4246 : 316 : gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
4247 : : SIMD_CLONE_ARG_TYPE_MASK);
4248 : : /* Penalize using a masked SIMD clone in a non-masked loop, that is
4249 : : not in a branch, as we'd have to construct an all-true mask. */
4250 : 316 : if (!loop_vinfo || !LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4251 : 316 : this_badness += 64;
4252 : : }
4253 : 1237 : if (bestn == NULL || this_badness < badness)
4254 : : {
4255 : 4702 : bestn = n;
4256 : 4702 : badness = this_badness;
4257 : : }
4258 : : }
4259 : :
4260 : 1332 : if (bestn == NULL)
4261 : : return false;
4262 : :
4263 : 817 : unsigned int num_mask_args = 0;
4264 : 817 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4265 : 111 : for (i = 0; i < nargs; i++)
4266 : 72 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4267 : 18 : num_mask_args++;
4268 : :
4269 : 2634 : for (i = 0; i < nargs; i++)
4270 : : {
4271 : 1859 : if ((arginfo[i].dt == vect_constant_def
4272 : 1690 : || arginfo[i].dt == vect_external_def)
4273 : 1960 : && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
4274 : : {
4275 : 65 : tree arg_type = TREE_TYPE (gimple_call_arg (stmt,
4276 : : i + masked_call_offset));
4277 : 65 : arginfo[i].vectype = get_vectype_for_scalar_type (vinfo, arg_type,
4278 : : slp_node);
4279 : 65 : if (arginfo[i].vectype == NULL
4280 : 130 : || !constant_multiple_p (bestn->simdclone->simdlen,
4281 : 130 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4282 : 0 : return false;
4283 : : }
4284 : :
4285 : 1859 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR
4286 : 1859 : && VECTOR_BOOLEAN_TYPE_P (bestn->simdclone->args[i].vector_type))
4287 : : {
4288 : 4 : if (dump_enabled_p ())
4289 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4290 : : "vector mask arguments are not supported.\n");
4291 : 4 : return false;
4292 : : }
4293 : :
4294 : 1855 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4295 : : {
4296 : 160 : tree clone_arg_vectype = bestn->simdclone->args[i].vector_type;
4297 : 160 : if (bestn->simdclone->mask_mode == VOIDmode)
4298 : : {
4299 : 142 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (clone_arg_vectype),
4300 : 284 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4301 : : {
4302 : : /* FORNOW we only have partial support for vector-type masks
4303 : : that can't hold all of simdlen. */
4304 : 20 : if (dump_enabled_p ())
4305 : 20 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4306 : : vect_location,
4307 : : "in-branch vector clones are not yet"
4308 : : " supported for mismatched vector sizes.\n");
4309 : 20 : return false;
4310 : : }
4311 : 122 : if (!expand_vec_cond_expr_p (clone_arg_vectype,
4312 : 122 : arginfo[i].vectype))
4313 : : {
4314 : 6 : if (dump_enabled_p ())
4315 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4316 : : vect_location,
4317 : : "cannot compute mask argument for"
4318 : : " in-branch vector clones.\n");
4319 : 6 : return false;
4320 : : }
4321 : : }
4322 : 18 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4323 : : {
4324 : 18 : if (!SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype))
4325 : 36 : || maybe_ne (exact_div (bestn->simdclone->simdlen,
4326 : : num_mask_args),
4327 : 24 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4328 : : {
4329 : : /* FORNOW we only have partial support for integer-type masks
4330 : : that represent the same number of lanes as the
4331 : : vectorized mask inputs. */
4332 : 12 : if (dump_enabled_p ())
4333 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4334 : : vect_location,
4335 : : "in-branch vector clones are not yet "
4336 : : "supported for mismatched vector sizes.\n");
4337 : 12 : return false;
4338 : : }
4339 : : }
4340 : : else
4341 : : {
4342 : 0 : if (dump_enabled_p ())
4343 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4344 : : vect_location,
4345 : : "in-branch vector clones not supported"
4346 : : " on this target.\n");
4347 : 0 : return false;
4348 : : }
4349 : : }
4350 : : }
4351 : :
4352 : 775 : fndecl = bestn->decl;
4353 : 775 : nunits = bestn->simdclone->simdlen;
4354 : 775 : ncopies = vector_unroll_factor (vf * group_size, nunits);
4355 : :
4356 : : /* If the function isn't const, only allow it in simd loops where user
4357 : : has asserted that at least nunits consecutive iterations can be
4358 : : performed using SIMD instructions. */
4359 : 773 : if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
4360 : 913 : && gimple_vuse (stmt))
4361 : : return false;
4362 : :
4363 : : /* Sanity check: make sure that at least one copy of the vectorized stmt
4364 : : needs to be generated. */
4365 : 775 : gcc_assert (ncopies >= 1);
4366 : :
4367 : 775 : if (cost_vec) /* transformation not required. */
4368 : : {
4369 : 1437 : for (unsigned i = 0; i < nargs; ++i)
4370 : 1004 : if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
4371 : : {
4372 : 0 : if (dump_enabled_p ())
4373 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4374 : : "incompatible vector types for invariants\n");
4375 : 0 : return false;
4376 : : }
4377 : : /* When the original call is pure or const but the SIMD ABI dictates
4378 : : an aggregate return we will have to use a virtual definition and
4379 : : in a loop eventually even need to add a virtual PHI. That's
4380 : : not straight-forward so allow to fix this up via renaming. */
4381 : 433 : if (gimple_call_lhs (stmt)
4382 : 427 : && !gimple_vdef (stmt)
4383 : 766 : && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
4384 : 27 : vinfo->any_known_not_updated_vssa = true;
4385 : : /* ??? For SLP code-gen we end up inserting after the last
4386 : : vector argument def rather than at the original call position
4387 : : so automagic virtual operand updating doesn't work. */
4388 : 866 : if (gimple_vuse (stmt))
4389 : 137 : vinfo->any_known_not_updated_vssa = true;
4390 : 433 : simd_clone_info.safe_push (bestn->decl);
4391 : 1448 : for (i = 0; i < bestn->simdclone->nargs; i++)
4392 : : {
4393 : 1015 : switch (bestn->simdclone->args[i].arg_type)
4394 : : {
4395 : 825 : default:
4396 : 825 : continue;
4397 : 118 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4398 : 118 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4399 : 118 : {
4400 : 118 : simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
4401 : 118 : simd_clone_info.safe_push (arginfo[i].op);
4402 : 202 : tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4403 : 202 : ? size_type_node : TREE_TYPE (arginfo[i].op);
4404 : 118 : tree ls = build_int_cst (lst, arginfo[i].linear_step);
4405 : 118 : simd_clone_info.safe_push (ls);
4406 : 118 : tree sll = arginfo[i].simd_lane_linear
4407 : 118 : ? boolean_true_node : boolean_false_node;
4408 : 118 : simd_clone_info.safe_push (sll);
4409 : : }
4410 : 118 : break;
4411 : 72 : case SIMD_CLONE_ARG_TYPE_MASK:
4412 : 72 : if (loop_vinfo
4413 : 72 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4414 : 0 : vect_record_loop_mask (loop_vinfo,
4415 : : &LOOP_VINFO_MASKS (loop_vinfo),
4416 : : ncopies, vectype, op);
4417 : :
4418 : : break;
4419 : 825 : }
4420 : : }
4421 : :
4422 : 433 : if (!bestn->simdclone->inbranch && loop_vinfo)
4423 : : {
4424 : 360 : if (dump_enabled_p ()
4425 : 360 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4426 : 3 : dump_printf_loc (MSG_NOTE, vect_location,
4427 : : "can't use a fully-masked loop because a"
4428 : : " non-masked simd clone was selected.\n");
4429 : 360 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
4430 : : }
4431 : :
4432 : 433 : SLP_TREE_TYPE (slp_node) = call_simd_clone_vec_info_type;
4433 : 433 : slp_node->data = new vect_simd_clone_data (std::move (_data));
4434 : 433 : DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
4435 : : /* vect_model_simple_cost (vinfo, 1, slp_node, cost_vec); */
4436 : 433 : return true;
4437 : : }
4438 : :
4439 : : /* Transform. */
4440 : :
4441 : 342 : if (dump_enabled_p ())
4442 : 238 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4443 : :
4444 : : /* Handle def. */
4445 : 342 : scalar_dest = gimple_call_lhs (stmt);
4446 : 342 : vec_dest = NULL_TREE;
4447 : 342 : rtype = NULL_TREE;
4448 : 342 : ratype = NULL_TREE;
4449 : 342 : if (scalar_dest)
4450 : : {
4451 : 336 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
4452 : 336 : rtype = TREE_TYPE (TREE_TYPE (fndecl));
4453 : 336 : if (TREE_CODE (rtype) == ARRAY_TYPE)
4454 : : {
4455 : 9 : ratype = rtype;
4456 : 9 : rtype = TREE_TYPE (ratype);
4457 : : }
4458 : : }
4459 : :
4460 : 684 : auto_vec<vec<tree> > vec_oprnds;
4461 : 342 : auto_vec<unsigned> vec_oprnds_i;
4462 : 342 : vec_oprnds_i.safe_grow_cleared (nargs, true);
4463 : 342 : vec_oprnds.reserve_exact (nargs);
4464 : 342 : vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
4465 : 793 : for (j = 0; j < ncopies; ++j)
4466 : : {
4467 : 451 : poly_uint64 callee_nelements;
4468 : 451 : poly_uint64 caller_nelements;
4469 : : /* Build argument list for the vectorized call. */
4470 : 451 : if (j == 0)
4471 : 342 : vargs.create (nargs);
4472 : : else
4473 : 109 : vargs.truncate (0);
4474 : :
4475 : 1534 : for (i = 0; i < nargs; i++)
4476 : : {
4477 : 1083 : unsigned int k, l, m, o;
4478 : 1083 : tree atype;
4479 : 1083 : op = gimple_call_arg (stmt, i + masked_call_offset);
4480 : 1083 : switch (bestn->simdclone->args[i].arg_type)
4481 : : {
4482 : 799 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4483 : 799 : atype = bestn->simdclone->args[i].vector_type;
4484 : 799 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4485 : 799 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4486 : 799 : o = vector_unroll_factor (nunits, callee_nelements);
4487 : 1828 : for (m = j * o; m < (j + 1) * o; m++)
4488 : : {
4489 : 1029 : if (known_lt (callee_nelements, caller_nelements))
4490 : : {
4491 : 516 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
4492 : 258 : if (!constant_multiple_p (caller_nelements,
4493 : : callee_nelements, &k))
4494 : 0 : gcc_unreachable ();
4495 : :
4496 : 258 : gcc_assert ((k & (k - 1)) == 0);
4497 : 258 : if (m == 0)
4498 : : {
4499 : 57 : vec_oprnds_i[i] = 0;
4500 : 57 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4501 : : }
4502 : : else
4503 : : {
4504 : 201 : vec_oprnd0 = arginfo[i].op;
4505 : 201 : if ((m & (k - 1)) == 0)
4506 : 72 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4507 : : }
4508 : 258 : arginfo[i].op = vec_oprnd0;
4509 : 258 : vec_oprnd0
4510 : 258 : = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
4511 : 258 : bitsize_int (prec),
4512 : 258 : bitsize_int ((m & (k - 1)) * prec));
4513 : 258 : gassign *new_stmt
4514 : 258 : = gimple_build_assign (make_ssa_name (atype),
4515 : : vec_oprnd0);
4516 : 258 : vect_finish_stmt_generation (vinfo, stmt_info,
4517 : : new_stmt, gsi);
4518 : 258 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4519 : : }
4520 : : else
4521 : : {
4522 : 771 : if (!constant_multiple_p (callee_nelements,
4523 : : caller_nelements, &k))
4524 : 0 : gcc_unreachable ();
4525 : 771 : gcc_assert ((k & (k - 1)) == 0);
4526 : 771 : vec<constructor_elt, va_gc> *ctor_elts;
4527 : 771 : if (k != 1)
4528 : 13 : vec_alloc (ctor_elts, k);
4529 : : else
4530 : 758 : ctor_elts = NULL;
4531 : 797 : for (l = 0; l < k; l++)
4532 : : {
4533 : 784 : if (m == 0 && l == 0)
4534 : : {
4535 : 433 : vec_oprnds_i[i] = 0;
4536 : 433 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4537 : : }
4538 : : else
4539 : 351 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4540 : 784 : arginfo[i].op = vec_oprnd0;
4541 : 784 : if (k == 1)
4542 : : break;
4543 : 26 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4544 : : vec_oprnd0);
4545 : : }
4546 : 771 : if (k == 1)
4547 : 758 : if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
4548 : : atype))
4549 : : {
4550 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
4551 : : vec_oprnd0);
4552 : 0 : gassign *new_stmt
4553 : 0 : = gimple_build_assign (make_ssa_name (atype),
4554 : : vec_oprnd0);
4555 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
4556 : : new_stmt, gsi);
4557 : 0 : vargs.safe_push (gimple_get_lhs (new_stmt));
4558 : : }
4559 : : else
4560 : 758 : vargs.safe_push (vec_oprnd0);
4561 : : else
4562 : : {
4563 : 13 : vec_oprnd0 = build_constructor (atype, ctor_elts);
4564 : 13 : gassign *new_stmt
4565 : 13 : = gimple_build_assign (make_ssa_name (atype),
4566 : : vec_oprnd0);
4567 : 13 : vect_finish_stmt_generation (vinfo, stmt_info,
4568 : : new_stmt, gsi);
4569 : 13 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4570 : : }
4571 : : }
4572 : : }
4573 : : break;
4574 : 61 : case SIMD_CLONE_ARG_TYPE_MASK:
4575 : 61 : if (bestn->simdclone->mask_mode == VOIDmode)
4576 : : {
4577 : 58 : atype = bestn->simdclone->args[i].vector_type;
4578 : 58 : tree elt_type = TREE_TYPE (atype);
4579 : 58 : tree one = fold_convert (elt_type, integer_one_node);
4580 : 58 : tree zero = fold_convert (elt_type, integer_zero_node);
4581 : 58 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4582 : 58 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4583 : 58 : o = vector_unroll_factor (nunits, callee_nelements);
4584 : 116 : for (m = j * o; m < (j + 1) * o; m++)
4585 : : {
4586 : 58 : if (maybe_lt (callee_nelements, caller_nelements))
4587 : : {
4588 : : /* The mask type has fewer elements than simdlen. */
4589 : :
4590 : : /* FORNOW */
4591 : 0 : gcc_unreachable ();
4592 : : }
4593 : 58 : else if (known_eq (callee_nelements, caller_nelements))
4594 : : {
4595 : : /* The SIMD clone function has the same number of
4596 : : elements as the current function. */
4597 : 58 : if (m == 0)
4598 : 58 : vec_oprnds_i[i] = 0;
4599 : 58 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4600 : 58 : if (loop_vinfo
4601 : 58 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4602 : : {
4603 : 0 : vec_loop_masks *loop_masks
4604 : : = &LOOP_VINFO_MASKS (loop_vinfo);
4605 : 0 : tree loop_mask
4606 : 0 : = vect_get_loop_mask (loop_vinfo, gsi,
4607 : : loop_masks, ncopies,
4608 : 0 : vectype, j);
4609 : 0 : vec_oprnd0
4610 : 0 : = prepare_vec_mask (loop_vinfo,
4611 : 0 : TREE_TYPE (loop_mask),
4612 : : loop_mask, vec_oprnd0,
4613 : : gsi);
4614 : 0 : loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
4615 : : loop_mask });
4616 : :
4617 : : }
4618 : 58 : vec_oprnd0
4619 : 58 : = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
4620 : : build_vector_from_val (atype, one),
4621 : : build_vector_from_val (atype, zero));
4622 : 58 : gassign *new_stmt
4623 : 58 : = gimple_build_assign (make_ssa_name (atype),
4624 : : vec_oprnd0);
4625 : 58 : vect_finish_stmt_generation (vinfo, stmt_info,
4626 : : new_stmt, gsi);
4627 : 58 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4628 : : }
4629 : : else
4630 : : {
4631 : : /* The mask type has more elements than simdlen. */
4632 : :
4633 : : /* FORNOW */
4634 : 0 : gcc_unreachable ();
4635 : : }
4636 : : }
4637 : : }
4638 : 3 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4639 : : {
4640 : 3 : atype = bestn->simdclone->args[i].vector_type;
4641 : : /* Guess the number of lanes represented by atype. */
4642 : 3 : poly_uint64 atype_subparts
4643 : 3 : = exact_div (bestn->simdclone->simdlen,
4644 : : num_mask_args);
4645 : 3 : o = vector_unroll_factor (nunits, atype_subparts);
4646 : 6 : for (m = j * o; m < (j + 1) * o; m++)
4647 : : {
4648 : 3 : if (m == 0)
4649 : 3 : vec_oprnds_i[i] = 0;
4650 : 3 : if (maybe_lt (atype_subparts,
4651 : 3 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4652 : : {
4653 : : /* The mask argument has fewer elements than the
4654 : : input vector. */
4655 : : /* FORNOW */
4656 : 0 : gcc_unreachable ();
4657 : : }
4658 : 3 : else if (known_eq (atype_subparts,
4659 : : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4660 : : {
4661 : : /* The vector mask argument matches the input
4662 : : in the number of lanes, but not necessarily
4663 : : in the mode. */
4664 : 3 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4665 : 3 : tree st = lang_hooks.types.type_for_mode
4666 : 3 : (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
4667 : 3 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
4668 : : vec_oprnd0);
4669 : 3 : gassign *new_stmt
4670 : 3 : = gimple_build_assign (make_ssa_name (st),
4671 : : vec_oprnd0);
4672 : 3 : vect_finish_stmt_generation (vinfo, stmt_info,
4673 : : new_stmt, gsi);
4674 : 3 : if (!types_compatible_p (atype, st))
4675 : : {
4676 : 3 : new_stmt
4677 : 3 : = gimple_build_assign (make_ssa_name (atype),
4678 : : NOP_EXPR,
4679 : : gimple_assign_lhs
4680 : : (new_stmt));
4681 : 3 : vect_finish_stmt_generation (vinfo, stmt_info,
4682 : : new_stmt, gsi);
4683 : : }
4684 : 3 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4685 : : }
4686 : : else
4687 : : {
4688 : : /* The mask argument has more elements than the
4689 : : input vector. */
4690 : : /* FORNOW */
4691 : 0 : gcc_unreachable ();
4692 : : }
4693 : : }
4694 : : }
4695 : : else
4696 : 0 : gcc_unreachable ();
4697 : : break;
4698 : 102 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4699 : 102 : vargs.safe_push (op);
4700 : 102 : break;
4701 : 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4702 : 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4703 : 121 : if (j == 0)
4704 : : {
4705 : 118 : gimple_seq stmts;
4706 : 118 : arginfo[i].op
4707 : 118 : = force_gimple_operand (unshare_expr (arginfo[i].op),
4708 : : &stmts, true, NULL_TREE);
4709 : 118 : if (stmts != NULL)
4710 : : {
4711 : 0 : basic_block new_bb;
4712 : 0 : edge pe = loop_preheader_edge (loop);
4713 : 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4714 : 0 : gcc_assert (!new_bb);
4715 : : }
4716 : 118 : if (arginfo[i].simd_lane_linear)
4717 : : {
4718 : 6 : vargs.safe_push (arginfo[i].op);
4719 : 6 : break;
4720 : : }
4721 : 112 : tree phi_res = copy_ssa_name (op);
4722 : 112 : gphi *new_phi = create_phi_node (phi_res, loop->header);
4723 : 112 : add_phi_arg (new_phi, arginfo[i].op,
4724 : : loop_preheader_edge (loop), UNKNOWN_LOCATION);
4725 : 112 : enum tree_code code
4726 : 196 : = POINTER_TYPE_P (TREE_TYPE (op))
4727 : 112 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4728 : 196 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4729 : 196 : ? sizetype : TREE_TYPE (op);
4730 : 112 : poly_widest_int cst
4731 : 112 : = wi::mul (bestn->simdclone->args[i].linear_step,
4732 : 112 : ncopies * nunits);
4733 : 112 : tree tcst = wide_int_to_tree (type, cst);
4734 : 112 : tree phi_arg = copy_ssa_name (op);
4735 : 112 : gassign *new_stmt
4736 : 112 : = gimple_build_assign (phi_arg, code, phi_res, tcst);
4737 : 112 : gimple_stmt_iterator si = gsi_after_labels (loop->header);
4738 : 112 : gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
4739 : 112 : add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
4740 : : UNKNOWN_LOCATION);
4741 : 112 : arginfo[i].op = phi_res;
4742 : 112 : vargs.safe_push (phi_res);
4743 : 112 : }
4744 : : else
4745 : : {
4746 : 3 : enum tree_code code
4747 : 6 : = POINTER_TYPE_P (TREE_TYPE (op))
4748 : 3 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4749 : 6 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4750 : 6 : ? sizetype : TREE_TYPE (op);
4751 : 3 : poly_widest_int cst
4752 : 3 : = wi::mul (bestn->simdclone->args[i].linear_step,
4753 : 3 : j * nunits);
4754 : 3 : tree tcst = wide_int_to_tree (type, cst);
4755 : 3 : new_temp = make_ssa_name (TREE_TYPE (op));
4756 : 3 : gassign *new_stmt
4757 : 6 : = gimple_build_assign (new_temp, code,
4758 : 3 : arginfo[i].op, tcst);
4759 : 3 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4760 : 3 : vargs.safe_push (new_temp);
4761 : 3 : }
4762 : : break;
4763 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4764 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4765 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4766 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4767 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4768 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4769 : 0 : default:
4770 : 0 : gcc_unreachable ();
4771 : : }
4772 : : }
4773 : :
4774 : 451 : if (masked_call_offset == 0
4775 : 390 : && bestn->simdclone->inbranch
4776 : 7 : && bestn->simdclone->nargs > nargs)
4777 : : {
4778 : 7 : unsigned long m, o;
4779 : 7 : size_t mask_i = bestn->simdclone->nargs - 1;
4780 : 7 : tree mask;
4781 : 7 : gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
4782 : : SIMD_CLONE_ARG_TYPE_MASK);
4783 : :
4784 : 7 : tree masktype = bestn->simdclone->args[mask_i].vector_type;
4785 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4786 : : /* Guess the number of lanes represented by masktype. */
4787 : 1 : callee_nelements = exact_div (bestn->simdclone->simdlen,
4788 : 1 : bestn->simdclone->nargs - nargs);
4789 : : else
4790 : 6 : callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
4791 : 7 : o = vector_unroll_factor (nunits, callee_nelements);
4792 : 14 : for (m = j * o; m < (j + 1) * o; m++)
4793 : : {
4794 : 7 : if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4795 : : {
4796 : 0 : vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
4797 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
4798 : : ncopies, masktype, j);
4799 : : }
4800 : : else
4801 : 7 : mask = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
4802 : :
4803 : 7 : gassign *new_stmt;
4804 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4805 : : {
4806 : : /* This means we are dealing with integer mask modes.
4807 : : First convert to an integer type with the same size as
4808 : : the current vector type. */
4809 : 1 : unsigned HOST_WIDE_INT intermediate_size
4810 : 1 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
4811 : 1 : tree mid_int_type =
4812 : 1 : build_nonstandard_integer_type (intermediate_size, 1);
4813 : 1 : mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
4814 : 1 : new_stmt
4815 : 1 : = gimple_build_assign (make_ssa_name (mid_int_type),
4816 : : mask);
4817 : 1 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4818 : : /* Then zero-extend to the mask mode. */
4819 : 1 : mask = fold_build1 (NOP_EXPR, masktype,
4820 : : gimple_get_lhs (new_stmt));
4821 : : }
4822 : 6 : else if (bestn->simdclone->mask_mode == VOIDmode)
4823 : : {
4824 : 6 : tree one = fold_convert (TREE_TYPE (masktype),
4825 : : integer_one_node);
4826 : 6 : tree zero = fold_convert (TREE_TYPE (masktype),
4827 : : integer_zero_node);
4828 : 6 : mask = build3 (VEC_COND_EXPR, masktype, mask,
4829 : : build_vector_from_val (masktype, one),
4830 : : build_vector_from_val (masktype, zero));
4831 : : }
4832 : : else
4833 : 0 : gcc_unreachable ();
4834 : :
4835 : 7 : new_stmt = gimple_build_assign (make_ssa_name (masktype), mask);
4836 : 7 : vect_finish_stmt_generation (vinfo, stmt_info,
4837 : : new_stmt, gsi);
4838 : 7 : mask = gimple_assign_lhs (new_stmt);
4839 : 7 : vargs.safe_push (mask);
4840 : : }
4841 : : }
4842 : :
4843 : 451 : gcall *new_call = gimple_build_call_vec (fndecl, vargs);
4844 : 451 : if (vec_dest)
4845 : : {
4846 : 445 : gcc_assert (ratype
4847 : : || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
4848 : 445 : if (ratype)
4849 : 15 : new_temp = create_tmp_var (ratype);
4850 : 430 : else if (useless_type_conversion_p (vectype, rtype))
4851 : 409 : new_temp = make_ssa_name (vec_dest, new_call);
4852 : : else
4853 : 21 : new_temp = make_ssa_name (rtype, new_call);
4854 : 445 : gimple_call_set_lhs (new_call, new_temp);
4855 : : }
4856 : 451 : vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
4857 : 451 : gimple *new_stmt = new_call;
4858 : :
4859 : 451 : if (vec_dest)
4860 : : {
4861 : 445 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
4862 : : {
4863 : 20 : unsigned int k, l;
4864 : 40 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
4865 : 40 : poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
4866 : 20 : k = vector_unroll_factor (nunits,
4867 : : TYPE_VECTOR_SUBPARTS (vectype));
4868 : 20 : gcc_assert ((k & (k - 1)) == 0);
4869 : 72 : for (l = 0; l < k; l++)
4870 : : {
4871 : 52 : tree t;
4872 : 52 : if (ratype)
4873 : : {
4874 : 42 : t = build_fold_addr_expr (new_temp);
4875 : 42 : t = build2 (MEM_REF, vectype, t,
4876 : 42 : build_int_cst (TREE_TYPE (t), l * bytes));
4877 : : }
4878 : : else
4879 : 10 : t = build3 (BIT_FIELD_REF, vectype, new_temp,
4880 : 10 : bitsize_int (prec), bitsize_int (l * prec));
4881 : 52 : new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
4882 : 52 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4883 : :
4884 : 52 : SLP_TREE_VEC_DEFS (slp_node)
4885 : 52 : .quick_push (gimple_assign_lhs (new_stmt));
4886 : : }
4887 : :
4888 : 20 : if (ratype)
4889 : 15 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
4890 : 20 : continue;
4891 : 20 : }
4892 : 425 : else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
4893 : : {
4894 : 16 : unsigned int k;
4895 : 16 : if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
4896 : 16 : TYPE_VECTOR_SUBPARTS (rtype), &k))
4897 : 0 : gcc_unreachable ();
4898 : 16 : gcc_assert ((k & (k - 1)) == 0);
4899 : 16 : if ((j & (k - 1)) == 0)
4900 : 8 : vec_alloc (ret_ctor_elts, k);
4901 : 16 : if (ratype)
4902 : : {
4903 : 0 : unsigned int m, o;
4904 : 0 : o = vector_unroll_factor (nunits,
4905 : : TYPE_VECTOR_SUBPARTS (rtype));
4906 : 0 : for (m = 0; m < o; m++)
4907 : : {
4908 : 0 : tree tem = build4 (ARRAY_REF, rtype, new_temp,
4909 : 0 : size_int (m), NULL_TREE, NULL_TREE);
4910 : 0 : new_stmt = gimple_build_assign (make_ssa_name (rtype),
4911 : : tem);
4912 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
4913 : : new_stmt, gsi);
4914 : 0 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
4915 : : gimple_assign_lhs (new_stmt));
4916 : : }
4917 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
4918 : : }
4919 : : else
4920 : 16 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
4921 : 16 : if ((j & (k - 1)) != k - 1)
4922 : 8 : continue;
4923 : 8 : vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
4924 : 8 : new_stmt
4925 : 8 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
4926 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4927 : :
4928 : 8 : SLP_TREE_VEC_DEFS (slp_node)
4929 : 8 : .quick_push (gimple_assign_lhs (new_stmt));
4930 : 8 : continue;
4931 : 8 : }
4932 : 409 : else if (ratype)
4933 : : {
4934 : 0 : tree t = build_fold_addr_expr (new_temp);
4935 : 0 : t = build2 (MEM_REF, vectype, t,
4936 : 0 : build_int_cst (TREE_TYPE (t), 0));
4937 : 0 : new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
4938 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4939 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
4940 : : }
4941 : 409 : else if (!useless_type_conversion_p (vectype, rtype))
4942 : : {
4943 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
4944 : 0 : new_stmt
4945 : 0 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
4946 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4947 : : }
4948 : : }
4949 : :
4950 : 415 : if (gimple_get_lhs (new_stmt))
4951 : 409 : SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
4952 : : }
4953 : :
4954 : 1113 : for (i = 0; i < nargs; ++i)
4955 : : {
4956 : 771 : vec<tree> oprndsi = vec_oprnds[i];
4957 : 771 : oprndsi.release ();
4958 : : }
4959 : 342 : vargs.release ();
4960 : :
4961 : : /* Mark the clone as no longer being a candidate for GC. */
4962 : 342 : bestn->gc_candidate = false;
4963 : :
4964 : 342 : return true;
4965 : 1332 : }
4966 : :
4967 : :
4968 : : /* Function vect_gen_widened_results_half
4969 : :
4970 : : Create a vector stmt whose code, type, number of arguments, and result
4971 : : variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
4972 : : VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at GSI.
4973 : : In the case that CODE is a CALL_EXPR, this means that a call to DECL
4974 : : needs to be created (DECL is a function-decl of a target-builtin).
4975 : : STMT_INFO is the original scalar stmt that we are vectorizing. */
4976 : :
4977 : : static gimple *
4978 : 28792 : vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
4979 : : tree vec_oprnd0, tree vec_oprnd1, int op_type,
4980 : : tree vec_dest, gimple_stmt_iterator *gsi,
4981 : : stmt_vec_info stmt_info)
4982 : : {
4983 : 28792 : gimple *new_stmt;
4984 : 28792 : tree new_temp;
4985 : :
4986 : : /* Generate half of the widened result: */
4987 : 28792 : if (op_type != binary_op)
4988 : 27742 : vec_oprnd1 = NULL;
4989 : 28792 : new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
4990 : 28792 : new_temp = make_ssa_name (vec_dest, new_stmt);
4991 : 28792 : gimple_set_lhs (new_stmt, new_temp);
4992 : 28792 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4993 : :
4994 : 28792 : return new_stmt;
4995 : : }
4996 : :
4997 : :
4998 : : /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
4999 : : For multi-step conversions store the resulting vectors and call the function
5000 : : recursively. When NARROW_SRC_P is true, there's still a conversion after
5001 : : narrowing, don't store the vectors in the SLP_NODE or in vector info of
5002 : : the scalar statement(or in STMT_VINFO_RELATED_STMT chain). */
5003 : :
5004 : : static void
5005 : 11262 : vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
5006 : : int multi_step_cvt,
5007 : : stmt_vec_info stmt_info,
5008 : : vec<tree> &vec_dsts,
5009 : : gimple_stmt_iterator *gsi,
5010 : : slp_tree slp_node, code_helper code,
5011 : : bool narrow_src_p)
5012 : : {
5013 : 11262 : unsigned int i;
5014 : 11262 : tree vop0, vop1, new_tmp, vec_dest;
5015 : :
5016 : 11262 : vec_dest = vec_dsts.pop ();
5017 : :
5018 : 26491 : for (i = 0; i < vec_oprnds->length (); i += 2)
5019 : : {
5020 : : /* Create demotion operation. */
5021 : 15229 : vop0 = (*vec_oprnds)[i];
5022 : 15229 : vop1 = (*vec_oprnds)[i + 1];
5023 : 15229 : gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
5024 : 15229 : new_tmp = make_ssa_name (vec_dest, new_stmt);
5025 : 15229 : gimple_set_lhs (new_stmt, new_tmp);
5026 : 15229 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5027 : 15229 : if (multi_step_cvt || narrow_src_p)
5028 : : /* Store the resulting vector for next recursive call,
5029 : : or return the resulting vector_tmp for NARROW FLOAT_EXPR. */
5030 : 6091 : (*vec_oprnds)[i/2] = new_tmp;
5031 : : else
5032 : : {
5033 : : /* This is the last step of the conversion sequence. Store the
5034 : : vectors in SLP_NODE. */
5035 : 9138 : slp_node->push_vec_def (new_stmt);
5036 : : }
5037 : : }
5038 : :
5039 : : /* For multi-step demotion operations we first generate demotion operations
5040 : : from the source type to the intermediate types, and then combine the
5041 : : results (stored in VEC_OPRNDS) in demotion operation to the destination
5042 : : type. */
5043 : 11262 : if (multi_step_cvt)
5044 : : {
5045 : : /* At each level of recursion we have half of the operands we had at the
5046 : : previous level. */
5047 : 2700 : vec_oprnds->truncate ((i+1)/2);
5048 : 2700 : vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
5049 : : multi_step_cvt - 1,
5050 : : stmt_info, vec_dsts, gsi,
5051 : 2700 : slp_node, VEC_PACK_TRUNC_EXPR,
5052 : : narrow_src_p);
5053 : : }
5054 : :
5055 : 11262 : vec_dsts.quick_push (vec_dest);
5056 : 11262 : }
5057 : :
5058 : :
5059 : : /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
5060 : : and VEC_OPRNDS1, for a binary operation associated with scalar statement
5061 : : STMT_INFO. For multi-step conversions store the resulting vectors and
5062 : : call the function recursively. */
5063 : :
5064 : : static void
5065 : 10663 : vect_create_vectorized_promotion_stmts (vec_info *vinfo,
5066 : : vec<tree> *vec_oprnds0,
5067 : : vec<tree> *vec_oprnds1,
5068 : : stmt_vec_info stmt_info, tree vec_dest,
5069 : : gimple_stmt_iterator *gsi,
5070 : : code_helper ch1,
5071 : : code_helper ch2, int op_type)
5072 : : {
5073 : 10663 : int i;
5074 : 10663 : tree vop0, vop1, new_tmp1, new_tmp2;
5075 : 10663 : gimple *new_stmt1, *new_stmt2;
5076 : 10663 : vec<tree> vec_tmp = vNULL;
5077 : :
5078 : 10663 : vec_tmp.create (vec_oprnds0->length () * 2);
5079 : 35722 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5080 : : {
5081 : 14396 : if (op_type == binary_op)
5082 : 525 : vop1 = (*vec_oprnds1)[i];
5083 : : else
5084 : : vop1 = NULL_TREE;
5085 : :
5086 : : /* Generate the two halves of promotion operation. */
5087 : 14396 : new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
5088 : : op_type, vec_dest, gsi,
5089 : : stmt_info);
5090 : 14396 : new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
5091 : : op_type, vec_dest, gsi,
5092 : : stmt_info);
5093 : 14396 : if (is_gimple_call (new_stmt1))
5094 : : {
5095 : 0 : new_tmp1 = gimple_call_lhs (new_stmt1);
5096 : 0 : new_tmp2 = gimple_call_lhs (new_stmt2);
5097 : : }
5098 : : else
5099 : : {
5100 : 14396 : new_tmp1 = gimple_assign_lhs (new_stmt1);
5101 : 14396 : new_tmp2 = gimple_assign_lhs (new_stmt2);
5102 : : }
5103 : :
5104 : : /* Store the results for the next step. */
5105 : 14396 : vec_tmp.quick_push (new_tmp1);
5106 : 14396 : vec_tmp.quick_push (new_tmp2);
5107 : : }
5108 : :
5109 : 10663 : vec_oprnds0->release ();
5110 : 10663 : *vec_oprnds0 = vec_tmp;
5111 : 10663 : }
5112 : :
5113 : : /* Create vectorized promotion stmts for widening stmts using only half the
5114 : : potential vector size for input. */
5115 : : static void
5116 : 14 : vect_create_half_widening_stmts (vec_info *vinfo,
5117 : : vec<tree> *vec_oprnds0,
5118 : : vec<tree> *vec_oprnds1,
5119 : : stmt_vec_info stmt_info, tree vec_dest,
5120 : : gimple_stmt_iterator *gsi,
5121 : : code_helper code1,
5122 : : int op_type)
5123 : : {
5124 : 14 : int i;
5125 : 14 : tree vop0, vop1;
5126 : 14 : gimple *new_stmt1;
5127 : 14 : gimple *new_stmt2;
5128 : 14 : gimple *new_stmt3;
5129 : 14 : vec<tree> vec_tmp = vNULL;
5130 : :
5131 : 14 : vec_tmp.create (vec_oprnds0->length ());
5132 : 28 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5133 : : {
5134 : 14 : tree new_tmp1, new_tmp2, new_tmp3, out_type;
5135 : :
5136 : 14 : gcc_assert (op_type == binary_op);
5137 : 14 : vop1 = (*vec_oprnds1)[i];
5138 : :
5139 : : /* Widen the first vector input. */
5140 : 14 : out_type = TREE_TYPE (vec_dest);
5141 : 14 : new_tmp1 = make_ssa_name (out_type);
5142 : 14 : new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
5143 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
5144 : 14 : if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
5145 : : {
5146 : : /* Widen the second vector input. */
5147 : 14 : new_tmp2 = make_ssa_name (out_type);
5148 : 14 : new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
5149 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
5150 : : /* Perform the operation. With both vector inputs widened. */
5151 : 14 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
5152 : : }
5153 : : else
5154 : : {
5155 : : /* Perform the operation. With the single vector input widened. */
5156 : 0 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
5157 : : }
5158 : :
5159 : 14 : new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
5160 : 14 : gimple_assign_set_lhs (new_stmt3, new_tmp3);
5161 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
5162 : :
5163 : : /* Store the results for the next step. */
5164 : 14 : vec_tmp.quick_push (new_tmp3);
5165 : : }
5166 : :
5167 : 14 : vec_oprnds0->release ();
5168 : 14 : *vec_oprnds0 = vec_tmp;
5169 : 14 : }
5170 : :
5171 : :
5172 : : /* Check if STMT_INFO performs a conversion operation that can be vectorized.
5173 : : If COST_VEC is passed, calculate costs but don't change anything,
5174 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5175 : : it, and insert it at GSI.
5176 : : Return true if STMT_INFO is vectorizable in this way. */
5177 : :
5178 : : static bool
5179 : 2312331 : vectorizable_conversion (vec_info *vinfo,
5180 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5181 : : slp_tree slp_node,
5182 : : stmt_vector_for_cost *cost_vec)
5183 : : {
5184 : 2312331 : tree vec_dest, cvt_op = NULL_TREE;
5185 : 2312331 : tree scalar_dest;
5186 : 2312331 : tree op0, op1 = NULL_TREE;
5187 : 2312331 : tree_code tc1;
5188 : 2312331 : code_helper code, code1, code2;
5189 : 2312331 : code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
5190 : 2312331 : tree new_temp;
5191 : 2312331 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
5192 : 2312331 : poly_uint64 nunits_in;
5193 : 2312331 : poly_uint64 nunits_out;
5194 : 2312331 : tree vectype_out, vectype_in;
5195 : 2312331 : int i;
5196 : 2312331 : tree lhs_type, rhs_type;
5197 : : /* For conversions between floating point and integer, there're 2 NARROW
5198 : : cases. NARROW_SRC is for FLOAT_EXPR, means
5199 : : integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
5200 : : This is safe when the range of the source integer can fit into the lower
5201 : : precision. NARROW_DST is for FIX_TRUNC_EXPR, means
5202 : : floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
5203 : : For other conversions, when there's narrowing, NARROW_DST is used as
5204 : : default. */
5205 : 2312331 : enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
5206 : 2312331 : vec<tree> vec_oprnds0 = vNULL;
5207 : 2312331 : vec<tree> vec_oprnds1 = vNULL;
5208 : 2312331 : tree vop0;
5209 : 2312331 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5210 : 2312331 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5211 : 2312331 : int multi_step_cvt = 0;
5212 : 2312331 : vec<tree> interm_types = vNULL;
5213 : 2312331 : tree intermediate_type, cvt_type = NULL_TREE;
5214 : 2312331 : int op_type;
5215 : 2312331 : unsigned short fltsz;
5216 : :
5217 : : /* Is STMT a vectorizable conversion? */
5218 : :
5219 : 2312331 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5220 : : return false;
5221 : :
5222 : 2312331 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5223 : 175983 : && cost_vec)
5224 : : return false;
5225 : :
5226 : 2136348 : gimple* stmt = stmt_info->stmt;
5227 : 2136348 : if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
5228 : : return false;
5229 : :
5230 : 2072077 : if (gimple_get_lhs (stmt) == NULL_TREE
5231 : 2072077 : || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5232 : 777909 : return false;
5233 : :
5234 : 1294168 : if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5235 : : return false;
5236 : :
5237 : 1294168 : if (is_gimple_assign (stmt))
5238 : : {
5239 : 1286197 : code = gimple_assign_rhs_code (stmt);
5240 : 1286197 : op_type = TREE_CODE_LENGTH ((tree_code) code);
5241 : : }
5242 : 7971 : else if (gimple_call_internal_p (stmt))
5243 : : {
5244 : 4376 : code = gimple_call_internal_fn (stmt);
5245 : 4376 : op_type = gimple_call_num_args (stmt);
5246 : : }
5247 : : else
5248 : : return false;
5249 : :
5250 : 1290573 : bool widen_arith = (code == WIDEN_MULT_EXPR
5251 : 1288039 : || code == WIDEN_LSHIFT_EXPR
5252 : 2578612 : || widening_fn_p (code));
5253 : :
5254 : 1288039 : if (!widen_arith
5255 : 1288039 : && !CONVERT_EXPR_CODE_P (code)
5256 : : && code != FIX_TRUNC_EXPR
5257 : : && code != FLOAT_EXPR)
5258 : : return false;
5259 : :
5260 : : /* Check types of lhs and rhs. */
5261 : 153314 : scalar_dest = gimple_get_lhs (stmt);
5262 : 153314 : lhs_type = TREE_TYPE (scalar_dest);
5263 : 153314 : vectype_out = SLP_TREE_VECTYPE (slp_node);
5264 : :
5265 : : /* Check the operands of the operation. */
5266 : 153314 : slp_tree slp_op0, slp_op1 = NULL;
5267 : 153314 : if (!vect_is_simple_use (vinfo, slp_node,
5268 : : 0, &op0, &slp_op0, &dt[0], &vectype_in))
5269 : : {
5270 : 0 : if (dump_enabled_p ())
5271 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5272 : : "use not simple.\n");
5273 : 0 : return false;
5274 : : }
5275 : :
5276 : 153314 : rhs_type = TREE_TYPE (op0);
5277 : 153314 : if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
5278 : 293718 : && !((INTEGRAL_TYPE_P (lhs_type)
5279 : 129200 : && INTEGRAL_TYPE_P (rhs_type))
5280 : : || (SCALAR_FLOAT_TYPE_P (lhs_type)
5281 : 6796 : && SCALAR_FLOAT_TYPE_P (rhs_type))))
5282 : : return false;
5283 : :
5284 : 148906 : if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5285 : 282743 : && ((INTEGRAL_TYPE_P (lhs_type)
5286 : 112871 : && !type_has_mode_precision_p (lhs_type))
5287 : 133414 : || (INTEGRAL_TYPE_P (rhs_type)
5288 : 124569 : && !type_has_mode_precision_p (rhs_type))))
5289 : : {
5290 : 804 : if (dump_enabled_p ())
5291 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5292 : : "type conversion to/from bit-precision unsupported."
5293 : : "\n");
5294 : 804 : return false;
5295 : : }
5296 : :
5297 : 148102 : if (op_type == binary_op)
5298 : : {
5299 : 2534 : gcc_assert (code == WIDEN_MULT_EXPR
5300 : : || code == WIDEN_LSHIFT_EXPR
5301 : : || widening_fn_p (code));
5302 : :
5303 : 2534 : op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
5304 : 0 : gimple_call_arg (stmt, 0);
5305 : 2534 : tree vectype1_in;
5306 : 2534 : if (!vect_is_simple_use (vinfo, slp_node, 1,
5307 : : &op1, &slp_op1, &dt[1], &vectype1_in))
5308 : : {
5309 : 0 : if (dump_enabled_p ())
5310 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5311 : : "use not simple.\n");
5312 : 0 : return false;
5313 : : }
5314 : : /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
5315 : : OP1. */
5316 : 2534 : if (!vectype_in)
5317 : 104 : vectype_in = vectype1_in;
5318 : : }
5319 : :
5320 : : /* If op0 is an external or constant def, infer the vector type
5321 : : from the scalar type. */
5322 : 148102 : if (!vectype_in)
5323 : 18993 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
5324 : 148102 : if (!cost_vec)
5325 : 21910 : gcc_assert (vectype_in);
5326 : 148102 : if (!vectype_in)
5327 : : {
5328 : 256 : if (dump_enabled_p ())
5329 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5330 : : "no vectype for scalar type %T\n", rhs_type);
5331 : :
5332 : 256 : return false;
5333 : : }
5334 : :
5335 : 147846 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
5336 : 162915 : && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
5337 : : {
5338 : 203 : if (dump_enabled_p ())
5339 : 30 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5340 : : "can't convert between boolean and non "
5341 : : "boolean vectors %T\n", rhs_type);
5342 : :
5343 : 203 : return false;
5344 : : }
5345 : :
5346 : 147643 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
5347 : 147643 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5348 : 147643 : if (known_eq (nunits_out, nunits_in))
5349 : 70376 : if (widen_arith)
5350 : : modifier = WIDEN;
5351 : : else
5352 : 147643 : modifier = NONE;
5353 : 77267 : else if (multiple_p (nunits_out, nunits_in))
5354 : : modifier = NARROW_DST;
5355 : : else
5356 : : {
5357 : 42485 : gcc_checking_assert (multiple_p (nunits_in, nunits_out));
5358 : : modifier = WIDEN;
5359 : : }
5360 : :
5361 : 147643 : bool found_mode = false;
5362 : 147643 : scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
5363 : 147643 : scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
5364 : 147643 : opt_scalar_mode rhs_mode_iter;
5365 : 147643 : auto_vec<std::pair<tree, tree_code>, 2> converts;
5366 : :
5367 : : /* Supportable by target? */
5368 : 147643 : switch (modifier)
5369 : : {
5370 : 70143 : case NONE:
5371 : 70143 : if (code != FIX_TRUNC_EXPR
5372 : : && code != FLOAT_EXPR
5373 : : && !CONVERT_EXPR_CODE_P (code))
5374 : : return false;
5375 : 70143 : gcc_assert (code.is_tree_code ());
5376 : 70143 : if (supportable_indirect_convert_operation (code,
5377 : : vectype_out, vectype_in,
5378 : : converts, op0, slp_op0))
5379 : : {
5380 : 17080 : gcc_assert (converts.length () <= 2);
5381 : 17080 : if (converts.length () == 1)
5382 : 17006 : code1 = converts[0].second;
5383 : : else
5384 : : {
5385 : 74 : cvt_type = NULL_TREE;
5386 : 74 : multi_step_cvt = converts.length () - 1;
5387 : 74 : codecvt1 = converts[0].second;
5388 : 74 : code1 = converts[1].second;
5389 : 74 : interm_types.safe_push (converts[0].first);
5390 : : }
5391 : : break;
5392 : : }
5393 : :
5394 : : /* FALLTHRU */
5395 : 53063 : unsupported:
5396 : 59461 : if (dump_enabled_p ())
5397 : 5849 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5398 : : "conversion not supported by target.\n");
5399 : : return false;
5400 : :
5401 : 42718 : case WIDEN:
5402 : 42718 : if (known_eq (nunits_in, nunits_out))
5403 : : {
5404 : 466 : if (!(code.is_tree_code ()
5405 : 233 : && supportable_half_widening_operation ((tree_code) code,
5406 : : vectype_out, vectype_in,
5407 : : &tc1)))
5408 : 70 : goto unsupported;
5409 : 163 : code1 = tc1;
5410 : 163 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5411 : : break;
5412 : : }
5413 : 42485 : if (supportable_widening_operation (vinfo, code, stmt_info,
5414 : : vectype_out, vectype_in, &code1,
5415 : : &code2, &multi_step_cvt,
5416 : : &interm_types))
5417 : : {
5418 : : /* Binary widening operation can only be supported directly by the
5419 : : architecture. */
5420 : 40349 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5421 : : break;
5422 : : }
5423 : :
5424 : 2136 : if (code != FLOAT_EXPR
5425 : 2460 : || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
5426 : 1974 : goto unsupported;
5427 : :
5428 : 162 : fltsz = GET_MODE_SIZE (lhs_mode);
5429 : 237 : FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
5430 : : {
5431 : 237 : rhs_mode = rhs_mode_iter.require ();
5432 : 474 : if (GET_MODE_SIZE (rhs_mode) > fltsz)
5433 : : break;
5434 : :
5435 : 237 : cvt_type
5436 : 237 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5437 : 237 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5438 : 237 : if (cvt_type == NULL_TREE)
5439 : 0 : goto unsupported;
5440 : :
5441 : 474 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5442 : : {
5443 : 57 : tc1 = ERROR_MARK;
5444 : 57 : gcc_assert (code.is_tree_code ());
5445 : 57 : if (!supportable_convert_operation ((tree_code) code, vectype_out,
5446 : : cvt_type, &tc1))
5447 : 22 : goto unsupported;
5448 : 35 : codecvt1 = tc1;
5449 : : }
5450 : 180 : else if (!supportable_widening_operation (vinfo, code,
5451 : : stmt_info, vectype_out,
5452 : : cvt_type, &codecvt1,
5453 : : &codecvt2, &multi_step_cvt,
5454 : : &interm_types))
5455 : 75 : continue;
5456 : : else
5457 : 105 : gcc_assert (multi_step_cvt == 0);
5458 : :
5459 : 140 : if (supportable_widening_operation (vinfo, NOP_EXPR, stmt_info,
5460 : : cvt_type,
5461 : : vectype_in, &code1,
5462 : : &code2, &multi_step_cvt,
5463 : : &interm_types))
5464 : : {
5465 : : found_mode = true;
5466 : : break;
5467 : : }
5468 : : }
5469 : :
5470 : 140 : if (!found_mode)
5471 : 0 : goto unsupported;
5472 : :
5473 : 280 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5474 : 35 : codecvt2 = ERROR_MARK;
5475 : : else
5476 : : {
5477 : 105 : multi_step_cvt++;
5478 : 105 : interm_types.safe_push (cvt_type);
5479 : 105 : cvt_type = NULL_TREE;
5480 : : }
5481 : : break;
5482 : :
5483 : 34782 : case NARROW_DST:
5484 : 34782 : gcc_assert (op_type == unary_op);
5485 : 34782 : if (supportable_narrowing_operation (code, vectype_out, vectype_in,
5486 : : &code1, &multi_step_cvt,
5487 : : &interm_types))
5488 : : break;
5489 : :
5490 : 13806 : if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
5491 : 803 : goto unsupported;
5492 : :
5493 : 3799 : if (code == FIX_TRUNC_EXPR)
5494 : : {
5495 : 274 : cvt_type
5496 : 274 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5497 : 274 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5498 : 274 : if (cvt_type == NULL_TREE)
5499 : 0 : goto unsupported;
5500 : 274 : if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
5501 : : &tc1))
5502 : 272 : codecvt1 = tc1;
5503 : : else
5504 : 2 : goto unsupported;
5505 : 272 : if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
5506 : : &code1, &multi_step_cvt,
5507 : : &interm_types))
5508 : : break;
5509 : : }
5510 : : /* If op0 can be represented with low precision integer,
5511 : : truncate it to cvt_type and the do FLOAT_EXPR. */
5512 : 3525 : else if (code == FLOAT_EXPR)
5513 : : {
5514 : 96 : wide_int op_min_value, op_max_value;
5515 : 96 : tree def;
5516 : : /* ??? Merge ranges in case of more than one lane. */
5517 : 96 : if (SLP_TREE_LANES (slp_op0) != 1
5518 : 94 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
5519 : 190 : || !vect_get_range_info (def, &op_min_value, &op_max_value))
5520 : 94 : goto unsupported;
5521 : :
5522 : 2 : cvt_type
5523 : 2 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
5524 : 2 : if (cvt_type == NULL_TREE
5525 : 2 : || (wi::min_precision (op_max_value, SIGNED)
5526 : 2 : > TYPE_PRECISION (cvt_type))
5527 : 4 : || (wi::min_precision (op_min_value, SIGNED)
5528 : 2 : > TYPE_PRECISION (cvt_type)))
5529 : 0 : goto unsupported;
5530 : :
5531 : 2 : cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
5532 : 2 : if (cvt_type == NULL_TREE)
5533 : 0 : goto unsupported;
5534 : 2 : if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
5535 : : &code1, &multi_step_cvt,
5536 : : &interm_types))
5537 : 0 : goto unsupported;
5538 : 2 : if (supportable_convert_operation ((tree_code) code, vectype_out,
5539 : : cvt_type, &tc1))
5540 : : {
5541 : 2 : codecvt1 = tc1;
5542 : 2 : modifier = NARROW_SRC;
5543 : 2 : break;
5544 : : }
5545 : 96 : }
5546 : :
5547 : 3433 : goto unsupported;
5548 : :
5549 : : default:
5550 : : gcc_unreachable ();
5551 : : }
5552 : :
5553 : 88182 : if (modifier == WIDEN
5554 : 88182 : && loop_vinfo
5555 : 39557 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
5556 : 88214 : && (code1 == VEC_WIDEN_MULT_EVEN_EXPR
5557 : 30 : || widening_evenodd_fn_p (code1)))
5558 : : {
5559 : 2 : if (dump_enabled_p ())
5560 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5561 : : "can't use a fully-masked loop because"
5562 : : " widening operation on even/odd elements"
5563 : : " mixes up lanes.\n");
5564 : 2 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
5565 : : }
5566 : :
5567 : 88182 : if (cost_vec) /* transformation not required. */
5568 : : {
5569 : 66272 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
5570 : 66272 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in))
5571 : : {
5572 : 0 : if (dump_enabled_p ())
5573 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5574 : : "incompatible vector types for invariants\n");
5575 : 0 : return false;
5576 : : }
5577 : 66272 : DUMP_VECT_SCOPE ("vectorizable_conversion");
5578 : 66272 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
5579 : 66272 : if (modifier == NONE)
5580 : : {
5581 : 12876 : SLP_TREE_TYPE (slp_node) = type_conversion_vec_info_type;
5582 : 12876 : vect_model_simple_cost (vinfo, (1 + multi_step_cvt),
5583 : : slp_node, cost_vec);
5584 : : }
5585 : 53396 : else if (modifier == NARROW_SRC || modifier == NARROW_DST)
5586 : : {
5587 : 21888 : SLP_TREE_TYPE (slp_node) = type_demotion_vec_info_type;
5588 : : /* The final packing step produces one vector result per copy. */
5589 : 21888 : vect_model_promotion_demotion_cost (slp_node, nvectors,
5590 : : multi_step_cvt, cost_vec,
5591 : : widen_arith);
5592 : : }
5593 : : else
5594 : : {
5595 : 31508 : SLP_TREE_TYPE (slp_node) = type_promotion_vec_info_type;
5596 : : /* The initial unpacking step produces two vector results
5597 : : per copy. MULTI_STEP_CVT is 0 for a single conversion,
5598 : : so >> MULTI_STEP_CVT divides by 2^(number of steps - 1). */
5599 : 31508 : vect_model_promotion_demotion_cost (slp_node,
5600 : : nvectors >> multi_step_cvt,
5601 : : multi_step_cvt, cost_vec,
5602 : : widen_arith);
5603 : : }
5604 : 66272 : interm_types.release ();
5605 : 66272 : return true;
5606 : : }
5607 : :
5608 : : /* Transform. */
5609 : 21910 : if (dump_enabled_p ())
5610 : 4174 : dump_printf_loc (MSG_NOTE, vect_location, "transform conversion.\n");
5611 : :
5612 : 21910 : if (op_type == binary_op)
5613 : : {
5614 : 485 : if (CONSTANT_CLASS_P (op0))
5615 : 0 : op0 = fold_convert (TREE_TYPE (op1), op0);
5616 : 485 : else if (CONSTANT_CLASS_P (op1))
5617 : 231 : op1 = fold_convert (TREE_TYPE (op0), op1);
5618 : : }
5619 : :
5620 : : /* In case of multi-step conversion, we first generate conversion operations
5621 : : to the intermediate types, and then from that types to the final one.
5622 : : We create vector destinations for the intermediate type (TYPES) received
5623 : : from supportable_*_operation, and store them in the correct order
5624 : : for future use in vect_create_vectorized_*_stmts (). */
5625 : 43820 : auto_vec<tree> vec_dsts (multi_step_cvt + 1);
5626 : 21910 : bool widen_or_narrow_float_p
5627 : 21910 : = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
5628 : 21910 : vec_dest = vect_create_destination_var (scalar_dest,
5629 : : widen_or_narrow_float_p
5630 : : ? cvt_type : vectype_out);
5631 : 21910 : vec_dsts.quick_push (vec_dest);
5632 : :
5633 : 21910 : if (multi_step_cvt)
5634 : : {
5635 : 7634 : for (i = interm_types.length () - 1;
5636 : 8071 : interm_types.iterate (i, &intermediate_type); i--)
5637 : : {
5638 : 4254 : vec_dest = vect_create_destination_var (scalar_dest,
5639 : : intermediate_type);
5640 : 4254 : vec_dsts.quick_push (vec_dest);
5641 : : }
5642 : : }
5643 : :
5644 : 21910 : if (cvt_type)
5645 : 69 : vec_dest = vect_create_destination_var (scalar_dest,
5646 : : widen_or_narrow_float_p
5647 : : ? vectype_out : cvt_type);
5648 : :
5649 : 21910 : switch (modifier)
5650 : : {
5651 : 4204 : case NONE:
5652 : 4204 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5653 : : /* vec_dest is intermediate type operand when multi_step_cvt. */
5654 : 4204 : if (multi_step_cvt)
5655 : : {
5656 : 21 : cvt_op = vec_dest;
5657 : 21 : vec_dest = vec_dsts[0];
5658 : : }
5659 : :
5660 : 8733 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5661 : : {
5662 : : /* Arguments are ready, create the new vector stmt. */
5663 : 4529 : gimple* new_stmt;
5664 : 4529 : if (multi_step_cvt)
5665 : : {
5666 : 21 : gcc_assert (multi_step_cvt == 1);
5667 : 21 : new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
5668 : 21 : new_temp = make_ssa_name (cvt_op, new_stmt);
5669 : 21 : gimple_assign_set_lhs (new_stmt, new_temp);
5670 : 21 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5671 : 21 : vop0 = new_temp;
5672 : : }
5673 : 4529 : new_stmt = vect_gimple_build (vec_dest, code1, vop0);
5674 : 4529 : new_temp = make_ssa_name (vec_dest, new_stmt);
5675 : 4529 : gimple_set_lhs (new_stmt, new_temp);
5676 : 4529 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5677 : :
5678 : 4529 : slp_node->push_vec_def (new_stmt);
5679 : : }
5680 : : break;
5681 : :
5682 : 9144 : case WIDEN:
5683 : : /* In case the vectorization factor (VF) is bigger than the number
5684 : : of elements that we can fit in a vectype (nunits), we have to
5685 : : generate more than one vector stmt - i.e - we need to "unroll"
5686 : : the vector stmt by a factor VF/nunits. */
5687 : 9144 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0,
5688 : 9144 : code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
5689 : : &vec_oprnds1);
5690 : 9144 : if (code == WIDEN_LSHIFT_EXPR)
5691 : : {
5692 : 0 : int oprnds_size = vec_oprnds0.length ();
5693 : 0 : vec_oprnds1.create (oprnds_size);
5694 : 0 : for (i = 0; i < oprnds_size; ++i)
5695 : 0 : vec_oprnds1.quick_push (op1);
5696 : : }
5697 : : /* Arguments are ready. Create the new vector stmts. */
5698 : 19821 : for (i = multi_step_cvt; i >= 0; i--)
5699 : : {
5700 : 10677 : tree this_dest = vec_dsts[i];
5701 : 10677 : code_helper c1 = code1, c2 = code2;
5702 : 10677 : if (i == 0 && codecvt2 != ERROR_MARK)
5703 : : {
5704 : 48 : c1 = codecvt1;
5705 : 48 : c2 = codecvt2;
5706 : : }
5707 : 10677 : if (known_eq (nunits_out, nunits_in))
5708 : 14 : vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
5709 : : stmt_info, this_dest, gsi, c1,
5710 : : op_type);
5711 : : else
5712 : 10663 : vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
5713 : : &vec_oprnds1, stmt_info,
5714 : : this_dest, gsi,
5715 : : c1, c2, op_type);
5716 : : }
5717 : :
5718 : 34328 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5719 : : {
5720 : 25184 : gimple *new_stmt;
5721 : 25184 : if (cvt_type)
5722 : : {
5723 : 120 : new_temp = make_ssa_name (vec_dest);
5724 : 120 : new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5725 : 120 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5726 : : }
5727 : : else
5728 : 25064 : new_stmt = SSA_NAME_DEF_STMT (vop0);
5729 : :
5730 : 25184 : slp_node->push_vec_def (new_stmt);
5731 : : }
5732 : : break;
5733 : :
5734 : 8562 : case NARROW_SRC:
5735 : 8562 : case NARROW_DST:
5736 : : /* In case the vectorization factor (VF) is bigger than the number
5737 : : of elements that we can fit in a vectype (nunits), we have to
5738 : : generate more than one vector stmt - i.e - we need to "unroll"
5739 : : the vector stmt by a factor VF/nunits. */
5740 : 8562 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5741 : : /* Arguments are ready. Create the new vector stmts. */
5742 : 8562 : if (cvt_type && modifier == NARROW_DST)
5743 : 153 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5744 : : {
5745 : 124 : new_temp = make_ssa_name (vec_dest);
5746 : 124 : gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5747 : 124 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5748 : 124 : vec_oprnds0[i] = new_temp;
5749 : : }
5750 : :
5751 : 8562 : vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
5752 : : multi_step_cvt,
5753 : : stmt_info, vec_dsts, gsi,
5754 : : slp_node, code1,
5755 : : modifier == NARROW_SRC);
5756 : : /* After demoting op0 to cvt_type, convert it to dest. */
5757 : 8562 : if (cvt_type && code == FLOAT_EXPR)
5758 : : {
5759 : 4 : for (unsigned int i = 0; i != vec_oprnds0.length() / 2; i++)
5760 : : {
5761 : : /* Arguments are ready, create the new vector stmt. */
5762 : 1 : gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
5763 : 1 : gimple *new_stmt
5764 : 1 : = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
5765 : 1 : new_temp = make_ssa_name (vec_dest, new_stmt);
5766 : 1 : gimple_set_lhs (new_stmt, new_temp);
5767 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5768 : :
5769 : : /* This is the last step of the conversion sequence. Store the
5770 : : vectors in SLP_NODE or in vector info of the scalar statement
5771 : : (or in STMT_VINFO_RELATED_STMT chain). */
5772 : 1 : slp_node->push_vec_def (new_stmt);
5773 : : }
5774 : : }
5775 : : break;
5776 : : }
5777 : :
5778 : 21910 : vec_oprnds0.release ();
5779 : 21910 : vec_oprnds1.release ();
5780 : 21910 : interm_types.release ();
5781 : :
5782 : 21910 : return true;
5783 : 147643 : }
5784 : :
5785 : : /* Return true if we can assume from the scalar form of STMT_INFO that
5786 : : neither the scalar nor the vector forms will generate code. STMT_INFO
5787 : : is known not to involve a data reference. */
5788 : :
5789 : : bool
5790 : 1168552 : vect_nop_conversion_p (stmt_vec_info stmt_info)
5791 : : {
5792 : 1168552 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5793 : 915088 : if (!stmt)
5794 : : return false;
5795 : :
5796 : 915088 : tree lhs = gimple_assign_lhs (stmt);
5797 : 915088 : tree_code code = gimple_assign_rhs_code (stmt);
5798 : 915088 : tree rhs = gimple_assign_rhs1 (stmt);
5799 : :
5800 : 915088 : if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
5801 : : return true;
5802 : :
5803 : 913119 : if (CONVERT_EXPR_CODE_P (code))
5804 : 195976 : return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
5805 : :
5806 : : return false;
5807 : : }
5808 : :
5809 : : /* Function vectorizable_assignment.
5810 : :
5811 : : Check if STMT_INFO performs an assignment (copy) that can be vectorized.
5812 : : If COST_VEC is passed, calculate costs but don't change anything,
5813 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5814 : : it, and insert it at GSI.
5815 : : Return true if STMT_INFO is vectorizable in this way. */
5816 : :
5817 : : static bool
5818 : 1840013 : vectorizable_assignment (vec_info *vinfo,
5819 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5820 : : slp_tree slp_node,
5821 : : stmt_vector_for_cost *cost_vec)
5822 : : {
5823 : 1840013 : tree vec_dest;
5824 : 1840013 : tree scalar_dest;
5825 : 1840013 : tree op;
5826 : 1840013 : tree new_temp;
5827 : 1840013 : enum vect_def_type dt[1] = {vect_unknown_def_type};
5828 : 1840013 : int i;
5829 : 1840013 : vec<tree> vec_oprnds = vNULL;
5830 : 1840013 : tree vop;
5831 : 1840013 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5832 : 1840013 : enum tree_code code;
5833 : 1840013 : tree vectype_in;
5834 : :
5835 : 1840013 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5836 : : return false;
5837 : :
5838 : 1840013 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5839 : 175983 : && cost_vec)
5840 : : return false;
5841 : :
5842 : : /* Is vectorizable assignment? */
5843 : 3360343 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5844 : 1590881 : if (!stmt)
5845 : : return false;
5846 : :
5847 : 1590881 : scalar_dest = gimple_assign_lhs (stmt);
5848 : 1590881 : if (TREE_CODE (scalar_dest) != SSA_NAME)
5849 : : return false;
5850 : :
5851 : 813879 : if (STMT_VINFO_DATA_REF (stmt_info))
5852 : : return false;
5853 : :
5854 : 351310 : code = gimple_assign_rhs_code (stmt);
5855 : 351310 : if (!(gimple_assign_single_p (stmt)
5856 : 350104 : || code == PAREN_EXPR
5857 : 348526 : || CONVERT_EXPR_CODE_P (code)))
5858 : : return false;
5859 : :
5860 : 80821 : tree vectype = SLP_TREE_VECTYPE (slp_node);
5861 : 80821 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5862 : :
5863 : 80821 : slp_tree slp_op;
5864 : 80821 : if (!vect_is_simple_use (vinfo, slp_node, 0, &op, &slp_op,
5865 : : &dt[0], &vectype_in))
5866 : : {
5867 : 0 : if (dump_enabled_p ())
5868 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5869 : : "use not simple.\n");
5870 : 0 : return false;
5871 : : }
5872 : 80821 : if (!vectype_in)
5873 : 17044 : vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
5874 : :
5875 : : /* We can handle VIEW_CONVERT conversions that do not change the number
5876 : : of elements or the vector size or other conversions when the component
5877 : : types are nop-convertible. */
5878 : 80821 : if (!vectype_in
5879 : 80545 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
5880 : 73824 : || (code == VIEW_CONVERT_EXPR
5881 : 2148 : && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
5882 : 2148 : GET_MODE_SIZE (TYPE_MODE (vectype_in))))
5883 : 154645 : || (CONVERT_EXPR_CODE_P (code)
5884 : 71072 : && !tree_nop_conversion_p (TREE_TYPE (vectype),
5885 : 71072 : TREE_TYPE (vectype_in))))
5886 : 10010 : return false;
5887 : :
5888 : 212319 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
5889 : : {
5890 : 2 : if (dump_enabled_p ())
5891 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5892 : : "can't convert between boolean and non "
5893 : 0 : "boolean vectors %T\n", TREE_TYPE (op));
5894 : :
5895 : 2 : return false;
5896 : : }
5897 : :
5898 : : /* We do not handle bit-precision changes. */
5899 : 70809 : if ((CONVERT_EXPR_CODE_P (code)
5900 : 2752 : || code == VIEW_CONVERT_EXPR)
5901 : 69131 : && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
5902 : 67863 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
5903 : 68833 : || (INTEGRAL_TYPE_P (TREE_TYPE (op))
5904 : 64529 : && !type_has_mode_precision_p (TREE_TYPE (op))))
5905 : : /* But a conversion that does not change the bit-pattern is ok. */
5906 : 71475 : && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
5907 : 666 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
5908 : 666 : && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
5909 : 666 : > TYPE_PRECISION (TREE_TYPE (op)))
5910 : 368 : && TYPE_UNSIGNED (TREE_TYPE (op)))
5911 : 314 : || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
5912 : 314 : == TYPE_PRECISION (TREE_TYPE (op))))))
5913 : : {
5914 : 258 : if (dump_enabled_p ())
5915 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5916 : : "type conversion to/from bit-precision "
5917 : : "unsupported.\n");
5918 : 258 : return false;
5919 : : }
5920 : :
5921 : 70551 : if (cost_vec) /* transformation not required. */
5922 : : {
5923 : 56289 : if (!vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
5924 : : {
5925 : 0 : if (dump_enabled_p ())
5926 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5927 : : "incompatible vector types for invariants\n");
5928 : 0 : return false;
5929 : : }
5930 : 56289 : SLP_TREE_TYPE (slp_node) = assignment_vec_info_type;
5931 : 56289 : DUMP_VECT_SCOPE ("vectorizable_assignment");
5932 : 56289 : if (!vect_nop_conversion_p (stmt_info))
5933 : 1325 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
5934 : 56289 : return true;
5935 : : }
5936 : :
5937 : : /* Transform. */
5938 : 14262 : if (dump_enabled_p ())
5939 : 3485 : dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
5940 : :
5941 : : /* Handle def. */
5942 : 14262 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
5943 : :
5944 : : /* Handle use. */
5945 : 14262 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
5946 : :
5947 : : /* Arguments are ready. create the new vector stmt. */
5948 : 32278 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
5949 : : {
5950 : 18016 : if (CONVERT_EXPR_CODE_P (code)
5951 : 642 : || code == VIEW_CONVERT_EXPR)
5952 : 17466 : vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
5953 : 18016 : gassign *new_stmt = gimple_build_assign (vec_dest, vop);
5954 : 18016 : new_temp = make_ssa_name (vec_dest, new_stmt);
5955 : 18016 : gimple_assign_set_lhs (new_stmt, new_temp);
5956 : 18016 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5957 : 18016 : slp_node->push_vec_def (new_stmt);
5958 : : }
5959 : :
5960 : 14262 : vec_oprnds.release ();
5961 : 14262 : return true;
5962 : : }
5963 : :
5964 : :
5965 : : /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
5966 : : either as shift by a scalar or by a vector. */
5967 : :
5968 : : bool
5969 : 269717 : vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
5970 : : {
5971 : 269717 : optab optab;
5972 : 269717 : tree vectype;
5973 : :
5974 : 269717 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
5975 : 269717 : if (!vectype)
5976 : : return false;
5977 : :
5978 : 269717 : optab = optab_for_tree_code (code, vectype, optab_scalar);
5979 : 269717 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
5980 : : return true;
5981 : :
5982 : 236564 : optab = optab_for_tree_code (code, vectype, optab_vector);
5983 : 236564 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
5984 : : return true;
5985 : :
5986 : : return false;
5987 : : }
5988 : :
5989 : :
5990 : : /* Function vectorizable_shift.
5991 : :
5992 : : Check if STMT_INFO performs a shift operation that can be vectorized.
5993 : : If COST_VEC is passed, calculate costs but don't change anything,
5994 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5995 : : it, and insert it at GSI.
5996 : : Return true if STMT_INFO is vectorizable in this way. */
5997 : :
5998 : : static bool
5999 : 625951 : vectorizable_shift (vec_info *vinfo,
6000 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6001 : : slp_tree slp_node,
6002 : : stmt_vector_for_cost *cost_vec)
6003 : : {
6004 : 625951 : tree vec_dest;
6005 : 625951 : tree scalar_dest;
6006 : 625951 : tree op0, op1 = NULL;
6007 : 625951 : tree vec_oprnd1 = NULL_TREE;
6008 : 625951 : tree vectype;
6009 : 625951 : enum tree_code code;
6010 : 625951 : machine_mode vec_mode;
6011 : 625951 : tree new_temp;
6012 : 625951 : optab optab;
6013 : 625951 : int icode;
6014 : 625951 : machine_mode optab_op2_mode;
6015 : 625951 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
6016 : 625951 : poly_uint64 nunits_in;
6017 : 625951 : poly_uint64 nunits_out;
6018 : 625951 : tree vectype_out;
6019 : 625951 : tree op1_vectype;
6020 : 625951 : int i;
6021 : 625951 : vec<tree> vec_oprnds0 = vNULL;
6022 : 625951 : vec<tree> vec_oprnds1 = vNULL;
6023 : 625951 : tree vop0, vop1;
6024 : 625951 : unsigned int k;
6025 : 625951 : bool scalar_shift_arg = true;
6026 : 625951 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6027 : 625951 : bool incompatible_op1_vectype_p = false;
6028 : :
6029 : 625951 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6030 : : return false;
6031 : :
6032 : 625951 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6033 : 175983 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
6034 : 174679 : && cost_vec)
6035 : : return false;
6036 : :
6037 : : /* Is STMT a vectorizable binary/unary operation? */
6038 : 956213 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6039 : 378535 : if (!stmt)
6040 : : return false;
6041 : :
6042 : 378535 : if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6043 : : return false;
6044 : :
6045 : 378045 : code = gimple_assign_rhs_code (stmt);
6046 : :
6047 : 378045 : if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
6048 : : || code == RROTATE_EXPR))
6049 : : return false;
6050 : :
6051 : 54303 : scalar_dest = gimple_assign_lhs (stmt);
6052 : 54303 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6053 : 54303 : if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6054 : : {
6055 : 0 : if (dump_enabled_p ())
6056 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6057 : : "bit-precision shifts not supported.\n");
6058 : 0 : return false;
6059 : : }
6060 : :
6061 : 54303 : slp_tree slp_op0;
6062 : 54303 : if (!vect_is_simple_use (vinfo, slp_node,
6063 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6064 : : {
6065 : 0 : if (dump_enabled_p ())
6066 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6067 : : "use not simple.\n");
6068 : 0 : return false;
6069 : : }
6070 : : /* If op0 is an external or constant def, infer the vector type
6071 : : from the scalar type. */
6072 : 54303 : if (!vectype)
6073 : 11951 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
6074 : 54303 : if (!cost_vec)
6075 : 7508 : gcc_assert (vectype);
6076 : 54303 : if (!vectype)
6077 : : {
6078 : 0 : if (dump_enabled_p ())
6079 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6080 : : "no vectype for scalar type\n");
6081 : 0 : return false;
6082 : : }
6083 : :
6084 : 54303 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6085 : 54303 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6086 : 54303 : if (maybe_ne (nunits_out, nunits_in))
6087 : : return false;
6088 : :
6089 : 54303 : stmt_vec_info op1_def_stmt_info;
6090 : 54303 : slp_tree slp_op1;
6091 : 54303 : if (!vect_is_simple_use (vinfo, slp_node, 1, &op1, &slp_op1,
6092 : : &dt[1], &op1_vectype, &op1_def_stmt_info))
6093 : : {
6094 : 0 : if (dump_enabled_p ())
6095 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6096 : : "use not simple.\n");
6097 : 0 : return false;
6098 : : }
6099 : :
6100 : : /* Determine whether the shift amount is a vector, or scalar. If the
6101 : : shift/rotate amount is a vector, use the vector/vector shift optabs. */
6102 : :
6103 : 54303 : if ((dt[1] == vect_internal_def
6104 : 54303 : || dt[1] == vect_induction_def
6105 : 41711 : || dt[1] == vect_nested_cycle)
6106 : 12610 : && SLP_TREE_LANES (slp_node) == 1)
6107 : : scalar_shift_arg = false;
6108 : 41748 : else if (dt[1] == vect_constant_def
6109 : : || dt[1] == vect_external_def
6110 : 41748 : || dt[1] == vect_internal_def)
6111 : : {
6112 : : /* In SLP, need to check whether the shift count is the same,
6113 : : in loops if it is a constant or invariant, it is always
6114 : : a scalar shift. */
6115 : 41742 : vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
6116 : 41742 : stmt_vec_info slpstmt_info;
6117 : :
6118 : 114345 : FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
6119 : 72603 : if (slpstmt_info)
6120 : : {
6121 : 72603 : gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
6122 : 145206 : if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
6123 : 72603 : scalar_shift_arg = false;
6124 : : }
6125 : :
6126 : : /* For internal SLP defs we have to make sure we see scalar stmts
6127 : : for all vector elements.
6128 : : ??? For different vectors we could resort to a different
6129 : : scalar shift operand but code-generation below simply always
6130 : : takes the first. */
6131 : 41742 : if (dt[1] == vect_internal_def
6132 : 41791 : && maybe_ne (nunits_out * vect_get_num_copies (vinfo, slp_node),
6133 : 49 : stmts.length ()))
6134 : : scalar_shift_arg = false;
6135 : :
6136 : : /* If the shift amount is computed by a pattern stmt we cannot
6137 : : use the scalar amount directly thus give up and use a vector
6138 : : shift. */
6139 : 41742 : if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
6140 : : scalar_shift_arg = false;
6141 : : }
6142 : : else
6143 : : {
6144 : 6 : if (dump_enabled_p ())
6145 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6146 : : "operand mode requires invariant argument.\n");
6147 : 6 : return false;
6148 : : }
6149 : :
6150 : : /* Vector shifted by vector. */
6151 : 54329 : bool was_scalar_shift_arg = scalar_shift_arg;
6152 : 41733 : if (!scalar_shift_arg)
6153 : : {
6154 : 12596 : optab = optab_for_tree_code (code, vectype, optab_vector);
6155 : 12596 : if (dump_enabled_p ())
6156 : 1179 : dump_printf_loc (MSG_NOTE, vect_location,
6157 : : "vector/vector shift/rotate found.\n");
6158 : :
6159 : 12596 : if (!op1_vectype)
6160 : 9 : op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
6161 : : slp_op1);
6162 : 12596 : incompatible_op1_vectype_p
6163 : 25192 : = (op1_vectype == NULL_TREE
6164 : 12596 : || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
6165 : 12596 : TYPE_VECTOR_SUBPARTS (vectype))
6166 : 25190 : || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
6167 : 12589 : if (incompatible_op1_vectype_p
6168 : 7 : && (SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
6169 : 1 : || slp_op1->refcnt != 1))
6170 : : {
6171 : 6 : if (dump_enabled_p ())
6172 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6173 : : "unusable type for last operand in"
6174 : : " vector/vector shift/rotate.\n");
6175 : 6 : return false;
6176 : : }
6177 : : }
6178 : : /* See if the machine has a vector shifted by scalar insn and if not
6179 : : then see if it has a vector shifted by vector insn. */
6180 : : else
6181 : : {
6182 : 41701 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6183 : 41701 : if (optab
6184 : 41701 : && can_implement_p (optab, TYPE_MODE (vectype)))
6185 : : {
6186 : 41701 : if (dump_enabled_p ())
6187 : 4874 : dump_printf_loc (MSG_NOTE, vect_location,
6188 : : "vector/scalar shift/rotate found.\n");
6189 : : }
6190 : : else
6191 : : {
6192 : 0 : optab = optab_for_tree_code (code, vectype, optab_vector);
6193 : 0 : if (optab
6194 : 0 : && can_implement_p (optab, TYPE_MODE (vectype)))
6195 : : {
6196 : 0 : scalar_shift_arg = false;
6197 : :
6198 : 0 : if (dump_enabled_p ())
6199 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6200 : : "vector/vector shift/rotate found.\n");
6201 : :
6202 : 0 : if (!op1_vectype)
6203 : 0 : op1_vectype = get_vectype_for_scalar_type (vinfo,
6204 : 0 : TREE_TYPE (op1),
6205 : : slp_op1);
6206 : :
6207 : : /* Unlike the other binary operators, shifts/rotates have
6208 : : the rhs being int, instead of the same type as the lhs,
6209 : : so make sure the scalar is the right type if we are
6210 : : dealing with vectors of long long/long/short/char. */
6211 : 0 : incompatible_op1_vectype_p
6212 : 0 : = (!op1_vectype
6213 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype),
6214 : 0 : TREE_TYPE (op1)));
6215 : 0 : if (incompatible_op1_vectype_p
6216 : 0 : && dt[1] == vect_internal_def)
6217 : : {
6218 : 0 : if (dump_enabled_p ())
6219 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6220 : : "unusable type for last operand in"
6221 : : " vector/vector shift/rotate.\n");
6222 : 0 : return false;
6223 : : }
6224 : : }
6225 : : }
6226 : : }
6227 : :
6228 : : /* Supportable by target? */
6229 : 54291 : if (!optab)
6230 : : {
6231 : 0 : if (dump_enabled_p ())
6232 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6233 : : "no optab.\n");
6234 : 0 : return false;
6235 : : }
6236 : 54291 : vec_mode = TYPE_MODE (vectype);
6237 : 54291 : icode = (int) optab_handler (optab, vec_mode);
6238 : 54291 : if (icode == CODE_FOR_nothing)
6239 : : {
6240 : 6018 : if (dump_enabled_p ())
6241 : 886 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6242 : : "op not supported by target.\n");
6243 : 6018 : return false;
6244 : : }
6245 : : /* vector lowering cannot optimize vector shifts using word arithmetic. */
6246 : 48273 : if (vect_emulated_vector_p (vectype))
6247 : : return false;
6248 : :
6249 : 48273 : if (cost_vec) /* transformation not required. */
6250 : : {
6251 : 40765 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6252 : 40765 : || ((!scalar_shift_arg || dt[1] == vect_internal_def)
6253 : 4666 : && (!incompatible_op1_vectype_p
6254 : 1 : || dt[1] == vect_constant_def)
6255 : 4666 : && !vect_maybe_update_slp_op_vectype
6256 : 4666 : (slp_op1,
6257 : : incompatible_op1_vectype_p ? vectype : op1_vectype)))
6258 : : {
6259 : 0 : if (dump_enabled_p ())
6260 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6261 : : "incompatible vector types for invariants\n");
6262 : 0 : return false;
6263 : : }
6264 : : /* Now adjust the constant shift amount in place. */
6265 : 40765 : if (incompatible_op1_vectype_p
6266 : 1 : && dt[1] == vect_constant_def)
6267 : 4 : for (unsigned i = 0;
6268 : 5 : i < SLP_TREE_SCALAR_OPS (slp_op1).length (); ++i)
6269 : : {
6270 : 4 : SLP_TREE_SCALAR_OPS (slp_op1)[i]
6271 : 4 : = fold_convert (TREE_TYPE (vectype),
6272 : : SLP_TREE_SCALAR_OPS (slp_op1)[i]);
6273 : 4 : gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
6274 : : == INTEGER_CST));
6275 : : }
6276 : 40765 : SLP_TREE_TYPE (slp_node) = shift_vec_info_type;
6277 : 40765 : DUMP_VECT_SCOPE ("vectorizable_shift");
6278 : 40765 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6279 : 40765 : return true;
6280 : : }
6281 : :
6282 : : /* Transform. */
6283 : :
6284 : 7508 : if (dump_enabled_p ())
6285 : 1986 : dump_printf_loc (MSG_NOTE, vect_location,
6286 : : "transform binary/unary operation.\n");
6287 : :
6288 : : /* Handle def. */
6289 : 7508 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6290 : :
6291 : 7508 : unsigned nvectors = vect_get_num_copies (vinfo, slp_node);
6292 : 7508 : if (scalar_shift_arg && dt[1] != vect_internal_def)
6293 : : {
6294 : : /* Vector shl and shr insn patterns can be defined with scalar
6295 : : operand 2 (shift operand). In this case, use constant or loop
6296 : : invariant op1 directly, without extending it to vector mode
6297 : : first. */
6298 : 5582 : optab_op2_mode = insn_data[icode].operand[2].mode;
6299 : 5582 : if (!VECTOR_MODE_P (optab_op2_mode))
6300 : : {
6301 : 5582 : if (dump_enabled_p ())
6302 : 1876 : dump_printf_loc (MSG_NOTE, vect_location,
6303 : : "operand 1 using scalar mode.\n");
6304 : 5582 : vec_oprnd1 = op1;
6305 : 5582 : vec_oprnds1.create (nvectors);
6306 : 5582 : vec_oprnds1.quick_push (vec_oprnd1);
6307 : : /* Store vec_oprnd1 for every vector stmt to be created.
6308 : : We check during the analysis that all the shift arguments
6309 : : are the same.
6310 : : TODO: Allow different constants for different vector
6311 : : stmts generated for an SLP instance. */
6312 : 13268 : for (k = 0; k < nvectors - 1; k++)
6313 : 2104 : vec_oprnds1.quick_push (vec_oprnd1);
6314 : : }
6315 : : }
6316 : 1926 : else if (!scalar_shift_arg && incompatible_op1_vectype_p)
6317 : : {
6318 : 0 : if (was_scalar_shift_arg)
6319 : : {
6320 : : /* If the argument was the same in all lanes create the
6321 : : correctly typed vector shift amount directly. Note
6322 : : we made SLP scheduling think we use the original scalars,
6323 : : so place the compensation code next to the shift which
6324 : : is conservative. See PR119640 where it otherwise breaks. */
6325 : 0 : op1 = fold_convert (TREE_TYPE (vectype), op1);
6326 : 0 : op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
6327 : : gsi);
6328 : 0 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
6329 : : gsi);
6330 : 0 : vec_oprnds1.create (nvectors);
6331 : 0 : for (k = 0; k < nvectors; k++)
6332 : 0 : vec_oprnds1.quick_push (vec_oprnd1);
6333 : : }
6334 : 0 : else if (dt[1] == vect_constant_def)
6335 : : /* The constant shift amount has been adjusted in place. */
6336 : : ;
6337 : : else
6338 : 0 : gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
6339 : : }
6340 : :
6341 : : /* vec_oprnd1 is available if operand 1 should be of a scalar-type
6342 : : (a special case for certain kind of vector shifts); otherwise,
6343 : : operand 1 should be of a vector type (the usual case). */
6344 : 1926 : vect_get_vec_defs (vinfo, slp_node,
6345 : : op0, &vec_oprnds0,
6346 : 7508 : vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
6347 : :
6348 : : /* Arguments are ready. Create the new vector stmt. */
6349 : 20112 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6350 : : {
6351 : : /* For internal defs where we need to use a scalar shift arg
6352 : : extract the first lane. */
6353 : 12604 : if (scalar_shift_arg && dt[1] == vect_internal_def)
6354 : : {
6355 : 10 : vop1 = vec_oprnds1[0];
6356 : 10 : new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
6357 : 10 : gassign *new_stmt
6358 : 10 : = gimple_build_assign (new_temp,
6359 : 10 : build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
6360 : : vop1,
6361 : 10 : TYPE_SIZE (TREE_TYPE (new_temp)),
6362 : : bitsize_zero_node));
6363 : 10 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6364 : 10 : vop1 = new_temp;
6365 : 10 : }
6366 : : else
6367 : 12594 : vop1 = vec_oprnds1[i];
6368 : 12604 : gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
6369 : 12604 : new_temp = make_ssa_name (vec_dest, new_stmt);
6370 : 12604 : gimple_assign_set_lhs (new_stmt, new_temp);
6371 : 12604 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6372 : 12604 : slp_node->push_vec_def (new_stmt);
6373 : : }
6374 : :
6375 : 7508 : vec_oprnds0.release ();
6376 : 7508 : vec_oprnds1.release ();
6377 : :
6378 : 7508 : return true;
6379 : : }
6380 : :
6381 : : /* Function vectorizable_operation.
6382 : :
6383 : : Check if STMT_INFO performs a binary, unary or ternary operation that can
6384 : : be vectorized.
6385 : : If COST_VEC is passed, calculate costs but don't change anything,
6386 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
6387 : : it, and insert it at GSI.
6388 : : Return true if STMT_INFO is vectorizable in this way. */
6389 : :
6390 : : static bool
6391 : 2335869 : vectorizable_operation (vec_info *vinfo,
6392 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6393 : : slp_tree slp_node,
6394 : : stmt_vector_for_cost *cost_vec)
6395 : : {
6396 : 2335869 : tree vec_dest;
6397 : 2335869 : tree scalar_dest;
6398 : 2335869 : tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
6399 : 2335869 : tree vectype;
6400 : 2335869 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6401 : 2335869 : enum tree_code code, orig_code;
6402 : 2335869 : machine_mode vec_mode;
6403 : 2335869 : tree new_temp;
6404 : 2335869 : int op_type;
6405 : 2335869 : optab optab;
6406 : 2335869 : bool target_support_p;
6407 : 2335869 : enum vect_def_type dt[3]
6408 : : = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
6409 : 2335869 : poly_uint64 nunits_in;
6410 : 2335869 : poly_uint64 nunits_out;
6411 : 2335869 : tree vectype_out;
6412 : 2335869 : int i;
6413 : 2335869 : vec<tree> vec_oprnds0 = vNULL;
6414 : 2335869 : vec<tree> vec_oprnds1 = vNULL;
6415 : 2335869 : vec<tree> vec_oprnds2 = vNULL;
6416 : 2335869 : tree vop0, vop1, vop2;
6417 : 2335869 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6418 : :
6419 : 2335869 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6420 : : return false;
6421 : :
6422 : 2335869 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6423 : 175983 : && cost_vec)
6424 : : return false;
6425 : :
6426 : : /* Is STMT a vectorizable binary/unary operation? */
6427 : 3912488 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6428 : 2086737 : if (!stmt)
6429 : : return false;
6430 : :
6431 : : /* Loads and stores are handled in vectorizable_{load,store}. */
6432 : 2086737 : if (STMT_VINFO_DATA_REF (stmt_info))
6433 : : return false;
6434 : :
6435 : 847166 : orig_code = code = gimple_assign_rhs_code (stmt);
6436 : :
6437 : : /* Shifts are handled in vectorizable_shift. */
6438 : 847166 : if (code == LSHIFT_EXPR
6439 : : || code == RSHIFT_EXPR
6440 : : || code == LROTATE_EXPR
6441 : 847166 : || code == RROTATE_EXPR)
6442 : : return false;
6443 : :
6444 : : /* Comparisons are handled in vectorizable_comparison. */
6445 : 800371 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6446 : : return false;
6447 : :
6448 : : /* Conditions are handled in vectorizable_condition. */
6449 : 626379 : if (code == COND_EXPR)
6450 : : return false;
6451 : :
6452 : : /* For pointer addition and subtraction, we should use the normal
6453 : : plus and minus for the vector operation. */
6454 : 606399 : if (code == POINTER_PLUS_EXPR)
6455 : : code = PLUS_EXPR;
6456 : 592224 : if (code == POINTER_DIFF_EXPR)
6457 : 1016 : code = MINUS_EXPR;
6458 : :
6459 : : /* Support only unary or binary operations. */
6460 : 606399 : op_type = TREE_CODE_LENGTH (code);
6461 : 606399 : if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
6462 : : {
6463 : 4 : if (dump_enabled_p ())
6464 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6465 : : "num. args = %d (not unary/binary/ternary op).\n",
6466 : : op_type);
6467 : 4 : return false;
6468 : : }
6469 : :
6470 : 606395 : scalar_dest = gimple_assign_lhs (stmt);
6471 : 606395 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6472 : :
6473 : : /* Most operations cannot handle bit-precision types without extra
6474 : : truncations. */
6475 : 606395 : bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
6476 : 599067 : if (!mask_op_p
6477 : 599067 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
6478 : : /* Exception are bitwise binary operations. */
6479 : : && code != BIT_IOR_EXPR
6480 : 1527 : && code != BIT_XOR_EXPR
6481 : 1178 : && code != BIT_AND_EXPR)
6482 : : {
6483 : 952 : if (dump_enabled_p ())
6484 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6485 : : "bit-precision arithmetic not supported.\n");
6486 : 952 : return false;
6487 : : }
6488 : :
6489 : 605443 : slp_tree slp_op0;
6490 : 605443 : if (!vect_is_simple_use (vinfo, slp_node,
6491 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6492 : : {
6493 : 0 : if (dump_enabled_p ())
6494 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6495 : : "use not simple.\n");
6496 : 0 : return false;
6497 : : }
6498 : 605443 : bool is_invariant = (dt[0] == vect_external_def
6499 : 605443 : || dt[0] == vect_constant_def);
6500 : : /* If op0 is an external or constant def, infer the vector type
6501 : : from the scalar type. */
6502 : 605443 : if (!vectype)
6503 : : {
6504 : : /* For boolean type we cannot determine vectype by
6505 : : invariant value (don't know whether it is a vector
6506 : : of booleans or vector of integers). We use output
6507 : : vectype because operations on boolean don't change
6508 : : type. */
6509 : 69813 : if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
6510 : : {
6511 : 1023 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
6512 : : {
6513 : 235 : if (dump_enabled_p ())
6514 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6515 : : "not supported operation on bool value.\n");
6516 : 235 : return false;
6517 : : }
6518 : 788 : vectype = vectype_out;
6519 : : }
6520 : : else
6521 : 68790 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
6522 : : slp_node);
6523 : : }
6524 : 605208 : if (!cost_vec)
6525 : 111720 : gcc_assert (vectype);
6526 : 605208 : if (!vectype)
6527 : : {
6528 : 288 : if (dump_enabled_p ())
6529 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6530 : : "no vectype for scalar type %T\n",
6531 : 0 : TREE_TYPE (op0));
6532 : :
6533 : 288 : return false;
6534 : : }
6535 : :
6536 : 604920 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6537 : 604920 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6538 : 604920 : if (maybe_ne (nunits_out, nunits_in)
6539 : 604920 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
6540 : 10817 : return false;
6541 : :
6542 : 594103 : tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
6543 : 594103 : slp_tree slp_op1 = NULL, slp_op2 = NULL;
6544 : 594103 : if (op_type == binary_op || op_type == ternary_op)
6545 : : {
6546 : 526995 : if (!vect_is_simple_use (vinfo, slp_node,
6547 : : 1, &op1, &slp_op1, &dt[1], &vectype2))
6548 : : {
6549 : 0 : if (dump_enabled_p ())
6550 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6551 : : "use not simple.\n");
6552 : 0 : return false;
6553 : : }
6554 : 526995 : is_invariant &= (dt[1] == vect_external_def
6555 : 526995 : || dt[1] == vect_constant_def);
6556 : 526995 : if (vectype2
6557 : 879896 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
6558 : 352901 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6559 : 352901 : TREE_TYPE (vectype2))))
6560 : 4 : return false;
6561 : : }
6562 : 594099 : if (op_type == ternary_op)
6563 : : {
6564 : 0 : if (!vect_is_simple_use (vinfo, slp_node,
6565 : : 2, &op2, &slp_op2, &dt[2], &vectype3))
6566 : : {
6567 : 0 : if (dump_enabled_p ())
6568 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6569 : : "use not simple.\n");
6570 : 0 : return false;
6571 : : }
6572 : 0 : is_invariant &= (dt[2] == vect_external_def
6573 : 0 : || dt[2] == vect_constant_def);
6574 : 0 : if (vectype3
6575 : 0 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
6576 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6577 : 0 : TREE_TYPE (vectype3))))
6578 : 0 : return false;
6579 : : }
6580 : :
6581 : : /* Multiple types in SLP are handled by creating the appropriate number of
6582 : : vectorized stmts for each SLP node. */
6583 : 594099 : auto vec_num = vect_get_num_copies (vinfo, slp_node);
6584 : :
6585 : : /* Reject attempts to combine mask types with nonmask types, e.g. if
6586 : : we have an AND between a (nonmask) boolean loaded from memory and
6587 : : a (mask) boolean result of a comparison.
6588 : :
6589 : : TODO: We could easily fix these cases up using pattern statements. */
6590 : 594099 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
6591 : 942425 : || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
6592 : 1188198 : || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
6593 : : {
6594 : 0 : if (dump_enabled_p ())
6595 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6596 : : "mixed mask and nonmask vector types\n");
6597 : 0 : return false;
6598 : : }
6599 : :
6600 : : /* Supportable by target? */
6601 : :
6602 : 594099 : vec_mode = TYPE_MODE (vectype);
6603 : 594099 : optab = optab_for_tree_code (code, vectype, optab_default);
6604 : 594099 : if (!optab)
6605 : : {
6606 : 55813 : if (dump_enabled_p ())
6607 : 5781 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6608 : : "no optab.\n");
6609 : 55813 : return false;
6610 : : }
6611 : 538286 : target_support_p = can_implement_p (optab, vec_mode);
6612 : :
6613 : 538286 : bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
6614 : 538286 : if (!target_support_p || using_emulated_vectors_p)
6615 : : {
6616 : 28775 : if (dump_enabled_p ())
6617 : 1086 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6618 : : "op not supported by target.\n");
6619 : : /* When vec_mode is not a vector mode and we verified ops we
6620 : : do not have to lower like AND are natively supported let
6621 : : those through even when the mode isn't word_mode. For
6622 : : ops we have to lower the lowering code assumes we are
6623 : : dealing with word_mode. */
6624 : 57550 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
6625 : 28681 : || !GET_MODE_SIZE (vec_mode).is_constant ()
6626 : 28681 : || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6627 : 23392 : || !target_support_p)
6628 : 61180 : && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
6629 : : /* Check only during analysis. */
6630 : 39439 : || (cost_vec && !vect_can_vectorize_without_simd_p (code)))
6631 : : {
6632 : 28009 : if (dump_enabled_p ())
6633 : 1086 : dump_printf (MSG_NOTE, "using word mode not possible.\n");
6634 : 28009 : return false;
6635 : : }
6636 : 766 : if (dump_enabled_p ())
6637 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6638 : : "proceeding using word mode.\n");
6639 : : using_emulated_vectors_p = true;
6640 : : }
6641 : :
6642 : 510277 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
6643 : 510277 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
6644 : 331436 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
6645 : 510277 : internal_fn cond_fn = get_conditional_internal_fn (code);
6646 : 510277 : internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
6647 : :
6648 : : /* If operating on inactive elements could generate spurious traps,
6649 : : we need to restrict the operation to active lanes. Note that this
6650 : : specifically doesn't apply to unhoisted invariants, since they
6651 : : operate on the same value for every lane.
6652 : :
6653 : : Similarly, if this operation is part of a reduction, a fully-masked
6654 : : loop should only change the active lanes of the reduction chain,
6655 : : keeping the inactive lanes as-is. */
6656 : 482315 : bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
6657 : 938529 : || reduc_idx >= 0);
6658 : :
6659 : 510277 : if (cost_vec) /* transformation not required. */
6660 : : {
6661 : 398557 : if (loop_vinfo
6662 : 231155 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
6663 : 75 : && mask_out_inactive)
6664 : : {
6665 : 42 : if (cond_len_fn != IFN_LAST
6666 : 42 : && direct_internal_fn_supported_p (cond_len_fn, vectype,
6667 : : OPTIMIZE_FOR_SPEED))
6668 : 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype,
6669 : : 1);
6670 : 42 : else if (cond_fn != IFN_LAST
6671 : 42 : && direct_internal_fn_supported_p (cond_fn, vectype,
6672 : : OPTIMIZE_FOR_SPEED))
6673 : 15 : vect_record_loop_mask (loop_vinfo, masks, vec_num,
6674 : : vectype, NULL);
6675 : : else
6676 : : {
6677 : 27 : if (dump_enabled_p ())
6678 : 27 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6679 : : "can't use a fully-masked loop because no"
6680 : : " conditional operation is available.\n");
6681 : 27 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
6682 : : }
6683 : : }
6684 : :
6685 : : /* Put types on constant and invariant SLP children. */
6686 : 398557 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6687 : 398493 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
6688 : 796955 : || !vect_maybe_update_slp_op_vectype (slp_op2, vectype))
6689 : : {
6690 : 159 : if (dump_enabled_p ())
6691 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6692 : : "incompatible vector types for invariants\n");
6693 : 159 : return false;
6694 : : }
6695 : :
6696 : 398398 : SLP_TREE_TYPE (slp_node) = op_vec_info_type;
6697 : 398398 : DUMP_VECT_SCOPE ("vectorizable_operation");
6698 : 398398 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6699 : 398398 : if (using_emulated_vectors_p)
6700 : : {
6701 : : /* The above vect_model_simple_cost call handles constants
6702 : : in the prologue and (mis-)costs one of the stmts as
6703 : : vector stmt. See below for the actual lowering that will
6704 : : be applied. */
6705 : 764 : unsigned n = vect_get_num_copies (vinfo, slp_node);
6706 : 764 : switch (code)
6707 : : {
6708 : 267 : case PLUS_EXPR:
6709 : 267 : n *= 5;
6710 : 267 : break;
6711 : 462 : case MINUS_EXPR:
6712 : 462 : n *= 6;
6713 : 462 : break;
6714 : 0 : case NEGATE_EXPR:
6715 : 0 : n *= 4;
6716 : 0 : break;
6717 : : default:
6718 : : /* Bit operations do not have extra cost and are accounted
6719 : : as vector stmt by vect_model_simple_cost. */
6720 : : n = 0;
6721 : : break;
6722 : : }
6723 : 729 : if (n != 0)
6724 : : {
6725 : : /* We also need to materialize two large constants. */
6726 : 729 : record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
6727 : : 0, vect_prologue);
6728 : 729 : record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
6729 : : 0, vect_body);
6730 : : }
6731 : : }
6732 : 398398 : return true;
6733 : : }
6734 : :
6735 : : /* Transform. */
6736 : :
6737 : 111720 : if (dump_enabled_p ())
6738 : 16468 : dump_printf_loc (MSG_NOTE, vect_location,
6739 : : "transform binary/unary operation.\n");
6740 : :
6741 : 111720 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
6742 : 100281 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
6743 : :
6744 : : /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
6745 : : vectors with unsigned elements, but the result is signed. So, we
6746 : : need to compute the MINUS_EXPR into vectype temporary and
6747 : : VIEW_CONVERT_EXPR it into the final vectype_out result. */
6748 : 111720 : tree vec_cvt_dest = NULL_TREE;
6749 : 111720 : if (orig_code == POINTER_DIFF_EXPR)
6750 : : {
6751 : 131 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6752 : 131 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6753 : : }
6754 : : /* For reduction operations with undefined overflow behavior make sure to
6755 : : pun them to unsigned since we change the order of evaluation.
6756 : : ??? Avoid for in-order reductions? */
6757 : 111589 : else if (arith_code_with_undefined_signed_overflow (orig_code)
6758 : 95717 : && ANY_INTEGRAL_TYPE_P (vectype)
6759 : 49864 : && TYPE_OVERFLOW_UNDEFINED (vectype)
6760 : 138586 : && SLP_TREE_REDUC_IDX (slp_node) != -1)
6761 : : {
6762 : 2319 : gcc_assert (orig_code == PLUS_EXPR || orig_code == MINUS_EXPR
6763 : : || orig_code == MULT_EXPR || orig_code == POINTER_PLUS_EXPR);
6764 : 2319 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6765 : 2319 : vectype = unsigned_type_for (vectype);
6766 : 2319 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6767 : : }
6768 : : /* Handle def. */
6769 : : else
6770 : 109270 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
6771 : :
6772 : 111720 : vect_get_vec_defs (vinfo, slp_node,
6773 : : op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
6774 : : /* Arguments are ready. Create the new vector stmt. */
6775 : 251484 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6776 : : {
6777 : 139764 : gimple *new_stmt = NULL;
6778 : 279528 : vop1 = ((op_type == binary_op || op_type == ternary_op)
6779 : 139764 : ? vec_oprnds1[i] : NULL_TREE);
6780 : 139764 : vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
6781 : :
6782 : 139764 : if (vec_cvt_dest
6783 : 139764 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop0)))
6784 : : {
6785 : 2708 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop0);
6786 : 2708 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6787 : : new_temp);
6788 : 2708 : new_temp = make_ssa_name (vec_dest, new_stmt);
6789 : 2708 : gimple_assign_set_lhs (new_stmt, new_temp);
6790 : 2708 : vect_finish_stmt_generation (vinfo, stmt_info,
6791 : : new_stmt, gsi);
6792 : 2708 : vop0 = new_temp;
6793 : : }
6794 : 139764 : if (vop1
6795 : 137277 : && vec_cvt_dest
6796 : 142618 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop1)))
6797 : : {
6798 : 2708 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop1);
6799 : 2708 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6800 : : new_temp);
6801 : 2708 : new_temp = make_ssa_name (vec_dest, new_stmt);
6802 : 2708 : gimple_assign_set_lhs (new_stmt, new_temp);
6803 : 2708 : vect_finish_stmt_generation (vinfo, stmt_info,
6804 : : new_stmt, gsi);
6805 : 2708 : vop1 = new_temp;
6806 : : }
6807 : 139764 : if (vop2
6808 : 0 : && vec_cvt_dest
6809 : 139764 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop2)))
6810 : : {
6811 : 0 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop2);
6812 : 0 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6813 : : new_temp);
6814 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6815 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6816 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
6817 : : new_stmt, gsi);
6818 : 0 : vop2 = new_temp;
6819 : : }
6820 : :
6821 : 139764 : if (using_emulated_vectors_p)
6822 : : {
6823 : : /* Lower the operation. This follows vector lowering. */
6824 : 2 : tree word_type = build_nonstandard_integer_type
6825 : 2 : (GET_MODE_BITSIZE (vec_mode).to_constant (), 1);
6826 : 2 : tree wvop0 = make_ssa_name (word_type);
6827 : 2 : new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
6828 : : build1 (VIEW_CONVERT_EXPR,
6829 : : word_type, vop0));
6830 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6831 : 2 : tree wvop1 = NULL_TREE;
6832 : 2 : if (vop1)
6833 : : {
6834 : 2 : wvop1 = make_ssa_name (word_type);
6835 : 2 : new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
6836 : : build1 (VIEW_CONVERT_EXPR,
6837 : : word_type, vop1));
6838 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6839 : : }
6840 : :
6841 : 2 : tree result_low;
6842 : 2 : if (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6843 : : {
6844 : 1 : unsigned int width = vector_element_bits (vectype);
6845 : 1 : tree inner_type = TREE_TYPE (vectype);
6846 : 1 : HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
6847 : 1 : tree low_bits
6848 : 1 : = build_replicated_int_cst (word_type, width, max >> 1);
6849 : 1 : tree high_bits
6850 : 2 : = build_replicated_int_cst (word_type,
6851 : 1 : width, max & ~(max >> 1));
6852 : 1 : tree signs;
6853 : 1 : if (code == PLUS_EXPR || code == MINUS_EXPR)
6854 : : {
6855 : 1 : signs = make_ssa_name (word_type);
6856 : 1 : new_stmt = gimple_build_assign (signs,
6857 : : BIT_XOR_EXPR, wvop0, wvop1);
6858 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6859 : 1 : tree b_low = make_ssa_name (word_type);
6860 : 1 : new_stmt = gimple_build_assign (b_low, BIT_AND_EXPR,
6861 : : wvop1, low_bits);
6862 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6863 : 1 : tree a_low = make_ssa_name (word_type);
6864 : 1 : if (code == PLUS_EXPR)
6865 : 1 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
6866 : : wvop0, low_bits);
6867 : : else
6868 : 0 : new_stmt = gimple_build_assign (a_low, BIT_IOR_EXPR,
6869 : : wvop0, high_bits);
6870 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6871 : 1 : if (code == MINUS_EXPR)
6872 : : {
6873 : 0 : new_stmt = gimple_build_assign (NULL_TREE,
6874 : : BIT_NOT_EXPR, signs);
6875 : 0 : signs = make_ssa_name (word_type);
6876 : 0 : gimple_assign_set_lhs (new_stmt, signs);
6877 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
6878 : : new_stmt, gsi);
6879 : : }
6880 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
6881 : : signs, high_bits);
6882 : 1 : signs = make_ssa_name (word_type);
6883 : 1 : gimple_assign_set_lhs (new_stmt, signs);
6884 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6885 : 1 : result_low = make_ssa_name (word_type);
6886 : 1 : new_stmt = gimple_build_assign (result_low, code,
6887 : : a_low, b_low);
6888 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6889 : : }
6890 : : else /* if (code == NEGATE_EXPR) */
6891 : : {
6892 : 0 : tree a_low = make_ssa_name (word_type);
6893 : 0 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
6894 : : wvop0, low_bits);
6895 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6896 : 0 : signs = make_ssa_name (word_type);
6897 : 0 : new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
6898 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6899 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
6900 : : signs, high_bits);
6901 : 0 : signs = make_ssa_name (word_type);
6902 : 0 : gimple_assign_set_lhs (new_stmt, signs);
6903 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6904 : 0 : result_low = make_ssa_name (word_type);
6905 : 0 : new_stmt = gimple_build_assign (result_low,
6906 : : MINUS_EXPR, high_bits, a_low);
6907 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6908 : : }
6909 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR,
6910 : : result_low, signs);
6911 : 1 : result_low = make_ssa_name (word_type);
6912 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
6913 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6914 : : }
6915 : : else
6916 : : {
6917 : 1 : new_stmt = gimple_build_assign (NULL_TREE, code, wvop0, wvop1);
6918 : 1 : result_low = make_ssa_name (word_type);
6919 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
6920 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6921 : :
6922 : : }
6923 : 2 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
6924 : : build1 (VIEW_CONVERT_EXPR,
6925 : : vectype, result_low));
6926 : 2 : new_temp = make_ssa_name (vectype);
6927 : 2 : gimple_assign_set_lhs (new_stmt, new_temp);
6928 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6929 : : }
6930 : 139762 : else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
6931 : : {
6932 : 16 : tree mask;
6933 : 16 : if (masked_loop_p)
6934 : 16 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
6935 : : vec_num, vectype, i);
6936 : : else
6937 : : /* Dummy mask. */
6938 : 0 : mask = build_minus_one_cst (truth_type_for (vectype));
6939 : 16 : auto_vec<tree> vops (6);
6940 : 16 : vops.quick_push (mask);
6941 : 16 : vops.quick_push (vop0);
6942 : 16 : if (vop1)
6943 : 16 : vops.quick_push (vop1);
6944 : 16 : if (vop2)
6945 : 0 : vops.quick_push (vop2);
6946 : 16 : if (reduc_idx >= 0)
6947 : : {
6948 : : /* Perform the operation on active elements only and take
6949 : : inactive elements from the reduction chain input. */
6950 : 8 : gcc_assert (!vop2);
6951 : 8 : vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
6952 : : }
6953 : : else
6954 : : {
6955 : 8 : auto else_value = targetm.preferred_else_value
6956 : 8 : (cond_fn, vectype, vops.length () - 1, &vops[1]);
6957 : 8 : vops.quick_push (else_value);
6958 : : }
6959 : 16 : if (len_loop_p)
6960 : : {
6961 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
6962 : 0 : vec_num, vectype, i, 1);
6963 : 0 : signed char biasval
6964 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
6965 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
6966 : 0 : vops.quick_push (len);
6967 : 0 : vops.quick_push (bias);
6968 : : }
6969 : 16 : gcall *call
6970 : 16 : = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
6971 : : : cond_len_fn,
6972 : : vops);
6973 : 16 : new_temp = make_ssa_name (vec_dest, call);
6974 : 16 : gimple_call_set_lhs (call, new_temp);
6975 : 16 : gimple_call_set_nothrow (call, true);
6976 : 16 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
6977 : 16 : new_stmt = call;
6978 : 16 : }
6979 : : else
6980 : : {
6981 : 139746 : tree mask = NULL_TREE;
6982 : : /* When combining two masks check if either of them is elsewhere
6983 : : combined with a loop mask, if that's the case we can mark that the
6984 : : new combined mask doesn't need to be combined with a loop mask. */
6985 : 139746 : if (masked_loop_p
6986 : 139746 : && code == BIT_AND_EXPR
6987 : 139746 : && VECTOR_BOOLEAN_TYPE_P (vectype))
6988 : : {
6989 : 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op0, vec_num }))
6990 : : {
6991 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
6992 : : vec_num, vectype, i);
6993 : :
6994 : 0 : vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
6995 : : vop0, gsi);
6996 : : }
6997 : :
6998 : 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op1, vec_num }))
6999 : : {
7000 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7001 : : vec_num, vectype, i);
7002 : :
7003 : 0 : vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7004 : : vop1, gsi);
7005 : : }
7006 : : }
7007 : :
7008 : 139746 : new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
7009 : 139746 : new_temp = make_ssa_name (vec_dest, new_stmt);
7010 : 139746 : gimple_assign_set_lhs (new_stmt, new_temp);
7011 : 139746 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7012 : 139746 : if (using_emulated_vectors_p)
7013 : : suppress_warning (new_stmt, OPT_Wvector_operation_performance);
7014 : :
7015 : : /* Enter the combined value into the vector cond hash so we don't
7016 : : AND it with a loop mask again. */
7017 : 139746 : if (mask)
7018 : 0 : loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
7019 : : }
7020 : :
7021 : 139764 : if (vec_cvt_dest)
7022 : : {
7023 : 2854 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
7024 : 2854 : new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
7025 : : new_temp);
7026 : 2854 : new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
7027 : 2854 : gimple_assign_set_lhs (new_stmt, new_temp);
7028 : 2854 : vect_finish_stmt_generation (vinfo, stmt_info,
7029 : : new_stmt, gsi);
7030 : : }
7031 : :
7032 : 139764 : slp_node->push_vec_def (new_stmt);
7033 : : }
7034 : :
7035 : 111720 : vec_oprnds0.release ();
7036 : 111720 : vec_oprnds1.release ();
7037 : 111720 : vec_oprnds2.release ();
7038 : :
7039 : 111720 : return true;
7040 : : }
7041 : :
7042 : : /* A helper function to ensure data reference DR_INFO's base alignment. */
7043 : :
7044 : : static void
7045 : 1858453 : ensure_base_align (dr_vec_info *dr_info)
7046 : : {
7047 : : /* Alignment is only analyzed for the first element of a DR group,
7048 : : use that to look at base alignment we need to enforce. */
7049 : 1858453 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
7050 : 1423897 : dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
7051 : :
7052 : 1858453 : gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
7053 : :
7054 : 1858453 : if (dr_info->base_misaligned)
7055 : : {
7056 : 169333 : tree base_decl = dr_info->base_decl;
7057 : :
7058 : : // We should only be able to increase the alignment of a base object if
7059 : : // we know what its new alignment should be at compile time.
7060 : 169333 : unsigned HOST_WIDE_INT align_base_to =
7061 : 169333 : DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
7062 : :
7063 : 169333 : if (decl_in_symtab_p (base_decl))
7064 : 4649 : symtab_node::get (base_decl)->increase_alignment (align_base_to);
7065 : 164684 : else if (DECL_ALIGN (base_decl) < align_base_to)
7066 : : {
7067 : 131903 : SET_DECL_ALIGN (base_decl, align_base_to);
7068 : 131903 : DECL_USER_ALIGN (base_decl) = 1;
7069 : : }
7070 : 169333 : dr_info->base_misaligned = false;
7071 : : }
7072 : 1858453 : }
7073 : :
7074 : :
7075 : : /* Function get_group_alias_ptr_type.
7076 : :
7077 : : Return the alias type for the group starting at FIRST_STMT_INFO. */
7078 : :
7079 : : static tree
7080 : 1590882 : get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
7081 : : {
7082 : 1590882 : struct data_reference *first_dr, *next_dr;
7083 : :
7084 : 1590882 : first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
7085 : 1590882 : stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
7086 : 3846216 : while (next_stmt_info)
7087 : : {
7088 : 2402939 : next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
7089 : 4805878 : if (get_alias_set (DR_REF (first_dr))
7090 : 2402939 : != get_alias_set (DR_REF (next_dr)))
7091 : : {
7092 : 147605 : if (dump_enabled_p ())
7093 : 30 : dump_printf_loc (MSG_NOTE, vect_location,
7094 : : "conflicting alias set types.\n");
7095 : 147605 : return ptr_type_node;
7096 : : }
7097 : 2255334 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7098 : : }
7099 : 1443277 : return reference_alias_ptr_type (DR_REF (first_dr));
7100 : : }
7101 : :
7102 : :
7103 : : /* Function scan_operand_equal_p.
7104 : :
7105 : : Helper function for check_scan_store. Compare two references
7106 : : with .GOMP_SIMD_LANE bases. */
7107 : :
7108 : : static bool
7109 : 1284 : scan_operand_equal_p (tree ref1, tree ref2)
7110 : : {
7111 : 1284 : tree ref[2] = { ref1, ref2 };
7112 : 1284 : poly_int64 bitsize[2], bitpos[2];
7113 : : tree offset[2], base[2];
7114 : 3852 : for (int i = 0; i < 2; ++i)
7115 : : {
7116 : 2568 : machine_mode mode;
7117 : 2568 : int unsignedp, reversep, volatilep = 0;
7118 : 2568 : base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
7119 : : &offset[i], &mode, &unsignedp,
7120 : : &reversep, &volatilep);
7121 : 2568 : if (reversep || volatilep || maybe_ne (bitpos[i], 0))
7122 : 0 : return false;
7123 : 2568 : if (TREE_CODE (base[i]) == MEM_REF
7124 : 42 : && offset[i] == NULL_TREE
7125 : 2610 : && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
7126 : : {
7127 : 42 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
7128 : 42 : if (is_gimple_assign (def_stmt)
7129 : 42 : && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
7130 : 42 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
7131 : 84 : && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
7132 : : {
7133 : 42 : if (maybe_ne (mem_ref_offset (base[i]), 0))
7134 : : return false;
7135 : 42 : base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
7136 : 42 : offset[i] = gimple_assign_rhs2 (def_stmt);
7137 : : }
7138 : : }
7139 : : }
7140 : :
7141 : 1284 : if (!operand_equal_p (base[0], base[1], 0))
7142 : : return false;
7143 : 934 : if (maybe_ne (bitsize[0], bitsize[1]))
7144 : : return false;
7145 : 934 : if (offset[0] != offset[1])
7146 : : {
7147 : 916 : if (!offset[0] || !offset[1])
7148 : : return false;
7149 : 916 : if (!operand_equal_p (offset[0], offset[1], 0))
7150 : : {
7151 : : tree step[2];
7152 : 0 : for (int i = 0; i < 2; ++i)
7153 : : {
7154 : 0 : step[i] = integer_one_node;
7155 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7156 : : {
7157 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7158 : 0 : if (is_gimple_assign (def_stmt)
7159 : 0 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
7160 : 0 : && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
7161 : : == INTEGER_CST))
7162 : : {
7163 : 0 : step[i] = gimple_assign_rhs2 (def_stmt);
7164 : 0 : offset[i] = gimple_assign_rhs1 (def_stmt);
7165 : : }
7166 : : }
7167 : 0 : else if (TREE_CODE (offset[i]) == MULT_EXPR)
7168 : : {
7169 : 0 : step[i] = TREE_OPERAND (offset[i], 1);
7170 : 0 : offset[i] = TREE_OPERAND (offset[i], 0);
7171 : : }
7172 : 0 : tree rhs1 = NULL_TREE;
7173 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7174 : : {
7175 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7176 : 0 : if (gimple_assign_cast_p (def_stmt))
7177 : 0 : rhs1 = gimple_assign_rhs1 (def_stmt);
7178 : : }
7179 : 0 : else if (CONVERT_EXPR_P (offset[i]))
7180 : 0 : rhs1 = TREE_OPERAND (offset[i], 0);
7181 : 0 : if (rhs1
7182 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
7183 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
7184 : 0 : && (TYPE_PRECISION (TREE_TYPE (offset[i]))
7185 : 0 : >= TYPE_PRECISION (TREE_TYPE (rhs1))))
7186 : 0 : offset[i] = rhs1;
7187 : : }
7188 : 0 : if (!operand_equal_p (offset[0], offset[1], 0)
7189 : 0 : || !operand_equal_p (step[0], step[1], 0))
7190 : 0 : return false;
7191 : : }
7192 : : }
7193 : : return true;
7194 : : }
7195 : :
7196 : :
7197 : : enum scan_store_kind {
7198 : : /* Normal permutation. */
7199 : : scan_store_kind_perm,
7200 : :
7201 : : /* Whole vector left shift permutation with zero init. */
7202 : : scan_store_kind_lshift_zero,
7203 : :
7204 : : /* Whole vector left shift permutation and VEC_COND_EXPR. */
7205 : : scan_store_kind_lshift_cond
7206 : : };
7207 : :
7208 : : /* Function check_scan_store.
7209 : :
7210 : : Verify if we can perform the needed permutations or whole vector shifts.
7211 : : Return -1 on failure, otherwise exact log2 of vectype's nunits.
7212 : : USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
7213 : : to do at each step. */
7214 : :
7215 : : static int
7216 : 1024 : scan_store_can_perm_p (tree vectype, tree init,
7217 : : vec<enum scan_store_kind> *use_whole_vector = NULL)
7218 : : {
7219 : 1024 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7220 : 1024 : unsigned HOST_WIDE_INT nunits;
7221 : 1024 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7222 : : return -1;
7223 : 1024 : int units_log2 = exact_log2 (nunits);
7224 : 1024 : if (units_log2 <= 0)
7225 : : return -1;
7226 : :
7227 : : int i;
7228 : : enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
7229 : 4784 : for (i = 0; i <= units_log2; ++i)
7230 : : {
7231 : 3760 : unsigned HOST_WIDE_INT j, k;
7232 : 3760 : enum scan_store_kind kind = scan_store_kind_perm;
7233 : 3760 : vec_perm_builder sel (nunits, nunits, 1);
7234 : 3760 : sel.quick_grow (nunits);
7235 : 3760 : if (i == units_log2)
7236 : : {
7237 : 9728 : for (j = 0; j < nunits; ++j)
7238 : 8704 : sel[j] = nunits - 1;
7239 : : }
7240 : : else
7241 : : {
7242 : 10416 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7243 : 7680 : sel[j] = j;
7244 : 26416 : for (k = 0; j < nunits; ++j, ++k)
7245 : 23680 : sel[j] = nunits + k;
7246 : : }
7247 : 6496 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7248 : 3760 : if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
7249 : : {
7250 : 0 : if (i == units_log2)
7251 : : return -1;
7252 : :
7253 : 0 : if (whole_vector_shift_kind == scan_store_kind_perm)
7254 : : {
7255 : 0 : if (!can_implement_p (vec_shl_optab, vec_mode))
7256 : : return -1;
7257 : 0 : whole_vector_shift_kind = scan_store_kind_lshift_zero;
7258 : : /* Whole vector shifts shift in zeros, so if init is all zero
7259 : : constant, there is no need to do anything further. */
7260 : 0 : if ((TREE_CODE (init) != INTEGER_CST
7261 : 0 : && TREE_CODE (init) != REAL_CST)
7262 : 0 : || !initializer_zerop (init))
7263 : : {
7264 : 0 : tree masktype = truth_type_for (vectype);
7265 : 0 : if (!expand_vec_cond_expr_p (vectype, masktype))
7266 : : return -1;
7267 : : whole_vector_shift_kind = scan_store_kind_lshift_cond;
7268 : : }
7269 : : }
7270 : 0 : kind = whole_vector_shift_kind;
7271 : : }
7272 : 3760 : if (use_whole_vector)
7273 : : {
7274 : 1880 : if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
7275 : 0 : use_whole_vector->safe_grow_cleared (i, true);
7276 : 5640 : if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
7277 : 0 : use_whole_vector->safe_push (kind);
7278 : : }
7279 : 3760 : }
7280 : :
7281 : : return units_log2;
7282 : : }
7283 : :
7284 : :
7285 : : /* Function check_scan_store.
7286 : :
7287 : : Check magic stores for #pragma omp scan {in,ex}clusive reductions. */
7288 : :
7289 : : static bool
7290 : 1076 : check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
7291 : : enum vect_def_type rhs_dt, slp_tree slp_node,
7292 : : slp_tree mask_node,
7293 : : vect_memory_access_type memory_access_type)
7294 : : {
7295 : 1076 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7296 : 1076 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7297 : 1076 : tree ref_type;
7298 : :
7299 : 1076 : gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
7300 : 1076 : if (SLP_TREE_LANES (slp_node) > 1
7301 : 1076 : || mask_node
7302 : 1076 : || memory_access_type != VMAT_CONTIGUOUS
7303 : 1076 : || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
7304 : 1076 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
7305 : 1076 : || loop_vinfo == NULL
7306 : 1076 : || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7307 : 1076 : || LOOP_VINFO_EPILOGUE_P (loop_vinfo)
7308 : 1076 : || STMT_VINFO_GROUPED_ACCESS (stmt_info)
7309 : 1076 : || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
7310 : 1076 : || !integer_zerop (DR_INIT (dr_info->dr))
7311 : 1076 : || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
7312 : 2152 : || !alias_sets_conflict_p (get_alias_set (vectype),
7313 : 1076 : get_alias_set (TREE_TYPE (ref_type))))
7314 : : {
7315 : 0 : if (dump_enabled_p ())
7316 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7317 : : "unsupported OpenMP scan store.\n");
7318 : 0 : return false;
7319 : : }
7320 : :
7321 : : /* We need to pattern match code built by OpenMP lowering and simplified
7322 : : by following optimizations into something we can handle.
7323 : : #pragma omp simd reduction(inscan,+:r)
7324 : : for (...)
7325 : : {
7326 : : r += something ();
7327 : : #pragma omp scan inclusive (r)
7328 : : use (r);
7329 : : }
7330 : : shall have body with:
7331 : : // Initialization for input phase, store the reduction initializer:
7332 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7333 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7334 : : D.2042[_21] = 0;
7335 : : // Actual input phase:
7336 : : ...
7337 : : r.0_5 = D.2042[_20];
7338 : : _6 = _4 + r.0_5;
7339 : : D.2042[_20] = _6;
7340 : : // Initialization for scan phase:
7341 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
7342 : : _26 = D.2043[_25];
7343 : : _27 = D.2042[_25];
7344 : : _28 = _26 + _27;
7345 : : D.2043[_25] = _28;
7346 : : D.2042[_25] = _28;
7347 : : // Actual scan phase:
7348 : : ...
7349 : : r.1_8 = D.2042[_20];
7350 : : ...
7351 : : The "omp simd array" variable D.2042 holds the privatized copy used
7352 : : inside of the loop and D.2043 is another one that holds copies of
7353 : : the current original list item. The separate GOMP_SIMD_LANE ifn
7354 : : kinds are there in order to allow optimizing the initializer store
7355 : : and combiner sequence, e.g. if it is originally some C++ish user
7356 : : defined reduction, but allow the vectorizer to pattern recognize it
7357 : : and turn into the appropriate vectorized scan.
7358 : :
7359 : : For exclusive scan, this is slightly different:
7360 : : #pragma omp simd reduction(inscan,+:r)
7361 : : for (...)
7362 : : {
7363 : : use (r);
7364 : : #pragma omp scan exclusive (r)
7365 : : r += something ();
7366 : : }
7367 : : shall have body with:
7368 : : // Initialization for input phase, store the reduction initializer:
7369 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7370 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7371 : : D.2042[_21] = 0;
7372 : : // Actual input phase:
7373 : : ...
7374 : : r.0_5 = D.2042[_20];
7375 : : _6 = _4 + r.0_5;
7376 : : D.2042[_20] = _6;
7377 : : // Initialization for scan phase:
7378 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
7379 : : _26 = D.2043[_25];
7380 : : D.2044[_25] = _26;
7381 : : _27 = D.2042[_25];
7382 : : _28 = _26 + _27;
7383 : : D.2043[_25] = _28;
7384 : : // Actual scan phase:
7385 : : ...
7386 : : r.1_8 = D.2044[_20];
7387 : : ... */
7388 : :
7389 : 1076 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
7390 : : {
7391 : : /* Match the D.2042[_21] = 0; store above. Just require that
7392 : : it is a constant or external definition store. */
7393 : 564 : if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
7394 : : {
7395 : 0 : fail_init:
7396 : 0 : if (dump_enabled_p ())
7397 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7398 : : "unsupported OpenMP scan initializer store.\n");
7399 : 0 : return false;
7400 : : }
7401 : :
7402 : 564 : if (! loop_vinfo->scan_map)
7403 : 322 : loop_vinfo->scan_map = new hash_map<tree, tree>;
7404 : 564 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7405 : 564 : tree &cached = loop_vinfo->scan_map->get_or_insert (var);
7406 : 564 : if (cached)
7407 : 0 : goto fail_init;
7408 : 564 : cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
7409 : :
7410 : : /* These stores can be vectorized normally. */
7411 : 564 : return true;
7412 : : }
7413 : :
7414 : 512 : if (rhs_dt != vect_internal_def)
7415 : : {
7416 : 0 : fail:
7417 : 0 : if (dump_enabled_p ())
7418 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7419 : : "unsupported OpenMP scan combiner pattern.\n");
7420 : 0 : return false;
7421 : : }
7422 : :
7423 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7424 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7425 : 512 : if (TREE_CODE (rhs) != SSA_NAME)
7426 : 0 : goto fail;
7427 : :
7428 : 512 : gimple *other_store_stmt = NULL;
7429 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7430 : 512 : bool inscan_var_store
7431 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7432 : :
7433 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7434 : : {
7435 : 252 : if (!inscan_var_store)
7436 : : {
7437 : 126 : use_operand_p use_p;
7438 : 126 : imm_use_iterator iter;
7439 : 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7440 : : {
7441 : 252 : gimple *use_stmt = USE_STMT (use_p);
7442 : 252 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7443 : 126 : continue;
7444 : 126 : if (gimple_bb (use_stmt) != gimple_bb (stmt)
7445 : 126 : || !is_gimple_assign (use_stmt)
7446 : 126 : || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
7447 : 126 : || other_store_stmt
7448 : 252 : || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
7449 : 0 : goto fail;
7450 : 126 : other_store_stmt = use_stmt;
7451 : : }
7452 : 126 : if (other_store_stmt == NULL)
7453 : 0 : goto fail;
7454 : 126 : rhs = gimple_assign_lhs (other_store_stmt);
7455 : 126 : if (!single_imm_use (rhs, &use_p, &other_store_stmt))
7456 : 0 : goto fail;
7457 : : }
7458 : : }
7459 : 260 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
7460 : : {
7461 : 260 : use_operand_p use_p;
7462 : 260 : imm_use_iterator iter;
7463 : 780 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7464 : : {
7465 : 520 : gimple *use_stmt = USE_STMT (use_p);
7466 : 520 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7467 : 260 : continue;
7468 : 260 : if (other_store_stmt)
7469 : 0 : goto fail;
7470 : 260 : other_store_stmt = use_stmt;
7471 : : }
7472 : : }
7473 : : else
7474 : 0 : goto fail;
7475 : :
7476 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7477 : 512 : if (gimple_bb (def_stmt) != gimple_bb (stmt)
7478 : 512 : || !is_gimple_assign (def_stmt)
7479 : 1024 : || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
7480 : 0 : goto fail;
7481 : :
7482 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7483 : : /* For pointer addition, we should use the normal plus for the vector
7484 : : operation. */
7485 : 512 : switch (code)
7486 : : {
7487 : 0 : case POINTER_PLUS_EXPR:
7488 : 0 : code = PLUS_EXPR;
7489 : 0 : break;
7490 : 0 : case MULT_HIGHPART_EXPR:
7491 : 0 : goto fail;
7492 : : default:
7493 : : break;
7494 : : }
7495 : 512 : if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
7496 : 0 : goto fail;
7497 : :
7498 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7499 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7500 : 512 : if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
7501 : 0 : goto fail;
7502 : :
7503 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7504 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7505 : 512 : if (gimple_bb (load1_stmt) != gimple_bb (stmt)
7506 : 512 : || !gimple_assign_load_p (load1_stmt)
7507 : 512 : || gimple_bb (load2_stmt) != gimple_bb (stmt)
7508 : 1024 : || !gimple_assign_load_p (load2_stmt))
7509 : 0 : goto fail;
7510 : :
7511 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7512 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7513 : 512 : if (load1_stmt_info == NULL
7514 : 512 : || load2_stmt_info == NULL
7515 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
7516 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
7517 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
7518 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7519 : 0 : goto fail;
7520 : :
7521 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
7522 : : {
7523 : 126 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7524 : 126 : if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
7525 : 126 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
7526 : 0 : goto fail;
7527 : 126 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7528 : 126 : tree lrhs;
7529 : 126 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7530 : : lrhs = rhs1;
7531 : : else
7532 : 16 : lrhs = rhs2;
7533 : 126 : use_operand_p use_p;
7534 : 126 : imm_use_iterator iter;
7535 : 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
7536 : : {
7537 : 252 : gimple *use_stmt = USE_STMT (use_p);
7538 : 252 : if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
7539 : 126 : continue;
7540 : 126 : if (other_store_stmt)
7541 : 0 : goto fail;
7542 : 126 : other_store_stmt = use_stmt;
7543 : : }
7544 : : }
7545 : :
7546 : 512 : if (other_store_stmt == NULL)
7547 : 0 : goto fail;
7548 : 512 : if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
7549 : 512 : || !gimple_store_p (other_store_stmt))
7550 : 0 : goto fail;
7551 : :
7552 : 512 : stmt_vec_info other_store_stmt_info
7553 : 512 : = loop_vinfo->lookup_stmt (other_store_stmt);
7554 : 512 : if (other_store_stmt_info == NULL
7555 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
7556 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7557 : 0 : goto fail;
7558 : :
7559 : 512 : gimple *stmt1 = stmt;
7560 : 512 : gimple *stmt2 = other_store_stmt;
7561 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7562 : : std::swap (stmt1, stmt2);
7563 : 512 : if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
7564 : : gimple_assign_rhs1 (load2_stmt)))
7565 : : {
7566 : 162 : std::swap (rhs1, rhs2);
7567 : 162 : std::swap (load1_stmt, load2_stmt);
7568 : 162 : std::swap (load1_stmt_info, load2_stmt_info);
7569 : : }
7570 : 512 : if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
7571 : : gimple_assign_rhs1 (load1_stmt)))
7572 : 0 : goto fail;
7573 : :
7574 : 512 : tree var3 = NULL_TREE;
7575 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
7576 : 512 : && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
7577 : : gimple_assign_rhs1 (load2_stmt)))
7578 : 0 : goto fail;
7579 : 512 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7580 : : {
7581 : 252 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7582 : 252 : if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
7583 : 252 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
7584 : 0 : goto fail;
7585 : 252 : var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7586 : 252 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
7587 : 252 : || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
7588 : 504 : || lookup_attribute ("omp simd inscan exclusive",
7589 : 252 : DECL_ATTRIBUTES (var3)))
7590 : 0 : goto fail;
7591 : : }
7592 : :
7593 : 512 : dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
7594 : 512 : if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
7595 : 512 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
7596 : 0 : goto fail;
7597 : :
7598 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7599 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
7600 : 512 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
7601 : 512 : || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
7602 : 1024 : || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7603 : 512 : == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
7604 : 0 : goto fail;
7605 : :
7606 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7607 : 256 : std::swap (var1, var2);
7608 : :
7609 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7610 : : {
7611 : 252 : if (!lookup_attribute ("omp simd inscan exclusive",
7612 : 252 : DECL_ATTRIBUTES (var1)))
7613 : 0 : goto fail;
7614 : 252 : var1 = var3;
7615 : : }
7616 : :
7617 : 512 : if (loop_vinfo->scan_map == NULL)
7618 : 0 : goto fail;
7619 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7620 : 512 : if (init == NULL)
7621 : 0 : goto fail;
7622 : :
7623 : : /* The IL is as expected, now check if we can actually vectorize it.
7624 : : Inclusive scan:
7625 : : _26 = D.2043[_25];
7626 : : _27 = D.2042[_25];
7627 : : _28 = _26 + _27;
7628 : : D.2043[_25] = _28;
7629 : : D.2042[_25] = _28;
7630 : : should be vectorized as (where _40 is the vectorized rhs
7631 : : from the D.2042[_21] = 0; store):
7632 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7633 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7634 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7635 : : _33 = _31 + _32;
7636 : : // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
7637 : : _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7638 : : _35 = _33 + _34;
7639 : : // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7640 : : // _31[1]+.._31[4], ... _31[4]+.._31[7] };
7641 : : _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7642 : : _37 = _35 + _36;
7643 : : // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7644 : : // _31[0]+.._31[4], ... _31[0]+.._31[7] };
7645 : : _38 = _30 + _37;
7646 : : _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7647 : : MEM <vector(8) int> [(int *)&D.2043] = _39;
7648 : : MEM <vector(8) int> [(int *)&D.2042] = _38;
7649 : : Exclusive scan:
7650 : : _26 = D.2043[_25];
7651 : : D.2044[_25] = _26;
7652 : : _27 = D.2042[_25];
7653 : : _28 = _26 + _27;
7654 : : D.2043[_25] = _28;
7655 : : should be vectorized as (where _40 is the vectorized rhs
7656 : : from the D.2042[_21] = 0; store):
7657 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7658 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7659 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7660 : : _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7661 : : _34 = _32 + _33;
7662 : : // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
7663 : : // _31[3]+_31[4], ... _31[5]+.._31[6] };
7664 : : _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7665 : : _36 = _34 + _35;
7666 : : // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7667 : : // _31[1]+.._31[4], ... _31[3]+.._31[6] };
7668 : : _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7669 : : _38 = _36 + _37;
7670 : : // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7671 : : // _31[0]+.._31[4], ... _31[0]+.._31[6] };
7672 : : _39 = _30 + _38;
7673 : : _50 = _31 + _39;
7674 : : _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7675 : : MEM <vector(8) int> [(int *)&D.2044] = _39;
7676 : : MEM <vector(8) int> [(int *)&D.2042] = _51; */
7677 : 512 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7678 : 512 : optab optab = optab_for_tree_code (code, vectype, optab_default);
7679 : 512 : if (!optab || !can_implement_p (optab, vec_mode))
7680 : 0 : goto fail;
7681 : :
7682 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init);
7683 : 512 : if (units_log2 == -1)
7684 : 0 : goto fail;
7685 : :
7686 : : return true;
7687 : : }
7688 : :
7689 : :
7690 : : /* Function vectorizable_scan_store.
7691 : :
7692 : : Helper of vectorizable_score, arguments like on vectorizable_store.
7693 : : Handle only the transformation, checking is done in check_scan_store. */
7694 : :
7695 : : static bool
7696 : 512 : vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
7697 : : slp_tree slp_node, gimple_stmt_iterator *gsi)
7698 : : {
7699 : 512 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7700 : 512 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7701 : 512 : tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
7702 : 512 : tree vectype = SLP_TREE_VECTYPE (slp_node);
7703 : :
7704 : 512 : if (dump_enabled_p ())
7705 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
7706 : : "transform scan store.\n");
7707 : :
7708 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7709 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7710 : 512 : gcc_assert (TREE_CODE (rhs) == SSA_NAME);
7711 : :
7712 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7713 : 512 : bool inscan_var_store
7714 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7715 : :
7716 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7717 : : {
7718 : 126 : use_operand_p use_p;
7719 : 126 : imm_use_iterator iter;
7720 : 126 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7721 : : {
7722 : 126 : gimple *use_stmt = USE_STMT (use_p);
7723 : 126 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7724 : 0 : continue;
7725 : 126 : rhs = gimple_assign_lhs (use_stmt);
7726 : 126 : break;
7727 : : }
7728 : : }
7729 : :
7730 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7731 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7732 : 512 : if (code == POINTER_PLUS_EXPR)
7733 : 0 : code = PLUS_EXPR;
7734 : 512 : gcc_assert (TREE_CODE_LENGTH (code) == binary_op
7735 : : && commutative_tree_code (code));
7736 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7737 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7738 : 512 : gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
7739 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7740 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7741 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7742 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7743 : 512 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7744 : 512 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7745 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7746 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7747 : :
7748 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7749 : : {
7750 : 436 : std::swap (rhs1, rhs2);
7751 : 436 : std::swap (var1, var2);
7752 : 436 : std::swap (load1_dr_info, load2_dr_info);
7753 : : }
7754 : :
7755 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7756 : 512 : gcc_assert (init);
7757 : :
7758 : 512 : unsigned HOST_WIDE_INT nunits;
7759 : 512 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7760 : : gcc_unreachable ();
7761 : 512 : auto_vec<enum scan_store_kind, 16> use_whole_vector;
7762 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
7763 : 512 : gcc_assert (units_log2 > 0);
7764 : 512 : auto_vec<tree, 16> perms;
7765 : 512 : perms.quick_grow (units_log2 + 1);
7766 : 512 : tree zero_vec = NULL_TREE, masktype = NULL_TREE;
7767 : 2392 : for (int i = 0; i <= units_log2; ++i)
7768 : : {
7769 : 1880 : unsigned HOST_WIDE_INT j, k;
7770 : 1880 : vec_perm_builder sel (nunits, nunits, 1);
7771 : 1880 : sel.quick_grow (nunits);
7772 : 1880 : if (i == units_log2)
7773 : 4864 : for (j = 0; j < nunits; ++j)
7774 : 4352 : sel[j] = nunits - 1;
7775 : : else
7776 : : {
7777 : 5208 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7778 : 3840 : sel[j] = j;
7779 : 13208 : for (k = 0; j < nunits; ++j, ++k)
7780 : 11840 : sel[j] = nunits + k;
7781 : : }
7782 : 3248 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7783 : 1880 : if (!use_whole_vector.is_empty ()
7784 : 0 : && use_whole_vector[i] != scan_store_kind_perm)
7785 : : {
7786 : 0 : if (zero_vec == NULL_TREE)
7787 : 0 : zero_vec = build_zero_cst (vectype);
7788 : 0 : if (masktype == NULL_TREE
7789 : 0 : && use_whole_vector[i] == scan_store_kind_lshift_cond)
7790 : 0 : masktype = truth_type_for (vectype);
7791 : 0 : perms[i] = vect_gen_perm_mask_any (vectype, indices);
7792 : : }
7793 : : else
7794 : 1880 : perms[i] = vect_gen_perm_mask_checked (vectype, indices);
7795 : 1880 : }
7796 : :
7797 : 512 : tree vec_oprnd1 = NULL_TREE;
7798 : 512 : tree vec_oprnd2 = NULL_TREE;
7799 : 512 : tree vec_oprnd3 = NULL_TREE;
7800 : 512 : tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
7801 : 512 : tree dataref_offset = build_int_cst (ref_type, 0);
7802 : 512 : tree bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info,
7803 : : vectype, VMAT_CONTIGUOUS);
7804 : 512 : tree ldataref_ptr = NULL_TREE;
7805 : 512 : tree orig = NULL_TREE;
7806 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7807 : 126 : ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
7808 : : /* The initialization is invariant. */
7809 : 512 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
7810 : 512 : auto_vec<tree> vec_oprnds2;
7811 : 512 : auto_vec<tree> vec_oprnds3;
7812 : 512 : if (ldataref_ptr == NULL)
7813 : : {
7814 : : /* We want to lookup the vector operands of the reduction, not those
7815 : : of the store - for SLP we have to use the proper SLP node for the
7816 : : lookup, which should be the single child of the scan store. */
7817 : 386 : vect_get_vec_defs (vinfo, SLP_TREE_CHILDREN (slp_node)[0],
7818 : : rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
7819 : : /* ??? For SLP we do not key the def on 'rhs1' or 'rhs2' but get
7820 : : them in SLP child order. So we have to swap here with logic
7821 : : similar to above. */
7822 : 386 : stmt_vec_info load
7823 : 386 : = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
7824 : 386 : (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
7825 : 386 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
7826 : 386 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7827 : 386 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
7828 : 820 : for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
7829 : 494 : std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
7830 : : }
7831 : : else
7832 : 126 : vect_get_vec_defs (vinfo, slp_node,
7833 : : rhs2, &vec_oprnds3);
7834 : 1248 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
7835 : : {
7836 : 736 : if (ldataref_ptr == NULL)
7837 : 554 : vec_oprnd2 = vec_oprnds2[j];
7838 : 736 : vec_oprnd3 = vec_oprnds3[j];
7839 : 736 : if (j == 0)
7840 : : orig = vec_oprnd3;
7841 : 224 : else if (!inscan_var_store)
7842 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
7843 : :
7844 : 736 : if (ldataref_ptr)
7845 : : {
7846 : 182 : vec_oprnd2 = make_ssa_name (vectype);
7847 : 182 : tree data_ref = fold_build2 (MEM_REF, vectype,
7848 : : unshare_expr (ldataref_ptr),
7849 : : dataref_offset);
7850 : 182 : vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
7851 : 182 : gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
7852 : 182 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7853 : : }
7854 : :
7855 : 736 : tree v = vec_oprnd2;
7856 : 3068 : for (int i = 0; i < units_log2; ++i)
7857 : : {
7858 : 2332 : tree new_temp = make_ssa_name (vectype);
7859 : 2332 : gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
7860 : : (zero_vec
7861 : 0 : && (use_whole_vector[i]
7862 : 0 : != scan_store_kind_perm))
7863 : : ? zero_vec : vec_oprnd1, v,
7864 : 2332 : perms[i]);
7865 : 2332 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7866 : :
7867 : 2332 : if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
7868 : : {
7869 : : /* Whole vector shift shifted in zero bits, but if *init
7870 : : is not initializer_zerop, we need to replace those elements
7871 : : with elements from vec_oprnd1. */
7872 : 0 : tree_vector_builder vb (masktype, nunits, 1);
7873 : 0 : for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
7874 : 0 : vb.quick_push (k < (HOST_WIDE_INT_1U << i)
7875 : : ? boolean_false_node : boolean_true_node);
7876 : :
7877 : 0 : tree new_temp2 = make_ssa_name (vectype);
7878 : 0 : g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
7879 : : new_temp, vec_oprnd1);
7880 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
7881 : : g, gsi);
7882 : 0 : new_temp = new_temp2;
7883 : 0 : }
7884 : :
7885 : : /* For exclusive scan, perform the perms[i] permutation once
7886 : : more. */
7887 : 2332 : if (i == 0
7888 : 1100 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
7889 : 728 : && v == vec_oprnd2)
7890 : : {
7891 : 364 : v = new_temp;
7892 : 364 : --i;
7893 : 364 : continue;
7894 : : }
7895 : :
7896 : 1968 : tree new_temp2 = make_ssa_name (vectype);
7897 : 1968 : g = gimple_build_assign (new_temp2, code, v, new_temp);
7898 : 1968 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7899 : :
7900 : 1968 : v = new_temp2;
7901 : : }
7902 : :
7903 : 736 : tree new_temp = make_ssa_name (vectype);
7904 : 736 : gimple *g = gimple_build_assign (new_temp, code, orig, v);
7905 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7906 : :
7907 : 736 : tree last_perm_arg = new_temp;
7908 : : /* For exclusive scan, new_temp computed above is the exclusive scan
7909 : : prefix sum. Turn it into inclusive prefix sum for the broadcast
7910 : : of the last element into orig. */
7911 : 736 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7912 : : {
7913 : 364 : last_perm_arg = make_ssa_name (vectype);
7914 : 364 : g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
7915 : 364 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7916 : : }
7917 : :
7918 : 736 : orig = make_ssa_name (vectype);
7919 : 2208 : g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
7920 : 736 : last_perm_arg, perms[units_log2]);
7921 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7922 : :
7923 : 736 : if (!inscan_var_store)
7924 : : {
7925 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
7926 : : unshare_expr (dataref_ptr),
7927 : : dataref_offset);
7928 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
7929 : 368 : g = gimple_build_assign (data_ref, new_temp);
7930 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7931 : : }
7932 : : }
7933 : :
7934 : 512 : if (inscan_var_store)
7935 : 624 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
7936 : : {
7937 : 368 : if (j != 0)
7938 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
7939 : :
7940 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
7941 : : unshare_expr (dataref_ptr),
7942 : : dataref_offset);
7943 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
7944 : 368 : gimple *g = gimple_build_assign (data_ref, orig);
7945 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7946 : : }
7947 : 512 : return true;
7948 : 512 : }
7949 : :
7950 : :
7951 : : /* Function vectorizable_store.
7952 : :
7953 : : Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
7954 : : that can be vectorized.
7955 : : If COST_VEC is passed, calculate costs but don't change anything,
7956 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
7957 : : it, and insert it at GSI.
7958 : : Return true if STMT_INFO is vectorizable in this way. */
7959 : :
7960 : : static bool
7961 : 1939465 : vectorizable_store (vec_info *vinfo,
7962 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
7963 : : slp_tree slp_node,
7964 : : stmt_vector_for_cost *cost_vec)
7965 : : {
7966 : 1939465 : tree data_ref;
7967 : 1939465 : tree vec_oprnd = NULL_TREE;
7968 : 1939465 : tree elem_type;
7969 : 1939465 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7970 : 1939465 : class loop *loop = NULL;
7971 : 1939465 : machine_mode vec_mode;
7972 : 1939465 : tree dummy;
7973 : 1939465 : enum vect_def_type rhs_dt = vect_unknown_def_type;
7974 : 1939465 : enum vect_def_type mask_dt = vect_unknown_def_type;
7975 : 1939465 : tree dataref_ptr = NULL_TREE;
7976 : 1939465 : tree dataref_offset = NULL_TREE;
7977 : 1939465 : gimple *ptr_incr = NULL;
7978 : 1939465 : int j;
7979 : 1939465 : stmt_vec_info first_stmt_info;
7980 : 1939465 : bool grouped_store;
7981 : 1939465 : unsigned int group_size, i;
7982 : 1939465 : unsigned int vec_num;
7983 : 1939465 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
7984 : 1939465 : tree aggr_type;
7985 : 1939465 : poly_uint64 vf;
7986 : 1939465 : vec_load_store_type vls_type;
7987 : 1939465 : tree ref_type;
7988 : :
7989 : 1939465 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
7990 : : return false;
7991 : :
7992 : 1939465 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
7993 : 175983 : && cost_vec)
7994 : : return false;
7995 : :
7996 : : /* Is vectorizable store? */
7997 : :
7998 : 1763482 : tree mask_vectype = NULL_TREE;
7999 : 1763482 : slp_tree mask_node = NULL;
8000 : 1763482 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
8001 : : {
8002 : 1690806 : tree scalar_dest = gimple_assign_lhs (assign);
8003 : 1690806 : if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
8004 : 1690806 : && is_pattern_stmt_p (stmt_info))
8005 : 1386 : scalar_dest = TREE_OPERAND (scalar_dest, 0);
8006 : 1690806 : if (TREE_CODE (scalar_dest) != ARRAY_REF
8007 : 1690806 : && TREE_CODE (scalar_dest) != BIT_FIELD_REF
8008 : : && TREE_CODE (scalar_dest) != INDIRECT_REF
8009 : : && TREE_CODE (scalar_dest) != COMPONENT_REF
8010 : : && TREE_CODE (scalar_dest) != IMAGPART_EXPR
8011 : : && TREE_CODE (scalar_dest) != REALPART_EXPR
8012 : : && TREE_CODE (scalar_dest) != MEM_REF)
8013 : : return false;
8014 : : }
8015 : : else
8016 : : {
8017 : 626190 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
8018 : 8405 : if (!call || !gimple_call_internal_p (call))
8019 : : return false;
8020 : :
8021 : 4810 : internal_fn ifn = gimple_call_internal_fn (call);
8022 : 4810 : if (!internal_store_fn_p (ifn))
8023 : : return false;
8024 : :
8025 : 1443 : int mask_index = internal_fn_mask_index (ifn);
8026 : 1443 : if (mask_index >= 0)
8027 : 1443 : mask_index = vect_slp_child_index_for_operand
8028 : 1443 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
8029 : 1443 : if (mask_index >= 0
8030 : 1443 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
8031 : : &mask_node, &mask_dt,
8032 : : &mask_vectype))
8033 : : return false;
8034 : : }
8035 : :
8036 : 1321712 : tree vectype = SLP_TREE_VECTYPE (slp_node), rhs_vectype = NULL_TREE;
8037 : 1321712 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
8038 : :
8039 : 1321712 : if (loop_vinfo)
8040 : : {
8041 : 185495 : loop = LOOP_VINFO_LOOP (loop_vinfo);
8042 : 185495 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8043 : : }
8044 : : else
8045 : : vf = 1;
8046 : 1321712 : vec_num = vect_get_num_copies (vinfo, slp_node);
8047 : :
8048 : : /* FORNOW. This restriction should be relaxed. */
8049 : 1321712 : if (loop
8050 : 1321933 : && nested_in_vect_loop_p (loop, stmt_info)
8051 : 1321941 : && vec_num > 1)
8052 : : {
8053 : 8 : if (dump_enabled_p ())
8054 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8055 : : "multiple types in nested loop.\n");
8056 : 8 : return false;
8057 : : }
8058 : :
8059 : 1321704 : slp_tree op_node;
8060 : 1321704 : if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
8061 : : &op_node, &rhs_dt, &rhs_vectype, &vls_type))
8062 : : return false;
8063 : :
8064 : 1321680 : elem_type = TREE_TYPE (vectype);
8065 : 1321680 : vec_mode = TYPE_MODE (vectype);
8066 : :
8067 : 1321680 : if (!STMT_VINFO_DATA_REF (stmt_info))
8068 : : return false;
8069 : :
8070 : 1321680 : vect_load_store_data _ls_data{};
8071 : 1321680 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
8072 : 1321680 : if (cost_vec
8073 : 1321680 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
8074 : : vls_type, &_ls_data))
8075 : : return false;
8076 : : /* Temporary aliases to analysis data, should not be modified through
8077 : : these. */
8078 : 1321132 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
8079 : 1321132 : const dr_alignment_support alignment_support_scheme
8080 : : = ls.alignment_support_scheme;
8081 : 1321132 : const int misalignment = ls.misalignment;
8082 : 1321132 : const poly_int64 poffset = ls.poffset;
8083 : :
8084 : 1321132 : if (slp_node->ldst_lanes
8085 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
8086 : : {
8087 : 0 : if (dump_enabled_p ())
8088 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8089 : : "discovered store-lane but cannot use it.\n");
8090 : 0 : return false;
8091 : : }
8092 : :
8093 : 1321132 : if (mask_node)
8094 : : {
8095 : 1353 : if (memory_access_type == VMAT_CONTIGUOUS)
8096 : : {
8097 : 459 : if (!VECTOR_MODE_P (vec_mode)
8098 : 2214 : || !can_vec_mask_load_store_p (vec_mode,
8099 : 1107 : TYPE_MODE (mask_vectype), false))
8100 : 18 : return false;
8101 : : }
8102 : 246 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
8103 : 246 : && (!mat_gather_scatter_p (memory_access_type)
8104 : 222 : || (memory_access_type == VMAT_GATHER_SCATTER_LEGACY
8105 : 154 : && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
8106 : : {
8107 : 24 : if (dump_enabled_p ())
8108 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8109 : : "unsupported access type for masked store.\n");
8110 : 24 : return false;
8111 : : }
8112 : 222 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
8113 : : {
8114 : 68 : if (dump_enabled_p ())
8115 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8116 : : "unsupported masked emulated scatter.\n");
8117 : 68 : return false;
8118 : : }
8119 : : }
8120 : : else
8121 : : {
8122 : : /* FORNOW. In some cases can vectorize even if data-type not supported
8123 : : (e.g. - array initialization with 0). */
8124 : 1319779 : if (!can_implement_p (mov_optab, vec_mode))
8125 : : return false;
8126 : : }
8127 : :
8128 : 1321022 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
8129 : 1321022 : grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
8130 : 2474284 : && !mat_gather_scatter_p (memory_access_type));
8131 : 1153262 : if (grouped_store)
8132 : : {
8133 : 1153262 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8134 : 1153262 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8135 : 1153262 : group_size = DR_GROUP_SIZE (first_stmt_info);
8136 : : }
8137 : : else
8138 : : {
8139 : 1321022 : first_stmt_info = stmt_info;
8140 : 1321022 : first_dr_info = dr_info;
8141 : : group_size = 1;
8142 : : }
8143 : :
8144 : 1321022 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && cost_vec)
8145 : : {
8146 : 1076 : if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node,
8147 : : mask_node, memory_access_type))
8148 : : return false;
8149 : : }
8150 : :
8151 : 2641276 : bool costing_p = cost_vec;
8152 : 1320254 : if (costing_p) /* transformation not required. */
8153 : : {
8154 : 777219 : if (loop_vinfo
8155 : 124078 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
8156 : 37 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
8157 : : vls_type, group_size, &ls,
8158 : : mask_node);
8159 : :
8160 : 777219 : if (!vect_maybe_update_slp_op_vectype (op_node, vectype)
8161 : 777219 : || (mask_node
8162 : 707 : && !vect_maybe_update_slp_op_vectype (mask_node,
8163 : : mask_vectype)))
8164 : : {
8165 : 0 : if (dump_enabled_p ())
8166 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8167 : : "incompatible vector types for invariants\n");
8168 : 0 : return false;
8169 : : }
8170 : :
8171 : 777219 : if (dump_enabled_p ()
8172 : : && memory_access_type != VMAT_ELEMENTWISE
8173 : 14203 : && memory_access_type != VMAT_STRIDED_SLP
8174 : 13592 : && memory_access_type != VMAT_INVARIANT
8175 : 790811 : && alignment_support_scheme != dr_aligned)
8176 : 4624 : dump_printf_loc (MSG_NOTE, vect_location,
8177 : : "Vectorizing an unaligned access.\n");
8178 : :
8179 : 777219 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
8180 : 777219 : slp_node->data = new vect_load_store_data (std::move (ls));
8181 : : }
8182 : :
8183 : : /* Transform. */
8184 : :
8185 : 1321022 : ensure_base_align (dr_info);
8186 : :
8187 : 1321022 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
8188 : : {
8189 : 1024 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
8190 : 1024 : gcc_assert (SLP_TREE_LANES (slp_node) == 1);
8191 : 1024 : if (costing_p)
8192 : : {
8193 : 512 : unsigned int inside_cost = 0, prologue_cost = 0;
8194 : 512 : if (vls_type == VLS_STORE_INVARIANT)
8195 : 0 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8196 : : slp_node, 0, vect_prologue);
8197 : 512 : vect_get_store_cost (vinfo, stmt_info, slp_node, 1,
8198 : : alignment_support_scheme, misalignment,
8199 : : &inside_cost, cost_vec);
8200 : :
8201 : 512 : if (dump_enabled_p ())
8202 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
8203 : : "vect_model_store_cost: inside_cost = %d, "
8204 : : "prologue_cost = %d .\n",
8205 : : inside_cost, prologue_cost);
8206 : :
8207 : 512 : return true;
8208 : : }
8209 : 512 : return vectorizable_scan_store (vinfo, stmt_info, slp_node, gsi);
8210 : : }
8211 : :
8212 : : /* FORNOW */
8213 : 1319998 : gcc_assert (!grouped_store
8214 : : || !loop
8215 : : || !nested_in_vect_loop_p (loop, stmt_info));
8216 : :
8217 : 1319998 : grouped_store = false;
8218 : 1319998 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
8219 : 1319998 : gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
8220 : : || (DR_GROUP_FIRST_ELEMENT (first_stmt_info) == first_stmt_info));
8221 : 1319998 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8222 : :
8223 : 1319998 : ref_type = get_group_alias_ptr_type (first_stmt_info);
8224 : :
8225 : 1319998 : if (!costing_p && dump_enabled_p ())
8226 : 11807 : dump_printf_loc (MSG_NOTE, vect_location, "transform store.\n");
8227 : :
8228 : 1319998 : if (memory_access_type == VMAT_ELEMENTWISE
8229 : 1319998 : || memory_access_type == VMAT_STRIDED_SLP)
8230 : : {
8231 : 28535 : unsigned inside_cost = 0, prologue_cost = 0;
8232 : 28535 : gimple_stmt_iterator incr_gsi;
8233 : 28535 : bool insert_after;
8234 : 28535 : tree offvar = NULL_TREE;
8235 : 28535 : tree ivstep;
8236 : 28535 : tree running_off;
8237 : 28535 : tree stride_base, stride_step, alias_off;
8238 : 28535 : tree vec_oprnd = NULL_TREE;
8239 : 28535 : tree dr_offset;
8240 : : /* Checked by get_load_store_type. */
8241 : 28535 : unsigned int const_nunits = nunits.to_constant ();
8242 : :
8243 : 28535 : gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
8244 : 28535 : gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
8245 : :
8246 : 28535 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
8247 : 28535 : stride_base
8248 : 28535 : = fold_build_pointer_plus
8249 : : (DR_BASE_ADDRESS (first_dr_info->dr),
8250 : : size_binop (PLUS_EXPR,
8251 : : convert_to_ptrofftype (dr_offset),
8252 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
8253 : 28535 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
8254 : :
8255 : : /* For a store with loop-invariant (but other than power-of-2)
8256 : : stride (i.e. not a grouped access) like so:
8257 : :
8258 : : for (i = 0; i < n; i += stride)
8259 : : array[i] = ...;
8260 : :
8261 : : we generate a new induction variable and new stores from
8262 : : the components of the (vectorized) rhs:
8263 : :
8264 : : for (j = 0; ; j += VF*stride)
8265 : : vectemp = ...;
8266 : : tmp1 = vectemp[0];
8267 : : array[j] = tmp1;
8268 : : tmp2 = vectemp[1];
8269 : : array[j + stride] = tmp2;
8270 : : ...
8271 : : */
8272 : :
8273 : : /* ??? Modify local copies of alignment_support_scheme and
8274 : : misalignment, but this part of analysis should be done
8275 : : earlier and remembered, likewise the chosen load mode. */
8276 : 28535 : const dr_alignment_support tem = alignment_support_scheme;
8277 : 28535 : dr_alignment_support alignment_support_scheme = tem;
8278 : 28535 : const int tem2 = misalignment;
8279 : 28535 : int misalignment = tem2;
8280 : :
8281 : 28535 : unsigned nstores = const_nunits;
8282 : 28535 : unsigned lnel = 1;
8283 : 28535 : tree ltype = elem_type;
8284 : 28535 : tree lvectype = vectype;
8285 : 28535 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
8286 : 28535 : if (n == const_nunits)
8287 : : {
8288 : 2529 : int mis_align = dr_misalignment (first_dr_info, vectype);
8289 : : /* With VF > 1 we advance the DR by step, if that is constant
8290 : : and only aligned when performed VF times, DR alignment
8291 : : analysis can analyze this as aligned since it assumes
8292 : : contiguous accesses. But that is not how we code generate
8293 : : here, so adjust for this. */
8294 : 2529 : if (maybe_gt (vf, 1u)
8295 : 4042 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8296 : 3814 : DR_TARGET_ALIGNMENT (first_dr_info)))
8297 : 228 : mis_align = -1;
8298 : 2529 : dr_alignment_support dr_align
8299 : 2529 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
8300 : : mis_align);
8301 : 2529 : if (dr_align == dr_aligned
8302 : 2529 : || dr_align == dr_unaligned_supported)
8303 : : {
8304 : 28535 : nstores = 1;
8305 : 28535 : lnel = const_nunits;
8306 : 28535 : ltype = vectype;
8307 : 28535 : lvectype = vectype;
8308 : 28535 : alignment_support_scheme = dr_align;
8309 : 28535 : misalignment = mis_align;
8310 : : }
8311 : : }
8312 : 26006 : else if (n > 1)
8313 : : {
8314 : 1795 : nstores = const_nunits / n;
8315 : 1795 : lnel = n;
8316 : 1795 : ltype = build_vector_type (elem_type, n);
8317 : 1795 : lvectype = vectype;
8318 : 1795 : int mis_align = dr_misalignment (first_dr_info, ltype);
8319 : 1795 : if (maybe_gt (vf, 1u)
8320 : 3590 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8321 : 2883 : DR_TARGET_ALIGNMENT (first_dr_info)))
8322 : 707 : mis_align = -1;
8323 : 1795 : dr_alignment_support dr_align
8324 : 1795 : = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
8325 : : mis_align);
8326 : 1795 : alignment_support_scheme = dr_align;
8327 : 1795 : misalignment = mis_align;
8328 : :
8329 : : /* First check if vec_extract optab doesn't support extraction
8330 : : of vector elts directly. */
8331 : 1795 : scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
8332 : 1795 : machine_mode vmode;
8333 : 3590 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8334 : 2021 : || !related_vector_mode (TYPE_MODE (vectype), elmode,
8335 : 1795 : n).exists (&vmode)
8336 : 1619 : || (convert_optab_handler (vec_extract_optab,
8337 : 1619 : TYPE_MODE (vectype), vmode)
8338 : : == CODE_FOR_nothing)
8339 : 1795 : || !(dr_align == dr_aligned
8340 : 226 : || dr_align == dr_unaligned_supported))
8341 : : {
8342 : : /* Try to avoid emitting an extract of vector elements
8343 : : by performing the extracts using an integer type of the
8344 : : same size, extracting from a vector of those and then
8345 : : re-interpreting it as the original vector type if
8346 : : supported. */
8347 : 1569 : unsigned lsize = n * GET_MODE_BITSIZE (elmode);
8348 : 1569 : unsigned int lnunits = const_nunits / n;
8349 : : /* If we can't construct such a vector fall back to
8350 : : element extracts from the original vector type and
8351 : : element size stores. */
8352 : 1569 : if (int_mode_for_size (lsize, 0).exists (&elmode)
8353 : 1569 : && VECTOR_MODE_P (TYPE_MODE (vectype))
8354 : 1569 : && related_vector_mode (TYPE_MODE (vectype), elmode,
8355 : 1569 : lnunits).exists (&vmode)
8356 : 1535 : && (convert_optab_handler (vec_extract_optab,
8357 : : vmode, elmode)
8358 : : != CODE_FOR_nothing))
8359 : : {
8360 : 1535 : nstores = lnunits;
8361 : 1535 : lnel = n;
8362 : 1535 : ltype = build_nonstandard_integer_type (lsize, 1);
8363 : 1535 : lvectype = build_vector_type (ltype, nstores);
8364 : : }
8365 : : /* Else fall back to vector extraction anyway.
8366 : : Fewer stores are more important than avoiding spilling
8367 : : of the vector we extract from. Compared to the
8368 : : construction case in vectorizable_load no store-forwarding
8369 : : issue exists here for reasonable archs. But only
8370 : : if the store is supported. */
8371 : 34 : else if (!(dr_align == dr_aligned
8372 : 34 : || dr_align == dr_unaligned_supported))
8373 : : {
8374 : : nstores = const_nunits;
8375 : : lnel = 1;
8376 : : ltype = elem_type;
8377 : : lvectype = vectype;
8378 : : }
8379 : : }
8380 : : }
8381 : 28535 : unsigned align;
8382 : 28535 : if (alignment_support_scheme == dr_aligned)
8383 : 1118 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
8384 : : else
8385 : 27417 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
8386 : : /* Alignment is at most the access size if we do multiple stores. */
8387 : 28535 : if (nstores > 1)
8388 : 26006 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
8389 : 28535 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
8390 : 28535 : int ncopies = vec_num;
8391 : :
8392 : 28535 : if (!costing_p)
8393 : : {
8394 : 3243 : ivstep = stride_step;
8395 : 3243 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
8396 : : build_int_cst (TREE_TYPE (ivstep), vf));
8397 : :
8398 : 3243 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
8399 : :
8400 : 3243 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
8401 : 3243 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
8402 : 3243 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
8403 : : insert_after, &offvar, NULL);
8404 : :
8405 : 3243 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
8406 : : }
8407 : :
8408 : 28535 : alias_off = build_int_cst (ref_type, 0);
8409 : 28535 : auto_vec<tree> vec_oprnds;
8410 : : /* For costing some adjacent vector stores, we'd like to cost with
8411 : : the total number of them once instead of cost each one by one. */
8412 : 28535 : unsigned int n_adjacent_stores = 0;
8413 : 28535 : running_off = offvar;
8414 : 28535 : if (!costing_p)
8415 : 3243 : vect_get_slp_defs (op_node, &vec_oprnds);
8416 : 28535 : unsigned int group_el = 0;
8417 : 28535 : unsigned HOST_WIDE_INT elsz
8418 : 28535 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
8419 : 67980 : for (j = 0; j < ncopies; j++)
8420 : : {
8421 : 39445 : if (!costing_p)
8422 : : {
8423 : 4956 : vec_oprnd = vec_oprnds[j];
8424 : : /* Pun the vector to extract from if necessary. */
8425 : 4956 : if (lvectype != vectype)
8426 : : {
8427 : 1210 : tree tem = make_ssa_name (lvectype);
8428 : 1210 : tree cvt = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
8429 : 1210 : gimple *pun = gimple_build_assign (tem, cvt);
8430 : 1210 : vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
8431 : 1210 : vec_oprnd = tem;
8432 : : }
8433 : : }
8434 : 177253 : for (i = 0; i < nstores; i++)
8435 : : {
8436 : 137808 : if (costing_p)
8437 : : {
8438 : 122167 : n_adjacent_stores++;
8439 : 122167 : continue;
8440 : : }
8441 : 15641 : tree newref, newoff;
8442 : 15641 : gimple *incr, *assign;
8443 : 15641 : tree size = TYPE_SIZE (ltype);
8444 : : /* Extract the i'th component. */
8445 : 15641 : tree pos = fold_build2 (MULT_EXPR, bitsizetype,
8446 : : bitsize_int (i), size);
8447 : 15641 : tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
8448 : : size, pos);
8449 : :
8450 : 15641 : elem = force_gimple_operand_gsi (gsi, elem, true, NULL_TREE, true,
8451 : : GSI_SAME_STMT);
8452 : :
8453 : 15641 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
8454 : 15641 : group_el * elsz);
8455 : 15641 : newref = build2 (MEM_REF, ltype, running_off, this_off);
8456 : 15641 : vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
8457 : :
8458 : : /* And store it to *running_off. */
8459 : 15641 : assign = gimple_build_assign (newref, elem);
8460 : 15641 : vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
8461 : :
8462 : 15641 : group_el += lnel;
8463 : 15641 : if (group_el == group_size)
8464 : : {
8465 : 14538 : newoff = copy_ssa_name (running_off, NULL);
8466 : 14538 : incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
8467 : : running_off, stride_step);
8468 : 14538 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
8469 : :
8470 : 14538 : running_off = newoff;
8471 : 14538 : group_el = 0;
8472 : : }
8473 : : }
8474 : : }
8475 : :
8476 : 28535 : if (costing_p)
8477 : : {
8478 : 25292 : if (n_adjacent_stores > 0)
8479 : : {
8480 : : /* Take a single lane vector type store as scalar
8481 : : store to avoid ICE like 110776. */
8482 : 25292 : if (VECTOR_TYPE_P (ltype)
8483 : 25292 : && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
8484 : 1202 : vect_get_store_cost (vinfo, stmt_info, slp_node,
8485 : : n_adjacent_stores, alignment_support_scheme,
8486 : : misalignment, &inside_cost, cost_vec);
8487 : : else
8488 : 24090 : inside_cost
8489 : 24090 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8490 : : scalar_store, slp_node, 0, vect_body);
8491 : : /* Only need vector extracting when there are more
8492 : : than one stores. */
8493 : 25292 : if (nstores > 1)
8494 : 23504 : inside_cost
8495 : 23504 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8496 : : vec_to_scalar, slp_node, 0, vect_body);
8497 : : }
8498 : 25292 : if (dump_enabled_p ())
8499 : 611 : dump_printf_loc (MSG_NOTE, vect_location,
8500 : : "vect_model_store_cost: inside_cost = %d, "
8501 : : "prologue_cost = %d .\n",
8502 : : inside_cost, prologue_cost);
8503 : : }
8504 : :
8505 : 28535 : return true;
8506 : 28535 : }
8507 : :
8508 : 1291463 : gcc_assert (alignment_support_scheme);
8509 : 1291463 : vec_loop_masks *loop_masks
8510 : 155246 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
8511 : 1291463 : ? &LOOP_VINFO_MASKS (loop_vinfo)
8512 : 10 : : NULL);
8513 : 10 : vec_loop_lens *loop_lens
8514 : 155246 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
8515 : : ? &LOOP_VINFO_LENS (loop_vinfo)
8516 : 0 : : NULL);
8517 : :
8518 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
8519 : : are some difference here. We cannot enable both the lens and masks
8520 : : during transform but it is allowed during analysis.
8521 : : Shouldn't go with length-based approach if fully masked. */
8522 : 1291463 : if (cost_vec == NULL)
8523 : : /* The cost_vec is NULL during transfrom. */
8524 : 540048 : gcc_assert ((!loop_lens || !loop_masks));
8525 : :
8526 : : /* Targets with store-lane instructions must not require explicit
8527 : : realignment. vect_supportable_dr_alignment always returns either
8528 : : dr_aligned or dr_unaligned_supported for masked operations. */
8529 : 1291463 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
8530 : : && !mask_node
8531 : : && !loop_masks)
8532 : : || alignment_support_scheme == dr_aligned
8533 : : || alignment_support_scheme == dr_unaligned_supported);
8534 : :
8535 : 1291463 : tree offset = NULL_TREE;
8536 : 1291463 : if (!known_eq (poffset, 0))
8537 : 4082 : offset = size_int (poffset);
8538 : :
8539 : 1291463 : tree bump;
8540 : 1291463 : tree vec_offset = NULL_TREE;
8541 : 1291463 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8542 : : {
8543 : 2397 : aggr_type = NULL_TREE;
8544 : 2397 : bump = NULL_TREE;
8545 : : }
8546 : 1289066 : else if (mat_gather_scatter_p (memory_access_type))
8547 : : {
8548 : 0 : aggr_type = elem_type;
8549 : 0 : if (!costing_p)
8550 : : {
8551 : 0 : tree vtype = ls.ls_type ? ls.ls_type : vectype;
8552 : 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vtype,
8553 : : ls.strided_offset_vectype,
8554 : : loop_vinfo, gsi,
8555 : : &bump, &vec_offset, loop_lens);
8556 : : }
8557 : : }
8558 : : else
8559 : : {
8560 : 1289066 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8561 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
8562 : : else
8563 : : aggr_type = vectype;
8564 : 1289066 : if (!costing_p)
8565 : 539574 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
8566 : : memory_access_type, loop_lens);
8567 : : }
8568 : :
8569 : 1291463 : if (mask_node && !costing_p)
8570 : 536 : LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
8571 : :
8572 : : /* In case the vectorization factor (VF) is bigger than the number
8573 : : of elements that we can fit in a vectype (nunits), we have to generate
8574 : : more than one vector stmt - i.e - we need to "unroll" the
8575 : : vector stmt by a factor VF/nunits. */
8576 : :
8577 : 1291463 : auto_vec<tree> dr_chain (group_size);
8578 : 1291463 : auto_vec<tree> vec_masks;
8579 : 1291463 : tree vec_mask = NULL;
8580 : 1291463 : auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
8581 : 5874760 : for (i = 0; i < group_size; i++)
8582 : 3291834 : gvec_oprnds.quick_push (new auto_vec<tree> ());
8583 : :
8584 : 1291463 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8585 : : {
8586 : 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
8587 : :
8588 : 0 : if (costing_p)
8589 : : /* Update all incoming store operand nodes, the general handling
8590 : : above only handles the mask and the first store operand node. */
8591 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
8592 : 0 : if (child != mask_node
8593 : 0 : && !vect_maybe_update_slp_op_vectype (child, vectype))
8594 : : {
8595 : 0 : if (dump_enabled_p ())
8596 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8597 : : "incompatible vector types for invariants\n");
8598 : 0 : return false;
8599 : : }
8600 : 0 : unsigned inside_cost = 0, prologue_cost = 0;
8601 : : /* For costing some adjacent vector stores, we'd like to cost with
8602 : : the total number of them once instead of cost each one by one. */
8603 : 0 : unsigned int n_adjacent_stores = 0;
8604 : 0 : int ncopies = vec_num / group_size;
8605 : 0 : for (j = 0; j < ncopies; j++)
8606 : : {
8607 : 0 : if (j == 0)
8608 : : {
8609 : 0 : if (!costing_p)
8610 : : {
8611 : 0 : if (mask_node)
8612 : : {
8613 : 0 : vect_get_slp_defs (mask_node, &vec_masks);
8614 : 0 : vec_mask = vec_masks[0];
8615 : : }
8616 : 0 : dataref_ptr
8617 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8618 : : aggr_type, NULL, offset, &dummy,
8619 : : gsi, &ptr_incr, false, bump);
8620 : : }
8621 : : }
8622 : 0 : else if (!costing_p)
8623 : : {
8624 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8625 : 0 : if (mask_node)
8626 : 0 : vec_mask = vec_masks[j];
8627 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
8628 : : stmt_info, bump);
8629 : : }
8630 : :
8631 : 0 : if (costing_p)
8632 : : {
8633 : 0 : n_adjacent_stores += group_size;
8634 : 0 : continue;
8635 : : }
8636 : :
8637 : : /* Get an array into which we can store the individual vectors. */
8638 : 0 : tree vec_array = create_vector_array (vectype, group_size);
8639 : :
8640 : : /* Invalidate the current contents of VEC_ARRAY. This should
8641 : : become an RTL clobber too, which prevents the vector registers
8642 : : from being upward-exposed. */
8643 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8644 : :
8645 : : /* Store the individual vectors into the array. */
8646 : 0 : for (i = 0; i < group_size; i++)
8647 : : {
8648 : 0 : slp_tree child;
8649 : 0 : if (i == 0 || !mask_node)
8650 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i];
8651 : : else
8652 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i + 1];
8653 : 0 : vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
8654 : 0 : write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
8655 : : i);
8656 : : }
8657 : :
8658 : 0 : tree final_mask = NULL;
8659 : 0 : tree final_len = NULL;
8660 : 0 : tree bias = NULL;
8661 : 0 : if (loop_masks)
8662 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
8663 : : ncopies, vectype, j);
8664 : 0 : if (vec_mask)
8665 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
8666 : : vec_mask, gsi);
8667 : :
8668 : 0 : if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
8669 : : {
8670 : 0 : if (loop_lens)
8671 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
8672 : : ncopies, vectype, j, 1);
8673 : : else
8674 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8675 : 0 : signed char biasval
8676 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8677 : 0 : bias = build_int_cst (intQI_type_node, biasval);
8678 : 0 : if (!final_mask)
8679 : : {
8680 : 0 : mask_vectype = truth_type_for (vectype);
8681 : 0 : final_mask = build_minus_one_cst (mask_vectype);
8682 : : }
8683 : : }
8684 : :
8685 : 0 : gcall *call;
8686 : 0 : if (final_len && final_mask)
8687 : : {
8688 : : /* Emit:
8689 : : MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8690 : : LEN, BIAS, VEC_ARRAY). */
8691 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8692 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
8693 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
8694 : : dataref_ptr, alias_ptr,
8695 : : final_mask, final_len, bias,
8696 : : vec_array);
8697 : : }
8698 : 0 : else if (final_mask)
8699 : : {
8700 : : /* Emit:
8701 : : MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8702 : : VEC_ARRAY). */
8703 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8704 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
8705 : 0 : call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
8706 : : dataref_ptr, alias_ptr,
8707 : : final_mask, vec_array);
8708 : : }
8709 : : else
8710 : : {
8711 : : /* Emit:
8712 : : MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
8713 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
8714 : 0 : call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
8715 : 0 : gimple_call_set_lhs (call, data_ref);
8716 : : }
8717 : 0 : gimple_call_set_nothrow (call, true);
8718 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
8719 : :
8720 : : /* Record that VEC_ARRAY is now dead. */
8721 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8722 : : }
8723 : :
8724 : 0 : if (costing_p)
8725 : : {
8726 : 0 : if (n_adjacent_stores > 0)
8727 : 0 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
8728 : : alignment_support_scheme, misalignment,
8729 : : &inside_cost, cost_vec);
8730 : 0 : if (dump_enabled_p ())
8731 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
8732 : : "vect_model_store_cost: inside_cost = %d, "
8733 : : "prologue_cost = %d .\n",
8734 : : inside_cost, prologue_cost);
8735 : : }
8736 : :
8737 : 0 : return true;
8738 : : }
8739 : :
8740 : 1291463 : if (mat_gather_scatter_p (memory_access_type))
8741 : : {
8742 : 2397 : gcc_assert (!grouped_store || ls.ls_type);
8743 : 2397 : if (ls.ls_type)
8744 : 0 : vectype = ls.ls_type;
8745 : 2397 : auto_vec<tree> vec_offsets;
8746 : 2397 : unsigned int inside_cost = 0, prologue_cost = 0;
8747 : 2397 : int num_stmts = vec_num;
8748 : 5196 : for (j = 0; j < num_stmts; j++)
8749 : : {
8750 : 2799 : gimple *new_stmt;
8751 : 2799 : if (j == 0)
8752 : : {
8753 : 2397 : if (costing_p && vls_type == VLS_STORE_INVARIANT)
8754 : 202 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8755 : : slp_node, 0, vect_prologue);
8756 : : else if (!costing_p)
8757 : : {
8758 : : /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
8759 : : DR_CHAIN is of size 1. */
8760 : 474 : gcc_assert (group_size == 1);
8761 : 474 : vect_get_slp_defs (op_node, gvec_oprnds[0]);
8762 : 474 : if (mask_node)
8763 : 70 : vect_get_slp_defs (mask_node, &vec_masks);
8764 : :
8765 : 474 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8766 : 474 : vect_get_gather_scatter_ops (loop, slp_node,
8767 : : &dataref_ptr, &vec_offsets);
8768 : : else
8769 : 0 : dataref_ptr
8770 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8771 : : aggr_type, NULL, offset,
8772 : : &dummy, gsi, &ptr_incr, false,
8773 : : bump);
8774 : : }
8775 : : }
8776 : 402 : else if (!costing_p)
8777 : : {
8778 : 38 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8779 : 38 : if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8780 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
8781 : : gsi, stmt_info, bump);
8782 : : }
8783 : :
8784 : 3513 : new_stmt = NULL;
8785 : 714 : if (!costing_p)
8786 : : {
8787 : 512 : vec_oprnd = (*gvec_oprnds[0])[j];
8788 : 512 : if (mask_node)
8789 : 90 : vec_mask = vec_masks[j];
8790 : : /* We should have catched mismatched types earlier. */
8791 : 512 : gcc_assert (ls.ls_type
8792 : : || useless_type_conversion_p
8793 : : (vectype, TREE_TYPE (vec_oprnd)));
8794 : : }
8795 : 512 : tree final_mask = NULL_TREE;
8796 : 3311 : tree final_len = NULL_TREE;
8797 : 3311 : tree bias = NULL_TREE;
8798 : 512 : if (!costing_p)
8799 : : {
8800 : 512 : if (loop_masks)
8801 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi,
8802 : : loop_masks, num_stmts,
8803 : : vectype, j);
8804 : 512 : if (vec_mask)
8805 : 90 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
8806 : : final_mask, vec_mask, gsi);
8807 : : }
8808 : :
8809 : 2799 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
8810 : 2799 : tree alias_align_ptr = build_int_cst (ref_type, align);
8811 : 2799 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
8812 : : {
8813 : 0 : if (costing_p)
8814 : : {
8815 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
8816 : 0 : inside_cost
8817 : 0 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
8818 : : slp_node, 0, vect_body);
8819 : 2799 : continue;
8820 : 0 : }
8821 : :
8822 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8823 : 0 : vec_offset = vec_offsets[j];
8824 : :
8825 : 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
8826 : :
8827 : 0 : if (ls.gs.ifn == IFN_MASK_LEN_SCATTER_STORE)
8828 : : {
8829 : 0 : if (loop_lens)
8830 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi,
8831 : : loop_lens, num_stmts,
8832 : : vectype, j, 1);
8833 : : else
8834 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8835 : :
8836 : 0 : signed char biasval
8837 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8838 : 0 : bias = build_int_cst (intQI_type_node, biasval);
8839 : 0 : if (!final_mask)
8840 : : {
8841 : 0 : mask_vectype = truth_type_for (vectype);
8842 : 0 : final_mask = build_minus_one_cst (mask_vectype);
8843 : : }
8844 : : }
8845 : :
8846 : 0 : if (ls.ls_type)
8847 : : {
8848 : 0 : gimple *conv_stmt
8849 : 0 : = gimple_build_assign (make_ssa_name (vectype),
8850 : : VIEW_CONVERT_EXPR,
8851 : : build1 (VIEW_CONVERT_EXPR, vectype,
8852 : : vec_oprnd));
8853 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, conv_stmt,
8854 : : gsi);
8855 : 0 : vec_oprnd = gimple_get_lhs (conv_stmt);
8856 : : }
8857 : :
8858 : 0 : gcall *call;
8859 : 0 : if (final_len && final_mask)
8860 : : {
8861 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
8862 : 0 : call = gimple_build_call_internal (
8863 : : IFN_MASK_LEN_SCATTER_STORE, 8, dataref_ptr,
8864 : : alias_align_ptr,
8865 : : vec_offset, scale, vec_oprnd, final_mask, final_len,
8866 : : bias);
8867 : : else
8868 : : /* Non-vector offset indicates that prefer to take
8869 : : MASK_LEN_STRIDED_STORE instead of the
8870 : : IFN_MASK_SCATTER_STORE with direct stride arg.
8871 : : Similar to the gather case we have checked the
8872 : : alignment for a scatter already and assume
8873 : : that the strided store has the same requirements. */
8874 : 0 : call = gimple_build_call_internal (
8875 : : IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
8876 : : vec_offset, vec_oprnd, final_mask, final_len, bias);
8877 : : }
8878 : 0 : else if (final_mask)
8879 : 0 : call = gimple_build_call_internal
8880 : 0 : (IFN_MASK_SCATTER_STORE, 6, dataref_ptr,
8881 : : alias_align_ptr,
8882 : : vec_offset, scale, vec_oprnd, final_mask);
8883 : : else
8884 : 0 : call = gimple_build_call_internal (IFN_SCATTER_STORE, 5,
8885 : : dataref_ptr,
8886 : : alias_align_ptr,
8887 : : vec_offset,
8888 : : scale, vec_oprnd);
8889 : 0 : gimple_call_set_nothrow (call, true);
8890 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
8891 : 0 : new_stmt = call;
8892 : : }
8893 : 2799 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
8894 : : {
8895 : : /* The builtin decls path for scatter is legacy, x86 only. */
8896 : 326 : gcc_assert (nunits.is_constant ()
8897 : : && (!final_mask
8898 : : || SCALAR_INT_MODE_P
8899 : : (TYPE_MODE (TREE_TYPE (final_mask)))));
8900 : 326 : if (costing_p)
8901 : : {
8902 : 187 : unsigned int cnunits = vect_nunits_for_cost (vectype);
8903 : 187 : inside_cost
8904 : 187 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
8905 : : slp_node, 0, vect_body);
8906 : 187 : continue;
8907 : 187 : }
8908 : :
8909 : 139 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
8910 : 139 : poly_uint64 offset_nunits
8911 : 139 : = TYPE_VECTOR_SUBPARTS (offset_vectype);
8912 : 139 : if (known_eq (nunits, offset_nunits))
8913 : : {
8914 : 63 : new_stmt = vect_build_one_scatter_store_call
8915 : 126 : (vinfo, stmt_info, slp_node, gsi,
8916 : 63 : ls.gs.decl, dataref_ptr, vec_offsets[j],
8917 : : vec_oprnd, final_mask);
8918 : 63 : vect_finish_stmt_generation (vinfo, stmt_info,
8919 : : new_stmt, gsi);
8920 : : }
8921 : 76 : else if (known_eq (nunits, offset_nunits * 2))
8922 : : {
8923 : : /* We have a offset vector with half the number of
8924 : : lanes but the builtins will store full vectype
8925 : : data from the lower lanes. */
8926 : 30 : new_stmt = vect_build_one_scatter_store_call
8927 : 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
8928 : 30 : dataref_ptr, vec_offsets[2 * j],
8929 : : vec_oprnd, final_mask);
8930 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
8931 : : new_stmt, gsi);
8932 : 30 : int count = nunits.to_constant ();
8933 : 30 : vec_perm_builder sel (count, count, 1);
8934 : 30 : sel.quick_grow (count);
8935 : 382 : for (int i = 0; i < count; ++i)
8936 : 352 : sel[i] = i | (count / 2);
8937 : 30 : vec_perm_indices indices (sel, 2, count);
8938 : 30 : tree perm_mask
8939 : 30 : = vect_gen_perm_mask_checked (vectype, indices);
8940 : 30 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
8941 : : vec_oprnd, vec_oprnd,
8942 : : perm_mask);
8943 : 30 : vec_oprnd = make_ssa_name (vectype);
8944 : 30 : gimple_set_lhs (new_stmt, vec_oprnd);
8945 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
8946 : : new_stmt, gsi);
8947 : 30 : if (final_mask)
8948 : : {
8949 : 20 : new_stmt = gimple_build_assign (NULL_TREE,
8950 : : VEC_UNPACK_HI_EXPR,
8951 : : final_mask);
8952 : 20 : final_mask = make_ssa_name
8953 : 20 : (truth_type_for (offset_vectype));
8954 : 20 : gimple_set_lhs (new_stmt, final_mask);
8955 : 20 : vect_finish_stmt_generation (vinfo, stmt_info,
8956 : : new_stmt, gsi);
8957 : : }
8958 : :
8959 : 30 : new_stmt = vect_build_one_scatter_store_call
8960 : 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
8961 : 30 : dataref_ptr, vec_offsets[2 * j + 1],
8962 : : vec_oprnd, final_mask);
8963 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
8964 : : new_stmt, gsi);
8965 : 30 : }
8966 : 46 : else if (known_eq (nunits * 2, offset_nunits))
8967 : : {
8968 : : /* We have a offset vector with double the number of
8969 : : lanes. Select the low/high part accordingly. */
8970 : 46 : vec_offset = vec_offsets[j / 2];
8971 : 46 : if (j & 1)
8972 : : {
8973 : 23 : int count = offset_nunits.to_constant ();
8974 : 23 : vec_perm_builder sel (count, count, 1);
8975 : 23 : sel.quick_grow (count);
8976 : 263 : for (int i = 0; i < count; ++i)
8977 : 240 : sel[i] = i | (count / 2);
8978 : 23 : vec_perm_indices indices (sel, 2, count);
8979 : 23 : tree perm_mask = vect_gen_perm_mask_checked
8980 : 23 : (TREE_TYPE (vec_offset), indices);
8981 : 23 : new_stmt = gimple_build_assign (NULL_TREE,
8982 : : VEC_PERM_EXPR,
8983 : : vec_offset,
8984 : : vec_offset,
8985 : : perm_mask);
8986 : 23 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
8987 : 23 : gimple_set_lhs (new_stmt, vec_offset);
8988 : 23 : vect_finish_stmt_generation (vinfo, stmt_info,
8989 : : new_stmt, gsi);
8990 : 23 : }
8991 : :
8992 : 46 : new_stmt = vect_build_one_scatter_store_call
8993 : 46 : (vinfo, stmt_info, slp_node, gsi,
8994 : : ls.gs.decl, dataref_ptr, vec_offset,
8995 : : vec_oprnd, final_mask);
8996 : 46 : vect_finish_stmt_generation (vinfo, stmt_info,
8997 : : new_stmt, gsi);
8998 : : }
8999 : : else
9000 : 0 : gcc_unreachable ();
9001 : : }
9002 : : else
9003 : : {
9004 : : /* Emulated scatter. */
9005 : 2473 : gcc_assert (!final_mask);
9006 : 2473 : if (costing_p)
9007 : : {
9008 : 2100 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9009 : : /* For emulated scatter N offset vector element extracts
9010 : : (we assume the scalar scaling and ptr + offset add is
9011 : : consumed by the load). */
9012 : 2100 : inside_cost
9013 : 2100 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9014 : : slp_node, 0, vect_body);
9015 : : /* N scalar stores plus extracting the elements. */
9016 : 2100 : inside_cost
9017 : 2100 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9018 : : slp_node, 0, vect_body);
9019 : 2100 : inside_cost
9020 : 2100 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9021 : : slp_node, 0, vect_body);
9022 : 2100 : continue;
9023 : 2100 : }
9024 : :
9025 : 373 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
9026 : 373 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
9027 : 373 : unsigned HOST_WIDE_INT const_offset_nunits
9028 : 373 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
9029 : 373 : vec<constructor_elt, va_gc> *ctor_elts;
9030 : 373 : vec_alloc (ctor_elts, const_nunits);
9031 : 373 : gimple_seq stmts = NULL;
9032 : 373 : tree elt_type = TREE_TYPE (vectype);
9033 : 373 : unsigned HOST_WIDE_INT elt_size
9034 : 373 : = tree_to_uhwi (TYPE_SIZE (elt_type));
9035 : : /* We support offset vectors with more elements
9036 : : than the data vector for now. */
9037 : 373 : unsigned HOST_WIDE_INT factor
9038 : : = const_offset_nunits / const_nunits;
9039 : 373 : vec_offset = vec_offsets[j / factor];
9040 : 373 : unsigned elt_offset
9041 : 373 : = (j % factor) * const_nunits;
9042 : 373 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
9043 : 373 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
9044 : 373 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
9045 : 1519 : for (unsigned k = 0; k < const_nunits; ++k)
9046 : : {
9047 : : /* Compute the offsetted pointer. */
9048 : 1146 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
9049 : : bitsize_int (k + elt_offset));
9050 : 1146 : tree idx
9051 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
9052 : 1146 : vec_offset, TYPE_SIZE (idx_type), boff);
9053 : 1146 : idx = gimple_convert (&stmts, sizetype, idx);
9054 : 1146 : idx = gimple_build (&stmts, MULT_EXPR, sizetype,
9055 : : idx, scale);
9056 : 1146 : tree ptr
9057 : 1146 : = gimple_build (&stmts, PLUS_EXPR,
9058 : 1146 : TREE_TYPE (dataref_ptr),
9059 : : dataref_ptr, idx);
9060 : 1146 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
9061 : : /* Extract the element to be stored. */
9062 : 1146 : tree elt
9063 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF,
9064 : 1146 : TREE_TYPE (vectype),
9065 : 1146 : vec_oprnd, TYPE_SIZE (elt_type),
9066 : 1146 : bitsize_int (k * elt_size));
9067 : 1146 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9068 : 1146 : stmts = NULL;
9069 : 1146 : tree ref
9070 : 1146 : = build2 (MEM_REF, ltype, ptr,
9071 : : build_int_cst (ref_type, 0));
9072 : 1146 : new_stmt = gimple_build_assign (ref, elt);
9073 : 1146 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9074 : : }
9075 : :
9076 : 373 : slp_node->push_vec_def (new_stmt);
9077 : : }
9078 : : }
9079 : :
9080 : 2397 : if (costing_p && dump_enabled_p ())
9081 : 68 : dump_printf_loc (MSG_NOTE, vect_location,
9082 : : "vect_model_store_cost: inside_cost = %d, "
9083 : : "prologue_cost = %d .\n",
9084 : : inside_cost, prologue_cost);
9085 : :
9086 : 2397 : return true;
9087 : 2397 : }
9088 : :
9089 : 1289066 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
9090 : : || memory_access_type == VMAT_CONTIGUOUS_DOWN
9091 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
9092 : :
9093 : 1289066 : unsigned inside_cost = 0, prologue_cost = 0;
9094 : : /* For costing some adjacent vector stores, we'd like to cost with
9095 : : the total number of them once instead of cost each one by one. */
9096 : 1289066 : unsigned int n_adjacent_stores = 0;
9097 : 1289066 : auto_vec<tree> result_chain (group_size);
9098 : 1289066 : auto_vec<tree, 1> vec_oprnds;
9099 : 1289066 : gimple *new_stmt;
9100 : 1289066 : if (!costing_p)
9101 : : {
9102 : : /* Get vectorized arguments for SLP_NODE. */
9103 : 539574 : vect_get_slp_defs (op_node, &vec_oprnds);
9104 : 539574 : vec_oprnd = vec_oprnds[0];
9105 : 539574 : if (mask_node)
9106 : : {
9107 : 466 : vect_get_slp_defs (mask_node, &vec_masks);
9108 : 466 : vec_mask = vec_masks[0];
9109 : : }
9110 : : }
9111 : :
9112 : : /* We should have catched mismatched types earlier. */
9113 : 539574 : gcc_assert (costing_p
9114 : : || useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
9115 : 1289066 : bool simd_lane_access_p
9116 : 1289066 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
9117 : 1289066 : if (!costing_p
9118 : 1289066 : && simd_lane_access_p
9119 : 4366 : && !loop_masks
9120 : 4366 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
9121 : 4366 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
9122 : 4366 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
9123 : 4366 : && integer_zerop (DR_INIT (first_dr_info->dr))
9124 : 1293432 : && alias_sets_conflict_p (get_alias_set (aggr_type),
9125 : 4366 : get_alias_set (TREE_TYPE (ref_type))))
9126 : : {
9127 : 4358 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
9128 : 4358 : dataref_offset = build_int_cst (ref_type, 0);
9129 : : }
9130 : 1284708 : else if (!costing_p)
9131 : 1070424 : dataref_ptr = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
9132 : : simd_lane_access_p ? loop : NULL,
9133 : : offset, &dummy, gsi, &ptr_incr,
9134 : : simd_lane_access_p, bump);
9135 : :
9136 : 1289066 : new_stmt = NULL;
9137 : 1289066 : gcc_assert (!grouped_store);
9138 : 2866115 : for (i = 0; i < vec_num; i++)
9139 : : {
9140 : 1577049 : if (!costing_p)
9141 : 668259 : vec_oprnd = vec_oprnds[i];
9142 : :
9143 : 1577049 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
9144 : : {
9145 : 3138 : if (costing_p)
9146 : 2009 : inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
9147 : : slp_node, 0, vect_body);
9148 : : else
9149 : : {
9150 : 1129 : tree perm_mask = perm_mask_for_reverse (vectype);
9151 : 1129 : tree new_temp = make_ssa_name (vectype);
9152 : :
9153 : : /* Generate the permute statement. */
9154 : 1129 : gimple *perm_stmt
9155 : 1129 : = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
9156 : : vec_oprnd, perm_mask);
9157 : 1129 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9158 : :
9159 : 1129 : perm_stmt = SSA_NAME_DEF_STMT (new_temp);
9160 : 1577049 : vec_oprnd = new_temp;
9161 : : }
9162 : : }
9163 : :
9164 : 1577049 : if (costing_p)
9165 : : {
9166 : 908790 : n_adjacent_stores++;
9167 : 908790 : continue;
9168 : : }
9169 : :
9170 : 668259 : tree final_mask = NULL_TREE;
9171 : 668259 : tree final_len = NULL_TREE;
9172 : 668259 : tree bias = NULL_TREE;
9173 : 668259 : if (loop_masks)
9174 : 76 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9175 : : vec_num, vectype, i);
9176 : 668259 : if (vec_mask)
9177 : 647 : vec_mask = vec_masks[i];
9178 : 647 : if (vec_mask)
9179 : 647 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9180 : : vec_mask, gsi);
9181 : :
9182 : 668259 : if (i > 0)
9183 : : /* Bump the vector pointer. */
9184 : 128685 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9185 : : stmt_info, bump);
9186 : :
9187 : 668259 : unsigned misalign;
9188 : 668259 : unsigned HOST_WIDE_INT align;
9189 : 668259 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
9190 : 668259 : if (alignment_support_scheme == dr_aligned)
9191 : : misalign = 0;
9192 : 307315 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
9193 : : {
9194 : 157384 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
9195 : 157384 : misalign = 0;
9196 : : }
9197 : : else
9198 : 149931 : misalign = misalignment;
9199 : 668259 : if (dataref_offset == NULL_TREE
9200 : 662887 : && TREE_CODE (dataref_ptr) == SSA_NAME)
9201 : 175029 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
9202 : 668259 : align = least_bit_hwi (misalign | align);
9203 : :
9204 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
9205 : 668259 : machine_mode vmode = TYPE_MODE (vectype);
9206 : 668259 : machine_mode new_vmode = vmode;
9207 : 668259 : internal_fn partial_ifn = IFN_LAST;
9208 : 668259 : if (loop_lens)
9209 : : {
9210 : 0 : opt_machine_mode new_ovmode
9211 : 0 : = get_len_load_store_mode (vmode, false, &partial_ifn);
9212 : 0 : new_vmode = new_ovmode.require ();
9213 : 0 : unsigned factor
9214 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
9215 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
9216 : : vec_num, vectype, i, factor);
9217 : : }
9218 : 668259 : else if (final_mask)
9219 : : {
9220 : 659 : if (!can_vec_mask_load_store_p (vmode,
9221 : 659 : TYPE_MODE (TREE_TYPE (final_mask)),
9222 : : false, &partial_ifn))
9223 : 0 : gcc_unreachable ();
9224 : : }
9225 : :
9226 : 668259 : if (partial_ifn == IFN_MASK_LEN_STORE)
9227 : : {
9228 : 0 : if (!final_len)
9229 : : {
9230 : : /* Pass VF value to 'len' argument of
9231 : : MASK_LEN_STORE if LOOP_LENS is invalid. */
9232 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9233 : : }
9234 : 0 : if (!final_mask)
9235 : : {
9236 : : /* Pass all ones value to 'mask' argument of
9237 : : MASK_LEN_STORE if final_mask is invalid. */
9238 : 0 : mask_vectype = truth_type_for (vectype);
9239 : 0 : final_mask = build_minus_one_cst (mask_vectype);
9240 : : }
9241 : : }
9242 : 668259 : if (final_len)
9243 : : {
9244 : 0 : signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9245 : 0 : bias = build_int_cst (intQI_type_node, biasval);
9246 : : }
9247 : :
9248 : : /* Arguments are ready. Create the new vector stmt. */
9249 : 668259 : if (final_len)
9250 : : {
9251 : 0 : gcall *call;
9252 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9253 : : /* Need conversion if it's wrapped with VnQI. */
9254 : 0 : if (vmode != new_vmode)
9255 : : {
9256 : 0 : tree new_vtype
9257 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
9258 : : new_vmode);
9259 : 0 : tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
9260 : 0 : vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
9261 : 0 : gassign *new_stmt
9262 : 0 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
9263 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9264 : 0 : vec_oprnd = var;
9265 : : }
9266 : :
9267 : 0 : if (partial_ifn == IFN_MASK_LEN_STORE)
9268 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
9269 : : dataref_ptr, ptr, final_mask,
9270 : : final_len, bias, vec_oprnd);
9271 : : else
9272 : 0 : call = gimple_build_call_internal (IFN_LEN_STORE, 5,
9273 : : dataref_ptr, ptr, final_len,
9274 : : bias, vec_oprnd);
9275 : 0 : gimple_call_set_nothrow (call, true);
9276 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9277 : 0 : new_stmt = call;
9278 : : }
9279 : 668259 : else if (final_mask)
9280 : : {
9281 : 659 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9282 : 659 : gcall *call
9283 : 659 : = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
9284 : : ptr, final_mask, vec_oprnd);
9285 : 659 : gimple_call_set_nothrow (call, true);
9286 : 659 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9287 : 659 : new_stmt = call;
9288 : : }
9289 : : else
9290 : : {
9291 : 667600 : data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr,
9292 : : dataref_offset ? dataref_offset
9293 : : : build_int_cst (ref_type, 0));
9294 : 667600 : if (alignment_support_scheme == dr_aligned
9295 : 667600 : && align >= TYPE_ALIGN_UNIT (vectype))
9296 : : ;
9297 : : else
9298 : 306826 : TREE_TYPE (data_ref)
9299 : 613652 : = build_aligned_type (TREE_TYPE (data_ref),
9300 : : align * BITS_PER_UNIT);
9301 : 667600 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
9302 : 667600 : new_stmt = gimple_build_assign (data_ref, vec_oprnd);
9303 : 667600 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9304 : : }
9305 : : }
9306 : :
9307 : 1289066 : if (costing_p)
9308 : : {
9309 : 749492 : if (n_adjacent_stores > 0)
9310 : 749492 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9311 : : alignment_support_scheme, misalignment,
9312 : : &inside_cost, cost_vec);
9313 : :
9314 : : /* When vectorizing a store into the function result assign
9315 : : a penalty if the function returns in a multi-register location.
9316 : : In this case we assume we'll end up with having to spill the
9317 : : vector result and do piecewise loads as a conservative estimate. */
9318 : 749492 : tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
9319 : 749492 : if (base
9320 : 749492 : && (TREE_CODE (base) == RESULT_DECL
9321 : 698810 : || (DECL_P (base) && cfun_returns (base)))
9322 : 812015 : && !aggregate_value_p (base, cfun->decl))
9323 : : {
9324 : 11290 : rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
9325 : : /* ??? Handle PARALLEL in some way. */
9326 : 11290 : if (REG_P (reg))
9327 : : {
9328 : 11091 : int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
9329 : : /* Assume that a single reg-reg move is possible and cheap,
9330 : : do not account for vector to gp register move cost. */
9331 : 11091 : if (nregs > 1)
9332 : : {
9333 : : /* Spill. */
9334 : 10219 : prologue_cost
9335 : 10219 : += record_stmt_cost (cost_vec, 1, vector_store,
9336 : : slp_node, 0, vect_epilogue);
9337 : : /* Loads. */
9338 : 10219 : prologue_cost
9339 : 10219 : += record_stmt_cost (cost_vec, nregs, scalar_load,
9340 : : slp_node, 0, vect_epilogue);
9341 : : }
9342 : : }
9343 : : }
9344 : 749492 : if (dump_enabled_p ())
9345 : 13032 : dump_printf_loc (MSG_NOTE, vect_location,
9346 : : "vect_model_store_cost: inside_cost = %d, "
9347 : : "prologue_cost = %d .\n",
9348 : : inside_cost, prologue_cost);
9349 : : }
9350 : :
9351 : 1289066 : return true;
9352 : 3902209 : }
9353 : :
9354 : : /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
9355 : : VECTOR_CST mask. No checks are made that the target platform supports the
9356 : : mask, so callers may wish to test can_vec_perm_const_p separately, or use
9357 : : vect_gen_perm_mask_checked. */
9358 : :
9359 : : tree
9360 : 59587 : vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
9361 : : {
9362 : 59587 : tree mask_type;
9363 : :
9364 : 59587 : poly_uint64 nunits = sel.length ();
9365 : 59587 : gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
9366 : :
9367 : 59587 : mask_type = build_vector_type (ssizetype, nunits);
9368 : 59587 : return vec_perm_indices_to_tree (mask_type, sel);
9369 : : }
9370 : :
9371 : : /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
9372 : : i.e. that the target supports the pattern _for arbitrary input vectors_. */
9373 : :
9374 : : tree
9375 : 57029 : vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
9376 : : {
9377 : 57029 : machine_mode vmode = TYPE_MODE (vectype);
9378 : 57029 : gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
9379 : 57029 : return vect_gen_perm_mask_any (vectype, sel);
9380 : : }
9381 : :
9382 : : /* Given a vector variable X and Y, that was generated for the scalar
9383 : : STMT_INFO, generate instructions to permute the vector elements of X and Y
9384 : : using permutation mask MASK_VEC, insert them at *GSI and return the
9385 : : permuted vector variable. */
9386 : :
9387 : : static tree
9388 : 1416 : permute_vec_elements (vec_info *vinfo,
9389 : : tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
9390 : : gimple_stmt_iterator *gsi)
9391 : : {
9392 : 1416 : tree vectype = TREE_TYPE (x);
9393 : 1416 : tree perm_dest, data_ref;
9394 : 1416 : gimple *perm_stmt;
9395 : :
9396 : 1416 : tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
9397 : 1416 : if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
9398 : 1416 : perm_dest = vect_create_destination_var (scalar_dest, vectype);
9399 : : else
9400 : 0 : perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
9401 : 1416 : data_ref = make_ssa_name (perm_dest);
9402 : :
9403 : : /* Generate the permute statement. */
9404 : 1416 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
9405 : 1416 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9406 : :
9407 : 1416 : return data_ref;
9408 : : }
9409 : :
9410 : : /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
9411 : : inserting them on the loops preheader edge. Returns true if we
9412 : : were successful in doing so (and thus STMT_INFO can be moved then),
9413 : : otherwise returns false. HOIST_P indicates if we want to hoist the
9414 : : definitions of all SSA uses, it would be false when we are costing. */
9415 : :
9416 : : static bool
9417 : 3744 : hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
9418 : : {
9419 : 3744 : ssa_op_iter i;
9420 : 3744 : use_operand_p use_p;
9421 : 3744 : auto_vec<use_operand_p, 8> to_hoist;
9422 : :
9423 : 7120 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
9424 : : {
9425 : 3384 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9426 : 3384 : if (!gimple_nop_p (def_stmt)
9427 : 3384 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
9428 : : {
9429 : : /* Make sure we don't need to recurse. While we could do
9430 : : so in simple cases when there are more complex use webs
9431 : : we don't have an easy way to preserve stmt order to fulfil
9432 : : dependencies within them. */
9433 : 47 : tree op2;
9434 : 47 : ssa_op_iter i2;
9435 : 47 : if (gimple_code (def_stmt) == GIMPLE_PHI
9436 : 47 : || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
9437 : : == NULL_DEF_OPERAND_P))
9438 : 8 : return false;
9439 : 94 : FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
9440 : : {
9441 : 55 : gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
9442 : 55 : if (!gimple_nop_p (def_stmt2)
9443 : 55 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
9444 : : return false;
9445 : : }
9446 : 39 : to_hoist.safe_push (use_p);
9447 : : }
9448 : : }
9449 : :
9450 : 7472 : if (to_hoist.is_empty ())
9451 : : return true;
9452 : :
9453 : 39 : if (!hoist_p)
9454 : : return true;
9455 : :
9456 : : /* Instead of moving defs we copy them so we can zero their UID to not
9457 : : confuse dominance queries in the preheader. */
9458 : 9 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9459 : 36 : for (use_operand_p use_p : to_hoist)
9460 : : {
9461 : 9 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9462 : 9 : gimple *copy = gimple_copy (def_stmt);
9463 : 9 : gimple_set_uid (copy, 0);
9464 : 9 : def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
9465 : 9 : tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
9466 : 9 : update_stmt (copy);
9467 : 9 : def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
9468 : 9 : SET_DEF (def_p, new_def);
9469 : 9 : SET_USE (use_p, new_def);
9470 : 9 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
9471 : : }
9472 : :
9473 : : return true;
9474 : 3744 : }
9475 : :
9476 : : /* vectorizable_load.
9477 : :
9478 : : Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
9479 : : that can be vectorized.
9480 : : If COST_VEC is passed, calculate costs but don't change anything,
9481 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
9482 : : it, and insert it at GSI.
9483 : : Return true if STMT_INFO is vectorizable in this way. */
9484 : :
9485 : : static bool
9486 : 1933093 : vectorizable_load (vec_info *vinfo,
9487 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
9488 : : slp_tree slp_node,
9489 : : stmt_vector_for_cost *cost_vec)
9490 : : {
9491 : 1933093 : tree scalar_dest;
9492 : 1933093 : tree vec_dest = NULL;
9493 : 1933093 : tree data_ref = NULL;
9494 : 1933093 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
9495 : 1933093 : class loop *loop = NULL;
9496 : 1933093 : class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
9497 : 1933093 : bool nested_in_vect_loop = false;
9498 : 1933093 : tree elem_type;
9499 : : /* Avoid false positive uninitialized warning, see PR110652. */
9500 : 1933093 : tree new_temp = NULL_TREE;
9501 : 1933093 : machine_mode mode;
9502 : 1933093 : tree dummy;
9503 : 1933093 : tree dataref_ptr = NULL_TREE;
9504 : 1933093 : tree dataref_offset = NULL_TREE;
9505 : 1933093 : gimple *ptr_incr = NULL;
9506 : 1933093 : int i, j;
9507 : 1933093 : unsigned int group_size;
9508 : 1933093 : poly_uint64 group_gap_adj;
9509 : 1933093 : tree msq = NULL_TREE, lsq;
9510 : 1933093 : tree realignment_token = NULL_TREE;
9511 : 1933093 : gphi *phi = NULL;
9512 : 1933093 : vec<tree> dr_chain = vNULL;
9513 : 1933093 : bool grouped_load = false;
9514 : 1933093 : stmt_vec_info first_stmt_info;
9515 : 1933093 : stmt_vec_info first_stmt_info_for_drptr = NULL;
9516 : 1933093 : bool compute_in_loop = false;
9517 : 1933093 : class loop *at_loop;
9518 : 1933093 : int vec_num;
9519 : 1933093 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
9520 : 1933093 : poly_uint64 vf;
9521 : 1933093 : tree aggr_type;
9522 : 1933093 : tree ref_type;
9523 : 1933093 : enum vect_def_type mask_dt = vect_unknown_def_type;
9524 : 1933093 : enum vect_def_type els_dt = vect_unknown_def_type;
9525 : :
9526 : 1933093 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
9527 : : return false;
9528 : :
9529 : 1933093 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
9530 : 175983 : && cost_vec)
9531 : : return false;
9532 : :
9533 : 1757110 : if (!STMT_VINFO_DATA_REF (stmt_info))
9534 : : return false;
9535 : :
9536 : 1405837 : tree mask_vectype = NULL_TREE;
9537 : 1405837 : tree els = NULL_TREE; tree els_vectype = NULL_TREE;
9538 : :
9539 : 1405837 : int mask_index = -1;
9540 : 1405837 : int els_index = -1;
9541 : 1405837 : slp_tree mask_node = NULL;
9542 : 1405837 : slp_tree els_op = NULL;
9543 : 1405837 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
9544 : : {
9545 : 1402596 : scalar_dest = gimple_assign_lhs (assign);
9546 : 1402596 : if (TREE_CODE (scalar_dest) != SSA_NAME)
9547 : : return false;
9548 : :
9549 : 625594 : tree_code code = gimple_assign_rhs_code (assign);
9550 : 625594 : if (code != ARRAY_REF
9551 : 625594 : && code != BIT_FIELD_REF
9552 : 625594 : && code != INDIRECT_REF
9553 : 426353 : && code != COMPONENT_REF
9554 : 426353 : && code != IMAGPART_EXPR
9555 : 286283 : && code != REALPART_EXPR
9556 : 286283 : && code != MEM_REF
9557 : 207 : && TREE_CODE_CLASS (code) != tcc_declaration)
9558 : : return false;
9559 : : }
9560 : : else
9561 : : {
9562 : 1308783 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
9563 : 3241 : if (!call || !gimple_call_internal_p (call))
9564 : : return false;
9565 : :
9566 : 3241 : internal_fn ifn = gimple_call_internal_fn (call);
9567 : 3241 : if (!internal_load_fn_p (ifn))
9568 : : return false;
9569 : :
9570 : 2334 : scalar_dest = gimple_call_lhs (call);
9571 : 2334 : if (!scalar_dest)
9572 : : return false;
9573 : :
9574 : 2334 : mask_index = internal_fn_mask_index (ifn);
9575 : 2334 : if (mask_index >= 0)
9576 : 2334 : mask_index = vect_slp_child_index_for_operand
9577 : 2334 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9578 : 2334 : if (mask_index >= 0
9579 : 2334 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
9580 : : &mask_node, &mask_dt, &mask_vectype))
9581 : : return false;
9582 : :
9583 : 2334 : els_index = internal_fn_else_index (ifn);
9584 : 2334 : if (els_index >= 0)
9585 : 2334 : els_index = vect_slp_child_index_for_operand
9586 : 2334 : (call, els_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9587 : 2334 : if (els_index >= 0
9588 : 2334 : && !vect_is_simple_use (vinfo, slp_node, els_index,
9589 : : &els, &els_op, &els_dt, &els_vectype))
9590 : : return false;
9591 : : }
9592 : :
9593 : 627861 : tree vectype = SLP_TREE_VECTYPE (slp_node);
9594 : 627861 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9595 : :
9596 : 627861 : if (loop_vinfo)
9597 : : {
9598 : 413789 : loop = LOOP_VINFO_LOOP (loop_vinfo);
9599 : 413789 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
9600 : 413789 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
9601 : : }
9602 : : else
9603 : : vf = 1;
9604 : :
9605 : 627861 : vec_num = vect_get_num_copies (vinfo, slp_node);
9606 : :
9607 : : /* FORNOW. This restriction should be relaxed. */
9608 : 627861 : if (nested_in_vect_loop && vec_num > 1)
9609 : : {
9610 : 298 : if (dump_enabled_p ())
9611 : 66 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9612 : : "multiple types in nested loop.\n");
9613 : 298 : return false;
9614 : : }
9615 : :
9616 : 627563 : elem_type = TREE_TYPE (vectype);
9617 : 627563 : mode = TYPE_MODE (vectype);
9618 : :
9619 : : /* FORNOW. In some cases can vectorize even if data-type not supported
9620 : : (e.g. - data copies). */
9621 : 627563 : if (!can_implement_p (mov_optab, mode))
9622 : : {
9623 : 0 : if (dump_enabled_p ())
9624 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9625 : : "Aligned load, but unsupported type.\n");
9626 : 0 : return false;
9627 : : }
9628 : :
9629 : : /* Check if the load is a part of an interleaving chain. */
9630 : 627563 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
9631 : : {
9632 : 301854 : grouped_load = true;
9633 : : /* FORNOW */
9634 : 301854 : gcc_assert (!nested_in_vect_loop);
9635 : 301854 : gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9636 : :
9637 : 301854 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
9638 : 301854 : group_size = DR_GROUP_SIZE (first_stmt_info);
9639 : :
9640 : : /* Invalidate assumptions made by dependence analysis when vectorization
9641 : : on the unrolled body effectively re-orders stmts. */
9642 : 301854 : if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
9643 : 301854 : && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
9644 : : STMT_VINFO_MIN_NEG_DIST (stmt_info)))
9645 : : {
9646 : 12 : if (dump_enabled_p ())
9647 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9648 : : "cannot perform implicit CSE when performing "
9649 : : "group loads with negative dependence distance\n");
9650 : 12 : return false;
9651 : : }
9652 : : }
9653 : : else
9654 : : group_size = 1;
9655 : :
9656 : 627551 : vect_load_store_data _ls_data{};
9657 : 627551 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
9658 : 627551 : if (cost_vec
9659 : 627551 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
9660 : : VLS_LOAD, &ls))
9661 : : return false;
9662 : : /* Temporary aliases to analysis data, should not be modified through
9663 : : these. */
9664 : 538030 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
9665 : 538030 : const dr_alignment_support alignment_support_scheme
9666 : : = ls.alignment_support_scheme;
9667 : 538030 : const int misalignment = ls.misalignment;
9668 : 538030 : const poly_int64 poffset = ls.poffset;
9669 : 538030 : const vec<int> &elsvals = ls.elsvals;
9670 : :
9671 : 538030 : int maskload_elsval = 0;
9672 : 538030 : bool need_zeroing = false;
9673 : :
9674 : : /* We might need to explicitly zero inactive elements if there are
9675 : : padding bits in the type that might leak otherwise.
9676 : : Refer to PR115336. */
9677 : 538030 : tree scalar_type = TREE_TYPE (scalar_dest);
9678 : 538030 : bool type_mode_padding_p
9679 : 1076060 : = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
9680 : :
9681 : 538030 : if (slp_node->ldst_lanes
9682 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
9683 : : {
9684 : 0 : if (dump_enabled_p ())
9685 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9686 : : "discovered load-lane but cannot use it.\n");
9687 : 0 : return false;
9688 : : }
9689 : :
9690 : 538030 : if (mask_node)
9691 : : {
9692 : 2214 : if (memory_access_type == VMAT_CONTIGUOUS)
9693 : : {
9694 : 1432 : machine_mode vec_mode = TYPE_MODE (vectype);
9695 : 375 : if (!VECTOR_MODE_P (vec_mode)
9696 : 2864 : || !can_vec_mask_load_store_p (vec_mode,
9697 : 1432 : TYPE_MODE (mask_vectype),
9698 : : true, NULL, &ls.elsvals))
9699 : 61 : return false;
9700 : : }
9701 : 782 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
9702 : 782 : && !mat_gather_scatter_p (memory_access_type))
9703 : : {
9704 : 62 : if (dump_enabled_p ())
9705 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9706 : : "unsupported access type for masked load.\n");
9707 : 62 : return false;
9708 : : }
9709 : 720 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
9710 : : {
9711 : 476 : if (dump_enabled_p ())
9712 : 26 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9713 : : "unsupported masked emulated gather.\n");
9714 : 476 : return false;
9715 : : }
9716 : : else if (memory_access_type == VMAT_ELEMENTWISE
9717 : : || memory_access_type == VMAT_STRIDED_SLP)
9718 : : {
9719 : : if (dump_enabled_p ())
9720 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9721 : : "unsupported masked strided access.\n");
9722 : : return false;
9723 : : }
9724 : : }
9725 : :
9726 : 537431 : bool costing_p = cost_vec;
9727 : :
9728 : 537431 : if (costing_p) /* transformation not required. */
9729 : : {
9730 : 373800 : if (mask_node
9731 : 373800 : && !vect_maybe_update_slp_op_vectype (mask_node,
9732 : : mask_vectype))
9733 : : {
9734 : 0 : if (dump_enabled_p ())
9735 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9736 : : "incompatible vector types for invariants\n");
9737 : 0 : return false;
9738 : : }
9739 : :
9740 : 373800 : if (loop_vinfo
9741 : 249926 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
9742 : 90 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
9743 : : VLS_LOAD, group_size, &ls,
9744 : : mask_node, &ls.elsvals);
9745 : :
9746 : 373800 : if (dump_enabled_p ()
9747 : 23710 : && memory_access_type != VMAT_ELEMENTWISE
9748 : 23602 : && !mat_gather_scatter_p (memory_access_type)
9749 : 23380 : && memory_access_type != VMAT_STRIDED_SLP
9750 : 23380 : && memory_access_type != VMAT_INVARIANT
9751 : 396354 : && alignment_support_scheme != dr_aligned)
9752 : 8933 : dump_printf_loc (MSG_NOTE, vect_location,
9753 : : "Vectorizing an unaligned access.\n");
9754 : :
9755 : 373800 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
9756 : 0 : vinfo->any_known_not_updated_vssa = true;
9757 : :
9758 : 373800 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
9759 : 373800 : slp_node->data = new vect_load_store_data (std::move (ls));
9760 : : }
9761 : :
9762 : : /* If the type needs padding we must zero inactive elements.
9763 : : Check if we can do that with a VEC_COND_EXPR and store the
9764 : : elsval we choose in MASKLOAD_ELSVAL. */
9765 : 537431 : if (elsvals.length ()
9766 : 528 : && type_mode_padding_p
9767 : 0 : && !elsvals.contains (MASK_LOAD_ELSE_ZERO)
9768 : 528 : && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
9769 : : {
9770 : 0 : if (dump_enabled_p ())
9771 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9772 : : "cannot zero inactive elements.\n");
9773 : 0 : return false;
9774 : : }
9775 : :
9776 : : /* For now just use the first available else value.
9777 : : get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
9778 : : select it here if it is supported. */
9779 : 537431 : if (elsvals.length ())
9780 : 528 : maskload_elsval = *elsvals.begin ();
9781 : :
9782 : 537431 : if (dump_enabled_p () && !costing_p)
9783 : 15888 : dump_printf_loc (MSG_NOTE, vect_location, "transform load.\n");
9784 : :
9785 : : /* Transform. */
9786 : :
9787 : 537431 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
9788 : 537431 : ensure_base_align (dr_info);
9789 : :
9790 : 537431 : if (memory_access_type == VMAT_INVARIANT)
9791 : : {
9792 : 3743 : gcc_assert (!grouped_load && !mask_node && !bb_vinfo);
9793 : : /* If we have versioned for aliasing or the loop doesn't
9794 : : have any data dependencies that would preclude this,
9795 : : then we are sure this is a loop invariant load and
9796 : : thus we can insert it on the preheader edge.
9797 : : TODO: hoist_defs_of_uses should ideally be computed
9798 : : once at analysis time, remembered and used in the
9799 : : transform time. */
9800 : 3743 : bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
9801 : 3547 : && !nested_in_vect_loop
9802 : 6874 : && hoist_defs_of_uses (stmt_info->stmt, loop, false));
9803 : 3743 : if (costing_p)
9804 : : {
9805 : 432 : enum vect_cost_model_location cost_loc
9806 : 2942 : = hoist_p ? vect_prologue : vect_body;
9807 : 2942 : unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
9808 : : slp_node, 0, cost_loc);
9809 : 2942 : cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
9810 : : slp_node, 0, cost_loc);
9811 : 2942 : unsigned int prologue_cost = hoist_p ? cost : 0;
9812 : 432 : unsigned int inside_cost = hoist_p ? 0 : cost;
9813 : 2942 : if (dump_enabled_p ())
9814 : 439 : dump_printf_loc (MSG_NOTE, vect_location,
9815 : : "vect_model_load_cost: inside_cost = %d, "
9816 : : "prologue_cost = %d .\n",
9817 : : inside_cost, prologue_cost);
9818 : 2942 : return true;
9819 : : }
9820 : 801 : if (hoist_p)
9821 : : {
9822 : 613 : gassign *stmt = as_a <gassign *> (stmt_info->stmt);
9823 : 613 : if (dump_enabled_p ())
9824 : 206 : dump_printf_loc (MSG_NOTE, vect_location,
9825 : : "hoisting out of the vectorized loop: %G",
9826 : : (gimple *) stmt);
9827 : 613 : scalar_dest = copy_ssa_name (scalar_dest);
9828 : 613 : tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
9829 : 613 : edge pe = loop_preheader_edge (loop);
9830 : 613 : gphi *vphi = get_virtual_phi (loop->header);
9831 : 613 : tree vuse;
9832 : 613 : if (vphi)
9833 : 607 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
9834 : : else
9835 : 6 : vuse = gimple_vuse (gsi_stmt (*gsi));
9836 : 613 : gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
9837 : 613 : gimple_set_vuse (new_stmt, vuse);
9838 : 613 : gsi_insert_on_edge_immediate (pe, new_stmt);
9839 : 613 : hoist_defs_of_uses (new_stmt, loop, true);
9840 : : }
9841 : : /* These copies are all equivalent. */
9842 : 801 : if (hoist_p)
9843 : 613 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
9844 : : vectype, NULL);
9845 : : else
9846 : : {
9847 : 188 : gimple_stmt_iterator gsi2 = *gsi;
9848 : 188 : gsi_next (&gsi2);
9849 : 188 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
9850 : : vectype, &gsi2);
9851 : : }
9852 : 801 : gimple *new_stmt = SSA_NAME_DEF_STMT (new_temp);
9853 : 1678 : for (j = 0; j < (int) vec_num; ++j)
9854 : 877 : slp_node->push_vec_def (new_stmt);
9855 : : return true;
9856 : : }
9857 : :
9858 : 533688 : if (memory_access_type == VMAT_ELEMENTWISE
9859 : 533688 : || memory_access_type == VMAT_STRIDED_SLP)
9860 : : {
9861 : 27970 : gimple_stmt_iterator incr_gsi;
9862 : 27970 : bool insert_after;
9863 : 27970 : tree offvar = NULL_TREE;
9864 : 27970 : tree ivstep;
9865 : 27970 : tree running_off;
9866 : 27970 : vec<constructor_elt, va_gc> *v = NULL;
9867 : 27970 : tree stride_base, stride_step, alias_off;
9868 : : /* Checked by get_load_store_type. */
9869 : 27970 : unsigned int const_nunits = nunits.to_constant ();
9870 : 27970 : unsigned HOST_WIDE_INT cst_offset = 0;
9871 : 27970 : tree dr_offset;
9872 : 27970 : unsigned int inside_cost = 0;
9873 : :
9874 : 27970 : gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
9875 : 27970 : gcc_assert (!nested_in_vect_loop);
9876 : :
9877 : 27970 : if (grouped_load)
9878 : : {
9879 : 15401 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
9880 : 15401 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
9881 : 15401 : ref_type = get_group_alias_ptr_type (first_stmt_info);
9882 : : }
9883 : : else
9884 : : {
9885 : 12569 : first_stmt_info = stmt_info;
9886 : 12569 : first_dr_info = dr_info;
9887 : 12569 : ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
9888 : : }
9889 : :
9890 : 27970 : if (grouped_load)
9891 : : {
9892 : 15401 : if (memory_access_type == VMAT_STRIDED_SLP)
9893 : 4205 : group_size = DR_GROUP_SIZE (first_stmt_info);
9894 : : else /* VMAT_ELEMENTWISE */
9895 : 11196 : group_size = SLP_TREE_LANES (slp_node);
9896 : : }
9897 : : else
9898 : : group_size = 1;
9899 : :
9900 : 27970 : if (!costing_p)
9901 : : {
9902 : 3304 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
9903 : 3304 : stride_base = fold_build_pointer_plus (
9904 : : DR_BASE_ADDRESS (first_dr_info->dr),
9905 : : size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
9906 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
9907 : 3304 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
9908 : :
9909 : : /* For a load with loop-invariant (but other than power-of-2)
9910 : : stride (i.e. not a grouped access) like so:
9911 : :
9912 : : for (i = 0; i < n; i += stride)
9913 : : ... = array[i];
9914 : :
9915 : : we generate a new induction variable and new accesses to
9916 : : form a new vector (or vectors, depending on ncopies):
9917 : :
9918 : : for (j = 0; ; j += VF*stride)
9919 : : tmp1 = array[j];
9920 : : tmp2 = array[j + stride];
9921 : : ...
9922 : : vectemp = {tmp1, tmp2, ...}
9923 : : */
9924 : :
9925 : 3304 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
9926 : : build_int_cst (TREE_TYPE (stride_step), vf));
9927 : :
9928 : 3304 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
9929 : :
9930 : 3304 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
9931 : 3304 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
9932 : 3304 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
9933 : : loop, &incr_gsi, insert_after,
9934 : : &offvar, NULL);
9935 : :
9936 : 3304 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
9937 : : }
9938 : :
9939 : 27970 : running_off = offvar;
9940 : 27970 : alias_off = build_int_cst (ref_type, 0);
9941 : 27970 : int nloads = const_nunits;
9942 : 27970 : int lnel = 1;
9943 : 27970 : tree ltype = TREE_TYPE (vectype);
9944 : 27970 : tree lvectype = vectype;
9945 : 27970 : auto_vec<tree> dr_chain;
9946 : : /* ??? Modify local copies of alignment_support_scheme and
9947 : : misalignment, but this part of analysis should be done
9948 : : earlier and remembered, likewise the chosen load mode. */
9949 : 27970 : const dr_alignment_support tem = alignment_support_scheme;
9950 : 27970 : dr_alignment_support alignment_support_scheme = tem;
9951 : 27970 : const int tem2 = misalignment;
9952 : 27970 : int misalignment = tem2;
9953 : 27970 : if (memory_access_type == VMAT_STRIDED_SLP)
9954 : : {
9955 : 16774 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
9956 : : /* Use the target vector type if the group size is a multiple
9957 : : of it. */
9958 : 16774 : if (n == const_nunits)
9959 : : {
9960 : 2096 : int mis_align = dr_misalignment (first_dr_info, vectype);
9961 : : /* With VF > 1 we advance the DR by step, if that is constant
9962 : : and only aligned when performed VF times, DR alignment
9963 : : analysis can analyze this as aligned since it assumes
9964 : : contiguous accesses. But that is not how we code generate
9965 : : here, so adjust for this. */
9966 : 2096 : if (maybe_gt (vf, 1u)
9967 : 3530 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
9968 : 3326 : DR_TARGET_ALIGNMENT (first_dr_info)))
9969 : 204 : mis_align = -1;
9970 : 2096 : dr_alignment_support dr_align
9971 : 2096 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
9972 : : mis_align);
9973 : 2096 : if (dr_align == dr_aligned
9974 : 2096 : || dr_align == dr_unaligned_supported)
9975 : : {
9976 : 16774 : nloads = 1;
9977 : 16774 : lnel = const_nunits;
9978 : 16774 : ltype = vectype;
9979 : 16774 : alignment_support_scheme = dr_align;
9980 : 16774 : misalignment = mis_align;
9981 : : }
9982 : : }
9983 : : /* Else use the biggest vector we can load the group without
9984 : : accessing excess elements. */
9985 : 14678 : else if (n > 1)
9986 : : {
9987 : 2116 : tree ptype;
9988 : 2116 : tree vtype
9989 : 2116 : = vector_vector_composition_type (vectype, const_nunits / n,
9990 : : &ptype);
9991 : 2116 : if (vtype != NULL_TREE)
9992 : : {
9993 : 2068 : dr_alignment_support dr_align;
9994 : 2068 : int mis_align = 0;
9995 : 2068 : if (VECTOR_TYPE_P (ptype))
9996 : : {
9997 : 1070 : mis_align = dr_misalignment (first_dr_info, ptype);
9998 : 1070 : if (maybe_gt (vf, 1u)
9999 : 2112 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10000 : 1076 : DR_TARGET_ALIGNMENT (first_dr_info)))
10001 : 1036 : mis_align = -1;
10002 : 1070 : dr_align
10003 : 1070 : = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
10004 : : mis_align);
10005 : : }
10006 : : else
10007 : : dr_align = dr_unaligned_supported;
10008 : 2068 : if (dr_align == dr_aligned
10009 : 2068 : || dr_align == dr_unaligned_supported)
10010 : : {
10011 : 2068 : nloads = const_nunits / n;
10012 : 2068 : lnel = n;
10013 : 2068 : lvectype = vtype;
10014 : 2068 : ltype = ptype;
10015 : 2068 : alignment_support_scheme = dr_align;
10016 : 2068 : misalignment = mis_align;
10017 : : }
10018 : : }
10019 : : }
10020 : 16774 : unsigned align;
10021 : 16774 : if (alignment_support_scheme == dr_aligned)
10022 : 20 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
10023 : : else
10024 : 16754 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
10025 : : /* Alignment is at most the access size if we do multiple loads. */
10026 : 16774 : if (nloads > 1)
10027 : 14678 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
10028 : 16774 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
10029 : : }
10030 : :
10031 : : /* For SLP permutation support we need to load the whole group,
10032 : : not only the number of vector stmts the permutation result
10033 : : fits in. */
10034 : 27970 : int ncopies;
10035 : 27970 : if (ls.slp_perm)
10036 : : {
10037 : 2836 : gcc_assert (memory_access_type != VMAT_ELEMENTWISE);
10038 : : /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
10039 : : variable VF. */
10040 : 2836 : unsigned int const_vf = vf.to_constant ();
10041 : 2836 : ncopies = CEIL (group_size * const_vf, const_nunits);
10042 : 2836 : dr_chain.create (ncopies);
10043 : : }
10044 : : else
10045 : : ncopies = vec_num;
10046 : :
10047 : 27970 : unsigned int group_el = 0;
10048 : 27970 : unsigned HOST_WIDE_INT
10049 : 27970 : elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
10050 : 27970 : unsigned int n_groups = 0;
10051 : : /* For costing some adjacent vector loads, we'd like to cost with
10052 : : the total number of them once instead of cost each one by one. */
10053 : 27970 : unsigned int n_adjacent_loads = 0;
10054 : 69349 : for (j = 0; j < ncopies; j++)
10055 : : {
10056 : 41379 : if (nloads > 1 && !costing_p)
10057 : 2806 : vec_alloc (v, nloads);
10058 : : gimple *new_stmt = NULL;
10059 : 167985 : for (i = 0; i < nloads; i++)
10060 : : {
10061 : 126606 : if (costing_p)
10062 : : {
10063 : : /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
10064 : : avoid ICE, see PR110776. */
10065 : 116666 : if (VECTOR_TYPE_P (ltype)
10066 : 5608 : && memory_access_type != VMAT_ELEMENTWISE)
10067 : 5608 : n_adjacent_loads++;
10068 : : else
10069 : 111058 : inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
10070 : : slp_node, 0, vect_body);
10071 : 116666 : continue;
10072 : : }
10073 : 9940 : unsigned int load_el = group_el;
10074 : : /* For elementwise accesses apply a load permutation directly. */
10075 : 9940 : if (memory_access_type == VMAT_ELEMENTWISE
10076 : 9940 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10077 : 1888 : load_el = SLP_TREE_LOAD_PERMUTATION (slp_node)[group_el];
10078 : 9940 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
10079 : 9940 : load_el * elsz + cst_offset);
10080 : 9940 : tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
10081 : 9940 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10082 : 9940 : new_temp = make_ssa_name (ltype);
10083 : 9940 : new_stmt = gimple_build_assign (new_temp, data_ref);
10084 : 9940 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10085 : 9940 : if (nloads > 1)
10086 : 8156 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
10087 : :
10088 : 9940 : group_el += lnel;
10089 : 9940 : if (group_el == group_size)
10090 : : {
10091 : 9545 : n_groups++;
10092 : : /* When doing SLP make sure to not load elements from
10093 : : the next vector iteration, those will not be accessed
10094 : : so just use the last element again. See PR107451. */
10095 : 9545 : if (known_lt (n_groups, vf))
10096 : : {
10097 : 6219 : tree newoff = copy_ssa_name (running_off);
10098 : 6219 : gimple *incr
10099 : 6219 : = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
10100 : : running_off, stride_step);
10101 : 6219 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
10102 : 6219 : running_off = newoff;
10103 : : }
10104 : : group_el = 0;
10105 : : }
10106 : : }
10107 : :
10108 : 41379 : if (nloads > 1)
10109 : : {
10110 : 31649 : if (costing_p)
10111 : 28843 : inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
10112 : : slp_node, 0, vect_body);
10113 : : else
10114 : : {
10115 : 2806 : tree vec_inv = build_constructor (lvectype, v);
10116 : 2806 : new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
10117 : : lvectype, gsi);
10118 : 2806 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
10119 : 2806 : if (lvectype != vectype)
10120 : : {
10121 : 227 : new_stmt
10122 : 227 : = gimple_build_assign (make_ssa_name (vectype),
10123 : : VIEW_CONVERT_EXPR,
10124 : : build1 (VIEW_CONVERT_EXPR,
10125 : : vectype, new_temp));
10126 : 227 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10127 : : gsi);
10128 : : }
10129 : : }
10130 : : }
10131 : 9730 : else if (!costing_p && ltype != vectype)
10132 : : {
10133 : 1759 : new_stmt = gimple_build_assign (make_ssa_name (vectype),
10134 : : VIEW_CONVERT_EXPR,
10135 : : build1 (VIEW_CONVERT_EXPR,
10136 : : vectype, new_temp));
10137 : 1759 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10138 : : gsi);
10139 : : }
10140 : :
10141 : 41379 : if (!costing_p)
10142 : : {
10143 : 4590 : if (ls.slp_perm)
10144 : 1470 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
10145 : : else
10146 : 3120 : slp_node->push_vec_def (new_stmt);
10147 : : }
10148 : : }
10149 : 27970 : if (ls.slp_perm)
10150 : : {
10151 : 2836 : if (costing_p)
10152 : : {
10153 : 2149 : gcc_assert (ls.n_perms != -1U);
10154 : 2149 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
10155 : : slp_node, 0, vect_body);
10156 : : }
10157 : : else
10158 : : {
10159 : 687 : unsigned n_perms2;
10160 : 687 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
10161 : : false, &n_perms2);
10162 : 687 : gcc_assert (ls.n_perms == n_perms2);
10163 : : }
10164 : : }
10165 : :
10166 : 27970 : if (costing_p)
10167 : : {
10168 : 24666 : if (n_adjacent_loads > 0)
10169 : 2068 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10170 : : alignment_support_scheme, misalignment, false,
10171 : : &inside_cost, nullptr, cost_vec, cost_vec,
10172 : : true);
10173 : 24666 : if (dump_enabled_p ())
10174 : 495 : dump_printf_loc (MSG_NOTE, vect_location,
10175 : : "vect_model_load_cost: inside_cost = %u, "
10176 : : "prologue_cost = 0 .\n",
10177 : : inside_cost);
10178 : : }
10179 : :
10180 : 27970 : return true;
10181 : 27970 : }
10182 : :
10183 : 505718 : if (mat_gather_scatter_p (memory_access_type)
10184 : 505718 : && !ls.ls_type)
10185 : : grouped_load = false;
10186 : :
10187 : 503025 : if (grouped_load
10188 : 505718 : || SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10189 : : {
10190 : 255483 : if (grouped_load)
10191 : : {
10192 : 255234 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10193 : 255234 : group_size = DR_GROUP_SIZE (first_stmt_info);
10194 : : }
10195 : : else
10196 : : {
10197 : : first_stmt_info = stmt_info;
10198 : : group_size = 1;
10199 : : }
10200 : : /* For SLP vectorization we directly vectorize a subchain
10201 : : without permutation. */
10202 : 255483 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10203 : 208754 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10204 : : /* For BB vectorization always use the first stmt to base
10205 : : the data ref pointer on. */
10206 : 255483 : if (bb_vinfo)
10207 : 207473 : first_stmt_info_for_drptr
10208 : 207473 : = vect_find_first_scalar_stmt_in_slp (slp_node);
10209 : :
10210 : 255483 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10211 : 255483 : group_gap_adj = 0;
10212 : :
10213 : : /* VEC_NUM is the number of vect stmts to be created for this group. */
10214 : 255483 : grouped_load = false;
10215 : : /* If an SLP permutation is from N elements to N elements,
10216 : : and if one vector holds a whole number of N, we can load
10217 : : the inputs to the permutation in the same way as an
10218 : : unpermuted sequence. In other cases we need to load the
10219 : : whole group, not only the number of vector stmts the
10220 : : permutation result fits in. */
10221 : 255483 : unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
10222 : 255483 : if (nested_in_vect_loop)
10223 : : /* We do not support grouped accesses in a nested loop,
10224 : : instead the access is contiguous but it might be
10225 : : permuted. No gap adjustment is needed though. */
10226 : : ;
10227 : 255481 : else if (ls.slp_perm
10228 : 255481 : && (group_size != scalar_lanes
10229 : 13003 : || !multiple_p (nunits, group_size)))
10230 : : {
10231 : : /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
10232 : : variable VF; see vect_transform_slp_perm_load. */
10233 : 34794 : unsigned int const_vf = vf.to_constant ();
10234 : 34794 : unsigned int const_nunits = nunits.to_constant ();
10235 : 34794 : vec_num = CEIL (group_size * const_vf, const_nunits);
10236 : 34794 : group_gap_adj = vf * group_size - nunits * vec_num;
10237 : : }
10238 : : else
10239 : : {
10240 : 220687 : group_gap_adj = group_size - scalar_lanes;
10241 : : }
10242 : 255483 : dr_chain.create (vec_num);
10243 : :
10244 : 255483 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10245 : : }
10246 : : else
10247 : : {
10248 : 250235 : first_stmt_info = stmt_info;
10249 : 250235 : first_dr_info = dr_info;
10250 : 250235 : group_size = 1;
10251 : 250235 : group_gap_adj = 0;
10252 : 250235 : ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
10253 : : }
10254 : :
10255 : 505718 : vec_loop_masks *loop_masks
10256 : 298245 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
10257 : 505718 : ? &LOOP_VINFO_MASKS (loop_vinfo)
10258 : 23 : : NULL);
10259 : 23 : vec_loop_lens *loop_lens
10260 : 298245 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
10261 : : ? &LOOP_VINFO_LENS (loop_vinfo)
10262 : 0 : : NULL);
10263 : :
10264 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
10265 : : are some difference here. We cannot enable both the lens and masks
10266 : : during transform but it is allowed during analysis.
10267 : : Shouldn't go with length-based approach if fully masked. */
10268 : 505718 : if (cost_vec == NULL)
10269 : : /* The cost_vec is NULL during transfrom. */
10270 : 159526 : gcc_assert ((!loop_lens || !loop_masks));
10271 : :
10272 : : /* Targets with store-lane instructions must not require explicit
10273 : : realignment. vect_supportable_dr_alignment always returns either
10274 : : dr_aligned or dr_unaligned_supported for (non-length) masked
10275 : : operations. */
10276 : 505718 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
10277 : : && !mask_node
10278 : : && !loop_masks)
10279 : : || mat_gather_scatter_p (memory_access_type)
10280 : : || alignment_support_scheme == dr_aligned
10281 : : || alignment_support_scheme == dr_unaligned_supported);
10282 : :
10283 : : /* In case the vectorization factor (VF) is bigger than the number
10284 : : of elements that we can fit in a vectype (nunits), we have to generate
10285 : : more than one vector stmt - i.e - we need to "unroll" the
10286 : : vector stmt by a factor VF/nunits. In doing so, we record a pointer
10287 : : from one copy of the vector stmt to the next, in the field
10288 : : STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
10289 : : stages to find the correct vector defs to be used when vectorizing
10290 : : stmts that use the defs of the current stmt. The example below
10291 : : illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
10292 : : need to create 4 vectorized stmts):
10293 : :
10294 : : before vectorization:
10295 : : RELATED_STMT VEC_STMT
10296 : : S1: x = memref - -
10297 : : S2: z = x + 1 - -
10298 : :
10299 : : step 1: vectorize stmt S1:
10300 : : We first create the vector stmt VS1_0, and, as usual, record a
10301 : : pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
10302 : : Next, we create the vector stmt VS1_1, and record a pointer to
10303 : : it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
10304 : : Similarly, for VS1_2 and VS1_3. This is the resulting chain of
10305 : : stmts and pointers:
10306 : : RELATED_STMT VEC_STMT
10307 : : VS1_0: vx0 = memref0 VS1_1 -
10308 : : VS1_1: vx1 = memref1 VS1_2 -
10309 : : VS1_2: vx2 = memref2 VS1_3 -
10310 : : VS1_3: vx3 = memref3 - -
10311 : : S1: x = load - VS1_0
10312 : : S2: z = x + 1 - -
10313 : : */
10314 : :
10315 : : /* If the data reference is aligned (dr_aligned) or potentially unaligned
10316 : : on a target that supports unaligned accesses (dr_unaligned_supported)
10317 : : we generate the following code:
10318 : : p = initial_addr;
10319 : : indx = 0;
10320 : : loop {
10321 : : p = p + indx * vectype_size;
10322 : : vec_dest = *(p);
10323 : : indx = indx + 1;
10324 : : }
10325 : :
10326 : : Otherwise, the data reference is potentially unaligned on a target that
10327 : : does not support unaligned accesses (dr_explicit_realign_optimized) -
10328 : : then generate the following code, in which the data in each iteration is
10329 : : obtained by two vector loads, one from the previous iteration, and one
10330 : : from the current iteration:
10331 : : p1 = initial_addr;
10332 : : msq_init = *(floor(p1))
10333 : : p2 = initial_addr + VS - 1;
10334 : : realignment_token = call target_builtin;
10335 : : indx = 0;
10336 : : loop {
10337 : : p2 = p2 + indx * vectype_size
10338 : : lsq = *(floor(p2))
10339 : : vec_dest = realign_load (msq, lsq, realignment_token)
10340 : : indx = indx + 1;
10341 : : msq = lsq;
10342 : : } */
10343 : :
10344 : : /* If the misalignment remains the same throughout the execution of the
10345 : : loop, we can create the init_addr and permutation mask at the loop
10346 : : preheader. Otherwise, it needs to be created inside the loop.
10347 : : This can only occur when vectorizing memory accesses in the inner-loop
10348 : : nested within an outer-loop that is being vectorized. */
10349 : :
10350 : 505718 : if (nested_in_vect_loop
10351 : 505718 : && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
10352 : 1196 : GET_MODE_SIZE (TYPE_MODE (vectype))))
10353 : : {
10354 : 191 : gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
10355 : : compute_in_loop = true;
10356 : : }
10357 : :
10358 : 505718 : bool diff_first_stmt_info
10359 : 505718 : = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
10360 : :
10361 : 505718 : tree offset = NULL_TREE;
10362 : 505718 : if ((alignment_support_scheme == dr_explicit_realign_optimized
10363 : 505718 : || alignment_support_scheme == dr_explicit_realign)
10364 : 0 : && !compute_in_loop)
10365 : : {
10366 : : /* If we have different first_stmt_info, we can't set up realignment
10367 : : here, since we can't guarantee first_stmt_info DR has been
10368 : : initialized yet, use first_stmt_info_for_drptr DR by bumping the
10369 : : distance from first_stmt_info DR instead as below. */
10370 : 0 : if (!costing_p)
10371 : : {
10372 : 0 : if (!diff_first_stmt_info)
10373 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype, gsi,
10374 : : &realignment_token,
10375 : : alignment_support_scheme, NULL_TREE,
10376 : : &at_loop);
10377 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
10378 : : {
10379 : 0 : phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
10380 : 0 : offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
10381 : : size_one_node);
10382 : 0 : gcc_assert (!first_stmt_info_for_drptr);
10383 : : }
10384 : : }
10385 : : }
10386 : : else
10387 : 505718 : at_loop = loop;
10388 : :
10389 : 505718 : if (!known_eq (poffset, 0))
10390 : 4427 : offset = (offset
10391 : 4427 : ? size_binop (PLUS_EXPR, offset, size_int (poffset))
10392 : 4427 : : size_int (poffset));
10393 : :
10394 : 505718 : tree bump;
10395 : 505718 : tree vec_offset = NULL_TREE;
10396 : :
10397 : 505718 : auto_vec<tree> vec_offsets;
10398 : 505718 : auto_vec<tree> vec_masks;
10399 : 505718 : if (mask_node && !costing_p)
10400 : 606 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
10401 : : &vec_masks);
10402 : :
10403 : 505718 : tree vec_mask = NULL_TREE;
10404 : 505718 : tree vec_els = NULL_TREE;
10405 : 505718 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10406 : : {
10407 : 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
10408 : :
10409 : 0 : gcc_assert (alignment_support_scheme == dr_aligned
10410 : : || alignment_support_scheme == dr_unaligned_supported);
10411 : :
10412 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
10413 : 0 : if (!costing_p)
10414 : 0 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
10415 : : memory_access_type, loop_lens);
10416 : :
10417 : 0 : unsigned int inside_cost = 0, prologue_cost = 0;
10418 : : /* For costing some adjacent vector loads, we'd like to cost with
10419 : : the total number of them once instead of cost each one by one. */
10420 : 0 : unsigned int n_adjacent_loads = 0;
10421 : 0 : int ncopies = vec_num / group_size;
10422 : 0 : for (j = 0; j < ncopies; j++)
10423 : : {
10424 : 0 : if (costing_p)
10425 : : {
10426 : : /* An IFN_LOAD_LANES will load all its vector results,
10427 : : regardless of which ones we actually need. Account
10428 : : for the cost of unused results. */
10429 : 0 : if (first_stmt_info == stmt_info)
10430 : : {
10431 : 0 : unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
10432 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
10433 : 0 : do
10434 : : {
10435 : 0 : gaps -= 1;
10436 : 0 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
10437 : : }
10438 : 0 : while (next_stmt_info);
10439 : 0 : if (gaps)
10440 : : {
10441 : 0 : if (dump_enabled_p ())
10442 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10443 : : "vect_model_load_cost: %d "
10444 : : "unused vectors.\n",
10445 : : gaps);
10446 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
10447 : : alignment_support_scheme,
10448 : : misalignment, false, &inside_cost,
10449 : : &prologue_cost, cost_vec, cost_vec,
10450 : : true);
10451 : : }
10452 : : }
10453 : 0 : n_adjacent_loads++;
10454 : 0 : continue;
10455 : 0 : }
10456 : :
10457 : : /* 1. Create the vector or array pointer update chain. */
10458 : 0 : if (j == 0)
10459 : 0 : dataref_ptr
10460 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10461 : : at_loop, offset, &dummy, gsi,
10462 : : &ptr_incr, false, bump);
10463 : : else
10464 : : {
10465 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
10466 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
10467 : : stmt_info, bump);
10468 : : }
10469 : 0 : if (mask_node)
10470 : 0 : vec_mask = vec_masks[j];
10471 : :
10472 : 0 : tree vec_array = create_vector_array (vectype, group_size);
10473 : :
10474 : 0 : tree final_mask = NULL_TREE;
10475 : 0 : tree final_len = NULL_TREE;
10476 : 0 : tree bias = NULL_TREE;
10477 : 0 : if (loop_masks)
10478 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10479 : : ncopies, vectype, j);
10480 : 0 : if (vec_mask)
10481 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
10482 : : vec_mask, gsi);
10483 : :
10484 : 0 : if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
10485 : : {
10486 : 0 : if (loop_lens)
10487 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10488 : : ncopies, vectype, j, 1);
10489 : : else
10490 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
10491 : 0 : signed char biasval
10492 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10493 : 0 : bias = build_int_cst (intQI_type_node, biasval);
10494 : 0 : if (!final_mask)
10495 : : {
10496 : 0 : mask_vectype = truth_type_for (vectype);
10497 : 0 : final_mask = build_minus_one_cst (mask_vectype);
10498 : : }
10499 : : }
10500 : :
10501 : 0 : if (final_mask)
10502 : : {
10503 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10504 : 0 : if (type_mode_padding_p
10505 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10506 : 0 : need_zeroing = true;
10507 : : }
10508 : :
10509 : 0 : gcall *call;
10510 : 0 : if (final_len && final_mask)
10511 : : {
10512 : : /* Emit:
10513 : : VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10514 : : VEC_MASK, LEN, BIAS). */
10515 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10516 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10517 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
10518 : : dataref_ptr, alias_ptr,
10519 : : final_mask, vec_els,
10520 : : final_len, bias);
10521 : : }
10522 : 0 : else if (final_mask)
10523 : : {
10524 : : /* Emit:
10525 : : VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10526 : : VEC_MASK). */
10527 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10528 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10529 : 0 : call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
10530 : : dataref_ptr, alias_ptr,
10531 : : final_mask, vec_els);
10532 : : }
10533 : : else
10534 : : {
10535 : : /* Emit:
10536 : : VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
10537 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
10538 : 0 : call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
10539 : : }
10540 : 0 : gimple_call_set_lhs (call, vec_array);
10541 : 0 : gimple_call_set_nothrow (call, true);
10542 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10543 : :
10544 : : /* Extract each vector into an SSA_NAME. */
10545 : 0 : for (unsigned i = 0; i < group_size; i++)
10546 : : {
10547 : 0 : new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
10548 : : vec_array, i, need_zeroing,
10549 : : final_mask);
10550 : 0 : slp_node->push_vec_def (new_temp);
10551 : : }
10552 : :
10553 : : /* Record that VEC_ARRAY is now dead. */
10554 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
10555 : : }
10556 : :
10557 : 0 : if (costing_p)
10558 : : {
10559 : 0 : if (n_adjacent_loads > 0)
10560 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10561 : : alignment_support_scheme, misalignment, false,
10562 : : &inside_cost, &prologue_cost, cost_vec,
10563 : : cost_vec, true);
10564 : 0 : if (dump_enabled_p ())
10565 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10566 : : "vect_model_load_cost: inside_cost = %u, "
10567 : : "prologue_cost = %u .\n",
10568 : : inside_cost, prologue_cost);
10569 : : }
10570 : :
10571 : 0 : return true;
10572 : : }
10573 : :
10574 : 505718 : if (mat_gather_scatter_p (memory_access_type))
10575 : : {
10576 : 2693 : gcc_assert ((!grouped_load && !ls.slp_perm) || ls.ls_type);
10577 : :
10578 : : /* If we pun the original vectype the loads as well as costing, length,
10579 : : etc. is performed with the new type. After loading we VIEW_CONVERT
10580 : : the data to the original vectype. */
10581 : 2693 : tree original_vectype = vectype;
10582 : 2693 : if (ls.ls_type)
10583 : 0 : vectype = ls.ls_type;
10584 : :
10585 : : /* 1. Create the vector or array pointer update chain. */
10586 : 2693 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10587 : : {
10588 : 2693 : aggr_type = NULL_TREE;
10589 : 2693 : bump = NULL_TREE;
10590 : 2693 : if (!costing_p)
10591 : 747 : vect_get_gather_scatter_ops (loop, slp_node, &dataref_ptr,
10592 : : &vec_offsets);
10593 : : }
10594 : : else
10595 : : {
10596 : 0 : aggr_type = elem_type;
10597 : 0 : if (!costing_p)
10598 : : {
10599 : 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vectype,
10600 : : ls.strided_offset_vectype,
10601 : : loop_vinfo, gsi,
10602 : : &bump, &vec_offset, loop_lens);
10603 : 0 : dataref_ptr
10604 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10605 : : at_loop, offset, &dummy, gsi,
10606 : : &ptr_incr, false, bump);
10607 : : }
10608 : : }
10609 : :
10610 : : unsigned int inside_cost = 0, prologue_cost = 0;
10611 : :
10612 : 6081 : gimple *new_stmt = NULL;
10613 : 6081 : for (i = 0; i < vec_num; i++)
10614 : : {
10615 : 3388 : tree final_mask = NULL_TREE;
10616 : 3388 : tree final_len = NULL_TREE;
10617 : 3388 : tree bias = NULL_TREE;
10618 : 3388 : if (!costing_p)
10619 : : {
10620 : 959 : if (mask_node)
10621 : 153 : vec_mask = vec_masks[i];
10622 : 959 : if (loop_masks)
10623 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10624 : : vec_num, vectype, i);
10625 : 959 : if (vec_mask)
10626 : 153 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
10627 : : final_mask, vec_mask, gsi);
10628 : :
10629 : 959 : if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10630 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
10631 : : gsi, stmt_info, bump);
10632 : : }
10633 : :
10634 : : /* 2. Create the vector-load in the loop. */
10635 : 3388 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
10636 : 3388 : tree alias_align_ptr = build_int_cst (ref_type, align);
10637 : 3388 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
10638 : : {
10639 : 0 : if (costing_p)
10640 : : {
10641 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
10642 : 0 : inside_cost
10643 : 0 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
10644 : : slp_node, 0, vect_body);
10645 : 3388 : continue;
10646 : 0 : }
10647 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10648 : 0 : vec_offset = vec_offsets[i];
10649 : 0 : tree zero = build_zero_cst (vectype);
10650 : 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
10651 : :
10652 : 0 : if (ls.gs.ifn == IFN_MASK_LEN_GATHER_LOAD)
10653 : : {
10654 : 0 : if (loop_lens)
10655 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10656 : : vec_num, vectype, i, 1);
10657 : : else
10658 : 0 : final_len = build_int_cst (sizetype,
10659 : 0 : TYPE_VECTOR_SUBPARTS (vectype));
10660 : 0 : signed char biasval
10661 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10662 : 0 : bias = build_int_cst (intQI_type_node, biasval);
10663 : 0 : if (!final_mask)
10664 : : {
10665 : 0 : mask_vectype = truth_type_for (vectype);
10666 : 0 : final_mask = build_minus_one_cst (mask_vectype);
10667 : : }
10668 : : }
10669 : :
10670 : 0 : if (final_mask)
10671 : : {
10672 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10673 : 0 : if (type_mode_padding_p
10674 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10675 : 0 : need_zeroing = true;
10676 : : }
10677 : :
10678 : 0 : gcall *call;
10679 : 0 : if (final_len && final_mask)
10680 : : {
10681 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
10682 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_GATHER_LOAD,
10683 : : 9, dataref_ptr,
10684 : : alias_align_ptr,
10685 : : vec_offset, scale, zero,
10686 : : final_mask, vec_els,
10687 : : final_len, bias);
10688 : : else
10689 : : /* Non-vector offset indicates that prefer to take
10690 : : MASK_LEN_STRIDED_LOAD instead of the
10691 : : MASK_LEN_GATHER_LOAD with direct stride arg. */
10692 : 0 : call = gimple_build_call_internal
10693 : 0 : (IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr,
10694 : : vec_offset, zero, final_mask, vec_els, final_len,
10695 : : bias);
10696 : : }
10697 : 0 : else if (final_mask)
10698 : 0 : call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
10699 : : 7, dataref_ptr,
10700 : : alias_align_ptr,
10701 : : vec_offset, scale,
10702 : : zero, final_mask, vec_els);
10703 : : else
10704 : 0 : call = gimple_build_call_internal (IFN_GATHER_LOAD, 5,
10705 : : dataref_ptr,
10706 : : alias_align_ptr,
10707 : : vec_offset, scale, zero);
10708 : 0 : gimple_call_set_nothrow (call, true);
10709 : 0 : new_stmt = call;
10710 : 0 : data_ref = NULL_TREE;
10711 : : }
10712 : 3388 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
10713 : : {
10714 : : /* The builtin decls path for gather is legacy, x86 only. */
10715 : 570 : gcc_assert (!final_len && nunits.is_constant ());
10716 : 570 : if (costing_p)
10717 : : {
10718 : 287 : unsigned int cnunits = vect_nunits_for_cost (vectype);
10719 : 287 : inside_cost
10720 : 287 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
10721 : : slp_node, 0, vect_body);
10722 : 287 : continue;
10723 : 287 : }
10724 : 283 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
10725 : 283 : poly_uint64 offset_nunits = TYPE_VECTOR_SUBPARTS (offset_vectype);
10726 : 283 : if (known_eq (nunits, offset_nunits))
10727 : : {
10728 : 134 : new_stmt = vect_build_one_gather_load_call
10729 : 134 : (vinfo, stmt_info, slp_node, vectype, gsi,
10730 : 134 : ls.gs.decl, dataref_ptr, vec_offsets[i],
10731 : : final_mask);
10732 : 134 : data_ref = NULL_TREE;
10733 : : }
10734 : 149 : else if (known_eq (nunits, offset_nunits * 2))
10735 : : {
10736 : : /* We have a offset vector with half the number of
10737 : : lanes but the builtins will produce full vectype
10738 : : data with just the lower lanes filled. */
10739 : 63 : new_stmt = vect_build_one_gather_load_call
10740 : 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
10741 : 63 : ls.gs.decl, dataref_ptr, vec_offsets[2 * i],
10742 : : final_mask);
10743 : 63 : tree low = make_ssa_name (vectype);
10744 : 63 : gimple_set_lhs (new_stmt, low);
10745 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10746 : :
10747 : : /* now put upper half of final_mask in final_mask low. */
10748 : 63 : if (final_mask
10749 : 63 : && !SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (final_mask))))
10750 : : {
10751 : 10 : int count = nunits.to_constant ();
10752 : 10 : vec_perm_builder sel (count, count, 1);
10753 : 10 : sel.quick_grow (count);
10754 : 78 : for (int i = 0; i < count; ++i)
10755 : 68 : sel[i] = i | (count / 2);
10756 : 10 : vec_perm_indices indices (sel, 2, count);
10757 : 10 : tree perm_mask = vect_gen_perm_mask_checked
10758 : 10 : (TREE_TYPE (final_mask), indices);
10759 : 10 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
10760 : : final_mask, final_mask,
10761 : : perm_mask);
10762 : 10 : final_mask = make_ssa_name (TREE_TYPE (final_mask));
10763 : 10 : gimple_set_lhs (new_stmt, final_mask);
10764 : 10 : vect_finish_stmt_generation (vinfo, stmt_info,
10765 : : new_stmt, gsi);
10766 : 10 : }
10767 : 53 : else if (final_mask)
10768 : : {
10769 : 25 : new_stmt = gimple_build_assign (NULL_TREE,
10770 : : VEC_UNPACK_HI_EXPR,
10771 : : final_mask);
10772 : 25 : final_mask = make_ssa_name
10773 : 25 : (truth_type_for (offset_vectype));
10774 : 25 : gimple_set_lhs (new_stmt, final_mask);
10775 : 25 : vect_finish_stmt_generation (vinfo, stmt_info,
10776 : : new_stmt, gsi);
10777 : : }
10778 : :
10779 : 63 : new_stmt = vect_build_one_gather_load_call
10780 : 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
10781 : : ls.gs.decl, dataref_ptr,
10782 : 63 : vec_offsets[2 * i + 1], final_mask);
10783 : 63 : tree high = make_ssa_name (vectype);
10784 : 63 : gimple_set_lhs (new_stmt, high);
10785 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10786 : :
10787 : : /* compose low + high. */
10788 : 63 : int count = nunits.to_constant ();
10789 : 63 : vec_perm_builder sel (count, count, 1);
10790 : 63 : sel.quick_grow (count);
10791 : 655 : for (int i = 0; i < count; ++i)
10792 : 592 : sel[i] = i < count / 2 ? i : i + count / 2;
10793 : 63 : vec_perm_indices indices (sel, 2, count);
10794 : 63 : tree perm_mask
10795 : 63 : = vect_gen_perm_mask_checked (vectype, indices);
10796 : 63 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
10797 : : low, high, perm_mask);
10798 : 63 : data_ref = NULL_TREE;
10799 : 63 : }
10800 : 86 : else if (known_eq (nunits * 2, offset_nunits))
10801 : : {
10802 : : /* We have a offset vector with double the number of
10803 : : lanes. Select the low/high part accordingly. */
10804 : 86 : vec_offset = vec_offsets[i / 2];
10805 : 86 : if (i & 1)
10806 : : {
10807 : 43 : int count = offset_nunits.to_constant ();
10808 : 43 : vec_perm_builder sel (count, count, 1);
10809 : 43 : sel.quick_grow (count);
10810 : 463 : for (int i = 0; i < count; ++i)
10811 : 420 : sel[i] = i | (count / 2);
10812 : 43 : vec_perm_indices indices (sel, 2, count);
10813 : 43 : tree perm_mask = vect_gen_perm_mask_checked
10814 : 43 : (TREE_TYPE (vec_offset), indices);
10815 : 43 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
10816 : : vec_offset, vec_offset,
10817 : : perm_mask);
10818 : 43 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
10819 : 43 : gimple_set_lhs (new_stmt, vec_offset);
10820 : 43 : vect_finish_stmt_generation (vinfo, stmt_info,
10821 : : new_stmt, gsi);
10822 : 43 : }
10823 : 86 : new_stmt = vect_build_one_gather_load_call
10824 : 86 : (vinfo, stmt_info, slp_node, vectype, gsi,
10825 : : ls.gs.decl,
10826 : : dataref_ptr, vec_offset, final_mask);
10827 : 86 : data_ref = NULL_TREE;
10828 : : }
10829 : : else
10830 : 0 : gcc_unreachable ();
10831 : : }
10832 : : else
10833 : : {
10834 : : /* Emulated gather-scatter. */
10835 : 2818 : gcc_assert (!final_mask);
10836 : 2818 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
10837 : 2818 : if (costing_p)
10838 : : {
10839 : : /* For emulated gathers N offset vector element
10840 : : offset add is consumed by the load). */
10841 : 2142 : inside_cost = record_stmt_cost (cost_vec, const_nunits,
10842 : : vec_to_scalar,
10843 : : slp_node, 0, vect_body);
10844 : : /* N scalar loads plus gathering them into a
10845 : : vector. */
10846 : 2142 : inside_cost
10847 : 2142 : = record_stmt_cost (cost_vec, const_nunits, scalar_load,
10848 : : slp_node, 0, vect_body);
10849 : 2142 : inside_cost
10850 : 2142 : = record_stmt_cost (cost_vec, 1, vec_construct,
10851 : : slp_node, 0, vect_body);
10852 : 2142 : continue;
10853 : : }
10854 : 676 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
10855 : 676 : unsigned HOST_WIDE_INT const_offset_nunits
10856 : 676 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
10857 : 676 : vec<constructor_elt, va_gc> *ctor_elts;
10858 : 676 : vec_alloc (ctor_elts, const_nunits);
10859 : 676 : gimple_seq stmts = NULL;
10860 : : /* We support offset vectors with more elements
10861 : : than the data vector for now. */
10862 : 676 : unsigned HOST_WIDE_INT factor
10863 : : = const_offset_nunits / const_nunits;
10864 : 676 : vec_offset = vec_offsets[i / factor];
10865 : 676 : unsigned elt_offset = (i % factor) * const_nunits;
10866 : 676 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
10867 : 676 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
10868 : 676 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
10869 : 2816 : for (unsigned k = 0; k < const_nunits; ++k)
10870 : : {
10871 : 2140 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
10872 : : bitsize_int (k + elt_offset));
10873 : 6420 : tree idx = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
10874 : 2140 : vec_offset, TYPE_SIZE (idx_type),
10875 : : boff);
10876 : 2140 : idx = gimple_convert (&stmts, sizetype, idx);
10877 : 2140 : idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx, scale);
10878 : 2140 : tree ptr = gimple_build (&stmts, PLUS_EXPR,
10879 : 2140 : TREE_TYPE (dataref_ptr),
10880 : : dataref_ptr, idx);
10881 : 2140 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
10882 : 2140 : tree elt = make_ssa_name (TREE_TYPE (vectype));
10883 : 2140 : tree ref = build2 (MEM_REF, ltype, ptr,
10884 : : build_int_cst (ref_type, 0));
10885 : 2140 : new_stmt = gimple_build_assign (elt, ref);
10886 : 4280 : gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
10887 : 2140 : gimple_seq_add_stmt (&stmts, new_stmt);
10888 : 2140 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
10889 : : }
10890 : 676 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
10891 : 676 : new_stmt = gimple_build_assign (NULL_TREE,
10892 : : build_constructor (vectype,
10893 : : ctor_elts));
10894 : 676 : data_ref = NULL_TREE;
10895 : : }
10896 : :
10897 : 959 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
10898 : : /* DATA_REF is null if we've already built the statement. */
10899 : 959 : if (data_ref)
10900 : : {
10901 : : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10902 : : new_stmt = gimple_build_assign (vec_dest, data_ref);
10903 : : }
10904 : 1918 : new_temp = (need_zeroing
10905 : 959 : ? make_ssa_name (vectype)
10906 : 959 : : make_ssa_name (vec_dest, new_stmt));
10907 : 959 : gimple_set_lhs (new_stmt, new_temp);
10908 : 959 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10909 : :
10910 : : /* If we need to explicitly zero inactive elements emit a
10911 : : VEC_COND_EXPR that does so. */
10912 : 959 : if (need_zeroing)
10913 : : {
10914 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
10915 : : vectype);
10916 : :
10917 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
10918 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
10919 : : final_mask, new_temp, vec_els);
10920 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10921 : 0 : new_temp = new_temp2;
10922 : : }
10923 : :
10924 : 959 : if (ls.ls_type)
10925 : : {
10926 : 0 : new_stmt = gimple_build_assign (make_ssa_name
10927 : : (original_vectype),
10928 : : VIEW_CONVERT_EXPR,
10929 : : build1 (VIEW_CONVERT_EXPR,
10930 : : original_vectype,
10931 : : new_temp));
10932 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10933 : : }
10934 : :
10935 : : /* Store vector loads in the corresponding SLP_NODE. */
10936 : 959 : if (!costing_p)
10937 : : {
10938 : 959 : if (ls.slp_perm)
10939 : 0 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
10940 : : else
10941 : 959 : slp_node->push_vec_def (new_stmt);
10942 : : }
10943 : : }
10944 : :
10945 : 2693 : if (ls.slp_perm)
10946 : : {
10947 : 0 : if (costing_p)
10948 : : {
10949 : 0 : gcc_assert (ls.n_perms != -1U);
10950 : 0 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
10951 : : slp_node, 0, vect_body);
10952 : : }
10953 : : else
10954 : : {
10955 : 0 : unsigned n_perms2;
10956 : 0 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
10957 : : false, &n_perms2);
10958 : 0 : gcc_assert (ls.n_perms == n_perms2);
10959 : : }
10960 : : }
10961 : :
10962 : 2693 : if (costing_p && dump_enabled_p ())
10963 : 222 : dump_printf_loc (MSG_NOTE, vect_location,
10964 : : "vect_model_load_cost: inside_cost = %u, "
10965 : : "prologue_cost = %u .\n",
10966 : : inside_cost, prologue_cost);
10967 : 2693 : return true;
10968 : : }
10969 : :
10970 : 503025 : aggr_type = vectype;
10971 : 503025 : if (!costing_p)
10972 : 158779 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
10973 : : memory_access_type, loop_lens);
10974 : :
10975 : 503025 : poly_uint64 group_elt = 0;
10976 : 503025 : unsigned int inside_cost = 0, prologue_cost = 0;
10977 : : /* For costing some adjacent vector loads, we'd like to cost with
10978 : : the total number of them once instead of cost each one by one. */
10979 : 503025 : unsigned int n_adjacent_loads = 0;
10980 : :
10981 : : /* 1. Create the vector or array pointer update chain. */
10982 : 503025 : if (!costing_p)
10983 : : {
10984 : 158779 : bool simd_lane_access_p
10985 : 158779 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
10986 : 158779 : if (simd_lane_access_p
10987 : 1629 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
10988 : 1629 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
10989 : 1629 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
10990 : 1629 : && integer_zerop (DR_INIT (first_dr_info->dr))
10991 : 1629 : && alias_sets_conflict_p (get_alias_set (aggr_type),
10992 : 1629 : get_alias_set (TREE_TYPE (ref_type)))
10993 : 158779 : && (alignment_support_scheme == dr_aligned
10994 : 1629 : || alignment_support_scheme == dr_unaligned_supported))
10995 : : {
10996 : 1629 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
10997 : 1629 : dataref_offset = build_int_cst (ref_type, 0);
10998 : : }
10999 : 157150 : else if (diff_first_stmt_info)
11000 : : {
11001 : 3036 : dataref_ptr
11002 : 3036 : = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
11003 : : aggr_type, at_loop, offset, &dummy,
11004 : : gsi, &ptr_incr, simd_lane_access_p,
11005 : : bump);
11006 : : /* Adjust the pointer by the difference to first_stmt. */
11007 : 3036 : data_reference_p ptrdr
11008 : : = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
11009 : 3036 : tree diff = fold_convert (sizetype,
11010 : : size_binop (MINUS_EXPR,
11011 : : DR_INIT (first_dr_info->dr),
11012 : : DR_INIT (ptrdr)));
11013 : 3036 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11014 : : stmt_info, diff);
11015 : 3036 : if (alignment_support_scheme == dr_explicit_realign)
11016 : : {
11017 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info_for_drptr,
11018 : : vectype, gsi,
11019 : : &realignment_token,
11020 : : alignment_support_scheme,
11021 : : dataref_ptr, &at_loop);
11022 : 0 : gcc_assert (!compute_in_loop);
11023 : : }
11024 : : }
11025 : : else
11026 : 154114 : dataref_ptr
11027 : 154114 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11028 : : at_loop,
11029 : : offset, &dummy, gsi, &ptr_incr,
11030 : : simd_lane_access_p, bump);
11031 : : }
11032 : : else if (!costing_p)
11033 : : {
11034 : : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
11035 : : if (dataref_offset)
11036 : : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
11037 : : else
11038 : : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11039 : : stmt_info, bump);
11040 : : }
11041 : :
11042 : 503025 : if (grouped_load || ls.slp_perm)
11043 : 46729 : dr_chain.create (vec_num);
11044 : :
11045 : : gimple *new_stmt = NULL;
11046 : 1319050 : for (i = 0; i < vec_num; i++)
11047 : : {
11048 : 816025 : tree final_mask = NULL_TREE;
11049 : 816025 : tree final_len = NULL_TREE;
11050 : 816025 : tree bias = NULL_TREE;
11051 : :
11052 : 816025 : if (!costing_p)
11053 : : {
11054 : 211068 : if (mask_node)
11055 : 560 : vec_mask = vec_masks[i];
11056 : 211068 : if (loop_masks)
11057 : 26 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11058 : : vec_num, vectype, i);
11059 : 211068 : if (vec_mask)
11060 : 560 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11061 : : final_mask, vec_mask, gsi);
11062 : :
11063 : 211068 : if (i > 0)
11064 : 52289 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11065 : : gsi, stmt_info, bump);
11066 : : }
11067 : :
11068 : : /* 2. Create the vector-load in the loop. */
11069 : 816025 : switch (alignment_support_scheme)
11070 : : {
11071 : 816025 : case dr_aligned:
11072 : 816025 : case dr_unaligned_supported:
11073 : 816025 : {
11074 : 816025 : if (costing_p)
11075 : : break;
11076 : :
11077 : 211068 : unsigned int misalign;
11078 : 211068 : unsigned HOST_WIDE_INT align;
11079 : 211068 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
11080 : 211068 : if (alignment_support_scheme == dr_aligned)
11081 : : misalign = 0;
11082 : 130034 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
11083 : : {
11084 : 105346 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
11085 : 105346 : misalign = 0;
11086 : : }
11087 : : else
11088 : 24688 : misalign = misalignment;
11089 : 211068 : if (dataref_offset == NULL_TREE
11090 : 208941 : && TREE_CODE (dataref_ptr) == SSA_NAME)
11091 : 141595 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
11092 : : misalign);
11093 : 211068 : align = least_bit_hwi (misalign | align);
11094 : :
11095 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
11096 : 211068 : machine_mode vmode = TYPE_MODE (vectype);
11097 : 211068 : machine_mode new_vmode = vmode;
11098 : 211068 : internal_fn partial_ifn = IFN_LAST;
11099 : 211068 : if (loop_lens)
11100 : : {
11101 : 0 : opt_machine_mode new_ovmode
11102 : 0 : = get_len_load_store_mode (vmode, true, &partial_ifn);
11103 : 0 : new_vmode = new_ovmode.require ();
11104 : 0 : unsigned factor
11105 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
11106 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11107 : : vec_num, vectype, i, factor);
11108 : : }
11109 : 211068 : else if (final_mask)
11110 : : {
11111 : 584 : if (!can_vec_mask_load_store_p (vmode,
11112 : 584 : TYPE_MODE
11113 : : (TREE_TYPE (final_mask)),
11114 : : true, &partial_ifn))
11115 : 0 : gcc_unreachable ();
11116 : : }
11117 : :
11118 : 211068 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11119 : : {
11120 : 0 : if (!final_len)
11121 : : {
11122 : : /* Pass VF value to 'len' argument of
11123 : : MASK_LEN_LOAD if LOOP_LENS is invalid. */
11124 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11125 : : }
11126 : 0 : if (!final_mask)
11127 : : {
11128 : : /* Pass all ones value to 'mask' argument of
11129 : : MASK_LEN_LOAD if final_mask is invalid. */
11130 : 0 : mask_vectype = truth_type_for (vectype);
11131 : 0 : final_mask = build_minus_one_cst (mask_vectype);
11132 : : }
11133 : : }
11134 : 211068 : if (final_len)
11135 : : {
11136 : 0 : signed char biasval
11137 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11138 : 0 : bias = build_int_cst (intQI_type_node, biasval);
11139 : : }
11140 : :
11141 : 211068 : tree vec_els;
11142 : :
11143 : 211068 : if (final_len)
11144 : : {
11145 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11146 : 0 : gcall *call;
11147 : 0 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11148 : : {
11149 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval,
11150 : : vectype);
11151 : 0 : if (type_mode_padding_p
11152 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11153 : 0 : need_zeroing = true;
11154 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
11155 : : 6, dataref_ptr, ptr,
11156 : : final_mask, vec_els,
11157 : : final_len, bias);
11158 : : }
11159 : : else
11160 : 0 : call = gimple_build_call_internal (IFN_LEN_LOAD, 4,
11161 : : dataref_ptr, ptr,
11162 : : final_len, bias);
11163 : 0 : gimple_call_set_nothrow (call, true);
11164 : 0 : new_stmt = call;
11165 : 0 : data_ref = NULL_TREE;
11166 : :
11167 : : /* Need conversion if it's wrapped with VnQI. */
11168 : 0 : if (vmode != new_vmode)
11169 : : {
11170 : 0 : tree new_vtype
11171 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
11172 : : new_vmode);
11173 : 0 : tree var = vect_get_new_ssa_name (new_vtype,
11174 : : vect_simple_var);
11175 : 0 : gimple_set_lhs (call, var);
11176 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call,
11177 : : gsi);
11178 : 0 : tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
11179 : 0 : new_stmt = gimple_build_assign (vec_dest,
11180 : : VIEW_CONVERT_EXPR, op);
11181 : : }
11182 : : }
11183 : 211068 : else if (final_mask)
11184 : : {
11185 : 584 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11186 : 584 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11187 : 584 : if (type_mode_padding_p
11188 : 584 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11189 : 0 : need_zeroing = true;
11190 : 584 : gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
11191 : : dataref_ptr, ptr,
11192 : : final_mask,
11193 : : vec_els);
11194 : 584 : gimple_call_set_nothrow (call, true);
11195 : 584 : new_stmt = call;
11196 : 584 : data_ref = NULL_TREE;
11197 : : }
11198 : : else
11199 : : {
11200 : 210484 : tree ltype = vectype;
11201 : 210484 : tree new_vtype = NULL_TREE;
11202 : 210484 : unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
11203 : 210484 : unsigned HOST_WIDE_INT dr_size
11204 : 210484 : = vect_get_scalar_dr_size (first_dr_info);
11205 : 210484 : poly_int64 off = 0;
11206 : 210484 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11207 : 1416 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
11208 : 210484 : unsigned int vect_align
11209 : 210484 : = vect_known_alignment_in_bytes (first_dr_info, vectype,
11210 : 210484 : off);
11211 : : /* Try to use a single smaller load when we are about
11212 : : to load excess elements compared to the unrolled
11213 : : scalar loop. */
11214 : 210484 : if (known_gt ((i + 1) * nunits,
11215 : : (group_size * vf - gap)))
11216 : : {
11217 : 2188 : poly_uint64 remain = ((group_size * vf - gap) - i * nunits);
11218 : 2188 : if (known_ge ((i + 1) * nunits - (group_size * vf - gap),
11219 : : nunits))
11220 : : /* DR will be unused. */
11221 : : ltype = NULL_TREE;
11222 : 1663 : else if (known_ge (vect_align,
11223 : : tree_to_poly_uint64
11224 : : (TYPE_SIZE_UNIT (vectype))))
11225 : : /* Aligned access to excess elements is OK if
11226 : : at least one element is accessed in the
11227 : : scalar loop. */
11228 : : ;
11229 : 1431 : else if (known_gt (vect_align,
11230 : : ((nunits - remain) * dr_size)))
11231 : : /* Aligned access to the gap area when there's
11232 : : at least one element in it is OK. */
11233 : : ;
11234 : : else
11235 : : {
11236 : : /* remain should now be > 0 and < nunits. */
11237 : 1428 : unsigned num;
11238 : 1428 : if (known_ne (remain, 0u)
11239 : 1428 : && constant_multiple_p (nunits, remain, &num))
11240 : : {
11241 : 1037 : tree ptype;
11242 : 1037 : new_vtype
11243 : 1037 : = vector_vector_composition_type (vectype, num,
11244 : : &ptype);
11245 : 1037 : if (new_vtype)
11246 : 1037 : ltype = ptype;
11247 : : }
11248 : : /* Else use multiple loads or a masked load? */
11249 : : /* For loop vectorization we now should have
11250 : : an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
11251 : : set. */
11252 : 1428 : if (loop_vinfo)
11253 : 1359 : gcc_assert (new_vtype
11254 : : || LOOP_VINFO_PEELING_FOR_GAPS
11255 : : (loop_vinfo));
11256 : : /* But still reduce the access size to the next
11257 : : required power-of-two so peeling a single
11258 : : scalar iteration is sufficient. */
11259 : 1428 : unsigned HOST_WIDE_INT cremain;
11260 : 1428 : if (remain.is_constant (&cremain))
11261 : : {
11262 : 1428 : unsigned HOST_WIDE_INT cpart_size
11263 : 1428 : = 1 << ceil_log2 (cremain);
11264 : 1428 : if (known_gt (nunits, cpart_size)
11265 : 1428 : && constant_multiple_p (nunits, cpart_size,
11266 : : &num))
11267 : : {
11268 : 1049 : tree ptype;
11269 : 1049 : new_vtype
11270 : 2098 : = vector_vector_composition_type (vectype,
11271 : 1049 : num,
11272 : : &ptype);
11273 : 1049 : if (new_vtype)
11274 : 1049 : ltype = ptype;
11275 : : }
11276 : : }
11277 : : }
11278 : : }
11279 : 210484 : tree offset = (dataref_offset ? dataref_offset
11280 : 208357 : : build_int_cst (ref_type, 0));
11281 : 210484 : if (!ltype)
11282 : : ;
11283 : 209959 : else if (ltype != vectype
11284 : 209959 : && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11285 : : {
11286 : 21 : poly_uint64 gap_offset
11287 : 21 : = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
11288 : 21 : - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
11289 : 21 : tree gapcst = build_int_cstu (ref_type, gap_offset);
11290 : 21 : offset = size_binop (PLUS_EXPR, offset, gapcst);
11291 : : }
11292 : 210484 : if (ltype)
11293 : : {
11294 : 209959 : data_ref = fold_build2 (MEM_REF, ltype,
11295 : : dataref_ptr, offset);
11296 : 209959 : if (alignment_support_scheme == dr_aligned
11297 : 209959 : && align >= TYPE_ALIGN_UNIT (ltype))
11298 : : ;
11299 : : else
11300 : 129374 : TREE_TYPE (data_ref)
11301 : 258748 : = build_aligned_type (TREE_TYPE (data_ref),
11302 : : align * BITS_PER_UNIT);
11303 : : }
11304 : 210484 : if (!ltype)
11305 : 525 : data_ref = build_constructor (vectype, NULL);
11306 : 209959 : else if (ltype != vectype)
11307 : : {
11308 : 1049 : vect_copy_ref_info (data_ref,
11309 : 1049 : DR_REF (first_dr_info->dr));
11310 : 1049 : tree tem = make_ssa_name (ltype);
11311 : 1049 : new_stmt = gimple_build_assign (tem, data_ref);
11312 : 1049 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11313 : : gsi);
11314 : 1049 : data_ref = NULL;
11315 : 1049 : vec<constructor_elt, va_gc> *v;
11316 : : /* We've computed 'num' above to statically two
11317 : : or via constant_multiple_p. */
11318 : 1049 : unsigned num
11319 : 1049 : = (exact_div (tree_to_poly_uint64
11320 : 1049 : (TYPE_SIZE_UNIT (vectype)),
11321 : : tree_to_poly_uint64
11322 : 1049 : (TYPE_SIZE_UNIT (ltype)))
11323 : 1049 : .to_constant ());
11324 : 1049 : vec_alloc (v, num);
11325 : 1049 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11326 : : {
11327 : 54 : while (--num)
11328 : 54 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11329 : : build_zero_cst (ltype));
11330 : 21 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11331 : : }
11332 : : else
11333 : : {
11334 : 1028 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11335 : 1028 : while (--num)
11336 : 2418 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11337 : : build_zero_cst (ltype));
11338 : : }
11339 : 1049 : gcc_assert (new_vtype != NULL_TREE);
11340 : 1049 : if (new_vtype == vectype)
11341 : 1019 : new_stmt
11342 : 1019 : = gimple_build_assign (vec_dest,
11343 : : build_constructor (vectype, v));
11344 : : else
11345 : : {
11346 : 30 : tree new_vname = make_ssa_name (new_vtype);
11347 : 30 : new_stmt
11348 : 30 : = gimple_build_assign (new_vname,
11349 : : build_constructor (new_vtype,
11350 : : v));
11351 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
11352 : : new_stmt, gsi);
11353 : 30 : new_stmt
11354 : 30 : = gimple_build_assign (vec_dest,
11355 : : build1 (VIEW_CONVERT_EXPR,
11356 : : vectype, new_vname));
11357 : : }
11358 : : }
11359 : : }
11360 : : break;
11361 : : }
11362 : 0 : case dr_explicit_realign:
11363 : 0 : {
11364 : 0 : if (costing_p)
11365 : : break;
11366 : 0 : tree ptr, bump;
11367 : :
11368 : 0 : tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11369 : :
11370 : 0 : if (compute_in_loop)
11371 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype,
11372 : : gsi, &realignment_token,
11373 : : dr_explicit_realign,
11374 : : dataref_ptr, NULL);
11375 : :
11376 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11377 : 0 : ptr = copy_ssa_name (dataref_ptr);
11378 : : else
11379 : 0 : ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
11380 : : // For explicit realign the target alignment should be
11381 : : // known at compile time.
11382 : 0 : unsigned HOST_WIDE_INT align
11383 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11384 : 0 : new_stmt = gimple_build_assign (ptr, BIT_AND_EXPR, dataref_ptr,
11385 : : build_int_cst
11386 : 0 : (TREE_TYPE (dataref_ptr),
11387 : 0 : -(HOST_WIDE_INT) align));
11388 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11389 : 0 : data_ref = build2 (MEM_REF, vectype,
11390 : : ptr, build_int_cst (ref_type, 0));
11391 : 0 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11392 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11393 : 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11394 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11395 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11396 : 0 : gimple_move_vops (new_stmt, stmt_info->stmt);
11397 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11398 : 0 : msq = new_temp;
11399 : :
11400 : 0 : bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
11401 : 0 : bump = size_binop (MINUS_EXPR, bump, size_one_node);
11402 : 0 : ptr = bump_vector_ptr (vinfo, dataref_ptr, NULL, gsi, stmt_info,
11403 : : bump);
11404 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR, ptr,
11405 : 0 : build_int_cst (TREE_TYPE (ptr),
11406 : 0 : -(HOST_WIDE_INT) align));
11407 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
11408 : 0 : ptr = copy_ssa_name (ptr, new_stmt);
11409 : : else
11410 : 0 : ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
11411 : 0 : gimple_assign_set_lhs (new_stmt, ptr);
11412 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11413 : 0 : data_ref = build2 (MEM_REF, vectype,
11414 : : ptr, build_int_cst (ref_type, 0));
11415 : 0 : break;
11416 : : }
11417 : 0 : case dr_explicit_realign_optimized:
11418 : 0 : {
11419 : 0 : if (costing_p)
11420 : : break;
11421 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11422 : 0 : new_temp = copy_ssa_name (dataref_ptr);
11423 : : else
11424 : 0 : new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
11425 : : // We should only be doing this if we know the target
11426 : : // alignment at compile time.
11427 : 0 : unsigned HOST_WIDE_INT align
11428 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11429 : 0 : new_stmt = gimple_build_assign (new_temp, BIT_AND_EXPR, dataref_ptr,
11430 : 0 : build_int_cst (TREE_TYPE (dataref_ptr),
11431 : 0 : -(HOST_WIDE_INT) align));
11432 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11433 : 0 : data_ref = build2 (MEM_REF, vectype, new_temp,
11434 : : build_int_cst (ref_type, 0));
11435 : 0 : break;
11436 : : }
11437 : 0 : default:
11438 : 0 : gcc_unreachable ();
11439 : : }
11440 : :
11441 : : /* One common place to cost the above vect load for different
11442 : : alignment support schemes. */
11443 : 816025 : if (costing_p)
11444 : : {
11445 : : /* For the prologue cost for realign,
11446 : : we only need to count it once for the whole group. */
11447 : 604957 : bool first_stmt_info_p = first_stmt_info == stmt_info;
11448 : 604957 : bool add_realign_cost = first_stmt_info_p && i == 0;
11449 : 604957 : if (memory_access_type == VMAT_CONTIGUOUS
11450 : 604957 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11451 : : {
11452 : : /* Leave realign cases alone to keep them simple. */
11453 : 604957 : if (alignment_support_scheme == dr_explicit_realign_optimized
11454 : : || alignment_support_scheme == dr_explicit_realign)
11455 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
11456 : : alignment_support_scheme, misalignment,
11457 : : add_realign_cost, &inside_cost,
11458 : : &prologue_cost, cost_vec, cost_vec,
11459 : : true);
11460 : : else
11461 : 604957 : n_adjacent_loads++;
11462 : : }
11463 : : }
11464 : : else
11465 : : {
11466 : 211068 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11467 : : /* DATA_REF is null if we've already built the statement. */
11468 : 211068 : if (data_ref)
11469 : : {
11470 : 209435 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11471 : 209435 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11472 : : }
11473 : :
11474 : 422136 : new_temp = (need_zeroing
11475 : 211068 : ? make_ssa_name (vectype)
11476 : 211068 : : make_ssa_name (vec_dest, new_stmt));
11477 : 211068 : gimple_set_lhs (new_stmt, new_temp);
11478 : 211068 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11479 : :
11480 : : /* If we need to explicitly zero inactive elements emit a
11481 : : VEC_COND_EXPR that does so. */
11482 : 211068 : if (need_zeroing)
11483 : : {
11484 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11485 : : vectype);
11486 : :
11487 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11488 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11489 : : final_mask, new_temp, vec_els);
11490 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11491 : : gsi);
11492 : 0 : new_temp = new_temp2;
11493 : : }
11494 : : }
11495 : :
11496 : : /* 3. Handle explicit realignment if necessary/supported.
11497 : : Create in loop:
11498 : : vec_dest = realign_load (msq, lsq, realignment_token) */
11499 : 816025 : if (!costing_p
11500 : 211068 : && (alignment_support_scheme == dr_explicit_realign_optimized
11501 : : || alignment_support_scheme == dr_explicit_realign))
11502 : : {
11503 : 0 : lsq = gimple_assign_lhs (new_stmt);
11504 : 0 : if (!realignment_token)
11505 : 0 : realignment_token = dataref_ptr;
11506 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11507 : 0 : new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
11508 : : lsq, realignment_token);
11509 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11510 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11511 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11512 : :
11513 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
11514 : : {
11515 : 0 : gcc_assert (phi);
11516 : 0 : if (i == vec_num - 1)
11517 : 0 : add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
11518 : : UNKNOWN_LOCATION);
11519 : : msq = lsq;
11520 : : }
11521 : : }
11522 : :
11523 : 816025 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11524 : : {
11525 : 5840 : if (costing_p)
11526 : 4424 : inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
11527 : : slp_node, 0, vect_body);
11528 : : else
11529 : : {
11530 : 1416 : tree perm_mask = perm_mask_for_reverse (vectype);
11531 : 1416 : new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
11532 : : perm_mask, stmt_info, gsi);
11533 : 1416 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
11534 : : }
11535 : : }
11536 : :
11537 : : /* Collect vector loads and later create their permutation in
11538 : : vect_transform_slp_perm_load. */
11539 : 816025 : if (!costing_p && (grouped_load || ls.slp_perm))
11540 : 34224 : dr_chain.quick_push (new_temp);
11541 : :
11542 : : /* Store vector loads in the corresponding SLP_NODE. */
11543 : 211068 : if (!costing_p && !ls.slp_perm)
11544 : 176844 : slp_node->push_vec_def (new_stmt);
11545 : :
11546 : : /* With SLP permutation we load the gaps as well, without
11547 : : we need to skip the gaps after we manage to fully load
11548 : : all elements. group_gap_adj is DR_GROUP_SIZE here. */
11549 : 816025 : group_elt += nunits;
11550 : 816025 : if (!costing_p
11551 : 211068 : && maybe_ne (group_gap_adj, 0U)
11552 : 19219 : && !ls.slp_perm
11553 : 834814 : && known_eq (group_elt, group_size - group_gap_adj))
11554 : : {
11555 : 15522 : poly_wide_int bump_val
11556 : 15522 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11557 : 15522 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11558 : 0 : bump_val = -bump_val;
11559 : 15522 : tree bump = wide_int_to_tree (sizetype, bump_val);
11560 : 15522 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11561 : : stmt_info, bump);
11562 : 15522 : group_elt = 0;
11563 : 15522 : }
11564 : : }
11565 : : /* Bump the vector pointer to account for a gap or for excess
11566 : : elements loaded for a permuted SLP load. */
11567 : 503025 : if (!costing_p
11568 : 158779 : && maybe_ne (group_gap_adj, 0U)
11569 : 518751 : && ls.slp_perm)
11570 : : {
11571 : 204 : poly_wide_int bump_val
11572 : 204 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11573 : 204 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11574 : 9 : bump_val = -bump_val;
11575 : 204 : tree bump = wide_int_to_tree (sizetype, bump_val);
11576 : 204 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11577 : : stmt_info, bump);
11578 : 204 : }
11579 : :
11580 : 503025 : if (ls.slp_perm)
11581 : : {
11582 : : /* For SLP we know we've seen all possible uses of dr_chain so
11583 : : direct vect_transform_slp_perm_load to DCE the unused parts.
11584 : : ??? This is a hack to prevent compile-time issues as seen
11585 : : in PR101120 and friends. */
11586 : 46729 : if (costing_p)
11587 : : {
11588 : 30135 : gcc_assert (ls.n_perms != -1U);
11589 : 30135 : if (ls.n_perms != 0)
11590 : 29741 : inside_cost = record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
11591 : : slp_node, 0, vect_body);
11592 : : }
11593 : : else
11594 : : {
11595 : 16594 : unsigned n_perms2;
11596 : 16594 : bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
11597 : : gsi, vf, false, &n_perms2,
11598 : : nullptr, true);
11599 : 16594 : gcc_assert (ok && ls.n_perms == n_perms2);
11600 : : }
11601 : 46729 : dr_chain.release ();
11602 : : }
11603 : :
11604 : 503025 : if (costing_p)
11605 : : {
11606 : 344246 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
11607 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
11608 : 344246 : if (n_adjacent_loads > 0)
11609 : 344246 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11610 : : alignment_support_scheme, misalignment, false,
11611 : : &inside_cost, &prologue_cost, cost_vec, cost_vec,
11612 : : true);
11613 : 344246 : if (dump_enabled_p ())
11614 : 22554 : dump_printf_loc (MSG_NOTE, vect_location,
11615 : : "vect_model_load_cost: inside_cost = %u, "
11616 : : "prologue_cost = %u .\n",
11617 : : inside_cost, prologue_cost);
11618 : : }
11619 : :
11620 : : return true;
11621 : 1133269 : }
11622 : :
11623 : : /* Function vect_is_simple_cond.
11624 : :
11625 : : Input:
11626 : : LOOP - the loop that is being vectorized.
11627 : : COND - Condition that is checked for simple use.
11628 : :
11629 : : Output:
11630 : : *COMP_VECTYPE - the vector type for the comparison.
11631 : : *DTS - The def types for the arguments of the comparison
11632 : :
11633 : : Returns whether a COND can be vectorized. Checks whether
11634 : : condition operands are supportable using vec_is_simple_use. */
11635 : :
11636 : : static bool
11637 : 28020 : vect_is_simple_cond (tree cond, vec_info *vinfo,
11638 : : slp_tree slp_node, tree *comp_vectype,
11639 : : enum vect_def_type *dts, tree vectype)
11640 : : {
11641 : 28020 : tree lhs, rhs;
11642 : 28020 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
11643 : 28020 : slp_tree slp_op;
11644 : :
11645 : : /* Mask case. */
11646 : 28020 : if (TREE_CODE (cond) == SSA_NAME
11647 : 28020 : && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
11648 : : {
11649 : 28008 : if (!vect_is_simple_use (vinfo, slp_node, 0, &cond,
11650 : : &slp_op, &dts[0], comp_vectype)
11651 : 28008 : || !*comp_vectype
11652 : 55997 : || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
11653 : : return false;
11654 : : return true;
11655 : : }
11656 : :
11657 : 12 : if (!COMPARISON_CLASS_P (cond))
11658 : : return false;
11659 : :
11660 : 0 : lhs = TREE_OPERAND (cond, 0);
11661 : 0 : rhs = TREE_OPERAND (cond, 1);
11662 : :
11663 : 0 : if (TREE_CODE (lhs) == SSA_NAME)
11664 : : {
11665 : 0 : if (!vect_is_simple_use (vinfo, slp_node, 0,
11666 : : &lhs, &slp_op, &dts[0], &vectype1))
11667 : : return false;
11668 : : }
11669 : 0 : else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
11670 : 0 : || TREE_CODE (lhs) == FIXED_CST)
11671 : 0 : dts[0] = vect_constant_def;
11672 : : else
11673 : : return false;
11674 : :
11675 : 0 : if (TREE_CODE (rhs) == SSA_NAME)
11676 : : {
11677 : 0 : if (!vect_is_simple_use (vinfo, slp_node, 1,
11678 : : &rhs, &slp_op, &dts[1], &vectype2))
11679 : : return false;
11680 : : }
11681 : 0 : else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
11682 : 0 : || TREE_CODE (rhs) == FIXED_CST)
11683 : 0 : dts[1] = vect_constant_def;
11684 : : else
11685 : : return false;
11686 : :
11687 : 0 : if (vectype1 && vectype2
11688 : 0 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
11689 : 0 : TYPE_VECTOR_SUBPARTS (vectype2)))
11690 : 0 : return false;
11691 : :
11692 : 0 : *comp_vectype = vectype1 ? vectype1 : vectype2;
11693 : : /* Invariant comparison. */
11694 : 0 : if (! *comp_vectype)
11695 : : {
11696 : 0 : tree scalar_type = TREE_TYPE (lhs);
11697 : 0 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
11698 : 0 : *comp_vectype = truth_type_for (vectype);
11699 : : else
11700 : : {
11701 : : /* If we can widen the comparison to match vectype do so. */
11702 : 0 : if (INTEGRAL_TYPE_P (scalar_type)
11703 : 0 : && !slp_node
11704 : 0 : && tree_int_cst_lt (TYPE_SIZE (scalar_type),
11705 : 0 : TYPE_SIZE (TREE_TYPE (vectype))))
11706 : 0 : scalar_type = build_nonstandard_integer_type
11707 : 0 : (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
11708 : 0 : *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
11709 : : slp_node);
11710 : : }
11711 : : }
11712 : :
11713 : : return true;
11714 : : }
11715 : :
11716 : : /* vectorizable_condition.
11717 : :
11718 : : Check if STMT_INFO is conditional modify expression that can be vectorized.
11719 : : If COST_VEC is passed, calculate costs but don't change anything,
11720 : : otherwise, vectorize STMT_INFO: create a vectorized stmt using
11721 : : VEC_COND_EXPR to replace it, and insert it at GSI.
11722 : :
11723 : : When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
11724 : :
11725 : : Return true if STMT_INFO is vectorizable in this way. */
11726 : :
11727 : : static bool
11728 : 585718 : vectorizable_condition (vec_info *vinfo,
11729 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
11730 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
11731 : : {
11732 : 585718 : tree scalar_dest = NULL_TREE;
11733 : 585718 : tree vec_dest = NULL_TREE;
11734 : 585718 : tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
11735 : 585718 : tree then_clause, else_clause;
11736 : 585718 : tree comp_vectype = NULL_TREE;
11737 : 585718 : tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
11738 : 585718 : tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
11739 : 585718 : tree vec_compare;
11740 : 585718 : tree new_temp;
11741 : 585718 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
11742 : 585718 : enum vect_def_type dts[4]
11743 : : = {vect_unknown_def_type, vect_unknown_def_type,
11744 : : vect_unknown_def_type, vect_unknown_def_type};
11745 : 585718 : enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
11746 : 585718 : int i;
11747 : 585718 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
11748 : 585718 : vec<tree> vec_oprnds0 = vNULL;
11749 : 585718 : vec<tree> vec_oprnds1 = vNULL;
11750 : 585718 : vec<tree> vec_oprnds2 = vNULL;
11751 : 585718 : vec<tree> vec_oprnds3 = vNULL;
11752 : 585718 : tree vec_cmp_type;
11753 : 585718 : bool masked = false;
11754 : :
11755 : 585718 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
11756 : : return false;
11757 : :
11758 : : /* Is vectorizable conditional operation? */
11759 : 902296 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
11760 : 344565 : if (!stmt)
11761 : : return false;
11762 : :
11763 : 344565 : code = gimple_assign_rhs_code (stmt);
11764 : 344565 : if (code != COND_EXPR)
11765 : : return false;
11766 : :
11767 : 28020 : int reduc_index = SLP_TREE_REDUC_IDX (slp_node);
11768 : 28020 : vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
11769 : 28020 : bool nested_cycle_p = false;
11770 : 28020 : bool for_reduction = vect_is_reduction (stmt_info);
11771 : 28020 : if (for_reduction)
11772 : : {
11773 : 493 : if (SLP_TREE_LANES (slp_node) > 1)
11774 : : return false;
11775 : : /* ??? With a reduction path we do not get at the reduction info from
11776 : : every stmt, use the conservative default setting then. */
11777 : 569 : if (STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)))
11778 : : {
11779 : 467 : vect_reduc_info reduc_info
11780 : 467 : = info_for_reduction (loop_vinfo, slp_node);
11781 : 467 : reduction_type = VECT_REDUC_INFO_TYPE (reduc_info);
11782 : 467 : nested_cycle_p = nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
11783 : : stmt_info);
11784 : : }
11785 : : }
11786 : : else
11787 : : {
11788 : 27527 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
11789 : : return false;
11790 : : }
11791 : :
11792 : 28020 : tree vectype = SLP_TREE_VECTYPE (slp_node);
11793 : 28020 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
11794 : :
11795 : 28020 : int vec_num = vect_get_num_copies (vinfo, slp_node);
11796 : :
11797 : 28020 : cond_expr = gimple_assign_rhs1 (stmt);
11798 : 28020 : gcc_assert (! COMPARISON_CLASS_P (cond_expr));
11799 : :
11800 : 28020 : if (!vect_is_simple_cond (cond_expr, vinfo, slp_node,
11801 : : &comp_vectype, &dts[0], vectype)
11802 : 28020 : || !comp_vectype)
11803 : : return false;
11804 : :
11805 : 27989 : unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
11806 : 27989 : slp_tree then_slp_node, else_slp_node;
11807 : 27989 : if (!vect_is_simple_use (vinfo, slp_node, 1 + op_adjust,
11808 : : &then_clause, &then_slp_node, &dts[2], &vectype1))
11809 : : return false;
11810 : 27989 : if (!vect_is_simple_use (vinfo, slp_node, 2 + op_adjust,
11811 : : &else_clause, &else_slp_node, &dts[3], &vectype2))
11812 : : return false;
11813 : :
11814 : 27989 : if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
11815 : : return false;
11816 : :
11817 : 27989 : if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
11818 : : return false;
11819 : :
11820 : 27989 : masked = !COMPARISON_CLASS_P (cond_expr);
11821 : 27989 : vec_cmp_type = truth_type_for (comp_vectype);
11822 : 27989 : if (vec_cmp_type == NULL_TREE
11823 : 55978 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
11824 : 27989 : TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
11825 : 0 : return false;
11826 : :
11827 : 27989 : cond_code = TREE_CODE (cond_expr);
11828 : 27989 : if (!masked)
11829 : : {
11830 : 0 : cond_expr0 = TREE_OPERAND (cond_expr, 0);
11831 : 0 : cond_expr1 = TREE_OPERAND (cond_expr, 1);
11832 : : }
11833 : :
11834 : : /* For conditional reductions, the "then" value needs to be the candidate
11835 : : value calculated by this iteration while the "else" value needs to be
11836 : : the result carried over from previous iterations. If the COND_EXPR
11837 : : is the other way around, we need to swap it. */
11838 : 27989 : bool must_invert_cmp_result = false;
11839 : 27989 : if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
11840 : : {
11841 : 0 : if (masked)
11842 : 0 : must_invert_cmp_result = true;
11843 : : else
11844 : : {
11845 : 0 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
11846 : 0 : tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
11847 : 0 : if (new_code == ERROR_MARK)
11848 : : must_invert_cmp_result = true;
11849 : : else
11850 : : {
11851 : 0 : cond_code = new_code;
11852 : : /* Make sure we don't accidentally use the old condition. */
11853 : 0 : cond_expr = NULL_TREE;
11854 : : }
11855 : : }
11856 : : /* ??? The vectorized operand query below doesn't allow swapping
11857 : : this way for SLP. */
11858 : 0 : return false;
11859 : : /* std::swap (then_clause, else_clause); */
11860 : : }
11861 : :
11862 : 27989 : if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
11863 : : {
11864 : : /* Boolean values may have another representation in vectors
11865 : : and therefore we prefer bit operations over comparison for
11866 : : them (which also works for scalar masks). We store opcodes
11867 : : to use in bitop1 and bitop2. Statement is vectorized as
11868 : : BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
11869 : : depending on bitop1 and bitop2 arity. */
11870 : 0 : switch (cond_code)
11871 : : {
11872 : : case GT_EXPR:
11873 : : bitop1 = BIT_NOT_EXPR;
11874 : : bitop2 = BIT_AND_EXPR;
11875 : : break;
11876 : 0 : case GE_EXPR:
11877 : 0 : bitop1 = BIT_NOT_EXPR;
11878 : 0 : bitop2 = BIT_IOR_EXPR;
11879 : 0 : break;
11880 : 0 : case LT_EXPR:
11881 : 0 : bitop1 = BIT_NOT_EXPR;
11882 : 0 : bitop2 = BIT_AND_EXPR;
11883 : 0 : std::swap (cond_expr0, cond_expr1);
11884 : 0 : break;
11885 : 0 : case LE_EXPR:
11886 : 0 : bitop1 = BIT_NOT_EXPR;
11887 : 0 : bitop2 = BIT_IOR_EXPR;
11888 : 0 : std::swap (cond_expr0, cond_expr1);
11889 : 0 : break;
11890 : 0 : case NE_EXPR:
11891 : 0 : bitop1 = BIT_XOR_EXPR;
11892 : 0 : break;
11893 : 0 : case EQ_EXPR:
11894 : 0 : bitop1 = BIT_XOR_EXPR;
11895 : 0 : bitop2 = BIT_NOT_EXPR;
11896 : 0 : break;
11897 : : default:
11898 : : return false;
11899 : : }
11900 : : cond_code = SSA_NAME;
11901 : : }
11902 : :
11903 : 27989 : if (TREE_CODE_CLASS (cond_code) == tcc_comparison
11904 : 0 : && reduction_type == EXTRACT_LAST_REDUCTION
11905 : 27989 : && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
11906 : : {
11907 : 0 : if (dump_enabled_p ())
11908 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11909 : : "reduction comparison operation not supported.\n");
11910 : 0 : return false;
11911 : : }
11912 : :
11913 : 27989 : if (cost_vec)
11914 : : {
11915 : 19949 : if (bitop1 != NOP_EXPR)
11916 : : {
11917 : 0 : machine_mode mode = TYPE_MODE (comp_vectype);
11918 : 0 : optab optab;
11919 : :
11920 : 0 : optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
11921 : 0 : if (!optab || !can_implement_p (optab, mode))
11922 : 0 : return false;
11923 : :
11924 : 0 : if (bitop2 != NOP_EXPR)
11925 : : {
11926 : 0 : optab = optab_for_tree_code (bitop2, comp_vectype,
11927 : : optab_default);
11928 : 0 : if (!optab || !can_implement_p (optab, mode))
11929 : 0 : return false;
11930 : : }
11931 : : }
11932 : :
11933 : 19949 : vect_cost_for_stmt kind = vector_stmt;
11934 : 19949 : if (reduction_type == EXTRACT_LAST_REDUCTION)
11935 : : /* Count one reduction-like operation per vector. */
11936 : : kind = vec_to_scalar;
11937 : 19949 : else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
11938 : 19949 : || (!masked
11939 : 0 : && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
11940 : : cond_code)
11941 : 0 : || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
11942 : 2 : return false;
11943 : :
11944 : 19947 : if (!vect_maybe_update_slp_op_vectype (SLP_TREE_CHILDREN (slp_node)[0],
11945 : : comp_vectype)
11946 : 19947 : || (op_adjust == 1
11947 : 0 : && !vect_maybe_update_slp_op_vectype
11948 : 0 : (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
11949 : 19947 : || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
11950 : 39894 : || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype))
11951 : : {
11952 : 0 : if (dump_enabled_p ())
11953 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11954 : : "incompatible vector types for invariants\n");
11955 : 0 : return false;
11956 : : }
11957 : :
11958 : 19947 : if (loop_vinfo && for_reduction
11959 : 338 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
11960 : : {
11961 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
11962 : : {
11963 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
11964 : : vectype, OPTIMIZE_FOR_SPEED))
11965 : 0 : vect_record_loop_len (loop_vinfo,
11966 : : &LOOP_VINFO_LENS (loop_vinfo),
11967 : : vec_num, vectype, 1);
11968 : : else
11969 : 0 : vect_record_loop_mask (loop_vinfo,
11970 : : &LOOP_VINFO_MASKS (loop_vinfo),
11971 : : vec_num, vectype, NULL);
11972 : : }
11973 : : /* Extra inactive lanes should be safe for vect_nested_cycle. */
11974 : 0 : else if (!nested_cycle_p)
11975 : : {
11976 : 0 : if (dump_enabled_p ())
11977 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
11978 : : "conditional reduction prevents the use"
11979 : : " of partial vectors.\n");
11980 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
11981 : : }
11982 : : }
11983 : :
11984 : 19947 : SLP_TREE_TYPE (slp_node) = condition_vec_info_type;
11985 : 19947 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec, kind);
11986 : 19947 : return true;
11987 : : }
11988 : :
11989 : : /* Transform. */
11990 : :
11991 : : /* Handle def. */
11992 : 8040 : scalar_dest = gimple_assign_lhs (stmt);
11993 : 8040 : if (reduction_type != EXTRACT_LAST_REDUCTION)
11994 : 8040 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11995 : :
11996 : 8040 : bool swap_cond_operands = false;
11997 : :
11998 : : /* See whether another part of the vectorized code applies a loop
11999 : : mask to the condition, or to its inverse. */
12000 : :
12001 : 8040 : vec_loop_masks *masks = NULL;
12002 : 8040 : vec_loop_lens *lens = NULL;
12003 : 8040 : if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12004 : : {
12005 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12006 : 0 : lens = &LOOP_VINFO_LENS (loop_vinfo);
12007 : : }
12008 : 8040 : else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
12009 : : {
12010 : 3 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12011 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12012 : : else
12013 : : {
12014 : 3 : scalar_cond_masked_key cond (cond_expr, 1);
12015 : 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12016 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12017 : : else
12018 : : {
12019 : 3 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
12020 : 3 : tree_code orig_code = cond.code;
12021 : 3 : cond.code = invert_tree_comparison (cond.code, honor_nans);
12022 : 3 : if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
12023 : : {
12024 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12025 : 0 : cond_code = cond.code;
12026 : 0 : swap_cond_operands = true;
12027 : : }
12028 : : else
12029 : : {
12030 : : /* Try the inverse of the current mask. We check if the
12031 : : inverse mask is live and if so we generate a negate of
12032 : : the current mask such that we still honor NaNs. */
12033 : 3 : cond.inverted_p = true;
12034 : 3 : cond.code = orig_code;
12035 : 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12036 : : {
12037 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12038 : 0 : cond_code = cond.code;
12039 : 0 : swap_cond_operands = true;
12040 : 0 : must_invert_cmp_result = true;
12041 : : }
12042 : : }
12043 : : }
12044 : : }
12045 : : }
12046 : :
12047 : : /* Handle cond expr. */
12048 : 8040 : if (masked)
12049 : 8040 : vect_get_vec_defs (vinfo, slp_node,
12050 : : cond_expr, &vec_oprnds0,
12051 : : then_clause, &vec_oprnds2,
12052 : : reduction_type != EXTRACT_LAST_REDUCTION
12053 : : ? else_clause : NULL, &vec_oprnds3);
12054 : : else
12055 : 0 : vect_get_vec_defs (vinfo, slp_node,
12056 : : cond_expr0, &vec_oprnds0,
12057 : : cond_expr1, &vec_oprnds1,
12058 : : then_clause, &vec_oprnds2,
12059 : : reduction_type != EXTRACT_LAST_REDUCTION
12060 : : ? else_clause : NULL, &vec_oprnds3);
12061 : :
12062 : 8040 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12063 : 0 : vec_else_clause = else_clause;
12064 : :
12065 : : /* Arguments are ready. Create the new vector stmt. */
12066 : 19067 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
12067 : : {
12068 : 11027 : vec_then_clause = vec_oprnds2[i];
12069 : 11027 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12070 : 11027 : vec_else_clause = vec_oprnds3[i];
12071 : :
12072 : 11027 : if (swap_cond_operands)
12073 : 0 : std::swap (vec_then_clause, vec_else_clause);
12074 : :
12075 : 11027 : if (masked)
12076 : : vec_compare = vec_cond_lhs;
12077 : : else
12078 : : {
12079 : 0 : vec_cond_rhs = vec_oprnds1[i];
12080 : 0 : if (bitop1 == NOP_EXPR)
12081 : : {
12082 : 0 : gimple_seq stmts = NULL;
12083 : 0 : vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
12084 : : vec_cond_lhs, vec_cond_rhs);
12085 : 0 : gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
12086 : : }
12087 : : else
12088 : : {
12089 : 0 : new_temp = make_ssa_name (vec_cmp_type);
12090 : 0 : gassign *new_stmt;
12091 : 0 : if (bitop1 == BIT_NOT_EXPR)
12092 : 0 : new_stmt = gimple_build_assign (new_temp, bitop1,
12093 : : vec_cond_rhs);
12094 : : else
12095 : 0 : new_stmt
12096 : 0 : = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
12097 : : vec_cond_rhs);
12098 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12099 : 0 : if (bitop2 == NOP_EXPR)
12100 : : vec_compare = new_temp;
12101 : 0 : else if (bitop2 == BIT_NOT_EXPR
12102 : 0 : && reduction_type != EXTRACT_LAST_REDUCTION)
12103 : : {
12104 : : /* Instead of doing ~x ? y : z do x ? z : y. */
12105 : : vec_compare = new_temp;
12106 : : std::swap (vec_then_clause, vec_else_clause);
12107 : : }
12108 : : else
12109 : : {
12110 : 0 : vec_compare = make_ssa_name (vec_cmp_type);
12111 : 0 : if (bitop2 == BIT_NOT_EXPR)
12112 : 0 : new_stmt
12113 : 0 : = gimple_build_assign (vec_compare, bitop2, new_temp);
12114 : : else
12115 : 0 : new_stmt
12116 : 0 : = gimple_build_assign (vec_compare, bitop2,
12117 : : vec_cond_lhs, new_temp);
12118 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
12119 : : new_stmt, gsi);
12120 : : }
12121 : : }
12122 : : }
12123 : :
12124 : : /* If we decided to apply a loop mask to the result of the vector
12125 : : comparison, AND the comparison with the mask now. Later passes
12126 : : should then be able to reuse the AND results between mulitple
12127 : : vector statements.
12128 : :
12129 : : For example:
12130 : : for (int i = 0; i < 100; ++i)
12131 : : x[i] = y[i] ? z[i] : 10;
12132 : :
12133 : : results in following optimized GIMPLE:
12134 : :
12135 : : mask__35.8_43 = vect__4.7_41 != { 0, ... };
12136 : : vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
12137 : : _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
12138 : : vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
12139 : : vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
12140 : : vect_iftmp.11_47, { 10, ... }>;
12141 : :
12142 : : instead of using a masked and unmasked forms of
12143 : : vec != { 0, ... } (masked in the MASK_LOAD,
12144 : : unmasked in the VEC_COND_EXPR). */
12145 : :
12146 : : /* Force vec_compare to be an SSA_NAME rather than a comparison,
12147 : : in cases where that's necessary. */
12148 : :
12149 : 11027 : tree len = NULL_TREE, bias = NULL_TREE;
12150 : 11027 : if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
12151 : : {
12152 : 0 : if (!is_gimple_val (vec_compare))
12153 : : {
12154 : 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12155 : 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12156 : : vec_compare);
12157 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12158 : 0 : vec_compare = vec_compare_name;
12159 : : }
12160 : :
12161 : 0 : if (must_invert_cmp_result)
12162 : : {
12163 : 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12164 : 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12165 : : BIT_NOT_EXPR,
12166 : : vec_compare);
12167 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12168 : 0 : vec_compare = vec_compare_name;
12169 : : }
12170 : :
12171 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12172 : : vectype, OPTIMIZE_FOR_SPEED))
12173 : : {
12174 : 0 : if (lens)
12175 : : {
12176 : 0 : len = vect_get_loop_len (loop_vinfo, gsi, lens,
12177 : : vec_num, vectype, i, 1);
12178 : 0 : signed char biasval
12179 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
12180 : 0 : bias = build_int_cst (intQI_type_node, biasval);
12181 : : }
12182 : : else
12183 : : {
12184 : 0 : len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12185 : 0 : bias = build_int_cst (intQI_type_node, 0);
12186 : : }
12187 : : }
12188 : 0 : if (masks)
12189 : : {
12190 : 0 : tree loop_mask
12191 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
12192 : : vectype, i);
12193 : 0 : tree tmp2 = make_ssa_name (vec_cmp_type);
12194 : 0 : gassign *g
12195 : 0 : = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
12196 : : loop_mask);
12197 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
12198 : 0 : vec_compare = tmp2;
12199 : : }
12200 : : }
12201 : :
12202 : 0 : gimple *new_stmt;
12203 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12204 : : {
12205 : 0 : gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
12206 : 0 : tree lhs = gimple_get_lhs (old_stmt);
12207 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12208 : 0 : lhs = copy_ssa_name (lhs);
12209 : 0 : if (len)
12210 : 0 : new_stmt = gimple_build_call_internal
12211 : 0 : (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
12212 : : vec_then_clause, len, bias);
12213 : : else
12214 : 0 : new_stmt = gimple_build_call_internal
12215 : 0 : (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
12216 : : vec_then_clause);
12217 : 0 : gimple_call_set_lhs (new_stmt, lhs);
12218 : 0 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12219 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12220 : : {
12221 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12222 : 0 : vec_else_clause = lhs;
12223 : : }
12224 : 0 : else if (old_stmt == gsi_stmt (*gsi))
12225 : 0 : vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
12226 : : else
12227 : : {
12228 : : /* In this case we're moving the definition to later in the
12229 : : block. That doesn't matter because the only uses of the
12230 : : lhs are in phi statements. */
12231 : 0 : gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
12232 : 0 : gsi_remove (&old_gsi, true);
12233 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12234 : : }
12235 : : }
12236 : : else
12237 : : {
12238 : 11027 : new_temp = make_ssa_name (vec_dest);
12239 : 11027 : new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
12240 : : vec_then_clause, vec_else_clause);
12241 : 11027 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12242 : : }
12243 : 11027 : slp_node->push_vec_def (new_stmt);
12244 : : }
12245 : :
12246 : 8040 : vec_oprnds0.release ();
12247 : 8040 : vec_oprnds1.release ();
12248 : 8040 : vec_oprnds2.release ();
12249 : 8040 : vec_oprnds3.release ();
12250 : :
12251 : 8040 : return true;
12252 : : }
12253 : :
12254 : : /* Helper of vectorizable_comparison.
12255 : :
12256 : : Check if STMT_INFO is comparison expression CODE that can be vectorized.
12257 : : If COST_VEC is passed, calculate costs but don't change anything,
12258 : : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12259 : : it at GSI.
12260 : :
12261 : : Return true if STMT_INFO is vectorizable in this way. */
12262 : :
12263 : : static bool
12264 : 323928 : vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
12265 : : stmt_vec_info stmt_info, tree_code code,
12266 : : gimple_stmt_iterator *gsi,
12267 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12268 : : {
12269 : 323928 : tree lhs, rhs1, rhs2;
12270 : 323928 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12271 : 323928 : tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
12272 : 323928 : tree new_temp;
12273 : 323928 : enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
12274 : 323928 : poly_uint64 nunits;
12275 : 323928 : enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12276 : 323928 : int i;
12277 : 323928 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12278 : 323928 : vec<tree> vec_oprnds0 = vNULL;
12279 : 323928 : vec<tree> vec_oprnds1 = vNULL;
12280 : 323928 : tree mask_type;
12281 : 323928 : tree mask = NULL_TREE;
12282 : :
12283 : 323928 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12284 : : return false;
12285 : :
12286 : 323928 : if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
12287 : : return false;
12288 : :
12289 : 145447 : mask_type = vectype;
12290 : 145447 : nunits = TYPE_VECTOR_SUBPARTS (vectype);
12291 : :
12292 : 145447 : if (TREE_CODE_CLASS (code) != tcc_comparison)
12293 : : return false;
12294 : :
12295 : 143804 : slp_tree slp_rhs1, slp_rhs2;
12296 : 143804 : if (!vect_is_simple_use (vinfo, slp_node,
12297 : : 0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
12298 : : return false;
12299 : :
12300 : 143804 : if (!vect_is_simple_use (vinfo, slp_node,
12301 : : 1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
12302 : : return false;
12303 : :
12304 : 109663 : if (vectype1 && vectype2
12305 : 209246 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12306 : 65442 : TYPE_VECTOR_SUBPARTS (vectype2)))
12307 : 16 : return false;
12308 : :
12309 : 143788 : vectype = vectype1 ? vectype1 : vectype2;
12310 : :
12311 : : /* Invariant comparison. */
12312 : 143788 : if (!vectype)
12313 : : {
12314 : 29667 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
12315 : 29667 : if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
12316 : 7 : return false;
12317 : : }
12318 : 114121 : else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
12319 : : return false;
12320 : :
12321 : : /* Can't compare mask and non-mask types. */
12322 : 109647 : if (vectype1 && vectype2
12323 : 339445 : && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
12324 : : return false;
12325 : :
12326 : : /* Boolean values may have another representation in vectors
12327 : : and therefore we prefer bit operations over comparison for
12328 : : them (which also works for scalar masks). We store opcodes
12329 : : to use in bitop1 and bitop2. Statement is vectorized as
12330 : : BITOP2 (rhs1 BITOP1 rhs2) or
12331 : : rhs1 BITOP2 (BITOP1 rhs2)
12332 : : depending on bitop1 and bitop2 arity. */
12333 : 143773 : bool swap_p = false;
12334 : 143773 : if (VECTOR_BOOLEAN_TYPE_P (vectype))
12335 : : {
12336 : 679 : if (code == GT_EXPR)
12337 : : {
12338 : : bitop1 = BIT_NOT_EXPR;
12339 : : bitop2 = BIT_AND_EXPR;
12340 : : }
12341 : : else if (code == GE_EXPR)
12342 : : {
12343 : : bitop1 = BIT_NOT_EXPR;
12344 : : bitop2 = BIT_IOR_EXPR;
12345 : : }
12346 : : else if (code == LT_EXPR)
12347 : : {
12348 : : bitop1 = BIT_NOT_EXPR;
12349 : : bitop2 = BIT_AND_EXPR;
12350 : : swap_p = true;
12351 : : }
12352 : : else if (code == LE_EXPR)
12353 : : {
12354 : : bitop1 = BIT_NOT_EXPR;
12355 : : bitop2 = BIT_IOR_EXPR;
12356 : : swap_p = true;
12357 : : }
12358 : : else
12359 : : {
12360 : : bitop1 = BIT_XOR_EXPR;
12361 : : if (code == EQ_EXPR)
12362 : : bitop2 = BIT_NOT_EXPR;
12363 : : }
12364 : : }
12365 : :
12366 : 143773 : if (cost_vec)
12367 : : {
12368 : 132343 : if (bitop1 == NOP_EXPR)
12369 : : {
12370 : 131814 : if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
12371 : : return false;
12372 : : }
12373 : : else
12374 : : {
12375 : 529 : machine_mode mode = TYPE_MODE (vectype);
12376 : 529 : optab optab;
12377 : :
12378 : 529 : optab = optab_for_tree_code (bitop1, vectype, optab_default);
12379 : 529 : if (!optab || !can_implement_p (optab, mode))
12380 : 0 : return false;
12381 : :
12382 : 529 : if (bitop2 != NOP_EXPR)
12383 : : {
12384 : 91 : optab = optab_for_tree_code (bitop2, vectype, optab_default);
12385 : 91 : if (!optab || !can_implement_p (optab, mode))
12386 : 0 : return false;
12387 : : }
12388 : : }
12389 : :
12390 : : /* Put types on constant and invariant SLP children. */
12391 : 123013 : if (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
12392 : 123013 : || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype))
12393 : : {
12394 : 2 : if (dump_enabled_p ())
12395 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12396 : : "incompatible vector types for invariants\n");
12397 : 2 : return false;
12398 : : }
12399 : :
12400 : 123011 : vect_model_simple_cost (vinfo, 1 + (bitop2 != NOP_EXPR),
12401 : : slp_node, cost_vec);
12402 : 123011 : return true;
12403 : : }
12404 : :
12405 : : /* Transform. */
12406 : :
12407 : : /* Handle def. */
12408 : 11430 : lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
12409 : 11430 : if (lhs)
12410 : 11430 : mask = vect_create_destination_var (lhs, mask_type);
12411 : :
12412 : 11430 : vect_get_vec_defs (vinfo, slp_node, rhs1, &vec_oprnds0, rhs2, &vec_oprnds1);
12413 : 11430 : if (swap_p)
12414 : 58 : std::swap (vec_oprnds0, vec_oprnds1);
12415 : :
12416 : : /* Arguments are ready. Create the new vector stmt. */
12417 : 28945 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
12418 : : {
12419 : 17515 : gimple *new_stmt;
12420 : 17515 : vec_rhs2 = vec_oprnds1[i];
12421 : :
12422 : 17515 : if (lhs)
12423 : 17515 : new_temp = make_ssa_name (mask);
12424 : : else
12425 : 0 : new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
12426 : 17515 : if (bitop1 == NOP_EXPR)
12427 : : {
12428 : 17360 : new_stmt = gimple_build_assign (new_temp, code,
12429 : : vec_rhs1, vec_rhs2);
12430 : 17360 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12431 : : }
12432 : : else
12433 : : {
12434 : 155 : if (bitop1 == BIT_NOT_EXPR)
12435 : 84 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
12436 : : else
12437 : 71 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
12438 : : vec_rhs2);
12439 : 155 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12440 : 155 : if (bitop2 != NOP_EXPR)
12441 : : {
12442 : 84 : tree res = make_ssa_name (mask);
12443 : 84 : if (bitop2 == BIT_NOT_EXPR)
12444 : 0 : new_stmt = gimple_build_assign (res, bitop2, new_temp);
12445 : : else
12446 : 84 : new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
12447 : : new_temp);
12448 : 84 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12449 : : }
12450 : : }
12451 : 17515 : slp_node->push_vec_def (new_stmt);
12452 : : }
12453 : :
12454 : 11430 : vec_oprnds0.release ();
12455 : 11430 : vec_oprnds1.release ();
12456 : :
12457 : 11430 : return true;
12458 : : }
12459 : :
12460 : : /* vectorizable_comparison.
12461 : :
12462 : : Check if STMT_INFO is comparison expression that can be vectorized.
12463 : : If COST_VEC is passed, calculate costs but don't change anything,
12464 : : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12465 : : it at GSI.
12466 : :
12467 : : Return true if STMT_INFO is vectorizable in this way. */
12468 : :
12469 : : static bool
12470 : 569161 : vectorizable_comparison (vec_info *vinfo,
12471 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12472 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12473 : : {
12474 : 569161 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12475 : :
12476 : 569161 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12477 : : return false;
12478 : :
12479 : 569161 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12480 : : return false;
12481 : :
12482 : 758648 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12483 : 321745 : if (!stmt)
12484 : : return false;
12485 : :
12486 : 321745 : enum tree_code code = gimple_assign_rhs_code (stmt);
12487 : 321745 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12488 : 321745 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
12489 : : slp_node, cost_vec))
12490 : : return false;
12491 : :
12492 : 132258 : if (cost_vec)
12493 : 120828 : SLP_TREE_TYPE (slp_node) = comparison_vec_info_type;
12494 : :
12495 : : return true;
12496 : : }
12497 : :
12498 : : /* Check to see if the current early break given in STMT_INFO is valid for
12499 : : vectorization. */
12500 : :
12501 : : bool
12502 : 213378 : vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
12503 : : gimple_stmt_iterator *gsi,
12504 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12505 : : {
12506 : 213378 : if (!is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
12507 : : return false;
12508 : :
12509 : 58288 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
12510 : : return false;
12511 : :
12512 : 58288 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
12513 : : return false;
12514 : :
12515 : 58288 : DUMP_VECT_SCOPE ("vectorizable_early_exit");
12516 : :
12517 : 58288 : auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
12518 : :
12519 : : /* For SLP we don't want to use the type of the operands of the SLP node, when
12520 : : vectorizing using SLP slp_node will be the children of the gcond and we
12521 : : want to use the type of the direct children which since the gcond is root
12522 : : will be the current node, rather than a child node as vect_is_simple_use
12523 : : assumes. */
12524 : 58288 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12525 : 58288 : if (!vectype)
12526 : : return false;
12527 : :
12528 : 58288 : machine_mode mode = TYPE_MODE (vectype);
12529 : 58288 : int vec_num = vect_get_num_copies (loop_vinfo, slp_node);
12530 : :
12531 : 58288 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
12532 : 58288 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
12533 : 58288 : bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
12534 : 58288 : bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
12535 : :
12536 : : /* Now build the new conditional. Pattern gimple_conds get dropped during
12537 : : codegen so we must replace the original insn. */
12538 : 58288 : gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
12539 : 58288 : gcond *cond_stmt = as_a <gcond *>(orig_stmt);
12540 : :
12541 : 58288 : tree vectype_out = vectype;
12542 : 58288 : auto bb = gimple_bb (cond_stmt);
12543 : 58288 : edge exit_true_edge = EDGE_SUCC (bb, 0);
12544 : 58288 : if (exit_true_edge->flags & EDGE_FALSE_VALUE)
12545 : 830 : exit_true_edge = EDGE_SUCC (bb, 1);
12546 : 58288 : gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
12547 : :
12548 : : /* When vectorizing we assume that if the branch edge is taken that we're
12549 : : exiting the loop. This is not however always the case as the compiler will
12550 : : rewrite conditions to always be a comparison against 0. To do this it
12551 : : sometimes flips the edges. This is fine for scalar, but for vector we
12552 : : then have to negate the result of the test, as we're still assuming that if
12553 : : you take the branch edge that we found the exit condition. i.e. we need to
12554 : : know whether we are generating a `forall` or an `exist` condition. */
12555 : 116576 : bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
12556 : 58288 : exit_true_edge->dest);
12557 : :
12558 : : /* See if we support ADDHN and use that for the reduction. */
12559 : 58288 : internal_fn ifn = IFN_VEC_TRUNC_ADD_HIGH;
12560 : 58288 : bool addhn_supported_p
12561 : 58288 : = direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_BOTH);
12562 : 58288 : tree narrow_type = NULL_TREE;
12563 : 58288 : if (addhn_supported_p)
12564 : : {
12565 : : /* Calculate the narrowing type for the result. */
12566 : 0 : auto halfprec = TYPE_PRECISION (TREE_TYPE (vectype)) / 2;
12567 : 0 : auto unsignedp = TYPE_UNSIGNED (TREE_TYPE (vectype));
12568 : 0 : tree itype = build_nonstandard_integer_type (halfprec, unsignedp);
12569 : 0 : tree tmp_type = build_vector_type (itype, TYPE_VECTOR_SUBPARTS (vectype));
12570 : 0 : narrow_type = truth_type_for (tmp_type);
12571 : :
12572 : 0 : if (direct_optab_handler (cbranch_optab, TYPE_MODE (narrow_type))
12573 : : == CODE_FOR_nothing)
12574 : : {
12575 : 0 : if (dump_enabled_p ())
12576 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12577 : : "can't use ADDHN reduction because cbranch for "
12578 : : "the narrowed type is not supported by the "
12579 : : "target.\n");
12580 : : addhn_supported_p = false;
12581 : : }
12582 : : }
12583 : :
12584 : : /* Analyze only. */
12585 : 58288 : if (cost_vec)
12586 : : {
12587 : 56668 : if (!addhn_supported_p
12588 : 113336 : && direct_optab_handler (cbranch_optab, mode) == CODE_FOR_nothing)
12589 : : {
12590 : 54485 : if (dump_enabled_p ())
12591 : 603 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12592 : : "can't vectorize early exit because the "
12593 : : "target doesn't support flag setting vector "
12594 : : "comparisons.\n");
12595 : 54485 : return false;
12596 : : }
12597 : :
12598 : 2183 : if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code, gsi,
12599 : : slp_node, cost_vec))
12600 : : return false;
12601 : :
12602 : 2183 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
12603 : : {
12604 : 1 : if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
12605 : : OPTIMIZE_FOR_SPEED))
12606 : 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
12607 : : else
12608 : 1 : vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
12609 : : }
12610 : :
12611 : 2183 : return true;
12612 : : }
12613 : :
12614 : : /* Tranform. */
12615 : :
12616 : 1620 : tree new_temp = NULL_TREE;
12617 : 1620 : gimple *new_stmt = NULL;
12618 : :
12619 : 1620 : if (dump_enabled_p ())
12620 : 345 : dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
12621 : :
12622 : : /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
12623 : : roots and so by the time we get to them we have already codegened the SLP tree
12624 : : and so we shouldn't try to do so again. The arguments have already been
12625 : : vectorized. It's not very clean to do this here, But the masking code below is
12626 : : complex and this keeps it all in one place to ease fixes and backports. Once we
12627 : : drop the non-SLP loop vect or split vectorizable_* this can be simplified. */
12628 : :
12629 : 1620 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
12630 : 1620 : basic_block cond_bb = gimple_bb (stmt);
12631 : 1620 : gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb);
12632 : :
12633 : 1620 : auto_vec<tree> stmts;
12634 : 1620 : stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
12635 : :
12636 : : /* If we're comparing against a previous forall we need to negate the resullts
12637 : : before we do the final comparison or reduction. */
12638 : 1620 : if (flipped)
12639 : : {
12640 : : /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
12641 : : if (any(~mask)) by negating the masks and flipping the branches.
12642 : :
12643 : : 1. For unmasked loops we simply reduce the ~mask.
12644 : : 2. For masked loops we reduce (~mask & loop_mask) which is the same as
12645 : : doing (mask & loop_mask) ^ loop_mask. */
12646 : 222 : for (unsigned i = 0; i < stmts.length (); i++)
12647 : : {
12648 : 133 : tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
12649 : 133 : auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
12650 : 133 : vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
12651 : : &cond_gsi);
12652 : 133 : stmts[i] = inv_lhs;
12653 : : }
12654 : :
12655 : 89 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
12656 : 89 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
12657 : : }
12658 : :
12659 : : /* Determine if we need to reduce the final value. */
12660 : 1620 : if (stmts.length () > 1)
12661 : : {
12662 : : /* We build the reductions in a way to maintain as much parallelism as
12663 : : possible. */
12664 : 399 : auto_vec<tree> workset (stmts.length ());
12665 : :
12666 : : /* Mask the statements as we queue them up. Normally we loop over
12667 : : vec_num, but since we inspect the exact results of vectorization
12668 : : we don't need to and instead can just use the stmts themselves. */
12669 : 399 : if (masked_loop_p)
12670 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
12671 : : {
12672 : 0 : tree stmt_mask
12673 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
12674 : : vectype, i);
12675 : 0 : stmt_mask
12676 : 0 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
12677 : 0 : stmts[i], &cond_gsi);
12678 : 0 : workset.quick_push (stmt_mask);
12679 : : }
12680 : 399 : else if (len_loop_p)
12681 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
12682 : : {
12683 : 0 : tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
12684 : : lens, vec_num,
12685 : 0 : vectype, stmts[i], i, 1);
12686 : :
12687 : 0 : workset.quick_push (len_mask);
12688 : : }
12689 : : else
12690 : 399 : workset.splice (stmts);
12691 : :
12692 : 938 : while (workset.length () > 1)
12693 : : {
12694 : 539 : tree arg0 = workset.pop ();
12695 : 539 : tree arg1 = workset.pop ();
12696 : 539 : if (addhn_supported_p && workset.length () == 0)
12697 : : {
12698 : 0 : new_stmt = gimple_build_call_internal (ifn, 2, arg0, arg1);
12699 : 0 : vectype_out = narrow_type;
12700 : 0 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
12701 : 0 : gimple_call_set_lhs (as_a <gcall *> (new_stmt), new_temp);
12702 : 0 : gimple_call_set_nothrow (as_a <gcall *> (new_stmt), true);
12703 : : }
12704 : : else
12705 : : {
12706 : 539 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
12707 : 539 : new_stmt
12708 : 539 : = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
12709 : : }
12710 : 539 : vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
12711 : : &cond_gsi);
12712 : 539 : workset.quick_insert (0, new_temp);
12713 : : }
12714 : 399 : }
12715 : : else
12716 : : {
12717 : 1221 : new_temp = stmts[0];
12718 : 1221 : if (masked_loop_p)
12719 : : {
12720 : 0 : tree mask
12721 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, 1, vectype, 0);
12722 : 0 : new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
12723 : : new_temp, &cond_gsi);
12724 : : }
12725 : 1221 : else if (len_loop_p)
12726 : 0 : new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
12727 : : 1, vectype, new_temp, 0, 1);
12728 : : }
12729 : :
12730 : 1620 : gcc_assert (new_temp);
12731 : :
12732 : 1620 : tree cst = build_zero_cst (vectype_out);
12733 : 1620 : gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
12734 : 1620 : update_stmt (orig_stmt);
12735 : :
12736 : : /* ??? */
12737 : 1620 : SLP_TREE_VEC_DEFS (slp_node).truncate (0);
12738 : :
12739 : 1620 : return true;
12740 : 1620 : }
12741 : :
12742 : : /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
12743 : : can handle all live statements in the node. Otherwise return true
12744 : : if STMT_INFO is not live or if vectorizable_live_operation can handle it.
12745 : : VEC_STMT_P is as for vectorizable_live_operation. */
12746 : :
12747 : : static bool
12748 : 1381236 : can_vectorize_live_stmts (vec_info *vinfo,
12749 : : slp_tree slp_node, slp_instance slp_node_instance,
12750 : : bool vec_stmt_p,
12751 : : stmt_vector_for_cost *cost_vec)
12752 : : {
12753 : 1381236 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12754 : 1381236 : stmt_vec_info slp_stmt_info;
12755 : 1381236 : unsigned int i;
12756 : 3127349 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
12757 : : {
12758 : 1746113 : if (slp_stmt_info
12759 : 1727066 : && (STMT_VINFO_LIVE_P (slp_stmt_info)
12760 : 1542513 : || (loop_vinfo
12761 : 1264334 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
12762 : 228068 : && STMT_VINFO_DEF_TYPE (slp_stmt_info)
12763 : : == vect_induction_def))
12764 : 1930666 : && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
12765 : : slp_node_instance, i,
12766 : : vec_stmt_p, cost_vec))
12767 : : return false;
12768 : : }
12769 : :
12770 : : return true;
12771 : : }
12772 : :
12773 : : /* Make sure the statement is vectorizable. */
12774 : :
12775 : : opt_result
12776 : 2296255 : vect_analyze_stmt (vec_info *vinfo,
12777 : : slp_tree node, slp_instance node_instance,
12778 : : stmt_vector_for_cost *cost_vec)
12779 : : {
12780 : 2296255 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
12781 : 2296255 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12782 : 2296255 : enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
12783 : 2296255 : bool ok;
12784 : :
12785 : 2296255 : if (dump_enabled_p ())
12786 : 95433 : dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
12787 : : stmt_info->stmt);
12788 : :
12789 : 4359134 : if (gimple_has_volatile_ops (stmt_info->stmt))
12790 : : {
12791 : : /* ??? This shouldn't really happen, volatile stmts should
12792 : : not end up in the SLP graph. */
12793 : 0 : return opt_result::failure_at (stmt_info->stmt,
12794 : : "not vectorized:"
12795 : : " stmt has volatile operands: %G\n",
12796 : : stmt_info->stmt);
12797 : : }
12798 : :
12799 : : /* Skip stmts that do not need to be vectorized. */
12800 : 2296255 : if (!STMT_VINFO_RELEVANT_P (stmt_info)
12801 : 0 : && !STMT_VINFO_LIVE_P (stmt_info))
12802 : : {
12803 : 0 : if (dump_enabled_p ())
12804 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
12805 : :
12806 : : /* ??? This shouldn't really happen, irrelevant stmts should
12807 : : not end up in the SLP graph. */
12808 : 0 : return opt_result::failure_at (stmt_info->stmt,
12809 : : "not vectorized:"
12810 : : " irrelevant stmt as SLP node %p "
12811 : : "representative.\n",
12812 : : (void *)node);
12813 : : }
12814 : :
12815 : 2296255 : switch (STMT_VINFO_DEF_TYPE (stmt_info))
12816 : : {
12817 : : case vect_internal_def:
12818 : : case vect_condition_def:
12819 : : break;
12820 : :
12821 : 54225 : case vect_reduction_def:
12822 : 54225 : case vect_nested_cycle:
12823 : 54225 : gcc_assert (!bb_vinfo
12824 : : && (relevance == vect_used_in_outer
12825 : : || relevance == vect_used_in_outer_by_reduction
12826 : : || relevance == vect_used_by_reduction
12827 : : || relevance == vect_unused_in_scope
12828 : : || relevance == vect_used_only_live));
12829 : : break;
12830 : :
12831 : 279 : case vect_double_reduction_def:
12832 : 279 : gcc_assert (!bb_vinfo && node);
12833 : : break;
12834 : :
12835 : 121479 : case vect_induction_def:
12836 : 121479 : case vect_first_order_recurrence:
12837 : 121479 : gcc_assert (!bb_vinfo);
12838 : : break;
12839 : :
12840 : 0 : case vect_constant_def:
12841 : 0 : case vect_external_def:
12842 : 0 : case vect_unknown_def_type:
12843 : 0 : default:
12844 : 0 : gcc_unreachable ();
12845 : : }
12846 : :
12847 : 2296255 : tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
12848 : 2296255 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
12849 : :
12850 : 2296255 : if (STMT_VINFO_RELEVANT_P (stmt_info))
12851 : : {
12852 : 2296255 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
12853 : 2296255 : gcc_assert (SLP_TREE_VECTYPE (node)
12854 : : || gimple_code (stmt_info->stmt) == GIMPLE_COND
12855 : : || (call && gimple_call_lhs (call) == NULL_TREE));
12856 : : }
12857 : :
12858 : 2296255 : ok = true;
12859 : 2296255 : if (bb_vinfo
12860 : 1126474 : || (STMT_VINFO_RELEVANT_P (stmt_info)
12861 : 0 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
12862 : : /* Prefer vectorizable_call over vectorizable_simd_clone_call so
12863 : : -mveclibabi= takes preference over library functions with
12864 : : the simd attribute. */
12865 : 2296255 : ok = (vectorizable_call (vinfo, stmt_info, NULL, node, cost_vec)
12866 : 2290854 : || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, node,
12867 : : cost_vec)
12868 : 2290421 : || vectorizable_conversion (vinfo, stmt_info, NULL, node, cost_vec)
12869 : 2224149 : || vectorizable_operation (vinfo, stmt_info, NULL, node, cost_vec)
12870 : 1825751 : || vectorizable_assignment (vinfo, stmt_info, NULL, node, cost_vec)
12871 : 1769462 : || vectorizable_load (vinfo, stmt_info, NULL, node, cost_vec)
12872 : 1395662 : || vectorizable_store (vinfo, stmt_info, NULL, node, cost_vec)
12873 : 618443 : || vectorizable_shift (vinfo, stmt_info, NULL, node, cost_vec)
12874 : 577678 : || vectorizable_condition (vinfo, stmt_info, NULL, node, cost_vec)
12875 : 557731 : || vectorizable_comparison (vinfo, stmt_info, NULL, node, cost_vec)
12876 : 436903 : || (bb_vinfo
12877 : 134960 : && vectorizable_phi (bb_vinfo, stmt_info, node, cost_vec))
12878 : 2669627 : || (is_a <loop_vec_info> (vinfo)
12879 : 301943 : && (vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
12880 : : stmt_info, node, cost_vec)
12881 : 301502 : || vectorizable_reduction (as_a <loop_vec_info> (vinfo),
12882 : : stmt_info,
12883 : : node, node_instance, cost_vec)
12884 : 250086 : || vectorizable_induction (as_a <loop_vec_info> (vinfo),
12885 : : stmt_info, node, cost_vec)
12886 : 156011 : || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
12887 : : stmt_info, node)
12888 : 155316 : || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
12889 : : stmt_info, node, cost_vec)
12890 : 155090 : || vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
12891 : : stmt_info, NULL, node,
12892 : : cost_vec))));
12893 : :
12894 : 2296255 : STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
12895 : :
12896 : 2069736 : if (!ok)
12897 : 226519 : return opt_result::failure_at (stmt_info->stmt,
12898 : : "not vectorized:"
12899 : : " relevant stmt not supported: %G",
12900 : : stmt_info->stmt);
12901 : :
12902 : : /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
12903 : : need extra handling, except for vectorizable reductions. */
12904 : 2069736 : if (!bb_vinfo
12905 : 971384 : && SLP_TREE_TYPE (node) != reduc_vec_info_type
12906 : 964065 : && (SLP_TREE_TYPE (node) != lc_phi_info_type
12907 : 695 : || SLP_TREE_DEF_TYPE (node) == vect_internal_def)
12908 : 964065 : && (!node->ldst_lanes || SLP_TREE_PERMUTE_P (node))
12909 : 3033801 : && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
12910 : : node, node_instance,
12911 : : false, cost_vec))
12912 : 0 : return opt_result::failure_at (stmt_info->stmt,
12913 : : "not vectorized:"
12914 : : " live stmt not supported: %G",
12915 : : stmt_info->stmt);
12916 : :
12917 : 2069736 : return opt_result::success ();
12918 : : }
12919 : :
12920 : :
12921 : : /* Function vect_transform_stmt.
12922 : :
12923 : : Create a vectorized stmt to replace STMT_INFO, and insert it at GSI. */
12924 : :
12925 : : bool
12926 : 960974 : vect_transform_stmt (vec_info *vinfo,
12927 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12928 : : slp_tree slp_node, slp_instance slp_node_instance)
12929 : : {
12930 : 960974 : bool is_store = false;
12931 : 960974 : bool done;
12932 : :
12933 : 960974 : gcc_assert (slp_node);
12934 : :
12935 : 960974 : if (stmt_info)
12936 : 960113 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
12937 : :
12938 : 960974 : switch (SLP_TREE_TYPE (slp_node))
12939 : : {
12940 : 21910 : case type_demotion_vec_info_type:
12941 : 21910 : case type_promotion_vec_info_type:
12942 : 21910 : case type_conversion_vec_info_type:
12943 : 21910 : done = vectorizable_conversion (vinfo, stmt_info, gsi, slp_node, NULL);
12944 : 21910 : gcc_assert (done);
12945 : : break;
12946 : :
12947 : 17303 : case induc_vec_info_type:
12948 : 17303 : done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
12949 : : stmt_info, slp_node, NULL);
12950 : 17303 : gcc_assert (done);
12951 : : break;
12952 : :
12953 : 7508 : case shift_vec_info_type:
12954 : 7508 : done = vectorizable_shift (vinfo, stmt_info, gsi, slp_node, NULL);
12955 : 7508 : gcc_assert (done);
12956 : : break;
12957 : :
12958 : 111720 : case op_vec_info_type:
12959 : 111720 : done = vectorizable_operation (vinfo, stmt_info, gsi, slp_node, NULL);
12960 : 111720 : gcc_assert (done);
12961 : : break;
12962 : :
12963 : 14262 : case assignment_vec_info_type:
12964 : 14262 : done = vectorizable_assignment (vinfo, stmt_info, gsi, slp_node, NULL);
12965 : 14262 : gcc_assert (done);
12966 : : break;
12967 : :
12968 : 163631 : case load_vec_info_type:
12969 : 163631 : done = vectorizable_load (vinfo, stmt_info, gsi, slp_node, NULL);
12970 : 163631 : gcc_assert (done);
12971 : : break;
12972 : :
12973 : 543803 : case store_vec_info_type:
12974 : 543803 : done = vectorizable_store (vinfo, stmt_info, gsi, slp_node, NULL);
12975 : 543803 : gcc_assert (done);
12976 : : is_store = true;
12977 : : break;
12978 : :
12979 : 8040 : case condition_vec_info_type:
12980 : 8040 : done = vectorizable_condition (vinfo, stmt_info, gsi, slp_node, NULL);
12981 : 8040 : gcc_assert (done);
12982 : : break;
12983 : :
12984 : 11430 : case comparison_vec_info_type:
12985 : 11430 : done = vectorizable_comparison (vinfo, stmt_info, gsi, slp_node, NULL);
12986 : 11430 : gcc_assert (done);
12987 : : break;
12988 : :
12989 : 4127 : case call_vec_info_type:
12990 : 4127 : done = vectorizable_call (vinfo, stmt_info, gsi, slp_node, NULL);
12991 : 4127 : break;
12992 : :
12993 : 342 : case call_simd_clone_vec_info_type:
12994 : 342 : done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi,
12995 : : slp_node, NULL);
12996 : 342 : break;
12997 : :
12998 : 2360 : case reduc_vec_info_type:
12999 : 2360 : done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
13000 : : gsi, slp_node);
13001 : 2360 : gcc_assert (done);
13002 : : break;
13003 : :
13004 : 22825 : case cycle_phi_info_type:
13005 : 22825 : done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
13006 : : slp_node, slp_node_instance);
13007 : 22825 : gcc_assert (done);
13008 : : break;
13009 : :
13010 : 438 : case lc_phi_info_type:
13011 : 438 : done = vect_transform_lc_phi (as_a <loop_vec_info> (vinfo),
13012 : : stmt_info, slp_node);
13013 : 438 : gcc_assert (done);
13014 : : break;
13015 : :
13016 : 42 : case recurr_info_type:
13017 : 42 : done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13018 : : stmt_info, slp_node, NULL);
13019 : 42 : gcc_assert (done);
13020 : : break;
13021 : :
13022 : 15298 : case phi_info_type:
13023 : 15298 : done = vectorizable_phi (as_a <bb_vec_info> (vinfo),
13024 : : stmt_info, slp_node, NULL);
13025 : 15298 : gcc_assert (done);
13026 : : break;
13027 : :
13028 : 0 : case loop_exit_ctrl_vec_info_type:
13029 : 0 : done = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
13030 : : stmt_info, gsi, slp_node, NULL);
13031 : 0 : gcc_assert (done);
13032 : : break;
13033 : :
13034 : 15935 : case permute_info_type:
13035 : 15935 : done = vectorizable_slp_permutation (vinfo, gsi, slp_node, NULL);
13036 : 15935 : gcc_assert (done);
13037 : : break;
13038 : :
13039 : 0 : default:
13040 : 0 : if (!STMT_VINFO_LIVE_P (stmt_info))
13041 : : {
13042 : 0 : if (dump_enabled_p ())
13043 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13044 : : "stmt not supported.\n");
13045 : 0 : gcc_unreachable ();
13046 : : }
13047 : 960974 : done = true;
13048 : : }
13049 : :
13050 : 960974 : if (SLP_TREE_TYPE (slp_node) != store_vec_info_type
13051 : 417171 : && (!slp_node->ldst_lanes || SLP_TREE_PERMUTE_P (slp_node)))
13052 : : {
13053 : : /* Handle stmts whose DEF is used outside the loop-nest that is
13054 : : being vectorized. */
13055 : 417171 : done = can_vectorize_live_stmts (vinfo, slp_node,
13056 : : slp_node_instance, true, NULL);
13057 : 417171 : gcc_assert (done);
13058 : : }
13059 : :
13060 : 960974 : return is_store;
13061 : : }
13062 : :
13063 : :
13064 : : /* Remove a group of stores (for SLP or interleaving), free their
13065 : : stmt_vec_info. */
13066 : :
13067 : : void
13068 : 0 : vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
13069 : : {
13070 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
13071 : :
13072 : 0 : while (next_stmt_info)
13073 : : {
13074 : 0 : stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
13075 : 0 : next_stmt_info = vect_orig_stmt (next_stmt_info);
13076 : : /* Free the attached stmt_vec_info and remove the stmt. */
13077 : 0 : vinfo->remove_stmt (next_stmt_info);
13078 : 0 : next_stmt_info = tmp;
13079 : : }
13080 : 0 : }
13081 : :
13082 : : /* If NUNITS is nonzero, return a vector type that contains NUNITS
13083 : : elements of type SCALAR_TYPE, or null if the target doesn't support
13084 : : such a type.
13085 : :
13086 : : If NUNITS is zero, return a vector type that contains elements of
13087 : : type SCALAR_TYPE, choosing whichever vector size the target prefers.
13088 : :
13089 : : If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
13090 : : for this vectorization region and want to "autodetect" the best choice.
13091 : : Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
13092 : : and we want the new type to be interoperable with it. PREVAILING_MODE
13093 : : in this case can be a scalar integer mode or a vector mode; when it
13094 : : is a vector mode, the function acts like a tree-level version of
13095 : : related_vector_mode. */
13096 : :
13097 : : tree
13098 : 31254100 : get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
13099 : : tree scalar_type, poly_uint64 nunits)
13100 : : {
13101 : 31254100 : tree orig_scalar_type = scalar_type;
13102 : 31254100 : scalar_mode inner_mode;
13103 : 31254100 : machine_mode simd_mode;
13104 : 31254100 : tree vectype;
13105 : :
13106 : 31254100 : if ((!INTEGRAL_TYPE_P (scalar_type)
13107 : 10475293 : && !POINTER_TYPE_P (scalar_type)
13108 : 1655372 : && !SCALAR_FLOAT_TYPE_P (scalar_type))
13109 : 41243996 : || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
13110 : 1170057 : && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
13111 : 488612 : return NULL_TREE;
13112 : :
13113 : 30765488 : unsigned int nbytes = GET_MODE_SIZE (inner_mode);
13114 : :
13115 : : /* Interoperability between modes requires one to be a constant multiple
13116 : : of the other, so that the number of vectors required for each operation
13117 : : is a compile-time constant. */
13118 : 30765488 : if (prevailing_mode != VOIDmode
13119 : 29693672 : && !constant_multiple_p (nunits * nbytes,
13120 : 29693672 : GET_MODE_SIZE (prevailing_mode))
13121 : 32274745 : && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
13122 : 1509257 : nunits * nbytes))
13123 : : return NULL_TREE;
13124 : :
13125 : : /* For vector types of elements whose mode precision doesn't
13126 : : match their types precision we use a element type of mode
13127 : : precision. The vectorization routines will have to make sure
13128 : : they support the proper result truncation/extension.
13129 : : We also make sure to build vector types with INTEGER_TYPE
13130 : : component type only. */
13131 : 30765488 : if (INTEGRAL_TYPE_P (scalar_type)
13132 : 51544213 : && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
13133 : 18803111 : || TREE_CODE (scalar_type) != INTEGER_TYPE))
13134 : 2230590 : scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
13135 : 2230590 : TYPE_UNSIGNED (scalar_type));
13136 : :
13137 : : /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
13138 : : When the component mode passes the above test simply use a type
13139 : : corresponding to that mode. The theory is that any use that
13140 : : would cause problems with this will disable vectorization anyway. */
13141 : 28534898 : else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
13142 : : && !INTEGRAL_TYPE_P (scalar_type))
13143 : 8819921 : scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
13144 : :
13145 : : /* We can't build a vector type of elements with alignment bigger than
13146 : : their size. */
13147 : 19714977 : else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
13148 : 340986 : scalar_type = lang_hooks.types.type_for_mode (inner_mode,
13149 : 170493 : TYPE_UNSIGNED (scalar_type));
13150 : :
13151 : : /* If we felt back to using the mode fail if there was
13152 : : no scalar type for it. */
13153 : 30765488 : if (scalar_type == NULL_TREE)
13154 : : return NULL_TREE;
13155 : :
13156 : : /* If no prevailing mode was supplied, use the mode the target prefers.
13157 : : Otherwise lookup a vector mode based on the prevailing mode. */
13158 : 30765488 : if (prevailing_mode == VOIDmode)
13159 : : {
13160 : 1071816 : gcc_assert (known_eq (nunits, 0U));
13161 : 1071816 : simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
13162 : 1071816 : if (SCALAR_INT_MODE_P (simd_mode))
13163 : : {
13164 : : /* Traditional behavior is not to take the integer mode
13165 : : literally, but simply to use it as a way of determining
13166 : : the vector size. It is up to mode_for_vector to decide
13167 : : what the TYPE_MODE should be.
13168 : :
13169 : : Note that nunits == 1 is allowed in order to support single
13170 : : element vector types. */
13171 : 34450 : if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
13172 : 343 : || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13173 : 16882 : return NULL_TREE;
13174 : : }
13175 : : }
13176 : 29693672 : else if (SCALAR_INT_MODE_P (prevailing_mode)
13177 : 29693672 : || !related_vector_mode (prevailing_mode,
13178 : 27704164 : inner_mode, nunits).exists (&simd_mode))
13179 : : {
13180 : : /* Fall back to using mode_for_vector, mostly in the hope of being
13181 : : able to use an integer mode. */
13182 : 1989508 : if (known_eq (nunits, 0U)
13183 : 4623964 : && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
13184 : : return NULL_TREE;
13185 : :
13186 : 146648 : if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13187 : 136672 : return NULL_TREE;
13188 : : }
13189 : :
13190 : 28769074 : vectype = build_vector_type_for_mode (scalar_type, simd_mode);
13191 : :
13192 : : /* In cases where the mode was chosen by mode_for_vector, check that
13193 : : the target actually supports the chosen mode, or that it at least
13194 : : allows the vector mode to be replaced by a like-sized integer. */
13195 : 57538148 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
13196 : 28779305 : && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
13197 : : return NULL_TREE;
13198 : :
13199 : : /* Re-attach the address-space qualifier if we canonicalized the scalar
13200 : : type. */
13201 : 28760998 : if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
13202 : 5 : return build_qualified_type
13203 : 5 : (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
13204 : :
13205 : : return vectype;
13206 : : }
13207 : :
13208 : : /* Function get_vectype_for_scalar_type.
13209 : :
13210 : : Returns the vector type corresponding to SCALAR_TYPE as supported
13211 : : by the target. If GROUP_SIZE is nonzero and we're performing BB
13212 : : vectorization, make sure that the number of elements in the vector
13213 : : is no bigger than GROUP_SIZE. */
13214 : :
13215 : : tree
13216 : 28069175 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
13217 : : unsigned int group_size)
13218 : : {
13219 : : /* For BB vectorization, we should always have a group size once we've
13220 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
13221 : : are tentative requests during things like early data reference
13222 : : analysis and pattern recognition. */
13223 : 28069175 : if (is_a <bb_vec_info> (vinfo))
13224 : 25809630 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
13225 : : else
13226 : : group_size = 0;
13227 : :
13228 : 28069175 : tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13229 : : scalar_type);
13230 : 28069175 : if (vectype && vinfo->vector_mode == VOIDmode)
13231 : 1054932 : vinfo->vector_mode = TYPE_MODE (vectype);
13232 : :
13233 : : /* Register the natural choice of vector type, before the group size
13234 : : has been applied. */
13235 : 0 : if (vectype)
13236 : 25579975 : vinfo->used_vector_modes.add (TYPE_MODE (vectype));
13237 : :
13238 : : /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
13239 : : try again with an explicit number of elements. */
13240 : 25579975 : if (vectype
13241 : 25579975 : && group_size
13242 : 28069175 : && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
13243 : : {
13244 : : /* Start with the biggest number of units that fits within
13245 : : GROUP_SIZE and halve it until we find a valid vector type.
13246 : : Usually either the first attempt will succeed or all will
13247 : : fail (in the latter case because GROUP_SIZE is too small
13248 : : for the target), but it's possible that a target could have
13249 : : a hole between supported vector types.
13250 : :
13251 : : If GROUP_SIZE is not a power of 2, this has the effect of
13252 : : trying the largest power of 2 that fits within the group,
13253 : : even though the group is not a multiple of that vector size.
13254 : : The BB vectorizer will then try to carve up the group into
13255 : : smaller pieces. */
13256 : 3046006 : unsigned int nunits = 1 << floor_log2 (group_size);
13257 : 3046006 : do
13258 : : {
13259 : 3046006 : vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13260 : 3046006 : scalar_type, nunits);
13261 : 3046006 : nunits /= 2;
13262 : : }
13263 : 3046006 : while (nunits > 1 && !vectype);
13264 : : }
13265 : :
13266 : 28069175 : return vectype;
13267 : : }
13268 : :
13269 : : /* Return the vector type corresponding to SCALAR_TYPE as supported
13270 : : by the target. NODE, if nonnull, is the SLP tree node that will
13271 : : use the returned vector type. */
13272 : :
13273 : : tree
13274 : 156753 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
13275 : : {
13276 : 156753 : unsigned int group_size = 0;
13277 : 156753 : if (node)
13278 : 156753 : group_size = SLP_TREE_LANES (node);
13279 : 156753 : return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13280 : : }
13281 : :
13282 : : /* Function get_mask_type_for_scalar_type.
13283 : :
13284 : : Returns the mask type corresponding to a result of comparison
13285 : : of vectors of specified SCALAR_TYPE as supported by target.
13286 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
13287 : : make sure that the number of elements in the vector is no bigger
13288 : : than GROUP_SIZE. */
13289 : :
13290 : : tree
13291 : 2029117 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13292 : : unsigned int group_size)
13293 : : {
13294 : 2029117 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13295 : :
13296 : 2029117 : if (!vectype)
13297 : : return NULL;
13298 : :
13299 : 2008966 : return truth_type_for (vectype);
13300 : : }
13301 : :
13302 : : /* Function get_mask_type_for_scalar_type.
13303 : :
13304 : : Returns the mask type corresponding to a result of comparison
13305 : : of vectors of specified SCALAR_TYPE as supported by target.
13306 : : NODE, if nonnull, is the SLP tree node that will use the returned
13307 : : vector type. */
13308 : :
13309 : : tree
13310 : 17 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13311 : : slp_tree node)
13312 : : {
13313 : 17 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
13314 : :
13315 : 17 : if (!vectype)
13316 : : return NULL;
13317 : :
13318 : 17 : return truth_type_for (vectype);
13319 : : }
13320 : :
13321 : : /* Function get_same_sized_vectype
13322 : :
13323 : : Returns a vector type corresponding to SCALAR_TYPE of size
13324 : : VECTOR_TYPE if supported by the target. */
13325 : :
13326 : : tree
13327 : 129430 : get_same_sized_vectype (tree scalar_type, tree vector_type)
13328 : : {
13329 : 129430 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
13330 : 0 : return truth_type_for (vector_type);
13331 : :
13332 : 129430 : poly_uint64 nunits;
13333 : 258860 : if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
13334 : 258860 : GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
13335 : : return NULL_TREE;
13336 : :
13337 : 129430 : return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
13338 : 129430 : scalar_type, nunits);
13339 : : }
13340 : :
13341 : : /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
13342 : : would not change the chosen vector modes. */
13343 : :
13344 : : bool
13345 : 1427138 : vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
13346 : : {
13347 : 1427138 : for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
13348 : 3405932 : i != vinfo->used_vector_modes.end (); ++i)
13349 : 1751122 : if (!VECTOR_MODE_P (*i)
13350 : 5253366 : || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
13351 : 761725 : return false;
13352 : 665413 : return true;
13353 : : }
13354 : :
13355 : : /* Return true if replacing VECTOR_MODE with ALT_VECTOR_MODE would not
13356 : : change the chosen vector modes for analysis of a loop. */
13357 : :
13358 : : bool
13359 : 293030 : vect_chooses_same_modes_p (machine_mode vector_mode,
13360 : : machine_mode alt_vector_mode)
13361 : : {
13362 : 49510 : return (VECTOR_MODE_P (vector_mode)
13363 : 293030 : && VECTOR_MODE_P (alt_vector_mode)
13364 : 586060 : && (related_vector_mode (vector_mode,
13365 : : GET_MODE_INNER (alt_vector_mode))
13366 : 293030 : == alt_vector_mode)
13367 : 318224 : && (related_vector_mode (alt_vector_mode,
13368 : : GET_MODE_INNER (vector_mode))
13369 : 12597 : == vector_mode));
13370 : : }
13371 : :
13372 : : /* Function vect_is_simple_use.
13373 : :
13374 : : Input:
13375 : : VINFO - the vect info of the loop or basic block that is being vectorized.
13376 : : OPERAND - operand in the loop or bb.
13377 : : Output:
13378 : : DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
13379 : : case OPERAND is an SSA_NAME that is defined in the vectorizable region
13380 : : DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
13381 : : the definition could be anywhere in the function
13382 : : DT - the type of definition
13383 : :
13384 : : Returns whether a stmt with OPERAND can be vectorized.
13385 : : For loops, supportable operands are constants, loop invariants, and operands
13386 : : that are defined by the current iteration of the loop. Unsupportable
13387 : : operands are those that are defined by a previous iteration of the loop (as
13388 : : is the case in reduction/induction computations).
13389 : : For basic blocks, supportable operands are constants and bb invariants.
13390 : : For now, operands defined outside the basic block are not supported. */
13391 : :
13392 : : bool
13393 : 39739476 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
13394 : : stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
13395 : : {
13396 : 39739476 : if (def_stmt_info_out)
13397 : 38007993 : *def_stmt_info_out = NULL;
13398 : 39739476 : if (def_stmt_out)
13399 : 8781385 : *def_stmt_out = NULL;
13400 : 39739476 : *dt = vect_unknown_def_type;
13401 : :
13402 : 39739476 : if (dump_enabled_p ())
13403 : : {
13404 : 715948 : dump_printf_loc (MSG_NOTE, vect_location,
13405 : : "vect_is_simple_use: operand ");
13406 : 715948 : if (TREE_CODE (operand) == SSA_NAME
13407 : 715948 : && !SSA_NAME_IS_DEFAULT_DEF (operand))
13408 : 648587 : dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
13409 : : else
13410 : 67361 : dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
13411 : : }
13412 : :
13413 : 39739476 : if (CONSTANT_CLASS_P (operand))
13414 : 3190792 : *dt = vect_constant_def;
13415 : 36548684 : else if (is_gimple_min_invariant (operand))
13416 : 342512 : *dt = vect_external_def;
13417 : 36206172 : else if (TREE_CODE (operand) != SSA_NAME)
13418 : 1081 : *dt = vect_unknown_def_type;
13419 : 36205091 : else if (SSA_NAME_IS_DEFAULT_DEF (operand))
13420 : 504517 : *dt = vect_external_def;
13421 : : else
13422 : : {
13423 : 35700574 : gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
13424 : 35700574 : stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
13425 : 35700574 : if (!stmt_vinfo)
13426 : 739757 : *dt = vect_external_def;
13427 : : else
13428 : : {
13429 : 34960817 : stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
13430 : 34960817 : def_stmt = stmt_vinfo->stmt;
13431 : 34960817 : *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
13432 : 34960817 : if (def_stmt_info_out)
13433 : 33235723 : *def_stmt_info_out = stmt_vinfo;
13434 : : }
13435 : 35700574 : if (def_stmt_out)
13436 : 8570841 : *def_stmt_out = def_stmt;
13437 : : }
13438 : :
13439 : 39739476 : if (dump_enabled_p ())
13440 : : {
13441 : 715948 : dump_printf (MSG_NOTE, ", type of def: ");
13442 : 715948 : switch (*dt)
13443 : : {
13444 : 0 : case vect_uninitialized_def:
13445 : 0 : dump_printf (MSG_NOTE, "uninitialized\n");
13446 : 0 : break;
13447 : 56808 : case vect_constant_def:
13448 : 56808 : dump_printf (MSG_NOTE, "constant\n");
13449 : 56808 : break;
13450 : 24555 : case vect_external_def:
13451 : 24555 : dump_printf (MSG_NOTE, "external\n");
13452 : 24555 : break;
13453 : 516895 : case vect_internal_def:
13454 : 516895 : dump_printf (MSG_NOTE, "internal\n");
13455 : 516895 : break;
13456 : 92250 : case vect_induction_def:
13457 : 92250 : dump_printf (MSG_NOTE, "induction\n");
13458 : 92250 : break;
13459 : 22498 : case vect_reduction_def:
13460 : 22498 : dump_printf (MSG_NOTE, "reduction\n");
13461 : 22498 : break;
13462 : 464 : case vect_double_reduction_def:
13463 : 464 : dump_printf (MSG_NOTE, "double reduction\n");
13464 : 464 : break;
13465 : 1877 : case vect_nested_cycle:
13466 : 1877 : dump_printf (MSG_NOTE, "nested cycle\n");
13467 : 1877 : break;
13468 : 264 : case vect_first_order_recurrence:
13469 : 264 : dump_printf (MSG_NOTE, "first order recurrence\n");
13470 : 264 : break;
13471 : 0 : case vect_condition_def:
13472 : 0 : dump_printf (MSG_NOTE, "control flow\n");
13473 : 0 : break;
13474 : 337 : case vect_unknown_def_type:
13475 : 337 : dump_printf (MSG_NOTE, "unknown\n");
13476 : 337 : break;
13477 : : }
13478 : : }
13479 : :
13480 : 39739476 : if (*dt == vect_unknown_def_type)
13481 : : {
13482 : 14869 : if (dump_enabled_p ())
13483 : 337 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13484 : : "Unsupported pattern.\n");
13485 : 14869 : return false;
13486 : : }
13487 : :
13488 : : return true;
13489 : : }
13490 : :
13491 : : /* Function vect_is_simple_use.
13492 : :
13493 : : Same as vect_is_simple_use but determines the operand by operand
13494 : : position OPERAND from either STMT or SLP_NODE, filling in *OP
13495 : : and *SLP_DEF (when SLP_NODE is not NULL). */
13496 : :
13497 : : bool
13498 : 3306348 : vect_is_simple_use (vec_info *vinfo, slp_tree slp_node,
13499 : : unsigned operand, tree *op, slp_tree *slp_def,
13500 : : enum vect_def_type *dt,
13501 : : tree *vectype, stmt_vec_info *def_stmt_info_out)
13502 : : {
13503 : 3306348 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
13504 : 3306348 : *slp_def = child;
13505 : 3306348 : *vectype = SLP_TREE_VECTYPE (child);
13506 : 3306348 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
13507 : : {
13508 : : /* ??? VEC_PERM nodes might be intermediate and their lane value
13509 : : have no representative (nor do we build a VEC_PERM stmt for
13510 : : the actual operation). Note for two-operator nodes we set
13511 : : a representative but leave scalar stmts empty as we'd only
13512 : : have one for a subset of lanes. Ideally no caller would
13513 : : require *op for internal defs. */
13514 : 1694661 : if (SLP_TREE_REPRESENTATIVE (child))
13515 : : {
13516 : 1693935 : *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
13517 : 1693935 : return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
13518 : : }
13519 : : else
13520 : : {
13521 : 726 : gcc_assert (SLP_TREE_PERMUTE_P (child));
13522 : 726 : *op = error_mark_node;
13523 : 726 : *dt = vect_internal_def;
13524 : 726 : if (def_stmt_info_out)
13525 : 0 : *def_stmt_info_out = NULL;
13526 : 726 : return true;
13527 : : }
13528 : : }
13529 : : else
13530 : : {
13531 : 1611687 : if (def_stmt_info_out)
13532 : 45816 : *def_stmt_info_out = NULL;
13533 : 1611687 : *op = SLP_TREE_SCALAR_OPS (child)[0];
13534 : 1611687 : *dt = SLP_TREE_DEF_TYPE (child);
13535 : 1611687 : return true;
13536 : : }
13537 : : }
13538 : :
13539 : : /* If OP is not NULL and is external or constant update its vector
13540 : : type with VECTYPE. Returns true if successful or false if not,
13541 : : for example when conflicting vector types are present. */
13542 : :
13543 : : bool
13544 : 2876147 : vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
13545 : : {
13546 : 2876147 : if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
13547 : : return true;
13548 : 1058138 : if (SLP_TREE_VECTYPE (op))
13549 : 66228 : return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
13550 : : /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
13551 : : should be handled by patters. Allow vect_constant_def for now
13552 : : as well as the trivial single-lane uniform vect_external_def case
13553 : : both of which we code-generate reasonably. */
13554 : 991910 : if (VECTOR_BOOLEAN_TYPE_P (vectype)
13555 : 948 : && SLP_TREE_DEF_TYPE (op) == vect_external_def
13556 : 992482 : && SLP_TREE_LANES (op) > 1)
13557 : : return false;
13558 : 991730 : SLP_TREE_VECTYPE (op) = vectype;
13559 : 991730 : return true;
13560 : : }
13561 : :
13562 : : /* Function supportable_widening_operation
13563 : :
13564 : : Check whether an operation represented by the code CODE is a
13565 : : widening operation that is supported by the target platform in
13566 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
13567 : : producing a result of type VECTYPE_OUT).
13568 : :
13569 : : Widening operations we currently support are NOP (CONVERT), FLOAT,
13570 : : FIX_TRUNC and WIDEN_MULT. This function checks if these operations
13571 : : are supported by the target platform either directly (via vector
13572 : : tree-codes), or via target builtins.
13573 : :
13574 : : Output:
13575 : : - CODE1 and CODE2 are codes of vector operations to be used when
13576 : : vectorizing the operation, if available.
13577 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
13578 : : case of multi-step conversion (like char->short->int - in that case
13579 : : MULTI_STEP_CVT will be 1).
13580 : : - INTERM_TYPES contains the intermediate type required to perform the
13581 : : widening operation (short in the above example). */
13582 : :
13583 : : bool
13584 : 310440 : supportable_widening_operation (vec_info *vinfo,
13585 : : code_helper code,
13586 : : stmt_vec_info stmt_info,
13587 : : tree vectype_out, tree vectype_in,
13588 : : code_helper *code1,
13589 : : code_helper *code2,
13590 : : int *multi_step_cvt,
13591 : : vec<tree> *interm_types)
13592 : : {
13593 : 310440 : loop_vec_info loop_info = dyn_cast <loop_vec_info> (vinfo);
13594 : 310440 : class loop *vect_loop = NULL;
13595 : 310440 : machine_mode vec_mode;
13596 : 310440 : enum insn_code icode1, icode2;
13597 : 310440 : optab optab1 = unknown_optab, optab2 = unknown_optab;
13598 : 310440 : tree vectype = vectype_in;
13599 : 310440 : tree wide_vectype = vectype_out;
13600 : 310440 : tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
13601 : 310440 : int i;
13602 : 310440 : tree prev_type, intermediate_type;
13603 : 310440 : machine_mode intermediate_mode, prev_mode;
13604 : 310440 : optab optab3, optab4;
13605 : :
13606 : 310440 : *multi_step_cvt = 0;
13607 : 179340 : if (loop_info)
13608 : 131100 : vect_loop = LOOP_VINFO_LOOP (loop_info);
13609 : :
13610 : 310440 : switch (code.safe_as_tree_code ())
13611 : : {
13612 : : case MAX_TREE_CODES:
13613 : : /* Don't set c1 and c2 if code is not a tree_code. */
13614 : : break;
13615 : :
13616 : 165077 : case WIDEN_MULT_EXPR:
13617 : : /* The result of a vectorized widening operation usually requires
13618 : : two vectors (because the widened results do not fit into one vector).
13619 : : The generated vector results would normally be expected to be
13620 : : generated in the same order as in the original scalar computation,
13621 : : i.e. if 8 results are generated in each vector iteration, they are
13622 : : to be organized as follows:
13623 : : vect1: [res1,res2,res3,res4],
13624 : : vect2: [res5,res6,res7,res8].
13625 : :
13626 : : However, in the special case that the result of the widening
13627 : : operation is used in a reduction computation only, the order doesn't
13628 : : matter (because when vectorizing a reduction we change the order of
13629 : : the computation). Some targets can take advantage of this and
13630 : : generate more efficient code. For example, targets like Altivec,
13631 : : that support widen_mult using a sequence of {mult_even,mult_odd}
13632 : : generate the following vectors:
13633 : : vect1: [res1,res3,res5,res7],
13634 : : vect2: [res2,res4,res6,res8].
13635 : :
13636 : : When vectorizing outer-loops, we execute the inner-loop sequentially
13637 : : (each vectorized inner-loop iteration contributes to VF outer-loop
13638 : : iterations in parallel). We therefore don't allow to change the
13639 : : order of the computation in the inner-loop during outer-loop
13640 : : vectorization. */
13641 : : /* TODO: Another case in which order doesn't *really* matter is when we
13642 : : widen and then contract again, e.g. (short)((int)x * y >> 8).
13643 : : Normally, pack_trunc performs an even/odd permute, whereas the
13644 : : repack from an even/odd expansion would be an interleave, which
13645 : : would be significantly simpler for e.g. AVX2. */
13646 : : /* In any case, in order to avoid duplicating the code below, recurse
13647 : : on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
13648 : : are properly set up for the caller. If we fail, we'll continue with
13649 : : a VEC_WIDEN_MULT_LO/HI_EXPR check. */
13650 : 165077 : if (vect_loop
13651 : 41730 : && !nested_in_vect_loop_p (vect_loop, stmt_info)
13652 : 207267 : && supportable_widening_operation (vinfo, VEC_WIDEN_MULT_EVEN_EXPR,
13653 : : stmt_info, vectype_out,
13654 : : vectype_in, code1,
13655 : : code2, multi_step_cvt,
13656 : : interm_types))
13657 : : {
13658 : : /* Elements in a vector with vect_used_by_reduction property cannot
13659 : : be reordered if the use chain with this property does not have the
13660 : : same operation. One such an example is s += a * b, where elements
13661 : : in a and b cannot be reordered. Here we check if the vector defined
13662 : : by STMT is only directly used in the reduction statement. */
13663 : 28883 : tree lhs = gimple_assign_lhs (vect_orig_stmt (stmt_info)->stmt);
13664 : 27164 : stmt_vec_info use_stmt_info = loop_info->lookup_single_use (lhs);
13665 : 27164 : if (use_stmt_info && STMT_VINFO_REDUC_DEF (use_stmt_info))
13666 : : return true;
13667 : : }
13668 : : c1 = VEC_WIDEN_MULT_LO_EXPR;
13669 : : c2 = VEC_WIDEN_MULT_HI_EXPR;
13670 : : break;
13671 : :
13672 : : case DOT_PROD_EXPR:
13673 : 310259 : c1 = DOT_PROD_EXPR;
13674 : 310259 : c2 = DOT_PROD_EXPR;
13675 : : break;
13676 : :
13677 : 0 : case SAD_EXPR:
13678 : 0 : c1 = SAD_EXPR;
13679 : 0 : c2 = SAD_EXPR;
13680 : 0 : break;
13681 : :
13682 : 41730 : case VEC_WIDEN_MULT_EVEN_EXPR:
13683 : : /* Support the recursion induced just above. */
13684 : 41730 : c1 = VEC_WIDEN_MULT_EVEN_EXPR;
13685 : 41730 : c2 = VEC_WIDEN_MULT_ODD_EXPR;
13686 : 41730 : break;
13687 : :
13688 : 9044 : case WIDEN_LSHIFT_EXPR:
13689 : 9044 : c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
13690 : 9044 : c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
13691 : 9044 : break;
13692 : :
13693 : 33636 : CASE_CONVERT:
13694 : 33636 : c1 = VEC_UNPACK_LO_EXPR;
13695 : 33636 : c2 = VEC_UNPACK_HI_EXPR;
13696 : 33636 : break;
13697 : :
13698 : 6662 : case FLOAT_EXPR:
13699 : 6662 : c1 = VEC_UNPACK_FLOAT_LO_EXPR;
13700 : 6662 : c2 = VEC_UNPACK_FLOAT_HI_EXPR;
13701 : 6662 : break;
13702 : :
13703 : 206 : case FIX_TRUNC_EXPR:
13704 : 206 : c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
13705 : 206 : c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
13706 : 206 : break;
13707 : :
13708 : 0 : default:
13709 : 0 : gcc_unreachable ();
13710 : : }
13711 : :
13712 : 310259 : if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
13713 : : std::swap (c1, c2);
13714 : :
13715 : 310259 : if (code == FIX_TRUNC_EXPR)
13716 : : {
13717 : : /* The signedness is determined from output operand. */
13718 : 206 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
13719 : 206 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
13720 : : }
13721 : 538199 : else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
13722 : 33636 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
13723 : 5572 : && VECTOR_BOOLEAN_TYPE_P (vectype)
13724 : 5572 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
13725 : 256215 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
13726 : : {
13727 : : /* If the input and result modes are the same, a different optab
13728 : : is needed where we pass in the number of units in vectype. */
13729 : : optab1 = vec_unpacks_sbool_lo_optab;
13730 : : optab2 = vec_unpacks_sbool_hi_optab;
13731 : : }
13732 : :
13733 : 310259 : vec_mode = TYPE_MODE (vectype);
13734 : 310259 : if (widening_fn_p (code))
13735 : : {
13736 : : /* If this is an internal fn then we must check whether the target
13737 : : supports either a low-high split or an even-odd split. */
13738 : 54085 : internal_fn ifn = as_internal_fn ((combined_fn) code);
13739 : :
13740 : 54085 : internal_fn lo, hi, even, odd;
13741 : 54085 : lookup_hilo_internal_fn (ifn, &lo, &hi);
13742 : 54085 : if (BYTES_BIG_ENDIAN)
13743 : : std::swap (lo, hi);
13744 : 54085 : *code1 = as_combined_fn (lo);
13745 : 54085 : *code2 = as_combined_fn (hi);
13746 : 54085 : optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
13747 : 54085 : optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
13748 : :
13749 : : /* If we don't support low-high, then check for even-odd. */
13750 : 54085 : if (!optab1
13751 : 54085 : || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
13752 : 0 : || !optab2
13753 : 54085 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
13754 : : {
13755 : 54085 : lookup_evenodd_internal_fn (ifn, &even, &odd);
13756 : 54085 : *code1 = as_combined_fn (even);
13757 : 54085 : *code2 = as_combined_fn (odd);
13758 : 54085 : optab1 = direct_internal_fn_optab (even, {vectype, vectype});
13759 : 54085 : optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
13760 : : }
13761 : : }
13762 : 256174 : else if (code.is_tree_code ())
13763 : : {
13764 : 256174 : if (code == FIX_TRUNC_EXPR)
13765 : : {
13766 : : /* The signedness is determined from output operand. */
13767 : 206 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
13768 : 206 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
13769 : : }
13770 : 255968 : else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
13771 : 33636 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
13772 : 5572 : && VECTOR_BOOLEAN_TYPE_P (vectype)
13773 : 5572 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
13774 : 256215 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
13775 : : {
13776 : : /* If the input and result modes are the same, a different optab
13777 : : is needed where we pass in the number of units in vectype. */
13778 : : optab1 = vec_unpacks_sbool_lo_optab;
13779 : : optab2 = vec_unpacks_sbool_hi_optab;
13780 : : }
13781 : : else
13782 : : {
13783 : 255721 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
13784 : 255721 : optab2 = optab_for_tree_code (c2, vectype, optab_default);
13785 : : }
13786 : 256174 : *code1 = c1;
13787 : 256174 : *code2 = c2;
13788 : : }
13789 : :
13790 : 310259 : if (!optab1 || !optab2)
13791 : : return false;
13792 : :
13793 : 310259 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
13794 : 310259 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
13795 : 151178 : return false;
13796 : :
13797 : :
13798 : 159081 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
13799 : 159081 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
13800 : : {
13801 : 149532 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
13802 : : return true;
13803 : : /* For scalar masks we may have different boolean
13804 : : vector types having the same QImode. Thus we
13805 : : add additional check for elements number. */
13806 : 2664 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
13807 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
13808 : : return true;
13809 : : }
13810 : :
13811 : : /* Check if it's a multi-step conversion that can be done using intermediate
13812 : : types. */
13813 : :
13814 : 9649 : prev_type = vectype;
13815 : 9649 : prev_mode = vec_mode;
13816 : :
13817 : 161122 : if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
13818 : : return false;
13819 : :
13820 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
13821 : : intermediate steps in promotion sequence. We try
13822 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
13823 : : not. */
13824 : 9597 : interm_types->create (MAX_INTERM_CVT_STEPS);
13825 : 10920 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
13826 : : {
13827 : 10920 : intermediate_mode = insn_data[icode1].operand[0].mode;
13828 : 10920 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
13829 : 3872 : intermediate_type
13830 : 3872 : = vect_halve_mask_nunits (prev_type, intermediate_mode);
13831 : 7048 : else if (VECTOR_MODE_P (intermediate_mode))
13832 : : {
13833 : 7048 : tree intermediate_element_type
13834 : 7048 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
13835 : 7048 : TYPE_UNSIGNED (prev_type));
13836 : 7048 : intermediate_type
13837 : 7048 : = build_vector_type_for_mode (intermediate_element_type,
13838 : : intermediate_mode);
13839 : 7048 : }
13840 : : else
13841 : 0 : intermediate_type
13842 : 0 : = lang_hooks.types.type_for_mode (intermediate_mode,
13843 : 0 : TYPE_UNSIGNED (prev_type));
13844 : :
13845 : 10920 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
13846 : 3872 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
13847 : 3872 : && intermediate_mode == prev_mode
13848 : 10920 : && SCALAR_INT_MODE_P (prev_mode))
13849 : : {
13850 : : /* If the input and result modes are the same, a different optab
13851 : : is needed where we pass in the number of units in vectype. */
13852 : : optab3 = vec_unpacks_sbool_lo_optab;
13853 : : optab4 = vec_unpacks_sbool_hi_optab;
13854 : : }
13855 : : else
13856 : : {
13857 : 10920 : optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
13858 : 10920 : optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
13859 : : }
13860 : :
13861 : 10920 : if (!optab3 || !optab4
13862 : 10920 : || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
13863 : 10920 : || insn_data[icode1].operand[0].mode != intermediate_mode
13864 : 10920 : || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
13865 : 10920 : || insn_data[icode2].operand[0].mode != intermediate_mode
13866 : 10920 : || ((icode1 = optab_handler (optab3, intermediate_mode))
13867 : : == CODE_FOR_nothing)
13868 : 21545 : || ((icode2 = optab_handler (optab4, intermediate_mode))
13869 : : == CODE_FOR_nothing))
13870 : : break;
13871 : :
13872 : 10625 : interm_types->quick_push (intermediate_type);
13873 : 10625 : (*multi_step_cvt)++;
13874 : :
13875 : 10625 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
13876 : 10625 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
13877 : : {
13878 : 9322 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
13879 : : return true;
13880 : 2709 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
13881 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
13882 : : return true;
13883 : : }
13884 : :
13885 : 1323 : prev_type = intermediate_type;
13886 : 1323 : prev_mode = intermediate_mode;
13887 : : }
13888 : :
13889 : 295 : interm_types->release ();
13890 : 295 : return false;
13891 : : }
13892 : :
13893 : :
13894 : : /* Function supportable_narrowing_operation
13895 : :
13896 : : Check whether an operation represented by the code CODE is a
13897 : : narrowing operation that is supported by the target platform in
13898 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
13899 : : and producing a result of type VECTYPE_OUT).
13900 : :
13901 : : Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
13902 : : and FLOAT. This function checks if these operations are supported by
13903 : : the target platform directly via vector tree-codes.
13904 : :
13905 : : Output:
13906 : : - CODE1 is the code of a vector operation to be used when
13907 : : vectorizing the operation, if available.
13908 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
13909 : : case of multi-step conversion (like int->short->char - in that case
13910 : : MULTI_STEP_CVT will be 1).
13911 : : - INTERM_TYPES contains the intermediate type required to perform the
13912 : : narrowing operation (short in the above example). */
13913 : :
13914 : : bool
13915 : 35122 : supportable_narrowing_operation (code_helper code,
13916 : : tree vectype_out, tree vectype_in,
13917 : : code_helper *code1, int *multi_step_cvt,
13918 : : vec<tree> *interm_types)
13919 : : {
13920 : 35122 : machine_mode vec_mode;
13921 : 35122 : enum insn_code icode1;
13922 : 35122 : optab optab1, interm_optab;
13923 : 35122 : tree vectype = vectype_in;
13924 : 35122 : tree narrow_vectype = vectype_out;
13925 : 35122 : enum tree_code c1;
13926 : 35122 : tree intermediate_type, prev_type;
13927 : 35122 : machine_mode intermediate_mode, prev_mode;
13928 : 35122 : int i;
13929 : 35122 : unsigned HOST_WIDE_INT n_elts;
13930 : 35122 : bool uns;
13931 : :
13932 : 35122 : if (!code.is_tree_code ())
13933 : : return false;
13934 : :
13935 : 35122 : *multi_step_cvt = 0;
13936 : 35122 : switch ((tree_code) code)
13937 : : {
13938 : 34120 : CASE_CONVERT:
13939 : 34120 : c1 = VEC_PACK_TRUNC_EXPR;
13940 : 34120 : if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
13941 : 9042 : && VECTOR_BOOLEAN_TYPE_P (vectype)
13942 : 9042 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
13943 : 4003 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
13944 : 38123 : && n_elts < BITS_PER_UNIT)
13945 : : optab1 = vec_pack_sbool_trunc_optab;
13946 : : else
13947 : 32400 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
13948 : : break;
13949 : :
13950 : 810 : case FIX_TRUNC_EXPR:
13951 : 810 : c1 = VEC_PACK_FIX_TRUNC_EXPR;
13952 : : /* The signedness is determined from output operand. */
13953 : 810 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
13954 : 810 : break;
13955 : :
13956 : 192 : case FLOAT_EXPR:
13957 : 192 : c1 = VEC_PACK_FLOAT_EXPR;
13958 : 192 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
13959 : 192 : break;
13960 : :
13961 : 0 : default:
13962 : 0 : gcc_unreachable ();
13963 : : }
13964 : :
13965 : 35122 : if (!optab1)
13966 : : return false;
13967 : :
13968 : 35122 : vec_mode = TYPE_MODE (vectype);
13969 : 35122 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
13970 : : return false;
13971 : :
13972 : 31233 : *code1 = c1;
13973 : :
13974 : 31233 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
13975 : : {
13976 : 18788 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
13977 : : return true;
13978 : : /* For scalar masks we may have different boolean
13979 : : vector types having the same QImode. Thus we
13980 : : add additional check for elements number. */
13981 : 4450 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
13982 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
13983 : : return true;
13984 : : }
13985 : :
13986 : 12446 : if (code == FLOAT_EXPR)
13987 : : return false;
13988 : :
13989 : : /* Check if it's a multi-step conversion that can be done using intermediate
13990 : : types. */
13991 : 12446 : prev_mode = vec_mode;
13992 : 12446 : prev_type = vectype;
13993 : 12446 : if (code == FIX_TRUNC_EXPR)
13994 : 172 : uns = TYPE_UNSIGNED (vectype_out);
13995 : : else
13996 : 12274 : uns = TYPE_UNSIGNED (vectype);
13997 : :
13998 : : /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
13999 : : conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
14000 : : costly than signed. */
14001 : 12446 : if (code == FIX_TRUNC_EXPR && uns)
14002 : : {
14003 : 76 : enum insn_code icode2;
14004 : :
14005 : 76 : intermediate_type
14006 : 76 : = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
14007 : 76 : interm_optab
14008 : 76 : = optab_for_tree_code (c1, intermediate_type, optab_default);
14009 : 76 : if (interm_optab != unknown_optab
14010 : 76 : && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
14011 : 76 : && insn_data[icode1].operand[0].mode
14012 : 76 : == insn_data[icode2].operand[0].mode)
14013 : : {
14014 : : uns = false;
14015 : : optab1 = interm_optab;
14016 : : icode1 = icode2;
14017 : : }
14018 : : }
14019 : :
14020 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14021 : : intermediate steps in promotion sequence. We try
14022 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
14023 : 12446 : interm_types->create (MAX_INTERM_CVT_STEPS);
14024 : 26750 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14025 : : {
14026 : 14304 : intermediate_mode = insn_data[icode1].operand[0].mode;
14027 : 14304 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14028 : 5627 : intermediate_type
14029 : 5627 : = vect_double_mask_nunits (prev_type, intermediate_mode);
14030 : : else
14031 : 8677 : intermediate_type
14032 : 8677 : = lang_hooks.types.type_for_mode (intermediate_mode, uns);
14033 : 14304 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14034 : 5627 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
14035 : 5627 : && SCALAR_INT_MODE_P (prev_mode)
14036 : 2337 : && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
14037 : 16641 : && n_elts < BITS_PER_UNIT)
14038 : : interm_optab = vec_pack_sbool_trunc_optab;
14039 : : else
14040 : 14187 : interm_optab
14041 : 14187 : = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
14042 : : optab_default);
14043 : 117 : if (!interm_optab
14044 : 14304 : || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
14045 : 14304 : || insn_data[icode1].operand[0].mode != intermediate_mode
14046 : 28491 : || ((icode1 = optab_handler (interm_optab, intermediate_mode))
14047 : : == CODE_FOR_nothing))
14048 : : break;
14049 : :
14050 : 13556 : interm_types->quick_push (intermediate_type);
14051 : 13556 : (*multi_step_cvt)++;
14052 : :
14053 : 13556 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14054 : : {
14055 : 11698 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14056 : : return true;
14057 : 3790 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
14058 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14059 : : return true;
14060 : : }
14061 : :
14062 : 1858 : prev_mode = intermediate_mode;
14063 : 1858 : prev_type = intermediate_type;
14064 : 1858 : optab1 = interm_optab;
14065 : : }
14066 : :
14067 : 748 : interm_types->release ();
14068 : 748 : return false;
14069 : : }
14070 : :
14071 : : /* Function supportable_indirect_convert_operation
14072 : :
14073 : : Check whether an operation represented by the code CODE is single or multi
14074 : : operations that are supported by the target platform in
14075 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14076 : : producing a result of type VECTYPE_OUT).
14077 : :
14078 : : Convert operations we currently support directly are FIX_TRUNC and FLOAT.
14079 : : This function checks if these operations are supported
14080 : : by the target platform directly (via vector tree-codes).
14081 : :
14082 : : Output:
14083 : : - converts contains some pairs to perform the convert operation,
14084 : : the pair's first is the intermediate type, and its second is the code of
14085 : : a vector operation to be used when converting the operation from the
14086 : : previous type to the intermediate type. */
14087 : : bool
14088 : 70591 : supportable_indirect_convert_operation (code_helper code,
14089 : : tree vectype_out,
14090 : : tree vectype_in,
14091 : : vec<std::pair<tree, tree_code> > &converts,
14092 : : tree op0, slp_tree slp_op0)
14093 : : {
14094 : 70591 : bool found_mode = false;
14095 : 70591 : scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
14096 : 70591 : scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
14097 : 70591 : tree_code tc1, tc2, code1, code2;
14098 : :
14099 : 70591 : tree cvt_type = NULL_TREE;
14100 : 70591 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
14101 : :
14102 : 70591 : if (supportable_convert_operation ((tree_code) code,
14103 : : vectype_out,
14104 : : vectype_in,
14105 : : &tc1))
14106 : : {
14107 : 17240 : converts.safe_push (std::make_pair (vectype_out, tc1));
14108 : 17240 : return true;
14109 : : }
14110 : :
14111 : : /* For conversions between float and integer types try whether
14112 : : we can use intermediate signed integer types to support the
14113 : : conversion. */
14114 : 106702 : if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
14115 : 53351 : && (code == FLOAT_EXPR
14116 : 3013 : || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
14117 : : {
14118 : 376 : bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
14119 : 188 : bool float_expr_p = code == FLOAT_EXPR;
14120 : 188 : unsigned short target_size;
14121 : 188 : scalar_mode intermediate_mode;
14122 : 188 : if (demotion)
14123 : : {
14124 : 84 : intermediate_mode = lhs_mode;
14125 : 84 : target_size = GET_MODE_SIZE (rhs_mode);
14126 : : }
14127 : : else
14128 : : {
14129 : 104 : target_size = GET_MODE_SIZE (lhs_mode);
14130 : 104 : if (!int_mode_for_size
14131 : 104 : (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
14132 : 122 : return false;
14133 : : }
14134 : 188 : code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
14135 : : code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
14136 : 188 : opt_scalar_mode mode_iter;
14137 : 292 : FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
14138 : : {
14139 : 292 : intermediate_mode = mode_iter.require ();
14140 : :
14141 : 584 : if (GET_MODE_SIZE (intermediate_mode) > target_size)
14142 : : break;
14143 : :
14144 : 264 : scalar_mode cvt_mode;
14145 : 264 : if (!int_mode_for_size
14146 : 264 : (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
14147 : : break;
14148 : :
14149 : 234 : cvt_type = build_nonstandard_integer_type
14150 : 234 : (GET_MODE_BITSIZE (cvt_mode), 0);
14151 : :
14152 : : /* Check if the intermediate type can hold OP0's range.
14153 : : When converting from float to integer this is not necessary
14154 : : because values that do not fit the (smaller) target type are
14155 : : unspecified anyway. */
14156 : 234 : if (demotion && float_expr_p)
14157 : : {
14158 : 8 : wide_int op_min_value, op_max_value;
14159 : : /* For vector form, it looks like op0 doesn't have RANGE_INFO.
14160 : : In the future, if it is supported, changes may need to be made
14161 : : to this part, such as checking the RANGE of each element
14162 : : in the vector. */
14163 : 8 : if (slp_op0)
14164 : : {
14165 : 4 : tree def;
14166 : : /* ??? Merge ranges in case of more than one lane. */
14167 : 4 : if (SLP_TREE_LANES (slp_op0) != 1
14168 : 0 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
14169 : 4 : || !vect_get_range_info (def,
14170 : : &op_min_value, &op_max_value))
14171 : : break;
14172 : : }
14173 : 4 : else if (!op0
14174 : 0 : || TREE_CODE (op0) != SSA_NAME
14175 : 0 : || !SSA_NAME_RANGE_INFO (op0)
14176 : 4 : || !vect_get_range_info (op0, &op_min_value,
14177 : : &op_max_value))
14178 : : break;
14179 : :
14180 : 0 : if (cvt_type == NULL_TREE
14181 : 0 : || (wi::min_precision (op_max_value, SIGNED)
14182 : 0 : > TYPE_PRECISION (cvt_type))
14183 : 0 : || (wi::min_precision (op_min_value, SIGNED)
14184 : 0 : > TYPE_PRECISION (cvt_type)))
14185 : 0 : continue;
14186 : 8 : }
14187 : :
14188 : 226 : cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
14189 : : cvt_type,
14190 : : nelts);
14191 : : /* This should only happened for SLP as long as loop vectorizer
14192 : : only supports same-sized vector. */
14193 : 330 : if (cvt_type == NULL_TREE
14194 : 348 : || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
14195 : 226 : || !supportable_convert_operation ((tree_code) code1,
14196 : : vectype_out,
14197 : : cvt_type, &tc1)
14198 : 396 : || !supportable_convert_operation ((tree_code) code2,
14199 : : cvt_type,
14200 : : vectype_in, &tc2))
14201 : 104 : continue;
14202 : :
14203 : : found_mode = true;
14204 : : break;
14205 : : }
14206 : :
14207 : 188 : if (found_mode)
14208 : : {
14209 : 122 : converts.safe_push (std::make_pair (cvt_type, tc2));
14210 : 122 : if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
14211 : 122 : converts.safe_push (std::make_pair (vectype_out, tc1));
14212 : 122 : return true;
14213 : : }
14214 : : }
14215 : : return false;
14216 : : }
14217 : :
14218 : : /* Generate and return a vector mask of MASK_TYPE such that
14219 : : mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
14220 : : Add the statements to SEQ. */
14221 : :
14222 : : tree
14223 : 0 : vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
14224 : : tree end_index, const char *name)
14225 : : {
14226 : 0 : tree cmp_type = TREE_TYPE (start_index);
14227 : 0 : gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
14228 : : cmp_type, mask_type,
14229 : : OPTIMIZE_FOR_SPEED));
14230 : 0 : gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
14231 : : start_index, end_index,
14232 : : build_zero_cst (mask_type));
14233 : 0 : tree tmp;
14234 : 0 : if (name)
14235 : 0 : tmp = make_temp_ssa_name (mask_type, NULL, name);
14236 : : else
14237 : 0 : tmp = make_ssa_name (mask_type);
14238 : 0 : gimple_call_set_lhs (call, tmp);
14239 : 0 : gimple_seq_add_stmt (seq, call);
14240 : 0 : return tmp;
14241 : : }
14242 : :
14243 : : /* Generate a vector mask of type MASK_TYPE for which index I is false iff
14244 : : J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
14245 : :
14246 : : tree
14247 : 0 : vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
14248 : : tree end_index)
14249 : : {
14250 : 0 : tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
14251 : 0 : return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
14252 : : }
14253 : :
14254 : : /* Try to compute the vector types required to vectorize STMT_INFO,
14255 : : returning true on success and false if vectorization isn't possible.
14256 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
14257 : : take sure that the number of elements in the vectors is no bigger
14258 : : than GROUP_SIZE.
14259 : :
14260 : : On success:
14261 : :
14262 : : - Set *STMT_VECTYPE_OUT to:
14263 : : - NULL_TREE if the statement doesn't need to be vectorized;
14264 : : - the equivalent of STMT_VINFO_VECTYPE otherwise.
14265 : :
14266 : : - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
14267 : : number of units needed to vectorize STMT_INFO, or NULL_TREE if the
14268 : : statement does not help to determine the overall number of units. */
14269 : :
14270 : : opt_result
14271 : 5693238 : vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
14272 : : tree *stmt_vectype_out,
14273 : : tree *nunits_vectype_out,
14274 : : unsigned int group_size)
14275 : : {
14276 : 5693238 : gimple *stmt = stmt_info->stmt;
14277 : :
14278 : : /* For BB vectorization, we should always have a group size once we've
14279 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
14280 : : are tentative requests during things like early data reference
14281 : : analysis and pattern recognition. */
14282 : 5693238 : if (is_a <bb_vec_info> (vinfo))
14283 : 4440603 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
14284 : : else
14285 : : group_size = 0;
14286 : :
14287 : 5693238 : *stmt_vectype_out = NULL_TREE;
14288 : 5693238 : *nunits_vectype_out = NULL_TREE;
14289 : :
14290 : 5693238 : if (gimple_get_lhs (stmt) == NULL_TREE
14291 : : /* Allow vector conditionals through here. */
14292 : 1461 : && !is_a <gcond *> (stmt)
14293 : : /* MASK_STORE and friends have no lhs, but are ok. */
14294 : 5696140 : && !(is_gimple_call (stmt)
14295 : 1461 : && gimple_call_internal_p (stmt)
14296 : 1441 : && internal_store_fn_p (gimple_call_internal_fn (stmt))))
14297 : : {
14298 : 20 : if (is_a <gcall *> (stmt))
14299 : : {
14300 : : /* Ignore calls with no lhs. These must be calls to
14301 : : #pragma omp simd functions, and what vectorization factor
14302 : : it really needs can't be determined until
14303 : : vectorizable_simd_clone_call. */
14304 : 20 : if (dump_enabled_p ())
14305 : 18 : dump_printf_loc (MSG_NOTE, vect_location,
14306 : : "defer to SIMD clone analysis.\n");
14307 : 20 : return opt_result::success ();
14308 : : }
14309 : :
14310 : 0 : return opt_result::failure_at (stmt,
14311 : : "not vectorized: irregular stmt: %G", stmt);
14312 : : }
14313 : :
14314 : 5693218 : tree vectype;
14315 : 5693218 : tree scalar_type = NULL_TREE;
14316 : 5693218 : if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
14317 : : {
14318 : 1295582 : vectype = STMT_VINFO_VECTYPE (stmt_info);
14319 : 1295582 : if (dump_enabled_p ())
14320 : 75315 : dump_printf_loc (MSG_NOTE, vect_location,
14321 : : "precomputed vectype: %T\n", vectype);
14322 : : }
14323 : 4397636 : else if (vect_use_mask_type_p (stmt_info))
14324 : : {
14325 : 186470 : unsigned int precision = stmt_info->mask_precision;
14326 : 186470 : scalar_type = build_nonstandard_integer_type (precision, 1);
14327 : 186470 : vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
14328 : 186470 : if (!vectype)
14329 : 0 : return opt_result::failure_at (stmt, "not vectorized: unsupported"
14330 : : " data-type %T\n", scalar_type);
14331 : 186470 : if (dump_enabled_p ())
14332 : 4001 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14333 : : }
14334 : : else
14335 : : {
14336 : : /* If we got here with a gcond it means that the target had no available vector
14337 : : mode for the scalar type. We can't vectorize so abort. */
14338 : 4211166 : if (is_a <gcond *> (stmt))
14339 : 0 : return opt_result::failure_at (stmt,
14340 : : "not vectorized:"
14341 : : " unsupported data-type for gcond %T\n",
14342 : : scalar_type);
14343 : :
14344 : 4211166 : if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
14345 : 1457154 : scalar_type = TREE_TYPE (DR_REF (dr));
14346 : : else
14347 : 2754012 : scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
14348 : :
14349 : 4211166 : if (dump_enabled_p ())
14350 : : {
14351 : 66233 : if (group_size)
14352 : 7239 : dump_printf_loc (MSG_NOTE, vect_location,
14353 : : "get vectype for scalar type (group size %d):"
14354 : : " %T\n", group_size, scalar_type);
14355 : : else
14356 : 58994 : dump_printf_loc (MSG_NOTE, vect_location,
14357 : : "get vectype for scalar type: %T\n", scalar_type);
14358 : : }
14359 : 4211166 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14360 : 4211166 : if (!vectype)
14361 : 203354 : return opt_result::failure_at (stmt,
14362 : : "not vectorized:"
14363 : : " unsupported data-type %T\n",
14364 : : scalar_type);
14365 : :
14366 : 4007812 : if (dump_enabled_p ())
14367 : 66068 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14368 : : }
14369 : :
14370 : 4269597 : if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
14371 : 0 : return opt_result::failure_at (stmt,
14372 : : "not vectorized: vector stmt in loop:%G",
14373 : : stmt);
14374 : :
14375 : 5489864 : *stmt_vectype_out = vectype;
14376 : :
14377 : : /* Don't try to compute scalar types if the stmt produces a boolean
14378 : : vector; use the existing vector type instead. */
14379 : 5489864 : tree nunits_vectype = vectype;
14380 : 5489864 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14381 : : {
14382 : : /* The number of units is set according to the smallest scalar
14383 : : type (or the largest vector size, but we only support one
14384 : : vector size per vectorization). */
14385 : 5021279 : scalar_type = vect_get_smallest_scalar_type (stmt_info,
14386 : 5021279 : TREE_TYPE (vectype));
14387 : 5021279 : if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
14388 : : {
14389 : 1013941 : if (dump_enabled_p ())
14390 : 9654 : dump_printf_loc (MSG_NOTE, vect_location,
14391 : : "get vectype for smallest scalar type: %T\n",
14392 : : scalar_type);
14393 : 1013941 : nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
14394 : : group_size);
14395 : 1013941 : if (!nunits_vectype)
14396 : 7 : return opt_result::failure_at
14397 : 7 : (stmt, "not vectorized: unsupported data-type %T\n",
14398 : : scalar_type);
14399 : 1013934 : if (dump_enabled_p ())
14400 : 9654 : dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
14401 : : nunits_vectype);
14402 : : }
14403 : : }
14404 : :
14405 : 5489857 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
14406 : 5489857 : TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
14407 : 0 : return opt_result::failure_at (stmt,
14408 : : "Not vectorized: Incompatible number "
14409 : : "of vector subparts between %T and %T\n",
14410 : : nunits_vectype, *stmt_vectype_out);
14411 : :
14412 : 5489857 : if (dump_enabled_p ())
14413 : : {
14414 : 145384 : dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
14415 : 145384 : dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
14416 : 145384 : dump_printf (MSG_NOTE, "\n");
14417 : : }
14418 : :
14419 : 5489857 : *nunits_vectype_out = nunits_vectype;
14420 : 5489857 : return opt_result::success ();
14421 : : }
14422 : :
14423 : : /* Generate and return statement sequence that sets vector length LEN that is:
14424 : :
14425 : : min_of_start_and_end = min (START_INDEX, END_INDEX);
14426 : : left_len = END_INDEX - min_of_start_and_end;
14427 : : rhs = min (left_len, LEN_LIMIT);
14428 : : LEN = rhs;
14429 : :
14430 : : Note: the cost of the code generated by this function is modeled
14431 : : by vect_estimate_min_profitable_iters, so changes here may need
14432 : : corresponding changes there. */
14433 : :
14434 : : gimple_seq
14435 : 0 : vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
14436 : : {
14437 : 0 : gimple_seq stmts = NULL;
14438 : 0 : tree len_type = TREE_TYPE (len);
14439 : 0 : gcc_assert (TREE_TYPE (start_index) == len_type);
14440 : :
14441 : 0 : tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
14442 : 0 : tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
14443 : 0 : tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
14444 : 0 : gimple* stmt = gimple_build_assign (len, rhs);
14445 : 0 : gimple_seq_add_stmt (&stmts, stmt);
14446 : :
14447 : 0 : return stmts;
14448 : : }
14449 : :
|