Line data Source code
1 : /* Statement Analysis and Transformation for Vectorization
2 : Copyright (C) 2003-2026 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 6106709 : stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
72 : {
73 6106709 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
74 6106709 : basic_block bb = gimple_bb (stmt);
75 6106709 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
76 2794385 : class loop* loop;
77 :
78 2794385 : if (!loop_vinfo)
79 : return false;
80 :
81 2794385 : loop = LOOP_VINFO_LOOP (loop_vinfo);
82 :
83 2794385 : 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 9015940 : 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 9015940 : if ((kind == vector_load || kind == unaligned_load)
98 1646159 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
99 : kind = vector_gather_load;
100 9015940 : if ((kind == vector_store || kind == unaligned_store)
101 1040435 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
102 9015940 : kind = vector_scatter_store;
103 :
104 9015940 : stmt_info_for_cost si
105 9015940 : = { count, kind, where, stmt_info, node, vectype, misalign };
106 9015940 : body_cost_vec->safe_push (si);
107 :
108 9015940 : return (unsigned)
109 9015940 : (builtin_vectorization_cost (kind, vectype, misalign) * count);
110 : }
111 :
112 : unsigned
113 4198468 : 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 4198468 : return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
119 4198468 : vectype, misalign, where);
120 : }
121 :
122 : unsigned
123 1691281 : 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 1691281 : return record_stmt_cost (body_cost_vec, count, kind,
129 : SLP_TREE_REPRESENTATIVE (node), node,
130 1691281 : vectype, misalign, where);
131 : }
132 :
133 : unsigned
134 0 : 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 0 : gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
139 : || kind == scalar_stmt);
140 0 : return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
141 0 : 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 3253088 : vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
253 : enum vect_relevant relevant, bool live_p)
254 : {
255 3253088 : enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
256 3253088 : bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
257 :
258 3253088 : if (dump_enabled_p ())
259 164599 : 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 3253088 : 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 249199 : if (dump_enabled_p ())
275 2822 : dump_printf_loc (MSG_NOTE, vect_location,
276 : "last stmt in pattern. don't mark"
277 : " relevant/live.\n");
278 :
279 249199 : stmt_vec_info old_stmt_info = stmt_info;
280 249199 : stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
281 249199 : gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
282 249199 : save_relevant = STMT_VINFO_RELEVANT (stmt_info);
283 249199 : save_live_p = STMT_VINFO_LIVE_P (stmt_info);
284 :
285 249199 : if (live_p && relevant == vect_unused_in_scope)
286 : {
287 110 : if (dump_enabled_p ())
288 10 : 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 249199 : if (dump_enabled_p ())
295 2822 : dump_printf_loc (MSG_NOTE, vect_location,
296 : "mark relevant %d, live %d: %G", relevant, live_p,
297 : stmt_info->stmt);
298 : }
299 :
300 3253088 : STMT_VINFO_LIVE_P (stmt_info) |= live_p;
301 3253088 : if (relevant > STMT_VINFO_RELEVANT (stmt_info))
302 2920479 : STMT_VINFO_RELEVANT (stmt_info) = relevant;
303 :
304 3253088 : if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
305 332609 : && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
306 : {
307 331896 : if (dump_enabled_p ())
308 19689 : dump_printf_loc (MSG_NOTE, vect_location,
309 : "already marked relevant/live.\n");
310 331896 : return;
311 : }
312 :
313 2921192 : 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 251581 : is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
323 : loop_vec_info loop_vinfo)
324 : {
325 251581 : tree op;
326 251581 : ssa_op_iter iter;
327 :
328 448062 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
329 197304 : if (!stmt)
330 : return false;
331 :
332 204870 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
333 : {
334 204047 : enum vect_def_type dt = vect_uninitialized_def;
335 :
336 204047 : if (!vect_is_simple_use (op, loop_vinfo, &dt))
337 : {
338 5371 : if (dump_enabled_p ())
339 16 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
340 : "use not simple.\n");
341 196481 : return false;
342 : }
343 :
344 198676 : 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 :
360 : CHECKME: what other side effects would the vectorizer allow? */
361 :
362 : static bool
363 5251813 : vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
364 : enum vect_relevant *relevant, bool *live_p)
365 : {
366 5251813 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
367 5251813 : ssa_op_iter op_iter;
368 5251813 : imm_use_iterator imm_iter;
369 5251813 : use_operand_p use_p;
370 5251813 : def_operand_p def_p;
371 :
372 5251813 : *relevant = vect_unused_in_scope;
373 5251813 : *live_p = false;
374 :
375 : /* cond stmt other than loop exit cond. */
376 5251813 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
377 5251813 : if (is_ctrl_stmt (stmt)
378 620542 : && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
379 5487630 : && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
380 233832 : *relevant = vect_used_in_scope;
381 :
382 : /* changing memory. */
383 5251813 : if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
384 4350414 : if (gimple_vdef (stmt_info->stmt)
385 3729872 : && !gimple_clobber_p (stmt_info->stmt))
386 : {
387 374331 : if (dump_enabled_p ())
388 28143 : dump_printf_loc (MSG_NOTE, vect_location,
389 : "vec_stmt_relevant_p: stmt has vdefs.\n");
390 374331 : *relevant = vect_used_in_scope;
391 374331 : if (! STMT_VINFO_DATA_REF (stmt_info)
392 374331 : && zero_ssa_operands (stmt_info->stmt, SSA_OP_DEF))
393 20 : LOOP_VINFO_ALTERNATE_DEFS (loop_vinfo).safe_push (stmt_info);
394 : }
395 :
396 : /* uses outside the loop. */
397 14753974 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
398 : {
399 15632426 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
400 : {
401 7131730 : basic_block bb = gimple_bb (USE_STMT (use_p));
402 7131730 : if (!flow_bb_inside_loop_p (loop, bb))
403 : {
404 266645 : if (is_gimple_debug (USE_STMT (use_p)))
405 1143 : continue;
406 :
407 265502 : if (dump_enabled_p ())
408 6005 : dump_printf_loc (MSG_NOTE, vect_location,
409 : "vec_stmt_relevant_p: used out of loop.\n");
410 :
411 : /* We expect all such uses to be in the loop exit phis
412 : (because of loop closed form) */
413 265502 : gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
414 :
415 265502 : *live_p = true;
416 265502 : LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo) = true;
417 : }
418 4250348 : }
419 : }
420 :
421 251583 : if (*live_p && *relevant == vect_unused_in_scope
422 5503394 : && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
423 : {
424 250758 : if (dump_enabled_p ())
425 5861 : dump_printf_loc (MSG_NOTE, vect_location,
426 : "vec_stmt_relevant_p: stmt live but not relevant.\n");
427 250758 : *relevant = vect_used_only_live;
428 : }
429 :
430 5251813 : return (*live_p || *relevant);
431 : }
432 :
433 :
434 : /* Function exist_non_indexing_operands_for_use_p
435 :
436 : USE is one of the uses attached to STMT_INFO. Check if USE is
437 : used in STMT_INFO for anything other than indexing an array. */
438 :
439 : static bool
440 4360758 : exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
441 : {
442 4360758 : tree operand;
443 :
444 : /* USE corresponds to some operand in STMT. If there is no data
445 : reference in STMT, then any operand that corresponds to USE
446 : is not indexing an array. */
447 4360758 : if (!STMT_VINFO_DATA_REF (stmt_info))
448 : return true;
449 :
450 : /* STMT has a data_ref. FORNOW this means that its of one of
451 : the following forms:
452 : -1- ARRAY_REF = var
453 : -2- var = ARRAY_REF
454 : (This should have been verified in analyze_data_refs).
455 :
456 : 'var' in the second case corresponds to a def, not a use,
457 : so USE cannot correspond to any operands that are not used
458 : for array indexing.
459 :
460 : Therefore, all we need to check is if STMT falls into the
461 : first case, and whether var corresponds to USE. */
462 :
463 1487605 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
464 1469619 : if (!assign || !gimple_assign_copy_p (assign))
465 : {
466 797576 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
467 17986 : if (call && gimple_call_internal_p (call))
468 : {
469 17986 : internal_fn ifn = gimple_call_internal_fn (call);
470 17986 : int mask_index = internal_fn_mask_index (ifn);
471 17986 : if (mask_index >= 0
472 17986 : && use == gimple_call_arg (call, mask_index))
473 : return true;
474 11670 : int els_index = internal_fn_else_index (ifn);
475 11670 : if (els_index >= 0
476 11670 : && use == gimple_call_arg (call, els_index))
477 : return true;
478 10166 : int stored_value_index = internal_fn_stored_value_index (ifn);
479 10166 : if (stored_value_index >= 0
480 10166 : && use == gimple_call_arg (call, stored_value_index))
481 : return true;
482 8016 : if (internal_gather_scatter_fn_p (ifn)
483 8016 : && use == gimple_call_arg (call, 1))
484 : return true;
485 : }
486 787606 : return false;
487 : }
488 :
489 690029 : if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
490 : return false;
491 690029 : operand = gimple_assign_rhs1 (assign);
492 690029 : if (TREE_CODE (operand) != SSA_NAME)
493 : return false;
494 :
495 596696 : if (operand == use)
496 : return true;
497 :
498 : return false;
499 : }
500 :
501 :
502 : /*
503 : Function process_use.
504 :
505 : Inputs:
506 : - a USE in STMT_VINFO in a loop represented by LOOP_VINFO
507 : - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
508 : that defined USE. This is done by calling mark_relevant and passing it
509 : the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
510 : - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
511 : be performed.
512 :
513 : Outputs:
514 : Generally, LIVE_P and RELEVANT are used to define the liveness and
515 : relevance info of the DEF_STMT of this USE:
516 : STMT_VINFO_LIVE_P (DEF_stmt_vinfo) <-- live_p
517 : STMT_VINFO_RELEVANT (DEF_stmt_vinfo) <-- relevant
518 : Exceptions:
519 : - case 1: If USE is used only for address computations (e.g. array indexing),
520 : which does not need to be directly vectorized, then the liveness/relevance
521 : of the respective DEF_STMT is left unchanged.
522 : - case 2: If STMT_VINFO is a reduction phi and DEF_STMT is a reduction stmt,
523 : we skip DEF_STMT cause it had already been processed.
524 : - case 3: If DEF_STMT and STMT_VINFO are in different nests, then
525 : "relevant" will be modified accordingly.
526 :
527 : Return true if everything is as expected. Return false otherwise. */
528 :
529 : static opt_result
530 4417627 : process_use (stmt_vec_info stmt_vinfo, tree use, loop_vec_info loop_vinfo,
531 : enum vect_relevant relevant, vec<stmt_vec_info> *worklist,
532 : bool force)
533 : {
534 4417627 : stmt_vec_info dstmt_vinfo;
535 4417627 : enum vect_def_type dt;
536 :
537 : /* case 1: we are only interested in uses that need to be vectorized. Uses
538 : that are used for address computation are not considered relevant. */
539 4417627 : if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
540 1195710 : return opt_result::success ();
541 :
542 3221917 : if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
543 35245 : return opt_result::failure_at (stmt_vinfo->stmt,
544 : "not vectorized:"
545 : " unsupported use in stmt.\n");
546 :
547 3186672 : if (!dstmt_vinfo)
548 602014 : return opt_result::success ();
549 :
550 2584658 : basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
551 2584658 : basic_block bb = gimple_bb (stmt_vinfo->stmt);
552 :
553 : /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
554 : We have to force the stmt live since the epilogue loop needs it to
555 : continue computing the reduction. */
556 2584658 : if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
557 272417 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
558 85127 : && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
559 85127 : && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
560 2669785 : && bb->loop_father == def_bb->loop_father)
561 : {
562 85127 : if (dump_enabled_p ())
563 3944 : dump_printf_loc (MSG_NOTE, vect_location,
564 : "reduc-stmt defining reduc-phi in the same nest.\n");
565 85127 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
566 85127 : return opt_result::success ();
567 : }
568 :
569 : /* case 3a: outer-loop stmt defining an inner-loop stmt:
570 : outer-loop-header-bb:
571 : d = dstmt_vinfo
572 : inner-loop:
573 : stmt # use (d)
574 : outer-loop-tail-bb:
575 : ... */
576 2499531 : if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
577 : {
578 2237 : if (dump_enabled_p ())
579 321 : dump_printf_loc (MSG_NOTE, vect_location,
580 : "outer-loop def-stmt defining inner-loop stmt.\n");
581 :
582 2237 : switch (relevant)
583 : {
584 0 : case vect_unused_in_scope:
585 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
586 : vect_used_in_scope : vect_unused_in_scope;
587 : break;
588 :
589 776 : case vect_used_in_outer_by_reduction:
590 776 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
591 : relevant = vect_used_by_reduction;
592 : break;
593 :
594 1181 : case vect_used_in_outer:
595 1181 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
596 : relevant = vect_used_in_scope;
597 : break;
598 :
599 : case vect_used_in_scope:
600 : break;
601 :
602 0 : default:
603 0 : gcc_unreachable ();
604 : }
605 : }
606 :
607 : /* case 3b: inner-loop stmt defining an outer-loop stmt:
608 : outer-loop-header-bb:
609 : ...
610 : inner-loop:
611 : d = dstmt_vinfo
612 : outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
613 : stmt # use (d) */
614 2497294 : else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
615 : {
616 2100 : if (dump_enabled_p ())
617 626 : dump_printf_loc (MSG_NOTE, vect_location,
618 : "inner-loop def-stmt defining outer-loop stmt.\n");
619 :
620 2100 : switch (relevant)
621 : {
622 0 : case vect_unused_in_scope:
623 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
624 0 : || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
625 : vect_used_in_outer_by_reduction : vect_unused_in_scope;
626 : break;
627 :
628 : case vect_used_by_reduction:
629 : case vect_used_only_live:
630 : relevant = vect_used_in_outer_by_reduction;
631 : break;
632 :
633 : case vect_used_in_scope:
634 2318618 : relevant = vect_used_in_outer;
635 : break;
636 :
637 0 : default:
638 0 : gcc_unreachable ();
639 : }
640 : }
641 : /* We are also not interested in uses on loop PHI backedges that are
642 : inductions. Otherwise we'll needlessly vectorize the IV increment
643 : and cause hybrid SLP for SLP inductions. */
644 2495194 : else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
645 183956 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
646 2676107 : && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
647 : loop_latch_edge (bb->loop_father))
648 : == use))
649 : {
650 180913 : if (dump_enabled_p ())
651 4882 : dump_printf_loc (MSG_NOTE, vect_location,
652 : "induction value on backedge.\n");
653 180913 : return opt_result::success ();
654 : }
655 :
656 2318618 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
657 2318618 : return opt_result::success ();
658 : }
659 :
660 :
661 : /* Function vect_mark_stmts_to_be_vectorized.
662 :
663 : Not all stmts in the loop need to be vectorized. For example:
664 :
665 : for i...
666 : for j...
667 : 1. T0 = i + j
668 : 2. T1 = a[T0]
669 :
670 : 3. j = j + 1
671 :
672 : Stmt 1 and 3 do not need to be vectorized, because loop control and
673 : addressing of vectorized data-refs are handled differently.
674 :
675 : This pass detects such stmts. */
676 :
677 : opt_result
678 441443 : vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
679 : {
680 441443 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
681 441443 : basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
682 441443 : unsigned int nbbs = loop->num_nodes;
683 441443 : gimple_stmt_iterator si;
684 441443 : unsigned int i;
685 441443 : basic_block bb;
686 441443 : bool live_p;
687 441443 : enum vect_relevant relevant;
688 :
689 441443 : DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
690 :
691 441443 : auto_vec<stmt_vec_info, 64> worklist;
692 :
693 : /* 1. Init worklist. */
694 1496877 : for (i = 0; i < nbbs; i++)
695 : {
696 1065979 : bb = bbs[i];
697 2190076 : for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
698 : {
699 2268996 : if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
700 233099 : continue;
701 901399 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
702 901399 : if (dump_enabled_p ())
703 41739 : dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
704 : phi_info->stmt);
705 :
706 901399 : if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
707 : {
708 45410 : if (STMT_VINFO_DEF_TYPE (phi_info) == vect_unknown_def_type)
709 10401 : return opt_result::failure_at
710 10401 : (*si, "not vectorized: unhandled relevant PHI: %G", *si);
711 35009 : vect_mark_relevant (&worklist, phi_info, relevant, live_p);
712 : }
713 : }
714 8464424 : for (si = gsi_after_labels (bb); !gsi_end_p (si); gsi_next (&si))
715 : {
716 7408990 : gimple *stmt = gsi_stmt (si);
717 7408990 : if (is_gimple_debug (stmt))
718 3058432 : continue;
719 4350558 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
720 4350558 : if (dump_enabled_p ())
721 224036 : dump_printf_loc (MSG_NOTE, vect_location,
722 : "init: stmt relevant? %G", stmt);
723 :
724 4350558 : if (gimple_get_lhs (stmt) == NULL_TREE
725 626769 : && !is_a <gcond *> (stmt)
726 4356785 : && !is_a <gcall *> (stmt))
727 144 : return opt_result::failure_at
728 144 : (stmt, "not vectorized: irregular stmt: %G", stmt);
729 :
730 4350414 : if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
731 814334 : vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
732 : }
733 : }
734 :
735 : /* 2. Process_worklist */
736 3227084 : while (worklist.length () > 0)
737 : {
738 2831433 : use_operand_p use_p;
739 2831433 : ssa_op_iter iter;
740 :
741 2831433 : stmt_vec_info stmt_vinfo = worklist.pop ();
742 2831433 : if (dump_enabled_p ())
743 144270 : dump_printf_loc (MSG_NOTE, vect_location,
744 : "worklist: examine stmt: %G", stmt_vinfo->stmt);
745 :
746 : /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
747 : (DEF_STMT) as relevant/irrelevant according to the relevance property
748 : of STMT. */
749 2831433 : relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
750 :
751 : /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
752 : propagated as is to the DEF_STMTs of its USEs.
753 :
754 : One exception is when STMT has been identified as defining a reduction
755 : variable; in this case we set the relevance to vect_used_by_reduction.
756 : This is because we distinguish between two kinds of relevant stmts -
757 : those that are used by a reduction computation, and those that are
758 : (also) used by a regular computation. This allows us later on to
759 : identify stmts that are used solely by a reduction, and therefore the
760 : order of the results that they produce does not have to be kept. */
761 :
762 2831433 : switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
763 : {
764 172357 : case vect_reduction_def:
765 172357 : gcc_assert (relevant != vect_unused_in_scope);
766 172357 : if (relevant != vect_unused_in_scope
767 172357 : && relevant != vect_used_in_scope
768 172357 : && relevant != vect_used_by_reduction
769 172357 : && relevant != vect_used_only_live)
770 0 : return opt_result::failure_at
771 0 : (stmt_vinfo->stmt, "unsupported use of reduction.\n");
772 : break;
773 :
774 2209 : case vect_nested_cycle:
775 2209 : if (relevant != vect_unused_in_scope
776 2209 : && relevant != vect_used_in_outer_by_reduction
777 1614 : && relevant != vect_used_in_outer)
778 2 : return opt_result::failure_at
779 2 : (stmt_vinfo->stmt, "unsupported use of nested cycle.\n");
780 : break;
781 :
782 1209 : case vect_double_reduction_def:
783 1209 : if (relevant != vect_unused_in_scope
784 1209 : && relevant != vect_used_by_reduction
785 409 : && relevant != vect_used_only_live)
786 0 : return opt_result::failure_at
787 0 : (stmt_vinfo->stmt, "unsupported use of double reduction.\n");
788 : break;
789 :
790 : default:
791 : break;
792 : }
793 :
794 2831431 : if (is_pattern_stmt_p (stmt_vinfo))
795 : {
796 : /* Pattern statements are not inserted into the code, so
797 : FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
798 : have to scan the RHS or function arguments instead. */
799 639002 : if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
800 : {
801 414683 : enum tree_code rhs_code = gimple_assign_rhs_code (assign);
802 414683 : tree op = gimple_assign_rhs1 (assign);
803 :
804 414683 : i = 1;
805 414683 : if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
806 : {
807 0 : opt_result res
808 0 : = process_use (stmt_vinfo, TREE_OPERAND (op, 0),
809 : loop_vinfo, relevant, &worklist, false);
810 0 : if (!res)
811 0 : return res;
812 0 : res = process_use (stmt_vinfo, TREE_OPERAND (op, 1),
813 : loop_vinfo, relevant, &worklist, false);
814 0 : if (!res)
815 0 : return res;
816 : i = 2;
817 : }
818 1194521 : for (; i < gimple_num_ops (assign); i++)
819 : {
820 783648 : op = gimple_op (assign, i);
821 783648 : if (TREE_CODE (op) == SSA_NAME)
822 : {
823 595040 : opt_result res
824 595040 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
825 : &worklist, false);
826 595040 : if (!res)
827 3810 : return res;
828 : }
829 : }
830 : }
831 224319 : else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
832 : {
833 217702 : tree_code rhs_code = gimple_cond_code (cond);
834 217702 : gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
835 217702 : opt_result res
836 217702 : = process_use (stmt_vinfo, gimple_cond_lhs (cond),
837 : loop_vinfo, relevant, &worklist, false);
838 217702 : if (!res)
839 35247 : return res;
840 217702 : res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
841 : loop_vinfo, relevant, &worklist, false);
842 217702 : if (!res)
843 0 : return res;
844 : }
845 6617 : else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
846 : {
847 31637 : for (i = 0; i < gimple_call_num_args (call); i++)
848 : {
849 25020 : tree arg = gimple_call_arg (call, i);
850 25020 : opt_result res
851 25020 : = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
852 : &worklist, false);
853 25020 : if (!res)
854 0 : return res;
855 : }
856 : }
857 : else
858 0 : gcc_unreachable ();
859 : }
860 : else
861 7672002 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
862 : {
863 3305294 : tree op = USE_FROM_PTR (use_p);
864 3305294 : opt_result res
865 3305294 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
866 : &worklist, false);
867 3305294 : if (!res)
868 18150 : return res;
869 : }
870 :
871 2809471 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
872 : {
873 56869 : gather_scatter_info gs_info;
874 56869 : if (!vect_check_gather_scatter (stmt_vinfo,
875 : STMT_VINFO_VECTYPE (stmt_vinfo),
876 : loop_vinfo, &gs_info))
877 0 : gcc_unreachable ();
878 56869 : opt_result res
879 56869 : = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
880 : &worklist, true);
881 56869 : if (!res)
882 : {
883 13285 : if (fatal)
884 13285 : *fatal = false;
885 13285 : return res;
886 : }
887 : }
888 : } /* while worklist */
889 :
890 395651 : return opt_result::success ();
891 441443 : }
892 :
893 : /* Function vect_model_simple_cost.
894 :
895 : Models cost for simple operations, i.e. those that only emit N operations
896 : of the same KIND. */
897 :
898 : static void
899 670861 : vect_model_simple_cost (vec_info *vinfo, int n, slp_tree node,
900 : stmt_vector_for_cost *cost_vec,
901 : vect_cost_for_stmt kind = vector_stmt)
902 : {
903 670861 : int inside_cost = 0, prologue_cost = 0;
904 :
905 670861 : gcc_assert (cost_vec != NULL);
906 :
907 670861 : n *= vect_get_num_copies (vinfo, node);
908 :
909 : /* Pass the inside-of-loop statements to the target-specific cost model. */
910 670861 : inside_cost += record_stmt_cost (cost_vec, n, kind, node, 0, vect_body);
911 :
912 670861 : if (dump_enabled_p ())
913 33316 : dump_printf_loc (MSG_NOTE, vect_location,
914 : "vect_model_simple_cost: inside_cost = %d, "
915 : "prologue_cost = %d .\n", inside_cost, prologue_cost);
916 670861 : }
917 :
918 :
919 : /* Model cost for type demotion and promotion operations. PWR is
920 : normally zero for single-step promotions and demotions. It will be
921 : one if two-step promotion/demotion is required, and so on. NCOPIES
922 : is the number of vector results (and thus number of instructions)
923 : for the narrowest end of the operation chain. Each additional
924 : step doubles the number of instructions required. If WIDEN_ARITH
925 : is true the stmt is doing widening arithmetic. */
926 :
927 : static void
928 69400 : vect_model_promotion_demotion_cost (slp_tree slp_node,
929 : unsigned int ncopies, int pwr,
930 : stmt_vector_for_cost *cost_vec,
931 : bool widen_arith)
932 : {
933 69400 : int i;
934 69400 : int inside_cost = 0, prologue_cost = 0;
935 :
936 161606 : for (i = 0; i < pwr + 1; i++)
937 : {
938 182511 : inside_cost += record_stmt_cost (cost_vec, ncopies,
939 : widen_arith
940 : ? vector_stmt : vec_promote_demote,
941 : slp_node, 0, vect_body);
942 92206 : ncopies *= 2;
943 : }
944 :
945 69400 : if (dump_enabled_p ())
946 6375 : dump_printf_loc (MSG_NOTE, vect_location,
947 : "vect_model_promotion_demotion_cost: inside_cost = %d, "
948 : "prologue_cost = %d .\n", inside_cost, prologue_cost);
949 69400 : }
950 :
951 : /* Returns true if the current function returns DECL. */
952 :
953 : static bool
954 566821 : cfun_returns (tree decl)
955 : {
956 566821 : edge_iterator ei;
957 566821 : edge e;
958 1116227 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
959 : {
960 1122294 : greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
961 561147 : if (!ret)
962 0 : continue;
963 561147 : if (gimple_return_retval (ret) == decl)
964 : return true;
965 : /* We often end up with an aggregate copy to the result decl,
966 : handle that case as well. First skip intermediate clobbers
967 : though. */
968 : gimple *def = ret;
969 1697708 : do
970 : {
971 3395416 : def = SSA_NAME_DEF_STMT (gimple_vuse (def));
972 : }
973 1697708 : while (gimple_clobber_p (def));
974 550123 : if (is_a <gassign *> (def)
975 62437 : && gimple_assign_lhs (def) == gimple_return_retval (ret)
976 557216 : && gimple_assign_rhs1 (def) == decl)
977 : return true;
978 : }
979 : return false;
980 : }
981 :
982 : /* Calculate cost of DR's memory access. */
983 : void
984 1030348 : vect_get_store_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
985 : int ncopies, dr_alignment_support alignment_support_scheme,
986 : int misalignment,
987 : unsigned int *inside_cost,
988 : stmt_vector_for_cost *body_cost_vec)
989 : {
990 1030348 : tree vectype
991 1030348 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
992 1030348 : switch (alignment_support_scheme)
993 : {
994 565866 : case dr_aligned:
995 565866 : {
996 565866 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
997 : vector_store, stmt_info, slp_node,
998 : vectype, 0, vect_body);
999 :
1000 565866 : if (dump_enabled_p ())
1001 14545 : dump_printf_loc (MSG_NOTE, vect_location,
1002 : "vect_model_store_cost: aligned.\n");
1003 : break;
1004 : }
1005 :
1006 464482 : case dr_unaligned_supported:
1007 464482 : {
1008 : /* Here, we assign an additional cost for the unaligned store. */
1009 464482 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1010 : unaligned_store, stmt_info, slp_node,
1011 : vectype, misalignment, vect_body);
1012 464482 : if (dump_enabled_p ())
1013 12958 : dump_printf_loc (MSG_NOTE, vect_location,
1014 : "vect_model_store_cost: unaligned supported by "
1015 : "hardware.\n");
1016 : break;
1017 : }
1018 :
1019 0 : case dr_unaligned_unsupported:
1020 0 : {
1021 0 : *inside_cost = VECT_MAX_COST;
1022 :
1023 0 : if (dump_enabled_p ())
1024 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1025 : "vect_model_store_cost: unsupported access.\n");
1026 : break;
1027 : }
1028 :
1029 0 : default:
1030 0 : gcc_unreachable ();
1031 : }
1032 1030348 : }
1033 :
1034 : /* Calculate cost of DR's memory access. */
1035 : void
1036 974327 : vect_get_load_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1037 : int ncopies, dr_alignment_support alignment_support_scheme,
1038 : int misalignment,
1039 : bool add_realign_cost, unsigned int *inside_cost,
1040 : unsigned int *prologue_cost,
1041 : stmt_vector_for_cost *prologue_cost_vec,
1042 : stmt_vector_for_cost *body_cost_vec,
1043 : bool record_prologue_costs)
1044 : {
1045 974327 : tree vectype
1046 974327 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1047 974327 : switch (alignment_support_scheme)
1048 : {
1049 559869 : case dr_aligned:
1050 559869 : {
1051 559869 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1052 : stmt_info, slp_node, vectype,
1053 : 0, vect_body);
1054 :
1055 559869 : if (dump_enabled_p ())
1056 19123 : dump_printf_loc (MSG_NOTE, vect_location,
1057 : "vect_model_load_cost: aligned.\n");
1058 :
1059 : break;
1060 : }
1061 354246 : case dr_unaligned_supported:
1062 354246 : {
1063 : /* Here, we assign an additional cost for the unaligned load. */
1064 354246 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1065 : unaligned_load, stmt_info, slp_node,
1066 : vectype, misalignment, vect_body);
1067 :
1068 354246 : if (dump_enabled_p ())
1069 22390 : dump_printf_loc (MSG_NOTE, vect_location,
1070 : "vect_model_load_cost: unaligned supported by "
1071 : "hardware.\n");
1072 :
1073 : break;
1074 : }
1075 0 : case dr_explicit_realign:
1076 0 : {
1077 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1078 : vector_load, stmt_info, slp_node,
1079 : vectype, 0, vect_body);
1080 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1081 : vec_perm, stmt_info, slp_node,
1082 : vectype, 0, vect_body);
1083 :
1084 : /* FIXME: If the misalignment remains fixed across the iterations of
1085 : the containing loop, the following cost should be added to the
1086 : prologue costs. */
1087 0 : if (targetm.vectorize.builtin_mask_for_load)
1088 0 : *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1089 : stmt_info, slp_node, vectype,
1090 : 0, vect_body);
1091 :
1092 0 : if (dump_enabled_p ())
1093 0 : dump_printf_loc (MSG_NOTE, vect_location,
1094 : "vect_model_load_cost: explicit realign\n");
1095 :
1096 : break;
1097 : }
1098 0 : case dr_explicit_realign_optimized:
1099 0 : {
1100 0 : if (dump_enabled_p ())
1101 0 : dump_printf_loc (MSG_NOTE, vect_location,
1102 : "vect_model_load_cost: unaligned software "
1103 : "pipelined.\n");
1104 :
1105 : /* Unaligned software pipeline has a load of an address, an initial
1106 : load, and possibly a mask operation to "prime" the loop. However,
1107 : if this is an access in a group of loads, which provide grouped
1108 : access, then the above cost should only be considered for one
1109 : access in the group. Inside the loop, there is a load op
1110 : and a realignment op. */
1111 :
1112 0 : if (add_realign_cost && record_prologue_costs)
1113 : {
1114 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1115 : vector_stmt, stmt_info,
1116 : slp_node, vectype,
1117 : 0, vect_prologue);
1118 0 : if (targetm.vectorize.builtin_mask_for_load)
1119 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1120 : vector_stmt, stmt_info,
1121 : slp_node, vectype,
1122 : 0, vect_prologue);
1123 : }
1124 :
1125 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1126 : stmt_info, slp_node, vectype,
1127 : 0, vect_body);
1128 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1129 : stmt_info, slp_node, vectype,
1130 : 0, vect_body);
1131 :
1132 0 : if (dump_enabled_p ())
1133 0 : dump_printf_loc (MSG_NOTE, vect_location,
1134 : "vect_model_load_cost: explicit realign optimized"
1135 : "\n");
1136 :
1137 : break;
1138 : }
1139 :
1140 60212 : case dr_unaligned_unsupported:
1141 60212 : {
1142 60212 : *inside_cost = VECT_MAX_COST;
1143 :
1144 60212 : if (dump_enabled_p ())
1145 104 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1146 : "vect_model_load_cost: unsupported access.\n");
1147 : break;
1148 : }
1149 :
1150 0 : default:
1151 0 : gcc_unreachable ();
1152 : }
1153 974327 : }
1154 :
1155 : /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1156 : the loop preheader for the vectorized stmt STMT_VINFO. */
1157 :
1158 : static void
1159 6635 : vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
1160 : gimple_stmt_iterator *gsi)
1161 : {
1162 6635 : if (gsi)
1163 3344 : vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
1164 : else
1165 3291 : vinfo->insert_on_entry (stmt_vinfo, new_stmt);
1166 :
1167 6635 : if (dump_enabled_p ())
1168 1815 : dump_printf_loc (MSG_NOTE, vect_location,
1169 : "created new init_stmt: %G", new_stmt);
1170 6635 : }
1171 :
1172 : /* Function vect_init_vector.
1173 :
1174 : Insert a new stmt (INIT_STMT) that initializes a new variable of type
1175 : TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1176 : vector type a vector with all elements equal to VAL is created first.
1177 : Place the initialization at GSI if it is not NULL. Otherwise, place the
1178 : initialization at the loop preheader.
1179 : Return the DEF of INIT_STMT.
1180 : It will be used in the vectorization of STMT_INFO. */
1181 :
1182 : tree
1183 4920 : vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
1184 : gimple_stmt_iterator *gsi)
1185 : {
1186 4920 : gimple *init_stmt;
1187 4920 : tree new_temp;
1188 :
1189 : /* We abuse this function to push sth to a SSA name with initial 'val'. */
1190 4920 : if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1191 : {
1192 1331 : gcc_assert (VECTOR_TYPE_P (type));
1193 1331 : if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1194 : {
1195 : /* Scalar boolean value should be transformed into
1196 : all zeros or all ones value before building a vector. */
1197 11 : if (VECTOR_BOOLEAN_TYPE_P (type))
1198 : {
1199 3 : tree true_val = build_all_ones_cst (TREE_TYPE (type));
1200 3 : tree false_val = build_zero_cst (TREE_TYPE (type));
1201 :
1202 3 : if (CONSTANT_CLASS_P (val))
1203 0 : val = integer_zerop (val) ? false_val : true_val;
1204 : else
1205 : {
1206 3 : new_temp = make_ssa_name (TREE_TYPE (type));
1207 3 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1208 : val, true_val, false_val);
1209 3 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1210 3 : val = new_temp;
1211 : }
1212 : }
1213 : else
1214 : {
1215 8 : gimple_seq stmts = NULL;
1216 8 : if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1217 8 : val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
1218 8 : TREE_TYPE (type), val);
1219 : else
1220 : /* ??? Condition vectorization expects us to do
1221 : promotion of invariant/external defs. */
1222 0 : val = gimple_convert (&stmts, TREE_TYPE (type), val);
1223 16 : for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
1224 16 : !gsi_end_p (gsi2); )
1225 : {
1226 8 : init_stmt = gsi_stmt (gsi2);
1227 8 : gsi_remove (&gsi2, false);
1228 8 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1229 : }
1230 : }
1231 : }
1232 1331 : val = build_vector_from_val (type, val);
1233 : }
1234 :
1235 4920 : new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1236 4920 : init_stmt = gimple_build_assign (new_temp, val);
1237 4920 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1238 4920 : return new_temp;
1239 : }
1240 :
1241 :
1242 : /* Get vectorized definitions for OP0 and OP1. */
1243 :
1244 : void
1245 191087 : vect_get_vec_defs (vec_info *, slp_tree slp_node,
1246 : tree op0, vec<tree> *vec_oprnds0,
1247 : tree op1, vec<tree> *vec_oprnds1,
1248 : tree op2, vec<tree> *vec_oprnds2,
1249 : tree op3, vec<tree> *vec_oprnds3)
1250 : {
1251 191087 : if (op0)
1252 189425 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
1253 191087 : if (op1)
1254 140303 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
1255 191087 : if (op2)
1256 9406 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
1257 191087 : if (op3)
1258 0 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
1259 191087 : }
1260 :
1261 : /* Helper function called by vect_finish_replace_stmt and
1262 : vect_finish_stmt_generation. Set the location of the new
1263 : statement and create and return a stmt_vec_info for it. */
1264 :
1265 : static void
1266 1450044 : vect_finish_stmt_generation_1 (vec_info *,
1267 : stmt_vec_info stmt_info, gimple *vec_stmt)
1268 : {
1269 1450044 : if (dump_enabled_p ())
1270 147589 : dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
1271 :
1272 1450044 : if (stmt_info)
1273 : {
1274 1421567 : gimple_set_location (vec_stmt, gimple_location (stmt_info->stmt));
1275 :
1276 : /* While EH edges will generally prevent vectorization, stmt might
1277 : e.g. be in a must-not-throw region. Ensure newly created stmts
1278 : that could throw are part of the same region. */
1279 1421567 : int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
1280 1421567 : if (lp_nr != 0 && stmt_could_throw_p (cfun, vec_stmt))
1281 48 : add_stmt_to_eh_lp (vec_stmt, lp_nr);
1282 : }
1283 : else
1284 28477 : gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
1285 1450044 : }
1286 :
1287 : /* Replace the scalar statement STMT_INFO with a new vector statement VEC_STMT,
1288 : which sets the same scalar result as STMT_INFO did. Create and return a
1289 : stmt_vec_info for VEC_STMT. */
1290 :
1291 : void
1292 911 : vect_finish_replace_stmt (vec_info *vinfo,
1293 : stmt_vec_info stmt_info, gimple *vec_stmt)
1294 : {
1295 911 : gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
1296 911 : gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
1297 :
1298 911 : gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
1299 911 : gsi_replace (&gsi, vec_stmt, true);
1300 :
1301 911 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1302 911 : }
1303 :
1304 : /* Add VEC_STMT to the vectorized implementation of STMT_INFO and insert it
1305 : before *GSI. Create and return a stmt_vec_info for VEC_STMT. */
1306 :
1307 : void
1308 1449133 : vect_finish_stmt_generation (vec_info *vinfo,
1309 : stmt_vec_info stmt_info, gimple *vec_stmt,
1310 : gimple_stmt_iterator *gsi)
1311 : {
1312 1449133 : gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
1313 :
1314 1449133 : if (!gsi_end_p (*gsi)
1315 2897093 : && gimple_has_mem_ops (vec_stmt))
1316 : {
1317 1447960 : gimple *at_stmt = gsi_stmt (*gsi);
1318 1447960 : tree vuse = gimple_vuse (at_stmt);
1319 1441967 : if (vuse && TREE_CODE (vuse) == SSA_NAME)
1320 : {
1321 1298161 : tree vdef = gimple_vdef (at_stmt);
1322 1298161 : gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1323 1298161 : gimple_set_modified (vec_stmt, true);
1324 : /* If we have an SSA vuse and insert a store, update virtual
1325 : SSA form to avoid triggering the renamer. Do so only
1326 : if we can easily see all uses - which is what almost always
1327 : happens with the way vectorized stmts are inserted. */
1328 759933 : if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1329 2058058 : && ((is_gimple_assign (vec_stmt)
1330 759062 : && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1331 63823 : || (is_gimple_call (vec_stmt)
1332 835 : && (!(gimple_call_flags (vec_stmt)
1333 835 : & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
1334 1 : || (gimple_call_lhs (vec_stmt)
1335 1 : && !is_gimple_reg (gimple_call_lhs (vec_stmt)))))))
1336 : {
1337 696908 : tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1338 696908 : gimple_set_vdef (vec_stmt, new_vdef);
1339 696908 : SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1340 : }
1341 : }
1342 : }
1343 1449133 : gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1344 1449133 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1345 1449133 : }
1346 :
1347 : /* We want to vectorize a call to combined function CFN with function
1348 : decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1349 : as the types of all inputs. Check whether this is possible using
1350 : an internal function, returning its code if so or IFN_LAST if not. */
1351 :
1352 : static internal_fn
1353 16333 : vectorizable_internal_function (combined_fn cfn, tree fndecl,
1354 : tree vectype_out, tree vectype_in)
1355 : {
1356 16333 : internal_fn ifn;
1357 16333 : if (internal_fn_p (cfn))
1358 13807 : ifn = as_internal_fn (cfn);
1359 : else
1360 2526 : ifn = associated_internal_fn (fndecl);
1361 16333 : if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1362 : {
1363 12935 : const direct_internal_fn_info &info = direct_internal_fn (ifn);
1364 12935 : if (info.vectorizable)
1365 : {
1366 12935 : bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
1367 12935 : tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1368 12935 : tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
1369 :
1370 : /* The type size of both the vectype_in and vectype_out should be
1371 : exactly the same when vectype_out isn't participating the optab.
1372 : While there is no restriction for type size when vectype_out
1373 : is part of the optab query. */
1374 12935 : if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
1375 : return IFN_LAST;
1376 :
1377 12898 : if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1378 : OPTIMIZE_FOR_SPEED))
1379 : return ifn;
1380 : }
1381 : }
1382 : return IFN_LAST;
1383 : }
1384 :
1385 :
1386 : static tree permute_vec_elements (vec_info *, tree, tree, tree, stmt_vec_info,
1387 : gimple_stmt_iterator *);
1388 :
1389 : /* Check whether a load or store statement in the loop described by
1390 : LOOP_VINFO is possible in a loop using partial vectors. This is
1391 : testing whether the vectorizer pass has the appropriate support,
1392 : as well as whether the target does.
1393 :
1394 : VLS_TYPE says whether the statement is a load or store and VECTYPE
1395 : is the type of the vector being loaded or stored. SLP_NODE is the SLP
1396 : node that contains the statement, or null if none. MEMORY_ACCESS_TYPE
1397 : says how the load or store is going to be implemented and GROUP_SIZE
1398 : is the number of load or store statements in the containing group.
1399 : If the access is a gather load or scatter store, GS_INFO describes
1400 : its arguments. If the load or store is conditional, SCALAR_MASK is the
1401 : condition under which it occurs.
1402 :
1403 : Clear LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P if a loop using partial
1404 : vectors is not supported, otherwise record the required rgroup control
1405 : types.
1406 :
1407 : If partial vectors can be used and ELSVALS is nonzero the supported
1408 : else values will be added to the vector ELSVALS points to. */
1409 :
1410 : static void
1411 296176 : check_load_store_for_partial_vectors (loop_vec_info loop_vinfo, tree vectype,
1412 : slp_tree slp_node,
1413 : vec_load_store_type vls_type,
1414 : int group_size,
1415 : vect_load_store_data *ls,
1416 : slp_tree mask_node,
1417 : vec<int> *elsvals = nullptr)
1418 : {
1419 296176 : vect_memory_access_type memory_access_type = ls->memory_access_type;
1420 :
1421 : /* Invariant loads need no special support. */
1422 296176 : if (memory_access_type == VMAT_INVARIANT)
1423 29132 : return;
1424 :
1425 : /* Figure whether the mask is uniform. scalar_mask is used to
1426 : populate the scalar_cond_masked_set. */
1427 294993 : tree scalar_mask = NULL_TREE;
1428 294993 : if (mask_node)
1429 4906 : for (unsigned i = 0; i < SLP_TREE_LANES (mask_node); ++i)
1430 : {
1431 2484 : tree def = vect_get_slp_scalar_def (mask_node, i);
1432 2484 : if (!def
1433 2484 : || (scalar_mask && def != scalar_mask))
1434 : {
1435 : scalar_mask = NULL;
1436 : break;
1437 : }
1438 : else
1439 2463 : scalar_mask = def;
1440 : }
1441 :
1442 294993 : unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node);
1443 294993 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1444 294993 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
1445 294993 : machine_mode vecmode = TYPE_MODE (vectype);
1446 294993 : bool is_load = (vls_type == VLS_LOAD);
1447 294993 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
1448 : {
1449 0 : nvectors /= group_size;
1450 0 : internal_fn ifn
1451 0 : = (is_load ? vect_load_lanes_supported (vectype, group_size, true,
1452 : elsvals)
1453 0 : : vect_store_lanes_supported (vectype, group_size, true));
1454 0 : if (ifn == IFN_MASK_LEN_LOAD_LANES || ifn == IFN_MASK_LEN_STORE_LANES)
1455 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1456 0 : else if (ifn == IFN_MASK_LOAD_LANES || ifn == IFN_MASK_STORE_LANES)
1457 0 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1458 : scalar_mask);
1459 : else
1460 : {
1461 0 : if (dump_enabled_p ())
1462 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1463 : "can't operate on partial vectors because"
1464 : " the target doesn't have an appropriate"
1465 : " load/store-lanes instruction.\n");
1466 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1467 : }
1468 0 : return;
1469 : }
1470 :
1471 294993 : if (mat_gather_scatter_p (memory_access_type))
1472 : {
1473 1743 : internal_fn ifn = (is_load
1474 1743 : ? IFN_MASK_GATHER_LOAD
1475 : : IFN_MASK_SCATTER_STORE);
1476 427 : internal_fn len_ifn = (is_load
1477 : ? IFN_MASK_LEN_GATHER_LOAD
1478 : : IFN_MASK_LEN_SCATTER_STORE);
1479 1743 : stmt_vec_info repr = SLP_TREE_REPRESENTATIVE (slp_node);
1480 1743 : tree off_vectype = (STMT_VINFO_GATHER_SCATTER_P (repr)
1481 1743 : ? SLP_TREE_VECTYPE (SLP_TREE_CHILDREN (slp_node)[0])
1482 1743 : : ls->strided_offset_vectype);
1483 1743 : tree memory_type = TREE_TYPE (DR_REF (STMT_VINFO_DR_INFO (repr)->dr));
1484 1743 : int scale = SLP_TREE_GS_SCALE (slp_node);
1485 :
1486 : /* The following "supported" checks just verify what we established in
1487 : get_load_store_type and don't try different offset types.
1488 : Therefore, off_vectype must be a supported offset type. In case
1489 : we chose a different one use this instead. */
1490 1743 : if (ls->supported_offset_vectype)
1491 0 : off_vectype = ls->supported_offset_vectype;
1492 : /* Same for scale. */
1493 1743 : if (ls->supported_scale)
1494 0 : scale = ls->supported_scale;
1495 :
1496 1743 : if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
1497 : memory_type,
1498 : off_vectype, scale,
1499 : elsvals))
1500 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1501 1743 : else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
1502 : memory_type,
1503 : off_vectype, scale,
1504 : elsvals)
1505 1743 : || memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
1506 572 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1507 : scalar_mask);
1508 : else
1509 : {
1510 1171 : if (dump_enabled_p ())
1511 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1512 : "can't operate on partial vectors because"
1513 : " the target doesn't have an appropriate"
1514 : " gather load or scatter store instruction.\n");
1515 1171 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1516 : }
1517 1743 : return;
1518 : }
1519 :
1520 293250 : if (memory_access_type != VMAT_CONTIGUOUS)
1521 : {
1522 : /* Element X of the data must come from iteration i * VF + X of the
1523 : scalar loop. We need more work to support other mappings. */
1524 26206 : if (dump_enabled_p ())
1525 726 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1526 : "can't operate on partial vectors because an"
1527 : " access isn't contiguous.\n");
1528 26206 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1529 26206 : return;
1530 : }
1531 :
1532 267044 : if (!VECTOR_MODE_P (vecmode))
1533 : {
1534 0 : if (dump_enabled_p ())
1535 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1536 : "can't operate on partial vectors when emulating"
1537 : " vector operations.\n");
1538 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1539 0 : return;
1540 : }
1541 :
1542 : /* We might load more scalars than we need for permuting SLP loads.
1543 : We checked in get_load_store_type that the extra elements
1544 : don't leak into a new vector. */
1545 357737 : auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
1546 : {
1547 90693 : unsigned int nvectors;
1548 90693 : if (can_div_away_from_zero_p (size, nunits, &nvectors))
1549 90693 : return nvectors;
1550 : gcc_unreachable ();
1551 : };
1552 :
1553 267044 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1554 267044 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1555 267044 : machine_mode mask_mode;
1556 267044 : machine_mode vmode;
1557 267044 : bool using_partial_vectors_p = false;
1558 267044 : if (get_len_load_store_mode
1559 267044 : (vecmode, is_load, nullptr, elsvals).exists (&vmode))
1560 : {
1561 0 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1562 0 : unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
1563 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
1564 0 : using_partial_vectors_p = true;
1565 : }
1566 357737 : else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
1567 267044 : && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
1568 : elsvals))
1569 : {
1570 90693 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1571 90693 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
1572 90693 : using_partial_vectors_p = true;
1573 : }
1574 :
1575 90693 : if (!using_partial_vectors_p)
1576 : {
1577 176351 : if (dump_enabled_p ())
1578 11900 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1579 : "can't operate on partial vectors because the"
1580 : " target doesn't have the appropriate partial"
1581 : " vectorization load or store.\n");
1582 176351 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1583 : }
1584 : }
1585 :
1586 : /* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1587 : form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1588 : that needs to be applied to all loads and stores in a vectorized loop.
1589 : Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
1590 : otherwise return VEC_MASK & LOOP_MASK.
1591 :
1592 : MASK_TYPE is the type of both masks. If new statements are needed,
1593 : insert them before GSI. */
1594 :
1595 : tree
1596 1611 : prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
1597 : tree vec_mask, gimple_stmt_iterator *gsi)
1598 : {
1599 1611 : gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1600 1611 : if (!loop_mask)
1601 : return vec_mask;
1602 :
1603 141 : gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1604 :
1605 141 : if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
1606 : return vec_mask;
1607 :
1608 141 : tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1609 141 : gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1610 : vec_mask, loop_mask);
1611 :
1612 141 : gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1613 141 : return and_res;
1614 : }
1615 :
1616 : /* Determine whether we can use a gather load or scatter store to vectorize
1617 : strided load or store STMT_INFO by truncating the current offset to a
1618 : smaller width. We need to be able to construct an offset vector:
1619 :
1620 : { 0, X, X*2, X*3, ... }
1621 :
1622 : without loss of precision, where X is STMT_INFO's DR_STEP.
1623 :
1624 : Return true if this is possible, describing the gather load or scatter
1625 : store in GS_INFO. MASKED_P is true if the load or store is conditional.
1626 :
1627 : If we can use gather/scatter and ELSVALS is nonzero the supported
1628 : else values will be stored in the vector ELSVALS points to. */
1629 :
1630 : static bool
1631 63890 : vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info, tree vectype,
1632 : loop_vec_info loop_vinfo, bool masked_p,
1633 : gather_scatter_info *gs_info,
1634 : vec<int> *elsvals)
1635 : {
1636 63890 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1637 63890 : data_reference *dr = dr_info->dr;
1638 63890 : tree step = DR_STEP (dr);
1639 63890 : if (TREE_CODE (step) != INTEGER_CST)
1640 : {
1641 : /* ??? Perhaps we could use range information here? */
1642 28589 : if (dump_enabled_p ())
1643 201 : dump_printf_loc (MSG_NOTE, vect_location,
1644 : "cannot truncate variable step.\n");
1645 28589 : return false;
1646 : }
1647 :
1648 : /* Get the number of bits in an element. */
1649 35301 : scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
1650 35301 : unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
1651 :
1652 : /* Set COUNT to the upper limit on the number of elements - 1.
1653 : Start with the maximum vectorization factor. */
1654 35301 : unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
1655 :
1656 : /* Try lowering COUNT to the number of scalar latch iterations. */
1657 35301 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1658 35301 : widest_int max_iters;
1659 35301 : if (max_loop_iterations (loop, &max_iters)
1660 69872 : && max_iters < count)
1661 2121 : count = max_iters.to_shwi ();
1662 :
1663 : /* Try scales of 1 and the element size. */
1664 35301 : unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
1665 35301 : wi::overflow_type overflow = wi::OVF_NONE;
1666 105903 : for (int i = 0; i < 2; ++i)
1667 : {
1668 70602 : unsigned int scale = scales[i];
1669 70602 : widest_int factor;
1670 70602 : if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
1671 0 : continue;
1672 :
1673 : /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE. */
1674 70602 : widest_int range = wi::mul (count, factor, SIGNED, &overflow);
1675 70602 : if (overflow)
1676 0 : continue;
1677 70602 : signop sign = range >= 0 ? UNSIGNED : SIGNED;
1678 70602 : unsigned int min_offset_bits = wi::min_precision (range, sign);
1679 :
1680 : /* Find the narrowest viable offset type. */
1681 70602 : unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
1682 70602 : tree offset_type = build_nonstandard_integer_type (offset_bits,
1683 : sign == UNSIGNED);
1684 :
1685 : /* See whether the target supports the operation with an offset
1686 : no narrower than OFFSET_TYPE. */
1687 70602 : tree memory_type = TREE_TYPE (DR_REF (dr));
1688 70602 : tree tmp_offset_vectype;
1689 70602 : int tmp_scale;
1690 70602 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
1691 : vectype, memory_type, offset_type,
1692 : scale, &tmp_scale,
1693 : &gs_info->ifn, &gs_info->offset_vectype,
1694 : &tmp_offset_vectype, elsvals)
1695 70602 : || gs_info->ifn == IFN_LAST)
1696 70602 : continue;
1697 :
1698 0 : gs_info->decl = NULL_TREE;
1699 : /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
1700 : but we don't need to store that here. */
1701 0 : gs_info->base = NULL_TREE;
1702 0 : gs_info->alias_ptr = build_int_cst
1703 0 : (reference_alias_ptr_type (DR_REF (dr)),
1704 0 : get_object_alignment (DR_REF (dr)));
1705 0 : gs_info->element_type = TREE_TYPE (vectype);
1706 0 : gs_info->offset = fold_convert (offset_type, step);
1707 0 : gs_info->scale = scale;
1708 0 : gs_info->memory_type = memory_type;
1709 0 : return true;
1710 141204 : }
1711 :
1712 35301 : if (overflow && dump_enabled_p ())
1713 0 : dump_printf_loc (MSG_NOTE, vect_location,
1714 : "truncating gather/scatter offset to %d bits"
1715 : " might change its value.\n", element_bits);
1716 :
1717 : return false;
1718 35301 : }
1719 :
1720 : /* Return true if we can use gather/scatter or strided internal functions
1721 : to vectorize STMT_INFO, which is a grouped or strided load or store
1722 : with multiple lanes and will be implemented by a type-punned access
1723 : of a vector with element size that matches the number of lanes.
1724 :
1725 : MASKED_P is true if load or store is conditional.
1726 : When returning true, fill in GS_INFO with the information required to
1727 : perform the operation. Also, store the punning type in PUNNED_VECTYPE.
1728 :
1729 : If successful and ELSVALS is nonzero the supported
1730 : else values will be stored in the vector ELSVALS points to. */
1731 :
1732 : static bool
1733 4613 : vect_use_grouped_gather (dr_vec_info *dr_info, tree vectype,
1734 : loop_vec_info loop_vinfo, bool masked_p,
1735 : unsigned int nelts,
1736 : gather_scatter_info *info, vec<int> *elsvals,
1737 : tree *pun_vectype)
1738 : {
1739 4613 : data_reference *dr = dr_info->dr;
1740 :
1741 : /* TODO: We can support nelts > BITS_PER_UNIT or non-power-of-two by
1742 : multiple gathers/scatter. */
1743 8899 : if (nelts > BITS_PER_UNIT || !pow2p_hwi (nelts))
1744 : return false;
1745 :
1746 : /* Pun the vectype with one of the same size but an element spanning
1747 : NELTS elements of VECTYPE.
1748 : The punned type of a V16QI with NELTS = 4 would be V4SI.
1749 : */
1750 3975 : tree tmp;
1751 3975 : unsigned int pieces;
1752 3975 : if (!can_div_trunc_p (TYPE_VECTOR_SUBPARTS (vectype), nelts, &pieces)
1753 3975 : || pieces <= 1)
1754 1987 : return false;
1755 :
1756 1988 : *pun_vectype = vector_vector_composition_type (vectype, pieces, &tmp, true);
1757 :
1758 1988 : if (!*pun_vectype || !VECTOR_TYPE_P (*pun_vectype))
1759 : return false;
1760 :
1761 1842 : internal_fn ifn;
1762 1842 : tree offset_vectype = *pun_vectype;
1763 :
1764 1261 : internal_fn strided_ifn = DR_IS_READ (dr)
1765 1842 : ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
1766 :
1767 : /* Check if we have a gather/scatter with the new type. We're just trying
1768 : with the type itself as offset for now. If not, check if we have a
1769 : strided load/store. These have fewer constraints (for example no offset
1770 : type must exist) so it is possible that even though a gather/scatter is
1771 : not available we still have a strided load/store. */
1772 1842 : bool ok = false;
1773 1842 : tree tmp_vectype;
1774 1842 : int tmp_scale;
1775 1842 : if (vect_gather_scatter_fn_p
1776 1842 : (loop_vinfo, DR_IS_READ (dr), masked_p, *pun_vectype,
1777 1842 : TREE_TYPE (*pun_vectype), *pun_vectype, 1, &tmp_scale, &ifn,
1778 : &offset_vectype, &tmp_vectype, elsvals))
1779 : ok = true;
1780 1842 : else if (internal_strided_fn_supported_p (strided_ifn, *pun_vectype,
1781 : elsvals))
1782 : {
1783 : /* Use gather/scatter IFNs, vect_get_strided_load_store_ops
1784 : will switch back to the strided variants. */
1785 0 : ifn = DR_IS_READ (dr) ? IFN_MASK_LEN_GATHER_LOAD :
1786 : IFN_MASK_LEN_SCATTER_STORE;
1787 0 : ok = true;
1788 : }
1789 :
1790 0 : if (ok)
1791 : {
1792 0 : info->ifn = ifn;
1793 0 : info->decl = NULL_TREE;
1794 0 : info->base = dr->ref;
1795 0 : info->alias_ptr = build_int_cst
1796 0 : (reference_alias_ptr_type (DR_REF (dr)),
1797 0 : get_object_alignment (DR_REF (dr)));
1798 0 : info->element_type = TREE_TYPE (*pun_vectype);
1799 0 : info->offset_vectype = offset_vectype;
1800 : /* No need to set the offset, vect_get_strided_load_store_ops
1801 : will do that. */
1802 0 : info->scale = 1;
1803 0 : info->memory_type = TREE_TYPE (DR_REF (dr));
1804 0 : return true;
1805 : }
1806 :
1807 : return false;
1808 : }
1809 :
1810 :
1811 : /* Return true if we can use gather/scatter internal functions to
1812 : vectorize STMT_INFO, which is a grouped or strided load or store.
1813 : MASKED_P is true if load or store is conditional. When returning
1814 : true, fill in GS_INFO with the information required to perform the
1815 : operation.
1816 :
1817 : If we can use gather/scatter and ELSVALS is nonzero the supported
1818 : else values will be stored in the vector ELSVALS points to. */
1819 :
1820 : static bool
1821 63890 : vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info, tree vectype,
1822 : loop_vec_info loop_vinfo, bool masked_p,
1823 : gather_scatter_info *gs_info,
1824 : vec<int> *elsvals,
1825 : unsigned int group_size,
1826 : bool single_element_p)
1827 : {
1828 63890 : if (!vect_check_gather_scatter (stmt_info, vectype,
1829 : loop_vinfo, gs_info, elsvals)
1830 63890 : || gs_info->ifn == IFN_LAST)
1831 : {
1832 63890 : if (!vect_truncate_gather_scatter_offset (stmt_info, vectype, loop_vinfo,
1833 : masked_p, gs_info, elsvals))
1834 : return false;
1835 : }
1836 :
1837 0 : if (!single_element_p
1838 0 : && !targetm.vectorize.prefer_gather_scatter (TYPE_MODE (vectype),
1839 : gs_info->scale,
1840 : group_size))
1841 : return false;
1842 :
1843 0 : if (dump_enabled_p ())
1844 0 : dump_printf_loc (MSG_NOTE, vect_location,
1845 : "using gather/scatter for strided/grouped access,"
1846 : " scale = %d\n", gs_info->scale);
1847 :
1848 : return true;
1849 : }
1850 :
1851 : /* STMT_INFO is a non-strided load or store, meaning that it accesses
1852 : elements with a known constant step. Return -1 if that step
1853 : is negative, 0 if it is zero, and 1 if it is greater than zero. */
1854 :
1855 : int
1856 1511721 : compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
1857 : {
1858 1511721 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1859 1511721 : return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
1860 1511721 : size_zero_node);
1861 : }
1862 :
1863 : /* If the target supports a permute mask that reverses the elements in
1864 : a vector of type VECTYPE, return that mask, otherwise return null. */
1865 :
1866 : tree
1867 9166 : perm_mask_for_reverse (tree vectype)
1868 : {
1869 9166 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1870 :
1871 : /* The encoding has a single stepped pattern. */
1872 9166 : vec_perm_builder sel (nunits, 1, 3);
1873 36664 : for (int i = 0; i < 3; ++i)
1874 27498 : sel.quick_push (nunits - 1 - i);
1875 :
1876 9166 : vec_perm_indices indices (sel, 1, nunits);
1877 9166 : if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
1878 : indices))
1879 : return NULL_TREE;
1880 8006 : return vect_gen_perm_mask_checked (vectype, indices);
1881 9166 : }
1882 :
1883 : /* A subroutine of get_load_store_type, with a subset of the same
1884 : arguments. Handle the case where STMT_INFO is a load or store that
1885 : accesses consecutive elements with a negative step. Sets *POFFSET
1886 : to the offset to be applied to the DR for the first access. */
1887 :
1888 : static vect_memory_access_type
1889 12209 : get_negative_load_store_type (vec_info *vinfo,
1890 : stmt_vec_info stmt_info, tree vectype,
1891 : vec_load_store_type vls_type,
1892 : unsigned int ncopies, poly_int64 *poffset)
1893 : {
1894 12209 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1895 12209 : dr_alignment_support alignment_support_scheme;
1896 :
1897 12209 : if (ncopies > 1)
1898 : {
1899 0 : if (dump_enabled_p ())
1900 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1901 : "multiple types with negative step.\n");
1902 0 : return VMAT_ELEMENTWISE;
1903 : }
1904 :
1905 : /* For backward running DRs the first access in vectype actually is
1906 : N-1 elements before the address of the DR. */
1907 12209 : *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
1908 12209 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1909 :
1910 12209 : int misalignment = dr_misalignment (dr_info, vectype, *poffset);
1911 12209 : alignment_support_scheme
1912 12209 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
1913 12209 : if (alignment_support_scheme != dr_aligned
1914 12209 : && alignment_support_scheme != dr_unaligned_supported)
1915 : {
1916 4428 : if (dump_enabled_p ())
1917 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1918 : "negative step but alignment required.\n");
1919 4428 : *poffset = 0;
1920 4428 : return VMAT_ELEMENTWISE;
1921 : }
1922 :
1923 7781 : if (vls_type == VLS_STORE_INVARIANT)
1924 : {
1925 1197 : if (dump_enabled_p ())
1926 21 : dump_printf_loc (MSG_NOTE, vect_location,
1927 : "negative step with invariant source;"
1928 : " no permute needed.\n");
1929 1197 : return VMAT_CONTIGUOUS_DOWN;
1930 : }
1931 :
1932 6584 : if (!perm_mask_for_reverse (vectype))
1933 : {
1934 1160 : if (dump_enabled_p ())
1935 52 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1936 : "negative step and reversing not supported.\n");
1937 1160 : *poffset = 0;
1938 1160 : return VMAT_ELEMENTWISE;
1939 : }
1940 :
1941 : return VMAT_CONTIGUOUS_REVERSE;
1942 : }
1943 :
1944 : /* STMT_INFO is either a masked or unconditional store. Return the value
1945 : being stored. */
1946 :
1947 : tree
1948 0 : vect_get_store_rhs (stmt_vec_info stmt_info)
1949 : {
1950 0 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
1951 : {
1952 0 : gcc_assert (gimple_assign_single_p (assign));
1953 0 : return gimple_assign_rhs1 (assign);
1954 : }
1955 0 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
1956 : {
1957 0 : internal_fn ifn = gimple_call_internal_fn (call);
1958 0 : int index = internal_fn_stored_value_index (ifn);
1959 0 : gcc_assert (index >= 0);
1960 0 : return gimple_call_arg (call, index);
1961 : }
1962 0 : gcc_unreachable ();
1963 : }
1964 :
1965 : /* Function VECTOR_VECTOR_COMPOSITION_TYPE
1966 :
1967 : This function returns a vector type which can be composed with NELTS pieces,
1968 : whose type is recorded in PTYPE. VTYPE should be a vector type, and has the
1969 : same vector size as the return vector. It checks target whether supports
1970 : pieces-size vector mode for construction firstly, if target fails to, check
1971 : pieces-size scalar mode for construction further. It returns NULL_TREE if
1972 : fails to find the available composition. If the caller only wants scalar
1973 : pieces where PTYPE e.g. is a possible gather/scatter element type
1974 : SCALAR_PTYPE_ONLY must be true.
1975 :
1976 : For example, for (vtype=V16QI, nelts=4), we can probably get:
1977 : - V16QI with PTYPE V4QI.
1978 : - V4SI with PTYPE SI.
1979 : - NULL_TREE. */
1980 :
1981 : static tree
1982 12651 : vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype,
1983 : bool scalar_ptype_only)
1984 : {
1985 12651 : gcc_assert (VECTOR_TYPE_P (vtype));
1986 12651 : gcc_assert (known_gt (nelts, 0U));
1987 :
1988 12651 : machine_mode vmode = TYPE_MODE (vtype);
1989 12651 : if (!VECTOR_MODE_P (vmode))
1990 : return NULL_TREE;
1991 :
1992 : /* When we are asked to compose the vector from its components let
1993 : that happen directly. */
1994 12651 : if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
1995 : {
1996 6068 : *ptype = TREE_TYPE (vtype);
1997 6068 : return vtype;
1998 : }
1999 :
2000 13166 : poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
2001 6583 : unsigned int pbsize;
2002 6583 : if (constant_multiple_p (vbsize, nelts, &pbsize))
2003 : {
2004 : /* First check if vec_init optab supports construction from
2005 : vector pieces directly. */
2006 6583 : scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
2007 13166 : poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
2008 6583 : machine_mode rmode;
2009 6583 : if (!scalar_ptype_only
2010 4595 : && related_vector_mode (vmode, elmode, inelts).exists (&rmode)
2011 10716 : && (convert_optab_handler (vec_init_optab, vmode, rmode)
2012 : != CODE_FOR_nothing))
2013 : {
2014 3493 : *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
2015 3493 : return vtype;
2016 : }
2017 :
2018 : /* Otherwise check if exists an integer type of the same piece size and
2019 : if vec_init optab supports construction from it directly. */
2020 3090 : if (int_mode_for_size (pbsize, 0).exists (&elmode)
2021 3090 : && related_vector_mode (vmode, elmode, nelts).exists (&rmode))
2022 : {
2023 2906 : if (scalar_ptype_only
2024 2906 : || convert_optab_handler (vec_init_optab, rmode, elmode)
2025 : != CODE_FOR_nothing)
2026 : {
2027 2906 : *ptype = build_nonstandard_integer_type (pbsize, 1);
2028 2906 : return build_vector_type (*ptype, nelts);
2029 : }
2030 : }
2031 : }
2032 :
2033 : return NULL_TREE;
2034 : }
2035 :
2036 : /* Check if the load permutation of NODE only refers to a consecutive
2037 : subset of the group indices where GROUP_SIZE is the size of the
2038 : dataref's group. We also assert that the length of the permutation
2039 : divides the group size and is a power of two.
2040 : Such load permutations can be elided in strided access schemes as
2041 : we can "jump over" the gap they leave. */
2042 :
2043 : bool
2044 45079 : has_consecutive_load_permutation (slp_tree node, unsigned group_size)
2045 : {
2046 45079 : load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION (node);
2047 45079 : if (!perm.exists ()
2048 2164 : || perm.length () <= 1
2049 496 : || !pow2p_hwi (perm.length ())
2050 45559 : || group_size % perm.length ())
2051 : return false;
2052 :
2053 433 : return vect_load_perm_consecutive_p (node);
2054 : }
2055 :
2056 :
2057 : /* Analyze load or store SLP_NODE of type VLS_TYPE. Return true
2058 : if there is a memory access type that the vectorized form can use,
2059 : storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2060 : or scatters, fill in GS_INFO accordingly. In addition
2061 : *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
2062 : the target does not support the alignment scheme. *MISALIGNMENT
2063 : is set according to the alignment of the access (including
2064 : DR_MISALIGNMENT_UNKNOWN when it is unknown).
2065 :
2066 : MASKED_P is true if the statement is conditional on a vectorized mask.
2067 : VECTYPE is the vector type that the vectorized statements will use.
2068 :
2069 : If ELSVALS is nonzero the supported else values will be stored in the
2070 : vector ELSVALS points to. */
2071 :
2072 : static bool
2073 1396562 : get_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2074 : tree vectype, slp_tree slp_node,
2075 : bool masked_p, vec_load_store_type vls_type,
2076 : vect_load_store_data *ls)
2077 : {
2078 1396562 : vect_memory_access_type *memory_access_type = &ls->memory_access_type;
2079 1396562 : poly_int64 *poffset = &ls->poffset;
2080 1396562 : dr_alignment_support *alignment_support_scheme
2081 : = &ls->alignment_support_scheme;
2082 1396562 : int *misalignment = &ls->misalignment;
2083 1396562 : internal_fn *lanes_ifn = &ls->lanes_ifn;
2084 1396562 : vec<int> *elsvals = &ls->elsvals;
2085 1396562 : tree *ls_type = &ls->ls_type;
2086 1396562 : bool *slp_perm = &ls->slp_perm;
2087 1396562 : unsigned *n_perms = &ls->n_perms;
2088 1396562 : unsigned *n_loads = &ls->n_loads;
2089 1396562 : tree *supported_offset_vectype = &ls->supported_offset_vectype;
2090 1396562 : int *supported_scale = &ls->supported_scale;
2091 1396562 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2092 1396562 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2093 1396562 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2094 1396562 : stmt_vec_info first_stmt_info;
2095 1396562 : unsigned int group_size;
2096 1396562 : unsigned HOST_WIDE_INT gap;
2097 1396562 : bool single_element_p;
2098 1396562 : poly_int64 neg_ldst_offset = 0;
2099 :
2100 1396562 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2101 1396562 : *poffset = 0;
2102 1396562 : *ls_type = NULL_TREE;
2103 1396562 : *slp_perm = false;
2104 1396562 : *n_perms = -1U;
2105 1396562 : *n_loads = -1U;
2106 1396562 : ls->subchain_p = false;
2107 :
2108 1396562 : bool perm_ok = true;
2109 1396562 : poly_int64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
2110 :
2111 1396562 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2112 80505 : perm_ok = vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL,
2113 80505 : vf, true, n_perms, n_loads);
2114 :
2115 1396562 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2116 : {
2117 894852 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2118 894852 : group_size = DR_GROUP_SIZE (first_stmt_info);
2119 894852 : gap = DR_GROUP_GAP (first_stmt_info);
2120 894852 : single_element_p = (stmt_info == first_stmt_info
2121 894852 : && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2122 : }
2123 : else
2124 : {
2125 : first_stmt_info = stmt_info;
2126 : group_size = 1;
2127 : gap = 0;
2128 : single_element_p = true;
2129 : }
2130 1396562 : dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
2131 :
2132 : /* True if the vectorized statements would access beyond the last
2133 : statement in the group. */
2134 1396562 : bool overrun_p = false;
2135 :
2136 : /* True if we can cope with such overrun by peeling for gaps, so that
2137 : there is at least one final scalar iteration after the vector loop. */
2138 2793124 : bool can_overrun_p = (!masked_p
2139 1396562 : && vls_type == VLS_LOAD
2140 567110 : && loop_vinfo
2141 1822054 : && !loop->inner);
2142 :
2143 : /* There can only be a gap at the end of the group if the stride is
2144 : known at compile time. */
2145 1396562 : gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
2146 :
2147 : /* For SLP vectorization we directly vectorize a subchain
2148 : without permutation. */
2149 1396562 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2150 1316057 : first_dr_info = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
2151 :
2152 1396562 : if (STMT_VINFO_STRIDED_P (first_stmt_info))
2153 : {
2154 : /* Try to use consecutive accesses of as many elements as possible,
2155 : separated by the stride, until we have a complete vector.
2156 : Fall back to scalar accesses if that isn't possible. */
2157 45079 : *memory_access_type = VMAT_STRIDED_SLP;
2158 :
2159 : /* If the load permutation is consecutive we can reduce the group to
2160 : the elements the permutation accesses. Then we release the
2161 : permutation. */
2162 45079 : if (has_consecutive_load_permutation (slp_node, group_size))
2163 : {
2164 32 : ls->subchain_p = true;
2165 32 : group_size = SLP_TREE_LANES (slp_node);
2166 32 : SLP_TREE_LOAD_PERMUTATION (slp_node).release ();
2167 : }
2168 : }
2169 1351483 : else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2170 : {
2171 10900 : slp_tree offset_node = SLP_TREE_CHILDREN (slp_node)[0];
2172 10900 : tree offset_vectype = SLP_TREE_VECTYPE (offset_node);
2173 10900 : int scale = SLP_TREE_GS_SCALE (slp_node);
2174 10900 : tree memory_type = TREE_TYPE (DR_REF (first_dr_info->dr));
2175 10900 : tree tem;
2176 10900 : if (vect_gather_scatter_fn_p (loop_vinfo, vls_type == VLS_LOAD,
2177 : masked_p, vectype, memory_type,
2178 : offset_vectype, scale, supported_scale,
2179 : &ls->gs.ifn, &tem,
2180 : supported_offset_vectype, elsvals))
2181 : {
2182 0 : if (dump_enabled_p ())
2183 : {
2184 0 : dump_printf_loc (MSG_NOTE, vect_location,
2185 : "gather/scatter with required "
2186 : "offset type "
2187 : "%T and offset scale %d.\n",
2188 : offset_vectype, scale);
2189 0 : if (*supported_offset_vectype)
2190 0 : dump_printf_loc (MSG_NOTE, vect_location,
2191 : " target supports offset type %T.\n",
2192 : *supported_offset_vectype);
2193 0 : if (*supported_scale)
2194 0 : dump_printf_loc (MSG_NOTE, vect_location,
2195 : " target supports offset scale %d.\n",
2196 : *supported_scale);
2197 : }
2198 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2199 : }
2200 10900 : else if (vls_type == VLS_LOAD
2201 10900 : ? (targetm.vectorize.builtin_gather
2202 9301 : && (ls->gs.decl
2203 9301 : = targetm.vectorize.builtin_gather (vectype,
2204 9301 : TREE_TYPE
2205 : (offset_vectype),
2206 : scale)))
2207 1599 : : (targetm.vectorize.builtin_scatter
2208 1599 : && (ls->gs.decl
2209 1599 : = targetm.vectorize.builtin_scatter (vectype,
2210 1599 : TREE_TYPE
2211 : (offset_vectype),
2212 : scale))))
2213 580 : *memory_access_type = VMAT_GATHER_SCATTER_LEGACY;
2214 : else
2215 : {
2216 : /* GATHER_SCATTER_EMULATED_P. */
2217 10320 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
2218 10320 : || !TYPE_VECTOR_SUBPARTS (offset_vectype).is_constant ()
2219 10320 : || VECTOR_BOOLEAN_TYPE_P (offset_vectype)
2220 10320 : || !constant_multiple_p (TYPE_VECTOR_SUBPARTS (offset_vectype),
2221 10320 : TYPE_VECTOR_SUBPARTS (vectype)))
2222 : {
2223 2740 : if (dump_enabled_p ())
2224 466 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2225 : "unsupported vector types for emulated "
2226 : "gather.\n");
2227 2740 : return false;
2228 : }
2229 7580 : *memory_access_type = VMAT_GATHER_SCATTER_EMULATED;
2230 : }
2231 : }
2232 : else
2233 : {
2234 1340583 : int cmp = compare_step_with_zero (vinfo, stmt_info);
2235 1340583 : if (cmp < 0)
2236 : {
2237 12387 : if (single_element_p)
2238 : /* ??? The VMAT_CONTIGUOUS_REVERSE code generation is
2239 : only correct for single element "interleaving" SLP. */
2240 12209 : *memory_access_type = get_negative_load_store_type
2241 12209 : (vinfo, stmt_info, vectype, vls_type, 1,
2242 : &neg_ldst_offset);
2243 : else
2244 : /* We can fall back to VMAT_STRIDED_SLP since that does
2245 : not care whether the stride between the group instances
2246 : is positive or negative. */
2247 178 : *memory_access_type = VMAT_STRIDED_SLP;
2248 : }
2249 1328196 : else if (cmp == 0 && loop_vinfo)
2250 : {
2251 3367 : gcc_assert (vls_type == VLS_LOAD);
2252 3367 : *memory_access_type = VMAT_INVARIANT;
2253 : }
2254 : /* Try using LOAD/STORE_LANES. */
2255 1324829 : else if (slp_node->ldst_lanes
2256 1324829 : && (*lanes_ifn
2257 0 : = (vls_type == VLS_LOAD
2258 0 : ? vect_load_lanes_supported (vectype, group_size,
2259 : masked_p, elsvals)
2260 0 : : vect_store_lanes_supported (vectype, group_size,
2261 : masked_p))) != IFN_LAST)
2262 0 : *memory_access_type = VMAT_LOAD_STORE_LANES;
2263 1324829 : else if (!loop_vinfo && slp_node->avoid_stlf_fail)
2264 : {
2265 70 : *memory_access_type = VMAT_ELEMENTWISE;
2266 70 : if (dump_enabled_p ())
2267 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2268 : "using element-wise load to avoid disrupting "
2269 : "cross iteration store-to-load forwarding\n");
2270 : }
2271 : else
2272 1324759 : *memory_access_type = VMAT_CONTIGUOUS;
2273 :
2274 : /* If this is single-element interleaving with an element
2275 : distance that leaves unused vector loads around fall back
2276 : to elementwise access if possible - we otherwise least
2277 : create very sub-optimal code in that case (and
2278 : blow up memory, see PR65518). */
2279 1340583 : if (loop_vinfo
2280 1340583 : && single_element_p
2281 482266 : && (*memory_access_type == VMAT_CONTIGUOUS
2282 15576 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2283 1822849 : && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
2284 : {
2285 17805 : *memory_access_type = VMAT_ELEMENTWISE;
2286 17805 : if (dump_enabled_p ())
2287 198 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2288 : "single-element interleaving not supported "
2289 : "for not adjacent vector loads, using "
2290 : "elementwise access\n");
2291 : }
2292 :
2293 : /* Also fall back to elementwise access in case we did not lower a
2294 : permutation and cannot code generate it. */
2295 1340583 : if (loop_vinfo
2296 536822 : && *memory_access_type != VMAT_ELEMENTWISE
2297 513429 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2298 1369161 : && !perm_ok)
2299 : {
2300 2080 : *memory_access_type = VMAT_ELEMENTWISE;
2301 2080 : if (dump_enabled_p ())
2302 248 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2303 : "permutation not supported, using elementwise "
2304 : "access\n");
2305 : }
2306 :
2307 536822 : overrun_p = (loop_vinfo && gap != 0
2308 1383512 : && *memory_access_type != VMAT_ELEMENTWISE);
2309 1340583 : if (overrun_p && vls_type != VLS_LOAD)
2310 : {
2311 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2312 : "Grouped store with gaps requires"
2313 : " non-consecutive accesses\n");
2314 9 : return false;
2315 : }
2316 :
2317 1340583 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (first_dr_info);
2318 1340583 : poly_int64 off = 0;
2319 1340583 : if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2320 5265 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2321 :
2322 : /* An overrun is fine if the trailing elements are smaller
2323 : than the alignment boundary B. Every vector access will
2324 : be a multiple of B and so we are guaranteed to access a
2325 : non-gap element in the same B-sized block. */
2326 1340583 : if (overrun_p
2327 1340583 : && gap < (vect_known_alignment_in_bytes (first_dr_info,
2328 22959 : vectype, off) / dr_size))
2329 : overrun_p = false;
2330 :
2331 : /* When we have a contiguous access across loop iterations
2332 : but the access in the loop doesn't cover the full vector
2333 : we can end up with no gap recorded but still excess
2334 : elements accessed, see PR103116. Make sure we peel for
2335 : gaps if necessary and sufficient and give up if not.
2336 :
2337 : If there is a combination of the access not covering the full
2338 : vector and a gap recorded then we may need to peel twice. */
2339 1340583 : bool large_vector_overrun_p = false;
2340 1340583 : if (loop_vinfo
2341 536822 : && (*memory_access_type == VMAT_CONTIGUOUS
2342 35475 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2343 506612 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2344 1366702 : && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
2345 : nunits))
2346 : large_vector_overrun_p = overrun_p = true;
2347 :
2348 : /* If the gap splits the vector in half and the target
2349 : can do half-vector operations avoid the epilogue peeling
2350 : by simply loading half of the vector only. Usually
2351 : the construction with an upper zero half will be elided. */
2352 1340583 : dr_alignment_support alss;
2353 1340583 : int misalign = dr_misalignment (first_dr_info, vectype, off);
2354 1340583 : tree half_vtype;
2355 1340583 : poly_uint64 remain;
2356 1340583 : unsigned HOST_WIDE_INT tem, num;
2357 1340583 : if (overrun_p
2358 1340583 : && !masked_p
2359 17511 : && *memory_access_type != VMAT_LOAD_STORE_LANES
2360 17511 : && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
2361 : vectype, misalign)))
2362 : == dr_aligned
2363 15011 : || alss == dr_unaligned_supported)
2364 9897 : && can_div_trunc_p (group_size
2365 9897 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2366 : nunits, &tem, &remain)
2367 1350480 : && (known_eq (remain, 0u)
2368 7418 : || (known_ne (remain, 0u)
2369 5747 : && constant_multiple_p (nunits, remain, &num)
2370 1338104 : && (vector_vector_composition_type (vectype, num, &half_vtype)
2371 : != NULL_TREE))))
2372 8226 : overrun_p = false;
2373 :
2374 1340583 : if (overrun_p && !can_overrun_p)
2375 : {
2376 6 : if (dump_enabled_p ())
2377 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2378 : "Peeling for outer loop is not supported\n");
2379 6 : return false;
2380 : }
2381 :
2382 : /* Peeling for gaps assumes that a single scalar iteration
2383 : is enough to make sure the last vector iteration doesn't
2384 : access excess elements. */
2385 1340577 : if (overrun_p
2386 1340577 : && (!can_div_trunc_p (group_size
2387 9279 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2388 : nunits, &tem, &remain)
2389 9279 : || maybe_lt (remain + group_size, nunits)))
2390 : {
2391 : /* But peeling a single scalar iteration is enough if
2392 : we can use the next power-of-two sized partial
2393 : access and that is sufficiently small to be covered
2394 : by the single scalar iteration. */
2395 16 : unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
2396 16 : if (masked_p
2397 16 : || !nunits.is_constant (&cnunits)
2398 16 : || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
2399 16 : || (((cremain = (group_size * cvf - gap) % cnunits), true)
2400 16 : && ((cpart_size = (1 << ceil_log2 (cremain))), true)
2401 16 : && (cremain + group_size < cpart_size
2402 13 : || (vector_vector_composition_type (vectype,
2403 13 : cnunits / cpart_size,
2404 : &half_vtype)
2405 : == NULL_TREE))))
2406 : {
2407 : /* If all fails we can still resort to niter masking unless
2408 : the vectors used are too big, so enforce the use of
2409 : partial vectors. */
2410 3 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
2411 3 : && !large_vector_overrun_p)
2412 : {
2413 0 : if (dump_enabled_p ())
2414 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2415 : "peeling for gaps insufficient for "
2416 : "access unless using partial "
2417 : "vectors\n");
2418 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2419 : }
2420 : else
2421 : {
2422 3 : if (dump_enabled_p ())
2423 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2424 : "peeling for gaps insufficient for "
2425 : "access\n");
2426 3 : return false;
2427 : }
2428 : }
2429 13 : else if (large_vector_overrun_p)
2430 : {
2431 13 : if (dump_enabled_p ())
2432 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2433 : "can't operate on partial vectors because "
2434 : "only unmasked loads handle access "
2435 : "shortening required because of gaps at "
2436 : "the end of the access\n");
2437 13 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
2438 : }
2439 : }
2440 : }
2441 :
2442 : /* As a last resort, trying using a gather load or scatter store.
2443 :
2444 : ??? Although the code can handle all group sizes correctly,
2445 : it probably isn't a win to use separate strided accesses based
2446 : on nearby locations. Or, even if it's a win over scalar code,
2447 : it might not be a win over vectorizing at a lower VF, if that
2448 : allows us to use contiguous accesses. */
2449 1393813 : vect_memory_access_type grouped_gather_fallback = VMAT_UNINITIALIZED;
2450 1393813 : if (loop_vinfo
2451 590052 : && (*memory_access_type == VMAT_ELEMENTWISE
2452 590052 : || *memory_access_type == VMAT_STRIDED_SLP))
2453 : {
2454 70725 : gather_scatter_info gs_info;
2455 70725 : tree tem;
2456 70725 : if (SLP_TREE_LANES (slp_node) == 1
2457 65918 : && (!SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2458 21603 : || single_element_p)
2459 134615 : && vect_use_strided_gather_scatters_p (stmt_info, vectype, loop_vinfo,
2460 : masked_p, &gs_info, elsvals,
2461 : group_size, single_element_p))
2462 : {
2463 : /* vect_use_strided_gather_scatters_p does not save the actually
2464 : supported scale and offset type so do that here.
2465 : We need it later in check_load_store_for_partial_vectors
2466 : where we only check if the given internal function is supported
2467 : (to choose whether to use the IFN, LEGACY, or EMULATED flavor
2468 : of gather/scatter) and don't re-do the full analysis. */
2469 0 : tree tmp;
2470 0 : gcc_assert (vect_gather_scatter_fn_p
2471 : (loop_vinfo, vls_type == VLS_LOAD, masked_p, vectype,
2472 : gs_info.memory_type, TREE_TYPE (gs_info.offset),
2473 : gs_info.scale, supported_scale, &gs_info.ifn,
2474 : &tmp, supported_offset_vectype, elsvals));
2475 :
2476 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2477 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2478 0 : ls->gs.ifn = gs_info.ifn;
2479 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2480 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2481 : }
2482 70725 : else if (SLP_TREE_LANES (slp_node) > 1
2483 : && !masked_p
2484 4807 : && !single_element_p
2485 75338 : && vect_use_grouped_gather (STMT_VINFO_DR_INFO (stmt_info),
2486 : vectype, loop_vinfo,
2487 : masked_p, group_size,
2488 : &gs_info, elsvals, &tem))
2489 : {
2490 0 : SLP_TREE_GS_SCALE (slp_node) = gs_info.scale;
2491 0 : SLP_TREE_GS_BASE (slp_node) = error_mark_node;
2492 0 : grouped_gather_fallback = *memory_access_type;
2493 0 : *memory_access_type = VMAT_GATHER_SCATTER_IFN;
2494 0 : ls->gs.ifn = gs_info.ifn;
2495 0 : vectype = *ls_type = tem;
2496 0 : ls->strided_offset_vectype = gs_info.offset_vectype;
2497 : }
2498 : }
2499 :
2500 1393813 : if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
2501 1393813 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2502 6458 : *poffset = neg_ldst_offset;
2503 :
2504 1393813 : if (*memory_access_type == VMAT_ELEMENTWISE
2505 1368270 : || *memory_access_type == VMAT_GATHER_SCATTER_LEGACY
2506 1367690 : || *memory_access_type == VMAT_STRIDED_SLP
2507 1322438 : || *memory_access_type == VMAT_INVARIANT)
2508 : {
2509 74742 : *alignment_support_scheme = dr_unaligned_supported;
2510 74742 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2511 : }
2512 : else
2513 : {
2514 1319071 : if (mat_gather_scatter_p (*memory_access_type)
2515 : && !first_dr_info)
2516 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2517 : else
2518 1319071 : *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
2519 1319071 : *alignment_support_scheme
2520 1319071 : = vect_supportable_dr_alignment
2521 1319071 : (vinfo, first_dr_info, vectype, *misalignment,
2522 1319071 : mat_gather_scatter_p (*memory_access_type));
2523 1319071 : if (grouped_gather_fallback != VMAT_UNINITIALIZED
2524 0 : && *alignment_support_scheme != dr_aligned
2525 0 : && *alignment_support_scheme != dr_unaligned_supported)
2526 : {
2527 : /* No supportable alignment for a grouped gather, fall back to the
2528 : original memory access type. Even though VMAT_STRIDED_SLP might
2529 : also try aligned vector loads it can still choose vector
2530 : construction from scalars. */
2531 0 : *memory_access_type = grouped_gather_fallback;
2532 0 : *alignment_support_scheme = dr_unaligned_supported;
2533 0 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2534 : }
2535 : }
2536 :
2537 1393813 : if (overrun_p)
2538 : {
2539 9276 : gcc_assert (can_overrun_p);
2540 9276 : if (dump_enabled_p ())
2541 511 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2542 : "Data access with gaps requires scalar "
2543 : "epilogue loop\n");
2544 9276 : LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2545 : }
2546 :
2547 1393813 : if ((*memory_access_type == VMAT_ELEMENTWISE
2548 1393813 : || *memory_access_type == VMAT_STRIDED_SLP)
2549 : && !nunits.is_constant ())
2550 : {
2551 : if (dump_enabled_p ())
2552 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2553 : "Not using elementwise accesses due to variable "
2554 : "vectorization factor.\n");
2555 : return false;
2556 : }
2557 :
2558 : /* Checks if all scalar iterations are known to be inbounds. */
2559 1393813 : bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
2560 :
2561 : /* Check if we support the operation if early breaks are needed. Here we
2562 : must ensure that we don't access any more than the scalar code would
2563 : have. A masked operation would ensure this, so for these load types
2564 : force masking. */
2565 1393813 : if (loop_vinfo
2566 590052 : && dr_safe_speculative_read_required (stmt_info)
2567 1584649 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
2568 : {
2569 190836 : if (mat_gather_scatter_p (*memory_access_type)
2570 190836 : || *memory_access_type == VMAT_STRIDED_SLP)
2571 : {
2572 9380 : if (dump_enabled_p ())
2573 8 : dump_printf_loc (MSG_NOTE, vect_location,
2574 : "early break not supported: cannot peel for "
2575 : "alignment. With non-contiguous memory vectorization"
2576 : " could read out of bounds at %G ",
2577 : STMT_VINFO_STMT (stmt_info));
2578 9380 : if (inbounds)
2579 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2580 : else
2581 : return false;
2582 : }
2583 : /* Block-level alignment: Even though individual accesses of
2584 : VMAT_ELEMENTWISE type do not cause alignment problems, loading the
2585 : whole vector's worth of values in a speculative early-break context
2586 : might cross a page boundary. Set the alignment scheme to `dr_aligned'
2587 : here in order to force checking of whether such accesses meet
2588 : alignment criteria. */
2589 181456 : else if (*memory_access_type == VMAT_ELEMENTWISE && !inbounds)
2590 14923 : *alignment_support_scheme = dr_aligned;
2591 : }
2592 :
2593 : /* If this DR needs alignment for correctness, we must ensure the target
2594 : alignment is a constant power-of-two multiple of the amount read per
2595 : vector iteration or force masking. */
2596 1384433 : if (dr_safe_speculative_read_required (stmt_info)
2597 1384433 : && (*alignment_support_scheme == dr_aligned
2598 109523 : && !mat_gather_scatter_p (*memory_access_type)))
2599 : {
2600 : /* We can only peel for loops, of course. */
2601 109523 : gcc_checking_assert (loop_vinfo);
2602 :
2603 109523 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2604 109523 : poly_uint64 read_amount
2605 109523 : = vf * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2606 109523 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2607 109523 : read_amount *= group_size;
2608 :
2609 109523 : auto target_alignment
2610 109523 : = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
2611 109523 : if (!multiple_p (target_alignment, read_amount))
2612 : {
2613 12688 : if (dump_enabled_p ())
2614 : {
2615 28 : dump_printf_loc (MSG_NOTE, vect_location,
2616 : "desired alignment not met, target was ");
2617 28 : dump_dec (MSG_NOTE, target_alignment);
2618 28 : dump_printf (MSG_NOTE, " previously, but read amount is ");
2619 28 : dump_dec (MSG_NOTE, read_amount);
2620 28 : dump_printf (MSG_NOTE, " at %G.\n", STMT_VINFO_STMT (stmt_info));
2621 : }
2622 14922 : return false;
2623 : }
2624 :
2625 : /* When using a group access the first element may be aligned but the
2626 : subsequent loads may not be. For LOAD_LANES since the loads are based
2627 : on the first DR then all loads in the group are aligned. For
2628 : non-LOAD_LANES this is not the case. In particular a load + blend when
2629 : there are gaps can have the non first loads issued unaligned, even
2630 : partially overlapping the memory of the first load in order to simplify
2631 : the blend. This is what the x86_64 backend does for instance. As
2632 : such only the first load in the group is aligned, the rest are not.
2633 : Because of this the permutes may break the alignment requirements that
2634 : have been set, and as such we should for now, reject them. */
2635 96835 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2636 : {
2637 2234 : if (dump_enabled_p ())
2638 75 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2639 : "loads with load permutations not supported for "
2640 : "speculative early break loads for %G",
2641 : STMT_VINFO_STMT (stmt_info));
2642 2234 : return false;
2643 : }
2644 :
2645 : /* Reject vectorization if we know the read mount per vector iteration
2646 : exceeds the min page size. */
2647 94601 : if (known_gt (read_amount, (unsigned) param_min_pagesize))
2648 : {
2649 0 : if (dump_enabled_p ())
2650 : {
2651 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2652 : "alignment required for correctness (");
2653 0 : dump_dec (MSG_MISSED_OPTIMIZATION, read_amount);
2654 0 : dump_printf (MSG_NOTE, ") may exceed page size.\n");
2655 : }
2656 0 : return false;
2657 : }
2658 :
2659 94601 : if (!vf.is_constant ())
2660 : {
2661 : /* For VLA modes, we need a runtime check to ensure any speculative
2662 : read amount does not exceed the page size. Here we record the max
2663 : possible read amount for the check. */
2664 : if (maybe_gt (read_amount,
2665 : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo)))
2666 : LOOP_VINFO_MAX_SPEC_READ_AMOUNT (loop_vinfo) = read_amount;
2667 :
2668 : /* For VLA modes, we must use partial vectors. */
2669 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2670 : }
2671 : }
2672 :
2673 1369511 : if (*alignment_support_scheme == dr_unaligned_unsupported)
2674 : {
2675 68565 : if (dump_enabled_p ())
2676 256 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2677 : "unsupported unaligned access\n");
2678 68565 : return false;
2679 : }
2680 :
2681 : /* FIXME: At the moment the cost model seems to underestimate the
2682 : cost of using elementwise accesses. This check preserves the
2683 : traditional behavior until that can be fixed. */
2684 1300946 : if (*memory_access_type == VMAT_ELEMENTWISE
2685 14813 : && !STMT_VINFO_STRIDED_P (first_stmt_info)
2686 1315759 : && !(STMT_VINFO_GROUPED_ACCESS (stmt_info)
2687 9664 : && single_element_p
2688 9019 : && !pow2p_hwi (group_size)))
2689 : {
2690 9134 : if (dump_enabled_p ())
2691 364 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2692 : "not falling back to elementwise accesses\n");
2693 9134 : return false;
2694 : }
2695 :
2696 : /* For BB vectorization build up the vector from existing scalar defs. */
2697 1291812 : if (!loop_vinfo && *memory_access_type == VMAT_ELEMENTWISE)
2698 : return false;
2699 :
2700 : /* Some loads need to explicitly permute the loaded data if there
2701 : is a load permutation. Among those are:
2702 : - VMAT_ELEMENTWISE.
2703 : - VMAT_STRIDED_SLP.
2704 : - VMAT_GATHER_SCATTER:
2705 : - Strided gather (fallback for VMAT_STRIDED_SLP if #lanes == 1).
2706 : - Grouped strided gather (ditto but for #lanes > 1).
2707 :
2708 : For VMAT_ELEMENTWISE we can fold the load permutation into the
2709 : individual indices we access directly, eliding the permutation.
2710 : Strided gather only allows load permutations for the
2711 : single-element case. */
2712 :
2713 1291812 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2714 1291812 : && !(*memory_access_type == VMAT_ELEMENTWISE
2715 53532 : || (mat_gather_scatter_p (*memory_access_type)
2716 0 : && SLP_TREE_LANES (slp_node) == 1
2717 0 : && single_element_p)))
2718 : {
2719 53532 : if (!loop_vinfo)
2720 : {
2721 : /* In BB vectorization we may not actually use a loaded vector
2722 : accessing elements in excess of DR_GROUP_SIZE. */
2723 31635 : stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
2724 31635 : group_info = DR_GROUP_FIRST_ELEMENT (group_info);
2725 31635 : unsigned HOST_WIDE_INT nunits;
2726 31635 : unsigned j, k, maxk = 0;
2727 110475 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
2728 78840 : if (k > maxk)
2729 : maxk = k;
2730 31635 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2731 57817 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
2732 31635 : || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
2733 : {
2734 5453 : if (dump_enabled_p ())
2735 31 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2736 : "BB vectorization with gaps at the end of "
2737 : "a load is not supported\n");
2738 5453 : return false;
2739 : }
2740 : }
2741 :
2742 48079 : if (!perm_ok)
2743 : {
2744 2544 : if (dump_enabled_p ())
2745 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
2746 : vect_location,
2747 : "unsupported load permutation\n");
2748 2544 : return false;
2749 : }
2750 :
2751 45535 : *slp_perm = true;
2752 : }
2753 :
2754 : return true;
2755 : }
2756 :
2757 : /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
2758 : conditional operation STMT_INFO. When returning true, store the mask
2759 : in *MASK_NODE, the type of its definition in *MASK_DT_OUT and the type of
2760 : the vectorized mask in *MASK_VECTYPE_OUT. */
2761 :
2762 : static bool
2763 12641 : vect_check_scalar_mask (vec_info *vinfo,
2764 : slp_tree slp_node, unsigned mask_index,
2765 : slp_tree *mask_node,
2766 : vect_def_type *mask_dt_out, tree *mask_vectype_out)
2767 : {
2768 12641 : enum vect_def_type mask_dt;
2769 12641 : tree mask_vectype;
2770 12641 : slp_tree mask_node_1;
2771 12641 : tree mask_;
2772 12641 : if (!vect_is_simple_use (vinfo, slp_node, mask_index,
2773 : &mask_, &mask_node_1, &mask_dt, &mask_vectype))
2774 : {
2775 0 : if (dump_enabled_p ())
2776 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2777 : "mask use not simple.\n");
2778 0 : return false;
2779 : }
2780 :
2781 12641 : if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
2782 12641 : && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (mask_)))
2783 : {
2784 0 : if (dump_enabled_p ())
2785 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2786 : "mask argument is not a boolean.\n");
2787 0 : return false;
2788 : }
2789 :
2790 12641 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2791 12641 : if (!mask_vectype)
2792 19 : mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
2793 : mask_node_1);
2794 :
2795 12641 : if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2796 : {
2797 0 : if (dump_enabled_p ())
2798 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2799 : "could not find an appropriate vector mask type.\n");
2800 0 : return false;
2801 : }
2802 :
2803 12641 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2804 25282 : TYPE_VECTOR_SUBPARTS (vectype)))
2805 : {
2806 0 : if (dump_enabled_p ())
2807 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2808 : "vector mask type %T"
2809 : " does not match vector data type %T.\n",
2810 : mask_vectype, vectype);
2811 :
2812 0 : return false;
2813 : }
2814 :
2815 12641 : *mask_dt_out = mask_dt;
2816 12641 : *mask_vectype_out = mask_vectype;
2817 12641 : *mask_node = mask_node_1;
2818 12641 : return true;
2819 : }
2820 :
2821 :
2822 : /* Return true if stored value is suitable for vectorizing store
2823 : statement STMT_INFO. When returning true, store the scalar stored
2824 : in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
2825 : the type of the vectorized store value in
2826 : *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
2827 :
2828 : static bool
2829 1379842 : vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
2830 : slp_tree slp_node, slp_tree *rhs_node,
2831 : vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
2832 : vec_load_store_type *vls_type_out)
2833 : {
2834 1379842 : int op_no = 0;
2835 1379842 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2836 : {
2837 1857 : if (gimple_call_internal_p (call)
2838 1857 : && internal_store_fn_p (gimple_call_internal_fn (call)))
2839 1857 : op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
2840 : }
2841 1379842 : op_no = vect_slp_child_index_for_operand (stmt_info, op_no);
2842 :
2843 1379842 : enum vect_def_type rhs_dt;
2844 1379842 : tree rhs_vectype;
2845 1379842 : tree rhs;
2846 1379842 : if (!vect_is_simple_use (vinfo, slp_node, op_no,
2847 : &rhs, rhs_node, &rhs_dt, &rhs_vectype))
2848 : {
2849 0 : if (dump_enabled_p ())
2850 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2851 : "use not simple.\n");
2852 0 : return false;
2853 : }
2854 :
2855 : /* In the case this is a store from a constant make sure
2856 : native_encode_expr can handle it. */
2857 1379842 : if (rhs_dt == vect_constant_def
2858 1379842 : && CONSTANT_CLASS_P (rhs) && native_encode_expr (rhs, NULL, 64) == 0)
2859 : {
2860 0 : if (dump_enabled_p ())
2861 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2862 : "cannot encode constant as a byte sequence.\n");
2863 0 : return false;
2864 : }
2865 :
2866 1379842 : tree vectype = SLP_TREE_VECTYPE (slp_node);
2867 1379842 : if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2868 : {
2869 24 : if (dump_enabled_p ())
2870 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2871 : "incompatible vector types.\n");
2872 24 : return false;
2873 : }
2874 :
2875 1379818 : *rhs_dt_out = rhs_dt;
2876 1379818 : *rhs_vectype_out = rhs_vectype;
2877 1379818 : if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
2878 1017818 : *vls_type_out = VLS_STORE_INVARIANT;
2879 : else
2880 362000 : *vls_type_out = VLS_STORE;
2881 : return true;
2882 : }
2883 :
2884 : /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
2885 : Note that we support masks with floating-point type, in which case the
2886 : floats are interpreted as a bitmask. */
2887 :
2888 : static tree
2889 170 : vect_build_all_ones_mask (vec_info *vinfo,
2890 : stmt_vec_info stmt_info, tree masktype)
2891 : {
2892 170 : if (TREE_CODE (masktype) == INTEGER_TYPE)
2893 98 : return build_int_cst (masktype, -1);
2894 72 : else if (VECTOR_BOOLEAN_TYPE_P (masktype)
2895 144 : || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2896 : {
2897 19 : tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2898 19 : mask = build_vector_from_val (masktype, mask);
2899 19 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2900 : }
2901 53 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2902 : {
2903 : REAL_VALUE_TYPE r;
2904 : long tmp[6];
2905 371 : for (int j = 0; j < 6; ++j)
2906 318 : tmp[j] = -1;
2907 53 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2908 53 : tree mask = build_real (TREE_TYPE (masktype), r);
2909 53 : mask = build_vector_from_val (masktype, mask);
2910 53 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2911 : }
2912 0 : gcc_unreachable ();
2913 : }
2914 :
2915 : /* Build an all-zero merge value of type VECTYPE while vectorizing
2916 : STMT_INFO as a gather load. */
2917 :
2918 : static tree
2919 158 : vect_build_zero_merge_argument (vec_info *vinfo,
2920 : stmt_vec_info stmt_info, tree vectype)
2921 : {
2922 158 : tree merge;
2923 158 : if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2924 49 : merge = build_int_cst (TREE_TYPE (vectype), 0);
2925 109 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2926 : {
2927 : REAL_VALUE_TYPE r;
2928 : long tmp[6];
2929 763 : for (int j = 0; j < 6; ++j)
2930 654 : tmp[j] = 0;
2931 109 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2932 109 : merge = build_real (TREE_TYPE (vectype), r);
2933 : }
2934 : else
2935 0 : gcc_unreachable ();
2936 158 : merge = build_vector_from_val (vectype, merge);
2937 158 : return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
2938 : }
2939 :
2940 : /* Return the corresponding else value for an else value constant
2941 : ELSVAL with type TYPE. */
2942 :
2943 : tree
2944 1962 : vect_get_mask_load_else (int elsval, tree type)
2945 : {
2946 1962 : tree els;
2947 1962 : if (elsval == MASK_LOAD_ELSE_UNDEFINED)
2948 : {
2949 0 : tree tmp = create_tmp_var (type);
2950 : /* No need to warn about anything. */
2951 0 : TREE_NO_WARNING (tmp) = 1;
2952 0 : els = get_or_create_ssa_default_def (cfun, tmp);
2953 : }
2954 1962 : else if (elsval == MASK_LOAD_ELSE_M1)
2955 0 : els = build_minus_one_cst (type);
2956 1962 : else if (elsval == MASK_LOAD_ELSE_ZERO)
2957 1962 : els = build_zero_cst (type);
2958 : else
2959 0 : gcc_unreachable ();
2960 :
2961 1962 : return els;
2962 : }
2963 :
2964 : /* Build a gather load call while vectorizing STMT_INFO. Insert new
2965 : instructions before GSI and add them to VEC_STMT. GS_INFO describes
2966 : the gather load operation. If the load is conditional, MASK is the
2967 : vectorized condition, otherwise MASK is null. PTR is the base
2968 : pointer and OFFSET is the vectorized offset. */
2969 :
2970 : static gimple *
2971 349 : vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
2972 : slp_tree slp_node, tree vectype,
2973 : gimple_stmt_iterator *gsi, tree decl,
2974 : tree ptr, tree offset, tree mask)
2975 : {
2976 349 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
2977 349 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
2978 349 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2979 349 : /* ptrtype */ arglist = TREE_CHAIN (arglist);
2980 349 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2981 349 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2982 349 : tree scaletype = TREE_VALUE (arglist);
2983 349 : tree var;
2984 349 : gcc_checking_assert (types_compatible_p (srctype, rettype)
2985 : && (!mask
2986 : || TREE_CODE (masktype) == INTEGER_TYPE
2987 : || types_compatible_p (srctype, masktype)));
2988 :
2989 349 : tree op = offset;
2990 349 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
2991 : {
2992 103 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
2993 : TYPE_VECTOR_SUBPARTS (idxtype)));
2994 103 : var = vect_get_new_ssa_name (idxtype, vect_simple_var);
2995 103 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
2996 103 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2997 103 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2998 103 : op = var;
2999 : }
3000 :
3001 349 : tree src_op = NULL_TREE;
3002 349 : tree mask_op = NULL_TREE;
3003 349 : if (mask)
3004 : {
3005 191 : if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
3006 : {
3007 191 : tree utype, optype = TREE_TYPE (mask);
3008 191 : if (VECTOR_TYPE_P (masktype)
3009 191 : || TYPE_MODE (masktype) == TYPE_MODE (optype))
3010 : utype = masktype;
3011 : else
3012 6 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3013 191 : var = vect_get_new_ssa_name (utype, vect_scalar_var);
3014 191 : tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
3015 191 : gassign *new_stmt
3016 191 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3017 191 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3018 191 : mask_arg = var;
3019 191 : if (!useless_type_conversion_p (masktype, utype))
3020 : {
3021 6 : gcc_assert (TYPE_PRECISION (utype)
3022 : <= TYPE_PRECISION (masktype));
3023 6 : var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3024 6 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3025 6 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3026 6 : mask_arg = var;
3027 : }
3028 191 : src_op = build_zero_cst (srctype);
3029 191 : mask_op = mask_arg;
3030 : }
3031 : else
3032 : {
3033 : src_op = mask;
3034 : mask_op = mask;
3035 : }
3036 : }
3037 : else
3038 : {
3039 158 : src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
3040 158 : mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
3041 : }
3042 :
3043 349 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
3044 349 : gimple *new_stmt = gimple_build_call (decl, 5, src_op, ptr, op,
3045 : mask_op, scale);
3046 :
3047 349 : if (!useless_type_conversion_p (vectype, rettype))
3048 : {
3049 52 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
3050 : TYPE_VECTOR_SUBPARTS (rettype)));
3051 52 : op = vect_get_new_ssa_name (rettype, vect_simple_var);
3052 52 : gimple_call_set_lhs (new_stmt, op);
3053 52 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3054 52 : op = build1 (VIEW_CONVERT_EXPR, vectype, op);
3055 52 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
3056 : }
3057 :
3058 349 : return new_stmt;
3059 : }
3060 :
3061 : /* Build a scatter store call while vectorizing STMT_INFO. Insert new
3062 : instructions before GSI. GS_INFO describes the scatter store operation.
3063 : PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
3064 : vectorized data to store.
3065 : If the store is conditional, MASK is the vectorized condition, otherwise
3066 : MASK is null. */
3067 :
3068 : static gimple *
3069 161 : vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
3070 : slp_tree slp_node,
3071 : gimple_stmt_iterator *gsi,
3072 : tree decl,
3073 : tree ptr, tree offset, tree oprnd, tree mask)
3074 : {
3075 161 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
3076 161 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (decl));
3077 161 : /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
3078 161 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3079 161 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3080 161 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3081 161 : tree scaletype = TREE_VALUE (arglist);
3082 161 : gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
3083 : && TREE_CODE (rettype) == VOID_TYPE);
3084 :
3085 161 : tree mask_arg = NULL_TREE;
3086 161 : if (mask)
3087 : {
3088 110 : mask_arg = mask;
3089 110 : tree optype = TREE_TYPE (mask_arg);
3090 110 : tree utype;
3091 110 : if (TYPE_MODE (masktype) == TYPE_MODE (optype))
3092 : utype = masktype;
3093 : else
3094 8 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3095 110 : tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
3096 110 : mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
3097 110 : gassign *new_stmt
3098 110 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3099 110 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3100 110 : mask_arg = var;
3101 110 : if (!useless_type_conversion_p (masktype, utype))
3102 : {
3103 8 : gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
3104 8 : tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3105 8 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3106 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3107 8 : mask_arg = var;
3108 : }
3109 : }
3110 : else
3111 : {
3112 51 : mask_arg = build_int_cst (masktype, -1);
3113 51 : mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
3114 : }
3115 :
3116 161 : tree src = oprnd;
3117 161 : if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
3118 : {
3119 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
3120 : TYPE_VECTOR_SUBPARTS (srctype)));
3121 0 : tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
3122 0 : src = build1 (VIEW_CONVERT_EXPR, srctype, src);
3123 0 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
3124 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3125 0 : src = var;
3126 : }
3127 :
3128 161 : tree op = offset;
3129 161 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3130 : {
3131 16 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3132 : TYPE_VECTOR_SUBPARTS (idxtype)));
3133 16 : tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3134 16 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3135 16 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3136 16 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3137 16 : op = var;
3138 : }
3139 :
3140 161 : tree scale = build_int_cst (scaletype, SLP_TREE_GS_SCALE (slp_node));
3141 161 : gcall *new_stmt
3142 161 : = gimple_build_call (decl, 5, ptr, mask_arg, op, src, scale);
3143 161 : return new_stmt;
3144 : }
3145 :
3146 : /* Prepare the base and offset in GS_INFO for vectorization.
3147 : Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
3148 : to the vectorized offset argument for the first copy of STMT_INFO.
3149 : STMT_INFO is the statement described by GS_INFO and LOOP is the
3150 : containing loop. */
3151 :
3152 : static void
3153 1235 : vect_get_gather_scatter_ops (class loop *loop, slp_tree slp_node,
3154 : tree *dataref_ptr, vec<tree> *vec_offset)
3155 : {
3156 1235 : gimple_seq stmts = NULL;
3157 1235 : *dataref_ptr = force_gimple_operand (SLP_TREE_GS_BASE (slp_node),
3158 : &stmts, true, NULL_TREE);
3159 1235 : if (stmts != NULL)
3160 : {
3161 1002 : basic_block new_bb;
3162 1002 : edge pe = loop_preheader_edge (loop);
3163 1002 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3164 1002 : gcc_assert (!new_bb);
3165 : }
3166 1235 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
3167 1235 : }
3168 :
3169 : /* Prepare to implement a grouped or strided load or store using
3170 : the gather load or scatter store operation described by GS_INFO.
3171 : STMT_INFO is the load or store statement.
3172 :
3173 : Set *DR_STEP to the amount that should be added to pointer base address
3174 : to get to the next iteration's base address.
3175 : Set *DR_BUMP to the amount that should be added to the base
3176 : address after each copy of the vectorized statement in a grouped read.
3177 : Set *VEC_OFFSET to an invariant offset vector in which element I has the
3178 : value I * DR_STEP / SCALE. */
3179 :
3180 : static void
3181 0 : vect_get_strided_load_store_ops (stmt_vec_info stmt_info, slp_tree node,
3182 : tree vectype, tree offset_vectype,
3183 : loop_vec_info loop_vinfo,
3184 : gimple_stmt_iterator *gsi,
3185 : tree *dr_step, tree *dr_bump,
3186 : tree *vec_offset)
3187 : {
3188 0 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3189 :
3190 0 : tree dr_step_temp
3191 0 : = size_binop (MULT_EXPR,
3192 : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3193 : LOOP_VINFO_IV_INCREMENT (loop_vinfo));
3194 0 : *dr_step = LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo)
3195 0 : ? cse_and_gimplify_to_preheader (loop_vinfo, dr_step_temp)
3196 0 : : force_gimple_operand_gsi (gsi, dr_step_temp, false, NULL_TREE,
3197 : true, GSI_SAME_STMT);
3198 0 : tree bump = size_binop (MULT_EXPR,
3199 : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3200 : size_int (TYPE_VECTOR_SUBPARTS (vectype)));
3201 0 : *dr_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
3202 :
3203 0 : internal_fn ifn
3204 0 : = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
3205 0 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
3206 : {
3207 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
3208 : unshare_expr (DR_STEP (dr)));
3209 0 : return;
3210 : }
3211 :
3212 : /* The offset given in GS_INFO can have pointer type, so use the element
3213 : type of the vector instead. */
3214 0 : tree offset_type = TREE_TYPE (offset_vectype);
3215 :
3216 : /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
3217 0 : tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
3218 : ssize_int (SLP_TREE_GS_SCALE (node)));
3219 0 : step = fold_convert (offset_type, step);
3220 :
3221 : /* Create {0, X, X*2, X*3, ...}. */
3222 0 : tree offset = fold_build2 (VEC_SERIES_EXPR, offset_vectype,
3223 : build_zero_cst (offset_type), step);
3224 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
3225 : }
3226 :
3227 : /* Return the amount that should be added to a vector pointer, represented by
3228 : DR_INFO, to increment to the next vectorized iteration. */
3229 :
3230 : static tree
3231 712902 : vect_get_data_ptr_step (vec_info *vinfo, dr_vec_info *dr_info,
3232 : vect_memory_access_type memory_access_type)
3233 : {
3234 712902 : if (memory_access_type == VMAT_INVARIANT)
3235 0 : return size_zero_node;
3236 :
3237 712902 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3238 :
3239 : /* For BB SLP there is no next iteration. */
3240 712902 : if (!loop_vinfo)
3241 577881 : return build_zero_cst (sizetype);
3242 :
3243 135021 : tree step = vect_dr_behavior (loop_vinfo, dr_info)->step;
3244 :
3245 : /* gather/scatter never reach here. */
3246 135021 : gcc_assert (!mat_gather_scatter_p (memory_access_type));
3247 :
3248 135021 : tree iv_increment = LOOP_VINFO_IV_INCREMENT (loop_vinfo);
3249 :
3250 135021 : return fold_build2 (MULT_EXPR, sizetype, iv_increment,
3251 : fold_convert (sizetype, step));
3252 : }
3253 :
3254 : /* Return the amount that should be added to a vector pointer to move
3255 : to the next or previous copy of AGGR_TYPE. DR_INFO is the data reference
3256 : being vectorized and MEMORY_ACCESS_TYPE describes the type of
3257 : vectorization. */
3258 :
3259 : static tree
3260 713414 : vect_get_data_ptr_bump (vec_info *vinfo,
3261 : dr_vec_info *dr_info, tree aggr_type,
3262 : vect_memory_access_type memory_access_type)
3263 : {
3264 713414 : if (memory_access_type == VMAT_INVARIANT)
3265 0 : return size_zero_node;
3266 :
3267 713414 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3268 : /* We do not support SLP loads where num_vec != 1 with SELECT_VL so this value
3269 : should never be needed. */
3270 135533 : if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3271 : return NULL_TREE;
3272 :
3273 713414 : tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3274 713414 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3275 713414 : if (tree_int_cst_sgn (step) == -1)
3276 2839 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3277 : return iv_step;
3278 : }
3279 :
3280 : /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}. */
3281 :
3282 : static bool
3283 140 : vectorizable_bswap (vec_info *vinfo,
3284 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3285 : slp_tree slp_node,
3286 : slp_tree *slp_op,
3287 : tree vectype_in, stmt_vector_for_cost *cost_vec)
3288 : {
3289 140 : tree op, vectype;
3290 140 : gcall *stmt = as_a <gcall *> (stmt_info->stmt);
3291 :
3292 140 : op = gimple_call_arg (stmt, 0);
3293 140 : vectype = SLP_TREE_VECTYPE (slp_node);
3294 140 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3295 :
3296 140 : if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
3297 : {
3298 0 : if (dump_enabled_p ())
3299 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3300 : "mismatched vector sizes %T and %T\n",
3301 : vectype_in, vectype);
3302 0 : return false;
3303 : }
3304 :
3305 140 : tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
3306 140 : if (! char_vectype)
3307 : return false;
3308 :
3309 140 : poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
3310 140 : unsigned word_bytes;
3311 140 : if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
3312 : return false;
3313 :
3314 : /* The encoding uses one stepped pattern for each byte in the word. */
3315 140 : vec_perm_builder elts (num_bytes, word_bytes, 3);
3316 560 : for (unsigned i = 0; i < 3; ++i)
3317 2532 : for (unsigned j = 0; j < word_bytes; ++j)
3318 2112 : elts.quick_push ((i + 1) * word_bytes - j - 1);
3319 :
3320 140 : vec_perm_indices indices (elts, 1, num_bytes);
3321 140 : machine_mode vmode = TYPE_MODE (char_vectype);
3322 140 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3323 : return false;
3324 :
3325 57 : if (cost_vec)
3326 : {
3327 41 : if (!vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
3328 : {
3329 0 : if (dump_enabled_p ())
3330 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3331 : "incompatible vector types for invariants\n");
3332 0 : return false;
3333 : }
3334 :
3335 41 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3336 41 : DUMP_VECT_SCOPE ("vectorizable_bswap");
3337 41 : record_stmt_cost (cost_vec,
3338 : 1, vector_stmt, slp_node, 0, vect_prologue);
3339 41 : record_stmt_cost (cost_vec,
3340 41 : vect_get_num_copies (vinfo, slp_node),
3341 : vec_perm, slp_node, 0, vect_body);
3342 41 : return true;
3343 : }
3344 :
3345 16 : tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
3346 :
3347 : /* Transform. */
3348 16 : vec<tree> vec_oprnds = vNULL;
3349 16 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
3350 : /* Arguments are ready. create the new vector stmt. */
3351 16 : unsigned i;
3352 16 : tree vop;
3353 32 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3354 : {
3355 16 : gimple *new_stmt;
3356 16 : tree tem = make_ssa_name (char_vectype);
3357 16 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3358 : char_vectype, vop));
3359 16 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3360 16 : tree tem2 = make_ssa_name (char_vectype);
3361 16 : new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3362 : tem, tem, bswap_vconst);
3363 16 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3364 16 : tem = make_ssa_name (vectype);
3365 16 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3366 : vectype, tem2));
3367 16 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3368 16 : slp_node->push_vec_def (new_stmt);
3369 : }
3370 :
3371 16 : vec_oprnds.release ();
3372 16 : return true;
3373 140 : }
3374 :
3375 : /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3376 : integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3377 : in a single step. On success, store the binary pack code in
3378 : *CONVERT_CODE. */
3379 :
3380 : static bool
3381 197 : simple_integer_narrowing (tree vectype_out, tree vectype_in,
3382 : code_helper *convert_code)
3383 : {
3384 394 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3385 394 : || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3386 : return false;
3387 :
3388 87 : code_helper code;
3389 87 : int multi_step_cvt = 0;
3390 87 : auto_vec <tree, 8> interm_types;
3391 130 : if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3392 : &code, &multi_step_cvt, &interm_types)
3393 87 : || multi_step_cvt)
3394 43 : return false;
3395 :
3396 44 : *convert_code = code;
3397 44 : return true;
3398 87 : }
3399 :
3400 : /* Function vectorizable_call.
3401 :
3402 : Check if STMT_INFO performs a function call that can be vectorized.
3403 : If COST_VEC is passed, calculate costs but don't change anything,
3404 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
3405 : it, and insert it at GSI.
3406 : Return true if STMT_INFO is vectorizable in this way. */
3407 :
3408 : static bool
3409 2638161 : vectorizable_call (vec_info *vinfo,
3410 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3411 : slp_tree slp_node,
3412 : stmt_vector_for_cost *cost_vec)
3413 : {
3414 2638161 : gcall *stmt;
3415 2638161 : tree vec_dest;
3416 2638161 : tree scalar_dest;
3417 2638161 : tree op;
3418 2638161 : tree vec_oprnd0 = NULL_TREE;
3419 2638161 : tree vectype_out, vectype_in;
3420 2638161 : poly_uint64 nunits_in;
3421 2638161 : poly_uint64 nunits_out;
3422 2638161 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3423 2638161 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3424 2638161 : tree fndecl, new_temp, rhs_type;
3425 2638161 : enum vect_def_type dt[5]
3426 : = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3427 : vect_unknown_def_type, vect_unknown_def_type };
3428 2638161 : tree vectypes[ARRAY_SIZE (dt)] = {};
3429 2638161 : slp_tree slp_op[ARRAY_SIZE (dt)] = {};
3430 2638161 : auto_vec<tree, 8> vargs;
3431 2638161 : enum { NARROW, NONE, WIDEN } modifier;
3432 2638161 : size_t i, nargs;
3433 2638161 : tree clz_ctz_arg1 = NULL_TREE;
3434 :
3435 2638161 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3436 : return false;
3437 :
3438 2638161 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3439 243533 : && cost_vec)
3440 : return false;
3441 :
3442 : /* Is STMT_INFO a vectorizable call? */
3443 2653348 : stmt = dyn_cast <gcall *> (stmt_info->stmt);
3444 26371 : if (!stmt)
3445 : return false;
3446 :
3447 26371 : if (gimple_call_internal_p (stmt)
3448 26371 : && (internal_load_fn_p (gimple_call_internal_fn (stmt))
3449 16523 : || internal_store_fn_p (gimple_call_internal_fn (stmt))))
3450 : /* Handled by vectorizable_load and vectorizable_store. */
3451 3792 : return false;
3452 :
3453 22579 : if (gimple_call_lhs (stmt) == NULL_TREE
3454 22579 : || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3455 : return false;
3456 :
3457 22573 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
3458 :
3459 22573 : vectype_out = SLP_TREE_VECTYPE (slp_node);
3460 :
3461 : /* Process function arguments. */
3462 22573 : rhs_type = NULL_TREE;
3463 22573 : vectype_in = NULL_TREE;
3464 22573 : nargs = gimple_call_num_args (stmt);
3465 :
3466 : /* Bail out if the function has more than four arguments, we do not have
3467 : interesting builtin functions to vectorize with more than two arguments
3468 : except for fma (cond_fma has more). No arguments is also not good. */
3469 22573 : if (nargs == 0 || nargs > 5)
3470 : return false;
3471 :
3472 : /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic. */
3473 22493 : combined_fn cfn = gimple_call_combined_fn (stmt);
3474 22493 : if (cfn == CFN_GOMP_SIMD_LANE)
3475 : {
3476 3199 : nargs = 0;
3477 3199 : rhs_type = unsigned_type_node;
3478 : }
3479 : /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
3480 : argument just says whether it is well-defined at zero or not and what
3481 : value should be returned for it. */
3482 22493 : if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
3483 : {
3484 168 : nargs = 1;
3485 168 : clz_ctz_arg1 = gimple_call_arg (stmt, 1);
3486 : }
3487 :
3488 22493 : int mask_opno = -1;
3489 22493 : if (internal_fn_p (cfn))
3490 : {
3491 : /* We can only handle direct internal masked calls here,
3492 : vectorizable_simd_clone_call is for the rest. */
3493 19127 : if (cfn == CFN_MASK_CALL)
3494 : return false;
3495 18973 : mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3496 : }
3497 :
3498 69583 : for (i = 0; i < nargs; i++)
3499 : {
3500 49308 : if ((int) i == mask_opno)
3501 : {
3502 7694 : if (!vect_check_scalar_mask (vinfo, slp_node, mask_opno,
3503 : &slp_op[i], &dt[i], &vectypes[i]))
3504 : return false;
3505 7694 : continue;
3506 : }
3507 :
3508 41614 : if (!vect_is_simple_use (vinfo, slp_node,
3509 : i, &op, &slp_op[i], &dt[i], &vectypes[i]))
3510 : {
3511 0 : if (dump_enabled_p ())
3512 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3513 : "use not simple.\n");
3514 0 : return false;
3515 : }
3516 :
3517 : /* We can only handle calls with arguments of the same type. */
3518 41614 : if (rhs_type
3519 41614 : && !types_compatible_p (rhs_type, TREE_TYPE (op)))
3520 : {
3521 2064 : if (dump_enabled_p ())
3522 222 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3523 : "argument types differ.\n");
3524 2064 : return false;
3525 : }
3526 39550 : if (!rhs_type)
3527 19140 : rhs_type = TREE_TYPE (op);
3528 :
3529 39550 : if (!vectype_in)
3530 20407 : vectype_in = vectypes[i];
3531 19143 : else if (vectypes[i]
3532 19143 : && !types_compatible_p (vectypes[i], vectype_in))
3533 : {
3534 0 : if (dump_enabled_p ())
3535 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3536 : "argument vector types differ.\n");
3537 0 : return false;
3538 : }
3539 : }
3540 : /* If all arguments are external or constant defs, infer the vector type
3541 : from the scalar type. */
3542 20275 : if (!vectype_in)
3543 6124 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
3544 20275 : if (!cost_vec)
3545 4207 : gcc_assert (vectype_in);
3546 16068 : if (!vectype_in)
3547 : {
3548 1121 : if (dump_enabled_p ())
3549 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3550 : "no vectype for scalar type %T\n", rhs_type);
3551 :
3552 1121 : return false;
3553 : }
3554 :
3555 38308 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3556 19154 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
3557 : {
3558 12 : if (dump_enabled_p ())
3559 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3560 : "mixed mask and nonmask vector types\n");
3561 12 : return false;
3562 : }
3563 :
3564 19142 : if (vect_emulated_vector_p (vectype_in)
3565 19142 : || vect_emulated_vector_p (vectype_out))
3566 : {
3567 20 : if (dump_enabled_p ())
3568 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3569 : "use emulated vector type for call\n");
3570 20 : return false;
3571 : }
3572 :
3573 : /* FORNOW */
3574 19122 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3575 19122 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3576 19122 : if (known_eq (nunits_in * 2, nunits_out))
3577 : modifier = NARROW;
3578 18488 : else if (known_eq (nunits_out, nunits_in))
3579 : modifier = NONE;
3580 50 : else if (known_eq (nunits_out * 2, nunits_in))
3581 : modifier = WIDEN;
3582 : else
3583 : return false;
3584 :
3585 : /* We only handle functions that do not read or clobber memory. */
3586 38244 : if (gimple_vuse (stmt))
3587 : {
3588 1750 : if (dump_enabled_p ())
3589 14 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3590 : "function reads from or writes to memory.\n");
3591 1750 : return false;
3592 : }
3593 :
3594 : /* For now, we only vectorize functions if a target specific builtin
3595 : is available. TODO -- in some cases, it might be profitable to
3596 : insert the calls for pieces of the vector, in order to be able
3597 : to vectorize other operations in the loop. */
3598 17372 : fndecl = NULL_TREE;
3599 17372 : internal_fn ifn = IFN_LAST;
3600 17372 : tree callee = gimple_call_fndecl (stmt);
3601 :
3602 : /* First try using an internal function. */
3603 17372 : code_helper convert_code = MAX_TREE_CODES;
3604 17372 : if (cfn != CFN_LAST
3605 17372 : && (modifier == NONE
3606 209 : || (modifier == NARROW
3607 197 : && simple_integer_narrowing (vectype_out, vectype_in,
3608 : &convert_code))))
3609 16333 : ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3610 : vectype_in);
3611 :
3612 : /* Check if the operation traps. */
3613 17372 : bool could_trap = gimple_could_trap_p (STMT_VINFO_STMT (stmt_info));
3614 17372 : if (could_trap && cost_vec && loop_vinfo)
3615 : {
3616 : /* If the operation can trap it must be conditional, otherwise fail. */
3617 474 : internal_fn cond_fn = (internal_fn_mask_index (ifn) != -1
3618 474 : ? ifn : get_conditional_internal_fn (ifn));
3619 474 : internal_fn cond_len_fn = get_len_internal_fn (cond_fn);
3620 474 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
3621 : {
3622 : /* We assume that BB SLP fills all lanes, so no inactive lanes can
3623 : cause issues. */
3624 84 : if ((cond_fn == IFN_LAST
3625 56 : || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3626 : OPTIMIZE_FOR_SPEED))
3627 140 : && (cond_len_fn == IFN_LAST
3628 56 : || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3629 : OPTIMIZE_FOR_SPEED)))
3630 : {
3631 84 : if (dump_enabled_p ())
3632 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3633 : "can't use a fully-masked loop because no"
3634 : " conditional operation is available.\n");
3635 84 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3636 : }
3637 : }
3638 : }
3639 :
3640 : /* If that fails, try asking for a target-specific built-in function. */
3641 17372 : if (ifn == IFN_LAST)
3642 : {
3643 9907 : if (cfn != CFN_LAST)
3644 9033 : fndecl = targetm.vectorize.builtin_vectorized_function
3645 9033 : (cfn, vectype_out, vectype_in);
3646 874 : else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
3647 24 : fndecl = targetm.vectorize.builtin_md_vectorized_function
3648 24 : (callee, vectype_out, vectype_in);
3649 : }
3650 :
3651 17372 : if (ifn == IFN_LAST && !fndecl)
3652 : {
3653 9527 : if (cfn == CFN_GOMP_SIMD_LANE
3654 3199 : && SLP_TREE_LANES (slp_node) == 1
3655 3199 : && loop_vinfo
3656 3199 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3657 3199 : && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3658 15925 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3659 3199 : == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3660 : {
3661 : /* We can handle IFN_GOMP_SIMD_LANE by returning a
3662 : { 0, 1, 2, ... vf - 1 } vector. */
3663 3199 : gcc_assert (nargs == 0);
3664 : }
3665 6328 : else if (modifier == NONE
3666 6328 : && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3667 5969 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3668 5916 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
3669 5863 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
3670 140 : return vectorizable_bswap (vinfo, stmt_info, gsi, slp_node,
3671 140 : slp_op, vectype_in, cost_vec);
3672 : else
3673 : {
3674 6188 : if (dump_enabled_p ())
3675 262 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3676 : "function is not vectorizable.\n");
3677 6188 : return false;
3678 : }
3679 : }
3680 :
3681 11044 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
3682 11044 : internal_fn cond_fn = (internal_fn_mask_index (ifn) != -1
3683 11044 : ? ifn : get_conditional_internal_fn (ifn));
3684 11044 : internal_fn cond_len_fn = get_len_internal_fn (cond_fn);
3685 11044 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
3686 9150 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
3687 11044 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
3688 11044 : if (cost_vec) /* transformation not required. */
3689 : {
3690 21736 : for (i = 0; i < nargs; ++i)
3691 14883 : if (!vect_maybe_update_slp_op_vectype (slp_op[i],
3692 14883 : vectypes[i]
3693 : ? vectypes[i] : vectype_in))
3694 : {
3695 0 : if (dump_enabled_p ())
3696 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3697 : "incompatible vector types for invariants\n");
3698 0 : return false;
3699 : }
3700 6853 : SLP_TREE_TYPE (slp_node) = call_vec_info_type;
3701 6853 : DUMP_VECT_SCOPE ("vectorizable_call");
3702 6853 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
3703 :
3704 6853 : if (loop_vinfo
3705 5899 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
3706 4056 : && (reduc_idx >= 0 || could_trap || mask_opno >= 0))
3707 : {
3708 2558 : if (reduc_idx >= 0
3709 1631 : && (cond_fn == IFN_LAST
3710 1631 : || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3711 : OPTIMIZE_FOR_SPEED))
3712 2570 : && (cond_len_fn == IFN_LAST
3713 12 : || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3714 : OPTIMIZE_FOR_SPEED)))
3715 : {
3716 12 : if (dump_enabled_p ())
3717 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3718 : "can't use a fully-masked loop because no"
3719 : " conditional operation is available.\n");
3720 12 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3721 : }
3722 : else
3723 : {
3724 2546 : tree scalar_mask = NULL_TREE;
3725 2546 : if (mask_opno >= 0)
3726 2546 : scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
3727 2546 : if (cond_len_fn != IFN_LAST
3728 2546 : && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3729 : OPTIMIZE_FOR_SPEED))
3730 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
3731 : 1);
3732 : else
3733 2546 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
3734 : scalar_mask);
3735 : }
3736 : }
3737 6853 : return true;
3738 : }
3739 :
3740 : /* Transform. */
3741 :
3742 4191 : if (dump_enabled_p ())
3743 416 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3744 :
3745 : /* Handle def. */
3746 4191 : scalar_dest = gimple_call_lhs (stmt);
3747 4191 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3748 :
3749 4191 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3750 3251 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
3751 4191 : unsigned int vect_nargs = nargs;
3752 4191 : if (len_loop_p && (reduc_idx >= 0 || could_trap || mask_opno >= 0))
3753 : {
3754 0 : ifn = cond_len_fn;
3755 : /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS. */
3756 0 : vect_nargs += 2;
3757 : /* But unless there's a mask argument already we need that
3758 : as well, and an else value. */
3759 0 : if (mask_opno == -1)
3760 0 : vect_nargs += 2;
3761 : }
3762 4191 : else if (masked_loop_p && mask_opno == -1 && (reduc_idx >= 0 || could_trap))
3763 : {
3764 0 : ifn = cond_fn;
3765 0 : vect_nargs += 2;
3766 : }
3767 4191 : int len_opno = internal_fn_len_index (ifn);
3768 4191 : if (clz_ctz_arg1)
3769 59 : ++vect_nargs;
3770 :
3771 4191 : if (modifier == NONE || ifn != IFN_LAST)
3772 : {
3773 4159 : tree prev_res = NULL_TREE;
3774 4159 : vargs.safe_grow (vect_nargs, true);
3775 4159 : auto_vec<vec<tree> > vec_defs (nargs);
3776 :
3777 : /* Build argument list for the vectorized call. */
3778 4159 : if (cfn == CFN_GOMP_SIMD_LANE)
3779 : {
3780 3300 : for (i = 0; i < nvectors; ++i)
3781 : {
3782 : /* ??? For multi-lane SLP we'd need to build
3783 : { 0, 0, .., 1, 1, ... }. */
3784 1704 : tree cst = build_index_vector (vectype_out,
3785 : i * nunits_out, 1);
3786 1704 : tree new_var
3787 1704 : = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
3788 1704 : gimple *init_stmt = gimple_build_assign (new_var, cst);
3789 1704 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3790 1704 : new_temp = make_ssa_name (vec_dest);
3791 1704 : gimple *new_stmt = gimple_build_assign (new_temp, new_var);
3792 1704 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3793 1704 : slp_node->push_vec_def (new_stmt);
3794 : }
3795 : }
3796 : else
3797 : {
3798 2563 : vec<tree> vec_oprnds0;
3799 2563 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3800 2563 : vec_oprnds0 = vec_defs[0];
3801 :
3802 : /* Arguments are ready. Create the new vector stmt. */
3803 5279 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
3804 : {
3805 2716 : int varg = 0;
3806 : /* Add the mask if necessary. */
3807 38 : if ((masked_loop_p || len_loop_p) && mask_opno == -1
3808 2718 : && internal_fn_mask_index (ifn) != -1)
3809 : {
3810 0 : gcc_assert (internal_fn_mask_index (ifn) == varg);
3811 0 : if (masked_loop_p)
3812 : {
3813 0 : unsigned int vec_num = vec_oprnds0.length ();
3814 0 : vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi,
3815 : masks, vec_num,
3816 : vectype_out, i);
3817 : }
3818 : else
3819 : {
3820 0 : tree mask_vectype = truth_type_for (vectype_out);
3821 0 : vargs[varg++] = vect_build_all_ones_mask (loop_vinfo,
3822 : stmt_info,
3823 : mask_vectype);
3824 : }
3825 : }
3826 : size_t k;
3827 9948 : for (k = 0; k < nargs; k++)
3828 : {
3829 7232 : vec<tree> vec_oprndsk = vec_defs[k];
3830 7232 : vargs[varg++] = vec_oprndsk[i];
3831 : }
3832 : /* Add the else value if necessary. */
3833 38 : if ((masked_loop_p || len_loop_p) && mask_opno == -1
3834 2718 : && internal_fn_else_index (ifn) != -1)
3835 : {
3836 0 : gcc_assert (internal_fn_else_index (ifn) == varg);
3837 0 : if (reduc_idx >= 0)
3838 0 : vargs[varg++] = vargs[reduc_idx + 1];
3839 : else
3840 : {
3841 0 : auto else_value = targetm.preferred_else_value
3842 0 : (ifn, vectype_out, varg - 1, &vargs[1]);
3843 0 : vargs[varg++] = else_value;
3844 : }
3845 : }
3846 2716 : if (clz_ctz_arg1)
3847 59 : vargs[varg++] = clz_ctz_arg1;
3848 :
3849 2716 : gimple *new_stmt;
3850 2716 : if (modifier == NARROW)
3851 : {
3852 : /* We don't define any narrowing conditional functions
3853 : at present. */
3854 0 : gcc_assert (mask_opno < 0);
3855 0 : tree half_res = make_ssa_name (vectype_in);
3856 0 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3857 0 : gimple_call_set_lhs (call, half_res);
3858 0 : gimple_call_set_nothrow (call, true);
3859 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3860 0 : if ((i & 1) == 0)
3861 : {
3862 0 : prev_res = half_res;
3863 0 : continue;
3864 : }
3865 0 : new_temp = make_ssa_name (vec_dest);
3866 0 : new_stmt = vect_gimple_build (new_temp, convert_code,
3867 : prev_res, half_res);
3868 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3869 : }
3870 : else
3871 : {
3872 2716 : if (len_opno >= 0 && len_loop_p)
3873 : {
3874 0 : unsigned int vec_num = vec_oprnds0.length ();
3875 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
3876 : vec_num, vectype_out, i, 1, true);
3877 0 : signed char biasval
3878 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3879 0 : tree bias = build_int_cst (intQI_type_node, biasval);
3880 0 : vargs[len_opno] = len;
3881 0 : vargs[len_opno + 1] = bias;
3882 : }
3883 2716 : else if (mask_opno >= 0 && masked_loop_p)
3884 : {
3885 36 : unsigned int vec_num = vec_oprnds0.length ();
3886 36 : tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
3887 : vec_num, vectype_out, i);
3888 36 : vargs[mask_opno]
3889 72 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
3890 36 : vargs[mask_opno], gsi);
3891 : }
3892 :
3893 2716 : gcall *call;
3894 2716 : if (ifn != IFN_LAST)
3895 2635 : call = gimple_build_call_internal_vec (ifn, vargs);
3896 : else
3897 81 : call = gimple_build_call_vec (fndecl, vargs);
3898 2716 : new_temp = make_ssa_name (vec_dest, call);
3899 2716 : gimple_call_set_lhs (call, new_temp);
3900 2716 : gimple_call_set_nothrow (call, true);
3901 2716 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3902 2716 : new_stmt = call;
3903 : }
3904 2716 : slp_node->push_vec_def (new_stmt);
3905 : }
3906 : }
3907 :
3908 11029 : for (i = 0; i < nargs; i++)
3909 : {
3910 6870 : vec<tree> vec_oprndsi = vec_defs[i];
3911 6870 : vec_oprndsi.release ();
3912 : }
3913 4159 : }
3914 32 : else if (modifier == NARROW)
3915 : {
3916 32 : auto_vec<vec<tree> > vec_defs (nargs);
3917 : /* We don't define any narrowing conditional functions at present. */
3918 32 : gcc_assert (mask_opno < 0);
3919 :
3920 : /* Build argument list for the vectorized call. */
3921 32 : vargs.create (nargs * 2);
3922 :
3923 32 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3924 32 : vec<tree> vec_oprnds0 = vec_defs[0];
3925 :
3926 : /* Arguments are ready. Create the new vector stmt. */
3927 64 : for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
3928 : {
3929 32 : size_t k;
3930 32 : vargs.truncate (0);
3931 64 : for (k = 0; k < nargs; k++)
3932 : {
3933 32 : vec<tree> vec_oprndsk = vec_defs[k];
3934 32 : vargs.quick_push (vec_oprndsk[i]);
3935 32 : vargs.quick_push (vec_oprndsk[i + 1]);
3936 : }
3937 32 : gcall *call;
3938 32 : if (ifn != IFN_LAST)
3939 : call = gimple_build_call_internal_vec (ifn, vargs);
3940 : else
3941 32 : call = gimple_build_call_vec (fndecl, vargs);
3942 32 : new_temp = make_ssa_name (vec_dest, call);
3943 32 : gimple_call_set_lhs (call, new_temp);
3944 32 : gimple_call_set_nothrow (call, true);
3945 32 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3946 32 : slp_node->push_vec_def (call);
3947 : }
3948 :
3949 64 : for (i = 0; i < nargs; i++)
3950 : {
3951 32 : vec<tree> vec_oprndsi = vec_defs[i];
3952 32 : vec_oprndsi.release ();
3953 : }
3954 32 : }
3955 : else
3956 : /* No current target implements this case. */
3957 : return false;
3958 :
3959 4191 : vargs.release ();
3960 :
3961 4191 : return true;
3962 2638161 : }
3963 :
3964 :
3965 : struct simd_call_arg_info
3966 : {
3967 : tree vectype;
3968 : tree op;
3969 : HOST_WIDE_INT linear_step;
3970 : enum vect_def_type dt;
3971 : unsigned int align;
3972 : bool simd_lane_linear;
3973 : };
3974 :
3975 : /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
3976 : is linear within simd lane (but not within whole loop), note it in
3977 : *ARGINFO. */
3978 :
3979 : static void
3980 15 : vect_simd_lane_linear (tree op, class loop *loop,
3981 : struct simd_call_arg_info *arginfo)
3982 : {
3983 15 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
3984 :
3985 15 : if (!is_gimple_assign (def_stmt)
3986 15 : || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
3987 27 : || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
3988 3 : return;
3989 :
3990 12 : tree base = gimple_assign_rhs1 (def_stmt);
3991 12 : HOST_WIDE_INT linear_step = 0;
3992 12 : tree v = gimple_assign_rhs2 (def_stmt);
3993 48 : while (TREE_CODE (v) == SSA_NAME)
3994 : {
3995 36 : tree t;
3996 36 : def_stmt = SSA_NAME_DEF_STMT (v);
3997 36 : if (is_gimple_assign (def_stmt))
3998 24 : switch (gimple_assign_rhs_code (def_stmt))
3999 : {
4000 0 : case PLUS_EXPR:
4001 0 : t = gimple_assign_rhs2 (def_stmt);
4002 0 : if (linear_step || TREE_CODE (t) != INTEGER_CST)
4003 : return;
4004 0 : base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
4005 0 : v = gimple_assign_rhs1 (def_stmt);
4006 0 : continue;
4007 12 : case MULT_EXPR:
4008 12 : t = gimple_assign_rhs2 (def_stmt);
4009 12 : if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
4010 0 : return;
4011 12 : linear_step = tree_to_shwi (t);
4012 12 : v = gimple_assign_rhs1 (def_stmt);
4013 12 : continue;
4014 12 : CASE_CONVERT:
4015 12 : t = gimple_assign_rhs1 (def_stmt);
4016 12 : if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
4017 12 : || (TYPE_PRECISION (TREE_TYPE (v))
4018 12 : < TYPE_PRECISION (TREE_TYPE (t))))
4019 : return;
4020 12 : if (!linear_step)
4021 0 : linear_step = 1;
4022 12 : v = t;
4023 12 : continue;
4024 : default:
4025 : return;
4026 : }
4027 12 : else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
4028 12 : && loop->simduid
4029 12 : && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
4030 24 : && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
4031 : == loop->simduid))
4032 : {
4033 12 : if (!linear_step)
4034 0 : linear_step = 1;
4035 12 : arginfo->linear_step = linear_step;
4036 12 : arginfo->op = base;
4037 12 : arginfo->simd_lane_linear = true;
4038 12 : return;
4039 : }
4040 : }
4041 : }
4042 :
4043 : /* Function vectorizable_simd_clone_call.
4044 :
4045 : Check if STMT_INFO performs a function call that can be vectorized
4046 : by calling a simd clone of the function.
4047 : If COST_VEC is passed, calculate costs but don't change anything,
4048 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
4049 : it, and insert it at GSI.
4050 : Return true if STMT_INFO is vectorizable in this way. */
4051 :
4052 : static bool
4053 2627418 : vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
4054 : gimple_stmt_iterator *gsi,
4055 : slp_tree slp_node,
4056 : stmt_vector_for_cost *cost_vec)
4057 : {
4058 2627418 : tree vec_dest;
4059 2627418 : tree scalar_dest;
4060 2627418 : tree vec_oprnd0 = NULL_TREE;
4061 2627418 : tree vectype;
4062 2627418 : poly_uint64 nunits;
4063 2627418 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
4064 2627418 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
4065 2627418 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
4066 2627418 : tree fndecl, new_temp;
4067 2627418 : int j;
4068 2627418 : auto_vec<simd_call_arg_info> arginfo;
4069 2627418 : vec<tree> vargs = vNULL;
4070 2627418 : size_t i, nargs;
4071 2627418 : tree rtype, ratype;
4072 2627418 : vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
4073 2627418 : int masked_call_offset = 0;
4074 :
4075 : /* Is STMT a vectorizable call? */
4076 2627418 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
4077 16809 : if (!stmt)
4078 : return false;
4079 :
4080 16809 : fndecl = gimple_call_fndecl (stmt);
4081 16809 : if (fndecl == NULL_TREE
4082 16809 : && gimple_call_internal_p (stmt, IFN_MASK_CALL))
4083 : {
4084 220 : fndecl = gimple_call_arg (stmt, 0);
4085 220 : gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
4086 220 : fndecl = TREE_OPERAND (fndecl, 0);
4087 220 : gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
4088 : masked_call_offset = 1;
4089 : }
4090 16589 : if (fndecl == NULL_TREE)
4091 : return false;
4092 :
4093 6321 : struct cgraph_node *node = cgraph_node::get (fndecl);
4094 6321 : if (node == NULL || node->simd_clones == NULL)
4095 : return false;
4096 :
4097 1585 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4098 : return false;
4099 :
4100 1585 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4101 0 : && cost_vec)
4102 : return false;
4103 :
4104 1585 : if (gimple_call_lhs (stmt)
4105 1585 : && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
4106 : return false;
4107 :
4108 1585 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
4109 :
4110 1585 : vectype = SLP_TREE_VECTYPE (slp_node);
4111 :
4112 2627482 : if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
4113 : return false;
4114 :
4115 : /* Process function arguments. */
4116 1585 : nargs = gimple_call_num_args (stmt) - masked_call_offset;
4117 :
4118 : /* Bail out if the function has zero arguments. */
4119 1585 : if (nargs == 0)
4120 : return false;
4121 :
4122 1521 : vect_simd_clone_data _data;
4123 1521 : vect_simd_clone_data &data = slp_node->get_data (_data);
4124 1521 : vec<tree>& simd_clone_info = data.simd_clone_info;
4125 1521 : arginfo.reserve (nargs, true);
4126 1521 : auto_vec<slp_tree> slp_op;
4127 1521 : slp_op.safe_grow_cleared (nargs);
4128 :
4129 4315 : for (i = 0; i < nargs; i++)
4130 : {
4131 2794 : simd_call_arg_info thisarginfo;
4132 2794 : affine_iv iv;
4133 2794 : tree op;
4134 :
4135 2794 : thisarginfo.linear_step = 0;
4136 2794 : thisarginfo.align = 0;
4137 2794 : thisarginfo.op = NULL_TREE;
4138 2794 : thisarginfo.simd_lane_linear = false;
4139 :
4140 5588 : int op_no = vect_slp_child_index_for_operand (stmt_info,
4141 2794 : i + masked_call_offset);
4142 5588 : if (!vect_is_simple_use (vinfo, slp_node,
4143 2794 : op_no, &op, &slp_op[i],
4144 : &thisarginfo.dt, &thisarginfo.vectype)
4145 2794 : || thisarginfo.dt == vect_uninitialized_def)
4146 : {
4147 0 : if (dump_enabled_p ())
4148 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4149 : "use not simple.\n");
4150 0 : return false;
4151 : }
4152 :
4153 2794 : if (thisarginfo.dt == vect_constant_def
4154 2794 : || thisarginfo.dt == vect_external_def)
4155 : {
4156 : /* With SLP we determine the vector type of constants/externals
4157 : at analysis time, handling conflicts via
4158 : vect_maybe_update_slp_op_vectype. At transform time
4159 : we have a vector type recorded for SLP. */
4160 783 : gcc_assert (cost_vec
4161 : || thisarginfo.vectype != NULL_TREE);
4162 : if (cost_vec)
4163 652 : thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
4164 652 : TREE_TYPE (op),
4165 : slp_node);
4166 : }
4167 : else
4168 2011 : gcc_assert (thisarginfo.vectype != NULL_TREE);
4169 :
4170 : /* For linear arguments, the analyze phase should have saved
4171 : the base and step. */
4172 2663 : if (!cost_vec
4173 1586 : && i * 3 + 4 <= simd_clone_info.length ()
4174 2873 : && simd_clone_info[i * 3 + 2])
4175 : {
4176 118 : thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
4177 118 : thisarginfo.op = simd_clone_info[i * 3 + 1];
4178 118 : thisarginfo.simd_lane_linear
4179 118 : = (simd_clone_info[i * 3 + 3] == boolean_true_node);
4180 : /* If loop has been peeled for alignment, we need to adjust it. */
4181 118 : tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
4182 118 : tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
4183 118 : if (n1 != n2 && !thisarginfo.simd_lane_linear)
4184 : {
4185 0 : tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
4186 0 : tree step = simd_clone_info[i * 3 + 2];
4187 0 : tree opt = TREE_TYPE (thisarginfo.op);
4188 0 : bias = fold_convert (TREE_TYPE (step), bias);
4189 0 : bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
4190 0 : thisarginfo.op
4191 0 : = fold_build2 (POINTER_TYPE_P (opt)
4192 : ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
4193 : thisarginfo.op, bias);
4194 : }
4195 : }
4196 2676 : else if (cost_vec
4197 2001 : && thisarginfo.dt != vect_constant_def
4198 1874 : && thisarginfo.dt != vect_external_def
4199 1349 : && loop_vinfo
4200 1344 : && SLP_TREE_LANES (slp_node) == 1
4201 1320 : && TREE_CODE (op) == SSA_NAME
4202 2640 : && simple_iv (loop, loop_containing_stmt (stmt), op,
4203 : &iv, false)
4204 2888 : && tree_fits_shwi_p (iv.step))
4205 : {
4206 212 : thisarginfo.linear_step = tree_to_shwi (iv.step);
4207 212 : thisarginfo.op = iv.base;
4208 : }
4209 2464 : else if ((thisarginfo.dt == vect_constant_def
4210 2464 : || thisarginfo.dt == vect_external_def)
4211 783 : && SLP_TREE_LANES (slp_node) == 1
4212 2770 : && POINTER_TYPE_P (TREE_TYPE (op)))
4213 86 : thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
4214 : /* Addresses of array elements indexed by GOMP_SIMD_LANE are
4215 : linear too. */
4216 2794 : if (SLP_TREE_LANES (slp_node) == 1
4217 2271 : && POINTER_TYPE_P (TREE_TYPE (op))
4218 196 : && !thisarginfo.linear_step
4219 112 : && cost_vec
4220 58 : && thisarginfo.dt != vect_constant_def
4221 58 : && thisarginfo.dt != vect_external_def
4222 15 : && loop_vinfo
4223 2809 : && TREE_CODE (op) == SSA_NAME)
4224 15 : vect_simd_lane_linear (op, loop, &thisarginfo);
4225 :
4226 2794 : if (!vectype)
4227 12 : vectype = thisarginfo.vectype;
4228 2794 : arginfo.quick_push (thisarginfo);
4229 : }
4230 :
4231 1521 : poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
4232 1521 : unsigned group_size = SLP_TREE_LANES (slp_node);
4233 1521 : unsigned int badness = 0;
4234 1521 : unsigned int badness_inbranch = 0;
4235 1521 : struct cgraph_node *bestn = NULL;
4236 1521 : struct cgraph_node *bestn_inbranch = NULL;
4237 1521 : if (!cost_vec)
4238 358 : bestn = ((loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4239 358 : ? data.clone_inbranch : data.clone);
4240 : else
4241 6563 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
4242 5400 : n = n->simdclone->next_clone)
4243 : {
4244 5400 : unsigned int this_badness = 0;
4245 5400 : unsigned int num_calls;
4246 : /* The number of arguments in the call and the number of parameters in
4247 : the simdclone should match. However, when the simdclone is
4248 : 'inbranch', it could have one more parameter than nargs when using
4249 : an inbranch simdclone to call a non-inbranch call, either in a
4250 : non-masked loop using a all true constant mask, or inside a masked
4251 : loop using it's mask. */
4252 5400 : size_t simd_nargs = n->simdclone->nargs;
4253 5400 : if (!masked_call_offset && n->simdclone->inbranch)
4254 2635 : simd_nargs--;
4255 5400 : if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
4256 : &num_calls)
4257 1948 : || (!n->simdclone->inbranch && (masked_call_offset > 0))
4258 1764 : || (nargs != simd_nargs))
4259 3636 : continue;
4260 1764 : if (num_calls != 1)
4261 1162 : this_badness += floor_log2 (num_calls) * 4096;
4262 1764 : if (n->simdclone->inbranch)
4263 729 : this_badness += 8192;
4264 :
4265 : /* If SLP_TREE_VECTYPE has not been set yet pass the general vector
4266 : mode, which for targets that use it will determine what ISA we can
4267 : vectorize this code with. */
4268 1764 : machine_mode vector_mode = vinfo->vector_mode;
4269 1764 : if (vectype)
4270 1764 : vector_mode = TYPE_MODE (vectype);
4271 1764 : int target_badness = targetm.simd_clone.usable (n, vector_mode);
4272 1764 : if (target_badness < 0)
4273 380 : continue;
4274 1384 : this_badness += target_badness * 512;
4275 4184 : for (i = 0; i < nargs; i++)
4276 : {
4277 3048 : switch (n->simdclone->args[i].arg_type)
4278 : {
4279 2118 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4280 2118 : if (VECTOR_BOOLEAN_TYPE_P (n->simdclone->args[i].vector_type))
4281 : /* Vector mask arguments are not supported. */
4282 : i = -1;
4283 2110 : else if (!useless_type_conversion_p
4284 2110 : (n->simdclone->args[i].orig_type,
4285 2110 : TREE_TYPE (gimple_call_arg (stmt,
4286 : i + masked_call_offset))))
4287 : i = -1;
4288 2110 : else if (arginfo[i].dt == vect_constant_def
4289 2003 : || arginfo[i].dt == vect_external_def
4290 4049 : || arginfo[i].linear_step)
4291 399 : this_badness += 64;
4292 : break;
4293 310 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4294 310 : if ((arginfo[i].dt != vect_constant_def
4295 145 : && arginfo[i].dt != vect_external_def)
4296 410 : || SLP_TREE_LANES (slp_node) != 1)
4297 : i = -1;
4298 : break;
4299 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4300 324 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4301 324 : if (arginfo[i].dt == vect_constant_def
4302 324 : || arginfo[i].dt == vect_external_def
4303 324 : || (arginfo[i].linear_step
4304 324 : != n->simdclone->args[i].linear_step))
4305 : i = -1;
4306 : break;
4307 : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4308 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4309 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4310 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4311 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4312 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4313 : /* FORNOW */
4314 : i = -1;
4315 : break;
4316 296 : case SIMD_CLONE_ARG_TYPE_MASK:
4317 296 : if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4318 264 : && n->simdclone->mask_mode != VOIDmode)
4319 : i = -1;
4320 : /* While we can create a traditional data vector from
4321 : an incoming integer mode mask we have no good way to
4322 : force generate an integer mode mask from a traditional
4323 : boolean vector input. */
4324 296 : else if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4325 296 : && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4326 : i = -1;
4327 290 : else if (n->simdclone->mask_mode == VOIDmode
4328 : /* FORNOW we only have partial support for vector-type
4329 : masks that can't hold all of simdlen. */
4330 554 : && (maybe_ne (TYPE_VECTOR_SUBPARTS (n->simdclone->args[i].vector_type),
4331 264 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype))
4332 : /* Verify we can compute the mask argument. */
4333 111 : || !expand_vec_cond_expr_p (n->simdclone->args[i].vector_type,
4334 111 : arginfo[i].vectype)))
4335 : i = -1;
4336 125 : else if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4337 : /* FORNOW we only have partial support for
4338 : integer-type masks that represent the same number
4339 : of lanes as the vectorized mask inputs. */
4340 151 : && maybe_ne (exact_div (n->simdclone->simdlen,
4341 : n->simdclone->args[i].linear_step),
4342 26 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4343 : i = -1;
4344 107 : else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4345 107 : && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4346 8 : this_badness += 2048;
4347 : break;
4348 : }
4349 183 : if (i == (size_t) -1)
4350 : break;
4351 2800 : if (n->simdclone->args[i].alignment > arginfo[i].align)
4352 : {
4353 : i = -1;
4354 : break;
4355 : }
4356 2800 : if (arginfo[i].align)
4357 110 : this_badness += (exact_log2 (arginfo[i].align)
4358 160 : - exact_log2 (n->simdclone->args[i].alignment));
4359 : }
4360 1384 : if (i == (size_t) -1)
4361 248 : continue;
4362 1136 : if (masked_call_offset == 0
4363 1029 : && n->simdclone->inbranch
4364 311 : && n->simdclone->nargs > nargs)
4365 : {
4366 311 : gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
4367 : SIMD_CLONE_ARG_TYPE_MASK);
4368 : /* Penalize using a masked SIMD clone in a non-masked loop, that is
4369 : not in a branch, as we'd have to construct an all-true mask. */
4370 311 : this_badness += 64;
4371 : }
4372 1136 : if (bestn == NULL || this_badness < badness)
4373 : {
4374 815 : bestn = n;
4375 815 : badness = this_badness;
4376 : }
4377 1136 : if (n->simdclone->inbranch
4378 418 : && (bestn_inbranch == NULL || this_badness < badness_inbranch))
4379 : {
4380 5400 : bestn_inbranch = n;
4381 5400 : badness_inbranch = this_badness;
4382 : }
4383 : }
4384 :
4385 1521 : if (bestn == NULL)
4386 : return false;
4387 :
4388 835 : fndecl = bestn->decl;
4389 835 : nunits = bestn->simdclone->simdlen;
4390 835 : int ncopies = vector_unroll_factor (vf * group_size, nunits);
4391 :
4392 : /* If the function isn't const, only allow it in simd loops where user
4393 : has asserted that at least nunits consecutive iterations can be
4394 : performed using SIMD instructions. */
4395 830 : if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
4396 996 : && gimple_vuse (stmt))
4397 : return false;
4398 :
4399 : /* ncopies is the number of SIMD clone calls we create, since simdlen
4400 : is not necessarily matching nunits of the vector types used, track
4401 : that in ncopies_in. */
4402 835 : int ncopies_in = vect_get_num_vectors (vf * group_size, vectype);
4403 :
4404 : /* Sanity check: make sure that at least one copy of the vectorized stmt
4405 : needs to be generated. */
4406 835 : gcc_assert (ncopies >= 1);
4407 :
4408 835 : if (cost_vec) /* transformation not required. */
4409 : {
4410 1578 : for (unsigned i = 0; i < nargs; ++i)
4411 1101 : if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
4412 : {
4413 0 : if (dump_enabled_p ())
4414 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4415 : "incompatible vector types for invariants\n");
4416 0 : return false;
4417 : }
4418 :
4419 477 : if (!bestn_inbranch && loop_vinfo)
4420 : {
4421 270 : if (dump_enabled_p ()
4422 270 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4423 193 : dump_printf_loc (MSG_NOTE, vect_location,
4424 : "can't use a fully-masked loop because no"
4425 : " masked simd clone was available.\n");
4426 270 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
4427 : }
4428 :
4429 : /* When the original call is pure or const but the SIMD ABI dictates
4430 : an aggregate return we will have to use a virtual definition and
4431 : in a loop eventually even need to add a virtual PHI. That's
4432 : not straight-forward so allow to fix this up via renaming. */
4433 477 : if (gimple_call_lhs (stmt)
4434 471 : && !gimple_vdef (stmt)
4435 844 : && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
4436 33 : vinfo->any_known_not_updated_vssa = true;
4437 : /* ??? For SLP code-gen we end up inserting after the last
4438 : vector argument def rather than at the original call position
4439 : so automagic virtual operand updating doesn't work. */
4440 954 : if (gimple_vuse (stmt))
4441 147 : vinfo->any_known_not_updated_vssa = true;
4442 :
4443 477 : data.clone = bestn;
4444 477 : data.clone_inbranch = bestn_inbranch;
4445 :
4446 477 : simd_clone_info.safe_push (NULL_TREE);
4447 1715 : for (i = 0;
4448 2642 : i < (bestn_inbranch ? bestn_inbranch : bestn)->simdclone->nargs; i++)
4449 : {
4450 1238 : if (loop_vinfo
4451 1232 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
4452 458 : && (bestn_inbranch->simdclone->args[i].arg_type
4453 : == SIMD_CLONE_ARG_TYPE_MASK))
4454 : {
4455 162 : if (masked_call_offset)
4456 : /* When there is an explicit mask we require the
4457 : number of elements to match up. */
4458 49 : vect_record_loop_mask (loop_vinfo,
4459 : &LOOP_VINFO_MASKS (loop_vinfo),
4460 : ncopies_in, vectype, NULL_TREE);
4461 : else
4462 : {
4463 : /* When there is no explicit mask on the call we have
4464 : more relaxed requirements. */
4465 113 : tree masktype;
4466 113 : poly_uint64 callee_nelements;
4467 113 : if (SCALAR_INT_MODE_P (bestn_inbranch->simdclone->mask_mode))
4468 : {
4469 12 : callee_nelements
4470 12 : = exact_div (bestn_inbranch->simdclone->simdlen,
4471 : bestn_inbranch->simdclone->args[i].linear_step);
4472 12 : masktype = get_related_vectype_for_scalar_type
4473 12 : (vinfo->vector_mode, TREE_TYPE (vectype),
4474 : callee_nelements);
4475 : }
4476 : else
4477 : {
4478 101 : masktype = bestn_inbranch->simdclone->args[i].vector_type;
4479 : /* The aarch64 port will add custom attributes to types
4480 : for SVE simdclones which make the types different. We
4481 : should use canonincal types for masks within the
4482 : vectorizer, hence we construct the related vectype
4483 : here. */
4484 101 : masktype
4485 : = build_truth_vector_type_for_mode
4486 101 : (TYPE_VECTOR_SUBPARTS (masktype),
4487 101 : TYPE_MODE (masktype));
4488 101 : callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
4489 : }
4490 113 : auto o = vector_unroll_factor (nunits, callee_nelements);
4491 113 : vect_record_loop_mask (loop_vinfo,
4492 : &LOOP_VINFO_MASKS (loop_vinfo),
4493 : ncopies * o, masktype, NULL_TREE);
4494 : }
4495 : }
4496 1076 : else if ((bestn->simdclone->args[i].arg_type
4497 : == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
4498 969 : || (bestn->simdclone->args[i].arg_type
4499 : == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP)
4500 958 : || (bestn_inbranch
4501 352 : && ((bestn_inbranch->simdclone->args[i].arg_type
4502 : == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
4503 352 : || (bestn_inbranch->simdclone->args[i].arg_type
4504 : == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP))))
4505 : {
4506 118 : simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
4507 118 : simd_clone_info.safe_push (arginfo[i].op);
4508 202 : tree lst = (POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4509 202 : ? size_type_node : TREE_TYPE (arginfo[i].op));
4510 118 : tree ls = build_int_cst (lst, arginfo[i].linear_step);
4511 118 : simd_clone_info.safe_push (ls);
4512 118 : tree sll = (arginfo[i].simd_lane_linear
4513 118 : ? boolean_true_node : boolean_false_node);
4514 118 : simd_clone_info.safe_push (sll);
4515 : }
4516 : }
4517 :
4518 477 : SLP_TREE_TYPE (slp_node) = call_simd_clone_vec_info_type;
4519 477 : slp_node->data = new vect_simd_clone_data (std::move (_data));
4520 477 : DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
4521 : /* ??? We're confused by calls w/o LHS. */
4522 477 : if (SLP_TREE_VECTYPE (slp_node))
4523 471 : vect_model_simple_cost (vinfo, ncopies, slp_node, cost_vec);
4524 477 : return true;
4525 : }
4526 :
4527 : /* Transform. */
4528 :
4529 358 : if (dump_enabled_p ())
4530 242 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4531 :
4532 : /* Handle def. */
4533 358 : scalar_dest = gimple_call_lhs (stmt);
4534 358 : vec_dest = NULL_TREE;
4535 358 : rtype = NULL_TREE;
4536 358 : ratype = NULL_TREE;
4537 358 : if (scalar_dest)
4538 : {
4539 352 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
4540 352 : rtype = TREE_TYPE (TREE_TYPE (fndecl));
4541 352 : if (TREE_CODE (rtype) == ARRAY_TYPE)
4542 : {
4543 9 : ratype = rtype;
4544 9 : rtype = TREE_TYPE (ratype);
4545 : }
4546 : }
4547 :
4548 716 : auto_vec<vec<tree> > vec_oprnds;
4549 358 : auto_vec<unsigned> vec_oprnds_i;
4550 358 : vec_oprnds_i.safe_grow_cleared (nargs, true);
4551 358 : vec_oprnds.reserve_exact (nargs);
4552 358 : vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
4553 823 : for (j = 0; j < ncopies; ++j)
4554 : {
4555 465 : poly_uint64 callee_nelements;
4556 465 : poly_uint64 caller_nelements;
4557 : /* Build argument list for the vectorized call. */
4558 465 : if (j == 0)
4559 358 : vargs.create (nargs);
4560 : else
4561 107 : vargs.truncate (0);
4562 :
4563 1568 : for (i = 0; i < nargs; i++)
4564 : {
4565 1103 : unsigned int k, l, m, o;
4566 1103 : tree atype;
4567 1103 : tree op = gimple_call_arg (stmt, i + masked_call_offset);
4568 1103 : switch (bestn->simdclone->args[i].arg_type)
4569 : {
4570 814 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4571 814 : atype = bestn->simdclone->args[i].vector_type;
4572 814 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4573 814 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4574 814 : o = vector_unroll_factor (nunits, callee_nelements);
4575 1858 : for (m = j * o; m < (j + 1) * o; m++)
4576 : {
4577 1044 : if (known_lt (callee_nelements, caller_nelements))
4578 : {
4579 516 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
4580 258 : if (!constant_multiple_p (caller_nelements,
4581 : callee_nelements, &k))
4582 0 : gcc_unreachable ();
4583 :
4584 258 : gcc_assert ((k & (k - 1)) == 0);
4585 258 : if (m == 0)
4586 : {
4587 57 : vec_oprnds_i[i] = 0;
4588 57 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4589 : }
4590 : else
4591 : {
4592 201 : vec_oprnd0 = arginfo[i].op;
4593 201 : if ((m & (k - 1)) == 0)
4594 72 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4595 : }
4596 258 : arginfo[i].op = vec_oprnd0;
4597 258 : vec_oprnd0
4598 258 : = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
4599 258 : bitsize_int (prec),
4600 258 : bitsize_int ((m & (k - 1)) * prec));
4601 258 : gassign *new_stmt
4602 258 : = gimple_build_assign (make_ssa_name (atype),
4603 : vec_oprnd0);
4604 258 : vect_finish_stmt_generation (vinfo, stmt_info,
4605 : new_stmt, gsi);
4606 258 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4607 : }
4608 : else
4609 : {
4610 786 : if (!constant_multiple_p (callee_nelements,
4611 : caller_nelements, &k))
4612 0 : gcc_unreachable ();
4613 786 : gcc_assert ((k & (k - 1)) == 0);
4614 786 : vec<constructor_elt, va_gc> *ctor_elts;
4615 786 : if (k != 1)
4616 12 : vec_alloc (ctor_elts, k);
4617 : else
4618 774 : ctor_elts = NULL;
4619 810 : for (l = 0; l < k; l++)
4620 : {
4621 798 : if (m == 0 && l == 0)
4622 : {
4623 450 : vec_oprnds_i[i] = 0;
4624 450 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4625 : }
4626 : else
4627 348 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4628 798 : arginfo[i].op = vec_oprnd0;
4629 798 : if (k == 1)
4630 : break;
4631 24 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4632 : vec_oprnd0);
4633 : }
4634 786 : if (k == 1)
4635 774 : if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
4636 : atype))
4637 : {
4638 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
4639 : vec_oprnd0);
4640 0 : gassign *new_stmt
4641 0 : = gimple_build_assign (make_ssa_name (atype),
4642 : vec_oprnd0);
4643 0 : vect_finish_stmt_generation (vinfo, stmt_info,
4644 : new_stmt, gsi);
4645 0 : vargs.safe_push (gimple_get_lhs (new_stmt));
4646 : }
4647 : else
4648 774 : vargs.safe_push (vec_oprnd0);
4649 : else
4650 : {
4651 12 : vec_oprnd0 = build_constructor (atype, ctor_elts);
4652 12 : gassign *new_stmt
4653 12 : = gimple_build_assign (make_ssa_name (atype),
4654 : vec_oprnd0);
4655 12 : vect_finish_stmt_generation (vinfo, stmt_info,
4656 : new_stmt, gsi);
4657 12 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4658 : }
4659 : }
4660 : }
4661 : break;
4662 66 : case SIMD_CLONE_ARG_TYPE_MASK:
4663 66 : if (bestn->simdclone->mask_mode == VOIDmode)
4664 : {
4665 60 : atype = bestn->simdclone->args[i].vector_type;
4666 60 : tree elt_type = TREE_TYPE (atype);
4667 60 : tree one = fold_convert (elt_type, integer_one_node);
4668 60 : tree zero = fold_convert (elt_type, integer_zero_node);
4669 60 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4670 60 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4671 60 : o = vector_unroll_factor (nunits, callee_nelements);
4672 120 : for (m = j * o; m < (j + 1) * o; m++)
4673 : {
4674 60 : if (maybe_lt (callee_nelements, caller_nelements))
4675 : {
4676 : /* The mask type has fewer elements than simdlen. */
4677 :
4678 : /* FORNOW */
4679 0 : gcc_unreachable ();
4680 : }
4681 60 : else if (known_eq (callee_nelements, caller_nelements))
4682 : {
4683 : /* The SIMD clone function has the same number of
4684 : elements as the current function. */
4685 60 : if (m == 0)
4686 60 : vec_oprnds_i[i] = 0;
4687 60 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4688 60 : if (loop_vinfo
4689 60 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4690 : {
4691 0 : vec_loop_masks *loop_masks
4692 : = &LOOP_VINFO_MASKS (loop_vinfo);
4693 0 : tree loop_mask
4694 0 : = vect_get_loop_mask (loop_vinfo, gsi,
4695 : loop_masks, ncopies_in,
4696 0 : vectype, j);
4697 0 : vec_oprnd0
4698 0 : = prepare_vec_mask (loop_vinfo,
4699 0 : TREE_TYPE (loop_mask),
4700 : loop_mask, vec_oprnd0,
4701 : gsi);
4702 0 : loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
4703 : loop_mask });
4704 :
4705 : }
4706 60 : vec_oprnd0
4707 60 : = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
4708 : build_vector_from_val (atype, one),
4709 : build_vector_from_val (atype, zero));
4710 60 : gassign *new_stmt
4711 60 : = gimple_build_assign (make_ssa_name (atype),
4712 : vec_oprnd0);
4713 60 : vect_finish_stmt_generation (vinfo, stmt_info,
4714 : new_stmt, gsi);
4715 60 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4716 : }
4717 : else
4718 : {
4719 : /* The mask type has more elements than simdlen. */
4720 :
4721 : /* FORNOW */
4722 0 : gcc_unreachable ();
4723 : }
4724 : }
4725 : }
4726 6 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4727 : {
4728 6 : atype = bestn->simdclone->args[i].vector_type;
4729 6 : poly_uint64 atype_subparts
4730 6 : = exact_div (bestn->simdclone->simdlen,
4731 : bestn->simdclone->args[i].linear_step);
4732 6 : o = bestn->simdclone->args[i].linear_step;
4733 12 : for (m = j * o; m < (j + 1) * o; m++)
4734 : {
4735 6 : if (m == 0)
4736 6 : vec_oprnds_i[i] = 0;
4737 6 : if (maybe_lt (atype_subparts,
4738 6 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4739 : {
4740 : /* The mask argument has fewer elements than the
4741 : input vector. */
4742 : /* FORNOW */
4743 0 : gcc_unreachable ();
4744 : }
4745 6 : else if (known_eq (atype_subparts,
4746 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4747 : {
4748 6 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4749 6 : if (loop_vinfo
4750 6 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4751 : {
4752 1 : vec_loop_masks *loop_masks
4753 : = &LOOP_VINFO_MASKS (loop_vinfo);
4754 1 : tree loop_mask
4755 1 : = vect_get_loop_mask (loop_vinfo, gsi,
4756 : loop_masks, ncopies_in,
4757 : vectype, j);
4758 1 : vec_oprnd0
4759 1 : = prepare_vec_mask (loop_vinfo,
4760 1 : TREE_TYPE (loop_mask),
4761 : loop_mask, vec_oprnd0,
4762 : gsi);
4763 : }
4764 : /* The vector mask argument matches the input
4765 : in the number of lanes, but not necessarily
4766 : in the mode. */
4767 6 : tree st = lang_hooks.types.type_for_mode
4768 6 : (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
4769 6 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
4770 : vec_oprnd0);
4771 6 : gassign *new_stmt
4772 6 : = gimple_build_assign (make_ssa_name (st),
4773 : vec_oprnd0);
4774 6 : vect_finish_stmt_generation (vinfo, stmt_info,
4775 : new_stmt, gsi);
4776 6 : if (!types_compatible_p (atype, st))
4777 : {
4778 6 : new_stmt
4779 6 : = gimple_build_assign (make_ssa_name (atype),
4780 : NOP_EXPR,
4781 : gimple_assign_lhs
4782 : (new_stmt));
4783 6 : vect_finish_stmt_generation (vinfo, stmt_info,
4784 : new_stmt, gsi);
4785 : }
4786 6 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4787 : }
4788 : else
4789 : {
4790 : /* The mask argument has more elements than the
4791 : input vector. */
4792 : /* FORNOW */
4793 0 : gcc_unreachable ();
4794 : }
4795 : }
4796 : }
4797 : else
4798 0 : gcc_unreachable ();
4799 : break;
4800 102 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4801 102 : vargs.safe_push (op);
4802 102 : break;
4803 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4804 121 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4805 121 : if (j == 0)
4806 : {
4807 118 : gimple_seq stmts;
4808 118 : arginfo[i].op
4809 118 : = force_gimple_operand (unshare_expr (arginfo[i].op),
4810 : &stmts, true, NULL_TREE);
4811 118 : if (stmts != NULL)
4812 : {
4813 0 : basic_block new_bb;
4814 0 : edge pe = loop_preheader_edge (loop);
4815 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4816 0 : gcc_assert (!new_bb);
4817 : }
4818 118 : if (arginfo[i].simd_lane_linear)
4819 : {
4820 6 : vargs.safe_push (arginfo[i].op);
4821 6 : break;
4822 : }
4823 112 : tree phi_res = copy_ssa_name (op);
4824 112 : gphi *new_phi = create_phi_node (phi_res, loop->header);
4825 112 : add_phi_arg (new_phi, arginfo[i].op,
4826 : loop_preheader_edge (loop), UNKNOWN_LOCATION);
4827 112 : enum tree_code code
4828 196 : = POINTER_TYPE_P (TREE_TYPE (op))
4829 112 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4830 196 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4831 196 : ? sizetype : TREE_TYPE (op);
4832 112 : poly_widest_int cst
4833 112 : = wi::mul (bestn->simdclone->args[i].linear_step,
4834 112 : ncopies * nunits);
4835 112 : tree tcst = wide_int_to_tree (type, cst);
4836 112 : tree phi_arg = copy_ssa_name (op);
4837 112 : gassign *new_stmt
4838 112 : = gimple_build_assign (phi_arg, code, phi_res, tcst);
4839 112 : gimple_stmt_iterator si = gsi_after_labels (loop->header);
4840 112 : gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
4841 112 : add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
4842 : UNKNOWN_LOCATION);
4843 112 : arginfo[i].op = phi_res;
4844 112 : vargs.safe_push (phi_res);
4845 112 : }
4846 : else
4847 : {
4848 3 : enum tree_code code
4849 6 : = POINTER_TYPE_P (TREE_TYPE (op))
4850 3 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4851 6 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4852 6 : ? sizetype : TREE_TYPE (op);
4853 3 : poly_widest_int cst
4854 3 : = wi::mul (bestn->simdclone->args[i].linear_step,
4855 3 : j * nunits);
4856 3 : tree tcst = wide_int_to_tree (type, cst);
4857 3 : new_temp = make_ssa_name (TREE_TYPE (op));
4858 3 : gassign *new_stmt
4859 6 : = gimple_build_assign (new_temp, code,
4860 3 : arginfo[i].op, tcst);
4861 3 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4862 3 : vargs.safe_push (new_temp);
4863 3 : }
4864 : break;
4865 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4866 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4867 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4868 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4869 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4870 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4871 0 : default:
4872 0 : gcc_unreachable ();
4873 : }
4874 : }
4875 :
4876 465 : if (masked_call_offset == 0
4877 399 : && bestn->simdclone->inbranch
4878 13 : && bestn->simdclone->nargs > nargs)
4879 : {
4880 13 : unsigned long m, o;
4881 13 : size_t mask_i = bestn->simdclone->nargs - 1;
4882 13 : tree mask;
4883 13 : gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
4884 : SIMD_CLONE_ARG_TYPE_MASK);
4885 :
4886 13 : tree mask_argtype = bestn->simdclone->args[mask_i].vector_type;
4887 13 : tree mask_vectype;
4888 13 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4889 : {
4890 2 : callee_nelements = exact_div (bestn->simdclone->simdlen,
4891 : bestn->simdclone->args[i].linear_step);
4892 2 : mask_vectype = get_related_vectype_for_scalar_type
4893 2 : (vinfo->vector_mode, TREE_TYPE (vectype), callee_nelements);
4894 : }
4895 : else
4896 : {
4897 11 : mask_vectype = mask_argtype;
4898 11 : callee_nelements = TYPE_VECTOR_SUBPARTS (mask_vectype);
4899 : }
4900 13 : o = vector_unroll_factor (nunits, callee_nelements);
4901 26 : for (m = j * o; m < (j + 1) * o; m++)
4902 : {
4903 13 : if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4904 : {
4905 1 : vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
4906 1 : mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
4907 : ncopies * o, mask_vectype, m);
4908 : }
4909 : else
4910 12 : mask = vect_build_all_ones_mask (vinfo, stmt_info,
4911 : mask_argtype);
4912 :
4913 13 : gassign *new_stmt;
4914 13 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4915 : {
4916 : /* This means we are dealing with integer mask modes.
4917 : First convert to an integer type with the same size as
4918 : the current vector type. */
4919 2 : unsigned HOST_WIDE_INT intermediate_size
4920 2 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
4921 2 : tree mid_int_type =
4922 2 : build_nonstandard_integer_type (intermediate_size, 1);
4923 2 : mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
4924 2 : new_stmt
4925 2 : = gimple_build_assign (make_ssa_name (mid_int_type),
4926 : mask);
4927 2 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4928 : /* Then zero-extend to the mask mode. */
4929 2 : mask = fold_build1 (NOP_EXPR, mask_argtype,
4930 : gimple_get_lhs (new_stmt));
4931 : }
4932 11 : else if (bestn->simdclone->mask_mode == VOIDmode)
4933 11 : mask = build3 (VEC_COND_EXPR, mask_argtype, mask,
4934 : build_one_cst (mask_argtype),
4935 : build_zero_cst (mask_argtype));
4936 : else
4937 0 : gcc_unreachable ();
4938 :
4939 13 : new_stmt = gimple_build_assign (make_ssa_name (mask_argtype),
4940 : mask);
4941 13 : vect_finish_stmt_generation (vinfo, stmt_info,
4942 : new_stmt, gsi);
4943 13 : mask = gimple_assign_lhs (new_stmt);
4944 13 : vargs.safe_push (mask);
4945 : }
4946 : }
4947 :
4948 465 : gcall *new_call = gimple_build_call_vec (fndecl, vargs);
4949 465 : if (vec_dest)
4950 : {
4951 459 : gcc_assert (ratype
4952 : || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
4953 459 : if (ratype)
4954 15 : new_temp = create_tmp_var (ratype);
4955 444 : else if (useless_type_conversion_p (vectype, rtype))
4956 424 : new_temp = make_ssa_name (vec_dest, new_call);
4957 : else
4958 20 : new_temp = make_ssa_name (rtype, new_call);
4959 459 : gimple_call_set_lhs (new_call, new_temp);
4960 : }
4961 465 : vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
4962 465 : gimple *new_stmt = new_call;
4963 :
4964 465 : if (vec_dest)
4965 : {
4966 459 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
4967 : {
4968 19 : unsigned int k, l;
4969 38 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
4970 38 : poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
4971 19 : k = vector_unroll_factor (nunits,
4972 : TYPE_VECTOR_SUBPARTS (vectype));
4973 19 : gcc_assert ((k & (k - 1)) == 0);
4974 69 : for (l = 0; l < k; l++)
4975 : {
4976 50 : tree t;
4977 50 : if (ratype)
4978 : {
4979 42 : t = build_fold_addr_expr (new_temp);
4980 42 : t = build2 (MEM_REF, vectype, t,
4981 42 : build_int_cst (TREE_TYPE (t), l * bytes));
4982 : }
4983 : else
4984 8 : t = build3 (BIT_FIELD_REF, vectype, new_temp,
4985 8 : bitsize_int (prec), bitsize_int (l * prec));
4986 50 : new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
4987 50 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4988 :
4989 50 : SLP_TREE_VEC_DEFS (slp_node)
4990 50 : .quick_push (gimple_assign_lhs (new_stmt));
4991 : }
4992 :
4993 19 : if (ratype)
4994 15 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
4995 19 : continue;
4996 19 : }
4997 440 : else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
4998 : {
4999 16 : unsigned int k;
5000 16 : if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
5001 16 : TYPE_VECTOR_SUBPARTS (rtype), &k))
5002 0 : gcc_unreachable ();
5003 16 : gcc_assert ((k & (k - 1)) == 0);
5004 16 : if ((j & (k - 1)) == 0)
5005 8 : vec_alloc (ret_ctor_elts, k);
5006 16 : if (ratype)
5007 : {
5008 0 : unsigned int m, o;
5009 0 : o = vector_unroll_factor (nunits,
5010 : TYPE_VECTOR_SUBPARTS (rtype));
5011 0 : for (m = 0; m < o; m++)
5012 : {
5013 0 : tree tem = build4 (ARRAY_REF, rtype, new_temp,
5014 0 : size_int (m), NULL_TREE, NULL_TREE);
5015 0 : new_stmt = gimple_build_assign (make_ssa_name (rtype),
5016 : tem);
5017 0 : vect_finish_stmt_generation (vinfo, stmt_info,
5018 : new_stmt, gsi);
5019 0 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
5020 : gimple_assign_lhs (new_stmt));
5021 : }
5022 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5023 : }
5024 : else
5025 16 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
5026 16 : if ((j & (k - 1)) != k - 1)
5027 8 : continue;
5028 8 : vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
5029 8 : new_stmt
5030 8 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5031 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5032 :
5033 8 : SLP_TREE_VEC_DEFS (slp_node)
5034 8 : .quick_push (gimple_assign_lhs (new_stmt));
5035 8 : continue;
5036 8 : }
5037 424 : else if (ratype)
5038 : {
5039 0 : tree t = build_fold_addr_expr (new_temp);
5040 0 : t = build2 (MEM_REF, vectype, t,
5041 0 : build_int_cst (TREE_TYPE (t), 0));
5042 0 : new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
5043 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5044 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5045 : }
5046 424 : else if (!useless_type_conversion_p (vectype, rtype))
5047 : {
5048 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
5049 0 : new_stmt
5050 0 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5051 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5052 : }
5053 : }
5054 :
5055 430 : if (gimple_get_lhs (new_stmt))
5056 424 : SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
5057 : }
5058 :
5059 1151 : for (i = 0; i < nargs; ++i)
5060 : {
5061 793 : vec<tree> oprndsi = vec_oprnds[i];
5062 793 : oprndsi.release ();
5063 : }
5064 358 : vargs.release ();
5065 :
5066 : /* Mark the clone as no longer being a candidate for GC. */
5067 358 : bestn->gc_candidate = false;
5068 :
5069 358 : return true;
5070 1521 : }
5071 :
5072 :
5073 : /* Function vect_gen_widened_results_half
5074 :
5075 : Create a vector stmt whose code, type, number of arguments, and result
5076 : variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
5077 : VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at GSI.
5078 : In the case that CODE is a CALL_EXPR, this means that a call to DECL
5079 : needs to be created (DECL is a function-decl of a target-builtin).
5080 : STMT_INFO is the original scalar stmt that we are vectorizing. */
5081 :
5082 : static gimple *
5083 32328 : vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
5084 : tree vec_oprnd0, tree vec_oprnd1, int op_type,
5085 : tree vec_dest, gimple_stmt_iterator *gsi,
5086 : stmt_vec_info stmt_info)
5087 : {
5088 32328 : gimple *new_stmt;
5089 32328 : tree new_temp;
5090 :
5091 : /* Generate half of the widened result: */
5092 32328 : if (op_type != binary_op)
5093 31216 : vec_oprnd1 = NULL;
5094 32328 : new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
5095 32328 : new_temp = make_ssa_name (vec_dest, new_stmt);
5096 32328 : gimple_set_lhs (new_stmt, new_temp);
5097 32328 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5098 :
5099 32328 : return new_stmt;
5100 : }
5101 :
5102 :
5103 : /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
5104 : For multi-step conversions store the resulting vectors and call the function
5105 : recursively. When NARROW_SRC_P is true, there's still a conversion after
5106 : narrowing, don't store the vectors in the SLP_NODE or in vector info of
5107 : the scalar statement(or in STMT_VINFO_RELATED_STMT chain). */
5108 :
5109 : static void
5110 12134 : vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
5111 : int multi_step_cvt,
5112 : stmt_vec_info stmt_info,
5113 : vec<tree> &vec_dsts,
5114 : gimple_stmt_iterator *gsi,
5115 : slp_tree slp_node, code_helper code,
5116 : bool narrow_src_p)
5117 : {
5118 12134 : unsigned int i;
5119 12134 : tree vop0, vop1, new_tmp, vec_dest;
5120 :
5121 12134 : vec_dest = vec_dsts.pop ();
5122 :
5123 28724 : for (i = 0; i < vec_oprnds->length (); i += 2)
5124 : {
5125 : /* Create demotion operation. */
5126 16590 : vop0 = (*vec_oprnds)[i];
5127 16590 : vop1 = (*vec_oprnds)[i + 1];
5128 16590 : gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
5129 16590 : new_tmp = make_ssa_name (vec_dest, new_stmt);
5130 16590 : gimple_set_lhs (new_stmt, new_tmp);
5131 16590 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5132 16590 : if (multi_step_cvt || narrow_src_p)
5133 : /* Store the resulting vector for next recursive call,
5134 : or return the resulting vector_tmp for NARROW FLOAT_EXPR. */
5135 6794 : (*vec_oprnds)[i/2] = new_tmp;
5136 : else
5137 : {
5138 : /* This is the last step of the conversion sequence. Store the
5139 : vectors in SLP_NODE. */
5140 9796 : slp_node->push_vec_def (new_stmt);
5141 : }
5142 : }
5143 :
5144 : /* For multi-step demotion operations we first generate demotion operations
5145 : from the source type to the intermediate types, and then combine the
5146 : results (stored in VEC_OPRNDS) in demotion operation to the destination
5147 : type. */
5148 12134 : if (multi_step_cvt)
5149 : {
5150 : /* At each level of recursion we have half of the operands we had at the
5151 : previous level. */
5152 3013 : vec_oprnds->truncate ((i+1)/2);
5153 3013 : vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
5154 : multi_step_cvt - 1,
5155 : stmt_info, vec_dsts, gsi,
5156 3013 : slp_node, VEC_PACK_TRUNC_EXPR,
5157 : narrow_src_p);
5158 : }
5159 :
5160 12134 : vec_dsts.quick_push (vec_dest);
5161 12134 : }
5162 :
5163 :
5164 : /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
5165 : and VEC_OPRNDS1, for a binary operation associated with scalar statement
5166 : STMT_INFO. For multi-step conversions store the resulting vectors and
5167 : call the function recursively. */
5168 :
5169 : static void
5170 11782 : vect_create_vectorized_promotion_stmts (vec_info *vinfo,
5171 : vec<tree> *vec_oprnds0,
5172 : vec<tree> *vec_oprnds1,
5173 : stmt_vec_info stmt_info, tree vec_dest,
5174 : gimple_stmt_iterator *gsi,
5175 : code_helper ch1,
5176 : code_helper ch2, int op_type)
5177 : {
5178 11782 : int i;
5179 11782 : tree vop0, vop1, new_tmp1, new_tmp2;
5180 11782 : gimple *new_stmt1, *new_stmt2;
5181 11782 : vec<tree> vec_tmp = vNULL;
5182 :
5183 11782 : vec_tmp.create (vec_oprnds0->length () * 2);
5184 39728 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5185 : {
5186 16164 : if (op_type == binary_op)
5187 556 : vop1 = (*vec_oprnds1)[i];
5188 : else
5189 : vop1 = NULL_TREE;
5190 :
5191 : /* Generate the two halves of promotion operation. */
5192 16164 : new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
5193 : op_type, vec_dest, gsi,
5194 : stmt_info);
5195 16164 : new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
5196 : op_type, vec_dest, gsi,
5197 : stmt_info);
5198 16164 : if (is_gimple_call (new_stmt1))
5199 : {
5200 0 : new_tmp1 = gimple_call_lhs (new_stmt1);
5201 0 : new_tmp2 = gimple_call_lhs (new_stmt2);
5202 : }
5203 : else
5204 : {
5205 16164 : new_tmp1 = gimple_assign_lhs (new_stmt1);
5206 16164 : new_tmp2 = gimple_assign_lhs (new_stmt2);
5207 : }
5208 :
5209 : /* Store the results for the next step. */
5210 16164 : vec_tmp.quick_push (new_tmp1);
5211 16164 : vec_tmp.quick_push (new_tmp2);
5212 : }
5213 :
5214 11782 : vec_oprnds0->release ();
5215 11782 : *vec_oprnds0 = vec_tmp;
5216 11782 : }
5217 :
5218 : /* Create vectorized promotion stmts for widening stmts using only half the
5219 : potential vector size for input. */
5220 : static void
5221 60 : vect_create_half_widening_stmts (vec_info *vinfo,
5222 : vec<tree> *vec_oprnds0,
5223 : vec<tree> *vec_oprnds1,
5224 : stmt_vec_info stmt_info, tree vec_dest,
5225 : gimple_stmt_iterator *gsi,
5226 : code_helper code1,
5227 : int op_type)
5228 : {
5229 60 : int i;
5230 60 : tree vop0, vop1;
5231 60 : gimple *new_stmt1;
5232 60 : gimple *new_stmt2;
5233 60 : gimple *new_stmt3;
5234 60 : vec<tree> vec_tmp = vNULL;
5235 :
5236 60 : vec_tmp.create (vec_oprnds0->length ());
5237 120 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5238 : {
5239 60 : tree new_tmp1, new_tmp2, new_tmp3, out_type;
5240 :
5241 60 : gcc_assert (op_type == binary_op);
5242 60 : vop1 = (*vec_oprnds1)[i];
5243 :
5244 : /* Widen the first vector input. */
5245 60 : out_type = TREE_TYPE (vec_dest);
5246 60 : new_tmp1 = make_ssa_name (out_type);
5247 60 : new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
5248 60 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
5249 60 : if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
5250 : {
5251 : /* Widen the second vector input. */
5252 60 : new_tmp2 = make_ssa_name (out_type);
5253 60 : new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
5254 60 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
5255 : /* Perform the operation. With both vector inputs widened. */
5256 60 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
5257 : }
5258 : else
5259 : {
5260 : /* Perform the operation. With the single vector input widened. */
5261 0 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
5262 : }
5263 :
5264 60 : new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
5265 60 : gimple_assign_set_lhs (new_stmt3, new_tmp3);
5266 60 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
5267 :
5268 : /* Store the results for the next step. */
5269 60 : vec_tmp.quick_push (new_tmp3);
5270 : }
5271 :
5272 60 : vec_oprnds0->release ();
5273 60 : *vec_oprnds0 = vec_tmp;
5274 60 : }
5275 :
5276 :
5277 : /* Check if STMT_INFO performs a conversion operation that can be vectorized.
5278 : If COST_VEC is passed, calculate costs but don't change anything,
5279 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5280 : it, and insert it at GSI.
5281 : Return true if STMT_INFO is vectorizable in this way. */
5282 :
5283 : static bool
5284 2650151 : vectorizable_conversion (vec_info *vinfo,
5285 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5286 : slp_tree slp_node,
5287 : stmt_vector_for_cost *cost_vec)
5288 : {
5289 2650151 : tree vec_dest, cvt_op = NULL_TREE;
5290 2650151 : tree scalar_dest;
5291 2650151 : tree op0, op1 = NULL_TREE;
5292 2650151 : tree_code tc1;
5293 2650151 : code_helper code, code1, code2;
5294 2650151 : code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
5295 2650151 : tree new_temp;
5296 2650151 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
5297 2650151 : poly_uint64 nunits_in;
5298 2650151 : poly_uint64 nunits_out;
5299 2650151 : tree vectype_out, vectype_in;
5300 2650151 : int i;
5301 2650151 : tree lhs_type, rhs_type;
5302 : /* For conversions between floating point and integer, there're 2 NARROW
5303 : cases. NARROW_SRC is for FLOAT_EXPR, means
5304 : integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
5305 : This is safe when the range of the source integer can fit into the lower
5306 : precision. NARROW_DST is for FIX_TRUNC_EXPR, means
5307 : floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
5308 : For other conversions, when there's narrowing, NARROW_DST is used as
5309 : default. */
5310 2650151 : enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
5311 2650151 : vec<tree> vec_oprnds0 = vNULL;
5312 2650151 : vec<tree> vec_oprnds1 = vNULL;
5313 2650151 : tree vop0;
5314 2650151 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5315 2650151 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5316 2650151 : int multi_step_cvt = 0;
5317 2650151 : vec<tree> interm_types = vNULL;
5318 2650151 : tree intermediate_type, cvt_type = NULL_TREE;
5319 2650151 : int op_type;
5320 2650151 : unsigned short fltsz;
5321 :
5322 : /* Is STMT a vectorizable conversion? */
5323 :
5324 2650151 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5325 : return false;
5326 :
5327 2650151 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5328 243533 : && cost_vec)
5329 : return false;
5330 :
5331 2406618 : gimple* stmt = stmt_info->stmt;
5332 2406618 : if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
5333 : return false;
5334 :
5335 2344495 : if (gimple_get_lhs (stmt) == NULL_TREE
5336 2344495 : || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5337 827022 : return false;
5338 :
5339 1517473 : if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5340 : return false;
5341 :
5342 1517473 : if (is_gimple_assign (stmt))
5343 : {
5344 1504010 : code = gimple_assign_rhs_code (stmt);
5345 1504010 : op_type = TREE_CODE_LENGTH ((tree_code) code);
5346 : }
5347 13463 : else if (gimple_call_internal_p (stmt))
5348 : {
5349 7950 : code = gimple_call_internal_fn (stmt);
5350 7950 : op_type = gimple_call_num_args (stmt);
5351 : }
5352 : else
5353 : return false;
5354 :
5355 1511960 : bool widen_arith = (code == WIDEN_MULT_EXPR
5356 1509349 : || code == WIDEN_LSHIFT_EXPR
5357 3021309 : || widening_fn_p (code));
5358 :
5359 1509349 : if (!widen_arith
5360 1509349 : && !CONVERT_EXPR_CODE_P (code)
5361 1336480 : && code != FIX_TRUNC_EXPR
5362 1334652 : && code != FLOAT_EXPR)
5363 : return false;
5364 :
5365 : /* Check types of lhs and rhs. */
5366 198599 : scalar_dest = gimple_get_lhs (stmt);
5367 198599 : lhs_type = TREE_TYPE (scalar_dest);
5368 198599 : vectype_out = SLP_TREE_VECTYPE (slp_node);
5369 :
5370 : /* Check the operands of the operation. */
5371 198599 : slp_tree slp_op0, slp_op1 = NULL;
5372 198599 : if (!vect_is_simple_use (vinfo, slp_node,
5373 : 0, &op0, &slp_op0, &dt[0], &vectype_in))
5374 : {
5375 0 : if (dump_enabled_p ())
5376 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5377 : "use not simple.\n");
5378 0 : return false;
5379 : }
5380 :
5381 198599 : rhs_type = TREE_TYPE (op0);
5382 196771 : if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
5383 377900 : && !((INTEGRAL_TYPE_P (lhs_type)
5384 164543 : && INTEGRAL_TYPE_P (rhs_type))
5385 : || (SCALAR_FLOAT_TYPE_P (lhs_type)
5386 9807 : && SCALAR_FLOAT_TYPE_P (rhs_type))))
5387 : return false;
5388 :
5389 193648 : if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5390 173474 : && INTEGRAL_TYPE_P (lhs_type)
5391 336024 : && !type_has_mode_precision_p (lhs_type))
5392 : {
5393 529 : if (dump_enabled_p ())
5394 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5395 : "type conversion to bit-precision unsupported\n");
5396 529 : return false;
5397 : }
5398 :
5399 193119 : if (op_type == binary_op)
5400 : {
5401 2611 : gcc_assert (code == WIDEN_MULT_EXPR
5402 : || code == WIDEN_LSHIFT_EXPR
5403 : || widening_fn_p (code));
5404 :
5405 2611 : op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
5406 0 : gimple_call_arg (stmt, 0);
5407 2611 : tree vectype1_in;
5408 2611 : if (!vect_is_simple_use (vinfo, slp_node, 1,
5409 : &op1, &slp_op1, &dt[1], &vectype1_in))
5410 : {
5411 0 : if (dump_enabled_p ())
5412 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5413 : "use not simple.\n");
5414 0 : return false;
5415 : }
5416 : /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
5417 : OP1. */
5418 2611 : if (!vectype_in)
5419 187 : vectype_in = vectype1_in;
5420 : }
5421 :
5422 : /* If op0 is an external or constant def, infer the vector type
5423 : from the scalar type. */
5424 193119 : if (!vectype_in)
5425 24799 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
5426 193119 : if (!cost_vec)
5427 23568 : gcc_assert (vectype_in);
5428 193119 : if (!vectype_in)
5429 : {
5430 266 : if (dump_enabled_p ())
5431 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5432 : "no vectype for scalar type %T\n", rhs_type);
5433 :
5434 266 : return false;
5435 : }
5436 :
5437 385706 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
5438 192853 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
5439 : {
5440 304 : if (dump_enabled_p ())
5441 36 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5442 : "can't convert between boolean and non "
5443 : "boolean vectors %T\n", rhs_type);
5444 :
5445 304 : return false;
5446 : }
5447 :
5448 192549 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
5449 192549 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5450 192549 : if (known_eq (nunits_out, nunits_in))
5451 91906 : if (widen_arith)
5452 : modifier = WIDEN;
5453 : else
5454 192549 : modifier = NONE;
5455 100643 : else if (multiple_p (nunits_out, nunits_in))
5456 : modifier = NARROW_DST;
5457 : else
5458 : {
5459 57120 : gcc_checking_assert (multiple_p (nunits_in, nunits_out));
5460 : modifier = WIDEN;
5461 : }
5462 :
5463 192549 : bool found_mode = false;
5464 192549 : scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
5465 192549 : scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
5466 192549 : opt_scalar_mode rhs_mode_iter;
5467 192549 : auto_vec<std::pair<tree, tree_code>, 2> converts;
5468 192549 : bool evenodd_ok = false;
5469 :
5470 : /* Supportable by target? */
5471 192549 : switch (modifier)
5472 : {
5473 91491 : case NONE:
5474 91491 : if (code != FIX_TRUNC_EXPR
5475 90379 : && code != FLOAT_EXPR
5476 170544 : && !CONVERT_EXPR_CODE_P (code))
5477 : return false;
5478 91491 : gcc_assert (code.is_tree_code ());
5479 91491 : if (supportable_indirect_convert_operation (code,
5480 : vectype_out, vectype_in,
5481 : converts, op0, slp_op0))
5482 : {
5483 21225 : gcc_assert (converts.length () <= 2);
5484 21225 : if (converts.length () == 1)
5485 21151 : code1 = converts[0].second;
5486 : else
5487 : {
5488 74 : cvt_type = NULL_TREE;
5489 74 : multi_step_cvt = converts.length () - 1;
5490 74 : codecvt1 = converts[0].second;
5491 74 : code1 = converts[1].second;
5492 74 : interm_types.safe_push (converts[0].first);
5493 : }
5494 : break;
5495 : }
5496 :
5497 : /* FALLTHRU */
5498 70266 : unsupported:
5499 82798 : if (dump_enabled_p ())
5500 6373 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5501 : "conversion not supported by target.\n");
5502 : return false;
5503 :
5504 57535 : case WIDEN:
5505 57535 : if (known_eq (nunits_in, nunits_out))
5506 : {
5507 830 : if (!(code.is_tree_code ()
5508 415 : && supportable_half_widening_operation ((tree_code) code,
5509 : vectype_out, vectype_in,
5510 : &tc1)))
5511 129 : goto unsupported;
5512 286 : code1 = tc1;
5513 286 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5514 : break;
5515 : }
5516 : /* Elements in a vector can only be reordered if used in a reduction
5517 : operation only. */
5518 57120 : if (code == WIDEN_MULT_EXPR
5519 2196 : && loop_vinfo
5520 2059 : && !nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo), stmt_info)
5521 : /* For a SLP reduction we cannot swizzle lanes, detecting a
5522 : reduction chain isn't possible here. */
5523 59157 : && SLP_TREE_LANES (slp_node) == 1)
5524 : {
5525 : /* ??? There is no way to look for SLP uses, so work on
5526 : the stmt and what the stmt-based cycle detection gives us. */
5527 1935 : tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
5528 1935 : stmt_vec_info use_stmt_info
5529 1935 : = lhs ? loop_vinfo->lookup_single_use (lhs) : NULL;
5530 1935 : if (use_stmt_info
5531 1786 : && STMT_VINFO_REDUC_DEF (use_stmt_info))
5532 57120 : evenodd_ok = true;
5533 : }
5534 57120 : if (supportable_widening_operation (code, vectype_out, vectype_in,
5535 : evenodd_ok, &code1,
5536 : &code2, &multi_step_cvt,
5537 : &interm_types))
5538 : {
5539 : /* Binary widening operation can only be supported directly by the
5540 : architecture. */
5541 50755 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5542 : break;
5543 : }
5544 :
5545 6365 : if (code != FLOAT_EXPR
5546 6737 : || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
5547 6179 : goto unsupported;
5548 :
5549 186 : fltsz = GET_MODE_SIZE (lhs_mode);
5550 273 : FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
5551 : {
5552 273 : rhs_mode = rhs_mode_iter.require ();
5553 546 : if (GET_MODE_SIZE (rhs_mode) > fltsz)
5554 : break;
5555 :
5556 273 : cvt_type
5557 273 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5558 273 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5559 273 : if (cvt_type == NULL_TREE)
5560 0 : goto unsupported;
5561 :
5562 546 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5563 : {
5564 81 : tc1 = ERROR_MARK;
5565 81 : gcc_assert (code.is_tree_code ());
5566 81 : if (!supportable_convert_operation ((tree_code) code, vectype_out,
5567 : cvt_type, &tc1))
5568 22 : goto unsupported;
5569 59 : codecvt1 = tc1;
5570 : }
5571 192 : else if (!supportable_widening_operation (code, vectype_out,
5572 : cvt_type, evenodd_ok,
5573 : &codecvt1,
5574 : &codecvt2, &multi_step_cvt,
5575 : &interm_types))
5576 87 : continue;
5577 : else
5578 105 : gcc_assert (multi_step_cvt == 0);
5579 :
5580 164 : if (supportable_widening_operation (NOP_EXPR, cvt_type,
5581 : vectype_in, evenodd_ok, &code1,
5582 : &code2, &multi_step_cvt,
5583 : &interm_types))
5584 : {
5585 : found_mode = true;
5586 : break;
5587 : }
5588 : }
5589 :
5590 164 : if (!found_mode)
5591 0 : goto unsupported;
5592 :
5593 328 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5594 59 : codecvt2 = ERROR_MARK;
5595 : else
5596 : {
5597 105 : multi_step_cvt++;
5598 105 : interm_types.safe_push (cvt_type);
5599 105 : cvt_type = NULL_TREE;
5600 : }
5601 : break;
5602 :
5603 43523 : case NARROW_DST:
5604 43523 : gcc_assert (op_type == unary_op);
5605 43523 : if (supportable_narrowing_operation (code, vectype_out, vectype_in,
5606 : &code1, &multi_step_cvt,
5607 : &interm_types))
5608 : break;
5609 :
5610 19008 : if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
5611 986 : goto unsupported;
5612 :
5613 5350 : if (code == FIX_TRUNC_EXPR)
5614 : {
5615 107 : cvt_type
5616 107 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5617 107 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5618 107 : if (cvt_type == NULL_TREE)
5619 0 : goto unsupported;
5620 107 : if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
5621 : &tc1))
5622 105 : codecvt1 = tc1;
5623 : else
5624 2 : goto unsupported;
5625 105 : if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
5626 : &code1, &multi_step_cvt,
5627 : &interm_types))
5628 : break;
5629 : }
5630 : /* If op0 can be represented with low precision integer,
5631 : truncate it to cvt_type and the do FLOAT_EXPR. */
5632 5243 : else if (code == FLOAT_EXPR)
5633 : {
5634 792 : if (cost_vec)
5635 : {
5636 784 : wide_int op_min_value, op_max_value;
5637 784 : tree def;
5638 :
5639 : /* ??? Merge ranges in case of more than one lane. */
5640 784 : if (SLP_TREE_LANES (slp_op0) != 1
5641 133 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
5642 917 : || !vect_get_range_info (def, &op_min_value, &op_max_value))
5643 755 : goto unsupported;
5644 :
5645 29 : if ((wi::min_precision (op_max_value, SIGNED)
5646 29 : > GET_MODE_BITSIZE (lhs_mode))
5647 29 : || (wi::min_precision (op_min_value, SIGNED)
5648 27 : > GET_MODE_BITSIZE (lhs_mode)))
5649 2 : goto unsupported;
5650 784 : }
5651 :
5652 35 : cvt_type
5653 35 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
5654 35 : cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
5655 35 : if (cvt_type == NULL_TREE)
5656 0 : goto unsupported;
5657 35 : if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
5658 : &code1, &multi_step_cvt,
5659 : &interm_types))
5660 2 : goto unsupported;
5661 33 : if (supportable_convert_operation ((tree_code) code, vectype_out,
5662 : cvt_type, &tc1))
5663 : {
5664 33 : codecvt1 = tc1;
5665 33 : modifier = NARROW_SRC;
5666 33 : break;
5667 : }
5668 : }
5669 :
5670 4455 : goto unsupported;
5671 :
5672 : default:
5673 : gcc_unreachable ();
5674 : }
5675 :
5676 109751 : if (modifier == WIDEN
5677 109751 : && loop_vinfo
5678 49545 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
5679 130835 : && (code1 == VEC_WIDEN_MULT_EVEN_EXPR
5680 21062 : || widening_evenodd_fn_p (code1)))
5681 : {
5682 22 : if (dump_enabled_p ())
5683 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5684 : "can't use a fully-masked loop because"
5685 : " widening operation on even/odd elements"
5686 : " mixes up lanes.\n");
5687 22 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
5688 : }
5689 :
5690 109751 : if (cost_vec) /* transformation not required. */
5691 : {
5692 86183 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
5693 86183 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in))
5694 : {
5695 0 : if (dump_enabled_p ())
5696 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5697 : "incompatible vector types for invariants\n");
5698 0 : return false;
5699 : }
5700 86183 : DUMP_VECT_SCOPE ("vectorizable_conversion");
5701 86183 : unsigned int nvectors = vect_get_num_copies (vinfo, slp_node);
5702 86183 : if (modifier == NONE)
5703 : {
5704 16783 : SLP_TREE_TYPE (slp_node) = type_conversion_vec_info_type;
5705 16783 : vect_model_simple_cost (vinfo, (1 + multi_step_cvt),
5706 : slp_node, cost_vec);
5707 : }
5708 69400 : else if (modifier == NARROW_SRC || modifier == NARROW_DST)
5709 : {
5710 28200 : SLP_TREE_TYPE (slp_node) = type_demotion_vec_info_type;
5711 : /* The final packing step produces one vector result per copy. */
5712 28200 : vect_model_promotion_demotion_cost (slp_node, nvectors,
5713 : multi_step_cvt, cost_vec,
5714 : widen_arith);
5715 : }
5716 : else
5717 : {
5718 41200 : SLP_TREE_TYPE (slp_node) = type_promotion_vec_info_type;
5719 : /* The initial unpacking step produces two vector results
5720 : per copy. MULTI_STEP_CVT is 0 for a single conversion,
5721 : so >> MULTI_STEP_CVT divides by 2^(number of steps - 1). */
5722 41200 : vect_model_promotion_demotion_cost (slp_node,
5723 : nvectors >> multi_step_cvt,
5724 : multi_step_cvt, cost_vec,
5725 : widen_arith);
5726 : }
5727 86183 : interm_types.release ();
5728 86183 : return true;
5729 86183 : }
5730 :
5731 : /* Transform. */
5732 23568 : if (dump_enabled_p ())
5733 4286 : dump_printf_loc (MSG_NOTE, vect_location, "transform conversion.\n");
5734 :
5735 23568 : if (op_type == binary_op)
5736 : {
5737 555 : if (CONSTANT_CLASS_P (op0))
5738 0 : op0 = fold_convert (TREE_TYPE (op1), op0);
5739 555 : else if (CONSTANT_CLASS_P (op1))
5740 250 : op1 = fold_convert (TREE_TYPE (op0), op1);
5741 : }
5742 :
5743 : /* In case of multi-step conversion, we first generate conversion operations
5744 : to the intermediate types, and then from that types to the final one.
5745 : We create vector destinations for the intermediate type (TYPES) received
5746 : from supportable_*_operation, and store them in the correct order
5747 : for future use in vect_create_vectorized_*_stmts (). */
5748 23568 : auto_vec<tree> vec_dsts (multi_step_cvt + 1);
5749 23568 : bool widen_or_narrow_float_p
5750 23568 : = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
5751 23568 : vec_dest = vect_create_destination_var (scalar_dest,
5752 : widen_or_narrow_float_p
5753 : ? cvt_type : vectype_out);
5754 23568 : vec_dsts.quick_push (vec_dest);
5755 :
5756 23568 : if (multi_step_cvt)
5757 : {
5758 9254 : for (i = interm_types.length () - 1;
5759 9254 : interm_types.iterate (i, &intermediate_type); i--)
5760 : {
5761 4871 : vec_dest = vect_create_destination_var (scalar_dest,
5762 : intermediate_type);
5763 4871 : vec_dsts.quick_push (vec_dest);
5764 : }
5765 : }
5766 :
5767 23568 : if (cvt_type)
5768 76 : vec_dest = vect_create_destination_var (scalar_dest,
5769 : widen_or_narrow_float_p
5770 : ? vectype_out : cvt_type);
5771 :
5772 23568 : switch (modifier)
5773 : {
5774 4442 : case NONE:
5775 4442 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5776 : /* vec_dest is intermediate type operand when multi_step_cvt. */
5777 4442 : if (multi_step_cvt)
5778 : {
5779 21 : cvt_op = vec_dest;
5780 21 : vec_dest = vec_dsts[0];
5781 : }
5782 :
5783 9307 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5784 : {
5785 : /* Arguments are ready, create the new vector stmt. */
5786 4865 : gimple* new_stmt;
5787 4865 : if (multi_step_cvt)
5788 : {
5789 21 : gcc_assert (multi_step_cvt == 1);
5790 21 : new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
5791 21 : new_temp = make_ssa_name (cvt_op, new_stmt);
5792 21 : gimple_assign_set_lhs (new_stmt, new_temp);
5793 21 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5794 21 : vop0 = new_temp;
5795 : }
5796 4865 : new_stmt = vect_gimple_build (vec_dest, code1, vop0);
5797 4865 : new_temp = make_ssa_name (vec_dest, new_stmt);
5798 4865 : gimple_set_lhs (new_stmt, new_temp);
5799 4865 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5800 :
5801 4865 : slp_node->push_vec_def (new_stmt);
5802 : }
5803 : break;
5804 :
5805 10005 : case WIDEN:
5806 : /* In case the vectorization factor (VF) is bigger than the number
5807 : of elements that we can fit in a vectype (nunits), we have to
5808 : generate more than one vector stmt - i.e - we need to "unroll"
5809 : the vector stmt by a factor VF/nunits. */
5810 10005 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0,
5811 10005 : code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
5812 : &vec_oprnds1);
5813 10005 : if (code == WIDEN_LSHIFT_EXPR)
5814 : {
5815 0 : int oprnds_size = vec_oprnds0.length ();
5816 0 : vec_oprnds1.create (oprnds_size);
5817 0 : for (i = 0; i < oprnds_size; ++i)
5818 0 : vec_oprnds1.quick_push (op1);
5819 : }
5820 : /* Arguments are ready. Create the new vector stmts. */
5821 21847 : for (i = multi_step_cvt; i >= 0; i--)
5822 : {
5823 11842 : tree this_dest = vec_dsts[i];
5824 11842 : code_helper c1 = code1, c2 = code2;
5825 11842 : if (i == 0 && codecvt2 != ERROR_MARK)
5826 : {
5827 48 : c1 = codecvt1;
5828 48 : c2 = codecvt2;
5829 : }
5830 11842 : if (known_eq (nunits_out, nunits_in))
5831 60 : vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
5832 : stmt_info, this_dest, gsi, c1,
5833 : op_type);
5834 : else
5835 11782 : vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
5836 : &vec_oprnds1, stmt_info,
5837 : this_dest, gsi,
5838 : c1, c2, op_type);
5839 : }
5840 :
5841 38111 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5842 : {
5843 28106 : gimple *new_stmt;
5844 28106 : if (cvt_type)
5845 : {
5846 120 : new_temp = make_ssa_name (vec_dest);
5847 120 : new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5848 120 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5849 : }
5850 : else
5851 27986 : new_stmt = SSA_NAME_DEF_STMT (vop0);
5852 :
5853 28106 : slp_node->push_vec_def (new_stmt);
5854 : }
5855 : break;
5856 :
5857 9121 : case NARROW_SRC:
5858 9121 : case NARROW_DST:
5859 : /* In case the vectorization factor (VF) is bigger than the number
5860 : of elements that we can fit in a vectype (nunits), we have to
5861 : generate more than one vector stmt - i.e - we need to "unroll"
5862 : the vector stmt by a factor VF/nunits. */
5863 9121 : vect_get_vec_defs (vinfo, slp_node, op0, &vec_oprnds0);
5864 : /* Arguments are ready. Create the new vector stmts. */
5865 9121 : if (cvt_type && modifier == NARROW_DST)
5866 153 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5867 : {
5868 124 : new_temp = make_ssa_name (vec_dest);
5869 124 : gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5870 124 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5871 124 : vec_oprnds0[i] = new_temp;
5872 : }
5873 :
5874 9121 : vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
5875 : multi_step_cvt,
5876 : stmt_info, vec_dsts, gsi,
5877 : slp_node, code1,
5878 : modifier == NARROW_SRC);
5879 : /* After demoting op0 to cvt_type, convert it to dest. */
5880 9121 : if (cvt_type && code == FLOAT_EXPR)
5881 : {
5882 16 : for (unsigned int i = 0; i != vec_oprnds0.length() / 2; i++)
5883 : {
5884 : /* Arguments are ready, create the new vector stmt. */
5885 8 : gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
5886 8 : gimple *new_stmt
5887 8 : = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
5888 8 : new_temp = make_ssa_name (vec_dest, new_stmt);
5889 8 : gimple_set_lhs (new_stmt, new_temp);
5890 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5891 :
5892 : /* This is the last step of the conversion sequence. Store the
5893 : vectors in SLP_NODE or in vector info of the scalar statement
5894 : (or in STMT_VINFO_RELATED_STMT chain). */
5895 8 : slp_node->push_vec_def (new_stmt);
5896 : }
5897 : }
5898 : break;
5899 : }
5900 :
5901 23568 : vec_oprnds0.release ();
5902 23568 : vec_oprnds1.release ();
5903 23568 : interm_types.release ();
5904 :
5905 23568 : return true;
5906 192549 : }
5907 :
5908 : /* Return true if we can assume from the scalar form of STMT_INFO that
5909 : neither the scalar nor the vector forms will generate code. STMT_INFO
5910 : is known not to involve a data reference. */
5911 :
5912 : bool
5913 3265367 : vect_nop_conversion_p (stmt_vec_info stmt_info)
5914 : {
5915 3265367 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5916 2973073 : if (!stmt || STMT_VINFO_DATA_REF (stmt_info))
5917 : return false;
5918 :
5919 978744 : tree lhs = gimple_assign_lhs (stmt);
5920 978744 : tree_code code = gimple_assign_rhs_code (stmt);
5921 978744 : tree rhs = gimple_assign_rhs1 (stmt);
5922 :
5923 978744 : if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
5924 : return true;
5925 :
5926 975506 : if (CONVERT_EXPR_CODE_P (code))
5927 243350 : return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
5928 :
5929 : return false;
5930 : }
5931 :
5932 : /* Function vectorizable_assignment.
5933 :
5934 : Check if STMT_INFO performs an assignment (copy) that can be vectorized.
5935 : If COST_VEC is passed, calculate costs but don't change anything,
5936 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
5937 : it, and insert it at GSI.
5938 : Return true if STMT_INFO is vectorizable in this way. */
5939 :
5940 : static bool
5941 2132495 : vectorizable_assignment (vec_info *vinfo,
5942 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5943 : slp_tree slp_node,
5944 : stmt_vector_for_cost *cost_vec)
5945 : {
5946 2132495 : tree vec_dest;
5947 2132495 : tree scalar_dest;
5948 2132495 : tree op;
5949 2132495 : tree new_temp;
5950 2132495 : enum vect_def_type dt[1] = {vect_unknown_def_type};
5951 2132495 : int i;
5952 2132495 : vec<tree> vec_oprnds = vNULL;
5953 2132495 : tree vop;
5954 2132495 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5955 2132495 : enum tree_code code;
5956 2132495 : tree vectype_in;
5957 :
5958 2132495 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5959 : return false;
5960 :
5961 2132495 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5962 243533 : && cost_vec)
5963 : return false;
5964 :
5965 : /* Is vectorizable assignment? */
5966 3857765 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
5967 1812046 : if (!stmt)
5968 : return false;
5969 :
5970 1812046 : scalar_dest = gimple_assign_lhs (stmt);
5971 1812046 : if (TREE_CODE (scalar_dest) != SSA_NAME)
5972 : return false;
5973 :
5974 986354 : if (STMT_VINFO_DATA_REF (stmt_info))
5975 : return false;
5976 :
5977 418846 : code = gimple_assign_rhs_code (stmt);
5978 418846 : if (!(gimple_assign_single_p (stmt)
5979 417220 : || code == PAREN_EXPR
5980 415973 : || CONVERT_EXPR_CODE_P (code)))
5981 : return false;
5982 :
5983 104134 : tree vectype = SLP_TREE_VECTYPE (slp_node);
5984 104134 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
5985 :
5986 104134 : slp_tree slp_op;
5987 104134 : if (!vect_is_simple_use (vinfo, slp_node, 0, &op, &slp_op,
5988 : &dt[0], &vectype_in))
5989 : {
5990 0 : if (dump_enabled_p ())
5991 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5992 : "use not simple.\n");
5993 0 : return false;
5994 : }
5995 104134 : if (!vectype_in)
5996 19766 : vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
5997 :
5998 : /* We can handle VIEW_CONVERT conversions that do not change the number
5999 : of elements or the vector size or other conversions when the component
6000 : types are nop-convertible. */
6001 104134 : if (!vectype_in
6002 103853 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
6003 91723 : || (code == VIEW_CONVERT_EXPR
6004 2996 : && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
6005 2996 : GET_MODE_SIZE (TYPE_MODE (vectype_in))))
6006 195857 : || (CONVERT_EXPR_CODE_P (code)
6007 88882 : && !tree_nop_conversion_p (TREE_TYPE (vectype),
6008 88882 : TREE_TYPE (vectype_in))))
6009 16998 : return false;
6010 :
6011 260959 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
6012 : {
6013 27 : if (dump_enabled_p ())
6014 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6015 : "can't convert between boolean and non "
6016 0 : "boolean vectors %T\n", TREE_TYPE (op));
6017 :
6018 27 : return false;
6019 : }
6020 :
6021 : /* We do not handle bit-precision changes. */
6022 87109 : if ((CONVERT_EXPR_CODE_P (code)
6023 2841 : || code == VIEW_CONVERT_EXPR)
6024 85766 : && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6025 84412 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6026 85266 : || (INTEGRAL_TYPE_P (TREE_TYPE (op))
6027 80031 : && !type_has_mode_precision_p (TREE_TYPE (op))))
6028 : /* But a conversion that does not change the bit-pattern is ok. */
6029 88159 : && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6030 1050 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
6031 1050 : && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
6032 1050 : > TYPE_PRECISION (TREE_TYPE (op)))
6033 550 : && TYPE_UNSIGNED (TREE_TYPE (op)))
6034 562 : || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
6035 562 : == TYPE_PRECISION (TREE_TYPE (op))))))
6036 : {
6037 333 : if (dump_enabled_p ())
6038 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6039 : "type conversion to/from bit-precision "
6040 : "unsupported.\n");
6041 333 : return false;
6042 : }
6043 :
6044 86776 : if (cost_vec) /* transformation not required. */
6045 : {
6046 70411 : if (!vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
6047 : {
6048 0 : if (dump_enabled_p ())
6049 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6050 : "incompatible vector types for invariants\n");
6051 0 : return false;
6052 : }
6053 70411 : SLP_TREE_TYPE (slp_node) = assignment_vec_info_type;
6054 70411 : DUMP_VECT_SCOPE ("vectorizable_assignment");
6055 70411 : if (!vect_nop_conversion_p (stmt_info))
6056 1046 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6057 70411 : return true;
6058 : }
6059 :
6060 : /* Transform. */
6061 16365 : if (dump_enabled_p ())
6062 3631 : dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
6063 :
6064 : /* Handle def. */
6065 16365 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6066 :
6067 : /* Handle use. */
6068 16365 : vect_get_vec_defs (vinfo, slp_node, op, &vec_oprnds);
6069 :
6070 : /* Arguments are ready. create the new vector stmt. */
6071 36883 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
6072 : {
6073 20518 : if (CONVERT_EXPR_CODE_P (code)
6074 719 : || code == VIEW_CONVERT_EXPR)
6075 19957 : vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
6076 20518 : gassign *new_stmt = gimple_build_assign (vec_dest, vop);
6077 20518 : new_temp = make_ssa_name (vec_dest, new_stmt);
6078 20518 : gimple_assign_set_lhs (new_stmt, new_temp);
6079 20518 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6080 20518 : slp_node->push_vec_def (new_stmt);
6081 : }
6082 :
6083 16365 : vec_oprnds.release ();
6084 16365 : return true;
6085 : }
6086 :
6087 :
6088 : /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
6089 : either as shift by a scalar or by a vector. */
6090 :
6091 : bool
6092 301645 : vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
6093 : {
6094 301645 : optab optab;
6095 301645 : tree vectype;
6096 :
6097 301645 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
6098 301645 : if (!vectype)
6099 : return false;
6100 :
6101 301645 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6102 301645 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6103 : return true;
6104 :
6105 265218 : optab = optab_for_tree_code (code, vectype, optab_vector);
6106 265218 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6107 : return true;
6108 :
6109 : return false;
6110 : }
6111 :
6112 :
6113 : /* Function vectorizable_shift.
6114 :
6115 : Check if STMT_INFO performs a shift operation that can be vectorized.
6116 : If COST_VEC is passed, calculate costs but don't change anything,
6117 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
6118 : it, and insert it at GSI.
6119 : Return true if STMT_INFO is vectorizable in this way. */
6120 :
6121 : static bool
6122 771720 : vectorizable_shift (vec_info *vinfo,
6123 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6124 : slp_tree slp_node,
6125 : stmt_vector_for_cost *cost_vec)
6126 : {
6127 771720 : tree vec_dest;
6128 771720 : tree scalar_dest;
6129 771720 : tree op0, op1 = NULL;
6130 771720 : tree vec_oprnd1 = NULL_TREE;
6131 771720 : tree vectype;
6132 771720 : enum tree_code code;
6133 771720 : machine_mode vec_mode;
6134 771720 : tree new_temp;
6135 771720 : optab optab;
6136 771720 : int icode;
6137 771720 : machine_mode optab_op2_mode;
6138 771720 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
6139 771720 : poly_uint64 nunits_in;
6140 771720 : poly_uint64 nunits_out;
6141 771720 : tree vectype_out;
6142 771720 : tree op1_vectype;
6143 771720 : int i;
6144 771720 : vec<tree> vec_oprnds0 = vNULL;
6145 771720 : vec<tree> vec_oprnds1 = vNULL;
6146 771720 : tree vop0, vop1;
6147 771720 : unsigned int k;
6148 771720 : bool scalar_shift_arg = true;
6149 771720 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6150 771720 : bool incompatible_op1_vectype_p = false;
6151 :
6152 771720 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6153 : return false;
6154 :
6155 771720 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6156 243533 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
6157 242043 : && cost_vec)
6158 : return false;
6159 :
6160 : /* Is STMT a vectorizable binary/unary operation? */
6161 1170061 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6162 453740 : if (!stmt)
6163 : return false;
6164 :
6165 453740 : if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6166 : return false;
6167 :
6168 453182 : code = gimple_assign_rhs_code (stmt);
6169 :
6170 453182 : if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
6171 : || code == RROTATE_EXPR))
6172 : return false;
6173 :
6174 62004 : scalar_dest = gimple_assign_lhs (stmt);
6175 62004 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6176 62004 : if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6177 : {
6178 0 : if (dump_enabled_p ())
6179 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6180 : "bit-precision shifts not supported.\n");
6181 0 : return false;
6182 : }
6183 :
6184 62004 : slp_tree slp_op0;
6185 62004 : if (!vect_is_simple_use (vinfo, slp_node,
6186 : 0, &op0, &slp_op0, &dt[0], &vectype))
6187 : {
6188 0 : if (dump_enabled_p ())
6189 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6190 : "use not simple.\n");
6191 0 : return false;
6192 : }
6193 : /* If op0 is an external or constant def, infer the vector type
6194 : from the scalar type. */
6195 62004 : if (!vectype)
6196 16514 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
6197 62004 : if (!cost_vec)
6198 8713 : gcc_assert (vectype);
6199 62004 : if (!vectype)
6200 : {
6201 0 : if (dump_enabled_p ())
6202 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6203 : "no vectype for scalar type\n");
6204 0 : return false;
6205 : }
6206 :
6207 62004 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6208 62004 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6209 62004 : if (maybe_ne (nunits_out, nunits_in))
6210 : return false;
6211 :
6212 62004 : stmt_vec_info op1_def_stmt_info;
6213 62004 : slp_tree slp_op1;
6214 62004 : if (!vect_is_simple_use (vinfo, slp_node, 1, &op1, &slp_op1,
6215 : &dt[1], &op1_vectype, &op1_def_stmt_info))
6216 : {
6217 0 : if (dump_enabled_p ())
6218 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6219 : "use not simple.\n");
6220 0 : return false;
6221 : }
6222 :
6223 : /* Determine whether the shift amount is a vector, or scalar. If the
6224 : shift/rotate amount is a vector, use the vector/vector shift optabs. */
6225 :
6226 62004 : if ((dt[1] == vect_internal_def
6227 62004 : || dt[1] == vect_induction_def
6228 45335 : || dt[1] == vect_nested_cycle)
6229 16687 : && SLP_TREE_LANES (slp_node) == 1)
6230 : scalar_shift_arg = false;
6231 45372 : else if (dt[1] == vect_constant_def
6232 : || dt[1] == vect_external_def
6233 45372 : || dt[1] == vect_internal_def)
6234 : {
6235 : /* In SLP, need to check whether the shift count is the same,
6236 : in loops if it is a constant or invariant, it is always
6237 : a scalar shift. */
6238 45366 : vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
6239 45366 : stmt_vec_info slpstmt_info;
6240 :
6241 118755 : FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
6242 73389 : if (slpstmt_info)
6243 : {
6244 73389 : gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
6245 146778 : if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
6246 73389 : scalar_shift_arg = false;
6247 : }
6248 :
6249 : /* For internal SLP defs we have to make sure we see scalar stmts
6250 : for all vector elements.
6251 : ??? For different vectors we could resort to a different
6252 : scalar shift operand but code-generation below simply always
6253 : takes the first. */
6254 45366 : if (dt[1] == vect_internal_def
6255 45415 : && maybe_ne (nunits_out * vect_get_num_copies (vinfo, slp_node),
6256 49 : stmts.length ()))
6257 : scalar_shift_arg = false;
6258 :
6259 : /* If the shift amount is computed by a pattern stmt we cannot
6260 : use the scalar amount directly thus give up and use a vector
6261 : shift. */
6262 45366 : if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
6263 : scalar_shift_arg = false;
6264 : }
6265 : else
6266 : {
6267 6 : if (dump_enabled_p ())
6268 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6269 : "operand mode requires invariant argument.\n");
6270 6 : return false;
6271 : }
6272 :
6273 : /* Vector shifted by vector. */
6274 62036 : bool was_scalar_shift_arg = scalar_shift_arg;
6275 45357 : if (!scalar_shift_arg)
6276 : {
6277 16679 : optab = optab_for_tree_code (code, vectype, optab_vector);
6278 16679 : if (dump_enabled_p ())
6279 1207 : dump_printf_loc (MSG_NOTE, vect_location,
6280 : "vector/vector shift/rotate found.\n");
6281 :
6282 16679 : if (!op1_vectype)
6283 15 : op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
6284 : slp_op1);
6285 16679 : incompatible_op1_vectype_p
6286 33358 : = (op1_vectype == NULL_TREE
6287 16679 : || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
6288 16679 : TYPE_VECTOR_SUBPARTS (vectype))
6289 33356 : || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
6290 16672 : if (incompatible_op1_vectype_p
6291 7 : && (SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
6292 1 : || slp_op1->refcnt != 1))
6293 : {
6294 6 : if (dump_enabled_p ())
6295 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6296 : "unusable type for last operand in"
6297 : " vector/vector shift/rotate.\n");
6298 6 : return false;
6299 : }
6300 : }
6301 : /* See if the machine has a vector shifted by scalar insn and if not
6302 : then see if it has a vector shifted by vector insn. */
6303 : else
6304 : {
6305 45319 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6306 45319 : if (optab
6307 45319 : && can_implement_p (optab, TYPE_MODE (vectype)))
6308 : {
6309 45012 : if (dump_enabled_p ())
6310 4950 : dump_printf_loc (MSG_NOTE, vect_location,
6311 : "vector/scalar shift/rotate found.\n");
6312 : }
6313 : else
6314 : {
6315 307 : optab = optab_for_tree_code (code, vectype, optab_vector);
6316 307 : if (optab
6317 307 : && can_implement_p (optab, TYPE_MODE (vectype)))
6318 : {
6319 0 : scalar_shift_arg = false;
6320 :
6321 0 : if (dump_enabled_p ())
6322 0 : dump_printf_loc (MSG_NOTE, vect_location,
6323 : "vector/vector shift/rotate found.\n");
6324 :
6325 0 : if (!op1_vectype)
6326 0 : op1_vectype = get_vectype_for_scalar_type (vinfo,
6327 0 : TREE_TYPE (op1),
6328 : slp_op1);
6329 :
6330 : /* Unlike the other binary operators, shifts/rotates have
6331 : the rhs being int, instead of the same type as the lhs,
6332 : so make sure the scalar is the right type if we are
6333 : dealing with vectors of long long/long/short/char. */
6334 0 : incompatible_op1_vectype_p
6335 0 : = (!op1_vectype
6336 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype),
6337 0 : TREE_TYPE (op1)));
6338 0 : if (incompatible_op1_vectype_p
6339 0 : && dt[1] == vect_internal_def)
6340 : {
6341 0 : if (dump_enabled_p ())
6342 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6343 : "unusable type for last operand in"
6344 : " vector/vector shift/rotate.\n");
6345 0 : return false;
6346 : }
6347 : }
6348 : }
6349 : }
6350 :
6351 : /* Supportable by target? */
6352 61992 : if (!optab)
6353 : {
6354 0 : if (dump_enabled_p ())
6355 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6356 : "no shift optab for %s and %T.\n",
6357 : get_tree_code_name (code), vectype);
6358 0 : return false;
6359 : }
6360 61992 : vec_mode = TYPE_MODE (vectype);
6361 61992 : icode = (int) optab_handler (optab, vec_mode);
6362 61992 : if (icode == CODE_FOR_nothing)
6363 : {
6364 6593 : if (dump_enabled_p ())
6365 930 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6366 : "shift op not supported by target.\n");
6367 6593 : return false;
6368 : }
6369 : /* vector lowering cannot optimize vector shifts using word arithmetic. */
6370 55399 : if (vect_emulated_vector_p (vectype))
6371 : return false;
6372 :
6373 55399 : if (cost_vec) /* transformation not required. */
6374 : {
6375 46686 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6376 46686 : || ((!scalar_shift_arg || dt[1] == vect_internal_def)
6377 8237 : && (!incompatible_op1_vectype_p
6378 1 : || dt[1] == vect_constant_def)
6379 8237 : && !vect_maybe_update_slp_op_vectype
6380 8237 : (slp_op1,
6381 : incompatible_op1_vectype_p ? vectype : op1_vectype)))
6382 : {
6383 0 : if (dump_enabled_p ())
6384 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6385 : "incompatible vector types for invariants\n");
6386 0 : return false;
6387 : }
6388 : /* Now adjust the constant shift amount in place. */
6389 46686 : if (incompatible_op1_vectype_p
6390 1 : && dt[1] == vect_constant_def)
6391 : {
6392 1 : unsigned group_size = SLP_TREE_LANES (slp_op1);
6393 2 : gcc_assert (SLP_TREE_SCALAR_OPS (slp_op1).length () == group_size);
6394 5 : for (unsigned i = 0; i < group_size; ++i)
6395 : {
6396 4 : SLP_TREE_SCALAR_OPS (slp_op1)[i]
6397 4 : = fold_convert (TREE_TYPE (vectype),
6398 : SLP_TREE_SCALAR_OPS (slp_op1)[i]);
6399 4 : gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
6400 : == INTEGER_CST));
6401 : }
6402 : }
6403 46686 : SLP_TREE_TYPE (slp_node) = shift_vec_info_type;
6404 46686 : DUMP_VECT_SCOPE ("vectorizable_shift");
6405 46686 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6406 46686 : return true;
6407 : }
6408 :
6409 : /* Transform. */
6410 :
6411 8713 : if (dump_enabled_p ())
6412 2033 : dump_printf_loc (MSG_NOTE, vect_location,
6413 : "transform binary/unary operation.\n");
6414 :
6415 : /* Handle def. */
6416 8713 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6417 :
6418 8713 : unsigned nvectors = vect_get_num_copies (vinfo, slp_node);
6419 8713 : if (scalar_shift_arg && dt[1] != vect_internal_def)
6420 : {
6421 : /* Vector shl and shr insn patterns can be defined with scalar
6422 : operand 2 (shift operand). In this case, use constant or loop
6423 : invariant op1 directly, without extending it to vector mode
6424 : first. */
6425 6543 : optab_op2_mode = insn_data[icode].operand[2].mode;
6426 6543 : if (!VECTOR_MODE_P (optab_op2_mode))
6427 : {
6428 6543 : if (dump_enabled_p ())
6429 1918 : dump_printf_loc (MSG_NOTE, vect_location,
6430 : "operand 1 using scalar mode.\n");
6431 6543 : vec_oprnd1 = op1;
6432 6543 : vec_oprnds1.create (nvectors);
6433 6543 : vec_oprnds1.quick_push (vec_oprnd1);
6434 : /* Store vec_oprnd1 for every vector stmt to be created.
6435 : We check during the analysis that all the shift arguments
6436 : are the same.
6437 : TODO: Allow different constants for different vector
6438 : stmts generated for an SLP instance. */
6439 15177 : for (k = 0; k < nvectors - 1; k++)
6440 2091 : vec_oprnds1.quick_push (vec_oprnd1);
6441 : }
6442 : }
6443 2170 : else if (!scalar_shift_arg && incompatible_op1_vectype_p)
6444 : {
6445 0 : if (was_scalar_shift_arg)
6446 : {
6447 : /* If the argument was the same in all lanes create the
6448 : correctly typed vector shift amount directly. Note
6449 : we made SLP scheduling think we use the original scalars,
6450 : so place the compensation code next to the shift which
6451 : is conservative. See PR119640 where it otherwise breaks. */
6452 0 : op1 = fold_convert (TREE_TYPE (vectype), op1);
6453 0 : op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
6454 : gsi);
6455 0 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
6456 : gsi);
6457 0 : vec_oprnds1.create (nvectors);
6458 0 : for (k = 0; k < nvectors; k++)
6459 0 : vec_oprnds1.quick_push (vec_oprnd1);
6460 : }
6461 0 : else if (dt[1] == vect_constant_def)
6462 : /* The constant shift amount has been adjusted in place. */
6463 : ;
6464 : else
6465 0 : gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
6466 : }
6467 :
6468 : /* vec_oprnd1 is available if operand 1 should be of a scalar-type
6469 : (a special case for certain kind of vector shifts); otherwise,
6470 : operand 1 should be of a vector type (the usual case). */
6471 2170 : vect_get_vec_defs (vinfo, slp_node,
6472 : op0, &vec_oprnds0,
6473 8713 : vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
6474 :
6475 : /* Arguments are ready. Create the new vector stmt. */
6476 23027 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6477 : {
6478 : /* For internal defs where we need to use a scalar shift arg
6479 : extract the first lane. */
6480 14314 : if (scalar_shift_arg && dt[1] == vect_internal_def)
6481 : {
6482 10 : vop1 = vec_oprnds1[0];
6483 10 : new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
6484 10 : gassign *new_stmt
6485 10 : = gimple_build_assign (new_temp,
6486 10 : build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
6487 : vop1,
6488 10 : TYPE_SIZE (TREE_TYPE (new_temp)),
6489 : bitsize_zero_node));
6490 10 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6491 10 : vop1 = new_temp;
6492 10 : }
6493 : else
6494 14304 : vop1 = vec_oprnds1[i];
6495 14314 : gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
6496 14314 : new_temp = make_ssa_name (vec_dest, new_stmt);
6497 14314 : gimple_assign_set_lhs (new_stmt, new_temp);
6498 14314 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6499 14314 : slp_node->push_vec_def (new_stmt);
6500 : }
6501 :
6502 8713 : vec_oprnds0.release ();
6503 8713 : vec_oprnds1.release ();
6504 :
6505 8713 : return true;
6506 : }
6507 :
6508 : /* Function vectorizable_operation.
6509 :
6510 : Check if STMT_INFO performs a binary, unary or ternary operation that can
6511 : be vectorized.
6512 : If COST_VEC is passed, calculate costs but don't change anything,
6513 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
6514 : it, and insert it at GSI.
6515 : Return true if STMT_INFO is vectorizable in this way. */
6516 :
6517 : static bool
6518 2656974 : vectorizable_operation (vec_info *vinfo,
6519 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6520 : slp_tree slp_node,
6521 : stmt_vector_for_cost *cost_vec)
6522 : {
6523 2656974 : tree vec_dest;
6524 2656974 : tree scalar_dest;
6525 2656974 : tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
6526 2656974 : tree vectype;
6527 2656974 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6528 2656974 : enum tree_code code, orig_code;
6529 2656974 : machine_mode vec_mode;
6530 2656974 : tree new_temp;
6531 2656974 : int op_type;
6532 2656974 : optab optab;
6533 2656974 : bool target_support_p;
6534 2656974 : enum vect_def_type dt[3]
6535 : = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
6536 2656974 : poly_uint64 nunits_in;
6537 2656974 : poly_uint64 nunits_out;
6538 2656974 : tree vectype_out;
6539 2656974 : int i;
6540 2656974 : vec<tree> vec_oprnds0 = vNULL;
6541 2656974 : vec<tree> vec_oprnds1 = vNULL;
6542 2656974 : vec<tree> vec_oprnds2 = vNULL;
6543 2656974 : tree vop0, vop1, vop2;
6544 2656974 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6545 :
6546 2656974 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6547 : return false;
6548 :
6549 2656974 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6550 243533 : && cost_vec)
6551 : return false;
6552 :
6553 : /* Is STMT a vectorizable binary/unary operation? */
6554 4452655 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6555 2336525 : if (!stmt)
6556 : return false;
6557 :
6558 : /* Loads and stores are handled in vectorizable_{load,store}. */
6559 2336525 : if (STMT_VINFO_DATA_REF (stmt_info))
6560 : return false;
6561 :
6562 943325 : orig_code = code = gimple_assign_rhs_code (stmt);
6563 :
6564 : /* Shifts are handled in vectorizable_shift. */
6565 943325 : if (code == LSHIFT_EXPR
6566 : || code == RSHIFT_EXPR
6567 : || code == LROTATE_EXPR
6568 943325 : || code == RROTATE_EXPR)
6569 : return false;
6570 :
6571 : /* Comparisons are handled in vectorizable_comparison. */
6572 890034 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6573 : return false;
6574 :
6575 : /* Conditions are handled in vectorizable_condition. */
6576 690223 : if (code == COND_EXPR)
6577 : return false;
6578 :
6579 : /* For pointer addition and subtraction, we should use the normal
6580 : plus and minus for the vector operation. */
6581 663355 : if (code == POINTER_PLUS_EXPR)
6582 : code = PLUS_EXPR;
6583 643444 : if (code == POINTER_DIFF_EXPR)
6584 3217 : code = MINUS_EXPR;
6585 :
6586 : /* Support only unary or binary operations. */
6587 663355 : op_type = TREE_CODE_LENGTH (code);
6588 663355 : if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
6589 : {
6590 0 : if (dump_enabled_p ())
6591 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6592 : "num. args = %d (not unary/binary/ternary op).\n",
6593 : op_type);
6594 0 : return false;
6595 : }
6596 :
6597 663355 : scalar_dest = gimple_assign_lhs (stmt);
6598 663355 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6599 :
6600 : /* Most operations cannot handle bit-precision types without extra
6601 : truncations. */
6602 663355 : bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
6603 651751 : if (!mask_op_p
6604 651751 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
6605 : /* Exception are bitwise binary operations. */
6606 : && code != BIT_IOR_EXPR
6607 1938 : && code != BIT_XOR_EXPR
6608 1103 : && code != BIT_AND_EXPR)
6609 : {
6610 793 : if (dump_enabled_p ())
6611 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6612 : "bit-precision arithmetic not supported.\n");
6613 793 : return false;
6614 : }
6615 :
6616 662562 : slp_tree slp_op0;
6617 662562 : if (!vect_is_simple_use (vinfo, slp_node,
6618 : 0, &op0, &slp_op0, &dt[0], &vectype))
6619 : {
6620 0 : if (dump_enabled_p ())
6621 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6622 : "use not simple.\n");
6623 0 : return false;
6624 : }
6625 662562 : bool is_invariant = (dt[0] == vect_external_def
6626 662562 : || dt[0] == vect_constant_def);
6627 : /* If op0 is an external or constant def, infer the vector type
6628 : from the scalar type. */
6629 662562 : if (!vectype)
6630 : {
6631 : /* For boolean type we cannot determine vectype by
6632 : invariant value (don't know whether it is a vector
6633 : of booleans or vector of integers). We use output
6634 : vectype because operations on boolean don't change
6635 : type. */
6636 78680 : if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
6637 : {
6638 2063 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
6639 : {
6640 321 : if (dump_enabled_p ())
6641 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6642 : "not supported operation on bool value.\n");
6643 321 : return false;
6644 : }
6645 1742 : vectype = vectype_out;
6646 : }
6647 : else
6648 76617 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
6649 : slp_node);
6650 : }
6651 662241 : if (!cost_vec)
6652 116574 : gcc_assert (vectype);
6653 662241 : if (!vectype)
6654 : {
6655 298 : if (dump_enabled_p ())
6656 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6657 : "no vectype for scalar type %T\n",
6658 2 : TREE_TYPE (op0));
6659 :
6660 298 : return false;
6661 : }
6662 :
6663 661943 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6664 661943 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6665 661943 : if (maybe_ne (nunits_out, nunits_in)
6666 661943 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
6667 20718 : return false;
6668 :
6669 641225 : tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
6670 641225 : slp_tree slp_op1 = NULL, slp_op2 = NULL;
6671 641225 : if (op_type == binary_op || op_type == ternary_op)
6672 : {
6673 559826 : if (!vect_is_simple_use (vinfo, slp_node,
6674 : 1, &op1, &slp_op1, &dt[1], &vectype2))
6675 : {
6676 0 : if (dump_enabled_p ())
6677 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6678 : "use not simple.\n");
6679 0 : return false;
6680 : }
6681 559826 : is_invariant &= (dt[1] == vect_external_def
6682 559826 : || dt[1] == vect_constant_def);
6683 559826 : if (vectype2
6684 906120 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
6685 346294 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6686 346294 : TREE_TYPE (vectype2))))
6687 4 : return false;
6688 : }
6689 641221 : if (op_type == ternary_op)
6690 : {
6691 0 : if (!vect_is_simple_use (vinfo, slp_node,
6692 : 2, &op2, &slp_op2, &dt[2], &vectype3))
6693 : {
6694 0 : if (dump_enabled_p ())
6695 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6696 : "use not simple.\n");
6697 0 : return false;
6698 : }
6699 0 : is_invariant &= (dt[2] == vect_external_def
6700 0 : || dt[2] == vect_constant_def);
6701 0 : if (vectype3
6702 0 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
6703 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6704 0 : TREE_TYPE (vectype3))))
6705 0 : return false;
6706 : }
6707 :
6708 : /* Multiple types in SLP are handled by creating the appropriate number of
6709 : vectorized stmts for each SLP node. */
6710 641221 : auto vec_num = vect_get_num_copies (vinfo, slp_node);
6711 :
6712 : /* Reject attempts to combine mask types with nonmask types, e.g. if
6713 : we have an AND between a (nonmask) boolean loaded from memory and
6714 : a (mask) boolean result of a comparison.
6715 :
6716 : TODO: We could easily fix these cases up using pattern statements. */
6717 641221 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
6718 979295 : || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
6719 1282442 : || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
6720 : {
6721 0 : if (dump_enabled_p ())
6722 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6723 : "mixed mask and nonmask vector types\n");
6724 0 : return false;
6725 : }
6726 :
6727 : /* Supportable by target? */
6728 :
6729 641221 : vec_mode = TYPE_MODE (vectype);
6730 641221 : optab = optab_for_tree_code (code, vectype, optab_default);
6731 641221 : if (!optab)
6732 : {
6733 69739 : if (dump_enabled_p ())
6734 6014 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6735 : "no optab for %s and %T.\n",
6736 : get_tree_code_name (code), vectype);
6737 69739 : return false;
6738 : }
6739 571482 : target_support_p = can_implement_p (optab, vec_mode);
6740 :
6741 571482 : bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
6742 571482 : if (!target_support_p || using_emulated_vectors_p)
6743 : {
6744 31097 : if (dump_enabled_p ())
6745 1152 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6746 : "op not supported by target.\n");
6747 : /* When vec_mode is not a vector mode and we verified ops we
6748 : do not have to lower like AND are natively supported let
6749 : those through even when the mode isn't word_mode. For
6750 : ops we have to lower the lowering code assumes we are
6751 : dealing with word_mode. */
6752 62194 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
6753 30955 : || !GET_MODE_SIZE (vec_mode).is_constant ()
6754 30955 : || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6755 25753 : || !target_support_p)
6756 66482 : && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
6757 : /* Check only during analysis. */
6758 43665 : || (cost_vec && !vect_can_vectorize_without_simd_p (code)))
6759 : {
6760 30489 : if (dump_enabled_p ())
6761 1150 : dump_printf (MSG_NOTE, "using word mode not possible.\n");
6762 30489 : return false;
6763 : }
6764 608 : if (dump_enabled_p ())
6765 2 : dump_printf_loc (MSG_NOTE, vect_location,
6766 : "proceeding using word mode.\n");
6767 : using_emulated_vectors_p = true;
6768 : }
6769 :
6770 540993 : int reduc_idx = SLP_TREE_REDUC_IDX (slp_node);
6771 540993 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
6772 436297 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
6773 540993 : internal_fn cond_fn = get_conditional_internal_fn (code);
6774 540993 : internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
6775 :
6776 : /* If operating on inactive elements could generate spurious traps,
6777 : we need to restrict the operation to active lanes. Note that this
6778 : specifically doesn't apply to unhoisted invariants, since they
6779 : operate on the same value for every lane.
6780 :
6781 : Similarly, if this operation is part of a reduction, a fully-masked
6782 : loop should only change the active lanes of the reduction chain,
6783 : keeping the inactive lanes as-is. */
6784 511657 : bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
6785 983407 : || reduc_idx >= 0);
6786 :
6787 540993 : if (cost_vec) /* transformation not required. */
6788 : {
6789 424419 : if (loop_vinfo
6790 332792 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
6791 90410 : && mask_out_inactive)
6792 : {
6793 20511 : if (cond_len_fn != IFN_LAST
6794 20511 : && direct_internal_fn_supported_p (cond_len_fn, vectype,
6795 : OPTIMIZE_FOR_SPEED))
6796 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype,
6797 : 1);
6798 20511 : else if (cond_fn != IFN_LAST
6799 20511 : && direct_internal_fn_supported_p (cond_fn, vectype,
6800 : OPTIMIZE_FOR_SPEED))
6801 8532 : vect_record_loop_mask (loop_vinfo, masks, vec_num,
6802 : vectype, NULL);
6803 : else
6804 : {
6805 11979 : if (dump_enabled_p ())
6806 610 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6807 : "can't use a fully-masked loop because no"
6808 : " conditional operation is available.\n");
6809 11979 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
6810 : }
6811 : }
6812 :
6813 : /* Put types on constant and invariant SLP children. */
6814 424419 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6815 424366 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
6816 848689 : || !vect_maybe_update_slp_op_vectype (slp_op2, vectype))
6817 : {
6818 149 : if (dump_enabled_p ())
6819 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6820 : "incompatible vector types for invariants\n");
6821 149 : return false;
6822 : }
6823 :
6824 424270 : SLP_TREE_TYPE (slp_node) = op_vec_info_type;
6825 424270 : DUMP_VECT_SCOPE ("vectorizable_operation");
6826 424270 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec);
6827 424270 : if (using_emulated_vectors_p)
6828 : {
6829 : /* The above vect_model_simple_cost call handles constants
6830 : in the prologue and (mis-)costs one of the stmts as
6831 : vector stmt. See below for the actual lowering that will
6832 : be applied. */
6833 606 : unsigned n = vect_get_num_copies (vinfo, slp_node);
6834 606 : switch (code)
6835 : {
6836 217 : case PLUS_EXPR:
6837 217 : n *= 5;
6838 217 : break;
6839 352 : case MINUS_EXPR:
6840 352 : n *= 6;
6841 352 : break;
6842 0 : case NEGATE_EXPR:
6843 0 : n *= 4;
6844 0 : break;
6845 : default:
6846 : /* Bit operations do not have extra cost and are accounted
6847 : as vector stmt by vect_model_simple_cost. */
6848 : n = 0;
6849 : break;
6850 : }
6851 569 : if (n != 0)
6852 : {
6853 : /* We also need to materialize two large constants. */
6854 569 : record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
6855 : 0, vect_prologue);
6856 569 : record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
6857 : 0, vect_body);
6858 : }
6859 : }
6860 424270 : return true;
6861 : }
6862 :
6863 : /* Transform. */
6864 :
6865 116574 : if (dump_enabled_p ())
6866 16494 : dump_printf_loc (MSG_NOTE, vect_location,
6867 : "transform binary/unary operation.\n");
6868 :
6869 116574 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
6870 103505 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
6871 :
6872 : /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
6873 : vectors with unsigned elements, but the result is signed. So, we
6874 : need to compute the MINUS_EXPR into vectype temporary and
6875 : VIEW_CONVERT_EXPR it into the final vectype_out result. */
6876 116574 : tree vec_cvt_dest = NULL_TREE;
6877 116574 : if (orig_code == POINTER_DIFF_EXPR)
6878 : {
6879 123 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6880 123 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6881 : }
6882 : /* For reduction operations with undefined overflow behavior make sure to
6883 : pun them to unsigned since we change the order of evaluation.
6884 : ??? Avoid for in-order reductions? */
6885 116451 : else if (arith_code_with_undefined_signed_overflow (orig_code)
6886 99292 : && ANY_INTEGRAL_TYPE_P (vectype)
6887 48104 : && TYPE_OVERFLOW_UNDEFINED (vectype)
6888 142286 : && SLP_TREE_REDUC_IDX (slp_node) != -1)
6889 : {
6890 2471 : gcc_assert (orig_code == PLUS_EXPR || orig_code == MINUS_EXPR
6891 : || orig_code == MULT_EXPR || orig_code == POINTER_PLUS_EXPR);
6892 2471 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
6893 2471 : vectype = unsigned_type_for (vectype);
6894 2471 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6895 : }
6896 : /* Handle def. */
6897 : else
6898 113980 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
6899 :
6900 116574 : vect_get_vec_defs (vinfo, slp_node,
6901 : op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
6902 : /* Arguments are ready. Create the new vector stmt. */
6903 257544 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6904 : {
6905 140970 : gimple *new_stmt = NULL;
6906 281940 : vop1 = ((op_type == binary_op || op_type == ternary_op)
6907 140970 : ? vec_oprnds1[i] : NULL_TREE);
6908 140970 : vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
6909 :
6910 140970 : if (vec_cvt_dest
6911 140970 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop0)))
6912 : {
6913 2924 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop0);
6914 2924 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6915 : new_temp);
6916 2924 : new_temp = make_ssa_name (vec_dest, new_stmt);
6917 2924 : gimple_assign_set_lhs (new_stmt, new_temp);
6918 2924 : vect_finish_stmt_generation (vinfo, stmt_info,
6919 : new_stmt, gsi);
6920 2924 : vop0 = new_temp;
6921 : }
6922 140970 : if (vop1
6923 138222 : && vec_cvt_dest
6924 144032 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop1)))
6925 : {
6926 2924 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop1);
6927 2924 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6928 : new_temp);
6929 2924 : new_temp = make_ssa_name (vec_dest, new_stmt);
6930 2924 : gimple_assign_set_lhs (new_stmt, new_temp);
6931 2924 : vect_finish_stmt_generation (vinfo, stmt_info,
6932 : new_stmt, gsi);
6933 2924 : vop1 = new_temp;
6934 : }
6935 140970 : if (vop2
6936 0 : && vec_cvt_dest
6937 140970 : && !useless_type_conversion_p (vectype, TREE_TYPE (vop2)))
6938 : {
6939 0 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype, vop2);
6940 0 : new_stmt = gimple_build_assign (vec_dest, VIEW_CONVERT_EXPR,
6941 : new_temp);
6942 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
6943 0 : gimple_assign_set_lhs (new_stmt, new_temp);
6944 0 : vect_finish_stmt_generation (vinfo, stmt_info,
6945 : new_stmt, gsi);
6946 0 : vop2 = new_temp;
6947 : }
6948 :
6949 140970 : if (using_emulated_vectors_p)
6950 : {
6951 : /* Lower the operation. This follows vector lowering. */
6952 2 : tree word_type = build_nonstandard_integer_type
6953 2 : (GET_MODE_BITSIZE (vec_mode).to_constant (), 1);
6954 2 : tree wvop0 = make_ssa_name (word_type);
6955 2 : new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
6956 : build1 (VIEW_CONVERT_EXPR,
6957 : word_type, vop0));
6958 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6959 2 : tree wvop1 = NULL_TREE;
6960 2 : if (vop1)
6961 : {
6962 2 : wvop1 = make_ssa_name (word_type);
6963 2 : new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
6964 : build1 (VIEW_CONVERT_EXPR,
6965 : word_type, vop1));
6966 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6967 : }
6968 :
6969 2 : tree result_low;
6970 2 : if (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6971 : {
6972 1 : unsigned int width = vector_element_bits (vectype);
6973 1 : tree inner_type = TREE_TYPE (vectype);
6974 1 : HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
6975 1 : tree low_bits
6976 1 : = build_replicated_int_cst (word_type, width, max >> 1);
6977 1 : tree high_bits
6978 2 : = build_replicated_int_cst (word_type,
6979 1 : width, max & ~(max >> 1));
6980 1 : tree signs;
6981 1 : if (code == PLUS_EXPR || code == MINUS_EXPR)
6982 : {
6983 1 : signs = make_ssa_name (word_type);
6984 1 : new_stmt = gimple_build_assign (signs,
6985 : BIT_XOR_EXPR, wvop0, wvop1);
6986 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6987 1 : tree b_low = make_ssa_name (word_type);
6988 1 : new_stmt = gimple_build_assign (b_low, BIT_AND_EXPR,
6989 : wvop1, low_bits);
6990 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6991 1 : tree a_low = make_ssa_name (word_type);
6992 1 : if (code == PLUS_EXPR)
6993 1 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
6994 : wvop0, low_bits);
6995 : else
6996 0 : new_stmt = gimple_build_assign (a_low, BIT_IOR_EXPR,
6997 : wvop0, high_bits);
6998 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6999 1 : if (code == MINUS_EXPR)
7000 : {
7001 0 : new_stmt = gimple_build_assign (NULL_TREE,
7002 : BIT_NOT_EXPR, signs);
7003 0 : signs = make_ssa_name (word_type);
7004 0 : gimple_assign_set_lhs (new_stmt, signs);
7005 0 : vect_finish_stmt_generation (vinfo, stmt_info,
7006 : new_stmt, gsi);
7007 : }
7008 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
7009 : signs, high_bits);
7010 1 : signs = make_ssa_name (word_type);
7011 1 : gimple_assign_set_lhs (new_stmt, signs);
7012 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7013 1 : result_low = make_ssa_name (word_type);
7014 1 : new_stmt = gimple_build_assign (result_low, code,
7015 : a_low, b_low);
7016 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7017 : }
7018 : else /* if (code == NEGATE_EXPR) */
7019 : {
7020 0 : tree a_low = make_ssa_name (word_type);
7021 0 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
7022 : wvop0, low_bits);
7023 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7024 0 : signs = make_ssa_name (word_type);
7025 0 : new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
7026 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7027 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
7028 : signs, high_bits);
7029 0 : signs = make_ssa_name (word_type);
7030 0 : gimple_assign_set_lhs (new_stmt, signs);
7031 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7032 0 : result_low = make_ssa_name (word_type);
7033 0 : new_stmt = gimple_build_assign (result_low,
7034 : MINUS_EXPR, high_bits, a_low);
7035 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7036 : }
7037 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR,
7038 : result_low, signs);
7039 1 : result_low = make_ssa_name (word_type);
7040 1 : gimple_assign_set_lhs (new_stmt, result_low);
7041 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7042 : }
7043 : else
7044 : {
7045 1 : new_stmt = gimple_build_assign (NULL_TREE, code, wvop0, wvop1);
7046 1 : result_low = make_ssa_name (word_type);
7047 1 : gimple_assign_set_lhs (new_stmt, result_low);
7048 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7049 :
7050 : }
7051 2 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
7052 : build1 (VIEW_CONVERT_EXPR,
7053 : vectype, result_low));
7054 2 : new_temp = make_ssa_name (vectype);
7055 2 : gimple_assign_set_lhs (new_stmt, new_temp);
7056 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7057 : }
7058 140968 : else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
7059 : {
7060 16 : tree mask;
7061 16 : if (masked_loop_p)
7062 16 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7063 : vec_num, vectype, i);
7064 : else
7065 : /* Dummy mask. */
7066 0 : mask = build_minus_one_cst (truth_type_for (vectype));
7067 16 : auto_vec<tree> vops (6);
7068 16 : vops.quick_push (mask);
7069 16 : vops.quick_push (vop0);
7070 16 : if (vop1)
7071 16 : vops.quick_push (vop1);
7072 16 : if (vop2)
7073 0 : vops.quick_push (vop2);
7074 16 : if (reduc_idx >= 0)
7075 : {
7076 : /* Perform the operation on active elements only and take
7077 : inactive elements from the reduction chain input. */
7078 8 : gcc_assert (!vop2);
7079 8 : vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
7080 : }
7081 : else
7082 : {
7083 8 : auto else_value = targetm.preferred_else_value
7084 8 : (cond_fn, vectype, vops.length () - 1, &vops[1]);
7085 8 : vops.quick_push (else_value);
7086 : }
7087 16 : if (len_loop_p)
7088 : {
7089 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
7090 0 : vec_num, vectype, i, 1, true);
7091 0 : signed char biasval
7092 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
7093 0 : tree bias = build_int_cst (intQI_type_node, biasval);
7094 0 : vops.quick_push (len);
7095 0 : vops.quick_push (bias);
7096 : }
7097 16 : gcall *call
7098 16 : = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
7099 : : cond_len_fn,
7100 : vops);
7101 16 : new_temp = make_ssa_name (vec_dest, call);
7102 16 : gimple_call_set_lhs (call, new_temp);
7103 16 : gimple_call_set_nothrow (call, true);
7104 16 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
7105 16 : new_stmt = call;
7106 16 : }
7107 : else
7108 : {
7109 140952 : tree mask = NULL_TREE;
7110 : /* When combining two masks check if either of them is elsewhere
7111 : combined with a loop mask, if that's the case we can mark that the
7112 : new combined mask doesn't need to be combined with a loop mask. */
7113 140952 : if (masked_loop_p
7114 140952 : && code == BIT_AND_EXPR
7115 140952 : && VECTOR_BOOLEAN_TYPE_P (vectype))
7116 : {
7117 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op0, vec_num }))
7118 : {
7119 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7120 : vec_num, vectype, i);
7121 :
7122 0 : vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7123 : vop0, gsi);
7124 : }
7125 :
7126 8 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op1, vec_num }))
7127 : {
7128 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7129 : vec_num, vectype, i);
7130 :
7131 0 : vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7132 : vop1, gsi);
7133 : }
7134 : }
7135 :
7136 140952 : new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
7137 140952 : new_temp = make_ssa_name (vec_dest, new_stmt);
7138 140952 : gimple_assign_set_lhs (new_stmt, new_temp);
7139 140952 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7140 140952 : if (using_emulated_vectors_p)
7141 : suppress_warning (new_stmt, OPT_Wvector_operation_performance);
7142 :
7143 : /* Enter the combined value into the vector cond hash so we don't
7144 : AND it with a loop mask again. */
7145 140952 : if (mask)
7146 0 : loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
7147 : }
7148 :
7149 140970 : if (vec_cvt_dest)
7150 : {
7151 3062 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
7152 3062 : new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
7153 : new_temp);
7154 3062 : new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
7155 3062 : gimple_assign_set_lhs (new_stmt, new_temp);
7156 3062 : vect_finish_stmt_generation (vinfo, stmt_info,
7157 : new_stmt, gsi);
7158 : }
7159 :
7160 140970 : slp_node->push_vec_def (new_stmt);
7161 : }
7162 :
7163 116574 : vec_oprnds0.release ();
7164 116574 : vec_oprnds1.release ();
7165 116574 : vec_oprnds2.release ();
7166 :
7167 116574 : return true;
7168 : }
7169 :
7170 : /* A helper function to ensure data reference DR_INFO's base alignment. */
7171 :
7172 : static void
7173 722278 : ensure_base_align (dr_vec_info *dr_info)
7174 : {
7175 : /* Alignment is only analyzed for the first element of a DR group,
7176 : use that to look at base alignment we need to enforce. */
7177 722278 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
7178 601540 : dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
7179 :
7180 722278 : gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
7181 :
7182 722278 : if (dr_info->base_misaligned)
7183 : {
7184 117831 : tree base_decl = dr_info->base_decl;
7185 :
7186 : // We should only be able to increase the alignment of a base object if
7187 : // we know what its new alignment should be at compile time.
7188 117831 : unsigned HOST_WIDE_INT align_base_to =
7189 117831 : DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
7190 :
7191 117831 : if (decl_in_symtab_p (base_decl))
7192 3256 : symtab_node::get (base_decl)->increase_alignment (align_base_to);
7193 114575 : else if (DECL_ALIGN (base_decl) < align_base_to)
7194 : {
7195 95113 : SET_DECL_ALIGN (base_decl, align_base_to);
7196 95113 : DECL_USER_ALIGN (base_decl) = 1;
7197 : }
7198 117831 : dr_info->base_misaligned = false;
7199 : }
7200 722278 : }
7201 :
7202 :
7203 : /* Function get_group_alias_ptr_type.
7204 :
7205 : Return the alias type for the group starting at FIRST_STMT_INFO. */
7206 :
7207 : static tree
7208 1665399 : get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
7209 : {
7210 1665399 : struct data_reference *first_dr, *next_dr;
7211 :
7212 1665399 : first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
7213 1665399 : stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
7214 3956713 : while (next_stmt_info)
7215 : {
7216 2479174 : next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
7217 4958348 : if (get_alias_set (DR_REF (first_dr))
7218 2479174 : != get_alias_set (DR_REF (next_dr)))
7219 : {
7220 187860 : if (dump_enabled_p ())
7221 30 : dump_printf_loc (MSG_NOTE, vect_location,
7222 : "conflicting alias set types.\n");
7223 187860 : return ptr_type_node;
7224 : }
7225 2291314 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7226 : }
7227 1477539 : return reference_alias_ptr_type (DR_REF (first_dr));
7228 : }
7229 :
7230 :
7231 : /* Function scan_operand_equal_p.
7232 :
7233 : Helper function for check_scan_store. Compare two references
7234 : with .GOMP_SIMD_LANE bases. */
7235 :
7236 : static bool
7237 1284 : scan_operand_equal_p (tree ref1, tree ref2)
7238 : {
7239 1284 : tree ref[2] = { ref1, ref2 };
7240 1284 : poly_int64 bitsize[2], bitpos[2];
7241 : tree offset[2], base[2];
7242 3852 : for (int i = 0; i < 2; ++i)
7243 : {
7244 2568 : machine_mode mode;
7245 2568 : int unsignedp, reversep, volatilep = 0;
7246 2568 : base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
7247 : &offset[i], &mode, &unsignedp,
7248 : &reversep, &volatilep);
7249 2568 : if (reversep || volatilep || maybe_ne (bitpos[i], 0))
7250 0 : return false;
7251 2568 : if (TREE_CODE (base[i]) == MEM_REF
7252 42 : && offset[i] == NULL_TREE
7253 2610 : && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
7254 : {
7255 42 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
7256 42 : if (is_gimple_assign (def_stmt)
7257 42 : && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
7258 42 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
7259 84 : && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
7260 : {
7261 42 : if (maybe_ne (mem_ref_offset (base[i]), 0))
7262 : return false;
7263 42 : base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
7264 42 : offset[i] = gimple_assign_rhs2 (def_stmt);
7265 : }
7266 : }
7267 : }
7268 :
7269 1284 : if (!operand_equal_p (base[0], base[1], 0))
7270 : return false;
7271 934 : if (maybe_ne (bitsize[0], bitsize[1]))
7272 : return false;
7273 934 : if (offset[0] != offset[1])
7274 : {
7275 916 : if (!offset[0] || !offset[1])
7276 : return false;
7277 916 : if (!operand_equal_p (offset[0], offset[1], 0))
7278 : {
7279 : tree step[2];
7280 0 : for (int i = 0; i < 2; ++i)
7281 : {
7282 0 : step[i] = integer_one_node;
7283 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7284 : {
7285 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7286 0 : if (is_gimple_assign (def_stmt)
7287 0 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
7288 0 : && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
7289 : == INTEGER_CST))
7290 : {
7291 0 : step[i] = gimple_assign_rhs2 (def_stmt);
7292 0 : offset[i] = gimple_assign_rhs1 (def_stmt);
7293 : }
7294 : }
7295 0 : else if (TREE_CODE (offset[i]) == MULT_EXPR)
7296 : {
7297 0 : step[i] = TREE_OPERAND (offset[i], 1);
7298 0 : offset[i] = TREE_OPERAND (offset[i], 0);
7299 : }
7300 0 : tree rhs1 = NULL_TREE;
7301 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7302 : {
7303 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7304 0 : if (gimple_assign_cast_p (def_stmt))
7305 0 : rhs1 = gimple_assign_rhs1 (def_stmt);
7306 : }
7307 0 : else if (CONVERT_EXPR_P (offset[i]))
7308 0 : rhs1 = TREE_OPERAND (offset[i], 0);
7309 0 : if (rhs1
7310 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
7311 0 : && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
7312 0 : && (TYPE_PRECISION (TREE_TYPE (offset[i]))
7313 0 : >= TYPE_PRECISION (TREE_TYPE (rhs1))))
7314 0 : offset[i] = rhs1;
7315 : }
7316 0 : if (!operand_equal_p (offset[0], offset[1], 0)
7317 0 : || !operand_equal_p (step[0], step[1], 0))
7318 0 : return false;
7319 : }
7320 : }
7321 : return true;
7322 : }
7323 :
7324 :
7325 : enum scan_store_kind {
7326 : /* Normal permutation. */
7327 : scan_store_kind_perm,
7328 :
7329 : /* Whole vector left shift permutation with zero init. */
7330 : scan_store_kind_lshift_zero,
7331 :
7332 : /* Whole vector left shift permutation and VEC_COND_EXPR. */
7333 : scan_store_kind_lshift_cond
7334 : };
7335 :
7336 : /* Function check_scan_store.
7337 :
7338 : Verify if we can perform the needed permutations or whole vector shifts.
7339 : Return -1 on failure, otherwise exact log2 of vectype's nunits.
7340 : USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
7341 : to do at each step. */
7342 :
7343 : static int
7344 1024 : scan_store_can_perm_p (tree vectype, tree init,
7345 : vec<enum scan_store_kind> *use_whole_vector = NULL)
7346 : {
7347 1024 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7348 1024 : unsigned HOST_WIDE_INT nunits;
7349 1024 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7350 : return -1;
7351 1024 : int units_log2 = exact_log2 (nunits);
7352 1024 : if (units_log2 <= 0)
7353 : return -1;
7354 :
7355 : int i;
7356 : enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
7357 4784 : for (i = 0; i <= units_log2; ++i)
7358 : {
7359 3760 : unsigned HOST_WIDE_INT j, k;
7360 3760 : enum scan_store_kind kind = scan_store_kind_perm;
7361 3760 : vec_perm_builder sel (nunits, nunits, 1);
7362 3760 : sel.quick_grow (nunits);
7363 3760 : if (i == units_log2)
7364 : {
7365 9728 : for (j = 0; j < nunits; ++j)
7366 8704 : sel[j] = nunits - 1;
7367 : }
7368 : else
7369 : {
7370 10416 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7371 7680 : sel[j] = j;
7372 26416 : for (k = 0; j < nunits; ++j, ++k)
7373 23680 : sel[j] = nunits + k;
7374 : }
7375 6496 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7376 3760 : if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
7377 : {
7378 0 : if (i == units_log2)
7379 : return -1;
7380 :
7381 0 : if (whole_vector_shift_kind == scan_store_kind_perm)
7382 : {
7383 0 : if (!can_implement_p (vec_shl_optab, vec_mode))
7384 : return -1;
7385 0 : whole_vector_shift_kind = scan_store_kind_lshift_zero;
7386 : /* Whole vector shifts shift in zeros, so if init is all zero
7387 : constant, there is no need to do anything further. */
7388 0 : if ((TREE_CODE (init) != INTEGER_CST
7389 0 : && TREE_CODE (init) != REAL_CST)
7390 0 : || !initializer_zerop (init))
7391 : {
7392 0 : tree masktype = truth_type_for (vectype);
7393 0 : if (!expand_vec_cond_expr_p (vectype, masktype))
7394 : return -1;
7395 : whole_vector_shift_kind = scan_store_kind_lshift_cond;
7396 : }
7397 : }
7398 0 : kind = whole_vector_shift_kind;
7399 : }
7400 3760 : if (use_whole_vector)
7401 : {
7402 1880 : if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
7403 0 : use_whole_vector->safe_grow_cleared (i, true);
7404 5640 : if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
7405 0 : use_whole_vector->safe_push (kind);
7406 : }
7407 3760 : }
7408 :
7409 : return units_log2;
7410 : }
7411 :
7412 :
7413 : /* Function check_scan_store.
7414 :
7415 : Check magic stores for #pragma omp scan {in,ex}clusive reductions. */
7416 :
7417 : static bool
7418 1076 : check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
7419 : enum vect_def_type rhs_dt, slp_tree slp_node,
7420 : slp_tree mask_node,
7421 : vect_memory_access_type memory_access_type)
7422 : {
7423 1076 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7424 1076 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7425 1076 : tree ref_type;
7426 :
7427 1076 : gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
7428 1076 : if (SLP_TREE_LANES (slp_node) > 1
7429 1076 : || mask_node
7430 1076 : || memory_access_type != VMAT_CONTIGUOUS
7431 1076 : || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
7432 1076 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
7433 1076 : || loop_vinfo == NULL
7434 1076 : || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7435 1076 : || LOOP_VINFO_EPILOGUE_P (loop_vinfo)
7436 1076 : || STMT_VINFO_GROUPED_ACCESS (stmt_info)
7437 1076 : || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
7438 1076 : || !integer_zerop (DR_INIT (dr_info->dr))
7439 1076 : || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
7440 2152 : || !alias_sets_conflict_p (get_alias_set (vectype),
7441 1076 : get_alias_set (TREE_TYPE (ref_type))))
7442 : {
7443 0 : if (dump_enabled_p ())
7444 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7445 : "unsupported OpenMP scan store.\n");
7446 0 : return false;
7447 : }
7448 :
7449 : /* We need to pattern match code built by OpenMP lowering and simplified
7450 : by following optimizations into something we can handle.
7451 : #pragma omp simd reduction(inscan,+:r)
7452 : for (...)
7453 : {
7454 : r += something ();
7455 : #pragma omp scan inclusive (r)
7456 : use (r);
7457 : }
7458 : shall have body with:
7459 : // Initialization for input phase, store the reduction initializer:
7460 : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7461 : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7462 : D.2042[_21] = 0;
7463 : // Actual input phase:
7464 : ...
7465 : r.0_5 = D.2042[_20];
7466 : _6 = _4 + r.0_5;
7467 : D.2042[_20] = _6;
7468 : // Initialization for scan phase:
7469 : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
7470 : _26 = D.2043[_25];
7471 : _27 = D.2042[_25];
7472 : _28 = _26 + _27;
7473 : D.2043[_25] = _28;
7474 : D.2042[_25] = _28;
7475 : // Actual scan phase:
7476 : ...
7477 : r.1_8 = D.2042[_20];
7478 : ...
7479 : The "omp simd array" variable D.2042 holds the privatized copy used
7480 : inside of the loop and D.2043 is another one that holds copies of
7481 : the current original list item. The separate GOMP_SIMD_LANE ifn
7482 : kinds are there in order to allow optimizing the initializer store
7483 : and combiner sequence, e.g. if it is originally some C++ish user
7484 : defined reduction, but allow the vectorizer to pattern recognize it
7485 : and turn into the appropriate vectorized scan.
7486 :
7487 : For exclusive scan, this is slightly different:
7488 : #pragma omp simd reduction(inscan,+:r)
7489 : for (...)
7490 : {
7491 : use (r);
7492 : #pragma omp scan exclusive (r)
7493 : r += something ();
7494 : }
7495 : shall have body with:
7496 : // Initialization for input phase, store the reduction initializer:
7497 : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7498 : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7499 : D.2042[_21] = 0;
7500 : // Actual input phase:
7501 : ...
7502 : r.0_5 = D.2042[_20];
7503 : _6 = _4 + r.0_5;
7504 : D.2042[_20] = _6;
7505 : // Initialization for scan phase:
7506 : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
7507 : _26 = D.2043[_25];
7508 : D.2044[_25] = _26;
7509 : _27 = D.2042[_25];
7510 : _28 = _26 + _27;
7511 : D.2043[_25] = _28;
7512 : // Actual scan phase:
7513 : ...
7514 : r.1_8 = D.2044[_20];
7515 : ... */
7516 :
7517 1076 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
7518 : {
7519 : /* Match the D.2042[_21] = 0; store above. Just require that
7520 : it is a constant or external definition store. */
7521 564 : if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
7522 : {
7523 0 : fail_init:
7524 0 : if (dump_enabled_p ())
7525 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7526 : "unsupported OpenMP scan initializer store.\n");
7527 0 : return false;
7528 : }
7529 :
7530 564 : if (! loop_vinfo->scan_map)
7531 322 : loop_vinfo->scan_map = new hash_map<tree, tree>;
7532 564 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7533 564 : tree &cached = loop_vinfo->scan_map->get_or_insert (var);
7534 564 : if (cached)
7535 0 : goto fail_init;
7536 564 : cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
7537 :
7538 : /* These stores can be vectorized normally. */
7539 564 : return true;
7540 : }
7541 :
7542 512 : if (rhs_dt != vect_internal_def)
7543 : {
7544 0 : fail:
7545 0 : if (dump_enabled_p ())
7546 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7547 : "unsupported OpenMP scan combiner pattern.\n");
7548 0 : return false;
7549 : }
7550 :
7551 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7552 512 : tree rhs = gimple_assign_rhs1 (stmt);
7553 512 : if (TREE_CODE (rhs) != SSA_NAME)
7554 0 : goto fail;
7555 :
7556 512 : gimple *other_store_stmt = NULL;
7557 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7558 512 : bool inscan_var_store
7559 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7560 :
7561 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7562 : {
7563 252 : if (!inscan_var_store)
7564 : {
7565 126 : use_operand_p use_p;
7566 126 : imm_use_iterator iter;
7567 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7568 : {
7569 252 : gimple *use_stmt = USE_STMT (use_p);
7570 252 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7571 126 : continue;
7572 126 : if (gimple_bb (use_stmt) != gimple_bb (stmt)
7573 126 : || !is_gimple_assign (use_stmt)
7574 126 : || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
7575 126 : || other_store_stmt
7576 252 : || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
7577 0 : goto fail;
7578 126 : other_store_stmt = use_stmt;
7579 0 : }
7580 126 : if (other_store_stmt == NULL)
7581 0 : goto fail;
7582 126 : rhs = gimple_assign_lhs (other_store_stmt);
7583 126 : if (!single_imm_use (rhs, &use_p, &other_store_stmt))
7584 0 : goto fail;
7585 : }
7586 : }
7587 260 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
7588 : {
7589 260 : use_operand_p use_p;
7590 260 : imm_use_iterator iter;
7591 1040 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7592 : {
7593 520 : gimple *use_stmt = USE_STMT (use_p);
7594 520 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7595 260 : continue;
7596 260 : if (other_store_stmt)
7597 0 : goto fail;
7598 260 : other_store_stmt = use_stmt;
7599 260 : }
7600 : }
7601 : else
7602 0 : goto fail;
7603 :
7604 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7605 512 : if (gimple_bb (def_stmt) != gimple_bb (stmt)
7606 512 : || !is_gimple_assign (def_stmt)
7607 1024 : || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
7608 0 : goto fail;
7609 :
7610 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7611 : /* For pointer addition, we should use the normal plus for the vector
7612 : operation. */
7613 512 : switch (code)
7614 : {
7615 0 : case POINTER_PLUS_EXPR:
7616 0 : code = PLUS_EXPR;
7617 0 : break;
7618 0 : case MULT_HIGHPART_EXPR:
7619 0 : goto fail;
7620 : default:
7621 : break;
7622 : }
7623 512 : if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
7624 0 : goto fail;
7625 :
7626 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7627 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7628 512 : if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
7629 0 : goto fail;
7630 :
7631 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7632 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7633 512 : if (gimple_bb (load1_stmt) != gimple_bb (stmt)
7634 512 : || !gimple_assign_load_p (load1_stmt)
7635 512 : || gimple_bb (load2_stmt) != gimple_bb (stmt)
7636 1024 : || !gimple_assign_load_p (load2_stmt))
7637 0 : goto fail;
7638 :
7639 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7640 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7641 512 : if (load1_stmt_info == NULL
7642 512 : || load2_stmt_info == NULL
7643 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
7644 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
7645 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
7646 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7647 0 : goto fail;
7648 :
7649 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
7650 : {
7651 126 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7652 126 : if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
7653 126 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
7654 0 : goto fail;
7655 126 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7656 126 : tree lrhs;
7657 126 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7658 : lrhs = rhs1;
7659 : else
7660 16 : lrhs = rhs2;
7661 126 : use_operand_p use_p;
7662 126 : imm_use_iterator iter;
7663 504 : FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
7664 : {
7665 252 : gimple *use_stmt = USE_STMT (use_p);
7666 252 : if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
7667 126 : continue;
7668 126 : if (other_store_stmt)
7669 0 : goto fail;
7670 126 : other_store_stmt = use_stmt;
7671 126 : }
7672 : }
7673 :
7674 512 : if (other_store_stmt == NULL)
7675 0 : goto fail;
7676 512 : if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
7677 512 : || !gimple_store_p (other_store_stmt))
7678 0 : goto fail;
7679 :
7680 512 : stmt_vec_info other_store_stmt_info
7681 512 : = loop_vinfo->lookup_stmt (other_store_stmt);
7682 512 : if (other_store_stmt_info == NULL
7683 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
7684 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7685 0 : goto fail;
7686 :
7687 512 : gimple *stmt1 = stmt;
7688 512 : gimple *stmt2 = other_store_stmt;
7689 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7690 : std::swap (stmt1, stmt2);
7691 512 : if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
7692 : gimple_assign_rhs1 (load2_stmt)))
7693 : {
7694 162 : std::swap (rhs1, rhs2);
7695 162 : std::swap (load1_stmt, load2_stmt);
7696 162 : std::swap (load1_stmt_info, load2_stmt_info);
7697 : }
7698 512 : if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
7699 : gimple_assign_rhs1 (load1_stmt)))
7700 0 : goto fail;
7701 :
7702 512 : tree var3 = NULL_TREE;
7703 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
7704 512 : && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
7705 : gimple_assign_rhs1 (load2_stmt)))
7706 0 : goto fail;
7707 512 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7708 : {
7709 252 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7710 252 : if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
7711 252 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
7712 0 : goto fail;
7713 252 : var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7714 252 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
7715 252 : || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
7716 504 : || lookup_attribute ("omp simd inscan exclusive",
7717 252 : DECL_ATTRIBUTES (var3)))
7718 0 : goto fail;
7719 : }
7720 :
7721 512 : dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
7722 512 : if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
7723 512 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
7724 0 : goto fail;
7725 :
7726 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7727 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
7728 512 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
7729 512 : || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
7730 1024 : || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7731 512 : == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
7732 0 : goto fail;
7733 :
7734 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7735 256 : std::swap (var1, var2);
7736 :
7737 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7738 : {
7739 252 : if (!lookup_attribute ("omp simd inscan exclusive",
7740 252 : DECL_ATTRIBUTES (var1)))
7741 0 : goto fail;
7742 252 : var1 = var3;
7743 : }
7744 :
7745 512 : if (loop_vinfo->scan_map == NULL)
7746 0 : goto fail;
7747 512 : tree *init = loop_vinfo->scan_map->get (var1);
7748 512 : if (init == NULL)
7749 0 : goto fail;
7750 :
7751 : /* The IL is as expected, now check if we can actually vectorize it.
7752 : Inclusive scan:
7753 : _26 = D.2043[_25];
7754 : _27 = D.2042[_25];
7755 : _28 = _26 + _27;
7756 : D.2043[_25] = _28;
7757 : D.2042[_25] = _28;
7758 : should be vectorized as (where _40 is the vectorized rhs
7759 : from the D.2042[_21] = 0; store):
7760 : _30 = MEM <vector(8) int> [(int *)&D.2043];
7761 : _31 = MEM <vector(8) int> [(int *)&D.2042];
7762 : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7763 : _33 = _31 + _32;
7764 : // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
7765 : _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7766 : _35 = _33 + _34;
7767 : // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7768 : // _31[1]+.._31[4], ... _31[4]+.._31[7] };
7769 : _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7770 : _37 = _35 + _36;
7771 : // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7772 : // _31[0]+.._31[4], ... _31[0]+.._31[7] };
7773 : _38 = _30 + _37;
7774 : _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7775 : MEM <vector(8) int> [(int *)&D.2043] = _39;
7776 : MEM <vector(8) int> [(int *)&D.2042] = _38;
7777 : Exclusive scan:
7778 : _26 = D.2043[_25];
7779 : D.2044[_25] = _26;
7780 : _27 = D.2042[_25];
7781 : _28 = _26 + _27;
7782 : D.2043[_25] = _28;
7783 : should be vectorized as (where _40 is the vectorized rhs
7784 : from the D.2042[_21] = 0; store):
7785 : _30 = MEM <vector(8) int> [(int *)&D.2043];
7786 : _31 = MEM <vector(8) int> [(int *)&D.2042];
7787 : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7788 : _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7789 : _34 = _32 + _33;
7790 : // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
7791 : // _31[3]+_31[4], ... _31[5]+.._31[6] };
7792 : _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7793 : _36 = _34 + _35;
7794 : // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7795 : // _31[1]+.._31[4], ... _31[3]+.._31[6] };
7796 : _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7797 : _38 = _36 + _37;
7798 : // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7799 : // _31[0]+.._31[4], ... _31[0]+.._31[6] };
7800 : _39 = _30 + _38;
7801 : _50 = _31 + _39;
7802 : _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7803 : MEM <vector(8) int> [(int *)&D.2044] = _39;
7804 : MEM <vector(8) int> [(int *)&D.2042] = _51; */
7805 512 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7806 512 : optab optab = optab_for_tree_code (code, vectype, optab_default);
7807 512 : if (!optab || !can_implement_p (optab, vec_mode))
7808 0 : goto fail;
7809 :
7810 512 : int units_log2 = scan_store_can_perm_p (vectype, *init);
7811 512 : if (units_log2 == -1)
7812 0 : goto fail;
7813 :
7814 : return true;
7815 : }
7816 :
7817 :
7818 : /* Function vectorizable_scan_store.
7819 :
7820 : Helper of vectorizable_score, arguments like on vectorizable_store.
7821 : Handle only the transformation, checking is done in check_scan_store. */
7822 :
7823 : static bool
7824 512 : vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
7825 : slp_tree slp_node, gimple_stmt_iterator *gsi)
7826 : {
7827 512 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7828 512 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7829 512 : tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
7830 512 : tree vectype = SLP_TREE_VECTYPE (slp_node);
7831 :
7832 512 : if (dump_enabled_p ())
7833 492 : dump_printf_loc (MSG_NOTE, vect_location,
7834 : "transform scan store.\n");
7835 :
7836 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7837 512 : tree rhs = gimple_assign_rhs1 (stmt);
7838 512 : gcc_assert (TREE_CODE (rhs) == SSA_NAME);
7839 :
7840 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7841 512 : bool inscan_var_store
7842 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7843 :
7844 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7845 : {
7846 126 : use_operand_p use_p;
7847 126 : imm_use_iterator iter;
7848 252 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7849 : {
7850 126 : gimple *use_stmt = USE_STMT (use_p);
7851 126 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7852 0 : continue;
7853 126 : rhs = gimple_assign_lhs (use_stmt);
7854 126 : break;
7855 126 : }
7856 : }
7857 :
7858 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7859 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7860 512 : if (code == POINTER_PLUS_EXPR)
7861 0 : code = PLUS_EXPR;
7862 512 : gcc_assert (TREE_CODE_LENGTH (code) == binary_op
7863 : && commutative_tree_code (code));
7864 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7865 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7866 512 : gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
7867 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7868 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7869 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7870 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7871 512 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7872 512 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7873 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7874 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7875 :
7876 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7877 : {
7878 436 : std::swap (rhs1, rhs2);
7879 436 : std::swap (var1, var2);
7880 436 : std::swap (load1_dr_info, load2_dr_info);
7881 : }
7882 :
7883 512 : tree *init = loop_vinfo->scan_map->get (var1);
7884 512 : gcc_assert (init);
7885 :
7886 512 : unsigned HOST_WIDE_INT nunits;
7887 512 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7888 : gcc_unreachable ();
7889 512 : auto_vec<enum scan_store_kind, 16> use_whole_vector;
7890 512 : int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
7891 512 : gcc_assert (units_log2 > 0);
7892 512 : auto_vec<tree, 16> perms;
7893 512 : perms.quick_grow (units_log2 + 1);
7894 512 : tree zero_vec = NULL_TREE, masktype = NULL_TREE;
7895 2392 : for (int i = 0; i <= units_log2; ++i)
7896 : {
7897 1880 : unsigned HOST_WIDE_INT j, k;
7898 1880 : vec_perm_builder sel (nunits, nunits, 1);
7899 1880 : sel.quick_grow (nunits);
7900 1880 : if (i == units_log2)
7901 4864 : for (j = 0; j < nunits; ++j)
7902 4352 : sel[j] = nunits - 1;
7903 : else
7904 : {
7905 5208 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7906 3840 : sel[j] = j;
7907 13208 : for (k = 0; j < nunits; ++j, ++k)
7908 11840 : sel[j] = nunits + k;
7909 : }
7910 3248 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7911 1880 : if (!use_whole_vector.is_empty ()
7912 0 : && use_whole_vector[i] != scan_store_kind_perm)
7913 : {
7914 0 : if (zero_vec == NULL_TREE)
7915 0 : zero_vec = build_zero_cst (vectype);
7916 0 : if (masktype == NULL_TREE
7917 0 : && use_whole_vector[i] == scan_store_kind_lshift_cond)
7918 0 : masktype = truth_type_for (vectype);
7919 0 : perms[i] = vect_gen_perm_mask_any (vectype, indices);
7920 : }
7921 : else
7922 1880 : perms[i] = vect_gen_perm_mask_checked (vectype, indices);
7923 1880 : }
7924 :
7925 512 : tree vec_oprnd1 = NULL_TREE;
7926 512 : tree vec_oprnd2 = NULL_TREE;
7927 512 : tree vec_oprnd3 = NULL_TREE;
7928 512 : tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
7929 512 : tree dataref_offset = build_int_cst (ref_type, 0);
7930 512 : tree bump = vect_get_data_ptr_bump (vinfo, dr_info, vectype, VMAT_CONTIGUOUS);
7931 512 : tree ldataref_ptr = NULL_TREE;
7932 512 : tree orig = NULL_TREE;
7933 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7934 126 : ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
7935 : /* The initialization is invariant. */
7936 512 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
7937 512 : auto_vec<tree> vec_oprnds2;
7938 512 : auto_vec<tree> vec_oprnds3;
7939 512 : if (ldataref_ptr == NULL)
7940 : {
7941 : /* We want to lookup the vector operands of the reduction, not those
7942 : of the store - for SLP we have to use the proper SLP node for the
7943 : lookup, which should be the single child of the scan store. */
7944 386 : vect_get_vec_defs (vinfo, SLP_TREE_CHILDREN (slp_node)[0],
7945 : rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
7946 : /* ??? For SLP we do not key the def on 'rhs1' or 'rhs2' but get
7947 : them in SLP child order. So we have to swap here with logic
7948 : similar to above. */
7949 386 : stmt_vec_info load
7950 386 : = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
7951 386 : (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
7952 386 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
7953 386 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7954 386 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
7955 820 : for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
7956 494 : std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
7957 : }
7958 : else
7959 126 : vect_get_vec_defs (vinfo, slp_node,
7960 : rhs2, &vec_oprnds3);
7961 1248 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
7962 : {
7963 736 : if (ldataref_ptr == NULL)
7964 554 : vec_oprnd2 = vec_oprnds2[j];
7965 736 : vec_oprnd3 = vec_oprnds3[j];
7966 736 : if (j == 0)
7967 : orig = vec_oprnd3;
7968 224 : else if (!inscan_var_store)
7969 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
7970 :
7971 736 : if (ldataref_ptr)
7972 : {
7973 182 : vec_oprnd2 = make_ssa_name (vectype);
7974 182 : tree data_ref = fold_build2 (MEM_REF, vectype,
7975 : unshare_expr (ldataref_ptr),
7976 : dataref_offset);
7977 182 : vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
7978 182 : gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
7979 182 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7980 : }
7981 :
7982 736 : tree v = vec_oprnd2;
7983 3068 : for (int i = 0; i < units_log2; ++i)
7984 : {
7985 2332 : tree new_temp = make_ssa_name (vectype);
7986 2332 : gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
7987 : (zero_vec
7988 0 : && (use_whole_vector[i]
7989 0 : != scan_store_kind_perm))
7990 : ? zero_vec : vec_oprnd1, v,
7991 2332 : perms[i]);
7992 2332 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
7993 :
7994 2332 : if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
7995 : {
7996 : /* Whole vector shift shifted in zero bits, but if *init
7997 : is not initializer_zerop, we need to replace those elements
7998 : with elements from vec_oprnd1. */
7999 0 : tree_vector_builder vb (masktype, nunits, 1);
8000 0 : for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
8001 0 : vb.quick_push (k < (HOST_WIDE_INT_1U << i)
8002 : ? boolean_false_node : boolean_true_node);
8003 :
8004 0 : tree new_temp2 = make_ssa_name (vectype);
8005 0 : g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
8006 : new_temp, vec_oprnd1);
8007 0 : vect_finish_stmt_generation (vinfo, stmt_info,
8008 : g, gsi);
8009 0 : new_temp = new_temp2;
8010 0 : }
8011 :
8012 : /* For exclusive scan, perform the perms[i] permutation once
8013 : more. */
8014 2332 : if (i == 0
8015 1100 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
8016 728 : && v == vec_oprnd2)
8017 : {
8018 364 : v = new_temp;
8019 364 : --i;
8020 364 : continue;
8021 : }
8022 :
8023 1968 : tree new_temp2 = make_ssa_name (vectype);
8024 1968 : g = gimple_build_assign (new_temp2, code, v, new_temp);
8025 1968 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8026 :
8027 1968 : v = new_temp2;
8028 : }
8029 :
8030 736 : tree new_temp = make_ssa_name (vectype);
8031 736 : gimple *g = gimple_build_assign (new_temp, code, orig, v);
8032 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8033 :
8034 736 : tree last_perm_arg = new_temp;
8035 : /* For exclusive scan, new_temp computed above is the exclusive scan
8036 : prefix sum. Turn it into inclusive prefix sum for the broadcast
8037 : of the last element into orig. */
8038 736 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8039 : {
8040 364 : last_perm_arg = make_ssa_name (vectype);
8041 364 : g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
8042 364 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8043 : }
8044 :
8045 736 : orig = make_ssa_name (vectype);
8046 2208 : g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
8047 736 : last_perm_arg, perms[units_log2]);
8048 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8049 :
8050 736 : if (!inscan_var_store)
8051 : {
8052 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8053 : unshare_expr (dataref_ptr),
8054 : dataref_offset);
8055 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8056 368 : g = gimple_build_assign (data_ref, new_temp);
8057 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8058 : }
8059 : }
8060 :
8061 512 : if (inscan_var_store)
8062 624 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8063 : {
8064 368 : if (j != 0)
8065 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8066 :
8067 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8068 : unshare_expr (dataref_ptr),
8069 : dataref_offset);
8070 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8071 368 : gimple *g = gimple_build_assign (data_ref, orig);
8072 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8073 : }
8074 512 : return true;
8075 512 : }
8076 :
8077 :
8078 : /* Function vectorizable_store.
8079 :
8080 : Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
8081 : that can be vectorized.
8082 : If COST_VEC is passed, calculate costs but don't change anything,
8083 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
8084 : it, and insert it at GSI.
8085 : Return true if STMT_INFO is vectorizable in this way. */
8086 :
8087 : static bool
8088 2142007 : vectorizable_store (vec_info *vinfo,
8089 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
8090 : slp_tree slp_node,
8091 : stmt_vector_for_cost *cost_vec)
8092 : {
8093 2142007 : tree data_ref;
8094 2142007 : tree vec_oprnd = NULL_TREE;
8095 2142007 : tree elem_type;
8096 2142007 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
8097 2142007 : class loop *loop = NULL;
8098 2142007 : machine_mode vec_mode;
8099 2142007 : tree dummy;
8100 2142007 : enum vect_def_type rhs_dt = vect_unknown_def_type;
8101 2142007 : enum vect_def_type mask_dt = vect_unknown_def_type;
8102 2142007 : tree dataref_ptr = NULL_TREE;
8103 2142007 : tree dataref_offset = NULL_TREE;
8104 2142007 : int j;
8105 2142007 : stmt_vec_info first_stmt_info;
8106 2142007 : bool grouped_store;
8107 2142007 : unsigned int group_size, i;
8108 2142007 : unsigned int vec_num;
8109 2142007 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
8110 2142007 : tree aggr_type;
8111 2142007 : poly_uint64 vf;
8112 2142007 : vec_load_store_type vls_type;
8113 2142007 : tree ref_type;
8114 :
8115 2142007 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
8116 : return false;
8117 :
8118 2142007 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
8119 243533 : && cost_vec)
8120 : return false;
8121 :
8122 : /* Is vectorizable store? */
8123 :
8124 1898474 : tree mask_vectype = NULL_TREE;
8125 1898474 : slp_tree mask_node = NULL;
8126 1898474 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
8127 : {
8128 1822462 : tree scalar_dest = gimple_assign_lhs (assign);
8129 1822462 : if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
8130 1822462 : && is_pattern_stmt_p (stmt_info))
8131 1680 : scalar_dest = TREE_OPERAND (scalar_dest, 0);
8132 1822462 : if (TREE_CODE (scalar_dest) != ARRAY_REF
8133 1822462 : && TREE_CODE (scalar_dest) != BIT_FIELD_REF
8134 : && TREE_CODE (scalar_dest) != INDIRECT_REF
8135 : && TREE_CODE (scalar_dest) != COMPONENT_REF
8136 : && TREE_CODE (scalar_dest) != IMAGPART_EXPR
8137 : && TREE_CODE (scalar_dest) != REALPART_EXPR
8138 : && TREE_CODE (scalar_dest) != MEM_REF)
8139 : return false;
8140 : }
8141 : else
8142 : {
8143 776078 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
8144 13889 : if (!call || !gimple_call_internal_p (call))
8145 : return false;
8146 :
8147 8376 : internal_fn ifn = gimple_call_internal_fn (call);
8148 8376 : if (!internal_store_fn_p (ifn))
8149 : return false;
8150 :
8151 1857 : int mask_index = internal_fn_mask_index (ifn);
8152 1857 : if (mask_index >= 0)
8153 1857 : mask_index = vect_slp_child_index_for_operand (stmt_info, mask_index);
8154 1857 : if (mask_index >= 0
8155 1857 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
8156 : &mask_node, &mask_dt,
8157 : &mask_vectype))
8158 : return false;
8159 : }
8160 :
8161 1379850 : tree vectype = SLP_TREE_VECTYPE (slp_node), rhs_vectype = NULL_TREE;
8162 1379850 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
8163 :
8164 1379850 : if (loop_vinfo)
8165 : {
8166 228221 : loop = LOOP_VINFO_LOOP (loop_vinfo);
8167 228221 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8168 : }
8169 : else
8170 : vf = 1;
8171 1379850 : vec_num = vect_get_num_copies (vinfo, slp_node);
8172 :
8173 : /* FORNOW. This restriction should be relaxed. */
8174 1379850 : if (loop
8175 1380125 : && nested_in_vect_loop_p (loop, stmt_info)
8176 1380133 : && vec_num > 1)
8177 : {
8178 8 : if (dump_enabled_p ())
8179 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8180 : "multiple types in nested loop.\n");
8181 8 : return false;
8182 : }
8183 :
8184 1379842 : slp_tree op_node;
8185 1379842 : if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
8186 : &op_node, &rhs_dt, &rhs_vectype, &vls_type))
8187 : return false;
8188 :
8189 1379818 : elem_type = TREE_TYPE (vectype);
8190 1379818 : vec_mode = TYPE_MODE (vectype);
8191 :
8192 1379818 : if (!STMT_VINFO_DATA_REF (stmt_info))
8193 : return false;
8194 :
8195 1379818 : vect_load_store_data _ls_data{};
8196 1379818 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
8197 1379818 : if (cost_vec
8198 1379818 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
8199 : vls_type, &_ls_data))
8200 : return false;
8201 : /* Temporary aliases to analysis data, should not be modified through
8202 : these. */
8203 1379202 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
8204 1379202 : const dr_alignment_support alignment_support_scheme
8205 : = ls.alignment_support_scheme;
8206 1379202 : const int misalignment = ls.misalignment;
8207 1379202 : const poly_int64 poffset = ls.poffset;
8208 :
8209 1379202 : if (slp_node->ldst_lanes
8210 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
8211 : {
8212 0 : if (dump_enabled_p ())
8213 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8214 : "discovered store-lane but cannot use it.\n");
8215 0 : return false;
8216 : }
8217 :
8218 1379202 : if (mask_node)
8219 : {
8220 1767 : if (memory_access_type == VMAT_CONTIGUOUS)
8221 : {
8222 616 : if (!VECTOR_MODE_P (vec_mode)
8223 3018 : || !can_vec_mask_load_store_p (vec_mode,
8224 1509 : TYPE_MODE (mask_vectype), false))
8225 114 : return false;
8226 : }
8227 258 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
8228 258 : && (!mat_gather_scatter_p (memory_access_type)
8229 242 : || (memory_access_type == VMAT_GATHER_SCATTER_LEGACY
8230 170 : && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
8231 : {
8232 16 : if (dump_enabled_p ())
8233 16 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8234 : "unsupported access type for masked store.\n");
8235 16 : return false;
8236 : }
8237 242 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
8238 : {
8239 72 : if (dump_enabled_p ())
8240 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8241 : "unsupported masked emulated scatter.\n");
8242 72 : return false;
8243 : }
8244 : }
8245 : else
8246 : {
8247 : /* FORNOW. In some cases can vectorize even if data-type not supported
8248 : (e.g. - array initialization with 0). */
8249 1377435 : if (!can_implement_p (mov_optab, vec_mode))
8250 : return false;
8251 : }
8252 :
8253 1379000 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
8254 1379000 : grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
8255 2550616 : && !mat_gather_scatter_p (memory_access_type));
8256 1171616 : if (grouped_store)
8257 : {
8258 1171616 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8259 1171616 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8260 1171616 : group_size = DR_GROUP_SIZE (first_stmt_info);
8261 : }
8262 : else
8263 : {
8264 1379000 : first_stmt_info = stmt_info;
8265 1379000 : first_dr_info = dr_info;
8266 : group_size = 1;
8267 : }
8268 :
8269 1379000 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && cost_vec)
8270 : {
8271 1076 : if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node,
8272 : mask_node, memory_access_type))
8273 : return false;
8274 : }
8275 :
8276 1931060 : bool costing_p = cost_vec;
8277 1378232 : if (costing_p) /* transformation not required. */
8278 : {
8279 826172 : if (loop_vinfo
8280 164032 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
8281 77336 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
8282 : vls_type, group_size, &ls,
8283 : mask_node);
8284 :
8285 826172 : if (!vect_maybe_update_slp_op_vectype (op_node, vectype)
8286 826172 : || (mask_node
8287 1038 : && !vect_maybe_update_slp_op_vectype (mask_node,
8288 : mask_vectype)))
8289 : {
8290 0 : if (dump_enabled_p ())
8291 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8292 : "incompatible vector types for invariants\n");
8293 0 : return false;
8294 : }
8295 :
8296 826172 : if (dump_enabled_p ()
8297 : && memory_access_type != VMAT_ELEMENTWISE
8298 15101 : && memory_access_type != VMAT_STRIDED_SLP
8299 14445 : && memory_access_type != VMAT_INVARIANT
8300 840617 : && alignment_support_scheme != dr_aligned)
8301 5005 : dump_printf_loc (MSG_NOTE, vect_location,
8302 : "Vectorizing an unaligned access.\n");
8303 : }
8304 :
8305 : /* Transform. */
8306 :
8307 552828 : if (!costing_p)
8308 552828 : ensure_base_align (dr_info);
8309 :
8310 1379000 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
8311 : {
8312 1024 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
8313 1024 : gcc_assert (SLP_TREE_LANES (slp_node) == 1);
8314 1024 : if (costing_p)
8315 : {
8316 512 : unsigned int inside_cost = 0, prologue_cost = 0;
8317 512 : if (vls_type == VLS_STORE_INVARIANT)
8318 0 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8319 : slp_node, 0, vect_prologue);
8320 512 : vect_get_store_cost (vinfo, stmt_info, slp_node, 1,
8321 : alignment_support_scheme, misalignment,
8322 : &inside_cost, cost_vec);
8323 :
8324 512 : if (dump_enabled_p ())
8325 492 : dump_printf_loc (MSG_NOTE, vect_location,
8326 : "vect_model_store_cost: inside_cost = %d, "
8327 : "prologue_cost = %d .\n",
8328 : inside_cost, prologue_cost);
8329 :
8330 512 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
8331 512 : slp_node->data = new vect_load_store_data (std::move (ls));
8332 :
8333 512 : return true;
8334 : }
8335 512 : return vectorizable_scan_store (vinfo, stmt_info, slp_node, gsi);
8336 : }
8337 :
8338 : /* FORNOW */
8339 1377976 : gcc_assert (!grouped_store
8340 : || !loop
8341 : || !nested_in_vect_loop_p (loop, stmt_info));
8342 :
8343 1377976 : grouped_store = false;
8344 1377976 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
8345 1377976 : gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
8346 : || (DR_GROUP_FIRST_ELEMENT (first_stmt_info) == first_stmt_info));
8347 1377976 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8348 :
8349 1377976 : ref_type = get_group_alias_ptr_type (first_stmt_info);
8350 :
8351 1377976 : if (!costing_p && dump_enabled_p ())
8352 12267 : dump_printf_loc (MSG_NOTE, vect_location, "transform store.\n");
8353 :
8354 1377976 : if (memory_access_type == VMAT_ELEMENTWISE
8355 1377976 : || memory_access_type == VMAT_STRIDED_SLP)
8356 : {
8357 29174 : unsigned inside_cost = 0, prologue_cost = 0;
8358 29174 : gimple_stmt_iterator incr_gsi;
8359 29174 : bool insert_after;
8360 29174 : tree offvar = NULL_TREE;
8361 29174 : tree ivstep;
8362 29174 : tree running_off;
8363 29174 : tree stride_base, stride_step, alias_off;
8364 29174 : tree vec_oprnd = NULL_TREE;
8365 29174 : tree dr_offset;
8366 : /* Checked by get_load_store_type. */
8367 29174 : unsigned int const_nunits = nunits.to_constant ();
8368 :
8369 29174 : gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
8370 29174 : gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
8371 :
8372 29174 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
8373 29174 : stride_base
8374 29174 : = fold_build_pointer_plus
8375 : (DR_BASE_ADDRESS (first_dr_info->dr),
8376 : size_binop (PLUS_EXPR,
8377 : convert_to_ptrofftype (dr_offset),
8378 : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
8379 29174 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
8380 :
8381 : /* For a store with loop-invariant (but other than power-of-2)
8382 : stride (i.e. not a grouped access) like so:
8383 :
8384 : for (i = 0; i < n; i += stride)
8385 : array[i] = ...;
8386 :
8387 : we generate a new induction variable and new stores from
8388 : the components of the (vectorized) rhs:
8389 :
8390 : for (j = 0; ; j += VF*stride)
8391 : vectemp = ...;
8392 : tmp1 = vectemp[0];
8393 : array[j] = tmp1;
8394 : tmp2 = vectemp[1];
8395 : array[j + stride] = tmp2;
8396 : ...
8397 : */
8398 :
8399 : /* ??? Modify local copies of alignment_support_scheme and
8400 : misalignment, but this part of analysis should be done
8401 : earlier and remembered, likewise the chosen load mode. */
8402 29174 : const dr_alignment_support tem = alignment_support_scheme;
8403 29174 : dr_alignment_support alignment_support_scheme = tem;
8404 29174 : const int tem2 = misalignment;
8405 29174 : int misalignment = tem2;
8406 :
8407 29174 : unsigned nstores = const_nunits;
8408 29174 : unsigned lnel = 1;
8409 29174 : tree ltype = elem_type;
8410 29174 : tree lvectype = vectype;
8411 29174 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
8412 29174 : if (n == const_nunits)
8413 : {
8414 2939 : int mis_align = dr_misalignment (first_dr_info, vectype);
8415 : /* With VF > 1 we advance the DR by step, if that is constant
8416 : and only aligned when performed VF times, DR alignment
8417 : analysis can analyze this as aligned since it assumes
8418 : contiguous accesses. But that is not how we code generate
8419 : here, so adjust for this. */
8420 2939 : if (maybe_gt (vf, 1u)
8421 4464 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8422 4235 : DR_TARGET_ALIGNMENT (first_dr_info)))
8423 229 : mis_align = -1;
8424 2939 : dr_alignment_support dr_align
8425 2939 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
8426 : mis_align);
8427 2939 : if (dr_align == dr_aligned
8428 2939 : || dr_align == dr_unaligned_supported)
8429 : {
8430 29174 : nstores = 1;
8431 29174 : lnel = const_nunits;
8432 29174 : ltype = vectype;
8433 29174 : lvectype = vectype;
8434 29174 : alignment_support_scheme = dr_align;
8435 29174 : misalignment = mis_align;
8436 : }
8437 : }
8438 26235 : else if (n > 1)
8439 : {
8440 1967 : nstores = const_nunits / n;
8441 1967 : lnel = n;
8442 1967 : ltype = build_vector_type (elem_type, n);
8443 1967 : lvectype = vectype;
8444 1967 : int mis_align = dr_misalignment (first_dr_info, ltype);
8445 1967 : if (maybe_gt (vf, 1u)
8446 3934 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8447 3292 : DR_TARGET_ALIGNMENT (first_dr_info)))
8448 642 : mis_align = -1;
8449 1967 : dr_alignment_support dr_align
8450 1967 : = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
8451 : mis_align);
8452 1967 : alignment_support_scheme = dr_align;
8453 1967 : misalignment = mis_align;
8454 :
8455 : /* First check if vec_extract optab doesn't support extraction
8456 : of vector elts directly. */
8457 1967 : scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
8458 1967 : machine_mode vmode;
8459 3934 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8460 2139 : || !related_vector_mode (TYPE_MODE (vectype), elmode,
8461 1967 : n).exists (&vmode)
8462 1773 : || (convert_optab_handler (vec_extract_optab,
8463 1773 : TYPE_MODE (vectype), vmode)
8464 : == CODE_FOR_nothing)
8465 1967 : || !(dr_align == dr_aligned
8466 172 : || dr_align == dr_unaligned_supported))
8467 : {
8468 : /* Try to avoid emitting an extract of vector elements
8469 : by performing the extracts using an integer type of the
8470 : same size, extracting from a vector of those and then
8471 : re-interpreting it as the original vector type if
8472 : supported. */
8473 1795 : unsigned lsize = n * GET_MODE_BITSIZE (elmode);
8474 1795 : unsigned int lnunits = const_nunits / n;
8475 : /* If we can't construct such a vector fall back to
8476 : element extracts from the original vector type and
8477 : element size stores. */
8478 1795 : if (int_mode_for_size (lsize, 0).exists (&elmode)
8479 1795 : && VECTOR_MODE_P (TYPE_MODE (vectype))
8480 1795 : && related_vector_mode (TYPE_MODE (vectype), elmode,
8481 1795 : lnunits).exists (&vmode)
8482 1767 : && (convert_optab_handler (vec_extract_optab,
8483 : vmode, elmode)
8484 : != CODE_FOR_nothing))
8485 : {
8486 1767 : nstores = lnunits;
8487 1767 : lnel = n;
8488 1767 : ltype = build_nonstandard_integer_type (lsize, 1);
8489 1767 : lvectype = build_vector_type (ltype, nstores);
8490 : }
8491 : /* Else fall back to vector extraction anyway.
8492 : Fewer stores are more important than avoiding spilling
8493 : of the vector we extract from. Compared to the
8494 : construction case in vectorizable_load no store-forwarding
8495 : issue exists here for reasonable archs. But only
8496 : if the store is supported. */
8497 28 : else if (!(dr_align == dr_aligned
8498 28 : || dr_align == dr_unaligned_supported))
8499 : {
8500 29174 : nstores = const_nunits;
8501 29174 : lnel = 1;
8502 29174 : ltype = elem_type;
8503 29174 : lvectype = vectype;
8504 : }
8505 : }
8506 : }
8507 :
8508 29174 : if (costing_p)
8509 : {
8510 : /* Record the decomposition type for target access during costing. */
8511 25804 : ls.ls_type = lvectype;
8512 25804 : ls.ls_eltype = ltype;
8513 : }
8514 : else
8515 3370 : gcc_assert (ls.ls_type == lvectype && ls.ls_eltype == ltype);
8516 :
8517 29174 : unsigned align;
8518 29174 : if (alignment_support_scheme == dr_aligned)
8519 1241 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
8520 : else
8521 27933 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
8522 : /* Alignment is at most the access size if we do multiple stores. */
8523 29174 : if (nstores > 1)
8524 26235 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
8525 29174 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
8526 29174 : int ncopies = vec_num;
8527 :
8528 29174 : if (!costing_p)
8529 : {
8530 3370 : ivstep = stride_step;
8531 :
8532 3370 : tree increment = fold_convert (TREE_TYPE (ivstep),
8533 : LOOP_VINFO_IV_INCREMENT (loop_vinfo));
8534 :
8535 3370 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
8536 : increment);
8537 :
8538 3370 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
8539 :
8540 3370 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
8541 3370 : if (LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo))
8542 3370 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
8543 : else
8544 0 : ivstep = force_gimple_operand_gsi (&incr_gsi, unshare_expr (ivstep),
8545 : true, NULL_TREE, true,
8546 : GSI_SAME_STMT);
8547 :
8548 3370 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
8549 : insert_after, &offvar, NULL,
8550 3370 : LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo));
8551 :
8552 3370 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
8553 : }
8554 :
8555 29174 : alias_off = build_int_cst (ref_type, 0);
8556 29174 : auto_vec<tree> vec_oprnds;
8557 : /* For costing some adjacent vector stores, we'd like to cost with
8558 : the total number of them once instead of cost each one by one. */
8559 29174 : unsigned int n_adjacent_stores = 0;
8560 29174 : running_off = offvar;
8561 29174 : if (!costing_p)
8562 3370 : vect_get_slp_defs (op_node, &vec_oprnds);
8563 29174 : unsigned int group_el = 0;
8564 29174 : unsigned HOST_WIDE_INT elsz
8565 29174 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
8566 69561 : for (j = 0; j < ncopies; j++)
8567 : {
8568 40387 : if (!costing_p)
8569 : {
8570 5206 : vec_oprnd = vec_oprnds[j];
8571 : /* Pun the vector to extract from if necessary. */
8572 5206 : if (lvectype != vectype)
8573 : {
8574 1008 : tree tem = make_ssa_name (lvectype);
8575 1008 : tree cvt = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
8576 1008 : gimple *pun = gimple_build_assign (tem, cvt);
8577 1008 : vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
8578 1008 : vec_oprnd = tem;
8579 : }
8580 : }
8581 179543 : for (i = 0; i < nstores; i++)
8582 : {
8583 139156 : if (costing_p)
8584 : {
8585 123301 : n_adjacent_stores++;
8586 123301 : continue;
8587 : }
8588 15855 : tree newref, newoff;
8589 15855 : gimple *incr, *assign;
8590 15855 : tree size = TYPE_SIZE (ltype);
8591 : /* Extract the i'th component. */
8592 15855 : tree pos = fold_build2 (MULT_EXPR, bitsizetype,
8593 : bitsize_int (i), size);
8594 15855 : tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
8595 : size, pos);
8596 :
8597 15855 : elem = force_gimple_operand_gsi (gsi, elem, true, NULL_TREE, true,
8598 : GSI_SAME_STMT);
8599 :
8600 15855 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
8601 15855 : group_el * elsz);
8602 15855 : newref = build2 (MEM_REF, ltype, running_off, this_off);
8603 15855 : vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
8604 :
8605 : /* And store it to *running_off. */
8606 15855 : assign = gimple_build_assign (newref, elem);
8607 15855 : vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
8608 :
8609 15855 : group_el += lnel;
8610 15855 : if (group_el == group_size)
8611 : {
8612 14218 : newoff = copy_ssa_name (running_off, NULL);
8613 14218 : incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
8614 : running_off, stride_step);
8615 14218 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
8616 :
8617 14218 : running_off = newoff;
8618 14218 : group_el = 0;
8619 : }
8620 : }
8621 : }
8622 :
8623 29174 : if (costing_p)
8624 : {
8625 25804 : if (n_adjacent_stores > 0)
8626 : {
8627 : /* Take a single lane vector type store as scalar
8628 : store to avoid ICE like 110776. */
8629 25804 : if (VECTOR_TYPE_P (ltype)
8630 25804 : && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
8631 1612 : vect_get_store_cost (vinfo, stmt_info, slp_node,
8632 : n_adjacent_stores, alignment_support_scheme,
8633 : misalignment, &inside_cost, cost_vec);
8634 : else
8635 24192 : inside_cost
8636 24192 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8637 : scalar_store, slp_node, 0, vect_body);
8638 : /* Only need vector deconstruction when there is more
8639 : than one store. */
8640 25804 : if (nstores > 1)
8641 23776 : inside_cost
8642 23776 : += record_stmt_cost (cost_vec, ncopies,
8643 : vec_deconstruct, slp_node, 0, vect_body);
8644 : }
8645 25804 : if (dump_enabled_p ())
8646 656 : dump_printf_loc (MSG_NOTE, vect_location,
8647 : "vect_model_store_cost: inside_cost = %d, "
8648 : "prologue_cost = %d .\n",
8649 : inside_cost, prologue_cost);
8650 :
8651 25804 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
8652 25804 : slp_node->data = new vect_load_store_data (std::move (ls));
8653 : }
8654 :
8655 29174 : return true;
8656 29174 : }
8657 :
8658 1348802 : gcc_assert (alignment_support_scheme);
8659 1348802 : vec_loop_masks *loop_masks
8660 197173 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
8661 1348802 : ? &LOOP_VINFO_MASKS (loop_vinfo)
8662 12 : : NULL);
8663 12 : vec_loop_lens *loop_lens
8664 197173 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
8665 : ? &LOOP_VINFO_LENS (loop_vinfo)
8666 0 : : NULL);
8667 :
8668 : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
8669 : are some difference here. We cannot enable both the lens and masks
8670 : during transform but it is allowed during analysis.
8671 : Shouldn't go with length-based approach if fully masked. */
8672 1348802 : if (cost_vec == NULL)
8673 : /* The cost_vec is NULL during transform. */
8674 548946 : gcc_assert ((!loop_lens || !loop_masks));
8675 :
8676 : /* Targets with store-lane instructions must not require explicit
8677 : realignment. vect_supportable_dr_alignment always returns either
8678 : dr_aligned or dr_unaligned_supported for masked operations. */
8679 1348802 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
8680 : && !mask_node
8681 : && !loop_masks)
8682 : || alignment_support_scheme == dr_aligned
8683 : || alignment_support_scheme == dr_unaligned_supported);
8684 :
8685 1348802 : tree offset = NULL_TREE;
8686 1348802 : if (!known_eq (poffset, 0))
8687 4647 : offset = size_int (poffset);
8688 :
8689 1348802 : tree dr_increment;
8690 1348802 : tree dr_bump;
8691 :
8692 1348802 : tree vec_offset = NULL_TREE;
8693 1348802 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8694 : {
8695 1464 : aggr_type = NULL_TREE;
8696 1464 : dr_increment = NULL_TREE;
8697 1464 : dr_bump = NULL_TREE;
8698 : }
8699 1347338 : else if (mat_gather_scatter_p (memory_access_type))
8700 : {
8701 0 : aggr_type = elem_type;
8702 0 : if (!costing_p)
8703 : {
8704 0 : tree vtype = ls.ls_type ? ls.ls_type : vectype;
8705 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vtype,
8706 : ls.strided_offset_vectype,
8707 : loop_vinfo, gsi,
8708 : &dr_increment, &dr_bump,
8709 : &vec_offset);
8710 : }
8711 : }
8712 : else
8713 : {
8714 1347338 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8715 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
8716 : else
8717 : aggr_type = vectype;
8718 1347338 : if (!costing_p)
8719 : {
8720 548475 : dr_increment = vect_get_data_ptr_step (vinfo, dr_info,
8721 : memory_access_type);
8722 548475 : dr_bump = vect_get_data_ptr_bump (vinfo, dr_info, aggr_type,
8723 : memory_access_type);
8724 : }
8725 : }
8726 :
8727 1348802 : if (loop_vinfo && mask_node && !costing_p)
8728 527 : LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
8729 :
8730 : /* In case the vectorization factor (VF) is bigger than the number
8731 : of elements that we can fit in a vectype (nunits), we have to generate
8732 : more than one vector stmt - i.e - we need to "unroll" the
8733 : vector stmt by a factor VF/nunits. */
8734 :
8735 1348802 : auto_vec<tree> dr_chain (group_size);
8736 1348802 : auto_vec<tree> vec_masks;
8737 1348802 : tree vec_mask = NULL;
8738 1348802 : auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
8739 6077238 : for (i = 0; i < group_size; i++)
8740 3379634 : gvec_oprnds.quick_push (new auto_vec<tree> ());
8741 :
8742 1348802 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8743 : {
8744 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
8745 :
8746 0 : if (costing_p)
8747 : /* Update all incoming store operand nodes, the general handling
8748 : above only handles the mask and the first store operand node. */
8749 0 : for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
8750 0 : if (child != mask_node
8751 0 : && !vect_maybe_update_slp_op_vectype (child, vectype))
8752 : {
8753 0 : if (dump_enabled_p ())
8754 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8755 : "incompatible vector types for invariants\n");
8756 0 : return false;
8757 : }
8758 0 : unsigned inside_cost = 0, prologue_cost = 0;
8759 : /* For costing some adjacent vector stores, we'd like to cost with
8760 : the total number of them once instead of cost each one by one. */
8761 0 : unsigned int n_adjacent_stores = 0;
8762 0 : int ncopies = vec_num / group_size;
8763 0 : for (j = 0; j < ncopies; j++)
8764 : {
8765 0 : if (j == 0)
8766 : {
8767 0 : if (!costing_p)
8768 : {
8769 0 : if (mask_node)
8770 : {
8771 0 : vect_get_slp_defs (mask_node, &vec_masks);
8772 0 : vec_mask = vec_masks[0];
8773 : }
8774 0 : dataref_ptr
8775 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8776 : aggr_type, NULL, offset, &dummy,
8777 : gsi, NULL, false, dr_increment);
8778 : }
8779 : }
8780 0 : else if (!costing_p)
8781 : {
8782 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8783 0 : if (mask_node)
8784 0 : vec_mask = vec_masks[j];
8785 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi,
8786 : stmt_info, dr_bump);
8787 : }
8788 :
8789 0 : if (costing_p)
8790 : {
8791 0 : n_adjacent_stores += group_size;
8792 0 : continue;
8793 : }
8794 :
8795 : /* Get an array into which we can store the individual vectors. */
8796 0 : tree vec_array = create_vector_array (vectype, group_size);
8797 :
8798 : /* Invalidate the current contents of VEC_ARRAY. This should
8799 : become an RTL clobber too, which prevents the vector registers
8800 : from being upward-exposed. */
8801 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8802 :
8803 : /* Store the individual vectors into the array. */
8804 0 : for (i = 0; i < group_size; i++)
8805 : {
8806 0 : slp_tree child;
8807 0 : if (i == 0 || !mask_node)
8808 0 : child = SLP_TREE_CHILDREN (slp_node)[i];
8809 : else
8810 0 : child = SLP_TREE_CHILDREN (slp_node)[i + 1];
8811 0 : vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
8812 0 : write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
8813 : i);
8814 : }
8815 :
8816 0 : tree final_mask = NULL;
8817 0 : tree final_len = NULL;
8818 0 : tree bias = NULL;
8819 0 : if (loop_masks)
8820 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
8821 : ncopies, vectype, j);
8822 0 : if (vec_mask)
8823 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
8824 : vec_mask, gsi);
8825 :
8826 0 : if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
8827 : {
8828 0 : if (loop_lens)
8829 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
8830 : ncopies, vectype, j, 1, true);
8831 : else
8832 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8833 0 : signed char biasval
8834 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8835 0 : bias = build_int_cst (intQI_type_node, biasval);
8836 0 : if (!final_mask)
8837 : {
8838 0 : mask_vectype = truth_type_for (vectype);
8839 0 : final_mask = build_minus_one_cst (mask_vectype);
8840 : }
8841 : }
8842 :
8843 0 : gcall *call;
8844 0 : if (final_len && final_mask)
8845 : {
8846 : /* Emit:
8847 : MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8848 : LEN, BIAS, VEC_ARRAY). */
8849 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8850 0 : tree alias_ptr = build_int_cst (ref_type, align);
8851 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
8852 : dataref_ptr, alias_ptr,
8853 : final_mask, final_len, bias,
8854 : vec_array);
8855 : }
8856 0 : else if (final_mask)
8857 : {
8858 : /* Emit:
8859 : MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8860 : VEC_ARRAY). */
8861 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8862 0 : tree alias_ptr = build_int_cst (ref_type, align);
8863 0 : call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
8864 : dataref_ptr, alias_ptr,
8865 : final_mask, vec_array);
8866 : }
8867 : else
8868 : {
8869 : /* Emit:
8870 : MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
8871 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
8872 0 : call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
8873 0 : gimple_call_set_lhs (call, data_ref);
8874 : }
8875 0 : gimple_call_set_nothrow (call, true);
8876 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
8877 :
8878 : /* Record that VEC_ARRAY is now dead. */
8879 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8880 : }
8881 :
8882 0 : if (costing_p)
8883 : {
8884 0 : if (n_adjacent_stores > 0)
8885 0 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
8886 : alignment_support_scheme, misalignment,
8887 : &inside_cost, cost_vec);
8888 0 : if (dump_enabled_p ())
8889 0 : dump_printf_loc (MSG_NOTE, vect_location,
8890 : "vect_model_store_cost: inside_cost = %d, "
8891 : "prologue_cost = %d .\n",
8892 : inside_cost, prologue_cost);
8893 :
8894 0 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
8895 0 : slp_node->data = new vect_load_store_data (std::move (ls));
8896 : }
8897 :
8898 0 : return true;
8899 : }
8900 :
8901 1348802 : if (mat_gather_scatter_p (memory_access_type))
8902 : {
8903 1464 : gcc_assert (!grouped_store || ls.ls_type);
8904 1464 : if (ls.ls_type)
8905 0 : vectype = ls.ls_type;
8906 1464 : auto_vec<tree> vec_offsets;
8907 1464 : unsigned int inside_cost = 0, prologue_cost = 0;
8908 1464 : int num_stmts = vec_num;
8909 3327 : for (j = 0; j < num_stmts; j++)
8910 : {
8911 1863 : gimple *new_stmt;
8912 1863 : if (j == 0)
8913 : {
8914 1464 : if (costing_p && vls_type == VLS_STORE_INVARIANT)
8915 210 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8916 : slp_node, 0, vect_prologue);
8917 : else if (!costing_p)
8918 : {
8919 : /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
8920 : DR_CHAIN is of size 1. */
8921 471 : gcc_assert (group_size == 1);
8922 471 : vect_get_slp_defs (op_node, gvec_oprnds[0]);
8923 471 : if (mask_node)
8924 70 : vect_get_slp_defs (mask_node, &vec_masks);
8925 :
8926 471 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8927 471 : vect_get_gather_scatter_ops (loop, slp_node,
8928 : &dataref_ptr, &vec_offsets);
8929 : else
8930 0 : dataref_ptr
8931 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8932 : aggr_type, NULL, offset,
8933 : &dummy, gsi, NULL, false,
8934 : dr_increment);
8935 : }
8936 : }
8937 399 : else if (!costing_p)
8938 : {
8939 35 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8940 35 : if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8941 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr,
8942 : gsi, stmt_info, dr_bump);
8943 : }
8944 :
8945 2579 : new_stmt = NULL;
8946 716 : if (!costing_p)
8947 : {
8948 506 : vec_oprnd = (*gvec_oprnds[0])[j];
8949 506 : if (mask_node)
8950 90 : vec_mask = vec_masks[j];
8951 : /* We should have caught mismatched types earlier. */
8952 506 : gcc_assert (ls.ls_type
8953 : || useless_type_conversion_p
8954 : (vectype, TREE_TYPE (vec_oprnd)));
8955 : }
8956 506 : tree final_mask = NULL_TREE;
8957 2369 : tree final_len = NULL_TREE;
8958 2369 : tree bias = NULL_TREE;
8959 506 : if (!costing_p)
8960 : {
8961 506 : if (loop_masks)
8962 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi,
8963 : loop_masks, num_stmts,
8964 : vectype, j);
8965 506 : if (vec_mask)
8966 90 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
8967 : final_mask, vec_mask, gsi);
8968 : }
8969 :
8970 1863 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
8971 1863 : tree alias_align_ptr = build_int_cst (ref_type, align);
8972 1863 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
8973 : {
8974 0 : if (costing_p)
8975 : {
8976 0 : if (ls.supported_offset_vectype
8977 0 : && !tree_nop_conversion_p (ls.supported_offset_vectype,
8978 : vec_offset))
8979 0 : inside_cost
8980 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
8981 : slp_node, 0, vect_body);
8982 0 : if (ls.supported_scale)
8983 0 : inside_cost
8984 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
8985 : slp_node, 0, vect_body);
8986 :
8987 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
8988 0 : inside_cost
8989 0 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
8990 : slp_node, 0, vect_body);
8991 1863 : continue;
8992 0 : }
8993 :
8994 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8995 0 : vec_offset = vec_offsets[j];
8996 :
8997 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
8998 0 : bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
8999 :
9000 : /* Perform the offset conversion and scaling if necessary. */
9001 0 : if (!strided
9002 0 : && (ls.supported_offset_vectype || ls.supported_scale))
9003 : {
9004 0 : gimple_seq stmts = NULL;
9005 0 : if (ls.supported_offset_vectype)
9006 0 : vec_offset = gimple_convert
9007 0 : (&stmts, ls.supported_offset_vectype, vec_offset);
9008 0 : if (ls.supported_scale)
9009 : {
9010 : /* Only scale the vec_offset if we haven't already. */
9011 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
9012 0 : || j == 0)
9013 : {
9014 0 : tree mult_cst = build_int_cst
9015 0 : (TREE_TYPE (TREE_TYPE (vec_offset)),
9016 0 : SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
9017 0 : tree mult = build_vector_from_val
9018 0 : (TREE_TYPE (vec_offset), mult_cst);
9019 0 : vec_offset = gimple_build
9020 0 : (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
9021 : vec_offset, mult);
9022 : }
9023 0 : scale = size_int (ls.supported_scale);
9024 : }
9025 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9026 : }
9027 :
9028 0 : if (ls.gs.ifn == IFN_MASK_LEN_SCATTER_STORE)
9029 : {
9030 0 : if (loop_lens)
9031 0 : final_len = vect_get_loop_len (loop_vinfo, gsi,
9032 : loop_lens, num_stmts,
9033 : vectype, j, 1, true);
9034 : else
9035 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9036 :
9037 0 : signed char biasval
9038 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9039 0 : bias = build_int_cst (intQI_type_node, biasval);
9040 0 : if (!final_mask)
9041 : {
9042 0 : mask_vectype = truth_type_for (vectype);
9043 0 : final_mask = build_minus_one_cst (mask_vectype);
9044 : }
9045 : }
9046 :
9047 0 : if (ls.ls_type)
9048 : {
9049 0 : gimple *conv_stmt
9050 0 : = gimple_build_assign (make_ssa_name (vectype),
9051 : VIEW_CONVERT_EXPR,
9052 : build1 (VIEW_CONVERT_EXPR, vectype,
9053 : vec_oprnd));
9054 0 : vect_finish_stmt_generation (vinfo, stmt_info, conv_stmt,
9055 : gsi);
9056 0 : vec_oprnd = gimple_get_lhs (conv_stmt);
9057 : }
9058 :
9059 0 : gcall *call;
9060 0 : if (final_len && final_mask)
9061 : {
9062 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
9063 0 : call = gimple_build_call_internal (
9064 : IFN_MASK_LEN_SCATTER_STORE, 8, dataref_ptr,
9065 : alias_align_ptr,
9066 : vec_offset, scale, vec_oprnd, final_mask, final_len,
9067 : bias);
9068 : else
9069 : /* Non-vector offset indicates that prefer to take
9070 : MASK_LEN_STRIDED_STORE instead of the
9071 : IFN_MASK_SCATTER_STORE with direct stride arg.
9072 : Similar to the gather case we have checked the
9073 : alignment for a scatter already and assume
9074 : that the strided store has the same requirements. */
9075 0 : call = gimple_build_call_internal (
9076 : IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
9077 : vec_offset, vec_oprnd, final_mask, final_len, bias);
9078 : }
9079 0 : else if (final_mask)
9080 0 : call = gimple_build_call_internal
9081 0 : (IFN_MASK_SCATTER_STORE, 6, dataref_ptr,
9082 : alias_align_ptr,
9083 : vec_offset, scale, vec_oprnd, final_mask);
9084 : else
9085 0 : call = gimple_build_call_internal (IFN_SCATTER_STORE, 5,
9086 : dataref_ptr,
9087 : alias_align_ptr,
9088 : vec_offset,
9089 : scale, vec_oprnd);
9090 0 : gimple_call_set_nothrow (call, true);
9091 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9092 0 : new_stmt = call;
9093 : }
9094 1863 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
9095 : {
9096 : /* The builtin decls path for scatter is legacy, x86 only. */
9097 330 : gcc_assert (nunits.is_constant ()
9098 : && (!final_mask
9099 : || SCALAR_INT_MODE_P
9100 : (TYPE_MODE (TREE_TYPE (final_mask)))));
9101 330 : if (costing_p)
9102 : {
9103 199 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9104 199 : inside_cost
9105 199 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9106 : slp_node, 0, vect_body);
9107 199 : continue;
9108 199 : }
9109 :
9110 131 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
9111 131 : poly_uint64 offset_nunits
9112 131 : = TYPE_VECTOR_SUBPARTS (offset_vectype);
9113 131 : if (known_eq (nunits, offset_nunits))
9114 : {
9115 55 : new_stmt = vect_build_one_scatter_store_call
9116 110 : (vinfo, stmt_info, slp_node, gsi,
9117 55 : ls.gs.decl, dataref_ptr, vec_offsets[j],
9118 : vec_oprnd, final_mask);
9119 55 : vect_finish_stmt_generation (vinfo, stmt_info,
9120 : new_stmt, gsi);
9121 : }
9122 76 : else if (known_eq (nunits, offset_nunits * 2))
9123 : {
9124 : /* We have a offset vector with half the number of
9125 : lanes but the builtins will store full vectype
9126 : data from the lower lanes. */
9127 30 : new_stmt = vect_build_one_scatter_store_call
9128 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
9129 30 : dataref_ptr, vec_offsets[2 * j],
9130 : vec_oprnd, final_mask);
9131 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9132 : new_stmt, gsi);
9133 30 : int count = nunits.to_constant ();
9134 30 : vec_perm_builder sel (count, count, 1);
9135 30 : sel.quick_grow (count);
9136 382 : for (int i = 0; i < count; ++i)
9137 352 : sel[i] = i | (count / 2);
9138 30 : vec_perm_indices indices (sel, 2, count);
9139 30 : tree perm_mask
9140 30 : = vect_gen_perm_mask_checked (vectype, indices);
9141 30 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
9142 : vec_oprnd, vec_oprnd,
9143 : perm_mask);
9144 30 : vec_oprnd = make_ssa_name (vectype);
9145 30 : gimple_set_lhs (new_stmt, vec_oprnd);
9146 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9147 : new_stmt, gsi);
9148 30 : if (final_mask)
9149 : {
9150 20 : new_stmt = gimple_build_assign (NULL_TREE,
9151 : VEC_UNPACK_HI_EXPR,
9152 : final_mask);
9153 20 : final_mask = make_ssa_name
9154 20 : (truth_type_for (offset_vectype));
9155 20 : gimple_set_lhs (new_stmt, final_mask);
9156 20 : vect_finish_stmt_generation (vinfo, stmt_info,
9157 : new_stmt, gsi);
9158 : }
9159 :
9160 30 : new_stmt = vect_build_one_scatter_store_call
9161 60 : (vinfo, stmt_info, slp_node, gsi, ls.gs.decl,
9162 30 : dataref_ptr, vec_offsets[2 * j + 1],
9163 : vec_oprnd, final_mask);
9164 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9165 : new_stmt, gsi);
9166 30 : }
9167 46 : else if (known_eq (nunits * 2, offset_nunits))
9168 : {
9169 : /* We have a offset vector with double the number of
9170 : lanes. Select the low/high part accordingly. */
9171 46 : vec_offset = vec_offsets[j / 2];
9172 46 : if (j & 1)
9173 : {
9174 23 : int count = offset_nunits.to_constant ();
9175 23 : vec_perm_builder sel (count, count, 1);
9176 23 : sel.quick_grow (count);
9177 263 : for (int i = 0; i < count; ++i)
9178 240 : sel[i] = i | (count / 2);
9179 23 : vec_perm_indices indices (sel, 2, count);
9180 23 : tree perm_mask = vect_gen_perm_mask_checked
9181 23 : (TREE_TYPE (vec_offset), indices);
9182 23 : new_stmt = gimple_build_assign (NULL_TREE,
9183 : VEC_PERM_EXPR,
9184 : vec_offset,
9185 : vec_offset,
9186 : perm_mask);
9187 23 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
9188 23 : gimple_set_lhs (new_stmt, vec_offset);
9189 23 : vect_finish_stmt_generation (vinfo, stmt_info,
9190 : new_stmt, gsi);
9191 23 : }
9192 :
9193 46 : new_stmt = vect_build_one_scatter_store_call
9194 46 : (vinfo, stmt_info, slp_node, gsi,
9195 : ls.gs.decl, dataref_ptr, vec_offset,
9196 : vec_oprnd, final_mask);
9197 46 : vect_finish_stmt_generation (vinfo, stmt_info,
9198 : new_stmt, gsi);
9199 : }
9200 : else
9201 0 : gcc_unreachable ();
9202 : }
9203 : else
9204 : {
9205 : /* Emulated scatter. */
9206 1533 : gcc_assert (!final_mask);
9207 1533 : if (costing_p)
9208 : {
9209 1158 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9210 : /* For emulated scatter N offset vector element extracts
9211 : (we assume the scalar scaling and ptr + offset add is
9212 : consumed by the load). */
9213 1158 : inside_cost
9214 1158 : += record_stmt_cost (cost_vec, 1, vec_deconstruct,
9215 : slp_node, 0, vect_body);
9216 : /* N scalar stores plus extracting the elements. */
9217 1158 : inside_cost
9218 1158 : += record_stmt_cost (cost_vec, 1, vec_deconstruct,
9219 : slp_node, 0, vect_body);
9220 1158 : inside_cost
9221 1158 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9222 : slp_node, 0, vect_body);
9223 1158 : continue;
9224 1158 : }
9225 :
9226 375 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
9227 375 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
9228 375 : unsigned HOST_WIDE_INT const_offset_nunits
9229 375 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
9230 375 : vec<constructor_elt, va_gc> *ctor_elts;
9231 375 : vec_alloc (ctor_elts, const_nunits);
9232 375 : gimple_seq stmts = NULL;
9233 375 : tree elt_type = TREE_TYPE (vectype);
9234 375 : unsigned HOST_WIDE_INT elt_size
9235 375 : = tree_to_uhwi (TYPE_SIZE (elt_type));
9236 : /* We support offset vectors with more elements
9237 : than the data vector for now. */
9238 375 : unsigned HOST_WIDE_INT factor
9239 : = const_offset_nunits / const_nunits;
9240 375 : vec_offset = vec_offsets[j / factor];
9241 375 : unsigned elt_offset
9242 375 : = (j % factor) * const_nunits;
9243 375 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
9244 375 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
9245 375 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
9246 1523 : for (unsigned k = 0; k < const_nunits; ++k)
9247 : {
9248 : /* Compute the offsetted pointer. */
9249 1148 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
9250 : bitsize_int (k + elt_offset));
9251 1148 : tree idx
9252 2296 : = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
9253 1148 : vec_offset, TYPE_SIZE (idx_type), boff);
9254 1148 : idx = gimple_convert (&stmts, sizetype, idx);
9255 1148 : idx = gimple_build (&stmts, MULT_EXPR, sizetype,
9256 : idx, scale);
9257 1148 : tree ptr
9258 1148 : = gimple_build (&stmts, PLUS_EXPR,
9259 1148 : TREE_TYPE (dataref_ptr),
9260 : dataref_ptr, idx);
9261 1148 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
9262 : /* Extract the element to be stored. */
9263 1148 : tree elt
9264 2296 : = gimple_build (&stmts, BIT_FIELD_REF,
9265 1148 : TREE_TYPE (vectype),
9266 1148 : vec_oprnd, TYPE_SIZE (elt_type),
9267 1148 : bitsize_int (k * elt_size));
9268 1148 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9269 1148 : stmts = NULL;
9270 1148 : tree ref
9271 1148 : = build2 (MEM_REF, ltype, ptr,
9272 : build_int_cst (ref_type, 0));
9273 1148 : new_stmt = gimple_build_assign (ref, elt);
9274 1148 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9275 : }
9276 :
9277 375 : slp_node->push_vec_def (new_stmt);
9278 : }
9279 : }
9280 :
9281 1464 : if (costing_p)
9282 : {
9283 993 : if (dump_enabled_p ())
9284 78 : dump_printf_loc (MSG_NOTE, vect_location,
9285 : "vect_model_store_cost: inside_cost = %d, "
9286 : "prologue_cost = %d .\n",
9287 : inside_cost, prologue_cost);
9288 993 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
9289 993 : slp_node->data = new vect_load_store_data (std::move (ls));
9290 : }
9291 :
9292 1464 : return true;
9293 1464 : }
9294 :
9295 1347338 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
9296 : || memory_access_type == VMAT_CONTIGUOUS_DOWN
9297 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
9298 :
9299 1347338 : unsigned inside_cost = 0, prologue_cost = 0;
9300 : /* For costing some adjacent vector stores, we'd like to cost with
9301 : the total number of them once instead of cost each one by one. */
9302 1347338 : unsigned int n_adjacent_stores = 0;
9303 1347338 : auto_vec<tree> result_chain (group_size);
9304 1347338 : auto_vec<tree, 1> vec_oprnds;
9305 1347338 : gimple *new_stmt;
9306 1347338 : if (!costing_p)
9307 : {
9308 : /* Get vectorized arguments for SLP_NODE. */
9309 548475 : vect_get_slp_defs (op_node, &vec_oprnds);
9310 548475 : vec_oprnd = vec_oprnds[0];
9311 548475 : if (mask_node)
9312 : {
9313 457 : vect_get_slp_defs (mask_node, &vec_masks);
9314 457 : vec_mask = vec_masks[0];
9315 : }
9316 : }
9317 :
9318 : /* We should have caught mismatched types earlier. */
9319 548475 : gcc_assert (costing_p
9320 : || useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
9321 1347338 : bool simd_lane_access_p
9322 1347338 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
9323 1347338 : if (!costing_p
9324 1347338 : && simd_lane_access_p
9325 4362 : && !loop_masks
9326 4362 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
9327 4362 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
9328 4362 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
9329 4362 : && integer_zerop (DR_INIT (first_dr_info->dr))
9330 1351700 : && alias_sets_conflict_p (get_alias_set (aggr_type),
9331 4362 : get_alias_set (TREE_TYPE (ref_type))))
9332 : {
9333 4354 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
9334 4354 : dataref_offset = build_int_cst (ref_type, 0);
9335 : }
9336 1342984 : else if (!costing_p)
9337 1088234 : dataref_ptr = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
9338 : simd_lane_access_p ? loop : NULL,
9339 : offset, &dummy, gsi, NULL,
9340 : simd_lane_access_p, dr_increment);
9341 :
9342 1347338 : new_stmt = NULL;
9343 1347338 : gcc_assert (!grouped_store);
9344 2995126 : for (i = 0; i < vec_num; i++)
9345 : {
9346 1647788 : if (!costing_p)
9347 679006 : vec_oprnd = vec_oprnds[i];
9348 :
9349 1647788 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
9350 : {
9351 3327 : if (costing_p)
9352 2189 : inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
9353 : slp_node, 0, vect_body);
9354 : else
9355 : {
9356 1138 : tree perm_mask = perm_mask_for_reverse (vectype);
9357 1138 : tree new_temp = make_ssa_name (vectype);
9358 :
9359 : /* Generate the permute statement. */
9360 1138 : gimple *perm_stmt
9361 1138 : = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
9362 : vec_oprnd, perm_mask);
9363 1138 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9364 :
9365 1138 : perm_stmt = SSA_NAME_DEF_STMT (new_temp);
9366 1647788 : vec_oprnd = new_temp;
9367 : }
9368 : }
9369 :
9370 1647788 : if (costing_p)
9371 : {
9372 968782 : n_adjacent_stores++;
9373 968782 : continue;
9374 : }
9375 :
9376 679006 : tree final_mask = NULL_TREE;
9377 679006 : tree final_len = NULL_TREE;
9378 679006 : tree bias = NULL_TREE;
9379 679006 : if (loop_masks)
9380 78 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9381 : vec_num, vectype, i);
9382 679006 : if (vec_mask)
9383 658 : vec_mask = vec_masks[i];
9384 658 : if (vec_mask)
9385 658 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9386 : vec_mask, gsi);
9387 :
9388 679006 : if (i > 0)
9389 : /* Bump the vector pointer. */
9390 130531 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi, stmt_info,
9391 : dr_bump);
9392 :
9393 679006 : unsigned misalign;
9394 679006 : unsigned HOST_WIDE_INT align;
9395 679006 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
9396 679006 : if (alignment_support_scheme == dr_aligned)
9397 : misalign = 0;
9398 310751 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
9399 : {
9400 162226 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
9401 162226 : misalign = 0;
9402 : }
9403 : else
9404 148525 : misalign = misalignment;
9405 679006 : if (dataref_offset == NULL_TREE
9406 673643 : && TREE_CODE (dataref_ptr) == SSA_NAME)
9407 184177 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
9408 679006 : align = least_bit_hwi (misalign | align);
9409 :
9410 : /* Compute IFN when LOOP_LENS or final_mask valid. */
9411 679006 : machine_mode vmode = TYPE_MODE (vectype);
9412 679006 : machine_mode new_vmode = vmode;
9413 679006 : internal_fn partial_ifn = IFN_LAST;
9414 679006 : if (loop_lens)
9415 : {
9416 0 : opt_machine_mode new_ovmode
9417 0 : = get_len_load_store_mode (vmode, false, &partial_ifn);
9418 0 : new_vmode = new_ovmode.require ();
9419 0 : unsigned factor
9420 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
9421 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
9422 : vec_num, vectype, i, factor, true);
9423 : }
9424 679006 : else if (final_mask)
9425 : {
9426 671 : if (!can_vec_mask_load_store_p (vmode,
9427 671 : TYPE_MODE (TREE_TYPE (final_mask)),
9428 : false, &partial_ifn))
9429 0 : gcc_unreachable ();
9430 : }
9431 :
9432 679006 : if (partial_ifn == IFN_MASK_LEN_STORE)
9433 : {
9434 0 : if (!final_len)
9435 : {
9436 : /* Pass VF value to 'len' argument of
9437 : MASK_LEN_STORE if LOOP_LENS is invalid. */
9438 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9439 : }
9440 0 : if (!final_mask)
9441 : {
9442 : /* Pass all ones value to 'mask' argument of
9443 : MASK_LEN_STORE if final_mask is invalid. */
9444 0 : mask_vectype = truth_type_for (vectype);
9445 0 : final_mask = build_minus_one_cst (mask_vectype);
9446 : }
9447 : }
9448 679006 : if (final_len)
9449 : {
9450 0 : signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9451 0 : bias = build_int_cst (intQI_type_node, biasval);
9452 : }
9453 :
9454 : /* Arguments are ready. Create the new vector stmt. */
9455 679006 : if (final_len)
9456 : {
9457 0 : gcall *call;
9458 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9459 : /* Need conversion if it's wrapped with VnQI. */
9460 0 : if (vmode != new_vmode)
9461 : {
9462 0 : tree new_vtype
9463 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
9464 : new_vmode);
9465 0 : tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
9466 0 : vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
9467 0 : gassign *new_stmt
9468 0 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
9469 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9470 0 : vec_oprnd = var;
9471 : }
9472 :
9473 0 : if (partial_ifn == IFN_MASK_LEN_STORE)
9474 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
9475 : dataref_ptr, ptr, final_mask,
9476 : final_len, bias, vec_oprnd);
9477 : else
9478 0 : call = gimple_build_call_internal (IFN_LEN_STORE, 5,
9479 : dataref_ptr, ptr, final_len,
9480 : bias, vec_oprnd);
9481 0 : gimple_call_set_nothrow (call, true);
9482 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9483 0 : new_stmt = call;
9484 : }
9485 679006 : else if (final_mask)
9486 : {
9487 671 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9488 671 : gcall *call
9489 671 : = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
9490 : ptr, final_mask, vec_oprnd);
9491 671 : gimple_call_set_nothrow (call, true);
9492 671 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9493 671 : new_stmt = call;
9494 : }
9495 : else
9496 : {
9497 678335 : data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr,
9498 : dataref_offset ? dataref_offset
9499 : : build_int_cst (ref_type, 0));
9500 678335 : if (alignment_support_scheme == dr_aligned
9501 678335 : && align >= TYPE_ALIGN_UNIT (vectype))
9502 : ;
9503 : else
9504 310238 : TREE_TYPE (data_ref)
9505 620476 : = build_aligned_type (TREE_TYPE (data_ref),
9506 : align * BITS_PER_UNIT);
9507 678335 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
9508 678335 : new_stmt = gimple_build_assign (data_ref, vec_oprnd);
9509 678335 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9510 : }
9511 : }
9512 :
9513 1347338 : if (costing_p)
9514 : {
9515 798863 : if (n_adjacent_stores > 0)
9516 798863 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9517 : alignment_support_scheme, misalignment,
9518 : &inside_cost, cost_vec);
9519 :
9520 : /* When vectorizing a store into the function result assign
9521 : a penalty if the function returns in a multi-register location.
9522 : In this case we assume we'll end up with having to spill the
9523 : vector result and do piecewise loads as a conservative estimate. */
9524 798863 : tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
9525 798863 : if (base
9526 798863 : && (TREE_CODE (base) == RESULT_DECL
9527 748432 : || (DECL_P (base) && cfun_returns (base)))
9528 861035 : && !aggregate_value_p (base, cfun->decl))
9529 : {
9530 11126 : rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
9531 : /* ??? Handle PARALLEL in some way. */
9532 11126 : if (REG_P (reg))
9533 : {
9534 10918 : int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
9535 : /* Assume that a single reg-reg move is possible and cheap,
9536 : do not account for vector to gp register move cost. */
9537 10918 : if (nregs > 1)
9538 : {
9539 : /* Spill. */
9540 10087 : prologue_cost
9541 10087 : += record_stmt_cost (cost_vec, 1, vector_store,
9542 : slp_node, 0, vect_epilogue);
9543 : /* Loads. */
9544 10087 : prologue_cost
9545 10087 : += record_stmt_cost (cost_vec, nregs, scalar_load,
9546 : slp_node, 0, vect_epilogue);
9547 : }
9548 : }
9549 : }
9550 798863 : if (dump_enabled_p ())
9551 13875 : dump_printf_loc (MSG_NOTE, vect_location,
9552 : "vect_model_store_cost: inside_cost = %d, "
9553 : "prologue_cost = %d .\n",
9554 : inside_cost, prologue_cost);
9555 :
9556 798863 : SLP_TREE_TYPE (slp_node) = store_vec_info_type;
9557 798863 : slp_node->data = new vect_load_store_data (std::move (ls));
9558 : }
9559 :
9560 1347338 : return true;
9561 2727156 : }
9562 :
9563 : /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
9564 : VECTOR_CST mask. No checks are made that the target platform supports the
9565 : mask, so callers may wish to test can_vec_perm_const_p separately, or use
9566 : vect_gen_perm_mask_checked. */
9567 :
9568 : tree
9569 62209 : vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
9570 : {
9571 62209 : tree mask_type;
9572 :
9573 62209 : poly_uint64 nunits = sel.length ();
9574 62209 : gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
9575 :
9576 62209 : mask_type = build_vector_type (ssizetype, nunits);
9577 62209 : return vec_perm_indices_to_tree (mask_type, sel);
9578 : }
9579 :
9580 : /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
9581 : i.e. that the target supports the pattern _for arbitrary input vectors_. */
9582 :
9583 : tree
9584 59342 : vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
9585 : {
9586 59342 : machine_mode vmode = TYPE_MODE (vectype);
9587 59342 : gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
9588 59342 : return vect_gen_perm_mask_any (vectype, sel);
9589 : }
9590 :
9591 : /* Given a vector variable X and Y, that was generated for the scalar
9592 : STMT_INFO, generate instructions to permute the vector elements of X and Y
9593 : using permutation mask MASK_VEC, insert them at *GSI and return the
9594 : permuted vector variable. */
9595 :
9596 : static tree
9597 1444 : permute_vec_elements (vec_info *vinfo,
9598 : tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
9599 : gimple_stmt_iterator *gsi)
9600 : {
9601 1444 : tree vectype = TREE_TYPE (x);
9602 1444 : tree perm_dest, data_ref;
9603 1444 : gimple *perm_stmt;
9604 :
9605 1444 : tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
9606 1444 : if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
9607 1444 : perm_dest = vect_create_destination_var (scalar_dest, vectype);
9608 : else
9609 0 : perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
9610 1444 : data_ref = make_ssa_name (perm_dest);
9611 :
9612 : /* Generate the permute statement. */
9613 1444 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
9614 1444 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9615 :
9616 1444 : return data_ref;
9617 : }
9618 :
9619 : /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
9620 : inserting them on the loops preheader edge. Returns true if we
9621 : were successful in doing so (and thus STMT_INFO can be moved then),
9622 : otherwise returns false. HOIST_P indicates if we want to hoist the
9623 : definitions of all SSA uses, it would be false when we are costing. */
9624 :
9625 : static bool
9626 3998 : hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
9627 : {
9628 3998 : ssa_op_iter i;
9629 3998 : use_operand_p use_p;
9630 3998 : auto_vec<use_operand_p, 8> to_hoist;
9631 :
9632 7563 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
9633 : {
9634 3595 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9635 3595 : if (!gimple_nop_p (def_stmt)
9636 3595 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
9637 : {
9638 : /* Make sure we don't need to recurse. While we could do
9639 : so in simple cases when there are more complex use webs
9640 : we don't have an easy way to preserve stmt order to fulfil
9641 : dependencies within them. */
9642 93 : tree op2;
9643 93 : ssa_op_iter i2;
9644 93 : if (gimple_code (def_stmt) == GIMPLE_PHI
9645 93 : || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
9646 : == NULL_DEF_OPERAND_P))
9647 30 : return false;
9648 188 : FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
9649 : {
9650 125 : gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
9651 125 : if (!gimple_nop_p (def_stmt2)
9652 125 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
9653 : return false;
9654 : }
9655 63 : to_hoist.safe_push (use_p);
9656 : }
9657 : }
9658 :
9659 7936 : if (to_hoist.is_empty ())
9660 : return true;
9661 :
9662 39 : if (!hoist_p)
9663 : return true;
9664 :
9665 : /* Instead of moving defs we copy them so we can zero their UID to not
9666 : confuse dominance queries in the preheader. */
9667 5 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9668 20 : for (use_operand_p use_p : to_hoist)
9669 : {
9670 5 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9671 5 : gimple *copy = gimple_copy (def_stmt);
9672 5 : gimple_set_uid (copy, 0);
9673 5 : def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
9674 5 : tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
9675 5 : update_stmt (copy);
9676 5 : def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
9677 5 : SET_DEF (def_p, new_def);
9678 5 : SET_USE (use_p, new_def);
9679 5 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
9680 : }
9681 :
9682 : return true;
9683 3998 : }
9684 :
9685 : /* vectorizable_load.
9686 :
9687 : Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
9688 : that can be vectorized.
9689 : If COST_VEC is passed, calculate costs but don't change anything,
9690 : otherwise, vectorize STMT_INFO: create a vectorized stmt to replace
9691 : it, and insert it at GSI.
9692 : Return true if STMT_INFO is vectorizable in this way. */
9693 :
9694 : static bool
9695 2215169 : vectorizable_load (vec_info *vinfo,
9696 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
9697 : slp_tree slp_node,
9698 : stmt_vector_for_cost *cost_vec)
9699 : {
9700 2215169 : tree scalar_dest;
9701 2215169 : tree vec_dest = NULL;
9702 2215169 : tree data_ref = NULL;
9703 2215169 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
9704 2215169 : class loop *loop = NULL;
9705 2215169 : class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
9706 2215169 : bool nested_in_vect_loop = false;
9707 2215169 : tree elem_type;
9708 : /* Avoid false positive uninitialized warning, see PR110652. */
9709 2215169 : tree new_temp = NULL_TREE;
9710 2215169 : machine_mode mode;
9711 2215169 : tree dummy;
9712 2215169 : tree dataref_ptr = NULL_TREE;
9713 2215169 : tree dataref_offset = NULL_TREE;
9714 2215169 : int i, j;
9715 2215169 : unsigned int group_size;
9716 2215169 : poly_uint64 group_gap_adj;
9717 2215169 : tree msq = NULL_TREE, lsq;
9718 2215169 : tree realignment_token = NULL_TREE;
9719 2215169 : gphi *phi = NULL;
9720 2215169 : bool grouped_load = false;
9721 2215169 : stmt_vec_info first_stmt_info;
9722 2215169 : stmt_vec_info first_stmt_info_for_drptr = NULL;
9723 2215169 : bool compute_in_loop = false;
9724 2215169 : class loop *at_loop;
9725 2215169 : int vec_num;
9726 2215169 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
9727 2215169 : poly_uint64 vf;
9728 2215169 : tree aggr_type;
9729 2215169 : tree ref_type;
9730 2215169 : enum vect_def_type mask_dt = vect_unknown_def_type;
9731 2215169 : enum vect_def_type els_dt = vect_unknown_def_type;
9732 :
9733 2215169 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
9734 : return false;
9735 :
9736 2215169 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
9737 243533 : && cost_vec)
9738 : return false;
9739 :
9740 1971636 : if (!STMT_VINFO_DATA_REF (stmt_info))
9741 : return false;
9742 :
9743 1566442 : tree mask_vectype = NULL_TREE;
9744 1566442 : tree els = NULL_TREE; tree els_vectype = NULL_TREE;
9745 :
9746 1566442 : int mask_index = -1;
9747 1566442 : int els_index = -1;
9748 1566442 : slp_tree mask_node = NULL;
9749 1566442 : slp_tree els_op = NULL;
9750 1566442 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
9751 : {
9752 1562022 : scalar_dest = gimple_assign_lhs (assign);
9753 1562022 : if (TREE_CODE (scalar_dest) != SSA_NAME)
9754 : return false;
9755 :
9756 736330 : tree_code code = gimple_assign_rhs_code (assign);
9757 736330 : if (code != ARRAY_REF
9758 736330 : && code != BIT_FIELD_REF
9759 736330 : && code != INDIRECT_REF
9760 512067 : && code != COMPONENT_REF
9761 512067 : && code != IMAGPART_EXPR
9762 365850 : && code != REALPART_EXPR
9763 365850 : && code != MEM_REF
9764 294 : && TREE_CODE_CLASS (code) != tcc_declaration)
9765 : return false;
9766 : }
9767 : else
9768 : {
9769 1480567 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
9770 4420 : if (!call || !gimple_call_internal_p (call))
9771 : return false;
9772 :
9773 4420 : internal_fn ifn = gimple_call_internal_fn (call);
9774 4420 : if (!internal_load_fn_p (ifn))
9775 : return false;
9776 :
9777 3090 : scalar_dest = gimple_call_lhs (call);
9778 3090 : if (!scalar_dest)
9779 : return false;
9780 :
9781 3090 : mask_index = internal_fn_mask_index (ifn);
9782 3090 : if (mask_index >= 0)
9783 3090 : mask_index = vect_slp_child_index_for_operand (stmt_info, mask_index);
9784 3090 : if (mask_index >= 0
9785 3090 : && !vect_check_scalar_mask (vinfo, slp_node, mask_index,
9786 : &mask_node, &mask_dt, &mask_vectype))
9787 : return false;
9788 :
9789 3090 : els_index = internal_fn_else_index (ifn);
9790 3090 : if (els_index >= 0)
9791 3090 : els_index = vect_slp_child_index_for_operand (stmt_info, els_index);
9792 3090 : if (els_index >= 0
9793 3090 : && !vect_is_simple_use (vinfo, slp_node, els_index,
9794 : &els, &els_op, &els_dt, &els_vectype))
9795 : return false;
9796 : }
9797 :
9798 739353 : tree vectype = SLP_TREE_VECTYPE (slp_node);
9799 739353 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9800 :
9801 739353 : if (loop_vinfo)
9802 : {
9803 509340 : loop = LOOP_VINFO_LOOP (loop_vinfo);
9804 509340 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
9805 509340 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
9806 : }
9807 : else
9808 : vf = 1;
9809 :
9810 739353 : vec_num = vect_get_num_copies (vinfo, slp_node);
9811 :
9812 : /* FORNOW. This restriction should be relaxed. */
9813 739353 : if (nested_in_vect_loop && vec_num > 1)
9814 : {
9815 319 : if (dump_enabled_p ())
9816 69 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9817 : "multiple types in nested loop.\n");
9818 319 : return false;
9819 : }
9820 :
9821 739034 : elem_type = TREE_TYPE (vectype);
9822 739034 : mode = TYPE_MODE (vectype);
9823 :
9824 : /* FORNOW. In some cases can vectorize even if data-type not supported
9825 : (e.g. - data copies). */
9826 739034 : if (!can_implement_p (mov_optab, mode))
9827 : {
9828 0 : if (dump_enabled_p ())
9829 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9830 : "Aligned load, but unsupported type.\n");
9831 0 : return false;
9832 : }
9833 :
9834 : /* Check if the load is a part of an interleaving chain. */
9835 739034 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
9836 : {
9837 324788 : grouped_load = true;
9838 : /* FORNOW */
9839 324788 : gcc_assert (!nested_in_vect_loop);
9840 324788 : gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9841 :
9842 324788 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
9843 324788 : group_size = DR_GROUP_SIZE (first_stmt_info);
9844 :
9845 : /* Invalidate assumptions made by dependence analysis when vectorization
9846 : on the unrolled body effectively re-orders stmts. */
9847 324788 : if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
9848 324788 : && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
9849 : STMT_VINFO_MIN_NEG_DIST (stmt_info)))
9850 : {
9851 12 : if (dump_enabled_p ())
9852 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9853 : "cannot perform implicit CSE when performing "
9854 : "group loads with negative dependence distance\n");
9855 12 : return false;
9856 : }
9857 : }
9858 : else
9859 : group_size = 1;
9860 :
9861 739022 : vect_load_store_data _ls_data{};
9862 739022 : vect_load_store_data &ls = slp_node->get_data (_ls_data);
9863 739022 : if (cost_vec
9864 739022 : && !get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask_node,
9865 : VLS_LOAD, &ls))
9866 : return false;
9867 : /* Temporary aliases to analysis data, should not be modified through
9868 : these. */
9869 626891 : const vect_memory_access_type memory_access_type = ls.memory_access_type;
9870 626891 : const dr_alignment_support alignment_support_scheme
9871 : = ls.alignment_support_scheme;
9872 626891 : const int misalignment = ls.misalignment;
9873 626891 : const poly_int64 poffset = ls.poffset;
9874 626891 : const vec<int> &elsvals = ls.elsvals;
9875 :
9876 626891 : int maskload_elsval = 0;
9877 626891 : bool need_zeroing = false;
9878 :
9879 : /* We might need to explicitly zero inactive elements if there are
9880 : padding bits in the type that might leak otherwise.
9881 : Refer to PR115336. */
9882 626891 : tree scalar_type = TREE_TYPE (scalar_dest);
9883 626891 : bool type_mode_padding_p
9884 1253782 : = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
9885 :
9886 626891 : if (slp_node->ldst_lanes
9887 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
9888 : {
9889 0 : if (dump_enabled_p ())
9890 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9891 : "discovered load-lane but cannot use it.\n");
9892 0 : return false;
9893 : }
9894 :
9895 626891 : if (mask_node)
9896 : {
9897 2960 : if (memory_access_type == VMAT_CONTIGUOUS)
9898 : {
9899 2079 : machine_mode vec_mode = TYPE_MODE (vectype);
9900 720 : if (!VECTOR_MODE_P (vec_mode)
9901 4158 : || !can_vec_mask_load_store_p (vec_mode,
9902 2079 : TYPE_MODE (mask_vectype),
9903 : true, NULL, &ls.elsvals))
9904 351 : return false;
9905 : }
9906 881 : else if (memory_access_type == VMAT_ELEMENTWISE
9907 881 : || memory_access_type == VMAT_STRIDED_SLP)
9908 : {
9909 0 : if (dump_enabled_p ())
9910 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9911 : "unsupported masked strided access.\n");
9912 0 : return false;
9913 : }
9914 881 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
9915 881 : && !mat_gather_scatter_p (memory_access_type))
9916 : {
9917 62 : if (dump_enabled_p ())
9918 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9919 : "unsupported access type for masked load.\n");
9920 62 : return false;
9921 : }
9922 819 : else if (memory_access_type == VMAT_GATHER_SCATTER_EMULATED)
9923 : {
9924 488 : if (dump_enabled_p ())
9925 28 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9926 : "unsupported masked emulated gather.\n");
9927 488 : return false;
9928 : }
9929 : }
9930 :
9931 625990 : bool costing_p = cost_vec;
9932 :
9933 625990 : if (costing_p) /* transformation not required. */
9934 : {
9935 456540 : if (loop_vinfo
9936 322977 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
9937 218840 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
9938 : VLS_LOAD, group_size, &ls,
9939 : mask_node, &ls.elsvals);
9940 :
9941 : /* If the type needs padding we must zero inactive elements.
9942 : Check if we can do that with a VEC_COND_EXPR and store the
9943 : elsval we choose in MASKLOAD_ELSVAL. */
9944 456540 : if (ls.elsvals.length ()
9945 61080 : && type_mode_padding_p
9946 7 : && !ls.elsvals.contains (MASK_LOAD_ELSE_ZERO)
9947 61080 : && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
9948 : {
9949 0 : if (dump_enabled_p ())
9950 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9951 : "cannot zero inactive elements.\n");
9952 0 : return false;
9953 : }
9954 :
9955 456540 : if (mask_node
9956 456540 : && !vect_maybe_update_slp_op_vectype (mask_node,
9957 : mask_vectype))
9958 : {
9959 0 : if (dump_enabled_p ())
9960 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9961 : "incompatible vector types for invariants\n");
9962 0 : return false;
9963 : }
9964 :
9965 456540 : if (dump_enabled_p ()
9966 25765 : && memory_access_type != VMAT_ELEMENTWISE
9967 25654 : && !mat_gather_scatter_p (memory_access_type)
9968 25339 : && memory_access_type != VMAT_STRIDED_SLP
9969 25339 : && memory_access_type != VMAT_INVARIANT
9970 480954 : && alignment_support_scheme != dr_aligned)
9971 9987 : dump_printf_loc (MSG_NOTE, vect_location,
9972 : "Vectorizing an unaligned access.\n");
9973 :
9974 456540 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
9975 0 : vinfo->any_known_not_updated_vssa = true;
9976 : }
9977 :
9978 : /* For now just use the first available else value.
9979 : get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
9980 : select it here if it is supported. */
9981 625990 : if (elsvals.length ())
9982 84312 : maskload_elsval = *elsvals.begin ();
9983 :
9984 625990 : if (dump_enabled_p () && !costing_p)
9985 16714 : dump_printf_loc (MSG_NOTE, vect_location, "transform load.\n");
9986 :
9987 : /* Transform. */
9988 :
9989 625990 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
9990 625990 : if (!costing_p)
9991 169450 : ensure_base_align (dr_info);
9992 :
9993 625990 : if (memory_access_type == VMAT_INVARIANT)
9994 : {
9995 4136 : gcc_assert (!grouped_load && !mask_node && !bb_vinfo);
9996 : /* If we have versioned for aliasing or the loop doesn't
9997 : have any data dependencies that would preclude this,
9998 : then we are sure this is a loop invariant load and
9999 : thus we can insert it on the preheader edge.
10000 : TODO: hoist_defs_of_uses should ideally be computed
10001 : once at analysis time, remembered and used in the
10002 : transform time. */
10003 8272 : bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
10004 4136 : && !nested_in_vect_loop);
10005 :
10006 4136 : bool uniform_p = true;
10007 17322 : for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
10008 : {
10009 : /* It is unsafe to hoist a conditional load over the conditions that
10010 : make it valid. When early break this means that any invariant load
10011 : can't be hoisted unless it's in the loop header or if we know
10012 : something else has verified the load is valid to do. Alignment
10013 : peeling would do this since getting through the prologue means the
10014 : load was done at least once and so the vector main body is free to
10015 : hoist it. However today GCC will hoist the load above the PFA
10016 : loop. As such that makes it still invalid and so we can't allow it
10017 : today. */
10018 4914 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
10019 1090 : && !DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (sinfo))
10020 5968 : && gimple_bb (STMT_VINFO_STMT (vect_orig_stmt (sinfo)))
10021 1054 : != loop->header)
10022 : {
10023 954 : if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
10024 954 : && dump_enabled_p ())
10025 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10026 : "not hoisting invariant load due to early break"
10027 : "constraints\n");
10028 948 : else if (dump_enabled_p ())
10029 16 : dump_printf_loc (MSG_NOTE, vect_location,
10030 : "not hoisting invariant load due to early break"
10031 : "constraints\n");
10032 : hoist_p = false;
10033 : }
10034 :
10035 3960 : hoist_p = hoist_p && hoist_defs_of_uses (sinfo->stmt, loop, false);
10036 4914 : if (sinfo != SLP_TREE_SCALAR_STMTS (slp_node)[0])
10037 279 : uniform_p = false;
10038 : }
10039 4136 : if (costing_p)
10040 : {
10041 3305 : if (!uniform_p && (!hoist_p || !vf.is_constant ()))
10042 : {
10043 0 : if (dump_enabled_p ())
10044 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10045 : "not vectorizing non-uniform invariant "
10046 : "load\n");
10047 0 : return false;
10048 : }
10049 1469 : enum vect_cost_model_location cost_loc
10050 3305 : = hoist_p ? vect_prologue : vect_body;
10051 3305 : unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
10052 : slp_node, 0, cost_loc);
10053 3305 : cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
10054 : slp_node, 0, cost_loc);
10055 3305 : unsigned int prologue_cost = hoist_p ? cost : 0;
10056 1469 : unsigned int inside_cost = hoist_p ? 0 : cost;
10057 3305 : if (dump_enabled_p ())
10058 538 : dump_printf_loc (MSG_NOTE, vect_location,
10059 : "vect_model_load_cost: inside_cost = %d, "
10060 : "prologue_cost = %d .\n",
10061 : inside_cost, prologue_cost);
10062 3305 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
10063 3305 : slp_node->data = new vect_load_store_data (std::move (ls));
10064 3305 : return true;
10065 : }
10066 831 : if (hoist_p)
10067 : {
10068 : /* ??? For non-uniform lanes there could be still duplicates.
10069 : We're leaving those to post-vectorizer CSE for the moment. */
10070 634 : auto_vec<tree> scalar_defs (SLP_TREE_LANES (slp_node));
10071 2043 : for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
10072 : {
10073 724 : gassign *stmt = as_a <gassign *> (sinfo->stmt);
10074 724 : if (dump_enabled_p ())
10075 352 : dump_printf_loc (MSG_NOTE, vect_location,
10076 : "hoisting out of the vectorized loop: %G",
10077 : (gimple *) stmt);
10078 724 : scalar_dest = copy_ssa_name (gimple_assign_lhs (stmt));
10079 724 : tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
10080 724 : edge pe = loop_preheader_edge (loop);
10081 724 : gphi *vphi = get_virtual_phi (loop->header);
10082 724 : tree vuse;
10083 724 : if (vphi)
10084 718 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
10085 : else
10086 6 : vuse = gimple_vuse (gsi_stmt (*gsi));
10087 724 : gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
10088 724 : gimple_set_vuse (new_stmt, vuse);
10089 724 : gsi_insert_on_edge_immediate (pe, new_stmt);
10090 724 : hoist_defs_of_uses (new_stmt, loop, true);
10091 724 : if (!useless_type_conversion_p (TREE_TYPE (vectype),
10092 724 : TREE_TYPE (scalar_dest)))
10093 : {
10094 14 : tree tem = make_ssa_name (TREE_TYPE (vectype));
10095 14 : new_stmt = gimple_build_assign (tem,
10096 : NOP_EXPR, scalar_dest);
10097 14 : gsi_insert_on_edge_immediate (pe, new_stmt);
10098 14 : scalar_dest = tem;
10099 : }
10100 724 : scalar_defs.quick_push (scalar_dest);
10101 724 : if (uniform_p)
10102 : break;
10103 : }
10104 634 : if (!uniform_p)
10105 : {
10106 51 : unsigned const_nunits
10107 51 : = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
10108 116 : for (j = 0; j < (int) vec_num; ++j)
10109 : {
10110 65 : vec<constructor_elt, va_gc> *v = NULL;
10111 65 : vec_safe_reserve (v, const_nunits, true);
10112 369 : for (unsigned i = 0; i < const_nunits; ++i)
10113 : {
10114 304 : unsigned def_idx
10115 304 : = (j * const_nunits + i) % SLP_TREE_LANES (slp_node);
10116 304 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
10117 : scalar_defs[def_idx]);
10118 : }
10119 65 : scalar_dest = build_constructor (vectype, v);
10120 65 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10121 : vectype, NULL);
10122 65 : slp_node->push_vec_def (new_temp);
10123 : }
10124 51 : return true;
10125 : }
10126 583 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10127 : vectype, NULL);
10128 634 : }
10129 : else
10130 : {
10131 197 : gcc_assert (uniform_p);
10132 197 : gimple_stmt_iterator gsi2 = *gsi;
10133 197 : gsi_next (&gsi2);
10134 197 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10135 : vectype, &gsi2);
10136 : }
10137 1636 : for (j = 0; j < (int) vec_num; ++j)
10138 856 : slp_node->push_vec_def (new_temp);
10139 : return true;
10140 : }
10141 :
10142 621854 : if (memory_access_type == VMAT_ELEMENTWISE
10143 621854 : || memory_access_type == VMAT_STRIDED_SLP)
10144 : {
10145 23526 : gimple_stmt_iterator incr_gsi;
10146 23526 : bool insert_after;
10147 23526 : tree offvar = NULL_TREE;
10148 23526 : tree ivstep;
10149 23526 : tree running_off;
10150 23526 : vec<constructor_elt, va_gc> *v = NULL;
10151 23526 : tree stride_base, stride_step = NULL_TREE, alias_off;
10152 : /* Checked by get_load_store_type. */
10153 23526 : unsigned int const_nunits = nunits.to_constant ();
10154 23526 : unsigned HOST_WIDE_INT cst_offset = 0;
10155 23526 : tree dr_offset;
10156 23526 : unsigned int inside_cost = 0;
10157 :
10158 23526 : gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
10159 23526 : gcc_assert (!nested_in_vect_loop);
10160 :
10161 23526 : if (grouped_load)
10162 : {
10163 : /* If we elided a consecutive load permutation, don't
10164 : use the original first statement (which could be elided)
10165 : but the one the load permutation starts with.
10166 : This ensures the stride_base below is correct. */
10167 10739 : if (!ls.subchain_p)
10168 10695 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10169 : else
10170 44 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10171 10739 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10172 10739 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10173 : }
10174 : else
10175 : {
10176 12787 : first_stmt_info = stmt_info;
10177 12787 : first_dr_info = dr_info;
10178 12787 : ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
10179 : }
10180 :
10181 23526 : if (grouped_load)
10182 : {
10183 10739 : if (memory_access_type == VMAT_STRIDED_SLP)
10184 : {
10185 : /* If we elided a consecutive load permutation, adjust
10186 : the group size here. */
10187 4217 : if (!ls.subchain_p)
10188 4173 : group_size = DR_GROUP_SIZE (first_stmt_info);
10189 : else
10190 44 : group_size = SLP_TREE_LANES (slp_node);
10191 : }
10192 : else /* VMAT_ELEMENTWISE */
10193 6522 : group_size = SLP_TREE_LANES (slp_node);
10194 : }
10195 : else
10196 : group_size = 1;
10197 :
10198 23526 : if (!costing_p)
10199 : {
10200 3428 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
10201 3428 : stride_base = fold_build_pointer_plus (
10202 : DR_BASE_ADDRESS (first_dr_info->dr),
10203 : size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
10204 : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
10205 3428 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
10206 :
10207 : /* For a load with loop-invariant (but other than power-of-2)
10208 : stride (i.e. not a grouped access) like so:
10209 :
10210 : for (i = 0; i < n; i += stride)
10211 : ... = array[i];
10212 :
10213 : we generate a new induction variable and new accesses to
10214 : form a new vector (or vectors, depending on ncopies):
10215 :
10216 : for (j = 0; ; j += VF*stride)
10217 : tmp1 = array[j];
10218 : tmp2 = array[j + stride];
10219 : ...
10220 : vectemp = {tmp1, tmp2, ...}
10221 : */
10222 :
10223 3428 : tree increment = fold_convert (TREE_TYPE (stride_step),
10224 : LOOP_VINFO_IV_INCREMENT (loop_vinfo));
10225 3428 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step),
10226 : stride_step, increment);
10227 :
10228 3428 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
10229 :
10230 3428 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
10231 3428 : if (LOOP_VINFO_IV_INCREMENT_INVARIANT_P (loop_vinfo))
10232 3428 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
10233 : else
10234 0 : ivstep = force_gimple_operand_gsi (&incr_gsi, unshare_expr (ivstep),
10235 : true, NULL_TREE, true,
10236 : GSI_SAME_STMT);
10237 3428 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
10238 : insert_after, &offvar, NULL, true);
10239 :
10240 3428 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
10241 : }
10242 :
10243 23526 : running_off = offvar;
10244 23526 : alias_off = build_int_cst (ref_type, 0);
10245 23526 : int nloads = const_nunits;
10246 23526 : int lnel = 1;
10247 23526 : tree ltype = TREE_TYPE (vectype);
10248 23526 : tree lvectype = vectype;
10249 23526 : auto_vec<tree> dr_chain;
10250 : /* ??? Modify local copies of alignment_support_scheme and
10251 : misalignment, but this part of analysis should be done
10252 : earlier and remembered, likewise the chosen load mode. */
10253 23526 : const dr_alignment_support tem = alignment_support_scheme;
10254 23526 : dr_alignment_support alignment_support_scheme = tem;
10255 23526 : const int tem2 = misalignment;
10256 23526 : int misalignment = tem2;
10257 23526 : if (memory_access_type == VMAT_STRIDED_SLP)
10258 : {
10259 17004 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
10260 : /* Use the target vector type if the group size is a multiple
10261 : of it. */
10262 17004 : if (n == const_nunits)
10263 : {
10264 2247 : int mis_align = dr_misalignment (first_dr_info, vectype);
10265 : /* With VF > 1 we advance the DR by step, if that is constant
10266 : and only aligned when performed VF times, DR alignment
10267 : analysis can analyze this as aligned since it assumes
10268 : contiguous accesses. But that is not how we code generate
10269 : here, so adjust for this. */
10270 2247 : if (maybe_gt (vf, 1u)
10271 3613 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10272 3401 : DR_TARGET_ALIGNMENT (first_dr_info)))
10273 212 : mis_align = -1;
10274 2247 : dr_alignment_support dr_align
10275 2247 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
10276 : mis_align);
10277 2247 : if (dr_align == dr_aligned
10278 2247 : || dr_align == dr_unaligned_supported)
10279 : {
10280 17004 : nloads = 1;
10281 17004 : lnel = const_nunits;
10282 17004 : ltype = vectype;
10283 17004 : alignment_support_scheme = dr_align;
10284 17004 : misalignment = mis_align;
10285 : }
10286 : }
10287 : /* Else use the biggest vector we can load the group without
10288 : accessing excess elements. */
10289 14757 : else if (n > 1)
10290 : {
10291 1965 : tree ptype;
10292 1965 : tree vtype
10293 1965 : = vector_vector_composition_type (vectype, const_nunits / n,
10294 : &ptype);
10295 1965 : if (vtype != NULL_TREE)
10296 : {
10297 1927 : dr_alignment_support dr_align;
10298 1927 : int mis_align = 0;
10299 1927 : if (VECTOR_TYPE_P (ptype))
10300 : {
10301 1005 : mis_align = dr_misalignment (first_dr_info, ptype);
10302 1005 : if (maybe_gt (vf, 1u)
10303 1980 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10304 1011 : DR_TARGET_ALIGNMENT (first_dr_info)))
10305 969 : mis_align = -1;
10306 1005 : dr_align
10307 1005 : = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
10308 : mis_align);
10309 : }
10310 : else
10311 : dr_align = dr_unaligned_supported;
10312 1927 : if (dr_align == dr_aligned
10313 1927 : || dr_align == dr_unaligned_supported)
10314 : {
10315 1927 : nloads = const_nunits / n;
10316 1927 : lnel = n;
10317 1927 : lvectype = vtype;
10318 1927 : ltype = ptype;
10319 1927 : alignment_support_scheme = dr_align;
10320 1927 : misalignment = mis_align;
10321 : }
10322 : }
10323 : }
10324 17004 : unsigned align;
10325 17004 : if (alignment_support_scheme == dr_aligned)
10326 20 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
10327 : else
10328 16984 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
10329 : /* Alignment is at most the access size if we do multiple loads. */
10330 17004 : if (nloads > 1)
10331 14757 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
10332 17004 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
10333 : }
10334 :
10335 23526 : if (costing_p)
10336 : {
10337 : /* Record the composition type for target access during costing. */
10338 20098 : ls.ls_type = lvectype;
10339 20098 : ls.ls_eltype = ltype;
10340 : }
10341 : else
10342 3428 : gcc_assert (ls.ls_type == lvectype && ls.ls_eltype == ltype);
10343 :
10344 : /* For SLP permutation support we need to load the whole group,
10345 : not only the number of vector stmts the permutation result
10346 : fits in. */
10347 23526 : int ncopies;
10348 23526 : if (ls.slp_perm)
10349 : {
10350 2869 : gcc_assert (memory_access_type != VMAT_ELEMENTWISE);
10351 : /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
10352 : variable VF. */
10353 2869 : unsigned int const_vf = vf.to_constant ();
10354 2869 : ncopies = CEIL (group_size * const_vf, const_nunits);
10355 2869 : dr_chain.create (ncopies);
10356 : }
10357 : else
10358 : ncopies = vec_num;
10359 :
10360 23526 : unsigned int group_el = 0;
10361 23526 : unsigned HOST_WIDE_INT
10362 23526 : elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
10363 23526 : unsigned int n_groups = 0;
10364 : /* For costing some adjacent vector loads, we'd like to cost with
10365 : the total number of them once instead of cost each one by one. */
10366 23526 : unsigned int n_adjacent_loads = 0;
10367 56290 : for (j = 0; j < ncopies; j++)
10368 : {
10369 32764 : if (nloads > 1 && !costing_p)
10370 3143 : vec_alloc (v, nloads);
10371 : gimple *new_stmt = NULL;
10372 138395 : for (i = 0; i < nloads; i++)
10373 : {
10374 105631 : if (costing_p)
10375 : {
10376 : /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
10377 : avoid ICE, see PR110776. */
10378 95574 : if (VECTOR_TYPE_P (ltype)
10379 5822 : && memory_access_type != VMAT_ELEMENTWISE)
10380 5822 : n_adjacent_loads++;
10381 : else
10382 89752 : inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
10383 : slp_node, 0, vect_body);
10384 95574 : continue;
10385 : }
10386 10057 : unsigned int load_el = group_el;
10387 : /* For elementwise accesses apply a load permutation directly. */
10388 10057 : if (memory_access_type == VMAT_ELEMENTWISE
10389 10057 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10390 2018 : load_el = SLP_TREE_LOAD_PERMUTATION (slp_node)[group_el];
10391 10057 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
10392 10057 : load_el * elsz + cst_offset);
10393 10057 : tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
10394 10057 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10395 10057 : new_temp = make_ssa_name (ltype);
10396 10057 : new_stmt = gimple_build_assign (new_temp, data_ref);
10397 10057 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10398 10057 : if (nloads > 1)
10399 8450 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
10400 :
10401 10057 : group_el += lnel;
10402 10057 : if (group_el == group_size)
10403 : {
10404 9710 : n_groups++;
10405 : /* When doing SLP make sure to not load elements from
10406 : the next vector iteration, those will not be accessed
10407 : so just use the last element again. See PR107451. */
10408 9710 : if (known_lt (n_groups, vf))
10409 : {
10410 6262 : tree newoff = copy_ssa_name (running_off);
10411 6262 : gimple *incr
10412 6262 : = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
10413 : running_off, stride_step);
10414 6262 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
10415 6262 : running_off = newoff;
10416 : }
10417 : group_el = 0;
10418 : }
10419 : }
10420 :
10421 32764 : if (nloads > 1)
10422 : {
10423 24027 : if (costing_p)
10424 20884 : inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
10425 : slp_node, 0, vect_body);
10426 : else
10427 : {
10428 3143 : tree vec_inv = build_constructor (lvectype, v);
10429 3143 : new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
10430 : lvectype, gsi);
10431 3143 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
10432 3143 : if (lvectype != vectype)
10433 : {
10434 398 : new_stmt
10435 398 : = gimple_build_assign (make_ssa_name (vectype),
10436 : VIEW_CONVERT_EXPR,
10437 : build1 (VIEW_CONVERT_EXPR,
10438 : vectype, new_temp));
10439 398 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10440 : gsi);
10441 : }
10442 : }
10443 : }
10444 8737 : else if (!costing_p && ltype != vectype)
10445 : {
10446 1588 : new_stmt = gimple_build_assign (make_ssa_name (vectype),
10447 : VIEW_CONVERT_EXPR,
10448 : build1 (VIEW_CONVERT_EXPR,
10449 : vectype, new_temp));
10450 1588 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10451 : gsi);
10452 : }
10453 :
10454 32764 : if (!costing_p)
10455 : {
10456 4750 : if (ls.slp_perm)
10457 1682 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
10458 : else
10459 3068 : slp_node->push_vec_def (new_stmt);
10460 : }
10461 : }
10462 23526 : if (ls.slp_perm)
10463 : {
10464 2869 : if (costing_p)
10465 : {
10466 2076 : gcc_assert (ls.n_perms != -1U);
10467 2076 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
10468 : slp_node, 0, vect_body);
10469 : }
10470 : else
10471 : {
10472 793 : unsigned n_perms2;
10473 793 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
10474 : false, &n_perms2);
10475 793 : gcc_assert (ls.n_perms == n_perms2);
10476 : }
10477 : }
10478 :
10479 23526 : if (costing_p)
10480 : {
10481 20098 : if (n_adjacent_loads > 0)
10482 2152 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10483 : alignment_support_scheme, misalignment, false,
10484 : &inside_cost, nullptr, cost_vec, cost_vec,
10485 : true);
10486 20098 : if (dump_enabled_p ())
10487 498 : dump_printf_loc (MSG_NOTE, vect_location,
10488 : "vect_model_load_cost: inside_cost = %u, "
10489 : "prologue_cost = 0 .\n",
10490 : inside_cost);
10491 20098 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
10492 20098 : slp_node->data = new vect_load_store_data (std::move (ls));
10493 : }
10494 :
10495 23526 : return true;
10496 23526 : }
10497 :
10498 598328 : if (mat_gather_scatter_p (memory_access_type)
10499 598328 : && !ls.ls_type)
10500 : grouped_load = false;
10501 :
10502 595337 : if (grouped_load
10503 598328 : || SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10504 : {
10505 276684 : if (grouped_load)
10506 : {
10507 276236 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10508 276236 : group_size = DR_GROUP_SIZE (first_stmt_info);
10509 : }
10510 : else
10511 : {
10512 : first_stmt_info = stmt_info;
10513 : group_size = 1;
10514 : }
10515 : /* For SLP vectorization we directly vectorize a subchain
10516 : without permutation. */
10517 276684 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10518 216289 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10519 : /* For BB vectorization always use the first stmt to base
10520 : the data ref pointer on. */
10521 276684 : if (bb_vinfo)
10522 221955 : first_stmt_info_for_drptr
10523 221955 : = vect_find_first_scalar_stmt_in_slp (slp_node);
10524 :
10525 276684 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10526 276684 : group_gap_adj = 0;
10527 :
10528 : /* VEC_NUM is the number of vect stmts to be created for this group. */
10529 276684 : grouped_load = false;
10530 : /* If an SLP permutation is from N elements to N elements,
10531 : and if one vector holds a whole number of N, we can load
10532 : the inputs to the permutation in the same way as an
10533 : unpermuted sequence. In other cases we need to load the
10534 : whole group, not only the number of vector stmts the
10535 : permutation result fits in. */
10536 276684 : unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
10537 276684 : if (nested_in_vect_loop)
10538 : /* We do not support grouped accesses in a nested loop,
10539 : instead the access is contiguous but it might be
10540 : permuted. No gap adjustment is needed though. */
10541 : ;
10542 276682 : else if (ls.slp_perm
10543 276682 : && (group_size != scalar_lanes
10544 11479 : || !multiple_p (nunits, group_size)))
10545 : {
10546 : /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
10547 : variable VF; see vect_transform_slp_perm_load. */
10548 50104 : unsigned int const_vf = vf.to_constant ();
10549 50104 : unsigned int const_nunits = nunits.to_constant ();
10550 50104 : vec_num = CEIL (group_size * const_vf, const_nunits);
10551 50104 : group_gap_adj = vf * group_size - nunits * vec_num;
10552 : }
10553 : else
10554 : {
10555 226578 : group_gap_adj = group_size - scalar_lanes;
10556 : }
10557 :
10558 276684 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10559 : }
10560 : else
10561 : {
10562 321644 : first_stmt_info = stmt_info;
10563 321644 : first_dr_info = dr_info;
10564 321644 : group_size = 1;
10565 321644 : group_gap_adj = 0;
10566 321644 : ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
10567 : }
10568 :
10569 598328 : vec_loop_masks *loop_masks
10570 376373 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
10571 598328 : ? &LOOP_VINFO_MASKS (loop_vinfo)
10572 34 : : NULL);
10573 34 : vec_loop_lens *loop_lens
10574 376373 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
10575 : ? &LOOP_VINFO_LENS (loop_vinfo)
10576 0 : : NULL);
10577 :
10578 : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
10579 : are some difference here. We cannot enable both the lens and masks
10580 : during transform but it is allowed during analysis.
10581 : Shouldn't go with length-based approach if fully masked. */
10582 598328 : if (cost_vec == NULL)
10583 : /* The cost_vec is NULL during transform. */
10584 165191 : gcc_assert ((!loop_lens || !loop_masks));
10585 :
10586 : /* Targets with store-lane instructions must not require explicit
10587 : realignment. vect_supportable_dr_alignment always returns either
10588 : dr_aligned or dr_unaligned_supported for (non-length) masked
10589 : operations. */
10590 598328 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
10591 : && !mask_node
10592 : && !loop_masks)
10593 : || mat_gather_scatter_p (memory_access_type)
10594 : || alignment_support_scheme == dr_aligned
10595 : || alignment_support_scheme == dr_unaligned_supported);
10596 :
10597 : /* In case the vectorization factor (VF) is bigger than the number
10598 : of elements that we can fit in a vectype (nunits), we have to generate
10599 : more than one vector stmt - i.e - we need to "unroll" the
10600 : vector stmt by a factor VF/nunits. In doing so, we record a pointer
10601 : from one copy of the vector stmt to the next, in the field
10602 : STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
10603 : stages to find the correct vector defs to be used when vectorizing
10604 : stmts that use the defs of the current stmt. The example below
10605 : illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
10606 : need to create 4 vectorized stmts):
10607 :
10608 : before vectorization:
10609 : RELATED_STMT VEC_STMT
10610 : S1: x = memref - -
10611 : S2: z = x + 1 - -
10612 :
10613 : step 1: vectorize stmt S1:
10614 : We first create the vector stmt VS1_0, and, as usual, record a
10615 : pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
10616 : Next, we create the vector stmt VS1_1, and record a pointer to
10617 : it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
10618 : Similarly, for VS1_2 and VS1_3. This is the resulting chain of
10619 : stmts and pointers:
10620 : RELATED_STMT VEC_STMT
10621 : VS1_0: vx0 = memref0 VS1_1 -
10622 : VS1_1: vx1 = memref1 VS1_2 -
10623 : VS1_2: vx2 = memref2 VS1_3 -
10624 : VS1_3: vx3 = memref3 - -
10625 : S1: x = load - VS1_0
10626 : S2: z = x + 1 - -
10627 : */
10628 :
10629 : /* If the data reference is aligned (dr_aligned) or potentially unaligned
10630 : on a target that supports unaligned accesses (dr_unaligned_supported)
10631 : we generate the following code:
10632 : p = initial_addr;
10633 : indx = 0;
10634 : loop {
10635 : p = p + indx * vectype_size;
10636 : vec_dest = *(p);
10637 : indx = indx + 1;
10638 : }
10639 :
10640 : Otherwise, the data reference is potentially unaligned on a target that
10641 : does not support unaligned accesses (dr_explicit_realign_optimized) -
10642 : then generate the following code, in which the data in each iteration is
10643 : obtained by two vector loads, one from the previous iteration, and one
10644 : from the current iteration:
10645 : p1 = initial_addr;
10646 : msq_init = *(floor(p1))
10647 : p2 = initial_addr + VS - 1;
10648 : realignment_token = call target_builtin;
10649 : indx = 0;
10650 : loop {
10651 : p2 = p2 + indx * vectype_size
10652 : lsq = *(floor(p2))
10653 : vec_dest = realign_load (msq, lsq, realignment_token)
10654 : indx = indx + 1;
10655 : msq = lsq;
10656 : } */
10657 :
10658 : /* If the misalignment remains the same throughout the execution of the
10659 : loop, we can create the init_addr and permutation mask at the loop
10660 : preheader. Otherwise, it needs to be created inside the loop.
10661 : This can only occur when vectorizing memory accesses in the inner-loop
10662 : nested within an outer-loop that is being vectorized. */
10663 :
10664 598328 : if (nested_in_vect_loop
10665 598328 : && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
10666 1234 : GET_MODE_SIZE (TYPE_MODE (vectype))))
10667 : {
10668 195 : gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
10669 : compute_in_loop = true;
10670 : }
10671 :
10672 598328 : bool diff_first_stmt_info
10673 598328 : = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
10674 :
10675 598328 : tree offset = NULL_TREE;
10676 598328 : if ((alignment_support_scheme == dr_explicit_realign_optimized
10677 598328 : || alignment_support_scheme == dr_explicit_realign)
10678 0 : && !compute_in_loop)
10679 : {
10680 : /* If we have different first_stmt_info, we can't set up realignment
10681 : here, since we can't guarantee first_stmt_info DR has been
10682 : initialized yet, use first_stmt_info_for_drptr DR by bumping the
10683 : distance from first_stmt_info DR instead as below. */
10684 0 : if (!costing_p)
10685 : {
10686 0 : if (!diff_first_stmt_info)
10687 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype, gsi,
10688 : &realignment_token,
10689 : alignment_support_scheme, NULL_TREE,
10690 : &at_loop);
10691 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
10692 : {
10693 0 : phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
10694 0 : offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
10695 : size_one_node);
10696 0 : gcc_assert (!first_stmt_info_for_drptr);
10697 : }
10698 : }
10699 : }
10700 : else
10701 598328 : at_loop = loop;
10702 :
10703 598328 : if (!known_eq (poffset, 0))
10704 4624 : offset = (offset
10705 4624 : ? size_binop (PLUS_EXPR, offset, size_int (poffset))
10706 4624 : : size_int (poffset));
10707 :
10708 598328 : tree dr_increment;
10709 598328 : tree dr_bump;
10710 598328 : tree vec_offset = NULL_TREE;
10711 :
10712 598328 : auto_vec<tree> vec_offsets;
10713 598328 : auto_vec<tree> vec_masks;
10714 598328 : if (mask_node && !costing_p)
10715 628 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
10716 : &vec_masks);
10717 :
10718 598328 : tree vec_mask = NULL_TREE;
10719 598328 : tree vec_els = NULL_TREE;
10720 598328 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10721 : {
10722 0 : const internal_fn lanes_ifn = ls.lanes_ifn;
10723 :
10724 0 : gcc_assert (alignment_support_scheme == dr_aligned
10725 : || alignment_support_scheme == dr_unaligned_supported);
10726 :
10727 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
10728 0 : if (!costing_p)
10729 : {
10730 0 : dr_increment = vect_get_data_ptr_step (vinfo, dr_info,
10731 : memory_access_type);
10732 0 : dr_bump = vect_get_data_ptr_bump (vinfo, dr_info, aggr_type,
10733 : memory_access_type);
10734 : }
10735 :
10736 0 : unsigned int inside_cost = 0, prologue_cost = 0;
10737 : /* For costing some adjacent vector loads, we'd like to cost with
10738 : the total number of them once instead of cost each one by one. */
10739 0 : unsigned int n_adjacent_loads = 0;
10740 0 : int ncopies = vec_num / group_size;
10741 0 : for (j = 0; j < ncopies; j++)
10742 : {
10743 0 : if (costing_p)
10744 : {
10745 : /* An IFN_LOAD_LANES will load all its vector results,
10746 : regardless of which ones we actually need. Account
10747 : for the cost of unused results. */
10748 0 : if (first_stmt_info == stmt_info)
10749 : {
10750 0 : unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
10751 0 : stmt_vec_info next_stmt_info = first_stmt_info;
10752 0 : do
10753 : {
10754 0 : gaps -= 1;
10755 0 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
10756 : }
10757 0 : while (next_stmt_info);
10758 0 : if (gaps)
10759 : {
10760 0 : if (dump_enabled_p ())
10761 0 : dump_printf_loc (MSG_NOTE, vect_location,
10762 : "vect_model_load_cost: %d "
10763 : "unused vectors.\n",
10764 : gaps);
10765 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
10766 : alignment_support_scheme,
10767 : misalignment, false, &inside_cost,
10768 : &prologue_cost, cost_vec, cost_vec,
10769 : true);
10770 : }
10771 : }
10772 0 : n_adjacent_loads++;
10773 0 : continue;
10774 0 : }
10775 :
10776 : /* 1. Create the vector or array pointer update chain. */
10777 0 : if (j == 0)
10778 0 : dataref_ptr
10779 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10780 : at_loop, offset, &dummy, gsi,
10781 : NULL, false, dr_increment);
10782 : else
10783 : {
10784 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
10785 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi,
10786 : stmt_info, dr_bump);
10787 : }
10788 0 : if (mask_node)
10789 0 : vec_mask = vec_masks[j];
10790 :
10791 0 : tree vec_array = create_vector_array (vectype, group_size);
10792 :
10793 0 : tree final_mask = NULL_TREE;
10794 0 : tree final_len = NULL_TREE;
10795 0 : tree bias = NULL_TREE;
10796 0 : if (loop_masks)
10797 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10798 : ncopies, vectype, j);
10799 0 : if (vec_mask)
10800 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
10801 : vec_mask, gsi);
10802 :
10803 0 : if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
10804 : {
10805 0 : if (loop_lens)
10806 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10807 : ncopies, vectype, j, 1, true);
10808 : else
10809 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
10810 0 : signed char biasval
10811 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10812 0 : bias = build_int_cst (intQI_type_node, biasval);
10813 0 : if (!final_mask)
10814 : {
10815 0 : mask_vectype = truth_type_for (vectype);
10816 0 : final_mask = build_minus_one_cst (mask_vectype);
10817 : }
10818 : }
10819 :
10820 0 : if (final_mask)
10821 : {
10822 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10823 0 : if (type_mode_padding_p
10824 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10825 0 : need_zeroing = true;
10826 : }
10827 :
10828 0 : gcall *call;
10829 0 : if (final_len && final_mask)
10830 : {
10831 : /* Emit:
10832 : VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10833 : VEC_MASK, LEN, BIAS). */
10834 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10835 0 : tree alias_ptr = build_int_cst (ref_type, align);
10836 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
10837 : dataref_ptr, alias_ptr,
10838 : final_mask, vec_els,
10839 : final_len, bias);
10840 : }
10841 0 : else if (final_mask)
10842 : {
10843 : /* Emit:
10844 : VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10845 : VEC_MASK). */
10846 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10847 0 : tree alias_ptr = build_int_cst (ref_type, align);
10848 0 : call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
10849 : dataref_ptr, alias_ptr,
10850 : final_mask, vec_els);
10851 : }
10852 : else
10853 : {
10854 : /* Emit:
10855 : VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
10856 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
10857 0 : call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
10858 : }
10859 0 : gimple_call_set_lhs (call, vec_array);
10860 0 : gimple_call_set_nothrow (call, true);
10861 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10862 :
10863 : /* Extract each vector into an SSA_NAME. */
10864 0 : for (unsigned i = 0; i < group_size; i++)
10865 : {
10866 0 : new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
10867 : vec_array, i, need_zeroing,
10868 : final_mask);
10869 0 : slp_node->push_vec_def (new_temp);
10870 : }
10871 :
10872 : /* Record that VEC_ARRAY is now dead. */
10873 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
10874 : }
10875 :
10876 0 : if (costing_p)
10877 : {
10878 0 : if (n_adjacent_loads > 0)
10879 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10880 : alignment_support_scheme, misalignment, false,
10881 : &inside_cost, &prologue_cost, cost_vec,
10882 : cost_vec, true);
10883 0 : if (dump_enabled_p ())
10884 0 : dump_printf_loc (MSG_NOTE, vect_location,
10885 : "vect_model_load_cost: inside_cost = %u, "
10886 : "prologue_cost = %u .\n",
10887 : inside_cost, prologue_cost);
10888 0 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
10889 0 : slp_node->data = new vect_load_store_data (std::move (ls));
10890 : }
10891 :
10892 0 : return true;
10893 : }
10894 :
10895 598328 : if (mat_gather_scatter_p (memory_access_type))
10896 : {
10897 2991 : gcc_assert ((!grouped_load && !ls.slp_perm) || ls.ls_type);
10898 :
10899 2991 : auto_vec<tree> dr_chain (vec_num);
10900 :
10901 : /* If we pun the original vectype the loads as well as costing, length,
10902 : etc. is performed with the new type. After loading we VIEW_CONVERT
10903 : the data to the original vectype. */
10904 2991 : tree original_vectype = vectype;
10905 2991 : if (ls.ls_type)
10906 0 : vectype = ls.ls_type;
10907 :
10908 : /* 1. Create the vector or array pointer update chain. */
10909 2991 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10910 : {
10911 2991 : aggr_type = NULL_TREE;
10912 2991 : dr_increment = NULL_TREE;
10913 2991 : if (!costing_p)
10914 764 : vect_get_gather_scatter_ops (loop, slp_node, &dataref_ptr,
10915 : &vec_offsets);
10916 : }
10917 : else
10918 : {
10919 0 : aggr_type = elem_type;
10920 0 : if (!costing_p)
10921 : {
10922 0 : vect_get_strided_load_store_ops (stmt_info, slp_node, vectype,
10923 : ls.strided_offset_vectype,
10924 : loop_vinfo, gsi,
10925 : &dr_increment, &dr_bump,
10926 : &vec_offset);
10927 0 : dataref_ptr
10928 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10929 : at_loop, offset, &dummy, gsi,
10930 : NULL, false, dr_increment);
10931 : }
10932 : }
10933 :
10934 : unsigned int inside_cost = 0, prologue_cost = 0;
10935 :
10936 6755 : gimple *new_stmt = NULL;
10937 6755 : for (i = 0; i < vec_num; i++)
10938 : {
10939 3764 : tree final_mask = NULL_TREE;
10940 3764 : tree final_len = NULL_TREE;
10941 3764 : tree bias = NULL_TREE;
10942 3764 : if (!costing_p)
10943 : {
10944 981 : if (mask_node)
10945 156 : vec_mask = vec_masks[i];
10946 981 : if (loop_masks)
10947 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10948 : vec_num, vectype, i);
10949 981 : if (vec_mask)
10950 156 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
10951 : final_mask, vec_mask, gsi);
10952 :
10953 981 : if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10954 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi,
10955 : stmt_info, dr_bump);
10956 : }
10957 :
10958 : /* 2. Create the vector-load in the loop. */
10959 3764 : unsigned align = get_object_alignment (DR_REF (first_dr_info->dr));
10960 3764 : tree alias_align_ptr = build_int_cst (ref_type, align);
10961 3764 : if (memory_access_type == VMAT_GATHER_SCATTER_IFN)
10962 : {
10963 0 : if (costing_p)
10964 : {
10965 0 : if (ls.supported_offset_vectype
10966 0 : && !tree_nop_conversion_p (ls.supported_offset_vectype,
10967 : vec_offset))
10968 0 : inside_cost
10969 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
10970 : slp_node, 0, vect_body);
10971 0 : if (ls.supported_scale)
10972 0 : inside_cost
10973 0 : += record_stmt_cost (cost_vec, 1, vector_stmt,
10974 : slp_node, 0, vect_body);
10975 :
10976 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
10977 0 : inside_cost
10978 0 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
10979 : slp_node, 0, vect_body);
10980 3764 : continue;
10981 0 : }
10982 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10983 0 : vec_offset = vec_offsets[i];
10984 0 : tree zero = build_zero_cst (vectype);
10985 0 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
10986 0 : bool strided = !VECTOR_TYPE_P (TREE_TYPE (vec_offset));
10987 :
10988 : /* Perform the offset conversion and scaling if necessary. */
10989 0 : if (!strided
10990 0 : && (ls.supported_offset_vectype || ls.supported_scale))
10991 : {
10992 0 : gimple_seq stmts = NULL;
10993 0 : if (ls.supported_offset_vectype)
10994 0 : vec_offset = gimple_convert
10995 0 : (&stmts, ls.supported_offset_vectype, vec_offset);
10996 0 : if (ls.supported_scale)
10997 : {
10998 : /* Only scale the vec_offset if we haven't already. */
10999 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info)
11000 0 : || i == 0)
11001 : {
11002 0 : tree mult_cst = build_int_cst
11003 0 : (TREE_TYPE (TREE_TYPE (vec_offset)),
11004 0 : SLP_TREE_GS_SCALE (slp_node) / ls.supported_scale);
11005 0 : tree mult = build_vector_from_val
11006 0 : (TREE_TYPE (vec_offset), mult_cst);
11007 0 : vec_offset = gimple_build
11008 0 : (&stmts, MULT_EXPR, TREE_TYPE (vec_offset),
11009 : vec_offset, mult);
11010 : }
11011 0 : scale = size_int (ls.supported_scale);
11012 : }
11013 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11014 : }
11015 :
11016 0 : if (ls.gs.ifn == IFN_MASK_LEN_GATHER_LOAD)
11017 : {
11018 0 : if (loop_lens)
11019 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11020 : vec_num, vectype, i, 1, true);
11021 : else
11022 0 : final_len = build_int_cst (sizetype,
11023 0 : TYPE_VECTOR_SUBPARTS (vectype));
11024 0 : signed char biasval
11025 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11026 0 : bias = build_int_cst (intQI_type_node, biasval);
11027 0 : if (!final_mask)
11028 : {
11029 0 : mask_vectype = truth_type_for (vectype);
11030 0 : final_mask = build_minus_one_cst (mask_vectype);
11031 : }
11032 : }
11033 :
11034 0 : if (final_mask)
11035 : {
11036 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11037 0 : if (type_mode_padding_p
11038 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11039 0 : need_zeroing = true;
11040 : }
11041 :
11042 0 : gcall *call;
11043 0 : if (final_len && final_mask)
11044 : {
11045 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
11046 0 : call = gimple_build_call_internal (IFN_MASK_LEN_GATHER_LOAD,
11047 : 9, dataref_ptr,
11048 : alias_align_ptr,
11049 : vec_offset, scale, zero,
11050 : final_mask, vec_els,
11051 : final_len, bias);
11052 : else
11053 : /* Non-vector offset indicates that prefer to take
11054 : MASK_LEN_STRIDED_LOAD instead of the
11055 : MASK_LEN_GATHER_LOAD with direct stride arg. */
11056 0 : call = gimple_build_call_internal
11057 0 : (IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr,
11058 : vec_offset, zero, final_mask, vec_els, final_len,
11059 : bias);
11060 : }
11061 0 : else if (final_mask)
11062 0 : call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
11063 : 7, dataref_ptr,
11064 : alias_align_ptr,
11065 : vec_offset, scale,
11066 : zero, final_mask, vec_els);
11067 : else
11068 0 : call = gimple_build_call_internal (IFN_GATHER_LOAD, 5,
11069 : dataref_ptr,
11070 : alias_align_ptr,
11071 : vec_offset, scale, zero);
11072 0 : gimple_call_set_nothrow (call, true);
11073 0 : new_stmt = call;
11074 0 : data_ref = NULL_TREE;
11075 : }
11076 3764 : else if (memory_access_type == VMAT_GATHER_SCATTER_LEGACY)
11077 : {
11078 : /* The builtin decls path for gather is legacy, x86 only. */
11079 858 : gcc_assert (!final_len && nunits.is_constant ());
11080 858 : if (costing_p)
11081 : {
11082 572 : unsigned int cnunits = vect_nunits_for_cost (vectype);
11083 572 : inside_cost
11084 572 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
11085 : slp_node, 0, vect_body);
11086 572 : continue;
11087 572 : }
11088 286 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
11089 286 : poly_uint64 offset_nunits = TYPE_VECTOR_SUBPARTS (offset_vectype);
11090 286 : if (known_eq (nunits, offset_nunits))
11091 : {
11092 137 : new_stmt = vect_build_one_gather_load_call
11093 137 : (vinfo, stmt_info, slp_node, vectype, gsi,
11094 137 : ls.gs.decl, dataref_ptr, vec_offsets[i],
11095 : final_mask);
11096 137 : data_ref = NULL_TREE;
11097 : }
11098 149 : else if (known_eq (nunits, offset_nunits * 2))
11099 : {
11100 : /* We have a offset vector with half the number of
11101 : lanes but the builtins will produce full vectype
11102 : data with just the lower lanes filled. */
11103 63 : new_stmt = vect_build_one_gather_load_call
11104 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
11105 63 : ls.gs.decl, dataref_ptr, vec_offsets[2 * i],
11106 : final_mask);
11107 63 : tree low = make_ssa_name (vectype);
11108 63 : gimple_set_lhs (new_stmt, low);
11109 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11110 :
11111 : /* now put upper half of final_mask in final_mask low. */
11112 63 : if (final_mask
11113 63 : && !SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (final_mask))))
11114 : {
11115 11 : int count = nunits.to_constant ();
11116 11 : vec_perm_builder sel (count, count, 1);
11117 11 : sel.quick_grow (count);
11118 87 : for (int i = 0; i < count; ++i)
11119 76 : sel[i] = i | (count / 2);
11120 11 : vec_perm_indices indices (sel, 2, count);
11121 11 : tree perm_mask = vect_gen_perm_mask_checked
11122 11 : (TREE_TYPE (final_mask), indices);
11123 11 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11124 : final_mask, final_mask,
11125 : perm_mask);
11126 11 : final_mask = make_ssa_name (TREE_TYPE (final_mask));
11127 11 : gimple_set_lhs (new_stmt, final_mask);
11128 11 : vect_finish_stmt_generation (vinfo, stmt_info,
11129 : new_stmt, gsi);
11130 11 : }
11131 52 : else if (final_mask)
11132 : {
11133 24 : new_stmt = gimple_build_assign (NULL_TREE,
11134 : VEC_UNPACK_HI_EXPR,
11135 : final_mask);
11136 24 : final_mask = make_ssa_name
11137 24 : (truth_type_for (offset_vectype));
11138 24 : gimple_set_lhs (new_stmt, final_mask);
11139 24 : vect_finish_stmt_generation (vinfo, stmt_info,
11140 : new_stmt, gsi);
11141 : }
11142 :
11143 63 : new_stmt = vect_build_one_gather_load_call
11144 126 : (vinfo, stmt_info, slp_node, vectype, gsi,
11145 : ls.gs.decl, dataref_ptr,
11146 63 : vec_offsets[2 * i + 1], final_mask);
11147 63 : tree high = make_ssa_name (vectype);
11148 63 : gimple_set_lhs (new_stmt, high);
11149 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11150 :
11151 : /* compose low + high. */
11152 63 : int count = nunits.to_constant ();
11153 63 : vec_perm_builder sel (count, count, 1);
11154 63 : sel.quick_grow (count);
11155 647 : for (int i = 0; i < count; ++i)
11156 584 : sel[i] = i < count / 2 ? i : i + count / 2;
11157 63 : vec_perm_indices indices (sel, 2, count);
11158 63 : tree perm_mask
11159 63 : = vect_gen_perm_mask_checked (vectype, indices);
11160 63 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11161 : low, high, perm_mask);
11162 63 : data_ref = NULL_TREE;
11163 63 : }
11164 86 : else if (known_eq (nunits * 2, offset_nunits))
11165 : {
11166 : /* We have a offset vector with double the number of
11167 : lanes. Select the low/high part accordingly. */
11168 86 : vec_offset = vec_offsets[i / 2];
11169 86 : if (i & 1)
11170 : {
11171 43 : int count = offset_nunits.to_constant ();
11172 43 : vec_perm_builder sel (count, count, 1);
11173 43 : sel.quick_grow (count);
11174 463 : for (int i = 0; i < count; ++i)
11175 420 : sel[i] = i | (count / 2);
11176 43 : vec_perm_indices indices (sel, 2, count);
11177 43 : tree perm_mask = vect_gen_perm_mask_checked
11178 43 : (TREE_TYPE (vec_offset), indices);
11179 43 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11180 : vec_offset, vec_offset,
11181 : perm_mask);
11182 43 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
11183 43 : gimple_set_lhs (new_stmt, vec_offset);
11184 43 : vect_finish_stmt_generation (vinfo, stmt_info,
11185 : new_stmt, gsi);
11186 43 : }
11187 86 : new_stmt = vect_build_one_gather_load_call
11188 86 : (vinfo, stmt_info, slp_node, vectype, gsi,
11189 : ls.gs.decl,
11190 : dataref_ptr, vec_offset, final_mask);
11191 86 : data_ref = NULL_TREE;
11192 : }
11193 : else
11194 0 : gcc_unreachable ();
11195 : }
11196 : else
11197 : {
11198 : /* Emulated gather-scatter. */
11199 2906 : gcc_assert (!final_mask);
11200 2906 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
11201 2906 : if (costing_p)
11202 : {
11203 : /* For emulated gathers N offset vector element
11204 : offset add is consumed by the load). */
11205 2211 : inside_cost = record_stmt_cost (cost_vec, 1, vec_deconstruct,
11206 : slp_node, 0, vect_body);
11207 : /* N scalar loads plus gathering them into a
11208 : vector. */
11209 2211 : inside_cost
11210 2211 : = record_stmt_cost (cost_vec, const_nunits, scalar_load,
11211 : slp_node, 0, vect_body);
11212 2211 : inside_cost
11213 2211 : = record_stmt_cost (cost_vec, 1, vec_construct,
11214 : slp_node, 0, vect_body);
11215 2211 : continue;
11216 : }
11217 695 : tree offset_vectype = TREE_TYPE (vec_offsets[0]);
11218 695 : unsigned HOST_WIDE_INT const_offset_nunits
11219 695 : = TYPE_VECTOR_SUBPARTS (offset_vectype).to_constant ();
11220 695 : vec<constructor_elt, va_gc> *ctor_elts;
11221 695 : vec_alloc (ctor_elts, const_nunits);
11222 695 : gimple_seq stmts = NULL;
11223 : /* We support offset vectors with more elements
11224 : than the data vector for now. */
11225 695 : unsigned HOST_WIDE_INT factor
11226 : = const_offset_nunits / const_nunits;
11227 695 : vec_offset = vec_offsets[i / factor];
11228 695 : unsigned elt_offset = (i % factor) * const_nunits;
11229 695 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
11230 695 : tree scale = size_int (SLP_TREE_GS_SCALE (slp_node));
11231 695 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
11232 2817 : for (unsigned k = 0; k < const_nunits; ++k)
11233 : {
11234 2122 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
11235 : bitsize_int (k + elt_offset));
11236 6366 : tree idx = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
11237 2122 : vec_offset, TYPE_SIZE (idx_type),
11238 : boff);
11239 2122 : idx = gimple_convert (&stmts, sizetype, idx);
11240 2122 : idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx, scale);
11241 2122 : tree ptr = gimple_build (&stmts, PLUS_EXPR,
11242 2122 : TREE_TYPE (dataref_ptr),
11243 : dataref_ptr, idx);
11244 2122 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
11245 2122 : tree elt = make_ssa_name (TREE_TYPE (vectype));
11246 2122 : tree ref = build2 (MEM_REF, ltype, ptr,
11247 : build_int_cst (ref_type, 0));
11248 2122 : new_stmt = gimple_build_assign (elt, ref);
11249 4244 : gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
11250 2122 : gimple_seq_add_stmt (&stmts, new_stmt);
11251 2122 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
11252 : }
11253 695 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11254 695 : new_stmt = gimple_build_assign (NULL_TREE,
11255 : build_constructor (vectype,
11256 : ctor_elts));
11257 695 : data_ref = NULL_TREE;
11258 : }
11259 :
11260 981 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11261 : /* DATA_REF is null if we've already built the statement. */
11262 981 : if (data_ref)
11263 : {
11264 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11265 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11266 : }
11267 1962 : new_temp = (need_zeroing
11268 981 : ? make_ssa_name (vectype)
11269 981 : : make_ssa_name (vec_dest, new_stmt));
11270 981 : gimple_set_lhs (new_stmt, new_temp);
11271 981 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11272 :
11273 : /* If we need to explicitly zero inactive elements emit a
11274 : VEC_COND_EXPR that does so. */
11275 981 : if (need_zeroing)
11276 : {
11277 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11278 : vectype);
11279 :
11280 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11281 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11282 : final_mask, new_temp, vec_els);
11283 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11284 0 : new_temp = new_temp2;
11285 : }
11286 :
11287 981 : if (ls.ls_type)
11288 : {
11289 0 : new_stmt = gimple_build_assign (make_ssa_name
11290 : (original_vectype),
11291 : VIEW_CONVERT_EXPR,
11292 : build1 (VIEW_CONVERT_EXPR,
11293 : original_vectype,
11294 : new_temp));
11295 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11296 : }
11297 :
11298 : /* Store vector loads in the corresponding SLP_NODE. */
11299 981 : if (!costing_p)
11300 : {
11301 981 : if (ls.slp_perm)
11302 0 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
11303 : else
11304 981 : slp_node->push_vec_def (new_stmt);
11305 : }
11306 : }
11307 :
11308 2991 : if (ls.slp_perm)
11309 : {
11310 0 : if (costing_p)
11311 : {
11312 0 : gcc_assert (ls.n_perms != -1U);
11313 0 : inside_cost += record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
11314 : slp_node, 0, vect_body);
11315 : }
11316 : else
11317 : {
11318 0 : unsigned n_perms2;
11319 0 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
11320 : false, &n_perms2);
11321 0 : gcc_assert (ls.n_perms == n_perms2);
11322 : }
11323 : }
11324 :
11325 2991 : if (costing_p)
11326 : {
11327 2227 : if (dump_enabled_p ())
11328 315 : dump_printf_loc (MSG_NOTE, vect_location,
11329 : "vect_model_load_cost: inside_cost = %u, "
11330 : "prologue_cost = %u .\n",
11331 : inside_cost, prologue_cost);
11332 2227 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
11333 2227 : slp_node->data = new vect_load_store_data (std::move (ls));
11334 : }
11335 2991 : return true;
11336 2991 : }
11337 :
11338 595337 : aggr_type = vectype;
11339 595337 : if (!costing_p)
11340 : {
11341 164427 : dr_increment = vect_get_data_ptr_step (vinfo, dr_info,
11342 : memory_access_type);
11343 164427 : dr_bump = vect_get_data_ptr_bump (vinfo, dr_info, aggr_type,
11344 : memory_access_type);
11345 : }
11346 :
11347 595337 : poly_uint64 group_elt = 0;
11348 595337 : unsigned int inside_cost = 0, prologue_cost = 0;
11349 : /* For costing some adjacent vector loads, we'd like to cost with
11350 : the total number of them once instead of cost each one by one. */
11351 595337 : unsigned int n_adjacent_loads = 0;
11352 :
11353 : /* 1. Create the vector or array pointer update chain. */
11354 595337 : if (!costing_p)
11355 : {
11356 164427 : bool simd_lane_access_p
11357 164427 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
11358 164427 : if (simd_lane_access_p
11359 1629 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
11360 1629 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
11361 1629 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
11362 1629 : && integer_zerop (DR_INIT (first_dr_info->dr))
11363 1629 : && alias_sets_conflict_p (get_alias_set (aggr_type),
11364 1629 : get_alias_set (TREE_TYPE (ref_type)))
11365 164427 : && (alignment_support_scheme == dr_aligned
11366 1629 : || alignment_support_scheme == dr_unaligned_supported))
11367 : {
11368 1629 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
11369 1629 : dataref_offset = build_int_cst (ref_type, 0);
11370 : }
11371 162798 : else if (diff_first_stmt_info)
11372 : {
11373 3968 : dataref_ptr
11374 3968 : = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
11375 : aggr_type, at_loop, offset, &dummy,
11376 : gsi, NULL, simd_lane_access_p,
11377 : dr_increment);
11378 : /* Adjust the pointer by the difference to first_stmt. */
11379 3968 : data_reference_p ptrdr
11380 : = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
11381 3968 : tree diff = fold_convert (sizetype,
11382 : size_binop (MINUS_EXPR,
11383 : DR_INIT (first_dr_info->dr),
11384 : DR_INIT (ptrdr)));
11385 3968 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi,
11386 : stmt_info, diff);
11387 3968 : if (alignment_support_scheme == dr_explicit_realign)
11388 : {
11389 0 : msq = vect_setup_realignment (vinfo, first_stmt_info_for_drptr,
11390 : vectype, gsi,
11391 : &realignment_token,
11392 : alignment_support_scheme,
11393 : dataref_ptr, &at_loop);
11394 0 : gcc_assert (!compute_in_loop);
11395 : }
11396 : }
11397 : else
11398 158830 : dataref_ptr
11399 158830 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11400 : at_loop,
11401 : offset, &dummy, gsi, NULL,
11402 : simd_lane_access_p, dr_increment);
11403 : }
11404 :
11405 595337 : auto_vec<tree> dr_chain;
11406 595337 : if (grouped_load || ls.slp_perm)
11407 60395 : dr_chain.create (vec_num);
11408 :
11409 : gimple *new_stmt = NULL;
11410 1557724 : for (i = 0; i < vec_num; i++)
11411 : {
11412 962387 : tree final_mask = NULL_TREE;
11413 962387 : tree final_len = NULL_TREE;
11414 962387 : tree bias = NULL_TREE;
11415 :
11416 962387 : if (!costing_p)
11417 : {
11418 257310 : if (mask_node)
11419 651 : vec_mask = vec_masks[i];
11420 257310 : if (loop_masks)
11421 51 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11422 : vec_num, vectype, i);
11423 257310 : if (vec_mask)
11424 651 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11425 : final_mask, vec_mask, gsi);
11426 :
11427 257310 : if (i > 0)
11428 92883 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi, stmt_info,
11429 : dr_bump);
11430 : }
11431 :
11432 : /* 2. Create the vector-load in the loop. */
11433 962387 : switch (alignment_support_scheme)
11434 : {
11435 962387 : case dr_aligned:
11436 962387 : case dr_unaligned_supported:
11437 962387 : {
11438 962387 : if (costing_p)
11439 : break;
11440 :
11441 257310 : unsigned int misalign;
11442 257310 : unsigned HOST_WIDE_INT align;
11443 257310 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
11444 257310 : if (alignment_support_scheme == dr_aligned)
11445 : misalign = 0;
11446 164736 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
11447 : {
11448 125186 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
11449 125186 : misalign = 0;
11450 : }
11451 : else
11452 39550 : misalign = misalignment;
11453 257310 : if (dataref_offset == NULL_TREE
11454 255183 : && TREE_CODE (dataref_ptr) == SSA_NAME)
11455 173640 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
11456 : misalign);
11457 257310 : align = least_bit_hwi (misalign | align);
11458 :
11459 : /* Compute IFN when LOOP_LENS or final_mask valid. */
11460 257310 : machine_mode vmode = TYPE_MODE (vectype);
11461 257310 : machine_mode new_vmode = vmode;
11462 257310 : internal_fn partial_ifn = IFN_LAST;
11463 257310 : if (loop_lens)
11464 : {
11465 0 : opt_machine_mode new_ovmode
11466 0 : = get_len_load_store_mode (vmode, true, &partial_ifn);
11467 0 : new_vmode = new_ovmode.require ();
11468 0 : unsigned factor
11469 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
11470 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11471 : vec_num, vectype, i, factor, true);
11472 : }
11473 257310 : else if (final_mask)
11474 : {
11475 682 : if (!can_vec_mask_load_store_p (vmode,
11476 682 : TYPE_MODE
11477 : (TREE_TYPE (final_mask)),
11478 : true, &partial_ifn))
11479 0 : gcc_unreachable ();
11480 : }
11481 :
11482 257310 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11483 : {
11484 0 : if (!final_len)
11485 : {
11486 : /* Pass VF value to 'len' argument of
11487 : MASK_LEN_LOAD if LOOP_LENS is invalid. */
11488 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11489 : }
11490 0 : if (!final_mask)
11491 : {
11492 : /* Pass all ones value to 'mask' argument of
11493 : MASK_LEN_LOAD if final_mask is invalid. */
11494 0 : mask_vectype = truth_type_for (vectype);
11495 0 : final_mask = build_minus_one_cst (mask_vectype);
11496 : }
11497 : }
11498 257310 : if (final_len)
11499 : {
11500 0 : signed char biasval
11501 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11502 0 : bias = build_int_cst (intQI_type_node, biasval);
11503 : }
11504 :
11505 257310 : tree vec_els;
11506 :
11507 257310 : if (final_len)
11508 : {
11509 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11510 0 : gcall *call;
11511 :
11512 : /* Need conversion if the vectype is punned by VnQI. */
11513 0 : els_vectype = vectype;
11514 0 : if (vmode != new_vmode)
11515 0 : els_vectype
11516 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
11517 : new_vmode);
11518 0 : vec_els = vect_get_mask_load_else (maskload_elsval,
11519 : els_vectype);
11520 :
11521 0 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11522 : {
11523 0 : if (type_mode_padding_p
11524 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11525 0 : need_zeroing = true;
11526 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
11527 : 6, dataref_ptr, ptr,
11528 : final_mask, vec_els,
11529 : final_len, bias);
11530 : }
11531 : else
11532 0 : call = gimple_build_call_internal (IFN_LEN_LOAD, 5,
11533 : dataref_ptr, ptr,
11534 : vec_els, final_len,
11535 : bias);
11536 0 : gimple_call_set_nothrow (call, true);
11537 0 : new_stmt = call;
11538 0 : data_ref = NULL_TREE;
11539 :
11540 : /* Need conversion if it's wrapped with VnQI. */
11541 0 : if (vmode != new_vmode)
11542 : {
11543 0 : tree new_vtype
11544 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
11545 : new_vmode);
11546 0 : tree var = vect_get_new_ssa_name (new_vtype,
11547 : vect_simple_var);
11548 0 : gimple_set_lhs (call, var);
11549 0 : vect_finish_stmt_generation (vinfo, stmt_info, call,
11550 : gsi);
11551 0 : tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
11552 0 : new_stmt = gimple_build_assign (vec_dest,
11553 : VIEW_CONVERT_EXPR, op);
11554 : }
11555 : }
11556 257310 : else if (final_mask)
11557 : {
11558 682 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11559 682 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11560 682 : if (type_mode_padding_p
11561 682 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11562 0 : need_zeroing = true;
11563 682 : gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
11564 : dataref_ptr, ptr,
11565 : final_mask,
11566 : vec_els);
11567 682 : gimple_call_set_nothrow (call, true);
11568 682 : new_stmt = call;
11569 682 : data_ref = NULL_TREE;
11570 : }
11571 : else
11572 : {
11573 256628 : tree ltype = vectype;
11574 256628 : tree new_vtype = NULL_TREE;
11575 256628 : unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
11576 256628 : unsigned HOST_WIDE_INT dr_size
11577 256628 : = vect_get_scalar_dr_size (first_dr_info);
11578 256628 : poly_int64 off = 0;
11579 256628 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11580 1444 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
11581 256628 : unsigned int vect_align
11582 256628 : = vect_known_alignment_in_bytes (first_dr_info, vectype,
11583 256628 : off);
11584 : /* Try to use a single smaller load when we are about
11585 : to load excess elements compared to the unrolled
11586 : scalar loop. */
11587 256628 : if (known_gt ((i + 1) * nunits,
11588 : (group_size * vf - gap)))
11589 : {
11590 6993 : poly_uint64 remain = ((group_size * vf - gap) - i * nunits);
11591 6993 : if (known_ge ((i + 1) * nunits - (group_size * vf - gap),
11592 : nunits))
11593 : /* DR will be unused. */
11594 : ltype = NULL_TREE;
11595 2334 : else if (known_ge (vect_align,
11596 : tree_to_poly_uint64
11597 : (TYPE_SIZE_UNIT (vectype))))
11598 : /* Aligned access to excess elements is OK if
11599 : at least one element is accessed in the
11600 : scalar loop. */
11601 : ;
11602 1940 : else if (known_gt (vect_align,
11603 : ((nunits - remain) * dr_size)))
11604 : /* Aligned access to the gap area when there's
11605 : at least one element in it is OK. */
11606 : ;
11607 : else
11608 : {
11609 : /* remain should now be > 0 and < nunits. */
11610 1937 : unsigned num;
11611 1937 : if (known_ne (remain, 0u)
11612 1937 : && constant_multiple_p (nunits, remain, &num))
11613 : {
11614 1463 : tree ptype;
11615 1463 : new_vtype
11616 1463 : = vector_vector_composition_type (vectype, num,
11617 : &ptype);
11618 1463 : if (new_vtype)
11619 1463 : ltype = ptype;
11620 : }
11621 : /* Else use multiple loads or a masked load? */
11622 : /* For loop vectorization we now should have
11623 : an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
11624 : set. */
11625 1937 : if (loop_vinfo)
11626 1659 : gcc_assert (new_vtype
11627 : || LOOP_VINFO_PEELING_FOR_GAPS
11628 : (loop_vinfo));
11629 : /* But still reduce the access size to the next
11630 : required power-of-two so peeling a single
11631 : scalar iteration is sufficient. */
11632 1937 : unsigned HOST_WIDE_INT cremain;
11633 1937 : if (remain.is_constant (&cremain))
11634 : {
11635 1937 : unsigned HOST_WIDE_INT cpart_size
11636 1937 : = 1 << ceil_log2 (cremain);
11637 1937 : if (known_gt (nunits, cpart_size)
11638 1937 : && constant_multiple_p (nunits, cpart_size,
11639 : &num))
11640 : {
11641 1475 : tree ptype;
11642 1475 : new_vtype
11643 2950 : = vector_vector_composition_type (vectype,
11644 1475 : num,
11645 : &ptype);
11646 1475 : if (new_vtype)
11647 1475 : ltype = ptype;
11648 : }
11649 : }
11650 : }
11651 : }
11652 256628 : tree offset = (dataref_offset ? dataref_offset
11653 254501 : : build_int_cst (ref_type, 0));
11654 256628 : if (!ltype)
11655 : ;
11656 251969 : else if (ltype != vectype
11657 251969 : && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11658 : {
11659 25 : poly_uint64 gap_offset
11660 25 : = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
11661 25 : - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
11662 25 : tree gapcst = build_int_cstu (ref_type, gap_offset);
11663 25 : offset = size_binop (PLUS_EXPR, offset, gapcst);
11664 : }
11665 256628 : if (ltype)
11666 : {
11667 251969 : data_ref = fold_build2 (MEM_REF, ltype,
11668 : dataref_ptr, offset);
11669 251969 : if (alignment_support_scheme == dr_aligned
11670 251969 : && align >= TYPE_ALIGN_UNIT (ltype))
11671 : ;
11672 : else
11673 163088 : TREE_TYPE (data_ref)
11674 326176 : = build_aligned_type (TREE_TYPE (data_ref),
11675 : align * BITS_PER_UNIT);
11676 : }
11677 256628 : if (!ltype)
11678 4659 : data_ref = build_constructor (vectype, NULL);
11679 251969 : else if (ltype != vectype)
11680 : {
11681 1475 : vect_copy_ref_info (data_ref,
11682 1475 : DR_REF (first_dr_info->dr));
11683 1475 : tree tem = make_ssa_name (ltype);
11684 1475 : new_stmt = gimple_build_assign (tem, data_ref);
11685 1475 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11686 : gsi);
11687 1475 : data_ref = NULL;
11688 1475 : vec<constructor_elt, va_gc> *v;
11689 : /* We've computed 'num' above to statically two
11690 : or via constant_multiple_p. */
11691 1475 : unsigned num
11692 1475 : = (exact_div (tree_to_poly_uint64
11693 1475 : (TYPE_SIZE_UNIT (vectype)),
11694 : tree_to_poly_uint64
11695 1475 : (TYPE_SIZE_UNIT (ltype)))
11696 1475 : .to_constant ());
11697 1475 : vec_alloc (v, num);
11698 1475 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11699 : {
11700 62 : while (--num)
11701 62 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11702 : build_zero_cst (ltype));
11703 25 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11704 : }
11705 : else
11706 : {
11707 1450 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11708 1450 : while (--num)
11709 3260 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11710 : build_zero_cst (ltype));
11711 : }
11712 1475 : gcc_assert (new_vtype != NULL_TREE);
11713 1475 : if (new_vtype == vectype)
11714 1443 : new_stmt
11715 1443 : = gimple_build_assign (vec_dest,
11716 : build_constructor (vectype, v));
11717 : else
11718 : {
11719 32 : tree new_vname = make_ssa_name (new_vtype);
11720 32 : new_stmt
11721 32 : = gimple_build_assign (new_vname,
11722 : build_constructor (new_vtype,
11723 : v));
11724 32 : vect_finish_stmt_generation (vinfo, stmt_info,
11725 : new_stmt, gsi);
11726 32 : new_stmt
11727 32 : = gimple_build_assign (vec_dest,
11728 : build1 (VIEW_CONVERT_EXPR,
11729 : vectype, new_vname));
11730 : }
11731 : }
11732 : }
11733 : break;
11734 : }
11735 0 : case dr_explicit_realign:
11736 0 : {
11737 0 : if (costing_p)
11738 : break;
11739 0 : tree ptr, bump;
11740 :
11741 0 : tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11742 :
11743 0 : if (compute_in_loop)
11744 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, vectype,
11745 : gsi, &realignment_token,
11746 : dr_explicit_realign,
11747 : dataref_ptr, NULL);
11748 :
11749 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11750 0 : ptr = copy_ssa_name (dataref_ptr);
11751 : else
11752 0 : ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
11753 : // For explicit realign the target alignment should be
11754 : // known at compile time.
11755 0 : unsigned HOST_WIDE_INT align
11756 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11757 0 : new_stmt = gimple_build_assign (ptr, BIT_AND_EXPR, dataref_ptr,
11758 : build_int_cst
11759 0 : (TREE_TYPE (dataref_ptr),
11760 0 : -(HOST_WIDE_INT) align));
11761 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11762 0 : data_ref = build2 (MEM_REF, vectype,
11763 : ptr, build_int_cst (ref_type, 0));
11764 0 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11765 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11766 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11767 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11768 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11769 0 : gimple_move_vops (new_stmt, stmt_info->stmt);
11770 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11771 0 : msq = new_temp;
11772 :
11773 0 : bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
11774 0 : bump = size_binop (MINUS_EXPR, bump, size_one_node);
11775 0 : ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi, stmt_info, bump);
11776 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR, ptr,
11777 0 : build_int_cst (TREE_TYPE (ptr),
11778 0 : -(HOST_WIDE_INT) align));
11779 0 : if (TREE_CODE (ptr) == SSA_NAME)
11780 0 : ptr = copy_ssa_name (ptr, new_stmt);
11781 : else
11782 0 : ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
11783 0 : gimple_assign_set_lhs (new_stmt, ptr);
11784 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11785 0 : data_ref = build2 (MEM_REF, vectype,
11786 : ptr, build_int_cst (ref_type, 0));
11787 0 : break;
11788 : }
11789 0 : case dr_explicit_realign_optimized:
11790 0 : {
11791 0 : if (costing_p)
11792 : break;
11793 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11794 0 : new_temp = copy_ssa_name (dataref_ptr);
11795 : else
11796 0 : new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
11797 : // We should only be doing this if we know the target
11798 : // alignment at compile time.
11799 0 : unsigned HOST_WIDE_INT align
11800 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11801 0 : new_stmt = gimple_build_assign (new_temp, BIT_AND_EXPR, dataref_ptr,
11802 0 : build_int_cst (TREE_TYPE (dataref_ptr),
11803 0 : -(HOST_WIDE_INT) align));
11804 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11805 0 : data_ref = build2 (MEM_REF, vectype, new_temp,
11806 : build_int_cst (ref_type, 0));
11807 0 : break;
11808 : }
11809 0 : default:
11810 0 : gcc_unreachable ();
11811 : }
11812 :
11813 : /* One common place to cost the above vect load for different
11814 : alignment support schemes. */
11815 962387 : if (costing_p)
11816 : {
11817 : /* For the prologue cost for realign,
11818 : we only need to count it once for the whole group. */
11819 705077 : bool first_stmt_info_p = first_stmt_info == stmt_info;
11820 705077 : bool add_realign_cost = first_stmt_info_p && i == 0;
11821 705077 : if (memory_access_type == VMAT_CONTIGUOUS
11822 705077 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11823 : {
11824 : /* Leave realign cases alone to keep them simple. */
11825 705077 : if (alignment_support_scheme == dr_explicit_realign_optimized
11826 : || alignment_support_scheme == dr_explicit_realign)
11827 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
11828 : alignment_support_scheme, misalignment,
11829 : add_realign_cost, &inside_cost,
11830 : &prologue_cost, cost_vec, cost_vec,
11831 : true);
11832 : else
11833 705077 : n_adjacent_loads++;
11834 : }
11835 : }
11836 : else
11837 : {
11838 257310 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11839 : /* DATA_REF is null if we've already built the statement. */
11840 257310 : if (data_ref)
11841 : {
11842 255153 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11843 255153 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11844 : }
11845 :
11846 514620 : new_temp = (need_zeroing
11847 257310 : ? make_ssa_name (vectype)
11848 257310 : : make_ssa_name (vec_dest, new_stmt));
11849 257310 : gimple_set_lhs (new_stmt, new_temp);
11850 257310 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11851 :
11852 : /* If we need to explicitly zero inactive elements emit a
11853 : VEC_COND_EXPR that does so. */
11854 257310 : if (need_zeroing)
11855 : {
11856 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11857 : vectype);
11858 :
11859 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11860 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11861 : final_mask, new_temp, vec_els);
11862 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11863 : gsi);
11864 0 : new_temp = new_temp2;
11865 : }
11866 : }
11867 :
11868 : /* 3. Handle explicit realignment if necessary/supported.
11869 : Create in loop:
11870 : vec_dest = realign_load (msq, lsq, realignment_token) */
11871 962387 : if (!costing_p
11872 257310 : && (alignment_support_scheme == dr_explicit_realign_optimized
11873 : || alignment_support_scheme == dr_explicit_realign))
11874 : {
11875 0 : lsq = gimple_assign_lhs (new_stmt);
11876 0 : if (!realignment_token)
11877 0 : realignment_token = dataref_ptr;
11878 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11879 0 : new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
11880 : lsq, realignment_token);
11881 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11882 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11883 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11884 :
11885 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
11886 : {
11887 0 : gcc_assert (phi);
11888 0 : if (i == vec_num - 1)
11889 0 : add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
11890 : UNKNOWN_LOCATION);
11891 : msq = lsq;
11892 : }
11893 : }
11894 :
11895 962387 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11896 : {
11897 5930 : if (costing_p)
11898 4486 : inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
11899 : slp_node, 0, vect_body);
11900 : else
11901 : {
11902 1444 : tree perm_mask = perm_mask_for_reverse (vectype);
11903 1444 : new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
11904 : perm_mask, stmt_info, gsi);
11905 1444 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
11906 : }
11907 : }
11908 :
11909 : /* Collect vector loads and later create their permutation in
11910 : vect_transform_slp_perm_load. */
11911 962387 : if (!costing_p && (grouped_load || ls.slp_perm))
11912 74149 : dr_chain.quick_push (new_temp);
11913 :
11914 : /* Store vector loads in the corresponding SLP_NODE. */
11915 257310 : if (!costing_p && !ls.slp_perm)
11916 183161 : slp_node->push_vec_def (new_stmt);
11917 :
11918 : /* With SLP permutation we load the gaps as well, without
11919 : we need to skip the gaps after we manage to fully load
11920 : all elements. group_gap_adj is DR_GROUP_SIZE here. */
11921 962387 : group_elt += nunits;
11922 962387 : if (!costing_p
11923 257310 : && maybe_ne (group_gap_adj, 0U)
11924 46864 : && !ls.slp_perm
11925 984224 : && known_eq (group_elt, group_size - group_gap_adj))
11926 : {
11927 17136 : poly_wide_int bump_val
11928 17136 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11929 17136 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11930 0 : bump_val = -bump_val;
11931 17136 : tree bump = wide_int_to_tree (sizetype, bump_val);
11932 17136 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi, stmt_info,
11933 : bump);
11934 17136 : group_elt = 0;
11935 17136 : }
11936 : }
11937 : /* Bump the vector pointer to account for a gap or for excess
11938 : elements loaded for a permuted SLP load. */
11939 595337 : if (!costing_p
11940 164427 : && maybe_ne (group_gap_adj, 0U)
11941 612960 : && ls.slp_perm)
11942 : {
11943 487 : poly_wide_int bump_val
11944 487 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11945 487 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11946 9 : bump_val = -bump_val;
11947 487 : tree bump = wide_int_to_tree (sizetype, bump_val);
11948 487 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, gsi, stmt_info, bump);
11949 487 : }
11950 :
11951 595337 : if (ls.slp_perm)
11952 : {
11953 : /* For SLP we know we've seen all possible uses of dr_chain so
11954 : direct vect_transform_slp_perm_load to DCE the unused parts.
11955 : ??? This is a hack to prevent compile-time issues as seen
11956 : in PR101120 and friends. */
11957 60395 : if (costing_p)
11958 : {
11959 43113 : gcc_assert (ls.n_perms != -1U && ls.n_loads != -1U);
11960 43113 : if (ls.n_perms != 0)
11961 42598 : inside_cost = record_stmt_cost (cost_vec, ls.n_perms, vec_perm,
11962 : slp_node, 0, vect_body);
11963 43113 : if (n_adjacent_loads > 0)
11964 43113 : n_adjacent_loads = ls.n_loads;
11965 : }
11966 : else
11967 : {
11968 17282 : unsigned n_perms2, n_loads2;
11969 17282 : bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
11970 : gsi, vf, false, &n_perms2,
11971 : &n_loads2, true);
11972 17282 : gcc_assert (ok && ls.n_perms == n_perms2 && ls.n_loads == n_loads2);
11973 : }
11974 : }
11975 :
11976 595337 : if (costing_p)
11977 : {
11978 430910 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
11979 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
11980 430910 : if (n_adjacent_loads > 0)
11981 430910 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11982 : alignment_support_scheme, misalignment, false,
11983 : &inside_cost, &prologue_cost, cost_vec, cost_vec,
11984 : true);
11985 430910 : if (dump_enabled_p ())
11986 24414 : dump_printf_loc (MSG_NOTE, vect_location,
11987 : "vect_model_load_cost: inside_cost = %u, "
11988 : "prologue_cost = %u .\n",
11989 : inside_cost, prologue_cost);
11990 430910 : SLP_TREE_TYPE (slp_node) = load_vec_info_type;
11991 430910 : slp_node->data = new vect_load_store_data (std::move (ls));
11992 : }
11993 :
11994 595337 : return true;
11995 1932687 : }
11996 :
11997 : /* Function vect_is_simple_cond.
11998 :
11999 : Input:
12000 : LOOP - the loop that is being vectorized.
12001 : COND - Condition that is checked for simple use.
12002 :
12003 : Output:
12004 : *COMP_VECTYPE - the vector type for the comparison.
12005 : *DTS - The def types for the arguments of the comparison
12006 :
12007 : Returns whether a COND can be vectorized. Checks whether
12008 : condition operands are supportable using vec_is_simple_use. */
12009 :
12010 : static bool
12011 35558 : vect_is_simple_cond (tree cond, vec_info *vinfo,
12012 : slp_tree slp_node, tree *comp_vectype,
12013 : enum vect_def_type *dts, tree vectype)
12014 : {
12015 35558 : tree lhs, rhs;
12016 35558 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12017 35558 : slp_tree slp_op;
12018 :
12019 : /* Mask case. */
12020 35558 : if (TREE_CODE (cond) == SSA_NAME
12021 35558 : && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
12022 : {
12023 35558 : if (!vect_is_simple_use (vinfo, slp_node, 0, &cond,
12024 : &slp_op, &dts[0], comp_vectype)
12025 35558 : || !*comp_vectype
12026 71100 : || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
12027 : return false;
12028 : return true;
12029 : }
12030 :
12031 0 : if (!COMPARISON_CLASS_P (cond))
12032 : return false;
12033 :
12034 0 : lhs = TREE_OPERAND (cond, 0);
12035 0 : rhs = TREE_OPERAND (cond, 1);
12036 :
12037 0 : if (TREE_CODE (lhs) == SSA_NAME)
12038 : {
12039 0 : if (!vect_is_simple_use (vinfo, slp_node, 0,
12040 : &lhs, &slp_op, &dts[0], &vectype1))
12041 : return false;
12042 : }
12043 0 : else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
12044 0 : || TREE_CODE (lhs) == FIXED_CST)
12045 0 : dts[0] = vect_constant_def;
12046 : else
12047 : return false;
12048 :
12049 0 : if (TREE_CODE (rhs) == SSA_NAME)
12050 : {
12051 0 : if (!vect_is_simple_use (vinfo, slp_node, 1,
12052 : &rhs, &slp_op, &dts[1], &vectype2))
12053 : return false;
12054 : }
12055 0 : else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
12056 0 : || TREE_CODE (rhs) == FIXED_CST)
12057 0 : dts[1] = vect_constant_def;
12058 : else
12059 : return false;
12060 :
12061 0 : if (vectype1 && vectype2
12062 0 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12063 0 : TYPE_VECTOR_SUBPARTS (vectype2)))
12064 0 : return false;
12065 :
12066 0 : *comp_vectype = vectype1 ? vectype1 : vectype2;
12067 : /* Invariant comparison. */
12068 0 : if (! *comp_vectype)
12069 : {
12070 0 : tree scalar_type = TREE_TYPE (lhs);
12071 0 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
12072 0 : *comp_vectype = truth_type_for (vectype);
12073 : else
12074 : {
12075 : /* If we can widen the comparison to match vectype do so. */
12076 0 : if (INTEGRAL_TYPE_P (scalar_type)
12077 0 : && !slp_node
12078 0 : && tree_int_cst_lt (TYPE_SIZE (scalar_type),
12079 0 : TYPE_SIZE (TREE_TYPE (vectype))))
12080 0 : scalar_type = build_nonstandard_integer_type
12081 0 : (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
12082 0 : *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
12083 : slp_node);
12084 : }
12085 : }
12086 :
12087 : return true;
12088 : }
12089 :
12090 : /* vectorizable_condition.
12091 :
12092 : Check if STMT_INFO is conditional modify expression that can be vectorized.
12093 : If COST_VEC is passed, calculate costs but don't change anything,
12094 : otherwise, vectorize STMT_INFO: create a vectorized stmt using
12095 : VEC_COND_EXPR to replace it, and insert it at GSI.
12096 :
12097 : When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
12098 :
12099 : Return true if STMT_INFO is vectorizable in this way. */
12100 :
12101 : static bool
12102 725011 : vectorizable_condition (vec_info *vinfo,
12103 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12104 : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12105 : {
12106 725011 : tree scalar_dest = NULL_TREE;
12107 725011 : tree vec_dest = NULL_TREE;
12108 725011 : tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
12109 725011 : tree then_clause, else_clause;
12110 725011 : tree comp_vectype = NULL_TREE;
12111 725011 : tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
12112 725011 : tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
12113 725011 : tree vec_compare;
12114 725011 : tree new_temp;
12115 725011 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12116 725011 : enum vect_def_type dts[4]
12117 : = {vect_unknown_def_type, vect_unknown_def_type,
12118 : vect_unknown_def_type, vect_unknown_def_type};
12119 725011 : enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12120 725011 : int i;
12121 725011 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12122 725011 : vec<tree> vec_oprnds0 = vNULL;
12123 725011 : vec<tree> vec_oprnds1 = vNULL;
12124 725011 : vec<tree> vec_oprnds2 = vNULL;
12125 725011 : vec<tree> vec_oprnds3 = vNULL;
12126 725011 : tree vec_cmp_type;
12127 725011 : bool masked = false;
12128 :
12129 725011 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12130 : return false;
12131 :
12132 : /* Is vectorizable conditional operation? */
12133 1103305 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12134 413822 : if (!stmt)
12135 : return false;
12136 :
12137 413822 : code = gimple_assign_rhs_code (stmt);
12138 413822 : if (code != COND_EXPR)
12139 : return false;
12140 :
12141 35558 : int reduc_index = SLP_TREE_REDUC_IDX (slp_node);
12142 35558 : vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
12143 35558 : bool nested_cycle_p = false;
12144 35558 : bool for_reduction = vect_is_reduction (stmt_info);
12145 35558 : if (for_reduction)
12146 : {
12147 614 : if (SLP_TREE_LANES (slp_node) > 1)
12148 : return false;
12149 : /* ??? With a reduction path we do not get at the reduction info from
12150 : every stmt, use the conservative default setting then. */
12151 694 : if (STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)))
12152 : {
12153 596 : vect_reduc_info reduc_info
12154 596 : = info_for_reduction (loop_vinfo, slp_node);
12155 596 : reduction_type = VECT_REDUC_INFO_TYPE (reduc_info);
12156 596 : nested_cycle_p = nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
12157 : stmt_info);
12158 : }
12159 : }
12160 : else
12161 : {
12162 34944 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12163 : return false;
12164 : }
12165 :
12166 35558 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12167 35558 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12168 :
12169 35558 : int vec_num = vect_get_num_copies (vinfo, slp_node);
12170 :
12171 35558 : cond_expr = gimple_assign_rhs1 (stmt);
12172 35558 : gcc_assert (! COMPARISON_CLASS_P (cond_expr));
12173 :
12174 35558 : if (!vect_is_simple_cond (cond_expr, vinfo, slp_node,
12175 : &comp_vectype, &dts[0], vectype)
12176 35558 : || !comp_vectype)
12177 : return false;
12178 :
12179 35542 : unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
12180 35542 : slp_tree then_slp_node, else_slp_node;
12181 35542 : if (!vect_is_simple_use (vinfo, slp_node, 1 + op_adjust,
12182 : &then_clause, &then_slp_node, &dts[2], &vectype1))
12183 : return false;
12184 35542 : if (!vect_is_simple_use (vinfo, slp_node, 2 + op_adjust,
12185 : &else_clause, &else_slp_node, &dts[3], &vectype2))
12186 : return false;
12187 :
12188 35542 : if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
12189 : return false;
12190 :
12191 35542 : if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
12192 : return false;
12193 :
12194 35542 : masked = !COMPARISON_CLASS_P (cond_expr);
12195 35542 : vec_cmp_type = truth_type_for (comp_vectype);
12196 35542 : if (vec_cmp_type == NULL_TREE
12197 71084 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
12198 35542 : TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
12199 0 : return false;
12200 :
12201 35542 : cond_code = TREE_CODE (cond_expr);
12202 35542 : if (!masked)
12203 : {
12204 0 : cond_expr0 = TREE_OPERAND (cond_expr, 0);
12205 0 : cond_expr1 = TREE_OPERAND (cond_expr, 1);
12206 : }
12207 :
12208 : /* For conditional reductions, the "then" value needs to be the candidate
12209 : value calculated by this iteration while the "else" value needs to be
12210 : the result carried over from previous iterations. If the COND_EXPR
12211 : is the other way around, we need to swap it. */
12212 35542 : bool must_invert_cmp_result = false;
12213 35542 : if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
12214 : {
12215 0 : if (masked)
12216 0 : must_invert_cmp_result = true;
12217 : else
12218 : {
12219 0 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
12220 0 : tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
12221 0 : if (new_code == ERROR_MARK)
12222 : must_invert_cmp_result = true;
12223 : else
12224 : {
12225 0 : cond_code = new_code;
12226 : /* Make sure we don't accidentally use the old condition. */
12227 0 : cond_expr = NULL_TREE;
12228 : }
12229 : }
12230 : /* ??? The vectorized operand query below doesn't allow swapping
12231 : this way for SLP. */
12232 0 : return false;
12233 : /* std::swap (then_clause, else_clause); */
12234 : }
12235 :
12236 35542 : if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
12237 : {
12238 : /* Boolean values may have another representation in vectors
12239 : and therefore we prefer bit operations over comparison for
12240 : them (which also works for scalar masks). We store opcodes
12241 : to use in bitop1 and bitop2. Statement is vectorized as
12242 : BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
12243 : depending on bitop1 and bitop2 arity. */
12244 0 : switch (cond_code)
12245 : {
12246 : case GT_EXPR:
12247 : bitop1 = BIT_NOT_EXPR;
12248 : bitop2 = BIT_AND_EXPR;
12249 : break;
12250 0 : case GE_EXPR:
12251 0 : bitop1 = BIT_NOT_EXPR;
12252 0 : bitop2 = BIT_IOR_EXPR;
12253 0 : break;
12254 0 : case LT_EXPR:
12255 0 : bitop1 = BIT_NOT_EXPR;
12256 0 : bitop2 = BIT_AND_EXPR;
12257 0 : std::swap (cond_expr0, cond_expr1);
12258 0 : break;
12259 0 : case LE_EXPR:
12260 0 : bitop1 = BIT_NOT_EXPR;
12261 0 : bitop2 = BIT_IOR_EXPR;
12262 0 : std::swap (cond_expr0, cond_expr1);
12263 0 : break;
12264 0 : case NE_EXPR:
12265 0 : bitop1 = BIT_XOR_EXPR;
12266 0 : break;
12267 0 : case EQ_EXPR:
12268 0 : bitop1 = BIT_XOR_EXPR;
12269 0 : bitop2 = BIT_NOT_EXPR;
12270 0 : break;
12271 : default:
12272 : return false;
12273 : }
12274 : cond_code = SSA_NAME;
12275 : }
12276 :
12277 35542 : if (TREE_CODE_CLASS (cond_code) == tcc_comparison
12278 0 : && reduction_type == EXTRACT_LAST_REDUCTION
12279 35542 : && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
12280 : {
12281 0 : if (dump_enabled_p ())
12282 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12283 : "reduction comparison operation not supported.\n");
12284 0 : return false;
12285 : }
12286 :
12287 35542 : if (cost_vec)
12288 : {
12289 26852 : if (bitop1 != NOP_EXPR)
12290 : {
12291 0 : machine_mode mode = TYPE_MODE (comp_vectype);
12292 0 : optab optab;
12293 :
12294 0 : optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
12295 0 : if (!optab || !can_implement_p (optab, mode))
12296 0 : return false;
12297 :
12298 0 : if (bitop2 != NOP_EXPR)
12299 : {
12300 0 : optab = optab_for_tree_code (bitop2, comp_vectype,
12301 : optab_default);
12302 0 : if (!optab || !can_implement_p (optab, mode))
12303 0 : return false;
12304 : }
12305 : }
12306 :
12307 26852 : vect_cost_for_stmt kind = vector_stmt;
12308 26852 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12309 : /* Count one reduction-like operation per vector. */
12310 : kind = vec_to_scalar;
12311 26852 : else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
12312 26852 : || (!masked
12313 0 : && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
12314 : cond_code)
12315 0 : || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
12316 14 : return false;
12317 :
12318 26838 : if (!vect_maybe_update_slp_op_vectype (SLP_TREE_CHILDREN (slp_node)[0],
12319 : comp_vectype)
12320 26838 : || (op_adjust == 1
12321 0 : && !vect_maybe_update_slp_op_vectype
12322 0 : (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
12323 26838 : || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
12324 53676 : || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype))
12325 : {
12326 0 : if (dump_enabled_p ())
12327 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12328 : "incompatible vector types for invariants\n");
12329 0 : return false;
12330 : }
12331 :
12332 26838 : if (loop_vinfo && for_reduction
12333 447 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
12334 : {
12335 68 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12336 : {
12337 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12338 : vectype, OPTIMIZE_FOR_SPEED))
12339 0 : vect_record_loop_len (loop_vinfo,
12340 : &LOOP_VINFO_LENS (loop_vinfo),
12341 : vec_num, vectype, 1);
12342 : else
12343 0 : vect_record_loop_mask (loop_vinfo,
12344 : &LOOP_VINFO_MASKS (loop_vinfo),
12345 : vec_num, vectype, NULL);
12346 : }
12347 : /* Extra inactive lanes should be safe for vect_nested_cycle. */
12348 68 : else if (!nested_cycle_p)
12349 : {
12350 68 : if (dump_enabled_p ())
12351 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12352 : "conditional reduction prevents the use"
12353 : " of partial vectors.\n");
12354 68 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
12355 : }
12356 : }
12357 :
12358 26838 : SLP_TREE_TYPE (slp_node) = condition_vec_info_type;
12359 26838 : vect_model_simple_cost (vinfo, 1, slp_node, cost_vec, kind);
12360 26838 : return true;
12361 : }
12362 :
12363 : /* Transform. */
12364 :
12365 : /* Handle def. */
12366 8690 : scalar_dest = gimple_assign_lhs (stmt);
12367 8690 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12368 8690 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
12369 :
12370 8690 : bool swap_cond_operands = false;
12371 :
12372 : /* See whether another part of the vectorized code applies a loop
12373 : mask to the condition, or to its inverse. */
12374 :
12375 8690 : vec_loop_masks *masks = NULL;
12376 8690 : vec_loop_lens *lens = NULL;
12377 8690 : if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12378 : {
12379 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12380 0 : lens = &LOOP_VINFO_LENS (loop_vinfo);
12381 : }
12382 8690 : else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
12383 : {
12384 3 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12385 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12386 : else
12387 : {
12388 3 : scalar_cond_masked_key cond (cond_expr, 1);
12389 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12390 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12391 : else
12392 : {
12393 3 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
12394 3 : tree_code orig_code = cond.code;
12395 3 : cond.code = invert_tree_comparison (cond.code, honor_nans);
12396 3 : if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
12397 : {
12398 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12399 0 : cond_code = cond.code;
12400 0 : swap_cond_operands = true;
12401 : }
12402 : else
12403 : {
12404 : /* Try the inverse of the current mask. We check if the
12405 : inverse mask is live and if so we generate a negate of
12406 : the current mask such that we still honor NaNs. */
12407 3 : cond.inverted_p = true;
12408 3 : cond.code = orig_code;
12409 3 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12410 : {
12411 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12412 0 : cond_code = cond.code;
12413 0 : swap_cond_operands = true;
12414 0 : must_invert_cmp_result = true;
12415 : }
12416 : }
12417 : }
12418 : }
12419 : }
12420 :
12421 : /* Handle cond expr. */
12422 8690 : if (masked)
12423 8690 : vect_get_vec_defs (vinfo, slp_node,
12424 : cond_expr, &vec_oprnds0,
12425 : then_clause, &vec_oprnds2,
12426 : reduction_type != EXTRACT_LAST_REDUCTION
12427 : ? else_clause : NULL, &vec_oprnds3);
12428 : else
12429 0 : vect_get_vec_defs (vinfo, slp_node,
12430 : cond_expr0, &vec_oprnds0,
12431 : cond_expr1, &vec_oprnds1,
12432 : then_clause, &vec_oprnds2,
12433 : reduction_type != EXTRACT_LAST_REDUCTION
12434 : ? else_clause : NULL, &vec_oprnds3);
12435 :
12436 8690 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12437 0 : vec_else_clause = else_clause;
12438 :
12439 : /* Arguments are ready. Create the new vector stmt. */
12440 20432 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
12441 : {
12442 11742 : vec_then_clause = vec_oprnds2[i];
12443 11742 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12444 11742 : vec_else_clause = vec_oprnds3[i];
12445 :
12446 11742 : if (swap_cond_operands)
12447 0 : std::swap (vec_then_clause, vec_else_clause);
12448 :
12449 11742 : if (masked)
12450 : vec_compare = vec_cond_lhs;
12451 : else
12452 : {
12453 0 : vec_cond_rhs = vec_oprnds1[i];
12454 0 : if (bitop1 == NOP_EXPR)
12455 : {
12456 0 : gimple_seq stmts = NULL;
12457 0 : vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
12458 : vec_cond_lhs, vec_cond_rhs);
12459 0 : gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
12460 : }
12461 : else
12462 : {
12463 0 : new_temp = make_ssa_name (vec_cmp_type);
12464 0 : gassign *new_stmt;
12465 0 : if (bitop1 == BIT_NOT_EXPR)
12466 0 : new_stmt = gimple_build_assign (new_temp, bitop1,
12467 : vec_cond_rhs);
12468 : else
12469 0 : new_stmt
12470 0 : = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
12471 : vec_cond_rhs);
12472 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12473 0 : if (bitop2 == NOP_EXPR)
12474 : vec_compare = new_temp;
12475 0 : else if (bitop2 == BIT_NOT_EXPR
12476 0 : && reduction_type != EXTRACT_LAST_REDUCTION)
12477 : {
12478 : /* Instead of doing ~x ? y : z do x ? z : y. */
12479 : vec_compare = new_temp;
12480 : std::swap (vec_then_clause, vec_else_clause);
12481 : }
12482 : else
12483 : {
12484 0 : vec_compare = make_ssa_name (vec_cmp_type);
12485 0 : if (bitop2 == BIT_NOT_EXPR)
12486 0 : new_stmt
12487 0 : = gimple_build_assign (vec_compare, bitop2, new_temp);
12488 : else
12489 0 : new_stmt
12490 0 : = gimple_build_assign (vec_compare, bitop2,
12491 : vec_cond_lhs, new_temp);
12492 0 : vect_finish_stmt_generation (vinfo, stmt_info,
12493 : new_stmt, gsi);
12494 : }
12495 : }
12496 : }
12497 :
12498 : /* If we decided to apply a loop mask to the result of the vector
12499 : comparison, AND the comparison with the mask now. Later passes
12500 : should then be able to reuse the AND results between multiple
12501 : vector statements.
12502 :
12503 : For example:
12504 : for (int i = 0; i < 100; ++i)
12505 : x[i] = y[i] ? z[i] : 10;
12506 :
12507 : results in following optimized GIMPLE:
12508 :
12509 : mask__35.8_43 = vect__4.7_41 != { 0, ... };
12510 : vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
12511 : _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
12512 : vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
12513 : vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
12514 : vect_iftmp.11_47, { 10, ... }>;
12515 :
12516 : instead of using a masked and unmasked forms of
12517 : vec != { 0, ... } (masked in the MASK_LOAD,
12518 : unmasked in the VEC_COND_EXPR). */
12519 :
12520 : /* Force vec_compare to be an SSA_NAME rather than a comparison,
12521 : in cases where that's necessary. */
12522 :
12523 11742 : tree len = NULL_TREE, bias = NULL_TREE;
12524 11742 : if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
12525 : {
12526 0 : if (!is_gimple_val (vec_compare))
12527 : {
12528 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12529 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12530 : vec_compare);
12531 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12532 0 : vec_compare = vec_compare_name;
12533 : }
12534 :
12535 0 : if (must_invert_cmp_result)
12536 : {
12537 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12538 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12539 : BIT_NOT_EXPR,
12540 : vec_compare);
12541 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12542 0 : vec_compare = vec_compare_name;
12543 : }
12544 :
12545 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12546 : vectype, OPTIMIZE_FOR_SPEED))
12547 : {
12548 0 : if (lens)
12549 : {
12550 : /* ??? Do we really want the adjusted LEN here? Isn't this
12551 : based on number of elements? */
12552 0 : len = vect_get_loop_len (loop_vinfo, gsi, lens,
12553 : vec_num, vectype, i, 1, true);
12554 0 : signed char biasval
12555 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
12556 0 : bias = build_int_cst (intQI_type_node, biasval);
12557 : }
12558 : else
12559 : {
12560 0 : len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12561 0 : bias = build_int_cst (intQI_type_node, 0);
12562 : }
12563 : }
12564 0 : if (masks)
12565 : {
12566 0 : tree loop_mask
12567 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
12568 : vectype, i);
12569 0 : tree tmp2 = make_ssa_name (vec_cmp_type);
12570 0 : gassign *g
12571 0 : = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
12572 : loop_mask);
12573 0 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
12574 0 : vec_compare = tmp2;
12575 : }
12576 : }
12577 :
12578 0 : gimple *new_stmt;
12579 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12580 : {
12581 0 : gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
12582 0 : tree lhs = gimple_get_lhs (old_stmt);
12583 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12584 0 : lhs = copy_ssa_name (lhs);
12585 0 : if (len)
12586 0 : new_stmt = gimple_build_call_internal
12587 0 : (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
12588 : vec_then_clause, len, bias);
12589 : else
12590 0 : new_stmt = gimple_build_call_internal
12591 0 : (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
12592 : vec_then_clause);
12593 0 : gimple_call_set_lhs (new_stmt, lhs);
12594 0 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12595 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12596 : {
12597 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12598 0 : vec_else_clause = lhs;
12599 : }
12600 0 : else if (old_stmt == gsi_stmt (*gsi))
12601 0 : vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
12602 : else
12603 : {
12604 : /* In this case we're moving the definition to later in the
12605 : block. That doesn't matter because the only uses of the
12606 : lhs are in phi statements. */
12607 0 : gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
12608 0 : gsi_remove (&old_gsi, true);
12609 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12610 : }
12611 : }
12612 : else
12613 : {
12614 11742 : new_temp = make_ssa_name (vec_dest);
12615 11742 : new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
12616 : vec_then_clause, vec_else_clause);
12617 11742 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12618 : }
12619 11742 : slp_node->push_vec_def (new_stmt);
12620 : }
12621 :
12622 8690 : vec_oprnds0.release ();
12623 8690 : vec_oprnds1.release ();
12624 8690 : vec_oprnds2.release ();
12625 8690 : vec_oprnds3.release ();
12626 :
12627 8690 : return true;
12628 : }
12629 :
12630 : /* Helper of vectorizable_comparison.
12631 :
12632 : Check if STMT_INFO is comparison expression CODE that can be vectorized.
12633 : If COST_VEC is passed, calculate costs but don't change anything,
12634 : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12635 : it at GSI.
12636 :
12637 : Return true if STMT_INFO is vectorizable in this way. */
12638 :
12639 : static bool
12640 386840 : vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
12641 : stmt_vec_info stmt_info, tree_code code,
12642 : gimple_stmt_iterator *gsi,
12643 : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12644 : {
12645 386840 : tree lhs, rhs1, rhs2;
12646 386840 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12647 386840 : tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
12648 386840 : tree new_temp;
12649 386840 : enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
12650 386840 : poly_uint64 nunits;
12651 386840 : enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12652 386840 : int i;
12653 386840 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12654 386840 : vec<tree> vec_oprnds0 = vNULL;
12655 386840 : vec<tree> vec_oprnds1 = vNULL;
12656 386840 : tree mask_type;
12657 386840 : tree mask = NULL_TREE;
12658 :
12659 386840 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12660 : return false;
12661 :
12662 386840 : if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
12663 : return false;
12664 :
12665 173059 : mask_type = vectype;
12666 173059 : nunits = TYPE_VECTOR_SUBPARTS (vectype);
12667 :
12668 173059 : if (TREE_CODE_CLASS (code) != tcc_comparison)
12669 : return false;
12670 :
12671 171191 : slp_tree slp_rhs1, slp_rhs2;
12672 171191 : if (!vect_is_simple_use (vinfo, slp_node,
12673 : 0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
12674 : return false;
12675 :
12676 171191 : if (!vect_is_simple_use (vinfo, slp_node,
12677 : 1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
12678 : return false;
12679 :
12680 133169 : if (vectype1 && vectype2
12681 247620 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12682 76429 : TYPE_VECTOR_SUBPARTS (vectype2)))
12683 16 : return false;
12684 :
12685 171175 : vectype = vectype1 ? vectype1 : vectype2;
12686 :
12687 : /* Invariant comparison. */
12688 171175 : if (!vectype)
12689 : {
12690 33237 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
12691 33237 : if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
12692 9 : return false;
12693 : }
12694 137938 : else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
12695 : return false;
12696 :
12697 : /* Can't compare mask and non-mask types. */
12698 133153 : if (vectype1 && vectype2
12699 399781 : && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
12700 : return false;
12701 :
12702 : /* Boolean values may have another representation in vectors
12703 : and therefore we prefer bit operations over comparison for
12704 : them (which also works for scalar masks). We store opcodes
12705 : to use in bitop1 and bitop2. Statement is vectorized as
12706 : BITOP2 (rhs1 BITOP1 rhs2) or
12707 : rhs1 BITOP2 (BITOP1 rhs2)
12708 : depending on bitop1 and bitop2 arity. */
12709 171158 : bool swap_p = false;
12710 171158 : if (VECTOR_BOOLEAN_TYPE_P (vectype))
12711 : {
12712 769 : if (code == GT_EXPR)
12713 : {
12714 : bitop1 = BIT_NOT_EXPR;
12715 : bitop2 = BIT_AND_EXPR;
12716 : }
12717 : else if (code == GE_EXPR)
12718 : {
12719 : bitop1 = BIT_NOT_EXPR;
12720 : bitop2 = BIT_IOR_EXPR;
12721 : }
12722 : else if (code == LT_EXPR)
12723 : {
12724 : bitop1 = BIT_NOT_EXPR;
12725 : bitop2 = BIT_AND_EXPR;
12726 : swap_p = true;
12727 : }
12728 : else if (code == LE_EXPR)
12729 : {
12730 : bitop1 = BIT_NOT_EXPR;
12731 : bitop2 = BIT_IOR_EXPR;
12732 : swap_p = true;
12733 : }
12734 : else
12735 : {
12736 : bitop1 = BIT_XOR_EXPR;
12737 : if (code == EQ_EXPR)
12738 : bitop2 = BIT_NOT_EXPR;
12739 : }
12740 : }
12741 :
12742 171158 : if (cost_vec)
12743 : {
12744 158455 : if (bitop1 == NOP_EXPR)
12745 : {
12746 157833 : if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
12747 : return false;
12748 : }
12749 : else
12750 : {
12751 622 : machine_mode mode = TYPE_MODE (vectype);
12752 622 : optab optab;
12753 :
12754 622 : optab = optab_for_tree_code (bitop1, vectype, optab_default);
12755 622 : if (!optab || !can_implement_p (optab, mode))
12756 0 : return false;
12757 :
12758 622 : if (bitop2 != NOP_EXPR)
12759 : {
12760 95 : optab = optab_for_tree_code (bitop2, vectype, optab_default);
12761 95 : if (!optab || !can_implement_p (optab, mode))
12762 0 : return false;
12763 : }
12764 : }
12765 :
12766 : /* Put types on constant and invariant SLP children. */
12767 147916 : if (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
12768 147916 : || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype))
12769 : {
12770 2 : if (dump_enabled_p ())
12771 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12772 : "incompatible vector types for invariants\n");
12773 2 : return false;
12774 : }
12775 :
12776 147914 : vect_model_simple_cost (vinfo, 1 + (bitop2 != NOP_EXPR),
12777 : slp_node, cost_vec);
12778 147914 : return true;
12779 : }
12780 :
12781 : /* Transform. */
12782 :
12783 : /* Handle def. */
12784 12703 : lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
12785 12703 : if (lhs)
12786 12703 : mask = vect_create_destination_var (lhs, mask_type);
12787 :
12788 12703 : vect_get_vec_defs (vinfo, slp_node, rhs1, &vec_oprnds0, rhs2, &vec_oprnds1);
12789 12703 : if (swap_p)
12790 58 : std::swap (vec_oprnds0, vec_oprnds1);
12791 :
12792 : /* Arguments are ready. Create the new vector stmt. */
12793 31915 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
12794 : {
12795 19212 : gimple *new_stmt;
12796 19212 : vec_rhs2 = vec_oprnds1[i];
12797 :
12798 19212 : if (lhs)
12799 19212 : new_temp = make_ssa_name (mask);
12800 : else
12801 0 : new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
12802 19212 : if (bitop1 == NOP_EXPR)
12803 : {
12804 19055 : new_stmt = gimple_build_assign (new_temp, code,
12805 : vec_rhs1, vec_rhs2);
12806 19055 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12807 : }
12808 : else
12809 : {
12810 157 : if (bitop1 == BIT_NOT_EXPR)
12811 84 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
12812 : else
12813 73 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
12814 : vec_rhs2);
12815 157 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12816 157 : if (bitop2 != NOP_EXPR)
12817 : {
12818 84 : tree res = make_ssa_name (mask);
12819 84 : if (bitop2 == BIT_NOT_EXPR)
12820 0 : new_stmt = gimple_build_assign (res, bitop2, new_temp);
12821 : else
12822 84 : new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
12823 : new_temp);
12824 84 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12825 : }
12826 : }
12827 19212 : slp_node->push_vec_def (new_stmt);
12828 : }
12829 :
12830 12703 : vec_oprnds0.release ();
12831 12703 : vec_oprnds1.release ();
12832 :
12833 12703 : return true;
12834 : }
12835 :
12836 : /* vectorizable_comparison.
12837 :
12838 : Check if STMT_INFO is comparison expression that can be vectorized.
12839 : If COST_VEC is passed, calculate costs but don't change anything,
12840 : otherwise, vectorize STMT_INFO: create a vectorized comparison, and insert
12841 : it at GSI.
12842 :
12843 : Return true if STMT_INFO is vectorizable in this way. */
12844 :
12845 : static bool
12846 702186 : vectorizable_comparison (vec_info *vinfo,
12847 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12848 : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12849 : {
12850 702186 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12851 :
12852 702186 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12853 : return false;
12854 :
12855 702186 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12856 : return false;
12857 :
12858 928409 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12859 384206 : if (!stmt)
12860 : return false;
12861 :
12862 384206 : enum tree_code code = gimple_assign_rhs_code (stmt);
12863 384206 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12864 384206 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
12865 : slp_node, cost_vec))
12866 : return false;
12867 :
12868 157983 : if (cost_vec)
12869 145280 : SLP_TREE_TYPE (slp_node) = comparison_vec_info_type;
12870 :
12871 : return true;
12872 : }
12873 :
12874 : /* Check to see if the target supports any of the compare and branch optabs for
12875 : vectors with MODE as these would be required when expanding. */
12876 : static bool
12877 65385 : supports_vector_compare_and_branch (loop_vec_info loop_vinfo, machine_mode mode)
12878 : {
12879 65385 : bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
12880 65385 : bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
12881 :
12882 : /* The vectorizer only produces vec_cbranch_any_optab directly. So only
12883 : check for support for that or vec_cbranch_any_optab when masked.
12884 : We can't produce vcond_cbranch_any directly from the vectorizer as we
12885 : want to keep gimple_cond as the GIMPLE representation. But we'll fold
12886 : it in expand. For that reason we require a backend to support the
12887 : unconditional vector cbranch optab if they support the conditional one,
12888 : which is just an optimization on the unconditional one. */
12889 65385 : if (masked_loop_p
12890 65385 : && direct_optab_handler (cond_vec_cbranch_any_optab, mode)
12891 : != CODE_FOR_nothing)
12892 : return true;
12893 65385 : else if (len_loop_p
12894 65385 : && direct_optab_handler (cond_len_vec_cbranch_any_optab, mode)
12895 : != CODE_FOR_nothing)
12896 : return true;
12897 65385 : else if (!masked_loop_p && !len_loop_p
12898 130770 : && direct_optab_handler (vec_cbranch_any_optab, mode)
12899 : != CODE_FOR_nothing)
12900 : return true;
12901 :
12902 : /* The target can implement cbranch to distinguish between boolean vector
12903 : types and data types if they don't have a different mode for both. */
12904 65385 : return direct_optab_handler (cbranch_optab, mode) != CODE_FOR_nothing;
12905 : }
12906 :
12907 : /* Determine the type to use for early break vectorization's scalar IV. If
12908 : no type is possible return false. */
12909 :
12910 : static bool
12911 2634 : vect_compute_type_for_early_break_scalar_iv (loop_vec_info loop_vinfo)
12912 : {
12913 : /* Check if we have a usable scalar IV type for vectorization. */
12914 2634 : tree iters_vf_type = sizetype;
12915 2634 : if (!LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo))
12916 : {
12917 : /* Find the type with the minimum precision we can use
12918 : for the scalar IV. */
12919 2411 : tree cand_type = TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo));
12920 :
12921 : /* Work out how many bits we need to represent the limit. */
12922 2411 : unsigned int min_ni_width
12923 2411 : = vect_min_prec_for_max_niters (loop_vinfo, 1);
12924 :
12925 : /* Check if we're using PFA, if so we need a signed IV and an
12926 : extra bit for the sign. */
12927 2411 : if (TYPE_UNSIGNED (cand_type)
12928 2411 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
12929 3982 : && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo))
12930 159 : min_ni_width += 1;
12931 :
12932 2411 : if (TYPE_PRECISION (cand_type) >= min_ni_width)
12933 2338 : iters_vf_type = unsigned_type_for (cand_type);
12934 : else
12935 : {
12936 73 : opt_scalar_int_mode cmp_mode_iter;
12937 73 : tree iv_type = NULL_TREE;
12938 357 : FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
12939 : {
12940 357 : auto cmp_mode = cmp_mode_iter.require ();
12941 357 : unsigned int cmp_bits = GET_MODE_BITSIZE (cmp_mode);
12942 357 : if (cmp_bits >= min_ni_width
12943 357 : && targetm.scalar_mode_supported_p (cmp_mode))
12944 : {
12945 73 : iv_type = build_nonstandard_integer_type (cmp_bits, true);
12946 73 : if (iv_type)
12947 : break;
12948 : }
12949 : }
12950 :
12951 73 : if (!iv_type)
12952 : {
12953 0 : if (dump_enabled_p ())
12954 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12955 : "can't vectorize early exit because the "
12956 : "target doesn't support a scalar type wide "
12957 : "wide enough to hold niters.\n");
12958 0 : return false;
12959 : }
12960 73 : iters_vf_type = iv_type;
12961 : }
12962 : }
12963 :
12964 2634 : LOOP_VINFO_EARLY_BRK_IV_TYPE (loop_vinfo) = iters_vf_type;
12965 2634 : return true;
12966 : }
12967 :
12968 : /* Check to see if the current early break given in STMT_INFO is valid for
12969 : vectorization. */
12970 :
12971 : bool
12972 260589 : vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
12973 : gimple_stmt_iterator *gsi,
12974 : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12975 : {
12976 260589 : if (!is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
12977 : return false;
12978 :
12979 66977 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
12980 : return false;
12981 :
12982 66977 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
12983 : return false;
12984 :
12985 66977 : DUMP_VECT_SCOPE ("vectorizable_early_exit");
12986 :
12987 66977 : auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
12988 :
12989 : /* For SLP we don't want to use the type of the operands of the SLP node, when
12990 : vectorizing using SLP slp_node will be the children of the gcond and we
12991 : want to use the type of the direct children which since the gcond is root
12992 : will be the current node, rather than a child node as vect_is_simple_use
12993 : assumes. */
12994 66977 : tree vectype = SLP_TREE_VECTYPE (slp_node);
12995 66977 : if (!vectype)
12996 : return false;
12997 :
12998 66977 : machine_mode mode = TYPE_MODE (vectype);
12999 66977 : int vec_num = vect_get_num_copies (loop_vinfo, slp_node);
13000 :
13001 66977 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
13002 66977 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
13003 66977 : bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
13004 66977 : bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
13005 :
13006 : /* Now build the new conditional. Pattern gimple_conds get dropped during
13007 : codegen so we must replace the original insn. */
13008 66977 : gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
13009 66977 : gcond *cond_stmt = as_a <gcond *>(orig_stmt);
13010 :
13011 66977 : tree vectype_out = vectype;
13012 66977 : auto bb = gimple_bb (cond_stmt);
13013 66977 : edge exit_true_edge = EDGE_SUCC (bb, 0);
13014 66977 : if (exit_true_edge->flags & EDGE_FALSE_VALUE)
13015 664 : exit_true_edge = EDGE_SUCC (bb, 1);
13016 66977 : gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
13017 :
13018 : /* When vectorizing we assume that if the branch edge is taken that we're
13019 : exiting the loop. This is not however always the case as the compiler will
13020 : rewrite conditions to always be a comparison against 0. To do this it
13021 : sometimes flips the edges. This is fine for scalar, but for vector we
13022 : then have to negate the result of the test, as we're still assuming that if
13023 : you take the branch edge that we found the exit condition. i.e. we need to
13024 : know whether we are generating a `forall` or an `exist` condition. */
13025 133954 : bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
13026 66977 : exit_true_edge->dest);
13027 :
13028 : /* See if we support ADDHN and use that for the reduction. */
13029 66977 : internal_fn ifn = IFN_VEC_TRUNC_ADD_HIGH;
13030 66977 : bool addhn_supported_p
13031 66977 : = direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_BOTH);
13032 66977 : tree narrow_type = NULL_TREE;
13033 66977 : if (addhn_supported_p)
13034 : {
13035 : /* Calculate the narrowing type for the result. */
13036 0 : auto halfprec = TYPE_PRECISION (TREE_TYPE (vectype)) / 2;
13037 0 : auto unsignedp = TYPE_UNSIGNED (TREE_TYPE (vectype));
13038 0 : tree itype = build_nonstandard_integer_type (halfprec, unsignedp);
13039 0 : tree tmp_type = build_vector_type (itype, TYPE_VECTOR_SUBPARTS (vectype));
13040 0 : narrow_type = truth_type_for (tmp_type);
13041 :
13042 0 : if (!supports_vector_compare_and_branch (loop_vinfo,
13043 0 : TYPE_MODE (narrow_type)))
13044 : {
13045 0 : if (dump_enabled_p ())
13046 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13047 : "can't use ADDHN reduction because cbranch for "
13048 : "the narrowed type is not supported by the "
13049 : "target.\n");
13050 : addhn_supported_p = false;
13051 : }
13052 : }
13053 :
13054 : /* Analyze only. */
13055 66977 : if (cost_vec)
13056 : {
13057 65385 : if (!addhn_supported_p
13058 65385 : && !supports_vector_compare_and_branch (loop_vinfo, mode))
13059 : {
13060 62751 : if (dump_enabled_p ())
13061 597 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13062 : "can't vectorize early exit because the "
13063 : "target doesn't support flag setting vector "
13064 : "comparisons.\n");
13065 62751 : return false;
13066 : }
13067 :
13068 2634 : if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code, gsi,
13069 : slp_node, cost_vec))
13070 : return false;
13071 :
13072 2634 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
13073 : {
13074 1571 : if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
13075 : OPTIMIZE_FOR_SPEED))
13076 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
13077 : else
13078 1571 : vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
13079 : }
13080 :
13081 2634 : if (!vect_compute_type_for_early_break_scalar_iv (loop_vinfo))
13082 : return false;
13083 :
13084 : return true;
13085 : }
13086 :
13087 : /* Transform. */
13088 :
13089 1592 : tree new_temp = NULL_TREE;
13090 1592 : gimple *new_stmt = NULL;
13091 :
13092 1592 : if (dump_enabled_p ())
13093 406 : dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
13094 :
13095 : /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
13096 : roots and so by the time we get to them we have already codegened the SLP tree
13097 : and so we shouldn't try to do so again. The arguments have already been
13098 : vectorized. It's not very clean to do this here, But the masking code below is
13099 : complex and this keeps it all in one place to ease fixes and backports. Once we
13100 : drop the non-SLP loop vect or split vectorizable_* this can be simplified. */
13101 :
13102 1592 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
13103 1592 : basic_block cond_bb = gimple_bb (stmt);
13104 1592 : gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb);
13105 :
13106 1592 : auto_vec<tree> stmts;
13107 1592 : stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
13108 :
13109 : /* If we're comparing against a previous forall we need to negate the results
13110 : before we do the final comparison or reduction. */
13111 1592 : if (flipped)
13112 : {
13113 : /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
13114 : if (any(~mask)) by negating the masks and flipping the branches.
13115 :
13116 : 1. For unmasked loops we simply reduce the ~mask.
13117 : 2. For masked loops we reduce (~mask & loop_mask) which is the same as
13118 : doing (mask & loop_mask) ^ loop_mask. */
13119 294 : for (unsigned i = 0; i < stmts.length (); i++)
13120 : {
13121 173 : tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
13122 173 : auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
13123 173 : vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
13124 : &cond_gsi);
13125 173 : stmts[i] = inv_lhs;
13126 : }
13127 :
13128 121 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13129 121 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13130 : }
13131 :
13132 : /* Determine if we need to reduce the final value. */
13133 1592 : if (stmts.length () > 1)
13134 : {
13135 : /* We build the reductions in a way to maintain as much parallelism as
13136 : possible. */
13137 142 : auto_vec<tree> workset (stmts.length ());
13138 :
13139 : /* Mask the statements as we queue them up. Normally we loop over
13140 : vec_num, but since we inspect the exact results of vectorization
13141 : we don't need to and instead can just use the stmts themselves. */
13142 142 : if (masked_loop_p)
13143 0 : for (unsigned i = 0; i < stmts.length (); i++)
13144 : {
13145 0 : tree stmt_mask
13146 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num,
13147 : vectype, i);
13148 0 : stmt_mask
13149 0 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
13150 0 : stmts[i], &cond_gsi);
13151 0 : workset.quick_push (stmt_mask);
13152 : }
13153 142 : else if (len_loop_p)
13154 0 : for (unsigned i = 0; i < stmts.length (); i++)
13155 : {
13156 0 : tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
13157 : lens, vec_num,
13158 0 : vectype, stmts[i], i, 1);
13159 :
13160 0 : workset.quick_push (len_mask);
13161 : }
13162 : else
13163 142 : workset.splice (stmts);
13164 :
13165 432 : while (workset.length () > 1)
13166 : {
13167 290 : tree arg0 = workset.pop ();
13168 290 : tree arg1 = workset.pop ();
13169 290 : if (addhn_supported_p && workset.length () == 0)
13170 : {
13171 0 : new_stmt = gimple_build_call_internal (ifn, 2, arg0, arg1);
13172 0 : vectype_out = narrow_type;
13173 0 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
13174 0 : gimple_call_set_lhs (as_a <gcall *> (new_stmt), new_temp);
13175 0 : gimple_call_set_nothrow (as_a <gcall *> (new_stmt), true);
13176 : }
13177 : else
13178 : {
13179 290 : new_temp = make_temp_ssa_name (vectype_out, NULL, "vexit_reduc");
13180 290 : new_stmt
13181 290 : = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
13182 : }
13183 290 : vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
13184 : &cond_gsi);
13185 290 : workset.quick_insert (0, new_temp);
13186 : }
13187 142 : }
13188 : else
13189 : {
13190 1450 : new_temp = stmts[0];
13191 1450 : if (masked_loop_p)
13192 : {
13193 2 : tree mask
13194 2 : = vect_get_loop_mask (loop_vinfo, gsi, masks, 1, vectype, 0);
13195 2 : new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
13196 : new_temp, &cond_gsi);
13197 : }
13198 1448 : else if (len_loop_p)
13199 0 : new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
13200 : 1, vectype, new_temp, 0, 1);
13201 : }
13202 :
13203 1592 : gcc_assert (new_temp);
13204 :
13205 1592 : tree cst = build_zero_cst (vectype_out);
13206 1592 : gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
13207 1592 : update_stmt (orig_stmt);
13208 :
13209 : /* ??? */
13210 1592 : SLP_TREE_VEC_DEFS (slp_node).truncate (0);
13211 :
13212 1592 : return true;
13213 1592 : }
13214 :
13215 : /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
13216 : can handle all live statements in the node. Otherwise return true
13217 : if STMT_INFO is not live or if vectorizable_live_operation can handle it.
13218 : VEC_STMT_P is as for vectorizable_live_operation. */
13219 :
13220 : static bool
13221 1317048 : can_vectorize_live_stmts (vec_info *vinfo,
13222 : slp_tree slp_node, slp_instance slp_node_instance,
13223 : bool vec_stmt_p,
13224 : stmt_vector_for_cost *cost_vec)
13225 : {
13226 1317048 : stmt_vec_info slp_stmt_info;
13227 1317048 : unsigned int i;
13228 2775504 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
13229 : {
13230 1458456 : if (slp_stmt_info
13231 1442255 : && STMT_VINFO_LIVE_P (slp_stmt_info)
13232 1595440 : && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
13233 : slp_node_instance, i,
13234 : vec_stmt_p, cost_vec))
13235 : return false;
13236 : }
13237 :
13238 : return true;
13239 : }
13240 :
13241 : /* Make sure the statement is vectorizable. */
13242 :
13243 : opt_result
13244 2633954 : vect_analyze_stmt (vec_info *vinfo,
13245 : slp_tree node, slp_instance node_instance,
13246 : stmt_vector_for_cost *cost_vec)
13247 : {
13248 2633954 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
13249 2633954 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13250 2633954 : enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
13251 2633954 : bool ok;
13252 :
13253 2633954 : if (dump_enabled_p ())
13254 101428 : dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
13255 : stmt_info->stmt);
13256 :
13257 4970224 : if (gimple_has_volatile_ops (stmt_info->stmt))
13258 : {
13259 : /* ??? This shouldn't really happen, volatile stmts should
13260 : not end up in the SLP graph. */
13261 0 : return opt_result::failure_at (stmt_info->stmt,
13262 : "not vectorized:"
13263 : " stmt has volatile operands: %G\n",
13264 : stmt_info->stmt);
13265 : }
13266 :
13267 : /* Skip stmts that do not need to be vectorized. */
13268 2633954 : if (!STMT_VINFO_RELEVANT_P (stmt_info)
13269 0 : && !STMT_VINFO_LIVE_P (stmt_info))
13270 : {
13271 0 : if (dump_enabled_p ())
13272 0 : dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
13273 :
13274 : /* ??? This shouldn't really happen, irrelevant stmts should
13275 : not end up in the SLP graph. */
13276 0 : return opt_result::failure_at (stmt_info->stmt,
13277 : "not vectorized:"
13278 : " irrelevant stmt as SLP node %p "
13279 : "representative.\n",
13280 : (void *)node);
13281 : }
13282 :
13283 2633954 : switch (STMT_VINFO_DEF_TYPE (stmt_info))
13284 : {
13285 : case vect_internal_def:
13286 : case vect_condition_def:
13287 : break;
13288 :
13289 84663 : case vect_reduction_def:
13290 84663 : case vect_nested_cycle:
13291 84663 : gcc_assert (!bb_vinfo
13292 : && (relevance == vect_used_in_outer
13293 : || relevance == vect_used_in_outer_by_reduction
13294 : || relevance == vect_used_by_reduction
13295 : || relevance == vect_unused_in_scope
13296 : || relevance == vect_used_only_live));
13297 : break;
13298 :
13299 322 : case vect_double_reduction_def:
13300 322 : gcc_assert (!bb_vinfo && node);
13301 : break;
13302 :
13303 158542 : case vect_induction_def:
13304 158542 : case vect_first_order_recurrence:
13305 158542 : gcc_assert (!bb_vinfo);
13306 : break;
13307 :
13308 0 : case vect_constant_def:
13309 0 : case vect_external_def:
13310 0 : case vect_unknown_def_type:
13311 0 : default:
13312 0 : gcc_unreachable ();
13313 : }
13314 :
13315 2633954 : tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
13316 2633954 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
13317 :
13318 2633954 : if (STMT_VINFO_RELEVANT_P (stmt_info))
13319 : {
13320 2633954 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
13321 2633954 : gcc_assert (SLP_TREE_VECTYPE (node)
13322 : || gimple_code (stmt_info->stmt) == GIMPLE_COND
13323 : || (call && gimple_call_lhs (call) == NULL_TREE));
13324 : }
13325 :
13326 2633954 : ok = true;
13327 2633954 : if (bb_vinfo
13328 1510660 : || (STMT_VINFO_RELEVANT_P (stmt_info)
13329 0 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
13330 : /* Prefer vectorizable_call over vectorizable_simd_clone_call so
13331 : -mveclibabi= takes preference over library functions with
13332 : the simd attribute. */
13333 2633954 : ok = (vectorizable_call (vinfo, stmt_info, NULL, node, cost_vec)
13334 2627060 : || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, node,
13335 : cost_vec)
13336 2626583 : || vectorizable_conversion (vinfo, stmt_info, NULL, node, cost_vec)
13337 2540400 : || vectorizable_operation (vinfo, stmt_info, NULL, node, cost_vec)
13338 2116130 : || vectorizable_assignment (vinfo, stmt_info, NULL, node, cost_vec)
13339 2045719 : || vectorizable_load (vinfo, stmt_info, NULL, node, cost_vec)
13340 1589179 : || vectorizable_store (vinfo, stmt_info, NULL, node, cost_vec)
13341 763007 : || vectorizable_shift (vinfo, stmt_info, NULL, node, cost_vec)
13342 716321 : || vectorizable_condition (vinfo, stmt_info, NULL, node, cost_vec)
13343 689483 : || vectorizable_comparison (vinfo, stmt_info, NULL, node, cost_vec)
13344 544203 : || (bb_vinfo
13345 142924 : && vectorizable_phi (bb_vinfo, stmt_info, node, cost_vec))
13346 3116899 : || (is_a <loop_vec_info> (vinfo)
13347 401279 : && (vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
13348 : stmt_info, node, cost_vec)
13349 400557 : || vectorizable_reduction (as_a <loop_vec_info> (vinfo),
13350 : stmt_info,
13351 : node, node_instance, cost_vec)
13352 318469 : || vectorizable_induction (as_a <loop_vec_info> (vinfo),
13353 : stmt_info, node, cost_vec)
13354 194694 : || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
13355 : stmt_info, node)
13356 193873 : || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13357 : stmt_info, node, cost_vec)
13358 193612 : || vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
13359 : stmt_info, NULL, node,
13360 : cost_vec))));
13361 :
13362 2633954 : STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
13363 :
13364 2358676 : if (!ok)
13365 275278 : return opt_result::failure_at (stmt_info->stmt,
13366 : "not vectorized:"
13367 : " relevant stmt not supported: %G",
13368 : stmt_info->stmt);
13369 :
13370 : /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
13371 : need extra handling, except for vectorizable reductions. */
13372 2358676 : if (!bb_vinfo
13373 1317048 : && (SLP_TREE_TYPE (node) != lc_phi_info_type
13374 821 : || SLP_TREE_DEF_TYPE (node) == vect_internal_def)
13375 1317048 : && (!node->ldst_lanes || SLP_TREE_PERMUTE_P (node))
13376 3675724 : && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
13377 : node, node_instance,
13378 : false, cost_vec))
13379 0 : return opt_result::failure_at (stmt_info->stmt,
13380 : "not vectorized:"
13381 : " live stmt not supported: %G",
13382 : stmt_info->stmt);
13383 :
13384 2358676 : return opt_result::success ();
13385 : }
13386 :
13387 :
13388 : /* Function vect_transform_stmt.
13389 :
13390 : Create a vectorized stmt to replace STMT_INFO, and insert it at GSI. */
13391 :
13392 : void
13393 988046 : vect_transform_stmt (vec_info *vinfo,
13394 : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
13395 : slp_tree slp_node, slp_instance slp_node_instance)
13396 : {
13397 988046 : bool done;
13398 :
13399 988046 : gcc_assert (slp_node);
13400 :
13401 988046 : if (stmt_info)
13402 987210 : STMT_VINFO_VECTYPE (stmt_info) = NULL_TREE;
13403 :
13404 988046 : switch (SLP_TREE_TYPE (slp_node))
13405 : {
13406 23568 : case type_demotion_vec_info_type:
13407 23568 : case type_promotion_vec_info_type:
13408 23568 : case type_conversion_vec_info_type:
13409 23568 : done = vectorizable_conversion (vinfo, stmt_info, gsi, slp_node, NULL);
13410 23568 : gcc_assert (done);
13411 : break;
13412 :
13413 16354 : case induc_vec_info_type:
13414 16354 : done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
13415 : stmt_info, slp_node, NULL);
13416 16354 : gcc_assert (done);
13417 : break;
13418 :
13419 8713 : case shift_vec_info_type:
13420 8713 : done = vectorizable_shift (vinfo, stmt_info, gsi, slp_node, NULL);
13421 8713 : gcc_assert (done);
13422 : break;
13423 :
13424 116574 : case op_vec_info_type:
13425 116574 : done = vectorizable_operation (vinfo, stmt_info, gsi, slp_node, NULL);
13426 116574 : gcc_assert (done);
13427 : break;
13428 :
13429 16365 : case assignment_vec_info_type:
13430 16365 : done = vectorizable_assignment (vinfo, stmt_info, gsi, slp_node, NULL);
13431 16365 : gcc_assert (done);
13432 : break;
13433 :
13434 169450 : case load_vec_info_type:
13435 169450 : done = vectorizable_load (vinfo, stmt_info, gsi, slp_node, NULL);
13436 169450 : gcc_assert (done);
13437 : break;
13438 :
13439 552828 : case store_vec_info_type:
13440 552828 : done = vectorizable_store (vinfo, stmt_info, gsi, slp_node, NULL);
13441 552828 : gcc_assert (done);
13442 : break;
13443 :
13444 8690 : case condition_vec_info_type:
13445 8690 : done = vectorizable_condition (vinfo, stmt_info, gsi, slp_node, NULL);
13446 8690 : gcc_assert (done);
13447 : break;
13448 :
13449 12703 : case comparison_vec_info_type:
13450 12703 : done = vectorizable_comparison (vinfo, stmt_info, gsi, slp_node, NULL);
13451 12703 : gcc_assert (done);
13452 : break;
13453 :
13454 4207 : case call_vec_info_type:
13455 4207 : done = vectorizable_call (vinfo, stmt_info, gsi, slp_node, NULL);
13456 4207 : break;
13457 :
13458 358 : case call_simd_clone_vec_info_type:
13459 358 : done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi,
13460 : slp_node, NULL);
13461 358 : break;
13462 :
13463 2665 : case reduc_vec_info_type:
13464 2665 : done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
13465 : gsi, slp_node);
13466 2665 : gcc_assert (done);
13467 : break;
13468 :
13469 23777 : case cycle_phi_info_type:
13470 23777 : done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
13471 : slp_node, slp_node_instance);
13472 23777 : gcc_assert (done);
13473 : break;
13474 :
13475 530 : case lc_phi_info_type:
13476 530 : done = vect_transform_lc_phi (as_a <loop_vec_info> (vinfo),
13477 : stmt_info, slp_node);
13478 530 : gcc_assert (done);
13479 : break;
13480 :
13481 45 : case recurr_info_type:
13482 45 : done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13483 : stmt_info, slp_node, NULL);
13484 45 : gcc_assert (done);
13485 : break;
13486 :
13487 14553 : case phi_info_type:
13488 14553 : done = vectorizable_phi (as_a <bb_vec_info> (vinfo),
13489 : stmt_info, slp_node, NULL);
13490 14553 : gcc_assert (done);
13491 : break;
13492 :
13493 0 : case loop_exit_ctrl_vec_info_type:
13494 0 : done = vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
13495 : stmt_info, gsi, slp_node, NULL);
13496 0 : gcc_assert (done);
13497 : break;
13498 :
13499 16666 : case permute_info_type:
13500 16666 : done = vectorizable_slp_permutation (vinfo, gsi, slp_node, NULL);
13501 16666 : gcc_assert (done);
13502 : break;
13503 :
13504 0 : default:
13505 0 : if (!STMT_VINFO_LIVE_P (stmt_info))
13506 : {
13507 0 : if (dump_enabled_p ())
13508 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13509 : "stmt not supported.\n");
13510 0 : gcc_unreachable ();
13511 : }
13512 988046 : done = true;
13513 : }
13514 :
13515 988046 : if (SLP_TREE_TYPE (slp_node) != store_vec_info_type
13516 435218 : && (!slp_node->ldst_lanes || SLP_TREE_PERMUTE_P (slp_node)))
13517 : {
13518 : /* Handle stmts whose DEF is used outside the loop-nest that is
13519 : being vectorized. */
13520 591017 : for (unsigned lane : SLP_TREE_LIVE_LANES (slp_node))
13521 : {
13522 64677 : stmt_vec_info slp_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[lane];
13523 64677 : done = vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
13524 : slp_node_instance, lane,
13525 : true, NULL);
13526 64677 : gcc_assert (done);
13527 : }
13528 : }
13529 988046 : }
13530 :
13531 :
13532 : /* Remove a group of stores (for SLP or interleaving), free their
13533 : stmt_vec_info. */
13534 :
13535 : void
13536 0 : vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
13537 : {
13538 0 : stmt_vec_info next_stmt_info = first_stmt_info;
13539 :
13540 0 : while (next_stmt_info)
13541 : {
13542 0 : stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
13543 0 : next_stmt_info = vect_orig_stmt (next_stmt_info);
13544 : /* Free the attached stmt_vec_info and remove the stmt. */
13545 0 : vinfo->remove_stmt (next_stmt_info);
13546 0 : next_stmt_info = tmp;
13547 : }
13548 0 : }
13549 :
13550 : /* If NUNITS is nonzero, return a vector type that contains NUNITS
13551 : elements of type SCALAR_TYPE, or null if the target doesn't support
13552 : such a type.
13553 :
13554 : If NUNITS is zero, return a vector type that contains elements of
13555 : type SCALAR_TYPE, choosing whichever vector size the target prefers.
13556 :
13557 : If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
13558 : for this vectorization region and want to "autodetect" the best choice.
13559 : Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
13560 : and we want the new type to be interoperable with it. PREVAILING_MODE
13561 : in this case can be a scalar integer mode or a vector mode; when it
13562 : is a vector mode, the function acts like a tree-level version of
13563 : related_vector_mode. */
13564 :
13565 : tree
13566 32546973 : get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
13567 : tree scalar_type, poly_uint64 nunits)
13568 : {
13569 32546973 : tree orig_scalar_type = scalar_type;
13570 32546973 : scalar_mode inner_mode;
13571 32546973 : machine_mode simd_mode;
13572 32546973 : tree vectype;
13573 :
13574 32546973 : if ((!INTEGRAL_TYPE_P (scalar_type)
13575 11166390 : && !POINTER_TYPE_P (scalar_type)
13576 2130871 : && !SCALAR_FLOAT_TYPE_P (scalar_type))
13577 43191259 : || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
13578 1608854 : && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
13579 525413 : return NULL_TREE;
13580 :
13581 32021560 : unsigned int nbytes = GET_MODE_SIZE (inner_mode);
13582 :
13583 : /* Interoperability between modes requires one to be a constant multiple
13584 : of the other, so that the number of vectors required for each operation
13585 : is a compile-time constant. */
13586 32021560 : if (prevailing_mode != VOIDmode
13587 30872432 : && !constant_multiple_p (nunits * nbytes,
13588 30872432 : GET_MODE_SIZE (prevailing_mode))
13589 33628819 : && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
13590 1607259 : nunits * nbytes))
13591 : return NULL_TREE;
13592 :
13593 : /* For vector types of elements whose mode precision doesn't
13594 : match their types precision we use a element type of mode
13595 : precision. The vectorization routines will have to make sure
13596 : they support the proper result truncation/extension.
13597 : We also make sure to build vector types with INTEGER_TYPE
13598 : component type only. */
13599 32021560 : if (INTEGRAL_TYPE_P (scalar_type)
13600 53402056 : && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
13601 19801813 : || TREE_CODE (scalar_type) != INTEGER_TYPE))
13602 1792225 : scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
13603 1792225 : TYPE_UNSIGNED (scalar_type));
13604 :
13605 : /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
13606 : When the component mode passes the above test simply use a type
13607 : corresponding to that mode. The theory is that any use that
13608 : would cause problems with this will disable vectorization anyway. */
13609 30229335 : else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
13610 : && !INTEGRAL_TYPE_P (scalar_type))
13611 9035519 : scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
13612 :
13613 : /* We can't build a vector type of elements with alignment bigger than
13614 : their size. */
13615 21193816 : else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
13616 406560 : scalar_type = lang_hooks.types.type_for_mode (inner_mode,
13617 203280 : TYPE_UNSIGNED (scalar_type));
13618 :
13619 : /* If we felt back to using the mode fail if there was
13620 : no scalar type for it. */
13621 32021560 : if (scalar_type == NULL_TREE)
13622 : return NULL_TREE;
13623 :
13624 : /* If no prevailing mode was supplied, use the mode the target prefers.
13625 : Otherwise lookup a vector mode based on the prevailing mode. */
13626 32021560 : if (prevailing_mode == VOIDmode)
13627 : {
13628 1149128 : gcc_assert (known_eq (nunits, 0U));
13629 1149128 : simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
13630 1149128 : if (SCALAR_INT_MODE_P (simd_mode))
13631 : {
13632 : /* Traditional behavior is not to take the integer mode
13633 : literally, but simply to use it as a way of determining
13634 : the vector size. It is up to mode_for_vector to decide
13635 : what the TYPE_MODE should be.
13636 :
13637 : Note that nunits == 1 is allowed in order to support single
13638 : element vector types. */
13639 65100 : if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
13640 648 : || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13641 31902 : return NULL_TREE;
13642 : }
13643 : }
13644 30872432 : else if (SCALAR_INT_MODE_P (prevailing_mode)
13645 30872432 : || !related_vector_mode (prevailing_mode,
13646 28582265 : inner_mode, nunits).exists (&simd_mode))
13647 : {
13648 : /* Fall back to using mode_for_vector, mostly in the hope of being
13649 : able to use an integer mode. */
13650 2290167 : if (known_eq (nunits, 0U)
13651 5362832 : && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
13652 : return NULL_TREE;
13653 :
13654 232994 : if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13655 222603 : return NULL_TREE;
13656 : }
13657 :
13658 29709882 : vectype = build_vector_type_for_mode (scalar_type, simd_mode);
13659 :
13660 : /* In cases where the mode was chosen by mode_for_vector, check that
13661 : the target actually supports the chosen mode, or that it at least
13662 : allows the vector mode to be replaced by a like-sized integer. */
13663 59419764 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
13664 29720542 : && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
13665 : return NULL_TREE;
13666 :
13667 : /* Re-attach the address-space qualifier if we canonicalized the scalar
13668 : type. */
13669 29701700 : if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
13670 5 : return build_qualified_type
13671 5 : (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
13672 :
13673 : return vectype;
13674 : }
13675 :
13676 : /* Function get_vectype_for_scalar_type.
13677 :
13678 : Returns the vector type corresponding to SCALAR_TYPE as supported
13679 : by the target. If GROUP_SIZE is nonzero and we're performing BB
13680 : vectorization, make sure that the number of elements in the vector
13681 : is no bigger than GROUP_SIZE. */
13682 :
13683 : tree
13684 27708922 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
13685 : unsigned int group_size)
13686 : {
13687 : /* For BB vectorization, we should always have a group size once we've
13688 : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
13689 : are tentative requests during things like early data reference
13690 : analysis and pattern recognition. */
13691 27708922 : if (is_a <bb_vec_info> (vinfo))
13692 24929797 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
13693 : else
13694 : group_size = 0;
13695 :
13696 27708922 : tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13697 : scalar_type);
13698 27708922 : if (vectype && vinfo->vector_mode == VOIDmode)
13699 1068811 : vinfo->vector_mode = TYPE_MODE (vectype);
13700 :
13701 : /* Register the natural choice of vector type, before the group size
13702 : has been applied. */
13703 0 : if (vectype)
13704 25035427 : vinfo->used_vector_modes.add (TYPE_MODE (vectype));
13705 :
13706 : /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
13707 : try again with an explicit number of elements. */
13708 25035427 : if (vectype
13709 25035427 : && group_size
13710 27708922 : && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
13711 : {
13712 : /* Start with the biggest number of units that fits within
13713 : GROUP_SIZE and halve it until we find a valid vector type.
13714 : Usually either the first attempt will succeed or all will
13715 : fail (in the latter case because GROUP_SIZE is too small
13716 : for the target), but it's possible that a target could have
13717 : a hole between supported vector types.
13718 :
13719 : If GROUP_SIZE is not a power of 2, this has the effect of
13720 : trying the largest power of 2 that fits within the group,
13721 : even though the group is not a multiple of that vector size.
13722 : The BB vectorizer will then try to carve up the group into
13723 : smaller pieces. */
13724 3305627 : unsigned int nunits = 1 << floor_log2 (group_size);
13725 3305627 : do
13726 : {
13727 3305627 : vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13728 3305627 : scalar_type, nunits);
13729 3305627 : nunits /= 2;
13730 : }
13731 3305627 : while (nunits > 1 && !vectype);
13732 : }
13733 :
13734 27708922 : return vectype;
13735 : }
13736 :
13737 : /* Return the vector type corresponding to SCALAR_TYPE as supported
13738 : by the target. NODE, if nonnull, is the SLP tree node that will
13739 : use the returned vector type. */
13740 :
13741 : tree
13742 185190 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
13743 : {
13744 185190 : unsigned int group_size = 0;
13745 185190 : if (node)
13746 185190 : group_size = SLP_TREE_LANES (node);
13747 185190 : return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13748 : }
13749 :
13750 : /* Function get_mask_type_for_scalar_type.
13751 :
13752 : Returns the mask type corresponding to a result of comparison
13753 : of vectors of specified SCALAR_TYPE as supported by target.
13754 : If GROUP_SIZE is nonzero and we're performing BB vectorization,
13755 : make sure that the number of elements in the vector is no bigger
13756 : than GROUP_SIZE. */
13757 :
13758 : tree
13759 1214029 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13760 : unsigned int group_size)
13761 : {
13762 1214029 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13763 :
13764 1214029 : if (!vectype)
13765 : return NULL;
13766 :
13767 1194183 : return truth_type_for (vectype);
13768 : }
13769 :
13770 : /* Function get_mask_type_for_scalar_type.
13771 :
13772 : Returns the mask type corresponding to a result of comparison
13773 : of vectors of specified SCALAR_TYPE as supported by target.
13774 : NODE, if nonnull, is the SLP tree node that will use the returned
13775 : vector type. */
13776 :
13777 : tree
13778 19 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13779 : slp_tree node)
13780 : {
13781 19 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
13782 :
13783 19 : if (!vectype)
13784 : return NULL;
13785 :
13786 19 : return truth_type_for (vectype);
13787 : }
13788 :
13789 : /* Function get_same_sized_vectype
13790 :
13791 : Returns a vector type corresponding to SCALAR_TYPE of size
13792 : VECTOR_TYPE if supported by the target. */
13793 :
13794 : tree
13795 165977 : get_same_sized_vectype (tree scalar_type, tree vector_type)
13796 : {
13797 165977 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
13798 0 : return truth_type_for (vector_type);
13799 :
13800 165977 : poly_uint64 nunits;
13801 331954 : if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
13802 331954 : GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
13803 : return NULL_TREE;
13804 :
13805 165977 : return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
13806 165977 : scalar_type, nunits);
13807 : }
13808 :
13809 : /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
13810 : would not change the chosen vector modes. */
13811 :
13812 : bool
13813 1645615 : vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
13814 : {
13815 1645615 : for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
13816 3771907 : i != vinfo->used_vector_modes.end (); ++i)
13817 1942858 : if (!VECTOR_MODE_P (*i)
13818 5828574 : || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
13819 879712 : return false;
13820 765903 : return true;
13821 : }
13822 :
13823 : /* Return true if replacing VECTOR_MODE with ALT_VECTOR_MODE would not
13824 : change the chosen vector modes for analysis of a loop. */
13825 :
13826 : bool
13827 390572 : vect_chooses_same_modes_p (machine_mode vector_mode,
13828 : machine_mode alt_vector_mode)
13829 : {
13830 63894 : return (VECTOR_MODE_P (vector_mode)
13831 390572 : && VECTOR_MODE_P (alt_vector_mode)
13832 781144 : && (related_vector_mode (vector_mode,
13833 : GET_MODE_INNER (alt_vector_mode))
13834 390572 : == alt_vector_mode)
13835 416960 : && (related_vector_mode (alt_vector_mode,
13836 : GET_MODE_INNER (vector_mode))
13837 13194 : == vector_mode));
13838 : }
13839 :
13840 : /* Function vect_is_simple_use.
13841 :
13842 : Input:
13843 : VINFO - the vect info of the loop or basic block that is being vectorized.
13844 : OPERAND - operand in the loop or bb.
13845 : Output:
13846 : DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
13847 : case OPERAND is an SSA_NAME that is defined in the vectorizable region
13848 : DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
13849 : the definition could be anywhere in the function
13850 : DT - the type of definition
13851 :
13852 : Returns whether a stmt with OPERAND can be vectorized.
13853 : For loops, supportable operands are constants, loop invariants, and operands
13854 : that are defined by the current iteration of the loop. Unsupportable
13855 : operands are those that are defined by a previous iteration of the loop (as
13856 : is the case in reduction/induction computations).
13857 : For basic blocks, supportable operands are constants and bb invariants.
13858 : For now, operands defined outside the basic block are not supported. */
13859 :
13860 : bool
13861 43537359 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
13862 : stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
13863 : {
13864 43537359 : if (def_stmt_info_out)
13865 41449160 : *def_stmt_info_out = NULL;
13866 43537359 : if (def_stmt_out)
13867 10016962 : *def_stmt_out = NULL;
13868 43537359 : *dt = vect_unknown_def_type;
13869 :
13870 43537359 : if (dump_enabled_p ())
13871 : {
13872 778212 : dump_printf_loc (MSG_NOTE, vect_location,
13873 : "vect_is_simple_use: operand ");
13874 778212 : if (TREE_CODE (operand) == SSA_NAME
13875 778212 : && !SSA_NAME_IS_DEFAULT_DEF (operand))
13876 713403 : dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
13877 : else
13878 64809 : dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
13879 : }
13880 :
13881 43537359 : if (CONSTANT_CLASS_P (operand))
13882 2982680 : *dt = vect_constant_def;
13883 40554679 : else if (is_gimple_min_invariant (operand))
13884 342032 : *dt = vect_external_def;
13885 40212647 : else if (TREE_CODE (operand) != SSA_NAME)
13886 991 : *dt = vect_unknown_def_type;
13887 40211656 : else if (SSA_NAME_IS_DEFAULT_DEF (operand))
13888 668815 : *dt = vect_external_def;
13889 : else
13890 : {
13891 39542841 : gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
13892 39542841 : stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
13893 39542841 : if (!stmt_vinfo)
13894 859766 : *dt = vect_external_def;
13895 : else
13896 : {
13897 38683075 : stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
13898 38683075 : def_stmt = stmt_vinfo->stmt;
13899 38683075 : *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
13900 38683075 : if (def_stmt_info_out)
13901 36603937 : *def_stmt_info_out = stmt_vinfo;
13902 : }
13903 39542841 : if (def_stmt_out)
13904 9797337 : *def_stmt_out = def_stmt;
13905 : }
13906 :
13907 43537359 : if (dump_enabled_p ())
13908 : {
13909 778212 : dump_printf (MSG_NOTE, ", type of def: ");
13910 778212 : switch (*dt)
13911 : {
13912 0 : case vect_uninitialized_def:
13913 0 : dump_printf (MSG_NOTE, "uninitialized\n");
13914 0 : break;
13915 53998 : case vect_constant_def:
13916 53998 : dump_printf (MSG_NOTE, "constant\n");
13917 53998 : break;
13918 26762 : case vect_external_def:
13919 26762 : dump_printf (MSG_NOTE, "external\n");
13920 26762 : break;
13921 556693 : case vect_internal_def:
13922 556693 : dump_printf (MSG_NOTE, "internal\n");
13923 556693 : break;
13924 109069 : case vect_induction_def:
13925 109069 : dump_printf (MSG_NOTE, "induction\n");
13926 109069 : break;
13927 28320 : case vect_reduction_def:
13928 28320 : dump_printf (MSG_NOTE, "reduction\n");
13929 28320 : break;
13930 482 : case vect_double_reduction_def:
13931 482 : dump_printf (MSG_NOTE, "double reduction\n");
13932 482 : break;
13933 2178 : case vect_nested_cycle:
13934 2178 : dump_printf (MSG_NOTE, "nested cycle\n");
13935 2178 : break;
13936 276 : case vect_first_order_recurrence:
13937 276 : dump_printf (MSG_NOTE, "first order recurrence\n");
13938 276 : break;
13939 0 : case vect_condition_def:
13940 0 : dump_printf (MSG_NOTE, "control flow\n");
13941 0 : break;
13942 434 : case vect_unknown_def_type:
13943 434 : dump_printf (MSG_NOTE, "unknown\n");
13944 434 : break;
13945 : }
13946 : }
13947 :
13948 43537359 : if (*dt == vect_unknown_def_type)
13949 : {
13950 58337 : if (dump_enabled_p ())
13951 434 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13952 : "Unsupported pattern.\n");
13953 58337 : return false;
13954 : }
13955 :
13956 : return true;
13957 : }
13958 :
13959 : /* Function vect_is_simple_use.
13960 :
13961 : Same as vect_is_simple_use but determines the operand by operand
13962 : position OPERAND from either STMT or SLP_NODE, filling in *OP
13963 : and *SLP_DEF (when SLP_NODE is not NULL). */
13964 :
13965 : bool
13966 3705373 : vect_is_simple_use (vec_info *vinfo, slp_tree slp_node,
13967 : unsigned operand, tree *op, slp_tree *slp_def,
13968 : enum vect_def_type *dt,
13969 : tree *vectype, stmt_vec_info *def_stmt_info_out)
13970 : {
13971 3705373 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
13972 3705373 : *slp_def = child;
13973 3705373 : *vectype = SLP_TREE_VECTYPE (child);
13974 3705373 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
13975 : {
13976 : /* ??? VEC_PERM nodes might be intermediate and their lane value
13977 : have no representative (nor do we build a VEC_PERM stmt for
13978 : the actual operation). Note for two-operator nodes we set
13979 : a representative but leave scalar stmts empty as we'd only
13980 : have one for a subset of lanes. Ideally no caller would
13981 : require *op for internal defs. */
13982 2019880 : if (SLP_TREE_REPRESENTATIVE (child))
13983 : {
13984 2019103 : *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
13985 2019103 : return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
13986 : }
13987 : else
13988 : {
13989 777 : gcc_assert (SLP_TREE_PERMUTE_P (child));
13990 777 : *op = error_mark_node;
13991 777 : *dt = vect_internal_def;
13992 777 : if (def_stmt_info_out)
13993 0 : *def_stmt_info_out = NULL;
13994 777 : return true;
13995 : }
13996 : }
13997 : else
13998 : {
13999 1685493 : if (def_stmt_info_out)
14000 52767 : *def_stmt_info_out = NULL;
14001 1685493 : *op = SLP_TREE_SCALAR_OPS (child)[0];
14002 1685493 : *dt = SLP_TREE_DEF_TYPE (child);
14003 1685493 : return true;
14004 : }
14005 : }
14006 :
14007 : /* If OP is not NULL and is external or constant update its vector
14008 : type with VECTYPE. Returns true if successful or false if not,
14009 : for example when conflicting vector types are present. */
14010 :
14011 : bool
14012 3224043 : vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
14013 : {
14014 3224043 : if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
14015 : return true;
14016 1111713 : if (SLP_TREE_VECTYPE (op))
14017 23540 : return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
14018 : /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
14019 : should be handled by patters. Allow vect_constant_def for now
14020 : as well as the trivial single-lane uniform vect_external_def case
14021 : both of which we code-generate reasonably. */
14022 1088173 : if (VECTOR_BOOLEAN_TYPE_P (vectype)
14023 1708 : && SLP_TREE_DEF_TYPE (op) == vect_external_def
14024 1089406 : && SLP_TREE_LANES (op) > 1)
14025 : return false;
14026 1087996 : SLP_TREE_VECTYPE (op) = vectype;
14027 1087996 : return true;
14028 : }
14029 :
14030 : /* Function supportable_widening_operation
14031 :
14032 : Check whether an operation represented by the code CODE is a
14033 : widening operation that is supported by the target platform in
14034 : vector form (i.e., when operating on arguments of type VECTYPE_IN
14035 : producing a result of type VECTYPE_OUT).
14036 :
14037 : Widening operations we currently support are NOP (CONVERT), FLOAT,
14038 : FIX_TRUNC and WIDEN_MULT. This function checks if these operations
14039 : are supported by the target platform either directly (via vector
14040 : tree-codes), or via target builtins.
14041 :
14042 : When EVENODD_OK then also lane-swizzling operations are considered.
14043 :
14044 : Output:
14045 : - CODE1 and CODE2 are codes of vector operations to be used when
14046 : vectorizing the operation, if available.
14047 : - MULTI_STEP_CVT determines the number of required intermediate steps in
14048 : case of multi-step conversion (like char->short->int - in that case
14049 : MULTI_STEP_CVT will be 1).
14050 : - INTERM_TYPES contains the intermediate type required to perform the
14051 : widening operation (short in the above example). */
14052 :
14053 : bool
14054 509857 : supportable_widening_operation (code_helper code,
14055 : tree vectype_out, tree vectype_in,
14056 : bool evenodd_ok,
14057 : code_helper *code1,
14058 : code_helper *code2,
14059 : int *multi_step_cvt,
14060 : vec<tree> *interm_types)
14061 : {
14062 509857 : machine_mode vec_mode;
14063 509857 : enum insn_code icode1, icode2;
14064 509857 : optab optab1 = unknown_optab, optab2 = unknown_optab;
14065 509857 : tree vectype = vectype_in;
14066 509857 : tree wide_vectype = vectype_out;
14067 509857 : tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
14068 509857 : int i;
14069 509857 : tree prev_type, intermediate_type;
14070 509857 : machine_mode intermediate_mode, prev_mode;
14071 509857 : optab optab3, optab4;
14072 :
14073 509857 : *multi_step_cvt = 0;
14074 :
14075 509857 : switch (code.safe_as_tree_code ())
14076 : {
14077 : case MAX_TREE_CODES:
14078 : /* Don't set c1 and c2 if code is not a tree_code. */
14079 : break;
14080 :
14081 197246 : case WIDEN_MULT_EXPR:
14082 : /* The result of a vectorized widening operation usually requires
14083 : two vectors (because the widened results do not fit into one vector).
14084 : The generated vector results would normally be expected to be
14085 : generated in the same order as in the original scalar computation,
14086 : i.e. if 8 results are generated in each vector iteration, they are
14087 : to be organized as follows:
14088 : vect1: [res1,res2,res3,res4],
14089 : vect2: [res5,res6,res7,res8].
14090 :
14091 : However, in the special case that the result of the widening
14092 : operation is used in a reduction computation only, the order doesn't
14093 : matter (because when vectorizing a reduction we change the order of
14094 : the computation). Some targets can take advantage of this and
14095 : generate more efficient code. For example, targets like Altivec,
14096 : that support widen_mult using a sequence of {mult_even,mult_odd}
14097 : generate the following vectors:
14098 : vect1: [res1,res3,res5,res7],
14099 : vect2: [res2,res4,res6,res8].
14100 :
14101 : When vectorizing outer-loops, we execute the inner-loop sequentially
14102 : (each vectorized inner-loop iteration contributes to VF outer-loop
14103 : iterations in parallel). We therefore don't allow to change the
14104 : order of the computation in the inner-loop during outer-loop
14105 : vectorization. */
14106 : /* TODO: Another case in which order doesn't *really* matter is when we
14107 : widen and then contract again, e.g. (short)((int)x * y >> 8).
14108 : Normally, pack_trunc performs an even/odd permute, whereas the
14109 : repack from an even/odd expansion would be an interleave, which
14110 : would be significantly simpler for e.g. AVX2. */
14111 : /* In any case, in order to avoid duplicating the code below, recurse
14112 : on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
14113 : are properly set up for the caller. If we fail, we'll continue with
14114 : a VEC_WIDEN_MULT_LO/HI_EXPR check. */
14115 197246 : if (evenodd_ok
14116 197246 : && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
14117 : vectype_out, vectype_in,
14118 : evenodd_ok, code1,
14119 : code2, multi_step_cvt,
14120 : interm_types))
14121 104442 : return true;
14122 : c1 = VEC_WIDEN_MULT_LO_EXPR;
14123 : c2 = VEC_WIDEN_MULT_HI_EXPR;
14124 : break;
14125 :
14126 : case DOT_PROD_EXPR:
14127 405415 : c1 = DOT_PROD_EXPR;
14128 405415 : c2 = DOT_PROD_EXPR;
14129 : break;
14130 :
14131 0 : case SAD_EXPR:
14132 0 : c1 = SAD_EXPR;
14133 0 : c2 = SAD_EXPR;
14134 0 : break;
14135 :
14136 195185 : case VEC_WIDEN_MULT_EVEN_EXPR:
14137 : /* Support the recursion induced just above. */
14138 195185 : c1 = VEC_WIDEN_MULT_EVEN_EXPR;
14139 195185 : c2 = VEC_WIDEN_MULT_ODD_EXPR;
14140 195185 : break;
14141 :
14142 9660 : case WIDEN_LSHIFT_EXPR:
14143 9660 : c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
14144 9660 : c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
14145 9660 : break;
14146 :
14147 45934 : CASE_CONVERT:
14148 45934 : c1 = VEC_UNPACK_LO_EXPR;
14149 45934 : c2 = VEC_UNPACK_HI_EXPR;
14150 45934 : break;
14151 :
14152 9219 : case FLOAT_EXPR:
14153 9219 : c1 = VEC_UNPACK_FLOAT_LO_EXPR;
14154 9219 : c2 = VEC_UNPACK_FLOAT_HI_EXPR;
14155 9219 : break;
14156 :
14157 127 : case FIX_TRUNC_EXPR:
14158 127 : c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
14159 127 : c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
14160 127 : break;
14161 :
14162 0 : default:
14163 0 : gcc_unreachable ();
14164 : }
14165 :
14166 405415 : if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
14167 : std::swap (c1, c2);
14168 :
14169 405415 : if (code == FIX_TRUNC_EXPR)
14170 : {
14171 : /* The signedness is determined from output operand. */
14172 127 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14173 127 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
14174 : }
14175 720483 : else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
14176 45934 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14177 7949 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14178 7949 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
14179 353266 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
14180 : {
14181 : /* If the input and result modes are the same, a different optab
14182 : is needed where we pass in the number of units in vectype. */
14183 : optab1 = vec_unpacks_sbool_lo_optab;
14184 : optab2 = vec_unpacks_sbool_hi_optab;
14185 : }
14186 :
14187 405415 : vec_mode = TYPE_MODE (vectype);
14188 405415 : if (widening_fn_p (code))
14189 : {
14190 : /* If this is an internal fn then we must check whether the target
14191 : supports either a low-high split or an even-odd split. */
14192 52486 : internal_fn ifn = as_internal_fn ((combined_fn) code);
14193 :
14194 52486 : internal_fn lo, hi, even, odd;
14195 52486 : lookup_hilo_internal_fn (ifn, &lo, &hi);
14196 52486 : if (BYTES_BIG_ENDIAN)
14197 : std::swap (lo, hi);
14198 52486 : *code1 = as_combined_fn (lo);
14199 52486 : *code2 = as_combined_fn (hi);
14200 52486 : optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
14201 52486 : optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
14202 :
14203 : /* If we don't support low-high, then check for even-odd. */
14204 52486 : if (!optab1
14205 52486 : || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
14206 0 : || !optab2
14207 52486 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
14208 : {
14209 52486 : lookup_evenodd_internal_fn (ifn, &even, &odd);
14210 52486 : *code1 = as_combined_fn (even);
14211 52486 : *code2 = as_combined_fn (odd);
14212 52486 : optab1 = direct_internal_fn_optab (even, {vectype, vectype});
14213 52486 : optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
14214 : }
14215 : }
14216 352929 : else if (code.is_tree_code ())
14217 : {
14218 352929 : if (code == FIX_TRUNC_EXPR)
14219 : {
14220 : /* The signedness is determined from output operand. */
14221 127 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14222 127 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
14223 : }
14224 352802 : else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
14225 45934 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14226 7949 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14227 7949 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
14228 353266 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
14229 : {
14230 : /* If the input and result modes are the same, a different optab
14231 : is needed where we pass in the number of units in vectype. */
14232 : optab1 = vec_unpacks_sbool_lo_optab;
14233 : optab2 = vec_unpacks_sbool_hi_optab;
14234 : }
14235 : else
14236 : {
14237 352338 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14238 352338 : optab2 = optab_for_tree_code (c2, vectype, optab_default);
14239 : }
14240 352929 : *code1 = c1;
14241 352929 : *code2 = c2;
14242 : }
14243 :
14244 405415 : if (!optab1 || !optab2)
14245 : return false;
14246 :
14247 405415 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
14248 405415 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
14249 241126 : return false;
14250 :
14251 :
14252 164289 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14253 164289 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14254 : {
14255 152640 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14256 : return true;
14257 : /* For scalar masks we may have different boolean
14258 : vector types having the same QImode. Thus we
14259 : add additional check for elements number. */
14260 4236 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
14261 : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14262 : return true;
14263 : }
14264 :
14265 : /* Check if it's a multi-step conversion that can be done using intermediate
14266 : types. */
14267 :
14268 11854 : prev_type = vectype;
14269 11854 : prev_mode = vec_mode;
14270 :
14271 253376 : if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
14272 : return false;
14273 :
14274 : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14275 : intermediate steps in promotion sequence. We try
14276 : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
14277 : not. */
14278 11802 : interm_types->create (MAX_INTERM_CVT_STEPS);
14279 13059 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14280 : {
14281 13059 : intermediate_mode = insn_data[icode1].operand[0].mode;
14282 13059 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14283 4571 : intermediate_type
14284 4571 : = vect_halve_mask_nunits (prev_type, intermediate_mode);
14285 8488 : else if (VECTOR_MODE_P (intermediate_mode))
14286 : {
14287 8488 : tree intermediate_element_type
14288 8488 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
14289 8488 : TYPE_UNSIGNED (prev_type));
14290 8488 : intermediate_type
14291 8488 : = build_vector_type_for_mode (intermediate_element_type,
14292 : intermediate_mode);
14293 8488 : }
14294 : else
14295 0 : intermediate_type
14296 0 : = lang_hooks.types.type_for_mode (intermediate_mode,
14297 0 : TYPE_UNSIGNED (prev_type));
14298 :
14299 13059 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14300 4571 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14301 4571 : && intermediate_mode == TYPE_MODE (wide_vectype)
14302 13330 : && SCALAR_INT_MODE_P (intermediate_mode))
14303 : {
14304 : /* If the input and result modes are the same, a different optab
14305 : is needed where we pass in the number of units in vectype. */
14306 : optab3 = vec_unpacks_sbool_lo_optab;
14307 : optab4 = vec_unpacks_sbool_hi_optab;
14308 : }
14309 : else
14310 : {
14311 12788 : optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
14312 12788 : optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
14313 : }
14314 :
14315 13059 : if (!optab3 || !optab4
14316 13059 : || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
14317 13027 : || insn_data[icode1].operand[0].mode != intermediate_mode
14318 13027 : || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
14319 13027 : || insn_data[icode2].operand[0].mode != intermediate_mode
14320 13027 : || ((icode1 = optab_handler (optab3, intermediate_mode))
14321 : == CODE_FOR_nothing)
14322 25722 : || ((icode2 = optab_handler (optab4, intermediate_mode))
14323 : == CODE_FOR_nothing))
14324 : break;
14325 :
14326 12663 : interm_types->quick_push (intermediate_type);
14327 12663 : (*multi_step_cvt)++;
14328 :
14329 12663 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14330 12663 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14331 : {
14332 11472 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14333 : return true;
14334 3701 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
14335 : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14336 : return true;
14337 : }
14338 :
14339 1257 : prev_type = intermediate_type;
14340 1257 : prev_mode = intermediate_mode;
14341 : }
14342 :
14343 396 : interm_types->release ();
14344 396 : return false;
14345 : }
14346 :
14347 :
14348 : /* Function supportable_narrowing_operation
14349 :
14350 : Check whether an operation represented by the code CODE is a
14351 : narrowing operation that is supported by the target platform in
14352 : vector form (i.e., when operating on arguments of type VECTYPE_IN
14353 : and producing a result of type VECTYPE_OUT).
14354 :
14355 : Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
14356 : and FLOAT. This function checks if these operations are supported by
14357 : the target platform directly via vector tree-codes.
14358 :
14359 : Output:
14360 : - CODE1 is the code of a vector operation to be used when
14361 : vectorizing the operation, if available.
14362 : - MULTI_STEP_CVT determines the number of required intermediate steps in
14363 : case of multi-step conversion (like int->short->char - in that case
14364 : MULTI_STEP_CVT will be 1).
14365 : - INTERM_TYPES contains the intermediate type required to perform the
14366 : narrowing operation (short in the above example). */
14367 :
14368 : bool
14369 43750 : supportable_narrowing_operation (code_helper code,
14370 : tree vectype_out, tree vectype_in,
14371 : code_helper *code1, int *multi_step_cvt,
14372 : vec<tree> *interm_types)
14373 : {
14374 43750 : machine_mode vec_mode;
14375 43750 : enum insn_code icode1;
14376 43750 : optab optab1, interm_optab;
14377 43750 : tree vectype = vectype_in;
14378 43750 : tree narrow_vectype = vectype_out;
14379 43750 : enum tree_code c1;
14380 43750 : tree intermediate_type, prev_type;
14381 43750 : machine_mode intermediate_mode, prev_mode;
14382 43750 : int i;
14383 43750 : unsigned HOST_WIDE_INT n_elts;
14384 43750 : bool uns;
14385 :
14386 43750 : if (!code.is_tree_code ())
14387 : return false;
14388 :
14389 43750 : *multi_step_cvt = 0;
14390 43750 : switch ((tree_code) code)
14391 : {
14392 42242 : CASE_CONVERT:
14393 42242 : c1 = VEC_PACK_TRUNC_EXPR;
14394 42242 : if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
14395 11564 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14396 11564 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
14397 5262 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
14398 47504 : && n_elts < BITS_PER_UNIT)
14399 : optab1 = vec_pack_sbool_trunc_optab;
14400 : else
14401 39757 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14402 : break;
14403 :
14404 570 : case FIX_TRUNC_EXPR:
14405 570 : c1 = VEC_PACK_FIX_TRUNC_EXPR;
14406 : /* The signedness is determined from output operand. */
14407 570 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14408 570 : break;
14409 :
14410 938 : case FLOAT_EXPR:
14411 938 : c1 = VEC_PACK_FLOAT_EXPR;
14412 938 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14413 938 : break;
14414 :
14415 0 : default:
14416 0 : gcc_unreachable ();
14417 : }
14418 :
14419 43750 : if (!optab1)
14420 : return false;
14421 :
14422 43750 : vec_mode = TYPE_MODE (vectype);
14423 43750 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
14424 : return false;
14425 :
14426 38280 : *code1 = c1;
14427 :
14428 38280 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14429 : {
14430 23856 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14431 : return true;
14432 : /* For scalar masks we may have different boolean
14433 : vector types having the same QImode. Thus we
14434 : add additional check for elements number. */
14435 5799 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
14436 : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14437 : return true;
14438 : }
14439 :
14440 14575 : if (code == FLOAT_EXPR)
14441 : return false;
14442 :
14443 : /* Check if it's a multi-step conversion that can be done using intermediate
14444 : types. */
14445 14575 : prev_mode = vec_mode;
14446 14575 : prev_type = vectype;
14447 14575 : if (code == FIX_TRUNC_EXPR)
14448 94 : uns = TYPE_UNSIGNED (vectype_out);
14449 : else
14450 14481 : uns = TYPE_UNSIGNED (vectype);
14451 :
14452 : /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
14453 : conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
14454 : costly than signed. */
14455 14575 : if (code == FIX_TRUNC_EXPR && uns)
14456 : {
14457 28 : enum insn_code icode2;
14458 :
14459 28 : intermediate_type
14460 28 : = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
14461 28 : interm_optab
14462 28 : = optab_for_tree_code (c1, intermediate_type, optab_default);
14463 28 : if (interm_optab != unknown_optab
14464 28 : && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
14465 28 : && insn_data[icode1].operand[0].mode
14466 28 : == insn_data[icode2].operand[0].mode)
14467 : {
14468 : uns = false;
14469 : optab1 = interm_optab;
14470 : icode1 = icode2;
14471 : }
14472 : }
14473 :
14474 : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14475 : intermediate steps in promotion sequence. We try
14476 : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
14477 14575 : interm_types->create (MAX_INTERM_CVT_STEPS);
14478 31234 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14479 : {
14480 16659 : intermediate_mode = insn_data[icode1].operand[0].mode;
14481 16659 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14482 7004 : intermediate_type
14483 7004 : = vect_double_mask_nunits (prev_type, intermediate_mode);
14484 : else
14485 9655 : intermediate_type
14486 9655 : = lang_hooks.types.type_for_mode (intermediate_mode, uns);
14487 16659 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14488 7004 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
14489 7004 : && SCALAR_INT_MODE_P (prev_mode)
14490 3134 : && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
14491 19793 : && n_elts < BITS_PER_UNIT)
14492 : interm_optab = vec_pack_sbool_trunc_optab;
14493 : else
14494 16305 : interm_optab
14495 16305 : = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
14496 : optab_default);
14497 354 : if (!interm_optab
14498 16659 : || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
14499 16659 : || insn_data[icode1].operand[0].mode != intermediate_mode
14500 32964 : || ((icode1 = optab_handler (interm_optab, intermediate_mode))
14501 : == CODE_FOR_nothing))
14502 : break;
14503 :
14504 15744 : interm_types->quick_push (intermediate_type);
14505 15744 : (*multi_step_cvt)++;
14506 :
14507 15744 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14508 : {
14509 13660 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14510 : return true;
14511 4930 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
14512 : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14513 : return true;
14514 : }
14515 :
14516 2084 : prev_mode = intermediate_mode;
14517 2084 : prev_type = intermediate_type;
14518 2084 : optab1 = interm_optab;
14519 : }
14520 :
14521 915 : interm_types->release ();
14522 915 : return false;
14523 : }
14524 :
14525 : /* Function supportable_indirect_convert_operation
14526 :
14527 : Check whether an operation represented by the code CODE is single or multi
14528 : operations that are supported by the target platform in
14529 : vector form (i.e., when operating on arguments of type VECTYPE_IN
14530 : producing a result of type VECTYPE_OUT).
14531 :
14532 : Convert operations we currently support directly are FIX_TRUNC and FLOAT.
14533 : This function checks if these operations are supported
14534 : by the target platform directly (via vector tree-codes).
14535 :
14536 : Output:
14537 : - converts contains some pairs to perform the convert operation,
14538 : the pair's first is the intermediate type, and its second is the code of
14539 : a vector operation to be used when converting the operation from the
14540 : previous type to the intermediate type. */
14541 : bool
14542 92611 : supportable_indirect_convert_operation (code_helper code,
14543 : tree vectype_out,
14544 : tree vectype_in,
14545 : vec<std::pair<tree, tree_code> > &converts,
14546 : tree op0, slp_tree slp_op0)
14547 : {
14548 92611 : bool found_mode = false;
14549 92611 : scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
14550 92611 : scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
14551 92611 : tree_code tc1, tc2, code1, code2;
14552 :
14553 92611 : tree cvt_type = NULL_TREE;
14554 92611 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
14555 :
14556 92611 : if (supportable_convert_operation ((tree_code) code,
14557 : vectype_out,
14558 : vectype_in,
14559 : &tc1))
14560 : {
14561 21695 : converts.safe_push (std::make_pair (vectype_out, tc1));
14562 21695 : return true;
14563 : }
14564 :
14565 : /* For conversions between float and integer types try whether
14566 : we can use intermediate signed integer types to support the
14567 : conversion. */
14568 141832 : if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
14569 70916 : && (code == FLOAT_EXPR
14570 4845 : || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
14571 : {
14572 1242 : bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
14573 621 : bool float_expr_p = code == FLOAT_EXPR;
14574 621 : unsigned short target_size;
14575 621 : scalar_mode intermediate_mode;
14576 621 : if (demotion)
14577 : {
14578 99 : intermediate_mode = lhs_mode;
14579 99 : target_size = GET_MODE_SIZE (rhs_mode);
14580 : }
14581 : else
14582 : {
14583 522 : target_size = GET_MODE_SIZE (lhs_mode);
14584 522 : if (!int_mode_for_size
14585 522 : (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
14586 142 : return false;
14587 : }
14588 621 : code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
14589 : code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
14590 621 : opt_scalar_mode mode_iter;
14591 1820 : FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
14592 : {
14593 1820 : intermediate_mode = mode_iter.require ();
14594 :
14595 3640 : if (GET_MODE_SIZE (intermediate_mode) > target_size)
14596 : break;
14597 :
14598 1394 : scalar_mode cvt_mode;
14599 1394 : if (!int_mode_for_size
14600 1394 : (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
14601 : break;
14602 :
14603 1364 : cvt_type = build_nonstandard_integer_type
14604 1364 : (GET_MODE_BITSIZE (cvt_mode), 0);
14605 :
14606 : /* Check if the intermediate type can hold OP0's range.
14607 : When converting from float to integer this is not necessary
14608 : because values that do not fit the (smaller) target type are
14609 : unspecified anyway. */
14610 1364 : if (demotion && float_expr_p)
14611 : {
14612 23 : wide_int op_min_value, op_max_value;
14613 : /* For vector form, it looks like op0 doesn't have RANGE_INFO.
14614 : In the future, if it is supported, changes may need to be made
14615 : to this part, such as checking the RANGE of each element
14616 : in the vector. */
14617 23 : if (slp_op0)
14618 : {
14619 13 : tree def;
14620 : /* ??? Merge ranges in case of more than one lane. */
14621 13 : if (SLP_TREE_LANES (slp_op0) != 1
14622 0 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
14623 13 : || !vect_get_range_info (def,
14624 : &op_min_value, &op_max_value))
14625 : break;
14626 : }
14627 10 : else if (!op0
14628 0 : || TREE_CODE (op0) != SSA_NAME
14629 0 : || !SSA_NAME_RANGE_INFO (op0)
14630 10 : || !vect_get_range_info (op0, &op_min_value,
14631 : &op_max_value))
14632 : break;
14633 :
14634 0 : if (cvt_type == NULL_TREE
14635 0 : || (wi::min_precision (op_max_value, SIGNED)
14636 0 : > TYPE_PRECISION (cvt_type))
14637 0 : || (wi::min_precision (op_min_value, SIGNED)
14638 0 : > TYPE_PRECISION (cvt_type)))
14639 0 : continue;
14640 23 : }
14641 :
14642 1341 : cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
14643 : cvt_type,
14644 : nelts);
14645 : /* This should only happened for SLP as long as loop vectorizer
14646 : only supports same-sized vector. */
14647 2540 : if (cvt_type == NULL_TREE
14648 1483 : || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
14649 1341 : || !supportable_convert_operation ((tree_code) code1,
14650 : vectype_out,
14651 : cvt_type, &tc1)
14652 1901 : || !supportable_convert_operation ((tree_code) code2,
14653 : cvt_type,
14654 : vectype_in, &tc2))
14655 1199 : continue;
14656 :
14657 : found_mode = true;
14658 : break;
14659 : }
14660 :
14661 621 : if (found_mode)
14662 : {
14663 142 : converts.safe_push (std::make_pair (cvt_type, tc2));
14664 142 : if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
14665 142 : converts.safe_push (std::make_pair (vectype_out, tc1));
14666 142 : return true;
14667 : }
14668 : }
14669 : return false;
14670 : }
14671 :
14672 : /* Generate and return a vector mask of MASK_TYPE such that
14673 : mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
14674 : Add the statements to SEQ. */
14675 :
14676 : tree
14677 0 : vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
14678 : tree end_index, const char *name)
14679 : {
14680 0 : tree cmp_type = TREE_TYPE (start_index);
14681 0 : gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
14682 : cmp_type, mask_type,
14683 : OPTIMIZE_FOR_SPEED));
14684 0 : gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
14685 : start_index, end_index,
14686 : build_zero_cst (mask_type));
14687 0 : tree tmp;
14688 0 : if (name)
14689 0 : tmp = make_temp_ssa_name (mask_type, NULL, name);
14690 : else
14691 0 : tmp = make_ssa_name (mask_type);
14692 0 : gimple_call_set_lhs (call, tmp);
14693 0 : gimple_seq_add_stmt (seq, call);
14694 0 : return tmp;
14695 : }
14696 :
14697 : /* Generate a vector mask of type MASK_TYPE for which index I is false iff
14698 : J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
14699 :
14700 : tree
14701 0 : vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
14702 : tree end_index)
14703 : {
14704 0 : tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
14705 0 : return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
14706 : }
14707 :
14708 : /* Try to compute the vector types required to vectorize STMT_INFO,
14709 : returning true on success and false if vectorization isn't possible.
14710 : If GROUP_SIZE is nonzero and we're performing BB vectorization,
14711 : take sure that the number of elements in the vectors is no bigger
14712 : than GROUP_SIZE.
14713 :
14714 : On success:
14715 :
14716 : - Set *STMT_VECTYPE_OUT to:
14717 : - NULL_TREE if the statement doesn't need to be vectorized;
14718 : - the equivalent of STMT_VINFO_VECTYPE otherwise.
14719 :
14720 : - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
14721 : number of units needed to vectorize STMT_INFO, or NULL_TREE if the
14722 : statement does not help to determine the overall number of units. */
14723 :
14724 : opt_result
14725 6204224 : vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
14726 : tree *stmt_vectype_out,
14727 : tree *nunits_vectype_out,
14728 : unsigned int group_size)
14729 : {
14730 6204224 : gimple *stmt = stmt_info->stmt;
14731 :
14732 : /* For BB vectorization, we should always have a group size once we've
14733 : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
14734 : are tentative requests during things like early data reference
14735 : analysis and pattern recognition. */
14736 6204224 : if (is_a <bb_vec_info> (vinfo))
14737 5035711 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
14738 : else
14739 : group_size = 0;
14740 :
14741 6204224 : *stmt_vectype_out = NULL_TREE;
14742 6204224 : *nunits_vectype_out = NULL_TREE;
14743 :
14744 6204224 : if (gimple_get_lhs (stmt) == NULL_TREE
14745 : /* Allow vector conditionals through here. */
14746 2610 : && !is_a <gcond *> (stmt)
14747 : /* MASK_STORE and friends have no lhs, but are ok. */
14748 6209424 : && !(is_gimple_call (stmt)
14749 2610 : && gimple_call_internal_p (stmt)
14750 2590 : && internal_store_fn_p (gimple_call_internal_fn (stmt))))
14751 : {
14752 20 : if (is_a <gcall *> (stmt))
14753 : {
14754 : /* Ignore calls with no lhs. These must be calls to
14755 : #pragma omp simd functions, and what vectorization factor
14756 : it really needs can't be determined until
14757 : vectorizable_simd_clone_call. */
14758 20 : if (dump_enabled_p ())
14759 18 : dump_printf_loc (MSG_NOTE, vect_location,
14760 : "defer to SIMD clone analysis.\n");
14761 20 : return opt_result::success ();
14762 : }
14763 :
14764 0 : return opt_result::failure_at (stmt,
14765 : "not vectorized: irregular stmt: %G", stmt);
14766 : }
14767 :
14768 6204204 : tree vectype;
14769 6204204 : tree scalar_type = NULL_TREE;
14770 6204204 : if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
14771 : {
14772 1641311 : vectype = STMT_VINFO_VECTYPE (stmt_info);
14773 1641311 : if (dump_enabled_p ())
14774 80906 : dump_printf_loc (MSG_NOTE, vect_location,
14775 : "precomputed vectype: %T\n", vectype);
14776 : }
14777 4562893 : else if (vect_use_mask_type_p (stmt_info))
14778 : {
14779 233664 : unsigned int precision = stmt_info->mask_precision;
14780 233664 : scalar_type = build_nonstandard_integer_type (precision, 1);
14781 233664 : vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
14782 233664 : if (!vectype)
14783 0 : return opt_result::failure_at (stmt, "not vectorized: unsupported"
14784 : " data-type %T\n", scalar_type);
14785 233664 : if (dump_enabled_p ())
14786 4749 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14787 : }
14788 : else
14789 : {
14790 : /* If we got here with a gcond it means that the target had no available vector
14791 : mode for the scalar type. We can't vectorize so abort. */
14792 4329229 : if (is_a <gcond *> (stmt))
14793 0 : return opt_result::failure_at (stmt,
14794 : "not vectorized:"
14795 : " unsupported data-type for gcond %T\n",
14796 : scalar_type);
14797 :
14798 4329229 : if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
14799 1538374 : scalar_type = TREE_TYPE (DR_REF (dr));
14800 : else
14801 2790855 : scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
14802 :
14803 4329229 : if (dump_enabled_p ())
14804 : {
14805 64440 : if (group_size)
14806 8208 : dump_printf_loc (MSG_NOTE, vect_location,
14807 : "get vectype for scalar type (group size %d):"
14808 : " %T\n", group_size, scalar_type);
14809 : else
14810 56232 : dump_printf_loc (MSG_NOTE, vect_location,
14811 : "get vectype for scalar type: %T\n", scalar_type);
14812 : }
14813 4329229 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14814 4329229 : if (!vectype)
14815 261188 : return opt_result::failure_at (stmt,
14816 : "not vectorized:"
14817 : " unsupported data-type %T\n",
14818 : scalar_type);
14819 :
14820 4068041 : if (dump_enabled_p ())
14821 64184 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14822 : }
14823 :
14824 4382611 : if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
14825 0 : return opt_result::failure_at (stmt,
14826 : "not vectorized: vector stmt in loop:%G",
14827 : stmt);
14828 :
14829 5943016 : *stmt_vectype_out = vectype;
14830 :
14831 : /* Don't try to compute scalar types if the stmt produces a boolean
14832 : vector; use the existing vector type instead. */
14833 5943016 : tree nunits_vectype = vectype;
14834 5943016 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14835 : {
14836 : /* The number of units is set according to the smallest scalar
14837 : type (or the largest vector size, but we only support one
14838 : vector size per vectorization). */
14839 5372938 : scalar_type = vect_get_smallest_scalar_type (stmt_info,
14840 5372938 : TREE_TYPE (vectype));
14841 5372938 : if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
14842 : {
14843 1047506 : if (dump_enabled_p ())
14844 8867 : dump_printf_loc (MSG_NOTE, vect_location,
14845 : "get vectype for smallest scalar type: %T\n",
14846 : scalar_type);
14847 1047506 : nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
14848 : group_size);
14849 1047506 : if (!nunits_vectype)
14850 16 : return opt_result::failure_at
14851 16 : (stmt, "not vectorized: unsupported data-type %T\n",
14852 : scalar_type);
14853 1047490 : if (dump_enabled_p ())
14854 8867 : dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
14855 : nunits_vectype);
14856 : }
14857 : }
14858 :
14859 5943000 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
14860 5943000 : TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
14861 0 : return opt_result::failure_at (stmt,
14862 : "Not vectorized: Incompatible number "
14863 : "of vector subparts between %T and %T\n",
14864 : nunits_vectype, *stmt_vectype_out);
14865 :
14866 5943000 : if (dump_enabled_p ())
14867 : {
14868 149839 : dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
14869 149839 : dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
14870 149839 : dump_printf (MSG_NOTE, "\n");
14871 : }
14872 :
14873 5943000 : *nunits_vectype_out = nunits_vectype;
14874 5943000 : return opt_result::success ();
14875 : }
14876 :
14877 : /* Generate and return statement sequence that sets vector length LEN that is:
14878 :
14879 : min_of_start_and_end = min (START_INDEX, END_INDEX);
14880 : left_len = END_INDEX - min_of_start_and_end;
14881 : rhs = min (left_len, LEN_LIMIT);
14882 : LEN = rhs;
14883 :
14884 : Note: the cost of the code generated by this function is modeled
14885 : by vect_estimate_min_profitable_iters, so changes here may need
14886 : corresponding changes there. */
14887 :
14888 : gimple_seq
14889 0 : vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
14890 : {
14891 0 : gimple_seq stmts = NULL;
14892 0 : tree len_type = TREE_TYPE (len);
14893 0 : gcc_assert (TREE_TYPE (start_index) == len_type);
14894 :
14895 0 : tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
14896 0 : tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
14897 0 : tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
14898 0 : gimple* stmt = gimple_build_assign (len, rhs);
14899 0 : gimple_seq_add_stmt (&stmts, stmt);
14900 :
14901 0 : return stmts;
14902 : }
14903 :
|