Branch data Line data Source code
1 : : /* Statement Analysis and Transformation for Vectorization
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 : : and Ira Rosen <irar@il.ibm.com>
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify it under
9 : : the terms of the GNU General Public License as published by the Free
10 : : Software Foundation; either version 3, or (at your option) any later
11 : : version.
12 : :
13 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : : for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "backend.h"
26 : : #include "target.h"
27 : : #include "rtl.h"
28 : : #include "tree.h"
29 : : #include "gimple.h"
30 : : #include "ssa.h"
31 : : #include "optabs-tree.h"
32 : : #include "insn-config.h"
33 : : #include "recog.h" /* FIXME: for insn_data */
34 : : #include "cgraph.h"
35 : : #include "dumpfile.h"
36 : : #include "alias.h"
37 : : #include "fold-const.h"
38 : : #include "stor-layout.h"
39 : : #include "tree-eh.h"
40 : : #include "gimplify.h"
41 : : #include "gimple-iterator.h"
42 : : #include "gimplify-me.h"
43 : : #include "tree-cfg.h"
44 : : #include "tree-ssa-loop-manip.h"
45 : : #include "cfgloop.h"
46 : : #include "explow.h"
47 : : #include "tree-ssa-loop.h"
48 : : #include "tree-scalar-evolution.h"
49 : : #include "tree-vectorizer.h"
50 : : #include "builtins.h"
51 : : #include "internal-fn.h"
52 : : #include "tree-vector-builder.h"
53 : : #include "vec-perm-indices.h"
54 : : #include "gimple-range.h"
55 : : #include "tree-ssa-loop-niter.h"
56 : : #include "gimple-fold.h"
57 : : #include "regs.h"
58 : : #include "attribs.h"
59 : : #include "optabs-libfuncs.h"
60 : : #include "tree-dfa.h"
61 : :
62 : : /* For lang_hooks.types.type_for_mode. */
63 : : #include "langhooks.h"
64 : :
65 : : /* Return the vectorized type for the given statement. */
66 : :
67 : : tree
68 : 0 : stmt_vectype (class _stmt_vec_info *stmt_info)
69 : : {
70 : 0 : return STMT_VINFO_VECTYPE (stmt_info);
71 : : }
72 : :
73 : : /* Return TRUE iff the given statement is in an inner loop relative to
74 : : the loop being vectorized. */
75 : : bool
76 : 3994155 : stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
77 : : {
78 : 3994155 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
79 : 3994155 : basic_block bb = gimple_bb (stmt);
80 : 3994155 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
81 : 706878 : class loop* loop;
82 : :
83 : 706878 : if (!loop_vinfo)
84 : : return false;
85 : :
86 : 706878 : loop = LOOP_VINFO_LOOP (loop_vinfo);
87 : :
88 : 706878 : return (bb->loop_father == loop->inner);
89 : : }
90 : :
91 : : /* Record the cost of a statement, either by directly informing the
92 : : target model or by saving it in a vector for later processing.
93 : : Return a preliminary estimate of the statement's cost. */
94 : :
95 : : unsigned
96 : 8957880 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
97 : : enum vect_cost_for_stmt kind,
98 : : stmt_vec_info stmt_info, slp_tree node,
99 : : tree vectype, int misalign,
100 : : enum vect_cost_model_location where)
101 : : {
102 : 8957880 : if ((kind == vector_load || kind == unaligned_load)
103 : 1266936 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
104 : : kind = vector_gather_load;
105 : 8957880 : if ((kind == vector_store || kind == unaligned_store)
106 : 977045 : && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
107 : 8957880 : kind = vector_scatter_store;
108 : :
109 : 8957880 : stmt_info_for_cost si
110 : 8957880 : = { count, kind, where, stmt_info, node, vectype, misalign };
111 : 8957880 : body_cost_vec->safe_push (si);
112 : :
113 : 8957880 : return (unsigned)
114 : 8957880 : (builtin_vectorization_cost (kind, vectype, misalign) * count);
115 : : }
116 : :
117 : : unsigned
118 : 4973561 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
119 : : enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
120 : : tree vectype, int misalign,
121 : : enum vect_cost_model_location where)
122 : : {
123 : 4973561 : return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
124 : 4973561 : vectype, misalign, where);
125 : : }
126 : :
127 : : unsigned
128 : 1040735 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
129 : : enum vect_cost_for_stmt kind, slp_tree node,
130 : : tree vectype, int misalign,
131 : : enum vect_cost_model_location where)
132 : : {
133 : 1040735 : return record_stmt_cost (body_cost_vec, count, kind,
134 : : SLP_TREE_REPRESENTATIVE (node), node,
135 : 1040735 : vectype, misalign, where);
136 : : }
137 : :
138 : : unsigned
139 : 117107 : record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
140 : : enum vect_cost_for_stmt kind,
141 : : enum vect_cost_model_location where)
142 : : {
143 : 117107 : gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
144 : : || kind == scalar_stmt);
145 : 117107 : return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
146 : 117107 : NULL_TREE, 0, where);
147 : : }
148 : :
149 : : /* Return a variable of type ELEM_TYPE[NELEMS]. */
150 : :
151 : : static tree
152 : 0 : create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
153 : : {
154 : 0 : return create_tmp_var (build_array_type_nelts (elem_type, nelems),
155 : 0 : "vect_array");
156 : : }
157 : :
158 : : /* ARRAY is an array of vectors created by create_vector_array.
159 : : Return an SSA_NAME for the vector in index N. The reference
160 : : is part of the vectorization of STMT_INFO and the vector is associated
161 : : with scalar destination SCALAR_DEST.
162 : : If we need to ensure that inactive elements are set to zero,
163 : : NEED_ZEROING is true, MASK contains the loop mask to be used. */
164 : :
165 : : static tree
166 : 0 : read_vector_array (vec_info *vinfo,
167 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
168 : : tree scalar_dest, tree array, unsigned HOST_WIDE_INT n,
169 : : bool need_zeroing, tree mask)
170 : : {
171 : 0 : tree vect_type, vect, vect_name, tmp, tmp_name, array_ref;
172 : 0 : gimple *new_stmt;
173 : :
174 : 0 : gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
175 : 0 : vect_type = TREE_TYPE (TREE_TYPE (array));
176 : 0 : tmp = vect_create_destination_var (scalar_dest, vect_type);
177 : 0 : vect = vect_create_destination_var (scalar_dest, vect_type);
178 : 0 : array_ref = build4 (ARRAY_REF, vect_type, array,
179 : 0 : build_int_cst (size_type_node, n),
180 : : NULL_TREE, NULL_TREE);
181 : :
182 : 0 : new_stmt = gimple_build_assign (tmp, array_ref);
183 : 0 : tmp_name = make_ssa_name (vect, new_stmt);
184 : 0 : gimple_assign_set_lhs (new_stmt, tmp_name);
185 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
186 : :
187 : 0 : if (need_zeroing)
188 : : {
189 : 0 : tree vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
190 : : vect_type);
191 : 0 : vect_name = make_ssa_name (vect, new_stmt);
192 : 0 : new_stmt
193 : 0 : = gimple_build_assign (vect_name, VEC_COND_EXPR,
194 : : mask, tmp_name, vec_els);
195 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
196 : : }
197 : : else
198 : : vect_name = tmp_name;
199 : :
200 : 0 : return vect_name;
201 : : }
202 : :
203 : : /* ARRAY is an array of vectors created by create_vector_array.
204 : : Emit code to store SSA_NAME VECT in index N of the array.
205 : : The store is part of the vectorization of STMT_INFO. */
206 : :
207 : : static void
208 : 0 : write_vector_array (vec_info *vinfo,
209 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
210 : : tree vect, tree array, unsigned HOST_WIDE_INT n)
211 : : {
212 : 0 : tree array_ref;
213 : 0 : gimple *new_stmt;
214 : :
215 : 0 : array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
216 : 0 : build_int_cst (size_type_node, n),
217 : : NULL_TREE, NULL_TREE);
218 : :
219 : 0 : new_stmt = gimple_build_assign (array_ref, vect);
220 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
221 : 0 : }
222 : :
223 : : /* PTR is a pointer to an array of type TYPE. Return a representation
224 : : of *PTR. The memory reference replaces those in FIRST_DR
225 : : (and its group). */
226 : :
227 : : static tree
228 : 0 : create_array_ref (tree type, tree ptr, tree alias_ptr_type)
229 : : {
230 : 0 : tree mem_ref;
231 : :
232 : 0 : mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
233 : : /* Arrays have the same alignment as their type. */
234 : 0 : set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
235 : 0 : return mem_ref;
236 : : }
237 : :
238 : : /* Add a clobber of variable VAR to the vectorization of STMT_INFO.
239 : : Emit the clobber before *GSI. */
240 : :
241 : : static void
242 : 15 : vect_clobber_variable (vec_info *vinfo, stmt_vec_info stmt_info,
243 : : gimple_stmt_iterator *gsi, tree var)
244 : : {
245 : 15 : tree clobber = build_clobber (TREE_TYPE (var));
246 : 15 : gimple *new_stmt = gimple_build_assign (var, clobber);
247 : 15 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
248 : 15 : }
249 : :
250 : : /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
251 : :
252 : : /* Function vect_mark_relevant.
253 : :
254 : : Mark STMT_INFO as "relevant for vectorization" and add it to WORKLIST. */
255 : :
256 : : static void
257 : 2892640 : vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
258 : : enum vect_relevant relevant, bool live_p)
259 : : {
260 : 2892640 : enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
261 : 2892640 : bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
262 : :
263 : 2892640 : if (dump_enabled_p ())
264 : 154959 : dump_printf_loc (MSG_NOTE, vect_location,
265 : : "mark relevant %d, live %d: %G", relevant, live_p,
266 : : stmt_info->stmt);
267 : :
268 : : /* If this stmt is an original stmt in a pattern, we might need to mark its
269 : : related pattern stmt instead of the original stmt. However, such stmts
270 : : may have their own uses that are not in any pattern, in such cases the
271 : : stmt itself should be marked. */
272 : 2892640 : if (STMT_VINFO_IN_PATTERN_P (stmt_info))
273 : : {
274 : : /* This is the last stmt in a sequence that was detected as a
275 : : pattern that can potentially be vectorized. Don't mark the stmt
276 : : as relevant/live because it's not going to be vectorized.
277 : : Instead mark the pattern-stmt that replaces it. */
278 : :
279 : 164583 : if (dump_enabled_p ())
280 : 2292 : dump_printf_loc (MSG_NOTE, vect_location,
281 : : "last stmt in pattern. don't mark"
282 : : " relevant/live.\n");
283 : :
284 : 164583 : stmt_vec_info old_stmt_info = stmt_info;
285 : 164583 : stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
286 : 164583 : gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
287 : 164583 : save_relevant = STMT_VINFO_RELEVANT (stmt_info);
288 : 164583 : save_live_p = STMT_VINFO_LIVE_P (stmt_info);
289 : :
290 : 164583 : if (live_p && relevant == vect_unused_in_scope)
291 : : {
292 : 92 : if (dump_enabled_p ())
293 : 4 : dump_printf_loc (MSG_NOTE, vect_location,
294 : : "vec_stmt_relevant_p: forcing live pattern stmt "
295 : : "relevant.\n");
296 : : relevant = vect_used_only_live;
297 : : }
298 : :
299 : 164583 : if (dump_enabled_p ())
300 : 2292 : dump_printf_loc (MSG_NOTE, vect_location,
301 : : "mark relevant %d, live %d: %G", relevant, live_p,
302 : : stmt_info->stmt);
303 : : }
304 : :
305 : 2892640 : STMT_VINFO_LIVE_P (stmt_info) |= live_p;
306 : 2892640 : if (relevant > STMT_VINFO_RELEVANT (stmt_info))
307 : 2404802 : STMT_VINFO_RELEVANT (stmt_info) = relevant;
308 : :
309 : 2892640 : if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
310 : 487838 : && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
311 : : {
312 : 487124 : if (dump_enabled_p ())
313 : 19748 : dump_printf_loc (MSG_NOTE, vect_location,
314 : : "already marked relevant/live.\n");
315 : 487124 : return;
316 : : }
317 : :
318 : 2405516 : worklist->safe_push (stmt_info);
319 : : }
320 : :
321 : :
322 : : /* Function is_simple_and_all_uses_invariant
323 : :
324 : : Return true if STMT_INFO is simple and all uses of it are invariant. */
325 : :
326 : : bool
327 : 355583 : is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
328 : : loop_vec_info loop_vinfo)
329 : : {
330 : 355583 : tree op;
331 : 355583 : ssa_op_iter iter;
332 : :
333 : 469510 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
334 : 114733 : if (!stmt)
335 : : return false;
336 : :
337 : 119882 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
338 : : {
339 : 119076 : enum vect_def_type dt = vect_uninitialized_def;
340 : :
341 : 119076 : if (!vect_is_simple_use (op, loop_vinfo, &dt))
342 : : {
343 : 1209 : if (dump_enabled_p ())
344 : 23 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
345 : : "use not simple.\n");
346 : 113927 : return false;
347 : : }
348 : :
349 : 117867 : if (dt != vect_external_def && dt != vect_constant_def)
350 : : return false;
351 : : }
352 : : return true;
353 : : }
354 : :
355 : : /* Function vect_stmt_relevant_p.
356 : :
357 : : Return true if STMT_INFO, in the loop that is represented by LOOP_VINFO,
358 : : is "relevant for vectorization".
359 : :
360 : : A stmt is considered "relevant for vectorization" if:
361 : : - it has uses outside the loop.
362 : : - it has vdefs (it alters memory).
363 : : - control stmts in the loop (except for the exit condition).
364 : : - it is an induction and we have multiple exits.
365 : :
366 : : CHECKME: what other side effects would the vectorizer allow? */
367 : :
368 : : static bool
369 : 3710677 : vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
370 : : enum vect_relevant *relevant, bool *live_p)
371 : : {
372 : 3710677 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
373 : 3710677 : ssa_op_iter op_iter;
374 : 3710677 : imm_use_iterator imm_iter;
375 : 3710677 : use_operand_p use_p;
376 : 3710677 : def_operand_p def_p;
377 : :
378 : 3710677 : *relevant = vect_unused_in_scope;
379 : 3710677 : *live_p = false;
380 : :
381 : : /* cond stmt other than loop exit cond. */
382 : 3710677 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
383 : 3710677 : if (is_ctrl_stmt (stmt)
384 : 465856 : && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
385 : 3871714 : && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
386 : 159406 : *relevant = vect_used_in_scope;
387 : :
388 : : /* changing memory. */
389 : 3710677 : if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
390 : 3081575 : if (gimple_vdef (stmt_info->stmt)
391 : 2613314 : && !gimple_clobber_p (stmt_info->stmt))
392 : : {
393 : 265165 : if (dump_enabled_p ())
394 : 25678 : dump_printf_loc (MSG_NOTE, vect_location,
395 : : "vec_stmt_relevant_p: stmt has vdefs.\n");
396 : 265165 : *relevant = vect_used_in_scope;
397 : : }
398 : :
399 : : /* uses outside the loop. */
400 : 10388688 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
401 : : {
402 : 7877563 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
403 : : {
404 : 4910229 : basic_block bb = gimple_bb (USE_STMT (use_p));
405 : 4910229 : if (!flow_bb_inside_loop_p (loop, bb))
406 : : {
407 : 163451 : if (is_gimple_debug (USE_STMT (use_p)))
408 : 621 : continue;
409 : :
410 : 162830 : if (dump_enabled_p ())
411 : 5186 : dump_printf_loc (MSG_NOTE, vect_location,
412 : : "vec_stmt_relevant_p: used out of loop.\n");
413 : :
414 : : /* We expect all such uses to be in the loop exit phis
415 : : (because of loop closed form) */
416 : 162830 : gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
417 : :
418 : 162830 : *live_p = true;
419 : : }
420 : : }
421 : : }
422 : :
423 : : /* Check if it's a not live PHI and multiple exits. In this case
424 : : there will be a usage later on after peeling which is needed for the
425 : : alternate exit. */
426 : 3710677 : if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
427 : 1306309 : && is_a <gphi *> (stmt)
428 : 237228 : && gimple_bb (stmt) == LOOP_VINFO_LOOP (loop_vinfo)->header
429 : 3947882 : && ((! VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
430 : 233294 : && ! *live_p)
431 : 33428 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def))
432 : : {
433 : 229570 : if (dump_enabled_p ())
434 : 2856 : dump_printf_loc (MSG_NOTE, vect_location,
435 : : "vec_stmt_relevant_p: PHI forced live for "
436 : : "early break.\n");
437 : 229570 : LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo).safe_push (stmt_info);
438 : 229570 : *live_p = true;
439 : : }
440 : :
441 : 355591 : if (*live_p && *relevant == vect_unused_in_scope
442 : 4066260 : && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
443 : : {
444 : 354777 : if (dump_enabled_p ())
445 : 7695 : dump_printf_loc (MSG_NOTE, vect_location,
446 : : "vec_stmt_relevant_p: stmt live but not relevant.\n");
447 : 354777 : *relevant = vect_used_only_live;
448 : : }
449 : :
450 : 3710677 : return (*live_p || *relevant);
451 : : }
452 : :
453 : :
454 : : /* Function exist_non_indexing_operands_for_use_p
455 : :
456 : : USE is one of the uses attached to STMT_INFO. Check if USE is
457 : : used in STMT_INFO for anything other than indexing an array. */
458 : :
459 : : static bool
460 : 3639952 : exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
461 : : {
462 : 3639952 : tree operand;
463 : :
464 : : /* USE corresponds to some operand in STMT. If there is no data
465 : : reference in STMT, then any operand that corresponds to USE
466 : : is not indexing an array. */
467 : 3639952 : if (!STMT_VINFO_DATA_REF (stmt_info))
468 : : return true;
469 : :
470 : : /* STMT has a data_ref. FORNOW this means that its of one of
471 : : the following forms:
472 : : -1- ARRAY_REF = var
473 : : -2- var = ARRAY_REF
474 : : (This should have been verified in analyze_data_refs).
475 : :
476 : : 'var' in the second case corresponds to a def, not a use,
477 : : so USE cannot correspond to any operands that are not used
478 : : for array indexing.
479 : :
480 : : Therefore, all we need to check is if STMT falls into the
481 : : first case, and whether var corresponds to USE. */
482 : :
483 : 1085180 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
484 : 1073333 : if (!assign || !gimple_assign_copy_p (assign))
485 : : {
486 : 585829 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
487 : 11847 : if (call && gimple_call_internal_p (call))
488 : : {
489 : 11847 : internal_fn ifn = gimple_call_internal_fn (call);
490 : 11847 : int mask_index = internal_fn_mask_index (ifn);
491 : 11847 : if (mask_index >= 0
492 : 11847 : && use == gimple_call_arg (call, mask_index))
493 : : return true;
494 : 7753 : int els_index = internal_fn_else_index (ifn);
495 : 7753 : if (els_index >= 0
496 : 7753 : && use == gimple_call_arg (call, els_index))
497 : : return true;
498 : 6648 : int stored_value_index = internal_fn_stored_value_index (ifn);
499 : 6648 : if (stored_value_index >= 0
500 : 6648 : && use == gimple_call_arg (call, stored_value_index))
501 : : return true;
502 : 5369 : if (internal_gather_scatter_fn_p (ifn)
503 : 5369 : && use == gimple_call_arg (call, 1))
504 : : return true;
505 : : }
506 : 579351 : return false;
507 : : }
508 : :
509 : 499351 : if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
510 : : return false;
511 : 499351 : operand = gimple_assign_rhs1 (assign);
512 : 499351 : if (TREE_CODE (operand) != SSA_NAME)
513 : : return false;
514 : :
515 : 434849 : if (operand == use)
516 : : return true;
517 : :
518 : : return false;
519 : : }
520 : :
521 : :
522 : : /*
523 : : Function process_use.
524 : :
525 : : Inputs:
526 : : - a USE in STMT_VINFO in a loop represented by LOOP_VINFO
527 : : - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
528 : : that defined USE. This is done by calling mark_relevant and passing it
529 : : the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
530 : : - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
531 : : be performed.
532 : :
533 : : Outputs:
534 : : Generally, LIVE_P and RELEVANT are used to define the liveness and
535 : : relevance info of the DEF_STMT of this USE:
536 : : STMT_VINFO_LIVE_P (DEF_stmt_vinfo) <-- live_p
537 : : STMT_VINFO_RELEVANT (DEF_stmt_vinfo) <-- relevant
538 : : Exceptions:
539 : : - case 1: If USE is used only for address computations (e.g. array indexing),
540 : : which does not need to be directly vectorized, then the liveness/relevance
541 : : of the respective DEF_STMT is left unchanged.
542 : : - case 2: If STMT_VINFO is a reduction phi and DEF_STMT is a reduction stmt,
543 : : we skip DEF_STMT cause it had already been processed.
544 : : - case 3: If DEF_STMT and STMT_VINFO are in different nests, then
545 : : "relevant" will be modified accordingly.
546 : :
547 : : Return true if everything is as expected. Return false otherwise. */
548 : :
549 : : static opt_result
550 : 3669229 : process_use (stmt_vec_info stmt_vinfo, tree use, loop_vec_info loop_vinfo,
551 : : enum vect_relevant relevant, vec<stmt_vec_info> *worklist,
552 : : bool force)
553 : : {
554 : 3669229 : stmt_vec_info dstmt_vinfo;
555 : 3669229 : enum vect_def_type dt;
556 : :
557 : : /* case 1: we are only interested in uses that need to be vectorized. Uses
558 : : that are used for address computation are not considered relevant. */
559 : 3669229 : if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
560 : 873501 : return opt_result::success ();
561 : :
562 : 2795728 : if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
563 : 12666 : return opt_result::failure_at (stmt_vinfo->stmt,
564 : : "not vectorized:"
565 : : " unsupported use in stmt.\n");
566 : :
567 : 2783062 : if (!dstmt_vinfo)
568 : 615781 : return opt_result::success ();
569 : :
570 : 2167281 : basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
571 : 2167281 : basic_block bb = gimple_bb (stmt_vinfo->stmt);
572 : :
573 : : /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
574 : : We have to force the stmt live since the epilogue loop needs it to
575 : : continue computing the reduction. */
576 : 2167281 : if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
577 : 375551 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
578 : 56117 : && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
579 : 56117 : && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
580 : 2223398 : && bb->loop_father == def_bb->loop_father)
581 : : {
582 : 56117 : if (dump_enabled_p ())
583 : 3316 : dump_printf_loc (MSG_NOTE, vect_location,
584 : : "reduc-stmt defining reduc-phi in the same nest.\n");
585 : 56117 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
586 : 56117 : return opt_result::success ();
587 : : }
588 : :
589 : : /* case 3a: outer-loop stmt defining an inner-loop stmt:
590 : : outer-loop-header-bb:
591 : : d = dstmt_vinfo
592 : : inner-loop:
593 : : stmt # use (d)
594 : : outer-loop-tail-bb:
595 : : ... */
596 : 2111164 : if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
597 : : {
598 : 1851 : if (dump_enabled_p ())
599 : 256 : dump_printf_loc (MSG_NOTE, vect_location,
600 : : "outer-loop def-stmt defining inner-loop stmt.\n");
601 : :
602 : 1851 : switch (relevant)
603 : : {
604 : 0 : case vect_unused_in_scope:
605 : 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
606 : : vect_used_in_scope : vect_unused_in_scope;
607 : : break;
608 : :
609 : 617 : case vect_used_in_outer_by_reduction:
610 : 617 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
611 : : relevant = vect_used_by_reduction;
612 : : break;
613 : :
614 : 1022 : case vect_used_in_outer:
615 : 1022 : gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
616 : : relevant = vect_used_in_scope;
617 : : break;
618 : :
619 : : case vect_used_in_scope:
620 : : break;
621 : :
622 : 0 : default:
623 : 0 : gcc_unreachable ();
624 : : }
625 : : }
626 : :
627 : : /* case 3b: inner-loop stmt defining an outer-loop stmt:
628 : : outer-loop-header-bb:
629 : : ...
630 : : inner-loop:
631 : : d = dstmt_vinfo
632 : : outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
633 : : stmt # use (d) */
634 : 2109313 : else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
635 : : {
636 : 1718 : if (dump_enabled_p ())
637 : 496 : dump_printf_loc (MSG_NOTE, vect_location,
638 : : "inner-loop def-stmt defining outer-loop stmt.\n");
639 : :
640 : 1718 : switch (relevant)
641 : : {
642 : 0 : case vect_unused_in_scope:
643 : 0 : relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
644 : 0 : || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
645 : : vect_used_in_outer_by_reduction : vect_unused_in_scope;
646 : : break;
647 : :
648 : : case vect_used_by_reduction:
649 : : case vect_used_only_live:
650 : : relevant = vect_used_in_outer_by_reduction;
651 : : break;
652 : :
653 : : case vect_used_in_scope:
654 : 2056369 : relevant = vect_used_in_outer;
655 : : break;
656 : :
657 : 0 : default:
658 : 0 : gcc_unreachable ();
659 : : }
660 : : }
661 : : /* We are also not interested in uses on loop PHI backedges that are
662 : : inductions. Otherwise we'll needlessly vectorize the IV increment
663 : : and cause hybrid SLP for SLP inductions. Unless the PHI is live
664 : : of course. */
665 : 2107595 : else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
666 : 316658 : && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
667 : 313165 : && ! STMT_VINFO_LIVE_P (stmt_vinfo)
668 : 2162390 : && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
669 : : loop_latch_edge (bb->loop_father))
670 : : == use))
671 : : {
672 : 54795 : if (dump_enabled_p ())
673 : 3345 : dump_printf_loc (MSG_NOTE, vect_location,
674 : : "induction value on backedge.\n");
675 : 54795 : return opt_result::success ();
676 : : }
677 : :
678 : :
679 : 2056369 : vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
680 : 2056369 : return opt_result::success ();
681 : : }
682 : :
683 : :
684 : : /* Function vect_mark_stmts_to_be_vectorized.
685 : :
686 : : Not all stmts in the loop need to be vectorized. For example:
687 : :
688 : : for i...
689 : : for j...
690 : : 1. T0 = i + j
691 : : 2. T1 = a[T0]
692 : :
693 : : 3. j = j + 1
694 : :
695 : : Stmt 1 and 3 do not need to be vectorized, because loop control and
696 : : addressing of vectorized data-refs are handled differently.
697 : :
698 : : This pass detects such stmts. */
699 : :
700 : : opt_result
701 : 304819 : vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
702 : : {
703 : 304819 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
704 : 304819 : basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
705 : 304819 : unsigned int nbbs = loop->num_nodes;
706 : 304819 : gimple_stmt_iterator si;
707 : 304819 : unsigned int i;
708 : 304819 : basic_block bb;
709 : 304819 : bool live_p;
710 : 304819 : enum vect_relevant relevant;
711 : :
712 : 304819 : DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
713 : :
714 : 304819 : auto_vec<stmt_vec_info, 64> worklist;
715 : :
716 : : /* 1. Init worklist. */
717 : 1078768 : for (i = 0; i < nbbs; i++)
718 : : {
719 : 773949 : bb = bbs[i];
720 : 1569240 : for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
721 : : {
722 : 1590582 : if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
723 : 166189 : continue;
724 : 629102 : stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
725 : 629102 : if (dump_enabled_p ())
726 : 37109 : dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
727 : : phi_info->stmt);
728 : :
729 : 629102 : if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
730 : 234922 : vect_mark_relevant (&worklist, phi_info, relevant, live_p);
731 : : }
732 : 6297645 : for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
733 : : {
734 : 4749747 : if (is_gimple_debug (gsi_stmt (si)))
735 : 1668172 : continue;
736 : 3081575 : stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
737 : 3081575 : if (dump_enabled_p ())
738 : 200834 : dump_printf_loc (MSG_NOTE, vect_location,
739 : : "init: stmt relevant? %G", stmt_info->stmt);
740 : :
741 : 3081575 : if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
742 : 545232 : vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
743 : : }
744 : : }
745 : :
746 : : /* 2. Process_worklist */
747 : 2659097 : while (worklist.length () > 0)
748 : : {
749 : 2366946 : use_operand_p use_p;
750 : 2366946 : ssa_op_iter iter;
751 : :
752 : 2366946 : stmt_vec_info stmt_vinfo = worklist.pop ();
753 : 2366946 : if (dump_enabled_p ())
754 : 134481 : dump_printf_loc (MSG_NOTE, vect_location,
755 : : "worklist: examine stmt: %G", stmt_vinfo->stmt);
756 : :
757 : : /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
758 : : (DEF_STMT) as relevant/irrelevant according to the relevance property
759 : : of STMT. */
760 : 2366946 : relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
761 : :
762 : : /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
763 : : propagated as is to the DEF_STMTs of its USEs.
764 : :
765 : : One exception is when STMT has been identified as defining a reduction
766 : : variable; in this case we set the relevance to vect_used_by_reduction.
767 : : This is because we distinguish between two kinds of relevant stmts -
768 : : those that are used by a reduction computation, and those that are
769 : : (also) used by a regular computation. This allows us later on to
770 : : identify stmts that are used solely by a reduction, and therefore the
771 : : order of the results that they produce does not have to be kept. */
772 : :
773 : 2366946 : switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
774 : : {
775 : 112810 : case vect_reduction_def:
776 : 112810 : gcc_assert (relevant != vect_unused_in_scope);
777 : 112810 : if (relevant != vect_unused_in_scope
778 : 112810 : && relevant != vect_used_in_scope
779 : 112810 : && relevant != vect_used_by_reduction
780 : 112810 : && relevant != vect_used_only_live)
781 : 0 : return opt_result::failure_at
782 : 0 : (stmt_vinfo->stmt, "unsupported use of reduction.\n");
783 : : break;
784 : :
785 : 1895 : case vect_nested_cycle:
786 : 1895 : if (relevant != vect_unused_in_scope
787 : 1895 : && relevant != vect_used_in_outer_by_reduction
788 : 1451 : && relevant != vect_used_in_outer)
789 : 2 : return opt_result::failure_at
790 : 2 : (stmt_vinfo->stmt, "unsupported use of nested cycle.\n");
791 : : break;
792 : :
793 : 976 : case vect_double_reduction_def:
794 : 976 : if (relevant != vect_unused_in_scope
795 : 976 : && relevant != vect_used_by_reduction
796 : 344 : && relevant != vect_used_only_live)
797 : 0 : return opt_result::failure_at
798 : 0 : (stmt_vinfo->stmt, "unsupported use of double reduction.\n");
799 : : break;
800 : :
801 : : default:
802 : : break;
803 : : }
804 : :
805 : 2366944 : if (is_pattern_stmt_p (stmt_vinfo))
806 : : {
807 : : /* Pattern statements are not inserted into the code, so
808 : : FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
809 : : have to scan the RHS or function arguments instead. */
810 : 427106 : if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
811 : : {
812 : 270062 : enum tree_code rhs_code = gimple_assign_rhs_code (assign);
813 : 270062 : tree op = gimple_assign_rhs1 (assign);
814 : :
815 : 270062 : i = 1;
816 : 270062 : if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
817 : : {
818 : 0 : opt_result res
819 : 0 : = process_use (stmt_vinfo, TREE_OPERAND (op, 0),
820 : : loop_vinfo, relevant, &worklist, false);
821 : 0 : if (!res)
822 : 0 : return res;
823 : 0 : res = process_use (stmt_vinfo, TREE_OPERAND (op, 1),
824 : : loop_vinfo, relevant, &worklist, false);
825 : 0 : if (!res)
826 : 0 : return res;
827 : : i = 2;
828 : : }
829 : 786784 : for (; i < gimple_num_ops (assign); i++)
830 : : {
831 : 518201 : op = gimple_op (assign, i);
832 : 518201 : if (TREE_CODE (op) == SSA_NAME)
833 : : {
834 : 393277 : opt_result res
835 : 393277 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
836 : : &worklist, false);
837 : 393277 : if (!res)
838 : 1479 : return res;
839 : : }
840 : : }
841 : : }
842 : 157044 : else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
843 : : {
844 : 153082 : tree_code rhs_code = gimple_cond_code (cond);
845 : 153082 : gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
846 : 153082 : opt_result res
847 : 153082 : = process_use (stmt_vinfo, gimple_cond_lhs (cond),
848 : : loop_vinfo, relevant, &worklist, false);
849 : 153082 : if (!res)
850 : 12668 : return res;
851 : 153082 : res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
852 : : loop_vinfo, relevant, &worklist, false);
853 : 153082 : if (!res)
854 : 0 : return res;
855 : : }
856 : 3962 : else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
857 : : {
858 : 19058 : for (i = 0; i < gimple_call_num_args (call); i++)
859 : : {
860 : 15096 : tree arg = gimple_call_arg (call, i);
861 : 15096 : opt_result res
862 : 15096 : = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
863 : : &worklist, false);
864 : 15096 : if (!res)
865 : 0 : return res;
866 : : }
867 : : }
868 : : else
869 : 0 : gcc_unreachable ();
870 : : }
871 : : else
872 : 6795283 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
873 : : {
874 : 2925415 : tree op = USE_FROM_PTR (use_p);
875 : 2925415 : opt_result res
876 : 2925415 : = process_use (stmt_vinfo, op, loop_vinfo, relevant,
877 : : &worklist, false);
878 : 2925415 : if (!res)
879 : 9808 : return res;
880 : : }
881 : :
882 : 2355657 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
883 : : {
884 : 29277 : gather_scatter_info gs_info;
885 : 29277 : if (!vect_check_gather_scatter (stmt_vinfo, loop_vinfo, &gs_info))
886 : 0 : gcc_unreachable ();
887 : 29277 : opt_result res
888 : 29277 : = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
889 : : &worklist, true);
890 : 29277 : if (!res)
891 : : {
892 : 1379 : if (fatal)
893 : 1379 : *fatal = false;
894 : 1379 : return res;
895 : : }
896 : : }
897 : : } /* while worklist */
898 : :
899 : 292151 : return opt_result::success ();
900 : 304819 : }
901 : :
902 : : /* Function vect_model_simple_cost.
903 : :
904 : : Models cost for simple operations, i.e. those that only emit ncopies of a
905 : : single op. Right now, this does not account for multiple insns that could
906 : : be generated for the single vector op. We will handle that shortly. */
907 : :
908 : : static void
909 : 583259 : vect_model_simple_cost (vec_info *, int ncopies, enum vect_def_type *dt,
910 : : int ndts, slp_tree node,
911 : : stmt_vector_for_cost *cost_vec,
912 : : vect_cost_for_stmt kind = vector_stmt)
913 : : {
914 : 583259 : int inside_cost = 0, prologue_cost = 0;
915 : :
916 : 583259 : gcc_assert (cost_vec != NULL);
917 : :
918 : : /* ??? Somehow we need to fix this at the callers. */
919 : 583259 : if (node)
920 : 583259 : ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
921 : :
922 : 583259 : if (!node)
923 : : /* Cost the "broadcast" of a scalar operand in to a vector operand.
924 : : Use scalar_to_vec to cost the broadcast, as elsewhere in the vector
925 : : cost model. */
926 : 0 : for (int i = 0; i < ndts; i++)
927 : 0 : if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
928 : 0 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
929 : : node, 0, vect_prologue);
930 : :
931 : : /* Pass the inside-of-loop statements to the target-specific cost model. */
932 : 583259 : inside_cost += record_stmt_cost (cost_vec, ncopies, kind,
933 : : node, 0, vect_body);
934 : :
935 : 583259 : if (dump_enabled_p ())
936 : 29977 : dump_printf_loc (MSG_NOTE, vect_location,
937 : : "vect_model_simple_cost: inside_cost = %d, "
938 : : "prologue_cost = %d .\n", inside_cost, prologue_cost);
939 : 583259 : }
940 : :
941 : :
942 : : /* Model cost for type demotion and promotion operations. PWR is
943 : : normally zero for single-step promotions and demotions. It will be
944 : : one if two-step promotion/demotion is required, and so on. NCOPIES
945 : : is the number of vector results (and thus number of instructions)
946 : : for the narrowest end of the operation chain. Each additional
947 : : step doubles the number of instructions required. If WIDEN_ARITH
948 : : is true the stmt is doing widening arithmetic. */
949 : :
950 : : static void
951 : 46908 : vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
952 : : enum vect_def_type *dt,
953 : : unsigned int ncopies, int pwr,
954 : : stmt_vector_for_cost *cost_vec,
955 : : bool widen_arith)
956 : : {
957 : 46908 : int i;
958 : 46908 : int inside_cost = 0, prologue_cost = 0;
959 : :
960 : 111995 : for (i = 0; i < pwr + 1; i++)
961 : : {
962 : 128306 : inside_cost += record_stmt_cost (cost_vec, ncopies,
963 : : widen_arith
964 : : ? vector_stmt : vec_promote_demote,
965 : : stmt_info, 0, vect_body);
966 : 65087 : ncopies *= 2;
967 : : }
968 : :
969 : : /* FORNOW: Assuming maximum 2 args per stmts. */
970 : 140724 : for (i = 0; i < 2; i++)
971 : 93816 : if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
972 : 2519 : prologue_cost += record_stmt_cost (cost_vec, 1, vector_stmt,
973 : : stmt_info, 0, vect_prologue);
974 : :
975 : 46908 : if (dump_enabled_p ())
976 : 5864 : dump_printf_loc (MSG_NOTE, vect_location,
977 : : "vect_model_promotion_demotion_cost: inside_cost = %d, "
978 : : "prologue_cost = %d .\n", inside_cost, prologue_cost);
979 : 46908 : }
980 : :
981 : : /* Returns true if the current function returns DECL. */
982 : :
983 : : static bool
984 : 592663 : cfun_returns (tree decl)
985 : : {
986 : 592663 : edge_iterator ei;
987 : 592663 : edge e;
988 : 1161641 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
989 : : {
990 : 1173842 : greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
991 : 586921 : if (!ret)
992 : 0 : continue;
993 : 586921 : if (gimple_return_retval (ret) == decl)
994 : : return true;
995 : : /* We often end up with an aggregate copy to the result decl,
996 : : handle that case as well. First skip intermediate clobbers
997 : : though. */
998 : : gimple *def = ret;
999 : 1639324 : do
1000 : : {
1001 : 3278648 : def = SSA_NAME_DEF_STMT (gimple_vuse (def));
1002 : : }
1003 : 1639324 : while (gimple_clobber_p (def));
1004 : 569818 : if (is_a <gassign *> (def)
1005 : 66242 : && gimple_assign_lhs (def) == gimple_return_retval (ret)
1006 : 577243 : && gimple_assign_rhs1 (def) == decl)
1007 : : return true;
1008 : : }
1009 : : return false;
1010 : : }
1011 : :
1012 : : /* Calculate cost of DR's memory access. */
1013 : : void
1014 : 960949 : vect_get_store_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1015 : : int ncopies, dr_alignment_support alignment_support_scheme,
1016 : : int misalignment,
1017 : : unsigned int *inside_cost,
1018 : : stmt_vector_for_cost *body_cost_vec)
1019 : : {
1020 : 960949 : tree vectype
1021 : 960949 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1022 : 960949 : switch (alignment_support_scheme)
1023 : : {
1024 : 544951 : case dr_aligned:
1025 : 544951 : {
1026 : 544951 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1027 : : vector_store, stmt_info, slp_node,
1028 : : vectype, 0, vect_body);
1029 : :
1030 : 544951 : if (dump_enabled_p ())
1031 : 13066 : dump_printf_loc (MSG_NOTE, vect_location,
1032 : : "vect_model_store_cost: aligned.\n");
1033 : : break;
1034 : : }
1035 : :
1036 : 415998 : case dr_unaligned_supported:
1037 : 415998 : {
1038 : : /* Here, we assign an additional cost for the unaligned store. */
1039 : 415998 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1040 : : unaligned_store, stmt_info, slp_node,
1041 : : vectype, misalignment, vect_body);
1042 : 415998 : if (dump_enabled_p ())
1043 : 12008 : dump_printf_loc (MSG_NOTE, vect_location,
1044 : : "vect_model_store_cost: unaligned supported by "
1045 : : "hardware.\n");
1046 : : break;
1047 : : }
1048 : :
1049 : 0 : case dr_unaligned_unsupported:
1050 : 0 : {
1051 : 0 : *inside_cost = VECT_MAX_COST;
1052 : :
1053 : 0 : if (dump_enabled_p ())
1054 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1055 : : "vect_model_store_cost: unsupported access.\n");
1056 : : break;
1057 : : }
1058 : :
1059 : 0 : default:
1060 : 0 : gcc_unreachable ();
1061 : : }
1062 : 960949 : }
1063 : :
1064 : : /* Calculate cost of DR's memory access. */
1065 : : void
1066 : 650770 : vect_get_load_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1067 : : int ncopies, dr_alignment_support alignment_support_scheme,
1068 : : int misalignment,
1069 : : bool add_realign_cost, unsigned int *inside_cost,
1070 : : unsigned int *prologue_cost,
1071 : : stmt_vector_for_cost *prologue_cost_vec,
1072 : : stmt_vector_for_cost *body_cost_vec,
1073 : : bool record_prologue_costs)
1074 : : {
1075 : 650770 : tree vectype
1076 : 650770 : = slp_node ? SLP_TREE_VECTYPE (slp_node) : STMT_VINFO_VECTYPE (stmt_info);
1077 : 650770 : switch (alignment_support_scheme)
1078 : : {
1079 : 377800 : case dr_aligned:
1080 : 377800 : {
1081 : 377800 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1082 : : stmt_info, slp_node, vectype,
1083 : : 0, vect_body);
1084 : :
1085 : 377800 : if (dump_enabled_p ())
1086 : 16604 : dump_printf_loc (MSG_NOTE, vect_location,
1087 : : "vect_model_load_cost: aligned.\n");
1088 : :
1089 : : break;
1090 : : }
1091 : 241720 : case dr_unaligned_supported:
1092 : 241720 : {
1093 : : /* Here, we assign an additional cost for the unaligned load. */
1094 : 241720 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1095 : : unaligned_load, stmt_info, slp_node,
1096 : : vectype, misalignment, vect_body);
1097 : :
1098 : 241720 : if (dump_enabled_p ())
1099 : 19846 : dump_printf_loc (MSG_NOTE, vect_location,
1100 : : "vect_model_load_cost: unaligned supported by "
1101 : : "hardware.\n");
1102 : :
1103 : : break;
1104 : : }
1105 : 0 : case dr_explicit_realign:
1106 : 0 : {
1107 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1108 : : vector_load, stmt_info, slp_node,
1109 : : vectype, 0, vect_body);
1110 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1111 : : vec_perm, stmt_info, slp_node,
1112 : : vectype, 0, vect_body);
1113 : :
1114 : : /* FIXME: If the misalignment remains fixed across the iterations of
1115 : : the containing loop, the following cost should be added to the
1116 : : prologue costs. */
1117 : 0 : if (targetm.vectorize.builtin_mask_for_load)
1118 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1119 : : stmt_info, slp_node, vectype,
1120 : : 0, vect_body);
1121 : :
1122 : 0 : if (dump_enabled_p ())
1123 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1124 : : "vect_model_load_cost: explicit realign\n");
1125 : :
1126 : : break;
1127 : : }
1128 : 0 : case dr_explicit_realign_optimized:
1129 : 0 : {
1130 : 0 : if (dump_enabled_p ())
1131 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1132 : : "vect_model_load_cost: unaligned software "
1133 : : "pipelined.\n");
1134 : :
1135 : : /* Unaligned software pipeline has a load of an address, an initial
1136 : : load, and possibly a mask operation to "prime" the loop. However,
1137 : : if this is an access in a group of loads, which provide grouped
1138 : : access, then the above cost should only be considered for one
1139 : : access in the group. Inside the loop, there is a load op
1140 : : and a realignment op. */
1141 : :
1142 : 0 : if (add_realign_cost && record_prologue_costs)
1143 : : {
1144 : 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1145 : : vector_stmt, stmt_info,
1146 : : slp_node, vectype,
1147 : : 0, vect_prologue);
1148 : 0 : if (targetm.vectorize.builtin_mask_for_load)
1149 : 0 : *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1150 : : vector_stmt, stmt_info,
1151 : : slp_node, vectype,
1152 : : 0, vect_prologue);
1153 : : }
1154 : :
1155 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1156 : : stmt_info, slp_node, vectype,
1157 : : 0, vect_body);
1158 : 0 : *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1159 : : stmt_info, slp_node, vectype,
1160 : : 0, vect_body);
1161 : :
1162 : 0 : if (dump_enabled_p ())
1163 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1164 : : "vect_model_load_cost: explicit realign optimized"
1165 : : "\n");
1166 : :
1167 : : break;
1168 : : }
1169 : :
1170 : 31250 : case dr_unaligned_unsupported:
1171 : 31250 : {
1172 : 31250 : *inside_cost = VECT_MAX_COST;
1173 : :
1174 : 31250 : if (dump_enabled_p ())
1175 : 85 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1176 : : "vect_model_load_cost: unsupported access.\n");
1177 : : break;
1178 : : }
1179 : :
1180 : 0 : default:
1181 : 0 : gcc_unreachable ();
1182 : : }
1183 : 650770 : }
1184 : :
1185 : : /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1186 : : the loop preheader for the vectorized stmt STMT_VINFO. */
1187 : :
1188 : : static void
1189 : 6144 : vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
1190 : : gimple_stmt_iterator *gsi)
1191 : : {
1192 : 6144 : if (gsi)
1193 : 2895 : vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
1194 : : else
1195 : 3249 : vinfo->insert_on_entry (stmt_vinfo, new_stmt);
1196 : :
1197 : 6144 : if (dump_enabled_p ())
1198 : 1682 : dump_printf_loc (MSG_NOTE, vect_location,
1199 : : "created new init_stmt: %G", new_stmt);
1200 : 6144 : }
1201 : :
1202 : : /* Function vect_init_vector.
1203 : :
1204 : : Insert a new stmt (INIT_STMT) that initializes a new variable of type
1205 : : TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1206 : : vector type a vector with all elements equal to VAL is created first.
1207 : : Place the initialization at GSI if it is not NULL. Otherwise, place the
1208 : : initialization at the loop preheader.
1209 : : Return the DEF of INIT_STMT.
1210 : : It will be used in the vectorization of STMT_INFO. */
1211 : :
1212 : : tree
1213 : 4431 : vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
1214 : : gimple_stmt_iterator *gsi)
1215 : : {
1216 : 4431 : gimple *init_stmt;
1217 : 4431 : tree new_temp;
1218 : :
1219 : : /* We abuse this function to push sth to a SSA name with initial 'val'. */
1220 : 4431 : if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1221 : : {
1222 : 1327 : gcc_assert (VECTOR_TYPE_P (type));
1223 : 1327 : if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1224 : : {
1225 : : /* Scalar boolean value should be transformed into
1226 : : all zeros or all ones value before building a vector. */
1227 : 14 : if (VECTOR_BOOLEAN_TYPE_P (type))
1228 : : {
1229 : 0 : tree true_val = build_all_ones_cst (TREE_TYPE (type));
1230 : 0 : tree false_val = build_zero_cst (TREE_TYPE (type));
1231 : :
1232 : 0 : if (CONSTANT_CLASS_P (val))
1233 : 0 : val = integer_zerop (val) ? false_val : true_val;
1234 : : else
1235 : : {
1236 : 0 : new_temp = make_ssa_name (TREE_TYPE (type));
1237 : 0 : init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1238 : : val, true_val, false_val);
1239 : 0 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1240 : 0 : val = new_temp;
1241 : : }
1242 : : }
1243 : : else
1244 : : {
1245 : 14 : gimple_seq stmts = NULL;
1246 : 14 : if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1247 : 12 : val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
1248 : 12 : TREE_TYPE (type), val);
1249 : : else
1250 : : /* ??? Condition vectorization expects us to do
1251 : : promotion of invariant/external defs. */
1252 : 2 : val = gimple_convert (&stmts, TREE_TYPE (type), val);
1253 : 28 : for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
1254 : 28 : !gsi_end_p (gsi2); )
1255 : : {
1256 : 14 : init_stmt = gsi_stmt (gsi2);
1257 : 14 : gsi_remove (&gsi2, false);
1258 : 14 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1259 : : }
1260 : : }
1261 : : }
1262 : 1327 : val = build_vector_from_val (type, val);
1263 : : }
1264 : :
1265 : 4431 : new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1266 : 4431 : init_stmt = gimple_build_assign (new_temp, val);
1267 : 4431 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1268 : 4431 : return new_temp;
1269 : : }
1270 : :
1271 : :
1272 : : /* Function vect_get_vec_defs_for_operand.
1273 : :
1274 : : OP is an operand in STMT_VINFO. This function returns a vector of
1275 : : NCOPIES defs that will be used in the vectorized stmts for STMT_VINFO.
1276 : :
1277 : : In the case that OP is an SSA_NAME which is defined in the loop, then
1278 : : STMT_VINFO_VEC_STMTS of the defining stmt holds the relevant defs.
1279 : :
1280 : : In case OP is an invariant or constant, a new stmt that creates a vector def
1281 : : needs to be introduced. VECTYPE may be used to specify a required type for
1282 : : vector invariant. */
1283 : :
1284 : : void
1285 : 0 : vect_get_vec_defs_for_operand (vec_info *vinfo, stmt_vec_info stmt_vinfo,
1286 : : unsigned ncopies,
1287 : : tree op, vec<tree> *vec_oprnds, tree vectype)
1288 : : {
1289 : 0 : gimple *def_stmt;
1290 : 0 : enum vect_def_type dt;
1291 : 0 : bool is_simple_use;
1292 : 0 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1293 : :
1294 : 0 : if (dump_enabled_p ())
1295 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1296 : : "vect_get_vec_defs_for_operand: %T\n", op);
1297 : :
1298 : 0 : stmt_vec_info def_stmt_info;
1299 : 0 : is_simple_use = vect_is_simple_use (op, loop_vinfo, &dt,
1300 : : &def_stmt_info, &def_stmt);
1301 : 0 : gcc_assert (is_simple_use);
1302 : 0 : if (def_stmt && dump_enabled_p ())
1303 : 0 : dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = %G", def_stmt);
1304 : :
1305 : 0 : vec_oprnds->create (ncopies);
1306 : 0 : if (dt == vect_constant_def || dt == vect_external_def)
1307 : : {
1308 : 0 : tree stmt_vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1309 : 0 : tree vector_type;
1310 : :
1311 : 0 : if (vectype)
1312 : : vector_type = vectype;
1313 : 0 : else if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
1314 : 0 : && VECTOR_BOOLEAN_TYPE_P (stmt_vectype))
1315 : 0 : vector_type = truth_type_for (stmt_vectype);
1316 : : else
1317 : 0 : vector_type = get_vectype_for_scalar_type (loop_vinfo, TREE_TYPE (op));
1318 : :
1319 : 0 : gcc_assert (vector_type);
1320 : : /* A masked load can have a default SSA definition as else operand.
1321 : : We should "vectorize" this instead of creating a duplicate from the
1322 : : scalar default. */
1323 : 0 : tree vop;
1324 : 0 : if (TREE_CODE (op) == SSA_NAME
1325 : 0 : && SSA_NAME_IS_DEFAULT_DEF (op)
1326 : 0 : && VAR_P (SSA_NAME_VAR (op)))
1327 : 0 : vop = get_or_create_ssa_default_def (cfun,
1328 : : create_tmp_var (vector_type));
1329 : : else
1330 : 0 : vop = vect_init_vector (vinfo, stmt_vinfo, op, vector_type, NULL);
1331 : 0 : while (ncopies--)
1332 : 0 : vec_oprnds->quick_push (vop);
1333 : : }
1334 : : else
1335 : : {
1336 : 0 : def_stmt_info = vect_stmt_to_vectorize (def_stmt_info);
1337 : 0 : gcc_assert (STMT_VINFO_VEC_STMTS (def_stmt_info).length () == ncopies);
1338 : 0 : for (unsigned i = 0; i < ncopies; ++i)
1339 : 0 : vec_oprnds->quick_push (gimple_get_lhs
1340 : 0 : (STMT_VINFO_VEC_STMTS (def_stmt_info)[i]));
1341 : : }
1342 : 0 : }
1343 : :
1344 : :
1345 : : /* Get vectorized definitions for OP0 and OP1. */
1346 : :
1347 : : void
1348 : 708048 : vect_get_vec_defs (vec_info *vinfo, stmt_vec_info stmt_info, slp_tree slp_node,
1349 : : unsigned ncopies,
1350 : : tree op0, tree vectype0, vec<tree> *vec_oprnds0,
1351 : : tree op1, tree vectype1, vec<tree> *vec_oprnds1,
1352 : : tree op2, tree vectype2, vec<tree> *vec_oprnds2,
1353 : : tree op3, tree vectype3, vec<tree> *vec_oprnds3)
1354 : : {
1355 : 708048 : if (slp_node)
1356 : : {
1357 : 708048 : if (op0)
1358 : 706633 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
1359 : 708048 : if (op1)
1360 : 126535 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
1361 : 708048 : if (op2)
1362 : 6948 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
1363 : 708048 : if (op3)
1364 : 0 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
1365 : : }
1366 : : else
1367 : : {
1368 : 0 : if (op0)
1369 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1370 : : op0, vec_oprnds0, vectype0);
1371 : 0 : if (op1)
1372 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1373 : : op1, vec_oprnds1, vectype1);
1374 : 0 : if (op2)
1375 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1376 : : op2, vec_oprnds2, vectype2);
1377 : 0 : if (op3)
1378 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1379 : : op3, vec_oprnds3, vectype3);
1380 : : }
1381 : 708048 : }
1382 : :
1383 : : void
1384 : 672268 : vect_get_vec_defs (vec_info *vinfo, stmt_vec_info stmt_info, slp_tree slp_node,
1385 : : unsigned ncopies,
1386 : : tree op0, vec<tree> *vec_oprnds0,
1387 : : tree op1, vec<tree> *vec_oprnds1,
1388 : : tree op2, vec<tree> *vec_oprnds2,
1389 : : tree op3, vec<tree> *vec_oprnds3)
1390 : : {
1391 : 672268 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
1392 : : op0, NULL_TREE, vec_oprnds0,
1393 : : op1, NULL_TREE, vec_oprnds1,
1394 : : op2, NULL_TREE, vec_oprnds2,
1395 : : op3, NULL_TREE, vec_oprnds3);
1396 : 672268 : }
1397 : :
1398 : : /* Helper function called by vect_finish_replace_stmt and
1399 : : vect_finish_stmt_generation. Set the location of the new
1400 : : statement and create and return a stmt_vec_info for it. */
1401 : :
1402 : : static void
1403 : 1284939 : vect_finish_stmt_generation_1 (vec_info *,
1404 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1405 : : {
1406 : 1284939 : if (dump_enabled_p ())
1407 : 135217 : dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
1408 : :
1409 : 1284939 : if (stmt_info)
1410 : : {
1411 : 1254649 : gimple_set_location (vec_stmt, gimple_location (stmt_info->stmt));
1412 : :
1413 : : /* While EH edges will generally prevent vectorization, stmt might
1414 : : e.g. be in a must-not-throw region. Ensure newly created stmts
1415 : : that could throw are part of the same region. */
1416 : 1254649 : int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
1417 : 1254649 : if (lp_nr != 0 && stmt_could_throw_p (cfun, vec_stmt))
1418 : 66 : add_stmt_to_eh_lp (vec_stmt, lp_nr);
1419 : : }
1420 : : else
1421 : 30290 : gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
1422 : 1284939 : }
1423 : :
1424 : : /* Replace the scalar statement STMT_INFO with a new vector statement VEC_STMT,
1425 : : which sets the same scalar result as STMT_INFO did. Create and return a
1426 : : stmt_vec_info for VEC_STMT. */
1427 : :
1428 : : void
1429 : 834 : vect_finish_replace_stmt (vec_info *vinfo,
1430 : : stmt_vec_info stmt_info, gimple *vec_stmt)
1431 : : {
1432 : 834 : gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
1433 : 834 : gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
1434 : :
1435 : 834 : gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
1436 : 834 : gsi_replace (&gsi, vec_stmt, true);
1437 : :
1438 : 834 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1439 : 834 : }
1440 : :
1441 : : /* Add VEC_STMT to the vectorized implementation of STMT_INFO and insert it
1442 : : before *GSI. Create and return a stmt_vec_info for VEC_STMT. */
1443 : :
1444 : : void
1445 : 1284105 : vect_finish_stmt_generation (vec_info *vinfo,
1446 : : stmt_vec_info stmt_info, gimple *vec_stmt,
1447 : : gimple_stmt_iterator *gsi)
1448 : : {
1449 : 1284105 : gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
1450 : :
1451 : 1284105 : if (!gsi_end_p (*gsi)
1452 : 2567227 : && gimple_has_mem_ops (vec_stmt))
1453 : : {
1454 : 1283122 : gimple *at_stmt = gsi_stmt (*gsi);
1455 : 1283122 : tree vuse = gimple_vuse (at_stmt);
1456 : 1276917 : if (vuse && TREE_CODE (vuse) == SSA_NAME)
1457 : : {
1458 : 1164503 : tree vdef = gimple_vdef (at_stmt);
1459 : 1164503 : gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1460 : 1164503 : gimple_set_modified (vec_stmt, true);
1461 : : /* If we have an SSA vuse and insert a store, update virtual
1462 : : SSA form to avoid triggering the renamer. Do so only
1463 : : if we can easily see all uses - which is what almost always
1464 : : happens with the way vectorized stmts are inserted. */
1465 : 745285 : if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1466 : 1909752 : && ((is_gimple_assign (vec_stmt)
1467 : 744603 : && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1468 : 62419 : || (is_gimple_call (vec_stmt)
1469 : 646 : && (!(gimple_call_flags (vec_stmt)
1470 : 646 : & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
1471 : 1 : || (gimple_call_lhs (vec_stmt)
1472 : 1 : && !is_gimple_reg (gimple_call_lhs (vec_stmt)))))))
1473 : : {
1474 : 683475 : tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1475 : 683475 : gimple_set_vdef (vec_stmt, new_vdef);
1476 : 683475 : SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1477 : : }
1478 : : }
1479 : : }
1480 : 1284105 : gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1481 : 1284105 : vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1482 : 1284105 : }
1483 : :
1484 : : /* We want to vectorize a call to combined function CFN with function
1485 : : decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1486 : : as the types of all inputs. Check whether this is possible using
1487 : : an internal function, returning its code if so or IFN_LAST if not. */
1488 : :
1489 : : static internal_fn
1490 : 11069 : vectorizable_internal_function (combined_fn cfn, tree fndecl,
1491 : : tree vectype_out, tree vectype_in)
1492 : : {
1493 : 11069 : internal_fn ifn;
1494 : 11069 : if (internal_fn_p (cfn))
1495 : 8717 : ifn = as_internal_fn (cfn);
1496 : : else
1497 : 2352 : ifn = associated_internal_fn (fndecl);
1498 : 11069 : if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1499 : : {
1500 : 7653 : const direct_internal_fn_info &info = direct_internal_fn (ifn);
1501 : 7653 : if (info.vectorizable)
1502 : : {
1503 : 7653 : bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
1504 : 7653 : tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1505 : 7653 : tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
1506 : :
1507 : : /* The type size of both the vectype_in and vectype_out should be
1508 : : exactly the same when vectype_out isn't participating the optab.
1509 : : While there is no restriction for type size when vectype_out
1510 : : is part of the optab query. */
1511 : 7653 : if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
1512 : : return IFN_LAST;
1513 : :
1514 : 7633 : if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1515 : : OPTIMIZE_FOR_SPEED))
1516 : : return ifn;
1517 : : }
1518 : : }
1519 : : return IFN_LAST;
1520 : : }
1521 : :
1522 : :
1523 : : static tree permute_vec_elements (vec_info *, tree, tree, tree, stmt_vec_info,
1524 : : gimple_stmt_iterator *);
1525 : :
1526 : : /* Check whether a load or store statement in the loop described by
1527 : : LOOP_VINFO is possible in a loop using partial vectors. This is
1528 : : testing whether the vectorizer pass has the appropriate support,
1529 : : as well as whether the target does.
1530 : :
1531 : : VLS_TYPE says whether the statement is a load or store and VECTYPE
1532 : : is the type of the vector being loaded or stored. SLP_NODE is the SLP
1533 : : node that contains the statement, or null if none. MEMORY_ACCESS_TYPE
1534 : : says how the load or store is going to be implemented and GROUP_SIZE
1535 : : is the number of load or store statements in the containing group.
1536 : : If the access is a gather load or scatter store, GS_INFO describes
1537 : : its arguments. If the load or store is conditional, SCALAR_MASK is the
1538 : : condition under which it occurs.
1539 : :
1540 : : Clear LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P if a loop using partial
1541 : : vectors is not supported, otherwise record the required rgroup control
1542 : : types.
1543 : :
1544 : : If partial vectors can be used and ELSVALS is nonzero the supported
1545 : : else values will be added to the vector ELSVALS points to. */
1546 : :
1547 : : static void
1548 : 124 : check_load_store_for_partial_vectors (loop_vec_info loop_vinfo, tree vectype,
1549 : : slp_tree slp_node,
1550 : : vec_load_store_type vls_type,
1551 : : int group_size,
1552 : : vect_memory_access_type
1553 : : memory_access_type,
1554 : : gather_scatter_info *gs_info,
1555 : : tree scalar_mask,
1556 : : vec<int> *elsvals = nullptr)
1557 : : {
1558 : : /* Invariant loads need no special support. */
1559 : 124 : if (memory_access_type == VMAT_INVARIANT)
1560 : 0 : return;
1561 : :
1562 : 124 : unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node, vectype);
1563 : 124 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1564 : 124 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
1565 : 124 : machine_mode vecmode = TYPE_MODE (vectype);
1566 : 124 : bool is_load = (vls_type == VLS_LOAD);
1567 : 124 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
1568 : : {
1569 : 0 : if (slp_node)
1570 : 0 : nvectors /= group_size;
1571 : 0 : internal_fn ifn
1572 : 0 : = (is_load ? vect_load_lanes_supported (vectype, group_size, true,
1573 : : elsvals)
1574 : 0 : : vect_store_lanes_supported (vectype, group_size, true));
1575 : 0 : if (ifn == IFN_MASK_LEN_LOAD_LANES || ifn == IFN_MASK_LEN_STORE_LANES)
1576 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1577 : 0 : else if (ifn == IFN_MASK_LOAD_LANES || ifn == IFN_MASK_STORE_LANES)
1578 : 0 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1579 : : scalar_mask);
1580 : : else
1581 : : {
1582 : 0 : if (dump_enabled_p ())
1583 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1584 : : "can't operate on partial vectors because"
1585 : : " the target doesn't have an appropriate"
1586 : : " load/store-lanes instruction.\n");
1587 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1588 : : }
1589 : 0 : return;
1590 : : }
1591 : :
1592 : 124 : if (memory_access_type == VMAT_GATHER_SCATTER)
1593 : : {
1594 : 0 : internal_fn ifn = (is_load
1595 : 0 : ? IFN_MASK_GATHER_LOAD
1596 : : : IFN_MASK_SCATTER_STORE);
1597 : 0 : internal_fn len_ifn = (is_load
1598 : : ? IFN_MASK_LEN_GATHER_LOAD
1599 : : : IFN_MASK_LEN_SCATTER_STORE);
1600 : 0 : if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
1601 : : gs_info->memory_type,
1602 : : gs_info->offset_vectype,
1603 : : gs_info->scale,
1604 : : elsvals))
1605 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1606 : 0 : else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
1607 : : gs_info->memory_type,
1608 : : gs_info->offset_vectype,
1609 : : gs_info->scale,
1610 : : elsvals))
1611 : 0 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1612 : : scalar_mask);
1613 : : else
1614 : : {
1615 : 0 : if (dump_enabled_p ())
1616 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1617 : : "can't operate on partial vectors because"
1618 : : " the target doesn't have an appropriate"
1619 : : " gather load or scatter store instruction.\n");
1620 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1621 : : }
1622 : 0 : return;
1623 : : }
1624 : :
1625 : 124 : if (memory_access_type != VMAT_CONTIGUOUS
1626 : 124 : && memory_access_type != VMAT_CONTIGUOUS_PERMUTE)
1627 : : {
1628 : : /* Element X of the data must come from iteration i * VF + X of the
1629 : : scalar loop. We need more work to support other mappings. */
1630 : 0 : if (dump_enabled_p ())
1631 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1632 : : "can't operate on partial vectors because an"
1633 : : " access isn't contiguous.\n");
1634 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1635 : 0 : return;
1636 : : }
1637 : :
1638 : 124 : if (!VECTOR_MODE_P (vecmode))
1639 : : {
1640 : 0 : if (dump_enabled_p ())
1641 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1642 : : "can't operate on partial vectors when emulating"
1643 : : " vector operations.\n");
1644 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1645 : 0 : return;
1646 : : }
1647 : :
1648 : : /* We might load more scalars than we need for permuting SLP loads.
1649 : : We checked in get_group_load_store_type that the extra elements
1650 : : don't leak into a new vector. */
1651 : 218 : auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
1652 : : {
1653 : 94 : unsigned int nvectors;
1654 : 188 : if (can_div_away_from_zero_p (size, nunits, &nvectors))
1655 : 94 : return nvectors;
1656 : : gcc_unreachable ();
1657 : : };
1658 : :
1659 : 124 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1660 : 124 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1661 : 124 : machine_mode mask_mode;
1662 : 124 : machine_mode vmode;
1663 : 124 : bool using_partial_vectors_p = false;
1664 : 124 : if (get_len_load_store_mode
1665 : 124 : (vecmode, is_load, nullptr, elsvals).exists (&vmode))
1666 : : {
1667 : 0 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1668 : 0 : unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
1669 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
1670 : 0 : using_partial_vectors_p = true;
1671 : : }
1672 : 218 : else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
1673 : 124 : && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
1674 : : elsvals))
1675 : : {
1676 : 94 : nvectors = group_memory_nvectors (group_size * vf, nunits);
1677 : 94 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
1678 : 94 : using_partial_vectors_p = true;
1679 : : }
1680 : :
1681 : 94 : if (!using_partial_vectors_p)
1682 : : {
1683 : 30 : if (dump_enabled_p ())
1684 : 9 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1685 : : "can't operate on partial vectors because the"
1686 : : " target doesn't have the appropriate partial"
1687 : : " vectorization load or store.\n");
1688 : 30 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1689 : : }
1690 : : }
1691 : :
1692 : : /* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1693 : : form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1694 : : that needs to be applied to all loads and stores in a vectorized loop.
1695 : : Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
1696 : : otherwise return VEC_MASK & LOOP_MASK.
1697 : :
1698 : : MASK_TYPE is the type of both masks. If new statements are needed,
1699 : : insert them before GSI. */
1700 : :
1701 : : tree
1702 : 1267 : prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
1703 : : tree vec_mask, gimple_stmt_iterator *gsi)
1704 : : {
1705 : 1267 : gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1706 : 1267 : if (!loop_mask)
1707 : : return vec_mask;
1708 : :
1709 : 36 : gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1710 : :
1711 : 36 : if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
1712 : : return vec_mask;
1713 : :
1714 : 36 : tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1715 : 36 : gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1716 : : vec_mask, loop_mask);
1717 : :
1718 : 36 : gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1719 : 36 : return and_res;
1720 : : }
1721 : :
1722 : : /* Determine whether we can use a gather load or scatter store to vectorize
1723 : : strided load or store STMT_INFO by truncating the current offset to a
1724 : : smaller width. We need to be able to construct an offset vector:
1725 : :
1726 : : { 0, X, X*2, X*3, ... }
1727 : :
1728 : : without loss of precision, where X is STMT_INFO's DR_STEP.
1729 : :
1730 : : Return true if this is possible, describing the gather load or scatter
1731 : : store in GS_INFO. MASKED_P is true if the load or store is conditional.
1732 : :
1733 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1734 : : else values will be stored in the vector ELSVALS points to. */
1735 : :
1736 : : static bool
1737 : 54402 : vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info,
1738 : : loop_vec_info loop_vinfo, bool masked_p,
1739 : : gather_scatter_info *gs_info,
1740 : : vec<int> *elsvals)
1741 : : {
1742 : 54402 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1743 : 54402 : data_reference *dr = dr_info->dr;
1744 : 54402 : tree step = DR_STEP (dr);
1745 : 54402 : if (TREE_CODE (step) != INTEGER_CST)
1746 : : {
1747 : : /* ??? Perhaps we could use range information here? */
1748 : 29158 : if (dump_enabled_p ())
1749 : 394 : dump_printf_loc (MSG_NOTE, vect_location,
1750 : : "cannot truncate variable step.\n");
1751 : 29158 : return false;
1752 : : }
1753 : :
1754 : : /* Get the number of bits in an element. */
1755 : 25244 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1756 : 25244 : scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
1757 : 25244 : unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
1758 : :
1759 : : /* Set COUNT to the upper limit on the number of elements - 1.
1760 : : Start with the maximum vectorization factor. */
1761 : 25244 : unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
1762 : :
1763 : : /* Try lowering COUNT to the number of scalar latch iterations. */
1764 : 25244 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1765 : 25244 : widest_int max_iters;
1766 : 25244 : if (max_loop_iterations (loop, &max_iters)
1767 : 50488 : && max_iters < count)
1768 : 2013 : count = max_iters.to_shwi ();
1769 : :
1770 : : /* Try scales of 1 and the element size. */
1771 : 25244 : unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
1772 : 25244 : wi::overflow_type overflow = wi::OVF_NONE;
1773 : 75732 : for (int i = 0; i < 2; ++i)
1774 : : {
1775 : 50488 : unsigned int scale = scales[i];
1776 : 50488 : widest_int factor;
1777 : 50488 : if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
1778 : 0 : continue;
1779 : :
1780 : : /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE. */
1781 : 50488 : widest_int range = wi::mul (count, factor, SIGNED, &overflow);
1782 : 50488 : if (overflow)
1783 : 0 : continue;
1784 : 50488 : signop sign = range >= 0 ? UNSIGNED : SIGNED;
1785 : 50488 : unsigned int min_offset_bits = wi::min_precision (range, sign);
1786 : :
1787 : : /* Find the narrowest viable offset type. */
1788 : 50488 : unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
1789 : 50488 : tree offset_type = build_nonstandard_integer_type (offset_bits,
1790 : : sign == UNSIGNED);
1791 : :
1792 : : /* See whether the target supports the operation with an offset
1793 : : no narrower than OFFSET_TYPE. */
1794 : 50488 : tree memory_type = TREE_TYPE (DR_REF (dr));
1795 : 50488 : if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
1796 : : vectype, memory_type, offset_type, scale,
1797 : : &gs_info->ifn, &gs_info->offset_vectype,
1798 : : elsvals)
1799 : 50488 : || gs_info->ifn == IFN_LAST)
1800 : 50488 : continue;
1801 : :
1802 : 0 : gs_info->decl = NULL_TREE;
1803 : : /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
1804 : : but we don't need to store that here. */
1805 : 0 : gs_info->base = NULL_TREE;
1806 : 0 : gs_info->element_type = TREE_TYPE (vectype);
1807 : 0 : gs_info->offset = fold_convert (offset_type, step);
1808 : 0 : gs_info->offset_dt = vect_constant_def;
1809 : 0 : gs_info->scale = scale;
1810 : 0 : gs_info->memory_type = memory_type;
1811 : 0 : return true;
1812 : 100976 : }
1813 : :
1814 : 25244 : if (overflow && dump_enabled_p ())
1815 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1816 : : "truncating gather/scatter offset to %d bits"
1817 : : " might change its value.\n", element_bits);
1818 : :
1819 : : return false;
1820 : 25244 : }
1821 : :
1822 : : /* Return true if we can use gather/scatter internal functions to
1823 : : vectorize STMT_INFO, which is a grouped or strided load or store.
1824 : : MASKED_P is true if load or store is conditional. When returning
1825 : : true, fill in GS_INFO with the information required to perform the
1826 : : operation.
1827 : :
1828 : : If we can use gather/scatter and ELSVALS is nonzero the supported
1829 : : else values will be stored in the vector ELSVALS points to. */
1830 : :
1831 : : static bool
1832 : 54402 : vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info,
1833 : : loop_vec_info loop_vinfo, bool masked_p,
1834 : : gather_scatter_info *gs_info,
1835 : : vec<int> *elsvals)
1836 : : {
1837 : 54402 : if (!vect_check_gather_scatter (stmt_info, loop_vinfo, gs_info, elsvals)
1838 : 54402 : || gs_info->ifn == IFN_LAST)
1839 : 54402 : return vect_truncate_gather_scatter_offset (stmt_info, loop_vinfo,
1840 : 54402 : masked_p, gs_info, elsvals);
1841 : :
1842 : 0 : tree old_offset_type = TREE_TYPE (gs_info->offset);
1843 : 0 : tree new_offset_type = TREE_TYPE (gs_info->offset_vectype);
1844 : :
1845 : 0 : gcc_assert (TYPE_PRECISION (new_offset_type)
1846 : : >= TYPE_PRECISION (old_offset_type));
1847 : 0 : gs_info->offset = fold_convert (new_offset_type, gs_info->offset);
1848 : :
1849 : 0 : if (dump_enabled_p ())
1850 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
1851 : : "using gather/scatter for strided/grouped access,"
1852 : : " scale = %d\n", gs_info->scale);
1853 : :
1854 : : return true;
1855 : : }
1856 : :
1857 : : /* STMT_INFO is a non-strided load or store, meaning that it accesses
1858 : : elements with a known constant step. Return -1 if that step
1859 : : is negative, 0 if it is zero, and 1 if it is greater than zero. */
1860 : :
1861 : : int
1862 : 1992036 : compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
1863 : : {
1864 : 1992036 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1865 : 1992036 : return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
1866 : 1992036 : size_zero_node);
1867 : : }
1868 : :
1869 : : /* If the target supports a permute mask that reverses the elements in
1870 : : a vector of type VECTYPE, return that mask, otherwise return null. */
1871 : :
1872 : : tree
1873 : 10860 : perm_mask_for_reverse (tree vectype)
1874 : : {
1875 : 10860 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1876 : :
1877 : : /* The encoding has a single stepped pattern. */
1878 : 10860 : vec_perm_builder sel (nunits, 1, 3);
1879 : 43440 : for (int i = 0; i < 3; ++i)
1880 : 32580 : sel.quick_push (nunits - 1 - i);
1881 : :
1882 : 10860 : vec_perm_indices indices (sel, 1, nunits);
1883 : 10860 : if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
1884 : : indices))
1885 : : return NULL_TREE;
1886 : 10064 : return vect_gen_perm_mask_checked (vectype, indices);
1887 : 10860 : }
1888 : :
1889 : : /* A subroutine of get_load_store_type, with a subset of the same
1890 : : arguments. Handle the case where STMT_INFO is a load or store that
1891 : : accesses consecutive elements with a negative step. Sets *POFFSET
1892 : : to the offset to be applied to the DR for the first access. */
1893 : :
1894 : : static vect_memory_access_type
1895 : 11337 : get_negative_load_store_type (vec_info *vinfo,
1896 : : stmt_vec_info stmt_info, tree vectype,
1897 : : vec_load_store_type vls_type,
1898 : : unsigned int ncopies, poly_int64 *poffset)
1899 : : {
1900 : 11337 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1901 : 11337 : dr_alignment_support alignment_support_scheme;
1902 : :
1903 : 11337 : if (ncopies > 1)
1904 : : {
1905 : 0 : if (dump_enabled_p ())
1906 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1907 : : "multiple types with negative step.\n");
1908 : 0 : return VMAT_ELEMENTWISE;
1909 : : }
1910 : :
1911 : : /* For backward running DRs the first access in vectype actually is
1912 : : N-1 elements before the address of the DR. */
1913 : 11337 : *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
1914 : 11337 : * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1915 : :
1916 : 11337 : int misalignment = dr_misalignment (dr_info, vectype, *poffset);
1917 : 11337 : alignment_support_scheme
1918 : 11337 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
1919 : 11337 : if (alignment_support_scheme != dr_aligned
1920 : 11337 : && alignment_support_scheme != dr_unaligned_supported)
1921 : : {
1922 : 2116 : if (dump_enabled_p ())
1923 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1924 : : "negative step but alignment required.\n");
1925 : 2116 : *poffset = 0;
1926 : 2116 : return VMAT_ELEMENTWISE;
1927 : : }
1928 : :
1929 : 9221 : if (vls_type == VLS_STORE_INVARIANT)
1930 : : {
1931 : 904 : if (dump_enabled_p ())
1932 : 42 : dump_printf_loc (MSG_NOTE, vect_location,
1933 : : "negative step with invariant source;"
1934 : : " no permute needed.\n");
1935 : 904 : return VMAT_CONTIGUOUS_DOWN;
1936 : : }
1937 : :
1938 : 8317 : if (!perm_mask_for_reverse (vectype))
1939 : : {
1940 : 796 : if (dump_enabled_p ())
1941 : 48 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1942 : : "negative step and reversing not supported.\n");
1943 : 796 : *poffset = 0;
1944 : 796 : return VMAT_ELEMENTWISE;
1945 : : }
1946 : :
1947 : : return VMAT_CONTIGUOUS_REVERSE;
1948 : : }
1949 : :
1950 : : /* STMT_INFO is either a masked or unconditional store. Return the value
1951 : : being stored. */
1952 : :
1953 : : tree
1954 : 5458719 : vect_get_store_rhs (stmt_vec_info stmt_info)
1955 : : {
1956 : 5458719 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
1957 : : {
1958 : 5457004 : gcc_assert (gimple_assign_single_p (assign));
1959 : 5457004 : return gimple_assign_rhs1 (assign);
1960 : : }
1961 : 1715 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
1962 : : {
1963 : 1715 : internal_fn ifn = gimple_call_internal_fn (call);
1964 : 1715 : int index = internal_fn_stored_value_index (ifn);
1965 : 1715 : gcc_assert (index >= 0);
1966 : 1715 : return gimple_call_arg (call, index);
1967 : : }
1968 : 0 : gcc_unreachable ();
1969 : : }
1970 : :
1971 : : /* Function VECTOR_VECTOR_COMPOSITION_TYPE
1972 : :
1973 : : This function returns a vector type which can be composed with NETLS pieces,
1974 : : whose type is recorded in PTYPE. VTYPE should be a vector type, and has the
1975 : : same vector size as the return vector. It checks target whether supports
1976 : : pieces-size vector mode for construction firstly, if target fails to, check
1977 : : pieces-size scalar mode for construction further. It returns NULL_TREE if
1978 : : fails to find the available composition.
1979 : :
1980 : : For example, for (vtype=V16QI, nelts=4), we can probably get:
1981 : : - V16QI with PTYPE V4QI.
1982 : : - V4SI with PTYPE SI.
1983 : : - NULL_TREE. */
1984 : :
1985 : : static tree
1986 : 10539 : vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype)
1987 : : {
1988 : 10539 : gcc_assert (VECTOR_TYPE_P (vtype));
1989 : 10539 : gcc_assert (known_gt (nelts, 0U));
1990 : :
1991 : 10539 : machine_mode vmode = TYPE_MODE (vtype);
1992 : 10539 : if (!VECTOR_MODE_P (vmode))
1993 : : return NULL_TREE;
1994 : :
1995 : : /* When we are asked to compose the vector from its components let
1996 : : that happen directly. */
1997 : 10539 : if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
1998 : : {
1999 : 5313 : *ptype = TREE_TYPE (vtype);
2000 : 5313 : return vtype;
2001 : : }
2002 : :
2003 : 10452 : poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
2004 : 5226 : unsigned int pbsize;
2005 : 5226 : if (constant_multiple_p (vbsize, nelts, &pbsize))
2006 : : {
2007 : : /* First check if vec_init optab supports construction from
2008 : : vector pieces directly. */
2009 : 5226 : scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
2010 : 10452 : poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
2011 : 5226 : machine_mode rmode;
2012 : 5226 : if (related_vector_mode (vmode, elmode, inelts).exists (&rmode)
2013 : 4714 : && (convert_optab_handler (vec_init_optab, vmode, rmode)
2014 : : != CODE_FOR_nothing))
2015 : : {
2016 : 3992 : *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
2017 : 3992 : return vtype;
2018 : : }
2019 : :
2020 : : /* Otherwise check if exists an integer type of the same piece size and
2021 : : if vec_init optab supports construction from it directly. */
2022 : 1234 : if (int_mode_for_size (pbsize, 0).exists (&elmode)
2023 : 1234 : && related_vector_mode (vmode, elmode, nelts).exists (&rmode)
2024 : 1182 : && (convert_optab_handler (vec_init_optab, rmode, elmode)
2025 : : != CODE_FOR_nothing))
2026 : : {
2027 : 1182 : *ptype = build_nonstandard_integer_type (pbsize, 1);
2028 : 1182 : return build_vector_type (*ptype, nelts);
2029 : : }
2030 : : }
2031 : :
2032 : : return NULL_TREE;
2033 : : }
2034 : :
2035 : : /* A subroutine of get_load_store_type, with a subset of the same
2036 : : arguments. Handle the case where STMT_INFO is part of a grouped load
2037 : : or store.
2038 : :
2039 : : For stores, the statements in the group are all consecutive
2040 : : and there is no gap at the end. For loads, the statements in the
2041 : : group might not be consecutive; there can be gaps between statements
2042 : : as well as at the end.
2043 : :
2044 : : If we can use gather/scatter and ELSVALS is nonzero the supported
2045 : : else values will be stored in the vector ELSVALS points to.
2046 : : */
2047 : :
2048 : : static bool
2049 : 1955716 : get_group_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2050 : : tree vectype, slp_tree slp_node,
2051 : : bool masked_p, vec_load_store_type vls_type,
2052 : : vect_memory_access_type *memory_access_type,
2053 : : poly_int64 *poffset,
2054 : : dr_alignment_support *alignment_support_scheme,
2055 : : int *misalignment,
2056 : : gather_scatter_info *gs_info,
2057 : : internal_fn *lanes_ifn,
2058 : : vec<int> *elsvals)
2059 : : {
2060 : 1955716 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2061 : 527434 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2062 : 1955716 : stmt_vec_info first_stmt_info;
2063 : 1955716 : unsigned int group_size;
2064 : 1955716 : unsigned HOST_WIDE_INT gap;
2065 : 1955716 : bool single_element_p;
2066 : 1955716 : poly_int64 neg_ldst_offset = 0;
2067 : 1955716 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2068 : : {
2069 : 1515512 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2070 : 1515512 : group_size = DR_GROUP_SIZE (first_stmt_info);
2071 : 1515512 : gap = DR_GROUP_GAP (first_stmt_info);
2072 : 1515512 : single_element_p = (stmt_info == first_stmt_info
2073 : 1515512 : && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2074 : : }
2075 : : else
2076 : : {
2077 : : first_stmt_info = stmt_info;
2078 : : group_size = 1;
2079 : : gap = 0;
2080 : : single_element_p = true;
2081 : : }
2082 : 1955716 : dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
2083 : 1955716 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2084 : :
2085 : : /* True if the vectorized statements would access beyond the last
2086 : : statement in the group. */
2087 : 1955716 : bool overrun_p = false;
2088 : :
2089 : : /* True if we can cope with such overrun by peeling for gaps, so that
2090 : : there is at least one final scalar iteration after the vector loop. */
2091 : 3911432 : bool can_overrun_p = (!masked_p
2092 : 1955716 : && vls_type == VLS_LOAD
2093 : 575941 : && loop_vinfo
2094 : 2309403 : && !loop->inner);
2095 : :
2096 : : /* There can only be a gap at the end of the group if the stride is
2097 : : known at compile time. */
2098 : 1955716 : gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
2099 : :
2100 : : /* For SLP vectorization we directly vectorize a subchain
2101 : : without permutation. */
2102 : 1955716 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2103 : 1884105 : first_dr_info = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
2104 : 1955716 : if (STMT_VINFO_STRIDED_P (first_stmt_info))
2105 : : /* Try to use consecutive accesses of as many elements as possible,
2106 : : separated by the stride, until we have a complete vector.
2107 : : Fall back to scalar accesses if that isn't possible. */
2108 : 46586 : *memory_access_type = VMAT_STRIDED_SLP;
2109 : : else
2110 : : {
2111 : 1909130 : int cmp = compare_step_with_zero (vinfo, stmt_info);
2112 : 1909130 : if (cmp < 0)
2113 : : {
2114 : 11577 : if (single_element_p)
2115 : : /* ??? The VMAT_CONTIGUOUS_REVERSE code generation is
2116 : : only correct for single element "interleaving" SLP. */
2117 : 11337 : *memory_access_type = get_negative_load_store_type
2118 : 11337 : (vinfo, stmt_info, vectype, vls_type, 1,
2119 : : &neg_ldst_offset);
2120 : : else
2121 : : {
2122 : : /* Try to use consecutive accesses of DR_GROUP_SIZE elements,
2123 : : separated by the stride, until we have a complete vector.
2124 : : Fall back to scalar accesses if that isn't possible. */
2125 : 240 : if (multiple_p (nunits, group_size))
2126 : 210 : *memory_access_type = VMAT_STRIDED_SLP;
2127 : : else
2128 : 30 : *memory_access_type = VMAT_ELEMENTWISE;
2129 : : }
2130 : : }
2131 : 1897553 : else if (cmp == 0 && loop_vinfo)
2132 : : {
2133 : 3895 : gcc_assert (vls_type == VLS_LOAD);
2134 : 3895 : *memory_access_type = VMAT_INVARIANT;
2135 : : }
2136 : : /* Try using LOAD/STORE_LANES. */
2137 : 1893658 : else if (slp_node->ldst_lanes
2138 : 1893658 : && (*lanes_ifn
2139 : 0 : = (vls_type == VLS_LOAD
2140 : 0 : ? vect_load_lanes_supported (vectype, group_size,
2141 : : masked_p, elsvals)
2142 : 0 : : vect_store_lanes_supported (vectype, group_size,
2143 : : masked_p))) != IFN_LAST)
2144 : 0 : *memory_access_type = VMAT_LOAD_STORE_LANES;
2145 : 1893658 : else if (!loop_vinfo && slp_node->avoid_stlf_fail)
2146 : : {
2147 : 73 : *memory_access_type = VMAT_ELEMENTWISE;
2148 : 73 : if (dump_enabled_p ())
2149 : 2 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2150 : : "using element-wise load to avoid disrupting "
2151 : : "cross iteration store-to-load forwarding\n");
2152 : : }
2153 : : else
2154 : 1893585 : *memory_access_type = VMAT_CONTIGUOUS;
2155 : :
2156 : : /* If this is single-element interleaving with an element
2157 : : distance that leaves unused vector loads around fall back
2158 : : to elementwise access if possible - we otherwise least
2159 : : create very sub-optimal code in that case (and
2160 : : blow up memory, see PR65518). */
2161 : 1909130 : if (loop_vinfo
2162 : 1909130 : && single_element_p
2163 : 424736 : && (*memory_access_type == VMAT_CONTIGUOUS
2164 : 15232 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2165 : 2333866 : && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
2166 : : {
2167 : 10765 : if (SLP_TREE_LANES (slp_node) == 1)
2168 : : {
2169 : 10749 : *memory_access_type = VMAT_ELEMENTWISE;
2170 : 10749 : if (dump_enabled_p ())
2171 : 235 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2172 : : "single-element interleaving not supported "
2173 : : "for not adjacent vector loads, using "
2174 : : "elementwise access\n");
2175 : : }
2176 : : else
2177 : : {
2178 : 16 : if (dump_enabled_p ())
2179 : 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2180 : : "single-element interleaving not supported "
2181 : : "for not adjacent vector loads\n");
2182 : 25 : return false;
2183 : : }
2184 : : }
2185 : :
2186 : : /* For single-element interleaving also fall back to elementwise
2187 : : access in case we did not lower a permutation and cannot
2188 : : code generate it. */
2189 : 1909114 : auto_vec<tree> temv;
2190 : 1909114 : unsigned n_perms;
2191 : 1909114 : if (loop_vinfo
2192 : : && single_element_p
2193 : 424720 : && SLP_TREE_LANES (slp_node) == 1
2194 : 418631 : && (*memory_access_type == VMAT_CONTIGUOUS
2195 : 25232 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2196 : 400730 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2197 : 1916586 : && !vect_transform_slp_perm_load
2198 : 7472 : (loop_vinfo, slp_node, temv, NULL,
2199 : : LOOP_VINFO_VECT_FACTOR (loop_vinfo), true, &n_perms))
2200 : : {
2201 : 1476 : *memory_access_type = VMAT_ELEMENTWISE;
2202 : 1476 : if (dump_enabled_p ())
2203 : 83 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2204 : : "single-element interleaving permutation not "
2205 : : "supported, using elementwise access\n");
2206 : : }
2207 : :
2208 : 480832 : overrun_p = (loop_vinfo && gap != 0
2209 : 1943011 : && *memory_access_type != VMAT_ELEMENTWISE);
2210 : 1909114 : if (overrun_p && vls_type != VLS_LOAD)
2211 : : {
2212 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2213 : : "Grouped store with gaps requires"
2214 : : " non-consecutive accesses\n");
2215 : 0 : return false;
2216 : : }
2217 : :
2218 : 1909114 : unsigned HOST_WIDE_INT dr_size = vect_get_scalar_dr_size (first_dr_info);
2219 : 1909114 : poly_int64 off = 0;
2220 : 1909114 : if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2221 : 7363 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2222 : :
2223 : : /* An overrun is fine if the trailing elements are smaller
2224 : : than the alignment boundary B. Every vector access will
2225 : : be a multiple of B and so we are guaranteed to access a
2226 : : non-gap element in the same B-sized block. */
2227 : 1909114 : if (overrun_p
2228 : 1909114 : && gap < (vect_known_alignment_in_bytes (first_dr_info,
2229 : 21355 : vectype, off) / dr_size))
2230 : : overrun_p = false;
2231 : :
2232 : : /* When we have a contiguous access across loop iterations
2233 : : but the access in the loop doesn't cover the full vector
2234 : : we can end up with no gap recorded but still excess
2235 : : elements accessed, see PR103116. Make sure we peel for
2236 : : gaps if necessary and sufficient and give up if not.
2237 : :
2238 : : If there is a combination of the access not covering the full
2239 : : vector and a gap recorded then we may need to peel twice. */
2240 : 1909114 : bool large_vector_overrun_p = false;
2241 : 1909114 : if (loop_vinfo
2242 : 480832 : && (*memory_access_type == VMAT_CONTIGUOUS
2243 : 27539 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2244 : 460656 : && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2245 : 1932568 : && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
2246 : : nunits))
2247 : : large_vector_overrun_p = overrun_p = true;
2248 : :
2249 : : /* If the gap splits the vector in half and the target
2250 : : can do half-vector operations avoid the epilogue peeling
2251 : : by simply loading half of the vector only. Usually
2252 : : the construction with an upper zero half will be elided. */
2253 : 1909114 : dr_alignment_support alss;
2254 : 1909114 : int misalign = dr_misalignment (first_dr_info, vectype, off);
2255 : 1909114 : tree half_vtype;
2256 : 1909114 : poly_uint64 remain;
2257 : 1909114 : unsigned HOST_WIDE_INT tem, num;
2258 : 1909114 : if (overrun_p
2259 : 1909114 : && !masked_p
2260 : 16047 : && *memory_access_type != VMAT_LOAD_STORE_LANES
2261 : 16047 : && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
2262 : : vectype, misalign)))
2263 : : == dr_aligned
2264 : 15621 : || alss == dr_unaligned_supported)
2265 : 8944 : && can_div_trunc_p (group_size
2266 : 8944 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2267 : : nunits, &tem, &remain)
2268 : 1918058 : && (known_eq (remain, 0u)
2269 : 8445 : || (known_ne (remain, 0u)
2270 : 6400 : && constant_multiple_p (nunits, remain, &num)
2271 : 1908615 : && (vector_vector_composition_type (vectype, num, &half_vtype)
2272 : : != NULL_TREE))))
2273 : 6899 : overrun_p = false;
2274 : :
2275 : 1909114 : if (overrun_p && !can_overrun_p)
2276 : : {
2277 : 6 : if (dump_enabled_p ())
2278 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2279 : : "Peeling for outer loop is not supported\n");
2280 : 6 : return false;
2281 : : }
2282 : :
2283 : : /* Peeling for gaps assumes that a single scalar iteration
2284 : : is enough to make sure the last vector iteration doesn't
2285 : : access excess elements. */
2286 : 1909108 : if (overrun_p
2287 : 1909108 : && (!can_div_trunc_p (group_size
2288 : 9142 : * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2289 : : nunits, &tem, &remain)
2290 : 9142 : || maybe_lt (remain + group_size, nunits)))
2291 : : {
2292 : : /* But peeling a single scalar iteration is enough if
2293 : : we can use the next power-of-two sized partial
2294 : : access and that is sufficiently small to be covered
2295 : : by the single scalar iteration. */
2296 : 29 : unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
2297 : 29 : if (masked_p
2298 : 29 : || !nunits.is_constant (&cnunits)
2299 : 29 : || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
2300 : 29 : || (((cremain = (group_size * cvf - gap) % cnunits), true)
2301 : 29 : && ((cpart_size = (1 << ceil_log2 (cremain))), true)
2302 : 29 : && (cremain + group_size < cpart_size
2303 : 26 : || (vector_vector_composition_type (vectype,
2304 : 26 : cnunits / cpart_size,
2305 : : &half_vtype)
2306 : : == NULL_TREE))))
2307 : : {
2308 : : /* If all fails we can still resort to niter masking unless
2309 : : the vectors used are too big, so enforce the use of
2310 : : partial vectors. */
2311 : 3 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
2312 : 0 : && !large_vector_overrun_p)
2313 : : {
2314 : 0 : if (dump_enabled_p ())
2315 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2316 : : "peeling for gaps insufficient for "
2317 : : "access unless using partial "
2318 : : "vectors\n");
2319 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2320 : : }
2321 : : else
2322 : : {
2323 : 3 : if (dump_enabled_p ())
2324 : 3 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2325 : : "peeling for gaps insufficient for "
2326 : : "access\n");
2327 : 3 : return false;
2328 : : }
2329 : : }
2330 : 26 : else if (large_vector_overrun_p)
2331 : : {
2332 : 26 : if (dump_enabled_p ())
2333 : 23 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2334 : : "can't operate on partial vectors because "
2335 : : "only unmasked loads handle access "
2336 : : "shortening required because of gaps at "
2337 : : "the end of the access\n");
2338 : 26 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
2339 : : }
2340 : : }
2341 : 1909114 : }
2342 : :
2343 : : /* As a last resort, trying using a gather load or scatter store.
2344 : :
2345 : : ??? Although the code can handle all group sizes correctly,
2346 : : it probably isn't a win to use separate strided accesses based
2347 : : on nearby locations. Or, even if it's a win over scalar code,
2348 : : it might not be a win over vectorizing at a lower VF, if that
2349 : : allows us to use contiguous accesses. */
2350 : 1955691 : if ((*memory_access_type == VMAT_ELEMENTWISE
2351 : 1955691 : || *memory_access_type == VMAT_STRIDED_SLP)
2352 : 62036 : && single_element_p
2353 : 54562 : && SLP_TREE_LANES (slp_node) == 1
2354 : 54402 : && loop_vinfo
2355 : 2010093 : && vect_use_strided_gather_scatters_p (stmt_info, loop_vinfo,
2356 : : masked_p, gs_info, elsvals))
2357 : 0 : *memory_access_type = VMAT_GATHER_SCATTER;
2358 : :
2359 : 1955691 : if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
2360 : 1954787 : || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2361 : 8263 : *poffset = neg_ldst_offset;
2362 : :
2363 : 1955691 : if (*memory_access_type == VMAT_GATHER_SCATTER
2364 : 1955691 : || *memory_access_type == VMAT_ELEMENTWISE
2365 : 1940451 : || *memory_access_type == VMAT_STRIDED_SLP
2366 : 1893655 : || *memory_access_type == VMAT_INVARIANT)
2367 : : {
2368 : 65931 : *alignment_support_scheme = dr_unaligned_supported;
2369 : 65931 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2370 : : }
2371 : : else
2372 : : {
2373 : 1889760 : *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
2374 : 1889760 : *alignment_support_scheme
2375 : 1889760 : = vect_supportable_dr_alignment (vinfo, first_dr_info, vectype,
2376 : : *misalignment);
2377 : : }
2378 : :
2379 : 1955691 : if (vls_type != VLS_LOAD && first_stmt_info == stmt_info)
2380 : : {
2381 : : /* STMT is the leader of the group. Check the operands of all the
2382 : : stmts of the group. */
2383 : 1378369 : stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
2384 : 3452221 : while (next_stmt_info)
2385 : : {
2386 : 2073852 : tree op = vect_get_store_rhs (next_stmt_info);
2387 : 2073852 : enum vect_def_type dt;
2388 : 2073852 : if (!vect_is_simple_use (op, vinfo, &dt))
2389 : : {
2390 : 0 : if (dump_enabled_p ())
2391 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2392 : : "use not simple.\n");
2393 : 0 : return false;
2394 : : }
2395 : 2073852 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
2396 : : }
2397 : : }
2398 : :
2399 : 1955691 : if (overrun_p)
2400 : : {
2401 : 9139 : gcc_assert (can_overrun_p);
2402 : 9139 : if (dump_enabled_p ())
2403 : 702 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2404 : : "Data access with gaps requires scalar "
2405 : : "epilogue loop\n");
2406 : 9139 : LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2407 : : }
2408 : :
2409 : : return true;
2410 : : }
2411 : :
2412 : : /* Analyze load or store statement STMT_INFO of type VLS_TYPE. Return true
2413 : : if there is a memory access type that the vectorized form can use,
2414 : : storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2415 : : or scatters, fill in GS_INFO accordingly. In addition
2416 : : *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
2417 : : the target does not support the alignment scheme. *MISALIGNMENT
2418 : : is set according to the alignment of the access (including
2419 : : DR_MISALIGNMENT_UNKNOWN when it is unknown).
2420 : :
2421 : : SLP says whether we're performing SLP rather than loop vectorization.
2422 : : MASKED_P is true if the statement is conditional on a vectorized mask.
2423 : : VECTYPE is the vector type that the vectorized statements will use.
2424 : : NCOPIES is the number of vector statements that will be needed.
2425 : :
2426 : : If ELSVALS is nonzero the supported else values will be stored in the
2427 : : vector ELSVALS points to. */
2428 : :
2429 : : static bool
2430 : 1963510 : get_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2431 : : tree vectype, slp_tree slp_node,
2432 : : bool masked_p, vec_load_store_type vls_type,
2433 : : unsigned int,
2434 : : vect_memory_access_type *memory_access_type,
2435 : : poly_int64 *poffset,
2436 : : dr_alignment_support *alignment_support_scheme,
2437 : : int *misalignment,
2438 : : gather_scatter_info *gs_info,
2439 : : internal_fn *lanes_ifn,
2440 : : vec<int> *elsvals = nullptr)
2441 : : {
2442 : 1963510 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2443 : 1963510 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2444 : 1963510 : *misalignment = DR_MISALIGNMENT_UNKNOWN;
2445 : 1963510 : *poffset = 0;
2446 : 1963510 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2447 : : {
2448 : 7794 : *memory_access_type = VMAT_GATHER_SCATTER;
2449 : 7794 : if (!vect_check_gather_scatter (stmt_info, loop_vinfo, gs_info,
2450 : : elsvals))
2451 : 0 : gcc_unreachable ();
2452 : : /* When using internal functions, we rely on pattern recognition
2453 : : to convert the type of the offset to the type that the target
2454 : : requires, with the result being a call to an internal function.
2455 : : If that failed for some reason (e.g. because another pattern
2456 : : took priority), just handle cases in which the offset already
2457 : : has the right type. */
2458 : 7794 : else if (gs_info->ifn != IFN_LAST
2459 : 0 : && !is_gimple_call (stmt_info->stmt)
2460 : 7794 : && !tree_nop_conversion_p (TREE_TYPE (gs_info->offset),
2461 : 0 : TREE_TYPE (gs_info->offset_vectype)))
2462 : : {
2463 : 0 : if (dump_enabled_p ())
2464 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2465 : : "%s offset requires a conversion\n",
2466 : : vls_type == VLS_LOAD ? "gather" : "scatter");
2467 : 0 : return false;
2468 : : }
2469 : 7794 : slp_tree offset_node = SLP_TREE_CHILDREN (slp_node)[0];
2470 : 7794 : gs_info->offset_dt = SLP_TREE_DEF_TYPE (offset_node);
2471 : 7794 : gs_info->offset_vectype = SLP_TREE_VECTYPE (offset_node);
2472 : 7794 : if (gs_info->ifn == IFN_LAST && !gs_info->decl)
2473 : : {
2474 : 7126 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
2475 : 7126 : || !TYPE_VECTOR_SUBPARTS (gs_info->offset_vectype).is_constant ()
2476 : 7126 : || VECTOR_BOOLEAN_TYPE_P (gs_info->offset_vectype)
2477 : 7126 : || !constant_multiple_p (TYPE_VECTOR_SUBPARTS
2478 : 7126 : (gs_info->offset_vectype),
2479 : 12392 : TYPE_VECTOR_SUBPARTS (vectype)))
2480 : : {
2481 : 1860 : if (dump_enabled_p ())
2482 : 316 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2483 : : "unsupported vector types for emulated "
2484 : : "gather.\n");
2485 : 1860 : return false;
2486 : : }
2487 : : }
2488 : : /* Gather-scatter accesses perform only component accesses, alignment
2489 : : is irrelevant for them. */
2490 : 5934 : *alignment_support_scheme = dr_unaligned_supported;
2491 : : }
2492 : 1955716 : else if (!get_group_load_store_type (vinfo, stmt_info, vectype, slp_node,
2493 : : masked_p,
2494 : : vls_type, memory_access_type, poffset,
2495 : : alignment_support_scheme,
2496 : : misalignment, gs_info, lanes_ifn,
2497 : : elsvals))
2498 : : return false;
2499 : :
2500 : 1961625 : if ((*memory_access_type == VMAT_ELEMENTWISE
2501 : 1961625 : || *memory_access_type == VMAT_STRIDED_SLP)
2502 : : && !nunits.is_constant ())
2503 : : {
2504 : : if (dump_enabled_p ())
2505 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2506 : : "Not using elementwise accesses due to variable "
2507 : : "vectorization factor.\n");
2508 : : return false;
2509 : : }
2510 : :
2511 : :
2512 : : /* Checks if all scalar iterations are known to be inbounds. */
2513 : 1961625 : bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
2514 : :
2515 : : /* Check if we support the operation if early breaks are needed. Here we
2516 : : must ensure that we don't access any more than the scalar code would
2517 : : have. A masked operation would ensure this, so for these load types
2518 : : force masking. */
2519 : 1961625 : if (loop_vinfo
2520 : 533343 : && dr_safe_speculative_read_required (stmt_info)
2521 : 130772 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
2522 : 1961625 : && (*memory_access_type == VMAT_GATHER_SCATTER
2523 : 130772 : || *memory_access_type == VMAT_STRIDED_SLP))
2524 : : {
2525 : 4450 : if (dump_enabled_p ())
2526 : 8 : dump_printf_loc (MSG_NOTE, vect_location,
2527 : : "early break not supported: cannot peel for "
2528 : : "alignment. With non-contiguous memory vectorization"
2529 : : " could read out of bounds at %G ",
2530 : : STMT_VINFO_STMT (stmt_info));
2531 : 4450 : if (inbounds)
2532 : 0 : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2533 : : else
2534 : : return false;
2535 : : }
2536 : :
2537 : : /* If this DR needs alignment for correctness, we must ensure the target
2538 : : alignment is a constant power-of-two multiple of the amount read per
2539 : : vector iteration or force masking. */
2540 : 1957175 : if (dr_safe_speculative_read_required (stmt_info)
2541 : 1957175 : && *alignment_support_scheme == dr_aligned)
2542 : : {
2543 : : /* We can only peel for loops, of course. */
2544 : 74676 : gcc_checking_assert (loop_vinfo);
2545 : :
2546 : 74676 : auto target_alignment
2547 : 74676 : = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
2548 : 74676 : unsigned HOST_WIDE_INT target_align;
2549 : :
2550 : 74676 : bool group_aligned = false;
2551 : 74676 : if (target_alignment.is_constant (&target_align)
2552 : : && nunits.is_constant ())
2553 : : {
2554 : 74676 : poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2555 : 74676 : auto vectype_size
2556 : 74676 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2557 : 74676 : poly_uint64 required_alignment = vf * vectype_size;
2558 : : /* If we have a grouped access we require that the alignment be N * elem. */
2559 : 74676 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2560 : 2603 : required_alignment *=
2561 : 2603 : DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
2562 : 74676 : if (!multiple_p (target_alignment, required_alignment))
2563 : : {
2564 : 1360 : if (dump_enabled_p ())
2565 : 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2566 : : "desired alignment %wu not met. Instead got %wu "
2567 : : "for DR alignment at %G",
2568 : : required_alignment.to_constant (),
2569 : : target_align, STMT_VINFO_STMT (stmt_info));
2570 : 1360 : return false;
2571 : : }
2572 : :
2573 : 73316 : if (!pow2p_hwi (target_align))
2574 : : {
2575 : 0 : if (dump_enabled_p ())
2576 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2577 : : "non-power-of-two vector alignment %wd "
2578 : : "for DR alignment at %G",
2579 : : target_align, STMT_VINFO_STMT (stmt_info));
2580 : 0 : return false;
2581 : : }
2582 : :
2583 : : /* For VLA we have to insert a runtime check that the vector loads
2584 : : per iterations don't exceed a page size. For now we can use
2585 : : POLY_VALUE_MAX as a proxy as we can't peel for VLA. */
2586 : 73316 : if (known_gt (required_alignment, (unsigned)param_min_pagesize))
2587 : : {
2588 : 0 : if (dump_enabled_p ())
2589 : : {
2590 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2591 : : "alignment required for correctness (");
2592 : 0 : dump_dec (MSG_MISSED_OPTIMIZATION, required_alignment);
2593 : 0 : dump_printf (MSG_NOTE, ") may exceed page size\n");
2594 : : }
2595 : 0 : return false;
2596 : : }
2597 : :
2598 : 73316 : group_aligned = true;
2599 : : }
2600 : :
2601 : : /* There are multiple loads that have a misalignment that we couldn't
2602 : : align. We would need LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P to
2603 : : vectorize. */
2604 : 73316 : if (!group_aligned)
2605 : : {
2606 : : if (inbounds)
2607 : : LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2608 : : else
2609 : : return false;
2610 : : }
2611 : :
2612 : : /* When using a group access the first element may be aligned but the
2613 : : subsequent loads may not be. For LOAD_LANES since the loads are based
2614 : : on the first DR then all loads in the group are aligned. For
2615 : : non-LOAD_LANES this is not the case. In particular a load + blend when
2616 : : there are gaps can have the non first loads issued unaligned, even
2617 : : partially overlapping the memory of the first load in order to simplify
2618 : : the blend. This is what the x86_64 backend does for instance. As
2619 : : such only the first load in the group is aligned, the rest are not.
2620 : : Because of this the permutes may break the alignment requirements that
2621 : : have been set, and as such we should for now, reject them. */
2622 : 73316 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2623 : : {
2624 : 842 : if (dump_enabled_p ())
2625 : 62 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2626 : : "loads with load permutations not supported for "
2627 : : "speculative early break loads for %G",
2628 : : STMT_VINFO_STMT (stmt_info));
2629 : 842 : return false;
2630 : : }
2631 : : }
2632 : :
2633 : 1954973 : if (*alignment_support_scheme == dr_unaligned_unsupported)
2634 : : {
2635 : 42732 : if (dump_enabled_p ())
2636 : 232 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2637 : : "unsupported unaligned access\n");
2638 : 42732 : return false;
2639 : : }
2640 : :
2641 : : /* FIXME: At the moment the cost model seems to underestimate the
2642 : : cost of using elementwise accesses. This check preserves the
2643 : : traditional behavior until that can be fixed. */
2644 : 1912241 : stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2645 : 1912241 : if (!first_stmt_info)
2646 : 408126 : first_stmt_info = stmt_info;
2647 : 1912241 : if (*memory_access_type == VMAT_ELEMENTWISE
2648 : 15240 : && !STMT_VINFO_STRIDED_P (first_stmt_info)
2649 : 1927481 : && !(stmt_info == DR_GROUP_FIRST_ELEMENT (stmt_info)
2650 : 12617 : && !DR_GROUP_NEXT_ELEMENT (stmt_info)
2651 : 12531 : && !pow2p_hwi (DR_GROUP_SIZE (stmt_info))))
2652 : : {
2653 : 6146 : if (dump_enabled_p ())
2654 : 206 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2655 : : "not falling back to elementwise accesses\n");
2656 : 6146 : return false;
2657 : : }
2658 : : return true;
2659 : : }
2660 : :
2661 : : /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
2662 : : conditional operation STMT_INFO. When returning true, store the mask
2663 : : in *MASK, the type of its definition in *MASK_DT_OUT, the type of the
2664 : : vectorized mask in *MASK_VECTYPE_OUT and the SLP node corresponding
2665 : : to the mask in *MASK_NODE if MASK_NODE is not NULL. */
2666 : :
2667 : : static bool
2668 : 7378 : vect_check_scalar_mask (vec_info *vinfo, stmt_vec_info stmt_info,
2669 : : slp_tree slp_node, unsigned mask_index,
2670 : : tree *mask, slp_tree *mask_node,
2671 : : vect_def_type *mask_dt_out, tree *mask_vectype_out)
2672 : : {
2673 : 7378 : enum vect_def_type mask_dt;
2674 : 7378 : tree mask_vectype;
2675 : 7378 : slp_tree mask_node_1;
2676 : 7378 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, mask_index,
2677 : : mask, &mask_node_1, &mask_dt, &mask_vectype))
2678 : : {
2679 : 0 : if (dump_enabled_p ())
2680 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2681 : : "mask use not simple.\n");
2682 : 0 : return false;
2683 : : }
2684 : :
2685 : 7378 : if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
2686 : 7378 : && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (*mask)))
2687 : : {
2688 : 0 : if (dump_enabled_p ())
2689 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2690 : : "mask argument is not a boolean.\n");
2691 : 0 : return false;
2692 : : }
2693 : :
2694 : : /* If the caller is not prepared for adjusting an external/constant
2695 : : SLP mask vector type fail. */
2696 : 7378 : if (slp_node
2697 : 7378 : && !mask_node
2698 : 0 : && SLP_TREE_DEF_TYPE (mask_node_1) != vect_internal_def)
2699 : : {
2700 : 0 : if (dump_enabled_p ())
2701 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2702 : : "SLP mask argument is not vectorized.\n");
2703 : 0 : return false;
2704 : : }
2705 : :
2706 : 7378 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2707 : 7378 : if (!mask_vectype)
2708 : 17 : mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
2709 : : mask_node_1);
2710 : :
2711 : 7378 : if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2712 : : {
2713 : 0 : if (dump_enabled_p ())
2714 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2715 : : "could not find an appropriate vector mask type.\n");
2716 : 0 : return false;
2717 : : }
2718 : :
2719 : 7378 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2720 : 14756 : TYPE_VECTOR_SUBPARTS (vectype)))
2721 : : {
2722 : 0 : if (dump_enabled_p ())
2723 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2724 : : "vector mask type %T"
2725 : : " does not match vector data type %T.\n",
2726 : : mask_vectype, vectype);
2727 : :
2728 : 0 : return false;
2729 : : }
2730 : :
2731 : 7378 : *mask_dt_out = mask_dt;
2732 : 7378 : *mask_vectype_out = mask_vectype;
2733 : 7378 : if (mask_node)
2734 : 7378 : *mask_node = mask_node_1;
2735 : : return true;
2736 : : }
2737 : :
2738 : : /* Return true if stored value is suitable for vectorizing store
2739 : : statement STMT_INFO. When returning true, store the scalar stored
2740 : : in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
2741 : : the type of the vectorized store value in
2742 : : *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
2743 : :
2744 : : static bool
2745 : 1381174 : vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
2746 : : slp_tree slp_node, tree *rhs, slp_tree *rhs_node,
2747 : : vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
2748 : : vec_load_store_type *vls_type_out)
2749 : : {
2750 : 1381174 : int op_no = 0;
2751 : 1381174 : if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2752 : : {
2753 : 1315 : if (gimple_call_internal_p (call)
2754 : 1315 : && internal_store_fn_p (gimple_call_internal_fn (call)))
2755 : 1315 : op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
2756 : : }
2757 : 1381174 : if (slp_node)
2758 : 1381174 : op_no = vect_slp_child_index_for_operand
2759 : 1381174 : (stmt_info->stmt, op_no, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
2760 : :
2761 : 1381174 : enum vect_def_type rhs_dt;
2762 : 1381174 : tree rhs_vectype;
2763 : 1381174 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, op_no,
2764 : : rhs, rhs_node, &rhs_dt, &rhs_vectype))
2765 : : {
2766 : 0 : if (dump_enabled_p ())
2767 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2768 : : "use not simple.\n");
2769 : 0 : return false;
2770 : : }
2771 : :
2772 : : /* In the case this is a store from a constant make sure
2773 : : native_encode_expr can handle it. */
2774 : 1381174 : if (rhs_dt == vect_constant_def
2775 : 1381174 : && CONSTANT_CLASS_P (*rhs) && native_encode_expr (*rhs, NULL, 64) == 0)
2776 : : {
2777 : 0 : if (dump_enabled_p ())
2778 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2779 : : "cannot encode constant as a byte sequence.\n");
2780 : 0 : return false;
2781 : : }
2782 : :
2783 : 1381174 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2784 : 1381174 : if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2785 : : {
2786 : 24 : if (dump_enabled_p ())
2787 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2788 : : "incompatible vector types.\n");
2789 : 24 : return false;
2790 : : }
2791 : :
2792 : 1381150 : *rhs_dt_out = rhs_dt;
2793 : 1381150 : *rhs_vectype_out = rhs_vectype;
2794 : 1381150 : if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
2795 : 1060845 : *vls_type_out = VLS_STORE_INVARIANT;
2796 : : else
2797 : 320305 : *vls_type_out = VLS_STORE;
2798 : : return true;
2799 : : }
2800 : :
2801 : : /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
2802 : : Note that we support masks with floating-point type, in which case the
2803 : : floats are interpreted as a bitmask. */
2804 : :
2805 : : static tree
2806 : 163 : vect_build_all_ones_mask (vec_info *vinfo,
2807 : : stmt_vec_info stmt_info, tree masktype)
2808 : : {
2809 : 163 : if (TREE_CODE (masktype) == INTEGER_TYPE)
2810 : 98 : return build_int_cst (masktype, -1);
2811 : 65 : else if (VECTOR_BOOLEAN_TYPE_P (masktype)
2812 : 130 : || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2813 : : {
2814 : 14 : tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2815 : 14 : mask = build_vector_from_val (masktype, mask);
2816 : 14 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2817 : : }
2818 : 51 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2819 : : {
2820 : : REAL_VALUE_TYPE r;
2821 : : long tmp[6];
2822 : 357 : for (int j = 0; j < 6; ++j)
2823 : 306 : tmp[j] = -1;
2824 : 51 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2825 : 51 : tree mask = build_real (TREE_TYPE (masktype), r);
2826 : 51 : mask = build_vector_from_val (masktype, mask);
2827 : 51 : return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2828 : : }
2829 : 0 : gcc_unreachable ();
2830 : : }
2831 : :
2832 : : /* Build an all-zero merge value of type VECTYPE while vectorizing
2833 : : STMT_INFO as a gather load. */
2834 : :
2835 : : static tree
2836 : 156 : vect_build_zero_merge_argument (vec_info *vinfo,
2837 : : stmt_vec_info stmt_info, tree vectype)
2838 : : {
2839 : 156 : tree merge;
2840 : 156 : if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2841 : 49 : merge = build_int_cst (TREE_TYPE (vectype), 0);
2842 : 107 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2843 : : {
2844 : : REAL_VALUE_TYPE r;
2845 : : long tmp[6];
2846 : 749 : for (int j = 0; j < 6; ++j)
2847 : 642 : tmp[j] = 0;
2848 : 107 : real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2849 : 107 : merge = build_real (TREE_TYPE (vectype), r);
2850 : : }
2851 : : else
2852 : 0 : gcc_unreachable ();
2853 : 156 : merge = build_vector_from_val (vectype, merge);
2854 : 156 : return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
2855 : : }
2856 : :
2857 : : /* Return the corresponding else value for an else value constant
2858 : : ELSVAL with type TYPE. */
2859 : :
2860 : : tree
2861 : 1622 : vect_get_mask_load_else (int elsval, tree type)
2862 : : {
2863 : 1622 : tree els;
2864 : 1622 : if (elsval == MASK_LOAD_ELSE_UNDEFINED)
2865 : : {
2866 : 0 : tree tmp = create_tmp_var (type);
2867 : : /* No need to warn about anything. */
2868 : 0 : TREE_NO_WARNING (tmp) = 1;
2869 : 0 : els = get_or_create_ssa_default_def (cfun, tmp);
2870 : : }
2871 : 1622 : else if (elsval == MASK_LOAD_ELSE_M1)
2872 : 0 : els = build_minus_one_cst (type);
2873 : 1622 : else if (elsval == MASK_LOAD_ELSE_ZERO)
2874 : 1622 : els = build_zero_cst (type);
2875 : : else
2876 : 0 : gcc_unreachable ();
2877 : :
2878 : 1622 : return els;
2879 : : }
2880 : :
2881 : : /* Build a gather load call while vectorizing STMT_INFO. Insert new
2882 : : instructions before GSI and add them to VEC_STMT. GS_INFO describes
2883 : : the gather load operation. If the load is conditional, MASK is the
2884 : : vectorized condition, otherwise MASK is null. PTR is the base
2885 : : pointer and OFFSET is the vectorized offset. */
2886 : :
2887 : : static gimple *
2888 : 344 : vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
2889 : : gimple_stmt_iterator *gsi,
2890 : : gather_scatter_info *gs_info,
2891 : : tree ptr, tree offset, tree mask)
2892 : : {
2893 : 344 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2894 : 344 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info->decl));
2895 : 344 : tree rettype = TREE_TYPE (TREE_TYPE (gs_info->decl));
2896 : 344 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2897 : 344 : /* ptrtype */ arglist = TREE_CHAIN (arglist);
2898 : 344 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2899 : 344 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2900 : 344 : tree scaletype = TREE_VALUE (arglist);
2901 : 344 : tree var;
2902 : 344 : gcc_checking_assert (types_compatible_p (srctype, rettype)
2903 : : && (!mask
2904 : : || TREE_CODE (masktype) == INTEGER_TYPE
2905 : : || types_compatible_p (srctype, masktype)));
2906 : :
2907 : 344 : tree op = offset;
2908 : 344 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
2909 : : {
2910 : 100 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
2911 : : TYPE_VECTOR_SUBPARTS (idxtype)));
2912 : 100 : var = vect_get_new_ssa_name (idxtype, vect_simple_var);
2913 : 100 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
2914 : 100 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
2915 : 100 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2916 : 100 : op = var;
2917 : : }
2918 : :
2919 : 344 : tree src_op = NULL_TREE;
2920 : 344 : tree mask_op = NULL_TREE;
2921 : 344 : if (mask)
2922 : : {
2923 : 188 : if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
2924 : : {
2925 : 188 : tree utype, optype = TREE_TYPE (mask);
2926 : 188 : if (VECTOR_TYPE_P (masktype)
2927 : 188 : || TYPE_MODE (masktype) == TYPE_MODE (optype))
2928 : : utype = masktype;
2929 : : else
2930 : 7 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
2931 : 188 : var = vect_get_new_ssa_name (utype, vect_scalar_var);
2932 : 188 : tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
2933 : 188 : gassign *new_stmt
2934 : 188 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
2935 : 188 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2936 : 188 : mask_arg = var;
2937 : 188 : if (!useless_type_conversion_p (masktype, utype))
2938 : : {
2939 : 7 : gcc_assert (TYPE_PRECISION (utype)
2940 : : <= TYPE_PRECISION (masktype));
2941 : 7 : var = vect_get_new_ssa_name (masktype, vect_scalar_var);
2942 : 7 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
2943 : 7 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2944 : 7 : mask_arg = var;
2945 : : }
2946 : 188 : src_op = build_zero_cst (srctype);
2947 : 188 : mask_op = mask_arg;
2948 : : }
2949 : : else
2950 : : {
2951 : : src_op = mask;
2952 : : mask_op = mask;
2953 : : }
2954 : : }
2955 : : else
2956 : : {
2957 : 156 : src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
2958 : 156 : mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
2959 : : }
2960 : :
2961 : 344 : tree scale = build_int_cst (scaletype, gs_info->scale);
2962 : 344 : gimple *new_stmt = gimple_build_call (gs_info->decl, 5, src_op, ptr, op,
2963 : : mask_op, scale);
2964 : :
2965 : 344 : if (!useless_type_conversion_p (vectype, rettype))
2966 : : {
2967 : 49 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
2968 : : TYPE_VECTOR_SUBPARTS (rettype)));
2969 : 49 : op = vect_get_new_ssa_name (rettype, vect_simple_var);
2970 : 49 : gimple_call_set_lhs (new_stmt, op);
2971 : 49 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
2972 : 49 : op = build1 (VIEW_CONVERT_EXPR, vectype, op);
2973 : 49 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
2974 : : }
2975 : :
2976 : 344 : return new_stmt;
2977 : : }
2978 : :
2979 : : /* Build a scatter store call while vectorizing STMT_INFO. Insert new
2980 : : instructions before GSI. GS_INFO describes the scatter store operation.
2981 : : PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
2982 : : vectorized data to store.
2983 : : If the store is conditional, MASK is the vectorized condition, otherwise
2984 : : MASK is null. */
2985 : :
2986 : : static gimple *
2987 : 169 : vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
2988 : : gimple_stmt_iterator *gsi,
2989 : : gather_scatter_info *gs_info,
2990 : : tree ptr, tree offset, tree oprnd, tree mask)
2991 : : {
2992 : 169 : tree rettype = TREE_TYPE (TREE_TYPE (gs_info->decl));
2993 : 169 : tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info->decl));
2994 : 169 : /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
2995 : 169 : tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2996 : 169 : tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2997 : 169 : tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2998 : 169 : tree scaletype = TREE_VALUE (arglist);
2999 : 169 : gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
3000 : : && TREE_CODE (rettype) == VOID_TYPE);
3001 : :
3002 : 169 : tree mask_arg = NULL_TREE;
3003 : 169 : if (mask)
3004 : : {
3005 : 110 : mask_arg = mask;
3006 : 110 : tree optype = TREE_TYPE (mask_arg);
3007 : 110 : tree utype;
3008 : 110 : if (TYPE_MODE (masktype) == TYPE_MODE (optype))
3009 : : utype = masktype;
3010 : : else
3011 : 8 : utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3012 : 110 : tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
3013 : 110 : mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
3014 : 110 : gassign *new_stmt
3015 : 110 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3016 : 110 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3017 : 110 : mask_arg = var;
3018 : 110 : if (!useless_type_conversion_p (masktype, utype))
3019 : : {
3020 : 8 : gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
3021 : 8 : tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3022 : 8 : new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3023 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3024 : 8 : mask_arg = var;
3025 : : }
3026 : : }
3027 : : else
3028 : : {
3029 : 59 : mask_arg = build_int_cst (masktype, -1);
3030 : 59 : mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
3031 : : }
3032 : :
3033 : 169 : tree src = oprnd;
3034 : 169 : if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
3035 : : {
3036 : 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
3037 : : TYPE_VECTOR_SUBPARTS (srctype)));
3038 : 0 : tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
3039 : 0 : src = build1 (VIEW_CONVERT_EXPR, srctype, src);
3040 : 0 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
3041 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3042 : 0 : src = var;
3043 : : }
3044 : :
3045 : 169 : tree op = offset;
3046 : 169 : if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3047 : : {
3048 : 24 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3049 : : TYPE_VECTOR_SUBPARTS (idxtype)));
3050 : 24 : tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3051 : 24 : op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3052 : 24 : gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3053 : 24 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3054 : 24 : op = var;
3055 : : }
3056 : :
3057 : 169 : tree scale = build_int_cst (scaletype, gs_info->scale);
3058 : 169 : gcall *new_stmt
3059 : 169 : = gimple_build_call (gs_info->decl, 5, ptr, mask_arg, op, src, scale);
3060 : 169 : return new_stmt;
3061 : : }
3062 : :
3063 : : /* Prepare the base and offset in GS_INFO for vectorization.
3064 : : Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
3065 : : to the vectorized offset argument for the first copy of STMT_INFO.
3066 : : STMT_INFO is the statement described by GS_INFO and LOOP is the
3067 : : containing loop. */
3068 : :
3069 : : static void
3070 : 1216 : vect_get_gather_scatter_ops (loop_vec_info loop_vinfo,
3071 : : class loop *loop, stmt_vec_info stmt_info,
3072 : : slp_tree slp_node, gather_scatter_info *gs_info,
3073 : : tree *dataref_ptr, vec<tree> *vec_offset)
3074 : : {
3075 : 1216 : gimple_seq stmts = NULL;
3076 : 1216 : *dataref_ptr = force_gimple_operand (gs_info->base, &stmts, true, NULL_TREE);
3077 : 1216 : if (stmts != NULL)
3078 : : {
3079 : 981 : basic_block new_bb;
3080 : 981 : edge pe = loop_preheader_edge (loop);
3081 : 981 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3082 : 981 : gcc_assert (!new_bb);
3083 : : }
3084 : 1216 : if (slp_node)
3085 : 1216 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
3086 : : else
3087 : : {
3088 : 0 : unsigned ncopies
3089 : 0 : = vect_get_num_copies (loop_vinfo, gs_info->offset_vectype);
3090 : 0 : vect_get_vec_defs_for_operand (loop_vinfo, stmt_info, ncopies,
3091 : : gs_info->offset, vec_offset,
3092 : : gs_info->offset_vectype);
3093 : : }
3094 : 1216 : }
3095 : :
3096 : : /* Prepare to implement a grouped or strided load or store using
3097 : : the gather load or scatter store operation described by GS_INFO.
3098 : : STMT_INFO is the load or store statement.
3099 : :
3100 : : Set *DATAREF_BUMP to the amount that should be added to the base
3101 : : address after each copy of the vectorized statement. Set *VEC_OFFSET
3102 : : to an invariant offset vector in which element I has the value
3103 : : I * DR_STEP / SCALE. */
3104 : :
3105 : : static void
3106 : 0 : vect_get_strided_load_store_ops (stmt_vec_info stmt_info,
3107 : : loop_vec_info loop_vinfo,
3108 : : gimple_stmt_iterator *gsi,
3109 : : gather_scatter_info *gs_info,
3110 : : tree *dataref_bump, tree *vec_offset,
3111 : : vec_loop_lens *loop_lens)
3112 : : {
3113 : 0 : struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3114 : 0 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3115 : :
3116 : 0 : if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3117 : : {
3118 : : /* _31 = .SELECT_VL (ivtmp_29, POLY_INT_CST [4, 4]);
3119 : : ivtmp_8 = _31 * 16 (step in bytes);
3120 : : .MASK_LEN_SCATTER_STORE (vectp_a.9_7, ... );
3121 : : vectp_a.9_26 = vectp_a.9_7 + ivtmp_8; */
3122 : 0 : tree loop_len
3123 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, vectype, 0, 0);
3124 : 0 : tree tmp
3125 : 0 : = fold_build2 (MULT_EXPR, sizetype,
3126 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3127 : : loop_len);
3128 : 0 : *dataref_bump = force_gimple_operand_gsi (gsi, tmp, true, NULL_TREE, true,
3129 : : GSI_SAME_STMT);
3130 : : }
3131 : : else
3132 : : {
3133 : 0 : tree bump
3134 : 0 : = size_binop (MULT_EXPR,
3135 : : fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3136 : : size_int (TYPE_VECTOR_SUBPARTS (vectype)));
3137 : 0 : *dataref_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
3138 : : }
3139 : :
3140 : 0 : internal_fn ifn
3141 : 0 : = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
3142 : 0 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
3143 : : {
3144 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
3145 : : unshare_expr (DR_STEP (dr)));
3146 : 0 : return;
3147 : : }
3148 : :
3149 : : /* The offset given in GS_INFO can have pointer type, so use the element
3150 : : type of the vector instead. */
3151 : 0 : tree offset_type = TREE_TYPE (gs_info->offset_vectype);
3152 : :
3153 : : /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
3154 : 0 : tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
3155 : : ssize_int (gs_info->scale));
3156 : 0 : step = fold_convert (offset_type, step);
3157 : :
3158 : : /* Create {0, X, X*2, X*3, ...}. */
3159 : 0 : tree offset = fold_build2 (VEC_SERIES_EXPR, gs_info->offset_vectype,
3160 : : build_zero_cst (offset_type), step);
3161 : 0 : *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
3162 : : }
3163 : :
3164 : : /* Prepare the pointer IVs which needs to be updated by a variable amount.
3165 : : Such variable amount is the outcome of .SELECT_VL. In this case, we can
3166 : : allow each iteration process the flexible number of elements as long as
3167 : : the number <= vf elments.
3168 : :
3169 : : Return data reference according to SELECT_VL.
3170 : : If new statements are needed, insert them before GSI. */
3171 : :
3172 : : static tree
3173 : 0 : vect_get_loop_variant_data_ptr_increment (
3174 : : vec_info *vinfo, tree aggr_type, gimple_stmt_iterator *gsi,
3175 : : vec_loop_lens *loop_lens, dr_vec_info *dr_info,
3176 : : vect_memory_access_type memory_access_type)
3177 : : {
3178 : 0 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3179 : 0 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3180 : :
3181 : : /* gather/scatter never reach here. */
3182 : 0 : gcc_assert (memory_access_type != VMAT_GATHER_SCATTER);
3183 : :
3184 : : /* When we support SELECT_VL pattern, we dynamic adjust
3185 : : the memory address by .SELECT_VL result.
3186 : :
3187 : : The result of .SELECT_VL is the number of elements to
3188 : : be processed of each iteration. So the memory address
3189 : : adjustment operation should be:
3190 : :
3191 : : addr = addr + .SELECT_VL (ARG..) * step;
3192 : : */
3193 : 0 : tree loop_len
3194 : 0 : = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, aggr_type, 0, 0);
3195 : 0 : tree len_type = TREE_TYPE (loop_len);
3196 : : /* Since the outcome of .SELECT_VL is element size, we should adjust
3197 : : it into bytesize so that it can be used in address pointer variable
3198 : : amount IVs adjustment. */
3199 : 0 : tree tmp = fold_build2 (MULT_EXPR, len_type, loop_len,
3200 : : wide_int_to_tree (len_type, wi::to_widest (step)));
3201 : 0 : tree bump = make_temp_ssa_name (len_type, NULL, "ivtmp");
3202 : 0 : gassign *assign = gimple_build_assign (bump, tmp);
3203 : 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3204 : 0 : return bump;
3205 : : }
3206 : :
3207 : : /* Return the amount that should be added to a vector pointer to move
3208 : : to the next or previous copy of AGGR_TYPE. DR_INFO is the data reference
3209 : : being vectorized and MEMORY_ACCESS_TYPE describes the type of
3210 : : vectorization. */
3211 : :
3212 : : static tree
3213 : 689127 : vect_get_data_ptr_increment (vec_info *vinfo, gimple_stmt_iterator *gsi,
3214 : : dr_vec_info *dr_info, tree aggr_type,
3215 : : vect_memory_access_type memory_access_type,
3216 : : vec_loop_lens *loop_lens = nullptr)
3217 : : {
3218 : 689127 : if (memory_access_type == VMAT_INVARIANT)
3219 : 0 : return size_zero_node;
3220 : :
3221 : 689127 : loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3222 : 125710 : if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3223 : 0 : return vect_get_loop_variant_data_ptr_increment (vinfo, aggr_type, gsi,
3224 : : loop_lens, dr_info,
3225 : 0 : memory_access_type);
3226 : :
3227 : 689127 : tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3228 : 689127 : tree step = vect_dr_behavior (vinfo, dr_info)->step;
3229 : 689127 : if (tree_int_cst_sgn (step) == -1)
3230 : 2808 : iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3231 : : return iv_step;
3232 : : }
3233 : :
3234 : : /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}. */
3235 : :
3236 : : static bool
3237 : 196 : vectorizable_bswap (vec_info *vinfo,
3238 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3239 : : gimple **vec_stmt, slp_tree slp_node,
3240 : : slp_tree *slp_op,
3241 : : tree vectype_in, stmt_vector_for_cost *cost_vec)
3242 : : {
3243 : 196 : tree op, vectype;
3244 : 196 : gcall *stmt = as_a <gcall *> (stmt_info->stmt);
3245 : 196 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3246 : 196 : unsigned ncopies;
3247 : :
3248 : 196 : op = gimple_call_arg (stmt, 0);
3249 : 196 : vectype = STMT_VINFO_VECTYPE (stmt_info);
3250 : 196 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3251 : :
3252 : : /* Multiple types in SLP are handled by creating the appropriate number of
3253 : : vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3254 : : case of SLP. */
3255 : 196 : if (slp_node)
3256 : : ncopies = 1;
3257 : : else
3258 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
3259 : :
3260 : 0 : gcc_assert (ncopies >= 1);
3261 : :
3262 : 196 : if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
3263 : : {
3264 : 0 : if (dump_enabled_p ())
3265 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3266 : : "mismatched vector sizes %T and %T\n",
3267 : : vectype_in, vectype);
3268 : 0 : return false;
3269 : : }
3270 : :
3271 : 196 : tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
3272 : 196 : if (! char_vectype)
3273 : : return false;
3274 : :
3275 : 196 : poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
3276 : 196 : unsigned word_bytes;
3277 : 196 : if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
3278 : : return false;
3279 : :
3280 : : /* The encoding uses one stepped pattern for each byte in the word. */
3281 : 196 : vec_perm_builder elts (num_bytes, word_bytes, 3);
3282 : 784 : for (unsigned i = 0; i < 3; ++i)
3283 : 3252 : for (unsigned j = 0; j < word_bytes; ++j)
3284 : 2664 : elts.quick_push ((i + 1) * word_bytes - j - 1);
3285 : :
3286 : 196 : vec_perm_indices indices (elts, 1, num_bytes);
3287 : 196 : machine_mode vmode = TYPE_MODE (char_vectype);
3288 : 196 : if (!can_vec_perm_const_p (vmode, vmode, indices))
3289 : : return false;
3290 : :
3291 : 152 : if (! vec_stmt)
3292 : : {
3293 : 140 : if (slp_node
3294 : 140 : && !vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
3295 : : {
3296 : 0 : if (dump_enabled_p ())
3297 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3298 : : "incompatible vector types for invariants\n");
3299 : 0 : return false;
3300 : : }
3301 : :
3302 : 140 : STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
3303 : 140 : DUMP_VECT_SCOPE ("vectorizable_bswap");
3304 : 140 : record_stmt_cost (cost_vec,
3305 : : 1, vector_stmt, stmt_info, 0, vect_prologue);
3306 : 140 : record_stmt_cost (cost_vec,
3307 : : slp_node
3308 : 140 : ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) : ncopies,
3309 : : vec_perm, stmt_info, 0, vect_body);
3310 : 140 : return true;
3311 : : }
3312 : :
3313 : 12 : tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
3314 : :
3315 : : /* Transform. */
3316 : 12 : vec<tree> vec_oprnds = vNULL;
3317 : 12 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
3318 : : op, &vec_oprnds);
3319 : : /* Arguments are ready. create the new vector stmt. */
3320 : 12 : unsigned i;
3321 : 12 : tree vop;
3322 : 24 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3323 : : {
3324 : 12 : gimple *new_stmt;
3325 : 12 : tree tem = make_ssa_name (char_vectype);
3326 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3327 : : char_vectype, vop));
3328 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3329 : 12 : tree tem2 = make_ssa_name (char_vectype);
3330 : 12 : new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3331 : : tem, tem, bswap_vconst);
3332 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3333 : 12 : tem = make_ssa_name (vectype);
3334 : 12 : new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3335 : : vectype, tem2));
3336 : 12 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3337 : 12 : if (slp_node)
3338 : 12 : slp_node->push_vec_def (new_stmt);
3339 : : else
3340 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
3341 : : }
3342 : :
3343 : 12 : if (!slp_node)
3344 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
3345 : :
3346 : 12 : vec_oprnds.release ();
3347 : 12 : return true;
3348 : 196 : }
3349 : :
3350 : : /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3351 : : integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3352 : : in a single step. On success, store the binary pack code in
3353 : : *CONVERT_CODE. */
3354 : :
3355 : : static bool
3356 : 141 : simple_integer_narrowing (tree vectype_out, tree vectype_in,
3357 : : code_helper *convert_code)
3358 : : {
3359 : 282 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3360 : 282 : || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3361 : : return false;
3362 : :
3363 : 51 : code_helper code;
3364 : 51 : int multi_step_cvt = 0;
3365 : 51 : auto_vec <tree, 8> interm_types;
3366 : 70 : if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3367 : : &code, &multi_step_cvt, &interm_types)
3368 : 51 : || multi_step_cvt)
3369 : 19 : return false;
3370 : :
3371 : 32 : *convert_code = code;
3372 : 32 : return true;
3373 : 51 : }
3374 : :
3375 : : /* Function vectorizable_call.
3376 : :
3377 : : Check if STMT_INFO performs a function call that can be vectorized.
3378 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
3379 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3380 : : Return true if STMT_INFO is vectorizable in this way. */
3381 : :
3382 : : static bool
3383 : 2301909 : vectorizable_call (vec_info *vinfo,
3384 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3385 : : gimple **vec_stmt, slp_tree slp_node,
3386 : : stmt_vector_for_cost *cost_vec)
3387 : : {
3388 : 2301909 : gcall *stmt;
3389 : 2301909 : tree vec_dest;
3390 : 2301909 : tree scalar_dest;
3391 : 2301909 : tree op;
3392 : 2301909 : tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3393 : 2301909 : tree vectype_out, vectype_in;
3394 : 2301909 : poly_uint64 nunits_in;
3395 : 2301909 : poly_uint64 nunits_out;
3396 : 2301909 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3397 : 2301909 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3398 : 2301909 : tree fndecl, new_temp, rhs_type;
3399 : 2301909 : enum vect_def_type dt[4]
3400 : : = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3401 : : vect_unknown_def_type };
3402 : 2301909 : tree vectypes[ARRAY_SIZE (dt)] = {};
3403 : 2301909 : slp_tree slp_op[ARRAY_SIZE (dt)] = {};
3404 : 2301909 : int ndts = ARRAY_SIZE (dt);
3405 : 2301909 : int ncopies, j;
3406 : 2301909 : auto_vec<tree, 8> vargs;
3407 : 2301909 : enum { NARROW, NONE, WIDEN } modifier;
3408 : 2301909 : size_t i, nargs;
3409 : 2301909 : tree lhs;
3410 : 2301909 : tree clz_ctz_arg1 = NULL_TREE;
3411 : :
3412 : 2301909 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3413 : : return false;
3414 : :
3415 : 2301909 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3416 : 158036 : && ! vec_stmt)
3417 : : return false;
3418 : :
3419 : : /* Is STMT_INFO a vectorizable call? */
3420 : 2311110 : stmt = dyn_cast <gcall *> (stmt_info->stmt);
3421 : 17956 : if (!stmt)
3422 : : return false;
3423 : :
3424 : 17956 : if (gimple_call_internal_p (stmt)
3425 : 17956 : && (internal_load_fn_p (gimple_call_internal_fn (stmt))
3426 : 10670 : || internal_store_fn_p (gimple_call_internal_fn (stmt))))
3427 : : /* Handled by vectorizable_load and vectorizable_store. */
3428 : 2498 : return false;
3429 : :
3430 : 15458 : if (gimple_call_lhs (stmt) == NULL_TREE
3431 : 15458 : || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3432 : : return false;
3433 : :
3434 : 15458 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
3435 : :
3436 : 15458 : vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3437 : :
3438 : : /* Process function arguments. */
3439 : 15458 : rhs_type = NULL_TREE;
3440 : 15458 : vectype_in = NULL_TREE;
3441 : 15458 : nargs = gimple_call_num_args (stmt);
3442 : :
3443 : : /* Bail out if the function has more than four arguments, we do not have
3444 : : interesting builtin functions to vectorize with more than two arguments
3445 : : except for fma. No arguments is also not good. */
3446 : 15458 : if (nargs == 0 || nargs > 4)
3447 : : return false;
3448 : :
3449 : : /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic. */
3450 : 15442 : combined_fn cfn = gimple_call_combined_fn (stmt);
3451 : 15442 : if (cfn == CFN_GOMP_SIMD_LANE)
3452 : : {
3453 : 3189 : nargs = 0;
3454 : 3189 : rhs_type = unsigned_type_node;
3455 : : }
3456 : : /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
3457 : : argument just says whether it is well-defined at zero or not and what
3458 : : value should be returned for it. */
3459 : 15442 : if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
3460 : : {
3461 : 118 : nargs = 1;
3462 : 118 : clz_ctz_arg1 = gimple_call_arg (stmt, 1);
3463 : : }
3464 : :
3465 : 15442 : int mask_opno = -1;
3466 : 15442 : if (internal_fn_p (cfn))
3467 : 12537 : mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3468 : :
3469 : 42356 : for (i = 0; i < nargs; i++)
3470 : : {
3471 : 28190 : if ((int) i == mask_opno)
3472 : : {
3473 : 3825 : if (!vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_opno,
3474 : : &op, &slp_op[i], &dt[i], &vectypes[i]))
3475 : : return false;
3476 : 3825 : continue;
3477 : : }
3478 : :
3479 : 24365 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
3480 : : i, &op, &slp_op[i], &dt[i], &vectypes[i]))
3481 : : {
3482 : 0 : if (dump_enabled_p ())
3483 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3484 : : "use not simple.\n");
3485 : 0 : return false;
3486 : : }
3487 : :
3488 : : /* We can only handle calls with arguments of the same type. */
3489 : 24365 : if (rhs_type
3490 : 24365 : && !types_compatible_p (rhs_type, TREE_TYPE (op)))
3491 : : {
3492 : 1276 : if (dump_enabled_p ())
3493 : 296 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3494 : : "argument types differ.\n");
3495 : 1276 : return false;
3496 : : }
3497 : 23089 : if (!rhs_type)
3498 : 12253 : rhs_type = TREE_TYPE (op);
3499 : :
3500 : 23089 : if (!vectype_in)
3501 : 12890 : vectype_in = vectypes[i];
3502 : 10199 : else if (vectypes[i]
3503 : 10199 : && !types_compatible_p (vectypes[i], vectype_in))
3504 : : {
3505 : 0 : if (dump_enabled_p ())
3506 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3507 : : "argument vector types differ.\n");
3508 : 0 : return false;
3509 : : }
3510 : : }
3511 : : /* If all arguments are external or constant defs, infer the vector type
3512 : : from the scalar type. */
3513 : 14166 : if (!vectype_in)
3514 : 5624 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
3515 : 14166 : if (vec_stmt)
3516 : 3484 : gcc_assert (vectype_in);
3517 : 10682 : if (!vectype_in)
3518 : : {
3519 : 1004 : if (dump_enabled_p ())
3520 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3521 : : "no vectype for scalar type %T\n", rhs_type);
3522 : :
3523 : 1004 : return false;
3524 : : }
3525 : :
3526 : 26324 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3527 : 13162 : != VECTOR_BOOLEAN_TYPE_P (vectype_in))
3528 : : {
3529 : 12 : if (dump_enabled_p ())
3530 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3531 : : "mixed mask and nonmask vector types\n");
3532 : 12 : return false;
3533 : : }
3534 : :
3535 : 13150 : if (vect_emulated_vector_p (vectype_in) || vect_emulated_vector_p (vectype_out))
3536 : : {
3537 : 0 : if (dump_enabled_p ())
3538 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3539 : : "use emulated vector type for call\n");
3540 : 0 : return false;
3541 : : }
3542 : :
3543 : : /* FORNOW */
3544 : 13150 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3545 : 13150 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3546 : 13150 : if (known_eq (nunits_in * 2, nunits_out))
3547 : : modifier = NARROW;
3548 : 12767 : else if (known_eq (nunits_out, nunits_in))
3549 : : modifier = NONE;
3550 : 15 : else if (known_eq (nunits_out * 2, nunits_in))
3551 : : modifier = WIDEN;
3552 : : else
3553 : : return false;
3554 : :
3555 : : /* We only handle functions that do not read or clobber memory. */
3556 : 26300 : if (gimple_vuse (stmt))
3557 : : {
3558 : 1354 : if (dump_enabled_p ())
3559 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3560 : : "function reads from or writes to memory.\n");
3561 : 1354 : return false;
3562 : : }
3563 : :
3564 : : /* For now, we only vectorize functions if a target specific builtin
3565 : : is available. TODO -- in some cases, it might be profitable to
3566 : : insert the calls for pieces of the vector, in order to be able
3567 : : to vectorize other operations in the loop. */
3568 : 11796 : fndecl = NULL_TREE;
3569 : 11796 : internal_fn ifn = IFN_LAST;
3570 : 11796 : tree callee = gimple_call_fndecl (stmt);
3571 : :
3572 : : /* First try using an internal function. */
3573 : 11796 : code_helper convert_code = MAX_TREE_CODES;
3574 : 11796 : if (cfn != CFN_LAST
3575 : 11796 : && (modifier == NONE
3576 : 153 : || (modifier == NARROW
3577 : 141 : && simple_integer_narrowing (vectype_out, vectype_in,
3578 : : &convert_code))))
3579 : 11069 : ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3580 : : vectype_in);
3581 : :
3582 : : /* If that fails, try asking for a target-specific built-in function. */
3583 : 11069 : if (ifn == IFN_LAST)
3584 : : {
3585 : 6720 : if (cfn != CFN_LAST)
3586 : 6114 : fndecl = targetm.vectorize.builtin_vectorized_function
3587 : 6114 : (cfn, vectype_out, vectype_in);
3588 : 606 : else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
3589 : 36 : fndecl = targetm.vectorize.builtin_md_vectorized_function
3590 : 36 : (callee, vectype_out, vectype_in);
3591 : : }
3592 : :
3593 : 11796 : if (ifn == IFN_LAST && !fndecl)
3594 : : {
3595 : 6426 : if (cfn == CFN_GOMP_SIMD_LANE
3596 : 3189 : && (!slp_node || SLP_TREE_LANES (slp_node) == 1)
3597 : 3189 : && loop_vinfo
3598 : 3189 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3599 : 3189 : && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3600 : 12804 : && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3601 : 3189 : == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3602 : : {
3603 : : /* We can handle IFN_GOMP_SIMD_LANE by returning a
3604 : : { 0, 1, 2, ... vf - 1 } vector. */
3605 : 3189 : gcc_assert (nargs == 0);
3606 : : }
3607 : 3237 : else if (modifier == NONE
3608 : 3237 : && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3609 : 3106 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3610 : 2960 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
3611 : 2926 : || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
3612 : 196 : return vectorizable_bswap (vinfo, stmt_info, gsi, vec_stmt, slp_node,
3613 : 196 : slp_op, vectype_in, cost_vec);
3614 : : else
3615 : : {
3616 : 3041 : if (dump_enabled_p ())
3617 : 225 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3618 : : "function is not vectorizable.\n");
3619 : 3041 : return false;
3620 : : }
3621 : : }
3622 : :
3623 : 8559 : if (slp_node)
3624 : : ncopies = 1;
3625 : 0 : else if (modifier == NARROW && ifn == IFN_LAST)
3626 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype_out);
3627 : : else
3628 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
3629 : :
3630 : : /* Sanity check: make sure that at least one copy of the vectorized stmt
3631 : : needs to be generated. */
3632 : 0 : gcc_assert (ncopies >= 1);
3633 : :
3634 : 8559 : int reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
3635 : 8559 : internal_fn cond_fn = get_conditional_internal_fn (ifn);
3636 : 8559 : internal_fn cond_len_fn = get_len_internal_fn (ifn);
3637 : 8559 : int len_opno = internal_fn_len_index (cond_len_fn);
3638 : 8559 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
3639 : 7481 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
3640 : 8559 : if (!vec_stmt) /* transformation not required. */
3641 : : {
3642 : 5087 : if (slp_node)
3643 : 14951 : for (i = 0; i < nargs; ++i)
3644 : 9864 : if (!vect_maybe_update_slp_op_vectype (slp_op[i],
3645 : 9864 : vectypes[i]
3646 : : ? vectypes[i] : vectype_in))
3647 : : {
3648 : 0 : if (dump_enabled_p ())
3649 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3650 : : "incompatible vector types for invariants\n");
3651 : 0 : return false;
3652 : : }
3653 : 5087 : STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
3654 : 5087 : DUMP_VECT_SCOPE ("vectorizable_call");
3655 : 5087 : vect_model_simple_cost (vinfo, ncopies, dt, ndts, slp_node, cost_vec);
3656 : 5087 : if (ifn != IFN_LAST && modifier == NARROW && !slp_node)
3657 : 0 : record_stmt_cost (cost_vec, ncopies / 2,
3658 : : vec_promote_demote, stmt_info, 0, vect_body);
3659 : :
3660 : 5087 : if (loop_vinfo
3661 : 4305 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
3662 : 19 : && (reduc_idx >= 0 || mask_opno >= 0))
3663 : : {
3664 : 18 : if (reduc_idx >= 0
3665 : 0 : && (cond_fn == IFN_LAST
3666 : 0 : || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3667 : : OPTIMIZE_FOR_SPEED))
3668 : 18 : && (cond_len_fn == IFN_LAST
3669 : 0 : || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3670 : : OPTIMIZE_FOR_SPEED)))
3671 : : {
3672 : 0 : if (dump_enabled_p ())
3673 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3674 : : "can't use a fully-masked loop because no"
3675 : : " conditional operation is available.\n");
3676 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3677 : : }
3678 : : else
3679 : : {
3680 : 18 : unsigned int nvectors
3681 : : = (slp_node
3682 : 18 : ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node)
3683 : : : ncopies);
3684 : 18 : tree scalar_mask = NULL_TREE;
3685 : 18 : if (mask_opno >= 0)
3686 : 18 : scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
3687 : 18 : if (cond_len_fn != IFN_LAST
3688 : 18 : && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3689 : : OPTIMIZE_FOR_SPEED))
3690 : 0 : vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
3691 : : 1);
3692 : : else
3693 : 18 : vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
3694 : : scalar_mask);
3695 : : }
3696 : : }
3697 : 5087 : return true;
3698 : : }
3699 : :
3700 : : /* Transform. */
3701 : :
3702 : 3472 : if (dump_enabled_p ())
3703 : 408 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3704 : :
3705 : : /* Handle def. */
3706 : 3472 : scalar_dest = gimple_call_lhs (stmt);
3707 : 3472 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3708 : :
3709 : 3472 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3710 : 3176 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
3711 : 3472 : unsigned int vect_nargs = nargs;
3712 : 3472 : if (len_loop_p)
3713 : : {
3714 : 0 : if (len_opno >= 0)
3715 : : {
3716 : 0 : ifn = cond_len_fn;
3717 : : /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS. */
3718 : 0 : vect_nargs += 2;
3719 : : }
3720 : 0 : else if (reduc_idx >= 0)
3721 : 0 : gcc_unreachable ();
3722 : : }
3723 : 3472 : else if (masked_loop_p && reduc_idx >= 0)
3724 : : {
3725 : 0 : ifn = cond_fn;
3726 : 0 : vect_nargs += 2;
3727 : : }
3728 : 3472 : if (clz_ctz_arg1)
3729 : 59 : ++vect_nargs;
3730 : :
3731 : 3472 : if (modifier == NONE || ifn != IFN_LAST)
3732 : : {
3733 : 3440 : tree prev_res = NULL_TREE;
3734 : 3440 : vargs.safe_grow (vect_nargs, true);
3735 : 3440 : auto_vec<vec<tree> > vec_defs (nargs);
3736 : 6880 : for (j = 0; j < ncopies; ++j)
3737 : : {
3738 : : /* Build argument list for the vectorized call. */
3739 : 3440 : if (slp_node)
3740 : : {
3741 : 3440 : if (cfn == CFN_GOMP_SIMD_LANE)
3742 : : {
3743 : 3290 : for (i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node); ++i)
3744 : : {
3745 : : /* ??? For multi-lane SLP we'd need to build
3746 : : { 0, 0, .., 1, 1, ... }. */
3747 : 1699 : tree cst = build_index_vector (vectype_out,
3748 : : i * nunits_out, 1);
3749 : 1699 : tree new_var
3750 : 1699 : = vect_get_new_ssa_name (vectype_out, vect_simple_var,
3751 : : "cst_");
3752 : 1699 : gimple *init_stmt = gimple_build_assign (new_var, cst);
3753 : 1699 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3754 : 1699 : new_temp = make_ssa_name (vec_dest);
3755 : 1699 : gimple *new_stmt
3756 : 1699 : = gimple_build_assign (new_temp, new_var);
3757 : 1699 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
3758 : : gsi);
3759 : 1699 : slp_node->push_vec_def (new_stmt);
3760 : : }
3761 : 1591 : continue;
3762 : 1591 : }
3763 : :
3764 : 1849 : vec<tree> vec_oprnds0;
3765 : 1849 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3766 : 1849 : vec_oprnds0 = vec_defs[0];
3767 : :
3768 : : /* Arguments are ready. Create the new vector stmt. */
3769 : 3823 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
3770 : : {
3771 : 1974 : int varg = 0;
3772 : 1974 : if (masked_loop_p && reduc_idx >= 0)
3773 : : {
3774 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3775 : : /* Always true for SLP. */
3776 : 0 : gcc_assert (ncopies == 1);
3777 : 0 : vargs[varg++] = vect_get_loop_mask (loop_vinfo,
3778 : : gsi, masks, vec_num,
3779 : : vectype_out, i);
3780 : : }
3781 : : size_t k;
3782 : 7318 : for (k = 0; k < nargs; k++)
3783 : : {
3784 : 5344 : vec<tree> vec_oprndsk = vec_defs[k];
3785 : 5344 : vargs[varg++] = vec_oprndsk[i];
3786 : : }
3787 : 1974 : if (masked_loop_p && reduc_idx >= 0)
3788 : 0 : vargs[varg++] = vargs[reduc_idx + 1];
3789 : 1974 : if (clz_ctz_arg1)
3790 : 59 : vargs[varg++] = clz_ctz_arg1;
3791 : :
3792 : 1974 : gimple *new_stmt;
3793 : 1974 : if (modifier == NARROW)
3794 : : {
3795 : : /* We don't define any narrowing conditional functions
3796 : : at present. */
3797 : 0 : gcc_assert (mask_opno < 0);
3798 : 0 : tree half_res = make_ssa_name (vectype_in);
3799 : 0 : gcall *call
3800 : 0 : = gimple_build_call_internal_vec (ifn, vargs);
3801 : 0 : gimple_call_set_lhs (call, half_res);
3802 : 0 : gimple_call_set_nothrow (call, true);
3803 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3804 : 0 : if ((i & 1) == 0)
3805 : : {
3806 : 0 : prev_res = half_res;
3807 : 0 : continue;
3808 : : }
3809 : 0 : new_temp = make_ssa_name (vec_dest);
3810 : 0 : new_stmt = vect_gimple_build (new_temp, convert_code,
3811 : : prev_res, half_res);
3812 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
3813 : : new_stmt, gsi);
3814 : : }
3815 : : else
3816 : : {
3817 : 1974 : if (len_opno >= 0 && len_loop_p)
3818 : : {
3819 : 0 : unsigned int vec_num = vec_oprnds0.length ();
3820 : : /* Always true for SLP. */
3821 : 0 : gcc_assert (ncopies == 1);
3822 : 0 : tree len
3823 : 0 : = vect_get_loop_len (loop_vinfo, gsi, lens, vec_num,
3824 : : vectype_out, i, 1);
3825 : 0 : signed char biasval
3826 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3827 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
3828 : 0 : vargs[len_opno] = len;
3829 : 0 : vargs[len_opno + 1] = bias;
3830 : : }
3831 : 1974 : else if (mask_opno >= 0 && masked_loop_p)
3832 : : {
3833 : 36 : unsigned int vec_num = vec_oprnds0.length ();
3834 : : /* Always true for SLP. */
3835 : 36 : gcc_assert (ncopies == 1);
3836 : 36 : tree mask = vect_get_loop_mask (loop_vinfo,
3837 : : gsi, masks, vec_num,
3838 : : vectype_out, i);
3839 : 36 : vargs[mask_opno] = prepare_vec_mask
3840 : 36 : (loop_vinfo, TREE_TYPE (mask), mask,
3841 : 36 : vargs[mask_opno], gsi);
3842 : : }
3843 : :
3844 : 1974 : gcall *call;
3845 : 1974 : if (ifn != IFN_LAST)
3846 : 1893 : call = gimple_build_call_internal_vec (ifn, vargs);
3847 : : else
3848 : 81 : call = gimple_build_call_vec (fndecl, vargs);
3849 : 1974 : new_temp = make_ssa_name (vec_dest, call);
3850 : 1974 : gimple_call_set_lhs (call, new_temp);
3851 : 1974 : gimple_call_set_nothrow (call, true);
3852 : 1974 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3853 : 1974 : new_stmt = call;
3854 : : }
3855 : 1974 : slp_node->push_vec_def (new_stmt);
3856 : : }
3857 : 1849 : continue;
3858 : 1849 : }
3859 : :
3860 : 0 : int varg = 0;
3861 : 0 : if (masked_loop_p && reduc_idx >= 0)
3862 : 0 : vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies,
3863 : : vectype_out, j);
3864 : 0 : for (i = 0; i < nargs; i++)
3865 : : {
3866 : 0 : op = gimple_call_arg (stmt, i);
3867 : 0 : if (j == 0)
3868 : : {
3869 : 0 : vec_defs.quick_push (vNULL);
3870 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
3871 : 0 : op, &vec_defs[i],
3872 : : vectypes[i]);
3873 : : }
3874 : 0 : vargs[varg++] = vec_defs[i][j];
3875 : : }
3876 : 0 : if (masked_loop_p && reduc_idx >= 0)
3877 : 0 : vargs[varg++] = vargs[reduc_idx + 1];
3878 : 0 : if (clz_ctz_arg1)
3879 : 0 : vargs[varg++] = clz_ctz_arg1;
3880 : :
3881 : 0 : if (len_opno >= 0 && len_loop_p)
3882 : : {
3883 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens, ncopies,
3884 : : vectype_out, j, 1);
3885 : 0 : signed char biasval
3886 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3887 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
3888 : 0 : vargs[len_opno] = len;
3889 : 0 : vargs[len_opno + 1] = bias;
3890 : : }
3891 : 0 : else if (mask_opno >= 0 && masked_loop_p)
3892 : : {
3893 : 0 : tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies,
3894 : : vectype_out, j);
3895 : 0 : vargs[mask_opno]
3896 : 0 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
3897 : 0 : vargs[mask_opno], gsi);
3898 : : }
3899 : :
3900 : 0 : gimple *new_stmt;
3901 : 0 : if (cfn == CFN_GOMP_SIMD_LANE)
3902 : : {
3903 : 0 : tree cst = build_index_vector (vectype_out, j * nunits_out, 1);
3904 : 0 : tree new_var
3905 : 0 : = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
3906 : 0 : gimple *init_stmt = gimple_build_assign (new_var, cst);
3907 : 0 : vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3908 : 0 : new_temp = make_ssa_name (vec_dest);
3909 : 0 : new_stmt = gimple_build_assign (new_temp, new_var);
3910 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3911 : : }
3912 : 0 : else if (modifier == NARROW)
3913 : : {
3914 : : /* We don't define any narrowing conditional functions at
3915 : : present. */
3916 : 0 : gcc_assert (mask_opno < 0);
3917 : 0 : tree half_res = make_ssa_name (vectype_in);
3918 : 0 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3919 : 0 : gimple_call_set_lhs (call, half_res);
3920 : 0 : gimple_call_set_nothrow (call, true);
3921 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3922 : 0 : if ((j & 1) == 0)
3923 : : {
3924 : 0 : prev_res = half_res;
3925 : 0 : continue;
3926 : : }
3927 : 0 : new_temp = make_ssa_name (vec_dest);
3928 : 0 : new_stmt = vect_gimple_build (new_temp, convert_code, prev_res,
3929 : : half_res);
3930 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3931 : : }
3932 : : else
3933 : : {
3934 : 0 : gcall *call;
3935 : 0 : if (ifn != IFN_LAST)
3936 : 0 : call = gimple_build_call_internal_vec (ifn, vargs);
3937 : : else
3938 : 0 : call = gimple_build_call_vec (fndecl, vargs);
3939 : 0 : new_temp = make_ssa_name (vec_dest, call);
3940 : 0 : gimple_call_set_lhs (call, new_temp);
3941 : 0 : gimple_call_set_nothrow (call, true);
3942 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3943 : 0 : new_stmt = call;
3944 : : }
3945 : :
3946 : 0 : if (j == (modifier == NARROW ? 1 : 0))
3947 : 0 : *vec_stmt = new_stmt;
3948 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
3949 : : }
3950 : 8490 : for (i = 0; i < nargs; i++)
3951 : : {
3952 : 5050 : vec<tree> vec_oprndsi = vec_defs[i];
3953 : 5050 : vec_oprndsi.release ();
3954 : : }
3955 : 3440 : }
3956 : 32 : else if (modifier == NARROW)
3957 : : {
3958 : 32 : auto_vec<vec<tree> > vec_defs (nargs);
3959 : : /* We don't define any narrowing conditional functions at present. */
3960 : 32 : gcc_assert (mask_opno < 0);
3961 : 64 : for (j = 0; j < ncopies; ++j)
3962 : : {
3963 : : /* Build argument list for the vectorized call. */
3964 : 32 : if (j == 0)
3965 : 32 : vargs.create (nargs * 2);
3966 : : else
3967 : 0 : vargs.truncate (0);
3968 : :
3969 : 32 : if (slp_node)
3970 : : {
3971 : 32 : vec<tree> vec_oprnds0;
3972 : :
3973 : 32 : vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3974 : 32 : vec_oprnds0 = vec_defs[0];
3975 : :
3976 : : /* Arguments are ready. Create the new vector stmt. */
3977 : 64 : for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
3978 : : {
3979 : 32 : size_t k;
3980 : 32 : vargs.truncate (0);
3981 : 64 : for (k = 0; k < nargs; k++)
3982 : : {
3983 : 32 : vec<tree> vec_oprndsk = vec_defs[k];
3984 : 32 : vargs.quick_push (vec_oprndsk[i]);
3985 : 32 : vargs.quick_push (vec_oprndsk[i + 1]);
3986 : : }
3987 : 32 : gcall *call;
3988 : 32 : if (ifn != IFN_LAST)
3989 : : call = gimple_build_call_internal_vec (ifn, vargs);
3990 : : else
3991 : 32 : call = gimple_build_call_vec (fndecl, vargs);
3992 : 32 : new_temp = make_ssa_name (vec_dest, call);
3993 : 32 : gimple_call_set_lhs (call, new_temp);
3994 : 32 : gimple_call_set_nothrow (call, true);
3995 : 32 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3996 : 32 : slp_node->push_vec_def (call);
3997 : : }
3998 : 32 : continue;
3999 : 32 : }
4000 : :
4001 : 0 : for (i = 0; i < nargs; i++)
4002 : : {
4003 : 0 : op = gimple_call_arg (stmt, i);
4004 : 0 : if (j == 0)
4005 : : {
4006 : 0 : vec_defs.quick_push (vNULL);
4007 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info, 2 * ncopies,
4008 : 0 : op, &vec_defs[i], vectypes[i]);
4009 : : }
4010 : 0 : vec_oprnd0 = vec_defs[i][2*j];
4011 : 0 : vec_oprnd1 = vec_defs[i][2*j+1];
4012 : :
4013 : 0 : vargs.quick_push (vec_oprnd0);
4014 : 0 : vargs.quick_push (vec_oprnd1);
4015 : : }
4016 : :
4017 : 0 : gcall *new_stmt = gimple_build_call_vec (fndecl, vargs);
4018 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
4019 : 0 : gimple_call_set_lhs (new_stmt, new_temp);
4020 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4021 : :
4022 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
4023 : : }
4024 : :
4025 : 32 : if (!slp_node)
4026 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
4027 : :
4028 : 64 : for (i = 0; i < nargs; i++)
4029 : : {
4030 : 32 : vec<tree> vec_oprndsi = vec_defs[i];
4031 : 32 : vec_oprndsi.release ();
4032 : : }
4033 : 32 : }
4034 : : else
4035 : : /* No current target implements this case. */
4036 : : return false;
4037 : :
4038 : 3472 : vargs.release ();
4039 : :
4040 : : /* The call in STMT might prevent it from being removed in dce.
4041 : : We however cannot remove it here, due to the way the ssa name
4042 : : it defines is mapped to the new definition. So just replace
4043 : : rhs of the statement with something harmless. */
4044 : :
4045 : 3472 : if (slp_node)
4046 : : return true;
4047 : :
4048 : 0 : stmt_info = vect_orig_stmt (stmt_info);
4049 : 0 : lhs = gimple_get_lhs (stmt_info->stmt);
4050 : :
4051 : 0 : gassign *new_stmt
4052 : 0 : = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
4053 : 0 : vinfo->replace_stmt (gsi, stmt_info, new_stmt);
4054 : :
4055 : 0 : return true;
4056 : 2301909 : }
4057 : :
4058 : :
4059 : : struct simd_call_arg_info
4060 : : {
4061 : : tree vectype;
4062 : : tree op;
4063 : : HOST_WIDE_INT linear_step;
4064 : : enum vect_def_type dt;
4065 : : unsigned int align;
4066 : : bool simd_lane_linear;
4067 : : };
4068 : :
4069 : : /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
4070 : : is linear within simd lane (but not within whole loop), note it in
4071 : : *ARGINFO. */
4072 : :
4073 : : static void
4074 : 3 : vect_simd_lane_linear (tree op, class loop *loop,
4075 : : struct simd_call_arg_info *arginfo)
4076 : : {
4077 : 3 : gimple *def_stmt = SSA_NAME_DEF_STMT (op);
4078 : :
4079 : 3 : if (!is_gimple_assign (def_stmt)
4080 : 3 : || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
4081 : 3 : || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
4082 : 3 : return;
4083 : :
4084 : 0 : tree base = gimple_assign_rhs1 (def_stmt);
4085 : 0 : HOST_WIDE_INT linear_step = 0;
4086 : 0 : tree v = gimple_assign_rhs2 (def_stmt);
4087 : 0 : while (TREE_CODE (v) == SSA_NAME)
4088 : : {
4089 : 0 : tree t;
4090 : 0 : def_stmt = SSA_NAME_DEF_STMT (v);
4091 : 0 : if (is_gimple_assign (def_stmt))
4092 : 0 : switch (gimple_assign_rhs_code (def_stmt))
4093 : : {
4094 : 0 : case PLUS_EXPR:
4095 : 0 : t = gimple_assign_rhs2 (def_stmt);
4096 : 0 : if (linear_step || TREE_CODE (t) != INTEGER_CST)
4097 : : return;
4098 : 0 : base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
4099 : 0 : v = gimple_assign_rhs1 (def_stmt);
4100 : 0 : continue;
4101 : 0 : case MULT_EXPR:
4102 : 0 : t = gimple_assign_rhs2 (def_stmt);
4103 : 0 : if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
4104 : 0 : return;
4105 : 0 : linear_step = tree_to_shwi (t);
4106 : 0 : v = gimple_assign_rhs1 (def_stmt);
4107 : 0 : continue;
4108 : 0 : CASE_CONVERT:
4109 : 0 : t = gimple_assign_rhs1 (def_stmt);
4110 : 0 : if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
4111 : 0 : || (TYPE_PRECISION (TREE_TYPE (v))
4112 : 0 : < TYPE_PRECISION (TREE_TYPE (t))))
4113 : : return;
4114 : 0 : if (!linear_step)
4115 : 0 : linear_step = 1;
4116 : 0 : v = t;
4117 : 0 : continue;
4118 : : default:
4119 : : return;
4120 : : }
4121 : 0 : else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
4122 : 0 : && loop->simduid
4123 : 0 : && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
4124 : 0 : && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
4125 : : == loop->simduid))
4126 : : {
4127 : 0 : if (!linear_step)
4128 : 0 : linear_step = 1;
4129 : 0 : arginfo->linear_step = linear_step;
4130 : 0 : arginfo->op = base;
4131 : 0 : arginfo->simd_lane_linear = true;
4132 : 0 : return;
4133 : : }
4134 : : }
4135 : : }
4136 : :
4137 : : /* Function vectorizable_simd_clone_call.
4138 : :
4139 : : Check if STMT_INFO performs a function call that can be vectorized
4140 : : by calling a simd clone of the function.
4141 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
4142 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
4143 : : Return true if STMT_INFO is vectorizable in this way. */
4144 : :
4145 : : static bool
4146 : 2293530 : vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
4147 : : gimple_stmt_iterator *gsi,
4148 : : gimple **vec_stmt, slp_tree slp_node,
4149 : : stmt_vector_for_cost *)
4150 : : {
4151 : 2293530 : tree vec_dest;
4152 : 2293530 : tree scalar_dest;
4153 : 2293530 : tree op, type;
4154 : 2293530 : tree vec_oprnd0 = NULL_TREE;
4155 : 2293530 : tree vectype;
4156 : 2293530 : poly_uint64 nunits;
4157 : 2293530 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
4158 : 2293530 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
4159 : 2293530 : class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
4160 : 2293530 : tree fndecl, new_temp;
4161 : 2293530 : int ncopies, j;
4162 : 2293530 : auto_vec<simd_call_arg_info> arginfo;
4163 : 2293530 : vec<tree> vargs = vNULL;
4164 : 2293530 : size_t i, nargs;
4165 : 2293530 : tree lhs, rtype, ratype;
4166 : 2293530 : vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
4167 : 2293530 : int masked_call_offset = 0;
4168 : :
4169 : : /* Is STMT a vectorizable call? */
4170 : 2293530 : gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
4171 : 10177 : if (!stmt)
4172 : : return false;
4173 : :
4174 : 10177 : fndecl = gimple_call_fndecl (stmt);
4175 : 10177 : if (fndecl == NULL_TREE
4176 : 10177 : && gimple_call_internal_p (stmt, IFN_MASK_CALL))
4177 : : {
4178 : 166 : fndecl = gimple_call_arg (stmt, 0);
4179 : 166 : gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
4180 : 166 : fndecl = TREE_OPERAND (fndecl, 0);
4181 : 166 : gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
4182 : : masked_call_offset = 1;
4183 : : }
4184 : 10011 : if (fndecl == NULL_TREE)
4185 : : return false;
4186 : :
4187 : 4557 : struct cgraph_node *node = cgraph_node::get (fndecl);
4188 : 4557 : if (node == NULL || node->simd_clones == NULL)
4189 : : return false;
4190 : :
4191 : 1336 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4192 : : return false;
4193 : :
4194 : 1336 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4195 : 0 : && ! vec_stmt)
4196 : : return false;
4197 : :
4198 : 1336 : if (gimple_call_lhs (stmt)
4199 : 1336 : && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
4200 : : return false;
4201 : :
4202 : 1336 : gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
4203 : :
4204 : 1336 : vectype = STMT_VINFO_VECTYPE (stmt_info);
4205 : :
4206 : 2293530 : if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
4207 : : return false;
4208 : :
4209 : : /* Process function arguments. */
4210 : 1336 : nargs = gimple_call_num_args (stmt) - masked_call_offset;
4211 : :
4212 : : /* Bail out if the function has zero arguments. */
4213 : 1336 : if (nargs == 0)
4214 : : return false;
4215 : :
4216 : 1336 : vec<tree>& simd_clone_info = (slp_node ? SLP_TREE_SIMD_CLONE_INFO (slp_node)
4217 : : : STMT_VINFO_SIMD_CLONE_INFO (stmt_info));
4218 : 1336 : if (!vec_stmt)
4219 : 1004 : simd_clone_info.truncate (0);
4220 : 1336 : arginfo.reserve (nargs, true);
4221 : 1336 : auto_vec<slp_tree> slp_op;
4222 : 1336 : slp_op.safe_grow_cleared (nargs);
4223 : :
4224 : 3808 : for (i = 0; i < nargs; i++)
4225 : : {
4226 : 2472 : simd_call_arg_info thisarginfo;
4227 : 2472 : affine_iv iv;
4228 : :
4229 : 2472 : thisarginfo.linear_step = 0;
4230 : 2472 : thisarginfo.align = 0;
4231 : 2472 : thisarginfo.op = NULL_TREE;
4232 : 2472 : thisarginfo.simd_lane_linear = false;
4233 : :
4234 : 2472 : int op_no = i + masked_call_offset;
4235 : 2472 : if (slp_node)
4236 : 2472 : op_no = vect_slp_child_index_for_operand (stmt, op_no, false);
4237 : 4944 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
4238 : 2472 : op_no, &op, &slp_op[i],
4239 : : &thisarginfo.dt, &thisarginfo.vectype)
4240 : 2472 : || thisarginfo.dt == vect_uninitialized_def)
4241 : : {
4242 : 0 : if (dump_enabled_p ())
4243 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4244 : : "use not simple.\n");
4245 : 0 : return false;
4246 : : }
4247 : :
4248 : 2472 : if (thisarginfo.dt == vect_constant_def
4249 : 2472 : || thisarginfo.dt == vect_external_def)
4250 : : {
4251 : : /* With SLP we determine the vector type of constants/externals
4252 : : at analysis time, handling conflicts via
4253 : : vect_maybe_update_slp_op_vectype. At transform time
4254 : : we have a vector type recorded for SLP. */
4255 : 727 : gcc_assert (!vec_stmt
4256 : : || !slp_node
4257 : : || thisarginfo.vectype != NULL_TREE);
4258 : 727 : if (!vec_stmt)
4259 : 597 : thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
4260 : 597 : TREE_TYPE (op),
4261 : : slp_node);
4262 : : }
4263 : : else
4264 : 1745 : gcc_assert (thisarginfo.vectype != NULL_TREE);
4265 : :
4266 : : /* For linear arguments, the analyze phase should have saved
4267 : : the base and step in {STMT_VINFO,SLP_TREE}_SIMD_CLONE_INFO. */
4268 : 2342 : if (vec_stmt
4269 : 1498 : && i * 3 + 4 <= simd_clone_info.length ()
4270 : 2543 : && simd_clone_info[i * 3 + 2])
4271 : : {
4272 : 112 : thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
4273 : 112 : thisarginfo.op = simd_clone_info[i * 3 + 1];
4274 : 112 : thisarginfo.simd_lane_linear
4275 : 112 : = (simd_clone_info[i * 3 + 3] == boolean_true_node);
4276 : : /* If loop has been peeled for alignment, we need to adjust it. */
4277 : 112 : tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
4278 : 112 : tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
4279 : 112 : if (n1 != n2 && !thisarginfo.simd_lane_linear)
4280 : : {
4281 : 0 : tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
4282 : 0 : tree step = simd_clone_info[i * 3 + 2];
4283 : 0 : tree opt = TREE_TYPE (thisarginfo.op);
4284 : 0 : bias = fold_convert (TREE_TYPE (step), bias);
4285 : 0 : bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
4286 : 0 : thisarginfo.op
4287 : 0 : = fold_build2 (POINTER_TYPE_P (opt)
4288 : : ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
4289 : : thisarginfo.op, bias);
4290 : : }
4291 : : }
4292 : 2360 : else if (!vec_stmt
4293 : 1723 : && thisarginfo.dt != vect_constant_def
4294 : 1600 : && thisarginfo.dt != vect_external_def
4295 : 1126 : && loop_vinfo
4296 : 1123 : && TREE_CODE (op) == SSA_NAME
4297 : 2246 : && simple_iv (loop, loop_containing_stmt (stmt), op,
4298 : : &iv, false)
4299 : 2564 : && tree_fits_shwi_p (iv.step))
4300 : : {
4301 : 204 : thisarginfo.linear_step = tree_to_shwi (iv.step);
4302 : 204 : thisarginfo.op = iv.base;
4303 : : }
4304 : 2156 : else if ((thisarginfo.dt == vect_constant_def
4305 : 2156 : || thisarginfo.dt == vect_external_def)
4306 : 2156 : && POINTER_TYPE_P (TREE_TYPE (op)))
4307 : 509 : thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
4308 : : /* Addresses of array elements indexed by GOMP_SIMD_LANE are
4309 : : linear too. */
4310 : 4349 : if (POINTER_TYPE_P (TREE_TYPE (op))
4311 : 595 : && !thisarginfo.linear_step
4312 : 517 : && !vec_stmt
4313 : 469 : && thisarginfo.dt != vect_constant_def
4314 : 469 : && thisarginfo.dt != vect_external_def
4315 : 3 : && loop_vinfo
4316 : 2475 : && TREE_CODE (op) == SSA_NAME)
4317 : 3 : vect_simd_lane_linear (op, loop, &thisarginfo);
4318 : :
4319 : 2472 : arginfo.quick_push (thisarginfo);
4320 : : }
4321 : :
4322 : 1336 : poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
4323 : 1336 : unsigned group_size = slp_node ? SLP_TREE_LANES (slp_node) : 1;
4324 : 1336 : unsigned int badness = 0;
4325 : 1336 : struct cgraph_node *bestn = NULL;
4326 : 1336 : if (vec_stmt)
4327 : 332 : bestn = cgraph_node::get (simd_clone_info[0]);
4328 : : else
4329 : 5724 : for (struct cgraph_node *n = node->simd_clones; n != NULL;
4330 : 4720 : n = n->simdclone->next_clone)
4331 : : {
4332 : 4720 : unsigned int this_badness = 0;
4333 : 4720 : unsigned int num_calls;
4334 : : /* The number of arguments in the call and the number of parameters in
4335 : : the simdclone should match. However, when the simdclone is
4336 : : 'inbranch', it could have one more paramater than nargs when using
4337 : : an inbranch simdclone to call a non-inbranch call, either in a
4338 : : non-masked loop using a all true constant mask, or inside a masked
4339 : : loop using it's mask. */
4340 : 4720 : size_t simd_nargs = n->simdclone->nargs;
4341 : 4720 : if (!masked_call_offset && n->simdclone->inbranch)
4342 : 2423 : simd_nargs--;
4343 : 4720 : if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
4344 : : &num_calls)
4345 : 1766 : || (!n->simdclone->inbranch && (masked_call_offset > 0))
4346 : 1650 : || (nargs != simd_nargs))
4347 : 3070 : continue;
4348 : 1650 : if (num_calls != 1)
4349 : 1084 : this_badness += floor_log2 (num_calls) * 4096;
4350 : 1650 : if (n->simdclone->inbranch)
4351 : 661 : this_badness += 8192;
4352 : :
4353 : : /* If STMT_VINFO_VECTYPE has not been set yet pass the general vector
4354 : : mode, which for targets that use it will determine what ISA we can
4355 : : vectorize this code with. */
4356 : 1650 : machine_mode vector_mode = vinfo->vector_mode;
4357 : 1650 : if (vectype)
4358 : 1650 : vector_mode = TYPE_MODE (vectype);
4359 : 1650 : int target_badness = targetm.simd_clone.usable (n, vector_mode);
4360 : 1650 : if (target_badness < 0)
4361 : 360 : continue;
4362 : 1290 : this_badness += target_badness * 512;
4363 : 4045 : for (i = 0; i < nargs; i++)
4364 : : {
4365 : 2812 : switch (n->simdclone->args[i].arg_type)
4366 : : {
4367 : 1972 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4368 : 1972 : if (!useless_type_conversion_p
4369 : 1972 : (n->simdclone->args[i].orig_type,
4370 : 1972 : TREE_TYPE (gimple_call_arg (stmt,
4371 : : i + masked_call_offset))))
4372 : : i = -1;
4373 : 1972 : else if (arginfo[i].dt == vect_constant_def
4374 : 1867 : || arginfo[i].dt == vect_external_def
4375 : 3777 : || arginfo[i].linear_step)
4376 : 374 : this_badness += 64;
4377 : : break;
4378 : 314 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4379 : 314 : if (arginfo[i].dt != vect_constant_def
4380 : 314 : && arginfo[i].dt != vect_external_def)
4381 : : i = -1;
4382 : : break;
4383 : 312 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4384 : 312 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4385 : 312 : if (arginfo[i].dt == vect_constant_def
4386 : 312 : || arginfo[i].dt == vect_external_def
4387 : 312 : || (arginfo[i].linear_step
4388 : 312 : != n->simdclone->args[i].linear_step))
4389 : : i = -1;
4390 : : break;
4391 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4392 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4393 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4394 : : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4395 : : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4396 : : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4397 : : /* FORNOW */
4398 : : i = -1;
4399 : : break;
4400 : 214 : case SIMD_CLONE_ARG_TYPE_MASK:
4401 : : /* While we can create a traditional data vector from
4402 : : an incoming integer mode mask we have no good way to
4403 : : force generate an integer mode mask from a traditional
4404 : : boolean vector input. */
4405 : 214 : if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4406 : 214 : && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4407 : : i = -1;
4408 : 208 : else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4409 : 208 : && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4410 : 45 : this_badness += 2048;
4411 : : break;
4412 : : }
4413 : 2755 : if (i == (size_t) -1)
4414 : : break;
4415 : 2755 : if (n->simdclone->args[i].alignment > arginfo[i].align)
4416 : : {
4417 : : i = -1;
4418 : : break;
4419 : : }
4420 : 2755 : if (arginfo[i].align)
4421 : 110 : this_badness += (exact_log2 (arginfo[i].align)
4422 : 160 : - exact_log2 (n->simdclone->args[i].alignment));
4423 : : }
4424 : 1290 : if (i == (size_t) -1)
4425 : 57 : continue;
4426 : 1233 : if (masked_call_offset == 0
4427 : 1025 : && n->simdclone->inbranch
4428 : 328 : && n->simdclone->nargs > nargs)
4429 : : {
4430 : 328 : gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
4431 : : SIMD_CLONE_ARG_TYPE_MASK);
4432 : : /* Penalize using a masked SIMD clone in a non-masked loop, that is
4433 : : not in a branch, as we'd have to construct an all-true mask. */
4434 : 328 : if (!loop_vinfo || !LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4435 : 328 : this_badness += 64;
4436 : : }
4437 : 1233 : if (bestn == NULL || this_badness < badness)
4438 : : {
4439 : 4720 : bestn = n;
4440 : 4720 : badness = this_badness;
4441 : : }
4442 : : }
4443 : :
4444 : 1336 : if (bestn == NULL)
4445 : : return false;
4446 : :
4447 : 797 : unsigned int num_mask_args = 0;
4448 : 797 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4449 : 111 : for (i = 0; i < nargs; i++)
4450 : 72 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4451 : 18 : num_mask_args++;
4452 : :
4453 : 2578 : for (i = 0; i < nargs; i++)
4454 : : {
4455 : 1815 : if ((arginfo[i].dt == vect_constant_def
4456 : 1646 : || arginfo[i].dt == vect_external_def)
4457 : 1916 : && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
4458 : : {
4459 : 65 : tree arg_type = TREE_TYPE (gimple_call_arg (stmt,
4460 : : i + masked_call_offset));
4461 : 65 : arginfo[i].vectype = get_vectype_for_scalar_type (vinfo, arg_type,
4462 : : slp_node);
4463 : 65 : if (arginfo[i].vectype == NULL
4464 : 130 : || !constant_multiple_p (bestn->simdclone->simdlen,
4465 : 130 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4466 : 0 : return false;
4467 : : }
4468 : :
4469 : 1815 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR
4470 : 1815 : && VECTOR_BOOLEAN_TYPE_P (bestn->simdclone->args[i].vector_type))
4471 : : {
4472 : 4 : if (dump_enabled_p ())
4473 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4474 : : "vector mask arguments are not supported.\n");
4475 : 4 : return false;
4476 : : }
4477 : :
4478 : 1811 : if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4479 : : {
4480 : 140 : tree clone_arg_vectype = bestn->simdclone->args[i].vector_type;
4481 : 140 : if (bestn->simdclone->mask_mode == VOIDmode)
4482 : : {
4483 : 122 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (clone_arg_vectype),
4484 : 244 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4485 : : {
4486 : : /* FORNOW we only have partial support for vector-type masks
4487 : : that can't hold all of simdlen. */
4488 : 12 : if (dump_enabled_p ())
4489 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4490 : : vect_location,
4491 : : "in-branch vector clones are not yet"
4492 : : " supported for mismatched vector sizes.\n");
4493 : 12 : return false;
4494 : : }
4495 : 110 : if (!expand_vec_cond_expr_p (clone_arg_vectype,
4496 : 110 : arginfo[i].vectype))
4497 : : {
4498 : 6 : if (dump_enabled_p ())
4499 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4500 : : vect_location,
4501 : : "cannot compute mask argument for"
4502 : : " in-branch vector clones.\n");
4503 : 6 : return false;
4504 : : }
4505 : : }
4506 : 18 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4507 : : {
4508 : 18 : if (!SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype))
4509 : 36 : || maybe_ne (exact_div (bestn->simdclone->simdlen,
4510 : : num_mask_args),
4511 : 24 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4512 : : {
4513 : : /* FORNOW we only have partial support for integer-type masks
4514 : : that represent the same number of lanes as the
4515 : : vectorized mask inputs. */
4516 : 12 : if (dump_enabled_p ())
4517 : 6 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4518 : : vect_location,
4519 : : "in-branch vector clones are not yet "
4520 : : "supported for mismatched vector sizes.\n");
4521 : 12 : return false;
4522 : : }
4523 : : }
4524 : : else
4525 : : {
4526 : 0 : if (dump_enabled_p ())
4527 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4528 : : vect_location,
4529 : : "in-branch vector clones not supported"
4530 : : " on this target.\n");
4531 : 0 : return false;
4532 : : }
4533 : : }
4534 : : }
4535 : :
4536 : 763 : fndecl = bestn->decl;
4537 : 763 : nunits = bestn->simdclone->simdlen;
4538 : 763 : if (slp_node)
4539 : 763 : ncopies = vector_unroll_factor (vf * group_size, nunits);
4540 : : else
4541 : 0 : ncopies = vector_unroll_factor (vf, nunits);
4542 : :
4543 : : /* If the function isn't const, only allow it in simd loops where user
4544 : : has asserted that at least nunits consecutive iterations can be
4545 : : performed using SIMD instructions. */
4546 : 761 : if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
4547 : 913 : && gimple_vuse (stmt))
4548 : : return false;
4549 : :
4550 : : /* Sanity check: make sure that at least one copy of the vectorized stmt
4551 : : needs to be generated. */
4552 : 763 : gcc_assert (ncopies >= 1);
4553 : :
4554 : 763 : if (!vec_stmt) /* transformation not required. */
4555 : : {
4556 : 431 : if (slp_node)
4557 : 1421 : for (unsigned i = 0; i < nargs; ++i)
4558 : 990 : if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
4559 : : {
4560 : 0 : if (dump_enabled_p ())
4561 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4562 : : "incompatible vector types for invariants\n");
4563 : 0 : return false;
4564 : : }
4565 : : /* When the original call is pure or const but the SIMD ABI dictates
4566 : : an aggregate return we will have to use a virtual definition and
4567 : : in a loop eventually even need to add a virtual PHI. That's
4568 : : not straight-forward so allow to fix this up via renaming. */
4569 : 431 : if (gimple_call_lhs (stmt)
4570 : 431 : && !gimple_vdef (stmt)
4571 : 768 : && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
4572 : 27 : vinfo->any_known_not_updated_vssa = true;
4573 : : /* ??? For SLP code-gen we end up inserting after the last
4574 : : vector argument def rather than at the original call position
4575 : : so automagic virtual operand updating doesn't work. */
4576 : 862 : if (gimple_vuse (stmt) && slp_node)
4577 : 131 : vinfo->any_known_not_updated_vssa = true;
4578 : 431 : simd_clone_info.safe_push (bestn->decl);
4579 : 1432 : for (i = 0; i < bestn->simdclone->nargs; i++)
4580 : : {
4581 : 1001 : switch (bestn->simdclone->args[i].arg_type)
4582 : : {
4583 : 823 : default:
4584 : 823 : continue;
4585 : 112 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4586 : 112 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4587 : 112 : {
4588 : 112 : simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
4589 : 112 : simd_clone_info.safe_push (arginfo[i].op);
4590 : 196 : tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4591 : 196 : ? size_type_node : TREE_TYPE (arginfo[i].op);
4592 : 112 : tree ls = build_int_cst (lst, arginfo[i].linear_step);
4593 : 112 : simd_clone_info.safe_push (ls);
4594 : 112 : tree sll = arginfo[i].simd_lane_linear
4595 : 112 : ? boolean_true_node : boolean_false_node;
4596 : 112 : simd_clone_info.safe_push (sll);
4597 : : }
4598 : 112 : break;
4599 : 66 : case SIMD_CLONE_ARG_TYPE_MASK:
4600 : 66 : if (loop_vinfo
4601 : 66 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4602 : 0 : vect_record_loop_mask (loop_vinfo,
4603 : : &LOOP_VINFO_MASKS (loop_vinfo),
4604 : : ncopies, vectype, op);
4605 : :
4606 : : break;
4607 : 823 : }
4608 : : }
4609 : :
4610 : 431 : if (!bestn->simdclone->inbranch && loop_vinfo)
4611 : : {
4612 : 364 : if (dump_enabled_p ()
4613 : 364 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4614 : 3 : dump_printf_loc (MSG_NOTE, vect_location,
4615 : : "can't use a fully-masked loop because a"
4616 : : " non-masked simd clone was selected.\n");
4617 : 364 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
4618 : : }
4619 : :
4620 : 431 : STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
4621 : 431 : DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
4622 : : /* vect_model_simple_cost (vinfo, ncopies, dt, slp_node, cost_vec); */
4623 : 431 : return true;
4624 : : }
4625 : :
4626 : : /* Transform. */
4627 : :
4628 : 332 : if (dump_enabled_p ())
4629 : 228 : dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4630 : :
4631 : : /* Handle def. */
4632 : 332 : scalar_dest = gimple_call_lhs (stmt);
4633 : 332 : vec_dest = NULL_TREE;
4634 : 332 : rtype = NULL_TREE;
4635 : 332 : ratype = NULL_TREE;
4636 : 332 : if (scalar_dest)
4637 : : {
4638 : 332 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
4639 : 332 : rtype = TREE_TYPE (TREE_TYPE (fndecl));
4640 : 332 : if (TREE_CODE (rtype) == ARRAY_TYPE)
4641 : : {
4642 : 9 : ratype = rtype;
4643 : 9 : rtype = TREE_TYPE (ratype);
4644 : : }
4645 : : }
4646 : :
4647 : 664 : auto_vec<vec<tree> > vec_oprnds;
4648 : 332 : auto_vec<unsigned> vec_oprnds_i;
4649 : 332 : vec_oprnds_i.safe_grow_cleared (nargs, true);
4650 : 332 : if (slp_node)
4651 : : {
4652 : 332 : vec_oprnds.reserve_exact (nargs);
4653 : 332 : vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
4654 : : }
4655 : : else
4656 : 0 : vec_oprnds.safe_grow_cleared (nargs, true);
4657 : 774 : for (j = 0; j < ncopies; ++j)
4658 : : {
4659 : 442 : poly_uint64 callee_nelements;
4660 : 442 : poly_uint64 caller_nelements;
4661 : : /* Build argument list for the vectorized call. */
4662 : 442 : if (j == 0)
4663 : 332 : vargs.create (nargs);
4664 : : else
4665 : 110 : vargs.truncate (0);
4666 : :
4667 : 1504 : for (i = 0; i < nargs; i++)
4668 : : {
4669 : 1062 : unsigned int k, l, m, o;
4670 : 1062 : tree atype;
4671 : 1062 : op = gimple_call_arg (stmt, i + masked_call_offset);
4672 : 1062 : switch (bestn->simdclone->args[i].arg_type)
4673 : : {
4674 : 790 : case SIMD_CLONE_ARG_TYPE_VECTOR:
4675 : 790 : atype = bestn->simdclone->args[i].vector_type;
4676 : 790 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4677 : 790 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4678 : 790 : o = vector_unroll_factor (nunits, callee_nelements);
4679 : 1804 : for (m = j * o; m < (j + 1) * o; m++)
4680 : : {
4681 : 1014 : if (known_lt (callee_nelements, caller_nelements))
4682 : : {
4683 : 516 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
4684 : 258 : if (!constant_multiple_p (caller_nelements,
4685 : : callee_nelements, &k))
4686 : 0 : gcc_unreachable ();
4687 : :
4688 : 258 : gcc_assert ((k & (k - 1)) == 0);
4689 : 258 : if (m == 0)
4690 : : {
4691 : 57 : if (!slp_node)
4692 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info,
4693 : 0 : ncopies * o / k, op,
4694 : 0 : &vec_oprnds[i]);
4695 : 57 : vec_oprnds_i[i] = 0;
4696 : 57 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4697 : : }
4698 : : else
4699 : : {
4700 : 201 : vec_oprnd0 = arginfo[i].op;
4701 : 201 : if ((m & (k - 1)) == 0)
4702 : 72 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4703 : : }
4704 : 258 : arginfo[i].op = vec_oprnd0;
4705 : 258 : vec_oprnd0
4706 : 258 : = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
4707 : 258 : bitsize_int (prec),
4708 : 258 : bitsize_int ((m & (k - 1)) * prec));
4709 : 258 : gassign *new_stmt
4710 : 258 : = gimple_build_assign (make_ssa_name (atype),
4711 : : vec_oprnd0);
4712 : 258 : vect_finish_stmt_generation (vinfo, stmt_info,
4713 : : new_stmt, gsi);
4714 : 258 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4715 : : }
4716 : : else
4717 : : {
4718 : 756 : if (!constant_multiple_p (callee_nelements,
4719 : : caller_nelements, &k))
4720 : 0 : gcc_unreachable ();
4721 : 756 : gcc_assert ((k & (k - 1)) == 0);
4722 : 756 : vec<constructor_elt, va_gc> *ctor_elts;
4723 : 756 : if (k != 1)
4724 : 14 : vec_alloc (ctor_elts, k);
4725 : : else
4726 : 742 : ctor_elts = NULL;
4727 : 784 : for (l = 0; l < k; l++)
4728 : : {
4729 : 770 : if (m == 0 && l == 0)
4730 : : {
4731 : 423 : if (!slp_node)
4732 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info,
4733 : 0 : k * o * ncopies,
4734 : : op,
4735 : 0 : &vec_oprnds[i]);
4736 : 423 : vec_oprnds_i[i] = 0;
4737 : 423 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4738 : : }
4739 : : else
4740 : 347 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4741 : 770 : arginfo[i].op = vec_oprnd0;
4742 : 770 : if (k == 1)
4743 : : break;
4744 : 28 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4745 : : vec_oprnd0);
4746 : : }
4747 : 756 : if (k == 1)
4748 : 742 : if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
4749 : : atype))
4750 : : {
4751 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
4752 : : vec_oprnd0);
4753 : 0 : gassign *new_stmt
4754 : 0 : = gimple_build_assign (make_ssa_name (atype),
4755 : : vec_oprnd0);
4756 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
4757 : : new_stmt, gsi);
4758 : 0 : vargs.safe_push (gimple_get_lhs (new_stmt));
4759 : : }
4760 : : else
4761 : 742 : vargs.safe_push (vec_oprnd0);
4762 : : else
4763 : : {
4764 : 14 : vec_oprnd0 = build_constructor (atype, ctor_elts);
4765 : 14 : gassign *new_stmt
4766 : 14 : = gimple_build_assign (make_ssa_name (atype),
4767 : : vec_oprnd0);
4768 : 14 : vect_finish_stmt_generation (vinfo, stmt_info,
4769 : : new_stmt, gsi);
4770 : 14 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4771 : : }
4772 : : }
4773 : : }
4774 : : break;
4775 : 55 : case SIMD_CLONE_ARG_TYPE_MASK:
4776 : 55 : if (bestn->simdclone->mask_mode == VOIDmode)
4777 : : {
4778 : 52 : atype = bestn->simdclone->args[i].vector_type;
4779 : 52 : tree elt_type = TREE_TYPE (atype);
4780 : 52 : tree one = fold_convert (elt_type, integer_one_node);
4781 : 52 : tree zero = fold_convert (elt_type, integer_zero_node);
4782 : 52 : callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4783 : 52 : caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4784 : 52 : o = vector_unroll_factor (nunits, callee_nelements);
4785 : 104 : for (m = j * o; m < (j + 1) * o; m++)
4786 : : {
4787 : 52 : if (maybe_lt (callee_nelements, caller_nelements))
4788 : : {
4789 : : /* The mask type has fewer elements than simdlen. */
4790 : :
4791 : : /* FORNOW */
4792 : 0 : gcc_unreachable ();
4793 : : }
4794 : 52 : else if (known_eq (callee_nelements, caller_nelements))
4795 : : {
4796 : : /* The SIMD clone function has the same number of
4797 : : elements as the current function. */
4798 : 52 : if (m == 0)
4799 : : {
4800 : 52 : if (!slp_node)
4801 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info,
4802 : 0 : o * ncopies,
4803 : : op,
4804 : 0 : &vec_oprnds[i]);
4805 : 52 : vec_oprnds_i[i] = 0;
4806 : : }
4807 : 52 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4808 : 52 : if (loop_vinfo
4809 : 52 : && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4810 : : {
4811 : 0 : vec_loop_masks *loop_masks
4812 : : = &LOOP_VINFO_MASKS (loop_vinfo);
4813 : 0 : tree loop_mask
4814 : 0 : = vect_get_loop_mask (loop_vinfo, gsi,
4815 : : loop_masks, ncopies,
4816 : 0 : vectype, j);
4817 : 0 : vec_oprnd0
4818 : 0 : = prepare_vec_mask (loop_vinfo,
4819 : 0 : TREE_TYPE (loop_mask),
4820 : : loop_mask, vec_oprnd0,
4821 : : gsi);
4822 : 0 : loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
4823 : : loop_mask });
4824 : :
4825 : : }
4826 : 52 : vec_oprnd0
4827 : 52 : = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
4828 : : build_vector_from_val (atype, one),
4829 : : build_vector_from_val (atype, zero));
4830 : 52 : gassign *new_stmt
4831 : 52 : = gimple_build_assign (make_ssa_name (atype),
4832 : : vec_oprnd0);
4833 : 52 : vect_finish_stmt_generation (vinfo, stmt_info,
4834 : : new_stmt, gsi);
4835 : 52 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4836 : : }
4837 : : else
4838 : : {
4839 : : /* The mask type has more elements than simdlen. */
4840 : :
4841 : : /* FORNOW */
4842 : 0 : gcc_unreachable ();
4843 : : }
4844 : : }
4845 : : }
4846 : 3 : else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4847 : : {
4848 : 3 : atype = bestn->simdclone->args[i].vector_type;
4849 : : /* Guess the number of lanes represented by atype. */
4850 : 3 : poly_uint64 atype_subparts
4851 : 3 : = exact_div (bestn->simdclone->simdlen,
4852 : : num_mask_args);
4853 : 3 : o = vector_unroll_factor (nunits, atype_subparts);
4854 : 6 : for (m = j * o; m < (j + 1) * o; m++)
4855 : : {
4856 : 3 : if (m == 0)
4857 : : {
4858 : 3 : if (!slp_node)
4859 : 0 : vect_get_vec_defs_for_operand (vinfo, stmt_info,
4860 : 0 : o * ncopies,
4861 : : op,
4862 : 0 : &vec_oprnds[i]);
4863 : 3 : vec_oprnds_i[i] = 0;
4864 : : }
4865 : 3 : if (maybe_lt (atype_subparts,
4866 : 3 : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4867 : : {
4868 : : /* The mask argument has fewer elements than the
4869 : : input vector. */
4870 : : /* FORNOW */
4871 : 0 : gcc_unreachable ();
4872 : : }
4873 : 3 : else if (known_eq (atype_subparts,
4874 : : TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4875 : : {
4876 : : /* The vector mask argument matches the input
4877 : : in the number of lanes, but not necessarily
4878 : : in the mode. */
4879 : 3 : vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4880 : 3 : tree st = lang_hooks.types.type_for_mode
4881 : 3 : (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
4882 : 3 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
4883 : : vec_oprnd0);
4884 : 3 : gassign *new_stmt
4885 : 3 : = gimple_build_assign (make_ssa_name (st),
4886 : : vec_oprnd0);
4887 : 3 : vect_finish_stmt_generation (vinfo, stmt_info,
4888 : : new_stmt, gsi);
4889 : 3 : if (!types_compatible_p (atype, st))
4890 : : {
4891 : 3 : new_stmt
4892 : 3 : = gimple_build_assign (make_ssa_name (atype),
4893 : : NOP_EXPR,
4894 : : gimple_assign_lhs
4895 : : (new_stmt));
4896 : 3 : vect_finish_stmt_generation (vinfo, stmt_info,
4897 : : new_stmt, gsi);
4898 : : }
4899 : 3 : vargs.safe_push (gimple_assign_lhs (new_stmt));
4900 : : }
4901 : : else
4902 : : {
4903 : : /* The mask argument has more elements than the
4904 : : input vector. */
4905 : : /* FORNOW */
4906 : 0 : gcc_unreachable ();
4907 : : }
4908 : : }
4909 : : }
4910 : : else
4911 : 0 : gcc_unreachable ();
4912 : : break;
4913 : 102 : case SIMD_CLONE_ARG_TYPE_UNIFORM:
4914 : 102 : vargs.safe_push (op);
4915 : 102 : break;
4916 : 115 : case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4917 : 115 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4918 : 115 : if (j == 0)
4919 : : {
4920 : 112 : gimple_seq stmts;
4921 : 112 : arginfo[i].op
4922 : 112 : = force_gimple_operand (unshare_expr (arginfo[i].op),
4923 : : &stmts, true, NULL_TREE);
4924 : 112 : if (stmts != NULL)
4925 : : {
4926 : 0 : basic_block new_bb;
4927 : 0 : edge pe = loop_preheader_edge (loop);
4928 : 0 : new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4929 : 0 : gcc_assert (!new_bb);
4930 : : }
4931 : 112 : if (arginfo[i].simd_lane_linear)
4932 : : {
4933 : 0 : vargs.safe_push (arginfo[i].op);
4934 : 0 : break;
4935 : : }
4936 : 112 : tree phi_res = copy_ssa_name (op);
4937 : 112 : gphi *new_phi = create_phi_node (phi_res, loop->header);
4938 : 112 : add_phi_arg (new_phi, arginfo[i].op,
4939 : : loop_preheader_edge (loop), UNKNOWN_LOCATION);
4940 : 112 : enum tree_code code
4941 : 196 : = POINTER_TYPE_P (TREE_TYPE (op))
4942 : 112 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4943 : 196 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4944 : 196 : ? sizetype : TREE_TYPE (op);
4945 : 112 : poly_widest_int cst
4946 : 112 : = wi::mul (bestn->simdclone->args[i].linear_step,
4947 : 112 : ncopies * nunits);
4948 : 112 : tree tcst = wide_int_to_tree (type, cst);
4949 : 112 : tree phi_arg = copy_ssa_name (op);
4950 : 112 : gassign *new_stmt
4951 : 112 : = gimple_build_assign (phi_arg, code, phi_res, tcst);
4952 : 112 : gimple_stmt_iterator si = gsi_after_labels (loop->header);
4953 : 112 : gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
4954 : 112 : add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
4955 : : UNKNOWN_LOCATION);
4956 : 112 : arginfo[i].op = phi_res;
4957 : 112 : vargs.safe_push (phi_res);
4958 : 112 : }
4959 : : else
4960 : : {
4961 : 3 : enum tree_code code
4962 : 6 : = POINTER_TYPE_P (TREE_TYPE (op))
4963 : 3 : ? POINTER_PLUS_EXPR : PLUS_EXPR;
4964 : 6 : tree type = POINTER_TYPE_P (TREE_TYPE (op))
4965 : 6 : ? sizetype : TREE_TYPE (op);
4966 : 3 : poly_widest_int cst
4967 : 3 : = wi::mul (bestn->simdclone->args[i].linear_step,
4968 : 3 : j * nunits);
4969 : 3 : tree tcst = wide_int_to_tree (type, cst);
4970 : 3 : new_temp = make_ssa_name (TREE_TYPE (op));
4971 : 3 : gassign *new_stmt
4972 : 6 : = gimple_build_assign (new_temp, code,
4973 : 3 : arginfo[i].op, tcst);
4974 : 3 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4975 : 3 : vargs.safe_push (new_temp);
4976 : 3 : }
4977 : : break;
4978 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4979 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4980 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4981 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4982 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4983 : 0 : case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4984 : 0 : default:
4985 : 0 : gcc_unreachable ();
4986 : : }
4987 : : }
4988 : :
4989 : 442 : if (masked_call_offset == 0
4990 : 387 : && bestn->simdclone->inbranch
4991 : 7 : && bestn->simdclone->nargs > nargs)
4992 : : {
4993 : 7 : unsigned long m, o;
4994 : 7 : size_t mask_i = bestn->simdclone->nargs - 1;
4995 : 7 : tree mask;
4996 : 7 : gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
4997 : : SIMD_CLONE_ARG_TYPE_MASK);
4998 : :
4999 : 7 : tree masktype = bestn->simdclone->args[mask_i].vector_type;
5000 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
5001 : : /* Guess the number of lanes represented by masktype. */
5002 : 1 : callee_nelements = exact_div (bestn->simdclone->simdlen,
5003 : 1 : bestn->simdclone->nargs - nargs);
5004 : : else
5005 : 6 : callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
5006 : 7 : o = vector_unroll_factor (nunits, callee_nelements);
5007 : 14 : for (m = j * o; m < (j + 1) * o; m++)
5008 : : {
5009 : 7 : if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
5010 : : {
5011 : 0 : vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
5012 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
5013 : : ncopies, masktype, j);
5014 : : }
5015 : : else
5016 : 7 : mask = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
5017 : :
5018 : 7 : gassign *new_stmt;
5019 : 7 : if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
5020 : : {
5021 : : /* This means we are dealing with integer mask modes.
5022 : : First convert to an integer type with the same size as
5023 : : the current vector type. */
5024 : 1 : unsigned HOST_WIDE_INT intermediate_size
5025 : 1 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
5026 : 1 : tree mid_int_type =
5027 : 1 : build_nonstandard_integer_type (intermediate_size, 1);
5028 : 1 : mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
5029 : 1 : new_stmt
5030 : 1 : = gimple_build_assign (make_ssa_name (mid_int_type),
5031 : : mask);
5032 : 1 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5033 : : /* Then zero-extend to the mask mode. */
5034 : 1 : mask = fold_build1 (NOP_EXPR, masktype,
5035 : : gimple_get_lhs (new_stmt));
5036 : : }
5037 : 6 : else if (bestn->simdclone->mask_mode == VOIDmode)
5038 : : {
5039 : 6 : tree one = fold_convert (TREE_TYPE (masktype),
5040 : : integer_one_node);
5041 : 6 : tree zero = fold_convert (TREE_TYPE (masktype),
5042 : : integer_zero_node);
5043 : 6 : mask = build3 (VEC_COND_EXPR, masktype, mask,
5044 : : build_vector_from_val (masktype, one),
5045 : : build_vector_from_val (masktype, zero));
5046 : : }
5047 : : else
5048 : 0 : gcc_unreachable ();
5049 : :
5050 : 7 : new_stmt = gimple_build_assign (make_ssa_name (masktype), mask);
5051 : 7 : vect_finish_stmt_generation (vinfo, stmt_info,
5052 : : new_stmt, gsi);
5053 : 7 : mask = gimple_assign_lhs (new_stmt);
5054 : 7 : vargs.safe_push (mask);
5055 : : }
5056 : : }
5057 : :
5058 : 442 : gcall *new_call = gimple_build_call_vec (fndecl, vargs);
5059 : 442 : if (vec_dest)
5060 : : {
5061 : 442 : gcc_assert (ratype
5062 : : || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
5063 : 442 : if (ratype)
5064 : 15 : new_temp = create_tmp_var (ratype);
5065 : 427 : else if (useless_type_conversion_p (vectype, rtype))
5066 : 405 : new_temp = make_ssa_name (vec_dest, new_call);
5067 : : else
5068 : 22 : new_temp = make_ssa_name (rtype, new_call);
5069 : 442 : gimple_call_set_lhs (new_call, new_temp);
5070 : : }
5071 : 442 : vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
5072 : 442 : gimple *new_stmt = new_call;
5073 : :
5074 : 442 : if (vec_dest)
5075 : : {
5076 : 442 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
5077 : : {
5078 : 21 : unsigned int k, l;
5079 : 42 : poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
5080 : 42 : poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
5081 : 21 : k = vector_unroll_factor (nunits,
5082 : : TYPE_VECTOR_SUBPARTS (vectype));
5083 : 21 : gcc_assert ((k & (k - 1)) == 0);
5084 : 75 : for (l = 0; l < k; l++)
5085 : : {
5086 : 54 : tree t;
5087 : 54 : if (ratype)
5088 : : {
5089 : 42 : t = build_fold_addr_expr (new_temp);
5090 : 42 : t = build2 (MEM_REF, vectype, t,
5091 : 42 : build_int_cst (TREE_TYPE (t), l * bytes));
5092 : : }
5093 : : else
5094 : 12 : t = build3 (BIT_FIELD_REF, vectype, new_temp,
5095 : 12 : bitsize_int (prec), bitsize_int (l * prec));
5096 : 54 : new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
5097 : 54 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5098 : :
5099 : 54 : if (j == 0 && l == 0)
5100 : 15 : *vec_stmt = new_stmt;
5101 : 54 : if (slp_node)
5102 : 54 : SLP_TREE_VEC_DEFS (slp_node)
5103 : 54 : .quick_push (gimple_assign_lhs (new_stmt));
5104 : : else
5105 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5106 : : }
5107 : :
5108 : 21 : if (ratype)
5109 : 15 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5110 : 21 : continue;
5111 : 21 : }
5112 : 421 : else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
5113 : : {
5114 : 16 : unsigned int k;
5115 : 16 : if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
5116 : 16 : TYPE_VECTOR_SUBPARTS (rtype), &k))
5117 : 0 : gcc_unreachable ();
5118 : 16 : gcc_assert ((k & (k - 1)) == 0);
5119 : 16 : if ((j & (k - 1)) == 0)
5120 : 8 : vec_alloc (ret_ctor_elts, k);
5121 : 16 : if (ratype)
5122 : : {
5123 : 0 : unsigned int m, o;
5124 : 0 : o = vector_unroll_factor (nunits,
5125 : : TYPE_VECTOR_SUBPARTS (rtype));
5126 : 0 : for (m = 0; m < o; m++)
5127 : : {
5128 : 0 : tree tem = build4 (ARRAY_REF, rtype, new_temp,
5129 : 0 : size_int (m), NULL_TREE, NULL_TREE);
5130 : 0 : new_stmt = gimple_build_assign (make_ssa_name (rtype),
5131 : : tem);
5132 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
5133 : : new_stmt, gsi);
5134 : 0 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
5135 : : gimple_assign_lhs (new_stmt));
5136 : : }
5137 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5138 : : }
5139 : : else
5140 : 16 : CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
5141 : 16 : if ((j & (k - 1)) != k - 1)
5142 : 8 : continue;
5143 : 8 : vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
5144 : 8 : new_stmt
5145 : 8 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5146 : 8 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5147 : :
5148 : 8 : if ((unsigned) j == k - 1)
5149 : 8 : *vec_stmt = new_stmt;
5150 : 8 : if (slp_node)
5151 : 8 : SLP_TREE_VEC_DEFS (slp_node)
5152 : 8 : .quick_push (gimple_assign_lhs (new_stmt));
5153 : : else
5154 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5155 : 8 : continue;
5156 : 8 : }
5157 : 405 : else if (ratype)
5158 : : {
5159 : 0 : tree t = build_fold_addr_expr (new_temp);
5160 : 0 : t = build2 (MEM_REF, vectype, t,
5161 : 0 : build_int_cst (TREE_TYPE (t), 0));
5162 : 0 : new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
5163 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5164 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5165 : : }
5166 : 405 : else if (!useless_type_conversion_p (vectype, rtype))
5167 : : {
5168 : 0 : vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
5169 : 0 : new_stmt
5170 : 0 : = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5171 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5172 : : }
5173 : : }
5174 : :
5175 : 405 : if (j == 0)
5176 : 309 : *vec_stmt = new_stmt;
5177 : 405 : if (slp_node)
5178 : 405 : SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
5179 : : else
5180 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5181 : : }
5182 : :
5183 : 1081 : for (i = 0; i < nargs; ++i)
5184 : : {
5185 : 749 : vec<tree> oprndsi = vec_oprnds[i];
5186 : 749 : oprndsi.release ();
5187 : : }
5188 : 332 : vargs.release ();
5189 : :
5190 : : /* Mark the clone as no longer being a candidate for GC. */
5191 : 332 : bestn->gc_candidate = false;
5192 : :
5193 : : /* The call in STMT might prevent it from being removed in dce.
5194 : : We however cannot remove it here, due to the way the ssa name
5195 : : it defines is mapped to the new definition. So just replace
5196 : : rhs of the statement with something harmless. */
5197 : :
5198 : 332 : if (slp_node)
5199 : : return true;
5200 : :
5201 : 0 : gimple *new_stmt;
5202 : 0 : if (scalar_dest)
5203 : : {
5204 : 0 : type = TREE_TYPE (scalar_dest);
5205 : 0 : lhs = gimple_call_lhs (vect_orig_stmt (stmt_info)->stmt);
5206 : 0 : new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
5207 : : }
5208 : : else
5209 : 0 : new_stmt = gimple_build_nop ();
5210 : 0 : vinfo->replace_stmt (gsi, vect_orig_stmt (stmt_info), new_stmt);
5211 : 0 : unlink_stmt_vdef (stmt);
5212 : :
5213 : 0 : return true;
5214 : 1336 : }
5215 : :
5216 : :
5217 : : /* Function vect_gen_widened_results_half
5218 : :
5219 : : Create a vector stmt whose code, type, number of arguments, and result
5220 : : variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
5221 : : VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at GSI.
5222 : : In the case that CODE is a CALL_EXPR, this means that a call to DECL
5223 : : needs to be created (DECL is a function-decl of a target-builtin).
5224 : : STMT_INFO is the original scalar stmt that we are vectorizing. */
5225 : :
5226 : : static gimple *
5227 : 23176 : vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
5228 : : tree vec_oprnd0, tree vec_oprnd1, int op_type,
5229 : : tree vec_dest, gimple_stmt_iterator *gsi,
5230 : : stmt_vec_info stmt_info)
5231 : : {
5232 : 23176 : gimple *new_stmt;
5233 : 23176 : tree new_temp;
5234 : :
5235 : : /* Generate half of the widened result: */
5236 : 23176 : if (op_type != binary_op)
5237 : 22180 : vec_oprnd1 = NULL;
5238 : 23176 : new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
5239 : 23176 : new_temp = make_ssa_name (vec_dest, new_stmt);
5240 : 23176 : gimple_set_lhs (new_stmt, new_temp);
5241 : 23176 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5242 : :
5243 : 23176 : return new_stmt;
5244 : : }
5245 : :
5246 : :
5247 : : /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
5248 : : For multi-step conversions store the resulting vectors and call the function
5249 : : recursively. When NARROW_SRC_P is true, there's still a conversion after
5250 : : narrowing, don't store the vectors in the SLP_NODE or in vector info of
5251 : : the scalar statement(or in STMT_VINFO_RELATED_STMT chain). */
5252 : :
5253 : : static void
5254 : 9550 : vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
5255 : : int multi_step_cvt,
5256 : : stmt_vec_info stmt_info,
5257 : : vec<tree> &vec_dsts,
5258 : : gimple_stmt_iterator *gsi,
5259 : : slp_tree slp_node, code_helper code,
5260 : : bool narrow_src_p)
5261 : : {
5262 : 9550 : unsigned int i;
5263 : 9550 : tree vop0, vop1, new_tmp, vec_dest;
5264 : :
5265 : 9550 : vec_dest = vec_dsts.pop ();
5266 : :
5267 : 22048 : for (i = 0; i < vec_oprnds->length (); i += 2)
5268 : : {
5269 : : /* Create demotion operation. */
5270 : 12498 : vop0 = (*vec_oprnds)[i];
5271 : 12498 : vop1 = (*vec_oprnds)[i + 1];
5272 : 12498 : gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
5273 : 12498 : new_tmp = make_ssa_name (vec_dest, new_stmt);
5274 : 12498 : gimple_set_lhs (new_stmt, new_tmp);
5275 : 12498 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5276 : 12498 : if (multi_step_cvt || narrow_src_p)
5277 : : /* Store the resulting vector for next recursive call,
5278 : : or return the resulting vector_tmp for NARROW FLOAT_EXPR. */
5279 : 4489 : (*vec_oprnds)[i/2] = new_tmp;
5280 : : else
5281 : : {
5282 : : /* This is the last step of the conversion sequence. Store the
5283 : : vectors in SLP_NODE or in vector info of the scalar statement
5284 : : (or in STMT_VINFO_RELATED_STMT chain). */
5285 : 8009 : if (slp_node)
5286 : 8009 : slp_node->push_vec_def (new_stmt);
5287 : : else
5288 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5289 : : }
5290 : : }
5291 : :
5292 : : /* For multi-step demotion operations we first generate demotion operations
5293 : : from the source type to the intermediate types, and then combine the
5294 : : results (stored in VEC_OPRNDS) in demotion operation to the destination
5295 : : type. */
5296 : 9550 : if (multi_step_cvt)
5297 : : {
5298 : : /* At each level of recursion we have half of the operands we had at the
5299 : : previous level. */
5300 : 2077 : vec_oprnds->truncate ((i+1)/2);
5301 : 2077 : vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
5302 : : multi_step_cvt - 1,
5303 : : stmt_info, vec_dsts, gsi,
5304 : 2077 : slp_node, VEC_PACK_TRUNC_EXPR,
5305 : : narrow_src_p);
5306 : : }
5307 : :
5308 : 9550 : vec_dsts.quick_push (vec_dest);
5309 : 9550 : }
5310 : :
5311 : :
5312 : : /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
5313 : : and VEC_OPRNDS1, for a binary operation associated with scalar statement
5314 : : STMT_INFO. For multi-step conversions store the resulting vectors and
5315 : : call the function recursively. */
5316 : :
5317 : : static void
5318 : 9039 : vect_create_vectorized_promotion_stmts (vec_info *vinfo,
5319 : : vec<tree> *vec_oprnds0,
5320 : : vec<tree> *vec_oprnds1,
5321 : : stmt_vec_info stmt_info, tree vec_dest,
5322 : : gimple_stmt_iterator *gsi,
5323 : : code_helper ch1,
5324 : : code_helper ch2, int op_type)
5325 : : {
5326 : 9039 : int i;
5327 : 9039 : tree vop0, vop1, new_tmp1, new_tmp2;
5328 : 9039 : gimple *new_stmt1, *new_stmt2;
5329 : 9039 : vec<tree> vec_tmp = vNULL;
5330 : :
5331 : 9039 : vec_tmp.create (vec_oprnds0->length () * 2);
5332 : 29666 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5333 : : {
5334 : 11588 : if (op_type == binary_op)
5335 : 498 : vop1 = (*vec_oprnds1)[i];
5336 : : else
5337 : : vop1 = NULL_TREE;
5338 : :
5339 : : /* Generate the two halves of promotion operation. */
5340 : 11588 : new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
5341 : : op_type, vec_dest, gsi,
5342 : : stmt_info);
5343 : 11588 : new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
5344 : : op_type, vec_dest, gsi,
5345 : : stmt_info);
5346 : 11588 : if (is_gimple_call (new_stmt1))
5347 : : {
5348 : 0 : new_tmp1 = gimple_call_lhs (new_stmt1);
5349 : 0 : new_tmp2 = gimple_call_lhs (new_stmt2);
5350 : : }
5351 : : else
5352 : : {
5353 : 11588 : new_tmp1 = gimple_assign_lhs (new_stmt1);
5354 : 11588 : new_tmp2 = gimple_assign_lhs (new_stmt2);
5355 : : }
5356 : :
5357 : : /* Store the results for the next step. */
5358 : 11588 : vec_tmp.quick_push (new_tmp1);
5359 : 11588 : vec_tmp.quick_push (new_tmp2);
5360 : : }
5361 : :
5362 : 9039 : vec_oprnds0->release ();
5363 : 9039 : *vec_oprnds0 = vec_tmp;
5364 : 9039 : }
5365 : :
5366 : : /* Create vectorized promotion stmts for widening stmts using only half the
5367 : : potential vector size for input. */
5368 : : static void
5369 : 14 : vect_create_half_widening_stmts (vec_info *vinfo,
5370 : : vec<tree> *vec_oprnds0,
5371 : : vec<tree> *vec_oprnds1,
5372 : : stmt_vec_info stmt_info, tree vec_dest,
5373 : : gimple_stmt_iterator *gsi,
5374 : : code_helper code1,
5375 : : int op_type)
5376 : : {
5377 : 14 : int i;
5378 : 14 : tree vop0, vop1;
5379 : 14 : gimple *new_stmt1;
5380 : 14 : gimple *new_stmt2;
5381 : 14 : gimple *new_stmt3;
5382 : 14 : vec<tree> vec_tmp = vNULL;
5383 : :
5384 : 14 : vec_tmp.create (vec_oprnds0->length ());
5385 : 28 : FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5386 : : {
5387 : 14 : tree new_tmp1, new_tmp2, new_tmp3, out_type;
5388 : :
5389 : 14 : gcc_assert (op_type == binary_op);
5390 : 14 : vop1 = (*vec_oprnds1)[i];
5391 : :
5392 : : /* Widen the first vector input. */
5393 : 14 : out_type = TREE_TYPE (vec_dest);
5394 : 14 : new_tmp1 = make_ssa_name (out_type);
5395 : 14 : new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
5396 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
5397 : 14 : if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
5398 : : {
5399 : : /* Widen the second vector input. */
5400 : 14 : new_tmp2 = make_ssa_name (out_type);
5401 : 14 : new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
5402 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
5403 : : /* Perform the operation. With both vector inputs widened. */
5404 : 14 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
5405 : : }
5406 : : else
5407 : : {
5408 : : /* Perform the operation. With the single vector input widened. */
5409 : 0 : new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
5410 : : }
5411 : :
5412 : 14 : new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
5413 : 14 : gimple_assign_set_lhs (new_stmt3, new_tmp3);
5414 : 14 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
5415 : :
5416 : : /* Store the results for the next step. */
5417 : 14 : vec_tmp.quick_push (new_tmp3);
5418 : : }
5419 : :
5420 : 14 : vec_oprnds0->release ();
5421 : 14 : *vec_oprnds0 = vec_tmp;
5422 : 14 : }
5423 : :
5424 : :
5425 : : /* Check if STMT_INFO performs a conversion operation that can be vectorized.
5426 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
5427 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5428 : : Return true if STMT_INFO is vectorizable in this way. */
5429 : :
5430 : : static bool
5431 : 2312193 : vectorizable_conversion (vec_info *vinfo,
5432 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5433 : : gimple **vec_stmt, slp_tree slp_node,
5434 : : stmt_vector_for_cost *cost_vec)
5435 : : {
5436 : 2312193 : tree vec_dest, cvt_op = NULL_TREE;
5437 : 2312193 : tree scalar_dest;
5438 : 2312193 : tree op0, op1 = NULL_TREE;
5439 : 2312193 : tree_code tc1;
5440 : 2312193 : code_helper code, code1, code2;
5441 : 2312193 : code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
5442 : 2312193 : tree new_temp;
5443 : 2312193 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
5444 : 2312193 : int ndts = 2;
5445 : 2312193 : poly_uint64 nunits_in;
5446 : 2312193 : poly_uint64 nunits_out;
5447 : 2312193 : tree vectype_out, vectype_in;
5448 : 2312193 : int i;
5449 : 2312193 : tree lhs_type, rhs_type;
5450 : : /* For conversions between floating point and integer, there're 2 NARROW
5451 : : cases. NARROW_SRC is for FLOAT_EXPR, means
5452 : : integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
5453 : : This is safe when the range of the source integer can fit into the lower
5454 : : precision. NARROW_DST is for FIX_TRUNC_EXPR, means
5455 : : floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
5456 : : For other conversions, when there's narrowing, NARROW_DST is used as
5457 : : default. */
5458 : 2312193 : enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
5459 : 2312193 : vec<tree> vec_oprnds0 = vNULL;
5460 : 2312193 : vec<tree> vec_oprnds1 = vNULL;
5461 : 2312193 : tree vop0;
5462 : 2312193 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5463 : 2312193 : int multi_step_cvt = 0;
5464 : 2312193 : vec<tree> interm_types = vNULL;
5465 : 2312193 : tree intermediate_type, cvt_type = NULL_TREE;
5466 : 2312193 : int op_type;
5467 : 2312193 : unsigned short fltsz;
5468 : :
5469 : : /* Is STMT a vectorizable conversion? */
5470 : :
5471 : 2312193 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5472 : : return false;
5473 : :
5474 : 2312193 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5475 : 158036 : && ! vec_stmt)
5476 : : return false;
5477 : :
5478 : 2154157 : gimple* stmt = stmt_info->stmt;
5479 : 2154157 : if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
5480 : : return false;
5481 : :
5482 : 2085166 : if (gimple_get_lhs (stmt) == NULL_TREE
5483 : 2085166 : || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5484 : 841537 : return false;
5485 : :
5486 : 1243629 : if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5487 : : return false;
5488 : :
5489 : 1243629 : if (is_gimple_assign (stmt))
5490 : : {
5491 : 1235649 : code = gimple_assign_rhs_code (stmt);
5492 : 1235649 : op_type = TREE_CODE_LENGTH ((tree_code) code);
5493 : : }
5494 : 7980 : else if (gimple_call_internal_p (stmt))
5495 : : {
5496 : 4124 : code = gimple_call_internal_fn (stmt);
5497 : 4124 : op_type = gimple_call_num_args (stmt);
5498 : : }
5499 : : else
5500 : : return false;
5501 : :
5502 : 1239773 : bool widen_arith = (code == WIDEN_MULT_EXPR
5503 : 1237376 : || code == WIDEN_LSHIFT_EXPR
5504 : 2477149 : || widening_fn_p (code));
5505 : :
5506 : 1237376 : if (!widen_arith
5507 : 1237376 : && !CONVERT_EXPR_CODE_P (code)
5508 : : && code != FIX_TRUNC_EXPR
5509 : : && code != FLOAT_EXPR)
5510 : : return false;
5511 : :
5512 : : /* Check types of lhs and rhs. */
5513 : 145418 : scalar_dest = gimple_get_lhs (stmt);
5514 : 145418 : lhs_type = TREE_TYPE (scalar_dest);
5515 : 145418 : vectype_out = SLP_TREE_VECTYPE (slp_node);
5516 : :
5517 : : /* Check the operands of the operation. */
5518 : 145418 : slp_tree slp_op0, slp_op1 = NULL;
5519 : 145418 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
5520 : : 0, &op0, &slp_op0, &dt[0], &vectype_in))
5521 : : {
5522 : 0 : if (dump_enabled_p ())
5523 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5524 : : "use not simple.\n");
5525 : 0 : return false;
5526 : : }
5527 : :
5528 : 145418 : rhs_type = TREE_TYPE (op0);
5529 : 145418 : if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
5530 : 279064 : && !((INTEGRAL_TYPE_P (lhs_type)
5531 : 120955 : && INTEGRAL_TYPE_P (rhs_type))
5532 : : || (SCALAR_FLOAT_TYPE_P (lhs_type)
5533 : 6808 : && SCALAR_FLOAT_TYPE_P (rhs_type))))
5534 : : return false;
5535 : :
5536 : 139535 : if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5537 : 269542 : && ((INTEGRAL_TYPE_P (lhs_type)
5538 : 108712 : && !type_has_mode_precision_p (lhs_type))
5539 : 129568 : || (INTEGRAL_TYPE_P (rhs_type)
5540 : 120745 : && !type_has_mode_precision_p (rhs_type))))
5541 : : {
5542 : 900 : if (dump_enabled_p ())
5543 : 10 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5544 : : "type conversion to/from bit-precision unsupported."
5545 : : "\n");
5546 : 900 : return false;
5547 : : }
5548 : :
5549 : 138635 : if (op_type == binary_op)
5550 : : {
5551 : 2397 : gcc_assert (code == WIDEN_MULT_EXPR
5552 : : || code == WIDEN_LSHIFT_EXPR
5553 : : || widening_fn_p (code));
5554 : :
5555 : 2397 : op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
5556 : 0 : gimple_call_arg (stmt, 0);
5557 : 2397 : tree vectype1_in;
5558 : 2397 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1,
5559 : : &op1, &slp_op1, &dt[1], &vectype1_in))
5560 : : {
5561 : 0 : if (dump_enabled_p ())
5562 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5563 : : "use not simple.\n");
5564 : 0 : return false;
5565 : : }
5566 : : /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
5567 : : OP1. */
5568 : 2397 : if (!vectype_in)
5569 : 117 : vectype_in = vectype1_in;
5570 : : }
5571 : :
5572 : : /* If op0 is an external or constant def, infer the vector type
5573 : : from the scalar type. */
5574 : 138635 : if (!vectype_in)
5575 : 20730 : vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
5576 : 138635 : if (vec_stmt)
5577 : 19426 : gcc_assert (vectype_in);
5578 : 138635 : if (!vectype_in)
5579 : : {
5580 : 1482 : if (dump_enabled_p ())
5581 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5582 : : "no vectype for scalar type %T\n", rhs_type);
5583 : :
5584 : 1482 : return false;
5585 : : }
5586 : :
5587 : 137153 : if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
5588 : 146681 : && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
5589 : : {
5590 : 205 : if (dump_enabled_p ())
5591 : 28 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5592 : : "can't convert between boolean and non "
5593 : : "boolean vectors %T\n", rhs_type);
5594 : :
5595 : 205 : return false;
5596 : : }
5597 : :
5598 : 136948 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
5599 : 136948 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5600 : 136948 : if (known_eq (nunits_out, nunits_in))
5601 : 69213 : if (widen_arith)
5602 : : modifier = WIDEN;
5603 : : else
5604 : 136948 : modifier = NONE;
5605 : 67735 : else if (multiple_p (nunits_out, nunits_in))
5606 : : modifier = NARROW_DST;
5607 : : else
5608 : : {
5609 : 37840 : gcc_checking_assert (multiple_p (nunits_in, nunits_out));
5610 : : modifier = WIDEN;
5611 : : }
5612 : :
5613 : 136948 : bool found_mode = false;
5614 : 136948 : scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
5615 : 136948 : scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
5616 : 136948 : opt_scalar_mode rhs_mode_iter;
5617 : 136948 : auto_vec<std::pair<tree, tree_code>, 2> converts;
5618 : :
5619 : : /* Supportable by target? */
5620 : 136948 : switch (modifier)
5621 : : {
5622 : 68978 : case NONE:
5623 : 68978 : if (code != FIX_TRUNC_EXPR
5624 : : && code != FLOAT_EXPR
5625 : : && !CONVERT_EXPR_CODE_P (code))
5626 : : return false;
5627 : 68978 : gcc_assert (code.is_tree_code ());
5628 : 68978 : if (supportable_indirect_convert_operation (code,
5629 : : vectype_out, vectype_in,
5630 : : converts, op0, slp_op0))
5631 : : {
5632 : 17438 : gcc_assert (converts.length () <= 2);
5633 : 17438 : if (converts.length () == 1)
5634 : 17364 : code1 = converts[0].second;
5635 : : else
5636 : : {
5637 : 74 : cvt_type = NULL_TREE;
5638 : 74 : multi_step_cvt = converts.length () - 1;
5639 : 74 : codecvt1 = converts[0].second;
5640 : 74 : code1 = converts[1].second;
5641 : 74 : interm_types.safe_push (converts[0].first);
5642 : : }
5643 : : break;
5644 : : }
5645 : :
5646 : : /* FALLTHRU */
5647 : 51540 : unsupported:
5648 : 57365 : if (dump_enabled_p ())
5649 : 5534 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5650 : : "conversion not supported by target.\n");
5651 : : return false;
5652 : :
5653 : 38075 : case WIDEN:
5654 : 38075 : if (known_eq (nunits_in, nunits_out))
5655 : : {
5656 : 470 : if (!(code.is_tree_code ()
5657 : 235 : && supportable_half_widening_operation ((tree_code) code,
5658 : : vectype_out, vectype_in,
5659 : : &tc1)))
5660 : 71 : goto unsupported;
5661 : 164 : code1 = tc1;
5662 : 164 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5663 : : break;
5664 : : }
5665 : 37840 : if (supportable_widening_operation (vinfo, code, stmt_info,
5666 : : vectype_out, vectype_in, &code1,
5667 : : &code2, &multi_step_cvt,
5668 : : &interm_types))
5669 : : {
5670 : : /* Binary widening operation can only be supported directly by the
5671 : : architecture. */
5672 : 35150 : gcc_assert (!(multi_step_cvt && op_type == binary_op));
5673 : : break;
5674 : : }
5675 : :
5676 : 2690 : if (code != FLOAT_EXPR
5677 : 3012 : || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
5678 : 2529 : goto unsupported;
5679 : :
5680 : 161 : fltsz = GET_MODE_SIZE (lhs_mode);
5681 : 236 : FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
5682 : : {
5683 : 236 : rhs_mode = rhs_mode_iter.require ();
5684 : 472 : if (GET_MODE_SIZE (rhs_mode) > fltsz)
5685 : : break;
5686 : :
5687 : 236 : cvt_type
5688 : 236 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5689 : 236 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5690 : 236 : if (cvt_type == NULL_TREE)
5691 : 0 : goto unsupported;
5692 : :
5693 : 472 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5694 : : {
5695 : 56 : tc1 = ERROR_MARK;
5696 : 56 : gcc_assert (code.is_tree_code ());
5697 : 56 : if (!supportable_convert_operation ((tree_code) code, vectype_out,
5698 : : cvt_type, &tc1))
5699 : 22 : goto unsupported;
5700 : 34 : codecvt1 = tc1;
5701 : : }
5702 : 180 : else if (!supportable_widening_operation (vinfo, code,
5703 : : stmt_info, vectype_out,
5704 : : cvt_type, &codecvt1,
5705 : : &codecvt2, &multi_step_cvt,
5706 : : &interm_types))
5707 : 75 : continue;
5708 : : else
5709 : 105 : gcc_assert (multi_step_cvt == 0);
5710 : :
5711 : 139 : if (supportable_widening_operation (vinfo, NOP_EXPR, stmt_info,
5712 : : cvt_type,
5713 : : vectype_in, &code1,
5714 : : &code2, &multi_step_cvt,
5715 : : &interm_types))
5716 : : {
5717 : : found_mode = true;
5718 : : break;
5719 : : }
5720 : : }
5721 : :
5722 : 139 : if (!found_mode)
5723 : 0 : goto unsupported;
5724 : :
5725 : 278 : if (GET_MODE_SIZE (rhs_mode) == fltsz)
5726 : 34 : codecvt2 = ERROR_MARK;
5727 : : else
5728 : : {
5729 : 105 : multi_step_cvt++;
5730 : 105 : interm_types.safe_push (cvt_type);
5731 : 105 : cvt_type = NULL_TREE;
5732 : : }
5733 : : break;
5734 : :
5735 : 29895 : case NARROW_DST:
5736 : 29895 : gcc_assert (op_type == unary_op);
5737 : 29895 : if (supportable_narrowing_operation (code, vectype_out, vectype_in,
5738 : : &code1, &multi_step_cvt,
5739 : : &interm_types))
5740 : : break;
5741 : :
5742 : 10419 : if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
5743 : 580 : goto unsupported;
5744 : :
5745 : 2893 : if (code == FIX_TRUNC_EXPR)
5746 : : {
5747 : 274 : cvt_type
5748 : 274 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5749 : 274 : cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5750 : 274 : if (cvt_type == NULL_TREE)
5751 : 0 : goto unsupported;
5752 : 274 : if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
5753 : : &tc1))
5754 : 272 : codecvt1 = tc1;
5755 : : else
5756 : 2 : goto unsupported;
5757 : 272 : if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
5758 : : &code1, &multi_step_cvt,
5759 : : &interm_types))
5760 : : break;
5761 : : }
5762 : : /* If op0 can be represented with low precision integer,
5763 : : truncate it to cvt_type and the do FLOAT_EXPR. */
5764 : 2619 : else if (code == FLOAT_EXPR)
5765 : : {
5766 : 54 : wide_int op_min_value, op_max_value;
5767 : 54 : tree def;
5768 : : /* ??? Merge ranges in case of more than one lane. */
5769 : 54 : if (SLP_TREE_LANES (slp_op0) != 1
5770 : 52 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
5771 : 106 : || !vect_get_range_info (def, &op_min_value, &op_max_value))
5772 : 52 : goto unsupported;
5773 : :
5774 : 2 : cvt_type
5775 : 2 : = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
5776 : 2 : if (cvt_type == NULL_TREE
5777 : 2 : || (wi::min_precision (op_max_value, SIGNED)
5778 : 2 : > TYPE_PRECISION (cvt_type))
5779 : 4 : || (wi::min_precision (op_min_value, SIGNED)
5780 : 2 : > TYPE_PRECISION (cvt_type)))
5781 : 0 : goto unsupported;
5782 : :
5783 : 2 : cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
5784 : 2 : if (cvt_type == NULL_TREE)
5785 : 0 : goto unsupported;
5786 : 2 : if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
5787 : : &code1, &multi_step_cvt,
5788 : : &interm_types))
5789 : 0 : goto unsupported;
5790 : 2 : if (supportable_convert_operation ((tree_code) code, vectype_out,
5791 : : cvt_type, &tc1))
5792 : : {
5793 : 2 : codecvt1 = tc1;
5794 : 2 : modifier = NARROW_SRC;
5795 : 2 : break;
5796 : : }
5797 : 54 : }
5798 : :
5799 : 2569 : goto unsupported;
5800 : :
5801 : : default:
5802 : : gcc_unreachable ();
5803 : : }
5804 : :
5805 : 79583 : if (!vec_stmt) /* transformation not required. */
5806 : : {
5807 : 60157 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
5808 : 60157 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in))
5809 : : {
5810 : 0 : if (dump_enabled_p ())
5811 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5812 : : "incompatible vector types for invariants\n");
5813 : 0 : return false;
5814 : : }
5815 : 60157 : DUMP_VECT_SCOPE ("vectorizable_conversion");
5816 : 60157 : if (modifier == NONE)
5817 : : {
5818 : 13249 : STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
5819 : 13249 : vect_model_simple_cost (vinfo, (1 + multi_step_cvt),
5820 : : dt, ndts, slp_node, cost_vec);
5821 : : }
5822 : 46908 : else if (modifier == NARROW_SRC || modifier == NARROW_DST)
5823 : : {
5824 : 19219 : STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
5825 : : /* The final packing step produces one vector result per copy. */
5826 : 19219 : unsigned int nvectors = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5827 : 19219 : vect_model_promotion_demotion_cost (stmt_info, dt, nvectors,
5828 : : multi_step_cvt, cost_vec,
5829 : : widen_arith);
5830 : : }
5831 : : else
5832 : : {
5833 : 27689 : STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
5834 : : /* The initial unpacking step produces two vector results
5835 : : per copy. MULTI_STEP_CVT is 0 for a single conversion,
5836 : : so >> MULTI_STEP_CVT divides by 2^(number of steps - 1). */
5837 : 27689 : unsigned int nvectors
5838 : 27689 : = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) >> multi_step_cvt;
5839 : 27689 : vect_model_promotion_demotion_cost (stmt_info, dt, nvectors,
5840 : : multi_step_cvt, cost_vec,
5841 : : widen_arith);
5842 : : }
5843 : 60157 : interm_types.release ();
5844 : 60157 : return true;
5845 : : }
5846 : :
5847 : : /* Transform. */
5848 : 19426 : if (dump_enabled_p ())
5849 : 4122 : dump_printf_loc (MSG_NOTE, vect_location, "transform conversion.\n");
5850 : :
5851 : 19426 : if (op_type == binary_op)
5852 : : {
5853 : 458 : if (CONSTANT_CLASS_P (op0))
5854 : 0 : op0 = fold_convert (TREE_TYPE (op1), op0);
5855 : 458 : else if (CONSTANT_CLASS_P (op1))
5856 : 204 : op1 = fold_convert (TREE_TYPE (op0), op1);
5857 : : }
5858 : :
5859 : : /* In case of multi-step conversion, we first generate conversion operations
5860 : : to the intermediate types, and then from that types to the final one.
5861 : : We create vector destinations for the intermediate type (TYPES) received
5862 : : from supportable_*_operation, and store them in the correct order
5863 : : for future use in vect_create_vectorized_*_stmts (). */
5864 : 38852 : auto_vec<tree> vec_dsts (multi_step_cvt + 1);
5865 : 19426 : bool widen_or_narrow_float_p
5866 : 19426 : = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
5867 : 19426 : vec_dest = vect_create_destination_var (scalar_dest,
5868 : : widen_or_narrow_float_p
5869 : : ? cvt_type : vectype_out);
5870 : 19426 : vec_dsts.quick_push (vec_dest);
5871 : :
5872 : 19426 : if (multi_step_cvt)
5873 : : {
5874 : 6268 : for (i = interm_types.length () - 1;
5875 : 6521 : interm_types.iterate (i, &intermediate_type); i--)
5876 : : {
5877 : 3387 : vec_dest = vect_create_destination_var (scalar_dest,
5878 : : intermediate_type);
5879 : 3387 : vec_dsts.quick_push (vec_dest);
5880 : : }
5881 : : }
5882 : :
5883 : 19426 : if (cvt_type)
5884 : 69 : vec_dest = vect_create_destination_var (scalar_dest,
5885 : : widen_or_narrow_float_p
5886 : : ? vectype_out : cvt_type);
5887 : :
5888 : 19426 : switch (modifier)
5889 : : {
5890 : 4189 : case NONE:
5891 : 4189 : vect_get_vec_defs (vinfo, stmt_info, slp_node, 1,
5892 : : op0, vectype_in, &vec_oprnds0);
5893 : : /* vec_dest is intermediate type operand when multi_step_cvt. */
5894 : 4189 : if (multi_step_cvt)
5895 : : {
5896 : 21 : cvt_op = vec_dest;
5897 : 21 : vec_dest = vec_dsts[0];
5898 : : }
5899 : :
5900 : 8703 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5901 : : {
5902 : : /* Arguments are ready, create the new vector stmt. */
5903 : 4514 : gimple* new_stmt;
5904 : 4514 : if (multi_step_cvt)
5905 : : {
5906 : 21 : gcc_assert (multi_step_cvt == 1);
5907 : 21 : new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
5908 : 21 : new_temp = make_ssa_name (cvt_op, new_stmt);
5909 : 21 : gimple_assign_set_lhs (new_stmt, new_temp);
5910 : 21 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5911 : 21 : vop0 = new_temp;
5912 : : }
5913 : 4514 : new_stmt = vect_gimple_build (vec_dest, code1, vop0);
5914 : 4514 : new_temp = make_ssa_name (vec_dest, new_stmt);
5915 : 4514 : gimple_set_lhs (new_stmt, new_temp);
5916 : 4514 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5917 : :
5918 : 4514 : slp_node->push_vec_def (new_stmt);
5919 : : }
5920 : : break;
5921 : :
5922 : 7764 : case WIDEN:
5923 : : /* In case the vectorization factor (VF) is bigger than the number
5924 : : of elements that we can fit in a vectype (nunits), we have to
5925 : : generate more than one vector stmt - i.e - we need to "unroll"
5926 : : the vector stmt by a factor VF/nunits. */
5927 : 7764 : vect_get_vec_defs (vinfo, stmt_info, slp_node, 1,
5928 : : op0, vectype_in, &vec_oprnds0,
5929 : 7764 : code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
5930 : : vectype_in, &vec_oprnds1);
5931 : 7764 : if (code == WIDEN_LSHIFT_EXPR)
5932 : : {
5933 : 0 : int oprnds_size = vec_oprnds0.length ();
5934 : 0 : vec_oprnds1.create (oprnds_size);
5935 : 0 : for (i = 0; i < oprnds_size; ++i)
5936 : 0 : vec_oprnds1.quick_push (op1);
5937 : : }
5938 : : /* Arguments are ready. Create the new vector stmts. */
5939 : 16817 : for (i = multi_step_cvt; i >= 0; i--)
5940 : : {
5941 : 9053 : tree this_dest = vec_dsts[i];
5942 : 9053 : code_helper c1 = code1, c2 = code2;
5943 : 9053 : if (i == 0 && codecvt2 != ERROR_MARK)
5944 : : {
5945 : 48 : c1 = codecvt1;
5946 : 48 : c2 = codecvt2;
5947 : : }
5948 : 9053 : if (known_eq (nunits_out, nunits_in))
5949 : 14 : vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
5950 : : stmt_info, this_dest, gsi, c1,
5951 : : op_type);
5952 : : else
5953 : 9039 : vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
5954 : : &vec_oprnds1, stmt_info,
5955 : : this_dest, gsi,
5956 : : c1, c2, op_type);
5957 : : }
5958 : :
5959 : 28016 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5960 : : {
5961 : 20252 : gimple *new_stmt;
5962 : 20252 : if (cvt_type)
5963 : : {
5964 : 120 : new_temp = make_ssa_name (vec_dest);
5965 : 120 : new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5966 : 120 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5967 : : }
5968 : : else
5969 : 20132 : new_stmt = SSA_NAME_DEF_STMT (vop0);
5970 : :
5971 : 20252 : slp_node->push_vec_def (new_stmt);
5972 : : }
5973 : : break;
5974 : :
5975 : 7473 : case NARROW_SRC:
5976 : 7473 : case NARROW_DST:
5977 : : /* In case the vectorization factor (VF) is bigger than the number
5978 : : of elements that we can fit in a vectype (nunits), we have to
5979 : : generate more than one vector stmt - i.e - we need to "unroll"
5980 : : the vector stmt by a factor VF/nunits. */
5981 : 7473 : vect_get_vec_defs (vinfo, stmt_info, slp_node, 1,
5982 : : op0, vectype_in, &vec_oprnds0);
5983 : : /* Arguments are ready. Create the new vector stmts. */
5984 : 7473 : if (cvt_type && modifier == NARROW_DST)
5985 : 153 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
5986 : : {
5987 : 124 : new_temp = make_ssa_name (vec_dest);
5988 : 124 : gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
5989 : 124 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5990 : 124 : vec_oprnds0[i] = new_temp;
5991 : : }
5992 : :
5993 : 7473 : vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
5994 : : multi_step_cvt,
5995 : : stmt_info, vec_dsts, gsi,
5996 : : slp_node, code1,
5997 : : modifier == NARROW_SRC);
5998 : : /* After demoting op0 to cvt_type, convert it to dest. */
5999 : 7473 : if (cvt_type && code == FLOAT_EXPR)
6000 : : {
6001 : 4 : for (unsigned int i = 0; i != vec_oprnds0.length() / 2; i++)
6002 : : {
6003 : : /* Arguments are ready, create the new vector stmt. */
6004 : 1 : gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
6005 : 1 : gimple *new_stmt
6006 : 1 : = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
6007 : 1 : new_temp = make_ssa_name (vec_dest, new_stmt);
6008 : 1 : gimple_set_lhs (new_stmt, new_temp);
6009 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6010 : :
6011 : : /* This is the last step of the conversion sequence. Store the
6012 : : vectors in SLP_NODE or in vector info of the scalar statement
6013 : : (or in STMT_VINFO_RELATED_STMT chain). */
6014 : 1 : slp_node->push_vec_def (new_stmt);
6015 : : }
6016 : : }
6017 : : break;
6018 : : }
6019 : :
6020 : 19426 : vec_oprnds0.release ();
6021 : 19426 : vec_oprnds1.release ();
6022 : 19426 : interm_types.release ();
6023 : :
6024 : 19426 : return true;
6025 : 136948 : }
6026 : :
6027 : : /* Return true if we can assume from the scalar form of STMT_INFO that
6028 : : neither the scalar nor the vector forms will generate code. STMT_INFO
6029 : : is known not to involve a data reference. */
6030 : :
6031 : : bool
6032 : 827969 : vect_nop_conversion_p (stmt_vec_info stmt_info)
6033 : : {
6034 : 827969 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6035 : 648289 : if (!stmt)
6036 : : return false;
6037 : :
6038 : 648289 : tree lhs = gimple_assign_lhs (stmt);
6039 : 648289 : tree_code code = gimple_assign_rhs_code (stmt);
6040 : 648289 : tree rhs = gimple_assign_rhs1 (stmt);
6041 : :
6042 : 648289 : if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
6043 : : return true;
6044 : :
6045 : 646461 : if (CONVERT_EXPR_CODE_P (code))
6046 : 156976 : return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
6047 : :
6048 : : return false;
6049 : : }
6050 : :
6051 : : /* Function vectorizable_assignment.
6052 : :
6053 : : Check if STMT_INFO performs an assignment (copy) that can be vectorized.
6054 : : If VEC_STMT is also passed, vectorize the STMT_INFO: create a vectorized
6055 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6056 : : Return true if STMT_INFO is vectorizable in this way. */
6057 : :
6058 : : static bool
6059 : 1823876 : vectorizable_assignment (vec_info *vinfo,
6060 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6061 : : gimple **vec_stmt, slp_tree slp_node,
6062 : : stmt_vector_for_cost *cost_vec)
6063 : : {
6064 : 1823876 : tree vec_dest;
6065 : 1823876 : tree scalar_dest;
6066 : 1823876 : tree op;
6067 : 1823876 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6068 : 1823876 : tree new_temp;
6069 : 1823876 : enum vect_def_type dt[1] = {vect_unknown_def_type};
6070 : 1823876 : int ndts = 1;
6071 : 1823876 : int ncopies;
6072 : 1823876 : int i;
6073 : 1823876 : vec<tree> vec_oprnds = vNULL;
6074 : 1823876 : tree vop;
6075 : 1823876 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6076 : 1823876 : enum tree_code code;
6077 : 1823876 : tree vectype_in;
6078 : :
6079 : 1823876 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6080 : : return false;
6081 : :
6082 : 1823876 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6083 : 158036 : && ! vec_stmt)
6084 : : return false;
6085 : :
6086 : : /* Is vectorizable assignment? */
6087 : 3341678 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6088 : 1588035 : if (!stmt)
6089 : : return false;
6090 : :
6091 : 1588035 : scalar_dest = gimple_assign_lhs (stmt);
6092 : 1588035 : if (TREE_CODE (scalar_dest) != SSA_NAME)
6093 : : return false;
6094 : :
6095 : 747332 : if (STMT_VINFO_DATA_REF (stmt_info))
6096 : : return false;
6097 : :
6098 : 323013 : code = gimple_assign_rhs_code (stmt);
6099 : 323013 : if (!(gimple_assign_single_p (stmt)
6100 : 321663 : || code == PAREN_EXPR
6101 : 320094 : || CONVERT_EXPR_CODE_P (code)))
6102 : : return false;
6103 : :
6104 : 81353 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6105 : 81353 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
6106 : :
6107 : : /* Multiple types in SLP are handled by creating the appropriate number of
6108 : : vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6109 : : case of SLP. */
6110 : 81353 : if (slp_node)
6111 : : ncopies = 1;
6112 : : else
6113 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
6114 : :
6115 : 0 : gcc_assert (ncopies >= 1);
6116 : :
6117 : 81353 : slp_tree slp_op;
6118 : 81353 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &op, &slp_op,
6119 : : &dt[0], &vectype_in))
6120 : : {
6121 : 0 : if (dump_enabled_p ())
6122 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6123 : : "use not simple.\n");
6124 : 0 : return false;
6125 : : }
6126 : 81353 : if (!vectype_in)
6127 : 19626 : vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
6128 : :
6129 : : /* We can handle VIEW_CONVERT conversions that do not change the number
6130 : : of elements or the vector size or other conversions when the component
6131 : : types are nop-convertible. */
6132 : 81353 : if (!vectype_in
6133 : 79744 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
6134 : 73552 : || (code == VIEW_CONVERT_EXPR
6135 : 2170 : && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
6136 : 2170 : GET_MODE_SIZE (TYPE_MODE (vectype_in))))
6137 : 154905 : || (CONVERT_EXPR_CODE_P (code)
6138 : 70804 : && !tree_nop_conversion_p (TREE_TYPE (vectype),
6139 : 70804 : TREE_TYPE (vectype_in))))
6140 : 10850 : return false;
6141 : :
6142 : 211397 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
6143 : : {
6144 : 0 : if (dump_enabled_p ())
6145 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6146 : : "can't convert between boolean and non "
6147 : 0 : "boolean vectors %T\n", TREE_TYPE (op));
6148 : :
6149 : 0 : return false;
6150 : : }
6151 : :
6152 : : /* We do not handle bit-precision changes. */
6153 : 70503 : if ((CONVERT_EXPR_CODE_P (code)
6154 : 2748 : || code == VIEW_CONVERT_EXPR)
6155 : 68840 : && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6156 : 67517 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6157 : 68532 : || (INTEGRAL_TYPE_P (TREE_TYPE (op))
6158 : 62810 : && !type_has_mode_precision_p (TREE_TYPE (op))))
6159 : : /* But a conversion that does not change the bit-pattern is ok. */
6160 : 71210 : && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6161 : 707 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
6162 : 707 : && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
6163 : 707 : > TYPE_PRECISION (TREE_TYPE (op)))
6164 : 399 : && TYPE_UNSIGNED (TREE_TYPE (op)))
6165 : 332 : || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
6166 : 332 : == TYPE_PRECISION (TREE_TYPE (op))))))
6167 : : {
6168 : 270 : if (dump_enabled_p ())
6169 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6170 : : "type conversion to/from bit-precision "
6171 : : "unsupported.\n");
6172 : 270 : return false;
6173 : : }
6174 : :
6175 : 70233 : if (!vec_stmt) /* transformation not required. */
6176 : : {
6177 : 56319 : if (slp_node
6178 : 56319 : && !vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
6179 : : {
6180 : 0 : if (dump_enabled_p ())
6181 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6182 : : "incompatible vector types for invariants\n");
6183 : 0 : return false;
6184 : : }
6185 : 56319 : STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
6186 : 56319 : DUMP_VECT_SCOPE ("vectorizable_assignment");
6187 : 56319 : if (!vect_nop_conversion_p (stmt_info))
6188 : 1336 : vect_model_simple_cost (vinfo, ncopies, dt, ndts, slp_node, cost_vec);
6189 : 56319 : return true;
6190 : : }
6191 : :
6192 : : /* Transform. */
6193 : 13914 : if (dump_enabled_p ())
6194 : 3428 : dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
6195 : :
6196 : : /* Handle def. */
6197 : 13914 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6198 : :
6199 : : /* Handle use. */
6200 : 13914 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies, op, &vec_oprnds);
6201 : :
6202 : : /* Arguments are ready. create the new vector stmt. */
6203 : 30611 : FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
6204 : : {
6205 : 16697 : if (CONVERT_EXPR_CODE_P (code)
6206 : 639 : || code == VIEW_CONVERT_EXPR)
6207 : 16150 : vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
6208 : 16697 : gassign *new_stmt = gimple_build_assign (vec_dest, vop);
6209 : 16697 : new_temp = make_ssa_name (vec_dest, new_stmt);
6210 : 16697 : gimple_assign_set_lhs (new_stmt, new_temp);
6211 : 16697 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6212 : 16697 : if (slp_node)
6213 : 16697 : slp_node->push_vec_def (new_stmt);
6214 : : else
6215 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6216 : : }
6217 : 13914 : if (!slp_node)
6218 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
6219 : :
6220 : 13914 : vec_oprnds.release ();
6221 : 13914 : return true;
6222 : : }
6223 : :
6224 : :
6225 : : /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
6226 : : either as shift by a scalar or by a vector. */
6227 : :
6228 : : bool
6229 : 266554 : vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
6230 : : {
6231 : 266554 : optab optab;
6232 : 266554 : tree vectype;
6233 : :
6234 : 266554 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
6235 : 266554 : if (!vectype)
6236 : : return false;
6237 : :
6238 : 266554 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6239 : 266554 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6240 : : return true;
6241 : :
6242 : 232902 : optab = optab_for_tree_code (code, vectype, optab_vector);
6243 : 232902 : if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6244 : : return true;
6245 : :
6246 : : return false;
6247 : : }
6248 : :
6249 : :
6250 : : /* Function vectorizable_shift.
6251 : :
6252 : : Check if STMT_INFO performs a shift operation that can be vectorized.
6253 : : If VEC_STMT is also passed, vectorize the STMT_INFO: create a vectorized
6254 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6255 : : Return true if STMT_INFO is vectorizable in this way. */
6256 : :
6257 : : static bool
6258 : 1484248 : vectorizable_shift (vec_info *vinfo,
6259 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6260 : : gimple **vec_stmt, slp_tree slp_node,
6261 : : stmt_vector_for_cost *cost_vec)
6262 : : {
6263 : 1484248 : tree vec_dest;
6264 : 1484248 : tree scalar_dest;
6265 : 1484248 : tree op0, op1 = NULL;
6266 : 1484248 : tree vec_oprnd1 = NULL_TREE;
6267 : 1484248 : tree vectype;
6268 : 1484248 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6269 : 1484248 : enum tree_code code;
6270 : 1484248 : machine_mode vec_mode;
6271 : 1484248 : tree new_temp;
6272 : 1484248 : optab optab;
6273 : 1484248 : int icode;
6274 : 1484248 : machine_mode optab_op2_mode;
6275 : 1484248 : enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
6276 : 1484248 : int ndts = 2;
6277 : 1484248 : poly_uint64 nunits_in;
6278 : 1484248 : poly_uint64 nunits_out;
6279 : 1484248 : tree vectype_out;
6280 : 1484248 : tree op1_vectype;
6281 : 1484248 : int ncopies;
6282 : 1484248 : int i;
6283 : 1484248 : vec<tree> vec_oprnds0 = vNULL;
6284 : 1484248 : vec<tree> vec_oprnds1 = vNULL;
6285 : 1484248 : tree vop0, vop1;
6286 : 1484248 : unsigned int k;
6287 : 1484248 : bool scalar_shift_arg = true;
6288 : 1484248 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6289 : 1484248 : bool incompatible_op1_vectype_p = false;
6290 : :
6291 : 1484248 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6292 : : return false;
6293 : :
6294 : 1484248 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6295 : 9581 : && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
6296 : 9581 : && ! vec_stmt)
6297 : : return false;
6298 : :
6299 : : /* Is STMT a vectorizable binary/unary operation? */
6300 : 2839279 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6301 : 1398465 : if (!stmt)
6302 : : return false;
6303 : :
6304 : 1398465 : if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6305 : : return false;
6306 : :
6307 : 673187 : code = gimple_assign_rhs_code (stmt);
6308 : :
6309 : 673187 : if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
6310 : : || code == RROTATE_EXPR))
6311 : : return false;
6312 : :
6313 : 57396 : scalar_dest = gimple_assign_lhs (stmt);
6314 : 57396 : vectype_out = STMT_VINFO_VECTYPE (stmt_info);
6315 : 57396 : if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6316 : : {
6317 : 0 : if (dump_enabled_p ())
6318 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6319 : : "bit-precision shifts not supported.\n");
6320 : 0 : return false;
6321 : : }
6322 : :
6323 : 57396 : slp_tree slp_op0;
6324 : 57396 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6325 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6326 : : {
6327 : 0 : if (dump_enabled_p ())
6328 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6329 : : "use not simple.\n");
6330 : 0 : return false;
6331 : : }
6332 : : /* If op0 is an external or constant def, infer the vector type
6333 : : from the scalar type. */
6334 : 57396 : if (!vectype)
6335 : 16935 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
6336 : 57396 : if (vec_stmt)
6337 : 5771 : gcc_assert (vectype);
6338 : 57396 : if (!vectype)
6339 : : {
6340 : 0 : if (dump_enabled_p ())
6341 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6342 : : "no vectype for scalar type\n");
6343 : 0 : return false;
6344 : : }
6345 : :
6346 : 57396 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6347 : 57396 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6348 : 57396 : if (maybe_ne (nunits_out, nunits_in))
6349 : : return false;
6350 : :
6351 : 57396 : stmt_vec_info op1_def_stmt_info;
6352 : 57396 : slp_tree slp_op1;
6353 : 57396 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1, &op1, &slp_op1,
6354 : : &dt[1], &op1_vectype, &op1_def_stmt_info))
6355 : : {
6356 : 0 : if (dump_enabled_p ())
6357 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6358 : : "use not simple.\n");
6359 : 0 : return false;
6360 : : }
6361 : :
6362 : : /* Multiple types in SLP are handled by creating the appropriate number of
6363 : : vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6364 : : case of SLP. */
6365 : 57396 : if (slp_node)
6366 : : ncopies = 1;
6367 : : else
6368 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
6369 : :
6370 : 0 : gcc_assert (ncopies >= 1);
6371 : :
6372 : : /* Determine whether the shift amount is a vector, or scalar. If the
6373 : : shift/rotate amount is a vector, use the vector/vector shift optabs. */
6374 : :
6375 : 57396 : if ((dt[1] == vect_internal_def
6376 : 57396 : || dt[1] == vect_induction_def
6377 : 40917 : || dt[1] == vect_nested_cycle)
6378 : 16497 : && (!slp_node || SLP_TREE_LANES (slp_node) == 1))
6379 : : scalar_shift_arg = false;
6380 : 40947 : else if (dt[1] == vect_constant_def
6381 : : || dt[1] == vect_external_def
6382 : 40947 : || dt[1] == vect_internal_def)
6383 : : {
6384 : : /* In SLP, need to check whether the shift count is the same,
6385 : : in loops if it is a constant or invariant, it is always
6386 : : a scalar shift. */
6387 : 40939 : if (slp_node)
6388 : : {
6389 : 40939 : vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
6390 : 40939 : stmt_vec_info slpstmt_info;
6391 : :
6392 : 113048 : FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
6393 : 72109 : if (slpstmt_info)
6394 : : {
6395 : 72109 : gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
6396 : 144218 : if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
6397 : 72109 : scalar_shift_arg = false;
6398 : : }
6399 : :
6400 : : /* For internal SLP defs we have to make sure we see scalar stmts
6401 : : for all vector elements.
6402 : : ??? For different vectors we could resort to a different
6403 : : scalar shift operand but code-generation below simply always
6404 : : takes the first. */
6405 : 40939 : if (dt[1] == vect_internal_def
6406 : 40939 : && maybe_ne (nunits_out * SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node),
6407 : 40 : stmts.length ()))
6408 : : scalar_shift_arg = false;
6409 : : }
6410 : :
6411 : : /* If the shift amount is computed by a pattern stmt we cannot
6412 : : use the scalar amount directly thus give up and use a vector
6413 : : shift. */
6414 : 40939 : if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
6415 : : scalar_shift_arg = false;
6416 : : }
6417 : : else
6418 : : {
6419 : 8 : if (dump_enabled_p ())
6420 : 8 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6421 : : "operand mode requires invariant argument.\n");
6422 : 8 : return false;
6423 : : }
6424 : :
6425 : : /* Vector shifted by vector. */
6426 : 57416 : bool was_scalar_shift_arg = scalar_shift_arg;
6427 : 40939 : if (!scalar_shift_arg)
6428 : : {
6429 : 16477 : optab = optab_for_tree_code (code, vectype, optab_vector);
6430 : 16477 : if (dump_enabled_p ())
6431 : 1121 : dump_printf_loc (MSG_NOTE, vect_location,
6432 : : "vector/vector shift/rotate found.\n");
6433 : :
6434 : 16477 : if (!op1_vectype)
6435 : 7 : op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
6436 : : slp_op1);
6437 : 16477 : incompatible_op1_vectype_p
6438 : 32954 : = (op1_vectype == NULL_TREE
6439 : 16477 : || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
6440 : 16477 : TYPE_VECTOR_SUBPARTS (vectype))
6441 : 22814 : || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
6442 : 6332 : if (incompatible_op1_vectype_p
6443 : 10145 : && (!slp_node
6444 : 10145 : || SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
6445 : 1 : || slp_op1->refcnt != 1))
6446 : : {
6447 : 10144 : if (dump_enabled_p ())
6448 : 72 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6449 : : "unusable type for last operand in"
6450 : : " vector/vector shift/rotate.\n");
6451 : 10144 : return false;
6452 : : }
6453 : : }
6454 : : /* See if the machine has a vector shifted by scalar insn and if not
6455 : : then see if it has a vector shifted by vector insn. */
6456 : : else
6457 : : {
6458 : 40911 : optab = optab_for_tree_code (code, vectype, optab_scalar);
6459 : 40911 : if (optab
6460 : 40911 : && can_implement_p (optab, TYPE_MODE (vectype)))
6461 : : {
6462 : 40911 : if (dump_enabled_p ())
6463 : 4552 : dump_printf_loc (MSG_NOTE, vect_location,
6464 : : "vector/scalar shift/rotate found.\n");
6465 : : }
6466 : : else
6467 : : {
6468 : 0 : optab = optab_for_tree_code (code, vectype, optab_vector);
6469 : 0 : if (optab
6470 : 0 : && can_implement_p (optab, TYPE_MODE (vectype)))
6471 : : {
6472 : 0 : scalar_shift_arg = false;
6473 : :
6474 : 0 : if (dump_enabled_p ())
6475 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
6476 : : "vector/vector shift/rotate found.\n");
6477 : :
6478 : 0 : if (!op1_vectype)
6479 : 0 : op1_vectype = get_vectype_for_scalar_type (vinfo,
6480 : 0 : TREE_TYPE (op1),
6481 : : slp_op1);
6482 : :
6483 : : /* Unlike the other binary operators, shifts/rotates have
6484 : : the rhs being int, instead of the same type as the lhs,
6485 : : so make sure the scalar is the right type if we are
6486 : : dealing with vectors of long long/long/short/char. */
6487 : 0 : incompatible_op1_vectype_p
6488 : 0 : = (!op1_vectype
6489 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype),
6490 : 0 : TREE_TYPE (op1)));
6491 : 0 : if (incompatible_op1_vectype_p
6492 : 0 : && dt[1] == vect_internal_def)
6493 : : {
6494 : 0 : if (dump_enabled_p ())
6495 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6496 : : "unusable type for last operand in"
6497 : : " vector/vector shift/rotate.\n");
6498 : 0 : return false;
6499 : : }
6500 : : }
6501 : : }
6502 : : }
6503 : :
6504 : : /* Supportable by target? */
6505 : 47244 : if (!optab)
6506 : : {
6507 : 0 : if (dump_enabled_p ())
6508 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6509 : : "no optab.\n");
6510 : 0 : return false;
6511 : : }
6512 : 47244 : vec_mode = TYPE_MODE (vectype);
6513 : 47244 : icode = (int) optab_handler (optab, vec_mode);
6514 : 47244 : if (icode == CODE_FOR_nothing)
6515 : : {
6516 : 3810 : if (dump_enabled_p ())
6517 : 776 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6518 : : "op not supported by target.\n");
6519 : 3810 : return false;
6520 : : }
6521 : : /* vector lowering cannot optimize vector shifts using word arithmetic. */
6522 : 43434 : if (vect_emulated_vector_p (vectype))
6523 : : return false;
6524 : :
6525 : 43434 : if (!vec_stmt) /* transformation not required. */
6526 : : {
6527 : 37663 : if (slp_node
6528 : 37663 : && (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6529 : 37663 : || ((!scalar_shift_arg || dt[1] == vect_internal_def)
6530 : 1787 : && (!incompatible_op1_vectype_p
6531 : 1 : || dt[1] == vect_constant_def)
6532 : 1787 : && !vect_maybe_update_slp_op_vectype
6533 : 1787 : (slp_op1,
6534 : : incompatible_op1_vectype_p ? vectype : op1_vectype))))
6535 : : {
6536 : 0 : if (dump_enabled_p ())
6537 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6538 : : "incompatible vector types for invariants\n");
6539 : 0 : return false;
6540 : : }
6541 : : /* Now adjust the constant shift amount in place. */
6542 : 37663 : if (slp_node
6543 : 37663 : && incompatible_op1_vectype_p
6544 : 1 : && dt[1] == vect_constant_def)
6545 : : {
6546 : 4 : for (unsigned i = 0;
6547 : 5 : i < SLP_TREE_SCALAR_OPS (slp_op1).length (); ++i)
6548 : : {
6549 : 4 : SLP_TREE_SCALAR_OPS (slp_op1)[i]
6550 : 4 : = fold_convert (TREE_TYPE (vectype),
6551 : : SLP_TREE_SCALAR_OPS (slp_op1)[i]);
6552 : 4 : gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
6553 : : == INTEGER_CST));
6554 : : }
6555 : : }
6556 : 37663 : STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
6557 : 37663 : DUMP_VECT_SCOPE ("vectorizable_shift");
6558 : 39440 : vect_model_simple_cost (vinfo, ncopies, dt,
6559 : : scalar_shift_arg ? 1 : ndts, slp_node, cost_vec);
6560 : 37663 : return true;
6561 : : }
6562 : :
6563 : : /* Transform. */
6564 : :
6565 : 5771 : if (dump_enabled_p ())
6566 : 1833 : dump_printf_loc (MSG_NOTE, vect_location,
6567 : : "transform binary/unary operation.\n");
6568 : :
6569 : 5771 : if (incompatible_op1_vectype_p && !slp_node)
6570 : : {
6571 : 0 : gcc_assert (!scalar_shift_arg && was_scalar_shift_arg);
6572 : 0 : op1 = fold_convert (TREE_TYPE (vectype), op1);
6573 : 0 : if (dt[1] != vect_constant_def)
6574 : 0 : op1 = vect_init_vector (vinfo, stmt_info, op1,
6575 : 0 : TREE_TYPE (vectype), NULL);
6576 : : }
6577 : :
6578 : : /* Handle def. */
6579 : 5771 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
6580 : :
6581 : 5771 : if (scalar_shift_arg && dt[1] != vect_internal_def)
6582 : : {
6583 : : /* Vector shl and shr insn patterns can be defined with scalar
6584 : : operand 2 (shift operand). In this case, use constant or loop
6585 : : invariant op1 directly, without extending it to vector mode
6586 : : first. */
6587 : 5015 : optab_op2_mode = insn_data[icode].operand[2].mode;
6588 : 5015 : if (!VECTOR_MODE_P (optab_op2_mode))
6589 : : {
6590 : 5015 : if (dump_enabled_p ())
6591 : 1727 : dump_printf_loc (MSG_NOTE, vect_location,
6592 : : "operand 1 using scalar mode.\n");
6593 : 5015 : vec_oprnd1 = op1;
6594 : 5015 : vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : ncopies);
6595 : 5015 : vec_oprnds1.quick_push (vec_oprnd1);
6596 : : /* Store vec_oprnd1 for every vector stmt to be created.
6597 : : We check during the analysis that all the shift arguments
6598 : : are the same.
6599 : : TODO: Allow different constants for different vector
6600 : : stmts generated for an SLP instance. */
6601 : 5015 : for (k = 0;
6602 : 7081 : k < (slp_node ? slp_node->vec_stmts_size - 1 : ncopies - 1); k++)
6603 : 2066 : vec_oprnds1.quick_push (vec_oprnd1);
6604 : : }
6605 : : }
6606 : 756 : else if (!scalar_shift_arg && slp_node && incompatible_op1_vectype_p)
6607 : : {
6608 : 0 : if (was_scalar_shift_arg)
6609 : : {
6610 : : /* If the argument was the same in all lanes create the
6611 : : correctly typed vector shift amount directly. Note
6612 : : we made SLP scheduling think we use the original scalars,
6613 : : so place the compensation code next to the shift which
6614 : : is conservative. See PR119640 where it otherwise breaks. */
6615 : 0 : op1 = fold_convert (TREE_TYPE (vectype), op1);
6616 : 0 : op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
6617 : : gsi);
6618 : 0 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
6619 : : gsi);
6620 : 0 : vec_oprnds1.create (slp_node->vec_stmts_size);
6621 : 0 : for (k = 0; k < slp_node->vec_stmts_size; k++)
6622 : 0 : vec_oprnds1.quick_push (vec_oprnd1);
6623 : : }
6624 : 0 : else if (dt[1] == vect_constant_def)
6625 : : /* The constant shift amount has been adjusted in place. */
6626 : : ;
6627 : : else
6628 : 0 : gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
6629 : : }
6630 : :
6631 : : /* vec_oprnd1 is available if operand 1 should be of a scalar-type
6632 : : (a special case for certain kind of vector shifts); otherwise,
6633 : : operand 1 should be of a vector type (the usual case). */
6634 : 756 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
6635 : : op0, &vec_oprnds0,
6636 : 5771 : vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
6637 : :
6638 : : /* Arguments are ready. Create the new vector stmt. */
6639 : 14016 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6640 : : {
6641 : : /* For internal defs where we need to use a scalar shift arg
6642 : : extract the first lane. */
6643 : 8245 : if (scalar_shift_arg && dt[1] == vect_internal_def)
6644 : : {
6645 : 10 : vop1 = vec_oprnds1[0];
6646 : 10 : new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
6647 : 10 : gassign *new_stmt
6648 : 10 : = gimple_build_assign (new_temp,
6649 : 10 : build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
6650 : : vop1,
6651 : 10 : TYPE_SIZE (TREE_TYPE (new_temp)),
6652 : : bitsize_zero_node));
6653 : 10 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6654 : 10 : vop1 = new_temp;
6655 : 10 : }
6656 : : else
6657 : 8235 : vop1 = vec_oprnds1[i];
6658 : 8245 : gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
6659 : 8245 : new_temp = make_ssa_name (vec_dest, new_stmt);
6660 : 8245 : gimple_assign_set_lhs (new_stmt, new_temp);
6661 : 8245 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6662 : 8245 : if (slp_node)
6663 : 8245 : slp_node->push_vec_def (new_stmt);
6664 : : else
6665 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6666 : : }
6667 : :
6668 : 5771 : if (!slp_node)
6669 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
6670 : :
6671 : 5771 : vec_oprnds0.release ();
6672 : 5771 : vec_oprnds1.release ();
6673 : :
6674 : 5771 : return true;
6675 : : }
6676 : :
6677 : : /* Function vectorizable_operation.
6678 : :
6679 : : Check if STMT_INFO performs a binary, unary or ternary operation that can
6680 : : be vectorized.
6681 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
6682 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6683 : : Return true if STMT_INFO is vectorizable in this way. */
6684 : :
6685 : : static bool
6686 : 2317726 : vectorizable_operation (vec_info *vinfo,
6687 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6688 : : gimple **vec_stmt, slp_tree slp_node,
6689 : : stmt_vector_for_cost *cost_vec)
6690 : : {
6691 : 2317726 : tree vec_dest;
6692 : 2317726 : tree scalar_dest;
6693 : 2317726 : tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
6694 : 2317726 : tree vectype;
6695 : 2317726 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6696 : 2317726 : enum tree_code code, orig_code;
6697 : 2317726 : machine_mode vec_mode;
6698 : 2317726 : tree new_temp;
6699 : 2317726 : int op_type;
6700 : 2317726 : optab optab;
6701 : 2317726 : bool target_support_p;
6702 : 2317726 : enum vect_def_type dt[3]
6703 : : = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
6704 : 2317726 : int ndts = 3;
6705 : 2317726 : poly_uint64 nunits_in;
6706 : 2317726 : poly_uint64 nunits_out;
6707 : 2317726 : tree vectype_out;
6708 : 2317726 : int vec_num;
6709 : 2317726 : int i;
6710 : 2317726 : vec<tree> vec_oprnds0 = vNULL;
6711 : 2317726 : vec<tree> vec_oprnds1 = vNULL;
6712 : 2317726 : vec<tree> vec_oprnds2 = vNULL;
6713 : 2317726 : tree vop0, vop1, vop2;
6714 : 2317726 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6715 : :
6716 : 2317726 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6717 : : return false;
6718 : :
6719 : 2317726 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6720 : 158036 : && ! vec_stmt)
6721 : : return false;
6722 : :
6723 : : /* Is STMT a vectorizable binary/unary operation? */
6724 : 3891847 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6725 : 2081885 : if (!stmt)
6726 : : return false;
6727 : :
6728 : : /* Loads and stores are handled in vectorizable_{load,store}. */
6729 : 2081885 : if (STMT_VINFO_DATA_REF (stmt_info))
6730 : : return false;
6731 : :
6732 : 816863 : orig_code = code = gimple_assign_rhs_code (stmt);
6733 : :
6734 : : /* Shifts are handled in vectorizable_shift. */
6735 : 816863 : if (code == LSHIFT_EXPR
6736 : : || code == RSHIFT_EXPR
6737 : : || code == LROTATE_EXPR
6738 : 816863 : || code == RROTATE_EXPR)
6739 : : return false;
6740 : :
6741 : : /* Comparisons are handled in vectorizable_comparison. */
6742 : 789263 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6743 : : return false;
6744 : :
6745 : : /* Conditions are handled in vectorizable_condition. */
6746 : 627179 : if (code == COND_EXPR)
6747 : : return false;
6748 : :
6749 : : /* For pointer addition and subtraction, we should use the normal
6750 : : plus and minus for the vector operation. */
6751 : 609402 : if (code == POINTER_PLUS_EXPR)
6752 : : code = PLUS_EXPR;
6753 : 595237 : if (code == POINTER_DIFF_EXPR)
6754 : 1058 : code = MINUS_EXPR;
6755 : :
6756 : : /* Support only unary or binary operations. */
6757 : 609402 : op_type = TREE_CODE_LENGTH (code);
6758 : 609402 : if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
6759 : : {
6760 : 4 : if (dump_enabled_p ())
6761 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6762 : : "num. args = %d (not unary/binary/ternary op).\n",
6763 : : op_type);
6764 : 4 : return false;
6765 : : }
6766 : :
6767 : 609398 : scalar_dest = gimple_assign_lhs (stmt);
6768 : 609398 : vectype_out = SLP_TREE_VECTYPE (slp_node);
6769 : :
6770 : : /* Most operations cannot handle bit-precision types without extra
6771 : : truncations. */
6772 : 609398 : bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
6773 : 602688 : if (!mask_op_p
6774 : 602688 : && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
6775 : : /* Exception are bitwise binary operations. */
6776 : : && code != BIT_IOR_EXPR
6777 : 1354 : && code != BIT_XOR_EXPR
6778 : 1219 : && code != BIT_AND_EXPR)
6779 : : {
6780 : 978 : if (dump_enabled_p ())
6781 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6782 : : "bit-precision arithmetic not supported.\n");
6783 : 978 : return false;
6784 : : }
6785 : :
6786 : 608420 : slp_tree slp_op0;
6787 : 608420 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6788 : : 0, &op0, &slp_op0, &dt[0], &vectype))
6789 : : {
6790 : 0 : if (dump_enabled_p ())
6791 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6792 : : "use not simple.\n");
6793 : 0 : return false;
6794 : : }
6795 : 608420 : bool is_invariant = (dt[0] == vect_external_def
6796 : 608420 : || dt[0] == vect_constant_def);
6797 : : /* If op0 is an external or constant def, infer the vector type
6798 : : from the scalar type. */
6799 : 608420 : if (!vectype)
6800 : : {
6801 : : /* For boolean type we cannot determine vectype by
6802 : : invariant value (don't know whether it is a vector
6803 : : of booleans or vector of integers). We use output
6804 : : vectype because operations on boolean don't change
6805 : : type. */
6806 : 80393 : if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
6807 : : {
6808 : 1081 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
6809 : : {
6810 : 291 : if (dump_enabled_p ())
6811 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6812 : : "not supported operation on bool value.\n");
6813 : 291 : return false;
6814 : : }
6815 : 790 : vectype = vectype_out;
6816 : : }
6817 : : else
6818 : 79312 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
6819 : : slp_node);
6820 : : }
6821 : 608129 : if (vec_stmt)
6822 : 109141 : gcc_assert (vectype);
6823 : 608129 : if (!vectype)
6824 : : {
6825 : 1653 : if (dump_enabled_p ())
6826 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6827 : : "no vectype for scalar type %T\n",
6828 : 0 : TREE_TYPE (op0));
6829 : :
6830 : 1653 : return false;
6831 : : }
6832 : :
6833 : 606476 : nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6834 : 606476 : nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6835 : 606476 : if (maybe_ne (nunits_out, nunits_in)
6836 : 606476 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
6837 : 10106 : return false;
6838 : :
6839 : 596370 : tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
6840 : 596370 : slp_tree slp_op1 = NULL, slp_op2 = NULL;
6841 : 596370 : if (op_type == binary_op || op_type == ternary_op)
6842 : : {
6843 : 529645 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6844 : : 1, &op1, &slp_op1, &dt[1], &vectype2))
6845 : : {
6846 : 0 : if (dump_enabled_p ())
6847 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6848 : : "use not simple.\n");
6849 : 0 : return false;
6850 : : }
6851 : 529645 : is_invariant &= (dt[1] == vect_external_def
6852 : 529645 : || dt[1] == vect_constant_def);
6853 : 529645 : if (vectype2
6854 : 879831 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
6855 : 350186 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6856 : 350186 : TREE_TYPE (vectype2))))
6857 : 328 : return false;
6858 : : }
6859 : 596042 : if (op_type == ternary_op)
6860 : : {
6861 : 0 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6862 : : 2, &op2, &slp_op2, &dt[2], &vectype3))
6863 : : {
6864 : 0 : if (dump_enabled_p ())
6865 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6866 : : "use not simple.\n");
6867 : 0 : return false;
6868 : : }
6869 : 0 : is_invariant &= (dt[2] == vect_external_def
6870 : 0 : || dt[2] == vect_constant_def);
6871 : 0 : if (vectype3
6872 : 0 : && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
6873 : 0 : || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
6874 : 0 : TREE_TYPE (vectype3))))
6875 : 0 : return false;
6876 : : }
6877 : :
6878 : : /* Multiple types in SLP are handled by creating the appropriate number of
6879 : : vectorized stmts for each SLP node. */
6880 : 596042 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6881 : :
6882 : : /* Reject attempts to combine mask types with nonmask types, e.g. if
6883 : : we have an AND between a (nonmask) boolean loaded from memory and
6884 : : a (mask) boolean result of a comparison.
6885 : :
6886 : : TODO: We could easily fix these cases up using pattern statements. */
6887 : 596042 : if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
6888 : 942089 : || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
6889 : 1192072 : || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
6890 : : {
6891 : 12 : if (dump_enabled_p ())
6892 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6893 : : "mixed mask and nonmask vector types\n");
6894 : 12 : return false;
6895 : : }
6896 : :
6897 : : /* Supportable by target? */
6898 : :
6899 : 596030 : vec_mode = TYPE_MODE (vectype);
6900 : 596030 : optab = optab_for_tree_code (code, vectype, optab_default);
6901 : 596030 : if (!optab)
6902 : : {
6903 : 55887 : if (dump_enabled_p ())
6904 : 5555 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6905 : : "no optab.\n");
6906 : 55887 : return false;
6907 : : }
6908 : 540143 : target_support_p = can_implement_p (optab, vec_mode);
6909 : :
6910 : 540143 : bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
6911 : 540143 : if (!target_support_p || using_emulated_vectors_p)
6912 : : {
6913 : 36426 : if (dump_enabled_p ())
6914 : 1091 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6915 : : "op not supported by target.\n");
6916 : : /* When vec_mode is not a vector mode and we verified ops we
6917 : : do not have to lower like AND are natively supported let
6918 : : those through even when the mode isn't word_mode. For
6919 : : ops we have to lower the lowering code assumes we are
6920 : : dealing with word_mode. */
6921 : 72852 : if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
6922 : 36332 : || !GET_MODE_SIZE (vec_mode).is_constant ()
6923 : 36332 : || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
6924 : 26865 : || !target_support_p)
6925 : 77716 : && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
6926 : : /* Check only during analysis. */
6927 : 51216 : || (!vec_stmt && !vect_can_vectorize_without_simd_p (code)))
6928 : : {
6929 : 32195 : if (dump_enabled_p ())
6930 : 1065 : dump_printf (MSG_NOTE, "using word mode not possible.\n");
6931 : 32195 : return false;
6932 : : }
6933 : 4231 : if (dump_enabled_p ())
6934 : 26 : dump_printf_loc (MSG_NOTE, vect_location,
6935 : : "proceeding using word mode.\n");
6936 : : using_emulated_vectors_p = true;
6937 : : }
6938 : :
6939 : 507948 : int reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
6940 : 507948 : vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
6941 : 329878 : vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
6942 : 507948 : internal_fn cond_fn = get_conditional_internal_fn (code);
6943 : 507948 : internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
6944 : :
6945 : : /* If operating on inactive elements could generate spurious traps,
6946 : : we need to restrict the operation to active lanes. Note that this
6947 : : specifically doesn't apply to unhoisted invariants, since they
6948 : : operate on the same value for every lane.
6949 : :
6950 : : Similarly, if this operation is part of a reduction, a fully-masked
6951 : : loop should only change the active lanes of the reduction chain,
6952 : : keeping the inactive lanes as-is. */
6953 : 478430 : bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
6954 : 932381 : || reduc_idx >= 0);
6955 : :
6956 : 507948 : if (!vec_stmt) /* transformation not required. */
6957 : : {
6958 : 398807 : if (loop_vinfo
6959 : 228554 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
6960 : 34 : && mask_out_inactive)
6961 : : {
6962 : 13 : if (cond_len_fn != IFN_LAST
6963 : 13 : && direct_internal_fn_supported_p (cond_len_fn, vectype,
6964 : : OPTIMIZE_FOR_SPEED))
6965 : 0 : vect_record_loop_len (loop_vinfo, lens, vec_num, vectype,
6966 : : 1);
6967 : 13 : else if (cond_fn != IFN_LAST
6968 : 13 : && direct_internal_fn_supported_p (cond_fn, vectype,
6969 : : OPTIMIZE_FOR_SPEED))
6970 : 13 : vect_record_loop_mask (loop_vinfo, masks, vec_num,
6971 : : vectype, NULL);
6972 : : else
6973 : : {
6974 : 0 : if (dump_enabled_p ())
6975 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6976 : : "can't use a fully-masked loop because no"
6977 : : " conditional operation is available.\n");
6978 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
6979 : : }
6980 : : }
6981 : :
6982 : : /* Put types on constant and invariant SLP children. */
6983 : 398807 : if (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6984 : 398640 : || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
6985 : 797430 : || !vect_maybe_update_slp_op_vectype (slp_op2, vectype))
6986 : : {
6987 : 184 : if (dump_enabled_p ())
6988 : 4 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6989 : : "incompatible vector types for invariants\n");
6990 : 184 : return false;
6991 : : }
6992 : :
6993 : 398623 : STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
6994 : 398623 : DUMP_VECT_SCOPE ("vectorizable_operation");
6995 : 398623 : vect_model_simple_cost (vinfo, 1, dt, ndts, slp_node, cost_vec);
6996 : 398623 : if (using_emulated_vectors_p)
6997 : : {
6998 : : /* The above vect_model_simple_cost call handles constants
6999 : : in the prologue and (mis-)costs one of the stmts as
7000 : : vector stmt. See below for the actual lowering that will
7001 : : be applied. */
7002 : 4229 : unsigned n = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
7003 : 4229 : switch (code)
7004 : : {
7005 : 1780 : case PLUS_EXPR:
7006 : 1780 : n *= 5;
7007 : 1780 : break;
7008 : 1749 : case MINUS_EXPR:
7009 : 1749 : n *= 6;
7010 : 1749 : break;
7011 : 8 : case NEGATE_EXPR:
7012 : 8 : n *= 4;
7013 : 8 : break;
7014 : : default:
7015 : : /* Bit operations do not have extra cost and are accounted
7016 : : as vector stmt by vect_model_simple_cost. */
7017 : : n = 0;
7018 : : break;
7019 : : }
7020 : 3537 : if (n != 0)
7021 : : {
7022 : : /* We also need to materialize two large constants. */
7023 : 3537 : record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
7024 : : 0, vect_prologue);
7025 : 3537 : record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
7026 : : 0, vect_body);
7027 : : }
7028 : : }
7029 : 398623 : return true;
7030 : : }
7031 : :
7032 : : /* Transform. */
7033 : :
7034 : 109141 : if (dump_enabled_p ())
7035 : 15669 : dump_printf_loc (MSG_NOTE, vect_location,
7036 : : "transform binary/unary operation.\n");
7037 : :
7038 : 109141 : bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
7039 : 101324 : bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
7040 : :
7041 : : /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
7042 : : vectors with unsigned elements, but the result is signed. So, we
7043 : : need to compute the MINUS_EXPR into vectype temporary and
7044 : : VIEW_CONVERT_EXPR it into the final vectype_out result. */
7045 : 109141 : tree vec_cvt_dest = NULL_TREE;
7046 : 109141 : if (orig_code == POINTER_DIFF_EXPR)
7047 : : {
7048 : 128 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
7049 : 128 : vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
7050 : : }
7051 : : /* Handle def. */
7052 : : else
7053 : 109013 : vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
7054 : :
7055 : 109141 : vect_get_vec_defs (vinfo, stmt_info, slp_node, 1,
7056 : : op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
7057 : : /* Arguments are ready. Create the new vector stmt. */
7058 : 238304 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
7059 : : {
7060 : 129163 : gimple *new_stmt = NULL;
7061 : 258326 : vop1 = ((op_type == binary_op || op_type == ternary_op)
7062 : 129163 : ? vec_oprnds1[i] : NULL_TREE);
7063 : 129163 : vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
7064 : 129163 : if (using_emulated_vectors_p)
7065 : : {
7066 : : /* Lower the operation. This follows vector lowering. */
7067 : 2 : tree word_type = build_nonstandard_integer_type
7068 : 2 : (GET_MODE_BITSIZE (vec_mode).to_constant (), 1);
7069 : 2 : tree wvop0 = make_ssa_name (word_type);
7070 : 2 : new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
7071 : : build1 (VIEW_CONVERT_EXPR,
7072 : : word_type, vop0));
7073 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7074 : 2 : tree wvop1 = NULL_TREE;
7075 : 2 : if (vop1)
7076 : : {
7077 : 2 : wvop1 = make_ssa_name (word_type);
7078 : 2 : new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
7079 : : build1 (VIEW_CONVERT_EXPR,
7080 : : word_type, vop1));
7081 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7082 : : }
7083 : :
7084 : 2 : tree result_low;
7085 : 2 : if (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
7086 : : {
7087 : 1 : unsigned int width = vector_element_bits (vectype);
7088 : 1 : tree inner_type = TREE_TYPE (vectype);
7089 : 1 : HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
7090 : 1 : tree low_bits
7091 : 1 : = build_replicated_int_cst (word_type, width, max >> 1);
7092 : 1 : tree high_bits
7093 : 2 : = build_replicated_int_cst (word_type,
7094 : 1 : width, max & ~(max >> 1));
7095 : 1 : tree signs;
7096 : 1 : if (code == PLUS_EXPR || code == MINUS_EXPR)
7097 : : {
7098 : 1 : signs = make_ssa_name (word_type);
7099 : 1 : new_stmt = gimple_build_assign (signs,
7100 : : BIT_XOR_EXPR, wvop0, wvop1);
7101 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7102 : 1 : tree b_low = make_ssa_name (word_type);
7103 : 1 : new_stmt = gimple_build_assign (b_low, BIT_AND_EXPR,
7104 : : wvop1, low_bits);
7105 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7106 : 1 : tree a_low = make_ssa_name (word_type);
7107 : 1 : if (code == PLUS_EXPR)
7108 : 1 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
7109 : : wvop0, low_bits);
7110 : : else
7111 : 0 : new_stmt = gimple_build_assign (a_low, BIT_IOR_EXPR,
7112 : : wvop0, high_bits);
7113 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7114 : 1 : if (code == MINUS_EXPR)
7115 : : {
7116 : 0 : new_stmt = gimple_build_assign (NULL_TREE,
7117 : : BIT_NOT_EXPR, signs);
7118 : 0 : signs = make_ssa_name (word_type);
7119 : 0 : gimple_assign_set_lhs (new_stmt, signs);
7120 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
7121 : : new_stmt, gsi);
7122 : : }
7123 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
7124 : : signs, high_bits);
7125 : 1 : signs = make_ssa_name (word_type);
7126 : 1 : gimple_assign_set_lhs (new_stmt, signs);
7127 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7128 : 1 : result_low = make_ssa_name (word_type);
7129 : 1 : new_stmt = gimple_build_assign (result_low, code,
7130 : : a_low, b_low);
7131 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7132 : : }
7133 : : else /* if (code == NEGATE_EXPR) */
7134 : : {
7135 : 0 : tree a_low = make_ssa_name (word_type);
7136 : 0 : new_stmt = gimple_build_assign (a_low, BIT_AND_EXPR,
7137 : : wvop0, low_bits);
7138 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7139 : 0 : signs = make_ssa_name (word_type);
7140 : 0 : new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
7141 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7142 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR,
7143 : : signs, high_bits);
7144 : 0 : signs = make_ssa_name (word_type);
7145 : 0 : gimple_assign_set_lhs (new_stmt, signs);
7146 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7147 : 0 : result_low = make_ssa_name (word_type);
7148 : 0 : new_stmt = gimple_build_assign (result_low,
7149 : : MINUS_EXPR, high_bits, a_low);
7150 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7151 : : }
7152 : 1 : new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR,
7153 : : result_low, signs);
7154 : 1 : result_low = make_ssa_name (word_type);
7155 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
7156 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7157 : : }
7158 : : else
7159 : : {
7160 : 1 : new_stmt = gimple_build_assign (NULL_TREE, code, wvop0, wvop1);
7161 : 1 : result_low = make_ssa_name (word_type);
7162 : 1 : gimple_assign_set_lhs (new_stmt, result_low);
7163 : 1 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7164 : :
7165 : : }
7166 : 2 : new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
7167 : : build1 (VIEW_CONVERT_EXPR,
7168 : : vectype, result_low));
7169 : 2 : new_temp = make_ssa_name (vectype);
7170 : 2 : gimple_assign_set_lhs (new_stmt, new_temp);
7171 : 2 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7172 : : }
7173 : 129161 : else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
7174 : : {
7175 : 16 : tree mask;
7176 : 16 : if (masked_loop_p)
7177 : 16 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7178 : : vec_num, vectype, i);
7179 : : else
7180 : : /* Dummy mask. */
7181 : 0 : mask = build_minus_one_cst (truth_type_for (vectype));
7182 : 16 : auto_vec<tree> vops (6);
7183 : 16 : vops.quick_push (mask);
7184 : 16 : vops.quick_push (vop0);
7185 : 16 : if (vop1)
7186 : 16 : vops.quick_push (vop1);
7187 : 16 : if (vop2)
7188 : 0 : vops.quick_push (vop2);
7189 : 16 : if (reduc_idx >= 0)
7190 : : {
7191 : : /* Perform the operation on active elements only and take
7192 : : inactive elements from the reduction chain input. */
7193 : 8 : gcc_assert (!vop2);
7194 : 8 : vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
7195 : : }
7196 : : else
7197 : : {
7198 : 8 : auto else_value = targetm.preferred_else_value
7199 : 8 : (cond_fn, vectype, vops.length () - 1, &vops[1]);
7200 : 8 : vops.quick_push (else_value);
7201 : : }
7202 : 16 : if (len_loop_p)
7203 : : {
7204 : 0 : tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
7205 : 0 : vec_num, vectype, i, 1);
7206 : 0 : signed char biasval
7207 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
7208 : 0 : tree bias = build_int_cst (intQI_type_node, biasval);
7209 : 0 : vops.quick_push (len);
7210 : 0 : vops.quick_push (bias);
7211 : : }
7212 : 16 : gcall *call
7213 : 16 : = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
7214 : : : cond_len_fn,
7215 : : vops);
7216 : 16 : new_temp = make_ssa_name (vec_dest, call);
7217 : 16 : gimple_call_set_lhs (call, new_temp);
7218 : 16 : gimple_call_set_nothrow (call, true);
7219 : 16 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
7220 : 16 : new_stmt = call;
7221 : 16 : }
7222 : : else
7223 : : {
7224 : 129145 : tree mask = NULL_TREE;
7225 : : /* When combining two masks check if either of them is elsewhere
7226 : : combined with a loop mask, if that's the case we can mark that the
7227 : : new combined mask doesn't need to be combined with a loop mask. */
7228 : 129145 : if (masked_loop_p
7229 : 129145 : && code == BIT_AND_EXPR
7230 : 129145 : && VECTOR_BOOLEAN_TYPE_P (vectype))
7231 : : {
7232 : 0 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op0, 1 }))
7233 : : {
7234 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7235 : : vec_num, vectype, i);
7236 : :
7237 : 0 : vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7238 : : vop0, gsi);
7239 : : }
7240 : :
7241 : 0 : if (loop_vinfo->scalar_cond_masked_set.contains ({ op1, 1 }))
7242 : : {
7243 : 0 : mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7244 : : vec_num, vectype, i);
7245 : :
7246 : 0 : vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7247 : : vop1, gsi);
7248 : : }
7249 : : }
7250 : :
7251 : 129145 : new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
7252 : 129145 : new_temp = make_ssa_name (vec_dest, new_stmt);
7253 : 129145 : gimple_assign_set_lhs (new_stmt, new_temp);
7254 : 129145 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7255 : 129145 : if (using_emulated_vectors_p)
7256 : : suppress_warning (new_stmt, OPT_Wvector_operation_performance);
7257 : :
7258 : : /* Enter the combined value into the vector cond hash so we don't
7259 : : AND it with a loop mask again. */
7260 : 129145 : if (mask)
7261 : 0 : loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
7262 : : }
7263 : :
7264 : 129163 : if (vec_cvt_dest)
7265 : : {
7266 : 143 : new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
7267 : 143 : new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
7268 : : new_temp);
7269 : 143 : new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
7270 : 143 : gimple_assign_set_lhs (new_stmt, new_temp);
7271 : 143 : vect_finish_stmt_generation (vinfo, stmt_info,
7272 : : new_stmt, gsi);
7273 : : }
7274 : :
7275 : 129163 : slp_node->push_vec_def (new_stmt);
7276 : : }
7277 : :
7278 : 109141 : vec_oprnds0.release ();
7279 : 109141 : vec_oprnds1.release ();
7280 : 109141 : vec_oprnds2.release ();
7281 : :
7282 : 109141 : return true;
7283 : : }
7284 : :
7285 : : /* A helper function to ensure data reference DR_INFO's base alignment. */
7286 : :
7287 : : static void
7288 : 1898123 : ensure_base_align (dr_vec_info *dr_info)
7289 : : {
7290 : : /* Alignment is only analyzed for the first element of a DR group,
7291 : : use that to look at base alignment we need to enforce. */
7292 : 1898123 : if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
7293 : 1493314 : dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
7294 : :
7295 : 1898123 : gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
7296 : :
7297 : 1898123 : if (dr_info->base_misaligned)
7298 : : {
7299 : 168810 : tree base_decl = dr_info->base_decl;
7300 : :
7301 : : // We should only be able to increase the alignment of a base object if
7302 : : // we know what its new alignment should be at compile time.
7303 : 168810 : unsigned HOST_WIDE_INT align_base_to =
7304 : 168810 : DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
7305 : :
7306 : 168810 : if (decl_in_symtab_p (base_decl))
7307 : 5229 : symtab_node::get (base_decl)->increase_alignment (align_base_to);
7308 : 163581 : else if (DECL_ALIGN (base_decl) < align_base_to)
7309 : : {
7310 : 132228 : SET_DECL_ALIGN (base_decl, align_base_to);
7311 : 132228 : DECL_USER_ALIGN (base_decl) = 1;
7312 : : }
7313 : 168810 : dr_info->base_misaligned = false;
7314 : : }
7315 : 1898123 : }
7316 : :
7317 : :
7318 : : /* Function get_group_alias_ptr_type.
7319 : :
7320 : : Return the alias type for the group starting at FIRST_STMT_INFO. */
7321 : :
7322 : : static tree
7323 : 1641333 : get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
7324 : : {
7325 : 1641333 : struct data_reference *first_dr, *next_dr;
7326 : :
7327 : 1641333 : first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
7328 : 1641333 : stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
7329 : 3960493 : while (next_stmt_info)
7330 : : {
7331 : 2477614 : next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
7332 : 4955228 : if (get_alias_set (DR_REF (first_dr))
7333 : 2477614 : != get_alias_set (DR_REF (next_dr)))
7334 : : {
7335 : 158454 : if (dump_enabled_p ())
7336 : 37 : dump_printf_loc (MSG_NOTE, vect_location,
7337 : : "conflicting alias set types.\n");
7338 : 158454 : return ptr_type_node;
7339 : : }
7340 : 2319160 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7341 : : }
7342 : 1482879 : return reference_alias_ptr_type (DR_REF (first_dr));
7343 : : }
7344 : :
7345 : :
7346 : : /* Function scan_operand_equal_p.
7347 : :
7348 : : Helper function for check_scan_store. Compare two references
7349 : : with .GOMP_SIMD_LANE bases. */
7350 : :
7351 : : static bool
7352 : 1284 : scan_operand_equal_p (tree ref1, tree ref2)
7353 : : {
7354 : 1284 : tree ref[2] = { ref1, ref2 };
7355 : 1284 : poly_int64 bitsize[2], bitpos[2];
7356 : : tree offset[2], base[2];
7357 : 3852 : for (int i = 0; i < 2; ++i)
7358 : : {
7359 : 2568 : machine_mode mode;
7360 : 2568 : int unsignedp, reversep, volatilep = 0;
7361 : 2568 : base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
7362 : : &offset[i], &mode, &unsignedp,
7363 : : &reversep, &volatilep);
7364 : 2568 : if (reversep || volatilep || maybe_ne (bitpos[i], 0))
7365 : 0 : return false;
7366 : 2568 : if (TREE_CODE (base[i]) == MEM_REF
7367 : 42 : && offset[i] == NULL_TREE
7368 : 2610 : && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
7369 : : {
7370 : 42 : gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
7371 : 42 : if (is_gimple_assign (def_stmt)
7372 : 42 : && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
7373 : 42 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
7374 : 84 : && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
7375 : : {
7376 : 42 : if (maybe_ne (mem_ref_offset (base[i]), 0))
7377 : : return false;
7378 : 42 : base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
7379 : 42 : offset[i] = gimple_assign_rhs2 (def_stmt);
7380 : : }
7381 : : }
7382 : : }
7383 : :
7384 : 1284 : if (!operand_equal_p (base[0], base[1], 0))
7385 : : return false;
7386 : 934 : if (maybe_ne (bitsize[0], bitsize[1]))
7387 : : return false;
7388 : 934 : if (offset[0] != offset[1])
7389 : : {
7390 : 916 : if (!offset[0] || !offset[1])
7391 : : return false;
7392 : 916 : if (!operand_equal_p (offset[0], offset[1], 0))
7393 : : {
7394 : : tree step[2];
7395 : 0 : for (int i = 0; i < 2; ++i)
7396 : : {
7397 : 0 : step[i] = integer_one_node;
7398 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7399 : : {
7400 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7401 : 0 : if (is_gimple_assign (def_stmt)
7402 : 0 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
7403 : 0 : && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
7404 : : == INTEGER_CST))
7405 : : {
7406 : 0 : step[i] = gimple_assign_rhs2 (def_stmt);
7407 : 0 : offset[i] = gimple_assign_rhs1 (def_stmt);
7408 : : }
7409 : : }
7410 : 0 : else if (TREE_CODE (offset[i]) == MULT_EXPR)
7411 : : {
7412 : 0 : step[i] = TREE_OPERAND (offset[i], 1);
7413 : 0 : offset[i] = TREE_OPERAND (offset[i], 0);
7414 : : }
7415 : 0 : tree rhs1 = NULL_TREE;
7416 : 0 : if (TREE_CODE (offset[i]) == SSA_NAME)
7417 : : {
7418 : 0 : gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7419 : 0 : if (gimple_assign_cast_p (def_stmt))
7420 : 0 : rhs1 = gimple_assign_rhs1 (def_stmt);
7421 : : }
7422 : 0 : else if (CONVERT_EXPR_P (offset[i]))
7423 : 0 : rhs1 = TREE_OPERAND (offset[i], 0);
7424 : 0 : if (rhs1
7425 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
7426 : 0 : && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
7427 : 0 : && (TYPE_PRECISION (TREE_TYPE (offset[i]))
7428 : 0 : >= TYPE_PRECISION (TREE_TYPE (rhs1))))
7429 : 0 : offset[i] = rhs1;
7430 : : }
7431 : 0 : if (!operand_equal_p (offset[0], offset[1], 0)
7432 : 0 : || !operand_equal_p (step[0], step[1], 0))
7433 : 0 : return false;
7434 : : }
7435 : : }
7436 : : return true;
7437 : : }
7438 : :
7439 : :
7440 : : enum scan_store_kind {
7441 : : /* Normal permutation. */
7442 : : scan_store_kind_perm,
7443 : :
7444 : : /* Whole vector left shift permutation with zero init. */
7445 : : scan_store_kind_lshift_zero,
7446 : :
7447 : : /* Whole vector left shift permutation and VEC_COND_EXPR. */
7448 : : scan_store_kind_lshift_cond
7449 : : };
7450 : :
7451 : : /* Function check_scan_store.
7452 : :
7453 : : Verify if we can perform the needed permutations or whole vector shifts.
7454 : : Return -1 on failure, otherwise exact log2 of vectype's nunits.
7455 : : USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
7456 : : to do at each step. */
7457 : :
7458 : : static int
7459 : 1024 : scan_store_can_perm_p (tree vectype, tree init,
7460 : : vec<enum scan_store_kind> *use_whole_vector = NULL)
7461 : : {
7462 : 1024 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7463 : 1024 : unsigned HOST_WIDE_INT nunits;
7464 : 1024 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7465 : : return -1;
7466 : 1024 : int units_log2 = exact_log2 (nunits);
7467 : 1024 : if (units_log2 <= 0)
7468 : : return -1;
7469 : :
7470 : : int i;
7471 : : enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
7472 : 4784 : for (i = 0; i <= units_log2; ++i)
7473 : : {
7474 : 3760 : unsigned HOST_WIDE_INT j, k;
7475 : 3760 : enum scan_store_kind kind = scan_store_kind_perm;
7476 : 3760 : vec_perm_builder sel (nunits, nunits, 1);
7477 : 3760 : sel.quick_grow (nunits);
7478 : 3760 : if (i == units_log2)
7479 : : {
7480 : 9728 : for (j = 0; j < nunits; ++j)
7481 : 8704 : sel[j] = nunits - 1;
7482 : : }
7483 : : else
7484 : : {
7485 : 10416 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7486 : 7680 : sel[j] = j;
7487 : 26416 : for (k = 0; j < nunits; ++j, ++k)
7488 : 23680 : sel[j] = nunits + k;
7489 : : }
7490 : 6496 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7491 : 3760 : if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
7492 : : {
7493 : 0 : if (i == units_log2)
7494 : : return -1;
7495 : :
7496 : 0 : if (whole_vector_shift_kind == scan_store_kind_perm)
7497 : : {
7498 : 0 : if (!can_implement_p (vec_shl_optab, vec_mode))
7499 : : return -1;
7500 : 0 : whole_vector_shift_kind = scan_store_kind_lshift_zero;
7501 : : /* Whole vector shifts shift in zeros, so if init is all zero
7502 : : constant, there is no need to do anything further. */
7503 : 0 : if ((TREE_CODE (init) != INTEGER_CST
7504 : 0 : && TREE_CODE (init) != REAL_CST)
7505 : 0 : || !initializer_zerop (init))
7506 : : {
7507 : 0 : tree masktype = truth_type_for (vectype);
7508 : 0 : if (!expand_vec_cond_expr_p (vectype, masktype))
7509 : : return -1;
7510 : : whole_vector_shift_kind = scan_store_kind_lshift_cond;
7511 : : }
7512 : : }
7513 : 0 : kind = whole_vector_shift_kind;
7514 : : }
7515 : 3760 : if (use_whole_vector)
7516 : : {
7517 : 1880 : if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
7518 : 0 : use_whole_vector->safe_grow_cleared (i, true);
7519 : 5640 : if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
7520 : 0 : use_whole_vector->safe_push (kind);
7521 : : }
7522 : 3760 : }
7523 : :
7524 : : return units_log2;
7525 : : }
7526 : :
7527 : :
7528 : : /* Function check_scan_store.
7529 : :
7530 : : Check magic stores for #pragma omp scan {in,ex}clusive reductions. */
7531 : :
7532 : : static bool
7533 : 1076 : check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
7534 : : enum vect_def_type rhs_dt, slp_tree slp_node, tree mask,
7535 : : vect_memory_access_type memory_access_type)
7536 : : {
7537 : 1076 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7538 : 1076 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7539 : 1076 : tree ref_type;
7540 : :
7541 : 1076 : gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
7542 : 1076 : if ((slp_node && SLP_TREE_LANES (slp_node) > 1)
7543 : 1076 : || mask
7544 : 1076 : || memory_access_type != VMAT_CONTIGUOUS
7545 : 1076 : || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
7546 : 1076 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
7547 : 1076 : || loop_vinfo == NULL
7548 : 1076 : || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7549 : 1076 : || STMT_VINFO_GROUPED_ACCESS (stmt_info)
7550 : 1076 : || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
7551 : 1076 : || !integer_zerop (DR_INIT (dr_info->dr))
7552 : 1076 : || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
7553 : 2152 : || !alias_sets_conflict_p (get_alias_set (vectype),
7554 : 1076 : get_alias_set (TREE_TYPE (ref_type))))
7555 : : {
7556 : 0 : if (dump_enabled_p ())
7557 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7558 : : "unsupported OpenMP scan store.\n");
7559 : 0 : return false;
7560 : : }
7561 : :
7562 : : /* We need to pattern match code built by OpenMP lowering and simplified
7563 : : by following optimizations into something we can handle.
7564 : : #pragma omp simd reduction(inscan,+:r)
7565 : : for (...)
7566 : : {
7567 : : r += something ();
7568 : : #pragma omp scan inclusive (r)
7569 : : use (r);
7570 : : }
7571 : : shall have body with:
7572 : : // Initialization for input phase, store the reduction initializer:
7573 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7574 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7575 : : D.2042[_21] = 0;
7576 : : // Actual input phase:
7577 : : ...
7578 : : r.0_5 = D.2042[_20];
7579 : : _6 = _4 + r.0_5;
7580 : : D.2042[_20] = _6;
7581 : : // Initialization for scan phase:
7582 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
7583 : : _26 = D.2043[_25];
7584 : : _27 = D.2042[_25];
7585 : : _28 = _26 + _27;
7586 : : D.2043[_25] = _28;
7587 : : D.2042[_25] = _28;
7588 : : // Actual scan phase:
7589 : : ...
7590 : : r.1_8 = D.2042[_20];
7591 : : ...
7592 : : The "omp simd array" variable D.2042 holds the privatized copy used
7593 : : inside of the loop and D.2043 is another one that holds copies of
7594 : : the current original list item. The separate GOMP_SIMD_LANE ifn
7595 : : kinds are there in order to allow optimizing the initializer store
7596 : : and combiner sequence, e.g. if it is originally some C++ish user
7597 : : defined reduction, but allow the vectorizer to pattern recognize it
7598 : : and turn into the appropriate vectorized scan.
7599 : :
7600 : : For exclusive scan, this is slightly different:
7601 : : #pragma omp simd reduction(inscan,+:r)
7602 : : for (...)
7603 : : {
7604 : : use (r);
7605 : : #pragma omp scan exclusive (r)
7606 : : r += something ();
7607 : : }
7608 : : shall have body with:
7609 : : // Initialization for input phase, store the reduction initializer:
7610 : : _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7611 : : _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7612 : : D.2042[_21] = 0;
7613 : : // Actual input phase:
7614 : : ...
7615 : : r.0_5 = D.2042[_20];
7616 : : _6 = _4 + r.0_5;
7617 : : D.2042[_20] = _6;
7618 : : // Initialization for scan phase:
7619 : : _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
7620 : : _26 = D.2043[_25];
7621 : : D.2044[_25] = _26;
7622 : : _27 = D.2042[_25];
7623 : : _28 = _26 + _27;
7624 : : D.2043[_25] = _28;
7625 : : // Actual scan phase:
7626 : : ...
7627 : : r.1_8 = D.2044[_20];
7628 : : ... */
7629 : :
7630 : 1076 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
7631 : : {
7632 : : /* Match the D.2042[_21] = 0; store above. Just require that
7633 : : it is a constant or external definition store. */
7634 : 564 : if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
7635 : : {
7636 : 0 : fail_init:
7637 : 0 : if (dump_enabled_p ())
7638 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7639 : : "unsupported OpenMP scan initializer store.\n");
7640 : 0 : return false;
7641 : : }
7642 : :
7643 : 564 : if (! loop_vinfo->scan_map)
7644 : 322 : loop_vinfo->scan_map = new hash_map<tree, tree>;
7645 : 564 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7646 : 564 : tree &cached = loop_vinfo->scan_map->get_or_insert (var);
7647 : 564 : if (cached)
7648 : 0 : goto fail_init;
7649 : 564 : cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
7650 : :
7651 : : /* These stores can be vectorized normally. */
7652 : 564 : return true;
7653 : : }
7654 : :
7655 : 512 : if (rhs_dt != vect_internal_def)
7656 : : {
7657 : 0 : fail:
7658 : 0 : if (dump_enabled_p ())
7659 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7660 : : "unsupported OpenMP scan combiner pattern.\n");
7661 : 0 : return false;
7662 : : }
7663 : :
7664 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7665 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7666 : 512 : if (TREE_CODE (rhs) != SSA_NAME)
7667 : 0 : goto fail;
7668 : :
7669 : 512 : gimple *other_store_stmt = NULL;
7670 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7671 : 512 : bool inscan_var_store
7672 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7673 : :
7674 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7675 : : {
7676 : 252 : if (!inscan_var_store)
7677 : : {
7678 : 126 : use_operand_p use_p;
7679 : 126 : imm_use_iterator iter;
7680 : 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7681 : : {
7682 : 252 : gimple *use_stmt = USE_STMT (use_p);
7683 : 252 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7684 : 126 : continue;
7685 : 126 : if (gimple_bb (use_stmt) != gimple_bb (stmt)
7686 : 126 : || !is_gimple_assign (use_stmt)
7687 : 126 : || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
7688 : 126 : || other_store_stmt
7689 : 252 : || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
7690 : 0 : goto fail;
7691 : 126 : other_store_stmt = use_stmt;
7692 : : }
7693 : 126 : if (other_store_stmt == NULL)
7694 : 0 : goto fail;
7695 : 126 : rhs = gimple_assign_lhs (other_store_stmt);
7696 : 126 : if (!single_imm_use (rhs, &use_p, &other_store_stmt))
7697 : 0 : goto fail;
7698 : : }
7699 : : }
7700 : 260 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
7701 : : {
7702 : 260 : use_operand_p use_p;
7703 : 260 : imm_use_iterator iter;
7704 : 780 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7705 : : {
7706 : 520 : gimple *use_stmt = USE_STMT (use_p);
7707 : 520 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7708 : 260 : continue;
7709 : 260 : if (other_store_stmt)
7710 : 0 : goto fail;
7711 : 260 : other_store_stmt = use_stmt;
7712 : : }
7713 : : }
7714 : : else
7715 : 0 : goto fail;
7716 : :
7717 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7718 : 512 : if (gimple_bb (def_stmt) != gimple_bb (stmt)
7719 : 512 : || !is_gimple_assign (def_stmt)
7720 : 1024 : || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
7721 : 0 : goto fail;
7722 : :
7723 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7724 : : /* For pointer addition, we should use the normal plus for the vector
7725 : : operation. */
7726 : 512 : switch (code)
7727 : : {
7728 : 0 : case POINTER_PLUS_EXPR:
7729 : 0 : code = PLUS_EXPR;
7730 : 0 : break;
7731 : 0 : case MULT_HIGHPART_EXPR:
7732 : 0 : goto fail;
7733 : : default:
7734 : : break;
7735 : : }
7736 : 512 : if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
7737 : 0 : goto fail;
7738 : :
7739 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7740 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7741 : 512 : if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
7742 : 0 : goto fail;
7743 : :
7744 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7745 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7746 : 512 : if (gimple_bb (load1_stmt) != gimple_bb (stmt)
7747 : 512 : || !gimple_assign_load_p (load1_stmt)
7748 : 512 : || gimple_bb (load2_stmt) != gimple_bb (stmt)
7749 : 1024 : || !gimple_assign_load_p (load2_stmt))
7750 : 0 : goto fail;
7751 : :
7752 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7753 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7754 : 512 : if (load1_stmt_info == NULL
7755 : 512 : || load2_stmt_info == NULL
7756 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
7757 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
7758 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
7759 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7760 : 0 : goto fail;
7761 : :
7762 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
7763 : : {
7764 : 126 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7765 : 126 : if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
7766 : 126 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
7767 : 0 : goto fail;
7768 : 126 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7769 : 126 : tree lrhs;
7770 : 126 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7771 : : lrhs = rhs1;
7772 : : else
7773 : 16 : lrhs = rhs2;
7774 : 126 : use_operand_p use_p;
7775 : 126 : imm_use_iterator iter;
7776 : 378 : FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
7777 : : {
7778 : 252 : gimple *use_stmt = USE_STMT (use_p);
7779 : 252 : if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
7780 : 126 : continue;
7781 : 126 : if (other_store_stmt)
7782 : 0 : goto fail;
7783 : 126 : other_store_stmt = use_stmt;
7784 : : }
7785 : : }
7786 : :
7787 : 512 : if (other_store_stmt == NULL)
7788 : 0 : goto fail;
7789 : 512 : if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
7790 : 512 : || !gimple_store_p (other_store_stmt))
7791 : 0 : goto fail;
7792 : :
7793 : 512 : stmt_vec_info other_store_stmt_info
7794 : 512 : = loop_vinfo->lookup_stmt (other_store_stmt);
7795 : 512 : if (other_store_stmt_info == NULL
7796 : 512 : || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
7797 : 512 : != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
7798 : 0 : goto fail;
7799 : :
7800 : 512 : gimple *stmt1 = stmt;
7801 : 512 : gimple *stmt2 = other_store_stmt;
7802 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7803 : : std::swap (stmt1, stmt2);
7804 : 512 : if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
7805 : : gimple_assign_rhs1 (load2_stmt)))
7806 : : {
7807 : 162 : std::swap (rhs1, rhs2);
7808 : 162 : std::swap (load1_stmt, load2_stmt);
7809 : 162 : std::swap (load1_stmt_info, load2_stmt_info);
7810 : : }
7811 : 512 : if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
7812 : : gimple_assign_rhs1 (load1_stmt)))
7813 : 0 : goto fail;
7814 : :
7815 : 512 : tree var3 = NULL_TREE;
7816 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
7817 : 512 : && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
7818 : : gimple_assign_rhs1 (load2_stmt)))
7819 : 0 : goto fail;
7820 : 512 : else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7821 : : {
7822 : 252 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7823 : 252 : if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
7824 : 252 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
7825 : 0 : goto fail;
7826 : 252 : var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7827 : 252 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
7828 : 252 : || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
7829 : 504 : || lookup_attribute ("omp simd inscan exclusive",
7830 : 252 : DECL_ATTRIBUTES (var3)))
7831 : 0 : goto fail;
7832 : : }
7833 : :
7834 : 512 : dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
7835 : 512 : if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
7836 : 512 : || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
7837 : 0 : goto fail;
7838 : :
7839 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7840 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
7841 : 512 : if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
7842 : 512 : || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
7843 : 1024 : || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7844 : 512 : == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
7845 : 0 : goto fail;
7846 : :
7847 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7848 : 256 : std::swap (var1, var2);
7849 : :
7850 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7851 : : {
7852 : 252 : if (!lookup_attribute ("omp simd inscan exclusive",
7853 : 252 : DECL_ATTRIBUTES (var1)))
7854 : 0 : goto fail;
7855 : 252 : var1 = var3;
7856 : : }
7857 : :
7858 : 512 : if (loop_vinfo->scan_map == NULL)
7859 : 0 : goto fail;
7860 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7861 : 512 : if (init == NULL)
7862 : 0 : goto fail;
7863 : :
7864 : : /* The IL is as expected, now check if we can actually vectorize it.
7865 : : Inclusive scan:
7866 : : _26 = D.2043[_25];
7867 : : _27 = D.2042[_25];
7868 : : _28 = _26 + _27;
7869 : : D.2043[_25] = _28;
7870 : : D.2042[_25] = _28;
7871 : : should be vectorized as (where _40 is the vectorized rhs
7872 : : from the D.2042[_21] = 0; store):
7873 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7874 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7875 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7876 : : _33 = _31 + _32;
7877 : : // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
7878 : : _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7879 : : _35 = _33 + _34;
7880 : : // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7881 : : // _31[1]+.._31[4], ... _31[4]+.._31[7] };
7882 : : _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7883 : : _37 = _35 + _36;
7884 : : // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7885 : : // _31[0]+.._31[4], ... _31[0]+.._31[7] };
7886 : : _38 = _30 + _37;
7887 : : _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7888 : : MEM <vector(8) int> [(int *)&D.2043] = _39;
7889 : : MEM <vector(8) int> [(int *)&D.2042] = _38;
7890 : : Exclusive scan:
7891 : : _26 = D.2043[_25];
7892 : : D.2044[_25] = _26;
7893 : : _27 = D.2042[_25];
7894 : : _28 = _26 + _27;
7895 : : D.2043[_25] = _28;
7896 : : should be vectorized as (where _40 is the vectorized rhs
7897 : : from the D.2042[_21] = 0; store):
7898 : : _30 = MEM <vector(8) int> [(int *)&D.2043];
7899 : : _31 = MEM <vector(8) int> [(int *)&D.2042];
7900 : : _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7901 : : _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
7902 : : _34 = _32 + _33;
7903 : : // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
7904 : : // _31[3]+_31[4], ... _31[5]+.._31[6] };
7905 : : _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
7906 : : _36 = _34 + _35;
7907 : : // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7908 : : // _31[1]+.._31[4], ... _31[3]+.._31[6] };
7909 : : _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
7910 : : _38 = _36 + _37;
7911 : : // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
7912 : : // _31[0]+.._31[4], ... _31[0]+.._31[6] };
7913 : : _39 = _30 + _38;
7914 : : _50 = _31 + _39;
7915 : : _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
7916 : : MEM <vector(8) int> [(int *)&D.2044] = _39;
7917 : : MEM <vector(8) int> [(int *)&D.2042] = _51; */
7918 : 512 : enum machine_mode vec_mode = TYPE_MODE (vectype);
7919 : 512 : optab optab = optab_for_tree_code (code, vectype, optab_default);
7920 : 512 : if (!optab || !can_implement_p (optab, vec_mode))
7921 : 0 : goto fail;
7922 : :
7923 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init);
7924 : 512 : if (units_log2 == -1)
7925 : 0 : goto fail;
7926 : :
7927 : : return true;
7928 : : }
7929 : :
7930 : :
7931 : : /* Function vectorizable_scan_store.
7932 : :
7933 : : Helper of vectorizable_score, arguments like on vectorizable_store.
7934 : : Handle only the transformation, checking is done in check_scan_store. */
7935 : :
7936 : : static bool
7937 : 512 : vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
7938 : : slp_tree slp_node, gimple_stmt_iterator *gsi,
7939 : : gimple **vec_stmt, int ncopies)
7940 : : {
7941 : 512 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7942 : 512 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7943 : 512 : tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
7944 : 512 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
7945 : :
7946 : 512 : if (dump_enabled_p ())
7947 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
7948 : : "transform scan store. ncopies = %d\n", ncopies);
7949 : :
7950 : 512 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7951 : 512 : tree rhs = gimple_assign_rhs1 (stmt);
7952 : 512 : gcc_assert (TREE_CODE (rhs) == SSA_NAME);
7953 : :
7954 : 512 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7955 : 512 : bool inscan_var_store
7956 : 512 : = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7957 : :
7958 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
7959 : : {
7960 : 126 : use_operand_p use_p;
7961 : 126 : imm_use_iterator iter;
7962 : 126 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7963 : : {
7964 : 126 : gimple *use_stmt = USE_STMT (use_p);
7965 : 126 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
7966 : 0 : continue;
7967 : 126 : rhs = gimple_assign_lhs (use_stmt);
7968 : 126 : break;
7969 : : }
7970 : : }
7971 : :
7972 : 512 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7973 : 512 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
7974 : 512 : if (code == POINTER_PLUS_EXPR)
7975 : 0 : code = PLUS_EXPR;
7976 : 512 : gcc_assert (TREE_CODE_LENGTH (code) == binary_op
7977 : : && commutative_tree_code (code));
7978 : 512 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
7979 : 512 : tree rhs2 = gimple_assign_rhs2 (def_stmt);
7980 : 512 : gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
7981 : 512 : gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7982 : 512 : gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7983 : 512 : stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7984 : 512 : stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7985 : 512 : dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
7986 : 512 : dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
7987 : 512 : tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
7988 : 512 : tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
7989 : :
7990 : 512 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
7991 : : {
7992 : 436 : std::swap (rhs1, rhs2);
7993 : 436 : std::swap (var1, var2);
7994 : 436 : std::swap (load1_dr_info, load2_dr_info);
7995 : : }
7996 : :
7997 : 512 : tree *init = loop_vinfo->scan_map->get (var1);
7998 : 512 : gcc_assert (init);
7999 : :
8000 : 512 : unsigned HOST_WIDE_INT nunits;
8001 : 512 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
8002 : : gcc_unreachable ();
8003 : 512 : auto_vec<enum scan_store_kind, 16> use_whole_vector;
8004 : 512 : int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
8005 : 512 : gcc_assert (units_log2 > 0);
8006 : 512 : auto_vec<tree, 16> perms;
8007 : 512 : perms.quick_grow (units_log2 + 1);
8008 : 512 : tree zero_vec = NULL_TREE, masktype = NULL_TREE;
8009 : 2392 : for (int i = 0; i <= units_log2; ++i)
8010 : : {
8011 : 1880 : unsigned HOST_WIDE_INT j, k;
8012 : 1880 : vec_perm_builder sel (nunits, nunits, 1);
8013 : 1880 : sel.quick_grow (nunits);
8014 : 1880 : if (i == units_log2)
8015 : 4864 : for (j = 0; j < nunits; ++j)
8016 : 4352 : sel[j] = nunits - 1;
8017 : : else
8018 : : {
8019 : 5208 : for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
8020 : 3840 : sel[j] = j;
8021 : 13208 : for (k = 0; j < nunits; ++j, ++k)
8022 : 11840 : sel[j] = nunits + k;
8023 : : }
8024 : 3248 : vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
8025 : 1880 : if (!use_whole_vector.is_empty ()
8026 : 0 : && use_whole_vector[i] != scan_store_kind_perm)
8027 : : {
8028 : 0 : if (zero_vec == NULL_TREE)
8029 : 0 : zero_vec = build_zero_cst (vectype);
8030 : 0 : if (masktype == NULL_TREE
8031 : 0 : && use_whole_vector[i] == scan_store_kind_lshift_cond)
8032 : 0 : masktype = truth_type_for (vectype);
8033 : 0 : perms[i] = vect_gen_perm_mask_any (vectype, indices);
8034 : : }
8035 : : else
8036 : 1880 : perms[i] = vect_gen_perm_mask_checked (vectype, indices);
8037 : 1880 : }
8038 : :
8039 : 512 : tree vec_oprnd1 = NULL_TREE;
8040 : 512 : tree vec_oprnd2 = NULL_TREE;
8041 : 512 : tree vec_oprnd3 = NULL_TREE;
8042 : 512 : tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
8043 : 512 : tree dataref_offset = build_int_cst (ref_type, 0);
8044 : 512 : tree bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info,
8045 : : vectype, VMAT_CONTIGUOUS);
8046 : 512 : tree ldataref_ptr = NULL_TREE;
8047 : 512 : tree orig = NULL_TREE;
8048 : 512 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
8049 : 126 : ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
8050 : : /* The initialization is invariant. */
8051 : 512 : vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
8052 : 512 : auto_vec<tree> vec_oprnds2;
8053 : 512 : auto_vec<tree> vec_oprnds3;
8054 : 512 : if (ldataref_ptr == NULL)
8055 : : {
8056 : : /* We want to lookup the vector operands of the reduction, not those
8057 : : of the store - for SLP we have to use the proper SLP node for the
8058 : : lookup, which should be the single child of the scan store. */
8059 : 386 : vect_get_vec_defs (vinfo, stmt_info, SLP_TREE_CHILDREN (slp_node)[0],
8060 : : ncopies, rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
8061 : : /* ??? For SLP we do not key the def on 'rhs1' or 'rhs2' but get
8062 : : them in SLP child order. So we have to swap here with logic
8063 : : similar to above. */
8064 : 386 : stmt_vec_info load
8065 : 386 : = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
8066 : 386 : (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
8067 : 386 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
8068 : 386 : tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
8069 : 386 : if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
8070 : 820 : for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
8071 : 494 : std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
8072 : : }
8073 : : else
8074 : 126 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
8075 : : rhs2, &vec_oprnds3);
8076 : 1248 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8077 : : {
8078 : 736 : if (ldataref_ptr == NULL)
8079 : 554 : vec_oprnd2 = vec_oprnds2[j];
8080 : 736 : vec_oprnd3 = vec_oprnds3[j];
8081 : 736 : if (j == 0)
8082 : : orig = vec_oprnd3;
8083 : 224 : else if (!inscan_var_store)
8084 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8085 : :
8086 : 736 : if (ldataref_ptr)
8087 : : {
8088 : 182 : vec_oprnd2 = make_ssa_name (vectype);
8089 : 182 : tree data_ref = fold_build2 (MEM_REF, vectype,
8090 : : unshare_expr (ldataref_ptr),
8091 : : dataref_offset);
8092 : 182 : vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
8093 : 182 : gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
8094 : 182 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8095 : 182 : if (! slp_node)
8096 : : {
8097 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8098 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
8099 : : }
8100 : : }
8101 : :
8102 : 736 : tree v = vec_oprnd2;
8103 : 3068 : for (int i = 0; i < units_log2; ++i)
8104 : : {
8105 : 2332 : tree new_temp = make_ssa_name (vectype);
8106 : 2332 : gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
8107 : : (zero_vec
8108 : 0 : && (use_whole_vector[i]
8109 : 0 : != scan_store_kind_perm))
8110 : : ? zero_vec : vec_oprnd1, v,
8111 : 2332 : perms[i]);
8112 : 2332 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8113 : 2332 : if (! slp_node)
8114 : : {
8115 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8116 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
8117 : : }
8118 : :
8119 : 2332 : if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
8120 : : {
8121 : : /* Whole vector shift shifted in zero bits, but if *init
8122 : : is not initializer_zerop, we need to replace those elements
8123 : : with elements from vec_oprnd1. */
8124 : 0 : tree_vector_builder vb (masktype, nunits, 1);
8125 : 0 : for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
8126 : 0 : vb.quick_push (k < (HOST_WIDE_INT_1U << i)
8127 : : ? boolean_false_node : boolean_true_node);
8128 : :
8129 : 0 : tree new_temp2 = make_ssa_name (vectype);
8130 : 0 : g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
8131 : : new_temp, vec_oprnd1);
8132 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
8133 : : g, gsi);
8134 : 0 : if (! slp_node)
8135 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8136 : 0 : new_temp = new_temp2;
8137 : 0 : }
8138 : :
8139 : : /* For exclusive scan, perform the perms[i] permutation once
8140 : : more. */
8141 : 2332 : if (i == 0
8142 : 1100 : && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
8143 : 728 : && v == vec_oprnd2)
8144 : : {
8145 : 364 : v = new_temp;
8146 : 364 : --i;
8147 : 364 : continue;
8148 : : }
8149 : :
8150 : 1968 : tree new_temp2 = make_ssa_name (vectype);
8151 : 1968 : g = gimple_build_assign (new_temp2, code, v, new_temp);
8152 : 1968 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8153 : 1968 : if (! slp_node)
8154 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8155 : :
8156 : 1968 : v = new_temp2;
8157 : : }
8158 : :
8159 : 736 : tree new_temp = make_ssa_name (vectype);
8160 : 736 : gimple *g = gimple_build_assign (new_temp, code, orig, v);
8161 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8162 : 736 : if (! slp_node)
8163 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8164 : :
8165 : 736 : tree last_perm_arg = new_temp;
8166 : : /* For exclusive scan, new_temp computed above is the exclusive scan
8167 : : prefix sum. Turn it into inclusive prefix sum for the broadcast
8168 : : of the last element into orig. */
8169 : 736 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8170 : : {
8171 : 364 : last_perm_arg = make_ssa_name (vectype);
8172 : 364 : g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
8173 : 364 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8174 : 364 : if (! slp_node)
8175 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8176 : : }
8177 : :
8178 : 736 : orig = make_ssa_name (vectype);
8179 : 2208 : g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
8180 : 736 : last_perm_arg, perms[units_log2]);
8181 : 736 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8182 : 736 : if (! slp_node)
8183 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8184 : :
8185 : 736 : if (!inscan_var_store)
8186 : : {
8187 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8188 : : unshare_expr (dataref_ptr),
8189 : : dataref_offset);
8190 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8191 : 368 : g = gimple_build_assign (data_ref, new_temp);
8192 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8193 : 368 : if (! slp_node)
8194 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8195 : : }
8196 : : }
8197 : :
8198 : 512 : if (inscan_var_store)
8199 : 624 : for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8200 : : {
8201 : 368 : if (j != 0)
8202 : 112 : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8203 : :
8204 : 368 : tree data_ref = fold_build2 (MEM_REF, vectype,
8205 : : unshare_expr (dataref_ptr),
8206 : : dataref_offset);
8207 : 368 : vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8208 : 368 : gimple *g = gimple_build_assign (data_ref, orig);
8209 : 368 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8210 : 368 : if (! slp_node)
8211 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8212 : : }
8213 : 512 : return true;
8214 : 512 : }
8215 : :
8216 : :
8217 : : /* Function vectorizable_store.
8218 : :
8219 : : Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
8220 : : that can be vectorized.
8221 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
8222 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
8223 : : Return true if STMT_INFO is vectorizable in this way. */
8224 : :
8225 : : static bool
8226 : 1933672 : vectorizable_store (vec_info *vinfo,
8227 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
8228 : : gimple **vec_stmt, slp_tree slp_node,
8229 : : stmt_vector_for_cost *cost_vec)
8230 : : {
8231 : 1933672 : tree data_ref;
8232 : 1933672 : tree vec_oprnd = NULL_TREE;
8233 : 1933672 : tree elem_type;
8234 : 1933672 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
8235 : 1933672 : class loop *loop = NULL;
8236 : 1933672 : machine_mode vec_mode;
8237 : 1933672 : tree dummy;
8238 : 1933672 : enum vect_def_type rhs_dt = vect_unknown_def_type;
8239 : 1933672 : enum vect_def_type mask_dt = vect_unknown_def_type;
8240 : 1933672 : tree dataref_ptr = NULL_TREE;
8241 : 1933672 : tree dataref_offset = NULL_TREE;
8242 : 1933672 : gimple *ptr_incr = NULL;
8243 : 1933672 : int j;
8244 : 1933672 : stmt_vec_info first_stmt_info;
8245 : 1933672 : bool grouped_store;
8246 : 1933672 : unsigned int group_size, i;
8247 : 1933672 : unsigned int vec_num;
8248 : 1933672 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
8249 : 1933672 : tree aggr_type;
8250 : 1933672 : gather_scatter_info gs_info;
8251 : 1933672 : poly_uint64 vf;
8252 : 1933672 : vec_load_store_type vls_type;
8253 : 1933672 : tree ref_type;
8254 : :
8255 : 1933672 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
8256 : : return false;
8257 : :
8258 : 1933672 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
8259 : 158036 : && ! vec_stmt)
8260 : : return false;
8261 : :
8262 : : /* Is vectorizable store? */
8263 : :
8264 : 1775636 : tree mask = NULL_TREE, mask_vectype = NULL_TREE;
8265 : 1775636 : slp_tree mask_node = NULL;
8266 : 1775636 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
8267 : : {
8268 : 1698303 : tree scalar_dest = gimple_assign_lhs (assign);
8269 : 1698303 : if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
8270 : 1698303 : && is_pattern_stmt_p (stmt_info))
8271 : 1021 : scalar_dest = TREE_OPERAND (scalar_dest, 0);
8272 : 1698303 : if (TREE_CODE (scalar_dest) != ARRAY_REF
8273 : 1698303 : && TREE_CODE (scalar_dest) != BIT_FIELD_REF
8274 : : && TREE_CODE (scalar_dest) != INDIRECT_REF
8275 : : && TREE_CODE (scalar_dest) != COMPONENT_REF
8276 : : && TREE_CODE (scalar_dest) != IMAGPART_EXPR
8277 : : && TREE_CODE (scalar_dest) != REALPART_EXPR
8278 : : && TREE_CODE (scalar_dest) != MEM_REF)
8279 : : return false;
8280 : : }
8281 : : else
8282 : : {
8283 : 561405 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
8284 : 8342 : if (!call || !gimple_call_internal_p (call))
8285 : : return false;
8286 : :
8287 : 4486 : internal_fn ifn = gimple_call_internal_fn (call);
8288 : 4486 : if (!internal_store_fn_p (ifn))
8289 : : return false;
8290 : :
8291 : 1315 : int mask_index = internal_fn_mask_index (ifn);
8292 : 1315 : if (mask_index >= 0)
8293 : 1315 : mask_index = vect_slp_child_index_for_operand
8294 : 1315 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
8295 : 1315 : if (mask_index >= 0
8296 : 1315 : && !vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_index,
8297 : : &mask, &mask_node, &mask_dt,
8298 : : &mask_vectype))
8299 : : return false;
8300 : : }
8301 : :
8302 : : /* Cannot have hybrid store SLP -- that would mean storing to the
8303 : : same location twice. */
8304 : 1381180 : gcc_assert (PURE_SLP_STMT (stmt_info));
8305 : :
8306 : 1381180 : tree vectype = SLP_TREE_VECTYPE (stmt_info), rhs_vectype = NULL_TREE;
8307 : 1381180 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
8308 : :
8309 : 1381180 : if (loop_vinfo)
8310 : : {
8311 : 175155 : loop = LOOP_VINFO_LOOP (loop_vinfo);
8312 : 175155 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8313 : : }
8314 : : else
8315 : : vf = 1;
8316 : :
8317 : : /* FORNOW. This restriction should be relaxed. */
8318 : 175155 : if (loop
8319 : 1381417 : && nested_in_vect_loop_p (loop, stmt_info)
8320 : 175398 : && SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) > 1)
8321 : : {
8322 : 6 : if (dump_enabled_p ())
8323 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8324 : : "multiple types in nested loop.\n");
8325 : 6 : return false;
8326 : : }
8327 : :
8328 : 1381174 : tree op;
8329 : 1381174 : slp_tree op_node;
8330 : 1381174 : if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
8331 : : &op, &op_node, &rhs_dt, &rhs_vectype, &vls_type))
8332 : : return false;
8333 : :
8334 : 1381150 : elem_type = TREE_TYPE (vectype);
8335 : 1381150 : vec_mode = TYPE_MODE (vectype);
8336 : :
8337 : 1381150 : if (!STMT_VINFO_DATA_REF (stmt_info))
8338 : : return false;
8339 : :
8340 : 1381150 : vect_memory_access_type memory_access_type;
8341 : 1381150 : enum dr_alignment_support alignment_support_scheme;
8342 : 1381150 : int misalignment;
8343 : 1381150 : poly_int64 poffset;
8344 : 1381150 : internal_fn lanes_ifn;
8345 : 1381150 : if (!get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask, vls_type,
8346 : : 1, &memory_access_type, &poffset,
8347 : : &alignment_support_scheme, &misalignment, &gs_info,
8348 : : &lanes_ifn))
8349 : : return false;
8350 : :
8351 : 1380721 : if (slp_node->ldst_lanes
8352 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
8353 : : {
8354 : 0 : if (dump_enabled_p ())
8355 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8356 : : "discovered store-lane but cannot use it.\n");
8357 : 0 : return false;
8358 : : }
8359 : :
8360 : 1380721 : if (mask)
8361 : : {
8362 : 1243 : if (memory_access_type == VMAT_CONTIGUOUS)
8363 : : {
8364 : 453 : if (!VECTOR_MODE_P (vec_mode)
8365 : 1986 : || !can_vec_mask_load_store_p (vec_mode,
8366 : 993 : TYPE_MODE (mask_vectype), false))
8367 : 16 : return false;
8368 : : }
8369 : 250 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
8370 : 250 : && (memory_access_type != VMAT_GATHER_SCATTER
8371 : 222 : || (gs_info.decl && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
8372 : : {
8373 : 28 : if (dump_enabled_p ())
8374 : 28 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8375 : : "unsupported access type for masked store.\n");
8376 : 28 : return false;
8377 : : }
8378 : 222 : else if (memory_access_type == VMAT_GATHER_SCATTER
8379 : 222 : && gs_info.ifn == IFN_LAST
8380 : 222 : && !gs_info.decl)
8381 : : {
8382 : 68 : if (dump_enabled_p ())
8383 : 24 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8384 : : "unsupported masked emulated scatter.\n");
8385 : 68 : return false;
8386 : : }
8387 : : }
8388 : : else
8389 : : {
8390 : : /* FORNOW. In some cases can vectorize even if data-type not supported
8391 : : (e.g. - array initialization with 0). */
8392 : 1379478 : if (!can_implement_p (mov_optab, vec_mode))
8393 : : return false;
8394 : : }
8395 : :
8396 : 1380609 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
8397 : 1380609 : grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
8398 : 2603307 : && memory_access_type != VMAT_GATHER_SCATTER);
8399 : 1222698 : if (grouped_store)
8400 : : {
8401 : 1222698 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8402 : 1222698 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8403 : 1222698 : group_size = DR_GROUP_SIZE (first_stmt_info);
8404 : : }
8405 : : else
8406 : : {
8407 : 1380609 : first_stmt_info = stmt_info;
8408 : 1380609 : first_dr_info = dr_info;
8409 : 1380609 : group_size = vec_num = 1;
8410 : : }
8411 : :
8412 : 1380609 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && !vec_stmt)
8413 : : {
8414 : 1076 : if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node, mask,
8415 : : memory_access_type))
8416 : : return false;
8417 : : }
8418 : :
8419 : 2760450 : bool costing_p = !vec_stmt;
8420 : 1379841 : if (costing_p) /* transformation not required. */
8421 : : {
8422 : 839729 : STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
8423 : 839729 : SLP_TREE_MEMORY_ACCESS_TYPE (slp_node) = memory_access_type;
8424 : :
8425 : 839729 : if (loop_vinfo
8426 : 116075 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
8427 : 26 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
8428 : : vls_type, group_size,
8429 : : memory_access_type, &gs_info,
8430 : : mask);
8431 : :
8432 : 839729 : if (!vect_maybe_update_slp_op_vectype (op_node, vectype)
8433 : 839729 : || (mask
8434 : 650 : && !vect_maybe_update_slp_op_vectype (mask_node,
8435 : : mask_vectype)))
8436 : : {
8437 : 0 : if (dump_enabled_p ())
8438 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8439 : : "incompatible vector types for invariants\n");
8440 : 0 : return false;
8441 : : }
8442 : :
8443 : 839729 : if (dump_enabled_p ()
8444 : : && memory_access_type != VMAT_ELEMENTWISE
8445 : : && memory_access_type != VMAT_GATHER_SCATTER
8446 : : && memory_access_type != VMAT_STRIDED_SLP
8447 : 13271 : && memory_access_type != VMAT_INVARIANT
8448 : 853000 : && alignment_support_scheme != dr_aligned)
8449 : 4413 : dump_printf_loc (MSG_NOTE, vect_location,
8450 : : "Vectorizing an unaligned access.\n");
8451 : :
8452 : 839729 : STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
8453 : : }
8454 : 1380609 : gcc_assert (memory_access_type == SLP_TREE_MEMORY_ACCESS_TYPE (stmt_info));
8455 : :
8456 : : /* Transform. */
8457 : :
8458 : 1380609 : ensure_base_align (dr_info);
8459 : :
8460 : 1380609 : if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
8461 : : {
8462 : 1024 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
8463 : 1024 : gcc_assert (SLP_TREE_LANES (slp_node) == 1);
8464 : 1024 : if (costing_p)
8465 : : {
8466 : 512 : unsigned int inside_cost = 0, prologue_cost = 0;
8467 : 512 : if (vls_type == VLS_STORE_INVARIANT)
8468 : 0 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8469 : : stmt_info, 0, vect_prologue);
8470 : 512 : vect_get_store_cost (vinfo, stmt_info, slp_node, 1,
8471 : : alignment_support_scheme, misalignment,
8472 : : &inside_cost, cost_vec);
8473 : :
8474 : 512 : if (dump_enabled_p ())
8475 : 492 : dump_printf_loc (MSG_NOTE, vect_location,
8476 : : "vect_model_store_cost: inside_cost = %d, "
8477 : : "prologue_cost = %d .\n",
8478 : : inside_cost, prologue_cost);
8479 : :
8480 : 512 : return true;
8481 : : }
8482 : 512 : return vectorizable_scan_store (vinfo, stmt_info, slp_node,
8483 : 512 : gsi, vec_stmt, 1);
8484 : : }
8485 : :
8486 : : /* FORNOW */
8487 : 1379585 : gcc_assert (!grouped_store
8488 : : || !loop
8489 : : || !nested_in_vect_loop_p (loop, stmt_info));
8490 : :
8491 : 1379585 : grouped_store = false;
8492 : : /* VEC_NUM is the number of vect stmts to be created for this
8493 : : group. */
8494 : 1379585 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
8495 : 1379585 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
8496 : 1379585 : gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
8497 : : || (DR_GROUP_FIRST_ELEMENT (first_stmt_info) == first_stmt_info));
8498 : 1379585 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8499 : 1379585 : op = vect_get_store_rhs (first_stmt_info);
8500 : :
8501 : 1379585 : ref_type = get_group_alias_ptr_type (first_stmt_info);
8502 : :
8503 : 1379585 : if (!costing_p && dump_enabled_p ())
8504 : 11636 : dump_printf_loc (MSG_NOTE, vect_location, "transform store.\n");
8505 : :
8506 : 1379585 : if (memory_access_type == VMAT_ELEMENTWISE
8507 : 1379585 : || memory_access_type == VMAT_STRIDED_SLP)
8508 : : {
8509 : 26631 : unsigned inside_cost = 0, prologue_cost = 0;
8510 : 26631 : gimple_stmt_iterator incr_gsi;
8511 : 26631 : bool insert_after;
8512 : 26631 : tree offvar = NULL_TREE;
8513 : 26631 : tree ivstep;
8514 : 26631 : tree running_off;
8515 : 26631 : tree stride_base, stride_step, alias_off;
8516 : 26631 : tree vec_oprnd = NULL_TREE;
8517 : 26631 : tree dr_offset;
8518 : : /* Checked by get_load_store_type. */
8519 : 26631 : unsigned int const_nunits = nunits.to_constant ();
8520 : :
8521 : 26631 : gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
8522 : 26631 : gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
8523 : :
8524 : 26631 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
8525 : 26631 : stride_base
8526 : 26631 : = fold_build_pointer_plus
8527 : : (DR_BASE_ADDRESS (first_dr_info->dr),
8528 : : size_binop (PLUS_EXPR,
8529 : : convert_to_ptrofftype (dr_offset),
8530 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
8531 : 26631 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
8532 : :
8533 : : /* For a store with loop-invariant (but other than power-of-2)
8534 : : stride (i.e. not a grouped access) like so:
8535 : :
8536 : : for (i = 0; i < n; i += stride)
8537 : : array[i] = ...;
8538 : :
8539 : : we generate a new induction variable and new stores from
8540 : : the components of the (vectorized) rhs:
8541 : :
8542 : : for (j = 0; ; j += VF*stride)
8543 : : vectemp = ...;
8544 : : tmp1 = vectemp[0];
8545 : : array[j] = tmp1;
8546 : : tmp2 = vectemp[1];
8547 : : array[j + stride] = tmp2;
8548 : : ...
8549 : : */
8550 : :
8551 : 26631 : unsigned nstores = const_nunits;
8552 : 26631 : unsigned lnel = 1;
8553 : 26631 : tree ltype = elem_type;
8554 : 26631 : tree lvectype = vectype;
8555 : 26631 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
8556 : 26631 : if (n == const_nunits)
8557 : : {
8558 : 1669 : int mis_align = dr_misalignment (first_dr_info, vectype);
8559 : : /* With VF > 1 we advance the DR by step, if that is constant
8560 : : and only aligned when performed VF times, DR alignment
8561 : : analysis can analyze this as aligned since it assumes
8562 : : contiguous accesses. But that is not how we code generate
8563 : : here, so adjust for this. */
8564 : 1669 : if (maybe_gt (vf, 1u)
8565 : 2410 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8566 : 2194 : DR_TARGET_ALIGNMENT (first_dr_info)))
8567 : 216 : mis_align = -1;
8568 : 1669 : dr_alignment_support dr_align
8569 : 1669 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
8570 : : mis_align);
8571 : 1669 : if (dr_align == dr_aligned
8572 : 1669 : || dr_align == dr_unaligned_supported)
8573 : : {
8574 : 1669 : nstores = 1;
8575 : 1669 : lnel = const_nunits;
8576 : 1669 : ltype = vectype;
8577 : 1669 : lvectype = vectype;
8578 : 1669 : alignment_support_scheme = dr_align;
8579 : 1669 : misalignment = mis_align;
8580 : : }
8581 : : }
8582 : 24962 : else if (n > 1)
8583 : : {
8584 : 1777 : nstores = const_nunits / n;
8585 : 1777 : lnel = n;
8586 : 1777 : ltype = build_vector_type (elem_type, n);
8587 : 1777 : lvectype = vectype;
8588 : 1777 : int mis_align = dr_misalignment (first_dr_info, ltype);
8589 : 1777 : if (maybe_gt (vf, 1u)
8590 : 3554 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
8591 : 2857 : DR_TARGET_ALIGNMENT (first_dr_info)))
8592 : 697 : mis_align = -1;
8593 : 1777 : dr_alignment_support dr_align
8594 : 1777 : = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
8595 : : mis_align);
8596 : 1777 : alignment_support_scheme = dr_align;
8597 : 1777 : misalignment = mis_align;
8598 : :
8599 : : /* First check if vec_extract optab doesn't support extraction
8600 : : of vector elts directly. */
8601 : 1777 : scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
8602 : 1777 : machine_mode vmode;
8603 : 3554 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8604 : 2003 : || !related_vector_mode (TYPE_MODE (vectype), elmode,
8605 : 1777 : n).exists (&vmode)
8606 : 1601 : || (convert_optab_handler (vec_extract_optab,
8607 : 1601 : TYPE_MODE (vectype), vmode)
8608 : : == CODE_FOR_nothing)
8609 : 1777 : || !(dr_align == dr_aligned
8610 : 226 : || dr_align == dr_unaligned_supported))
8611 : : {
8612 : : /* Try to avoid emitting an extract of vector elements
8613 : : by performing the extracts using an integer type of the
8614 : : same size, extracting from a vector of those and then
8615 : : re-interpreting it as the original vector type if
8616 : : supported. */
8617 : 1551 : unsigned lsize = n * GET_MODE_BITSIZE (elmode);
8618 : 1551 : unsigned int lnunits = const_nunits / n;
8619 : : /* If we can't construct such a vector fall back to
8620 : : element extracts from the original vector type and
8621 : : element size stores. */
8622 : 1551 : if (int_mode_for_size (lsize, 0).exists (&elmode)
8623 : 1551 : && VECTOR_MODE_P (TYPE_MODE (vectype))
8624 : 1551 : && related_vector_mode (TYPE_MODE (vectype), elmode,
8625 : 1551 : lnunits).exists (&vmode)
8626 : 1517 : && (convert_optab_handler (vec_extract_optab,
8627 : : vmode, elmode)
8628 : : != CODE_FOR_nothing))
8629 : : {
8630 : 1517 : nstores = lnunits;
8631 : 1517 : lnel = n;
8632 : 1517 : ltype = build_nonstandard_integer_type (lsize, 1);
8633 : 1517 : lvectype = build_vector_type (ltype, nstores);
8634 : : }
8635 : : /* Else fall back to vector extraction anyway.
8636 : : Fewer stores are more important than avoiding spilling
8637 : : of the vector we extract from. Compared to the
8638 : : construction case in vectorizable_load no store-forwarding
8639 : : issue exists here for reasonable archs. But only
8640 : : if the store is supported. */
8641 : 34 : else if (!(dr_align == dr_aligned
8642 : 34 : || dr_align == dr_unaligned_supported))
8643 : : {
8644 : 23185 : nstores = const_nunits;
8645 : 23185 : lnel = 1;
8646 : 23185 : ltype = elem_type;
8647 : 23185 : lvectype = vectype;
8648 : : }
8649 : : }
8650 : : }
8651 : 26631 : unsigned align;
8652 : 26631 : if (alignment_support_scheme == dr_aligned)
8653 : 311 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
8654 : : else
8655 : 26320 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
8656 : : /* Alignment is at most the access size if we do multiple stores. */
8657 : 26631 : if (nstores > 1)
8658 : 24962 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
8659 : 26631 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
8660 : 26631 : int ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
8661 : :
8662 : 26631 : if (!costing_p)
8663 : : {
8664 : 3199 : ivstep = stride_step;
8665 : 3199 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
8666 : : build_int_cst (TREE_TYPE (ivstep), vf));
8667 : :
8668 : 3199 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
8669 : :
8670 : 3199 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
8671 : 3199 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
8672 : 3199 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
8673 : : insert_after, &offvar, NULL);
8674 : :
8675 : 3199 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
8676 : : }
8677 : :
8678 : 26631 : alias_off = build_int_cst (ref_type, 0);
8679 : 26631 : stmt_vec_info next_stmt_info = first_stmt_info;
8680 : 26631 : auto_vec<tree> vec_oprnds;
8681 : : /* For costing some adjacent vector stores, we'd like to cost with
8682 : : the total number of them once instead of cost each one by one. */
8683 : 26631 : unsigned int n_adjacent_stores = 0;
8684 : 26631 : running_off = offvar;
8685 : 26631 : if (!costing_p)
8686 : 3199 : vect_get_vec_defs (vinfo, next_stmt_info, slp_node, ncopies, op,
8687 : : &vec_oprnds);
8688 : 26631 : unsigned int group_el = 0;
8689 : 26631 : unsigned HOST_WIDE_INT elsz
8690 : 26631 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
8691 : 61993 : for (j = 0; j < ncopies; j++)
8692 : : {
8693 : 35362 : if (!costing_p)
8694 : : {
8695 : 4888 : vec_oprnd = vec_oprnds[j];
8696 : : /* Pun the vector to extract from if necessary. */
8697 : 4888 : if (lvectype != vectype)
8698 : : {
8699 : 1190 : tree tem = make_ssa_name (lvectype);
8700 : 1190 : tree cvt = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
8701 : 1190 : gimple *pun = gimple_build_assign (tem, cvt);
8702 : 1190 : vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
8703 : 1190 : vec_oprnd = tem;
8704 : : }
8705 : : }
8706 : 165951 : for (i = 0; i < nstores; i++)
8707 : : {
8708 : 130589 : if (costing_p)
8709 : : {
8710 : 115015 : n_adjacent_stores++;
8711 : 115015 : continue;
8712 : : }
8713 : 15574 : tree newref, newoff;
8714 : 15574 : gimple *incr, *assign;
8715 : 15574 : tree size = TYPE_SIZE (ltype);
8716 : : /* Extract the i'th component. */
8717 : 15574 : tree pos = fold_build2 (MULT_EXPR, bitsizetype,
8718 : : bitsize_int (i), size);
8719 : 15574 : tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
8720 : : size, pos);
8721 : :
8722 : 15574 : elem = force_gimple_operand_gsi (gsi, elem, true, NULL_TREE, true,
8723 : : GSI_SAME_STMT);
8724 : :
8725 : 15574 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
8726 : 15574 : group_el * elsz);
8727 : 15574 : newref = build2 (MEM_REF, ltype, running_off, this_off);
8728 : 15574 : vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
8729 : :
8730 : : /* And store it to *running_off. */
8731 : 15574 : assign = gimple_build_assign (newref, elem);
8732 : 15574 : vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
8733 : :
8734 : 15574 : group_el += lnel;
8735 : 15574 : if (group_el == group_size)
8736 : : {
8737 : 14507 : newoff = copy_ssa_name (running_off, NULL);
8738 : 14507 : incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
8739 : : running_off, stride_step);
8740 : 14507 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
8741 : :
8742 : 14507 : running_off = newoff;
8743 : 14507 : group_el = 0;
8744 : : }
8745 : : }
8746 : : }
8747 : :
8748 : 26631 : if (costing_p)
8749 : : {
8750 : 23432 : if (n_adjacent_stores > 0)
8751 : : {
8752 : : /* Take a single lane vector type store as scalar
8753 : : store to avoid ICE like 110776. */
8754 : 23432 : if (VECTOR_TYPE_P (ltype)
8755 : 23432 : && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
8756 : 1156 : vect_get_store_cost (vinfo, stmt_info, slp_node,
8757 : : n_adjacent_stores, alignment_support_scheme,
8758 : : misalignment, &inside_cost, cost_vec);
8759 : : else
8760 : 22276 : inside_cost
8761 : 22276 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8762 : : scalar_store, stmt_info, 0, vect_body);
8763 : : /* Only need vector extracting when there are more
8764 : : than one stores. */
8765 : 23432 : if (nstores > 1)
8766 : 22467 : inside_cost
8767 : 22467 : += record_stmt_cost (cost_vec, n_adjacent_stores,
8768 : : vec_to_scalar, slp_node, 0, vect_body);
8769 : : }
8770 : 23432 : if (dump_enabled_p ())
8771 : 616 : dump_printf_loc (MSG_NOTE, vect_location,
8772 : : "vect_model_store_cost: inside_cost = %d, "
8773 : : "prologue_cost = %d .\n",
8774 : : inside_cost, prologue_cost);
8775 : : }
8776 : :
8777 : 26631 : return true;
8778 : 26631 : }
8779 : :
8780 : 1352954 : gcc_assert (alignment_support_scheme);
8781 : 1352954 : vec_loop_masks *loop_masks
8782 : 146929 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
8783 : 1352954 : ? &LOOP_VINFO_MASKS (loop_vinfo)
8784 : 7 : : NULL);
8785 : 7 : vec_loop_lens *loop_lens
8786 : 146929 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
8787 : : ? &LOOP_VINFO_LENS (loop_vinfo)
8788 : 0 : : NULL);
8789 : :
8790 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
8791 : : are some difference here. We cannot enable both the lens and masks
8792 : : during transform but it is allowed during analysis.
8793 : : Shouldn't go with length-based approach if fully masked. */
8794 : 1352954 : if (cost_vec == NULL)
8795 : : /* The cost_vec is NULL during transfrom. */
8796 : 537169 : gcc_assert ((!loop_lens || !loop_masks));
8797 : :
8798 : : /* Targets with store-lane instructions must not require explicit
8799 : : realignment. vect_supportable_dr_alignment always returns either
8800 : : dr_aligned or dr_unaligned_supported for masked operations. */
8801 : 1352954 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
8802 : : && !mask
8803 : : && !loop_masks)
8804 : : || alignment_support_scheme == dr_aligned
8805 : : || alignment_support_scheme == dr_unaligned_supported);
8806 : :
8807 : 1352954 : tree offset = NULL_TREE;
8808 : 1352954 : if (!known_eq (poffset, 0))
8809 : 3956 : offset = size_int (poffset);
8810 : :
8811 : 1352954 : tree bump;
8812 : 1352954 : tree vec_offset = NULL_TREE;
8813 : 1352954 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
8814 : : {
8815 : 2373 : aggr_type = NULL_TREE;
8816 : 2373 : bump = NULL_TREE;
8817 : : }
8818 : 1350581 : else if (memory_access_type == VMAT_GATHER_SCATTER)
8819 : : {
8820 : 0 : aggr_type = elem_type;
8821 : 0 : if (!costing_p)
8822 : 0 : vect_get_strided_load_store_ops (stmt_info, loop_vinfo, gsi, &gs_info,
8823 : : &bump, &vec_offset, loop_lens);
8824 : : }
8825 : : else
8826 : : {
8827 : 1350581 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8828 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
8829 : : else
8830 : : aggr_type = vectype;
8831 : 1350581 : if (!costing_p)
8832 : 536695 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
8833 : : memory_access_type, loop_lens);
8834 : : }
8835 : :
8836 : 1352954 : if (mask && !costing_p)
8837 : 481 : LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
8838 : :
8839 : : /* In case the vectorization factor (VF) is bigger than the number
8840 : : of elements that we can fit in a vectype (nunits), we have to generate
8841 : : more than one vector stmt - i.e - we need to "unroll" the
8842 : : vector stmt by a factor VF/nunits. */
8843 : :
8844 : : /* In case of interleaving (non-unit grouped access):
8845 : :
8846 : : S1: &base + 2 = x2
8847 : : S2: &base = x0
8848 : : S3: &base + 1 = x1
8849 : : S4: &base + 3 = x3
8850 : :
8851 : : We create vectorized stores starting from base address (the access of the
8852 : : first stmt in the chain (S2 in the above example), when the last store stmt
8853 : : of the chain (S4) is reached:
8854 : :
8855 : : VS1: &base = vx2
8856 : : VS2: &base + vec_size*1 = vx0
8857 : : VS3: &base + vec_size*2 = vx1
8858 : : VS4: &base + vec_size*3 = vx3
8859 : :
8860 : : Then permutation statements are generated:
8861 : :
8862 : : VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
8863 : : VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
8864 : : ...
8865 : :
8866 : : And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
8867 : : (the order of the data-refs in the output of vect_permute_store_chain
8868 : : corresponds to the order of scalar stmts in the interleaving chain - see
8869 : : the documentation of vect_permute_store_chain()).
8870 : :
8871 : : In case of both multiple types and interleaving, above vector stores and
8872 : : permutation stmts are created for every copy. The result vector stmts are
8873 : : put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
8874 : : STMT_VINFO_RELATED_STMT for the next copies.
8875 : : */
8876 : :
8877 : 1352954 : auto_vec<tree> dr_chain (group_size);
8878 : 1352954 : auto_vec<tree> vec_masks;
8879 : 1352954 : tree vec_mask = NULL;
8880 : 1352954 : auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
8881 : 6124865 : for (i = 0; i < group_size; i++)
8882 : 3418957 : gvec_oprnds.quick_push (new auto_vec<tree> ());
8883 : :
8884 : 1352954 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
8885 : : {
8886 : 0 : if (costing_p)
8887 : : /* Update all incoming store operand nodes, the general handling
8888 : : above only handles the mask and the first store operand node. */
8889 : 0 : for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
8890 : 0 : if (child != mask_node
8891 : 0 : && !vect_maybe_update_slp_op_vectype (child, vectype))
8892 : : {
8893 : 0 : if (dump_enabled_p ())
8894 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8895 : : "incompatible vector types for invariants\n");
8896 : 0 : return false;
8897 : : }
8898 : 0 : unsigned inside_cost = 0, prologue_cost = 0;
8899 : : /* For costing some adjacent vector stores, we'd like to cost with
8900 : : the total number of them once instead of cost each one by one. */
8901 : 0 : unsigned int n_adjacent_stores = 0;
8902 : 0 : int ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) / group_size;
8903 : 0 : for (j = 0; j < ncopies; j++)
8904 : : {
8905 : 0 : if (j == 0)
8906 : : {
8907 : 0 : if (!costing_p)
8908 : : {
8909 : 0 : if (mask)
8910 : : {
8911 : 0 : vect_get_slp_defs (mask_node, &vec_masks);
8912 : 0 : vec_mask = vec_masks[0];
8913 : : }
8914 : 0 : dataref_ptr
8915 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
8916 : : aggr_type, NULL, offset, &dummy,
8917 : : gsi, &ptr_incr, false, bump);
8918 : : }
8919 : : }
8920 : 0 : else if (!costing_p)
8921 : : {
8922 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
8923 : 0 : if (mask)
8924 : 0 : vec_mask = vec_masks[j];
8925 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
8926 : : stmt_info, bump);
8927 : : }
8928 : :
8929 : 0 : if (costing_p)
8930 : : {
8931 : 0 : n_adjacent_stores += group_size;
8932 : 0 : continue;
8933 : : }
8934 : :
8935 : : /* Get an array into which we can store the individual vectors. */
8936 : 0 : tree vec_array = create_vector_array (vectype, group_size);
8937 : :
8938 : : /* Invalidate the current contents of VEC_ARRAY. This should
8939 : : become an RTL clobber too, which prevents the vector registers
8940 : : from being upward-exposed. */
8941 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
8942 : :
8943 : : /* Store the individual vectors into the array. */
8944 : 0 : for (i = 0; i < group_size; i++)
8945 : : {
8946 : 0 : slp_tree child;
8947 : 0 : if (i == 0 || !mask_node)
8948 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i];
8949 : : else
8950 : 0 : child = SLP_TREE_CHILDREN (slp_node)[i + 1];
8951 : 0 : vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
8952 : 0 : write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
8953 : : i);
8954 : : }
8955 : :
8956 : 0 : tree final_mask = NULL;
8957 : 0 : tree final_len = NULL;
8958 : 0 : tree bias = NULL;
8959 : 0 : if (loop_masks)
8960 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
8961 : : ncopies, vectype, j);
8962 : 0 : if (vec_mask)
8963 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
8964 : : vec_mask, gsi);
8965 : :
8966 : 0 : if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
8967 : : {
8968 : 0 : if (loop_lens)
8969 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
8970 : : ncopies, vectype, j, 1);
8971 : : else
8972 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
8973 : 0 : signed char biasval
8974 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
8975 : 0 : bias = build_int_cst (intQI_type_node, biasval);
8976 : 0 : if (!final_mask)
8977 : : {
8978 : 0 : mask_vectype = truth_type_for (vectype);
8979 : 0 : final_mask = build_minus_one_cst (mask_vectype);
8980 : : }
8981 : : }
8982 : :
8983 : 0 : gcall *call;
8984 : 0 : if (final_len && final_mask)
8985 : : {
8986 : : /* Emit:
8987 : : MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
8988 : : LEN, BIAS, VEC_ARRAY). */
8989 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
8990 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
8991 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
8992 : : dataref_ptr, alias_ptr,
8993 : : final_mask, final_len, bias,
8994 : : vec_array);
8995 : : }
8996 : 0 : else if (final_mask)
8997 : : {
8998 : : /* Emit:
8999 : : MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
9000 : : VEC_ARRAY). */
9001 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
9002 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
9003 : 0 : call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
9004 : : dataref_ptr, alias_ptr,
9005 : : final_mask, vec_array);
9006 : : }
9007 : : else
9008 : : {
9009 : : /* Emit:
9010 : : MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
9011 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
9012 : 0 : call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
9013 : 0 : gimple_call_set_lhs (call, data_ref);
9014 : : }
9015 : 0 : gimple_call_set_nothrow (call, true);
9016 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9017 : :
9018 : : /* Record that VEC_ARRAY is now dead. */
9019 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
9020 : : }
9021 : :
9022 : 0 : if (costing_p)
9023 : : {
9024 : 0 : if (n_adjacent_stores > 0)
9025 : 0 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9026 : : alignment_support_scheme, misalignment,
9027 : : &inside_cost, cost_vec);
9028 : 0 : if (dump_enabled_p ())
9029 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
9030 : : "vect_model_store_cost: inside_cost = %d, "
9031 : : "prologue_cost = %d .\n",
9032 : : inside_cost, prologue_cost);
9033 : : }
9034 : :
9035 : 0 : return true;
9036 : : }
9037 : :
9038 : 1352954 : if (memory_access_type == VMAT_GATHER_SCATTER)
9039 : : {
9040 : 2373 : gcc_assert (!grouped_store);
9041 : 2373 : auto_vec<tree> vec_offsets;
9042 : 2373 : unsigned int inside_cost = 0, prologue_cost = 0;
9043 : 2373 : int num_stmts = vec_num;
9044 : 5136 : for (j = 0; j < num_stmts; j++)
9045 : : {
9046 : 2763 : gimple *new_stmt;
9047 : 2763 : if (j == 0)
9048 : : {
9049 : 2373 : if (costing_p && vls_type == VLS_STORE_INVARIANT)
9050 : 210 : prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
9051 : : slp_node, 0, vect_prologue);
9052 : : else if (!costing_p)
9053 : : {
9054 : : /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
9055 : : DR_CHAIN is of size 1. */
9056 : 474 : gcc_assert (group_size == 1);
9057 : 474 : vect_get_slp_defs (op_node, gvec_oprnds[0]);
9058 : 474 : if (mask)
9059 : 70 : vect_get_slp_defs (mask_node, &vec_masks);
9060 : :
9061 : 474 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9062 : 474 : vect_get_gather_scatter_ops (loop_vinfo, loop, stmt_info,
9063 : : slp_node, &gs_info,
9064 : : &dataref_ptr, &vec_offsets);
9065 : : else
9066 : 0 : dataref_ptr
9067 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info,
9068 : : aggr_type, NULL, offset,
9069 : : &dummy, gsi, &ptr_incr, false,
9070 : : bump);
9071 : : }
9072 : : }
9073 : 390 : else if (!costing_p)
9074 : : {
9075 : 38 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
9076 : 38 : if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9077 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
9078 : : gsi, stmt_info, bump);
9079 : : }
9080 : :
9081 : 3485 : new_stmt = NULL;
9082 : 722 : if (!costing_p)
9083 : : {
9084 : 512 : vec_oprnd = (*gvec_oprnds[0])[j];
9085 : 512 : if (mask)
9086 : 90 : vec_mask = vec_masks[j];
9087 : : /* We should have catched mismatched types earlier. */
9088 : 512 : gcc_assert (useless_type_conversion_p (vectype,
9089 : : TREE_TYPE (vec_oprnd)));
9090 : : }
9091 : 512 : unsigned HOST_WIDE_INT align;
9092 : 512 : tree final_mask = NULL_TREE;
9093 : 3275 : tree final_len = NULL_TREE;
9094 : 3275 : tree bias = NULL_TREE;
9095 : 512 : if (!costing_p)
9096 : : {
9097 : 512 : if (loop_masks)
9098 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi,
9099 : : loop_masks, num_stmts,
9100 : : vectype, j);
9101 : 512 : if (vec_mask)
9102 : 90 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
9103 : : final_mask, vec_mask, gsi);
9104 : : }
9105 : :
9106 : 2763 : if (gs_info.ifn != IFN_LAST)
9107 : : {
9108 : 0 : if (costing_p)
9109 : : {
9110 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9111 : 0 : inside_cost
9112 : 0 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9113 : : slp_node, 0, vect_body);
9114 : 0 : continue;
9115 : 0 : }
9116 : :
9117 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9118 : 0 : vec_offset = vec_offsets[j];
9119 : :
9120 : 0 : tree scale = size_int (gs_info.scale);
9121 : :
9122 : 0 : if (gs_info.ifn == IFN_MASK_LEN_SCATTER_STORE)
9123 : : {
9124 : 0 : if (loop_lens)
9125 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi,
9126 : : loop_lens, num_stmts,
9127 : : vectype, j, 1);
9128 : : else
9129 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9130 : :
9131 : 0 : signed char biasval
9132 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9133 : 0 : bias = build_int_cst (intQI_type_node, biasval);
9134 : 0 : if (!final_mask)
9135 : : {
9136 : 0 : mask_vectype = truth_type_for (vectype);
9137 : 0 : final_mask = build_minus_one_cst (mask_vectype);
9138 : : }
9139 : : }
9140 : :
9141 : 0 : gcall *call;
9142 : 0 : if (final_len && final_mask)
9143 : : {
9144 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
9145 : 0 : call = gimple_build_call_internal (
9146 : : IFN_MASK_LEN_SCATTER_STORE, 7, dataref_ptr,
9147 : : vec_offset, scale, vec_oprnd, final_mask, final_len,
9148 : : bias);
9149 : : else
9150 : : /* Non-vector offset indicates that prefer to take
9151 : : MASK_LEN_STRIDED_STORE instead of the
9152 : : IFN_MASK_SCATTER_STORE with direct stride arg. */
9153 : 0 : call = gimple_build_call_internal (
9154 : : IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
9155 : : vec_offset, vec_oprnd, final_mask, final_len, bias);
9156 : : }
9157 : 0 : else if (final_mask)
9158 : 0 : call = gimple_build_call_internal
9159 : 0 : (IFN_MASK_SCATTER_STORE, 5, dataref_ptr,
9160 : : vec_offset, scale, vec_oprnd, final_mask);
9161 : : else
9162 : 0 : call = gimple_build_call_internal (IFN_SCATTER_STORE, 4,
9163 : : dataref_ptr, vec_offset,
9164 : : scale, vec_oprnd);
9165 : 0 : gimple_call_set_nothrow (call, true);
9166 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9167 : 0 : new_stmt = call;
9168 : : }
9169 : 2763 : else if (gs_info.decl)
9170 : : {
9171 : : /* The builtin decls path for scatter is legacy, x86 only. */
9172 : 326 : gcc_assert (nunits.is_constant ()
9173 : : && (!final_mask
9174 : : || SCALAR_INT_MODE_P
9175 : : (TYPE_MODE (TREE_TYPE (final_mask)))));
9176 : 326 : if (costing_p)
9177 : : {
9178 : 187 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9179 : 187 : inside_cost
9180 : 187 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9181 : : slp_node, 0, vect_body);
9182 : 187 : continue;
9183 : 187 : }
9184 : :
9185 : 139 : poly_uint64 offset_nunits
9186 : 139 : = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
9187 : 139 : if (known_eq (nunits, offset_nunits))
9188 : : {
9189 : 63 : new_stmt = vect_build_one_scatter_store_call
9190 : 189 : (vinfo, stmt_info, gsi, &gs_info,
9191 : 63 : dataref_ptr, vec_offsets[j],
9192 : : vec_oprnd, final_mask);
9193 : 63 : vect_finish_stmt_generation (vinfo, stmt_info,
9194 : : new_stmt, gsi);
9195 : : }
9196 : 76 : else if (known_eq (nunits, offset_nunits * 2))
9197 : : {
9198 : : /* We have a offset vector with half the number of
9199 : : lanes but the builtins will store full vectype
9200 : : data from the lower lanes. */
9201 : 30 : new_stmt = vect_build_one_scatter_store_call
9202 : 90 : (vinfo, stmt_info, gsi, &gs_info,
9203 : 30 : dataref_ptr, vec_offsets[2 * j],
9204 : : vec_oprnd, final_mask);
9205 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9206 : : new_stmt, gsi);
9207 : 30 : int count = nunits.to_constant ();
9208 : 30 : vec_perm_builder sel (count, count, 1);
9209 : 30 : sel.quick_grow (count);
9210 : 382 : for (int i = 0; i < count; ++i)
9211 : 352 : sel[i] = i | (count / 2);
9212 : 30 : vec_perm_indices indices (sel, 2, count);
9213 : 30 : tree perm_mask
9214 : 30 : = vect_gen_perm_mask_checked (vectype, indices);
9215 : 30 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
9216 : : vec_oprnd, vec_oprnd,
9217 : : perm_mask);
9218 : 30 : vec_oprnd = make_ssa_name (vectype);
9219 : 30 : gimple_set_lhs (new_stmt, vec_oprnd);
9220 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9221 : : new_stmt, gsi);
9222 : 30 : if (final_mask)
9223 : : {
9224 : 20 : new_stmt = gimple_build_assign (NULL_TREE,
9225 : : VEC_UNPACK_HI_EXPR,
9226 : : final_mask);
9227 : 20 : final_mask = make_ssa_name
9228 : 20 : (truth_type_for (gs_info.offset_vectype));
9229 : 20 : gimple_set_lhs (new_stmt, final_mask);
9230 : 20 : vect_finish_stmt_generation (vinfo, stmt_info,
9231 : : new_stmt, gsi);
9232 : : }
9233 : :
9234 : 30 : new_stmt = vect_build_one_scatter_store_call
9235 : 60 : (vinfo, stmt_info, gsi, &gs_info,
9236 : 30 : dataref_ptr, vec_offsets[2 * j + 1],
9237 : : vec_oprnd, final_mask);
9238 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
9239 : : new_stmt, gsi);
9240 : 30 : }
9241 : 46 : else if (known_eq (nunits * 2, offset_nunits))
9242 : : {
9243 : : /* We have a offset vector with double the number of
9244 : : lanes. Select the low/high part accordingly. */
9245 : 46 : vec_offset = vec_offsets[j / 2];
9246 : 46 : if (j & 1)
9247 : : {
9248 : 23 : int count = offset_nunits.to_constant ();
9249 : 23 : vec_perm_builder sel (count, count, 1);
9250 : 23 : sel.quick_grow (count);
9251 : 263 : for (int i = 0; i < count; ++i)
9252 : 240 : sel[i] = i | (count / 2);
9253 : 23 : vec_perm_indices indices (sel, 2, count);
9254 : 23 : tree perm_mask = vect_gen_perm_mask_checked
9255 : 23 : (TREE_TYPE (vec_offset), indices);
9256 : 23 : new_stmt = gimple_build_assign (NULL_TREE,
9257 : : VEC_PERM_EXPR,
9258 : : vec_offset,
9259 : : vec_offset,
9260 : : perm_mask);
9261 : 23 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
9262 : 23 : gimple_set_lhs (new_stmt, vec_offset);
9263 : 23 : vect_finish_stmt_generation (vinfo, stmt_info,
9264 : : new_stmt, gsi);
9265 : 23 : }
9266 : :
9267 : 46 : new_stmt = vect_build_one_scatter_store_call
9268 : 46 : (vinfo, stmt_info, gsi, &gs_info,
9269 : : dataref_ptr, vec_offset,
9270 : : vec_oprnd, final_mask);
9271 : 46 : vect_finish_stmt_generation (vinfo, stmt_info,
9272 : : new_stmt, gsi);
9273 : : }
9274 : : else
9275 : 0 : gcc_unreachable ();
9276 : : }
9277 : : else
9278 : : {
9279 : : /* Emulated scatter. */
9280 : 2437 : gcc_assert (!final_mask);
9281 : 2437 : if (costing_p)
9282 : : {
9283 : 2064 : unsigned int cnunits = vect_nunits_for_cost (vectype);
9284 : : /* For emulated scatter N offset vector element extracts
9285 : : (we assume the scalar scaling and ptr + offset add is
9286 : : consumed by the load). */
9287 : 2064 : inside_cost
9288 : 2064 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9289 : : slp_node, 0, vect_body);
9290 : : /* N scalar stores plus extracting the elements. */
9291 : 2064 : inside_cost
9292 : 2064 : += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9293 : : slp_node, 0, vect_body);
9294 : 2064 : inside_cost
9295 : 2064 : += record_stmt_cost (cost_vec, cnunits, scalar_store,
9296 : : slp_node, 0, vect_body);
9297 : 2064 : continue;
9298 : 2064 : }
9299 : :
9300 : 373 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
9301 : 373 : unsigned HOST_WIDE_INT const_offset_nunits
9302 : 373 : = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype).to_constant ();
9303 : 373 : vec<constructor_elt, va_gc> *ctor_elts;
9304 : 373 : vec_alloc (ctor_elts, const_nunits);
9305 : 373 : gimple_seq stmts = NULL;
9306 : 373 : tree elt_type = TREE_TYPE (vectype);
9307 : 373 : unsigned HOST_WIDE_INT elt_size
9308 : 373 : = tree_to_uhwi (TYPE_SIZE (elt_type));
9309 : : /* We support offset vectors with more elements
9310 : : than the data vector for now. */
9311 : 373 : unsigned HOST_WIDE_INT factor
9312 : : = const_offset_nunits / const_nunits;
9313 : 373 : vec_offset = vec_offsets[j / factor];
9314 : 373 : unsigned elt_offset
9315 : 373 : = (j % factor) * const_nunits;
9316 : 373 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
9317 : 373 : tree scale = size_int (gs_info.scale);
9318 : 373 : align = get_object_alignment (DR_REF (first_dr_info->dr));
9319 : 373 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
9320 : 1519 : for (unsigned k = 0; k < const_nunits; ++k)
9321 : : {
9322 : : /* Compute the offsetted pointer. */
9323 : 1146 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
9324 : : bitsize_int (k + elt_offset));
9325 : 1146 : tree idx
9326 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
9327 : 1146 : vec_offset, TYPE_SIZE (idx_type), boff);
9328 : 1146 : idx = gimple_convert (&stmts, sizetype, idx);
9329 : 1146 : idx = gimple_build (&stmts, MULT_EXPR, sizetype,
9330 : : idx, scale);
9331 : 1146 : tree ptr
9332 : 1146 : = gimple_build (&stmts, PLUS_EXPR,
9333 : 1146 : TREE_TYPE (dataref_ptr),
9334 : : dataref_ptr, idx);
9335 : 1146 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
9336 : : /* Extract the element to be stored. */
9337 : 1146 : tree elt
9338 : 2292 : = gimple_build (&stmts, BIT_FIELD_REF,
9339 : 1146 : TREE_TYPE (vectype),
9340 : 1146 : vec_oprnd, TYPE_SIZE (elt_type),
9341 : 1146 : bitsize_int (k * elt_size));
9342 : 1146 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9343 : 1146 : stmts = NULL;
9344 : 1146 : tree ref
9345 : 1146 : = build2 (MEM_REF, ltype, ptr,
9346 : : build_int_cst (ref_type, 0));
9347 : 1146 : new_stmt = gimple_build_assign (ref, elt);
9348 : 1146 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9349 : : }
9350 : :
9351 : 373 : slp_node->push_vec_def (new_stmt);
9352 : : }
9353 : : }
9354 : :
9355 : 2373 : if (costing_p && dump_enabled_p ())
9356 : 68 : dump_printf_loc (MSG_NOTE, vect_location,
9357 : : "vect_model_store_cost: inside_cost = %d, "
9358 : : "prologue_cost = %d .\n",
9359 : : inside_cost, prologue_cost);
9360 : :
9361 : 2373 : return true;
9362 : 2373 : }
9363 : :
9364 : 1350581 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
9365 : : || memory_access_type == VMAT_CONTIGUOUS_DOWN
9366 : : || memory_access_type == VMAT_CONTIGUOUS_PERMUTE
9367 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
9368 : :
9369 : 1350581 : unsigned inside_cost = 0, prologue_cost = 0;
9370 : : /* For costing some adjacent vector stores, we'd like to cost with
9371 : : the total number of them once instead of cost each one by one. */
9372 : 1350581 : unsigned int n_adjacent_stores = 0;
9373 : 1350581 : auto_vec<tree> result_chain (group_size);
9374 : 1350581 : auto_vec<tree, 1> vec_oprnds;
9375 : 1350581 : gimple *new_stmt;
9376 : 1350581 : if (!costing_p)
9377 : : {
9378 : : /* Get vectorized arguments for SLP_NODE. */
9379 : 536695 : vect_get_vec_defs (vinfo, stmt_info, slp_node, 1, op,
9380 : : &vec_oprnds, mask, &vec_masks);
9381 : 536695 : vec_oprnd = vec_oprnds[0];
9382 : 536695 : if (mask)
9383 : 411 : vec_mask = vec_masks[0];
9384 : : }
9385 : : else
9386 : : {
9387 : : /* For interleaved stores we collect vectorized defs for all the
9388 : : stores in the group in DR_CHAIN. DR_CHAIN is then used as an
9389 : : input to vect_permute_store_chain().
9390 : :
9391 : : If the store is not grouped, DR_GROUP_SIZE is 1, and DR_CHAIN
9392 : : is of size 1. */
9393 : : stmt_vec_info next_stmt_info = first_stmt_info;
9394 : 2818037 : for (i = 0; i < group_size; i++)
9395 : : {
9396 : : /* Since gaps are not supported for interleaved stores,
9397 : : DR_GROUP_SIZE is the exact number of stmts in the chain.
9398 : : Therefore, NEXT_STMT_INFO can't be NULL_TREE. In case
9399 : : that there is no interleaving, DR_GROUP_SIZE is 1,
9400 : : and only one iteration of the loop will be executed. */
9401 : 2004151 : op = vect_get_store_rhs (next_stmt_info);
9402 : 2004151 : if (!costing_p)
9403 : : {
9404 : : vect_get_vec_defs_for_operand (vinfo, next_stmt_info,
9405 : : 1, op, gvec_oprnds[i]);
9406 : : vec_oprnd = (*gvec_oprnds[i])[0];
9407 : : dr_chain.quick_push (vec_oprnd);
9408 : : }
9409 : 2004151 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
9410 : : }
9411 : 813886 : if (mask && !costing_p)
9412 : : {
9413 : : vect_get_vec_defs_for_operand (vinfo, stmt_info, 1,
9414 : : mask, &vec_masks, mask_vectype);
9415 : : vec_mask = vec_masks[0];
9416 : : }
9417 : : }
9418 : :
9419 : : /* We should have catched mismatched types earlier. */
9420 : 1350015 : gcc_assert (costing_p
9421 : : || useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
9422 : 1350581 : bool simd_lane_access_p
9423 : 1350581 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
9424 : 1350581 : if (!costing_p
9425 : 1350581 : && simd_lane_access_p
9426 : 4351 : && !loop_masks
9427 : 4351 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
9428 : 4351 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
9429 : 4351 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
9430 : 4351 : && integer_zerop (DR_INIT (first_dr_info->dr))
9431 : 1354932 : && alias_sets_conflict_p (get_alias_set (aggr_type),
9432 : 4351 : get_alias_set (TREE_TYPE (ref_type))))
9433 : : {
9434 : 4343 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
9435 : 4343 : dataref_offset = build_int_cst (ref_type, 0);
9436 : : }
9437 : 1346238 : else if (!costing_p)
9438 : 1064696 : dataref_ptr = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
9439 : : simd_lane_access_p ? loop : NULL,
9440 : : offset, &dummy, gsi, &ptr_incr,
9441 : : simd_lane_access_p, bump);
9442 : :
9443 : 1350581 : new_stmt = NULL;
9444 : 1350581 : if (grouped_store)
9445 : : {
9446 : : /* Permute. */
9447 : : gcc_assert (memory_access_type == VMAT_CONTIGUOUS_PERMUTE);
9448 : : if (costing_p)
9449 : : {
9450 : : int group_size = DR_GROUP_SIZE (first_stmt_info);
9451 : : int nstmts = ceil_log2 (group_size) * group_size;
9452 : : inside_cost += record_stmt_cost (cost_vec, nstmts, vec_perm,
9453 : : slp_node, 0, vect_body);
9454 : : if (dump_enabled_p ())
9455 : : dump_printf_loc (MSG_NOTE, vect_location, "vect_model_store_cost: "
9456 : : "strided group_size = %d .\n", group_size);
9457 : : }
9458 : : else
9459 : : vect_permute_store_chain (vinfo, dr_chain, group_size, stmt_info,
9460 : : gsi, &result_chain);
9461 : : }
9462 : :
9463 : 3072565 : for (i = 0; i < vec_num; i++)
9464 : : {
9465 : 1721984 : if (!costing_p)
9466 : 665850 : vec_oprnd = vec_oprnds[i];
9467 : :
9468 : 1721984 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
9469 : : {
9470 : 3108 : if (costing_p)
9471 : 1977 : inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
9472 : : slp_node, 0, vect_body);
9473 : : else
9474 : : {
9475 : 1131 : tree perm_mask = perm_mask_for_reverse (vectype);
9476 : 1131 : tree perm_dest
9477 : 1131 : = vect_create_destination_var (vect_get_store_rhs (stmt_info),
9478 : : vectype);
9479 : 1131 : tree new_temp = make_ssa_name (perm_dest);
9480 : :
9481 : : /* Generate the permute statement. */
9482 : 1131 : gimple *perm_stmt
9483 : 1131 : = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
9484 : : vec_oprnd, perm_mask);
9485 : 1131 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9486 : :
9487 : 1131 : perm_stmt = SSA_NAME_DEF_STMT (new_temp);
9488 : 1131 : vec_oprnd = new_temp;
9489 : : }
9490 : : }
9491 : :
9492 : 1721984 : if (costing_p)
9493 : : {
9494 : 1056134 : n_adjacent_stores++;
9495 : 1056134 : continue;
9496 : : }
9497 : :
9498 : 665850 : tree final_mask = NULL_TREE;
9499 : 665850 : tree final_len = NULL_TREE;
9500 : 665850 : tree bias = NULL_TREE;
9501 : 665850 : if (loop_masks)
9502 : 8 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9503 : : vec_num, vectype, i);
9504 : 665850 : if (vec_mask)
9505 : 468 : vec_mask = vec_masks[i];
9506 : 468 : if (vec_mask)
9507 : 468 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9508 : : vec_mask, gsi);
9509 : :
9510 : 665850 : if (i > 0)
9511 : : /* Bump the vector pointer. */
9512 : 129155 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9513 : : stmt_info, bump);
9514 : :
9515 : 665850 : unsigned misalign;
9516 : 665850 : unsigned HOST_WIDE_INT align;
9517 : 665850 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
9518 : 665850 : if (alignment_support_scheme == dr_aligned)
9519 : : misalign = 0;
9520 : 305312 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
9521 : : {
9522 : 155758 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
9523 : 155758 : misalign = 0;
9524 : : }
9525 : : else
9526 : 149554 : misalign = misalignment;
9527 : 665850 : if (dataref_offset == NULL_TREE
9528 : 660503 : && TREE_CODE (dataref_ptr) == SSA_NAME)
9529 : 172408 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
9530 : 665850 : align = least_bit_hwi (misalign | align);
9531 : :
9532 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
9533 : 665850 : machine_mode vmode = TYPE_MODE (vectype);
9534 : 665850 : machine_mode new_vmode = vmode;
9535 : 665850 : internal_fn partial_ifn = IFN_LAST;
9536 : 665850 : if (loop_lens)
9537 : : {
9538 : 0 : opt_machine_mode new_ovmode
9539 : 0 : = get_len_load_store_mode (vmode, false, &partial_ifn);
9540 : 0 : new_vmode = new_ovmode.require ();
9541 : 0 : unsigned factor
9542 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
9543 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
9544 : : vec_num, vectype, i, factor);
9545 : : }
9546 : 665850 : else if (final_mask)
9547 : : {
9548 : 476 : if (!can_vec_mask_load_store_p (vmode,
9549 : 476 : TYPE_MODE (TREE_TYPE (final_mask)),
9550 : : false, &partial_ifn))
9551 : 0 : gcc_unreachable ();
9552 : : }
9553 : :
9554 : 665850 : if (partial_ifn == IFN_MASK_LEN_STORE)
9555 : : {
9556 : 0 : if (!final_len)
9557 : : {
9558 : : /* Pass VF value to 'len' argument of
9559 : : MASK_LEN_STORE if LOOP_LENS is invalid. */
9560 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9561 : : }
9562 : 0 : if (!final_mask)
9563 : : {
9564 : : /* Pass all ones value to 'mask' argument of
9565 : : MASK_LEN_STORE if final_mask is invalid. */
9566 : 0 : mask_vectype = truth_type_for (vectype);
9567 : 0 : final_mask = build_minus_one_cst (mask_vectype);
9568 : : }
9569 : : }
9570 : 665850 : if (final_len)
9571 : : {
9572 : 0 : signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9573 : 0 : bias = build_int_cst (intQI_type_node, biasval);
9574 : : }
9575 : :
9576 : : /* Arguments are ready. Create the new vector stmt. */
9577 : 665850 : if (final_len)
9578 : : {
9579 : 0 : gcall *call;
9580 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9581 : : /* Need conversion if it's wrapped with VnQI. */
9582 : 0 : if (vmode != new_vmode)
9583 : : {
9584 : 0 : tree new_vtype
9585 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
9586 : : new_vmode);
9587 : 0 : tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
9588 : 0 : vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
9589 : 0 : gassign *new_stmt
9590 : 0 : = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
9591 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9592 : 0 : vec_oprnd = var;
9593 : : }
9594 : :
9595 : 0 : if (partial_ifn == IFN_MASK_LEN_STORE)
9596 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
9597 : : dataref_ptr, ptr, final_mask,
9598 : : final_len, bias, vec_oprnd);
9599 : : else
9600 : 0 : call = gimple_build_call_internal (IFN_LEN_STORE, 5,
9601 : : dataref_ptr, ptr, final_len,
9602 : : bias, vec_oprnd);
9603 : 0 : gimple_call_set_nothrow (call, true);
9604 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9605 : 0 : new_stmt = call;
9606 : : }
9607 : 665850 : else if (final_mask)
9608 : : {
9609 : 476 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
9610 : 476 : gcall *call
9611 : 476 : = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
9612 : : ptr, final_mask, vec_oprnd);
9613 : 476 : gimple_call_set_nothrow (call, true);
9614 : 476 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9615 : 476 : new_stmt = call;
9616 : : }
9617 : : else
9618 : : {
9619 : 665374 : data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr,
9620 : : dataref_offset ? dataref_offset
9621 : : : build_int_cst (ref_type, 0));
9622 : 665374 : if (alignment_support_scheme == dr_aligned
9623 : 665374 : && align >= TYPE_ALIGN_UNIT (vectype))
9624 : : ;
9625 : : else
9626 : 304984 : TREE_TYPE (data_ref)
9627 : 609968 : = build_aligned_type (TREE_TYPE (data_ref),
9628 : : align * BITS_PER_UNIT);
9629 : 665374 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
9630 : 665374 : new_stmt = gimple_build_assign (data_ref, vec_oprnd);
9631 : 665374 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9632 : : }
9633 : : }
9634 : :
9635 : 1350581 : if (costing_p)
9636 : : {
9637 : 813886 : if (n_adjacent_stores > 0)
9638 : 813886 : vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9639 : : alignment_support_scheme, misalignment,
9640 : : &inside_cost, cost_vec);
9641 : :
9642 : : /* When vectorizing a store into the function result assign
9643 : : a penalty if the function returns in a multi-register location.
9644 : : In this case we assume we'll end up with having to spill the
9645 : : vector result and do piecewise loads as a conservative estimate. */
9646 : 813886 : tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
9647 : 813886 : if (base
9648 : 813886 : && (TREE_CODE (base) == RESULT_DECL
9649 : 752253 : || (DECL_P (base) && cfun_returns (base)))
9650 : 893462 : && !aggregate_value_p (base, cfun->decl))
9651 : : {
9652 : 17219 : rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
9653 : : /* ??? Handle PARALLEL in some way. */
9654 : 17219 : if (REG_P (reg))
9655 : : {
9656 : 17023 : int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
9657 : : /* Assume that a single reg-reg move is possible and cheap,
9658 : : do not account for vector to gp register move cost. */
9659 : 17023 : if (nregs > 1)
9660 : : {
9661 : : /* Spill. */
9662 : 16096 : prologue_cost
9663 : 16096 : += record_stmt_cost (cost_vec, 1, vector_store,
9664 : : slp_node, 0, vect_epilogue);
9665 : : /* Loads. */
9666 : 16096 : prologue_cost
9667 : 16096 : += record_stmt_cost (cost_vec, nregs, scalar_load,
9668 : : slp_node, 0, vect_epilogue);
9669 : : }
9670 : : }
9671 : : }
9672 : 813886 : if (dump_enabled_p ())
9673 : 12779 : dump_printf_loc (MSG_NOTE, vect_location,
9674 : : "vect_model_store_cost: inside_cost = %d, "
9675 : : "prologue_cost = %d .\n",
9676 : : inside_cost, prologue_cost);
9677 : : }
9678 : :
9679 : 1350581 : return true;
9680 : 2703535 : }
9681 : :
9682 : : /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
9683 : : VECTOR_CST mask. No checks are made that the target platform supports the
9684 : : mask, so callers may wish to test can_vec_perm_const_p separately, or use
9685 : : vect_gen_perm_mask_checked. */
9686 : :
9687 : : tree
9688 : 58354 : vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
9689 : : {
9690 : 58354 : tree mask_type;
9691 : :
9692 : 58354 : poly_uint64 nunits = sel.length ();
9693 : 58354 : gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
9694 : :
9695 : 58354 : mask_type = build_vector_type (ssizetype, nunits);
9696 : 58354 : return vec_perm_indices_to_tree (mask_type, sel);
9697 : : }
9698 : :
9699 : : /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
9700 : : i.e. that the target supports the pattern _for arbitrary input vectors_. */
9701 : :
9702 : : tree
9703 : 55824 : vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
9704 : : {
9705 : 55824 : machine_mode vmode = TYPE_MODE (vectype);
9706 : 55824 : gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
9707 : 55824 : return vect_gen_perm_mask_any (vectype, sel);
9708 : : }
9709 : :
9710 : : /* Given a vector variable X and Y, that was generated for the scalar
9711 : : STMT_INFO, generate instructions to permute the vector elements of X and Y
9712 : : using permutation mask MASK_VEC, insert them at *GSI and return the
9713 : : permuted vector variable. */
9714 : :
9715 : : static tree
9716 : 1412 : permute_vec_elements (vec_info *vinfo,
9717 : : tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
9718 : : gimple_stmt_iterator *gsi)
9719 : : {
9720 : 1412 : tree vectype = TREE_TYPE (x);
9721 : 1412 : tree perm_dest, data_ref;
9722 : 1412 : gimple *perm_stmt;
9723 : :
9724 : 1412 : tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
9725 : 1412 : if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
9726 : 1412 : perm_dest = vect_create_destination_var (scalar_dest, vectype);
9727 : : else
9728 : 0 : perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
9729 : 1412 : data_ref = make_ssa_name (perm_dest);
9730 : :
9731 : : /* Generate the permute statement. */
9732 : 1412 : perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
9733 : 1412 : vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
9734 : :
9735 : 1412 : return data_ref;
9736 : : }
9737 : :
9738 : : /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
9739 : : inserting them on the loops preheader edge. Returns true if we
9740 : : were successful in doing so (and thus STMT_INFO can be moved then),
9741 : : otherwise returns false. HOIST_P indicates if we want to hoist the
9742 : : definitions of all SSA uses, it would be false when we are costing. */
9743 : :
9744 : : static bool
9745 : 3873 : hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
9746 : : {
9747 : 3873 : ssa_op_iter i;
9748 : 3873 : use_operand_p use_p;
9749 : 3873 : auto_vec<use_operand_p, 8> to_hoist;
9750 : :
9751 : 7399 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
9752 : : {
9753 : 3544 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9754 : 3544 : if (!gimple_nop_p (def_stmt)
9755 : 3544 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
9756 : : {
9757 : : /* Make sure we don't need to recurse. While we could do
9758 : : so in simple cases when there are more complex use webs
9759 : : we don't have an easy way to preserve stmt order to fulfil
9760 : : dependencies within them. */
9761 : 69 : tree op2;
9762 : 69 : ssa_op_iter i2;
9763 : 69 : if (gimple_code (def_stmt) == GIMPLE_PHI
9764 : 69 : || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
9765 : : == NULL_DEF_OPERAND_P))
9766 : 18 : return false;
9767 : 136 : FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
9768 : : {
9769 : 85 : gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
9770 : 85 : if (!gimple_nop_p (def_stmt2)
9771 : 85 : && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
9772 : : return false;
9773 : : }
9774 : 51 : to_hoist.safe_push (use_p);
9775 : : }
9776 : : }
9777 : :
9778 : 7710 : if (to_hoist.is_empty ())
9779 : : return true;
9780 : :
9781 : 51 : if (!hoist_p)
9782 : : return true;
9783 : :
9784 : : /* Instead of moving defs we copy them so we can zero their UID to not
9785 : : confuse dominance queries in the preheader. */
9786 : 13 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9787 : 52 : for (use_operand_p use_p : to_hoist)
9788 : : {
9789 : 13 : gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
9790 : 13 : gimple *copy = gimple_copy (def_stmt);
9791 : 13 : gimple_set_uid (copy, 0);
9792 : 13 : def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
9793 : 13 : tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
9794 : 13 : update_stmt (copy);
9795 : 13 : def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
9796 : 13 : SET_DEF (def_p, new_def);
9797 : 13 : SET_USE (use_p, new_def);
9798 : 13 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
9799 : : }
9800 : :
9801 : : return true;
9802 : 3873 : }
9803 : :
9804 : : /* vectorizable_load.
9805 : :
9806 : : Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
9807 : : that can be vectorized.
9808 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
9809 : : stmt to replace it, put it in VEC_STMT, and insert it at GSI.
9810 : : Return true if STMT_INFO is vectorizable in this way. */
9811 : :
9812 : : static bool
9813 : 1910306 : vectorizable_load (vec_info *vinfo,
9814 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
9815 : : gimple **vec_stmt, slp_tree slp_node,
9816 : : stmt_vector_for_cost *cost_vec)
9817 : : {
9818 : 1910306 : tree scalar_dest;
9819 : 1910306 : tree vec_dest = NULL;
9820 : 1910306 : tree data_ref = NULL;
9821 : 1910306 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
9822 : 1910306 : class loop *loop = NULL;
9823 : 1910306 : class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
9824 : 1910306 : bool nested_in_vect_loop = false;
9825 : 1910306 : tree elem_type;
9826 : : /* Avoid false positive uninitialized warning, see PR110652. */
9827 : 1910306 : tree new_temp = NULL_TREE;
9828 : 1910306 : machine_mode mode;
9829 : 1910306 : tree dummy;
9830 : 1910306 : tree dataref_ptr = NULL_TREE;
9831 : 1910306 : tree dataref_offset = NULL_TREE;
9832 : 1910306 : gimple *ptr_incr = NULL;
9833 : 1910306 : int i, j;
9834 : 1910306 : unsigned int group_size;
9835 : 1910306 : poly_uint64 group_gap_adj;
9836 : 1910306 : tree msq = NULL_TREE, lsq;
9837 : 1910306 : tree realignment_token = NULL_TREE;
9838 : 1910306 : gphi *phi = NULL;
9839 : 1910306 : vec<tree> dr_chain = vNULL;
9840 : 1910306 : bool grouped_load = false;
9841 : 1910306 : stmt_vec_info first_stmt_info;
9842 : 1910306 : stmt_vec_info first_stmt_info_for_drptr = NULL;
9843 : 1910306 : bool compute_in_loop = false;
9844 : 1910306 : class loop *at_loop;
9845 : 1910306 : int vec_num;
9846 : 1910306 : bool slp_perm = false;
9847 : 1910306 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
9848 : 1910306 : poly_uint64 vf;
9849 : 1910306 : tree aggr_type;
9850 : 1910306 : gather_scatter_info gs_info;
9851 : 1910306 : tree ref_type;
9852 : 1910306 : enum vect_def_type mask_dt = vect_unknown_def_type;
9853 : 1910306 : enum vect_def_type els_dt = vect_unknown_def_type;
9854 : :
9855 : 1910306 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
9856 : : return false;
9857 : :
9858 : 1910306 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
9859 : 158036 : && ! vec_stmt)
9860 : : return false;
9861 : :
9862 : 1752270 : if (!STMT_VINFO_DATA_REF (stmt_info))
9863 : : return false;
9864 : :
9865 : 1424183 : tree mask = NULL_TREE, mask_vectype = NULL_TREE;
9866 : 1424183 : tree els = NULL_TREE; tree els_vectype = NULL_TREE;
9867 : :
9868 : 1424183 : int mask_index = -1;
9869 : 1424183 : int els_index = -1;
9870 : 1424183 : slp_tree slp_op = NULL;
9871 : 1424183 : slp_tree els_op = NULL;
9872 : 1424183 : if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
9873 : : {
9874 : 1421111 : scalar_dest = gimple_assign_lhs (assign);
9875 : 1421111 : if (TREE_CODE (scalar_dest) != SSA_NAME)
9876 : : return false;
9877 : :
9878 : 580408 : tree_code code = gimple_assign_rhs_code (assign);
9879 : 580408 : if (code != ARRAY_REF
9880 : 580408 : && code != BIT_FIELD_REF
9881 : 580408 : && code != INDIRECT_REF
9882 : 397081 : && code != COMPONENT_REF
9883 : 397081 : && code != IMAGPART_EXPR
9884 : 263632 : && code != REALPART_EXPR
9885 : 263632 : && code != MEM_REF
9886 : 148 : && TREE_CODE_CLASS (code) != tcc_declaration)
9887 : : return false;
9888 : : }
9889 : : else
9890 : : {
9891 : 1331018 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
9892 : 3072 : if (!call || !gimple_call_internal_p (call))
9893 : : return false;
9894 : :
9895 : 3072 : internal_fn ifn = gimple_call_internal_fn (call);
9896 : 3072 : if (!internal_load_fn_p (ifn))
9897 : : return false;
9898 : :
9899 : 2238 : scalar_dest = gimple_call_lhs (call);
9900 : 2238 : if (!scalar_dest)
9901 : : return false;
9902 : :
9903 : 2238 : mask_index = internal_fn_mask_index (ifn);
9904 : 2238 : if (mask_index >= 0)
9905 : 2238 : mask_index = vect_slp_child_index_for_operand
9906 : 2238 : (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9907 : 2238 : if (mask_index >= 0
9908 : 2238 : && !vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_index,
9909 : : &mask, &slp_op, &mask_dt, &mask_vectype))
9910 : : return false;
9911 : :
9912 : 2238 : els_index = internal_fn_else_index (ifn);
9913 : 2238 : if (els_index >= 0)
9914 : 2238 : els_index = vect_slp_child_index_for_operand
9915 : 2238 : (call, els_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9916 : 2238 : if (els_index >= 0
9917 : 2238 : && !vect_is_simple_use (vinfo, stmt_info, slp_node, els_index,
9918 : : &els, &els_op, &els_dt, &els_vectype))
9919 : : return false;
9920 : : }
9921 : :
9922 : 582646 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
9923 : 582646 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
9924 : :
9925 : 582646 : if (loop_vinfo)
9926 : : {
9927 : 360389 : loop = LOOP_VINFO_LOOP (loop_vinfo);
9928 : 360389 : nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
9929 : 360389 : vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
9930 : : }
9931 : : else
9932 : : vf = 1;
9933 : :
9934 : : /* FORNOW. This restriction should be relaxed. */
9935 : 360389 : if (nested_in_vect_loop
9936 : 1316 : && SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) > 1)
9937 : : {
9938 : 274 : if (dump_enabled_p ())
9939 : 46 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9940 : : "multiple types in nested loop.\n");
9941 : 274 : return false;
9942 : : }
9943 : :
9944 : 582372 : elem_type = TREE_TYPE (vectype);
9945 : 582372 : mode = TYPE_MODE (vectype);
9946 : :
9947 : : /* FORNOW. In some cases can vectorize even if data-type not supported
9948 : : (e.g. - data copies). */
9949 : 582372 : if (!can_implement_p (mov_optab, mode))
9950 : : {
9951 : 0 : if (dump_enabled_p ())
9952 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9953 : : "Aligned load, but unsupported type.\n");
9954 : 0 : return false;
9955 : : }
9956 : :
9957 : : /* Check if the load is a part of an interleaving chain. */
9958 : 582372 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
9959 : : {
9960 : 292807 : grouped_load = true;
9961 : : /* FORNOW */
9962 : 292807 : gcc_assert (!nested_in_vect_loop);
9963 : 292807 : gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
9964 : :
9965 : 292807 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
9966 : 292807 : group_size = DR_GROUP_SIZE (first_stmt_info);
9967 : :
9968 : : /* Invalidate assumptions made by dependence analysis when vectorization
9969 : : on the unrolled body effectively re-orders stmts. */
9970 : 292807 : if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
9971 : 292807 : && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
9972 : : STMT_VINFO_MIN_NEG_DIST (stmt_info)))
9973 : : {
9974 : 12 : if (dump_enabled_p ())
9975 : 12 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9976 : : "cannot perform implicit CSE when performing "
9977 : : "group loads with negative dependence distance\n");
9978 : 12 : return false;
9979 : : }
9980 : : }
9981 : : else
9982 : : group_size = 1;
9983 : :
9984 : 582360 : vect_memory_access_type memory_access_type;
9985 : 582360 : enum dr_alignment_support alignment_support_scheme;
9986 : 582360 : int misalignment;
9987 : 582360 : poly_int64 poffset;
9988 : 582360 : internal_fn lanes_ifn;
9989 : 582360 : auto_vec<int> elsvals;
9990 : 582360 : int maskload_elsval = 0;
9991 : 582360 : bool need_zeroing = false;
9992 : 582360 : if (!get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask, VLS_LOAD,
9993 : : 1, &memory_access_type, &poffset,
9994 : : &alignment_support_scheme, &misalignment, &gs_info,
9995 : : &lanes_ifn, &elsvals))
9996 : : return false;
9997 : :
9998 : :
9999 : : /* We might need to explicitly zero inactive elements if there are
10000 : : padding bits in the type that might leak otherwise.
10001 : : Refer to PR115336. */
10002 : 525374 : tree scalar_type = TREE_TYPE (scalar_dest);
10003 : 525374 : bool type_mode_padding_p
10004 : 1050748 : = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
10005 : :
10006 : : /* ??? The following checks should really be part of
10007 : : get_group_load_store_type. */
10008 : 525374 : if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
10009 : 525374 : && !((memory_access_type == VMAT_ELEMENTWISE
10010 : 54544 : || memory_access_type == VMAT_GATHER_SCATTER)
10011 : 9014 : && SLP_TREE_LANES (slp_node) == 1))
10012 : : {
10013 : 54544 : slp_perm = true;
10014 : :
10015 : 54544 : if (!loop_vinfo)
10016 : : {
10017 : : /* In BB vectorization we may not actually use a loaded vector
10018 : : accessing elements in excess of DR_GROUP_SIZE. */
10019 : 32756 : stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10020 : 32756 : group_info = DR_GROUP_FIRST_ELEMENT (group_info);
10021 : 32756 : unsigned HOST_WIDE_INT nunits;
10022 : 32756 : unsigned j, k, maxk = 0;
10023 : 118619 : FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
10024 : 85863 : if (k > maxk)
10025 : : maxk = k;
10026 : 32756 : tree vectype = SLP_TREE_VECTYPE (slp_node);
10027 : 61035 : if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
10028 : 32756 : || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
10029 : : {
10030 : 4477 : if (dump_enabled_p ())
10031 : 33 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10032 : : "BB vectorization with gaps at the end of "
10033 : : "a load is not supported\n");
10034 : 4477 : return false;
10035 : : }
10036 : : }
10037 : :
10038 : 50067 : auto_vec<tree> tem;
10039 : 50067 : unsigned n_perms;
10040 : 50067 : if (!vect_transform_slp_perm_load (vinfo, slp_node, tem, NULL, vf,
10041 : : true, &n_perms))
10042 : : {
10043 : 2784 : if (dump_enabled_p ())
10044 : 125 : dump_printf_loc (MSG_MISSED_OPTIMIZATION,
10045 : : vect_location,
10046 : : "unsupported load permutation\n");
10047 : 2784 : return false;
10048 : : }
10049 : 50067 : }
10050 : :
10051 : 518113 : if (slp_node->ldst_lanes
10052 : 0 : && memory_access_type != VMAT_LOAD_STORE_LANES)
10053 : : {
10054 : 0 : if (dump_enabled_p ())
10055 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10056 : : "discovered load-lane but cannot use it.\n");
10057 : 0 : return false;
10058 : : }
10059 : :
10060 : 518113 : if (mask)
10061 : : {
10062 : 2126 : if (memory_access_type == VMAT_CONTIGUOUS)
10063 : : {
10064 : 1344 : machine_mode vec_mode = TYPE_MODE (vectype);
10065 : 361 : if (!VECTOR_MODE_P (vec_mode)
10066 : 2688 : || !can_vec_mask_load_store_p (vec_mode,
10067 : 1344 : TYPE_MODE (mask_vectype),
10068 : : true, NULL, &elsvals))
10069 : 61 : return false;
10070 : : }
10071 : 782 : else if (memory_access_type != VMAT_LOAD_STORE_LANES
10072 : 782 : && memory_access_type != VMAT_GATHER_SCATTER)
10073 : : {
10074 : 62 : if (dump_enabled_p ())
10075 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10076 : : "unsupported access type for masked load.\n");
10077 : 62 : return false;
10078 : : }
10079 : 720 : else if (memory_access_type == VMAT_GATHER_SCATTER
10080 : 720 : && gs_info.ifn == IFN_LAST
10081 : 720 : && !gs_info.decl)
10082 : : {
10083 : 476 : if (dump_enabled_p ())
10084 : 26 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10085 : : "unsupported masked emulated gather.\n");
10086 : 476 : return false;
10087 : : }
10088 : : else if (memory_access_type == VMAT_ELEMENTWISE
10089 : : || memory_access_type == VMAT_STRIDED_SLP)
10090 : : {
10091 : : if (dump_enabled_p ())
10092 : : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10093 : : "unsupported masked strided access.\n");
10094 : : return false;
10095 : : }
10096 : : }
10097 : :
10098 : 517514 : bool costing_p = !vec_stmt;
10099 : :
10100 : 517514 : if (costing_p) /* transformation not required. */
10101 : : {
10102 : 360851 : if (mask
10103 : 360851 : && !vect_maybe_update_slp_op_vectype (slp_op,
10104 : : mask_vectype))
10105 : : {
10106 : 0 : if (dump_enabled_p ())
10107 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10108 : : "incompatible vector types for invariants\n");
10109 : 0 : return false;
10110 : : }
10111 : :
10112 : 360851 : SLP_TREE_MEMORY_ACCESS_TYPE (slp_node) = memory_access_type;
10113 : :
10114 : 360851 : if (loop_vinfo
10115 : 226450 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
10116 : 67 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
10117 : : VLS_LOAD, group_size,
10118 : : memory_access_type, &gs_info,
10119 : : mask, &elsvals);
10120 : :
10121 : 360851 : if (dump_enabled_p ()
10122 : : && memory_access_type != VMAT_ELEMENTWISE
10123 : : && memory_access_type != VMAT_GATHER_SCATTER
10124 : : && memory_access_type != VMAT_STRIDED_SLP
10125 : 22107 : && memory_access_type != VMAT_INVARIANT
10126 : 382507 : && alignment_support_scheme != dr_aligned)
10127 : 8657 : dump_printf_loc (MSG_NOTE, vect_location,
10128 : : "Vectorizing an unaligned access.\n");
10129 : :
10130 : 360851 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10131 : 0 : vinfo->any_known_not_updated_vssa = true;
10132 : :
10133 : 360851 : STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
10134 : : }
10135 : : else
10136 : : {
10137 : : /* Here just get the else values. */
10138 : 156663 : if (loop_vinfo
10139 : 75617 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
10140 : 31 : check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
10141 : : VLS_LOAD, group_size,
10142 : : memory_access_type, &gs_info,
10143 : : mask, &elsvals);
10144 : : }
10145 : :
10146 : : /* If the type needs padding we must zero inactive elements.
10147 : : Check if we can do that with a VEC_COND_EXPR and store the
10148 : : elsval we choose in MASKLOAD_ELSVAL. */
10149 : 517514 : if (elsvals.length ()
10150 : 1357 : && type_mode_padding_p
10151 : 0 : && !elsvals.contains (MASK_LOAD_ELSE_ZERO)
10152 : 1357 : && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
10153 : : {
10154 : 0 : if (dump_enabled_p ())
10155 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10156 : : "cannot zero inactive elements.\n");
10157 : 0 : return false;
10158 : : }
10159 : :
10160 : : /* For now just use the first available else value.
10161 : : get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
10162 : : select it here if it is supported. */
10163 : 517514 : if (elsvals.length ())
10164 : 1357 : maskload_elsval = *elsvals.begin ();
10165 : :
10166 : 517514 : gcc_assert (memory_access_type == SLP_TREE_MEMORY_ACCESS_TYPE (slp_node));
10167 : :
10168 : 517514 : if (dump_enabled_p () && !costing_p)
10169 : 15662 : dump_printf_loc (MSG_NOTE, vect_location, "transform load.\n");
10170 : :
10171 : : /* Transform. */
10172 : :
10173 : 517514 : dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
10174 : 517514 : ensure_base_align (dr_info);
10175 : :
10176 : 517514 : if (memory_access_type == VMAT_INVARIANT)
10177 : : {
10178 : 3833 : gcc_assert (!grouped_load && !mask && !bb_vinfo);
10179 : : /* If we have versioned for aliasing or the loop doesn't
10180 : : have any data dependencies that would preclude this,
10181 : : then we are sure this is a loop invariant load and
10182 : : thus we can insert it on the preheader edge.
10183 : : TODO: hoist_defs_of_uses should ideally be computed
10184 : : once at analysis time, remembered and used in the
10185 : : transform time. */
10186 : 3833 : bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
10187 : 3667 : && !nested_in_vect_loop
10188 : 7084 : && hoist_defs_of_uses (stmt_info->stmt, loop, false));
10189 : 3833 : if (costing_p)
10190 : : {
10191 : 407 : enum vect_cost_model_location cost_loc
10192 : 3018 : = hoist_p ? vect_prologue : vect_body;
10193 : 3018 : unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
10194 : : slp_node, 0, cost_loc);
10195 : 3018 : cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
10196 : : slp_node, 0, cost_loc);
10197 : 3018 : unsigned int prologue_cost = hoist_p ? cost : 0;
10198 : 407 : unsigned int inside_cost = hoist_p ? 0 : cost;
10199 : 3018 : if (dump_enabled_p ())
10200 : 451 : dump_printf_loc (MSG_NOTE, vect_location,
10201 : : "vect_model_load_cost: inside_cost = %d, "
10202 : : "prologue_cost = %d .\n",
10203 : : inside_cost, prologue_cost);
10204 : 3018 : return true;
10205 : : }
10206 : 815 : if (hoist_p)
10207 : : {
10208 : 622 : gassign *stmt = as_a <gassign *> (stmt_info->stmt);
10209 : 622 : if (dump_enabled_p ())
10210 : 210 : dump_printf_loc (MSG_NOTE, vect_location,
10211 : : "hoisting out of the vectorized loop: %G",
10212 : : (gimple *) stmt);
10213 : 622 : scalar_dest = copy_ssa_name (scalar_dest);
10214 : 622 : tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
10215 : 622 : edge pe = loop_preheader_edge (loop);
10216 : 622 : gphi *vphi = get_virtual_phi (loop->header);
10217 : 622 : tree vuse;
10218 : 622 : if (vphi)
10219 : 616 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
10220 : : else
10221 : 6 : vuse = gimple_vuse (gsi_stmt (*gsi));
10222 : 622 : gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
10223 : 622 : gimple_set_vuse (new_stmt, vuse);
10224 : 622 : gsi_insert_on_edge_immediate (pe, new_stmt);
10225 : 622 : hoist_defs_of_uses (new_stmt, loop, true);
10226 : : }
10227 : : /* These copies are all equivalent. */
10228 : 815 : if (hoist_p)
10229 : 622 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10230 : : vectype, NULL);
10231 : : else
10232 : : {
10233 : 193 : gimple_stmt_iterator gsi2 = *gsi;
10234 : 193 : gsi_next (&gsi2);
10235 : 193 : new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10236 : : vectype, &gsi2);
10237 : : }
10238 : 815 : gimple *new_stmt = SSA_NAME_DEF_STMT (new_temp);
10239 : 1691 : for (j = 0; j < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node); ++j)
10240 : 876 : slp_node->push_vec_def (new_stmt);
10241 : : return true;
10242 : : }
10243 : :
10244 : 513681 : if (memory_access_type == VMAT_ELEMENTWISE
10245 : 513681 : || memory_access_type == VMAT_STRIDED_SLP)
10246 : : {
10247 : 25160 : gimple_stmt_iterator incr_gsi;
10248 : 25160 : bool insert_after;
10249 : 25160 : tree offvar = NULL_TREE;
10250 : 25160 : tree ivstep;
10251 : 25160 : tree running_off;
10252 : 25160 : vec<constructor_elt, va_gc> *v = NULL;
10253 : 25160 : tree stride_base, stride_step, alias_off;
10254 : : /* Checked by get_load_store_type. */
10255 : 25160 : unsigned int const_nunits = nunits.to_constant ();
10256 : 25160 : unsigned HOST_WIDE_INT cst_offset = 0;
10257 : 25160 : tree dr_offset;
10258 : 25160 : unsigned int inside_cost = 0;
10259 : :
10260 : 25160 : gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
10261 : 25160 : gcc_assert (!nested_in_vect_loop);
10262 : :
10263 : 25160 : if (grouped_load)
10264 : : {
10265 : 12750 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10266 : 12750 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10267 : : }
10268 : : else
10269 : : {
10270 : : first_stmt_info = stmt_info;
10271 : : first_dr_info = dr_info;
10272 : : }
10273 : :
10274 : 12750 : if (grouped_load && memory_access_type == VMAT_STRIDED_SLP)
10275 : : {
10276 : 3656 : group_size = DR_GROUP_SIZE (first_stmt_info);
10277 : 3656 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10278 : : }
10279 : : else
10280 : : {
10281 : 9094 : if (grouped_load)
10282 : 9094 : cst_offset
10283 : 9094 : = (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)))
10284 : 9094 : * vect_get_place_in_interleaving_chain (stmt_info,
10285 : : first_stmt_info));
10286 : 21504 : group_size = 1;
10287 : 21504 : ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
10288 : : }
10289 : :
10290 : 25160 : if (!costing_p)
10291 : : {
10292 : 3186 : dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
10293 : 3186 : stride_base = fold_build_pointer_plus (
10294 : : DR_BASE_ADDRESS (first_dr_info->dr),
10295 : : size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
10296 : : convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
10297 : 3186 : stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
10298 : :
10299 : : /* For a load with loop-invariant (but other than power-of-2)
10300 : : stride (i.e. not a grouped access) like so:
10301 : :
10302 : : for (i = 0; i < n; i += stride)
10303 : : ... = array[i];
10304 : :
10305 : : we generate a new induction variable and new accesses to
10306 : : form a new vector (or vectors, depending on ncopies):
10307 : :
10308 : : for (j = 0; ; j += VF*stride)
10309 : : tmp1 = array[j];
10310 : : tmp2 = array[j + stride];
10311 : : ...
10312 : : vectemp = {tmp1, tmp2, ...}
10313 : : */
10314 : :
10315 : 3186 : ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
10316 : : build_int_cst (TREE_TYPE (stride_step), vf));
10317 : :
10318 : 3186 : standard_iv_increment_position (loop, &incr_gsi, &insert_after);
10319 : :
10320 : 3186 : stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
10321 : 3186 : ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
10322 : 3186 : create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
10323 : : loop, &incr_gsi, insert_after,
10324 : : &offvar, NULL);
10325 : :
10326 : 3186 : stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
10327 : : }
10328 : :
10329 : 25160 : running_off = offvar;
10330 : 25160 : alias_off = build_int_cst (ref_type, 0);
10331 : 25160 : int nloads = const_nunits;
10332 : 25160 : int lnel = 1;
10333 : 25160 : tree ltype = TREE_TYPE (vectype);
10334 : 25160 : tree lvectype = vectype;
10335 : 25160 : auto_vec<tree> dr_chain;
10336 : 25160 : if (memory_access_type == VMAT_STRIDED_SLP)
10337 : : {
10338 : 16066 : HOST_WIDE_INT n = gcd (group_size, const_nunits);
10339 : : /* Use the target vector type if the group size is a multiple
10340 : : of it. */
10341 : 16066 : if (n == const_nunits)
10342 : : {
10343 : 1512 : int mis_align = dr_misalignment (first_dr_info, vectype);
10344 : : /* With VF > 1 we advance the DR by step, if that is constant
10345 : : and only aligned when performed VF times, DR alignment
10346 : : analysis can analyze this as aligned since it assumes
10347 : : contiguous accesses. But that is not how we code generate
10348 : : here, so adjust for this. */
10349 : 1512 : if (maybe_gt (vf, 1u)
10350 : 2460 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10351 : 2278 : DR_TARGET_ALIGNMENT (first_dr_info)))
10352 : 182 : mis_align = -1;
10353 : 1512 : dr_alignment_support dr_align
10354 : 1512 : = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
10355 : : mis_align);
10356 : 1512 : if (dr_align == dr_aligned
10357 : 1512 : || dr_align == dr_unaligned_supported)
10358 : : {
10359 : 1512 : nloads = 1;
10360 : 1512 : lnel = const_nunits;
10361 : 1512 : ltype = vectype;
10362 : 1512 : alignment_support_scheme = dr_align;
10363 : 1512 : misalignment = mis_align;
10364 : : }
10365 : : }
10366 : : /* Else use the biggest vector we can load the group without
10367 : : accessing excess elements. */
10368 : 14554 : else if (n > 1)
10369 : : {
10370 : 2097 : tree ptype;
10371 : 2097 : tree vtype
10372 : 2097 : = vector_vector_composition_type (vectype, const_nunits / n,
10373 : : &ptype);
10374 : 2097 : if (vtype != NULL_TREE)
10375 : : {
10376 : 2045 : dr_alignment_support dr_align;
10377 : 2045 : int mis_align = 0;
10378 : 2045 : if (VECTOR_TYPE_P (ptype))
10379 : : {
10380 : 1017 : mis_align = dr_misalignment (first_dr_info, ptype);
10381 : 1017 : if (maybe_gt (vf, 1u)
10382 : 2006 : && !multiple_p (DR_STEP_ALIGNMENT (first_dr_info->dr),
10383 : 1023 : DR_TARGET_ALIGNMENT (first_dr_info)))
10384 : 983 : mis_align = -1;
10385 : 1017 : dr_align
10386 : 1017 : = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
10387 : : mis_align);
10388 : : }
10389 : : else
10390 : : dr_align = dr_unaligned_supported;
10391 : 2045 : if (dr_align == dr_aligned
10392 : 2045 : || dr_align == dr_unaligned_supported)
10393 : : {
10394 : 2045 : nloads = const_nunits / n;
10395 : 2045 : lnel = n;
10396 : 2045 : lvectype = vtype;
10397 : 2045 : ltype = ptype;
10398 : 2045 : alignment_support_scheme = dr_align;
10399 : 2045 : misalignment = mis_align;
10400 : : }
10401 : : }
10402 : : }
10403 : 16066 : unsigned align;
10404 : 16066 : if (alignment_support_scheme == dr_aligned)
10405 : 16 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
10406 : : else
10407 : 16050 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
10408 : : /* Alignment is at most the access size if we do multiple loads. */
10409 : 16066 : if (nloads > 1)
10410 : 14554 : align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
10411 : 16066 : ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
10412 : : }
10413 : :
10414 : : /* For SLP permutation support we need to load the whole group,
10415 : : not only the number of vector stmts the permutation result
10416 : : fits in. */
10417 : 25160 : int ncopies;
10418 : 25160 : if (slp_perm)
10419 : : {
10420 : : /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
10421 : : variable VF. */
10422 : 2475 : unsigned int const_vf = vf.to_constant ();
10423 : 2475 : ncopies = CEIL (group_size * const_vf, const_nunits);
10424 : 2475 : dr_chain.create (ncopies);
10425 : : }
10426 : : else
10427 : 22685 : ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
10428 : :
10429 : 25160 : unsigned int group_el = 0;
10430 : 25160 : unsigned HOST_WIDE_INT
10431 : 25160 : elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
10432 : 25160 : unsigned int n_groups = 0;
10433 : : /* For costing some adjacent vector loads, we'd like to cost with
10434 : : the total number of them once instead of cost each one by one. */
10435 : 25160 : unsigned int n_adjacent_loads = 0;
10436 : 58425 : for (j = 0; j < ncopies; j++)
10437 : : {
10438 : 33265 : if (nloads > 1 && !costing_p)
10439 : 2698 : vec_alloc (v, nloads);
10440 : : gimple *new_stmt = NULL;
10441 : 150311 : for (i = 0; i < nloads; i++)
10442 : : {
10443 : 117046 : if (costing_p)
10444 : : {
10445 : : /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
10446 : : avoid ICE, see PR110776. */
10447 : 107788 : if (VECTOR_TYPE_P (ltype)
10448 : 4036 : && memory_access_type != VMAT_ELEMENTWISE)
10449 : 4036 : n_adjacent_loads++;
10450 : : else
10451 : 103752 : inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
10452 : : slp_node, 0, vect_body);
10453 : 107788 : continue;
10454 : : }
10455 : 9258 : tree this_off = build_int_cst (TREE_TYPE (alias_off),
10456 : 9258 : group_el * elsz + cst_offset);
10457 : 9258 : tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
10458 : 9258 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10459 : 9258 : new_temp = make_ssa_name (ltype);
10460 : 9258 : new_stmt = gimple_build_assign (new_temp, data_ref);
10461 : 9258 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10462 : 9258 : if (nloads > 1)
10463 : 7870 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
10464 : :
10465 : 9258 : group_el += lnel;
10466 : 9258 : if (group_el == group_size)
10467 : : {
10468 : 8952 : n_groups++;
10469 : : /* When doing SLP make sure to not load elements from
10470 : : the next vector iteration, those will not be accessed
10471 : : so just use the last element again. See PR107451. */
10472 : 8952 : if (known_lt (n_groups, vf))
10473 : : {
10474 : 5744 : tree newoff = copy_ssa_name (running_off);
10475 : 5744 : gimple *incr
10476 : 5744 : = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
10477 : : running_off, stride_step);
10478 : 5744 : vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
10479 : 5744 : running_off = newoff;
10480 : : }
10481 : : group_el = 0;
10482 : : }
10483 : : }
10484 : :
10485 : 33265 : if (nloads > 1)
10486 : : {
10487 : 30325 : if (costing_p)
10488 : 27627 : inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
10489 : : slp_node, 0, vect_body);
10490 : : else
10491 : : {
10492 : 2698 : tree vec_inv = build_constructor (lvectype, v);
10493 : 2698 : new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
10494 : : lvectype, gsi);
10495 : 2698 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
10496 : 2698 : if (lvectype != vectype)
10497 : : {
10498 : 221 : new_stmt
10499 : 221 : = gimple_build_assign (make_ssa_name (vectype),
10500 : : VIEW_CONVERT_EXPR,
10501 : : build1 (VIEW_CONVERT_EXPR,
10502 : : vectype, new_temp));
10503 : 221 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10504 : : gsi);
10505 : : }
10506 : : }
10507 : : }
10508 : 2940 : else if (!costing_p && ltype != vectype)
10509 : : {
10510 : 1371 : new_stmt = gimple_build_assign (make_ssa_name (vectype),
10511 : : VIEW_CONVERT_EXPR,
10512 : : build1 (VIEW_CONVERT_EXPR,
10513 : : vectype, new_temp));
10514 : 1371 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
10515 : : gsi);
10516 : : }
10517 : :
10518 : 33265 : if (!costing_p)
10519 : : {
10520 : 4086 : if (slp_perm)
10521 : 1102 : dr_chain.quick_push (gimple_assign_lhs (new_stmt));
10522 : : else
10523 : 2984 : slp_node->push_vec_def (new_stmt);
10524 : : }
10525 : : }
10526 : 25160 : if (slp_perm)
10527 : : {
10528 : 2475 : unsigned n_perms;
10529 : 2475 : if (costing_p)
10530 : : {
10531 : 1838 : unsigned n_loads;
10532 : 1838 : vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL, vf,
10533 : : true, &n_perms, &n_loads);
10534 : 1838 : inside_cost += record_stmt_cost (cost_vec, n_perms, vec_perm,
10535 : : slp_node, 0, vect_body);
10536 : : }
10537 : : else
10538 : 637 : vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
10539 : : false, &n_perms);
10540 : : }
10541 : :
10542 : 25160 : if (costing_p)
10543 : : {
10544 : 21974 : if (n_adjacent_loads > 0)
10545 : 1523 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10546 : : alignment_support_scheme, misalignment, false,
10547 : : &inside_cost, nullptr, cost_vec, cost_vec,
10548 : : true);
10549 : 21974 : if (dump_enabled_p ())
10550 : 469 : dump_printf_loc (MSG_NOTE, vect_location,
10551 : : "vect_model_load_cost: inside_cost = %u, "
10552 : : "prologue_cost = 0 .\n",
10553 : : inside_cost);
10554 : : }
10555 : :
10556 : 25160 : return true;
10557 : 25160 : }
10558 : :
10559 : 488521 : if (memory_access_type == VMAT_GATHER_SCATTER)
10560 : : grouped_load = false;
10561 : :
10562 : 485896 : if (grouped_load
10563 : 488521 : || SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10564 : : {
10565 : 258092 : if (grouped_load)
10566 : : {
10567 : 257866 : first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10568 : 257866 : group_size = DR_GROUP_SIZE (first_stmt_info);
10569 : : }
10570 : : else
10571 : : {
10572 : : first_stmt_info = stmt_info;
10573 : : group_size = 1;
10574 : : }
10575 : : /* For SLP vectorization we directly vectorize a subchain
10576 : : without permutation. */
10577 : 258092 : if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
10578 : 213711 : first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10579 : : /* For BB vectorization always use the first stmt to base
10580 : : the data ref pointer on. */
10581 : 258092 : if (bb_vinfo)
10582 : 215447 : first_stmt_info_for_drptr
10583 : 215447 : = vect_find_first_scalar_stmt_in_slp (slp_node);
10584 : :
10585 : 258092 : first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10586 : 258092 : group_gap_adj = 0;
10587 : :
10588 : : /* VEC_NUM is the number of vect stmts to be created for this group. */
10589 : 258092 : grouped_load = false;
10590 : : /* If an SLP permutation is from N elements to N elements,
10591 : : and if one vector holds a whole number of N, we can load
10592 : : the inputs to the permutation in the same way as an
10593 : : unpermuted sequence. In other cases we need to load the
10594 : : whole group, not only the number of vector stmts the
10595 : : permutation result fits in. */
10596 : 258092 : unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
10597 : 258092 : if (nested_in_vect_loop)
10598 : : /* We do not support grouped accesses in a nested loop,
10599 : : instead the access is contiguous but it might be
10600 : : permuted. No gap adjustment is needed though. */
10601 : 2 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
10602 : 258090 : else if (slp_perm
10603 : 258090 : && (group_size != scalar_lanes
10604 : 8246 : || !multiple_p (nunits, group_size)))
10605 : : {
10606 : : /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
10607 : : variable VF; see vect_transform_slp_perm_load. */
10608 : 38185 : unsigned int const_vf = vf.to_constant ();
10609 : 38185 : unsigned int const_nunits = nunits.to_constant ();
10610 : 38185 : vec_num = CEIL (group_size * const_vf, const_nunits);
10611 : 38185 : group_gap_adj = vf * group_size - nunits * vec_num;
10612 : : }
10613 : : else
10614 : : {
10615 : 219905 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
10616 : 219905 : group_gap_adj = group_size - scalar_lanes;
10617 : : }
10618 : :
10619 : 258092 : ref_type = get_group_alias_ptr_type (first_stmt_info);
10620 : : }
10621 : : else
10622 : : {
10623 : 230429 : first_stmt_info = stmt_info;
10624 : 230429 : first_dr_info = dr_info;
10625 : 230429 : group_size = vec_num = 1;
10626 : 230429 : group_gap_adj = 0;
10627 : 230429 : ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
10628 : 230429 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
10629 : : }
10630 : :
10631 : 488521 : gcc_assert (alignment_support_scheme);
10632 : 488521 : vec_loop_masks *loop_masks
10633 : 273074 : = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
10634 : 488521 : ? &LOOP_VINFO_MASKS (loop_vinfo)
10635 : 17 : : NULL);
10636 : 17 : vec_loop_lens *loop_lens
10637 : 273074 : = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
10638 : : ? &LOOP_VINFO_LENS (loop_vinfo)
10639 : 0 : : NULL);
10640 : :
10641 : : /* The vect_transform_stmt and vect_analyze_stmt will go here but there
10642 : : are some difference here. We cannot enable both the lens and masks
10643 : : during transform but it is allowed during analysis.
10644 : : Shouldn't go with length-based approach if fully masked. */
10645 : 488521 : if (cost_vec == NULL)
10646 : : /* The cost_vec is NULL during transfrom. */
10647 : 152662 : gcc_assert ((!loop_lens || !loop_masks));
10648 : :
10649 : : /* Targets with store-lane instructions must not require explicit
10650 : : realignment. vect_supportable_dr_alignment always returns either
10651 : : dr_aligned or dr_unaligned_supported for masked operations. */
10652 : 488521 : gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
10653 : : && !mask
10654 : : && !loop_masks)
10655 : : || alignment_support_scheme == dr_aligned
10656 : : || alignment_support_scheme == dr_unaligned_supported);
10657 : :
10658 : : /* In case the vectorization factor (VF) is bigger than the number
10659 : : of elements that we can fit in a vectype (nunits), we have to generate
10660 : : more than one vector stmt - i.e - we need to "unroll" the
10661 : : vector stmt by a factor VF/nunits. In doing so, we record a pointer
10662 : : from one copy of the vector stmt to the next, in the field
10663 : : STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
10664 : : stages to find the correct vector defs to be used when vectorizing
10665 : : stmts that use the defs of the current stmt. The example below
10666 : : illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
10667 : : need to create 4 vectorized stmts):
10668 : :
10669 : : before vectorization:
10670 : : RELATED_STMT VEC_STMT
10671 : : S1: x = memref - -
10672 : : S2: z = x + 1 - -
10673 : :
10674 : : step 1: vectorize stmt S1:
10675 : : We first create the vector stmt VS1_0, and, as usual, record a
10676 : : pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
10677 : : Next, we create the vector stmt VS1_1, and record a pointer to
10678 : : it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
10679 : : Similarly, for VS1_2 and VS1_3. This is the resulting chain of
10680 : : stmts and pointers:
10681 : : RELATED_STMT VEC_STMT
10682 : : VS1_0: vx0 = memref0 VS1_1 -
10683 : : VS1_1: vx1 = memref1 VS1_2 -
10684 : : VS1_2: vx2 = memref2 VS1_3 -
10685 : : VS1_3: vx3 = memref3 - -
10686 : : S1: x = load - VS1_0
10687 : : S2: z = x + 1 - -
10688 : : */
10689 : :
10690 : : /* In case of interleaving (non-unit grouped access):
10691 : :
10692 : : S1: x2 = &base + 2
10693 : : S2: x0 = &base
10694 : : S3: x1 = &base + 1
10695 : : S4: x3 = &base + 3
10696 : :
10697 : : Vectorized loads are created in the order of memory accesses
10698 : : starting from the access of the first stmt of the chain:
10699 : :
10700 : : VS1: vx0 = &base
10701 : : VS2: vx1 = &base + vec_size*1
10702 : : VS3: vx3 = &base + vec_size*2
10703 : : VS4: vx4 = &base + vec_size*3
10704 : :
10705 : : Then permutation statements are generated:
10706 : :
10707 : : VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
10708 : : VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
10709 : : ...
10710 : :
10711 : : And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
10712 : : (the order of the data-refs in the output of vect_permute_load_chain
10713 : : corresponds to the order of scalar stmts in the interleaving chain - see
10714 : : the documentation of vect_permute_load_chain()).
10715 : : The generation of permutation stmts and recording them in
10716 : : STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
10717 : :
10718 : : In case of both multiple types and interleaving, the vector loads and
10719 : : permutation stmts above are created for every copy. The result vector
10720 : : stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
10721 : : corresponding STMT_VINFO_RELATED_STMT for the next copies. */
10722 : :
10723 : : /* If the data reference is aligned (dr_aligned) or potentially unaligned
10724 : : on a target that supports unaligned accesses (dr_unaligned_supported)
10725 : : we generate the following code:
10726 : : p = initial_addr;
10727 : : indx = 0;
10728 : : loop {
10729 : : p = p + indx * vectype_size;
10730 : : vec_dest = *(p);
10731 : : indx = indx + 1;
10732 : : }
10733 : :
10734 : : Otherwise, the data reference is potentially unaligned on a target that
10735 : : does not support unaligned accesses (dr_explicit_realign_optimized) -
10736 : : then generate the following code, in which the data in each iteration is
10737 : : obtained by two vector loads, one from the previous iteration, and one
10738 : : from the current iteration:
10739 : : p1 = initial_addr;
10740 : : msq_init = *(floor(p1))
10741 : : p2 = initial_addr + VS - 1;
10742 : : realignment_token = call target_builtin;
10743 : : indx = 0;
10744 : : loop {
10745 : : p2 = p2 + indx * vectype_size
10746 : : lsq = *(floor(p2))
10747 : : vec_dest = realign_load (msq, lsq, realignment_token)
10748 : : indx = indx + 1;
10749 : : msq = lsq;
10750 : : } */
10751 : :
10752 : : /* If the misalignment remains the same throughout the execution of the
10753 : : loop, we can create the init_addr and permutation mask at the loop
10754 : : preheader. Otherwise, it needs to be created inside the loop.
10755 : : This can only occur when vectorizing memory accesses in the inner-loop
10756 : : nested within an outer-loop that is being vectorized. */
10757 : :
10758 : 488521 : if (nested_in_vect_loop
10759 : 488521 : && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
10760 : 1100 : GET_MODE_SIZE (TYPE_MODE (vectype))))
10761 : : {
10762 : 151 : gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
10763 : : compute_in_loop = true;
10764 : : }
10765 : :
10766 : 488521 : bool diff_first_stmt_info
10767 : 488521 : = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
10768 : :
10769 : 488521 : tree offset = NULL_TREE;
10770 : 488521 : if ((alignment_support_scheme == dr_explicit_realign_optimized
10771 : 488521 : || alignment_support_scheme == dr_explicit_realign)
10772 : 0 : && !compute_in_loop)
10773 : : {
10774 : : /* If we have different first_stmt_info, we can't set up realignment
10775 : : here, since we can't guarantee first_stmt_info DR has been
10776 : : initialized yet, use first_stmt_info_for_drptr DR by bumping the
10777 : : distance from first_stmt_info DR instead as below. */
10778 : 0 : if (!costing_p)
10779 : : {
10780 : 0 : if (!diff_first_stmt_info)
10781 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, gsi,
10782 : : &realignment_token,
10783 : : alignment_support_scheme, NULL_TREE,
10784 : : &at_loop);
10785 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
10786 : : {
10787 : 0 : phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
10788 : 0 : offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
10789 : : size_one_node);
10790 : 0 : gcc_assert (!first_stmt_info_for_drptr);
10791 : : }
10792 : : }
10793 : : }
10794 : : else
10795 : 488521 : at_loop = loop;
10796 : :
10797 : 488521 : if (!known_eq (poffset, 0))
10798 : 4293 : offset = (offset
10799 : 4293 : ? size_binop (PLUS_EXPR, offset, size_int (poffset))
10800 : 4293 : : size_int (poffset));
10801 : :
10802 : 488521 : tree bump;
10803 : 488521 : tree vec_offset = NULL_TREE;
10804 : 488521 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
10805 : : {
10806 : 2625 : aggr_type = NULL_TREE;
10807 : 2625 : bump = NULL_TREE;
10808 : : }
10809 : 485896 : else if (memory_access_type == VMAT_GATHER_SCATTER)
10810 : : {
10811 : 0 : aggr_type = elem_type;
10812 : 0 : if (!costing_p)
10813 : 0 : vect_get_strided_load_store_ops (stmt_info, loop_vinfo, gsi, &gs_info,
10814 : : &bump, &vec_offset, loop_lens);
10815 : : }
10816 : : else
10817 : : {
10818 : 485896 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10819 : 0 : aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
10820 : : else
10821 : : aggr_type = vectype;
10822 : 485896 : if (!costing_p)
10823 : 151920 : bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
10824 : : memory_access_type, loop_lens);
10825 : : }
10826 : :
10827 : 488521 : auto_vec<tree> vec_offsets;
10828 : 488521 : auto_vec<tree> vec_masks;
10829 : 488521 : if (mask && !costing_p)
10830 : 574 : vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
10831 : : &vec_masks);
10832 : :
10833 : 488521 : tree vec_mask = NULL_TREE;
10834 : 488521 : tree vec_els = NULL_TREE;
10835 : 488521 : if (memory_access_type == VMAT_LOAD_STORE_LANES)
10836 : : {
10837 : 0 : gcc_assert (alignment_support_scheme == dr_aligned
10838 : : || alignment_support_scheme == dr_unaligned_supported);
10839 : :
10840 : 0 : unsigned int inside_cost = 0, prologue_cost = 0;
10841 : : /* For costing some adjacent vector loads, we'd like to cost with
10842 : : the total number of them once instead of cost each one by one. */
10843 : 0 : unsigned int n_adjacent_loads = 0;
10844 : 0 : int ncopies = slp_node->vec_stmts_size / group_size;
10845 : 0 : for (j = 0; j < ncopies; j++)
10846 : : {
10847 : 0 : if (costing_p)
10848 : : {
10849 : : /* An IFN_LOAD_LANES will load all its vector results,
10850 : : regardless of which ones we actually need. Account
10851 : : for the cost of unused results. */
10852 : 0 : if (first_stmt_info == stmt_info)
10853 : : {
10854 : 0 : unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
10855 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
10856 : 0 : do
10857 : : {
10858 : 0 : gaps -= 1;
10859 : 0 : next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
10860 : : }
10861 : 0 : while (next_stmt_info);
10862 : 0 : if (gaps)
10863 : : {
10864 : 0 : if (dump_enabled_p ())
10865 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10866 : : "vect_model_load_cost: %d "
10867 : : "unused vectors.\n",
10868 : : gaps);
10869 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
10870 : : alignment_support_scheme,
10871 : : misalignment, false, &inside_cost,
10872 : : &prologue_cost, cost_vec, cost_vec,
10873 : : true);
10874 : : }
10875 : : }
10876 : 0 : n_adjacent_loads++;
10877 : 0 : continue;
10878 : 0 : }
10879 : :
10880 : : /* 1. Create the vector or array pointer update chain. */
10881 : 0 : if (j == 0)
10882 : 0 : dataref_ptr
10883 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
10884 : : at_loop, offset, &dummy, gsi,
10885 : : &ptr_incr, false, bump);
10886 : : else
10887 : : {
10888 : 0 : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
10889 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
10890 : : stmt_info, bump);
10891 : : }
10892 : 0 : if (mask)
10893 : 0 : vec_mask = vec_masks[j];
10894 : :
10895 : 0 : tree vec_array = create_vector_array (vectype, group_size);
10896 : :
10897 : 0 : tree final_mask = NULL_TREE;
10898 : 0 : tree final_len = NULL_TREE;
10899 : 0 : tree bias = NULL_TREE;
10900 : 0 : if (loop_masks)
10901 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
10902 : : ncopies, vectype, j);
10903 : 0 : if (vec_mask)
10904 : 0 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
10905 : : vec_mask, gsi);
10906 : :
10907 : 0 : if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
10908 : : {
10909 : 0 : if (loop_lens)
10910 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10911 : : ncopies, vectype, j, 1);
10912 : : else
10913 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
10914 : 0 : signed char biasval
10915 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10916 : 0 : bias = build_int_cst (intQI_type_node, biasval);
10917 : 0 : if (!final_mask)
10918 : : {
10919 : 0 : mask_vectype = truth_type_for (vectype);
10920 : 0 : final_mask = build_minus_one_cst (mask_vectype);
10921 : : }
10922 : : }
10923 : :
10924 : 0 : if (final_mask)
10925 : : {
10926 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
10927 : 0 : if (type_mode_padding_p
10928 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
10929 : 0 : need_zeroing = true;
10930 : : }
10931 : :
10932 : 0 : gcall *call;
10933 : 0 : if (final_len && final_mask)
10934 : : {
10935 : : /* Emit:
10936 : : VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10937 : : VEC_MASK, LEN, BIAS). */
10938 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10939 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10940 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
10941 : : dataref_ptr, alias_ptr,
10942 : : final_mask, vec_els,
10943 : : final_len, bias);
10944 : : }
10945 : 0 : else if (final_mask)
10946 : : {
10947 : : /* Emit:
10948 : : VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
10949 : : VEC_MASK). */
10950 : 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
10951 : 0 : tree alias_ptr = build_int_cst (ref_type, align);
10952 : 0 : call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
10953 : : dataref_ptr, alias_ptr,
10954 : : final_mask, vec_els);
10955 : : }
10956 : : else
10957 : : {
10958 : : /* Emit:
10959 : : VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
10960 : 0 : data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
10961 : 0 : call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
10962 : : }
10963 : 0 : gimple_call_set_lhs (call, vec_array);
10964 : 0 : gimple_call_set_nothrow (call, true);
10965 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10966 : :
10967 : : /* Extract each vector into an SSA_NAME. */
10968 : 0 : for (unsigned i = 0; i < group_size; i++)
10969 : : {
10970 : 0 : new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
10971 : : vec_array, i, need_zeroing,
10972 : : final_mask);
10973 : 0 : slp_node->push_vec_def (new_temp);
10974 : : }
10975 : :
10976 : : /* Record that VEC_ARRAY is now dead. */
10977 : 0 : vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
10978 : : }
10979 : :
10980 : 0 : if (costing_p)
10981 : : {
10982 : 0 : if (n_adjacent_loads > 0)
10983 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
10984 : : alignment_support_scheme, misalignment, false,
10985 : : &inside_cost, &prologue_cost, cost_vec,
10986 : : cost_vec, true);
10987 : 0 : if (dump_enabled_p ())
10988 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
10989 : : "vect_model_load_cost: inside_cost = %u, "
10990 : : "prologue_cost = %u .\n",
10991 : : inside_cost, prologue_cost);
10992 : : }
10993 : :
10994 : 0 : return true;
10995 : : }
10996 : :
10997 : 488521 : if (memory_access_type == VMAT_GATHER_SCATTER)
10998 : : {
10999 : 2625 : gcc_assert (alignment_support_scheme == dr_aligned
11000 : : || alignment_support_scheme == dr_unaligned_supported);
11001 : 2625 : gcc_assert (!grouped_load && !slp_perm);
11002 : :
11003 : 2625 : unsigned int inside_cost = 0, prologue_cost = 0;
11004 : :
11005 : : /* 1. Create the vector or array pointer update chain. */
11006 : 2625 : if (!costing_p)
11007 : : {
11008 : 742 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11009 : 742 : vect_get_gather_scatter_ops (loop_vinfo, loop, stmt_info,
11010 : : slp_node, &gs_info, &dataref_ptr,
11011 : : &vec_offsets);
11012 : : else
11013 : 0 : dataref_ptr
11014 : 0 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11015 : : at_loop, offset, &dummy, gsi,
11016 : : &ptr_incr, false, bump);
11017 : : }
11018 : :
11019 : 5917 : gimple *new_stmt = NULL;
11020 : 5917 : for (i = 0; i < vec_num; i++)
11021 : : {
11022 : 3292 : tree final_mask = NULL_TREE;
11023 : 3292 : tree final_len = NULL_TREE;
11024 : 3292 : tree bias = NULL_TREE;
11025 : 3292 : if (!costing_p)
11026 : : {
11027 : 954 : if (mask)
11028 : 153 : vec_mask = vec_masks[i];
11029 : 954 : if (loop_masks)
11030 : 0 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11031 : : vec_num, vectype, i);
11032 : 954 : if (vec_mask)
11033 : 153 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11034 : : final_mask, vec_mask, gsi);
11035 : :
11036 : 954 : if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11037 : 0 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11038 : : gsi, stmt_info, bump);
11039 : : }
11040 : :
11041 : : /* 2. Create the vector-load in the loop. */
11042 : 3292 : unsigned HOST_WIDE_INT align;
11043 : 3292 : if (gs_info.ifn != IFN_LAST)
11044 : : {
11045 : 0 : if (costing_p)
11046 : : {
11047 : 0 : unsigned int cnunits = vect_nunits_for_cost (vectype);
11048 : 0 : inside_cost
11049 : 0 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
11050 : : slp_node, 0, vect_body);
11051 : 0 : continue;
11052 : 0 : }
11053 : 0 : if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11054 : 0 : vec_offset = vec_offsets[i];
11055 : 0 : tree zero = build_zero_cst (vectype);
11056 : 0 : tree scale = size_int (gs_info.scale);
11057 : :
11058 : 0 : if (gs_info.ifn == IFN_MASK_LEN_GATHER_LOAD)
11059 : : {
11060 : 0 : if (loop_lens)
11061 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11062 : : vec_num, vectype, i, 1);
11063 : : else
11064 : 0 : final_len = build_int_cst (sizetype,
11065 : 0 : TYPE_VECTOR_SUBPARTS (vectype));
11066 : 0 : signed char biasval
11067 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11068 : 0 : bias = build_int_cst (intQI_type_node, biasval);
11069 : 0 : if (!final_mask)
11070 : : {
11071 : 0 : mask_vectype = truth_type_for (vectype);
11072 : 0 : final_mask = build_minus_one_cst (mask_vectype);
11073 : : }
11074 : : }
11075 : :
11076 : 0 : if (final_mask)
11077 : : {
11078 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11079 : 0 : if (type_mode_padding_p
11080 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11081 : 0 : need_zeroing = true;
11082 : : }
11083 : :
11084 : 0 : gcall *call;
11085 : 0 : if (final_len && final_mask)
11086 : : {
11087 : 0 : if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
11088 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_GATHER_LOAD,
11089 : : 8, dataref_ptr,
11090 : : vec_offset, scale, zero,
11091 : : final_mask, vec_els,
11092 : : final_len, bias);
11093 : : else
11094 : : /* Non-vector offset indicates that prefer to take
11095 : : MASK_LEN_STRIDED_LOAD instead of the
11096 : : MASK_LEN_GATHER_LOAD with direct stride arg. */
11097 : 0 : call = gimple_build_call_internal
11098 : 0 : (IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr,
11099 : : vec_offset, zero, final_mask, vec_els, final_len,
11100 : : bias);
11101 : : }
11102 : 0 : else if (final_mask)
11103 : 0 : call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
11104 : : 6, dataref_ptr,
11105 : : vec_offset, scale,
11106 : : zero, final_mask, vec_els);
11107 : : else
11108 : 0 : call = gimple_build_call_internal (IFN_GATHER_LOAD, 4,
11109 : : dataref_ptr, vec_offset,
11110 : : scale, zero);
11111 : 0 : gimple_call_set_nothrow (call, true);
11112 : 0 : new_stmt = call;
11113 : 0 : data_ref = NULL_TREE;
11114 : : }
11115 : 3292 : else if (gs_info.decl)
11116 : : {
11117 : : /* The builtin decls path for gather is legacy, x86 only. */
11118 : 566 : gcc_assert (!final_len && nunits.is_constant ());
11119 : 566 : if (costing_p)
11120 : : {
11121 : 285 : unsigned int cnunits = vect_nunits_for_cost (vectype);
11122 : 285 : inside_cost
11123 : 285 : = record_stmt_cost (cost_vec, cnunits, scalar_load,
11124 : : slp_node, 0, vect_body);
11125 : 285 : continue;
11126 : 285 : }
11127 : 281 : poly_uint64 offset_nunits
11128 : 281 : = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
11129 : 281 : if (known_eq (nunits, offset_nunits))
11130 : : {
11131 : 132 : new_stmt = vect_build_one_gather_load_call
11132 : 132 : (vinfo, stmt_info, gsi, &gs_info,
11133 : 132 : dataref_ptr, vec_offsets[i], final_mask);
11134 : 132 : data_ref = NULL_TREE;
11135 : : }
11136 : 149 : else if (known_eq (nunits, offset_nunits * 2))
11137 : : {
11138 : : /* We have a offset vector with half the number of
11139 : : lanes but the builtins will produce full vectype
11140 : : data with just the lower lanes filled. */
11141 : 63 : new_stmt = vect_build_one_gather_load_call
11142 : 126 : (vinfo, stmt_info, gsi, &gs_info,
11143 : 63 : dataref_ptr, vec_offsets[2 * i], final_mask);
11144 : 63 : tree low = make_ssa_name (vectype);
11145 : 63 : gimple_set_lhs (new_stmt, low);
11146 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11147 : :
11148 : : /* now put upper half of final_mask in final_mask low. */
11149 : 63 : if (final_mask
11150 : 63 : && !SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (final_mask))))
11151 : : {
11152 : 10 : int count = nunits.to_constant ();
11153 : 10 : vec_perm_builder sel (count, count, 1);
11154 : 10 : sel.quick_grow (count);
11155 : 78 : for (int i = 0; i < count; ++i)
11156 : 68 : sel[i] = i | (count / 2);
11157 : 10 : vec_perm_indices indices (sel, 2, count);
11158 : 10 : tree perm_mask = vect_gen_perm_mask_checked
11159 : 10 : (TREE_TYPE (final_mask), indices);
11160 : 10 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11161 : : final_mask, final_mask,
11162 : : perm_mask);
11163 : 10 : final_mask = make_ssa_name (TREE_TYPE (final_mask));
11164 : 10 : gimple_set_lhs (new_stmt, final_mask);
11165 : 10 : vect_finish_stmt_generation (vinfo, stmt_info,
11166 : : new_stmt, gsi);
11167 : 10 : }
11168 : 53 : else if (final_mask)
11169 : : {
11170 : 25 : new_stmt = gimple_build_assign (NULL_TREE,
11171 : : VEC_UNPACK_HI_EXPR,
11172 : : final_mask);
11173 : 25 : final_mask = make_ssa_name
11174 : 25 : (truth_type_for (gs_info.offset_vectype));
11175 : 25 : gimple_set_lhs (new_stmt, final_mask);
11176 : 25 : vect_finish_stmt_generation (vinfo, stmt_info,
11177 : : new_stmt, gsi);
11178 : : }
11179 : :
11180 : 63 : new_stmt = vect_build_one_gather_load_call
11181 : 126 : (vinfo, stmt_info, gsi, &gs_info, dataref_ptr,
11182 : 63 : vec_offsets[2 * i + 1], final_mask);
11183 : 63 : tree high = make_ssa_name (vectype);
11184 : 63 : gimple_set_lhs (new_stmt, high);
11185 : 63 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11186 : :
11187 : : /* compose low + high. */
11188 : 63 : int count = nunits.to_constant ();
11189 : 63 : vec_perm_builder sel (count, count, 1);
11190 : 63 : sel.quick_grow (count);
11191 : 655 : for (int i = 0; i < count; ++i)
11192 : 592 : sel[i] = i < count / 2 ? i : i + count / 2;
11193 : 63 : vec_perm_indices indices (sel, 2, count);
11194 : 63 : tree perm_mask
11195 : 63 : = vect_gen_perm_mask_checked (vectype, indices);
11196 : 63 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11197 : : low, high, perm_mask);
11198 : 63 : data_ref = NULL_TREE;
11199 : 63 : }
11200 : 86 : else if (known_eq (nunits * 2, offset_nunits))
11201 : : {
11202 : : /* We have a offset vector with double the number of
11203 : : lanes. Select the low/high part accordingly. */
11204 : 86 : vec_offset = vec_offsets[i / 2];
11205 : 86 : if (i & 1)
11206 : : {
11207 : 43 : int count = offset_nunits.to_constant ();
11208 : 43 : vec_perm_builder sel (count, count, 1);
11209 : 43 : sel.quick_grow (count);
11210 : 463 : for (int i = 0; i < count; ++i)
11211 : 420 : sel[i] = i | (count / 2);
11212 : 43 : vec_perm_indices indices (sel, 2, count);
11213 : 43 : tree perm_mask = vect_gen_perm_mask_checked
11214 : 43 : (TREE_TYPE (vec_offset), indices);
11215 : 43 : new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
11216 : : vec_offset, vec_offset,
11217 : : perm_mask);
11218 : 43 : vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
11219 : 43 : gimple_set_lhs (new_stmt, vec_offset);
11220 : 43 : vect_finish_stmt_generation (vinfo, stmt_info,
11221 : : new_stmt, gsi);
11222 : 43 : }
11223 : 86 : new_stmt = vect_build_one_gather_load_call
11224 : 86 : (vinfo, stmt_info, gsi, &gs_info,
11225 : : dataref_ptr, vec_offset, final_mask);
11226 : 86 : data_ref = NULL_TREE;
11227 : : }
11228 : : else
11229 : 0 : gcc_unreachable ();
11230 : : }
11231 : : else
11232 : : {
11233 : : /* Emulated gather-scatter. */
11234 : 2726 : gcc_assert (!final_mask);
11235 : 2726 : unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
11236 : 2726 : if (costing_p)
11237 : : {
11238 : : /* For emulated gathers N offset vector element
11239 : : offset add is consumed by the load). */
11240 : 2053 : inside_cost = record_stmt_cost (cost_vec, const_nunits,
11241 : : vec_to_scalar,
11242 : : slp_node, 0, vect_body);
11243 : : /* N scalar loads plus gathering them into a
11244 : : vector. */
11245 : 2053 : inside_cost
11246 : 2053 : = record_stmt_cost (cost_vec, const_nunits, scalar_load,
11247 : : slp_node, 0, vect_body);
11248 : 2053 : inside_cost
11249 : 2053 : = record_stmt_cost (cost_vec, 1, vec_construct,
11250 : : slp_node, 0, vect_body);
11251 : 2053 : continue;
11252 : : }
11253 : 673 : unsigned HOST_WIDE_INT const_offset_nunits
11254 : 673 : = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype) .to_constant ();
11255 : 673 : vec<constructor_elt, va_gc> *ctor_elts;
11256 : 673 : vec_alloc (ctor_elts, const_nunits);
11257 : 673 : gimple_seq stmts = NULL;
11258 : : /* We support offset vectors with more elements
11259 : : than the data vector for now. */
11260 : 673 : unsigned HOST_WIDE_INT factor
11261 : : = const_offset_nunits / const_nunits;
11262 : 673 : vec_offset = vec_offsets[i / factor];
11263 : 673 : unsigned elt_offset = (i % factor) * const_nunits;
11264 : 673 : tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
11265 : 673 : tree scale = size_int (gs_info.scale);
11266 : 673 : align = get_object_alignment (DR_REF (first_dr_info->dr));
11267 : 673 : tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
11268 : 2807 : for (unsigned k = 0; k < const_nunits; ++k)
11269 : : {
11270 : 2134 : tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
11271 : : bitsize_int (k + elt_offset));
11272 : 6402 : tree idx = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
11273 : 2134 : vec_offset, TYPE_SIZE (idx_type),
11274 : : boff);
11275 : 2134 : idx = gimple_convert (&stmts, sizetype, idx);
11276 : 2134 : idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx, scale);
11277 : 2134 : tree ptr = gimple_build (&stmts, PLUS_EXPR,
11278 : 2134 : TREE_TYPE (dataref_ptr),
11279 : : dataref_ptr, idx);
11280 : 2134 : ptr = gimple_convert (&stmts, ptr_type_node, ptr);
11281 : 2134 : tree elt = make_ssa_name (TREE_TYPE (vectype));
11282 : 2134 : tree ref = build2 (MEM_REF, ltype, ptr,
11283 : : build_int_cst (ref_type, 0));
11284 : 2134 : new_stmt = gimple_build_assign (elt, ref);
11285 : 4268 : gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
11286 : 2134 : gimple_seq_add_stmt (&stmts, new_stmt);
11287 : 2134 : CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
11288 : : }
11289 : 673 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11290 : 673 : new_stmt = gimple_build_assign (NULL_TREE,
11291 : : build_constructor (vectype,
11292 : : ctor_elts));
11293 : 673 : data_ref = NULL_TREE;
11294 : : }
11295 : :
11296 : 954 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11297 : : /* DATA_REF is null if we've already built the statement. */
11298 : 954 : if (data_ref)
11299 : : {
11300 : : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11301 : : new_stmt = gimple_build_assign (vec_dest, data_ref);
11302 : : }
11303 : 1908 : new_temp = (need_zeroing
11304 : 954 : ? make_ssa_name (vectype)
11305 : 954 : : make_ssa_name (vec_dest, new_stmt));
11306 : 954 : gimple_set_lhs (new_stmt, new_temp);
11307 : 954 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11308 : :
11309 : : /* If we need to explicitly zero inactive elements emit a
11310 : : VEC_COND_EXPR that does so. */
11311 : 954 : if (need_zeroing)
11312 : : {
11313 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11314 : : vectype);
11315 : :
11316 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11317 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11318 : : final_mask, new_temp, vec_els);
11319 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11320 : 0 : new_temp = new_temp2;
11321 : : }
11322 : :
11323 : : /* Store vector loads in the corresponding SLP_NODE. */
11324 : 954 : slp_node->push_vec_def (new_stmt);
11325 : : }
11326 : :
11327 : 2625 : if (costing_p && dump_enabled_p ())
11328 : 215 : 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 : 2625 : return true;
11333 : : }
11334 : :
11335 : 485896 : poly_uint64 group_elt = 0;
11336 : 485896 : unsigned int inside_cost = 0, prologue_cost = 0;
11337 : : /* For costing some adjacent vector loads, we'd like to cost with
11338 : : the total number of them once instead of cost each one by one. */
11339 : 485896 : unsigned int n_adjacent_loads = 0;
11340 : :
11341 : : /* 1. Create the vector or array pointer update chain. */
11342 : 485896 : if (!costing_p)
11343 : : {
11344 : 151920 : bool simd_lane_access_p
11345 : 151920 : = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
11346 : 151920 : if (simd_lane_access_p
11347 : 1605 : && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
11348 : 1605 : && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
11349 : 1605 : && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
11350 : 1605 : && integer_zerop (DR_INIT (first_dr_info->dr))
11351 : 1605 : && alias_sets_conflict_p (get_alias_set (aggr_type),
11352 : 1605 : get_alias_set (TREE_TYPE (ref_type)))
11353 : 153525 : && (alignment_support_scheme == dr_aligned
11354 : 0 : || alignment_support_scheme == dr_unaligned_supported))
11355 : : {
11356 : 1605 : dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
11357 : 1605 : dataref_offset = build_int_cst (ref_type, 0);
11358 : : }
11359 : 150315 : else if (diff_first_stmt_info)
11360 : : {
11361 : 2470 : dataref_ptr
11362 : 2470 : = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
11363 : : aggr_type, at_loop, offset, &dummy,
11364 : : gsi, &ptr_incr, simd_lane_access_p,
11365 : : bump);
11366 : : /* Adjust the pointer by the difference to first_stmt. */
11367 : 2470 : data_reference_p ptrdr
11368 : : = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
11369 : 2470 : tree diff = fold_convert (sizetype,
11370 : : size_binop (MINUS_EXPR,
11371 : : DR_INIT (first_dr_info->dr),
11372 : : DR_INIT (ptrdr)));
11373 : 2470 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11374 : : stmt_info, diff);
11375 : 2470 : if (alignment_support_scheme == dr_explicit_realign)
11376 : : {
11377 : 0 : msq = vect_setup_realignment (vinfo,
11378 : : first_stmt_info_for_drptr, gsi,
11379 : : &realignment_token,
11380 : : alignment_support_scheme,
11381 : : dataref_ptr, &at_loop);
11382 : 0 : gcc_assert (!compute_in_loop);
11383 : : }
11384 : : }
11385 : : else
11386 : 147845 : dataref_ptr
11387 : 147845 : = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11388 : : at_loop,
11389 : : offset, &dummy, gsi, &ptr_incr,
11390 : : simd_lane_access_p, bump);
11391 : : }
11392 : : else if (!costing_p)
11393 : : {
11394 : : gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
11395 : : if (dataref_offset)
11396 : : dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
11397 : : else
11398 : : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11399 : : stmt_info, bump);
11400 : : }
11401 : :
11402 : 485896 : if (grouped_load || slp_perm)
11403 : 44381 : dr_chain.create (vec_num);
11404 : :
11405 : : gimple *new_stmt = NULL;
11406 : 1282726 : for (i = 0; i < vec_num; i++)
11407 : : {
11408 : 796830 : tree final_mask = NULL_TREE;
11409 : 796830 : tree final_len = NULL_TREE;
11410 : 796830 : tree bias = NULL_TREE;
11411 : :
11412 : 796830 : if (!costing_p)
11413 : : {
11414 : 201818 : if (mask)
11415 : 520 : vec_mask = vec_masks[i];
11416 : 201818 : if (loop_masks)
11417 : 20 : final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11418 : : vec_num, vectype, i);
11419 : 201818 : if (vec_mask)
11420 : 520 : final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11421 : : final_mask, vec_mask, gsi);
11422 : :
11423 : 201818 : if (i > 0)
11424 : 49898 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11425 : : gsi, stmt_info, bump);
11426 : : }
11427 : :
11428 : : /* 2. Create the vector-load in the loop. */
11429 : 796830 : switch (alignment_support_scheme)
11430 : : {
11431 : 796830 : case dr_aligned:
11432 : 796830 : case dr_unaligned_supported:
11433 : 796830 : {
11434 : 796830 : if (costing_p)
11435 : : break;
11436 : :
11437 : 201818 : unsigned int misalign;
11438 : 201818 : unsigned HOST_WIDE_INT align;
11439 : 201818 : align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
11440 : 201818 : if (alignment_support_scheme == dr_aligned)
11441 : : misalign = 0;
11442 : 120931 : else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
11443 : : {
11444 : 94858 : align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
11445 : 94858 : misalign = 0;
11446 : : }
11447 : : else
11448 : 26073 : misalign = misalignment;
11449 : 201818 : if (dataref_offset == NULL_TREE
11450 : 199725 : && TREE_CODE (dataref_ptr) == SSA_NAME)
11451 : 129731 : set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
11452 : : misalign);
11453 : 201818 : align = least_bit_hwi (misalign | align);
11454 : :
11455 : : /* Compute IFN when LOOP_LENS or final_mask valid. */
11456 : 201818 : machine_mode vmode = TYPE_MODE (vectype);
11457 : 201818 : machine_mode new_vmode = vmode;
11458 : 201818 : internal_fn partial_ifn = IFN_LAST;
11459 : 201818 : if (loop_lens)
11460 : : {
11461 : 0 : opt_machine_mode new_ovmode
11462 : 0 : = get_len_load_store_mode (vmode, true, &partial_ifn);
11463 : 0 : new_vmode = new_ovmode.require ();
11464 : 0 : unsigned factor
11465 : 0 : = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
11466 : 0 : final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11467 : : vec_num, vectype, i, factor);
11468 : : }
11469 : 201818 : else if (final_mask)
11470 : : {
11471 : 540 : if (!can_vec_mask_load_store_p (vmode,
11472 : 540 : TYPE_MODE
11473 : : (TREE_TYPE (final_mask)),
11474 : : true, &partial_ifn))
11475 : 0 : gcc_unreachable ();
11476 : : }
11477 : :
11478 : 201818 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11479 : : {
11480 : 0 : if (!final_len)
11481 : : {
11482 : : /* Pass VF value to 'len' argument of
11483 : : MASK_LEN_LOAD if LOOP_LENS is invalid. */
11484 : 0 : final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11485 : : }
11486 : 0 : if (!final_mask)
11487 : : {
11488 : : /* Pass all ones value to 'mask' argument of
11489 : : MASK_LEN_LOAD if final_mask is invalid. */
11490 : 0 : mask_vectype = truth_type_for (vectype);
11491 : 0 : final_mask = build_minus_one_cst (mask_vectype);
11492 : : }
11493 : : }
11494 : 201818 : if (final_len)
11495 : : {
11496 : 0 : signed char biasval
11497 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11498 : 0 : bias = build_int_cst (intQI_type_node, biasval);
11499 : : }
11500 : :
11501 : 201818 : tree vec_els;
11502 : :
11503 : 201818 : if (final_len)
11504 : : {
11505 : 0 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11506 : 0 : gcall *call;
11507 : 0 : if (partial_ifn == IFN_MASK_LEN_LOAD)
11508 : : {
11509 : 0 : vec_els = vect_get_mask_load_else (maskload_elsval,
11510 : : vectype);
11511 : 0 : if (type_mode_padding_p
11512 : 0 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11513 : 0 : need_zeroing = true;
11514 : 0 : call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
11515 : : 6, dataref_ptr, ptr,
11516 : : final_mask, vec_els,
11517 : : final_len, bias);
11518 : : }
11519 : : else
11520 : 0 : call = gimple_build_call_internal (IFN_LEN_LOAD, 4,
11521 : : dataref_ptr, ptr,
11522 : : final_len, bias);
11523 : 0 : gimple_call_set_nothrow (call, true);
11524 : 0 : new_stmt = call;
11525 : 0 : data_ref = NULL_TREE;
11526 : :
11527 : : /* Need conversion if it's wrapped with VnQI. */
11528 : 0 : if (vmode != new_vmode)
11529 : : {
11530 : 0 : tree new_vtype
11531 : 0 : = build_vector_type_for_mode (unsigned_intQI_type_node,
11532 : : new_vmode);
11533 : 0 : tree var = vect_get_new_ssa_name (new_vtype,
11534 : : vect_simple_var);
11535 : 0 : gimple_set_lhs (call, var);
11536 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, call,
11537 : : gsi);
11538 : 0 : tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
11539 : 0 : new_stmt = gimple_build_assign (vec_dest,
11540 : : VIEW_CONVERT_EXPR, op);
11541 : : }
11542 : : }
11543 : 201818 : else if (final_mask)
11544 : : {
11545 : 540 : tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
11546 : 540 : vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11547 : 540 : if (type_mode_padding_p
11548 : 540 : && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11549 : 0 : need_zeroing = true;
11550 : 540 : gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
11551 : : dataref_ptr, ptr,
11552 : : final_mask,
11553 : : vec_els);
11554 : 540 : gimple_call_set_nothrow (call, true);
11555 : 540 : new_stmt = call;
11556 : 540 : data_ref = NULL_TREE;
11557 : : }
11558 : : else
11559 : : {
11560 : 201278 : tree ltype = vectype;
11561 : 201278 : tree new_vtype = NULL_TREE;
11562 : 201278 : unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
11563 : 201278 : unsigned HOST_WIDE_INT dr_size
11564 : 201278 : = vect_get_scalar_dr_size (first_dr_info);
11565 : 201278 : poly_int64 off = 0;
11566 : 201278 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11567 : 1412 : off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
11568 : 201278 : unsigned int vect_align
11569 : 201278 : = vect_known_alignment_in_bytes (first_dr_info, vectype,
11570 : 201278 : off);
11571 : : /* Try to use a single smaller load when we are about
11572 : : to load excess elements compared to the unrolled
11573 : : scalar loop. */
11574 : 201278 : if (known_gt ((i + 1) * nunits,
11575 : : (group_size * vf - gap)))
11576 : : {
11577 : 2142 : poly_uint64 remain = ((group_size * vf - gap) - i * nunits);
11578 : 2142 : if (known_ge ((i + 1) * nunits - (group_size * vf - gap),
11579 : : nunits))
11580 : : /* DR will be unused. */
11581 : : ltype = NULL_TREE;
11582 : 1617 : else if (known_ge (vect_align,
11583 : : tree_to_poly_uint64
11584 : : (TYPE_SIZE_UNIT (vectype))))
11585 : : /* Aligned access to excess elements is OK if
11586 : : at least one element is accessed in the
11587 : : scalar loop. */
11588 : : ;
11589 : 1390 : else if (known_gt (vect_align,
11590 : : ((nunits - remain) * dr_size)))
11591 : : /* Aligned access to the gap area when there's
11592 : : at least one element in it is OK. */
11593 : : ;
11594 : : else
11595 : : {
11596 : : /* remain should now be > 0 and < nunits. */
11597 : 1387 : unsigned num;
11598 : 1387 : if (known_ne (remain, 0u)
11599 : 1387 : && constant_multiple_p (nunits, remain, &num))
11600 : : {
11601 : 1002 : tree ptype;
11602 : 1002 : new_vtype
11603 : 1002 : = vector_vector_composition_type (vectype, num,
11604 : : &ptype);
11605 : 1002 : if (new_vtype)
11606 : 1002 : ltype = ptype;
11607 : : }
11608 : : /* Else use multiple loads or a masked load? */
11609 : : /* For loop vectorization we now should have
11610 : : an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
11611 : : set. */
11612 : 1387 : if (loop_vinfo)
11613 : 1349 : gcc_assert (new_vtype
11614 : : || LOOP_VINFO_PEELING_FOR_GAPS
11615 : : (loop_vinfo));
11616 : : /* But still reduce the access size to the next
11617 : : required power-of-two so peeling a single
11618 : : scalar iteration is sufficient. */
11619 : 1387 : unsigned HOST_WIDE_INT cremain;
11620 : 1387 : if (remain.is_constant (&cremain))
11621 : : {
11622 : 1387 : unsigned HOST_WIDE_INT cpart_size
11623 : 1387 : = 1 << ceil_log2 (cremain);
11624 : 1387 : if (known_gt (nunits, cpart_size)
11625 : 1387 : && constant_multiple_p (nunits, cpart_size,
11626 : : &num))
11627 : : {
11628 : 1014 : tree ptype;
11629 : 1014 : new_vtype
11630 : 2028 : = vector_vector_composition_type (vectype,
11631 : 1014 : num,
11632 : : &ptype);
11633 : 1014 : if (new_vtype)
11634 : 1014 : ltype = ptype;
11635 : : }
11636 : : }
11637 : : }
11638 : : }
11639 : 201278 : tree offset = (dataref_offset ? dataref_offset
11640 : 199185 : : build_int_cst (ref_type, 0));
11641 : 201278 : if (!ltype)
11642 : : ;
11643 : 200753 : else if (ltype != vectype
11644 : 1014 : && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11645 : : {
11646 : 21 : poly_uint64 gap_offset
11647 : 21 : = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
11648 : 21 : - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
11649 : 21 : tree gapcst = build_int_cstu (ref_type, gap_offset);
11650 : 21 : offset = size_binop (PLUS_EXPR, offset, gapcst);
11651 : : }
11652 : 201278 : if (ltype)
11653 : : {
11654 : 200753 : data_ref = fold_build2 (MEM_REF, ltype,
11655 : : dataref_ptr, offset);
11656 : 200753 : if (alignment_support_scheme == dr_aligned
11657 : 200753 : && align >= TYPE_ALIGN_UNIT (ltype))
11658 : : ;
11659 : : else
11660 : 120287 : TREE_TYPE (data_ref)
11661 : 240574 : = build_aligned_type (TREE_TYPE (data_ref),
11662 : : align * BITS_PER_UNIT);
11663 : : }
11664 : 201278 : if (!ltype)
11665 : 525 : data_ref = build_constructor (vectype, NULL);
11666 : 200753 : else if (ltype != vectype)
11667 : : {
11668 : 1014 : vect_copy_ref_info (data_ref,
11669 : 1014 : DR_REF (first_dr_info->dr));
11670 : 1014 : tree tem = make_ssa_name (ltype);
11671 : 1014 : new_stmt = gimple_build_assign (tem, data_ref);
11672 : 1014 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11673 : : gsi);
11674 : 1014 : data_ref = NULL;
11675 : 1014 : vec<constructor_elt, va_gc> *v;
11676 : : /* We've computed 'num' above to statically two
11677 : : or via constant_multiple_p. */
11678 : 1014 : unsigned num
11679 : 1014 : = (exact_div (tree_to_poly_uint64
11680 : 1014 : (TYPE_SIZE_UNIT (vectype)),
11681 : : tree_to_poly_uint64
11682 : 1014 : (TYPE_SIZE_UNIT (ltype)))
11683 : 1014 : .to_constant ());
11684 : 1014 : vec_alloc (v, num);
11685 : 1014 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11686 : : {
11687 : 54 : while (--num)
11688 : 54 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11689 : : build_zero_cst (ltype));
11690 : 21 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11691 : : }
11692 : : else
11693 : : {
11694 : 993 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
11695 : 993 : while (--num)
11696 : 2316 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
11697 : : build_zero_cst (ltype));
11698 : : }
11699 : 1014 : gcc_assert (new_vtype != NULL_TREE);
11700 : 1014 : if (new_vtype == vectype)
11701 : 984 : new_stmt
11702 : 984 : = gimple_build_assign (vec_dest,
11703 : : build_constructor (vectype, v));
11704 : : else
11705 : : {
11706 : 30 : tree new_vname = make_ssa_name (new_vtype);
11707 : 30 : new_stmt
11708 : 30 : = gimple_build_assign (new_vname,
11709 : : build_constructor (new_vtype,
11710 : : v));
11711 : 30 : vect_finish_stmt_generation (vinfo, stmt_info,
11712 : : new_stmt, gsi);
11713 : 30 : new_stmt
11714 : 30 : = gimple_build_assign (vec_dest,
11715 : : build1 (VIEW_CONVERT_EXPR,
11716 : : vectype, new_vname));
11717 : : }
11718 : : }
11719 : : }
11720 : : break;
11721 : : }
11722 : 0 : case dr_explicit_realign:
11723 : 0 : {
11724 : 0 : if (costing_p)
11725 : : break;
11726 : 0 : tree ptr, bump;
11727 : :
11728 : 0 : tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11729 : :
11730 : 0 : if (compute_in_loop)
11731 : 0 : msq = vect_setup_realignment (vinfo, first_stmt_info, gsi,
11732 : : &realignment_token,
11733 : : dr_explicit_realign,
11734 : : dataref_ptr, NULL);
11735 : :
11736 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11737 : 0 : ptr = copy_ssa_name (dataref_ptr);
11738 : : else
11739 : 0 : ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
11740 : : // For explicit realign the target alignment should be
11741 : : // known at compile time.
11742 : 0 : unsigned HOST_WIDE_INT align
11743 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11744 : 0 : new_stmt = gimple_build_assign (ptr, BIT_AND_EXPR, dataref_ptr,
11745 : : build_int_cst
11746 : 0 : (TREE_TYPE (dataref_ptr),
11747 : 0 : -(HOST_WIDE_INT) align));
11748 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11749 : 0 : data_ref = build2 (MEM_REF, vectype,
11750 : : ptr, build_int_cst (ref_type, 0));
11751 : 0 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11752 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11753 : 0 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11754 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11755 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11756 : 0 : gimple_move_vops (new_stmt, stmt_info->stmt);
11757 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11758 : 0 : msq = new_temp;
11759 : :
11760 : 0 : bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
11761 : 0 : bump = size_binop (MINUS_EXPR, bump, size_one_node);
11762 : 0 : ptr = bump_vector_ptr (vinfo, dataref_ptr, NULL, gsi, stmt_info,
11763 : : bump);
11764 : 0 : new_stmt = gimple_build_assign (NULL_TREE, BIT_AND_EXPR, ptr,
11765 : 0 : build_int_cst (TREE_TYPE (ptr),
11766 : 0 : -(HOST_WIDE_INT) align));
11767 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
11768 : 0 : ptr = copy_ssa_name (ptr, new_stmt);
11769 : : else
11770 : 0 : ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
11771 : 0 : gimple_assign_set_lhs (new_stmt, ptr);
11772 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11773 : 0 : data_ref = build2 (MEM_REF, vectype,
11774 : : ptr, build_int_cst (ref_type, 0));
11775 : 0 : break;
11776 : : }
11777 : 0 : case dr_explicit_realign_optimized:
11778 : 0 : {
11779 : 0 : if (costing_p)
11780 : : break;
11781 : 0 : if (TREE_CODE (dataref_ptr) == SSA_NAME)
11782 : 0 : new_temp = copy_ssa_name (dataref_ptr);
11783 : : else
11784 : 0 : new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
11785 : : // We should only be doing this if we know the target
11786 : : // alignment at compile time.
11787 : 0 : unsigned HOST_WIDE_INT align
11788 : 0 : = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
11789 : 0 : new_stmt = gimple_build_assign (new_temp, BIT_AND_EXPR, dataref_ptr,
11790 : 0 : build_int_cst (TREE_TYPE (dataref_ptr),
11791 : 0 : -(HOST_WIDE_INT) align));
11792 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11793 : 0 : data_ref = build2 (MEM_REF, vectype, new_temp,
11794 : : build_int_cst (ref_type, 0));
11795 : 0 : break;
11796 : : }
11797 : 0 : default:
11798 : 0 : gcc_unreachable ();
11799 : : }
11800 : :
11801 : : /* One common place to cost the above vect load for different
11802 : : alignment support schemes. */
11803 : 796830 : if (costing_p)
11804 : : {
11805 : : /* For VMAT_CONTIGUOUS_PERMUTE if it's grouped load, we
11806 : : only need to take care of the first stmt, whose
11807 : : stmt_info is first_stmt_info, vec_num iterating on it
11808 : : will cover the cost for the remaining, it's consistent
11809 : : with transforming. For the prologue cost for realign,
11810 : : we only need to count it once for the whole group. */
11811 : 595012 : bool first_stmt_info_p = first_stmt_info == stmt_info;
11812 : 595012 : bool add_realign_cost = first_stmt_info_p && i == 0;
11813 : 595012 : if (memory_access_type == VMAT_CONTIGUOUS
11814 : 4109 : || memory_access_type == VMAT_CONTIGUOUS_REVERSE
11815 : 0 : || (memory_access_type == VMAT_CONTIGUOUS_PERMUTE
11816 : : && (!grouped_load || first_stmt_info_p)))
11817 : : {
11818 : : /* Leave realign cases alone to keep them simple. */
11819 : 595012 : if (alignment_support_scheme == dr_explicit_realign_optimized
11820 : 595012 : || alignment_support_scheme == dr_explicit_realign)
11821 : 0 : vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
11822 : : alignment_support_scheme, misalignment,
11823 : : add_realign_cost, &inside_cost,
11824 : : &prologue_cost, cost_vec, cost_vec,
11825 : : true);
11826 : : else
11827 : 595012 : n_adjacent_loads++;
11828 : : }
11829 : : }
11830 : : else
11831 : : {
11832 : 201818 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11833 : : /* DATA_REF is null if we've already built the statement. */
11834 : 201818 : if (data_ref)
11835 : : {
11836 : 200264 : vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11837 : 200264 : new_stmt = gimple_build_assign (vec_dest, data_ref);
11838 : : }
11839 : :
11840 : 403636 : new_temp = (need_zeroing
11841 : 201818 : ? make_ssa_name (vectype)
11842 : 201818 : : make_ssa_name (vec_dest, new_stmt));
11843 : 201818 : gimple_set_lhs (new_stmt, new_temp);
11844 : 201818 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11845 : :
11846 : : /* If we need to explicitly zero inactive elements emit a
11847 : : VEC_COND_EXPR that does so. */
11848 : 201818 : if (need_zeroing)
11849 : : {
11850 : 0 : vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11851 : : vectype);
11852 : :
11853 : 0 : tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11854 : 0 : new_stmt = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11855 : : final_mask, new_temp, vec_els);
11856 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11857 : : gsi);
11858 : 0 : new_temp = new_temp2;
11859 : : }
11860 : : }
11861 : :
11862 : : /* 3. Handle explicit realignment if necessary/supported.
11863 : : Create in loop:
11864 : : vec_dest = realign_load (msq, lsq, realignment_token) */
11865 : 796830 : if (!costing_p
11866 : 201818 : && (alignment_support_scheme == dr_explicit_realign_optimized
11867 : 201818 : || alignment_support_scheme == dr_explicit_realign))
11868 : : {
11869 : 0 : lsq = gimple_assign_lhs (new_stmt);
11870 : 0 : if (!realignment_token)
11871 : 0 : realignment_token = dataref_ptr;
11872 : 0 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
11873 : 0 : new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
11874 : : lsq, realignment_token);
11875 : 0 : new_temp = make_ssa_name (vec_dest, new_stmt);
11876 : 0 : gimple_assign_set_lhs (new_stmt, new_temp);
11877 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11878 : :
11879 : 0 : if (alignment_support_scheme == dr_explicit_realign_optimized)
11880 : : {
11881 : 0 : gcc_assert (phi);
11882 : 0 : if (i == vec_num - 1)
11883 : 0 : add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
11884 : : UNKNOWN_LOCATION);
11885 : : msq = lsq;
11886 : : }
11887 : : }
11888 : :
11889 : 796830 : if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
11890 : : {
11891 : 5521 : if (costing_p)
11892 : 4109 : inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
11893 : : slp_node, 0, vect_body);
11894 : : else
11895 : : {
11896 : 1412 : tree perm_mask = perm_mask_for_reverse (vectype);
11897 : 1412 : new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
11898 : : perm_mask, stmt_info, gsi);
11899 : 1412 : new_stmt = SSA_NAME_DEF_STMT (new_temp);
11900 : : }
11901 : : }
11902 : :
11903 : : /* Collect vector loads and later create their permutation in
11904 : : vect_transform_grouped_load (). */
11905 : 796830 : if (!costing_p && (grouped_load || slp_perm))
11906 : 29867 : dr_chain.quick_push (new_temp);
11907 : :
11908 : : /* Store vector loads in the corresponding SLP_NODE. */
11909 : 796830 : if (!costing_p && !slp_perm)
11910 : 171951 : slp_node->push_vec_def (new_stmt);
11911 : :
11912 : : /* With SLP permutation we load the gaps as well, without
11913 : : we need to skip the gaps after we manage to fully load
11914 : : all elements. group_gap_adj is DR_GROUP_SIZE here. */
11915 : 796830 : group_elt += nunits;
11916 : 796830 : if (!costing_p
11917 : 201818 : && maybe_ne (group_gap_adj, 0U)
11918 : 21382 : && !slp_perm
11919 : 817876 : && known_eq (group_elt, group_size - group_gap_adj))
11920 : : {
11921 : 16227 : poly_wide_int bump_val
11922 : 16227 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11923 : 16227 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11924 : 0 : bump_val = -bump_val;
11925 : 16227 : tree bump = wide_int_to_tree (sizetype, bump_val);
11926 : 16227 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11927 : : stmt_info, bump);
11928 : 16227 : group_elt = 0;
11929 : 16227 : }
11930 : : }
11931 : : /* Bump the vector pointer to account for a gap or for excess
11932 : : elements loaded for a permuted SLP load. */
11933 : 485896 : if (!costing_p
11934 : 151920 : && maybe_ne (group_gap_adj, 0U)
11935 : 502290 : && slp_perm)
11936 : : {
11937 : 167 : poly_wide_int bump_val
11938 : 167 : = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
11939 : 167 : if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
11940 : 9 : bump_val = -bump_val;
11941 : 167 : tree bump = wide_int_to_tree (sizetype, bump_val);
11942 : 167 : dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11943 : : stmt_info, bump);
11944 : 167 : }
11945 : :
11946 : 485896 : if (slp_perm)
11947 : : {
11948 : 44381 : unsigned n_perms;
11949 : : /* For SLP we know we've seen all possible uses of dr_chain so
11950 : : direct vect_transform_slp_perm_load to DCE the unused parts.
11951 : : ??? This is a hack to prevent compile-time issues as seen
11952 : : in PR101120 and friends. */
11953 : 44381 : if (costing_p)
11954 : : {
11955 : 30731 : vect_transform_slp_perm_load (vinfo, slp_node, vNULL, nullptr, vf,
11956 : : true, &n_perms, nullptr);
11957 : 30731 : inside_cost = record_stmt_cost (cost_vec, n_perms, vec_perm,
11958 : : slp_node, 0, vect_body);
11959 : : }
11960 : : else
11961 : : {
11962 : 13650 : bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
11963 : : gsi, vf, false, &n_perms,
11964 : : nullptr, true);
11965 : 13650 : gcc_assert (ok);
11966 : : }
11967 : 44381 : dr_chain.release ();
11968 : : }
11969 : :
11970 : 485896 : if (costing_p)
11971 : : {
11972 : 333976 : gcc_assert (memory_access_type == VMAT_CONTIGUOUS
11973 : : || memory_access_type == VMAT_CONTIGUOUS_REVERSE
11974 : : || memory_access_type == VMAT_CONTIGUOUS_PERMUTE);
11975 : 333976 : if (n_adjacent_loads > 0)
11976 : 333976 : vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11977 : : alignment_support_scheme, misalignment, false,
11978 : : &inside_cost, &prologue_cost, cost_vec, cost_vec,
11979 : : true);
11980 : 333976 : if (dump_enabled_p ())
11981 : 21656 : dump_printf_loc (MSG_NOTE, vect_location,
11982 : : "vect_model_load_cost: inside_cost = %u, "
11983 : : "prologue_cost = %u .\n",
11984 : : inside_cost, prologue_cost);
11985 : : }
11986 : :
11987 : : return true;
11988 : 1070881 : }
11989 : :
11990 : : /* Function vect_is_simple_cond.
11991 : :
11992 : : Input:
11993 : : LOOP - the loop that is being vectorized.
11994 : : COND - Condition that is checked for simple use.
11995 : :
11996 : : Output:
11997 : : *COMP_VECTYPE - the vector type for the comparison.
11998 : : *DTS - The def types for the arguments of the comparison
11999 : :
12000 : : Returns whether a COND can be vectorized. Checks whether
12001 : : condition operands are supportable using vec_is_simple_use. */
12002 : :
12003 : : static bool
12004 : 24220 : vect_is_simple_cond (tree cond, vec_info *vinfo, stmt_vec_info stmt_info,
12005 : : slp_tree slp_node, tree *comp_vectype,
12006 : : enum vect_def_type *dts, tree vectype)
12007 : : {
12008 : 24220 : tree lhs, rhs;
12009 : 24220 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12010 : 24220 : slp_tree slp_op;
12011 : :
12012 : : /* Mask case. */
12013 : 24220 : if (TREE_CODE (cond) == SSA_NAME
12014 : 24220 : && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
12015 : : {
12016 : 24208 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &cond,
12017 : : &slp_op, &dts[0], comp_vectype)
12018 : 24208 : || !*comp_vectype
12019 : 48382 : || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
12020 : : return false;
12021 : : return true;
12022 : : }
12023 : :
12024 : 12 : if (!COMPARISON_CLASS_P (cond))
12025 : : return false;
12026 : :
12027 : 0 : lhs = TREE_OPERAND (cond, 0);
12028 : 0 : rhs = TREE_OPERAND (cond, 1);
12029 : :
12030 : 0 : if (TREE_CODE (lhs) == SSA_NAME)
12031 : : {
12032 : 0 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0,
12033 : : &lhs, &slp_op, &dts[0], &vectype1))
12034 : : return false;
12035 : : }
12036 : 0 : else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
12037 : 0 : || TREE_CODE (lhs) == FIXED_CST)
12038 : 0 : dts[0] = vect_constant_def;
12039 : : else
12040 : : return false;
12041 : :
12042 : 0 : if (TREE_CODE (rhs) == SSA_NAME)
12043 : : {
12044 : 0 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1,
12045 : : &rhs, &slp_op, &dts[1], &vectype2))
12046 : : return false;
12047 : : }
12048 : 0 : else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
12049 : 0 : || TREE_CODE (rhs) == FIXED_CST)
12050 : 0 : dts[1] = vect_constant_def;
12051 : : else
12052 : : return false;
12053 : :
12054 : 0 : if (vectype1 && vectype2
12055 : 0 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12056 : 0 : TYPE_VECTOR_SUBPARTS (vectype2)))
12057 : 0 : return false;
12058 : :
12059 : 0 : *comp_vectype = vectype1 ? vectype1 : vectype2;
12060 : : /* Invariant comparison. */
12061 : 0 : if (! *comp_vectype)
12062 : : {
12063 : 0 : tree scalar_type = TREE_TYPE (lhs);
12064 : 0 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
12065 : 0 : *comp_vectype = truth_type_for (vectype);
12066 : : else
12067 : : {
12068 : : /* If we can widen the comparison to match vectype do so. */
12069 : 0 : if (INTEGRAL_TYPE_P (scalar_type)
12070 : 0 : && !slp_node
12071 : 0 : && tree_int_cst_lt (TYPE_SIZE (scalar_type),
12072 : 0 : TYPE_SIZE (TREE_TYPE (vectype))))
12073 : 0 : scalar_type = build_nonstandard_integer_type
12074 : 0 : (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
12075 : 0 : *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
12076 : : slp_node);
12077 : : }
12078 : : }
12079 : :
12080 : : return true;
12081 : : }
12082 : :
12083 : : /* vectorizable_condition.
12084 : :
12085 : : Check if STMT_INFO is conditional modify expression that can be vectorized.
12086 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
12087 : : stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
12088 : : at GSI.
12089 : :
12090 : : When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
12091 : :
12092 : : Return true if STMT_INFO is vectorizable in this way. */
12093 : :
12094 : : static bool
12095 : 397077 : vectorizable_condition (vec_info *vinfo,
12096 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12097 : : gimple **vec_stmt,
12098 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12099 : : {
12100 : 397077 : tree scalar_dest = NULL_TREE;
12101 : 397077 : tree vec_dest = NULL_TREE;
12102 : 397077 : tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
12103 : 397077 : tree then_clause, else_clause;
12104 : 397077 : tree comp_vectype = NULL_TREE;
12105 : 397077 : tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
12106 : 397077 : tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
12107 : 397077 : tree vec_compare;
12108 : 397077 : tree new_temp;
12109 : 397077 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12110 : 397077 : enum vect_def_type dts[4]
12111 : : = {vect_unknown_def_type, vect_unknown_def_type,
12112 : : vect_unknown_def_type, vect_unknown_def_type};
12113 : 397077 : int ndts = 4;
12114 : 397077 : int ncopies;
12115 : 397077 : int vec_num;
12116 : 397077 : enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12117 : 397077 : int i;
12118 : 397077 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12119 : 397077 : vec<tree> vec_oprnds0 = vNULL;
12120 : 397077 : vec<tree> vec_oprnds1 = vNULL;
12121 : 397077 : vec<tree> vec_oprnds2 = vNULL;
12122 : 397077 : vec<tree> vec_oprnds3 = vNULL;
12123 : 397077 : tree vec_cmp_type;
12124 : 397077 : bool masked = false;
12125 : :
12126 : 397077 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12127 : : return false;
12128 : :
12129 : : /* Is vectorizable conditional operation? */
12130 : 685235 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12131 : 311294 : if (!stmt)
12132 : : return false;
12133 : :
12134 : 311294 : code = gimple_assign_rhs_code (stmt);
12135 : 311294 : if (code != COND_EXPR)
12136 : : return false;
12137 : :
12138 : 24220 : stmt_vec_info reduc_info = NULL;
12139 : 24220 : int reduc_index = -1;
12140 : 24220 : vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
12141 : 24220 : bool for_reduction
12142 : 24220 : = STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)) != NULL;
12143 : 24220 : if (for_reduction)
12144 : : {
12145 : 750 : if (slp_node && SLP_TREE_LANES (slp_node) > 1)
12146 : : return false;
12147 : 750 : reduc_info = info_for_reduction (vinfo, stmt_info);
12148 : 750 : reduction_type = STMT_VINFO_REDUC_TYPE (reduc_info);
12149 : 750 : reduc_index = STMT_VINFO_REDUC_IDX (stmt_info);
12150 : 750 : gcc_assert (reduction_type != EXTRACT_LAST_REDUCTION
12151 : : || reduc_index != -1);
12152 : : }
12153 : : else
12154 : : {
12155 : 23470 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12156 : : return false;
12157 : : }
12158 : :
12159 : 24220 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
12160 : 24220 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12161 : :
12162 : 24220 : if (slp_node)
12163 : : {
12164 : 24220 : ncopies = 1;
12165 : 24220 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
12166 : : }
12167 : : else
12168 : : {
12169 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
12170 : 0 : vec_num = 1;
12171 : : }
12172 : :
12173 : 24220 : gcc_assert (ncopies >= 1);
12174 : 24220 : if (for_reduction && ncopies > 1)
12175 : : return false; /* FORNOW */
12176 : :
12177 : 24220 : cond_expr = gimple_assign_rhs1 (stmt);
12178 : 24220 : gcc_assert (! COMPARISON_CLASS_P (cond_expr));
12179 : :
12180 : 24220 : if (!vect_is_simple_cond (cond_expr, vinfo, stmt_info, slp_node,
12181 : : &comp_vectype, &dts[0], vectype)
12182 : 24220 : || !comp_vectype)
12183 : : return false;
12184 : :
12185 : 24118 : unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
12186 : 24118 : slp_tree then_slp_node, else_slp_node;
12187 : 24118 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1 + op_adjust,
12188 : : &then_clause, &then_slp_node, &dts[2], &vectype1))
12189 : : return false;
12190 : 24118 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 2 + op_adjust,
12191 : : &else_clause, &else_slp_node, &dts[3], &vectype2))
12192 : : return false;
12193 : :
12194 : 24118 : if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
12195 : : return false;
12196 : :
12197 : 24118 : if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
12198 : : return false;
12199 : :
12200 : 24118 : masked = !COMPARISON_CLASS_P (cond_expr);
12201 : 24118 : vec_cmp_type = truth_type_for (comp_vectype);
12202 : 24118 : if (vec_cmp_type == NULL_TREE
12203 : 48236 : || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
12204 : 24118 : TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
12205 : 854 : return false;
12206 : :
12207 : 23264 : cond_code = TREE_CODE (cond_expr);
12208 : 23264 : if (!masked)
12209 : : {
12210 : 0 : cond_expr0 = TREE_OPERAND (cond_expr, 0);
12211 : 0 : cond_expr1 = TREE_OPERAND (cond_expr, 1);
12212 : : }
12213 : :
12214 : : /* For conditional reductions, the "then" value needs to be the candidate
12215 : : value calculated by this iteration while the "else" value needs to be
12216 : : the result carried over from previous iterations. If the COND_EXPR
12217 : : is the other way around, we need to swap it. */
12218 : 23264 : bool must_invert_cmp_result = false;
12219 : 23264 : if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
12220 : : {
12221 : 0 : if (masked)
12222 : : must_invert_cmp_result = true;
12223 : : else
12224 : : {
12225 : 0 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
12226 : 0 : tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
12227 : 0 : if (new_code == ERROR_MARK)
12228 : : must_invert_cmp_result = true;
12229 : : else
12230 : : {
12231 : 0 : cond_code = new_code;
12232 : : /* Make sure we don't accidentally use the old condition. */
12233 : 0 : cond_expr = NULL_TREE;
12234 : : }
12235 : : }
12236 : : /* ??? The vectorized operand query below doesn't allow swapping
12237 : : this way for SLP. */
12238 : 0 : if (slp_node)
12239 : : return false;
12240 : 0 : std::swap (then_clause, else_clause);
12241 : : }
12242 : :
12243 : 23264 : if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
12244 : : {
12245 : : /* Boolean values may have another representation in vectors
12246 : : and therefore we prefer bit operations over comparison for
12247 : : them (which also works for scalar masks). We store opcodes
12248 : : to use in bitop1 and bitop2. Statement is vectorized as
12249 : : BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
12250 : : depending on bitop1 and bitop2 arity. */
12251 : 0 : switch (cond_code)
12252 : : {
12253 : : case GT_EXPR:
12254 : : bitop1 = BIT_NOT_EXPR;
12255 : : bitop2 = BIT_AND_EXPR;
12256 : : break;
12257 : 0 : case GE_EXPR:
12258 : 0 : bitop1 = BIT_NOT_EXPR;
12259 : 0 : bitop2 = BIT_IOR_EXPR;
12260 : 0 : break;
12261 : 0 : case LT_EXPR:
12262 : 0 : bitop1 = BIT_NOT_EXPR;
12263 : 0 : bitop2 = BIT_AND_EXPR;
12264 : 0 : std::swap (cond_expr0, cond_expr1);
12265 : 0 : break;
12266 : 0 : case LE_EXPR:
12267 : 0 : bitop1 = BIT_NOT_EXPR;
12268 : 0 : bitop2 = BIT_IOR_EXPR;
12269 : 0 : std::swap (cond_expr0, cond_expr1);
12270 : 0 : break;
12271 : 0 : case NE_EXPR:
12272 : 0 : bitop1 = BIT_XOR_EXPR;
12273 : 0 : break;
12274 : 0 : case EQ_EXPR:
12275 : 0 : bitop1 = BIT_XOR_EXPR;
12276 : 0 : bitop2 = BIT_NOT_EXPR;
12277 : 0 : break;
12278 : : default:
12279 : : return false;
12280 : : }
12281 : : cond_code = SSA_NAME;
12282 : : }
12283 : :
12284 : 23264 : if (TREE_CODE_CLASS (cond_code) == tcc_comparison
12285 : 0 : && reduction_type == EXTRACT_LAST_REDUCTION
12286 : 23264 : && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
12287 : : {
12288 : 0 : if (dump_enabled_p ())
12289 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12290 : : "reduction comparison operation not supported.\n");
12291 : 0 : return false;
12292 : : }
12293 : :
12294 : 23264 : if (!vec_stmt)
12295 : : {
12296 : 16821 : if (bitop1 != NOP_EXPR)
12297 : : {
12298 : 0 : machine_mode mode = TYPE_MODE (comp_vectype);
12299 : 0 : optab optab;
12300 : :
12301 : 0 : optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
12302 : 0 : if (!optab || !can_implement_p (optab, mode))
12303 : 0 : return false;
12304 : :
12305 : 0 : if (bitop2 != NOP_EXPR)
12306 : : {
12307 : 0 : optab = optab_for_tree_code (bitop2, comp_vectype,
12308 : : optab_default);
12309 : 0 : if (!optab || !can_implement_p (optab, mode))
12310 : 0 : return false;
12311 : : }
12312 : : }
12313 : :
12314 : 16821 : vect_cost_for_stmt kind = vector_stmt;
12315 : 16821 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12316 : : /* Count one reduction-like operation per vector. */
12317 : : kind = vec_to_scalar;
12318 : 16821 : else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
12319 : 16821 : || (!masked
12320 : 0 : && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
12321 : : cond_code)
12322 : 0 : || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
12323 : 128 : return false;
12324 : :
12325 : 16693 : if (slp_node
12326 : 33386 : && (!vect_maybe_update_slp_op_vectype
12327 : 16693 : (SLP_TREE_CHILDREN (slp_node)[0], comp_vectype)
12328 : 16693 : || (op_adjust == 1
12329 : 0 : && !vect_maybe_update_slp_op_vectype
12330 : 0 : (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
12331 : 16693 : || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
12332 : 16693 : || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype)))
12333 : : {
12334 : 0 : if (dump_enabled_p ())
12335 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12336 : : "incompatible vector types for invariants\n");
12337 : 0 : return false;
12338 : : }
12339 : :
12340 : 16693 : if (loop_vinfo && for_reduction
12341 : 521 : && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
12342 : : {
12343 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12344 : : {
12345 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12346 : : vectype, OPTIMIZE_FOR_SPEED))
12347 : 0 : vect_record_loop_len (loop_vinfo,
12348 : : &LOOP_VINFO_LENS (loop_vinfo),
12349 : 0 : ncopies * vec_num, vectype, 1);
12350 : : else
12351 : 0 : vect_record_loop_mask (loop_vinfo,
12352 : : &LOOP_VINFO_MASKS (loop_vinfo),
12353 : 0 : ncopies * vec_num, vectype, NULL);
12354 : : }
12355 : : /* Extra inactive lanes should be safe for vect_nested_cycle. */
12356 : 0 : else if (STMT_VINFO_DEF_TYPE (reduc_info) != vect_nested_cycle)
12357 : : {
12358 : 0 : if (dump_enabled_p ())
12359 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12360 : : "conditional reduction prevents the use"
12361 : : " of partial vectors.\n");
12362 : 0 : LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
12363 : : }
12364 : : }
12365 : :
12366 : 16693 : STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
12367 : 16693 : vect_model_simple_cost (vinfo, ncopies, dts, ndts, slp_node,
12368 : : cost_vec, kind);
12369 : 16693 : return true;
12370 : : }
12371 : :
12372 : : /* Transform. */
12373 : :
12374 : : /* Handle def. */
12375 : 6443 : scalar_dest = gimple_assign_lhs (stmt);
12376 : 6443 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12377 : 6443 : vec_dest = vect_create_destination_var (scalar_dest, vectype);
12378 : :
12379 : 6443 : bool swap_cond_operands = false;
12380 : :
12381 : : /* See whether another part of the vectorized code applies a loop
12382 : : mask to the condition, or to its inverse. */
12383 : :
12384 : 6443 : vec_loop_masks *masks = NULL;
12385 : 6443 : vec_loop_lens *lens = NULL;
12386 : 6443 : if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
12387 : : {
12388 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12389 : 0 : lens = &LOOP_VINFO_LENS (loop_vinfo);
12390 : : }
12391 : 6443 : else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
12392 : : {
12393 : 2 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12394 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12395 : : else
12396 : : {
12397 : 2 : scalar_cond_masked_key cond (cond_expr, ncopies);
12398 : 2 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12399 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12400 : : else
12401 : : {
12402 : 2 : bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
12403 : 2 : tree_code orig_code = cond.code;
12404 : 2 : cond.code = invert_tree_comparison (cond.code, honor_nans);
12405 : 2 : if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
12406 : : {
12407 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12408 : 0 : cond_code = cond.code;
12409 : 0 : swap_cond_operands = true;
12410 : : }
12411 : : else
12412 : : {
12413 : : /* Try the inverse of the current mask. We check if the
12414 : : inverse mask is live and if so we generate a negate of
12415 : : the current mask such that we still honor NaNs. */
12416 : 2 : cond.inverted_p = true;
12417 : 2 : cond.code = orig_code;
12418 : 2 : if (loop_vinfo->scalar_cond_masked_set.contains (cond))
12419 : : {
12420 : 0 : masks = &LOOP_VINFO_MASKS (loop_vinfo);
12421 : 0 : cond_code = cond.code;
12422 : 0 : swap_cond_operands = true;
12423 : 0 : must_invert_cmp_result = true;
12424 : : }
12425 : : }
12426 : : }
12427 : : }
12428 : : }
12429 : :
12430 : : /* Handle cond expr. */
12431 : 6443 : if (masked)
12432 : 6443 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
12433 : : cond_expr, comp_vectype, &vec_oprnds0,
12434 : : then_clause, vectype, &vec_oprnds2,
12435 : : reduction_type != EXTRACT_LAST_REDUCTION
12436 : : ? else_clause : NULL, vectype, &vec_oprnds3);
12437 : : else
12438 : 0 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
12439 : : cond_expr0, comp_vectype, &vec_oprnds0,
12440 : : cond_expr1, comp_vectype, &vec_oprnds1,
12441 : : then_clause, vectype, &vec_oprnds2,
12442 : : reduction_type != EXTRACT_LAST_REDUCTION
12443 : : ? else_clause : NULL, vectype, &vec_oprnds3);
12444 : :
12445 : 6443 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12446 : 0 : vec_else_clause = else_clause;
12447 : :
12448 : : /* Arguments are ready. Create the new vector stmt. */
12449 : 15440 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
12450 : : {
12451 : 8997 : vec_then_clause = vec_oprnds2[i];
12452 : 8997 : if (reduction_type != EXTRACT_LAST_REDUCTION)
12453 : 8997 : vec_else_clause = vec_oprnds3[i];
12454 : :
12455 : 8997 : if (swap_cond_operands)
12456 : 0 : std::swap (vec_then_clause, vec_else_clause);
12457 : :
12458 : 8997 : if (masked)
12459 : : vec_compare = vec_cond_lhs;
12460 : : else
12461 : : {
12462 : 0 : vec_cond_rhs = vec_oprnds1[i];
12463 : 0 : if (bitop1 == NOP_EXPR)
12464 : : {
12465 : 0 : gimple_seq stmts = NULL;
12466 : 0 : vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
12467 : : vec_cond_lhs, vec_cond_rhs);
12468 : 0 : gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
12469 : : }
12470 : : else
12471 : : {
12472 : 0 : new_temp = make_ssa_name (vec_cmp_type);
12473 : 0 : gassign *new_stmt;
12474 : 0 : if (bitop1 == BIT_NOT_EXPR)
12475 : 0 : new_stmt = gimple_build_assign (new_temp, bitop1,
12476 : : vec_cond_rhs);
12477 : : else
12478 : 0 : new_stmt
12479 : 0 : = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
12480 : : vec_cond_rhs);
12481 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12482 : 0 : if (bitop2 == NOP_EXPR)
12483 : : vec_compare = new_temp;
12484 : 0 : else if (bitop2 == BIT_NOT_EXPR
12485 : 0 : && reduction_type != EXTRACT_LAST_REDUCTION)
12486 : : {
12487 : : /* Instead of doing ~x ? y : z do x ? z : y. */
12488 : : vec_compare = new_temp;
12489 : : std::swap (vec_then_clause, vec_else_clause);
12490 : : }
12491 : : else
12492 : : {
12493 : 0 : vec_compare = make_ssa_name (vec_cmp_type);
12494 : 0 : if (bitop2 == BIT_NOT_EXPR)
12495 : 0 : new_stmt
12496 : 0 : = gimple_build_assign (vec_compare, bitop2, new_temp);
12497 : : else
12498 : 0 : new_stmt
12499 : 0 : = gimple_build_assign (vec_compare, bitop2,
12500 : : vec_cond_lhs, new_temp);
12501 : 0 : vect_finish_stmt_generation (vinfo, stmt_info,
12502 : : new_stmt, gsi);
12503 : : }
12504 : : }
12505 : : }
12506 : :
12507 : : /* If we decided to apply a loop mask to the result of the vector
12508 : : comparison, AND the comparison with the mask now. Later passes
12509 : : should then be able to reuse the AND results between mulitple
12510 : : vector statements.
12511 : :
12512 : : For example:
12513 : : for (int i = 0; i < 100; ++i)
12514 : : x[i] = y[i] ? z[i] : 10;
12515 : :
12516 : : results in following optimized GIMPLE:
12517 : :
12518 : : mask__35.8_43 = vect__4.7_41 != { 0, ... };
12519 : : vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
12520 : : _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
12521 : : vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
12522 : : vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
12523 : : vect_iftmp.11_47, { 10, ... }>;
12524 : :
12525 : : instead of using a masked and unmasked forms of
12526 : : vec != { 0, ... } (masked in the MASK_LOAD,
12527 : : unmasked in the VEC_COND_EXPR). */
12528 : :
12529 : : /* Force vec_compare to be an SSA_NAME rather than a comparison,
12530 : : in cases where that's necessary. */
12531 : :
12532 : 8997 : tree len = NULL_TREE, bias = NULL_TREE;
12533 : 8997 : if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
12534 : : {
12535 : 0 : if (!is_gimple_val (vec_compare))
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 : : vec_compare);
12540 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12541 : 0 : vec_compare = vec_compare_name;
12542 : : }
12543 : :
12544 : 0 : if (must_invert_cmp_result)
12545 : : {
12546 : 0 : tree vec_compare_name = make_ssa_name (vec_cmp_type);
12547 : 0 : gassign *new_stmt = gimple_build_assign (vec_compare_name,
12548 : : BIT_NOT_EXPR,
12549 : : vec_compare);
12550 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12551 : 0 : vec_compare = vec_compare_name;
12552 : : }
12553 : :
12554 : 0 : if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
12555 : : vectype, OPTIMIZE_FOR_SPEED))
12556 : : {
12557 : 0 : if (lens)
12558 : : {
12559 : 0 : len = vect_get_loop_len (loop_vinfo, gsi, lens,
12560 : 0 : vec_num * ncopies, vectype, i, 1);
12561 : 0 : signed char biasval
12562 : 0 : = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
12563 : 0 : bias = build_int_cst (intQI_type_node, biasval);
12564 : : }
12565 : : else
12566 : : {
12567 : 0 : len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12568 : 0 : bias = build_int_cst (intQI_type_node, 0);
12569 : : }
12570 : : }
12571 : 0 : if (masks)
12572 : : {
12573 : 0 : tree loop_mask
12574 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num * ncopies,
12575 : : vectype, i);
12576 : 0 : tree tmp2 = make_ssa_name (vec_cmp_type);
12577 : 0 : gassign *g
12578 : 0 : = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
12579 : : loop_mask);
12580 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
12581 : 0 : vec_compare = tmp2;
12582 : : }
12583 : : }
12584 : :
12585 : 0 : gimple *new_stmt;
12586 : 0 : if (reduction_type == EXTRACT_LAST_REDUCTION)
12587 : : {
12588 : 0 : gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
12589 : 0 : tree lhs = gimple_get_lhs (old_stmt);
12590 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12591 : 0 : lhs = copy_ssa_name (lhs);
12592 : 0 : if (len)
12593 : 0 : new_stmt = gimple_build_call_internal
12594 : 0 : (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
12595 : : vec_then_clause, len, bias);
12596 : : else
12597 : 0 : new_stmt = gimple_build_call_internal
12598 : 0 : (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
12599 : : vec_then_clause);
12600 : 0 : gimple_call_set_lhs (new_stmt, lhs);
12601 : 0 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
12602 : 0 : if ((unsigned)i != vec_oprnds0.length () - 1)
12603 : : {
12604 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12605 : 0 : vec_else_clause = lhs;
12606 : : }
12607 : 0 : else if (old_stmt == gsi_stmt (*gsi))
12608 : 0 : vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
12609 : : else
12610 : : {
12611 : : /* In this case we're moving the definition to later in the
12612 : : block. That doesn't matter because the only uses of the
12613 : : lhs are in phi statements. */
12614 : 0 : gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
12615 : 0 : gsi_remove (&old_gsi, true);
12616 : 0 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12617 : : }
12618 : : }
12619 : : else
12620 : : {
12621 : 8997 : new_temp = make_ssa_name (vec_dest);
12622 : 8997 : new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
12623 : : vec_then_clause, vec_else_clause);
12624 : 8997 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12625 : : }
12626 : 8997 : if (slp_node)
12627 : 8997 : slp_node->push_vec_def (new_stmt);
12628 : : else
12629 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
12630 : : }
12631 : :
12632 : 6443 : if (!slp_node)
12633 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
12634 : :
12635 : 6443 : vec_oprnds0.release ();
12636 : 6443 : vec_oprnds1.release ();
12637 : 6443 : vec_oprnds2.release ();
12638 : 6443 : vec_oprnds3.release ();
12639 : :
12640 : 6443 : return true;
12641 : : }
12642 : :
12643 : : /* Helper of vectorizable_comparison.
12644 : :
12645 : : Check if STMT_INFO is comparison expression CODE that can be vectorized.
12646 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
12647 : : comparison, put it in VEC_STMT, and insert it at GSI.
12648 : :
12649 : : Return true if STMT_INFO is vectorizable in this way. */
12650 : :
12651 : : static bool
12652 : 299975 : vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
12653 : : stmt_vec_info stmt_info, tree_code code,
12654 : : gimple_stmt_iterator *gsi, gimple **vec_stmt,
12655 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12656 : : {
12657 : 299975 : tree lhs, rhs1, rhs2;
12658 : 299975 : tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12659 : 299975 : tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
12660 : 299975 : tree new_temp;
12661 : 299975 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12662 : 299975 : enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
12663 : 299975 : int ndts = 2;
12664 : 299975 : poly_uint64 nunits;
12665 : 299975 : int ncopies;
12666 : 299975 : enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12667 : 299975 : int i;
12668 : 299975 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12669 : 299975 : vec<tree> vec_oprnds0 = vNULL;
12670 : 299975 : vec<tree> vec_oprnds1 = vNULL;
12671 : 299975 : tree mask_type;
12672 : 299975 : tree mask = NULL_TREE;
12673 : :
12674 : 299975 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12675 : : return false;
12676 : :
12677 : 299975 : if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
12678 : : return false;
12679 : :
12680 : 130802 : mask_type = vectype;
12681 : 130802 : nunits = TYPE_VECTOR_SUBPARTS (vectype);
12682 : :
12683 : 130802 : if (slp_node)
12684 : : ncopies = 1;
12685 : : else
12686 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
12687 : :
12688 : 0 : gcc_assert (ncopies >= 1);
12689 : :
12690 : 130802 : if (TREE_CODE_CLASS (code) != tcc_comparison)
12691 : : return false;
12692 : :
12693 : 129259 : slp_tree slp_rhs1, slp_rhs2;
12694 : 129259 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
12695 : : 0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
12696 : : return false;
12697 : :
12698 : 129259 : if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
12699 : : 1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
12700 : : return false;
12701 : :
12702 : 72874 : if (vectype1 && vectype2
12703 : 187391 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12704 : 58132 : TYPE_VECTOR_SUBPARTS (vectype2)))
12705 : 16 : return false;
12706 : :
12707 : 129243 : vectype = vectype1 ? vectype1 : vectype2;
12708 : :
12709 : : /* Invariant comparison. */
12710 : 129243 : if (!vectype)
12711 : : {
12712 : 29756 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
12713 : 29756 : if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
12714 : 7 : return false;
12715 : : }
12716 : 99487 : else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
12717 : : return false;
12718 : :
12719 : : /* Can't compare mask and non-mask types. */
12720 : 72858 : if (vectype1 && vectype2
12721 : 303110 : && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
12722 : : return false;
12723 : :
12724 : : /* Boolean values may have another representation in vectors
12725 : : and therefore we prefer bit operations over comparison for
12726 : : them (which also works for scalar masks). We store opcodes
12727 : : to use in bitop1 and bitop2. Statement is vectorized as
12728 : : BITOP2 (rhs1 BITOP1 rhs2) or
12729 : : rhs1 BITOP2 (BITOP1 rhs2)
12730 : : depending on bitop1 and bitop2 arity. */
12731 : 129232 : bool swap_p = false;
12732 : 129232 : if (VECTOR_BOOLEAN_TYPE_P (vectype))
12733 : : {
12734 : 593 : if (code == GT_EXPR)
12735 : : {
12736 : : bitop1 = BIT_NOT_EXPR;
12737 : : bitop2 = BIT_AND_EXPR;
12738 : : }
12739 : : else if (code == GE_EXPR)
12740 : : {
12741 : : bitop1 = BIT_NOT_EXPR;
12742 : : bitop2 = BIT_IOR_EXPR;
12743 : : }
12744 : : else if (code == LT_EXPR)
12745 : : {
12746 : : bitop1 = BIT_NOT_EXPR;
12747 : : bitop2 = BIT_AND_EXPR;
12748 : : swap_p = true;
12749 : : }
12750 : : else if (code == LE_EXPR)
12751 : : {
12752 : : bitop1 = BIT_NOT_EXPR;
12753 : : bitop2 = BIT_IOR_EXPR;
12754 : : swap_p = true;
12755 : : }
12756 : : else
12757 : : {
12758 : : bitop1 = BIT_XOR_EXPR;
12759 : : if (code == EQ_EXPR)
12760 : : bitop2 = BIT_NOT_EXPR;
12761 : : }
12762 : : }
12763 : :
12764 : 129232 : if (!vec_stmt)
12765 : : {
12766 : 119589 : if (bitop1 == NOP_EXPR)
12767 : : {
12768 : 119114 : if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
12769 : : return false;
12770 : : }
12771 : : else
12772 : : {
12773 : 475 : machine_mode mode = TYPE_MODE (vectype);
12774 : 475 : optab optab;
12775 : :
12776 : 475 : optab = optab_for_tree_code (bitop1, vectype, optab_default);
12777 : 475 : if (!optab || !can_implement_p (optab, mode))
12778 : 0 : return false;
12779 : :
12780 : 475 : if (bitop2 != NOP_EXPR)
12781 : : {
12782 : 53 : optab = optab_for_tree_code (bitop2, vectype, optab_default);
12783 : 53 : if (!optab || !can_implement_p (optab, mode))
12784 : 0 : return false;
12785 : : }
12786 : : }
12787 : :
12788 : : /* Put types on constant and invariant SLP children. */
12789 : 110608 : if (slp_node
12790 : 110608 : && (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
12791 : 110608 : || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype)))
12792 : : {
12793 : 0 : if (dump_enabled_p ())
12794 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12795 : : "incompatible vector types for invariants\n");
12796 : 0 : return false;
12797 : : }
12798 : :
12799 : 110608 : vect_model_simple_cost (vinfo, ncopies * (1 + (bitop2 != NOP_EXPR)),
12800 : : dts, ndts, slp_node, cost_vec);
12801 : 110608 : return true;
12802 : : }
12803 : :
12804 : : /* Transform. */
12805 : :
12806 : : /* Handle def. */
12807 : 9643 : lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
12808 : 9643 : if (lhs)
12809 : 9643 : mask = vect_create_destination_var (lhs, mask_type);
12810 : :
12811 : 9643 : vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
12812 : : rhs1, vectype, &vec_oprnds0,
12813 : : rhs2, vectype, &vec_oprnds1);
12814 : 9643 : if (swap_p)
12815 : 26 : std::swap (vec_oprnds0, vec_oprnds1);
12816 : :
12817 : : /* Arguments are ready. Create the new vector stmt. */
12818 : 22452 : FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
12819 : : {
12820 : 12809 : gimple *new_stmt;
12821 : 12809 : vec_rhs2 = vec_oprnds1[i];
12822 : :
12823 : 12809 : if (lhs)
12824 : 12809 : new_temp = make_ssa_name (mask);
12825 : : else
12826 : 0 : new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
12827 : 12809 : if (bitop1 == NOP_EXPR)
12828 : : {
12829 : 12686 : new_stmt = gimple_build_assign (new_temp, code,
12830 : : vec_rhs1, vec_rhs2);
12831 : 12686 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12832 : : }
12833 : : else
12834 : : {
12835 : 123 : if (bitop1 == BIT_NOT_EXPR)
12836 : 52 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
12837 : : else
12838 : 71 : new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
12839 : : vec_rhs2);
12840 : 123 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12841 : 123 : if (bitop2 != NOP_EXPR)
12842 : : {
12843 : 52 : tree res = make_ssa_name (mask);
12844 : 52 : if (bitop2 == BIT_NOT_EXPR)
12845 : 0 : new_stmt = gimple_build_assign (res, bitop2, new_temp);
12846 : : else
12847 : 52 : new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
12848 : : new_temp);
12849 : 52 : vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12850 : : }
12851 : : }
12852 : 12809 : if (slp_node)
12853 : 12809 : slp_node->push_vec_def (new_stmt);
12854 : : else
12855 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
12856 : : }
12857 : :
12858 : 9643 : if (!slp_node)
12859 : 0 : *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
12860 : :
12861 : 9643 : vec_oprnds0.release ();
12862 : 9643 : vec_oprnds1.release ();
12863 : :
12864 : 9643 : return true;
12865 : : }
12866 : :
12867 : : /* vectorizable_comparison.
12868 : :
12869 : : Check if STMT_INFO is comparison expression that can be vectorized.
12870 : : If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
12871 : : comparison, put it in VEC_STMT, and insert it at GSI.
12872 : :
12873 : : Return true if STMT_INFO is vectorizable in this way. */
12874 : :
12875 : : static bool
12876 : 383584 : vectorizable_comparison (vec_info *vinfo,
12877 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12878 : : gimple **vec_stmt,
12879 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12880 : : {
12881 : 383584 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12882 : :
12883 : 383584 : if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12884 : : return false;
12885 : :
12886 : 383584 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12887 : : return false;
12888 : :
12889 : 563308 : gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12890 : 297801 : if (!stmt)
12891 : : return false;
12892 : :
12893 : 297801 : enum tree_code code = gimple_assign_rhs_code (stmt);
12894 : 297801 : tree vectype = STMT_VINFO_VECTYPE (stmt_info);
12895 : 297801 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
12896 : : vec_stmt, slp_node, cost_vec))
12897 : : return false;
12898 : :
12899 : 118077 : if (!vec_stmt)
12900 : 108434 : STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
12901 : :
12902 : : return true;
12903 : : }
12904 : :
12905 : : /* Check to see if the current early break given in STMT_INFO is valid for
12906 : : vectorization. */
12907 : :
12908 : : bool
12909 : 244908 : vectorizable_early_exit (vec_info *vinfo, stmt_vec_info stmt_info,
12910 : : gimple_stmt_iterator *gsi, gimple **vec_stmt,
12911 : : slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12912 : : {
12913 : 244908 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12914 : 160543 : if (!loop_vinfo
12915 : 160543 : || !is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
12916 : : return false;
12917 : :
12918 : 48640 : if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
12919 : : return false;
12920 : :
12921 : 48640 : if (!STMT_VINFO_RELEVANT_P (stmt_info))
12922 : : return false;
12923 : :
12924 : 48640 : DUMP_VECT_SCOPE ("vectorizable_early_exit");
12925 : :
12926 : 48640 : auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
12927 : :
12928 : 48640 : tree vectype = NULL_TREE;
12929 : 48640 : slp_tree slp_op0;
12930 : 48640 : tree op0;
12931 : 48640 : enum vect_def_type dt0;
12932 : :
12933 : : /* Early break gcond kind SLP trees can be root only and have no children,
12934 : : for instance in the case where the argument is an external. If that's
12935 : : the case there is no operand to analyse use of. */
12936 : 48640 : if ((!slp_node || !SLP_TREE_CHILDREN (slp_node).is_empty ())
12937 : 97280 : && !vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &op0, &slp_op0, &dt0,
12938 : : &vectype))
12939 : : {
12940 : 0 : if (dump_enabled_p ())
12941 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
12942 : : "use not simple.\n");
12943 : 0 : return false;
12944 : : }
12945 : :
12946 : : /* For SLP we don't want to use the type of the operands of the SLP node, when
12947 : : vectorizing using SLP slp_node will be the children of the gcond and we
12948 : : want to use the type of the direct children which since the gcond is root
12949 : : will be the current node, rather than a child node as vect_is_simple_use
12950 : : assumes. */
12951 : 48640 : if (slp_node)
12952 : 48640 : vectype = SLP_TREE_VECTYPE (slp_node);
12953 : :
12954 : 48640 : if (!vectype)
12955 : : return false;
12956 : :
12957 : 48640 : machine_mode mode = TYPE_MODE (vectype);
12958 : 48640 : int ncopies, vec_num;
12959 : :
12960 : 48640 : if (slp_node)
12961 : : {
12962 : 48640 : ncopies = 1;
12963 : 48640 : vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
12964 : : }
12965 : : else
12966 : : {
12967 : 0 : ncopies = vect_get_num_copies (loop_vinfo, vectype);
12968 : 0 : vec_num = 1;
12969 : : }
12970 : :
12971 : 48640 : vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
12972 : 48640 : vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
12973 : 48640 : bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
12974 : 48640 : bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
12975 : :
12976 : : /* Now build the new conditional. Pattern gimple_conds get dropped during
12977 : : codegen so we must replace the original insn. */
12978 : 48640 : gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
12979 : 48640 : gcond *cond_stmt = as_a <gcond *>(orig_stmt);
12980 : :
12981 : 48640 : tree cst = build_zero_cst (vectype);
12982 : 48640 : auto bb = gimple_bb (cond_stmt);
12983 : 48640 : edge exit_true_edge = EDGE_SUCC (bb, 0);
12984 : 48640 : if (exit_true_edge->flags & EDGE_FALSE_VALUE)
12985 : 666 : exit_true_edge = EDGE_SUCC (bb, 1);
12986 : 48640 : gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
12987 : :
12988 : : /* When vectorizing we assume that if the branch edge is taken that we're
12989 : : exiting the loop. This is not however always the case as the compiler will
12990 : : rewrite conditions to always be a comparison against 0. To do this it
12991 : : sometimes flips the edges. This is fine for scalar, but for vector we
12992 : : then have to negate the result of the test, as we're still assuming that if
12993 : : you take the branch edge that we found the exit condition. i.e. we need to
12994 : : know whether we are generating a `forall` or an `exist` condition. */
12995 : 97280 : bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
12996 : 48640 : exit_true_edge->dest);
12997 : :
12998 : : /* Analyze only. */
12999 : 48640 : if (!vec_stmt)
13000 : : {
13001 : 47027 : if (direct_optab_handler (cbranch_optab, mode) == CODE_FOR_nothing)
13002 : : {
13003 : 44853 : if (dump_enabled_p ())
13004 : 513 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13005 : : "can't vectorize early exit because the "
13006 : : "target doesn't support flag setting vector "
13007 : : "comparisons.\n");
13008 : 44853 : return false;
13009 : : }
13010 : :
13011 : 2174 : if (ncopies > 1
13012 : 2174 : && direct_optab_handler (ior_optab, mode) == CODE_FOR_nothing)
13013 : : {
13014 : 0 : if (dump_enabled_p ())
13015 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13016 : : "can't vectorize early exit because the "
13017 : : "target does not support boolean vector IOR "
13018 : : "for type %T.\n",
13019 : : vectype);
13020 : 0 : return false;
13021 : : }
13022 : :
13023 : 2174 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
13024 : : vec_stmt, slp_node, cost_vec))
13025 : : return false;
13026 : :
13027 : 2174 : if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
13028 : : {
13029 : 1 : if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
13030 : : OPTIMIZE_FOR_SPEED))
13031 : 0 : vect_record_loop_len (loop_vinfo, lens, ncopies * vec_num,
13032 : : vectype, 1);
13033 : : else
13034 : 1 : vect_record_loop_mask (loop_vinfo, masks, ncopies * vec_num,
13035 : : vectype, NULL);
13036 : : }
13037 : :
13038 : 2174 : return true;
13039 : : }
13040 : :
13041 : : /* Tranform. */
13042 : :
13043 : 1613 : tree new_temp = NULL_TREE;
13044 : 1613 : gimple *new_stmt = NULL;
13045 : :
13046 : 1613 : if (dump_enabled_p ())
13047 : 345 : dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
13048 : :
13049 : : /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
13050 : : roots and so by the time we get to them we have already codegened the SLP tree
13051 : : and so we shouldn't try to do so again. The arguments have already been
13052 : : vectorized. It's not very clean to do this here, But the masking code below is
13053 : : complex and this keeps it all in one place to ease fixes and backports. Once we
13054 : : drop the non-SLP loop vect or split vectorizable_* this can be simplified. */
13055 : 1613 : if (!slp_node)
13056 : : {
13057 : 0 : if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
13058 : : vec_stmt, slp_node, cost_vec))
13059 : 0 : gcc_unreachable ();
13060 : : }
13061 : :
13062 : 1613 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
13063 : 1613 : basic_block cond_bb = gimple_bb (stmt);
13064 : 1613 : gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb);
13065 : :
13066 : 1613 : auto_vec<tree> stmts;
13067 : :
13068 : 1613 : if (slp_node)
13069 : 1613 : stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
13070 : : else
13071 : : {
13072 : 0 : auto vec_stmts = STMT_VINFO_VEC_STMTS (stmt_info);
13073 : 0 : stmts.reserve_exact (vec_stmts.length ());
13074 : 0 : for (auto stmt : vec_stmts)
13075 : 0 : stmts.quick_push (gimple_assign_lhs (stmt));
13076 : : }
13077 : :
13078 : : /* If we're comparing against a previous forall we need to negate the resullts
13079 : : before we do the final comparison or reduction. */
13080 : 1613 : if (flipped)
13081 : : {
13082 : : /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
13083 : : if (any(~mask)) by negating the masks and flipping the branches.
13084 : :
13085 : : 1. For unmasked loops we simply reduce the ~mask.
13086 : : 2. For masked loops we reduce (~mask & loop_mask) which is the same as
13087 : : doing (mask & loop_mask) ^ loop_mask. */
13088 : 222 : for (unsigned i = 0; i < stmts.length (); i++)
13089 : : {
13090 : 133 : tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
13091 : 133 : auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
13092 : 133 : vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
13093 : : &cond_gsi);
13094 : 133 : stmts[i] = inv_lhs;
13095 : : }
13096 : :
13097 : 89 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13098 : 89 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13099 : : }
13100 : :
13101 : : /* Determine if we need to reduce the final value. */
13102 : 1613 : if (stmts.length () > 1)
13103 : : {
13104 : : /* We build the reductions in a way to maintain as much parallelism as
13105 : : possible. */
13106 : 393 : auto_vec<tree> workset (stmts.length ());
13107 : :
13108 : : /* Mask the statements as we queue them up. Normally we loop over
13109 : : vec_num, but since we inspect the exact results of vectorization
13110 : : we don't need to and instead can just use the stmts themselves. */
13111 : 393 : if (masked_loop_p)
13112 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
13113 : : {
13114 : 0 : tree stmt_mask
13115 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies * vec_num,
13116 : : vectype, i);
13117 : 0 : stmt_mask
13118 : 0 : = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
13119 : 0 : stmts[i], &cond_gsi);
13120 : 0 : workset.quick_push (stmt_mask);
13121 : : }
13122 : 393 : else if (len_loop_p)
13123 : 0 : for (unsigned i = 0; i < stmts.length (); i++)
13124 : : {
13125 : 0 : tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
13126 : 0 : lens, ncopies * vec_num,
13127 : 0 : vectype, stmts[i], i, 1);
13128 : :
13129 : 0 : workset.quick_push (len_mask);
13130 : : }
13131 : : else
13132 : 393 : workset.splice (stmts);
13133 : :
13134 : 926 : while (workset.length () > 1)
13135 : : {
13136 : 533 : new_temp = make_temp_ssa_name (vectype, NULL, "vexit_reduc");
13137 : 533 : tree arg0 = workset.pop ();
13138 : 533 : tree arg1 = workset.pop ();
13139 : 533 : new_stmt = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
13140 : 533 : vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
13141 : : &cond_gsi);
13142 : 533 : workset.quick_insert (0, new_temp);
13143 : : }
13144 : 393 : }
13145 : : else
13146 : : {
13147 : 1220 : new_temp = stmts[0];
13148 : 1220 : if (masked_loop_p)
13149 : : {
13150 : 0 : tree mask
13151 : 0 : = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies, vectype, 0);
13152 : 0 : new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
13153 : : new_temp, &cond_gsi);
13154 : : }
13155 : 1220 : else if (len_loop_p)
13156 : 0 : new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
13157 : : ncopies, vectype, new_temp, 0, 1);
13158 : : }
13159 : :
13160 : 1613 : gcc_assert (new_temp);
13161 : :
13162 : 1613 : gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
13163 : 1613 : update_stmt (orig_stmt);
13164 : :
13165 : 1613 : if (slp_node)
13166 : 1613 : SLP_TREE_VEC_DEFS (slp_node).truncate (0);
13167 : : else
13168 : 0 : STMT_VINFO_VEC_STMTS (stmt_info).truncate (0);
13169 : :
13170 : 1613 : if (!slp_node)
13171 : 0 : *vec_stmt = orig_stmt;
13172 : :
13173 : 1613 : return true;
13174 : 1613 : }
13175 : :
13176 : : /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
13177 : : can handle all live statements in the node. Otherwise return true
13178 : : if STMT_INFO is not live or if vectorizable_live_operation can handle it.
13179 : : VEC_STMT_P is as for vectorizable_live_operation. */
13180 : :
13181 : : static bool
13182 : 1284541 : can_vectorize_live_stmts (vec_info *vinfo,
13183 : : slp_tree slp_node, slp_instance slp_node_instance,
13184 : : bool vec_stmt_p,
13185 : : stmt_vector_for_cost *cost_vec)
13186 : : {
13187 : 1284541 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
13188 : 1284541 : stmt_vec_info slp_stmt_info;
13189 : 1284541 : unsigned int i;
13190 : 2893592 : FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
13191 : : {
13192 : 1609051 : if (slp_stmt_info
13193 : 1592036 : && (STMT_VINFO_LIVE_P (slp_stmt_info)
13194 : 1411051 : || (loop_vinfo
13195 : 1158246 : && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
13196 : 206540 : && STMT_VINFO_DEF_TYPE (slp_stmt_info)
13197 : : == vect_induction_def))
13198 : 1790036 : && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
13199 : : slp_node_instance, i,
13200 : : vec_stmt_p, cost_vec))
13201 : : return false;
13202 : : }
13203 : :
13204 : : return true;
13205 : : }
13206 : :
13207 : : /* Make sure the statement is vectorizable. */
13208 : :
13209 : : opt_result
13210 : 2298425 : vect_analyze_stmt (vec_info *vinfo,
13211 : : slp_tree node, slp_instance node_instance,
13212 : : stmt_vector_for_cost *cost_vec)
13213 : : {
13214 : 2298425 : stmt_vec_info stmt_info = SLP_TREE_REPRESENTATIVE (node);
13215 : 2298425 : bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13216 : 2298425 : enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
13217 : 2298425 : bool ok;
13218 : :
13219 : 2298425 : if (dump_enabled_p ())
13220 : 90963 : dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
13221 : : stmt_info->stmt);
13222 : :
13223 : 4376605 : if (gimple_has_volatile_ops (stmt_info->stmt))
13224 : : {
13225 : : /* ??? This shouldn't really happen, volatile stmts should
13226 : : not end up in the SLP graph. */
13227 : 0 : return opt_result::failure_at (stmt_info->stmt,
13228 : : "not vectorized:"
13229 : : " stmt has volatile operands: %G\n",
13230 : : stmt_info->stmt);
13231 : : }
13232 : :
13233 : : /* Skip stmts that do not need to be vectorized. */
13234 : 2298425 : if (!STMT_VINFO_RELEVANT_P (stmt_info)
13235 : 0 : && !STMT_VINFO_LIVE_P (stmt_info))
13236 : : {
13237 : 0 : if (dump_enabled_p ())
13238 : 0 : dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
13239 : :
13240 : : /* ??? This shouldn't really happen, irrelevant stmts should
13241 : : not end up in the SLP graph. */
13242 : 0 : return opt_result::failure_at (stmt_info->stmt,
13243 : : "not vectorized:"
13244 : : " irrelevant stmt as SLP node %p "
13245 : : "representative.\n",
13246 : : (void *)node);
13247 : : }
13248 : :
13249 : 2298425 : switch (STMT_VINFO_DEF_TYPE (stmt_info))
13250 : : {
13251 : : case vect_internal_def:
13252 : : case vect_condition_def:
13253 : : break;
13254 : :
13255 : 52202 : case vect_reduction_def:
13256 : 52202 : case vect_nested_cycle:
13257 : 52202 : gcc_assert (!bb_vinfo
13258 : : && (relevance == vect_used_in_outer
13259 : : || relevance == vect_used_in_outer_by_reduction
13260 : : || relevance == vect_used_by_reduction
13261 : : || relevance == vect_unused_in_scope
13262 : : || relevance == vect_used_only_live));
13263 : : break;
13264 : :
13265 : 324 : case vect_double_reduction_def:
13266 : 324 : gcc_assert (!bb_vinfo && node);
13267 : : break;
13268 : :
13269 : 105510 : case vect_induction_def:
13270 : 105510 : case vect_first_order_recurrence:
13271 : 105510 : gcc_assert (!bb_vinfo);
13272 : : break;
13273 : :
13274 : 0 : case vect_constant_def:
13275 : 0 : case vect_external_def:
13276 : 0 : case vect_unknown_def_type:
13277 : 0 : default:
13278 : 0 : gcc_unreachable ();
13279 : : }
13280 : :
13281 : 2298425 : tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
13282 : 2298425 : STMT_VINFO_VECTYPE (stmt_info) = SLP_TREE_VECTYPE (node);
13283 : :
13284 : 2298425 : if (STMT_VINFO_RELEVANT_P (stmt_info))
13285 : : {
13286 : 2298425 : gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
13287 : 2298425 : gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
13288 : : || gimple_code (stmt_info->stmt) == GIMPLE_COND
13289 : : || (call && gimple_call_lhs (call) == NULL_TREE));
13290 : : }
13291 : :
13292 : 2298425 : ok = true;
13293 : 2298425 : if (!bb_vinfo
13294 : 1024933 : && (STMT_VINFO_RELEVANT_P (stmt_info)
13295 : 0 : || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
13296 : : /* Prefer vectorizable_call over vectorizable_simd_clone_call so
13297 : : -mveclibabi= takes preference over library functions with
13298 : : the simd attribute. */
13299 : 1024933 : ok = (vectorizable_call (vinfo, stmt_info, NULL, NULL, node, cost_vec)
13300 : 1020614 : || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, NULL, node,
13301 : : cost_vec)
13302 : 1020184 : || vectorizable_conversion (vinfo, stmt_info,
13303 : : NULL, NULL, node, cost_vec)
13304 : 970489 : || vectorizable_operation (vinfo, stmt_info,
13305 : : NULL, NULL, node, cost_vec)
13306 : 741941 : || vectorizable_assignment (vinfo, stmt_info,
13307 : : NULL, NULL, node, cost_vec)
13308 : 707672 : || vectorizable_load (vinfo, stmt_info, NULL, NULL, node, cost_vec)
13309 : 481222 : || vectorizable_store (vinfo, stmt_info, NULL, NULL, node, cost_vec)
13310 : 365147 : || vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
13311 : : stmt_info, node, cost_vec)
13312 : 364811 : || vectorizable_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
13313 : : node, node_instance, cost_vec)
13314 : 315141 : || vectorizable_induction (as_a <loop_vec_info> (vinfo), stmt_info,
13315 : : NULL, node, cost_vec)
13316 : 216356 : || vectorizable_shift (vinfo, stmt_info, NULL, NULL, node, cost_vec)
13317 : 202718 : || vectorizable_condition (vinfo, stmt_info,
13318 : : NULL, NULL, node, cost_vec)
13319 : 186723 : || vectorizable_comparison (vinfo, stmt_info, NULL, NULL, node,
13320 : : cost_vec)
13321 : 112758 : || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
13322 : : stmt_info, node)
13323 : 112101 : || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13324 : : stmt_info, NULL, node, cost_vec)
13325 : 1136836 : || vectorizable_early_exit (vinfo, stmt_info, NULL, NULL, node,
13326 : : cost_vec));
13327 : : else
13328 : : {
13329 : 0 : if (bb_vinfo)
13330 : 1273492 : ok = (vectorizable_call (vinfo, stmt_info, NULL, NULL, node, cost_vec)
13331 : 1272584 : || vectorizable_simd_clone_call (vinfo, stmt_info,
13332 : : NULL, NULL, node, cost_vec)
13333 : 1272583 : || vectorizable_conversion (vinfo, stmt_info, NULL, NULL, node,
13334 : : cost_vec)
13335 : 1262121 : || vectorizable_shift (vinfo, stmt_info,
13336 : : NULL, NULL, node, cost_vec)
13337 : 1238096 : || vectorizable_operation (vinfo, stmt_info,
13338 : : NULL, NULL, node, cost_vec)
13339 : 1068021 : || vectorizable_assignment (vinfo, stmt_info, NULL, NULL, node,
13340 : : cost_vec)
13341 : 1045971 : || vectorizable_load (vinfo, stmt_info,
13342 : : NULL, NULL, node, cost_vec)
13343 : 911570 : || vectorizable_store (vinfo, stmt_info,
13344 : : NULL, NULL, node, cost_vec)
13345 : 187916 : || vectorizable_condition (vinfo, stmt_info,
13346 : : NULL, NULL, node, cost_vec)
13347 : 187218 : || vectorizable_comparison (vinfo, stmt_info, NULL, NULL, node,
13348 : : cost_vec)
13349 : 152749 : || vectorizable_phi (vinfo, stmt_info, NULL, node, cost_vec)
13350 : 1357857 : || vectorizable_early_exit (vinfo, stmt_info, NULL, NULL, node,
13351 : : cost_vec));
13352 : :
13353 : : }
13354 : :
13355 : 2298425 : STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
13356 : :
13357 : 2298425 : if (!ok)
13358 : 196268 : return opt_result::failure_at (stmt_info->stmt,
13359 : : "not vectorized:"
13360 : : " relevant stmt not supported: %G",
13361 : : stmt_info->stmt);
13362 : :
13363 : : /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
13364 : : need extra handling, except for vectorizable reductions. */
13365 : 2102157 : if (!bb_vinfo
13366 : 913030 : && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
13367 : 905912 : && STMT_VINFO_TYPE (stmt_info) != lc_phi_info_type
13368 : 905255 : && (!node->ldst_lanes || SLP_TREE_CODE (node) == VEC_PERM_EXPR)
13369 : 3007412 : && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
13370 : : node, node_instance,
13371 : : false, cost_vec))
13372 : 0 : return opt_result::failure_at (stmt_info->stmt,
13373 : : "not vectorized:"
13374 : : " live stmt not supported: %G",
13375 : : stmt_info->stmt);
13376 : :
13377 : 2102157 : return opt_result::success ();
13378 : : }
13379 : :
13380 : :
13381 : : /* Function vect_transform_stmt.
13382 : :
13383 : : Create a vectorized stmt to replace STMT_INFO, and insert it at GSI. */
13384 : :
13385 : : bool
13386 : 920166 : vect_transform_stmt (vec_info *vinfo,
13387 : : stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
13388 : : slp_tree slp_node, slp_instance slp_node_instance)
13389 : : {
13390 : 920166 : bool is_store = false;
13391 : 920166 : gimple *vec_stmt = NULL;
13392 : 920166 : bool done;
13393 : :
13394 : 920166 : gcc_assert (slp_node || !PURE_SLP_STMT (stmt_info));
13395 : :
13396 : 920166 : if (dump_enabled_p ())
13397 : 63809 : dump_printf_loc (MSG_NOTE, vect_location,
13398 : : "------>vectorizing statement: %G", stmt_info->stmt);
13399 : :
13400 : 920166 : tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
13401 : 920166 : if (slp_node)
13402 : 920166 : STMT_VINFO_VECTYPE (stmt_info) = SLP_TREE_VECTYPE (slp_node);
13403 : :
13404 : 920166 : switch (STMT_VINFO_TYPE (stmt_info))
13405 : : {
13406 : 19426 : case type_demotion_vec_info_type:
13407 : 19426 : case type_promotion_vec_info_type:
13408 : 19426 : case type_conversion_vec_info_type:
13409 : 19426 : done = vectorizable_conversion (vinfo, stmt_info,
13410 : : gsi, &vec_stmt, slp_node, NULL);
13411 : 19426 : gcc_assert (done);
13412 : : break;
13413 : :
13414 : 16006 : case induc_vec_info_type:
13415 : 16006 : done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
13416 : : stmt_info, &vec_stmt, slp_node,
13417 : : NULL);
13418 : 16006 : gcc_assert (done);
13419 : : break;
13420 : :
13421 : 5771 : case shift_vec_info_type:
13422 : 5771 : done = vectorizable_shift (vinfo, stmt_info,
13423 : : gsi, &vec_stmt, slp_node, NULL);
13424 : 5771 : gcc_assert (done);
13425 : : break;
13426 : :
13427 : 109141 : case op_vec_info_type:
13428 : 109141 : done = vectorizable_operation (vinfo, stmt_info, gsi, &vec_stmt, slp_node,
13429 : : NULL);
13430 : 109141 : gcc_assert (done);
13431 : : break;
13432 : :
13433 : 13914 : case assignment_vec_info_type:
13434 : 13914 : done = vectorizable_assignment (vinfo, stmt_info,
13435 : : gsi, &vec_stmt, slp_node, NULL);
13436 : 13914 : gcc_assert (done);
13437 : : break;
13438 : :
13439 : 156663 : case load_vec_info_type:
13440 : 156663 : done = vectorizable_load (vinfo, stmt_info, gsi, &vec_stmt, slp_node,
13441 : : NULL);
13442 : 156663 : gcc_assert (done);
13443 : : break;
13444 : :
13445 : 540880 : case store_vec_info_type:
13446 : 540880 : if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
13447 : 489116 : && !slp_node
13448 : 540880 : && (++DR_GROUP_STORE_COUNT (DR_GROUP_FIRST_ELEMENT (stmt_info))
13449 : 0 : < DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info))))
13450 : : /* In case of interleaving, the whole chain is vectorized when the
13451 : : last store in the chain is reached. Store stmts before the last
13452 : : one are skipped, and there vec_stmt_info shouldn't be freed
13453 : : meanwhile. */
13454 : : ;
13455 : : else
13456 : : {
13457 : 540880 : done = vectorizable_store (vinfo, stmt_info,
13458 : : gsi, &vec_stmt, slp_node, NULL);
13459 : 540880 : gcc_assert (done);
13460 : : is_store = true;
13461 : : }
13462 : : break;
13463 : :
13464 : 6443 : case condition_vec_info_type:
13465 : 6443 : done = vectorizable_condition (vinfo, stmt_info,
13466 : : gsi, &vec_stmt, slp_node, NULL);
13467 : 6443 : gcc_assert (done);
13468 : : break;
13469 : :
13470 : 9643 : case comparison_vec_info_type:
13471 : 9643 : done = vectorizable_comparison (vinfo, stmt_info, gsi, &vec_stmt,
13472 : : slp_node, NULL);
13473 : 9643 : gcc_assert (done);
13474 : : break;
13475 : :
13476 : 3484 : case call_vec_info_type:
13477 : 3484 : done = vectorizable_call (vinfo, stmt_info,
13478 : : gsi, &vec_stmt, slp_node, NULL);
13479 : 3484 : break;
13480 : :
13481 : 332 : case call_simd_clone_vec_info_type:
13482 : 332 : done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi, &vec_stmt,
13483 : : slp_node, NULL);
13484 : 332 : break;
13485 : :
13486 : 2297 : case reduc_vec_info_type:
13487 : 2297 : done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
13488 : : gsi, slp_node);
13489 : 2297 : gcc_assert (done);
13490 : : break;
13491 : :
13492 : 22601 : case cycle_phi_info_type:
13493 : 22601 : done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
13494 : : slp_node, slp_node_instance);
13495 : 22601 : gcc_assert (done);
13496 : : break;
13497 : :
13498 : 414 : case lc_phi_info_type:
13499 : 414 : done = vect_transform_lc_phi (as_a <loop_vec_info> (vinfo),
13500 : : stmt_info, slp_node);
13501 : 414 : gcc_assert (done);
13502 : : break;
13503 : :
13504 : 32 : case recurr_info_type:
13505 : 32 : done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
13506 : : stmt_info, &vec_stmt, slp_node, NULL);
13507 : 32 : gcc_assert (done);
13508 : : break;
13509 : :
13510 : 13119 : case phi_info_type:
13511 : 13119 : done = vectorizable_phi (vinfo, stmt_info, &vec_stmt, slp_node, NULL);
13512 : 13119 : gcc_assert (done);
13513 : : break;
13514 : :
13515 : 0 : case loop_exit_ctrl_vec_info_type:
13516 : 0 : done = vectorizable_early_exit (vinfo, stmt_info, gsi, &vec_stmt,
13517 : : slp_node, NULL);
13518 : 0 : gcc_assert (done);
13519 : : break;
13520 : :
13521 : 0 : default:
13522 : 0 : if (!STMT_VINFO_LIVE_P (stmt_info))
13523 : : {
13524 : 0 : if (dump_enabled_p ())
13525 : 0 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13526 : : "stmt not supported.\n");
13527 : 0 : gcc_unreachable ();
13528 : : }
13529 : 920166 : done = true;
13530 : : }
13531 : :
13532 : 920166 : if (!slp_node && vec_stmt)
13533 : 0 : gcc_assert (STMT_VINFO_VEC_STMTS (stmt_info).exists ());
13534 : :
13535 : 920166 : if (STMT_VINFO_TYPE (stmt_info) != store_vec_info_type
13536 : 379286 : && (!slp_node
13537 : 379286 : || !slp_node->ldst_lanes
13538 : 0 : || SLP_TREE_CODE (slp_node) == VEC_PERM_EXPR))
13539 : : {
13540 : : /* Handle stmts whose DEF is used outside the loop-nest that is
13541 : : being vectorized. */
13542 : 379286 : done = can_vectorize_live_stmts (vinfo, slp_node,
13543 : : slp_node_instance, true, NULL);
13544 : 379286 : gcc_assert (done);
13545 : : }
13546 : :
13547 : 920166 : if (slp_node)
13548 : 920166 : STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
13549 : :
13550 : 920166 : return is_store;
13551 : : }
13552 : :
13553 : :
13554 : : /* Remove a group of stores (for SLP or interleaving), free their
13555 : : stmt_vec_info. */
13556 : :
13557 : : void
13558 : 0 : vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
13559 : : {
13560 : 0 : stmt_vec_info next_stmt_info = first_stmt_info;
13561 : :
13562 : 0 : while (next_stmt_info)
13563 : : {
13564 : 0 : stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
13565 : 0 : next_stmt_info = vect_orig_stmt (next_stmt_info);
13566 : : /* Free the attached stmt_vec_info and remove the stmt. */
13567 : 0 : vinfo->remove_stmt (next_stmt_info);
13568 : 0 : next_stmt_info = tmp;
13569 : : }
13570 : 0 : }
13571 : :
13572 : : /* If NUNITS is nonzero, return a vector type that contains NUNITS
13573 : : elements of type SCALAR_TYPE, or null if the target doesn't support
13574 : : such a type.
13575 : :
13576 : : If NUNITS is zero, return a vector type that contains elements of
13577 : : type SCALAR_TYPE, choosing whichever vector size the target prefers.
13578 : :
13579 : : If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
13580 : : for this vectorization region and want to "autodetect" the best choice.
13581 : : Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
13582 : : and we want the new type to be interoperable with it. PREVAILING_MODE
13583 : : in this case can be a scalar integer mode or a vector mode; when it
13584 : : is a vector mode, the function acts like a tree-level version of
13585 : : related_vector_mode. */
13586 : :
13587 : : tree
13588 : 29833449 : get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
13589 : : tree scalar_type, poly_uint64 nunits)
13590 : : {
13591 : 29833449 : tree orig_scalar_type = scalar_type;
13592 : 29833449 : scalar_mode inner_mode;
13593 : 29833449 : machine_mode simd_mode;
13594 : 29833449 : tree vectype;
13595 : :
13596 : 29833449 : if ((!INTEGRAL_TYPE_P (scalar_type)
13597 : 10330114 : && !POINTER_TYPE_P (scalar_type)
13598 : 1615973 : && !SCALAR_FLOAT_TYPE_P (scalar_type))
13599 : 39694773 : || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
13600 : 1147264 : && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
13601 : 473358 : return NULL_TREE;
13602 : :
13603 : 29360091 : unsigned int nbytes = GET_MODE_SIZE (inner_mode);
13604 : :
13605 : : /* Interoperability between modes requires one to be a constant multiple
13606 : : of the other, so that the number of vectors required for each operation
13607 : : is a compile-time constant. */
13608 : 29360091 : if (prevailing_mode != VOIDmode
13609 : 28294748 : && !constant_multiple_p (nunits * nbytes,
13610 : 28294748 : GET_MODE_SIZE (prevailing_mode))
13611 : 30866918 : && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
13612 : 1506827 : nunits * nbytes))
13613 : : return NULL_TREE;
13614 : :
13615 : : /* For vector types of elements whose mode precision doesn't
13616 : : match their types precision we use a element type of mode
13617 : : precision. The vectorization routines will have to make sure
13618 : : they support the proper result truncation/extension.
13619 : : We also make sure to build vector types with INTEGER_TYPE
13620 : : component type only. */
13621 : 29360091 : if (INTEGRAL_TYPE_P (scalar_type)
13622 : 48863345 : && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
13623 : 17620482 : || TREE_CODE (scalar_type) != INTEGER_TYPE))
13624 : 2132721 : scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
13625 : 2132721 : TYPE_UNSIGNED (scalar_type));
13626 : :
13627 : : /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
13628 : : When the component mode passes the above test simply use a type
13629 : : corresponding to that mode. The theory is that any use that
13630 : : would cause problems with this will disable vectorization anyway. */
13631 : 27227370 : else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
13632 : : && !INTEGRAL_TYPE_P (scalar_type))
13633 : 8714141 : scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
13634 : :
13635 : : /* We can't build a vector type of elements with alignment bigger than
13636 : : their size. */
13637 : 18513229 : else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
13638 : 302100 : scalar_type = lang_hooks.types.type_for_mode (inner_mode,
13639 : 151050 : TYPE_UNSIGNED (scalar_type));
13640 : :
13641 : : /* If we felt back to using the mode fail if there was
13642 : : no scalar type for it. */
13643 : 29360091 : if (scalar_type == NULL_TREE)
13644 : : return NULL_TREE;
13645 : :
13646 : : /* If no prevailing mode was supplied, use the mode the target prefers.
13647 : : Otherwise lookup a vector mode based on the prevailing mode. */
13648 : 29360091 : if (prevailing_mode == VOIDmode)
13649 : : {
13650 : 1065343 : gcc_assert (known_eq (nunits, 0U));
13651 : 1065343 : simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
13652 : 1065343 : if (SCALAR_INT_MODE_P (simd_mode))
13653 : : {
13654 : : /* Traditional behavior is not to take the integer mode
13655 : : literally, but simply to use it as a way of determining
13656 : : the vector size. It is up to mode_for_vector to decide
13657 : : what the TYPE_MODE should be.
13658 : :
13659 : : Note that nunits == 1 is allowed in order to support single
13660 : : element vector types. */
13661 : 34270 : if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
13662 : 341 : || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13663 : 16794 : return NULL_TREE;
13664 : : }
13665 : : }
13666 : 28294748 : else if (SCALAR_INT_MODE_P (prevailing_mode)
13667 : 28294748 : || !related_vector_mode (prevailing_mode,
13668 : 26337425 : inner_mode, nunits).exists (&simd_mode))
13669 : : {
13670 : : /* Fall back to using mode_for_vector, mostly in the hope of being
13671 : : able to use an integer mode. */
13672 : 1957323 : if (known_eq (nunits, 0U)
13673 : 4544880 : && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
13674 : : return NULL_TREE;
13675 : :
13676 : 147150 : if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
13677 : 137301 : return NULL_TREE;
13678 : : }
13679 : :
13680 : 27395823 : vectype = build_vector_type_for_mode (scalar_type, simd_mode);
13681 : :
13682 : : /* In cases where the mode was chosen by mode_for_vector, check that
13683 : : the target actually supports the chosen mode, or that it at least
13684 : : allows the vector mode to be replaced by a like-sized integer. */
13685 : 54791646 : if (!VECTOR_MODE_P (TYPE_MODE (vectype))
13686 : 27405923 : && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
13687 : : return NULL_TREE;
13688 : :
13689 : : /* Re-attach the address-space qualifier if we canonicalized the scalar
13690 : : type. */
13691 : 27387869 : if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
13692 : 6 : return build_qualified_type
13693 : 6 : (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
13694 : :
13695 : : return vectype;
13696 : : }
13697 : :
13698 : : /* Function get_vectype_for_scalar_type.
13699 : :
13700 : : Returns the vector type corresponding to SCALAR_TYPE as supported
13701 : : by the target. If GROUP_SIZE is nonzero and we're performing BB
13702 : : vectorization, make sure that the number of elements in the vector
13703 : : is no bigger than GROUP_SIZE. */
13704 : :
13705 : : tree
13706 : 26666127 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
13707 : : unsigned int group_size)
13708 : : {
13709 : : /* For BB vectorization, we should always have a group size once we've
13710 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
13711 : : are tentative requests during things like early data reference
13712 : : analysis and pattern recognition. */
13713 : 26666127 : if (is_a <bb_vec_info> (vinfo))
13714 : 25506448 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
13715 : : else
13716 : : group_size = 0;
13717 : :
13718 : 26666127 : tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13719 : : scalar_type);
13720 : 26666127 : if (vectype && vinfo->vector_mode == VOIDmode)
13721 : 1048547 : vinfo->vector_mode = TYPE_MODE (vectype);
13722 : :
13723 : : /* Register the natural choice of vector type, before the group size
13724 : : has been applied. */
13725 : 0 : if (vectype)
13726 : 24224331 : vinfo->used_vector_modes.add (TYPE_MODE (vectype));
13727 : :
13728 : : /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
13729 : : try again with an explicit number of elements. */
13730 : 24224331 : if (vectype
13731 : 24224331 : && group_size
13732 : 26666127 : && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
13733 : : {
13734 : : /* Start with the biggest number of units that fits within
13735 : : GROUP_SIZE and halve it until we find a valid vector type.
13736 : : Usually either the first attempt will succeed or all will
13737 : : fail (in the latter case because GROUP_SIZE is too small
13738 : : for the target), but it's possible that a target could have
13739 : : a hole between supported vector types.
13740 : :
13741 : : If GROUP_SIZE is not a power of 2, this has the effect of
13742 : : trying the largest power of 2 that fits within the group,
13743 : : even though the group is not a multiple of that vector size.
13744 : : The BB vectorizer will then try to carve up the group into
13745 : : smaller pieces. */
13746 : 3041114 : unsigned int nunits = 1 << floor_log2 (group_size);
13747 : 3041114 : do
13748 : : {
13749 : 3041114 : vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
13750 : 3041114 : scalar_type, nunits);
13751 : 3041114 : nunits /= 2;
13752 : : }
13753 : 3041114 : while (nunits > 1 && !vectype);
13754 : : }
13755 : :
13756 : 26666127 : return vectype;
13757 : : }
13758 : :
13759 : : /* Return the vector type corresponding to SCALAR_TYPE as supported
13760 : : by the target. NODE, if nonnull, is the SLP tree node that will
13761 : : use the returned vector type. */
13762 : :
13763 : : tree
13764 : 176445 : get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
13765 : : {
13766 : 176445 : unsigned int group_size = 0;
13767 : 176445 : if (node)
13768 : 176445 : group_size = SLP_TREE_LANES (node);
13769 : 176445 : return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13770 : : }
13771 : :
13772 : : /* Function get_mask_type_for_scalar_type.
13773 : :
13774 : : Returns the mask type corresponding to a result of comparison
13775 : : of vectors of specified SCALAR_TYPE as supported by target.
13776 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
13777 : : make sure that the number of elements in the vector is no bigger
13778 : : than GROUP_SIZE. */
13779 : :
13780 : : tree
13781 : 1704391 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13782 : : unsigned int group_size)
13783 : : {
13784 : 1704391 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
13785 : :
13786 : 1704391 : if (!vectype)
13787 : : return NULL;
13788 : :
13789 : 1684529 : return truth_type_for (vectype);
13790 : : }
13791 : :
13792 : : /* Function get_mask_type_for_scalar_type.
13793 : :
13794 : : Returns the mask type corresponding to a result of comparison
13795 : : of vectors of specified SCALAR_TYPE as supported by target.
13796 : : NODE, if nonnull, is the SLP tree node that will use the returned
13797 : : vector type. */
13798 : :
13799 : : tree
13800 : 17 : get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
13801 : : slp_tree node)
13802 : : {
13803 : 17 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
13804 : :
13805 : 17 : if (!vectype)
13806 : : return NULL;
13807 : :
13808 : 17 : return truth_type_for (vectype);
13809 : : }
13810 : :
13811 : : /* Function get_same_sized_vectype
13812 : :
13813 : : Returns a vector type corresponding to SCALAR_TYPE of size
13814 : : VECTOR_TYPE if supported by the target. */
13815 : :
13816 : : tree
13817 : 115398 : get_same_sized_vectype (tree scalar_type, tree vector_type)
13818 : : {
13819 : 115398 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
13820 : 0 : return truth_type_for (vector_type);
13821 : :
13822 : 115398 : poly_uint64 nunits;
13823 : 230796 : if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
13824 : 230796 : GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
13825 : : return NULL_TREE;
13826 : :
13827 : 115398 : return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
13828 : 115398 : scalar_type, nunits);
13829 : : }
13830 : :
13831 : : /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
13832 : : would not change the chosen vector modes. */
13833 : :
13834 : : bool
13835 : 1417233 : vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
13836 : : {
13837 : 1417233 : for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
13838 : 3365275 : i != vinfo->used_vector_modes.end (); ++i)
13839 : 1728559 : if (!VECTOR_MODE_P (*i)
13840 : 5185677 : || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
13841 : 754538 : return false;
13842 : 662695 : return true;
13843 : : }
13844 : :
13845 : : /* Return true if replacing VECTOR_MODE with ALT_VECTOR_MODE would not
13846 : : change the chosen vector modes for analysis of a loop. */
13847 : :
13848 : : bool
13849 : 289031 : vect_chooses_same_modes_p (machine_mode vector_mode,
13850 : : machine_mode alt_vector_mode)
13851 : : {
13852 : 50159 : return (VECTOR_MODE_P (vector_mode)
13853 : 289031 : && VECTOR_MODE_P (alt_vector_mode)
13854 : 578062 : && (related_vector_mode (vector_mode,
13855 : : GET_MODE_INNER (alt_vector_mode))
13856 : 289031 : == alt_vector_mode)
13857 : 313837 : && (related_vector_mode (alt_vector_mode,
13858 : : GET_MODE_INNER (vector_mode))
13859 : 12403 : == vector_mode));
13860 : : }
13861 : :
13862 : : /* Function vect_is_simple_use.
13863 : :
13864 : : Input:
13865 : : VINFO - the vect info of the loop or basic block that is being vectorized.
13866 : : OPERAND - operand in the loop or bb.
13867 : : Output:
13868 : : DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
13869 : : case OPERAND is an SSA_NAME that is defined in the vectorizable region
13870 : : DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
13871 : : the definition could be anywhere in the function
13872 : : DT - the type of definition
13873 : :
13874 : : Returns whether a stmt with OPERAND can be vectorized.
13875 : : For loops, supportable operands are constants, loop invariants, and operands
13876 : : that are defined by the current iteration of the loop. Unsupportable
13877 : : operands are those that are defined by a previous iteration of the loop (as
13878 : : is the case in reduction/induction computations).
13879 : : For basic blocks, supportable operands are constants and bb invariants.
13880 : : For now, operands defined outside the basic block are not supported. */
13881 : :
13882 : : bool
13883 : 40983359 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
13884 : : stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
13885 : : {
13886 : 40983359 : if (def_stmt_info_out)
13887 : 37191931 : *def_stmt_info_out = NULL;
13888 : 40983359 : if (def_stmt_out)
13889 : 8621053 : *def_stmt_out = NULL;
13890 : 40983359 : *dt = vect_unknown_def_type;
13891 : :
13892 : 40983359 : if (dump_enabled_p ())
13893 : : {
13894 : 706956 : dump_printf_loc (MSG_NOTE, vect_location,
13895 : : "vect_is_simple_use: operand ");
13896 : 706956 : if (TREE_CODE (operand) == SSA_NAME
13897 : 706956 : && !SSA_NAME_IS_DEFAULT_DEF (operand))
13898 : 643689 : dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
13899 : : else
13900 : 63267 : dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
13901 : : }
13902 : :
13903 : 40983359 : if (CONSTANT_CLASS_P (operand))
13904 : 4168431 : *dt = vect_constant_def;
13905 : 36814928 : else if (is_gimple_min_invariant (operand))
13906 : 597092 : *dt = vect_external_def;
13907 : 36217836 : else if (TREE_CODE (operand) != SSA_NAME)
13908 : 1321 : *dt = vect_unknown_def_type;
13909 : 36216515 : else if (SSA_NAME_IS_DEFAULT_DEF (operand))
13910 : 557042 : *dt = vect_external_def;
13911 : : else
13912 : : {
13913 : 35659473 : gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
13914 : 35659473 : stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
13915 : 35659473 : if (!stmt_vinfo)
13916 : 831220 : *dt = vect_external_def;
13917 : : else
13918 : : {
13919 : 34828253 : stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
13920 : 34828253 : def_stmt = stmt_vinfo->stmt;
13921 : 34828253 : *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
13922 : 34828253 : if (def_stmt_info_out)
13923 : 32612161 : *def_stmt_info_out = stmt_vinfo;
13924 : : }
13925 : 35659473 : if (def_stmt_out)
13926 : 8422498 : *def_stmt_out = def_stmt;
13927 : : }
13928 : :
13929 : 40983359 : if (dump_enabled_p ())
13930 : : {
13931 : 706956 : dump_printf (MSG_NOTE, ", type of def: ");
13932 : 706956 : switch (*dt)
13933 : : {
13934 : 0 : case vect_uninitialized_def:
13935 : 0 : dump_printf (MSG_NOTE, "uninitialized\n");
13936 : 0 : break;
13937 : 52657 : case vect_constant_def:
13938 : 52657 : dump_printf (MSG_NOTE, "constant\n");
13939 : 52657 : break;
13940 : 25623 : case vect_external_def:
13941 : 25623 : dump_printf (MSG_NOTE, "external\n");
13942 : 25623 : break;
13943 : 516059 : case vect_internal_def:
13944 : 516059 : dump_printf (MSG_NOTE, "internal\n");
13945 : 516059 : break;
13946 : 89242 : case vect_induction_def:
13947 : 89242 : dump_printf (MSG_NOTE, "induction\n");
13948 : 89242 : break;
13949 : 20591 : case vect_reduction_def:
13950 : 20591 : dump_printf (MSG_NOTE, "reduction\n");
13951 : 20591 : break;
13952 : 328 : case vect_double_reduction_def:
13953 : 328 : dump_printf (MSG_NOTE, "double reduction\n");
13954 : 328 : break;
13955 : 1770 : case vect_nested_cycle:
13956 : 1770 : dump_printf (MSG_NOTE, "nested cycle\n");
13957 : 1770 : break;
13958 : 195 : case vect_first_order_recurrence:
13959 : 195 : dump_printf (MSG_NOTE, "first order recurrence\n");
13960 : 195 : break;
13961 : 0 : case vect_condition_def:
13962 : 0 : dump_printf (MSG_NOTE, "control flow\n");
13963 : 0 : break;
13964 : 491 : case vect_unknown_def_type:
13965 : 491 : dump_printf (MSG_NOTE, "unknown\n");
13966 : 491 : break;
13967 : : }
13968 : : }
13969 : :
13970 : 40983359 : if (*dt == vect_unknown_def_type)
13971 : : {
13972 : 19839 : if (dump_enabled_p ())
13973 : 491 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13974 : : "Unsupported pattern.\n");
13975 : 19839 : return false;
13976 : : }
13977 : :
13978 : : return true;
13979 : : }
13980 : :
13981 : : /* Function vect_is_simple_use.
13982 : :
13983 : : Same as vect_is_simple_use but also determines the vector operand
13984 : : type of OPERAND and stores it to *VECTYPE. If the definition of
13985 : : OPERAND is vect_uninitialized_def, vect_constant_def or
13986 : : vect_external_def *VECTYPE will be set to NULL_TREE and the caller
13987 : : is responsible to compute the best suited vector type for the
13988 : : scalar operand. */
13989 : :
13990 : : bool
13991 : 0 : vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
13992 : : tree *vectype, stmt_vec_info *def_stmt_info_out,
13993 : : gimple **def_stmt_out)
13994 : : {
13995 : 0 : stmt_vec_info def_stmt_info;
13996 : 0 : gimple *def_stmt;
13997 : 0 : if (!vect_is_simple_use (operand, vinfo, dt, &def_stmt_info, &def_stmt))
13998 : : return false;
13999 : :
14000 : 0 : if (def_stmt_out)
14001 : 0 : *def_stmt_out = def_stmt;
14002 : 0 : if (def_stmt_info_out)
14003 : 0 : *def_stmt_info_out = def_stmt_info;
14004 : :
14005 : : /* Now get a vector type if the def is internal, otherwise supply
14006 : : NULL_TREE and leave it up to the caller to figure out a proper
14007 : : type for the use stmt. */
14008 : 0 : if (*dt == vect_internal_def
14009 : : || *dt == vect_induction_def
14010 : : || *dt == vect_reduction_def
14011 : : || *dt == vect_double_reduction_def
14012 : : || *dt == vect_nested_cycle
14013 : 0 : || *dt == vect_first_order_recurrence)
14014 : : {
14015 : 0 : *vectype = STMT_VINFO_VECTYPE (def_stmt_info);
14016 : 0 : gcc_assert (*vectype != NULL_TREE);
14017 : 0 : if (dump_enabled_p ())
14018 : 0 : dump_printf_loc (MSG_NOTE, vect_location,
14019 : : "vect_is_simple_use: vectype %T\n", *vectype);
14020 : : }
14021 : 0 : else if (*dt == vect_uninitialized_def
14022 : : || *dt == vect_constant_def
14023 : : || *dt == vect_external_def)
14024 : 0 : *vectype = NULL_TREE;
14025 : : else
14026 : 0 : gcc_unreachable ();
14027 : :
14028 : : return true;
14029 : : }
14030 : :
14031 : : /* Function vect_is_simple_use.
14032 : :
14033 : : Same as vect_is_simple_use but determines the operand by operand
14034 : : position OPERAND from either STMT or SLP_NODE, filling in *OP
14035 : : and *SLP_DEF (when SLP_NODE is not NULL). */
14036 : :
14037 : : bool
14038 : 3373053 : vect_is_simple_use (vec_info *vinfo, stmt_vec_info stmt, slp_tree slp_node,
14039 : : unsigned operand, tree *op, slp_tree *slp_def,
14040 : : enum vect_def_type *dt,
14041 : : tree *vectype, stmt_vec_info *def_stmt_info_out)
14042 : : {
14043 : 3373053 : if (slp_node)
14044 : : {
14045 : 3373053 : slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
14046 : 3373053 : *slp_def = child;
14047 : 3373053 : *vectype = SLP_TREE_VECTYPE (child);
14048 : 3373053 : if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
14049 : : {
14050 : : /* ??? VEC_PERM nodes might be intermediate and their lane value
14051 : : have no representative (nor do we build a VEC_PERM stmt for
14052 : : the actual operation). Note for two-operator nodes we set
14053 : : a representative but leave scalar stmts empty as we'd only
14054 : : have one for a subset of lanes. Ideally no caller would
14055 : : require *op for internal defs. */
14056 : 1678215 : if (SLP_TREE_REPRESENTATIVE (child))
14057 : : {
14058 : 1677509 : *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
14059 : 1677509 : return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
14060 : : }
14061 : : else
14062 : : {
14063 : 706 : gcc_assert (SLP_TREE_CODE (child) == VEC_PERM_EXPR);
14064 : 706 : *op = error_mark_node;
14065 : 706 : *dt = vect_internal_def;
14066 : 706 : if (def_stmt_info_out)
14067 : 0 : *def_stmt_info_out = NULL;
14068 : 706 : return true;
14069 : : }
14070 : : }
14071 : : else
14072 : : {
14073 : 1694838 : if (def_stmt_info_out)
14074 : 44691 : *def_stmt_info_out = NULL;
14075 : 1694838 : *op = SLP_TREE_SCALAR_OPS (child)[0];
14076 : 1694838 : *dt = SLP_TREE_DEF_TYPE (child);
14077 : 1694838 : return true;
14078 : : }
14079 : : }
14080 : : else
14081 : : {
14082 : 0 : *slp_def = NULL;
14083 : 0 : if (gassign *ass = dyn_cast <gassign *> (stmt->stmt))
14084 : : {
14085 : 0 : if (gimple_assign_rhs_code (ass) == COND_EXPR
14086 : 0 : && COMPARISON_CLASS_P (gimple_assign_rhs1 (ass)))
14087 : 0 : gcc_unreachable ();
14088 : 0 : else if (gimple_assign_rhs_code (ass) == VIEW_CONVERT_EXPR)
14089 : 0 : *op = TREE_OPERAND (gimple_assign_rhs1 (ass), 0);
14090 : : else
14091 : 0 : *op = gimple_op (ass, operand + 1);
14092 : : }
14093 : 0 : else if (gcond *cond = dyn_cast <gcond *> (stmt->stmt))
14094 : 0 : *op = gimple_op (cond, operand);
14095 : 0 : else if (gcall *call = dyn_cast <gcall *> (stmt->stmt))
14096 : 0 : *op = gimple_call_arg (call, operand);
14097 : : else
14098 : 0 : gcc_unreachable ();
14099 : 0 : return vect_is_simple_use (*op, vinfo, dt, vectype, def_stmt_info_out);
14100 : : }
14101 : : }
14102 : :
14103 : : /* If OP is not NULL and is external or constant update its vector
14104 : : type with VECTYPE. Returns true if successful or false if not,
14105 : : for example when conflicting vector types are present. */
14106 : :
14107 : : bool
14108 : 2906087 : vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
14109 : : {
14110 : 2906087 : if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
14111 : : return true;
14112 : 1119959 : if (SLP_TREE_VECTYPE (op))
14113 : 67852 : return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
14114 : : /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
14115 : : should be handled by patters. Allow vect_constant_def for now
14116 : : as well as the trivial single-lane uniform vect_external_def case
14117 : : both of which we code-generate reasonably. */
14118 : 1052107 : if (VECTOR_BOOLEAN_TYPE_P (vectype)
14119 : 913 : && SLP_TREE_DEF_TYPE (op) == vect_external_def
14120 : 1052660 : && SLP_TREE_LANES (op) > 1)
14121 : : return false;
14122 : 1051906 : SLP_TREE_VECTYPE (op) = vectype;
14123 : 1051906 : return true;
14124 : : }
14125 : :
14126 : : /* Function supportable_widening_operation
14127 : :
14128 : : Check whether an operation represented by the code CODE is a
14129 : : widening operation that is supported by the target platform in
14130 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14131 : : producing a result of type VECTYPE_OUT).
14132 : :
14133 : : Widening operations we currently support are NOP (CONVERT), FLOAT,
14134 : : FIX_TRUNC and WIDEN_MULT. This function checks if these operations
14135 : : are supported by the target platform either directly (via vector
14136 : : tree-codes), or via target builtins.
14137 : :
14138 : : Output:
14139 : : - CODE1 and CODE2 are codes of vector operations to be used when
14140 : : vectorizing the operation, if available.
14141 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
14142 : : case of multi-step conversion (like char->short->int - in that case
14143 : : MULTI_STEP_CVT will be 1).
14144 : : - INTERM_TYPES contains the intermediate type required to perform the
14145 : : widening operation (short in the above example). */
14146 : :
14147 : : bool
14148 : 307380 : supportable_widening_operation (vec_info *vinfo,
14149 : : code_helper code,
14150 : : stmt_vec_info stmt_info,
14151 : : tree vectype_out, tree vectype_in,
14152 : : code_helper *code1,
14153 : : code_helper *code2,
14154 : : int *multi_step_cvt,
14155 : : vec<tree> *interm_types)
14156 : : {
14157 : 307380 : loop_vec_info loop_info = dyn_cast <loop_vec_info> (vinfo);
14158 : 307380 : class loop *vect_loop = NULL;
14159 : 307380 : machine_mode vec_mode;
14160 : 307380 : enum insn_code icode1, icode2;
14161 : 307380 : optab optab1 = unknown_optab, optab2 = unknown_optab;
14162 : 307380 : tree vectype = vectype_in;
14163 : 307380 : tree wide_vectype = vectype_out;
14164 : 307380 : tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
14165 : 307380 : int i;
14166 : 307380 : tree prev_type, intermediate_type;
14167 : 307380 : machine_mode intermediate_mode, prev_mode;
14168 : 307380 : optab optab3, optab4;
14169 : :
14170 : 307380 : *multi_step_cvt = 0;
14171 : 181190 : if (loop_info)
14172 : 126190 : vect_loop = LOOP_VINFO_LOOP (loop_info);
14173 : :
14174 : 307380 : switch (code.safe_as_tree_code ())
14175 : : {
14176 : : case MAX_TREE_CODES:
14177 : : /* Don't set c1 and c2 if code is not a tree_code. */
14178 : : break;
14179 : :
14180 : 165556 : case WIDEN_MULT_EXPR:
14181 : : /* The result of a vectorized widening operation usually requires
14182 : : two vectors (because the widened results do not fit into one vector).
14183 : : The generated vector results would normally be expected to be
14184 : : generated in the same order as in the original scalar computation,
14185 : : i.e. if 8 results are generated in each vector iteration, they are
14186 : : to be organized as follows:
14187 : : vect1: [res1,res2,res3,res4],
14188 : : vect2: [res5,res6,res7,res8].
14189 : :
14190 : : However, in the special case that the result of the widening
14191 : : operation is used in a reduction computation only, the order doesn't
14192 : : matter (because when vectorizing a reduction we change the order of
14193 : : the computation). Some targets can take advantage of this and
14194 : : generate more efficient code. For example, targets like Altivec,
14195 : : that support widen_mult using a sequence of {mult_even,mult_odd}
14196 : : generate the following vectors:
14197 : : vect1: [res1,res3,res5,res7],
14198 : : vect2: [res2,res4,res6,res8].
14199 : :
14200 : : When vectorizing outer-loops, we execute the inner-loop sequentially
14201 : : (each vectorized inner-loop iteration contributes to VF outer-loop
14202 : : iterations in parallel). We therefore don't allow to change the
14203 : : order of the computation in the inner-loop during outer-loop
14204 : : vectorization. */
14205 : : /* TODO: Another case in which order doesn't *really* matter is when we
14206 : : widen and then contract again, e.g. (short)((int)x * y >> 8).
14207 : : Normally, pack_trunc performs an even/odd permute, whereas the
14208 : : repack from an even/odd expansion would be an interleave, which
14209 : : would be significantly simpler for e.g. AVX2. */
14210 : : /* In any case, in order to avoid duplicating the code below, recurse
14211 : : on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
14212 : : are properly set up for the caller. If we fail, we'll continue with
14213 : : a VEC_WIDEN_MULT_LO/HI_EXPR check. */
14214 : 165556 : if (vect_loop
14215 : 42642 : && !nested_in_vect_loop_p (vect_loop, stmt_info)
14216 : 208662 : && supportable_widening_operation (vinfo, VEC_WIDEN_MULT_EVEN_EXPR,
14217 : : stmt_info, vectype_out,
14218 : : vectype_in, code1,
14219 : : code2, multi_step_cvt,
14220 : : interm_types))
14221 : : {
14222 : : /* Elements in a vector with vect_used_by_reduction property cannot
14223 : : be reordered if the use chain with this property does not have the
14224 : : same operation. One such an example is s += a * b, where elements
14225 : : in a and b cannot be reordered. Here we check if the vector defined
14226 : : by STMT is only directly used in the reduction statement. */
14227 : 29215 : tree lhs = gimple_assign_lhs (vect_orig_stmt (stmt_info)->stmt);
14228 : 27639 : stmt_vec_info use_stmt_info = loop_info->lookup_single_use (lhs);
14229 : 27639 : if (use_stmt_info && STMT_VINFO_REDUC_DEF (use_stmt_info))
14230 : : return true;
14231 : : }
14232 : : c1 = VEC_WIDEN_MULT_LO_EXPR;
14233 : : c2 = VEC_WIDEN_MULT_HI_EXPR;
14234 : : break;
14235 : :
14236 : : case DOT_PROD_EXPR:
14237 : 307219 : c1 = DOT_PROD_EXPR;
14238 : 307219 : c2 = DOT_PROD_EXPR;
14239 : : break;
14240 : :
14241 : 0 : case SAD_EXPR:
14242 : 0 : c1 = SAD_EXPR;
14243 : 0 : c2 = SAD_EXPR;
14244 : 0 : break;
14245 : :
14246 : 42642 : case VEC_WIDEN_MULT_EVEN_EXPR:
14247 : : /* Support the recursion induced just above. */
14248 : 42642 : c1 = VEC_WIDEN_MULT_EVEN_EXPR;
14249 : 42642 : c2 = VEC_WIDEN_MULT_ODD_EXPR;
14250 : 42642 : break;
14251 : :
14252 : 9794 : case WIDEN_LSHIFT_EXPR:
14253 : 9794 : c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
14254 : 9794 : c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
14255 : 9794 : break;
14256 : :
14257 : 28693 : CASE_CONVERT:
14258 : 28693 : c1 = VEC_UNPACK_LO_EXPR;
14259 : 28693 : c2 = VEC_UNPACK_HI_EXPR;
14260 : 28693 : break;
14261 : :
14262 : 7126 : case FLOAT_EXPR:
14263 : 7126 : c1 = VEC_UNPACK_FLOAT_LO_EXPR;
14264 : 7126 : c2 = VEC_UNPACK_FLOAT_HI_EXPR;
14265 : 7126 : break;
14266 : :
14267 : 178 : case FIX_TRUNC_EXPR:
14268 : 178 : c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
14269 : 178 : c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
14270 : 178 : break;
14271 : :
14272 : 0 : default:
14273 : 0 : gcc_unreachable ();
14274 : : }
14275 : :
14276 : 307219 : if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
14277 : : std::swap (c1, c2);
14278 : :
14279 : 307219 : if (code == FIX_TRUNC_EXPR)
14280 : : {
14281 : : /* The signedness is determined from output operand. */
14282 : 178 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14283 : 178 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
14284 : : }
14285 : 536958 : else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
14286 : 28693 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14287 : 4674 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14288 : 4674 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
14289 : 253905 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
14290 : : {
14291 : : /* If the input and result modes are the same, a different optab
14292 : : is needed where we pass in the number of units in vectype. */
14293 : : optab1 = vec_unpacks_sbool_lo_optab;
14294 : : optab2 = vec_unpacks_sbool_hi_optab;
14295 : : }
14296 : :
14297 : 307219 : vec_mode = TYPE_MODE (vectype);
14298 : 307219 : if (widening_fn_p (code))
14299 : : {
14300 : : /* If this is an internal fn then we must check whether the target
14301 : : supports either a low-high split or an even-odd split. */
14302 : 53391 : internal_fn ifn = as_internal_fn ((combined_fn) code);
14303 : :
14304 : 53391 : internal_fn lo, hi, even, odd;
14305 : 53391 : lookup_hilo_internal_fn (ifn, &lo, &hi);
14306 : 53391 : if (BYTES_BIG_ENDIAN)
14307 : : std::swap (lo, hi);
14308 : 53391 : *code1 = as_combined_fn (lo);
14309 : 53391 : *code2 = as_combined_fn (hi);
14310 : 53391 : optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
14311 : 53391 : optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
14312 : :
14313 : : /* If we don't support low-high, then check for even-odd. */
14314 : 53391 : if (!optab1
14315 : 53391 : || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
14316 : 0 : || !optab2
14317 : 53391 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
14318 : : {
14319 : 53391 : lookup_evenodd_internal_fn (ifn, &even, &odd);
14320 : 53391 : *code1 = as_combined_fn (even);
14321 : 53391 : *code2 = as_combined_fn (odd);
14322 : 53391 : optab1 = direct_internal_fn_optab (even, {vectype, vectype});
14323 : 53391 : optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
14324 : : }
14325 : : }
14326 : 253828 : else if (code.is_tree_code ())
14327 : : {
14328 : 253828 : if (code == FIX_TRUNC_EXPR)
14329 : : {
14330 : : /* The signedness is determined from output operand. */
14331 : 178 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14332 : 178 : optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
14333 : : }
14334 : 253650 : else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
14335 : 28693 : && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
14336 : 4674 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14337 : 4674 : && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
14338 : 253905 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
14339 : : {
14340 : : /* If the input and result modes are the same, a different optab
14341 : : is needed where we pass in the number of units in vectype. */
14342 : : optab1 = vec_unpacks_sbool_lo_optab;
14343 : : optab2 = vec_unpacks_sbool_hi_optab;
14344 : : }
14345 : : else
14346 : : {
14347 : 253395 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14348 : 253395 : optab2 = optab_for_tree_code (c2, vectype, optab_default);
14349 : : }
14350 : 253828 : *code1 = c1;
14351 : 253828 : *code2 = c2;
14352 : : }
14353 : :
14354 : 307219 : if (!optab1 || !optab2)
14355 : : return false;
14356 : :
14357 : 307219 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
14358 : 307219 : || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
14359 : 152722 : return false;
14360 : :
14361 : :
14362 : 154497 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14363 : 154497 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14364 : : {
14365 : 144451 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14366 : : return true;
14367 : : /* For scalar masks we may have different boolean
14368 : : vector types having the same QImode. Thus we
14369 : : add additional check for elements number. */
14370 : 2667 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
14371 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14372 : : return true;
14373 : : }
14374 : :
14375 : : /* Check if it's a multi-step conversion that can be done using intermediate
14376 : : types. */
14377 : :
14378 : 10146 : prev_type = vectype;
14379 : 10146 : prev_mode = vec_mode;
14380 : :
14381 : 163130 : if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
14382 : : return false;
14383 : :
14384 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14385 : : intermediate steps in promotion sequence. We try
14386 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
14387 : : not. */
14388 : 10094 : interm_types->create (MAX_INTERM_CVT_STEPS);
14389 : 11440 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14390 : : {
14391 : 11440 : intermediate_mode = insn_data[icode1].operand[0].mode;
14392 : 11440 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14393 : 3068 : intermediate_type
14394 : 3068 : = vect_halve_mask_nunits (prev_type, intermediate_mode);
14395 : 8372 : else if (VECTOR_MODE_P (intermediate_mode))
14396 : : {
14397 : 8372 : tree intermediate_element_type
14398 : 8372 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
14399 : 8372 : TYPE_UNSIGNED (prev_type));
14400 : 8372 : intermediate_type
14401 : 8372 : = build_vector_type_for_mode (intermediate_element_type,
14402 : : intermediate_mode);
14403 : 8372 : }
14404 : : else
14405 : 0 : intermediate_type
14406 : 0 : = lang_hooks.types.type_for_mode (intermediate_mode,
14407 : 0 : TYPE_UNSIGNED (prev_type));
14408 : :
14409 : 11440 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14410 : 3068 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
14411 : 3068 : && intermediate_mode == prev_mode
14412 : 11440 : && SCALAR_INT_MODE_P (prev_mode))
14413 : : {
14414 : : /* If the input and result modes are the same, a different optab
14415 : : is needed where we pass in the number of units in vectype. */
14416 : : optab3 = vec_unpacks_sbool_lo_optab;
14417 : : optab4 = vec_unpacks_sbool_hi_optab;
14418 : : }
14419 : : else
14420 : : {
14421 : 11440 : optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
14422 : 11440 : optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
14423 : : }
14424 : :
14425 : 11440 : if (!optab3 || !optab4
14426 : 11440 : || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
14427 : 11440 : || insn_data[icode1].operand[0].mode != intermediate_mode
14428 : 11440 : || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
14429 : 11440 : || insn_data[icode2].operand[0].mode != intermediate_mode
14430 : 11440 : || ((icode1 = optab_handler (optab3, intermediate_mode))
14431 : : == CODE_FOR_nothing)
14432 : 22618 : || ((icode2 = optab_handler (optab4, intermediate_mode))
14433 : : == CODE_FOR_nothing))
14434 : : break;
14435 : :
14436 : 11178 : interm_types->quick_push (intermediate_type);
14437 : 11178 : (*multi_step_cvt)++;
14438 : :
14439 : 11178 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
14440 : 11178 : && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
14441 : : {
14442 : 9852 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14443 : : return true;
14444 : 1907 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
14445 : : TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
14446 : : return true;
14447 : : }
14448 : :
14449 : 1346 : prev_type = intermediate_type;
14450 : 1346 : prev_mode = intermediate_mode;
14451 : : }
14452 : :
14453 : 262 : interm_types->release ();
14454 : 262 : return false;
14455 : : }
14456 : :
14457 : :
14458 : : /* Function supportable_narrowing_operation
14459 : :
14460 : : Check whether an operation represented by the code CODE is a
14461 : : narrowing operation that is supported by the target platform in
14462 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14463 : : and producing a result of type VECTYPE_OUT).
14464 : :
14465 : : Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
14466 : : and FLOAT. This function checks if these operations are supported by
14467 : : the target platform directly via vector tree-codes.
14468 : :
14469 : : Output:
14470 : : - CODE1 is the code of a vector operation to be used when
14471 : : vectorizing the operation, if available.
14472 : : - MULTI_STEP_CVT determines the number of required intermediate steps in
14473 : : case of multi-step conversion (like int->short->char - in that case
14474 : : MULTI_STEP_CVT will be 1).
14475 : : - INTERM_TYPES contains the intermediate type required to perform the
14476 : : narrowing operation (short in the above example). */
14477 : :
14478 : : bool
14479 : 30220 : supportable_narrowing_operation (code_helper code,
14480 : : tree vectype_out, tree vectype_in,
14481 : : code_helper *code1, int *multi_step_cvt,
14482 : : vec<tree> *interm_types)
14483 : : {
14484 : 30220 : machine_mode vec_mode;
14485 : 30220 : enum insn_code icode1;
14486 : 30220 : optab optab1, interm_optab;
14487 : 30220 : tree vectype = vectype_in;
14488 : 30220 : tree narrow_vectype = vectype_out;
14489 : 30220 : enum tree_code c1;
14490 : 30220 : tree intermediate_type, prev_type;
14491 : 30220 : machine_mode intermediate_mode, prev_mode;
14492 : 30220 : int i;
14493 : 30220 : unsigned HOST_WIDE_INT n_elts;
14494 : 30220 : bool uns;
14495 : :
14496 : 30220 : if (!code.is_tree_code ())
14497 : : return false;
14498 : :
14499 : 30220 : *multi_step_cvt = 0;
14500 : 30220 : switch ((tree_code) code)
14501 : : {
14502 : 29274 : CASE_CONVERT:
14503 : 29274 : c1 = VEC_PACK_TRUNC_EXPR;
14504 : 29274 : if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
14505 : 4585 : && VECTOR_BOOLEAN_TYPE_P (vectype)
14506 : 4585 : && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
14507 : 1693 : && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
14508 : 30967 : && n_elts < BITS_PER_UNIT)
14509 : : optab1 = vec_pack_sbool_trunc_optab;
14510 : : else
14511 : 28507 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14512 : : break;
14513 : :
14514 : 796 : case FIX_TRUNC_EXPR:
14515 : 796 : c1 = VEC_PACK_FIX_TRUNC_EXPR;
14516 : : /* The signedness is determined from output operand. */
14517 : 796 : optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
14518 : 796 : break;
14519 : :
14520 : 150 : case FLOAT_EXPR:
14521 : 150 : c1 = VEC_PACK_FLOAT_EXPR;
14522 : 150 : optab1 = optab_for_tree_code (c1, vectype, optab_default);
14523 : 150 : break;
14524 : :
14525 : 0 : default:
14526 : 0 : gcc_unreachable ();
14527 : : }
14528 : :
14529 : 30220 : if (!optab1)
14530 : : return false;
14531 : :
14532 : 30220 : vec_mode = TYPE_MODE (vectype);
14533 : 30220 : if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
14534 : : return false;
14535 : :
14536 : 27254 : *code1 = c1;
14537 : :
14538 : 27254 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14539 : : {
14540 : 17157 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14541 : : return true;
14542 : : /* For scalar masks we may have different boolean
14543 : : vector types having the same QImode. Thus we
14544 : : add additional check for elements number. */
14545 : 3010 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
14546 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14547 : : return true;
14548 : : }
14549 : :
14550 : 10098 : if (code == FLOAT_EXPR)
14551 : : return false;
14552 : :
14553 : : /* Check if it's a multi-step conversion that can be done using intermediate
14554 : : types. */
14555 : 10098 : prev_mode = vec_mode;
14556 : 10098 : prev_type = vectype;
14557 : 10098 : if (code == FIX_TRUNC_EXPR)
14558 : 172 : uns = TYPE_UNSIGNED (vectype_out);
14559 : : else
14560 : 9926 : uns = TYPE_UNSIGNED (vectype);
14561 : :
14562 : : /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
14563 : : conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
14564 : : costly than signed. */
14565 : 10098 : if (code == FIX_TRUNC_EXPR && uns)
14566 : : {
14567 : 76 : enum insn_code icode2;
14568 : :
14569 : 76 : intermediate_type
14570 : 76 : = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
14571 : 76 : interm_optab
14572 : 76 : = optab_for_tree_code (c1, intermediate_type, optab_default);
14573 : 76 : if (interm_optab != unknown_optab
14574 : 76 : && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
14575 : 76 : && insn_data[icode1].operand[0].mode
14576 : 76 : == insn_data[icode2].operand[0].mode)
14577 : : {
14578 : : uns = false;
14579 : : optab1 = interm_optab;
14580 : : icode1 = icode2;
14581 : : }
14582 : : }
14583 : :
14584 : : /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
14585 : : intermediate steps in promotion sequence. We try
14586 : : MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
14587 : 10098 : interm_types->create (MAX_INTERM_CVT_STEPS);
14588 : 21458 : for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
14589 : : {
14590 : 11360 : intermediate_mode = insn_data[icode1].operand[0].mode;
14591 : 11360 : if (VECTOR_BOOLEAN_TYPE_P (prev_type))
14592 : 2015 : intermediate_type
14593 : 2015 : = vect_double_mask_nunits (prev_type, intermediate_mode);
14594 : : else
14595 : 9345 : intermediate_type
14596 : 9345 : = lang_hooks.types.type_for_mode (intermediate_mode, uns);
14597 : 11360 : if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
14598 : 2015 : && VECTOR_BOOLEAN_TYPE_P (prev_type)
14599 : 2015 : && SCALAR_INT_MODE_P (prev_mode)
14600 : 765 : && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
14601 : 12125 : && n_elts < BITS_PER_UNIT)
14602 : : interm_optab = vec_pack_sbool_trunc_optab;
14603 : : else
14604 : 11319 : interm_optab
14605 : 11319 : = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
14606 : : optab_default);
14607 : 41 : if (!interm_optab
14608 : 11360 : || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
14609 : 11360 : || insn_data[icode1].operand[0].mode != intermediate_mode
14610 : 22679 : || ((icode1 = optab_handler (interm_optab, intermediate_mode))
14611 : : == CODE_FOR_nothing))
14612 : : break;
14613 : :
14614 : 10830 : interm_types->quick_push (intermediate_type);
14615 : 10830 : (*multi_step_cvt)++;
14616 : :
14617 : 10830 : if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
14618 : : {
14619 : 9568 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14620 : : return true;
14621 : 996 : if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
14622 : : TYPE_VECTOR_SUBPARTS (narrow_vectype)))
14623 : : return true;
14624 : : }
14625 : :
14626 : 1262 : prev_mode = intermediate_mode;
14627 : 1262 : prev_type = intermediate_type;
14628 : 1262 : optab1 = interm_optab;
14629 : : }
14630 : :
14631 : 530 : interm_types->release ();
14632 : 530 : return false;
14633 : : }
14634 : :
14635 : : /* Function supportable_indirect_convert_operation
14636 : :
14637 : : Check whether an operation represented by the code CODE is single or multi
14638 : : operations that are supported by the target platform in
14639 : : vector form (i.e., when operating on arguments of type VECTYPE_IN
14640 : : producing a result of type VECTYPE_OUT).
14641 : :
14642 : : Convert operations we currently support directly are FIX_TRUNC and FLOAT.
14643 : : This function checks if these operations are supported
14644 : : by the target platform directly (via vector tree-codes).
14645 : :
14646 : : Output:
14647 : : - converts contains some pairs to perform the convert operation,
14648 : : the pair's first is the intermediate type, and its second is the code of
14649 : : a vector operation to be used when converting the operation from the
14650 : : previous type to the intermediate type. */
14651 : : bool
14652 : 69426 : supportable_indirect_convert_operation (code_helper code,
14653 : : tree vectype_out,
14654 : : tree vectype_in,
14655 : : vec<std::pair<tree, tree_code> > &converts,
14656 : : tree op0, slp_tree slp_op0)
14657 : : {
14658 : 69426 : bool found_mode = false;
14659 : 69426 : scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
14660 : 69426 : scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
14661 : 69426 : tree_code tc1, tc2, code1, code2;
14662 : :
14663 : 69426 : tree cvt_type = NULL_TREE;
14664 : 69426 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
14665 : :
14666 : 69426 : if (supportable_convert_operation ((tree_code) code,
14667 : : vectype_out,
14668 : : vectype_in,
14669 : : &tc1))
14670 : : {
14671 : 17598 : converts.safe_push (std::make_pair (vectype_out, tc1));
14672 : 17598 : return true;
14673 : : }
14674 : :
14675 : : /* For conversions between float and integer types try whether
14676 : : we can use intermediate signed integer types to support the
14677 : : conversion. */
14678 : 103656 : if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
14679 : 51828 : && (code == FLOAT_EXPR
14680 : 2960 : || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
14681 : : {
14682 : 376 : bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
14683 : 188 : bool float_expr_p = code == FLOAT_EXPR;
14684 : 188 : unsigned short target_size;
14685 : 188 : scalar_mode intermediate_mode;
14686 : 188 : if (demotion)
14687 : : {
14688 : 84 : intermediate_mode = lhs_mode;
14689 : 84 : target_size = GET_MODE_SIZE (rhs_mode);
14690 : : }
14691 : : else
14692 : : {
14693 : 104 : target_size = GET_MODE_SIZE (lhs_mode);
14694 : 104 : if (!int_mode_for_size
14695 : 104 : (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
14696 : 122 : return false;
14697 : : }
14698 : 188 : code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
14699 : : code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
14700 : 188 : opt_scalar_mode mode_iter;
14701 : 292 : FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
14702 : : {
14703 : 292 : intermediate_mode = mode_iter.require ();
14704 : :
14705 : 584 : if (GET_MODE_SIZE (intermediate_mode) > target_size)
14706 : : break;
14707 : :
14708 : 264 : scalar_mode cvt_mode;
14709 : 264 : if (!int_mode_for_size
14710 : 264 : (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
14711 : : break;
14712 : :
14713 : 234 : cvt_type = build_nonstandard_integer_type
14714 : 234 : (GET_MODE_BITSIZE (cvt_mode), 0);
14715 : :
14716 : : /* Check if the intermediate type can hold OP0's range.
14717 : : When converting from float to integer this is not necessary
14718 : : because values that do not fit the (smaller) target type are
14719 : : unspecified anyway. */
14720 : 234 : if (demotion && float_expr_p)
14721 : : {
14722 : 8 : wide_int op_min_value, op_max_value;
14723 : : /* For vector form, it looks like op0 doesn't have RANGE_INFO.
14724 : : In the future, if it is supported, changes may need to be made
14725 : : to this part, such as checking the RANGE of each element
14726 : : in the vector. */
14727 : 8 : if (slp_op0)
14728 : : {
14729 : 4 : tree def;
14730 : : /* ??? Merge ranges in case of more than one lane. */
14731 : 4 : if (SLP_TREE_LANES (slp_op0) != 1
14732 : 0 : || !(def = vect_get_slp_scalar_def (slp_op0, 0))
14733 : 4 : || !vect_get_range_info (def,
14734 : : &op_min_value, &op_max_value))
14735 : : break;
14736 : : }
14737 : 4 : else if (!op0
14738 : 0 : || TREE_CODE (op0) != SSA_NAME
14739 : 0 : || !SSA_NAME_RANGE_INFO (op0)
14740 : 4 : || !vect_get_range_info (op0, &op_min_value,
14741 : : &op_max_value))
14742 : : break;
14743 : :
14744 : 0 : if (cvt_type == NULL_TREE
14745 : 0 : || (wi::min_precision (op_max_value, SIGNED)
14746 : 0 : > TYPE_PRECISION (cvt_type))
14747 : 0 : || (wi::min_precision (op_min_value, SIGNED)
14748 : 0 : > TYPE_PRECISION (cvt_type)))
14749 : 0 : continue;
14750 : 8 : }
14751 : :
14752 : 226 : cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
14753 : : cvt_type,
14754 : : nelts);
14755 : : /* This should only happened for SLP as long as loop vectorizer
14756 : : only supports same-sized vector. */
14757 : 330 : if (cvt_type == NULL_TREE
14758 : 348 : || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
14759 : 226 : || !supportable_convert_operation ((tree_code) code1,
14760 : : vectype_out,
14761 : : cvt_type, &tc1)
14762 : 396 : || !supportable_convert_operation ((tree_code) code2,
14763 : : cvt_type,
14764 : : vectype_in, &tc2))
14765 : 104 : continue;
14766 : :
14767 : : found_mode = true;
14768 : : break;
14769 : : }
14770 : :
14771 : 188 : if (found_mode)
14772 : : {
14773 : 122 : converts.safe_push (std::make_pair (cvt_type, tc2));
14774 : 122 : if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
14775 : 122 : converts.safe_push (std::make_pair (vectype_out, tc1));
14776 : 122 : return true;
14777 : : }
14778 : : }
14779 : : return false;
14780 : : }
14781 : :
14782 : : /* Generate and return a vector mask of MASK_TYPE such that
14783 : : mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
14784 : : Add the statements to SEQ. */
14785 : :
14786 : : tree
14787 : 0 : vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
14788 : : tree end_index, const char *name)
14789 : : {
14790 : 0 : tree cmp_type = TREE_TYPE (start_index);
14791 : 0 : gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
14792 : : cmp_type, mask_type,
14793 : : OPTIMIZE_FOR_SPEED));
14794 : 0 : gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
14795 : : start_index, end_index,
14796 : : build_zero_cst (mask_type));
14797 : 0 : tree tmp;
14798 : 0 : if (name)
14799 : 0 : tmp = make_temp_ssa_name (mask_type, NULL, name);
14800 : : else
14801 : 0 : tmp = make_ssa_name (mask_type);
14802 : 0 : gimple_call_set_lhs (call, tmp);
14803 : 0 : gimple_seq_add_stmt (seq, call);
14804 : 0 : return tmp;
14805 : : }
14806 : :
14807 : : /* Generate a vector mask of type MASK_TYPE for which index I is false iff
14808 : : J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
14809 : :
14810 : : tree
14811 : 0 : vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
14812 : : tree end_index)
14813 : : {
14814 : 0 : tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
14815 : 0 : return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
14816 : : }
14817 : :
14818 : : /* Try to compute the vector types required to vectorize STMT_INFO,
14819 : : returning true on success and false if vectorization isn't possible.
14820 : : If GROUP_SIZE is nonzero and we're performing BB vectorization,
14821 : : take sure that the number of elements in the vectors is no bigger
14822 : : than GROUP_SIZE.
14823 : :
14824 : : On success:
14825 : :
14826 : : - Set *STMT_VECTYPE_OUT to:
14827 : : - NULL_TREE if the statement doesn't need to be vectorized;
14828 : : - the equivalent of STMT_VINFO_VECTYPE otherwise.
14829 : :
14830 : : - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
14831 : : number of units needed to vectorize STMT_INFO, or NULL_TREE if the
14832 : : statement does not help to determine the overall number of units. */
14833 : :
14834 : : opt_result
14835 : 6134427 : vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
14836 : : tree *stmt_vectype_out,
14837 : : tree *nunits_vectype_out,
14838 : : unsigned int group_size)
14839 : : {
14840 : 6134427 : gimple *stmt = stmt_info->stmt;
14841 : :
14842 : : /* For BB vectorization, we should always have a group size once we've
14843 : : constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
14844 : : are tentative requests during things like early data reference
14845 : : analysis and pattern recognition. */
14846 : 6134427 : if (is_a <bb_vec_info> (vinfo))
14847 : 4619328 : gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
14848 : : else
14849 : : group_size = 0;
14850 : :
14851 : 6134427 : *stmt_vectype_out = NULL_TREE;
14852 : 6134427 : *nunits_vectype_out = NULL_TREE;
14853 : :
14854 : 6134427 : if (gimple_get_lhs (stmt) == NULL_TREE
14855 : : /* Allow vector conditionals through here. */
14856 : 76852 : && !is_a <gcond *> (stmt)
14857 : : /* MASK_STORE and friends have no lhs, but are ok. */
14858 : 6139166 : && !(is_gimple_call (stmt)
14859 : 2324 : && gimple_call_internal_p (stmt)
14860 : 2308 : && internal_store_fn_p (gimple_call_internal_fn (stmt))))
14861 : : {
14862 : 123 : if (is_a <gcall *> (stmt))
14863 : : {
14864 : : /* Ignore calls with no lhs. These must be calls to
14865 : : #pragma omp simd functions, and what vectorization factor
14866 : : it really needs can't be determined until
14867 : : vectorizable_simd_clone_call. */
14868 : 16 : if (dump_enabled_p ())
14869 : 15 : dump_printf_loc (MSG_NOTE, vect_location,
14870 : : "defer to SIMD clone analysis.\n");
14871 : 16 : return opt_result::success ();
14872 : : }
14873 : :
14874 : 107 : return opt_result::failure_at (stmt,
14875 : : "not vectorized: irregular stmt: %G", stmt);
14876 : : }
14877 : :
14878 : 6134304 : tree vectype;
14879 : 6134304 : tree scalar_type = NULL_TREE;
14880 : 6134304 : if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
14881 : : {
14882 : 2612418 : vectype = STMT_VINFO_VECTYPE (stmt_info);
14883 : 2612418 : if (dump_enabled_p ())
14884 : 190020 : dump_printf_loc (MSG_NOTE, vect_location,
14885 : : "precomputed vectype: %T\n", vectype);
14886 : : }
14887 : 3521886 : else if (vect_use_mask_type_p (stmt_info))
14888 : : {
14889 : 169041 : unsigned int precision = stmt_info->mask_precision;
14890 : 169041 : scalar_type = build_nonstandard_integer_type (precision, 1);
14891 : 169041 : vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
14892 : 169041 : if (!vectype)
14893 : 0 : return opt_result::failure_at (stmt, "not vectorized: unsupported"
14894 : : " data-type %T\n", scalar_type);
14895 : 169041 : if (dump_enabled_p ())
14896 : 3565 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14897 : : }
14898 : : else
14899 : : {
14900 : : /* If we got here with a gcond it means that the target had no available vector
14901 : : mode for the scalar type. We can't vectorize so abort. */
14902 : 3352845 : if (is_a <gcond *> (stmt))
14903 : 5 : return opt_result::failure_at (stmt,
14904 : : "not vectorized:"
14905 : : " unsupported data-type for gcond %T\n",
14906 : : scalar_type);
14907 : :
14908 : 3352840 : if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
14909 : 1470575 : scalar_type = TREE_TYPE (DR_REF (dr));
14910 : : else
14911 : 1882265 : scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
14912 : :
14913 : 3352840 : if (dump_enabled_p ())
14914 : : {
14915 : 44570 : if (group_size)
14916 : 7526 : dump_printf_loc (MSG_NOTE, vect_location,
14917 : : "get vectype for scalar type (group size %d):"
14918 : : " %T\n", group_size, scalar_type);
14919 : : else
14920 : 37044 : dump_printf_loc (MSG_NOTE, vect_location,
14921 : : "get vectype for scalar type: %T\n", scalar_type);
14922 : : }
14923 : 3352840 : vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14924 : 3352840 : if (!vectype)
14925 : 187339 : return opt_result::failure_at (stmt,
14926 : : "not vectorized:"
14927 : : " unsupported data-type %T\n",
14928 : : scalar_type);
14929 : :
14930 : 3165501 : if (dump_enabled_p ())
14931 : 44478 : dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
14932 : : }
14933 : :
14934 : 3524562 : if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
14935 : 0 : return opt_result::failure_at (stmt,
14936 : : "not vectorized: vector stmt in loop:%G",
14937 : : stmt);
14938 : :
14939 : 5946960 : *stmt_vectype_out = vectype;
14940 : :
14941 : : /* Don't try to compute scalar types if the stmt produces a boolean
14942 : : vector; use the existing vector type instead. */
14943 : 5946960 : tree nunits_vectype = vectype;
14944 : 5946960 : if (!VECTOR_BOOLEAN_TYPE_P (vectype))
14945 : : {
14946 : : /* The number of units is set according to the smallest scalar
14947 : : type (or the largest vector size, but we only support one
14948 : : vector size per vectorization). */
14949 : 5402524 : scalar_type = vect_get_smallest_scalar_type (stmt_info,
14950 : 5402524 : TREE_TYPE (vectype));
14951 : 5402524 : if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
14952 : : {
14953 : 1026462 : if (dump_enabled_p ())
14954 : 15190 : dump_printf_loc (MSG_NOTE, vect_location,
14955 : : "get vectype for smallest scalar type: %T\n",
14956 : : scalar_type);
14957 : 1026462 : nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
14958 : : group_size);
14959 : 1026462 : if (!nunits_vectype)
14960 : 7 : return opt_result::failure_at
14961 : 7 : (stmt, "not vectorized: unsupported data-type %T\n",
14962 : : scalar_type);
14963 : 1026455 : if (dump_enabled_p ())
14964 : 15190 : dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
14965 : : nunits_vectype);
14966 : : }
14967 : : }
14968 : :
14969 : 5946953 : if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
14970 : 5946953 : TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
14971 : 0 : return opt_result::failure_at (stmt,
14972 : : "Not vectorized: Incompatible number "
14973 : : "of vector subparts between %T and %T\n",
14974 : : nunits_vectype, *stmt_vectype_out);
14975 : :
14976 : 5946953 : if (dump_enabled_p ())
14977 : : {
14978 : 238063 : dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
14979 : 238063 : dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
14980 : 238063 : dump_printf (MSG_NOTE, "\n");
14981 : : }
14982 : :
14983 : 5946953 : *nunits_vectype_out = nunits_vectype;
14984 : 5946953 : return opt_result::success ();
14985 : : }
14986 : :
14987 : : /* Generate and return statement sequence that sets vector length LEN that is:
14988 : :
14989 : : min_of_start_and_end = min (START_INDEX, END_INDEX);
14990 : : left_len = END_INDEX - min_of_start_and_end;
14991 : : rhs = min (left_len, LEN_LIMIT);
14992 : : LEN = rhs;
14993 : :
14994 : : Note: the cost of the code generated by this function is modeled
14995 : : by vect_estimate_min_profitable_iters, so changes here may need
14996 : : corresponding changes there. */
14997 : :
14998 : : gimple_seq
14999 : 0 : vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
15000 : : {
15001 : 0 : gimple_seq stmts = NULL;
15002 : 0 : tree len_type = TREE_TYPE (len);
15003 : 0 : gcc_assert (TREE_TYPE (start_index) == len_type);
15004 : :
15005 : 0 : tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
15006 : 0 : tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
15007 : 0 : tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
15008 : 0 : gimple* stmt = gimple_build_assign (len, rhs);
15009 : 0 : gimple_seq_add_stmt (&stmts, stmt);
15010 : :
15011 : 0 : return stmts;
15012 : : }
15013 : :
|