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 : 3825306 : stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
72 : : {
73 : 3825306 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
74 : 3825306 : basic_block bb = gimple_bb (stmt);
75 : 3825306 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
76 : 773796 : class loop* loop;
77 : :
78 : 773796 : if (!loop_vinfo)
79 : : return false;
80 : :
81 : 773796 : loop = LOOP_VINFO_LOOP (loop_vinfo);
82 : :
83 : 773796 : 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 : 9510223 : 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 : 9510223 : if ((kind == vector_load || kind == unaligned_load)
98 : 1332766 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
99 : : kind = vector_gather_load;
100 : 9510223 : if ((kind == vector_store || kind == unaligned_store)
101 : 908257 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
102 : 9510223 : kind = vector_scatter_store;
103 : :
104 : 9510223 : stmt_info_for_cost si
105 : 9510223 : = { count, kind, where, stmt_info, node, vectype, misalign };
106 : 9510223 : body_cost_vec->safe_push (si);
107 : :
108 : 9510223 : return (unsigned)
109 : 9510223 : (builtin_vectorization_cost (kind, vectype, misalign) * count);
110 : : }
111 : :
112 : : unsigned
113 : 5173863 : 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 : 5173863 : return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
119 : 5173863 : vectype, misalign, where);
120 : : }
121 : :
122 : : unsigned
123 : 1502733 : 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 : 1502733 : return record_stmt_cost (body_cost_vec, count, kind,
129 : : SLP_TREE_REPRESENTATIVE (node), node,
130 : 1502733 : vectype, misalign, where);
131 : : }
132 : :
133 : : unsigned
134 : 141967 : 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 : 141967 : gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
139 : : || kind == scalar_stmt);
140 : 141967 : return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
141 : 141967 : 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 : 2953176 : vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
253 : : enum vect_relevant relevant, bool live_p)
254 : : {
255 : 2953176 : enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
256 : 2953176 : bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
257 : :
258 : 2953176 : if (dump_enabled_p ())
259 : 157772 : 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 : 2953176 : 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 : 166335 : if (dump_enabled_p ())
275 : 2221 : dump_printf_loc (MSG_NOTE, vect_location,
276 : : "last stmt in pattern. don't mark"
277 : : " relevant/live.\n");
278 : :
279 : 166335 : stmt_vec_info old_stmt_info = stmt_info;
280 : 166335 : stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
281 : 166335 : gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
282 : 166335 : save_relevant = STMT_VINFO_RELEVANT (stmt_info);
283 : 166335 : save_live_p = STMT_VINFO_LIVE_P (stmt_info);
284 : :
285 : 166335 : 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 : 166335 : if (dump_enabled_p ())
295 : 2221 : dump_printf_loc (MSG_NOTE, vect_location,
296 : : "mark relevant %d, live %d: %G", relevant, live_p,
297 : : stmt_info->stmt);
298 : : }
299 : :
300 : 2953176 : STMT_VINFO_LIVE_P (stmt_info) |= live_p;
301 : 2953176 : if (relevant > STMT_VINFO_RELEVANT (stmt_info))
302 : 2451558 : STMT_VINFO_RELEVANT (stmt_info) = relevant;
303 : :
304 : 2953176 : if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
305 : 501618 : && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
306 : : {
307 : 500968 : if (dump_enabled_p ())
308 : 20529 : dump_printf_loc (MSG_NOTE, vect_location,
309 : : "already marked relevant/live.\n");
310 : 500968 : return;
311 : : }
312 : :
313 : 2452208 : 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 : 356362 : is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
323 : : loop_vec_info loop_vinfo)
324 : : {
325 : 356362 : tree op;
326 : 356362 : ssa_op_iter iter;
327 : :
328 : 467776 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
329 : 112156 : if (!stmt)
330 : : return false;
331 : :
332 : 117210 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
333 : : {
334 : 116468 : enum vect_def_type dt = vect_uninitialized_def;
335 : :
336 : 116468 : if (!vect_is_simple_use (op, loop_vinfo, &dt))
337 : : {
338 : 699 : if (dump_enabled_p ())
339 : 15 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
340 : : "use not simple.\n");
341 : 111414 : return false;
342 : : }
343 : :
344 : 115769 : 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 : 3753424 : vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
365 : : enum vect_relevant *relevant, bool *live_p)
366 : : {
367 : 3753424 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
368 : 3753424 : ssa_op_iter op_iter;
369 : 3753424 : imm_use_iterator imm_iter;
370 : 3753424 : use_operand_p use_p;
371 : 3753424 : def_operand_p def_p;
372 : :
373 : 3753424 : *relevant = vect_unused_in_scope;
374 : 3753424 : *live_p = false;
375 : :
376 : : /* cond stmt other than loop exit cond. */
377 : 3753424 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
378 : 3753424 : if (is_ctrl_stmt (stmt)
379 : 470396 : && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
380 : 3915427 : && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
381 : 160383 : *relevant = vect_used_in_scope;
382 : :
383 : : /* changing memory. */
384 : 3753424 : if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
385 : 3115779 : if (gimple_vdef (stmt_info->stmt)
386 : 2645383 : && !gimple_clobber_p (stmt_info->stmt))
387 : : {
388 : 266942 : if (dump_enabled_p ())
389 : 25811 : dump_printf_loc (MSG_NOTE, vect_location,
390 : : "vec_stmt_relevant_p: stmt has vdefs.\n");
391 : 266942 : *relevant = vect_used_in_scope;
392 : 266942 : if (! STMT_VINFO_DATA_REF (stmt_info)
393 : 266942 : && 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 : 10516513 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
399 : : {
400 : 11003445 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
401 : : {
402 : 4984115 : basic_block bb = gimple_bb (USE_STMT (use_p));
403 : 4984115 : if (!flow_bb_inside_loop_p (loop, bb))
404 : : {
405 : 157415 : if (is_gimple_debug (USE_STMT (use_p)))
406 : 640 : continue;
407 : :
408 : 156775 : if (dump_enabled_p ())
409 : 5363 : 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 : 156775 : gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
415 : :
416 : 156775 : *live_p = true;
417 : : }
418 : 3009665 : }
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 : 3753424 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
427 : 1330756 : && is_a <gphi *> (stmt)
428 : 240197 : && gimple_bb (stmt) == LOOP_VINFO_LOOP (loop_vinfo)->header
429 : 3993615 : && ((! VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
430 : 236732 : && ! *live_p)
431 : 30324 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def))
432 : : {
433 : 234470 : if (dump_enabled_p ())
434 : 2700 : dump_printf_loc (MSG_NOTE, vect_location,
435 : : "vec_stmt_relevant_p: PHI forced live for "
436 : : "early break.\n");
437 : 234470 : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo).safe_push (stmt_info);
438 : 234470 : *live_p = true;
439 : : }
440 : :
441 : 356364 : if (*live_p && *relevant == vect_unused_in_scope
442 : 4109786 : && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
443 : : {
444 : 355620 : if (dump_enabled_p ())
445 : 7722 : dump_printf_loc (MSG_NOTE, vect_location,
446 : : "vec_stmt_relevant_p: stmt live but not relevant.\n");
447 : 355620 : *relevant = vect_used_only_live;
448 : : }
449 : :
450 : 3753424 : 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 : 3739408 : exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
461 : : {
462 : 3739408 : 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 : 3739408 : 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 : 1118300 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
484 : 1105848 : if (!assign || !gimple_assign_copy_p (assign))
485 : : {
486 : 615189 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
487 : 12452 : if (call && gimple_call_internal_p (call))
488 : : {
489 : 12452 : internal_fn ifn = gimple_call_internal_fn (call);
490 : 12452 : int mask_index = internal_fn_mask_index (ifn);
491 : 12452 : if (mask_index >= 0
492 : 12452 : && use == gimple_call_arg (call, mask_index))
493 : : return true;
494 : 8198 : int els_index = internal_fn_else_index (ifn);
495 : 8198 : if (els_index >= 0
496 : 8198 : && use == gimple_call_arg (call, els_index))
497 : : return true;
498 : 6971 : int stored_value_index = internal_fn_stored_value_index (ifn);
499 : 6971 : if (stored_value_index >= 0
500 : 6971 : && use == gimple_call_arg (call, stored_value_index))
501 : : return true;
502 : 5677 : if (internal_gather_scatter_fn_p (ifn)
503 : 5677 : && use == gimple_call_arg (call, 1))
504 : : return true;
505 : : }
506 : 608414 : return false;
507 : : }
508 : :
509 : 503111 : if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
510 : : return false;
511 : 503111 : operand = gimple_assign_rhs1 (assign);
512 : 503111 : if (TREE_CODE (operand) != SSA_NAME)
513 : : return false;
514 : :
515 : 436489 : 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 : 3768166 : 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 : 3768166 : stmt_vec_info dstmt_vinfo;
555 : 3768166 : 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 : 3768166 : if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
560 : 905691 : return opt_result::success ();
561 : :
562 : 2862475 : if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
563 : 8558 : return opt_result::failure_at (stmt_vinfo->stmt,
564 : : "not vectorized:"
565 : : " unsupported use in stmt.\n");
566 : :
567 : 2853917 : if (!dstmt_vinfo)
568 : 626510 : return opt_result::success ();
569 : :
570 : 2227407 : basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
571 : 2227407 : 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 : 2227407 : if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
577 : 385482 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
578 : 57528 : && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
579 : 57528 : && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
580 : 2284935 : && bb->loop_father == def_bb->loop_father)
581 : : {
582 : 57528 : if (dump_enabled_p ())
583 : 3529 : dump_printf_loc (MSG_NOTE, vect_location,
584 : : "reduc-stmt defining reduc-phi in the same nest.\n");
585 : 57528 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
586 : 57528 : 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 : 2169879 : if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
597 : : {
598 : 1833 : if (dump_enabled_p ())
599 : 281 : dump_printf_loc (MSG_NOTE, vect_location,
600 : : "outer-loop def-stmt defining inner-loop stmt.\n");
601 : :
602 : 1833 : 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 : 614 : case vect_used_in_outer_by_reduction:
610 : 614 : 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 : 2168046 : else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
635 : : {
636 : 1724 : 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 : 1724 : 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 : 2116806 : 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 : 2166322 : else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
665 : 325161 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
666 : 322477 : && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
667 : : loop_latch_edge (bb->loop_father))
668 : : == use)
669 : 2488799 : && (!LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
670 : 269404 : || (gimple_bb (stmt_vinfo->stmt)
671 : 269404 : != LOOP_VINFO_LOOP (loop_vinfo)->header)))
672 : : {
673 : 53073 : if (dump_enabled_p ())
674 : 3431 : dump_printf_loc (MSG_NOTE, vect_location,
675 : : "induction value on backedge.\n");
676 : 53073 : return opt_result::success ();
677 : : }
678 : :
679 : 2116806 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
680 : 2116806 : 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 : 313353 : vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
702 : : {
703 : 313353 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
704 : 313353 : basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
705 : 313353 : unsigned int nbbs = loop->num_nodes;
706 : 313353 : gimple_stmt_iterator si;
707 : 313353 : unsigned int i;
708 : 313353 : basic_block bb;
709 : 313353 : bool live_p;
710 : 313353 : enum vect_relevant relevant;
711 : :
712 : 313353 : DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
713 : :
714 : 313353 : auto_vec<stmt_vec_info, 64> worklist;
715 : :
716 : : /* 1. Init worklist. */
717 : 1095394 : for (i = 0; i < nbbs; i++)
718 : : {
719 : 787001 : bb = bbs[i];
720 : 1586178 : for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
721 : : {
722 : 1608044 : if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
723 : 166377 : continue;
724 : 637645 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
725 : 637645 : if (dump_enabled_p ())
726 : 37933 : dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
727 : : phi_info->stmt);
728 : :
729 : 637645 : if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
730 : : {
731 : 237998 : if (STMT_VINFO_DEF_TYPE (phi_info) == vect_unknown_def_type)
732 : 4845 : return opt_result::failure_at
733 : 4845 : (*si, "not vectorized: unhandled relevant PHI: %G", *si);
734 : 233153 : vect_mark_relevant (&worklist, phi_info, relevant, live_p);
735 : : }
736 : : }
737 : 5629595 : for (si = gsi_after_labels (bb); !gsi_end_p (si); gsi_next (&si))
738 : : {
739 : 4847554 : gimple *stmt = gsi_stmt (si);
740 : 4847554 : if (is_gimple_debug (stmt))
741 : 1731660 : continue;
742 : 3115894 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
743 : 3115894 : if (dump_enabled_p ())
744 : 202707 : dump_printf_loc (MSG_NOTE, vect_location,
745 : : "init: stmt relevant? %G", stmt);
746 : :
747 : 3115894 : if (gimple_get_lhs (stmt) == NULL_TREE
748 : 475688 : && !is_a <gcond *> (stmt)
749 : 3121186 : && !is_a <gcall *> (stmt))
750 : 115 : return opt_result::failure_at
751 : 115 : (stmt, "not vectorized: irregular stmt: %G", stmt);
752 : :
753 : 3115779 : if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
754 : 545689 : vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
755 : : }
756 : : }
757 : :
758 : : /* 2. Process_worklist */
759 : 2730698 : while (worklist.length () > 0)
760 : : {
761 : 2430865 : use_operand_p use_p;
762 : 2430865 : ssa_op_iter iter;
763 : :
764 : 2430865 : stmt_vec_info stmt_vinfo = worklist.pop ();
765 : 2430865 : if (dump_enabled_p ())
766 : 136901 : 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 : 2430865 : 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 : 2430865 : switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
786 : : {
787 : 115352 : case vect_reduction_def:
788 : 115352 : gcc_assert (relevant != vect_unused_in_scope);
789 : 115352 : if (relevant != vect_unused_in_scope
790 : 115352 : && relevant != vect_used_in_scope
791 : 115352 : && relevant != vect_used_by_reduction
792 : 115352 : && 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 : 1905 : case vect_nested_cycle:
798 : 1905 : if (relevant != vect_unused_in_scope
799 : 1905 : && 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 : 989 : case vect_double_reduction_def:
806 : 989 : if (relevant != vect_unused_in_scope
807 : 989 : && relevant != vect_used_by_reduction
808 : 335 : && 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 : 2430863 : 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 : 452270 : if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
823 : : {
824 : 292169 : enum tree_code rhs_code = gimple_assign_rhs_code (assign);
825 : 292169 : tree op = gimple_assign_rhs1 (assign);
826 : :
827 : 292169 : i = 1;
828 : 292169 : 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 : 841372 : for (; i < gimple_num_ops (assign); i++)
842 : : {
843 : 549589 : op = gimple_op (assign, i);
844 : 549589 : if (TREE_CODE (op) == SSA_NAME)
845 : : {
846 : 420548 : opt_result res
847 : 420548 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
848 : : &worklist, false);
849 : 420548 : if (!res)
850 : 386 : return res;
851 : : }
852 : : }
853 : : }
854 : 160101 : else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
855 : : {
856 : 155830 : tree_code rhs_code = gimple_cond_code (cond);
857 : 155830 : gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
858 : 155830 : opt_result res
859 : 155830 : = process_use (stmt_vinfo, gimple_cond_lhs (cond),
860 : : loop_vinfo, relevant, &worklist, false);
861 : 155830 : if (!res)
862 : 8560 : return res;
863 : 155830 : res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
864 : : loop_vinfo, relevant, &worklist, false);
865 : 155830 : if (!res)
866 : 0 : return res;
867 : : }
868 : 4271 : else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
869 : : {
870 : 20603 : for (i = 0; i < gimple_call_num_args (call); i++)
871 : : {
872 : 16332 : tree arg = gimple_call_arg (call, i);
873 : 16332 : opt_result res
874 : 16332 : = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
875 : : &worklist, false);
876 : 16332 : if (!res)
877 : 0 : return res;
878 : : }
879 : : }
880 : : else
881 : 0 : gcc_unreachable ();
882 : : }
883 : : else
884 : 6940763 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
885 : : {
886 : 2990868 : tree op = USE_FROM_PTR (use_p);
887 : 2990868 : opt_result res
888 : 2990868 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
889 : : &worklist, false);
890 : 2990868 : if (!res)
891 : 7291 : return res;
892 : : }
893 : :
894 : 2423186 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
895 : : {
896 : 28758 : gather_scatter_info gs_info;
897 : 28758 : if (!vect_check_gather_scatter (stmt_vinfo,
898 : : STMT_VINFO_VECTYPE (stmt_vinfo),
899 : : loop_vinfo, &gs_info))
900 : 0 : gcc_unreachable ();
901 : 28758 : opt_result res
902 : 28758 : = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
903 : : &worklist, true);
904 : 28758 : if (!res)
905 : : {
906 : 881 : if (fatal)
907 : 881 : *fatal = false;
908 : 881 : return res;
909 : : }
910 : : }
911 : : } /* while worklist */
912 : :
913 : 299833 : return opt_result::success ();
914 : 313353 : }
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 : 613413 : 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 : 613413 : int inside_cost = 0, prologue_cost = 0;
927 : :
928 : 613413 : gcc_assert (cost_vec != NULL);
929 : :
930 : 613413 : n *= vect_get_num_copies (vinfo, node);
931 : :
932 : : /* Pass the inside-of-loop statements to the target-specific cost model. */
933 : 613413 : inside_cost += record_stmt_cost (cost_vec, n, kind, node, 0, vect_body);
934 : :
935 : 613413 : if (dump_enabled_p ())
936 : 32103 : 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 : 613413 : }
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 : 55319 : 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 : 55319 : int i;
957 : 55319 : int inside_cost = 0, prologue_cost = 0;
958 : :
959 : 130601 : for (i = 0; i < pwr + 1; i++)
960 : : {
961 : 148609 : inside_cost += record_stmt_cost (cost_vec, ncopies,
962 : : widen_arith
963 : : ? vector_stmt : vec_promote_demote,
964 : : slp_node, 0, vect_body);
965 : 75282 : ncopies *= 2;
966 : : }
967 : :
968 : 55319 : if (dump_enabled_p ())
969 : 6105 : 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 : 55319 : }
973 : :
974 : : /* Returns true if the current function returns DECL. */
975 : :
976 : : static bool
977 : 547676 : cfun_returns (tree decl)
978 : : {
979 : 547676 : edge_iterator ei;
980 : 547676 : edge e;
981 : 1078089 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
982 : : {
983 : 1084546 : greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
984 : 542273 : if (!ret)
985 : 0 : continue;
986 : 542273 : 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 : 1595868 : do
993 : : {
994 : 3191736 : def = SSA_NAME_DEF_STMT (gimple_vuse (def));
995 : : }
996 : 1595868 : while (gimple_clobber_p (def));
997 : 531083 : if (is_a <gassign *> (def)
998 : 62901 : && gimple_assign_lhs (def) == gimple_return_retval (ret)
999 : 539285 : && 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 : 898043 : 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 : 898043 : tree vectype
1014 : 898043 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1015 : 898043 : switch (alignment_support_scheme)
1016 : : {
1017 : 477723 : case dr_aligned:
1018 : 477723 : {
1019 : 477723 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1020 : : vector_store, stmt_info, slp_node,
1021 : : vectype, 0, vect_body);
1022 : :
1023 : 477723 : if (dump_enabled_p ())
1024 : 13462 : dump_printf_loc (MSG_NOTE, vect_location,
1025 : : "vect_model_store_cost: aligned.\n");
1026 : : break;
1027 : : }
1028 : :
1029 : 420320 : case dr_unaligned_supported:
1030 : 420320 : {
1031 : : /* Here, we assign an additional cost for the unaligned store. */
1032 : 420320 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1033 : : unaligned_store, stmt_info, slp_node,
1034 : : vectype, misalignment, vect_body);
1035 : 420320 : if (dump_enabled_p ())
1036 : 12320 : 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 : 898043 : }
1056 : :
1057 : : /* Calculate cost of DR's memory access. */
1058 : : void
1059 : 726782 : 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 : 726782 : tree vectype
1069 : 726782 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1070 : 726782 : switch (alignment_support_scheme)
1071 : : {
1072 : 421912 : case dr_aligned:
1073 : 421912 : {
1074 : 421912 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1075 : : stmt_info, slp_node, vectype,
1076 : : 0, vect_body);
1077 : :
1078 : 421912 : if (dump_enabled_p ())
1079 : 17679 : dump_printf_loc (MSG_NOTE, vect_location,
1080 : : "vect_model_load_cost: aligned.\n");
1081 : :
1082 : : break;
1083 : : }
1084 : 254243 : case dr_unaligned_supported:
1085 : 254243 : {
1086 : : /* Here, we assign an additional cost for the unaligned load. */
1087 : 254243 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1088 : : unaligned_load, stmt_info, slp_node,
1089 : : vectype, misalignment, vect_body);
1090 : :
1091 : 254243 : if (dump_enabled_p ())
1092 : 20431 : 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 : 50627 : case dr_unaligned_unsupported:
1164 : 50627 : {
1165 : 50627 : *inside_cost = VECT_MAX_COST;
1166 : :
1167 : 50627 : 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 : 726782 : }
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 : 6257 : vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
1183 : : gimple_stmt_iterator *gsi)
1184 : : {
1185 : 6257 : if (gsi)
1186 : 2972 : vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
1187 : : else
1188 : 3285 : vinfo->insert_on_entry (stmt_vinfo, new_stmt);
1189 : :
1190 : 6257 : if (dump_enabled_p ())
1191 : 1754 : dump_printf_loc (MSG_NOTE, vect_location,
1192 : : "created new init_stmt: %G", new_stmt);
1193 : 6257 : }
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 : 4541 : vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
1207 : : gimple_stmt_iterator *gsi)
1208 : : {
1209 : 4541 : gimple *init_stmt;
1210 : 4541 : tree new_temp;
1211 : :
1212 : : /* We abuse this function to push sth to a SSA name with initial 'val'. */
1213 : 4541 : if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1214 : : {
1215 : 1336 : gcc_assert (VECTOR_TYPE_P (type));
1216 : 1336 : 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 : 8 : 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 : 8 : gimple_seq stmts = NULL;
1239 : 8 : if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1240 : 8 : val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
1241 : 8 : TREE_TYPE (type), val);
1242 : : else
1243 : : /* ??? Condition vectorization expects us to do
1244 : : promotion of invariant/external defs. */
1245 : 0 : val = gimple_convert (&stmts, TREE_TYPE (type), val);
1246 : 16 : for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
1247 : 16 : !gsi_end_p (gsi2); )
1248 : : {
1249 : 8 : init_stmt = gsi_stmt (gsi2);
1250 : 8 : gsi_remove (&gsi2, false);
1251 : 8 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1252 : : }
1253 : : }
1254 : : }
1255 : 1336 : val = build_vector_from_val (type, val);
1256 : : }
1257 : :
1258 : 4541 : new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1259 : 4541 : init_stmt = gimple_build_assign (new_temp, val);
1260 : 4541 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1261 : 4541 : return new_temp;
1262 : : }
1263 : :
1264 : :
1265 : : /* Get vectorized definitions for OP0 and OP1. */
1266 : :
1267 : : void
1268 : 186601 : 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 : 186601 : if (op0)
1275 : 184969 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
1276 : 186601 : if (op1)
1277 : 139556 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
1278 : 186601 : if (op2)
1279 : 9172 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
1280 : 186601 : if (op3)
1281 : 0 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
1282 : 186601 : }
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 : 1350032 : vect_finish_stmt_generation_1 (vec_info *,
1290 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1291 : : {
1292 : 1350032 : if (dump_enabled_p ())
1293 : 140737 : dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
1294 : :
1295 : 1350032 : if (stmt_info)
1296 : : {
1297 : 1319809 : 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 : 1319809 : int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
1303 : 1319809 : 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 : 30223 : gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
1308 : 1350032 : }
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 : 830 : vect_finish_replace_stmt (vec_info *vinfo,
1316 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1317 : : {
1318 : 830 : gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
1319 : 830 : gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
1320 : :
1321 : 830 : gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
1322 : 830 : gsi_replace (&gsi, vec_stmt, true);
1323 : :
1324 : 830 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1325 : 830 : }
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 : 1349202 : vect_finish_stmt_generation (vec_info *vinfo,
1332 : : stmt_vec_info stmt_info, gimple *vec_stmt,
1333 : : gimple_stmt_iterator *gsi)
1334 : : {
1335 : 1349202 : gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
1336 : :
1337 : 1349202 : if (!gsi_end_p (*gsi)
1338 : 2697385 : && gimple_has_mem_ops (vec_stmt))
1339 : : {
1340 : 1348183 : gimple *at_stmt = gsi_stmt (*gsi);
1341 : 1348183 : tree vuse = gimple_vuse (at_stmt);
1342 : 1341694 : if (vuse && TREE_CODE (vuse) == SSA_NAME)
1343 : : {
1344 : 1200283 : tree vdef = gimple_vdef (at_stmt);
1345 : 1200283 : gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1346 : 1200283 : 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 : 749877 : if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1352 : 1950124 : && ((is_gimple_assign (vec_stmt)
1353 : 749010 : && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1354 : 63186 : || (is_gimple_call (vec_stmt)
1355 : 831 : && (!(gimple_call_flags (vec_stmt)
1356 : 831 : & (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 : 687485 : tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1361 : 687485 : gimple_set_vdef (vec_stmt, new_vdef);
1362 : 687485 : SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1363 : : }
1364 : : }
1365 : : }
1366 : 1349202 : gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1367 : 1349202 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1368 : 1349202 : }
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 : 12018 : vectorizable_internal_function (combined_fn cfn, tree fndecl,
1377 : : tree vectype_out, tree vectype_in)
1378 : : {
1379 : 12018 : internal_fn ifn;
1380 : 12018 : if (internal_fn_p (cfn))
1381 : 9675 : ifn = as_internal_fn (cfn);
1382 : : else
1383 : 2343 : ifn = associated_internal_fn (fndecl);
1384 : 12018 : if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1385 : : {
1386 : 8574 : const direct_internal_fn_info &info = direct_internal_fn (ifn);
1387 : 8574 : if (info.vectorizable)
1388 : : {
1389 : 8574 : bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
1390 : 8574 : tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1391 : 8574 : 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 : 8574 : if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
1398 : : return IFN_LAST;
1399 : :
1400 : 8554 : 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 : 229006 : 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 : 229006 : vect_memory_access_type memory_access_type = ls->memory_access_type;
1443 : :
1444 : : /* Invariant loads need no special support. */
1445 : 229006 : if (memory_access_type == VMAT_INVARIANT)
1446 : 30640 : return;
1447 : :
1448 : : /* Figure whether the mask is uniform. scalar_mask is used to
1449 : : populate the scalar_cond_masked_set. */
1450 : 227778 : tree scalar_mask = NULL_TREE;
1451 : 227778 : if (mask_node)
1452 : 3440 : for (unsigned i = 0; i < SLP_TREE_LANES (mask_node); ++i)
1453 : : {
1454 : 1736 : tree def = vect_get_slp_scalar_def (mask_node, i);
1455 : 1736 : if (!def
1456 : 1736 : || (scalar_mask && def != scalar_mask))
1457 : : {
1458 : : scalar_mask = NULL;
1459 : : break;
1460 : : }
1461 : : else
1462 : 1720 : scalar_mask = def;
1463 : : }
1464 : :
1465 : 227778 : unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node);
1466 : 227778 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1467 : 227778 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
1468 : 227778 : machine_mode vecmode = TYPE_MODE (vectype);
1469 : 227778 : bool is_load = (vls_type == VLS_LOAD);
1470 : 227778 : 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 : 227778 : if (mat_gather_scatter_p (memory_access_type))
1495 : : {
1496 : 1435 : internal_fn ifn = (is_load
1497 : 1435 : ? IFN_MASK_GATHER_LOAD
1498 : : : IFN_MASK_SCATTER_STORE);
1499 : 330 : internal_fn len_ifn = (is_load
1500 : : ? IFN_MASK_LEN_GATHER_LOAD
1501 : : : IFN_MASK_LEN_SCATTER_STORE);
1502 : 1435 : stmt_vec_info repr = SLP_TREE_REPRESENTATIVE (slp_node);
1503 : 1435 : tree off_vectype = (STMT_VINFO_GATHER_SCATTER_P (repr)
1504 : 1435 : ? SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (slp_node)[0])
1505 : 1435 : : ls->strided_offset_vectype);
1506 : 1435 : tree memory_type = TREE_TYPE (DR_REF (STMT_VINFO_DR_INFO (repr)->dr));
1507 : 1435 : int scale = SLP_TREE_GS_SCALE (slp_node);
1508 : :
1509 : : /* The following "supported" checks just verify what we established in
1510 : : get_load_store_type and don't try different offset types.
1511 : : Therefore, off_vectype must be a supported offset type. In case
1512 : : we chose a different one use this instead. */
1513 : 1435 : if (ls->supported_offset_vectype)
1514 : 0 : off_vectype = ls->supported_offset_vectype;
1515 : : /* Same for scale. */
1516 : 1435 : if (ls->supported_scale)
1517 : 0 : scale = ls->supported_scale;
1518 : :
1519 : 1435 : if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
1520 : : memory_type,
1521 : : off_vectype, scale,
1522 : : elsvals))
1523 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1524 : 1435 : else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
1525 : : memory_type,
1526 : : off_vectype, scale,
1527 : : elsvals)
1528 : 1435 : || memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
1529 : 343 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1530 : : scalar_mask);
1531 : : else
1532 : : {
1533 : 1092 : if (dump_enabled_p ())
1534 : 22 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1535 : : "can't operate on partial vectors because"
1536 : : " the target doesn't have an appropriate"
1537 : : " gather load or scatter store instruction.\n");
1538 : 1092 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1539 : : }
1540 : 1435 : return;
1541 : : }
1542 : :
1543 : 226343 : if (memory_access_type != VMAT_CONTIGUOUS)
1544 : : {
1545 : : /* Element X of the data must come from iteration i * VF + X of the
1546 : : scalar loop. We need more work to support other mappings. */
1547 : 27977 : if (dump_enabled_p ())
1548 : 689 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1549 : : "can't operate on partial vectors because an"
1550 : : " access isn't contiguous.\n");
1551 : 27977 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1552 : 27977 : return;
1553 : : }
1554 : :
1555 : 198366 : if (!VECTOR_MODE_P (vecmode))
1556 : : {
1557 : 0 : if (dump_enabled_p ())
1558 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1559 : : "can't operate on partial vectors when emulating"
1560 : : " vector operations.\n");
1561 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1562 : 0 : return;
1563 : : }
1564 : :
1565 : : /* We might load more scalars than we need for permuting SLP loads.
1566 : : We checked in get_load_store_type that the extra elements
1567 : : don't leak into a new vector. */
1568 : 264115 : auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
1569 : : {
1570 : 65749 : unsigned int nvectors;
1571 : 131498 : if (can_div_away_from_zero_p (size, nunits, &nvectors))
1572 : 65749 : return nvectors;
1573 : : gcc_unreachable ();
1574 : : };
1575 : :
1576 : 198366 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1577 : 198366 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1578 : 198366 : machine_mode mask_mode;
1579 : 198366 : machine_mode vmode;
1580 : 198366 : bool using_partial_vectors_p = false;
1581 : 198366 : if (get_len_load_store_mode
1582 : 198366 : (vecmode, is_load, nullptr, elsvals).exists (&vmode))
1583 : : {
1584 : 0 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1585 : 0 : unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
1586 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
1587 : 0 : using_partial_vectors_p = true;
1588 : : }
1589 : 264115 : else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
1590 : 198366 : && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
1591 : : elsvals))
1592 : : {
1593 : 65749 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1594 : 65749 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
1595 : 65749 : using_partial_vectors_p = true;
1596 : : }
1597 : :
1598 : 65749 : if (!using_partial_vectors_p)
1599 : : {
1600 : 132617 : if (dump_enabled_p ())
1601 : 10734 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1602 : : "can't operate on partial vectors because the"
1603 : : " target doesn't have the appropriate partial"
1604 : : " vectorization load or store.\n");
1605 : 132617 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1606 : : }
1607 : : }
1608 : :
1609 : : /* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1610 : : form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1611 : : that needs to be applied to all loads and stores in a vectorized loop.
1612 : : Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
1613 : : otherwise return VEC_MASK & LOOP_MASK.
1614 : :
1615 : : MASK_TYPE is the type of both masks. If new statements are needed,
1616 : : insert them before GSI. */
1617 : :
1618 : : tree
1619 : 1575 : prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
1620 : : tree vec_mask, gimple_stmt_iterator *gsi)
1621 : : {
1622 : 1575 : gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1623 : 1575 : if (!loop_mask)
1624 : : return vec_mask;
1625 : :
1626 : 136 : gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1627 : :
1628 : 136 : if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
1629 : : return vec_mask;
1630 : :
1631 : 136 : tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1632 : 136 : gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1633 : : vec_mask, loop_mask);
1634 : :
1635 : 136 : gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1636 : 136 : return and_res;
1637 : : }
1638 : :
1639 : : /* Determine whether we can use a gather load or scatter store to vectorize
1640 : : strided load or store STMT_INFO by truncating the current offset to a
1641 : : smaller width. We need to be able to construct an offset vector:
1642 : :
1643 : : { 0, X, X*2, X*3, ... }
1644 : :
1645 : : without loss of precision, where X is STMT_INFO's DR_STEP.
1646 : :
1647 : : Return true if this is possible, describing the gather load or scatter
1648 : : store in GS_INFO. MASKED_P is true if the load or store is conditional.
1649 : :
1650 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1651 : : else values will be stored in the vector ELSVALS points to. */
1652 : :
1653 : : static bool
1654 : 60147 : vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info, tree vectype,
1655 : : loop_vec_info loop_vinfo, bool masked_p,
1656 : : gather_scatter_info *gs_info,
1657 : : vec<int> *elsvals)
1658 : : {
1659 : 60147 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1660 : 60147 : data_reference *dr = dr_info->dr;
1661 : 60147 : tree step = DR_STEP (dr);
1662 : 60147 : if (TREE_CODE (step) != INTEGER_CST)
1663 : : {
1664 : : /* ??? Perhaps we could use range information here? */
1665 : 30141 : if (dump_enabled_p ())
1666 : 223 : dump_printf_loc (MSG_NOTE, vect_location,
1667 : : "cannot truncate variable step.\n");
1668 : 30141 : return false;
1669 : : }
1670 : :
1671 : : /* Get the number of bits in an element. */
1672 : 30006 : scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
1673 : 30006 : unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
1674 : :
1675 : : /* Set COUNT to the upper limit on the number of elements - 1.
1676 : : Start with the maximum vectorization factor. */
1677 : 30006 : unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
1678 : :
1679 : : /* Try lowering COUNT to the number of scalar latch iterations. */
1680 : 30006 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1681 : 30006 : widest_int max_iters;
1682 : 30006 : if (max_loop_iterations (loop, &max_iters)
1683 : 60012 : && max_iters < count)
1684 : 2302 : count = max_iters.to_shwi ();
1685 : :
1686 : : /* Try scales of 1 and the element size. */
1687 : 30006 : unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
1688 : 30006 : wi::overflow_type overflow = wi::OVF_NONE;
1689 : 90018 : for (int i = 0; i < 2; ++i)
1690 : : {
1691 : 60012 : unsigned int scale = scales[i];
1692 : 60012 : widest_int factor;
1693 : 60012 : if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
1694 : 0 : continue;
1695 : :
1696 : : /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE. */
1697 : 60012 : widest_int range = wi::mul (count, factor, SIGNED, &overflow);
1698 : 60012 : if (overflow)
1699 : 0 : continue;
1700 : 60012 : signop sign = range >= 0 ? UNSIGNED : SIGNED;
1701 : 60012 : unsigned int min_offset_bits = wi::min_precision (range, sign);
1702 : :
1703 : : /* Find the narrowest viable offset type. */
1704 : 60012 : unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
1705 : 60012 : tree offset_type = build_nonstandard_integer_type (offset_bits,
1706 : : sign == UNSIGNED);
1707 : :
1708 : : /* See whether the target supports the operation with an offset
1709 : : no narrower than OFFSET_TYPE. */
1710 : 60012 : tree memory_type = TREE_TYPE (DR_REF (dr));
1711 : 60012 : tree tmp_offset_vectype;
1712 : 60012 : int tmp_scale;
1713 : 60012 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
1714 : : vectype, memory_type, offset_type,
1715 : : scale, &tmp_scale,
1716 : : &gs_info->ifn, &gs_info->offset_vectype,
1717 : : &tmp_offset_vectype, elsvals)
1718 : 60012 : || gs_info->ifn == IFN_LAST)
1719 : 60012 : continue;
1720 : :
1721 : 0 : gs_info->decl = NULL_TREE;
1722 : : /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
1723 : : but we don't need to store that here. */
1724 : 0 : gs_info->base = NULL_TREE;
1725 : 0 : gs_info->alias_ptr = build_int_cst
1726 : 0 : (reference_alias_ptr_type (DR_REF (dr)),
1727 : 0 : get_object_alignment (DR_REF (dr)));
1728 : 0 : gs_info->element_type = TREE_TYPE (vectype);
1729 : 0 : gs_info->offset = fold_convert (offset_type, step);
1730 : 0 : gs_info->scale = scale;
1731 : 0 : gs_info->memory_type = memory_type;
1732 : 0 : return true;
1733 : 120024 : }
1734 : :
1735 : 30006 : if (overflow && dump_enabled_p ())
1736 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1737 : : "truncating gather/scatter offset to %d bits"
1738 : : " might change its value.\n", element_bits);
1739 : :
1740 : : return false;
1741 : 30006 : }
1742 : :
1743 : : /* Return true if we can use gather/scatter or strided internal functions
1744 : : to vectorize STMT_INFO, which is a grouped or strided load or store
1745 : : with multiple lanes and will be implemented by a type-punned access
1746 : : of a vector with element size that matches the number of lanes.
1747 : :
1748 : : MASKED_P is true if load or store is conditional.
1749 : : When returning true, fill in GS_INFO with the information required to
1750 : : perform the operation. Also, store the punning type in PUNNED_VECTYPE.
1751 : :
1752 : : If successful and ELSVALS is nonzero the supported
1753 : : else values will be stored in the vector ELSVALS points to. */
1754 : :
1755 : : static bool
1756 : 3606 : vect_use_grouped_gather (dr_vec_info *dr_info, tree vectype,
1757 : : loop_vec_info loop_vinfo, bool masked_p,
1758 : : unsigned int nelts,
1759 : : gather_scatter_info *info, vec<int> *elsvals,
1760 : : tree *pun_vectype)
1761 : : {
1762 : 3606 : data_reference *dr = dr_info->dr;
1763 : :
1764 : : /* TODO: We can support nelts > BITS_PER_UNIT or non-power-of-two by
1765 : : multiple gathers/scatter. */
1766 : 6924 : if (nelts > BITS_PER_UNIT || !pow2p_hwi (nelts))
1767 : : return false;
1768 : :
1769 : : /* Pun the vectype with one of the same size but an element spanning
1770 : : NELTS elements of VECTYPE.
1771 : : The punned type of a V16QI with NELTS = 4 would be V4SI.
1772 : : */
1773 : 3081 : tree tmp;
1774 : 3081 : unsigned int pieces;
1775 : 3081 : if (!can_div_trunc_p (TYPE_VECTOR_SUBPARTS (vectype), nelts, &pieces)
1776 : 3081 : || !pieces)
1777 : 221 : return false;
1778 : :
1779 : 2860 : *pun_vectype = vector_vector_composition_type (vectype, pieces, &tmp, true);
1780 : :
1781 : 2860 : if (!*pun_vectype || !VECTOR_TYPE_P (*pun_vectype))
1782 : : return false;
1783 : :
1784 : 2492 : internal_fn ifn;
1785 : 2492 : tree offset_vectype = *pun_vectype;
1786 : :
1787 : 1595 : internal_fn strided_ifn = DR_IS_READ (dr)
1788 : 2492 : ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
1789 : :
1790 : : /* Check if we have a gather/scatter with the new type. We're just trying
1791 : : with the type itself as offset for now. If not, check if we have a
1792 : : strided load/store. These have fewer constraints (for example no offset
1793 : : type must exist) so it is possible that even though a gather/scatter is
1794 : : not available we still have a strided load/store. */
1795 : 2492 : bool ok = false;
1796 : 2492 : tree tmp_vectype;
1797 : 2492 : int tmp_scale;
1798 : 2492 : if (vect_gather_scatter_fn_p
1799 : 2492 : (loop_vinfo, DR_IS_READ (dr), masked_p, *pun_vectype,
1800 : 2492 : TREE_TYPE (*pun_vectype), *pun_vectype, 1, &tmp_scale, &ifn,
1801 : : &offset_vectype, &tmp_vectype, elsvals))
1802 : : ok = true;
1803 : 2492 : else if (internal_strided_fn_supported_p (strided_ifn, *pun_vectype,
1804 : : elsvals))
1805 : : {
1806 : : /* Use gather/scatter IFNs, vect_get_strided_load_store_ops
1807 : : will switch back to the strided variants. */
1808 : 0 : ifn = DR_IS_READ (dr) ? IFN_MASK_LEN_GATHER_LOAD :
1809 : : IFN_MASK_LEN_SCATTER_STORE;
1810 : 0 : ok = true;
1811 : : }
1812 : :
1813 : 0 : if (ok)
1814 : : {
1815 : 0 : info->ifn = ifn;
1816 : 0 : info->decl = NULL_TREE;
1817 : 0 : info->base = dr->ref;
1818 : 0 : info->alias_ptr = build_int_cst
1819 : 0 : (reference_alias_ptr_type (DR_REF (dr)),
1820 : 0 : get_object_alignment (DR_REF (dr)));
1821 : 0 : info->element_type = TREE_TYPE (*pun_vectype);
1822 : 0 : info->offset_vectype = offset_vectype;
1823 : : /* No need to set the offset, vect_get_strided_load_store_ops
1824 : : will do that. */
1825 : 0 : info->scale = 1;
1826 : 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
1827 : 0 : return true;
1828 : : }
1829 : :
1830 : : return false;
1831 : : }
1832 : :
1833 : :
1834 : : /* Return true if we can use gather/scatter internal functions to
1835 : : vectorize STMT_INFO, which is a grouped or strided load or store.
1836 : : MASKED_P is true if load or store is conditional. When returning
1837 : : true, fill in GS_INFO with the information required to perform the
1838 : : operation.
1839 : :
1840 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1841 : : else values will be stored in the vector ELSVALS points to. */
1842 : :
1843 : : static bool
1844 : 60147 : vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info, tree vectype,
1845 : : loop_vec_info loop_vinfo, bool masked_p,
1846 : : gather_scatter_info *gs_info,
1847 : : vec<int> *elsvals,
1848 : : unsigned int group_size,
1849 : : bool single_element_p)
1850 : : {
1851 : 60147 : if (!vect_check_gather_scatter (stmt_info, vectype,
1852 : : loop_vinfo, gs_info, elsvals)
1853 : 60147 : || gs_info->ifn == IFN_LAST)
1854 : : {
1855 : 60147 : if (!vect_truncate_gather_scatter_offset (stmt_info, vectype, loop_vinfo,
1856 : : masked_p, gs_info, elsvals))
1857 : : return false;
1858 : : }
1859 : :
1860 : 0 : if (!single_element_p
1861 : 0 : && !targetm.vectorize.prefer_gather_scatter (TYPE_MODE (vectype),
1862 : : gs_info->scale,
1863 : : group_size))
1864 : : return false;
1865 : :
1866 : 0 : if (dump_enabled_p ())
1867 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1868 : : "using gather/scatter for strided/grouped access,"
1869 : : " scale = %d\n", gs_info->scale);
1870 : :
1871 : : return true;
1872 : : }
1873 : :
1874 : : /* STMT_INFO is a non-strided load or store, meaning that it accesses
1875 : : elements with a known constant step. Return -1 if that step
1876 : : is negative, 0 if it is zero, and 1 if it is greater than zero. */
1877 : :
1878 : : int
1879 : 1341390 : compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
1880 : : {
1881 : 1341390 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1882 : 1341390 : return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
1883 : 1341390 : size_zero_node);
1884 : : }
1885 : :
1886 : : /* If the target supports a permute mask that reverses the elements in
1887 : : a vector of type VECTYPE, return that mask, otherwise return null. */
1888 : :
1889 : : tree
1890 : 8819 : perm_mask_for_reverse (tree vectype)
1891 : : {
1892 : 8819 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1893 : :
1894 : : /* The encoding has a single stepped pattern. */
1895 : 8819 : vec_perm_builder sel (nunits, 1, 3);
1896 : 35276 : for (int i = 0; i < 3; ++i)
1897 : 26457 : sel.quick_push (nunits - 1 - i);
1898 : :
1899 : 8819 : vec_perm_indices indices (sel, 1, nunits);
1900 : 8819 : if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
1901 : : indices))
1902 : : return NULL_TREE;
1903 : 7727 : return vect_gen_perm_mask_checked (vectype, indices);
1904 : 8819 : }
1905 : :
1906 : : /* A subroutine of get_load_store_type, with a subset of the same
1907 : : arguments. Handle the case where STMT_INFO is a load or store that
1908 : : accesses consecutive elements with a negative step. Sets *POFFSET
1909 : : to the offset to be applied to the DR for the first access. */
1910 : :
1911 : : static vect_memory_access_type
1912 : 10172 : get_negative_load_store_type (vec_info *vinfo,
1913 : : stmt_vec_info stmt_info, tree vectype,
1914 : : vec_load_store_type vls_type,
1915 : : unsigned int ncopies, poly_int64 *poffset)
1916 : : {
1917 : 10172 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1918 : 10172 : dr_alignment_support alignment_support_scheme;
1919 : :
1920 : 10172 : if (ncopies > 1)
1921 : : {
1922 : 0 : if (dump_enabled_p ())
1923 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1924 : : "multiple types with negative step.\n");
1925 : 0 : return VMAT_ELEMENTWISE;
1926 : : }
1927 : :
1928 : : /* For backward running DRs the first access in vectype actually is
1929 : : N-1 elements before the address of the DR. */
1930 : 10172 : *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
1931 : 10172 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1932 : :
1933 : 10172 : int misalignment = dr_misalignment (dr_info, vectype, *poffset);
1934 : 10172 : alignment_support_scheme
1935 : 10172 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
1936 : 10172 : if (alignment_support_scheme != dr_aligned
1937 : 10172 : && alignment_support_scheme != dr_unaligned_supported)
1938 : : {
1939 : 3184 : if (dump_enabled_p ())
1940 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1941 : : "negative step but alignment required.\n");
1942 : 3184 : *poffset = 0;
1943 : 3184 : return VMAT_ELEMENTWISE;
1944 : : }
1945 : :
1946 : 6988 : if (vls_type == VLS_STORE_INVARIANT)
1947 : : {
1948 : 711 : if (dump_enabled_p ())
1949 : 21 : dump_printf_loc (MSG_NOTE, vect_location,
1950 : : "negative step with invariant source;"
1951 : : " no permute needed.\n");
1952 : 711 : return VMAT_CONTIGUOUS_DOWN;
1953 : : }
1954 : :
1955 : 6277 : if (!perm_mask_for_reverse (vectype))
1956 : : {
1957 : 1092 : if (dump_enabled_p ())
1958 : 50 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1959 : : "negative step and reversing not supported.\n");
1960 : 1092 : *poffset = 0;
1961 : 1092 : return VMAT_ELEMENTWISE;
1962 : : }
1963 : :
1964 : : return VMAT_CONTIGUOUS_REVERSE;
1965 : : }
1966 : :
1967 : : /* STMT_INFO is either a masked or unconditional store. Return the value
1968 : : being stored. */
1969 : :
1970 : : tree
1971 : 0 : vect_get_store_rhs (stmt_vec_info stmt_info)
1972 : : {
1973 : 0 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
1974 : : {
1975 : 0 : gcc_assert (gimple_assign_single_p (assign));
1976 : 0 : return gimple_assign_rhs1 (assign);
1977 : : }
1978 : 0 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
1979 : : {
1980 : 0 : internal_fn ifn = gimple_call_internal_fn (call);
1981 : 0 : int index = internal_fn_stored_value_index (ifn);
1982 : 0 : gcc_assert (index >= 0);
1983 : 0 : return gimple_call_arg (call, index);
1984 : : }
1985 : 0 : gcc_unreachable ();
1986 : : }
1987 : :
1988 : : /* Function VECTOR_VECTOR_COMPOSITION_TYPE
1989 : :
1990 : : This function returns a vector type which can be composed with NELTS pieces,
1991 : : whose type is recorded in PTYPE. VTYPE should be a vector type, and has the
1992 : : same vector size as the return vector. It checks target whether supports
1993 : : pieces-size vector mode for construction firstly, if target fails to, check
1994 : : pieces-size scalar mode for construction further. It returns NULL_TREE if
1995 : : fails to find the available composition. If the caller only wants scalar
1996 : : pieces where PTYPE e.g. is a possible gather/scatter element type
1997 : : SCALAR_PTYPE_ONLY must be true.
1998 : :
1999 : : For example, for (vtype=V16QI, nelts=4), we can probably get:
2000 : : - V16QI with PTYPE V4QI.
2001 : : - V4SI with PTYPE SI.
2002 : : - NULL_TREE. */
2003 : :
2004 : : static tree
2005 : 12224 : vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype,
2006 : : bool scalar_ptype_only)
2007 : : {
2008 : 12224 : gcc_assert (VECTOR_TYPE_P (vtype));
2009 : 12224 : gcc_assert (known_gt (nelts, 0U));
2010 : :
2011 : 12224 : machine_mode vmode = TYPE_MODE (vtype);
2012 : 12224 : if (!VECTOR_MODE_P (vmode))
2013 : : return NULL_TREE;
2014 : :
2015 : : /* When we are asked to compose the vector from its components let
2016 : : that happen directly. */
2017 : 12224 : if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
2018 : : {
2019 : 5123 : *ptype = TREE_TYPE (vtype);
2020 : 5123 : return vtype;
2021 : : }
2022 : :
2023 : 14202 : poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
2024 : 7101 : unsigned int pbsize;
2025 : 7101 : if (constant_multiple_p (vbsize, nelts, &pbsize))
2026 : : {
2027 : : /* First check if vec_init optab supports construction from
2028 : : vector pieces directly. */
2029 : 7101 : scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
2030 : 14202 : poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
2031 : 7101 : machine_mode rmode;
2032 : 7101 : if (!scalar_ptype_only
2033 : 4241 : && related_vector_mode (vmode, elmode, inelts).exists (&rmode)
2034 : 10964 : && (convert_optab_handler (vec_init_optab, vmode, rmode)
2035 : : != CODE_FOR_nothing))
2036 : : {
2037 : 3253 : *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
2038 : 3253 : return vtype;
2039 : : }
2040 : :
2041 : : /* Otherwise check if exists an integer type of the same piece size and
2042 : : if vec_init optab supports construction from it directly. */
2043 : 3848 : if (int_mode_for_size (pbsize, 0).exists (&elmode)
2044 : 3848 : && related_vector_mode (vmode, elmode, nelts).exists (&rmode))
2045 : : {
2046 : 3444 : if (scalar_ptype_only
2047 : 3444 : || convert_optab_handler (vec_init_optab, rmode, elmode)
2048 : : != CODE_FOR_nothing)
2049 : : {
2050 : 3444 : *ptype = build_nonstandard_integer_type (pbsize, 1);
2051 : 3444 : return build_vector_type (*ptype, nelts);
2052 : : }
2053 : : }
2054 : : }
2055 : :
2056 : : return NULL_TREE;
2057 : : }
2058 : :
2059 : : /* Check if the load permutation of NODE only refers to a consecutive
2060 : : subset of the group indices where GROUP_SIZE is the size of the
2061 : : dataref's group. We also assert that the length of the permutation
2062 : : divides the group size and is a power of two.
2063 : : Such load permutations can be elided in strided access schemes as
2064 : : we can "jump over" the gap they leave. */
2065 : :
2066 : : bool
2067 : 45958 : has_consecutive_load_permutation (slp_tree node, unsigned group_size)
2068 : : {
2069 : 45958 : load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION (node);
2070 : 45958 : if (!perm.exists ()
2071 : 1837 : || perm.length () <= 1
2072 : 409 : || !pow2p_hwi (perm.length ())
2073 : 46353 : || group_size % perm.length ())
2074 : : return false;
2075 : :
2076 : 362 : return vect_load_perm_consecutive_p (node);
2077 : : }
2078 : :
2079 : :
2080 : : /* Analyze load or store SLP_NODE of type VLS_TYPE. Return true
2081 : : if there is a memory access type that the vectorized form can use,
2082 : : storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2083 : : or scatters, fill in GS_INFO accordingly. In addition
2084 : : *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
2085 : : the target does not support the alignment scheme. *MISALIGNMENT
2086 : : is set according to the alignment of the access (including
2087 : : DR_MISALIGNMENT_UNKNOWN when it is unknown).
2088 : :
2089 : : MASKED_P is true if the statement is conditional on a vectorized mask.
2090 : : VECTYPE is the vector type that the vectorized statements will use.
2091 : :
2092 : : If ELSVALS is nonzero the supported else values will be stored in the
2093 : : vector ELSVALS points to. */
2094 : :
2095 : : static bool
2096 : 1248371 : get_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2097 : : tree vectype, slp_tree slp_node,
2098 : : bool masked_p, vec_load_store_type vls_type,
2099 : : vect_load_store_data *ls)
2100 : : {
2101 : 1248371 : vect_memory_access_type *memory_access_type = &ls->memory_access_type;
2102 : 1248371 : poly_int64 *poffset = &ls->poffset;
2103 : 1248371 : dr_alignment_support *alignment_support_scheme
2104 : : = &ls->alignment_support_scheme;
2105 : 1248371 : int *misalignment = &ls->misalignment;
2106 : 1248371 : internal_fn *lanes_ifn = &ls->lanes_ifn;
2107 : 1248371 : vec<int> *elsvals = &ls->elsvals;
2108 : 1248371 : tree *ls_type = &ls->ls_type;
2109 : 1248371 : bool *slp_perm = &ls->slp_perm;
2110 : 1248371 : unsigned *n_perms = &ls->n_perms;
2111 : 1248371 : tree *supported_offset_vectype = &ls->supported_offset_vectype;
2112 : 1248371 : int *supported_scale = &ls->supported_scale;
2113 : 1248371 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2114 : 1248371 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2115 : 1248371 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2116 : 1248371 : stmt_vec_info first_stmt_info;
2117 : 1248371 : unsigned int group_size;
2118 : 1248371 : unsigned HOST_WIDE_INT gap;
2119 : 1248371 : bool single_element_p;
2120 : 1248371 : poly_int64 neg_ldst_offset = 0;
2121 : :
2122 : 1248371 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2123 : 1248371 : *poffset = 0;
2124 : 1248371 : *ls_type = NULL_TREE;
2125 : 1248371 : *slp_perm = false;
2126 : 1248371 : *n_perms = -1U;
2127 : 1248371 : ls->subchain_p = false;
2128 : :
2129 : 1248371 : bool perm_ok = true;
2130 : 1248371 : poly_int64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
2131 : :
2132 : 1248371 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2133 : 60761 : perm_ok = vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL,
2134 : 60761 : vf, true, n_perms);
2135 : :
2136 : 1248371 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2137 : : {
2138 : 866346 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2139 : 866346 : group_size = DR_GROUP_SIZE (first_stmt_info);
2140 : 866346 : gap = DR_GROUP_GAP (first_stmt_info);
2141 : 866346 : single_element_p = (stmt_info == first_stmt_info
2142 : 866346 : && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2143 : : }
2144 : : else
2145 : : {
2146 : : first_stmt_info = stmt_info;
2147 : : group_size = 1;
2148 : : gap = 0;
2149 : : single_element_p = true;
2150 : : }
2151 : 1248371 : dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
2152 : :
2153 : : /* True if the vectorized statements would access beyond the last
2154 : : statement in the group. */
2155 : 1248371 : bool overrun_p = false;
2156 : :
2157 : : /* True if we can cope with such overrun by peeling for gaps, so that
2158 : : there is at least one final scalar iteration after the vector loop. */
2159 : 2496742 : bool can_overrun_p = (!masked_p
2160 : 1248371 : && vls_type == VLS_LOAD
2161 : 464611 : && loop_vinfo
2162 : 1582315 : && !loop->inner);
2163 : :
2164 : : /* There can only be a gap at the end of the group if the stride is
2165 : : known at compile time. */
2166 : 1248371 : gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
2167 : :
2168 : : /* For SLP vectorization we directly vectorize a subchain
2169 : : without permutation. */
2170 : 1248371 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2171 : 1187610 : first_dr_info = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
2172 : :
2173 : 1248371 : if (STMT_VINFO_STRIDED_P (first_stmt_info))
2174 : : {
2175 : : /* Try to use consecutive accesses of as many elements as possible,
2176 : : separated by the stride, until we have a complete vector.
2177 : : Fall back to scalar accesses if that isn't possible. */
2178 : 45958 : *memory_access_type = VMAT_STRIDED_SLP;
2179 : :
2180 : : /* If the load permutation is consecutive we can reduce the group to
2181 : : the elements the permutation accesses. Then we release the
2182 : : permutation. */
2183 : 45958 : if (has_consecutive_load_permutation (slp_node, group_size))
2184 : : {
2185 : 20 : ls->subchain_p = true;
2186 : 20 : group_size = SLP_TREE_LANES (slp_node);
2187 : 20 : SLP_TREE_LOAD_PERMUTATION (slp_node).release ();
2188 : : }
2189 : : }
2190 : 1202413 : else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2191 : : {
2192 : 6393 : slp_tree offset_node = SLP_TREE_CHILDREN (slp_node)[0];
2193 : 6393 : tree offset_vectype = SLP_TREE_VECTYPE (offset_node);
2194 : 6393 : int scale = SLP_TREE_GS_SCALE (slp_node);
2195 : 6393 : tree memory_type = TREE_TYPE (DR_REF (first_dr_info->dr));
2196 : 6393 : tree tem;
2197 : 6393 : if (vect_gather_scatter_fn_p (loop_vinfo, vls_type == VLS_LOAD,
2198 : : masked_p, vectype, memory_type,
2199 : : offset_vectype, scale, supported_scale,
2200 : : &ls->gs.ifn, &tem,
2201 : : supported_offset_vectype, elsvals))
2202 : : {
2203 : 0 : if (dump_enabled_p ())
2204 : : {
2205 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2206 : : "gather/scatter with required "
2207 : : "offset type "
2208 : : "%T and offset scale %d.\n",
2209 : : offset_vectype, scale);
2210 : 0 : if (*supported_offset_vectype)
2211 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2212 : : " target supports offset type %T.\n",
2213 : : *supported_offset_vectype);
2214 : 0 : if (*supported_scale)
2215 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
2216 : : " target supports offset scale %d.\n",
2217 : : *supported_scale);
2218 : : }
2219 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2220 : : }
2221 : 6393 : else if (vls_type == VLS_LOAD
2222 : 6393 : ? (targetm.vectorize.builtin_gather
2223 : 4986 : && (ls->gs.decl
2224 : 4986 : = targetm.vectorize.builtin_gather (vectype,
2225 : 4986 : TREE_TYPE
2226 : : (offset_vectype),
2227 : : scale)))
2228 : 1407 : : (targetm.vectorize.builtin_scatter
2229 : 1407 : && (ls->gs.decl
2230 : 1407 : = targetm.vectorize.builtin_scatter (vectype,
2231 : 1407 : TREE_TYPE
2232 : : (offset_vectype),
2233 : : scale))))
2234 : 349 : *memory_access_type = VMAT_GATHER_SCATTER_LEGACY;
2235 : : else
2236 : : {
2237 : : /* GATHER_SCATTER_EMULATED_P. */
2238 : 6044 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
2239 : 6044 : || !TYPE_VECTOR_SUBPARTS (offset_vectype).is_constant ()
2240 : 6044 : || VECTOR_BOOLEAN_TYPE_P (offset_vectype)
2241 : 6044 : || !constant_multiple_p (TYPE_VECTOR_SUBPARTS (offset_vectype),
2242 : 6044 : TYPE_VECTOR_SUBPARTS (vectype)))
2243 : : {
2244 : 2612 : if (dump_enabled_p ())
2245 : 442 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2246 : : "unsupported vector types for emulated "
2247 : : "gather.\n");
2248 : 2612 : return false;
2249 : : }
2250 : 3432 : *memory_access_type = VMAT_GATHER_SCATTER_EMULATED;
2251 : : }
2252 : : }
2253 : : else
2254 : : {
2255 : 1196020 : int cmp = compare_step_with_zero (vinfo, stmt_info);
2256 : 1196020 : if (cmp < 0)
2257 : : {
2258 : 10327 : if (single_element_p)
2259 : : /* ??? The VMAT_CONTIGUOUS_REVERSE code generation is
2260 : : only correct for single element "interleaving" SLP. */
2261 : 10172 : *memory_access_type = get_negative_load_store_type
2262 : 10172 : (vinfo, stmt_info, vectype, vls_type, 1,
2263 : : &neg_ldst_offset);
2264 : : else
2265 : : /* We can fall back to VMAT_STRIDED_SLP since that does
2266 : : not care whether the stride between the group instances
2267 : : is positive or negative. */
2268 : 155 : *memory_access_type = VMAT_STRIDED_SLP;
2269 : : }
2270 : 1185693 : else if (cmp == 0 && loop_vinfo)
2271 : : {
2272 : 3025 : gcc_assert (vls_type == VLS_LOAD);
2273 : 3025 : *memory_access_type = VMAT_INVARIANT;
2274 : : }
2275 : : /* Try using LOAD/STORE_LANES. */
2276 : 1182668 : else if (slp_node->ldst_lanes
2277 : 1182668 : && (*lanes_ifn
2278 : 0 : = (vls_type == VLS_LOAD
2279 : 0 : ? vect_load_lanes_supported (vectype, group_size,
2280 : : masked_p, elsvals)
2281 : 0 : : vect_store_lanes_supported (vectype, group_size,
2282 : : masked_p))) != IFN_LAST)
2283 : 0 : *memory_access_type = VMAT_LOAD_STORE_LANES;
2284 : 1182668 : else if (!loop_vinfo && slp_node->avoid_stlf_fail)
2285 : : {
2286 : 72 : *memory_access_type = VMAT_ELEMENTWISE;
2287 : 72 : if (dump_enabled_p ())
2288 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2289 : : "using element-wise load to avoid disrupting "
2290 : : "cross iteration store-to-load forwarding\n");
2291 : : }
2292 : : else
2293 : 1182596 : *memory_access_type = VMAT_CONTIGUOUS;
2294 : :
2295 : : /* If this is single-element interleaving with an element
2296 : : distance that leaves unused vector loads around fall back
2297 : : to elementwise access if possible - we otherwise least
2298 : : create very sub-optimal code in that case (and
2299 : : blow up memory, see PR65518). */
2300 : 1196020 : if (loop_vinfo
2301 : 1196020 : && single_element_p
2302 : 360523 : && (*memory_access_type == VMAT_CONTIGUOUS
2303 : 13197 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2304 : 1556543 : && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
2305 : : {
2306 : 13306 : *memory_access_type = VMAT_ELEMENTWISE;
2307 : 13306 : if (dump_enabled_p ())
2308 : 193 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2309 : : "single-element interleaving not supported "
2310 : : "for not adjacent vector loads, using "
2311 : : "elementwise access\n");
2312 : : }
2313 : :
2314 : : /* Also fall back to elementwise access in case we did not lower a
2315 : : permutation and cannot code generate it. */
2316 : 1196020 : if (loop_vinfo
2317 : 410461 : && *memory_access_type != VMAT_ELEMENTWISE
2318 : 392879 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2319 : 1218033 : && !perm_ok)
2320 : : {
2321 : 2039 : *memory_access_type = VMAT_ELEMENTWISE;
2322 : 2039 : if (dump_enabled_p ())
2323 : 209 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2324 : : "permutation not supported, using elementwise "
2325 : : "access\n");
2326 : : }
2327 : :
2328 : 410461 : overrun_p = (loop_vinfo && gap != 0
2329 : 1239185 : && *memory_access_type != VMAT_ELEMENTWISE);
2330 : 1196020 : if (overrun_p && vls_type != VLS_LOAD)
2331 : : {
2332 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2333 : : "Grouped store with gaps requires"
2334 : : " non-consecutive accesses\n");
2335 : 9 : return false;
2336 : : }
2337 : :
2338 : 1196020 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (first_dr_info);
2339 : 1196020 : poly_int64 off = 0;
2340 : 1196020 : if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2341 : 5033 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2342 : :
2343 : : /* An overrun is fine if the trailing elements are smaller
2344 : : than the alignment boundary B. Every vector access will
2345 : : be a multiple of B and so we are guaranteed to access a
2346 : : non-gap element in the same B-sized block. */
2347 : 1196020 : if (overrun_p
2348 : 1196020 : && gap < (vect_known_alignment_in_bytes (first_dr_info,
2349 : 27772 : vectype, off) / dr_size))
2350 : : overrun_p = false;
2351 : :
2352 : : /* When we have a contiguous access across loop iterations
2353 : : but the access in the loop doesn't cover the full vector
2354 : : we can end up with no gap recorded but still excess
2355 : : elements accessed, see PR103116. Make sure we peel for
2356 : : gaps if necessary and sufficient and give up if not.
2357 : :
2358 : : If there is a combination of the access not covering the full
2359 : : vector and a gap recorded then we may need to peel twice. */
2360 : 1196020 : bool large_vector_overrun_p = false;
2361 : 1196020 : if (loop_vinfo
2362 : 410461 : && (*memory_access_type == VMAT_CONTIGUOUS
2363 : 28542 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2364 : 386952 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2365 : 1215710 : && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
2366 : : nunits))
2367 : : large_vector_overrun_p = overrun_p = true;
2368 : :
2369 : : /* If the gap splits the vector in half and the target
2370 : : can do half-vector operations avoid the epilogue peeling
2371 : : by simply loading half of the vector only. Usually
2372 : : the construction with an upper zero half will be elided. */
2373 : 1196020 : dr_alignment_support alss;
2374 : 1196020 : int misalign = dr_misalignment (first_dr_info, vectype, off);
2375 : 1196020 : tree half_vtype;
2376 : 1196020 : poly_uint64 remain;
2377 : 1196020 : unsigned HOST_WIDE_INT tem, num;
2378 : 1196020 : if (overrun_p
2379 : 1196020 : && !masked_p
2380 : 22312 : && *memory_access_type != VMAT_LOAD_STORE_LANES
2381 : 22312 : && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
2382 : : vectype, misalign)))
2383 : : == dr_aligned
2384 : 17428 : || alss == dr_unaligned_supported)
2385 : 12000 : && can_div_trunc_p (group_size
2386 : 12000 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2387 : : nunits, &tem, &remain)
2388 : 1208020 : && (known_eq (remain, 0u)
2389 : 7139 : || (known_ne (remain, 0u)
2390 : 5513 : && constant_multiple_p (nunits, remain, &num)
2391 : 1191159 : && (vector_vector_composition_type (vectype, num, &half_vtype)
2392 : : != NULL_TREE))))
2393 : 10374 : overrun_p = false;
2394 : :
2395 : 1196020 : if (overrun_p && !can_overrun_p)
2396 : : {
2397 : 6 : if (dump_enabled_p ())
2398 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2399 : : "Peeling for outer loop is not supported\n");
2400 : 6 : return false;
2401 : : }
2402 : :
2403 : : /* Peeling for gaps assumes that a single scalar iteration
2404 : : is enough to make sure the last vector iteration doesn't
2405 : : access excess elements. */
2406 : 1196014 : if (overrun_p
2407 : 1196014 : && (!can_div_trunc_p (group_size
2408 : 11932 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2409 : : nunits, &tem, &remain)
2410 : 11932 : || maybe_lt (remain + group_size, nunits)))
2411 : : {
2412 : : /* But peeling a single scalar iteration is enough if
2413 : : we can use the next power-of-two sized partial
2414 : : access and that is sufficiently small to be covered
2415 : : by the single scalar iteration. */
2416 : 16 : unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
2417 : 16 : if (masked_p
2418 : 16 : || !nunits.is_constant (&cnunits)
2419 : 16 : || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
2420 : 16 : || (((cremain = (group_size * cvf - gap) % cnunits), true)
2421 : 16 : && ((cpart_size = (1 << ceil_log2 (cremain))), true)
2422 : 16 : && (cremain + group_size < cpart_size
2423 : 13 : || (vector_vector_composition_type (vectype,
2424 : 13 : cnunits / cpart_size,
2425 : : &half_vtype)
2426 : : == NULL_TREE))))
2427 : : {
2428 : : /* If all fails we can still resort to niter masking unless
2429 : : the vectors used are too big, so enforce the use of
2430 : : partial vectors. */
2431 : 3 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
2432 : 3 : && !large_vector_overrun_p)
2433 : : {
2434 : 0 : if (dump_enabled_p ())
2435 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2436 : : "peeling for gaps insufficient for "
2437 : : "access unless using partial "
2438 : : "vectors\n");
2439 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2440 : : }
2441 : : else
2442 : : {
2443 : 3 : if (dump_enabled_p ())
2444 : 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2445 : : "peeling for gaps insufficient for "
2446 : : "access\n");
2447 : 3 : return false;
2448 : : }
2449 : : }
2450 : 13 : else if (large_vector_overrun_p)
2451 : : {
2452 : 13 : if (dump_enabled_p ())
2453 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2454 : : "can't operate on partial vectors because "
2455 : : "only unmasked loads handle access "
2456 : : "shortening required because of gaps at "
2457 : : "the end of the access\n");
2458 : 13 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
2459 : : }
2460 : : }
2461 : : }
2462 : :
2463 : : /* As a last resort, trying using a gather load or scatter store.
2464 : :
2465 : : ??? Although the code can handle all group sizes correctly,
2466 : : it probably isn't a win to use separate strided accesses based
2467 : : on nearby locations. Or, even if it's a win over scalar code,
2468 : : it might not be a win over vectorizing at a lower VF, if that
2469 : : allows us to use contiguous accesses. */
2470 : 1245750 : vect_memory_access_type grouped_gather_fallback = VMAT_UNINITIALIZED;
2471 : 1245750 : if (loop_vinfo
2472 : 460191 : && (*memory_access_type == VMAT_ELEMENTWISE
2473 : 460191 : || *memory_access_type == VMAT_STRIDED_SLP))
2474 : : {
2475 : 65731 : gather_scatter_info gs_info;
2476 : 65731 : if (SLP_TREE_LANES (slp_node) == 1
2477 : 61927 : && (!SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2478 : 16802 : || single_element_p)
2479 : 125878 : && vect_use_strided_gather_scatters_p (stmt_info, vectype, loop_vinfo,
2480 : : masked_p, &gs_info, elsvals,
2481 : : group_size, single_element_p))
2482 : : {
2483 : : /* vect_use_strided_gather_scatters_p does not save the actually
2484 : : supported scale and offset type so do that here.
2485 : : We need it later in check_load_store_for_partial_vectors
2486 : : where we only check if the given internal function is supported
2487 : : (to choose whether to use the IFN, LEGACY, or EMULATED flavor
2488 : : of gather/scatter) and don't re-do the full analysis. */
2489 : 0 : tree tmp;
2490 : 0 : gcc_assert (vect_gather_scatter_fn_p
2491 : : (loop_vinfo, vls_type == VLS_LOAD, masked_p, vectype,
2492 : : gs_info.memory_type, TREE_TYPE (gs_info.offset),
2493 : : gs_info.scale, supported_scale, &gs_info.ifn,
2494 : : &tmp, supported_offset_vectype, elsvals));
2495 : :
2496 : 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2497 : 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2498 : 0 : ls->gs.ifn = gs_info.ifn;
2499 : 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2500 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2501 : : }
2502 : 65731 : else if (SLP_TREE_LANES (slp_node) > 1
2503 : : && !masked_p
2504 : 3804 : && !single_element_p
2505 : 69337 : && vect_use_grouped_gather (STMT_VINFO_DR_INFO (stmt_info),
2506 : : vectype, loop_vinfo,
2507 : : masked_p, group_size,
2508 : : &gs_info, elsvals, ls_type))
2509 : : {
2510 : 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2511 : 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2512 : 0 : grouped_gather_fallback = *memory_access_type;
2513 : 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2514 : 0 : ls->gs.ifn = gs_info.ifn;
2515 : 0 : vectype = *ls_type;
2516 : 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2517 : : }
2518 : : }
2519 : :
2520 : 1245750 : if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
2521 : 1245750 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2522 : 5740 : *poffset = neg_ldst_offset;
2523 : :
2524 : 1245750 : if (*memory_access_type == VMAT_ELEMENTWISE
2525 : 1226057 : || *memory_access_type == VMAT_GATHER_SCATTER_LEGACY
2526 : 1225708 : || *memory_access_type == VMAT_STRIDED_SLP
2527 : 1179598 : || *memory_access_type == VMAT_INVARIANT)
2528 : : {
2529 : 69177 : *alignment_support_scheme = dr_unaligned_supported;
2530 : 69177 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2531 : : }
2532 : : else
2533 : : {
2534 : 1176573 : if (mat_gather_scatter_p (*memory_access_type)
2535 : : && !first_dr_info)
2536 : : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2537 : : else
2538 : 1176573 : *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
2539 : 1176573 : *alignment_support_scheme
2540 : 1176573 : = vect_supportable_dr_alignment
2541 : 1176573 : (vinfo, first_dr_info, vectype, *misalignment,
2542 : 1176573 : mat_gather_scatter_p (*memory_access_type));
2543 : 1176573 : if (grouped_gather_fallback != VMAT_UNINITIALIZED
2544 : 0 : && *alignment_support_scheme != dr_aligned
2545 : 0 : && *alignment_support_scheme != dr_unaligned_supported)
2546 : : {
2547 : : /* No supportable alignment for a grouped gather, fall back to the
2548 : : original memory access type. Even though VMAT_STRIDED_SLP might
2549 : : also try aligned vector loads it can still choose vector
2550 : : construction from scalars. */
2551 : 0 : *memory_access_type = grouped_gather_fallback;
2552 : 0 : *alignment_support_scheme = dr_unaligned_supported;
2553 : 0 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2554 : : }
2555 : : }
2556 : :
2557 : 1245750 : if (overrun_p)
2558 : : {
2559 : 11929 : gcc_assert (can_overrun_p);
2560 : 11929 : if (dump_enabled_p ())
2561 : 507 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2562 : : "Data access with gaps requires scalar "
2563 : : "epilogue loop\n");
2564 : 11929 : LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2565 : : }
2566 : :
2567 : 1245750 : if ((*memory_access_type == VMAT_ELEMENTWISE
2568 : 1245750 : || *memory_access_type == VMAT_STRIDED_SLP)
2569 : : && !nunits.is_constant ())
2570 : : {
2571 : : if (dump_enabled_p ())
2572 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2573 : : "Not using elementwise accesses due to variable "
2574 : : "vectorization factor.\n");
2575 : : return false;
2576 : : }
2577 : :
2578 : : /* Checks if all scalar iterations are known to be inbounds. */
2579 : 1245750 : bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
2580 : :
2581 : : /* Check if we support the operation if early breaks are needed. Here we
2582 : : must ensure that we don't access any more than the scalar code would
2583 : : have. A masked operation would ensure this, so for these load types
2584 : : force masking. */
2585 : 1245750 : if (loop_vinfo
2586 : 460191 : && dr_safe_speculative_read_required (stmt_info)
2587 : 164895 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
2588 : 1410645 : && (mat_gather_scatter_p (*memory_access_type)
2589 : 164487 : || *memory_access_type == VMAT_STRIDED_SLP))
2590 : : {
2591 : 6220 : if (dump_enabled_p ())
2592 : 8 : dump_printf_loc (MSG_NOTE, vect_location,
2593 : : "early break not supported: cannot peel for "
2594 : : "alignment. With non-contiguous memory vectorization"
2595 : : " could read out of bounds at %G ",
2596 : : STMT_VINFO_STMT (stmt_info));
2597 : 6220 : if (inbounds)
2598 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2599 : : else
2600 : : return false;
2601 : : }
2602 : :
2603 : : /* If this DR needs alignment for correctness, we must ensure the target
2604 : : alignment is a constant power-of-two multiple of the amount read per
2605 : : vector iteration or force masking. */
2606 : 1239530 : if (dr_safe_speculative_read_required (stmt_info)
2607 : 1239530 : && (*alignment_support_scheme == dr_aligned
2608 : 86219 : && !mat_gather_scatter_p (*memory_access_type)))
2609 : : {
2610 : : /* We can only peel for loops, of course. */
2611 : 86219 : gcc_checking_assert (loop_vinfo);
2612 : :
2613 : 86219 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2614 : 86219 : poly_uint64 read_amount
2615 : 86219 : = vf * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2616 : 86219 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2617 : 86219 : read_amount *= group_size;
2618 : :
2619 : 86219 : auto target_alignment
2620 : 86219 : = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
2621 : 86219 : if (!multiple_p (target_alignment, read_amount))
2622 : : {
2623 : 5342 : if (dump_enabled_p ())
2624 : : {
2625 : 10 : dump_printf_loc (MSG_NOTE, vect_location,
2626 : : "desired alignment not met, target was ");
2627 : 10 : dump_dec (MSG_NOTE, target_alignment);
2628 : 10 : dump_printf (MSG_NOTE, " previously, but read amount is ");
2629 : 10 : dump_dec (MSG_NOTE, read_amount);
2630 : 10 : dump_printf (MSG_NOTE, " at %G.\n", STMT_VINFO_STMT (stmt_info));
2631 : : }
2632 : 6536 : return false;
2633 : : }
2634 : :
2635 : : /* When using a group access the first element may be aligned but the
2636 : : subsequent loads may not be. For LOAD_LANES since the loads are based
2637 : : on the first DR then all loads in the group are aligned. For
2638 : : non-LOAD_LANES this is not the case. In particular a load + blend when
2639 : : there are gaps can have the non first loads issued unaligned, even
2640 : : partially overlapping the memory of the first load in order to simplify
2641 : : the blend. This is what the x86_64 backend does for instance. As
2642 : : such only the first load in the group is aligned, the rest are not.
2643 : : Because of this the permutes may break the alignment requirements that
2644 : : have been set, and as such we should for now, reject them. */
2645 : 80877 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2646 : : {
2647 : 1194 : if (dump_enabled_p ())
2648 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2649 : : "loads with load permutations not supported for "
2650 : : "speculative early break loads for %G",
2651 : : STMT_VINFO_STMT (stmt_info));
2652 : 1194 : return false;
2653 : : }
2654 : :
2655 : : /* Reject vectorization if we know the read mount per vector iteration
2656 : : exceeds the min page size. */
2657 : 79683 : if (known_gt (read_amount, (unsigned) param_min_pagesize))
2658 : : {
2659 : 0 : if (dump_enabled_p ())
2660 : : {
2661 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2662 : : "alignment required for correctness (");
2663 : 0 : dump_dec (MSG_MISSED_OPTIMIZATION, read_amount);
2664 : 0 : dump_printf (MSG_NOTE, ") may exceed page size.\n");
2665 : : }
2666 : 0 : return false;
2667 : : }
2668 : :
2669 : 79683 : if (!vf.is_constant ())
2670 : : {
2671 : : /* For VLA modes, we need a runtime check to ensure any speculative
2672 : : read amount does not exceed the page size. Here we record the max
2673 : : possible read amount for the check. */
2674 : : if (maybe_gt (read_amount,
2675 : : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo)))
2676 : : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo) = read_amount;
2677 : :
2678 : : /* For VLA modes, we must use partial vectors. */
2679 : : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2680 : : }
2681 : : }
2682 : :
2683 : 1232994 : if (*alignment_support_scheme == dr_unaligned_unsupported)
2684 : : {
2685 : 59833 : if (dump_enabled_p ())
2686 : 270 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2687 : : "unsupported unaligned access\n");
2688 : 59833 : return false;
2689 : : }
2690 : :
2691 : : /* FIXME: At the moment the cost model seems to underestimate the
2692 : : cost of using elementwise accesses. This check preserves the
2693 : : traditional behavior until that can be fixed. */
2694 : 1173161 : if (*memory_access_type == VMAT_ELEMENTWISE
2695 : 19693 : && !STMT_VINFO_STRIDED_P (first_stmt_info)
2696 : 1192854 : && !(STMT_VINFO_GROUPED_ACCESS (stmt_info)
2697 : 15788 : && single_element_p
2698 : 15182 : && !pow2p_hwi (group_size)))
2699 : : {
2700 : 9284 : if (dump_enabled_p ())
2701 : 346 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2702 : : "not falling back to elementwise accesses\n");
2703 : 9284 : return false;
2704 : : }
2705 : :
2706 : : /* For BB vectorization build up the vector from existing scalar defs. */
2707 : 1163877 : if (!loop_vinfo && *memory_access_type == VMAT_ELEMENTWISE)
2708 : : return false;
2709 : :
2710 : : /* Some loads need to explicitly permute the loaded data if there
2711 : : is a load permutation. Among those are:
2712 : : - VMAT_ELEMENTWISE.
2713 : : - VMAT_STRIDED_SLP.
2714 : : - VMAT_GATHER_SCATTER:
2715 : : - Strided gather (fallback for VMAT_STRIDED_SLP if #lanes == 1).
2716 : : - Grouped strided gather (ditto but for #lanes > 1).
2717 : :
2718 : : For VMAT_ELEMENTWISE we can fold the load permutation into the
2719 : : individual indices we access directly, eliding the permutation.
2720 : : Strided gather only allows load permutations for the
2721 : : single-element case. */
2722 : :
2723 : 1163877 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2724 : 1163877 : && !(*memory_access_type == VMAT_ELEMENTWISE
2725 : 38790 : || (mat_gather_scatter_p (*memory_access_type)
2726 : 0 : && SLP_TREE_LANES (slp_node) == 1
2727 : 0 : && single_element_p)))
2728 : : {
2729 : 38790 : if (!loop_vinfo)
2730 : : {
2731 : : /* In BB vectorization we may not actually use a loaded vector
2732 : : accessing elements in excess of DR_GROUP_SIZE. */
2733 : 23358 : stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2734 : 23358 : group_info = DR_GROUP_FIRST_ELEMENT (group_info);
2735 : 23358 : unsigned HOST_WIDE_INT nunits;
2736 : 23358 : unsigned j, k, maxk = 0;
2737 : 83828 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
2738 : 60470 : if (k > maxk)
2739 : : maxk = k;
2740 : 23358 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2741 : 42420 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
2742 : 23358 : || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
2743 : : {
2744 : 4296 : if (dump_enabled_p ())
2745 : 31 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2746 : : "BB vectorization with gaps at the end of "
2747 : : "a load is not supported\n");
2748 : 4296 : return false;
2749 : : }
2750 : : }
2751 : :
2752 : 34494 : if (!perm_ok)
2753 : : {
2754 : 1932 : if (dump_enabled_p ())
2755 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
2756 : : vect_location,
2757 : : "unsupported load permutation\n");
2758 : 1932 : return false;
2759 : : }
2760 : :
2761 : 32562 : *slp_perm = true;
2762 : : }
2763 : :
2764 : : return true;
2765 : : }
2766 : :
2767 : : /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
2768 : : conditional operation STMT_INFO. When returning true, store the mask
2769 : : in *MASK_NODE, the type of its definition in *MASK_DT_OUT and the type of
2770 : : the vectorized mask in *MASK_VECTYPE_OUT. */
2771 : :
2772 : : static bool
2773 : 7811 : vect_check_scalar_mask (vec_info *vinfo,
2774 : : slp_tree slp_node, unsigned mask_index,
2775 : : slp_tree *mask_node,
2776 : : vect_def_type *mask_dt_out, tree *mask_vectype_out)
2777 : : {
2778 : 7811 : enum vect_def_type mask_dt;
2779 : 7811 : tree mask_vectype;
2780 : 7811 : slp_tree mask_node_1;
2781 : 7811 : tree mask_;
2782 : 7811 : if (!vect_is_simple_use (vinfo, slp_node, mask_index,
2783 : : &mask_, &mask_node_1, &mask_dt, &mask_vectype))
2784 : : {
2785 : 0 : if (dump_enabled_p ())
2786 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2787 : : "mask use not simple.\n");
2788 : 0 : return false;
2789 : : }
2790 : :
2791 : 7811 : if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
2792 : 7811 : && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (mask_)))
2793 : : {
2794 : 0 : if (dump_enabled_p ())
2795 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2796 : : "mask argument is not a boolean.\n");
2797 : 0 : return false;
2798 : : }
2799 : :
2800 : 7811 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2801 : 7811 : if (!mask_vectype)
2802 : 17 : mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
2803 : : mask_node_1);
2804 : :
2805 : 7811 : if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2806 : : {
2807 : 0 : if (dump_enabled_p ())
2808 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2809 : : "could not find an appropriate vector mask type.\n");
2810 : 0 : return false;
2811 : : }
2812 : :
2813 : 7811 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2814 : 15622 : TYPE_VECTOR_SUBPARTS (vectype)))
2815 : : {
2816 : 0 : if (dump_enabled_p ())
2817 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2818 : : "vector mask type %T"
2819 : : " does not match vector data type %T.\n",
2820 : : mask_vectype, vectype);
2821 : :
2822 : 0 : return false;
2823 : : }
2824 : :
2825 : 7811 : *mask_dt_out = mask_dt;
2826 : 7811 : *mask_vectype_out = mask_vectype;
2827 : 7811 : *mask_node = mask_node_1;
2828 : 7811 : return true;
2829 : : }
2830 : :
2831 : :
2832 : : /* Return true if stored value is suitable for vectorizing store
2833 : : statement STMT_INFO. When returning true, store the scalar stored
2834 : : in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
2835 : : the type of the vectorized store value in
2836 : : *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
2837 : :
2838 : : static bool
2839 : 1327526 : vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
2840 : : slp_tree slp_node, slp_tree *rhs_node,
2841 : : vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
2842 : : vec_load_store_type *vls_type_out)
2843 : : {
2844 : 1327526 : int op_no = 0;
2845 : 1327526 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2846 : : {
2847 : 1444 : if (gimple_call_internal_p (call)
2848 : 1444 : && internal_store_fn_p (gimple_call_internal_fn (call)))
2849 : 1444 : op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
2850 : : }
2851 : 1327526 : op_no = vect_slp_child_index_for_operand
2852 : 1327526 : (stmt_info->stmt, op_no, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
2853 : :
2854 : 1327526 : enum vect_def_type rhs_dt;
2855 : 1327526 : tree rhs_vectype;
2856 : 1327526 : tree rhs;
2857 : 1327526 : if (!vect_is_simple_use (vinfo, slp_node, op_no,
2858 : : &rhs, rhs_node, &rhs_dt, &rhs_vectype))
2859 : : {
2860 : 0 : if (dump_enabled_p ())
2861 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2862 : : "use not simple.\n");
2863 : 0 : return false;
2864 : : }
2865 : :
2866 : : /* In the case this is a store from a constant make sure
2867 : : native_encode_expr can handle it. */
2868 : 1327526 : if (rhs_dt == vect_constant_def
2869 : 1327526 : && CONSTANT_CLASS_P (rhs) && native_encode_expr (rhs, NULL, 64) == 0)
2870 : : {
2871 : 0 : if (dump_enabled_p ())
2872 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2873 : : "cannot encode constant as a byte sequence.\n");
2874 : 0 : return false;
2875 : : }
2876 : :
2877 : 1327526 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2878 : 1327526 : if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2879 : : {
2880 : 24 : if (dump_enabled_p ())
2881 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2882 : : "incompatible vector types.\n");
2883 : 24 : return false;
2884 : : }
2885 : :
2886 : 1327502 : *rhs_dt_out = rhs_dt;
2887 : 1327502 : *rhs_vectype_out = rhs_vectype;
2888 : 1327502 : if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
2889 : 1004356 : *vls_type_out = VLS_STORE_INVARIANT;
2890 : : else
2891 : 323146 : *vls_type_out = VLS_STORE;
2892 : : return true;
2893 : : }
2894 : :
2895 : : /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
2896 : : Note that we support masks with floating-point type, in which case the
2897 : : floats are interpreted as a bitmask. */
2898 : :
2899 : : static tree
2900 : 165 : vect_build_all_ones_mask (vec_info *vinfo,
2901 : : stmt_vec_info stmt_info, tree masktype)
2902 : : {
2903 : 165 : if (TREE_CODE (masktype) == INTEGER_TYPE)
2904 : 98 : return build_int_cst (masktype, -1);
2905 : 67 : else if (VECTOR_BOOLEAN_TYPE_P (masktype)
2906 : 134 : || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2907 : : {
2908 : 14 : tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2909 : 14 : mask = build_vector_from_val (masktype, mask);
2910 : 14 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2911 : : }
2912 : 53 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2913 : : {
2914 : : REAL_VALUE_TYPE r;
2915 : : long tmp[6];
2916 : 371 : for (int j = 0; j < 6; ++j)
2917 : 318 : tmp[j] = -1;
2918 : 53 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2919 : 53 : tree mask = build_real (TREE_TYPE (masktype), r);
2920 : 53 : mask = build_vector_from_val (masktype, mask);
2921 : 53 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2922 : : }
2923 : 0 : gcc_unreachable ();
2924 : : }
2925 : :
2926 : : /* Build an all-zero merge value of type VECTYPE while vectorizing
2927 : : STMT_INFO as a gather load. */
2928 : :
2929 : : static tree
2930 : 158 : vect_build_zero_merge_argument (vec_info *vinfo,
2931 : : stmt_vec_info stmt_info, tree vectype)
2932 : : {
2933 : 158 : tree merge;
2934 : 158 : if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2935 : 49 : merge = build_int_cst (TREE_TYPE (vectype), 0);
2936 : 109 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2937 : : {
2938 : : REAL_VALUE_TYPE r;
2939 : : long tmp[6];
2940 : 763 : for (int j = 0; j < 6; ++j)
2941 : 654 : tmp[j] = 0;
2942 : 109 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2943 : 109 : merge = build_real (TREE_TYPE (vectype), r);
2944 : : }
2945 : : else
2946 : 0 : gcc_unreachable ();
2947 : 158 : merge = build_vector_from_val (vectype, merge);
2948 : 158 : return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
2949 : : }
2950 : :
2951 : : /* Return the corresponding else value for an else value constant
2952 : : ELSVAL with type TYPE. */
2953 : :
2954 : : tree
2955 : 1783 : vect_get_mask_load_else (int elsval, tree type)
2956 : : {
2957 : 1783 : tree els;
2958 : 1783 : if (elsval == MASK_LOAD_ELSE_UNDEFINED)
2959 : : {
2960 : 0 : tree tmp = create_tmp_var (type);
2961 : : /* No need to warn about anything. */
2962 : 0 : TREE_NO_WARNING (tmp) = 1;
2963 : 0 : els = get_or_create_ssa_default_def (cfun, tmp);
2964 : : }
2965 : 1783 : else if (elsval == MASK_LOAD_ELSE_M1)
2966 : 0 : els = build_minus_one_cst (type);
2967 : 1783 : else if (elsval == MASK_LOAD_ELSE_ZERO)
2968 : 1783 : els = build_zero_cst (type);
2969 : : else
2970 : 0 : gcc_unreachable ();
2971 : :
2972 : 1783 : return els;
2973 : : }
2974 : :
2975 : : /* Build a gather load call while vectorizing STMT_INFO. Insert new
2976 : : instructions before GSI and add them to VEC_STMT. GS_INFO describes
2977 : : the gather load operation. If the load is conditional, MASK is the
2978 : : vectorized condition, otherwise MASK is null. PTR is the base
2979 : : pointer and OFFSET is the vectorized offset. */
2980 : :
2981 : : static gimple *
2982 : 346 : vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
2983 : : slp_tree slp_node, tree vectype,
2984 : : gimple_stmt_iterator *gsi, tree decl,
2985 : : tree ptr, tree offset, tree mask)
2986 : : {
2987 : 346 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
2988 : 346 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
2989 : 346 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2990 : 346 : /* ptrtype */ arglist = TREE_CHAIN (arglist);
2991 : 346 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2992 : 346 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2993 : 346 : tree scaletype = TREE_VALUE (arglist);
2994 : 346 : tree var;
2995 : 346 : gcc_checking_assert (types_compatible_p (srctype, rettype)
2996 : : && (!mask
2997 : : || TREE_CODE (masktype) == INTEGER_TYPE
2998 : : || types_compatible_p (srctype, masktype)));
2999 : :
3000 : 346 : tree op = offset;
3001 : 346 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3002 : : {
3003 : 100 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3004 : : TYPE_VECTOR_SUBPARTS (idxtype)));
3005 : 100 : var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3006 : 100 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3007 : 100 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3008 : 100 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3009 : 100 : op = var;
3010 : : }
3011 : :
3012 : 346 : tree src_op = NULL_TREE;
3013 : 346 : tree mask_op = NULL_TREE;
3014 : 346 : if (mask)
3015 : : {
3016 : 188 : if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
3017 : : {
3018 : 188 : tree utype, optype = TREE_TYPE (mask);
3019 : 188 : if (VECTOR_TYPE_P (masktype)
3020 : 188 : || TYPE_MODE (masktype) == TYPE_MODE (optype))
3021 : : utype = masktype;
3022 : : else
3023 : 7 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3024 : 188 : var = vect_get_new_ssa_name (utype, vect_scalar_var);
3025 : 188 : tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
3026 : 188 : gassign *new_stmt
3027 : 188 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3028 : 188 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3029 : 188 : mask_arg = var;
3030 : 188 : if (!useless_type_conversion_p (masktype, utype))
3031 : : {
3032 : 7 : gcc_assert (TYPE_PRECISION (utype)
3033 : : <= TYPE_PRECISION (masktype));
3034 : 7 : var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3035 : 7 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3036 : 7 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3037 : 7 : mask_arg = var;
3038 : : }
3039 : 188 : src_op = build_zero_cst (srctype);
3040 : 188 : mask_op = mask_arg;
3041 : : }
3042 : : else
3043 : : {
3044 : : src_op = mask;
3045 : : mask_op = mask;
3046 : : }
3047 : : }
3048 : : else
3049 : : {
3050 : 158 : src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
3051 : 158 : mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
3052 : : }
3053 : :
3054 : 346 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
3055 : 346 : gimple *new_stmt = gimple_build_call (decl, 5, src_op, ptr, op,
3056 : : mask_op, scale);
3057 : :
3058 : 346 : if (!useless_type_conversion_p (vectype, rettype))
3059 : : {
3060 : 49 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
3061 : : TYPE_VECTOR_SUBPARTS (rettype)));
3062 : 49 : op = vect_get_new_ssa_name (rettype, vect_simple_var);
3063 : 49 : gimple_call_set_lhs (new_stmt, op);
3064 : 49 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3065 : 49 : op = build1 (VIEW_CONVERT_EXPR, vectype, op);
3066 : 49 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
3067 : : }
3068 : :
3069 : 346 : return new_stmt;
3070 : : }
3071 : :
3072 : : /* Build a scatter store call while vectorizing STMT_INFO. Insert new
3073 : : instructions before GSI. GS_INFO describes the scatter store operation.
3074 : : PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
3075 : : vectorized data to store.
3076 : : If the store is conditional, MASK is the vectorized condition, otherwise
3077 : : MASK is null. */
3078 : :
3079 : : static gimple *
3080 : 169 : vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
3081 : : slp_tree slp_node,
3082 : : gimple_stmt_iterator *gsi,
3083 : : tree decl,
3084 : : tree ptr, tree offset, tree oprnd, tree mask)
3085 : : {
3086 : 169 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
3087 : 169 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
3088 : 169 : /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
3089 : 169 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3090 : 169 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3091 : 169 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3092 : 169 : tree scaletype = TREE_VALUE (arglist);
3093 : 169 : gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
3094 : : && TREE_CODE (rettype) == VOID_TYPE);
3095 : :
3096 : 169 : tree mask_arg = NULL_TREE;
3097 : 169 : if (mask)
3098 : : {
3099 : 110 : mask_arg = mask;
3100 : 110 : tree optype = TREE_TYPE (mask_arg);
3101 : 110 : tree utype;
3102 : 110 : if (TYPE_MODE (masktype) == TYPE_MODE (optype))
3103 : : utype = masktype;
3104 : : else
3105 : 8 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3106 : 110 : tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
3107 : 110 : mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
3108 : 110 : gassign *new_stmt
3109 : 110 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3110 : 110 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3111 : 110 : mask_arg = var;
3112 : 110 : if (!useless_type_conversion_p (masktype, utype))
3113 : : {
3114 : 8 : gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
3115 : 8 : tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3116 : 8 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3117 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3118 : 8 : mask_arg = var;
3119 : : }
3120 : : }
3121 : : else
3122 : : {
3123 : 59 : mask_arg = build_int_cst (masktype, -1);
3124 : 59 : mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
3125 : : }
3126 : :
3127 : 169 : tree src = oprnd;
3128 : 169 : if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
3129 : : {
3130 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
3131 : : TYPE_VECTOR_SUBPARTS (srctype)));
3132 : 0 : tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
3133 : 0 : src = build1 (VIEW_CONVERT_EXPR, srctype, src);
3134 : 0 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
3135 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3136 : 0 : src = var;
3137 : : }
3138 : :
3139 : 169 : tree op = offset;
3140 : 169 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3141 : : {
3142 : 24 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3143 : : TYPE_VECTOR_SUBPARTS (idxtype)));
3144 : 24 : tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3145 : 24 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3146 : 24 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3147 : 24 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3148 : 24 : op = var;
3149 : : }
3150 : :
3151 : 169 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
3152 : 169 : gcall *new_stmt
3153 : 169 : = gimple_build_call (decl, 5, ptr, mask_arg, op, src, scale);
3154 : 169 : return new_stmt;
3155 : : }
3156 : :
3157 : : /* Prepare the base and offset in GS_INFO for vectorization.
3158 : : Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
3159 : : to the vectorized offset argument for the first copy of STMT_INFO.
3160 : : STMT_INFO is the statement described by GS_INFO and LOOP is the
3161 : : containing loop. */
3162 : :
3163 : : static void
3164 : 1223 : vect_get_gather_scatter_ops (class loop *loop, slp_tree slp_node,
3165 : : tree *dataref_ptr, vec<tree> *vec_offset)
3166 : : {
3167 : 1223 : gimple_seq stmts = NULL;
3168 : 1223 : *dataref_ptr = force_gimple_operand (SLP_TREE_GS_BASE (slp_node),
3169 : : &stmts, true, NULL_TREE);
3170 : 1223 : if (stmts != NULL)
3171 : : {
3172 : 986 : basic_block new_bb;
3173 : 986 : edge pe = loop_preheader_edge (loop);
3174 : 986 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3175 : 986 : gcc_assert (!new_bb);
3176 : : }
3177 : 1223 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
3178 : 1223 : }
3179 : :
3180 : : /* Prepare to implement a grouped or strided load or store using
3181 : : the gather load or scatter store operation described by GS_INFO.
3182 : : STMT_INFO is the load or store statement.
3183 : :
3184 : : Set *DATAREF_BUMP to the amount that should be added to the base
3185 : : address after each copy of the vectorized statement. Set *VEC_OFFSET
3186 : : to an invariant offset vector in which element I has the value
3187 : : I * DR_STEP / SCALE. */
3188 : :
3189 : : static void
3190 : 0 : vect_get_strided_load_store_ops (stmt_vec_info stmt_info, slp_tree node,
3191 : : tree vectype, tree offset_vectype,
3192 : : loop_vec_info loop_vinfo,
3193 : : gimple_stmt_iterator *gsi,
3194 : : tree *dataref_bump, tree *vec_offset,
3195 : : vec_loop_lens *loop_lens)
3196 : : {
3197 : 0 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3198 : :
3199 : 0 : if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3200 : : {
3201 : : /* _31 = .SELECT_VL (ivtmp_29, POLY_INT_CST [4, 4]);
3202 : : ivtmp_8 = _31 * 16 (step in bytes);
3203 : : .MASK_LEN_SCATTER_STORE (vectp_a.9_7, ... );
3204 : : vectp_a.9_26 = vectp_a.9_7 + ivtmp_8; */
3205 : 0 : tree loop_len
3206 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, vectype, 0, 0);
3207 : 0 : tree tmp
3208 : 0 : = fold_build2 (MULT_EXPR, sizetype,
3209 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3210 : : loop_len);
3211 : 0 : *dataref_bump = force_gimple_operand_gsi (gsi, tmp, true, NULL_TREE, true,
3212 : : GSI_SAME_STMT);
3213 : : }
3214 : : else
3215 : : {
3216 : 0 : tree bump
3217 : 0 : = size_binop (MULT_EXPR,
3218 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3219 : : size_int (TYPE_VECTOR_SUBPARTS (vectype)));
3220 : 0 : *dataref_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
3221 : : }
3222 : :
3223 : 0 : internal_fn ifn
3224 : 0 : = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
3225 : 0 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
3226 : : {
3227 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
3228 : : unshare_expr (DR_STEP (dr)));
3229 : 0 : return;
3230 : : }
3231 : :
3232 : : /* The offset given in GS_INFO can have pointer type, so use the element
3233 : : type of the vector instead. */
3234 : 0 : tree offset_type = TREE_TYPE (offset_vectype);
3235 : :
3236 : : /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
3237 : 0 : tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
3238 : : ssize_int (SLP_TREE_GS_SCALE (node)));
3239 : 0 : step = fold_convert (offset_type, step);
3240 : :
3241 : : /* Create {0, X, X*2, X*3, ...}. */
3242 : 0 : tree offset = fold_build2 (VEC_SERIES_EXPR, offset_vectype,
3243 : : build_zero_cst (offset_type), step);
3244 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
3245 : : }
3246 : :
3247 : : /* Prepare the pointer IVs which needs to be updated by a variable amount.
3248 : : Such variable amount is the outcome of .SELECT_VL. In this case, we can
3249 : : allow each iteration process the flexible number of elements as long as
3250 : : the number <= vf elments.
3251 : :
3252 : : Return data reference according to SELECT_VL.
3253 : : If new statements are needed, insert them before GSI. */
3254 : :
3255 : : static tree
3256 : 0 : vect_get_loop_variant_data_ptr_increment (
3257 : : vec_info *vinfo, tree aggr_type, gimple_stmt_iterator *gsi,
3258 : : vec_loop_lens *loop_lens, dr_vec_info *dr_info,
3259 : : vect_memory_access_type memory_access_type)
3260 : : {
3261 : 0 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3262 : 0 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3263 : :
3264 : : /* gather/scatter never reach here. */
3265 : 0 : gcc_assert (!mat_gather_scatter_p (memory_access_type));
3266 : :
3267 : : /* When we support SELECT_VL pattern, we dynamic adjust
3268 : : the memory address by .SELECT_VL result.
3269 : :
3270 : : The result of .SELECT_VL is the number of elements to
3271 : : be processed of each iteration. So the memory address
3272 : : adjustment operation should be:
3273 : :
3274 : : addr = addr + .SELECT_VL (ARG..) * step;
3275 : : */
3276 : 0 : tree loop_len
3277 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, aggr_type, 0, 0);
3278 : 0 : tree len_type = TREE_TYPE (loop_len);
3279 : : /* Since the outcome of .SELECT_VL is element size, we should adjust
3280 : : it into bytesize so that it can be used in address pointer variable
3281 : : amount IVs adjustment. */
3282 : 0 : tree tmp = fold_build2 (MULT_EXPR, len_type, loop_len,
3283 : : wide_int_to_tree (len_type, wi::to_widest (step)));
3284 : 0 : tree bump = make_temp_ssa_name (len_type, NULL, "ivtmp");
3285 : 0 : gassign *assign = gimple_build_assign (bump, tmp);
3286 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3287 : 0 : return bump;
3288 : : }
3289 : :
3290 : : /* Return the amount that should be added to a vector pointer to move
3291 : : to the next or previous copy of AGGR_TYPE. DR_INFO is the data reference
3292 : : being vectorized and MEMORY_ACCESS_TYPE describes the type of
3293 : : vectorization. */
3294 : :
3295 : : static tree
3296 : 699660 : vect_get_data_ptr_increment (vec_info *vinfo, gimple_stmt_iterator *gsi,
3297 : : dr_vec_info *dr_info, tree aggr_type,
3298 : : vect_memory_access_type memory_access_type,
3299 : : vec_loop_lens *loop_lens = nullptr)
3300 : : {
3301 : 699660 : if (memory_access_type == VMAT_INVARIANT)
3302 : 0 : return size_zero_node;
3303 : :
3304 : 699660 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3305 : 132609 : if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3306 : 0 : return vect_get_loop_variant_data_ptr_increment (vinfo, aggr_type, gsi,
3307 : : loop_lens, dr_info,
3308 : 0 : memory_access_type);
3309 : :
3310 : 699660 : tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3311 : 699660 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3312 : 699660 : if (tree_int_cst_sgn (step) == -1)
3313 : 2803 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3314 : : return iv_step;
3315 : : }
3316 : :
3317 : : /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}. */
3318 : :
3319 : : static bool
3320 : 206 : vectorizable_bswap (vec_info *vinfo,
3321 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3322 : : slp_tree slp_node,
3323 : : slp_tree *slp_op,
3324 : : tree vectype_in, stmt_vector_for_cost *cost_vec)
3325 : : {
3326 : 206 : tree op, vectype;
3327 : 206 : gcall *stmt = as_a <gcall *> (stmt_info->stmt);
3328 : :
3329 : 206 : op = gimple_call_arg (stmt, 0);
3330 : 206 : vectype = SLP_TREE_VECTYPE (slp_node);
3331 : 206 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3332 : :
3333 : 206 : if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
3334 : : {
3335 : 0 : if (dump_enabled_p ())
3336 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3337 : : "mismatched vector sizes %T and %T\n",
3338 : : vectype_in, vectype);
3339 : 0 : return false;
3340 : : }
3341 : :
3342 : 206 : tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
3343 : 206 : if (! char_vectype)
3344 : : return false;
3345 : :
3346 : 206 : poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
3347 : 206 : unsigned word_bytes;
3348 : 206 : if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
3349 : : return false;
3350 : :
3351 : : /* The encoding uses one stepped pattern for each byte in the word. */
3352 : 206 : vec_perm_builder elts (num_bytes, word_bytes, 3);
3353 : 824 : for (unsigned i = 0; i < 3; ++i)
3354 : 3318 : for (unsigned j = 0; j < word_bytes; ++j)
3355 : 2700 : elts.quick_push ((i + 1) * word_bytes - j - 1);
3356 : :
3357 : 206 : vec_perm_indices indices (elts, 1, num_bytes);
3358 : 206 : machine_mode vmode = TYPE_MODE (char_vectype);
3359 : 206 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3360 : : return false;
3361 : :
3362 : 152 : if (cost_vec)
3363 : : {
3364 : 140 : if (!vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
3365 : : {
3366 : 0 : if (dump_enabled_p ())
3367 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3368 : : "incompatible vector types for invariants\n");
3369 : 0 : return false;
3370 : : }
3371 : :
3372 : 140 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3373 : 140 : DUMP_VECT_SCOPE ("vectorizable_bswap");
3374 : 140 : record_stmt_cost (cost_vec,
3375 : : 1, vector_stmt, slp_node, 0, vect_prologue);
3376 : 140 : record_stmt_cost (cost_vec,
3377 : 140 : vect_get_num_copies (vinfo, slp_node),
3378 : : vec_perm, slp_node, 0, vect_body);
3379 : 140 : return true;
3380 : : }
3381 : :
3382 : 12 : tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
3383 : :
3384 : : /* Transform. */
3385 : 12 : vec<tree> vec_oprnds = vNULL;
3386 : 12 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
3387 : : /* Arguments are ready. create the new vector stmt. */
3388 : 12 : unsigned i;
3389 : 12 : tree vop;
3390 : 24 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3391 : : {
3392 : 12 : gimple *new_stmt;
3393 : 12 : tree tem = make_ssa_name (char_vectype);
3394 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3395 : : char_vectype, vop));
3396 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3397 : 12 : tree tem2 = make_ssa_name (char_vectype);
3398 : 12 : new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3399 : : tem, tem, bswap_vconst);
3400 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3401 : 12 : tem = make_ssa_name (vectype);
3402 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3403 : : vectype, tem2));
3404 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3405 : 12 : slp_node->push_vec_def (new_stmt);
3406 : : }
3407 : :
3408 : 12 : vec_oprnds.release ();
3409 : 12 : return true;
3410 : 206 : }
3411 : :
3412 : : /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3413 : : integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3414 : : in a single step. On success, store the binary pack code in
3415 : : *CONVERT_CODE. */
3416 : :
3417 : : static bool
3418 : 156 : simple_integer_narrowing (tree vectype_out, tree vectype_in,
3419 : : code_helper *convert_code)
3420 : : {
3421 : 312 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3422 : 312 : || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3423 : : return false;
3424 : :
3425 : 66 : code_helper code;
3426 : 66 : int multi_step_cvt = 0;
3427 : 66 : auto_vec <tree, 8> interm_types;
3428 : 97 : if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3429 : : &code, &multi_step_cvt, &interm_types)
3430 : 66 : || multi_step_cvt)
3431 : 31 : return false;
3432 : :
3433 : 35 : *convert_code = code;
3434 : 35 : return true;
3435 : 66 : }
3436 : :
3437 : : /* Function vectorizable_call.
3438 : :
3439 : : Check if STMT_INFO performs a function call that can be vectorized.
3440 : : If COST_VEC is passed, calculate costs but don't change anything,
3441 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
3442 : : it, and insert it at GSI.
3443 : : Return true if STMT_INFO is vectorizable in this way. */
3444 : :
3445 : : static bool
3446 : 2317718 : vectorizable_call (vec_info *vinfo,
3447 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3448 : : slp_tree slp_node,
3449 : : stmt_vector_for_cost *cost_vec)
3450 : : {
3451 : 2317718 : gcall *stmt;
3452 : 2317718 : tree vec_dest;
3453 : 2317718 : tree scalar_dest;
3454 : 2317718 : tree op;
3455 : 2317718 : tree vec_oprnd0 = NULL_TREE;
3456 : 2317718 : tree vectype_out, vectype_in;
3457 : 2317718 : poly_uint64 nunits_in;
3458 : 2317718 : poly_uint64 nunits_out;
3459 : 2317718 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3460 : 2317718 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3461 : 2317718 : tree fndecl, new_temp, rhs_type;
3462 : 2317718 : enum vect_def_type dt[4]
3463 : : = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3464 : : vect_unknown_def_type };
3465 : 2317718 : tree vectypes[ARRAY_SIZE (dt)] = {};
3466 : 2317718 : slp_tree slp_op[ARRAY_SIZE (dt)] = {};
3467 : 2317718 : auto_vec<tree, 8> vargs;
3468 : 2317718 : enum { NARROW, NONE, WIDEN } modifier;
3469 : 2317718 : size_t i, nargs;
3470 : 2317718 : tree clz_ctz_arg1 = NULL_TREE;
3471 : :
3472 : 2317718 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3473 : : return false;
3474 : :
3475 : 2317718 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3476 : 179117 : && cost_vec)
3477 : : return false;
3478 : :
3479 : : /* Is STMT_INFO a vectorizable call? */
3480 : 2327184 : stmt = dyn_cast <gcall *> (stmt_info->stmt);
3481 : 19269 : if (!stmt)
3482 : : return false;
3483 : :
3484 : 19269 : if (gimple_call_internal_p (stmt)
3485 : 19269 : && (internal_load_fn_p (gimple_call_internal_fn (stmt))
3486 : 11911 : || internal_store_fn_p (gimple_call_internal_fn (stmt))))
3487 : : /* Handled by vectorizable_load and vectorizable_store. */
3488 : 2664 : return false;
3489 : :
3490 : 16605 : if (gimple_call_lhs (stmt) == NULL_TREE
3491 : 16605 : || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3492 : : return false;
3493 : :
3494 : 16599 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
3495 : :
3496 : 16599 : vectype_out = SLP_TREE_VECTYPE (slp_node);
3497 : :
3498 : : /* Process function arguments. */
3499 : 16599 : rhs_type = NULL_TREE;
3500 : 16599 : vectype_in = NULL_TREE;
3501 : 16599 : nargs = gimple_call_num_args (stmt);
3502 : :
3503 : : /* Bail out if the function has more than four arguments, we do not have
3504 : : interesting builtin functions to vectorize with more than two arguments
3505 : : except for fma. No arguments is also not good. */
3506 : 16599 : if (nargs == 0 || nargs > 4)
3507 : : return false;
3508 : :
3509 : : /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic. */
3510 : 16519 : combined_fn cfn = gimple_call_combined_fn (stmt);
3511 : 16519 : if (cfn == CFN_GOMP_SIMD_LANE)
3512 : : {
3513 : 3207 : nargs = 0;
3514 : 3207 : rhs_type = unsigned_type_node;
3515 : : }
3516 : : /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
3517 : : argument just says whether it is well-defined at zero or not and what
3518 : : value should be returned for it. */
3519 : 16519 : if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
3520 : : {
3521 : 118 : nargs = 1;
3522 : 118 : clz_ctz_arg1 = gimple_call_arg (stmt, 1);
3523 : : }
3524 : :
3525 : 16519 : int mask_opno = -1;
3526 : 16519 : if (internal_fn_p (cfn))
3527 : : {
3528 : : /* We can only handle direct internal masked calls here,
3529 : : vectorizable_simd_clone_call is for the rest. */
3530 : 13701 : if (cfn == CFN_MASK_CALL)
3531 : : return false;
3532 : 13549 : mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3533 : : }
3534 : :
3535 : 45406 : for (i = 0; i < nargs; i++)
3536 : : {
3537 : 30142 : if ((int) i == mask_opno)
3538 : : {
3539 : 3992 : if (!vect_check_scalar_mask (vinfo, slp_node, mask_opno,
3540 : : &slp_op[i], &dt[i], &vectypes[i]))
3541 : : return false;
3542 : 3992 : continue;
3543 : : }
3544 : :
3545 : 26150 : if (!vect_is_simple_use (vinfo, slp_node,
3546 : : i, &op, &slp_op[i], &dt[i], &vectypes[i]))
3547 : : {
3548 : 0 : if (dump_enabled_p ())
3549 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3550 : : "use not simple.\n");
3551 : 0 : return false;
3552 : : }
3553 : :
3554 : : /* We can only handle calls with arguments of the same type. */
3555 : 26150 : if (rhs_type
3556 : 26150 : && !types_compatible_p (rhs_type, TREE_TYPE (op)))
3557 : : {
3558 : 1103 : if (dump_enabled_p ())
3559 : 200 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3560 : : "argument types differ.\n");
3561 : 1103 : return false;
3562 : : }
3563 : 25047 : if (!rhs_type)
3564 : 13160 : rhs_type = TREE_TYPE (op);
3565 : :
3566 : 25047 : if (!vectype_in)
3567 : 13636 : vectype_in = vectypes[i];
3568 : 11411 : else if (vectypes[i]
3569 : 11411 : && !types_compatible_p (vectypes[i], vectype_in))
3570 : : {
3571 : 0 : if (dump_enabled_p ())
3572 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3573 : : "argument vector types differ.\n");
3574 : 0 : return false;
3575 : : }
3576 : : }
3577 : : /* If all arguments are external or constant defs, infer the vector type
3578 : : from the scalar type. */
3579 : 15264 : if (!vectype_in)
3580 : 5531 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
3581 : 15264 : if (!cost_vec)
3582 : 4199 : gcc_assert (vectype_in);
3583 : 11065 : if (!vectype_in)
3584 : : {
3585 : 1052 : if (dump_enabled_p ())
3586 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3587 : : "no vectype for scalar type %T\n", rhs_type);
3588 : :
3589 : 1052 : return false;
3590 : : }
3591 : :
3592 : 28424 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3593 : 14212 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
3594 : : {
3595 : 12 : if (dump_enabled_p ())
3596 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3597 : : "mixed mask and nonmask vector types\n");
3598 : 12 : return false;
3599 : : }
3600 : :
3601 : 14200 : if (vect_emulated_vector_p (vectype_in)
3602 : 14200 : || vect_emulated_vector_p (vectype_out))
3603 : : {
3604 : 0 : if (dump_enabled_p ())
3605 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3606 : : "use emulated vector type for call\n");
3607 : 0 : return false;
3608 : : }
3609 : :
3610 : : /* FORNOW */
3611 : 14200 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3612 : 14200 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3613 : 14200 : if (known_eq (nunits_in * 2, nunits_out))
3614 : : modifier = NARROW;
3615 : 13658 : else if (known_eq (nunits_out, nunits_in))
3616 : : modifier = NONE;
3617 : 49 : else if (known_eq (nunits_out * 2, nunits_in))
3618 : : modifier = WIDEN;
3619 : : else
3620 : : return false;
3621 : :
3622 : : /* We only handle functions that do not read or clobber memory. */
3623 : 28400 : if (gimple_vuse (stmt))
3624 : : {
3625 : 1230 : if (dump_enabled_p ())
3626 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3627 : : "function reads from or writes to memory.\n");
3628 : 1230 : return false;
3629 : : }
3630 : :
3631 : : /* For now, we only vectorize functions if a target specific builtin
3632 : : is available. TODO -- in some cases, it might be profitable to
3633 : : insert the calls for pieces of the vector, in order to be able
3634 : : to vectorize other operations in the loop. */
3635 : 12970 : fndecl = NULL_TREE;
3636 : 12970 : internal_fn ifn = IFN_LAST;
3637 : 12970 : tree callee = gimple_call_fndecl (stmt);
3638 : :
3639 : : /* First try using an internal function. */
3640 : 12970 : code_helper convert_code = MAX_TREE_CODES;
3641 : 12970 : if (cfn != CFN_LAST
3642 : 12970 : && (modifier == NONE
3643 : 172 : || (modifier == NARROW
3644 : 156 : && simple_integer_narrowing (vectype_out, vectype_in,
3645 : : &convert_code))))
3646 : 12018 : ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3647 : : vectype_in);
3648 : :
3649 : : /* If that fails, try asking for a target-specific built-in function. */
3650 : 12018 : if (ifn == IFN_LAST)
3651 : : {
3652 : 6874 : if (cfn != CFN_LAST)
3653 : 6059 : fndecl = targetm.vectorize.builtin_vectorized_function
3654 : 6059 : (cfn, vectype_out, vectype_in);
3655 : 815 : else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
3656 : 24 : fndecl = targetm.vectorize.builtin_md_vectorized_function
3657 : 24 : (callee, vectype_out, vectype_in);
3658 : : }
3659 : :
3660 : 12970 : if (ifn == IFN_LAST && !fndecl)
3661 : : {
3662 : 6580 : if (cfn == CFN_GOMP_SIMD_LANE
3663 : 3207 : && SLP_TREE_LANES (slp_node) == 1
3664 : 3207 : && loop_vinfo
3665 : 3207 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3666 : 3207 : && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3667 : 12994 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3668 : 3207 : == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3669 : : {
3670 : : /* We can handle IFN_GOMP_SIMD_LANE by returning a
3671 : : { 0, 1, 2, ... vf - 1 } vector. */
3672 : 3207 : gcc_assert (nargs == 0);
3673 : : }
3674 : 3373 : else if (modifier == NONE
3675 : 3373 : && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3676 : 3039 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3677 : 2891 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
3678 : 2859 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
3679 : 206 : return vectorizable_bswap (vinfo, stmt_info, gsi, slp_node,
3680 : 206 : slp_op, vectype_in, cost_vec);
3681 : : else
3682 : : {
3683 : 3167 : if (dump_enabled_p ())
3684 : 236 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3685 : : "function is not vectorizable.\n");
3686 : 3167 : return false;
3687 : : }
3688 : : }
3689 : :
3690 : 9597 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
3691 : 9597 : internal_fn cond_fn = (internal_fn_mask_index (ifn) != -1
3692 : 9597 : ? ifn : get_conditional_internal_fn (ifn));
3693 : 9597 : internal_fn cond_len_fn = get_len_internal_fn (ifn);
3694 : 9597 : int len_opno = internal_fn_len_index (cond_len_fn);
3695 : 9597 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
3696 : 7743 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
3697 : 9597 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
3698 : 9597 : if (cost_vec) /* transformation not required. */
3699 : : {
3700 : 16236 : for (i = 0; i < nargs; ++i)
3701 : 10826 : if (!vect_maybe_update_slp_op_vectype (slp_op[i],
3702 : 10826 : vectypes[i]
3703 : : ? vectypes[i] : vectype_in))
3704 : : {
3705 : 0 : if (dump_enabled_p ())
3706 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3707 : : "incompatible vector types for invariants\n");
3708 : 0 : return false;
3709 : : }
3710 : 5410 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3711 : 5410 : DUMP_VECT_SCOPE ("vectorizable_call");
3712 : 5410 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
3713 : :
3714 : 5410 : if (loop_vinfo
3715 : 4476 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
3716 : 3096 : && (reduc_idx >= 0 || mask_opno >= 0))
3717 : : {
3718 : 1812 : if (reduc_idx >= 0
3719 : 1440 : && (cond_fn == IFN_LAST
3720 : 1440 : || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3721 : : OPTIMIZE_FOR_SPEED))
3722 : 1824 : && (cond_len_fn == IFN_LAST
3723 : 0 : || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3724 : : OPTIMIZE_FOR_SPEED)))
3725 : : {
3726 : 12 : if (dump_enabled_p ())
3727 : 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3728 : : "can't use a fully-masked loop because no"
3729 : : " conditional operation is available.\n");
3730 : 12 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3731 : : }
3732 : : else
3733 : : {
3734 : 1800 : tree scalar_mask = NULL_TREE;
3735 : 1800 : if (mask_opno >= 0)
3736 : 1800 : scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
3737 : 1800 : if (cond_len_fn != IFN_LAST
3738 : 1800 : && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3739 : : OPTIMIZE_FOR_SPEED))
3740 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
3741 : : 1);
3742 : : else
3743 : 1800 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
3744 : : scalar_mask);
3745 : : }
3746 : : }
3747 : 5410 : return true;
3748 : : }
3749 : :
3750 : : /* Transform. */
3751 : :
3752 : 4187 : if (dump_enabled_p ())
3753 : 416 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3754 : :
3755 : : /* Handle def. */
3756 : 4187 : scalar_dest = gimple_call_lhs (stmt);
3757 : 4187 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3758 : :
3759 : 4187 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3760 : 3267 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
3761 : 4187 : unsigned int vect_nargs = nargs;
3762 : 4187 : if (len_loop_p)
3763 : : {
3764 : 0 : if (len_opno >= 0)
3765 : : {
3766 : 0 : ifn = cond_len_fn;
3767 : : /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS. */
3768 : 0 : vect_nargs += 2;
3769 : : }
3770 : 0 : else if (reduc_idx >= 0)
3771 : 0 : gcc_unreachable ();
3772 : : }
3773 : 4187 : else if (masked_loop_p && mask_opno == -1 && reduc_idx >= 0)
3774 : : {
3775 : 0 : ifn = cond_fn;
3776 : 0 : vect_nargs += 2;
3777 : : }
3778 : 4187 : if (clz_ctz_arg1)
3779 : 59 : ++vect_nargs;
3780 : :
3781 : 4187 : if (modifier == NONE || ifn != IFN_LAST)
3782 : : {
3783 : 4155 : tree prev_res = NULL_TREE;
3784 : 4155 : vargs.safe_grow (vect_nargs, true);
3785 : 4155 : auto_vec<vec<tree> > vec_defs (nargs);
3786 : :
3787 : : /* Build argument list for the vectorized call. */
3788 : 4155 : if (cfn == CFN_GOMP_SIMD_LANE)
3789 : : {
3790 : 3308 : for (i = 0; i < nvectors; ++i)
3791 : : {
3792 : : /* ??? For multi-lane SLP we'd need to build
3793 : : { 0, 0, .., 1, 1, ... }. */
3794 : 1708 : tree cst = build_index_vector (vectype_out,
3795 : : i * nunits_out, 1);
3796 : 1708 : tree new_var
3797 : 1708 : = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
3798 : 1708 : gimple *init_stmt = gimple_build_assign (new_var, cst);
3799 : 1708 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3800 : 1708 : new_temp = make_ssa_name (vec_dest);
3801 : 1708 : gimple *new_stmt = gimple_build_assign (new_temp, new_var);
3802 : 1708 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3803 : 1708 : slp_node->push_vec_def (new_stmt);
3804 : : }
3805 : : }
3806 : : else
3807 : : {
3808 : 2555 : vec<tree> vec_oprnds0;
3809 : 2555 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3810 : 2555 : vec_oprnds0 = vec_defs[0];
3811 : :
3812 : : /* Arguments are ready. Create the new vector stmt. */
3813 : 5268 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
3814 : : {
3815 : 2713 : int varg = 0;
3816 : : /* Add the mask if necessary. */
3817 : 2713 : if (masked_loop_p && mask_opno == -1 && reduc_idx >= 0)
3818 : : {
3819 : 0 : gcc_assert (internal_fn_mask_index (ifn) == varg);
3820 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3821 : 0 : vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi, masks,
3822 : : vec_num, vectype_out, i);
3823 : : }
3824 : : size_t k;
3825 : 9700 : for (k = 0; k < nargs; k++)
3826 : : {
3827 : 6987 : vec<tree> vec_oprndsk = vec_defs[k];
3828 : 6987 : vargs[varg++] = vec_oprndsk[i];
3829 : : }
3830 : : /* Add the else value if necessary. */
3831 : 2713 : if (masked_loop_p && mask_opno == -1 && reduc_idx >= 0)
3832 : : {
3833 : 0 : gcc_assert (internal_fn_else_index (ifn) == varg);
3834 : 0 : vargs[varg++] = vargs[reduc_idx + 1];
3835 : : }
3836 : 2713 : if (clz_ctz_arg1)
3837 : 59 : vargs[varg++] = clz_ctz_arg1;
3838 : :
3839 : 2713 : gimple *new_stmt;
3840 : 2713 : if (modifier == NARROW)
3841 : : {
3842 : : /* We don't define any narrowing conditional functions
3843 : : at present. */
3844 : 0 : gcc_assert (mask_opno < 0);
3845 : 0 : tree half_res = make_ssa_name (vectype_in);
3846 : 0 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3847 : 0 : gimple_call_set_lhs (call, half_res);
3848 : 0 : gimple_call_set_nothrow (call, true);
3849 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3850 : 0 : if ((i & 1) == 0)
3851 : : {
3852 : 0 : prev_res = half_res;
3853 : 0 : continue;
3854 : : }
3855 : 0 : new_temp = make_ssa_name (vec_dest);
3856 : 0 : new_stmt = vect_gimple_build (new_temp, convert_code,
3857 : : prev_res, half_res);
3858 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3859 : : }
3860 : : else
3861 : : {
3862 : 2713 : if (len_opno >= 0 && len_loop_p)
3863 : : {
3864 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3865 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
3866 : : vec_num, vectype_out, i, 1);
3867 : 0 : signed char biasval
3868 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3869 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
3870 : 0 : vargs[len_opno] = len;
3871 : 0 : vargs[len_opno + 1] = bias;
3872 : : }
3873 : 2713 : else if (mask_opno >= 0 && masked_loop_p)
3874 : : {
3875 : 36 : unsigned int vec_num = vec_oprnds0.length ();
3876 : 36 : tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
3877 : : vec_num, vectype_out, i);
3878 : 36 : vargs[mask_opno]
3879 : 72 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
3880 : 36 : vargs[mask_opno], gsi);
3881 : : }
3882 : :
3883 : 2713 : gcall *call;
3884 : 2713 : if (ifn != IFN_LAST)
3885 : 2632 : call = gimple_build_call_internal_vec (ifn, vargs);
3886 : : else
3887 : 81 : call = gimple_build_call_vec (fndecl, vargs);
3888 : 2713 : new_temp = make_ssa_name (vec_dest, call);
3889 : 2713 : gimple_call_set_lhs (call, new_temp);
3890 : 2713 : gimple_call_set_nothrow (call, true);
3891 : 2713 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3892 : 2713 : new_stmt = call;
3893 : : }
3894 : 2713 : slp_node->push_vec_def (new_stmt);
3895 : : }
3896 : : }
3897 : :
3898 : 10779 : for (i = 0; i < nargs; i++)
3899 : : {
3900 : 6624 : vec<tree> vec_oprndsi = vec_defs[i];
3901 : 6624 : vec_oprndsi.release ();
3902 : : }
3903 : 4155 : }
3904 : 32 : else if (modifier == NARROW)
3905 : : {
3906 : 32 : auto_vec<vec<tree> > vec_defs (nargs);
3907 : : /* We don't define any narrowing conditional functions at present. */
3908 : 32 : gcc_assert (mask_opno < 0);
3909 : :
3910 : : /* Build argument list for the vectorized call. */
3911 : 32 : vargs.create (nargs * 2);
3912 : :
3913 : 32 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3914 : 32 : vec<tree> vec_oprnds0 = vec_defs[0];
3915 : :
3916 : : /* Arguments are ready. Create the new vector stmt. */
3917 : 64 : for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
3918 : : {
3919 : 32 : size_t k;
3920 : 32 : vargs.truncate (0);
3921 : 64 : for (k = 0; k < nargs; k++)
3922 : : {
3923 : 32 : vec<tree> vec_oprndsk = vec_defs[k];
3924 : 32 : vargs.quick_push (vec_oprndsk[i]);
3925 : 32 : vargs.quick_push (vec_oprndsk[i + 1]);
3926 : : }
3927 : 32 : gcall *call;
3928 : 32 : if (ifn != IFN_LAST)
3929 : : call = gimple_build_call_internal_vec (ifn, vargs);
3930 : : else
3931 : 32 : call = gimple_build_call_vec (fndecl, vargs);
3932 : 32 : new_temp = make_ssa_name (vec_dest, call);
3933 : 32 : gimple_call_set_lhs (call, new_temp);
3934 : 32 : gimple_call_set_nothrow (call, true);
3935 : 32 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3936 : 32 : slp_node->push_vec_def (call);
3937 : : }
3938 : :
3939 : 64 : for (i = 0; i < nargs; i++)
3940 : : {
3941 : 32 : vec<tree> vec_oprndsi = vec_defs[i];
3942 : 32 : vec_oprndsi.release ();
3943 : : }
3944 : 32 : }
3945 : : else
3946 : : /* No current target implements this case. */
3947 : : return false;
3948 : :
3949 : 4187 : vargs.release ();
3950 : :
3951 : 4187 : return true;
3952 : 2317718 : }
3953 : :
3954 : :
3955 : : struct simd_call_arg_info
3956 : : {
3957 : : tree vectype;
3958 : : tree op;
3959 : : HOST_WIDE_INT linear_step;
3960 : : enum vect_def_type dt;
3961 : : unsigned int align;
3962 : : bool simd_lane_linear;
3963 : : };
3964 : :
3965 : : /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
3966 : : is linear within simd lane (but not within whole loop), note it in
3967 : : *ARGINFO. */
3968 : :
3969 : : static void
3970 : 15 : vect_simd_lane_linear (tree op, class loop *loop,
3971 : : struct simd_call_arg_info *arginfo)
3972 : : {
3973 : 15 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
3974 : :
3975 : 15 : if (!is_gimple_assign (def_stmt)
3976 : 15 : || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
3977 : 27 : || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
3978 : 3 : return;
3979 : :
3980 : 12 : tree base = gimple_assign_rhs1 (def_stmt);
3981 : 12 : HOST_WIDE_INT linear_step = 0;
3982 : 12 : tree v = gimple_assign_rhs2 (def_stmt);
3983 : 48 : while (TREE_CODE (v) == SSA_NAME)
3984 : : {
3985 : 36 : tree t;
3986 : 36 : def_stmt = SSA_NAME_DEF_STMT (v);
3987 : 36 : if (is_gimple_assign (def_stmt))
3988 : 24 : switch (gimple_assign_rhs_code (def_stmt))
3989 : : {
3990 : 0 : case PLUS_EXPR:
3991 : 0 : t = gimple_assign_rhs2 (def_stmt);
3992 : 0 : if (linear_step || TREE_CODE (t) != INTEGER_CST)
3993 : : return;
3994 : 0 : base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
3995 : 0 : v = gimple_assign_rhs1 (def_stmt);
3996 : 0 : continue;
3997 : 12 : case MULT_EXPR:
3998 : 12 : t = gimple_assign_rhs2 (def_stmt);
3999 : 12 : if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
4000 : 0 : return;
4001 : 12 : linear_step = tree_to_shwi (t);
4002 : 12 : v = gimple_assign_rhs1 (def_stmt);
4003 : 12 : continue;
4004 : 12 : CASE_CONVERT:
4005 : 12 : t = gimple_assign_rhs1 (def_stmt);
4006 : 12 : if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
4007 : 12 : || (TYPE_PRECISION (TREE_TYPE (v))
4008 : 12 : < TYPE_PRECISION (TREE_TYPE (t))))
4009 : : return;
4010 : 12 : if (!linear_step)
4011 : 0 : linear_step = 1;
4012 : 12 : v = t;
4013 : 12 : continue;
4014 : : default:
4015 : : return;
4016 : : }
4017 : 12 : else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
4018 : 12 : && loop->simduid
4019 : 12 : && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
4020 : 24 : && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
4021 : : == loop->simduid))
4022 : : {
4023 : 12 : if (!linear_step)
4024 : 0 : linear_step = 1;
4025 : 12 : arginfo->linear_step = linear_step;
4026 : 12 : arginfo->op = base;
4027 : 12 : arginfo->simd_lane_linear = true;
4028 : 12 : return;
4029 : : }
4030 : : }
4031 : : }
4032 : :
4033 : : /* Function vectorizable_simd_clone_call.
4034 : :
4035 : : Check if STMT_INFO performs a function call that can be vectorized
4036 : : by calling a simd clone of the function.
4037 : : If COST_VEC is passed, calculate costs but don't change anything,
4038 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
4039 : : it, and insert it at GSI.
4040 : : Return true if STMT_INFO is vectorizable in this way. */
4041 : :
4042 : : static bool
4043 : 2308317 : vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
4044 : : gimple_stmt_iterator *gsi,
4045 : : slp_tree slp_node,
4046 : : stmt_vector_for_cost *cost_vec)
4047 : : {
4048 : 2308317 : tree vec_dest;
4049 : 2308317 : tree scalar_dest;
4050 : 2308317 : tree op;
4051 : 2308317 : tree vec_oprnd0 = NULL_TREE;
4052 : 2308317 : tree vectype;
4053 : 2308317 : poly_uint64 nunits;
4054 : 2308317 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
4055 : 2308317 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
4056 : 2308317 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
4057 : 2308317 : tree fndecl, new_temp;
4058 : 2308317 : int j;
4059 : 2308317 : auto_vec<simd_call_arg_info> arginfo;
4060 : 2308317 : vec<tree> vargs = vNULL;
4061 : 2308317 : size_t i, nargs;
4062 : 2308317 : tree rtype, ratype;
4063 : 2308317 : vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
4064 : 2308317 : int masked_call_offset = 0;
4065 : :
4066 : : /* Is STMT a vectorizable call? */
4067 : 2308317 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
4068 : 10794 : if (!stmt)
4069 : : return false;
4070 : :
4071 : 10794 : fndecl = gimple_call_fndecl (stmt);
4072 : 10794 : if (fndecl == NULL_TREE
4073 : 10794 : && gimple_call_internal_p (stmt, IFN_MASK_CALL))
4074 : : {
4075 : 216 : fndecl = gimple_call_arg (stmt, 0);
4076 : 216 : gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
4077 : 216 : fndecl = TREE_OPERAND (fndecl, 0);
4078 : 216 : gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
4079 : : masked_call_offset = 1;
4080 : : }
4081 : 10578 : if (fndecl == NULL_TREE)
4082 : : return false;
4083 : :
4084 : 4601 : struct cgraph_node *node = cgraph_node::get (fndecl);
4085 : 4601 : if (node == NULL || node->simd_clones == NULL)
4086 : : return false;
4087 : :
4088 : 1433 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4089 : : return false;
4090 : :
4091 : 1433 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4092 : 0 : && cost_vec)
4093 : : return false;
4094 : :
4095 : 1433 : if (gimple_call_lhs (stmt)
4096 : 1433 : && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
4097 : : return false;
4098 : :
4099 : 1433 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
4100 : :
4101 : 1433 : vectype = SLP_TREE_VECTYPE (slp_node);
4102 : :
4103 : 2308381 : if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
4104 : : return false;
4105 : :
4106 : : /* Process function arguments. */
4107 : 1433 : nargs = gimple_call_num_args (stmt) - masked_call_offset;
4108 : :
4109 : : /* Bail out if the function has zero arguments. */
4110 : 1433 : if (nargs == 0)
4111 : : return false;
4112 : :
4113 : 1369 : vect_simd_clone_data _data;
4114 : 1369 : vect_simd_clone_data &data = slp_node->get_data (_data);
4115 : 1369 : vec<tree>& simd_clone_info = data.simd_clone_info;
4116 : 1369 : arginfo.reserve (nargs, true);
4117 : 1369 : auto_vec<slp_tree> slp_op;
4118 : 1369 : slp_op.safe_grow_cleared (nargs);
4119 : :
4120 : 3947 : for (i = 0; i < nargs; i++)
4121 : : {
4122 : 2578 : simd_call_arg_info thisarginfo;
4123 : 2578 : affine_iv iv;
4124 : :
4125 : 2578 : thisarginfo.linear_step = 0;
4126 : 2578 : thisarginfo.align = 0;
4127 : 2578 : thisarginfo.op = NULL_TREE;
4128 : 2578 : thisarginfo.simd_lane_linear = false;
4129 : :
4130 : 5156 : int op_no = vect_slp_child_index_for_operand (stmt,
4131 : 2578 : i + masked_call_offset,
4132 : : false);
4133 : 5156 : if (!vect_is_simple_use (vinfo, slp_node,
4134 : 2578 : op_no, &op, &slp_op[i],
4135 : : &thisarginfo.dt, &thisarginfo.vectype)
4136 : 2578 : || thisarginfo.dt == vect_uninitialized_def)
4137 : : {
4138 : 0 : if (dump_enabled_p ())
4139 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4140 : : "use not simple.\n");
4141 : 0 : return false;
4142 : : }
4143 : :
4144 : 2578 : if (thisarginfo.dt == vect_constant_def
4145 : 2578 : || thisarginfo.dt == vect_external_def)
4146 : : {
4147 : : /* With SLP we determine the vector type of constants/externals
4148 : : at analysis time, handling conflicts via
4149 : : vect_maybe_update_slp_op_vectype. At transform time
4150 : : we have a vector type recorded for SLP. */
4151 : 696 : gcc_assert (cost_vec
4152 : : || thisarginfo.vectype != NULL_TREE);
4153 : : if (cost_vec)
4154 : 566 : thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
4155 : 566 : TREE_TYPE (op),
4156 : : slp_node);
4157 : : }
4158 : : else
4159 : 1882 : gcc_assert (thisarginfo.vectype != NULL_TREE);
4160 : :
4161 : : /* For linear arguments, the analyze phase should have saved
4162 : : the base and step in {STMT_VINFO,SLP_TREE}_SIMD_CLONE_INFO. */
4163 : 2448 : if (!cost_vec
4164 : 1560 : && i * 3 + 4 <= simd_clone_info.length ()
4165 : 2658 : && simd_clone_info[i * 3 + 2])
4166 : : {
4167 : 118 : thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
4168 : 118 : thisarginfo.op = simd_clone_info[i * 3 + 1];
4169 : 118 : thisarginfo.simd_lane_linear
4170 : 118 : = (simd_clone_info[i * 3 + 3] == boolean_true_node);
4171 : : /* If loop has been peeled for alignment, we need to adjust it. */
4172 : 118 : tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
4173 : 118 : tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
4174 : 118 : if (n1 != n2 && !thisarginfo.simd_lane_linear)
4175 : : {
4176 : 0 : tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
4177 : 0 : tree step = simd_clone_info[i * 3 + 2];
4178 : 0 : tree opt = TREE_TYPE (thisarginfo.op);
4179 : 0 : bias = fold_convert (TREE_TYPE (step), bias);
4180 : 0 : bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
4181 : 0 : thisarginfo.op
4182 : 0 : = fold_build2 (POINTER_TYPE_P (opt)
4183 : : ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
4184 : : thisarginfo.op, bias);
4185 : : }
4186 : : }
4187 : 2460 : else if (cost_vec
4188 : 1798 : && thisarginfo.dt != vect_constant_def
4189 : 1676 : && thisarginfo.dt != vect_external_def
4190 : 1232 : && loop_vinfo
4191 : 1229 : && TREE_CODE (op) == SSA_NAME
4192 : 2458 : && simple_iv (loop, loop_containing_stmt (stmt), op,
4193 : : &iv, false)
4194 : 2664 : && tree_fits_shwi_p (iv.step))
4195 : : {
4196 : 204 : thisarginfo.linear_step = tree_to_shwi (iv.step);
4197 : 204 : thisarginfo.op = iv.base;
4198 : : }
4199 : 2256 : else if ((thisarginfo.dt == vect_constant_def
4200 : 2256 : || thisarginfo.dt == vect_external_def)
4201 : 2256 : && POINTER_TYPE_P (TREE_TYPE (op)))
4202 : 479 : thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
4203 : : /* Addresses of array elements indexed by GOMP_SIMD_LANE are
4204 : : linear too. */
4205 : 4567 : if (POINTER_TYPE_P (TREE_TYPE (op))
4206 : 589 : && !thisarginfo.linear_step
4207 : 505 : && cost_vec
4208 : 451 : && thisarginfo.dt != vect_constant_def
4209 : 451 : && thisarginfo.dt != vect_external_def
4210 : 15 : && loop_vinfo
4211 : 2593 : && TREE_CODE (op) == SSA_NAME)
4212 : 15 : vect_simd_lane_linear (op, loop, &thisarginfo);
4213 : :
4214 : 2578 : if (!vectype)
4215 : 12 : vectype = thisarginfo.vectype;
4216 : 2578 : arginfo.quick_push (thisarginfo);
4217 : : }
4218 : :
4219 : 1369 : poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
4220 : 1369 : unsigned group_size = SLP_TREE_LANES (slp_node);
4221 : 1369 : unsigned int badness = 0;
4222 : 1369 : struct cgraph_node *bestn = NULL;
4223 : 1369 : if (!cost_vec)
4224 : 348 : bestn = cgraph_node::get (simd_clone_info[0]);
4225 : : else
4226 : 5895 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
4227 : 4874 : n = n->simdclone->next_clone)
4228 : : {
4229 : 4874 : unsigned int this_badness = 0;
4230 : 4874 : unsigned int num_calls;
4231 : : /* The number of arguments in the call and the number of parameters in
4232 : : the simdclone should match. However, when the simdclone is
4233 : : 'inbranch', it could have one more paramater than nargs when using
4234 : : an inbranch simdclone to call a non-inbranch call, either in a
4235 : : non-masked loop using a all true constant mask, or inside a masked
4236 : : loop using it's mask. */
4237 : 4874 : size_t simd_nargs = n->simdclone->nargs;
4238 : 4874 : if (!masked_call_offset && n->simdclone->inbranch)
4239 : 2317 : simd_nargs--;
4240 : 4874 : if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
4241 : : &num_calls)
4242 : 1908 : || (!n->simdclone->inbranch && (masked_call_offset > 0))
4243 : 1732 : || (nargs != simd_nargs))
4244 : 3142 : continue;
4245 : 1732 : if (num_calls != 1)
4246 : 1136 : this_badness += floor_log2 (num_calls) * 4096;
4247 : 1732 : if (n->simdclone->inbranch)
4248 : 744 : this_badness += 8192;
4249 : :
4250 : : /* If SLP_TREE_VECTYPE has not been set yet pass the general vector
4251 : : mode, which for targets that use it will determine what ISA we can
4252 : : vectorize this code with. */
4253 : 1732 : machine_mode vector_mode = vinfo->vector_mode;
4254 : 1732 : if (vectype)
4255 : 1732 : vector_mode = TYPE_MODE (vectype);
4256 : 1732 : int target_badness = targetm.simd_clone.usable (n, vector_mode);
4257 : 1732 : if (target_badness < 0)
4258 : 364 : continue;
4259 : 1368 : this_badness += target_badness * 512;
4260 : 4263 : for (i = 0; i < nargs; i++)
4261 : : {
4262 : 2952 : switch (n->simdclone->args[i].arg_type)
4263 : : {
4264 : 2030 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4265 : 2030 : if (!useless_type_conversion_p
4266 : 2030 : (n->simdclone->args[i].orig_type,
4267 : 2030 : TREE_TYPE (gimple_call_arg (stmt,
4268 : : i + masked_call_offset))))
4269 : : i = -1;
4270 : 2030 : else if (arginfo[i].dt == vect_constant_def
4271 : 1925 : || arginfo[i].dt == vect_external_def
4272 : 3893 : || arginfo[i].linear_step)
4273 : 386 : this_badness += 64;
4274 : : break;
4275 : 310 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4276 : 310 : if (arginfo[i].dt != vect_constant_def
4277 : 310 : && arginfo[i].dt != vect_external_def)
4278 : : i = -1;
4279 : : break;
4280 : 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4281 : 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4282 : 324 : if (arginfo[i].dt == vect_constant_def
4283 : 324 : || arginfo[i].dt == vect_external_def
4284 : 324 : || (arginfo[i].linear_step
4285 : 324 : != n->simdclone->args[i].linear_step))
4286 : : i = -1;
4287 : : break;
4288 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4289 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4290 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4291 : : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4292 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4293 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4294 : : /* FORNOW */
4295 : : i = -1;
4296 : : break;
4297 : 288 : case SIMD_CLONE_ARG_TYPE_MASK:
4298 : : /* While we can create a traditional data vector from
4299 : : an incoming integer mode mask we have no good way to
4300 : : force generate an integer mode mask from a traditional
4301 : : boolean vector input. */
4302 : 288 : if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4303 : 288 : && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4304 : : i = -1;
4305 : 282 : else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4306 : 282 : && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4307 : 78 : this_badness += 2048;
4308 : : break;
4309 : : }
4310 : 2895 : if (i == (size_t) -1)
4311 : : break;
4312 : 2895 : if (n->simdclone->args[i].alignment > arginfo[i].align)
4313 : : {
4314 : : i = -1;
4315 : : break;
4316 : : }
4317 : 2895 : if (arginfo[i].align)
4318 : 110 : this_badness += (exact_log2 (arginfo[i].align)
4319 : 160 : - exact_log2 (n->simdclone->args[i].alignment));
4320 : : }
4321 : 1368 : if (i == (size_t) -1)
4322 : 57 : continue;
4323 : 1311 : if (masked_call_offset == 0
4324 : 1029 : && n->simdclone->inbranch
4325 : 334 : && n->simdclone->nargs > nargs)
4326 : : {
4327 : 334 : gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
4328 : : SIMD_CLONE_ARG_TYPE_MASK);
4329 : : /* Penalize using a masked SIMD clone in a non-masked loop, that is
4330 : : not in a branch, as we'd have to construct an all-true mask. */
4331 : 334 : if (!loop_vinfo || !LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4332 : 334 : this_badness += 64;
4333 : : }
4334 : 1311 : if (bestn == NULL || this_badness < badness)
4335 : : {
4336 : 4874 : bestn = n;
4337 : 4874 : badness = this_badness;
4338 : : }
4339 : : }
4340 : :
4341 : 1369 : if (bestn == NULL)
4342 : : return false;
4343 : :
4344 : 2683 : for (i = 0; i < nargs; i++)
4345 : : {
4346 : 1893 : if ((arginfo[i].dt == vect_constant_def
4347 : 1725 : || arginfo[i].dt == vect_external_def)
4348 : 1994 : && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
4349 : : {
4350 : 65 : tree arg_type = TREE_TYPE (gimple_call_arg (stmt,
4351 : : i + masked_call_offset));
4352 : 65 : arginfo[i].vectype = get_vectype_for_scalar_type (vinfo, arg_type,
4353 : : slp_node);
4354 : 65 : if (arginfo[i].vectype == NULL
4355 : 130 : || !constant_multiple_p (bestn->simdclone->simdlen,
4356 : 130 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4357 : 0 : return false;
4358 : : }
4359 : :
4360 : 1893 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR
4361 : 1893 : && VECTOR_BOOLEAN_TYPE_P (bestn->simdclone->args[i].vector_type))
4362 : : {
4363 : 4 : if (dump_enabled_p ())
4364 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4365 : : "vector mask arguments are not supported.\n");
4366 : 4 : return false;
4367 : : }
4368 : :
4369 : 1889 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4370 : : {
4371 : 174 : tree clone_arg_vectype = bestn->simdclone->args[i].vector_type;
4372 : 174 : if (bestn->simdclone->mask_mode == VOIDmode)
4373 : : {
4374 : 146 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (clone_arg_vectype),
4375 : 292 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4376 : : {
4377 : : /* FORNOW we only have partial support for vector-type masks
4378 : : that can't hold all of simdlen. */
4379 : 20 : if (dump_enabled_p ())
4380 : 20 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4381 : : vect_location,
4382 : : "in-branch vector clones are not yet"
4383 : : " supported for mismatched vector sizes.\n");
4384 : 20 : return false;
4385 : : }
4386 : 126 : if (!expand_vec_cond_expr_p (clone_arg_vectype,
4387 : 126 : arginfo[i].vectype))
4388 : : {
4389 : 6 : if (dump_enabled_p ())
4390 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4391 : : vect_location,
4392 : : "cannot compute mask argument for"
4393 : : " in-branch vector clones.\n");
4394 : 6 : return false;
4395 : : }
4396 : : }
4397 : 28 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4398 : : {
4399 : 28 : if (!SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype))
4400 : 56 : || maybe_ne (exact_div (bestn->simdclone->simdlen,
4401 : 28 : bestn->simdclone->args[i].linear_step),
4402 : 38 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4403 : : {
4404 : : /* FORNOW we only have partial support for integer-type masks
4405 : : that represent the same number of lanes as the
4406 : : vectorized mask inputs. */
4407 : 18 : if (dump_enabled_p ())
4408 : 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4409 : : vect_location,
4410 : : "in-branch vector clones are not yet "
4411 : : "supported for mismatched vector sizes.\n");
4412 : 18 : return false;
4413 : : }
4414 : : }
4415 : : else
4416 : : {
4417 : 0 : if (dump_enabled_p ())
4418 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4419 : : vect_location,
4420 : : "in-branch vector clones not supported"
4421 : : " on this target.\n");
4422 : 0 : return false;
4423 : : }
4424 : : }
4425 : : }
4426 : :
4427 : 790 : fndecl = bestn->decl;
4428 : 790 : nunits = bestn->simdclone->simdlen;
4429 : 790 : int ncopies = vector_unroll_factor (vf * group_size, nunits);
4430 : :
4431 : : /* If the function isn't const, only allow it in simd loops where user
4432 : : has asserted that at least nunits consecutive iterations can be
4433 : : performed using SIMD instructions. */
4434 : 788 : if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
4435 : 932 : && gimple_vuse (stmt))
4436 : : return false;
4437 : :
4438 : : /* ncopies is the number of SIMD clone calls we create, since simdlen
4439 : : is not necessarily matching nunits of the vector types used, track
4440 : : that in ncopies_in. */
4441 : 790 : int ncopies_in = vect_get_num_vectors (vf * group_size, vectype);
4442 : :
4443 : : /* Sanity check: make sure that at least one copy of the vectorized stmt
4444 : : needs to be generated. */
4445 : 790 : gcc_assert (ncopies >= 1);
4446 : :
4447 : 790 : if (cost_vec) /* transformation not required. */
4448 : : {
4449 : 1459 : for (unsigned i = 0; i < nargs; ++i)
4450 : 1017 : if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
4451 : : {
4452 : 0 : if (dump_enabled_p ())
4453 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4454 : : "incompatible vector types for invariants\n");
4455 : 0 : return false;
4456 : : }
4457 : : /* When the original call is pure or const but the SIMD ABI dictates
4458 : : an aggregate return we will have to use a virtual definition and
4459 : : in a loop eventually even need to add a virtual PHI. That's
4460 : : not straight-forward so allow to fix this up via renaming. */
4461 : 442 : if (gimple_call_lhs (stmt)
4462 : 436 : && !gimple_vdef (stmt)
4463 : 784 : && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
4464 : 27 : vinfo->any_known_not_updated_vssa = true;
4465 : : /* ??? For SLP code-gen we end up inserting after the last
4466 : : vector argument def rather than at the original call position
4467 : : so automagic virtual operand updating doesn't work. */
4468 : 884 : if (gimple_vuse (stmt))
4469 : 137 : vinfo->any_known_not_updated_vssa = true;
4470 : 442 : simd_clone_info.safe_push (bestn->decl);
4471 : 1470 : for (i = 0; i < bestn->simdclone->nargs; i++)
4472 : : {
4473 : 1028 : switch (bestn->simdclone->args[i].arg_type)
4474 : : {
4475 : 833 : default:
4476 : 833 : continue;
4477 : 118 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4478 : 118 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4479 : 118 : {
4480 : 118 : simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
4481 : 118 : simd_clone_info.safe_push (arginfo[i].op);
4482 : 202 : tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4483 : 202 : ? size_type_node : TREE_TYPE (arginfo[i].op);
4484 : 118 : tree ls = build_int_cst (lst, arginfo[i].linear_step);
4485 : 118 : simd_clone_info.safe_push (ls);
4486 : 118 : tree sll = arginfo[i].simd_lane_linear
4487 : 118 : ? boolean_true_node : boolean_false_node;
4488 : 118 : simd_clone_info.safe_push (sll);
4489 : : }
4490 : 118 : break;
4491 : 77 : case SIMD_CLONE_ARG_TYPE_MASK:
4492 : 77 : if (loop_vinfo
4493 : 77 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4494 : 58 : vect_record_loop_mask (loop_vinfo,
4495 : : &LOOP_VINFO_MASKS (loop_vinfo),
4496 : : ncopies_in, vectype, op);
4497 : : break;
4498 : 833 : }
4499 : : }
4500 : :
4501 : 442 : if (!bestn->simdclone->inbranch && loop_vinfo)
4502 : : {
4503 : 364 : if (dump_enabled_p ()
4504 : 364 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4505 : 230 : dump_printf_loc (MSG_NOTE, vect_location,
4506 : : "can't use a fully-masked loop because a"
4507 : : " non-masked simd clone was selected.\n");
4508 : 364 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
4509 : : }
4510 : :
4511 : 442 : SLP_TREE_TYPE (slp_node) = call_simd_clone_vec_info_type;
4512 : 442 : slp_node->data = new vect_simd_clone_data (std::move (_data));
4513 : 442 : DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
4514 : : /* vect_model_simple_cost (vinfo, 1, slp_node, cost_vec); */
4515 : 442 : return true;
4516 : : }
4517 : :
4518 : : /* Transform. */
4519 : :
4520 : 348 : if (dump_enabled_p ())
4521 : 238 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4522 : :
4523 : : /* Handle def. */
4524 : 348 : scalar_dest = gimple_call_lhs (stmt);
4525 : 348 : vec_dest = NULL_TREE;
4526 : 348 : rtype = NULL_TREE;
4527 : 348 : ratype = NULL_TREE;
4528 : 348 : if (scalar_dest)
4529 : : {
4530 : 342 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
4531 : 342 : rtype = TREE_TYPE (TREE_TYPE (fndecl));
4532 : 342 : if (TREE_CODE (rtype) == ARRAY_TYPE)
4533 : : {
4534 : 9 : ratype = rtype;
4535 : 9 : rtype = TREE_TYPE (ratype);
4536 : : }
4537 : : }
4538 : :
4539 : 696 : auto_vec<vec<tree> > vec_oprnds;
4540 : 348 : auto_vec<unsigned> vec_oprnds_i;
4541 : 348 : vec_oprnds_i.safe_grow_cleared (nargs, true);
4542 : 348 : vec_oprnds.reserve_exact (nargs);
4543 : 348 : vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
4544 : 805 : for (j = 0; j < ncopies; ++j)
4545 : : {
4546 : 457 : poly_uint64 callee_nelements;
4547 : 457 : poly_uint64 caller_nelements;
4548 : : /* Build argument list for the vectorized call. */
4549 : 457 : if (j == 0)
4550 : 348 : vargs.create (nargs);
4551 : : else
4552 : 109 : vargs.truncate (0);
4553 : :
4554 : 1549 : for (i = 0; i < nargs; i++)
4555 : : {
4556 : 1092 : unsigned int k, l, m, o;
4557 : 1092 : tree atype;
4558 : 1092 : op = gimple_call_arg (stmt, i + masked_call_offset);
4559 : 1092 : switch (bestn->simdclone->args[i].arg_type)
4560 : : {
4561 : 805 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4562 : 805 : atype = bestn->simdclone->args[i].vector_type;
4563 : 805 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4564 : 805 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4565 : 805 : o = vector_unroll_factor (nunits, callee_nelements);
4566 : 1840 : for (m = j * o; m < (j + 1) * o; m++)
4567 : : {
4568 : 1035 : if (known_lt (callee_nelements, caller_nelements))
4569 : : {
4570 : 516 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
4571 : 258 : if (!constant_multiple_p (caller_nelements,
4572 : : callee_nelements, &k))
4573 : 0 : gcc_unreachable ();
4574 : :
4575 : 258 : gcc_assert ((k & (k - 1)) == 0);
4576 : 258 : if (m == 0)
4577 : : {
4578 : 57 : vec_oprnds_i[i] = 0;
4579 : 57 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4580 : : }
4581 : : else
4582 : : {
4583 : 201 : vec_oprnd0 = arginfo[i].op;
4584 : 201 : if ((m & (k - 1)) == 0)
4585 : 72 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4586 : : }
4587 : 258 : arginfo[i].op = vec_oprnd0;
4588 : 258 : vec_oprnd0
4589 : 258 : = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
4590 : 258 : bitsize_int (prec),
4591 : 258 : bitsize_int ((m & (k - 1)) * prec));
4592 : 258 : gassign *new_stmt
4593 : 258 : = gimple_build_assign (make_ssa_name (atype),
4594 : : vec_oprnd0);
4595 : 258 : vect_finish_stmt_generation (vinfo, stmt_info,
4596 : : new_stmt, gsi);
4597 : 258 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4598 : : }
4599 : : else
4600 : : {
4601 : 777 : if (!constant_multiple_p (callee_nelements,
4602 : : caller_nelements, &k))
4603 : 0 : gcc_unreachable ();
4604 : 777 : gcc_assert ((k & (k - 1)) == 0);
4605 : 777 : vec<constructor_elt, va_gc> *ctor_elts;
4606 : 777 : if (k != 1)
4607 : 14 : vec_alloc (ctor_elts, k);
4608 : : else
4609 : 763 : ctor_elts = NULL;
4610 : 805 : for (l = 0; l < k; l++)
4611 : : {
4612 : 791 : if (m == 0 && l == 0)
4613 : : {
4614 : 439 : vec_oprnds_i[i] = 0;
4615 : 439 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4616 : : }
4617 : : else
4618 : 352 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4619 : 791 : arginfo[i].op = vec_oprnd0;
4620 : 791 : if (k == 1)
4621 : : break;
4622 : 28 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4623 : : vec_oprnd0);
4624 : : }
4625 : 777 : if (k == 1)
4626 : 763 : if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
4627 : : atype))
4628 : : {
4629 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
4630 : : vec_oprnd0);
4631 : 0 : gassign *new_stmt
4632 : 0 : = gimple_build_assign (make_ssa_name (atype),
4633 : : vec_oprnd0);
4634 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
4635 : : new_stmt, gsi);
4636 : 0 : vargs.safe_push (gimple_get_lhs (new_stmt));
4637 : : }
4638 : : else
4639 : 763 : vargs.safe_push (vec_oprnd0);
4640 : : else
4641 : : {
4642 : 14 : vec_oprnd0 = build_constructor (atype, ctor_elts);
4643 : 14 : gassign *new_stmt
4644 : 14 : = gimple_build_assign (make_ssa_name (atype),
4645 : : vec_oprnd0);
4646 : 14 : vect_finish_stmt_generation (vinfo, stmt_info,
4647 : : new_stmt, gsi);
4648 : 14 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4649 : : }
4650 : : }
4651 : : }
4652 : : break;
4653 : 64 : case SIMD_CLONE_ARG_TYPE_MASK:
4654 : 64 : if (bestn->simdclone->mask_mode == VOIDmode)
4655 : : {
4656 : 60 : atype = bestn->simdclone->args[i].vector_type;
4657 : 60 : tree elt_type = TREE_TYPE (atype);
4658 : 60 : tree one = fold_convert (elt_type, integer_one_node);
4659 : 60 : tree zero = fold_convert (elt_type, integer_zero_node);
4660 : 60 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4661 : 60 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4662 : 60 : o = vector_unroll_factor (nunits, callee_nelements);
4663 : 120 : for (m = j * o; m < (j + 1) * o; m++)
4664 : : {
4665 : 60 : if (maybe_lt (callee_nelements, caller_nelements))
4666 : : {
4667 : : /* The mask type has fewer elements than simdlen. */
4668 : :
4669 : : /* FORNOW */
4670 : 0 : gcc_unreachable ();
4671 : : }
4672 : 60 : else if (known_eq (callee_nelements, caller_nelements))
4673 : : {
4674 : : /* The SIMD clone function has the same number of
4675 : : elements as the current function. */
4676 : 60 : if (m == 0)
4677 : 60 : vec_oprnds_i[i] = 0;
4678 : 60 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4679 : 60 : if (loop_vinfo
4680 : 60 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4681 : : {
4682 : 0 : vec_loop_masks *loop_masks
4683 : : = &LOOP_VINFO_MASKS (loop_vinfo);
4684 : 0 : tree loop_mask
4685 : 0 : = vect_get_loop_mask (loop_vinfo, gsi,
4686 : : loop_masks, ncopies_in,
4687 : 0 : vectype, j);
4688 : 0 : vec_oprnd0
4689 : 0 : = prepare_vec_mask (loop_vinfo,
4690 : 0 : TREE_TYPE (loop_mask),
4691 : : loop_mask, vec_oprnd0,
4692 : : gsi);
4693 : 0 : loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
4694 : : loop_mask });
4695 : :
4696 : : }
4697 : 60 : vec_oprnd0
4698 : 60 : = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
4699 : : build_vector_from_val (atype, one),
4700 : : build_vector_from_val (atype, zero));
4701 : 60 : gassign *new_stmt
4702 : 60 : = gimple_build_assign (make_ssa_name (atype),
4703 : : vec_oprnd0);
4704 : 60 : vect_finish_stmt_generation (vinfo, stmt_info,
4705 : : new_stmt, gsi);
4706 : 60 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4707 : : }
4708 : : else
4709 : : {
4710 : : /* The mask type has more elements than simdlen. */
4711 : :
4712 : : /* FORNOW */
4713 : 0 : gcc_unreachable ();
4714 : : }
4715 : : }
4716 : : }
4717 : 4 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4718 : : {
4719 : 4 : atype = bestn->simdclone->args[i].vector_type;
4720 : 4 : poly_uint64 atype_subparts
4721 : 4 : = exact_div (bestn->simdclone->simdlen,
4722 : : bestn->simdclone->args[i].linear_step);
4723 : 4 : o = bestn->simdclone->args[i].linear_step;
4724 : 8 : for (m = j * o; m < (j + 1) * o; m++)
4725 : : {
4726 : 4 : if (m == 0)
4727 : 4 : vec_oprnds_i[i] = 0;
4728 : 4 : if (maybe_lt (atype_subparts,
4729 : 4 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4730 : : {
4731 : : /* The mask argument has fewer elements than the
4732 : : input vector. */
4733 : : /* FORNOW */
4734 : 0 : gcc_unreachable ();
4735 : : }
4736 : 4 : else if (known_eq (atype_subparts,
4737 : : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4738 : : {
4739 : 4 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4740 : 4 : if (loop_vinfo
4741 : 4 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4742 : : {
4743 : 0 : vec_loop_masks *loop_masks
4744 : : = &LOOP_VINFO_MASKS (loop_vinfo);
4745 : 0 : tree loop_mask
4746 : 0 : = vect_get_loop_mask (loop_vinfo, gsi,
4747 : : loop_masks, ncopies_in,
4748 : : vectype, j);
4749 : 0 : vec_oprnd0
4750 : 0 : = prepare_vec_mask (loop_vinfo,
4751 : 0 : TREE_TYPE (loop_mask),
4752 : : loop_mask, vec_oprnd0,
4753 : : gsi);
4754 : : }
4755 : : /* The vector mask argument matches the input
4756 : : in the number of lanes, but not necessarily
4757 : : in the mode. */
4758 : 4 : tree st = lang_hooks.types.type_for_mode
4759 : 4 : (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
4760 : 4 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
4761 : : vec_oprnd0);
4762 : 4 : gassign *new_stmt
4763 : 4 : = gimple_build_assign (make_ssa_name (st),
4764 : : vec_oprnd0);
4765 : 4 : vect_finish_stmt_generation (vinfo, stmt_info,
4766 : : new_stmt, gsi);
4767 : 4 : if (!types_compatible_p (atype, st))
4768 : : {
4769 : 4 : new_stmt
4770 : 4 : = gimple_build_assign (make_ssa_name (atype),
4771 : : NOP_EXPR,
4772 : : gimple_assign_lhs
4773 : : (new_stmt));
4774 : 4 : vect_finish_stmt_generation (vinfo, stmt_info,
4775 : : new_stmt, gsi);
4776 : : }
4777 : 4 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4778 : : }
4779 : : else
4780 : : {
4781 : : /* The mask argument has more elements than the
4782 : : input vector. */
4783 : : /* FORNOW */
4784 : 0 : gcc_unreachable ();
4785 : : }
4786 : : }
4787 : : }
4788 : : else
4789 : 0 : gcc_unreachable ();
4790 : : break;
4791 : 102 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4792 : 102 : vargs.safe_push (op);
4793 : 102 : break;
4794 : 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4795 : 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4796 : 121 : if (j == 0)
4797 : : {
4798 : 118 : gimple_seq stmts;
4799 : 118 : arginfo[i].op
4800 : 118 : = force_gimple_operand (unshare_expr (arginfo[i].op),
4801 : : &stmts, true, NULL_TREE);
4802 : 118 : if (stmts != NULL)
4803 : : {
4804 : 0 : basic_block new_bb;
4805 : 0 : edge pe = loop_preheader_edge (loop);
4806 : 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4807 : 0 : gcc_assert (!new_bb);
4808 : : }
4809 : 118 : if (arginfo[i].simd_lane_linear)
4810 : : {
4811 : 6 : vargs.safe_push (arginfo[i].op);
4812 : 6 : break;
4813 : : }
4814 : 112 : tree phi_res = copy_ssa_name (op);
4815 : 112 : gphi *new_phi = create_phi_node (phi_res, loop->header);
4816 : 112 : add_phi_arg (new_phi, arginfo[i].op,
4817 : : loop_preheader_edge (loop), UNKNOWN_LOCATION);
4818 : 112 : enum tree_code code
4819 : 196 : = POINTER_TYPE_P (TREE_TYPE (op))
4820 : 112 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4821 : 196 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4822 : 196 : ? sizetype : TREE_TYPE (op);
4823 : 112 : poly_widest_int cst
4824 : 112 : = wi::mul (bestn->simdclone->args[i].linear_step,
4825 : 112 : ncopies * nunits);
4826 : 112 : tree tcst = wide_int_to_tree (type, cst);
4827 : 112 : tree phi_arg = copy_ssa_name (op);
4828 : 112 : gassign *new_stmt
4829 : 112 : = gimple_build_assign (phi_arg, code, phi_res, tcst);
4830 : 112 : gimple_stmt_iterator si = gsi_after_labels (loop->header);
4831 : 112 : gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
4832 : 112 : add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
4833 : : UNKNOWN_LOCATION);
4834 : 112 : arginfo[i].op = phi_res;
4835 : 112 : vargs.safe_push (phi_res);
4836 : 112 : }
4837 : : else
4838 : : {
4839 : 3 : enum tree_code code
4840 : 6 : = POINTER_TYPE_P (TREE_TYPE (op))
4841 : 3 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4842 : 6 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4843 : 6 : ? sizetype : TREE_TYPE (op);
4844 : 3 : poly_widest_int cst
4845 : 3 : = wi::mul (bestn->simdclone->args[i].linear_step,
4846 : 3 : j * nunits);
4847 : 3 : tree tcst = wide_int_to_tree (type, cst);
4848 : 3 : new_temp = make_ssa_name (TREE_TYPE (op));
4849 : 3 : gassign *new_stmt
4850 : 6 : = gimple_build_assign (new_temp, code,
4851 : 3 : arginfo[i].op, tcst);
4852 : 3 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4853 : 3 : vargs.safe_push (new_temp);
4854 : 3 : }
4855 : : break;
4856 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4857 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4858 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4859 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4860 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4861 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4862 : 0 : default:
4863 : 0 : gcc_unreachable ();
4864 : : }
4865 : : }
4866 : :
4867 : 457 : if (masked_call_offset == 0
4868 : 393 : && bestn->simdclone->inbranch
4869 : 7 : && bestn->simdclone->nargs > nargs)
4870 : : {
4871 : 7 : unsigned long m, o;
4872 : 7 : size_t mask_i = bestn->simdclone->nargs - 1;
4873 : 7 : tree mask;
4874 : 7 : gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
4875 : : SIMD_CLONE_ARG_TYPE_MASK);
4876 : :
4877 : 7 : tree masktype = bestn->simdclone->args[mask_i].vector_type;
4878 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4879 : : /* Guess the number of lanes represented by masktype. */
4880 : 1 : callee_nelements = exact_div (bestn->simdclone->simdlen,
4881 : 1 : bestn->simdclone->nargs - nargs);
4882 : : else
4883 : 6 : callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
4884 : 7 : o = vector_unroll_factor (nunits, callee_nelements);
4885 : 14 : for (m = j * o; m < (j + 1) * o; m++)
4886 : : {
4887 : 7 : if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4888 : : {
4889 : 0 : vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
4890 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
4891 : : ncopies, masktype, j);
4892 : : }
4893 : : else
4894 : 7 : mask = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
4895 : :
4896 : 7 : gassign *new_stmt;
4897 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4898 : : {
4899 : : /* This means we are dealing with integer mask modes.
4900 : : First convert to an integer type with the same size as
4901 : : the current vector type. */
4902 : 1 : unsigned HOST_WIDE_INT intermediate_size
4903 : 1 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
4904 : 1 : tree mid_int_type =
4905 : 1 : build_nonstandard_integer_type (intermediate_size, 1);
4906 : 1 : mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
4907 : 1 : new_stmt
4908 : 1 : = gimple_build_assign (make_ssa_name (mid_int_type),
4909 : : mask);
4910 : 1 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4911 : : /* Then zero-extend to the mask mode. */
4912 : 1 : mask = fold_build1 (NOP_EXPR, masktype,
4913 : : gimple_get_lhs (new_stmt));
4914 : : }
4915 : 6 : else if (bestn->simdclone->mask_mode == VOIDmode)
4916 : : {
4917 : 6 : tree one = fold_convert (TREE_TYPE (masktype),
4918 : : integer_one_node);
4919 : 6 : tree zero = fold_convert (TREE_TYPE (masktype),
4920 : : integer_zero_node);
4921 : 6 : mask = build3 (VEC_COND_EXPR, masktype, mask,
4922 : : build_vector_from_val (masktype, one),
4923 : : build_vector_from_val (masktype, zero));
4924 : : }
4925 : : else
4926 : 0 : gcc_unreachable ();
4927 : :
4928 : 7 : new_stmt = gimple_build_assign (make_ssa_name (masktype), mask);
4929 : 7 : vect_finish_stmt_generation (vinfo, stmt_info,
4930 : : new_stmt, gsi);
4931 : 7 : mask = gimple_assign_lhs (new_stmt);
4932 : 7 : vargs.safe_push (mask);
4933 : : }
4934 : : }
4935 : :
4936 : 457 : gcall *new_call = gimple_build_call_vec (fndecl, vargs);
4937 : 457 : if (vec_dest)
4938 : : {
4939 : 451 : gcc_assert (ratype
4940 : : || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
4941 : 451 : if (ratype)
4942 : 15 : new_temp = create_tmp_var (ratype);
4943 : 436 : else if (useless_type_conversion_p (vectype, rtype))
4944 : 414 : new_temp = make_ssa_name (vec_dest, new_call);
4945 : : else
4946 : 22 : new_temp = make_ssa_name (rtype, new_call);
4947 : 451 : gimple_call_set_lhs (new_call, new_temp);
4948 : : }
4949 : 457 : vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
4950 : 457 : gimple *new_stmt = new_call;
4951 : :
4952 : 457 : if (vec_dest)
4953 : : {
4954 : 451 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
4955 : : {
4956 : 21 : unsigned int k, l;
4957 : 42 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
4958 : 42 : poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
4959 : 21 : k = vector_unroll_factor (nunits,
4960 : : TYPE_VECTOR_SUBPARTS (vectype));
4961 : 21 : gcc_assert ((k & (k - 1)) == 0);
4962 : 75 : for (l = 0; l < k; l++)
4963 : : {
4964 : 54 : tree t;
4965 : 54 : if (ratype)
4966 : : {
4967 : 42 : t = build_fold_addr_expr (new_temp);
4968 : 42 : t = build2 (MEM_REF, vectype, t,
4969 : 42 : build_int_cst (TREE_TYPE (t), l * bytes));
4970 : : }
4971 : : else
4972 : 12 : t = build3 (BIT_FIELD_REF, vectype, new_temp,
4973 : 12 : bitsize_int (prec), bitsize_int (l * prec));
4974 : 54 : new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
4975 : 54 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4976 : :
4977 : 54 : SLP_TREE_VEC_DEFS (slp_node)
4978 : 54 : .quick_push (gimple_assign_lhs (new_stmt));
4979 : : }
4980 : :
4981 : 21 : if (ratype)
4982 : 15 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
4983 : 21 : continue;
4984 : 21 : }
4985 : 430 : else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
4986 : : {
4987 : 16 : unsigned int k;
4988 : 16 : if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
4989 : 16 : TYPE_VECTOR_SUBPARTS (rtype), &k))
4990 : 0 : gcc_unreachable ();
4991 : 16 : gcc_assert ((k & (k - 1)) == 0);
4992 : 16 : if ((j & (k - 1)) == 0)
4993 : 8 : vec_alloc (ret_ctor_elts, k);
4994 : 16 : if (ratype)
4995 : : {
4996 : 0 : unsigned int m, o;
4997 : 0 : o = vector_unroll_factor (nunits,
4998 : : TYPE_VECTOR_SUBPARTS (rtype));
4999 : 0 : for (m = 0; m < o; m++)
5000 : : {
5001 : 0 : tree tem = build4 (ARRAY_REF, rtype, new_temp,
5002 : 0 : size_int (m), NULL_TREE, NULL_TREE);
5003 : 0 : new_stmt = gimple_build_assign (make_ssa_name (rtype),
5004 : : tem);
5005 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
5006 : : new_stmt, gsi);
5007 : 0 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
5008 : : gimple_assign_lhs (new_stmt));
5009 : : }
5010 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5011 : : }
5012 : : else
5013 : 16 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
5014 : 16 : if ((j & (k - 1)) != k - 1)
5015 : 8 : continue;
5016 : 8 : vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
5017 : 8 : new_stmt
5018 : 8 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5019 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5020 : :
5021 : 8 : SLP_TREE_VEC_DEFS (slp_node)
5022 : 8 : .quick_push (gimple_assign_lhs (new_stmt));
5023 : 8 : continue;
5024 : 8 : }
5025 : 414 : else if (ratype)
5026 : : {
5027 : 0 : tree t = build_fold_addr_expr (new_temp);
5028 : 0 : t = build2 (MEM_REF, vectype, t,
5029 : 0 : build_int_cst (TREE_TYPE (t), 0));
5030 : 0 : new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
5031 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5032 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5033 : : }
5034 : 414 : else if (!useless_type_conversion_p (vectype, rtype))
5035 : : {
5036 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
5037 : 0 : new_stmt
5038 : 0 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5039 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5040 : : }
5041 : : }
5042 : :
5043 : 420 : if (gimple_get_lhs (new_stmt))
5044 : 414 : SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
5045 : : }
5046 : :
5047 : 1128 : for (i = 0; i < nargs; ++i)
5048 : : {
5049 : 780 : vec<tree> oprndsi = vec_oprnds[i];
5050 : 780 : oprndsi.release ();
5051 : : }
5052 : 348 : vargs.release ();
5053 : :
5054 : : /* Mark the clone as no longer being a candidate for GC. */
5055 : 348 : bestn->gc_candidate = false;
5056 : :
5057 : 348 : return true;
5058 : 1369 : }
5059 : :
5060 : :
5061 : : /* Function vect_gen_widened_results_half
5062 : :
5063 : : Create a vector stmt whose code, type, number of arguments, and result
5064 : : variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
5065 : : VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at GSI.
5066 : : In the case that CODE is a CALL_EXPR, this means that a call to DECL
5067 : : needs to be created (DECL is a function-decl of a target-builtin).
5068 : : STMT_INFO is the original scalar stmt that we are vectorizing. */
5069 : :
5070 : : static gimple *
5071 : 31074 : vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
5072 : : tree vec_oprnd0, tree vec_oprnd1, int op_type,
5073 : : tree vec_dest, gimple_stmt_iterator *gsi,
5074 : : stmt_vec_info stmt_info)
5075 : : {
5076 : 31074 : gimple *new_stmt;
5077 : 31074 : tree new_temp;
5078 : :
5079 : : /* Generate half of the widened result: */
5080 : 31074 : if (op_type != binary_op)
5081 : 30002 : vec_oprnd1 = NULL;
5082 : 31074 : new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
5083 : 31074 : new_temp = make_ssa_name (vec_dest, new_stmt);
5084 : 31074 : gimple_set_lhs (new_stmt, new_temp);
5085 : 31074 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5086 : :
5087 : 31074 : return new_stmt;
5088 : : }
5089 : :
5090 : :
5091 : : /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
5092 : : For multi-step conversions store the resulting vectors and call the function
5093 : : recursively. When NARROW_SRC_P is true, there's still a conversion after
5094 : : narrowing, don't store the vectors in the SLP_NODE or in vector info of
5095 : : the scalar statement(or in STMT_VINFO_RELATED_STMT chain). */
5096 : :
5097 : : static void
5098 : 11843 : vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
5099 : : int multi_step_cvt,
5100 : : stmt_vec_info stmt_info,
5101 : : vec<tree> &vec_dsts,
5102 : : gimple_stmt_iterator *gsi,
5103 : : slp_tree slp_node, code_helper code,
5104 : : bool narrow_src_p)
5105 : : {
5106 : 11843 : unsigned int i;
5107 : 11843 : tree vop0, vop1, new_tmp, vec_dest;
5108 : :
5109 : 11843 : vec_dest = vec_dsts.pop ();
5110 : :
5111 : 27949 : for (i = 0; i < vec_oprnds->length (); i += 2)
5112 : : {
5113 : : /* Create demotion operation. */
5114 : 16106 : vop0 = (*vec_oprnds)[i];
5115 : 16106 : vop1 = (*vec_oprnds)[i + 1];
5116 : 16106 : gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
5117 : 16106 : new_tmp = make_ssa_name (vec_dest, new_stmt);
5118 : 16106 : gimple_set_lhs (new_stmt, new_tmp);
5119 : 16106 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5120 : 16106 : if (multi_step_cvt || narrow_src_p)
5121 : : /* Store the resulting vector for next recursive call,
5122 : : or return the resulting vector_tmp for NARROW FLOAT_EXPR. */
5123 : 6641 : (*vec_oprnds)[i/2] = new_tmp;
5124 : : else
5125 : : {
5126 : : /* This is the last step of the conversion sequence. Store the
5127 : : vectors in SLP_NODE. */
5128 : 9465 : slp_node->push_vec_def (new_stmt);
5129 : : }
5130 : : }
5131 : :
5132 : : /* For multi-step demotion operations we first generate demotion operations
5133 : : from the source type to the intermediate types, and then combine the
5134 : : results (stored in VEC_OPRNDS) in demotion operation to the destination
5135 : : type. */
5136 : 11843 : if (multi_step_cvt)
5137 : : {
5138 : : /* At each level of recursion we have half of the operands we had at the
5139 : : previous level. */
5140 : 2944 : vec_oprnds->truncate ((i+1)/2);
5141 : 2944 : vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
5142 : : multi_step_cvt - 1,
5143 : : stmt_info, vec_dsts, gsi,
5144 : 2944 : slp_node, VEC_PACK_TRUNC_EXPR,
5145 : : narrow_src_p);
5146 : : }
5147 : :
5148 : 11843 : vec_dsts.quick_push (vec_dest);
5149 : 11843 : }
5150 : :
5151 : :
5152 : : /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
5153 : : and VEC_OPRNDS1, for a binary operation associated with scalar statement
5154 : : STMT_INFO. For multi-step conversions store the resulting vectors and
5155 : : call the function recursively. */
5156 : :
5157 : : static void
5158 : 11371 : vect_create_vectorized_promotion_stmts (vec_info *vinfo,
5159 : : vec<tree> *vec_oprnds0,
5160 : : vec<tree> *vec_oprnds1,
5161 : : stmt_vec_info stmt_info, tree vec_dest,
5162 : : gimple_stmt_iterator *gsi,
5163 : : code_helper ch1,
5164 : : code_helper ch2, int op_type)
5165 : : {
5166 : 11371 : int i;
5167 : 11371 : tree vop0, vop1, new_tmp1, new_tmp2;
5168 : 11371 : gimple *new_stmt1, *new_stmt2;
5169 : 11371 : vec<tree> vec_tmp = vNULL;
5170 : :
5171 : 11371 : vec_tmp.create (vec_oprnds0->length () * 2);
5172 : 38279 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5173 : : {
5174 : 15537 : if (op_type == binary_op)
5175 : 536 : vop1 = (*vec_oprnds1)[i];
5176 : : else
5177 : : vop1 = NULL_TREE;
5178 : :
5179 : : /* Generate the two halves of promotion operation. */
5180 : 15537 : new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
5181 : : op_type, vec_dest, gsi,
5182 : : stmt_info);
5183 : 15537 : new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
5184 : : op_type, vec_dest, gsi,
5185 : : stmt_info);
5186 : 15537 : if (is_gimple_call (new_stmt1))
5187 : : {
5188 : 0 : new_tmp1 = gimple_call_lhs (new_stmt1);
5189 : 0 : new_tmp2 = gimple_call_lhs (new_stmt2);
5190 : : }
5191 : : else
5192 : : {
5193 : 15537 : new_tmp1 = gimple_assign_lhs (new_stmt1);
5194 : 15537 : new_tmp2 = gimple_assign_lhs (new_stmt2);
5195 : : }
5196 : :
5197 : : /* Store the results for the next step. */
5198 : 15537 : vec_tmp.quick_push (new_tmp1);
5199 : 15537 : vec_tmp.quick_push (new_tmp2);
5200 : : }
5201 : :
5202 : 11371 : vec_oprnds0->release ();
5203 : 11371 : *vec_oprnds0 = vec_tmp;
5204 : 11371 : }
5205 : :
5206 : : /* Create vectorized promotion stmts for widening stmts using only half the
5207 : : potential vector size for input. */
5208 : : static void
5209 : 14 : vect_create_half_widening_stmts (vec_info *vinfo,
5210 : : vec<tree> *vec_oprnds0,
5211 : : vec<tree> *vec_oprnds1,
5212 : : stmt_vec_info stmt_info, tree vec_dest,
5213 : : gimple_stmt_iterator *gsi,
5214 : : code_helper code1,
5215 : : int op_type)
5216 : : {
5217 : 14 : int i;
5218 : 14 : tree vop0, vop1;
5219 : 14 : gimple *new_stmt1;
5220 : 14 : gimple *new_stmt2;
5221 : 14 : gimple *new_stmt3;
5222 : 14 : vec<tree> vec_tmp = vNULL;
5223 : :
5224 : 14 : vec_tmp.create (vec_oprnds0->length ());
5225 : 28 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5226 : : {
5227 : 14 : tree new_tmp1, new_tmp2, new_tmp3, out_type;
5228 : :
5229 : 14 : gcc_assert (op_type == binary_op);
5230 : 14 : vop1 = (*vec_oprnds1)[i];
5231 : :
5232 : : /* Widen the first vector input. */
5233 : 14 : out_type = TREE_TYPE (vec_dest);
5234 : 14 : new_tmp1 = make_ssa_name (out_type);
5235 : 14 : new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
5236 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
5237 : 14 : if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
5238 : : {
5239 : : /* Widen the second vector input. */
5240 : 14 : new_tmp2 = make_ssa_name (out_type);
5241 : 14 : new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
5242 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
5243 : : /* Perform the operation. With both vector inputs widened. */
5244 : 14 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
5245 : : }
5246 : : else
5247 : : {
5248 : : /* Perform the operation. With the single vector input widened. */
5249 : 0 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
5250 : : }
5251 : :
5252 : 14 : new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
5253 : 14 : gimple_assign_set_lhs (new_stmt3, new_tmp3);
5254 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
5255 : :
5256 : : /* Store the results for the next step. */
5257 : 14 : vec_tmp.quick_push (new_tmp3);
5258 : : }
5259 : :
5260 : 14 : vec_oprnds0->release ();
5261 : 14 : *vec_oprnds0 = vec_tmp;
5262 : 14 : }
5263 : :
5264 : :
5265 : : /* Check if STMT_INFO performs a conversion operation that can be vectorized.
5266 : : If COST_VEC is passed, calculate costs but don't change anything,
5267 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5268 : : it, and insert it at GSI.
5269 : : Return true if STMT_INFO is vectorizable in this way. */
5270 : :
5271 : : static bool
5272 : 2330403 : vectorizable_conversion (vec_info *vinfo,
5273 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5274 : : slp_tree slp_node,
5275 : : stmt_vector_for_cost *cost_vec)
5276 : : {
5277 : 2330403 : tree vec_dest, cvt_op = NULL_TREE;
5278 : 2330403 : tree scalar_dest;
5279 : 2330403 : tree op0, op1 = NULL_TREE;
5280 : 2330403 : tree_code tc1;
5281 : 2330403 : code_helper code, code1, code2;
5282 : 2330403 : code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
5283 : 2330403 : tree new_temp;
5284 : 2330403 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
5285 : 2330403 : poly_uint64 nunits_in;
5286 : 2330403 : poly_uint64 nunits_out;
5287 : 2330403 : tree vectype_out, vectype_in;
5288 : 2330403 : int i;
5289 : 2330403 : tree lhs_type, rhs_type;
5290 : : /* For conversions between floating point and integer, there're 2 NARROW
5291 : : cases. NARROW_SRC is for FLOAT_EXPR, means
5292 : : integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
5293 : : This is safe when the range of the source integer can fit into the lower
5294 : : precision. NARROW_DST is for FIX_TRUNC_EXPR, means
5295 : : floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
5296 : : For other conversions, when there's narrowing, NARROW_DST is used as
5297 : : default. */
5298 : 2330403 : enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
5299 : 2330403 : vec<tree> vec_oprnds0 = vNULL;
5300 : 2330403 : vec<tree> vec_oprnds1 = vNULL;
5301 : 2330403 : tree vop0;
5302 : 2330403 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5303 : 2330403 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5304 : 2330403 : int multi_step_cvt = 0;
5305 : 2330403 : vec<tree> interm_types = vNULL;
5306 : 2330403 : tree intermediate_type, cvt_type = NULL_TREE;
5307 : 2330403 : int op_type;
5308 : 2330403 : unsigned short fltsz;
5309 : :
5310 : : /* Is STMT a vectorizable conversion? */
5311 : :
5312 : 2330403 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5313 : : return false;
5314 : :
5315 : 2330403 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5316 : 179117 : && cost_vec)
5317 : : return false;
5318 : :
5319 : 2151286 : gimple* stmt = stmt_info->stmt;
5320 : 2151286 : if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
5321 : : return false;
5322 : :
5323 : 2095560 : if (gimple_get_lhs (stmt) == NULL_TREE
5324 : 2095560 : || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5325 : 782034 : return false;
5326 : :
5327 : 1313526 : if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5328 : : return false;
5329 : :
5330 : 1313526 : if (is_gimple_assign (stmt))
5331 : : {
5332 : 1305354 : code = gimple_assign_rhs_code (stmt);
5333 : 1305354 : op_type = TREE_CODE_LENGTH ((tree_code) code);
5334 : : }
5335 : 8172 : else if (gimple_call_internal_p (stmt))
5336 : : {
5337 : 4340 : code = gimple_call_internal_fn (stmt);
5338 : 4340 : op_type = gimple_call_num_args (stmt);
5339 : : }
5340 : : else
5341 : : return false;
5342 : :
5343 : 1309694 : bool widen_arith = (code == WIDEN_MULT_EXPR
5344 : 1307172 : || code == WIDEN_LSHIFT_EXPR
5345 : 2616866 : || widening_fn_p (code));
5346 : :
5347 : 1307172 : if (!widen_arith
5348 : 1307172 : && !CONVERT_EXPR_CODE_P (code)
5349 : 1168595 : && code != FIX_TRUNC_EXPR
5350 : 1166531 : && code != FLOAT_EXPR)
5351 : : return false;
5352 : :
5353 : : /* Check types of lhs and rhs. */
5354 : 158354 : scalar_dest = gimple_get_lhs (stmt);
5355 : 158354 : lhs_type = TREE_TYPE (scalar_dest);
5356 : 158354 : vectype_out = SLP_TREE_VECTYPE (slp_node);
5357 : :
5358 : : /* Check the operands of the operation. */
5359 : 158354 : slp_tree slp_op0, slp_op1 = NULL;
5360 : 158354 : if (!vect_is_simple_use (vinfo, slp_node,
5361 : : 0, &op0, &slp_op0, &dt[0], &vectype_in))
5362 : : {
5363 : 0 : if (dump_enabled_p ())
5364 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5365 : : "use not simple.\n");
5366 : 0 : return false;
5367 : : }
5368 : :
5369 : 158354 : rhs_type = TREE_TYPE (op0);
5370 : 156290 : if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
5371 : 302778 : && !((INTEGRAL_TYPE_P (lhs_type)
5372 : 132759 : && INTEGRAL_TYPE_P (rhs_type))
5373 : : || (SCALAR_FLOAT_TYPE_P (lhs_type)
5374 : 7250 : && SCALAR_FLOAT_TYPE_P (rhs_type))))
5375 : : return false;
5376 : :
5377 : 153939 : if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5378 : 138098 : && INTEGRAL_TYPE_P (lhs_type)
5379 : 269596 : && !type_has_mode_precision_p (lhs_type))
5380 : : {
5381 : 467 : if (dump_enabled_p ())
5382 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5383 : : "type conversion to bit-precision unsupported\n");
5384 : 467 : return false;
5385 : : }
5386 : :
5387 : 153472 : if (op_type == binary_op)
5388 : : {
5389 : 2522 : gcc_assert (code == WIDEN_MULT_EXPR
5390 : : || code == WIDEN_LSHIFT_EXPR
5391 : : || widening_fn_p (code));
5392 : :
5393 : 2522 : op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
5394 : 0 : gimple_call_arg (stmt, 0);
5395 : 2522 : tree vectype1_in;
5396 : 2522 : if (!vect_is_simple_use (vinfo, slp_node, 1,
5397 : : &op1, &slp_op1, &dt[1], &vectype1_in))
5398 : : {
5399 : 0 : if (dump_enabled_p ())
5400 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5401 : : "use not simple.\n");
5402 : 0 : return false;
5403 : : }
5404 : : /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
5405 : : OP1. */
5406 : 2522 : if (!vectype_in)
5407 : 99 : vectype_in = vectype1_in;
5408 : : }
5409 : :
5410 : : /* If op0 is an external or constant def, infer the vector type
5411 : : from the scalar type. */
5412 : 153472 : if (!vectype_in)
5413 : 20126 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
5414 : 153472 : if (!cost_vec)
5415 : 22876 : gcc_assert (vectype_in);
5416 : 153472 : if (!vectype_in)
5417 : : {
5418 : 256 : if (dump_enabled_p ())
5419 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5420 : : "no vectype for scalar type %T\n", rhs_type);
5421 : :
5422 : 256 : return false;
5423 : : }
5424 : :
5425 : 306432 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
5426 : 153216 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
5427 : : {
5428 : 233 : if (dump_enabled_p ())
5429 : 36 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5430 : : "can't convert between boolean and non "
5431 : : "boolean vectors %T\n", rhs_type);
5432 : :
5433 : 233 : return false;
5434 : : }
5435 : :
5436 : 152983 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
5437 : 152983 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5438 : 152983 : if (known_eq (nunits_out, nunits_in))
5439 : 72808 : if (widen_arith)
5440 : : modifier = WIDEN;
5441 : : else
5442 : 152983 : modifier = NONE;
5443 : 80175 : else if (multiple_p (nunits_out, nunits_in))
5444 : : modifier = NARROW_DST;
5445 : : else
5446 : : {
5447 : 44293 : gcc_checking_assert (multiple_p (nunits_in, nunits_out));
5448 : : modifier = WIDEN;
5449 : : }
5450 : :
5451 : 152983 : bool found_mode = false;
5452 : 152983 : scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
5453 : 152983 : scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
5454 : 152983 : opt_scalar_mode rhs_mode_iter;
5455 : 152983 : auto_vec<std::pair<tree, tree_code>, 2> converts;
5456 : :
5457 : : /* Supportable by target? */
5458 : 152983 : switch (modifier)
5459 : : {
5460 : 72574 : case NONE:
5461 : 72574 : if (code != FIX_TRUNC_EXPR
5462 : 71555 : && code != FLOAT_EXPR
5463 : 136362 : && !CONVERT_EXPR_CODE_P (code))
5464 : : return false;
5465 : 72574 : gcc_assert (code.is_tree_code ());
5466 : 72574 : if (supportable_indirect_convert_operation (code,
5467 : : vectype_out, vectype_in,
5468 : : converts, op0, slp_op0))
5469 : : {
5470 : 17487 : gcc_assert (converts.length () <= 2);
5471 : 17487 : if (converts.length () == 1)
5472 : 17413 : code1 = converts[0].second;
5473 : : else
5474 : : {
5475 : 74 : cvt_type = NULL_TREE;
5476 : 74 : multi_step_cvt = converts.length () - 1;
5477 : 74 : codecvt1 = converts[0].second;
5478 : 74 : code1 = converts[1].second;
5479 : 74 : interm_types.safe_push (converts[0].first);
5480 : : }
5481 : : break;
5482 : : }
5483 : :
5484 : : /* FALLTHRU */
5485 : 55087 : unsupported:
5486 : 61542 : if (dump_enabled_p ())
5487 : 5765 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5488 : : "conversion not supported by target.\n");
5489 : : return false;
5490 : :
5491 : 44527 : case WIDEN:
5492 : 44527 : if (known_eq (nunits_in, nunits_out))
5493 : : {
5494 : 468 : if (!(code.is_tree_code ()
5495 : 234 : && supportable_half_widening_operation ((tree_code) code,
5496 : : vectype_out, vectype_in,
5497 : : &tc1)))
5498 : 71 : goto unsupported;
5499 : 163 : code1 = tc1;
5500 : 163 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5501 : : break;
5502 : : }
5503 : 44293 : if (supportable_widening_operation (vinfo, code, stmt_info,
5504 : : vectype_out, vectype_in, &code1,
5505 : : &code2, &multi_step_cvt,
5506 : : &interm_types))
5507 : : {
5508 : : /* Binary widening operation can only be supported directly by the
5509 : : architecture. */
5510 : 42209 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5511 : : break;
5512 : : }
5513 : :
5514 : 2084 : if (code != FLOAT_EXPR
5515 : 2408 : || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
5516 : 1922 : goto unsupported;
5517 : :
5518 : 162 : fltsz = GET_MODE_SIZE (lhs_mode);
5519 : 237 : FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
5520 : : {
5521 : 237 : rhs_mode = rhs_mode_iter.require ();
5522 : 474 : if (GET_MODE_SIZE (rhs_mode) > fltsz)
5523 : : break;
5524 : :
5525 : 237 : cvt_type
5526 : 237 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5527 : 237 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5528 : 237 : if (cvt_type == NULL_TREE)
5529 : 0 : goto unsupported;
5530 : :
5531 : 474 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5532 : : {
5533 : 57 : tc1 = ERROR_MARK;
5534 : 57 : gcc_assert (code.is_tree_code ());
5535 : 57 : if (!supportable_convert_operation ((tree_code) code, vectype_out,
5536 : : cvt_type, &tc1))
5537 : 22 : goto unsupported;
5538 : 35 : codecvt1 = tc1;
5539 : : }
5540 : 180 : else if (!supportable_widening_operation (vinfo, code,
5541 : : stmt_info, vectype_out,
5542 : : cvt_type, &codecvt1,
5543 : : &codecvt2, &multi_step_cvt,
5544 : : &interm_types))
5545 : 75 : continue;
5546 : : else
5547 : 105 : gcc_assert (multi_step_cvt == 0);
5548 : :
5549 : 140 : if (supportable_widening_operation (vinfo, NOP_EXPR, stmt_info,
5550 : : cvt_type,
5551 : : vectype_in, &code1,
5552 : : &code2, &multi_step_cvt,
5553 : : &interm_types))
5554 : : {
5555 : : found_mode = true;
5556 : : break;
5557 : : }
5558 : : }
5559 : :
5560 : 140 : if (!found_mode)
5561 : 0 : goto unsupported;
5562 : :
5563 : 280 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5564 : 35 : codecvt2 = ERROR_MARK;
5565 : : else
5566 : : {
5567 : 105 : multi_step_cvt++;
5568 : 105 : interm_types.safe_push (cvt_type);
5569 : 105 : cvt_type = NULL_TREE;
5570 : : }
5571 : : break;
5572 : :
5573 : 35882 : case NARROW_DST:
5574 : 35882 : gcc_assert (op_type == unary_op);
5575 : 35882 : if (supportable_narrowing_operation (code, vectype_out, vectype_in,
5576 : : &code1, &multi_step_cvt,
5577 : : &interm_types))
5578 : : break;
5579 : :
5580 : 14154 : if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
5581 : 805 : goto unsupported;
5582 : :
5583 : 3913 : if (code == FIX_TRUNC_EXPR)
5584 : : {
5585 : 274 : cvt_type
5586 : 274 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5587 : 274 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5588 : 274 : if (cvt_type == NULL_TREE)
5589 : 0 : goto unsupported;
5590 : 274 : if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
5591 : : &tc1))
5592 : 272 : codecvt1 = tc1;
5593 : : else
5594 : 2 : goto unsupported;
5595 : 272 : if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
5596 : : &code1, &multi_step_cvt,
5597 : : &interm_types))
5598 : : break;
5599 : : }
5600 : : /* If op0 can be represented with low precision integer,
5601 : : truncate it to cvt_type and the do FLOAT_EXPR. */
5602 : 3639 : else if (code == FLOAT_EXPR)
5603 : : {
5604 : 104 : if (cost_vec)
5605 : : {
5606 : 99 : wide_int op_min_value, op_max_value;
5607 : 99 : tree def;
5608 : :
5609 : : /* ??? Merge ranges in case of more than one lane. */
5610 : 99 : if (SLP_TREE_LANES (slp_op0) != 1
5611 : 97 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
5612 : 196 : || !vect_get_range_info (def, &op_min_value, &op_max_value))
5613 : 94 : goto unsupported;
5614 : :
5615 : 5 : if ((wi::min_precision (op_max_value, SIGNED)
5616 : 5 : > GET_MODE_BITSIZE (lhs_mode))
5617 : 5 : || (wi::min_precision (op_min_value, SIGNED)
5618 : 5 : > GET_MODE_BITSIZE (lhs_mode)))
5619 : 0 : goto unsupported;
5620 : 99 : }
5621 : :
5622 : 10 : cvt_type
5623 : 10 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
5624 : 10 : cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
5625 : 10 : if (cvt_type == NULL_TREE)
5626 : 0 : goto unsupported;
5627 : 10 : if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
5628 : : &code1, &multi_step_cvt,
5629 : : &interm_types))
5630 : 0 : goto unsupported;
5631 : 10 : if (supportable_convert_operation ((tree_code) code, vectype_out,
5632 : : cvt_type, &tc1))
5633 : : {
5634 : 10 : codecvt1 = tc1;
5635 : 10 : modifier = NARROW_SRC;
5636 : 10 : break;
5637 : : }
5638 : : }
5639 : :
5640 : 3539 : goto unsupported;
5641 : :
5642 : : default:
5643 : : gcc_unreachable ();
5644 : : }
5645 : :
5646 : 91441 : if (modifier == WIDEN
5647 : 91441 : && loop_vinfo
5648 : 41425 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
5649 : 106797 : && (code1 == VEC_WIDEN_MULT_EVEN_EXPR
5650 : 15326 : || widening_evenodd_fn_p (code1)))
5651 : : {
5652 : 30 : if (dump_enabled_p ())
5653 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5654 : : "can't use a fully-masked loop because"
5655 : : " widening operation on even/odd elements"
5656 : : " mixes up lanes.\n");
5657 : 30 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
5658 : : }
5659 : :
5660 : 91441 : if (cost_vec) /* transformation not required. */
5661 : : {
5662 : 68565 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
5663 : 68565 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in))
5664 : : {
5665 : 0 : if (dump_enabled_p ())
5666 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5667 : : "incompatible vector types for invariants\n");
5668 : 0 : return false;
5669 : : }
5670 : 68565 : DUMP_VECT_SCOPE ("vectorizable_conversion");
5671 : 68565 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
5672 : 68565 : if (modifier == NONE)
5673 : : {
5674 : 13246 : SLP_TREE_TYPE (slp_node) = type_conversion_vec_info_type;
5675 : 13246 : vect_model_simple_cost (vinfo, (1 + multi_step_cvt),
5676 : : slp_node, cost_vec);
5677 : : }
5678 : 55319 : else if (modifier == NARROW_SRC || modifier == NARROW_DST)
5679 : : {
5680 : 22543 : SLP_TREE_TYPE (slp_node) = type_demotion_vec_info_type;
5681 : : /* The final packing step produces one vector result per copy. */
5682 : 22543 : vect_model_promotion_demotion_cost (slp_node, nvectors,
5683 : : multi_step_cvt, cost_vec,
5684 : : widen_arith);
5685 : : }
5686 : : else
5687 : : {
5688 : 32776 : SLP_TREE_TYPE (slp_node) = type_promotion_vec_info_type;
5689 : : /* The initial unpacking step produces two vector results
5690 : : per copy. MULTI_STEP_CVT is 0 for a single conversion,
5691 : : so >> MULTI_STEP_CVT divides by 2^(number of steps - 1). */
5692 : 32776 : vect_model_promotion_demotion_cost (slp_node,
5693 : : nvectors >> multi_step_cvt,
5694 : : multi_step_cvt, cost_vec,
5695 : : widen_arith);
5696 : : }
5697 : 68565 : interm_types.release ();
5698 : 68565 : return true;
5699 : 68565 : }
5700 : :
5701 : : /* Transform. */
5702 : 22876 : if (dump_enabled_p ())
5703 : 4231 : dump_printf_loc (MSG_NOTE, vect_location, "transform conversion.\n");
5704 : :
5705 : 22876 : if (op_type == binary_op)
5706 : : {
5707 : 496 : if (CONSTANT_CLASS_P (op0))
5708 : 0 : op0 = fold_convert (TREE_TYPE (op1), op0);
5709 : 496 : else if (CONSTANT_CLASS_P (op1))
5710 : 228 : op1 = fold_convert (TREE_TYPE (op0), op1);
5711 : : }
5712 : :
5713 : : /* In case of multi-step conversion, we first generate conversion operations
5714 : : to the intermediate types, and then from that types to the final one.
5715 : : We create vector destinations for the intermediate type (TYPES) received
5716 : : from supportable_*_operation, and store them in the correct order
5717 : : for future use in vect_create_vectorized_*_stmts (). */
5718 : 22876 : auto_vec<tree> vec_dsts (multi_step_cvt + 1);
5719 : 22876 : bool widen_or_narrow_float_p
5720 : 22876 : = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
5721 : 22876 : vec_dest = vect_create_destination_var (scalar_dest,
5722 : : widen_or_narrow_float_p
5723 : : ? cvt_type : vectype_out);
5724 : 22876 : vec_dsts.quick_push (vec_dest);
5725 : :
5726 : 22876 : if (multi_step_cvt)
5727 : : {
5728 : 8741 : for (i = interm_types.length () - 1;
5729 : 8741 : interm_types.iterate (i, &intermediate_type); i--)
5730 : : {
5731 : 4614 : vec_dest = vect_create_destination_var (scalar_dest,
5732 : : intermediate_type);
5733 : 4614 : vec_dsts.quick_push (vec_dest);
5734 : : }
5735 : : }
5736 : :
5737 : 22876 : if (cvt_type)
5738 : 73 : vec_dest = vect_create_destination_var (scalar_dest,
5739 : : widen_or_narrow_float_p
5740 : : ? vectype_out : cvt_type);
5741 : :
5742 : 22876 : switch (modifier)
5743 : : {
5744 : 4241 : case NONE:
5745 : 4241 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5746 : : /* vec_dest is intermediate type operand when multi_step_cvt. */
5747 : 4241 : if (multi_step_cvt)
5748 : : {
5749 : 21 : cvt_op = vec_dest;
5750 : 21 : vec_dest = vec_dsts[0];
5751 : : }
5752 : :
5753 : 8814 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5754 : : {
5755 : : /* Arguments are ready, create the new vector stmt. */
5756 : 4573 : gimple* new_stmt;
5757 : 4573 : if (multi_step_cvt)
5758 : : {
5759 : 21 : gcc_assert (multi_step_cvt == 1);
5760 : 21 : new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
5761 : 21 : new_temp = make_ssa_name (cvt_op, new_stmt);
5762 : 21 : gimple_assign_set_lhs (new_stmt, new_temp);
5763 : 21 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5764 : 21 : vop0 = new_temp;
5765 : : }
5766 : 4573 : new_stmt = vect_gimple_build (vec_dest, code1, vop0);
5767 : 4573 : new_temp = make_ssa_name (vec_dest, new_stmt);
5768 : 4573 : gimple_set_lhs (new_stmt, new_temp);
5769 : 4573 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5770 : :
5771 : 4573 : slp_node->push_vec_def (new_stmt);
5772 : : }
5773 : : break;
5774 : :
5775 : 9736 : case WIDEN:
5776 : : /* In case the vectorization factor (VF) is bigger than the number
5777 : : of elements that we can fit in a vectype (nunits), we have to
5778 : : generate more than one vector stmt - i.e - we need to "unroll"
5779 : : the vector stmt by a factor VF/nunits. */
5780 : 9736 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0,
5781 : 9736 : code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
5782 : : &vec_oprnds1);
5783 : 9736 : if (code == WIDEN_LSHIFT_EXPR)
5784 : : {
5785 : 0 : int oprnds_size = vec_oprnds0.length ();
5786 : 0 : vec_oprnds1.create (oprnds_size);
5787 : 0 : for (i = 0; i < oprnds_size; ++i)
5788 : 0 : vec_oprnds1.quick_push (op1);
5789 : : }
5790 : : /* Arguments are ready. Create the new vector stmts. */
5791 : 21121 : for (i = multi_step_cvt; i >= 0; i--)
5792 : : {
5793 : 11385 : tree this_dest = vec_dsts[i];
5794 : 11385 : code_helper c1 = code1, c2 = code2;
5795 : 11385 : if (i == 0 && codecvt2 != ERROR_MARK)
5796 : : {
5797 : 48 : c1 = codecvt1;
5798 : 48 : c2 = codecvt2;
5799 : : }
5800 : 11385 : if (known_eq (nunits_out, nunits_in))
5801 : 14 : vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
5802 : : stmt_info, this_dest, gsi, c1,
5803 : : op_type);
5804 : : else
5805 : 11371 : vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
5806 : : &vec_oprnds1, stmt_info,
5807 : : this_dest, gsi,
5808 : : c1, c2, op_type);
5809 : : }
5810 : :
5811 : 36928 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5812 : : {
5813 : 27192 : gimple *new_stmt;
5814 : 27192 : if (cvt_type)
5815 : : {
5816 : 120 : new_temp = make_ssa_name (vec_dest);
5817 : 120 : new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5818 : 120 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5819 : : }
5820 : : else
5821 : 27072 : new_stmt = SSA_NAME_DEF_STMT (vop0);
5822 : :
5823 : 27192 : slp_node->push_vec_def (new_stmt);
5824 : : }
5825 : : break;
5826 : :
5827 : 8899 : case NARROW_SRC:
5828 : 8899 : case NARROW_DST:
5829 : : /* In case the vectorization factor (VF) is bigger than the number
5830 : : of elements that we can fit in a vectype (nunits), we have to
5831 : : generate more than one vector stmt - i.e - we need to "unroll"
5832 : : the vector stmt by a factor VF/nunits. */
5833 : 8899 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5834 : : /* Arguments are ready. Create the new vector stmts. */
5835 : 8899 : if (cvt_type && modifier == NARROW_DST)
5836 : 153 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5837 : : {
5838 : 124 : new_temp = make_ssa_name (vec_dest);
5839 : 124 : gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5840 : 124 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5841 : 124 : vec_oprnds0[i] = new_temp;
5842 : : }
5843 : :
5844 : 8899 : vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
5845 : : multi_step_cvt,
5846 : : stmt_info, vec_dsts, gsi,
5847 : : slp_node, code1,
5848 : : modifier == NARROW_SRC);
5849 : : /* After demoting op0 to cvt_type, convert it to dest. */
5850 : 8899 : if (cvt_type && code == FLOAT_EXPR)
5851 : : {
5852 : 10 : for (unsigned int i = 0; i != vec_oprnds0.length() / 2; i++)
5853 : : {
5854 : : /* Arguments are ready, create the new vector stmt. */
5855 : 5 : gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
5856 : 5 : gimple *new_stmt
5857 : 5 : = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
5858 : 5 : new_temp = make_ssa_name (vec_dest, new_stmt);
5859 : 5 : gimple_set_lhs (new_stmt, new_temp);
5860 : 5 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5861 : :
5862 : : /* This is the last step of the conversion sequence. Store the
5863 : : vectors in SLP_NODE or in vector info of the scalar statement
5864 : : (or in STMT_VINFO_RELATED_STMT chain). */
5865 : 5 : slp_node->push_vec_def (new_stmt);
5866 : : }
5867 : : }
5868 : : break;
5869 : : }
5870 : :
5871 : 22876 : vec_oprnds0.release ();
5872 : 22876 : vec_oprnds1.release ();
5873 : 22876 : interm_types.release ();
5874 : :
5875 : 22876 : return true;
5876 : 152983 : }
5877 : :
5878 : : /* Return true if we can assume from the scalar form of STMT_INFO that
5879 : : neither the scalar nor the vector forms will generate code. STMT_INFO
5880 : : is known not to involve a data reference. */
5881 : :
5882 : : bool
5883 : 1172582 : vect_nop_conversion_p (stmt_vec_info stmt_info)
5884 : : {
5885 : 1172582 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5886 : 926912 : if (!stmt)
5887 : : return false;
5888 : :
5889 : 926912 : tree lhs = gimple_assign_lhs (stmt);
5890 : 926912 : tree_code code = gimple_assign_rhs_code (stmt);
5891 : 926912 : tree rhs = gimple_assign_rhs1 (stmt);
5892 : :
5893 : 926912 : if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
5894 : : return true;
5895 : :
5896 : 924655 : if (CONVERT_EXPR_CODE_P (code))
5897 : 199821 : return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
5898 : :
5899 : : return false;
5900 : : }
5901 : :
5902 : : /* Function vectorizable_assignment.
5903 : :
5904 : : Check if STMT_INFO performs an assignment (copy) that can be vectorized.
5905 : : If COST_VEC is passed, calculate costs but don't change anything,
5906 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5907 : : it, and insert it at GSI.
5908 : : Return true if STMT_INFO is vectorizable in this way. */
5909 : :
5910 : : static bool
5911 : 1844084 : vectorizable_assignment (vec_info *vinfo,
5912 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5913 : : slp_tree slp_node,
5914 : : stmt_vector_for_cost *cost_vec)
5915 : : {
5916 : 1844084 : tree vec_dest;
5917 : 1844084 : tree scalar_dest;
5918 : 1844084 : tree op;
5919 : 1844084 : tree new_temp;
5920 : 1844084 : enum vect_def_type dt[1] = {vect_unknown_def_type};
5921 : 1844084 : int i;
5922 : 1844084 : vec<tree> vec_oprnds = vNULL;
5923 : 1844084 : tree vop;
5924 : 1844084 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5925 : 1844084 : enum tree_code code;
5926 : 1844084 : tree vectype_in;
5927 : :
5928 : 1844084 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5929 : : return false;
5930 : :
5931 : 1844084 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5932 : 179117 : && cost_vec)
5933 : : return false;
5934 : :
5935 : : /* Is vectorizable assignment? */
5936 : 3371388 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5937 : 1600163 : if (!stmt)
5938 : : return false;
5939 : :
5940 : 1600163 : scalar_dest = gimple_assign_lhs (stmt);
5941 : 1600163 : if (TREE_CODE (scalar_dest) != SSA_NAME)
5942 : : return false;
5943 : :
5944 : 819035 : if (STMT_VINFO_DATA_REF (stmt_info))
5945 : : return false;
5946 : :
5947 : 354047 : code = gimple_assign_rhs_code (stmt);
5948 : 354047 : if (!(gimple_assign_single_p (stmt)
5949 : 352713 : || code == PAREN_EXPR
5950 : 351131 : || CONVERT_EXPR_CODE_P (code)))
5951 : : return false;
5952 : :
5953 : 83123 : tree vectype = SLP_TREE_VECTYPE (slp_node);
5954 : 83123 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5955 : :
5956 : 83123 : slp_tree slp_op;
5957 : 83123 : if (!vect_is_simple_use (vinfo, slp_node, 0, &op, &slp_op,
5958 : : &dt[0], &vectype_in))
5959 : : {
5960 : 0 : if (dump_enabled_p ())
5961 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5962 : : "use not simple.\n");
5963 : 0 : return false;
5964 : : }
5965 : 83123 : if (!vectype_in)
5966 : 17999 : vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
5967 : :
5968 : : /* We can handle VIEW_CONVERT conversions that do not change the number
5969 : : of elements or the vector size or other conversions when the component
5970 : : types are nop-convertible. */
5971 : 83123 : if (!vectype_in
5972 : 82847 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
5973 : 76133 : || (code == VIEW_CONVERT_EXPR
5974 : 2412 : && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
5975 : 2412 : GET_MODE_SIZE (TYPE_MODE (vectype_in))))
5976 : 159256 : || (CONVERT_EXPR_CODE_P (code)
5977 : 73249 : && !tree_nop_conversion_p (TREE_TYPE (vectype),
5978 : 73249 : TREE_TYPE (vectype_in))))
5979 : 10002 : return false;
5980 : :
5981 : 219285 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
5982 : : {
5983 : 2 : if (dump_enabled_p ())
5984 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5985 : : "can't convert between boolean and non "
5986 : 0 : "boolean vectors %T\n", TREE_TYPE (op));
5987 : :
5988 : 2 : return false;
5989 : : }
5990 : :
5991 : : /* We do not handle bit-precision changes. */
5992 : 73119 : if ((CONVERT_EXPR_CODE_P (code)
5993 : 2884 : || code == VIEW_CONVERT_EXPR)
5994 : 71441 : && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
5995 : 70173 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
5996 : 71141 : || (INTEGRAL_TYPE_P (TREE_TYPE (op))
5997 : 66710 : && !type_has_mode_precision_p (TREE_TYPE (op))))
5998 : : /* But a conversion that does not change the bit-pattern is ok. */
5999 : 73787 : && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6000 : 668 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
6001 : 668 : && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
6002 : 668 : > TYPE_PRECISION (TREE_TYPE (op)))
6003 : 368 : && TYPE_UNSIGNED (TREE_TYPE (op)))
6004 : 316 : || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
6005 : 316 : == TYPE_PRECISION (TREE_TYPE (op))))))
6006 : : {
6007 : 260 : if (dump_enabled_p ())
6008 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6009 : : "type conversion to/from bit-precision "
6010 : : "unsupported.\n");
6011 : 260 : return false;
6012 : : }
6013 : :
6014 : 72859 : if (cost_vec) /* transformation not required. */
6015 : : {
6016 : 58171 : if (!vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
6017 : : {
6018 : 0 : if (dump_enabled_p ())
6019 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6020 : : "incompatible vector types for invariants\n");
6021 : 0 : return false;
6022 : : }
6023 : 58171 : SLP_TREE_TYPE (slp_node) = assignment_vec_info_type;
6024 : 58171 : DUMP_VECT_SCOPE ("vectorizable_assignment");
6025 : 58171 : if (!vect_nop_conversion_p (stmt_info))
6026 : 1329 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6027 : 58171 : return true;
6028 : : }
6029 : :
6030 : : /* Transform. */
6031 : 14688 : if (dump_enabled_p ())
6032 : 3501 : dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
6033 : :
6034 : : /* Handle def. */
6035 : 14688 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6036 : :
6037 : : /* Handle use. */
6038 : 14688 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
6039 : :
6040 : : /* Arguments are ready. create the new vector stmt. */
6041 : 33445 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
6042 : : {
6043 : 18757 : if (CONVERT_EXPR_CODE_P (code)
6044 : 682 : || code == VIEW_CONVERT_EXPR)
6045 : 18207 : vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
6046 : 18757 : gassign *new_stmt = gimple_build_assign (vec_dest, vop);
6047 : 18757 : new_temp = make_ssa_name (vec_dest, new_stmt);
6048 : 18757 : gimple_assign_set_lhs (new_stmt, new_temp);
6049 : 18757 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6050 : 18757 : slp_node->push_vec_def (new_stmt);
6051 : : }
6052 : :
6053 : 14688 : vec_oprnds.release ();
6054 : 14688 : return true;
6055 : : }
6056 : :
6057 : :
6058 : : /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
6059 : : either as shift by a scalar or by a vector. */
6060 : :
6061 : : bool
6062 : 270324 : vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
6063 : : {
6064 : 270324 : optab optab;
6065 : 270324 : tree vectype;
6066 : :
6067 : 270324 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
6068 : 270324 : if (!vectype)
6069 : : return false;
6070 : :
6071 : 270324 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6072 : 270324 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6073 : : return true;
6074 : :
6075 : 237044 : optab = optab_for_tree_code (code, vectype, optab_vector);
6076 : 237044 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6077 : : return true;
6078 : :
6079 : : return false;
6080 : : }
6081 : :
6082 : :
6083 : : /* Function vectorizable_shift.
6084 : :
6085 : : Check if STMT_INFO performs a shift operation that can be vectorized.
6086 : : If COST_VEC is passed, calculate costs but don't change anything,
6087 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
6088 : : it, and insert it at GSI.
6089 : : Return true if STMT_INFO is vectorizable in this way. */
6090 : :
6091 : : static bool
6092 : 621864 : vectorizable_shift (vec_info *vinfo,
6093 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6094 : : slp_tree slp_node,
6095 : : stmt_vector_for_cost *cost_vec)
6096 : : {
6097 : 621864 : tree vec_dest;
6098 : 621864 : tree scalar_dest;
6099 : 621864 : tree op0, op1 = NULL;
6100 : 621864 : tree vec_oprnd1 = NULL_TREE;
6101 : 621864 : tree vectype;
6102 : 621864 : enum tree_code code;
6103 : 621864 : machine_mode vec_mode;
6104 : 621864 : tree new_temp;
6105 : 621864 : optab optab;
6106 : 621864 : int icode;
6107 : 621864 : machine_mode optab_op2_mode;
6108 : 621864 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
6109 : 621864 : poly_uint64 nunits_in;
6110 : 621864 : poly_uint64 nunits_out;
6111 : 621864 : tree vectype_out;
6112 : 621864 : tree op1_vectype;
6113 : 621864 : int i;
6114 : 621864 : vec<tree> vec_oprnds0 = vNULL;
6115 : 621864 : vec<tree> vec_oprnds1 = vNULL;
6116 : 621864 : tree vop0, vop1;
6117 : 621864 : unsigned int k;
6118 : 621864 : bool scalar_shift_arg = true;
6119 : 621864 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6120 : 621864 : bool incompatible_op1_vectype_p = false;
6121 : :
6122 : 621864 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6123 : : return false;
6124 : :
6125 : 621864 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6126 : 179117 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
6127 : 177778 : && cost_vec)
6128 : : return false;
6129 : :
6130 : : /* Is STMT a vectorizable binary/unary operation? */
6131 : 952990 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6132 : 379686 : if (!stmt)
6133 : : return false;
6134 : :
6135 : 379686 : if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6136 : : return false;
6137 : :
6138 : 379196 : code = gimple_assign_rhs_code (stmt);
6139 : :
6140 : 379196 : if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
6141 : : || code == RROTATE_EXPR))
6142 : : return false;
6143 : :
6144 : 54618 : scalar_dest = gimple_assign_lhs (stmt);
6145 : 54618 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6146 : 54618 : if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6147 : : {
6148 : 0 : if (dump_enabled_p ())
6149 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6150 : : "bit-precision shifts not supported.\n");
6151 : 0 : return false;
6152 : : }
6153 : :
6154 : 54618 : slp_tree slp_op0;
6155 : 54618 : if (!vect_is_simple_use (vinfo, slp_node,
6156 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6157 : : {
6158 : 0 : if (dump_enabled_p ())
6159 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6160 : : "use not simple.\n");
6161 : 0 : return false;
6162 : : }
6163 : : /* If op0 is an external or constant def, infer the vector type
6164 : : from the scalar type. */
6165 : 54618 : if (!vectype)
6166 : 12295 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
6167 : 54618 : if (!cost_vec)
6168 : 7577 : gcc_assert (vectype);
6169 : 54618 : if (!vectype)
6170 : : {
6171 : 0 : if (dump_enabled_p ())
6172 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6173 : : "no vectype for scalar type\n");
6174 : 0 : return false;
6175 : : }
6176 : :
6177 : 54618 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6178 : 54618 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6179 : 54618 : if (maybe_ne (nunits_out, nunits_in))
6180 : : return false;
6181 : :
6182 : 54618 : stmt_vec_info op1_def_stmt_info;
6183 : 54618 : slp_tree slp_op1;
6184 : 54618 : if (!vect_is_simple_use (vinfo, slp_node, 1, &op1, &slp_op1,
6185 : : &dt[1], &op1_vectype, &op1_def_stmt_info))
6186 : : {
6187 : 0 : if (dump_enabled_p ())
6188 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6189 : : "use not simple.\n");
6190 : 0 : return false;
6191 : : }
6192 : :
6193 : : /* Determine whether the shift amount is a vector, or scalar. If the
6194 : : shift/rotate amount is a vector, use the vector/vector shift optabs. */
6195 : :
6196 : 54618 : if ((dt[1] == vect_internal_def
6197 : 54618 : || dt[1] == vect_induction_def
6198 : 41550 : || dt[1] == vect_nested_cycle)
6199 : 13086 : && SLP_TREE_LANES (slp_node) == 1)
6200 : : scalar_shift_arg = false;
6201 : 41587 : else if (dt[1] == vect_constant_def
6202 : : || dt[1] == vect_external_def
6203 : 41587 : || dt[1] == vect_internal_def)
6204 : : {
6205 : : /* In SLP, need to check whether the shift count is the same,
6206 : : in loops if it is a constant or invariant, it is always
6207 : : a scalar shift. */
6208 : 41581 : vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
6209 : 41581 : stmt_vec_info slpstmt_info;
6210 : :
6211 : 114110 : FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
6212 : 72529 : if (slpstmt_info)
6213 : : {
6214 : 72529 : gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
6215 : 145058 : if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
6216 : 72529 : scalar_shift_arg = false;
6217 : : }
6218 : :
6219 : : /* For internal SLP defs we have to make sure we see scalar stmts
6220 : : for all vector elements.
6221 : : ??? For different vectors we could resort to a different
6222 : : scalar shift operand but code-generation below simply always
6223 : : takes the first. */
6224 : 41581 : if (dt[1] == vect_internal_def
6225 : 41630 : && maybe_ne (nunits_out * vect_get_num_copies (vinfo, slp_node),
6226 : 49 : stmts.length ()))
6227 : : scalar_shift_arg = false;
6228 : :
6229 : : /* If the shift amount is computed by a pattern stmt we cannot
6230 : : use the scalar amount directly thus give up and use a vector
6231 : : shift. */
6232 : 41581 : if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
6233 : : scalar_shift_arg = false;
6234 : : }
6235 : : else
6236 : : {
6237 : 6 : if (dump_enabled_p ())
6238 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6239 : : "operand mode requires invariant argument.\n");
6240 : 6 : return false;
6241 : : }
6242 : :
6243 : : /* Vector shifted by vector. */
6244 : 54650 : bool was_scalar_shift_arg = scalar_shift_arg;
6245 : 41572 : if (!scalar_shift_arg)
6246 : : {
6247 : 13078 : optab = optab_for_tree_code (code, vectype, optab_vector);
6248 : 13078 : if (dump_enabled_p ())
6249 : 1191 : dump_printf_loc (MSG_NOTE, vect_location,
6250 : : "vector/vector shift/rotate found.\n");
6251 : :
6252 : 13078 : if (!op1_vectype)
6253 : 15 : op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
6254 : : slp_op1);
6255 : 13078 : incompatible_op1_vectype_p
6256 : 26156 : = (op1_vectype == NULL_TREE
6257 : 13078 : || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
6258 : 13078 : TYPE_VECTOR_SUBPARTS (vectype))
6259 : 26154 : || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
6260 : 13071 : if (incompatible_op1_vectype_p
6261 : 7 : && (SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
6262 : 1 : || slp_op1->refcnt != 1))
6263 : : {
6264 : 6 : if (dump_enabled_p ())
6265 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6266 : : "unusable type for last operand in"
6267 : : " vector/vector shift/rotate.\n");
6268 : 6 : return false;
6269 : : }
6270 : : }
6271 : : /* See if the machine has a vector shifted by scalar insn and if not
6272 : : then see if it has a vector shifted by vector insn. */
6273 : : else
6274 : : {
6275 : 41534 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6276 : 41534 : if (optab
6277 : 41534 : && can_implement_p (optab, TYPE_MODE (vectype)))
6278 : : {
6279 : 41534 : if (dump_enabled_p ())
6280 : 4870 : dump_printf_loc (MSG_NOTE, vect_location,
6281 : : "vector/scalar shift/rotate found.\n");
6282 : : }
6283 : : else
6284 : : {
6285 : 0 : optab = optab_for_tree_code (code, vectype, optab_vector);
6286 : 0 : if (optab
6287 : 0 : && can_implement_p (optab, TYPE_MODE (vectype)))
6288 : : {
6289 : 0 : scalar_shift_arg = false;
6290 : :
6291 : 0 : if (dump_enabled_p ())
6292 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6293 : : "vector/vector shift/rotate found.\n");
6294 : :
6295 : 0 : if (!op1_vectype)
6296 : 0 : op1_vectype = get_vectype_for_scalar_type (vinfo,
6297 : 0 : TREE_TYPE (op1),
6298 : : slp_op1);
6299 : :
6300 : : /* Unlike the other binary operators, shifts/rotates have
6301 : : the rhs being int, instead of the same type as the lhs,
6302 : : so make sure the scalar is the right type if we are
6303 : : dealing with vectors of long long/long/short/char. */
6304 : 0 : incompatible_op1_vectype_p
6305 : 0 : = (!op1_vectype
6306 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype),
6307 : 0 : TREE_TYPE (op1)));
6308 : 0 : if (incompatible_op1_vectype_p
6309 : 0 : && dt[1] == vect_internal_def)
6310 : : {
6311 : 0 : if (dump_enabled_p ())
6312 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6313 : : "unusable type for last operand in"
6314 : : " vector/vector shift/rotate.\n");
6315 : 0 : return false;
6316 : : }
6317 : : }
6318 : : }
6319 : : }
6320 : :
6321 : : /* Supportable by target? */
6322 : 54606 : if (!optab)
6323 : : {
6324 : 0 : if (dump_enabled_p ())
6325 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6326 : : "no optab.\n");
6327 : 0 : return false;
6328 : : }
6329 : 54606 : vec_mode = TYPE_MODE (vectype);
6330 : 54606 : icode = (int) optab_handler (optab, vec_mode);
6331 : 54606 : if (icode == CODE_FOR_nothing)
6332 : : {
6333 : 6046 : if (dump_enabled_p ())
6334 : 886 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6335 : : "op not supported by target.\n");
6336 : 6046 : return false;
6337 : : }
6338 : : /* vector lowering cannot optimize vector shifts using word arithmetic. */
6339 : 48560 : if (vect_emulated_vector_p (vectype))
6340 : : return false;
6341 : :
6342 : 48560 : if (cost_vec) /* transformation not required. */
6343 : : {
6344 : 40983 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6345 : 40983 : || ((!scalar_shift_arg || dt[1] == vect_internal_def)
6346 : 4946 : && (!incompatible_op1_vectype_p
6347 : 1 : || dt[1] == vect_constant_def)
6348 : 4946 : && !vect_maybe_update_slp_op_vectype
6349 : 4946 : (slp_op1,
6350 : : incompatible_op1_vectype_p ? vectype : op1_vectype)))
6351 : : {
6352 : 0 : if (dump_enabled_p ())
6353 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6354 : : "incompatible vector types for invariants\n");
6355 : 0 : return false;
6356 : : }
6357 : : /* Now adjust the constant shift amount in place. */
6358 : 40983 : if (incompatible_op1_vectype_p
6359 : 1 : && dt[1] == vect_constant_def)
6360 : 4 : for (unsigned i = 0;
6361 : 5 : i < SLP_TREE_SCALAR_OPS (slp_op1).length (); ++i)
6362 : : {
6363 : 4 : SLP_TREE_SCALAR_OPS (slp_op1)[i]
6364 : 4 : = fold_convert (TREE_TYPE (vectype),
6365 : : SLP_TREE_SCALAR_OPS (slp_op1)[i]);
6366 : 4 : gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
6367 : : == INTEGER_CST));
6368 : : }
6369 : 40983 : SLP_TREE_TYPE (slp_node) = shift_vec_info_type;
6370 : 40983 : DUMP_VECT_SCOPE ("vectorizable_shift");
6371 : 40983 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6372 : 40983 : return true;
6373 : : }
6374 : :
6375 : : /* Transform. */
6376 : :
6377 : 7577 : if (dump_enabled_p ())
6378 : 1988 : dump_printf_loc (MSG_NOTE, vect_location,
6379 : : "transform binary/unary operation.\n");
6380 : :
6381 : : /* Handle def. */
6382 : 7577 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6383 : :
6384 : 7577 : unsigned nvectors = vect_get_num_copies (vinfo, slp_node);
6385 : 7577 : if (scalar_shift_arg && dt[1] != vect_internal_def)
6386 : : {
6387 : : /* Vector shl and shr insn patterns can be defined with scalar
6388 : : operand 2 (shift operand). In this case, use constant or loop
6389 : : invariant op1 directly, without extending it to vector mode
6390 : : first. */
6391 : 5477 : optab_op2_mode = insn_data[icode].operand[2].mode;
6392 : 5477 : if (!VECTOR_MODE_P (optab_op2_mode))
6393 : : {
6394 : 5477 : if (dump_enabled_p ())
6395 : 1874 : dump_printf_loc (MSG_NOTE, vect_location,
6396 : : "operand 1 using scalar mode.\n");
6397 : 5477 : vec_oprnd1 = op1;
6398 : 5477 : vec_oprnds1.create (nvectors);
6399 : 5477 : vec_oprnds1.quick_push (vec_oprnd1);
6400 : : /* Store vec_oprnd1 for every vector stmt to be created.
6401 : : We check during the analysis that all the shift arguments
6402 : : are the same.
6403 : : TODO: Allow different constants for different vector
6404 : : stmts generated for an SLP instance. */
6405 : 12967 : for (k = 0; k < nvectors - 1; k++)
6406 : 2013 : vec_oprnds1.quick_push (vec_oprnd1);
6407 : : }
6408 : : }
6409 : 2100 : else if (!scalar_shift_arg && incompatible_op1_vectype_p)
6410 : : {
6411 : 0 : if (was_scalar_shift_arg)
6412 : : {
6413 : : /* If the argument was the same in all lanes create the
6414 : : correctly typed vector shift amount directly. Note
6415 : : we made SLP scheduling think we use the original scalars,
6416 : : so place the compensation code next to the shift which
6417 : : is conservative. See PR119640 where it otherwise breaks. */
6418 : 0 : op1 = fold_convert (TREE_TYPE (vectype), op1);
6419 : 0 : op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
6420 : : gsi);
6421 : 0 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
6422 : : gsi);
6423 : 0 : vec_oprnds1.create (nvectors);
6424 : 0 : for (k = 0; k < nvectors; k++)
6425 : 0 : vec_oprnds1.quick_push (vec_oprnd1);
6426 : : }
6427 : 0 : else if (dt[1] == vect_constant_def)
6428 : : /* The constant shift amount has been adjusted in place. */
6429 : : ;
6430 : : else
6431 : 0 : gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
6432 : : }
6433 : :
6434 : : /* vec_oprnd1 is available if operand 1 should be of a scalar-type
6435 : : (a special case for certain kind of vector shifts); otherwise,
6436 : : operand 1 should be of a vector type (the usual case). */
6437 : 2100 : vect_get_vec_defs (vinfo, slp_node,
6438 : : op0, &vec_oprnds0,
6439 : 7577 : vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
6440 : :
6441 : : /* Arguments are ready. Create the new vector stmt. */
6442 : 20598 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6443 : : {
6444 : : /* For internal defs where we need to use a scalar shift arg
6445 : : extract the first lane. */
6446 : 13021 : if (scalar_shift_arg && dt[1] == vect_internal_def)
6447 : : {
6448 : 10 : vop1 = vec_oprnds1[0];
6449 : 10 : new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
6450 : 10 : gassign *new_stmt
6451 : 10 : = gimple_build_assign (new_temp,
6452 : 10 : build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
6453 : : vop1,
6454 : 10 : TYPE_SIZE (TREE_TYPE (new_temp)),
6455 : : bitsize_zero_node));
6456 : 10 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6457 : 10 : vop1 = new_temp;
6458 : 10 : }
6459 : : else
6460 : 13011 : vop1 = vec_oprnds1[i];
6461 : 13021 : gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
6462 : 13021 : new_temp = make_ssa_name (vec_dest, new_stmt);
6463 : 13021 : gimple_assign_set_lhs (new_stmt, new_temp);
6464 : 13021 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6465 : 13021 : slp_node->push_vec_def (new_stmt);
6466 : : }
6467 : :
6468 : 7577 : vec_oprnds0.release ();
6469 : 7577 : vec_oprnds1.release ();
6470 : :
6471 : 7577 : return true;
6472 : : }
6473 : :
6474 : : /* Function vectorizable_operation.
6475 : :
6476 : : Check if STMT_INFO performs a binary, unary or ternary operation that can
6477 : : be vectorized.
6478 : : If COST_VEC is passed, calculate costs but don't change anything,
6479 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
6480 : : it, and insert it at GSI.
6481 : : Return true if STMT_INFO is vectorizable in this way. */
6482 : :
6483 : : static bool
6484 : 2355311 : vectorizable_operation (vec_info *vinfo,
6485 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6486 : : slp_tree slp_node,
6487 : : stmt_vector_for_cost *cost_vec)
6488 : : {
6489 : 2355311 : tree vec_dest;
6490 : 2355311 : tree scalar_dest;
6491 : 2355311 : tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
6492 : 2355311 : tree vectype;
6493 : 2355311 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6494 : 2355311 : enum tree_code code, orig_code;
6495 : 2355311 : machine_mode vec_mode;
6496 : 2355311 : tree new_temp;
6497 : 2355311 : int op_type;
6498 : 2355311 : optab optab;
6499 : 2355311 : bool target_support_p;
6500 : 2355311 : enum vect_def_type dt[3]
6501 : : = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
6502 : 2355311 : poly_uint64 nunits_in;
6503 : 2355311 : poly_uint64 nunits_out;
6504 : 2355311 : tree vectype_out;
6505 : 2355311 : int i;
6506 : 2355311 : vec<tree> vec_oprnds0 = vNULL;
6507 : 2355311 : vec<tree> vec_oprnds1 = vNULL;
6508 : 2355311 : vec<tree> vec_oprnds2 = vNULL;
6509 : 2355311 : tree vop0, vop1, vop2;
6510 : 2355311 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6511 : :
6512 : 2355311 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6513 : : return false;
6514 : :
6515 : 2355311 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6516 : 179117 : && cost_vec)
6517 : : return false;
6518 : :
6519 : : /* Is STMT a vectorizable binary/unary operation? */
6520 : 3940786 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6521 : 2111390 : if (!stmt)
6522 : : return false;
6523 : :
6524 : : /* Loads and stores are handled in vectorizable_{load,store}. */
6525 : 2111390 : if (STMT_VINFO_DATA_REF (stmt_info))
6526 : : return false;
6527 : :
6528 : 865274 : orig_code = code = gimple_assign_rhs_code (stmt);
6529 : :
6530 : : /* Shifts are handled in vectorizable_shift. */
6531 : 865274 : if (code == LSHIFT_EXPR
6532 : : || code == RSHIFT_EXPR
6533 : : || code == LROTATE_EXPR
6534 : 865274 : || code == RROTATE_EXPR)
6535 : : return false;
6536 : :
6537 : : /* Comparisons are handled in vectorizable_comparison. */
6538 : 818233 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6539 : : return false;
6540 : :
6541 : : /* Conditions are handled in vectorizable_condition. */
6542 : 643797 : if (code == COND_EXPR)
6543 : : return false;
6544 : :
6545 : : /* For pointer addition and subtraction, we should use the normal
6546 : : plus and minus for the vector operation. */
6547 : 624274 : if (code == POINTER_PLUS_EXPR)
6548 : : code = PLUS_EXPR;
6549 : 610014 : if (code == POINTER_DIFF_EXPR)
6550 : 980 : code = MINUS_EXPR;
6551 : :
6552 : : /* Support only unary or binary operations. */
6553 : 624274 : op_type = TREE_CODE_LENGTH (code);
6554 : 624274 : if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
6555 : : {
6556 : 0 : if (dump_enabled_p ())
6557 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6558 : : "num. args = %d (not unary/binary/ternary op).\n",
6559 : : op_type);
6560 : 0 : return false;
6561 : : }
6562 : :
6563 : 624274 : scalar_dest = gimple_assign_lhs (stmt);
6564 : 624274 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6565 : :
6566 : : /* Most operations cannot handle bit-precision types without extra
6567 : : truncations. */
6568 : 624274 : bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
6569 : 615878 : if (!mask_op_p
6570 : 615878 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
6571 : : /* Exception are bitwise binary operations. */
6572 : : && code != BIT_IOR_EXPR
6573 : 1533 : && code != BIT_XOR_EXPR
6574 : 1248 : && code != BIT_AND_EXPR)
6575 : : {
6576 : 1010 : if (dump_enabled_p ())
6577 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6578 : : "bit-precision arithmetic not supported.\n");
6579 : 1010 : return false;
6580 : : }
6581 : :
6582 : 623264 : slp_tree slp_op0;
6583 : 623264 : if (!vect_is_simple_use (vinfo, slp_node,
6584 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6585 : : {
6586 : 0 : if (dump_enabled_p ())
6587 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6588 : : "use not simple.\n");
6589 : 0 : return false;
6590 : : }
6591 : 623264 : bool is_invariant = (dt[0] == vect_external_def
6592 : 623264 : || dt[0] == vect_constant_def);
6593 : : /* If op0 is an external or constant def, infer the vector type
6594 : : from the scalar type. */
6595 : 623264 : if (!vectype)
6596 : : {
6597 : : /* For boolean type we cannot determine vectype by
6598 : : invariant value (don't know whether it is a vector
6599 : : of booleans or vector of integers). We use output
6600 : : vectype because operations on boolean don't change
6601 : : type. */
6602 : 69860 : if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
6603 : : {
6604 : 1083 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
6605 : : {
6606 : 229 : if (dump_enabled_p ())
6607 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6608 : : "not supported operation on bool value.\n");
6609 : 229 : return false;
6610 : : }
6611 : 854 : vectype = vectype_out;
6612 : : }
6613 : : else
6614 : 68777 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
6615 : : slp_node);
6616 : : }
6617 : 623035 : if (!cost_vec)
6618 : 116349 : gcc_assert (vectype);
6619 : 623035 : if (!vectype)
6620 : : {
6621 : 288 : if (dump_enabled_p ())
6622 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6623 : : "no vectype for scalar type %T\n",
6624 : 0 : TREE_TYPE (op0));
6625 : :
6626 : 288 : return false;
6627 : : }
6628 : :
6629 : 622747 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6630 : 622747 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6631 : 622747 : if (maybe_ne (nunits_out, nunits_in)
6632 : 622747 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
6633 : 10875 : return false;
6634 : :
6635 : 611872 : tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
6636 : 611872 : slp_tree slp_op1 = NULL, slp_op2 = NULL;
6637 : 611872 : if (op_type == binary_op || op_type == ternary_op)
6638 : : {
6639 : 542918 : if (!vect_is_simple_use (vinfo, slp_node,
6640 : : 1, &op1, &slp_op1, &dt[1], &vectype2))
6641 : : {
6642 : 0 : if (dump_enabled_p ())
6643 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6644 : : "use not simple.\n");
6645 : 0 : return false;
6646 : : }
6647 : 542918 : is_invariant &= (dt[1] == vect_external_def
6648 : 542918 : || dt[1] == vect_constant_def);
6649 : 542918 : if (vectype2
6650 : 910470 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
6651 : 367552 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6652 : 367552 : TREE_TYPE (vectype2))))
6653 : 4 : return false;
6654 : : }
6655 : 611868 : if (op_type == ternary_op)
6656 : : {
6657 : 0 : if (!vect_is_simple_use (vinfo, slp_node,
6658 : : 2, &op2, &slp_op2, &dt[2], &vectype3))
6659 : : {
6660 : 0 : if (dump_enabled_p ())
6661 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6662 : : "use not simple.\n");
6663 : 0 : return false;
6664 : : }
6665 : 0 : is_invariant &= (dt[2] == vect_external_def
6666 : 0 : || dt[2] == vect_constant_def);
6667 : 0 : if (vectype3
6668 : 0 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
6669 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6670 : 0 : TREE_TYPE (vectype3))))
6671 : 0 : return false;
6672 : : }
6673 : :
6674 : : /* Multiple types in SLP are handled by creating the appropriate number of
6675 : : vectorized stmts for each SLP node. */
6676 : 611868 : auto vec_num = vect_get_num_copies (vinfo, slp_node);
6677 : :
6678 : : /* Reject attempts to combine mask types with nonmask types, e.g. if
6679 : : we have an AND between a (nonmask) boolean loaded from memory and
6680 : : a (mask) boolean result of a comparison.
6681 : :
6682 : : TODO: We could easily fix these cases up using pattern statements. */
6683 : 611868 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
6684 : 973679 : || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
6685 : 1223736 : || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
6686 : : {
6687 : 0 : if (dump_enabled_p ())
6688 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6689 : : "mixed mask and nonmask vector types\n");
6690 : 0 : return false;
6691 : : }
6692 : :
6693 : : /* Supportable by target? */
6694 : :
6695 : 611868 : vec_mode = TYPE_MODE (vectype);
6696 : 611868 : optab = optab_for_tree_code (code, vectype, optab_default);
6697 : 611868 : if (!optab)
6698 : : {
6699 : 57617 : if (dump_enabled_p ())
6700 : 5693 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6701 : : "no optab.\n");
6702 : 57617 : return false;
6703 : : }
6704 : 554251 : target_support_p = can_implement_p (optab, vec_mode);
6705 : :
6706 : 554251 : bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
6707 : 554251 : if (!target_support_p || using_emulated_vectors_p)
6708 : : {
6709 : 28931 : if (dump_enabled_p ())
6710 : 1088 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6711 : : "op not supported by target.\n");
6712 : : /* When vec_mode is not a vector mode and we verified ops we
6713 : : do not have to lower like AND are natively supported let
6714 : : those through even when the mode isn't word_mode. For
6715 : : ops we have to lower the lowering code assumes we are
6716 : : dealing with word_mode. */
6717 : 57862 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
6718 : 28837 : || !GET_MODE_SIZE (vec_mode).is_constant ()
6719 : 28837 : || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6720 : 23509 : || !target_support_p)
6721 : 61480 : && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
6722 : : /* Check only during analysis. */
6723 : 39622 : || (cost_vec && !vect_can_vectorize_without_simd_p (code)))
6724 : : {
6725 : 28173 : if (dump_enabled_p ())
6726 : 1088 : dump_printf (MSG_NOTE, "using word mode not possible.\n");
6727 : 28173 : return false;
6728 : : }
6729 : 758 : if (dump_enabled_p ())
6730 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6731 : : "proceeding using word mode.\n");
6732 : : using_emulated_vectors_p = true;
6733 : : }
6734 : :
6735 : 526078 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
6736 : 526078 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
6737 : 347755 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
6738 : 526078 : internal_fn cond_fn = get_conditional_internal_fn (code);
6739 : 526078 : internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
6740 : :
6741 : : /* If operating on inactive elements could generate spurious traps,
6742 : : we need to restrict the operation to active lanes. Note that this
6743 : : specifically doesn't apply to unhoisted invariants, since they
6744 : : operate on the same value for every lane.
6745 : :
6746 : : Similarly, if this operation is part of a reduction, a fully-masked
6747 : : loop should only change the active lanes of the reduction chain,
6748 : : keeping the inactive lanes as-is. */
6749 : 498479 : bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
6750 : 969774 : || reduc_idx >= 0);
6751 : :
6752 : 526078 : if (cost_vec) /* transformation not required. */
6753 : : {
6754 : 409729 : if (loop_vinfo
6755 : 242630 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
6756 : 55505 : && mask_out_inactive)
6757 : : {
6758 : 14923 : if (cond_len_fn != IFN_LAST
6759 : 14923 : && direct_internal_fn_supported_p (cond_len_fn, vectype,
6760 : : OPTIMIZE_FOR_SPEED))
6761 : 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype,
6762 : : 1);
6763 : 14923 : else if (cond_fn != IFN_LAST
6764 : 14923 : && direct_internal_fn_supported_p (cond_fn, vectype,
6765 : : OPTIMIZE_FOR_SPEED))
6766 : 6885 : vect_record_loop_mask (loop_vinfo, masks, vec_num,
6767 : : vectype, NULL);
6768 : : else
6769 : : {
6770 : 8038 : if (dump_enabled_p ())
6771 : 498 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6772 : : "can't use a fully-masked loop because no"
6773 : : " conditional operation is available.\n");
6774 : 8038 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
6775 : : }
6776 : : }
6777 : :
6778 : : /* Put types on constant and invariant SLP children. */
6779 : 409729 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6780 : 409665 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
6781 : 819295 : || !vect_maybe_update_slp_op_vectype (slp_op2, vectype))
6782 : : {
6783 : 163 : if (dump_enabled_p ())
6784 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6785 : : "incompatible vector types for invariants\n");
6786 : 163 : return false;
6787 : : }
6788 : :
6789 : 409566 : SLP_TREE_TYPE (slp_node) = op_vec_info_type;
6790 : 409566 : DUMP_VECT_SCOPE ("vectorizable_operation");
6791 : 409566 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6792 : 409566 : if (using_emulated_vectors_p)
6793 : : {
6794 : : /* The above vect_model_simple_cost call handles constants
6795 : : in the prologue and (mis-)costs one of the stmts as
6796 : : vector stmt. See below for the actual lowering that will
6797 : : be applied. */
6798 : 756 : unsigned n = vect_get_num_copies (vinfo, slp_node);
6799 : 756 : switch (code)
6800 : : {
6801 : 263 : case PLUS_EXPR:
6802 : 263 : n *= 5;
6803 : 263 : break;
6804 : 458 : case MINUS_EXPR:
6805 : 458 : n *= 6;
6806 : 458 : break;
6807 : 0 : case NEGATE_EXPR:
6808 : 0 : n *= 4;
6809 : 0 : break;
6810 : : default:
6811 : : /* Bit operations do not have extra cost and are accounted
6812 : : as vector stmt by vect_model_simple_cost. */
6813 : : n = 0;
6814 : : break;
6815 : : }
6816 : 721 : if (n != 0)
6817 : : {
6818 : : /* We also need to materialize two large constants. */
6819 : 721 : record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
6820 : : 0, vect_prologue);
6821 : 721 : record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
6822 : : 0, vect_body);
6823 : : }
6824 : : }
6825 : 409566 : return true;
6826 : : }
6827 : :
6828 : : /* Transform. */
6829 : :
6830 : 116349 : if (dump_enabled_p ())
6831 : 16649 : dump_printf_loc (MSG_NOTE, vect_location,
6832 : : "transform binary/unary operation.\n");
6833 : :
6834 : 116349 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
6835 : 105125 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
6836 : :
6837 : : /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
6838 : : vectors with unsigned elements, but the result is signed. So, we
6839 : : need to compute the MINUS_EXPR into vectype temporary and
6840 : : VIEW_CONVERT_EXPR it into the final vectype_out result. */
6841 : 116349 : tree vec_cvt_dest = NULL_TREE;
6842 : 116349 : if (orig_code == POINTER_DIFF_EXPR)
6843 : : {
6844 : 131 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6845 : 131 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6846 : : }
6847 : : /* For reduction operations with undefined overflow behavior make sure to
6848 : : pun them to unsigned since we change the order of evaluation.
6849 : : ??? Avoid for in-order reductions? */
6850 : 116218 : else if (arith_code_with_undefined_signed_overflow (orig_code)
6851 : 99984 : && ANY_INTEGRAL_TYPE_P (vectype)
6852 : 50263 : && TYPE_OVERFLOW_UNDEFINED (vectype)
6853 : 143296 : && SLP_TREE_REDUC_IDX (slp_node) != -1)
6854 : : {
6855 : 2383 : gcc_assert (orig_code == PLUS_EXPR || orig_code == MINUS_EXPR
6856 : : || orig_code == MULT_EXPR || orig_code == POINTER_PLUS_EXPR);
6857 : 2383 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6858 : 2383 : vectype = unsigned_type_for (vectype);
6859 : 2383 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6860 : : }
6861 : : /* Handle def. */
6862 : : else
6863 : 113835 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
6864 : :
6865 : 116349 : vect_get_vec_defs (vinfo, slp_node,
6866 : : op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
6867 : : /* Arguments are ready. Create the new vector stmt. */
6868 : 257826 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6869 : : {
6870 : 141477 : gimple *new_stmt = NULL;
6871 : 282954 : vop1 = ((op_type == binary_op || op_type == ternary_op)
6872 : 141477 : ? vec_oprnds1[i] : NULL_TREE);
6873 : 141477 : vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
6874 : :
6875 : 141477 : if (vec_cvt_dest
6876 : 141477 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop0)))
6877 : : {
6878 : 2764 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop0);
6879 : 2764 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6880 : : new_temp);
6881 : 2764 : new_temp = make_ssa_name (vec_dest, new_stmt);
6882 : 2764 : gimple_assign_set_lhs (new_stmt, new_temp);
6883 : 2764 : vect_finish_stmt_generation (vinfo, stmt_info,
6884 : : new_stmt, gsi);
6885 : 2764 : vop0 = new_temp;
6886 : : }
6887 : 141477 : if (vop1
6888 : 138962 : && vec_cvt_dest
6889 : 144387 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop1)))
6890 : : {
6891 : 2764 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop1);
6892 : 2764 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6893 : : new_temp);
6894 : 2764 : new_temp = make_ssa_name (vec_dest, new_stmt);
6895 : 2764 : gimple_assign_set_lhs (new_stmt, new_temp);
6896 : 2764 : vect_finish_stmt_generation (vinfo, stmt_info,
6897 : : new_stmt, gsi);
6898 : 2764 : vop1 = new_temp;
6899 : : }
6900 : 141477 : if (vop2
6901 : 0 : && vec_cvt_dest
6902 : 141477 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop2)))
6903 : : {
6904 : 0 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop2);
6905 : 0 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6906 : : new_temp);
6907 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6908 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6909 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
6910 : : new_stmt, gsi);
6911 : 0 : vop2 = new_temp;
6912 : : }
6913 : :
6914 : 141477 : if (using_emulated_vectors_p)
6915 : : {
6916 : : /* Lower the operation. This follows vector lowering. */
6917 : 2 : tree word_type = build_nonstandard_integer_type
6918 : 2 : (GET_MODE_BITSIZE (vec_mode).to_constant (), 1);
6919 : 2 : tree wvop0 = make_ssa_name (word_type);
6920 : 2 : new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
6921 : : build1 (VIEW_CONVERT_EXPR,
6922 : : word_type, vop0));
6923 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6924 : 2 : tree wvop1 = NULL_TREE;
6925 : 2 : if (vop1)
6926 : : {
6927 : 2 : wvop1 = make_ssa_name (word_type);
6928 : 2 : new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
6929 : : build1 (VIEW_CONVERT_EXPR,
6930 : : word_type, vop1));
6931 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6932 : : }
6933 : :
6934 : 2 : tree result_low;
6935 : 2 : if (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6936 : : {
6937 : 1 : unsigned int width = vector_element_bits (vectype);
6938 : 1 : tree inner_type = TREE_TYPE (vectype);
6939 : 1 : HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
6940 : 1 : tree low_bits
6941 : 1 : = build_replicated_int_cst (word_type, width, max >> 1);
6942 : 1 : tree high_bits
6943 : 2 : = build_replicated_int_cst (word_type,
6944 : 1 : width, max & ~(max >> 1));
6945 : 1 : tree signs;
6946 : 1 : if (code == PLUS_EXPR || code == MINUS_EXPR)
6947 : : {
6948 : 1 : signs = make_ssa_name (word_type);
6949 : 1 : new_stmt = gimple_build_assign (signs,
6950 : : BIT_XOR_EXPR, wvop0, wvop1);
6951 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6952 : 1 : tree b_low = make_ssa_name (word_type);
6953 : 1 : new_stmt = gimple_build_assign (b_low, BIT_AND_EXPR,
6954 : : wvop1, low_bits);
6955 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6956 : 1 : tree a_low = make_ssa_name (word_type);
6957 : 1 : if (code == PLUS_EXPR)
6958 : 1 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
6959 : : wvop0, low_bits);
6960 : : else
6961 : 0 : new_stmt = gimple_build_assign (a_low, BIT_IOR_EXPR,
6962 : : wvop0, high_bits);
6963 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6964 : 1 : if (code == MINUS_EXPR)
6965 : : {
6966 : 0 : new_stmt = gimple_build_assign (NULL_TREE,
6967 : : BIT_NOT_EXPR, signs);
6968 : 0 : signs = make_ssa_name (word_type);
6969 : 0 : gimple_assign_set_lhs (new_stmt, signs);
6970 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
6971 : : new_stmt, gsi);
6972 : : }
6973 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
6974 : : signs, high_bits);
6975 : 1 : signs = make_ssa_name (word_type);
6976 : 1 : gimple_assign_set_lhs (new_stmt, signs);
6977 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6978 : 1 : result_low = make_ssa_name (word_type);
6979 : 1 : new_stmt = gimple_build_assign (result_low, code,
6980 : : a_low, b_low);
6981 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6982 : : }
6983 : : else /* if (code == NEGATE_EXPR) */
6984 : : {
6985 : 0 : tree a_low = make_ssa_name (word_type);
6986 : 0 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
6987 : : wvop0, low_bits);
6988 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6989 : 0 : signs = make_ssa_name (word_type);
6990 : 0 : new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
6991 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6992 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
6993 : : signs, high_bits);
6994 : 0 : signs = make_ssa_name (word_type);
6995 : 0 : gimple_assign_set_lhs (new_stmt, signs);
6996 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6997 : 0 : result_low = make_ssa_name (word_type);
6998 : 0 : new_stmt = gimple_build_assign (result_low,
6999 : : MINUS_EXPR, high_bits, a_low);
7000 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7001 : : }
7002 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR,
7003 : : result_low, signs);
7004 : 1 : result_low = make_ssa_name (word_type);
7005 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
7006 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7007 : : }
7008 : : else
7009 : : {
7010 : 1 : new_stmt = gimple_build_assign (NULL_TREE, code, wvop0, wvop1);
7011 : 1 : result_low = make_ssa_name (word_type);
7012 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
7013 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7014 : :
7015 : : }
7016 : 2 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
7017 : : build1 (VIEW_CONVERT_EXPR,
7018 : : vectype, result_low));
7019 : 2 : new_temp = make_ssa_name (vectype);
7020 : 2 : gimple_assign_set_lhs (new_stmt, new_temp);
7021 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7022 : : }
7023 : 141475 : else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
7024 : : {
7025 : 16 : tree mask;
7026 : 16 : if (masked_loop_p)
7027 : 16 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7028 : : vec_num, vectype, i);
7029 : : else
7030 : : /* Dummy mask. */
7031 : 0 : mask = build_minus_one_cst (truth_type_for (vectype));
7032 : 16 : auto_vec<tree> vops (6);
7033 : 16 : vops.quick_push (mask);
7034 : 16 : vops.quick_push (vop0);
7035 : 16 : if (vop1)
7036 : 16 : vops.quick_push (vop1);
7037 : 16 : if (vop2)
7038 : 0 : vops.quick_push (vop2);
7039 : 16 : if (reduc_idx >= 0)
7040 : : {
7041 : : /* Perform the operation on active elements only and take
7042 : : inactive elements from the reduction chain input. */
7043 : 8 : gcc_assert (!vop2);
7044 : 8 : vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
7045 : : }
7046 : : else
7047 : : {
7048 : 8 : auto else_value = targetm.preferred_else_value
7049 : 8 : (cond_fn, vectype, vops.length () - 1, &vops[1]);
7050 : 8 : vops.quick_push (else_value);
7051 : : }
7052 : 16 : if (len_loop_p)
7053 : : {
7054 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
7055 : 0 : vec_num, vectype, i, 1);
7056 : 0 : signed char biasval
7057 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
7058 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
7059 : 0 : vops.quick_push (len);
7060 : 0 : vops.quick_push (bias);
7061 : : }
7062 : 16 : gcall *call
7063 : 16 : = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
7064 : : : cond_len_fn,
7065 : : vops);
7066 : 16 : new_temp = make_ssa_name (vec_dest, call);
7067 : 16 : gimple_call_set_lhs (call, new_temp);
7068 : 16 : gimple_call_set_nothrow (call, true);
7069 : 16 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
7070 : 16 : new_stmt = call;
7071 : 16 : }
7072 : : else
7073 : : {
7074 : 141459 : tree mask = NULL_TREE;
7075 : : /* When combining two masks check if either of them is elsewhere
7076 : : combined with a loop mask, if that's the case we can mark that the
7077 : : new combined mask doesn't need to be combined with a loop mask. */
7078 : 141459 : if (masked_loop_p
7079 : 141459 : && code == BIT_AND_EXPR
7080 : 141459 : && VECTOR_BOOLEAN_TYPE_P (vectype))
7081 : : {
7082 : 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op0, vec_num }))
7083 : : {
7084 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7085 : : vec_num, vectype, i);
7086 : :
7087 : 0 : vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7088 : : vop0, gsi);
7089 : : }
7090 : :
7091 : 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op1, vec_num }))
7092 : : {
7093 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7094 : : vec_num, vectype, i);
7095 : :
7096 : 0 : vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7097 : : vop1, gsi);
7098 : : }
7099 : : }
7100 : :
7101 : 141459 : new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
7102 : 141459 : new_temp = make_ssa_name (vec_dest, new_stmt);
7103 : 141459 : gimple_assign_set_lhs (new_stmt, new_temp);
7104 : 141459 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7105 : 141459 : if (using_emulated_vectors_p)
7106 : : suppress_warning (new_stmt, OPT_Wvector_operation_performance);
7107 : :
7108 : : /* Enter the combined value into the vector cond hash so we don't
7109 : : AND it with a loop mask again. */
7110 : 141459 : if (mask)
7111 : 0 : loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
7112 : : }
7113 : :
7114 : 141477 : if (vec_cvt_dest)
7115 : : {
7116 : 2910 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
7117 : 2910 : new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
7118 : : new_temp);
7119 : 2910 : new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
7120 : 2910 : gimple_assign_set_lhs (new_stmt, new_temp);
7121 : 2910 : vect_finish_stmt_generation (vinfo, stmt_info,
7122 : : new_stmt, gsi);
7123 : : }
7124 : :
7125 : 141477 : slp_node->push_vec_def (new_stmt);
7126 : : }
7127 : :
7128 : 116349 : vec_oprnds0.release ();
7129 : 116349 : vec_oprnds1.release ();
7130 : 116349 : vec_oprnds2.release ();
7131 : :
7132 : 116349 : return true;
7133 : : }
7134 : :
7135 : : /* A helper function to ensure data reference DR_INFO's base alignment. */
7136 : :
7137 : : static void
7138 : 1865166 : ensure_base_align (dr_vec_info *dr_info)
7139 : : {
7140 : : /* Alignment is only analyzed for the first element of a DR group,
7141 : : use that to look at base alignment we need to enforce. */
7142 : 1865166 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
7143 : 1424472 : dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
7144 : :
7145 : 1865166 : gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
7146 : :
7147 : 1865166 : if (dr_info->base_misaligned)
7148 : : {
7149 : 170171 : tree base_decl = dr_info->base_decl;
7150 : :
7151 : : // We should only be able to increase the alignment of a base object if
7152 : : // we know what its new alignment should be at compile time.
7153 : 170171 : unsigned HOST_WIDE_INT align_base_to =
7154 : 170171 : DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
7155 : :
7156 : 170171 : if (decl_in_symtab_p (base_decl))
7157 : 4649 : symtab_node::get (base_decl)->increase_alignment (align_base_to);
7158 : 165522 : else if (DECL_ALIGN (base_decl) < align_base_to)
7159 : : {
7160 : 132322 : SET_DECL_ALIGN (base_decl, align_base_to);
7161 : 132322 : DECL_USER_ALIGN (base_decl) = 1;
7162 : : }
7163 : 170171 : dr_info->base_misaligned = false;
7164 : : }
7165 : 1865166 : }
7166 : :
7167 : :
7168 : : /* Function get_group_alias_ptr_type.
7169 : :
7170 : : Return the alias type for the group starting at FIRST_STMT_INFO. */
7171 : :
7172 : : static tree
7173 : 1595036 : get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
7174 : : {
7175 : 1595036 : struct data_reference *first_dr, *next_dr;
7176 : :
7177 : 1595036 : first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
7178 : 1595036 : stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
7179 : 3842806 : while (next_stmt_info)
7180 : : {
7181 : 2396818 : next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
7182 : 4793636 : if (get_alias_set (DR_REF (first_dr))
7183 : 2396818 : != get_alias_set (DR_REF (next_dr)))
7184 : : {
7185 : 149048 : if (dump_enabled_p ())
7186 : 27 : dump_printf_loc (MSG_NOTE, vect_location,
7187 : : "conflicting alias set types.\n");
7188 : 149048 : return ptr_type_node;
7189 : : }
7190 : 2247770 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7191 : : }
7192 : 1445988 : return reference_alias_ptr_type (DR_REF (first_dr));
7193 : : }
7194 : :
7195 : :
7196 : : /* Function scan_operand_equal_p.
7197 : :
7198 : : Helper function for check_scan_store. Compare two references
7199 : : with .GOMP_SIMD_LANE bases. */
7200 : :
7201 : : static bool
7202 : 1284 : scan_operand_equal_p (tree ref1, tree ref2)
7203 : : {
7204 : 1284 : tree ref[2] = { ref1, ref2 };
7205 : 1284 : poly_int64 bitsize[2], bitpos[2];
7206 : : tree offset[2], base[2];
7207 : 3852 : for (int i = 0; i < 2; ++i)
7208 : : {
7209 : 2568 : machine_mode mode;
7210 : 2568 : int unsignedp, reversep, volatilep = 0;
7211 : 2568 : base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
7212 : : &offset[i], &mode, &unsignedp,
7213 : : &reversep, &volatilep);
7214 : 2568 : if (reversep || volatilep || maybe_ne (bitpos[i], 0))
7215 : 0 : return false;
7216 : 2568 : if (TREE_CODE (base[i]) == MEM_REF
7217 : 42 : && offset[i] == NULL_TREE
7218 : 2610 : && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
7219 : : {
7220 : 42 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
7221 : 42 : if (is_gimple_assign (def_stmt)
7222 : 42 : && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
7223 : 42 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
7224 : 84 : && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
7225 : : {
7226 : 42 : if (maybe_ne (mem_ref_offset (base[i]), 0))
7227 : : return false;
7228 : 42 : base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
7229 : 42 : offset[i] = gimple_assign_rhs2 (def_stmt);
7230 : : }
7231 : : }
7232 : : }
7233 : :
7234 : 1284 : if (!operand_equal_p (base[0], base[1], 0))
7235 : : return false;
7236 : 934 : if (maybe_ne (bitsize[0], bitsize[1]))
7237 : : return false;
7238 : 934 : if (offset[0] != offset[1])
7239 : : {
7240 : 916 : if (!offset[0] || !offset[1])
7241 : : return false;
7242 : 916 : if (!operand_equal_p (offset[0], offset[1], 0))
7243 : : {
7244 : : tree step[2];
7245 : 0 : for (int i = 0; i < 2; ++i)
7246 : : {
7247 : 0 : step[i] = integer_one_node;
7248 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7249 : : {
7250 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7251 : 0 : if (is_gimple_assign (def_stmt)
7252 : 0 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
7253 : 0 : && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
7254 : : == INTEGER_CST))
7255 : : {
7256 : 0 : step[i] = gimple_assign_rhs2 (def_stmt);
7257 : 0 : offset[i] = gimple_assign_rhs1 (def_stmt);
7258 : : }
7259 : : }
7260 : 0 : else if (TREE_CODE (offset[i]) == MULT_EXPR)
7261 : : {
7262 : 0 : step[i] = TREE_OPERAND (offset[i], 1);
7263 : 0 : offset[i] = TREE_OPERAND (offset[i], 0);
7264 : : }
7265 : 0 : tree rhs1 = NULL_TREE;
7266 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7267 : : {
7268 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7269 : 0 : if (gimple_assign_cast_p (def_stmt))
7270 : 0 : rhs1 = gimple_assign_rhs1 (def_stmt);
7271 : : }
7272 : 0 : else if (CONVERT_EXPR_P (offset[i]))
7273 : 0 : rhs1 = TREE_OPERAND (offset[i], 0);
7274 : 0 : if (rhs1
7275 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
7276 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
7277 : 0 : && (TYPE_PRECISION (TREE_TYPE (offset[i]))
7278 : 0 : >= TYPE_PRECISION (TREE_TYPE (rhs1))))
7279 : 0 : offset[i] = rhs1;
7280 : : }
7281 : 0 : if (!operand_equal_p (offset[0], offset[1], 0)
7282 : 0 : || !operand_equal_p (step[0], step[1], 0))
7283 : 0 : return false;
7284 : : }
7285 : : }
7286 : : return true;
7287 : : }
7288 : :
7289 : :
7290 : : enum scan_store_kind {
7291 : : /* Normal permutation. */
7292 : : scan_store_kind_perm,
7293 : :
7294 : : /* Whole vector left shift permutation with zero init. */
7295 : : scan_store_kind_lshift_zero,
7296 : :
7297 : : /* Whole vector left shift permutation and VEC_COND_EXPR. */
7298 : : scan_store_kind_lshift_cond
7299 : : };
7300 : :
7301 : : /* Function check_scan_store.
7302 : :
7303 : : Verify if we can perform the needed permutations or whole vector shifts.
7304 : : Return -1 on failure, otherwise exact log2 of vectype's nunits.
7305 : : USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
7306 : : to do at each step. */
7307 : :
7308 : : static int
7309 : 1024 : scan_store_can_perm_p (tree vectype, tree init,
7310 : : vec<enum scan_store_kind> *use_whole_vector = NULL)
7311 : : {
7312 : 1024 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7313 : 1024 : unsigned HOST_WIDE_INT nunits;
7314 : 1024 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7315 : : return -1;
7316 : 1024 : int units_log2 = exact_log2 (nunits);
7317 : 1024 : if (units_log2 <= 0)
7318 : : return -1;
7319 : :
7320 : : int i;
7321 : : enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
7322 : 4784 : for (i = 0; i <= units_log2; ++i)
7323 : : {
7324 : 3760 : unsigned HOST_WIDE_INT j, k;
7325 : 3760 : enum scan_store_kind kind = scan_store_kind_perm;
7326 : 3760 : vec_perm_builder sel (nunits, nunits, 1);
7327 : 3760 : sel.quick_grow (nunits);
7328 : 3760 : if (i == units_log2)
7329 : : {
7330 : 9728 : for (j = 0; j < nunits; ++j)
7331 : 8704 : sel[j] = nunits - 1;
7332 : : }
7333 : : else
7334 : : {
7335 : 10416 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7336 : 7680 : sel[j] = j;
7337 : 26416 : for (k = 0; j < nunits; ++j, ++k)
7338 : 23680 : sel[j] = nunits + k;
7339 : : }
7340 : 6496 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7341 : 3760 : if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
7342 : : {
7343 : 0 : if (i == units_log2)
7344 : : return -1;
7345 : :
7346 : 0 : if (whole_vector_shift_kind == scan_store_kind_perm)
7347 : : {
7348 : 0 : if (!can_implement_p (vec_shl_optab, vec_mode))
7349 : : return -1;
7350 : 0 : whole_vector_shift_kind = scan_store_kind_lshift_zero;
7351 : : /* Whole vector shifts shift in zeros, so if init is all zero
7352 : : constant, there is no need to do anything further. */
7353 : 0 : if ((TREE_CODE (init) != INTEGER_CST
7354 : 0 : && TREE_CODE (init) != REAL_CST)
7355 : 0 : || !initializer_zerop (init))
7356 : : {
7357 : 0 : tree masktype = truth_type_for (vectype);
7358 : 0 : if (!expand_vec_cond_expr_p (vectype, masktype))
7359 : : return -1;
7360 : : whole_vector_shift_kind = scan_store_kind_lshift_cond;
7361 : : }
7362 : : }
7363 : 0 : kind = whole_vector_shift_kind;
7364 : : }
7365 : 3760 : if (use_whole_vector)
7366 : : {
7367 : 1880 : if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
7368 : 0 : use_whole_vector->safe_grow_cleared (i, true);
7369 : 5640 : if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
7370 : 0 : use_whole_vector->safe_push (kind);
7371 : : }
7372 : 3760 : }
7373 : :
7374 : : return units_log2;
7375 : : }
7376 : :
7377 : :
7378 : : /* Function check_scan_store.
7379 : :
7380 : : Check magic stores for #pragma omp scan {in,ex}clusive reductions. */
7381 : :
7382 : : static bool
7383 : 1076 : check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
7384 : : enum vect_def_type rhs_dt, slp_tree slp_node,
7385 : : slp_tree mask_node,
7386 : : vect_memory_access_type memory_access_type)
7387 : : {
7388 : 1076 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7389 : 1076 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7390 : 1076 : tree ref_type;
7391 : :
7392 : 1076 : gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
7393 : 1076 : if (SLP_TREE_LANES (slp_node) > 1
7394 : 1076 : || mask_node
7395 : 1076 : || memory_access_type != VMAT_CONTIGUOUS
7396 : 1076 : || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
7397 : 1076 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
7398 : 1076 : || loop_vinfo == NULL
7399 : 1076 : || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7400 : 1076 : || LOOP_VINFO_EPILOGUE_P (loop_vinfo)
7401 : 1076 : || STMT_VINFO_GROUPED_ACCESS (stmt_info)
7402 : 1076 : || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
7403 : 1076 : || !integer_zerop (DR_INIT (dr_info->dr))
7404 : 1076 : || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
7405 : 2152 : || !alias_sets_conflict_p (get_alias_set (vectype),
7406 : 1076 : get_alias_set (TREE_TYPE (ref_type))))
7407 : : {
7408 : 0 : if (dump_enabled_p ())
7409 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7410 : : "unsupported OpenMP scan store.\n");
7411 : 0 : return false;
7412 : : }
7413 : :
7414 : : /* We need to pattern match code built by OpenMP lowering and simplified
7415 : : by following optimizations into something we can handle.
7416 : : #pragma omp simd reduction(inscan,+:r)
7417 : : for (...)
7418 : : {
7419 : : r += something ();
7420 : : #pragma omp scan inclusive (r)
7421 : : use (r);
7422 : : }
7423 : : shall have body with:
7424 : : // Initialization for input phase, store the reduction initializer:
7425 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7426 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7427 : : D.2042[_21] = 0;
7428 : : // Actual input phase:
7429 : : ...
7430 : : r.0_5 = D.2042[_20];
7431 : : _6 = _4 + r.0_5;
7432 : : D.2042[_20] = _6;
7433 : : // Initialization for scan phase:
7434 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
7435 : : _26 = D.2043[_25];
7436 : : _27 = D.2042[_25];
7437 : : _28 = _26 + _27;
7438 : : D.2043[_25] = _28;
7439 : : D.2042[_25] = _28;
7440 : : // Actual scan phase:
7441 : : ...
7442 : : r.1_8 = D.2042[_20];
7443 : : ...
7444 : : The "omp simd array" variable D.2042 holds the privatized copy used
7445 : : inside of the loop and D.2043 is another one that holds copies of
7446 : : the current original list item. The separate GOMP_SIMD_LANE ifn
7447 : : kinds are there in order to allow optimizing the initializer store
7448 : : and combiner sequence, e.g. if it is originally some C++ish user
7449 : : defined reduction, but allow the vectorizer to pattern recognize it
7450 : : and turn into the appropriate vectorized scan.
7451 : :
7452 : : For exclusive scan, this is slightly different:
7453 : : #pragma omp simd reduction(inscan,+:r)
7454 : : for (...)
7455 : : {
7456 : : use (r);
7457 : : #pragma omp scan exclusive (r)
7458 : : r += something ();
7459 : : }
7460 : : shall have body with:
7461 : : // Initialization for input phase, store the reduction initializer:
7462 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7463 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7464 : : D.2042[_21] = 0;
7465 : : // Actual input phase:
7466 : : ...
7467 : : r.0_5 = D.2042[_20];
7468 : : _6 = _4 + r.0_5;
7469 : : D.2042[_20] = _6;
7470 : : // Initialization for scan phase:
7471 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
7472 : : _26 = D.2043[_25];
7473 : : D.2044[_25] = _26;
7474 : : _27 = D.2042[_25];
7475 : : _28 = _26 + _27;
7476 : : D.2043[_25] = _28;
7477 : : // Actual scan phase:
7478 : : ...
7479 : : r.1_8 = D.2044[_20];
7480 : : ... */
7481 : :
7482 : 1076 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
7483 : : {
7484 : : /* Match the D.2042[_21] = 0; store above. Just require that
7485 : : it is a constant or external definition store. */
7486 : 564 : if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
7487 : : {
7488 : 0 : fail_init:
7489 : 0 : if (dump_enabled_p ())
7490 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7491 : : "unsupported OpenMP scan initializer store.\n");
7492 : 0 : return false;
7493 : : }
7494 : :
7495 : 564 : if (! loop_vinfo->scan_map)
7496 : 322 : loop_vinfo->scan_map = new hash_map<tree, tree>;
7497 : 564 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7498 : 564 : tree &cached = loop_vinfo->scan_map->get_or_insert (var);
7499 : 564 : if (cached)
7500 : 0 : goto fail_init;
7501 : 564 : cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
7502 : :
7503 : : /* These stores can be vectorized normally. */
7504 : 564 : return true;
7505 : : }
7506 : :
7507 : 512 : if (rhs_dt != vect_internal_def)
7508 : : {
7509 : 0 : fail:
7510 : 0 : if (dump_enabled_p ())
7511 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7512 : : "unsupported OpenMP scan combiner pattern.\n");
7513 : 0 : return false;
7514 : : }
7515 : :
7516 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7517 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7518 : 512 : if (TREE_CODE (rhs) != SSA_NAME)
7519 : 0 : goto fail;
7520 : :
7521 : 512 : gimple *other_store_stmt = NULL;
7522 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7523 : 512 : bool inscan_var_store
7524 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7525 : :
7526 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7527 : : {
7528 : 252 : if (!inscan_var_store)
7529 : : {
7530 : 126 : use_operand_p use_p;
7531 : 126 : imm_use_iterator iter;
7532 : 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7533 : : {
7534 : 252 : gimple *use_stmt = USE_STMT (use_p);
7535 : 252 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7536 : 126 : continue;
7537 : 126 : if (gimple_bb (use_stmt) != gimple_bb (stmt)
7538 : 126 : || !is_gimple_assign (use_stmt)
7539 : 126 : || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
7540 : 126 : || other_store_stmt
7541 : 252 : || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
7542 : 0 : goto fail;
7543 : 126 : other_store_stmt = use_stmt;
7544 : 0 : }
7545 : 126 : if (other_store_stmt == NULL)
7546 : 0 : goto fail;
7547 : 126 : rhs = gimple_assign_lhs (other_store_stmt);
7548 : 126 : if (!single_imm_use (rhs, &use_p, &other_store_stmt))
7549 : 0 : goto fail;
7550 : : }
7551 : : }
7552 : 260 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
7553 : : {
7554 : 260 : use_operand_p use_p;
7555 : 260 : imm_use_iterator iter;
7556 : 1040 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7557 : : {
7558 : 520 : gimple *use_stmt = USE_STMT (use_p);
7559 : 520 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7560 : 260 : continue;
7561 : 260 : if (other_store_stmt)
7562 : 0 : goto fail;
7563 : 260 : other_store_stmt = use_stmt;
7564 : 260 : }
7565 : : }
7566 : : else
7567 : 0 : goto fail;
7568 : :
7569 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7570 : 512 : if (gimple_bb (def_stmt) != gimple_bb (stmt)
7571 : 512 : || !is_gimple_assign (def_stmt)
7572 : 1024 : || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
7573 : 0 : goto fail;
7574 : :
7575 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7576 : : /* For pointer addition, we should use the normal plus for the vector
7577 : : operation. */
7578 : 512 : switch (code)
7579 : : {
7580 : 0 : case POINTER_PLUS_EXPR:
7581 : 0 : code = PLUS_EXPR;
7582 : 0 : break;
7583 : 0 : case MULT_HIGHPART_EXPR:
7584 : 0 : goto fail;
7585 : : default:
7586 : : break;
7587 : : }
7588 : 512 : if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
7589 : 0 : goto fail;
7590 : :
7591 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7592 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7593 : 512 : if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
7594 : 0 : goto fail;
7595 : :
7596 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7597 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7598 : 512 : if (gimple_bb (load1_stmt) != gimple_bb (stmt)
7599 : 512 : || !gimple_assign_load_p (load1_stmt)
7600 : 512 : || gimple_bb (load2_stmt) != gimple_bb (stmt)
7601 : 1024 : || !gimple_assign_load_p (load2_stmt))
7602 : 0 : goto fail;
7603 : :
7604 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7605 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7606 : 512 : if (load1_stmt_info == NULL
7607 : 512 : || load2_stmt_info == NULL
7608 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
7609 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
7610 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
7611 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7612 : 0 : goto fail;
7613 : :
7614 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
7615 : : {
7616 : 126 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7617 : 126 : if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
7618 : 126 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
7619 : 0 : goto fail;
7620 : 126 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7621 : 126 : tree lrhs;
7622 : 126 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7623 : : lrhs = rhs1;
7624 : : else
7625 : 16 : lrhs = rhs2;
7626 : 126 : use_operand_p use_p;
7627 : 126 : imm_use_iterator iter;
7628 : 504 : FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
7629 : : {
7630 : 252 : gimple *use_stmt = USE_STMT (use_p);
7631 : 252 : if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
7632 : 126 : continue;
7633 : 126 : if (other_store_stmt)
7634 : 0 : goto fail;
7635 : 126 : other_store_stmt = use_stmt;
7636 : 126 : }
7637 : : }
7638 : :
7639 : 512 : if (other_store_stmt == NULL)
7640 : 0 : goto fail;
7641 : 512 : if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
7642 : 512 : || !gimple_store_p (other_store_stmt))
7643 : 0 : goto fail;
7644 : :
7645 : 512 : stmt_vec_info other_store_stmt_info
7646 : 512 : = loop_vinfo->lookup_stmt (other_store_stmt);
7647 : 512 : if (other_store_stmt_info == NULL
7648 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
7649 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7650 : 0 : goto fail;
7651 : :
7652 : 512 : gimple *stmt1 = stmt;
7653 : 512 : gimple *stmt2 = other_store_stmt;
7654 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7655 : : std::swap (stmt1, stmt2);
7656 : 512 : if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
7657 : : gimple_assign_rhs1 (load2_stmt)))
7658 : : {
7659 : 162 : std::swap (rhs1, rhs2);
7660 : 162 : std::swap (load1_stmt, load2_stmt);
7661 : 162 : std::swap (load1_stmt_info, load2_stmt_info);
7662 : : }
7663 : 512 : if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
7664 : : gimple_assign_rhs1 (load1_stmt)))
7665 : 0 : goto fail;
7666 : :
7667 : 512 : tree var3 = NULL_TREE;
7668 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
7669 : 512 : && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
7670 : : gimple_assign_rhs1 (load2_stmt)))
7671 : 0 : goto fail;
7672 : 512 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7673 : : {
7674 : 252 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7675 : 252 : if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
7676 : 252 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
7677 : 0 : goto fail;
7678 : 252 : var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7679 : 252 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
7680 : 252 : || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
7681 : 504 : || lookup_attribute ("omp simd inscan exclusive",
7682 : 252 : DECL_ATTRIBUTES (var3)))
7683 : 0 : goto fail;
7684 : : }
7685 : :
7686 : 512 : dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
7687 : 512 : if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
7688 : 512 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
7689 : 0 : goto fail;
7690 : :
7691 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7692 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
7693 : 512 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
7694 : 512 : || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
7695 : 1024 : || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7696 : 512 : == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
7697 : 0 : goto fail;
7698 : :
7699 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7700 : 256 : std::swap (var1, var2);
7701 : :
7702 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7703 : : {
7704 : 252 : if (!lookup_attribute ("omp simd inscan exclusive",
7705 : 252 : DECL_ATTRIBUTES (var1)))
7706 : 0 : goto fail;
7707 : 252 : var1 = var3;
7708 : : }
7709 : :
7710 : 512 : if (loop_vinfo->scan_map == NULL)
7711 : 0 : goto fail;
7712 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7713 : 512 : if (init == NULL)
7714 : 0 : goto fail;
7715 : :
7716 : : /* The IL is as expected, now check if we can actually vectorize it.
7717 : : Inclusive scan:
7718 : : _26 = D.2043[_25];
7719 : : _27 = D.2042[_25];
7720 : : _28 = _26 + _27;
7721 : : D.2043[_25] = _28;
7722 : : D.2042[_25] = _28;
7723 : : should be vectorized as (where _40 is the vectorized rhs
7724 : : from the D.2042[_21] = 0; store):
7725 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7726 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7727 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7728 : : _33 = _31 + _32;
7729 : : // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
7730 : : _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7731 : : _35 = _33 + _34;
7732 : : // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7733 : : // _31[1]+.._31[4], ... _31[4]+.._31[7] };
7734 : : _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7735 : : _37 = _35 + _36;
7736 : : // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7737 : : // _31[0]+.._31[4], ... _31[0]+.._31[7] };
7738 : : _38 = _30 + _37;
7739 : : _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7740 : : MEM <vector(8) int> [(int *)&D.2043] = _39;
7741 : : MEM <vector(8) int> [(int *)&D.2042] = _38;
7742 : : Exclusive scan:
7743 : : _26 = D.2043[_25];
7744 : : D.2044[_25] = _26;
7745 : : _27 = D.2042[_25];
7746 : : _28 = _26 + _27;
7747 : : D.2043[_25] = _28;
7748 : : should be vectorized as (where _40 is the vectorized rhs
7749 : : from the D.2042[_21] = 0; store):
7750 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7751 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7752 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7753 : : _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7754 : : _34 = _32 + _33;
7755 : : // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
7756 : : // _31[3]+_31[4], ... _31[5]+.._31[6] };
7757 : : _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7758 : : _36 = _34 + _35;
7759 : : // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7760 : : // _31[1]+.._31[4], ... _31[3]+.._31[6] };
7761 : : _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7762 : : _38 = _36 + _37;
7763 : : // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7764 : : // _31[0]+.._31[4], ... _31[0]+.._31[6] };
7765 : : _39 = _30 + _38;
7766 : : _50 = _31 + _39;
7767 : : _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7768 : : MEM <vector(8) int> [(int *)&D.2044] = _39;
7769 : : MEM <vector(8) int> [(int *)&D.2042] = _51; */
7770 : 512 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7771 : 512 : optab optab = optab_for_tree_code (code, vectype, optab_default);
7772 : 512 : if (!optab || !can_implement_p (optab, vec_mode))
7773 : 0 : goto fail;
7774 : :
7775 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init);
7776 : 512 : if (units_log2 == -1)
7777 : 0 : goto fail;
7778 : :
7779 : : return true;
7780 : : }
7781 : :
7782 : :
7783 : : /* Function vectorizable_scan_store.
7784 : :
7785 : : Helper of vectorizable_score, arguments like on vectorizable_store.
7786 : : Handle only the transformation, checking is done in check_scan_store. */
7787 : :
7788 : : static bool
7789 : 512 : vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
7790 : : slp_tree slp_node, gimple_stmt_iterator *gsi)
7791 : : {
7792 : 512 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7793 : 512 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7794 : 512 : tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
7795 : 512 : tree vectype = SLP_TREE_VECTYPE (slp_node);
7796 : :
7797 : 512 : if (dump_enabled_p ())
7798 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
7799 : : "transform scan store.\n");
7800 : :
7801 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7802 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7803 : 512 : gcc_assert (TREE_CODE (rhs) == SSA_NAME);
7804 : :
7805 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7806 : 512 : bool inscan_var_store
7807 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7808 : :
7809 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7810 : : {
7811 : 126 : use_operand_p use_p;
7812 : 126 : imm_use_iterator iter;
7813 : 252 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7814 : : {
7815 : 126 : gimple *use_stmt = USE_STMT (use_p);
7816 : 126 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7817 : 0 : continue;
7818 : 126 : rhs = gimple_assign_lhs (use_stmt);
7819 : 126 : break;
7820 : 126 : }
7821 : : }
7822 : :
7823 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7824 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7825 : 512 : if (code == POINTER_PLUS_EXPR)
7826 : 0 : code = PLUS_EXPR;
7827 : 512 : gcc_assert (TREE_CODE_LENGTH (code) == binary_op
7828 : : && commutative_tree_code (code));
7829 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7830 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7831 : 512 : gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
7832 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7833 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7834 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7835 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7836 : 512 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7837 : 512 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7838 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7839 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7840 : :
7841 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7842 : : {
7843 : 436 : std::swap (rhs1, rhs2);
7844 : 436 : std::swap (var1, var2);
7845 : 436 : std::swap (load1_dr_info, load2_dr_info);
7846 : : }
7847 : :
7848 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7849 : 512 : gcc_assert (init);
7850 : :
7851 : 512 : unsigned HOST_WIDE_INT nunits;
7852 : 512 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7853 : : gcc_unreachable ();
7854 : 512 : auto_vec<enum scan_store_kind, 16> use_whole_vector;
7855 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
7856 : 512 : gcc_assert (units_log2 > 0);
7857 : 512 : auto_vec<tree, 16> perms;
7858 : 512 : perms.quick_grow (units_log2 + 1);
7859 : 512 : tree zero_vec = NULL_TREE, masktype = NULL_TREE;
7860 : 2392 : for (int i = 0; i <= units_log2; ++i)
7861 : : {
7862 : 1880 : unsigned HOST_WIDE_INT j, k;
7863 : 1880 : vec_perm_builder sel (nunits, nunits, 1);
7864 : 1880 : sel.quick_grow (nunits);
7865 : 1880 : if (i == units_log2)
7866 : 4864 : for (j = 0; j < nunits; ++j)
7867 : 4352 : sel[j] = nunits - 1;
7868 : : else
7869 : : {
7870 : 5208 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7871 : 3840 : sel[j] = j;
7872 : 13208 : for (k = 0; j < nunits; ++j, ++k)
7873 : 11840 : sel[j] = nunits + k;
7874 : : }
7875 : 3248 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7876 : 1880 : if (!use_whole_vector.is_empty ()
7877 : 0 : && use_whole_vector[i] != scan_store_kind_perm)
7878 : : {
7879 : 0 : if (zero_vec == NULL_TREE)
7880 : 0 : zero_vec = build_zero_cst (vectype);
7881 : 0 : if (masktype == NULL_TREE
7882 : 0 : && use_whole_vector[i] == scan_store_kind_lshift_cond)
7883 : 0 : masktype = truth_type_for (vectype);
7884 : 0 : perms[i] = vect_gen_perm_mask_any (vectype, indices);
7885 : : }
7886 : : else
7887 : 1880 : perms[i] = vect_gen_perm_mask_checked (vectype, indices);
7888 : 1880 : }
7889 : :
7890 : 512 : tree vec_oprnd1 = NULL_TREE;
7891 : 512 : tree vec_oprnd2 = NULL_TREE;
7892 : 512 : tree vec_oprnd3 = NULL_TREE;
7893 : 512 : tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
7894 : 512 : tree dataref_offset = build_int_cst (ref_type, 0);
7895 : 512 : tree bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info,
7896 : : vectype, VMAT_CONTIGUOUS);
7897 : 512 : tree ldataref_ptr = NULL_TREE;
7898 : 512 : tree orig = NULL_TREE;
7899 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7900 : 126 : ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
7901 : : /* The initialization is invariant. */
7902 : 512 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
7903 : 512 : auto_vec<tree> vec_oprnds2;
7904 : 512 : auto_vec<tree> vec_oprnds3;
7905 : 512 : if (ldataref_ptr == NULL)
7906 : : {
7907 : : /* We want to lookup the vector operands of the reduction, not those
7908 : : of the store - for SLP we have to use the proper SLP node for the
7909 : : lookup, which should be the single child of the scan store. */
7910 : 386 : vect_get_vec_defs (vinfo, SLP_TREE_CHILDREN (slp_node)[0],
7911 : : rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
7912 : : /* ??? For SLP we do not key the def on 'rhs1' or 'rhs2' but get
7913 : : them in SLP child order. So we have to swap here with logic
7914 : : similar to above. */
7915 : 386 : stmt_vec_info load
7916 : 386 : = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
7917 : 386 : (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
7918 : 386 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
7919 : 386 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7920 : 386 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
7921 : 820 : for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
7922 : 494 : std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
7923 : : }
7924 : : else
7925 : 126 : vect_get_vec_defs (vinfo, slp_node,
7926 : : rhs2, &vec_oprnds3);
7927 : 1248 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
7928 : : {
7929 : 736 : if (ldataref_ptr == NULL)
7930 : 554 : vec_oprnd2 = vec_oprnds2[j];
7931 : 736 : vec_oprnd3 = vec_oprnds3[j];
7932 : 736 : if (j == 0)
7933 : : orig = vec_oprnd3;
7934 : 224 : else if (!inscan_var_store)
7935 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
7936 : :
7937 : 736 : if (ldataref_ptr)
7938 : : {
7939 : 182 : vec_oprnd2 = make_ssa_name (vectype);
7940 : 182 : tree data_ref = fold_build2 (MEM_REF, vectype,
7941 : : unshare_expr (ldataref_ptr),
7942 : : dataref_offset);
7943 : 182 : vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
7944 : 182 : gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
7945 : 182 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7946 : : }
7947 : :
7948 : 736 : tree v = vec_oprnd2;
7949 : 3068 : for (int i = 0; i < units_log2; ++i)
7950 : : {
7951 : 2332 : tree new_temp = make_ssa_name (vectype);
7952 : 2332 : gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
7953 : : (zero_vec
7954 : 0 : && (use_whole_vector[i]
7955 : 0 : != scan_store_kind_perm))
7956 : : ? zero_vec : vec_oprnd1, v,
7957 : 2332 : perms[i]);
7958 : 2332 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7959 : :
7960 : 2332 : if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
7961 : : {
7962 : : /* Whole vector shift shifted in zero bits, but if *init
7963 : : is not initializer_zerop, we need to replace those elements
7964 : : with elements from vec_oprnd1. */
7965 : 0 : tree_vector_builder vb (masktype, nunits, 1);
7966 : 0 : for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
7967 : 0 : vb.quick_push (k < (HOST_WIDE_INT_1U << i)
7968 : : ? boolean_false_node : boolean_true_node);
7969 : :
7970 : 0 : tree new_temp2 = make_ssa_name (vectype);
7971 : 0 : g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
7972 : : new_temp, vec_oprnd1);
7973 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
7974 : : g, gsi);
7975 : 0 : new_temp = new_temp2;
7976 : 0 : }
7977 : :
7978 : : /* For exclusive scan, perform the perms[i] permutation once
7979 : : more. */
7980 : 2332 : if (i == 0
7981 : 1100 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
7982 : 728 : && v == vec_oprnd2)
7983 : : {
7984 : 364 : v = new_temp;
7985 : 364 : --i;
7986 : 364 : continue;
7987 : : }
7988 : :
7989 : 1968 : tree new_temp2 = make_ssa_name (vectype);
7990 : 1968 : g = gimple_build_assign (new_temp2, code, v, new_temp);
7991 : 1968 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7992 : :
7993 : 1968 : v = new_temp2;
7994 : : }
7995 : :
7996 : 736 : tree new_temp = make_ssa_name (vectype);
7997 : 736 : gimple *g = gimple_build_assign (new_temp, code, orig, v);
7998 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7999 : :
8000 : 736 : tree last_perm_arg = new_temp;
8001 : : /* For exclusive scan, new_temp computed above is the exclusive scan
8002 : : prefix sum. Turn it into inclusive prefix sum for the broadcast
8003 : : of the last element into orig. */
8004 : 736 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8005 : : {
8006 : 364 : last_perm_arg = make_ssa_name (vectype);
8007 : 364 : g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
8008 : 364 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8009 : : }
8010 : :
8011 : 736 : orig = make_ssa_name (vectype);
8012 : 2208 : g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
8013 : 736 : last_perm_arg, perms[units_log2]);
8014 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8015 : :
8016 : 736 : if (!inscan_var_store)
8017 : : {
8018 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8019 : : unshare_expr (dataref_ptr),
8020 : : dataref_offset);
8021 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8022 : 368 : g = gimple_build_assign (data_ref, new_temp);
8023 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8024 : : }
8025 : : }
8026 : :
8027 : 512 : if (inscan_var_store)
8028 : 624 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8029 : : {
8030 : 368 : if (j != 0)
8031 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8032 : :
8033 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8034 : : unshare_expr (dataref_ptr),
8035 : : dataref_offset);
8036 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8037 : 368 : gimple *g = gimple_build_assign (data_ref, orig);
8038 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8039 : : }
8040 : 512 : return true;
8041 : 512 : }
8042 : :
8043 : :
8044 : : /* Function vectorizable_store.
8045 : :
8046 : : Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
8047 : : that can be vectorized.
8048 : : If COST_VEC is passed, calculate costs but don't change anything,
8049 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
8050 : : it, and insert it at GSI.
8051 : : Return true if STMT_INFO is vectorizable in this way. */
8052 : :
8053 : : static bool
8054 : 1941135 : vectorizable_store (vec_info *vinfo,
8055 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
8056 : : slp_tree slp_node,
8057 : : stmt_vector_for_cost *cost_vec)
8058 : : {
8059 : 1941135 : tree data_ref;
8060 : 1941135 : tree vec_oprnd = NULL_TREE;
8061 : 1941135 : tree elem_type;
8062 : 1941135 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
8063 : 1941135 : class loop *loop = NULL;
8064 : 1941135 : machine_mode vec_mode;
8065 : 1941135 : tree dummy;
8066 : 1941135 : enum vect_def_type rhs_dt = vect_unknown_def_type;
8067 : 1941135 : enum vect_def_type mask_dt = vect_unknown_def_type;
8068 : 1941135 : tree dataref_ptr = NULL_TREE;
8069 : 1941135 : tree dataref_offset = NULL_TREE;
8070 : 1941135 : gimple *ptr_incr = NULL;
8071 : 1941135 : int j;
8072 : 1941135 : stmt_vec_info first_stmt_info;
8073 : 1941135 : bool grouped_store;
8074 : 1941135 : unsigned int group_size, i;
8075 : 1941135 : unsigned int vec_num;
8076 : 1941135 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
8077 : 1941135 : tree aggr_type;
8078 : 1941135 : poly_uint64 vf;
8079 : 1941135 : vec_load_store_type vls_type;
8080 : 1941135 : tree ref_type;
8081 : :
8082 : 1941135 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
8083 : : return false;
8084 : :
8085 : 1941135 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
8086 : 179117 : && cost_vec)
8087 : : return false;
8088 : :
8089 : : /* Is vectorizable store? */
8090 : :
8091 : 1762018 : tree mask_vectype = NULL_TREE;
8092 : 1762018 : slp_tree mask_node = NULL;
8093 : 1762018 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
8094 : : {
8095 : 1697709 : tree scalar_dest = gimple_assign_lhs (assign);
8096 : 1697709 : if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
8097 : 1697709 : && is_pattern_stmt_p (stmt_info))
8098 : 1371 : scalar_dest = TREE_OPERAND (scalar_dest, 0);
8099 : 1697709 : if (TREE_CODE (scalar_dest) != ARRAY_REF
8100 : 1697709 : && TREE_CODE (scalar_dest) != BIT_FIELD_REF
8101 : : && TREE_CODE (scalar_dest) != INDIRECT_REF
8102 : : && TREE_CODE (scalar_dest) != COMPONENT_REF
8103 : : && TREE_CODE (scalar_dest) != IMAGPART_EXPR
8104 : : && TREE_CODE (scalar_dest) != REALPART_EXPR
8105 : : && TREE_CODE (scalar_dest) != MEM_REF)
8106 : : return false;
8107 : : }
8108 : : else
8109 : : {
8110 : 622216 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
8111 : 8583 : if (!call || !gimple_call_internal_p (call))
8112 : : return false;
8113 : :
8114 : 4751 : internal_fn ifn = gimple_call_internal_fn (call);
8115 : 4751 : if (!internal_store_fn_p (ifn))
8116 : : return false;
8117 : :
8118 : 1444 : int mask_index = internal_fn_mask_index (ifn);
8119 : 1444 : if (mask_index >= 0)
8120 : 1444 : mask_index = vect_slp_child_index_for_operand
8121 : 1444 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
8122 : 1444 : if (mask_index >= 0
8123 : 1444 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
8124 : : &mask_node, &mask_dt,
8125 : : &mask_vectype))
8126 : : return false;
8127 : : }
8128 : :
8129 : 1327534 : tree vectype = SLP_TREE_VECTYPE (slp_node), rhs_vectype = NULL_TREE;
8130 : 1327534 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
8131 : :
8132 : 1327534 : if (loop_vinfo)
8133 : : {
8134 : 189059 : loop = LOOP_VINFO_LOOP (loop_vinfo);
8135 : 189059 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8136 : : }
8137 : : else
8138 : : vf = 1;
8139 : 1327534 : vec_num = vect_get_num_copies (vinfo, slp_node);
8140 : :
8141 : : /* FORNOW. This restriction should be relaxed. */
8142 : 1327534 : if (loop
8143 : 1327755 : && nested_in_vect_loop_p (loop, stmt_info)
8144 : 1327763 : && vec_num > 1)
8145 : : {
8146 : 8 : if (dump_enabled_p ())
8147 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8148 : : "multiple types in nested loop.\n");
8149 : 8 : return false;
8150 : : }
8151 : :
8152 : 1327526 : slp_tree op_node;
8153 : 1327526 : if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
8154 : : &op_node, &rhs_dt, &rhs_vectype, &vls_type))
8155 : : return false;
8156 : :
8157 : 1327502 : elem_type = TREE_TYPE (vectype);
8158 : 1327502 : vec_mode = TYPE_MODE (vectype);
8159 : :
8160 : 1327502 : if (!STMT_VINFO_DATA_REF (stmt_info))
8161 : : return false;
8162 : :
8163 : 1327502 : vect_load_store_data _ls_data{};
8164 : 1327502 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
8165 : 1327502 : if (cost_vec
8166 : 1327502 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
8167 : : vls_type, &_ls_data))
8168 : : return false;
8169 : : /* Temporary aliases to analysis data, should not be modified through
8170 : : these. */
8171 : 1326954 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
8172 : 1326954 : const dr_alignment_support alignment_support_scheme
8173 : : = ls.alignment_support_scheme;
8174 : 1326954 : const int misalignment = ls.misalignment;
8175 : 1326954 : const poly_int64 poffset = ls.poffset;
8176 : :
8177 : 1326954 : if (slp_node->ldst_lanes
8178 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
8179 : : {
8180 : 0 : if (dump_enabled_p ())
8181 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8182 : : "discovered store-lane but cannot use it.\n");
8183 : 0 : return false;
8184 : : }
8185 : :
8186 : 1326954 : if (mask_node)
8187 : : {
8188 : 1354 : if (memory_access_type == VMAT_CONTIGUOUS)
8189 : : {
8190 : 459 : if (!VECTOR_MODE_P (vec_mode)
8191 : 2224 : || !can_vec_mask_load_store_p (vec_mode,
8192 : 1112 : TYPE_MODE (mask_vectype), false))
8193 : 18 : return false;
8194 : : }
8195 : 242 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
8196 : 242 : && (!mat_gather_scatter_p (memory_access_type)
8197 : 218 : || (memory_access_type == VMAT_GATHER_SCATTER_LEGACY
8198 : 154 : && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
8199 : : {
8200 : 24 : if (dump_enabled_p ())
8201 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8202 : : "unsupported access type for masked store.\n");
8203 : 24 : return false;
8204 : : }
8205 : 218 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
8206 : : {
8207 : 64 : if (dump_enabled_p ())
8208 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8209 : : "unsupported masked emulated scatter.\n");
8210 : 64 : return false;
8211 : : }
8212 : : }
8213 : : else
8214 : : {
8215 : : /* FORNOW. In some cases can vectorize even if data-type not supported
8216 : : (e.g. - array initialization with 0). */
8217 : 1325600 : if (!can_implement_p (mov_optab, vec_mode))
8218 : : return false;
8219 : : }
8220 : :
8221 : 1326848 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
8222 : 1326848 : grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
8223 : 2482427 : && !mat_gather_scatter_p (memory_access_type));
8224 : 1155579 : if (grouped_store)
8225 : : {
8226 : 1155579 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8227 : 1155579 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8228 : 1155579 : group_size = DR_GROUP_SIZE (first_stmt_info);
8229 : : }
8230 : : else
8231 : : {
8232 : 1326848 : first_stmt_info = stmt_info;
8233 : 1326848 : first_dr_info = dr_info;
8234 : : group_size = 1;
8235 : : }
8236 : :
8237 : 1326848 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && cost_vec)
8238 : : {
8239 : 1076 : if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node,
8240 : : mask_node, memory_access_type))
8241 : : return false;
8242 : : }
8243 : :
8244 : 2652928 : bool costing_p = cost_vec;
8245 : 1326080 : if (costing_p) /* transformation not required. */
8246 : : {
8247 : 781348 : if (loop_vinfo
8248 : 126459 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
8249 : 57132 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
8250 : : vls_type, group_size, &ls,
8251 : : mask_node);
8252 : :
8253 : 781348 : if (!vect_maybe_update_slp_op_vectype (op_node, vectype)
8254 : 781348 : || (mask_node
8255 : 710 : && !vect_maybe_update_slp_op_vectype (mask_node,
8256 : : mask_vectype)))
8257 : : {
8258 : 0 : if (dump_enabled_p ())
8259 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8260 : : "incompatible vector types for invariants\n");
8261 : 0 : return false;
8262 : : }
8263 : :
8264 : 781348 : if (dump_enabled_p ()
8265 : : && memory_access_type != VMAT_ELEMENTWISE
8266 : 14234 : && memory_access_type != VMAT_STRIDED_SLP
8267 : 13611 : && memory_access_type != VMAT_INVARIANT
8268 : 794959 : && alignment_support_scheme != dr_aligned)
8269 : 4642 : dump_printf_loc (MSG_NOTE, vect_location,
8270 : : "Vectorizing an unaligned access.\n");
8271 : :
8272 : 781348 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
8273 : 781348 : slp_node->data = new vect_load_store_data (std::move (ls));
8274 : : }
8275 : :
8276 : : /* Transform. */
8277 : :
8278 : 1326848 : ensure_base_align (dr_info);
8279 : :
8280 : 1326848 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
8281 : : {
8282 : 1024 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
8283 : 1024 : gcc_assert (SLP_TREE_LANES (slp_node) == 1);
8284 : 1024 : if (costing_p)
8285 : : {
8286 : 512 : unsigned int inside_cost = 0, prologue_cost = 0;
8287 : 512 : if (vls_type == VLS_STORE_INVARIANT)
8288 : 0 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8289 : : slp_node, 0, vect_prologue);
8290 : 512 : vect_get_store_cost (vinfo, stmt_info, slp_node, 1,
8291 : : alignment_support_scheme, misalignment,
8292 : : &inside_cost, cost_vec);
8293 : :
8294 : 512 : if (dump_enabled_p ())
8295 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
8296 : : "vect_model_store_cost: inside_cost = %d, "
8297 : : "prologue_cost = %d .\n",
8298 : : inside_cost, prologue_cost);
8299 : :
8300 : 512 : return true;
8301 : : }
8302 : 512 : return vectorizable_scan_store (vinfo, stmt_info, slp_node, gsi);
8303 : : }
8304 : :
8305 : : /* FORNOW */
8306 : 1325824 : gcc_assert (!grouped_store
8307 : : || !loop
8308 : : || !nested_in_vect_loop_p (loop, stmt_info));
8309 : :
8310 : 1325824 : grouped_store = false;
8311 : 1325824 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
8312 : 1325824 : gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
8313 : : || (DR_GROUP_FIRST_ELEMENT (first_stmt_info) == first_stmt_info));
8314 : 1325824 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8315 : :
8316 : 1325824 : ref_type = get_group_alias_ptr_type (first_stmt_info);
8317 : :
8318 : 1325824 : if (!costing_p && dump_enabled_p ())
8319 : 11830 : dump_printf_loc (MSG_NOTE, vect_location, "transform store.\n");
8320 : :
8321 : 1325824 : if (memory_access_type == VMAT_ELEMENTWISE
8322 : 1325824 : || memory_access_type == VMAT_STRIDED_SLP)
8323 : : {
8324 : 29820 : unsigned inside_cost = 0, prologue_cost = 0;
8325 : 29820 : gimple_stmt_iterator incr_gsi;
8326 : 29820 : bool insert_after;
8327 : 29820 : tree offvar = NULL_TREE;
8328 : 29820 : tree ivstep;
8329 : 29820 : tree running_off;
8330 : 29820 : tree stride_base, stride_step, alias_off;
8331 : 29820 : tree vec_oprnd = NULL_TREE;
8332 : 29820 : tree dr_offset;
8333 : : /* Checked by get_load_store_type. */
8334 : 29820 : unsigned int const_nunits = nunits.to_constant ();
8335 : :
8336 : 29820 : gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
8337 : 29820 : gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
8338 : :
8339 : 29820 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
8340 : 29820 : stride_base
8341 : 29820 : = fold_build_pointer_plus
8342 : : (DR_BASE_ADDRESS (first_dr_info->dr),
8343 : : size_binop (PLUS_EXPR,
8344 : : convert_to_ptrofftype (dr_offset),
8345 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
8346 : 29820 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
8347 : :
8348 : : /* For a store with loop-invariant (but other than power-of-2)
8349 : : stride (i.e. not a grouped access) like so:
8350 : :
8351 : : for (i = 0; i < n; i += stride)
8352 : : array[i] = ...;
8353 : :
8354 : : we generate a new induction variable and new stores from
8355 : : the components of the (vectorized) rhs:
8356 : :
8357 : : for (j = 0; ; j += VF*stride)
8358 : : vectemp = ...;
8359 : : tmp1 = vectemp[0];
8360 : : array[j] = tmp1;
8361 : : tmp2 = vectemp[1];
8362 : : array[j + stride] = tmp2;
8363 : : ...
8364 : : */
8365 : :
8366 : : /* ??? Modify local copies of alignment_support_scheme and
8367 : : misalignment, but this part of analysis should be done
8368 : : earlier and remembered, likewise the chosen load mode. */
8369 : 29820 : const dr_alignment_support tem = alignment_support_scheme;
8370 : 29820 : dr_alignment_support alignment_support_scheme = tem;
8371 : 29820 : const int tem2 = misalignment;
8372 : 29820 : int misalignment = tem2;
8373 : :
8374 : 29820 : unsigned nstores = const_nunits;
8375 : 29820 : unsigned lnel = 1;
8376 : 29820 : tree ltype = elem_type;
8377 : 29820 : tree lvectype = vectype;
8378 : 29820 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
8379 : 29820 : if (n == const_nunits)
8380 : : {
8381 : 2605 : int mis_align = dr_misalignment (first_dr_info, vectype);
8382 : : /* With VF > 1 we advance the DR by step, if that is constant
8383 : : and only aligned when performed VF times, DR alignment
8384 : : analysis can analyze this as aligned since it assumes
8385 : : contiguous accesses. But that is not how we code generate
8386 : : here, so adjust for this. */
8387 : 2605 : if (maybe_gt (vf, 1u)
8388 : 4118 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8389 : 3890 : DR_TARGET_ALIGNMENT (first_dr_info)))
8390 : 228 : mis_align = -1;
8391 : 2605 : dr_alignment_support dr_align
8392 : 2605 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
8393 : : mis_align);
8394 : 2605 : if (dr_align == dr_aligned
8395 : 2605 : || dr_align == dr_unaligned_supported)
8396 : : {
8397 : 29820 : nstores = 1;
8398 : 29820 : lnel = const_nunits;
8399 : 29820 : ltype = vectype;
8400 : 29820 : lvectype = vectype;
8401 : 29820 : alignment_support_scheme = dr_align;
8402 : 29820 : misalignment = mis_align;
8403 : : }
8404 : : }
8405 : 27215 : else if (n > 1)
8406 : : {
8407 : 1803 : nstores = const_nunits / n;
8408 : 1803 : lnel = n;
8409 : 1803 : ltype = build_vector_type (elem_type, n);
8410 : 1803 : lvectype = vectype;
8411 : 1803 : int mis_align = dr_misalignment (first_dr_info, ltype);
8412 : 1803 : if (maybe_gt (vf, 1u)
8413 : 3606 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8414 : 2891 : DR_TARGET_ALIGNMENT (first_dr_info)))
8415 : 715 : mis_align = -1;
8416 : 1803 : dr_alignment_support dr_align
8417 : 1803 : = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
8418 : : mis_align);
8419 : 1803 : alignment_support_scheme = dr_align;
8420 : 1803 : misalignment = mis_align;
8421 : :
8422 : : /* First check if vec_extract optab doesn't support extraction
8423 : : of vector elts directly. */
8424 : 1803 : scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
8425 : 1803 : machine_mode vmode;
8426 : 3606 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8427 : 2029 : || !related_vector_mode (TYPE_MODE (vectype), elmode,
8428 : 1803 : n).exists (&vmode)
8429 : 1623 : || (convert_optab_handler (vec_extract_optab,
8430 : 1623 : TYPE_MODE (vectype), vmode)
8431 : : == CODE_FOR_nothing)
8432 : 1803 : || !(dr_align == dr_aligned
8433 : 226 : || dr_align == dr_unaligned_supported))
8434 : : {
8435 : : /* Try to avoid emitting an extract of vector elements
8436 : : by performing the extracts using an integer type of the
8437 : : same size, extracting from a vector of those and then
8438 : : re-interpreting it as the original vector type if
8439 : : supported. */
8440 : 1577 : unsigned lsize = n * GET_MODE_BITSIZE (elmode);
8441 : 1577 : unsigned int lnunits = const_nunits / n;
8442 : : /* If we can't construct such a vector fall back to
8443 : : element extracts from the original vector type and
8444 : : element size stores. */
8445 : 1577 : if (int_mode_for_size (lsize, 0).exists (&elmode)
8446 : 1577 : && VECTOR_MODE_P (TYPE_MODE (vectype))
8447 : 1577 : && related_vector_mode (TYPE_MODE (vectype), elmode,
8448 : 1577 : lnunits).exists (&vmode)
8449 : 1543 : && (convert_optab_handler (vec_extract_optab,
8450 : : vmode, elmode)
8451 : : != CODE_FOR_nothing))
8452 : : {
8453 : 1543 : nstores = lnunits;
8454 : 1543 : lnel = n;
8455 : 1543 : ltype = build_nonstandard_integer_type (lsize, 1);
8456 : 1543 : lvectype = build_vector_type (ltype, nstores);
8457 : : }
8458 : : /* Else fall back to vector extraction anyway.
8459 : : Fewer stores are more important than avoiding spilling
8460 : : of the vector we extract from. Compared to the
8461 : : construction case in vectorizable_load no store-forwarding
8462 : : issue exists here for reasonable archs. But only
8463 : : if the store is supported. */
8464 : 34 : else if (!(dr_align == dr_aligned
8465 : 34 : || dr_align == dr_unaligned_supported))
8466 : : {
8467 : : nstores = const_nunits;
8468 : : lnel = 1;
8469 : : ltype = elem_type;
8470 : : lvectype = vectype;
8471 : : }
8472 : : }
8473 : : }
8474 : 29820 : unsigned align;
8475 : 29820 : if (alignment_support_scheme == dr_aligned)
8476 : 1118 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
8477 : : else
8478 : 28702 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
8479 : : /* Alignment is at most the access size if we do multiple stores. */
8480 : 29820 : if (nstores > 1)
8481 : 27215 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
8482 : 29820 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
8483 : 29820 : int ncopies = vec_num;
8484 : :
8485 : 29820 : if (!costing_p)
8486 : : {
8487 : 3310 : ivstep = stride_step;
8488 : 3310 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
8489 : : build_int_cst (TREE_TYPE (ivstep), vf));
8490 : :
8491 : 3310 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
8492 : :
8493 : 3310 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
8494 : 3310 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
8495 : 3310 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
8496 : : insert_after, &offvar, NULL);
8497 : :
8498 : 3310 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
8499 : : }
8500 : :
8501 : 29820 : alias_off = build_int_cst (ref_type, 0);
8502 : 29820 : auto_vec<tree> vec_oprnds;
8503 : : /* For costing some adjacent vector stores, we'd like to cost with
8504 : : the total number of them once instead of cost each one by one. */
8505 : 29820 : unsigned int n_adjacent_stores = 0;
8506 : 29820 : running_off = offvar;
8507 : 29820 : if (!costing_p)
8508 : 3310 : vect_get_slp_defs (op_node, &vec_oprnds);
8509 : 29820 : unsigned int group_el = 0;
8510 : 29820 : unsigned HOST_WIDE_INT elsz
8511 : 29820 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
8512 : 70562 : for (j = 0; j < ncopies; j++)
8513 : : {
8514 : 40742 : if (!costing_p)
8515 : : {
8516 : 5023 : vec_oprnd = vec_oprnds[j];
8517 : : /* Pun the vector to extract from if necessary. */
8518 : 5023 : if (lvectype != vectype)
8519 : : {
8520 : 1210 : tree tem = make_ssa_name (lvectype);
8521 : 1210 : tree cvt = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
8522 : 1210 : gimple *pun = gimple_build_assign (tem, cvt);
8523 : 1210 : vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
8524 : 1210 : vec_oprnd = tem;
8525 : : }
8526 : : }
8527 : 183654 : for (i = 0; i < nstores; i++)
8528 : : {
8529 : 142912 : if (costing_p)
8530 : : {
8531 : 127197 : n_adjacent_stores++;
8532 : 127197 : continue;
8533 : : }
8534 : 15715 : tree newref, newoff;
8535 : 15715 : gimple *incr, *assign;
8536 : 15715 : tree size = TYPE_SIZE (ltype);
8537 : : /* Extract the i'th component. */
8538 : 15715 : tree pos = fold_build2 (MULT_EXPR, bitsizetype,
8539 : : bitsize_int (i), size);
8540 : 15715 : tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
8541 : : size, pos);
8542 : :
8543 : 15715 : elem = force_gimple_operand_gsi (gsi, elem, true, NULL_TREE, true,
8544 : : GSI_SAME_STMT);
8545 : :
8546 : 15715 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
8547 : 15715 : group_el * elsz);
8548 : 15715 : newref = build2 (MEM_REF, ltype, running_off, this_off);
8549 : 15715 : vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
8550 : :
8551 : : /* And store it to *running_off. */
8552 : 15715 : assign = gimple_build_assign (newref, elem);
8553 : 15715 : vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
8554 : :
8555 : 15715 : group_el += lnel;
8556 : 15715 : if (group_el == group_size)
8557 : : {
8558 : 14612 : newoff = copy_ssa_name (running_off, NULL);
8559 : 14612 : incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
8560 : : running_off, stride_step);
8561 : 14612 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
8562 : :
8563 : 14612 : running_off = newoff;
8564 : 14612 : group_el = 0;
8565 : : }
8566 : : }
8567 : : }
8568 : :
8569 : 29820 : if (costing_p)
8570 : : {
8571 : 26510 : if (n_adjacent_stores > 0)
8572 : : {
8573 : : /* Take a single lane vector type store as scalar
8574 : : store to avoid ICE like 110776. */
8575 : 26510 : if (VECTOR_TYPE_P (ltype)
8576 : 26510 : && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
8577 : 1214 : vect_get_store_cost (vinfo, stmt_info, slp_node,
8578 : : n_adjacent_stores, alignment_support_scheme,
8579 : : misalignment, &inside_cost, cost_vec);
8580 : : else
8581 : 25296 : inside_cost
8582 : 25296 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8583 : : scalar_store, slp_node, 0, vect_body);
8584 : : /* Only need vector extracting when there are more
8585 : : than one stores. */
8586 : 26510 : if (nstores > 1)
8587 : 24710 : inside_cost
8588 : 24710 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8589 : : vec_to_scalar, slp_node, 0, vect_body);
8590 : : }
8591 : 26510 : if (dump_enabled_p ())
8592 : 623 : dump_printf_loc (MSG_NOTE, vect_location,
8593 : : "vect_model_store_cost: inside_cost = %d, "
8594 : : "prologue_cost = %d .\n",
8595 : : inside_cost, prologue_cost);
8596 : : }
8597 : :
8598 : 29820 : return true;
8599 : 29820 : }
8600 : :
8601 : 1296004 : gcc_assert (alignment_support_scheme);
8602 : 1296004 : vec_loop_masks *loop_masks
8603 : 157529 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
8604 : 1296004 : ? &LOOP_VINFO_MASKS (loop_vinfo)
8605 : 10 : : NULL);
8606 : 10 : vec_loop_lens *loop_lens
8607 : 157529 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
8608 : : ? &LOOP_VINFO_LENS (loop_vinfo)
8609 : 0 : : NULL);
8610 : :
8611 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
8612 : : are some difference here. We cannot enable both the lens and masks
8613 : : during transform but it is allowed during analysis.
8614 : : Shouldn't go with length-based approach if fully masked. */
8615 : 1296004 : if (cost_vec == NULL)
8616 : : /* The cost_vec is NULL during transfrom. */
8617 : 541678 : gcc_assert ((!loop_lens || !loop_masks));
8618 : :
8619 : : /* Targets with store-lane instructions must not require explicit
8620 : : realignment. vect_supportable_dr_alignment always returns either
8621 : : dr_aligned or dr_unaligned_supported for masked operations. */
8622 : 1296004 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
8623 : : && !mask_node
8624 : : && !loop_masks)
8625 : : || alignment_support_scheme == dr_aligned
8626 : : || alignment_support_scheme == dr_unaligned_supported);
8627 : :
8628 : 1296004 : tree offset = NULL_TREE;
8629 : 1296004 : if (!known_eq (poffset, 0))
8630 : 4084 : offset = size_int (poffset);
8631 : :
8632 : 1296004 : tree bump;
8633 : 1296004 : tree vec_offset = NULL_TREE;
8634 : 1296004 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8635 : : {
8636 : 1359 : aggr_type = NULL_TREE;
8637 : 1359 : bump = NULL_TREE;
8638 : : }
8639 : 1294645 : else if (mat_gather_scatter_p (memory_access_type))
8640 : : {
8641 : 0 : aggr_type = elem_type;
8642 : 0 : if (!costing_p)
8643 : : {
8644 : 0 : tree vtype = ls.ls_type ? ls.ls_type : vectype;
8645 : 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vtype,
8646 : : ls.strided_offset_vectype,
8647 : : loop_vinfo, gsi,
8648 : : &bump, &vec_offset, loop_lens);
8649 : : }
8650 : : }
8651 : : else
8652 : : {
8653 : 1294645 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8654 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
8655 : : else
8656 : : aggr_type = vectype;
8657 : 1294645 : if (!costing_p)
8658 : 541204 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
8659 : : memory_access_type, loop_lens);
8660 : : }
8661 : :
8662 : 1296004 : if (mask_node && !costing_p)
8663 : 538 : LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
8664 : :
8665 : : /* In case the vectorization factor (VF) is bigger than the number
8666 : : of elements that we can fit in a vectype (nunits), we have to generate
8667 : : more than one vector stmt - i.e - we need to "unroll" the
8668 : : vector stmt by a factor VF/nunits. */
8669 : :
8670 : 1296004 : auto_vec<tree> dr_chain (group_size);
8671 : 1296004 : auto_vec<tree> vec_masks;
8672 : 1296004 : tree vec_mask = NULL;
8673 : 1296004 : auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
8674 : 5890862 : for (i = 0; i < group_size; i++)
8675 : 3298854 : gvec_oprnds.quick_push (new auto_vec<tree> ());
8676 : :
8677 : 1296004 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8678 : : {
8679 : 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
8680 : :
8681 : 0 : if (costing_p)
8682 : : /* Update all incoming store operand nodes, the general handling
8683 : : above only handles the mask and the first store operand node. */
8684 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
8685 : 0 : if (child != mask_node
8686 : 0 : && !vect_maybe_update_slp_op_vectype (child, vectype))
8687 : : {
8688 : 0 : if (dump_enabled_p ())
8689 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8690 : : "incompatible vector types for invariants\n");
8691 : 0 : return false;
8692 : : }
8693 : 0 : unsigned inside_cost = 0, prologue_cost = 0;
8694 : : /* For costing some adjacent vector stores, we'd like to cost with
8695 : : the total number of them once instead of cost each one by one. */
8696 : 0 : unsigned int n_adjacent_stores = 0;
8697 : 0 : int ncopies = vec_num / group_size;
8698 : 0 : for (j = 0; j < ncopies; j++)
8699 : : {
8700 : 0 : if (j == 0)
8701 : : {
8702 : 0 : if (!costing_p)
8703 : : {
8704 : 0 : if (mask_node)
8705 : : {
8706 : 0 : vect_get_slp_defs (mask_node, &vec_masks);
8707 : 0 : vec_mask = vec_masks[0];
8708 : : }
8709 : 0 : dataref_ptr
8710 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8711 : : aggr_type, NULL, offset, &dummy,
8712 : : gsi, &ptr_incr, false, bump);
8713 : : }
8714 : : }
8715 : 0 : else if (!costing_p)
8716 : : {
8717 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8718 : 0 : if (mask_node)
8719 : 0 : vec_mask = vec_masks[j];
8720 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
8721 : : stmt_info, bump);
8722 : : }
8723 : :
8724 : 0 : if (costing_p)
8725 : : {
8726 : 0 : n_adjacent_stores += group_size;
8727 : 0 : continue;
8728 : : }
8729 : :
8730 : : /* Get an array into which we can store the individual vectors. */
8731 : 0 : tree vec_array = create_vector_array (vectype, group_size);
8732 : :
8733 : : /* Invalidate the current contents of VEC_ARRAY. This should
8734 : : become an RTL clobber too, which prevents the vector registers
8735 : : from being upward-exposed. */
8736 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8737 : :
8738 : : /* Store the individual vectors into the array. */
8739 : 0 : for (i = 0; i < group_size; i++)
8740 : : {
8741 : 0 : slp_tree child;
8742 : 0 : if (i == 0 || !mask_node)
8743 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i];
8744 : : else
8745 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i + 1];
8746 : 0 : vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
8747 : 0 : write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
8748 : : i);
8749 : : }
8750 : :
8751 : 0 : tree final_mask = NULL;
8752 : 0 : tree final_len = NULL;
8753 : 0 : tree bias = NULL;
8754 : 0 : if (loop_masks)
8755 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
8756 : : ncopies, vectype, j);
8757 : 0 : if (vec_mask)
8758 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
8759 : : vec_mask, gsi);
8760 : :
8761 : 0 : if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
8762 : : {
8763 : 0 : if (loop_lens)
8764 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
8765 : : ncopies, vectype, j, 1);
8766 : : else
8767 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8768 : 0 : signed char biasval
8769 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8770 : 0 : bias = build_int_cst (intQI_type_node, biasval);
8771 : 0 : if (!final_mask)
8772 : : {
8773 : 0 : mask_vectype = truth_type_for (vectype);
8774 : 0 : final_mask = build_minus_one_cst (mask_vectype);
8775 : : }
8776 : : }
8777 : :
8778 : 0 : gcall *call;
8779 : 0 : if (final_len && final_mask)
8780 : : {
8781 : : /* Emit:
8782 : : MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8783 : : LEN, BIAS, VEC_ARRAY). */
8784 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8785 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
8786 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
8787 : : dataref_ptr, alias_ptr,
8788 : : final_mask, final_len, bias,
8789 : : vec_array);
8790 : : }
8791 : 0 : else if (final_mask)
8792 : : {
8793 : : /* Emit:
8794 : : MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8795 : : VEC_ARRAY). */
8796 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8797 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
8798 : 0 : call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
8799 : : dataref_ptr, alias_ptr,
8800 : : final_mask, vec_array);
8801 : : }
8802 : : else
8803 : : {
8804 : : /* Emit:
8805 : : MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
8806 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
8807 : 0 : call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
8808 : 0 : gimple_call_set_lhs (call, data_ref);
8809 : : }
8810 : 0 : gimple_call_set_nothrow (call, true);
8811 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
8812 : :
8813 : : /* Record that VEC_ARRAY is now dead. */
8814 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8815 : : }
8816 : :
8817 : 0 : if (costing_p)
8818 : : {
8819 : 0 : if (n_adjacent_stores > 0)
8820 : 0 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
8821 : : alignment_support_scheme, misalignment,
8822 : : &inside_cost, cost_vec);
8823 : 0 : if (dump_enabled_p ())
8824 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
8825 : : "vect_model_store_cost: inside_cost = %d, "
8826 : : "prologue_cost = %d .\n",
8827 : : inside_cost, prologue_cost);
8828 : : }
8829 : :
8830 : 0 : return true;
8831 : : }
8832 : :
8833 : 1296004 : if (mat_gather_scatter_p (memory_access_type))
8834 : : {
8835 : 1359 : gcc_assert (!grouped_store || ls.ls_type);
8836 : 1359 : if (ls.ls_type)
8837 : 0 : vectype = ls.ls_type;
8838 : 1359 : auto_vec<tree> vec_offsets;
8839 : 1359 : unsigned int inside_cost = 0, prologue_cost = 0;
8840 : 1359 : int num_stmts = vec_num;
8841 : 3126 : for (j = 0; j < num_stmts; j++)
8842 : : {
8843 : 1767 : gimple *new_stmt;
8844 : 1767 : if (j == 0)
8845 : : {
8846 : 1359 : if (costing_p && vls_type == VLS_STORE_INVARIANT)
8847 : 202 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8848 : : slp_node, 0, vect_prologue);
8849 : : else if (!costing_p)
8850 : : {
8851 : : /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
8852 : : DR_CHAIN is of size 1. */
8853 : 474 : gcc_assert (group_size == 1);
8854 : 474 : vect_get_slp_defs (op_node, gvec_oprnds[0]);
8855 : 474 : if (mask_node)
8856 : 70 : vect_get_slp_defs (mask_node, &vec_masks);
8857 : :
8858 : 474 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8859 : 474 : vect_get_gather_scatter_ops (loop, slp_node,
8860 : : &dataref_ptr, &vec_offsets);
8861 : : else
8862 : 0 : dataref_ptr
8863 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8864 : : aggr_type, NULL, offset,
8865 : : &dummy, gsi, &ptr_incr, false,
8866 : : bump);
8867 : : }
8868 : : }
8869 : 408 : else if (!costing_p)
8870 : : {
8871 : 38 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8872 : 38 : if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8873 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
8874 : : gsi, stmt_info, bump);
8875 : : }
8876 : :
8877 : 2481 : new_stmt = NULL;
8878 : 714 : if (!costing_p)
8879 : : {
8880 : 512 : vec_oprnd = (*gvec_oprnds[0])[j];
8881 : 512 : if (mask_node)
8882 : 90 : vec_mask = vec_masks[j];
8883 : : /* We should have catched mismatched types earlier. */
8884 : 512 : gcc_assert (ls.ls_type
8885 : : || useless_type_conversion_p
8886 : : (vectype, TREE_TYPE (vec_oprnd)));
8887 : : }
8888 : 512 : tree final_mask = NULL_TREE;
8889 : 2279 : tree final_len = NULL_TREE;
8890 : 2279 : tree bias = NULL_TREE;
8891 : 512 : if (!costing_p)
8892 : : {
8893 : 512 : if (loop_masks)
8894 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi,
8895 : : loop_masks, num_stmts,
8896 : : vectype, j);
8897 : 512 : if (vec_mask)
8898 : 90 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
8899 : : final_mask, vec_mask, gsi);
8900 : : }
8901 : :
8902 : 1767 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
8903 : 1767 : tree alias_align_ptr = build_int_cst (ref_type, align);
8904 : 1767 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
8905 : : {
8906 : 0 : if (costing_p)
8907 : : {
8908 : 0 : if (ls.supported_offset_vectype)
8909 : 0 : inside_cost
8910 : 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
8911 : : slp_node, 0, vect_body);
8912 : 0 : if (ls.supported_scale)
8913 : 0 : inside_cost
8914 : 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
8915 : : slp_node, 0, vect_body);
8916 : :
8917 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
8918 : 0 : inside_cost
8919 : 0 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
8920 : : slp_node, 0, vect_body);
8921 : 1767 : continue;
8922 : 0 : }
8923 : :
8924 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8925 : 0 : vec_offset = vec_offsets[j];
8926 : :
8927 : 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
8928 : 0 : bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
8929 : :
8930 : : /* Perform the offset conversion and scaling if necessary. */
8931 : 0 : if (!strided
8932 : 0 : && (ls.supported_offset_vectype || ls.supported_scale))
8933 : : {
8934 : 0 : gimple_seq stmts = NULL;
8935 : 0 : if (ls.supported_offset_vectype)
8936 : 0 : vec_offset = gimple_convert
8937 : 0 : (&stmts, ls.supported_offset_vectype, vec_offset);
8938 : 0 : if (ls.supported_scale)
8939 : : {
8940 : 0 : tree mult_cst = build_int_cst
8941 : 0 : (TREE_TYPE (TREE_TYPE (vec_offset)),
8942 : 0 : SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
8943 : 0 : tree mult = build_vector_from_val
8944 : 0 : (TREE_TYPE (vec_offset), mult_cst);
8945 : 0 : vec_offset = gimple_build
8946 : 0 : (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
8947 : : vec_offset, mult);
8948 : 0 : scale = size_int (ls.supported_scale);
8949 : : }
8950 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
8951 : : }
8952 : :
8953 : 0 : if (ls.gs.ifn == IFN_MASK_LEN_SCATTER_STORE)
8954 : : {
8955 : 0 : if (loop_lens)
8956 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi,
8957 : : loop_lens, num_stmts,
8958 : : vectype, j, 1);
8959 : : else
8960 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8961 : :
8962 : 0 : signed char biasval
8963 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8964 : 0 : bias = build_int_cst (intQI_type_node, biasval);
8965 : 0 : if (!final_mask)
8966 : : {
8967 : 0 : mask_vectype = truth_type_for (vectype);
8968 : 0 : final_mask = build_minus_one_cst (mask_vectype);
8969 : : }
8970 : : }
8971 : :
8972 : 0 : if (ls.ls_type)
8973 : : {
8974 : 0 : gimple *conv_stmt
8975 : 0 : = gimple_build_assign (make_ssa_name (vectype),
8976 : : VIEW_CONVERT_EXPR,
8977 : : build1 (VIEW_CONVERT_EXPR, vectype,
8978 : : vec_oprnd));
8979 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, conv_stmt,
8980 : : gsi);
8981 : 0 : vec_oprnd = gimple_get_lhs (conv_stmt);
8982 : : }
8983 : :
8984 : 0 : gcall *call;
8985 : 0 : if (final_len && final_mask)
8986 : : {
8987 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
8988 : 0 : call = gimple_build_call_internal (
8989 : : IFN_MASK_LEN_SCATTER_STORE, 8, dataref_ptr,
8990 : : alias_align_ptr,
8991 : : vec_offset, scale, vec_oprnd, final_mask, final_len,
8992 : : bias);
8993 : : else
8994 : : /* Non-vector offset indicates that prefer to take
8995 : : MASK_LEN_STRIDED_STORE instead of the
8996 : : IFN_MASK_SCATTER_STORE with direct stride arg.
8997 : : Similar to the gather case we have checked the
8998 : : alignment for a scatter already and assume
8999 : : that the strided store has the same requirements. */
9000 : 0 : call = gimple_build_call_internal (
9001 : : IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
9002 : : vec_offset, vec_oprnd, final_mask, final_len, bias);
9003 : : }
9004 : 0 : else if (final_mask)
9005 : 0 : call = gimple_build_call_internal
9006 : 0 : (IFN_MASK_SCATTER_STORE, 6, dataref_ptr,
9007 : : alias_align_ptr,
9008 : : vec_offset, scale, vec_oprnd, final_mask);
9009 : : else
9010 : 0 : call = gimple_build_call_internal (IFN_SCATTER_STORE, 5,
9011 : : dataref_ptr,
9012 : : alias_align_ptr,
9013 : : vec_offset,
9014 : : scale, vec_oprnd);
9015 : 0 : gimple_call_set_nothrow (call, true);
9016 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9017 : 0 : new_stmt = call;
9018 : : }
9019 : 1767 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
9020 : : {
9021 : : /* The builtin decls path for scatter is legacy, x86 only. */
9022 : 326 : gcc_assert (nunits.is_constant ()
9023 : : && (!final_mask
9024 : : || SCALAR_INT_MODE_P
9025 : : (TYPE_MODE (TREE_TYPE (final_mask)))));
9026 : 326 : if (costing_p)
9027 : : {
9028 : 187 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9029 : 187 : inside_cost
9030 : 187 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9031 : : slp_node, 0, vect_body);
9032 : 187 : continue;
9033 : 187 : }
9034 : :
9035 : 139 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
9036 : 139 : poly_uint64 offset_nunits
9037 : 139 : = TYPE_VECTOR_SUBPARTS (offset_vectype);
9038 : 139 : if (known_eq (nunits, offset_nunits))
9039 : : {
9040 : 63 : new_stmt = vect_build_one_scatter_store_call
9041 : 126 : (vinfo, stmt_info, slp_node, gsi,
9042 : 63 : ls.gs.decl, dataref_ptr, vec_offsets[j],
9043 : : vec_oprnd, final_mask);
9044 : 63 : vect_finish_stmt_generation (vinfo, stmt_info,
9045 : : new_stmt, gsi);
9046 : : }
9047 : 76 : else if (known_eq (nunits, offset_nunits * 2))
9048 : : {
9049 : : /* We have a offset vector with half the number of
9050 : : lanes but the builtins will store full vectype
9051 : : data from the lower lanes. */
9052 : 30 : new_stmt = vect_build_one_scatter_store_call
9053 : 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
9054 : 30 : dataref_ptr, vec_offsets[2 * j],
9055 : : vec_oprnd, final_mask);
9056 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9057 : : new_stmt, gsi);
9058 : 30 : int count = nunits.to_constant ();
9059 : 30 : vec_perm_builder sel (count, count, 1);
9060 : 30 : sel.quick_grow (count);
9061 : 382 : for (int i = 0; i < count; ++i)
9062 : 352 : sel[i] = i | (count / 2);
9063 : 30 : vec_perm_indices indices (sel, 2, count);
9064 : 30 : tree perm_mask
9065 : 30 : = vect_gen_perm_mask_checked (vectype, indices);
9066 : 30 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
9067 : : vec_oprnd, vec_oprnd,
9068 : : perm_mask);
9069 : 30 : vec_oprnd = make_ssa_name (vectype);
9070 : 30 : gimple_set_lhs (new_stmt, vec_oprnd);
9071 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9072 : : new_stmt, gsi);
9073 : 30 : if (final_mask)
9074 : : {
9075 : 20 : new_stmt = gimple_build_assign (NULL_TREE,
9076 : : VEC_UNPACK_HI_EXPR,
9077 : : final_mask);
9078 : 20 : final_mask = make_ssa_name
9079 : 20 : (truth_type_for (offset_vectype));
9080 : 20 : gimple_set_lhs (new_stmt, final_mask);
9081 : 20 : vect_finish_stmt_generation (vinfo, stmt_info,
9082 : : new_stmt, gsi);
9083 : : }
9084 : :
9085 : 30 : new_stmt = vect_build_one_scatter_store_call
9086 : 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
9087 : 30 : dataref_ptr, vec_offsets[2 * j + 1],
9088 : : vec_oprnd, final_mask);
9089 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9090 : : new_stmt, gsi);
9091 : 30 : }
9092 : 46 : else if (known_eq (nunits * 2, offset_nunits))
9093 : : {
9094 : : /* We have a offset vector with double the number of
9095 : : lanes. Select the low/high part accordingly. */
9096 : 46 : vec_offset = vec_offsets[j / 2];
9097 : 46 : if (j & 1)
9098 : : {
9099 : 23 : int count = offset_nunits.to_constant ();
9100 : 23 : vec_perm_builder sel (count, count, 1);
9101 : 23 : sel.quick_grow (count);
9102 : 263 : for (int i = 0; i < count; ++i)
9103 : 240 : sel[i] = i | (count / 2);
9104 : 23 : vec_perm_indices indices (sel, 2, count);
9105 : 23 : tree perm_mask = vect_gen_perm_mask_checked
9106 : 23 : (TREE_TYPE (vec_offset), indices);
9107 : 23 : new_stmt = gimple_build_assign (NULL_TREE,
9108 : : VEC_PERM_EXPR,
9109 : : vec_offset,
9110 : : vec_offset,
9111 : : perm_mask);
9112 : 23 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
9113 : 23 : gimple_set_lhs (new_stmt, vec_offset);
9114 : 23 : vect_finish_stmt_generation (vinfo, stmt_info,
9115 : : new_stmt, gsi);
9116 : 23 : }
9117 : :
9118 : 46 : new_stmt = vect_build_one_scatter_store_call
9119 : 46 : (vinfo, stmt_info, slp_node, gsi,
9120 : : ls.gs.decl, dataref_ptr, vec_offset,
9121 : : vec_oprnd, final_mask);
9122 : 46 : vect_finish_stmt_generation (vinfo, stmt_info,
9123 : : new_stmt, gsi);
9124 : : }
9125 : : else
9126 : 0 : gcc_unreachable ();
9127 : : }
9128 : : else
9129 : : {
9130 : : /* Emulated scatter. */
9131 : 1441 : gcc_assert (!final_mask);
9132 : 1441 : if (costing_p)
9133 : : {
9134 : 1068 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9135 : : /* For emulated scatter N offset vector element extracts
9136 : : (we assume the scalar scaling and ptr + offset add is
9137 : : consumed by the load). */
9138 : 1068 : inside_cost
9139 : 1068 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9140 : : slp_node, 0, vect_body);
9141 : : /* N scalar stores plus extracting the elements. */
9142 : 1068 : inside_cost
9143 : 1068 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9144 : : slp_node, 0, vect_body);
9145 : 1068 : inside_cost
9146 : 1068 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9147 : : slp_node, 0, vect_body);
9148 : 1068 : continue;
9149 : 1068 : }
9150 : :
9151 : 373 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
9152 : 373 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
9153 : 373 : unsigned HOST_WIDE_INT const_offset_nunits
9154 : 373 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
9155 : 373 : vec<constructor_elt, va_gc> *ctor_elts;
9156 : 373 : vec_alloc (ctor_elts, const_nunits);
9157 : 373 : gimple_seq stmts = NULL;
9158 : 373 : tree elt_type = TREE_TYPE (vectype);
9159 : 373 : unsigned HOST_WIDE_INT elt_size
9160 : 373 : = tree_to_uhwi (TYPE_SIZE (elt_type));
9161 : : /* We support offset vectors with more elements
9162 : : than the data vector for now. */
9163 : 373 : unsigned HOST_WIDE_INT factor
9164 : : = const_offset_nunits / const_nunits;
9165 : 373 : vec_offset = vec_offsets[j / factor];
9166 : 373 : unsigned elt_offset
9167 : 373 : = (j % factor) * const_nunits;
9168 : 373 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
9169 : 373 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
9170 : 373 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
9171 : 1519 : for (unsigned k = 0; k < const_nunits; ++k)
9172 : : {
9173 : : /* Compute the offsetted pointer. */
9174 : 1146 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
9175 : : bitsize_int (k + elt_offset));
9176 : 1146 : tree idx
9177 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
9178 : 1146 : vec_offset, TYPE_SIZE (idx_type), boff);
9179 : 1146 : idx = gimple_convert (&stmts, sizetype, idx);
9180 : 1146 : idx = gimple_build (&stmts, MULT_EXPR, sizetype,
9181 : : idx, scale);
9182 : 1146 : tree ptr
9183 : 1146 : = gimple_build (&stmts, PLUS_EXPR,
9184 : 1146 : TREE_TYPE (dataref_ptr),
9185 : : dataref_ptr, idx);
9186 : 1146 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
9187 : : /* Extract the element to be stored. */
9188 : 1146 : tree elt
9189 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF,
9190 : 1146 : TREE_TYPE (vectype),
9191 : 1146 : vec_oprnd, TYPE_SIZE (elt_type),
9192 : 1146 : bitsize_int (k * elt_size));
9193 : 1146 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9194 : 1146 : stmts = NULL;
9195 : 1146 : tree ref
9196 : 1146 : = build2 (MEM_REF, ltype, ptr,
9197 : : build_int_cst (ref_type, 0));
9198 : 1146 : new_stmt = gimple_build_assign (ref, elt);
9199 : 1146 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9200 : : }
9201 : :
9202 : 373 : slp_node->push_vec_def (new_stmt);
9203 : : }
9204 : : }
9205 : :
9206 : 1359 : if (costing_p && dump_enabled_p ())
9207 : 68 : dump_printf_loc (MSG_NOTE, vect_location,
9208 : : "vect_model_store_cost: inside_cost = %d, "
9209 : : "prologue_cost = %d .\n",
9210 : : inside_cost, prologue_cost);
9211 : :
9212 : 1359 : return true;
9213 : 1359 : }
9214 : :
9215 : 1294645 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
9216 : : || memory_access_type == VMAT_CONTIGUOUS_DOWN
9217 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
9218 : :
9219 : 1294645 : unsigned inside_cost = 0, prologue_cost = 0;
9220 : : /* For costing some adjacent vector stores, we'd like to cost with
9221 : : the total number of them once instead of cost each one by one. */
9222 : 1294645 : unsigned int n_adjacent_stores = 0;
9223 : 1294645 : auto_vec<tree> result_chain (group_size);
9224 : 1294645 : auto_vec<tree, 1> vec_oprnds;
9225 : 1294645 : gimple *new_stmt;
9226 : 1294645 : if (!costing_p)
9227 : : {
9228 : : /* Get vectorized arguments for SLP_NODE. */
9229 : 541204 : vect_get_slp_defs (op_node, &vec_oprnds);
9230 : 541204 : vec_oprnd = vec_oprnds[0];
9231 : 541204 : if (mask_node)
9232 : : {
9233 : 468 : vect_get_slp_defs (mask_node, &vec_masks);
9234 : 468 : vec_mask = vec_masks[0];
9235 : : }
9236 : : }
9237 : :
9238 : : /* We should have catched mismatched types earlier. */
9239 : 541204 : gcc_assert (costing_p
9240 : : || useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
9241 : 1294645 : bool simd_lane_access_p
9242 : 1294645 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
9243 : 1294645 : if (!costing_p
9244 : 1294645 : && simd_lane_access_p
9245 : 4374 : && !loop_masks
9246 : 4374 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
9247 : 4374 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
9248 : 4374 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
9249 : 4374 : && integer_zerop (DR_INIT (first_dr_info->dr))
9250 : 1299019 : && alias_sets_conflict_p (get_alias_set (aggr_type),
9251 : 4374 : get_alias_set (TREE_TYPE (ref_type))))
9252 : : {
9253 : 4366 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
9254 : 4366 : dataref_offset = build_int_cst (ref_type, 0);
9255 : : }
9256 : 1290279 : else if (!costing_p)
9257 : 1073668 : dataref_ptr = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
9258 : : simd_lane_access_p ? loop : NULL,
9259 : : offset, &dummy, gsi, &ptr_incr,
9260 : : simd_lane_access_p, bump);
9261 : :
9262 : 1294645 : new_stmt = NULL;
9263 : 1294645 : gcc_assert (!grouped_store);
9264 : 2877374 : for (i = 0; i < vec_num; i++)
9265 : : {
9266 : 1582729 : if (!costing_p)
9267 : 669719 : vec_oprnd = vec_oprnds[i];
9268 : :
9269 : 1582729 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
9270 : : {
9271 : 3140 : if (costing_p)
9272 : 2013 : inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
9273 : : slp_node, 0, vect_body);
9274 : : else
9275 : : {
9276 : 1127 : tree perm_mask = perm_mask_for_reverse (vectype);
9277 : 1127 : tree new_temp = make_ssa_name (vectype);
9278 : :
9279 : : /* Generate the permute statement. */
9280 : 1127 : gimple *perm_stmt
9281 : 1127 : = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
9282 : : vec_oprnd, perm_mask);
9283 : 1127 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9284 : :
9285 : 1127 : perm_stmt = SSA_NAME_DEF_STMT (new_temp);
9286 : 1582729 : vec_oprnd = new_temp;
9287 : : }
9288 : : }
9289 : :
9290 : 1582729 : if (costing_p)
9291 : : {
9292 : 913010 : n_adjacent_stores++;
9293 : 913010 : continue;
9294 : : }
9295 : :
9296 : 669719 : tree final_mask = NULL_TREE;
9297 : 669719 : tree final_len = NULL_TREE;
9298 : 669719 : tree bias = NULL_TREE;
9299 : 669719 : if (loop_masks)
9300 : 76 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9301 : : vec_num, vectype, i);
9302 : 669719 : if (vec_mask)
9303 : 649 : vec_mask = vec_masks[i];
9304 : 649 : if (vec_mask)
9305 : 649 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9306 : : vec_mask, gsi);
9307 : :
9308 : 669719 : if (i > 0)
9309 : : /* Bump the vector pointer. */
9310 : 128515 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9311 : : stmt_info, bump);
9312 : :
9313 : 669719 : unsigned misalign;
9314 : 669719 : unsigned HOST_WIDE_INT align;
9315 : 669719 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
9316 : 669719 : if (alignment_support_scheme == dr_aligned)
9317 : : misalign = 0;
9318 : 307767 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
9319 : : {
9320 : 157752 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
9321 : 157752 : misalign = 0;
9322 : : }
9323 : : else
9324 : 150015 : misalign = misalignment;
9325 : 669719 : if (dataref_offset == NULL_TREE
9326 : 664339 : && TREE_CODE (dataref_ptr) == SSA_NAME)
9327 : 176895 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
9328 : 669719 : align = least_bit_hwi (misalign | align);
9329 : :
9330 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
9331 : 669719 : machine_mode vmode = TYPE_MODE (vectype);
9332 : 669719 : machine_mode new_vmode = vmode;
9333 : 669719 : internal_fn partial_ifn = IFN_LAST;
9334 : 669719 : if (loop_lens)
9335 : : {
9336 : 0 : opt_machine_mode new_ovmode
9337 : 0 : = get_len_load_store_mode (vmode, false, &partial_ifn);
9338 : 0 : new_vmode = new_ovmode.require ();
9339 : 0 : unsigned factor
9340 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
9341 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
9342 : : vec_num, vectype, i, factor);
9343 : : }
9344 : 669719 : else if (final_mask)
9345 : : {
9346 : 661 : if (!can_vec_mask_load_store_p (vmode,
9347 : 661 : TYPE_MODE (TREE_TYPE (final_mask)),
9348 : : false, &partial_ifn))
9349 : 0 : gcc_unreachable ();
9350 : : }
9351 : :
9352 : 669719 : if (partial_ifn == IFN_MASK_LEN_STORE)
9353 : : {
9354 : 0 : if (!final_len)
9355 : : {
9356 : : /* Pass VF value to 'len' argument of
9357 : : MASK_LEN_STORE if LOOP_LENS is invalid. */
9358 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9359 : : }
9360 : 0 : if (!final_mask)
9361 : : {
9362 : : /* Pass all ones value to 'mask' argument of
9363 : : MASK_LEN_STORE if final_mask is invalid. */
9364 : 0 : mask_vectype = truth_type_for (vectype);
9365 : 0 : final_mask = build_minus_one_cst (mask_vectype);
9366 : : }
9367 : : }
9368 : 669719 : if (final_len)
9369 : : {
9370 : 0 : signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9371 : 0 : bias = build_int_cst (intQI_type_node, biasval);
9372 : : }
9373 : :
9374 : : /* Arguments are ready. Create the new vector stmt. */
9375 : 669719 : if (final_len)
9376 : : {
9377 : 0 : gcall *call;
9378 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9379 : : /* Need conversion if it's wrapped with VnQI. */
9380 : 0 : if (vmode != new_vmode)
9381 : : {
9382 : 0 : tree new_vtype
9383 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
9384 : : new_vmode);
9385 : 0 : tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
9386 : 0 : vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
9387 : 0 : gassign *new_stmt
9388 : 0 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
9389 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9390 : 0 : vec_oprnd = var;
9391 : : }
9392 : :
9393 : 0 : if (partial_ifn == IFN_MASK_LEN_STORE)
9394 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
9395 : : dataref_ptr, ptr, final_mask,
9396 : : final_len, bias, vec_oprnd);
9397 : : else
9398 : 0 : call = gimple_build_call_internal (IFN_LEN_STORE, 5,
9399 : : dataref_ptr, ptr, final_len,
9400 : : bias, vec_oprnd);
9401 : 0 : gimple_call_set_nothrow (call, true);
9402 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9403 : 0 : new_stmt = call;
9404 : : }
9405 : 669719 : else if (final_mask)
9406 : : {
9407 : 661 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9408 : 661 : gcall *call
9409 : 661 : = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
9410 : : ptr, final_mask, vec_oprnd);
9411 : 661 : gimple_call_set_nothrow (call, true);
9412 : 661 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9413 : 661 : new_stmt = call;
9414 : : }
9415 : : else
9416 : : {
9417 : 669058 : data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr,
9418 : : dataref_offset ? dataref_offset
9419 : : : build_int_cst (ref_type, 0));
9420 : 669058 : if (alignment_support_scheme == dr_aligned
9421 : 669058 : && align >= TYPE_ALIGN_UNIT (vectype))
9422 : : ;
9423 : : else
9424 : 307276 : TREE_TYPE (data_ref)
9425 : 614552 : = build_aligned_type (TREE_TYPE (data_ref),
9426 : : align * BITS_PER_UNIT);
9427 : 669058 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
9428 : 669058 : new_stmt = gimple_build_assign (data_ref, vec_oprnd);
9429 : 669058 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9430 : : }
9431 : : }
9432 : :
9433 : 1294645 : if (costing_p)
9434 : : {
9435 : 753441 : if (n_adjacent_stores > 0)
9436 : 753441 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9437 : : alignment_support_scheme, misalignment,
9438 : : &inside_cost, cost_vec);
9439 : :
9440 : : /* When vectorizing a store into the function result assign
9441 : : a penalty if the function returns in a multi-register location.
9442 : : In this case we assume we'll end up with having to spill the
9443 : : vector result and do piecewise loads as a conservative estimate. */
9444 : 753441 : tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
9445 : 753441 : if (base
9446 : 753441 : && (TREE_CODE (base) == RESULT_DECL
9447 : 702921 : || (DECL_P (base) && cfun_returns (base)))
9448 : 815821 : && !aggregate_value_p (base, cfun->decl))
9449 : : {
9450 : 11285 : rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
9451 : : /* ??? Handle PARALLEL in some way. */
9452 : 11285 : if (REG_P (reg))
9453 : : {
9454 : 11086 : int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
9455 : : /* Assume that a single reg-reg move is possible and cheap,
9456 : : do not account for vector to gp register move cost. */
9457 : 11086 : if (nregs > 1)
9458 : : {
9459 : : /* Spill. */
9460 : 10214 : prologue_cost
9461 : 10214 : += record_stmt_cost (cost_vec, 1, vector_store,
9462 : : slp_node, 0, vect_epilogue);
9463 : : /* Loads. */
9464 : 10214 : prologue_cost
9465 : 10214 : += record_stmt_cost (cost_vec, nregs, scalar_load,
9466 : : slp_node, 0, vect_epilogue);
9467 : : }
9468 : : }
9469 : : }
9470 : 753441 : if (dump_enabled_p ())
9471 : 13051 : dump_printf_loc (MSG_NOTE, vect_location,
9472 : : "vect_model_store_cost: inside_cost = %d, "
9473 : : "prologue_cost = %d .\n",
9474 : : inside_cost, prologue_cost);
9475 : : }
9476 : :
9477 : 1294645 : return true;
9478 : 2622147 : }
9479 : :
9480 : : /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
9481 : : VECTOR_CST mask. No checks are made that the target platform supports the
9482 : : mask, so callers may wish to test can_vec_perm_const_p separately, or use
9483 : : vect_gen_perm_mask_checked. */
9484 : :
9485 : : tree
9486 : 58550 : vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
9487 : : {
9488 : 58550 : tree mask_type;
9489 : :
9490 : 58550 : poly_uint64 nunits = sel.length ();
9491 : 58550 : gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
9492 : :
9493 : 58550 : mask_type = build_vector_type (ssizetype, nunits);
9494 : 58550 : return vec_perm_indices_to_tree (mask_type, sel);
9495 : : }
9496 : :
9497 : : /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
9498 : : i.e. that the target supports the pattern _for arbitrary input vectors_. */
9499 : :
9500 : : tree
9501 : 55837 : vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
9502 : : {
9503 : 55837 : machine_mode vmode = TYPE_MODE (vectype);
9504 : 55837 : gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
9505 : 55837 : return vect_gen_perm_mask_any (vectype, sel);
9506 : : }
9507 : :
9508 : : /* Given a vector variable X and Y, that was generated for the scalar
9509 : : STMT_INFO, generate instructions to permute the vector elements of X and Y
9510 : : using permutation mask MASK_VEC, insert them at *GSI and return the
9511 : : permuted vector variable. */
9512 : :
9513 : : static tree
9514 : 1415 : permute_vec_elements (vec_info *vinfo,
9515 : : tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
9516 : : gimple_stmt_iterator *gsi)
9517 : : {
9518 : 1415 : tree vectype = TREE_TYPE (x);
9519 : 1415 : tree perm_dest, data_ref;
9520 : 1415 : gimple *perm_stmt;
9521 : :
9522 : 1415 : tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
9523 : 1415 : if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
9524 : 1415 : perm_dest = vect_create_destination_var (scalar_dest, vectype);
9525 : : else
9526 : 0 : perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
9527 : 1415 : data_ref = make_ssa_name (perm_dest);
9528 : :
9529 : : /* Generate the permute statement. */
9530 : 1415 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
9531 : 1415 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9532 : :
9533 : 1415 : return data_ref;
9534 : : }
9535 : :
9536 : : /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
9537 : : inserting them on the loops preheader edge. Returns true if we
9538 : : were successful in doing so (and thus STMT_INFO can be moved then),
9539 : : otherwise returns false. HOIST_P indicates if we want to hoist the
9540 : : definitions of all SSA uses, it would be false when we are costing. */
9541 : :
9542 : : static bool
9543 : 4439 : hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
9544 : : {
9545 : 4439 : ssa_op_iter i;
9546 : 4439 : use_operand_p use_p;
9547 : 4439 : auto_vec<use_operand_p, 8> to_hoist;
9548 : :
9549 : 8504 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
9550 : : {
9551 : 4073 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9552 : 4073 : if (!gimple_nop_p (def_stmt)
9553 : 4073 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
9554 : : {
9555 : : /* Make sure we don't need to recurse. While we could do
9556 : : so in simple cases when there are more complex use webs
9557 : : we don't have an easy way to preserve stmt order to fulfil
9558 : : dependencies within them. */
9559 : 47 : tree op2;
9560 : 47 : ssa_op_iter i2;
9561 : 47 : if (gimple_code (def_stmt) == GIMPLE_PHI
9562 : 47 : || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
9563 : : == NULL_DEF_OPERAND_P))
9564 : 8 : return false;
9565 : 94 : FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
9566 : : {
9567 : 55 : gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
9568 : 55 : if (!gimple_nop_p (def_stmt2)
9569 : 55 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
9570 : : return false;
9571 : : }
9572 : 39 : to_hoist.safe_push (use_p);
9573 : : }
9574 : : }
9575 : :
9576 : 8862 : if (to_hoist.is_empty ())
9577 : : return true;
9578 : :
9579 : 39 : if (!hoist_p)
9580 : : return true;
9581 : :
9582 : : /* Instead of moving defs we copy them so we can zero their UID to not
9583 : : confuse dominance queries in the preheader. */
9584 : 9 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9585 : 36 : for (use_operand_p use_p : to_hoist)
9586 : : {
9587 : 9 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9588 : 9 : gimple *copy = gimple_copy (def_stmt);
9589 : 9 : gimple_set_uid (copy, 0);
9590 : 9 : def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
9591 : 9 : tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
9592 : 9 : update_stmt (copy);
9593 : 9 : def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
9594 : 9 : SET_DEF (def_p, new_def);
9595 : 9 : SET_USE (use_p, new_def);
9596 : 9 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
9597 : : }
9598 : :
9599 : : return true;
9600 : 4439 : }
9601 : :
9602 : : /* vectorizable_load.
9603 : :
9604 : : Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
9605 : : that can be vectorized.
9606 : : If COST_VEC is passed, calculate costs but don't change anything,
9607 : : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
9608 : : it, and insert it at GSI.
9609 : : Return true if STMT_INFO is vectorizable in this way. */
9610 : :
9611 : : static bool
9612 : 1933953 : vectorizable_load (vec_info *vinfo,
9613 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
9614 : : slp_tree slp_node,
9615 : : stmt_vector_for_cost *cost_vec)
9616 : : {
9617 : 1933953 : tree scalar_dest;
9618 : 1933953 : tree vec_dest = NULL;
9619 : 1933953 : tree data_ref = NULL;
9620 : 1933953 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
9621 : 1933953 : class loop *loop = NULL;
9622 : 1933953 : class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
9623 : 1933953 : bool nested_in_vect_loop = false;
9624 : 1933953 : tree elem_type;
9625 : : /* Avoid false positive uninitialized warning, see PR110652. */
9626 : 1933953 : tree new_temp = NULL_TREE;
9627 : 1933953 : machine_mode mode;
9628 : 1933953 : tree dummy;
9629 : 1933953 : tree dataref_ptr = NULL_TREE;
9630 : 1933953 : tree dataref_offset = NULL_TREE;
9631 : 1933953 : gimple *ptr_incr = NULL;
9632 : 1933953 : int i, j;
9633 : 1933953 : unsigned int group_size;
9634 : 1933953 : poly_uint64 group_gap_adj;
9635 : 1933953 : tree msq = NULL_TREE, lsq;
9636 : 1933953 : tree realignment_token = NULL_TREE;
9637 : 1933953 : gphi *phi = NULL;
9638 : 1933953 : bool grouped_load = false;
9639 : 1933953 : stmt_vec_info first_stmt_info;
9640 : 1933953 : stmt_vec_info first_stmt_info_for_drptr = NULL;
9641 : 1933953 : bool compute_in_loop = false;
9642 : 1933953 : class loop *at_loop;
9643 : 1933953 : int vec_num;
9644 : 1933953 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
9645 : 1933953 : poly_uint64 vf;
9646 : 1933953 : tree aggr_type;
9647 : 1933953 : tree ref_type;
9648 : 1933953 : enum vect_def_type mask_dt = vect_unknown_def_type;
9649 : 1933953 : enum vect_def_type els_dt = vect_unknown_def_type;
9650 : :
9651 : 1933953 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
9652 : : return false;
9653 : :
9654 : 1933953 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
9655 : 179117 : && cost_vec)
9656 : : return false;
9657 : :
9658 : 1754836 : if (!STMT_VINFO_DATA_REF (stmt_info))
9659 : : return false;
9660 : :
9661 : 1411508 : tree mask_vectype = NULL_TREE;
9662 : 1411508 : tree els = NULL_TREE; tree els_vectype = NULL_TREE;
9663 : :
9664 : 1411508 : int mask_index = -1;
9665 : 1411508 : int els_index = -1;
9666 : 1411508 : slp_tree mask_node = NULL;
9667 : 1411508 : slp_tree els_op = NULL;
9668 : 1411508 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
9669 : : {
9670 : 1408227 : scalar_dest = gimple_assign_lhs (assign);
9671 : 1408227 : if (TREE_CODE (scalar_dest) != SSA_NAME)
9672 : : return false;
9673 : :
9674 : 627099 : tree_code code = gimple_assign_rhs_code (assign);
9675 : 627099 : if (code != ARRAY_REF
9676 : 627099 : && code != BIT_FIELD_REF
9677 : 627099 : && code != INDIRECT_REF
9678 : 426453 : && code != COMPONENT_REF
9679 : 426453 : && code != IMAGPART_EXPR
9680 : 286897 : && code != REALPART_EXPR
9681 : 286897 : && code != MEM_REF
9682 : 205 : && TREE_CODE_CLASS (code) != tcc_declaration)
9683 : : return false;
9684 : : }
9685 : : else
9686 : : {
9687 : 1308137 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
9688 : 3281 : if (!call || !gimple_call_internal_p (call))
9689 : : return false;
9690 : :
9691 : 3281 : internal_fn ifn = gimple_call_internal_fn (call);
9692 : 3281 : if (!internal_load_fn_p (ifn))
9693 : : return false;
9694 : :
9695 : 2375 : scalar_dest = gimple_call_lhs (call);
9696 : 2375 : if (!scalar_dest)
9697 : : return false;
9698 : :
9699 : 2375 : mask_index = internal_fn_mask_index (ifn);
9700 : 2375 : if (mask_index >= 0)
9701 : 2375 : mask_index = vect_slp_child_index_for_operand
9702 : 2375 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9703 : 2375 : if (mask_index >= 0
9704 : 2375 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
9705 : : &mask_node, &mask_dt, &mask_vectype))
9706 : : return false;
9707 : :
9708 : 2375 : els_index = internal_fn_else_index (ifn);
9709 : 2375 : if (els_index >= 0)
9710 : 2375 : els_index = vect_slp_child_index_for_operand
9711 : 2375 : (call, els_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9712 : 2375 : if (els_index >= 0
9713 : 2375 : && !vect_is_simple_use (vinfo, slp_node, els_index,
9714 : : &els, &els_op, &els_dt, &els_vectype))
9715 : : return false;
9716 : : }
9717 : :
9718 : 629407 : tree vectype = SLP_TREE_VECTYPE (slp_node);
9719 : 629407 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9720 : :
9721 : 629407 : if (loop_vinfo)
9722 : : {
9723 : 415272 : loop = LOOP_VINFO_LOOP (loop_vinfo);
9724 : 415272 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
9725 : 415272 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
9726 : : }
9727 : : else
9728 : : vf = 1;
9729 : :
9730 : 629407 : vec_num = vect_get_num_copies (vinfo, slp_node);
9731 : :
9732 : : /* FORNOW. This restriction should be relaxed. */
9733 : 629407 : if (nested_in_vect_loop && vec_num > 1)
9734 : : {
9735 : 298 : if (dump_enabled_p ())
9736 : 66 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9737 : : "multiple types in nested loop.\n");
9738 : 298 : return false;
9739 : : }
9740 : :
9741 : 629109 : elem_type = TREE_TYPE (vectype);
9742 : 629109 : mode = TYPE_MODE (vectype);
9743 : :
9744 : : /* FORNOW. In some cases can vectorize even if data-type not supported
9745 : : (e.g. - data copies). */
9746 : 629109 : if (!can_implement_p (mov_optab, mode))
9747 : : {
9748 : 0 : if (dump_enabled_p ())
9749 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9750 : : "Aligned load, but unsupported type.\n");
9751 : 0 : return false;
9752 : : }
9753 : :
9754 : : /* Check if the load is a part of an interleaving chain. */
9755 : 629109 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
9756 : : {
9757 : 300215 : grouped_load = true;
9758 : : /* FORNOW */
9759 : 300215 : gcc_assert (!nested_in_vect_loop);
9760 : 300215 : gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9761 : :
9762 : 300215 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
9763 : 300215 : group_size = DR_GROUP_SIZE (first_stmt_info);
9764 : :
9765 : : /* Invalidate assumptions made by dependence analysis when vectorization
9766 : : on the unrolled body effectively re-orders stmts. */
9767 : 300215 : if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
9768 : 300215 : && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
9769 : : STMT_VINFO_MIN_NEG_DIST (stmt_info)))
9770 : : {
9771 : 12 : if (dump_enabled_p ())
9772 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9773 : : "cannot perform implicit CSE when performing "
9774 : : "group loads with negative dependence distance\n");
9775 : 12 : return false;
9776 : : }
9777 : : }
9778 : : else
9779 : : group_size = 1;
9780 : :
9781 : 629097 : vect_load_store_data _ls_data{};
9782 : 629097 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
9783 : 629097 : if (cost_vec
9784 : 629097 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
9785 : : VLS_LOAD, &ls))
9786 : : return false;
9787 : : /* Temporary aliases to analysis data, should not be modified through
9788 : : these. */
9789 : 538923 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
9790 : 538923 : const dr_alignment_support alignment_support_scheme
9791 : : = ls.alignment_support_scheme;
9792 : 538923 : const int misalignment = ls.misalignment;
9793 : 538923 : const poly_int64 poffset = ls.poffset;
9794 : 538923 : const vec<int> &elsvals = ls.elsvals;
9795 : :
9796 : 538923 : int maskload_elsval = 0;
9797 : 538923 : bool need_zeroing = false;
9798 : :
9799 : : /* We might need to explicitly zero inactive elements if there are
9800 : : padding bits in the type that might leak otherwise.
9801 : : Refer to PR115336. */
9802 : 538923 : tree scalar_type = TREE_TYPE (scalar_dest);
9803 : 538923 : bool type_mode_padding_p
9804 : 1077846 : = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
9805 : :
9806 : 538923 : if (slp_node->ldst_lanes
9807 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
9808 : : {
9809 : 0 : if (dump_enabled_p ())
9810 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9811 : : "discovered load-lane but cannot use it.\n");
9812 : 0 : return false;
9813 : : }
9814 : :
9815 : 538923 : if (mask_node)
9816 : : {
9817 : 2255 : if (memory_access_type == VMAT_CONTIGUOUS)
9818 : : {
9819 : 1473 : machine_mode vec_mode = TYPE_MODE (vectype);
9820 : 395 : if (!VECTOR_MODE_P (vec_mode)
9821 : 2946 : || !can_vec_mask_load_store_p (vec_mode,
9822 : 1473 : TYPE_MODE (mask_vectype),
9823 : : true, NULL, &ls.elsvals))
9824 : 67 : return false;
9825 : : }
9826 : 782 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
9827 : 782 : && !mat_gather_scatter_p (memory_access_type))
9828 : : {
9829 : 62 : if (dump_enabled_p ())
9830 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9831 : : "unsupported access type for masked load.\n");
9832 : 62 : return false;
9833 : : }
9834 : 720 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
9835 : : {
9836 : 476 : if (dump_enabled_p ())
9837 : 26 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9838 : : "unsupported masked emulated gather.\n");
9839 : 476 : return false;
9840 : : }
9841 : : else if (memory_access_type == VMAT_ELEMENTWISE
9842 : : || memory_access_type == VMAT_STRIDED_SLP)
9843 : : {
9844 : : if (dump_enabled_p ())
9845 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9846 : : "unsupported masked strided access.\n");
9847 : : return false;
9848 : : }
9849 : : }
9850 : :
9851 : 538318 : bool costing_p = cost_vec;
9852 : :
9853 : 538318 : if (costing_p) /* transformation not required. */
9854 : : {
9855 : 375590 : if (mask_node
9856 : 375590 : && !vect_maybe_update_slp_op_vectype (mask_node,
9857 : : mask_vectype))
9858 : : {
9859 : 0 : if (dump_enabled_p ())
9860 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9861 : : "incompatible vector types for invariants\n");
9862 : 0 : return false;
9863 : : }
9864 : :
9865 : 375590 : if (loop_vinfo
9866 : 251217 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
9867 : 171874 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
9868 : : VLS_LOAD, group_size, &ls,
9869 : : mask_node, &ls.elsvals);
9870 : :
9871 : 375590 : if (dump_enabled_p ()
9872 : 23958 : && memory_access_type != VMAT_ELEMENTWISE
9873 : 23850 : && !mat_gather_scatter_p (memory_access_type)
9874 : 23628 : && memory_access_type != VMAT_STRIDED_SLP
9875 : 23628 : && memory_access_type != VMAT_INVARIANT
9876 : 398380 : && alignment_support_scheme != dr_aligned)
9877 : 9027 : dump_printf_loc (MSG_NOTE, vect_location,
9878 : : "Vectorizing an unaligned access.\n");
9879 : :
9880 : 375590 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
9881 : 0 : vinfo->any_known_not_updated_vssa = true;
9882 : :
9883 : 375590 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
9884 : 375590 : slp_node->data = new vect_load_store_data (std::move (ls));
9885 : : }
9886 : :
9887 : : /* If the type needs padding we must zero inactive elements.
9888 : : Check if we can do that with a VEC_COND_EXPR and store the
9889 : : elsval we choose in MASKLOAD_ELSVAL. */
9890 : 538318 : if (elsvals.length ()
9891 : 23111 : && type_mode_padding_p
9892 : 3 : && !elsvals.contains (MASK_LOAD_ELSE_ZERO)
9893 : 23111 : && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
9894 : : {
9895 : 0 : if (dump_enabled_p ())
9896 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9897 : : "cannot zero inactive elements.\n");
9898 : 0 : return false;
9899 : : }
9900 : :
9901 : : /* For now just use the first available else value.
9902 : : get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
9903 : : select it here if it is supported. */
9904 : 538318 : if (elsvals.length ())
9905 : 23111 : maskload_elsval = *elsvals.begin ();
9906 : :
9907 : 538318 : if (dump_enabled_p () && !costing_p)
9908 : 16100 : dump_printf_loc (MSG_NOTE, vect_location, "transform load.\n");
9909 : :
9910 : : /* Transform. */
9911 : :
9912 : 538318 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
9913 : 538318 : ensure_base_align (dr_info);
9914 : :
9915 : 538318 : if (memory_access_type == VMAT_INVARIANT)
9916 : : {
9917 : 3780 : gcc_assert (!grouped_load && !mask_node && !bb_vinfo);
9918 : : /* If we have versioned for aliasing or the loop doesn't
9919 : : have any data dependencies that would preclude this,
9920 : : then we are sure this is a loop invariant load and
9921 : : thus we can insert it on the preheader edge.
9922 : : TODO: hoist_defs_of_uses should ideally be computed
9923 : : once at analysis time, remembered and used in the
9924 : : transform time. */
9925 : 7560 : bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
9926 : 3780 : && !nested_in_vect_loop);
9927 : 3780 : bool uniform_p = true;
9928 : 15704 : for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
9929 : : {
9930 : 4364 : hoist_p = hoist_p && hoist_defs_of_uses (sinfo->stmt, loop, false);
9931 : 4364 : if (sinfo != SLP_TREE_SCALAR_STMTS (slp_node)[0])
9932 : 155 : uniform_p = false;
9933 : : }
9934 : 3780 : if (costing_p)
9935 : : {
9936 : 2963 : if (!uniform_p && (!hoist_p || !vf.is_constant ()))
9937 : : {
9938 : 0 : if (dump_enabled_p ())
9939 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9940 : : "not vectorizing non-uniform invariant "
9941 : : "load\n");
9942 : 0 : return false;
9943 : : }
9944 : 448 : enum vect_cost_model_location cost_loc
9945 : 2963 : = hoist_p ? vect_prologue : vect_body;
9946 : 2963 : unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
9947 : : slp_node, 0, cost_loc);
9948 : 2963 : cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
9949 : : slp_node, 0, cost_loc);
9950 : 2963 : unsigned int prologue_cost = hoist_p ? cost : 0;
9951 : 448 : unsigned int inside_cost = hoist_p ? 0 : cost;
9952 : 2963 : if (dump_enabled_p ())
9953 : 451 : dump_printf_loc (MSG_NOTE, vect_location,
9954 : : "vect_model_load_cost: inside_cost = %d, "
9955 : : "prologue_cost = %d .\n",
9956 : : inside_cost, prologue_cost);
9957 : 2963 : return true;
9958 : : }
9959 : 817 : if (hoist_p)
9960 : : {
9961 : : /* ??? For non-uniform lanes there could be still duplicates.
9962 : : We're leaving those to post-vectorizer CSE for the moment. */
9963 : 629 : auto_vec<tree> scalar_defs (SLP_TREE_LANES (slp_node));
9964 : 1988 : for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
9965 : : {
9966 : 703 : gassign *stmt = as_a <gassign *> (sinfo->stmt);
9967 : 703 : if (dump_enabled_p ())
9968 : 294 : dump_printf_loc (MSG_NOTE, vect_location,
9969 : : "hoisting out of the vectorized loop: %G",
9970 : : (gimple *) stmt);
9971 : 703 : scalar_dest = copy_ssa_name (gimple_assign_lhs (stmt));
9972 : 703 : tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
9973 : 703 : edge pe = loop_preheader_edge (loop);
9974 : 703 : gphi *vphi = get_virtual_phi (loop->header);
9975 : 703 : tree vuse;
9976 : 703 : if (vphi)
9977 : 695 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
9978 : : else
9979 : 8 : vuse = gimple_vuse (gsi_stmt (*gsi));
9980 : 703 : gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
9981 : 703 : gimple_set_vuse (new_stmt, vuse);
9982 : 703 : gsi_insert_on_edge_immediate (pe, new_stmt);
9983 : 703 : hoist_defs_of_uses (new_stmt, loop, true);
9984 : 703 : if (!useless_type_conversion_p (TREE_TYPE (vectype),
9985 : 703 : TREE_TYPE (scalar_dest)))
9986 : : {
9987 : 12 : tree tem = make_ssa_name (TREE_TYPE (vectype));
9988 : 12 : new_stmt = gimple_build_assign (tem,
9989 : : NOP_EXPR, scalar_dest);
9990 : 12 : gsi_insert_on_edge_immediate (pe, new_stmt);
9991 : 12 : scalar_dest = tem;
9992 : : }
9993 : 703 : scalar_defs.quick_push (scalar_dest);
9994 : 703 : if (uniform_p)
9995 : : break;
9996 : : }
9997 : 629 : if (!uniform_p)
9998 : : {
9999 : 27 : unsigned const_nunits
10000 : 27 : = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
10001 : 68 : for (j = 0; j < (int) vec_num; ++j)
10002 : : {
10003 : 41 : vec<constructor_elt, va_gc> *v = NULL;
10004 : 41 : vec_safe_reserve (v, const_nunits, true);
10005 : 293 : for (unsigned i = 0; i < const_nunits; ++i)
10006 : : {
10007 : 252 : unsigned def_idx
10008 : 252 : = (j * const_nunits + i) % SLP_TREE_LANES (slp_node);
10009 : 252 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
10010 : : scalar_defs[def_idx]);
10011 : : }
10012 : 41 : scalar_dest = build_constructor (vectype, v);
10013 : 41 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10014 : : vectype, NULL);
10015 : 41 : slp_node->push_vec_def (new_temp);
10016 : : }
10017 : 27 : return true;
10018 : : }
10019 : 602 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10020 : : vectype, NULL);
10021 : 629 : }
10022 : : else
10023 : : {
10024 : 188 : gcc_assert (uniform_p);
10025 : 188 : gimple_stmt_iterator gsi2 = *gsi;
10026 : 188 : gsi_next (&gsi2);
10027 : 188 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10028 : : vectype, &gsi2);
10029 : : }
10030 : 1656 : for (j = 0; j < (int) vec_num; ++j)
10031 : 866 : slp_node->push_vec_def (new_temp);
10032 : : return true;
10033 : : }
10034 : :
10035 : 534538 : if (memory_access_type == VMAT_ELEMENTWISE
10036 : 534538 : || memory_access_type == VMAT_STRIDED_SLP)
10037 : : {
10038 : 27384 : gimple_stmt_iterator incr_gsi;
10039 : 27384 : bool insert_after;
10040 : 27384 : tree offvar = NULL_TREE;
10041 : 27384 : tree ivstep;
10042 : 27384 : tree running_off;
10043 : 27384 : vec<constructor_elt, va_gc> *v = NULL;
10044 : 27384 : tree stride_base, stride_step, alias_off;
10045 : : /* Checked by get_load_store_type. */
10046 : 27384 : unsigned int const_nunits = nunits.to_constant ();
10047 : 27384 : unsigned HOST_WIDE_INT cst_offset = 0;
10048 : 27384 : tree dr_offset;
10049 : 27384 : unsigned int inside_cost = 0;
10050 : :
10051 : 27384 : gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
10052 : 27384 : gcc_assert (!nested_in_vect_loop);
10053 : :
10054 : 27384 : if (grouped_load)
10055 : : {
10056 : : /* If we elided a consecutive load permutation, don't
10057 : : use the original first statement (which could be elided)
10058 : : but the one the load permutation starts with.
10059 : : This ensures the stride_base below is correct. */
10060 : 14841 : if (!ls.subchain_p)
10061 : 14809 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10062 : : else
10063 : 32 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10064 : 14841 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10065 : 14841 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10066 : : }
10067 : : else
10068 : : {
10069 : 12543 : first_stmt_info = stmt_info;
10070 : 12543 : first_dr_info = dr_info;
10071 : 12543 : ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
10072 : : }
10073 : :
10074 : 27384 : if (grouped_load)
10075 : : {
10076 : 14841 : if (memory_access_type == VMAT_STRIDED_SLP)
10077 : : {
10078 : : /* If we elided a consecutive load permutation, adjust
10079 : : the group size here. */
10080 : 3681 : if (!ls.subchain_p)
10081 : 3649 : group_size = DR_GROUP_SIZE (first_stmt_info);
10082 : : else
10083 : 32 : group_size = SLP_TREE_LANES (slp_node);
10084 : : }
10085 : : else /* VMAT_ELEMENTWISE */
10086 : 11160 : group_size = SLP_TREE_LANES (slp_node);
10087 : : }
10088 : : else
10089 : : group_size = 1;
10090 : :
10091 : 27384 : if (!costing_p)
10092 : : {
10093 : 3218 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
10094 : 3218 : stride_base = fold_build_pointer_plus (
10095 : : DR_BASE_ADDRESS (first_dr_info->dr),
10096 : : size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
10097 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
10098 : 3218 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
10099 : :
10100 : : /* For a load with loop-invariant (but other than power-of-2)
10101 : : stride (i.e. not a grouped access) like so:
10102 : :
10103 : : for (i = 0; i < n; i += stride)
10104 : : ... = array[i];
10105 : :
10106 : : we generate a new induction variable and new accesses to
10107 : : form a new vector (or vectors, depending on ncopies):
10108 : :
10109 : : for (j = 0; ; j += VF*stride)
10110 : : tmp1 = array[j];
10111 : : tmp2 = array[j + stride];
10112 : : ...
10113 : : vectemp = {tmp1, tmp2, ...}
10114 : : */
10115 : :
10116 : 3218 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
10117 : : build_int_cst (TREE_TYPE (stride_step), vf));
10118 : :
10119 : 3218 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
10120 : :
10121 : 3218 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
10122 : 3218 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
10123 : 3218 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
10124 : : loop, &incr_gsi, insert_after,
10125 : : &offvar, NULL);
10126 : :
10127 : 3218 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
10128 : : }
10129 : :
10130 : 27384 : running_off = offvar;
10131 : 27384 : alias_off = build_int_cst (ref_type, 0);
10132 : 27384 : int nloads = const_nunits;
10133 : 27384 : int lnel = 1;
10134 : 27384 : tree ltype = TREE_TYPE (vectype);
10135 : 27384 : tree lvectype = vectype;
10136 : 27384 : auto_vec<tree> dr_chain;
10137 : : /* ??? Modify local copies of alignment_support_scheme and
10138 : : misalignment, but this part of analysis should be done
10139 : : earlier and remembered, likewise the chosen load mode. */
10140 : 27384 : const dr_alignment_support tem = alignment_support_scheme;
10141 : 27384 : dr_alignment_support alignment_support_scheme = tem;
10142 : 27384 : const int tem2 = misalignment;
10143 : 27384 : int misalignment = tem2;
10144 : 27384 : if (memory_access_type == VMAT_STRIDED_SLP)
10145 : : {
10146 : 16224 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
10147 : : /* Use the target vector type if the group size is a multiple
10148 : : of it. */
10149 : 16224 : if (n == const_nunits)
10150 : : {
10151 : 1904 : int mis_align = dr_misalignment (first_dr_info, vectype);
10152 : : /* With VF > 1 we advance the DR by step, if that is constant
10153 : : and only aligned when performed VF times, DR alignment
10154 : : analysis can analyze this as aligned since it assumes
10155 : : contiguous accesses. But that is not how we code generate
10156 : : here, so adjust for this. */
10157 : 1904 : if (maybe_gt (vf, 1u)
10158 : 3146 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10159 : 2942 : DR_TARGET_ALIGNMENT (first_dr_info)))
10160 : 204 : mis_align = -1;
10161 : 1904 : dr_alignment_support dr_align
10162 : 1904 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
10163 : : mis_align);
10164 : 1904 : if (dr_align == dr_aligned
10165 : 1904 : || dr_align == dr_unaligned_supported)
10166 : : {
10167 : 16224 : nloads = 1;
10168 : 16224 : lnel = const_nunits;
10169 : 16224 : ltype = vectype;
10170 : 16224 : alignment_support_scheme = dr_align;
10171 : 16224 : misalignment = mis_align;
10172 : : }
10173 : : }
10174 : : /* Else use the biggest vector we can load the group without
10175 : : accessing excess elements. */
10176 : 14320 : else if (n > 1)
10177 : : {
10178 : 1784 : tree ptype;
10179 : 1784 : tree vtype
10180 : 1784 : = vector_vector_composition_type (vectype, const_nunits / n,
10181 : : &ptype);
10182 : 1784 : if (vtype != NULL_TREE)
10183 : : {
10184 : 1748 : dr_alignment_support dr_align;
10185 : 1748 : int mis_align = 0;
10186 : 1748 : if (VECTOR_TYPE_P (ptype))
10187 : : {
10188 : 904 : mis_align = dr_misalignment (first_dr_info, ptype);
10189 : 904 : if (maybe_gt (vf, 1u)
10190 : 1780 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10191 : 910 : DR_TARGET_ALIGNMENT (first_dr_info)))
10192 : 870 : mis_align = -1;
10193 : 904 : dr_align
10194 : 904 : = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
10195 : : mis_align);
10196 : : }
10197 : : else
10198 : : dr_align = dr_unaligned_supported;
10199 : 1748 : if (dr_align == dr_aligned
10200 : 1748 : || dr_align == dr_unaligned_supported)
10201 : : {
10202 : 1748 : nloads = const_nunits / n;
10203 : 1748 : lnel = n;
10204 : 1748 : lvectype = vtype;
10205 : 1748 : ltype = ptype;
10206 : 1748 : alignment_support_scheme = dr_align;
10207 : 1748 : misalignment = mis_align;
10208 : : }
10209 : : }
10210 : : }
10211 : 16224 : unsigned align;
10212 : 16224 : if (alignment_support_scheme == dr_aligned)
10213 : 20 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
10214 : : else
10215 : 16204 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
10216 : : /* Alignment is at most the access size if we do multiple loads. */
10217 : 16224 : if (nloads > 1)
10218 : 14320 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
10219 : 16224 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
10220 : : }
10221 : :
10222 : : /* For SLP permutation support we need to load the whole group,
10223 : : not only the number of vector stmts the permutation result
10224 : : fits in. */
10225 : 27384 : int ncopies;
10226 : 27384 : if (ls.slp_perm)
10227 : : {
10228 : 2412 : gcc_assert (memory_access_type != VMAT_ELEMENTWISE);
10229 : : /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
10230 : : variable VF. */
10231 : 2412 : unsigned int const_vf = vf.to_constant ();
10232 : 2412 : ncopies = CEIL (group_size * const_vf, const_nunits);
10233 : 2412 : dr_chain.create (ncopies);
10234 : : }
10235 : : else
10236 : : ncopies = vec_num;
10237 : :
10238 : 27384 : unsigned int group_el = 0;
10239 : 27384 : unsigned HOST_WIDE_INT
10240 : 27384 : elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
10241 : 27384 : unsigned int n_groups = 0;
10242 : : /* For costing some adjacent vector loads, we'd like to cost with
10243 : : the total number of them once instead of cost each one by one. */
10244 : 27384 : unsigned int n_adjacent_loads = 0;
10245 : 64833 : for (j = 0; j < ncopies; j++)
10246 : : {
10247 : 37449 : if (nloads > 1 && !costing_p)
10248 : 2780 : vec_alloc (v, nloads);
10249 : : gimple *new_stmt = NULL;
10250 : 156093 : for (i = 0; i < nloads; i++)
10251 : : {
10252 : 118644 : if (costing_p)
10253 : : {
10254 : : /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
10255 : : avoid ICE, see PR110776. */
10256 : 108920 : if (VECTOR_TYPE_P (ltype)
10257 : 4700 : && memory_access_type != VMAT_ELEMENTWISE)
10258 : 4700 : n_adjacent_loads++;
10259 : : else
10260 : 104220 : inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
10261 : : slp_node, 0, vect_body);
10262 : 108920 : continue;
10263 : : }
10264 : 9724 : unsigned int load_el = group_el;
10265 : : /* For elementwise accesses apply a load permutation directly. */
10266 : 9724 : if (memory_access_type == VMAT_ELEMENTWISE
10267 : 9724 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10268 : 1864 : load_el = SLP_TREE_LOAD_PERMUTATION (slp_node)[group_el];
10269 : 9724 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
10270 : 9724 : load_el * elsz + cst_offset);
10271 : 9724 : tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
10272 : 9724 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10273 : 9724 : new_temp = make_ssa_name (ltype);
10274 : 9724 : new_stmt = gimple_build_assign (new_temp, data_ref);
10275 : 9724 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10276 : 9724 : if (nloads > 1)
10277 : 8132 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
10278 : :
10279 : 9724 : group_el += lnel;
10280 : 9724 : if (group_el == group_size)
10281 : : {
10282 : 9329 : n_groups++;
10283 : : /* When doing SLP make sure to not load elements from
10284 : : the next vector iteration, those will not be accessed
10285 : : so just use the last element again. See PR107451. */
10286 : 9329 : if (known_lt (n_groups, vf))
10287 : : {
10288 : 6089 : tree newoff = copy_ssa_name (running_off);
10289 : 6089 : gimple *incr
10290 : 6089 : = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
10291 : : running_off, stride_step);
10292 : 6089 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
10293 : 6089 : running_off = newoff;
10294 : : }
10295 : : group_el = 0;
10296 : : }
10297 : : }
10298 : :
10299 : 37449 : if (nloads > 1)
10300 : : {
10301 : 28199 : if (costing_p)
10302 : 25419 : inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
10303 : : slp_node, 0, vect_body);
10304 : : else
10305 : : {
10306 : 2780 : tree vec_inv = build_constructor (lvectype, v);
10307 : 2780 : new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
10308 : : lvectype, gsi);
10309 : 2780 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
10310 : 2780 : if (lvectype != vectype)
10311 : : {
10312 : 239 : new_stmt
10313 : 239 : = gimple_build_assign (make_ssa_name (vectype),
10314 : : VIEW_CONVERT_EXPR,
10315 : : build1 (VIEW_CONVERT_EXPR,
10316 : : vectype, new_temp));
10317 : 239 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10318 : : gsi);
10319 : : }
10320 : : }
10321 : : }
10322 : 9250 : else if (!costing_p && ltype != vectype)
10323 : : {
10324 : 1567 : new_stmt = gimple_build_assign (make_ssa_name (vectype),
10325 : : VIEW_CONVERT_EXPR,
10326 : : build1 (VIEW_CONVERT_EXPR,
10327 : : vectype, new_temp));
10328 : 1567 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10329 : : gsi);
10330 : : }
10331 : :
10332 : 37449 : if (!costing_p)
10333 : : {
10334 : 4372 : if (ls.slp_perm)
10335 : 1338 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
10336 : : else
10337 : 3034 : slp_node->push_vec_def (new_stmt);
10338 : : }
10339 : : }
10340 : 27384 : if (ls.slp_perm)
10341 : : {
10342 : 2412 : if (costing_p)
10343 : : {
10344 : 1783 : gcc_assert (ls.n_perms != -1U);
10345 : 1783 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
10346 : : slp_node, 0, vect_body);
10347 : : }
10348 : : else
10349 : : {
10350 : 629 : unsigned n_perms2;
10351 : 629 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
10352 : : false, &n_perms2);
10353 : 629 : gcc_assert (ls.n_perms == n_perms2);
10354 : : }
10355 : : }
10356 : :
10357 : 27384 : if (costing_p)
10358 : : {
10359 : 24166 : if (n_adjacent_loads > 0)
10360 : 1794 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10361 : : alignment_support_scheme, misalignment, false,
10362 : : &inside_cost, nullptr, cost_vec, cost_vec,
10363 : : true);
10364 : 24166 : if (dump_enabled_p ())
10365 : 495 : dump_printf_loc (MSG_NOTE, vect_location,
10366 : : "vect_model_load_cost: inside_cost = %u, "
10367 : : "prologue_cost = 0 .\n",
10368 : : inside_cost);
10369 : : }
10370 : :
10371 : 27384 : return true;
10372 : 27384 : }
10373 : :
10374 : 507154 : if (mat_gather_scatter_p (memory_access_type)
10375 : 507154 : && !ls.ls_type)
10376 : : grouped_load = false;
10377 : :
10378 : 504457 : if (grouped_load
10379 : 507154 : || SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10380 : : {
10381 : 254371 : if (grouped_load)
10382 : : {
10383 : 254052 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10384 : 254052 : group_size = DR_GROUP_SIZE (first_stmt_info);
10385 : : }
10386 : : else
10387 : : {
10388 : : first_stmt_info = stmt_info;
10389 : : group_size = 1;
10390 : : }
10391 : : /* For SLP vectorization we directly vectorize a subchain
10392 : : without permutation. */
10393 : 254371 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10394 : 208350 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10395 : : /* For BB vectorization always use the first stmt to base
10396 : : the data ref pointer on. */
10397 : 254371 : if (bb_vinfo)
10398 : 207838 : first_stmt_info_for_drptr
10399 : 207838 : = vect_find_first_scalar_stmt_in_slp (slp_node);
10400 : :
10401 : 254371 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10402 : 254371 : group_gap_adj = 0;
10403 : :
10404 : : /* VEC_NUM is the number of vect stmts to be created for this group. */
10405 : 254371 : grouped_load = false;
10406 : : /* If an SLP permutation is from N elements to N elements,
10407 : : and if one vector holds a whole number of N, we can load
10408 : : the inputs to the permutation in the same way as an
10409 : : unpermuted sequence. In other cases we need to load the
10410 : : whole group, not only the number of vector stmts the
10411 : : permutation result fits in. */
10412 : 254371 : unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
10413 : 254371 : if (nested_in_vect_loop)
10414 : : /* We do not support grouped accesses in a nested loop,
10415 : : instead the access is contiguous but it might be
10416 : : permuted. No gap adjustment is needed though. */
10417 : : ;
10418 : 254369 : else if (ls.slp_perm
10419 : 254369 : && (group_size != scalar_lanes
10420 : 11179 : || !multiple_p (nunits, group_size)))
10421 : : {
10422 : : /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
10423 : : variable VF; see vect_transform_slp_perm_load. */
10424 : 35918 : unsigned int const_vf = vf.to_constant ();
10425 : 35918 : unsigned int const_nunits = nunits.to_constant ();
10426 : 35918 : vec_num = CEIL (group_size * const_vf, const_nunits);
10427 : 35918 : group_gap_adj = vf * group_size - nunits * vec_num;
10428 : : }
10429 : : else
10430 : : {
10431 : 218451 : group_gap_adj = group_size - scalar_lanes;
10432 : : }
10433 : :
10434 : 254371 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10435 : : }
10436 : : else
10437 : : {
10438 : 252783 : first_stmt_info = stmt_info;
10439 : 252783 : first_dr_info = dr_info;
10440 : 252783 : group_size = 1;
10441 : 252783 : group_gap_adj = 0;
10442 : 252783 : ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
10443 : : }
10444 : :
10445 : 507154 : vec_loop_masks *loop_masks
10446 : 299316 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
10447 : 507154 : ? &LOOP_VINFO_MASKS (loop_vinfo)
10448 : 29 : : NULL);
10449 : 29 : vec_loop_lens *loop_lens
10450 : 299316 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
10451 : : ? &LOOP_VINFO_LENS (loop_vinfo)
10452 : 0 : : NULL);
10453 : :
10454 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
10455 : : are some difference here. We cannot enable both the lens and masks
10456 : : during transform but it is allowed during analysis.
10457 : : Shouldn't go with length-based approach if fully masked. */
10458 : 507154 : if (cost_vec == NULL)
10459 : : /* The cost_vec is NULL during transfrom. */
10460 : 158693 : gcc_assert ((!loop_lens || !loop_masks));
10461 : :
10462 : : /* Targets with store-lane instructions must not require explicit
10463 : : realignment. vect_supportable_dr_alignment always returns either
10464 : : dr_aligned or dr_unaligned_supported for (non-length) masked
10465 : : operations. */
10466 : 507154 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
10467 : : && !mask_node
10468 : : && !loop_masks)
10469 : : || mat_gather_scatter_p (memory_access_type)
10470 : : || alignment_support_scheme == dr_aligned
10471 : : || alignment_support_scheme == dr_unaligned_supported);
10472 : :
10473 : : /* In case the vectorization factor (VF) is bigger than the number
10474 : : of elements that we can fit in a vectype (nunits), we have to generate
10475 : : more than one vector stmt - i.e - we need to "unroll" the
10476 : : vector stmt by a factor VF/nunits. In doing so, we record a pointer
10477 : : from one copy of the vector stmt to the next, in the field
10478 : : STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
10479 : : stages to find the correct vector defs to be used when vectorizing
10480 : : stmts that use the defs of the current stmt. The example below
10481 : : illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
10482 : : need to create 4 vectorized stmts):
10483 : :
10484 : : before vectorization:
10485 : : RELATED_STMT VEC_STMT
10486 : : S1: x = memref - -
10487 : : S2: z = x + 1 - -
10488 : :
10489 : : step 1: vectorize stmt S1:
10490 : : We first create the vector stmt VS1_0, and, as usual, record a
10491 : : pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
10492 : : Next, we create the vector stmt VS1_1, and record a pointer to
10493 : : it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
10494 : : Similarly, for VS1_2 and VS1_3. This is the resulting chain of
10495 : : stmts and pointers:
10496 : : RELATED_STMT VEC_STMT
10497 : : VS1_0: vx0 = memref0 VS1_1 -
10498 : : VS1_1: vx1 = memref1 VS1_2 -
10499 : : VS1_2: vx2 = memref2 VS1_3 -
10500 : : VS1_3: vx3 = memref3 - -
10501 : : S1: x = load - VS1_0
10502 : : S2: z = x + 1 - -
10503 : : */
10504 : :
10505 : : /* If the data reference is aligned (dr_aligned) or potentially unaligned
10506 : : on a target that supports unaligned accesses (dr_unaligned_supported)
10507 : : we generate the following code:
10508 : : p = initial_addr;
10509 : : indx = 0;
10510 : : loop {
10511 : : p = p + indx * vectype_size;
10512 : : vec_dest = *(p);
10513 : : indx = indx + 1;
10514 : : }
10515 : :
10516 : : Otherwise, the data reference is potentially unaligned on a target that
10517 : : does not support unaligned accesses (dr_explicit_realign_optimized) -
10518 : : then generate the following code, in which the data in each iteration is
10519 : : obtained by two vector loads, one from the previous iteration, and one
10520 : : from the current iteration:
10521 : : p1 = initial_addr;
10522 : : msq_init = *(floor(p1))
10523 : : p2 = initial_addr + VS - 1;
10524 : : realignment_token = call target_builtin;
10525 : : indx = 0;
10526 : : loop {
10527 : : p2 = p2 + indx * vectype_size
10528 : : lsq = *(floor(p2))
10529 : : vec_dest = realign_load (msq, lsq, realignment_token)
10530 : : indx = indx + 1;
10531 : : msq = lsq;
10532 : : } */
10533 : :
10534 : : /* If the misalignment remains the same throughout the execution of the
10535 : : loop, we can create the init_addr and permutation mask at the loop
10536 : : preheader. Otherwise, it needs to be created inside the loop.
10537 : : This can only occur when vectorizing memory accesses in the inner-loop
10538 : : nested within an outer-loop that is being vectorized. */
10539 : :
10540 : 507154 : if (nested_in_vect_loop
10541 : 507154 : && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
10542 : 1196 : GET_MODE_SIZE (TYPE_MODE (vectype))))
10543 : : {
10544 : 191 : gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
10545 : : compute_in_loop = true;
10546 : : }
10547 : :
10548 : 507154 : bool diff_first_stmt_info
10549 : 507154 : = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
10550 : :
10551 : 507154 : tree offset = NULL_TREE;
10552 : 507154 : if ((alignment_support_scheme == dr_explicit_realign_optimized
10553 : 507154 : || alignment_support_scheme == dr_explicit_realign)
10554 : 0 : && !compute_in_loop)
10555 : : {
10556 : : /* If we have different first_stmt_info, we can't set up realignment
10557 : : here, since we can't guarantee first_stmt_info DR has been
10558 : : initialized yet, use first_stmt_info_for_drptr DR by bumping the
10559 : : distance from first_stmt_info DR instead as below. */
10560 : 0 : if (!costing_p)
10561 : : {
10562 : 0 : if (!diff_first_stmt_info)
10563 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype, gsi,
10564 : : &realignment_token,
10565 : : alignment_support_scheme, NULL_TREE,
10566 : : &at_loop);
10567 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
10568 : : {
10569 : 0 : phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
10570 : 0 : offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
10571 : : size_one_node);
10572 : 0 : gcc_assert (!first_stmt_info_for_drptr);
10573 : : }
10574 : : }
10575 : : }
10576 : : else
10577 : 507154 : at_loop = loop;
10578 : :
10579 : 507154 : if (!known_eq (poffset, 0))
10580 : 4433 : offset = (offset
10581 : 4433 : ? size_binop (PLUS_EXPR, offset, size_int (poffset))
10582 : 4433 : : size_int (poffset));
10583 : :
10584 : 507154 : tree bump;
10585 : 507154 : tree vec_offset = NULL_TREE;
10586 : :
10587 : 507154 : auto_vec<tree> vec_offsets;
10588 : 507154 : auto_vec<tree> vec_masks;
10589 : 507154 : if (mask_node && !costing_p)
10590 : 617 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
10591 : : &vec_masks);
10592 : :
10593 : 507154 : tree vec_mask = NULL_TREE;
10594 : 507154 : tree vec_els = NULL_TREE;
10595 : 507154 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10596 : : {
10597 : 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
10598 : :
10599 : 0 : gcc_assert (alignment_support_scheme == dr_aligned
10600 : : || alignment_support_scheme == dr_unaligned_supported);
10601 : :
10602 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
10603 : 0 : if (!costing_p)
10604 : 0 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
10605 : : memory_access_type, loop_lens);
10606 : :
10607 : 0 : unsigned int inside_cost = 0, prologue_cost = 0;
10608 : : /* For costing some adjacent vector loads, we'd like to cost with
10609 : : the total number of them once instead of cost each one by one. */
10610 : 0 : unsigned int n_adjacent_loads = 0;
10611 : 0 : int ncopies = vec_num / group_size;
10612 : 0 : for (j = 0; j < ncopies; j++)
10613 : : {
10614 : 0 : if (costing_p)
10615 : : {
10616 : : /* An IFN_LOAD_LANES will load all its vector results,
10617 : : regardless of which ones we actually need. Account
10618 : : for the cost of unused results. */
10619 : 0 : if (first_stmt_info == stmt_info)
10620 : : {
10621 : 0 : unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
10622 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
10623 : 0 : do
10624 : : {
10625 : 0 : gaps -= 1;
10626 : 0 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
10627 : : }
10628 : 0 : while (next_stmt_info);
10629 : 0 : if (gaps)
10630 : : {
10631 : 0 : if (dump_enabled_p ())
10632 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10633 : : "vect_model_load_cost: %d "
10634 : : "unused vectors.\n",
10635 : : gaps);
10636 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
10637 : : alignment_support_scheme,
10638 : : misalignment, false, &inside_cost,
10639 : : &prologue_cost, cost_vec, cost_vec,
10640 : : true);
10641 : : }
10642 : : }
10643 : 0 : n_adjacent_loads++;
10644 : 0 : continue;
10645 : 0 : }
10646 : :
10647 : : /* 1. Create the vector or array pointer update chain. */
10648 : 0 : if (j == 0)
10649 : 0 : dataref_ptr
10650 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10651 : : at_loop, offset, &dummy, gsi,
10652 : : &ptr_incr, false, bump);
10653 : : else
10654 : : {
10655 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
10656 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
10657 : : stmt_info, bump);
10658 : : }
10659 : 0 : if (mask_node)
10660 : 0 : vec_mask = vec_masks[j];
10661 : :
10662 : 0 : tree vec_array = create_vector_array (vectype, group_size);
10663 : :
10664 : 0 : tree final_mask = NULL_TREE;
10665 : 0 : tree final_len = NULL_TREE;
10666 : 0 : tree bias = NULL_TREE;
10667 : 0 : if (loop_masks)
10668 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10669 : : ncopies, vectype, j);
10670 : 0 : if (vec_mask)
10671 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
10672 : : vec_mask, gsi);
10673 : :
10674 : 0 : if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
10675 : : {
10676 : 0 : if (loop_lens)
10677 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10678 : : ncopies, vectype, j, 1);
10679 : : else
10680 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
10681 : 0 : signed char biasval
10682 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10683 : 0 : bias = build_int_cst (intQI_type_node, biasval);
10684 : 0 : if (!final_mask)
10685 : : {
10686 : 0 : mask_vectype = truth_type_for (vectype);
10687 : 0 : final_mask = build_minus_one_cst (mask_vectype);
10688 : : }
10689 : : }
10690 : :
10691 : 0 : if (final_mask)
10692 : : {
10693 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10694 : 0 : if (type_mode_padding_p
10695 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10696 : 0 : need_zeroing = true;
10697 : : }
10698 : :
10699 : 0 : gcall *call;
10700 : 0 : if (final_len && final_mask)
10701 : : {
10702 : : /* Emit:
10703 : : VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10704 : : VEC_MASK, LEN, BIAS). */
10705 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10706 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10707 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
10708 : : dataref_ptr, alias_ptr,
10709 : : final_mask, vec_els,
10710 : : final_len, bias);
10711 : : }
10712 : 0 : else if (final_mask)
10713 : : {
10714 : : /* Emit:
10715 : : VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10716 : : VEC_MASK). */
10717 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10718 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10719 : 0 : call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
10720 : : dataref_ptr, alias_ptr,
10721 : : final_mask, vec_els);
10722 : : }
10723 : : else
10724 : : {
10725 : : /* Emit:
10726 : : VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
10727 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
10728 : 0 : call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
10729 : : }
10730 : 0 : gimple_call_set_lhs (call, vec_array);
10731 : 0 : gimple_call_set_nothrow (call, true);
10732 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10733 : :
10734 : : /* Extract each vector into an SSA_NAME. */
10735 : 0 : for (unsigned i = 0; i < group_size; i++)
10736 : : {
10737 : 0 : new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
10738 : : vec_array, i, need_zeroing,
10739 : : final_mask);
10740 : 0 : slp_node->push_vec_def (new_temp);
10741 : : }
10742 : :
10743 : : /* Record that VEC_ARRAY is now dead. */
10744 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
10745 : : }
10746 : :
10747 : 0 : if (costing_p)
10748 : : {
10749 : 0 : if (n_adjacent_loads > 0)
10750 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10751 : : alignment_support_scheme, misalignment, false,
10752 : : &inside_cost, &prologue_cost, cost_vec,
10753 : : cost_vec, true);
10754 : 0 : if (dump_enabled_p ())
10755 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10756 : : "vect_model_load_cost: inside_cost = %u, "
10757 : : "prologue_cost = %u .\n",
10758 : : inside_cost, prologue_cost);
10759 : : }
10760 : :
10761 : 0 : return true;
10762 : : }
10763 : :
10764 : 507154 : if (mat_gather_scatter_p (memory_access_type))
10765 : : {
10766 : 2697 : gcc_assert ((!grouped_load && !ls.slp_perm) || ls.ls_type);
10767 : :
10768 : 2697 : auto_vec<tree> dr_chain (vec_num);
10769 : :
10770 : : /* If we pun the original vectype the loads as well as costing, length,
10771 : : etc. is performed with the new type. After loading we VIEW_CONVERT
10772 : : the data to the original vectype. */
10773 : 2697 : tree original_vectype = vectype;
10774 : 2697 : if (ls.ls_type)
10775 : 0 : vectype = ls.ls_type;
10776 : :
10777 : : /* 1. Create the vector or array pointer update chain. */
10778 : 2697 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10779 : : {
10780 : 2697 : aggr_type = NULL_TREE;
10781 : 2697 : bump = NULL_TREE;
10782 : 2697 : if (!costing_p)
10783 : 749 : vect_get_gather_scatter_ops (loop, slp_node, &dataref_ptr,
10784 : : &vec_offsets);
10785 : : }
10786 : : else
10787 : : {
10788 : 0 : aggr_type = elem_type;
10789 : 0 : if (!costing_p)
10790 : : {
10791 : 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vectype,
10792 : : ls.strided_offset_vectype,
10793 : : loop_vinfo, gsi,
10794 : : &bump, &vec_offset, loop_lens);
10795 : 0 : dataref_ptr
10796 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10797 : : at_loop, offset, &dummy, gsi,
10798 : : &ptr_incr, false, bump);
10799 : : }
10800 : : }
10801 : :
10802 : : unsigned int inside_cost = 0, prologue_cost = 0;
10803 : :
10804 : 6097 : gimple *new_stmt = NULL;
10805 : 6097 : for (i = 0; i < vec_num; i++)
10806 : : {
10807 : 3400 : tree final_mask = NULL_TREE;
10808 : 3400 : tree final_len = NULL_TREE;
10809 : 3400 : tree bias = NULL_TREE;
10810 : 3400 : if (!costing_p)
10811 : : {
10812 : 963 : if (mask_node)
10813 : 153 : vec_mask = vec_masks[i];
10814 : 963 : if (loop_masks)
10815 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10816 : : vec_num, vectype, i);
10817 : 963 : if (vec_mask)
10818 : 153 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
10819 : : final_mask, vec_mask, gsi);
10820 : :
10821 : 963 : if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10822 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
10823 : : gsi, stmt_info, bump);
10824 : : }
10825 : :
10826 : : /* 2. Create the vector-load in the loop. */
10827 : 3400 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
10828 : 3400 : tree alias_align_ptr = build_int_cst (ref_type, align);
10829 : 3400 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
10830 : : {
10831 : 0 : if (costing_p)
10832 : : {
10833 : 0 : if (ls.supported_offset_vectype)
10834 : 0 : inside_cost
10835 : 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
10836 : : slp_node, 0, vect_body);
10837 : 0 : if (ls.supported_scale)
10838 : 0 : inside_cost
10839 : 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
10840 : : slp_node, 0, vect_body);
10841 : :
10842 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
10843 : 0 : inside_cost
10844 : 0 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
10845 : : slp_node, 0, vect_body);
10846 : 3400 : continue;
10847 : 0 : }
10848 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10849 : 0 : vec_offset = vec_offsets[i];
10850 : 0 : tree zero = build_zero_cst (vectype);
10851 : 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
10852 : 0 : bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
10853 : :
10854 : : /* Perform the offset conversion and scaling if necessary. */
10855 : 0 : if (!strided
10856 : 0 : && (ls.supported_offset_vectype || ls.supported_scale))
10857 : : {
10858 : 0 : gimple_seq stmts = NULL;
10859 : 0 : if (ls.supported_offset_vectype)
10860 : 0 : vec_offset = gimple_convert
10861 : 0 : (&stmts, ls.supported_offset_vectype, vec_offset);
10862 : 0 : if (ls.supported_scale)
10863 : : {
10864 : 0 : tree mult_cst = build_int_cst
10865 : 0 : (TREE_TYPE (TREE_TYPE (vec_offset)),
10866 : 0 : SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
10867 : 0 : tree mult = build_vector_from_val
10868 : 0 : (TREE_TYPE (vec_offset), mult_cst);
10869 : 0 : vec_offset = gimple_build
10870 : 0 : (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
10871 : : vec_offset, mult);
10872 : 0 : scale = size_int (ls.supported_scale);
10873 : : }
10874 : 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
10875 : : }
10876 : :
10877 : 0 : if (ls.gs.ifn == IFN_MASK_LEN_GATHER_LOAD)
10878 : : {
10879 : 0 : if (loop_lens)
10880 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10881 : : vec_num, vectype, i, 1);
10882 : : else
10883 : 0 : final_len = build_int_cst (sizetype,
10884 : 0 : TYPE_VECTOR_SUBPARTS (vectype));
10885 : 0 : signed char biasval
10886 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10887 : 0 : bias = build_int_cst (intQI_type_node, biasval);
10888 : 0 : if (!final_mask)
10889 : : {
10890 : 0 : mask_vectype = truth_type_for (vectype);
10891 : 0 : final_mask = build_minus_one_cst (mask_vectype);
10892 : : }
10893 : : }
10894 : :
10895 : 0 : if (final_mask)
10896 : : {
10897 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10898 : 0 : if (type_mode_padding_p
10899 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10900 : 0 : need_zeroing = true;
10901 : : }
10902 : :
10903 : 0 : gcall *call;
10904 : 0 : if (final_len && final_mask)
10905 : : {
10906 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
10907 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_GATHER_LOAD,
10908 : : 9, dataref_ptr,
10909 : : alias_align_ptr,
10910 : : vec_offset, scale, zero,
10911 : : final_mask, vec_els,
10912 : : final_len, bias);
10913 : : else
10914 : : /* Non-vector offset indicates that prefer to take
10915 : : MASK_LEN_STRIDED_LOAD instead of the
10916 : : MASK_LEN_GATHER_LOAD with direct stride arg. */
10917 : 0 : call = gimple_build_call_internal
10918 : 0 : (IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr,
10919 : : vec_offset, zero, final_mask, vec_els, final_len,
10920 : : bias);
10921 : : }
10922 : 0 : else if (final_mask)
10923 : 0 : call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
10924 : : 7, dataref_ptr,
10925 : : alias_align_ptr,
10926 : : vec_offset, scale,
10927 : : zero, final_mask, vec_els);
10928 : : else
10929 : 0 : call = gimple_build_call_internal (IFN_GATHER_LOAD, 5,
10930 : : dataref_ptr,
10931 : : alias_align_ptr,
10932 : : vec_offset, scale, zero);
10933 : 0 : gimple_call_set_nothrow (call, true);
10934 : 0 : new_stmt = call;
10935 : 0 : data_ref = NULL_TREE;
10936 : : }
10937 : 3400 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
10938 : : {
10939 : : /* The builtin decls path for gather is legacy, x86 only. */
10940 : 570 : gcc_assert (!final_len && nunits.is_constant ());
10941 : 570 : if (costing_p)
10942 : : {
10943 : 287 : unsigned int cnunits = vect_nunits_for_cost (vectype);
10944 : 287 : inside_cost
10945 : 287 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
10946 : : slp_node, 0, vect_body);
10947 : 287 : continue;
10948 : 287 : }
10949 : 283 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
10950 : 283 : poly_uint64 offset_nunits = TYPE_VECTOR_SUBPARTS (offset_vectype);
10951 : 283 : if (known_eq (nunits, offset_nunits))
10952 : : {
10953 : 134 : new_stmt = vect_build_one_gather_load_call
10954 : 134 : (vinfo, stmt_info, slp_node, vectype, gsi,
10955 : 134 : ls.gs.decl, dataref_ptr, vec_offsets[i],
10956 : : final_mask);
10957 : 134 : data_ref = NULL_TREE;
10958 : : }
10959 : 149 : else if (known_eq (nunits, offset_nunits * 2))
10960 : : {
10961 : : /* We have a offset vector with half the number of
10962 : : lanes but the builtins will produce full vectype
10963 : : data with just the lower lanes filled. */
10964 : 63 : new_stmt = vect_build_one_gather_load_call
10965 : 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
10966 : 63 : ls.gs.decl, dataref_ptr, vec_offsets[2 * i],
10967 : : final_mask);
10968 : 63 : tree low = make_ssa_name (vectype);
10969 : 63 : gimple_set_lhs (new_stmt, low);
10970 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10971 : :
10972 : : /* now put upper half of final_mask in final_mask low. */
10973 : 63 : if (final_mask
10974 : 63 : && !SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (final_mask))))
10975 : : {
10976 : 10 : int count = nunits.to_constant ();
10977 : 10 : vec_perm_builder sel (count, count, 1);
10978 : 10 : sel.quick_grow (count);
10979 : 78 : for (int i = 0; i < count; ++i)
10980 : 68 : sel[i] = i | (count / 2);
10981 : 10 : vec_perm_indices indices (sel, 2, count);
10982 : 10 : tree perm_mask = vect_gen_perm_mask_checked
10983 : 10 : (TREE_TYPE (final_mask), indices);
10984 : 10 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
10985 : : final_mask, final_mask,
10986 : : perm_mask);
10987 : 10 : final_mask = make_ssa_name (TREE_TYPE (final_mask));
10988 : 10 : gimple_set_lhs (new_stmt, final_mask);
10989 : 10 : vect_finish_stmt_generation (vinfo, stmt_info,
10990 : : new_stmt, gsi);
10991 : 10 : }
10992 : 53 : else if (final_mask)
10993 : : {
10994 : 25 : new_stmt = gimple_build_assign (NULL_TREE,
10995 : : VEC_UNPACK_HI_EXPR,
10996 : : final_mask);
10997 : 25 : final_mask = make_ssa_name
10998 : 25 : (truth_type_for (offset_vectype));
10999 : 25 : gimple_set_lhs (new_stmt, final_mask);
11000 : 25 : vect_finish_stmt_generation (vinfo, stmt_info,
11001 : : new_stmt, gsi);
11002 : : }
11003 : :
11004 : 63 : new_stmt = vect_build_one_gather_load_call
11005 : 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
11006 : : ls.gs.decl, dataref_ptr,
11007 : 63 : vec_offsets[2 * i + 1], final_mask);
11008 : 63 : tree high = make_ssa_name (vectype);
11009 : 63 : gimple_set_lhs (new_stmt, high);
11010 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11011 : :
11012 : : /* compose low + high. */
11013 : 63 : int count = nunits.to_constant ();
11014 : 63 : vec_perm_builder sel (count, count, 1);
11015 : 63 : sel.quick_grow (count);
11016 : 655 : for (int i = 0; i < count; ++i)
11017 : 592 : sel[i] = i < count / 2 ? i : i + count / 2;
11018 : 63 : vec_perm_indices indices (sel, 2, count);
11019 : 63 : tree perm_mask
11020 : 63 : = vect_gen_perm_mask_checked (vectype, indices);
11021 : 63 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11022 : : low, high, perm_mask);
11023 : 63 : data_ref = NULL_TREE;
11024 : 63 : }
11025 : 86 : else if (known_eq (nunits * 2, offset_nunits))
11026 : : {
11027 : : /* We have a offset vector with double the number of
11028 : : lanes. Select the low/high part accordingly. */
11029 : 86 : vec_offset = vec_offsets[i / 2];
11030 : 86 : if (i & 1)
11031 : : {
11032 : 43 : int count = offset_nunits.to_constant ();
11033 : 43 : vec_perm_builder sel (count, count, 1);
11034 : 43 : sel.quick_grow (count);
11035 : 463 : for (int i = 0; i < count; ++i)
11036 : 420 : sel[i] = i | (count / 2);
11037 : 43 : vec_perm_indices indices (sel, 2, count);
11038 : 43 : tree perm_mask = vect_gen_perm_mask_checked
11039 : 43 : (TREE_TYPE (vec_offset), indices);
11040 : 43 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11041 : : vec_offset, vec_offset,
11042 : : perm_mask);
11043 : 43 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
11044 : 43 : gimple_set_lhs (new_stmt, vec_offset);
11045 : 43 : vect_finish_stmt_generation (vinfo, stmt_info,
11046 : : new_stmt, gsi);
11047 : 43 : }
11048 : 86 : new_stmt = vect_build_one_gather_load_call
11049 : 86 : (vinfo, stmt_info, slp_node, vectype, gsi,
11050 : : ls.gs.decl,
11051 : : dataref_ptr, vec_offset, final_mask);
11052 : 86 : data_ref = NULL_TREE;
11053 : : }
11054 : : else
11055 : 0 : gcc_unreachable ();
11056 : : }
11057 : : else
11058 : : {
11059 : : /* Emulated gather-scatter. */
11060 : 2830 : gcc_assert (!final_mask);
11061 : 2830 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
11062 : 2830 : if (costing_p)
11063 : : {
11064 : : /* For emulated gathers N offset vector element
11065 : : offset add is consumed by the load). */
11066 : 2150 : inside_cost = record_stmt_cost (cost_vec, const_nunits,
11067 : : vec_to_scalar,
11068 : : slp_node, 0, vect_body);
11069 : : /* N scalar loads plus gathering them into a
11070 : : vector. */
11071 : 2150 : inside_cost
11072 : 2150 : = record_stmt_cost (cost_vec, const_nunits, scalar_load,
11073 : : slp_node, 0, vect_body);
11074 : 2150 : inside_cost
11075 : 2150 : = record_stmt_cost (cost_vec, 1, vec_construct,
11076 : : slp_node, 0, vect_body);
11077 : 2150 : continue;
11078 : : }
11079 : 680 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
11080 : 680 : unsigned HOST_WIDE_INT const_offset_nunits
11081 : 680 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
11082 : 680 : vec<constructor_elt, va_gc> *ctor_elts;
11083 : 680 : vec_alloc (ctor_elts, const_nunits);
11084 : 680 : gimple_seq stmts = NULL;
11085 : : /* We support offset vectors with more elements
11086 : : than the data vector for now. */
11087 : 680 : unsigned HOST_WIDE_INT factor
11088 : : = const_offset_nunits / const_nunits;
11089 : 680 : vec_offset = vec_offsets[i / factor];
11090 : 680 : unsigned elt_offset = (i % factor) * const_nunits;
11091 : 680 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
11092 : 680 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
11093 : 680 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
11094 : 2828 : for (unsigned k = 0; k < const_nunits; ++k)
11095 : : {
11096 : 2148 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
11097 : : bitsize_int (k + elt_offset));
11098 : 6444 : tree idx = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
11099 : 2148 : vec_offset, TYPE_SIZE (idx_type),
11100 : : boff);
11101 : 2148 : idx = gimple_convert (&stmts, sizetype, idx);
11102 : 2148 : idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx, scale);
11103 : 2148 : tree ptr = gimple_build (&stmts, PLUS_EXPR,
11104 : 2148 : TREE_TYPE (dataref_ptr),
11105 : : dataref_ptr, idx);
11106 : 2148 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
11107 : 2148 : tree elt = make_ssa_name (TREE_TYPE (vectype));
11108 : 2148 : tree ref = build2 (MEM_REF, ltype, ptr,
11109 : : build_int_cst (ref_type, 0));
11110 : 2148 : new_stmt = gimple_build_assign (elt, ref);
11111 : 4296 : gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
11112 : 2148 : gimple_seq_add_stmt (&stmts, new_stmt);
11113 : 2148 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
11114 : : }
11115 : 680 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11116 : 680 : new_stmt = gimple_build_assign (NULL_TREE,
11117 : : build_constructor (vectype,
11118 : : ctor_elts));
11119 : 680 : data_ref = NULL_TREE;
11120 : : }
11121 : :
11122 : 963 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11123 : : /* DATA_REF is null if we've already built the statement. */
11124 : 963 : if (data_ref)
11125 : : {
11126 : : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11127 : : new_stmt = gimple_build_assign (vec_dest, data_ref);
11128 : : }
11129 : 1926 : new_temp = (need_zeroing
11130 : 963 : ? make_ssa_name (vectype)
11131 : 963 : : make_ssa_name (vec_dest, new_stmt));
11132 : 963 : gimple_set_lhs (new_stmt, new_temp);
11133 : 963 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11134 : :
11135 : : /* If we need to explicitly zero inactive elements emit a
11136 : : VEC_COND_EXPR that does so. */
11137 : 963 : if (need_zeroing)
11138 : : {
11139 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11140 : : vectype);
11141 : :
11142 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11143 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11144 : : final_mask, new_temp, vec_els);
11145 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11146 : 0 : new_temp = new_temp2;
11147 : : }
11148 : :
11149 : 963 : if (ls.ls_type)
11150 : : {
11151 : 0 : new_stmt = gimple_build_assign (make_ssa_name
11152 : : (original_vectype),
11153 : : VIEW_CONVERT_EXPR,
11154 : : build1 (VIEW_CONVERT_EXPR,
11155 : : original_vectype,
11156 : : new_temp));
11157 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11158 : : }
11159 : :
11160 : : /* Store vector loads in the corresponding SLP_NODE. */
11161 : 963 : if (!costing_p)
11162 : : {
11163 : 963 : if (ls.slp_perm)
11164 : 0 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
11165 : : else
11166 : 963 : slp_node->push_vec_def (new_stmt);
11167 : : }
11168 : : }
11169 : :
11170 : 2697 : if (ls.slp_perm)
11171 : : {
11172 : 0 : if (costing_p)
11173 : : {
11174 : 0 : gcc_assert (ls.n_perms != -1U);
11175 : 0 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
11176 : : slp_node, 0, vect_body);
11177 : : }
11178 : : else
11179 : : {
11180 : 0 : unsigned n_perms2;
11181 : 0 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
11182 : : false, &n_perms2);
11183 : 0 : gcc_assert (ls.n_perms == n_perms2);
11184 : : }
11185 : : }
11186 : :
11187 : 2697 : if (costing_p && dump_enabled_p ())
11188 : 222 : dump_printf_loc (MSG_NOTE, vect_location,
11189 : : "vect_model_load_cost: inside_cost = %u, "
11190 : : "prologue_cost = %u .\n",
11191 : : inside_cost, prologue_cost);
11192 : 2697 : return true;
11193 : 2697 : }
11194 : :
11195 : 504457 : aggr_type = vectype;
11196 : 504457 : if (!costing_p)
11197 : 157944 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
11198 : : memory_access_type, loop_lens);
11199 : :
11200 : 504457 : poly_uint64 group_elt = 0;
11201 : 504457 : unsigned int inside_cost = 0, prologue_cost = 0;
11202 : : /* For costing some adjacent vector loads, we'd like to cost with
11203 : : the total number of them once instead of cost each one by one. */
11204 : 504457 : unsigned int n_adjacent_loads = 0;
11205 : :
11206 : : /* 1. Create the vector or array pointer update chain. */
11207 : 504457 : if (!costing_p)
11208 : : {
11209 : 157944 : bool simd_lane_access_p
11210 : 157944 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
11211 : 157944 : if (simd_lane_access_p
11212 : 1629 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
11213 : 1629 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
11214 : 1629 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
11215 : 1629 : && integer_zerop (DR_INIT (first_dr_info->dr))
11216 : 1629 : && alias_sets_conflict_p (get_alias_set (aggr_type),
11217 : 1629 : get_alias_set (TREE_TYPE (ref_type)))
11218 : 157944 : && (alignment_support_scheme == dr_aligned
11219 : 1629 : || alignment_support_scheme == dr_unaligned_supported))
11220 : : {
11221 : 1629 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
11222 : 1629 : dataref_offset = build_int_cst (ref_type, 0);
11223 : : }
11224 : 156315 : else if (diff_first_stmt_info)
11225 : : {
11226 : 3174 : dataref_ptr
11227 : 3174 : = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
11228 : : aggr_type, at_loop, offset, &dummy,
11229 : : gsi, &ptr_incr, simd_lane_access_p,
11230 : : bump);
11231 : : /* Adjust the pointer by the difference to first_stmt. */
11232 : 3174 : data_reference_p ptrdr
11233 : : = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
11234 : 3174 : tree diff = fold_convert (sizetype,
11235 : : size_binop (MINUS_EXPR,
11236 : : DR_INIT (first_dr_info->dr),
11237 : : DR_INIT (ptrdr)));
11238 : 3174 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11239 : : stmt_info, diff);
11240 : 3174 : if (alignment_support_scheme == dr_explicit_realign)
11241 : : {
11242 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info_for_drptr,
11243 : : vectype, gsi,
11244 : : &realignment_token,
11245 : : alignment_support_scheme,
11246 : : dataref_ptr, &at_loop);
11247 : 0 : gcc_assert (!compute_in_loop);
11248 : : }
11249 : : }
11250 : : else
11251 : 153141 : dataref_ptr
11252 : 153141 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11253 : : at_loop,
11254 : : offset, &dummy, gsi, &ptr_incr,
11255 : : simd_lane_access_p, bump);
11256 : : }
11257 : : else if (!costing_p)
11258 : : {
11259 : : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
11260 : : if (dataref_offset)
11261 : : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
11262 : : else
11263 : : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11264 : : stmt_info, bump);
11265 : : }
11266 : :
11267 : 504457 : auto_vec<tree> dr_chain;
11268 : 504457 : if (grouped_load || ls.slp_perm)
11269 : 46021 : dr_chain.create (vec_num);
11270 : :
11271 : : gimple *new_stmt = NULL;
11272 : 1309541 : for (i = 0; i < vec_num; i++)
11273 : : {
11274 : 805084 : tree final_mask = NULL_TREE;
11275 : 805084 : tree final_len = NULL_TREE;
11276 : 805084 : tree bias = NULL_TREE;
11277 : :
11278 : 805084 : if (!costing_p)
11279 : : {
11280 : 208274 : if (mask_node)
11281 : 630 : vec_mask = vec_masks[i];
11282 : 208274 : if (loop_masks)
11283 : 46 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11284 : : vec_num, vectype, i);
11285 : 208274 : if (vec_mask)
11286 : 630 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11287 : : final_mask, vec_mask, gsi);
11288 : :
11289 : 208274 : if (i > 0)
11290 : 50330 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11291 : : gsi, stmt_info, bump);
11292 : : }
11293 : :
11294 : : /* 2. Create the vector-load in the loop. */
11295 : 805084 : switch (alignment_support_scheme)
11296 : : {
11297 : 805084 : case dr_aligned:
11298 : 805084 : case dr_unaligned_supported:
11299 : 805084 : {
11300 : 805084 : if (costing_p)
11301 : : break;
11302 : :
11303 : 208274 : unsigned int misalign;
11304 : 208274 : unsigned HOST_WIDE_INT align;
11305 : 208274 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
11306 : 208274 : if (alignment_support_scheme == dr_aligned)
11307 : : misalign = 0;
11308 : 126533 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
11309 : : {
11310 : 101984 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
11311 : 101984 : misalign = 0;
11312 : : }
11313 : : else
11314 : 24549 : misalign = misalignment;
11315 : 208274 : if (dataref_offset == NULL_TREE
11316 : 206147 : && TREE_CODE (dataref_ptr) == SSA_NAME)
11317 : 139119 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
11318 : : misalign);
11319 : 208274 : align = least_bit_hwi (misalign | align);
11320 : :
11321 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
11322 : 208274 : machine_mode vmode = TYPE_MODE (vectype);
11323 : 208274 : machine_mode new_vmode = vmode;
11324 : 208274 : internal_fn partial_ifn = IFN_LAST;
11325 : 208274 : if (loop_lens)
11326 : : {
11327 : 0 : opt_machine_mode new_ovmode
11328 : 0 : = get_len_load_store_mode (vmode, true, &partial_ifn);
11329 : 0 : new_vmode = new_ovmode.require ();
11330 : 0 : unsigned factor
11331 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
11332 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11333 : : vec_num, vectype, i, factor);
11334 : : }
11335 : 208274 : else if (final_mask)
11336 : : {
11337 : 657 : if (!can_vec_mask_load_store_p (vmode,
11338 : 657 : TYPE_MODE
11339 : : (TREE_TYPE (final_mask)),
11340 : : true, &partial_ifn))
11341 : 0 : gcc_unreachable ();
11342 : : }
11343 : :
11344 : 208274 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11345 : : {
11346 : 0 : if (!final_len)
11347 : : {
11348 : : /* Pass VF value to 'len' argument of
11349 : : MASK_LEN_LOAD if LOOP_LENS is invalid. */
11350 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11351 : : }
11352 : 0 : if (!final_mask)
11353 : : {
11354 : : /* Pass all ones value to 'mask' argument of
11355 : : MASK_LEN_LOAD if final_mask is invalid. */
11356 : 0 : mask_vectype = truth_type_for (vectype);
11357 : 0 : final_mask = build_minus_one_cst (mask_vectype);
11358 : : }
11359 : : }
11360 : 208274 : if (final_len)
11361 : : {
11362 : 0 : signed char biasval
11363 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11364 : 0 : bias = build_int_cst (intQI_type_node, biasval);
11365 : : }
11366 : :
11367 : 208274 : tree vec_els;
11368 : :
11369 : 208274 : if (final_len)
11370 : : {
11371 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11372 : 0 : gcall *call;
11373 : 0 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11374 : : {
11375 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval,
11376 : : vectype);
11377 : 0 : if (type_mode_padding_p
11378 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11379 : 0 : need_zeroing = true;
11380 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
11381 : : 6, dataref_ptr, ptr,
11382 : : final_mask, vec_els,
11383 : : final_len, bias);
11384 : : }
11385 : : else
11386 : 0 : call = gimple_build_call_internal (IFN_LEN_LOAD, 4,
11387 : : dataref_ptr, ptr,
11388 : : final_len, bias);
11389 : 0 : gimple_call_set_nothrow (call, true);
11390 : 0 : new_stmt = call;
11391 : 0 : data_ref = NULL_TREE;
11392 : :
11393 : : /* Need conversion if it's wrapped with VnQI. */
11394 : 0 : if (vmode != new_vmode)
11395 : : {
11396 : 0 : tree new_vtype
11397 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
11398 : : new_vmode);
11399 : 0 : tree var = vect_get_new_ssa_name (new_vtype,
11400 : : vect_simple_var);
11401 : 0 : gimple_set_lhs (call, var);
11402 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call,
11403 : : gsi);
11404 : 0 : tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
11405 : 0 : new_stmt = gimple_build_assign (vec_dest,
11406 : : VIEW_CONVERT_EXPR, op);
11407 : : }
11408 : : }
11409 : 208274 : else if (final_mask)
11410 : : {
11411 : 657 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11412 : 657 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11413 : 657 : if (type_mode_padding_p
11414 : 657 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11415 : 0 : need_zeroing = true;
11416 : 657 : gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
11417 : : dataref_ptr, ptr,
11418 : : final_mask,
11419 : : vec_els);
11420 : 657 : gimple_call_set_nothrow (call, true);
11421 : 657 : new_stmt = call;
11422 : 657 : data_ref = NULL_TREE;
11423 : : }
11424 : : else
11425 : : {
11426 : 207617 : tree ltype = vectype;
11427 : 207617 : tree new_vtype = NULL_TREE;
11428 : 207617 : unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
11429 : 207617 : unsigned HOST_WIDE_INT dr_size
11430 : 207617 : = vect_get_scalar_dr_size (first_dr_info);
11431 : 207617 : poly_int64 off = 0;
11432 : 207617 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11433 : 1415 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
11434 : 207617 : unsigned int vect_align
11435 : 207617 : = vect_known_alignment_in_bytes (first_dr_info, vectype,
11436 : 207617 : off);
11437 : : /* Try to use a single smaller load when we are about
11438 : : to load excess elements compared to the unrolled
11439 : : scalar loop. */
11440 : 207617 : if (known_gt ((i + 1) * nunits,
11441 : : (group_size * vf - gap)))
11442 : : {
11443 : 2088 : poly_uint64 remain = ((group_size * vf - gap) - i * nunits);
11444 : 2088 : if (known_ge ((i + 1) * nunits - (group_size * vf - gap),
11445 : : nunits))
11446 : : /* DR will be unused. */
11447 : : ltype = NULL_TREE;
11448 : 1663 : else if (known_ge (vect_align,
11449 : : tree_to_poly_uint64
11450 : : (TYPE_SIZE_UNIT (vectype))))
11451 : : /* Aligned access to excess elements is OK if
11452 : : at least one element is accessed in the
11453 : : scalar loop. */
11454 : : ;
11455 : 1429 : else if (known_gt (vect_align,
11456 : : ((nunits - remain) * dr_size)))
11457 : : /* Aligned access to the gap area when there's
11458 : : at least one element in it is OK. */
11459 : : ;
11460 : : else
11461 : : {
11462 : : /* remain should now be > 0 and < nunits. */
11463 : 1426 : unsigned num;
11464 : 1426 : if (known_ne (remain, 0u)
11465 : 1426 : && constant_multiple_p (nunits, remain, &num))
11466 : : {
11467 : 1021 : tree ptype;
11468 : 1021 : new_vtype
11469 : 1021 : = vector_vector_composition_type (vectype, num,
11470 : : &ptype);
11471 : 1021 : if (new_vtype)
11472 : 1021 : ltype = ptype;
11473 : : }
11474 : : /* Else use multiple loads or a masked load? */
11475 : : /* For loop vectorization we now should have
11476 : : an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
11477 : : set. */
11478 : 1426 : if (loop_vinfo)
11479 : 1389 : gcc_assert (new_vtype
11480 : : || LOOP_VINFO_PEELING_FOR_GAPS
11481 : : (loop_vinfo));
11482 : : /* But still reduce the access size to the next
11483 : : required power-of-two so peeling a single
11484 : : scalar iteration is sufficient. */
11485 : 1426 : unsigned HOST_WIDE_INT cremain;
11486 : 1426 : if (remain.is_constant (&cremain))
11487 : : {
11488 : 1426 : unsigned HOST_WIDE_INT cpart_size
11489 : 1426 : = 1 << ceil_log2 (cremain);
11490 : 1426 : if (known_gt (nunits, cpart_size)
11491 : 1426 : && constant_multiple_p (nunits, cpart_size,
11492 : : &num))
11493 : : {
11494 : 1033 : tree ptype;
11495 : 1033 : new_vtype
11496 : 2066 : = vector_vector_composition_type (vectype,
11497 : 1033 : num,
11498 : : &ptype);
11499 : 1033 : if (new_vtype)
11500 : 1033 : ltype = ptype;
11501 : : }
11502 : : }
11503 : : }
11504 : : }
11505 : 207617 : tree offset = (dataref_offset ? dataref_offset
11506 : 205490 : : build_int_cst (ref_type, 0));
11507 : 207617 : if (!ltype)
11508 : : ;
11509 : 207192 : else if (ltype != vectype
11510 : 207192 : && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11511 : : {
11512 : 21 : poly_uint64 gap_offset
11513 : 21 : = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
11514 : 21 : - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
11515 : 21 : tree gapcst = build_int_cstu (ref_type, gap_offset);
11516 : 21 : offset = size_binop (PLUS_EXPR, offset, gapcst);
11517 : : }
11518 : 207617 : if (ltype)
11519 : : {
11520 : 207192 : data_ref = fold_build2 (MEM_REF, ltype,
11521 : : dataref_ptr, offset);
11522 : 207192 : if (alignment_support_scheme == dr_aligned
11523 : 207192 : && align >= TYPE_ALIGN_UNIT (ltype))
11524 : : ;
11525 : : else
11526 : 125900 : TREE_TYPE (data_ref)
11527 : 251800 : = build_aligned_type (TREE_TYPE (data_ref),
11528 : : align * BITS_PER_UNIT);
11529 : : }
11530 : 207617 : if (!ltype)
11531 : 425 : data_ref = build_constructor (vectype, NULL);
11532 : 207192 : else if (ltype != vectype)
11533 : : {
11534 : 1033 : vect_copy_ref_info (data_ref,
11535 : 1033 : DR_REF (first_dr_info->dr));
11536 : 1033 : tree tem = make_ssa_name (ltype);
11537 : 1033 : new_stmt = gimple_build_assign (tem, data_ref);
11538 : 1033 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11539 : : gsi);
11540 : 1033 : data_ref = NULL;
11541 : 1033 : vec<constructor_elt, va_gc> *v;
11542 : : /* We've computed 'num' above to statically two
11543 : : or via constant_multiple_p. */
11544 : 1033 : unsigned num
11545 : 1033 : = (exact_div (tree_to_poly_uint64
11546 : 1033 : (TYPE_SIZE_UNIT (vectype)),
11547 : : tree_to_poly_uint64
11548 : 1033 : (TYPE_SIZE_UNIT (ltype)))
11549 : 1033 : .to_constant ());
11550 : 1033 : vec_alloc (v, num);
11551 : 1033 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11552 : : {
11553 : 54 : while (--num)
11554 : 54 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11555 : : build_zero_cst (ltype));
11556 : 21 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11557 : : }
11558 : : else
11559 : : {
11560 : 1012 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11561 : 1012 : while (--num)
11562 : 2354 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11563 : : build_zero_cst (ltype));
11564 : : }
11565 : 1033 : gcc_assert (new_vtype != NULL_TREE);
11566 : 1033 : if (new_vtype == vectype)
11567 : 1003 : new_stmt
11568 : 1003 : = gimple_build_assign (vec_dest,
11569 : : build_constructor (vectype, v));
11570 : : else
11571 : : {
11572 : 30 : tree new_vname = make_ssa_name (new_vtype);
11573 : 30 : new_stmt
11574 : 30 : = gimple_build_assign (new_vname,
11575 : : build_constructor (new_vtype,
11576 : : v));
11577 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
11578 : : new_stmt, gsi);
11579 : 30 : new_stmt
11580 : 30 : = gimple_build_assign (vec_dest,
11581 : : build1 (VIEW_CONVERT_EXPR,
11582 : : vectype, new_vname));
11583 : : }
11584 : : }
11585 : : }
11586 : : break;
11587 : : }
11588 : 0 : case dr_explicit_realign:
11589 : 0 : {
11590 : 0 : if (costing_p)
11591 : : break;
11592 : 0 : tree ptr, bump;
11593 : :
11594 : 0 : tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11595 : :
11596 : 0 : if (compute_in_loop)
11597 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype,
11598 : : gsi, &realignment_token,
11599 : : dr_explicit_realign,
11600 : : dataref_ptr, NULL);
11601 : :
11602 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11603 : 0 : ptr = copy_ssa_name (dataref_ptr);
11604 : : else
11605 : 0 : ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
11606 : : // For explicit realign the target alignment should be
11607 : : // known at compile time.
11608 : 0 : unsigned HOST_WIDE_INT align
11609 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11610 : 0 : new_stmt = gimple_build_assign (ptr, BIT_AND_EXPR, dataref_ptr,
11611 : : build_int_cst
11612 : 0 : (TREE_TYPE (dataref_ptr),
11613 : 0 : -(HOST_WIDE_INT) align));
11614 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11615 : 0 : data_ref = build2 (MEM_REF, vectype,
11616 : : ptr, build_int_cst (ref_type, 0));
11617 : 0 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11618 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11619 : 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11620 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11621 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11622 : 0 : gimple_move_vops (new_stmt, stmt_info->stmt);
11623 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11624 : 0 : msq = new_temp;
11625 : :
11626 : 0 : bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
11627 : 0 : bump = size_binop (MINUS_EXPR, bump, size_one_node);
11628 : 0 : ptr = bump_vector_ptr (vinfo, dataref_ptr, NULL, gsi, stmt_info,
11629 : : bump);
11630 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR, ptr,
11631 : 0 : build_int_cst (TREE_TYPE (ptr),
11632 : 0 : -(HOST_WIDE_INT) align));
11633 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
11634 : 0 : ptr = copy_ssa_name (ptr, new_stmt);
11635 : : else
11636 : 0 : ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
11637 : 0 : gimple_assign_set_lhs (new_stmt, ptr);
11638 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11639 : 0 : data_ref = build2 (MEM_REF, vectype,
11640 : : ptr, build_int_cst (ref_type, 0));
11641 : 0 : break;
11642 : : }
11643 : 0 : case dr_explicit_realign_optimized:
11644 : 0 : {
11645 : 0 : if (costing_p)
11646 : : break;
11647 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11648 : 0 : new_temp = copy_ssa_name (dataref_ptr);
11649 : : else
11650 : 0 : new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
11651 : : // We should only be doing this if we know the target
11652 : : // alignment at compile time.
11653 : 0 : unsigned HOST_WIDE_INT align
11654 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11655 : 0 : new_stmt = gimple_build_assign (new_temp, BIT_AND_EXPR, dataref_ptr,
11656 : 0 : build_int_cst (TREE_TYPE (dataref_ptr),
11657 : 0 : -(HOST_WIDE_INT) align));
11658 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11659 : 0 : data_ref = build2 (MEM_REF, vectype, new_temp,
11660 : : build_int_cst (ref_type, 0));
11661 : 0 : break;
11662 : : }
11663 : 0 : default:
11664 : 0 : gcc_unreachable ();
11665 : : }
11666 : :
11667 : : /* One common place to cost the above vect load for different
11668 : : alignment support schemes. */
11669 : 805084 : if (costing_p)
11670 : : {
11671 : : /* For the prologue cost for realign,
11672 : : we only need to count it once for the whole group. */
11673 : 596810 : bool first_stmt_info_p = first_stmt_info == stmt_info;
11674 : 596810 : bool add_realign_cost = first_stmt_info_p && i == 0;
11675 : 596810 : if (memory_access_type == VMAT_CONTIGUOUS
11676 : 596810 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11677 : : {
11678 : : /* Leave realign cases alone to keep them simple. */
11679 : 596810 : if (alignment_support_scheme == dr_explicit_realign_optimized
11680 : : || alignment_support_scheme == dr_explicit_realign)
11681 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
11682 : : alignment_support_scheme, misalignment,
11683 : : add_realign_cost, &inside_cost,
11684 : : &prologue_cost, cost_vec, cost_vec,
11685 : : true);
11686 : : else
11687 : 596810 : n_adjacent_loads++;
11688 : : }
11689 : : }
11690 : : else
11691 : : {
11692 : 208274 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11693 : : /* DATA_REF is null if we've already built the statement. */
11694 : 208274 : if (data_ref)
11695 : : {
11696 : 206584 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11697 : 206584 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11698 : : }
11699 : :
11700 : 416548 : new_temp = (need_zeroing
11701 : 208274 : ? make_ssa_name (vectype)
11702 : 208274 : : make_ssa_name (vec_dest, new_stmt));
11703 : 208274 : gimple_set_lhs (new_stmt, new_temp);
11704 : 208274 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11705 : :
11706 : : /* If we need to explicitly zero inactive elements emit a
11707 : : VEC_COND_EXPR that does so. */
11708 : 208274 : if (need_zeroing)
11709 : : {
11710 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11711 : : vectype);
11712 : :
11713 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11714 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11715 : : final_mask, new_temp, vec_els);
11716 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11717 : : gsi);
11718 : 0 : new_temp = new_temp2;
11719 : : }
11720 : : }
11721 : :
11722 : : /* 3. Handle explicit realignment if necessary/supported.
11723 : : Create in loop:
11724 : : vec_dest = realign_load (msq, lsq, realignment_token) */
11725 : 805084 : if (!costing_p
11726 : 208274 : && (alignment_support_scheme == dr_explicit_realign_optimized
11727 : : || alignment_support_scheme == dr_explicit_realign))
11728 : : {
11729 : 0 : lsq = gimple_assign_lhs (new_stmt);
11730 : 0 : if (!realignment_token)
11731 : 0 : realignment_token = dataref_ptr;
11732 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11733 : 0 : new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
11734 : : lsq, realignment_token);
11735 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11736 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11737 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11738 : :
11739 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
11740 : : {
11741 : 0 : gcc_assert (phi);
11742 : 0 : if (i == vec_num - 1)
11743 : 0 : add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
11744 : : UNKNOWN_LOCATION);
11745 : : msq = lsq;
11746 : : }
11747 : : }
11748 : :
11749 : 805084 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11750 : : {
11751 : 5846 : if (costing_p)
11752 : 4431 : inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
11753 : : slp_node, 0, vect_body);
11754 : : else
11755 : : {
11756 : 1415 : tree perm_mask = perm_mask_for_reverse (vectype);
11757 : 1415 : new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
11758 : : perm_mask, stmt_info, gsi);
11759 : 1415 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
11760 : : }
11761 : : }
11762 : :
11763 : : /* Collect vector loads and later create their permutation in
11764 : : vect_transform_slp_perm_load. */
11765 : 805084 : if (!costing_p && (grouped_load || ls.slp_perm))
11766 : 31597 : dr_chain.quick_push (new_temp);
11767 : :
11768 : : /* Store vector loads in the corresponding SLP_NODE. */
11769 : 208274 : if (!costing_p && !ls.slp_perm)
11770 : 176677 : slp_node->push_vec_def (new_stmt);
11771 : :
11772 : : /* With SLP permutation we load the gaps as well, without
11773 : : we need to skip the gaps after we manage to fully load
11774 : : all elements. group_gap_adj is DR_GROUP_SIZE here. */
11775 : 805084 : group_elt += nunits;
11776 : 805084 : if (!costing_p
11777 : 208274 : && maybe_ne (group_gap_adj, 0U)
11778 : 19187 : && !ls.slp_perm
11779 : 823907 : && known_eq (group_elt, group_size - group_gap_adj))
11780 : : {
11781 : 15538 : poly_wide_int bump_val
11782 : 15538 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11783 : 15538 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11784 : 0 : bump_val = -bump_val;
11785 : 15538 : tree bump = wide_int_to_tree (sizetype, bump_val);
11786 : 15538 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11787 : : stmt_info, bump);
11788 : 15538 : group_elt = 0;
11789 : 15538 : }
11790 : : }
11791 : : /* Bump the vector pointer to account for a gap or for excess
11792 : : elements loaded for a permuted SLP load. */
11793 : 504457 : if (!costing_p
11794 : 157944 : && maybe_ne (group_gap_adj, 0U)
11795 : 520181 : && ls.slp_perm)
11796 : : {
11797 : 186 : poly_wide_int bump_val
11798 : 186 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11799 : 186 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11800 : 9 : bump_val = -bump_val;
11801 : 186 : tree bump = wide_int_to_tree (sizetype, bump_val);
11802 : 186 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11803 : : stmt_info, bump);
11804 : 186 : }
11805 : :
11806 : 504457 : if (ls.slp_perm)
11807 : : {
11808 : : /* For SLP we know we've seen all possible uses of dr_chain so
11809 : : direct vect_transform_slp_perm_load to DCE the unused parts.
11810 : : ??? This is a hack to prevent compile-time issues as seen
11811 : : in PR101120 and friends. */
11812 : 46021 : if (costing_p)
11813 : : {
11814 : 30544 : gcc_assert (ls.n_perms != -1U);
11815 : 30544 : if (ls.n_perms != 0)
11816 : 30299 : inside_cost = record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
11817 : : slp_node, 0, vect_body);
11818 : : }
11819 : : else
11820 : : {
11821 : 15477 : unsigned n_perms2;
11822 : 15477 : bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
11823 : : gsi, vf, false, &n_perms2,
11824 : : nullptr, true);
11825 : 15477 : gcc_assert (ok && ls.n_perms == n_perms2);
11826 : : }
11827 : : }
11828 : :
11829 : 504457 : if (costing_p)
11830 : : {
11831 : 346513 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
11832 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
11833 : 346513 : if (n_adjacent_loads > 0)
11834 : 346513 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11835 : : alignment_support_scheme, misalignment, false,
11836 : : &inside_cost, &prologue_cost, cost_vec, cost_vec,
11837 : : true);
11838 : 346513 : if (dump_enabled_p ())
11839 : 22790 : dump_printf_loc (MSG_NOTE, vect_location,
11840 : : "vect_model_load_cost: inside_cost = %u, "
11841 : : "prologue_cost = %u .\n",
11842 : : inside_cost, prologue_cost);
11843 : : }
11844 : :
11845 : 504457 : return true;
11846 : 1640708 : }
11847 : :
11848 : : /* Function vect_is_simple_cond.
11849 : :
11850 : : Input:
11851 : : LOOP - the loop that is being vectorized.
11852 : : COND - Condition that is checked for simple use.
11853 : :
11854 : : Output:
11855 : : *COMP_VECTYPE - the vector type for the comparison.
11856 : : *DTS - The def types for the arguments of the comparison
11857 : :
11858 : : Returns whether a COND can be vectorized. Checks whether
11859 : : condition operands are supportable using vec_is_simple_use. */
11860 : :
11861 : : static bool
11862 : 27987 : vect_is_simple_cond (tree cond, vec_info *vinfo,
11863 : : slp_tree slp_node, tree *comp_vectype,
11864 : : enum vect_def_type *dts, tree vectype)
11865 : : {
11866 : 27987 : tree lhs, rhs;
11867 : 27987 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
11868 : 27987 : slp_tree slp_op;
11869 : :
11870 : : /* Mask case. */
11871 : 27987 : if (TREE_CODE (cond) == SSA_NAME
11872 : 27987 : && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
11873 : : {
11874 : 27975 : if (!vect_is_simple_use (vinfo, slp_node, 0, &cond,
11875 : : &slp_op, &dts[0], comp_vectype)
11876 : 27975 : || !*comp_vectype
11877 : 55931 : || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
11878 : : return false;
11879 : : return true;
11880 : : }
11881 : :
11882 : 12 : if (!COMPARISON_CLASS_P (cond))
11883 : : return false;
11884 : :
11885 : 0 : lhs = TREE_OPERAND (cond, 0);
11886 : 0 : rhs = TREE_OPERAND (cond, 1);
11887 : :
11888 : 0 : if (TREE_CODE (lhs) == SSA_NAME)
11889 : : {
11890 : 0 : if (!vect_is_simple_use (vinfo, slp_node, 0,
11891 : : &lhs, &slp_op, &dts[0], &vectype1))
11892 : : return false;
11893 : : }
11894 : 0 : else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
11895 : 0 : || TREE_CODE (lhs) == FIXED_CST)
11896 : 0 : dts[0] = vect_constant_def;
11897 : : else
11898 : : return false;
11899 : :
11900 : 0 : if (TREE_CODE (rhs) == SSA_NAME)
11901 : : {
11902 : 0 : if (!vect_is_simple_use (vinfo, slp_node, 1,
11903 : : &rhs, &slp_op, &dts[1], &vectype2))
11904 : : return false;
11905 : : }
11906 : 0 : else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
11907 : 0 : || TREE_CODE (rhs) == FIXED_CST)
11908 : 0 : dts[1] = vect_constant_def;
11909 : : else
11910 : : return false;
11911 : :
11912 : 0 : if (vectype1 && vectype2
11913 : 0 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
11914 : 0 : TYPE_VECTOR_SUBPARTS (vectype2)))
11915 : 0 : return false;
11916 : :
11917 : 0 : *comp_vectype = vectype1 ? vectype1 : vectype2;
11918 : : /* Invariant comparison. */
11919 : 0 : if (! *comp_vectype)
11920 : : {
11921 : 0 : tree scalar_type = TREE_TYPE (lhs);
11922 : 0 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
11923 : 0 : *comp_vectype = truth_type_for (vectype);
11924 : : else
11925 : : {
11926 : : /* If we can widen the comparison to match vectype do so. */
11927 : 0 : if (INTEGRAL_TYPE_P (scalar_type)
11928 : 0 : && !slp_node
11929 : 0 : && tree_int_cst_lt (TYPE_SIZE (scalar_type),
11930 : 0 : TYPE_SIZE (TREE_TYPE (vectype))))
11931 : 0 : scalar_type = build_nonstandard_integer_type
11932 : 0 : (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
11933 : 0 : *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
11934 : : slp_node);
11935 : : }
11936 : : }
11937 : :
11938 : : return true;
11939 : : }
11940 : :
11941 : : /* vectorizable_condition.
11942 : :
11943 : : Check if STMT_INFO is conditional modify expression that can be vectorized.
11944 : : If COST_VEC is passed, calculate costs but don't change anything,
11945 : : otherwise, vectorize STMT_INFO: create a vectorized stmt using
11946 : : VEC_COND_EXPR to replace it, and insert it at GSI.
11947 : :
11948 : : When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
11949 : :
11950 : : Return true if STMT_INFO is vectorizable in this way. */
11951 : :
11952 : : static bool
11953 : 581768 : vectorizable_condition (vec_info *vinfo,
11954 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
11955 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
11956 : : {
11957 : 581768 : tree scalar_dest = NULL_TREE;
11958 : 581768 : tree vec_dest = NULL_TREE;
11959 : 581768 : tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
11960 : 581768 : tree then_clause, else_clause;
11961 : 581768 : tree comp_vectype = NULL_TREE;
11962 : 581768 : tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
11963 : 581768 : tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
11964 : 581768 : tree vec_compare;
11965 : 581768 : tree new_temp;
11966 : 581768 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
11967 : 581768 : enum vect_def_type dts[4]
11968 : : = {vect_unknown_def_type, vect_unknown_def_type,
11969 : : vect_unknown_def_type, vect_unknown_def_type};
11970 : 581768 : enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
11971 : 581768 : int i;
11972 : 581768 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
11973 : 581768 : vec<tree> vec_oprnds0 = vNULL;
11974 : 581768 : vec<tree> vec_oprnds1 = vNULL;
11975 : 581768 : vec<tree> vec_oprnds2 = vNULL;
11976 : 581768 : vec<tree> vec_oprnds3 = vNULL;
11977 : 581768 : tree vec_cmp_type;
11978 : 581768 : bool masked = false;
11979 : :
11980 : 581768 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
11981 : : return false;
11982 : :
11983 : : /* Is vectorizable conditional operation? */
11984 : 899498 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
11985 : 345684 : if (!stmt)
11986 : : return false;
11987 : :
11988 : 345684 : code = gimple_assign_rhs_code (stmt);
11989 : 345684 : if (code != COND_EXPR)
11990 : : return false;
11991 : :
11992 : 27987 : int reduc_index = SLP_TREE_REDUC_IDX (slp_node);
11993 : 27987 : vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
11994 : 27987 : bool nested_cycle_p = false;
11995 : 27987 : bool for_reduction = vect_is_reduction (stmt_info);
11996 : 27987 : if (for_reduction)
11997 : : {
11998 : 561 : if (SLP_TREE_LANES (slp_node) > 1)
11999 : : return false;
12000 : : /* ??? With a reduction path we do not get at the reduction info from
12001 : : every stmt, use the conservative default setting then. */
12002 : 639 : if (STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)))
12003 : : {
12004 : 535 : vect_reduc_info reduc_info
12005 : 535 : = info_for_reduction (loop_vinfo, slp_node);
12006 : 535 : reduction_type = VECT_REDUC_INFO_TYPE (reduc_info);
12007 : 535 : nested_cycle_p = nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
12008 : : stmt_info);
12009 : : }
12010 : : }
12011 : : else
12012 : : {
12013 : 27426 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12014 : : return false;
12015 : : }
12016 : :
12017 : 27987 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12018 : 27987 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12019 : :
12020 : 27987 : int vec_num = vect_get_num_copies (vinfo, slp_node);
12021 : :
12022 : 27987 : cond_expr = gimple_assign_rhs1 (stmt);
12023 : 27987 : gcc_assert (! COMPARISON_CLASS_P (cond_expr));
12024 : :
12025 : 27987 : if (!vect_is_simple_cond (cond_expr, vinfo, slp_node,
12026 : : &comp_vectype, &dts[0], vectype)
12027 : 27987 : || !comp_vectype)
12028 : : return false;
12029 : :
12030 : 27956 : unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
12031 : 27956 : slp_tree then_slp_node, else_slp_node;
12032 : 27956 : if (!vect_is_simple_use (vinfo, slp_node, 1 + op_adjust,
12033 : : &then_clause, &then_slp_node, &dts[2], &vectype1))
12034 : : return false;
12035 : 27956 : if (!vect_is_simple_use (vinfo, slp_node, 2 + op_adjust,
12036 : : &else_clause, &else_slp_node, &dts[3], &vectype2))
12037 : : return false;
12038 : :
12039 : 27956 : if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
12040 : : return false;
12041 : :
12042 : 27956 : if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
12043 : : return false;
12044 : :
12045 : 27956 : masked = !COMPARISON_CLASS_P (cond_expr);
12046 : 27956 : vec_cmp_type = truth_type_for (comp_vectype);
12047 : 27956 : if (vec_cmp_type == NULL_TREE
12048 : 55912 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
12049 : 27956 : TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
12050 : 0 : return false;
12051 : :
12052 : 27956 : cond_code = TREE_CODE (cond_expr);
12053 : 27956 : if (!masked)
12054 : : {
12055 : 0 : cond_expr0 = TREE_OPERAND (cond_expr, 0);
12056 : 0 : cond_expr1 = TREE_OPERAND (cond_expr, 1);
12057 : : }
12058 : :
12059 : : /* For conditional reductions, the "then" value needs to be the candidate
12060 : : value calculated by this iteration while the "else" value needs to be
12061 : : the result carried over from previous iterations. If the COND_EXPR
12062 : : is the other way around, we need to swap it. */
12063 : 27956 : bool must_invert_cmp_result = false;
12064 : 27956 : if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
12065 : : {
12066 : 0 : if (masked)
12067 : 0 : must_invert_cmp_result = true;
12068 : : else
12069 : : {
12070 : 0 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
12071 : 0 : tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
12072 : 0 : if (new_code == ERROR_MARK)
12073 : : must_invert_cmp_result = true;
12074 : : else
12075 : : {
12076 : 0 : cond_code = new_code;
12077 : : /* Make sure we don't accidentally use the old condition. */
12078 : 0 : cond_expr = NULL_TREE;
12079 : : }
12080 : : }
12081 : : /* ??? The vectorized operand query below doesn't allow swapping
12082 : : this way for SLP. */
12083 : 0 : return false;
12084 : : /* std::swap (then_clause, else_clause); */
12085 : : }
12086 : :
12087 : 27956 : if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
12088 : : {
12089 : : /* Boolean values may have another representation in vectors
12090 : : and therefore we prefer bit operations over comparison for
12091 : : them (which also works for scalar masks). We store opcodes
12092 : : to use in bitop1 and bitop2. Statement is vectorized as
12093 : : BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
12094 : : depending on bitop1 and bitop2 arity. */
12095 : 0 : switch (cond_code)
12096 : : {
12097 : : case GT_EXPR:
12098 : : bitop1 = BIT_NOT_EXPR;
12099 : : bitop2 = BIT_AND_EXPR;
12100 : : break;
12101 : 0 : case GE_EXPR:
12102 : 0 : bitop1 = BIT_NOT_EXPR;
12103 : 0 : bitop2 = BIT_IOR_EXPR;
12104 : 0 : break;
12105 : 0 : case LT_EXPR:
12106 : 0 : bitop1 = BIT_NOT_EXPR;
12107 : 0 : bitop2 = BIT_AND_EXPR;
12108 : 0 : std::swap (cond_expr0, cond_expr1);
12109 : 0 : break;
12110 : 0 : case LE_EXPR:
12111 : 0 : bitop1 = BIT_NOT_EXPR;
12112 : 0 : bitop2 = BIT_IOR_EXPR;
12113 : 0 : std::swap (cond_expr0, cond_expr1);
12114 : 0 : break;
12115 : 0 : case NE_EXPR:
12116 : 0 : bitop1 = BIT_XOR_EXPR;
12117 : 0 : break;
12118 : 0 : case EQ_EXPR:
12119 : 0 : bitop1 = BIT_XOR_EXPR;
12120 : 0 : bitop2 = BIT_NOT_EXPR;
12121 : 0 : break;
12122 : : default:
12123 : : return false;
12124 : : }
12125 : : cond_code = SSA_NAME;
12126 : : }
12127 : :
12128 : 27956 : if (TREE_CODE_CLASS (cond_code) == tcc_comparison
12129 : 0 : && reduction_type == EXTRACT_LAST_REDUCTION
12130 : 27956 : && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
12131 : : {
12132 : 0 : if (dump_enabled_p ())
12133 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12134 : : "reduction comparison operation not supported.\n");
12135 : 0 : return false;
12136 : : }
12137 : :
12138 : 27956 : if (cost_vec)
12139 : : {
12140 : 19492 : if (bitop1 != NOP_EXPR)
12141 : : {
12142 : 0 : machine_mode mode = TYPE_MODE (comp_vectype);
12143 : 0 : optab optab;
12144 : :
12145 : 0 : optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
12146 : 0 : if (!optab || !can_implement_p (optab, mode))
12147 : 0 : return false;
12148 : :
12149 : 0 : if (bitop2 != NOP_EXPR)
12150 : : {
12151 : 0 : optab = optab_for_tree_code (bitop2, comp_vectype,
12152 : : optab_default);
12153 : 0 : if (!optab || !can_implement_p (optab, mode))
12154 : 0 : return false;
12155 : : }
12156 : : }
12157 : :
12158 : 19492 : vect_cost_for_stmt kind = vector_stmt;
12159 : 19492 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12160 : : /* Count one reduction-like operation per vector. */
12161 : : kind = vec_to_scalar;
12162 : 19492 : else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
12163 : 19492 : || (!masked
12164 : 0 : && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
12165 : : cond_code)
12166 : 0 : || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
12167 : 2 : return false;
12168 : :
12169 : 19490 : if (!vect_maybe_update_slp_op_vectype (SLP_TREE_CHILDREN (slp_node)[0],
12170 : : comp_vectype)
12171 : 19490 : || (op_adjust == 1
12172 : 0 : && !vect_maybe_update_slp_op_vectype
12173 : 0 : (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
12174 : 19490 : || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
12175 : 38980 : || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype))
12176 : : {
12177 : 0 : if (dump_enabled_p ())
12178 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12179 : : "incompatible vector types for invariants\n");
12180 : 0 : return false;
12181 : : }
12182 : :
12183 : 19490 : if (loop_vinfo && for_reduction
12184 : 406 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
12185 : : {
12186 : 65 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12187 : : {
12188 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12189 : : vectype, OPTIMIZE_FOR_SPEED))
12190 : 0 : vect_record_loop_len (loop_vinfo,
12191 : : &LOOP_VINFO_LENS (loop_vinfo),
12192 : : vec_num, vectype, 1);
12193 : : else
12194 : 0 : vect_record_loop_mask (loop_vinfo,
12195 : : &LOOP_VINFO_MASKS (loop_vinfo),
12196 : : vec_num, vectype, NULL);
12197 : : }
12198 : : /* Extra inactive lanes should be safe for vect_nested_cycle. */
12199 : 65 : else if (!nested_cycle_p)
12200 : : {
12201 : 65 : if (dump_enabled_p ())
12202 : 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12203 : : "conditional reduction prevents the use"
12204 : : " of partial vectors.\n");
12205 : 65 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
12206 : : }
12207 : : }
12208 : :
12209 : 19490 : SLP_TREE_TYPE (slp_node) = condition_vec_info_type;
12210 : 19490 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec, kind);
12211 : 19490 : return true;
12212 : : }
12213 : :
12214 : : /* Transform. */
12215 : :
12216 : : /* Handle def. */
12217 : 8464 : scalar_dest = gimple_assign_lhs (stmt);
12218 : 8464 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12219 : 8464 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
12220 : :
12221 : 8464 : bool swap_cond_operands = false;
12222 : :
12223 : : /* See whether another part of the vectorized code applies a loop
12224 : : mask to the condition, or to its inverse. */
12225 : :
12226 : 8464 : vec_loop_masks *masks = NULL;
12227 : 8464 : vec_loop_lens *lens = NULL;
12228 : 8464 : if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12229 : : {
12230 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12231 : 0 : lens = &LOOP_VINFO_LENS (loop_vinfo);
12232 : : }
12233 : 8464 : else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
12234 : : {
12235 : 3 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12236 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12237 : : else
12238 : : {
12239 : 3 : scalar_cond_masked_key cond (cond_expr, 1);
12240 : 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12241 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12242 : : else
12243 : : {
12244 : 3 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
12245 : 3 : tree_code orig_code = cond.code;
12246 : 3 : cond.code = invert_tree_comparison (cond.code, honor_nans);
12247 : 3 : if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
12248 : : {
12249 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12250 : 0 : cond_code = cond.code;
12251 : 0 : swap_cond_operands = true;
12252 : : }
12253 : : else
12254 : : {
12255 : : /* Try the inverse of the current mask. We check if the
12256 : : inverse mask is live and if so we generate a negate of
12257 : : the current mask such that we still honor NaNs. */
12258 : 3 : cond.inverted_p = true;
12259 : 3 : cond.code = orig_code;
12260 : 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12261 : : {
12262 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12263 : 0 : cond_code = cond.code;
12264 : 0 : swap_cond_operands = true;
12265 : 0 : must_invert_cmp_result = true;
12266 : : }
12267 : : }
12268 : : }
12269 : : }
12270 : : }
12271 : :
12272 : : /* Handle cond expr. */
12273 : 8464 : if (masked)
12274 : 8464 : vect_get_vec_defs (vinfo, slp_node,
12275 : : cond_expr, &vec_oprnds0,
12276 : : then_clause, &vec_oprnds2,
12277 : : reduction_type != EXTRACT_LAST_REDUCTION
12278 : : ? else_clause : NULL, &vec_oprnds3);
12279 : : else
12280 : 0 : vect_get_vec_defs (vinfo, slp_node,
12281 : : cond_expr0, &vec_oprnds0,
12282 : : cond_expr1, &vec_oprnds1,
12283 : : then_clause, &vec_oprnds2,
12284 : : reduction_type != EXTRACT_LAST_REDUCTION
12285 : : ? else_clause : NULL, &vec_oprnds3);
12286 : :
12287 : 8464 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12288 : 0 : vec_else_clause = else_clause;
12289 : :
12290 : : /* Arguments are ready. Create the new vector stmt. */
12291 : 19916 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
12292 : : {
12293 : 11452 : vec_then_clause = vec_oprnds2[i];
12294 : 11452 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12295 : 11452 : vec_else_clause = vec_oprnds3[i];
12296 : :
12297 : 11452 : if (swap_cond_operands)
12298 : 0 : std::swap (vec_then_clause, vec_else_clause);
12299 : :
12300 : 11452 : if (masked)
12301 : : vec_compare = vec_cond_lhs;
12302 : : else
12303 : : {
12304 : 0 : vec_cond_rhs = vec_oprnds1[i];
12305 : 0 : if (bitop1 == NOP_EXPR)
12306 : : {
12307 : 0 : gimple_seq stmts = NULL;
12308 : 0 : vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
12309 : : vec_cond_lhs, vec_cond_rhs);
12310 : 0 : gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
12311 : : }
12312 : : else
12313 : : {
12314 : 0 : new_temp = make_ssa_name (vec_cmp_type);
12315 : 0 : gassign *new_stmt;
12316 : 0 : if (bitop1 == BIT_NOT_EXPR)
12317 : 0 : new_stmt = gimple_build_assign (new_temp, bitop1,
12318 : : vec_cond_rhs);
12319 : : else
12320 : 0 : new_stmt
12321 : 0 : = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
12322 : : vec_cond_rhs);
12323 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12324 : 0 : if (bitop2 == NOP_EXPR)
12325 : : vec_compare = new_temp;
12326 : 0 : else if (bitop2 == BIT_NOT_EXPR
12327 : 0 : && reduction_type != EXTRACT_LAST_REDUCTION)
12328 : : {
12329 : : /* Instead of doing ~x ? y : z do x ? z : y. */
12330 : : vec_compare = new_temp;
12331 : : std::swap (vec_then_clause, vec_else_clause);
12332 : : }
12333 : : else
12334 : : {
12335 : 0 : vec_compare = make_ssa_name (vec_cmp_type);
12336 : 0 : if (bitop2 == BIT_NOT_EXPR)
12337 : 0 : new_stmt
12338 : 0 : = gimple_build_assign (vec_compare, bitop2, new_temp);
12339 : : else
12340 : 0 : new_stmt
12341 : 0 : = gimple_build_assign (vec_compare, bitop2,
12342 : : vec_cond_lhs, new_temp);
12343 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
12344 : : new_stmt, gsi);
12345 : : }
12346 : : }
12347 : : }
12348 : :
12349 : : /* If we decided to apply a loop mask to the result of the vector
12350 : : comparison, AND the comparison with the mask now. Later passes
12351 : : should then be able to reuse the AND results between mulitple
12352 : : vector statements.
12353 : :
12354 : : For example:
12355 : : for (int i = 0; i < 100; ++i)
12356 : : x[i] = y[i] ? z[i] : 10;
12357 : :
12358 : : results in following optimized GIMPLE:
12359 : :
12360 : : mask__35.8_43 = vect__4.7_41 != { 0, ... };
12361 : : vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
12362 : : _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
12363 : : vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
12364 : : vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
12365 : : vect_iftmp.11_47, { 10, ... }>;
12366 : :
12367 : : instead of using a masked and unmasked forms of
12368 : : vec != { 0, ... } (masked in the MASK_LOAD,
12369 : : unmasked in the VEC_COND_EXPR). */
12370 : :
12371 : : /* Force vec_compare to be an SSA_NAME rather than a comparison,
12372 : : in cases where that's necessary. */
12373 : :
12374 : 11452 : tree len = NULL_TREE, bias = NULL_TREE;
12375 : 11452 : if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
12376 : : {
12377 : 0 : if (!is_gimple_val (vec_compare))
12378 : : {
12379 : 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12380 : 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12381 : : vec_compare);
12382 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12383 : 0 : vec_compare = vec_compare_name;
12384 : : }
12385 : :
12386 : 0 : if (must_invert_cmp_result)
12387 : : {
12388 : 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12389 : 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12390 : : BIT_NOT_EXPR,
12391 : : vec_compare);
12392 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12393 : 0 : vec_compare = vec_compare_name;
12394 : : }
12395 : :
12396 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12397 : : vectype, OPTIMIZE_FOR_SPEED))
12398 : : {
12399 : 0 : if (lens)
12400 : : {
12401 : 0 : len = vect_get_loop_len (loop_vinfo, gsi, lens,
12402 : : vec_num, vectype, i, 1);
12403 : 0 : signed char biasval
12404 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
12405 : 0 : bias = build_int_cst (intQI_type_node, biasval);
12406 : : }
12407 : : else
12408 : : {
12409 : 0 : len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12410 : 0 : bias = build_int_cst (intQI_type_node, 0);
12411 : : }
12412 : : }
12413 : 0 : if (masks)
12414 : : {
12415 : 0 : tree loop_mask
12416 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
12417 : : vectype, i);
12418 : 0 : tree tmp2 = make_ssa_name (vec_cmp_type);
12419 : 0 : gassign *g
12420 : 0 : = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
12421 : : loop_mask);
12422 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
12423 : 0 : vec_compare = tmp2;
12424 : : }
12425 : : }
12426 : :
12427 : 0 : gimple *new_stmt;
12428 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12429 : : {
12430 : 0 : gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
12431 : 0 : tree lhs = gimple_get_lhs (old_stmt);
12432 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12433 : 0 : lhs = copy_ssa_name (lhs);
12434 : 0 : if (len)
12435 : 0 : new_stmt = gimple_build_call_internal
12436 : 0 : (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
12437 : : vec_then_clause, len, bias);
12438 : : else
12439 : 0 : new_stmt = gimple_build_call_internal
12440 : 0 : (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
12441 : : vec_then_clause);
12442 : 0 : gimple_call_set_lhs (new_stmt, lhs);
12443 : 0 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12444 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12445 : : {
12446 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12447 : 0 : vec_else_clause = lhs;
12448 : : }
12449 : 0 : else if (old_stmt == gsi_stmt (*gsi))
12450 : 0 : vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
12451 : : else
12452 : : {
12453 : : /* In this case we're moving the definition to later in the
12454 : : block. That doesn't matter because the only uses of the
12455 : : lhs are in phi statements. */
12456 : 0 : gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
12457 : 0 : gsi_remove (&old_gsi, true);
12458 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12459 : : }
12460 : : }
12461 : : else
12462 : : {
12463 : 11452 : new_temp = make_ssa_name (vec_dest);
12464 : 11452 : new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
12465 : : vec_then_clause, vec_else_clause);
12466 : 11452 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12467 : : }
12468 : 11452 : slp_node->push_vec_def (new_stmt);
12469 : : }
12470 : :
12471 : 8464 : vec_oprnds0.release ();
12472 : 8464 : vec_oprnds1.release ();
12473 : 8464 : vec_oprnds2.release ();
12474 : 8464 : vec_oprnds3.release ();
12475 : :
12476 : 8464 : return true;
12477 : : }
12478 : :
12479 : : /* Helper of vectorizable_comparison.
12480 : :
12481 : : Check if STMT_INFO is comparison expression CODE that can be vectorized.
12482 : : If COST_VEC is passed, calculate costs but don't change anything,
12483 : : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12484 : : it at GSI.
12485 : :
12486 : : Return true if STMT_INFO is vectorizable in this way. */
12487 : :
12488 : : static bool
12489 : 326164 : vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
12490 : : stmt_vec_info stmt_info, tree_code code,
12491 : : gimple_stmt_iterator *gsi,
12492 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12493 : : {
12494 : 326164 : tree lhs, rhs1, rhs2;
12495 : 326164 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12496 : 326164 : tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
12497 : 326164 : tree new_temp;
12498 : 326164 : enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
12499 : 326164 : poly_uint64 nunits;
12500 : 326164 : enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12501 : 326164 : int i;
12502 : 326164 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12503 : 326164 : vec<tree> vec_oprnds0 = vNULL;
12504 : 326164 : vec<tree> vec_oprnds1 = vNULL;
12505 : 326164 : tree mask_type;
12506 : 326164 : tree mask = NULL_TREE;
12507 : :
12508 : 326164 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12509 : : return false;
12510 : :
12511 : 326164 : if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
12512 : : return false;
12513 : :
12514 : 146656 : mask_type = vectype;
12515 : 146656 : nunits = TYPE_VECTOR_SUBPARTS (vectype);
12516 : :
12517 : 146656 : if (TREE_CODE_CLASS (code) != tcc_comparison)
12518 : : return false;
12519 : :
12520 : 145077 : slp_tree slp_rhs1, slp_rhs2;
12521 : 145077 : if (!vect_is_simple_use (vinfo, slp_node,
12522 : : 0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
12523 : : return false;
12524 : :
12525 : 145077 : if (!vect_is_simple_use (vinfo, slp_node,
12526 : : 1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
12527 : : return false;
12528 : :
12529 : 110664 : if (vectype1 && vectype2
12530 : 211787 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12531 : 66710 : TYPE_VECTOR_SUBPARTS (vectype2)))
12532 : 16 : return false;
12533 : :
12534 : 145061 : vectype = vectype1 ? vectype1 : vectype2;
12535 : :
12536 : : /* Invariant comparison. */
12537 : 145061 : if (!vectype)
12538 : : {
12539 : 30047 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
12540 : 30047 : if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
12541 : 7 : return false;
12542 : : }
12543 : 115014 : else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
12544 : : return false;
12545 : :
12546 : : /* Can't compare mask and non-mask types. */
12547 : 110648 : if (vectype1 && vectype2
12548 : 344566 : && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
12549 : : return false;
12550 : :
12551 : : /* Boolean values may have another representation in vectors
12552 : : and therefore we prefer bit operations over comparison for
12553 : : them (which also works for scalar masks). We store opcodes
12554 : : to use in bitop1 and bitop2. Statement is vectorized as
12555 : : BITOP2 (rhs1 BITOP1 rhs2) or
12556 : : rhs1 BITOP2 (BITOP1 rhs2)
12557 : : depending on bitop1 and bitop2 arity. */
12558 : 145046 : bool swap_p = false;
12559 : 145046 : if (VECTOR_BOOLEAN_TYPE_P (vectype))
12560 : : {
12561 : 646 : if (code == GT_EXPR)
12562 : : {
12563 : : bitop1 = BIT_NOT_EXPR;
12564 : : bitop2 = BIT_AND_EXPR;
12565 : : }
12566 : : else if (code == GE_EXPR)
12567 : : {
12568 : : bitop1 = BIT_NOT_EXPR;
12569 : : bitop2 = BIT_IOR_EXPR;
12570 : : }
12571 : : else if (code == LT_EXPR)
12572 : : {
12573 : : bitop1 = BIT_NOT_EXPR;
12574 : : bitop2 = BIT_AND_EXPR;
12575 : : swap_p = true;
12576 : : }
12577 : : else if (code == LE_EXPR)
12578 : : {
12579 : : bitop1 = BIT_NOT_EXPR;
12580 : : bitop2 = BIT_IOR_EXPR;
12581 : : swap_p = true;
12582 : : }
12583 : : else
12584 : : {
12585 : : bitop1 = BIT_XOR_EXPR;
12586 : : if (code == EQ_EXPR)
12587 : : bitop2 = BIT_NOT_EXPR;
12588 : : }
12589 : : }
12590 : :
12591 : 145046 : if (cost_vec)
12592 : : {
12593 : 132719 : if (bitop1 == NOP_EXPR)
12594 : : {
12595 : 132212 : if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
12596 : : return false;
12597 : : }
12598 : : else
12599 : : {
12600 : 507 : machine_mode mode = TYPE_MODE (vectype);
12601 : 507 : optab optab;
12602 : :
12603 : 507 : optab = optab_for_tree_code (bitop1, vectype, optab_default);
12604 : 507 : if (!optab || !can_implement_p (optab, mode))
12605 : 0 : return false;
12606 : :
12607 : 507 : if (bitop2 != NOP_EXPR)
12608 : : {
12609 : 91 : optab = optab_for_tree_code (bitop2, vectype, optab_default);
12610 : 91 : if (!optab || !can_implement_p (optab, mode))
12611 : 0 : return false;
12612 : : }
12613 : : }
12614 : :
12615 : : /* Put types on constant and invariant SLP children. */
12616 : 123391 : if (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
12617 : 123391 : || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype))
12618 : : {
12619 : 2 : if (dump_enabled_p ())
12620 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12621 : : "incompatible vector types for invariants\n");
12622 : 2 : return false;
12623 : : }
12624 : :
12625 : 123389 : vect_model_simple_cost (vinfo, 1 + (bitop2 != NOP_EXPR),
12626 : : slp_node, cost_vec);
12627 : 123389 : return true;
12628 : : }
12629 : :
12630 : : /* Transform. */
12631 : :
12632 : : /* Handle def. */
12633 : 12327 : lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
12634 : 12327 : if (lhs)
12635 : 12327 : mask = vect_create_destination_var (lhs, mask_type);
12636 : :
12637 : 12327 : vect_get_vec_defs (vinfo, slp_node, rhs1, &vec_oprnds0, rhs2, &vec_oprnds1);
12638 : 12327 : if (swap_p)
12639 : 58 : std::swap (vec_oprnds0, vec_oprnds1);
12640 : :
12641 : : /* Arguments are ready. Create the new vector stmt. */
12642 : 31204 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
12643 : : {
12644 : 18877 : gimple *new_stmt;
12645 : 18877 : vec_rhs2 = vec_oprnds1[i];
12646 : :
12647 : 18877 : if (lhs)
12648 : 18877 : new_temp = make_ssa_name (mask);
12649 : : else
12650 : 0 : new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
12651 : 18877 : if (bitop1 == NOP_EXPR)
12652 : : {
12653 : 18733 : new_stmt = gimple_build_assign (new_temp, code,
12654 : : vec_rhs1, vec_rhs2);
12655 : 18733 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12656 : : }
12657 : : else
12658 : : {
12659 : 144 : if (bitop1 == BIT_NOT_EXPR)
12660 : 84 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
12661 : : else
12662 : 60 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
12663 : : vec_rhs2);
12664 : 144 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12665 : 144 : if (bitop2 != NOP_EXPR)
12666 : : {
12667 : 84 : tree res = make_ssa_name (mask);
12668 : 84 : if (bitop2 == BIT_NOT_EXPR)
12669 : 0 : new_stmt = gimple_build_assign (res, bitop2, new_temp);
12670 : : else
12671 : 84 : new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
12672 : : new_temp);
12673 : 84 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12674 : : }
12675 : : }
12676 : 18877 : slp_node->push_vec_def (new_stmt);
12677 : : }
12678 : :
12679 : 12327 : vec_oprnds0.release ();
12680 : 12327 : vec_oprnds1.release ();
12681 : :
12682 : 12327 : return true;
12683 : : }
12684 : :
12685 : : /* vectorizable_comparison.
12686 : :
12687 : : Check if STMT_INFO is comparison expression that can be vectorized.
12688 : : If COST_VEC is passed, calculate costs but don't change anything,
12689 : : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12690 : : it at GSI.
12691 : :
12692 : : Return true if STMT_INFO is vectorizable in this way. */
12693 : :
12694 : : static bool
12695 : 566141 : vectorizable_comparison (vec_info *vinfo,
12696 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12697 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12698 : : {
12699 : 566141 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12700 : :
12701 : 566141 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12702 : : return false;
12703 : :
12704 : 566141 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12705 : : return false;
12706 : :
12707 : 756589 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12708 : 323963 : if (!stmt)
12709 : : return false;
12710 : :
12711 : 323963 : enum tree_code code = gimple_assign_rhs_code (stmt);
12712 : 323963 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12713 : 323963 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
12714 : : slp_node, cost_vec))
12715 : : return false;
12716 : :
12717 : 133515 : if (cost_vec)
12718 : 121188 : SLP_TREE_TYPE (slp_node) = comparison_vec_info_type;
12719 : :
12720 : : return true;
12721 : : }
12722 : :
12723 : : /* Check to see if the current early break given in STMT_INFO is valid for
12724 : : vectorization. */
12725 : :
12726 : : bool
12727 : 214235 : vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
12728 : : gimple_stmt_iterator *gsi,
12729 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12730 : : {
12731 : 214235 : if (!is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
12732 : : return false;
12733 : :
12734 : 58331 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
12735 : : return false;
12736 : :
12737 : 58331 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
12738 : : return false;
12739 : :
12740 : 58331 : DUMP_VECT_SCOPE ("vectorizable_early_exit");
12741 : :
12742 : 58331 : auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
12743 : :
12744 : : /* For SLP we don't want to use the type of the operands of the SLP node, when
12745 : : vectorizing using SLP slp_node will be the children of the gcond and we
12746 : : want to use the type of the direct children which since the gcond is root
12747 : : will be the current node, rather than a child node as vect_is_simple_use
12748 : : assumes. */
12749 : 58331 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12750 : 58331 : if (!vectype)
12751 : : return false;
12752 : :
12753 : 58331 : machine_mode mode = TYPE_MODE (vectype);
12754 : 58331 : int vec_num = vect_get_num_copies (loop_vinfo, slp_node);
12755 : :
12756 : 58331 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
12757 : 58331 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
12758 : 58331 : bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
12759 : 58331 : bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
12760 : :
12761 : : /* Now build the new conditional. Pattern gimple_conds get dropped during
12762 : : codegen so we must replace the original insn. */
12763 : 58331 : gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
12764 : 58331 : gcond *cond_stmt = as_a <gcond *>(orig_stmt);
12765 : :
12766 : 58331 : tree vectype_out = vectype;
12767 : 58331 : auto bb = gimple_bb (cond_stmt);
12768 : 58331 : edge exit_true_edge = EDGE_SUCC (bb, 0);
12769 : 58331 : if (exit_true_edge->flags & EDGE_FALSE_VALUE)
12770 : 832 : exit_true_edge = EDGE_SUCC (bb, 1);
12771 : 58331 : gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
12772 : :
12773 : : /* When vectorizing we assume that if the branch edge is taken that we're
12774 : : exiting the loop. This is not however always the case as the compiler will
12775 : : rewrite conditions to always be a comparison against 0. To do this it
12776 : : sometimes flips the edges. This is fine for scalar, but for vector we
12777 : : then have to negate the result of the test, as we're still assuming that if
12778 : : you take the branch edge that we found the exit condition. i.e. we need to
12779 : : know whether we are generating a `forall` or an `exist` condition. */
12780 : 116662 : bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
12781 : 58331 : exit_true_edge->dest);
12782 : :
12783 : : /* See if we support ADDHN and use that for the reduction. */
12784 : 58331 : internal_fn ifn = IFN_VEC_TRUNC_ADD_HIGH;
12785 : 58331 : bool addhn_supported_p
12786 : 58331 : = direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_BOTH);
12787 : 58331 : tree narrow_type = NULL_TREE;
12788 : 58331 : if (addhn_supported_p)
12789 : : {
12790 : : /* Calculate the narrowing type for the result. */
12791 : 0 : auto halfprec = TYPE_PRECISION (TREE_TYPE (vectype)) / 2;
12792 : 0 : auto unsignedp = TYPE_UNSIGNED (TREE_TYPE (vectype));
12793 : 0 : tree itype = build_nonstandard_integer_type (halfprec, unsignedp);
12794 : 0 : tree tmp_type = build_vector_type (itype, TYPE_VECTOR_SUBPARTS (vectype));
12795 : 0 : narrow_type = truth_type_for (tmp_type);
12796 : :
12797 : 0 : if (direct_optab_handler (cbranch_optab, TYPE_MODE (narrow_type))
12798 : : == CODE_FOR_nothing)
12799 : : {
12800 : 0 : if (dump_enabled_p ())
12801 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12802 : : "can't use ADDHN reduction because cbranch for "
12803 : : "the narrowed type is not supported by the "
12804 : : "target.\n");
12805 : : addhn_supported_p = false;
12806 : : }
12807 : : }
12808 : :
12809 : : /* Analyze only. */
12810 : 58331 : if (cost_vec)
12811 : : {
12812 : 56693 : if (!addhn_supported_p
12813 : 113386 : && direct_optab_handler (cbranch_optab, mode) == CODE_FOR_nothing)
12814 : : {
12815 : 54492 : if (dump_enabled_p ())
12816 : 567 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12817 : : "can't vectorize early exit because the "
12818 : : "target doesn't support flag setting vector "
12819 : : "comparisons.\n");
12820 : 54492 : return false;
12821 : : }
12822 : :
12823 : 2201 : if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code, gsi,
12824 : : slp_node, cost_vec))
12825 : : return false;
12826 : :
12827 : 2201 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
12828 : : {
12829 : 1125 : if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
12830 : : OPTIMIZE_FOR_SPEED))
12831 : 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
12832 : : else
12833 : 1125 : vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
12834 : : }
12835 : :
12836 : 2201 : return true;
12837 : : }
12838 : :
12839 : : /* Tranform. */
12840 : :
12841 : 1638 : tree new_temp = NULL_TREE;
12842 : 1638 : gimple *new_stmt = NULL;
12843 : :
12844 : 1638 : if (dump_enabled_p ())
12845 : 347 : dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
12846 : :
12847 : : /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
12848 : : roots and so by the time we get to them we have already codegened the SLP tree
12849 : : and so we shouldn't try to do so again. The arguments have already been
12850 : : vectorized. It's not very clean to do this here, But the masking code below is
12851 : : complex and this keeps it all in one place to ease fixes and backports. Once we
12852 : : drop the non-SLP loop vect or split vectorizable_* this can be simplified. */
12853 : :
12854 : 1638 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
12855 : 1638 : basic_block cond_bb = gimple_bb (stmt);
12856 : 1638 : gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb);
12857 : :
12858 : 1638 : auto_vec<tree> stmts;
12859 : 1638 : stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
12860 : :
12861 : : /* If we're comparing against a previous forall we need to negate the resullts
12862 : : before we do the final comparison or reduction. */
12863 : 1638 : if (flipped)
12864 : : {
12865 : : /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
12866 : : if (any(~mask)) by negating the masks and flipping the branches.
12867 : :
12868 : : 1. For unmasked loops we simply reduce the ~mask.
12869 : : 2. For masked loops we reduce (~mask & loop_mask) which is the same as
12870 : : doing (mask & loop_mask) ^ loop_mask. */
12871 : 222 : for (unsigned i = 0; i < stmts.length (); i++)
12872 : : {
12873 : 133 : tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
12874 : 133 : auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
12875 : 133 : vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
12876 : : &cond_gsi);
12877 : 133 : stmts[i] = inv_lhs;
12878 : : }
12879 : :
12880 : 89 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
12881 : 89 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
12882 : : }
12883 : :
12884 : : /* Determine if we need to reduce the final value. */
12885 : 1638 : if (stmts.length () > 1)
12886 : : {
12887 : : /* We build the reductions in a way to maintain as much parallelism as
12888 : : possible. */
12889 : 399 : auto_vec<tree> workset (stmts.length ());
12890 : :
12891 : : /* Mask the statements as we queue them up. Normally we loop over
12892 : : vec_num, but since we inspect the exact results of vectorization
12893 : : we don't need to and instead can just use the stmts themselves. */
12894 : 399 : if (masked_loop_p)
12895 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
12896 : : {
12897 : 0 : tree stmt_mask
12898 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
12899 : : vectype, i);
12900 : 0 : stmt_mask
12901 : 0 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
12902 : 0 : stmts[i], &cond_gsi);
12903 : 0 : workset.quick_push (stmt_mask);
12904 : : }
12905 : 399 : else if (len_loop_p)
12906 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
12907 : : {
12908 : 0 : tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
12909 : : lens, vec_num,
12910 : 0 : vectype, stmts[i], i, 1);
12911 : :
12912 : 0 : workset.quick_push (len_mask);
12913 : : }
12914 : : else
12915 : 399 : workset.splice (stmts);
12916 : :
12917 : 938 : while (workset.length () > 1)
12918 : : {
12919 : 539 : tree arg0 = workset.pop ();
12920 : 539 : tree arg1 = workset.pop ();
12921 : 539 : if (addhn_supported_p && workset.length () == 0)
12922 : : {
12923 : 0 : new_stmt = gimple_build_call_internal (ifn, 2, arg0, arg1);
12924 : 0 : vectype_out = narrow_type;
12925 : 0 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
12926 : 0 : gimple_call_set_lhs (as_a <gcall *> (new_stmt), new_temp);
12927 : 0 : gimple_call_set_nothrow (as_a <gcall *> (new_stmt), true);
12928 : : }
12929 : : else
12930 : : {
12931 : 539 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
12932 : 539 : new_stmt
12933 : 539 : = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
12934 : : }
12935 : 539 : vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
12936 : : &cond_gsi);
12937 : 539 : workset.quick_insert (0, new_temp);
12938 : : }
12939 : 399 : }
12940 : : else
12941 : : {
12942 : 1239 : new_temp = stmts[0];
12943 : 1239 : if (masked_loop_p)
12944 : : {
12945 : 0 : tree mask
12946 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, 1, vectype, 0);
12947 : 0 : new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
12948 : : new_temp, &cond_gsi);
12949 : : }
12950 : 1239 : else if (len_loop_p)
12951 : 0 : new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
12952 : : 1, vectype, new_temp, 0, 1);
12953 : : }
12954 : :
12955 : 1638 : gcc_assert (new_temp);
12956 : :
12957 : 1638 : tree cst = build_zero_cst (vectype_out);
12958 : 1638 : gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
12959 : 1638 : update_stmt (orig_stmt);
12960 : :
12961 : : /* ??? */
12962 : 1638 : SLP_TREE_VEC_DEFS (slp_node).truncate (0);
12963 : :
12964 : 1638 : return true;
12965 : 1638 : }
12966 : :
12967 : : /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
12968 : : can handle all live statements in the node. Otherwise return true
12969 : : if STMT_INFO is not live or if vectorizable_live_operation can handle it.
12970 : : VEC_STMT_P is as for vectorizable_live_operation. */
12971 : :
12972 : : static bool
12973 : 1408967 : can_vectorize_live_stmts (vec_info *vinfo,
12974 : : slp_tree slp_node, slp_instance slp_node_instance,
12975 : : bool vec_stmt_p,
12976 : : stmt_vector_for_cost *cost_vec)
12977 : : {
12978 : 1408967 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12979 : 1408967 : stmt_vec_info slp_stmt_info;
12980 : 1408967 : unsigned int i;
12981 : 3171305 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
12982 : : {
12983 : 1762338 : if (slp_stmt_info
12984 : 1742948 : && (STMT_VINFO_LIVE_P (slp_stmt_info)
12985 : 1554889 : || (loop_vinfo
12986 : 1282268 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
12987 : 228537 : && STMT_VINFO_DEF_TYPE (slp_stmt_info)
12988 : : == vect_induction_def))
12989 : 1950397 : && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
12990 : : slp_node_instance, i,
12991 : : vec_stmt_p, cost_vec))
12992 : : return false;
12993 : : }
12994 : :
12995 : : return true;
12996 : : }
12997 : :
12998 : : /* Make sure the statement is vectorizable. */
12999 : :
13000 : : opt_result
13001 : 2313519 : vect_analyze_stmt (vec_info *vinfo,
13002 : : slp_tree node, slp_instance node_instance,
13003 : : stmt_vector_for_cost *cost_vec)
13004 : : {
13005 : 2313519 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
13006 : 2313519 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13007 : 2313519 : enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
13008 : 2313519 : bool ok;
13009 : :
13010 : 2313519 : if (dump_enabled_p ())
13011 : 95984 : dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
13012 : : stmt_info->stmt);
13013 : :
13014 : 4399215 : if (gimple_has_volatile_ops (stmt_info->stmt))
13015 : : {
13016 : : /* ??? This shouldn't really happen, volatile stmts should
13017 : : not end up in the SLP graph. */
13018 : 0 : return opt_result::failure_at (stmt_info->stmt,
13019 : : "not vectorized:"
13020 : : " stmt has volatile operands: %G\n",
13021 : : stmt_info->stmt);
13022 : : }
13023 : :
13024 : : /* Skip stmts that do not need to be vectorized. */
13025 : 2313519 : if (!STMT_VINFO_RELEVANT_P (stmt_info)
13026 : 0 : && !STMT_VINFO_LIVE_P (stmt_info))
13027 : : {
13028 : 0 : if (dump_enabled_p ())
13029 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
13030 : :
13031 : : /* ??? This shouldn't really happen, irrelevant stmts should
13032 : : not end up in the SLP graph. */
13033 : 0 : return opt_result::failure_at (stmt_info->stmt,
13034 : : "not vectorized:"
13035 : : " irrelevant stmt as SLP node %p "
13036 : : "representative.\n",
13037 : : (void *)node);
13038 : : }
13039 : :
13040 : 2313519 : switch (STMT_VINFO_DEF_TYPE (stmt_info))
13041 : : {
13042 : : case vect_internal_def:
13043 : : case vect_condition_def:
13044 : : break;
13045 : :
13046 : 56706 : case vect_reduction_def:
13047 : 56706 : case vect_nested_cycle:
13048 : 56706 : gcc_assert (!bb_vinfo
13049 : : && (relevance == vect_used_in_outer
13050 : : || relevance == vect_used_in_outer_by_reduction
13051 : : || relevance == vect_used_by_reduction
13052 : : || relevance == vect_unused_in_scope
13053 : : || relevance == vect_used_only_live));
13054 : : break;
13055 : :
13056 : 266 : case vect_double_reduction_def:
13057 : 266 : gcc_assert (!bb_vinfo && node);
13058 : : break;
13059 : :
13060 : 122139 : case vect_induction_def:
13061 : 122139 : case vect_first_order_recurrence:
13062 : 122139 : gcc_assert (!bb_vinfo);
13063 : : break;
13064 : :
13065 : 0 : case vect_constant_def:
13066 : 0 : case vect_external_def:
13067 : 0 : case vect_unknown_def_type:
13068 : 0 : default:
13069 : 0 : gcc_unreachable ();
13070 : : }
13071 : :
13072 : 2313519 : tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
13073 : 2313519 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
13074 : :
13075 : 2313519 : if (STMT_VINFO_RELEVANT_P (stmt_info))
13076 : : {
13077 : 2313519 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
13078 : 2313519 : gcc_assert (SLP_TREE_VECTYPE (node)
13079 : : || gimple_code (stmt_info->stmt) == GIMPLE_COND
13080 : : || (call && gimple_call_lhs (call) == NULL_TREE));
13081 : : }
13082 : :
13083 : 2313519 : ok = true;
13084 : 2313519 : if (bb_vinfo
13085 : 1149119 : || (STMT_VINFO_RELEVANT_P (stmt_info)
13086 : 0 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
13087 : : /* Prefer vectorizable_call over vectorizable_simd_clone_call so
13088 : : -mveclibabi= takes preference over library functions with
13089 : : the simd attribute. */
13090 : 2313519 : ok = (vectorizable_call (vinfo, stmt_info, NULL, node, cost_vec)
13091 : 2307969 : || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, node,
13092 : : cost_vec)
13093 : 2307527 : || vectorizable_conversion (vinfo, stmt_info, NULL, node, cost_vec)
13094 : 2238962 : || vectorizable_operation (vinfo, stmt_info, NULL, node, cost_vec)
13095 : 1829396 : || vectorizable_assignment (vinfo, stmt_info, NULL, node, cost_vec)
13096 : 1771225 : || vectorizable_load (vinfo, stmt_info, NULL, node, cost_vec)
13097 : 1395635 : || vectorizable_store (vinfo, stmt_info, NULL, node, cost_vec)
13098 : 614287 : || vectorizable_shift (vinfo, stmt_info, NULL, node, cost_vec)
13099 : 573304 : || vectorizable_condition (vinfo, stmt_info, NULL, node, cost_vec)
13100 : 553814 : || vectorizable_comparison (vinfo, stmt_info, NULL, node, cost_vec)
13101 : 432626 : || (bb_vinfo
13102 : 126249 : && vectorizable_phi (bb_vinfo, stmt_info, node, cost_vec))
13103 : 2691159 : || (is_a <loop_vec_info> (vinfo)
13104 : 306377 : && (vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
13105 : : stmt_info, node, cost_vec)
13106 : 305915 : || vectorizable_reduction (as_a <loop_vec_info> (vinfo),
13107 : : stmt_info,
13108 : : node, node_instance, cost_vec)
13109 : 251421 : || vectorizable_induction (as_a <loop_vec_info> (vinfo),
13110 : : stmt_info, node, cost_vec)
13111 : 156864 : || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
13112 : : stmt_info, node)
13113 : 156160 : || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13114 : : stmt_info, node, cost_vec)
13115 : 155904 : || vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
13116 : : stmt_info, NULL, node,
13117 : : cost_vec))));
13118 : :
13119 : 2313519 : STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
13120 : :
13121 : 2086352 : if (!ok)
13122 : 227167 : return opt_result::failure_at (stmt_info->stmt,
13123 : : "not vectorized:"
13124 : : " relevant stmt not supported: %G",
13125 : : stmt_info->stmt);
13126 : :
13127 : : /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
13128 : : need extra handling, except for vectorizable reductions. */
13129 : 2086352 : if (!bb_vinfo
13130 : 993215 : && SLP_TREE_TYPE (node) != reduc_vec_info_type
13131 : 985739 : && (SLP_TREE_TYPE (node) != lc_phi_info_type
13132 : 704 : || SLP_TREE_DEF_TYPE (node) == vect_internal_def)
13133 : 985739 : && (!node->ldst_lanes || SLP_TREE_PERMUTE_P (node))
13134 : 3072091 : && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
13135 : : node, node_instance,
13136 : : false, cost_vec))
13137 : 0 : return opt_result::failure_at (stmt_info->stmt,
13138 : : "not vectorized:"
13139 : : " live stmt not supported: %G",
13140 : : stmt_info->stmt);
13141 : :
13142 : 2086352 : return opt_result::success ();
13143 : : }
13144 : :
13145 : :
13146 : : /* Function vect_transform_stmt.
13147 : :
13148 : : Create a vectorized stmt to replace STMT_INFO, and insert it at GSI. */
13149 : :
13150 : : bool
13151 : 968728 : vect_transform_stmt (vec_info *vinfo,
13152 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
13153 : : slp_tree slp_node, slp_instance slp_node_instance)
13154 : : {
13155 : 968728 : bool is_store = false;
13156 : 968728 : bool done;
13157 : :
13158 : 968728 : gcc_assert (slp_node);
13159 : :
13160 : 968728 : if (stmt_info)
13161 : 967897 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
13162 : :
13163 : 968728 : switch (SLP_TREE_TYPE (slp_node))
13164 : : {
13165 : 22876 : case type_demotion_vec_info_type:
13166 : 22876 : case type_promotion_vec_info_type:
13167 : 22876 : case type_conversion_vec_info_type:
13168 : 22876 : done = vectorizable_conversion (vinfo, stmt_info, gsi, slp_node, NULL);
13169 : 22876 : gcc_assert (done);
13170 : : break;
13171 : :
13172 : 17984 : case induc_vec_info_type:
13173 : 17984 : done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
13174 : : stmt_info, slp_node, NULL);
13175 : 17984 : gcc_assert (done);
13176 : : break;
13177 : :
13178 : 7577 : case shift_vec_info_type:
13179 : 7577 : done = vectorizable_shift (vinfo, stmt_info, gsi, slp_node, NULL);
13180 : 7577 : gcc_assert (done);
13181 : : break;
13182 : :
13183 : 116349 : case op_vec_info_type:
13184 : 116349 : done = vectorizable_operation (vinfo, stmt_info, gsi, slp_node, NULL);
13185 : 116349 : gcc_assert (done);
13186 : : break;
13187 : :
13188 : 14688 : case assignment_vec_info_type:
13189 : 14688 : done = vectorizable_assignment (vinfo, stmt_info, gsi, slp_node, NULL);
13190 : 14688 : gcc_assert (done);
13191 : : break;
13192 : :
13193 : 162728 : case load_vec_info_type:
13194 : 162728 : done = vectorizable_load (vinfo, stmt_info, gsi, slp_node, NULL);
13195 : 162728 : gcc_assert (done);
13196 : : break;
13197 : :
13198 : 545500 : case store_vec_info_type:
13199 : 545500 : done = vectorizable_store (vinfo, stmt_info, gsi, slp_node, NULL);
13200 : 545500 : gcc_assert (done);
13201 : : is_store = true;
13202 : : break;
13203 : :
13204 : 8464 : case condition_vec_info_type:
13205 : 8464 : done = vectorizable_condition (vinfo, stmt_info, gsi, slp_node, NULL);
13206 : 8464 : gcc_assert (done);
13207 : : break;
13208 : :
13209 : 12327 : case comparison_vec_info_type:
13210 : 12327 : done = vectorizable_comparison (vinfo, stmt_info, gsi, slp_node, NULL);
13211 : 12327 : gcc_assert (done);
13212 : : break;
13213 : :
13214 : 4199 : case call_vec_info_type:
13215 : 4199 : done = vectorizable_call (vinfo, stmt_info, gsi, slp_node, NULL);
13216 : 4199 : break;
13217 : :
13218 : 348 : case call_simd_clone_vec_info_type:
13219 : 348 : done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi,
13220 : : slp_node, NULL);
13221 : 348 : break;
13222 : :
13223 : 2547 : case reduc_vec_info_type:
13224 : 2547 : done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
13225 : : gsi, slp_node);
13226 : 2547 : gcc_assert (done);
13227 : : break;
13228 : :
13229 : 23342 : case cycle_phi_info_type:
13230 : 23342 : done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
13231 : : slp_node, slp_node_instance);
13232 : 23342 : gcc_assert (done);
13233 : : break;
13234 : :
13235 : 447 : case lc_phi_info_type:
13236 : 447 : done = vect_transform_lc_phi (as_a <loop_vec_info> (vinfo),
13237 : : stmt_info, slp_node);
13238 : 447 : gcc_assert (done);
13239 : : break;
13240 : :
13241 : 40 : case recurr_info_type:
13242 : 40 : done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13243 : : stmt_info, slp_node, NULL);
13244 : 40 : gcc_assert (done);
13245 : : break;
13246 : :
13247 : 13409 : case phi_info_type:
13248 : 13409 : done = vectorizable_phi (as_a <bb_vec_info> (vinfo),
13249 : : stmt_info, slp_node, NULL);
13250 : 13409 : gcc_assert (done);
13251 : : break;
13252 : :
13253 : 0 : case loop_exit_ctrl_vec_info_type:
13254 : 0 : done = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
13255 : : stmt_info, gsi, slp_node, NULL);
13256 : 0 : gcc_assert (done);
13257 : : break;
13258 : :
13259 : 15903 : case permute_info_type:
13260 : 15903 : done = vectorizable_slp_permutation (vinfo, gsi, slp_node, NULL);
13261 : 15903 : gcc_assert (done);
13262 : : break;
13263 : :
13264 : 0 : default:
13265 : 0 : if (!STMT_VINFO_LIVE_P (stmt_info))
13266 : : {
13267 : 0 : if (dump_enabled_p ())
13268 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13269 : : "stmt not supported.\n");
13270 : 0 : gcc_unreachable ();
13271 : : }
13272 : 968728 : done = true;
13273 : : }
13274 : :
13275 : 968728 : if (SLP_TREE_TYPE (slp_node) != store_vec_info_type
13276 : 423228 : && (!slp_node->ldst_lanes || SLP_TREE_PERMUTE_P (slp_node)))
13277 : : {
13278 : : /* Handle stmts whose DEF is used outside the loop-nest that is
13279 : : being vectorized. */
13280 : 423228 : done = can_vectorize_live_stmts (vinfo, slp_node,
13281 : : slp_node_instance, true, NULL);
13282 : 423228 : gcc_assert (done);
13283 : : }
13284 : :
13285 : 968728 : return is_store;
13286 : : }
13287 : :
13288 : :
13289 : : /* Remove a group of stores (for SLP or interleaving), free their
13290 : : stmt_vec_info. */
13291 : :
13292 : : void
13293 : 0 : vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
13294 : : {
13295 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
13296 : :
13297 : 0 : while (next_stmt_info)
13298 : : {
13299 : 0 : stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
13300 : 0 : next_stmt_info = vect_orig_stmt (next_stmt_info);
13301 : : /* Free the attached stmt_vec_info and remove the stmt. */
13302 : 0 : vinfo->remove_stmt (next_stmt_info);
13303 : 0 : next_stmt_info = tmp;
13304 : : }
13305 : 0 : }
13306 : :
13307 : : /* If NUNITS is nonzero, return a vector type that contains NUNITS
13308 : : elements of type SCALAR_TYPE, or null if the target doesn't support
13309 : : such a type.
13310 : :
13311 : : If NUNITS is zero, return a vector type that contains elements of
13312 : : type SCALAR_TYPE, choosing whichever vector size the target prefers.
13313 : :
13314 : : If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
13315 : : for this vectorization region and want to "autodetect" the best choice.
13316 : : Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
13317 : : and we want the new type to be interoperable with it. PREVAILING_MODE
13318 : : in this case can be a scalar integer mode or a vector mode; when it
13319 : : is a vector mode, the function acts like a tree-level version of
13320 : : related_vector_mode. */
13321 : :
13322 : : tree
13323 : 30754577 : get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
13324 : : tree scalar_type, poly_uint64 nunits)
13325 : : {
13326 : 30754577 : tree orig_scalar_type = scalar_type;
13327 : 30754577 : scalar_mode inner_mode;
13328 : 30754577 : machine_mode simd_mode;
13329 : 30754577 : tree vectype;
13330 : :
13331 : 30754577 : if ((!INTEGRAL_TYPE_P (scalar_type)
13332 : 10436256 : && !POINTER_TYPE_P (scalar_type)
13333 : 1645901 : && !SCALAR_FLOAT_TYPE_P (scalar_type))
13334 : 40698106 : || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
13335 : 1153256 : && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
13336 : 495942 : return NULL_TREE;
13337 : :
13338 : 30258635 : unsigned int nbytes = GET_MODE_SIZE (inner_mode);
13339 : :
13340 : : /* Interoperability between modes requires one to be a constant multiple
13341 : : of the other, so that the number of vectors required for each operation
13342 : : is a compile-time constant. */
13343 : 30258635 : if (prevailing_mode != VOIDmode
13344 : 29124561 : && !constant_multiple_p (nunits * nbytes,
13345 : 29124561 : GET_MODE_SIZE (prevailing_mode))
13346 : 31772738 : && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
13347 : 1514103 : nunits * nbytes))
13348 : : return NULL_TREE;
13349 : :
13350 : : /* For vector types of elements whose mode precision doesn't
13351 : : match their types precision we use a element type of mode
13352 : : precision. The vectorization routines will have to make sure
13353 : : they support the proper result truncation/extension.
13354 : : We also make sure to build vector types with INTEGER_TYPE
13355 : : component type only. */
13356 : 30258635 : if (INTEGRAL_TYPE_P (scalar_type)
13357 : 50576874 : && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
13358 : 18751493 : || TREE_CODE (scalar_type) != INTEGER_TYPE))
13359 : 1813792 : scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
13360 : 1813792 : TYPE_UNSIGNED (scalar_type));
13361 : :
13362 : : /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
13363 : : When the component mode passes the above test simply use a type
13364 : : corresponding to that mode. The theory is that any use that
13365 : : would cause problems with this will disable vectorization anyway. */
13366 : 28444843 : else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
13367 : : && !INTEGRAL_TYPE_P (scalar_type))
13368 : 8790355 : scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
13369 : :
13370 : : /* We can't build a vector type of elements with alignment bigger than
13371 : : their size. */
13372 : 19654488 : else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
13373 : 342406 : scalar_type = lang_hooks.types.type_for_mode (inner_mode,
13374 : 171203 : TYPE_UNSIGNED (scalar_type));
13375 : :
13376 : : /* If we felt back to using the mode fail if there was
13377 : : no scalar type for it. */
13378 : 30258635 : if (scalar_type == NULL_TREE)
13379 : : return NULL_TREE;
13380 : :
13381 : : /* If no prevailing mode was supplied, use the mode the target prefers.
13382 : : Otherwise lookup a vector mode based on the prevailing mode. */
13383 : 30258635 : if (prevailing_mode == VOIDmode)
13384 : : {
13385 : 1134074 : gcc_assert (known_eq (nunits, 0U));
13386 : 1134074 : simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
13387 : 1134074 : if (SCALAR_INT_MODE_P (simd_mode))
13388 : : {
13389 : : /* Traditional behavior is not to take the integer mode
13390 : : literally, but simply to use it as a way of determining
13391 : : the vector size. It is up to mode_for_vector to decide
13392 : : what the TYPE_MODE should be.
13393 : :
13394 : : Note that nunits == 1 is allowed in order to support single
13395 : : element vector types. */
13396 : 55820 : if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
13397 : 545 : || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13398 : 27365 : return NULL_TREE;
13399 : : }
13400 : : }
13401 : 29124561 : else if (SCALAR_INT_MODE_P (prevailing_mode)
13402 : 29124561 : || !related_vector_mode (prevailing_mode,
13403 : 27129448 : inner_mode, nunits).exists (&simd_mode))
13404 : : {
13405 : : /* Fall back to using mode_for_vector, mostly in the hope of being
13406 : : able to use an integer mode. */
13407 : 1995113 : if (known_eq (nunits, 0U)
13408 : 4642767 : && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
13409 : : return NULL_TREE;
13410 : :
13411 : 136528 : if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13412 : 126503 : return NULL_TREE;
13413 : : }
13414 : :
13415 : 28246182 : vectype = build_vector_type_for_mode (scalar_type, simd_mode);
13416 : :
13417 : : /* In cases where the mode was chosen by mode_for_vector, check that
13418 : : the target actually supports the chosen mode, or that it at least
13419 : : allows the vector mode to be replaced by a like-sized integer. */
13420 : 56492364 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
13421 : 28256465 : && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
13422 : : return NULL_TREE;
13423 : :
13424 : : /* Re-attach the address-space qualifier if we canonicalized the scalar
13425 : : type. */
13426 : 28238102 : if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
13427 : 5 : return build_qualified_type
13428 : 5 : (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
13429 : :
13430 : : return vectype;
13431 : : }
13432 : :
13433 : : /* Function get_vectype_for_scalar_type.
13434 : :
13435 : : Returns the vector type corresponding to SCALAR_TYPE as supported
13436 : : by the target. If GROUP_SIZE is nonzero and we're performing BB
13437 : : vectorization, make sure that the number of elements in the vector
13438 : : is no bigger than GROUP_SIZE. */
13439 : :
13440 : : tree
13441 : 26267497 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
13442 : : unsigned int group_size)
13443 : : {
13444 : : /* For BB vectorization, we should always have a group size once we've
13445 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
13446 : : are tentative requests during things like early data reference
13447 : : analysis and pattern recognition. */
13448 : 26267497 : if (is_a <bb_vec_info> (vinfo))
13449 : 23781572 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
13450 : : else
13451 : : group_size = 0;
13452 : :
13453 : 26267497 : tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13454 : : scalar_type);
13455 : 26267497 : if (vectype && vinfo->vector_mode == VOIDmode)
13456 : 1059509 : vinfo->vector_mode = TYPE_MODE (vectype);
13457 : :
13458 : : /* Register the natural choice of vector type, before the group size
13459 : : has been applied. */
13460 : 0 : if (vectype)
13461 : 23922593 : vinfo->used_vector_modes.add (TYPE_MODE (vectype));
13462 : :
13463 : : /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
13464 : : try again with an explicit number of elements. */
13465 : 23922593 : if (vectype
13466 : 23922593 : && group_size
13467 : 26267497 : && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
13468 : : {
13469 : : /* Start with the biggest number of units that fits within
13470 : : GROUP_SIZE and halve it until we find a valid vector type.
13471 : : Usually either the first attempt will succeed or all will
13472 : : fail (in the latter case because GROUP_SIZE is too small
13473 : : for the target), but it's possible that a target could have
13474 : : a hole between supported vector types.
13475 : :
13476 : : If GROUP_SIZE is not a power of 2, this has the effect of
13477 : : trying the largest power of 2 that fits within the group,
13478 : : even though the group is not a multiple of that vector size.
13479 : : The BB vectorizer will then try to carve up the group into
13480 : : smaller pieces. */
13481 : 3052142 : unsigned int nunits = 1 << floor_log2 (group_size);
13482 : 3052142 : do
13483 : : {
13484 : 3052142 : vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13485 : 3052142 : scalar_type, nunits);
13486 : 3052142 : nunits /= 2;
13487 : : }
13488 : 3052142 : while (nunits > 1 && !vectype);
13489 : : }
13490 : :
13491 : 26267497 : return vectype;
13492 : : }
13493 : :
13494 : : /* Return the vector type corresponding to SCALAR_TYPE as supported
13495 : : by the target. NODE, if nonnull, is the SLP tree node that will
13496 : : use the returned vector type. */
13497 : :
13498 : : tree
13499 : 159875 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
13500 : : {
13501 : 159875 : unsigned int group_size = 0;
13502 : 159875 : if (node)
13503 : 159875 : group_size = SLP_TREE_LANES (node);
13504 : 159875 : return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13505 : : }
13506 : :
13507 : : /* Function get_mask_type_for_scalar_type.
13508 : :
13509 : : Returns the mask type corresponding to a result of comparison
13510 : : of vectors of specified SCALAR_TYPE as supported by target.
13511 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
13512 : : make sure that the number of elements in the vector is no bigger
13513 : : than GROUP_SIZE. */
13514 : :
13515 : : tree
13516 : 989826 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13517 : : unsigned int group_size)
13518 : : {
13519 : 989826 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13520 : :
13521 : 989826 : if (!vectype)
13522 : : return NULL;
13523 : :
13524 : 969590 : return truth_type_for (vectype);
13525 : : }
13526 : :
13527 : : /* Function get_mask_type_for_scalar_type.
13528 : :
13529 : : Returns the mask type corresponding to a result of comparison
13530 : : of vectors of specified SCALAR_TYPE as supported by target.
13531 : : NODE, if nonnull, is the SLP tree node that will use the returned
13532 : : vector type. */
13533 : :
13534 : : tree
13535 : 17 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13536 : : slp_tree node)
13537 : : {
13538 : 17 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
13539 : :
13540 : 17 : if (!vectype)
13541 : : return NULL;
13542 : :
13543 : 17 : return truth_type_for (vectype);
13544 : : }
13545 : :
13546 : : /* Function get_same_sized_vectype
13547 : :
13548 : : Returns a vector type corresponding to SCALAR_TYPE of size
13549 : : VECTOR_TYPE if supported by the target. */
13550 : :
13551 : : tree
13552 : 130492 : get_same_sized_vectype (tree scalar_type, tree vector_type)
13553 : : {
13554 : 130492 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
13555 : 0 : return truth_type_for (vector_type);
13556 : :
13557 : 130492 : poly_uint64 nunits;
13558 : 260984 : if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
13559 : 260984 : GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
13560 : : return NULL_TREE;
13561 : :
13562 : 130492 : return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
13563 : 130492 : scalar_type, nunits);
13564 : : }
13565 : :
13566 : : /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
13567 : : would not change the chosen vector modes. */
13568 : :
13569 : : bool
13570 : 1436297 : vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
13571 : : {
13572 : 1436297 : for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
13573 : 3384425 : i != vinfo->used_vector_modes.end (); ++i)
13574 : 1741366 : if (!VECTOR_MODE_P (*i)
13575 : 5224098 : || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
13576 : 767302 : return false;
13577 : 668995 : return true;
13578 : : }
13579 : :
13580 : : /* Return true if replacing VECTOR_MODE with ALT_VECTOR_MODE would not
13581 : : change the chosen vector modes for analysis of a loop. */
13582 : :
13583 : : bool
13584 : 296840 : vect_chooses_same_modes_p (machine_mode vector_mode,
13585 : : machine_mode alt_vector_mode)
13586 : : {
13587 : 50187 : return (VECTOR_MODE_P (vector_mode)
13588 : 296840 : && VECTOR_MODE_P (alt_vector_mode)
13589 : 593680 : && (related_vector_mode (vector_mode,
13590 : : GET_MODE_INNER (alt_vector_mode))
13591 : 296840 : == alt_vector_mode)
13592 : 322256 : && (related_vector_mode (alt_vector_mode,
13593 : : GET_MODE_INNER (vector_mode))
13594 : 12708 : == vector_mode));
13595 : : }
13596 : :
13597 : : /* Function vect_is_simple_use.
13598 : :
13599 : : Input:
13600 : : VINFO - the vect info of the loop or basic block that is being vectorized.
13601 : : OPERAND - operand in the loop or bb.
13602 : : Output:
13603 : : DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
13604 : : case OPERAND is an SSA_NAME that is defined in the vectorizable region
13605 : : DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
13606 : : the definition could be anywhere in the function
13607 : : DT - the type of definition
13608 : :
13609 : : Returns whether a stmt with OPERAND can be vectorized.
13610 : : For loops, supportable operands are constants, loop invariants, and operands
13611 : : that are defined by the current iteration of the loop. Unsupportable
13612 : : operands are those that are defined by a previous iteration of the loop (as
13613 : : is the case in reduction/induction computations).
13614 : : For basic blocks, supportable operands are constants and bb invariants.
13615 : : For now, operands defined outside the basic block are not supported. */
13616 : :
13617 : : bool
13618 : 40490108 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
13619 : : stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
13620 : : {
13621 : 40490108 : if (def_stmt_info_out)
13622 : 38718209 : *def_stmt_info_out = NULL;
13623 : 40490108 : if (def_stmt_out)
13624 : 9082834 : *def_stmt_out = NULL;
13625 : 40490108 : *dt = vect_unknown_def_type;
13626 : :
13627 : 40490108 : if (dump_enabled_p ())
13628 : : {
13629 : 733059 : dump_printf_loc (MSG_NOTE, vect_location,
13630 : : "vect_is_simple_use: operand ");
13631 : 733059 : if (TREE_CODE (operand) == SSA_NAME
13632 : 733059 : && !SSA_NAME_IS_DEFAULT_DEF (operand))
13633 : 666147 : dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
13634 : : else
13635 : 66912 : dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
13636 : : }
13637 : :
13638 : 40490108 : if (CONSTANT_CLASS_P (operand))
13639 : 3214690 : *dt = vect_constant_def;
13640 : 37275418 : else if (is_gimple_min_invariant (operand))
13641 : 345092 : *dt = vect_external_def;
13642 : 36930326 : else if (TREE_CODE (operand) != SSA_NAME)
13643 : 1109 : *dt = vect_unknown_def_type;
13644 : 36929217 : else if (SSA_NAME_IS_DEFAULT_DEF (operand))
13645 : 508141 : *dt = vect_external_def;
13646 : : else
13647 : : {
13648 : 36421076 : gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
13649 : 36421076 : stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
13650 : 36421076 : if (!stmt_vinfo)
13651 : 757257 : *dt = vect_external_def;
13652 : : else
13653 : : {
13654 : 35663819 : stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
13655 : 35663819 : def_stmt = stmt_vinfo->stmt;
13656 : 35663819 : *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
13657 : 35663819 : if (def_stmt_info_out)
13658 : 33898287 : *def_stmt_info_out = stmt_vinfo;
13659 : : }
13660 : 36421076 : if (def_stmt_out)
13661 : 8870630 : *def_stmt_out = def_stmt;
13662 : : }
13663 : :
13664 : 40490108 : if (dump_enabled_p ())
13665 : : {
13666 : 733059 : dump_printf (MSG_NOTE, ", type of def: ");
13667 : 733059 : switch (*dt)
13668 : : {
13669 : 0 : case vect_uninitialized_def:
13670 : 0 : dump_printf (MSG_NOTE, "uninitialized\n");
13671 : 0 : break;
13672 : 56508 : case vect_constant_def:
13673 : 56508 : dump_printf (MSG_NOTE, "constant\n");
13674 : 56508 : break;
13675 : 24539 : case vect_external_def:
13676 : 24539 : dump_printf (MSG_NOTE, "external\n");
13677 : 24539 : break;
13678 : 518535 : case vect_internal_def:
13679 : 518535 : dump_printf (MSG_NOTE, "internal\n");
13680 : 518535 : break;
13681 : 105736 : case vect_induction_def:
13682 : 105736 : dump_printf (MSG_NOTE, "induction\n");
13683 : 105736 : break;
13684 : 24828 : case vect_reduction_def:
13685 : 24828 : dump_printf (MSG_NOTE, "reduction\n");
13686 : 24828 : break;
13687 : 464 : case vect_double_reduction_def:
13688 : 464 : dump_printf (MSG_NOTE, "double reduction\n");
13689 : 464 : break;
13690 : 1854 : case vect_nested_cycle:
13691 : 1854 : dump_printf (MSG_NOTE, "nested cycle\n");
13692 : 1854 : break;
13693 : 264 : case vect_first_order_recurrence:
13694 : 264 : dump_printf (MSG_NOTE, "first order recurrence\n");
13695 : 264 : break;
13696 : 0 : case vect_condition_def:
13697 : 0 : dump_printf (MSG_NOTE, "control flow\n");
13698 : 0 : break;
13699 : 331 : case vect_unknown_def_type:
13700 : 331 : dump_printf (MSG_NOTE, "unknown\n");
13701 : 331 : break;
13702 : : }
13703 : : }
13704 : :
13705 : 40490108 : if (*dt == vect_unknown_def_type)
13706 : : {
13707 : 15216 : if (dump_enabled_p ())
13708 : 331 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13709 : : "Unsupported pattern.\n");
13710 : 15216 : return false;
13711 : : }
13712 : :
13713 : : return true;
13714 : : }
13715 : :
13716 : : /* Function vect_is_simple_use.
13717 : :
13718 : : Same as vect_is_simple_use but determines the operand by operand
13719 : : position OPERAND from either STMT or SLP_NODE, filling in *OP
13720 : : and *SLP_DEF (when SLP_NODE is not NULL). */
13721 : :
13722 : : bool
13723 : 3364053 : vect_is_simple_use (vec_info *vinfo, slp_tree slp_node,
13724 : : unsigned operand, tree *op, slp_tree *slp_def,
13725 : : enum vect_def_type *dt,
13726 : : tree *vectype, stmt_vec_info *def_stmt_info_out)
13727 : : {
13728 : 3364053 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
13729 : 3364053 : *slp_def = child;
13730 : 3364053 : *vectype = SLP_TREE_VECTYPE (child);
13731 : 3364053 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
13732 : : {
13733 : : /* ??? VEC_PERM nodes might be intermediate and their lane value
13734 : : have no representative (nor do we build a VEC_PERM stmt for
13735 : : the actual operation). Note for two-operator nodes we set
13736 : : a representative but leave scalar stmts empty as we'd only
13737 : : have one for a subset of lanes. Ideally no caller would
13738 : : require *op for internal defs. */
13739 : 1741927 : if (SLP_TREE_REPRESENTATIVE (child))
13740 : : {
13741 : 1741261 : *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
13742 : 1741261 : return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
13743 : : }
13744 : : else
13745 : : {
13746 : 666 : gcc_assert (SLP_TREE_PERMUTE_P (child));
13747 : 666 : *op = error_mark_node;
13748 : 666 : *dt = vect_internal_def;
13749 : 666 : if (def_stmt_info_out)
13750 : 0 : *def_stmt_info_out = NULL;
13751 : 666 : return true;
13752 : : }
13753 : : }
13754 : : else
13755 : : {
13756 : 1622126 : if (def_stmt_info_out)
13757 : 45972 : *def_stmt_info_out = NULL;
13758 : 1622126 : *op = SLP_TREE_SCALAR_OPS (child)[0];
13759 : 1622126 : *dt = SLP_TREE_DEF_TYPE (child);
13760 : 1622126 : return true;
13761 : : }
13762 : : }
13763 : :
13764 : : /* If OP is not NULL and is external or constant update its vector
13765 : : type with VECTYPE. Returns true if successful or false if not,
13766 : : for example when conflicting vector types are present. */
13767 : :
13768 : : bool
13769 : 2918268 : vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
13770 : : {
13771 : 2918268 : if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
13772 : : return true;
13773 : 1063315 : if (SLP_TREE_VECTYPE (op))
13774 : 66261 : return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
13775 : : /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
13776 : : should be handled by patters. Allow vect_constant_def for now
13777 : : as well as the trivial single-lane uniform vect_external_def case
13778 : : both of which we code-generate reasonably. */
13779 : 997054 : if (VECTOR_BOOLEAN_TYPE_P (vectype)
13780 : 1028 : && SLP_TREE_DEF_TYPE (op) == vect_external_def
13781 : 997711 : && SLP_TREE_LANES (op) > 1)
13782 : : return false;
13783 : 996871 : SLP_TREE_VECTYPE (op) = vectype;
13784 : 996871 : return true;
13785 : : }
13786 : :
13787 : : /* Function supportable_widening_operation
13788 : :
13789 : : Check whether an operation represented by the code CODE is a
13790 : : widening operation that is supported by the target platform in
13791 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
13792 : : producing a result of type VECTYPE_OUT).
13793 : :
13794 : : Widening operations we currently support are NOP (CONVERT), FLOAT,
13795 : : FIX_TRUNC and WIDEN_MULT. This function checks if these operations
13796 : : are supported by the target platform either directly (via vector
13797 : : tree-codes), or via target builtins.
13798 : :
13799 : : Output:
13800 : : - CODE1 and CODE2 are codes of vector operations to be used when
13801 : : vectorizing the operation, if available.
13802 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
13803 : : case of multi-step conversion (like char->short->int - in that case
13804 : : MULTI_STEP_CVT will be 1).
13805 : : - INTERM_TYPES contains the intermediate type required to perform the
13806 : : widening operation (short in the above example). */
13807 : :
13808 : : bool
13809 : 313745 : supportable_widening_operation (vec_info *vinfo,
13810 : : code_helper code,
13811 : : stmt_vec_info stmt_info,
13812 : : tree vectype_out, tree vectype_in,
13813 : : code_helper *code1,
13814 : : code_helper *code2,
13815 : : int *multi_step_cvt,
13816 : : vec<tree> *interm_types)
13817 : : {
13818 : 313745 : loop_vec_info loop_info = dyn_cast <loop_vec_info> (vinfo);
13819 : 313745 : class loop *vect_loop = NULL;
13820 : 313745 : machine_mode vec_mode;
13821 : 313745 : enum insn_code icode1, icode2;
13822 : 313745 : optab optab1 = unknown_optab, optab2 = unknown_optab;
13823 : 313745 : tree vectype = vectype_in;
13824 : 313745 : tree wide_vectype = vectype_out;
13825 : 313745 : tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
13826 : 313745 : int i;
13827 : 313745 : tree prev_type, intermediate_type;
13828 : 313745 : machine_mode intermediate_mode, prev_mode;
13829 : 313745 : optab optab3, optab4;
13830 : :
13831 : 313745 : *multi_step_cvt = 0;
13832 : 180213 : if (loop_info)
13833 : 133532 : vect_loop = LOOP_VINFO_LOOP (loop_info);
13834 : :
13835 : 313745 : switch (code.safe_as_tree_code ())
13836 : : {
13837 : : case MAX_TREE_CODES:
13838 : : /* Don't set c1 and c2 if code is not a tree_code. */
13839 : : break;
13840 : :
13841 : 165639 : case WIDEN_MULT_EXPR:
13842 : : /* The result of a vectorized widening operation usually requires
13843 : : two vectors (because the widened results do not fit into one vector).
13844 : : The generated vector results would normally be expected to be
13845 : : generated in the same order as in the original scalar computation,
13846 : : i.e. if 8 results are generated in each vector iteration, they are
13847 : : to be organized as follows:
13848 : : vect1: [res1,res2,res3,res4],
13849 : : vect2: [res5,res6,res7,res8].
13850 : :
13851 : : However, in the special case that the result of the widening
13852 : : operation is used in a reduction computation only, the order doesn't
13853 : : matter (because when vectorizing a reduction we change the order of
13854 : : the computation). Some targets can take advantage of this and
13855 : : generate more efficient code. For example, targets like Altivec,
13856 : : that support widen_mult using a sequence of {mult_even,mult_odd}
13857 : : generate the following vectors:
13858 : : vect1: [res1,res3,res5,res7],
13859 : : vect2: [res2,res4,res6,res8].
13860 : :
13861 : : When vectorizing outer-loops, we execute the inner-loop sequentially
13862 : : (each vectorized inner-loop iteration contributes to VF outer-loop
13863 : : iterations in parallel). We therefore don't allow to change the
13864 : : order of the computation in the inner-loop during outer-loop
13865 : : vectorization. */
13866 : : /* TODO: Another case in which order doesn't *really* matter is when we
13867 : : widen and then contract again, e.g. (short)((int)x * y >> 8).
13868 : : Normally, pack_trunc performs an even/odd permute, whereas the
13869 : : repack from an even/odd expansion would be an interleave, which
13870 : : would be significantly simpler for e.g. AVX2. */
13871 : : /* In any case, in order to avoid duplicating the code below, recurse
13872 : : on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
13873 : : are properly set up for the caller. If we fail, we'll continue with
13874 : : a VEC_WIDEN_MULT_LO/HI_EXPR check. */
13875 : 165639 : if (vect_loop
13876 : 41997 : && !nested_in_vect_loop_p (vect_loop, stmt_info)
13877 : 208093 : && supportable_widening_operation (vinfo, VEC_WIDEN_MULT_EVEN_EXPR,
13878 : : stmt_info, vectype_out,
13879 : : vectype_in, code1,
13880 : : code2, multi_step_cvt,
13881 : : interm_types))
13882 : : {
13883 : : /* Elements in a vector with vect_used_by_reduction property cannot
13884 : : be reordered if the use chain with this property does not have the
13885 : : same operation. One such an example is s += a * b, where elements
13886 : : in a and b cannot be reordered. Here we check if the vector defined
13887 : : by STMT is only directly used in the reduction statement. */
13888 : 29251 : tree lhs = gimple_assign_lhs (vect_orig_stmt (stmt_info)->stmt);
13889 : 27579 : stmt_vec_info use_stmt_info = loop_info->lookup_single_use (lhs);
13890 : 27579 : if (use_stmt_info && STMT_VINFO_REDUC_DEF (use_stmt_info))
13891 : : return true;
13892 : : }
13893 : : c1 = VEC_WIDEN_MULT_LO_EXPR;
13894 : : c2 = VEC_WIDEN_MULT_HI_EXPR;
13895 : : break;
13896 : :
13897 : : case DOT_PROD_EXPR:
13898 : 313562 : c1 = DOT_PROD_EXPR;
13899 : 313562 : c2 = DOT_PROD_EXPR;
13900 : : break;
13901 : :
13902 : 0 : case SAD_EXPR:
13903 : 0 : c1 = SAD_EXPR;
13904 : 0 : c2 = SAD_EXPR;
13905 : 0 : break;
13906 : :
13907 : 41997 : case VEC_WIDEN_MULT_EVEN_EXPR:
13908 : : /* Support the recursion induced just above. */
13909 : 41997 : c1 = VEC_WIDEN_MULT_EVEN_EXPR;
13910 : 41997 : c2 = VEC_WIDEN_MULT_ODD_EXPR;
13911 : 41997 : break;
13912 : :
13913 : 9419 : case WIDEN_LSHIFT_EXPR:
13914 : 9419 : c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
13915 : 9419 : c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
13916 : 9419 : break;
13917 : :
13918 : 34733 : CASE_CONVERT:
13919 : 34733 : c1 = VEC_UNPACK_LO_EXPR;
13920 : 34733 : c2 = VEC_UNPACK_HI_EXPR;
13921 : 34733 : break;
13922 : :
13923 : 7386 : case FLOAT_EXPR:
13924 : 7386 : c1 = VEC_UNPACK_FLOAT_LO_EXPR;
13925 : 7386 : c2 = VEC_UNPACK_FLOAT_HI_EXPR;
13926 : 7386 : break;
13927 : :
13928 : 206 : case FIX_TRUNC_EXPR:
13929 : 206 : c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
13930 : 206 : c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
13931 : 206 : break;
13932 : :
13933 : 0 : default:
13934 : 0 : gcc_unreachable ();
13935 : : }
13936 : :
13937 : 313562 : if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
13938 : : std::swap (c1, c2);
13939 : :
13940 : 313562 : if (code == FIX_TRUNC_EXPR)
13941 : : {
13942 : : /* The signedness is determined from output operand. */
13943 : 206 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
13944 : 206 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
13945 : : }
13946 : 543782 : else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
13947 : 34733 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
13948 : 5926 : && VECTOR_BOOLEAN_TYPE_P (vectype)
13949 : 5926 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
13950 : 259306 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
13951 : : {
13952 : : /* If the input and result modes are the same, a different optab
13953 : : is needed where we pass in the number of units in vectype. */
13954 : : optab1 = vec_unpacks_sbool_lo_optab;
13955 : : optab2 = vec_unpacks_sbool_hi_optab;
13956 : : }
13957 : :
13958 : 313562 : vec_mode = TYPE_MODE (vectype);
13959 : 313562 : if (widening_fn_p (code))
13960 : : {
13961 : : /* If this is an internal fn then we must check whether the target
13962 : : supports either a low-high split or an even-odd split. */
13963 : 54365 : internal_fn ifn = as_internal_fn ((combined_fn) code);
13964 : :
13965 : 54365 : internal_fn lo, hi, even, odd;
13966 : 54365 : lookup_hilo_internal_fn (ifn, &lo, &hi);
13967 : 54365 : if (BYTES_BIG_ENDIAN)
13968 : : std::swap (lo, hi);
13969 : 54365 : *code1 = as_combined_fn (lo);
13970 : 54365 : *code2 = as_combined_fn (hi);
13971 : 54365 : optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
13972 : 54365 : optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
13973 : :
13974 : : /* If we don't support low-high, then check for even-odd. */
13975 : 54365 : if (!optab1
13976 : 54365 : || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
13977 : 0 : || !optab2
13978 : 54365 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
13979 : : {
13980 : 54365 : lookup_evenodd_internal_fn (ifn, &even, &odd);
13981 : 54365 : *code1 = as_combined_fn (even);
13982 : 54365 : *code2 = as_combined_fn (odd);
13983 : 54365 : optab1 = direct_internal_fn_optab (even, {vectype, vectype});
13984 : 54365 : optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
13985 : : }
13986 : : }
13987 : 259197 : else if (code.is_tree_code ())
13988 : : {
13989 : 259197 : if (code == FIX_TRUNC_EXPR)
13990 : : {
13991 : : /* The signedness is determined from output operand. */
13992 : 206 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
13993 : 206 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
13994 : : }
13995 : 258991 : else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
13996 : 34733 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
13997 : 5926 : && VECTOR_BOOLEAN_TYPE_P (vectype)
13998 : 5926 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
13999 : 259306 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
14000 : : {
14001 : : /* If the input and result modes are the same, a different optab
14002 : : is needed where we pass in the number of units in vectype. */
14003 : : optab1 = vec_unpacks_sbool_lo_optab;
14004 : : optab2 = vec_unpacks_sbool_hi_optab;
14005 : : }
14006 : : else
14007 : : {
14008 : 258676 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14009 : 258676 : optab2 = optab_for_tree_code (c2, vectype, optab_default);
14010 : : }
14011 : 259197 : *code1 = c1;
14012 : 259197 : *code2 = c2;
14013 : : }
14014 : :
14015 : 313562 : if (!optab1 || !optab2)
14016 : : return false;
14017 : :
14018 : 313562 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
14019 : 313562 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
14020 : 152078 : return false;
14021 : :
14022 : :
14023 : 161484 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14024 : 161484 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14025 : : {
14026 : 151744 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14027 : : return true;
14028 : : /* For scalar masks we may have different boolean
14029 : : vector types having the same QImode. Thus we
14030 : : add additional check for elements number. */
14031 : 2990 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
14032 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14033 : : return true;
14034 : : }
14035 : :
14036 : : /* Check if it's a multi-step conversion that can be done using intermediate
14037 : : types. */
14038 : :
14039 : 9861 : prev_type = vectype;
14040 : 9861 : prev_mode = vec_mode;
14041 : :
14042 : 162164 : if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
14043 : : return false;
14044 : :
14045 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14046 : : intermediate steps in promotion sequence. We try
14047 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
14048 : : not. */
14049 : 9809 : interm_types->create (MAX_INTERM_CVT_STEPS);
14050 : 11171 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14051 : : {
14052 : 11171 : intermediate_mode = insn_data[icode1].operand[0].mode;
14053 : 11171 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14054 : 3920 : intermediate_type
14055 : 3920 : = vect_halve_mask_nunits (prev_type, intermediate_mode);
14056 : 7251 : else if (VECTOR_MODE_P (intermediate_mode))
14057 : : {
14058 : 7251 : tree intermediate_element_type
14059 : 7251 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
14060 : 7251 : TYPE_UNSIGNED (prev_type));
14061 : 7251 : intermediate_type
14062 : 7251 : = build_vector_type_for_mode (intermediate_element_type,
14063 : : intermediate_mode);
14064 : 7251 : }
14065 : : else
14066 : 0 : intermediate_type
14067 : 0 : = lang_hooks.types.type_for_mode (intermediate_mode,
14068 : 0 : TYPE_UNSIGNED (prev_type));
14069 : :
14070 : 11171 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14071 : 3920 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14072 : 3920 : && intermediate_mode == TYPE_MODE (wide_vectype)
14073 : 11328 : && SCALAR_INT_MODE_P (intermediate_mode))
14074 : : {
14075 : : /* If the input and result modes are the same, a different optab
14076 : : is needed where we pass in the number of units in vectype. */
14077 : : optab3 = vec_unpacks_sbool_lo_optab;
14078 : : optab4 = vec_unpacks_sbool_hi_optab;
14079 : : }
14080 : : else
14081 : : {
14082 : 11014 : optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
14083 : 11014 : optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
14084 : : }
14085 : :
14086 : 11171 : if (!optab3 || !optab4
14087 : 11171 : || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
14088 : 11155 : || insn_data[icode1].operand[0].mode != intermediate_mode
14089 : 11155 : || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
14090 : 11155 : || insn_data[icode2].operand[0].mode != intermediate_mode
14091 : 11155 : || ((icode1 = optab_handler (optab3, intermediate_mode))
14092 : : == CODE_FOR_nothing)
14093 : 22117 : || ((icode2 = optab_handler (optab4, intermediate_mode))
14094 : : == CODE_FOR_nothing))
14095 : : break;
14096 : :
14097 : 10946 : interm_types->quick_push (intermediate_type);
14098 : 10946 : (*multi_step_cvt)++;
14099 : :
14100 : 10946 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14101 : 10946 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14102 : : {
14103 : 9620 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14104 : : return true;
14105 : 2874 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
14106 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14107 : : return true;
14108 : : }
14109 : :
14110 : 1362 : prev_type = intermediate_type;
14111 : 1362 : prev_mode = intermediate_mode;
14112 : : }
14113 : :
14114 : 225 : interm_types->release ();
14115 : 225 : return false;
14116 : : }
14117 : :
14118 : :
14119 : : /* Function supportable_narrowing_operation
14120 : :
14121 : : Check whether an operation represented by the code CODE is a
14122 : : narrowing operation that is supported by the target platform in
14123 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14124 : : and producing a result of type VECTYPE_OUT).
14125 : :
14126 : : Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
14127 : : and FLOAT. This function checks if these operations are supported by
14128 : : the target platform directly via vector tree-codes.
14129 : :
14130 : : Output:
14131 : : - CODE1 is the code of a vector operation to be used when
14132 : : vectorizing the operation, if available.
14133 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
14134 : : case of multi-step conversion (like int->short->char - in that case
14135 : : MULTI_STEP_CVT will be 1).
14136 : : - INTERM_TYPES contains the intermediate type required to perform the
14137 : : narrowing operation (short in the above example). */
14138 : :
14139 : : bool
14140 : 36230 : supportable_narrowing_operation (code_helper code,
14141 : : tree vectype_out, tree vectype_in,
14142 : : code_helper *code1, int *multi_step_cvt,
14143 : : vec<tree> *interm_types)
14144 : : {
14145 : 36230 : machine_mode vec_mode;
14146 : 36230 : enum insn_code icode1;
14147 : 36230 : optab optab1, interm_optab;
14148 : 36230 : tree vectype = vectype_in;
14149 : 36230 : tree narrow_vectype = vectype_out;
14150 : 36230 : enum tree_code c1;
14151 : 36230 : tree intermediate_type, prev_type;
14152 : 36230 : machine_mode intermediate_mode, prev_mode;
14153 : 36230 : int i;
14154 : 36230 : unsigned HOST_WIDE_INT n_elts;
14155 : 36230 : bool uns;
14156 : :
14157 : 36230 : if (!code.is_tree_code ())
14158 : : return false;
14159 : :
14160 : 36230 : *multi_step_cvt = 0;
14161 : 36230 : switch ((tree_code) code)
14162 : : {
14163 : 35187 : CASE_CONVERT:
14164 : 35187 : c1 = VEC_PACK_TRUNC_EXPR;
14165 : 35187 : if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
14166 : 9463 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14167 : 9463 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
14168 : 4390 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
14169 : 39577 : && n_elts < BITS_PER_UNIT)
14170 : : optab1 = vec_pack_sbool_trunc_optab;
14171 : : else
14172 : 33384 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14173 : : break;
14174 : :
14175 : 825 : case FIX_TRUNC_EXPR:
14176 : 825 : c1 = VEC_PACK_FIX_TRUNC_EXPR;
14177 : : /* The signedness is determined from output operand. */
14178 : 825 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14179 : 825 : break;
14180 : :
14181 : 218 : case FLOAT_EXPR:
14182 : 218 : c1 = VEC_PACK_FLOAT_EXPR;
14183 : 218 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14184 : 218 : break;
14185 : :
14186 : 0 : default:
14187 : 0 : gcc_unreachable ();
14188 : : }
14189 : :
14190 : 36230 : if (!optab1)
14191 : : return false;
14192 : :
14193 : 36230 : vec_mode = TYPE_MODE (vectype);
14194 : 36230 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
14195 : : return false;
14196 : :
14197 : 32305 : *code1 = c1;
14198 : :
14199 : 32305 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14200 : : {
14201 : 19154 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14202 : : return true;
14203 : : /* For scalar masks we may have different boolean
14204 : : vector types having the same QImode. Thus we
14205 : : add additional check for elements number. */
14206 : 4649 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
14207 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14208 : : return true;
14209 : : }
14210 : :
14211 : 13152 : if (code == FLOAT_EXPR)
14212 : : return false;
14213 : :
14214 : : /* Check if it's a multi-step conversion that can be done using intermediate
14215 : : types. */
14216 : 13152 : prev_mode = vec_mode;
14217 : 13152 : prev_type = vectype;
14218 : 13152 : if (code == FIX_TRUNC_EXPR)
14219 : 183 : uns = TYPE_UNSIGNED (vectype_out);
14220 : : else
14221 : 12969 : uns = TYPE_UNSIGNED (vectype);
14222 : :
14223 : : /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
14224 : : conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
14225 : : costly than signed. */
14226 : 13152 : if (code == FIX_TRUNC_EXPR && uns)
14227 : : {
14228 : 76 : enum insn_code icode2;
14229 : :
14230 : 76 : intermediate_type
14231 : 76 : = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
14232 : 76 : interm_optab
14233 : 76 : = optab_for_tree_code (c1, intermediate_type, optab_default);
14234 : 76 : if (interm_optab != unknown_optab
14235 : 76 : && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
14236 : 76 : && insn_data[icode1].operand[0].mode
14237 : 76 : == insn_data[icode2].operand[0].mode)
14238 : : {
14239 : : uns = false;
14240 : : optab1 = interm_optab;
14241 : : icode1 = icode2;
14242 : : }
14243 : : }
14244 : :
14245 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14246 : : intermediate steps in promotion sequence. We try
14247 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
14248 : 13152 : interm_types->create (MAX_INTERM_CVT_STEPS);
14249 : 28311 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14250 : : {
14251 : 15159 : intermediate_mode = insn_data[icode1].operand[0].mode;
14252 : 15159 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14253 : 5911 : intermediate_type
14254 : 5911 : = vect_double_mask_nunits (prev_type, intermediate_mode);
14255 : : else
14256 : 9248 : intermediate_type
14257 : 9248 : = lang_hooks.types.type_for_mode (intermediate_mode, uns);
14258 : 15159 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14259 : 5911 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
14260 : 5911 : && SCALAR_INT_MODE_P (prev_mode)
14261 : 2617 : && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
14262 : 17776 : && n_elts < BITS_PER_UNIT)
14263 : : interm_optab = vec_pack_sbool_trunc_optab;
14264 : : else
14265 : 15042 : interm_optab
14266 : 15042 : = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
14267 : : optab_default);
14268 : 117 : if (!interm_optab
14269 : 15159 : || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
14270 : 15159 : || insn_data[icode1].operand[0].mode != intermediate_mode
14271 : 30201 : || ((icode1 = optab_handler (interm_optab, intermediate_mode))
14272 : : == CODE_FOR_nothing))
14273 : : break;
14274 : :
14275 : 14331 : interm_types->quick_push (intermediate_type);
14276 : 14331 : (*multi_step_cvt)++;
14277 : :
14278 : 14331 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14279 : : {
14280 : 12324 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14281 : : return true;
14282 : 4010 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
14283 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14284 : : return true;
14285 : : }
14286 : :
14287 : 2007 : prev_mode = intermediate_mode;
14288 : 2007 : prev_type = intermediate_type;
14289 : 2007 : optab1 = interm_optab;
14290 : : }
14291 : :
14292 : 828 : interm_types->release ();
14293 : 828 : return false;
14294 : : }
14295 : :
14296 : : /* Function supportable_indirect_convert_operation
14297 : :
14298 : : Check whether an operation represented by the code CODE is single or multi
14299 : : operations that are supported by the target platform in
14300 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14301 : : producing a result of type VECTYPE_OUT).
14302 : :
14303 : : Convert operations we currently support directly are FIX_TRUNC and FLOAT.
14304 : : This function checks if these operations are supported
14305 : : by the target platform directly (via vector tree-codes).
14306 : :
14307 : : Output:
14308 : : - converts contains some pairs to perform the convert operation,
14309 : : the pair's first is the intermediate type, and its second is the code of
14310 : : a vector operation to be used when converting the operation from the
14311 : : previous type to the intermediate type. */
14312 : : bool
14313 : 73022 : supportable_indirect_convert_operation (code_helper code,
14314 : : tree vectype_out,
14315 : : tree vectype_in,
14316 : : vec<std::pair<tree, tree_code> > &converts,
14317 : : tree op0, slp_tree slp_op0)
14318 : : {
14319 : 73022 : bool found_mode = false;
14320 : 73022 : scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
14321 : 73022 : scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
14322 : 73022 : tree_code tc1, tc2, code1, code2;
14323 : :
14324 : 73022 : tree cvt_type = NULL_TREE;
14325 : 73022 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
14326 : :
14327 : 73022 : if (supportable_convert_operation ((tree_code) code,
14328 : : vectype_out,
14329 : : vectype_in,
14330 : : &tc1))
14331 : : {
14332 : 17647 : converts.safe_push (std::make_pair (vectype_out, tc1));
14333 : 17647 : return true;
14334 : : }
14335 : :
14336 : : /* For conversions between float and integer types try whether
14337 : : we can use intermediate signed integer types to support the
14338 : : conversion. */
14339 : 110750 : if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
14340 : 55375 : && (code == FLOAT_EXPR
14341 : 3109 : || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
14342 : : {
14343 : 376 : bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
14344 : 188 : bool float_expr_p = code == FLOAT_EXPR;
14345 : 188 : unsigned short target_size;
14346 : 188 : scalar_mode intermediate_mode;
14347 : 188 : if (demotion)
14348 : : {
14349 : 84 : intermediate_mode = lhs_mode;
14350 : 84 : target_size = GET_MODE_SIZE (rhs_mode);
14351 : : }
14352 : : else
14353 : : {
14354 : 104 : target_size = GET_MODE_SIZE (lhs_mode);
14355 : 104 : if (!int_mode_for_size
14356 : 104 : (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
14357 : 122 : return false;
14358 : : }
14359 : 188 : code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
14360 : : code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
14361 : 188 : opt_scalar_mode mode_iter;
14362 : 292 : FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
14363 : : {
14364 : 292 : intermediate_mode = mode_iter.require ();
14365 : :
14366 : 584 : if (GET_MODE_SIZE (intermediate_mode) > target_size)
14367 : : break;
14368 : :
14369 : 264 : scalar_mode cvt_mode;
14370 : 264 : if (!int_mode_for_size
14371 : 264 : (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
14372 : : break;
14373 : :
14374 : 234 : cvt_type = build_nonstandard_integer_type
14375 : 234 : (GET_MODE_BITSIZE (cvt_mode), 0);
14376 : :
14377 : : /* Check if the intermediate type can hold OP0's range.
14378 : : When converting from float to integer this is not necessary
14379 : : because values that do not fit the (smaller) target type are
14380 : : unspecified anyway. */
14381 : 234 : if (demotion && float_expr_p)
14382 : : {
14383 : 8 : wide_int op_min_value, op_max_value;
14384 : : /* For vector form, it looks like op0 doesn't have RANGE_INFO.
14385 : : In the future, if it is supported, changes may need to be made
14386 : : to this part, such as checking the RANGE of each element
14387 : : in the vector. */
14388 : 8 : if (slp_op0)
14389 : : {
14390 : 4 : tree def;
14391 : : /* ??? Merge ranges in case of more than one lane. */
14392 : 4 : if (SLP_TREE_LANES (slp_op0) != 1
14393 : 0 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
14394 : 4 : || !vect_get_range_info (def,
14395 : : &op_min_value, &op_max_value))
14396 : : break;
14397 : : }
14398 : 4 : else if (!op0
14399 : 0 : || TREE_CODE (op0) != SSA_NAME
14400 : 0 : || !SSA_NAME_RANGE_INFO (op0)
14401 : 4 : || !vect_get_range_info (op0, &op_min_value,
14402 : : &op_max_value))
14403 : : break;
14404 : :
14405 : 0 : if (cvt_type == NULL_TREE
14406 : 0 : || (wi::min_precision (op_max_value, SIGNED)
14407 : 0 : > TYPE_PRECISION (cvt_type))
14408 : 0 : || (wi::min_precision (op_min_value, SIGNED)
14409 : 0 : > TYPE_PRECISION (cvt_type)))
14410 : 0 : continue;
14411 : 8 : }
14412 : :
14413 : 226 : cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
14414 : : cvt_type,
14415 : : nelts);
14416 : : /* This should only happened for SLP as long as loop vectorizer
14417 : : only supports same-sized vector. */
14418 : 330 : if (cvt_type == NULL_TREE
14419 : 348 : || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
14420 : 226 : || !supportable_convert_operation ((tree_code) code1,
14421 : : vectype_out,
14422 : : cvt_type, &tc1)
14423 : 396 : || !supportable_convert_operation ((tree_code) code2,
14424 : : cvt_type,
14425 : : vectype_in, &tc2))
14426 : 104 : continue;
14427 : :
14428 : : found_mode = true;
14429 : : break;
14430 : : }
14431 : :
14432 : 188 : if (found_mode)
14433 : : {
14434 : 122 : converts.safe_push (std::make_pair (cvt_type, tc2));
14435 : 122 : if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
14436 : 122 : converts.safe_push (std::make_pair (vectype_out, tc1));
14437 : 122 : return true;
14438 : : }
14439 : : }
14440 : : return false;
14441 : : }
14442 : :
14443 : : /* Generate and return a vector mask of MASK_TYPE such that
14444 : : mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
14445 : : Add the statements to SEQ. */
14446 : :
14447 : : tree
14448 : 0 : vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
14449 : : tree end_index, const char *name)
14450 : : {
14451 : 0 : tree cmp_type = TREE_TYPE (start_index);
14452 : 0 : gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
14453 : : cmp_type, mask_type,
14454 : : OPTIMIZE_FOR_SPEED));
14455 : 0 : gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
14456 : : start_index, end_index,
14457 : : build_zero_cst (mask_type));
14458 : 0 : tree tmp;
14459 : 0 : if (name)
14460 : 0 : tmp = make_temp_ssa_name (mask_type, NULL, name);
14461 : : else
14462 : 0 : tmp = make_ssa_name (mask_type);
14463 : 0 : gimple_call_set_lhs (call, tmp);
14464 : 0 : gimple_seq_add_stmt (seq, call);
14465 : 0 : return tmp;
14466 : : }
14467 : :
14468 : : /* Generate a vector mask of type MASK_TYPE for which index I is false iff
14469 : : J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
14470 : :
14471 : : tree
14472 : 0 : vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
14473 : : tree end_index)
14474 : : {
14475 : 0 : tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
14476 : 0 : return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
14477 : : }
14478 : :
14479 : : /* Try to compute the vector types required to vectorize STMT_INFO,
14480 : : returning true on success and false if vectorization isn't possible.
14481 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
14482 : : take sure that the number of elements in the vectors is no bigger
14483 : : than GROUP_SIZE.
14484 : :
14485 : : On success:
14486 : :
14487 : : - Set *STMT_VECTYPE_OUT to:
14488 : : - NULL_TREE if the statement doesn't need to be vectorized;
14489 : : - the equivalent of STMT_VINFO_VECTYPE otherwise.
14490 : :
14491 : : - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
14492 : : number of units needed to vectorize STMT_INFO, or NULL_TREE if the
14493 : : statement does not help to determine the overall number of units. */
14494 : :
14495 : : opt_result
14496 : 5765875 : vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
14497 : : tree *stmt_vectype_out,
14498 : : tree *nunits_vectype_out,
14499 : : unsigned int group_size)
14500 : : {
14501 : 5765875 : gimple *stmt = stmt_info->stmt;
14502 : :
14503 : : /* For BB vectorization, we should always have a group size once we've
14504 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
14505 : : are tentative requests during things like early data reference
14506 : : analysis and pattern recognition. */
14507 : 5765875 : if (is_a <bb_vec_info> (vinfo))
14508 : 4438502 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
14509 : : else
14510 : : group_size = 0;
14511 : :
14512 : 5765875 : *stmt_vectype_out = NULL_TREE;
14513 : 5765875 : *nunits_vectype_out = NULL_TREE;
14514 : :
14515 : 5765875 : if (gimple_get_lhs (stmt) == NULL_TREE
14516 : : /* Allow vector conditionals through here. */
14517 : 1457 : && !is_a <gcond *> (stmt)
14518 : : /* MASK_STORE and friends have no lhs, but are ok. */
14519 : 5768769 : && !(is_gimple_call (stmt)
14520 : 1457 : && gimple_call_internal_p (stmt)
14521 : 1437 : && internal_store_fn_p (gimple_call_internal_fn (stmt))))
14522 : : {
14523 : 20 : if (is_a <gcall *> (stmt))
14524 : : {
14525 : : /* Ignore calls with no lhs. These must be calls to
14526 : : #pragma omp simd functions, and what vectorization factor
14527 : : it really needs can't be determined until
14528 : : vectorizable_simd_clone_call. */
14529 : 20 : if (dump_enabled_p ())
14530 : 18 : dump_printf_loc (MSG_NOTE, vect_location,
14531 : : "defer to SIMD clone analysis.\n");
14532 : 20 : return opt_result::success ();
14533 : : }
14534 : :
14535 : 0 : return opt_result::failure_at (stmt,
14536 : : "not vectorized: irregular stmt: %G", stmt);
14537 : : }
14538 : :
14539 : 5765855 : tree vectype;
14540 : 5765855 : tree scalar_type = NULL_TREE;
14541 : 5765855 : if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
14542 : : {
14543 : 1321689 : vectype = STMT_VINFO_VECTYPE (stmt_info);
14544 : 1321689 : if (dump_enabled_p ())
14545 : 74811 : dump_printf_loc (MSG_NOTE, vect_location,
14546 : : "precomputed vectype: %T\n", vectype);
14547 : : }
14548 : 4444166 : else if (vect_use_mask_type_p (stmt_info))
14549 : : {
14550 : 191420 : unsigned int precision = stmt_info->mask_precision;
14551 : 191420 : scalar_type = build_nonstandard_integer_type (precision, 1);
14552 : 191420 : vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
14553 : 191420 : if (!vectype)
14554 : 0 : return opt_result::failure_at (stmt, "not vectorized: unsupported"
14555 : : " data-type %T\n", scalar_type);
14556 : 191420 : if (dump_enabled_p ())
14557 : 4345 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14558 : : }
14559 : : else
14560 : : {
14561 : : /* If we got here with a gcond it means that the target had no available vector
14562 : : mode for the scalar type. We can't vectorize so abort. */
14563 : 4252746 : if (is_a <gcond *> (stmt))
14564 : 0 : return opt_result::failure_at (stmt,
14565 : : "not vectorized:"
14566 : : " unsupported data-type for gcond %T\n",
14567 : : scalar_type);
14568 : :
14569 : 4252746 : if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
14570 : 1461326 : scalar_type = TREE_TYPE (DR_REF (dr));
14571 : : else
14572 : 2791420 : scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
14573 : :
14574 : 4252746 : if (dump_enabled_p ())
14575 : : {
14576 : 64927 : if (group_size)
14577 : 7236 : dump_printf_loc (MSG_NOTE, vect_location,
14578 : : "get vectype for scalar type (group size %d):"
14579 : : " %T\n", group_size, scalar_type);
14580 : : else
14581 : 57691 : dump_printf_loc (MSG_NOTE, vect_location,
14582 : : "get vectype for scalar type: %T\n", scalar_type);
14583 : : }
14584 : 4252746 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14585 : 4252746 : if (!vectype)
14586 : 204599 : return opt_result::failure_at (stmt,
14587 : : "not vectorized:"
14588 : : " unsupported data-type %T\n",
14589 : : scalar_type);
14590 : :
14591 : 4048147 : if (dump_enabled_p ())
14592 : 64762 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14593 : : }
14594 : :
14595 : 4314378 : if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
14596 : 0 : return opt_result::failure_at (stmt,
14597 : : "not vectorized: vector stmt in loop:%G",
14598 : : stmt);
14599 : :
14600 : 5561256 : *stmt_vectype_out = vectype;
14601 : :
14602 : : /* Don't try to compute scalar types if the stmt produces a boolean
14603 : : vector; use the existing vector type instead. */
14604 : 5561256 : tree nunits_vectype = vectype;
14605 : 5561256 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14606 : : {
14607 : : /* The number of units is set according to the smallest scalar
14608 : : type (or the largest vector size, but we only support one
14609 : : vector size per vectorization). */
14610 : 5089113 : scalar_type = vect_get_smallest_scalar_type (stmt_info,
14611 : 5089113 : TREE_TYPE (vectype));
14612 : 5089113 : if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
14613 : : {
14614 : 1017940 : if (dump_enabled_p ())
14615 : 9456 : dump_printf_loc (MSG_NOTE, vect_location,
14616 : : "get vectype for smallest scalar type: %T\n",
14617 : : scalar_type);
14618 : 1017940 : nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
14619 : : group_size);
14620 : 1017940 : if (!nunits_vectype)
14621 : 7 : return opt_result::failure_at
14622 : 7 : (stmt, "not vectorized: unsupported data-type %T\n",
14623 : : scalar_type);
14624 : 1017933 : if (dump_enabled_p ())
14625 : 9456 : dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
14626 : : nunits_vectype);
14627 : : }
14628 : : }
14629 : :
14630 : 5561249 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
14631 : 5561249 : TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
14632 : 0 : return opt_result::failure_at (stmt,
14633 : : "Not vectorized: Incompatible number "
14634 : : "of vector subparts between %T and %T\n",
14635 : : nunits_vectype, *stmt_vectype_out);
14636 : :
14637 : 5561249 : if (dump_enabled_p ())
14638 : : {
14639 : 143918 : dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
14640 : 143918 : dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
14641 : 143918 : dump_printf (MSG_NOTE, "\n");
14642 : : }
14643 : :
14644 : 5561249 : *nunits_vectype_out = nunits_vectype;
14645 : 5561249 : return opt_result::success ();
14646 : : }
14647 : :
14648 : : /* Generate and return statement sequence that sets vector length LEN that is:
14649 : :
14650 : : min_of_start_and_end = min (START_INDEX, END_INDEX);
14651 : : left_len = END_INDEX - min_of_start_and_end;
14652 : : rhs = min (left_len, LEN_LIMIT);
14653 : : LEN = rhs;
14654 : :
14655 : : Note: the cost of the code generated by this function is modeled
14656 : : by vect_estimate_min_profitable_iters, so changes here may need
14657 : : corresponding changes there. */
14658 : :
14659 : : gimple_seq
14660 : 0 : vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
14661 : : {
14662 : 0 : gimple_seq stmts = NULL;
14663 : 0 : tree len_type = TREE_TYPE (len);
14664 : 0 : gcc_assert (TREE_TYPE (start_index) == len_type);
14665 : :
14666 : 0 : tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
14667 : 0 : tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
14668 : 0 : tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
14669 : 0 : gimple* stmt = gimple_build_assign (len, rhs);
14670 : 0 : gimple_seq_add_stmt (&stmts, stmt);
14671 : :
14672 : 0 : return stmts;
14673 : : }
14674 : :
|